GNU Linux-libre 4.19.211-gnu1
[releases.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/ipv6_frag.h>
37
38 #include <net/protocol.h>
39 #include <net/transp_v6.h>
40 #include <net/rawv6.h>
41 #include <net/ndisc.h>
42 #include <net/addrconf.h>
43 #include <net/inet_ecn.h>
44 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
45 #include <linux/sysctl.h>
46 #include <linux/netfilter.h>
47 #include <linux/netfilter_ipv6.h>
48 #include <linux/kernel.h>
49 #include <linux/module.h>
50 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
51
52 static const char nf_frags_cache_name[] = "nf-frags";
53
54 static struct inet_frags nf_frags;
55
56 #ifdef CONFIG_SYSCTL
57
58 static struct ctl_table nf_ct_frag6_sysctl_table[] = {
59         {
60                 .procname       = "nf_conntrack_frag6_timeout",
61                 .data           = &init_net.nf_frag.frags.timeout,
62                 .maxlen         = sizeof(unsigned int),
63                 .mode           = 0644,
64                 .proc_handler   = proc_dointvec_jiffies,
65         },
66         {
67                 .procname       = "nf_conntrack_frag6_low_thresh",
68                 .data           = &init_net.nf_frag.frags.low_thresh,
69                 .maxlen         = sizeof(unsigned long),
70                 .mode           = 0644,
71                 .proc_handler   = proc_doulongvec_minmax,
72                 .extra2         = &init_net.nf_frag.frags.high_thresh
73         },
74         {
75                 .procname       = "nf_conntrack_frag6_high_thresh",
76                 .data           = &init_net.nf_frag.frags.high_thresh,
77                 .maxlen         = sizeof(unsigned long),
78                 .mode           = 0644,
79                 .proc_handler   = proc_doulongvec_minmax,
80                 .extra1         = &init_net.nf_frag.frags.low_thresh
81         },
82         { }
83 };
84
85 static int nf_ct_frag6_sysctl_register(struct net *net)
86 {
87         struct ctl_table *table;
88         struct ctl_table_header *hdr;
89
90         table = nf_ct_frag6_sysctl_table;
91         if (!net_eq(net, &init_net)) {
92                 table = kmemdup(table, sizeof(nf_ct_frag6_sysctl_table),
93                                 GFP_KERNEL);
94                 if (table == NULL)
95                         goto err_alloc;
96
97                 table[0].data = &net->nf_frag.frags.timeout;
98                 table[1].data = &net->nf_frag.frags.low_thresh;
99                 table[1].extra2 = &net->nf_frag.frags.high_thresh;
100                 table[2].data = &net->nf_frag.frags.high_thresh;
101                 table[2].extra1 = &net->nf_frag.frags.low_thresh;
102                 table[2].extra2 = &init_net.nf_frag.frags.high_thresh;
103         }
104
105         hdr = register_net_sysctl(net, "net/netfilter", table);
106         if (hdr == NULL)
107                 goto err_reg;
108
109         net->nf_frag_frags_hdr = hdr;
110         return 0;
111
112 err_reg:
113         if (!net_eq(net, &init_net))
114                 kfree(table);
115 err_alloc:
116         return -ENOMEM;
117 }
118
119 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
120 {
121         struct ctl_table *table;
122
123         table = net->nf_frag_frags_hdr->ctl_table_arg;
124         unregister_net_sysctl_table(net->nf_frag_frags_hdr);
125         if (!net_eq(net, &init_net))
126                 kfree(table);
127 }
128
129 #else
130 static int nf_ct_frag6_sysctl_register(struct net *net)
131 {
132         return 0;
133 }
134 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
135 {
136 }
137 #endif
138
139 static int nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *skb,
140                              struct sk_buff *prev_tail, struct net_device *dev);
141
142 static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
143 {
144         return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
145 }
146
147 static void nf_ct_frag6_expire(struct timer_list *t)
148 {
149         struct inet_frag_queue *frag = from_timer(frag, t, timer);
150         struct frag_queue *fq;
151         struct net *net;
152
153         fq = container_of(frag, struct frag_queue, q);
154         net = container_of(fq->q.net, struct net, nf_frag.frags);
155
156         ip6frag_expire_frag_queue(net, fq);
157 }
158
159 /* Creation primitives. */
160 static struct frag_queue *fq_find(struct net *net, __be32 id, u32 user,
161                                   const struct ipv6hdr *hdr, int iif)
162 {
163         struct frag_v6_compare_key key = {
164                 .id = id,
165                 .saddr = hdr->saddr,
166                 .daddr = hdr->daddr,
167                 .user = user,
168                 .iif = iif,
169         };
170         struct inet_frag_queue *q;
171
172         q = inet_frag_find(&net->nf_frag.frags, &key);
173         if (!q)
174                 return NULL;
175
176         return container_of(q, struct frag_queue, q);
177 }
178
179
180 static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
181                              const struct frag_hdr *fhdr, int nhoff)
182 {
183         unsigned int payload_len;
184         struct net_device *dev;
185         struct sk_buff *prev;
186         int offset, end, err;
187         u8 ecn;
188
189         if (fq->q.flags & INET_FRAG_COMPLETE) {
190                 pr_debug("Already completed\n");
191                 goto err;
192         }
193
194         payload_len = ntohs(ipv6_hdr(skb)->payload_len);
195
196         offset = ntohs(fhdr->frag_off) & ~0x7;
197         end = offset + (payload_len -
198                         ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
199
200         if ((unsigned int)end > IPV6_MAXPLEN) {
201                 pr_debug("offset is too large.\n");
202                 return -EINVAL;
203         }
204
205         ecn = ip6_frag_ecn(ipv6_hdr(skb));
206
207         if (skb->ip_summed == CHECKSUM_COMPLETE) {
208                 const unsigned char *nh = skb_network_header(skb);
209                 skb->csum = csum_sub(skb->csum,
210                                      csum_partial(nh, (u8 *)(fhdr + 1) - nh,
211                                                   0));
212         }
213
214         /* Is this the final fragment? */
215         if (!(fhdr->frag_off & htons(IP6_MF))) {
216                 /* If we already have some bits beyond end
217                  * or have different end, the segment is corrupted.
218                  */
219                 if (end < fq->q.len ||
220                     ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) {
221                         pr_debug("already received last fragment\n");
222                         goto err;
223                 }
224                 fq->q.flags |= INET_FRAG_LAST_IN;
225                 fq->q.len = end;
226         } else {
227                 /* Check if the fragment is rounded to 8 bytes.
228                  * Required by the RFC.
229                  */
230                 if (end & 0x7) {
231                         /* RFC2460 says always send parameter problem in
232                          * this case. -DaveM
233                          */
234                         pr_debug("end of fragment not rounded to 8 bytes.\n");
235                         inet_frag_kill(&fq->q);
236                         return -EPROTO;
237                 }
238                 if (end > fq->q.len) {
239                         /* Some bits beyond end -> corruption. */
240                         if (fq->q.flags & INET_FRAG_LAST_IN) {
241                                 pr_debug("last packet already reached.\n");
242                                 goto err;
243                         }
244                         fq->q.len = end;
245                 }
246         }
247
248         if (end == offset)
249                 goto err;
250
251         /* Point into the IP datagram 'data' part. */
252         if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
253                 pr_debug("queue: message is too short.\n");
254                 goto err;
255         }
256         if (pskb_trim_rcsum(skb, end - offset)) {
257                 pr_debug("Can't trim\n");
258                 goto err;
259         }
260
261         /* Note : skb->rbnode and skb->dev share the same location. */
262         dev = skb->dev;
263         /* Makes sure compiler wont do silly aliasing games */
264         barrier();
265
266         prev = fq->q.fragments_tail;
267         err = inet_frag_queue_insert(&fq->q, skb, offset, end);
268         if (err) {
269                 if (err == IPFRAG_DUP) {
270                         /* No error for duplicates, pretend they got queued. */
271                         kfree_skb(skb);
272                         return -EINPROGRESS;
273                 }
274                 goto insert_error;
275         }
276
277         if (dev)
278                 fq->iif = dev->ifindex;
279
280         fq->q.stamp = skb->tstamp;
281         fq->q.meat += skb->len;
282         fq->ecn |= ecn;
283         if (payload_len > fq->q.max_size)
284                 fq->q.max_size = payload_len;
285         add_frag_mem_limit(fq->q.net, skb->truesize);
286
287         /* The first fragment.
288          * nhoffset is obtained from the first fragment, of course.
289          */
290         if (offset == 0) {
291                 fq->nhoffset = nhoff;
292                 fq->q.flags |= INET_FRAG_FIRST_IN;
293         }
294
295         if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
296             fq->q.meat == fq->q.len) {
297                 unsigned long orefdst = skb->_skb_refdst;
298
299                 skb->_skb_refdst = 0UL;
300                 err = nf_ct_frag6_reasm(fq, skb, prev, dev);
301                 skb->_skb_refdst = orefdst;
302
303                 /* After queue has assumed skb ownership, only 0 or
304                  * -EINPROGRESS must be returned.
305                  */
306                 return err ? -EINPROGRESS : 0;
307         }
308
309         skb_dst_drop(skb);
310         return -EINPROGRESS;
311
312 insert_error:
313         inet_frag_kill(&fq->q);
314 err:
315         skb_dst_drop(skb);
316         return -EINVAL;
317 }
318
319 /*
320  *      Check if this packet is complete.
321  *
322  *      It is called with locked fq, and caller must check that
323  *      queue is eligible for reassembly i.e. it is not COMPLETE,
324  *      the last and the first frames arrived and all the bits are here.
325  */
326 static int nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *skb,
327                              struct sk_buff *prev_tail, struct net_device *dev)
328 {
329         void *reasm_data;
330         int payload_len;
331         u8 ecn;
332
333         inet_frag_kill(&fq->q);
334
335         ecn = ip_frag_ecn_table[fq->ecn];
336         if (unlikely(ecn == 0xff))
337                 goto err;
338
339         reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
340         if (!reasm_data)
341                 goto err;
342
343         payload_len = ((skb->data - skb_network_header(skb)) -
344                        sizeof(struct ipv6hdr) + fq->q.len -
345                        sizeof(struct frag_hdr));
346         if (payload_len > IPV6_MAXPLEN) {
347                 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
348                                     payload_len);
349                 goto err;
350         }
351
352         /* We have to remove fragment header from datagram and to relocate
353          * header in order to calculate ICV correctly. */
354         skb_network_header(skb)[fq->nhoffset] = skb_transport_header(skb)[0];
355         memmove(skb->head + sizeof(struct frag_hdr), skb->head,
356                 (skb->data - skb->head) - sizeof(struct frag_hdr));
357         skb->mac_header += sizeof(struct frag_hdr);
358         skb->network_header += sizeof(struct frag_hdr);
359
360         skb_reset_transport_header(skb);
361
362         inet_frag_reasm_finish(&fq->q, skb, reasm_data);
363
364         skb->ignore_df = 1;
365         skb->dev = dev;
366         ipv6_hdr(skb)->payload_len = htons(payload_len);
367         ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn);
368         IP6CB(skb)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size;
369
370         /* Yes, and fold redundant checksum back. 8) */
371         if (skb->ip_summed == CHECKSUM_COMPLETE)
372                 skb->csum = csum_partial(skb_network_header(skb),
373                                          skb_network_header_len(skb),
374                                          skb->csum);
375
376         fq->q.fragments = NULL;
377         fq->q.rb_fragments = RB_ROOT;
378         fq->q.fragments_tail = NULL;
379         fq->q.last_run_head = NULL;
380
381         return 0;
382
383 err:
384         inet_frag_kill(&fq->q);
385         return -EINVAL;
386 }
387
388 /*
389  * find the header just before Fragment Header.
390  *
391  * if success return 0 and set ...
392  * (*prevhdrp): the value of "Next Header Field" in the header
393  *              just before Fragment Header.
394  * (*prevhoff): the offset of "Next Header Field" in the header
395  *              just before Fragment Header.
396  * (*fhoff)   : the offset of Fragment Header.
397  *
398  * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
399  *
400  */
401 static int
402 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
403 {
404         u8 nexthdr = ipv6_hdr(skb)->nexthdr;
405         const int netoff = skb_network_offset(skb);
406         u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
407         int start = netoff + sizeof(struct ipv6hdr);
408         int len = skb->len - start;
409         u8 prevhdr = NEXTHDR_IPV6;
410
411         while (nexthdr != NEXTHDR_FRAGMENT) {
412                 struct ipv6_opt_hdr hdr;
413                 int hdrlen;
414
415                 if (!ipv6_ext_hdr(nexthdr)) {
416                         return -1;
417                 }
418                 if (nexthdr == NEXTHDR_NONE) {
419                         pr_debug("next header is none\n");
420                         return -1;
421                 }
422                 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
423                         pr_debug("too short\n");
424                         return -1;
425                 }
426                 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
427                         BUG();
428                 if (nexthdr == NEXTHDR_AUTH)
429                         hdrlen = (hdr.hdrlen+2)<<2;
430                 else
431                         hdrlen = ipv6_optlen(&hdr);
432
433                 prevhdr = nexthdr;
434                 prev_nhoff = start;
435
436                 nexthdr = hdr.nexthdr;
437                 len -= hdrlen;
438                 start += hdrlen;
439         }
440
441         if (len < 0)
442                 return -1;
443
444         *prevhdrp = prevhdr;
445         *prevhoff = prev_nhoff;
446         *fhoff = start;
447
448         return 0;
449 }
450
451 int nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user)
452 {
453         u16 savethdr = skb->transport_header;
454         int fhoff, nhoff, ret;
455         struct frag_hdr *fhdr;
456         struct frag_queue *fq;
457         struct ipv6hdr *hdr;
458         u8 prevhdr;
459
460         /* Jumbo payload inhibits frag. header */
461         if (ipv6_hdr(skb)->payload_len == 0) {
462                 pr_debug("payload len = 0\n");
463                 return 0;
464         }
465
466         if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
467                 return 0;
468
469         if (!pskb_may_pull(skb, fhoff + sizeof(*fhdr)))
470                 return -ENOMEM;
471
472         skb_set_transport_header(skb, fhoff);
473         hdr = ipv6_hdr(skb);
474         fhdr = (struct frag_hdr *)skb_transport_header(skb);
475
476         skb_orphan(skb);
477         fq = fq_find(net, fhdr->identification, user, hdr,
478                      skb->dev ? skb->dev->ifindex : 0);
479         if (fq == NULL) {
480                 pr_debug("Can't find and can't create new queue\n");
481                 return -ENOMEM;
482         }
483
484         spin_lock_bh(&fq->q.lock);
485
486         ret = nf_ct_frag6_queue(fq, skb, fhdr, nhoff);
487         if (ret == -EPROTO) {
488                 skb->transport_header = savethdr;
489                 ret = 0;
490         }
491
492         spin_unlock_bh(&fq->q.lock);
493         inet_frag_put(&fq->q);
494         return ret;
495 }
496 EXPORT_SYMBOL_GPL(nf_ct_frag6_gather);
497
498 static int nf_ct_net_init(struct net *net)
499 {
500         int res;
501
502         net->nf_frag.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
503         net->nf_frag.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
504         net->nf_frag.frags.timeout = IPV6_FRAG_TIMEOUT;
505         net->nf_frag.frags.f = &nf_frags;
506
507         res = inet_frags_init_net(&net->nf_frag.frags);
508         if (res < 0)
509                 return res;
510         res = nf_ct_frag6_sysctl_register(net);
511         if (res < 0)
512                 inet_frags_exit_net(&net->nf_frag.frags);
513         return res;
514 }
515
516 static void nf_ct_net_exit(struct net *net)
517 {
518         nf_ct_frags6_sysctl_unregister(net);
519         inet_frags_exit_net(&net->nf_frag.frags);
520 }
521
522 static struct pernet_operations nf_ct_net_ops = {
523         .init = nf_ct_net_init,
524         .exit = nf_ct_net_exit,
525 };
526
527 static const struct rhashtable_params nfct_rhash_params = {
528         .head_offset            = offsetof(struct inet_frag_queue, node),
529         .hashfn                 = ip6frag_key_hashfn,
530         .obj_hashfn             = ip6frag_obj_hashfn,
531         .obj_cmpfn              = ip6frag_obj_cmpfn,
532         .automatic_shrinking    = true,
533 };
534
535 int nf_ct_frag6_init(void)
536 {
537         int ret = 0;
538
539         nf_frags.constructor = ip6frag_init;
540         nf_frags.destructor = NULL;
541         nf_frags.qsize = sizeof(struct frag_queue);
542         nf_frags.frag_expire = nf_ct_frag6_expire;
543         nf_frags.frags_cache_name = nf_frags_cache_name;
544         nf_frags.rhash_params = nfct_rhash_params;
545         ret = inet_frags_init(&nf_frags);
546         if (ret)
547                 goto out;
548         ret = register_pernet_subsys(&nf_ct_net_ops);
549         if (ret)
550                 inet_frags_fini(&nf_frags);
551
552 out:
553         return ret;
554 }
555
556 void nf_ct_frag6_cleanup(void)
557 {
558         unregister_pernet_subsys(&nf_ct_net_ops);
559         inet_frags_fini(&nf_frags);
560 }