xref: /xnu-11215/bsd/net/if_var_private.h (revision 8d741a5d)
1 /*
2  * Copyright (c) 2000-2023 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * Copyright (c) 1982, 1986, 1989, 1993
30  *    The Regents of the University of California.  All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
35  * 1. Redistributions of source code must retain the above copyright
36  *    notice, this list of conditions and the following disclaimer.
37  * 2. Redistributions in binary form must reproduce the above copyright
38  *    notice, this list of conditions and the following disclaimer in the
39  *    documentation and/or other materials provided with the distribution.
40  * 3. All advertising materials mentioning features or use of this software
41  *    must display the following acknowledgement:
42  *    This product includes software developed by the University of
43  *    California, Berkeley and its contributors.
44  * 4. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *    From: @(#)if.h    8.1 (Berkeley) 6/10/93
61  * $FreeBSD: src/sys/net/if_var.h,v 1.18.2.7 2001/07/24 19:10:18 brooks Exp $
62  */
63 
64 #ifndef _NET_IF_VAR_PRIVATE_H_
65 #define _NET_IF_VAR_PRIVATE_H_
66 
67 #ifndef DRIVERKIT
68 #ifndef DRIVERKIT_PRIVATE
69 #include <net/if_var_status.h>
70 #endif
71 #include <net/route.h>
72 #include <stdint.h>
73 #include <sys/types.h>
74 #include <sys/ioccom.h>
75 #ifdef KERNEL_PRIVATE
76 #include <kern/locks.h>
77 #endif /* KERNEL_PRIVATE */
78 
79 struct if_traffic_class {
80 	u_int64_t               ifi_ibepackets;/* TC_BE packets received on interface */
81 	u_int64_t               ifi_ibebytes;/* TC_BE bytes received on interface */
82 	u_int64_t               ifi_obepackets;/* TC_BE packet sent on interface */
83 	u_int64_t               ifi_obebytes;/* TC_BE bytes sent on interface */
84 	u_int64_t               ifi_ibkpackets;/* TC_BK packets received on interface */
85 	u_int64_t               ifi_ibkbytes;/* TC_BK bytes received on interface */
86 	u_int64_t               ifi_obkpackets;/* TC_BK packet sent on interface */
87 	u_int64_t               ifi_obkbytes;/* TC_BK bytes sent on interface */
88 	u_int64_t               ifi_ivipackets;/* TC_VI packets received on interface */
89 	u_int64_t               ifi_ivibytes;/* TC_VI bytes received on interface */
90 	u_int64_t               ifi_ovipackets;/* TC_VI packets sent on interface */
91 	u_int64_t               ifi_ovibytes;/* TC_VI bytes sent on interface */
92 	u_int64_t               ifi_ivopackets;/* TC_VO packets received on interface */
93 	u_int64_t               ifi_ivobytes;/* TC_VO bytes received on interface */
94 	u_int64_t               ifi_ovopackets;/* TC_VO packets sent on interface */
95 	u_int64_t               ifi_ovobytes;/* TC_VO bytes sent on interface */
96 	u_int64_t               ifi_ipvpackets;/* TC priv packets received on interface */
97 	u_int64_t               ifi_ipvbytes;/* TC priv bytes received on interface */
98 	u_int64_t               ifi_opvpackets;/* TC priv packets sent on interface */
99 	u_int64_t               ifi_opvbytes;/* TC priv bytes sent on interface */
100 };
101 
102 struct if_data_extended {
103 	u_int64_t       ifi_alignerrs;/* unaligned (32-bit) input pkts */
104 	u_int64_t       ifi_dt_bytes;/* Data threshold counter */
105 	u_int64_t       ifi_fpackets;/* forwarded packets on interface */
106 	u_int64_t       ifi_fbytes; /* forwarded bytes on interface */
107 	u_int64_t       reserved[12];/* for future */
108 };
109 
110 struct if_packet_stats {
111 	/* TCP */
112 	u_int64_t               ifi_tcp_badformat;
113 	u_int64_t               ifi_tcp_unspecv6;
114 	u_int64_t               ifi_tcp_synfin;
115 	u_int64_t               ifi_tcp_badformatipsec;
116 	u_int64_t               ifi_tcp_noconnnolist;
117 	u_int64_t               ifi_tcp_noconnlist;
118 	u_int64_t               ifi_tcp_listbadsyn;
119 	u_int64_t               ifi_tcp_icmp6unreach;
120 	u_int64_t               ifi_tcp_deprecate6;
121 	u_int64_t               ifi_tcp_rstinsynrcv;
122 	u_int64_t               ifi_tcp_ooopacket;
123 	u_int64_t               ifi_tcp_dospacket;
124 	u_int64_t               ifi_tcp_cleanup;
125 	u_int64_t               ifi_tcp_synwindow;
126 	u_int64_t               reserved[6];
127 	/* UDP */
128 	u_int64_t               ifi_udp_port_unreach;
129 	u_int64_t               ifi_udp_faithprefix;
130 	u_int64_t               ifi_udp_port0;
131 	u_int64_t               ifi_udp_badlength;
132 	u_int64_t               ifi_udp_badchksum;
133 	u_int64_t               ifi_udp_badmcast;
134 	u_int64_t               ifi_udp_cleanup;
135 	u_int64_t               ifi_udp_badipsec;
136 	u_int64_t               _reserved[4];
137 };
138 
139 struct if_description {
140 	u_int32_t       ifd_maxlen; /* must be IF_DESCSIZE */
141 	u_int32_t       ifd_len;    /* actual ifd_desc length */
142 	u_int8_t        *__sized_by(ifd_maxlen) ifd_desc;  /* ptr to desc buffer */
143 };
144 
145 struct if_bandwidths {
146 	uint64_t       eff_bw;     /* effective bandwidth */
147 	uint64_t       max_bw;     /* maximum theoretical bandwidth */
148 };
149 
150 struct if_latencies {
151 	u_int64_t       eff_lt;     /* effective latency */
152 	u_int64_t       max_lt;     /* maximum theoretical latency */
153 };
154 
155 typedef enum {
156 	IF_NETEM_MODEL_NULL = 0,
157 	IF_NETEM_MODEL_NLC = 1,
158 } if_netem_model_t;
159 
160 #define IF_NETEM_PARAMS_PSCALE  100000
161 struct if_netem_params {
162 	/* packet scheduler model */
163 	if_netem_model_t        ifnetem_model;
164 
165 	/* bandwidth limit */
166 	uint64_t        ifnetem_bandwidth_bps;
167 
168 	/* latency (normal distribution with jitter as stdev) */
169 	uint32_t        ifnetem_latency_ms;
170 	uint32_t        ifnetem_jitter_ms;
171 
172 	/*
173 	 * NetEm probabilistic model parameters has a scaling factor of 100,000
174 	 * for 5 digits precision. For instance, probability 12.345% is
175 	 * expressed as uint32_t fixed point 12345 in ifnet_*_p variable below.
176 	 */
177 	/* random packet corruption */
178 	uint32_t        ifnetem_corruption_p;
179 
180 	/* random packet duplication */
181 	uint32_t        ifnetem_duplication_p;
182 
183 	/* 4 state Markov loss model */
184 	uint32_t        ifnetem_loss_p_gr_gl;/* P( gap_loss   | gap_rx     ) */
185 	uint32_t        ifnetem_loss_p_gr_bl;/* P( burst_loss | gap_rx     ) */
186 	uint32_t        ifnetem_loss_p_bl_br;/* P( burst_rx   | burst_loss ) */
187 	uint32_t        ifnetem_loss_p_bl_gr;/* P( gap_rx     | burst_loss ) */
188 	uint32_t        ifnetem_loss_p_br_bl;/* P( burst_loss | burst_rx   ) */
189 
190 	uint32_t        ifnetem_loss_recovery_ms;/* time to recovery loss */
191 
192 	/* random packet reordering */
193 	uint32_t        ifnetem_reordering_p;/* reorder probability */
194 
195 	/*
196 	 * NetEm output scheduler by default is waken up upon input event as
197 	 * well as timer interval to avoid excessive delay. If
198 	 * ifnetem_output_ival is set to non-zero value, it overrides the
199 	 * default output interval as well as disables output scheduler wakeup
200 	 * upon input events.
201 	 */
202 	uint32_t        ifnetem_output_ival_ms;/* output interval */
203 };
204 
205 struct if_rxpoll_stats {
206 	u_int32_t       ifi_poll_off_req;   /* total # of POLL_OFF reqs */
207 	u_int32_t       ifi_poll_off_err;   /* total # of POLL_OFF errors */
208 	u_int32_t       ifi_poll_on_req;    /* total # of POLL_ON reqs */
209 	u_int32_t       ifi_poll_on_err;    /* total # of POLL_ON errors */
210 
211 	u_int32_t       ifi_poll_wakeups_avg;/* avg # of wakeup reqs */
212 	u_int32_t       ifi_poll_wakeups_lowat;/* wakeups low watermark */
213 	u_int32_t       ifi_poll_wakeups_hiwat;/* wakeups high watermark */
214 
215 	u_int64_t       ifi_poll_packets;   /* total # of polled packets */
216 	u_int32_t       ifi_poll_packets_avg;/* average polled packets */
217 	u_int32_t       ifi_poll_packets_min;/* smallest polled packets */
218 	u_int32_t       ifi_poll_packets_max;/* largest polled packets */
219 	u_int32_t       ifi_poll_packets_lowat;/* packets low watermark */
220 	u_int32_t       ifi_poll_packets_hiwat;/* packets high watermark */
221 
222 	u_int64_t       ifi_poll_bytes;     /* total # of polled bytes */
223 	u_int32_t       ifi_poll_bytes_avg; /* average polled bytes */
224 	u_int32_t       ifi_poll_bytes_min; /* smallest polled bytes */
225 	u_int32_t       ifi_poll_bytes_max; /* largest polled bytes */
226 	u_int32_t       ifi_poll_bytes_lowat;/* bytes low watermark */
227 	u_int32_t       ifi_poll_bytes_hiwat;/* bytes high watermark */
228 
229 	u_int32_t       ifi_poll_packets_limit;/* max packets per poll call */
230 	u_int64_t       ifi_poll_interval_time;/* poll interval (nsec) */
231 };
232 
233 struct if_netif_stats {
234 	u_int64_t       ifn_rx_mit_interval;/* rx mitigation ival (nsec) */
235 	u_int32_t       ifn_rx_mit_mode;    /* 0: static, 1: dynamic */
236 	u_int32_t       ifn_rx_mit_packets_avg;/* average # of packets */
237 	u_int32_t       ifn_rx_mit_packets_min;/* smallest # of packets */
238 	u_int32_t       ifn_rx_mit_packets_max;/* largest # of packets */
239 	u_int32_t       ifn_rx_mit_bytes_avg;/* average # of bytes */
240 	u_int32_t       ifn_rx_mit_bytes_min;/* smallest # of bytes */
241 	u_int32_t       ifn_rx_mit_bytes_max;/* largest # of bytes */
242 	u_int32_t       ifn_rx_mit_cfg_idx; /* current config selector */
243 	u_int32_t       ifn_rx_mit_cfg_packets_lowat;/* pkts low watermark */
244 	u_int32_t       ifn_rx_mit_cfg_packets_hiwat;/* pkts high watermark */
245 	u_int32_t       ifn_rx_mit_cfg_bytes_lowat;/* bytes low watermark */
246 	u_int32_t       ifn_rx_mit_cfg_bytes_hiwat;/* bytes high watermark */
247 	u_int32_t       ifn_rx_mit_cfg_interval;/* delay interval (nsec) */
248 };
249 
250 struct if_tcp_ecn_perf_stat {
251 	u_int64_t total_txpkts;
252 	u_int64_t total_rxmitpkts;
253 	u_int64_t total_rxpkts;
254 	u_int64_t total_oopkts;
255 	u_int64_t total_reorderpkts;
256 	u_int64_t rtt_avg;
257 	u_int64_t rtt_var;
258 	u_int64_t sack_episodes;
259 	u_int64_t rxmit_drop;
260 	u_int64_t rst_drop;
261 	u_int64_t oo_percent;
262 	u_int64_t reorder_percent;
263 	u_int64_t rxmit_percent;
264 };
265 
266 struct if_tcp_ecn_stat {
267 	u_int64_t timestamp;
268 	u_int64_t ecn_client_setup;
269 	u_int64_t ecn_server_setup;
270 	u_int64_t ecn_client_success;
271 	u_int64_t ecn_server_success;
272 	u_int64_t ecn_peer_nosupport;
273 	u_int64_t ecn_syn_lost;
274 	u_int64_t ecn_synack_lost;
275 	u_int64_t ecn_recv_ce;
276 	u_int64_t ecn_recv_ece;
277 	u_int64_t ecn_conn_recv_ce;
278 	u_int64_t ecn_conn_recv_ece;
279 	u_int64_t ecn_conn_plnoce;
280 	u_int64_t ecn_conn_plce;
281 	u_int64_t ecn_conn_noplce;
282 	u_int64_t ecn_fallback_synloss;
283 	u_int64_t ecn_fallback_reorder;
284 	u_int64_t ecn_fallback_ce;
285 	u_int64_t ecn_off_conn;
286 	u_int64_t ecn_total_conn;
287 	u_int64_t ecn_fallback_droprst;
288 	u_int64_t ecn_fallback_droprxmt;
289 	u_int64_t ecn_fallback_synrst;
290 	struct if_tcp_ecn_perf_stat ecn_on;
291 	struct if_tcp_ecn_perf_stat ecn_off;
292 };
293 
294 struct if_lim_perf_stat {
295 	u_int64_t lim_dl_max_bandwidth; /* bits per second */
296 	u_int64_t lim_ul_max_bandwidth; /* bits per second */
297 	u_int64_t lim_total_txpkts; /* Total transmit packets, count */
298 	u_int64_t lim_total_rxpkts; /* Total receive packets, count */
299 	u_int64_t lim_total_retxpkts; /* Total retransmit packets */
300 	u_int64_t lim_packet_loss_percent; /* Packet loss rate */
301 	u_int64_t lim_total_oopkts; /* Total out-of-order packets */
302 	u_int64_t lim_packet_ooo_percent; /* Out-of-order packet rate */
303 	u_int64_t lim_rtt_variance; /* RTT variance, milliseconds */
304 	u_int64_t lim_rtt_average;  /* RTT average, milliseconds */
305 	u_int64_t lim_rtt_min;      /* RTT minimum, milliseconds */
306 	u_int64_t lim_conn_timeouts; /* connection timeouts */
307 	u_int64_t lim_conn_attempts; /* connection attempts */
308 	u_int64_t lim_conn_timeout_percent; /* Rate of connection timeouts */
309 	u_int64_t lim_bk_txpkts;    /* Transmit packets with BK service class, that use delay based algorithms */
310 	u_int64_t lim_dl_detected:1, /* Low internet */
311 	    lim_ul_detected:1;
312 };
313 
314 #define IF_VAR_H_HAS_IFNET_STATS_PER_FLOW 1
315 struct ifnet_stats_per_flow {
316 	u_int64_t bk_txpackets;
317 	u_int64_t txpackets;
318 	u_int64_t rxpackets;
319 	u_int32_t txretransmitbytes;
320 	u_int32_t rxoutoforderbytes;
321 	u_int32_t rxmitpkts;
322 	u_int32_t rcvoopack;
323 	u_int32_t pawsdrop;
324 	u_int32_t sack_recovery_episodes;
325 	u_int32_t reordered_pkts;
326 	u_int32_t dsack_sent;
327 	u_int32_t dsack_recvd;
328 	u_int32_t srtt;
329 	u_int32_t rttupdated;
330 	u_int32_t rttvar;
331 	u_int32_t rttmin;
332 	u_int32_t bw_sndbw_max;
333 	u_int32_t bw_rcvbw_max;
334 	u_int32_t ecn_recv_ece;
335 	u_int32_t ecn_recv_ce;
336 	u_int32_t ecn_flags;
337 	u_int16_t ipv4:1,
338 	    local:1,
339 	    connreset:1,
340 	    conntimeout:1,
341 	    rxmit_drop:1,
342 	    ecn_fallback_synloss:1,
343 	    ecn_fallback_droprst:1,
344 	    ecn_fallback_droprxmt:1,
345 	    ecn_fallback_ce:1,
346 	    ecn_fallback_reorder:1;
347 };
348 
349 struct if_interface_state {
350 	/*
351 	 * The bitmask tells which of the fields
352 	 * to consider:
353 	 * - When setting, to control which fields
354 	 *   are being modified;
355 	 * - When getting, it tells which fields are set.
356 	 */
357 	u_int8_t valid_bitmask;
358 #define IF_INTERFACE_STATE_RRC_STATE_VALID              0x1
359 #define IF_INTERFACE_STATE_LQM_STATE_VALID              0x2
360 #define IF_INTERFACE_STATE_INTERFACE_AVAILABILITY_VALID 0x4
361 
362 	/*
363 	 * Valid only for cellular interface
364 	 */
365 	u_int8_t rrc_state;
366 #define IF_INTERFACE_STATE_RRC_STATE_IDLE       0x0
367 #define IF_INTERFACE_STATE_RRC_STATE_CONNECTED  0x1
368 
369 	/*
370 	 * Values normalized to the edge of the following values
371 	 * that are defined on <net/if.h>:
372 	 *  IFNET_LQM_THRESH_BAD
373 	 *  IFNET_LQM_THRESH_POOR
374 	 *  IFNET_LQM_THRESH_GOOD
375 	 */
376 	int8_t lqm_state;
377 
378 	/*
379 	 * Indicate if the underlying link is currently
380 	 * available
381 	 */
382 	u_int8_t interface_availability;
383 #define IF_INTERFACE_STATE_INTERFACE_AVAILABLE          0x0
384 #define IF_INTERFACE_STATE_INTERFACE_UNAVAILABLE        0x1
385 };
386 
387 struct chain_len_stats {
388 	uint64_t        cls_one;
389 	uint64_t        cls_two;
390 	uint64_t        cls_three;
391 	uint64_t        cls_four;
392 	uint64_t        cls_five_or_more;
393 } __attribute__((__aligned__(sizeof(uint64_t))));
394 
395 #ifdef BSD_KERNEL_PRIVATE
396 #define IFNETS_MAX      64
397 
398 /*
399  * Internal storage of if_data. This is bound to change. Various places in the
400  * stack will translate this data structure in to the externally visible
401  * if_data structure above.  Note that during interface attach time, the
402  * embedded if_data structure in ifnet is cleared, with the exception of
403  * some non-statistics related fields.
404  */
405 struct if_data_internal {
406 	/* generic interface information */
407 	u_char          ifi_type;       /* ethernet, tokenring, etc */
408 	u_char          ifi_typelen;    /* Length of frame type id */
409 	u_char          ifi_physical;   /* e.g., AUI, Thinnet, 10base-T, etc */
410 	u_char          ifi_addrlen;    /* media address length */
411 	u_char          ifi_hdrlen;     /* media header length */
412 	u_char          ifi_recvquota;  /* polling quota for receive intrs */
413 	u_char          ifi_xmitquota;  /* polling quota for xmit intrs */
414 	u_char          ifi_unused1;    /* for future use */
415 	u_int32_t       ifi_mtu;        /* maximum transmission unit */
416 	u_int32_t       ifi_metric;     /* routing metric (external only) */
417 	u_int32_t       ifi_baudrate;   /* linespeed */
418 	/* volatile statistics */
419 	u_int64_t       ifi_ipackets;   /* packets received on interface */
420 	u_int64_t       ifi_ierrors;    /* input errors on interface */
421 	u_int64_t       ifi_opackets;   /* packets sent on interface */
422 	u_int64_t       ifi_oerrors;    /* output errors on interface */
423 	u_int64_t       ifi_collisions; /* collisions on csma interfaces */
424 	u_int64_t       ifi_ibytes;     /* total number of octets received */
425 	u_int64_t       ifi_obytes;     /* total number of octets sent */
426 	u_int64_t       ifi_imcasts;    /* packets received via multicast */
427 	u_int64_t       ifi_omcasts;    /* packets sent via multicast */
428 	u_int64_t       ifi_iqdrops;    /* dropped on input, this interface */
429 	u_int64_t       ifi_noproto;    /* destined for unsupported protocol */
430 	u_int32_t       ifi_recvtiming; /* usec spent receiving when timing */
431 	u_int32_t       ifi_xmittiming; /* usec spent xmitting when timing */
432 	u_int64_t       ifi_alignerrs;  /* unaligned (32-bit) input pkts */
433 	u_int64_t       ifi_dt_bytes;   /* Data threshold counter */
434 	u_int64_t       ifi_fpackets;   /* forwarded packets on interface */
435 	u_int64_t       ifi_fbytes;     /* forwarded bytes on interface */
436 	struct  timeval ifi_lastchange; /* time of last administrative change */
437 	struct  timeval ifi_lastupdown; /* time of last up/down event */
438 	u_int32_t       ifi_hwassist;   /* HW offload capabilities */
439 	u_int32_t       ifi_tso_v4_mtu; /* TCP Segment Offload IPv4 maximum segment size */
440 	u_int32_t       ifi_tso_v6_mtu; /* TCP Segment Offload IPv6 maximum segment size */
441 };
442 #endif /* BSD_KERNEL_PRIVATE */
443 
444 #define if_mtu          if_data.ifi_mtu
445 #define if_type         if_data.ifi_type
446 #define if_typelen      if_data.ifi_typelen
447 #define if_physical     if_data.ifi_physical
448 #define if_addrlen      if_data.ifi_addrlen
449 #define if_hdrlen       if_data.ifi_hdrlen
450 #define if_metric       if_data.ifi_metric
451 #define if_baudrate     if_data.ifi_baudrate
452 #define if_hwassist     if_data.ifi_hwassist
453 #define if_ipackets     if_data.ifi_ipackets
454 #define if_ierrors      if_data.ifi_ierrors
455 #define if_opackets     if_data.ifi_opackets
456 #define if_oerrors      if_data.ifi_oerrors
457 #define if_collisions   if_data.ifi_collisions
458 #define if_ibytes       if_data.ifi_ibytes
459 #define if_obytes       if_data.ifi_obytes
460 #define if_imcasts      if_data.ifi_imcasts
461 #define if_omcasts      if_data.ifi_omcasts
462 #define if_iqdrops      if_data.ifi_iqdrops
463 #define if_noproto      if_data.ifi_noproto
464 #define if_lastchange   if_data.ifi_lastchange
465 #define if_recvquota    if_data.ifi_recvquota
466 #define if_xmitquota    if_data.ifi_xmitquota
467 #ifdef BSD_KERNEL_PRIVATE
468 #define if_tso_v4_mtu   if_data.ifi_tso_v4_mtu
469 #define if_tso_v6_mtu   if_data.ifi_tso_v6_mtu
470 #define if_alignerrs    if_data.ifi_alignerrs
471 #define if_dt_bytes     if_data.ifi_dt_bytes
472 #define if_fpackets     if_data.ifi_fpackets
473 #define if_fbytes       if_data.ifi_fbytes
474 #define if_lastupdown   if_data.ifi_lastupdown
475 
476 /*
477  * Forward structure declarations for function prototypes [sic].
478  */
479 struct proc;
480 struct rtentry;
481 struct socket;
482 struct ifnet_filter;
483 struct mbuf;
484 struct ifaddr;
485 struct tqdummy;
486 struct proto_hash_entry;
487 struct dlil_threading_info;
488 struct tcpstat_local;
489 struct udpstat_local;
490 #if PF
491 struct pfi_kif;
492 #endif /* PF */
493 #if SKYWALK
494 struct nexus_netif_adapter;
495 #endif /* SKYWALK */
496 
497 /* we use TAILQs so that the order of instantiation is preserved in the list */
498 TAILQ_HEAD(ifnethead, ifnet);
499 TAILQ_HEAD(ifaddrhead, ifaddr);
500 LIST_HEAD(ifmultihead, ifmultiaddr);
501 TAILQ_HEAD(tailq_head, tqdummy);
502 TAILQ_HEAD(ifnet_filter_head, ifnet_filter);
503 TAILQ_HEAD(ddesc_head_name, dlil_demux_desc);
504 
505 extern bool intcoproc_unrestricted;
506 extern bool management_data_unrestricted;
507 extern bool management_control_unrestricted;
508 extern bool if_management_interface_check_needed;
509 extern int if_management_verbose;
510 extern bool if_ultra_constrained_check_needed;
511 #endif /* BSD_KERNEL_PRIVATE */
512 
513 /*
514  * All of the following IF_HWASSIST_* flags are defined in kpi_interface.h as
515  * IFNET_* flags. These are redefined here as constants to avoid failures to
516  * build user level programs that can not include kpi_interface.h. It is
517  * important to keep this in sync with the definitions in kpi_interface.h.
518  * The corresponding constant for each definition is mentioned in the comment.
519  *
520  * Bottom 16 bits reserved for hardware checksum
521  */
522 #define IF_HWASSIST_CSUM_IP             0x0001  /* will csum IP, IFNET_CSUM_IP */
523 #define IF_HWASSIST_CSUM_TCP            0x0002  /* will csum TCP, IFNET_CSUM_TCP */
524 #define IF_HWASSIST_CSUM_UDP            0x0004  /* will csum UDP, IFNET_CSUM_UDP */
525 #define IF_HWASSIST_CSUM_IP_FRAGS       0x0008  /* will csum IP fragments, IFNET_CSUM_FRAGMENT */
526 #define IF_HWASSIST_CSUM_FRAGMENT       0x0010  /* will do IP fragmentation, IFNET_IP_FRAGMENT */
527 #define IF_HWASSIST_CSUM_TCPIPV6        0x0020  /* will csum TCPv6, IFNET_CSUM_TCPIPV6 */
528 #define IF_HWASSIST_CSUM_UDPIPV6        0x0040  /* will csum UDPv6, IFNET_CSUM_UDP */
529 #define IF_HWASSIST_CSUM_FRAGMENT_IPV6  0x0080  /* will do IPv6 fragmentation, IFNET_IPV6_FRAGMENT */
530 #define IF_HWASSIST_CSUM_PARTIAL        0x1000  /* simple Sum16 computation, IFNET_CSUM_PARTIAL */
531 #define IF_HWASSIST_CSUM_ZERO_INVERT    0x2000  /* capable of inverting csum of 0 to -0 (0xffff) */
532 #define IF_HWASSIST_CSUM_MASK           0xffff
533 #define IF_HWASSIST_CSUM_FLAGS(hwassist)        ((hwassist) & IF_HWASSIST_CSUM_MASK)
534 
535 /* VLAN support */
536 #define IF_HWASSIST_VLAN_TAGGING        0x00010000      /* supports VLAN tagging, IFNET_VLAN_TAGGING */
537 #define IF_HWASSIST_VLAN_MTU            0x00020000      /* supports VLAN MTU-sized packet (for software VLAN), IFNET_VLAN_MTU */
538 
539 /* TCP Segment Offloading support */
540 
541 #define IF_HWASSIST_TSO_V4              0x00200000      /* will do TCP Segment offload for IPv4, IFNET_TSO_IPV4 */
542 #define IF_HWASSIST_TSO_V6              0x00400000      /* will do TCP Segment offload for IPv6, IFNET_TSO_IPV6 */
543 
544 #define IFXNAMSIZ       (IFNAMSIZ + 8)  /* external name (name + unit) */
545 #define IFNET_NETWORK_ID_LEN 32
546 
547 #ifdef BSD_KERNEL_PRIVATE
548 /*
549  * ifnet is private to BSD portion of kernel
550  */
551 #include <sys/mcache.h>
552 #include <sys/tree.h>
553 #include <netinet/in.h>
554 #include <net/if_dl.h>
555 #include <net/classq/if_classq.h>
556 #include <net/if_types.h>
557 #include <net/route.h>
558 
559 RB_HEAD(ll_reach_tree, if_llreach);     /* define struct ll_reach_tree */
560 
561 #if SKYWALK
562 struct nexus_ifnet_ops {
563 	void (*ni_finalize)(struct nexus_netif_adapter *, struct ifnet *);
564 	void (*ni_reap)(struct nexus_netif_adapter *, struct ifnet *,
565 	    uint32_t, boolean_t);
566 	errno_t (*ni_dequeue)(struct nexus_netif_adapter *, uint32_t,
567 	    uint32_t, uint32_t, classq_pkt_t *, classq_pkt_t *, uint32_t *,
568 	    uint32_t *, boolean_t, errno_t);
569 	errno_t (*ni_get_len)(struct nexus_netif_adapter *, uint32_t,
570 	    uint32_t *, uint32_t *, errno_t);
571 };
572 typedef struct {
573 	uuid_t          if_nif_provider;
574 	uuid_t          if_nif_instance;
575 	uuid_t          if_nif_attach;
576 } if_nexus_netif, *if_nexus_netif_t;
577 
578 typedef struct {
579 	uuid_t          if_fsw_provider;
580 	uuid_t          if_fsw_instance;
581 	uuid_t          if_fsw_device;
582 	uint32_t        if_fsw_ipaddr_gencnt;
583 } if_nexus_flowswitch, *if_nexus_flowswitch_t;
584 
585 typedef void (*ifnet_fsw_rx_cb_t)(void *, struct pktq *);
586 typedef void (*ifnet_detach_notify_cb_t)(void *);
587 #endif /* SKYWALK */
588 
589 typedef errno_t (*dlil_input_func)(ifnet_t ifp, mbuf_t m_head,
590     mbuf_t m_tail, const struct ifnet_stat_increment_param *s,
591     boolean_t poll, struct thread *tp);
592 typedef errno_t (*dlil_output_func)(ifnet_t interface, mbuf_t data);
593 
594 typedef u_int8_t        ipv6_router_mode_t;
595 
596 #define if_name(ifp)    ifp->if_xname
597 /*
598  * Structure defining a network interface.
599  *
600  * (Would like to call this struct ``if'', but C isn't PL/1.)
601  */
602 struct ifnet {
603 	/*
604 	 * Lock (RW or mutex) to protect this data structure (static storage.)
605 	 */
606 	decl_lck_rw_data(, if_lock);
607 	void            *if_softc;      /* pointer to driver state */
608 	const char      *if_name;       /* name, e.g. ``en'' or ``lo'' */
609 	const char      *if_xname;      /* external name (name + unit) */
610 	struct if_description if_desc;  /* extended description */
611 	TAILQ_ENTRY(ifnet) if_link;     /* all struct ifnets are chained */
612 	TAILQ_ENTRY(ifnet) if_detaching_link; /* list of detaching ifnets */
613 	TAILQ_ENTRY(ifnet) if_ordered_link;     /* list of ordered ifnets */
614 
615 	decl_lck_mtx_data(, if_ref_lock);
616 	u_int32_t       if_refflags;    /* see IFRF flags below */
617 	u_int32_t       if_refio;       /* number of io ops to the underlying driver */
618 	u_int32_t       if_threads_pending;    /* Threads created but waiting for first run */
619 	u_int32_t       if_datamov;     /* number of threads moving data */
620 	u_int32_t       if_drainers;    /* number of draining threads */
621 	u_int32_t       if_suspend;     /* number of suspend requests */
622 
623 #define if_list         if_link
624 	struct ifaddrhead if_addrhead;  /* linked list of addresses per if */
625 #define if_addrlist     if_addrhead
626 	struct ifaddr   *if_lladdr;     /* link address (first/permanent) */
627 
628 	u_int32_t       if_qosmarking_mode;     /* generation to use with NECP clients */
629 
630 	int             if_pcount;      /* number of promiscuous listeners */
631 	struct bpf_if   *if_bpf;        /* packet filter structure */
632 	u_short         if_index;       /* numeric abbreviation for this if  */
633 	short           if_unit;        /* sub-unit for lower level driver */
634 	short           if_timer;       /* time 'til if_watchdog called */
635 	short           if_flags;       /* up/down, broadcast, etc. */
636 	u_int32_t       if_eflags;      /* see <net/if.h> */
637 	u_int32_t       if_xflags;      /* see <net/if.h> */
638 
639 	int             if_capabilities;        /* interface features & capabilities */
640 	int             if_capenable;           /* enabled features & capabilities */
641 
642 	void *__sized_by(if_linkmiblen) if_linkmib; /* link-type-specific MIB data */
643 	uint32_t         if_linkmiblen;  /* length of above data */
644 
645 	struct if_data_internal if_data __attribute__((aligned(8)));
646 
647 	ifnet_family_t          if_family;      /* value assigned by Apple */
648 	ifnet_subfamily_t       if_subfamily;   /* value assigned by Apple */
649 	u_int32_t               peer_egress_functional_type; /* value assigned by Apple */
650 
651 	uintptr_t               if_family_cookie;
652 	volatile dlil_input_func if_input_dlil;
653 	volatile dlil_output_func if_output_dlil;
654 	volatile ifnet_start_func if_start;
655 	ifnet_output_func       if_output;
656 	ifnet_pre_enqueue_func  if_pre_enqueue;
657 	ifnet_ctl_func          if_output_ctl;
658 	ifnet_input_poll_func   if_input_poll;
659 	ifnet_ctl_func          if_input_ctl;
660 	ifnet_ioctl_func        if_ioctl;
661 	ifnet_set_bpf_tap       if_set_bpf_tap;
662 	ifnet_detached_func     if_free;
663 	ifnet_demux_func        if_demux;
664 	ifnet_event_func        if_event;
665 	ifnet_framer_func       if_framer_legacy;
666 	ifnet_framer_extended_func if_framer;
667 	ifnet_add_proto_func    if_add_proto;
668 	ifnet_del_proto_func    if_del_proto;
669 	ifnet_check_multi       if_check_multi;
670 	struct proto_hash_entry *if_proto_hash;
671 	ifnet_detached_func     if_detach;
672 
673 	u_int32_t               if_flowhash;    /* interface flow control ID */
674 
675 	decl_lck_mtx_data(, if_start_lock);
676 	u_int32_t               if_start_flags; /* see IFSF flags below */
677 	u_int32_t               if_start_req;
678 	u_int8_t                if_start_embryonic;
679 	u_int8_t                if_start_active; /* output is active */
680 	u_int16_t               if_start_delayed;
681 	u_int16_t               if_start_delay_qlen;
682 	u_int16_t               if_start_delay_idle;
683 	u_int64_t               if_start_delay_swin;
684 	u_int32_t               if_start_delay_cnt;
685 	u_int32_t               if_start_delay_timeout; /* nanoseconds */
686 	struct timespec         if_start_cycle;  /* restart interval */
687 	struct thread           *if_start_thread;
688 
689 	struct ifclassq         *if_snd;         /* transmit queue */
690 	u_int32_t               if_output_sched_model;  /* tx sched model */
691 
692 	struct if_bandwidths    if_output_bw;
693 	struct if_bandwidths    if_input_bw;
694 
695 	struct if_latencies     if_output_lt;
696 	struct if_latencies     if_input_lt;
697 
698 	decl_lck_mtx_data(, if_flt_lock);
699 	u_int32_t               if_flt_busy;
700 	u_int32_t               if_flt_waiters;
701 	struct ifnet_filter_head if_flt_head;
702 	uint32_t                if_flt_non_os_count;
703 	uint32_t                if_flt_no_tso_count;
704 
705 	struct ifmultihead      if_multiaddrs;  /* multicast addresses */
706 	u_int32_t               if_updatemcasts; /* mcast addrs need updating */
707 	int                     if_amcount;     /* # of all-multicast reqs */
708 	decl_lck_mtx_data(, if_addrconfig_lock); /* for serializing addr config */
709 	struct in_multi         *if_allhostsinm; /* store all-hosts inm for this ifp */
710 
711 	/*
712 	 * Opportunistic polling parameters.
713 	 */
714 	decl_lck_mtx_data(, if_poll_lock);
715 	struct if_poll_params {
716 		u_int16_t       poll_req;
717 		u_int16_t       poll_update; /* link update */
718 		u_int32_t       poll_flags;
719 #define IF_POLLF_READY          0x1     /* poll thread is ready */
720 #define IF_POLLF_RUNNING        0x2     /* poll thread is running/active */
721 #define IF_POLLF_TERMINATING    0x4     /* poll thread is terminating */
722 #define IF_POLLF_EMBRYONIC      0x8000  /* poll thread is being setup */
723 		struct timespec poll_cycle;  /* poll interval */
724 		struct thread   *poll_thread;
725 
726 		ifnet_model_t   poll_mode;   /* current mode */
727 		struct pktcntr  poll_tstats; /* incremental polling statistics */
728 		struct if_rxpoll_stats poll_pstats;  /* polling statistics */
729 		struct pktcntr  poll_sstats; /* packets and bytes per sampling */
730 		struct timespec poll_mode_holdtime; /* mode holdtime in nsec */
731 		struct timespec poll_mode_lasttime; /* last mode change time in nsec */
732 		struct timespec poll_sample_holdtime; /* sampling holdtime in nsec */
733 		struct timespec poll_sample_lasttime; /* last sampling time in nsec */
734 		struct timespec poll_dbg_lasttime; /* last debug message time in nsec */
735 	} rxpoll_params;
736 #define if_poll_req     rxpoll_params.poll_req
737 #define if_poll_update  rxpoll_params.poll_update
738 #define if_poll_flags   rxpoll_params.poll_flags
739 #define if_poll_cycle   rxpoll_params.poll_cycle
740 #define if_poll_thread  rxpoll_params.poll_thread
741 #define if_poll_mode    rxpoll_params.poll_mode
742 #define if_poll_tstats  rxpoll_params.poll_tstats
743 #define if_poll_sstats  rxpoll_params.poll_sstats
744 #define if_poll_pstats  rxpoll_params.poll_pstats
745 
746 #define if_poll_mode_holdtime    rxpoll_params.poll_mode_holdtime
747 #define if_poll_mode_lasttime    rxpoll_params.poll_mode_lasttime
748 #define if_poll_sample_holdtime  rxpoll_params.poll_sample_holdtime
749 #define if_poll_sample_lasttime  rxpoll_params.poll_sample_lasttime
750 #define if_poll_dbg_lasttime     rxpoll_params.poll_dbg_lasttime
751 
752 #define if_rxpoll_offreq   rxpoll_params.poll_pstats.ifi_poll_off_req
753 #define if_rxpoll_offerr   rxpoll_params.poll_pstats.ifi_poll_off_err
754 #define if_rxpoll_onreq    rxpoll_params.poll_pstats.ifi_poll_on_req
755 #define if_rxpoll_onerr    rxpoll_params.poll_pstats.ifi_poll_on_err
756 #define if_rxpoll_wavg     rxpoll_params.poll_pstats.ifi_poll_wakeups_avg
757 #define if_rxpoll_wlowat   rxpoll_params.poll_pstats.ifi_poll_wakeups_lowat
758 #define if_rxpoll_whiwat   rxpoll_params.poll_pstats.ifi_poll_wakeups_hiwat
759 #define if_rxpoll_pavg     rxpoll_params.poll_pstats.ifi_poll_packets_avg
760 #define if_rxpoll_pmin     rxpoll_params.poll_pstats.ifi_poll_packets_min
761 #define if_rxpoll_pmax     rxpoll_params.poll_pstats.ifi_poll_packets_max
762 #define if_rxpoll_plowat   rxpoll_params.poll_pstats.ifi_poll_packets_lowat
763 #define if_rxpoll_phiwat   rxpoll_params.poll_pstats.ifi_poll_packets_hiwat
764 #define if_rxpoll_bavg     rxpoll_params.poll_pstats.ifi_poll_bytes_avg
765 #define if_rxpoll_bmin     rxpoll_params.poll_pstats.ifi_poll_bytes_min
766 #define if_rxpoll_bmax     rxpoll_params.poll_pstats.ifi_poll_bytes_max
767 #define if_rxpoll_blowat   rxpoll_params.poll_pstats.ifi_poll_bytes_lowat
768 #define if_rxpoll_bhiwat   rxpoll_params.poll_pstats.ifi_poll_bytes_hiwat
769 #define if_rxpoll_plim     rxpoll_params.poll_pstats.ifi_poll_packets_limit
770 #define if_rxpoll_ival     rxpoll_params.poll_pstats.ifi_poll_interval_time
771 
772 	struct dlil_threading_info *if_inp;
773 
774 	/* allocated once along with dlil_ifnet and is never freed */
775 	thread_call_t           if_dt_tcall;
776 
777 	struct {
778 		u_int32_t       length;
779 		u_char *__sized_by(length) ptr;
780 	} if_broadcast;
781 
782 #if PF
783 	struct pfi_kif          *if_pf_kif;
784 #endif /* PF */
785 #if SKYWALK
786 	struct nexus_ifnet_ops *if_na_ops;
787 	struct nexus_netif_adapter *if_na;
788 
789 	/* compat netif attachment */
790 	if_nexus_netif          if_nx_netif;
791 
792 	/* flowswitch attachment */
793 	if_nexus_flowswitch     if_nx_flowswitch;
794 
795 	/* headroom space to be reserved in tx packets */
796 	uint16_t                if_tx_headroom;
797 
798 	/* trailer space to be reserved in tx packets */
799 	uint16_t                if_tx_trailer;
800 
801 	/*
802 	 * mitigation interval in microseconds for the rx interrupt mitigation
803 	 * logic while operating in the high throughput mode.
804 	 */
805 	uint32_t                if_rx_mit_ival;
806 
807 	/*
808 	 * Number of threads waiting for the start callback to be finished;
809 	 * access is protected by if_start_lock; also serves as wait channel.
810 	 */
811 	uint32_t                if_start_waiters;
812 
813 	ifnet_start_func        if_save_start;
814 	ifnet_output_func       if_save_output;
815 
816 	/*
817 	 * Used for intercepting packets meant for the host stack.
818 	 */
819 	ifnet_fsw_rx_cb_t       if_fsw_rx_cb;
820 	void                    *if_fsw_rx_cb_arg;
821 	uint32_t                if_fsw_rx_cb_ref;
822 
823 	uint32_t                if_delegate_parent_ref;
824 	struct ifnet            *if_delegate_parent;
825 	decl_lck_mtx_data(, if_delegate_lock);
826 
827 	/*
828 	 * Detach notify callback. Used by llw and redirect interfaces.
829 	 */
830 	ifnet_detach_notify_cb_t if_detach_notify;
831 	void                     *if_detach_notify_arg;
832 #endif /* SKYWALK */
833 
834 	decl_lck_mtx_data(, if_cached_route_lock);
835 	u_int32_t               if_fwd_cacheok;
836 	struct route            if_fwd_route;   /* cached forwarding route */
837 	struct route            if_src_route;   /* cached ipv4 source route */
838 	struct route_in6        if_src_route6;  /* cached ipv6 source route */
839 
840 	decl_lck_rw_data(, if_llreach_lock);
841 	struct ll_reach_tree    if_ll_srcs;     /* source link-layer tree */
842 
843 	void                    *if_bridge;     /* bridge glue */
844 
845 	u_int32_t               if_idle_flags;  /* idle flags */
846 	u_int32_t               if_idle_new_flags; /* temporary idle flags */
847 	u_int32_t               if_idle_new_flags_mask; /* temporary mask */
848 	u_int32_t               if_route_refcnt; /* idle: route ref count */
849 	u_int32_t               if_rt_sendts;   /* last of a real time packet */
850 
851 	struct if_traffic_class if_tc __attribute__((aligned(8)));
852 #if INET
853 	struct igmp_ifinfo      *if_igi;        /* for IGMPv3 */
854 #endif /* INET */
855 	struct mld_ifinfo       *if_mli;        /* for MLDv2 */
856 
857 	struct tcpstat_local    *if_tcp_stat;   /* TCP specific stats */
858 	struct udpstat_local    *if_udp_stat;   /* UDP specific stats */
859 
860 	struct {
861 		int32_t         level;          /* cached logging level */
862 		u_int32_t       flags;          /* cached logging flags */
863 		int32_t         category;       /* cached category */
864 		int32_t         subcategory;    /* cached subcategory */
865 	} if_log;
866 
867 	struct {
868 		struct ifnet    *ifp;           /* delegated ifp */
869 		u_int32_t       type;           /* delegated i/f type */
870 		u_int32_t       family;         /* delegated i/f family */
871 		u_int32_t       subfamily;      /* delegated i/f sub-family */
872 		uint32_t        expensive:1,    /* delegated i/f expensive? */
873 		    constrained:1,              /* delegated i/f constrained? */
874 		    ultra_constrained:1;      /* delegated i/f ultra constrained? */
875 	} if_delegated;
876 
877 	uuid_t                  *if_agentids __counted_by(if_agentcount);   /* network agents attached to interface */
878 	u_int32_t               if_agentcount;
879 
880 	volatile uint32_t       if_low_power_gencnt;
881 
882 	u_int32_t               if_generation;  /* generation to use with NECP clients */
883 	u_int32_t               if_fg_sendts;   /* last send on a fg socket in seconds */
884 
885 	u_int64_t               if_data_threshold;
886 
887 	/* Total bytes in send socket buffer */
888 	int64_t                 if_sndbyte_total __attribute__ ((aligned(8)));
889 	/* Total unsent bytes in send socket buffer */
890 	int64_t                 if_sndbyte_unsent __attribute__ ((aligned(8)));
891 	/* count of times, when there was data to send when sleep is impending */
892 	uint32_t                if_unsent_data_cnt;
893 
894 	boolean_t   if_inet6_ioctl_busy;
895 	decl_lck_mtx_data(, if_inet6_ioctl_lock);
896 
897 #if INET
898 	decl_lck_rw_data(, if_inetdata_lock);
899 	struct in_ifextra       *if_inetdata;
900 #endif /* INET */
901 	decl_lck_rw_data(, if_inet6data_lock);
902 	struct in6_ifextra      *if_inet6data;
903 	decl_lck_rw_data(, if_link_status_lock);
904 	struct if_link_status   *if_link_status;
905 	struct if_interface_state       if_interface_state;
906 	struct if_tcp_ecn_stat *if_ipv4_stat;
907 	struct if_tcp_ecn_stat *if_ipv6_stat;
908 
909 #if SKYWALK
910 	/* Keeps track of local ports bound to this interface
911 	 * Protected by the global lock in skywalk/netns/netns.c */
912 	LIST_HEAD(, ns_token) if_netns_tokens;
913 #endif /* SKYWALK */
914 	struct if_lim_perf_stat if_lim_stat;
915 
916 	uint32_t        if_tcp_kao_max;
917 	uint32_t        if_tcp_kao_cnt;
918 
919 #if SKYWALK
920 	struct netem    *if_input_netem;
921 #endif /* SKYWALK */
922 	struct netem    *if_output_netem;
923 
924 	ipv6_router_mode_t if_ipv6_router_mode; /* see <netinet6/in6_var.h> */
925 
926 	u_int8_t        if_estimated_up_bucket;
927 	u_int8_t        if_estimated_down_bucket;
928 	u_int8_t        if_radio_type;
929 	u_int8_t        if_radio_channel;
930 
931 	uint8_t         network_id[IFNET_NETWORK_ID_LEN];
932 	uint8_t         network_id_len;
933 
934 	atomic_bool     if_mcast_add_signaled;
935 	atomic_bool     if_mcast_del_signaled;
936 
937 	uint32_t        if_traffic_rule_count;
938 	uint32_t        if_traffic_rule_genid;
939 
940 	/*
941 	 * if_creation_generation_id is assigned the value of a global counter that
942 	 * is incremented when the interface is allocated and when it is freed.
943 	 *
944 	 * This allows to discriminate between different instances of an interface
945 	 * that has the same name or same index
946 	 */
947 	uint64_t        if_creation_generation_id;
948 };
949 
950 /* Interface event handling declarations */
951 extern struct eventhandler_lists_ctxt ifnet_evhdlr_ctxt;
952 
953 typedef enum {
954 	INTF_EVENT_CODE_CREATED,
955 	INTF_EVENT_CODE_REMOVED,
956 	INTF_EVENT_CODE_STATUS_UPDATE,
957 	INTF_EVENT_CODE_IPADDR_ATTACHED,
958 	INTF_EVENT_CODE_IPADDR_DETACHED,
959 	INTF_EVENT_CODE_LLADDR_UPDATE,
960 	INTF_EVENT_CODE_MTU_CHANGED,
961 	INTF_EVENT_CODE_LOW_POWER_UPDATE,
962 } intf_event_code_t;
963 
964 typedef void (*ifnet_event_fn)(struct eventhandler_entry_arg, struct ifnet *, struct sockaddr *, intf_event_code_t);
965 EVENTHANDLER_DECLARE(ifnet_event, ifnet_event_fn);
966 
967 #define IF_TCP_STATINC(_ifp, _s) do {                               \
968     if ((_ifp)->if_tcp_stat != NULL)                                \
969 	    os_atomic_inc(&(_ifp)->if_tcp_stat->_s, relaxed);       \
970 } while (0);
971 
972 #define IF_UDP_STATINC(_ifp, _s) do {                               \
973     if ((_ifp)->if_udp_stat != NULL)                                \
974 	    os_atomic_inc(&(_ifp)->if_udp_stat->_s, relaxed);       \
975 } while (0);
976 
977 /*
978  * Valid values for if_refflags
979  */
980 #define IFRF_EMBRYONIC  0x1     /* ifnet is allocated; awaiting attach */
981 #define IFRF_ATTACHED   0x2     /* ifnet attach is completely done */
982 #define IFRF_DETACHING  0x4     /* detach has been requested */
983 #define IFRF_READY      0x8     /* data path is ready */
984 
985 #define IFRF_ATTACH_MASK        \
986     (IFRF_EMBRYONIC|IFRF_ATTACHED|IFRF_DETACHING)
987 
988 #define IF_FULLY_ATTACHED(_ifp) \
989     (((_ifp)->if_refflags & IFRF_ATTACH_MASK) == IFRF_ATTACHED)
990 
991 #define IF_FULLY_ATTACHED_AND_READY(_ifp) \
992     (IF_FULLY_ATTACHED(_ifp) && ((_ifp)->if_refflags & IFRF_READY))
993 /*
994  * Valid values for if_start_flags
995  */
996 #define IFSF_FLOW_CONTROLLED     0x1     /* flow controlled */
997 #define IFSF_TERMINATING         0x2     /* terminating */
998 #define IFSF_NO_DELAY            0x4     /* no delay */
999 #define IFSF_FLOW_RESUME_PENDING 0x8     /* ignore next flow control event */
1000 /*
1001  * Structure describing a `cloning' interface.
1002  */
1003 struct if_clone {
1004 	LIST_ENTRY(if_clone) ifc_list;  /* on list of cloners */
1005 	decl_lck_mtx_data(, ifc_mutex); /* To serialize clone create/delete */
1006 	u_int32_t       ifc_minifs;     /* minimum number of interfaces */
1007 	u_int32_t       ifc_maxunit;    /* maximum unit number */
1008 	u_int32_t       ifc_bmlen;      /* bitmap length */
1009 	unsigned char   *__counted_by(ifc_bmlen) ifc_units;     /* bitmap to handle units */
1010 	int             (*ifc_create)(struct if_clone *, u_int32_t, void *);
1011 	int             (*ifc_destroy)(struct ifnet *);
1012 	uint8_t         ifc_namelen;    /* length of name */
1013 	char            ifc_name[IFNAMSIZ + 1]; /* name of device, e.g. `vlan' */
1014 };
1015 
1016 #define IF_CLONE_INITIALIZER(name, create, destroy, minifs, maxunit) {          \
1017     .ifc_name = "" name,                                                        \
1018     .ifc_namelen = (sizeof(name) - 1),                                          \
1019     .ifc_minifs = (minifs),                                                     \
1020     .ifc_maxunit = (maxunit),                                                   \
1021     .ifc_create = (create),                                                     \
1022     .ifc_destroy = (destroy)                                                    \
1023 }
1024 
1025 /*
1026  * Macros to manipulate ifqueue.  Users of these macros are responsible
1027  * for serialization, by holding whatever lock is appropriate for the
1028  * corresponding structure that is referring the ifqueue.
1029  */
1030 #define IF_QFULL(ifq)           ((ifq)->ifq_len >= (ifq)->ifq_maxlen)
1031 #define IF_DROP(ifq)            ((ifq)->ifq_drops++)
1032 
1033 #define IF_ENQUEUE(ifq, m) do {                                     \
1034     (m)->m_nextpkt = NULL;                                          \
1035     if ((ifq)->ifq_tail == NULL)                                    \
1036 	    (ifq)->ifq_head = m;                                    \
1037     else                                                            \
1038 	    ((struct mbuf*)(ifq)->ifq_tail)->m_nextpkt = m;         \
1039     (ifq)->ifq_tail = m;                                            \
1040     (ifq)->ifq_len++;                                               \
1041 } while (0)
1042 
1043 #define IF_PREPEND(ifq, m) do {                                     \
1044     (m)->m_nextpkt = (mbuf_ref_t)(ifq)->ifq_head;                   \
1045     if ((ifq)->ifq_tail == NULL)                                    \
1046 	    (ifq)->ifq_tail = (m);                                  \
1047     (ifq)->ifq_head = (m);                                          \
1048     (ifq)->ifq_len++;                                               \
1049 } while (0)
1050 
1051 #define IF_DEQUEUE(ifq, m) do {                                     \
1052     (m) = (mbuf_ref_t)(ifq)->ifq_head;                              \
1053     if (m != NULL) {                                                \
1054 	    if (((ifq)->ifq_head = (m)->m_nextpkt) == NULL)         \
1055 	            (ifq)->ifq_tail = NULL;                         \
1056 	    (m)->m_nextpkt = NULL;                                  \
1057 	    (ifq)->ifq_len--;                                       \
1058     }                                                               \
1059 } while (0)
1060 
1061 #define IF_REMQUEUE(ifq, m) do {                                    \
1062     mbuf_ref_t _p = (mbuf_ref_t)(ifq)->ifq_head;                    \
1063     mbuf_ref_t _n = (m)->m_nextpkt;                                 \
1064     if ((m) == _p)                                                  \
1065 	    _p = NULL;                                              \
1066     while (_p != NULL) {                                            \
1067 	    if (_p->m_nextpkt == (m))                               \
1068 	            break;                                          \
1069 	    _p = _p->m_nextpkt;                                     \
1070     }                                                               \
1071     VERIFY(_p != NULL || ((m) == (ifq)->ifq_head));                 \
1072     if ((m) == (ifq)->ifq_head)                                     \
1073 	    (ifq)->ifq_head = _n;                                   \
1074     if ((m) == (ifq)->ifq_tail)                                     \
1075 	    (ifq)->ifq_tail = _p;                                   \
1076     VERIFY((ifq)->ifq_tail != NULL || (ifq)->ifq_head == NULL);     \
1077     VERIFY((ifq)->ifq_len != 0);                                    \
1078     --(ifq)->ifq_len;                                               \
1079     if (_p != NULL)                                                 \
1080 	    _p->m_nextpkt = _n;                                     \
1081     (m)->m_nextpkt = NULL;                                          \
1082 } while (0)
1083 
1084 #define IF_DRAIN(ifq) do {                                          \
1085     struct mbuf *_m;                                                \
1086     for (;;) {                                                      \
1087 	    IF_DEQUEUE(ifq, _m);                                    \
1088 	    if (_m == NULL)                                         \
1089 	            break;                                          \
1090 	    m_freem(_m);                                            \
1091     }                                                               \
1092 } while (0)
1093 
1094 /*
1095  * The ifaddr structure contains information about one address
1096  * of an interface.  They are maintained by the different address families,
1097  * are allocated and attached when an address is set, and are linked
1098  * together so all addresses for an interface can be located.
1099  */
1100 struct ifaddr {
1101 	decl_lck_mtx_data(, ifa_lock);  /* lock for ifaddr */
1102 	os_ref_atomic_t ifa_refcnt;     /* ref count, use IFA_{ADD,REM}REF */
1103 	uint32_t        ifa_debug;      /* debug flags */
1104 	struct sockaddr *ifa_addr;      /* address of interface */
1105 	struct sockaddr *ifa_dstaddr;   /* other end of p-to-p link */
1106 #define ifa_broadaddr   ifa_dstaddr     /* broadcast address interface */
1107 	struct sockaddr *ifa_netmask;   /* used to determine subnet */
1108 	struct ifnet    *ifa_ifp;       /* back-pointer to interface */
1109 	TAILQ_ENTRY(ifaddr) ifa_link;   /* queue macro glue */
1110 	void (*ifa_rtrequest)           /* check or clean routes (+ or -)'d */
1111 	(int, struct rtentry *, struct sockaddr *);
1112 	uint32_t        ifa_flags;      /* mostly rt_flags for cloning */
1113 	int32_t         ifa_metric;     /* cost of going out this interface */
1114 	void (*ifa_free)(struct ifaddr *); /* callback fn for freeing */
1115 	void *ifa_del_wc;               /* Wait channel to avoid address deletion races */
1116 	int ifa_del_waiters;            /* Threads in wait to delete the address */
1117 };
1118 
1119 /*
1120  * Valid values for ifa_flags
1121  */
1122 #define IFA_ROUTE       RTF_UP          /* route installed (0x1) */
1123 #define IFA_CLONING     RTF_CLONING     /* (0x100) */
1124 
1125 /*
1126  * Valid values for ifa_debug
1127  */
1128 #define IFD_ATTACHED    0x1             /* attached to list */
1129 #define IFD_ALLOC       0x2             /* dynamically allocated */
1130 #define IFD_DEBUG       0x4             /* has debugging info */
1131 #define IFD_LINK        0x8             /* link address */
1132 #define IFD_TRASHED     0x10            /* in trash list */
1133 #define IFD_DETACHING   0x20            /* detach is in progress */
1134 #define IFD_NOTREADY    0x40            /* embryonic; not yet ready */
1135 
1136 #define IFA_LOCK_ASSERT_HELD(_ifa)                                      \
1137     LCK_MTX_ASSERT(&(_ifa)->ifa_lock, LCK_MTX_ASSERT_OWNED)
1138 
1139 #define IFA_LOCK_ASSERT_NOTHELD(_ifa)                                   \
1140     LCK_MTX_ASSERT(&(_ifa)->ifa_lock, LCK_MTX_ASSERT_NOTOWNED)
1141 
1142 #define IFA_LOCK(_ifa)                                                  \
1143     lck_mtx_lock(&(_ifa)->ifa_lock)
1144 
1145 #define IFA_LOCK_SPIN(_ifa)                                             \
1146     lck_mtx_lock_spin(&(_ifa)->ifa_lock)
1147 
1148 #define IFA_CONVERT_LOCK(_ifa) do {                                     \
1149     IFA_LOCK_ASSERT_HELD(_ifa);                                         \
1150     lck_mtx_convert_spin(&(_ifa)->ifa_lock);                            \
1151 } while (0)
1152 
1153 #define IFA_UNLOCK(_ifa)                                                \
1154     lck_mtx_unlock(&(_ifa)->ifa_lock)
1155 
1156 os_refgrp_decl(static, ifa_refgrp, "ifa refcounts", NULL);
1157 
1158 static inline void
ifa_addref(struct ifaddr * ifa)1159 ifa_addref(struct ifaddr *ifa)
1160 {
1161 	os_ref_retain_raw(&ifa->ifa_refcnt, &ifa_refgrp);
1162 }
1163 
1164 __private_extern__ void ifa_deallocated(struct ifaddr *ifa);
1165 
1166 static inline void
ifa_remref(struct ifaddr * ifa)1167 ifa_remref(struct ifaddr *ifa)
1168 {
1169 	/* We can use _relaxed, because if we hit 0 we make sure the lock is held */
1170 	if (os_ref_release_raw_relaxed(&ifa->ifa_refcnt, &ifa_refgrp) == 0) {
1171 		ifa_deallocated(ifa);
1172 	}
1173 }
1174 
1175 /*
1176  * Multicast address structure.  This is analogous to the ifaddr
1177  * structure except that it keeps track of multicast addresses.
1178  * Also, the request count here is a count of requests for this
1179  * address, not a count of pointers to this structure; anonymous
1180  * membership(s) holds one outstanding request count.
1181  */
1182 struct ifmultiaddr {
1183 	decl_lck_mtx_data(, ifma_lock);
1184 	u_int32_t ifma_refcount;        /* reference count */
1185 	u_int32_t ifma_anoncnt;         /* # of anonymous requests */
1186 	u_int32_t ifma_reqcnt;          /* total requests for this address */
1187 	u_int32_t ifma_debug;           /* see ifa_debug flags */
1188 	u_int32_t ifma_flags;           /* see below */
1189 	LIST_ENTRY(ifmultiaddr) ifma_link; /* queue macro glue */
1190 	struct sockaddr *ifma_addr;     /* address this membership is for */
1191 	struct ifmultiaddr *ifma_ll;    /* link-layer translation, if any */
1192 	struct ifnet *ifma_ifp;         /* back-pointer to interface */
1193 	void *ifma_protospec;           /* protocol-specific state, if any */
1194 	void (*ifma_trace)              /* callback fn for tracing refs */
1195 	(struct ifmultiaddr *, int);
1196 };
1197 
1198 /*
1199  * Values for ifma_flags
1200  */
1201 #define IFMAF_ANONYMOUS         0x1     /* has anonymous request ref(s) held */
1202 
1203 #define IFMA_LOCK_ASSERT_HELD(_ifma)                                    \
1204     LCK_MTX_ASSERT(&(_ifma)->ifma_lock, LCK_MTX_ASSERT_OWNED)
1205 
1206 #define IFMA_LOCK_ASSERT_NOTHELD(_ifma)                                 \
1207     LCK_MTX_ASSERT(&(_ifma)->ifma_lock, LCK_MTX_ASSERT_NOTOWNED)
1208 
1209 #define IFMA_LOCK(_ifma)                                                \
1210     lck_mtx_lock(&(_ifma)->ifma_lock)
1211 
1212 #define IFMA_LOCK_SPIN(_ifma)                                           \
1213     lck_mtx_lock_spin(&(_ifma)->ifma_lock)
1214 
1215 #define IFMA_CONVERT_LOCK(_ifma) do {                                   \
1216     IFMA_LOCK_ASSERT_HELD(_ifma);                                       \
1217     lck_mtx_convert_spin(&(_ifma)->ifma_lock);                          \
1218 } while (0)
1219 
1220 #define IFMA_UNLOCK(_ifma)                                              \
1221     lck_mtx_unlock(&(_ifma)->ifma_lock)
1222 
1223 #define IFMA_ADDREF(_ifma)                                              \
1224     ifma_addref(_ifma, 0)
1225 
1226 #define IFMA_ADDREF_LOCKED(_ifma)                                       \
1227     ifma_addref(_ifma, 1)
1228 
1229 #define IFMA_REMREF(_ifma)                                              \
1230     ifma_remref(_ifma)
1231 
1232 /*
1233  * Indicate whether or not the immediate interface, or the interface delegated
1234  * by it, is a cellular interface (IFT_CELLULAR).  Delegated interface type is
1235  * set/cleared along with the delegated ifp; we cache the type for performance
1236  * to avoid dereferencing delegated ifp each time.
1237  *
1238  * Note that this is meant to be used only for accounting and policy purposes;
1239  * certain places need to explicitly know the immediate interface type, and
1240  * this macro should not be used there.
1241  *
1242  * The test is done against IFT_CELLULAR instead of IFNET_FAMILY_CELLULAR to
1243  * handle certain cases where the family isn't set to the latter.
1244  *
1245  * This macro also handles the case of IFNET_FAMILY_ETHERNET with
1246  * IFNET_SUBFAMILY_SIMCELL which is used to simulate a cellular interface
1247  * for testing purposes. The underlying interface is Ethernet but we treat
1248  * it as cellular for accounting and policy purposes.
1249  */
1250 #define IFNET_IS_CELLULAR(_ifp)                                     \
1251     ((_ifp)->if_type == IFT_CELLULAR ||                             \
1252     (_ifp)->if_delegated.type == IFT_CELLULAR ||                    \
1253     (((_ifp)->if_family == IFNET_FAMILY_ETHERNET  &&                \
1254     (_ifp)->if_subfamily == IFNET_SUBFAMILY_SIMCELL)) ||            \
1255     ((_ifp)->if_delegated.family == IFNET_FAMILY_ETHERNET &&        \
1256     (_ifp)->if_delegated.subfamily == IFNET_SUBFAMILY_SIMCELL))
1257 
1258 /*
1259  * Indicate whether or not the immediate interface, or the interface delegated
1260  * by it, is an ETHERNET interface.
1261  */
1262 #define IFNET_IS_ETHERNET(_ifp)                                     \
1263     ((_ifp)->if_family == IFNET_FAMILY_ETHERNET ||                  \
1264     (_ifp)->if_delegated.family == IFNET_FAMILY_ETHERNET)
1265 /*
1266  * Indicate whether or not the immediate interface, or the interface delegated
1267  * by it, is a Wi-Fi interface (IFNET_SUBFAMILY_WIFI).  Delegated interface
1268  * subfamily is set/cleared along with the delegated ifp; we cache the subfamily
1269  * for performance to avoid dereferencing delegated ifp each time.
1270  *
1271  * Note that this is meant to be used only for accounting and policy purposes;
1272  * certain places need to explicitly know the immediate interface type, and
1273  * this macro should not be used there.
1274  *
1275  * The test is done against IFNET_SUBFAMILY_WIFI as the family may be set to
1276  * IFNET_FAMILY_ETHERNET (as well as type to IFT_ETHER) which is too generic.
1277  */
1278 #define IFNET_IS_WIFI(_ifp)                                         \
1279     (((_ifp)->if_family == IFNET_FAMILY_ETHERNET  &&                \
1280     (_ifp)->if_subfamily == IFNET_SUBFAMILY_WIFI) ||                \
1281     ((_ifp)->if_delegated.family == IFNET_FAMILY_ETHERNET &&        \
1282     (_ifp)->if_delegated.subfamily == IFNET_SUBFAMILY_WIFI))
1283 
1284 /*
1285  * Indicate whether or not the immediate interface, or the interface delegated
1286  * by it, is a Wired interface (several families).  Delegated interface
1287  * family is set/cleared along with the delegated ifp; we cache the family
1288  * for performance to avoid dereferencing delegated ifp each time.
1289  *
1290  * Note that this is meant to be used only for accounting and policy purposes;
1291  * certain places need to explicitly know the immediate interface type, and
1292  * this macro should not be used there.
1293  */
1294 #define IFNET_IS_WIRED(_ifp)                                        \
1295     ((_ifp)->if_family == IFNET_FAMILY_ETHERNET ||                  \
1296     (_ifp)->if_delegated.family == IFNET_FAMILY_ETHERNET ||         \
1297     (_ifp)->if_family == IFNET_FAMILY_FIREWIRE ||                   \
1298     (_ifp)->if_delegated.family == IFNET_FAMILY_FIREWIRE)
1299 
1300 /*
1301  * Indicate whether or not the immediate WiFi interface is on an infrastructure
1302  * network
1303  */
1304 #define IFNET_IS_WIFI_INFRA(_ifp)                           \
1305     ((_ifp)->if_family == IFNET_FAMILY_ETHERNET &&          \
1306     (_ifp)->if_subfamily == IFNET_SUBFAMILY_WIFI &&         \
1307     !((_ifp)->if_eflags & IFEF_AWDL) &&                     \
1308     !((_ifp)->if_xflags & IFXF_LOW_LATENCY))
1309 
1310 /*
1311  * Indicate whether or not the immediate interface is a companion link
1312  * interface.
1313  */
1314 #define IFNET_IS_COMPANION_LINK(_ifp)                       \
1315     ((_ifp)->if_family == IFNET_FAMILY_IPSEC &&             \
1316     ((_ifp)->if_subfamily == IFNET_SUBFAMILY_BLUETOOTH ||   \
1317     (_ifp)->if_subfamily == IFNET_SUBFAMILY_WIFI ||         \
1318     (_ifp)->if_subfamily == IFNET_SUBFAMILY_QUICKRELAY ||   \
1319     (_ifp)->if_subfamily == IFNET_SUBFAMILY_DEFAULT))
1320 
1321 /*
1322  * Indicate whether or not the immediate interface is a companion link
1323  * interface using Bluetooth
1324  */
1325 #define IFNET_IS_COMPANION_LINK_BLUETOOTH(_ifp)             \
1326     ((_ifp)->if_family == IFNET_FAMILY_IPSEC &&             \
1327     (_ifp)->if_subfamily == IFNET_SUBFAMILY_BLUETOOTH)
1328 
1329 /*
1330  * Indicate whether or not the immediate interface, or the interface delegated
1331  * by it, is marked as expensive.  The delegated interface is set/cleared
1332  * along with the delegated ifp; we cache the flag for performance to avoid
1333  * dereferencing delegated ifp each time.
1334  *
1335  * Note that this is meant to be used only for policy purposes.
1336  */
1337 #define IFNET_IS_EXPENSIVE(_ifp)                                        \
1338     ((_ifp)->if_eflags & IFEF_EXPENSIVE ||                              \
1339     (_ifp)->if_delegated.expensive)
1340 
1341 #define IFNET_IS_LOW_POWER(_ifp)                                    \
1342     (if_low_power_restricted != 0 &&                                \
1343     ((_ifp)->if_xflags & IFXF_LOW_POWER) ||                         \
1344     ((_ifp)->if_delegated.ifp != NULL &&                            \
1345     ((_ifp)->if_delegated.ifp->if_xflags & IFXF_LOW_POWER)))
1346 
1347 #define IFNET_IS_CONSTRAINED(_ifp)                                      \
1348     ((_ifp)->if_xflags & IFXF_CONSTRAINED ||                            \
1349     (_ifp)->if_delegated.constrained ||                                 \
1350     (_ifp)->if_xflags & IFXF_ULTRA_CONSTRAINED ||                       \
1351     (_ifp)->if_delegated.ultra_constrained)
1352 
1353 #define IFNET_IS_ULTRA_CONSTRAINED(_ifp)                                \
1354     ((_ifp)->if_xflags & IFXF_ULTRA_CONSTRAINED ||                      \
1355     (_ifp)->if_delegated.ultra_constrained)
1356 
1357 #define IFNET_IS_VPN(_ifp)                                          \
1358 	((_ifp)->if_xflags & IFXF_IS_VPN)                               \
1359 
1360 /*
1361  * We don't support AWDL interface delegation.
1362  */
1363 #define IFNET_IS_AWDL_RESTRICTED(_ifp)                              \
1364     (((_ifp)->if_eflags & (IFEF_AWDL|IFEF_AWDL_RESTRICTED)) ==      \
1365     (IFEF_AWDL|IFEF_AWDL_RESTRICTED))
1366 
1367 #define IFNET_IS_INTCOPROC(_ifp)                                    \
1368     ((_ifp)->if_family == IFNET_FAMILY_ETHERNET &&                  \
1369      (_ifp)->if_subfamily == IFNET_SUBFAMILY_INTCOPROC)
1370 
1371 #define IFNET_IS_VMNET(_ifp)                                        \
1372     ((_ifp)->if_family == IFNET_FAMILY_ETHERNET &&                  \
1373      (_ifp)->if_subfamily == IFNET_SUBFAMILY_VMNET)
1374 
1375 #define IFNET_IS_MANAGEMENT(_ifp)                                        \
1376      (((_ifp)->if_xflags & IFXF_MANAGEMENT) != 0)
1377 
1378 /*
1379  * Indicate whether or not the immediate interface is IP over Thunderbolt.
1380  */
1381 #define IFNET_IS_THUNDERBOLT_IP(_ifp)                            \
1382     ((_ifp)->if_family == IFNET_FAMILY_ETHERNET &&               \
1383      (_ifp)->if_subfamily == IFNET_SUBFAMILY_THUNDERBOLT)
1384 
1385 #define IFNET_IS_REDIRECT(_ifp)                       \
1386     (((_ifp)->if_family == IFNET_FAMILY_ETHERNET ||   \
1387     (_ifp)->if_family == IFNET_FAMILY_CELLULAR) &&    \
1388     (_ifp)->if_subfamily == IFNET_SUBFAMILY_REDIRECT)
1389 
1390 extern int if_index;
1391 extern int if_indexcount;
1392 extern uint32_t ifindex2ifnetcount;
1393 extern struct ifnethead ifnet_head;
1394 extern struct ifnethead ifnet_ordered_head;
1395 extern struct ifnet **__counted_by_or_null(ifindex2ifnetcount) ifindex2ifnet;
1396 extern u_int32_t if_sndq_maxlen;
1397 extern u_int32_t if_rcvq_maxlen;
1398 extern struct ifaddr **__counted_by_or_null(if_indexcount) ifnet_addrs;
1399 extern lck_attr_t ifa_mtx_attr;
1400 extern lck_grp_t ifa_mtx_grp;
1401 extern lck_grp_t ifnet_lock_group;
1402 extern lck_attr_t ifnet_lock_attr;
1403 extern ifnet_t lo_ifp;
1404 extern uint32_t net_wake_pkt_debug;
1405 
1406 extern int if_addmulti(struct ifnet *, const struct sockaddr *,
1407     struct ifmultiaddr **);
1408 extern int if_addmulti_anon(struct ifnet *, const struct sockaddr *,
1409     struct ifmultiaddr **);
1410 extern int if_allmulti(struct ifnet *, int);
1411 extern int if_delmulti(struct ifnet *, const struct sockaddr *);
1412 extern int if_delmulti_ifma(struct ifmultiaddr *);
1413 extern int if_delmulti_anon(struct ifnet *, const struct sockaddr *);
1414 extern void if_down(struct ifnet *);
1415 extern int if_down_all(void);
1416 extern void if_up(struct ifnet *);
1417 __private_extern__ void if_updown(struct ifnet *ifp, int up);
1418 extern int ifioctl(struct socket *, u_long cmd, caddr_t __sized_by(IOCPARM_LEN(cmd)), struct proc *);
1419 extern int ifioctllocked(struct socket *, u_long cmd, caddr_t __sized_by(IOCPARM_LEN(cmd)), struct proc *);
1420 extern struct ifnet *ifunit(const char *);
1421 extern struct ifnet *ifunit_ref(const char *);
1422 extern int ifunit_extract(const char *src, char *__counted_by(dstlen)dst, size_t dstlen, int *unit);
1423 extern struct ifnet *if_withname(struct sockaddr *);
1424 extern void if_qflush(struct ifnet *, struct ifclassq *, bool);
1425 extern void if_qflush_snd(struct ifnet *, bool);
1426 extern void if_qflush_sc(struct ifnet *, mbuf_svc_class_t, u_int32_t,
1427     u_int32_t *, u_int32_t *, int);
1428 
1429 extern struct if_clone *if_clone_lookup(const char *__counted_by(namelen) name, size_t namelen, u_int32_t *);
1430 extern int if_clone_attach(struct if_clone *);
1431 extern void if_clone_detach(struct if_clone *);
1432 extern u_int32_t if_functional_type(struct ifnet *, bool);
1433 extern u_int32_t if_peer_egress_functional_type(struct ifnet *, bool);
1434 
1435 extern errno_t if_mcasts_update(struct ifnet *);
1436 
1437 extern const char *intf_event2str(int);
1438 
1439 typedef enum {
1440 	IFNET_LCK_ASSERT_EXCLUSIVE,     /* RW: held as writer */
1441 	IFNET_LCK_ASSERT_SHARED,        /* RW: held as reader */
1442 	IFNET_LCK_ASSERT_OWNED,         /* RW: writer/reader, MTX: held */
1443 	IFNET_LCK_ASSERT_NOTOWNED       /* not held */
1444 } ifnet_lock_assert_t;
1445 
1446 static inline char *__header_indexable
if_inline_lladdr(struct ifnet * ifp)1447 if_inline_lladdr(struct ifnet *ifp)
1448 {
1449 	// N.B.: unable to use SDL() from sockaddr_utils.h yet.
1450 	struct sockaddr *s = ifp->if_lladdr->ifa_addr;
1451 #pragma clang diagnostic push
1452 #pragma clang diagnostic ignored "-Wcast-align"
1453 	struct sockaddr_dl *dl = __unsafe_forge_bidi_indexable(struct sockaddr_dl *,
1454 	    s, s->sa_len);
1455 #pragma clang diagnostic pop
1456 
1457 	return LLADDR(dl);
1458 }
1459 #define IF_LLADDR(_ifp) if_inline_lladdr(_ifp)
1460 
1461 #define IF_INDEX_IN_RANGE(_ind_) ((_ind_) > 0 && \
1462     (unsigned int)(_ind_) <= (unsigned int)if_index)
1463 
1464 __private_extern__ void ifnet_lock_assert(struct ifnet *, ifnet_lock_assert_t);
1465 __private_extern__ void ifnet_lock_shared(struct ifnet *ifp);
1466 __private_extern__ void ifnet_lock_exclusive(struct ifnet *ifp);
1467 __private_extern__ void ifnet_lock_done(struct ifnet *ifp);
1468 
1469 #if INET
1470 __private_extern__ void if_inetdata_lock_shared(struct ifnet *ifp);
1471 __private_extern__ void if_inetdata_lock_exclusive(struct ifnet *ifp);
1472 __private_extern__ void if_inetdata_lock_done(struct ifnet *ifp);
1473 #endif
1474 
1475 __private_extern__ void if_inet6data_lock_shared(struct ifnet *ifp);
1476 __private_extern__ void if_inet6data_lock_exclusive(struct ifnet *ifp);
1477 __private_extern__ void if_inet6data_lock_done(struct ifnet *ifp);
1478 
1479 __private_extern__ void ifnet_head_lock_shared(void);
1480 __private_extern__ void ifnet_head_lock_exclusive(void);
1481 __private_extern__ void ifnet_head_done(void);
1482 __private_extern__ void ifnet_head_assert_exclusive(void);
1483 
1484 __private_extern__ errno_t ifnet_set_idle_flags_locked(ifnet_t, u_int32_t,
1485     u_int32_t);
1486 __private_extern__ int ifnet_is_attached(struct ifnet *, int refio);
1487 __private_extern__ void ifnet_incr_pending_thread_count(struct ifnet *);
1488 __private_extern__ void ifnet_decr_pending_thread_count(struct ifnet *);
1489 __private_extern__ void ifnet_incr_iorefcnt(struct ifnet *);
1490 __private_extern__ void ifnet_decr_iorefcnt(struct ifnet *);
1491 __private_extern__ boolean_t ifnet_datamov_begin(struct ifnet *);
1492 __private_extern__ void ifnet_datamov_end(struct ifnet *);
1493 __private_extern__ void ifnet_datamov_suspend(struct ifnet *);
1494 __private_extern__ boolean_t ifnet_datamov_suspend_if_needed(struct ifnet *);
1495 __private_extern__ void ifnet_datamov_drain(struct ifnet *);
1496 __private_extern__ void ifnet_datamov_suspend_and_drain(struct ifnet *);
1497 __private_extern__ void ifnet_datamov_resume(struct ifnet *);
1498 __private_extern__ void ifnet_set_start_cycle(struct ifnet *,
1499     struct timespec *);
1500 __private_extern__ void ifnet_set_poll_cycle(struct ifnet *,
1501     struct timespec *);
1502 
1503 __private_extern__ void if_attach_ifa(struct ifnet *, struct ifaddr *);
1504 __private_extern__ void if_attach_link_ifa(struct ifnet *, struct ifaddr *);
1505 __private_extern__ void if_detach_ifa(struct ifnet *, struct ifaddr *);
1506 __private_extern__ void if_detach_link_ifa(struct ifnet *, struct ifaddr *);
1507 
1508 __private_extern__ void dlil_if_lock(void);
1509 __private_extern__ void dlil_if_unlock(void);
1510 __private_extern__ void dlil_if_lock_assert(void);
1511 
1512 extern struct ifaddr *ifa_ifwithaddr(const struct sockaddr *);
1513 extern struct ifaddr *ifa_ifwithaddr_locked(const struct sockaddr *);
1514 extern struct ifaddr *ifa_ifwithaddr_scoped(const struct sockaddr *,
1515     unsigned int);
1516 extern struct ifaddr *ifa_ifwithaddr_scoped_locked(const struct sockaddr *,
1517     unsigned int);
1518 extern struct ifaddr *ifa_ifwithdstaddr(const struct sockaddr *);
1519 extern struct ifaddr *ifa_ifwithdstaddr_scoped(const struct sockaddr *,
1520     unsigned int);
1521 extern struct ifaddr *ifa_ifwithnet(const struct sockaddr *);
1522 extern struct ifaddr *ifa_ifwithnet_scoped(const struct sockaddr *,
1523     unsigned int);
1524 extern struct ifaddr *ifa_ifwithroute(int, const struct sockaddr *,
1525     const struct sockaddr *);
1526 extern struct   ifaddr *ifa_ifwithroute_locked(int, const struct sockaddr *,
1527     const struct sockaddr *);
1528 extern struct ifaddr *ifa_ifwithroute_scoped_locked(int,
1529     const struct sockaddr *, const struct sockaddr *, unsigned int);
1530 extern struct ifaddr *ifaof_ifpforaddr_select(const struct sockaddr *, struct ifnet *);
1531 extern struct ifaddr *ifaof_ifpforaddr(const struct sockaddr *, struct ifnet *);
1532 __private_extern__ struct ifaddr *ifa_ifpgetprimary(struct ifnet *, int);
1533 extern void ifa_initref(struct ifaddr *);
1534 extern void ifa_lock_init(struct ifaddr *);
1535 extern void ifa_lock_destroy(struct ifaddr *);
1536 extern void ifma_addref(struct ifmultiaddr *, int);
1537 extern void ifma_remref(struct ifmultiaddr *);
1538 
1539 extern void ifa_init(void);
1540 
1541 __private_extern__ struct in_ifaddr *ifa_foraddr(unsigned int);
1542 __private_extern__ struct in_ifaddr *ifa_foraddr_scoped(unsigned int,
1543     unsigned int);
1544 
1545 struct ifreq;
1546 extern errno_t ifnet_getset_opportunistic(struct ifnet *, u_long,
1547     struct ifreq *, struct proc *);
1548 extern int ifnet_get_throttle(struct ifnet *, u_int32_t *);
1549 extern int ifnet_set_throttle(struct ifnet *, u_int32_t);
1550 extern errno_t ifnet_getset_log(struct ifnet *, u_long,
1551     struct ifreq *, struct proc *);
1552 extern int ifnet_set_log(struct ifnet *, int32_t, uint32_t, int32_t, int32_t);
1553 extern int ifnet_get_log(struct ifnet *, int32_t *, uint32_t *, int32_t *,
1554     int32_t *);
1555 extern int ifnet_notify_address(struct ifnet *, int);
1556 extern void ifnet_notify_data_threshold(struct ifnet *);
1557 
1558 struct in6_addr;
1559 __private_extern__ struct in6_ifaddr *ifa_foraddr6(struct in6_addr *);
1560 __private_extern__ struct in6_ifaddr *ifa_foraddr6_scoped(struct in6_addr *,
1561     unsigned int);
1562 
1563 __private_extern__ void if_data_internal_to_if_data(struct ifnet *ifp,
1564     const struct if_data_internal *if_data_int, struct if_data *if_data);
1565 __private_extern__ void if_data_internal_to_if_data64(struct ifnet *ifp,
1566     const struct if_data_internal *if_data_int, struct if_data64 *if_data64);
1567 __private_extern__ void if_copy_traffic_class(struct ifnet *ifp,
1568     struct if_traffic_class *if_tc);
1569 __private_extern__ void if_copy_data_extended(struct ifnet *ifp,
1570     struct if_data_extended *if_de);
1571 __private_extern__ void if_copy_packet_stats(struct ifnet *ifp,
1572     struct if_packet_stats *if_ps);
1573 __private_extern__ void if_copy_rxpoll_stats(struct ifnet *ifp,
1574     struct if_rxpoll_stats *if_rs);
1575 __private_extern__ void if_copy_netif_stats(struct ifnet *ifp,
1576     struct if_netif_stats *if_ns);
1577 
1578 __private_extern__ struct rtentry *ifnet_cached_rtlookup_inet(struct ifnet *,
1579     struct in_addr);
1580 __private_extern__ struct rtentry *ifnet_cached_rtlookup_inet6(struct ifnet *,
1581     struct in6_addr *);
1582 
1583 __private_extern__ u_int32_t if_get_protolist(struct ifnet * ifp,
1584     u_int32_t *protolist, u_int32_t count);
1585 __private_extern__ void if_free_protolist(u_int32_t *list);
1586 __private_extern__ errno_t if_state_update(struct ifnet *,
1587     struct if_interface_state *);
1588 __private_extern__ void if_get_state(struct ifnet *,
1589     struct if_interface_state *);
1590 __private_extern__ errno_t if_probe_connectivity(struct ifnet *ifp,
1591     u_int32_t conn_probe);
1592 __private_extern__ void if_lqm_update(struct ifnet *, int32_t, int);
1593 __private_extern__ void ifnet_update_sndq(struct ifclassq *, cqev_t);
1594 __private_extern__ void ifnet_update_rcv(struct ifnet *, cqev_t);
1595 
1596 __private_extern__ void ifnet_flowadv(uint32_t);
1597 
1598 __private_extern__ errno_t ifnet_set_input_bandwidths(struct ifnet *,
1599     struct if_bandwidths *);
1600 __private_extern__ errno_t ifnet_set_output_bandwidths(struct ifnet *,
1601     struct if_bandwidths *, boolean_t);
1602 __private_extern__ u_int64_t ifnet_output_linkrate(struct ifnet *);
1603 __private_extern__ u_int64_t ifnet_input_linkrate(struct ifnet *);
1604 
1605 __private_extern__ errno_t ifnet_set_input_latencies(struct ifnet *,
1606     struct if_latencies *);
1607 __private_extern__ errno_t ifnet_set_output_latencies(struct ifnet *,
1608     struct if_latencies *, boolean_t);
1609 
1610 __private_extern__ void ifnet_clear_netagent(uuid_t);
1611 
1612 __private_extern__ int ifnet_set_netsignature(struct ifnet *, uint8_t,
1613     uint8_t, uint16_t, uint8_t *);
1614 __private_extern__ int ifnet_get_netsignature(struct ifnet *, uint8_t,
1615     uint8_t *, uint16_t *, uint8_t *);
1616 
1617 struct ipv6_prefix;
1618 __private_extern__ int ifnet_set_nat64prefix(struct ifnet *,
1619     struct ipv6_prefix *);
1620 __private_extern__ int ifnet_get_nat64prefix(struct ifnet *,
1621     struct ipv6_prefix *);
1622 
1623 /* Required exclusive ifnet_head lock */
1624 __private_extern__ void ifnet_remove_from_ordered_list(struct ifnet *);
1625 
1626 __private_extern__ void ifnet_increment_generation(struct ifnet *);
1627 __private_extern__ u_int32_t ifnet_get_generation(struct ifnet *);
1628 
1629 /* Adding and deleting netagents will take ifnet lock */
1630 __private_extern__ int if_add_netagent(struct ifnet *, uuid_t);
1631 __private_extern__ int if_add_netagent_locked(struct ifnet *, uuid_t);
1632 __private_extern__ int if_delete_netagent(struct ifnet *, uuid_t);
1633 __private_extern__ boolean_t if_check_netagent(struct ifnet *, uuid_t);
1634 
1635 #if SKYWALK
1636 extern unsigned int if_enable_fsw_ip_netagent;
1637 static inline boolean_t
if_is_fsw_ip_netagent_enabled(void)1638 if_is_fsw_ip_netagent_enabled(void)
1639 {
1640 	return if_enable_fsw_ip_netagent != 0;
1641 }
1642 extern unsigned int if_enable_fsw_transport_netagent;
1643 static inline boolean_t
if_is_fsw_transport_netagent_enabled(void)1644 if_is_fsw_transport_netagent_enabled(void)
1645 {
1646 	return if_enable_fsw_transport_netagent != 0;
1647 }
1648 static inline boolean_t
if_is_fsw_netagent_enabled(void)1649 if_is_fsw_netagent_enabled(void)
1650 {
1651 	return if_is_fsw_transport_netagent_enabled() ||
1652 	       if_is_fsw_ip_netagent_enabled();
1653 }
1654 #endif /* SKYWALK */
1655 
1656 extern int if_set_qosmarking_mode(struct ifnet *, u_int32_t);
1657 __private_extern__ uint32_t ifnet_mbuf_packetpreamblelen(struct ifnet *);
1658 __private_extern__ void intf_event_enqueue_nwk_wq_entry(struct ifnet *ifp,
1659     struct sockaddr *addrp, uint32_t intf_event_code);
1660 __private_extern__ void ifnet_update_stats_per_flow(struct ifnet_stats_per_flow *,
1661     struct ifnet *);
1662 __private_extern__ int if_get_tcp_kao_max(struct ifnet *);
1663 #if XNU_TARGET_OS_OSX
1664 __private_extern__ errno_t ifnet_framer_stub(struct ifnet *, struct mbuf **,
1665     const struct sockaddr *, const char *, const char *, u_int32_t *,
1666     u_int32_t *);
1667 #endif /* XNU_TARGET_OS_OSX */
1668 __private_extern__ void ifnet_enqueue_multi_setup(struct ifnet *, uint16_t,
1669     uint16_t);
1670 __private_extern__ errno_t ifnet_enqueue_mbuf(struct ifnet *, struct mbuf *,
1671     boolean_t, boolean_t *);
1672 __private_extern__ errno_t ifnet_enqueue_mbuf_chain(struct ifnet *,
1673     struct mbuf *, struct mbuf *, uint32_t, uint32_t, boolean_t, boolean_t *);
1674 __private_extern__ int ifnet_enqueue_netem(void *handle, pktsched_pkt_t *pkts,
1675     uint32_t n_pkts);
1676 #if SKYWALK
1677 struct __kern_packet;
1678 extern errno_t ifnet_enqueue_pkt(struct ifnet *,
1679     struct __kern_packet *, boolean_t, boolean_t *);
1680 extern errno_t ifnet_enqueue_ifcq_pkt(struct ifnet *, struct ifclassq *,
1681     struct __kern_packet *, boolean_t, boolean_t *);
1682 extern errno_t ifnet_enqueue_pkt_chain(struct ifnet *, struct __kern_packet *,
1683     struct __kern_packet *, uint32_t, uint32_t, boolean_t, boolean_t *);
1684 extern errno_t ifnet_enqueue_ifcq_pkt_chain(struct ifnet *, struct ifclassq *,
1685     struct __kern_packet *, struct __kern_packet *, uint32_t, uint32_t, boolean_t,
1686     boolean_t *);
1687 extern errno_t ifnet_set_output_handler(struct ifnet *, ifnet_output_func);
1688 extern void ifnet_reset_output_handler(struct ifnet *);
1689 extern errno_t ifnet_set_start_handler(struct ifnet *, ifnet_start_func);
1690 extern void ifnet_reset_start_handler(struct ifnet *);
1691 
1692 #define SK_NXS_MS_IF_ADDR_GENCNT_INC(ifp)        \
1693     os_atomic_inc(&(ifp)->if_nx_flowswitch.if_fsw_ipaddr_gencnt, relaxed);
1694 #endif /* SKYWALK */
1695 
1696 extern int if_low_power_verbose;
1697 extern int if_low_power_restricted;
1698 extern void if_low_power_evhdlr_init(void);
1699 extern int if_set_low_power(struct ifnet *, bool);
1700 extern u_int32_t if_set_eflags(ifnet_t, u_int32_t);
1701 extern void if_clear_eflags(ifnet_t, u_int32_t);
1702 extern u_int32_t if_set_xflags(ifnet_t, u_int32_t);
1703 extern void if_clear_xflags(ifnet_t, u_int32_t);
1704 extern boolean_t sa_equal(const struct sockaddr *, const struct sockaddr *);
1705 extern void ifnet_update_traffic_rule_genid(struct ifnet *);
1706 extern boolean_t ifnet_sync_traffic_rule_genid(struct ifnet *, uint32_t *);
1707 extern void ifnet_update_traffic_rule_count(struct ifnet *, uint32_t);
1708 
1709 #endif /* BSD_KERNEL_PRIVATE */
1710 #endif /* DRIVERKIT */
1711 
1712 #endif /* !_NET_IF_VAR_PRIVATE_H_ */
1713