1/*
2 * Copyright (c) 2000-2014 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_H_
65#define	_NET_IF_VAR_H_
66
67#include <sys/appleapiopts.h>
68#include <stdint.h>
69#include <sys/types.h>
70#include <sys/time.h>
71#include <sys/queue.h>		/* get TAILQ macros */
72#ifdef KERNEL_PRIVATE
73#include <kern/locks.h>
74#endif /* KERNEL_PRIVATE */
75#ifdef PRIVATE
76#include <net/route.h>
77#endif
78
79#ifdef KERNEL
80#include <net/kpi_interface.h>
81#endif /* KERNEL */
82
83#ifdef __APPLE__
84#define APPLE_IF_FAM_LOOPBACK  1
85#define APPLE_IF_FAM_ETHERNET  2
86#define APPLE_IF_FAM_SLIP      3
87#define APPLE_IF_FAM_TUN       4
88#define APPLE_IF_FAM_VLAN      5
89#define APPLE_IF_FAM_PPP       6
90#define APPLE_IF_FAM_PVC       7
91#define APPLE_IF_FAM_DISC      8
92#define APPLE_IF_FAM_MDECAP    9
93#define APPLE_IF_FAM_GIF       10
94#define APPLE_IF_FAM_FAITH     11	/* deprecated */
95#define APPLE_IF_FAM_STF       12
96#define APPLE_IF_FAM_FIREWIRE  13
97#define APPLE_IF_FAM_BOND      14
98#endif /* __APPLE__ */
99
100/*
101 * 72 was chosen below because it is the size of a TCP/IP
102 * header (40) + the minimum mss (32).
103 */
104#define	IF_MINMTU	72
105#define	IF_MAXMTU	65535
106
107/*
108 * Structures defining a network interface, providing a packet
109 * transport mechanism (ala level 0 of the PUP protocols).
110 *
111 * Each interface accepts output datagrams of a specified maximum
112 * length, and provides higher level routines with input datagrams
113 * received from its medium.
114 *
115 * Output occurs when the routine if_output is called, with three parameters:
116 *	(*ifp->if_output)(ifp, m, dst, rt)
117 * Here m is the mbuf chain to be sent and dst is the destination address.
118 * The output routine encapsulates the supplied datagram if necessary,
119 * and then transmits it on its medium.
120 *
121 * On input, each interface unwraps the data received by it, and either
122 * places it on the input queue of a internetwork datagram routine
123 * and posts the associated software interrupt, or passes the datagram to a raw
124 * packet input routine.
125 *
126 * Routines exist for locating interfaces by their addresses
127 * or for locating a interface on a certain network, as well as more general
128 * routing and gateway routines maintaining information used to locate
129 * interfaces.  These routines live in the files if.c and route.c
130 */
131
132#define	IFNAMSIZ	16
133
134/* This belongs up in socket.h or socketvar.h, depending on how far the
135 *   event bubbles up.
136 */
137
138struct net_event_data {
139	u_int32_t	if_family;
140	u_int32_t	if_unit;
141	char		if_name[IFNAMSIZ];
142};
143
144#if defined(__LP64__)
145#include <sys/_types/_timeval32.h>
146#define IF_DATA_TIMEVAL timeval32
147#else
148#define IF_DATA_TIMEVAL timeval
149#endif
150
151#pragma pack(4)
152
153/*
154 * Structure describing information about an interface
155 * which may be of interest to management entities.
156 */
157struct if_data {
158	/* generic interface information */
159	u_char		ifi_type;	/* ethernet, tokenring, etc */
160	u_char		ifi_typelen;	/* Length of frame type id */
161	u_char		ifi_physical;	/* e.g., AUI, Thinnet, 10base-T, etc */
162	u_char		ifi_addrlen;	/* media address length */
163	u_char		ifi_hdrlen;	/* media header length */
164	u_char		ifi_recvquota;	/* polling quota for receive intrs */
165	u_char		ifi_xmitquota;	/* polling quota for xmit intrs */
166	u_char		ifi_unused1;	/* for future use */
167	u_int32_t	ifi_mtu;	/* maximum transmission unit */
168	u_int32_t	ifi_metric;	/* routing metric (external only) */
169	u_int32_t	ifi_baudrate;	/* linespeed */
170	/* volatile statistics */
171	u_int32_t	ifi_ipackets;	/* packets received on interface */
172	u_int32_t	ifi_ierrors;	/* input errors on interface */
173	u_int32_t	ifi_opackets;	/* packets sent on interface */
174	u_int32_t	ifi_oerrors;	/* output errors on interface */
175	u_int32_t	ifi_collisions;	/* collisions on csma interfaces */
176	u_int32_t	ifi_ibytes;	/* total number of octets received */
177	u_int32_t	ifi_obytes;	/* total number of octets sent */
178	u_int32_t	ifi_imcasts;	/* packets received via multicast */
179	u_int32_t	ifi_omcasts;	/* packets sent via multicast */
180	u_int32_t	ifi_iqdrops;	/* dropped on input, this interface */
181	u_int32_t	ifi_noproto;	/* destined for unsupported protocol */
182	u_int32_t	ifi_recvtiming;	/* usec spent receiving when timing */
183	u_int32_t	ifi_xmittiming;	/* usec spent xmitting when timing */
184	struct IF_DATA_TIMEVAL ifi_lastchange;	/* time of last administrative change */
185	u_int32_t	ifi_unused2;	/* used to be the default_proto */
186	u_int32_t	ifi_hwassist;	/* HW offload capabilities */
187	u_int32_t	ifi_reserved1;	/* for future use */
188	u_int32_t	ifi_reserved2;	/* for future use */
189};
190
191/*
192 * Structure describing information about an interface
193 * which may be of interest to management entities.
194 */
195struct if_data64 {
196	/* generic interface information */
197	u_char		ifi_type;		/* ethernet, tokenring, etc */
198	u_char		ifi_typelen;		/* Length of frame type id */
199	u_char		ifi_physical;		/* e.g., AUI, Thinnet, 10base-T, etc */
200	u_char		ifi_addrlen;		/* media address length */
201	u_char		ifi_hdrlen;		/* media header length */
202	u_char		ifi_recvquota;		/* polling quota for receive intrs */
203	u_char		ifi_xmitquota;		/* polling quota for xmit intrs */
204	u_char		ifi_unused1;		/* for future use */
205	u_int32_t	ifi_mtu;		/* maximum transmission unit */
206	u_int32_t	ifi_metric;		/* routing metric (external only) */
207	u_int64_t	ifi_baudrate;		/* linespeed */
208	/* volatile statistics */
209	u_int64_t	ifi_ipackets;		/* packets received on interface */
210	u_int64_t	ifi_ierrors;		/* input errors on interface */
211	u_int64_t	ifi_opackets;		/* packets sent on interface */
212	u_int64_t	ifi_oerrors;		/* output errors on interface */
213	u_int64_t	ifi_collisions;		/* collisions on csma interfaces */
214	u_int64_t	ifi_ibytes;		/* total number of octets received */
215	u_int64_t	ifi_obytes;		/* total number of octets sent */
216	u_int64_t	ifi_imcasts;		/* packets received via multicast */
217	u_int64_t	ifi_omcasts;		/* packets sent via multicast */
218	u_int64_t	ifi_iqdrops;		/* dropped on input, this interface */
219	u_int64_t	ifi_noproto;		/* destined for unsupported protocol */
220	u_int32_t	ifi_recvtiming;		/* usec spent receiving when timing */
221	u_int32_t	ifi_xmittiming;		/* usec spent xmitting when timing */
222	struct IF_DATA_TIMEVAL ifi_lastchange;	/* time of last administrative change */
223};
224
225#ifdef PRIVATE
226struct if_traffic_class {
227	u_int64_t		ifi_ibepackets;	/* TC_BE packets received on interface */
228	u_int64_t		ifi_ibebytes;	/* TC_BE bytes received on interface */
229	u_int64_t		ifi_obepackets;	/* TC_BE packet sent on interface */
230	u_int64_t		ifi_obebytes;	/* TC_BE bytes sent on interface */
231	u_int64_t		ifi_ibkpackets;	/* TC_BK packets received on interface */
232	u_int64_t		ifi_ibkbytes;	/* TC_BK bytes received on interface */
233	u_int64_t		ifi_obkpackets;	/* TC_BK packet sent on interface */
234	u_int64_t		ifi_obkbytes;	/* TC_BK bytes sent on interface */
235	u_int64_t		ifi_ivipackets;	/* TC_VI packets received on interface */
236	u_int64_t		ifi_ivibytes;	/* TC_VI bytes received on interface */
237	u_int64_t		ifi_ovipackets;	/* TC_VI packets sent on interface */
238	u_int64_t		ifi_ovibytes;	/* TC_VI bytes sent on interface */
239	u_int64_t		ifi_ivopackets;	/* TC_VO packets received on interface */
240	u_int64_t		ifi_ivobytes;	/* TC_VO bytes received on interface */
241	u_int64_t		ifi_ovopackets;	/* TC_VO packets sent on interface */
242	u_int64_t		ifi_ovobytes;	/* TC_VO bytes sent on interface */
243	u_int64_t		ifi_ipvpackets;	/* TC priv packets received on interface */
244	u_int64_t		ifi_ipvbytes;	/* TC priv bytes received on interface */
245	u_int64_t		ifi_opvpackets;	/* TC priv packets sent on interface */
246	u_int64_t		ifi_opvbytes;	/* TC priv bytes sent on interface */
247};
248
249struct if_data_extended {
250	u_int64_t	ifi_alignerrs;	/* unaligned (32-bit) input pkts */
251	u_int64_t	ifi_dt_bytes;	/* Data threshold counter */
252	u_int64_t	ifi_fpackets;	/* forwarded packets on interface */
253	u_int64_t	ifi_fbytes;	/* forwarded bytes on interface */
254	u_int64_t	reserved[12];	/* for future */
255};
256
257struct if_packet_stats {
258	/* TCP */
259	u_int64_t		ifi_tcp_badformat;
260	u_int64_t		ifi_tcp_unspecv6;
261	u_int64_t		ifi_tcp_synfin;
262	u_int64_t		ifi_tcp_badformatipsec;
263	u_int64_t		ifi_tcp_noconnnolist;
264	u_int64_t		ifi_tcp_noconnlist;
265	u_int64_t		ifi_tcp_listbadsyn;
266	u_int64_t		ifi_tcp_icmp6unreach;
267	u_int64_t		ifi_tcp_deprecate6;
268	u_int64_t		ifi_tcp_rstinsynrcv;
269	u_int64_t		ifi_tcp_ooopacket;
270	u_int64_t		ifi_tcp_dospacket;
271	u_int64_t		ifi_tcp_cleanup;
272	u_int64_t		ifi_tcp_synwindow;
273	u_int64_t		reserved[6];
274	/* UDP */
275	u_int64_t		ifi_udp_port_unreach;
276	u_int64_t		ifi_udp_faithprefix;
277	u_int64_t		ifi_udp_port0;
278	u_int64_t		ifi_udp_badlength;
279	u_int64_t		ifi_udp_badchksum;
280	u_int64_t		ifi_udp_badmcast;
281	u_int64_t		ifi_udp_cleanup;
282	u_int64_t		ifi_udp_badipsec;
283	u_int64_t		_reserved[4];
284};
285
286struct if_description {
287	u_int32_t	ifd_maxlen;	/* must be IF_DESCSIZE */
288	u_int32_t	ifd_len;	/* actual ifd_desc length */
289	u_int8_t	*ifd_desc;	/* ptr to desc buffer */
290};
291
292struct if_bandwidths {
293	u_int64_t	eff_bw;		/* effective bandwidth */
294	u_int64_t	max_bw;		/* maximum theoretical bandwidth */
295};
296
297struct if_latencies {
298	u_int64_t	eff_lt;		/* effective latency */
299	u_int64_t	max_lt;		/* maximum theoretical latency */
300};
301
302struct if_rxpoll_stats {
303	u_int32_t	ifi_poll_off_req;	/* total # of POLL_OFF reqs */
304	u_int32_t	ifi_poll_off_err;	/* total # of POLL_OFF errors */
305	u_int32_t	ifi_poll_on_req;	/* total # of POLL_ON reqs */
306	u_int32_t	ifi_poll_on_err;	/* total # of POLL_ON errors */
307
308	u_int32_t	ifi_poll_wakeups_avg;	/* avg # of wakeup reqs */
309	u_int32_t	ifi_poll_wakeups_lowat;	/* wakeups low watermark */
310	u_int32_t	ifi_poll_wakeups_hiwat;	/* wakeups high watermark */
311
312	u_int64_t	ifi_poll_packets;	/* total # of polled packets */
313	u_int32_t	ifi_poll_packets_avg;	/* average polled packets */
314	u_int32_t	ifi_poll_packets_min;	/* smallest polled packets */
315	u_int32_t	ifi_poll_packets_max;	/* largest polled packets */
316	u_int32_t	ifi_poll_packets_lowat;	/* packets low watermark */
317	u_int32_t	ifi_poll_packets_hiwat;	/* packets high watermark */
318
319	u_int64_t	ifi_poll_bytes;		/* total # of polled bytes */
320	u_int32_t	ifi_poll_bytes_avg;	/* average polled bytes */
321	u_int32_t	ifi_poll_bytes_min;	/* smallest polled bytes */
322	u_int32_t	ifi_poll_bytes_max;	/* largest polled bytes */
323	u_int32_t	ifi_poll_bytes_lowat;	/* bytes low watermark */
324	u_int32_t	ifi_poll_bytes_hiwat;	/* bytes high watermark */
325
326	u_int32_t	ifi_poll_packets_limit;	/* max packets per poll call */
327	u_int64_t	ifi_poll_interval_time;	/* poll interval (nsec) */
328};
329#endif /* PRIVATE */
330
331#pragma pack()
332
333/*
334 * Structure defining a queue for a network interface.
335 */
336struct	ifqueue {
337	void	*ifq_head;
338	void	*ifq_tail;
339	int	ifq_len;
340	int	ifq_maxlen;
341	int	ifq_drops;
342};
343
344#ifdef BSD_KERNEL_PRIVATE
345/*
346 * Internal storage of if_data. This is bound to change. Various places in the
347 * stack will translate this data structure in to the externally visible
348 * if_data structure above.  Note that during interface attach time, the
349 * embedded if_data structure in ifnet is cleared, with the exception of
350 * some non-statistics related fields.
351 */
352struct if_data_internal {
353	/* generic interface information */
354	u_char		ifi_type;	/* ethernet, tokenring, etc */
355	u_char		ifi_typelen;	/* Length of frame type id */
356	u_char		ifi_physical;	/* e.g., AUI, Thinnet, 10base-T, etc */
357	u_char		ifi_addrlen;	/* media address length */
358	u_char		ifi_hdrlen;	/* media header length */
359	u_char		ifi_recvquota;	/* polling quota for receive intrs */
360	u_char		ifi_xmitquota;	/* polling quota for xmit intrs */
361	u_char		ifi_unused1;	/* for future use */
362	u_int32_t	ifi_mtu;	/* maximum transmission unit */
363	u_int32_t	ifi_metric;	/* routing metric (external only) */
364	u_int32_t	ifi_baudrate;	/* linespeed */
365	u_int32_t	_pad;
366	/* volatile statistics */
367	u_int64_t	ifi_ipackets;	/* packets received on interface */
368	u_int64_t	ifi_ierrors;	/* input errors on interface */
369	u_int64_t	ifi_opackets;	/* packets sent on interface */
370	u_int64_t	ifi_oerrors;	/* output errors on interface */
371	u_int64_t	ifi_collisions;	/* collisions on csma interfaces */
372	u_int64_t	ifi_ibytes;	/* total number of octets received */
373	u_int64_t	ifi_obytes;	/* total number of octets sent */
374	u_int64_t	ifi_imcasts;	/* packets received via multicast */
375	u_int64_t	ifi_omcasts;	/* packets sent via multicast */
376	u_int64_t	ifi_iqdrops;	/* dropped on input, this interface */
377	u_int64_t	ifi_noproto;	/* destined for unsupported protocol */
378	u_int32_t	ifi_recvtiming;	/* usec spent receiving when timing */
379	u_int32_t	ifi_xmittiming;	/* usec spent xmitting when timing */
380	u_int64_t	ifi_alignerrs;	/* unaligned (32-bit) input pkts */
381	u_int64_t	ifi_dt_bytes;	/* Data threshold counter */
382	u_int64_t	ifi_fpackets;	/* forwarded packets on interface */
383	u_int64_t	ifi_fbytes;	/* forwarded bytes on interface */
384	struct	timeval ifi_lastchange;	/* time of last administrative change */
385	u_int32_t	ifi_hwassist;	/* HW offload capabilities */
386	u_int32_t	ifi_tso_v4_mtu;	/* TCP Segment Offload IPv4 maximum segment size */
387	u_int32_t	ifi_tso_v6_mtu;	/* TCP Segment Offload IPv6 maximum segment size */
388};
389
390#if MEASURE_BW
391/*
392 * Fields per interface to measure perceived bandwidth.
393 */
394struct if_measured_bw {
395	u_int64_t	bw;		/* measured bandwidth in bytes per ms */
396	u_int64_t	bytes;		/* XXX not needed */
397	u_int64_t	ts;		/* XXX not needed */
398	u_int64_t	cur_seq __attribute((aligned(8)));	/* current sequence for marking a packet */
399	u_int64_t	start_ts;	/* time at which a measurement started */
400	u_int64_t	start_seq;	/* sequence at which a measurement should start */
401	u_int64_t	last_seq;	/* last recorded seq */
402	u_int64_t	last_ts;	/* last recorded ts */
403	u_int32_t	flags __attribute__((aligned(4)));		/* flags */
404#define IF_MEASURED_BW_INPROGRESS 0x1
405#define IF_MEASURED_BW_CALCULATION 0x2
406};
407#endif /* MEASURE_BW */
408#endif /* BSD_KERNEL_PRIVATE */
409
410#ifdef PRIVATE
411#define	if_mtu		if_data.ifi_mtu
412#define	if_type		if_data.ifi_type
413#define if_typelen	if_data.ifi_typelen
414#define if_physical	if_data.ifi_physical
415#define	if_addrlen	if_data.ifi_addrlen
416#define	if_hdrlen	if_data.ifi_hdrlen
417#define	if_metric	if_data.ifi_metric
418#define	if_baudrate	if_data.ifi_baudrate
419#define	if_hwassist	if_data.ifi_hwassist
420#define	if_ipackets	if_data.ifi_ipackets
421#define	if_ierrors	if_data.ifi_ierrors
422#define	if_opackets	if_data.ifi_opackets
423#define	if_oerrors	if_data.ifi_oerrors
424#define	if_collisions	if_data.ifi_collisions
425#define	if_ibytes	if_data.ifi_ibytes
426#define	if_obytes	if_data.ifi_obytes
427#define	if_imcasts	if_data.ifi_imcasts
428#define	if_omcasts	if_data.ifi_omcasts
429#define	if_iqdrops	if_data.ifi_iqdrops
430#define	if_noproto	if_data.ifi_noproto
431#define	if_lastchange	if_data.ifi_lastchange
432#define if_recvquota	if_data.ifi_recvquota
433#define	if_xmitquota	if_data.ifi_xmitquota
434#endif /* PRIVATE */
435#ifdef BSD_KERNEL_PRIVATE
436#define	if_tso_v4_mtu	if_data.ifi_tso_v4_mtu
437#define	if_tso_v6_mtu	if_data.ifi_tso_v6_mtu
438#define	if_alignerrs	if_data.ifi_alignerrs
439#define	if_dt_bytes	if_data.ifi_dt_bytes
440#define	if_fpackets	if_data.ifi_fpackets
441#define	if_fbytes	if_data.ifi_fbytes
442#endif /* BSD_KERNEL_PRIVATE */
443
444#ifdef BSD_KERNEL_PRIVATE
445/*
446 * Forward structure declarations for function prototypes [sic].
447 */
448struct proc;
449struct rtentry;
450struct socket;
451struct ifnet_filter;
452struct mbuf;
453struct ifaddr;
454struct tqdummy;
455struct proto_hash_entry;
456struct dlil_threading_info;
457struct tcpstat_local;
458struct udpstat_local;
459#if PF
460struct pfi_kif;
461#endif /* PF */
462
463/* we use TAILQs so that the order of instantiation is preserved in the list */
464TAILQ_HEAD(ifnethead, ifnet);
465TAILQ_HEAD(ifaddrhead, ifaddr);
466TAILQ_HEAD(ifprefixhead, ifprefix);
467LIST_HEAD(ifmultihead, ifmultiaddr);
468TAILQ_HEAD(tailq_head, tqdummy);
469TAILQ_HEAD(ifnet_filter_head, ifnet_filter);
470TAILQ_HEAD(ddesc_head_name, dlil_demux_desc);
471#endif /* BSD_KERNEL_PRIVATE */
472
473#ifdef PRIVATE
474/*
475 * All of the following IF_HWASSIST_* flags are defined in kpi_inteface.h as
476 * IFNET_* flags. These are redefined here as constants to avoid failures to
477 * build user level programs that can not include kpi_interface.h. It is
478 * important to keep this in sync with the definitions in kpi_interface.h.
479 * The corresponding constant for each definition is mentioned in the comment.
480 *
481 * Bottom 16 bits reserved for hardware checksum
482 */
483#define IF_HWASSIST_CSUM_IP		0x0001	/* will csum IP, IFNET_CSUM_IP */
484#define IF_HWASSIST_CSUM_TCP		0x0002	/* will csum TCP, IFNET_CSUM_TCP */
485#define IF_HWASSIST_CSUM_UDP		0x0004	/* will csum UDP, IFNET_CSUM_UDP */
486#define IF_HWASSIST_CSUM_IP_FRAGS	0x0008	/* will csum IP fragments, IFNET_CSUM_FRAGMENT */
487#define IF_HWASSIST_CSUM_FRAGMENT	0x0010	/* will do IP fragmentation, IFNET_IP_FRAGMENT */
488#define IF_HWASSIST_CSUM_TCPIPV6	0x0020	/* will csum TCPv6, IFNET_CSUM_TCPIPV6 */
489#define IF_HWASSIST_CSUM_UDPIPV6	0x0040	/* will csum UDPv6, IFNET_CSUM_UDP */
490#define IF_HWASSIST_CSUM_FRAGMENT_IPV6	0x0080	/* will do IPv6 fragmentation, IFNET_IPV6_FRAGMENT */
491#define IF_HWASSIST_CSUM_PARTIAL	0x1000	/* simple Sum16 computation, IFNET_CSUM_PARTIAL */
492#define IF_HWASSIST_CSUM_MASK		0xffff
493#define IF_HWASSIST_CSUM_FLAGS(hwassist)	((hwassist) & IF_HWASSIST_CSUM_MASK)
494
495/* VLAN support */
496#define IF_HWASSIST_VLAN_TAGGING	0x00010000	/* supports VLAN tagging, IFNET_VLAN_TAGGING */
497#define IF_HWASSIST_VLAN_MTU		0x00020000	/* supports VLAN MTU-sized packet (for software VLAN), IFNET_VLAN_MTU */
498
499/* TCP Segment Offloading support */
500
501#define IF_HWASSIST_TSO_V4		0x00200000	/* will do TCP Segment offload for IPv4, IFNET_TSO_IPV4 */
502#define IF_HWASSIST_TSO_V6		0x00400000	/* will do TCP Segment offload for IPv6, IFNET_TSO_IPV6 */
503#endif /* PRIVATE */
504
505#ifdef PRIVATE
506#define	IFXNAMSIZ	(IFNAMSIZ + 8)	/* external name (name + unit) */
507#endif
508
509#ifdef BSD_KERNEL_PRIVATE
510/*
511 * ifnet is private to BSD portion of kernel
512 */
513#include <sys/mcache.h>
514#include <sys/tree.h>
515#include <netinet/in.h>
516#include <net/if_dl.h>
517#include <net/classq/if_classq.h>
518#include <net/if_types.h>
519
520RB_HEAD(ll_reach_tree, if_llreach);	/* define struct ll_reach_tree */
521
522#define	if_name(ifp)	ifp->if_xname
523/*
524 * Structure defining a network interface.
525 *
526 * (Would like to call this struct ``if'', but C isn't PL/1.)
527 */
528struct ifnet {
529	/*
530	 * Lock (RW or mutex) to protect this data structure (static storage.)
531	 */
532	decl_lck_rw_data(, if_lock);
533	void		*if_softc;	/* pointer to driver state */
534	const char	*if_name;	/* name, e.g. ``en'' or ``lo'' */
535	const char	*if_xname;	/* external name (name + unit) */
536	struct if_description if_desc;	/* extended description */
537	TAILQ_ENTRY(ifnet) if_link;	/* all struct ifnets are chained */
538	TAILQ_ENTRY(ifnet) if_detaching_link; /* list of detaching ifnets */
539
540	decl_lck_mtx_data(, if_ref_lock)
541	u_int32_t	if_refflags;	/* see IFRF flags below */
542	u_int32_t	if_refio;	/* number of io ops to the underlying driver */
543
544#define	if_list		if_link
545	struct ifaddrhead if_addrhead;	/* linked list of addresses per if */
546#define	if_addrlist	if_addrhead
547	struct ifaddr	*if_lladdr;	/* link address (first/permanent) */
548
549	int		if_pcount;	/* number of promiscuous listeners */
550	struct bpf_if	*if_bpf;	/* packet filter structure */
551	u_short		if_index;	/* numeric abbreviation for this if  */
552	short		if_unit;	/* sub-unit for lower level driver */
553	short		if_timer;	/* time 'til if_watchdog called */
554	short		if_flags;	/* up/down, broadcast, etc. */
555	u_int32_t	if_eflags;	/* see <net/if.h> */
556
557	int		if_capabilities;	/* interface features & capabilities */
558	int		if_capenable;		/* enabled features & capabilities */
559
560	void		*if_linkmib;	/* link-type-specific MIB data */
561	size_t		if_linkmiblen;	/* length of above data */
562
563	struct if_data_internal if_data __attribute__((aligned(8)));
564
565	ifnet_family_t		if_family;	/* value assigned by Apple */
566	ifnet_subfamily_t	if_subfamily;	/* value assigned by Apple */
567	uintptr_t		if_family_cookie;
568	ifnet_output_func	if_output;
569	ifnet_pre_enqueue_func	if_pre_enqueue;
570	ifnet_start_func	if_start;
571	ifnet_ctl_func		if_output_ctl;
572	ifnet_input_poll_func	if_input_poll;
573	ifnet_ctl_func		if_input_ctl;
574	ifnet_ioctl_func	if_ioctl;
575	ifnet_set_bpf_tap	if_set_bpf_tap;
576	ifnet_detached_func	if_free;
577	ifnet_demux_func	if_demux;
578	ifnet_event_func	if_event;
579	ifnet_framer_func	if_framer_legacy;
580	ifnet_framer_extended_func if_framer;
581	ifnet_add_proto_func	if_add_proto;
582	ifnet_del_proto_func	if_del_proto;
583	ifnet_check_multi	if_check_multi;
584	struct proto_hash_entry	*if_proto_hash;
585	void			*if_kpi_storage;
586
587	u_int32_t		if_flowhash;	/* interface flow control ID */
588
589	decl_lck_mtx_data(, if_start_lock);
590	u_int32_t		if_start_flags;	/* see IFSF flags below */
591	u_int32_t		if_start_req;
592	u_int32_t		if_start_active; /* output is active */
593	struct timespec		if_start_cycle;	 /* restart interval */
594	struct thread		*if_start_thread;
595
596	struct ifclassq		if_snd;		/* transmit queue */
597	u_int32_t		if_output_sched_model;	/* tx sched model */
598
599	struct if_bandwidths	if_output_bw;
600	struct if_bandwidths	if_input_bw;
601
602	struct if_latencies	if_output_lt;
603	struct if_latencies	if_input_lt;
604
605	decl_lck_mtx_data(, if_flt_lock)
606	u_int32_t		if_flt_busy;
607	u_int32_t		if_flt_waiters;
608	struct ifnet_filter_head if_flt_head;
609
610	struct ifmultihead	if_multiaddrs;	/* multicast addresses */
611	u_int32_t		if_updatemcasts; /* mcast addrs need updating */
612	int			if_amcount;	/* # of all-multicast reqs */
613	decl_lck_mtx_data(, if_addrconfig_lock); /* for serializing addr config */
614	struct in_multi		*if_allhostsinm; /* store all-hosts inm for this ifp */
615
616	decl_lck_mtx_data(, if_poll_lock);
617	u_int16_t		if_poll_req;
618	u_int16_t		if_poll_update;	/* link update */
619	u_int32_t		if_poll_active;	/* polling is active */
620	struct timespec		if_poll_cycle;  /* poll interval */
621	struct thread		*if_poll_thread;
622
623	struct dlil_threading_info *if_inp;
624
625	struct	ifprefixhead	if_prefixhead;	/* list of prefixes per if */
626	struct {
627		u_int32_t	length;
628		union {
629			u_char	buffer[8];
630			u_char	*ptr;
631		} u;
632	} if_broadcast;
633#if CONFIG_MACF_NET
634	struct label		*if_label;	/* interface MAC label */
635#endif
636
637	u_int32_t		if_wake_properties;
638#if PF
639	struct pfi_kif		*if_pf_kif;
640#endif /* PF */
641
642	decl_lck_mtx_data(, if_cached_route_lock);
643	u_int32_t		if_fwd_cacheok;
644	struct route		if_fwd_route;	/* cached forwarding route */
645	struct route		if_src_route;	/* cached ipv4 source route */
646	struct route_in6	if_src_route6;	/* cached ipv6 source route */
647
648	decl_lck_rw_data(, if_llreach_lock);
649	struct ll_reach_tree	if_ll_srcs;	/* source link-layer tree */
650
651	void			*if_bridge;	/* bridge glue */
652
653	u_int32_t		if_want_aggressive_drain;
654	u_int32_t		if_idle_flags;	/* idle flags */
655	u_int32_t		if_idle_new_flags; /* temporary idle flags */
656	u_int32_t		if_idle_new_flags_mask; /* temporary mask */
657	u_int32_t		if_route_refcnt; /* idle: route ref count */
658
659	struct if_traffic_class if_tc __attribute__((aligned(8)));
660#if INET
661	struct igmp_ifinfo	*if_igi;	/* for IGMPv3 */
662#endif /* INET */
663#if INET6
664	struct mld_ifinfo	*if_mli;	/* for MLDv2 */
665#endif /* INET6 */
666
667	int			if_lqm;		/* link quality metric */
668#if MEASURE_BW
669	struct if_measured_bw	if_bw;
670#endif /* MEASURE_BW */
671	struct tcpstat_local	*if_tcp_stat;	/* TCP specific stats */
672	struct udpstat_local	*if_udp_stat;	/* UDP specific stats */
673
674	struct {
675		int32_t		level;		/* cached logging level */
676		u_int32_t	flags;		/* cached logging flags */
677		int32_t		category;	/* cached category */
678		int32_t		subcategory;	/* cached subcategory */
679	} if_log;
680
681	struct {
682		struct ifnet	*ifp;		/* delegated ifp */
683		u_int32_t	type;		/* delegated i/f type */
684		u_int32_t	family;		/* delegated i/f family */
685		u_int32_t	subfamily;	/* delegated i/f sub-family */
686		uint32_t	expensive:1;	/* delegated i/f expensive? */
687	} if_delegated;
688
689	u_int64_t		if_data_threshold;
690	u_int32_t		if_fg_sendts;	/* last send on a fg socket in seconds */
691
692#if INET6
693	decl_lck_rw_data(, if_inet6data_lock);
694	void			*if_inet6data;
695#endif
696};
697
698#define	IF_TCP_STATINC(_ifp, _s) do {					\
699	if ((_ifp)->if_tcp_stat != NULL)				\
700		atomic_add_64(&(_ifp)->if_tcp_stat->_s, 1);		\
701} while (0);
702
703#define	IF_UDP_STATINC(_ifp, _s) do {					\
704	if ((_ifp)->if_udp_stat != NULL)				\
705		atomic_add_64(&(_ifp)->if_udp_stat->_s, 1);		\
706} while (0);
707
708/*
709 * Valid values for if_refflags
710 */
711#define	IFRF_ATTACHED	0x1	/* ifnet attach is completely done */
712#define	IFRF_DETACHING	0x2	/* detach has been requested */
713
714/*
715 * Valid values for if_start_flags
716 */
717#define	IFSF_FLOW_CONTROLLED	0x1	/* flow controlled */
718
719/*
720 * Structure describing a `cloning' interface.
721 */
722struct if_clone {
723	LIST_ENTRY(if_clone) ifc_list;	/* on list of cloners */
724	const char	*ifc_name;	/* name of device, e.g. `vlan' */
725	size_t		ifc_namelen;	/* length of name */
726	u_int32_t	ifc_minifs;	/* minimum number of interfaces */
727	u_int32_t	ifc_maxunit;	/* maximum unit number */
728	unsigned char	*ifc_units;	/* bitmap to handle units */
729	u_int32_t	ifc_bmlen;	/* bitmap length */
730
731	int		(*ifc_create)(struct if_clone *, u_int32_t, void *);
732	int		(*ifc_destroy)(struct ifnet *);
733};
734
735#define IF_CLONE_INITIALIZER(name, create, destroy, minifs, maxunit) {	      \
736	{ NULL, NULL }, name, (sizeof (name) - 1), minifs, maxunit, NULL, 0,  \
737	create, destroy							      \
738}
739
740#define M_CLONE         M_IFADDR
741
742/*
743 * Macros to manipulate ifqueue.  Users of these macros are responsible
744 * for serialization, by holding whatever lock is appropriate for the
745 * corresponding structure that is referring the ifqueue.
746 */
747#define	IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
748#define	IF_DROP(ifq)		((ifq)->ifq_drops++)
749
750#define	IF_ENQUEUE(ifq, m) do {						\
751	(m)->m_nextpkt = NULL;						\
752	if ((ifq)->ifq_tail == NULL)					\
753		(ifq)->ifq_head = m;					\
754	else								\
755		((struct mbuf*)(ifq)->ifq_tail)->m_nextpkt = m;		\
756	(ifq)->ifq_tail = m;						\
757	(ifq)->ifq_len++;						\
758} while (0)
759
760#define	IF_PREPEND(ifq, m) do {						\
761	(m)->m_nextpkt = (ifq)->ifq_head;				\
762	if ((ifq)->ifq_tail == NULL)					\
763		(ifq)->ifq_tail = (m);					\
764	(ifq)->ifq_head = (m);						\
765	(ifq)->ifq_len++;						\
766} while (0)
767
768#define	IF_DEQUEUE(ifq, m) do {						\
769	(m) = (ifq)->ifq_head;						\
770	if (m != NULL) {						\
771		if (((ifq)->ifq_head = (m)->m_nextpkt) == NULL)		\
772			(ifq)->ifq_tail = NULL;				\
773		(m)->m_nextpkt = NULL;					\
774		(ifq)->ifq_len--;					\
775	}								\
776} while (0)
777
778#define	IF_REMQUEUE(ifq, m) do {					\
779	struct mbuf *_p = (ifq)->ifq_head;				\
780	struct mbuf *_n = (m)->m_nextpkt;				\
781	if ((m) == _p)							\
782		_p = NULL;						\
783	while (_p != NULL) {						\
784		if (_p->m_nextpkt == (m))				\
785			break;						\
786		_p = _p->m_nextpkt;					\
787	}								\
788	VERIFY(_p != NULL || ((m) == (ifq)->ifq_head));			\
789	if ((m) == (ifq)->ifq_head)					\
790		(ifq)->ifq_head = _n;					\
791	if ((m) == (ifq)->ifq_tail)					\
792		(ifq)->ifq_tail = _p;					\
793	VERIFY((ifq)->ifq_tail != NULL || (ifq)->ifq_head == NULL);	\
794	VERIFY((ifq)->ifq_len != 0);					\
795	--(ifq)->ifq_len;						\
796	if (_p != NULL)							\
797		_p->m_nextpkt = _n;					\
798	(m)->m_nextpkt = NULL;						\
799} while (0)
800
801#define IF_DRAIN(ifq) do {						\
802	struct mbuf *_m;						\
803	for (;;) {							\
804		IF_DEQUEUE(ifq, _m);					\
805		if (_m == NULL)						\
806			break;						\
807		m_freem(_m);						\
808	}								\
809} while (0)
810
811/*
812 * The ifaddr structure contains information about one address
813 * of an interface.  They are maintained by the different address families,
814 * are allocated and attached when an address is set, and are linked
815 * together so all addresses for an interface can be located.
816 */
817struct ifaddr {
818	decl_lck_mtx_data(, ifa_lock);	/* lock for ifaddr */
819	uint32_t	ifa_refcnt;	/* ref count, use IFA_{ADD,REM}REF */
820	uint32_t	ifa_debug;	/* debug flags */
821	struct sockaddr	*ifa_addr;	/* address of interface */
822	struct sockaddr	*ifa_dstaddr;	/* other end of p-to-p link */
823#define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
824	struct sockaddr	*ifa_netmask;	/* used to determine subnet */
825	struct ifnet	*ifa_ifp;	/* back-pointer to interface */
826	TAILQ_ENTRY(ifaddr) ifa_link;	/* queue macro glue */
827	void (*ifa_rtrequest)		/* check or clean routes (+ or -)'d */
828	    (int, struct rtentry *, struct sockaddr *);
829	uint32_t	ifa_flags;	/* mostly rt_flags for cloning */
830	int32_t		ifa_metric;	/* cost of going out this interface */
831	void (*ifa_free)(struct ifaddr *); /* callback fn for freeing */
832	void (*ifa_trace)		/* callback fn for tracing refs */
833	    (struct ifaddr *, int);
834	void (*ifa_attached)(struct ifaddr *); /* callback fn for attaching */
835	void (*ifa_detached)(struct ifaddr *); /* callback fn for detaching */
836};
837
838/*
839 * Valid values for ifa_flags
840 */
841#define	IFA_ROUTE	RTF_UP		/* route installed (0x1) */
842#define	IFA_CLONING	RTF_CLONING	/* (0x100) */
843
844/*
845 * Valid values for ifa_debug
846 */
847#define	IFD_ATTACHED	0x1		/* attached to list */
848#define	IFD_ALLOC	0x2		/* dynamically allocated */
849#define	IFD_DEBUG	0x4		/* has debugging info */
850#define	IFD_LINK	0x8		/* link address */
851#define	IFD_TRASHED	0x10		/* in trash list */
852#define	IFD_SKIP	0x20		/* skip this entry */
853#define	IFD_NOTREADY	0x40		/* embryonic; not yet ready */
854
855#define	IFA_LOCK_ASSERT_HELD(_ifa)					\
856	lck_mtx_assert(&(_ifa)->ifa_lock, LCK_MTX_ASSERT_OWNED)
857
858#define	IFA_LOCK_ASSERT_NOTHELD(_ifa)					\
859	lck_mtx_assert(&(_ifa)->ifa_lock, LCK_MTX_ASSERT_NOTOWNED)
860
861#define	IFA_LOCK(_ifa)							\
862	lck_mtx_lock(&(_ifa)->ifa_lock)
863
864#define	IFA_LOCK_SPIN(_ifa)						\
865	lck_mtx_lock_spin(&(_ifa)->ifa_lock)
866
867#define	IFA_CONVERT_LOCK(_ifa) do {					\
868	IFA_LOCK_ASSERT_HELD(_ifa);					\
869	lck_mtx_convert_spin(&(_ifa)->ifa_lock);			\
870} while (0)
871
872#define	IFA_UNLOCK(_ifa)						\
873	lck_mtx_unlock(&(_ifa)->ifa_lock)
874
875#define	IFA_ADDREF(_ifa)						\
876	ifa_addref(_ifa, 0)
877
878#define	IFA_ADDREF_LOCKED(_ifa)						\
879	ifa_addref(_ifa, 1)
880
881#define	IFA_REMREF(_ifa) do {						\
882	(void) ifa_remref(_ifa, 0);					\
883} while (0)
884
885#define	IFA_REMREF_LOCKED(_ifa)						\
886	ifa_remref(_ifa, 1)
887
888/*
889 * The prefix structure contains information about one prefix
890 * of an interface.  They are maintained by the different address families,
891 * are allocated and attached when an prefix or an address is set,
892 * and are linked together so all prefixes for an interface can be located.
893 */
894struct ifprefix {
895	struct	sockaddr *ifpr_prefix;	/* prefix of interface */
896	struct	ifnet *ifpr_ifp;	/* back-pointer to interface */
897	TAILQ_ENTRY(ifprefix) ifpr_list; /* queue macro glue */
898	u_char	ifpr_plen;		/* prefix length in bits */
899	u_char	ifpr_type;		/* protocol dependent prefix type */
900};
901
902/*
903 * Multicast address structure.  This is analogous to the ifaddr
904 * structure except that it keeps track of multicast addresses.
905 * Also, the request count here is a count of requests for this
906 * address, not a count of pointers to this structure; anonymous
907 * membership(s) holds one outstanding request count.
908 */
909struct ifmultiaddr {
910	decl_lck_mtx_data(, ifma_lock);
911	u_int32_t ifma_refcount;	/* reference count */
912	u_int32_t ifma_anoncnt;		/* # of anonymous requests */
913	u_int32_t ifma_reqcnt;		/* total requests for this address */
914	u_int32_t ifma_debug;		/* see ifa_debug flags */
915	u_int32_t ifma_flags;		/* see below */
916	LIST_ENTRY(ifmultiaddr) ifma_link; /* queue macro glue */
917	struct sockaddr *ifma_addr;	/* address this membership is for */
918	struct ifmultiaddr *ifma_ll;	/* link-layer translation, if any */
919	struct ifnet *ifma_ifp;		/* back-pointer to interface */
920	void *ifma_protospec;		/* protocol-specific state, if any */
921	void (*ifma_trace)		/* callback fn for tracing refs */
922	    (struct ifmultiaddr *, int);
923};
924
925/*
926 * Values for ifma_flags
927 */
928#define	IFMAF_ANONYMOUS		0x1	/* has anonymous request ref(s) held */
929
930#define	IFMA_LOCK_ASSERT_HELD(_ifma)					\
931	lck_mtx_assert(&(_ifma)->ifma_lock, LCK_MTX_ASSERT_OWNED)
932
933#define	IFMA_LOCK_ASSERT_NOTHELD(_ifma)					\
934	lck_mtx_assert(&(_ifma)->ifma_lock, LCK_MTX_ASSERT_NOTOWNED)
935
936#define	IFMA_LOCK(_ifma)						\
937	lck_mtx_lock(&(_ifma)->ifma_lock)
938
939#define	IFMA_LOCK_SPIN(_ifma)						\
940	lck_mtx_lock_spin(&(_ifma)->ifma_lock)
941
942#define	IFMA_CONVERT_LOCK(_ifma) do {					\
943	IFMA_LOCK_ASSERT_HELD(_ifma);					\
944	lck_mtx_convert_spin(&(_ifma)->ifma_lock);			\
945} while (0)
946
947#define	IFMA_UNLOCK(_ifma)						\
948	lck_mtx_unlock(&(_ifma)->ifma_lock)
949
950#define	IFMA_ADDREF(_ifma)						\
951	ifma_addref(_ifma, 0)
952
953#define	IFMA_ADDREF_LOCKED(_ifma)					\
954	ifma_addref(_ifma, 1)
955
956#define	IFMA_REMREF(_ifma)						\
957	ifma_remref(_ifma)
958
959/*
960 * Indicate whether or not the immediate interface, or the interface delegated
961 * by it, is a cellular interface (IFT_CELLULAR).  Delegated interface type is
962 * set/cleared along with the delegated ifp; we cache the type for performance
963 * to avoid dereferencing delegated ifp each time.
964 *
965 * Note that this is meant to be used only for accounting and policy purposes;
966 * certain places need to explicitly know the immediate interface type, and
967 * this macro should not be used there.
968 *
969 * The test is done against IFT_CELLULAR instead of IFNET_FAMILY_CELLULAR to
970 * handle certain cases where the family isn't set to the latter.
971 */
972#define	IFNET_IS_CELLULAR(_ifp)						\
973	((_ifp)->if_type == IFT_CELLULAR ||				\
974	(_ifp)->if_delegated.type == IFT_CELLULAR)
975
976/*
977 * Indicate whether or not the immediate interface, or the interface delegated
978 * by it, is a Wi-Fi interface (IFNET_SUBFAMILY_WIFI).  Delegated interface
979 * subfamily is set/cleared along with the delegated ifp; we cache the subfamily
980 * for performance to avoid dereferencing delegated ifp each time.
981 *
982 * Note that this is meant to be used only for accounting and policy purposes;
983 * certain places need to explicitly know the immediate interface type, and
984 * this macro should not be used there.
985 *
986 * The test is done against IFNET_SUBFAMILY_WIFI as the family may be set to
987 * IFNET_FAMILY_ETHERNET (as well as type to IFT_ETHER) which is too generic.
988 */
989#define	IFNET_IS_WIFI(_ifp)						\
990	((_ifp)->if_subfamily == IFNET_SUBFAMILY_WIFI ||		\
991	(_ifp)->if_delegated.subfamily == IFNET_SUBFAMILY_WIFI)
992
993/*
994 * Indicate whether or not the immediate interface, or the interface delegated
995 * by it, is a Wired interface (several families).  Delegated interface
996 * family is set/cleared along with the delegated ifp; we cache the family
997 * for performance to avoid dereferencing delegated ifp each time.
998 *
999 * Note that this is meant to be used only for accounting and policy purposes;
1000 * certain places need to explicitly know the immediate interface type, and
1001 * this macro should not be used there.
1002 */
1003#define	IFNET_IS_WIRED(_ifp)						\
1004	((_ifp)->if_family == IFNET_FAMILY_ETHERNET ||			\
1005	(_ifp)->if_delegated.family == IFNET_FAMILY_ETHERNET ||		\
1006	(_ifp)->if_family == IFNET_FAMILY_FIREWIRE ||			\
1007	(_ifp)->if_delegated.family == IFNET_FAMILY_FIREWIRE)
1008
1009/*
1010 * Indicate whether or not the immediate interface, or the interface delegated
1011 * by it, is marked as expensive.  The delegated interface is set/cleared
1012 * along with the delegated ifp; we cache the flag for performance to avoid
1013 * dereferencing delegated ifp each time.
1014 *
1015 * Note that this is meant to be used only for policy purposes.
1016 */
1017#define	IFNET_IS_EXPENSIVE(_ifp)					\
1018	((_ifp)->if_eflags & IFEF_EXPENSIVE ||				\
1019	(_ifp)->if_delegated.expensive)
1020
1021/*
1022 * We don't support AWDL interface delegation.
1023 */
1024#define	IFNET_IS_AWDL_RESTRICTED(_ifp)					\
1025	(((_ifp)->if_eflags & (IFEF_AWDL|IFEF_AWDL_RESTRICTED)) == 	\
1026	    (IFEF_AWDL|IFEF_AWDL_RESTRICTED))
1027
1028
1029extern struct ifnethead ifnet_head;
1030extern struct ifnet **ifindex2ifnet;
1031extern u_int32_t if_sndq_maxlen;
1032extern u_int32_t if_rcvq_maxlen;
1033extern int if_index;
1034extern struct ifaddr **ifnet_addrs;
1035extern lck_attr_t *ifa_mtx_attr;
1036extern lck_grp_t *ifa_mtx_grp;
1037extern lck_grp_t *ifnet_lock_group;
1038extern lck_attr_t *ifnet_lock_attr;
1039extern ifnet_t lo_ifp;
1040extern uint32_t if_bw_measure_size;
1041extern u_int32_t if_bw_smoothing_val;
1042
1043extern int if_addmulti(struct ifnet *, const struct sockaddr *,
1044    struct ifmultiaddr **);
1045extern int if_addmulti_anon(struct ifnet *, const struct sockaddr *,
1046    struct ifmultiaddr **);
1047extern int if_allmulti(struct ifnet *, int);
1048extern int if_delmulti(struct ifnet *, const struct sockaddr *);
1049extern int if_delmulti_ifma(struct ifmultiaddr *);
1050extern int if_delmulti_anon(struct ifnet *, const struct sockaddr *);
1051extern void if_down(struct ifnet *);
1052extern int if_down_all(void);
1053extern void if_up(struct ifnet *);
1054__private_extern__ void if_updown(struct ifnet *ifp, int up);
1055extern int ifioctl(struct socket *, u_long, caddr_t, struct proc *);
1056extern int ifioctllocked(struct socket *, u_long, caddr_t, struct proc *);
1057extern struct ifnet *ifunit(const char *);
1058extern struct ifnet *if_withname(struct sockaddr *);
1059extern void if_qflush(struct ifnet *, int);
1060extern void if_qflush_sc(struct ifnet *, mbuf_svc_class_t, u_int32_t,
1061    u_int32_t *, u_int32_t *, int);
1062
1063extern struct if_clone *if_clone_lookup(const char *, u_int32_t *);
1064extern int if_clone_attach(struct if_clone *);
1065extern void if_clone_detach(struct if_clone *);
1066
1067extern errno_t if_mcasts_update(struct ifnet *);
1068
1069typedef enum {
1070	IFNET_LCK_ASSERT_EXCLUSIVE,	/* RW: held as writer */
1071	IFNET_LCK_ASSERT_SHARED,	/* RW: held as reader */
1072	IFNET_LCK_ASSERT_OWNED,		/* RW: writer/reader, MTX: held */
1073	IFNET_LCK_ASSERT_NOTOWNED	/* not held */
1074} ifnet_lock_assert_t;
1075
1076#define	IF_LLADDR(_ifp)	\
1077	(LLADDR(SDL(((_ifp)->if_lladdr)->ifa_addr)))
1078
1079__private_extern__ void ifnet_lock_assert(struct ifnet *, ifnet_lock_assert_t);
1080__private_extern__ void ifnet_lock_shared(struct ifnet *ifp);
1081__private_extern__ void ifnet_lock_exclusive(struct ifnet *ifp);
1082__private_extern__ void ifnet_lock_done(struct ifnet *ifp);
1083
1084#if INET6
1085__private_extern__ void if_inet6data_lock_shared(struct ifnet *ifp);
1086__private_extern__ void if_inet6data_lock_exclusive(struct ifnet *ifp);
1087__private_extern__ void if_inet6data_lock_done(struct ifnet *ifp);
1088#endif
1089
1090__private_extern__ void	ifnet_head_lock_shared(void);
1091__private_extern__ void	ifnet_head_lock_exclusive(void);
1092__private_extern__ void	ifnet_head_done(void);
1093
1094__private_extern__ errno_t ifnet_set_idle_flags_locked(ifnet_t, u_int32_t,
1095    u_int32_t);
1096__private_extern__ int ifnet_is_attached(struct ifnet *, int refio);
1097__private_extern__ void ifnet_decr_iorefcnt(struct ifnet *);
1098__private_extern__ void ifnet_set_start_cycle(struct ifnet *,
1099    struct timespec *);
1100__private_extern__ void ifnet_set_poll_cycle(struct ifnet *,
1101    struct timespec *);
1102
1103__private_extern__ void if_attach_ifa(struct ifnet *, struct ifaddr *);
1104__private_extern__ void if_attach_link_ifa(struct ifnet *, struct ifaddr *);
1105__private_extern__ void if_detach_ifa(struct ifnet *, struct ifaddr *);
1106__private_extern__ void if_detach_link_ifa(struct ifnet *, struct ifaddr *);
1107
1108__private_extern__ void dlil_if_lock(void);
1109__private_extern__ void dlil_if_unlock(void);
1110__private_extern__ void dlil_if_lock_assert(void);
1111
1112extern struct ifaddr *ifa_ifwithaddr(const struct sockaddr *);
1113extern struct ifaddr *ifa_ifwithaddr_scoped(const struct sockaddr *,
1114    unsigned int);
1115extern struct ifaddr *ifa_ifwithdstaddr(const struct sockaddr *);
1116extern struct ifaddr *ifa_ifwithnet(const struct sockaddr *);
1117extern struct ifaddr *ifa_ifwithnet_scoped(const struct sockaddr *,
1118    unsigned int);
1119extern struct ifaddr *ifa_ifwithroute(int, const struct sockaddr *,
1120    const struct sockaddr *);
1121extern struct	ifaddr *ifa_ifwithroute_locked(int, const struct sockaddr *,
1122    const struct sockaddr *);
1123extern struct ifaddr *ifa_ifwithroute_scoped_locked(int,
1124    const struct sockaddr *, const struct sockaddr *, unsigned int);
1125extern struct ifaddr *ifaof_ifpforaddr(const struct sockaddr *, struct ifnet *);
1126__private_extern__ struct ifaddr *ifa_ifpgetprimary(struct ifnet *, int);
1127extern void ifa_addref(struct ifaddr *, int);
1128extern struct ifaddr *ifa_remref(struct ifaddr *, int);
1129extern void ifa_lock_init(struct ifaddr *);
1130extern void ifa_lock_destroy(struct ifaddr *);
1131extern void ifma_addref(struct ifmultiaddr *, int);
1132extern void ifma_remref(struct ifmultiaddr *);
1133
1134extern void ifa_init(void);
1135
1136__private_extern__ struct in_ifaddr *ifa_foraddr(unsigned int);
1137__private_extern__ struct in_ifaddr *ifa_foraddr_scoped(unsigned int,
1138    unsigned int);
1139
1140struct ifreq;
1141extern errno_t ifnet_getset_opportunistic(struct ifnet *, u_long,
1142    struct ifreq *, struct proc *);
1143extern int ifnet_get_throttle(struct ifnet *, u_int32_t *);
1144extern int ifnet_set_throttle(struct ifnet *, u_int32_t);
1145extern errno_t ifnet_getset_log(struct ifnet *, u_long,
1146    struct ifreq *, struct proc *);
1147extern int ifnet_set_log(struct ifnet *, int32_t, uint32_t, int32_t, int32_t);
1148extern int ifnet_get_log(struct ifnet *, int32_t *, uint32_t *, int32_t *,
1149    int32_t *);
1150extern int ifnet_notify_address(struct ifnet *, int);
1151
1152#if INET6
1153struct in6_addr;
1154__private_extern__ struct in6_ifaddr *ifa_foraddr6(struct in6_addr *);
1155__private_extern__ struct in6_ifaddr *ifa_foraddr6_scoped(struct in6_addr *,
1156    unsigned int);
1157#endif /* INET6 */
1158
1159__private_extern__ void if_data_internal_to_if_data(struct ifnet *ifp,
1160    const struct if_data_internal *if_data_int, struct if_data *if_data);
1161__private_extern__ void	if_data_internal_to_if_data64(struct ifnet *ifp,
1162    const struct if_data_internal *if_data_int, struct if_data64 *if_data64);
1163__private_extern__ void	if_copy_traffic_class(struct ifnet *ifp,
1164    struct if_traffic_class *if_tc);
1165__private_extern__ void	if_copy_data_extended(struct ifnet *ifp,
1166    struct if_data_extended *if_de);
1167__private_extern__ void if_copy_packet_stats(struct ifnet *ifp,
1168    struct if_packet_stats *if_ps);
1169__private_extern__ void if_copy_rxpoll_stats(struct ifnet *ifp,
1170    struct if_rxpoll_stats *if_rs);
1171
1172__private_extern__ struct rtentry *ifnet_cached_rtlookup_inet(struct ifnet *,
1173    struct in_addr);
1174#if INET6
1175__private_extern__ struct rtentry *ifnet_cached_rtlookup_inet6(struct ifnet *,
1176    struct in6_addr *);
1177#endif /* INET6 */
1178
1179__private_extern__ void if_lqm_update(struct ifnet *, int32_t);
1180__private_extern__ void ifnet_update_sndq(struct ifclassq *, cqev_t);
1181__private_extern__ void ifnet_update_rcv(struct ifnet *, cqev_t);
1182
1183__private_extern__ void ifnet_flowadv(uint32_t);
1184
1185__private_extern__ errno_t ifnet_set_input_bandwidths(struct ifnet *,
1186    struct if_bandwidths *);
1187__private_extern__ errno_t ifnet_set_output_bandwidths(struct ifnet *,
1188    struct if_bandwidths *, boolean_t);
1189__private_extern__ u_int64_t ifnet_output_linkrate(struct ifnet *);
1190__private_extern__ u_int64_t ifnet_input_linkrate(struct ifnet *);
1191
1192__private_extern__ errno_t ifnet_set_input_latencies(struct ifnet *,
1193    struct if_latencies *);
1194__private_extern__ errno_t ifnet_set_output_latencies(struct ifnet *,
1195    struct if_latencies *, boolean_t);
1196
1197__private_extern__ errno_t ifnet_framer_stub(struct ifnet *, struct mbuf **,
1198    const struct sockaddr *, const char *, const char *, u_int32_t *,
1199    u_int32_t *);
1200#endif /* BSD_KERNEL_PRIVATE */
1201#ifdef XNU_KERNEL_PRIVATE
1202/* for uuid.c */
1203__private_extern__ int uuid_get_ethernet(u_int8_t *);
1204#endif /* XNU_KERNEL_PRIVATE */
1205#endif /* !_NET_IF_VAR_H_ */
1206