ip_nat_ftp.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* FTP extension for TCP NAT alteration. */
  2. /* (C) 1999-2001 Paul `Rusty' Russell
  3. * (C) 2002-2004 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/netfilter_ipv4.h>
  11. #include <linux/ip.h>
  12. #include <linux/tcp.h>
  13. #include <linux/moduleparam.h>
  14. #include <net/tcp.h>
  15. #include <linux/netfilter_ipv4/ip_nat.h>
  16. #include <linux/netfilter_ipv4/ip_nat_helper.h>
  17. #include <linux/netfilter_ipv4/ip_nat_rule.h>
  18. #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
  19. #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
  20. MODULE_LICENSE("GPL");
  21. MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
  22. MODULE_DESCRIPTION("ftp NAT helper");
  23. #if 0
  24. #define DEBUGP printk
  25. #else
  26. #define DEBUGP(format, args...)
  27. #endif
  28. /* FIXME: Time out? --RR */
  29. static int
  30. mangle_rfc959_packet(struct sk_buff **pskb,
  31. __be32 newip,
  32. u_int16_t port,
  33. unsigned int matchoff,
  34. unsigned int matchlen,
  35. struct ip_conntrack *ct,
  36. enum ip_conntrack_info ctinfo,
  37. u32 *seq)
  38. {
  39. char buffer[sizeof("nnn,nnn,nnn,nnn,nnn,nnn")];
  40. sprintf(buffer, "%u,%u,%u,%u,%u,%u",
  41. NIPQUAD(newip), port>>8, port&0xFF);
  42. DEBUGP("calling ip_nat_mangle_tcp_packet\n");
  43. *seq += strlen(buffer) - matchlen;
  44. return ip_nat_mangle_tcp_packet(pskb, ct, ctinfo, matchoff,
  45. matchlen, buffer, strlen(buffer));
  46. }
  47. /* |1|132.235.1.2|6275| */
  48. static int
  49. mangle_eprt_packet(struct sk_buff **pskb,
  50. __be32 newip,
  51. u_int16_t port,
  52. unsigned int matchoff,
  53. unsigned int matchlen,
  54. struct ip_conntrack *ct,
  55. enum ip_conntrack_info ctinfo,
  56. u32 *seq)
  57. {
  58. char buffer[sizeof("|1|255.255.255.255|65535|")];
  59. sprintf(buffer, "|1|%u.%u.%u.%u|%u|", NIPQUAD(newip), port);
  60. DEBUGP("calling ip_nat_mangle_tcp_packet\n");
  61. *seq += strlen(buffer) - matchlen;
  62. return ip_nat_mangle_tcp_packet(pskb, ct, ctinfo, matchoff,
  63. matchlen, buffer, strlen(buffer));
  64. }
  65. /* |1|132.235.1.2|6275| */
  66. static int
  67. mangle_epsv_packet(struct sk_buff **pskb,
  68. __be32 newip,
  69. u_int16_t port,
  70. unsigned int matchoff,
  71. unsigned int matchlen,
  72. struct ip_conntrack *ct,
  73. enum ip_conntrack_info ctinfo,
  74. u32 *seq)
  75. {
  76. char buffer[sizeof("|||65535|")];
  77. sprintf(buffer, "|||%u|", port);
  78. DEBUGP("calling ip_nat_mangle_tcp_packet\n");
  79. *seq += strlen(buffer) - matchlen;
  80. return ip_nat_mangle_tcp_packet(pskb, ct, ctinfo, matchoff,
  81. matchlen, buffer, strlen(buffer));
  82. }
  83. static int (*mangle[])(struct sk_buff **, __be32, u_int16_t,
  84. unsigned int,
  85. unsigned int,
  86. struct ip_conntrack *,
  87. enum ip_conntrack_info,
  88. u32 *seq)
  89. = { [IP_CT_FTP_PORT] = mangle_rfc959_packet,
  90. [IP_CT_FTP_PASV] = mangle_rfc959_packet,
  91. [IP_CT_FTP_EPRT] = mangle_eprt_packet,
  92. [IP_CT_FTP_EPSV] = mangle_epsv_packet
  93. };
  94. /* So, this packet has hit the connection tracking matching code.
  95. Mangle it, and change the expectation to match the new version. */
  96. static unsigned int ip_nat_ftp(struct sk_buff **pskb,
  97. enum ip_conntrack_info ctinfo,
  98. enum ip_ct_ftp_type type,
  99. unsigned int matchoff,
  100. unsigned int matchlen,
  101. struct ip_conntrack_expect *exp,
  102. u32 *seq)
  103. {
  104. __be32 newip;
  105. u_int16_t port;
  106. int dir = CTINFO2DIR(ctinfo);
  107. struct ip_conntrack *ct = exp->master;
  108. DEBUGP("FTP_NAT: type %i, off %u len %u\n", type, matchoff, matchlen);
  109. /* Connection will come from wherever this packet goes, hence !dir */
  110. newip = ct->tuplehash[!dir].tuple.dst.ip;
  111. exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
  112. exp->dir = !dir;
  113. /* When you see the packet, we need to NAT it the same as the
  114. * this one. */
  115. exp->expectfn = ip_nat_follow_master;
  116. /* Try to get same port: if not, try to change it. */
  117. for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) {
  118. exp->tuple.dst.u.tcp.port = htons(port);
  119. if (ip_conntrack_expect_related(exp) == 0)
  120. break;
  121. }
  122. if (port == 0)
  123. return NF_DROP;
  124. if (!mangle[type](pskb, newip, port, matchoff, matchlen, ct, ctinfo,
  125. seq)) {
  126. ip_conntrack_unexpect_related(exp);
  127. return NF_DROP;
  128. }
  129. return NF_ACCEPT;
  130. }
  131. static void __exit ip_nat_ftp_fini(void)
  132. {
  133. rcu_assign_pointer(ip_nat_ftp_hook, NULL);
  134. synchronize_rcu();
  135. }
  136. static int __init ip_nat_ftp_init(void)
  137. {
  138. BUG_ON(rcu_dereference(ip_nat_ftp_hook));
  139. rcu_assign_pointer(ip_nat_ftp_hook, ip_nat_ftp);
  140. return 0;
  141. }
  142. /* Prior to 2.6.11, we had a ports param. No longer, but don't break users. */
  143. static int warn_set(const char *val, struct kernel_param *kp)
  144. {
  145. printk(KERN_INFO KBUILD_MODNAME
  146. ": kernel >= 2.6.10 only uses 'ports' for conntrack modules\n");
  147. return 0;
  148. }
  149. module_param_call(ports, warn_set, NULL, NULL, 0);
  150. module_init(ip_nat_ftp_init);
  151. module_exit(ip_nat_ftp_fini);