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