nf_nat_ftp.c 3.9 KB

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