~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv4/netfilter/nf_nat_ftp.c

Version: ~ [ linux-5.5-rc7 ] ~ [ linux-5.4.13 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.97 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.166 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.210 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.210 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp