8a7e1711d14f2bb6f399259fd1dd438fab6c6445
[linux-flexiantxendom0-3.2.10.git] / net / ipv4 / netfilter / ip_conntrack_tftp.c
1 /*
2  * Licensed under GNU GPL version 2 Copyright Magnus Boden <mb@ozaba.mine.nu>
3  * Version: 0.0.7
4  *
5  * Thu 21 Mar 2002 Harald Welte <laforge@gnumonks.org>
6  *      - port to newnat API
7  *
8  */
9
10 #include <linux/module.h>
11 #include <linux/ip.h>
12 #include <linux/udp.h>
13
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>
18
19 MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
20 MODULE_DESCRIPTION("Netfilter connection tracking module for tftp");
21 MODULE_LICENSE("GPL");
22
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
30
31 #if 0
32 #define DEBUGP(format, args...) printk(__FILE__ ":" __FUNCTION__ ": " \
33                                        format, ## args)
34 #else
35 #define DEBUGP(format, args...)
36 #endif
37
38 static int tftp_help(struct sk_buff *skb,
39                      struct ip_conntrack *ct,
40                      enum ip_conntrack_info ctinfo)
41 {
42         struct tftphdr tftph;
43         struct ip_conntrack_expect exp;
44
45         if (skb_copy_bits(skb, skb->nh.iph->ihl * 4 + sizeof(struct udphdr),
46                           &tftph, sizeof(tftph)) != 0)
47                 return -1;
48
49         switch (ntohs(tftph.opcode)) {
50         /* RRQ and WRQ works the same way */
51         case TFTP_OPCODE_READ:
52         case TFTP_OPCODE_WRITE:
53                 DEBUGP("");
54                 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
55                 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
56                 memset(&exp, 0, sizeof(exp));
57
58                 exp.tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
59                 exp.mask.src.ip = 0xffffffff;
60                 exp.mask.dst.ip = 0xffffffff;
61                 exp.mask.dst.u.udp.port = 0xffff;
62                 exp.mask.dst.protonum = 0xffff;
63                 exp.expectfn = NULL;
64
65                 DEBUGP("expect: ");
66                 DUMP_TUPLE(&exp.tuple);
67                 DUMP_TUPLE(&exp.mask);
68                 ip_conntrack_expect_related(ct, &exp);
69                 break;
70         default:
71                 DEBUGP("Unknown opcode\n");
72         }
73         return NF_ACCEPT;
74 }
75
76 static struct ip_conntrack_helper tftp[MAX_PORTS];
77 static char tftp_names[MAX_PORTS][10];
78
79 static void fini(void)
80 {
81         int i;
82
83         for (i = 0 ; i < ports_c; i++) {
84                 DEBUGP("unregistering helper for port %d\n",
85                         ports[i]);
86                 ip_conntrack_helper_unregister(&tftp[i]);
87         } 
88 }
89
90 static int __init init(void)
91 {
92         int i, ret;
93         char *tmpname;
94
95         if (!ports[0])
96                 ports[0]=TFTP_PORT;
97
98         for (i = 0 ; (i < MAX_PORTS) && ports[i] ; i++) {
99                 /* Create helper structure */
100                 memset(&tftp[i], 0, sizeof(struct ip_conntrack_helper));
101
102                 tftp[i].tuple.dst.protonum = IPPROTO_UDP;
103                 tftp[i].tuple.src.u.udp.port = htons(ports[i]);
104                 tftp[i].mask.dst.protonum = 0xFFFF;
105                 tftp[i].mask.src.u.udp.port = 0xFFFF;
106                 tftp[i].max_expected = 1;
107                 tftp[i].timeout = 0;
108                 tftp[i].flags = IP_CT_HELPER_F_REUSE_EXPECT;
109                 tftp[i].me = THIS_MODULE;
110                 tftp[i].help = tftp_help;
111
112                 tmpname = &tftp_names[i][0];
113                 if (ports[i] == TFTP_PORT)
114                         sprintf(tmpname, "tftp");
115                 else
116                         sprintf(tmpname, "tftp-%d", i);
117                 tftp[i].name = tmpname;
118
119                 DEBUGP("port #%d: %d\n", i, ports[i]);
120
121                 ret=ip_conntrack_helper_register(&tftp[i]);
122                 if (ret) {
123                         printk("ERROR registering helper for port %d\n",
124                                 ports[i]);
125                         fini();
126                         return(ret);
127                 }
128                 ports_c++;
129         }
130         return(0);
131 }
132
133 PROVIDES_CONNTRACK(tftp);
134
135 module_init(init);
136 module_exit(fini);