1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 * Copyright (c) 2019 Netflix, Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $
33 */
34
35 #include "opt_rss.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/domain.h>
40 #include <sys/eventhandler.h>
41 #include <sys/hash.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/protosw.h>
46 #include <sys/queue.h>
47 #include <sys/socket.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
50
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/if_private.h>
54 #include <net/netisr.h>
55 #include <net/route.h>
56 #include <net/vnet.h>
57
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip6.h>
61 #include <netinet6/ip6_var.h>
62 #include <netinet/icmp6.h>
63 #include <netinet/in_systm.h> /* For ECN definitions. */
64 #include <netinet/ip.h> /* For ECN definitions. */
65
66 #ifdef MAC
67 #include <security/mac/mac_framework.h>
68 #endif
69
70 /*
71 * A "big picture" of how IPv6 fragment queues are all linked together.
72 *
73 * struct ip6qbucket ip6qb[...]; hashed buckets
74 * ||||||||
75 * |
76 * +--- TAILQ(struct ip6q, packets) *q6; tailq entries holding
77 * |||||||| fragmented packets
78 * | (1 per original packet)
79 * |
80 * +--- TAILQ(struct ip6asfrag, ip6q_frags) *af6; tailq entries of IPv6
81 * | *ip6af;fragment packets
82 * | for one original packet
83 * + *mbuf
84 */
85
86 /* Reassembly headers are stored in hash buckets. */
87 #define IP6REASS_NHASH_LOG2 10
88 #define IP6REASS_NHASH (1 << IP6REASS_NHASH_LOG2)
89 #define IP6REASS_HMASK (IP6REASS_NHASH - 1)
90
91 TAILQ_HEAD(ip6qhead, ip6q);
92 struct ip6qbucket {
93 struct ip6qhead packets;
94 struct mtx lock;
95 int count;
96 };
97
98 struct ip6asfrag {
99 TAILQ_ENTRY(ip6asfrag) ip6af_tq;
100 struct mbuf *ip6af_m;
101 int ip6af_offset; /* Offset in ip6af_m to next header. */
102 int ip6af_frglen; /* Fragmentable part length. */
103 int ip6af_off; /* Fragment offset. */
104 bool ip6af_mff; /* More fragment bit in frag off. */
105 };
106
107 static MALLOC_DEFINE(M_FRAG6, "frag6", "IPv6 fragment reassembly header");
108
109 #ifdef VIMAGE
110 /* A flag to indicate if IPv6 fragmentation is initialized. */
111 VNET_DEFINE_STATIC(bool, frag6_on);
112 #define V_frag6_on VNET(frag6_on)
113 #endif
114
115 /* System wide (global) maximum and count of packets in reassembly queues. */
116 static int ip6_maxfrags;
117 static u_int __exclusive_cache_line frag6_nfrags;
118
119 /* Maximum and current packets in per-VNET reassembly queue. */
120 VNET_DEFINE_STATIC(int, ip6_maxfragpackets);
121 VNET_DEFINE_STATIC(volatile u_int, frag6_nfragpackets);
122 #define V_ip6_maxfragpackets VNET(ip6_maxfragpackets)
123 #define V_frag6_nfragpackets VNET(frag6_nfragpackets)
124
125 /* Maximum per-VNET reassembly timeout (milliseconds) */
126 VNET_DEFINE_STATIC(u_int, ip6_fraglifetime) = IPV6_DEFFRAGTTL;
127 #define V_ip6_fraglifetime VNET(ip6_fraglifetime)
128
129 /* Maximum per-VNET reassembly queues per bucket and fragments per packet. */
130 VNET_DEFINE_STATIC(int, ip6_maxfragbucketsize);
131 VNET_DEFINE_STATIC(int, ip6_maxfragsperpacket);
132 #define V_ip6_maxfragbucketsize VNET(ip6_maxfragbucketsize)
133 #define V_ip6_maxfragsperpacket VNET(ip6_maxfragsperpacket)
134
135 /* Per-VNET reassembly queue buckets. */
136 VNET_DEFINE_STATIC(struct ip6qbucket, ip6qb[IP6REASS_NHASH]);
137 VNET_DEFINE_STATIC(uint32_t, ip6qb_hashseed);
138 #define V_ip6qb VNET(ip6qb)
139 #define V_ip6qb_hashseed VNET(ip6qb_hashseed)
140
141 #define IP6QB_LOCK(_b) mtx_lock(&V_ip6qb[(_b)].lock)
142 #define IP6QB_TRYLOCK(_b) mtx_trylock(&V_ip6qb[(_b)].lock)
143 #define IP6QB_LOCK_ASSERT(_b) mtx_assert(&V_ip6qb[(_b)].lock, MA_OWNED)
144 #define IP6QB_UNLOCK(_b) mtx_unlock(&V_ip6qb[(_b)].lock)
145 #define IP6QB_HEAD(_b) (&V_ip6qb[(_b)].packets)
146
147 /*
148 * By default, limit the number of IP6 fragments across all reassembly
149 * queues to 1/32 of the total number of mbuf clusters.
150 *
151 * Limit the total number of reassembly queues per VNET to the
152 * IP6 fragment limit, but ensure the limit will not allow any bucket
153 * to grow above 100 items. (The bucket limit is
154 * IP_MAXFRAGPACKETS / (IPREASS_NHASH / 2), so the 50 is the correct
155 * multiplier to reach a 100-item limit.)
156 * The 100-item limit was chosen as brief testing seems to show that
157 * this produces "reasonable" performance on some subset of systems
158 * under DoS attack.
159 */
160 #define IP6_MAXFRAGS (nmbclusters / 32)
161 #define IP6_MAXFRAGPACKETS (imin(IP6_MAXFRAGS, IP6REASS_NHASH * 50))
162
163 /* Interval between periodic reassembly queue inspections */
164 #define IP6_CALLOUT_INTERVAL_MS 500
165
166 /*
167 * Sysctls and helper function.
168 */
169 SYSCTL_DECL(_net_inet6_ip6);
170
171 SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, frag6_nfrags,
172 CTLFLAG_RD, &frag6_nfrags, 0,
173 "Global number of IPv6 fragments across all reassembly queues.");
174
175 static void
frag6_set_bucketsize(void)176 frag6_set_bucketsize(void)
177 {
178 int i;
179
180 if ((i = V_ip6_maxfragpackets) > 0)
181 V_ip6_maxfragbucketsize = imax(i / (IP6REASS_NHASH / 2), 1);
182 }
183
184 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGS, maxfrags,
185 CTLFLAG_RW, &ip6_maxfrags, 0,
186 "Maximum allowed number of outstanding IPv6 packet fragments. "
187 "A value of 0 means no fragmented packets will be accepted, while "
188 "a value of -1 means no limit");
189
190 static int
sysctl_ip6_maxfragpackets(SYSCTL_HANDLER_ARGS)191 sysctl_ip6_maxfragpackets(SYSCTL_HANDLER_ARGS)
192 {
193 int error, val;
194
195 val = V_ip6_maxfragpackets;
196 error = sysctl_handle_int(oidp, &val, 0, req);
197 if (error != 0 || !req->newptr)
198 return (error);
199 V_ip6_maxfragpackets = val;
200 frag6_set_bucketsize();
201 return (0);
202 }
203 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_MAXFRAGPACKETS, maxfragpackets,
204 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
205 NULL, 0, sysctl_ip6_maxfragpackets, "I",
206 "Default maximum number of outstanding fragmented IPv6 packets. "
207 "A value of 0 means no fragmented packets will be accepted, while a "
208 "a value of -1 means no limit");
209 SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, frag6_nfragpackets,
210 CTLFLAG_VNET | CTLFLAG_RD,
211 __DEVOLATILE(u_int *, &VNET_NAME(frag6_nfragpackets)), 0,
212 "Per-VNET number of IPv6 fragments across all reassembly queues.");
213 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGSPERPACKET, maxfragsperpacket,
214 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragsperpacket), 0,
215 "Maximum allowed number of fragments per packet");
216 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGBUCKETSIZE, maxfragbucketsize,
217 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragbucketsize), 0,
218 "Maximum number of reassembly queues per hash bucket");
219
220 static int
frag6_milli_to_callout_ticks(int ms)221 frag6_milli_to_callout_ticks(int ms)
222 {
223 return (ms / IP6_CALLOUT_INTERVAL_MS);
224 }
225
226 static int
frag6_callout_ticks_to_milli(int ms)227 frag6_callout_ticks_to_milli(int ms)
228 {
229 return (ms * IP6_CALLOUT_INTERVAL_MS);
230 }
231
232 _Static_assert(sizeof(((struct ip6q *)NULL)->ip6q_ttl) >= 2,
233 "ip6q_ttl field is not large enough");
234
235 static int
sysctl_ip6_fraglifetime(SYSCTL_HANDLER_ARGS)236 sysctl_ip6_fraglifetime(SYSCTL_HANDLER_ARGS)
237 {
238 int error, val;
239
240 val = V_ip6_fraglifetime;
241 error = sysctl_handle_int(oidp, &val, 0, req);
242 if (error != 0 || !req->newptr)
243 return (error);
244 if (val <= 0)
245 val = IPV6_DEFFRAGTTL;
246
247 if (frag6_milli_to_callout_ticks(val) >= 65536)
248 val = frag6_callout_ticks_to_milli(65535);
249 #ifdef VIMAGE
250 if (!IS_DEFAULT_VNET(curvnet)) {
251 CURVNET_SET(vnet0);
252 int host_val = V_ip6_fraglifetime;
253 CURVNET_RESTORE();
254
255 if (val > host_val)
256 val = host_val;
257 }
258 #endif
259 V_ip6_fraglifetime = val;
260 return (0);
261 }
262 SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, fraglifetime_ms,
263 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
264 NULL, 0, sysctl_ip6_fraglifetime, "I",
265 "Fragment lifetime, in milliseconds");
266
267 /*
268 * Remove the IPv6 fragmentation header from the mbuf.
269 */
270 int
ip6_deletefraghdr(struct mbuf * m,int offset,int wait __unused)271 ip6_deletefraghdr(struct mbuf *m, int offset, int wait __unused)
272 {
273 struct ip6_hdr *ip6;
274
275 KASSERT(m->m_len >= offset + sizeof(struct ip6_frag),
276 ("%s: ext headers not contigous in mbuf %p m_len %d >= "
277 "offset %d + %zu\n", __func__, m, m->m_len, offset,
278 sizeof(struct ip6_frag)));
279
280 /* Delete frag6 header. */
281 ip6 = mtod(m, struct ip6_hdr *);
282 bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag), offset);
283 m->m_data += sizeof(struct ip6_frag);
284 m->m_len -= sizeof(struct ip6_frag);
285 m->m_flags |= M_FRAGMENTED;
286
287 return (0);
288 }
289
290 static void
frag6_rmqueue(struct ip6q * q6,uint32_t bucket)291 frag6_rmqueue(struct ip6q *q6, uint32_t bucket)
292 {
293 IP6QB_LOCK_ASSERT(bucket);
294
295 TAILQ_REMOVE(IP6QB_HEAD(bucket), q6, ip6q_tq);
296 V_ip6qb[bucket].count--;
297 #ifdef MAC
298 mac_ip6q_destroy(q6);
299 #endif
300 free(q6, M_FRAG6);
301 atomic_subtract_int(&V_frag6_nfragpackets, 1);
302 }
303
304 /*
305 * Free a fragment reassembly header and all associated datagrams.
306 */
307 static void
frag6_freef(struct ip6q * q6,uint32_t bucket)308 frag6_freef(struct ip6q *q6, uint32_t bucket)
309 {
310 struct ip6_hdr *ip6;
311 struct ip6asfrag *af6;
312 struct mbuf *m;
313
314 IP6QB_LOCK_ASSERT(bucket);
315
316 while ((af6 = TAILQ_FIRST(&q6->ip6q_frags)) != NULL) {
317 m = af6->ip6af_m;
318 TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq);
319
320 /*
321 * Return ICMP time exceeded error for the 1st fragment.
322 * Just free other fragments.
323 */
324 if (af6->ip6af_off == 0 && m->m_pkthdr.rcvif != NULL) {
325 /* Adjust pointer. */
326 ip6 = mtod(m, struct ip6_hdr *);
327
328 /* Restore source and destination addresses. */
329 ip6->ip6_src = q6->ip6q_src;
330 ip6->ip6_dst = q6->ip6q_dst;
331
332 icmp6_error(m, ICMP6_TIME_EXCEEDED,
333 ICMP6_TIME_EXCEED_REASSEMBLY, 0);
334 } else
335 m_freem(m);
336
337 free(af6, M_FRAG6);
338 }
339
340 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
341 frag6_rmqueue(q6, bucket);
342 }
343
344 /*
345 * Drain off all datagram fragments belonging to
346 * the given network interface.
347 */
348 static void
frag6_cleanup(void * arg __unused,struct ifnet * ifp)349 frag6_cleanup(void *arg __unused, struct ifnet *ifp)
350 {
351 struct ip6qhead *head;
352 struct ip6q *q6;
353 struct ip6asfrag *af6;
354 uint32_t bucket;
355
356 KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__));
357
358 CURVNET_SET_QUIET(ifp->if_vnet);
359 #ifdef VIMAGE
360 /*
361 * Skip processing if IPv6 reassembly is not initialised or
362 * torn down by frag6_destroy().
363 */
364 if (!V_frag6_on) {
365 CURVNET_RESTORE();
366 return;
367 }
368 #endif
369
370 for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
371 IP6QB_LOCK(bucket);
372 head = IP6QB_HEAD(bucket);
373 /* Scan fragment list. */
374 TAILQ_FOREACH(q6, head, ip6q_tq) {
375 TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq) {
376 /* Clear no longer valid rcvif pointer. */
377 if (af6->ip6af_m->m_pkthdr.rcvif == ifp)
378 af6->ip6af_m->m_pkthdr.rcvif = NULL;
379 }
380 }
381 IP6QB_UNLOCK(bucket);
382 }
383 CURVNET_RESTORE();
384 }
385 EVENTHANDLER_DEFINE(ifnet_departure_event, frag6_cleanup, NULL, 0);
386
387 /*
388 * Like in RFC2460, in RFC8200, fragment and reassembly rules do not agree with
389 * each other, in terms of next header field handling in fragment header.
390 * While the sender will use the same value for all of the fragmented packets,
391 * receiver is suggested not to check for consistency.
392 *
393 * Fragment rules (p18,p19):
394 * (2) A Fragment header containing:
395 * The Next Header value that identifies the first header
396 * after the Per-Fragment headers of the original packet.
397 * -> next header field is same for all fragments
398 *
399 * Reassembly rule (p20):
400 * The Next Header field of the last header of the Per-Fragment
401 * headers is obtained from the Next Header field of the first
402 * fragment's Fragment header.
403 * -> should grab it from the first fragment only
404 *
405 * The following note also contradicts with fragment rule - no one is going to
406 * send different fragment with different next header field.
407 *
408 * Additional note (p22) [not an error]:
409 * The Next Header values in the Fragment headers of different
410 * fragments of the same original packet may differ. Only the value
411 * from the Offset zero fragment packet is used for reassembly.
412 * -> should grab it from the first fragment only
413 *
414 * There is no explicit reason given in the RFC. Historical reason maybe?
415 */
416 /*
417 * Fragment input.
418 */
419 int
frag6_input(struct mbuf ** mp,int * offp,int proto)420 frag6_input(struct mbuf **mp, int *offp, int proto)
421 {
422 struct mbuf *m, *t;
423 struct ip6_hdr *ip6;
424 struct ip6_frag *ip6f;
425 struct ip6qhead *head;
426 struct ip6q *q6;
427 struct ip6asfrag *af6, *ip6af, *af6tmp;
428 struct in6_ifaddr *ia6;
429 struct ifnet *dstifp, *srcifp;
430 uint32_t hashkey[(sizeof(struct in6_addr) * 2 +
431 sizeof(ip6f->ip6f_ident)) / sizeof(uint32_t)];
432 uint32_t bucket, *hashkeyp;
433 int fragoff, frgpartlen; /* Must be larger than uint16_t. */
434 int nxt, offset, plen;
435 uint8_t ecn, ecn0;
436 bool only_frag;
437 #ifdef RSS
438 struct ip6_direct_ctx *ip6dc;
439 struct m_tag *mtag;
440 #endif
441
442 m = *mp;
443 offset = *offp;
444
445 M_ASSERTPKTHDR(m);
446
447 if (m->m_len < offset + sizeof(struct ip6_frag)) {
448 m = m_pullup(m, offset + sizeof(struct ip6_frag));
449 if (m == NULL) {
450 IP6STAT_INC(ip6s_exthdrtoolong);
451 *mp = NULL;
452 return (IPPROTO_DONE);
453 }
454 }
455 ip6 = mtod(m, struct ip6_hdr *);
456
457 dstifp = NULL;
458 /* Find the destination interface of the packet. */
459 ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
460 if (ia6 != NULL)
461 dstifp = ia6->ia_ifp;
462
463 /* Jumbo payload cannot contain a fragment header. */
464 if (ip6->ip6_plen == 0) {
465 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
466 in6_ifstat_inc(dstifp, ifs6_reass_fail);
467 *mp = NULL;
468 return (IPPROTO_DONE);
469 }
470
471 /*
472 * Check whether fragment packet's fragment length is a
473 * multiple of 8 octets (unless it is the last one).
474 * sizeof(struct ip6_frag) == 8
475 * sizeof(struct ip6_hdr) = 40
476 */
477 ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
478 if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
479 (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
480 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
481 offsetof(struct ip6_hdr, ip6_plen));
482 in6_ifstat_inc(dstifp, ifs6_reass_fail);
483 *mp = NULL;
484 return (IPPROTO_DONE);
485 }
486
487 IP6STAT_INC(ip6s_fragments);
488 in6_ifstat_inc(dstifp, ifs6_reass_reqd);
489
490 /*
491 * Handle "atomic" fragments (offset and m bit set to 0) upfront,
492 * unrelated to any reassembly. We need to remove the frag hdr
493 * which is ugly.
494 * See RFC 6946 and section 4.5 of RFC 8200.
495 */
496 if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {
497 IP6STAT_INC(ip6s_atomicfrags);
498 nxt = ip6f->ip6f_nxt;
499 /*
500 * Set nxt(-hdr field value) to the original value.
501 * We cannot just set ip6->ip6_nxt as there might be
502 * an unfragmentable part with extension headers and
503 * we must update the last one.
504 */
505 m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
506 (caddr_t)&nxt);
507 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) -
508 sizeof(struct ip6_frag));
509 if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0)
510 goto dropfrag2;
511 m->m_pkthdr.len -= sizeof(struct ip6_frag);
512 in6_ifstat_inc(dstifp, ifs6_reass_ok);
513 *mp = m;
514 return (nxt);
515 }
516
517 /* Offset now points to data portion. */
518 offset += sizeof(struct ip6_frag);
519
520 /* Get fragment length and discard 0-byte fragments. */
521 frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
522 if (frgpartlen == 0) {
523 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
524 offsetof(struct ip6_hdr, ip6_plen));
525 in6_ifstat_inc(dstifp, ifs6_reass_fail);
526 IP6STAT_INC(ip6s_fragdropped);
527 *mp = NULL;
528 return (IPPROTO_DONE);
529 }
530
531 /*
532 * Enforce upper bound on number of fragments for the entire system.
533 * If maxfrag is 0, never accept fragments.
534 * If maxfrag is -1, accept all fragments without limitation.
535 */
536 if (ip6_maxfrags < 0)
537 ;
538 else if (atomic_load_int(&frag6_nfrags) >= (u_int)ip6_maxfrags)
539 goto dropfrag2;
540
541 /*
542 * Validate that a full header chain to the ULP is present in the
543 * packet containing the first fragment as per RFC RFC7112 and
544 * RFC 8200 pages 18,19:
545 * The first fragment packet is composed of:
546 * (3) Extension headers, if any, and the Upper-Layer header. These
547 * headers must be in the first fragment. ...
548 */
549 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
550 /* XXX TODO. thj has D16851 open for this. */
551 /* Send ICMPv6 4,3 in case of violation. */
552
553 /* Store receive network interface pointer for later. */
554 srcifp = m->m_pkthdr.rcvif;
555
556 /* Generate a hash value for fragment bucket selection. */
557 hashkeyp = hashkey;
558 memcpy(hashkeyp, &ip6->ip6_src, sizeof(struct in6_addr));
559 hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
560 memcpy(hashkeyp, &ip6->ip6_dst, sizeof(struct in6_addr));
561 hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
562 *hashkeyp = ip6f->ip6f_ident;
563 bucket = jenkins_hash32(hashkey, nitems(hashkey), V_ip6qb_hashseed);
564 bucket &= IP6REASS_HMASK;
565 IP6QB_LOCK(bucket);
566 head = IP6QB_HEAD(bucket);
567
568 TAILQ_FOREACH(q6, head, ip6q_tq)
569 if (ip6f->ip6f_ident == q6->ip6q_ident &&
570 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
571 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
572 #ifdef MAC
573 && mac_ip6q_match(m, q6)
574 #endif
575 )
576 break;
577
578 only_frag = false;
579 if (q6 == NULL) {
580 /* A first fragment to arrive creates a reassembly queue. */
581 only_frag = true;
582
583 /*
584 * Enforce upper bound on number of fragmented packets
585 * for which we attempt reassembly;
586 * If maxfragpackets is 0, never accept fragments.
587 * If maxfragpackets is -1, accept all fragments without
588 * limitation.
589 */
590 if (V_ip6_maxfragpackets < 0)
591 ;
592 else if (V_ip6qb[bucket].count >= V_ip6_maxfragbucketsize ||
593 atomic_load_int(&V_frag6_nfragpackets) >=
594 (u_int)V_ip6_maxfragpackets)
595 goto dropfrag;
596
597 /* Allocate IPv6 fragement packet queue entry. */
598 q6 = malloc(sizeof(struct ip6q), M_FRAG6, M_NOWAIT | M_ZERO);
599 if (q6 == NULL)
600 goto dropfrag;
601 #ifdef MAC
602 if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
603 free(q6, M_FRAG6);
604 goto dropfrag;
605 }
606 mac_ip6q_create(m, q6);
607 #endif
608 atomic_add_int(&V_frag6_nfragpackets, 1);
609
610 /* ip6q_nxt will be filled afterwards, from 1st fragment. */
611 TAILQ_INIT(&q6->ip6q_frags);
612 q6->ip6q_ident = ip6f->ip6f_ident;
613 q6->ip6q_ttl = frag6_milli_to_callout_ticks(V_ip6_fraglifetime);
614 q6->ip6q_src = ip6->ip6_src;
615 q6->ip6q_dst = ip6->ip6_dst;
616 q6->ip6q_ecn = IPV6_ECN(ip6);
617 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
618
619 /* Add the fragemented packet to the bucket. */
620 TAILQ_INSERT_HEAD(head, q6, ip6q_tq);
621 V_ip6qb[bucket].count++;
622 }
623
624 /*
625 * If it is the 1st fragment, record the length of the
626 * unfragmentable part and the next header of the fragment header.
627 * Assume the first 1st fragement to arrive will be correct.
628 * We do not have any duplicate checks here yet so another packet
629 * with fragoff == 0 could come and overwrite the ip6q_unfrglen
630 * and worse, the next header, at any time.
631 */
632 if (fragoff == 0 && q6->ip6q_unfrglen == -1) {
633 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
634 sizeof(struct ip6_frag);
635 q6->ip6q_nxt = ip6f->ip6f_nxt;
636 /* XXX ECN? */
637 }
638
639 /*
640 * Check that the reassembled packet would not exceed 65535 bytes
641 * in size.
642 * If it would exceed, discard the fragment and return an ICMP error.
643 */
644 if (q6->ip6q_unfrglen >= 0) {
645 /* The 1st fragment has already arrived. */
646 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
647 if (only_frag)
648 frag6_rmqueue(q6, bucket);
649 IP6QB_UNLOCK(bucket);
650 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
651 offset - sizeof(struct ip6_frag) +
652 offsetof(struct ip6_frag, ip6f_offlg));
653 *mp = NULL;
654 return (IPPROTO_DONE);
655 }
656 } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
657 if (only_frag)
658 frag6_rmqueue(q6, bucket);
659 IP6QB_UNLOCK(bucket);
660 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
661 offset - sizeof(struct ip6_frag) +
662 offsetof(struct ip6_frag, ip6f_offlg));
663 *mp = NULL;
664 return (IPPROTO_DONE);
665 }
666
667 /*
668 * If it is the first fragment, do the above check for each
669 * fragment already stored in the reassembly queue.
670 */
671 if (fragoff == 0 && !only_frag) {
672 TAILQ_FOREACH_SAFE(af6, &q6->ip6q_frags, ip6af_tq, af6tmp) {
673 if (q6->ip6q_unfrglen + af6->ip6af_off +
674 af6->ip6af_frglen > IPV6_MAXPACKET) {
675 struct ip6_hdr *ip6err;
676 struct mbuf *merr;
677 int erroff;
678
679 merr = af6->ip6af_m;
680 erroff = af6->ip6af_offset;
681
682 /* Dequeue the fragment. */
683 TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq);
684 q6->ip6q_nfrag--;
685 atomic_subtract_int(&frag6_nfrags, 1);
686 free(af6, M_FRAG6);
687
688 /* Set a valid receive interface pointer. */
689 merr->m_pkthdr.rcvif = srcifp;
690
691 /* Adjust pointer. */
692 ip6err = mtod(merr, struct ip6_hdr *);
693
694 /*
695 * Restore source and destination addresses
696 * in the erroneous IPv6 header.
697 */
698 ip6err->ip6_src = q6->ip6q_src;
699 ip6err->ip6_dst = q6->ip6q_dst;
700
701 icmp6_error(merr, ICMP6_PARAM_PROB,
702 ICMP6_PARAMPROB_HEADER,
703 erroff - sizeof(struct ip6_frag) +
704 offsetof(struct ip6_frag, ip6f_offlg));
705 }
706 }
707 }
708
709 /* Allocate an IPv6 fragement queue entry for this fragmented part. */
710 ip6af = malloc(sizeof(struct ip6asfrag), M_FRAG6, M_NOWAIT | M_ZERO);
711 if (ip6af == NULL)
712 goto dropfrag;
713 ip6af->ip6af_mff = (ip6f->ip6f_offlg & IP6F_MORE_FRAG) ? true : false;
714 ip6af->ip6af_off = fragoff;
715 ip6af->ip6af_frglen = frgpartlen;
716 ip6af->ip6af_offset = offset;
717 ip6af->ip6af_m = m;
718
719 if (only_frag) {
720 /*
721 * Do a manual insert rather than a hard-to-understand cast
722 * to a different type relying on data structure order to work.
723 */
724 TAILQ_INSERT_HEAD(&q6->ip6q_frags, ip6af, ip6af_tq);
725 goto postinsert;
726 }
727
728 /* Do duplicate, condition, and boundry checks. */
729 /*
730 * Handle ECN by comparing this segment with the first one;
731 * if CE is set, do not lose CE.
732 * Drop if CE and not-ECT are mixed for the same packet.
733 */
734 ecn = IPV6_ECN(ip6);
735 ecn0 = q6->ip6q_ecn;
736 if (ecn == IPTOS_ECN_CE) {
737 if (ecn0 == IPTOS_ECN_NOTECT) {
738 free(ip6af, M_FRAG6);
739 goto dropfrag;
740 }
741 if (ecn0 != IPTOS_ECN_CE)
742 q6->ip6q_ecn = IPTOS_ECN_CE;
743 }
744 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
745 free(ip6af, M_FRAG6);
746 goto dropfrag;
747 }
748
749 /* Find a fragmented part which begins after this one does. */
750 TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq)
751 if (af6->ip6af_off > ip6af->ip6af_off)
752 break;
753
754 /*
755 * If the incoming framgent overlaps some existing fragments in
756 * the reassembly queue, drop both the new fragment and the
757 * entire reassembly queue. However, if the new fragment
758 * is an exact duplicate of an existing fragment, only silently
759 * drop the existing fragment and leave the fragmentation queue
760 * unchanged, as allowed by the RFC. (RFC 8200, 4.5)
761 */
762 if (af6 != NULL)
763 af6tmp = TAILQ_PREV(af6, ip6fraghead, ip6af_tq);
764 else
765 af6tmp = TAILQ_LAST(&q6->ip6q_frags, ip6fraghead);
766 if (af6tmp != NULL) {
767 if (af6tmp->ip6af_off + af6tmp->ip6af_frglen -
768 ip6af->ip6af_off > 0) {
769 if (af6tmp->ip6af_off != ip6af->ip6af_off ||
770 af6tmp->ip6af_frglen != ip6af->ip6af_frglen)
771 frag6_freef(q6, bucket);
772 free(ip6af, M_FRAG6);
773 goto dropfrag;
774 }
775 }
776 if (af6 != NULL) {
777 if (ip6af->ip6af_off + ip6af->ip6af_frglen -
778 af6->ip6af_off > 0) {
779 if (af6->ip6af_off != ip6af->ip6af_off ||
780 af6->ip6af_frglen != ip6af->ip6af_frglen)
781 frag6_freef(q6, bucket);
782 free(ip6af, M_FRAG6);
783 goto dropfrag;
784 }
785 }
786
787 #ifdef MAC
788 mac_ip6q_update(m, q6);
789 #endif
790
791 /*
792 * Stick new segment in its place; check for complete reassembly.
793 * If not complete, check fragment limit. Move to front of packet
794 * queue, as we are the most recently active fragmented packet.
795 */
796 if (af6 != NULL)
797 TAILQ_INSERT_BEFORE(af6, ip6af, ip6af_tq);
798 else
799 TAILQ_INSERT_TAIL(&q6->ip6q_frags, ip6af, ip6af_tq);
800 postinsert:
801 atomic_add_int(&frag6_nfrags, 1);
802 q6->ip6q_nfrag++;
803
804 plen = 0;
805 TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq) {
806 if (af6->ip6af_off != plen) {
807 if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
808 IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
809 frag6_freef(q6, bucket);
810 }
811 IP6QB_UNLOCK(bucket);
812 *mp = NULL;
813 return (IPPROTO_DONE);
814 }
815 plen += af6->ip6af_frglen;
816 }
817 af6 = TAILQ_LAST(&q6->ip6q_frags, ip6fraghead);
818 if (af6->ip6af_mff) {
819 if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
820 IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
821 frag6_freef(q6, bucket);
822 }
823 IP6QB_UNLOCK(bucket);
824 *mp = NULL;
825 return (IPPROTO_DONE);
826 }
827
828 /* Reassembly is complete; concatenate fragments. */
829 ip6af = TAILQ_FIRST(&q6->ip6q_frags);
830 t = m = ip6af->ip6af_m;
831 TAILQ_REMOVE(&q6->ip6q_frags, ip6af, ip6af_tq);
832 while ((af6 = TAILQ_FIRST(&q6->ip6q_frags)) != NULL) {
833 m->m_pkthdr.csum_flags &=
834 af6->ip6af_m->m_pkthdr.csum_flags;
835 m->m_pkthdr.csum_data +=
836 af6->ip6af_m->m_pkthdr.csum_data;
837
838 TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq);
839 t = m_last(t);
840 m_adj(af6->ip6af_m, af6->ip6af_offset);
841 m_demote_pkthdr(af6->ip6af_m);
842 m_cat(t, af6->ip6af_m);
843 free(af6, M_FRAG6);
844 }
845
846 while (m->m_pkthdr.csum_data & 0xffff0000)
847 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
848 (m->m_pkthdr.csum_data >> 16);
849
850 /* Adjust offset to point where the original next header starts. */
851 offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
852 free(ip6af, M_FRAG6);
853 if ((u_int)plen + (u_int)offset - sizeof(struct ip6_hdr) >
854 IPV6_MAXPACKET) {
855 frag6_freef(q6, bucket);
856 goto dropfrag;
857 }
858 ip6 = mtod(m, struct ip6_hdr *);
859 ip6->ip6_plen = htons((u_short)plen + offset - sizeof(struct ip6_hdr));
860 if (q6->ip6q_ecn == IPTOS_ECN_CE)
861 ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
862 nxt = q6->ip6q_nxt;
863
864 ip6_deletefraghdr(m, offset, M_NOWAIT);
865
866 /* Set nxt(-hdr field value) to the original value. */
867 m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
868 (caddr_t)&nxt);
869
870 #ifdef MAC
871 mac_ip6q_reassemble(q6, m);
872 #endif
873 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
874 frag6_rmqueue(q6, bucket);
875
876 if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
877
878 plen = 0;
879 for (t = m; t; t = t->m_next)
880 plen += t->m_len;
881 m->m_pkthdr.len = plen;
882 /* Set a valid receive interface pointer. */
883 m->m_pkthdr.rcvif = srcifp;
884 }
885
886 #ifdef RSS
887 mtag = m_tag_alloc(MTAG_ABI_IPV6, IPV6_TAG_DIRECT, sizeof(*ip6dc),
888 M_NOWAIT);
889 if (mtag == NULL)
890 goto dropfrag;
891
892 ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
893 ip6dc->ip6dc_nxt = nxt;
894 ip6dc->ip6dc_off = offset;
895
896 m_tag_prepend(m, mtag);
897 #endif
898
899 IP6QB_UNLOCK(bucket);
900 IP6STAT_INC(ip6s_reassembled);
901 in6_ifstat_inc(dstifp, ifs6_reass_ok);
902
903 #ifdef RSS
904 /* Queue/dispatch for reprocessing. */
905 netisr_dispatch(NETISR_IPV6_DIRECT, m);
906 *mp = NULL;
907 return (IPPROTO_DONE);
908 #endif
909
910 /* Tell launch routine the next header. */
911 *mp = m;
912 *offp = offset;
913
914 return (nxt);
915
916 dropfrag:
917 IP6QB_UNLOCK(bucket);
918 dropfrag2:
919 in6_ifstat_inc(dstifp, ifs6_reass_fail);
920 IP6STAT_INC(ip6s_fragdropped);
921 m_freem(m);
922 *mp = NULL;
923 return (IPPROTO_DONE);
924 }
925
926 /*
927 * IPv6 reassembling timer processing;
928 * if a timer expires on a reassembly queue, discard it.
929 */
930 static struct callout frag6_callout;
931 static void
frag6_slowtimo(void * arg __unused)932 frag6_slowtimo(void *arg __unused)
933 {
934 VNET_ITERATOR_DECL(vnet_iter);
935 struct ip6qhead *head;
936 struct ip6q *q6, *q6tmp;
937 uint32_t bucket;
938
939 if (atomic_load_int(&frag6_nfrags) == 0)
940 goto done;
941
942 VNET_LIST_RLOCK_NOSLEEP();
943 VNET_FOREACH(vnet_iter) {
944 CURVNET_SET(vnet_iter);
945 for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
946 if (V_ip6qb[bucket].count == 0)
947 continue;
948 IP6QB_LOCK(bucket);
949 head = IP6QB_HEAD(bucket);
950 TAILQ_FOREACH_SAFE(q6, head, ip6q_tq, q6tmp)
951 if (--q6->ip6q_ttl == 0) {
952 IP6STAT_ADD(ip6s_fragtimeout,
953 q6->ip6q_nfrag);
954 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
955 frag6_freef(q6, bucket);
956 }
957 /*
958 * If we are over the maximum number of fragments
959 * (due to the limit being lowered), drain off
960 * enough to get down to the new limit.
961 * Note that we drain all reassembly queues if
962 * maxfragpackets is 0 (fragmentation is disabled),
963 * and do not enforce a limit when maxfragpackets
964 * is negative.
965 */
966 while ((V_ip6_maxfragpackets == 0 ||
967 (V_ip6_maxfragpackets > 0 &&
968 V_ip6qb[bucket].count > V_ip6_maxfragbucketsize)) &&
969 (q6 = TAILQ_LAST(head, ip6qhead)) != NULL) {
970 IP6STAT_ADD(ip6s_fragoverflow, q6->ip6q_nfrag);
971 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
972 frag6_freef(q6, bucket);
973 }
974 IP6QB_UNLOCK(bucket);
975 }
976 /*
977 * If we are still over the maximum number of fragmented
978 * packets, drain off enough to get down to the new limit.
979 */
980 bucket = 0;
981 while (V_ip6_maxfragpackets >= 0 &&
982 atomic_load_int(&V_frag6_nfragpackets) >
983 (u_int)V_ip6_maxfragpackets) {
984 IP6QB_LOCK(bucket);
985 q6 = TAILQ_LAST(IP6QB_HEAD(bucket), ip6qhead);
986 if (q6 != NULL) {
987 IP6STAT_ADD(ip6s_fragoverflow, q6->ip6q_nfrag);
988 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
989 frag6_freef(q6, bucket);
990 }
991 IP6QB_UNLOCK(bucket);
992 bucket = (bucket + 1) % IP6REASS_NHASH;
993 }
994 CURVNET_RESTORE();
995 }
996 VNET_LIST_RUNLOCK_NOSLEEP();
997 done:
998 callout_reset_sbt(&frag6_callout, SBT_1MS * IP6_CALLOUT_INTERVAL_MS,
999 SBT_1MS * 10, frag6_slowtimo, NULL, 0);
1000 }
1001
1002 static void
frag6_slowtimo_init(void * arg __unused)1003 frag6_slowtimo_init(void *arg __unused)
1004 {
1005
1006 callout_init(&frag6_callout, 1);
1007 callout_reset_sbt(&frag6_callout, SBT_1MS * IP6_CALLOUT_INTERVAL_MS,
1008 SBT_1MS * 10, frag6_slowtimo, NULL, 0);
1009 }
1010 SYSINIT(frag6, SI_SUB_VNET_DONE, SI_ORDER_ANY, frag6_slowtimo_init, NULL);
1011
1012 /*
1013 * Eventhandler to adjust limits in case nmbclusters change.
1014 */
1015 static void
frag6_change(void * tag)1016 frag6_change(void *tag)
1017 {
1018 VNET_ITERATOR_DECL(vnet_iter);
1019
1020 ip6_maxfrags = IP6_MAXFRAGS;
1021 VNET_LIST_RLOCK_NOSLEEP();
1022 VNET_FOREACH(vnet_iter) {
1023 CURVNET_SET(vnet_iter);
1024 V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
1025 frag6_set_bucketsize();
1026 CURVNET_RESTORE();
1027 }
1028 VNET_LIST_RUNLOCK_NOSLEEP();
1029 }
1030
1031 /*
1032 * Initialise reassembly queue and fragment identifier.
1033 */
1034 void
frag6_init(void)1035 frag6_init(void)
1036 {
1037 uint32_t bucket;
1038
1039 V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
1040 frag6_set_bucketsize();
1041 for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
1042 TAILQ_INIT(IP6QB_HEAD(bucket));
1043 mtx_init(&V_ip6qb[bucket].lock, "ip6qb", NULL, MTX_DEF);
1044 V_ip6qb[bucket].count = 0;
1045 }
1046 V_ip6qb_hashseed = arc4random();
1047 V_ip6_maxfragsperpacket = 64;
1048 #ifdef VIMAGE
1049 V_frag6_on = true;
1050 #endif
1051 if (!IS_DEFAULT_VNET(curvnet))
1052 return;
1053
1054 ip6_maxfrags = IP6_MAXFRAGS;
1055 EVENTHANDLER_REGISTER(nmbclusters_change,
1056 frag6_change, NULL, EVENTHANDLER_PRI_ANY);
1057 }
1058
1059 /*
1060 * Drain off all datagram fragments.
1061 */
1062 static void
frag6_drain_one(void)1063 frag6_drain_one(void)
1064 {
1065 struct ip6q *q6;
1066 uint32_t bucket;
1067
1068 for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
1069 IP6QB_LOCK(bucket);
1070 while ((q6 = TAILQ_FIRST(IP6QB_HEAD(bucket))) != NULL) {
1071 IP6STAT_INC(ip6s_fragdropped);
1072 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
1073 frag6_freef(q6, bucket);
1074 }
1075 IP6QB_UNLOCK(bucket);
1076 }
1077 }
1078
1079 void
frag6_drain(void * arg __unused,int flags __unused)1080 frag6_drain(void *arg __unused, int flags __unused)
1081 {
1082 VNET_ITERATOR_DECL(vnet_iter);
1083
1084 VNET_LIST_RLOCK_NOSLEEP();
1085 VNET_FOREACH(vnet_iter) {
1086 CURVNET_SET(vnet_iter);
1087 frag6_drain_one();
1088 CURVNET_RESTORE();
1089 }
1090 VNET_LIST_RUNLOCK_NOSLEEP();
1091 }
1092
1093 #ifdef VIMAGE
1094 /*
1095 * Clear up IPv6 reassembly structures.
1096 */
1097 void
frag6_destroy(void)1098 frag6_destroy(void)
1099 {
1100 uint32_t bucket;
1101
1102 frag6_drain_one();
1103 V_frag6_on = false;
1104 for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
1105 KASSERT(V_ip6qb[bucket].count == 0,
1106 ("%s: V_ip6qb[%d] (%p) count not 0 (%d)", __func__,
1107 bucket, &V_ip6qb[bucket], V_ip6qb[bucket].count));
1108 mtx_destroy(&V_ip6qb[bucket].lock);
1109 }
1110 }
1111 #endif
1112