Lines Matching refs:fd_cb
227 struct flow_divert_pcb *fd_cb = NULL; in flow_divert_pcb_lookup() local
232 fd_cb = RB_FIND(fd_pcb_tree, &group->pcb_tree, &key_item); in flow_divert_pcb_lookup()
233 FDRETAIN(fd_cb); in flow_divert_pcb_lookup()
236 return fd_cb; in flow_divert_pcb_lookup()
240 flow_divert_group_lookup(uint32_t ctl_unit, struct flow_divert_pcb *fd_cb) in flow_divert_group_lookup() argument
245 if (fd_cb != NULL) { in flow_divert_group_lookup()
246 FDLOG0(LOG_ERR, fd_cb, "No active groups, flow divert cannot be used for this socket"); in flow_divert_group_lookup()
249 FDLOG(LOG_ERR, fd_cb, "Cannot lookup group with invalid control unit (%u)", ctl_unit); in flow_divert_group_lookup()
253 if (fd_cb != NULL) { in flow_divert_group_lookup()
254 …FDLOG(LOG_ERR, fd_cb, "Group for control unit %u is NULL, flow divert cannot be used for this sock… in flow_divert_group_lookup()
265 flow_divert_pcb_insert(struct flow_divert_pcb *fd_cb, struct flow_divert_group *group) in flow_divert_pcb_insert() argument
270 if (NULL == RB_INSERT(fd_pcb_tree, &group->pcb_tree, fd_cb)) { in flow_divert_pcb_insert()
271 fd_cb->group = group; in flow_divert_pcb_insert()
272 fd_cb->control_group_unit = group->ctl_unit; in flow_divert_pcb_insert()
273 FDRETAIN(fd_cb); /* The group now has a reference */ in flow_divert_pcb_insert()
275 …FDLOG(LOG_ERR, fd_cb, "Group %u already contains a PCB with hash %u", group->ctl_unit, fd_cb->hash… in flow_divert_pcb_insert()
279 FDLOG(LOG_ERR, fd_cb, "Group %u is defunct, cannot insert", group->ctl_unit); in flow_divert_pcb_insert()
287 flow_divert_add_to_group(struct flow_divert_pcb *fd_cb, uint32_t ctl_unit) in flow_divert_add_to_group() argument
295 group = flow_divert_group_lookup(ctl_unit, fd_cb); in flow_divert_add_to_group()
312 fd_cb->hash = net_flowhash(key, sizeof(key), g_hash_seed); in flow_divert_add_to_group()
321 if (NULL != RB_FIND(fd_pcb_tree, &curr_group->pcb_tree, fd_cb)) { in flow_divert_add_to_group()
330 error = flow_divert_pcb_insert(fd_cb, group); in flow_divert_add_to_group()
335 FDLOG0(LOG_ERR, fd_cb, "Failed to create a unique hash"); in flow_divert_add_to_group()
336 fd_cb->hash = 0; in flow_divert_add_to_group()
359 flow_divert_pcb_destroy(struct flow_divert_pcb *fd_cb) in flow_divert_pcb_destroy() argument
361 FDLOG(LOG_INFO, fd_cb, "Destroying, app tx %u, tunnel tx %u, tunnel rx %u", in flow_divert_pcb_destroy()
362 fd_cb->bytes_written_by_app, fd_cb->bytes_sent, fd_cb->bytes_received); in flow_divert_pcb_destroy()
364 if (fd_cb->connect_token != NULL) { in flow_divert_pcb_destroy()
365 mbuf_freem(fd_cb->connect_token); in flow_divert_pcb_destroy()
367 if (fd_cb->connect_packet != NULL) { in flow_divert_pcb_destroy()
368 mbuf_freem(fd_cb->connect_packet); in flow_divert_pcb_destroy()
370 if (fd_cb->app_data != NULL) { in flow_divert_pcb_destroy()
371 kfree_data(fd_cb->app_data, fd_cb->app_data_length); in flow_divert_pcb_destroy()
373 if (fd_cb->original_remote_endpoint != NULL) { in flow_divert_pcb_destroy()
374 free_sockaddr(fd_cb->original_remote_endpoint); in flow_divert_pcb_destroy()
376 zfree(flow_divert_pcb_zone, fd_cb); in flow_divert_pcb_destroy()
380 flow_divert_pcb_remove(struct flow_divert_pcb *fd_cb) in flow_divert_pcb_remove() argument
382 if (fd_cb->group != NULL) { in flow_divert_pcb_remove()
383 struct flow_divert_group *group = fd_cb->group; in flow_divert_pcb_remove()
385 …FDLOG(LOG_INFO, fd_cb, "Removing from group %d, ref count = %d", group->ctl_unit, fd_cb->ref_count… in flow_divert_pcb_remove()
386 RB_REMOVE(fd_pcb_tree, &group->pcb_tree, fd_cb); in flow_divert_pcb_remove()
387 fd_cb->group = NULL; in flow_divert_pcb_remove()
388 FDRELEASE(fd_cb); /* Release the group's reference */ in flow_divert_pcb_remove()
394 flow_divert_packet_init(struct flow_divert_pcb *fd_cb, uint8_t packet_type, mbuf_t *packet) in flow_divert_packet_init() argument
401 FDLOG(LOG_ERR, fd_cb, "failed to allocate the header mbuf: %d", error); in flow_divert_packet_init()
406 hdr.conn_id = htonl(fd_cb->hash); in flow_divert_packet_init()
411 FDLOG(LOG_ERR, fd_cb, "mbuf_copyback(hdr) failed: %d", error); in flow_divert_packet_init()
609 flow_divert_add_data_statistics(struct flow_divert_pcb *fd_cb, size_t data_len, Boolean send) in flow_divert_add_data_statistics() argument
617 inp = sotoinpcb(fd_cb->so); in flow_divert_add_data_statistics()
644 flow_divert_check_no_cellular(struct flow_divert_pcb *fd_cb) in flow_divert_check_no_cellular() argument
646 struct inpcb *inp = sotoinpcb(fd_cb->so); in flow_divert_check_no_cellular()
655 FDLOG0(LOG_ERR, fd_cb, "Cellular is denied"); in flow_divert_check_no_cellular()
663 flow_divert_check_no_expensive(struct flow_divert_pcb *fd_cb) in flow_divert_check_no_expensive() argument
665 struct inpcb *inp = sotoinpcb(fd_cb->so); in flow_divert_check_no_expensive()
674 FDLOG0(LOG_ERR, fd_cb, "Expensive is denied"); in flow_divert_check_no_expensive()
682 flow_divert_check_no_constrained(struct flow_divert_pcb *fd_cb) in flow_divert_check_no_constrained() argument
684 struct inpcb *inp = sotoinpcb(fd_cb->so); in flow_divert_check_no_constrained()
693 FDLOG0(LOG_ERR, fd_cb, "Constrained is denied"); in flow_divert_check_no_constrained()
701 flow_divert_update_closed_state(struct flow_divert_pcb *fd_cb, int how, Boolean tunnel) in flow_divert_update_closed_state() argument
704 fd_cb->flags |= FLOW_DIVERT_WRITE_CLOSED; in flow_divert_update_closed_state()
705 if (tunnel || !(fd_cb->flags & FLOW_DIVERT_CONNECT_STARTED)) { in flow_divert_update_closed_state()
706 fd_cb->flags |= FLOW_DIVERT_TUNNEL_WR_CLOSED; in flow_divert_update_closed_state()
708 sbflush(&fd_cb->so->so_snd); in flow_divert_update_closed_state()
712 fd_cb->flags |= FLOW_DIVERT_READ_CLOSED; in flow_divert_update_closed_state()
713 if (tunnel || !(fd_cb->flags & FLOW_DIVERT_CONNECT_STARTED)) { in flow_divert_update_closed_state()
714 fd_cb->flags |= FLOW_DIVERT_TUNNEL_RD_CLOSED; in flow_divert_update_closed_state()
993 flow_divert_add_proc_info(struct flow_divert_pcb *fd_cb, proc_t proc, const char *signing_id, mbuf_… in flow_divert_add_proc_info() argument
1006 FDLOG0(LOG_ERR, fd_cb, "Signature of proc is invalid"); in flow_divert_add_proc_info()
1011 lck_rw_lock_shared(&fd_cb->group->lck); in flow_divert_add_proc_info()
1012 if (!(fd_cb->group->flags & FLOW_DIVERT_GROUP_FLAG_NO_APP_MAP)) { in flow_divert_add_proc_info()
1014 …uint16_t result = flow_divert_trie_search(&fd_cb->group->signing_id_trie, (const uint8_t *)proc_cs… in flow_divert_add_proc_info()
1016 FDLOG(LOG_WARNING, fd_cb, "%s did not match", proc_cs_id); in flow_divert_add_proc_info()
1019 FDLOG(LOG_INFO, fd_cb, "%s matched", proc_cs_id); in flow_divert_add_proc_info()
1025 lck_rw_done(&fd_cb->group->lck); in flow_divert_add_proc_info()
1042 FDLOG(LOG_ERR, fd_cb, "failed to append the signing ID: %d", error); in flow_divert_add_proc_info()
1054 FDLOG(LOG_ERR, fd_cb, "failed to append the cdhash: %d", error); in flow_divert_add_proc_info()
1058 FDLOG0(LOG_ERR, fd_cb, "failed to get the cdhash"); in flow_divert_add_proc_info()
1071 FDLOG(LOG_ERR, fd_cb, "failed to append app audit token: %d", append_error); in flow_divert_add_proc_info()
1083 flow_divert_add_all_proc_info(struct flow_divert_pcb *fd_cb, struct socket *so, proc_t proc, const … in flow_divert_add_all_proc_info() argument
1095 FDLOG(LOG_ERR, fd_cb, "failed to find the real proc record for %d", so->last_pid); in flow_divert_add_all_proc_info()
1112 lck_rw_lock_shared(&fd_cb->group->lck); in flow_divert_add_all_proc_info()
1113 if (!(fd_cb->group->flags & FLOW_DIVERT_GROUP_FLAG_NO_APP_MAP)) { in flow_divert_add_all_proc_info()
1118 lck_rw_done(&fd_cb->group->lck); in flow_divert_add_all_proc_info()
1134 error = flow_divert_add_proc_info(fd_cb, src_proc, signing_id, connect_packet, true); in flow_divert_add_all_proc_info()
1140 error = flow_divert_add_proc_info(fd_cb, real_src_proc, NULL, connect_packet, false); in flow_divert_add_all_proc_info()
1163 flow_divert_send_packet(struct flow_divert_pcb *fd_cb, mbuf_t packet, Boolean enqueue) in flow_divert_send_packet() argument
1167 if (fd_cb->group == NULL) { in flow_divert_send_packet()
1168 FDLOG0(LOG_INFO, fd_cb, "no provider, cannot send packet"); in flow_divert_send_packet()
1169 flow_divert_update_closed_state(fd_cb, SHUT_RDWR, TRUE); in flow_divert_send_packet()
1170 flow_divert_disconnect_socket(fd_cb->so, !(fd_cb->flags & FLOW_DIVERT_IMPLICIT_CONNECT)); in flow_divert_send_packet()
1171 if (SOCK_TYPE(fd_cb->so) == SOCK_STREAM) { in flow_divert_send_packet()
1176 fd_cb->so->so_error = (uint16_t)error; in flow_divert_send_packet()
1180 lck_rw_lock_shared(&fd_cb->group->lck); in flow_divert_send_packet()
1182 if (MBUFQ_EMPTY(&fd_cb->group->send_queue)) { in flow_divert_send_packet()
1183 error = ctl_enqueuembuf(g_flow_divert_kctl_ref, fd_cb->group->ctl_unit, packet, CTL_DATA_EOR); in flow_divert_send_packet()
1190 if (!lck_rw_lock_shared_to_exclusive(&fd_cb->group->lck)) { in flow_divert_send_packet()
1191 lck_rw_lock_exclusive(&fd_cb->group->lck); in flow_divert_send_packet()
1193 MBUFQ_ENQUEUE(&fd_cb->group->send_queue, packet); in flow_divert_send_packet()
1196 OSTestAndSet(GROUP_BIT_CTL_ENQUEUE_BLOCKED, &fd_cb->group->atomic_bits); in flow_divert_send_packet()
1199 lck_rw_done(&fd_cb->group->lck); in flow_divert_send_packet()
1224 flow_divert_create_connect_packet(struct flow_divert_pcb *fd_cb, struct sockaddr *to, struct socket… in flow_divert_create_connect_packet() argument
1238 error = flow_divert_packet_init(fd_cb, FLOW_DIVERT_PKT_CONNECT, &connect_packet); in flow_divert_create_connect_packet()
1243 if (fd_cb->connect_token != NULL && (fd_cb->flags & FLOW_DIVERT_HAS_HMAC)) { in flow_divert_create_connect_packet()
1244 …int find_error = flow_divert_packet_get_tlv(fd_cb->connect_token, 0, FLOW_DIVERT_TLV_SIGNING_ID, 0… in flow_divert_create_connect_packet()
1248 …flow_divert_packet_get_tlv(fd_cb->connect_token, 0, FLOW_DIVERT_TLV_SIGNING_ID, sid_size, signing_… in flow_divert_create_connect_packet()
1249 FDLOG(LOG_INFO, fd_cb, "Got %s from token", signing_id); in flow_divert_create_connect_packet()
1254 error = flow_divert_add_all_proc_info(fd_cb, so, p, signing_id, connect_packet); in flow_divert_create_connect_packet()
1261 FDLOG(LOG_ERR, fd_cb, "Failed to add source proc info: %d", error); in flow_divert_create_connect_packet()
1267 sizeof(fd_cb->so->so_traffic_class), in flow_divert_create_connect_packet()
1268 &fd_cb->so->so_traffic_class); in flow_divert_create_connect_packet()
1273 if (SOCK_TYPE(fd_cb->so) == SOCK_STREAM) { in flow_divert_create_connect_packet()
1275 } else if (SOCK_TYPE(fd_cb->so) == SOCK_DGRAM) { in flow_divert_create_connect_packet()
1290 if (fd_cb->connect_token != NULL) { in flow_divert_create_connect_packet()
1291 unsigned int token_len = m_length(fd_cb->connect_token); in flow_divert_create_connect_packet()
1292 mbuf_concatenate(connect_packet, fd_cb->connect_token); in flow_divert_create_connect_packet()
1294 fd_cb->connect_token = NULL; in flow_divert_create_connect_packet()
1304 …if (fd_cb->local_endpoint.sa.sa_family == AF_INET || fd_cb->local_endpoint.sa.sa_family == AF_INET… in flow_divert_create_connect_packet()
1305 …_append_tlv(connect_packet, FLOW_DIVERT_TLV_LOCAL_ADDR, fd_cb->local_endpoint.sa.sa_len, &(fd_cb->… in flow_divert_create_connect_packet()
1374 flow_divert_send_connect_packet(struct flow_divert_pcb *fd_cb) in flow_divert_send_connect_packet() argument
1377 mbuf_t connect_packet = fd_cb->connect_packet; in flow_divert_send_connect_packet()
1383 FDLOG0(LOG_ERR, fd_cb, "Failed to copy the connect packet"); in flow_divert_send_connect_packet()
1387 error = flow_divert_send_packet(fd_cb, connect_packet, TRUE); in flow_divert_send_connect_packet()
1392 fd_cb->connect_packet = saved_connect_packet; in flow_divert_send_connect_packet()
1406 flow_divert_send_connect_result(struct flow_divert_pcb *fd_cb) in flow_divert_send_connect_result() argument
1412 error = flow_divert_packet_init(fd_cb, FLOW_DIVERT_PKT_CONNECT_RESULT, &packet); in flow_divert_send_connect_result()
1414 FDLOG(LOG_ERR, fd_cb, "failed to create a connect result packet: %d", error); in flow_divert_send_connect_result()
1418 rbuff_space = fd_cb->so->so_rcv.sb_hiwat; in flow_divert_send_connect_result()
1431 …if (fd_cb->local_endpoint.sa.sa_family == AF_INET || fd_cb->local_endpoint.sa.sa_family == AF_INET… in flow_divert_send_connect_result()
1432 …t_packet_append_tlv(packet, FLOW_DIVERT_TLV_LOCAL_ADDR, fd_cb->local_endpoint.sa.sa_len, &(fd_cb->… in flow_divert_send_connect_result()
1438 error = flow_divert_send_packet(fd_cb, packet, TRUE); in flow_divert_send_connect_result()
1452 flow_divert_send_close(struct flow_divert_pcb *fd_cb, int how) in flow_divert_send_close() argument
1458 error = flow_divert_packet_init(fd_cb, FLOW_DIVERT_PKT_CLOSE, &packet); in flow_divert_send_close()
1460 FDLOG(LOG_ERR, fd_cb, "failed to create a close packet: %d", error); in flow_divert_send_close()
1466 FDLOG(LOG_ERR, fd_cb, "failed to add the error code TLV: %d", error); in flow_divert_send_close()
1473 FDLOG(LOG_ERR, fd_cb, "failed to add the how flag: %d", error); in flow_divert_send_close()
1477 error = flow_divert_send_packet(fd_cb, packet, TRUE); in flow_divert_send_close()
1491 flow_divert_tunnel_how_closed(struct flow_divert_pcb *fd_cb) in flow_divert_tunnel_how_closed() argument
1493 if ((fd_cb->flags & (FLOW_DIVERT_TUNNEL_RD_CLOSED | FLOW_DIVERT_TUNNEL_WR_CLOSED)) == in flow_divert_tunnel_how_closed()
1496 } else if (fd_cb->flags & FLOW_DIVERT_TUNNEL_RD_CLOSED) { in flow_divert_tunnel_how_closed()
1498 } else if (fd_cb->flags & FLOW_DIVERT_TUNNEL_WR_CLOSED) { in flow_divert_tunnel_how_closed()
1510 flow_divert_send_close_if_needed(struct flow_divert_pcb *fd_cb) in flow_divert_send_close_if_needed() argument
1515 if (fd_cb->so->so_snd.sb_cc == 0) { in flow_divert_send_close_if_needed()
1516 …if ((fd_cb->flags & (FLOW_DIVERT_READ_CLOSED | FLOW_DIVERT_TUNNEL_RD_CLOSED)) == FLOW_DIVERT_READ_… in flow_divert_send_close_if_needed()
1520 …if ((fd_cb->flags & (FLOW_DIVERT_WRITE_CLOSED | FLOW_DIVERT_TUNNEL_WR_CLOSED)) == FLOW_DIVERT_WRIT… in flow_divert_send_close_if_needed()
1531 FDLOG(LOG_INFO, fd_cb, "sending close, how = %d", how); in flow_divert_send_close_if_needed()
1532 if (flow_divert_send_close(fd_cb, how) != ENOBUFS) { in flow_divert_send_close_if_needed()
1535 fd_cb->flags |= FLOW_DIVERT_TUNNEL_WR_CLOSED; in flow_divert_send_close_if_needed()
1538 fd_cb->flags |= FLOW_DIVERT_TUNNEL_RD_CLOSED; in flow_divert_send_close_if_needed()
1543 if (flow_divert_tunnel_how_closed(fd_cb) == SHUT_RDWR) { in flow_divert_send_close_if_needed()
1544 flow_divert_disconnect_socket(fd_cb->so, !(fd_cb->flags & FLOW_DIVERT_IMPLICIT_CONNECT)); in flow_divert_send_close_if_needed()
1549 flow_divert_send_data_packet(struct flow_divert_pcb *fd_cb, mbuf_t data, size_t data_len, struct so… in flow_divert_send_data_packet() argument
1555 error = flow_divert_packet_init(fd_cb, FLOW_DIVERT_PKT_DATA, &packet); in flow_divert_send_data_packet()
1557 FDLOG(LOG_ERR, fd_cb, "flow_divert_packet_init failed: %d", error); in flow_divert_send_data_packet()
1564 FDLOG(LOG_ERR, fd_cb, "flow_divert_append_target_endpoint_tlv() failed: %d", error); in flow_divert_send_data_packet()
1576 error = flow_divert_send_packet(fd_cb, packet, force); in flow_divert_send_data_packet()
1578 fd_cb->bytes_sent += data_len; in flow_divert_send_data_packet()
1579 flow_divert_add_data_statistics(fd_cb, data_len, TRUE); in flow_divert_send_data_packet()
1596 flow_divert_send_buffered_data(struct flow_divert_pcb *fd_cb, Boolean force) in flow_divert_send_buffered_data() argument
1603 to_send = fd_cb->so->so_snd.sb_cc; in flow_divert_send_buffered_data()
1604 buffer = fd_cb->so->so_snd.sb_mb; in flow_divert_send_buffered_data()
1607 FDLOG(LOG_ERR, fd_cb, "Send buffer is NULL, but size is supposed to be %lu", to_send); in flow_divert_send_buffered_data()
1612 if (!force && (to_send > fd_cb->send_window)) { in flow_divert_send_buffered_data()
1613 to_send = fd_cb->send_window; in flow_divert_send_buffered_data()
1616 if (SOCK_TYPE(fd_cb->so) == SOCK_STREAM) { in flow_divert_send_buffered_data()
1628 FDLOG(LOG_ERR, fd_cb, "mbuf_copym failed: %d", error); in flow_divert_send_buffered_data()
1632 error = flow_divert_send_data_packet(fd_cb, data, data_len, NULL, force); in flow_divert_send_buffered_data()
1642 sbdrop(&fd_cb->so->so_snd, (int)sent); in flow_divert_send_buffered_data()
1643 sowwakeup(fd_cb->so); in flow_divert_send_buffered_data()
1644 } else if (SOCK_TYPE(fd_cb->so) == SOCK_DGRAM) { in flow_divert_send_buffered_data()
1663 FDLOG0(LOG_ERR, fd_cb, "failed to find type MT_DATA in the mbuf chain."); in flow_divert_send_buffered_data()
1669 FDLOG(LOG_DEBUG, fd_cb, "mbuf_copym() data_len = %lu", data_len); in flow_divert_send_buffered_data()
1672 FDLOG(LOG_ERR, fd_cb, "mbuf_copym failed: %d", error); in flow_divert_send_buffered_data()
1678 error = flow_divert_send_data_packet(fd_cb, data, data_len, toaddr, force); in flow_divert_send_buffered_data()
1688 (void) sbdroprecord(&(fd_cb->so->so_snd)); in flow_divert_send_buffered_data()
1693 FDLOG(LOG_DEBUG, fd_cb, "sent %lu bytes of buffered data", sent); in flow_divert_send_buffered_data()
1694 if (fd_cb->send_window >= sent) { in flow_divert_send_buffered_data()
1695 fd_cb->send_window -= sent; in flow_divert_send_buffered_data()
1697 fd_cb->send_window = 0; in flow_divert_send_buffered_data()
1703 flow_divert_send_app_data(struct flow_divert_pcb *fd_cb, mbuf_t data, struct sockaddr *toaddr) in flow_divert_send_app_data() argument
1708 if (to_send > fd_cb->send_window) { in flow_divert_send_app_data()
1709 to_send = fd_cb->send_window; in flow_divert_send_app_data()
1712 if (fd_cb->so->so_snd.sb_cc > 0) { in flow_divert_send_app_data()
1716 if (SOCK_TYPE(fd_cb->so) == SOCK_STREAM) { in flow_divert_send_app_data()
1734 FDLOG(LOG_ERR, fd_cb, "mbuf_split failed: %d", error); in flow_divert_send_app_data()
1742 error = flow_divert_send_data_packet(fd_cb, pkt_data, pkt_data_len, NULL, FALSE); in flow_divert_send_app_data()
1752 fd_cb->send_window -= sent; in flow_divert_send_app_data()
1757 if (sbspace(&fd_cb->so->so_snd) > 0) { in flow_divert_send_app_data()
1758 if (!sbappendstream(&fd_cb->so->so_snd, pkt_data)) { in flow_divert_send_app_data()
1759 …FDLOG(LOG_ERR, fd_cb, "sbappendstream failed with pkt_data, send buffer size = %u, send_window = %… in flow_divert_send_app_data()
1760 fd_cb->so->so_snd.sb_cc, fd_cb->send_window); in flow_divert_send_app_data()
1769 if (sbspace(&fd_cb->so->so_snd) > 0) { in flow_divert_send_app_data()
1770 if (!sbappendstream(&fd_cb->so->so_snd, remaining_data)) { in flow_divert_send_app_data()
1771 …FDLOG(LOG_ERR, fd_cb, "sbappendstream failed with remaining_data, send buffer size = %u, send_wind… in flow_divert_send_app_data()
1772 fd_cb->so->so_snd.sb_cc, fd_cb->send_window); in flow_divert_send_app_data()
1779 } else if (SOCK_TYPE(fd_cb->so) == SOCK_DGRAM) { in flow_divert_send_app_data()
1781 error = flow_divert_send_data_packet(fd_cb, data, to_send, toaddr, FALSE); in flow_divert_send_app_data()
1783 FDLOG(LOG_ERR, fd_cb, "flow_divert_send_data_packet failed. send data size = %lu", to_send); in flow_divert_send_app_data()
1788 fd_cb->send_window -= to_send; in flow_divert_send_app_data()
1792 if (sbspace(&fd_cb->so->so_snd) >= (int)mbuf_pkthdr_len(data)) { in flow_divert_send_app_data()
1794 if (!sbappendaddr(&fd_cb->so->so_snd, toaddr, data, NULL, &error)) { in flow_divert_send_app_data()
1795 FDLOG(LOG_ERR, fd_cb, in flow_divert_send_app_data()
1797 fd_cb->so->so_snd.sb_cc, fd_cb->send_window, error); in flow_divert_send_app_data()
1801 if (!sbappendrecord(&fd_cb->so->so_snd, data)) { in flow_divert_send_app_data()
1802 FDLOG(LOG_ERR, fd_cb, in flow_divert_send_app_data()
1804 fd_cb->so->so_snd.sb_cc, fd_cb->send_window, error); in flow_divert_send_app_data()
1820 flow_divert_send_read_notification(struct flow_divert_pcb *fd_cb) in flow_divert_send_read_notification() argument
1825 error = flow_divert_packet_init(fd_cb, FLOW_DIVERT_PKT_READ_NOTIFY, &packet); in flow_divert_send_read_notification()
1827 FDLOG(LOG_ERR, fd_cb, "failed to create a read notification packet: %d", error); in flow_divert_send_read_notification()
1831 error = flow_divert_send_packet(fd_cb, packet, TRUE); in flow_divert_send_read_notification()
1845 flow_divert_send_traffic_class_update(struct flow_divert_pcb *fd_cb, int traffic_class) in flow_divert_send_traffic_class_update() argument
1850 error = flow_divert_packet_init(fd_cb, FLOW_DIVERT_PKT_PROPERTIES_UPDATE, &packet); in flow_divert_send_traffic_class_update()
1852 FDLOG(LOG_ERR, fd_cb, "failed to create a properties update packet: %d", error); in flow_divert_send_traffic_class_update()
1858 FDLOG(LOG_ERR, fd_cb, "failed to add the traffic class: %d", error); in flow_divert_send_traffic_class_update()
1862 error = flow_divert_send_packet(fd_cb, packet, TRUE); in flow_divert_send_traffic_class_update()
1876 flow_divert_set_local_endpoint(struct flow_divert_pcb *fd_cb, struct sockaddr *local_endpoint) in flow_divert_set_local_endpoint() argument
1878 struct inpcb *inp = sotoinpcb(fd_cb->so); in flow_divert_set_local_endpoint()
1881 …if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) && (fd_cb->flags & FLOW_DIVERT_SHOULD_SET_LOCAL_ADDR… in flow_divert_set_local_endpoint()
1882 fd_cb->flags |= FLOW_DIVERT_DID_SET_LOCAL_ADDR; in flow_divert_set_local_endpoint()
1891 if (inp->inp_laddr.s_addr == INADDR_ANY && (fd_cb->flags & FLOW_DIVERT_SHOULD_SET_LOCAL_ADDR)) { in flow_divert_set_local_endpoint()
1892 fd_cb->flags |= FLOW_DIVERT_DID_SET_LOCAL_ADDR; in flow_divert_set_local_endpoint()
1902 flow_divert_set_remote_endpoint(struct flow_divert_pcb *fd_cb, struct sockaddr *remote_endpoint) in flow_divert_set_remote_endpoint() argument
1904 struct inpcb *inp = sotoinpcb(fd_cb->so); in flow_divert_set_remote_endpoint()
1953 flow_divert_try_next_group(struct flow_divert_pcb *fd_cb) in flow_divert_try_next_group() argument
1956 uint32_t policy_control_unit = fd_cb->policy_control_unit; in flow_divert_try_next_group()
1958 flow_divert_pcb_remove(fd_cb); in flow_divert_try_next_group()
1963 …uint32_t next_ctl_unit = flow_divert_derive_kernel_control_unit(&policy_control_unit, &(fd_cb->agg… in flow_divert_try_next_group()
1965 if (fd_cb->control_group_unit == next_ctl_unit) { in flow_divert_try_next_group()
1966 …FDLOG0(LOG_NOTICE, fd_cb, "Next control unit is the same as the current control unit, disabling fl… in flow_divert_try_next_group()
1972 FDLOG0(LOG_NOTICE, fd_cb, "No more valid control units, disabling flow divert"); in flow_divert_try_next_group()
1977 next_group = flow_divert_group_lookup(next_ctl_unit, fd_cb); in flow_divert_try_next_group()
1979 FDLOG(LOG_NOTICE, fd_cb, "Group for control unit %u does not exist", next_ctl_unit); in flow_divert_try_next_group()
1983 FDLOG(LOG_NOTICE, fd_cb, "Moving from %u to %u", fd_cb->control_group_unit, next_ctl_unit); in flow_divert_try_next_group()
1985 error = flow_divert_pcb_insert(fd_cb, next_group); in flow_divert_try_next_group()
1988 fd_cb->flags |= FLOW_DIVERT_FLOW_IS_TRANSPARENT; in flow_divert_try_next_group()
1990 fd_cb->flags &= ~FLOW_DIVERT_FLOW_IS_TRANSPARENT; in flow_divert_try_next_group()
1994 } while (fd_cb->group == NULL); in flow_divert_try_next_group()
1996 if (fd_cb->group == NULL) { in flow_divert_try_next_group()
2000 error = flow_divert_send_connect_packet(fd_cb); in flow_divert_try_next_group()
2002 …FDLOG(LOG_NOTICE, fd_cb, "Failed to send the connect packet to %u, disabling flow divert", fd_cb->… in flow_divert_try_next_group()
2003 flow_divert_pcb_remove(fd_cb); in flow_divert_try_next_group()
2011 flow_divert_disable(struct flow_divert_pcb *fd_cb) in flow_divert_disable() argument
2017 struct sockaddr *remote_endpoint = fd_cb->original_remote_endpoint; in flow_divert_disable()
2018 bool do_connect = !(fd_cb->flags & FLOW_DIVERT_IMPLICIT_CONNECT); in flow_divert_disable()
2021 so = fd_cb->so; in flow_divert_disable()
2026 FDLOG0(LOG_NOTICE, fd_cb, "Skipped all flow divert services, disabling flow divert"); in flow_divert_disable()
2030 inp->inp_vflag = fd_cb->original_vflag; in flow_divert_disable()
2037 if (fd_cb->flags & FLOW_DIVERT_DID_SET_LOCAL_ADDR) { in flow_divert_disable()
2042 inp->inp_last_outifp = fd_cb->original_last_outifp; in flow_divert_disable()
2043 inp->in6p_last_outifp = fd_cb->original_last_outifp6; in flow_divert_disable()
2049 fd_cb->so = NULL; in flow_divert_disable()
2051 FDRELEASE(fd_cb); /* Release the socket's reference */ in flow_divert_disable()
2065 FDLOG(LOG_ERR, fd_cb, "Failed to connect using the socket's original protocol: %d", error); in flow_divert_disable()
2083 FDLOG0(LOG_ERR, fd_cb, "Failed to copy the mbuf chain in the socket's send buffer"); in flow_divert_disable()
2101 …FDLOG(LOG_ERR, fd_cb, "Failed to send queued TCP data using the socket's original protocol: %d", e… in flow_divert_disable()
2153 FDLOG0(LOG_NOTICE, fd_cb, "Failed to get the remote address from the buffer"); in flow_divert_disable()
2158 FDLOG0(LOG_ERR, fd_cb, "Buffered record does not contain any data"); in flow_divert_disable()
2164 FDLOG0(LOG_ERR, fd_cb, "Buffered data does not have a packet header"); in flow_divert_disable()
2189 …FDLOG(LOG_ERR, fd_cb, "Failed to send queued UDP data using the socket's original protocol: %d", e… in flow_divert_disable()
2205 flow_divert_scope(struct flow_divert_pcb *fd_cb, int out_if_index, bool derive_new_address) in flow_divert_scope() argument
2213 so = fd_cb->so; in flow_divert_scope()
2239 …FDLOG(LOG_ERR, fd_cb, "failed to scope to %d because inp_bindif returned %d", out_if_index, error); in flow_divert_scope()
2243 if (derive_new_address && fd_cb->original_remote_endpoint != NULL) { in flow_divert_scope()
2247 …error = in6_pcbladdr(inp, fd_cb->original_remote_endpoint, &(fd_cb->local_endpoint.sin6.sin6_addr)… in flow_divert_scope()
2250 …error = in_pcbladdr(inp, fd_cb->original_remote_endpoint, &(fd_cb->local_endpoint.sin.sin_addr), I… in flow_divert_scope()
2254 …FDLOG(LOG_WARNING, fd_cb, "failed to derive a new local address from %d because in_pcbladdr return… in flow_divert_scope()
2282 flow_divert_handle_connect_result(struct flow_divert_pcb *fd_cb, mbuf_t packet, int offset) in flow_divert_handle_connect_result() argument
2298 FDLOG(LOG_ERR, fd_cb, "failed to get the connect result: %d", error); in flow_divert_handle_connect_result()
2303 FDLOG(LOG_INFO, fd_cb, "received connect result %u", connect_error); in flow_divert_handle_connect_result()
2307 FDLOG(LOG_ERR, fd_cb, "failed to get the send window: %d", error); in flow_divert_handle_connect_result()
2313 FDLOG0(LOG_INFO, fd_cb, "No control unit provided in the connect result"); in flow_divert_handle_connect_result()
2318 FDLOG0(LOG_INFO, fd_cb, "No local address provided"); in flow_divert_handle_connect_result()
2323 FDLOG0(LOG_INFO, fd_cb, "No remote address provided"); in flow_divert_handle_connect_result()
2328 FDLOG0(LOG_INFO, fd_cb, "No output if index provided"); in flow_divert_handle_connect_result()
2333 FDLOG0(LOG_INFO, fd_cb, "No application data provided in connect result"); in flow_divert_handle_connect_result()
2338 FDLOCK(fd_cb); in flow_divert_handle_connect_result()
2339 if (fd_cb->so != NULL) { in flow_divert_handle_connect_result()
2341 struct socket *so = fd_cb->so; in flow_divert_handle_connect_result()
2347 FDLOG0(LOG_NOTICE, fd_cb, "socket is not attached any more, ignoring connect result"); in flow_divert_handle_connect_result()
2352 FDLOG0(LOG_ERR, fd_cb, "TCP socket is not in the connecting state, ignoring connect result"); in flow_divert_handle_connect_result()
2367 fd_cb->local_endpoint = local_endpoint; in flow_divert_handle_connect_result()
2370 fd_cb->local_endpoint.sin.sin_port = local_endpoint.sin.sin_port; in flow_divert_handle_connect_result()
2376 fd_cb->local_endpoint = local_endpoint; in flow_divert_handle_connect_result()
2379 fd_cb->local_endpoint.sin6.sin6_port = local_endpoint.sin6.sin6_port; in flow_divert_handle_connect_result()
2384 flow_divert_scope(fd_cb, out_if_index, !local_address_is_valid); in flow_divert_handle_connect_result()
2385 flow_divert_set_local_endpoint(fd_cb, SA(&fd_cb->local_endpoint)); in flow_divert_handle_connect_result()
2393 flow_divert_set_remote_endpoint(fd_cb, SA(&remote_endpoint)); in flow_divert_handle_connect_result()
2402 FDLOG(LOG_INFO, fd_cb, "Got %u bytes of app data from the connect result", app_data_length); in flow_divert_handle_connect_result()
2403 if (fd_cb->app_data != NULL) { in flow_divert_handle_connect_result()
2404 kfree_data(fd_cb->app_data, fd_cb->app_data_length); in flow_divert_handle_connect_result()
2406 fd_cb->app_data = app_data; in flow_divert_handle_connect_result()
2407 fd_cb->app_data_length = app_data_length; in flow_divert_handle_connect_result()
2409 …FDLOG(LOG_ERR, fd_cb, "Failed to copy %u bytes of application data from the connect result packet"… in flow_divert_handle_connect_result()
2413 …FDLOG(LOG_ERR, fd_cb, "Failed to allocate a buffer of size %u to hold the application data from th… in flow_divert_handle_connect_result()
2421 if (fd_cb->group == NULL) { in flow_divert_handle_connect_result()
2432 FDLOG(LOG_ERR, fd_cb, "Connect result contains an invalid control unit: %u", ctl_unit); in flow_divert_handle_connect_result()
2437 grp = flow_divert_group_lookup(ctl_unit, fd_cb); in flow_divert_handle_connect_result()
2443 flow_divert_pcb_remove(fd_cb); in flow_divert_handle_connect_result()
2444 insert_error = flow_divert_pcb_insert(fd_cb, grp); in flow_divert_handle_connect_result()
2453 fd_cb->send_window = ntohl(send_window); in flow_divert_handle_connect_result()
2457 FDLOG0(LOG_INFO, fd_cb, "sending connect result"); in flow_divert_handle_connect_result()
2458 error = flow_divert_send_connect_result(fd_cb); in flow_divert_handle_connect_result()
2462 if (connect_error && fd_cb->control_group_unit != fd_cb->policy_control_unit) { in flow_divert_handle_connect_result()
2463 error = flow_divert_try_next_group(fd_cb); in flow_divert_handle_connect_result()
2464 if (error && fd_cb->policy_control_unit == 0) { in flow_divert_handle_connect_result()
2465 flow_divert_disable(fd_cb); in flow_divert_handle_connect_result()
2473 flow_divert_update_closed_state(fd_cb, SHUT_RDWR, FALSE); in flow_divert_handle_connect_result()
2475 flow_divert_send_close_if_needed(fd_cb); in flow_divert_handle_connect_result()
2477 flow_divert_update_closed_state(fd_cb, SHUT_RDWR, TRUE); in flow_divert_handle_connect_result()
2480 flow_divert_disconnect_socket(so, !(fd_cb->flags & FLOW_DIVERT_IMPLICIT_CONNECT)); in flow_divert_handle_connect_result()
2495 flow_divert_send_buffered_data(fd_cb, FALSE); in flow_divert_handle_connect_result()
2500 if (fd_cb->connect_packet != NULL) { in flow_divert_handle_connect_result()
2501 mbuf_freem(fd_cb->connect_packet); in flow_divert_handle_connect_result()
2502 fd_cb->connect_packet = NULL; in flow_divert_handle_connect_result()
2506 free_sockaddr(fd_cb->original_remote_endpoint); in flow_divert_handle_connect_result()
2510 FDUNLOCK(fd_cb); in flow_divert_handle_connect_result()
2514 flow_divert_handle_close(struct flow_divert_pcb *fd_cb, mbuf_t packet, int offset) in flow_divert_handle_close() argument
2522 FDLOG(LOG_ERR, fd_cb, "failed to get the close error: %d", error); in flow_divert_handle_close()
2528 FDLOG(LOG_ERR, fd_cb, "failed to get the close how flag: %d", error); in flow_divert_handle_close()
2534 FDLOG(LOG_INFO, fd_cb, "close received, how = %d", how); in flow_divert_handle_close()
2536 FDLOCK(fd_cb); in flow_divert_handle_close()
2537 if (fd_cb->so != NULL) { in flow_divert_handle_close()
2538 …bool is_connected = (SOCK_TYPE(fd_cb->so) == SOCK_STREAM || !(fd_cb->flags & FLOW_DIVERT_IMPLICIT_… in flow_divert_handle_close()
2539 socket_lock(fd_cb->so, 0); in flow_divert_handle_close()
2541 if (!(fd_cb->so->so_flags & SOF_FLOW_DIVERT)) { in flow_divert_handle_close()
2542 FDLOG0(LOG_NOTICE, fd_cb, "socket is not attached any more, ignoring close from provider"); in flow_divert_handle_close()
2546 fd_cb->so->so_error = (uint16_t)ntohl(close_error); in flow_divert_handle_close()
2548 flow_divert_update_closed_state(fd_cb, how, TRUE); in flow_divert_handle_close()
2551 how = flow_divert_tunnel_how_closed(fd_cb); in flow_divert_handle_close()
2553 flow_divert_disconnect_socket(fd_cb->so, is_connected); in flow_divert_handle_close()
2555 socantrcvmore(fd_cb->so); in flow_divert_handle_close()
2557 socantsendmore(fd_cb->so); in flow_divert_handle_close()
2560 socket_unlock(fd_cb->so, 0); in flow_divert_handle_close()
2562 FDUNLOCK(fd_cb); in flow_divert_handle_close()
2566 flow_divert_create_control_mbuf(struct flow_divert_pcb *fd_cb) in flow_divert_create_control_mbuf() argument
2568 struct inpcb *inp = sotoinpcb(fd_cb->so); in flow_divert_create_control_mbuf()
2573 fd_cb->local_endpoint.sa.sa_family == AF_INET && in flow_divert_create_control_mbuf()
2575 …return sbcreatecontrol((caddr_t)&(fd_cb->local_endpoint.sin.sin_addr), sizeof(struct in_addr), IP_… in flow_divert_create_control_mbuf()
2577 fd_cb->local_endpoint.sa.sa_family == AF_INET6 && in flow_divert_create_control_mbuf()
2581 pi6.ipi6_addr = fd_cb->local_endpoint.sin6.sin6_addr; in flow_divert_create_control_mbuf()
2589 flow_divert_handle_data(struct flow_divert_pcb *fd_cb, mbuf_t packet, size_t offset) in flow_divert_handle_data() argument
2593 FDLOCK(fd_cb); in flow_divert_handle_data()
2594 if (fd_cb->so != NULL) { in flow_divert_handle_data()
2602 socket_lock(fd_cb->so, 0); in flow_divert_handle_data()
2604 if (!(fd_cb->so->so_flags & SOF_FLOW_DIVERT)) { in flow_divert_handle_data()
2605 FDLOG0(LOG_NOTICE, fd_cb, "socket is not attached any more, ignoring inbound data"); in flow_divert_handle_data()
2609 if (sbspace(&fd_cb->so->so_rcv) == 0) { in flow_divert_handle_data()
2611 fd_cb->flags |= FLOW_DIVERT_NOTIFY_ON_RECEIVED; in flow_divert_handle_data()
2612 …FDLOG0(LOG_INFO, fd_cb, "Receive buffer is full, will send read notification when app reads some d… in flow_divert_handle_data()
2616 if (SOCK_TYPE(fd_cb->so) == SOCK_DGRAM) { in flow_divert_handle_data()
2623 FDLOG0(LOG_INFO, fd_cb, "No remote address provided"); in flow_divert_handle_data()
2633 FDLOG0(LOG_INFO, fd_cb, "Remote address is invalid"); in flow_divert_handle_data()
2641 if (fd_cb->so->so_state & SS_CANTRCVMORE) { in flow_divert_handle_data()
2642 …FDLOG(LOG_NOTICE, fd_cb, "app cannot receive any more data, dropping %lu bytes of data", data_size… in flow_divert_handle_data()
2646 if (SOCK_TYPE(fd_cb->so) != SOCK_STREAM && SOCK_TYPE(fd_cb->so) != SOCK_DGRAM) { in flow_divert_handle_data()
2647 FDLOG(LOG_ERR, fd_cb, "socket has an unsupported type: %d", SOCK_TYPE(fd_cb->so)); in flow_divert_handle_data()
2651 FDLOG(LOG_DEBUG, fd_cb, "received %lu bytes of data", data_size); in flow_divert_handle_data()
2655 FDLOG(LOG_ERR, fd_cb, "mbuf_split failed: %d", error); in flow_divert_handle_data()
2659 if (SOCK_TYPE(fd_cb->so) == SOCK_STREAM) { in flow_divert_handle_data()
2660 appended = (sbappendstream(&fd_cb->so->so_rcv, data) != 0); in flow_divert_handle_data()
2669 if (fd_cb->so->so_proto->pr_domain->dom_family == AF_INET6) { in flow_divert_handle_data()
2670 error = in6_mapped_peeraddr(fd_cb->so, &append_sa); in flow_divert_handle_data()
2672 error = in_getpeeraddr(fd_cb->so, &append_sa); in flow_divert_handle_data()
2676 FDLOG0(LOG_ERR, fd_cb, "failed to dup the socket address."); in flow_divert_handle_data()
2679 mctl = flow_divert_create_control_mbuf(fd_cb); in flow_divert_handle_data()
2681 appended = sbappendaddr(&fd_cb->so->so_rcv, append_sa, data, mctl, &append_error); in flow_divert_handle_data()
2685 FDLOG(LOG_ERR, fd_cb, "failed to append %lu bytes of data: %d", data_size, append_error); in flow_divert_handle_data()
2692 fd_cb->bytes_received += data_size; in flow_divert_handle_data()
2693 flow_divert_add_data_statistics(fd_cb, data_size, FALSE); in flow_divert_handle_data()
2697 sorwakeup(fd_cb->so); in flow_divert_handle_data()
2700 socket_unlock(fd_cb->so, 0); in flow_divert_handle_data()
2702 FDUNLOCK(fd_cb); in flow_divert_handle_data()
2708 flow_divert_handle_read_notification(struct flow_divert_pcb *fd_cb, mbuf_t packet, int offset) in flow_divert_handle_read_notification() argument
2715 FDLOG(LOG_ERR, fd_cb, "failed to get the read count: %d", error); in flow_divert_handle_read_notification()
2719 FDLOG(LOG_DEBUG, fd_cb, "received a read notification for %u bytes", ntohl(read_count)); in flow_divert_handle_read_notification()
2721 FDLOCK(fd_cb); in flow_divert_handle_read_notification()
2722 if (fd_cb->so != NULL) { in flow_divert_handle_read_notification()
2723 socket_lock(fd_cb->so, 0); in flow_divert_handle_read_notification()
2725 if (!(fd_cb->so->so_flags & SOF_FLOW_DIVERT)) { in flow_divert_handle_read_notification()
2726 FDLOG0(LOG_NOTICE, fd_cb, "socket is not attached any more, ignoring read notification"); in flow_divert_handle_read_notification()
2730 fd_cb->send_window += ntohl(read_count); in flow_divert_handle_read_notification()
2731 flow_divert_send_buffered_data(fd_cb, FALSE); in flow_divert_handle_read_notification()
2733 socket_unlock(fd_cb->so, 0); in flow_divert_handle_read_notification()
2735 FDUNLOCK(fd_cb); in flow_divert_handle_read_notification()
2796 flow_divert_handle_properties_update(struct flow_divert_pcb *fd_cb, mbuf_t packet, int offset) in flow_divert_handle_properties_update() argument
2802 FDLOG0(LOG_INFO, fd_cb, "received a properties update"); in flow_divert_handle_properties_update()
2806 FDLOG0(LOG_INFO, fd_cb, "No output if index provided in properties update"); in flow_divert_handle_properties_update()
2811 FDLOG0(LOG_INFO, fd_cb, "No application data provided in properties update"); in flow_divert_handle_properties_update()
2814 FDLOCK(fd_cb); in flow_divert_handle_properties_update()
2815 if (fd_cb->so != NULL) { in flow_divert_handle_properties_update()
2816 socket_lock(fd_cb->so, 0); in flow_divert_handle_properties_update()
2818 if (!(fd_cb->so->so_flags & SOF_FLOW_DIVERT)) { in flow_divert_handle_properties_update()
2819 FDLOG0(LOG_NOTICE, fd_cb, "socket is not attached any more, ignoring properties update"); in flow_divert_handle_properties_update()
2824 flow_divert_scope(fd_cb, out_if_index, true); in flow_divert_handle_properties_update()
2825 flow_divert_set_local_endpoint(fd_cb, SA(&fd_cb->local_endpoint)); in flow_divert_handle_properties_update()
2834 if (fd_cb->app_data != NULL) { in flow_divert_handle_properties_update()
2835 kfree_data(fd_cb->app_data, fd_cb->app_data_length); in flow_divert_handle_properties_update()
2837 fd_cb->app_data = app_data; in flow_divert_handle_properties_update()
2838 fd_cb->app_data_length = app_data_length; in flow_divert_handle_properties_update()
2840 …FDLOG(LOG_ERR, fd_cb, "Failed to copy %u bytes of application data from the properties update pack… in flow_divert_handle_properties_update()
2844 …FDLOG(LOG_ERR, fd_cb, "Failed to allocate a buffer of size %u to hold the application data from th… in flow_divert_handle_properties_update()
2848 socket_unlock(fd_cb->so, 0); in flow_divert_handle_properties_update()
2850 FDUNLOCK(fd_cb); in flow_divert_handle_properties_update()
3024 struct flow_divert_pcb *fd_cb; in flow_divert_input() local
3056 fd_cb = flow_divert_pcb_lookup(hdr.conn_id, group); /* This retains the PCB */ in flow_divert_input()
3057 if (fd_cb == NULL) { in flow_divert_input()
3066 flow_divert_handle_connect_result(fd_cb, packet, sizeof(hdr)); in flow_divert_input()
3069 flow_divert_handle_close(fd_cb, packet, sizeof(hdr)); in flow_divert_input()
3072 error = flow_divert_handle_data(fd_cb, packet, sizeof(hdr)); in flow_divert_input()
3075 flow_divert_handle_read_notification(fd_cb, packet, sizeof(hdr)); in flow_divert_input()
3078 flow_divert_handle_properties_update(fd_cb, packet, sizeof(hdr)); in flow_divert_input()
3081 FDLOG(LOG_WARNING, fd_cb, "got an unknown message type: %d", hdr.packet_type); in flow_divert_input()
3085 FDRELEASE(fd_cb); in flow_divert_input()
3095 struct flow_divert_pcb *fd_cb; in flow_divert_close_all() local
3104 RB_FOREACH(fd_cb, fd_pcb_tree, &group->pcb_tree) { in flow_divert_close_all()
3105 FDRETAIN(fd_cb); in flow_divert_close_all()
3106 SLIST_INSERT_HEAD(&tmp_list, fd_cb, tmp_list_entry); in flow_divert_close_all()
3114 fd_cb = SLIST_FIRST(&tmp_list); in flow_divert_close_all()
3115 FDLOCK(fd_cb); in flow_divert_close_all()
3117 if (fd_cb->so != NULL) { in flow_divert_close_all()
3118 socket_lock(fd_cb->so, 0); in flow_divert_close_all()
3119 flow_divert_pcb_remove(fd_cb); in flow_divert_close_all()
3120 flow_divert_update_closed_state(fd_cb, SHUT_RDWR, TRUE); in flow_divert_close_all()
3121 fd_cb->so->so_error = ECONNABORTED; in flow_divert_close_all()
3122 flow_divert_disconnect_socket(fd_cb->so, !(fd_cb->flags & FLOW_DIVERT_IMPLICIT_CONNECT)); in flow_divert_close_all()
3123 socket_unlock(fd_cb->so, 0); in flow_divert_close_all()
3125 FDUNLOCK(fd_cb); in flow_divert_close_all()
3126 FDRELEASE(fd_cb); in flow_divert_close_all()
3133 struct flow_divert_pcb *fd_cb = so->so_fd_pcb; in flow_divert_detach() local
3142 FDLOG(LOG_INFO, fd_cb, "Detaching, ref count = %d", fd_cb->ref_count); in flow_divert_detach()
3144 if (fd_cb->group != NULL) { in flow_divert_detach()
3146 flow_divert_send_buffered_data(fd_cb, TRUE); in flow_divert_detach()
3148 flow_divert_update_closed_state(fd_cb, SHUT_RDWR, FALSE); in flow_divert_detach()
3149 flow_divert_send_close_if_needed(fd_cb); in flow_divert_detach()
3151 flow_divert_pcb_remove(fd_cb); in flow_divert_detach()
3155 FDLOCK(fd_cb); in flow_divert_detach()
3156 fd_cb->so = NULL; in flow_divert_detach()
3157 FDUNLOCK(fd_cb); in flow_divert_detach()
3160 FDRELEASE(fd_cb); /* Release the socket's reference */ in flow_divert_detach()
3166 struct flow_divert_pcb *fd_cb = so->so_fd_pcb; in flow_divert_close() local
3172 FDLOG0(LOG_INFO, fd_cb, "Closing"); in flow_divert_close()
3179 flow_divert_send_buffered_data(fd_cb, TRUE); in flow_divert_close()
3180 flow_divert_update_closed_state(fd_cb, SHUT_RDWR, FALSE); in flow_divert_close()
3181 flow_divert_send_close_if_needed(fd_cb); in flow_divert_close()
3184 flow_divert_pcb_remove(fd_cb); in flow_divert_close()
3203 struct flow_divert_pcb *fd_cb = so->so_fd_pcb; in flow_divert_shutdown() local
3209 FDLOG0(LOG_INFO, fd_cb, "Can't send more"); in flow_divert_shutdown()
3213 flow_divert_update_closed_state(fd_cb, SHUT_WR, FALSE); in flow_divert_shutdown()
3214 flow_divert_send_close_if_needed(fd_cb); in flow_divert_shutdown()
3222 struct flow_divert_pcb *fd_cb = so->so_fd_pcb; in flow_divert_rcvd() local
3230 FDLOG(LOG_DEBUG, fd_cb, "app read bytes, space = %d", space); in flow_divert_rcvd()
3231 if ((fd_cb->flags & FLOW_DIVERT_NOTIFY_ON_RECEIVED) && in flow_divert_rcvd()
3233 flow_divert_send_read_notification(fd_cb) == 0) { in flow_divert_rcvd()
3234 FDLOG0(LOG_INFO, fd_cb, "Sent a read notification"); in flow_divert_rcvd()
3235 fd_cb->flags &= ~FLOW_DIVERT_NOTIFY_ON_RECEIVED; in flow_divert_rcvd()
3371 struct flow_divert_pcb *fd_cb = so->so_fd_pcb; in flow_divert_ctloutput() local
3378 if (sopt->sopt_dir == SOPT_SET && fd_cb->flags & FLOW_DIVERT_CONNECT_STARTED) { in flow_divert_ctloutput()
3379 flow_divert_send_traffic_class_update(fd_cb, so->so_traffic_class); in flow_divert_ctloutput()
3394 struct flow_divert_pcb *fd_cb = so->so_fd_pcb; in flow_divert_connect_out_internal() local
3405 if (fd_cb->group == NULL) { in flow_divert_connect_out_internal()
3423 if (fd_cb->flags & FLOW_DIVERT_CONNECT_STARTED) { in flow_divert_connect_out_internal()
3428 FDLOG0(LOG_INFO, fd_cb, "Connecting"); in flow_divert_connect_out_internal()
3430 if (fd_cb->connect_packet == NULL) { in flow_divert_connect_out_internal()
3435 FDLOG0(LOG_ERR, fd_cb, "No destination address available when creating connect packet"); in flow_divert_connect_out_internal()
3441 FDLOG0(LOG_ERR, fd_cb, "Destination address is not valid when creating connect packet"); in flow_divert_connect_out_internal()
3446 fd_cb->original_remote_endpoint = dup_sockaddr(to, 0); in flow_divert_connect_out_internal()
3447 if (fd_cb->original_remote_endpoint == NULL) { in flow_divert_connect_out_internal()
3448 FDLOG0(LOG_ERR, fd_cb, "Failed to dup the remote endpoint"); in flow_divert_connect_out_internal()
3452 fd_cb->original_vflag = inp->inp_vflag; in flow_divert_connect_out_internal()
3453 fd_cb->original_last_outifp = inp->inp_last_outifp; in flow_divert_connect_out_internal()
3454 fd_cb->original_last_outifp6 = inp->in6p_last_outifp; in flow_divert_connect_out_internal()
3479 fd_cb->local_endpoint.sin6.sin6_len = sizeof(struct sockaddr_in6); in flow_divert_connect_out_internal()
3480 fd_cb->local_endpoint.sin6.sin6_family = AF_INET6; in flow_divert_connect_out_internal()
3481 fd_cb->local_endpoint.sin6.sin6_port = inp->inp_lport; in flow_divert_connect_out_internal()
3482 error = in6_pcbladdr(inp, to, &(fd_cb->local_endpoint.sin6.sin6_addr), &ifp); in flow_divert_connect_out_internal()
3484 FDLOG(LOG_WARNING, fd_cb, "failed to get a local IPv6 address: %d", error); in flow_divert_connect_out_internal()
3485 …if (!(fd_cb->flags & FLOW_DIVERT_FLOW_IS_TRANSPARENT) || IN6_IS_ADDR_UNSPECIFIED(&(satosin6(to)->s… in flow_divert_connect_out_internal()
3496 if (IN6_IS_SCOPE_EMBED(&(fd_cb->local_endpoint.sin6.sin6_addr)) && in flow_divert_connect_out_internal()
3498 fd_cb->local_endpoint.sin6.sin6_addr.s6_addr16[1] != 0) { in flow_divert_connect_out_internal()
3499 …fd_cb->local_endpoint.sin6.sin6_scope_id = ntohs(fd_cb->local_endpoint.sin6.sin6_addr.s6_addr16[1]… in flow_divert_connect_out_internal()
3500 fd_cb->local_endpoint.sin6.sin6_addr.s6_addr16[1] = 0; in flow_divert_connect_out_internal()
3512 fd_cb->local_endpoint.sin.sin_len = sizeof(struct sockaddr_in); in flow_divert_connect_out_internal()
3513 fd_cb->local_endpoint.sin.sin_family = AF_INET; in flow_divert_connect_out_internal()
3514 fd_cb->local_endpoint.sin.sin_port = inp->inp_lport; in flow_divert_connect_out_internal()
3515 error = in_pcbladdr(inp, to, &(fd_cb->local_endpoint.sin.sin_addr), IFSCOPE_NONE, &ifp, 0); in flow_divert_connect_out_internal()
3517 FDLOG(LOG_WARNING, fd_cb, "failed to get a local IPv4 address: %d", error); in flow_divert_connect_out_internal()
3518 …if (!(fd_cb->flags & FLOW_DIVERT_FLOW_IS_TRANSPARENT) || satosin(to)->sin_addr.s_addr == INADDR_AN… in flow_divert_connect_out_internal()
3529 FDLOG(LOG_WARNING, fd_cb, "target address has an unsupported family: %d", to->sa_family); in flow_divert_connect_out_internal()
3532 error = flow_divert_check_no_cellular(fd_cb) || in flow_divert_connect_out_internal()
3533 flow_divert_check_no_expensive(fd_cb) || in flow_divert_connect_out_internal()
3534 flow_divert_check_no_constrained(fd_cb); in flow_divert_connect_out_internal()
3543 fd_cb->flags |= FLOW_DIVERT_SHOULD_SET_LOCAL_ADDR; in flow_divert_connect_out_internal()
3546 error = flow_divert_create_connect_packet(fd_cb, to, so, p, &connect_packet); in flow_divert_connect_out_internal()
3552 flow_divert_set_remote_endpoint(fd_cb, to); in flow_divert_connect_out_internal()
3553 flow_divert_set_local_endpoint(fd_cb, SA(&fd_cb->local_endpoint)); in flow_divert_connect_out_internal()
3557 fd_cb->flags |= FLOW_DIVERT_IMPLICIT_CONNECT; in flow_divert_connect_out_internal()
3561 FDLOG0(LOG_INFO, fd_cb, "Delaying sending the connect packet until send or receive"); in flow_divert_connect_out_internal()
3565 fd_cb->connect_packet = connect_packet; in flow_divert_connect_out_internal()
3568 FDLOG0(LOG_INFO, fd_cb, "Sending saved connect packet"); in flow_divert_connect_out_internal()
3572 error = flow_divert_send_connect_packet(fd_cb); in flow_divert_connect_out_internal()
3577 fd_cb->flags |= FLOW_DIVERT_CONNECT_STARTED; in flow_divert_connect_out_internal()
3580 if (SOCK_TYPE(so) == SOCK_DGRAM && !(fd_cb->flags & FLOW_DIVERT_HAS_TOKEN)) { in flow_divert_connect_out_internal()
3597 struct flow_divert_pcb *fd_cb = so->so_fd_pcb; in flow_divert_connect_out() local
3598 FDLOG(LOG_ERR, fd_cb, "Failed to attach cfil: %d", error); in flow_divert_connect_out()
3621 struct flow_divert_pcb *fd_cb = so->so_fd_pcb; in flow_divert_connectx_out_common() local
3622 if (fd_cb != NULL && (fd_cb->flags & FLOW_DIVERT_HAS_TOKEN) && in flow_divert_connectx_out_common()
3691 struct flow_divert_pcb *fd_cb = so->so_fd_pcb; in flow_divert_data_out() local
3718 if ((fd_cb->flags & FLOW_DIVERT_TUNNEL_WR_CLOSED) && SOCK_TYPE(so) == SOCK_DGRAM) { in flow_divert_data_out()
3720 FDLOG0(LOG_INFO, fd_cb, "provider is no longer accepting writes, cannot send data"); in flow_divert_data_out()
3736 FDLOG(LOG_INFO, fd_cb, "Using remote address from CFIL saved state: %p", to); in flow_divert_data_out()
3741 if (!(fd_cb->flags & FLOW_DIVERT_CONNECT_STARTED)) { in flow_divert_data_out()
3742 FDLOG0(LOG_INFO, fd_cb, "implicit connect"); in flow_divert_data_out()
3749 error = flow_divert_check_no_cellular(fd_cb) || in flow_divert_data_out()
3750 flow_divert_check_no_expensive(fd_cb) || in flow_divert_data_out()
3751 flow_divert_check_no_constrained(fd_cb); in flow_divert_data_out()
3757 FDLOG(LOG_DEBUG, fd_cb, "app wrote %lu bytes", mbuf_pkthdr_len(data)); in flow_divert_data_out()
3759 fd_cb->bytes_written_by_app += mbuf_pkthdr_len(data); in flow_divert_data_out()
3760 error = flow_divert_send_app_data(fd_cb, data, to); in flow_divert_data_out()
3792 struct flow_divert_pcb *fd_cb = so->so_fd_pcb; in flow_divert_preconnect() local
3798 if (!(fd_cb->flags & FLOW_DIVERT_CONNECT_STARTED)) { in flow_divert_preconnect()
3799 FDLOG0(LOG_INFO, fd_cb, "Pre-connect read: sending saved connect packet"); in flow_divert_preconnect()
3805 fd_cb->flags |= FLOW_DIVERT_CONNECT_STARTED; in flow_divert_preconnect()
3836 struct flow_divert_pcb *fd_cb = so->so_fd_pcb; in flow_divert_implicit_data_out() local
3845 if (fd_cb == NULL) { in flow_divert_implicit_data_out()
3847 fd_cb = so->so_fd_pcb; in flow_divert_implicit_data_out()
3848 if (error != 0 || fd_cb == NULL) { in flow_divert_implicit_data_out()
3869 struct flow_divert_pcb *fd_cb = NULL; in flow_divert_pcb_init_internal() local
3878 fd_cb = flow_divert_pcb_create(so); in flow_divert_pcb_init_internal()
3879 if (fd_cb == NULL) { in flow_divert_pcb_init_internal()
3886 FDLOG0(LOG_ERR, fd_cb, "No valid group is available, cannot init flow divert"); in flow_divert_pcb_init_internal()
3891 error = flow_divert_add_to_group(fd_cb, group_unit); in flow_divert_pcb_init_internal()
3893 so->so_fd_pcb = fd_cb; in flow_divert_pcb_init_internal()
3895 fd_cb->control_group_unit = group_unit; in flow_divert_pcb_init_internal()
3896 fd_cb->policy_control_unit = ctl_unit; in flow_divert_pcb_init_internal()
3897 fd_cb->aggregate_unit = agg_unit; in flow_divert_pcb_init_internal()
3899 fd_cb->flags |= FLOW_DIVERT_FLOW_IS_TRANSPARENT; in flow_divert_pcb_init_internal()
3901 fd_cb->flags &= ~FLOW_DIVERT_FLOW_IS_TRANSPARENT; in flow_divert_pcb_init_internal()
3910 FDLOG0(LOG_INFO, fd_cb, "Created"); in flow_divert_pcb_init_internal()
3912 FDLOG(LOG_ERR, fd_cb, "pcb insert failed: %d", error); in flow_divert_pcb_init_internal()
3917 FDRELEASE(fd_cb); in flow_divert_pcb_init_internal()
4020 struct flow_divert_pcb *fd_cb = so->so_fd_pcb; in flow_divert_token_set() local
4025 fd_cb->log_level = (uint8_t)log_level; in flow_divert_token_set()
4029 fd_cb->connect_token = token; in flow_divert_token_set()
4032 fd_cb->flags |= FLOW_DIVERT_HAS_TOKEN; in flow_divert_token_set()
4036 struct flow_divert_pcb *fd_cb = so->so_fd_pcb; in flow_divert_token_set() local
4037 if (fd_cb != NULL) { in flow_divert_token_set()
4038 fd_cb->flags |= FLOW_DIVERT_HAS_HMAC; in flow_divert_token_set()
4056 struct flow_divert_pcb *fd_cb = so->so_fd_pcb; in flow_divert_token_get() local
4065 if (fd_cb->group == NULL) { in flow_divert_token_get()
4072 FDLOG(LOG_ERR, fd_cb, "failed to allocate the header mbuf: %d", error); in flow_divert_token_get()
4076 ctl_unit = htonl(fd_cb->group->ctl_unit); in flow_divert_token_get()
4083 …error = flow_divert_packet_append_tlv(token, FLOW_DIVERT_TLV_FLOW_ID, sizeof(fd_cb->hash), &fd_cb-… in flow_divert_token_get()
4088 if (fd_cb->app_data != NULL) { in flow_divert_token_get()
4089 …_packet_append_tlv(token, FLOW_DIVERT_TLV_APP_DATA, (uint32_t)fd_cb->app_data_length, fd_cb->app_d… in flow_divert_token_get()
4095 control_group = flow_divert_group_lookup(fd_cb->control_group_unit, fd_cb); in flow_divert_token_get()
4283 struct flow_divert_pcb *fd_cb; in flow_divert_kctl_rcvd() local
4304 RB_FOREACH(fd_cb, fd_pcb_tree, &group->pcb_tree) { in flow_divert_kctl_rcvd()
4305 FDRETAIN(fd_cb); in flow_divert_kctl_rcvd()
4306 SLIST_INSERT_HEAD(&tmp_list, fd_cb, tmp_list_entry); in flow_divert_kctl_rcvd()
4311 SLIST_FOREACH(fd_cb, &tmp_list, tmp_list_entry) { in flow_divert_kctl_rcvd()
4312 FDLOCK(fd_cb); in flow_divert_kctl_rcvd()
4313 if (fd_cb->so != NULL) { in flow_divert_kctl_rcvd()
4314 socket_lock(fd_cb->so, 0); in flow_divert_kctl_rcvd()
4315 if (fd_cb->group != NULL) { in flow_divert_kctl_rcvd()
4316 flow_divert_send_buffered_data(fd_cb, FALSE); in flow_divert_kctl_rcvd()
4318 socket_unlock(fd_cb->so, 0); in flow_divert_kctl_rcvd()
4320 FDUNLOCK(fd_cb); in flow_divert_kctl_rcvd()
4321 FDRELEASE(fd_cb); in flow_divert_kctl_rcvd()