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