xref: /freebsd/share/man/man9/ifnet.9 (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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.Dd January 15, 1997
31.Os
32.Dt IFNET 9
33.Sh NAME
34.Nm ifnet ,
35.Nm ifaddr ,
36.Nm ifqueue ,
37.Nm if_data
38.Nd kernel interfaces for manipulating network interfaces
39.Sh SYNOPSIS
40.In sys/param.h
41.In sys/time.h
42.In sys/socket.h
43.In net/if.h
44.In net/if_var.h
45.In net/if_types.h
46.\"
47.Ss "Interface Manipulation Functions"
48.Ft void
49.Fn if_attach "struct ifnet *ifp"
50.Ft void
51.Fn if_down "struct ifnet *ifp"
52.Ft int
53.Fn ifioctl "struct socket *so" "u_long cmd" "caddr_t data" "struct thread *td"
54.Ft int
55.Fn ifpromisc "struct ifnet *ifp" "int pswitch"
56.Ft int
57.Fn if_allmulti "struct ifnet *ifp" "int amswitch"
58.Ft "struct ifnet *"
59.Fn ifunit "const char *name"
60.Ft void
61.Fn if_up "struct ifnet *ifp"
62.\"
63.Ss "Interface Address Functions"
64.Ft "struct ifaddr *"
65.Fn ifa_ifwithaddr "struct sockaddr *addr"
66.Ft "struct ifaddr *"
67.Fn ifa_ifwithdstaddr "struct sockaddr *addr"
68.Ft "struct ifaddr *"
69.Fn ifa_ifwithnet "struct sockaddr *addr"
70.Ft "struct ifaddr *"
71.Fn ifaof_ifpforaddr "struct sockaddr *addr" "struct ifnet *ifp"
72.Ft void
73.Fn ifafree "struct ifaddr *ifa"
74.Fn IFAFREE "struct ifaddr *ifa"
75.\"
76.Ss "Interface Multicast Address Functions"
77.Ft int
78.Fn if_addmulti "struct ifnet *ifp" "struct sockaddr *sa" "struct ifmultiaddr **ifmap"
79.Ft int
80.Fn if_delmulti "struct ifnet *ifp" "struct sockaddr *sa"
81.Ft "struct ifmultiaddr *"
82.Fn ifmaof_ifpforaddr "struct sockaddr *addr" "struct ifnet *ifp"
83.Ss "Output queue macros"
84.Fn IF_DEQUEUE "struct ifqueue *ifq" "struct mbuf *m"
85.\"
86.Ss "struct ifnet Member Functions"
87.Ft int
88.Fo \*(lp*if_output\*(rp
89.Fa "struct ifnet *ifp" "struct mbuf *m"
90.Fa "struct sockaddr *dst" "struct rtentry *rt"
91.Fc
92.Ft void
93.Fn \*(lp*if_start\*(rp "struct ifnet *ifp"
94.Ft int
95.Fn \*(lp*if_done\*(rp "struct ifnet *ifp"
96.Ft int
97.Fn \*(lp*if_ioctl\*(rp "struct ifnet *ifp" "int cmd" "caddr_t data"
98.Ft void
99.Fn \*(lp*if_watchdog\*(rp "struct ifnet *ifp"
100.Ft int
101.Fn \*(lp*if_poll_recv\*(rp "struct ifnet *ifp" "int *quotap"
102.Ft int
103.Fn \*(lp*if_poll_xmit\*(rp "struct ifnet *ifp" "int *quotap"
104.Ft void
105.Fn \*(lp*if_poll_inttrn\*(rp "struct ifnet *ifp"
106.Ft void
107.Fn \*(lp*if_poll_slowinput\*(rp "struct ifnet *ifp" "struct mbuf *m"
108.Ft void
109.Fn \*(lp*if_init\*(rp "void *if_softc"
110.Ft int
111.Fo \*(lp*if_resolvemulti\*(rp
112.Fa "struct ifnet *ifp" "struct sockaddr **retsa" "struct sockaddr *addr"
113.Fc
114.Ss "struct ifaddr member function"
115.Ft void
116.Fo \*(lp*ifa_rtrequest\*(rp
117.Fa "int cmd" "struct rtentry *rt" "struct sockaddr *dst"
118.Fc
119.\"
120.Ss "Global Variables"
121.Vt extern struct ifnethead ifnet ;
122.Vt extern struct ifaddr **ifnet_addrs ;
123.Vt extern int if_index ;
124.Vt extern int ifqmaxlen ;
125.Sh DATA STRUCTURES
126The kernel mechanisms for handling network interfaces reside primarily
127in the
128.Vt ifnet , if_data , ifaddr ,
129and
130.Vt ifmultiaddr
131structures in
132.In net/if.h
133and
134.In net/if_var.h
135and the functions named above and defined in
136.Pa /sys/net/if.c .
137Those interfaces which are intended to be used by user programs
138are defined in
139.In net/if.h ;
140these include the interface flags, the
141.Vt if_data
142structure, and the structures defining the appearance of
143interface-related messages on the
144.Xr route 4
145routing socket and in
146.Xr sysctl 3 .
147The header file
148.In net/if_var.h
149defines the kernel-internal interfaces, including the
150.Vt ifnet , ifaddr ,
151and
152.Vt ifmultiaddr
153structures and the functions which manipulate them.
154(A few user programs will need
155.In net/if_var.h
156because it is the prerequisite of some other header file like
157.In netinet/if_ether.h .
158Most references to those two files in particular can be replaced by
159.In net/ethernet.h . )
160.Pp
161The system keeps a linked list of interfaces using the
162.Li TAILQ
163macros defined in
164.Xr queue 3 ;
165this list is headed by a
166.Vt "struct ifnethead"
167called
168.Va ifnet .
169The elements of this list are of type
170.Vt "struct ifnet" ,
171and most kernel routines which manipulate interface as such accept or
172return pointers to these structures.
173Each interface structure
174contains an
175.Vt if_data
176structure, which contains statistics and identifying information used
177by management programs, and which is exported to user programs by way
178of the
179.Xr ifmib 4
180branch of the
181.Xr sysctl 3
182MIB.
183Each interface also has a
184.Li TAILQ
185of interface addresses, described by
186.Vt ifaddr
187structures; the head of the queue is always an
188.Dv AF_LINK
189address
190(see
191.Xr link_addr 3 )
192describing the link layer implemented by the interface (if any).
193(Some trivial interfaces do not provide any link layer addresses;
194this structure, while still present, serves only to identify the
195interface name and index.)
196.Pp
197Finally, those interfaces supporting reception of multicast datagrams
198have a
199.Li TAILQ
200of multicast group memberships, described by
201.Vt ifmultiaddr
202structures.
203These memberships are reference-counted.
204.Pp
205Interfaces are also associated with an output queue, defined as a
206.Vt "struct ifqueue" ;
207this structure is used to hold packets while the interface is in the
208process of sending another.
209.Pp
210.Ss The Vt ifnet Ss structure
211The fields of
212.Vt "struct ifnet"
213are as follows:
214.Bl -tag -width ".Va if_capabilities" -offset indent
215.It Va if_softc
216.Pq Vt "void *"
217A pointer to the driver's private state block.
218(Initialized by driver.)
219.It Va if_name
220.Pq Vt "char *"
221The name of the interface, not including the unit number
222(e.g.,
223.Dq Li de
224or
225.Dq Li lo ) .
226(Initialized by driver.)
227.It Va if_link
228.Pq Fn TAILQ_ENTRY ifnet
229.Xr queue 3
230macro glue.
231.It Va if_addrhead
232.Pq Vt "struct ifaddrhead"
233The head of the
234.Xr queue 3
235.Li TAILQ
236containing the list of addresses assigned to this interface.
237.It Va if_pcount
238.Pq Vt int
239A count of promiscuous listeners on this interface, used to
240reference-count the
241.Dv IFF_PROMISC
242flag.
243.It Va if_bpf
244.Pq Vt "struct bpf_if *"
245Opaque per-interface data for the packet filter,
246.Xr bpf 4 .
247(Initialized by
248.Fn bpf_attach . )
249.It Va if_index
250.Pq Vt u_short
251A unique number assigned to each interface in sequence as it is
252attached.
253This number can be used in a
254.Vt "struct sockaddr_dl"
255to refer to a particular interface by index
256(see
257.Xr link_addr 3 ) .
258.It Va if_unit
259.Pq Vt short
260A unique number assigned to each interface managed by a particular
261driver, usually related to the unit number of a physical device in the
262kernel configuration file
263(see
264.Xr config 8 ) .
265(Initialized by driver.)
266.It Va if_timer
267.Pq Vt short
268Number of seconds until the watchdog timer
269.Fn if_watchdog
270is called, or zero if the timer is disabled.
271(Set by driver,
272decremented by generic watchdog code.)
273.It Va if_flags
274.Pq Vt int
275Flags describing operational parameters of this interface (see below).
276(Manipulated by both driver and generic code.)
277.It Va if_capabilities
278.Pq Vt int
279Flags describing the capabilities the interface supports (see below).
280.It Va if_capenable
281.Pq Vt int
282Flags describing the enabled capabilities of the interface (see below).
283.\" .It Va if_ipending
284.\" Interrupt-pending bits for polled operation:
285.\" .Dv IFI_XMIT
286.\" (transmit complete interrupt)
287.\" and
288.\" .Dv IFI_RECV
289.\" (received packet ready interrupt).
290.\" See the
291.\" .Sx Polling
292.\" section, below.
293.\" (Manipulated by driver.)
294.It Va if_linkmib
295.Pq Vt "void *"
296A pointer to an interface-specific MIB structure exported by
297.Xr ifmib 4 .
298(Initialized by driver.)
299.It Va if_linkmiblen
300.Pq Vt size_t
301The size of said structure.
302(Initialized by driver.)
303.It Va if_data
304.Pq Vt "struct if_data"
305More statistics and information; see
306.Sx "The if_data structure" ,
307below.
308(Initialized by driver, manipulated by both driver and generic
309code.)
310.It Va if_snd
311.Pq Vt "struct ifqueue"
312The output queue.
313(Manipulated by driver.)
314.\".It Va if_poll_slowq
315.\".Pq Vt "struct ifqueue *"
316.\"A pointer to the input queue for devices which do not support polling
317.\"well.
318.\"See the
319.\".Sx Polling
320.\"section, below.
321.\"(Initialized by driver.)
322.El
323.Pp
324There are in addition a number of function pointers which the driver
325must initialize to complete its interface with the generic interface
326layer:
327.Bl -ohang -offset indent
328.It Fn if_output
329Output a packet on interface
330.Fa ifp ,
331or queue it on the output queue if the interface is already active.
332.It Fn if_start
333Start queued output on an interface.
334This function is exposed in
335order to provide for some interface classes to share a
336.Fn if_output
337among all drivers.
338.Fn if_start
339may only be called when the
340.Dv IFF_OACTIVE
341flag is not set.
342(Thus,
343.Dv IFF_OACTIVE
344does not literally mean that output is active, but rather that the
345device's internal output queue is full.)
346.It Fn if_done
347Not used.
348We are not even sure what it was ever for.
349The prototype is faked.
350.It Fn if_ioctl
351Process interface-related
352.Xr ioctl 2
353requests
354(defined in
355.In sys/sockio.h ) .
356Preliminary processing is done by the generic routine
357.Fn ifioctl
358to check for appropriate privileges, locate the interface being
359manipulated, and perform certain generic operations like twiddling
360flags and flushing queues.
361See the description of
362.Fn ifioctl
363below for more information.
364.It Fn if_watchdog
365Routine called by the generic code when the watchdog timer,
366.Va if_timer ,
367expires.
368Usually this will reset the interface.
369.\" .It Fn if_poll_recv
370.\" .It Fn if_poll_xmit
371.\" .It Fn if_poll_slowinput
372.\" .It Fn if_poll_intren
373.\" See the
374.\" .Sx Polling
375.\" section, below.
376.It Fn if_init
377Initialize and bring up the hardware,
378e.g., reset the chip and the watchdog timer and enable the receiver unit.
379Should mark the interface running,
380but not active
381.Dv ( IFF_RUNNING , ~IIF_OACTIVE ) .
382.It Fn if_resolvemulti
383Check the requested multicast group membership,
384.Fa addr ,
385for validity, and if necessary compute a link-layer group which
386corresponds to that address which is returned in
387.Fa *retsa .
388Returns zero on success, or an error code on failure.
389.El
390.Ss "Interface Flags"
391Interface flags are used for a number of different purposes.
392Some
393flags simply indicate information about the type of interface and its
394capabilities; others are dynamically manipulated to reflect the
395current state of the interface.
396Flags of the former kind are marked
397.Aq S
398in this table; the latter are marked
399.Aq D .
400.Pp
401.Bl -tag -width ".Dv IFF_POINTOPOINT" -offset indent -compact
402.It Dv IFF_UP
403.Aq D
404The interface has been configured up by the user-level code.
405.It Dv IFF_BROADCAST
406.Aq S*
407The interface supports broadcast.
408.It Dv IFF_DEBUG
409.Aq D
410Used to enable/disable driver debugging code.
411.It Dv IFF_LOOPBACK
412.Aq S
413The interface is a loopback device.
414.It Dv IFF_POINTOPOINT
415.Aq S*
416The interface is point-to-point;
417.Dq broadcast
418address is actually the address of the other end.
419.It Dv IFF_RUNNING
420.Aq D*
421The interface has been configured and dynamic resources were
422successfully allocated.
423Probably only useful internal to the
424interface.
425.It Dv IFF_NOARP
426.Aq D
427Disable network address resolution on this interface.
428.It Dv IFF_PROMISC
429.Aq D*
430This interface is in promiscuous mode.
431.It Dv IFF_PPROMISC
432.Aq D
433This interface is in the permanently promiscuous mode (implies
434.Dv IFF_PROMISC ) .
435.It Dv IFF_ALLMULTI
436.Aq D*
437This interface is in all-multicasts mode (used by multicast routers).
438.It Dv IFF_OACTIVE
439.Aq D*
440The interface's hardware output queue (if any) is full; output packets
441are to be queued.
442.It Dv IFF_SIMPLEX
443.Aq S*
444The interface cannot hear its own transmissions.
445.It Dv IFF_LINK0
446.It Dv IFF_LINK1
447.It Dv IFF_LINK2
448.Aq D
449Control flags for the link layer.
450(Currently abused to select among
451multiple physical layers on some devices.)
452.It Dv IFF_MULTICAST
453.Aq S*
454This interface supports multicast.
455.El
456.Pp
457The macro
458.Dv IFF_CANTCHANGE
459defines the bits which cannot be set by a user program using the
460.Dv SIOCSIFFLAGS
461command to
462.Xr ioctl 2 ;
463these are indicated by an asterisk in the listing above.
464.Ss "Interface Capabilities Flags"
465Interface capabilities are specialized features an interface may
466or may not support.
467These capabilities are very hardware-specific
468and allow, when enabled,
469to offload specific network processing to the interface.
470.Bl -tag -width ".Dv IFCAP_NETCONS" -offset indent
471.It Dv IFCAP_NETCONS
472This interface can be a network console.
473.It Dv IFCAP_RXCSUM
474This interface can do checksum validation on receiving data.
475Some interfaces do not have sufficient buffer storage to store frames
476above a certain MTU-size completely.
477The driver for the interface might disable hardware checksum validation
478if the MTU is set above the hardcoded limit.
479.It Dv IFCAP_TXCSUM
480This interface can do checksum calculation on transmitting data.
481.El
482.Ss The Vt if_data Ss Structure
483In
484.Bx 4.4 ,
485a subset of the interface information believed to be of interest to
486management stations was segregated from the
487.Vt ifnet
488structure and moved into its own
489.Vt if_data
490structure to facilitate its use by user programs.
491The following elements of the
492.Vt if_data
493structure are initialized by the interface and are not expected to change
494significantly over the course of normal operation:
495.Bl -tag -width ".Va ifi_lastchange" -offset indent
496.It Va ifi_type
497.Pq Vt u_char
498The type of the interface, as defined in
499.In net/if_types.h
500and described below in the
501.Sx "Interface Types"
502section.
503.It Va ifi_physical
504.Pq Vt u_char
505Intended to represent a selection of physical layers on devices which
506support more than one; never implemented.
507.It Va ifi_addrlen
508.Pq Vt u_char
509Length of a link-layer address on this device, or zero if there are
510none.
511Used to initialized the address length field in
512.Vt sockaddr_dl
513structures referring to this interface.
514.It Va ifi_hdrlen
515.Pq Vt u_char
516Maximum length of any link-layer header which might be prepended by
517the driver to a packet before transmission.
518The generic code computes
519the maximum over all interfaces and uses that value to influence the
520placement of data in
521.Vt mbuf Ns s
522to attempt to ensure that there is always
523sufficient space to prepend a link-layer header without allocating an
524additional
525.Vt mbuf .
526.\" (See
527.\" .Xr mbuf 9 . )
528.\" .It Va ifi_recvquota
529.\" .Pq Vt u_char
530.\" Number of packets the interface is permitted to receive at one time
531.\" when in polled mode.
532.\" .It Va ifi_xmitquota
533.\" .Pq Vt u_char
534.\" Number of packets the interface is permitted to queue for transmission
535.\" at one time when in polled mode.
536.\" There is some controversy over
537.\" whether such a restriction makes any sense at all.
538.It Va ifi_mtu
539.Pq Vt u_long
540The maximum transmission unit of the medium, exclusive of any
541link-layer overhead.
542.It Va ifi_metric
543.Pq Vt u_long
544A dimensionless metric interpreted by a user-mode routing process.
545.It Va ifi_baudrate
546.Pq Vt u_long
547The line rate of the interface, in bits per second.
548.El
549.Pp
550The structure additionally contains generic statistics applicable to a
551variety of different interface types (except as noted, all members are
552of type
553.Vt u_long ) :
554.Bl -tag -width ".Va ifi_lastchange" -offset indent
555.It Va ifi_ipackets
556Number of packets received.
557.It Va ifi_ierrors
558Number of receive errors detected (e.g., FCS errors, DMA overruns,
559etc.).
560More detailed breakdowns can often be had by way of a
561link-specific MIB.
562.It Va ifi_opackets
563Number of packets transmitted.
564.It Va ifi_oerrors
565Number of output errors detected (e.g., late collisions, DMA overruns,
566etc.).
567More detailed breakdowns can often be had by way of a
568link-specific MIB.
569.It Va ifi_collisions
570Total number of collisions detected on output for CSMA interfaces.
571(This member is sometimes [ab]used by other types of interfaces for
572other output error counts.)
573.It Va ifi_ibytes
574Total traffic received, in bytes.
575.It Va ifi_obytes
576Total traffic transmitted, in bytes.
577.It Va ifi_imcasts
578Number of packets received which were sent by link-layer multicast.
579.It Va ifi_omcasts
580Number of packets sent by link-layer multicast.
581.It Va ifi_iqdrops
582Number of packets dropped on input.
583Rarely implemented.
584.It Va ifi_noproto
585Number of packets received for unknown network-layer protocol.
586.\" .It Va ifi_recvtiming
587.\" Amount of time, in microseconds, spent to receive an average packet on
588.\" this interface.
589.\" See the
590.\" .Sx Polling
591.\" section, below.
592.\" .It Va ifi_xmittiming
593.\" Amount of time, in microseconds, spent to service a transmit-complete
594.\" interrupt on this interface.
595.\" See the
596.\" .Sx Polling
597.\" section, below.
598.It Va ifi_lastchange
599.Pq Vt "struct timeval"
600The time of the last administrative change to the interface (as required
601for
602.Tn SNMP ) .
603.El
604.Ss Interface Types
605The header file
606.In net/if_types.h
607defines symbolic constants for a number of different types of
608interfaces.
609The most common are:
610.Pp
611.Bl -tag -offset indent -width ".Dv IFT_PROPVIRTUAL" -compact
612.It Dv IFT_OTHER
613none of the following
614.It Dv IFT_ETHER
615Ethernet
616.It Dv IFT_ISO88023
617ISO 8802-3 CSMA/CD
618.It Dv IFT_ISO88024
619ISO 8802-4 Token Bus
620.It Dv IFT_ISO88025
621ISO 8802-5 Token Ring
622.It Dv IFT_ISO88026
623ISO 8802-6 DQDB MAN
624.It Dv IFT_FDDI
625FDDI
626.It Dv IFT_PPP
627Internet Point-to-Point Protocol
628.Pq Xr ppp 8
629.It Dv IFT_LOOP
630The loopback
631.Pq Xr lo 4
632interface
633.It Dv IFT_SLIP
634Serial Line IP
635.It Dv IFT_PARA
636Parallel-port IP
637.Pq Dq Tn PLIP
638.It Dv IFT_ATM
639Asynchronous Transfer Mode
640.El
641.Ss The Vt ifaddr Ss Structure
642Every interface is associated with a list
643(or, rather, a
644.Li TAILQ )
645of addresses, rooted at the interface structure's
646.Va if_addrlist
647member.
648The first element in this list is always an
649.Dv AF_LINK
650address representing the interface itself; multi-access network
651drivers should complete this structure by filling in their link-layer
652addresses after calling
653.Fn if_attach .
654Other members of the structure represent network-layer addresses which
655have been configured by means of the
656.Dv SIOCAIFADDR
657command to
658.Xr ioctl 2 ,
659called on a socket of the appropriate protocol family.
660The elements of this list consist of
661.Vt ifaddr
662structures.
663Most protocols will declare their own protocol-specific
664interface address structures, but all begin with a
665.Vt "struct ifaddr"
666which provides the most-commonly-needed functionality across all
667protocols.
668Interface addresses are reference-counted.
669.Pp
670The members of
671.Vt "struct ifaddr"
672are as follows:
673.Bl -tag -width ".Va ifa_rtrequest" -offset indent
674.It Va ifa_addr
675.Pq Vt "struct sockaddr *"
676The local address of the interface.
677.It Va ifa_dstaddr
678.Pq Vt "struct sockaddr *"
679The remote address of point-to-point interfaces, and the broadcast
680address of broadcast interfaces.
681.Va ( ifa_broadaddr
682is a macro for
683.Va ifa_dstaddr . )
684.It Va ifa_netmask
685.Pq Vt "struct sockaddr *"
686The network mask for multi-access interfaces, and the confusion
687generator for point-to-point interfaces.
688.It Va ifa_ifp
689.Pq Vt "struct ifnet *"
690A link back to the interface structure.
691.It Va ifa_link
692.Pq Fn TAILQ_ENTRY ifaddr
693.Xr queue 3
694glue for list of addresses on each interface.
695.It Va ifa_rtrequest
696See below.
697.It Va ifa_flags
698.Pq Vt u_short
699Some of the flags which would be used for a route representing this
700address in the route table.
701.It Va ifa_refcnt
702.Pq Vt short
703The reference count.
704.It Va ifa_metric
705.Pq Vt int
706A metric associated with this interface address, for the use of some
707external routing protocol.
708.El
709.Pp
710References to
711.Vt ifaddr
712structures are gained manually, by incrementing the
713.Va ifa_refcnt
714member.
715References are released by calling either the
716.Fn ifafree
717function or the
718.Fn IFAFREE
719macro.
720.Pp
721.Fn ifa_rtrequest
722is a pointer to a function which receives callouts from the routing
723code
724.Pq Fn rtrequest
725to perform link-layer-specific actions upon requests to add, resolve,
726or delete routes.
727The
728.Fa cmd
729argument indicates the request in question:
730.Dv RTM_ADD , RTM_RESOLVE ,
731or
732.Dv RTM_DELETE .
733The
734.Fa rt
735argument is the route in question; the
736.Fa dst
737argument is the specific destination being manipulated
738for
739.Dv RTM_RESOLVE ,
740or a null pointer otherwise.
741.Sh FUNCTIONS
742The functions provided by the generic interface code can be divided
743into two groups: those which manipulate interfaces, and those which
744manipulate interface addresses.
745In addition to these functions, there
746may also be link-layer support routines which are used by a number of
747drivers implementing a specific link layer over different hardware;
748see the documentation for that link layer for more details.
749.Ss The Vt ifmultiaddr Ss Structure
750Every multicast-capable interface is associated with a list of
751multicast group memberships, which indicate at a low level which
752link-layer multicast addresses (if any) should be accepted, and at a
753high level, in which network-layer multicast groups a user process has
754expressed interest.
755.Pp
756The elements of the structure are as follows:
757.Bl -tag -width ".Va ifma_refcount" -offset indent
758.It Va ifma_link
759.Pq Fn LIST_ENTRY ifmultiaddr
760.Xr queue 3
761macro glue.
762.It Va ifma_addr
763.Pq Vt "struct sockaddr *"
764A pointer to the address which this record represents.
765The
766memberships for various address families are stored in arbitrary
767order.
768.It Va ifma_lladdr
769.Pq Vt "struct sockaddr *"
770A pointer to the link-layer multicast address, if any, to which the
771network-layer multicast address in
772.Va ifma_addr
773is mapped, else a null pointer.
774If this element is non-nil, this
775membership also holds an invisible reference to another membership for
776that link-layer address.
777.It Va ifma_refcount
778.Pq Vt u_int
779A reference count of requests for this particular membership.
780.El
781.Ss Interface Manipulation Functions
782.Bl -ohang -offset indent
783.It Fn if_attach
784Link the specified interface
785.Fa ifp
786into the list of network interfaces.
787Also initialize the list of
788addresses on that interface, and create a link-layer
789.Vt ifaddr
790structure to be the first element in that list.
791(A pointer to
792this address structure is saved in the global array
793.Va ifnet_addrs . )
794.It Fn if_down
795Mark the interface
796.Fa ifp
797as down (i.e.,
798.Dv IFF_UP
799is not set),
800flush its output queue, notify protocols of the transition,
801and generate a message from the
802.Xr route 4
803routing socket.
804.It Fn if_up
805Mark the interface
806.Fa ifp
807as up, notify protocols of the transition,
808and generate a message from the
809.Xr route 4
810routing socket.
811.It Fn ifpromisc
812Add or remove a promiscuous reference to
813.Fa ifp .
814If
815.Fa pswitch
816is true, add a reference;
817if it is false, remove a reference.
818On reference count transitions
819from zero to one and one to zero, set the
820.Dv IFF_PROMISC
821flag appropriately and call
822.Fn if_ioctl
823to set up the interface in the desired mode.
824.It Fn if_allmulti
825As
826.Fn ifpromisc ,
827but for the all-multicasts
828.Pq Dv IFF_ALLMULTI
829flag instead of the promiscuous flag.
830.It Fn ifunit
831Return an
832.Vt ifnet
833pointer for the interface named
834.Fa name .
835.It Fn ifioctl
836Process the ioctl request
837.Fa cmd ,
838issued on socket
839.Fa so
840by thread
841.Fa td ,
842with data parameter
843.Fa data .
844This is the main routine for handling all interface configuration
845requests from user mode.
846It is ordinarily only called from the socket-layer
847.Xr ioctl 2
848handler, and only for commands with class
849.Sq Li i .
850Any unrecognized commands will be passed down to socket
851.Fa so Ns 's
852protocol for
853further interpretation.
854The following commands are handled by
855.Fn ifioctl :
856.Pp
857.Bl -tag -width ".Dv OSIOCGIFNETMASK" -offset indent -compact
858.It Dv SIOCGIFCONF
859.It Dv OSIOCGIFCONF
860Get interface configuration.
861(No call-down to driver.)
862.Pp
863.It Dv SIOCGIFCAP
864.It Dv SIOCGIFFLAGS
865.It Dv SIOCGIFMETRIC
866.It Dv SIOCGIFMTU
867.It Dv SIOCGIFPHYS
868Get interface capabilities, flags, metric, MTU, medium selection.
869(No call-down to driver.)
870.Pp
871.It Dv SIOCSIFCAP
872Enable interface capabilities.
873Caller must have appropriate privilege.
874.Pp
875.It Dv SIOCSIFFLAGS
876Change interface flags.
877Caller must have appropriate privilege.
878If a change to the
879.Dv IFF_UP
880flag is requested,
881.Fn if_up
882or
883.Fn if_down
884is called as appropriate.
885Flags listed in
886.Dv IFF_CANTCHANGE
887are masked off, and the driver
888.Fn if_ioctl
889routine is called to perform any setup
890requested.
891.Pp
892.It Dv SIOCSIFMETRIC
893.It Dv SIOCSIFPHYS
894Change interface metric or medium.
895Caller must have appropriate privilege.
896.Pp
897.It Dv SIOCSIFMTU
898Change interface MTU.
899Caller must have appropriate privilege.
900MTU
901values less than 72 or greater than 65535 are considered invalid.
902The driver
903.Fn if_ioctl
904routine is called to implement the change; it is responsible for any
905additional sanity checking and for actually modifying the MTU in the
906interface structure.
907.Pp
908.It Dv SIOCADDMULTI
909.It Dv SIOCDELMULTI
910Add or delete permanent multicast group memberships on the interface.
911Caller must have appropriate privilege.
912The
913.Fn if_addmulti
914or
915.Fn if_delmulti
916function is called to perform the operation; qq.v.
917.Pp
918.It Dv SIOCSIFDSTADDR
919.It Dv SIOCSIFADDR
920.It Dv SIOCSIFBRDADDR
921.It Dv SIOCSIFNETMASK
922The socket's protocol control routine is called to implement the
923requested action.
924.Pp
925.It Dv OSIOGIFADDR
926.It Dv OSIOCGIFDSTADDR
927.It Dv OSIOCGIFBRDADDR
928.It Dv OSIOCGIFNETMASK
929The socket's protocol control routine is called to implement the
930requested action.
931On return,
932.Vt sockaddr
933structures are converted into old-style (no
934.Va sa_len
935member).
936.El
937.El
938.Pp
939.Fn if_down ,
940.Fn ifioctl ,
941.Fn ifpromisc ,
942and
943.Fn if_up
944must be called at
945.Fn splnet
946or higher.
947.Ss "Interface Address Functions"
948Several functions exist to look up an interface address structure
949given an address.
950.Fn ifa_ifwithaddr
951returns an interface address with either a local address or a
952broadcast address precisely matching the parameter
953.Fa addr .
954.Fn ifa_ifwithdstaddr
955returns an interface address for a point-to-point interface whose
956remote
957.Pq Dq destination
958address is
959.Fa addr .
960.Pp
961.Fn ifa_ifwithnet
962returns the most specific interface address which matches the
963specified address,
964.Fa addr ,
965subject to its configured netmask, or a point-to-point interface
966address whose remote address is
967.Fa addr
968if one is found.
969.Pp
970.Fn ifaof_ifpforaddr
971returns the most specific address configured on interface
972.Fa ifp
973which matches address
974.Fa addr ,
975subject to its configured netmask.
976If the interface is
977point-to-point, only an interface address whose remote address is
978precisely
979.Fa addr
980will be returned.
981.Pp
982All of these functions return a null pointer if no such address can be
983found.
984.Ss "Interface Multicast Address Functions"
985The
986.Fn if_addmulti ,
987.Fn if_delmulti ,
988and
989.Fn ifmaof_ifpforaddr
990functions provide support for requesting and relinquishing multicast
991group memberships, and for querying an interface's membership list,
992respectively.
993The
994.Fn if_addmulti
995function takes a pointer to an interface,
996.Fa ifp ,
997and a generic address,
998.Fa sa .
999It also takes a pointer to a
1000.Vt "struct ifmultiaddr *"
1001which is filled in on successful return with the address of the
1002group membership control block.
1003The
1004.Fn if_addmulti
1005function performs the following four-step process:
1006.Bl -enum -offset indent
1007.It
1008Call the interface's
1009.Fn if_resolvemulti
1010entry point to determine the link-layer address, if any, corresponding
1011to this membership request, and also to give the link layer an
1012opportunity to veto this membership request should it so desire.
1013.It
1014Check the interface's group membership list for a pre-existing
1015membership for this group.
1016If one is not found, allocate a new one;
1017if one is, increment its reference count.
1018.It
1019If the
1020.Fn if_resolvemulti
1021routine returned a link-layer address corresponding to the group,
1022repeat the previous step for that address as well.
1023.It
1024If the interface's multicast address filter needs to be changed
1025because a new membership was added, call the interface's
1026.Fn if_ioctl
1027routine
1028(with a
1029.Fa cmd
1030argument of
1031.Dv SIOCADDMULTI )
1032to request that it do so.
1033.El
1034.Pp
1035The
1036.Fn if_delmulti
1037function, given an interface
1038.Fa ifp
1039and an address,
1040.Fa sa ,
1041reverses this process.
1042Both functions return zero on success, or a
1043standard error number on failure.
1044.Pp
1045The
1046.Fn ifmaof_ifpforaddr
1047function examines the membership list of interface
1048.Fa ifp
1049for an address matching
1050.Fa addr ,
1051and returns a pointer to that
1052.Vt "struct ifmultiaddr"
1053if one is found, else it returns a null pointer.
1054.\" .Sh POLLING
1055.\" XXX write me!
1056.Sh SEE ALSO
1057.Xr ioctl 2 ,
1058.Xr link_addr 3 ,
1059.Xr queue 3 ,
1060.Xr sysctl 3 ,
1061.Xr bpf 4 ,
1062.Xr ifmib 4 ,
1063.Xr lo 4 ,
1064.Xr netintro 4 ,
1065.Xr config 8 ,
1066.Xr ppp 8 ,
1067.\" .Xr mbuf 9 ,
1068.Xr rtentry 9
1069.Rs
1070.%A Gary R. Wright
1071.%A W. Richard Stevens
1072.%B TCP/IP Illustrated
1073.%V Vol. 2
1074.%O Addison-Wesley, ISBN 0-201-63354-X
1075.Re
1076.Sh AUTHORS
1077This manual page was written by
1078.An Garrett A. Wollman .
1079