added files
[bcm963xx.git] / kernel / linux / net / ipv4 / netfilter / ip_conntrack_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  */
13
14 #include <linux/module.h>
15 #include <linux/ip.h>
16 #include <linux/udp.h>
17
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>
22
23 MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
24 MODULE_DESCRIPTION("tftp connection tracking helper");
25 MODULE_LICENSE("GPL");
26
27 #define MAX_PORTS 8
28 static int ports[MAX_PORTS];
29 static int ports_c;
30 MODULE_PARM(ports, "1-" __MODULE_STRING(MAX_PORTS) "i");
31 MODULE_PARM_DESC(ports, "port numbers of tftp servers");
32
33 #if 0
34 #define DEBUGP(format, args...) printk("%s:%s:" format, \
35                                        __FILE__, __FUNCTION__ , ## args)
36 #else
37 #define DEBUGP(format, args...)
38 #endif
39
40 static int tftp_help(struct sk_buff *skb,
41                      struct ip_conntrack *ct,
42                      enum ip_conntrack_info ctinfo)
43 {
44         struct tftphdr tftph;
45         struct ip_conntrack_expect *exp;
46
47         if (skb_copy_bits(skb, skb->nh.iph->ihl * 4 + sizeof(struct udphdr),
48                           &tftph, sizeof(tftph)) != 0)
49                 return NF_ACCEPT;
50
51         switch (ntohs(tftph.opcode)) {
52         /* RRQ and WRQ works the same way */
53         case TFTP_OPCODE_READ:
54         case TFTP_OPCODE_WRITE:
55                 DEBUGP("");
56                 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
57                 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
58
59                 exp = ip_conntrack_expect_alloc();
60                 if (exp == NULL)
61                         return NF_ACCEPT;
62
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;
68                 exp->expectfn = NULL;
69
70                 DEBUGP("expect: ");
71                 DUMP_TUPLE(&exp->tuple);
72                 DUMP_TUPLE(&exp->mask);
73                 ip_conntrack_expect_related(exp, ct);
74                 break;
75         case TFTP_OPCODE_DATA:
76         case TFTP_OPCODE_ACK:
77                 DEBUGP("Data/ACK opcode\n");
78                 break;
79         case TFTP_OPCODE_ERROR:
80                 DEBUGP("Error opcode\n");
81                 break;
82         default:
83                 DEBUGP("Unknown opcode\n");
84         }
85         return NF_ACCEPT;
86 }
87
88 static struct ip_conntrack_helper tftp[MAX_PORTS];
89 static char tftp_names[MAX_PORTS][10];
90
91 static void fini(void)
92 {
93         int i;
94
95         for (i = 0 ; i < ports_c; i++) {
96                 DEBUGP("unregistering helper for port %d\n",
97                         ports[i]);
98                 ip_conntrack_helper_unregister(&tftp[i]);
99         } 
100 }
101
102 static int __init init(void)
103 {
104         int i, ret;
105         char *tmpname;
106
107         if (!ports[0])
108                 ports[0]=TFTP_PORT;
109
110         for (i = 0 ; (i < MAX_PORTS) && ports[i] ; i++) {
111                 /* Create helper structure */
112                 memset(&tftp[i], 0, sizeof(struct ip_conntrack_helper));
113
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;
119                 tftp[i].timeout = 0;
120                 tftp[i].flags = IP_CT_HELPER_F_REUSE_EXPECT;
121                 tftp[i].me = THIS_MODULE;
122                 tftp[i].help = tftp_help;
123
124                 tmpname = &tftp_names[i][0];
125                 if (ports[i] == TFTP_PORT)
126                         sprintf(tmpname, "tftp");
127                 else
128                         sprintf(tmpname, "tftp-%d", i);
129                 tftp[i].name = tmpname;
130
131                 DEBUGP("port #%d: %d\n", i, ports[i]);
132
133                 ret=ip_conntrack_helper_register(&tftp[i]);
134                 if (ret) {
135                         printk("ERROR registering helper for port %d\n",
136                                 ports[i]);
137                         fini();
138                         return(ret);
139                 }
140                 ports_c++;
141         }
142         return(0);
143 }
144
145 PROVIDES_CONNTRACK(tftp);
146
147 module_init(init);
148 module_exit(fini);