netfilter: nf_nat: add protoff argument to packet mangling functions
[deliverable/linux.git] / net / ipv4 / netfilter / nf_nat_ftp.c
CommitLineData
55a73324
JK
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
23MODULE_LICENSE("GPL");
24MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
25MODULE_DESCRIPTION("ftp NAT helper");
26MODULE_ALIAS("ip_nat_ftp");
27
55a73324
JK
28/* FIXME: Time out? --RR */
29
c299bd53
JP
30static int nf_nat_ftp_fmt_cmd(enum nf_ct_ftp_type type,
31 char *buffer, size_t buflen,
32 __be32 addr, u16 port)
55a73324 33{
c299bd53
JP
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 }
55a73324 49
c299bd53 50 return 0;
55a73324
JK
51}
52
55a73324
JK
53/* So, this packet has hit the connection tracking matching code.
54 Mangle it, and change the expectation to match the new version. */
3db05fea 55static unsigned int nf_nat_ftp(struct sk_buff *skb,
55a73324
JK
56 enum ip_conntrack_info ctinfo,
57 enum nf_ct_ftp_type type,
051966c0 58 unsigned int protoff,
55a73324
JK
59 unsigned int matchoff,
60 unsigned int matchlen,
25b86e05 61 struct nf_conntrack_expect *exp)
55a73324
JK
62{
63 __be32 newip;
64 u_int16_t port;
65 int dir = CTINFO2DIR(ctinfo);
66 struct nf_conn *ct = exp->master;
c299bd53
JP
67 char buffer[sizeof("|1|255.255.255.255|65535|")];
68 unsigned int buflen;
55a73324 69
0d53778e 70 pr_debug("FTP_NAT: type %i, off %u len %u\n", type, matchoff, matchlen);
55a73324
JK
71
72 /* Connection will come from wherever this packet goes, hence !dir */
73 newip = ct->tuplehash[!dir].tuple.dst.u3.ip;
74 exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
75 exp->dir = !dir;
76
77 /* When you see the packet, we need to NAT it the same as the
78 * this one. */
79 exp->expectfn = nf_nat_follow_master;
80
81 /* Try to get same port: if not, try to change it. */
82 for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) {
5b92b61f
PNA
83 int ret;
84
55a73324 85 exp->tuple.dst.u.tcp.port = htons(port);
5b92b61f
PNA
86 ret = nf_ct_expect_related(exp);
87 if (ret == 0)
88 break;
89 else if (ret != -EBUSY) {
90 port = 0;
55a73324 91 break;
5b92b61f 92 }
55a73324
JK
93 }
94
95 if (port == 0)
96 return NF_DROP;
97
c299bd53
JP
98 buflen = nf_nat_ftp_fmt_cmd(type, buffer, sizeof(buffer), newip, port);
99 if (!buflen)
100 goto out;
101
102 pr_debug("calling nf_nat_mangle_tcp_packet\n");
103
051966c0 104 if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff, matchoff,
c299bd53
JP
105 matchlen, buffer, buflen))
106 goto out;
107
55a73324 108 return NF_ACCEPT;
c299bd53
JP
109
110out:
111 nf_ct_unexpect_related(exp);
112 return NF_DROP;
55a73324
JK
113}
114
115static void __exit nf_nat_ftp_fini(void)
116{
a9b3cd7f 117 RCU_INIT_POINTER(nf_nat_ftp_hook, NULL);
55a73324
JK
118 synchronize_rcu();
119}
120
121static int __init nf_nat_ftp_init(void)
122{
d1332e0a 123 BUG_ON(nf_nat_ftp_hook != NULL);
a9b3cd7f 124 RCU_INIT_POINTER(nf_nat_ftp_hook, nf_nat_ftp);
55a73324
JK
125 return 0;
126}
127
128/* Prior to 2.6.11, we had a ports param. No longer, but don't break users. */
129static int warn_set(const char *val, struct kernel_param *kp)
130{
131 printk(KERN_INFO KBUILD_MODNAME
132 ": kernel >= 2.6.10 only uses 'ports' for conntrack modules\n");
133 return 0;
134}
135module_param_call(ports, warn_set, NULL, NULL, 0);
136
137module_init(nf_nat_ftp_init);
138module_exit(nf_nat_ftp_fini);
This page took 0.488552 seconds and 5 git commands to generate.