2 * Copyright (c) 2012-2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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>
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>
44 #include <sys/codesign.h>
45 #include <libkern/tree.h>
46 #include <kern/locks.h>
47 #include <kern/debug.h>
48 #include <net/if_var.h>
49 #include <net/route.h>
50 #include <net/flowhash.h>
51 #include <net/ntstat.h>
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <netinet/tcp.h>
55 #include <netinet/tcp_var.h>
56 #include <netinet/tcp_fsm.h>
57 #include <netinet/flow_divert.h>
58 #include <netinet/flow_divert_proto.h>
60 #include <netinet6/in6_pcb.h>
61 #include <netinet6/ip6protosw.h>
63 #include <dev/random/randomdev.h>
64 #include <libkern/crypto/sha1.h>
65 #include <libkern/crypto/crypto_internal.h>
68 #define FLOW_DIVERT_CONNECT_STARTED 0x00000001
69 #define FLOW_DIVERT_READ_CLOSED 0x00000002
70 #define FLOW_DIVERT_WRITE_CLOSED 0x00000004
71 #define FLOW_DIVERT_TUNNEL_RD_CLOSED 0x00000008
72 #define FLOW_DIVERT_TUNNEL_WR_CLOSED 0x00000010
73 #define FLOW_DIVERT_TRANSFERRED 0x00000020
74 #define FLOW_DIVERT_HAS_HMAC 0x00000040
76 #define FDLOG(level, pcb, format, ...) \
77 os_log_with_type(OS_LOG_DEFAULT, flow_divert_syslog_type_to_oslog_type(level), "(%u): " format "\n", (pcb)->hash, __VA_ARGS__)
79 #define FDLOG0(level, pcb, msg) \
80 os_log_with_type(OS_LOG_DEFAULT, flow_divert_syslog_type_to_oslog_type(level), "(%u): " msg "\n", (pcb)->hash)
82 #define FDRETAIN(pcb) if ((pcb) != NULL) OSIncrementAtomic(&(pcb)->ref_count)
83 #define FDRELEASE(pcb) \
85 if ((pcb) != NULL && 1 == OSDecrementAtomic(&(pcb)->ref_count)) { \
86 flow_divert_pcb_destroy(pcb); \
90 #define FDLOCK(pcb) lck_mtx_lock(&(pcb)->mtx)
91 #define FDUNLOCK(pcb) lck_mtx_unlock(&(pcb)->mtx)
93 #define FD_CTL_SENDBUFF_SIZE (128 * 1024)
94 #define FD_CTL_RCVBUFF_SIZE (128 * 1024)
96 #define GROUP_BIT_CTL_ENQUEUE_BLOCKED 0
98 #define GROUP_COUNT_MAX 32
99 #define FLOW_DIVERT_MAX_NAME_SIZE 4096
100 #define FLOW_DIVERT_MAX_KEY_SIZE 1024
101 #define FLOW_DIVERT_MAX_TRIE_MEMORY (1024 * 1024)
103 struct flow_divert_trie_node
110 #define CHILD_MAP_SIZE 256
111 #define NULL_TRIE_IDX 0xffff
112 #define TRIE_NODE(t, i) ((t)->nodes[(i)])
113 #define TRIE_CHILD(t, i, b) (((t)->child_maps + (CHILD_MAP_SIZE * TRIE_NODE(t, i).child_map))[(b)])
114 #define TRIE_BYTE(t, i) ((t)->bytes[(i)])
116 static struct flow_divert_pcb nil_pcb
;
118 decl_lck_rw_data(static, g_flow_divert_group_lck
);
119 static struct flow_divert_group
**g_flow_divert_groups
= NULL
;
120 static uint32_t g_active_group_count
= 0;
122 static lck_grp_attr_t
*flow_divert_grp_attr
= NULL
;
123 static lck_attr_t
*flow_divert_mtx_attr
= NULL
;
124 static lck_grp_t
*flow_divert_mtx_grp
= NULL
;
125 static errno_t g_init_result
= 0;
127 static kern_ctl_ref g_flow_divert_kctl_ref
= NULL
;
129 static struct protosw g_flow_divert_in_protosw
;
130 static struct pr_usrreqs g_flow_divert_in_usrreqs
;
131 static struct protosw g_flow_divert_in_udp_protosw
;
132 static struct pr_usrreqs g_flow_divert_in_udp_usrreqs
;
134 static struct ip6protosw g_flow_divert_in6_protosw
;
135 static struct pr_usrreqs g_flow_divert_in6_usrreqs
;
136 static struct ip6protosw g_flow_divert_in6_udp_protosw
;
137 static struct pr_usrreqs g_flow_divert_in6_udp_usrreqs
;
140 static struct protosw
*g_tcp_protosw
= NULL
;
141 static struct ip6protosw
*g_tcp6_protosw
= NULL
;
142 static struct protosw
*g_udp_protosw
= NULL
;
143 static struct ip6protosw
*g_udp6_protosw
= NULL
;
146 flow_divert_dup_addr(sa_family_t family
, struct sockaddr
*addr
, struct sockaddr
**dup
);
149 flow_divert_inp_to_sockaddr(const struct inpcb
*inp
, struct sockaddr
**local_socket
);
152 flow_divert_is_sockaddr_valid(struct sockaddr
*addr
);
155 flow_divert_append_target_endpoint_tlv(mbuf_t connect_packet
, struct sockaddr
*toaddr
);
158 flow_divert_get_buffered_target_address(mbuf_t buffer
);
161 flow_divert_has_pcb_local_address(const struct inpcb
*inp
);
164 flow_divert_disconnect_socket(struct socket
*so
);
166 static inline uint8_t
167 flow_divert_syslog_type_to_oslog_type(int syslog_type
)
169 switch (syslog_type
) {
170 case LOG_ERR
: return OS_LOG_TYPE_ERROR
;
171 case LOG_INFO
: return OS_LOG_TYPE_INFO
;
172 case LOG_DEBUG
: return OS_LOG_TYPE_DEBUG
;
173 default: return OS_LOG_TYPE_DEFAULT
;
178 flow_divert_pcb_cmp(const struct flow_divert_pcb
*pcb_a
, const struct flow_divert_pcb
*pcb_b
)
180 return memcmp(&pcb_a
->hash
, &pcb_b
->hash
, sizeof(pcb_a
->hash
));
183 RB_PROTOTYPE(fd_pcb_tree
, flow_divert_pcb
, rb_link
, flow_divert_pcb_cmp
);
184 RB_GENERATE(fd_pcb_tree
, flow_divert_pcb
, rb_link
, flow_divert_pcb_cmp
);
187 flow_divert_packet_type2str(uint8_t packet_type
)
189 switch (packet_type
) {
190 case FLOW_DIVERT_PKT_CONNECT
:
192 case FLOW_DIVERT_PKT_CONNECT_RESULT
:
193 return "connect result";
194 case FLOW_DIVERT_PKT_DATA
:
196 case FLOW_DIVERT_PKT_CLOSE
:
198 case FLOW_DIVERT_PKT_READ_NOTIFY
:
199 return "read notification";
200 case FLOW_DIVERT_PKT_PROPERTIES_UPDATE
:
201 return "properties update";
202 case FLOW_DIVERT_PKT_APP_MAP_CREATE
:
203 return "app map create";
209 static struct flow_divert_pcb
*
210 flow_divert_pcb_lookup(uint32_t hash
, struct flow_divert_group
*group
)
212 struct flow_divert_pcb key_item
;
213 struct flow_divert_pcb
*fd_cb
= NULL
;
215 key_item
.hash
= hash
;
217 lck_rw_lock_shared(&group
->lck
);
218 fd_cb
= RB_FIND(fd_pcb_tree
, &group
->pcb_tree
, &key_item
);
220 lck_rw_done(&group
->lck
);
226 flow_divert_pcb_insert(struct flow_divert_pcb
*fd_cb
, uint32_t ctl_unit
)
229 struct flow_divert_pcb
*exist
= NULL
;
230 struct flow_divert_group
*group
;
231 static uint32_t g_nextkey
= 1;
232 static uint32_t g_hash_seed
= 0;
235 if (ctl_unit
== 0 || ctl_unit
>= GROUP_COUNT_MAX
) {
239 socket_unlock(fd_cb
->so
, 0);
240 lck_rw_lock_shared(&g_flow_divert_group_lck
);
242 if (g_flow_divert_groups
== NULL
|| g_active_group_count
== 0) {
243 FDLOG0(LOG_ERR
, &nil_pcb
, "No active groups, flow divert cannot be used for this socket");
248 group
= g_flow_divert_groups
[ctl_unit
];
250 FDLOG(LOG_ERR
, &nil_pcb
, "Group for control unit %u is NULL, flow divert cannot be used for this socket", ctl_unit
);
255 socket_lock(fd_cb
->so
, 0);
261 key
[0] = g_nextkey
++;
262 key
[1] = RandomULong();
264 if (g_hash_seed
== 0) {
265 g_hash_seed
= RandomULong();
268 fd_cb
->hash
= net_flowhash(key
, sizeof(key
), g_hash_seed
);
270 for (idx
= 1; idx
< GROUP_COUNT_MAX
; idx
++) {
271 struct flow_divert_group
*curr_group
= g_flow_divert_groups
[idx
];
272 if (curr_group
!= NULL
&& curr_group
!= group
) {
273 lck_rw_lock_shared(&curr_group
->lck
);
274 exist
= RB_FIND(fd_pcb_tree
, &curr_group
->pcb_tree
, fd_cb
);
275 lck_rw_done(&curr_group
->lck
);
283 lck_rw_lock_exclusive(&group
->lck
);
284 exist
= RB_INSERT(fd_pcb_tree
, &group
->pcb_tree
, fd_cb
);
285 lck_rw_done(&group
->lck
);
287 } while (exist
!= NULL
&& try_count
++ < 3);
290 fd_cb
->group
= group
;
291 FDRETAIN(fd_cb
); /* The group now has a reference */
297 socket_unlock(fd_cb
->so
, 0);
300 lck_rw_done(&g_flow_divert_group_lck
);
301 socket_lock(fd_cb
->so
, 0);
306 static struct flow_divert_pcb
*
307 flow_divert_pcb_create(socket_t so
)
309 struct flow_divert_pcb
*new_pcb
= NULL
;
311 MALLOC_ZONE(new_pcb
, struct flow_divert_pcb
*, sizeof(*new_pcb
), M_FLOW_DIVERT_PCB
, M_WAITOK
);
312 if (new_pcb
== NULL
) {
313 FDLOG0(LOG_ERR
, &nil_pcb
, "failed to allocate a pcb");
317 memset(new_pcb
, 0, sizeof(*new_pcb
));
319 lck_mtx_init(&new_pcb
->mtx
, flow_divert_mtx_grp
, flow_divert_mtx_attr
);
321 new_pcb
->log_level
= nil_pcb
.log_level
;
323 FDRETAIN(new_pcb
); /* Represents the socket's reference */
329 flow_divert_pcb_destroy(struct flow_divert_pcb
*fd_cb
)
331 FDLOG(LOG_INFO
, fd_cb
, "Destroying, app tx %u, app rx %u, tunnel tx %u, tunnel rx %u",
332 fd_cb
->bytes_written_by_app
, fd_cb
->bytes_read_by_app
, fd_cb
->bytes_sent
, fd_cb
->bytes_received
);
334 if (fd_cb
->local_address
!= NULL
) {
335 FREE(fd_cb
->local_address
, M_SONAME
);
337 if (fd_cb
->remote_address
!= NULL
) {
338 FREE(fd_cb
->remote_address
, M_SONAME
);
340 if (fd_cb
->connect_token
!= NULL
) {
341 mbuf_freem(fd_cb
->connect_token
);
343 if (fd_cb
->connect_packet
!= NULL
) {
344 mbuf_freem(fd_cb
->connect_packet
);
346 if (fd_cb
->app_data
!= NULL
) {
347 FREE(fd_cb
->app_data
, M_TEMP
);
349 FREE_ZONE(fd_cb
, sizeof(*fd_cb
), M_FLOW_DIVERT_PCB
);
353 flow_divert_pcb_remove(struct flow_divert_pcb
*fd_cb
)
355 if (fd_cb
->group
!= NULL
) {
356 struct flow_divert_group
*group
= fd_cb
->group
;
357 lck_rw_lock_exclusive(&group
->lck
);
358 FDLOG(LOG_INFO
, fd_cb
, "Removing from group %d, ref count = %d", group
->ctl_unit
, fd_cb
->ref_count
);
359 RB_REMOVE(fd_pcb_tree
, &group
->pcb_tree
, fd_cb
);
361 FDRELEASE(fd_cb
); /* Release the group's reference */
362 lck_rw_done(&group
->lck
);
367 flow_divert_packet_init(struct flow_divert_pcb
*fd_cb
, uint8_t packet_type
, mbuf_t
*packet
)
369 struct flow_divert_packet_header hdr
;
372 error
= mbuf_gethdr(MBUF_DONTWAIT
, MBUF_TYPE_HEADER
, packet
);
374 FDLOG(LOG_ERR
, fd_cb
, "failed to allocate the header mbuf: %d", error
);
378 hdr
.packet_type
= packet_type
;
379 hdr
.conn_id
= htonl(fd_cb
->hash
);
381 /* Lay down the header */
382 error
= mbuf_copyback(*packet
, 0, sizeof(hdr
), &hdr
, MBUF_DONTWAIT
);
384 FDLOG(LOG_ERR
, fd_cb
, "mbuf_copyback(hdr) failed: %d", error
);
394 flow_divert_packet_append_tlv(mbuf_t packet
, uint8_t type
, uint32_t length
, const void *value
)
396 uint32_t net_length
= htonl(length
);
399 error
= mbuf_copyback(packet
, mbuf_pkthdr_len(packet
), sizeof(type
), &type
, MBUF_DONTWAIT
);
401 FDLOG(LOG_ERR
, &nil_pcb
, "failed to append the type (%d)", type
);
405 error
= mbuf_copyback(packet
, mbuf_pkthdr_len(packet
), sizeof(net_length
), &net_length
, MBUF_DONTWAIT
);
407 FDLOG(LOG_ERR
, &nil_pcb
, "failed to append the length (%u)", length
);
411 error
= mbuf_copyback(packet
, mbuf_pkthdr_len(packet
), length
, value
, MBUF_DONTWAIT
);
413 FDLOG0(LOG_ERR
, &nil_pcb
, "failed to append the value");
421 flow_divert_packet_find_tlv(mbuf_t packet
, int offset
, uint8_t type
, int *err
, int next
)
423 size_t cursor
= offset
;
425 uint32_t curr_length
;
432 error
= mbuf_copydata(packet
, cursor
, sizeof(curr_type
), &curr_type
);
439 curr_type
= FLOW_DIVERT_TLV_NIL
;
442 if (curr_type
!= type
) {
443 cursor
+= sizeof(curr_type
);
444 error
= mbuf_copydata(packet
, cursor
, sizeof(curr_length
), &curr_length
);
450 cursor
+= (sizeof(curr_length
) + ntohl(curr_length
));
452 } while (curr_type
!= type
);
458 flow_divert_packet_get_tlv(mbuf_t packet
, int offset
, uint8_t type
, size_t buff_len
, void *buff
, uint32_t *val_size
)
464 tlv_offset
= flow_divert_packet_find_tlv(packet
, offset
, type
, &error
, 0);
465 if (tlv_offset
< 0) {
469 error
= mbuf_copydata(packet
, tlv_offset
+ sizeof(type
), sizeof(length
), &length
);
474 length
= ntohl(length
);
476 if (val_size
!= NULL
) {
480 if (buff
!= NULL
&& buff_len
> 0) {
481 size_t to_copy
= (length
< buff_len
) ? length
: buff_len
;
482 error
= mbuf_copydata(packet
, tlv_offset
+ sizeof(type
) + sizeof(length
), to_copy
, buff
);
492 flow_divert_packet_compute_hmac(mbuf_t packet
, struct flow_divert_group
*group
, uint8_t *hmac
)
494 mbuf_t curr_mbuf
= packet
;
496 if (g_crypto_funcs
== NULL
|| group
->token_key
== NULL
) {
500 cchmac_di_decl(g_crypto_funcs
->ccsha1_di
, hmac_ctx
);
501 g_crypto_funcs
->cchmac_init_fn(g_crypto_funcs
->ccsha1_di
, hmac_ctx
, group
->token_key_size
, group
->token_key
);
503 while (curr_mbuf
!= NULL
) {
504 g_crypto_funcs
->cchmac_update_fn(g_crypto_funcs
->ccsha1_di
, hmac_ctx
, mbuf_len(curr_mbuf
), mbuf_data(curr_mbuf
));
505 curr_mbuf
= mbuf_next(curr_mbuf
);
508 g_crypto_funcs
->cchmac_final_fn(g_crypto_funcs
->ccsha1_di
, hmac_ctx
, hmac
);
514 flow_divert_packet_verify_hmac(mbuf_t packet
, uint32_t ctl_unit
)
517 struct flow_divert_group
*group
= NULL
;
519 uint8_t packet_hmac
[SHA_DIGEST_LENGTH
];
520 uint8_t computed_hmac
[SHA_DIGEST_LENGTH
];
523 lck_rw_lock_shared(&g_flow_divert_group_lck
);
525 if (g_flow_divert_groups
!= NULL
&& g_active_group_count
> 0) {
526 group
= g_flow_divert_groups
[ctl_unit
];
530 lck_rw_done(&g_flow_divert_group_lck
);
534 lck_rw_lock_shared(&group
->lck
);
536 if (group
->token_key
== NULL
) {
541 hmac_offset
= flow_divert_packet_find_tlv(packet
, 0, FLOW_DIVERT_TLV_HMAC
, &error
, 0);
542 if (hmac_offset
< 0) {
546 error
= flow_divert_packet_get_tlv(packet
, hmac_offset
, FLOW_DIVERT_TLV_HMAC
, sizeof(packet_hmac
), packet_hmac
, NULL
);
551 /* Chop off the HMAC TLV */
552 error
= mbuf_split(packet
, hmac_offset
, MBUF_WAITOK
, &tail
);
559 error
= flow_divert_packet_compute_hmac(packet
, group
, computed_hmac
);
564 if (memcmp(packet_hmac
, computed_hmac
, sizeof(packet_hmac
))) {
565 FDLOG0(LOG_WARNING
, &nil_pcb
, "HMAC in token does not match computed HMAC");
571 lck_rw_done(&group
->lck
);
572 lck_rw_done(&g_flow_divert_group_lck
);
577 flow_divert_add_data_statistics(struct flow_divert_pcb
*fd_cb
, int data_len
, Boolean send
)
579 struct inpcb
*inp
= NULL
;
580 struct ifnet
*ifp
= NULL
;
581 Boolean cell
= FALSE
;
582 Boolean wifi
= FALSE
;
583 Boolean wired
= FALSE
;
585 inp
= sotoinpcb(fd_cb
->so
);
590 ifp
= inp
->inp_last_outifp
;
592 cell
= IFNET_IS_CELLULAR(ifp
);
593 wifi
= (!cell
&& IFNET_IS_WIFI(ifp
));
594 wired
= (!wifi
&& IFNET_IS_WIRED(ifp
));
598 INP_ADD_STAT(inp
, cell
, wifi
, wired
, txpackets
, 1);
599 INP_ADD_STAT(inp
, cell
, wifi
, wired
, txbytes
, data_len
);
601 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxpackets
, 1);
602 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxbytes
, data_len
);
604 inp_set_activity_bitmap(inp
);
608 flow_divert_check_no_cellular(struct flow_divert_pcb
*fd_cb
)
610 struct inpcb
*inp
= NULL
;
612 inp
= sotoinpcb(fd_cb
->so
);
613 if (inp
&& INP_NO_CELLULAR(inp
) && inp
->inp_last_outifp
&&
614 IFNET_IS_CELLULAR(inp
->inp_last_outifp
))
621 flow_divert_check_no_expensive(struct flow_divert_pcb
*fd_cb
)
623 struct inpcb
*inp
= NULL
;
625 inp
= sotoinpcb(fd_cb
->so
);
626 if (inp
&& INP_NO_EXPENSIVE(inp
) && inp
->inp_last_outifp
&&
627 IFNET_IS_EXPENSIVE(inp
->inp_last_outifp
))
634 flow_divert_update_closed_state(struct flow_divert_pcb
*fd_cb
, int how
, Boolean tunnel
)
636 if (how
!= SHUT_RD
) {
637 fd_cb
->flags
|= FLOW_DIVERT_WRITE_CLOSED
;
638 if (tunnel
|| !(fd_cb
->flags
& FLOW_DIVERT_CONNECT_STARTED
)) {
639 fd_cb
->flags
|= FLOW_DIVERT_TUNNEL_WR_CLOSED
;
640 /* If the tunnel is not accepting writes any more, then flush the send buffer */
641 sbflush(&fd_cb
->so
->so_snd
);
644 if (how
!= SHUT_WR
) {
645 fd_cb
->flags
|= FLOW_DIVERT_READ_CLOSED
;
646 if (tunnel
|| !(fd_cb
->flags
& FLOW_DIVERT_CONNECT_STARTED
)) {
647 fd_cb
->flags
|= FLOW_DIVERT_TUNNEL_RD_CLOSED
;
653 trie_node_alloc(struct flow_divert_trie
*trie
)
655 if (trie
->nodes_free_next
< trie
->nodes_count
) {
656 uint16_t node_idx
= trie
->nodes_free_next
++;
657 TRIE_NODE(trie
, node_idx
).child_map
= NULL_TRIE_IDX
;
660 return NULL_TRIE_IDX
;
665 trie_child_map_alloc(struct flow_divert_trie
*trie
)
667 if (trie
->child_maps_free_next
< trie
->child_maps_count
) {
668 return trie
->child_maps_free_next
++;
670 return NULL_TRIE_IDX
;
675 trie_bytes_move(struct flow_divert_trie
*trie
, uint16_t bytes_idx
, size_t bytes_size
)
677 uint16_t start
= trie
->bytes_free_next
;
678 if (start
+ bytes_size
<= trie
->bytes_count
) {
679 if (start
!= bytes_idx
) {
680 memmove(&TRIE_BYTE(trie
, start
), &TRIE_BYTE(trie
, bytes_idx
), bytes_size
);
682 trie
->bytes_free_next
+= bytes_size
;
685 return NULL_TRIE_IDX
;
690 flow_divert_trie_insert(struct flow_divert_trie
*trie
, uint16_t string_start
, size_t string_len
)
692 uint16_t current
= trie
->root
;
693 uint16_t child
= trie
->root
;
694 uint16_t string_end
= string_start
+ string_len
;
695 uint16_t string_idx
= string_start
;
696 uint16_t string_remainder
= string_len
;
698 while (child
!= NULL_TRIE_IDX
) {
699 uint16_t parent
= current
;
701 uint16_t current_end
;
704 child
= NULL_TRIE_IDX
;
706 current_end
= TRIE_NODE(trie
, current
).start
+ TRIE_NODE(trie
, current
).length
;
708 for (node_idx
= TRIE_NODE(trie
, current
).start
;
709 node_idx
< current_end
&&
710 string_idx
< string_end
&&
711 TRIE_BYTE(trie
, node_idx
) == TRIE_BYTE(trie
, string_idx
);
712 node_idx
++, string_idx
++);
714 string_remainder
= string_end
- string_idx
;
716 if (node_idx
< (TRIE_NODE(trie
, current
).start
+ TRIE_NODE(trie
, current
).length
)) {
718 * We did not reach the end of the current node's string.
719 * We need to split the current node into two:
720 * 1. A new node that contains the prefix of the node that matches
721 * the prefix of the string being inserted.
722 * 2. The current node modified to point to the remainder
723 * of the current node's string.
725 uint16_t prefix
= trie_node_alloc(trie
);
726 if (prefix
== NULL_TRIE_IDX
) {
727 FDLOG0(LOG_ERR
, &nil_pcb
, "Ran out of trie nodes while splitting an existing node");
728 return NULL_TRIE_IDX
;
732 * Prefix points to the portion of the current nodes's string that has matched
733 * the input string thus far.
735 TRIE_NODE(trie
, prefix
).start
= TRIE_NODE(trie
, current
).start
;
736 TRIE_NODE(trie
, prefix
).length
= (node_idx
- TRIE_NODE(trie
, current
).start
);
739 * Prefix has the current node as the child corresponding to the first byte
742 TRIE_NODE(trie
, prefix
).child_map
= trie_child_map_alloc(trie
);
743 if (TRIE_NODE(trie
, prefix
).child_map
== NULL_TRIE_IDX
) {
744 FDLOG0(LOG_ERR
, &nil_pcb
, "Ran out of child maps while splitting an existing node");
745 return NULL_TRIE_IDX
;
747 TRIE_CHILD(trie
, prefix
, TRIE_BYTE(trie
, node_idx
)) = current
;
749 /* Parent has the prefix as the child correspoding to the first byte in the prefix */
750 TRIE_CHILD(trie
, parent
, TRIE_BYTE(trie
, TRIE_NODE(trie
, prefix
).start
)) = prefix
;
752 /* Current node is adjusted to point to the remainder */
753 TRIE_NODE(trie
, current
).start
= node_idx
;
754 TRIE_NODE(trie
, current
).length
-= TRIE_NODE(trie
, prefix
).length
;
756 /* We want to insert the new leaf (if any) as a child of the prefix */
760 if (string_remainder
> 0) {
762 * We still have bytes in the string that have not been matched yet.
763 * If the current node has children, iterate to the child corresponding
764 * to the next byte in the string.
766 if (TRIE_NODE(trie
, current
).child_map
!= NULL_TRIE_IDX
) {
767 child
= TRIE_CHILD(trie
, current
, TRIE_BYTE(trie
, string_idx
));
770 } /* while (child != NULL_TRIE_IDX) */
772 if (string_remainder
> 0) {
773 /* Add a new leaf containing the remainder of the string */
774 uint16_t leaf
= trie_node_alloc(trie
);
775 if (leaf
== NULL_TRIE_IDX
) {
776 FDLOG0(LOG_ERR
, &nil_pcb
, "Ran out of trie nodes while inserting a new leaf");
777 return NULL_TRIE_IDX
;
780 TRIE_NODE(trie
, leaf
).start
= trie_bytes_move(trie
, string_idx
, string_remainder
);
781 if (TRIE_NODE(trie
, leaf
).start
== NULL_TRIE_IDX
) {
782 FDLOG0(LOG_ERR
, &nil_pcb
, "Ran out of bytes while inserting a new leaf");
783 return NULL_TRIE_IDX
;
785 TRIE_NODE(trie
, leaf
).length
= string_remainder
;
787 /* Set the new leaf as the child of the current node */
788 if (TRIE_NODE(trie
, current
).child_map
== NULL_TRIE_IDX
) {
789 TRIE_NODE(trie
, current
).child_map
= trie_child_map_alloc(trie
);
790 if (TRIE_NODE(trie
, current
).child_map
== NULL_TRIE_IDX
) {
791 FDLOG0(LOG_ERR
, &nil_pcb
, "Ran out of child maps while inserting a new leaf");
792 return NULL_TRIE_IDX
;
795 TRIE_CHILD(trie
, current
, TRIE_BYTE(trie
, TRIE_NODE(trie
, leaf
).start
)) = leaf
;
797 } /* else duplicate or this string is a prefix of one of the existing strings */
802 #define APPLE_WEBCLIP_ID_PREFIX "com.apple.webapp"
804 flow_divert_trie_search(struct flow_divert_trie
*trie
, uint8_t *string_bytes
)
806 uint16_t current
= trie
->root
;
807 uint16_t string_idx
= 0;
809 while (current
!= NULL_TRIE_IDX
) {
810 uint16_t next
= NULL_TRIE_IDX
;
811 uint16_t node_end
= TRIE_NODE(trie
, current
).start
+ TRIE_NODE(trie
, current
).length
;
814 for (node_idx
= TRIE_NODE(trie
, current
).start
;
815 node_idx
< node_end
&& string_bytes
[string_idx
] != '\0' && string_bytes
[string_idx
] == TRIE_BYTE(trie
, node_idx
);
816 node_idx
++, string_idx
++);
818 if (node_idx
== node_end
) {
819 if (string_bytes
[string_idx
] == '\0') {
820 return current
; /* Got an exact match */
821 } else if (string_idx
== strlen(APPLE_WEBCLIP_ID_PREFIX
) &&
822 0 == strncmp((const char *)string_bytes
, APPLE_WEBCLIP_ID_PREFIX
, string_idx
)) {
823 string_bytes
[string_idx
] = '\0';
824 return current
; /* Got an apple webclip id prefix match */
825 } else if (TRIE_NODE(trie
, current
).child_map
!= NULL_TRIE_IDX
) {
826 next
= TRIE_CHILD(trie
, current
, string_bytes
[string_idx
]);
832 return NULL_TRIE_IDX
;
835 struct uuid_search_info
{
837 char *found_signing_id
;
838 boolean_t found_multiple_signing_ids
;
843 flow_divert_find_proc_by_uuid_callout(proc_t p
, void *arg
)
845 struct uuid_search_info
*info
= (struct uuid_search_info
*)arg
;
846 int result
= PROC_RETURNED_DONE
; /* By default, we didn't find the process */
848 if (info
->found_signing_id
!= NULL
) {
849 if (!info
->found_multiple_signing_ids
) {
850 /* All processes that were found had the same signing identifier, so just claim this first one and be done. */
851 info
->found_proc
= p
;
852 result
= PROC_CLAIMED_DONE
;
854 uuid_string_t uuid_str
;
855 uuid_unparse(info
->target_uuid
, uuid_str
);
856 FDLOG(LOG_WARNING
, &nil_pcb
, "Found multiple processes with UUID %s with different signing identifiers", uuid_str
);
858 FREE(info
->found_signing_id
, M_TEMP
);
859 info
->found_signing_id
= NULL
;
862 if (result
== PROC_RETURNED_DONE
) {
863 uuid_string_t uuid_str
;
864 uuid_unparse(info
->target_uuid
, uuid_str
);
865 FDLOG(LOG_WARNING
, &nil_pcb
, "Failed to find a process with UUID %s", uuid_str
);
872 flow_divert_find_proc_by_uuid_filter(proc_t p
, void *arg
)
874 struct uuid_search_info
*info
= (struct uuid_search_info
*)arg
;
877 if (info
->found_multiple_signing_ids
) {
881 include
= (uuid_compare(p
->p_uuid
, info
->target_uuid
) == 0);
883 const char *signing_id
= cs_identity_get(p
);
884 if (signing_id
!= NULL
) {
885 FDLOG(LOG_INFO
, &nil_pcb
, "Found process %d with signing identifier %s", p
->p_pid
, signing_id
);
886 size_t signing_id_size
= strlen(signing_id
) + 1;
887 if (info
->found_signing_id
== NULL
) {
888 MALLOC(info
->found_signing_id
, char *, signing_id_size
, M_TEMP
, M_WAITOK
);
889 memcpy(info
->found_signing_id
, signing_id
, signing_id_size
);
890 } else if (memcmp(signing_id
, info
->found_signing_id
, signing_id_size
)) {
891 info
->found_multiple_signing_ids
= TRUE
;
894 info
->found_multiple_signing_ids
= TRUE
;
896 include
= !info
->found_multiple_signing_ids
;
903 flow_divert_find_proc_by_uuid(uuid_t uuid
)
905 struct uuid_search_info info
;
907 if (LOG_INFO
<= nil_pcb
.log_level
) {
908 uuid_string_t uuid_str
;
909 uuid_unparse(uuid
, uuid_str
);
910 FDLOG(LOG_INFO
, &nil_pcb
, "Looking for process with UUID %s", uuid_str
);
913 memset(&info
, 0, sizeof(info
));
914 info
.found_proc
= PROC_NULL
;
915 uuid_copy(info
.target_uuid
, uuid
);
917 proc_iterate(PROC_ALLPROCLIST
, flow_divert_find_proc_by_uuid_callout
, &info
, flow_divert_find_proc_by_uuid_filter
, &info
);
919 return info
.found_proc
;
923 flow_divert_get_src_proc(struct socket
*so
, proc_t
*proc
)
927 if (so
->so_flags
& SOF_DELEGATED
) {
928 if ((*proc
)->p_pid
!= so
->e_pid
) {
929 *proc
= proc_find(so
->e_pid
);
931 } else if (uuid_compare((*proc
)->p_uuid
, so
->e_uuid
)) {
932 *proc
= flow_divert_find_proc_by_uuid(so
->e_uuid
);
935 } else if (*proc
== PROC_NULL
) {
936 *proc
= current_proc();
939 if (*proc
!= PROC_NULL
) {
940 if ((*proc
)->p_pid
== 0) {
953 flow_divert_send_packet(struct flow_divert_pcb
*fd_cb
, mbuf_t packet
, Boolean enqueue
)
957 if (fd_cb
->group
== NULL
) {
958 fd_cb
->so
->so_error
= ECONNABORTED
;
959 flow_divert_disconnect_socket(fd_cb
->so
);
963 lck_rw_lock_shared(&fd_cb
->group
->lck
);
965 if (MBUFQ_EMPTY(&fd_cb
->group
->send_queue
)) {
966 error
= ctl_enqueuembuf(g_flow_divert_kctl_ref
, fd_cb
->group
->ctl_unit
, packet
, CTL_DATA_EOR
);
971 if (error
== ENOBUFS
) {
973 if (!lck_rw_lock_shared_to_exclusive(&fd_cb
->group
->lck
)) {
974 lck_rw_lock_exclusive(&fd_cb
->group
->lck
);
976 MBUFQ_ENQUEUE(&fd_cb
->group
->send_queue
, packet
);
979 OSTestAndSet(GROUP_BIT_CTL_ENQUEUE_BLOCKED
, &fd_cb
->group
->atomic_bits
);
982 lck_rw_done(&fd_cb
->group
->lck
);
988 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
)
992 char *signing_id
= NULL
;
993 int free_signing_id
= 0;
994 mbuf_t connect_packet
= NULL
;
996 int release_proc
= 0;
998 error
= flow_divert_packet_init(fd_cb
, FLOW_DIVERT_PKT_CONNECT
, &connect_packet
);
1005 if (fd_cb
->connect_token
!= NULL
&& (fd_cb
->flags
& FLOW_DIVERT_HAS_HMAC
)) {
1006 uint32_t sid_size
= 0;
1007 int find_error
= flow_divert_packet_get_tlv(fd_cb
->connect_token
, 0, FLOW_DIVERT_TLV_SIGNING_ID
, 0, NULL
, &sid_size
);
1008 if (find_error
== 0 && sid_size
> 0) {
1009 MALLOC(signing_id
, char *, sid_size
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
1010 if (signing_id
!= NULL
) {
1011 flow_divert_packet_get_tlv(fd_cb
->connect_token
, 0, FLOW_DIVERT_TLV_SIGNING_ID
, sid_size
, signing_id
, NULL
);
1012 FDLOG(LOG_INFO
, fd_cb
, "Got %s from token", signing_id
);
1013 free_signing_id
= 1;
1018 socket_unlock(so
, 0);
1020 if (signing_id
== NULL
) {
1021 release_proc
= flow_divert_get_src_proc(so
, &src_proc
);
1022 if (src_proc
!= PROC_NULL
) {
1023 proc_lock(src_proc
);
1024 if (src_proc
->p_csflags
& (CS_VALID
|CS_DEBUGGED
)) {
1026 cs_id
= cs_identity_get(src_proc
);
1027 signing_id
= __DECONST(char *, cs_id
);
1029 FDLOG0(LOG_WARNING
, fd_cb
, "Signature is invalid");
1032 FDLOG0(LOG_WARNING
, fd_cb
, "Failed to determine the current proc");
1035 src_proc
= PROC_NULL
;
1038 if (signing_id
!= NULL
) {
1039 uint16_t result
= NULL_TRIE_IDX
;
1040 lck_rw_lock_shared(&fd_cb
->group
->lck
);
1041 if (fd_cb
->group
->flags
& FLOW_DIVERT_GROUP_FLAG_NO_APP_MAP
) {
1044 result
= flow_divert_trie_search(&fd_cb
->group
->signing_id_trie
, (uint8_t *)signing_id
);
1046 lck_rw_done(&fd_cb
->group
->lck
);
1047 if (result
!= NULL_TRIE_IDX
) {
1049 FDLOG(LOG_INFO
, fd_cb
, "%s matched", signing_id
);
1051 error
= flow_divert_packet_append_tlv(connect_packet
, FLOW_DIVERT_TLV_SIGNING_ID
, strlen(signing_id
), signing_id
);
1053 if (src_proc
!= PROC_NULL
) {
1054 unsigned char cdhash
[SHA1_RESULTLEN
];
1055 error
= proc_getcdhash(src_proc
, cdhash
);
1057 error
= flow_divert_packet_append_tlv(connect_packet
, FLOW_DIVERT_TLV_CDHASH
, sizeof(cdhash
), cdhash
);
1059 FDLOG(LOG_ERR
, fd_cb
, "failed to append the cdhash: %d", error
);
1062 FDLOG(LOG_ERR
, fd_cb
, "failed to get the cdhash: %d", error
);
1066 FDLOG(LOG_ERR
, fd_cb
, "failed to append the signing ID: %d", error
);
1069 FDLOG(LOG_WARNING
, fd_cb
, "%s did not match", signing_id
);
1072 FDLOG0(LOG_WARNING
, fd_cb
, "Failed to get the code signing identity");
1075 if (src_proc
!= PROC_NULL
) {
1076 proc_unlock(src_proc
);
1078 proc_rele(src_proc
);
1083 if (free_signing_id
) {
1084 FREE(signing_id
, M_TEMP
);
1091 error
= flow_divert_packet_append_tlv(connect_packet
,
1092 FLOW_DIVERT_TLV_TRAFFIC_CLASS
,
1093 sizeof(fd_cb
->so
->so_traffic_class
),
1094 &fd_cb
->so
->so_traffic_class
);
1099 if (SOCK_TYPE(fd_cb
->so
) == SOCK_STREAM
) {
1100 flow_type
= FLOW_DIVERT_FLOW_TYPE_TCP
;
1101 } else if (SOCK_TYPE(fd_cb
->so
) == SOCK_DGRAM
) {
1102 flow_type
= FLOW_DIVERT_FLOW_TYPE_UDP
;
1107 error
= flow_divert_packet_append_tlv(connect_packet
,
1108 FLOW_DIVERT_TLV_FLOW_TYPE
,
1116 if (fd_cb
->so
->so_flags
& SOF_DELEGATED
) {
1117 error
= flow_divert_packet_append_tlv(connect_packet
,
1118 FLOW_DIVERT_TLV_PID
,
1119 sizeof(fd_cb
->so
->e_pid
),
1125 error
= flow_divert_packet_append_tlv(connect_packet
,
1126 FLOW_DIVERT_TLV_UUID
,
1127 sizeof(fd_cb
->so
->e_uuid
),
1128 &fd_cb
->so
->e_uuid
);
1133 error
= flow_divert_packet_append_tlv(connect_packet
,
1134 FLOW_DIVERT_TLV_PID
,
1135 sizeof(fd_cb
->so
->e_pid
),
1136 &fd_cb
->so
->last_pid
);
1141 error
= flow_divert_packet_append_tlv(connect_packet
,
1142 FLOW_DIVERT_TLV_UUID
,
1143 sizeof(fd_cb
->so
->e_uuid
),
1144 &fd_cb
->so
->last_uuid
);
1150 if (fd_cb
->connect_token
!= NULL
) {
1151 unsigned int token_len
= m_length(fd_cb
->connect_token
);
1152 mbuf_concatenate(connect_packet
, fd_cb
->connect_token
);
1153 mbuf_pkthdr_adjustlen(connect_packet
, token_len
);
1154 fd_cb
->connect_token
= NULL
;
1156 uint32_t ctl_unit
= htonl(fd_cb
->control_group_unit
);
1158 error
= flow_divert_packet_append_tlv(connect_packet
, FLOW_DIVERT_TLV_CTL_UNIT
, sizeof(ctl_unit
), &ctl_unit
);
1163 error
= flow_divert_append_target_endpoint_tlv(connect_packet
, to
);
1169 if (fd_cb
->local_address
!= NULL
) {
1173 struct inpcb
*inp
= sotoinpcb(so
);
1174 if (flow_divert_has_pcb_local_address(inp
)) {
1175 error
= flow_divert_inp_to_sockaddr(inp
, &fd_cb
->local_address
);
1177 FDLOG0(LOG_ERR
, fd_cb
, "failed to get the local socket address.");
1183 if (fd_cb
->local_address
!= NULL
) {
1184 /* socket is bound. */
1185 error
= flow_divert_packet_append_tlv(connect_packet
, FLOW_DIVERT_TLV_LOCAL_ADDR
,
1186 sizeof(struct sockaddr_storage
), fd_cb
->local_address
);
1192 if (so
->so_flags1
& SOF1_DATA_IDEMPOTENT
) {
1193 uint32_t flags
= FLOW_DIVERT_TOKEN_FLAG_TFO
;
1194 error
= flow_divert_packet_append_tlv(connect_packet
, FLOW_DIVERT_TLV_FLAGS
, sizeof(flags
), &flags
);
1202 *out_connect_packet
= connect_packet
;
1203 } else if (connect_packet
!= NULL
) {
1204 mbuf_freem(connect_packet
);
1211 flow_divert_send_connect_result(struct flow_divert_pcb
*fd_cb
)
1214 mbuf_t packet
= NULL
;
1215 int rbuff_space
= 0;
1217 error
= flow_divert_packet_init(fd_cb
, FLOW_DIVERT_PKT_CONNECT_RESULT
, &packet
);
1219 FDLOG(LOG_ERR
, fd_cb
, "failed to create a connect result packet: %d", error
);
1223 rbuff_space
= fd_cb
->so
->so_rcv
.sb_hiwat
;
1224 if (rbuff_space
< 0) {
1227 rbuff_space
= htonl(rbuff_space
);
1228 error
= flow_divert_packet_append_tlv(packet
,
1229 FLOW_DIVERT_TLV_SPACE_AVAILABLE
,
1230 sizeof(rbuff_space
),
1236 error
= flow_divert_send_packet(fd_cb
, packet
, TRUE
);
1242 if (error
&& packet
!= NULL
) {
1250 flow_divert_send_close(struct flow_divert_pcb
*fd_cb
, int how
)
1253 mbuf_t packet
= NULL
;
1256 error
= flow_divert_packet_init(fd_cb
, FLOW_DIVERT_PKT_CLOSE
, &packet
);
1258 FDLOG(LOG_ERR
, fd_cb
, "failed to create a close packet: %d", error
);
1262 error
= flow_divert_packet_append_tlv(packet
, FLOW_DIVERT_TLV_ERROR_CODE
, sizeof(zero
), &zero
);
1264 FDLOG(LOG_ERR
, fd_cb
, "failed to add the error code TLV: %d", error
);
1269 error
= flow_divert_packet_append_tlv(packet
, FLOW_DIVERT_TLV_HOW
, sizeof(how
), &how
);
1271 FDLOG(LOG_ERR
, fd_cb
, "failed to add the how flag: %d", error
);
1275 error
= flow_divert_send_packet(fd_cb
, packet
, TRUE
);
1281 if (error
&& packet
!= NULL
) {
1289 flow_divert_tunnel_how_closed(struct flow_divert_pcb
*fd_cb
)
1291 if ((fd_cb
->flags
& (FLOW_DIVERT_TUNNEL_RD_CLOSED
|FLOW_DIVERT_TUNNEL_WR_CLOSED
)) ==
1292 (FLOW_DIVERT_TUNNEL_RD_CLOSED
|FLOW_DIVERT_TUNNEL_WR_CLOSED
))
1295 } else if (fd_cb
->flags
& FLOW_DIVERT_TUNNEL_RD_CLOSED
) {
1297 } else if (fd_cb
->flags
& FLOW_DIVERT_TUNNEL_WR_CLOSED
) {
1305 * Determine what close messages if any need to be sent to the tunnel. Returns TRUE if the tunnel is closed for both reads and
1306 * writes. Returns FALSE otherwise.
1309 flow_divert_send_close_if_needed(struct flow_divert_pcb
*fd_cb
)
1313 /* Do not send any close messages if there is still data in the send buffer */
1314 if (fd_cb
->so
->so_snd
.sb_cc
== 0) {
1315 if ((fd_cb
->flags
& (FLOW_DIVERT_READ_CLOSED
|FLOW_DIVERT_TUNNEL_RD_CLOSED
)) == FLOW_DIVERT_READ_CLOSED
) {
1316 /* Socket closed reads, but tunnel did not. Tell tunnel to close reads */
1319 if ((fd_cb
->flags
& (FLOW_DIVERT_WRITE_CLOSED
|FLOW_DIVERT_TUNNEL_WR_CLOSED
)) == FLOW_DIVERT_WRITE_CLOSED
) {
1320 /* Socket closed writes, but tunnel did not. Tell tunnel to close writes */
1321 if (how
== SHUT_RD
) {
1330 FDLOG(LOG_INFO
, fd_cb
, "sending close, how = %d", how
);
1331 if (flow_divert_send_close(fd_cb
, how
) != ENOBUFS
) {
1332 /* Successfully sent the close packet. Record the ways in which the tunnel has been closed */
1333 if (how
!= SHUT_RD
) {
1334 fd_cb
->flags
|= FLOW_DIVERT_TUNNEL_WR_CLOSED
;
1336 if (how
!= SHUT_WR
) {
1337 fd_cb
->flags
|= FLOW_DIVERT_TUNNEL_RD_CLOSED
;
1342 if (flow_divert_tunnel_how_closed(fd_cb
) == SHUT_RDWR
) {
1343 flow_divert_disconnect_socket(fd_cb
->so
);
1348 flow_divert_send_data_packet(struct flow_divert_pcb
*fd_cb
, mbuf_t data
, size_t data_len
, struct sockaddr
*toaddr
, Boolean force
)
1354 error
= flow_divert_packet_init(fd_cb
, FLOW_DIVERT_PKT_DATA
, &packet
);
1356 FDLOG(LOG_ERR
, fd_cb
, "flow_divert_packet_init failed: %d", error
);
1360 if (toaddr
!= NULL
) {
1361 error
= flow_divert_append_target_endpoint_tlv(packet
, toaddr
);
1363 FDLOG(LOG_ERR
, fd_cb
, "flow_divert_append_target_endpoint_tlv() failed: %d", error
);
1368 if (data_len
> 0 && data
!= NULL
) {
1369 last
= m_last(packet
);
1370 mbuf_setnext(last
, data
);
1371 mbuf_pkthdr_adjustlen(packet
, data_len
);
1373 error
= flow_divert_send_packet(fd_cb
, packet
, force
);
1376 mbuf_setnext(last
, NULL
);
1379 fd_cb
->bytes_sent
+= data_len
;
1380 flow_divert_add_data_statistics(fd_cb
, data_len
, TRUE
);
1387 flow_divert_send_buffered_data(struct flow_divert_pcb
*fd_cb
, Boolean force
)
1394 to_send
= fd_cb
->so
->so_snd
.sb_cc
;
1395 buffer
= fd_cb
->so
->so_snd
.sb_mb
;
1397 if (buffer
== NULL
&& to_send
> 0) {
1398 FDLOG(LOG_ERR
, fd_cb
, "Send buffer is NULL, but size is supposed to be %lu", to_send
);
1402 /* Ignore the send window if force is enabled */
1403 if (!force
&& (to_send
> fd_cb
->send_window
)) {
1404 to_send
= fd_cb
->send_window
;
1407 if (SOCK_TYPE(fd_cb
->so
) == SOCK_STREAM
) {
1408 while (sent
< to_send
) {
1412 data_len
= to_send
- sent
;
1413 if (data_len
> FLOW_DIVERT_CHUNK_SIZE
) {
1414 data_len
= FLOW_DIVERT_CHUNK_SIZE
;
1417 error
= mbuf_copym(buffer
, sent
, data_len
, MBUF_DONTWAIT
, &data
);
1419 FDLOG(LOG_ERR
, fd_cb
, "mbuf_copym failed: %d", error
);
1423 error
= flow_divert_send_data_packet(fd_cb
, data
, data_len
, NULL
, force
);
1431 sbdrop(&fd_cb
->so
->so_snd
, sent
);
1432 sowwakeup(fd_cb
->so
);
1433 } else if (SOCK_TYPE(fd_cb
->so
) == SOCK_DGRAM
) {
1439 struct sockaddr
*toaddr
= flow_divert_get_buffered_target_address(buffer
);
1442 if (toaddr
!= NULL
) {
1443 /* look for data in the chain */
1446 if (m
!= NULL
&& m
->m_type
== MT_DATA
) {
1452 FDLOG0(LOG_ERR
, fd_cb
, "failed to find type MT_DATA in the mbuf chain.");
1456 data_len
= mbuf_pkthdr_len(m
);
1458 FDLOG(LOG_DEBUG
, fd_cb
, "mbuf_copym() data_len = %lu", data_len
);
1459 error
= mbuf_copym(m
, 0, data_len
, MBUF_DONTWAIT
, &data
);
1461 FDLOG(LOG_ERR
, fd_cb
, "mbuf_copym failed: %d", error
);
1467 error
= flow_divert_send_data_packet(fd_cb
, data
, data_len
, toaddr
, force
);
1474 buffer
= buffer
->m_nextpkt
;
1475 (void) sbdroprecord(&(fd_cb
->so
->so_snd
));
1480 FDLOG(LOG_DEBUG
, fd_cb
, "sent %lu bytes of buffered data", sent
);
1481 if (fd_cb
->send_window
>= sent
) {
1482 fd_cb
->send_window
-= sent
;
1484 fd_cb
->send_window
= 0;
1490 flow_divert_send_app_data(struct flow_divert_pcb
*fd_cb
, mbuf_t data
, struct sockaddr
*toaddr
)
1492 size_t to_send
= mbuf_pkthdr_len(data
);
1495 if (to_send
> fd_cb
->send_window
) {
1496 to_send
= fd_cb
->send_window
;
1499 if (fd_cb
->so
->so_snd
.sb_cc
> 0) {
1500 to_send
= 0; /* If the send buffer is non-empty, then we can't send anything */
1503 if (SOCK_TYPE(fd_cb
->so
) == SOCK_STREAM
) {
1505 mbuf_t remaining_data
= data
;
1506 mbuf_t pkt_data
= NULL
;
1507 while (sent
< to_send
&& remaining_data
!= NULL
) {
1508 size_t pkt_data_len
;
1510 pkt_data
= remaining_data
;
1512 if ((to_send
- sent
) > FLOW_DIVERT_CHUNK_SIZE
) {
1513 pkt_data_len
= FLOW_DIVERT_CHUNK_SIZE
;
1515 pkt_data_len
= to_send
- sent
;
1518 if (pkt_data_len
< mbuf_pkthdr_len(pkt_data
)) {
1519 error
= mbuf_split(pkt_data
, pkt_data_len
, MBUF_DONTWAIT
, &remaining_data
);
1521 FDLOG(LOG_ERR
, fd_cb
, "mbuf_split failed: %d", error
);
1526 remaining_data
= NULL
;
1529 error
= flow_divert_send_data_packet(fd_cb
, pkt_data
, pkt_data_len
, NULL
, FALSE
);
1536 sent
+= pkt_data_len
;
1539 fd_cb
->send_window
-= sent
;
1543 if (pkt_data
!= NULL
) {
1544 if (sbspace(&fd_cb
->so
->so_snd
) > 0) {
1545 if (!sbappendstream(&fd_cb
->so
->so_snd
, pkt_data
)) {
1546 FDLOG(LOG_ERR
, fd_cb
, "sbappendstream failed with pkt_data, send buffer size = %u, send_window = %u\n",
1547 fd_cb
->so
->so_snd
.sb_cc
, fd_cb
->send_window
);
1554 if (remaining_data
!= NULL
) {
1555 if (sbspace(&fd_cb
->so
->so_snd
) > 0) {
1556 if (!sbappendstream(&fd_cb
->so
->so_snd
, remaining_data
)) {
1557 FDLOG(LOG_ERR
, fd_cb
, "sbappendstream failed with remaining_data, send buffer size = %u, send_window = %u\n",
1558 fd_cb
->so
->so_snd
.sb_cc
, fd_cb
->send_window
);
1564 } else if (SOCK_TYPE(fd_cb
->so
) == SOCK_DGRAM
) {
1565 if (to_send
|| mbuf_pkthdr_len(data
) == 0) {
1566 error
= flow_divert_send_data_packet(fd_cb
, data
, to_send
, toaddr
, FALSE
);
1568 FDLOG(LOG_ERR
, fd_cb
, "flow_divert_send_data_packet failed. send data size = %lu", to_send
);
1570 fd_cb
->send_window
-= to_send
;
1574 if (sbspace(&fd_cb
->so
->so_snd
) >= (int)mbuf_pkthdr_len(data
)) {
1575 if (toaddr
!= NULL
) {
1576 if (!sbappendaddr(&fd_cb
->so
->so_snd
, toaddr
, data
, NULL
, &error
)) {
1577 FDLOG(LOG_ERR
, fd_cb
,
1578 "sbappendaddr failed. send buffer size = %u, send_window = %u, error = %d\n",
1579 fd_cb
->so
->so_snd
.sb_cc
, fd_cb
->send_window
, error
);
1582 if (!sbappendrecord(&fd_cb
->so
->so_snd
, data
)) {
1583 FDLOG(LOG_ERR
, fd_cb
,
1584 "sbappendrecord failed. send buffer size = %u, send_window = %u, error = %d\n",
1585 fd_cb
->so
->so_snd
.sb_cc
, fd_cb
->send_window
, error
);
1598 flow_divert_send_read_notification(struct flow_divert_pcb
*fd_cb
, uint32_t read_count
)
1601 mbuf_t packet
= NULL
;
1602 uint32_t net_read_count
= htonl(read_count
);
1604 error
= flow_divert_packet_init(fd_cb
, FLOW_DIVERT_PKT_READ_NOTIFY
, &packet
);
1606 FDLOG(LOG_ERR
, fd_cb
, "failed to create a read notification packet: %d", error
);
1610 error
= flow_divert_packet_append_tlv(packet
, FLOW_DIVERT_TLV_READ_COUNT
, sizeof(net_read_count
), &net_read_count
);
1612 FDLOG(LOG_ERR
, fd_cb
, "failed to add the read count: %d", error
);
1616 error
= flow_divert_send_packet(fd_cb
, packet
, TRUE
);
1622 if (error
&& packet
!= NULL
) {
1630 flow_divert_send_traffic_class_update(struct flow_divert_pcb
*fd_cb
, int traffic_class
)
1633 mbuf_t packet
= NULL
;
1635 error
= flow_divert_packet_init(fd_cb
, FLOW_DIVERT_PKT_PROPERTIES_UPDATE
, &packet
);
1637 FDLOG(LOG_ERR
, fd_cb
, "failed to create a properties update packet: %d", error
);
1641 error
= flow_divert_packet_append_tlv(packet
, FLOW_DIVERT_TLV_TRAFFIC_CLASS
, sizeof(traffic_class
), &traffic_class
);
1643 FDLOG(LOG_ERR
, fd_cb
, "failed to add the traffic class: %d", error
);
1647 error
= flow_divert_send_packet(fd_cb
, packet
, TRUE
);
1653 if (error
&& packet
!= NULL
) {
1661 flow_divert_handle_connect_result(struct flow_divert_pcb
*fd_cb
, mbuf_t packet
, int offset
)
1663 uint32_t connect_error
;
1664 uint32_t ctl_unit
= 0;
1666 struct flow_divert_group
*grp
= NULL
;
1667 struct sockaddr_storage local_address
;
1668 int out_if_index
= 0;
1669 struct sockaddr_storage remote_address
;
1670 uint32_t send_window
;
1671 uint32_t app_data_length
= 0;
1673 memset(&local_address
, 0, sizeof(local_address
));
1674 memset(&remote_address
, 0, sizeof(remote_address
));
1676 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_ERROR_CODE
, sizeof(connect_error
), &connect_error
, NULL
);
1678 FDLOG(LOG_ERR
, fd_cb
, "failed to get the connect result: %d", error
);
1682 FDLOG(LOG_INFO
, fd_cb
, "received connect result %u", connect_error
);
1684 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_SPACE_AVAILABLE
, sizeof(send_window
), &send_window
, NULL
);
1686 FDLOG(LOG_ERR
, fd_cb
, "failed to get the send window: %d", error
);
1690 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_CTL_UNIT
, sizeof(ctl_unit
), &ctl_unit
, NULL
);
1692 FDLOG0(LOG_INFO
, fd_cb
, "No control unit provided in the connect result");
1695 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_LOCAL_ADDR
, sizeof(local_address
), &local_address
, NULL
);
1697 FDLOG0(LOG_INFO
, fd_cb
, "No local address provided");
1700 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_REMOTE_ADDR
, sizeof(remote_address
), &remote_address
, NULL
);
1702 FDLOG0(LOG_INFO
, fd_cb
, "No remote address provided");
1705 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_OUT_IF_INDEX
, sizeof(out_if_index
), &out_if_index
, NULL
);
1707 FDLOG0(LOG_INFO
, fd_cb
, "No output if index provided");
1710 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_APP_DATA
, 0, NULL
, &app_data_length
);
1712 FDLOG0(LOG_INFO
, fd_cb
, "No application data provided in connect result");
1716 connect_error
= ntohl(connect_error
);
1717 ctl_unit
= ntohl(ctl_unit
);
1719 lck_rw_lock_shared(&g_flow_divert_group_lck
);
1721 if (connect_error
== 0 && ctl_unit
> 0) {
1722 if (ctl_unit
>= GROUP_COUNT_MAX
) {
1723 FDLOG(LOG_ERR
, fd_cb
, "Connect result contains an invalid control unit: %u", ctl_unit
);
1725 } else if (g_flow_divert_groups
== NULL
|| g_active_group_count
== 0) {
1726 FDLOG0(LOG_ERR
, fd_cb
, "No active groups, dropping connection");
1729 grp
= g_flow_divert_groups
[ctl_unit
];
1737 if (fd_cb
->so
!= NULL
) {
1738 struct inpcb
*inp
= NULL
;
1739 struct ifnet
*ifp
= NULL
;
1740 struct flow_divert_group
*old_group
;
1742 socket_lock(fd_cb
->so
, 0);
1744 if (!(fd_cb
->so
->so_state
& SS_ISCONNECTING
)) {
1748 inp
= sotoinpcb(fd_cb
->so
);
1750 if (connect_error
|| error
) {
1751 goto set_socket_state
;
1754 if (local_address
.ss_family
== 0 && fd_cb
->local_address
== NULL
) {
1756 goto set_socket_state
;
1758 if (local_address
.ss_family
!= 0 && fd_cb
->local_address
== NULL
) {
1759 if (local_address
.ss_len
> sizeof(local_address
)) {
1760 local_address
.ss_len
= sizeof(local_address
);
1762 fd_cb
->local_address
= dup_sockaddr((struct sockaddr
*)&local_address
, 1);
1765 if (remote_address
.ss_family
!= 0) {
1766 if (remote_address
.ss_len
> sizeof(remote_address
)) {
1767 remote_address
.ss_len
= sizeof(remote_address
);
1769 fd_cb
->remote_address
= dup_sockaddr((struct sockaddr
*)&remote_address
, 1);
1772 goto set_socket_state
;
1775 if (app_data_length
> 0) {
1776 uint8_t *app_data
= NULL
;
1777 MALLOC(app_data
, uint8_t *, app_data_length
, M_TEMP
, M_WAITOK
);
1778 if (app_data
!= NULL
) {
1779 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_APP_DATA
, app_data_length
, app_data
, NULL
);
1781 FDLOG(LOG_INFO
, fd_cb
, "Got %u bytes of app data from the connect result", app_data_length
);
1782 if (fd_cb
->app_data
!= NULL
) {
1783 FREE(fd_cb
->app_data
, M_TEMP
);
1785 fd_cb
->app_data
= app_data
;
1786 fd_cb
->app_data_length
= app_data_length
;
1788 FDLOG(LOG_ERR
, fd_cb
, "Failed to copy %u bytes of application data from the connect result packet", app_data_length
);
1789 FREE(app_data
, M_TEMP
);
1792 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
);
1796 ifnet_head_lock_shared();
1797 if (out_if_index
> 0 && out_if_index
<= if_index
) {
1798 ifp
= ifindex2ifnet
[out_if_index
];
1802 inp
->inp_last_outifp
= ifp
;
1809 goto set_socket_state
;
1812 if (fd_cb
->group
== NULL
) {
1814 goto set_socket_state
;
1818 old_group
= fd_cb
->group
;
1820 lck_rw_lock_exclusive(&old_group
->lck
);
1821 lck_rw_lock_exclusive(&grp
->lck
);
1823 RB_REMOVE(fd_pcb_tree
, &old_group
->pcb_tree
, fd_cb
);
1824 if (RB_INSERT(fd_pcb_tree
, &grp
->pcb_tree
, fd_cb
) != NULL
) {
1825 panic("group with unit %u already contains a connection with hash %u", grp
->ctl_unit
, fd_cb
->hash
);
1830 lck_rw_done(&grp
->lck
);
1831 lck_rw_done(&old_group
->lck
);
1834 fd_cb
->send_window
= ntohl(send_window
);
1837 if (!connect_error
&& !error
) {
1838 FDLOG0(LOG_INFO
, fd_cb
, "sending connect result");
1839 error
= flow_divert_send_connect_result(fd_cb
);
1842 if (connect_error
|| error
) {
1843 if (!connect_error
) {
1844 flow_divert_update_closed_state(fd_cb
, SHUT_RDWR
, FALSE
);
1845 fd_cb
->so
->so_error
= error
;
1846 flow_divert_send_close_if_needed(fd_cb
);
1848 flow_divert_update_closed_state(fd_cb
, SHUT_RDWR
, TRUE
);
1849 fd_cb
->so
->so_error
= connect_error
;
1851 flow_divert_disconnect_socket(fd_cb
->so
);
1853 flow_divert_send_buffered_data(fd_cb
, FALSE
);
1854 soisconnected(fd_cb
->so
);
1858 socket_unlock(fd_cb
->so
, 0);
1862 lck_rw_done(&g_flow_divert_group_lck
);
1866 flow_divert_handle_close(struct flow_divert_pcb
*fd_cb
, mbuf_t packet
, int offset
)
1868 uint32_t close_error
;
1872 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_ERROR_CODE
, sizeof(close_error
), &close_error
, NULL
);
1874 FDLOG(LOG_ERR
, fd_cb
, "failed to get the close error: %d", error
);
1878 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_HOW
, sizeof(how
), &how
, NULL
);
1880 FDLOG(LOG_ERR
, fd_cb
, "failed to get the close how flag: %d", error
);
1886 FDLOG(LOG_INFO
, fd_cb
, "close received, how = %d", how
);
1889 if (fd_cb
->so
!= NULL
) {
1890 socket_lock(fd_cb
->so
, 0);
1892 fd_cb
->so
->so_error
= ntohl(close_error
);
1894 flow_divert_update_closed_state(fd_cb
, how
, TRUE
);
1896 how
= flow_divert_tunnel_how_closed(fd_cb
);
1897 if (how
== SHUT_RDWR
) {
1898 flow_divert_disconnect_socket(fd_cb
->so
);
1899 } else if (how
== SHUT_RD
) {
1900 socantrcvmore(fd_cb
->so
);
1901 } else if (how
== SHUT_WR
) {
1902 socantsendmore(fd_cb
->so
);
1905 socket_unlock(fd_cb
->so
, 0);
1911 flow_divert_get_control_mbuf(struct flow_divert_pcb
*fd_cb
)
1913 struct inpcb
*inp
= sotoinpcb(fd_cb
->so
);
1914 if (inp
->inp_vflag
& INP_IPV4
&& inp
->inp_flags
& INP_RECVDSTADDR
) {
1915 struct sockaddr_in
*sin
= (struct sockaddr_in
*)(void *)fd_cb
->local_address
;
1917 return sbcreatecontrol((caddr_t
) &sin
->sin_addr
, sizeof(struct in_addr
), IP_RECVDSTADDR
, IPPROTO_IP
);
1918 } else if (inp
->inp_vflag
& INP_IPV6
&& (inp
->inp_flags
& IN6P_PKTINFO
) != 0) {
1919 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)(void *)fd_cb
->local_address
;
1920 struct in6_pktinfo pi6
;
1922 bcopy(&sin6
->sin6_addr
, &pi6
.ipi6_addr
, sizeof (struct in6_addr
));
1923 pi6
.ipi6_ifindex
= 0;
1924 return sbcreatecontrol((caddr_t
)&pi6
, sizeof (struct in6_pktinfo
), IPV6_PKTINFO
, IPPROTO_IPV6
);
1930 flow_divert_handle_data(struct flow_divert_pcb
*fd_cb
, mbuf_t packet
, size_t offset
)
1933 if (fd_cb
->so
!= NULL
) {
1937 struct sockaddr_storage remote_address
;
1938 boolean_t got_remote_sa
= FALSE
;
1940 socket_lock(fd_cb
->so
, 0);
1942 if (SOCK_TYPE(fd_cb
->so
) == SOCK_DGRAM
) {
1943 uint32_t val_size
= 0;
1945 /* check if we got remote address with data */
1946 memset(&remote_address
, 0, sizeof(remote_address
));
1947 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_REMOTE_ADDR
, sizeof(remote_address
), &remote_address
, &val_size
);
1948 if (error
|| val_size
> sizeof(remote_address
)) {
1949 FDLOG0(LOG_INFO
, fd_cb
, "No remote address provided");
1952 /* validate the address */
1953 if (flow_divert_is_sockaddr_valid((struct sockaddr
*)&remote_address
)) {
1954 got_remote_sa
= TRUE
;
1956 offset
+= (sizeof(uint8_t) + sizeof(uint32_t) + val_size
);
1960 data_size
= (mbuf_pkthdr_len(packet
) - offset
);
1962 FDLOG(LOG_DEBUG
, fd_cb
, "received %lu bytes of data", data_size
);
1964 error
= mbuf_split(packet
, offset
, MBUF_DONTWAIT
, &data
);
1965 if (error
|| data
== NULL
) {
1966 FDLOG(LOG_ERR
, fd_cb
, "mbuf_split failed: %d", error
);
1968 if (flow_divert_check_no_cellular(fd_cb
) ||
1969 flow_divert_check_no_expensive(fd_cb
))
1971 flow_divert_update_closed_state(fd_cb
, SHUT_RDWR
, TRUE
);
1972 flow_divert_send_close(fd_cb
, SHUT_RDWR
);
1973 flow_divert_disconnect_socket(fd_cb
->so
);
1974 } else if (!(fd_cb
->so
->so_state
& SS_CANTRCVMORE
)) {
1975 if (SOCK_TYPE(fd_cb
->so
) == SOCK_STREAM
) {
1976 if (sbappendstream(&fd_cb
->so
->so_rcv
, data
)) {
1977 fd_cb
->bytes_received
+= data_size
;
1978 flow_divert_add_data_statistics(fd_cb
, data_size
, FALSE
);
1979 fd_cb
->sb_size
= fd_cb
->so
->so_rcv
.sb_cc
;
1980 sorwakeup(fd_cb
->so
);
1983 FDLOG0(LOG_ERR
, fd_cb
, "received data, but appendstream failed");
1985 } else if (SOCK_TYPE(fd_cb
->so
) == SOCK_DGRAM
) {
1986 struct sockaddr
*append_sa
;
1989 if (got_remote_sa
== TRUE
) {
1990 error
= flow_divert_dup_addr(fd_cb
->so
->so_proto
->pr_domain
->dom_family
,
1991 (struct sockaddr
*)&remote_address
, &append_sa
);
1993 error
= flow_divert_dup_addr(fd_cb
->so
->so_proto
->pr_domain
->dom_family
,
1994 fd_cb
->remote_address
, &append_sa
);
1997 FDLOG0(LOG_ERR
, fd_cb
, "failed to dup the socket address.");
2000 mctl
= flow_divert_get_control_mbuf(fd_cb
);
2001 if (sbappendaddr(&fd_cb
->so
->so_rcv
, append_sa
, data
, mctl
, NULL
)) {
2002 fd_cb
->bytes_received
+= data_size
;
2003 flow_divert_add_data_statistics(fd_cb
, data_size
, FALSE
);
2004 fd_cb
->sb_size
= fd_cb
->so
->so_rcv
.sb_cc
;
2005 sorwakeup(fd_cb
->so
);
2008 FDLOG0(LOG_ERR
, fd_cb
, "received data, but sbappendaddr failed");
2011 FREE(append_sa
, M_TEMP
);
2016 socket_unlock(fd_cb
->so
, 0);
2026 flow_divert_handle_read_notification(struct flow_divert_pcb
*fd_cb
, mbuf_t packet
, int offset
)
2028 uint32_t read_count
;
2031 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_READ_COUNT
, sizeof(read_count
), &read_count
, NULL
);
2033 FDLOG(LOG_ERR
, fd_cb
, "failed to get the read count: %d", error
);
2037 FDLOG(LOG_DEBUG
, fd_cb
, "received a read notification for %u bytes", ntohl(read_count
));
2040 if (fd_cb
->so
!= NULL
) {
2041 socket_lock(fd_cb
->so
, 0);
2042 fd_cb
->send_window
+= ntohl(read_count
);
2043 flow_divert_send_buffered_data(fd_cb
, FALSE
);
2044 socket_unlock(fd_cb
->so
, 0);
2050 flow_divert_handle_group_init(struct flow_divert_group
*group
, mbuf_t packet
, int offset
)
2053 uint32_t key_size
= 0;
2057 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_TOKEN_KEY
, 0, NULL
, &key_size
);
2059 FDLOG(LOG_ERR
, &nil_pcb
, "failed to get the key size: %d", error
);
2063 if (key_size
== 0 || key_size
> FLOW_DIVERT_MAX_KEY_SIZE
) {
2064 FDLOG(LOG_ERR
, &nil_pcb
, "Invalid key size: %u", key_size
);
2068 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_LOG_LEVEL
, sizeof(log_level
), &log_level
, NULL
);
2070 nil_pcb
.log_level
= log_level
;
2073 lck_rw_lock_exclusive(&group
->lck
);
2075 MALLOC(group
->token_key
, uint8_t *, key_size
, M_TEMP
, M_WAITOK
);
2076 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_TOKEN_KEY
, key_size
, group
->token_key
, NULL
);
2078 FDLOG(LOG_ERR
, &nil_pcb
, "failed to get the token key: %d", error
);
2079 FREE(group
->token_key
, M_TEMP
);
2080 group
->token_key
= NULL
;
2081 lck_rw_done(&group
->lck
);
2085 group
->token_key_size
= key_size
;
2087 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_FLAGS
, sizeof(flags
), &flags
, NULL
);
2089 group
->flags
= flags
;
2092 lck_rw_done(&group
->lck
);
2096 flow_divert_handle_properties_update(struct flow_divert_pcb
*fd_cb
, mbuf_t packet
, int offset
)
2099 struct sockaddr_storage local_address
;
2100 int out_if_index
= 0;
2101 struct sockaddr_storage remote_address
;
2102 uint32_t app_data_length
= 0;
2104 FDLOG0(LOG_INFO
, fd_cb
, "received a properties update");
2106 memset(&local_address
, 0, sizeof(local_address
));
2107 memset(&remote_address
, 0, sizeof(remote_address
));
2109 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_LOCAL_ADDR
, sizeof(local_address
), &local_address
, NULL
);
2111 FDLOG0(LOG_INFO
, fd_cb
, "No local address provided in properties update");
2114 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_REMOTE_ADDR
, sizeof(remote_address
), &remote_address
, NULL
);
2116 FDLOG0(LOG_INFO
, fd_cb
, "No remote address provided in properties update");
2119 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_OUT_IF_INDEX
, sizeof(out_if_index
), &out_if_index
, NULL
);
2121 FDLOG0(LOG_INFO
, fd_cb
, "No output if index provided in properties update");
2124 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_APP_DATA
, 0, NULL
, &app_data_length
);
2126 FDLOG0(LOG_INFO
, fd_cb
, "No application data provided in properties update");
2130 if (fd_cb
->so
!= NULL
) {
2131 socket_lock(fd_cb
->so
, 0);
2133 if (local_address
.ss_family
!= 0) {
2134 if (local_address
.ss_len
> sizeof(local_address
)) {
2135 local_address
.ss_len
= sizeof(local_address
);
2137 if (fd_cb
->local_address
!= NULL
) {
2138 FREE(fd_cb
->local_address
, M_SONAME
);
2139 fd_cb
->local_address
= NULL
;
2141 fd_cb
->local_address
= dup_sockaddr((struct sockaddr
*)&local_address
, 1);
2144 if (remote_address
.ss_family
!= 0) {
2145 if (remote_address
.ss_len
> sizeof(remote_address
)) {
2146 remote_address
.ss_len
= sizeof(remote_address
);
2148 if (fd_cb
->remote_address
!= NULL
) {
2149 FREE(fd_cb
->remote_address
, M_SONAME
);
2150 fd_cb
->remote_address
= NULL
;
2152 fd_cb
->remote_address
= dup_sockaddr((struct sockaddr
*)&remote_address
, 1);
2155 if (out_if_index
> 0) {
2156 struct inpcb
*inp
= NULL
;
2157 struct ifnet
*ifp
= NULL
;
2159 inp
= sotoinpcb(fd_cb
->so
);
2161 ifnet_head_lock_shared();
2162 if (out_if_index
<= if_index
) {
2163 ifp
= ifindex2ifnet
[out_if_index
];
2167 inp
->inp_last_outifp
= ifp
;
2172 if (app_data_length
> 0) {
2173 uint8_t *app_data
= NULL
;
2174 MALLOC(app_data
, uint8_t *, app_data_length
, M_TEMP
, M_WAITOK
);
2175 if (app_data
!= NULL
) {
2176 error
= flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_APP_DATA
, app_data_length
, app_data
, NULL
);
2178 if (fd_cb
->app_data
!= NULL
) {
2179 FREE(fd_cb
->app_data
, M_TEMP
);
2181 fd_cb
->app_data
= app_data
;
2182 fd_cb
->app_data_length
= app_data_length
;
2184 FDLOG(LOG_ERR
, fd_cb
, "Failed to copy %u bytes of application data from the properties update packet", app_data_length
);
2185 FREE(app_data
, M_TEMP
);
2188 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
);
2192 socket_unlock(fd_cb
->so
, 0);
2198 flow_divert_handle_app_map_create(struct flow_divert_group
*group
, mbuf_t packet
, int offset
)
2200 size_t bytes_mem_size
;
2201 size_t child_maps_mem_size
;
2204 struct flow_divert_trie new_trie
;
2205 int insert_error
= 0;
2206 size_t nodes_mem_size
;
2207 int prefix_count
= 0;
2208 int signing_id_count
= 0;
2209 size_t trie_memory_size
= 0;
2211 lck_rw_lock_exclusive(&group
->lck
);
2213 /* Re-set the current trie */
2214 if (group
->signing_id_trie
.memory
!= NULL
) {
2215 FREE(group
->signing_id_trie
.memory
, M_TEMP
);
2217 memset(&group
->signing_id_trie
, 0, sizeof(group
->signing_id_trie
));
2218 group
->signing_id_trie
.root
= NULL_TRIE_IDX
;
2220 memset(&new_trie
, 0, sizeof(new_trie
));
2222 /* Get the number of shared prefixes in the new set of signing ID strings */
2223 flow_divert_packet_get_tlv(packet
, offset
, FLOW_DIVERT_TLV_PREFIX_COUNT
, sizeof(prefix_count
), &prefix_count
, NULL
);
2225 if (prefix_count
< 0) {
2226 lck_rw_done(&group
->lck
);
2230 /* Compute the number of signing IDs and the total amount of bytes needed to store them */
2231 for (cursor
= flow_divert_packet_find_tlv(packet
, offset
, FLOW_DIVERT_TLV_SIGNING_ID
, &error
, 0);
2233 cursor
= flow_divert_packet_find_tlv(packet
, cursor
, FLOW_DIVERT_TLV_SIGNING_ID
, &error
, 1))
2235 uint32_t sid_size
= 0;
2236 flow_divert_packet_get_tlv(packet
, cursor
, FLOW_DIVERT_TLV_SIGNING_ID
, 0, NULL
, &sid_size
);
2237 new_trie
.bytes_count
+= sid_size
;
2241 if (signing_id_count
== 0) {
2242 lck_rw_done(&group
->lck
);
2246 new_trie
.nodes_count
= (prefix_count
+ signing_id_count
+ 1); /* + 1 for the root node */
2247 new_trie
.child_maps_count
= (prefix_count
+ 1); /* + 1 for the root node */
2249 FDLOG(LOG_INFO
, &nil_pcb
, "Nodes count = %lu, child maps count = %lu, bytes_count = %lu",
2250 new_trie
.nodes_count
, new_trie
.child_maps_count
, new_trie
.bytes_count
);
2252 nodes_mem_size
= (sizeof(*new_trie
.nodes
) * new_trie
.nodes_count
);
2253 child_maps_mem_size
= (sizeof(*new_trie
.child_maps
) * CHILD_MAP_SIZE
* new_trie
.child_maps_count
);
2254 bytes_mem_size
= (sizeof(*new_trie
.bytes
) * new_trie
.bytes_count
);
2256 trie_memory_size
= nodes_mem_size
+ child_maps_mem_size
+ bytes_mem_size
;
2257 if (trie_memory_size
> FLOW_DIVERT_MAX_TRIE_MEMORY
) {
2258 FDLOG(LOG_ERR
, &nil_pcb
, "Trie memory size (%lu) is too big (maximum is %u)", trie_memory_size
, FLOW_DIVERT_MAX_TRIE_MEMORY
);
2259 lck_rw_done(&group
->lck
);
2263 MALLOC(new_trie
.memory
, void *, trie_memory_size
, M_TEMP
, M_WAITOK
);
2264 if (new_trie
.memory
== NULL
) {
2265 FDLOG(LOG_ERR
, &nil_pcb
, "Failed to allocate %lu bytes of memory for the signing ID trie",
2266 nodes_mem_size
+ child_maps_mem_size
+ bytes_mem_size
);
2267 lck_rw_done(&group
->lck
);
2271 /* Initialize the free lists */
2272 new_trie
.nodes
= (struct flow_divert_trie_node
*)new_trie
.memory
;
2273 new_trie
.nodes_free_next
= 0;
2274 memset(new_trie
.nodes
, 0, nodes_mem_size
);
2276 new_trie
.child_maps
= (uint16_t *)(void *)((uint8_t *)new_trie
.memory
+ nodes_mem_size
);
2277 new_trie
.child_maps_free_next
= 0;
2278 memset(new_trie
.child_maps
, 0xff, child_maps_mem_size
);
2280 new_trie
.bytes
= (uint8_t *)(void *)((uint8_t *)new_trie
.memory
+ nodes_mem_size
+ child_maps_mem_size
);
2281 new_trie
.bytes_free_next
= 0;
2283 /* The root is an empty node */
2284 new_trie
.root
= trie_node_alloc(&new_trie
);
2286 /* Add each signing ID to the trie */
2287 for (cursor
= flow_divert_packet_find_tlv(packet
, offset
, FLOW_DIVERT_TLV_SIGNING_ID
, &error
, 0);
2289 cursor
= flow_divert_packet_find_tlv(packet
, cursor
, FLOW_DIVERT_TLV_SIGNING_ID
, &error
, 1))
2291 uint32_t sid_size
= 0;
2292 flow_divert_packet_get_tlv(packet
, cursor
, FLOW_DIVERT_TLV_SIGNING_ID
, 0, NULL
, &sid_size
);
2293 if (new_trie
.bytes_free_next
+ sid_size
<= new_trie
.bytes_count
) {
2294 uint16_t new_node_idx
;
2295 flow_divert_packet_get_tlv(packet
, cursor
, FLOW_DIVERT_TLV_SIGNING_ID
, sid_size
, &TRIE_BYTE(&new_trie
, new_trie
.bytes_free_next
), NULL
);
2296 new_node_idx
= flow_divert_trie_insert(&new_trie
, new_trie
.bytes_free_next
, sid_size
);
2297 if (new_node_idx
== NULL_TRIE_IDX
) {
2298 insert_error
= EINVAL
;
2302 FDLOG0(LOG_ERR
, &nil_pcb
, "No place to put signing ID for insertion");
2303 insert_error
= ENOBUFS
;
2308 if (!insert_error
) {
2309 group
->signing_id_trie
= new_trie
;
2311 FREE(new_trie
.memory
, M_TEMP
);
2314 lck_rw_done(&group
->lck
);
2318 flow_divert_input(mbuf_t packet
, struct flow_divert_group
*group
)
2320 struct flow_divert_packet_header hdr
;
2322 struct flow_divert_pcb
*fd_cb
;
2324 if (mbuf_pkthdr_len(packet
) < sizeof(hdr
)) {
2325 FDLOG(LOG_ERR
, &nil_pcb
, "got a bad packet, length (%lu) < sizeof hdr (%lu)", mbuf_pkthdr_len(packet
), sizeof(hdr
));
2330 if (mbuf_pkthdr_len(packet
) > FD_CTL_RCVBUFF_SIZE
) {
2331 FDLOG(LOG_ERR
, &nil_pcb
, "got a bad packet, length (%lu) > %d", mbuf_pkthdr_len(packet
), FD_CTL_RCVBUFF_SIZE
);
2336 error
= mbuf_copydata(packet
, 0, sizeof(hdr
), &hdr
);
2338 FDLOG(LOG_ERR
, &nil_pcb
, "mbuf_copydata failed for the header: %d", error
);
2343 hdr
.conn_id
= ntohl(hdr
.conn_id
);
2345 if (hdr
.conn_id
== 0) {
2346 switch (hdr
.packet_type
) {
2347 case FLOW_DIVERT_PKT_GROUP_INIT
:
2348 flow_divert_handle_group_init(group
, packet
, sizeof(hdr
));
2350 case FLOW_DIVERT_PKT_APP_MAP_CREATE
:
2351 flow_divert_handle_app_map_create(group
, packet
, sizeof(hdr
));
2354 FDLOG(LOG_WARNING
, &nil_pcb
, "got an unknown message type: %d", hdr
.packet_type
);
2360 fd_cb
= flow_divert_pcb_lookup(hdr
.conn_id
, group
); /* This retains the PCB */
2361 if (fd_cb
== NULL
) {
2362 if (hdr
.packet_type
!= FLOW_DIVERT_PKT_CLOSE
&& hdr
.packet_type
!= FLOW_DIVERT_PKT_READ_NOTIFY
) {
2363 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
);
2368 switch (hdr
.packet_type
) {
2369 case FLOW_DIVERT_PKT_CONNECT_RESULT
:
2370 flow_divert_handle_connect_result(fd_cb
, packet
, sizeof(hdr
));
2372 case FLOW_DIVERT_PKT_CLOSE
:
2373 flow_divert_handle_close(fd_cb
, packet
, sizeof(hdr
));
2375 case FLOW_DIVERT_PKT_DATA
:
2376 flow_divert_handle_data(fd_cb
, packet
, sizeof(hdr
));
2378 case FLOW_DIVERT_PKT_READ_NOTIFY
:
2379 flow_divert_handle_read_notification(fd_cb
, packet
, sizeof(hdr
));
2381 case FLOW_DIVERT_PKT_PROPERTIES_UPDATE
:
2382 flow_divert_handle_properties_update(fd_cb
, packet
, sizeof(hdr
));
2385 FDLOG(LOG_WARNING
, fd_cb
, "got an unknown message type: %d", hdr
.packet_type
);
2397 flow_divert_close_all(struct flow_divert_group
*group
)
2399 struct flow_divert_pcb
*fd_cb
;
2400 SLIST_HEAD(, flow_divert_pcb
) tmp_list
;
2402 SLIST_INIT(&tmp_list
);
2404 lck_rw_lock_exclusive(&group
->lck
);
2406 MBUFQ_DRAIN(&group
->send_queue
);
2408 RB_FOREACH(fd_cb
, fd_pcb_tree
, &group
->pcb_tree
) {
2410 SLIST_INSERT_HEAD(&tmp_list
, fd_cb
, tmp_list_entry
);
2413 lck_rw_done(&group
->lck
);
2415 while (!SLIST_EMPTY(&tmp_list
)) {
2416 fd_cb
= SLIST_FIRST(&tmp_list
);
2418 SLIST_REMOVE_HEAD(&tmp_list
, tmp_list_entry
);
2419 if (fd_cb
->so
!= NULL
) {
2420 socket_lock(fd_cb
->so
, 0);
2421 flow_divert_pcb_remove(fd_cb
);
2422 flow_divert_update_closed_state(fd_cb
, SHUT_RDWR
, TRUE
);
2423 fd_cb
->so
->so_error
= ECONNABORTED
;
2424 flow_divert_disconnect_socket(fd_cb
->so
);
2425 socket_unlock(fd_cb
->so
, 0);
2433 flow_divert_detach(struct socket
*so
)
2435 struct flow_divert_pcb
*fd_cb
= so
->so_fd_pcb
;
2437 VERIFY((so
->so_flags
& SOF_FLOW_DIVERT
) && so
->so_fd_pcb
!= NULL
);
2439 so
->so_flags
&= ~SOF_FLOW_DIVERT
;
2440 so
->so_fd_pcb
= NULL
;
2442 FDLOG(LOG_INFO
, fd_cb
, "Detaching, ref count = %d", fd_cb
->ref_count
);
2444 if (fd_cb
->group
!= NULL
) {
2445 /* Last-ditch effort to send any buffered data */
2446 flow_divert_send_buffered_data(fd_cb
, TRUE
);
2448 flow_divert_update_closed_state(fd_cb
, SHUT_RDWR
, FALSE
);
2449 flow_divert_send_close_if_needed(fd_cb
);
2450 /* Remove from the group */
2451 flow_divert_pcb_remove(fd_cb
);
2454 socket_unlock(so
, 0);
2460 FDRELEASE(fd_cb
); /* Release the socket's reference */
2464 flow_divert_close(struct socket
*so
)
2466 struct flow_divert_pcb
*fd_cb
= so
->so_fd_pcb
;
2468 VERIFY((so
->so_flags
& SOF_FLOW_DIVERT
) && so
->so_fd_pcb
!= NULL
);
2470 FDLOG0(LOG_INFO
, fd_cb
, "Closing");
2472 if (SOCK_TYPE(so
) == SOCK_STREAM
) {
2473 soisdisconnecting(so
);
2474 sbflush(&so
->so_rcv
);
2477 flow_divert_send_buffered_data(fd_cb
, TRUE
);
2478 flow_divert_update_closed_state(fd_cb
, SHUT_RDWR
, FALSE
);
2479 flow_divert_send_close_if_needed(fd_cb
);
2481 /* Remove from the group */
2482 flow_divert_pcb_remove(fd_cb
);
2488 flow_divert_disconnectx(struct socket
*so
, sae_associd_t aid
,
2489 sae_connid_t cid __unused
)
2491 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
) {
2495 return (flow_divert_close(so
));
2499 flow_divert_shutdown(struct socket
*so
)
2501 struct flow_divert_pcb
*fd_cb
= so
->so_fd_pcb
;
2503 VERIFY((so
->so_flags
& SOF_FLOW_DIVERT
) && so
->so_fd_pcb
!= NULL
);
2505 FDLOG0(LOG_INFO
, fd_cb
, "Can't send more");
2509 flow_divert_update_closed_state(fd_cb
, SHUT_WR
, FALSE
);
2510 flow_divert_send_close_if_needed(fd_cb
);
2516 flow_divert_rcvd(struct socket
*so
, int flags __unused
)
2518 struct flow_divert_pcb
*fd_cb
= so
->so_fd_pcb
;
2519 uint32_t latest_sb_size
;
2520 uint32_t read_count
;
2522 VERIFY((so
->so_flags
& SOF_FLOW_DIVERT
) && so
->so_fd_pcb
!= NULL
);
2524 latest_sb_size
= fd_cb
->so
->so_rcv
.sb_cc
;
2526 if (fd_cb
->sb_size
< latest_sb_size
) {
2527 panic("flow divert rcvd event handler (%u): saved rcv buffer size (%u) is less than latest rcv buffer size (%u)",
2528 fd_cb
->hash
, fd_cb
->sb_size
, latest_sb_size
);
2531 read_count
= fd_cb
->sb_size
- latest_sb_size
;
2533 FDLOG(LOG_DEBUG
, fd_cb
, "app read %u bytes", read_count
);
2535 if (read_count
> 0 && flow_divert_send_read_notification(fd_cb
, read_count
) == 0) {
2536 fd_cb
->bytes_read_by_app
+= read_count
;
2537 fd_cb
->sb_size
= latest_sb_size
;
2544 flow_divert_append_target_endpoint_tlv(mbuf_t connect_packet
, struct sockaddr
*toaddr
)
2549 error
= flow_divert_packet_append_tlv(connect_packet
, FLOW_DIVERT_TLV_TARGET_ADDRESS
, toaddr
->sa_len
, toaddr
);
2554 if (toaddr
->sa_family
== AF_INET
) {
2555 port
= ntohs((satosin(toaddr
))->sin_port
);
2559 port
= ntohs((satosin6(toaddr
))->sin6_port
);
2563 error
= flow_divert_packet_append_tlv(connect_packet
, FLOW_DIVERT_TLV_TARGET_PORT
, sizeof(port
), &port
);
2573 flow_divert_get_buffered_target_address(mbuf_t buffer
)
2575 if (buffer
!= NULL
&& buffer
->m_type
== MT_SONAME
) {
2576 struct sockaddr
*toaddr
= mtod(buffer
, struct sockaddr
*);
2577 if (toaddr
!= NULL
&& flow_divert_is_sockaddr_valid(toaddr
)) {
2585 flow_divert_is_sockaddr_valid(struct sockaddr
*addr
)
2587 switch(addr
->sa_family
)
2590 if (addr
->sa_len
!= sizeof(struct sockaddr_in
)) {
2596 if (addr
->sa_len
!= sizeof(struct sockaddr_in6
)) {
2608 flow_divert_inp_to_sockaddr(const struct inpcb
*inp
, struct sockaddr
**local_socket
)
2611 union sockaddr_in_4_6 sin46
;
2613 bzero(&sin46
, sizeof(sin46
));
2614 if (inp
->inp_vflag
& INP_IPV4
) {
2615 struct sockaddr_in
*sin
= &sin46
.sin
;
2617 sin
->sin_family
= AF_INET
;
2618 sin
->sin_len
= sizeof(*sin
);
2619 sin
->sin_port
= inp
->inp_lport
;
2620 sin
->sin_addr
= inp
->inp_laddr
;
2621 } else if (inp
->inp_vflag
& INP_IPV6
) {
2622 struct sockaddr_in6
*sin6
= &sin46
.sin6
;
2624 sin6
->sin6_len
= sizeof(*sin6
);
2625 sin6
->sin6_family
= AF_INET6
;
2626 sin6
->sin6_port
= inp
->inp_lport
;
2627 sin6
->sin6_addr
= inp
->in6p_laddr
;
2629 *local_socket
= dup_sockaddr((struct sockaddr
*)&sin46
, 1);
2630 if (*local_socket
== NULL
) {
2637 flow_divert_has_pcb_local_address(const struct inpcb
*inp
)
2639 return (inp
->inp_lport
!= 0
2640 && (inp
->inp_laddr
.s_addr
!= INADDR_ANY
|| !IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)));
2644 flow_divert_dup_addr(sa_family_t family
, struct sockaddr
*addr
,
2645 struct sockaddr
**dup
)
2648 struct sockaddr
*result
;
2649 struct sockaddr_storage ss
;
2654 memset(&ss
, 0, sizeof(ss
));
2655 ss
.ss_family
= family
;
2656 if (ss
.ss_family
== AF_INET
) {
2657 ss
.ss_len
= sizeof(struct sockaddr_in
);
2660 else if (ss
.ss_family
== AF_INET6
) {
2661 ss
.ss_len
= sizeof(struct sockaddr_in6
);
2667 result
= (struct sockaddr
*)&ss
;
2671 *dup
= dup_sockaddr(result
, 1);
2681 flow_divert_disconnect_socket(struct socket
*so
)
2683 soisdisconnected(so
);
2684 if (SOCK_TYPE(so
) == SOCK_DGRAM
) {
2685 struct inpcb
*inp
= NULL
;
2687 inp
= sotoinpcb(so
);
2690 if (SOCK_CHECK_DOM(so
, PF_INET6
))
2700 flow_divert_getpeername(struct socket
*so
, struct sockaddr
**sa
)
2702 struct flow_divert_pcb
*fd_cb
= so
->so_fd_pcb
;
2704 VERIFY((so
->so_flags
& SOF_FLOW_DIVERT
) && so
->so_fd_pcb
!= NULL
);
2706 return flow_divert_dup_addr(so
->so_proto
->pr_domain
->dom_family
,
2707 fd_cb
->remote_address
,
2712 flow_divert_getsockaddr(struct socket
*so
, struct sockaddr
**sa
)
2714 struct flow_divert_pcb
*fd_cb
= so
->so_fd_pcb
;
2716 VERIFY((so
->so_flags
& SOF_FLOW_DIVERT
) && so
->so_fd_pcb
!= NULL
);
2718 return flow_divert_dup_addr(so
->so_proto
->pr_domain
->dom_family
,
2719 fd_cb
->local_address
,
2724 flow_divert_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
2726 struct flow_divert_pcb
*fd_cb
= so
->so_fd_pcb
;
2728 VERIFY((so
->so_flags
& SOF_FLOW_DIVERT
) && so
->so_fd_pcb
!= NULL
);
2730 if (sopt
->sopt_name
== SO_TRAFFIC_CLASS
) {
2731 if (sopt
->sopt_dir
== SOPT_SET
&& fd_cb
->flags
& FLOW_DIVERT_CONNECT_STARTED
) {
2732 flow_divert_send_traffic_class_update(fd_cb
, so
->so_traffic_class
);
2736 if (SOCK_DOM(so
) == PF_INET
) {
2737 return g_tcp_protosw
->pr_ctloutput(so
, sopt
);
2740 else if (SOCK_DOM(so
) == PF_INET6
) {
2741 return g_tcp6_protosw
->pr_ctloutput(so
, sopt
);
2748 flow_divert_connect_out(struct socket
*so
, struct sockaddr
*to
, proc_t p
)
2750 struct flow_divert_pcb
*fd_cb
= so
->so_fd_pcb
;
2752 struct inpcb
*inp
= sotoinpcb(so
);
2753 struct sockaddr_in
*sinp
;
2754 mbuf_t connect_packet
= NULL
;
2757 VERIFY((so
->so_flags
& SOF_FLOW_DIVERT
) && so
->so_fd_pcb
!= NULL
);
2759 if (fd_cb
->group
== NULL
) {
2760 error
= ENETUNREACH
;
2767 } else if (inp
->inp_state
== INPCB_STATE_DEAD
) {
2769 error
= so
->so_error
;
2777 if ((fd_cb
->flags
& FLOW_DIVERT_CONNECT_STARTED
) && !(fd_cb
->flags
& FLOW_DIVERT_TRANSFERRED
)) {
2782 if (fd_cb
->flags
& FLOW_DIVERT_TRANSFERRED
) {
2783 FDLOG0(LOG_INFO
, fd_cb
, "fully transferred");
2784 fd_cb
->flags
&= ~FLOW_DIVERT_TRANSFERRED
;
2785 if (fd_cb
->remote_address
!= NULL
) {
2786 soisconnected(fd_cb
->so
);
2791 FDLOG0(LOG_INFO
, fd_cb
, "Connecting");
2793 if (fd_cb
->connect_packet
== NULL
) {
2795 FDLOG0(LOG_ERR
, fd_cb
, "No destination address available when creating connect packet");
2800 sinp
= (struct sockaddr_in
*)(void *)to
;
2801 if (sinp
->sin_family
== AF_INET
&& IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
2802 error
= EAFNOSUPPORT
;
2806 error
= flow_divert_create_connect_packet(fd_cb
, to
, so
, p
, &connect_packet
);
2811 if (so
->so_flags1
& SOF1_PRECONNECT_DATA
) {
2812 FDLOG0(LOG_INFO
, fd_cb
, "Delaying sending the connect packet until send or receive");
2816 FDLOG0(LOG_INFO
, fd_cb
, "Sending saved connect packet");
2817 connect_packet
= fd_cb
->connect_packet
;
2818 fd_cb
->connect_packet
= NULL
;
2822 error
= flow_divert_send_packet(fd_cb
, connect_packet
, TRUE
);
2827 fd_cb
->flags
|= FLOW_DIVERT_CONNECT_STARTED
;
2829 fd_cb
->connect_packet
= connect_packet
;
2830 connect_packet
= NULL
;
2836 if (error
&& connect_packet
!= NULL
) {
2837 mbuf_freem(connect_packet
);
2843 flow_divert_connectx_out_common(struct socket
*so
, struct sockaddr
*dst
,
2844 struct proc
*p
, sae_connid_t
*pcid
, struct uio
*auio
, user_ssize_t
*bytes_written
)
2846 struct inpcb
*inp
= sotoinpcb(so
);
2853 VERIFY(dst
!= NULL
);
2855 error
= flow_divert_connect_out(so
, dst
, p
);
2861 /* if there is data, send it */
2863 user_ssize_t datalen
= 0;
2865 socket_unlock(so
, 0);
2867 VERIFY(bytes_written
!= NULL
);
2869 datalen
= uio_resid(auio
);
2870 error
= so
->so_proto
->pr_usrreqs
->pru_sosend(so
, NULL
, (uio_t
)auio
, NULL
, NULL
, 0);
2873 if (error
== 0 || error
== EWOULDBLOCK
) {
2874 *bytes_written
= datalen
- uio_resid(auio
);
2878 * sosend returns EWOULDBLOCK if it's a non-blocking
2879 * socket or a timeout occured (this allows to return
2880 * the amount of queued data through sendit()).
2882 * However, connectx() returns EINPROGRESS in case of a
2883 * blocking socket. So we change the return value here.
2885 if (error
== EWOULDBLOCK
) {
2886 error
= EINPROGRESS
;
2890 if (error
== 0 && pcid
!= NULL
) {
2891 *pcid
= 1; /* there is only 1 connection for a TCP */
2898 flow_divert_connectx_out(struct socket
*so
, struct sockaddr
*src __unused
,
2899 struct sockaddr
*dst
, struct proc
*p
, uint32_t ifscope __unused
,
2900 sae_associd_t aid __unused
, sae_connid_t
*pcid
, uint32_t flags __unused
, void *arg __unused
,
2901 uint32_t arglen __unused
, struct uio
*uio
, user_ssize_t
*bytes_written
)
2903 return (flow_divert_connectx_out_common(so
, dst
, p
, pcid
, uio
, bytes_written
));
2908 flow_divert_connectx6_out(struct socket
*so
, struct sockaddr
*src __unused
,
2909 struct sockaddr
*dst
, struct proc
*p
, uint32_t ifscope __unused
,
2910 sae_associd_t aid __unused
, sae_connid_t
*pcid
, uint32_t flags __unused
, void *arg __unused
,
2911 uint32_t arglen __unused
, struct uio
*uio
, user_ssize_t
*bytes_written
)
2913 return (flow_divert_connectx_out_common(so
, dst
, p
, pcid
, uio
, bytes_written
));
2918 flow_divert_getconninfo(struct socket
*so
, sae_connid_t cid
, uint32_t *flags
,
2919 uint32_t *ifindex
, int32_t *soerror
, user_addr_t src
, socklen_t
*src_len
,
2920 user_addr_t dst
, socklen_t
*dst_len
, uint32_t *aux_type
,
2921 user_addr_t aux_data __unused
, uint32_t *aux_len
)
2924 struct flow_divert_pcb
*fd_cb
= so
->so_fd_pcb
;
2925 struct ifnet
*ifp
= NULL
;
2926 struct inpcb
*inp
= sotoinpcb(so
);
2928 VERIFY((so
->so_flags
& SOF_FLOW_DIVERT
));
2930 if (so
->so_fd_pcb
== NULL
|| inp
== NULL
) {
2935 if (cid
!= SAE_CONNID_ANY
&& cid
!= SAE_CONNID_ALL
&& cid
!= 1) {
2940 ifp
= inp
->inp_last_outifp
;
2941 *ifindex
= ((ifp
!= NULL
) ? ifp
->if_index
: 0);
2942 *soerror
= so
->so_error
;
2945 if (so
->so_state
& SS_ISCONNECTED
) {
2946 *flags
|= (CIF_CONNECTED
| CIF_PREFERRED
);
2949 if (fd_cb
->local_address
== NULL
) {
2950 struct sockaddr_in sin
;
2951 bzero(&sin
, sizeof(sin
));
2952 sin
.sin_len
= sizeof(sin
);
2953 sin
.sin_family
= AF_INET
;
2954 *src_len
= sin
.sin_len
;
2955 if (src
!= USER_ADDR_NULL
) {
2956 error
= copyout(&sin
, src
, sin
.sin_len
);
2962 *src_len
= fd_cb
->local_address
->sa_len
;
2963 if (src
!= USER_ADDR_NULL
) {
2964 error
= copyout(fd_cb
->local_address
, src
, fd_cb
->local_address
->sa_len
);
2971 if (fd_cb
->remote_address
== NULL
) {
2972 struct sockaddr_in sin
;
2973 bzero(&sin
, sizeof(sin
));
2974 sin
.sin_len
= sizeof(sin
);
2975 sin
.sin_family
= AF_INET
;
2976 *dst_len
= sin
.sin_len
;
2977 if (dst
!= USER_ADDR_NULL
) {
2978 error
= copyout(&sin
, dst
, sin
.sin_len
);
2984 *dst_len
= fd_cb
->remote_address
->sa_len
;
2985 if (dst
!= USER_ADDR_NULL
) {
2986 error
= copyout(fd_cb
->remote_address
, dst
, fd_cb
->remote_address
->sa_len
);
3001 flow_divert_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp __unused
, struct proc
*p __unused
)
3006 case SIOCGCONNINFO32
: {
3007 struct so_cinforeq32 cifr
;
3008 bcopy(data
, &cifr
, sizeof (cifr
));
3009 error
= flow_divert_getconninfo(so
, cifr
.scir_cid
, &cifr
.scir_flags
,
3010 &cifr
.scir_ifindex
, &cifr
.scir_error
, cifr
.scir_src
,
3011 &cifr
.scir_src_len
, cifr
.scir_dst
, &cifr
.scir_dst_len
,
3012 &cifr
.scir_aux_type
, cifr
.scir_aux_data
,
3013 &cifr
.scir_aux_len
);
3015 bcopy(&cifr
, data
, sizeof (cifr
));
3020 case SIOCGCONNINFO64
: {
3021 struct so_cinforeq64 cifr
;
3022 bcopy(data
, &cifr
, sizeof (cifr
));
3023 error
= flow_divert_getconninfo(so
, cifr
.scir_cid
, &cifr
.scir_flags
,
3024 &cifr
.scir_ifindex
, &cifr
.scir_error
, cifr
.scir_src
,
3025 &cifr
.scir_src_len
, cifr
.scir_dst
, &cifr
.scir_dst_len
,
3026 &cifr
.scir_aux_type
, cifr
.scir_aux_data
,
3027 &cifr
.scir_aux_len
);
3029 bcopy(&cifr
, data
, sizeof (cifr
));
3042 flow_divert_in_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
, struct proc
*p
)
3044 int error
= flow_divert_control(so
, cmd
, data
, ifp
, p
);
3046 if (error
== EOPNOTSUPP
) {
3047 error
= in_control(so
, cmd
, data
, ifp
, p
);
3054 flow_divert_in6_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
, struct proc
*p
)
3056 int error
= flow_divert_control(so
, cmd
, data
, ifp
, p
);
3058 if (error
== EOPNOTSUPP
) {
3059 error
= in6_control(so
, cmd
, data
, ifp
, p
);
3066 flow_divert_data_out(struct socket
*so
, int flags
, mbuf_t data
, struct sockaddr
*to
, mbuf_t control
, struct proc
*p
)
3068 struct flow_divert_pcb
*fd_cb
= so
->so_fd_pcb
;
3072 VERIFY((so
->so_flags
& SOF_FLOW_DIVERT
) && so
->so_fd_pcb
!= NULL
);
3074 inp
= sotoinpcb(so
);
3075 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
) {
3080 if (control
&& mbuf_len(control
) > 0) {
3085 if (flags
& MSG_OOB
) {
3087 goto done
; /* We don't support OOB data */
3090 error
= flow_divert_check_no_cellular(fd_cb
) ||
3091 flow_divert_check_no_expensive(fd_cb
);
3096 /* Implicit connect */
3097 if (!(fd_cb
->flags
& FLOW_DIVERT_CONNECT_STARTED
)) {
3098 FDLOG0(LOG_INFO
, fd_cb
, "implicit connect");
3099 error
= flow_divert_connect_out(so
, to
, p
);
3104 if (so
->so_flags1
& SOF1_DATA_IDEMPOTENT
) {
3105 /* Open up the send window so that the data will get sent right away */
3106 fd_cb
->send_window
= mbuf_pkthdr_len(data
);
3110 FDLOG(LOG_DEBUG
, fd_cb
, "app wrote %lu bytes", mbuf_pkthdr_len(data
));
3112 fd_cb
->bytes_written_by_app
+= mbuf_pkthdr_len(data
);
3113 error
= flow_divert_send_app_data(fd_cb
, data
, to
);
3120 if (flags
& PRUS_EOF
) {
3121 flow_divert_shutdown(so
);
3135 flow_divert_preconnect(struct socket
*so
)
3137 struct flow_divert_pcb
*fd_cb
= so
->so_fd_pcb
;
3140 if (!(fd_cb
->flags
& FLOW_DIVERT_CONNECT_STARTED
) && fd_cb
->connect_packet
!= NULL
) {
3141 FDLOG0(LOG_INFO
, fd_cb
, "Pre-connect read: sending saved connect packet");
3142 mbuf_t connect_packet
= fd_cb
->connect_packet
;
3143 fd_cb
->connect_packet
= NULL
;
3145 error
= flow_divert_send_packet(fd_cb
, connect_packet
, TRUE
);
3147 mbuf_freem(connect_packet
);
3150 fd_cb
->flags
|= FLOW_DIVERT_CONNECT_STARTED
;
3153 soclearfastopen(so
);
3159 flow_divert_set_protosw(struct socket
*so
)
3161 so
->so_flags
|= SOF_FLOW_DIVERT
;
3162 if (SOCK_DOM(so
) == PF_INET
) {
3163 so
->so_proto
= &g_flow_divert_in_protosw
;
3167 so
->so_proto
= (struct protosw
*)&g_flow_divert_in6_protosw
;
3173 flow_divert_set_udp_protosw(struct socket
*so
)
3175 so
->so_flags
|= SOF_FLOW_DIVERT
;
3176 if (SOCK_DOM(so
) == PF_INET
) {
3177 so
->so_proto
= &g_flow_divert_in_udp_protosw
;
3181 so
->so_proto
= (struct protosw
*)&g_flow_divert_in6_udp_protosw
;
3187 flow_divert_attach(struct socket
*so
, uint32_t flow_id
, uint32_t ctl_unit
)
3190 struct flow_divert_pcb
*fd_cb
= NULL
;
3191 struct ifnet
*ifp
= NULL
;
3192 struct inpcb
*inp
= NULL
;
3193 struct socket
*old_so
;
3194 mbuf_t recv_data
= NULL
;
3196 socket_unlock(so
, 0);
3198 FDLOG(LOG_INFO
, &nil_pcb
, "Attaching socket to flow %u", flow_id
);
3200 /* Find the flow divert control block */
3201 lck_rw_lock_shared(&g_flow_divert_group_lck
);
3202 if (g_flow_divert_groups
!= NULL
&& g_active_group_count
> 0) {
3203 struct flow_divert_group
*group
= g_flow_divert_groups
[ctl_unit
];
3204 if (group
!= NULL
) {
3205 fd_cb
= flow_divert_pcb_lookup(flow_id
, group
);
3208 lck_rw_done(&g_flow_divert_group_lck
);
3210 if (fd_cb
== NULL
) {
3217 /* Dis-associate the flow divert control block from its current socket */
3220 inp
= sotoinpcb(old_so
);
3222 VERIFY(inp
!= NULL
);
3224 socket_lock(old_so
, 0);
3225 flow_divert_disconnect_socket(old_so
);
3226 old_so
->so_flags
&= ~SOF_FLOW_DIVERT
;
3227 old_so
->so_fd_pcb
= NULL
;
3228 if (SOCK_TYPE(old_so
) == SOCK_STREAM
) {
3229 old_so
->so_proto
= pffindproto(SOCK_DOM(old_so
), IPPROTO_TCP
, SOCK_STREAM
);
3230 } else if (SOCK_TYPE(old_so
) == SOCK_DGRAM
) {
3231 old_so
->so_proto
= pffindproto(SOCK_DOM(old_so
), IPPROTO_UDP
, SOCK_DGRAM
);
3234 /* Save the output interface */
3235 ifp
= inp
->inp_last_outifp
;
3236 if (old_so
->so_rcv
.sb_cc
> 0) {
3237 error
= mbuf_dup(old_so
->so_rcv
.sb_mb
, MBUF_DONTWAIT
, &recv_data
);
3238 sbflush(&old_so
->so_rcv
);
3240 socket_unlock(old_so
, 0);
3242 /* Associate the new socket with the flow divert control block */
3244 so
->so_fd_pcb
= fd_cb
;
3245 inp
= sotoinpcb(so
);
3246 inp
->inp_last_outifp
= ifp
;
3247 if (recv_data
!= NULL
) {
3248 if (sbappendstream(&so
->so_rcv
, recv_data
)) {
3252 flow_divert_set_protosw(so
);
3253 socket_unlock(so
, 0);
3256 fd_cb
->flags
|= FLOW_DIVERT_TRANSFERRED
;
3263 if (fd_cb
!= NULL
) {
3264 FDRELEASE(fd_cb
); /* Release the reference obtained via flow_divert_pcb_lookup */
3271 flow_divert_implicit_data_out(struct socket
*so
, int flags
, mbuf_t data
, struct sockaddr
*to
, mbuf_t control
, struct proc
*p
)
3273 struct flow_divert_pcb
*fd_cb
= so
->so_fd_pcb
;
3277 inp
= sotoinpcb(so
);
3282 if (fd_cb
== NULL
) {
3283 uint32_t fd_ctl_unit
= necp_socket_get_flow_divert_control_unit(inp
);
3284 if (fd_ctl_unit
> 0) {
3285 error
= flow_divert_pcb_init(so
, fd_ctl_unit
);
3286 fd_cb
= so
->so_fd_pcb
;
3287 if (error
!= 0 || fd_cb
== NULL
) {
3295 return flow_divert_data_out(so
, flags
, data
, to
, control
, p
);
3309 flow_divert_pcb_init(struct socket
*so
, uint32_t ctl_unit
)
3312 struct flow_divert_pcb
*fd_cb
;
3314 if (so
->so_flags
& SOF_FLOW_DIVERT
) {
3318 fd_cb
= flow_divert_pcb_create(so
);
3319 if (fd_cb
!= NULL
) {
3320 error
= flow_divert_pcb_insert(fd_cb
, ctl_unit
);
3322 FDLOG(LOG_ERR
, fd_cb
, "pcb insert failed: %d", error
);
3325 fd_cb
->control_group_unit
= ctl_unit
;
3326 so
->so_fd_pcb
= fd_cb
;
3328 if (SOCK_TYPE(so
) == SOCK_STREAM
) {
3329 flow_divert_set_protosw(so
);
3330 } else if (SOCK_TYPE(so
) == SOCK_DGRAM
) {
3331 flow_divert_set_udp_protosw(so
);
3334 FDLOG0(LOG_INFO
, fd_cb
, "Created");
3344 flow_divert_token_set(struct socket
*so
, struct sockopt
*sopt
)
3346 uint32_t ctl_unit
= 0;
3347 uint32_t key_unit
= 0;
3348 uint32_t flow_id
= 0;
3351 mbuf_t token
= NULL
;
3353 if (so
->so_flags
& SOF_FLOW_DIVERT
) {
3358 if (g_init_result
) {
3359 FDLOG(LOG_ERR
, &nil_pcb
, "flow_divert_init failed (%d), cannot use flow divert", g_init_result
);
3360 error
= ENOPROTOOPT
;
3364 if ((SOCK_TYPE(so
) != SOCK_STREAM
&& SOCK_TYPE(so
) != SOCK_DGRAM
) ||
3365 (SOCK_PROTO(so
) != IPPROTO_TCP
&& SOCK_PROTO(so
) != IPPROTO_UDP
) ||
3366 (SOCK_DOM(so
) != PF_INET
3368 && SOCK_DOM(so
) != PF_INET6
3375 if (SOCK_TYPE(so
) == SOCK_STREAM
&& SOCK_PROTO(so
) == IPPROTO_TCP
) {
3376 struct tcpcb
*tp
= sototcpcb(so
);
3377 if (tp
== NULL
|| tp
->t_state
!= TCPS_CLOSED
) {
3384 error
= soopt_getm(sopt
, &token
);
3389 error
= soopt_mcopyin(sopt
, token
);
3394 error
= flow_divert_packet_get_tlv(token
, 0, FLOW_DIVERT_TLV_KEY_UNIT
, sizeof(key_unit
), (void *)&key_unit
, NULL
);
3396 key_unit
= ntohl(key_unit
);
3397 if (key_unit
>= GROUP_COUNT_MAX
) {
3400 } else if (error
!= ENOENT
) {
3401 FDLOG(LOG_ERR
, &nil_pcb
, "Failed to get the key unit from the token: %d", error
);
3407 error
= flow_divert_packet_get_tlv(token
, 0, FLOW_DIVERT_TLV_CTL_UNIT
, sizeof(ctl_unit
), (void *)&ctl_unit
, NULL
);
3409 FDLOG(LOG_ERR
, &nil_pcb
, "Failed to get the control socket unit from the token: %d", error
);
3413 /* A valid kernel control unit is required */
3414 ctl_unit
= ntohl(ctl_unit
);
3415 if (ctl_unit
== 0 || ctl_unit
>= GROUP_COUNT_MAX
) {
3416 FDLOG(LOG_ERR
, &nil_pcb
, "Got an invalid control socket unit: %u", ctl_unit
);
3421 socket_unlock(so
, 0);
3422 hmac_error
= flow_divert_packet_verify_hmac(token
, (key_unit
!= 0 ? key_unit
: ctl_unit
));
3425 if (hmac_error
&& hmac_error
!= ENOENT
) {
3426 FDLOG(LOG_ERR
, &nil_pcb
, "HMAC verfication failed: %d", hmac_error
);
3431 error
= flow_divert_packet_get_tlv(token
, 0, FLOW_DIVERT_TLV_FLOW_ID
, sizeof(flow_id
), (void *)&flow_id
, NULL
);
3432 if (error
&& error
!= ENOENT
) {
3433 FDLOG(LOG_ERR
, &nil_pcb
, "Failed to get the flow ID from the token: %d", error
);
3438 error
= flow_divert_pcb_init(so
, ctl_unit
);
3440 struct flow_divert_pcb
*fd_cb
= so
->so_fd_pcb
;
3441 int log_level
= LOG_NOTICE
;
3443 error
= flow_divert_packet_get_tlv(token
, 0, FLOW_DIVERT_TLV_LOG_LEVEL
,
3444 sizeof(log_level
), &log_level
, NULL
);
3446 fd_cb
->log_level
= log_level
;
3450 fd_cb
->connect_token
= token
;
3454 error
= flow_divert_attach(so
, flow_id
, ctl_unit
);
3457 if (hmac_error
== 0) {
3458 struct flow_divert_pcb
*fd_cb
= so
->so_fd_pcb
;
3459 if (fd_cb
!= NULL
) {
3460 fd_cb
->flags
|= FLOW_DIVERT_HAS_HMAC
;
3465 if (token
!= NULL
) {
3473 flow_divert_token_get(struct socket
*so
, struct sockopt
*sopt
)
3477 uint8_t hmac
[SHA_DIGEST_LENGTH
];
3478 struct flow_divert_pcb
*fd_cb
= so
->so_fd_pcb
;
3479 mbuf_t token
= NULL
;
3480 struct flow_divert_group
*control_group
= NULL
;
3482 if (!(so
->so_flags
& SOF_FLOW_DIVERT
)) {
3487 VERIFY((so
->so_flags
& SOF_FLOW_DIVERT
) && so
->so_fd_pcb
!= NULL
);
3489 if (fd_cb
->group
== NULL
) {
3494 error
= mbuf_gethdr(MBUF_DONTWAIT
, MBUF_TYPE_HEADER
, &token
);
3496 FDLOG(LOG_ERR
, fd_cb
, "failed to allocate the header mbuf: %d", error
);
3500 ctl_unit
= htonl(fd_cb
->group
->ctl_unit
);
3502 error
= flow_divert_packet_append_tlv(token
, FLOW_DIVERT_TLV_CTL_UNIT
, sizeof(ctl_unit
), &ctl_unit
);
3507 error
= flow_divert_packet_append_tlv(token
, FLOW_DIVERT_TLV_FLOW_ID
, sizeof(fd_cb
->hash
), &fd_cb
->hash
);
3512 if (fd_cb
->app_data
!= NULL
) {
3513 error
= flow_divert_packet_append_tlv(token
, FLOW_DIVERT_TLV_APP_DATA
, fd_cb
->app_data_length
, fd_cb
->app_data
);
3519 socket_unlock(so
, 0);
3520 lck_rw_lock_shared(&g_flow_divert_group_lck
);
3522 if (g_flow_divert_groups
!= NULL
&& g_active_group_count
> 0 &&
3523 fd_cb
->control_group_unit
> 0 && fd_cb
->control_group_unit
< GROUP_COUNT_MAX
)
3525 control_group
= g_flow_divert_groups
[fd_cb
->control_group_unit
];
3528 if (control_group
!= NULL
) {
3529 lck_rw_lock_shared(&control_group
->lck
);
3530 ctl_unit
= htonl(control_group
->ctl_unit
);
3531 error
= flow_divert_packet_append_tlv(token
, FLOW_DIVERT_TLV_KEY_UNIT
, sizeof(ctl_unit
), &ctl_unit
);
3533 error
= flow_divert_packet_compute_hmac(token
, control_group
, hmac
);
3535 lck_rw_done(&control_group
->lck
);
3537 error
= ENOPROTOOPT
;
3540 lck_rw_done(&g_flow_divert_group_lck
);
3547 error
= flow_divert_packet_append_tlv(token
, FLOW_DIVERT_TLV_HMAC
, sizeof(hmac
), hmac
);
3552 if (sopt
->sopt_val
== USER_ADDR_NULL
) {
3553 /* If the caller passed NULL to getsockopt, just set the size of the token and return */
3554 sopt
->sopt_valsize
= mbuf_pkthdr_len(token
);
3558 error
= soopt_mcopyout(sopt
, token
);
3560 token
= NULL
; /* For some reason, soopt_mcopyout() frees the mbuf if it fails */
3565 if (token
!= NULL
) {
3573 flow_divert_kctl_connect(kern_ctl_ref kctlref __unused
, struct sockaddr_ctl
*sac
, void **unitinfo
)
3575 struct flow_divert_group
*new_group
= NULL
;
3578 if (sac
->sc_unit
>= GROUP_COUNT_MAX
) {
3585 MALLOC_ZONE(new_group
, struct flow_divert_group
*, sizeof(*new_group
), M_FLOW_DIVERT_GROUP
, M_WAITOK
);
3586 if (new_group
== NULL
) {
3591 memset(new_group
, 0, sizeof(*new_group
));
3593 lck_rw_init(&new_group
->lck
, flow_divert_mtx_grp
, flow_divert_mtx_attr
);
3594 RB_INIT(&new_group
->pcb_tree
);
3595 new_group
->ctl_unit
= sac
->sc_unit
;
3596 MBUFQ_INIT(&new_group
->send_queue
);
3597 new_group
->signing_id_trie
.root
= NULL_TRIE_IDX
;
3599 lck_rw_lock_exclusive(&g_flow_divert_group_lck
);
3601 if (g_flow_divert_groups
== NULL
) {
3602 MALLOC(g_flow_divert_groups
,
3603 struct flow_divert_group
**,
3604 GROUP_COUNT_MAX
* sizeof(struct flow_divert_group
*),
3609 if (g_flow_divert_groups
== NULL
) {
3611 } else if (g_flow_divert_groups
[sac
->sc_unit
] != NULL
) {
3614 g_flow_divert_groups
[sac
->sc_unit
] = new_group
;
3615 g_active_group_count
++;
3618 lck_rw_done(&g_flow_divert_group_lck
);
3620 *unitinfo
= new_group
;
3623 if (error
!= 0 && new_group
!= NULL
) {
3624 FREE_ZONE(new_group
, sizeof(*new_group
), M_FLOW_DIVERT_GROUP
);
3630 flow_divert_kctl_disconnect(kern_ctl_ref kctlref __unused
, uint32_t unit
, void *unitinfo
)
3632 struct flow_divert_group
*group
= NULL
;
3635 if (unit
>= GROUP_COUNT_MAX
) {
3639 FDLOG(LOG_INFO
, &nil_pcb
, "disconnecting group %d", unit
);
3641 lck_rw_lock_exclusive(&g_flow_divert_group_lck
);
3643 if (g_flow_divert_groups
== NULL
|| g_active_group_count
== 0) {
3644 panic("flow divert group %u is disconnecting, but no groups are active (groups = %p, active count = %u", unit
,
3645 g_flow_divert_groups
, g_active_group_count
);
3648 group
= g_flow_divert_groups
[unit
];
3650 if (group
!= (struct flow_divert_group
*)unitinfo
) {
3651 panic("group with unit %d (%p) != unit info (%p)", unit
, group
, unitinfo
);
3654 if (group
!= NULL
) {
3655 flow_divert_close_all(group
);
3656 if (group
->token_key
!= NULL
) {
3657 memset(group
->token_key
, 0, group
->token_key_size
);
3658 FREE(group
->token_key
, M_TEMP
);
3659 group
->token_key
= NULL
;
3660 group
->token_key_size
= 0;
3663 /* Re-set the current trie */
3664 if (group
->signing_id_trie
.memory
!= NULL
) {
3665 FREE(group
->signing_id_trie
.memory
, M_TEMP
);
3667 memset(&group
->signing_id_trie
, 0, sizeof(group
->signing_id_trie
));
3668 group
->signing_id_trie
.root
= NULL_TRIE_IDX
;
3670 FREE_ZONE(group
, sizeof(*group
), M_FLOW_DIVERT_GROUP
);
3671 g_flow_divert_groups
[unit
] = NULL
;
3672 g_active_group_count
--;
3677 if (g_active_group_count
== 0) {
3678 FREE(g_flow_divert_groups
, M_TEMP
);
3679 g_flow_divert_groups
= NULL
;
3682 lck_rw_done(&g_flow_divert_group_lck
);
3688 flow_divert_kctl_send(kern_ctl_ref kctlref __unused
, uint32_t unit __unused
, void *unitinfo
, mbuf_t m
, int flags __unused
)
3690 return flow_divert_input(m
, (struct flow_divert_group
*)unitinfo
);
3694 flow_divert_kctl_rcvd(kern_ctl_ref kctlref __unused
, uint32_t unit __unused
, void *unitinfo
, int flags __unused
)
3696 struct flow_divert_group
*group
= (struct flow_divert_group
*)unitinfo
;
3698 if (!OSTestAndClear(GROUP_BIT_CTL_ENQUEUE_BLOCKED
, &group
->atomic_bits
)) {
3699 struct flow_divert_pcb
*fd_cb
;
3700 SLIST_HEAD(, flow_divert_pcb
) tmp_list
;
3702 lck_rw_lock_shared(&g_flow_divert_group_lck
);
3703 lck_rw_lock_exclusive(&group
->lck
);
3705 while (!MBUFQ_EMPTY(&group
->send_queue
)) {
3707 FDLOG0(LOG_DEBUG
, &nil_pcb
, "trying ctl_enqueuembuf again");
3708 next_packet
= MBUFQ_FIRST(&group
->send_queue
);
3709 int error
= ctl_enqueuembuf(g_flow_divert_kctl_ref
, group
->ctl_unit
, next_packet
, CTL_DATA_EOR
);
3711 FDLOG(LOG_DEBUG
, &nil_pcb
, "ctl_enqueuembuf returned an error: %d", error
);
3712 OSTestAndSet(GROUP_BIT_CTL_ENQUEUE_BLOCKED
, &group
->atomic_bits
);
3713 lck_rw_done(&group
->lck
);
3714 lck_rw_done(&g_flow_divert_group_lck
);
3717 MBUFQ_DEQUEUE(&group
->send_queue
, next_packet
);
3720 SLIST_INIT(&tmp_list
);
3722 RB_FOREACH(fd_cb
, fd_pcb_tree
, &group
->pcb_tree
) {
3724 SLIST_INSERT_HEAD(&tmp_list
, fd_cb
, tmp_list_entry
);
3727 lck_rw_done(&group
->lck
);
3729 SLIST_FOREACH(fd_cb
, &tmp_list
, tmp_list_entry
) {
3731 if (fd_cb
->so
!= NULL
) {
3732 socket_lock(fd_cb
->so
, 0);
3733 if (fd_cb
->group
!= NULL
) {
3734 flow_divert_send_buffered_data(fd_cb
, FALSE
);
3736 socket_unlock(fd_cb
->so
, 0);
3742 lck_rw_done(&g_flow_divert_group_lck
);
3747 flow_divert_kctl_init(void)
3749 struct kern_ctl_reg ctl_reg
;
3752 memset(&ctl_reg
, 0, sizeof(ctl_reg
));
3754 strlcpy(ctl_reg
.ctl_name
, FLOW_DIVERT_CONTROL_NAME
, sizeof(ctl_reg
.ctl_name
));
3755 ctl_reg
.ctl_name
[sizeof(ctl_reg
.ctl_name
)-1] = '\0';
3756 ctl_reg
.ctl_flags
= CTL_FLAG_PRIVILEGED
| CTL_FLAG_REG_EXTENDED
;
3757 ctl_reg
.ctl_sendsize
= FD_CTL_SENDBUFF_SIZE
;
3758 ctl_reg
.ctl_recvsize
= FD_CTL_RCVBUFF_SIZE
;
3760 ctl_reg
.ctl_connect
= flow_divert_kctl_connect
;
3761 ctl_reg
.ctl_disconnect
= flow_divert_kctl_disconnect
;
3762 ctl_reg
.ctl_send
= flow_divert_kctl_send
;
3763 ctl_reg
.ctl_rcvd
= flow_divert_kctl_rcvd
;
3765 result
= ctl_register(&ctl_reg
, &g_flow_divert_kctl_ref
);
3768 FDLOG(LOG_ERR
, &nil_pcb
, "flow_divert_kctl_init - ctl_register failed: %d\n", result
);
3776 flow_divert_init(void)
3778 memset(&nil_pcb
, 0, sizeof(nil_pcb
));
3779 nil_pcb
.log_level
= LOG_NOTICE
;
3781 g_tcp_protosw
= pffindproto(AF_INET
, IPPROTO_TCP
, SOCK_STREAM
);
3783 VERIFY(g_tcp_protosw
!= NULL
);
3785 memcpy(&g_flow_divert_in_protosw
, g_tcp_protosw
, sizeof(g_flow_divert_in_protosw
));
3786 memcpy(&g_flow_divert_in_usrreqs
, g_tcp_protosw
->pr_usrreqs
, sizeof(g_flow_divert_in_usrreqs
));
3788 g_flow_divert_in_usrreqs
.pru_connect
= flow_divert_connect_out
;
3789 g_flow_divert_in_usrreqs
.pru_connectx
= flow_divert_connectx_out
;
3790 g_flow_divert_in_usrreqs
.pru_control
= flow_divert_in_control
;
3791 g_flow_divert_in_usrreqs
.pru_disconnect
= flow_divert_close
;
3792 g_flow_divert_in_usrreqs
.pru_disconnectx
= flow_divert_disconnectx
;
3793 g_flow_divert_in_usrreqs
.pru_peeraddr
= flow_divert_getpeername
;
3794 g_flow_divert_in_usrreqs
.pru_rcvd
= flow_divert_rcvd
;
3795 g_flow_divert_in_usrreqs
.pru_send
= flow_divert_data_out
;
3796 g_flow_divert_in_usrreqs
.pru_shutdown
= flow_divert_shutdown
;
3797 g_flow_divert_in_usrreqs
.pru_sockaddr
= flow_divert_getsockaddr
;
3798 g_flow_divert_in_usrreqs
.pru_preconnect
= flow_divert_preconnect
;
3800 g_flow_divert_in_protosw
.pr_usrreqs
= &g_flow_divert_in_usrreqs
;
3801 g_flow_divert_in_protosw
.pr_ctloutput
= flow_divert_ctloutput
;
3804 * Socket filters shouldn't attach/detach to/from this protosw
3805 * since pr_protosw is to be used instead, which points to the
3806 * real protocol; if they do, it is a bug and we should panic.
3808 g_flow_divert_in_protosw
.pr_filter_head
.tqh_first
=
3809 (struct socket_filter
*)(uintptr_t)0xdeadbeefdeadbeef;
3810 g_flow_divert_in_protosw
.pr_filter_head
.tqh_last
=
3811 (struct socket_filter
**)(uintptr_t)0xdeadbeefdeadbeef;
3814 g_udp_protosw
= pffindproto(AF_INET
, IPPROTO_UDP
, SOCK_DGRAM
);
3815 VERIFY(g_udp_protosw
!= NULL
);
3817 memcpy(&g_flow_divert_in_udp_protosw
, g_udp_protosw
, sizeof(g_flow_divert_in_udp_protosw
));
3818 memcpy(&g_flow_divert_in_udp_usrreqs
, g_udp_protosw
->pr_usrreqs
, sizeof(g_flow_divert_in_udp_usrreqs
));
3820 g_flow_divert_in_udp_usrreqs
.pru_connect
= flow_divert_connect_out
;
3821 g_flow_divert_in_udp_usrreqs
.pru_connectx
= flow_divert_connectx_out
;
3822 g_flow_divert_in_udp_usrreqs
.pru_control
= flow_divert_in_control
;
3823 g_flow_divert_in_udp_usrreqs
.pru_disconnect
= flow_divert_close
;
3824 g_flow_divert_in_udp_usrreqs
.pru_disconnectx
= flow_divert_disconnectx
;
3825 g_flow_divert_in_udp_usrreqs
.pru_peeraddr
= flow_divert_getpeername
;
3826 g_flow_divert_in_udp_usrreqs
.pru_rcvd
= flow_divert_rcvd
;
3827 g_flow_divert_in_udp_usrreqs
.pru_send
= flow_divert_data_out
;
3828 g_flow_divert_in_udp_usrreqs
.pru_shutdown
= flow_divert_shutdown
;
3829 g_flow_divert_in_udp_usrreqs
.pru_sockaddr
= flow_divert_getsockaddr
;
3830 g_flow_divert_in_udp_usrreqs
.pru_sosend_list
= pru_sosend_list_notsupp
;
3831 g_flow_divert_in_udp_usrreqs
.pru_soreceive_list
= pru_soreceive_list_notsupp
;
3832 g_flow_divert_in_udp_usrreqs
.pru_preconnect
= flow_divert_preconnect
;
3834 g_flow_divert_in_udp_protosw
.pr_usrreqs
= &g_flow_divert_in_usrreqs
;
3835 g_flow_divert_in_udp_protosw
.pr_ctloutput
= flow_divert_ctloutput
;
3838 * Socket filters shouldn't attach/detach to/from this protosw
3839 * since pr_protosw is to be used instead, which points to the
3840 * real protocol; if they do, it is a bug and we should panic.
3842 g_flow_divert_in_udp_protosw
.pr_filter_head
.tqh_first
=
3843 (struct socket_filter
*)(uintptr_t)0xdeadbeefdeadbeef;
3844 g_flow_divert_in_udp_protosw
.pr_filter_head
.tqh_last
=
3845 (struct socket_filter
**)(uintptr_t)0xdeadbeefdeadbeef;
3848 g_tcp6_protosw
= (struct ip6protosw
*)pffindproto(AF_INET6
, IPPROTO_TCP
, SOCK_STREAM
);
3850 VERIFY(g_tcp6_protosw
!= NULL
);
3852 memcpy(&g_flow_divert_in6_protosw
, g_tcp6_protosw
, sizeof(g_flow_divert_in6_protosw
));
3853 memcpy(&g_flow_divert_in6_usrreqs
, g_tcp6_protosw
->pr_usrreqs
, sizeof(g_flow_divert_in6_usrreqs
));
3855 g_flow_divert_in6_usrreqs
.pru_connect
= flow_divert_connect_out
;
3856 g_flow_divert_in6_usrreqs
.pru_connectx
= flow_divert_connectx6_out
;
3857 g_flow_divert_in6_usrreqs
.pru_control
= flow_divert_in6_control
;
3858 g_flow_divert_in6_usrreqs
.pru_disconnect
= flow_divert_close
;
3859 g_flow_divert_in6_usrreqs
.pru_disconnectx
= flow_divert_disconnectx
;
3860 g_flow_divert_in6_usrreqs
.pru_peeraddr
= flow_divert_getpeername
;
3861 g_flow_divert_in6_usrreqs
.pru_rcvd
= flow_divert_rcvd
;
3862 g_flow_divert_in6_usrreqs
.pru_send
= flow_divert_data_out
;
3863 g_flow_divert_in6_usrreqs
.pru_shutdown
= flow_divert_shutdown
;
3864 g_flow_divert_in6_usrreqs
.pru_sockaddr
= flow_divert_getsockaddr
;
3865 g_flow_divert_in6_usrreqs
.pru_preconnect
= flow_divert_preconnect
;
3867 g_flow_divert_in6_protosw
.pr_usrreqs
= &g_flow_divert_in6_usrreqs
;
3868 g_flow_divert_in6_protosw
.pr_ctloutput
= flow_divert_ctloutput
;
3870 * Socket filters shouldn't attach/detach to/from this protosw
3871 * since pr_protosw is to be used instead, which points to the
3872 * real protocol; if they do, it is a bug and we should panic.
3874 g_flow_divert_in6_protosw
.pr_filter_head
.tqh_first
=
3875 (struct socket_filter
*)(uintptr_t)0xdeadbeefdeadbeef;
3876 g_flow_divert_in6_protosw
.pr_filter_head
.tqh_last
=
3877 (struct socket_filter
**)(uintptr_t)0xdeadbeefdeadbeef;
3880 g_udp6_protosw
= (struct ip6protosw
*)pffindproto(AF_INET6
, IPPROTO_UDP
, SOCK_DGRAM
);
3882 VERIFY(g_udp6_protosw
!= NULL
);
3884 memcpy(&g_flow_divert_in6_udp_protosw
, g_udp6_protosw
, sizeof(g_flow_divert_in6_udp_protosw
));
3885 memcpy(&g_flow_divert_in6_udp_usrreqs
, g_udp6_protosw
->pr_usrreqs
, sizeof(g_flow_divert_in6_udp_usrreqs
));
3887 g_flow_divert_in6_udp_usrreqs
.pru_connect
= flow_divert_connect_out
;
3888 g_flow_divert_in6_udp_usrreqs
.pru_connectx
= flow_divert_connectx6_out
;
3889 g_flow_divert_in6_udp_usrreqs
.pru_control
= flow_divert_in6_control
;
3890 g_flow_divert_in6_udp_usrreqs
.pru_disconnect
= flow_divert_close
;
3891 g_flow_divert_in6_udp_usrreqs
.pru_disconnectx
= flow_divert_disconnectx
;
3892 g_flow_divert_in6_udp_usrreqs
.pru_peeraddr
= flow_divert_getpeername
;
3893 g_flow_divert_in6_udp_usrreqs
.pru_rcvd
= flow_divert_rcvd
;
3894 g_flow_divert_in6_udp_usrreqs
.pru_send
= flow_divert_data_out
;
3895 g_flow_divert_in6_udp_usrreqs
.pru_shutdown
= flow_divert_shutdown
;
3896 g_flow_divert_in6_udp_usrreqs
.pru_sockaddr
= flow_divert_getsockaddr
;
3897 g_flow_divert_in6_udp_usrreqs
.pru_sosend_list
= pru_sosend_list_notsupp
;
3898 g_flow_divert_in6_udp_usrreqs
.pru_soreceive_list
= pru_soreceive_list_notsupp
;
3899 g_flow_divert_in6_udp_usrreqs
.pru_preconnect
= flow_divert_preconnect
;
3901 g_flow_divert_in6_udp_protosw
.pr_usrreqs
= &g_flow_divert_in6_udp_usrreqs
;
3902 g_flow_divert_in6_udp_protosw
.pr_ctloutput
= flow_divert_ctloutput
;
3904 * Socket filters shouldn't attach/detach to/from this protosw
3905 * since pr_protosw is to be used instead, which points to the
3906 * real protocol; if they do, it is a bug and we should panic.
3908 g_flow_divert_in6_udp_protosw
.pr_filter_head
.tqh_first
=
3909 (struct socket_filter
*)(uintptr_t)0xdeadbeefdeadbeef;
3910 g_flow_divert_in6_udp_protosw
.pr_filter_head
.tqh_last
=
3911 (struct socket_filter
**)(uintptr_t)0xdeadbeefdeadbeef;
3914 flow_divert_grp_attr
= lck_grp_attr_alloc_init();
3915 if (flow_divert_grp_attr
== NULL
) {
3916 FDLOG0(LOG_ERR
, &nil_pcb
, "lck_grp_attr_alloc_init failed");
3917 g_init_result
= ENOMEM
;
3921 flow_divert_mtx_grp
= lck_grp_alloc_init(FLOW_DIVERT_CONTROL_NAME
, flow_divert_grp_attr
);
3922 if (flow_divert_mtx_grp
== NULL
) {
3923 FDLOG0(LOG_ERR
, &nil_pcb
, "lck_grp_alloc_init failed");
3924 g_init_result
= ENOMEM
;
3928 flow_divert_mtx_attr
= lck_attr_alloc_init();
3929 if (flow_divert_mtx_attr
== NULL
) {
3930 FDLOG0(LOG_ERR
, &nil_pcb
, "lck_attr_alloc_init failed");
3931 g_init_result
= ENOMEM
;
3935 g_init_result
= flow_divert_kctl_init();
3936 if (g_init_result
) {
3940 lck_rw_init(&g_flow_divert_group_lck
, flow_divert_mtx_grp
, flow_divert_mtx_attr
);
3943 if (g_init_result
!= 0) {
3944 if (flow_divert_mtx_attr
!= NULL
) {
3945 lck_attr_free(flow_divert_mtx_attr
);
3946 flow_divert_mtx_attr
= NULL
;
3948 if (flow_divert_mtx_grp
!= NULL
) {
3949 lck_grp_free(flow_divert_mtx_grp
);
3950 flow_divert_mtx_grp
= NULL
;
3952 if (flow_divert_grp_attr
!= NULL
) {
3953 lck_grp_attr_free(flow_divert_grp_attr
);
3954 flow_divert_grp_attr
= NULL
;
3957 if (g_flow_divert_kctl_ref
!= NULL
) {
3958 ctl_deregister(g_flow_divert_kctl_ref
);
3959 g_flow_divert_kctl_ref
= NULL
;