2 * Licensed under GNU GPL version 2 Copyright Magnus Boden <mb@ozaba.mine.nu>
5 * Thu 21 Mar 2002 Harald Welte <laforge@gnumonks.org>
8 * This module currently supports DNAT:
9 * iptables -t nat -A PREROUTING -d x.x.x.x -j DNAT --to-dest x.x.x.y
12 * iptables -t nat -A POSTROUTING { -j MASQUERADE , -j SNAT --to-source x.x.x.x }
14 * It has not been tested with
15 * -j SNAT --to-source x.x.x.x-x.x.x.y since I only have one external ip
16 * If you do test this please let me know if it works or not.
20 #include <linux/module.h>
21 #include <linux/netfilter_ipv4.h>
23 #include <linux/udp.h>
25 #include <linux/netfilter.h>
26 #include <linux/netfilter_ipv4/ip_tables.h>
27 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
28 #include <linux/netfilter_ipv4/ip_conntrack_tftp.h>
29 #include <linux/netfilter_ipv4/ip_nat_helper.h>
30 #include <linux/netfilter_ipv4/ip_nat_rule.h>
32 MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
33 MODULE_DESCRIPTION("Netfilter NAT helper for tftp");
34 MODULE_LICENSE("GPL");
38 static int ports[MAX_PORTS];
39 static int ports_c = 0;
41 MODULE_PARM(ports,"1-" __MODULE_STRING(MAX_PORTS) "i");
42 MODULE_PARM_DESC(ports, "port numbers of tftp servers");
46 #define DEBUGP(format, args...) printk(__FILE__ ":" __FUNCTION__ ": " \
49 #define DEBUGP(format, args...)
52 tftp_nat_help(struct ip_conntrack *ct,
53 struct ip_conntrack_expect *exp,
54 struct ip_nat_info *info,
55 enum ip_conntrack_info ctinfo,
57 struct sk_buff **pskb)
59 int dir = CTINFO2DIR(ctinfo);
60 struct iphdr *iph = (*pskb)->nh.iph;
61 struct udphdr *udph = (void *)iph + iph->ihl * 4;
62 struct tftphdr *tftph = (void *)udph + 8;
63 struct ip_conntrack_tuple repl;
65 if (!((hooknum == NF_IP_POST_ROUTING && dir == IP_CT_DIR_ORIGINAL)
66 || (hooknum == NF_IP_PRE_ROUTING && dir == IP_CT_DIR_REPLY)))
70 DEBUGP("no conntrack expectation to modify\n");
74 switch (ntohs(tftph->opcode)) {
75 /* RRQ and WRQ works the same way */
76 case TFTP_OPCODE_READ:
77 case TFTP_OPCODE_WRITE:
78 repl = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
80 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
81 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
82 DEBUGP("expecting: ");
84 DUMP_TUPLE(&exp->mask);
85 ip_conntrack_change_expect(exp, &repl);
88 DEBUGP("Unknown opcode\n");
95 tftp_nat_expected(struct sk_buff **pskb,
97 struct ip_conntrack *ct,
98 struct ip_nat_info *info)
100 const struct ip_conntrack *master = ct->master->expectant;
101 const struct ip_conntrack_tuple *orig =
102 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
103 struct ip_nat_multi_range mr;
105 const struct ip_conntrack_tuple *repl =
106 &master->tuplehash[IP_CT_DIR_REPLY].tuple;
107 struct iphdr *iph = (*pskb)->nh.iph;
108 struct udphdr *udph = (void *)iph + iph->ihl*4;
112 IP_NF_ASSERT(master);
113 IP_NF_ASSERT(!(info->initialized & (1 << HOOK2MANIP(hooknum))));
116 mr.range[0].flags = IP_NAT_RANGE_MAP_IPS;
118 if (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC) {
119 mr.range[0].min_ip = mr.range[0].max_ip = orig->dst.ip;
120 DEBUGP("orig: %u.%u.%u.%u:%u <-> %u.%u.%u.%u:%u "
121 "newsrc: %u.%u.%u.%u\n",
122 NIPQUAD((*pskb)->nh.iph->saddr), ntohs(udph->source),
123 NIPQUAD((*pskb)->nh.iph->daddr), ntohs(udph->dest),
124 NIPQUAD(orig->dst.ip));
126 mr.range[0].min_ip = mr.range[0].max_ip = orig->src.ip;
127 mr.range[0].min.udp.port = mr.range[0].max.udp.port =
128 orig->src.u.udp.port;
129 mr.range[0].flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
131 DEBUGP("orig: %u.%u.%u.%u:%u <-> %u.%u.%u.%u:%u "
132 "newdst: %u.%u.%u.%u:%u\n",
133 NIPQUAD((*pskb)->nh.iph->saddr), ntohs(udph->source),
134 NIPQUAD((*pskb)->nh.iph->daddr), ntohs(udph->dest),
135 NIPQUAD(orig->src.ip), ntohs(orig->src.u.udp.port));
138 return ip_nat_setup_info(ct,&mr,hooknum);
141 static struct ip_nat_helper tftp[MAX_PORTS];
142 static char tftp_names[MAX_PORTS][10];
144 static void fini(void)
148 for (i = 0 ; i < ports_c; i++) {
149 DEBUGP("unregistering helper for port %d\n", ports[i]);
150 ip_nat_helper_unregister(&tftp[i]);
154 static int __init init(void)
160 ports[0] = TFTP_PORT;
162 for (i = 0 ; (i < MAX_PORTS) && ports[i] ; i++) {
163 tftp[i].tuple.dst.protonum = IPPROTO_UDP;
164 tftp[i].tuple.src.u.udp.port = htons(ports[i]);
165 tftp[i].mask.dst.protonum = 0xFFFF;
166 tftp[i].mask.src.u.udp.port = 0xFFFF;
167 tftp[i].help = tftp_nat_help;
169 tftp[i].me = THIS_MODULE;
170 tftp[i].expect = tftp_nat_expected;
172 tmpname = &tftp_names[i][0];
173 if (ports[i] == TFTP_PORT)
174 sprintf(tmpname, "tftp");
176 sprintf(tmpname, "tftp-%d", i);
177 tftp[i].name = tmpname;
179 DEBUGP("ip_nat_tftp: registering for port %d: name %s\n",
180 ports[i], tftp[i].name);
181 ret = ip_nat_helper_register(&tftp[i]);
184 printk("ip_nat_tftp: unable to register for port %d\n",