xref: /xnu-8020.121.3/bsd/netinet/flow_divert.c (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1 /*
2  * Copyright (c) 2012-2022 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 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/syslog.h>
32 #include <sys/queue.h>
33 #include <sys/malloc.h>
34 #include <sys/socket.h>
35 #include <sys/kpi_mbuf.h>
36 #include <sys/mbuf.h>
37 #include <sys/domain.h>
38 #include <sys/protosw.h>
39 #include <sys/socketvar.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <sys/kern_control.h>
43 #include <sys/ubc.h>
44 #include <sys/codesign.h>
45 #include <libkern/tree.h>
46 #include <kern/locks.h>
47 #include <kern/debug.h>
48 #include <kern/task.h>
49 #include <mach/task_info.h>
50 #include <net/if_var.h>
51 #include <net/route.h>
52 #include <net/flowhash.h>
53 #include <net/ntstat.h>
54 #include <net/content_filter.h>
55 #include <net/necp.h>
56 #include <netinet/in.h>
57 #include <netinet/in_var.h>
58 #include <netinet/tcp.h>
59 #include <netinet/tcp_var.h>
60 #include <netinet/tcp_fsm.h>
61 #include <netinet/flow_divert.h>
62 #include <netinet/flow_divert_proto.h>
63 #include <netinet6/in6_pcb.h>
64 #include <netinet6/ip6protosw.h>
65 #include <dev/random/randomdev.h>
66 #include <libkern/crypto/sha1.h>
67 #include <libkern/crypto/crypto_internal.h>
68 #include <os/log.h>
69 #include <corecrypto/cc.h>
70 #if CONTENT_FILTER
71 #include <net/content_filter.h>
72 #endif /* CONTENT_FILTER */
73 
74 #define FLOW_DIVERT_CONNECT_STARTED             0x00000001
75 #define FLOW_DIVERT_READ_CLOSED                 0x00000002
76 #define FLOW_DIVERT_WRITE_CLOSED                0x00000004
77 #define FLOW_DIVERT_TUNNEL_RD_CLOSED    0x00000008
78 #define FLOW_DIVERT_TUNNEL_WR_CLOSED    0x00000010
79 #define FLOW_DIVERT_HAS_HMAC            0x00000040
80 #define FLOW_DIVERT_NOTIFY_ON_RECEIVED  0x00000080
81 #define FLOW_DIVERT_IMPLICIT_CONNECT    0x00000100
82 #define FLOW_DIVERT_DID_SET_LOCAL_ADDR  0x00000200
83 #define FLOW_DIVERT_HAS_TOKEN           0x00000400
84 #define FLOW_DIVERT_SHOULD_SET_LOCAL_ADDR 0x00000800
85 #define FLOW_DIVERT_FLOW_IS_TRANSPARENT   0x00001000
86 
87 #define FDLOG(level, pcb, format, ...) \
88 	os_log_with_type(OS_LOG_DEFAULT, flow_divert_syslog_type_to_oslog_type(level), "(%u): " format "\n", (pcb)->hash, __VA_ARGS__)
89 
90 #define FDLOG0(level, pcb, msg) \
91 	os_log_with_type(OS_LOG_DEFAULT, flow_divert_syslog_type_to_oslog_type(level), "(%u): " msg "\n", (pcb)->hash)
92 
93 #define FDRETAIN(pcb)                   if ((pcb) != NULL) OSIncrementAtomic(&(pcb)->ref_count)
94 #define FDRELEASE(pcb)                                                                                                          \
95 	do {                                                                                                                                    \
96 	        if ((pcb) != NULL && 1 == OSDecrementAtomic(&(pcb)->ref_count)) {       \
97 	                flow_divert_pcb_destroy(pcb);                                                                   \
98 	        }                                                                                                                                       \
99 	} while (0)
100 
101 #define FDLOCK(pcb)                                             lck_mtx_lock(&(pcb)->mtx)
102 #define FDUNLOCK(pcb)                                   lck_mtx_unlock(&(pcb)->mtx)
103 
104 #define FD_CTL_SENDBUFF_SIZE                    (128 * 1024)
105 
106 #define GROUP_BIT_CTL_ENQUEUE_BLOCKED   0
107 
108 #define GROUP_COUNT_MAX                                 31
109 #define FLOW_DIVERT_MAX_NAME_SIZE               4096
110 #define FLOW_DIVERT_MAX_KEY_SIZE                1024
111 #define FLOW_DIVERT_MAX_TRIE_MEMORY             (1024 * 1024)
112 
113 struct flow_divert_trie_node {
114 	uint16_t start;
115 	uint16_t length;
116 	uint16_t child_map;
117 };
118 
119 #define CHILD_MAP_SIZE                  256
120 #define NULL_TRIE_IDX                   0xffff
121 #define TRIE_NODE(t, i)                 ((t)->nodes[(i)])
122 #define TRIE_CHILD(t, i, b)             (((t)->child_maps + (CHILD_MAP_SIZE * TRIE_NODE(t, i).child_map))[(b)])
123 #define TRIE_BYTE(t, i)                 ((t)->bytes[(i)])
124 
125 static struct flow_divert_pcb           nil_pcb;
126 
127 static LCK_ATTR_DECLARE(flow_divert_mtx_attr, 0, 0);
128 static LCK_GRP_DECLARE(flow_divert_mtx_grp, FLOW_DIVERT_CONTROL_NAME);
129 static LCK_RW_DECLARE_ATTR(g_flow_divert_group_lck, &flow_divert_mtx_grp,
130     &flow_divert_mtx_attr);
131 
132 static struct flow_divert_group         **g_flow_divert_groups  = NULL;
133 static uint32_t                         g_active_group_count    = 0;
134 
135 static  errno_t                         g_init_result           = 0;
136 
137 static  kern_ctl_ref                    g_flow_divert_kctl_ref  = NULL;
138 
139 static struct protosw                   g_flow_divert_in_protosw;
140 static struct pr_usrreqs                g_flow_divert_in_usrreqs;
141 static struct protosw                   g_flow_divert_in_udp_protosw;
142 static struct pr_usrreqs                g_flow_divert_in_udp_usrreqs;
143 static struct ip6protosw                g_flow_divert_in6_protosw;
144 static struct pr_usrreqs                g_flow_divert_in6_usrreqs;
145 static struct ip6protosw                g_flow_divert_in6_udp_protosw;
146 static struct pr_usrreqs                g_flow_divert_in6_udp_usrreqs;
147 
148 static struct protosw                   *g_tcp_protosw          = NULL;
149 static struct ip6protosw                *g_tcp6_protosw         = NULL;
150 static struct protosw                   *g_udp_protosw          = NULL;
151 static struct ip6protosw                *g_udp6_protosw         = NULL;
152 
153 ZONE_DEFINE(flow_divert_group_zone, "flow_divert_group",
154     sizeof(struct flow_divert_group), ZC_ZFREE_CLEARMEM);
155 ZONE_DEFINE(flow_divert_pcb_zone, "flow_divert_pcb",
156     sizeof(struct flow_divert_pcb), ZC_ZFREE_CLEARMEM);
157 
158 static errno_t
159 flow_divert_dup_addr(sa_family_t family, struct sockaddr *addr, struct sockaddr **dup);
160 
161 static boolean_t
162 flow_divert_is_sockaddr_valid(struct sockaddr *addr);
163 
164 static int
165 flow_divert_append_target_endpoint_tlv(mbuf_t connect_packet, struct sockaddr *toaddr);
166 
167 struct sockaddr *
168 flow_divert_get_buffered_target_address(mbuf_t buffer);
169 
170 static void
171 flow_divert_disconnect_socket(struct socket *so, bool is_connected);
172 
173 static inline uint8_t
flow_divert_syslog_type_to_oslog_type(int syslog_type)174 flow_divert_syslog_type_to_oslog_type(int syslog_type)
175 {
176 	switch (syslog_type) {
177 	case LOG_ERR: return OS_LOG_TYPE_ERROR;
178 	case LOG_INFO: return OS_LOG_TYPE_INFO;
179 	case LOG_DEBUG: return OS_LOG_TYPE_DEBUG;
180 	default: return OS_LOG_TYPE_DEFAULT;
181 	}
182 }
183 
184 static inline int
flow_divert_pcb_cmp(const struct flow_divert_pcb * pcb_a,const struct flow_divert_pcb * pcb_b)185 flow_divert_pcb_cmp(const struct flow_divert_pcb *pcb_a, const struct flow_divert_pcb *pcb_b)
186 {
187 	return memcmp(&pcb_a->hash, &pcb_b->hash, sizeof(pcb_a->hash));
188 }
189 
190 RB_PROTOTYPE(fd_pcb_tree, flow_divert_pcb, rb_link, flow_divert_pcb_cmp);
191 RB_GENERATE(fd_pcb_tree, flow_divert_pcb, rb_link, flow_divert_pcb_cmp);
192 
193 static const char *
flow_divert_packet_type2str(uint8_t packet_type)194 flow_divert_packet_type2str(uint8_t packet_type)
195 {
196 	switch (packet_type) {
197 	case FLOW_DIVERT_PKT_CONNECT:
198 		return "connect";
199 	case FLOW_DIVERT_PKT_CONNECT_RESULT:
200 		return "connect result";
201 	case FLOW_DIVERT_PKT_DATA:
202 		return "data";
203 	case FLOW_DIVERT_PKT_CLOSE:
204 		return "close";
205 	case FLOW_DIVERT_PKT_READ_NOTIFY:
206 		return "read notification";
207 	case FLOW_DIVERT_PKT_PROPERTIES_UPDATE:
208 		return "properties update";
209 	case FLOW_DIVERT_PKT_APP_MAP_CREATE:
210 		return "app map create";
211 	default:
212 		return "unknown";
213 	}
214 }
215 
216 static struct flow_divert_pcb *
flow_divert_pcb_lookup(uint32_t hash,struct flow_divert_group * group)217 flow_divert_pcb_lookup(uint32_t hash, struct flow_divert_group *group)
218 {
219 	struct flow_divert_pcb  key_item;
220 	struct flow_divert_pcb  *fd_cb          = NULL;
221 
222 	key_item.hash = hash;
223 
224 	lck_rw_lock_shared(&group->lck);
225 	fd_cb = RB_FIND(fd_pcb_tree, &group->pcb_tree, &key_item);
226 	FDRETAIN(fd_cb);
227 	lck_rw_done(&group->lck);
228 
229 	return fd_cb;
230 }
231 
232 static errno_t
flow_divert_pcb_insert(struct socket * so,struct flow_divert_pcb * fd_cb,uint32_t ctl_unit)233 flow_divert_pcb_insert(struct socket *so, struct flow_divert_pcb *fd_cb, uint32_t ctl_unit)
234 {
235 	errno_t                                                 error                                           = 0;
236 	struct                                          flow_divert_pcb *exist          = NULL;
237 	struct flow_divert_group        *group;
238 	static uint32_t                         g_nextkey                                       = 1;
239 	static uint32_t                         g_hash_seed                                     = 0;
240 	int                                                     try_count                                       = 0;
241 
242 	if (ctl_unit == 0 || ctl_unit >= GROUP_COUNT_MAX) {
243 		return EINVAL;
244 	}
245 
246 	socket_unlock(so, 0);
247 	lck_rw_lock_shared(&g_flow_divert_group_lck);
248 
249 	if (g_flow_divert_groups == NULL || g_active_group_count == 0) {
250 		FDLOG0(LOG_ERR, &nil_pcb, "No active groups, flow divert cannot be used for this socket");
251 		error = ENETUNREACH;
252 		goto done;
253 	}
254 
255 	group = g_flow_divert_groups[ctl_unit];
256 	if (group == NULL) {
257 		FDLOG(LOG_ERR, &nil_pcb, "Group for control unit %u is NULL, flow divert cannot be used for this socket", ctl_unit);
258 		error = ENETUNREACH;
259 		goto done;
260 	}
261 
262 	socket_lock(so, 0);
263 	if (!(so->so_flags & SOF_FLOW_DIVERT)) {
264 		error = EINVAL;
265 		goto unlock;
266 	}
267 
268 	do {
269 		uint32_t        key[2];
270 		uint32_t        idx;
271 
272 		key[0] = g_nextkey++;
273 		key[1] = RandomULong();
274 
275 		if (g_hash_seed == 0) {
276 			g_hash_seed = RandomULong();
277 		}
278 
279 		fd_cb->hash = net_flowhash(key, sizeof(key), g_hash_seed);
280 
281 		for (idx = 1; idx < GROUP_COUNT_MAX; idx++) {
282 			struct flow_divert_group *curr_group = g_flow_divert_groups[idx];
283 			if (curr_group != NULL && curr_group != group) {
284 				lck_rw_lock_shared(&curr_group->lck);
285 				exist = RB_FIND(fd_pcb_tree, &curr_group->pcb_tree, fd_cb);
286 				lck_rw_done(&curr_group->lck);
287 				if (exist != NULL) {
288 					break;
289 				}
290 			}
291 		}
292 
293 		if (exist == NULL) {
294 			lck_rw_lock_exclusive(&group->lck);
295 			exist = RB_INSERT(fd_pcb_tree, &group->pcb_tree, fd_cb);
296 			lck_rw_done(&group->lck);
297 		}
298 	} while (exist != NULL && try_count++ < 3);
299 
300 	if (exist == NULL) {
301 		fd_cb->group = group;
302 		FDRETAIN(fd_cb);                /* The group now has a reference */
303 	} else {
304 		fd_cb->hash = 0;
305 		error = EEXIST;
306 	}
307 
308 unlock:
309 	socket_unlock(so, 0);
310 
311 done:
312 	lck_rw_done(&g_flow_divert_group_lck);
313 	socket_lock(so, 0);
314 
315 	if (!(so->so_flags & SOF_FLOW_DIVERT)) {
316 		error = EINVAL;
317 	}
318 
319 	return error;
320 }
321 
322 static struct flow_divert_pcb *
flow_divert_pcb_create(socket_t so)323 flow_divert_pcb_create(socket_t so)
324 {
325 	struct flow_divert_pcb  *new_pcb = NULL;
326 
327 	new_pcb = zalloc_flags(flow_divert_pcb_zone, Z_WAITOK | Z_ZERO);
328 	lck_mtx_init(&new_pcb->mtx, &flow_divert_mtx_grp, &flow_divert_mtx_attr);
329 	new_pcb->so = so;
330 	new_pcb->log_level = nil_pcb.log_level;
331 
332 	FDRETAIN(new_pcb);      /* Represents the socket's reference */
333 
334 	return new_pcb;
335 }
336 
337 static void
flow_divert_pcb_destroy(struct flow_divert_pcb * fd_cb)338 flow_divert_pcb_destroy(struct flow_divert_pcb *fd_cb)
339 {
340 	FDLOG(LOG_INFO, fd_cb, "Destroying, app tx %u, tunnel tx %u, tunnel rx %u",
341 	    fd_cb->bytes_written_by_app, fd_cb->bytes_sent, fd_cb->bytes_received);
342 
343 	if (fd_cb->connect_token != NULL) {
344 		mbuf_freem(fd_cb->connect_token);
345 	}
346 	if (fd_cb->connect_packet != NULL) {
347 		mbuf_freem(fd_cb->connect_packet);
348 	}
349 	if (fd_cb->app_data != NULL) {
350 		kfree_data(fd_cb->app_data, fd_cb->app_data_length);
351 	}
352 	free_sockaddr(fd_cb->original_remote_endpoint);
353 	zfree(flow_divert_pcb_zone, fd_cb);
354 }
355 
356 static void
flow_divert_pcb_remove(struct flow_divert_pcb * fd_cb)357 flow_divert_pcb_remove(struct flow_divert_pcb *fd_cb)
358 {
359 	if (fd_cb->group != NULL) {
360 		struct flow_divert_group *group = fd_cb->group;
361 		lck_rw_lock_exclusive(&group->lck);
362 		FDLOG(LOG_INFO, fd_cb, "Removing from group %d, ref count = %d", group->ctl_unit, fd_cb->ref_count);
363 		RB_REMOVE(fd_pcb_tree, &group->pcb_tree, fd_cb);
364 		fd_cb->group = NULL;
365 		FDRELEASE(fd_cb);                               /* Release the group's reference */
366 		lck_rw_done(&group->lck);
367 	}
368 }
369 
370 static int
flow_divert_packet_init(struct flow_divert_pcb * fd_cb,uint8_t packet_type,mbuf_t * packet)371 flow_divert_packet_init(struct flow_divert_pcb *fd_cb, uint8_t packet_type, mbuf_t *packet)
372 {
373 	struct flow_divert_packet_header        hdr;
374 	int                                     error           = 0;
375 
376 	error = mbuf_gethdr(MBUF_DONTWAIT, MBUF_TYPE_HEADER, packet);
377 	if (error) {
378 		FDLOG(LOG_ERR, fd_cb, "failed to allocate the header mbuf: %d", error);
379 		return error;
380 	}
381 
382 	hdr.packet_type = packet_type;
383 	hdr.conn_id = htonl(fd_cb->hash);
384 
385 	/* Lay down the header */
386 	error = mbuf_copyback(*packet, 0, sizeof(hdr), &hdr, MBUF_DONTWAIT);
387 	if (error) {
388 		FDLOG(LOG_ERR, fd_cb, "mbuf_copyback(hdr) failed: %d", error);
389 		mbuf_freem(*packet);
390 		*packet = NULL;
391 		return error;
392 	}
393 
394 	return 0;
395 }
396 
397 static int
flow_divert_packet_append_tlv(mbuf_t packet,uint8_t type,uint32_t length,const void * value)398 flow_divert_packet_append_tlv(mbuf_t packet, uint8_t type, uint32_t length, const void *value)
399 {
400 	uint32_t        net_length      = htonl(length);
401 	int                     error           = 0;
402 
403 	error = mbuf_copyback(packet, mbuf_pkthdr_len(packet), sizeof(type), &type, MBUF_DONTWAIT);
404 	if (error) {
405 		FDLOG(LOG_ERR, &nil_pcb, "failed to append the type (%d)", type);
406 		return error;
407 	}
408 
409 	error = mbuf_copyback(packet, mbuf_pkthdr_len(packet), sizeof(net_length), &net_length, MBUF_DONTWAIT);
410 	if (error) {
411 		FDLOG(LOG_ERR, &nil_pcb, "failed to append the length (%u)", length);
412 		return error;
413 	}
414 
415 	error = mbuf_copyback(packet, mbuf_pkthdr_len(packet), length, value, MBUF_DONTWAIT);
416 	if (error) {
417 		FDLOG0(LOG_ERR, &nil_pcb, "failed to append the value");
418 		return error;
419 	}
420 
421 	return error;
422 }
423 
424 static int
flow_divert_packet_find_tlv(mbuf_t packet,int offset,uint8_t type,int * err,int next)425 flow_divert_packet_find_tlv(mbuf_t packet, int offset, uint8_t type, int *err, int next)
426 {
427 	size_t          cursor                  = offset;
428 	int                     error                   = 0;
429 	uint32_t        curr_length;
430 	uint8_t         curr_type;
431 
432 	*err = 0;
433 
434 	do {
435 		if (!next) {
436 			error = mbuf_copydata(packet, cursor, sizeof(curr_type), &curr_type);
437 			if (error) {
438 				*err = ENOENT;
439 				return -1;
440 			}
441 		} else {
442 			next = 0;
443 			curr_type = FLOW_DIVERT_TLV_NIL;
444 		}
445 
446 		if (curr_type != type) {
447 			cursor += sizeof(curr_type);
448 			error = mbuf_copydata(packet, cursor, sizeof(curr_length), &curr_length);
449 			if (error) {
450 				*err = error;
451 				return -1;
452 			}
453 
454 			cursor += (sizeof(curr_length) + ntohl(curr_length));
455 		}
456 	} while (curr_type != type);
457 
458 	return (int)cursor;
459 }
460 
461 static int
flow_divert_packet_get_tlv(mbuf_t packet,int offset,uint8_t type,size_t buff_len,void * buff,uint32_t * val_size)462 flow_divert_packet_get_tlv(mbuf_t packet, int offset, uint8_t type, size_t buff_len, void *buff, uint32_t *val_size)
463 {
464 	int                     error           = 0;
465 	uint32_t        length;
466 	int                     tlv_offset;
467 
468 	tlv_offset = flow_divert_packet_find_tlv(packet, offset, type, &error, 0);
469 	if (tlv_offset < 0) {
470 		return error;
471 	}
472 
473 	error = mbuf_copydata(packet, tlv_offset + sizeof(type), sizeof(length), &length);
474 	if (error) {
475 		return error;
476 	}
477 
478 	length = ntohl(length);
479 
480 	uint32_t data_offset = tlv_offset + sizeof(type) + sizeof(length);
481 
482 	if (length > (mbuf_pkthdr_len(packet) - data_offset)) {
483 		FDLOG(LOG_ERR, &nil_pcb, "Length of %u TLV (%u) is larger than remaining packet data (%lu)", type, length, (mbuf_pkthdr_len(packet) - data_offset));
484 		return EINVAL;
485 	}
486 
487 	if (val_size != NULL) {
488 		*val_size = length;
489 	}
490 
491 	if (buff != NULL && buff_len > 0) {
492 		memset(buff, 0, buff_len);
493 		size_t to_copy = (length < buff_len) ? length : buff_len;
494 		error = mbuf_copydata(packet, data_offset, to_copy, buff);
495 		if (error) {
496 			return error;
497 		}
498 	}
499 
500 	return 0;
501 }
502 
503 static int
flow_divert_packet_compute_hmac(mbuf_t packet,struct flow_divert_group * group,uint8_t * hmac)504 flow_divert_packet_compute_hmac(mbuf_t packet, struct flow_divert_group *group, uint8_t *hmac)
505 {
506 	mbuf_t  curr_mbuf       = packet;
507 
508 	if (g_crypto_funcs == NULL || group->token_key == NULL) {
509 		return ENOPROTOOPT;
510 	}
511 
512 	cchmac_di_decl(g_crypto_funcs->ccsha1_di, hmac_ctx);
513 	g_crypto_funcs->cchmac_init_fn(g_crypto_funcs->ccsha1_di, hmac_ctx, group->token_key_size, group->token_key);
514 
515 	while (curr_mbuf != NULL) {
516 		g_crypto_funcs->cchmac_update_fn(g_crypto_funcs->ccsha1_di, hmac_ctx, mbuf_len(curr_mbuf), mbuf_data(curr_mbuf));
517 		curr_mbuf = mbuf_next(curr_mbuf);
518 	}
519 
520 	g_crypto_funcs->cchmac_final_fn(g_crypto_funcs->ccsha1_di, hmac_ctx, hmac);
521 
522 	return 0;
523 }
524 
525 static int
flow_divert_packet_verify_hmac(mbuf_t packet,uint32_t ctl_unit)526 flow_divert_packet_verify_hmac(mbuf_t packet, uint32_t ctl_unit)
527 {
528 	int                                                     error = 0;
529 	struct flow_divert_group        *group = NULL;
530 	int                                                     hmac_offset;
531 	uint8_t                                         packet_hmac[SHA_DIGEST_LENGTH];
532 	uint8_t                                         computed_hmac[SHA_DIGEST_LENGTH];
533 	mbuf_t                                          tail;
534 
535 	lck_rw_lock_shared(&g_flow_divert_group_lck);
536 
537 	if (g_flow_divert_groups != NULL && g_active_group_count > 0) {
538 		group = g_flow_divert_groups[ctl_unit];
539 	}
540 
541 	if (group == NULL) {
542 		lck_rw_done(&g_flow_divert_group_lck);
543 		return ENOPROTOOPT;
544 	}
545 
546 	lck_rw_lock_shared(&group->lck);
547 
548 	if (group->token_key == NULL) {
549 		error = ENOPROTOOPT;
550 		goto done;
551 	}
552 
553 	hmac_offset = flow_divert_packet_find_tlv(packet, 0, FLOW_DIVERT_TLV_HMAC, &error, 0);
554 	if (hmac_offset < 0) {
555 		goto done;
556 	}
557 
558 	error = flow_divert_packet_get_tlv(packet, hmac_offset, FLOW_DIVERT_TLV_HMAC, sizeof(packet_hmac), packet_hmac, NULL);
559 	if (error) {
560 		goto done;
561 	}
562 
563 	/* Chop off the HMAC TLV */
564 	error = mbuf_split(packet, hmac_offset, MBUF_WAITOK, &tail);
565 	if (error) {
566 		goto done;
567 	}
568 
569 	mbuf_free(tail);
570 
571 	error = flow_divert_packet_compute_hmac(packet, group, computed_hmac);
572 	if (error) {
573 		goto done;
574 	}
575 
576 	if (cc_cmp_safe(sizeof(packet_hmac), packet_hmac, computed_hmac)) {
577 		FDLOG0(LOG_WARNING, &nil_pcb, "HMAC in token does not match computed HMAC");
578 		error = EINVAL;
579 		goto done;
580 	}
581 
582 done:
583 	lck_rw_done(&group->lck);
584 	lck_rw_done(&g_flow_divert_group_lck);
585 	return error;
586 }
587 
588 static void
flow_divert_add_data_statistics(struct flow_divert_pcb * fd_cb,size_t data_len,Boolean send)589 flow_divert_add_data_statistics(struct flow_divert_pcb *fd_cb, size_t data_len, Boolean send)
590 {
591 	struct inpcb *inp = NULL;
592 	struct ifnet *ifp = NULL;
593 	Boolean cell = FALSE;
594 	Boolean wifi = FALSE;
595 	Boolean wired = FALSE;
596 
597 	inp = sotoinpcb(fd_cb->so);
598 	if (inp == NULL) {
599 		return;
600 	}
601 
602 	if (inp->inp_vflag & INP_IPV4) {
603 		ifp = inp->inp_last_outifp;
604 	} else if (inp->inp_vflag & INP_IPV6) {
605 		ifp = inp->in6p_last_outifp;
606 	}
607 	if (ifp != NULL) {
608 		cell = IFNET_IS_CELLULAR(ifp);
609 		wifi = (!cell && IFNET_IS_WIFI(ifp));
610 		wired = (!wifi && IFNET_IS_WIRED(ifp));
611 	}
612 
613 	if (send) {
614 		INP_ADD_STAT(inp, cell, wifi, wired, txpackets, 1);
615 		INP_ADD_STAT(inp, cell, wifi, wired, txbytes, data_len);
616 	} else {
617 		INP_ADD_STAT(inp, cell, wifi, wired, rxpackets, 1);
618 		INP_ADD_STAT(inp, cell, wifi, wired, rxbytes, data_len);
619 	}
620 	inp_set_activity_bitmap(inp);
621 }
622 
623 static errno_t
flow_divert_check_no_cellular(struct flow_divert_pcb * fd_cb)624 flow_divert_check_no_cellular(struct flow_divert_pcb *fd_cb)
625 {
626 	struct inpcb *inp = sotoinpcb(fd_cb->so);
627 	if (INP_NO_CELLULAR(inp)) {
628 		struct ifnet *ifp = NULL;
629 		if (inp->inp_vflag & INP_IPV4) {
630 			ifp = inp->inp_last_outifp;
631 		} else if (inp->inp_vflag & INP_IPV6) {
632 			ifp = inp->in6p_last_outifp;
633 		}
634 		if (ifp != NULL && IFNET_IS_CELLULAR(ifp)) {
635 			FDLOG0(LOG_ERR, fd_cb, "Cellular is denied");
636 			return EHOSTUNREACH;
637 		}
638 	}
639 	return 0;
640 }
641 
642 static errno_t
flow_divert_check_no_expensive(struct flow_divert_pcb * fd_cb)643 flow_divert_check_no_expensive(struct flow_divert_pcb *fd_cb)
644 {
645 	struct inpcb *inp = sotoinpcb(fd_cb->so);
646 	if (INP_NO_EXPENSIVE(inp)) {
647 		struct ifnet *ifp = NULL;
648 		if (inp->inp_vflag & INP_IPV4) {
649 			ifp = inp->inp_last_outifp;
650 		} else if (inp->inp_vflag & INP_IPV6) {
651 			ifp = inp->in6p_last_outifp;
652 		}
653 		if (ifp != NULL && IFNET_IS_EXPENSIVE(ifp)) {
654 			FDLOG0(LOG_ERR, fd_cb, "Expensive is denied");
655 			return EHOSTUNREACH;
656 		}
657 	}
658 	return 0;
659 }
660 
661 static errno_t
flow_divert_check_no_constrained(struct flow_divert_pcb * fd_cb)662 flow_divert_check_no_constrained(struct flow_divert_pcb *fd_cb)
663 {
664 	struct inpcb *inp = sotoinpcb(fd_cb->so);
665 	if (INP_NO_CONSTRAINED(inp)) {
666 		struct ifnet *ifp = NULL;
667 		if (inp->inp_vflag & INP_IPV4) {
668 			ifp = inp->inp_last_outifp;
669 		} else if (inp->inp_vflag & INP_IPV6) {
670 			ifp = inp->in6p_last_outifp;
671 		}
672 		if (ifp != NULL && IFNET_IS_CONSTRAINED(ifp)) {
673 			FDLOG0(LOG_ERR, fd_cb, "Constrained is denied");
674 			return EHOSTUNREACH;
675 		}
676 	}
677 	return 0;
678 }
679 
680 static void
flow_divert_update_closed_state(struct flow_divert_pcb * fd_cb,int how,Boolean tunnel)681 flow_divert_update_closed_state(struct flow_divert_pcb *fd_cb, int how, Boolean tunnel)
682 {
683 	if (how != SHUT_RD) {
684 		fd_cb->flags |= FLOW_DIVERT_WRITE_CLOSED;
685 		if (tunnel || !(fd_cb->flags & FLOW_DIVERT_CONNECT_STARTED)) {
686 			fd_cb->flags |= FLOW_DIVERT_TUNNEL_WR_CLOSED;
687 			/* If the tunnel is not accepting writes any more, then flush the send buffer */
688 			sbflush(&fd_cb->so->so_snd);
689 		}
690 	}
691 	if (how != SHUT_WR) {
692 		fd_cb->flags |= FLOW_DIVERT_READ_CLOSED;
693 		if (tunnel || !(fd_cb->flags & FLOW_DIVERT_CONNECT_STARTED)) {
694 			fd_cb->flags |= FLOW_DIVERT_TUNNEL_RD_CLOSED;
695 		}
696 	}
697 }
698 
699 static uint16_t
trie_node_alloc(struct flow_divert_trie * trie)700 trie_node_alloc(struct flow_divert_trie *trie)
701 {
702 	if (trie->nodes_free_next < trie->nodes_count) {
703 		uint16_t node_idx = trie->nodes_free_next++;
704 		TRIE_NODE(trie, node_idx).child_map = NULL_TRIE_IDX;
705 		return node_idx;
706 	} else {
707 		return NULL_TRIE_IDX;
708 	}
709 }
710 
711 static uint16_t
trie_child_map_alloc(struct flow_divert_trie * trie)712 trie_child_map_alloc(struct flow_divert_trie *trie)
713 {
714 	if (trie->child_maps_free_next < trie->child_maps_count) {
715 		return trie->child_maps_free_next++;
716 	} else {
717 		return NULL_TRIE_IDX;
718 	}
719 }
720 
721 static uint16_t
trie_bytes_move(struct flow_divert_trie * trie,uint16_t bytes_idx,size_t bytes_size)722 trie_bytes_move(struct flow_divert_trie *trie, uint16_t bytes_idx, size_t bytes_size)
723 {
724 	uint16_t start = trie->bytes_free_next;
725 	if (start + bytes_size <= trie->bytes_count) {
726 		if (start != bytes_idx) {
727 			memmove(&TRIE_BYTE(trie, start), &TRIE_BYTE(trie, bytes_idx), bytes_size);
728 		}
729 		trie->bytes_free_next += bytes_size;
730 		return start;
731 	} else {
732 		return NULL_TRIE_IDX;
733 	}
734 }
735 
736 static uint16_t
flow_divert_trie_insert(struct flow_divert_trie * trie,uint16_t string_start,size_t string_len)737 flow_divert_trie_insert(struct flow_divert_trie *trie, uint16_t string_start, size_t string_len)
738 {
739 	uint16_t current = trie->root;
740 	uint16_t child = trie->root;
741 	uint16_t string_end = string_start + (uint16_t)string_len;
742 	uint16_t string_idx = string_start;
743 	uint16_t string_remainder = (uint16_t)string_len;
744 
745 	while (child != NULL_TRIE_IDX) {
746 		uint16_t parent = current;
747 		uint16_t node_idx;
748 		uint16_t current_end;
749 
750 		current = child;
751 		child = NULL_TRIE_IDX;
752 
753 		current_end = TRIE_NODE(trie, current).start + TRIE_NODE(trie, current).length;
754 
755 		for (node_idx = TRIE_NODE(trie, current).start;
756 		    node_idx < current_end &&
757 		    string_idx < string_end &&
758 		    TRIE_BYTE(trie, node_idx) == TRIE_BYTE(trie, string_idx);
759 		    node_idx++, string_idx++) {
760 			;
761 		}
762 
763 		string_remainder = string_end - string_idx;
764 
765 		if (node_idx < (TRIE_NODE(trie, current).start + TRIE_NODE(trie, current).length)) {
766 			/*
767 			 * We did not reach the end of the current node's string.
768 			 * We need to split the current node into two:
769 			 *   1. A new node that contains the prefix of the node that matches
770 			 *      the prefix of the string being inserted.
771 			 *   2. The current node modified to point to the remainder
772 			 *      of the current node's string.
773 			 */
774 			uint16_t prefix = trie_node_alloc(trie);
775 			if (prefix == NULL_TRIE_IDX) {
776 				FDLOG0(LOG_ERR, &nil_pcb, "Ran out of trie nodes while splitting an existing node");
777 				return NULL_TRIE_IDX;
778 			}
779 
780 			/*
781 			 * Prefix points to the portion of the current nodes's string that has matched
782 			 * the input string thus far.
783 			 */
784 			TRIE_NODE(trie, prefix).start = TRIE_NODE(trie, current).start;
785 			TRIE_NODE(trie, prefix).length = (node_idx - TRIE_NODE(trie, current).start);
786 
787 			/*
788 			 * Prefix has the current node as the child corresponding to the first byte
789 			 * after the split.
790 			 */
791 			TRIE_NODE(trie, prefix).child_map = trie_child_map_alloc(trie);
792 			if (TRIE_NODE(trie, prefix).child_map == NULL_TRIE_IDX) {
793 				FDLOG0(LOG_ERR, &nil_pcb, "Ran out of child maps while splitting an existing node");
794 				return NULL_TRIE_IDX;
795 			}
796 			TRIE_CHILD(trie, prefix, TRIE_BYTE(trie, node_idx)) = current;
797 
798 			/* Parent has the prefix as the child correspoding to the first byte in the prefix */
799 			TRIE_CHILD(trie, parent, TRIE_BYTE(trie, TRIE_NODE(trie, prefix).start)) = prefix;
800 
801 			/* Current node is adjusted to point to the remainder */
802 			TRIE_NODE(trie, current).start = node_idx;
803 			TRIE_NODE(trie, current).length -= TRIE_NODE(trie, prefix).length;
804 
805 			/* We want to insert the new leaf (if any) as a child of the prefix */
806 			current = prefix;
807 		}
808 
809 		if (string_remainder > 0) {
810 			/*
811 			 * We still have bytes in the string that have not been matched yet.
812 			 * If the current node has children, iterate to the child corresponding
813 			 * to the next byte in the string.
814 			 */
815 			if (TRIE_NODE(trie, current).child_map != NULL_TRIE_IDX) {
816 				child = TRIE_CHILD(trie, current, TRIE_BYTE(trie, string_idx));
817 			}
818 		}
819 	} /* while (child != NULL_TRIE_IDX) */
820 
821 	if (string_remainder > 0) {
822 		/* Add a new leaf containing the remainder of the string */
823 		uint16_t leaf = trie_node_alloc(trie);
824 		if (leaf == NULL_TRIE_IDX) {
825 			FDLOG0(LOG_ERR, &nil_pcb, "Ran out of trie nodes while inserting a new leaf");
826 			return NULL_TRIE_IDX;
827 		}
828 
829 		TRIE_NODE(trie, leaf).start = trie_bytes_move(trie, string_idx, string_remainder);
830 		if (TRIE_NODE(trie, leaf).start == NULL_TRIE_IDX) {
831 			FDLOG0(LOG_ERR, &nil_pcb, "Ran out of bytes while inserting a new leaf");
832 			return NULL_TRIE_IDX;
833 		}
834 		TRIE_NODE(trie, leaf).length = string_remainder;
835 
836 		/* Set the new leaf as the child of the current node */
837 		if (TRIE_NODE(trie, current).child_map == NULL_TRIE_IDX) {
838 			TRIE_NODE(trie, current).child_map = trie_child_map_alloc(trie);
839 			if (TRIE_NODE(trie, current).child_map == NULL_TRIE_IDX) {
840 				FDLOG0(LOG_ERR, &nil_pcb, "Ran out of child maps while inserting a new leaf");
841 				return NULL_TRIE_IDX;
842 			}
843 		}
844 		TRIE_CHILD(trie, current, TRIE_BYTE(trie, TRIE_NODE(trie, leaf).start)) = leaf;
845 		current = leaf;
846 	} /* else duplicate or this string is a prefix of one of the existing strings */
847 
848 	return current;
849 }
850 
851 #define APPLE_WEBCLIP_ID_PREFIX "com.apple.webapp"
852 static uint16_t
flow_divert_trie_search(struct flow_divert_trie * trie,const uint8_t * string_bytes)853 flow_divert_trie_search(struct flow_divert_trie *trie, const uint8_t *string_bytes)
854 {
855 	uint16_t current = trie->root;
856 	uint16_t string_idx = 0;
857 
858 	while (current != NULL_TRIE_IDX) {
859 		uint16_t next = NULL_TRIE_IDX;
860 		uint16_t node_end = TRIE_NODE(trie, current).start + TRIE_NODE(trie, current).length;
861 		uint16_t node_idx;
862 
863 		for (node_idx = TRIE_NODE(trie, current).start;
864 		    node_idx < node_end && string_bytes[string_idx] != '\0' && string_bytes[string_idx] == TRIE_BYTE(trie, node_idx);
865 		    node_idx++, string_idx++) {
866 			;
867 		}
868 
869 		if (node_idx == node_end) {
870 			if (string_bytes[string_idx] == '\0') {
871 				return current; /* Got an exact match */
872 			} else if (string_idx == strlen(APPLE_WEBCLIP_ID_PREFIX) &&
873 			    0 == strncmp((const char *)string_bytes, APPLE_WEBCLIP_ID_PREFIX, string_idx)) {
874 				return current; /* Got an apple webclip id prefix match */
875 			} else if (TRIE_NODE(trie, current).child_map != NULL_TRIE_IDX) {
876 				next = TRIE_CHILD(trie, current, string_bytes[string_idx]);
877 			}
878 		}
879 		current = next;
880 	}
881 
882 	return NULL_TRIE_IDX;
883 }
884 
885 struct uuid_search_info {
886 	uuid_t target_uuid;
887 	char *found_signing_id;
888 	boolean_t found_multiple_signing_ids;
889 	proc_t found_proc;
890 };
891 
892 static int
flow_divert_find_proc_by_uuid_callout(proc_t p,void * arg)893 flow_divert_find_proc_by_uuid_callout(proc_t p, void *arg)
894 {
895 	struct uuid_search_info *info = (struct uuid_search_info *)arg;
896 	int result = PROC_RETURNED_DONE; /* By default, we didn't find the process */
897 
898 	if (info->found_signing_id != NULL) {
899 		if (!info->found_multiple_signing_ids) {
900 			/* All processes that were found had the same signing identifier, so just claim this first one and be done. */
901 			info->found_proc = p;
902 			result = PROC_CLAIMED_DONE;
903 		} else {
904 			uuid_string_t uuid_str;
905 			uuid_unparse(info->target_uuid, uuid_str);
906 			FDLOG(LOG_WARNING, &nil_pcb, "Found multiple processes with UUID %s with different signing identifiers", uuid_str);
907 		}
908 		kfree_data(info->found_signing_id, strlen(info->found_signing_id) + 1);
909 		info->found_signing_id = NULL;
910 	}
911 
912 	if (result == PROC_RETURNED_DONE) {
913 		uuid_string_t uuid_str;
914 		uuid_unparse(info->target_uuid, uuid_str);
915 		FDLOG(LOG_WARNING, &nil_pcb, "Failed to find a process with UUID %s", uuid_str);
916 	}
917 
918 	return result;
919 }
920 
921 static int
flow_divert_find_proc_by_uuid_filter(proc_t p,void * arg)922 flow_divert_find_proc_by_uuid_filter(proc_t p, void *arg)
923 {
924 	struct uuid_search_info *info = (struct uuid_search_info *)arg;
925 	int include = 0;
926 
927 	if (info->found_multiple_signing_ids) {
928 		return include;
929 	}
930 
931 	include = (uuid_compare(proc_executableuuid_addr(p), info->target_uuid) == 0);
932 	if (include) {
933 		const char *signing_id = cs_identity_get(p);
934 		if (signing_id != NULL) {
935 			FDLOG(LOG_INFO, &nil_pcb, "Found process %d with signing identifier %s", proc_getpid(p), signing_id);
936 			size_t signing_id_size = strlen(signing_id) + 1;
937 			if (info->found_signing_id == NULL) {
938 				info->found_signing_id = kalloc_data(signing_id_size, Z_WAITOK);
939 				memcpy(info->found_signing_id, signing_id, signing_id_size);
940 			} else if (memcmp(signing_id, info->found_signing_id, signing_id_size)) {
941 				info->found_multiple_signing_ids = TRUE;
942 			}
943 		} else {
944 			info->found_multiple_signing_ids = TRUE;
945 		}
946 		include = !info->found_multiple_signing_ids;
947 	}
948 
949 	return include;
950 }
951 
952 static proc_t
flow_divert_find_proc_by_uuid(uuid_t uuid)953 flow_divert_find_proc_by_uuid(uuid_t uuid)
954 {
955 	struct uuid_search_info info;
956 
957 	if (LOG_INFO <= nil_pcb.log_level) {
958 		uuid_string_t uuid_str;
959 		uuid_unparse(uuid, uuid_str);
960 		FDLOG(LOG_INFO, &nil_pcb, "Looking for process with UUID %s", uuid_str);
961 	}
962 
963 	memset(&info, 0, sizeof(info));
964 	info.found_proc = PROC_NULL;
965 	uuid_copy(info.target_uuid, uuid);
966 
967 	proc_iterate(PROC_ALLPROCLIST, flow_divert_find_proc_by_uuid_callout, &info, flow_divert_find_proc_by_uuid_filter, &info);
968 
969 	return info.found_proc;
970 }
971 
972 static int
flow_divert_add_proc_info(struct flow_divert_pcb * fd_cb,proc_t proc,const char * signing_id,mbuf_t connect_packet,bool is_effective)973 flow_divert_add_proc_info(struct flow_divert_pcb *fd_cb, proc_t proc, const char *signing_id, mbuf_t connect_packet, bool is_effective)
974 {
975 	int error = 0;
976 	uint8_t *cdhash = NULL;
977 	audit_token_t audit_token = {};
978 	const char *proc_cs_id = signing_id;
979 
980 	proc_lock(proc);
981 
982 	if (proc_cs_id == NULL) {
983 		if (proc_getcsflags(proc) & (CS_VALID | CS_DEBUGGED)) {
984 			proc_cs_id = cs_identity_get(proc);
985 		} else {
986 			FDLOG0(LOG_ERR, fd_cb, "Signature of proc is invalid");
987 		}
988 	}
989 
990 	if (is_effective) {
991 		lck_rw_lock_shared(&fd_cb->group->lck);
992 		if (!(fd_cb->group->flags & FLOW_DIVERT_GROUP_FLAG_NO_APP_MAP)) {
993 			if (proc_cs_id != NULL) {
994 				uint16_t result = flow_divert_trie_search(&fd_cb->group->signing_id_trie, (const uint8_t *)proc_cs_id);
995 				if (result == NULL_TRIE_IDX) {
996 					FDLOG(LOG_WARNING, fd_cb, "%s did not match", proc_cs_id);
997 					error = EPERM;
998 				} else {
999 					FDLOG(LOG_INFO, fd_cb, "%s matched", proc_cs_id);
1000 				}
1001 			} else {
1002 				error = EPERM;
1003 			}
1004 		}
1005 		lck_rw_done(&fd_cb->group->lck);
1006 	}
1007 
1008 	if (error != 0) {
1009 		goto done;
1010 	}
1011 
1012 	/*
1013 	 * If signing_id is not NULL then it came from the flow divert token and will be added
1014 	 * as part of the token, so there is no need to add it here.
1015 	 */
1016 	if (signing_id == NULL && proc_cs_id != NULL) {
1017 		error = flow_divert_packet_append_tlv(connect_packet,
1018 		    (is_effective ? FLOW_DIVERT_TLV_SIGNING_ID : FLOW_DIVERT_TLV_APP_REAL_SIGNING_ID),
1019 		    (uint32_t)strlen(proc_cs_id),
1020 		    proc_cs_id);
1021 		if (error != 0) {
1022 			FDLOG(LOG_ERR, fd_cb, "failed to append the signing ID: %d", error);
1023 			goto done;
1024 		}
1025 	}
1026 
1027 	cdhash = cs_get_cdhash(proc);
1028 	if (cdhash != NULL) {
1029 		error = flow_divert_packet_append_tlv(connect_packet,
1030 		    (is_effective ? FLOW_DIVERT_TLV_CDHASH : FLOW_DIVERT_TLV_APP_REAL_CDHASH),
1031 		    SHA1_RESULTLEN,
1032 		    cdhash);
1033 		if (error) {
1034 			FDLOG(LOG_ERR, fd_cb, "failed to append the cdhash: %d", error);
1035 			goto done;
1036 		}
1037 	} else {
1038 		FDLOG0(LOG_ERR, fd_cb, "failed to get the cdhash");
1039 	}
1040 
1041 	task_t task = proc_task(proc);
1042 	if (task != TASK_NULL) {
1043 		mach_msg_type_number_t count = TASK_AUDIT_TOKEN_COUNT;
1044 		kern_return_t rc = task_info(task, TASK_AUDIT_TOKEN, (task_info_t)&audit_token, &count);
1045 		if (rc == KERN_SUCCESS) {
1046 			int append_error = flow_divert_packet_append_tlv(connect_packet,
1047 			    (is_effective ? FLOW_DIVERT_TLV_APP_AUDIT_TOKEN : FLOW_DIVERT_TLV_APP_REAL_AUDIT_TOKEN),
1048 			    sizeof(audit_token_t),
1049 			    &audit_token);
1050 			if (append_error) {
1051 				FDLOG(LOG_ERR, fd_cb, "failed to append app audit token: %d", append_error);
1052 			}
1053 		}
1054 	}
1055 
1056 done:
1057 	proc_unlock(proc);
1058 
1059 	return error;
1060 }
1061 
1062 static int
flow_divert_add_all_proc_info(struct flow_divert_pcb * fd_cb,struct socket * so,proc_t proc,const char * signing_id,mbuf_t connect_packet)1063 flow_divert_add_all_proc_info(struct flow_divert_pcb *fd_cb, struct socket *so, proc_t proc, const char *signing_id, mbuf_t connect_packet)
1064 {
1065 	int error = 0;
1066 	proc_t effective_proc = PROC_NULL;
1067 	proc_t responsible_proc = PROC_NULL;
1068 	proc_t real_proc = proc_find(so->last_pid);
1069 	bool release_real_proc = true;
1070 
1071 	proc_t src_proc = PROC_NULL;
1072 	proc_t real_src_proc = PROC_NULL;
1073 
1074 	if (real_proc == PROC_NULL) {
1075 		FDLOG(LOG_ERR, fd_cb, "failed to find the real proc record for %d", so->last_pid);
1076 		release_real_proc = false;
1077 		real_proc = proc;
1078 		if (real_proc == PROC_NULL) {
1079 			real_proc = current_proc();
1080 		}
1081 	}
1082 
1083 	if (so->so_flags & SOF_DELEGATED) {
1084 		if (proc_getpid(real_proc) != so->e_pid) {
1085 			effective_proc = proc_find(so->e_pid);
1086 		} else if (uuid_compare(proc_executableuuid_addr(real_proc), so->e_uuid)) {
1087 			effective_proc = flow_divert_find_proc_by_uuid(so->e_uuid);
1088 		}
1089 	}
1090 
1091 #if defined(XNU_TARGET_OS_OSX)
1092 	lck_rw_lock_shared(&fd_cb->group->lck);
1093 	if (!(fd_cb->group->flags & FLOW_DIVERT_GROUP_FLAG_NO_APP_MAP)) {
1094 		if (so->so_rpid > 0) {
1095 			responsible_proc = proc_find(so->so_rpid);
1096 		}
1097 	}
1098 	lck_rw_done(&fd_cb->group->lck);
1099 #endif
1100 
1101 	real_src_proc = real_proc;
1102 
1103 	if (responsible_proc != PROC_NULL) {
1104 		src_proc = responsible_proc;
1105 		if (effective_proc != NULL) {
1106 			real_src_proc = effective_proc;
1107 		}
1108 	} else if (effective_proc != PROC_NULL) {
1109 		src_proc = effective_proc;
1110 	} else {
1111 		src_proc = real_proc;
1112 	}
1113 
1114 	error = flow_divert_add_proc_info(fd_cb, src_proc, signing_id, connect_packet, true);
1115 	if (error != 0) {
1116 		goto done;
1117 	}
1118 
1119 	if (real_src_proc != NULL && real_src_proc != src_proc) {
1120 		error = flow_divert_add_proc_info(fd_cb, real_src_proc, NULL, connect_packet, false);
1121 		if (error != 0) {
1122 			goto done;
1123 		}
1124 	}
1125 
1126 done:
1127 	if (responsible_proc != PROC_NULL) {
1128 		proc_rele(responsible_proc);
1129 	}
1130 
1131 	if (effective_proc != PROC_NULL) {
1132 		proc_rele(effective_proc);
1133 	}
1134 
1135 	if (real_proc != PROC_NULL && release_real_proc) {
1136 		proc_rele(real_proc);
1137 	}
1138 
1139 	return error;
1140 }
1141 
1142 static int
flow_divert_send_packet(struct flow_divert_pcb * fd_cb,mbuf_t packet,Boolean enqueue)1143 flow_divert_send_packet(struct flow_divert_pcb *fd_cb, mbuf_t packet, Boolean enqueue)
1144 {
1145 	int             error;
1146 
1147 	if (fd_cb->group == NULL) {
1148 		FDLOG0(LOG_INFO, fd_cb, "no provider, cannot send packet");
1149 		flow_divert_update_closed_state(fd_cb, SHUT_RDWR, TRUE);
1150 		flow_divert_disconnect_socket(fd_cb->so, !(fd_cb->flags & FLOW_DIVERT_IMPLICIT_CONNECT));
1151 		if (SOCK_TYPE(fd_cb->so) == SOCK_STREAM) {
1152 			error = ECONNABORTED;
1153 		} else {
1154 			error = EHOSTUNREACH;
1155 		}
1156 		fd_cb->so->so_error = (uint16_t)error;
1157 		return error;
1158 	}
1159 
1160 	lck_rw_lock_shared(&fd_cb->group->lck);
1161 
1162 	if (MBUFQ_EMPTY(&fd_cb->group->send_queue)) {
1163 		error = ctl_enqueuembuf(g_flow_divert_kctl_ref, fd_cb->group->ctl_unit, packet, CTL_DATA_EOR);
1164 	} else {
1165 		error = ENOBUFS;
1166 	}
1167 
1168 	if (error == ENOBUFS) {
1169 		if (enqueue) {
1170 			if (!lck_rw_lock_shared_to_exclusive(&fd_cb->group->lck)) {
1171 				lck_rw_lock_exclusive(&fd_cb->group->lck);
1172 			}
1173 			MBUFQ_ENQUEUE(&fd_cb->group->send_queue, packet);
1174 			error = 0;
1175 		}
1176 		OSTestAndSet(GROUP_BIT_CTL_ENQUEUE_BLOCKED, &fd_cb->group->atomic_bits);
1177 	}
1178 
1179 	lck_rw_done(&fd_cb->group->lck);
1180 
1181 	return error;
1182 }
1183 
1184 static int
flow_divert_create_connect_packet(struct flow_divert_pcb * fd_cb,struct sockaddr * to,struct socket * so,proc_t p,mbuf_t * out_connect_packet)1185 flow_divert_create_connect_packet(struct flow_divert_pcb *fd_cb, struct sockaddr *to, struct socket *so, proc_t p, mbuf_t *out_connect_packet)
1186 {
1187 	int                     error                   = 0;
1188 	int                     flow_type               = 0;
1189 	char                    *signing_id = NULL;
1190 	uint32_t                sid_size = 0;
1191 	mbuf_t                  connect_packet = NULL;
1192 	cfil_sock_id_t          cfil_sock_id            = CFIL_SOCK_ID_NONE;
1193 	const void              *cfil_id                = NULL;
1194 	size_t                  cfil_id_size            = 0;
1195 	struct inpcb            *inp = sotoinpcb(so);
1196 	struct ifnet *ifp = NULL;
1197 	uint32_t flags = 0;
1198 
1199 	error = flow_divert_packet_init(fd_cb, FLOW_DIVERT_PKT_CONNECT, &connect_packet);
1200 	if (error) {
1201 		goto done;
1202 	}
1203 
1204 	if (fd_cb->connect_token != NULL && (fd_cb->flags & FLOW_DIVERT_HAS_HMAC)) {
1205 		int find_error = flow_divert_packet_get_tlv(fd_cb->connect_token, 0, FLOW_DIVERT_TLV_SIGNING_ID, 0, NULL, &sid_size);
1206 		if (find_error == 0 && sid_size > 0) {
1207 			signing_id = kalloc_data(sid_size + 1, Z_WAITOK | Z_ZERO);
1208 			if (signing_id != NULL) {
1209 				flow_divert_packet_get_tlv(fd_cb->connect_token, 0, FLOW_DIVERT_TLV_SIGNING_ID, sid_size, signing_id, NULL);
1210 				FDLOG(LOG_INFO, fd_cb, "Got %s from token", signing_id);
1211 			}
1212 		}
1213 	}
1214 
1215 	error = flow_divert_add_all_proc_info(fd_cb, so, p, signing_id, connect_packet);
1216 
1217 	if (signing_id != NULL) {
1218 		kfree_data(signing_id, sid_size + 1);
1219 	}
1220 
1221 	if (error) {
1222 		FDLOG(LOG_ERR, fd_cb, "Failed to add source proc info: %d", error);
1223 		goto done;
1224 	}
1225 
1226 	error = flow_divert_packet_append_tlv(connect_packet,
1227 	    FLOW_DIVERT_TLV_TRAFFIC_CLASS,
1228 	    sizeof(fd_cb->so->so_traffic_class),
1229 	    &fd_cb->so->so_traffic_class);
1230 	if (error) {
1231 		goto done;
1232 	}
1233 
1234 	if (SOCK_TYPE(fd_cb->so) == SOCK_STREAM) {
1235 		flow_type = FLOW_DIVERT_FLOW_TYPE_TCP;
1236 	} else if (SOCK_TYPE(fd_cb->so) == SOCK_DGRAM) {
1237 		flow_type = FLOW_DIVERT_FLOW_TYPE_UDP;
1238 	} else {
1239 		error = EINVAL;
1240 		goto done;
1241 	}
1242 	error = flow_divert_packet_append_tlv(connect_packet,
1243 	    FLOW_DIVERT_TLV_FLOW_TYPE,
1244 	    sizeof(flow_type),
1245 	    &flow_type);
1246 
1247 	if (error) {
1248 		goto done;
1249 	}
1250 
1251 	if (fd_cb->connect_token != NULL) {
1252 		unsigned int token_len = m_length(fd_cb->connect_token);
1253 		mbuf_concatenate(connect_packet, fd_cb->connect_token);
1254 		mbuf_pkthdr_adjustlen(connect_packet, token_len);
1255 		fd_cb->connect_token = NULL;
1256 	} else {
1257 		error = flow_divert_append_target_endpoint_tlv(connect_packet, to);
1258 		if (error) {
1259 			goto done;
1260 		}
1261 
1262 		if (inp->inp_necp_attributes.inp_domain != NULL) {
1263 			size_t domain_length = strlen(inp->inp_necp_attributes.inp_domain);
1264 			if (domain_length > 0 && domain_length <= FLOW_DIVERT_MAX_NAME_SIZE) {
1265 				error = flow_divert_packet_append_tlv(connect_packet, FLOW_DIVERT_TLV_TARGET_HOSTNAME, (uint32_t)domain_length, inp->inp_necp_attributes.inp_domain);
1266 			}
1267 		}
1268 	}
1269 
1270 	if (fd_cb->local_endpoint.sa.sa_family == AF_INET || fd_cb->local_endpoint.sa.sa_family == AF_INET6) {
1271 		error = flow_divert_packet_append_tlv(connect_packet, FLOW_DIVERT_TLV_LOCAL_ADDR, fd_cb->local_endpoint.sa.sa_len, &(fd_cb->local_endpoint.sa));
1272 		if (error) {
1273 			goto done;
1274 		}
1275 	}
1276 
1277 	if (inp->inp_vflag & INP_IPV4) {
1278 		ifp = inp->inp_last_outifp;
1279 	} else if (inp->inp_vflag & INP_IPV6) {
1280 		ifp = inp->in6p_last_outifp;
1281 	}
1282 	if (ifp != NULL) {
1283 		uint32_t flow_if_index = ifp->if_index;
1284 		error = flow_divert_packet_append_tlv(connect_packet, FLOW_DIVERT_TLV_OUT_IF_INDEX,
1285 		    sizeof(flow_if_index), &flow_if_index);
1286 		if (error) {
1287 			goto done;
1288 		}
1289 	}
1290 
1291 	if (so->so_flags1 & SOF1_DATA_IDEMPOTENT) {
1292 		flags |= FLOW_DIVERT_TOKEN_FLAG_TFO;
1293 	}
1294 
1295 	if ((inp->inp_flags & INP_BOUND_IF) ||
1296 	    ((inp->inp_vflag & INP_IPV6) && !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) ||
1297 	    ((inp->inp_vflag & INP_IPV4) && inp->inp_laddr.s_addr != INADDR_ANY)) {
1298 		flags |= FLOW_DIVERT_TOKEN_FLAG_BOUND;
1299 	}
1300 
1301 	if (flags != 0) {
1302 		error = flow_divert_packet_append_tlv(connect_packet, FLOW_DIVERT_TLV_FLAGS, sizeof(flags), &flags);
1303 		if (error) {
1304 			goto done;
1305 		}
1306 	}
1307 
1308 	if (SOCK_TYPE(so) == SOCK_DGRAM) {
1309 		cfil_sock_id = cfil_sock_id_from_datagram_socket(so, NULL, to);
1310 	} else {
1311 		cfil_sock_id = cfil_sock_id_from_socket(so);
1312 	}
1313 
1314 	if (cfil_sock_id != CFIL_SOCK_ID_NONE) {
1315 		cfil_id = &cfil_sock_id;
1316 		cfil_id_size = sizeof(cfil_sock_id);
1317 	} else if (so->so_flags1 & SOF1_CONTENT_FILTER_SKIP) {
1318 		cfil_id = &inp->necp_client_uuid;
1319 		cfil_id_size = sizeof(inp->necp_client_uuid);
1320 	}
1321 
1322 	if (cfil_id != NULL && cfil_id_size > 0 && cfil_id_size <= sizeof(uuid_t)) {
1323 		error = flow_divert_packet_append_tlv(connect_packet, FLOW_DIVERT_TLV_CFIL_ID, (uint32_t)cfil_id_size, cfil_id);
1324 		if (error) {
1325 			goto done;
1326 		}
1327 	}
1328 
1329 done:
1330 	if (!error) {
1331 		*out_connect_packet = connect_packet;
1332 	} else if (connect_packet != NULL) {
1333 		mbuf_freem(connect_packet);
1334 	}
1335 
1336 	return error;
1337 }
1338 
1339 static int
flow_divert_send_connect_packet(struct flow_divert_pcb * fd_cb)1340 flow_divert_send_connect_packet(struct flow_divert_pcb *fd_cb)
1341 {
1342 	int error = 0;
1343 	mbuf_t connect_packet = fd_cb->connect_packet;
1344 	mbuf_t saved_connect_packet = NULL;
1345 
1346 	if (connect_packet != NULL) {
1347 		error = mbuf_copym(connect_packet, 0, mbuf_pkthdr_len(connect_packet), MBUF_DONTWAIT, &saved_connect_packet);
1348 		if (error) {
1349 			FDLOG0(LOG_ERR, fd_cb, "Failed to copy the connect packet");
1350 			goto done;
1351 		}
1352 
1353 		error = flow_divert_send_packet(fd_cb, connect_packet, TRUE);
1354 		if (error) {
1355 			goto done;
1356 		}
1357 
1358 		fd_cb->connect_packet = saved_connect_packet;
1359 		saved_connect_packet = NULL;
1360 	} else {
1361 		error = ENOENT;
1362 	}
1363 done:
1364 	if (saved_connect_packet != NULL) {
1365 		mbuf_freem(saved_connect_packet);
1366 	}
1367 
1368 	return error;
1369 }
1370 
1371 static int
flow_divert_send_connect_result(struct flow_divert_pcb * fd_cb)1372 flow_divert_send_connect_result(struct flow_divert_pcb *fd_cb)
1373 {
1374 	int             error                   = 0;
1375 	mbuf_t  packet                  = NULL;
1376 	int             rbuff_space             = 0;
1377 
1378 	error = flow_divert_packet_init(fd_cb, FLOW_DIVERT_PKT_CONNECT_RESULT, &packet);
1379 	if (error) {
1380 		FDLOG(LOG_ERR, fd_cb, "failed to create a connect result packet: %d", error);
1381 		goto done;
1382 	}
1383 
1384 	rbuff_space = fd_cb->so->so_rcv.sb_hiwat;
1385 	if (rbuff_space < 0) {
1386 		rbuff_space = 0;
1387 	}
1388 	rbuff_space = htonl(rbuff_space);
1389 	error = flow_divert_packet_append_tlv(packet,
1390 	    FLOW_DIVERT_TLV_SPACE_AVAILABLE,
1391 	    sizeof(rbuff_space),
1392 	    &rbuff_space);
1393 	if (error) {
1394 		goto done;
1395 	}
1396 
1397 	if (fd_cb->local_endpoint.sa.sa_family == AF_INET || fd_cb->local_endpoint.sa.sa_family == AF_INET6) {
1398 		error = flow_divert_packet_append_tlv(packet, FLOW_DIVERT_TLV_LOCAL_ADDR, fd_cb->local_endpoint.sa.sa_len, &(fd_cb->local_endpoint.sa));
1399 		if (error) {
1400 			goto done;
1401 		}
1402 	}
1403 
1404 	error = flow_divert_send_packet(fd_cb, packet, TRUE);
1405 	if (error) {
1406 		goto done;
1407 	}
1408 
1409 done:
1410 	if (error && packet != NULL) {
1411 		mbuf_freem(packet);
1412 	}
1413 
1414 	return error;
1415 }
1416 
1417 static int
flow_divert_send_close(struct flow_divert_pcb * fd_cb,int how)1418 flow_divert_send_close(struct flow_divert_pcb *fd_cb, int how)
1419 {
1420 	int             error   = 0;
1421 	mbuf_t  packet  = NULL;
1422 	uint32_t        zero    = 0;
1423 
1424 	error = flow_divert_packet_init(fd_cb, FLOW_DIVERT_PKT_CLOSE, &packet);
1425 	if (error) {
1426 		FDLOG(LOG_ERR, fd_cb, "failed to create a close packet: %d", error);
1427 		goto done;
1428 	}
1429 
1430 	error = flow_divert_packet_append_tlv(packet, FLOW_DIVERT_TLV_ERROR_CODE, sizeof(zero), &zero);
1431 	if (error) {
1432 		FDLOG(LOG_ERR, fd_cb, "failed to add the error code TLV: %d", error);
1433 		goto done;
1434 	}
1435 
1436 	how = htonl(how);
1437 	error = flow_divert_packet_append_tlv(packet, FLOW_DIVERT_TLV_HOW, sizeof(how), &how);
1438 	if (error) {
1439 		FDLOG(LOG_ERR, fd_cb, "failed to add the how flag: %d", error);
1440 		goto done;
1441 	}
1442 
1443 	error = flow_divert_send_packet(fd_cb, packet, TRUE);
1444 	if (error) {
1445 		goto done;
1446 	}
1447 
1448 done:
1449 	if (error && packet != NULL) {
1450 		mbuf_free(packet);
1451 	}
1452 
1453 	return error;
1454 }
1455 
1456 static int
flow_divert_tunnel_how_closed(struct flow_divert_pcb * fd_cb)1457 flow_divert_tunnel_how_closed(struct flow_divert_pcb *fd_cb)
1458 {
1459 	if ((fd_cb->flags & (FLOW_DIVERT_TUNNEL_RD_CLOSED | FLOW_DIVERT_TUNNEL_WR_CLOSED)) ==
1460 	    (FLOW_DIVERT_TUNNEL_RD_CLOSED | FLOW_DIVERT_TUNNEL_WR_CLOSED)) {
1461 		return SHUT_RDWR;
1462 	} else if (fd_cb->flags & FLOW_DIVERT_TUNNEL_RD_CLOSED) {
1463 		return SHUT_RD;
1464 	} else if (fd_cb->flags & FLOW_DIVERT_TUNNEL_WR_CLOSED) {
1465 		return SHUT_WR;
1466 	}
1467 
1468 	return -1;
1469 }
1470 
1471 /*
1472  * Determine what close messages if any need to be sent to the tunnel. Returns TRUE if the tunnel is closed for both reads and
1473  * writes. Returns FALSE otherwise.
1474  */
1475 static void
flow_divert_send_close_if_needed(struct flow_divert_pcb * fd_cb)1476 flow_divert_send_close_if_needed(struct flow_divert_pcb *fd_cb)
1477 {
1478 	int             how             = -1;
1479 
1480 	/* Do not send any close messages if there is still data in the send buffer */
1481 	if (fd_cb->so->so_snd.sb_cc == 0) {
1482 		if ((fd_cb->flags & (FLOW_DIVERT_READ_CLOSED | FLOW_DIVERT_TUNNEL_RD_CLOSED)) == FLOW_DIVERT_READ_CLOSED) {
1483 			/* Socket closed reads, but tunnel did not. Tell tunnel to close reads */
1484 			how = SHUT_RD;
1485 		}
1486 		if ((fd_cb->flags & (FLOW_DIVERT_WRITE_CLOSED | FLOW_DIVERT_TUNNEL_WR_CLOSED)) == FLOW_DIVERT_WRITE_CLOSED) {
1487 			/* Socket closed writes, but tunnel did not. Tell tunnel to close writes */
1488 			if (how == SHUT_RD) {
1489 				how = SHUT_RDWR;
1490 			} else {
1491 				how = SHUT_WR;
1492 			}
1493 		}
1494 	}
1495 
1496 	if (how != -1) {
1497 		FDLOG(LOG_INFO, fd_cb, "sending close, how = %d", how);
1498 		if (flow_divert_send_close(fd_cb, how) != ENOBUFS) {
1499 			/* Successfully sent the close packet. Record the ways in which the tunnel has been closed */
1500 			if (how != SHUT_RD) {
1501 				fd_cb->flags |= FLOW_DIVERT_TUNNEL_WR_CLOSED;
1502 			}
1503 			if (how != SHUT_WR) {
1504 				fd_cb->flags |= FLOW_DIVERT_TUNNEL_RD_CLOSED;
1505 			}
1506 		}
1507 	}
1508 
1509 	if (flow_divert_tunnel_how_closed(fd_cb) == SHUT_RDWR) {
1510 		flow_divert_disconnect_socket(fd_cb->so, !(fd_cb->flags & FLOW_DIVERT_IMPLICIT_CONNECT));
1511 	}
1512 }
1513 
1514 static errno_t
flow_divert_send_data_packet(struct flow_divert_pcb * fd_cb,mbuf_t data,size_t data_len,struct sockaddr * toaddr,Boolean force)1515 flow_divert_send_data_packet(struct flow_divert_pcb *fd_cb, mbuf_t data, size_t data_len, struct sockaddr *toaddr, Boolean force)
1516 {
1517 	mbuf_t  packet = NULL;
1518 	mbuf_t  last = NULL;
1519 	int             error   = 0;
1520 
1521 	error = flow_divert_packet_init(fd_cb, FLOW_DIVERT_PKT_DATA, &packet);
1522 	if (error || packet == NULL) {
1523 		FDLOG(LOG_ERR, fd_cb, "flow_divert_packet_init failed: %d", error);
1524 		goto done;
1525 	}
1526 
1527 	if (toaddr != NULL) {
1528 		error = flow_divert_append_target_endpoint_tlv(packet, toaddr);
1529 		if (error) {
1530 			FDLOG(LOG_ERR, fd_cb, "flow_divert_append_target_endpoint_tlv() failed: %d", error);
1531 			goto done;
1532 		}
1533 	}
1534 
1535 	if (data_len > 0 && data_len <= INT_MAX && data != NULL) {
1536 		last = m_last(packet);
1537 		mbuf_setnext(last, data);
1538 		mbuf_pkthdr_adjustlen(packet, (int)data_len);
1539 	} else {
1540 		data_len = 0;
1541 	}
1542 	error = flow_divert_send_packet(fd_cb, packet, force);
1543 	if (error == 0 && data_len > 0) {
1544 		fd_cb->bytes_sent += data_len;
1545 		flow_divert_add_data_statistics(fd_cb, data_len, TRUE);
1546 	}
1547 
1548 done:
1549 	if (error) {
1550 		if (last != NULL) {
1551 			mbuf_setnext(last, NULL);
1552 		}
1553 		if (packet != NULL) {
1554 			mbuf_freem(packet);
1555 		}
1556 	}
1557 
1558 	return error;
1559 }
1560 
1561 static void
flow_divert_send_buffered_data(struct flow_divert_pcb * fd_cb,Boolean force)1562 flow_divert_send_buffered_data(struct flow_divert_pcb *fd_cb, Boolean force)
1563 {
1564 	size_t  to_send;
1565 	size_t  sent    = 0;
1566 	int             error   = 0;
1567 	mbuf_t  buffer;
1568 
1569 	to_send = fd_cb->so->so_snd.sb_cc;
1570 	buffer = fd_cb->so->so_snd.sb_mb;
1571 
1572 	if (buffer == NULL && to_send > 0) {
1573 		FDLOG(LOG_ERR, fd_cb, "Send buffer is NULL, but size is supposed to be %lu", to_send);
1574 		return;
1575 	}
1576 
1577 	/* Ignore the send window if force is enabled */
1578 	if (!force && (to_send > fd_cb->send_window)) {
1579 		to_send = fd_cb->send_window;
1580 	}
1581 
1582 	if (SOCK_TYPE(fd_cb->so) == SOCK_STREAM) {
1583 		while (sent < to_send) {
1584 			mbuf_t  data;
1585 			size_t  data_len;
1586 
1587 			data_len = to_send - sent;
1588 			if (data_len > FLOW_DIVERT_CHUNK_SIZE) {
1589 				data_len = FLOW_DIVERT_CHUNK_SIZE;
1590 			}
1591 
1592 			error = mbuf_copym(buffer, sent, data_len, MBUF_DONTWAIT, &data);
1593 			if (error) {
1594 				FDLOG(LOG_ERR, fd_cb, "mbuf_copym failed: %d", error);
1595 				break;
1596 			}
1597 
1598 			error = flow_divert_send_data_packet(fd_cb, data, data_len, NULL, force);
1599 			if (error) {
1600 				if (data != NULL) {
1601 					mbuf_freem(data);
1602 				}
1603 				break;
1604 			}
1605 
1606 			sent += data_len;
1607 		}
1608 		sbdrop(&fd_cb->so->so_snd, (int)sent);
1609 		sowwakeup(fd_cb->so);
1610 	} else if (SOCK_TYPE(fd_cb->so) == SOCK_DGRAM) {
1611 		mbuf_t data;
1612 		mbuf_t m;
1613 		size_t data_len;
1614 
1615 		while (buffer) {
1616 			struct sockaddr *toaddr = flow_divert_get_buffered_target_address(buffer);
1617 
1618 			m = buffer;
1619 			if (toaddr != NULL) {
1620 				/* look for data in the chain */
1621 				do {
1622 					m = m->m_next;
1623 					if (m != NULL && m->m_type == MT_DATA) {
1624 						break;
1625 					}
1626 				} while (m);
1627 				if (m == NULL) {
1628 					/* unexpected */
1629 					FDLOG0(LOG_ERR, fd_cb, "failed to find type MT_DATA in the mbuf chain.");
1630 					goto move_on;
1631 				}
1632 			}
1633 			data_len = mbuf_pkthdr_len(m);
1634 			if (data_len > 0) {
1635 				FDLOG(LOG_DEBUG, fd_cb, "mbuf_copym() data_len = %lu", data_len);
1636 				error = mbuf_copym(m, 0, data_len, MBUF_DONTWAIT, &data);
1637 				if (error) {
1638 					FDLOG(LOG_ERR, fd_cb, "mbuf_copym failed: %d", error);
1639 					break;
1640 				}
1641 			} else {
1642 				data = NULL;
1643 			}
1644 			error = flow_divert_send_data_packet(fd_cb, data, data_len, toaddr, force);
1645 			if (error) {
1646 				if (data != NULL) {
1647 					mbuf_freem(data);
1648 				}
1649 				break;
1650 			}
1651 			sent += data_len;
1652 move_on:
1653 			buffer = buffer->m_nextpkt;
1654 			(void) sbdroprecord(&(fd_cb->so->so_snd));
1655 		}
1656 	}
1657 
1658 	if (sent > 0) {
1659 		FDLOG(LOG_DEBUG, fd_cb, "sent %lu bytes of buffered data", sent);
1660 		if (fd_cb->send_window >= sent) {
1661 			fd_cb->send_window -= sent;
1662 		} else {
1663 			fd_cb->send_window = 0;
1664 		}
1665 	}
1666 }
1667 
1668 static int
flow_divert_send_app_data(struct flow_divert_pcb * fd_cb,mbuf_t data,struct sockaddr * toaddr)1669 flow_divert_send_app_data(struct flow_divert_pcb *fd_cb, mbuf_t data, struct sockaddr *toaddr)
1670 {
1671 	size_t  to_send         = mbuf_pkthdr_len(data);
1672 	int     error           = 0;
1673 
1674 	if (to_send > fd_cb->send_window) {
1675 		to_send = fd_cb->send_window;
1676 	}
1677 
1678 	if (fd_cb->so->so_snd.sb_cc > 0) {
1679 		to_send = 0;    /* If the send buffer is non-empty, then we can't send anything */
1680 	}
1681 
1682 	if (SOCK_TYPE(fd_cb->so) == SOCK_STREAM) {
1683 		size_t  sent            = 0;
1684 		mbuf_t  remaining_data  = data;
1685 		mbuf_t  pkt_data        = NULL;
1686 		while (sent < to_send && remaining_data != NULL) {
1687 			size_t  pkt_data_len;
1688 
1689 			pkt_data = remaining_data;
1690 
1691 			if ((to_send - sent) > FLOW_DIVERT_CHUNK_SIZE) {
1692 				pkt_data_len = FLOW_DIVERT_CHUNK_SIZE;
1693 			} else {
1694 				pkt_data_len = to_send - sent;
1695 			}
1696 
1697 			if (pkt_data_len < mbuf_pkthdr_len(pkt_data)) {
1698 				error = mbuf_split(pkt_data, pkt_data_len, MBUF_DONTWAIT, &remaining_data);
1699 				if (error) {
1700 					FDLOG(LOG_ERR, fd_cb, "mbuf_split failed: %d", error);
1701 					pkt_data = NULL;
1702 					break;
1703 				}
1704 			} else {
1705 				remaining_data = NULL;
1706 			}
1707 
1708 			error = flow_divert_send_data_packet(fd_cb, pkt_data, pkt_data_len, NULL, FALSE);
1709 
1710 			if (error) {
1711 				break;
1712 			}
1713 
1714 			pkt_data = NULL;
1715 			sent += pkt_data_len;
1716 		}
1717 
1718 		fd_cb->send_window -= sent;
1719 
1720 		error = 0;
1721 
1722 		if (pkt_data != NULL) {
1723 			if (sbspace(&fd_cb->so->so_snd) > 0) {
1724 				if (!sbappendstream(&fd_cb->so->so_snd, pkt_data)) {
1725 					FDLOG(LOG_ERR, fd_cb, "sbappendstream failed with pkt_data, send buffer size = %u, send_window = %u\n",
1726 					    fd_cb->so->so_snd.sb_cc, fd_cb->send_window);
1727 				}
1728 			} else {
1729 				mbuf_freem(pkt_data);
1730 				error = ENOBUFS;
1731 			}
1732 		}
1733 
1734 		if (remaining_data != NULL) {
1735 			if (sbspace(&fd_cb->so->so_snd) > 0) {
1736 				if (!sbappendstream(&fd_cb->so->so_snd, remaining_data)) {
1737 					FDLOG(LOG_ERR, fd_cb, "sbappendstream failed with remaining_data, send buffer size = %u, send_window = %u\n",
1738 					    fd_cb->so->so_snd.sb_cc, fd_cb->send_window);
1739 				}
1740 			} else {
1741 				mbuf_freem(remaining_data);
1742 				error = ENOBUFS;
1743 			}
1744 		}
1745 	} else if (SOCK_TYPE(fd_cb->so) == SOCK_DGRAM) {
1746 		if (to_send || mbuf_pkthdr_len(data) == 0) {
1747 			error = flow_divert_send_data_packet(fd_cb, data, to_send, toaddr, FALSE);
1748 			if (error) {
1749 				FDLOG(LOG_ERR, fd_cb, "flow_divert_send_data_packet failed. send data size = %lu", to_send);
1750 				if (data != NULL) {
1751 					mbuf_freem(data);
1752 				}
1753 			} else {
1754 				fd_cb->send_window -= to_send;
1755 			}
1756 		} else {
1757 			/* buffer it */
1758 			if (sbspace(&fd_cb->so->so_snd) >= (int)mbuf_pkthdr_len(data)) {
1759 				if (toaddr != NULL) {
1760 					if (!sbappendaddr(&fd_cb->so->so_snd, toaddr, data, NULL, &error)) {
1761 						FDLOG(LOG_ERR, fd_cb,
1762 						    "sbappendaddr failed. send buffer size = %u, send_window = %u, error = %d\n",
1763 						    fd_cb->so->so_snd.sb_cc, fd_cb->send_window, error);
1764 					}
1765 					error = 0;
1766 				} else {
1767 					if (!sbappendrecord(&fd_cb->so->so_snd, data)) {
1768 						FDLOG(LOG_ERR, fd_cb,
1769 						    "sbappendrecord failed. send buffer size = %u, send_window = %u, error = %d\n",
1770 						    fd_cb->so->so_snd.sb_cc, fd_cb->send_window, error);
1771 					}
1772 				}
1773 			} else {
1774 				if (data != NULL) {
1775 					mbuf_freem(data);
1776 				}
1777 				error = ENOBUFS;
1778 			}
1779 		}
1780 	}
1781 
1782 	return error;
1783 }
1784 
1785 static int
flow_divert_send_read_notification(struct flow_divert_pcb * fd_cb)1786 flow_divert_send_read_notification(struct flow_divert_pcb *fd_cb)
1787 {
1788 	int error = 0;
1789 	mbuf_t packet = NULL;
1790 
1791 	error = flow_divert_packet_init(fd_cb, FLOW_DIVERT_PKT_READ_NOTIFY, &packet);
1792 	if (error) {
1793 		FDLOG(LOG_ERR, fd_cb, "failed to create a read notification packet: %d", error);
1794 		goto done;
1795 	}
1796 
1797 	error = flow_divert_send_packet(fd_cb, packet, TRUE);
1798 	if (error) {
1799 		goto done;
1800 	}
1801 
1802 done:
1803 	if (error && packet != NULL) {
1804 		mbuf_free(packet);
1805 	}
1806 
1807 	return error;
1808 }
1809 
1810 static int
flow_divert_send_traffic_class_update(struct flow_divert_pcb * fd_cb,int traffic_class)1811 flow_divert_send_traffic_class_update(struct flow_divert_pcb *fd_cb, int traffic_class)
1812 {
1813 	int             error           = 0;
1814 	mbuf_t  packet          = NULL;
1815 
1816 	error = flow_divert_packet_init(fd_cb, FLOW_DIVERT_PKT_PROPERTIES_UPDATE, &packet);
1817 	if (error) {
1818 		FDLOG(LOG_ERR, fd_cb, "failed to create a properties update packet: %d", error);
1819 		goto done;
1820 	}
1821 
1822 	error = flow_divert_packet_append_tlv(packet, FLOW_DIVERT_TLV_TRAFFIC_CLASS, sizeof(traffic_class), &traffic_class);
1823 	if (error) {
1824 		FDLOG(LOG_ERR, fd_cb, "failed to add the traffic class: %d", error);
1825 		goto done;
1826 	}
1827 
1828 	error = flow_divert_send_packet(fd_cb, packet, TRUE);
1829 	if (error) {
1830 		goto done;
1831 	}
1832 
1833 done:
1834 	if (error && packet != NULL) {
1835 		mbuf_free(packet);
1836 	}
1837 
1838 	return error;
1839 }
1840 
1841 static void
flow_divert_set_local_endpoint(struct flow_divert_pcb * fd_cb,struct sockaddr * local_endpoint)1842 flow_divert_set_local_endpoint(struct flow_divert_pcb *fd_cb, struct sockaddr *local_endpoint)
1843 {
1844 	struct inpcb *inp = sotoinpcb(fd_cb->so);
1845 
1846 	if (local_endpoint->sa_family == AF_INET6) {
1847 		if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) && (fd_cb->flags & FLOW_DIVERT_SHOULD_SET_LOCAL_ADDR)) {
1848 			fd_cb->flags |= FLOW_DIVERT_DID_SET_LOCAL_ADDR;
1849 			inp->in6p_laddr = (satosin6(local_endpoint))->sin6_addr;
1850 			inp->inp_lifscope = (satosin6(local_endpoint))->sin6_scope_id;
1851 			in6_verify_ifscope(&inp->in6p_laddr, inp->inp_lifscope);
1852 		}
1853 		if (inp->inp_lport == 0) {
1854 			inp->inp_lport = (satosin6(local_endpoint))->sin6_port;
1855 		}
1856 	} else if (local_endpoint->sa_family == AF_INET) {
1857 		if (inp->inp_laddr.s_addr == INADDR_ANY && (fd_cb->flags & FLOW_DIVERT_SHOULD_SET_LOCAL_ADDR)) {
1858 			fd_cb->flags |= FLOW_DIVERT_DID_SET_LOCAL_ADDR;
1859 			inp->inp_laddr = (satosin(local_endpoint))->sin_addr;
1860 		}
1861 		if (inp->inp_lport == 0) {
1862 			inp->inp_lport = (satosin(local_endpoint))->sin_port;
1863 		}
1864 	}
1865 }
1866 
1867 static void
flow_divert_set_remote_endpoint(struct flow_divert_pcb * fd_cb,struct sockaddr * remote_endpoint)1868 flow_divert_set_remote_endpoint(struct flow_divert_pcb *fd_cb, struct sockaddr *remote_endpoint)
1869 {
1870 	struct inpcb *inp = sotoinpcb(fd_cb->so);
1871 
1872 	if (remote_endpoint->sa_family == AF_INET6) {
1873 		if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1874 			inp->in6p_faddr = (satosin6(remote_endpoint))->sin6_addr;
1875 			inp->inp_fifscope = (satosin6(remote_endpoint))->sin6_scope_id;
1876 			in6_verify_ifscope(&inp->in6p_faddr, inp->inp_fifscope);
1877 		}
1878 		if (inp->inp_fport == 0) {
1879 			inp->inp_fport = (satosin6(remote_endpoint))->sin6_port;
1880 		}
1881 	} else if (remote_endpoint->sa_family == AF_INET) {
1882 		if (inp->inp_faddr.s_addr == INADDR_ANY) {
1883 			inp->inp_faddr = (satosin(remote_endpoint))->sin_addr;
1884 		}
1885 		if (inp->inp_fport == 0) {
1886 			inp->inp_fport = (satosin(remote_endpoint))->sin_port;
1887 		}
1888 	}
1889 }
1890 
1891 static uint32_t
flow_divert_derive_kernel_control_unit(uint32_t ctl_unit,uint32_t * aggregate_unit,bool * is_aggregate)1892 flow_divert_derive_kernel_control_unit(uint32_t ctl_unit, uint32_t *aggregate_unit, bool *is_aggregate)
1893 {
1894 	*is_aggregate = false;
1895 	if (aggregate_unit != NULL && *aggregate_unit != 0) {
1896 		uint32_t counter;
1897 		for (counter = 0; counter < (GROUP_COUNT_MAX - 1); counter++) {
1898 			if ((*aggregate_unit) & (1 << counter)) {
1899 				break;
1900 			}
1901 		}
1902 		if (counter < (GROUP_COUNT_MAX - 1)) {
1903 			*aggregate_unit &= ~(1 << counter);
1904 			*is_aggregate = true;
1905 			return counter + 1;
1906 		} else {
1907 			return ctl_unit;
1908 		}
1909 	} else {
1910 		return ctl_unit;
1911 	}
1912 }
1913 
1914 static int
flow_divert_try_next(struct flow_divert_pcb * fd_cb)1915 flow_divert_try_next(struct flow_divert_pcb *fd_cb)
1916 {
1917 	uint32_t current_ctl_unit = 0;
1918 	uint32_t next_ctl_unit = 0;
1919 	struct flow_divert_group *current_group = NULL;
1920 	struct flow_divert_group *next_group = NULL;
1921 	int error = 0;
1922 	bool is_aggregate = false;
1923 
1924 	next_ctl_unit = flow_divert_derive_kernel_control_unit(fd_cb->policy_control_unit, &(fd_cb->aggregate_unit), &is_aggregate);
1925 	current_ctl_unit = fd_cb->control_group_unit;
1926 
1927 	if (current_ctl_unit == next_ctl_unit) {
1928 		FDLOG0(LOG_NOTICE, fd_cb, "Next control unit is the same as the current control unit, disabling flow divert");
1929 		error = EALREADY;
1930 		goto done;
1931 	}
1932 
1933 	if (next_ctl_unit == 0 || next_ctl_unit >= GROUP_COUNT_MAX) {
1934 		FDLOG0(LOG_NOTICE, fd_cb, "No more valid control units, disabling flow divert");
1935 		error = ENOENT;
1936 		goto done;
1937 	}
1938 
1939 	if (g_flow_divert_groups == NULL || g_active_group_count == 0) {
1940 		FDLOG0(LOG_NOTICE, fd_cb, "No active groups, disabling flow divert");
1941 		error = ENOENT;
1942 		goto done;
1943 	}
1944 
1945 	next_group = g_flow_divert_groups[next_ctl_unit];
1946 	if (next_group == NULL) {
1947 		FDLOG(LOG_NOTICE, fd_cb, "Group for control unit %u does not exist", next_ctl_unit);
1948 		error = ENOENT;
1949 		goto done;
1950 	}
1951 
1952 	current_group = fd_cb->group;
1953 
1954 	lck_rw_lock_exclusive(&(current_group->lck));
1955 	lck_rw_lock_exclusive(&(next_group->lck));
1956 
1957 	FDLOG(LOG_NOTICE, fd_cb, "Moving from %u to %u", current_ctl_unit, next_ctl_unit);
1958 
1959 	RB_REMOVE(fd_pcb_tree, &(current_group->pcb_tree), fd_cb);
1960 	if (RB_INSERT(fd_pcb_tree, &(next_group->pcb_tree), fd_cb) != NULL) {
1961 		panic("group with unit %u already contains a connection with hash %u", next_ctl_unit, fd_cb->hash);
1962 	}
1963 
1964 	fd_cb->group = next_group;
1965 	fd_cb->control_group_unit = next_ctl_unit;
1966 	if (is_aggregate) {
1967 		fd_cb->flags |= FLOW_DIVERT_FLOW_IS_TRANSPARENT;
1968 	} else {
1969 		fd_cb->flags &= ~FLOW_DIVERT_FLOW_IS_TRANSPARENT;
1970 	}
1971 
1972 	lck_rw_done(&(next_group->lck));
1973 	lck_rw_done(&(current_group->lck));
1974 
1975 	error = flow_divert_send_connect_packet(fd_cb);
1976 	if (error) {
1977 		FDLOG(LOG_NOTICE, fd_cb, "Failed to send the connect packet to %u, disabling flow divert", next_ctl_unit);
1978 		error = ENOENT;
1979 		goto done;
1980 	}
1981 
1982 done:
1983 	return error;
1984 }
1985 
1986 static void
flow_divert_disable(struct flow_divert_pcb * fd_cb)1987 flow_divert_disable(struct flow_divert_pcb *fd_cb)
1988 {
1989 	struct socket *so = NULL;
1990 	mbuf_t  buffer;
1991 	int error = 0;
1992 	proc_t last_proc = NULL;
1993 	struct sockaddr *remote_endpoint = fd_cb->original_remote_endpoint;
1994 	bool do_connect = !(fd_cb->flags & FLOW_DIVERT_IMPLICIT_CONNECT);
1995 	bool unset_connecting = false;
1996 	struct inpcb *inp = NULL;
1997 
1998 	so = fd_cb->so;
1999 	if (so == NULL) {
2000 		goto done;
2001 	}
2002 
2003 	FDLOG0(LOG_NOTICE, fd_cb, "Skipped all flow divert services, disabling flow divert");
2004 
2005 	unset_connecting = (SOCK_TYPE(so) == SOCK_STREAM || (fd_cb->flags & FLOW_DIVERT_HAS_TOKEN));
2006 
2007 	/* Restore the IP state */
2008 	inp = sotoinpcb(so);
2009 	inp->inp_vflag = fd_cb->original_vflag;
2010 	inp->inp_faddr.s_addr = INADDR_ANY;
2011 	inp->inp_fport = 0;
2012 	memset(&(inp->in6p_faddr), 0, sizeof(inp->in6p_faddr));
2013 	inp->inp_fifscope = IFSCOPE_NONE;
2014 	inp->in6p_fport = 0;
2015 	/* If flow divert set the local address, clear it out */
2016 	if (fd_cb->flags & FLOW_DIVERT_DID_SET_LOCAL_ADDR) {
2017 		inp->inp_laddr.s_addr = INADDR_ANY;
2018 		memset(&(inp->in6p_laddr), 0, sizeof(inp->in6p_laddr));
2019 		inp->inp_lifscope = IFSCOPE_NONE;
2020 	}
2021 	inp->inp_last_outifp = fd_cb->original_last_outifp;
2022 	inp->in6p_last_outifp = fd_cb->original_last_outifp6;
2023 
2024 	/* Dis-associate the socket */
2025 	so->so_flags &= ~SOF_FLOW_DIVERT;
2026 	so->so_flags1 |= SOF1_FLOW_DIVERT_SKIP;
2027 	so->so_fd_pcb = NULL;
2028 	fd_cb->so = NULL;
2029 
2030 	/* Remove from the group */
2031 	flow_divert_pcb_remove(fd_cb);
2032 
2033 	FDRELEASE(fd_cb); /* Release the socket's reference */
2034 
2035 	/* Revert back to the original protocol */
2036 	so->so_proto = pffindproto(SOCK_DOM(so), SOCK_PROTO(so), SOCK_TYPE(so));
2037 
2038 	last_proc = proc_find(so->last_pid);
2039 
2040 	if (do_connect) {
2041 		if (unset_connecting) {
2042 			so->so_state &= ~SS_ISCONNECTING; /* Get out of the connecting state to avoid confusing NECP */
2043 		}
2044 		/* Connect using the original protocol */
2045 		error = (*so->so_proto->pr_usrreqs->pru_connect)(so, remote_endpoint, (last_proc != NULL ? last_proc : current_proc()));
2046 		if (error) {
2047 			FDLOG(LOG_ERR, fd_cb, "Failed to connect using the socket's original protocol: %d", error);
2048 			goto done;
2049 		}
2050 	}
2051 
2052 	buffer = so->so_snd.sb_mb;
2053 	if (buffer == NULL) {
2054 		/* No buffered data, done */
2055 		goto done;
2056 	}
2057 
2058 	/* Send any buffered data using the original protocol */
2059 	if (SOCK_TYPE(so) == SOCK_STREAM) {
2060 		mbuf_t data_to_send = NULL;
2061 		size_t data_len = so->so_snd.sb_cc;
2062 
2063 		error = mbuf_copym(buffer, 0, data_len, MBUF_DONTWAIT, &data_to_send);
2064 		if (error) {
2065 			FDLOG0(LOG_ERR, fd_cb, "Failed to copy the mbuf chain in the socket's send buffer");
2066 			goto done;
2067 		}
2068 
2069 		sbflush(&so->so_snd);
2070 
2071 		if (data_to_send->m_flags & M_PKTHDR) {
2072 			mbuf_pkthdr_setlen(data_to_send, data_len);
2073 		}
2074 
2075 		error = (*so->so_proto->pr_usrreqs->pru_send)(so,
2076 		    0,
2077 		    data_to_send,
2078 		    NULL,
2079 		    NULL,
2080 		    (last_proc != NULL ? last_proc : current_proc()));
2081 
2082 		if (error && error != EWOULDBLOCK) {
2083 			FDLOG(LOG_ERR, fd_cb, "Failed to send queued TCP data using the socket's original protocol: %d", error);
2084 		} else {
2085 			error = 0;
2086 		}
2087 	} else if (SOCK_TYPE(so) == SOCK_DGRAM) {
2088 		struct sockbuf *sb = &so->so_snd;
2089 		MBUFQ_HEAD(send_queue_head) send_queue;
2090 		MBUFQ_INIT(&send_queue);
2091 
2092 		/* Flush the send buffer, moving all records to a temporary queue */
2093 		while (sb->sb_mb != NULL) {
2094 			mbuf_t record = sb->sb_mb;
2095 			mbuf_t m = record;
2096 			sb->sb_mb = sb->sb_mb->m_nextpkt;
2097 			while (m != NULL) {
2098 				sbfree(sb, m);
2099 				m = m->m_next;
2100 			}
2101 			record->m_nextpkt = NULL;
2102 			MBUFQ_ENQUEUE(&send_queue, record);
2103 		}
2104 		SB_EMPTY_FIXUP(sb);
2105 
2106 		while (!MBUFQ_EMPTY(&send_queue)) {
2107 			mbuf_t next_record = MBUFQ_FIRST(&send_queue);
2108 			mbuf_t addr = NULL;
2109 			mbuf_t control = NULL;
2110 			mbuf_t last_control = NULL;
2111 			mbuf_t data = NULL;
2112 			mbuf_t m = next_record;
2113 			struct sockaddr *to_endpoint = NULL;
2114 
2115 			MBUFQ_DEQUEUE(&send_queue, next_record);
2116 
2117 			while (m != NULL) {
2118 				if (m->m_type == MT_SONAME) {
2119 					addr = m;
2120 				} else if (m->m_type == MT_CONTROL) {
2121 					if (control == NULL) {
2122 						control = m;
2123 					}
2124 					last_control = m;
2125 				} else if (m->m_type == MT_DATA) {
2126 					data = m;
2127 					break;
2128 				}
2129 				m = m->m_next;
2130 			}
2131 
2132 			if (addr != NULL && !do_connect) {
2133 				to_endpoint = flow_divert_get_buffered_target_address(addr);
2134 				if (to_endpoint == NULL) {
2135 					FDLOG0(LOG_NOTICE, fd_cb, "Failed to get the remote address from the buffer");
2136 				}
2137 			}
2138 
2139 			if (data == NULL) {
2140 				FDLOG0(LOG_ERR, fd_cb, "Buffered record does not contain any data");
2141 				mbuf_freem(next_record);
2142 				continue;
2143 			}
2144 
2145 			if (!(data->m_flags & M_PKTHDR)) {
2146 				FDLOG0(LOG_ERR, fd_cb, "Buffered data does not have a packet header");
2147 				mbuf_freem(next_record);
2148 				continue;
2149 			}
2150 
2151 			if (addr != NULL) {
2152 				addr->m_next = NULL;
2153 			}
2154 
2155 			if (last_control != NULL) {
2156 				last_control->m_next = NULL;
2157 			}
2158 
2159 			error = (*so->so_proto->pr_usrreqs->pru_send)(so,
2160 			    0,
2161 			    data,
2162 			    to_endpoint,
2163 			    control,
2164 			    (last_proc != NULL ? last_proc : current_proc()));
2165 
2166 			if (addr != NULL) {
2167 				mbuf_freem(addr);
2168 			}
2169 
2170 			if (error) {
2171 				FDLOG(LOG_ERR, fd_cb, "Failed to send queued UDP data using the socket's original protocol: %d", error);
2172 			}
2173 		}
2174 	}
2175 done:
2176 	if (last_proc != NULL) {
2177 		proc_rele(last_proc);
2178 	}
2179 
2180 	if (error && so != NULL) {
2181 		so->so_error = (uint16_t)error;
2182 		flow_divert_disconnect_socket(so, do_connect);
2183 	}
2184 }
2185 
2186 static void
flow_divert_scope(struct flow_divert_pcb * fd_cb,int out_if_index,bool derive_new_address)2187 flow_divert_scope(struct flow_divert_pcb *fd_cb, int out_if_index, bool derive_new_address)
2188 {
2189 	struct socket *so = NULL;
2190 	struct inpcb *inp = NULL;
2191 	struct ifnet *current_ifp = NULL;
2192 	struct ifnet *new_ifp = NULL;
2193 	int error = 0;
2194 
2195 	so = fd_cb->so;
2196 	if (so == NULL) {
2197 		return;
2198 	}
2199 
2200 	inp = sotoinpcb(so);
2201 
2202 	if (out_if_index <= 0) {
2203 		return;
2204 	}
2205 
2206 	if (inp->inp_vflag & INP_IPV6) {
2207 		current_ifp = inp->in6p_last_outifp;
2208 	} else {
2209 		current_ifp = inp->inp_last_outifp;
2210 	}
2211 
2212 	if (current_ifp != NULL) {
2213 		if (current_ifp->if_index == out_if_index) {
2214 			/* No change */
2215 			return;
2216 		}
2217 
2218 		/* Scope the socket to the given interface */
2219 		error = inp_bindif(inp, out_if_index, &new_ifp);
2220 		if (error != 0) {
2221 			FDLOG(LOG_ERR, fd_cb, "failed to scope to %d because inp_bindif returned %d", out_if_index, error);
2222 			return;
2223 		}
2224 
2225 		if (derive_new_address && fd_cb->original_remote_endpoint != NULL) {
2226 			/* Get the appropriate address for the given interface */
2227 			if (inp->inp_vflag & INP_IPV6) {
2228 				inp->in6p_laddr = sa6_any.sin6_addr;
2229 				error = in6_pcbladdr(inp, fd_cb->original_remote_endpoint, &(fd_cb->local_endpoint.sin6.sin6_addr), NULL);
2230 			} else {
2231 				inp->inp_laddr.s_addr = INADDR_ANY;
2232 				error = in_pcbladdr(inp, fd_cb->original_remote_endpoint, &(fd_cb->local_endpoint.sin.sin_addr), IFSCOPE_NONE, NULL, 0);
2233 			}
2234 
2235 			if (error != 0) {
2236 				FDLOG(LOG_WARNING, fd_cb, "failed to derive a new local address from %d because in_pcbladdr returned %d", out_if_index, error);
2237 			}
2238 		}
2239 	} else {
2240 		ifnet_head_lock_shared();
2241 		if (out_if_index <= if_index) {
2242 			new_ifp = ifindex2ifnet[out_if_index];
2243 		}
2244 		ifnet_head_done();
2245 	}
2246 
2247 	/* Update the "last interface" of the socket */
2248 	if (new_ifp != NULL) {
2249 		if (inp->inp_vflag & INP_IPV6) {
2250 			inp->in6p_last_outifp = new_ifp;
2251 		} else {
2252 			inp->inp_last_outifp = new_ifp;
2253 		}
2254 
2255 #if SKYWALK
2256 		if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
2257 			netns_set_ifnet(&inp->inp_netns_token, new_ifp);
2258 		}
2259 #endif /* SKYWALK */
2260 	}
2261 }
2262 
2263 static void
flow_divert_handle_connect_result(struct flow_divert_pcb * fd_cb,mbuf_t packet,int offset)2264 flow_divert_handle_connect_result(struct flow_divert_pcb *fd_cb, mbuf_t packet, int offset)
2265 {
2266 	uint32_t                                        connect_error = 0;
2267 	uint32_t                                        ctl_unit                        = 0;
2268 	int                                                     error                           = 0;
2269 	struct flow_divert_group        *grp                            = NULL;
2270 	union sockaddr_in_4_6 local_endpoint = {};
2271 	union sockaddr_in_4_6 remote_endpoint = {};
2272 	int                                                     out_if_index            = 0;
2273 	uint32_t                                        send_window;
2274 	uint32_t                                        app_data_length         = 0;
2275 
2276 	memset(&local_endpoint, 0, sizeof(local_endpoint));
2277 	memset(&remote_endpoint, 0, sizeof(remote_endpoint));
2278 
2279 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_ERROR_CODE, sizeof(connect_error), &connect_error, NULL);
2280 	if (error) {
2281 		FDLOG(LOG_ERR, fd_cb, "failed to get the connect result: %d", error);
2282 		return;
2283 	}
2284 
2285 	connect_error = ntohl(connect_error);
2286 	FDLOG(LOG_INFO, fd_cb, "received connect result %u", connect_error);
2287 
2288 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_SPACE_AVAILABLE, sizeof(send_window), &send_window, NULL);
2289 	if (error) {
2290 		FDLOG(LOG_ERR, fd_cb, "failed to get the send window: %d", error);
2291 		return;
2292 	}
2293 
2294 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_CTL_UNIT, sizeof(ctl_unit), &ctl_unit, NULL);
2295 	if (error) {
2296 		FDLOG0(LOG_INFO, fd_cb, "No control unit provided in the connect result");
2297 	}
2298 
2299 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_LOCAL_ADDR, sizeof(local_endpoint), &(local_endpoint.sa), NULL);
2300 	if (error) {
2301 		FDLOG0(LOG_INFO, fd_cb, "No local address provided");
2302 	}
2303 
2304 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_REMOTE_ADDR, sizeof(remote_endpoint), &(remote_endpoint.sa), NULL);
2305 	if (error) {
2306 		FDLOG0(LOG_INFO, fd_cb, "No remote address provided");
2307 	}
2308 
2309 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_OUT_IF_INDEX, sizeof(out_if_index), &out_if_index, NULL);
2310 	if (error) {
2311 		FDLOG0(LOG_INFO, fd_cb, "No output if index provided");
2312 	}
2313 
2314 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_APP_DATA, 0, NULL, &app_data_length);
2315 	if (error) {
2316 		FDLOG0(LOG_INFO, fd_cb, "No application data provided in connect result");
2317 	}
2318 
2319 	error = 0;
2320 	ctl_unit                = ntohl(ctl_unit);
2321 
2322 	lck_rw_lock_shared(&g_flow_divert_group_lck);
2323 
2324 	if (connect_error == 0 && ctl_unit > 0) {
2325 		if (ctl_unit >= GROUP_COUNT_MAX) {
2326 			FDLOG(LOG_ERR, fd_cb, "Connect result contains an invalid control unit: %u", ctl_unit);
2327 			error = EINVAL;
2328 		} else if (g_flow_divert_groups == NULL || g_active_group_count == 0) {
2329 			FDLOG0(LOG_ERR, fd_cb, "No active groups, dropping connection");
2330 			error = EINVAL;
2331 		} else {
2332 			grp = g_flow_divert_groups[ctl_unit];
2333 			if (grp == NULL) {
2334 				error = ECONNRESET;
2335 			}
2336 		}
2337 	}
2338 
2339 	FDLOCK(fd_cb);
2340 	if (fd_cb->so != NULL) {
2341 		struct inpcb                            *inp = NULL;
2342 		struct flow_divert_group        *old_group;
2343 		struct socket *so = fd_cb->so;
2344 		bool local_address_is_valid = false;
2345 
2346 		socket_lock(so, 0);
2347 
2348 		if (!(so->so_flags & SOF_FLOW_DIVERT)) {
2349 			FDLOG0(LOG_NOTICE, fd_cb, "socket is not attached any more, ignoring connect result");
2350 			goto done;
2351 		}
2352 
2353 		if (SOCK_TYPE(so) == SOCK_STREAM && !(so->so_state & SS_ISCONNECTING)) {
2354 			FDLOG0(LOG_ERR, fd_cb, "TCP socket is not in the connecting state, ignoring connect result");
2355 			goto done;
2356 		}
2357 
2358 		inp = sotoinpcb(so);
2359 
2360 		if (connect_error || error) {
2361 			goto set_socket_state;
2362 		}
2363 
2364 		if (flow_divert_is_sockaddr_valid(&(local_endpoint.sa))) {
2365 			if (local_endpoint.sa.sa_family == AF_INET) {
2366 				local_endpoint.sa.sa_len = sizeof(struct sockaddr_in);
2367 				if ((inp->inp_vflag & INP_IPV4) && local_endpoint.sin.sin_addr.s_addr != INADDR_ANY) {
2368 					local_address_is_valid = true;
2369 					fd_cb->local_endpoint = local_endpoint;
2370 					inp->inp_laddr.s_addr = INADDR_ANY;
2371 				} else {
2372 					fd_cb->local_endpoint.sin.sin_port = local_endpoint.sin.sin_port;
2373 				}
2374 			} else if (local_endpoint.sa.sa_family == AF_INET6) {
2375 				local_endpoint.sa.sa_len = sizeof(struct sockaddr_in6);
2376 				if ((inp->inp_vflag & INP_IPV6) && !IN6_IS_ADDR_UNSPECIFIED(&local_endpoint.sin6.sin6_addr)) {
2377 					local_address_is_valid = true;
2378 					fd_cb->local_endpoint = local_endpoint;
2379 					inp->in6p_laddr = sa6_any.sin6_addr;
2380 				} else {
2381 					fd_cb->local_endpoint.sin6.sin6_port = local_endpoint.sin6.sin6_port;
2382 				}
2383 			}
2384 		}
2385 
2386 		flow_divert_scope(fd_cb, out_if_index, !local_address_is_valid);
2387 		flow_divert_set_local_endpoint(fd_cb, &(fd_cb->local_endpoint.sa));
2388 
2389 		if (flow_divert_is_sockaddr_valid(&(remote_endpoint.sa)) && SOCK_TYPE(so) == SOCK_STREAM) {
2390 			if (remote_endpoint.sa.sa_family == AF_INET) {
2391 				remote_endpoint.sa.sa_len = sizeof(struct sockaddr_in);
2392 			} else if (remote_endpoint.sa.sa_family == AF_INET6) {
2393 				remote_endpoint.sa.sa_len = sizeof(struct sockaddr_in6);
2394 			}
2395 			flow_divert_set_remote_endpoint(fd_cb, &(remote_endpoint.sa));
2396 		}
2397 
2398 		if (app_data_length > 0) {
2399 			uint8_t *app_data = NULL;
2400 			app_data = kalloc_data(app_data_length, Z_WAITOK);
2401 			if (app_data != NULL) {
2402 				error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_APP_DATA, app_data_length, app_data, NULL);
2403 				if (error == 0) {
2404 					FDLOG(LOG_INFO, fd_cb, "Got %u bytes of app data from the connect result", app_data_length);
2405 					if (fd_cb->app_data != NULL) {
2406 						kfree_data(fd_cb->app_data, fd_cb->app_data_length);
2407 					}
2408 					fd_cb->app_data = app_data;
2409 					fd_cb->app_data_length = app_data_length;
2410 				} else {
2411 					FDLOG(LOG_ERR, fd_cb, "Failed to copy %u bytes of application data from the connect result packet", app_data_length);
2412 					kfree_data(app_data, app_data_length);
2413 				}
2414 			} else {
2415 				FDLOG(LOG_ERR, fd_cb, "Failed to allocate a buffer of size %u to hold the application data from the connect result", app_data_length);
2416 			}
2417 		}
2418 
2419 		if (error) {
2420 			goto set_socket_state;
2421 		}
2422 
2423 		if (fd_cb->group == NULL) {
2424 			error = EINVAL;
2425 			goto set_socket_state;
2426 		}
2427 
2428 		if (grp != NULL) {
2429 			old_group = fd_cb->group;
2430 
2431 			lck_rw_lock_exclusive(&old_group->lck);
2432 			lck_rw_lock_exclusive(&grp->lck);
2433 
2434 			RB_REMOVE(fd_pcb_tree, &old_group->pcb_tree, fd_cb);
2435 			if (RB_INSERT(fd_pcb_tree, &grp->pcb_tree, fd_cb) != NULL) {
2436 				panic("group with unit %u already contains a connection with hash %u", grp->ctl_unit, fd_cb->hash);
2437 			}
2438 
2439 			fd_cb->group = grp;
2440 
2441 			lck_rw_done(&grp->lck);
2442 			lck_rw_done(&old_group->lck);
2443 		}
2444 
2445 		fd_cb->send_window = ntohl(send_window);
2446 
2447 set_socket_state:
2448 		if (!connect_error && !error) {
2449 			FDLOG0(LOG_INFO, fd_cb, "sending connect result");
2450 			error = flow_divert_send_connect_result(fd_cb);
2451 		}
2452 
2453 		if (connect_error || error) {
2454 			if (connect_error && fd_cb->control_group_unit != fd_cb->policy_control_unit) {
2455 				error = flow_divert_try_next(fd_cb);
2456 				if (error) {
2457 					flow_divert_disable(fd_cb);
2458 				}
2459 				goto done;
2460 			}
2461 
2462 			if (!connect_error) {
2463 				flow_divert_update_closed_state(fd_cb, SHUT_RDWR, FALSE);
2464 				so->so_error = (uint16_t)error;
2465 				flow_divert_send_close_if_needed(fd_cb);
2466 			} else {
2467 				flow_divert_update_closed_state(fd_cb, SHUT_RDWR, TRUE);
2468 				so->so_error = (uint16_t)connect_error;
2469 			}
2470 			flow_divert_disconnect_socket(so, !(fd_cb->flags & FLOW_DIVERT_IMPLICIT_CONNECT));
2471 		} else {
2472 #if NECP
2473 			/* Update NECP client with connected five-tuple */
2474 			if (!uuid_is_null(inp->necp_client_uuid)) {
2475 				socket_unlock(so, 0);
2476 				necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp);
2477 				socket_lock(so, 0);
2478 			}
2479 #endif /* NECP */
2480 
2481 			flow_divert_send_buffered_data(fd_cb, FALSE);
2482 			soisconnected(so);
2483 		}
2484 
2485 		/* We don't need the connect packet any more */
2486 		if (fd_cb->connect_packet != NULL) {
2487 			mbuf_freem(fd_cb->connect_packet);
2488 			fd_cb->connect_packet = NULL;
2489 		}
2490 
2491 		/* We don't need the original remote endpoint any more */
2492 		free_sockaddr(fd_cb->original_remote_endpoint);
2493 done:
2494 		socket_unlock(so, 0);
2495 	}
2496 	FDUNLOCK(fd_cb);
2497 
2498 	lck_rw_done(&g_flow_divert_group_lck);
2499 }
2500 
2501 static void
flow_divert_handle_close(struct flow_divert_pcb * fd_cb,mbuf_t packet,int offset)2502 flow_divert_handle_close(struct flow_divert_pcb *fd_cb, mbuf_t packet, int offset)
2503 {
2504 	uint32_t        close_error                     = 0;
2505 	int                     error                   = 0;
2506 	int                     how                     = 0;
2507 
2508 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_ERROR_CODE, sizeof(close_error), &close_error, NULL);
2509 	if (error) {
2510 		FDLOG(LOG_ERR, fd_cb, "failed to get the close error: %d", error);
2511 		return;
2512 	}
2513 
2514 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_HOW, sizeof(how), &how, NULL);
2515 	if (error) {
2516 		FDLOG(LOG_ERR, fd_cb, "failed to get the close how flag: %d", error);
2517 		return;
2518 	}
2519 
2520 	how = ntohl(how);
2521 
2522 	FDLOG(LOG_INFO, fd_cb, "close received, how = %d", how);
2523 
2524 	FDLOCK(fd_cb);
2525 	if (fd_cb->so != NULL) {
2526 		bool is_connected = (SOCK_TYPE(fd_cb->so) == SOCK_STREAM || !(fd_cb->flags & FLOW_DIVERT_IMPLICIT_CONNECT));
2527 		socket_lock(fd_cb->so, 0);
2528 
2529 		if (!(fd_cb->so->so_flags & SOF_FLOW_DIVERT)) {
2530 			FDLOG0(LOG_NOTICE, fd_cb, "socket is not attached any more, ignoring close from provider");
2531 			goto done;
2532 		}
2533 
2534 		fd_cb->so->so_error = (uint16_t)ntohl(close_error);
2535 
2536 		flow_divert_update_closed_state(fd_cb, how, TRUE);
2537 
2538 		/* Only do this for stream flows because "shutdown by peer" doesn't make sense for datagram flows */
2539 		how = flow_divert_tunnel_how_closed(fd_cb);
2540 		if (how == SHUT_RDWR) {
2541 			flow_divert_disconnect_socket(fd_cb->so, is_connected);
2542 		} else if (how == SHUT_RD && is_connected) {
2543 			socantrcvmore(fd_cb->so);
2544 		} else if (how == SHUT_WR && is_connected) {
2545 			socantsendmore(fd_cb->so);
2546 		}
2547 done:
2548 		socket_unlock(fd_cb->so, 0);
2549 	}
2550 	FDUNLOCK(fd_cb);
2551 }
2552 
2553 static mbuf_t
flow_divert_create_control_mbuf(struct flow_divert_pcb * fd_cb)2554 flow_divert_create_control_mbuf(struct flow_divert_pcb *fd_cb)
2555 {
2556 	struct inpcb *inp = sotoinpcb(fd_cb->so);
2557 	bool need_recvdstaddr = false;
2558 	/* Socket flow tracking needs to see the local address */
2559 	need_recvdstaddr = SOFLOW_ENABLED(inp->inp_socket);
2560 	if ((inp->inp_vflag & INP_IPV4) &&
2561 	    fd_cb->local_endpoint.sa.sa_family == AF_INET &&
2562 	    ((inp->inp_flags & INP_RECVDSTADDR) || need_recvdstaddr)) {
2563 		return sbcreatecontrol((caddr_t)&(fd_cb->local_endpoint.sin.sin_addr), sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
2564 	} else if ((inp->inp_vflag & INP_IPV6) &&
2565 	    fd_cb->local_endpoint.sa.sa_family == AF_INET6 &&
2566 	    ((inp->inp_flags & IN6P_PKTINFO) || need_recvdstaddr)) {
2567 		struct in6_pktinfo pi6;
2568 		memset(&pi6, 0, sizeof(pi6));
2569 		pi6.ipi6_addr = fd_cb->local_endpoint.sin6.sin6_addr;
2570 
2571 		return sbcreatecontrol((caddr_t)&pi6, sizeof(pi6), IPV6_PKTINFO, IPPROTO_IPV6);
2572 	}
2573 	return NULL;
2574 }
2575 
2576 static int
flow_divert_handle_data(struct flow_divert_pcb * fd_cb,mbuf_t packet,size_t offset)2577 flow_divert_handle_data(struct flow_divert_pcb *fd_cb, mbuf_t packet, size_t offset)
2578 {
2579 	int error = 0;
2580 
2581 	FDLOCK(fd_cb);
2582 	if (fd_cb->so != NULL) {
2583 		mbuf_t  data            = NULL;
2584 		size_t  data_size;
2585 		struct sockaddr_storage remote_address;
2586 		boolean_t got_remote_sa = FALSE;
2587 		boolean_t appended = FALSE;
2588 		boolean_t append_success = FALSE;
2589 
2590 		socket_lock(fd_cb->so, 0);
2591 
2592 		if (!(fd_cb->so->so_flags & SOF_FLOW_DIVERT)) {
2593 			FDLOG0(LOG_NOTICE, fd_cb, "socket is not attached any more, ignoring inbound data");
2594 			goto done;
2595 		}
2596 
2597 		if (sbspace(&fd_cb->so->so_rcv) == 0) {
2598 			error = ENOBUFS;
2599 			fd_cb->flags |= FLOW_DIVERT_NOTIFY_ON_RECEIVED;
2600 			FDLOG0(LOG_INFO, fd_cb, "Receive buffer is full, will send read notification when app reads some data");
2601 			goto done;
2602 		}
2603 
2604 		if (SOCK_TYPE(fd_cb->so) == SOCK_DGRAM) {
2605 			uint32_t val_size = 0;
2606 
2607 			/* check if we got remote address with data */
2608 			memset(&remote_address, 0, sizeof(remote_address));
2609 			error = flow_divert_packet_get_tlv(packet, (int)offset, FLOW_DIVERT_TLV_REMOTE_ADDR, sizeof(remote_address), &remote_address, &val_size);
2610 			if (error || val_size > sizeof(remote_address)) {
2611 				FDLOG0(LOG_INFO, fd_cb, "No remote address provided");
2612 				error = 0;
2613 			} else {
2614 				if (remote_address.ss_len > sizeof(remote_address)) {
2615 					remote_address.ss_len = sizeof(remote_address);
2616 				}
2617 				/* validate the address */
2618 				if (flow_divert_is_sockaddr_valid((struct sockaddr *)&remote_address)) {
2619 					got_remote_sa = TRUE;
2620 				} else {
2621 					FDLOG0(LOG_INFO, fd_cb, "Remote address is invalid");
2622 				}
2623 				offset += (sizeof(uint8_t) + sizeof(uint32_t) + val_size);
2624 			}
2625 		}
2626 
2627 		data_size = (mbuf_pkthdr_len(packet) - offset);
2628 
2629 		if (fd_cb->so->so_state & SS_CANTRCVMORE) {
2630 			FDLOG(LOG_NOTICE, fd_cb, "app cannot receive any more data, dropping %lu bytes of data", data_size);
2631 			goto done;
2632 		}
2633 
2634 		if (SOCK_TYPE(fd_cb->so) != SOCK_STREAM && SOCK_TYPE(fd_cb->so) != SOCK_DGRAM) {
2635 			FDLOG(LOG_ERR, fd_cb, "socket has an unsupported type: %d", SOCK_TYPE(fd_cb->so));
2636 			goto done;
2637 		}
2638 
2639 		FDLOG(LOG_DEBUG, fd_cb, "received %lu bytes of data", data_size);
2640 
2641 		error = mbuf_split(packet, offset, MBUF_DONTWAIT, &data);
2642 		if (error || data == NULL) {
2643 			FDLOG(LOG_ERR, fd_cb, "mbuf_split failed: %d", error);
2644 			goto done;
2645 		}
2646 
2647 		if (SOCK_TYPE(fd_cb->so) == SOCK_STREAM) {
2648 			appended = (sbappendstream(&fd_cb->so->so_rcv, data) != 0);
2649 			append_success = TRUE;
2650 		} else {
2651 			struct sockaddr *append_sa = NULL;
2652 			mbuf_t mctl;
2653 
2654 			if (got_remote_sa == TRUE) {
2655 				error = flow_divert_dup_addr(remote_address.ss_family, (struct sockaddr *)&remote_address, &append_sa);
2656 			} else {
2657 				if (fd_cb->so->so_proto->pr_domain->dom_family == AF_INET6) {
2658 					error = in6_mapped_peeraddr(fd_cb->so, &append_sa);
2659 				} else {
2660 					error = in_getpeeraddr(fd_cb->so, &append_sa);
2661 				}
2662 			}
2663 			if (error) {
2664 				FDLOG0(LOG_ERR, fd_cb, "failed to dup the socket address.");
2665 			}
2666 
2667 			mctl = flow_divert_create_control_mbuf(fd_cb);
2668 			int append_error = 0;
2669 			appended = sbappendaddr(&fd_cb->so->so_rcv, append_sa, data, mctl, &append_error);
2670 			if (appended || append_error == 0) {
2671 				append_success = TRUE;
2672 			} else {
2673 				FDLOG(LOG_ERR, fd_cb, "failed to append %lu bytes of data: %d", data_size, append_error);
2674 			}
2675 
2676 			free_sockaddr(append_sa);
2677 		}
2678 
2679 		if (append_success) {
2680 			fd_cb->bytes_received += data_size;
2681 			flow_divert_add_data_statistics(fd_cb, data_size, FALSE);
2682 		}
2683 
2684 		if (appended) {
2685 			sorwakeup(fd_cb->so);
2686 		}
2687 done:
2688 		socket_unlock(fd_cb->so, 0);
2689 	}
2690 	FDUNLOCK(fd_cb);
2691 
2692 	return error;
2693 }
2694 
2695 static void
flow_divert_handle_read_notification(struct flow_divert_pcb * fd_cb,mbuf_t packet,int offset)2696 flow_divert_handle_read_notification(struct flow_divert_pcb *fd_cb, mbuf_t packet, int offset)
2697 {
2698 	uint32_t        read_count              = 0;
2699 	int             error                   = 0;
2700 
2701 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_READ_COUNT, sizeof(read_count), &read_count, NULL);
2702 	if (error) {
2703 		FDLOG(LOG_ERR, fd_cb, "failed to get the read count: %d", error);
2704 		return;
2705 	}
2706 
2707 	FDLOG(LOG_DEBUG, fd_cb, "received a read notification for %u bytes", ntohl(read_count));
2708 
2709 	FDLOCK(fd_cb);
2710 	if (fd_cb->so != NULL) {
2711 		socket_lock(fd_cb->so, 0);
2712 
2713 		if (!(fd_cb->so->so_flags & SOF_FLOW_DIVERT)) {
2714 			FDLOG0(LOG_NOTICE, fd_cb, "socket is not attached any more, ignoring read notification");
2715 			goto done;
2716 		}
2717 
2718 		fd_cb->send_window += ntohl(read_count);
2719 		flow_divert_send_buffered_data(fd_cb, FALSE);
2720 done:
2721 		socket_unlock(fd_cb->so, 0);
2722 	}
2723 	FDUNLOCK(fd_cb);
2724 }
2725 
2726 static void
flow_divert_handle_group_init(struct flow_divert_group * group,mbuf_t packet,int offset)2727 flow_divert_handle_group_init(struct flow_divert_group *group, mbuf_t packet, int offset)
2728 {
2729 	int error         = 0;
2730 	uint32_t key_size = 0;
2731 	int log_level     = 0;
2732 	uint32_t flags    = 0;
2733 
2734 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_TOKEN_KEY, 0, NULL, &key_size);
2735 	if (error) {
2736 		FDLOG(LOG_ERR, &nil_pcb, "failed to get the key size: %d", error);
2737 		return;
2738 	}
2739 
2740 	if (key_size == 0 || key_size > FLOW_DIVERT_MAX_KEY_SIZE) {
2741 		FDLOG(LOG_ERR, &nil_pcb, "Invalid key size: %u", key_size);
2742 		return;
2743 	}
2744 
2745 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_LOG_LEVEL, sizeof(log_level), &log_level, NULL);
2746 	if (!error) {
2747 		nil_pcb.log_level = (uint8_t)log_level;
2748 	}
2749 
2750 	lck_rw_lock_exclusive(&group->lck);
2751 
2752 	if (group->token_key != NULL) {
2753 		kfree_data(group->token_key, group->token_key_size);
2754 		group->token_key = NULL;
2755 	}
2756 
2757 	group->token_key = kalloc_data(key_size, Z_WAITOK);
2758 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_TOKEN_KEY, key_size, group->token_key, NULL);
2759 	if (error) {
2760 		FDLOG(LOG_ERR, &nil_pcb, "failed to get the token key: %d", error);
2761 		kfree_data(group->token_key, key_size);
2762 		group->token_key = NULL;
2763 		lck_rw_done(&group->lck);
2764 		return;
2765 	}
2766 
2767 	group->token_key_size = key_size;
2768 
2769 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_FLAGS, sizeof(flags), &flags, NULL);
2770 	if (!error) {
2771 		group->flags = flags;
2772 	}
2773 
2774 	lck_rw_done(&group->lck);
2775 }
2776 
2777 static void
flow_divert_handle_properties_update(struct flow_divert_pcb * fd_cb,mbuf_t packet,int offset)2778 flow_divert_handle_properties_update(struct flow_divert_pcb *fd_cb, mbuf_t packet, int offset)
2779 {
2780 	int                                                     error                           = 0;
2781 	int                                                     out_if_index            = 0;
2782 	uint32_t                                        app_data_length         = 0;
2783 
2784 	FDLOG0(LOG_INFO, fd_cb, "received a properties update");
2785 
2786 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_OUT_IF_INDEX, sizeof(out_if_index), &out_if_index, NULL);
2787 	if (error) {
2788 		FDLOG0(LOG_INFO, fd_cb, "No output if index provided in properties update");
2789 	}
2790 
2791 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_APP_DATA, 0, NULL, &app_data_length);
2792 	if (error) {
2793 		FDLOG0(LOG_INFO, fd_cb, "No application data provided in properties update");
2794 	}
2795 
2796 	FDLOCK(fd_cb);
2797 	if (fd_cb->so != NULL) {
2798 		socket_lock(fd_cb->so, 0);
2799 
2800 		if (!(fd_cb->so->so_flags & SOF_FLOW_DIVERT)) {
2801 			FDLOG0(LOG_NOTICE, fd_cb, "socket is not attached any more, ignoring properties update");
2802 			goto done;
2803 		}
2804 
2805 		if (out_if_index > 0) {
2806 			flow_divert_scope(fd_cb, out_if_index, true);
2807 			flow_divert_set_local_endpoint(fd_cb, &(fd_cb->local_endpoint.sa));
2808 		}
2809 
2810 		if (app_data_length > 0) {
2811 			uint8_t *app_data = NULL;
2812 			app_data = kalloc_data(app_data_length, Z_WAITOK);
2813 			if (app_data != NULL) {
2814 				error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_APP_DATA, app_data_length, app_data, NULL);
2815 				if (error == 0) {
2816 					if (fd_cb->app_data != NULL) {
2817 						kfree_data(fd_cb->app_data, fd_cb->app_data_length);
2818 					}
2819 					fd_cb->app_data = app_data;
2820 					fd_cb->app_data_length = app_data_length;
2821 				} else {
2822 					FDLOG(LOG_ERR, fd_cb, "Failed to copy %u bytes of application data from the properties update packet", app_data_length);
2823 					kfree_data(app_data, app_data_length);
2824 				}
2825 			} else {
2826 				FDLOG(LOG_ERR, fd_cb, "Failed to allocate a buffer of size %u to hold the application data from the properties update", app_data_length);
2827 			}
2828 		}
2829 done:
2830 		socket_unlock(fd_cb->so, 0);
2831 	}
2832 	FDUNLOCK(fd_cb);
2833 }
2834 
2835 static void
flow_divert_handle_app_map_create(struct flow_divert_group * group,mbuf_t packet,int offset)2836 flow_divert_handle_app_map_create(struct flow_divert_group *group, mbuf_t packet, int offset)
2837 {
2838 	size_t bytes_mem_size;
2839 	size_t child_maps_mem_size;
2840 	size_t nodes_mem_size;
2841 	size_t trie_memory_size = 0;
2842 	int cursor;
2843 	int error = 0;
2844 	struct flow_divert_trie new_trie;
2845 	int insert_error = 0;
2846 	int prefix_count = -1;
2847 	int signing_id_count = 0;
2848 	size_t bytes_count = 0;
2849 	size_t nodes_count = 0;
2850 	size_t maps_count = 0;
2851 
2852 	lck_rw_lock_exclusive(&group->lck);
2853 
2854 	/* Re-set the current trie */
2855 	if (group->signing_id_trie.memory != NULL) {
2856 		kfree_data_addr(group->signing_id_trie.memory);
2857 	}
2858 	memset(&group->signing_id_trie, 0, sizeof(group->signing_id_trie));
2859 	group->signing_id_trie.root = NULL_TRIE_IDX;
2860 
2861 	memset(&new_trie, 0, sizeof(new_trie));
2862 
2863 	/* Get the number of shared prefixes in the new set of signing ID strings */
2864 	error = flow_divert_packet_get_tlv(packet, offset, FLOW_DIVERT_TLV_PREFIX_COUNT, sizeof(prefix_count), &prefix_count, NULL);
2865 
2866 	if (prefix_count < 0 || error) {
2867 		FDLOG(LOG_ERR, &nil_pcb, "Invalid prefix count (%d) or an error occurred while reading the prefix count: %d", prefix_count, error);
2868 		lck_rw_done(&group->lck);
2869 		return;
2870 	}
2871 
2872 	/* Compute the number of signing IDs and the total amount of bytes needed to store them */
2873 	for (cursor = flow_divert_packet_find_tlv(packet, offset, FLOW_DIVERT_TLV_SIGNING_ID, &error, 0);
2874 	    cursor >= 0;
2875 	    cursor = flow_divert_packet_find_tlv(packet, cursor, FLOW_DIVERT_TLV_SIGNING_ID, &error, 1)) {
2876 		uint32_t sid_size = 0;
2877 		error = flow_divert_packet_get_tlv(packet, cursor, FLOW_DIVERT_TLV_SIGNING_ID, 0, NULL, &sid_size);
2878 		if (error || sid_size == 0) {
2879 			FDLOG(LOG_ERR, &nil_pcb, "Failed to get the length of the signing identifier at offset %d: %d", cursor, error);
2880 			signing_id_count = 0;
2881 			break;
2882 		}
2883 		if (os_add_overflow(bytes_count, sid_size, &bytes_count)) {
2884 			FDLOG0(LOG_ERR, &nil_pcb, "Overflow while incrementing number of bytes");
2885 			signing_id_count = 0;
2886 			break;
2887 		}
2888 		signing_id_count++;
2889 	}
2890 
2891 	if (signing_id_count == 0) {
2892 		lck_rw_done(&group->lck);
2893 		FDLOG0(LOG_NOTICE, &nil_pcb, "No signing identifiers");
2894 		return;
2895 	}
2896 
2897 	if (os_add3_overflow(prefix_count, signing_id_count, 1, &nodes_count)) { /* + 1 for the root node */
2898 		lck_rw_done(&group->lck);
2899 		FDLOG0(LOG_ERR, &nil_pcb, "Overflow while computing the number of nodes");
2900 		return;
2901 	}
2902 
2903 	if (os_add_overflow(prefix_count, 1, &maps_count)) { /* + 1 for the root node */
2904 		lck_rw_done(&group->lck);
2905 		FDLOG0(LOG_ERR, &nil_pcb, "Overflow while computing the number of maps");
2906 		return;
2907 	}
2908 
2909 	if (bytes_count > UINT16_MAX || nodes_count > UINT16_MAX || maps_count > UINT16_MAX) {
2910 		lck_rw_done(&group->lck);
2911 		FDLOG(LOG_NOTICE, &nil_pcb, "Invalid bytes count (%lu), nodes count (%lu) or maps count (%lu)", bytes_count, nodes_count, maps_count);
2912 		return;
2913 	}
2914 
2915 	FDLOG(LOG_INFO, &nil_pcb, "Nodes count = %lu, child maps count = %lu, bytes_count = %lu",
2916 	    nodes_count, maps_count, bytes_count);
2917 
2918 	if (os_mul_overflow(sizeof(*new_trie.nodes), (size_t)nodes_count, &nodes_mem_size) ||
2919 	    os_mul3_overflow(sizeof(*new_trie.child_maps), CHILD_MAP_SIZE, (size_t)maps_count, &child_maps_mem_size) ||
2920 	    os_mul_overflow(sizeof(*new_trie.bytes), (size_t)bytes_count, &bytes_mem_size) ||
2921 	    os_add3_overflow(nodes_mem_size, child_maps_mem_size, bytes_mem_size, &trie_memory_size)) {
2922 		FDLOG0(LOG_ERR, &nil_pcb, "Overflow while computing trie memory sizes");
2923 		lck_rw_done(&group->lck);
2924 		return;
2925 	}
2926 
2927 	if (trie_memory_size > FLOW_DIVERT_MAX_TRIE_MEMORY) {
2928 		FDLOG(LOG_ERR, &nil_pcb, "Trie memory size (%lu) is too big (maximum is %u)", trie_memory_size, FLOW_DIVERT_MAX_TRIE_MEMORY);
2929 		lck_rw_done(&group->lck);
2930 		return;
2931 	}
2932 
2933 	new_trie.memory = kalloc_data(trie_memory_size, Z_WAITOK);
2934 	if (new_trie.memory == NULL) {
2935 		FDLOG(LOG_ERR, &nil_pcb, "Failed to allocate %lu bytes of memory for the signing ID trie",
2936 		    nodes_mem_size + child_maps_mem_size + bytes_mem_size);
2937 		lck_rw_done(&group->lck);
2938 		return;
2939 	}
2940 
2941 	new_trie.bytes_count = (uint16_t)bytes_count;
2942 	new_trie.nodes_count = (uint16_t)nodes_count;
2943 	new_trie.child_maps_count = (uint16_t)maps_count;
2944 
2945 	/* Initialize the free lists */
2946 	new_trie.nodes = (struct flow_divert_trie_node *)new_trie.memory;
2947 	new_trie.nodes_free_next = 0;
2948 	memset(new_trie.nodes, 0, nodes_mem_size);
2949 
2950 	new_trie.child_maps = (uint16_t *)(void *)((uint8_t *)new_trie.memory + nodes_mem_size);
2951 	new_trie.child_maps_free_next = 0;
2952 	memset(new_trie.child_maps, 0xff, child_maps_mem_size);
2953 
2954 	new_trie.bytes = (uint8_t *)(void *)((uint8_t *)new_trie.memory + nodes_mem_size + child_maps_mem_size);
2955 	new_trie.bytes_free_next = 0;
2956 	memset(new_trie.bytes, 0, bytes_mem_size);
2957 
2958 	/* The root is an empty node */
2959 	new_trie.root = trie_node_alloc(&new_trie);
2960 
2961 	/* Add each signing ID to the trie */
2962 	for (cursor = flow_divert_packet_find_tlv(packet, offset, FLOW_DIVERT_TLV_SIGNING_ID, &error, 0);
2963 	    cursor >= 0;
2964 	    cursor = flow_divert_packet_find_tlv(packet, cursor, FLOW_DIVERT_TLV_SIGNING_ID, &error, 1)) {
2965 		uint32_t sid_size = 0;
2966 		error = flow_divert_packet_get_tlv(packet, cursor, FLOW_DIVERT_TLV_SIGNING_ID, 0, NULL, &sid_size);
2967 		if (error || sid_size == 0) {
2968 			FDLOG(LOG_ERR, &nil_pcb, "Failed to get the length of the signing identifier at offset %d while building: %d", cursor, error);
2969 			insert_error = EINVAL;
2970 			break;
2971 		}
2972 		if (sid_size <= UINT16_MAX && new_trie.bytes_free_next + (uint16_t)sid_size <= new_trie.bytes_count) {
2973 			uint16_t new_node_idx;
2974 			error = flow_divert_packet_get_tlv(packet, cursor, FLOW_DIVERT_TLV_SIGNING_ID, sid_size, &TRIE_BYTE(&new_trie, new_trie.bytes_free_next), NULL);
2975 			if (error) {
2976 				FDLOG(LOG_ERR, &nil_pcb, "Failed to read the signing identifier at offset %d: %d", cursor, error);
2977 				insert_error = EINVAL;
2978 				break;
2979 			}
2980 			new_node_idx = flow_divert_trie_insert(&new_trie, new_trie.bytes_free_next, sid_size);
2981 			if (new_node_idx == NULL_TRIE_IDX) {
2982 				insert_error = EINVAL;
2983 				break;
2984 			}
2985 		} else {
2986 			FDLOG0(LOG_ERR, &nil_pcb, "No place to put signing ID for insertion");
2987 			insert_error = ENOBUFS;
2988 			break;
2989 		}
2990 	}
2991 
2992 	if (!insert_error) {
2993 		group->signing_id_trie = new_trie;
2994 	} else {
2995 		kfree_data(new_trie.memory, trie_memory_size);
2996 	}
2997 
2998 	lck_rw_done(&group->lck);
2999 }
3000 
3001 static int
flow_divert_input(mbuf_t packet,struct flow_divert_group * group)3002 flow_divert_input(mbuf_t packet, struct flow_divert_group *group)
3003 {
3004 	struct flow_divert_packet_header        hdr;
3005 	int                                                                     error           = 0;
3006 	struct flow_divert_pcb                          *fd_cb;
3007 
3008 	if (mbuf_pkthdr_len(packet) < sizeof(hdr)) {
3009 		FDLOG(LOG_ERR, &nil_pcb, "got a bad packet, length (%lu) < sizeof hdr (%lu)", mbuf_pkthdr_len(packet), sizeof(hdr));
3010 		error = EINVAL;
3011 		goto done;
3012 	}
3013 
3014 	error = mbuf_copydata(packet, 0, sizeof(hdr), &hdr);
3015 	if (error) {
3016 		FDLOG(LOG_ERR, &nil_pcb, "mbuf_copydata failed for the header: %d", error);
3017 		error = ENOBUFS;
3018 		goto done;
3019 	}
3020 
3021 	hdr.conn_id = ntohl(hdr.conn_id);
3022 
3023 	if (hdr.conn_id == 0) {
3024 		switch (hdr.packet_type) {
3025 		case FLOW_DIVERT_PKT_GROUP_INIT:
3026 			flow_divert_handle_group_init(group, packet, sizeof(hdr));
3027 			break;
3028 		case FLOW_DIVERT_PKT_APP_MAP_CREATE:
3029 			flow_divert_handle_app_map_create(group, packet, sizeof(hdr));
3030 			break;
3031 		default:
3032 			FDLOG(LOG_WARNING, &nil_pcb, "got an unknown message type: %d", hdr.packet_type);
3033 			break;
3034 		}
3035 		goto done;
3036 	}
3037 
3038 	fd_cb = flow_divert_pcb_lookup(hdr.conn_id, group);             /* This retains the PCB */
3039 	if (fd_cb == NULL) {
3040 		if (hdr.packet_type != FLOW_DIVERT_PKT_CLOSE && hdr.packet_type != FLOW_DIVERT_PKT_READ_NOTIFY) {
3041 			FDLOG(LOG_NOTICE, &nil_pcb, "got a %s message from group %d for an unknown pcb: %u", flow_divert_packet_type2str(hdr.packet_type), group->ctl_unit, hdr.conn_id);
3042 		}
3043 		goto done;
3044 	}
3045 
3046 	switch (hdr.packet_type) {
3047 	case FLOW_DIVERT_PKT_CONNECT_RESULT:
3048 		flow_divert_handle_connect_result(fd_cb, packet, sizeof(hdr));
3049 		break;
3050 	case FLOW_DIVERT_PKT_CLOSE:
3051 		flow_divert_handle_close(fd_cb, packet, sizeof(hdr));
3052 		break;
3053 	case FLOW_DIVERT_PKT_DATA:
3054 		error = flow_divert_handle_data(fd_cb, packet, sizeof(hdr));
3055 		break;
3056 	case FLOW_DIVERT_PKT_READ_NOTIFY:
3057 		flow_divert_handle_read_notification(fd_cb, packet, sizeof(hdr));
3058 		break;
3059 	case FLOW_DIVERT_PKT_PROPERTIES_UPDATE:
3060 		flow_divert_handle_properties_update(fd_cb, packet, sizeof(hdr));
3061 		break;
3062 	default:
3063 		FDLOG(LOG_WARNING, fd_cb, "got an unknown message type: %d", hdr.packet_type);
3064 		break;
3065 	}
3066 
3067 	FDRELEASE(fd_cb);
3068 
3069 done:
3070 	mbuf_freem(packet);
3071 	return error;
3072 }
3073 
3074 static void
flow_divert_close_all(struct flow_divert_group * group)3075 flow_divert_close_all(struct flow_divert_group *group)
3076 {
3077 	struct flow_divert_pcb                  *fd_cb;
3078 	SLIST_HEAD(, flow_divert_pcb)   tmp_list;
3079 
3080 	SLIST_INIT(&tmp_list);
3081 
3082 	lck_rw_lock_exclusive(&group->lck);
3083 
3084 	MBUFQ_DRAIN(&group->send_queue);
3085 
3086 	RB_FOREACH(fd_cb, fd_pcb_tree, &group->pcb_tree) {
3087 		FDRETAIN(fd_cb);
3088 		SLIST_INSERT_HEAD(&tmp_list, fd_cb, tmp_list_entry);
3089 	}
3090 
3091 	lck_rw_done(&group->lck);
3092 
3093 	while (!SLIST_EMPTY(&tmp_list)) {
3094 		fd_cb = SLIST_FIRST(&tmp_list);
3095 		FDLOCK(fd_cb);
3096 		SLIST_REMOVE_HEAD(&tmp_list, tmp_list_entry);
3097 		if (fd_cb->so != NULL) {
3098 			socket_lock(fd_cb->so, 0);
3099 			flow_divert_pcb_remove(fd_cb);
3100 			flow_divert_update_closed_state(fd_cb, SHUT_RDWR, TRUE);
3101 			fd_cb->so->so_error = ECONNABORTED;
3102 			flow_divert_disconnect_socket(fd_cb->so, !(fd_cb->flags & FLOW_DIVERT_IMPLICIT_CONNECT));
3103 			socket_unlock(fd_cb->so, 0);
3104 		}
3105 		FDUNLOCK(fd_cb);
3106 		FDRELEASE(fd_cb);
3107 	}
3108 }
3109 
3110 void
flow_divert_detach(struct socket * so)3111 flow_divert_detach(struct socket *so)
3112 {
3113 	struct flow_divert_pcb  *fd_cb          = so->so_fd_pcb;
3114 
3115 	VERIFY((so->so_flags & SOF_FLOW_DIVERT) && so->so_fd_pcb != NULL);
3116 
3117 	so->so_flags &= ~SOF_FLOW_DIVERT;
3118 	so->so_fd_pcb = NULL;
3119 
3120 	FDLOG(LOG_INFO, fd_cb, "Detaching, ref count = %d", fd_cb->ref_count);
3121 
3122 	if (fd_cb->group != NULL) {
3123 		/* Last-ditch effort to send any buffered data */
3124 		flow_divert_send_buffered_data(fd_cb, TRUE);
3125 
3126 		flow_divert_update_closed_state(fd_cb, SHUT_RDWR, FALSE);
3127 		flow_divert_send_close_if_needed(fd_cb);
3128 		/* Remove from the group */
3129 		flow_divert_pcb_remove(fd_cb);
3130 	}
3131 
3132 	socket_unlock(so, 0);
3133 	FDLOCK(fd_cb);
3134 	fd_cb->so = NULL;
3135 	FDUNLOCK(fd_cb);
3136 	socket_lock(so, 0);
3137 
3138 	FDRELEASE(fd_cb);       /* Release the socket's reference */
3139 }
3140 
3141 static int
flow_divert_close(struct socket * so)3142 flow_divert_close(struct socket *so)
3143 {
3144 	struct flow_divert_pcb  *fd_cb          = so->so_fd_pcb;
3145 
3146 	VERIFY((so->so_flags & SOF_FLOW_DIVERT) && so->so_fd_pcb != NULL);
3147 
3148 	FDLOG0(LOG_INFO, fd_cb, "Closing");
3149 
3150 	if (SOCK_TYPE(so) == SOCK_STREAM) {
3151 		soisdisconnecting(so);
3152 		sbflush(&so->so_rcv);
3153 	}
3154 
3155 	flow_divert_send_buffered_data(fd_cb, TRUE);
3156 	flow_divert_update_closed_state(fd_cb, SHUT_RDWR, FALSE);
3157 	flow_divert_send_close_if_needed(fd_cb);
3158 
3159 	/* Remove from the group */
3160 	flow_divert_pcb_remove(fd_cb);
3161 
3162 	return 0;
3163 }
3164 
3165 static int
flow_divert_disconnectx(struct socket * so,sae_associd_t aid,sae_connid_t cid __unused)3166 flow_divert_disconnectx(struct socket *so, sae_associd_t aid,
3167     sae_connid_t cid __unused)
3168 {
3169 	if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL) {
3170 		return EINVAL;
3171 	}
3172 
3173 	return flow_divert_close(so);
3174 }
3175 
3176 static int
flow_divert_shutdown(struct socket * so)3177 flow_divert_shutdown(struct socket *so)
3178 {
3179 	struct flow_divert_pcb  *fd_cb          = so->so_fd_pcb;
3180 
3181 	VERIFY((so->so_flags & SOF_FLOW_DIVERT) && so->so_fd_pcb != NULL);
3182 
3183 	FDLOG0(LOG_INFO, fd_cb, "Can't send more");
3184 
3185 	socantsendmore(so);
3186 
3187 	flow_divert_update_closed_state(fd_cb, SHUT_WR, FALSE);
3188 	flow_divert_send_close_if_needed(fd_cb);
3189 
3190 	return 0;
3191 }
3192 
3193 static int
flow_divert_rcvd(struct socket * so,int flags __unused)3194 flow_divert_rcvd(struct socket *so, int flags __unused)
3195 {
3196 	struct flow_divert_pcb  *fd_cb = so->so_fd_pcb;
3197 	int space = sbspace(&so->so_rcv);
3198 
3199 	VERIFY((so->so_flags & SOF_FLOW_DIVERT) && so->so_fd_pcb != NULL);
3200 
3201 	FDLOG(LOG_DEBUG, fd_cb, "app read bytes, space = %d", space);
3202 	if ((fd_cb->flags & FLOW_DIVERT_NOTIFY_ON_RECEIVED) &&
3203 	    (space > 0) &&
3204 	    flow_divert_send_read_notification(fd_cb) == 0) {
3205 		FDLOG0(LOG_INFO, fd_cb, "Sent a read notification");
3206 		fd_cb->flags &= ~FLOW_DIVERT_NOTIFY_ON_RECEIVED;
3207 	}
3208 
3209 	return 0;
3210 }
3211 
3212 static int
flow_divert_append_target_endpoint_tlv(mbuf_t connect_packet,struct sockaddr * toaddr)3213 flow_divert_append_target_endpoint_tlv(mbuf_t connect_packet, struct sockaddr *toaddr)
3214 {
3215 	int error = 0;
3216 	int port  = 0;
3217 
3218 	if (!flow_divert_is_sockaddr_valid(toaddr)) {
3219 		FDLOG(LOG_ERR, &nil_pcb, "Invalid target address, family = %u, length = %u", toaddr->sa_family, toaddr->sa_len);
3220 		error = EINVAL;
3221 		goto done;
3222 	}
3223 
3224 	error = flow_divert_packet_append_tlv(connect_packet, FLOW_DIVERT_TLV_TARGET_ADDRESS, toaddr->sa_len, toaddr);
3225 	if (error) {
3226 		goto done;
3227 	}
3228 
3229 	if (toaddr->sa_family == AF_INET) {
3230 		port = ntohs((satosin(toaddr))->sin_port);
3231 	} else {
3232 		port = ntohs((satosin6(toaddr))->sin6_port);
3233 	}
3234 
3235 	error = flow_divert_packet_append_tlv(connect_packet, FLOW_DIVERT_TLV_TARGET_PORT, sizeof(port), &port);
3236 	if (error) {
3237 		goto done;
3238 	}
3239 
3240 done:
3241 	return error;
3242 }
3243 
3244 struct sockaddr *
flow_divert_get_buffered_target_address(mbuf_t buffer)3245 flow_divert_get_buffered_target_address(mbuf_t buffer)
3246 {
3247 	if (buffer != NULL && buffer->m_type == MT_SONAME) {
3248 		struct sockaddr *toaddr = mtod(buffer, struct sockaddr *);
3249 		if (toaddr != NULL && flow_divert_is_sockaddr_valid(toaddr)) {
3250 			return toaddr;
3251 		}
3252 	}
3253 	return NULL;
3254 }
3255 
3256 static boolean_t
flow_divert_is_sockaddr_valid(struct sockaddr * addr)3257 flow_divert_is_sockaddr_valid(struct sockaddr *addr)
3258 {
3259 	switch (addr->sa_family) {
3260 	case AF_INET:
3261 		if (addr->sa_len < sizeof(struct sockaddr_in)) {
3262 			return FALSE;
3263 		}
3264 		break;
3265 	case AF_INET6:
3266 		if (addr->sa_len < sizeof(struct sockaddr_in6)) {
3267 			return FALSE;
3268 		}
3269 		break;
3270 	default:
3271 		return FALSE;
3272 	}
3273 	return TRUE;
3274 }
3275 
3276 static errno_t
flow_divert_dup_addr(sa_family_t family,struct sockaddr * addr,struct sockaddr ** dup)3277 flow_divert_dup_addr(sa_family_t family, struct sockaddr *addr,
3278     struct sockaddr **dup)
3279 {
3280 	int                                             error           = 0;
3281 	struct sockaddr                 *result;
3282 	struct sockaddr_storage ss;
3283 
3284 	if (addr != NULL) {
3285 		result = addr;
3286 	} else {
3287 		memset(&ss, 0, sizeof(ss));
3288 		ss.ss_family = family;
3289 		if (ss.ss_family == AF_INET) {
3290 			ss.ss_len = sizeof(struct sockaddr_in);
3291 		} else if (ss.ss_family == AF_INET6) {
3292 			ss.ss_len = sizeof(struct sockaddr_in6);
3293 		} else {
3294 			error = EINVAL;
3295 		}
3296 		result = (struct sockaddr *)&ss;
3297 	}
3298 
3299 	if (!error) {
3300 		*dup = dup_sockaddr(result, 1);
3301 		if (*dup == NULL) {
3302 			error = ENOBUFS;
3303 		}
3304 	}
3305 
3306 	return error;
3307 }
3308 
3309 static void
flow_divert_disconnect_socket(struct socket * so,bool is_connected)3310 flow_divert_disconnect_socket(struct socket *so, bool is_connected)
3311 {
3312 	if (SOCK_TYPE(so) == SOCK_STREAM || is_connected) {
3313 		soisdisconnected(so);
3314 	}
3315 	if (SOCK_TYPE(so) == SOCK_DGRAM) {
3316 		struct inpcb *inp = sotoinpcb(so);
3317 		if (inp != NULL && !(so->so_flags & SOF_PCBCLEARING)) {
3318 			/*
3319 			 * Let NetworkStatistics know this PCB is going away
3320 			 * before we detach it.
3321 			 */
3322 			if (nstat_collect && (SOCK_PROTO(so) == IPPROTO_TCP || SOCK_PROTO(so) == IPPROTO_UDP)) {
3323 				nstat_pcb_detach(inp);
3324 			}
3325 
3326 			if (SOCK_DOM(so) == PF_INET6) {
3327 				ROUTE_RELEASE(&inp->in6p_route);
3328 			} else {
3329 				ROUTE_RELEASE(&inp->inp_route);
3330 			}
3331 			inp->inp_state = INPCB_STATE_DEAD;
3332 			/* makes sure we're not called twice from so_close */
3333 			so->so_flags |= SOF_PCBCLEARING;
3334 			inpcb_gc_sched(inp->inp_pcbinfo, INPCB_TIMER_FAST);
3335 		}
3336 	}
3337 }
3338 
3339 static errno_t
flow_divert_ctloutput(struct socket * so,struct sockopt * sopt)3340 flow_divert_ctloutput(struct socket *so, struct sockopt *sopt)
3341 {
3342 	struct flow_divert_pcb  *fd_cb  = so->so_fd_pcb;
3343 
3344 	VERIFY((so->so_flags & SOF_FLOW_DIVERT) && so->so_fd_pcb != NULL);
3345 
3346 	if (sopt->sopt_name == SO_TRAFFIC_CLASS) {
3347 		if (sopt->sopt_dir == SOPT_SET && fd_cb->flags & FLOW_DIVERT_CONNECT_STARTED) {
3348 			flow_divert_send_traffic_class_update(fd_cb, so->so_traffic_class);
3349 		}
3350 	}
3351 
3352 	if (SOCK_DOM(so) == PF_INET) {
3353 		return g_tcp_protosw->pr_ctloutput(so, sopt);
3354 	} else if (SOCK_DOM(so) == PF_INET6) {
3355 		return g_tcp6_protosw->pr_ctloutput(so, sopt);
3356 	}
3357 	return 0;
3358 }
3359 
3360 static errno_t
flow_divert_connect_out_internal(struct socket * so,struct sockaddr * to,proc_t p,bool implicit)3361 flow_divert_connect_out_internal(struct socket *so, struct sockaddr *to, proc_t p, bool implicit)
3362 {
3363 	struct flow_divert_pcb  *fd_cb  = so->so_fd_pcb;
3364 	int                                             error   = 0;
3365 	struct inpcb                    *inp    = sotoinpcb(so);
3366 	struct sockaddr_in              *sinp;
3367 	mbuf_t                                  connect_packet = NULL;
3368 	int                                             do_send = 1;
3369 
3370 	VERIFY((so->so_flags & SOF_FLOW_DIVERT) && so->so_fd_pcb != NULL);
3371 
3372 	if (fd_cb->group == NULL) {
3373 		error = ENETUNREACH;
3374 		goto done;
3375 	}
3376 
3377 	if (inp == NULL) {
3378 		error = EINVAL;
3379 		goto done;
3380 	} else if (inp->inp_state == INPCB_STATE_DEAD) {
3381 		if (so->so_error) {
3382 			error = so->so_error;
3383 			so->so_error = 0;
3384 		} else {
3385 			error = EINVAL;
3386 		}
3387 		goto done;
3388 	}
3389 
3390 	if (fd_cb->flags & FLOW_DIVERT_CONNECT_STARTED) {
3391 		error = EALREADY;
3392 		goto done;
3393 	}
3394 
3395 	FDLOG0(LOG_INFO, fd_cb, "Connecting");
3396 
3397 	if (fd_cb->connect_packet == NULL) {
3398 		struct sockaddr_in sin = {};
3399 		struct ifnet *ifp = NULL;
3400 
3401 		if (to == NULL) {
3402 			FDLOG0(LOG_ERR, fd_cb, "No destination address available when creating connect packet");
3403 			error = EINVAL;
3404 			goto done;
3405 		}
3406 
3407 		fd_cb->original_remote_endpoint = dup_sockaddr(to, 0);
3408 		if (fd_cb->original_remote_endpoint == NULL) {
3409 			FDLOG0(LOG_ERR, fd_cb, "Failed to dup the remote endpoint");
3410 			error = ENOMEM;
3411 			goto done;
3412 		}
3413 		fd_cb->original_vflag = inp->inp_vflag;
3414 		fd_cb->original_last_outifp = inp->inp_last_outifp;
3415 		fd_cb->original_last_outifp6 = inp->in6p_last_outifp;
3416 
3417 		sinp = (struct sockaddr_in *)(void *)to;
3418 		if (sinp->sin_family == AF_INET && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
3419 			error = EAFNOSUPPORT;
3420 			goto done;
3421 		}
3422 
3423 		if (to->sa_family == AF_INET6 && !(inp->inp_flags & IN6P_IPV6_V6ONLY)) {
3424 			struct sockaddr_in6 sin6 = {};
3425 			sin6.sin6_family = AF_INET6;
3426 			sin6.sin6_len = sizeof(struct sockaddr_in6);
3427 			sin6.sin6_port = satosin6(to)->sin6_port;
3428 			sin6.sin6_addr = satosin6(to)->sin6_addr;
3429 			if (IN6_IS_ADDR_V4MAPPED(&(sin6.sin6_addr))) {
3430 				in6_sin6_2_sin(&sin, &sin6);
3431 				to = (struct sockaddr *)&sin;
3432 			}
3433 		}
3434 
3435 		if (to->sa_family == AF_INET6) {
3436 			inp->inp_vflag &= ~INP_IPV4;
3437 			inp->inp_vflag |= INP_IPV6;
3438 			fd_cb->local_endpoint.sin6.sin6_len = sizeof(struct sockaddr_in6);
3439 			fd_cb->local_endpoint.sin6.sin6_family = AF_INET6;
3440 			fd_cb->local_endpoint.sin6.sin6_port = inp->inp_lport;
3441 			error = in6_pcbladdr(inp, to, &(fd_cb->local_endpoint.sin6.sin6_addr), &ifp);
3442 			if (error) {
3443 				FDLOG(LOG_WARNING, fd_cb, "failed to get a local IPv6 address: %d", error);
3444 				if (!(fd_cb->flags & FLOW_DIVERT_FLOW_IS_TRANSPARENT) || IN6_IS_ADDR_UNSPECIFIED(&(satosin6(to)->sin6_addr))) {
3445 					error = 0;
3446 				} else {
3447 					goto done;
3448 				}
3449 			}
3450 			if (ifp != NULL) {
3451 				inp->in6p_last_outifp = ifp;
3452 				ifnet_release(ifp);
3453 			}
3454 		} else if (to->sa_family == AF_INET) {
3455 			inp->inp_vflag |= INP_IPV4;
3456 			inp->inp_vflag &= ~INP_IPV6;
3457 			fd_cb->local_endpoint.sin.sin_len = sizeof(struct sockaddr_in);
3458 			fd_cb->local_endpoint.sin.sin_family = AF_INET;
3459 			fd_cb->local_endpoint.sin.sin_port = inp->inp_lport;
3460 			error = in_pcbladdr(inp, to, &(fd_cb->local_endpoint.sin.sin_addr), IFSCOPE_NONE, &ifp, 0);
3461 			if (error) {
3462 				FDLOG(LOG_WARNING, fd_cb, "failed to get a local IPv4 address: %d", error);
3463 				if (!(fd_cb->flags & FLOW_DIVERT_FLOW_IS_TRANSPARENT) || satosin(to)->sin_addr.s_addr == INADDR_ANY) {
3464 					error = 0;
3465 				} else {
3466 					goto done;
3467 				}
3468 			}
3469 			if (ifp != NULL) {
3470 				inp->inp_last_outifp = ifp;
3471 				ifnet_release(ifp);
3472 			}
3473 		} else {
3474 			FDLOG(LOG_WARNING, fd_cb, "target address has an unsupported family: %d", to->sa_family);
3475 		}
3476 
3477 		error = flow_divert_check_no_cellular(fd_cb) ||
3478 		    flow_divert_check_no_expensive(fd_cb) ||
3479 		    flow_divert_check_no_constrained(fd_cb);
3480 		if (error) {
3481 			goto done;
3482 		}
3483 
3484 		if (SOCK_TYPE(so) == SOCK_STREAM || /* TCP or */
3485 		    !implicit || /* connect() was called or */
3486 		    ((inp->inp_vflag & INP_IPV6) && !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) || /* local address is not un-specified */
3487 		    ((inp->inp_vflag & INP_IPV4) && inp->inp_laddr.s_addr != INADDR_ANY)) {
3488 			fd_cb->flags |= FLOW_DIVERT_SHOULD_SET_LOCAL_ADDR;
3489 		}
3490 
3491 		error = flow_divert_create_connect_packet(fd_cb, to, so, p, &connect_packet);
3492 		if (error) {
3493 			goto done;
3494 		}
3495 
3496 		if (!implicit || SOCK_TYPE(so) == SOCK_STREAM) {
3497 			flow_divert_set_remote_endpoint(fd_cb, to);
3498 			flow_divert_set_local_endpoint(fd_cb, &(fd_cb->local_endpoint.sa));
3499 		}
3500 
3501 		if (implicit) {
3502 			fd_cb->flags |= FLOW_DIVERT_IMPLICIT_CONNECT;
3503 		}
3504 
3505 		if (so->so_flags1 & SOF1_PRECONNECT_DATA) {
3506 			FDLOG0(LOG_INFO, fd_cb, "Delaying sending the connect packet until send or receive");
3507 			do_send = 0;
3508 		}
3509 
3510 		fd_cb->connect_packet = connect_packet;
3511 		connect_packet = NULL;
3512 	} else {
3513 		FDLOG0(LOG_INFO, fd_cb, "Sending saved connect packet");
3514 	}
3515 
3516 	if (do_send) {
3517 		error = flow_divert_send_connect_packet(fd_cb);
3518 		if (error) {
3519 			goto done;
3520 		}
3521 
3522 		fd_cb->flags |= FLOW_DIVERT_CONNECT_STARTED;
3523 	}
3524 
3525 	if (SOCK_TYPE(so) == SOCK_DGRAM && !(fd_cb->flags & FLOW_DIVERT_HAS_TOKEN)) {
3526 		soisconnected(so);
3527 	} else {
3528 		soisconnecting(so);
3529 	}
3530 
3531 done:
3532 	return error;
3533 }
3534 
3535 errno_t
flow_divert_connect_out(struct socket * so,struct sockaddr * to,proc_t p)3536 flow_divert_connect_out(struct socket *so, struct sockaddr *to, proc_t p)
3537 {
3538 #if CONTENT_FILTER
3539 	if (SOCK_TYPE(so) == SOCK_STREAM && !(so->so_flags & SOF_CONTENT_FILTER)) {
3540 		int error = cfil_sock_attach(so, NULL, to, CFS_CONNECTION_DIR_OUT);
3541 		if (error != 0) {
3542 			struct flow_divert_pcb  *fd_cb  = so->so_fd_pcb;
3543 			FDLOG(LOG_ERR, fd_cb, "Failed to attach cfil: %d", error);
3544 			return error;
3545 		}
3546 	}
3547 #endif /* CONTENT_FILTER */
3548 
3549 	return flow_divert_connect_out_internal(so, to, p, false);
3550 }
3551 
3552 static int
flow_divert_connectx_out_common(struct socket * so,struct sockaddr * dst,struct proc * p,sae_connid_t * pcid,struct uio * auio,user_ssize_t * bytes_written)3553 flow_divert_connectx_out_common(struct socket *so, struct sockaddr *dst,
3554     struct proc *p, sae_connid_t *pcid, struct uio *auio, user_ssize_t *bytes_written)
3555 {
3556 	struct inpcb *inp = sotoinpcb(so);
3557 	int error;
3558 
3559 	if (inp == NULL) {
3560 		return EINVAL;
3561 	}
3562 
3563 	VERIFY(dst != NULL);
3564 
3565 #if CONTENT_FILTER && NECP
3566 	struct flow_divert_pcb *fd_cb = so->so_fd_pcb;
3567 	if (fd_cb != NULL && (fd_cb->flags & FLOW_DIVERT_HAS_TOKEN) &&
3568 	    SOCK_TYPE(so) == SOCK_STREAM && !(so->so_flags & SOF_CONTENT_FILTER)) {
3569 		inp_update_necp_policy(sotoinpcb(so), NULL, dst, 0);
3570 	}
3571 #endif /* CONTENT_FILTER */
3572 
3573 	error = flow_divert_connect_out(so, dst, p);
3574 
3575 	if (error != 0) {
3576 		return error;
3577 	}
3578 
3579 	/* if there is data, send it */
3580 	if (auio != NULL) {
3581 		user_ssize_t datalen = 0;
3582 
3583 		socket_unlock(so, 0);
3584 
3585 		VERIFY(bytes_written != NULL);
3586 
3587 		datalen = uio_resid(auio);
3588 		error = so->so_proto->pr_usrreqs->pru_sosend(so, NULL, (uio_t)auio, NULL, NULL, 0);
3589 		socket_lock(so, 0);
3590 
3591 		if (error == 0 || error == EWOULDBLOCK) {
3592 			*bytes_written = datalen - uio_resid(auio);
3593 		}
3594 
3595 		/*
3596 		 * sosend returns EWOULDBLOCK if it's a non-blocking
3597 		 * socket or a timeout occured (this allows to return
3598 		 * the amount of queued data through sendit()).
3599 		 *
3600 		 * However, connectx() returns EINPROGRESS in case of a
3601 		 * blocking socket. So we change the return value here.
3602 		 */
3603 		if (error == EWOULDBLOCK) {
3604 			error = EINPROGRESS;
3605 		}
3606 	}
3607 
3608 	if (error == 0 && pcid != NULL) {
3609 		*pcid = 1;      /* there is only 1 connection for a TCP */
3610 	}
3611 
3612 	return error;
3613 }
3614 
3615 static int
flow_divert_connectx_out(struct socket * so,struct sockaddr * src __unused,struct sockaddr * dst,struct proc * p,uint32_t ifscope __unused,sae_associd_t aid __unused,sae_connid_t * pcid,uint32_t flags __unused,void * arg __unused,uint32_t arglen __unused,struct uio * uio,user_ssize_t * bytes_written)3616 flow_divert_connectx_out(struct socket *so, struct sockaddr *src __unused,
3617     struct sockaddr *dst, struct proc *p, uint32_t ifscope __unused,
3618     sae_associd_t aid __unused, sae_connid_t *pcid, uint32_t flags __unused, void *arg __unused,
3619     uint32_t arglen __unused, struct uio *uio, user_ssize_t *bytes_written)
3620 {
3621 	return flow_divert_connectx_out_common(so, dst, p, pcid, uio, bytes_written);
3622 }
3623 
3624 static int
flow_divert_connectx6_out(struct socket * so,struct sockaddr * src __unused,struct sockaddr * dst,struct proc * p,uint32_t ifscope __unused,sae_associd_t aid __unused,sae_connid_t * pcid,uint32_t flags __unused,void * arg __unused,uint32_t arglen __unused,struct uio * uio,user_ssize_t * bytes_written)3625 flow_divert_connectx6_out(struct socket *so, struct sockaddr *src __unused,
3626     struct sockaddr *dst, struct proc *p, uint32_t ifscope __unused,
3627     sae_associd_t aid __unused, sae_connid_t *pcid, uint32_t flags __unused, void *arg __unused,
3628     uint32_t arglen __unused, struct uio *uio, user_ssize_t *bytes_written)
3629 {
3630 	return flow_divert_connectx_out_common(so, dst, p, pcid, uio, bytes_written);
3631 }
3632 
3633 static errno_t
flow_divert_data_out(struct socket * so,int flags,mbuf_t data,struct sockaddr * to,mbuf_t control,struct proc * p)3634 flow_divert_data_out(struct socket *so, int flags, mbuf_t data, struct sockaddr *to, mbuf_t control, struct proc *p)
3635 {
3636 	struct flow_divert_pcb  *fd_cb  = so->so_fd_pcb;
3637 	int                                             error   = 0;
3638 	struct inpcb *inp;
3639 #if CONTENT_FILTER
3640 	struct m_tag *cfil_tag = NULL;
3641 #endif
3642 
3643 	VERIFY((so->so_flags & SOF_FLOW_DIVERT) && so->so_fd_pcb != NULL);
3644 
3645 	inp = sotoinpcb(so);
3646 	if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) {
3647 		error = ECONNRESET;
3648 		goto done;
3649 	}
3650 
3651 	if (control && mbuf_len(control) > 0) {
3652 		error = EINVAL;
3653 		goto done;
3654 	}
3655 
3656 	if (flags & MSG_OOB) {
3657 		error = EINVAL;
3658 		goto done; /* We don't support OOB data */
3659 	}
3660 
3661 	if ((fd_cb->flags & FLOW_DIVERT_TUNNEL_WR_CLOSED) && SOCK_TYPE(so) == SOCK_DGRAM) {
3662 		/* The provider considers this datagram flow to be closed, so no data can be sent */
3663 		FDLOG0(LOG_INFO, fd_cb, "provider is no longer accepting writes, cannot send data");
3664 		error = EHOSTUNREACH;
3665 		goto done;
3666 	}
3667 
3668 #if CONTENT_FILTER
3669 	/*
3670 	 * If the socket is subject to a UDP Content Filter and no remote address is passed in,
3671 	 * retrieve the CFIL saved remote address from the mbuf and use it.
3672 	 */
3673 	if (to == NULL && CFIL_DGRAM_FILTERED(so)) {
3674 		struct sockaddr *cfil_faddr = NULL;
3675 		cfil_tag = cfil_dgram_get_socket_state(data, NULL, NULL, &cfil_faddr, NULL);
3676 		if (cfil_tag) {
3677 			to = (struct sockaddr *)(void *)cfil_faddr;
3678 		}
3679 		FDLOG(LOG_INFO, fd_cb, "Using remote address from CFIL saved state: %p", to);
3680 	}
3681 #endif
3682 
3683 	/* Implicit connect */
3684 	if (!(fd_cb->flags & FLOW_DIVERT_CONNECT_STARTED)) {
3685 		FDLOG0(LOG_INFO, fd_cb, "implicit connect");
3686 
3687 		error = flow_divert_connect_out_internal(so, to, p, true);
3688 		if (error) {
3689 			goto done;
3690 		}
3691 	} else {
3692 		error = flow_divert_check_no_cellular(fd_cb) ||
3693 		    flow_divert_check_no_expensive(fd_cb) ||
3694 		    flow_divert_check_no_constrained(fd_cb);
3695 		if (error) {
3696 			goto done;
3697 		}
3698 	}
3699 
3700 	FDLOG(LOG_DEBUG, fd_cb, "app wrote %lu bytes", mbuf_pkthdr_len(data));
3701 
3702 	fd_cb->bytes_written_by_app += mbuf_pkthdr_len(data);
3703 	error = flow_divert_send_app_data(fd_cb, data, to);
3704 
3705 	data = NULL;
3706 
3707 	if (error) {
3708 		goto done;
3709 	}
3710 
3711 	if (flags & PRUS_EOF) {
3712 		flow_divert_shutdown(so);
3713 	}
3714 
3715 done:
3716 	if (data) {
3717 		mbuf_freem(data);
3718 	}
3719 	if (control) {
3720 		mbuf_free(control);
3721 	}
3722 #if CONTENT_FILTER
3723 	if (cfil_tag) {
3724 		m_tag_free(cfil_tag);
3725 	}
3726 #endif
3727 
3728 	return error;
3729 }
3730 
3731 static int
flow_divert_preconnect(struct socket * so)3732 flow_divert_preconnect(struct socket *so)
3733 {
3734 	int error = 0;
3735 	struct flow_divert_pcb  *fd_cb  = so->so_fd_pcb;
3736 
3737 	VERIFY((so->so_flags & SOF_FLOW_DIVERT) && so->so_fd_pcb != NULL);
3738 
3739 	if (!(fd_cb->flags & FLOW_DIVERT_CONNECT_STARTED)) {
3740 		FDLOG0(LOG_INFO, fd_cb, "Pre-connect read: sending saved connect packet");
3741 		error = flow_divert_send_connect_packet(so->so_fd_pcb);
3742 		if (error) {
3743 			return error;
3744 		}
3745 
3746 		fd_cb->flags |= FLOW_DIVERT_CONNECT_STARTED;
3747 	}
3748 
3749 	soclearfastopen(so);
3750 
3751 	return error;
3752 }
3753 
3754 static void
flow_divert_set_protosw(struct socket * so)3755 flow_divert_set_protosw(struct socket *so)
3756 {
3757 	if (SOCK_DOM(so) == PF_INET) {
3758 		so->so_proto = &g_flow_divert_in_protosw;
3759 	} else {
3760 		so->so_proto = (struct protosw *)&g_flow_divert_in6_protosw;
3761 	}
3762 }
3763 
3764 static void
flow_divert_set_udp_protosw(struct socket * so)3765 flow_divert_set_udp_protosw(struct socket *so)
3766 {
3767 	if (SOCK_DOM(so) == PF_INET) {
3768 		so->so_proto = &g_flow_divert_in_udp_protosw;
3769 	} else {
3770 		so->so_proto = (struct protosw *)&g_flow_divert_in6_udp_protosw;
3771 	}
3772 }
3773 
3774 errno_t
flow_divert_implicit_data_out(struct socket * so,int flags,mbuf_t data,struct sockaddr * to,mbuf_t control,struct proc * p)3775 flow_divert_implicit_data_out(struct socket *so, int flags, mbuf_t data, struct sockaddr *to, mbuf_t control, struct proc *p)
3776 {
3777 	struct flow_divert_pcb  *fd_cb  = so->so_fd_pcb;
3778 	struct inpcb *inp;
3779 	int error = 0;
3780 
3781 	inp = sotoinpcb(so);
3782 	if (inp == NULL) {
3783 		return EINVAL;
3784 	}
3785 
3786 	if (fd_cb == NULL) {
3787 		error = flow_divert_pcb_init(so);
3788 		fd_cb  = so->so_fd_pcb;
3789 		if (error != 0 || fd_cb == NULL) {
3790 			goto done;
3791 		}
3792 	}
3793 	return flow_divert_data_out(so, flags, data, to, control, p);
3794 
3795 done:
3796 	if (data) {
3797 		mbuf_freem(data);
3798 	}
3799 	if (control) {
3800 		mbuf_free(control);
3801 	}
3802 
3803 	return error;
3804 }
3805 
3806 static errno_t
flow_divert_pcb_init_internal(struct socket * so,uint32_t ctl_unit,uint32_t aggregate_unit)3807 flow_divert_pcb_init_internal(struct socket *so, uint32_t ctl_unit, uint32_t aggregate_unit)
3808 {
3809 	errno_t error = 0;
3810 	struct flow_divert_pcb *fd_cb;
3811 	uint32_t agg_unit = aggregate_unit;
3812 	bool is_aggregate = false;
3813 	uint32_t group_unit = flow_divert_derive_kernel_control_unit(ctl_unit, &agg_unit, &is_aggregate);
3814 
3815 	if (group_unit == 0) {
3816 		return EINVAL;
3817 	}
3818 
3819 	if (so->so_flags & SOF_FLOW_DIVERT) {
3820 		return EALREADY;
3821 	}
3822 
3823 	fd_cb = flow_divert_pcb_create(so);
3824 	if (fd_cb != NULL) {
3825 		so->so_fd_pcb = fd_cb;
3826 		so->so_flags |= SOF_FLOW_DIVERT;
3827 		fd_cb->control_group_unit = group_unit;
3828 		fd_cb->policy_control_unit = ctl_unit;
3829 		fd_cb->aggregate_unit = agg_unit;
3830 		if (is_aggregate) {
3831 			fd_cb->flags |= FLOW_DIVERT_FLOW_IS_TRANSPARENT;
3832 		} else {
3833 			fd_cb->flags &= ~FLOW_DIVERT_FLOW_IS_TRANSPARENT;
3834 		}
3835 
3836 		error = flow_divert_pcb_insert(so, fd_cb, group_unit);
3837 		if (error) {
3838 			FDLOG(LOG_ERR, fd_cb, "pcb insert failed: %d", error);
3839 			if (so->so_flags & SOF_FLOW_DIVERT) {
3840 				so->so_fd_pcb = NULL;
3841 				so->so_flags &= ~SOF_FLOW_DIVERT;
3842 				FDRELEASE(fd_cb);
3843 			}
3844 		} else {
3845 			if (SOCK_TYPE(so) == SOCK_STREAM) {
3846 				flow_divert_set_protosw(so);
3847 			} else if (SOCK_TYPE(so) == SOCK_DGRAM) {
3848 				flow_divert_set_udp_protosw(so);
3849 			}
3850 
3851 			FDLOG0(LOG_INFO, fd_cb, "Created");
3852 		}
3853 	} else {
3854 		error = ENOMEM;
3855 	}
3856 
3857 	return error;
3858 }
3859 
3860 errno_t
flow_divert_pcb_init(struct socket * so)3861 flow_divert_pcb_init(struct socket *so)
3862 {
3863 	struct inpcb *inp = sotoinpcb(so);
3864 	uint32_t aggregate_units = 0;
3865 	uint32_t ctl_unit = necp_socket_get_flow_divert_control_unit(inp, &aggregate_units);
3866 	return flow_divert_pcb_init_internal(so, ctl_unit, aggregate_units);
3867 }
3868 
3869 errno_t
flow_divert_token_set(struct socket * so,struct sockopt * sopt)3870 flow_divert_token_set(struct socket *so, struct sockopt *sopt)
3871 {
3872 	uint32_t ctl_unit = 0;
3873 	uint32_t key_unit = 0;
3874 	uint32_t aggregate_unit = 0;
3875 	int error = 0;
3876 	int hmac_error = 0;
3877 	mbuf_t token = NULL;
3878 
3879 	if (so->so_flags & SOF_FLOW_DIVERT) {
3880 		error = EALREADY;
3881 		goto done;
3882 	}
3883 
3884 	if (g_init_result) {
3885 		FDLOG(LOG_ERR, &nil_pcb, "flow_divert_init failed (%d), cannot use flow divert", g_init_result);
3886 		error = ENOPROTOOPT;
3887 		goto done;
3888 	}
3889 
3890 	if ((SOCK_TYPE(so) != SOCK_STREAM && SOCK_TYPE(so) != SOCK_DGRAM) ||
3891 	    (SOCK_PROTO(so) != IPPROTO_TCP && SOCK_PROTO(so) != IPPROTO_UDP) ||
3892 	    (SOCK_DOM(so) != PF_INET && SOCK_DOM(so) != PF_INET6)) {
3893 		error = EINVAL;
3894 		goto done;
3895 	} else {
3896 		if (SOCK_TYPE(so) == SOCK_STREAM && SOCK_PROTO(so) == IPPROTO_TCP) {
3897 			struct tcpcb *tp = sototcpcb(so);
3898 			if (tp == NULL || tp->t_state != TCPS_CLOSED) {
3899 				error = EINVAL;
3900 				goto done;
3901 			}
3902 		}
3903 	}
3904 
3905 	error = soopt_getm(sopt, &token);
3906 	if (error) {
3907 		token = NULL;
3908 		goto done;
3909 	}
3910 
3911 	error = soopt_mcopyin(sopt, token);
3912 	if (error) {
3913 		token = NULL;
3914 		goto done;
3915 	}
3916 
3917 	error = flow_divert_packet_get_tlv(token, 0, FLOW_DIVERT_TLV_KEY_UNIT, sizeof(key_unit), (void *)&key_unit, NULL);
3918 	if (!error) {
3919 		key_unit = ntohl(key_unit);
3920 		if (key_unit >= GROUP_COUNT_MAX) {
3921 			key_unit = 0;
3922 		}
3923 	} else if (error != ENOENT) {
3924 		FDLOG(LOG_ERR, &nil_pcb, "Failed to get the key unit from the token: %d", error);
3925 		goto done;
3926 	} else {
3927 		key_unit = 0;
3928 	}
3929 
3930 	error = flow_divert_packet_get_tlv(token, 0, FLOW_DIVERT_TLV_CTL_UNIT, sizeof(ctl_unit), (void *)&ctl_unit, NULL);
3931 	if (error) {
3932 		FDLOG(LOG_ERR, &nil_pcb, "Failed to get the control socket unit from the token: %d", error);
3933 		goto done;
3934 	}
3935 
3936 	error = flow_divert_packet_get_tlv(token, 0, FLOW_DIVERT_TLV_AGGREGATE_UNIT, sizeof(aggregate_unit), (void *)&aggregate_unit, NULL);
3937 	if (error && error != ENOENT) {
3938 		FDLOG(LOG_ERR, &nil_pcb, "Failed to get the aggregate unit from the token: %d", error);
3939 		goto done;
3940 	}
3941 
3942 	/* A valid kernel control unit is required */
3943 	ctl_unit = ntohl(ctl_unit);
3944 	aggregate_unit = ntohl(aggregate_unit);
3945 
3946 	if (ctl_unit > 0 && ctl_unit < GROUP_COUNT_MAX) {
3947 		socket_unlock(so, 0);
3948 		hmac_error = flow_divert_packet_verify_hmac(token, (key_unit != 0 ? key_unit : ctl_unit));
3949 		socket_lock(so, 0);
3950 
3951 		if (hmac_error && hmac_error != ENOENT) {
3952 			FDLOG(LOG_ERR, &nil_pcb, "HMAC verfication failed: %d", hmac_error);
3953 			error = hmac_error;
3954 			goto done;
3955 		}
3956 	}
3957 
3958 	error = flow_divert_pcb_init_internal(so, ctl_unit, aggregate_unit);
3959 	if (error == 0) {
3960 		struct flow_divert_pcb *fd_cb = so->so_fd_pcb;
3961 		int log_level = LOG_NOTICE;
3962 
3963 		error = flow_divert_packet_get_tlv(token, 0, FLOW_DIVERT_TLV_LOG_LEVEL, sizeof(log_level), &log_level, NULL);
3964 		if (error == 0) {
3965 			fd_cb->log_level = (uint8_t)log_level;
3966 		}
3967 		error = 0;
3968 
3969 		fd_cb->connect_token = token;
3970 		token = NULL;
3971 
3972 		fd_cb->flags |= FLOW_DIVERT_HAS_TOKEN;
3973 	}
3974 
3975 	if (hmac_error == 0) {
3976 		struct flow_divert_pcb *fd_cb = so->so_fd_pcb;
3977 		if (fd_cb != NULL) {
3978 			fd_cb->flags |= FLOW_DIVERT_HAS_HMAC;
3979 		}
3980 	}
3981 
3982 done:
3983 	if (token != NULL) {
3984 		mbuf_freem(token);
3985 	}
3986 
3987 	return error;
3988 }
3989 
3990 errno_t
flow_divert_token_get(struct socket * so,struct sockopt * sopt)3991 flow_divert_token_get(struct socket *so, struct sockopt *sopt)
3992 {
3993 	uint32_t                                        ctl_unit;
3994 	int                                                     error                                           = 0;
3995 	uint8_t                                         hmac[SHA_DIGEST_LENGTH];
3996 	struct flow_divert_pcb          *fd_cb                                          = so->so_fd_pcb;
3997 	mbuf_t                                          token                                           = NULL;
3998 	struct flow_divert_group        *control_group                          = NULL;
3999 
4000 	if (!(so->so_flags & SOF_FLOW_DIVERT)) {
4001 		error = EINVAL;
4002 		goto done;
4003 	}
4004 
4005 	VERIFY((so->so_flags & SOF_FLOW_DIVERT) && so->so_fd_pcb != NULL);
4006 
4007 	if (fd_cb->group == NULL) {
4008 		error = EINVAL;
4009 		goto done;
4010 	}
4011 
4012 	error = mbuf_gethdr(MBUF_DONTWAIT, MBUF_TYPE_HEADER, &token);
4013 	if (error) {
4014 		FDLOG(LOG_ERR, fd_cb, "failed to allocate the header mbuf: %d", error);
4015 		goto done;
4016 	}
4017 
4018 	ctl_unit = htonl(fd_cb->group->ctl_unit);
4019 
4020 	error = flow_divert_packet_append_tlv(token, FLOW_DIVERT_TLV_CTL_UNIT, sizeof(ctl_unit), &ctl_unit);
4021 	if (error) {
4022 		goto done;
4023 	}
4024 
4025 	error = flow_divert_packet_append_tlv(token, FLOW_DIVERT_TLV_FLOW_ID, sizeof(fd_cb->hash), &fd_cb->hash);
4026 	if (error) {
4027 		goto done;
4028 	}
4029 
4030 	if (fd_cb->app_data != NULL) {
4031 		error = flow_divert_packet_append_tlv(token, FLOW_DIVERT_TLV_APP_DATA, (uint32_t)fd_cb->app_data_length, fd_cb->app_data);
4032 		if (error) {
4033 			goto done;
4034 		}
4035 	}
4036 
4037 	socket_unlock(so, 0);
4038 	lck_rw_lock_shared(&g_flow_divert_group_lck);
4039 
4040 	if (g_flow_divert_groups != NULL && g_active_group_count > 0 &&
4041 	    fd_cb->control_group_unit > 0 && fd_cb->control_group_unit < GROUP_COUNT_MAX) {
4042 		control_group = g_flow_divert_groups[fd_cb->control_group_unit];
4043 	}
4044 
4045 	if (control_group != NULL) {
4046 		lck_rw_lock_shared(&control_group->lck);
4047 		ctl_unit = htonl(control_group->ctl_unit);
4048 		error = flow_divert_packet_append_tlv(token, FLOW_DIVERT_TLV_KEY_UNIT, sizeof(ctl_unit), &ctl_unit);
4049 		if (!error) {
4050 			error = flow_divert_packet_compute_hmac(token, control_group, hmac);
4051 		}
4052 		lck_rw_done(&control_group->lck);
4053 	} else {
4054 		error = ENOPROTOOPT;
4055 	}
4056 
4057 	lck_rw_done(&g_flow_divert_group_lck);
4058 	socket_lock(so, 0);
4059 
4060 	if (error) {
4061 		goto done;
4062 	}
4063 
4064 	error = flow_divert_packet_append_tlv(token, FLOW_DIVERT_TLV_HMAC, sizeof(hmac), hmac);
4065 	if (error) {
4066 		goto done;
4067 	}
4068 
4069 	if (sopt->sopt_val == USER_ADDR_NULL) {
4070 		/* If the caller passed NULL to getsockopt, just set the size of the token and return */
4071 		sopt->sopt_valsize = mbuf_pkthdr_len(token);
4072 		goto done;
4073 	}
4074 
4075 	error = soopt_mcopyout(sopt, token);
4076 	if (error) {
4077 		token = NULL;   /* For some reason, soopt_mcopyout() frees the mbuf if it fails */
4078 		goto done;
4079 	}
4080 
4081 done:
4082 	if (token != NULL) {
4083 		mbuf_freem(token);
4084 	}
4085 
4086 	return error;
4087 }
4088 
4089 static errno_t
flow_divert_kctl_connect(kern_ctl_ref kctlref __unused,struct sockaddr_ctl * sac,void ** unitinfo)4090 flow_divert_kctl_connect(kern_ctl_ref kctlref __unused, struct sockaddr_ctl *sac, void **unitinfo)
4091 {
4092 	struct flow_divert_group        *new_group      = NULL;
4093 	int                             error           = 0;
4094 
4095 	if (sac->sc_unit >= GROUP_COUNT_MAX) {
4096 		error = EINVAL;
4097 		goto done;
4098 	}
4099 
4100 	*unitinfo = NULL;
4101 
4102 	new_group = zalloc_flags(flow_divert_group_zone, Z_WAITOK | Z_ZERO);
4103 	lck_rw_init(&new_group->lck, &flow_divert_mtx_grp, &flow_divert_mtx_attr);
4104 	RB_INIT(&new_group->pcb_tree);
4105 	new_group->ctl_unit = sac->sc_unit;
4106 	MBUFQ_INIT(&new_group->send_queue);
4107 	new_group->signing_id_trie.root = NULL_TRIE_IDX;
4108 
4109 	lck_rw_lock_exclusive(&g_flow_divert_group_lck);
4110 
4111 	if (g_flow_divert_groups == NULL) {
4112 		g_flow_divert_groups = kalloc_type(struct flow_divert_group *,
4113 		    GROUP_COUNT_MAX, Z_WAITOK | Z_ZERO | Z_NOFAIL);
4114 	}
4115 
4116 	if (g_flow_divert_groups[sac->sc_unit] != NULL) {
4117 		error = EALREADY;
4118 	} else {
4119 		g_flow_divert_groups[sac->sc_unit] = new_group;
4120 		g_active_group_count++;
4121 	}
4122 
4123 	lck_rw_done(&g_flow_divert_group_lck);
4124 
4125 done:
4126 	if (error == 0) {
4127 		*unitinfo = new_group;
4128 	} else if (new_group != NULL) {
4129 		zfree(flow_divert_group_zone, new_group);
4130 	}
4131 	return error;
4132 }
4133 
4134 static errno_t
flow_divert_kctl_disconnect(kern_ctl_ref kctlref __unused,uint32_t unit,void * unitinfo)4135 flow_divert_kctl_disconnect(kern_ctl_ref kctlref __unused, uint32_t unit, void *unitinfo)
4136 {
4137 	struct flow_divert_group        *group  = NULL;
4138 	errno_t                                         error   = 0;
4139 
4140 	if (unit >= GROUP_COUNT_MAX) {
4141 		return EINVAL;
4142 	}
4143 
4144 	if (unitinfo == NULL) {
4145 		return 0;
4146 	}
4147 
4148 	FDLOG(LOG_INFO, &nil_pcb, "disconnecting group %d", unit);
4149 
4150 	lck_rw_lock_exclusive(&g_flow_divert_group_lck);
4151 
4152 	if (g_flow_divert_groups == NULL || g_active_group_count == 0) {
4153 		panic("flow divert group %u is disconnecting, but no groups are active (groups = %p, active count = %u", unit,
4154 		    g_flow_divert_groups, g_active_group_count);
4155 	}
4156 
4157 	group = g_flow_divert_groups[unit];
4158 
4159 	if (group != (struct flow_divert_group *)unitinfo) {
4160 		panic("group with unit %d (%p) != unit info (%p)", unit, group, unitinfo);
4161 	}
4162 
4163 	g_flow_divert_groups[unit] = NULL;
4164 	g_active_group_count--;
4165 
4166 	if (g_active_group_count == 0) {
4167 		kfree_type(struct flow_divert_group *,
4168 		    GROUP_COUNT_MAX, g_flow_divert_groups);
4169 		g_flow_divert_groups = NULL;
4170 	}
4171 
4172 	lck_rw_done(&g_flow_divert_group_lck);
4173 
4174 	if (group != NULL) {
4175 		flow_divert_close_all(group);
4176 
4177 		lck_rw_lock_exclusive(&group->lck);
4178 
4179 		if (group->token_key != NULL) {
4180 			memset(group->token_key, 0, group->token_key_size);
4181 			kfree_data(group->token_key, group->token_key_size);
4182 			group->token_key = NULL;
4183 			group->token_key_size = 0;
4184 		}
4185 
4186 		/* Re-set the current trie */
4187 		if (group->signing_id_trie.memory != NULL) {
4188 			kfree_data_addr(group->signing_id_trie.memory);
4189 		}
4190 		memset(&group->signing_id_trie, 0, sizeof(group->signing_id_trie));
4191 		group->signing_id_trie.root = NULL_TRIE_IDX;
4192 
4193 		lck_rw_done(&group->lck);
4194 
4195 		zfree(flow_divert_group_zone, group);
4196 	} else {
4197 		error = EINVAL;
4198 	}
4199 
4200 	return error;
4201 }
4202 
4203 static errno_t
flow_divert_kctl_send(kern_ctl_ref kctlref __unused,uint32_t unit __unused,void * unitinfo,mbuf_t m,int flags __unused)4204 flow_divert_kctl_send(kern_ctl_ref kctlref __unused, uint32_t unit __unused, void *unitinfo, mbuf_t m, int flags __unused)
4205 {
4206 	return flow_divert_input(m, (struct flow_divert_group *)unitinfo);
4207 }
4208 
4209 static void
flow_divert_kctl_rcvd(kern_ctl_ref kctlref __unused,uint32_t unit __unused,void * unitinfo,int flags __unused)4210 flow_divert_kctl_rcvd(kern_ctl_ref kctlref __unused, uint32_t unit __unused, void *unitinfo, int flags __unused)
4211 {
4212 	struct flow_divert_group        *group  = (struct flow_divert_group *)unitinfo;
4213 
4214 	if (!OSTestAndClear(GROUP_BIT_CTL_ENQUEUE_BLOCKED, &group->atomic_bits)) {
4215 		struct flow_divert_pcb                  *fd_cb;
4216 		SLIST_HEAD(, flow_divert_pcb)   tmp_list;
4217 
4218 		lck_rw_lock_shared(&g_flow_divert_group_lck);
4219 		lck_rw_lock_exclusive(&group->lck);
4220 
4221 		while (!MBUFQ_EMPTY(&group->send_queue)) {
4222 			mbuf_t next_packet;
4223 			FDLOG0(LOG_DEBUG, &nil_pcb, "trying ctl_enqueuembuf again");
4224 			next_packet = MBUFQ_FIRST(&group->send_queue);
4225 			int error = ctl_enqueuembuf(g_flow_divert_kctl_ref, group->ctl_unit, next_packet, CTL_DATA_EOR);
4226 			if (error) {
4227 				FDLOG(LOG_DEBUG, &nil_pcb, "ctl_enqueuembuf returned an error: %d", error);
4228 				OSTestAndSet(GROUP_BIT_CTL_ENQUEUE_BLOCKED, &group->atomic_bits);
4229 				lck_rw_done(&group->lck);
4230 				lck_rw_done(&g_flow_divert_group_lck);
4231 				return;
4232 			}
4233 			MBUFQ_DEQUEUE(&group->send_queue, next_packet);
4234 		}
4235 
4236 		SLIST_INIT(&tmp_list);
4237 
4238 		RB_FOREACH(fd_cb, fd_pcb_tree, &group->pcb_tree) {
4239 			FDRETAIN(fd_cb);
4240 			SLIST_INSERT_HEAD(&tmp_list, fd_cb, tmp_list_entry);
4241 		}
4242 
4243 		lck_rw_done(&group->lck);
4244 
4245 		SLIST_FOREACH(fd_cb, &tmp_list, tmp_list_entry) {
4246 			FDLOCK(fd_cb);
4247 			if (fd_cb->so != NULL) {
4248 				socket_lock(fd_cb->so, 0);
4249 				if (fd_cb->group != NULL) {
4250 					flow_divert_send_buffered_data(fd_cb, FALSE);
4251 				}
4252 				socket_unlock(fd_cb->so, 0);
4253 			}
4254 			FDUNLOCK(fd_cb);
4255 			FDRELEASE(fd_cb);
4256 		}
4257 
4258 		lck_rw_done(&g_flow_divert_group_lck);
4259 	}
4260 }
4261 
4262 static int
flow_divert_kctl_init(void)4263 flow_divert_kctl_init(void)
4264 {
4265 	struct kern_ctl_reg     ctl_reg;
4266 	int                     result;
4267 
4268 	memset(&ctl_reg, 0, sizeof(ctl_reg));
4269 
4270 	strlcpy(ctl_reg.ctl_name, FLOW_DIVERT_CONTROL_NAME, sizeof(ctl_reg.ctl_name));
4271 	ctl_reg.ctl_name[sizeof(ctl_reg.ctl_name) - 1] = '\0';
4272 	ctl_reg.ctl_flags = CTL_FLAG_PRIVILEGED | CTL_FLAG_REG_EXTENDED;
4273 	ctl_reg.ctl_sendsize = FD_CTL_SENDBUFF_SIZE;
4274 
4275 	ctl_reg.ctl_connect = flow_divert_kctl_connect;
4276 	ctl_reg.ctl_disconnect = flow_divert_kctl_disconnect;
4277 	ctl_reg.ctl_send = flow_divert_kctl_send;
4278 	ctl_reg.ctl_rcvd = flow_divert_kctl_rcvd;
4279 
4280 	result = ctl_register(&ctl_reg, &g_flow_divert_kctl_ref);
4281 
4282 	if (result) {
4283 		FDLOG(LOG_ERR, &nil_pcb, "flow_divert_kctl_init - ctl_register failed: %d\n", result);
4284 		return result;
4285 	}
4286 
4287 	return 0;
4288 }
4289 
4290 void
flow_divert_init(void)4291 flow_divert_init(void)
4292 {
4293 	memset(&nil_pcb, 0, sizeof(nil_pcb));
4294 	nil_pcb.log_level = LOG_NOTICE;
4295 
4296 	g_tcp_protosw = pffindproto(AF_INET, IPPROTO_TCP, SOCK_STREAM);
4297 
4298 	VERIFY(g_tcp_protosw != NULL);
4299 
4300 	memcpy(&g_flow_divert_in_protosw, g_tcp_protosw, sizeof(g_flow_divert_in_protosw));
4301 	memcpy(&g_flow_divert_in_usrreqs, g_tcp_protosw->pr_usrreqs, sizeof(g_flow_divert_in_usrreqs));
4302 
4303 	g_flow_divert_in_usrreqs.pru_connect = flow_divert_connect_out;
4304 	g_flow_divert_in_usrreqs.pru_connectx = flow_divert_connectx_out;
4305 	g_flow_divert_in_usrreqs.pru_disconnect = flow_divert_close;
4306 	g_flow_divert_in_usrreqs.pru_disconnectx = flow_divert_disconnectx;
4307 	g_flow_divert_in_usrreqs.pru_rcvd = flow_divert_rcvd;
4308 	g_flow_divert_in_usrreqs.pru_send = flow_divert_data_out;
4309 	g_flow_divert_in_usrreqs.pru_shutdown = flow_divert_shutdown;
4310 	g_flow_divert_in_usrreqs.pru_preconnect = flow_divert_preconnect;
4311 
4312 	g_flow_divert_in_protosw.pr_usrreqs = &g_flow_divert_in_usrreqs;
4313 	g_flow_divert_in_protosw.pr_ctloutput = flow_divert_ctloutput;
4314 
4315 	/*
4316 	 * Socket filters shouldn't attach/detach to/from this protosw
4317 	 * since pr_protosw is to be used instead, which points to the
4318 	 * real protocol; if they do, it is a bug and we should panic.
4319 	 */
4320 	g_flow_divert_in_protosw.pr_filter_head.tqh_first =
4321 	    (struct socket_filter *)(uintptr_t)0xdeadbeefdeadbeef;
4322 	g_flow_divert_in_protosw.pr_filter_head.tqh_last =
4323 	    (struct socket_filter **)(uintptr_t)0xdeadbeefdeadbeef;
4324 
4325 	/* UDP */
4326 	g_udp_protosw = pffindproto(AF_INET, IPPROTO_UDP, SOCK_DGRAM);
4327 	VERIFY(g_udp_protosw != NULL);
4328 
4329 	memcpy(&g_flow_divert_in_udp_protosw, g_udp_protosw, sizeof(g_flow_divert_in_udp_protosw));
4330 	memcpy(&g_flow_divert_in_udp_usrreqs, g_udp_protosw->pr_usrreqs, sizeof(g_flow_divert_in_udp_usrreqs));
4331 
4332 	g_flow_divert_in_udp_usrreqs.pru_connect = flow_divert_connect_out;
4333 	g_flow_divert_in_udp_usrreqs.pru_connectx = flow_divert_connectx_out;
4334 	g_flow_divert_in_udp_usrreqs.pru_disconnect = flow_divert_close;
4335 	g_flow_divert_in_udp_usrreqs.pru_disconnectx = flow_divert_disconnectx;
4336 	g_flow_divert_in_udp_usrreqs.pru_rcvd = flow_divert_rcvd;
4337 	g_flow_divert_in_udp_usrreqs.pru_send = flow_divert_data_out;
4338 	g_flow_divert_in_udp_usrreqs.pru_shutdown = flow_divert_shutdown;
4339 	g_flow_divert_in_udp_usrreqs.pru_sosend_list = pru_sosend_list_notsupp;
4340 	g_flow_divert_in_udp_usrreqs.pru_soreceive_list = pru_soreceive_list_notsupp;
4341 	g_flow_divert_in_udp_usrreqs.pru_preconnect = flow_divert_preconnect;
4342 
4343 	g_flow_divert_in_udp_protosw.pr_usrreqs = &g_flow_divert_in_usrreqs;
4344 	g_flow_divert_in_udp_protosw.pr_ctloutput = flow_divert_ctloutput;
4345 
4346 	/*
4347 	 * Socket filters shouldn't attach/detach to/from this protosw
4348 	 * since pr_protosw is to be used instead, which points to the
4349 	 * real protocol; if they do, it is a bug and we should panic.
4350 	 */
4351 	g_flow_divert_in_udp_protosw.pr_filter_head.tqh_first =
4352 	    (struct socket_filter *)(uintptr_t)0xdeadbeefdeadbeef;
4353 	g_flow_divert_in_udp_protosw.pr_filter_head.tqh_last =
4354 	    (struct socket_filter **)(uintptr_t)0xdeadbeefdeadbeef;
4355 
4356 	g_tcp6_protosw = (struct ip6protosw *)pffindproto(AF_INET6, IPPROTO_TCP, SOCK_STREAM);
4357 
4358 	VERIFY(g_tcp6_protosw != NULL);
4359 
4360 	memcpy(&g_flow_divert_in6_protosw, g_tcp6_protosw, sizeof(g_flow_divert_in6_protosw));
4361 	memcpy(&g_flow_divert_in6_usrreqs, g_tcp6_protosw->pr_usrreqs, sizeof(g_flow_divert_in6_usrreqs));
4362 
4363 	g_flow_divert_in6_usrreqs.pru_connect = flow_divert_connect_out;
4364 	g_flow_divert_in6_usrreqs.pru_connectx = flow_divert_connectx6_out;
4365 	g_flow_divert_in6_usrreqs.pru_disconnect = flow_divert_close;
4366 	g_flow_divert_in6_usrreqs.pru_disconnectx = flow_divert_disconnectx;
4367 	g_flow_divert_in6_usrreqs.pru_rcvd = flow_divert_rcvd;
4368 	g_flow_divert_in6_usrreqs.pru_send = flow_divert_data_out;
4369 	g_flow_divert_in6_usrreqs.pru_shutdown = flow_divert_shutdown;
4370 	g_flow_divert_in6_usrreqs.pru_preconnect = flow_divert_preconnect;
4371 
4372 	g_flow_divert_in6_protosw.pr_usrreqs = &g_flow_divert_in6_usrreqs;
4373 	g_flow_divert_in6_protosw.pr_ctloutput = flow_divert_ctloutput;
4374 	/*
4375 	 * Socket filters shouldn't attach/detach to/from this protosw
4376 	 * since pr_protosw is to be used instead, which points to the
4377 	 * real protocol; if they do, it is a bug and we should panic.
4378 	 */
4379 	g_flow_divert_in6_protosw.pr_filter_head.tqh_first =
4380 	    (struct socket_filter *)(uintptr_t)0xdeadbeefdeadbeef;
4381 	g_flow_divert_in6_protosw.pr_filter_head.tqh_last =
4382 	    (struct socket_filter **)(uintptr_t)0xdeadbeefdeadbeef;
4383 
4384 	/* UDP6 */
4385 	g_udp6_protosw = (struct ip6protosw *)pffindproto(AF_INET6, IPPROTO_UDP, SOCK_DGRAM);
4386 
4387 	VERIFY(g_udp6_protosw != NULL);
4388 
4389 	memcpy(&g_flow_divert_in6_udp_protosw, g_udp6_protosw, sizeof(g_flow_divert_in6_udp_protosw));
4390 	memcpy(&g_flow_divert_in6_udp_usrreqs, g_udp6_protosw->pr_usrreqs, sizeof(g_flow_divert_in6_udp_usrreqs));
4391 
4392 	g_flow_divert_in6_udp_usrreqs.pru_connect = flow_divert_connect_out;
4393 	g_flow_divert_in6_udp_usrreqs.pru_connectx = flow_divert_connectx6_out;
4394 	g_flow_divert_in6_udp_usrreqs.pru_disconnect = flow_divert_close;
4395 	g_flow_divert_in6_udp_usrreqs.pru_disconnectx = flow_divert_disconnectx;
4396 	g_flow_divert_in6_udp_usrreqs.pru_rcvd = flow_divert_rcvd;
4397 	g_flow_divert_in6_udp_usrreqs.pru_send = flow_divert_data_out;
4398 	g_flow_divert_in6_udp_usrreqs.pru_shutdown = flow_divert_shutdown;
4399 	g_flow_divert_in6_udp_usrreqs.pru_sosend_list = pru_sosend_list_notsupp;
4400 	g_flow_divert_in6_udp_usrreqs.pru_soreceive_list = pru_soreceive_list_notsupp;
4401 	g_flow_divert_in6_udp_usrreqs.pru_preconnect = flow_divert_preconnect;
4402 
4403 	g_flow_divert_in6_udp_protosw.pr_usrreqs = &g_flow_divert_in6_udp_usrreqs;
4404 	g_flow_divert_in6_udp_protosw.pr_ctloutput = flow_divert_ctloutput;
4405 	/*
4406 	 * Socket filters shouldn't attach/detach to/from this protosw
4407 	 * since pr_protosw is to be used instead, which points to the
4408 	 * real protocol; if they do, it is a bug and we should panic.
4409 	 */
4410 	g_flow_divert_in6_udp_protosw.pr_filter_head.tqh_first =
4411 	    (struct socket_filter *)(uintptr_t)0xdeadbeefdeadbeef;
4412 	g_flow_divert_in6_udp_protosw.pr_filter_head.tqh_last =
4413 	    (struct socket_filter **)(uintptr_t)0xdeadbeefdeadbeef;
4414 
4415 	g_init_result = flow_divert_kctl_init();
4416 	if (g_init_result) {
4417 		goto done;
4418 	}
4419 
4420 done:
4421 	if (g_init_result != 0) {
4422 		if (g_flow_divert_kctl_ref != NULL) {
4423 			ctl_deregister(g_flow_divert_kctl_ref);
4424 			g_flow_divert_kctl_ref = NULL;
4425 		}
4426 	}
4427 }
4428