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