xref: /freebsd/share/man/man9/ifnet.9 (revision 3157ba21)
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.El
592.Pp
593The ability of advanced network interfaces to offload certain
594computational tasks from the host CPU to the board is limited
595mostly to TCP/IP.
596Therefore a separate field associated with an interface
597(see
598.Va ifnet.if_data.ifi_hwassist
599below)
600keeps a detailed description of its enabled capabilities
601specific to TCP/IP processing.
602The TCP/IP module consults the field to see which tasks
603can be done on an
604.Em outgoing
605packet by the interface.
606The flags defined for that field are a superset of those for
607.Va mbuf.m_pkthdr.csum_flags ,
608namely:
609.Bl -tag -width ".Dv CSUM_FRAGMENT" -offset indent
610.It Dv CSUM_IP
611The interface will compute IP checksums.
612.It Dv CSUM_TCP
613The interface will compute TCP checksums.
614.It Dv CSUM_UDP
615The interface will compute UDP checksums.
616.It Dv CSUM_IP_FRAGS
617The interface can compute a TCP or UDP checksum for a packet
618fragmented by the host CPU.
619Makes sense only along with
620.Dv CSUM_TCP
621or
622.Dv CSUM_UDP .
623.It Dv CSUM_FRAGMENT
624The interface will do the fragmentation of IP packets if necessary.
625The host CPU does not need to care about MTU on this interface
626as long as a packet to transmit through it is an IP one and it
627does not exceed the size of the hardware buffer.
628.El
629.Pp
630An interface notifies the TCP/IP module about the tasks
631the former has performed on an
632.Em incoming
633packet by setting the corresponding flags in the field
634.Va mbuf.m_pkthdr.csum_flags
635of the
636.Vt mbuf chain
637containing the packet.
638See
639.Xr mbuf 9
640for details.
641.Pp
642The capability of a network interface to operate in
643.Xr polling 4
644mode involves several flags in different
645global variables and per-interface fields.
646First, there is a system-wide
647.Xr sysctl 8
648master switch named
649.Va kern.polling.enable ,
650which can toggle
651.Xr polling 4
652globally.
653If that variable is set to non-zero,
654.Xr polling 4
655will be used on those devices where it is enabled individually.
656Otherwise,
657.Xr polling 4
658will not be used in the system.
659Second, the capability flag
660.Dv IFCAP_POLLING
661set in interface's
662.Va if_capabilities
663indicates support for
664.Xr polling 4
665on the particular interface.
666If set in
667.Va if_capabilities ,
668the same flag can be marked or cleared in the interface's
669.Va if_capenable ,
670thus initiating switch of the interface to
671.Xr polling 4
672mode or interrupt
673mode, respectively.
674The actual mode change will occur at an implementation-specific moment
675in the future, e.g., during the next interrupt or
676.Xr polling 4
677cycle.
678And finally, if the mode transition has been successful, the flag
679.Dv IFF_POLLING
680is marked or cleared in the interface's
681.Va if_flags
682to indicate the current mode of the interface.
683.Ss The Vt if_data Ss Structure
684In
685.Bx 4.4 ,
686a subset of the interface information believed to be of interest to
687management stations was segregated from the
688.Vt ifnet
689structure and moved into its own
690.Vt if_data
691structure to facilitate its use by user programs.
692The following elements of the
693.Vt if_data
694structure are initialized by the interface and are not expected to change
695significantly over the course of normal operation:
696.Bl -tag -width ".Va ifi_lastchange" -offset indent
697.It Va ifi_type
698.Pq Vt u_char
699The type of the interface, as defined in
700.In net/if_types.h
701and described below in the
702.Sx "Interface Types"
703section.
704.It Va ifi_physical
705.Pq Vt u_char
706Intended to represent a selection of physical layers on devices which
707support more than one; never implemented.
708.It Va ifi_addrlen
709.Pq Vt u_char
710Length of a link-layer address on this device, or zero if there are
711none.
712Used to initialized the address length field in
713.Vt sockaddr_dl
714structures referring to this interface.
715.It Va ifi_hdrlen
716.Pq Vt u_char
717Maximum length of any link-layer header which might be prepended by
718the driver to a packet before transmission.
719The generic code computes
720the maximum over all interfaces and uses that value to influence the
721placement of data in
722.Vt mbuf Ns s
723to attempt to ensure that there is always
724sufficient space to prepend a link-layer header without allocating an
725additional
726.Vt mbuf .
727.\" (See
728.\" .Xr mbuf 9 . )
729.\" .It Va ifi_recvquota
730.\" .Pq Vt u_char
731.\" Number of packets the interface is permitted to receive at one time
732.\" when in polled mode.
733.\" .It Va ifi_xmitquota
734.\" .Pq Vt u_char
735.\" Number of packets the interface is permitted to queue for transmission
736.\" at one time when in polled mode.
737.\" There is some controversy over
738.\" whether such a restriction makes any sense at all.
739.It Va ifi_datalen
740.Pq Vt u_char
741Length of the
742.Vt if_data
743structure.
744Allows some stabilization of the routing socket ABI in the face of
745increases in the length of
746.Vt struct ifdata .
747.It Va ifi_mtu
748.Pq Vt u_long
749The maximum transmission unit of the medium, exclusive of any
750link-layer overhead.
751.It Va ifi_metric
752.Pq Vt u_long
753A dimensionless metric interpreted by a user-mode routing process.
754.It Va ifi_baudrate
755.Pq Vt u_long
756The line rate of the interface, in bits per second.
757.It Va ifi_hwassist
758.Pq Vt u_long
759A detailed interpretation of the capabilities
760to offload computational tasks for
761.Em outgoing
762packets.
763The interface driver must keep this field in accord with
764the current value of
765.Va if_capenable .
766.It Va ifi_epoch
767.Pq Vt time_t
768The system uptime when interface was attached or the statistics
769below were reset.
770This is intended to be used to set the SNMP variable
771.Va ifCounterDiscontinuityTime .
772It may also be used to determine if two successive queries for an
773interface of the same index have returned results for the same
774interface.
775.El
776.Pp
777The structure additionally contains generic statistics applicable to a
778variety of different interface types (except as noted, all members are
779of type
780.Vt u_long ) :
781.Bl -tag -width ".Va ifi_lastchange" -offset indent
782.It Va ifi_link_state
783.Pq Vt u_char
784The current link state of Ethernet interfaces.
785See the
786.Sx Interface Link States
787section for possible values.
788.It Va ifi_ipackets
789Number of packets received.
790.It Va ifi_ierrors
791Number of receive errors detected (e.g., FCS errors, DMA overruns,
792etc.).
793More detailed breakdowns can often be had by way of a
794link-specific MIB.
795.It Va ifi_opackets
796Number of packets transmitted.
797.It Va ifi_oerrors
798Number of output errors detected (e.g., late collisions, DMA overruns,
799etc.).
800More detailed breakdowns can often be had by way of a
801link-specific MIB.
802.It Va ifi_collisions
803Total number of collisions detected on output for CSMA interfaces.
804(This member is sometimes [ab]used by other types of interfaces for
805other output error counts.)
806.It Va ifi_ibytes
807Total traffic received, in bytes.
808.It Va ifi_obytes
809Total traffic transmitted, in bytes.
810.It Va ifi_imcasts
811Number of packets received which were sent by link-layer multicast.
812.It Va ifi_omcasts
813Number of packets sent by link-layer multicast.
814.It Va ifi_iqdrops
815Number of packets dropped on input.
816Rarely implemented.
817.It Va ifi_noproto
818Number of packets received for unknown network-layer protocol.
819.\" .It Va ifi_recvtiming
820.\" Amount of time, in microseconds, spent to receive an average packet on
821.\" this interface.
822.\" See the
823.\" .Sx Polling
824.\" section, below.
825.\" .It Va ifi_xmittiming
826.\" Amount of time, in microseconds, spent to service a transmit-complete
827.\" interrupt on this interface.
828.\" See the
829.\" .Sx Polling
830.\" section, below.
831.It Va ifi_lastchange
832.Pq Vt "struct timeval"
833The time of the last administrative change to the interface (as required
834for
835.Tn SNMP ) .
836.El
837.Ss Interface Types
838The header file
839.In net/if_types.h
840defines symbolic constants for a number of different types of
841interfaces.
842The most common are:
843.Pp
844.Bl -tag -offset indent -width ".Dv IFT_PROPVIRTUAL" -compact
845.It Dv IFT_OTHER
846none of the following
847.It Dv IFT_ETHER
848Ethernet
849.It Dv IFT_ISO88023
850ISO 8802-3 CSMA/CD
851.It Dv IFT_ISO88024
852ISO 8802-4 Token Bus
853.It Dv IFT_ISO88025
854ISO 8802-5 Token Ring
855.It Dv IFT_ISO88026
856ISO 8802-6 DQDB MAN
857.It Dv IFT_FDDI
858FDDI
859.It Dv IFT_PPP
860Internet Point-to-Point Protocol
861.Pq Xr ppp 8
862.It Dv IFT_LOOP
863The loopback
864.Pq Xr lo 4
865interface
866.It Dv IFT_SLIP
867Serial Line IP
868.It Dv IFT_PARA
869Parallel-port IP
870.Pq Dq Tn PLIP
871.It Dv IFT_ATM
872Asynchronous Transfer Mode
873.El
874.Ss Interface Link States
875The following link states are currently defined:
876.Pp
877.Bl -tag -offset indent -width ".Dv LINK_STATE_UNKNOWN" -compact
878.It Dv LINK_STATE_UNKNOWN
879The link is in an invalid or unknown state.
880.It Dv LINK_STATE_DOWN
881The link is down.
882.It Dv LINK_STATE_UP
883The link is up.
884.El
885.Ss The Vt ifaddr Ss Structure
886Every interface is associated with a list
887(or, rather, a
888.Li TAILQ )
889of addresses, rooted at the interface structure's
890.Va if_addrlist
891member.
892The first element in this list is always an
893.Dv AF_LINK
894address representing the interface itself; multi-access network
895drivers should complete this structure by filling in their link-layer
896addresses after calling
897.Fn if_attach .
898Other members of the structure represent network-layer addresses which
899have been configured by means of the
900.Dv SIOCAIFADDR
901command to
902.Xr ioctl 2 ,
903called on a socket of the appropriate protocol family.
904The elements of this list consist of
905.Vt ifaddr
906structures.
907Most protocols will declare their own protocol-specific
908interface address structures, but all begin with a
909.Vt "struct ifaddr"
910which provides the most-commonly-needed functionality across all
911protocols.
912Interface addresses are reference-counted.
913.Pp
914The members of
915.Vt "struct ifaddr"
916are as follows:
917.Bl -tag -width ".Va ifa_rtrequest" -offset indent
918.It Va ifa_addr
919.Pq Vt "struct sockaddr *"
920The local address of the interface.
921.It Va ifa_dstaddr
922.Pq Vt "struct sockaddr *"
923The remote address of point-to-point interfaces, and the broadcast
924address of broadcast interfaces.
925.Va ( ifa_broadaddr
926is a macro for
927.Va ifa_dstaddr . )
928.It Va ifa_netmask
929.Pq Vt "struct sockaddr *"
930The network mask for multi-access interfaces, and the confusion
931generator for point-to-point interfaces.
932.It Va ifa_ifp
933.Pq Vt "struct ifnet *"
934A link back to the interface structure.
935.It Va ifa_link
936.Pq Fn TAILQ_ENTRY ifaddr
937.Xr queue 3
938glue for list of addresses on each interface.
939.It Va ifa_rtrequest
940See below.
941.It Va ifa_flags
942.Pq Vt u_short
943Some of the flags which would be used for a route representing this
944address in the route table.
945.It Va ifa_refcnt
946.Pq Vt short
947The reference count.
948.It Va ifa_metric
949.Pq Vt int
950A metric associated with this interface address, for the use of some
951external routing protocol.
952.El
953.Pp
954References to
955.Vt ifaddr
956structures are gained manually, by incrementing the
957.Va ifa_refcnt
958member.
959References are released by calling either the
960.Fn ifafree
961function or the
962.Fn IFAFREE
963macro.
964.Pp
965.Fn ifa_rtrequest
966is a pointer to a function which receives callouts from the routing
967code
968.Pq Fn rtrequest
969to perform link-layer-specific actions upon requests to add, resolve,
970or delete routes.
971The
972.Fa cmd
973argument indicates the request in question:
974.Dv RTM_ADD , RTM_RESOLVE ,
975or
976.Dv RTM_DELETE .
977The
978.Fa rt
979argument is the route in question; the
980.Fa dst
981argument is the specific destination being manipulated
982for
983.Dv RTM_RESOLVE ,
984or a null pointer otherwise.
985.Sh FUNCTIONS
986The functions provided by the generic interface code can be divided
987into two groups: those which manipulate interfaces, and those which
988manipulate interface addresses.
989In addition to these functions, there
990may also be link-layer support routines which are used by a number of
991drivers implementing a specific link layer over different hardware;
992see the documentation for that link layer for more details.
993.Ss The Vt ifmultiaddr Ss Structure
994Every multicast-capable interface is associated with a list of
995multicast group memberships, which indicate at a low level which
996link-layer multicast addresses (if any) should be accepted, and at a
997high level, in which network-layer multicast groups a user process has
998expressed interest.
999.Pp
1000The elements of the structure are as follows:
1001.Bl -tag -width ".Va ifma_refcount" -offset indent
1002.It Va ifma_link
1003.Pq Fn LIST_ENTRY ifmultiaddr
1004.Xr queue 3
1005macro glue.
1006.It Va ifma_addr
1007.Pq Vt "struct sockaddr *"
1008A pointer to the address which this record represents.
1009The
1010memberships for various address families are stored in arbitrary
1011order.
1012.It Va ifma_lladdr
1013.Pq Vt "struct sockaddr *"
1014A pointer to the link-layer multicast address, if any, to which the
1015network-layer multicast address in
1016.Va ifma_addr
1017is mapped, else a null pointer.
1018If this element is non-nil, this
1019membership also holds an invisible reference to another membership for
1020that link-layer address.
1021.It Va ifma_refcount
1022.Pq Vt u_int
1023A reference count of requests for this particular membership.
1024.El
1025.Ss Interface Manipulation Functions
1026.Bl -ohang -offset indent
1027.It Fn if_alloc
1028Allocate and initialize
1029.Vt "struct ifnet" .
1030Initialization includes the allocation of an interface index and may
1031include the allocation of a
1032.Fa type
1033specific structure in
1034.Va if_l2com .
1035.It Fn if_attach
1036Link the specified interface
1037.Fa ifp
1038into the list of network interfaces.
1039Also initialize the list of
1040addresses on that interface, and create a link-layer
1041.Vt ifaddr
1042structure to be the first element in that list.
1043(A pointer to
1044this address structure is saved in the global array
1045.Va ifnet_addrs . )
1046The
1047.Fa ifp
1048must have been allocated by
1049.Fn if_alloc .
1050.It Fn if_detach
1051Shut down and unlink the specified
1052.Fa ifp
1053from the interface list.
1054.It Fn if_free
1055Free the given
1056.Fa ifp
1057back to the system.
1058The interface must have been previously detached if it was ever attached.
1059.It Fn if_free_type
1060Identical to
1061.Fn if_free
1062except that the given
1063.Fa type
1064is used to free
1065.Va if_l2com
1066instead of the type in
1067.Va if_type .
1068This is intended for use with drivers that change their interface type.
1069.It Fn if_down
1070Mark the interface
1071.Fa ifp
1072as down (i.e.,
1073.Dv IFF_UP
1074is not set),
1075flush its output queue, notify protocols of the transition,
1076and generate a message from the
1077.Xr route 4
1078routing socket.
1079.It Fn if_up
1080Mark the interface
1081.Fa ifp
1082as up, notify protocols of the transition,
1083and generate a message from the
1084.Xr route 4
1085routing socket.
1086.It Fn ifpromisc
1087Add or remove a promiscuous reference to
1088.Fa ifp .
1089If
1090.Fa pswitch
1091is true, add a reference;
1092if it is false, remove a reference.
1093On reference count transitions
1094from zero to one and one to zero, set the
1095.Dv IFF_PROMISC
1096flag appropriately and call
1097.Fn if_ioctl
1098to set up the interface in the desired mode.
1099.It Fn if_allmulti
1100As
1101.Fn ifpromisc ,
1102but for the all-multicasts
1103.Pq Dv IFF_ALLMULTI
1104flag instead of the promiscuous flag.
1105.It Fn ifunit
1106Return an
1107.Vt ifnet
1108pointer for the interface named
1109.Fa name .
1110.It Fn ifioctl
1111Process the ioctl request
1112.Fa cmd ,
1113issued on socket
1114.Fa so
1115by thread
1116.Fa td ,
1117with data parameter
1118.Fa data .
1119This is the main routine for handling all interface configuration
1120requests from user mode.
1121It is ordinarily only called from the socket-layer
1122.Xr ioctl 2
1123handler, and only for commands with class
1124.Sq Li i .
1125Any unrecognized commands will be passed down to socket
1126.Fa so Ns 's
1127protocol for
1128further interpretation.
1129The following commands are handled by
1130.Fn ifioctl :
1131.Pp
1132.Bl -tag -width ".Dv OSIOCGIFNETMASK" -offset indent -compact
1133.It Dv SIOCGIFCONF
1134.It Dv OSIOCGIFCONF
1135Get interface configuration.
1136(No call-down to driver.)
1137.Pp
1138.It Dv SIOCSIFNAME
1139Set the interface name.
1140.Dv RTM_IFANNOUNCE
1141departure and arrival messages are sent so that
1142routing code that relies on the interface name will update its interface
1143list.
1144Caller must have appropriate privilege.
1145(No call-down to driver.)
1146.It Dv SIOCGIFCAP
1147.It Dv SIOCGIFFLAGS
1148.It Dv SIOCGIFMETRIC
1149.It Dv SIOCGIFMTU
1150.It Dv SIOCGIFPHYS
1151Get interface capabilities, flags, metric, MTU, medium selection.
1152(No call-down to driver.)
1153.Pp
1154.It Dv SIOCSIFCAP
1155Enable or disable interface capabilities.
1156Caller must have appropriate privilege.
1157Before a call to the driver-specific
1158.Fn if_ioctl
1159routine, the requested mask for enabled capabilities is checked
1160against the mask of capabilities supported by the interface,
1161.Va if_capabilities .
1162Requesting to enable an unsupported capability is invalid.
1163The rest is supposed to be done by the driver,
1164which includes updating
1165.Va if_capenable
1166and
1167.Va if_data.ifi_hwassist
1168appropriately.
1169.Pp
1170.It Dv SIOCSIFFLAGS
1171Change interface flags.
1172Caller must have appropriate privilege.
1173If a change to the
1174.Dv IFF_UP
1175flag is requested,
1176.Fn if_up
1177or
1178.Fn if_down
1179is called as appropriate.
1180Flags listed in
1181.Dv IFF_CANTCHANGE
1182are masked off, and the field
1183.Va if_flags
1184in the interface structure is updated.
1185Finally, the driver
1186.Fn if_ioctl
1187routine is called to perform any setup
1188requested.
1189.Pp
1190.It Dv SIOCSIFMETRIC
1191.It Dv SIOCSIFPHYS
1192Change interface metric or medium.
1193Caller must have appropriate privilege.
1194.Pp
1195.It Dv SIOCSIFMTU
1196Change interface MTU.
1197Caller must have appropriate privilege.
1198MTU
1199values less than 72 or greater than 65535 are considered invalid.
1200The driver
1201.Fn if_ioctl
1202routine is called to implement the change; it is responsible for any
1203additional sanity checking and for actually modifying the MTU in the
1204interface structure.
1205.Pp
1206.It Dv SIOCADDMULTI
1207.It Dv SIOCDELMULTI
1208Add or delete permanent multicast group memberships on the interface.
1209Caller must have appropriate privilege.
1210The
1211.Fn if_addmulti
1212or
1213.Fn if_delmulti
1214function is called to perform the operation; qq.v.
1215.Pp
1216.It Dv SIOCSIFDSTADDR
1217.It Dv SIOCSIFADDR
1218.It Dv SIOCSIFBRDADDR
1219.It Dv SIOCSIFNETMASK
1220The socket's protocol control routine is called to implement the
1221requested action.
1222.Pp
1223.It Dv OSIOGIFADDR
1224.It Dv OSIOCGIFDSTADDR
1225.It Dv OSIOCGIFBRDADDR
1226.It Dv OSIOCGIFNETMASK
1227The socket's protocol control routine is called to implement the
1228requested action.
1229On return,
1230.Vt sockaddr
1231structures are converted into old-style (no
1232.Va sa_len
1233member).
1234.El
1235.El
1236.Pp
1237.Fn if_down ,
1238.Fn ifioctl ,
1239.Fn ifpromisc ,
1240and
1241.Fn if_up
1242must be called at
1243.Fn splnet
1244or higher.
1245.Ss "Interface Address Functions"
1246Several functions exist to look up an interface address structure
1247given an address.
1248.Fn ifa_ifwithaddr
1249returns an interface address with either a local address or a
1250broadcast address precisely matching the parameter
1251.Fa addr .
1252.Fn ifa_ifwithdstaddr
1253returns an interface address for a point-to-point interface whose
1254remote
1255.Pq Dq destination
1256address is
1257.Fa addr .
1258.Pp
1259.Fn ifa_ifwithnet
1260returns the most specific interface address which matches the
1261specified address,
1262.Fa addr ,
1263subject to its configured netmask, or a point-to-point interface
1264address whose remote address is
1265.Fa addr
1266if one is found.
1267.Pp
1268.Fn ifaof_ifpforaddr
1269returns the most specific address configured on interface
1270.Fa ifp
1271which matches address
1272.Fa addr ,
1273subject to its configured netmask.
1274If the interface is
1275point-to-point, only an interface address whose remote address is
1276precisely
1277.Fa addr
1278will be returned.
1279.Pp
1280All of these functions return a null pointer if no such address can be
1281found.
1282.Ss "Interface Multicast Address Functions"
1283The
1284.Fn if_addmulti ,
1285.Fn if_delmulti ,
1286and
1287.Fn ifmaof_ifpforaddr
1288functions provide support for requesting and relinquishing multicast
1289group memberships, and for querying an interface's membership list,
1290respectively.
1291The
1292.Fn if_addmulti
1293function takes a pointer to an interface,
1294.Fa ifp ,
1295and a generic address,
1296.Fa sa .
1297It also takes a pointer to a
1298.Vt "struct ifmultiaddr *"
1299which is filled in on successful return with the address of the
1300group membership control block.
1301The
1302.Fn if_addmulti
1303function performs the following four-step process:
1304.Bl -enum -offset indent
1305.It
1306Call the interface's
1307.Fn if_resolvemulti
1308entry point to determine the link-layer address, if any, corresponding
1309to this membership request, and also to give the link layer an
1310opportunity to veto this membership request should it so desire.
1311.It
1312Check the interface's group membership list for a pre-existing
1313membership for this group.
1314If one is not found, allocate a new one;
1315if one is, increment its reference count.
1316.It
1317If the
1318.Fn if_resolvemulti
1319routine returned a link-layer address corresponding to the group,
1320repeat the previous step for that address as well.
1321.It
1322If the interface's multicast address filter needs to be changed
1323because a new membership was added, call the interface's
1324.Fn if_ioctl
1325routine
1326(with a
1327.Fa cmd
1328argument of
1329.Dv SIOCADDMULTI )
1330to request that it do so.
1331.El
1332.Pp
1333The
1334.Fn if_delmulti
1335function, given an interface
1336.Fa ifp
1337and an address,
1338.Fa sa ,
1339reverses this process.
1340Both functions return zero on success, or a
1341standard error number on failure.
1342.Pp
1343The
1344.Fn ifmaof_ifpforaddr
1345function examines the membership list of interface
1346.Fa ifp
1347for an address matching
1348.Fa addr ,
1349and returns a pointer to that
1350.Vt "struct ifmultiaddr"
1351if one is found, else it returns a null pointer.
1352.Sh SEE ALSO
1353.Xr ioctl 2 ,
1354.Xr link_addr 3 ,
1355.Xr queue 3 ,
1356.Xr sysctl 3 ,
1357.Xr bpf 4 ,
1358.Xr ifmib 4 ,
1359.Xr lo 4 ,
1360.Xr netintro 4 ,
1361.Xr polling 4 ,
1362.Xr config 8 ,
1363.Xr ppp 8 ,
1364.Xr mbuf 9 ,
1365.Xr rtentry 9
1366.Rs
1367.%A Gary R. Wright
1368.%A W. Richard Stevens
1369.%B TCP/IP Illustrated
1370.%V Vol. 2
1371.%O Addison-Wesley, ISBN 0-201-63354-X
1372.Re
1373.Sh AUTHORS
1374This manual page was written by
1375.An Garrett A. Wollman .
1376