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];
30 MODULE_PARM(ports, "1-" __MODULE_STRING(MAX_PORTS) "i");
31 MODULE_PARM_DESC(ports, "port numbers of tftp servers");
34 #define DEBUGP(format, args...) printk("%s:%s:" format, \
35 __FILE__, __FUNCTION__ , ## args)
37 #define DEBUGP(format, args...)
40 static int tftp_help(struct sk_buff *skb,
41 struct ip_conntrack *ct,
42 enum ip_conntrack_info ctinfo)
45 struct ip_conntrack_expect *exp;
47 if (skb_copy_bits(skb, skb->nh.iph->ihl * 4 + sizeof(struct udphdr),
48 &tftph, sizeof(tftph)) != 0)
51 switch (ntohs(tftph.opcode)) {
52 /* RRQ and WRQ works the same way */
53 case TFTP_OPCODE_READ:
54 case TFTP_OPCODE_WRITE:
56 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
57 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
59 exp = ip_conntrack_expect_alloc();
63 exp->tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
64 exp->mask.src.ip = 0xffffffff;
65 exp->mask.dst.ip = 0xffffffff;
66 exp->mask.dst.u.udp.port = 0xffff;
67 exp->mask.dst.protonum = 0xffff;
71 DUMP_TUPLE(&exp->tuple);
72 DUMP_TUPLE(&exp->mask);
73 ip_conntrack_expect_related(exp, ct);
75 case TFTP_OPCODE_DATA:
77 DEBUGP("Data/ACK opcode\n");
79 case TFTP_OPCODE_ERROR:
80 DEBUGP("Error opcode\n");
83 DEBUGP("Unknown opcode\n");
88 static struct ip_conntrack_helper tftp[MAX_PORTS];
89 static char tftp_names[MAX_PORTS][10];
91 static void fini(void)
95 for (i = 0 ; i < ports_c; i++) {
96 DEBUGP("unregistering helper for port %d\n",
98 ip_conntrack_helper_unregister(&tftp[i]);
102 static int __init init(void)
110 for (i = 0 ; (i < MAX_PORTS) && ports[i] ; i++) {
111 /* Create helper structure */
112 memset(&tftp[i], 0, sizeof(struct ip_conntrack_helper));
114 tftp[i].tuple.dst.protonum = IPPROTO_UDP;
115 tftp[i].tuple.src.u.udp.port = htons(ports[i]);
116 tftp[i].mask.dst.protonum = 0xFFFF;
117 tftp[i].mask.src.u.udp.port = 0xFFFF;
118 tftp[i].max_expected = 1;
120 tftp[i].flags = IP_CT_HELPER_F_REUSE_EXPECT;
121 tftp[i].me = THIS_MODULE;
122 tftp[i].help = tftp_help;
124 tmpname = &tftp_names[i][0];
125 if (ports[i] == TFTP_PORT)
126 sprintf(tmpname, "tftp");
128 sprintf(tmpname, "tftp-%d", i);
129 tftp[i].name = tmpname;
131 DEBUGP("port #%d: %d\n", i, ports[i]);
133 ret=ip_conntrack_helper_register(&tftp[i]);
135 printk("ERROR registering helper for port %d\n",
145 PROVIDES_CONNTRACK(tftp);