1/*
  2 * Copyright (c) 2000-2021 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 *	@(#)if.h	8.1 (Berkeley) 6/10/93
 61 */
 62
 63#ifndef _NET_IF_H_
 64#define _NET_IF_H_
 65
 66#define IF_NAMESIZE     16
 67
 68#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
 69#include <sys/cdefs.h>
 70#include <sys/appleapiopts.h>
 71#ifdef __APPLE__
 72
 73#include <net/if_var.h>
 74#include <net/net_kev.h>
 75#if __has_ptrcheck
 76#include <netinet/in.h>
 77#endif
 78#include <sys/types.h>
 79#include <sys/socket.h>
 80
 81#endif
 82
 83#ifndef IFNAMSIZ
 84#define IFNAMSIZ        IF_NAMESIZE
 85#endif
 86
 87struct if_clonereq {
 88	int     ifcr_total;             /* total cloners (out) */
 89	int     ifcr_count;             /* room for this many in user buffer */
 90	char    *ifcr_buffer;           /* buffer for cloner names */
 91};
 92
 93#define IFF_UP          0x1             /* interface is up */
 94#define IFF_BROADCAST   0x2             /* broadcast address valid */
 95#define IFF_DEBUG       0x4             /* turn on debugging */
 96#define IFF_LOOPBACK    0x8             /* is a loopback net */
 97#define IFF_POINTOPOINT 0x10            /* interface is point-to-point link */
 98#define IFF_NOTRAILERS  0x20            /* obsolete: avoid use of trailers */
 99#define IFF_RUNNING     0x40            /* resources allocated */
