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