Commit | Line | Data |
---|---|---|
59eecdfb PM |
1 | /* (C) 1999-2001 Paul `Rusty' Russell |
2 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> | |
3 | * (C) 2007 Patrick McHardy <kaber@trash.net> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | */ | |
9 | ||
10 | #include <linux/types.h> | |
11 | #include <linux/timer.h> | |
12 | #include <linux/module.h> | |
59eecdfb PM |
13 | #include <linux/udp.h> |
14 | #include <linux/seq_file.h> | |
15 | #include <linux/skbuff.h> | |
16 | #include <linux/ipv6.h> | |
17 | #include <net/ip6_checksum.h> | |
18 | #include <net/checksum.h> | |
19 | ||
20 | #include <linux/netfilter.h> | |
21 | #include <linux/netfilter_ipv4.h> | |
22 | #include <linux/netfilter_ipv6.h> | |
23 | #include <net/netfilter/nf_conntrack_l4proto.h> | |
24 | #include <net/netfilter/nf_conntrack_ecache.h> | |
f01ffbd6 | 25 | #include <net/netfilter/nf_log.h> |
59eecdfb | 26 | |
5a41db94 PNA |
27 | enum udplite_conntrack { |
28 | UDPLITE_CT_UNREPLIED, | |
29 | UDPLITE_CT_REPLIED, | |
30 | UDPLITE_CT_MAX | |
31 | }; | |
32 | ||
33 | static unsigned int udplite_timeouts[UDPLITE_CT_MAX] = { | |
34 | [UDPLITE_CT_UNREPLIED] = 30*HZ, | |
35 | [UDPLITE_CT_REPLIED] = 180*HZ, | |
36 | }; | |
59eecdfb | 37 | |
a8021fed G |
38 | static int udplite_net_id __read_mostly; |
39 | struct udplite_net { | |
40 | struct nf_proto_net pn; | |
41 | unsigned int timeouts[UDPLITE_CT_MAX]; | |
42 | }; | |
43 | ||
44 | static inline struct udplite_net *udplite_pernet(struct net *net) | |
45 | { | |
46 | return net_generic(net, udplite_net_id); | |
47 | } | |
48 | ||
09f263cd JE |
49 | static bool udplite_pkt_to_tuple(const struct sk_buff *skb, |
50 | unsigned int dataoff, | |
a31f1adc | 51 | struct net *net, |
09f263cd | 52 | struct nf_conntrack_tuple *tuple) |
59eecdfb | 53 | { |
da3f13c9 JE |
54 | const struct udphdr *hp; |
55 | struct udphdr _hdr; | |
59eecdfb PM |
56 | |
57 | hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); | |
58 | if (hp == NULL) | |
09f263cd | 59 | return false; |
59eecdfb PM |
60 | |
61 | tuple->src.u.udp.port = hp->source; | |
62 | tuple->dst.u.udp.port = hp->dest; | |
09f263cd | 63 | return true; |
59eecdfb PM |
64 | } |
65 | ||
09f263cd JE |
66 | static bool udplite_invert_tuple(struct nf_conntrack_tuple *tuple, |
67 | const struct nf_conntrack_tuple *orig) | |
59eecdfb PM |
68 | { |
69 | tuple->src.u.udp.port = orig->dst.u.udp.port; | |
70 | tuple->dst.u.udp.port = orig->src.u.udp.port; | |
09f263cd | 71 | return true; |
59eecdfb PM |
72 | } |
73 | ||
74 | /* Print out the per-protocol part of the tuple. */ | |
824f1fbe JP |
75 | static void udplite_print_tuple(struct seq_file *s, |
76 | const struct nf_conntrack_tuple *tuple) | |
59eecdfb | 77 | { |
824f1fbe JP |
78 | seq_printf(s, "sport=%hu dport=%hu ", |
79 | ntohs(tuple->src.u.udp.port), | |
80 | ntohs(tuple->dst.u.udp.port)); | |
59eecdfb PM |
81 | } |
82 | ||
2c8503f5 PNA |
83 | static unsigned int *udplite_get_timeouts(struct net *net) |
84 | { | |
a8021fed | 85 | return udplite_pernet(net)->timeouts; |
2c8503f5 PNA |
86 | } |
87 | ||
59eecdfb | 88 | /* Returns verdict for packet, and may modify conntracktype */ |
c88130bc | 89 | static int udplite_packet(struct nf_conn *ct, |
59eecdfb PM |
90 | const struct sk_buff *skb, |
91 | unsigned int dataoff, | |
92 | enum ip_conntrack_info ctinfo, | |
76108cea | 93 | u_int8_t pf, |
2c8503f5 PNA |
94 | unsigned int hooknum, |
95 | unsigned int *timeouts) | |
59eecdfb PM |
96 | { |
97 | /* If we've seen traffic both ways, this is some kind of UDP | |
98 | stream. Extend timeout. */ | |
c88130bc PM |
99 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) { |
100 | nf_ct_refresh_acct(ct, ctinfo, skb, | |
2c8503f5 | 101 | timeouts[UDPLITE_CT_REPLIED]); |
59eecdfb | 102 | /* Also, more likely to be important, and not a probe */ |
c88130bc | 103 | if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status)) |
858b3133 | 104 | nf_conntrack_event_cache(IPCT_ASSURED, ct); |
5a41db94 PNA |
105 | } else { |
106 | nf_ct_refresh_acct(ct, ctinfo, skb, | |
2c8503f5 | 107 | timeouts[UDPLITE_CT_UNREPLIED]); |
5a41db94 | 108 | } |
59eecdfb PM |
109 | return NF_ACCEPT; |
110 | } | |
111 | ||
112 | /* Called when a new connection for this protocol found. */ | |
09f263cd | 113 | static bool udplite_new(struct nf_conn *ct, const struct sk_buff *skb, |
2c8503f5 | 114 | unsigned int dataoff, unsigned int *timeouts) |
59eecdfb | 115 | { |
09f263cd | 116 | return true; |
59eecdfb PM |
117 | } |
118 | ||
8fea97ec | 119 | static int udplite_error(struct net *net, struct nf_conn *tmpl, |
74c51a14 AD |
120 | struct sk_buff *skb, |
121 | unsigned int dataoff, | |
59eecdfb | 122 | enum ip_conntrack_info *ctinfo, |
76108cea | 123 | u_int8_t pf, |
59eecdfb PM |
124 | unsigned int hooknum) |
125 | { | |
126 | unsigned int udplen = skb->len - dataoff; | |
da3f13c9 JE |
127 | const struct udphdr *hdr; |
128 | struct udphdr _hdr; | |
59eecdfb PM |
129 | unsigned int cscov; |
130 | ||
131 | /* Header is too small? */ | |
132 | hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); | |
133 | if (hdr == NULL) { | |
c2a2c7e0 | 134 | if (LOG_INVALID(net, IPPROTO_UDPLITE)) |
30e0c6a6 | 135 | nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL, |
59eecdfb PM |
136 | "nf_ct_udplite: short packet "); |
137 | return -NF_ACCEPT; | |
138 | } | |
139 | ||
140 | cscov = ntohs(hdr->len); | |
141 | if (cscov == 0) | |
142 | cscov = udplen; | |
143 | else if (cscov < sizeof(*hdr) || cscov > udplen) { | |
c2a2c7e0 | 144 | if (LOG_INVALID(net, IPPROTO_UDPLITE)) |
30e0c6a6 | 145 | nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL, |
59eecdfb PM |
146 | "nf_ct_udplite: invalid checksum coverage "); |
147 | return -NF_ACCEPT; | |
148 | } | |
149 | ||
150 | /* UDPLITE mandates checksums */ | |
151 | if (!hdr->check) { | |
c2a2c7e0 | 152 | if (LOG_INVALID(net, IPPROTO_UDPLITE)) |
30e0c6a6 | 153 | nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL, |
59eecdfb PM |
154 | "nf_ct_udplite: checksum missing "); |
155 | return -NF_ACCEPT; | |
156 | } | |
157 | ||
158 | /* Checksum invalid? Ignore. */ | |
c04d0552 | 159 | if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING && |
d63a6507 PM |
160 | nf_checksum_partial(skb, hooknum, dataoff, cscov, IPPROTO_UDP, |
161 | pf)) { | |
c2a2c7e0 | 162 | if (LOG_INVALID(net, IPPROTO_UDPLITE)) |
30e0c6a6 | 163 | nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL, |
d63a6507 PM |
164 | "nf_ct_udplite: bad UDPLite checksum "); |
165 | return -NF_ACCEPT; | |
59eecdfb PM |
166 | } |
167 | ||
168 | return NF_ACCEPT; | |
169 | } | |
170 | ||
50978462 PNA |
171 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
172 | ||
173 | #include <linux/netfilter/nfnetlink.h> | |
174 | #include <linux/netfilter/nfnetlink_cttimeout.h> | |
175 | ||
8264deb8 G |
176 | static int udplite_timeout_nlattr_to_obj(struct nlattr *tb[], |
177 | struct net *net, void *data) | |
50978462 PNA |
178 | { |
179 | unsigned int *timeouts = data; | |
8264deb8 | 180 | struct udplite_net *un = udplite_pernet(net); |
50978462 PNA |
181 | |
182 | /* set default timeouts for UDPlite. */ | |
8264deb8 G |
183 | timeouts[UDPLITE_CT_UNREPLIED] = un->timeouts[UDPLITE_CT_UNREPLIED]; |
184 | timeouts[UDPLITE_CT_REPLIED] = un->timeouts[UDPLITE_CT_REPLIED]; | |
50978462 PNA |
185 | |
186 | if (tb[CTA_TIMEOUT_UDPLITE_UNREPLIED]) { | |
187 | timeouts[UDPLITE_CT_UNREPLIED] = | |
188 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDPLITE_UNREPLIED])) * HZ; | |
189 | } | |
190 | if (tb[CTA_TIMEOUT_UDPLITE_REPLIED]) { | |
191 | timeouts[UDPLITE_CT_REPLIED] = | |
192 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDPLITE_REPLIED])) * HZ; | |
193 | } | |
194 | return 0; | |
195 | } | |
196 | ||
197 | static int | |
198 | udplite_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data) | |
199 | { | |
200 | const unsigned int *timeouts = data; | |
201 | ||
3c60a17b DM |
202 | if (nla_put_be32(skb, CTA_TIMEOUT_UDPLITE_UNREPLIED, |
203 | htonl(timeouts[UDPLITE_CT_UNREPLIED] / HZ)) || | |
204 | nla_put_be32(skb, CTA_TIMEOUT_UDPLITE_REPLIED, | |
205 | htonl(timeouts[UDPLITE_CT_REPLIED] / HZ))) | |
206 | goto nla_put_failure; | |
50978462 PNA |
207 | return 0; |
208 | ||
209 | nla_put_failure: | |
210 | return -ENOSPC; | |
211 | } | |
212 | ||
213 | static const struct nla_policy | |
214 | udplite_timeout_nla_policy[CTA_TIMEOUT_UDPLITE_MAX+1] = { | |
215 | [CTA_TIMEOUT_UDPLITE_UNREPLIED] = { .type = NLA_U32 }, | |
216 | [CTA_TIMEOUT_UDPLITE_REPLIED] = { .type = NLA_U32 }, | |
217 | }; | |
218 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ | |
219 | ||
59eecdfb | 220 | #ifdef CONFIG_SYSCTL |
59eecdfb PM |
221 | static struct ctl_table udplite_sysctl_table[] = { |
222 | { | |
59eecdfb | 223 | .procname = "nf_conntrack_udplite_timeout", |
59eecdfb PM |
224 | .maxlen = sizeof(unsigned int), |
225 | .mode = 0644, | |
6d9f239a | 226 | .proc_handler = proc_dointvec_jiffies, |
59eecdfb PM |
227 | }, |
228 | { | |
59eecdfb | 229 | .procname = "nf_conntrack_udplite_timeout_stream", |
59eecdfb PM |
230 | .maxlen = sizeof(unsigned int), |
231 | .mode = 0644, | |
6d9f239a | 232 | .proc_handler = proc_dointvec_jiffies, |
59eecdfb | 233 | }, |
f8572d8f | 234 | { } |
59eecdfb PM |
235 | }; |
236 | #endif /* CONFIG_SYSCTL */ | |
237 | ||
51b4c824 G |
238 | static int udplite_kmemdup_sysctl_table(struct nf_proto_net *pn, |
239 | struct udplite_net *un) | |
a8021fed | 240 | { |
a8021fed | 241 | #ifdef CONFIG_SYSCTL |
51b4c824 G |
242 | if (pn->ctl_table) |
243 | return 0; | |
244 | ||
245 | pn->ctl_table = kmemdup(udplite_sysctl_table, | |
246 | sizeof(udplite_sysctl_table), | |
247 | GFP_KERNEL); | |
248 | if (!pn->ctl_table) | |
249 | return -ENOMEM; | |
250 | ||
251 | pn->ctl_table[0].data = &un->timeouts[UDPLITE_CT_UNREPLIED]; | |
252 | pn->ctl_table[1].data = &un->timeouts[UDPLITE_CT_REPLIED]; | |
a8021fed | 253 | #endif |
51b4c824 G |
254 | return 0; |
255 | } | |
256 | ||
257 | static int udplite_init_net(struct net *net, u_int16_t proto) | |
258 | { | |
259 | struct udplite_net *un = udplite_pernet(net); | |
260 | struct nf_proto_net *pn = &un->pn; | |
261 | ||
262 | if (!pn->users) { | |
263 | int i; | |
264 | ||
a8021fed G |
265 | for (i = 0 ; i < UDPLITE_CT_MAX; i++) |
266 | un->timeouts[i] = udplite_timeouts[i]; | |
a8021fed | 267 | } |
51b4c824 G |
268 | |
269 | return udplite_kmemdup_sysctl_table(pn, un); | |
a8021fed G |
270 | } |
271 | ||
59eecdfb PM |
272 | static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly = |
273 | { | |
274 | .l3proto = PF_INET, | |
275 | .l4proto = IPPROTO_UDPLITE, | |
276 | .name = "udplite", | |
277 | .pkt_to_tuple = udplite_pkt_to_tuple, | |
278 | .invert_tuple = udplite_invert_tuple, | |
279 | .print_tuple = udplite_print_tuple, | |
59eecdfb | 280 | .packet = udplite_packet, |
2c8503f5 | 281 | .get_timeouts = udplite_get_timeouts, |
59eecdfb PM |
282 | .new = udplite_new, |
283 | .error = udplite_error, | |
c0cd1156 | 284 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
fdf70832 | 285 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
a400c30e | 286 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
fdf70832 | 287 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
f73e924c | 288 | .nla_policy = nf_ct_port_nla_policy, |
59eecdfb | 289 | #endif |
50978462 PNA |
290 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
291 | .ctnl_timeout = { | |
292 | .nlattr_to_obj = udplite_timeout_nlattr_to_obj, | |
293 | .obj_to_nlattr = udplite_timeout_obj_to_nlattr, | |
294 | .nlattr_max = CTA_TIMEOUT_UDPLITE_MAX, | |
295 | .obj_size = sizeof(unsigned int) * | |
296 | CTA_TIMEOUT_UDPLITE_MAX, | |
297 | .nla_policy = udplite_timeout_nla_policy, | |
298 | }, | |
299 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ | |
a8021fed G |
300 | .net_id = &udplite_net_id, |
301 | .init_net = udplite_init_net, | |
59eecdfb PM |
302 | }; |
303 | ||
304 | static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly = | |
305 | { | |
306 | .l3proto = PF_INET6, | |
307 | .l4proto = IPPROTO_UDPLITE, | |
308 | .name = "udplite", | |
309 | .pkt_to_tuple = udplite_pkt_to_tuple, | |
310 | .invert_tuple = udplite_invert_tuple, | |
311 | .print_tuple = udplite_print_tuple, | |
59eecdfb | 312 | .packet = udplite_packet, |
2c8503f5 | 313 | .get_timeouts = udplite_get_timeouts, |
59eecdfb PM |
314 | .new = udplite_new, |
315 | .error = udplite_error, | |
c0cd1156 | 316 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
fdf70832 | 317 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
5ff48294 | 318 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
fdf70832 | 319 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
f73e924c | 320 | .nla_policy = nf_ct_port_nla_policy, |
59eecdfb | 321 | #endif |
50978462 PNA |
322 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
323 | .ctnl_timeout = { | |
324 | .nlattr_to_obj = udplite_timeout_nlattr_to_obj, | |
325 | .obj_to_nlattr = udplite_timeout_obj_to_nlattr, | |
326 | .nlattr_max = CTA_TIMEOUT_UDPLITE_MAX, | |
327 | .obj_size = sizeof(unsigned int) * | |
328 | CTA_TIMEOUT_UDPLITE_MAX, | |
329 | .nla_policy = udplite_timeout_nla_policy, | |
330 | }, | |
331 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ | |
a8021fed G |
332 | .net_id = &udplite_net_id, |
333 | .init_net = udplite_init_net, | |
59eecdfb PM |
334 | }; |
335 | ||
a8021fed | 336 | static int udplite_net_init(struct net *net) |
59eecdfb | 337 | { |
a8021fed G |
338 | int ret = 0; |
339 | ||
c296bb4d | 340 | ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udplite4); |
a8021fed | 341 | if (ret < 0) { |
c296bb4d | 342 | pr_err("nf_conntrack_udplite4: pernet registration failed.\n"); |
a8021fed G |
343 | goto out; |
344 | } | |
c296bb4d | 345 | ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udplite6); |
a8021fed | 346 | if (ret < 0) { |
c296bb4d | 347 | pr_err("nf_conntrack_udplite6: pernet registration failed.\n"); |
a8021fed G |
348 | goto cleanup_udplite4; |
349 | } | |
59eecdfb | 350 | return 0; |
a8021fed G |
351 | |
352 | cleanup_udplite4: | |
c296bb4d | 353 | nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udplite4); |
a8021fed G |
354 | out: |
355 | return ret; | |
356 | } | |
357 | ||
358 | static void udplite_net_exit(struct net *net) | |
359 | { | |
c296bb4d G |
360 | nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udplite6); |
361 | nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udplite4); | |
a8021fed G |
362 | } |
363 | ||
364 | static struct pernet_operations udplite_net_ops = { | |
365 | .init = udplite_net_init, | |
366 | .exit = udplite_net_exit, | |
367 | .id = &udplite_net_id, | |
368 | .size = sizeof(struct udplite_net), | |
369 | }; | |
370 | ||
371 | static int __init nf_conntrack_proto_udplite_init(void) | |
372 | { | |
c296bb4d G |
373 | int ret; |
374 | ||
0d98da5d G |
375 | ret = register_pernet_subsys(&udplite_net_ops); |
376 | if (ret < 0) | |
377 | goto out_pernet; | |
378 | ||
c296bb4d G |
379 | ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udplite4); |
380 | if (ret < 0) | |
381 | goto out_udplite4; | |
382 | ||
383 | ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udplite6); | |
384 | if (ret < 0) | |
385 | goto out_udplite6; | |
386 | ||
c296bb4d | 387 | return 0; |
c296bb4d G |
388 | out_udplite6: |
389 | nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite4); | |
390 | out_udplite4: | |
0d98da5d G |
391 | unregister_pernet_subsys(&udplite_net_ops); |
392 | out_pernet: | |
c296bb4d | 393 | return ret; |
59eecdfb PM |
394 | } |
395 | ||
396 | static void __exit nf_conntrack_proto_udplite_exit(void) | |
397 | { | |
c296bb4d G |
398 | nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite6); |
399 | nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite4); | |
a8021fed | 400 | unregister_pernet_subsys(&udplite_net_ops); |
59eecdfb PM |
401 | } |
402 | ||
403 | module_init(nf_conntrack_proto_udplite_init); | |
404 | module_exit(nf_conntrack_proto_udplite_exit); | |
405 | ||
406 | MODULE_LICENSE("GPL"); |