IFNET(9)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1

🌸 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() β€” the 2-suffixed capability accessors, same names, same family energy. πŸ’«
  • if_getcapenable2() if_setcapenable2() if_setcapenable2bit() if_togglecapenable2() β€” the 2-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 to IF_DUNIT_NONE if a unit number is not associated with the device. (Initialized by driver, usually via if_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 a struct sockaddr_dl to point at one specific interface by index (peep link_addr(3)). It gets initialized by if_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 of if_initname()).
  • if_name(), if_setname() β€” (char *) the name of the interface, like fxp0 or lo0. Also initialized by the driver, usually via if_initname().
  • if_getalloctype() β€” (u_char) the type of the interface as it was at the moment of allocation. It caches the type you handed to if_alloc(), and here's the vibe check: unlike if_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 whatever if_capenable currently 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 with if_alloc_dev() or if_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 by if_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 by bpf_attach().
  • if_getpcp() β€” name only, no description.
  • if_getl2com() β€” a pointer to the common data for the interface's layer 2 protocol. Initialized by if_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-specific if_ioctl() routine. (Real talk: in the original page this entry is all smushed onto the end of the if_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 implementing if_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. Returns ENOBUFS if the device's software and hardware queues are both full 🫠. You must install this after if_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 to ifq. The suggested internal software queuing mechanism is buf_ring.
  • if_qflush() β€” free mbufs sitting in internally managed queues when the interface gets marked down. Also must be installed after if_attach() to override the default. Also exposed so drivers can manage their own queues and dodge that frequently gratuitous enqueue/dequeue latency to ifq. Also suggests buf_ring as 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 one if_output() across all their drivers. Big rule: if_start() may only be called when the IFF_DRV_OACTIVE flag is not set. So no, IFF_DRV_OACTIVE doesn'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-related ioctl(2) requests (defined in <sys/sockio.h>). The generic routine ifioctl() 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 of ifioctl() 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 β€” so IFF_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 macro IFF_CANTCHANGE defines the bits that a user program cannot set by issuing the SIOCSIFFLAGS command to ioctl(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 (implies IFF_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 (implies IFF_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 for IFT_USB interfaces 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 the ifnet structure of this interface is being released and still has if_refcount references.
  • 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 in if_capenable should never be modified directly by kernel code other than the interface driver. The command SIOCSIFCAP to ifioctl() is the dedicated means to attempt altering if_capenable on an interface. Userland code shall use ioctl(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 β€” The vlan(4) driver can operate over this interface in software tagging mode without having to decrease MTU on vlan(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 supports polling(4). See below for details.
  • IFCAP_VLAN_HWCSUM β€” This interface can do checksum calculation on both transmitting and receiving data on vlan(4) interfaces (implies IFCAP_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 by wake(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 on vlan(4) interfaces.
  • IFCAP_VLAN_HWTSO β€” This interface supports TCP Segmentation offloading on vlan(4) interfaces (implies IFCAP_TSO).
  • IFCAP_LINKSTATE β€” This Ethernet interface supports dynamic link state changes.
  • IFCAP_NETMAP β€” This Ethernet interface supports netmap(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 in sockaddr_dl structures 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 the if_data structure. Allows some stabilization of the routing socket ABI in the face of increases in the length of struct 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 variable ifCounterDiscontinuityTime. 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 following
  • IFT_ETHER β€” Ethernet
  • IFT_ISO88023 β€” ISO 8802-3 CSMA/CD
  • IFT_ISO88024 β€” ISO 8802-4 Token Bus
  • IFT_ISO88025 β€” ISO 8802-5 Token Ring
  • IFT_ISO88026 β€” ISO 8802-6 DQDB MAN
  • IFT_FDDI β€” FDDI
  • IFT_PPP β€” Internet Point-to-Point Protocol (ppp(8))
  • IFT_LOOP β€” The loopback (lo(4)) interface
  • IFT_SLIP β€” Serial Line IP
  • IFT_PARA β€” Parallel-port IP (β€œPLIP”)
  • IFT_ATM β€” Asynchronous Transfer Mode
  • IFT_USB β€” USB Interface

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_broadaddr is a macro for ifa_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 in ifma_addr is 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 initialize struct ifnet. Initialization includes the allocation of an interface index and may include the allocation of a type specific structure in if_l2com.
  • if_alloc_dev() β€” allocate and initialize struct ifnet as if_alloc() does, with the addition that the ifnet can be tagged with the appropriate NUMA domain derived from the dev argument passed by the caller.
  • if_alloc_domain() β€” allocate and initialize struct ifnet as if_alloc() does, with the addition that the ifnet will be tagged with the NUMA domain via the numa_domain argument passed by the caller.
  • if_attach() β€” link the specified interface ifp into the list of network interfaces. Also initialize the list of addresses on that interface, and create a link-layer ifaddr structure to be the first element in that list. (A pointer to this address structure is saved in the ifnet structure.) The ifp must have been allocated by if_alloc(), if_alloc_dev() or if_alloc_domain().
  • if_detach() β€” shut down and unlink the specified ifp from the interface list.
  • if_free() β€” free the given ifp back to the system. The interface must have been previously detached if it was ever attached.
  • if_free_type() β€” identical to if_free() except that the given type is used to free if_l2com instead of the type in if_type. This is intended for use with drivers that change their interface type.
  • if_down() β€” mark the interface ifp as down (i.e., IFF_UP is not set), flush its output queue, notify protocols of the transition, and generate a message from the route(4) routing socket.
  • if_up() β€” mark the interface ifp as up, notify protocols of the transition, and generate a message from the route(4) routing socket.
  • ifpromisc() β€” add or remove a promiscuous reference to ifp. If pswitch is true, add a reference; if it is false, remove a reference. On reference count transitions from zero to one and one to zero, set the IFF_PROMISC flag appropriately and call if_ioctl() to set up the interface in the desired mode.
  • if_allmulti() β€” as ifpromisc(), but for the all-multicasts (IFF_ALLMULTI) flag instead of the promiscuous flag.
  • ifunit() β€” return an ifnet pointer for the interface named name.
  • ifunit_ref() β€” return a reference-counted (via ifa_ref()) ifnet pointer for the interface named name. This is the preferred function over ifunit(). The caller is responsible for releasing the reference with if_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_IFANNOUNCE departure 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-specific if_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 updating if_capenable and if_data.ifi_hwassist appropriately.
  • SIOCGIFCAPNV β€” nv(9) version of the SIOCGIFCAP ioctl. Caller must provide a pointer to struct ifreq_cap_nv as data, where the member buffer points to some buffer containing buf_length bytes. The serialized nvlist with description of the device capabilities is written to the buffer. If buffer is too short, the structure is updated with buffer member set to NULL, length set to the minimal required length, and error EFBIG is 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 the SIOCSIFCAP ioctl. Caller must provide the pointer to struct ifreq_cap_nv as data, where the member buffer points to serialized nvlist of length bytes. Each element of nvlist describes a requested update of one capability, identified by the element name. For simple capabilities, the element must be boolean. Its true value means that the caller asks to enable the capability, and false value 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 at 0 and values greater or equal to net.fibs are considered invalid.
  • SIOCSIFFLAGS β€” Change interface flags. Caller must have appropriate privilege. If a change to the IFF_UP flag is requested, if_up() or if_down() is called as appropriate. Flags listed in IFF_CANTCHANGE are masked off, and the field if_flags in the interface structure is updated. Finally, the driver if_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 than 72 or greater than 65535 are considered invalid. The driver if_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. The if_addmulti() function is called to perform the operation; qq.v.
  • SIOCDELMULTI β€” Delete permanent multicast group membership on the interface. Caller must have appropriate privilege. The if_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 parameter addr.
  • ifa_ifwithdstaddr() β€” returns an interface address for a point-to-point interface whose remote (β€œdestination”) address is addr and a fib is fib. If fib is RT_ALL_FIBS, then the first interface address matching addr will 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 is addr if one is found. If ignore_ptp is true, skip point-to-point interface addresses. The fib parameter is handled the same way as by ifa_ifwithdstaddr().
  • ifaof_ifpforaddr() β€” returns the most specific address configured on interface ifp which matches address addr, subject to its configured netmask. If the interface is point-to-point, only an interface address whose remote address is precisely addr will 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:

  1. 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.
  2. 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.
  3. If the if_resolvemulti() routine returned a link-layer address corresponding to the group, repeat the previous step for that address as well.
  4. 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 a cmd argument of SIOCADDMULTI) 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 πŸ‘‘

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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms