]> err.no Git - linux-2.6/blob - net/netfilter/nf_conntrack_tftp.c
d46a1263fcb560f3438810bbb775a98e3e60b073
[linux-2.6] / net / netfilter / nf_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
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/in.h>
11 #include <linux/udp.h>
12
13 #include <net/netfilter/nf_conntrack.h>
14 #include <net/netfilter/nf_conntrack_tuple.h>
15 #include <net/netfilter/nf_conntrack_expect.h>
16 #include <net/netfilter/nf_conntrack_ecache.h>
17 #include <net/netfilter/nf_conntrack_helper.h>
18 #include <linux/netfilter/nf_conntrack_tftp.h>
19
20 MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
21 MODULE_DESCRIPTION("TFTP connection tracking helper");
22 MODULE_LICENSE("GPL");
23 MODULE_ALIAS("ip_conntrack_tftp");
24
25 #define MAX_PORTS 8
26 static unsigned short ports[MAX_PORTS];
27 static int ports_c;
28 module_param_array(ports, ushort, &ports_c, 0400);
29 MODULE_PARM_DESC(ports, "Port numbers of TFTP servers");
30
31 #if 0
32 #define DEBUGP(format, args...) printk("%s:%s:" format, \
33                                        __FILE__, __FUNCTION__ , ## args)
34 #else
35 #define DEBUGP(format, args...)
36 #endif
37
38 unsigned int (*nf_nat_tftp_hook)(struct sk_buff **pskb,
39                                  enum ip_conntrack_info ctinfo,
40                                  struct nf_conntrack_expect *exp) __read_mostly;
41 EXPORT_SYMBOL_GPL(nf_nat_tftp_hook);
42
43 static int tftp_help(struct sk_buff **pskb,
44                      unsigned int protoff,
45                      struct nf_conn *ct,
46                      enum ip_conntrack_info ctinfo)
47 {
48         struct tftphdr _tftph, *tfh;
49         struct nf_conntrack_expect *exp;
50         struct nf_conntrack_tuple *tuple;
51         unsigned int ret = NF_ACCEPT;
52         int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
53         typeof(nf_nat_tftp_hook) nf_nat_tftp;
54
55         tfh = skb_header_pointer(*pskb, protoff + sizeof(struct udphdr),
56                                  sizeof(_tftph), &_tftph);
57         if (tfh == NULL)
58                 return NF_ACCEPT;
59
60         switch (ntohs(tfh->opcode)) {
61         case TFTP_OPCODE_READ:
62         case TFTP_OPCODE_WRITE:
63                 /* RRQ and WRQ works the same way */
64                 DEBUGP("");
65                 NF_CT_DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
66                 NF_CT_DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
67
68                 exp = nf_conntrack_expect_alloc(ct);
69                 if (exp == NULL)
70                         return NF_DROP;
71                 tuple = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
72                 nf_conntrack_expect_init(exp, family,
73                                          &tuple->src.u3, &tuple->dst.u3,
74                                          IPPROTO_UDP,
75                                          NULL, &tuple->dst.u.udp.port);
76
77                 DEBUGP("expect: ");
78                 NF_CT_DUMP_TUPLE(&exp->tuple);
79                 NF_CT_DUMP_TUPLE(&exp->mask);
80
81                 nf_nat_tftp = rcu_dereference(nf_nat_tftp_hook);
82                 if (nf_nat_tftp && ct->status & IPS_NAT_MASK)
83                         ret = nf_nat_tftp(pskb, ctinfo, exp);
84                 else if (nf_conntrack_expect_related(exp) != 0)
85                         ret = NF_DROP;
86                 nf_conntrack_expect_put(exp);
87                 break;
88         case TFTP_OPCODE_DATA:
89         case TFTP_OPCODE_ACK:
90                 DEBUGP("Data/ACK opcode\n");
91                 break;
92         case TFTP_OPCODE_ERROR:
93                 DEBUGP("Error opcode\n");
94                 break;
95         default:
96                 DEBUGP("Unknown opcode\n");
97         }
98         return ret;
99 }
100
101 static struct nf_conntrack_helper tftp[MAX_PORTS][2] __read_mostly;
102 static char tftp_names[MAX_PORTS][2][sizeof("tftp-65535")] __read_mostly;
103
104 static void nf_conntrack_tftp_fini(void)
105 {
106         int i, j;
107
108         for (i = 0; i < ports_c; i++) {
109                 for (j = 0; j < 2; j++)
110                         nf_conntrack_helper_unregister(&tftp[i][j]);
111         }
112 }
113
114 static int __init nf_conntrack_tftp_init(void)
115 {
116         int i, j, ret;
117         char *tmpname;
118
119         if (ports_c == 0)
120                 ports[ports_c++] = TFTP_PORT;
121
122         for (i = 0; i < ports_c; i++) {
123                 memset(&tftp[i], 0, sizeof(tftp[i]));
124
125                 tftp[i][0].tuple.src.l3num = AF_INET;
126                 tftp[i][1].tuple.src.l3num = AF_INET6;
127                 for (j = 0; j < 2; j++) {
128                         tftp[i][j].tuple.dst.protonum = IPPROTO_UDP;
129                         tftp[i][j].tuple.src.u.udp.port = htons(ports[i]);
130                         tftp[i][j].mask.src.l3num = 0xFFFF;
131                         tftp[i][j].mask.dst.protonum = 0xFF;
132                         tftp[i][j].mask.src.u.udp.port = htons(0xFFFF);
133                         tftp[i][j].max_expected = 1;
134                         tftp[i][j].timeout = 5 * 60; /* 5 minutes */
135                         tftp[i][j].me = THIS_MODULE;
136                         tftp[i][j].help = tftp_help;
137
138                         tmpname = &tftp_names[i][j][0];
139                         if (ports[i] == TFTP_PORT)
140                                 sprintf(tmpname, "tftp");
141                         else
142                                 sprintf(tmpname, "tftp-%u", i);
143                         tftp[i][j].name = tmpname;
144
145                         ret = nf_conntrack_helper_register(&tftp[i][j]);
146                         if (ret) {
147                                 printk("nf_ct_tftp: failed to register helper "
148                                        "for pf: %u port: %u\n",
149                                         tftp[i][j].tuple.src.l3num, ports[i]);
150                                 nf_conntrack_tftp_fini();
151                                 return ret;
152                         }
153                 }
154         }
155         return 0;
156 }
157
158 module_init(nf_conntrack_tftp_init);
159 module_exit(nf_conntrack_tftp_fini);