nf_nat_ftp.c 4.8 KB

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