Tomato 1.25
[tomato.git] / release / src / linux / linux / net / ipv4 / netfilter / ip_conntrack_tftp.c
blob4b61ef897da574c192a114b873f2bab95d46598d
1 /*
2 * Licensed under GNU GPL version 2 Copyright Magnus Boden <mb@ozaba.mine.nu>
3 * Version: 0.0.7
5 * Thu 21 Mar 2002 Harald Welte <laforge@gnumonks.org>
6 * - port to newnat API
8 */
10 #include <linux/module.h>
11 #include <linux/ip.h>
12 #include <linux/udp.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter_ipv4/ip_tables.h>
16 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
17 #include <linux/netfilter_ipv4/ip_conntrack_tftp.h>
19 MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
20 MODULE_DESCRIPTION("Netfilter connection tracking module for tftp");
21 MODULE_LICENSE("GPL");
23 #define MAX_PORTS 8
24 static int ports[MAX_PORTS];
25 static int ports_c = 0;
26 #ifdef MODULE_PARM
27 MODULE_PARM(ports, "1-" __MODULE_STRING(MAX_PORTS) "i");
28 MODULE_PARM_DESC(ports, "port numbers of tftp servers");
29 #endif
31 #define DEBUGP(format, args...)
33 static int tftp_help(const struct iphdr *iph, size_t len,
34 struct ip_conntrack *ct,
35 enum ip_conntrack_info ctinfo)
37 struct udphdr *udph = (void *)iph + iph->ihl * 4;
38 struct tftphdr *tftph = (void *)udph + 8;
39 struct ip_conntrack_expect exp;
41 switch (ntohs(tftph->opcode)) {
42 /* RRQ and WRQ works the same way */
43 case TFTP_OPCODE_READ:
44 case TFTP_OPCODE_WRITE:
45 DEBUGP("");
46 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
47 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
48 memset(&exp, 0, sizeof(exp));
50 exp.tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
51 exp.mask.src.ip = 0xffffffff;
52 exp.mask.dst.ip = 0xffffffff;
53 exp.mask.dst.u.udp.port = 0xffff;
54 exp.mask.dst.protonum = 0xffff;
55 exp.expectfn = NULL;
57 DEBUGP("expect: ");
58 DUMP_TUPLE(&exp.tuple);
59 DUMP_TUPLE(&exp.mask);
60 ip_conntrack_expect_related(ct, &exp);
61 break;
62 default:
63 DEBUGP("Unknown opcode\n");
65 return NF_ACCEPT;
68 static struct ip_conntrack_helper tftp[MAX_PORTS];
69 static char tftp_names[MAX_PORTS][10];
71 static void fini(void)
73 int i;
75 for (i = 0 ; i < ports_c; i++) {
76 DEBUGP("unregistering helper for port %d\n",
77 ports[i]);
78 ip_conntrack_helper_unregister(&tftp[i]);
82 static int __init init(void)
84 int i, ret;
85 char *tmpname;
87 if (!ports[0])
88 ports[0]=TFTP_PORT;
90 for (i = 0 ; (i < MAX_PORTS) && ports[i] ; i++) {
91 /* Create helper structure */
92 memset(&tftp[i], 0, sizeof(struct ip_conntrack_helper));
94 tftp[i].tuple.dst.protonum = IPPROTO_UDP;
95 tftp[i].tuple.src.u.udp.port = htons(ports[i]);
96 tftp[i].mask.dst.protonum = 0xFFFF;
97 tftp[i].mask.src.u.udp.port = 0xFFFF;
98 tftp[i].max_expected = 1;
99 tftp[i].timeout = 0;
100 tftp[i].flags = IP_CT_HELPER_F_REUSE_EXPECT;
101 tftp[i].me = THIS_MODULE;
102 tftp[i].help = tftp_help;
104 tmpname = &tftp_names[i][0];
105 if (ports[i] == TFTP_PORT)
106 sprintf(tmpname, "tftp");
107 else
108 sprintf(tmpname, "tftp-%d", i);
109 tftp[i].name = tmpname;
111 DEBUGP("port #%d: %d\n", i, ports[i]);
113 ret=ip_conntrack_helper_register(&tftp[i]);
114 if (ret) {
115 printk("ERROR registering helper for port %d\n",
116 ports[i]);
117 fini();
118 return(ret);
120 ports_c++;
122 return(0);
125 module_init(init);
126 module_exit(fini);