1 /* (C) 2001-2002 Magnus Boden <mb@ozaba.mine.nu>
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.
9 * Thu 21 Mar 2002 Harald Welte <laforge@gnumonks.org>
10 * - port to newnat API
14 #include <linux/module.h>
16 #include <linux/udp.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4/ip_tables.h>
20 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
21 #include <linux/netfilter_ipv4/ip_conntrack_tftp.h>
23 MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
24 MODULE_DESCRIPTION("tftp connection tracking helper");
25 MODULE_LICENSE("GPL");
28 static int ports[MAX_PORTS];
31 MODULE_PARM(ports, "1-" __MODULE_STRING(MAX_PORTS) "i");
32 MODULE_PARM_DESC(ports, "port numbers of tftp servers");
36 #define DEBUGP(format, args...) printk("%s:%s:" format, \
37 __FILE__, __FUNCTION__ , ## args)
39 #define DEBUGP(format, args...)
42 static int tftp_help(struct sk_buff *skb,
43 struct ip_conntrack *ct,
44 enum ip_conntrack_info ctinfo)
47 struct ip_conntrack_expect *exp;
49 if (skb_copy_bits(skb, skb->nh.iph->ihl * 4 + sizeof(struct udphdr),
50 &tftph, sizeof(tftph)) != 0)
53 switch (ntohs(tftph.opcode)) {
54 /* RRQ and WRQ works the same way */
55 case TFTP_OPCODE_READ:
56 case TFTP_OPCODE_WRITE:
58 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
59 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
61 exp = ip_conntrack_expect_alloc();
65 exp->tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
66 exp->mask.src.ip = 0xffffffff;
67 exp->mask.dst.ip = 0xffffffff;
68 exp->mask.dst.u.udp.port = 0xffff;
69 exp->mask.dst.protonum = 0xffff;
73 DUMP_TUPLE(&exp->tuple);
74 DUMP_TUPLE(&exp->mask);
75 ip_conntrack_expect_related(exp, ct);
77 case TFTP_OPCODE_DATA:
79 DEBUGP("Data/ACK opcode\n");
81 case TFTP_OPCODE_ERROR:
82 DEBUGP("Error opcode\n");
85 DEBUGP("Unknown opcode\n");
90 static struct ip_conntrack_helper tftp[MAX_PORTS];
91 static char tftp_names[MAX_PORTS][10];
93 static void fini(void)
97 for (i = 0 ; i < ports_c; i++) {
98 DEBUGP("unregistering helper for port %d\n",
100 ip_conntrack_helper_unregister(&tftp[i]);
104 static int __init init(void)
112 for (i = 0 ; (i < MAX_PORTS) && ports[i] ; i++) {
113 /* Create helper structure */
114 memset(&tftp[i], 0, sizeof(struct ip_conntrack_helper));
116 tftp[i].tuple.dst.protonum = IPPROTO_UDP;
117 tftp[i].tuple.src.u.udp.port = htons(ports[i]);
118 tftp[i].mask.dst.protonum = 0xFFFF;
119 tftp[i].mask.src.u.udp.port = 0xFFFF;
120 tftp[i].max_expected = 1;
122 tftp[i].flags = IP_CT_HELPER_F_REUSE_EXPECT;
123 tftp[i].me = THIS_MODULE;
124 tftp[i].help = tftp_help;
126 tmpname = &tftp_names[i][0];
127 if (ports[i] == TFTP_PORT)
128 sprintf(tmpname, "tftp");
130 sprintf(tmpname, "tftp-%d", i);
131 tftp[i].name = tmpname;
133 DEBUGP("port #%d: %d\n", i, ports[i]);
135 ret=ip_conntrack_helper_register(&tftp[i]);
137 printk("ERROR registering helper for port %d\n",
147 PROVIDES_CONNTRACK(tftp);