]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/ipv6/netfilter/nf_conntrack_reasm.c
Merge tag 'rpmsg-v4.15' of git://github.com/andersson/remoteproc
[mirror_ubuntu-bionic-kernel.git] / net / ipv6 / netfilter / nf_conntrack_reasm.c
1 /*
2 * IPv6 fragment reassembly for connection tracking
3 *
4 * Copyright (C)2004 USAGI/WIDE Project
5 *
6 * Author:
7 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
8 *
9 * Based on: net/ipv6/reassembly.c
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 */
16
17 #define pr_fmt(fmt) "IPv6-nf: " fmt
18
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
24 #include <linux/jiffies.h>
25 #include <linux/net.h>
26 #include <linux/list.h>
27 #include <linux/netdevice.h>
28 #include <linux/in6.h>
29 #include <linux/ipv6.h>
30 #include <linux/icmpv6.h>
31 #include <linux/random.h>
32 #include <linux/slab.h>
33
34 #include <net/sock.h>
35 #include <net/snmp.h>
36 #include <net/inet_frag.h>
37
38 #include <net/ipv6.h>
39 #include <net/protocol.h>
40 #include <net/transp_v6.h>
41 #include <net/rawv6.h>
42 #include <net/ndisc.h>
43 #include <net/addrconf.h>
44 #include <net/inet_ecn.h>
45 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
46 #include <linux/sysctl.h>
47 #include <linux/netfilter.h>
48 #include <linux/netfilter_ipv6.h>
49 #include <linux/kernel.h>
50 #include <linux/module.h>
51 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
52
53 static const char nf_frags_cache_name[] = "nf-frags";
54
55 struct nf_ct_frag6_skb_cb
56 {
57 struct inet6_skb_parm h;
58 int offset;
59 };
60
61 #define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb *)((skb)->cb))
62
63 static struct inet_frags nf_frags;
64
65 #ifdef CONFIG_SYSCTL
66 static int zero;
67
68 static struct ctl_table nf_ct_frag6_sysctl_table[] = {
69 {
70 .procname = "nf_conntrack_frag6_timeout",
71 .data = &init_net.nf_frag.frags.timeout,
72 .maxlen = sizeof(unsigned int),
73 .mode = 0644,
74 .proc_handler = proc_dointvec_jiffies,
75 },
76 {
77 .procname = "nf_conntrack_frag6_low_thresh",
78 .data = &init_net.nf_frag.frags.low_thresh,
79 .maxlen = sizeof(unsigned int),
80 .mode = 0644,
81 .proc_handler = proc_dointvec_minmax,
82 .extra1 = &zero,
83 .extra2 = &init_net.nf_frag.frags.high_thresh
84 },
85 {
86 .procname = "nf_conntrack_frag6_high_thresh",
87 .data = &init_net.nf_frag.frags.high_thresh,
88 .maxlen = sizeof(unsigned int),
89 .mode = 0644,
90 .proc_handler = proc_dointvec_minmax,
91 .extra1 = &init_net.nf_frag.frags.low_thresh
92 },
93 { }
94 };
95
96 static int nf_ct_frag6_sysctl_register(struct net *net)
97 {
98 struct ctl_table *table;
99 struct ctl_table_header *hdr;
100
101 table = nf_ct_frag6_sysctl_table;
102 if (!net_eq(net, &init_net)) {
103 table = kmemdup(table, sizeof(nf_ct_frag6_sysctl_table),
104 GFP_KERNEL);
105 if (table == NULL)
106 goto err_alloc;
107
108 table[0].data = &net->nf_frag.frags.timeout;
109 table[1].data = &net->nf_frag.frags.low_thresh;
110 table[1].extra2 = &net->nf_frag.frags.high_thresh;
111 table[2].data = &net->nf_frag.frags.high_thresh;
112 table[2].extra1 = &net->nf_frag.frags.low_thresh;
113 table[2].extra2 = &init_net.nf_frag.frags.high_thresh;
114 }
115
116 hdr = register_net_sysctl(net, "net/netfilter", table);
117 if (hdr == NULL)
118 goto err_reg;
119
120 net->nf_frag.sysctl.frags_hdr = hdr;
121 return 0;
122
123 err_reg:
124 if (!net_eq(net, &init_net))
125 kfree(table);
126 err_alloc:
127 return -ENOMEM;
128 }
129
130 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
131 {
132 struct ctl_table *table;
133
134 table = net->nf_frag.sysctl.frags_hdr->ctl_table_arg;
135 unregister_net_sysctl_table(net->nf_frag.sysctl.frags_hdr);
136 if (!net_eq(net, &init_net))
137 kfree(table);
138 }
139
140 #else
141 static int nf_ct_frag6_sysctl_register(struct net *net)
142 {
143 return 0;
144 }
145 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
146 {
147 }
148 #endif
149
150 static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
151 {
152 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
153 }
154
155 static unsigned int nf_hash_frag(__be32 id, const struct in6_addr *saddr,
156 const struct in6_addr *daddr)
157 {
158 net_get_random_once(&nf_frags.rnd, sizeof(nf_frags.rnd));
159 return jhash_3words(ipv6_addr_hash(saddr), ipv6_addr_hash(daddr),
160 (__force u32)id, nf_frags.rnd);
161 }
162
163
164 static unsigned int nf_hashfn(const struct inet_frag_queue *q)
165 {
166 const struct frag_queue *nq;
167
168 nq = container_of(q, struct frag_queue, q);
169 return nf_hash_frag(nq->id, &nq->saddr, &nq->daddr);
170 }
171
172 static void nf_ct_frag6_expire(struct timer_list *t)
173 {
174 struct inet_frag_queue *frag = from_timer(frag, t, timer);
175 struct frag_queue *fq;
176 struct net *net;
177
178 fq = container_of(frag, struct frag_queue, q);
179 net = container_of(fq->q.net, struct net, nf_frag.frags);
180
181 ip6_expire_frag_queue(net, fq, &nf_frags);
182 }
183
184 /* Creation primitives. */
185 static inline struct frag_queue *fq_find(struct net *net, __be32 id,
186 u32 user, struct in6_addr *src,
187 struct in6_addr *dst, int iif, u8 ecn)
188 {
189 struct inet_frag_queue *q;
190 struct ip6_create_arg arg;
191 unsigned int hash;
192
193 arg.id = id;
194 arg.user = user;
195 arg.src = src;
196 arg.dst = dst;
197 arg.iif = iif;
198 arg.ecn = ecn;
199
200 local_bh_disable();
201 hash = nf_hash_frag(id, src, dst);
202
203 q = inet_frag_find(&net->nf_frag.frags, &nf_frags, &arg, hash);
204 local_bh_enable();
205 if (IS_ERR_OR_NULL(q)) {
206 inet_frag_maybe_warn_overflow(q, pr_fmt());
207 return NULL;
208 }
209 return container_of(q, struct frag_queue, q);
210 }
211
212
213 static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
214 const struct frag_hdr *fhdr, int nhoff)
215 {
216 struct sk_buff *prev, *next;
217 unsigned int payload_len;
218 int offset, end;
219 u8 ecn;
220
221 if (fq->q.flags & INET_FRAG_COMPLETE) {
222 pr_debug("Already completed\n");
223 goto err;
224 }
225
226 payload_len = ntohs(ipv6_hdr(skb)->payload_len);
227
228 offset = ntohs(fhdr->frag_off) & ~0x7;
229 end = offset + (payload_len -
230 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
231
232 if ((unsigned int)end > IPV6_MAXPLEN) {
233 pr_debug("offset is too large.\n");
234 return -1;
235 }
236
237 ecn = ip6_frag_ecn(ipv6_hdr(skb));
238
239 if (skb->ip_summed == CHECKSUM_COMPLETE) {
240 const unsigned char *nh = skb_network_header(skb);
241 skb->csum = csum_sub(skb->csum,
242 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
243 0));
244 }
245
246 /* Is this the final fragment? */
247 if (!(fhdr->frag_off & htons(IP6_MF))) {
248 /* If we already have some bits beyond end
249 * or have different end, the segment is corrupted.
250 */
251 if (end < fq->q.len ||
252 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) {
253 pr_debug("already received last fragment\n");
254 goto err;
255 }
256 fq->q.flags |= INET_FRAG_LAST_IN;
257 fq->q.len = end;
258 } else {
259 /* Check if the fragment is rounded to 8 bytes.
260 * Required by the RFC.
261 */
262 if (end & 0x7) {
263 /* RFC2460 says always send parameter problem in
264 * this case. -DaveM
265 */
266 pr_debug("end of fragment not rounded to 8 bytes.\n");
267 return -1;
268 }
269 if (end > fq->q.len) {
270 /* Some bits beyond end -> corruption. */
271 if (fq->q.flags & INET_FRAG_LAST_IN) {
272 pr_debug("last packet already reached.\n");
273 goto err;
274 }
275 fq->q.len = end;
276 }
277 }
278
279 if (end == offset)
280 goto err;
281
282 /* Point into the IP datagram 'data' part. */
283 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
284 pr_debug("queue: message is too short.\n");
285 goto err;
286 }
287 if (pskb_trim_rcsum(skb, end - offset)) {
288 pr_debug("Can't trim\n");
289 goto err;
290 }
291
292 /* Find out which fragments are in front and at the back of us
293 * in the chain of fragments so far. We must know where to put
294 * this fragment, right?
295 */
296 prev = fq->q.fragments_tail;
297 if (!prev || NFCT_FRAG6_CB(prev)->offset < offset) {
298 next = NULL;
299 goto found;
300 }
301 prev = NULL;
302 for (next = fq->q.fragments; next != NULL; next = next->next) {
303 if (NFCT_FRAG6_CB(next)->offset >= offset)
304 break; /* bingo! */
305 prev = next;
306 }
307
308 found:
309 /* RFC5722, Section 4:
310 * When reassembling an IPv6 datagram, if
311 * one or more its constituent fragments is determined to be an
312 * overlapping fragment, the entire datagram (and any constituent
313 * fragments, including those not yet received) MUST be silently
314 * discarded.
315 */
316
317 /* Check for overlap with preceding fragment. */
318 if (prev &&
319 (NFCT_FRAG6_CB(prev)->offset + prev->len) > offset)
320 goto discard_fq;
321
322 /* Look for overlap with succeeding segment. */
323 if (next && NFCT_FRAG6_CB(next)->offset < end)
324 goto discard_fq;
325
326 NFCT_FRAG6_CB(skb)->offset = offset;
327
328 /* Insert this fragment in the chain of fragments. */
329 skb->next = next;
330 if (!next)
331 fq->q.fragments_tail = skb;
332 if (prev)
333 prev->next = skb;
334 else
335 fq->q.fragments = skb;
336
337 if (skb->dev) {
338 fq->iif = skb->dev->ifindex;
339 skb->dev = NULL;
340 }
341 fq->q.stamp = skb->tstamp;
342 fq->q.meat += skb->len;
343 fq->ecn |= ecn;
344 if (payload_len > fq->q.max_size)
345 fq->q.max_size = payload_len;
346 add_frag_mem_limit(fq->q.net, skb->truesize);
347
348 /* The first fragment.
349 * nhoffset is obtained from the first fragment, of course.
350 */
351 if (offset == 0) {
352 fq->nhoffset = nhoff;
353 fq->q.flags |= INET_FRAG_FIRST_IN;
354 }
355
356 return 0;
357
358 discard_fq:
359 inet_frag_kill(&fq->q, &nf_frags);
360 err:
361 return -1;
362 }
363
364 /*
365 * Check if this packet is complete.
366 *
367 * It is called with locked fq, and caller must check that
368 * queue is eligible for reassembly i.e. it is not COMPLETE,
369 * the last and the first frames arrived and all the bits are here.
370 *
371 * returns true if *prev skb has been transformed into the reassembled
372 * skb, false otherwise.
373 */
374 static bool
375 nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *prev, struct net_device *dev)
376 {
377 struct sk_buff *fp, *head = fq->q.fragments;
378 int payload_len;
379 u8 ecn;
380
381 inet_frag_kill(&fq->q, &nf_frags);
382
383 WARN_ON(head == NULL);
384 WARN_ON(NFCT_FRAG6_CB(head)->offset != 0);
385
386 ecn = ip_frag_ecn_table[fq->ecn];
387 if (unlikely(ecn == 0xff))
388 return false;
389
390 /* Unfragmented part is taken from the first segment. */
391 payload_len = ((head->data - skb_network_header(head)) -
392 sizeof(struct ipv6hdr) + fq->q.len -
393 sizeof(struct frag_hdr));
394 if (payload_len > IPV6_MAXPLEN) {
395 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
396 payload_len);
397 return false;
398 }
399
400 /* Head of list must not be cloned. */
401 if (skb_unclone(head, GFP_ATOMIC))
402 return false;
403
404 /* If the first fragment is fragmented itself, we split
405 * it to two chunks: the first with data and paged part
406 * and the second, holding only fragments. */
407 if (skb_has_frag_list(head)) {
408 struct sk_buff *clone;
409 int i, plen = 0;
410
411 clone = alloc_skb(0, GFP_ATOMIC);
412 if (clone == NULL)
413 return false;
414
415 clone->next = head->next;
416 head->next = clone;
417 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
418 skb_frag_list_init(head);
419 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
420 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
421 clone->len = clone->data_len = head->data_len - plen;
422 head->data_len -= clone->len;
423 head->len -= clone->len;
424 clone->csum = 0;
425 clone->ip_summed = head->ip_summed;
426
427 add_frag_mem_limit(fq->q.net, clone->truesize);
428 }
429
430 /* morph head into last received skb: prev.
431 *
432 * This allows callers of ipv6 conntrack defrag to continue
433 * to use the last skb(frag) passed into the reasm engine.
434 * The last skb frag 'silently' turns into the full reassembled skb.
435 *
436 * Since prev is also part of q->fragments we have to clone it first.
437 */
438 if (head != prev) {
439 struct sk_buff *iter;
440
441 fp = skb_clone(prev, GFP_ATOMIC);
442 if (!fp)
443 return false;
444
445 fp->next = prev->next;
446
447 iter = head;
448 while (iter) {
449 if (iter->next == prev) {
450 iter->next = fp;
451 break;
452 }
453 iter = iter->next;
454 }
455
456 skb_morph(prev, head);
457 prev->next = head->next;
458 consume_skb(head);
459 head = prev;
460 }
461
462 /* We have to remove fragment header from datagram and to relocate
463 * header in order to calculate ICV correctly. */
464 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
465 memmove(head->head + sizeof(struct frag_hdr), head->head,
466 (head->data - head->head) - sizeof(struct frag_hdr));
467 head->mac_header += sizeof(struct frag_hdr);
468 head->network_header += sizeof(struct frag_hdr);
469
470 skb_shinfo(head)->frag_list = head->next;
471 skb_reset_transport_header(head);
472 skb_push(head, head->data - skb_network_header(head));
473
474 for (fp = head->next; fp; fp = fp->next) {
475 head->data_len += fp->len;
476 head->len += fp->len;
477 if (head->ip_summed != fp->ip_summed)
478 head->ip_summed = CHECKSUM_NONE;
479 else if (head->ip_summed == CHECKSUM_COMPLETE)
480 head->csum = csum_add(head->csum, fp->csum);
481 head->truesize += fp->truesize;
482 }
483 sub_frag_mem_limit(fq->q.net, head->truesize);
484
485 head->ignore_df = 1;
486 head->next = NULL;
487 head->dev = dev;
488 head->tstamp = fq->q.stamp;
489 ipv6_hdr(head)->payload_len = htons(payload_len);
490 ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
491 IP6CB(head)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size;
492
493 /* Yes, and fold redundant checksum back. 8) */
494 if (head->ip_summed == CHECKSUM_COMPLETE)
495 head->csum = csum_partial(skb_network_header(head),
496 skb_network_header_len(head),
497 head->csum);
498
499 fq->q.fragments = NULL;
500 fq->q.fragments_tail = NULL;
501
502 return true;
503 }
504
505 /*
506 * find the header just before Fragment Header.
507 *
508 * if success return 0 and set ...
509 * (*prevhdrp): the value of "Next Header Field" in the header
510 * just before Fragment Header.
511 * (*prevhoff): the offset of "Next Header Field" in the header
512 * just before Fragment Header.
513 * (*fhoff) : the offset of Fragment Header.
514 *
515 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
516 *
517 */
518 static int
519 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
520 {
521 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
522 const int netoff = skb_network_offset(skb);
523 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
524 int start = netoff + sizeof(struct ipv6hdr);
525 int len = skb->len - start;
526 u8 prevhdr = NEXTHDR_IPV6;
527
528 while (nexthdr != NEXTHDR_FRAGMENT) {
529 struct ipv6_opt_hdr hdr;
530 int hdrlen;
531
532 if (!ipv6_ext_hdr(nexthdr)) {
533 return -1;
534 }
535 if (nexthdr == NEXTHDR_NONE) {
536 pr_debug("next header is none\n");
537 return -1;
538 }
539 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
540 pr_debug("too short\n");
541 return -1;
542 }
543 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
544 BUG();
545 if (nexthdr == NEXTHDR_AUTH)
546 hdrlen = (hdr.hdrlen+2)<<2;
547 else
548 hdrlen = ipv6_optlen(&hdr);
549
550 prevhdr = nexthdr;
551 prev_nhoff = start;
552
553 nexthdr = hdr.nexthdr;
554 len -= hdrlen;
555 start += hdrlen;
556 }
557
558 if (len < 0)
559 return -1;
560
561 *prevhdrp = prevhdr;
562 *prevhoff = prev_nhoff;
563 *fhoff = start;
564
565 return 0;
566 }
567
568 int nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user)
569 {
570 struct net_device *dev = skb->dev;
571 int fhoff, nhoff, ret;
572 struct frag_hdr *fhdr;
573 struct frag_queue *fq;
574 struct ipv6hdr *hdr;
575 u8 prevhdr;
576
577 /* Jumbo payload inhibits frag. header */
578 if (ipv6_hdr(skb)->payload_len == 0) {
579 pr_debug("payload len = 0\n");
580 return 0;
581 }
582
583 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
584 return 0;
585
586 if (!pskb_may_pull(skb, fhoff + sizeof(*fhdr)))
587 return -ENOMEM;
588
589 skb_set_transport_header(skb, fhoff);
590 hdr = ipv6_hdr(skb);
591 fhdr = (struct frag_hdr *)skb_transport_header(skb);
592
593 skb_orphan(skb);
594 fq = fq_find(net, fhdr->identification, user, &hdr->saddr, &hdr->daddr,
595 skb->dev ? skb->dev->ifindex : 0, ip6_frag_ecn(hdr));
596 if (fq == NULL) {
597 pr_debug("Can't find and can't create new queue\n");
598 return -ENOMEM;
599 }
600
601 spin_lock_bh(&fq->q.lock);
602
603 if (nf_ct_frag6_queue(fq, skb, fhdr, nhoff) < 0) {
604 ret = -EINVAL;
605 goto out_unlock;
606 }
607
608 /* after queue has assumed skb ownership, only 0 or -EINPROGRESS
609 * must be returned.
610 */
611 ret = -EINPROGRESS;
612 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
613 fq->q.meat == fq->q.len &&
614 nf_ct_frag6_reasm(fq, skb, dev))
615 ret = 0;
616
617 out_unlock:
618 spin_unlock_bh(&fq->q.lock);
619 inet_frag_put(&fq->q, &nf_frags);
620 return ret;
621 }
622 EXPORT_SYMBOL_GPL(nf_ct_frag6_gather);
623
624 static int nf_ct_net_init(struct net *net)
625 {
626 net->nf_frag.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
627 net->nf_frag.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
628 net->nf_frag.frags.timeout = IPV6_FRAG_TIMEOUT;
629 inet_frags_init_net(&net->nf_frag.frags);
630
631 return nf_ct_frag6_sysctl_register(net);
632 }
633
634 static void nf_ct_net_exit(struct net *net)
635 {
636 nf_ct_frags6_sysctl_unregister(net);
637 inet_frags_exit_net(&net->nf_frag.frags, &nf_frags);
638 }
639
640 static struct pernet_operations nf_ct_net_ops = {
641 .init = nf_ct_net_init,
642 .exit = nf_ct_net_exit,
643 };
644
645 int nf_ct_frag6_init(void)
646 {
647 int ret = 0;
648
649 nf_frags.hashfn = nf_hashfn;
650 nf_frags.constructor = ip6_frag_init;
651 nf_frags.destructor = NULL;
652 nf_frags.qsize = sizeof(struct frag_queue);
653 nf_frags.match = ip6_frag_match;
654 nf_frags.frag_expire = nf_ct_frag6_expire;
655 nf_frags.frags_cache_name = nf_frags_cache_name;
656 ret = inet_frags_init(&nf_frags);
657 if (ret)
658 goto out;
659 ret = register_pernet_subsys(&nf_ct_net_ops);
660 if (ret)
661 inet_frags_fini(&nf_frags);
662
663 out:
664 return ret;
665 }
666
667 void nf_ct_frag6_cleanup(void)
668 {
669 unregister_pernet_subsys(&nf_ct_net_ops);
670 inet_frags_fini(&nf_frags);
671 }