]>
Commit | Line | Data |
---|---|---|
b0d623f7 | 1 | /* |
5ba3f43e | 2 | * Copyright (c) 2000-2017 Apple Inc. All rights reserved. |
b0d623f7 A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
0a7de745 | 5 | * |
b0d623f7 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
0a7de745 | 14 | * |
b0d623f7 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
b0d623f7 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
0a7de745 | 25 | * |
b0d623f7 A |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ | |
28 | ||
9bccf70c A |
29 | /* $FreeBSD: src/sys/netinet6/frag6.c,v 1.2.2.5 2001/07/03 11:01:50 ume Exp $ */ |
30 | /* $KAME: frag6.c,v 1.31 2001/05/17 13:45:34 jinmei Exp $ */ | |
1c79356b A |
31 | |
32 | /* | |
33 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
34 | * All rights reserved. | |
35 | * | |
36 | * Redistribution and use in source and binary forms, with or without | |
37 | * modification, are permitted provided that the following conditions | |
38 | * are met: | |
39 | * 1. Redistributions of source code must retain the above copyright | |
40 | * notice, this list of conditions and the following disclaimer. | |
41 | * 2. Redistributions in binary form must reproduce the above copyright | |
42 | * notice, this list of conditions and the following disclaimer in the | |
43 | * documentation and/or other materials provided with the distribution. | |
44 | * 3. Neither the name of the project nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | */ | |
60 | ||
61 | #include <sys/param.h> | |
62 | #include <sys/systm.h> | |
63 | #include <sys/malloc.h> | |
316670eb | 64 | #include <sys/mcache.h> |
1c79356b A |
65 | #include <sys/mbuf.h> |
66 | #include <sys/domain.h> | |
67 | #include <sys/protosw.h> | |
68 | #include <sys/socket.h> | |
69 | #include <sys/errno.h> | |
70 | #include <sys/time.h> | |
71 | #include <sys/kernel.h> | |
72 | #include <sys/syslog.h> | |
73 | #include <kern/queue.h> | |
91447636 | 74 | #include <kern/locks.h> |
1c79356b A |
75 | |
76 | #include <net/if.h> | |
77 | #include <net/route.h> | |
78 | ||
79 | #include <netinet/in.h> | |
80 | #include <netinet/in_var.h> | |
6d2010ae | 81 | #include <netinet/ip.h> |
5ba3f43e | 82 | #include <netinet/ip_var.h> |
1c79356b A |
83 | #include <netinet/ip6.h> |
84 | #include <netinet6/ip6_var.h> | |
1c79356b A |
85 | #include <netinet/icmp6.h> |
86 | ||
87 | #include <net/net_osdep.h> | |
39236c6e | 88 | #include <dev/random/randomdev.h> |
1c79356b A |
89 | |
90 | /* | |
91 | * Define it to get a correct behavior on per-interface statistics. | |
1c79356b A |
92 | */ |
93 | #define IN6_IFSTAT_STRICT | |
94 | ||
39236c6e A |
95 | MBUFQ_HEAD(fq6_head); |
96 | ||
97 | static void frag6_save_context(struct mbuf *, int); | |
98 | static void frag6_scrub_context(struct mbuf *); | |
99 | static int frag6_restore_context(struct mbuf *); | |
100 | ||
101 | static void frag6_icmp6_paramprob_error(struct fq6_head *); | |
102 | static void frag6_icmp6_timeex_error(struct fq6_head *); | |
103 | ||
91447636 A |
104 | static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *); |
105 | static void frag6_deq(struct ip6asfrag *); | |
106 | static void frag6_insque(struct ip6q *, struct ip6q *); | |
107 | static void frag6_remque(struct ip6q *); | |
39236c6e A |
108 | static void frag6_freef(struct ip6q *, struct fq6_head *, struct fq6_head *); |
109 | ||
0a7de745 | 110 | static int frag6_timeout_run; /* frag6 timer is scheduled to run */ |
39236c6e A |
111 | static void frag6_timeout(void *); |
112 | static void frag6_sched_timeout(void); | |
113 | ||
114 | static struct ip6q *ip6q_alloc(int); | |
115 | static void ip6q_free(struct ip6q *); | |
116 | static void ip6q_updateparams(void); | |
117 | static struct ip6asfrag *ip6af_alloc(int); | |
118 | static void ip6af_free(struct ip6asfrag *); | |
119 | ||
120 | decl_lck_mtx_data(static, ip6qlock); | |
0a7de745 A |
121 | static lck_attr_t *ip6qlock_attr; |
122 | static lck_grp_t *ip6qlock_grp; | |
123 | static lck_grp_attr_t *ip6qlock_grp_attr; | |
39236c6e A |
124 | |
125 | /* IPv6 fragment reassembly queues (protected by ip6qlock) */ | |
0a7de745 A |
126 | static struct ip6q ip6q; /* ip6 reassembly queues */ |
127 | static int ip6_maxfragpackets; /* max packets in reass queues */ | |
128 | static u_int32_t frag6_nfragpackets; /* # of packets in reass queues */ | |
129 | static int ip6_maxfrags; /* max fragments in reass queues */ | |
130 | static u_int32_t frag6_nfrags; /* # of fragments in reass queues */ | |
131 | static u_int32_t ip6q_limit; /* ip6q allocation limit */ | |
132 | static u_int32_t ip6q_count; /* current # of allocated ip6q's */ | |
133 | static u_int32_t ip6af_limit; /* ip6asfrag allocation limit */ | |
134 | static u_int32_t ip6af_count; /* current # of allocated ip6asfrag's */ | |
39236c6e A |
135 | |
136 | static int sysctl_maxfragpackets SYSCTL_HANDLER_ARGS; | |
137 | static int sysctl_maxfrags SYSCTL_HANDLER_ARGS; | |
138 | ||
139 | SYSCTL_DECL(_net_inet6_ip6); | |
140 | ||
141 | SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_MAXFRAGPACKETS, maxfragpackets, | |
142 | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_maxfragpackets, 0, | |
143 | sysctl_maxfragpackets, "I", | |
144 | "Maximum number of IPv6 fragment reassembly queue entries"); | |
145 | ||
146 | SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, fragpackets, | |
147 | CTLFLAG_RD | CTLFLAG_LOCKED, &frag6_nfragpackets, 0, | |
148 | "Current number of IPv6 fragment reassembly queue entries"); | |
149 | ||
150 | SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_MAXFRAGS, maxfrags, | |
151 | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_maxfrags, 0, | |
152 | sysctl_maxfrags, "I", "Maximum number of IPv6 fragments allowed"); | |
1c79356b | 153 | |
1c79356b A |
154 | /* |
155 | * Initialise reassembly queue and fragment identifier. | |
156 | */ | |
157 | void | |
39236c6e | 158 | frag6_init(void) |
1c79356b | 159 | { |
39236c6e | 160 | /* ip6q_alloc() uses mbufs for IPv6 fragment queue structures */ |
0a7de745 | 161 | _CASSERT(sizeof(struct ip6q) <= _MLEN); |
39236c6e | 162 | /* ip6af_alloc() uses mbufs for IPv6 fragment queue structures */ |
0a7de745 | 163 | _CASSERT(sizeof(struct ip6asfrag) <= _MLEN); |
39236c6e A |
164 | |
165 | /* IPv6 fragment reassembly queue lock */ | |
166 | ip6qlock_grp_attr = lck_grp_attr_alloc_init(); | |
167 | ip6qlock_grp = lck_grp_alloc_init("ip6qlock", ip6qlock_grp_attr); | |
168 | ip6qlock_attr = lck_attr_alloc_init(); | |
169 | lck_mtx_init(&ip6qlock, ip6qlock_grp, ip6qlock_attr); | |
170 | ||
171 | lck_mtx_lock(&ip6qlock); | |
172 | /* Initialize IPv6 reassembly queue. */ | |
173 | ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q; | |
1c79356b | 174 | |
39236c6e | 175 | /* same limits as IPv4 */ |
483a1d10 | 176 | ip6_maxfragpackets = nmbclusters / 32; |
39236c6e A |
177 | ip6_maxfrags = ip6_maxfragpackets * 2; |
178 | ip6q_updateparams(); | |
179 | lck_mtx_unlock(&ip6qlock); | |
180 | } | |
9bccf70c | 181 | |
39236c6e A |
182 | static void |
183 | frag6_save_context(struct mbuf *m, int val) | |
184 | { | |
185 | m->m_pkthdr.pkt_hdr = (void *)(uintptr_t)val; | |
186 | } | |
187 | ||
188 | static void | |
189 | frag6_scrub_context(struct mbuf *m) | |
190 | { | |
191 | m->m_pkthdr.pkt_hdr = NULL; | |
192 | } | |
193 | ||
194 | static int | |
195 | frag6_restore_context(struct mbuf *m) | |
196 | { | |
0a7de745 | 197 | return (int)m->m_pkthdr.pkt_hdr; |
39236c6e A |
198 | } |
199 | ||
200 | /* | |
201 | * Send any deferred ICMP param problem error messages; caller must not be | |
202 | * holding ip6qlock and is expected to have saved the per-packet parameter | |
203 | * value via frag6_save_context(). | |
204 | */ | |
205 | static void | |
206 | frag6_icmp6_paramprob_error(struct fq6_head *diq6) | |
207 | { | |
5ba3f43e | 208 | LCK_MTX_ASSERT(&ip6qlock, LCK_MTX_ASSERT_NOTOWNED); |
39236c6e A |
209 | |
210 | if (!MBUFQ_EMPTY(diq6)) { | |
211 | struct mbuf *merr, *merr_tmp; | |
212 | int param; | |
213 | MBUFQ_FOREACH_SAFE(merr, diq6, merr_tmp) { | |
214 | MBUFQ_REMOVE(diq6, merr); | |
215 | MBUFQ_NEXT(merr) = NULL; | |
216 | param = frag6_restore_context(merr); | |
217 | frag6_scrub_context(merr); | |
218 | icmp6_error(merr, ICMP6_PARAM_PROB, | |
219 | ICMP6_PARAMPROB_HEADER, param); | |
220 | } | |
221 | } | |
222 | } | |
223 | ||
224 | /* | |
225 | * Send any deferred ICMP time exceeded error messages; | |
226 | * caller must not be holding ip6qlock. | |
227 | */ | |
228 | static void | |
229 | frag6_icmp6_timeex_error(struct fq6_head *diq6) | |
230 | { | |
5ba3f43e | 231 | LCK_MTX_ASSERT(&ip6qlock, LCK_MTX_ASSERT_NOTOWNED); |
39236c6e A |
232 | |
233 | if (!MBUFQ_EMPTY(diq6)) { | |
234 | struct mbuf *m, *m_tmp; | |
235 | MBUFQ_FOREACH_SAFE(m, diq6, m_tmp) { | |
236 | MBUFQ_REMOVE(diq6, m); | |
237 | MBUFQ_NEXT(m) = NULL; | |
3e170ce0 A |
238 | icmp6_error_flag(m, ICMP6_TIME_EXCEEDED, |
239 | ICMP6_TIME_EXCEED_REASSEMBLY, 0, 0); | |
39236c6e A |
240 | } |
241 | } | |
1c79356b A |
242 | } |
243 | ||
244 | /* | |
245 | * In RFC2460, fragment and reassembly rule do not agree with each other, | |
246 | * in terms of next header field handling in fragment header. | |
247 | * While the sender will use the same value for all of the fragmented packets, | |
248 | * receiver is suggested not to check the consistency. | |
249 | * | |
250 | * fragment rule (p20): | |
251 | * (2) A Fragment header containing: | |
252 | * The Next Header value that identifies the first header of | |
253 | * the Fragmentable Part of the original packet. | |
254 | * -> next header field is same for all fragments | |
255 | * | |
256 | * reassembly rule (p21): | |
257 | * The Next Header field of the last header of the Unfragmentable | |
258 | * Part is obtained from the Next Header field of the first | |
259 | * fragment's Fragment header. | |
260 | * -> should grab it from the first fragment only | |
261 | * | |
262 | * The following note also contradicts with fragment rule - noone is going to | |
263 | * send different fragment with different next header field. | |
264 | * | |
265 | * additional note (p22): | |
266 | * The Next Header values in the Fragment headers of different | |
267 | * fragments of the same original packet may differ. Only the value | |
268 | * from the Offset zero fragment packet is used for reassembly. | |
269 | * -> should grab it from the first fragment only | |
270 | * | |
271 | * There is no explicit reason given in the RFC. Historical reason maybe? | |
272 | */ | |
273 | /* | |
274 | * Fragment input | |
275 | */ | |
276 | int | |
6d2010ae | 277 | frag6_input(struct mbuf **mp, int *offp, int proto) |
1c79356b | 278 | { |
6d2010ae | 279 | #pragma unused(proto) |
1c79356b A |
280 | struct mbuf *m = *mp, *t; |
281 | struct ip6_hdr *ip6; | |
282 | struct ip6_frag *ip6f; | |
283 | struct ip6q *q6; | |
284 | struct ip6asfrag *af6, *ip6af, *af6dwn; | |
285 | int offset = *offp, nxt, i, next; | |
286 | int first_frag = 0; | |
0a7de745 | 287 | int fragoff, frgpartlen; /* must be larger than u_int16_t */ |
39236c6e | 288 | struct ifnet *dstifp = NULL; |
6d2010ae | 289 | u_int8_t ecn, ecn0; |
39236c6e A |
290 | uint32_t csum, csum_flags; |
291 | struct fq6_head diq6; | |
292 | int locked = 0; | |
6d2010ae | 293 | |
39236c6e A |
294 | VERIFY(m->m_flags & M_PKTHDR); |
295 | ||
0a7de745 | 296 | MBUFQ_INIT(&diq6); /* for deferred ICMP param problem errors */ |
1c79356b | 297 | |
316670eb A |
298 | /* Expect 32-bit aligned data pointer on strict-align platforms */ |
299 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); | |
300 | ||
1c79356b | 301 | ip6 = mtod(m, struct ip6_hdr *); |
39236c6e | 302 | IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), goto done); |
1c79356b | 303 | ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset); |
1c79356b | 304 | |
1c79356b A |
305 | #ifdef IN6_IFSTAT_STRICT |
306 | /* find the destination interface of the packet. */ | |
39236c6e A |
307 | if (m->m_pkthdr.pkt_flags & PKTF_IFAINFO) { |
308 | uint32_t idx; | |
309 | ||
310 | if (ip6_getdstifaddr_info(m, &idx, NULL) == 0) { | |
311 | if (idx > 0 && idx <= if_index) { | |
312 | ifnet_head_lock_shared(); | |
313 | dstifp = ifindex2ifnet[idx]; | |
314 | ifnet_head_done(); | |
315 | } | |
b0d623f7 | 316 | } |
1c79356b | 317 | } |
39236c6e A |
318 | #endif /* IN6_IFSTAT_STRICT */ |
319 | ||
320 | /* we are violating the spec, this may not be the dst interface */ | |
0a7de745 | 321 | if (dstifp == NULL) { |
1c79356b | 322 | dstifp = m->m_pkthdr.rcvif; |
0a7de745 | 323 | } |
1c79356b A |
324 | |
325 | /* jumbo payload can't contain a fragment header */ | |
326 | if (ip6->ip6_plen == 0) { | |
327 | icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset); | |
328 | in6_ifstat_inc(dstifp, ifs6_reass_fail); | |
39236c6e A |
329 | m = NULL; |
330 | goto done; | |
1c79356b A |
331 | } |
332 | ||
333 | /* | |
334 | * check whether fragment packet's fragment length is | |
335 | * multiple of 8 octets. | |
336 | * sizeof(struct ip6_frag) == 8 | |
337 | * sizeof(struct ip6_hdr) = 40 | |
338 | */ | |
339 | if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) && | |
340 | (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) { | |
39236c6e A |
341 | icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, |
342 | offsetof(struct ip6_hdr, ip6_plen)); | |
1c79356b | 343 | in6_ifstat_inc(dstifp, ifs6_reass_fail); |
39236c6e A |
344 | m = NULL; |
345 | goto done; | |
346 | } | |
347 | ||
348 | /* If ip6_maxfragpackets or ip6_maxfrags is 0, never accept fragments */ | |
349 | if (ip6_maxfragpackets == 0 || ip6_maxfrags == 0) { | |
350 | ip6stat.ip6s_fragments++; | |
351 | ip6stat.ip6s_fragdropped++; | |
352 | in6_ifstat_inc(dstifp, ifs6_reass_fail); | |
353 | m_freem(m); | |
354 | m = NULL; | |
355 | goto done; | |
1c79356b A |
356 | } |
357 | ||
1c79356b A |
358 | /* offset now points to data portion */ |
359 | offset += sizeof(struct ip6_frag); | |
360 | ||
39037602 A |
361 | /* |
362 | * RFC 6946: Handle "atomic" fragments (offset and m bit set to 0) | |
363 | * upfront, unrelated to any reassembly. Just skip the fragment header. | |
364 | */ | |
365 | if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) { | |
366 | /* | |
367 | * In ICMPv6 processing, we drop certain | |
368 | * NDP messages that are not expected to | |
369 | * have fragment header based on recommendations | |
370 | * against security vulnerability as described in | |
371 | * RFC 6980. | |
372 | * We set PKTF_REASSEMBLED flag to let ICMPv6 NDP | |
373 | * drop such packets. | |
374 | * However there are already devices running software | |
375 | * that are creating interface with MTU < IPv6 Min | |
376 | * MTU. We should not have allowed that but they are | |
377 | * out, and sending atomic NDP fragments. | |
378 | * For that reason, we do not set the same flag here | |
379 | * and relax the check. | |
380 | */ | |
381 | ip6stat.ip6s_atmfrag_rcvd++; | |
382 | in6_ifstat_inc(dstifp, ifs6_atmfrag_rcvd); | |
383 | *offp = offset; | |
0a7de745 | 384 | return ip6f->ip6f_nxt; |
39037602 A |
385 | } |
386 | ||
91447636 | 387 | /* |
39236c6e A |
388 | * Leverage partial checksum offload for simple UDP/IP fragments, |
389 | * as that is the most common case. | |
390 | * | |
391 | * Perform 1's complement adjustment of octets that got included/ | |
5ba3f43e A |
392 | * excluded in the hardware-calculated checksum value. Also take |
393 | * care of any trailing bytes and subtract out their partial sum. | |
91447636 | 394 | */ |
39236c6e | 395 | if (ip6f->ip6f_nxt == IPPROTO_UDP && |
0a7de745 | 396 | offset == (sizeof(*ip6) + sizeof(*ip6f)) && |
39236c6e A |
397 | (m->m_pkthdr.csum_flags & |
398 | (CSUM_DATA_VALID | CSUM_PARTIAL | CSUM_PSEUDO_HDR)) == | |
399 | (CSUM_DATA_VALID | CSUM_PARTIAL)) { | |
5ba3f43e | 400 | uint32_t start = m->m_pkthdr.csum_rx_start; |
0a7de745 | 401 | uint32_t ip_len = (sizeof(*ip6) + ntohs(ip6->ip6_plen)); |
5ba3f43e A |
402 | int32_t trailer = (m_pktlen(m) - ip_len); |
403 | uint32_t swbytes = (uint32_t)trailer; | |
39236c6e | 404 | |
39236c6e A |
405 | csum = m->m_pkthdr.csum_rx_val; |
406 | ||
5ba3f43e A |
407 | ASSERT(trailer >= 0); |
408 | if (start != offset || trailer != 0) { | |
409 | uint16_t s = 0, d = 0; | |
39236c6e A |
410 | |
411 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) { | |
412 | s = ip6->ip6_src.s6_addr16[1]; | |
0a7de745 | 413 | ip6->ip6_src.s6_addr16[1] = 0; |
39236c6e A |
414 | } |
415 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) { | |
416 | d = ip6->ip6_dst.s6_addr16[1]; | |
417 | ip6->ip6_dst.s6_addr16[1] = 0; | |
418 | } | |
419 | ||
420 | /* callee folds in sum */ | |
5ba3f43e A |
421 | csum = m_adj_sum16(m, start, offset, |
422 | (ip_len - offset), csum); | |
0a7de745 | 423 | if (offset > start) { |
5ba3f43e | 424 | swbytes += (offset - start); |
0a7de745 | 425 | } else { |
5ba3f43e | 426 | swbytes += (start - offset); |
0a7de745 | 427 | } |
39236c6e | 428 | |
0a7de745 | 429 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) { |
39236c6e | 430 | ip6->ip6_src.s6_addr16[1] = s; |
0a7de745 A |
431 | } |
432 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) { | |
39236c6e | 433 | ip6->ip6_dst.s6_addr16[1] = d; |
0a7de745 | 434 | } |
39236c6e A |
435 | } |
436 | csum_flags = m->m_pkthdr.csum_flags; | |
5ba3f43e | 437 | |
0a7de745 | 438 | if (swbytes != 0) { |
5ba3f43e | 439 | udp_in6_cksum_stats(swbytes); |
0a7de745 A |
440 | } |
441 | if (trailer != 0) { | |
5ba3f43e | 442 | m_adj(m, -trailer); |
0a7de745 | 443 | } |
39236c6e A |
444 | } else { |
445 | csum = 0; | |
446 | csum_flags = 0; | |
447 | } | |
448 | ||
449 | /* Invalidate checksum */ | |
450 | m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID; | |
451 | ||
452 | ip6stat.ip6s_fragments++; | |
453 | in6_ifstat_inc(dstifp, ifs6_reass_reqd); | |
454 | ||
455 | lck_mtx_lock(&ip6qlock); | |
456 | locked = 1; | |
91447636 | 457 | |
0a7de745 | 458 | for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next) { |
1c79356b A |
459 | if (ip6f->ip6f_ident == q6->ip6q_ident && |
460 | IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) && | |
0a7de745 | 461 | IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)) { |
1c79356b | 462 | break; |
0a7de745 A |
463 | } |
464 | } | |
1c79356b A |
465 | |
466 | if (q6 == &ip6q) { | |
467 | /* | |
468 | * the first fragment to arrive, create a reassembly queue. | |
469 | */ | |
470 | first_frag = 1; | |
1c79356b | 471 | |
39236c6e | 472 | q6 = ip6q_alloc(M_DONTWAIT); |
0a7de745 | 473 | if (q6 == NULL) { |
1c79356b | 474 | goto dropfrag; |
0a7de745 | 475 | } |
1c79356b A |
476 | |
477 | frag6_insque(q6, &ip6q); | |
39236c6e | 478 | frag6_nfragpackets++; |
1c79356b A |
479 | |
480 | /* ip6q_nxt will be filled afterwards, from 1st fragment */ | |
0a7de745 | 481 | q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6; |
9bccf70c | 482 | #ifdef notyet |
0a7de745 | 483 | q6->ip6q_nxtp = (u_char *)nxtp; |
1c79356b | 484 | #endif |
0a7de745 A |
485 | q6->ip6q_ident = ip6f->ip6f_ident; |
486 | q6->ip6q_ttl = IPV6_FRAGTTL; | |
487 | q6->ip6q_src = ip6->ip6_src; | |
488 | q6->ip6q_dst = ip6->ip6_dst; | |
489 | q6->ip6q_ecn = | |
6d2010ae | 490 | (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK; |
0a7de745 | 491 | q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */ |
91447636 | 492 | |
39236c6e A |
493 | q6->ip6q_nfrag = 0; |
494 | ||
495 | /* | |
496 | * If the first fragment has valid checksum offload | |
497 | * info, the rest of fragments are eligible as well. | |
498 | */ | |
499 | if (csum_flags != 0) { | |
500 | q6->ip6q_csum = csum; | |
501 | q6->ip6q_csum_flags = csum_flags; | |
502 | } | |
1c79356b A |
503 | } |
504 | ||
505 | /* | |
506 | * If it's the 1st fragment, record the length of the | |
507 | * unfragmentable part and the next header of the fragment header. | |
508 | */ | |
509 | fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK); | |
510 | if (fragoff == 0) { | |
39236c6e A |
511 | q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) - |
512 | sizeof(struct ip6_frag); | |
1c79356b A |
513 | q6->ip6q_nxt = ip6f->ip6f_nxt; |
514 | } | |
515 | ||
516 | /* | |
517 | * Check that the reassembled packet would not exceed 65535 bytes | |
518 | * in size. | |
519 | * If it would exceed, discard the fragment and return an ICMP error. | |
520 | */ | |
521 | frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset; | |
522 | if (q6->ip6q_unfrglen >= 0) { | |
523 | /* The 1st fragment has already arrived. */ | |
524 | if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) { | |
39236c6e A |
525 | lck_mtx_unlock(&ip6qlock); |
526 | locked = 0; | |
1c79356b | 527 | icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, |
39236c6e A |
528 | offset - sizeof(struct ip6_frag) + |
529 | offsetof(struct ip6_frag, ip6f_offlg)); | |
530 | m = NULL; | |
531 | goto done; | |
1c79356b | 532 | } |
39236c6e A |
533 | } else if (fragoff + frgpartlen > IPV6_MAXPACKET) { |
534 | lck_mtx_unlock(&ip6qlock); | |
535 | locked = 0; | |
1c79356b | 536 | icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, |
39236c6e A |
537 | offset - sizeof(struct ip6_frag) + |
538 | offsetof(struct ip6_frag, ip6f_offlg)); | |
539 | m = NULL; | |
540 | goto done; | |
1c79356b A |
541 | } |
542 | /* | |
543 | * If it's the first fragment, do the above check for each | |
544 | * fragment already stored in the reassembly queue. | |
545 | */ | |
546 | if (fragoff == 0) { | |
547 | for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; | |
0a7de745 | 548 | af6 = af6dwn) { |
1c79356b A |
549 | af6dwn = af6->ip6af_down; |
550 | ||
551 | if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen > | |
552 | IPV6_MAXPACKET) { | |
553 | struct mbuf *merr = IP6_REASS_MBUF(af6); | |
554 | struct ip6_hdr *ip6err; | |
555 | int erroff = af6->ip6af_offset; | |
556 | ||
557 | /* dequeue the fragment. */ | |
558 | frag6_deq(af6); | |
39236c6e | 559 | ip6af_free(af6); |
1c79356b A |
560 | |
561 | /* adjust pointer. */ | |
562 | ip6err = mtod(merr, struct ip6_hdr *); | |
563 | ||
564 | /* | |
565 | * Restore source and destination addresses | |
566 | * in the erroneous IPv6 header. | |
567 | */ | |
568 | ip6err->ip6_src = q6->ip6q_src; | |
569 | ip6err->ip6_dst = q6->ip6q_dst; | |
570 | ||
39236c6e | 571 | frag6_save_context(merr, |
0a7de745 | 572 | erroff - sizeof(struct ip6_frag) + |
39236c6e A |
573 | offsetof(struct ip6_frag, ip6f_offlg)); |
574 | ||
575 | MBUFQ_ENQUEUE(&diq6, merr); | |
1c79356b A |
576 | } |
577 | } | |
578 | } | |
579 | ||
39236c6e | 580 | ip6af = ip6af_alloc(M_DONTWAIT); |
0a7de745 | 581 | if (ip6af == NULL) { |
1c79356b | 582 | goto dropfrag; |
0a7de745 | 583 | } |
39236c6e | 584 | |
1c79356b A |
585 | ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG; |
586 | ip6af->ip6af_off = fragoff; | |
587 | ip6af->ip6af_frglen = frgpartlen; | |
588 | ip6af->ip6af_offset = offset; | |
589 | IP6_REASS_MBUF(ip6af) = m; | |
590 | ||
591 | if (first_frag) { | |
592 | af6 = (struct ip6asfrag *)q6; | |
593 | goto insert; | |
594 | } | |
595 | ||
6d2010ae A |
596 | /* |
597 | * Handle ECN by comparing this segment with the first one; | |
598 | * if CE is set, do not lose CE. | |
599 | * drop if CE and not-ECT are mixed for the same packet. | |
600 | */ | |
601 | ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK; | |
602 | ecn0 = q6->ip6q_ecn; | |
603 | if (ecn == IPTOS_ECN_CE) { | |
604 | if (ecn0 == IPTOS_ECN_NOTECT) { | |
39236c6e | 605 | ip6af_free(ip6af); |
6d2010ae A |
606 | goto dropfrag; |
607 | } | |
0a7de745 | 608 | if (ecn0 != IPTOS_ECN_CE) { |
6d2010ae | 609 | q6->ip6q_ecn = IPTOS_ECN_CE; |
0a7de745 | 610 | } |
6d2010ae A |
611 | } |
612 | if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) { | |
39236c6e | 613 | ip6af_free(ip6af); |
6d2010ae A |
614 | goto dropfrag; |
615 | } | |
616 | ||
1c79356b A |
617 | /* |
618 | * Find a segment which begins after this one does. | |
619 | */ | |
620 | for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; | |
0a7de745 A |
621 | af6 = af6->ip6af_down) { |
622 | if (af6->ip6af_off > ip6af->ip6af_off) { | |
1c79356b | 623 | break; |
0a7de745 A |
624 | } |
625 | } | |
1c79356b A |
626 | |
627 | #if 0 | |
628 | /* | |
629 | * If there is a preceding segment, it may provide some of | |
630 | * our data already. If so, drop the data from the incoming | |
631 | * segment. If it provides all of our data, drop us. | |
39236c6e A |
632 | * |
633 | * If some of the data is dropped from the preceding | |
634 | * segment, then it's checksum is invalidated. | |
1c79356b A |
635 | */ |
636 | if (af6->ip6af_up != (struct ip6asfrag *)q6) { | |
637 | i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen | |
0a7de745 | 638 | - ip6af->ip6af_off; |
1c79356b | 639 | if (i > 0) { |
0a7de745 | 640 | if (i >= ip6af->ip6af_frglen) { |
1c79356b | 641 | goto dropfrag; |
0a7de745 | 642 | } |
1c79356b | 643 | m_adj(IP6_REASS_MBUF(ip6af), i); |
39236c6e | 644 | q6->ip6q_csum_flags = 0; |
1c79356b A |
645 | ip6af->ip6af_off += i; |
646 | ip6af->ip6af_frglen -= i; | |
647 | } | |
648 | } | |
649 | ||
650 | /* | |
651 | * While we overlap succeeding segments trim them or, | |
652 | * if they are completely covered, dequeue them. | |
653 | */ | |
654 | while (af6 != (struct ip6asfrag *)q6 && | |
0a7de745 | 655 | ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) { |
1c79356b A |
656 | i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off; |
657 | if (i < af6->ip6af_frglen) { | |
658 | af6->ip6af_frglen -= i; | |
659 | af6->ip6af_off += i; | |
660 | m_adj(IP6_REASS_MBUF(af6), i); | |
39236c6e | 661 | q6->ip6q_csum_flags = 0; |
1c79356b A |
662 | break; |
663 | } | |
664 | af6 = af6->ip6af_down; | |
665 | m_freem(IP6_REASS_MBUF(af6->ip6af_up)); | |
666 | frag6_deq(af6->ip6af_up); | |
667 | } | |
668 | #else | |
669 | /* | |
670 | * If the incoming framgent overlaps some existing fragments in | |
671 | * the reassembly queue, drop it, since it is dangerous to override | |
672 | * existing fragments from a security point of view. | |
6d2010ae A |
673 | * We don't know which fragment is the bad guy - here we trust |
674 | * fragment that came in earlier, with no real reason. | |
675 | * | |
676 | * Note: due to changes after disabling this part, mbuf passed to | |
677 | * m_adj() below now does not meet the requirement. | |
1c79356b A |
678 | */ |
679 | if (af6->ip6af_up != (struct ip6asfrag *)q6) { | |
680 | i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen | |
0a7de745 | 681 | - ip6af->ip6af_off; |
1c79356b | 682 | if (i > 0) { |
0a7de745 | 683 | #if 0 /* suppress the noisy log */ |
1c79356b A |
684 | log(LOG_ERR, "%d bytes of a fragment from %s " |
685 | "overlaps the previous fragment\n", | |
686 | i, ip6_sprintf(&q6->ip6q_src)); | |
9bccf70c | 687 | #endif |
39236c6e | 688 | ip6af_free(ip6af); |
1c79356b A |
689 | goto dropfrag; |
690 | } | |
691 | } | |
692 | if (af6 != (struct ip6asfrag *)q6) { | |
693 | i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off; | |
694 | if (i > 0) { | |
0a7de745 | 695 | #if 0 /* suppress the noisy log */ |
1c79356b A |
696 | log(LOG_ERR, "%d bytes of a fragment from %s " |
697 | "overlaps the succeeding fragment", | |
698 | i, ip6_sprintf(&q6->ip6q_src)); | |
9bccf70c | 699 | #endif |
39236c6e | 700 | ip6af_free(ip6af); |
1c79356b A |
701 | goto dropfrag; |
702 | } | |
703 | } | |
704 | #endif | |
705 | ||
39236c6e A |
706 | /* |
707 | * If this fragment contains similar checksum offload info | |
708 | * as that of the existing ones, accumulate checksum. Otherwise, | |
709 | * invalidate checksum offload info for the entire datagram. | |
710 | */ | |
0a7de745 | 711 | if (csum_flags != 0 && csum_flags == q6->ip6q_csum_flags) { |
39236c6e | 712 | q6->ip6q_csum += csum; |
0a7de745 | 713 | } else if (q6->ip6q_csum_flags != 0) { |
39236c6e | 714 | q6->ip6q_csum_flags = 0; |
0a7de745 | 715 | } |
39236c6e | 716 | |
1c79356b A |
717 | insert: |
718 | ||
719 | /* | |
720 | * Stick new segment in its place; | |
721 | * check for complete reassembly. | |
722 | * Move to front of packet queue, as we are | |
723 | * the most recently active fragmented packet. | |
724 | */ | |
725 | frag6_enq(ip6af, af6->ip6af_up); | |
91447636 A |
726 | frag6_nfrags++; |
727 | q6->ip6q_nfrag++; | |
1c79356b A |
728 | #if 0 /* xxx */ |
729 | if (q6 != ip6q.ip6q_next) { | |
730 | frag6_remque(q6); | |
731 | frag6_insque(q6, &ip6q); | |
732 | } | |
733 | #endif | |
734 | next = 0; | |
735 | for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; | |
0a7de745 | 736 | af6 = af6->ip6af_down) { |
1c79356b | 737 | if (af6->ip6af_off != next) { |
39236c6e A |
738 | lck_mtx_unlock(&ip6qlock); |
739 | locked = 0; | |
740 | m = NULL; | |
741 | goto done; | |
1c79356b A |
742 | } |
743 | next += af6->ip6af_frglen; | |
744 | } | |
745 | if (af6->ip6af_up->ip6af_mff) { | |
39236c6e A |
746 | lck_mtx_unlock(&ip6qlock); |
747 | locked = 0; | |
748 | m = NULL; | |
749 | goto done; | |
1c79356b A |
750 | } |
751 | ||
752 | /* | |
753 | * Reassembly is complete; concatenate fragments. | |
754 | */ | |
755 | ip6af = q6->ip6q_down; | |
756 | t = m = IP6_REASS_MBUF(ip6af); | |
757 | af6 = ip6af->ip6af_down; | |
758 | frag6_deq(ip6af); | |
759 | while (af6 != (struct ip6asfrag *)q6) { | |
760 | af6dwn = af6->ip6af_down; | |
761 | frag6_deq(af6); | |
0a7de745 | 762 | while (t->m_next) { |
1c79356b | 763 | t = t->m_next; |
0a7de745 | 764 | } |
1c79356b A |
765 | t->m_next = IP6_REASS_MBUF(af6); |
766 | m_adj(t->m_next, af6->ip6af_offset); | |
39236c6e | 767 | ip6af_free(af6); |
1c79356b A |
768 | af6 = af6dwn; |
769 | } | |
770 | ||
39236c6e A |
771 | /* |
772 | * Store partial hardware checksum info from the fragment queue; | |
773 | * the receive start offset is set to 40 bytes (see code at the | |
774 | * top of this routine.) | |
775 | */ | |
776 | if (q6->ip6q_csum_flags != 0) { | |
777 | csum = q6->ip6q_csum; | |
778 | ||
779 | ADDCARRY(csum); | |
780 | ||
781 | m->m_pkthdr.csum_rx_val = csum; | |
0a7de745 | 782 | m->m_pkthdr.csum_rx_start = sizeof(struct ip6_hdr); |
39236c6e A |
783 | m->m_pkthdr.csum_flags = q6->ip6q_csum_flags; |
784 | } else if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) || | |
785 | (m->m_pkthdr.pkt_flags & PKTF_LOOP)) { | |
786 | /* loopback checksums are always OK */ | |
787 | m->m_pkthdr.csum_data = 0xffff; | |
788 | m->m_pkthdr.csum_flags &= ~CSUM_PARTIAL; | |
789 | m->m_pkthdr.csum_flags = CSUM_DATA_VALID | CSUM_PSEUDO_HDR; | |
790 | } | |
791 | ||
1c79356b A |
792 | /* adjust offset to point where the original next header starts */ |
793 | offset = ip6af->ip6af_offset - sizeof(struct ip6_frag); | |
39236c6e | 794 | ip6af_free(ip6af); |
1c79356b A |
795 | ip6 = mtod(m, struct ip6_hdr *); |
796 | ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr)); | |
797 | ip6->ip6_src = q6->ip6q_src; | |
798 | ip6->ip6_dst = q6->ip6q_dst; | |
0a7de745 | 799 | if (q6->ip6q_ecn == IPTOS_ECN_CE) { |
6d2010ae | 800 | ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20); |
0a7de745 | 801 | } |
6d2010ae | 802 | |
1c79356b | 803 | nxt = q6->ip6q_nxt; |
39236c6e | 804 | #ifdef notyet |
1c79356b A |
805 | *q6->ip6q_nxtp = (u_char)(nxt & 0xff); |
806 | #endif | |
807 | ||
6d2010ae A |
808 | /* Delete frag6 header */ |
809 | if (m->m_len >= offset + sizeof(struct ip6_frag)) { | |
810 | /* This is the only possible case with !PULLDOWN_TEST */ | |
1c79356b | 811 | ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag), |
39236c6e | 812 | offset); |
1c79356b A |
813 | m->m_data += sizeof(struct ip6_frag); |
814 | m->m_len -= sizeof(struct ip6_frag); | |
815 | } else { | |
816 | /* this comes with no copy if the boundary is on cluster */ | |
817 | if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) { | |
818 | frag6_remque(q6); | |
1c79356b | 819 | frag6_nfragpackets--; |
39236c6e A |
820 | frag6_nfrags -= q6->ip6q_nfrag; |
821 | ip6q_free(q6); | |
1c79356b A |
822 | goto dropfrag; |
823 | } | |
824 | m_adj(t, sizeof(struct ip6_frag)); | |
825 | m_cat(m, t); | |
826 | } | |
827 | ||
828 | /* | |
829 | * Store NXT to the original. | |
830 | */ | |
831 | { | |
832 | char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */ | |
833 | *prvnxtp = nxt; | |
834 | } | |
835 | ||
836 | frag6_remque(q6); | |
1c79356b | 837 | frag6_nfragpackets--; |
39236c6e A |
838 | frag6_nfrags -= q6->ip6q_nfrag; |
839 | ip6q_free(q6); | |
840 | ||
0a7de745 | 841 | if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */ |
39236c6e | 842 | m_fixhdr(m); |
39037602 A |
843 | /* |
844 | * Mark packet as reassembled | |
845 | * In ICMPv6 processing, we drop certain | |
846 | * NDP messages that are not expected to | |
847 | * have fragment header based on recommendations | |
848 | * against security vulnerability as described in | |
849 | * RFC 6980. | |
850 | */ | |
851 | m->m_pkthdr.pkt_flags |= PKTF_REASSEMBLED; | |
852 | } | |
1c79356b | 853 | ip6stat.ip6s_reassembled++; |
1c79356b A |
854 | |
855 | /* | |
856 | * Tell launch routine the next header | |
857 | */ | |
1c79356b A |
858 | *mp = m; |
859 | *offp = offset; | |
860 | ||
39236c6e A |
861 | /* arm the purge timer if not already and if there's work to do */ |
862 | frag6_sched_timeout(); | |
863 | lck_mtx_unlock(&ip6qlock); | |
864 | in6_ifstat_inc(dstifp, ifs6_reass_ok); | |
865 | frag6_icmp6_paramprob_error(&diq6); | |
866 | VERIFY(MBUFQ_EMPTY(&diq6)); | |
0a7de745 | 867 | return nxt; |
39236c6e A |
868 | |
869 | done: | |
870 | VERIFY(m == NULL); | |
871 | if (!locked) { | |
872 | if (frag6_nfragpackets == 0) { | |
873 | frag6_icmp6_paramprob_error(&diq6); | |
874 | VERIFY(MBUFQ_EMPTY(&diq6)); | |
0a7de745 | 875 | return IPPROTO_DONE; |
39236c6e A |
876 | } |
877 | lck_mtx_lock(&ip6qlock); | |
878 | } | |
879 | /* arm the purge timer if not already and if there's work to do */ | |
880 | frag6_sched_timeout(); | |
881 | lck_mtx_unlock(&ip6qlock); | |
882 | frag6_icmp6_paramprob_error(&diq6); | |
883 | VERIFY(MBUFQ_EMPTY(&diq6)); | |
0a7de745 | 884 | return IPPROTO_DONE; |
39236c6e A |
885 | |
886 | dropfrag: | |
1c79356b | 887 | ip6stat.ip6s_fragdropped++; |
39236c6e A |
888 | /* arm the purge timer if not already and if there's work to do */ |
889 | frag6_sched_timeout(); | |
890 | lck_mtx_unlock(&ip6qlock); | |
891 | in6_ifstat_inc(dstifp, ifs6_reass_fail); | |
1c79356b | 892 | m_freem(m); |
39236c6e A |
893 | frag6_icmp6_paramprob_error(&diq6); |
894 | VERIFY(MBUFQ_EMPTY(&diq6)); | |
0a7de745 | 895 | return IPPROTO_DONE; |
1c79356b A |
896 | } |
897 | ||
898 | /* | |
899 | * Free a fragment reassembly header and all | |
900 | * associated datagrams. | |
901 | */ | |
902 | void | |
39236c6e | 903 | frag6_freef(struct ip6q *q6, struct fq6_head *dfq6, struct fq6_head *diq6) |
1c79356b A |
904 | { |
905 | struct ip6asfrag *af6, *down6; | |
906 | ||
5ba3f43e | 907 | LCK_MTX_ASSERT(&ip6qlock, LCK_MTX_ASSERT_OWNED); |
39236c6e | 908 | |
1c79356b | 909 | for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; |
0a7de745 | 910 | af6 = down6) { |
1c79356b A |
911 | struct mbuf *m = IP6_REASS_MBUF(af6); |
912 | ||
913 | down6 = af6->ip6af_down; | |
914 | frag6_deq(af6); | |
915 | ||
916 | /* | |
917 | * Return ICMP time exceeded error for the 1st fragment. | |
918 | * Just free other fragments. | |
919 | */ | |
920 | if (af6->ip6af_off == 0) { | |
921 | struct ip6_hdr *ip6; | |
922 | ||
923 | /* adjust pointer */ | |
924 | ip6 = mtod(m, struct ip6_hdr *); | |
925 | ||
6d2010ae | 926 | /* restore source and destination addresses */ |
1c79356b A |
927 | ip6->ip6_src = q6->ip6q_src; |
928 | ip6->ip6_dst = q6->ip6q_dst; | |
39236c6e A |
929 | |
930 | MBUFQ_ENQUEUE(diq6, m); | |
931 | } else { | |
932 | MBUFQ_ENQUEUE(dfq6, m); | |
933 | } | |
934 | ip6af_free(af6); | |
1c79356b A |
935 | } |
936 | frag6_remque(q6); | |
1c79356b | 937 | frag6_nfragpackets--; |
39236c6e A |
938 | frag6_nfrags -= q6->ip6q_nfrag; |
939 | ip6q_free(q6); | |
1c79356b A |
940 | } |
941 | ||
942 | /* | |
943 | * Put an ip fragment on a reassembly chain. | |
944 | * Like insque, but pointers in middle of structure. | |
945 | */ | |
946 | void | |
39236c6e | 947 | frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6) |
1c79356b | 948 | { |
5ba3f43e | 949 | LCK_MTX_ASSERT(&ip6qlock, LCK_MTX_ASSERT_OWNED); |
39236c6e | 950 | |
1c79356b A |
951 | af6->ip6af_up = up6; |
952 | af6->ip6af_down = up6->ip6af_down; | |
953 | up6->ip6af_down->ip6af_up = af6; | |
954 | up6->ip6af_down = af6; | |
955 | } | |
956 | ||
957 | /* | |
958 | * To frag6_enq as remque is to insque. | |
959 | */ | |
960 | void | |
39236c6e | 961 | frag6_deq(struct ip6asfrag *af6) |
1c79356b | 962 | { |
5ba3f43e | 963 | LCK_MTX_ASSERT(&ip6qlock, LCK_MTX_ASSERT_OWNED); |
39236c6e | 964 | |
1c79356b A |
965 | af6->ip6af_up->ip6af_down = af6->ip6af_down; |
966 | af6->ip6af_down->ip6af_up = af6->ip6af_up; | |
967 | } | |
968 | ||
969 | void | |
39236c6e | 970 | frag6_insque(struct ip6q *new, struct ip6q *old) |
1c79356b | 971 | { |
5ba3f43e | 972 | LCK_MTX_ASSERT(&ip6qlock, LCK_MTX_ASSERT_OWNED); |
39236c6e | 973 | |
1c79356b A |
974 | new->ip6q_prev = old; |
975 | new->ip6q_next = old->ip6q_next; | |
0a7de745 | 976 | old->ip6q_next->ip6q_prev = new; |
1c79356b A |
977 | old->ip6q_next = new; |
978 | } | |
979 | ||
980 | void | |
39236c6e | 981 | frag6_remque(struct ip6q *p6) |
1c79356b | 982 | { |
5ba3f43e | 983 | LCK_MTX_ASSERT(&ip6qlock, LCK_MTX_ASSERT_OWNED); |
39236c6e | 984 | |
1c79356b A |
985 | p6->ip6q_prev->ip6q_next = p6->ip6q_next; |
986 | p6->ip6q_next->ip6q_prev = p6->ip6q_prev; | |
987 | } | |
988 | ||
989 | /* | |
9bccf70c | 990 | * IPv6 reassembling timer processing; |
1c79356b A |
991 | * if a timer expires on a reassembly |
992 | * queue, discard it. | |
993 | */ | |
39236c6e A |
994 | static void |
995 | frag6_timeout(void *arg) | |
1c79356b | 996 | { |
39236c6e A |
997 | #pragma unused(arg) |
998 | struct fq6_head dfq6, diq6; | |
1c79356b | 999 | struct ip6q *q6; |
1c79356b | 1000 | |
0a7de745 A |
1001 | MBUFQ_INIT(&dfq6); /* for deferred frees */ |
1002 | MBUFQ_INIT(&diq6); /* for deferred ICMP time exceeded errors */ | |
39236c6e A |
1003 | |
1004 | /* | |
1005 | * Update coarse-grained networking timestamp (in sec.); the idea | |
1006 | * is to piggy-back on the timeout callout to update the counter | |
1007 | * returnable via net_uptime(). | |
1008 | */ | |
1009 | net_update_uptime(); | |
1010 | ||
1011 | lck_mtx_lock(&ip6qlock); | |
1c79356b | 1012 | q6 = ip6q.ip6q_next; |
0a7de745 | 1013 | if (q6) { |
1c79356b A |
1014 | while (q6 != &ip6q) { |
1015 | --q6->ip6q_ttl; | |
1016 | q6 = q6->ip6q_next; | |
1017 | if (q6->ip6q_prev->ip6q_ttl == 0) { | |
1018 | ip6stat.ip6s_fragtimeout++; | |
1019 | /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ | |
39236c6e | 1020 | frag6_freef(q6->ip6q_prev, &dfq6, &diq6); |
1c79356b A |
1021 | } |
1022 | } | |
0a7de745 | 1023 | } |
1c79356b A |
1024 | /* |
1025 | * If we are over the maximum number of fragments | |
1026 | * (due to the limit being lowered), drain off | |
1027 | * enough to get down to the new limit. | |
1028 | */ | |
39236c6e A |
1029 | if (ip6_maxfragpackets >= 0) { |
1030 | while (frag6_nfragpackets > (unsigned)ip6_maxfragpackets && | |
1031 | ip6q.ip6q_prev) { | |
1032 | ip6stat.ip6s_fragoverflow++; | |
1033 | /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ | |
1034 | frag6_freef(ip6q.ip6q_prev, &dfq6, &diq6); | |
1035 | } | |
1036 | } | |
1037 | /* re-arm the purge timer if there's work to do */ | |
1038 | frag6_timeout_run = 0; | |
1039 | frag6_sched_timeout(); | |
1040 | lck_mtx_unlock(&ip6qlock); | |
1041 | ||
1042 | /* free fragments that need to be freed */ | |
0a7de745 | 1043 | if (!MBUFQ_EMPTY(&dfq6)) { |
39236c6e | 1044 | MBUFQ_DRAIN(&dfq6); |
0a7de745 | 1045 | } |
39236c6e A |
1046 | |
1047 | frag6_icmp6_timeex_error(&diq6); | |
1048 | ||
1049 | VERIFY(MBUFQ_EMPTY(&dfq6)); | |
1050 | VERIFY(MBUFQ_EMPTY(&diq6)); | |
1051 | } | |
1052 | ||
1053 | static void | |
1054 | frag6_sched_timeout(void) | |
1055 | { | |
5ba3f43e | 1056 | LCK_MTX_ASSERT(&ip6qlock, LCK_MTX_ASSERT_OWNED); |
39236c6e A |
1057 | |
1058 | if (!frag6_timeout_run && frag6_nfragpackets > 0) { | |
1059 | frag6_timeout_run = 1; | |
1060 | timeout(frag6_timeout, NULL, hz); | |
1c79356b | 1061 | } |
1c79356b A |
1062 | } |
1063 | ||
1064 | /* | |
1065 | * Drain off all datagram fragments. | |
1066 | */ | |
1067 | void | |
39236c6e | 1068 | frag6_drain(void) |
1c79356b | 1069 | { |
39236c6e A |
1070 | struct fq6_head dfq6, diq6; |
1071 | ||
0a7de745 A |
1072 | MBUFQ_INIT(&dfq6); /* for deferred frees */ |
1073 | MBUFQ_INIT(&diq6); /* for deferred ICMP time exceeded errors */ | |
39236c6e A |
1074 | |
1075 | lck_mtx_lock(&ip6qlock); | |
1c79356b A |
1076 | while (ip6q.ip6q_next != &ip6q) { |
1077 | ip6stat.ip6s_fragdropped++; | |
1078 | /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ | |
39236c6e A |
1079 | frag6_freef(ip6q.ip6q_next, &dfq6, &diq6); |
1080 | } | |
1081 | lck_mtx_unlock(&ip6qlock); | |
1082 | ||
1083 | /* free fragments that need to be freed */ | |
0a7de745 | 1084 | if (!MBUFQ_EMPTY(&dfq6)) { |
39236c6e | 1085 | MBUFQ_DRAIN(&dfq6); |
0a7de745 | 1086 | } |
39236c6e A |
1087 | |
1088 | frag6_icmp6_timeex_error(&diq6); | |
1089 | ||
1090 | VERIFY(MBUFQ_EMPTY(&dfq6)); | |
1091 | VERIFY(MBUFQ_EMPTY(&diq6)); | |
1092 | } | |
1093 | ||
1094 | static struct ip6q * | |
1095 | ip6q_alloc(int how) | |
1096 | { | |
1097 | struct mbuf *t; | |
1098 | struct ip6q *q6; | |
1099 | ||
1100 | /* | |
1101 | * See comments in ip6q_updateparams(). Keep the count separate | |
1102 | * from frag6_nfragpackets since the latter represents the elements | |
1103 | * already in the reassembly queues. | |
1104 | */ | |
0a7de745 A |
1105 | if (ip6q_limit > 0 && ip6q_count > ip6q_limit) { |
1106 | return NULL; | |
1107 | } | |
39236c6e A |
1108 | |
1109 | t = m_get(how, MT_FTABLE); | |
1110 | if (t != NULL) { | |
1111 | atomic_add_32(&ip6q_count, 1); | |
1112 | q6 = mtod(t, struct ip6q *); | |
0a7de745 | 1113 | bzero(q6, sizeof(*q6)); |
39236c6e A |
1114 | } else { |
1115 | q6 = NULL; | |
1116 | } | |
0a7de745 | 1117 | return q6; |
39236c6e A |
1118 | } |
1119 | ||
1120 | static void | |
1121 | ip6q_free(struct ip6q *q6) | |
1122 | { | |
1123 | (void) m_free(dtom(q6)); | |
1124 | atomic_add_32(&ip6q_count, -1); | |
1125 | } | |
1126 | ||
1127 | static struct ip6asfrag * | |
1128 | ip6af_alloc(int how) | |
1129 | { | |
1130 | struct mbuf *t; | |
1131 | struct ip6asfrag *af6; | |
1132 | ||
1133 | /* | |
1134 | * See comments in ip6q_updateparams(). Keep the count separate | |
1135 | * from frag6_nfrags since the latter represents the elements | |
1136 | * already in the reassembly queues. | |
1137 | */ | |
0a7de745 A |
1138 | if (ip6af_limit > 0 && ip6af_count > ip6af_limit) { |
1139 | return NULL; | |
1140 | } | |
39236c6e A |
1141 | |
1142 | t = m_get(how, MT_FTABLE); | |
1143 | if (t != NULL) { | |
1144 | atomic_add_32(&ip6af_count, 1); | |
1145 | af6 = mtod(t, struct ip6asfrag *); | |
0a7de745 | 1146 | bzero(af6, sizeof(*af6)); |
39236c6e A |
1147 | } else { |
1148 | af6 = NULL; | |
1149 | } | |
0a7de745 | 1150 | return af6; |
39236c6e A |
1151 | } |
1152 | ||
1153 | static void | |
1154 | ip6af_free(struct ip6asfrag *af6) | |
1155 | { | |
1156 | (void) m_free(dtom(af6)); | |
1157 | atomic_add_32(&ip6af_count, -1); | |
1158 | } | |
1159 | ||
1160 | static void | |
1161 | ip6q_updateparams(void) | |
1162 | { | |
5ba3f43e | 1163 | LCK_MTX_ASSERT(&ip6qlock, LCK_MTX_ASSERT_OWNED); |
39236c6e A |
1164 | /* |
1165 | * -1 for unlimited allocation. | |
1166 | */ | |
0a7de745 | 1167 | if (ip6_maxfragpackets < 0) { |
39236c6e | 1168 | ip6q_limit = 0; |
0a7de745 A |
1169 | } |
1170 | if (ip6_maxfrags < 0) { | |
39236c6e | 1171 | ip6af_limit = 0; |
0a7de745 | 1172 | } |
39236c6e A |
1173 | /* |
1174 | * Positive number for specific bound. | |
1175 | */ | |
0a7de745 | 1176 | if (ip6_maxfragpackets > 0) { |
39236c6e | 1177 | ip6q_limit = ip6_maxfragpackets; |
0a7de745 A |
1178 | } |
1179 | if (ip6_maxfrags > 0) { | |
39236c6e | 1180 | ip6af_limit = ip6_maxfrags; |
0a7de745 | 1181 | } |
39236c6e A |
1182 | /* |
1183 | * Zero specifies no further fragment queue allocation -- set the | |
1184 | * bound very low, but rely on implementation elsewhere to actually | |
1185 | * prevent allocation and reclaim current queues. | |
1186 | */ | |
0a7de745 | 1187 | if (ip6_maxfragpackets == 0) { |
39236c6e | 1188 | ip6q_limit = 1; |
0a7de745 A |
1189 | } |
1190 | if (ip6_maxfrags == 0) { | |
39236c6e | 1191 | ip6af_limit = 1; |
0a7de745 | 1192 | } |
39236c6e A |
1193 | /* |
1194 | * Arm the purge timer if not already and if there's work to do | |
1195 | */ | |
1196 | frag6_sched_timeout(); | |
1197 | } | |
1198 | ||
1199 | static int | |
1200 | sysctl_maxfragpackets SYSCTL_HANDLER_ARGS | |
1201 | { | |
1202 | #pragma unused(arg1, arg2) | |
1203 | int error, i; | |
1204 | ||
1205 | lck_mtx_lock(&ip6qlock); | |
1206 | i = ip6_maxfragpackets; | |
1207 | error = sysctl_handle_int(oidp, &i, 0, req); | |
0a7de745 | 1208 | if (error || req->newptr == USER_ADDR_NULL) { |
39236c6e | 1209 | goto done; |
0a7de745 | 1210 | } |
39236c6e A |
1211 | /* impose bounds */ |
1212 | if (i < -1 || i > (nmbclusters / 4)) { | |
1213 | error = EINVAL; | |
1214 | goto done; | |
1215 | } | |
1216 | ip6_maxfragpackets = i; | |
1217 | ip6q_updateparams(); | |
1218 | done: | |
1219 | lck_mtx_unlock(&ip6qlock); | |
0a7de745 | 1220 | return error; |
39236c6e A |
1221 | } |
1222 | ||
1223 | static int | |
1224 | sysctl_maxfrags SYSCTL_HANDLER_ARGS | |
1225 | { | |
1226 | #pragma unused(arg1, arg2) | |
1227 | int error, i; | |
1228 | ||
1229 | lck_mtx_lock(&ip6qlock); | |
1230 | i = ip6_maxfrags; | |
1231 | error = sysctl_handle_int(oidp, &i, 0, req); | |
0a7de745 | 1232 | if (error || req->newptr == USER_ADDR_NULL) { |
39236c6e | 1233 | goto done; |
0a7de745 | 1234 | } |
39236c6e A |
1235 | /* impose bounds */ |
1236 | if (i < -1 || i > (nmbclusters / 4)) { | |
1237 | error = EINVAL; | |
1238 | goto done; | |
1c79356b | 1239 | } |
0a7de745 A |
1240 | ip6_maxfrags = i; |
1241 | ip6q_updateparams(); /* see if we need to arm timer */ | |
39236c6e A |
1242 | done: |
1243 | lck_mtx_unlock(&ip6qlock); | |
0a7de745 | 1244 | return error; |
1c79356b | 1245 | } |