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