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