1 /*
2 * Copyright (c) 2017-2023 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
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 $ */
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 /*
62 * @file
63 * flowswitch IP Reassembly for both v4 and v6
64 *
65 * Implementation of IP packet fragmentation and reassembly.
66 *
67 */
68
69 #include <sys/domain.h>
70 #include <netinet/in.h>
71 #include <netinet/ip6.h>
72 #include <netinet/icmp6.h>
73 #include <skywalk/os_skywalk_private.h>
74 #include <skywalk/nexus/flowswitch/nx_flowswitch.h>
75 #include <skywalk/nexus/flowswitch/fsw_var.h>
76
77 #define IPFM_MAX_FRAGS_PER_QUEUE 128 /* RFC 791 64K/(512 min MTU) */
78 #define IPFM_MAX_QUEUES 1024 /* same as ip/ip6 */
79 #define IPFM_FRAG_TTL 60 /* RFC 2460 */
80 #define IPFM_TIMEOUT_TCALL_INTERVAL 1
81
82 static uint32_t ipfm_max_frags_per_queue = IPFM_MAX_FRAGS_PER_QUEUE;
83 static uint32_t ipfm_frag_ttl = IPFM_FRAG_TTL;
84 static uint32_t ipfm_timeout_tcall_ival = IPFM_TIMEOUT_TCALL_INTERVAL;
85
86 #if (DEVELOPMENT || DEBUG)
87 SYSCTL_INT(_kern_skywalk_flowswitch, OID_AUTO,
88 ipfm_max_frags_per_queue, CTLFLAG_RW | CTLFLAG_LOCKED,
89 &ipfm_max_frags_per_queue, 0, "");
90 #endif /* !DEVELOPMENT && !DEBUG */
91
92 SYSCTL_INT(_kern_skywalk_flowswitch, OID_AUTO, ipfm_frag_ttl,
93 CTLFLAG_RW | CTLFLAG_LOCKED, &ipfm_frag_ttl, 0, "");
94 SYSCTL_INT(_kern_skywalk_flowswitch, OID_AUTO,
95 ipfm_timeout_tcall_ival, CTLFLAG_RW | CTLFLAG_LOCKED,
96 &ipfm_timeout_tcall_ival, 0, "");
97
98 static LCK_GRP_DECLARE(fsw_ipfm_lock_group, "sk_fsw_ipfm_lock");
99 static LCK_ATTR_DECLARE(fsw_ipfm_lock_attr, 0, 0);
100
101 /* @internal ip fragment wrapper (chained in an ipfq) for __kern_packet */
102 struct ipf {
103 struct ipf *ipf_down;
104 struct ipf *ipf_up;
105 struct __kern_packet *ipf_pkt;
106 int ipf_len; /* fragmentable part length */
107 int ipf_off; /* fragment offset */
108 uint16_t ipf_mff; /* more fragment bit in frag off */
109 };
110
111 /* @internal ip fragment lookup key */
112 struct ipf_key {
113 uint64_t ipfk_addr[4]; /* src + dst ip addr (v4/v6) */
114 uint32_t ipfk_ident; /* IP identification */
115 uint16_t ipfk_len; /* len of ipfk_addr field */
116 };
117
118 enum {
119 IPFK_LEN_V4 = 2 * sizeof(struct in_addr),
120 IPFK_LEN_V6 = 2 * sizeof(struct in6_addr),
121 };
122
123 /*
124 * @internal
125 * IP reassembly queue structure. Each fragment (struct ipf)
126 * being reassembled is attached to one of these structures.
127 */
128 struct ipfq {
129 struct ipf *ipfq_down; /* fragment chain */
130 struct ipf *ipfq_up;
131 struct ipfq *ipfq_next; /* queue chain */
132 struct ipfq *ipfq_prev;
133 uint64_t ipfq_timestamp; /* time of creation */
134 struct ipf_key ipfq_key; /* ipfq search key */
135 uint16_t ipfq_nfrag; /* # of fragments in queue */
136 int ipfq_unfraglen; /* len of unfragmentable part */
137 bool ipfq_is_dirty; /* q is dirty, don't use */
138 };
139
140 /*
141 * @internal (externally opaque)
142 * flowswitch IP Fragment Manager
143 */
144 struct fsw_ip_frag_mgr {
145 struct skoid ipfm_skoid;
146 struct ipfq ipfm_q; /* ip reassembly queues */
147 uint32_t ipfm_q_limit; /* limit # of reass queues */
148 uint32_t ipfm_q_count; /* # of allocated reass queues */
149 uint32_t ipfm_f_limit; /* limit # of ipfs */
150 uint32_t ipfm_f_count; /* current # of allocated ipfs */
151 decl_lck_mtx_data(, ipfm_lock); /* guard reass and timeout cleanup */
152 thread_call_t ipfm_timeout_tcall; /* frag timeout thread */
153
154 struct ifnet *ipfm_ifp;
155 struct fsw_stats *ipfm_stats; /* indirect stats in fsw */
156 };
157
158 static int ipf_process(struct fsw_ip_frag_mgr *, struct __kern_packet **,
159 struct ipf_key *, uint16_t, uint16_t, uint16_t, uint16_t, uint16_t *,
160 uint16_t *);
161 static int ipf_key_cmp(struct ipf_key *, struct ipf_key *);
162 static void ipf_enq(struct ipf *, struct ipf *);
163 static void ipf_deq(struct ipf *);
164 static void ipfq_insque(struct ipfq *, struct ipfq *);
165 static void ipfq_remque(struct ipfq *);
166 static uint32_t ipfq_freef(struct fsw_ip_frag_mgr *mgr, struct ipfq *,
167 void (*)(struct fsw_ip_frag_mgr *, struct ipf *));
168
169 static void ipfq_timeout(thread_call_param_t, thread_call_param_t);
170 static void ipfq_sched_timeout(struct fsw_ip_frag_mgr *, boolean_t);
171
172 static struct ipfq *ipfq_alloc(struct fsw_ip_frag_mgr *mgr);
173 static void ipfq_free(struct fsw_ip_frag_mgr *mgr, struct ipfq *q);
174 static uint32_t ipfq_freefq(struct fsw_ip_frag_mgr *mgr, struct ipfq *q,
175 void (*ipf_cb)(struct fsw_ip_frag_mgr *, struct ipf *));
176 static struct ipf *ipf_alloc(struct fsw_ip_frag_mgr *mgr);
177 static void ipf_free(struct fsw_ip_frag_mgr *mgr, struct ipf *f);
178 static void ipf_free_pkt(struct ipf *f);
179 static void ipfq_drain(struct fsw_ip_frag_mgr *mgr);
180 static void ipfq_reap(struct fsw_ip_frag_mgr *mgr);
181 static int ipfq_drain_sysctl SYSCTL_HANDLER_ARGS;
182 void ipf_icmp_param_err(struct fsw_ip_frag_mgr *, struct __kern_packet *pkt,
183 int param);
184 void ipf_icmp_timeout_err(struct fsw_ip_frag_mgr *, struct ipf *f);
185
186 /* Create a flowswitch IP fragment manager. */
187 struct fsw_ip_frag_mgr *
fsw_ip_frag_mgr_create(struct nx_flowswitch * fsw,struct ifnet * ifp,size_t f_limit)188 fsw_ip_frag_mgr_create(struct nx_flowswitch *fsw, struct ifnet *ifp,
189 size_t f_limit)
190 {
191 struct fsw_ip_frag_mgr *mgr;
192
193 ASSERT(ifp != NULL);
194 mgr = sk_alloc_type(struct fsw_ip_frag_mgr, Z_WAITOK | Z_NOFAIL,
195 skmem_tag_fsw_frag_mgr);
196 mgr->ipfm_q.ipfq_next = mgr->ipfm_q.ipfq_prev = &mgr->ipfm_q;
197 lck_mtx_init(&mgr->ipfm_lock, &fsw_ipfm_lock_group, &fsw_ipfm_lock_attr);
198
199 mgr->ipfm_timeout_tcall =
200 thread_call_allocate_with_options(ipfq_timeout, mgr,
201 THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
202 VERIFY(mgr->ipfm_timeout_tcall != NULL);
203
204 mgr->ipfm_ifp = ifp;
205 mgr->ipfm_stats = &fsw->fsw_stats;
206
207 /* Use caller provided limit (caller knows pool size) */
208 ASSERT(f_limit >= 2 && f_limit < UINT32_MAX);
209 mgr->ipfm_f_limit = (uint32_t)f_limit;
210 mgr->ipfm_f_count = 0;
211 mgr->ipfm_q_limit = MIN(IPFM_MAX_QUEUES, mgr->ipfm_f_limit / 2);
212 mgr->ipfm_q_count = 0;
213
214 skoid_create(&mgr->ipfm_skoid, SKOID_DNODE(fsw->fsw_skoid), "ipfm", 0);
215 skoid_add_uint(&mgr->ipfm_skoid, "frag_limit", CTLFLAG_RW,
216 &mgr->ipfm_f_limit);
217 skoid_add_uint(&mgr->ipfm_skoid, "frag_count", CTLFLAG_RD,
218 &mgr->ipfm_f_count);
219 skoid_add_uint(&mgr->ipfm_skoid, "queue_limit", CTLFLAG_RW,
220 &mgr->ipfm_q_limit);
221 skoid_add_uint(&mgr->ipfm_skoid, "queue_count", CTLFLAG_RD,
222 &mgr->ipfm_q_count);
223 skoid_add_handler(&mgr->ipfm_skoid, "drain", CTLFLAG_RW,
224 ipfq_drain_sysctl, mgr, 0);
225
226 return mgr;
227 }
228
229 /* Free a flowswitch IP fragment manager. */
230 void
fsw_ip_frag_mgr_destroy(struct fsw_ip_frag_mgr * mgr)231 fsw_ip_frag_mgr_destroy(struct fsw_ip_frag_mgr *mgr)
232 {
233 thread_call_t __single tcall;
234
235 lck_mtx_lock(&mgr->ipfm_lock);
236 if ((tcall = mgr->ipfm_timeout_tcall) != NULL) {
237 lck_mtx_unlock(&mgr->ipfm_lock);
238 (void) thread_call_cancel_wait(tcall);
239 (void) thread_call_free(tcall);
240 mgr->ipfm_timeout_tcall = NULL;
241 lck_mtx_lock(&mgr->ipfm_lock);
242 }
243
244 ipfq_drain(mgr);
245
246 lck_mtx_unlock(&mgr->ipfm_lock);
247 lck_mtx_destroy(&mgr->ipfm_lock, &fsw_ipfm_lock_group);
248
249 skoid_destroy(&mgr->ipfm_skoid);
250 sk_free_type(struct fsw_ip_frag_mgr, mgr);
251 }
252
253 /*
254 * Reassemble a received IPv4 fragment.
255 *
256 * @param mgr
257 * fragment manager
258 * @param pkt
259 * received packet (must have ipv4 header validated)
260 * @param ip4
261 * pointer to the packet's IPv4 header
262 * @param nfrags
263 * number of fragments reassembled
264 * @return
265 * Successfully processed (not fully reassembled)
266 * ret = 0, *pkt = NULL(ipfm owns it), *nfrags=0
267 * Successfully reassembled
268 * ret = 0, *pkt = 1st fragment(fragments chained in order by pkt_nextpkt)
269 * *nfrags = number of all fragments (>0)
270 * Error
271 * ret != 0 && *pkt unmodified (caller to decide what to do with *pkt)
272 * *nfrags = 0
273 */
274 int
fsw_ip_frag_reass_v4(struct fsw_ip_frag_mgr * mgr,struct __kern_packet ** pkt,struct ip * ip4,uint16_t * nfrags,uint16_t * tlen)275 fsw_ip_frag_reass_v4(struct fsw_ip_frag_mgr *mgr, struct __kern_packet **pkt,
276 struct ip *ip4, uint16_t *nfrags, uint16_t *tlen)
277 {
278 struct ipf_key key;
279 uint16_t unfragpartlen, offflag, fragoff, fragpartlen, fragflag;
280 int err;
281 uint8_t *src;
282
283 STATS_INC(mgr->ipfm_stats, FSW_STATS_RX_FRAG_V4);
284
285 src = (uint8_t *)(struct ip *__bidi_indexable)ip4 +
286 offsetof(struct ip, ip_src);
287 bcopy(src, (void *)key.ipfk_addr, IPFK_LEN_V4);
288 key.ipfk_len = IPFK_LEN_V4;
289 key.ipfk_ident = (uint32_t)ip4->ip_id;
290
291 unfragpartlen = (uint16_t)(ip4->ip_hl << 2);
292 offflag = ntohs(ip4->ip_off);
293 fragoff = (uint16_t)(offflag << 3);
294 fragpartlen = ntohs(ip4->ip_len) - (uint16_t)(ip4->ip_hl << 2);
295 fragflag = offflag & IP_MF;
296
297 err = ipf_process(mgr, pkt, &key, unfragpartlen, fragoff, fragpartlen,
298 fragflag, nfrags, tlen);
299
300 /*
301 * If packet has been reassembled compute the user data length.
302 */
303 if (*pkt != NULL) {
304 struct __kern_packet *p = *pkt;
305 struct ip *__single iph = __unsafe_forge_single(struct ip *,
306 (struct ip *)p->pkt_flow_ip_hdr);
307
308 p->pkt_flow_ulen = ntohs(iph->ip_len) -
309 p->pkt_flow_ip_hlen - p->pkt_flow->flow_l4._l4_hlen;
310 }
311 return err;
312 }
313
314 /*
315 * Reassemble a received IPv6 fragment.
316 *
317 * @param mgr
318 * fragment manager
319 * @param pkt
320 * received packet (must have ipv6 header validated)
321 * @param ip6
322 * pointer to the packet's IPv6 header
323 * @param ip6f
324 * pointer to the packet's IPv6 Fragment Header
325 * @param nfrags
326 * number of fragments reassembled
327 * @return
328 * Successfully processed (not fully reassembled)
329 * ret = 0, *pkt = NULL(ipfm owns it), *nfrags=0
330 * Successfully reassembled
331 * ret = 0, *pkt = 1st fragment(fragments chained in ordrer by pkt_nextpkt)
332 * *nfrags = number of all fragments (>0)
333 * Error
334 * ret != 0 && *pkt unmodified (caller to decide what to do with *pkt)
335 * *nfrags = 0
336 */
337 int
fsw_ip_frag_reass_v6(struct fsw_ip_frag_mgr * mgr,struct __kern_packet ** pkt,struct ip6_hdr * ip6,struct ip6_frag * ip6f,uint16_t * nfrags,uint16_t * tlen)338 fsw_ip_frag_reass_v6(struct fsw_ip_frag_mgr *mgr, struct __kern_packet **pkt,
339 struct ip6_hdr *ip6, struct ip6_frag *ip6f, uint16_t *nfrags,
340 uint16_t *tlen)
341 {
342 struct ipf_key key;
343 ptrdiff_t ip6f_ptroff = (uintptr_t)ip6f - (uintptr_t)ip6;
344 uint16_t ip6f_off, fragoff, fragpartlen, unfragpartlen, fragflag;
345 int err;
346 uint8_t *src;
347
348 STATS_INC(mgr->ipfm_stats, FSW_STATS_RX_FRAG_V6);
349
350 /* jumbo payload can't contain a fragment header */
351 if (ip6->ip6_plen == 0) {
352 *nfrags = 0;
353 return ERANGE;
354 }
355
356 ASSERT(ip6f_ptroff < UINT16_MAX);
357 ip6f_off = (uint16_t)ip6f_ptroff;
358 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
359 fragpartlen = ntohs(ip6->ip6_plen) -
360 (ip6f_off + sizeof(struct ip6_frag) - sizeof(struct ip6_hdr));
361 unfragpartlen = ip6f_off;
362 fragflag = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
363
364 /*
365 * RFC 6946: Handle "atomic" fragments (offset and m bit set to 0)
366 * upfront, unrelated to any reassembly.
367 *
368 * Flow classifier should process those as non-frag, ipfm shouldn't see
369 * them.
370 */
371 ASSERT((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) != 0);
372
373 src = (uint8_t *)(struct ip6_hdr *__bidi_indexable)ip6 +
374 offsetof(struct ip6_hdr, ip6_src);
375 bcopy(src, (void *)key.ipfk_addr, IPFK_LEN_V6);
376 key.ipfk_len = IPFK_LEN_V6;
377 key.ipfk_ident = ip6f->ip6f_ident;
378
379 err = ipf_process(mgr, pkt, &key, unfragpartlen, fragoff, fragpartlen,
380 fragflag, nfrags, tlen);
381
382 /*
383 * If packet has been reassembled compute the user data length.
384 */
385 if (*pkt != NULL) {
386 struct __kern_packet *p = *pkt;
387 struct ip6_hdr *__single ip6h = __unsafe_forge_single(struct ip6_hdr *,
388 (struct ip6_hdr *)p->pkt_flow_ip_hdr);
389
390 p->pkt_flow_ulen = ntohs(ip6h->ip6_plen) -
391 p->pkt_flow->flow_l4._l4_hlen;
392 }
393 return err;
394 }
395
396 static struct mbuf *
ipf_pkt2mbuf(struct fsw_ip_frag_mgr * mgr,struct __kern_packet * pkt)397 ipf_pkt2mbuf(struct fsw_ip_frag_mgr *mgr, struct __kern_packet *pkt)
398 {
399 unsigned int one = 1;
400 struct mbuf *__single m = NULL;
401 struct mbuf *pkt_mbuf = pkt->pkt_mbuf;
402 uint8_t *buf;
403 struct ip6_hdr *ip6;
404 uint32_t l3t_len;
405 int err;
406
407 l3t_len = pkt->pkt_length - pkt->pkt_l2_len;
408 if (pkt->pkt_link_flags & PKT_LINKF_ETHFCS) {
409 l3t_len -= ETHER_CRC_LEN;
410 }
411
412 err = mbuf_allocpacket(MBUF_WAITOK, l3t_len, &one, &m);
413 VERIFY(err == 0);
414 ASSERT(l3t_len <= mbuf_maxlen(m));
415
416 if (pkt->pkt_pflags & PKT_F_MBUF_DATA) {
417 if ((pkt_mbuf->m_len < l3t_len) &&
418 (pkt_mbuf = m_pullup(pkt->pkt_mbuf, l3t_len)) == NULL) {
419 return NULL;
420 } else {
421 pkt->pkt_mbuf = pkt_mbuf;
422 bcopy(m_mtod_current(pkt->pkt_mbuf) + pkt->pkt_l2_len,
423 m_mtod_current(m), l3t_len);
424 }
425 } else {
426 MD_BUFLET_ADDR_ABS(pkt, buf);
427 buf += (pkt->pkt_headroom + pkt->pkt_l2_len);
428 bcopy(buf, m_mtod_current(m), l3t_len);
429 }
430 m->m_pkthdr.len = m->m_len = l3t_len;
431
432 ip6 = mtod(m, struct ip6_hdr *);
433 /* note for casting: IN6_IS_SCOPE_ doesn't need alignment */
434 if (IN6_IS_SCOPE_LINKLOCAL((struct in6_addr *)(uintptr_t)&ip6->ip6_src)) {
435 if (in6_embedded_scope) {
436 ip6->ip6_src.s6_addr16[1] = htons(mgr->ipfm_ifp->if_index);
437 }
438 ip6_output_setsrcifscope(m, mgr->ipfm_ifp->if_index, NULL);
439 }
440 if (IN6_IS_SCOPE_EMBED((struct in6_addr *)(uintptr_t)&ip6->ip6_dst)) {
441 if (in6_embedded_scope) {
442 ip6->ip6_dst.s6_addr16[1] = htons(mgr->ipfm_ifp->if_index);
443 }
444 ip6_output_setdstifscope(m, mgr->ipfm_ifp->if_index, NULL);
445 }
446
447 return m;
448 }
449
450 /*
451 * Since this function can be called while holding fsw_ip_frag_mgr.ipfm_lock,
452 * we need to ensure we don't enter the driver directly because a deadlock
453 * can happen if this same thread tries to get the workloop lock.
454 */
455 static void
ipf_icmp6_error_flag(struct mbuf * m,int type,int code,int param,int flags)456 ipf_icmp6_error_flag(struct mbuf *m, int type, int code, int param, int flags)
457 {
458 sk_protect_t protect = sk_async_transmit_protect();
459 icmp6_error_flag(m, type, code, param, flags);
460 sk_async_transmit_unprotect(protect);
461 }
462
463 /*
464 * @internal IP fragment ICMP parameter problem error handling
465 *
466 * @param param
467 * offending parameter offset, only applicable to ICMPv6
468 */
469 void
ipf_icmp_param_err(struct fsw_ip_frag_mgr * mgr,struct __kern_packet * pkt,int param_offset)470 ipf_icmp_param_err(struct fsw_ip_frag_mgr *mgr, struct __kern_packet *pkt,
471 int param_offset)
472 {
473 if (pkt->pkt_flow_ip_ver != IPV6_VERSION) {
474 return;
475 }
476
477 struct mbuf *m = NULL;
478 m = ipf_pkt2mbuf(mgr, pkt);
479 if (__probable(m != NULL)) {
480 ipf_icmp6_error_flag(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
481 param_offset, 0);
482 }
483
484 /* m would be free by icmp6_error_flag function */
485 }
486
487 /* @internal IP fragment ICMP timeout error handling */
488 void
ipf_icmp_timeout_err(struct fsw_ip_frag_mgr * mgr,struct ipf * f)489 ipf_icmp_timeout_err(struct fsw_ip_frag_mgr *mgr, struct ipf *f)
490 {
491 struct __kern_packet *pkt = f->ipf_pkt;
492 ASSERT(pkt != NULL);
493
494 /* no icmp error packet for ipv4 */
495 if (pkt->pkt_flow_ip_ver != IPV6_VERSION) {
496 return;
497 }
498
499 /* only for the first fragment */
500 if (f->ipf_off != 0) {
501 return;
502 }
503
504 struct mbuf *m = NULL;
505 m = ipf_pkt2mbuf(mgr, pkt);
506 if (__probable(m != NULL)) {
507 ipf_icmp6_error_flag(m, ICMP6_TIME_EXCEEDED,
508 ICMP6_TIME_EXCEED_REASSEMBLY, 0, 0);
509 }
510
511 /* m would be free by icmp6_error_flag function */
512 }
513
514 /* @internal IP fragment processing, v4/v6 agonistic */
515 int
ipf_process(struct fsw_ip_frag_mgr * mgr,struct __kern_packet ** pkt_ptr,struct ipf_key * key,uint16_t unfraglen,uint16_t fragoff,uint16_t fragpartlen,uint16_t fragflag,uint16_t * nfrags,uint16_t * tlen)516 ipf_process(struct fsw_ip_frag_mgr *mgr, struct __kern_packet **pkt_ptr,
517 struct ipf_key *key, uint16_t unfraglen, uint16_t fragoff,
518 uint16_t fragpartlen, uint16_t fragflag, uint16_t *nfrags, uint16_t *tlen)
519 {
520 struct __kern_packet *pkt = *pkt_ptr;
521 struct __kern_packet *pkt_reassed = NULL;
522 struct ipfq *q, *mq = &mgr->ipfm_q;
523 struct ipf *f, *f_new, *f_down;
524 uint32_t nfrags_freed;
525 int next;
526 int first_frag = 0;
527 int err = 0;
528 int local_ipfq_unfraglen;
529
530 *nfrags = 0;
531
532 SK_DF(SK_VERB_IP_FRAG, "id %5d fragoff %5d fragpartlen %5d "
533 "fragflag 0x%x", key->ipfk_ident, fragoff, fragpartlen, fragflag);
534
535 /*
536 * Make sure that all fragments except last one have a data length
537 * that's a non-zero multiple of 8 bytes.
538 */
539 if (fragflag && (fragpartlen == 0 || (fragpartlen & 0x7) != 0)) {
540 SK_DF(SK_VERB_IP_FRAG, "frag not multiple of 8 bytes");
541 STATS_INC(mgr->ipfm_stats, FSW_STATS_RX_FRAG_DROP_BAD_LEN);
542 ipf_icmp_param_err(mgr, pkt,
543 offsetof(struct ip6_hdr, ip6_plen));
544 return ERANGE;
545 }
546
547 lck_mtx_lock(&mgr->ipfm_lock);
548
549 /* find ipfq */
550 for (q = mq->ipfq_next; q != mq; q = q->ipfq_next) {
551 if (ipf_key_cmp(key, &q->ipfq_key) == 0) {
552 if (q->ipfq_is_dirty) {
553 SK_DF(SK_VERB_IP_FRAG, "found dirty q, skip");
554 err = EINVAL;
555 goto done;
556 }
557 break;
558 }
559 }
560
561 /* not found, create new ipfq */
562 if (q == mq) {
563 first_frag = 1;
564
565 q = ipfq_alloc(mgr);
566 if (q == NULL) {
567 STATS_INC(mgr->ipfm_stats,
568 FSW_STATS_RX_FRAG_DROP_NOMEM);
569 err = ENOMEM;
570 goto done;
571 }
572
573 ipfq_insque(q, mq);
574 net_update_uptime();
575
576 bcopy(key, &q->ipfq_key, sizeof(struct ipf_key));
577 q->ipfq_down = q->ipfq_up = (struct ipf *)q;
578 q->ipfq_unfraglen = -1; /* The 1st fragment has not arrived. */
579 q->ipfq_nfrag = 0;
580 q->ipfq_timestamp = _net_uptime;
581 }
582
583 ASSERT(!q->ipfq_is_dirty);
584
585 /* this queue has reached per queue frag limit */
586 if (q->ipfq_nfrag > ipfm_max_frags_per_queue) {
587 nfrags_freed = ipfq_freefq(mgr, q, NULL);
588 STATS_ADD(mgr->ipfm_stats,
589 FSW_STATS_RX_FRAG_DROP_PER_QUEUE_LIMIT, nfrags_freed);
590 err = ENOMEM;
591 goto done;
592 }
593
594 local_ipfq_unfraglen = q->ipfq_unfraglen;
595
596 /*
597 * If it's the 1st fragment, record the length of the
598 * unfragmentable part and the next header of the fragment header.
599 * Assume the first fragement to arrive will be correct.
600 * We do not have any duplicate checks here yet so another packet
601 * with fragoff == 0 could come and overwrite the ipfq_unfraglen
602 * and worse, the next header, at any time.
603 */
604 if (fragoff == 0 && local_ipfq_unfraglen == -1) {
605 local_ipfq_unfraglen = unfraglen;
606 }
607
608 /* Check that the reassembled packet would not exceed 65535 bytes. */
609 if (local_ipfq_unfraglen + fragoff + fragpartlen > IP_MAXPACKET) {
610 SK_DF(SK_VERB_IP_FRAG, "frag too big");
611 STATS_INC(mgr->ipfm_stats, FSW_STATS_RX_FRAG_BAD);
612 ipf_icmp_param_err(mgr, pkt, sizeof(struct ip6_hdr) +
613 offsetof(struct ip6_frag, ip6f_offlg));
614 err = ERANGE;
615 goto done;
616 }
617
618 /*
619 * If it's the 1st fragment, do the above check for each
620 * fragment already stored in the reassembly queue.
621 * If an error is found, still return 0, since we don't return
622 * ownership of a chain of offending packets back to caller.
623 */
624 if (fragoff == 0) {
625 for (f = q->ipfq_down; f != (struct ipf *)q; f = f_down) {
626 f_down = f->ipf_down;
627 if (local_ipfq_unfraglen + f->ipf_off + f->ipf_len >
628 IP_MAXPACKET) {
629 SK_DF(SK_VERB_IP_FRAG, "frag too big");
630 STATS_INC(mgr->ipfm_stats,
631 FSW_STATS_RX_FRAG_BAD);
632 ipf_deq(f);
633 ipf_free_pkt(f);
634 ipf_free(mgr, f);
635 }
636 }
637 }
638
639 f_new = ipf_alloc(mgr);
640 if (f_new == NULL) {
641 STATS_INC(mgr->ipfm_stats, FSW_STATS_RX_FRAG_DROP_NOMEM);
642 err = ENOMEM;
643 goto done;
644 }
645
646 f_new->ipf_mff = fragflag;
647 f_new->ipf_off = fragoff;
648 f_new->ipf_len = fragpartlen;
649 f_new->ipf_pkt = pkt;
650
651 if (first_frag) {
652 f = (struct ipf *)q;
653 goto insert;
654 }
655
656 /* Find a segment which begins after this one does. */
657 for (f = q->ipfq_down; f != (struct ipf *)q; f = f->ipf_down) {
658 if (f->ipf_off > f_new->ipf_off) {
659 break;
660 }
661 }
662
663 /*
664 * If any of the fragments being reassembled overlap with any
665 * other fragments being reassembled for the same packet,
666 * reassembly of that packet must be abandoned and all the
667 * fragments that have been received for that packet must be
668 * discarded, and no ICMP error messages should be sent.
669 *
670 * It should be noted that fragments may be duplicated in the
671 * network. Instead of treating these exact duplicate fragments
672 * as overlapping fragments, an implementation may choose to
673 * detect this case and drop exact duplicate fragments while
674 * keeping the other fragments belonging to the same packet.
675 *
676 * https://tools.ietf.org/html/rfc8200#appendix-B
677 *
678 * We apply this rule for both for IPv4 and IPv6 here.
679 */
680 if (((f->ipf_up != (struct ipf *)q) && /* prev frag spans into f_new */
681 (f->ipf_up->ipf_off + f->ipf_up->ipf_len - f_new->ipf_off > 0)) ||
682 ((f != (struct ipf *)q) && /* f_new spans into next */
683 (f_new->ipf_off + f_new->ipf_len - f->ipf_off > 0))) {
684 STATS_INC(mgr->ipfm_stats, FSW_STATS_RX_FRAG_BAD);
685 /* Check for exact duplicate offset/length */
686 if (((f->ipf_up != (struct ipf *)q) &&
687 ((f->ipf_up->ipf_off != f_new->ipf_off) ||
688 (f->ipf_up->ipf_len != f_new->ipf_len))) ||
689 ((f != (struct ipf *)q) &&
690 ((f->ipf_off != f_new->ipf_off) ||
691 (f->ipf_len != f_new->ipf_len)))) {
692 SK_DF(SK_VERB_IP_FRAG, "frag overlap");
693 ipf_free(mgr, f_new);
694 /* give up over-lapping fragments queue */
695 SK_DF(SK_VERB_IP_FRAG, "free overlapping queue");
696 ipfq_freef(mgr, q, NULL);
697 q->ipfq_is_dirty = true;
698 } else {
699 ipf_free(mgr, f_new);
700 SK_DF(SK_VERB_IP_FRAG, "frag dup");
701 }
702 err = ERANGE;
703 goto done;
704 }
705
706 insert:
707 q->ipfq_unfraglen = local_ipfq_unfraglen;
708
709 /*
710 * Stick new segment in its place;
711 * check for complete reassembly.
712 * Move to front of packet queue, as we are
713 * the most recently active fragmented packet.
714 */
715 ipf_enq(f_new, f->ipf_up);
716 q->ipfq_nfrag++;
717 next = 0;
718 for (f = q->ipfq_down; f != (struct ipf *)q; f = f->ipf_down) {
719 /* there is a hole */
720 if (f->ipf_off != next) {
721 goto done;
722 }
723 next += f->ipf_len;
724 }
725 /* we haven't got last frag yet */
726 if (f->ipf_up->ipf_mff) {
727 goto done;
728 }
729
730 /*
731 * Reassembly is complete; concatenate fragments.
732 */
733 f = q->ipfq_down;
734 f_down = f->ipf_down;
735 pkt_reassed = f->ipf_pkt;
736 *nfrags = 1;
737 while (f_down != (struct ipf *)q) {
738 /* chain __kern_packet with pkt_nextpkt ptr */
739 f->ipf_pkt->pkt_nextpkt = f_down->ipf_pkt;
740 (*nfrags)++;
741 (*tlen) += f_down->ipf_len;
742 f_down = f->ipf_down;
743 ipf_deq(f);
744 ipf_free(mgr, f);
745 f = f_down;
746 f_down = f->ipf_down;
747 }
748 ipf_free(mgr, f);
749
750 err = 0;
751 STATS_INC(mgr->ipfm_stats, FSW_STATS_RX_FRAG_REASSED);
752 ipfq_remque(q);
753 ipfq_free(mgr, q);
754
755 done:
756 /* ipfm take ownership of, or return assembled packet, if no error */
757 if (err == 0) {
758 /* reass'ed packet if done; NULL otherwise */
759 *pkt_ptr = pkt_reassed;
760 }
761 ipfq_sched_timeout(mgr, FALSE);
762 lck_mtx_unlock(&mgr->ipfm_lock);
763 return err;
764 }
765
766 static int
ipf_key_cmp(struct ipf_key * a,struct ipf_key * b)767 ipf_key_cmp(struct ipf_key *a, struct ipf_key *b)
768 {
769 int d;
770
771 if ((d = (a->ipfk_len - b->ipfk_len)) != 0) {
772 return d;
773 }
774
775 if ((d = (a->ipfk_ident - b->ipfk_ident)) != 0) {
776 return d;
777 }
778
779 return memcmp(a->ipfk_addr, b->ipfk_addr, a->ipfk_len);
780 }
781
782 /*
783 * Put an ip fragment on a reassembly chain.
784 * Like insque, but pointers in middle of structure.
785 */
786 static void
ipf_enq(struct ipf * f,struct ipf * up6)787 ipf_enq(struct ipf *f, struct ipf *up6)
788 {
789 f->ipf_up = up6;
790 f->ipf_down = up6->ipf_down;
791 up6->ipf_down->ipf_up = f;
792 up6->ipf_down = f;
793 }
794
795 /*
796 * To ipf_enq as remque is to insque.
797 */
798 static void
ipf_deq(struct ipf * f)799 ipf_deq(struct ipf *f)
800 {
801 f->ipf_up->ipf_down = f->ipf_down;
802 f->ipf_down->ipf_up = f->ipf_up;
803 }
804
805 static void
ipfq_insque(struct ipfq * new,struct ipfq * old)806 ipfq_insque(struct ipfq *new, struct ipfq *old)
807 {
808 new->ipfq_prev = old;
809 new->ipfq_next = old->ipfq_next;
810 old->ipfq_next->ipfq_prev = new;
811 old->ipfq_next = new;
812 }
813
814 static void
ipfq_remque(struct ipfq * p6)815 ipfq_remque(struct ipfq *p6)
816 {
817 p6->ipfq_prev->ipfq_next = p6->ipfq_next;
818 p6->ipfq_next->ipfq_prev = p6->ipfq_prev;
819 }
820
821 /*
822 * @internal drain reassembly queue till reaching target q count.
823 */
824 static void
_ipfq_reap(struct fsw_ip_frag_mgr * mgr,uint32_t target_q_count,void (* ipf_cb)(struct fsw_ip_frag_mgr *,struct ipf *))825 _ipfq_reap(struct fsw_ip_frag_mgr *mgr, uint32_t target_q_count,
826 void (*ipf_cb)(struct fsw_ip_frag_mgr *, struct ipf *))
827 {
828 uint32_t n_freed = 0;
829
830 LCK_MTX_ASSERT(&mgr->ipfm_lock, LCK_MTX_ASSERT_OWNED);
831
832 SK_DF(SK_VERB_IP_FRAG, "draining (frag %d/%d queue %d/%d)",
833 mgr->ipfm_f_count, mgr->ipfm_f_limit, mgr->ipfm_q_count,
834 mgr->ipfm_q_limit);
835
836 while (mgr->ipfm_q.ipfq_next != &mgr->ipfm_q &&
837 mgr->ipfm_q_count > target_q_count) {
838 n_freed += ipfq_freefq(mgr, mgr->ipfm_q.ipfq_prev,
839 mgr->ipfm_q.ipfq_prev->ipfq_is_dirty ? NULL : ipf_cb);
840 }
841
842 STATS_ADD(mgr->ipfm_stats, FSW_STATS_RX_FRAG_DROP_REAPED, n_freed);
843 }
844
845 /*
846 * @internal reap half reassembly queues to allow newer fragment assembly.
847 */
848 static void
ipfq_reap(struct fsw_ip_frag_mgr * mgr)849 ipfq_reap(struct fsw_ip_frag_mgr *mgr)
850 {
851 _ipfq_reap(mgr, mgr->ipfm_q_count / 2, ipf_icmp_timeout_err);
852 }
853
854 /*
855 * @internal reap all reassembly queues, for shutdown etc.
856 */
857 static void
ipfq_drain(struct fsw_ip_frag_mgr * mgr)858 ipfq_drain(struct fsw_ip_frag_mgr *mgr)
859 {
860 _ipfq_reap(mgr, 0, NULL);
861 }
862
863 static void
ipfq_timeout(thread_call_param_t arg0,thread_call_param_t arg1)864 ipfq_timeout(thread_call_param_t arg0, thread_call_param_t arg1)
865 {
866 #pragma unused(arg1)
867 struct fsw_ip_frag_mgr *__single mgr = arg0;
868 struct ipfq *q;
869 uint64_t now, elapsed;
870 uint32_t n_freed = 0;
871
872 net_update_uptime();
873 now = _net_uptime;
874
875 SK_DF(SK_VERB_IP_FRAG, "run");
876 lck_mtx_lock(&mgr->ipfm_lock);
877 q = mgr->ipfm_q.ipfq_next;
878 if (q) {
879 while (q != &mgr->ipfm_q) {
880 q = q->ipfq_next;
881 elapsed = now - q->ipfq_prev->ipfq_timestamp;
882 if (elapsed > ipfm_frag_ttl) {
883 SK_DF(SK_VERB_IP_FRAG, "timing out q id %5d",
884 q->ipfq_prev->ipfq_key.ipfk_ident);
885 n_freed = ipfq_freefq(mgr, q->ipfq_prev,
886 q->ipfq_is_dirty ? NULL :
887 ipf_icmp_timeout_err);
888 }
889 }
890 }
891 STATS_ADD(mgr->ipfm_stats, FSW_STATS_RX_FRAG_DROP_TIMEOUT, n_freed);
892
893 /* If running out of resources, drain ipfm queues (oldest one first) */
894 if (mgr->ipfm_f_count >= mgr->ipfm_f_limit ||
895 mgr->ipfm_q_count >= mgr->ipfm_q_limit) {
896 ipfq_reap(mgr);
897 }
898
899 /* re-arm the purge timer if there's work to do */
900 if (mgr->ipfm_q_count > 0) {
901 ipfq_sched_timeout(mgr, TRUE);
902 }
903 lck_mtx_unlock(&mgr->ipfm_lock);
904 }
905
906 static void
ipfq_sched_timeout(struct fsw_ip_frag_mgr * mgr,boolean_t in_tcall)907 ipfq_sched_timeout(struct fsw_ip_frag_mgr *mgr, boolean_t in_tcall)
908 {
909 uint32_t delay = MAX(1, ipfm_timeout_tcall_ival); /* seconds */
910 thread_call_t __single tcall = mgr->ipfm_timeout_tcall;
911 uint64_t now = mach_absolute_time();
912 uint64_t ival, deadline = now;
913
914 LCK_MTX_ASSERT(&mgr->ipfm_lock, LCK_MTX_ASSERT_OWNED);
915
916 ASSERT(tcall != NULL);
917 if (mgr->ipfm_q_count > 0 &&
918 (!thread_call_isactive(tcall) || in_tcall)) {
919 nanoseconds_to_absolutetime(delay * NSEC_PER_SEC, &ival);
920 clock_deadline_for_periodic_event(ival, now, &deadline);
921 (void) thread_call_enter_delayed(tcall, deadline);
922 }
923 }
924
925 static int
926 ipfq_drain_sysctl SYSCTL_HANDLER_ARGS
927 {
928 #pragma unused(oidp, arg2)
929 struct fsw_ip_frag_mgr *__single mgr = arg1;
930
931 SKOID_PROC_CALL_GUARD;
932
933 lck_mtx_lock(&mgr->ipfm_lock);
934 ipfq_drain(mgr);
935 lck_mtx_unlock(&mgr->ipfm_lock);
936
937 return 0;
938 }
939
940 static struct ipfq *
ipfq_alloc(struct fsw_ip_frag_mgr * mgr)941 ipfq_alloc(struct fsw_ip_frag_mgr *mgr)
942 {
943 struct ipfq *q;
944
945 if (mgr->ipfm_q_count > mgr->ipfm_q_limit) {
946 ipfq_reap(mgr);
947 }
948 ASSERT(mgr->ipfm_q_count <= mgr->ipfm_q_limit);
949
950 q = kalloc_type(struct ipfq, Z_WAITOK | Z_ZERO);
951 if (q != NULL) {
952 mgr->ipfm_q_count++;
953 q->ipfq_is_dirty = false;
954 }
955 return q;
956 }
957
958 /* free q */
959 static void
ipfq_free(struct fsw_ip_frag_mgr * mgr,struct ipfq * q)960 ipfq_free(struct fsw_ip_frag_mgr *mgr, struct ipfq *q)
961 {
962 kfree_type(struct ipfq, q);
963 mgr->ipfm_q_count--;
964 }
965
966 /*
967 * Free all fragments, keep q.
968 * @return: number of frags freed
969 */
970 static uint32_t
ipfq_freef(struct fsw_ip_frag_mgr * mgr,struct ipfq * q,void (* ipf_cb)(struct fsw_ip_frag_mgr *,struct ipf *))971 ipfq_freef(struct fsw_ip_frag_mgr *mgr, struct ipfq *q,
972 void (*ipf_cb)(struct fsw_ip_frag_mgr *, struct ipf *))
973 {
974 struct ipf *f, *down6;
975 uint32_t nfrags = 0;
976
977 for (f = q->ipfq_down; f != (struct ipf *)q; f = down6) {
978 nfrags++;
979 down6 = f->ipf_down;
980 ipf_deq(f);
981 if (ipf_cb != NULL) {
982 (*ipf_cb)(mgr, f);
983 }
984 ipf_free_pkt(f);
985 ipf_free(mgr, f);
986 }
987
988 return nfrags;
989 }
990
991 /* Free both all fragments and q
992 * @return: number of frags freed
993 */
994 static uint32_t
ipfq_freefq(struct fsw_ip_frag_mgr * mgr,struct ipfq * q,void (* ipf_cb)(struct fsw_ip_frag_mgr *,struct ipf *))995 ipfq_freefq(struct fsw_ip_frag_mgr *mgr, struct ipfq *q,
996 void (*ipf_cb)(struct fsw_ip_frag_mgr *, struct ipf *))
997 {
998 uint32_t freed_count;
999 freed_count = ipfq_freef(mgr, q, ipf_cb);
1000 ipfq_remque(q);
1001 ipfq_free(mgr, q);
1002 return freed_count;
1003 }
1004
1005 static struct ipf *
ipf_alloc(struct fsw_ip_frag_mgr * mgr)1006 ipf_alloc(struct fsw_ip_frag_mgr *mgr)
1007 {
1008 struct ipf *f;
1009
1010 if (mgr->ipfm_f_count > mgr->ipfm_f_limit) {
1011 STATS_INC(mgr->ipfm_stats, FSW_STATS_RX_FRAG_DROP_FRAG_LIMIT);
1012 return NULL;
1013 }
1014
1015 f = kalloc_type(struct ipf, Z_WAITOK | Z_ZERO);
1016 if (f != NULL) {
1017 mgr->ipfm_f_count++;
1018 }
1019 return f;
1020 }
1021
1022 static void
ipf_free_pkt(struct ipf * f)1023 ipf_free_pkt(struct ipf *f)
1024 {
1025 struct __kern_packet *pkt = f->ipf_pkt;
1026 ASSERT(pkt != NULL);
1027 pp_free_packet(__DECONST(struct kern_pbufpool *, pkt->pkt_qum.qum_pp),
1028 SK_PTR_ADDR(pkt));
1029 }
1030
1031 static void
ipf_free(struct fsw_ip_frag_mgr * mgr,struct ipf * f)1032 ipf_free(struct fsw_ip_frag_mgr *mgr, struct ipf *f)
1033 {
1034 kfree_type(struct ipf, f);
1035 mgr->ipfm_f_count--;
1036 }
1037