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