/* * Copyright (c) 2021, 2023-2025, Apple Inc. All rights reserved. * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ *//* * LOCKING STRATEGY * * The struct socket's so_flow_db field (struct soflow_db and its hash entries * struct soflow_hash_entry) is protected by the socket lock. This covers all the * socket paths that calls soflow_get_flow() as well as the garbage collection. * For the socket detach path, soflow_detach() cannot assume the socket lock is * held. Thus, reference counts are added to both struct soflow_db and struct * soflow_hash_entry to avoid access after freed issues. * * The global list, soflow_entry_head, keeps track of all struct soflow_hash_entry * entries which is used by garbage collection when detecting idle entries. This list * is protected by the global lock soflow_lck_rw. * */#include<sys/types.h>#include<sys/kern_control.h>#include<sys/queue.h>#include<sys/domain.h>#include<sys/protosw.h>#include<sys/syslog.h>#include<sys/systm.h>#include<sys/sysproto.h>#include<sys/socket.h>#include<sys/socketvar.h>#include<kern/sched_prim.h>#include<kern/locks.h>#include<kern/zalloc.h>#include<kern/debug.h>#include<net/ntstat.h>#include<netinet6/in6_var.h>#define _IP_VHL#include<netinet/ip.h>#include<netinet/in_pcb.h>#include<netinet/udp.h>#include<netinet/udp_var.h>#include<string.h>#include<libkern/libkern.h>#include<kern/socket_flows.h>#include<net/sockaddr_utils.h>externstructinpcbinforipcbinfo;/* * Per-Socket Flow Management */staticintsoflow_log_level=LOG_ERR;staticintsoflow_log_port=0;staticintsoflow_log_pid=0;staticintsoflow_log_proto=0;staticintsoflow_nstat_disable=0;staticintsoflow_disable=0;staticlongsoflow_attached_count=0;staticlongsoflow_attached_high_water_mark=0;staticos_log_tsoflow_log_handle=NULL;/* * Sysctls for debug logs control */SYSCTL_NODE(_net,OID_AUTO,soflow,CTLFLAG_RW|CTLFLAG_LOCKED,0,"soflow");SYSCTL_INT(_net_soflow,OID_AUTO,log_level,CTLFLAG_RW|CTLFLAG_LOCKED,&soflow_log_level,0,"");SYSCTL_INT(_net_soflow,OID_AUTO,log_port,CTLFLAG_RW|CTLFLAG_LOCKED,&soflow_log_port,0,"");SYSCTL_INT(_net_soflow,OID_AUTO,log_pid,CTLFLAG_RW|CTLFLAG_LOCKED,&soflow_log_pid,0,"");SYSCTL_INT(_net_soflow,OID_AUTO,log_proto,CTLFLAG_RW|CTLFLAG_LOCKED,&soflow_log_proto,0,"");SYSCTL_INT(_net_soflow,OID_AUTO,nstat_disable,CTLFLAG_RW|CTLFLAG_LOCKED,&soflow_nstat_disable,0,"");SYSCTL_INT(_net_soflow,OID_AUTO,disable,CTLFLAG_RW|CTLFLAG_LOCKED,&soflow_disable,0,"");SYSCTL_LONG(_net_soflow,OID_AUTO,count,CTLFLAG_LOCKED|CTLFLAG_RD,&soflow_attached_count,"");SYSCTL_LONG(_net_soflow,OID_AUTO,high_water_mark,CTLFLAG_LOCKED|CTLFLAG_RD,&soflow_attached_high_water_mark,"");#define SOFLOW_LOG(level, so, debug, fmt, ...) \do { \ if (soflow_log_level >= level && debug && soflow_log_handle) { \ if (level == LOG_ERR) { \