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