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