IFNET(9)
πΈ NAME πΈ
okay so this is the IFNET(9) page, and it is literally the group chat for if_t, ifnet, ifaddr, ifqueue, and if_data β the kernel interfaces for manipulating network interfaces. no cap, this is the one you keep open while your driver is acting up. π
β¨ SYNOPSIS β¨
#include <sys/param.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_types.h>
π«Ά Interface Manipulation Functions π«Ά
if_t
if_alloc(u_char type);
if_t
if_alloc_dev(u_char type, device_t dev);
if_t
if_alloc_domain(u_char type, int numa_domain);
void
if_attach(if_t ifp);
void
if_detach(if_t ifp);
void
if_free(if_t ifp);
void
if_free_type(if_t ifp, u_char type);
void
if_down(if_t ifp);
int
ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td);
int
ifpromisc(if_t ifp, int pswitch);
int
if_allmulti(if_t ifp, int amswitch);
if_t
ifunit(const char *name);
if_t
ifunit_ref(const char *name);
void
if_up(if_t ifp);
π Interface Address Functions π
struct ifaddr *
ifa_ifwithaddr(struct sockaddr *addr);
struct ifaddr *
ifa_ifwithdstaddr(struct sockaddr *addr, int fib);
struct ifaddr *
ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp, int fib);
struct ifaddr *
ifaof_ifpforaddr(struct sockaddr *addr, if_t ifp);
void
ifa_ref(struct ifaddr *ifa);
void
ifa_free(struct ifaddr *ifa);
π¦ Interface Multicast Address Functions π¦
int
if_addmulti(if_t ifp, struct sockaddr *sa, struct ifmultiaddr **ifmap);
int
if_delmulti(if_t ifp, struct sockaddr *sa);
struct ifmultiaddr *
if_findmulti(if_t ifp, struct sockaddr *sa);
π Output queue accessors π
if_dequeue(if_t ifp, struct mbuf *m);
π Output queue macros π
IF_DEQUEUE(struct ifqueue *ifq, struct mbuf *m);
π if_t accesors π
uint64_t
if_setbaudrate(if_t ifp, uint64_t baudrate);
uint64_t
if_getbaudrate(const if_t ifp);
int
if_setcapabilities(if_t ifp, int capabilities);
int
if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit);
int
if_getcapabilities(const if_t ifp);
int
if_togglecapenable(if_t ifp, int togglecap);
int
if_setcapenable(if_t ifp, int capenable);
int
if_setcapenablebit(if_t ifp, int setcap, int clearcap);
int
if_getcapenable(const if_t ifp);
int
if_setcapabilities2(if_t ifp, int capabilities);
int
if_setcapabilities2bit(if_t ifp, int setbit, int clearbit);
int
if_getcapabilities2(const if_t ifp);
int
if_togglecapenable2(if_t ifp, int togglecap);
int
if_setcapenable2(if_t ifp, int capenable);
int
if_setcapenable2bit(if_t ifp, int setcap, int clearcap);
int
if_getcapenable2(const if_t ifp);
int
if_getdunit(const if_t ifp);
int
if_getindex(const if_t ifp);
int
if_getidxgen(const if_t ifp);
const char *
if_getdname(const if_t ifp);
void
if_setdname(if_t ifp, const char *name);
const char *
if_name(if_t ifp);
int
if_setname(if_t ifp, const char *name);
void
if_setdescr(if_t ifp, char *descrbuf);
char *
if_allocdescr(size_t sz, int malloc_flag);
void
if_freedescr(char *descrbuf);
int
if_getalloctype(const if_t ifp);
int
if_gettype(const if_t ifp);
int
if_setdev(if_t ifp, void *dev);
int
if_setdrvflagbits(if_t ifp, int if_setflags, int clear_flags);
int
if_getdrvflags(const if_t ifp);
int
if_setdrvflags(if_t ifp, int flags);
int
if_getlinkstate(if_t ifp);
int
if_clearhwassist(if_t ifp);
int
if_sethwassistbits(if_t ifp, int toset, int toclear);
int
if_sethwassist(if_t ifp, int hwassist_bit);
int
if_gethwassist(const if_t ifp);
int
if_togglehwassist(if_t ifp, int toggle_bits);
int
if_setsoftc(if_t ifp, void *softc);
void *
if_getsoftc(if_t ifp);
void
if_setllsoftc(if_t ifp, void *softc);
void *
if_getllsoftc(if_t ifp);
u_int
if_getfib(if_t ifp);
uint8_t
if_getaddrlen(if_t ifp);
int
if_gethwaddr(const if_t ifp, struct ifreq *);
const uint8_t *
if_getbroadcastaddr(const if_t ifp);
void
if_setbroadcastaddr(if_t ifp, const uint8_t *);
int
if_setmtu(if_t ifp, int mtu);
int
if_getmtu(const if_t ifp);
int
if_getmtu_family(const if_t ifp, int family);
void
if_notifymtu(if_t ifp);
int
if_setflagbits(if_t ifp, int set, int clear);
int
if_setflags(if_t ifp, int flags);
int
if_getflags(const if_t ifp);
int
if_getnumadomain(if_t ifp);
int
if_sendq_empty(if_t ifp);
int
if_setsendqready(if_t ifp);
int
if_setsendqlen(if_t ifp, int tx_desc_count);
int
if_sethwtsomax(if_t ifp, u_int if_hw_tsomax);
int
if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount);
int
if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize);
u_int
if_gethwtsomax(const if_t ifp);
u_int
if_gethwtsomaxsegcount(const if_t ifp);
u_int
if_gethwtsomaxsegsize(const if_t ifp);
void
if_setnetmapadapter(if_t ifp, struct netmap_adapter *na);
struct netmap_adapter *
if_getnetmapadapter(if_t ifp);
void
if_input(if_t ifp, struct mbuf* sendmp);
int
if_sendq_prepend(if_t ifp, struct mbuf *m);
struct mbuf *
if_dequeue(if_t ifp);
int
if_setifheaderlen(if_t ifp, int len);
void
if_setrcvif(struct mbuf *m, if_t ifp);
void
if_setvtag(struct mbuf *m, u_int16_t tag);
u_int16_t
if_getvtag(struct mbuf *m);
int
if_vlantrunkinuse(if_t ifp);
caddr_t
if_getlladdr(const if_t ifp);
struct vnet *
if_getvnet(const if_t ifp);
void *
if_gethandle(u_char);
void
if_bpfmtap(if_t ifp, struct mbuf *m);
void
if_etherbpfmtap(if_t ifp, struct mbuf *m);
void
if_vlancap(if_t ifp);
int
if_transmit(if_t ifp, struct mbuf *m);
void
if_init(if_t ifp, void *ctx);
int
if_resolvemulti(if_t ifp, struct sockaddr **, struct sockaddr *);
uint64_t
if_getcounter(if_t ifp, ift_counter counter);
struct label *
if_getmaclabel(if_t ifp);
void
if_setmaclabel(if_t ifp, struct label *label);
struct bpf_if *
if_getbpf(if_t ifp);
uint8_t
if_getpcp(if_t ifp);
void *
if_getl2com(if_t ifp);
struct ifvlantrunk *
if_getvlantrunk(if_t ifp);
bool
if_altq_is_enabled(if_t ifp);
π§ struct ifnet Member Functions π§
void
(*if_input)(if_t ifp, struct mbuf *m);
int
(*if_output)(if_t ifp, struct mbuf *m, const struct sockaddr *dst,
struct route *ro);
void
(*if_start)(if_t ifp);
int
(*if_transmit)(if_t ifp, struct mbuf *m);
void
(*if_qflush)(if_t ifp);
int
(*if_ioctl)(if_t ifp, u_long cmd, caddr_t data);
void
(*if_init)(void *if_softc);
int
(*if_resolvemulti)(if_t ifp, struct sockaddr **retsa,
struct sockaddr *addr);
π struct ifaddr member function π
void
(*ifa_rtrequest)(int cmd, struct rtentry *rt, struct rt_addrinfo *info);
πΊ Global Variables πΊ
extern struct ifnethead ifnet;
extern int if_index;
extern int ifqmaxlen;
π¦ DATA STRUCTURES π¦
okay so the kernel mechanisms for handling network interfaces live primarily in the ifnet, if_data, ifaddr, and ifmultiaddr structures in <net/if.h> and <net/if_var.h>, plus the functions named above and defined in /sys/net/if.c. iconic. π
The interfaces meant for user programs are in <net/if.h> β those include the interface flags, the if_data structure, and the structures that define how interface-related messages show up on the route(4) routing socket and in sysctl(3). The header file <net/if_var.h> defines the kernel-internal interfaces, including the ifnet, ifaddr, and ifmultiaddr structures and the functions that manipulate them. (A few user programs will need <net/if_var.h> because it is the prerequisite of some other header file like <netinet/if_ether.h>. Most references to those two files in particular can be replaced by <net/ethernet.h>.) π«Ά
The system keeps a linked list of interfaces using the TAILQ macros defined in queue(3); this list is headed by a struct ifnethead called ifnet. The elements of this list are of type struct ifnet, and most kernel routines that manipulate interfaces as such accept or return pointers to these structures. Each interface structure contains an if_data structure used for statistics and information. Each interface also has a TAILQ of interface addresses, described by ifaddr structures. An AF_LINK address (see link_addr(3)) describing the link layer implemented by the interface (if any) is accessed by the if_addr structure. (Some trivial interfaces do not provide any link layer addresses; this structure, while still present, serves only to identify the interface name and index.) π
Finally, those interfaces supporting reception of multicast datagrams have a TAILQ of multicast group memberships, described by ifmultiaddr structures. These memberships are reference-counted. β¨
Interfaces are also associated with an output queue, defined as a struct ifqueue; this structure is used to hold packets while the interface is in the process of sending another. π¦
π
The ifnet accessors π
The accessors for if_t are as follows:
if_getbaudrate()if_setbaudrate()β(u_long)the line rate of the interface, in bits per second. speed check, bestie. β‘if_setcapabilities()if_setcapabilitiesbit()if_getcapabilities()β(int)flags describing the capabilities the interface supports (see below).if_getcapenable()if_setcapenable()if_setcapenablebit()if_togglecapenable()β(int)flags describing the enabled capabilities of the interface (see below).if_getcapabilities2()if_setcapabilities2()if_setcapabilities2bit()β the2-suffixed capability accessors, same names, same family energy. π«if_getcapenable2()if_setcapenable2()if_setcapenable2bit()if_togglecapenable2()β the2-suffixed enabled-capability accessors, also right there in the list. πif_getdunit()β(int)a unique number assigned to each interface managed by a particular driver. Drivers may choose to set this toIF_DUNIT_NONEif a unit number is not associated with the device. (Initialized by driver, usually viaif_initname().) πif_getindex()β(u_short)returns the unique number that got assigned to the device when it was attached β¨. You can drop this number into astruct sockaddr_dlto point at one specific interface by index (peeplink_addr(3)). It gets initialized byif_alloc().if_getidxgen()β bestie the page literally gives us no description here, just the name. That's the whole entry. πif_getdname(),if_setdname()β(const char *)the name of the driver. Initialized by the driver (usually by way ofif_initname()).if_name(),if_setname()β(char *)the name of the interface, likefxp0orlo0. Also initialized by the driver, usually viaif_initname().if_getalloctype()β(u_char)the type of the interface as it was at the moment of allocation. It caches the type you handed toif_alloc(), and here's the vibe check: unlikeif_type, drivers don't get to change it. πif_gettype()β listed, and that's all she wrote. No description on this one. π«Άif_setdev()β same situation, name only, no blurb.if_getdrvflags(),if_setdrvflags(),if_setdrvflagbits()β first appearance of these three: no description given. (Spoiler: they come back later with an actual blurb. The manpage does this. We don't question it. πΈ)if_getlinkstate()β just the name, no description. πif_clearhwassist(),if_sethwassistbits(),if_gethwassist(),if_sethwassist(),if_togglehwassist()β(u_long)this is the detailed breakdown of which computational tasks the interface can offload for outgoing packets π₯. The driver must keep this field in sync with whateverif_capenablecurrently says β that's not optional, bestie.if_getsoftc(),if_setsoftc()β(void *)a pointer to the driver's own private state block. The driver initializes it at attach.if_setllsoftc()β name only, no description. πif_getllsoftc()β name only, no description. πif_getfib()β name only, no description.if_getaddrlen()β name only, no description.if_gethwaddr()β name only, no description.if_getbroadcastaddr(),if_setbroadcastaddr()β access the interface broadcast address. (Yep, this pair shows up twice in this page β hold that thought, there's a second, more specific blurb further down. π )if_setmtu()β name only, no description.if_getmtu()β access the interface MTU.if_setflags(),if_getflags(),if_setflagbits()β(int)flags that describe the operational parameters of this interface (see below, the flag table is coming β¨). These particular flags get manipulated by generic code.if_getnumadomain()β(uint8_t)the NUMA domain of the hardware device tied to this interface. Heads up: it's filled in with a wildcard value unless the kernel is NUMA aware, the system is a NUMA system, and the ifnet was allocated withif_alloc_dev()orif_alloc_domain(). All three, no cap.if_sendq_empty()β name only, no description. π¦if_setsendqready()β name only, no description. π¦if_setsendqlen()β name only, no description. π¦if_sethwtsomax(),if_gethwtsomax()β names only, no description.if_sethwtsomaxsegcount(),if_gethwtsomaxsegcount()β names only, no description.if_sethwtsomaxsegsize(),if_gethwtsomaxsegsize()β names only, no description.if_setnetmapadapter(),if_getnetmapadapter()β names only, no description.if_setifheaderlen()β name only, no description.if_setrcvif()β name only, no description.if_setvtag(),if_getvtag()β names only, no description.if_vlantrunkinuse()β name only, no description.if_getlladdr()β name only, no description.if_getvnet()β(struct vnet *)a pointer to the virtual network stack instance. This one gets initialized byif_attach().if_gethandle()β name only, no description.if_vlancap()β name only, no description.if_getcounter()β name only, no description.if_getmaclabel(),if_setmaclabel()β names only, no description.if_getbpf()β(struct bpf_if *)opaque per-interface data for the packet filter,bpf(4). Initialized bybpf_attach().if_getpcp()β name only, no description.if_getl2com()β a pointer to the common data for the interface's layer 2 protocol. Initialized byif_alloc().if_getvlantrunk(if_t ifp)β(struct ifvlantrunk *)a pointer to the 802.1Q trunk structure,vlan(4). This one gets initialized by the driver-specificif_ioctl()routine. (Real talk: in the original page this entry is all smushed onto the end of theif_getl2com()line β we are un-smushing it for you, you're welcome π)if_getdrvflags(),if_setdrvflags(),if_setdrvflagbits()β and here they are again, this time with the actual description:(int)flags describing the operational status of this interface (see below β¨). These flags are manipulated by the driver.if_addmulti(),if_delmulti(),if_findmulti()β add, remove, and find the multicast addresses assigned to this interface.if_getifaddr()β(struct ifaddr *)get a pointer to the interface's link-level address.if_getbroadcastaddr(),if_setbroadcastaddr()β yep, round two:(const u_int8_t *)a link-level broadcast bytestring, for protocols whose addresses have variable length.if_getafdata()β(void *)an address-family-dependent data region.if_addgroup(),if_delgroup()β add and delete groups from the interface.
Okay so about keeping ifnet structures alive π«Ά β you gain a reference to one by calling if_ref(), and you release it by calling if_rele(). The whole point is that kernel code walking the global interface lists can drop the ifnet lock and still keep the ifnet structure from vanishing out from under them. Stable struct, no lock held, immaculate vibes. β¨
π¦ the function pointers you gotta fill in π¦
On top of all that, there's a whole set of function pointers the driver must initialize to actually complete its interface with the generic interface layer. Don't leave these hanging, bestie:
if_input()β pass a packet up to the appropriate upper layer, decided by the packet's link-layer header. Call this from an interrupt handler, or use it to emulate receiving a packet on this interface. One single function implementingif_input()can be shared across multiple drivers that use the same link-layer framing β Ethernet being the obvious example. Iconic efficiency. πif_output()β output a packet on interface ifp, or queue it on the output queue if the interface is already active.if_transmit()β transmit a packet on an interface, or queue it if the interface is in use. ReturnsENOBUFSif the device's software and hardware queues are both full π« . You must install this afterif_attach()to override the default implementation. It's exposed so drivers can manage their own queues and cut down on the latency caused by a frequently gratuitous enqueue/dequeue pair toifq. The suggested internal software queuing mechanism isbuf_ring.if_qflush()β free mbufs sitting in internally managed queues when the interface gets marked down. Also must be installed afterif_attach()to override the default. Also exposed so drivers can manage their own queues and dodge that frequently gratuitous enqueue/dequeue latency toifq. Also suggestsbuf_ringas the internal software queuing mechanism. (Yes, this blurb is basically a twin of the last one β that's the page's choice, not mine. π)if_start()β start queued output on an interface. It's exposed so that some interface classes can share oneif_output()across all their drivers. Big rule:if_start()may only be called when theIFF_DRV_OACTIVEflag is not set. So no,IFF_DRV_OACTIVEdoesn't literally mean "output is active" β it means the device's internal output queue is full. And heads up: this function will soon be deprecated. πif_ioctl()β process interface-relatedioctl(2)requests (defined in<sys/sockio.h>). The generic routineifioctl()does the preliminary work first: checking for appropriate privileges, locating the interface being manipulated, and doing certain generic operations like twiddling flags and flushing queues. See the description ofifioctl()below for more. πif_init()β initialize and bring up the hardware: reset the chip, enable the receiver unit, that kind of thing. It should mark the interface running but not active β soIFF_DRV_RUNNING,~IIF_DRV_OACTIVE. (That's exactly how the page writes it, tilde and all β¨.)if_resolvemulti()β check that the requested multicast group membership, addr, is actually valid, and if needed compute the link-layer group that corresponds to that address, returned in*retsa. Returns zero on success, or an error code if it fails.
πΈ Interface Flags πΈ
Interface flags get used for a bunch of different things, so let's get into it. Some flags just tell you about the type of interface and what it's capable of; others get fiddled with dynamically to reflect the interface's current state. In the table, flags of the first kind are marked β¨Sβ©, and flags of the second kind are marked β¨Dβ©. Easy.
Also: any flag that starts with IFF_DRV_ lives in if_drv_flags; every other flag lives in if_flags. Keep them in their correct homes please. π
The macroIFF_CANTCHANGEdefines the bits that a user program cannot set by issuing theSIOCSIFFLAGScommand toioctl(2). Those un-settable bits are marked with an asterisk (*) in the listing that follows.
IFF_UPβ β¨Dβ© The interface has been configured up by user-level code. πIFF_BROADCASTβ β¨S*β© The interface supports broadcast.IFF_DEBUGβ β¨Dβ© Used to enable/disable driver debugging code.IFF_LOOPBACKβ β¨Sβ© The interface is a loopback device.IFF_POINTOPOINTβ β¨S*β© The interface is point-to-point; the βbroadcastβ address is actually the address of the other end.IFF_DRV_RUNNINGβ β¨D*β© The interface has been configured and dynamic resources were successfully allocated. Probably only useful internal to the interface.IFF_NOARPβ β¨Dβ© Disable network address resolution on this interface.IFF_PROMISCβ β¨D*β© This interface is in promiscuous mode.IFF_PPROMISCβ β¨Dβ© This interface is in permanently promiscuous mode (impliesIFF_PROMISC).IFF_ALLMULTIβ β¨D*β© This interface is in all-multicasts mode (used by multicast routers).IFF_PALLMULTIβ β¨Dβ© This interface is in permanently all-multicasts mode (impliesIFF_ALLMULTI).IFF_DRV_OACTIVEβ β¨D*β© The interface's hardware output queue (if any) is full; output packets are to be queued.IFF_SIMPLEXβ β¨S*β© The interface cannot hear its own transmissions.IFF_LINK0β β¨Dβ© Control flags for the link layer. (Currently abused to select among multiple physical layers on some devices.)IFF_LINK1β β¨Dβ© Control flags for the link layer. (Currently abused to select among multiple physical layers on some devices.)IFF_LINK2β β¨Dβ© Control flags for the link layer. (Currently abused to select among multiple physical layers on some devices.)IFF_MULTICASTβ β¨S*β© This interface supports multicast.IFF_CANTCONFIGβ β¨S*β© The interface is not configurable in a meaningful way. Primarily useful forIFT_USBinterfaces registered at the interface list.IFF_MONITORβ β¨Dβ© This interface blocks transmission of packets and discards incoming packets after BPF processing. Used to monitor network traffic but not interact with the network in question.IFF_STATICARPβ β¨Dβ© Used to enable/disable ARP requests on this interface.IFF_DYINGβ β¨D*β© Set when theifnetstructure of this interface is being released and still hasif_refcountreferences.IFF_RENAMINGβ β¨Dβ© Set when this interface is being renamed.
π Interface Capabilities Flags π
Interface capabilities are specialized features that an interface may or may not support. They're very hardware-specific, and when enabled they let the interface offload specific network processing or offer a particular feature for use by other kernel parts. β¨
β οΈ It should be stressed that a capability can be completely uncontrolled (i.e., stay always enabled with no way to disable it) or allow limited control over itself (e.g., depend on another capability's state). Such peculiarities are determined solely by the hardware and driver of a particular interface. Only the driver possesses the knowledge on whether and how the interface capabilities can be controlled. Consequently, capabilities flags inif_capenableshould never be modified directly by kernel code other than the interface driver. The commandSIOCSIFCAPtoifioctl()is the dedicated means to attempt alteringif_capenableon an interface. Userland code shall useioctl(2).
The following capabilities are currently supported by the system:
IFCAP_RXCSUMβ This interface can do checksum validation on receiving data. Some interfaces do not have sufficient buffer storage to store frames above a certain MTU-size completely. The driver for the interface might disable hardware checksum validation if the MTU is set above the hardcoded limit.IFCAP_TXCSUMβ This interface can do checksum calculation on transmitting data.IFCAP_HWCSUMβ A shorthand for(IFCAP_RXCSUM | IFCAP_TXCSUM).IFCAP_NETCONSβ This interface can be a network console.IFCAP_VLAN_MTUβ Thevlan(4)driver can operate over this interface in software tagging mode without having to decrease MTU onvlan(4)interfaces below 1500 bytes. This implies the ability of this interface to cope with frames somewhat longer than permitted by the Ethernet specification.IFCAP_VLAN_HWTAGGINGβ This interface can do VLAN tagging on output and demultiplex frames by their VLAN tag on input.IFCAP_JUMBO_MTUβ This Ethernet interface can transmit and receive frames up to 9000 bytes long.IFCAP_POLLINGβ This interface supportspolling(4). See below for details.IFCAP_VLAN_HWCSUMβ This interface can do checksum calculation on both transmitting and receiving data onvlan(4)interfaces (impliesIFCAP_HWCSUM).IFCAP_TSO4β This Ethernet interface supports TCP4 Segmentation offloading.IFCAP_TSO6β This Ethernet interface supports TCP6 Segmentation offloading.IFCAP_TSOβ A shorthand for(IFCAP_TSO4 | IFCAP_TSO6).IFCAP_TOE4β This Ethernet interface supports TCP4 Offload Engine.IFCAP_TOE6β This Ethernet interface supports TCP6 Offload Engine.IFCAP_TOEβ A shorthand for(IFCAP_TOE4 | IFCAP_TOE6).IFCAP_WOL_UCASTβ This Ethernet interface supports waking up on any Unicast packet.IFCAP_WOL_MCASTβ This Ethernet interface supports waking up on any Multicast packet.IFCAP_WOL_MAGICβ This Ethernet interface supports waking up on any Magic packet such as those sent bywake(8).IFCAP_WOLβ A shorthand for(IFCAP_WOL_UCAST | IFCAP_WOL_MCAST | IFCAP_WOL_MAGIC).IFCAP_VLAN_HWFILTERβ This interface supports frame filtering in hardware onvlan(4)interfaces.IFCAP_VLAN_HWTSOβ This interface supports TCP Segmentation offloading onvlan(4)interfaces (impliesIFCAP_TSO).IFCAP_LINKSTATEβ This Ethernet interface supports dynamic link state changes.IFCAP_NETMAPβ This Ethernet interface supportsnetmap(4).
The ability of advanced network interfaces to offload certain computational tasks from the host CPU to the board is limited mostly to TCP/IP. Therefore a separate field associated with an interface (see ifnet.if_data.ifi_hwassist below) keeps a detailed description of its enabled capabilities specific to TCP/IP processing. The TCP/IP module consults the field to see which tasks can be done on an outgoing packet by the interface. The flags defined for that field are a superset of those for mbuf.m_pkthdr.csum_flags, namely:
CSUM_IPβ The interface will compute IP checksums.CSUM_TCPβ The interface will compute TCP checksums.CSUM_UDPβ The interface will compute UDP checksums.
An interface notifies the TCP/IP module about the tasks the former has performed on an incoming packet by setting the corresponding flags in the field mbuf.m_pkthdr.csum_flags of the mbuf chain containing the packet. See mbuf(9) for details. π¦
The capability of a network interface to operate in polling(4) mode involves several flags in different global variables and per-interface fields. The capability flag IFCAP_POLLING set in interface's if_capabilities indicates support for polling(4) on the particular interface. If set in if_capabilities, the same flag can be marked or cleared in the interface's if_capenable within ifioctl(), thus initiating switch of the interface to polling(4) mode or interrupt mode, respectively. The actual mode change is managed by the driver-specific if_ioctl() routine. The polling(4) handler returns the number of packets processed. π
π§Έ The if_data Structure π§Έ
The if_data structure contains statistics and identifying information used by management programs, and which is exported to user programs by way of the ifmib(4) branch of the sysctl(3) MIB. The following elements of the if_data structure are initialized by the interface and are not expected to change significantly over the course of normal operation:
ifi_type(u_char) β The type of the interface, as defined in<net/if_types.h>and described below in the βInterface Typesβ section.ifi_physical(u_char) β Intended to represent a selection of physical layers on devices which support more than one; never implemented.ifi_addrlen(u_char) β Length of a link-layer address on this device, or zero if there are none. Used to initialize the address length field insockaddr_dlstructures referring to this interface.ifi_hdrlen(u_char) β Maximum length of any link-layer header which might be prepended by the driver to a packet before transmission. The generic code computes the maximum over all interfaces and uses that value to influence the placement of data in mbufs to attempt to ensure that there is always sufficient space to prepend a link-layer header without allocating an additional mbuf.ifi_datalen(u_char) β Length of theif_datastructure. Allows some stabilization of the routing socket ABI in the face of increases in the length ofstruct ifdata.ifi_mtu(u_long)β the max transmission unit of the medium, exclusive of any link-layer overhead.ifi_metric(u_long)β a dimensionless metric that a user-mode routing process interprets.ifi_epoch(time_t)β the system uptime when the interface was attached or when the stats below got reset. Itβs meant to set the SNMP variableifCounterDiscontinuityTime. It can also be used to figure out whether two successive queries for an interface with the same index actually returned results for the same interface. iykyk β¨
The structure additionally contains generic statistics applicable to a variety of different interface types β and except as noted, all members are of type u_long:
ifi_link_state(u_char)β the current link state of Ethernet interfaces. Peep the βInterface Link Statesβ section for possible values.ifi_ipacketsβ number of packets received.ifi_ierrorsβ number of receive errors detected (e.g., FCS errors, DMA overruns, etc.). More detailed breakdowns can often be had by way of a link-specific MIB.ifi_opacketsβ number of packets transmitted.ifi_oerrorsβ number of output errors detected (e.g., late collisions, DMA overruns, etc.). More detailed breakdowns can often be had by way of a link-specific MIB.ifi_collisionsβ total number of collisions detected on output for CSMA interfaces. (This member is sometimes [ab]used by other types of interfaces for other output error counts. messy but iconic π )ifi_ibytesβ total traffic received, in bytes.ifi_obytesβ total traffic transmitted, in bytes.ifi_imcastsβ number of packets received which were sent by link-layer multicast.ifi_omcastsβ number of packets sent by link-layer multicast.ifi_iqdropsβ number of packets dropped on input. Rarely implemented.ifi_oqdropsβ number of packets dropped on output.ifi_noprotoβ number of packets received for unknown network-layer protocol.ifi_lastchange(struct timeval)β the time of the last administrative change to the interface (as required for SNMP).
β¨ Interface Types β¨
The header file <net/if_types.h> defines symbolic constants for a number of different types of interfaces. The most common are:
IFT_OTHERβ none of the followingIFT_ETHERβ EthernetIFT_ISO88023β ISO 8802-3 CSMA/CDIFT_ISO88024β ISO 8802-4 Token BusIFT_ISO88025β ISO 8802-5 Token RingIFT_ISO88026β ISO 8802-6 DQDB MANIFT_FDDIβ FDDIIFT_PPPβ Internet Point-to-Point Protocol (ppp(8))IFT_LOOPβ The loopback (lo(4)) interfaceIFT_SLIPβ Serial Line IPIFT_PARAβ Parallel-port IP (βPLIPβ)IFT_ATMβ Asynchronous Transfer ModeIFT_USBβ USB Interface
π¦ Interface Link States π¦
The following link states are currently defined:
LINK_STATE_UNKNOWNβ The link is in an invalid or unknown state.LINK_STATE_DOWNβ The link is down.LINK_STATE_UPβ The link is up.
π The ifaddr Structure π
Every interface is associated with a list (or, rather, a TAILQ) of addresses, rooted at the interface structureβs if_addrhead member. The first element in this list is always an AF_LINK address representing the interface itself; multi-access network drivers should complete this structure by filling in their link-layer addresses after calling if_attach(). Other members of the structure represent network-layer addresses which have been configured by means of the SIOCAIFADDR command to ioctl(2), called on a socket of the appropriate protocol family.
The elements of this list consist of ifaddr structures. Most protocols will declare their own protocol-specific interface address structures, but all begin with a struct ifaddr which provides the most-commonly-needed functionality across all protocols. Interface addresses are reference-counted. very responsible of them π«Ά
The members of struct ifaddr are as follows:
ifa_addr(struct sockaddr *)β the local address of the interface.ifa_dstaddr(struct sockaddr *)β the remote address of point-to-point interfaces, and the broadcast address of broadcast interfaces. (ifa_broadaddris a macro forifa_dstaddr.)ifa_netmask(struct sockaddr *)β the network mask for multi-access interfaces, and the confusion generator for point-to-point interfaces. iconic behavior honestly πifa_ifp(if_t)β a link back to the interface structure.ifa_link(TAILQ_ENTRY(ifaddr))βqueue(3)glue for list of addresses on each interface.ifa_rtrequestβ see below.ifa_flags(u_short)β some of the flags which would be used for a route representing this address in the route table.ifa_refcnt(short)β the reference count.
References to ifaddr structures are gained by calling the ifa_ref() function and released by calling the ifa_free() function.
ifa_rtrequest() is a pointer to a function which receives callouts from the routing code (rtrequest()) to perform link-layer-specific actions upon requests to add, or delete routes. The cmd argument indicates the request in question: RTM_ADD, or RTM_DELETE. The rt argument is the route in question; the info argument contains the specific destination being manipulated.
π FUNCTIONS π
The functions provided by the generic interface code can be divided into two groups: those which manipulate interfaces, and those which manipulate interface addresses. In addition to these functions, there may also be link-layer support routines which are used by a number of drivers implementing a specific link layer over different hardware; see the documentation for that link layer for more details. yes bestie, thereβs lore in the link layer too π§
π§ The ifmultiaddr Structure π§
Every multicast-capable interface is associated with a list of multicast group memberships, which indicate at a low level which link-layer multicast addresses (if any) should be accepted, and at a high level, in which network-layer multicast groups a user process has expressed interest.
The elements of the structure are as follows:
ifma_link(LIST_ENTRY(ifmultiaddr))βqueue(3)macro glue.ifma_addr(struct sockaddr *)β a pointer to the address which this record represents. The memberships for various address families are stored in arbitrary order.ifma_lladdr(struct sockaddr *)β a pointer to the link-layer multicast address, if any, to which the network-layer multicast address inifma_addris mapped, else a null pointer. If this element is non-nil, this membership also holds an invisible reference to another membership for that link-layer address.ifma_refcount(u_int)β a reference count of requests for this particular membership.
π Interface Manipulation Functions π
if_alloc()β allocate and initializestruct ifnet. Initialization includes the allocation of an interface index and may include the allocation of a type specific structure inif_l2com.if_alloc_dev()β allocate and initializestruct ifnetasif_alloc()does, with the addition that the ifnet can be tagged with the appropriate NUMA domain derived from thedevargument passed by the caller.if_alloc_domain()β allocate and initializestruct ifnetasif_alloc()does, with the addition that the ifnet will be tagged with the NUMA domain via thenuma_domainargument passed by the caller.if_attach()β link the specified interfaceifpinto the list of network interfaces. Also initialize the list of addresses on that interface, and create a link-layerifaddrstructure to be the first element in that list. (A pointer to this address structure is saved in the ifnet structure.) Theifpmust have been allocated byif_alloc(),if_alloc_dev()orif_alloc_domain().if_detach()β shut down and unlink the specifiedifpfrom the interface list.if_free()β free the givenifpback to the system. The interface must have been previously detached if it was ever attached.if_free_type()β identical toif_free()except that the given type is used to freeif_l2cominstead of the type inif_type. This is intended for use with drivers that change their interface type.if_down()β mark the interfaceifpas down (i.e.,IFF_UPis not set), flush its output queue, notify protocols of the transition, and generate a message from theroute(4)routing socket.if_up()β mark the interfaceifpas up, notify protocols of the transition, and generate a message from theroute(4)routing socket.ifpromisc()β add or remove a promiscuous reference toifp. Ifpswitchis true, add a reference; if it is false, remove a reference. On reference count transitions from zero to one and one to zero, set theIFF_PROMISCflag appropriately and callif_ioctl()to set up the interface in the desired mode.if_allmulti()β asifpromisc(), but for the all-multicasts (IFF_ALLMULTI) flag instead of the promiscuous flag.ifunit()β return anifnetpointer for the interface namedname.ifunit_ref()β return a reference-counted (viaifa_ref())ifnetpointer for the interface namedname. This is the preferred function overifunit(). The caller is responsible for releasing the reference withif_rele()when it is finished with the ifnet.
β¨ ifioctl() β¨
Okay bestie, meet ifioctl() β it handles the ioctl request cmd, issued on socket so by thread td, with data parameter data. It is literally the main routine for all interface configuration requests from user mode. It is ordinarily only called from the socket-layer ioctl(2) handler, and only for commands with class βiβ. Any unrecognized commands get passed down to socket so's protocol for further interpretation. Here's the command menu it handles:
SIOCGIFCONFβ Get interface configuration. (No call-down to driver.) πSIOCSIFNAMEβ Set the interface name.RTM_IFANNOUNCEdeparture and arrival messages are sent so that routing code that relies on the interface name will update its interface list. Caller must have appropriate privilege. (No call-down to driver.)SIOCGIFCAPβ Get interface capabilities. (No call-down to driver.)SIOCGIFDATAβ Get interface data. (No call-down to driver.)SIOCGIFFIBβ Get interface FIB. (No call-down to driver.)SIOCGIFFLAGSβ Get interface flags. (No call-down to driver.)SIOCGIFMETRICβ Get interface metric. (No call-down to driver.)SIOCGIFMTUβ Get interface MTU. (No call-down to driver.)SIOCGIFPHYSβ Get interface medium selection. (No call-down to driver.)SIOCSIFCAPβ Enable or disable interface capabilities. Caller must have appropriate privilege. Before a call to the driver-specificif_ioctl()routine, the requested mask for enabled capabilities is checked against the mask of capabilities supported by the interface,if_capabilities. Requesting to enable an unsupported capability is invalid. The rest is supposed to be done by the driver, which includes updatingif_capenableandif_data.ifi_hwassistappropriately.SIOCGIFCAPNVβnv(9)version of theSIOCGIFCAPioctl. Caller must provide a pointer tostruct ifreq_cap_nvasdata, where the memberbufferpoints to some buffer containingbuf_lengthbytes. The serialized nvlist with description of the device capabilities is written to the buffer. If buffer is too short, the structure is updated withbuffermember set toNULL, length set to the minimal required length, and errorEFBIGis returned.
Elements of the returned nvlist for simple capabilities are boolean, identified by names. Presence of the boolean element means that corresponding capability is supported by the interface. Element's value describes the current configured state: true means that the capability is enabled, and false that it is disabled.
Driver indicates support for both SIOCGIFCAPNV and SIOCSIFCAPNV requests by setting IFCAP_NV non-modifiable capability bit in if_capabilities.
SIOCSIFCAPNVβnv(9)version of theSIOCSIFCAPioctl. Caller must provide the pointer tostruct ifreq_cap_nvasdata, where the memberbufferpoints to serialized nvlist oflengthbytes. Each element of nvlist describes a requested update of one capability, identified by the element name. For simple capabilities, the element must be boolean. Itstruevalue means that the caller asks to enable the capability, andfalsevalue to disable. Only capabilities listed in the nvlist are affected by the call.SIOCSIFFIBβ Sets interface FIB. Caller must have appropriate privilege. FIB values start at0and values greater or equal tonet.fibsare considered invalid.SIOCSIFFLAGSβ Change interface flags. Caller must have appropriate privilege. If a change to theIFF_UPflag is requested,if_up()orif_down()is called as appropriate. Flags listed inIFF_CANTCHANGEare masked off, and the fieldif_flagsin the interface structure is updated. Finally, the driverif_ioctl()routine is called to perform any setup requested.SIOCSIFMETRICβ Change interface metric. Caller must have appropriate privilege.SIOCSIFPHYSβ Change interface medium. Caller must have appropriate privilege.SIOCSIFMTUβ Change interface MTU. Caller must have appropriate privilege. MTU values less than72or greater than65535are considered invalid. The driverif_ioctl()routine is called to implement the change; it is responsible for any additional sanity checking and for actually modifying the MTU in the interface structure.SIOCADDMULTIβ Add permanent multicast group membership on the interface. Caller must have appropriate privilege. Theif_addmulti()function is called to perform the operation; qq.v.SIOCDELMULTIβ Delete permanent multicast group membership on the interface. Caller must have appropriate privilege. Theif_delmulti()function is called to perform the operation; qq.v.SIOCAIFADDRβ The socket's protocol control routine is called to implement the requested action.SIOCDIFADDRβ The socket's protocol control routine is called to implement the requested action.
π«Ά Interface Address Functions π«Ά
Several functions exist to look up an interface address structure given an address, and honestly they're giving very useful squad energy:
ifa_ifwithaddr()β returns an interface address with either a local address or a broadcast address precisely matching the parameteraddr.ifa_ifwithdstaddr()β returns an interface address for a point-to-point interface whose remote (βdestinationβ) address isaddrand afibisfib. IffibisRT_ALL_FIBS, then the first interface address matchingaddrwill be returned.ifa_ifwithnet()β returns the most specific interface address which matches the specified address,addr, subject to its configured netmask, or a point-to-point interface address whose remote address isaddrif one is found. Ifignore_ptpis true, skip point-to-point interface addresses. Thefibparameter is handled the same way as byifa_ifwithdstaddr().ifaof_ifpforaddr()β returns the most specific address configured on interfaceifpwhich matches addressaddr, subject to its configured netmask. If the interface is point-to-point, only an interface address whose remote address is preciselyaddrwill be returned.
All of these functions return a null pointer if no such address can be found. No cap. π
π¦ Interface Multicast Address Functions π¦
The if_addmulti(), if_delmulti(), and if_findmulti() functions provide support for requesting and relinquishing multicast group memberships, and for querying an interface's membership list, respectively.
The if_addmulti() function takes a pointer to an interface, ifp, and a generic address, sa. It also takes a pointer to a struct ifmultiaddr * which is filled in on successful return with the address of the group membership control block. The if_addmulti() function performs the following four-step process:
- Call the interface's
if_resolvemulti()entry point to determine the link-layer address, if any, corresponding to this membership request, and also to give the link layer an opportunity to veto this membership request should it so desire. - Check the interface's group membership list for a pre-existing membership for this group. If one is not found, allocate a new one; if one is, increment its reference count.
- If the
if_resolvemulti()routine returned a link-layer address corresponding to the group, repeat the previous step for that address as well. - If the interface's multicast address filter needs to be changed because a new membership was added, call the interface's
if_ioctl()routine (with acmdargument ofSIOCADDMULTI) to request that it do so.
The if_delmulti() function, given an interface ifp and an address, sa, reverses this process. Both functions return zero on success, or a standard error number on failure.
The if_findmulti() function examines the membership list of interface ifp for an address matching sa, and returns a pointer to that struct ifmultiaddr if one is found, else it returns a null pointer. Obsessed with the clarity. π
π SEE ALSO π
ioctl(2)link_addr(3)queue(3)sysctl(3)bpf(4)ifmib(4)lo(4)netintro(4)polling(4)config(8)ppp(8)mbuf(9)rtentry(9)
And a book rec for the girlies who love deep dives: Gary R. Wright and W. Richard Stevens, TCP/IP Illustrated, Vol. 2, Addison-Wesley, ISBN 0-201-63354-X. πβ¨
π AUTHORS π
This manual page was written by Garrett A. Wollman. π
Okay that's the whole page, bestie π«Ά β this one came from Debian, dated December 10, 2024, and it's IFNET(9). Byeee! π
πΈ
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright 1996, 1997 Massachusetts Institute of Technology
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms