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