initial commit with v2.6.9
[linux-2.6.9-moxart.git] / net / ipv4 / netfilter / ip_conntrack_tftp.c
blobd132a3c48d8d8778dc842c0236fedd60b468999e
1 /* (C) 2001-2002 Magnus Boden <mb@ozaba.mine.nu>
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.
7 * Version: 0.0.7
9 * Thu 21 Mar 2002 Harald Welte <laforge@gnumonks.org>
10 * - port to newnat API
14 #include <linux/module.h>
15 #include <linux/ip.h>
16 #include <linux/udp.h>
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>
24 MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
25 MODULE_DESCRIPTION("tftp connection tracking helper");
26 MODULE_LICENSE("GPL");
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");
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
41 static int tftp_help(struct sk_buff *skb,
42 struct ip_conntrack *ct,
43 enum ip_conntrack_info ctinfo)
45 struct tftphdr _tftph, *tfh;
46 struct ip_conntrack_expect *exp;
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;
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);
62 exp = ip_conntrack_expect_alloc();
63 if (exp == NULL)
64 return NF_ACCEPT;
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;
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");
88 return NF_ACCEPT;
91 static struct ip_conntrack_helper tftp[MAX_PORTS];
92 static char tftp_names[MAX_PORTS][10];
94 static void fini(void)
96 int i;
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]);
105 static int __init init(void)
107 int i, ret;
108 char *tmpname;
110 if (ports_c == 0)
111 ports[ports_c++] = TFTP_PORT;
113 for (i = 0; i < ports_c; i++) {
114 /* Create helper structure */
115 memset(&tftp[i], 0, sizeof(struct ip_conntrack_helper));
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;
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;
134 DEBUGP("port #%d: %d\n", i, ports[i]);
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);
144 return(0);
147 PROVIDES_CONNTRACK(tftp);
149 module_init(init);
150 module_exit(fini);