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