/*-
* Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by William Studenmund and Jason R. Thorpe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1982, 1986, 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)if.h 8.3 (Berkeley) 2/9/95
*/
/*
* Length of interface external name, including terminating '\0'.
* Note: this is the same size as a generic device's external name.
*/
#define IF_NAMESIZE 16
/*
* Length of interface description, including terminating '\0'.
*/
#define IFDESCRSIZE 64
/*
* Always include ALTQ glue here -- we use the ALTQ interface queue
* structure even when ALTQ is not configured into the kernel so that
* the size of struct ifnet does not changed based on the option. The
* ALTQ queue structure is API-compatible with the legacy ifqueue.
*/
#include <altq/if_altq.h>
/*
* Structures defining a network interface, providing a packet
* transport mechanism (ala level 0 of the PUP protocols).
*
* Each interface accepts output datagrams of a specified maximum
* length, and provides higher level routines with input datagrams
* received from its medium.
*
* Output occurs when the routine if_output is called, with four parameters:
* (*ifp->if_output)(ifp, m, dst, rt)
* Here m is the mbuf chain to be sent and dst is the destination address.
* The output routine encapsulates the supplied datagram if necessary,
* and then transmits it on its medium.
*
* On input, each interface unwraps the data received by it, and either
* places it on the input queue of a internetwork datagram routine
* and posts the associated software interrupt, or passes the datagram to a raw
* packet input routine.
*
* Routines exist for locating interfaces by their addresses
* or for locating a interface on a certain network, as well as more general
* routing and gateway routines maintaining information used to locate
* interfaces. These routines live in the files if.c and route.c
*/
#include <sys/time.h>
/*
* Structure describing a `cloning' interface.
*/
struct if_clone {
LIST_ENTRY(if_clone) ifc_list; /* on list of cloners */
const char *ifc_name; /* name of device, e.g. `gif' */
size_t ifc_namelen; /* length of name */
int (*ifc_create)(struct if_clone *, int);
int (*ifc_destroy)(struct ifnet *);
};
/*
* Structure used to query names of interface cloners.
*/
struct if_clonereq {
int ifcr_total; /* total cloners (out) */
int ifcr_count; /* room for this many in user buffer */
char *ifcr_buffer; /* buffer for cloner names */
};
/*
* Structure defining statistics and other data kept regarding a network
* interface.
*
* Only used for exporting data from the interface.
*/
struct if_data {
/* generic interface information */
u_char ifi_type; /* ethernet, tokenring, etc. */
u_char ifi_addrlen; /* media address length */
u_char ifi_hdrlen; /* media header length */
int ifi_link_state; /* current link state */
uint64_t ifi_mtu; /* maximum transmission unit */
uint64_t ifi_metric; /* routing metric (external only) */
uint64_t ifi_baudrate; /* linespeed */
/* volatile statistics */
uint64_t ifi_ipackets; /* packets received on interface */
uint64_t ifi_ierrors; /* input errors on interface */
uint64_t ifi_opackets; /* packets sent on interface */
uint64_t ifi_oerrors; /* output errors on interface */
uint64_t ifi_collisions; /* collisions on csma interfaces */
uint64_t ifi_ibytes; /* total number of octets received */
uint64_t ifi_obytes; /* total number of octets sent */
uint64_t ifi_imcasts; /* packets received via multicast */
uint64_t ifi_omcasts; /* packets sent via multicast */
uint64_t ifi_iqdrops; /* dropped on input, this interface */
uint64_t ifi_noproto; /* destined for unsupported protocol */
struct timespec ifi_lastchange;/* last operational state change */
};
/*
* Values for if_link_state.
*/
#define LINK_STATE_UNKNOWN 0 /* link invalid/unknown */
#define LINK_STATE_DOWN 1 /* link is down */
#define LINK_STATE_UP 2 /* link is up */
/*
* Status bit descriptions for the various interface types.
*/
struct if_status_description {
unsigned char ifs_type;
unsigned char ifs_state;
const char *ifs_string;
};
/*
* Structure defining a queue for a network interface.
*
* (Would like to call this struct ``if'', but C isn't PL/1.)
*/
TAILQ_HEAD(ifnet_head, ifnet); /* the actual queue head */
/*
* pf specific data, used only when #if NPF > 0.
*/
void *if_pf_kif; /* ?: pf interface abstraction */
void *if_pf_groups; /* ?: pf interface groups */
/*
* During an ifnet's lifetime, it has only one if_index, but
* an if_index is not sufficient to identify an ifnet
* because during the lifetime of the system, many ifnets may occupy a
* given if_index. Let us tell different ifnets at the same
* if_index apart by their if_index_gen, a unique number that each ifnet
* is assigned when it if_attach()s. Now, the kernel can use the
* pair (if_index, if_index_gen) as a weak reference to an ifnet.
*/
uint64_t if_index_gen; /* :: generation number for the ifnet
* at if_index: if two ifnets' index
* and generation number are both the
* same, they are the same ifnet.
*/
struct sysctllog
*if_sysctl_log; /* :: */
int (*if_initaddr) /* :: */
(struct ifnet *, struct ifaddr *, bool);
int (*if_setflags) /* :: */
(struct ifnet *, const u_short);
kmutex_t *if_ioctl_lock; /* :: */
char *if_description; /* i: interface description */
#ifdef _KERNEL /* XXX kvm(3) */
struct if_slowtimo_data *if_slowtimo_data; /* :: */
struct krwlock *if_afdata_lock;/* :: */
struct if_percpuq
*if_percpuq; /* :: we should remove it in the future */
struct work if_link_work; /* q: linkage on link state work queue */
uint16_t if_link_queue; /* q: masked link state change queue */
/* q: is link state work scheduled? */
bool if_link_scheduled;
struct pslist_entry
if_pslist_entry;/* i: */
struct psref_target
if_psref; /* :: */
struct pslist_head
if_addr_pslist; /* i: */
struct if_deferred_start
*if_deferred_start;
/* :: */
/* XXX should be protocol independent */
LIST_HEAD(, in6_multi)
if_multiaddrs; /* 6: */
khook_list_t *if_linkstate_hooks; /* :: */
#endif
} ifnet_t;
#include <net/if_stats.h>
#define if_name(ifp) ((ifp)->if_xname)
#define IFF_UP 0x0001 /* interface is up */
#define IFF_BROADCAST 0x0002 /* broadcast address valid */
#define IFF_DEBUG 0x0004 /* turn on debugging */
#define IFF_LOOPBACK 0x0008 /* is a loopback net */
#define IFF_POINTOPOINT 0x0010 /* interface is point-to-point link */
#if 0
/* 0x0020 was IFF_NOTRAILERS */
#else
/*
* sys/compat/svr4 is removed on 19 Dec 2018.
* And then, IFF_NOTRAILERS itself is removed by if.h:r1.268 on 5 Feb 2019.
*/
#define IFF_UNNUMBERED 0x0020 /* explicit unnumbered */
#endif
#define IFF_RUNNING 0x0040 /* resources allocated */
#define IFF_NOARP 0x0080 /* no address resolution protocol */
#define IFF_PROMISC 0x0100 /* receive all packets */
#define IFF_ALLMULTI 0x0200 /* OBSOLETE -- DO NOT USE */
/*
* IFF_ALLMULTI obsoleted on 2019-05-15 -- existing non-MP-safe drivers
* can use it for themselves under IFNET_LOCK, but they should be
* converted to use ETHER_F_ALLMULTI under ETHER_LOCK instead. For
* compatibility with existing drivers, if_ethersubr and if_arcsubr
* will set IFF_ALLMULTI according to other flags, but you should not
* rely on this.
*/
#define IFF_OACTIVE 0x0400 /* transmission in progress */
#define IFF_SIMPLEX 0x0800 /* can't hear own transmissions */
#define IFF_LINK0 0x1000 /* per link layer defined bit */
#define IFF_LINK1 0x2000 /* per link layer defined bit */
#define IFF_LINK2 0x4000 /* per link layer defined bit */
#define IFF_MULTICAST 0x8000 /* supports multicast */
#define IFEF_MPSAFE __BIT(0) /* handlers can run in parallel (see below) */
/*
* The guidelines for converting an interface to IFEF_MPSAFE are as follows
*
* Enabling IFEF_MPSAFE on an interface suppresses taking KERNEL_LOCK when
* calling the following handlers:
* - if_start
* - Note that if_transmit is always called without KERNEL_LOCK
* - if_output
* - if_ioctl
* - if_init
* - if_stop
*
* This means that an interface with IFEF_MPSAFE must make the above handlers
* MP-safe or take KERNEL_LOCK by itself inside handlers that aren't MP-safe
* yet.
*
* There are some additional restrictions to access member variables of struct
* ifnet:
* - if_flags
* - Must be updated with holding IFNET_LOCK
* - You cannot use the flag in Tx/Rx paths anymore because there is no
* synchronization on the flag except for IFNET_LOCK
* - Note that IFNET_LOCK can't be taken in softint because it's known
* that it causes a deadlock
* - Some synchronization mechanisms such as pserialize_perform are called
* with IFNET_LOCK and also require context switches on every CPUs
* that mean softints finish so trying to take IFNET_LOCK in softint
* might block on IFNET_LOCK and prevent such synchronization mechanisms
* from being completed
* - Currently the deadlock occurs only if NET_MPSAFE is enabled, however,
* we should deal with the restriction because NET_MPSAFE will be enabled
* by default in the future
* - if_watchdog and if_timer
* - The watchdog framework works only for non-IFEF_MPSAFE interfaces
* that rely on KERNEL_LOCK
* - Interfaces with IFEF_MPSAFE have to provide its own watchdog mechanism
* if needed
* - Keep if_watchdog NULL when calling if_attach
*/
#define KERNEL_LOCK_IF_IFP_MPSAFE(ifp) \
do { if (if_is_mpsafe(ifp)) { KERNEL_LOCK(1, NULL); } } while (0)
#define KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp) \
do { if (if_is_mpsafe(ifp)) { KERNEL_UNLOCK_ONE(NULL); } } while (0)
#define KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp) \
do { if (!if_is_mpsafe(ifp)) { KERNEL_LOCK(1, NULL); } } while (0)
#define KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp) \
do { if (!if_is_mpsafe(ifp)) { KERNEL_UNLOCK_ONE(NULL); } } while (0)
/* XXX explore a better place to define */
#ifdef NET_MPSAFE
#define KERNEL_LOCK_UNLESS_NET_MPSAFE() do { } while (0)
#define KERNEL_UNLOCK_UNLESS_NET_MPSAFE() do { } while (0)
#define SOFTNET_LOCK_UNLESS_NET_MPSAFE() do { } while (0)
#define SOFTNET_UNLOCK_UNLESS_NET_MPSAFE() do { } while (0)
#define SOFTNET_LOCK_IF_NET_MPSAFE() \
do { mutex_enter(softnet_lock); } while (0)
#define SOFTNET_UNLOCK_IF_NET_MPSAFE() \
do { mutex_exit(softnet_lock); } while (0)
#else /* NET_MPSAFE */
#define KERNEL_LOCK_UNLESS_NET_MPSAFE() \
do { KERNEL_LOCK(1, NULL); } while (0)
#define KERNEL_UNLOCK_UNLESS_NET_MPSAFE() \
do { KERNEL_UNLOCK_ONE(NULL); } while (0)
#define SOFTNET_LOCK_UNLESS_NET_MPSAFE() \
do { mutex_enter(softnet_lock); } while (0)
#define SOFTNET_UNLOCK_UNLESS_NET_MPSAFE() \
do { mutex_exit(softnet_lock); } while (0)
#define SOFTNET_LOCK_IF_NET_MPSAFE() do { } while (0)
#define SOFTNET_UNLOCK_IF_NET_MPSAFE() do { } while (0)
#endif /* NET_MPSAFE */
#define SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE() \
do { \
SOFTNET_LOCK_UNLESS_NET_MPSAFE(); \
KERNEL_LOCK_UNLESS_NET_MPSAFE(); \
} while (0)
#define SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE() \
do { \
KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); \
SOFTNET_UNLOCK_UNLESS_NET_MPSAFE(); \
} while (0)
/* Capabilities that interfaces can advertise. */
/* 0x01 .. 0x40 were previously used */
#define IFCAP_TSOv4 0x00080 /* can do TCPv4 segmentation offload */
#define IFCAP_CSUM_IPv4_Rx 0x00100 /* can do IPv4 header checksums (Rx) */
#define IFCAP_CSUM_IPv4_Tx 0x00200 /* can do IPv4 header checksums (Tx) */
#define IFCAP_CSUM_TCPv4_Rx 0x00400 /* can do IPv4/TCP checksums (Rx) */
#define IFCAP_CSUM_TCPv4_Tx 0x00800 /* can do IPv4/TCP checksums (Tx) */
#define IFCAP_CSUM_UDPv4_Rx 0x01000 /* can do IPv4/UDP checksums (Rx) */
#define IFCAP_CSUM_UDPv4_Tx 0x02000 /* can do IPv4/UDP checksums (Tx) */
#define IFCAP_CSUM_TCPv6_Rx 0x04000 /* can do IPv6/TCP checksums (Rx) */
#define IFCAP_CSUM_TCPv6_Tx 0x08000 /* can do IPv6/TCP checksums (Tx) */
#define IFCAP_CSUM_UDPv6_Rx 0x10000 /* can do IPv6/UDP checksums (Rx) */
#define IFCAP_CSUM_UDPv6_Tx 0x20000 /* can do IPv6/UDP checksums (Tx) */
#define IFCAP_TSOv6 0x40000 /* can do TCPv6 segmentation offload */
#define IFCAP_LRO 0x80000 /* can do Large Receive Offload */
#define IFCAP_MASK 0xfff80 /* currently valid capabilities */
/*
* Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
* input routines have queues of messages stored on ifqueue structures
* (defined above). Entries are added to and deleted from these structures
* by these macros, which should be called with ipl raised to splnet().
*/
#define IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen)
#define IF_DROP(ifq) ((ifq)->ifq_drops++)
#define IF_ENQUEUE(ifq, m) do { \
(m)->m_nextpkt = 0; \
if ((ifq)->ifq_tail == 0) \
(ifq)->ifq_head = m; \
else \
(ifq)->ifq_tail->m_nextpkt = m; \
(ifq)->ifq_tail = m; \
(ifq)->ifq_len++; \
} while (/*CONSTCOND*/0)
#define IF_PREPEND(ifq, m) do { \
(m)->m_nextpkt = (ifq)->ifq_head; \
if ((ifq)->ifq_tail == 0) \
(ifq)->ifq_tail = (m); \
(ifq)->ifq_head = (m); \
(ifq)->ifq_len++; \
} while (/*CONSTCOND*/0)
#define IF_DEQUEUE(ifq, m) do { \
(m) = (ifq)->ifq_head; \
if (m) { \
if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \
(ifq)->ifq_tail = 0; \
(m)->m_nextpkt = 0; \
(ifq)->ifq_len--; \
} \
} while (/*CONSTCOND*/0)
#define IF_POLL(ifq, m) ((m) = (ifq)->ifq_head)
#define IF_PURGE(ifq) \
do { \
struct mbuf *__m0; \
\
for (;;) { \
IF_DEQUEUE((ifq), __m0); \
if (__m0 == NULL) \
break; \
else \
m_freem(__m0); \
} \
} while (/*CONSTCOND*/ 0)
#define IF_IS_EMPTY(ifq) ((ifq)->ifq_len == 0)
#ifndef IFQ_MAXLEN
#define IFQ_MAXLEN 256
#endif
#define IFNET_SLOWHZ 1 /* granularity is 1 second */
/*
* Structure defining statistics and other data kept regarding an address
* on a network interface.
*/
struct ifaddr_data {
int64_t ifad_inbytes;
int64_t ifad_outbytes;
};
/*
* The ifaddr structure contains information about one address
* of an interface. They are maintained by the different address families,
* are allocated and attached when an address is set, and are linked
* together so all addresses for an interface can be located.
*/
struct ifaddr {
struct sockaddr *ifa_addr; /* address of interface */
struct sockaddr *ifa_dstaddr; /* other end of p-to-p link */
#define ifa_broadaddr ifa_dstaddr /* broadcast address interface */
struct sockaddr *ifa_netmask; /* used to determine subnet */
struct ifnet *ifa_ifp; /* back-pointer to interface */
TAILQ_ENTRY(ifaddr) ifa_list; /* list of addresses for interface */
struct ifaddr_data ifa_data; /* statistics on the address */
void (*ifa_rtrequest) /* check or clean routes (+ or -)'d */
(int, struct rtentry *, const struct rt_addrinfo *);
u_int ifa_flags; /* mostly rt_flags for cloning */
int ifa_refcnt; /* count of references */
int ifa_metric; /* cost of going out this interface */
struct ifaddr *(*ifa_getifa)(struct ifaddr *,
const struct sockaddr *);
uint32_t *ifa_seqno;
int16_t ifa_preference; /* preference level for this address */
#ifdef _KERNEL
struct pslist_entry ifa_pslist_entry;
struct psref_target ifa_psref;
#endif
};
#define IFA_ROUTE RTF_UP /* (0x01) route installed */
#define IFA_DESTROYING 0x2
/*
* Message format for use in obtaining information about interfaces from
* sysctl and the routing socket. We need to force 64-bit alignment if we
* aren't using compatibility definitions.
*/
#if !defined(_KERNEL) || !defined(COMPAT_RTSOCK)
#define __align64 __aligned(sizeof(uint64_t))
#else
#define __align64
#endif
struct if_msghdr {
u_short ifm_msglen __align64;
/* to skip over non-understood messages */
u_char ifm_version; /* future binary compatibility */
u_char ifm_type; /* message type */
int ifm_addrs; /* like rtm_addrs */
int ifm_flags; /* value of if_flags */
u_short ifm_index; /* index for associated ifp */
struct if_data ifm_data __align64;
/* statistics and other data about if */
};
/*
* Message format for use in obtaining information about interface addresses
* from sysctl and the routing socket.
*/
struct ifa_msghdr {
u_short ifam_msglen __align64;
/* to skip over non-understood messages */
u_char ifam_version; /* future binary compatibility */
u_char ifam_type; /* message type */
u_short ifam_index; /* index for associated ifp */
int ifam_flags; /* value of ifa_flags */
int ifam_addrs; /* like rtm_addrs */
pid_t ifam_pid; /* identify sender */
int ifam_addrflags; /* family specific address flags */
int ifam_metric; /* value of ifa_metric */
};
/*
* Message format announcing the arrival or departure of a network interface.
*/
struct if_announcemsghdr {
u_short ifan_msglen __align64;
/* to skip over non-understood messages */
u_char ifan_version; /* future binary compatibility */
u_char ifan_type; /* message type */
u_short ifan_index; /* index for associated ifp */
char ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */
u_short ifan_what; /* what type of announcement */
};
struct ifdatareq {
char ifdr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
struct if_data ifdr_data;
};
struct ifmediareq {
char ifm_name[IFNAMSIZ]; /* if name, e.g. "en0" */
int ifm_current; /* IFMWD: current media options */
int ifm_mask; /* IFMWD: don't care mask */
int ifm_status; /* media status */
int ifm_active; /* IFMWD: active options */
int ifm_count; /* # entries in ifm_ulist
array */
int *ifm_ulist; /* array of ifmedia word */
};
struct ifdrv {
char ifd_name[IFNAMSIZ]; /* if name, e.g. "en0" */
unsigned long ifd_cmd;
size_t ifd_len;
void *ifd_data;
};
#define IFLINKSTR_QUERYLEN 0x01
#define IFLINKSTR_UNSET 0x02
/*
* Structure used in SIOCGIFCONF request.
* Used to retrieve interface configuration
* for machine (useful for programs which
* must know all networks accessible).
*/
struct ifconf {
int ifc_len; /* size of associated buffer */
union {
void * ifcu_buf;
struct ifreq *ifcu_req;
} ifc_ifcu;
#define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */
#define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */
};