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