100#define IFF_NOARP       0x80            /* no address resolution protocol */
101#define IFF_PROMISC     0x100           /* receive all packets */
102#define IFF_ALLMULTI    0x200           /* receive all multicast packets */
103#define IFF_OACTIVE     0x400           /* transmission in progress */
104#define IFF_SIMPLEX     0x800           /* can't hear own transmissions */
105#define IFF_LINK0       0x1000          /* per link layer defined bit */
106#define IFF_LINK1       0x2000          /* per link layer defined bit */
107#define IFF_LINK2       0x4000          /* per link layer defined bit */
108#define IFF_ALTPHYS     IFF_LINK2       /* use alternate physical connection */
109#define IFF_MULTICAST   0x8000          /* supports multicast */
110
111
112/*
113 * Capabilities that interfaces can advertise.
114 *
115 * struct ifnet.if_capabilities
116 *   contains the optional features & capabilities a particular interface
117 *   supports (not only the driver but also the detected hw revision).
118 *   Capabilities are defined by IFCAP_* below.
119 * struct ifnet.if_capenable
120 *   contains the enabled (either by default or through ifconfig) optional
121 *   features & capabilities on this interface.
122 *   Capabilities are defined by IFCAP_* below.
123 * struct if_data.ifi_hwassist in IFNET_* form, defined in net/kpi_interface.h,
124 *   contains the enabled optional features & capabilites that can be used
125 *   individually per packet and are specified in the mbuf pkthdr.csum_flags
126 *   field.  IFCAP_* and IFNET_* do not match one to one and IFNET_* may be
127 *   more detailed or differentiated than IFCAP_*.
128 *   IFNET_* hwassist flags have corresponding CSUM_* in sys/mbuf.h
129 */
130#define IFCAP_RXCSUM            0x00001 /* can offload checksum on RX */
131#define IFCAP_TXCSUM            0x00002 /* can offload checksum on TX */
132#define IFCAP_VLAN_MTU          0x00004 /* VLAN-compatible MTU */
133#define IFCAP_VLAN_HWTAGGING    0x00008 /* hardware VLAN tag support */
134#define IFCAP_JUMBO_MTU         0x00010 /* 9000 byte MTU supported */
135#define IFCAP_TSO4              0x00020 /* can do TCP Segmentation Offload */
136#define IFCAP_TSO6              0x00040 /* can do TCP6 Segmentation Offload */
137#define IFCAP_LRO               0x00080 /* can do Large Receive Offload */
138#define IFCAP_AV                0x00100 /* can do 802.1 AV Bridging */
139#define IFCAP_TXSTATUS          0x00200 /* can return linklevel xmit status */
140#define IFCAP_SKYWALK           0x00400 /* Skywalk mode supported/enabled */
141#define IFCAP_HW_TIMESTAMP      0x00800 /* Time stamping in hardware */
142#define IFCAP_SW_TIMESTAMP      0x01000 /* Time stamping in software */
143#define IFCAP_CSUM_PARTIAL      0x02000 /* can offload partial checksum */
144#define IFCAP_CSUM_ZERO_INVERT  0x04000 /* can invert 0 to -0 (0xffff) */
145#define IFCAP_LRO_NUM_SEG       0x08000 /* NIC & driver can set the num of segments a LRO-packet is built of */
146
147#define IFCAP_HWCSUM    (IFCAP_RXCSUM | IFCAP_TXCSUM)
148#define IFCAP_TSO       (IFCAP_TSO4 | IFCAP_TSO6)
149
150#define IFCAP_VALID (IFCAP_HWCSUM | IFCAP_TSO | IFCAP_LRO | IFCAP_VLAN_MTU | \
151	IFCAP_VLAN_HWTAGGING | IFCAP_JUMBO_MTU | IFCAP_AV | IFCAP_TXSTATUS | \
152	IFCAP_SKYWALK | IFCAP_SW_TIMESTAMP | IFCAP_HW_TIMESTAMP | \
153	IFCAP_CSUM_PARTIAL | IFCAP_CSUM_ZERO_INVERT | IFCAP_LRO_NUM_SEG)
154
155#define IFQ_MAXLEN      128
156#define IFNET_SLOWHZ    1       /* granularity is 1 second */
157/*
158 * Message format for use in obtaining information about interfaces
159 * from sysctl and the routing socket
160 */
161struct if_msghdr {
162	unsigned short  ifm_msglen;     /* to skip non-understood messages */
163	unsigned char   ifm_version;    /* future binary compatability */
164	unsigned char   ifm_type;       /* message type */
165	int             ifm_addrs;      /* like rtm_addrs */
166	int             ifm_flags;      /* value of if_flags */
167	unsigned short  ifm_index;      /* index for associated ifp */
168	struct  if_data ifm_data;       /* statistics and other data about if */
169};
170
171/*
172 * Message format for use in obtaining information about interface addresses
173 * from sysctl and the routing socket
174 */
175struct ifa_msghdr {
176	unsigned short  ifam_msglen;    /* to skip non-understood messages */
177	unsigned char   ifam_version;   /* future binary compatability */
178	unsigned char   ifam_type;      /* message type */
179	int             ifam_addrs;     /* like rtm_addrs */
180	int             ifam_flags;     /* value of ifa_flags */
181	unsigned short  ifam_index;     /* index for associated ifp */
182	int             ifam_metric;    /* value of ifa_metric */
183};
184
185/*
186 * Message format for use in obtaining information about multicast addresses
187 * from the routing socket
188 */
189struct ifma_msghdr {
190	unsigned short  ifmam_msglen;   /* to skip non-understood messages */
191	unsigned char   ifmam_version;  /* future binary compatability */
192	unsigned char   ifmam_type;     /* message type */
193	int             ifmam_addrs;    /* like rtm_addrs */
194	int             ifmam_flags;    /* value of ifa_flags */
195	unsigned short  ifmam_index;    /* index for associated ifp */
196};
197
198/*
199 * Message format for use in obtaining information about interfaces
200 * from sysctl
201 */
202struct if_msghdr2 {
203	u_short ifm_msglen;     /* to skip over non-understood messages */
204	u_char  ifm_version;    /* future binary compatability */
205	u_char  ifm_type;       /* message type */
206	int     ifm_addrs;      /* like rtm_addrs */
207	int     ifm_flags;      /* value of if_flags */
208	u_short ifm_index;      /* index for associated ifp */
209	int     ifm_snd_len;    /* instantaneous length of send queue */
210	int     ifm_snd_maxlen; /* maximum length of send queue */
211	int     ifm_snd_drops;  /* number of drops in send queue */
212	int     ifm_timer;      /* time until if_watchdog called */
213	struct if_data64        ifm_data;       /* statistics and other data */
214};
215
216/*
217 * Message format for use in obtaining information about multicast addresses
218 * from sysctl
219 */
220struct ifma_msghdr2 {
221	u_short ifmam_msglen;   /* to skip over non-understood messages */
222	u_char  ifmam_version;  /* future binary compatability */
223	u_char  ifmam_type;     /* message type */
224	int     ifmam_addrs;    /* like rtm_addrs */
225	int     ifmam_flags;    /* value of ifa_flags */
226	u_short ifmam_index;    /* index for associated ifp */
227	int32_t ifmam_refcount;
228};
229
230/*
231 * ifdevmtu: interface device mtu
232 *    Used with SIOCGIFDEVMTU to get the current mtu in use by the device,
233 *    as well as the minimum and maximum mtu allowed by the device.
234 */
235struct ifdevmtu {
236	int     ifdm_current;
237	int     ifdm_min;
238	int     ifdm_max;
239};
240
241#pragma pack(4)
242
243/*
244 *  ifkpi: interface kpi ioctl
245 *  Used with SIOCSIFKPI and SIOCGIFKPI.
246 *
247 *  ifk_module_id - From in the kernel, a value from kev_vendor_code_find. From
248 *       user space, a value from SIOCGKEVVENDOR ioctl on a kernel event socket.
249 *  ifk_type - The type. Types are specific to each module id.
250 *  ifk_data - The data. ifk_ptr may be a 64bit pointer for 64 bit processes.
251 *
252 *  Copying data between user space and kernel space is done using copyin
253 *  and copyout. A process may be running in 64bit mode. In such a case,
254 *  the pointer will be a 64bit pointer, not a 32bit pointer. The following
255 *  sample is a safe way to copy the data in to the kernel from either a
256 *  32bit or 64bit process:
257 *
258 *  user_addr_t tmp_ptr;
259 *  if (IS_64BIT_PROCESS(current_proc())) {
260 *       tmp_ptr = CAST_USER_ADDR_T(ifkpi.ifk_data.ifk_ptr64);
261 *  }
262 *  else {
263 *       tmp_ptr = CAST_USER_ADDR_T(ifkpi.ifk_data.ifk_ptr);
264 *  }
265 *  error = copyin(tmp_ptr, allocated_dst_buffer, size of allocated_dst_buffer);
266 */
267
268struct ifkpi {
269	unsigned int    ifk_module_id;
270	unsigned int    ifk_type;
271	union {
272		void            *ifk_ptr;
273		int             ifk_value;
274	} ifk_data;
275};
276
277/* Wake capabilities of a interface */
278#define IF_WAKE_ON_MAGIC_PACKET         0x01
279
280
281#pragma pack()
282
283
284/*
285 * Interface request structure used for socket
286 * ioctl's.  All interface ioctl's must have parameter
287 * definitions which begin with ifr_name.  The
288 * remainder may be interface specific.
289 */
290#ifdef IFREQ_OPAQUE
291struct  ifreq;
292#else
293struct  ifreq {
294	char    ifr_name[IFNAMSIZ];             /* if name, e.g. "en0" */
295	union {
296		struct  sockaddr ifru_addr;
297		struct  sockaddr ifru_dstaddr;
298		struct  sockaddr ifru_broadaddr;
299		short   ifru_flags;
300		int     ifru_metric;
301		int     ifru_mtu;
302		int     ifru_phys;
303		int     ifru_media;
304		int     ifru_intval;
305		caddr_t ifru_data;
306		struct  ifdevmtu ifru_devmtu;
307		struct  ifkpi   ifru_kpi;
308		u_int32_t ifru_wake_flags;
309		u_int32_t ifru_route_refcnt;
310		int     ifru_cap[2];
311		u_int32_t ifru_functional_type;
312		u_int32_t ifru_peer_egress_functional_type;
313#define IFRTYPE_FUNCTIONAL_UNKNOWN              0
314#define IFRTYPE_FUNCTIONAL_LOOPBACK             1
315#define IFRTYPE_FUNCTIONAL_WIRED                2
316#define IFRTYPE_FUNCTIONAL_WIFI_INFRA           3
317#define IFRTYPE_FUNCTIONAL_WIFI_AWDL            4
318#define IFRTYPE_FUNCTIONAL_CELLULAR             5
319#define IFRTYPE_FUNCTIONAL_INTCOPROC            6
320#define IFRTYPE_FUNCTIONAL_COMPANIONLINK        7
321#define IFRTYPE_FUNCTIONAL_MANAGEMENT           8
322#define IFRTYPE_FUNCTIONAL_LAST                 8
323		u_int8_t ifru_is_directlink;
324		u_int8_t ifru_is_vpn;
325		u_int8_t ifru_is_companionlink;
326	} ifr_ifru;
327#define ifr_addr        ifr_ifru.ifru_addr      /* address */
328#define ifr_dstaddr     ifr_ifru.ifru_dstaddr   /* other end of p-to-p link */
329#define ifr_broadaddr   ifr_ifru.ifru_broadaddr /* broadcast address */
330#ifdef __APPLE__
331#define ifr_flags       ifr_ifru.ifru_flags     /* flags */
332#else
333#define ifr_flags       ifr_ifru.ifru_flags[0]  /* flags */
334#define ifr_prevflags   ifr_ifru.ifru_flags[1]  /* flags */
335#endif /* __APPLE__ */
336#define ifr_metric      ifr_ifru.ifru_metric    /* metric */
337#define ifr_mtu         ifr_ifru.ifru_mtu       /* mtu */
338#define ifr_phys        ifr_ifru.ifru_phys      /* physical wire */
339#define ifr_media       ifr_ifru.ifru_media     /* physical media */
340#define ifr_data        ifr_ifru.ifru_data      /* for use by interface */
341#define ifr_devmtu      ifr_ifru.ifru_devmtu
342#define ifr_intval      ifr_ifru.ifru_intval    /* integer value */
343#define ifr_kpi         ifr_ifru.ifru_kpi
344#define ifr_wake_flags  ifr_ifru.ifru_wake_flags /* wake capabilities */
345#define ifr_route_refcnt ifr_ifru.ifru_route_refcnt /* route references count */
346#define ifr_reqcap      ifr_ifru.ifru_cap[0]    /* requested capabilities */
347#define ifr_curcap      ifr_ifru.ifru_cap[1]    /* current capabilities */
348};
349
350#define _SIZEOF_ADDR_IFREQ(ifr) \
351	((ifr).ifr_addr.sa_len > sizeof (struct sockaddr) ? \
352	(sizeof (struct ifreq) - sizeof (struct sockaddr) + \
353	(ifr).ifr_addr.sa_len) : sizeof (struct ifreq))
354#endif /* IFREQ_OPAQUE */
355
356struct ifaliasreq {
357	char    ifra_name[IFNAMSIZ];            /* if name, e.g. "en0" */
358#if __has_ptrcheck
359	struct  sockaddr_in ifra_addr;
360	struct  sockaddr_in ifra_broadaddr;
361	struct  sockaddr_in ifra_mask;
362#else
363	struct  sockaddr ifra_addr;
364	struct  sockaddr ifra_broadaddr;
365	struct  sockaddr ifra_mask;
366#endif /* __has_ptrcheck */
367};
368
369struct rslvmulti_req {
370	struct sockaddr *sa;
371	struct sockaddr **llsa;
372};
373
374#pragma pack(4)
375
376struct ifmediareq {
377	char    ifm_name[IFNAMSIZ];     /* if name, e.g. "en0" */
378	int     ifm_current;            /* current media options */
379	int     ifm_mask;               /* don't care mask */
380	int     ifm_status;             /* media status */
381	int     ifm_active;             /* active options */
382	int     ifm_count;              /* # entries in ifm_ulist array */
383	int     *ifm_ulist;             /* media words */
384};
385
386#pragma pack()
387
388
389#pragma pack(4)
390struct  ifdrv {
391	char            ifd_name[IFNAMSIZ];     /* if name, e.g. "en0" */
392	unsigned long   ifd_cmd;
393	size_t          ifd_len;                /* length of ifd_data buffer */
394	void            *ifd_data;
395};
396#pragma pack()
397
398
399/*
400 * Structure used to retrieve aux status data from interfaces.
401 * Kernel suppliers to this interface should respect the formatting
402 * needed by ifconfig(8): each line starts with a TAB and ends with
403 * a newline.
404 */
405
406#define IFSTATMAX       800             /* 10 lines of text */
407struct ifstat {
408	char    ifs_name[IFNAMSIZ];     /* if name, e.g. "en0" */
409	char    ascii[IFSTATMAX + 1];
410};
411
412/*
413 * Structure used in SIOCGIFCONF request.
414 * Used to retrieve interface configuration
415 * for machine (useful for programs which
416 * must know all networks accessible).
417 */
418#pragma pack(4)
419struct  ifconf {
420	int     ifc_len;                /* size of associated buffer */
421	union {
422		caddr_t ifcu_buf;
423		struct  ifreq *ifcu_req;
424	} ifc_ifcu;
425};
426#pragma pack()
427#define ifc_buf ifc_ifcu.ifcu_buf       /* buffer address */
428#define ifc_req ifc_ifcu.ifcu_req       /* array of structures returned */
429
430/*
431 * DLIL KEV_DL_PROTO_ATTACHED/DETACHED structure
432 */
433struct kev_dl_proto_data {
434	struct net_event_data           link_data;
435	u_int32_t                       proto_family;
436	u_int32_t                       proto_remaining_count;
437};
438
439#endif /* (_POSIX_C_SOURCE && !_DARWIN_C_SOURCE) */
440
441struct if_nameindex {
442	unsigned int     if_index;      /* 1, 2, ... */
443	char            *if_name;       /* null terminated name: "le0", ... */
444};
445
446__BEGIN_DECLS
447unsigned int     if_nametoindex(const char *);
448char            *if_indextoname(unsigned int, char *);
449struct           if_nameindex *if_nameindex(void);
450void             if_freenameindex(struct if_nameindex *);
451__END_DECLS
452
453
454
455#endif /* !_NET_IF_H_ */