1.\" -*- Nroff -*- 2.\" Copyright 1996, 1997 Massachusetts Institute of Technology 3.\" 4.\" Permission to use, copy, modify, and distribute this software and 5.\" its documentation for any purpose and without fee is hereby 6.\" granted, provided that both the above copyright notice and this 7.\" permission notice appear in all copies, that both the above 8.\" copyright notice and this permission notice appear in all 9.\" supporting documentation, and that the name of M.I.T. not be used 10.\" in advertising or publicity pertaining to distribution of the 11.\" software without specific, written prior permission. M.I.T. makes 12.\" no representations about the suitability of this software for any 13.\" purpose. It is provided "as is" without express or implied 14.\" warranty. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 17.\" ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 18.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 20.\" SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22.\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 23.\" USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24.\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27.\" SUCH DAMAGE. 28.\" 29.\" $FreeBSD$ 30.\" 31.Dd March 14, 2007 32.Os 33.Dt IFNET 9 34.Sh NAME 35.Nm ifnet , 36.Nm ifaddr , 37.Nm ifqueue , 38.Nm if_data 39.Nd kernel interfaces for manipulating network interfaces 40.Sh SYNOPSIS 41.In sys/param.h 42.In sys/time.h 43.In sys/socket.h 44.In net/if.h 45.In net/if_var.h 46.In net/if_types.h 47.\" 48.Ss "Interface Manipulation Functions" 49.Ft "struct ifnet *" 50.Fn if_alloc "u_char type" 51.Ft void 52.Fn if_attach "struct ifnet *ifp" 53.Ft void 54.Fn if_detach "struct ifnet *ifp" 55.Ft void 56.Fn if_free "struct ifnet *ifp" 57.Ft void 58.Fn if_free_type "struct ifnet *ifp" "u_char type" 59.Ft void 60.Fn if_down "struct ifnet *ifp" 61.Ft int 62.Fn ifioctl "struct socket *so" "u_long cmd" "caddr_t data" "struct thread *td" 63.Ft int 64.Fn ifpromisc "struct ifnet *ifp" "int pswitch" 65.Ft int 66.Fn if_allmulti "struct ifnet *ifp" "int amswitch" 67.Ft "struct ifnet *" 68.Fn ifunit "const char *name" 69.Ft void 70.Fn if_up "struct ifnet *ifp" 71.\" 72.Ss "Interface Address Functions" 73.Ft "struct ifaddr *" 74.Fn ifa_ifwithaddr "struct sockaddr *addr" 75.Ft "struct ifaddr *" 76.Fn ifa_ifwithdstaddr "struct sockaddr *addr" 77.Ft "struct ifaddr *" 78.Fn ifa_ifwithnet "struct sockaddr *addr" 79.Ft "struct ifaddr *" 80.Fn ifaof_ifpforaddr "struct sockaddr *addr" "struct ifnet *ifp" 81.Ft void 82.Fn ifafree "struct ifaddr *ifa" 83.Fn IFAFREE "struct ifaddr *ifa" 84.\" 85.Ss "Interface Multicast Address Functions" 86.Ft int 87.Fn if_addmulti "struct ifnet *ifp" "struct sockaddr *sa" "struct ifmultiaddr **ifmap" 88.Ft int 89.Fn if_delmulti "struct ifnet *ifp" "struct sockaddr *sa" 90.Ft "struct ifmultiaddr *" 91.Fn ifmaof_ifpforaddr "struct sockaddr *addr" "struct ifnet *ifp" 92.Ss "Output queue macros" 93.Fn IF_DEQUEUE "struct ifqueue *ifq" "struct mbuf *m" 94.\" 95.Ss "struct ifnet Member Functions" 96.Ft void 97.Fn \*(lp*if_input\*(rp "struct ifnet *ifp" "struct mbuf *m" 98.Ft int 99.Fo \*(lp*if_output\*(rp 100.Fa "struct ifnet *ifp" "struct mbuf *m" 101.Fa "struct sockaddr *dst" "struct rtentry *rt" 102.Fc 103.Ft void 104.Fn \*(lp*if_start\*(rp "struct ifnet *ifp" 105.Ft int 106.Fn \*(lp*if_transmit\*(rp "struct ifnet *ifp" "struct mbuf *m" 107.Ft void 108.Fn \*(lp*if_qflush\*(rp "struct ifnet *ifp" 109.Ft int 110.Fn \*(lp*if_ioctl\*(rp "struct ifnet *ifp" "int cmd" "caddr_t data" 111.Ft void 112.Fn \*(lp*if_watchdog\*(rp "struct ifnet *ifp" 113.Ft void 114.Fn \*(lp*if_init\*(rp "void *if_softc" 115.Ft int 116.Fo \*(lp*if_resolvemulti\*(rp 117.Fa "struct ifnet *ifp" "struct sockaddr **retsa" "struct sockaddr *addr" 118.Fc 119.Ss "struct ifaddr member function" 120.Ft void 121.Fo \*(lp*ifa_rtrequest\*(rp 122.Fa "int cmd" "struct rtentry *rt" "struct sockaddr *dst" 123.Fc 124.\" 125.Ss "Global Variables" 126.Vt extern struct ifnethead ifnet ; 127.Vt extern struct ifaddr **ifnet_addrs ; 128.Vt extern int if_index ; 129.Vt extern int ifqmaxlen ; 130.Sh DATA STRUCTURES 131The kernel mechanisms for handling network interfaces reside primarily 132in the 133.Vt ifnet , if_data , ifaddr , 134and 135.Vt ifmultiaddr 136structures in 137.In net/if.h 138and 139.In net/if_var.h 140and the functions named above and defined in 141.Pa /sys/net/if.c . 142Those interfaces which are intended to be used by user programs 143are defined in 144.In net/if.h ; 145these include the interface flags, the 146.Vt if_data 147structure, and the structures defining the appearance of 148interface-related messages on the 149.Xr route 4 150routing socket and in 151.Xr sysctl 3 . 152The header file 153.In net/if_var.h 154defines the kernel-internal interfaces, including the 155.Vt ifnet , ifaddr , 156and 157.Vt ifmultiaddr 158structures and the functions which manipulate them. 159(A few user programs will need 160.In net/if_var.h 161because it is the prerequisite of some other header file like 162.In netinet/if_ether.h . 163Most references to those two files in particular can be replaced by 164.In net/ethernet.h . ) 165.Pp 166The system keeps a linked list of interfaces using the 167.Li TAILQ 168macros defined in 169.Xr queue 3 ; 170this list is headed by a 171.Vt "struct ifnethead" 172called 173.Va ifnet . 174The elements of this list are of type 175.Vt "struct ifnet" , 176and most kernel routines which manipulate interface as such accept or 177return pointers to these structures. 178Each interface structure 179contains an 180.Vt if_data 181structure, which contains statistics and identifying information used 182by management programs, and which is exported to user programs by way 183of the 184.Xr ifmib 4 185branch of the 186.Xr sysctl 3 187MIB. 188Each interface also has a 189.Li TAILQ 190of interface addresses, described by 191.Vt ifaddr 192structures; the head of the queue is always an 193.Dv AF_LINK 194address 195(see 196.Xr link_addr 3 ) 197describing the link layer implemented by the interface (if any). 198(Some trivial interfaces do not provide any link layer addresses; 199this structure, while still present, serves only to identify the 200interface name and index.) 201.Pp 202Finally, those interfaces supporting reception of multicast datagrams 203have a 204.Li TAILQ 205of multicast group memberships, described by 206.Vt ifmultiaddr 207structures. 208These memberships are reference-counted. 209.Pp 210Interfaces are also associated with an output queue, defined as a 211.Vt "struct ifqueue" ; 212this structure is used to hold packets while the interface is in the 213process of sending another. 214.Pp 215.Ss The Vt ifnet Ss structure 216The fields of 217.Vt "struct ifnet" 218are as follows: 219.Bl -tag -width ".Va if_capabilities" -offset indent 220.It Va if_softc 221.Pq Vt "void *" 222A pointer to the driver's private state block. 223(Initialized by driver.) 224.It Va if_l2com 225.Pq Vt "void *" 226A pointer to the common data for the interface's layer 2 protocol. 227(Initialized by 228.Fn if_alloc . ) 229.It Va if_link 230.Pq Fn TAILQ_ENTRY ifnet 231.Xr queue 3 232macro glue. 233.It Va if_xname 234.Pq Vt "char *" 235The name of the interface, 236(e.g., 237.Dq Li fxp0 238or 239.Dq Li lo0 ) . 240(Initialized by driver.) 241.It Va if_dname 242.Pq Vt "const char *" 243The name of the driver. 244(Initialized by driver.) 245.It Va if_dunit 246.Pq Vt int 247A unique number assigned to each interface managed by a particular 248driver. 249Drivers may choose to set this to 250.Dv IF_DUNIT_NONE 251if a unit number is not associated with the device. 252(Initialized by driver.) 253.It Va if_addrhead 254.Pq Vt "struct ifaddrhead" 255The head of the 256.Xr queue 3 257.Li TAILQ 258containing the list of addresses assigned to this interface. 259.It Va if_pcount 260.Pq Vt int 261A count of promiscuous listeners on this interface, used to 262reference-count the 263.Dv IFF_PROMISC 264flag. 265.It Va if_bpf 266.Pq Vt "struct bpf_if *" 267Opaque per-interface data for the packet filter, 268.Xr bpf 4 . 269(Initialized by 270.Fn bpf_attach . ) 271.It Va if_index 272.Pq Vt u_short 273A unique number assigned to each interface in sequence as it is 274attached. 275This number can be used in a 276.Vt "struct sockaddr_dl" 277to refer to a particular interface by index 278(see 279.Xr link_addr 3 ) . 280(Initialized by 281.Fn if_alloc . ) 282.It Va if_timer 283.Pq Vt short 284Number of seconds until the watchdog timer 285.Fn if_watchdog 286is called, or zero if the timer is disabled. 287(Set by driver, 288decremented by generic watchdog code.) 289.It Va if_flags 290.Pq Vt int 291Flags describing operational parameters of this interface (see below). 292(Manipulated by both driver and generic code.) 293.It Va if_capabilities 294.Pq Vt int 295Flags describing the capabilities the interface supports (see below). 296.It Va if_capenable 297.Pq Vt int 298Flags describing the enabled capabilities of the interface (see below). 299.\" .It Va if_ipending 300.\" Interrupt-pending bits for polled operation: 301.\" .Dv IFI_XMIT 302.\" (transmit complete interrupt) 303.\" and 304.\" .Dv IFI_RECV 305.\" (received packet ready interrupt). 306.\" See the 307.\" .Sx Polling 308.\" section, below. 309.\" (Manipulated by driver.) 310.It Va if_linkmib 311.Pq Vt "void *" 312A pointer to an interface-specific MIB structure exported by 313.Xr ifmib 4 . 314(Initialized by driver.) 315.It Va if_linkmiblen 316.Pq Vt size_t 317The size of said structure. 318(Initialized by driver.) 319.It Va if_data 320.Pq Vt "struct if_data" 321More statistics and information; see 322.Sx "The if_data structure" , 323below. 324(Initialized by driver, manipulated by both driver and generic 325code.) 326.It Va if_snd 327.Pq Vt "struct ifqueue" 328The output queue. 329(Manipulated by driver.) 330.\".It Va if_poll_slowq 331.\".Pq Vt "struct ifqueue *" 332.\"A pointer to the input queue for devices which do not support polling 333.\"well. 334.\"See the 335.\".Sx Polling 336.\"section, below. 337.\"(Initialized by driver.) 338.El 339.Pp 340There are in addition a number of function pointers which the driver 341must initialize to complete its interface with the generic interface 342layer: 343.Bl -ohang -offset indent 344.It Fn if_input 345Pass a packet to an appropriate upper layer as determined 346from the link-layer header of the packet. 347This routine is to be called from an interrupt handler or 348used to emulate reception of a packet on this interface. 349A single function implementing 350.Fn if_input 351can be shared among multiple drivers utilizing the same link-layer 352framing, e.g., Ethernet. 353.It Fn if_output 354Output a packet on interface 355.Fa ifp , 356or queue it on the output queue if the interface is already active. 357.It Fn if_transmit 358Transmit a packet on an interface or queue it if the interface is 359in use. This function will return 360.Dv ENOBUFS 361if the devices software and hardware queues are both full. This 362function must be installed after 363.It Fn if_qflush 364Free mbufs in internally managed queues when the interface is marked down. 365This function must be installed after 366.Fn if_attach 367to override the default implementation. This function is exposed in order 368to allow drivers to manage their own queues and to reduce the latency 369caused by a frequently gratuitous enqueue / dequeue pair to ifq. The 370suggested internal software queueing mechanism is buf_ring. 371.It Fn if_start 372Start queued output on an interface. 373This function is exposed in 374order to provide for some interface classes to share a 375.Fn if_output 376among all drivers. 377.Fn if_start 378may only be called when the 379.Dv IFF_OACTIVE 380flag is not set. 381(Thus, 382.Dv IFF_OACTIVE 383does not literally mean that output is active, but rather that the 384device's internal output queue is full.) Please note that this function 385will soon be deprecated. 386.It Fn if_done 387Not used. 388We are not even sure what it was ever for. 389The prototype is faked. 390.It Fn if_ioctl 391Process interface-related 392.Xr ioctl 2 393requests 394(defined in 395.In sys/sockio.h ) . 396Preliminary processing is done by the generic routine 397.Fn ifioctl 398to check for appropriate privileges, locate the interface being 399manipulated, and perform certain generic operations like twiddling 400flags and flushing queues. 401See the description of 402.Fn ifioctl 403below for more information. 404.It Fn if_watchdog 405Routine called by the generic code when the watchdog timer, 406.Va if_timer , 407expires. 408Usually this will reset the interface. 409.\" .It Fn if_poll_recv 410.\" .It Fn if_poll_xmit 411.\" .It Fn if_poll_slowinput 412.\" .It Fn if_poll_intren 413.\" See the 414.\" .Sx Polling 415.\" section, below. 416.It Fn if_init 417Initialize and bring up the hardware, 418e.g., reset the chip and the watchdog timer and enable the receiver unit. 419Should mark the interface running, 420but not active 421.Dv ( IFF_RUNNING , ~IIF_OACTIVE ) . 422.It Fn if_resolvemulti 423Check the requested multicast group membership, 424.Fa addr , 425for validity, and if necessary compute a link-layer group which 426corresponds to that address which is returned in 427.Fa *retsa . 428Returns zero on success, or an error code on failure. 429.El 430.Ss "Interface Flags" 431Interface flags are used for a number of different purposes. 432Some 433flags simply indicate information about the type of interface and its 434capabilities; others are dynamically manipulated to reflect the 435current state of the interface. 436Flags of the former kind are marked 437.Aq S 438in this table; the latter are marked 439.Aq D . 440.Pp 441The macro 442.Dv IFF_CANTCHANGE 443defines the bits which cannot be set by a user program using the 444.Dv SIOCSIFFLAGS 445command to 446.Xr ioctl 2 ; 447these are indicated by an asterisk 448.Pq Ql * 449in the following listing. 450.Pp 451.Bl -tag -width ".Dv IFF_POINTOPOINT" -offset indent -compact 452.It Dv IFF_UP 453.Aq D 454The interface has been configured up by the user-level code. 455.It Dv IFF_BROADCAST 456.Aq S* 457The interface supports broadcast. 458.It Dv IFF_DEBUG 459.Aq D 460Used to enable/disable driver debugging code. 461.It Dv IFF_LOOPBACK 462.Aq S 463The interface is a loopback device. 464.It Dv IFF_POINTOPOINT 465.Aq S* 466The interface is point-to-point; 467.Dq broadcast 468address is actually the address of the other end. 469.It Dv IFF_RUNNING 470.Aq D* 471The interface has been configured and dynamic resources were 472successfully allocated. 473Probably only useful internal to the 474interface. 475.It Dv IFF_NOARP 476.Aq D 477Disable network address resolution on this interface. 478.It Dv IFF_PROMISC 479.Aq D* 480This interface is in promiscuous mode. 481.It Dv IFF_PPROMISC 482.Aq D 483This interface is in the permanently promiscuous mode (implies 484.Dv IFF_PROMISC ) . 485.It Dv IFF_ALLMULTI 486.Aq D* 487This interface is in all-multicasts mode (used by multicast routers). 488.It Dv IFF_OACTIVE 489.Aq D* 490The interface's hardware output queue (if any) is full; output packets 491are to be queued. 492.It Dv IFF_SIMPLEX 493.Aq S* 494The interface cannot hear its own transmissions. 495.It Dv IFF_LINK0 496.It Dv IFF_LINK1 497.It Dv IFF_LINK2 498.Aq D 499Control flags for the link layer. 500(Currently abused to select among 501multiple physical layers on some devices.) 502.It Dv IFF_MULTICAST 503.Aq S* 504This interface supports multicast. 505.It Dv IFF_POLLING 506.Aq D* 507The interface is in 508.Xr polling 4 509mode. 510See 511.Sx Interface Capabilities Flags 512for details. 513.El 514.Ss "Interface Capabilities Flags" 515Interface capabilities are specialized features an interface may 516or may not support. 517These capabilities are very hardware-specific 518and allow, when enabled, 519to offload specific network processing to the interface 520or to offer a particular feature for use by other kernel parts. 521.Pp 522It should be stressed that a capability can be completely 523uncontrolled (i.e., stay always enabled with no way to disable it) 524or allow limited control over itself (e.g., depend on another 525capability's state.) 526Such peculiarities are determined solely by the hardware and driver 527of a particular interface. 528Only the driver possesses 529the knowledge on whether and how the interface capabilities 530can be controlled. 531Consequently, capabilities flags in 532.Va if_capenable 533should never be modified directly by kernel code other than 534the interface driver. 535The command 536.Dv SIOCSIFCAP 537to 538.Fn ifioctl 539is the dedicated means to attempt altering 540.Va if_capenable 541on an interface. 542Userland code shall use 543.Xr ioctl 2 . 544.Pp 545The following capabilities are currently supported by the system: 546.Bl -tag -width ".Dv IFCAP_VLAN_HWTAGGING" -offset indent 547.It Dv IFCAP_NETCONS 548This interface can be a network console. 549.It Dv IFCAP_POLLING 550This interface supports 551.Xr polling 4 . 552See below for details. 553.It Dv IFCAP_RXCSUM 554This interface can do checksum validation on receiving data. 555Some interfaces do not have sufficient buffer storage to store frames 556above a certain MTU-size completely. 557The driver for the interface might disable hardware checksum validation 558if the MTU is set above the hardcoded limit. 559.It Dv IFCAP_TXCSUM 560This interface can do checksum calculation on transmitting data. 561.It Dv IFCAP_HWCSUM 562A shorthand for 563.Pq Dv IFCAP_RXCSUM | IFCAP_TXCSUM . 564.It Dv IFCAP_VLAN_HWTAGGING 565This interface can do VLAN tagging on output and 566demultiplex frames by their VLAN tag on input. 567.It Dv IFCAP_VLAN_MTU 568The 569.Xr vlan 4 570driver can operate over this interface in software tagging mode 571without having to decrease MTU on 572.Xr vlan 4 573interfaces below 1500 bytes. 574This implies the ability of this interface to cope with frames somewhat 575longer than permitted by the Ethernet specification. 576.It Dv IFCAP_JUMBO_MTU 577This Ethernet interface can transmit and receive frames up to 5789000 bytes long. 579.El 580.Pp 581The ability of advanced network interfaces to offload certain 582computational tasks from the host CPU to the board is limited 583mostly to TCP/IP. 584Therefore a separate field associated with an interface 585(see 586.Va ifnet.if_data.ifi_hwassist 587below) 588keeps a detailed description of its enabled capabilities 589specific to TCP/IP processing. 590The TCP/IP module consults the field to see which tasks 591can be done on an 592.Em outgoing 593packet by the interface. 594The flags defined for that field are a superset of those for 595.Va mbuf.m_pkthdr.csum_flags , 596namely: 597.Bl -tag -width ".Dv CSUM_FRAGMENT" -offset indent 598.It Dv CSUM_IP 599The interface will compute IP checksums. 600.It Dv CSUM_TCP 601The interface will compute TCP checksums. 602.It Dv CSUM_UDP 603The interface will compute UDP checksums. 604.It Dv CSUM_IP_FRAGS 605The interface can compute a TCP or UDP checksum for a packet 606fragmented by the host CPU. 607Makes sense only along with 608.Dv CSUM_TCP 609or 610.Dv CSUM_UDP . 611.It Dv CSUM_FRAGMENT 612The interface will do the fragmentation of IP packets if necessary. 613The host CPU does not need to care about MTU on this interface 614as long as a packet to transmit through it is an IP one and it 615does not exceed the size of the hardware buffer. 616.El 617.Pp 618An interface notifies the TCP/IP module about the tasks 619the former has performed on an 620.Em incoming 621packet by setting the corresponding flags in the field 622.Va mbuf.m_pkthdr.csum_flags 623of the 624.Vt mbuf chain 625containing the packet. 626See 627.Xr mbuf 9 628for details. 629.Pp 630The capability of a network interface to operate in 631.Xr polling 4 632mode involves several flags in different 633global variables and per-interface fields. 634First, there is a system-wide 635.Xr sysctl 8 636master switch named 637.Va kern.polling.enable , 638which can toggle 639.Xr polling 4 640globally. 641If that variable is set to non-zero, 642.Xr polling 4 643will be used on those devices where it is enabled individually. 644Otherwise, 645.Xr polling 4 646will not be used in the system. 647Second, the capability flag 648.Dv IFCAP_POLLING 649set in interface's 650.Va if_capabilities 651indicates support for 652.Xr polling 4 653on the particular interface. 654If set in 655.Va if_capabilities , 656the same flag can be marked or cleared in the interface's 657.Va if_capenable , 658thus initiating switch of the interface to 659.Xr polling 4 660mode or interrupt 661mode, respectively. 662The actual mode change will occur at an implementation-specific moment 663in the future, e.g., during the next interrupt or 664.Xr polling 4 665cycle. 666And finally, if the mode transition has been successful, the flag 667.Dv IFF_POLLING 668is marked or cleared in the interface's 669.Va if_flags 670to indicate the current mode of the interface. 671.Ss The Vt if_data Ss Structure 672In 673.Bx 4.4 , 674a subset of the interface information believed to be of interest to 675management stations was segregated from the 676.Vt ifnet 677structure and moved into its own 678.Vt if_data 679structure to facilitate its use by user programs. 680The following elements of the 681.Vt if_data 682structure are initialized by the interface and are not expected to change 683significantly over the course of normal operation: 684.Bl -tag -width ".Va ifi_lastchange" -offset indent 685.It Va ifi_type 686.Pq Vt u_char 687The type of the interface, as defined in 688.In net/if_types.h 689and described below in the 690.Sx "Interface Types" 691section. 692.It Va ifi_physical 693.Pq Vt u_char 694Intended to represent a selection of physical layers on devices which 695support more than one; never implemented. 696.It Va ifi_addrlen 697.Pq Vt u_char 698Length of a link-layer address on this device, or zero if there are 699none. 700Used to initialized the address length field in 701.Vt sockaddr_dl 702structures referring to this interface. 703.It Va ifi_hdrlen 704.Pq Vt u_char 705Maximum length of any link-layer header which might be prepended by 706the driver to a packet before transmission. 707The generic code computes 708the maximum over all interfaces and uses that value to influence the 709placement of data in 710.Vt mbuf Ns s 711to attempt to ensure that there is always 712sufficient space to prepend a link-layer header without allocating an 713additional 714.Vt mbuf . 715.\" (See 716.\" .Xr mbuf 9 . ) 717.\" .It Va ifi_recvquota 718.\" .Pq Vt u_char 719.\" Number of packets the interface is permitted to receive at one time 720.\" when in polled mode. 721.\" .It Va ifi_xmitquota 722.\" .Pq Vt u_char 723.\" Number of packets the interface is permitted to queue for transmission 724.\" at one time when in polled mode. 725.\" There is some controversy over 726.\" whether such a restriction makes any sense at all. 727.It Va ifi_datalen 728.Pq Vt u_char 729Length of the 730.Vt if_data 731structure. 732Allows some stabilization of the routing socket ABI in the face of 733increases in the length of 734.Vt struct ifdata . 735.It Va ifi_mtu 736.Pq Vt u_long 737The maximum transmission unit of the medium, exclusive of any 738link-layer overhead. 739.It Va ifi_metric 740.Pq Vt u_long 741A dimensionless metric interpreted by a user-mode routing process. 742.It Va ifi_baudrate 743.Pq Vt u_long 744The line rate of the interface, in bits per second. 745.It Va ifi_hwassist 746.Pq Vt u_long 747A detailed interpretation of the capabilities 748to offload computational tasks for 749.Em outgoing 750packets. 751The interface driver must keep this field in accord with 752the current value of 753.Va if_capenable . 754.It Va ifi_epoch 755.Pq Vt time_t 756The system uptime when interface was attached or the statistics 757below were reset. 758This is intended to be used to set the SNMP variable 759.Va ifCounterDiscontinuityTime . 760It may also be used to determine if two successive queries for an 761interface of the same index have returned results for the same 762interface. 763.El 764.Pp 765The structure additionally contains generic statistics applicable to a 766variety of different interface types (except as noted, all members are 767of type 768.Vt u_long ) : 769.Bl -tag -width ".Va ifi_lastchange" -offset indent 770.It Va ifi_link_state 771.Pq Vt u_char 772The current link state of Ethernet interfaces. 773See the 774.Sx Interface Link States 775section for possible values. 776.It Va ifi_ipackets 777Number of packets received. 778.It Va ifi_ierrors 779Number of receive errors detected (e.g., FCS errors, DMA overruns, 780etc.). 781More detailed breakdowns can often be had by way of a 782link-specific MIB. 783.It Va ifi_opackets 784Number of packets transmitted. 785.It Va ifi_oerrors 786Number of output errors detected (e.g., late collisions, DMA overruns, 787etc.). 788More detailed breakdowns can often be had by way of a 789link-specific MIB. 790.It Va ifi_collisions 791Total number of collisions detected on output for CSMA interfaces. 792(This member is sometimes [ab]used by other types of interfaces for 793other output error counts.) 794.It Va ifi_ibytes 795Total traffic received, in bytes. 796.It Va ifi_obytes 797Total traffic transmitted, in bytes. 798.It Va ifi_imcasts 799Number of packets received which were sent by link-layer multicast. 800.It Va ifi_omcasts 801Number of packets sent by link-layer multicast. 802.It Va ifi_iqdrops 803Number of packets dropped on input. 804Rarely implemented. 805.It Va ifi_noproto 806Number of packets received for unknown network-layer protocol. 807.\" .It Va ifi_recvtiming 808.\" Amount of time, in microseconds, spent to receive an average packet on 809.\" this interface. 810.\" See the 811.\" .Sx Polling 812.\" section, below. 813.\" .It Va ifi_xmittiming 814.\" Amount of time, in microseconds, spent to service a transmit-complete 815.\" interrupt on this interface. 816.\" See the 817.\" .Sx Polling 818.\" section, below. 819.It Va ifi_lastchange 820.Pq Vt "struct timeval" 821The time of the last administrative change to the interface (as required 822for 823.Tn SNMP ) . 824.El 825.Ss Interface Types 826The header file 827.In net/if_types.h 828defines symbolic constants for a number of different types of 829interfaces. 830The most common are: 831.Pp 832.Bl -tag -offset indent -width ".Dv IFT_PROPVIRTUAL" -compact 833.It Dv IFT_OTHER 834none of the following 835.It Dv IFT_ETHER 836Ethernet 837.It Dv IFT_ISO88023 838ISO 8802-3 CSMA/CD 839.It Dv IFT_ISO88024 840ISO 8802-4 Token Bus 841.It Dv IFT_ISO88025 842ISO 8802-5 Token Ring 843.It Dv IFT_ISO88026 844ISO 8802-6 DQDB MAN 845.It Dv IFT_FDDI 846FDDI 847.It Dv IFT_PPP 848Internet Point-to-Point Protocol 849.Pq Xr ppp 8 850.It Dv IFT_LOOP 851The loopback 852.Pq Xr lo 4 853interface 854.It Dv IFT_SLIP 855Serial Line IP 856.It Dv IFT_PARA 857Parallel-port IP 858.Pq Dq Tn PLIP 859.It Dv IFT_ATM 860Asynchronous Transfer Mode 861.El 862.Ss Interface Link States 863The following link states are currently defined: 864.Pp 865.Bl -tag -offset indent -width ".Dv LINK_STATE_UNKNOWN" -compact 866.It Dv LINK_STATE_UNKNOWN 867The link is in an invalid or unknown state. 868.It Dv LINK_STATE_DOWN 869The link is down. 870.It Dv LINK_STATE_UP 871The link is up. 872.El 873.Ss The Vt ifaddr Ss Structure 874Every interface is associated with a list 875(or, rather, a 876.Li TAILQ ) 877of addresses, rooted at the interface structure's 878.Va if_addrlist 879member. 880The first element in this list is always an 881.Dv AF_LINK 882address representing the interface itself; multi-access network 883drivers should complete this structure by filling in their link-layer 884addresses after calling 885.Fn if_attach . 886Other members of the structure represent network-layer addresses which 887have been configured by means of the 888.Dv SIOCAIFADDR 889command to 890.Xr ioctl 2 , 891called on a socket of the appropriate protocol family. 892The elements of this list consist of 893.Vt ifaddr 894structures. 895Most protocols will declare their own protocol-specific 896interface address structures, but all begin with a 897.Vt "struct ifaddr" 898which provides the most-commonly-needed functionality across all 899protocols. 900Interface addresses are reference-counted. 901.Pp 902The members of 903.Vt "struct ifaddr" 904are as follows: 905.Bl -tag -width ".Va ifa_rtrequest" -offset indent 906.It Va ifa_addr 907.Pq Vt "struct sockaddr *" 908The local address of the interface. 909.It Va ifa_dstaddr 910.Pq Vt "struct sockaddr *" 911The remote address of point-to-point interfaces, and the broadcast 912address of broadcast interfaces. 913.Va ( ifa_broadaddr 914is a macro for 915.Va ifa_dstaddr . ) 916.It Va ifa_netmask 917.Pq Vt "struct sockaddr *" 918The network mask for multi-access interfaces, and the confusion 919generator for point-to-point interfaces. 920.It Va ifa_ifp 921.Pq Vt "struct ifnet *" 922A link back to the interface structure. 923.It Va ifa_link 924.Pq Fn TAILQ_ENTRY ifaddr 925.Xr queue 3 926glue for list of addresses on each interface. 927.It Va ifa_rtrequest 928See below. 929.It Va ifa_flags 930.Pq Vt u_short 931Some of the flags which would be used for a route representing this 932address in the route table. 933.It Va ifa_refcnt 934.Pq Vt short 935The reference count. 936.It Va ifa_metric 937.Pq Vt int 938A metric associated with this interface address, for the use of some 939external routing protocol. 940.El 941.Pp 942References to 943.Vt ifaddr 944structures are gained manually, by incrementing the 945.Va ifa_refcnt 946member. 947References are released by calling either the 948.Fn ifafree 949function or the 950.Fn IFAFREE 951macro. 952.Pp 953.Fn ifa_rtrequest 954is a pointer to a function which receives callouts from the routing 955code 956.Pq Fn rtrequest 957to perform link-layer-specific actions upon requests to add, resolve, 958or delete routes. 959The 960.Fa cmd 961argument indicates the request in question: 962.Dv RTM_ADD , RTM_RESOLVE , 963or 964.Dv RTM_DELETE . 965The 966.Fa rt 967argument is the route in question; the 968.Fa dst 969argument is the specific destination being manipulated 970for 971.Dv RTM_RESOLVE , 972or a null pointer otherwise. 973.Sh FUNCTIONS 974The functions provided by the generic interface code can be divided 975into two groups: those which manipulate interfaces, and those which 976manipulate interface addresses. 977In addition to these functions, there 978may also be link-layer support routines which are used by a number of 979drivers implementing a specific link layer over different hardware; 980see the documentation for that link layer for more details. 981.Ss The Vt ifmultiaddr Ss Structure 982Every multicast-capable interface is associated with a list of 983multicast group memberships, which indicate at a low level which 984link-layer multicast addresses (if any) should be accepted, and at a 985high level, in which network-layer multicast groups a user process has 986expressed interest. 987.Pp 988The elements of the structure are as follows: 989.Bl -tag -width ".Va ifma_refcount" -offset indent 990.It Va ifma_link 991.Pq Fn LIST_ENTRY ifmultiaddr 992.Xr queue 3 993macro glue. 994.It Va ifma_addr 995.Pq Vt "struct sockaddr *" 996A pointer to the address which this record represents. 997The 998memberships for various address families are stored in arbitrary 999order. 1000.It Va ifma_lladdr 1001.Pq Vt "struct sockaddr *" 1002A pointer to the link-layer multicast address, if any, to which the 1003network-layer multicast address in 1004.Va ifma_addr 1005is mapped, else a null pointer. 1006If this element is non-nil, this 1007membership also holds an invisible reference to another membership for 1008that link-layer address. 1009.It Va ifma_refcount 1010.Pq Vt u_int 1011A reference count of requests for this particular membership. 1012.El 1013.Ss Interface Manipulation Functions 1014.Bl -ohang -offset indent 1015.It Fn if_alloc 1016Allocate and initialize 1017.Vt "struct ifnet" . 1018Initialization includes the allocation of an interface index and may 1019include the allocation of a 1020.Fa type 1021specific structure in 1022.Va if_l2com . 1023.It Fn if_attach 1024Link the specified interface 1025.Fa ifp 1026into the list of network interfaces. 1027Also initialize the list of 1028addresses on that interface, and create a link-layer 1029.Vt ifaddr 1030structure to be the first element in that list. 1031(A pointer to 1032this address structure is saved in the global array 1033.Va ifnet_addrs . ) 1034The 1035.Fa ifp 1036must have been allocated by 1037.Fn if_alloc . 1038.It Fn if_detach 1039Shut down and unlink the specified 1040.Fa ifp 1041from the interface list. 1042.It Fn if_free 1043Free the given 1044.Fa ifp 1045back to the system. 1046The interface must have been previously detached if it was ever attached. 1047.It Fn if_free_type 1048Identical to 1049.Fn if_free 1050except that the given 1051.Fa type 1052is used to free 1053.Va if_l2com 1054instead of the type in 1055.Va if_type . 1056This is intended for use with drivers that change their interface type. 1057.It Fn if_down 1058Mark the interface 1059.Fa ifp 1060as down (i.e., 1061.Dv IFF_UP 1062is not set), 1063flush its output queue, notify protocols of the transition, 1064and generate a message from the 1065.Xr route 4 1066routing socket. 1067.It Fn if_up 1068Mark the interface 1069.Fa ifp 1070as up, notify protocols of the transition, 1071and generate a message from the 1072.Xr route 4 1073routing socket. 1074.It Fn ifpromisc 1075Add or remove a promiscuous reference to 1076.Fa ifp . 1077If 1078.Fa pswitch 1079is true, add a reference; 1080if it is false, remove a reference. 1081On reference count transitions 1082from zero to one and one to zero, set the 1083.Dv IFF_PROMISC 1084flag appropriately and call 1085.Fn if_ioctl 1086to set up the interface in the desired mode. 1087.It Fn if_allmulti 1088As 1089.Fn ifpromisc , 1090but for the all-multicasts 1091.Pq Dv IFF_ALLMULTI 1092flag instead of the promiscuous flag. 1093.It Fn ifunit 1094Return an 1095.Vt ifnet 1096pointer for the interface named 1097.Fa name . 1098.It Fn ifioctl 1099Process the ioctl request 1100.Fa cmd , 1101issued on socket 1102.Fa so 1103by thread 1104.Fa td , 1105with data parameter 1106.Fa data . 1107This is the main routine for handling all interface configuration 1108requests from user mode. 1109It is ordinarily only called from the socket-layer 1110.Xr ioctl 2 1111handler, and only for commands with class 1112.Sq Li i . 1113Any unrecognized commands will be passed down to socket 1114.Fa so Ns 's 1115protocol for 1116further interpretation. 1117The following commands are handled by 1118.Fn ifioctl : 1119.Pp 1120.Bl -tag -width ".Dv OSIOCGIFNETMASK" -offset indent -compact 1121.It Dv SIOCGIFCONF 1122.It Dv OSIOCGIFCONF 1123Get interface configuration. 1124(No call-down to driver.) 1125.Pp 1126.It Dv SIOCSIFNAME 1127Set the interface name. 1128.Dv RTM_IFANNOUNCE 1129departure and arrival messages are sent so that 1130routing code that relies on the interface name will update its interface 1131list. 1132Caller must have appropriate privilege. 1133(No call-down to driver.) 1134.It Dv SIOCGIFCAP 1135.It Dv SIOCGIFFLAGS 1136.It Dv SIOCGIFMETRIC 1137.It Dv SIOCGIFMTU 1138.It Dv SIOCGIFPHYS 1139Get interface capabilities, flags, metric, MTU, medium selection. 1140(No call-down to driver.) 1141.Pp 1142.It Dv SIOCSIFCAP 1143Enable or disable interface capabilities. 1144Caller must have appropriate privilege. 1145Before a call to the driver-specific 1146.Fn if_ioctl 1147routine, the requested mask for enabled capabilities is checked 1148against the mask of capabilities supported by the interface, 1149.Va if_capabilities . 1150Requesting to enable an unsupported capability is invalid. 1151The rest is supposed to be done by the driver, 1152which includes updating 1153.Va if_capenable 1154and 1155.Va if_data.ifi_hwassist 1156appropriately. 1157.Pp 1158.It Dv SIOCSIFFLAGS 1159Change interface flags. 1160Caller must have appropriate privilege. 1161If a change to the 1162.Dv IFF_UP 1163flag is requested, 1164.Fn if_up 1165or 1166.Fn if_down 1167is called as appropriate. 1168Flags listed in 1169.Dv IFF_CANTCHANGE 1170are masked off, and the field 1171.Va if_flags 1172in the interface structure is updated. 1173Finally, the driver 1174.Fn if_ioctl 1175routine is called to perform any setup 1176requested. 1177.Pp 1178.It Dv SIOCSIFMETRIC 1179.It Dv SIOCSIFPHYS 1180Change interface metric or medium. 1181Caller must have appropriate privilege. 1182.Pp 1183.It Dv SIOCSIFMTU 1184Change interface MTU. 1185Caller must have appropriate privilege. 1186MTU 1187values less than 72 or greater than 65535 are considered invalid. 1188The driver 1189.Fn if_ioctl 1190routine is called to implement the change; it is responsible for any 1191additional sanity checking and for actually modifying the MTU in the 1192interface structure. 1193.Pp 1194.It Dv SIOCADDMULTI 1195.It Dv SIOCDELMULTI 1196Add or delete permanent multicast group memberships on the interface. 1197Caller must have appropriate privilege. 1198The 1199.Fn if_addmulti 1200or 1201.Fn if_delmulti 1202function is called to perform the operation; qq.v. 1203.Pp 1204.It Dv SIOCSIFDSTADDR 1205.It Dv SIOCSIFADDR 1206.It Dv SIOCSIFBRDADDR 1207.It Dv SIOCSIFNETMASK 1208The socket's protocol control routine is called to implement the 1209requested action. 1210.Pp 1211.It Dv OSIOGIFADDR 1212.It Dv OSIOCGIFDSTADDR 1213.It Dv OSIOCGIFBRDADDR 1214.It Dv OSIOCGIFNETMASK 1215The socket's protocol control routine is called to implement the 1216requested action. 1217On return, 1218.Vt sockaddr 1219structures are converted into old-style (no 1220.Va sa_len 1221member). 1222.El 1223.El 1224.Pp 1225.Fn if_down , 1226.Fn ifioctl , 1227.Fn ifpromisc , 1228and 1229.Fn if_up 1230must be called at 1231.Fn splnet 1232or higher. 1233.Ss "Interface Address Functions" 1234Several functions exist to look up an interface address structure 1235given an address. 1236.Fn ifa_ifwithaddr 1237returns an interface address with either a local address or a 1238broadcast address precisely matching the parameter 1239.Fa addr . 1240.Fn ifa_ifwithdstaddr 1241returns an interface address for a point-to-point interface whose 1242remote 1243.Pq Dq destination 1244address is 1245.Fa addr . 1246.Pp 1247.Fn ifa_ifwithnet 1248returns the most specific interface address which matches the 1249specified address, 1250.Fa addr , 1251subject to its configured netmask, or a point-to-point interface 1252address whose remote address is 1253.Fa addr 1254if one is found. 1255.Pp 1256.Fn ifaof_ifpforaddr 1257returns the most specific address configured on interface 1258.Fa ifp 1259which matches address 1260.Fa addr , 1261subject to its configured netmask. 1262If the interface is 1263point-to-point, only an interface address whose remote address is 1264precisely 1265.Fa addr 1266will be returned. 1267.Pp 1268All of these functions return a null pointer if no such address can be 1269found. 1270.Ss "Interface Multicast Address Functions" 1271The 1272.Fn if_addmulti , 1273.Fn if_delmulti , 1274and 1275.Fn ifmaof_ifpforaddr 1276functions provide support for requesting and relinquishing multicast 1277group memberships, and for querying an interface's membership list, 1278respectively. 1279The 1280.Fn if_addmulti 1281function takes a pointer to an interface, 1282.Fa ifp , 1283and a generic address, 1284.Fa sa . 1285It also takes a pointer to a 1286.Vt "struct ifmultiaddr *" 1287which is filled in on successful return with the address of the 1288group membership control block. 1289The 1290.Fn if_addmulti 1291function performs the following four-step process: 1292.Bl -enum -offset indent 1293.It 1294Call the interface's 1295.Fn if_resolvemulti 1296entry point to determine the link-layer address, if any, corresponding 1297to this membership request, and also to give the link layer an 1298opportunity to veto this membership request should it so desire. 1299.It 1300Check the interface's group membership list for a pre-existing 1301membership for this group. 1302If one is not found, allocate a new one; 1303if one is, increment its reference count. 1304.It 1305If the 1306.Fn if_resolvemulti 1307routine returned a link-layer address corresponding to the group, 1308repeat the previous step for that address as well. 1309.It 1310If the interface's multicast address filter needs to be changed 1311because a new membership was added, call the interface's 1312.Fn if_ioctl 1313routine 1314(with a 1315.Fa cmd 1316argument of 1317.Dv SIOCADDMULTI ) 1318to request that it do so. 1319.El 1320.Pp 1321The 1322.Fn if_delmulti 1323function, given an interface 1324.Fa ifp 1325and an address, 1326.Fa sa , 1327reverses this process. 1328Both functions return zero on success, or a 1329standard error number on failure. 1330.Pp 1331The 1332.Fn ifmaof_ifpforaddr 1333function examines the membership list of interface 1334.Fa ifp 1335for an address matching 1336.Fa addr , 1337and returns a pointer to that 1338.Vt "struct ifmultiaddr" 1339if one is found, else it returns a null pointer. 1340.Sh SEE ALSO 1341.Xr ioctl 2 , 1342.Xr link_addr 3 , 1343.Xr queue 3 , 1344.Xr sysctl 3 , 1345.Xr bpf 4 , 1346.Xr ifmib 4 , 1347.Xr lo 4 , 1348.Xr netintro 4 , 1349.Xr polling 4 , 1350.Xr config 8 , 1351.Xr ppp 8 , 1352.Xr mbuf 9 , 1353.Xr rtentry 9 1354.Rs 1355.%A Gary R. Wright 1356.%A W. Richard Stevens 1357.%B TCP/IP Illustrated 1358.%V Vol. 2 1359.%O Addison-Wesley, ISBN 0-201-63354-X 1360.Re 1361.Sh AUTHORS 1362This manual page was written by 1363.An Garrett A. Wollman . 1364