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