This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / net / ipv4 / netfilter / ip_nat_tftp.c
1 /* (C) 2001-2002 Magnus Boden <mb@ozaba.mine.nu>
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation.
6  *
7  * Version: 0.0.7
8  *
9  * Thu 21 Mar 2002 Harald Welte <laforge@gnumonks.org>
10  *      - Port to newnat API
11  *
12  * This module currently supports DNAT:
13  * iptables -t nat -A PREROUTING -d x.x.x.x -j DNAT --to-dest x.x.x.y
14  *
15  * and SNAT:
16  * iptables -t nat -A POSTROUTING { -j MASQUERADE , -j SNAT --to-source x.x.x.x }
17  *
18  * It has not been tested with
19  * -j SNAT --to-source x.x.x.x-x.x.x.y since I only have one external ip
20  * If you do test this please let me know if it works or not.
21  *
22  */
23
24 #include <linux/module.h>
25 #include <linux/netfilter_ipv4.h>
26 #include <linux/ip.h>
27 #include <linux/udp.h>
28
29 #include <linux/netfilter.h>
30 #include <linux/netfilter_ipv4/ip_tables.h>
31 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
32 #include <linux/netfilter_ipv4/ip_conntrack_tftp.h>
33 #include <linux/netfilter_ipv4/ip_nat_helper.h>
34 #include <linux/netfilter_ipv4/ip_nat_rule.h>
35
36 MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
37 MODULE_DESCRIPTION("tftp NAT helper");
38 MODULE_LICENSE("GPL");
39
40 #define MAX_PORTS 8
41
42 static int ports[MAX_PORTS];
43 static int ports_c = 0;
44 #ifdef MODULE_PARM
45 MODULE_PARM(ports,"1-" __MODULE_STRING(MAX_PORTS) "i");
46 MODULE_PARM_DESC(ports, "port numbers of tftp servers");
47 #endif
48
49 #if 0
50 #define DEBUGP(format, args...) printk("%s:%s:" format, \
51                                        __FILE__, __FUNCTION__ , ## args)
52 #else
53 #define DEBUGP(format, args...)
54 #endif
55 static unsigned int 
56 tftp_nat_help(struct ip_conntrack *ct,
57               struct ip_conntrack_expect *exp,
58               struct ip_nat_info *info,
59               enum ip_conntrack_info ctinfo,
60               unsigned int hooknum,
61               struct sk_buff **pskb)
62 {
63         int dir = CTINFO2DIR(ctinfo);
64         struct tftphdr tftph;
65         struct ip_conntrack_tuple repl;
66
67         if (!((hooknum == NF_IP_POST_ROUTING && dir == IP_CT_DIR_ORIGINAL)
68               || (hooknum == NF_IP_PRE_ROUTING && dir == IP_CT_DIR_REPLY))) 
69                 return NF_ACCEPT;
70
71         if (!exp) {
72                 DEBUGP("no conntrack expectation to modify\n");
73                 return NF_ACCEPT;
74         }
75
76         if (skb_copy_bits(*pskb, (*pskb)->nh.iph->ihl*4+sizeof(struct udphdr),
77                           &tftph, sizeof(tftph)) != 0)
78                 return NF_DROP;
79
80         switch (ntohs(tftph.opcode)) {
81         /* RRQ and WRQ works the same way */
82         case TFTP_OPCODE_READ:
83         case TFTP_OPCODE_WRITE:
84                 repl = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
85                 DEBUGP("");
86                 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
87                 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
88                 DEBUGP("expecting: ");
89                 DUMP_TUPLE(&repl);
90                 DUMP_TUPLE(&exp->mask);
91                 ip_conntrack_change_expect(exp, &repl);
92                 break;
93         default:
94                 DEBUGP("Unknown opcode\n");
95         }               
96
97         return NF_ACCEPT;
98 }
99
100 static unsigned int 
101 tftp_nat_expected(struct sk_buff **pskb,
102                   unsigned int hooknum,
103                   struct ip_conntrack *ct, 
104                   struct ip_nat_info *info) 
105 {
106         const struct ip_conntrack *master = ct->master->expectant;
107         const struct ip_conntrack_tuple *orig = 
108                         &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
109         struct ip_nat_multi_range mr;
110 #if 0
111         const struct ip_conntrack_tuple *repl =
112                         &master->tuplehash[IP_CT_DIR_REPLY].tuple;
113         struct udphdr udph;
114
115         if (skb_copy_bits(*pskb,(*pskb)->nh.iph->ihl*4,&udph,sizeof(udph))!=0)
116                 return NF_DROP;
117 #endif
118
119         IP_NF_ASSERT(info);
120         IP_NF_ASSERT(master);
121         IP_NF_ASSERT(!(info->initialized & (1 << HOOK2MANIP(hooknum))));
122
123         mr.rangesize = 1;
124         mr.range[0].flags = IP_NAT_RANGE_MAP_IPS;
125
126         if (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC) {
127                 mr.range[0].min_ip = mr.range[0].max_ip = orig->dst.ip; 
128                 DEBUGP("orig: %u.%u.%u.%u:%u <-> %u.%u.%u.%u:%u "
129                         "newsrc: %u.%u.%u.%u\n",
130                         NIPQUAD((*pskb)->nh.iph->saddr), ntohs(udph.source),
131                         NIPQUAD((*pskb)->nh.iph->daddr), ntohs(udph.dest),
132                         NIPQUAD(orig->dst.ip));
133         } else {
134                 mr.range[0].min_ip = mr.range[0].max_ip = orig->src.ip;
135                 mr.range[0].min.udp.port = mr.range[0].max.udp.port = 
136                                                         orig->src.u.udp.port;
137                 mr.range[0].flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
138
139                 DEBUGP("orig: %u.%u.%u.%u:%u <-> %u.%u.%u.%u:%u "
140                         "newdst: %u.%u.%u.%u:%u\n",
141                         NIPQUAD((*pskb)->nh.iph->saddr), ntohs(udph.source),
142                         NIPQUAD((*pskb)->nh.iph->daddr), ntohs(udph.dest),
143                         NIPQUAD(orig->src.ip), ntohs(orig->src.u.udp.port));
144         }
145
146         return ip_nat_setup_info(ct,&mr,hooknum);
147 }
148
149 static struct ip_nat_helper tftp[MAX_PORTS];
150 static char tftp_names[MAX_PORTS][10];
151
152 static void fini(void)
153 {
154         int i;
155
156         for (i = 0 ; i < ports_c; i++) {
157                 DEBUGP("unregistering helper for port %d\n", ports[i]);
158                 ip_nat_helper_unregister(&tftp[i]);
159         }
160 }
161
162 static int __init init(void)
163 {
164         int i, ret = 0;
165         char *tmpname;
166
167         if (!ports[0])
168                 ports[0] = TFTP_PORT;
169
170         for (i = 0 ; (i < MAX_PORTS) && ports[i] ; i++) {
171                 memset(&tftp[i], 0, sizeof(struct ip_nat_helper));
172
173                 tftp[i].tuple.dst.protonum = IPPROTO_UDP;
174                 tftp[i].tuple.src.u.udp.port = htons(ports[i]);
175                 tftp[i].mask.dst.protonum = 0xFFFF;
176                 tftp[i].mask.src.u.udp.port = 0xFFFF;
177                 tftp[i].help = tftp_nat_help;
178                 tftp[i].flags = 0;
179                 tftp[i].me = THIS_MODULE;
180                 tftp[i].expect = tftp_nat_expected;
181
182                 tmpname = &tftp_names[i][0];
183                 if (ports[i] == TFTP_PORT)
184                         sprintf(tmpname, "tftp");
185                 else
186                         sprintf(tmpname, "tftp-%d", i);
187                 tftp[i].name = tmpname;
188                 
189                 DEBUGP("ip_nat_tftp: registering for port %d: name %s\n",
190                         ports[i], tftp[i].name);
191                 ret = ip_nat_helper_register(&tftp[i]);
192
193                 if (ret) {
194                         printk("ip_nat_tftp: unable to register for port %d\n",
195                                 ports[i]);
196                         fini();
197                         return ret;
198                 }
199                 ports_c++;
200         }
201         return ret;
202 }
203
204 module_init(init);
205 module_exit(fini);