nf_nat_ftp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* FTP extension for TCP NAT alteration. */
  3. /* (C) 1999-2001 Paul `Rusty' Russell
  4. * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/module.h>
  8. #include <linux/moduleparam.h>
  9. #include <linux/inet.h>
  10. #include <linux/tcp.h>
  11. #include <linux/netfilter_ipv4.h>
  12. #include <net/netfilter/nf_nat.h>
  13. #include <net/netfilter/nf_nat_helper.h>
  14. #include <net/netfilter/nf_conntrack_helper.h>
  15. #include <net/netfilter/nf_conntrack_expect.h>
  16. #include <linux/netfilter/nf_conntrack_ftp.h>
  17. #define NAT_HELPER_NAME "ftp"
  18. MODULE_LICENSE("GPL");
  19. MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
  20. MODULE_DESCRIPTION("ftp NAT helper");
  21. MODULE_ALIAS_NF_NAT_HELPER(NAT_HELPER_NAME);
  22. /* FIXME: Time out? --RR */
  23. static struct nf_conntrack_nat_helper nat_helper_ftp =
  24. NF_CT_NAT_HELPER_INIT(NAT_HELPER_NAME);
  25. static int nf_nat_ftp_fmt_cmd(struct nf_conn *ct, enum nf_ct_ftp_type type,
  26. char *buffer, size_t buflen,
  27. union nf_inet_addr *addr, u16 port)
  28. {
  29. switch (type) {
  30. case NF_CT_FTP_PORT:
  31. case NF_CT_FTP_PASV:
  32. return snprintf(buffer, buflen, "%u,%u,%u,%u,%u,%u",
  33. ((unsigned char *)&addr->ip)[0],
  34. ((unsigned char *)&addr->ip)[1],
  35. ((unsigned char *)&addr->ip)[2],
  36. ((unsigned char *)&addr->ip)[3],
  37. port >> 8,
  38. port & 0xFF);
  39. case NF_CT_FTP_EPRT:
  40. if (nf_ct_l3num(ct) == NFPROTO_IPV4)
  41. return snprintf(buffer, buflen, "|1|%pI4|%u|",
  42. &addr->ip, port);
  43. else
  44. return snprintf(buffer, buflen, "|2|%pI6|%u|",
  45. &addr->ip6, port);
  46. case NF_CT_FTP_EPSV:
  47. return snprintf(buffer, buflen, "|||%u|", port);
  48. }
  49. return 0;
  50. }
  51. /* So, this packet has hit the connection tracking matching code.
  52. Mangle it, and change the expectation to match the new version. */
  53. static unsigned int nf_nat_ftp(struct sk_buff *skb,
  54. enum ip_conntrack_info ctinfo,
  55. enum nf_ct_ftp_type type,
  56. unsigned int protoff,
  57. unsigned int matchoff,
  58. unsigned int matchlen,
  59. struct nf_conntrack_expect *exp)
  60. {
  61. union nf_inet_addr newaddr;
  62. u_int16_t port;
  63. int dir = CTINFO2DIR(ctinfo);
  64. struct nf_conn *ct = exp->master;
  65. char buffer[sizeof("|1||65535|") + INET6_ADDRSTRLEN];
  66. unsigned int buflen;
  67. pr_debug("type %i, off %u len %u\n", type, matchoff, matchlen);
  68. /* Connection will come from wherever this packet goes, hence !dir */
  69. newaddr = ct->tuplehash[!dir].tuple.dst.u3;
  70. exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
  71. exp->dir = !dir;
  72. /* When you see the packet, we need to NAT it the same as the
  73. * this one. */
  74. exp->expectfn = nf_nat_follow_master;
  75. /* Try to get same port: if not, try to change it. */
  76. for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) {
  77. int ret;
  78. exp->tuple.dst.u.tcp.port = htons(port);
  79. ret = nf_ct_expect_related(exp, 0);
  80. if (ret == 0)
  81. break;
  82. else if (ret != -EBUSY) {
  83. port = 0;
  84. break;
  85. }
  86. }
  87. if (port == 0) {
  88. nf_ct_helper_log(skb, ct, "all ports in use");
  89. return NF_DROP;
  90. }
  91. buflen = nf_nat_ftp_fmt_cmd(ct, type, buffer, sizeof(buffer),
  92. &newaddr, port);
  93. if (!buflen)
  94. goto out;
  95. pr_debug("calling nf_nat_mangle_tcp_packet\n");
  96. if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff, matchoff,
  97. matchlen, buffer, buflen))
  98. goto out;
  99. return NF_ACCEPT;
  100. out:
  101. nf_ct_helper_log(skb, ct, "cannot mangle packet");
  102. nf_ct_unexpect_related(exp);
  103. return NF_DROP;
  104. }
  105. static void __exit nf_nat_ftp_fini(void)
  106. {
  107. nf_nat_helper_unregister(&nat_helper_ftp);
  108. RCU_INIT_POINTER(nf_nat_ftp_hook, NULL);
  109. synchronize_rcu();
  110. }
  111. static int __init nf_nat_ftp_init(void)
  112. {
  113. BUG_ON(nf_nat_ftp_hook != NULL);
  114. nf_nat_helper_register(&nat_helper_ftp);
  115. RCU_INIT_POINTER(nf_nat_ftp_hook, nf_nat_ftp);
  116. return 0;
  117. }
  118. /* Prior to 2.6.11, we had a ports param. No longer, but don't break users. */
  119. static int warn_set(const char *val, const struct kernel_param *kp)
  120. {
  121. pr_info("kernel >= 2.6.10 only uses 'ports' for conntrack modules\n");
  122. return 0;
  123. }
  124. module_param_call(ports, warn_set, NULL, NULL, 0);
  125. module_init(nf_nat_ftp_init);
  126. module_exit(nf_nat_ftp_fini);