import of upstream 2.4.34.4 from kernel.org
[linux-2.4.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(const struct iphdr *iph, size_t len,
39         struct ip_conntrack *ct,
40         enum ip_conntrack_info ctinfo)
41 {
42         struct udphdr *udph = (void *)iph + iph->ihl * 4;
43         struct tftphdr *tftph = (void *)udph + 8;
44         struct ip_conntrack_expect exp;
45         
46         switch (ntohs(tftph->opcode)) {
47         /* RRQ and WRQ works the same way */
48         case TFTP_OPCODE_READ:
49         case TFTP_OPCODE_WRITE:
50                 DEBUGP("");
51                 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
52                 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
53                 memset(&exp, 0, sizeof(exp));
54
55                 exp.tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
56                 exp.mask.src.ip = 0xffffffff;
57                 exp.mask.dst.ip = 0xffffffff;
58                 exp.mask.dst.u.udp.port = 0xffff;
59                 exp.mask.dst.protonum = 0xffff;
60                 exp.expectfn = NULL;
61
62                 DEBUGP("expect: ");
63                 DUMP_TUPLE(&exp.tuple);
64                 DUMP_TUPLE(&exp.mask);
65                 ip_conntrack_expect_related(ct, &exp);
66                 break;
67         case TFTP_OPCODE_DATA:
68         case TFTP_OPCODE_ACK:
69                 DEBUGP("Data/ACK opcode\n");
70                 break;
71         case TFTP_OPCODE_ERROR:
72                 DEBUGP("Error opcode\n");
73                 break;
74         default:
75                 DEBUGP("Unknown opcode\n");
76         }
77         return NF_ACCEPT;
78 }
79
80 static struct ip_conntrack_helper tftp[MAX_PORTS];
81 static char tftp_names[MAX_PORTS][10];
82
83 static void fini(void)
84 {
85         int i;
86
87         for (i = 0 ; i < ports_c; i++) {
88                 DEBUGP("unregistering helper for port %d\n",
89                         ports[i]);
90                 ip_conntrack_helper_unregister(&tftp[i]);
91         } 
92 }
93
94 static int __init init(void)
95 {
96         int i, ret;
97         char *tmpname;
98
99         if (!ports[0])
100                 ports[0]=TFTP_PORT;
101
102         for (i = 0 ; (i < MAX_PORTS) && ports[i] ; i++) {
103                 /* Create helper structure */
104                 tftp[i].tuple.dst.protonum = IPPROTO_UDP;
105                 tftp[i].tuple.src.u.udp.port = htons(ports[i]);
106                 tftp[i].mask.dst.protonum = 0xFFFF;
107                 tftp[i].mask.src.u.udp.port = 0xFFFF;
108                 tftp[i].max_expected = 1;
109                 tftp[i].timeout = 0;
110                 tftp[i].flags = IP_CT_HELPER_F_REUSE_EXPECT;
111                 tftp[i].me = THIS_MODULE;
112                 tftp[i].help = tftp_help;
113
114                 tmpname = &tftp_names[i][0];
115                 if (ports[i] == TFTP_PORT)
116                         sprintf(tmpname, "tftp");
117                 else
118                         sprintf(tmpname, "tftp-%d", i);
119                 tftp[i].name = tmpname;
120
121                 DEBUGP("port #%d: %d\n", i, ports[i]);
122
123                 ret=ip_conntrack_helper_register(&tftp[i]);
124                 if (ret) {
125                         printk("ERROR registering helper for port %d\n",
126                                 ports[i]);
127                         fini();
128                         return(ret);
129                 }
130                 ports_c++;
131         }
132         return(0);
133 }
134
135 module_init(init);
136 module_exit(fini);