IFNET(9)
๐ IFNET(9) โ Kernel Developer's Manual ๐
okay bestie, welcome to the ifnet man page! this is part 1 of 5, so grab your boba and let's get into it. we're talking kernel interfaces for manipulating network interfaces. it's giving big kernel energy. ๐
โจ NAME โจ
ifnet, ifaddr, ifqueue, if_data โ kernel interfaces for manipulating network interfaces
no cap, that's the whole squad. they're the ones doing the network interface manipulation. periodt.
๐ธ SYNOPSIS ๐ธ
first, the includes. you gotta have these or you're literally lost:
#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>
then we got the functions, grouped by vibe.
๐ฆ Interface Manipulation Functions ๐ฆ
these are the manipulators, bestie. they do the interface stuff:
struct ifnet *
if_alloc(u_char type);
void
if_attach(struct ifnet *ifp);
void
if_detach(struct ifnet *ifp);
void
if_free(struct ifnet *ifp);
void
if_free_type(struct ifnet *ifp, u_char type);
void
if_down(struct ifnet *ifp);
int
ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td);
int
ifpromisc(struct ifnet *ifp, int pswitch);
int
if_allmulti(struct ifnet *ifp, int amswitch);
struct ifnet *
ifunit(const char *name);
struct ifnet *
ifunit_ref(const char *name);
void
if_up(struct ifnet *ifp);
๐ Interface Address Functions ๐
address functions, for when you need to deal with addresses:
struct ifaddr *
ifaddr_byindex(u_short idx);
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, struct ifnet *ifp);
void
ifa_ref(struct ifaddr *ifa);
void
ifa_free(struct ifaddr *ifa);
๐ Interface Multicast Address Functions ๐
multicast? we love a good multicast moment:
int
if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
struct ifmultiaddr **ifmap);
int
if_delmulti(struct ifnet *ifp, struct sockaddr *sa);
struct ifmultiaddr *
if_findmulti(struct ifnet *ifp, struct sockaddr *sa);
๐ซถ Output queue macros ๐ซถ
just one macro here, but it's iconic:
IF_DEQUEUE(struct ifqueue *ifq, struct mbuf *m);
๐ struct ifnet Member Functions ๐
these are the function pointers you can set in your struct ifnet. they're the main character energy of the interface:
void
(*if_input)(struct ifnet *ifp, struct mbuf *m);
int
(*if_output)(struct ifnet *ifp, struct mbuf *m,
const struct sockaddr *dst, struct route *ro);
void
(*if_start)(struct ifnet *ifp);
int
(*if_transmit)(struct ifnet *ifp, struct mbuf *m);
void
(*if_qflush)(struct ifnet *ifp);
int
(*if_ioctl)(struct ifnet *ifp, u_long cmd, caddr_t data);
void
(*if_init)(void *if_softc);
int
(*if_resolvemulti)(struct ifnet *ifp, struct sockaddr **retsa,
struct sockaddr *addr);
๐ง struct ifaddr member function ๐ง
one function for ifaddr:
void
(*ifa_rtrequest)(int cmd, struct rtentry *rt, struct rt_addrinfo *info);
๐ Global Variables ๐
these are the globals, they're kind of a big deal:
extern struct ifnethead ifnet;
extern int if_index;
extern int ifqmaxlen;
๐ DATA STRUCTURES ๐
okay so now we get into the actual structures. the kernel's whole setup for handling network interfaces lives in these four structures: ifnet, if_data, ifaddr, and ifmultiaddr. you'll find them in <net/if.h> and <net/if_var.h>, and the functions are defined in /sys/net/if.c. the user-facing stuff is in <net/if.h> โ that's where the interface flags, the if_data structure, and the structures that define how interface-related messages look on the route(4) routing socket and in sysctl(3) are. the kernel-internal interfaces are in <net/if_var.h> โ that includes the ifnet, ifaddr, and ifmultiaddr structures and the functions that manipulate them. (a few user programs will need <net/if_var.h> because it's a prerequisite for other headers like <netinet/if_ether.h>. most references to those two files can be replaced by <net/ethernet.h>.)
the system keeps a linked list of interfaces using the TAILQ macros from queue(3). the head of this list is a struct ifnethead called ifnet. the elements are struct ifnet, and most kernel routines that manipulate interfaces 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)) that describes the link layer implemented by the interface (if any) is accessed by the ifaddr_byindex() function or the if_addr structure. (some trivial interfaces don't provide any link layer addresses; this structure, while still present, serves only to identify the interface name and index.)
finally, those interfaces that support 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 Structure ๐ฆ
the fields of struct ifnet are literally all these, bestie:
if_softc(void *) โ A pointer to the driver's private state block. (Initialized by driver.)if_l2com(void *) โ A pointer to the common data for the interface's layer 2 protocol. (Initialized byif_alloc().)if_vnet(struct vnet *) โ A pointer to the virtual network stack instance. (Initialized byif_attach().)if_home_vnet(struct vnet *) โ A pointer to the parent virtual network stack, where thisstruct ifnetoriginates from. (Initialized byif_attach().)if_link(TAILQ_ENTRY(ifnet)) โqueue(3)macro glue.if_xname(char *) โ The name of the interface, (e.g.,"fxp0"or"lo0"). (Initialized by driver (usually viaif_initname()).)if_dname(const char *) โ The name of the driver. (Initialized by driver (usually viaif_initname()).)if_dunit(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_refcount(u_int) โ The reference count. (Initialized byif_alloc().)if_addrhead(struct ifaddrhead) โ The head of thequeue(3)TAILQcontaining the list of addresses assigned to this interface.if_pcount(int) โ A count of promiscuous listeners on this interface, used to reference-count theIFF_PROMISCflag.if_carp(struct carp_if *) โ A pointer to the CARP interface structure,carp(4). (Initialized by the driver-specificif_ioctl()routine.)if_bpf(struct bpf_if *) โ Opaque per-interface data for the packet filter,bpf(4). (Initialized bybpf_attach().)if_index(u_short) โ A unique number assigned to each interface in sequence as it is attached. This number can be used in astruct sockaddr_dlto refer to a particular interface by index (seelink_addr(3)). (Initialized byif_alloc().)if_vlantrunk(struct ifvlantrunk *) โ A pointer to 802.1Q trunk structure,vlan(4). (Initialized by the driver-specificif_ioctl()routine.)if_flags(int) โ Flags describing operational parameters of this interface (see below). (Manipulated by generic code.)if_drv_flags(int) โ Flags describing operational status of this interface (see below). (Manipulated by driver.)if_capabilities(int) โ Flags describing the capabilities the interface supports (see below).if_capenable(int) โ Flags describing the enabled capabilities of the interface (see below).if_linkmib(void *) โ A pointer to an interface-specific MIB structure exported byifmib(4). (Initialized by driver.)if_linkmiblen(size_t) โ The size of said structure. (Initialized by driver.)if_data(struct if_data) โ More statistics and information; see โThe if_data structureโ, below. (Initialized by driver, manipulated by both driver and generic code.)if_multiaddrs(struct ifmultihead) โ The head of thequeue(3)TAILQcontaining the list of multicast addresses assigned to this interface.if_amcount(int) โ A number of multicast requests on this interface, used to reference-count theIFF_ALLMULTIflag.if_addr(struct ifaddr *) โ A pointer to the link-level interface address. (Initialized byif_alloc().)if_snd(struct ifaltq) โ The output queue. (Manipulated by driver.)if_broadcastaddr(const u_int8_t *) โ A link-level broadcast bytestring for protocols with variable address length.if_bridge(void *) โ A pointer to the bridge interface structure,if_bridge(4). (Initialized by the driver-specificif_ioctl()routine.)if_label(struct label *) โ A pointer to the MAC Framework label structure,mac(4). (Initialized byif_alloc().)if_afdata(void *) โ An address family dependent data region.if_afdata_initialized(int) โ Used to track the current state of address family initialization.if_afdata_lock(struct rwlock) โ Anrwlock(9)lock used to protectif_afdatainternals.if_linktask(struct task) โ Ataskqueue(9)task scheduled for link state change events of the interface.if_addr_lock(struct rwlock) โ Anrwlock(9)lock used to protect interface-related address lists.if_clones(LIST_ENTRY(ifnet)) โqueue(3)macro glue for the list of clonable network interfaces.if_groups(TAILQ_HEAD(, ifg_list)) โ bestie this is literally the head of thequeue(3)TAILQthat holds the list of groups per interface. group chat, but for interfaces ๐if_pf_kif(void *) โ a pointer to the structure thatpf(4)uses for interface abstraction.if_lagg(void *) โ a pointer to thelagg(4)interface structure.if_alloctype(u_char) โ the type of the interface as it was at the time of its allocation. It's there to cache the type that got passed toif_alloc(), and โ plot twist โ unlikeif_type, drivers don't get to change it. periodt.
References to ifnet structures are gained by calling the if_ref() function and released by calling the if_rele() function. They exist so kernel code that's out here walking global interface lists can release the ifnet lock yet keep the ifnet structure stable. iconic behaviour honestly.
There are also a bunch of function pointers that a driver must initialize to complete its interface with the generic interface layer. no skipping these, they're mandatory fr fr:
if_input()โ Pass a packet to an appropriate upper layer, as determined from the link-layer header of the packet. This routine is to be called from an interrupt handler, or used to emulate reception of a packet on this interface. A single function implementingif_input()can be shared among multiple drivers utilizing the same link-layer framing, e.g., Ethernet. sharing is caring โจ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. This function will returnENOBUFSif the device's software and hardware queues are both full. This function must be installed afterif_attach()to override the default implementation. It's exposed so drivers can manage their own queues and reduce the latency caused by a frequently gratuitous enqueue / dequeue pair toifq. The suggested internal software queuing mechanism isbuf_ring.if_qflush()โ Free mbufs in internally managed queues when the interface is marked down. This function must be installed afterif_attach()to override the default implementation. It's exposed so drivers can manage their own queues and reduce the latency caused by a frequently gratuitous enqueue / dequeue pair toifq. The suggested internal software queuing mechanism isbuf_ring. (yes bestie, same energy asif_transmit(), the manpage said it twice, so I'm telling you twice ๐)if_start()โ Start queued output on an interface. This function is exposed to provide for some interface classes to share anif_output()among all drivers.if_start()may only be called when theIFF_DRV_OACTIVEflag is not set. (So lowkeyIFF_DRV_OACTIVEdoes not literally mean output is active โ it means the device's internal output queue is full.) ๐ Please note that this function will soon be deprecated.if_ioctl()โ Process interface-relatedioctl(2)requests (defined in<sys/sockio.h>). Preliminary processing is done by the generic routineifioctl()to check for appropriate privileges, locate the interface being manipulated, and perform certain generic operations like twiddling flags and flushing queues. See the description ofifioctl()below for more information.if_init()โ Initialize and bring up the hardware, e.g., reset the chip and enable the receiver unit. Should mark the interface running, but not active (IFF_DRV_RUNNING,~IIF_DRV_OACTIVE).if_resolvemulti()โ Check the requested multicast group membership, addr, for validity, and if necessary compute a link-layer group which corresponds to that address, which is returned in*retsa. Returns zero on success, or an error code on failure.
๐ฉ Interface Flags ๐ฉ
Interface flags are used for a number of different purposes. Some flags simply indicate information about the type of interface and its capabilities; others are dynamically manipulated to reflect the current state of the interface. Flags of the former kind are marked โจSโฉ in this table; the latter are marked โจDโฉ. Flags which begin with โIFF_DRV_โ are stored in if_drv_flags; all other flags are stored in if_flags.
The macro IFF_CANTCHANGE defines the bits which cannot be set by a user program using the SIOCSIFFLAGS command to ioctl(2); these are indicated by an asterisk (โ*โ) in the following listing. so like, don't try it ๐
IFF_UPโจDโฉ โ The interface has been configured up by the 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 the permanently promiscuous mode (impliesIFF_PROMISC).IFF_ALLMULTIโจD*โฉ โ This interface is in all-multicasts mode (used by multicast routers).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. relatable honestly ๐ญIFF_LINK0IFF_LINK1IFF_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. main character energy: watching everything, saying nothing ๐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. it's giving... expiry date ๐IFF_RENAMINGโจDโฉ โ Set when this interface is being renamed.
โจ Interface Capabilities Flags โจ
Interface capabilities are specialized features an interface may or may not support. These capabilities are very hardware-specific, and allow โ when enabled โ offloading specific network processing to the interface, or offering 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).
bestie. do NOT just go poking atif_capenableyourself. that's not your lane, that's the driver's lane. useSIOCSIFCAP. ๐ซถ
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. big frames, big slay ๐ฅ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 TCP offloading.IFCAP_TOE6โ This Ethernet interface supports TCP6 offloading.IFCAP_TOEโ A shorthand for(IFCAP_TOE4 | IFCAP_TOE6).IFCAP_WOL_UCASTโ bestie, this Ethernet interface can wake up on any Unicast packet. iconic behavior fr. ๐คโจIFCAP_WOL_MCASTโ same energy but for any Multicast packet. she hears everything.IFCAP_WOL_MAGICโ wakes up on any Magic packet, like the oneswake(8)sends. it's giving summoned by spell. ๐ชIFCAP_WOLโ a lil shorthand for(IFCAP_WOL_UCAST | IFCAP_WOL_MCAST | IFCAP_WOL_MAGIC). one flag to rule them all.IFCAP_TOE4โ this Ethernet interface supports TCP4 Offload Engine. CPU said "not my job."IFCAP_TOE6โ same but TCP6 Offload Engine. equal opportunity offloading. ๐ซถIFCAP_TOEโ shorthand for(IFCAP_TOE4 | IFCAP_TOE6). slay.IFCAP_VLAN_HWFILTERโ supports frame filtering in hardware onvlan(4)interfaces. hardware doing the heavy lifting, we love to see it.IFCAP_VLAN_HWTSOโ supports TCP Segmentation offloading onvlan(4)interfaces (and yes, it impliesIFCAP_TSO). lowkey doing two jobs at once.IFCAP_LINKSTATEโ this Ethernet interface supports dynamic link state changes. she can change, okay?? growth. ๐ฑIFCAP_NETMAPโ supportsnetmap(4). fancy.
The ability of advanced network interfaces to offload certain computational tasks from the host CPU to the board is limited mostly to TCP/IP. So, like, a separate field associated with an interface (peep ifnet.if_data.ifi_hwassist down below ๐) keeps a detailed description of its enabled capabilities specific to TCP/IP processing. The TCP/IP module consults that 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. iykyk. ๐จ
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. (not the number of vibes, the actual packets. ๐
)
๐ธ The if_data Structure ๐ธ
Okay so 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. girl was announced and then ghosted. ๐ญifi_addrlen(u_char) โ length of a link-layer address on this device, or zero if there are none. Used to initialized 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 maximum transmission unit of the medium, exclusive of any link-layer overhead.ifi_metric(u_long) โ a dimensionless metric interpreted by a user-mode routing process.ifi_baudrate(u_long) โ the line rate of the interface, in bits per second. fast girl. ๐โโ๏ธifi_hwassist(u_long) โ a detailed interpretation of the capabilities to offload computational tasks for outgoing packets. The interface driver must keep this field in accord with the current value ofif_capenable.ifi_epoch(time_t) โ the system uptime when interface was attached or the statistics below were reset. This is intended to be used to set the SNMP variableifCounterDiscontinuityTime. It may also be used to determine if two successive queries for an interface of the same index have returned results for the same interface.
The structure additionally contains generic statistics applicable to a variety of different interface types (except as noted, all members are of type u_long):
ifi_link_state(u_char) โ the current link state of Ethernet interfaces. See 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.) the square brackets are doing so much work here ๐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. mystery era. ๐ฎLINK_STATE_DOWNโ the link is down. ๐LINK_STATE_UPโ the link is up. WE'RE SO BACK. ๐ฅ
๐ 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. (she's counting her references, as she should ๐
)
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.) one field, two personalities.ifa_netmask(struct sockaddr *) โ the network mask for multi-access interfaces, and the confusion generator for point-to-point interfaces. ๐ตโ๐ซifa_ifp(struct ifnet *) โ 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) โ bestie, itโs literally the reference count. ๐
References to ifaddr structures are gained by calling the ifa_ref() function and released by calling the ifa_free() function. No cap. ๐ฆ
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. Periodt. โจ
โจ 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. iykyk ๐
๐ 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_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 theifnetstructure and is accessed by theifaddr_byindex()function.) Theifpmust have been allocated byif_alloc(). ๐ฆ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 theifnet. ๐ซถifioctl()โ Process theioctlrequestcmd, issued onsocketsoby threadtd, with data parameterdata. This is the main routine for handling all interface configuration requests from user mode. It is ordinarily only called from the socket-layerioctl(2)handler, and only for commands with class โiโ. Any unrecognized commands will be passed down tosocketso's protocol for further interpretation. The following commands are handled byifioctl(): ๐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,SIOCGIFFIB,SIOCGIFFLAGS,SIOCGIFMETRIC,SIOCGIFMTU,SIOCGIFPHYSโ Get interface capabilities, FIB, flags, metric, MTU, 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. ๐ฅSIOCSIFFIBโ Sets interface FIB. Caller must have appropriate privilege. FIB values start at0and values greater or equals thannet.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,SIOCSIFPHYSโ Change interface metric or 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,SIOCDELMULTIโ Add or delete permanent multicast group memberships on the interface. Caller must have appropriate privilege. Theif_addmulti()orif_delmulti()function is called to perform the operation; qq.v. ๐ฌSIOCAIFADDR,SIOCDIFADDRโ The socket's protocol control routine is called to implement the requested action. ๐ง
if_down(),ifioctl(),ifpromisc(), andif_up()must be called atsplnet()or higher. โ ๏ธ
๐บ๏ธ Interface Address Functions ๐บ๏ธ
Several functions exist to look up an interface address structure given an address. 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. ๐
ifaddr_byindex() returns the link-level address of the interface with the given index idx. ๐ฏ
All of these functions return a null pointer if no such address can be found. ๐
๐ฌ 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: ๐ฆ
- Hit up the interface's
if_resolvemulti()entry point to figure out the link-layer address (if there even is one, bestie ๐ ) that matches this membership request โ and also to let the link layer veto the whole thing if it's feeling some type of way about it. It's giving "we asked, you can say no." - Now go check the interface's group membership list for a membership that already exists for this group. Didn't find one? Slay, allocate a fresh new one. Found one? Cool, just increment its reference count. No drama.
- If
if_resolvemulti()handed back a link-layer address that corresponds to the group, then run that exact same previous step for that address too. Same energy, different address, periodt. - If the interface's multicast address filter needs to be changed because a new membership just got added, then call the interface's
if_ioctl()routine (with acmdargument ofSIOCADDMULTI) to politely request that it go ahead and do that. โจ
The if_delmulti() function, when you hand it an interface ifp and an address sa, just runs that whole process backwards. Undoing, iconic behaviour honestly. Both functions return zero on success, or a standard error number on failure. ๐ซถ
The if_findmulti() function digs through the membership list of interface ifp looking for an address that matches sa, and returns a pointer to that struct ifmultiaddr if it finds one. If not? It returns a null pointer. Sorry bestie, no results. ๐ญ
๐ 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)
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. Absolute legend, no cap. ๐
And that's a wrap on the whole page, bestie! This one shipped with Debian's manpage set, dated November 14, 2018 โ go off, historic documentation. Study it, love it, and may your interfaces always resolve their multis correctly. ๐ฆโจ
๐ฆ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 ยท BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 ยท BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 ยท BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 ยท BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 ยท BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 ยท BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright 1996, 1997 Massachusetts Institute of Technology
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms