2 .\" Copyright 1996, 1997 Massachusetts Institute of Technology
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
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
29 .\" $FreeBSD: src/share/man/man9/ifnet.9,v 1.9.2.10 2003/06/15 02:22:30 hmp Exp $
38 .Nd kernel interfaces for manipulating network interfaces
47 .Ss "Interface Manipulation Functions"
49 .Fn if_attach "struct ifnet *ifp"
51 .Fn if_down "struct ifnet *ifp"
53 .Fn ifioctl "struct socket *so" "u_long cmd" "caddr_t data" "struct proc *p"
55 .Fn ifpromisc "struct ifnet *ifp" "int pswitch"
57 .Fn if_allmulti "struct ifnet *ifp" "int amswitch"
59 .Fn ifunit "const char *name"
61 .Fn if_up "struct ifnet *ifp"
63 .Ss "Interface Address Functions"
65 .Fn ifa_ifwithaddr "struct sockaddr *addr"
67 .Fn ifa_ifwithdstaddr "struct sockaddr *addr"
69 .Fn ifa_ifwithnet "struct sockaddr *addr"
71 .Fn ifaof_ifpforaddr "struct sockaddr *addr" "struct ifnet *ifp"
73 .Fn ifafree "struct ifaddr *ifa"
74 .Fn IFAFREE "struct ifaddr *ifa"
76 .Ss "Interface Multicast Address Functions"
78 .Fn if_addmulti "struct ifnet *ifp" "struct sockaddr *sa" "struct ifmultiaddr **ifmap"
80 .Fn if_delmulti "struct ifnet *ifp" "struct sockaddr *sa"
81 .Ft "struct ifmultiaddr *"
82 .Fn ifmaof_ifpforaddr "struct sockaddr *addr" "struct ifnet *ifp"
83 .Ss "Output queue macros"
84 .Fn IF_DEQUEUE "struct ifqueue *ifq" "struct mbuf *m"
86 .Ss "struct ifnet Member Functions"
88 .Fo \*(lp*if_output\*(rp
89 .Fa "struct ifnet *ifp" "struct mbuf *m"
90 .Fa "struct sockaddr *dst" "struct rtentry *rt"
93 .Fn \*(lp*if_start\*(rp "struct ifnet *ifp"
95 .Fn \*(lp*if_done\*(rp "struct ifnet *ifp"
97 .Fn \*(lp*if_ioctl\*(rp "struct ifnet *ifp" "int cmd" "caddr_t data"
99 .Fn \*(lp*if_watchdog\*(rp "struct ifnet *ifp"
101 .Fn \*(lp*if_poll_recv\*(rp "struct ifnet *ifp" "int *quotap"
103 .Fn \*(lp*if_poll_xmit\*(rp "struct ifnet *ifp" "int *quotap"
105 .Fn \*(lp*if_poll_inttrn\*(rp "struct ifnet *ifp"
107 .Fn \*(lp*if_poll_slowinput\*(rp "struct ifnet *ifp" "struct mbuf *m"
109 .Fn \*(lp*if_init\*(rp "void *if_softc"
111 .Fo \*(lp*if_resolvemulti\*(rp
112 .Fa "struct ifnet *ifp" "struct sockaddr **retsa" "struct sockaddr *addr"
114 .Ss "struct ifaddr member function"
116 .Fo \*(lp*ifa_rtrequest\*(rp
117 .Fa "int cmd" "struct rtentry *rt" "struct sockaddr *dst"
120 .Ss "Global Variables"
121 .Vt extern struct ifnethead ifnet ;
122 .Vt extern struct ifaddr **ifnet_addrs ;
123 .Vt extern int if_index ;
124 .Vt extern int ifqmaxlen ;
126 The kernel mechanisms for handling network interfaces reside primarily
128 .Vt ifnet , if_data , ifaddr ,
135 and the functions named above and defined in
137 Those interfaces which are intended to be used by user programs
140 these include the interface flags, the
142 structure, and the structures defining the appearance of
143 interface-related messages on the
145 routing socket and in
149 defines the kernel-internal interfaces, including the
153 structures and the functions which manipulate them.
154 (A few user programs will need
156 because it is the prerequisite of some other header file like
157 .Aq Pa netinet/if_ether.h .
158 Most references to those two files in particular can be replaced by
159 .Aq Pa net/ethernet.h . )
161 The system keeps a linked list of interfaces using the
165 this list is headed by a
166 .Vt "struct ifnethead"
169 The elements of this list are of type
171 and most kernel routines which manipulate interface as such accept or
172 return pointers to these structures.
173 Each interface structure
176 structure, which contains statistics and identifying information used
177 by management programs, and which is exported to user programs by way
183 Each interface also has a
185 of interface addresses, described by
187 structures; the head of the queue is always an
192 describing the link layer implemented by the interface (if any).
193 (Some trivial interfaces do not provide any link layer addresses;
194 this structure, while still present, serves only to identify the
195 interface name and index.)
197 Finally, those interfaces supporting reception of multicast datagrams
200 of multicast group memberships, described by
203 These memberships are reference-counted.
205 Interfaces are also associated with an output queue, defined as a
206 .Vt "struct ifqueue" ;
207 this structure is used to hold packets while the interface is in the
208 process of sending another.
210 .Ss The Vt ifnet Ss structure
214 .Bl -tag -width ".Va if_poll_slowq" -offset indent
217 A pointer to the driver's private state block.
218 (Initialized by driver.)
221 The name of the interface, not including the unit number
226 (Initialized by driver.)
228 .Pq Fn TAILQ_ENTRY ifnet
232 .Pq Vt "struct ifaddrhead"
236 containing the list of addresses assigned to this interface.
239 A count of promiscuous listeners on this interface, used to
244 .Pq Vt "struct bpf_if *"
245 Opaque per-interface data for the packet filter,
251 A unique number assigned to each interface in sequence as it is
253 This number can be used in a
254 .Vt "struct sockaddr_dl"
255 to refer to a particular interface by index
260 A unique number assigned to each interface managed by a particular
261 driver, usually related to the unit number of a physical device in the
262 kernel configuration file
265 (Initialized by driver.)
268 Number of seconds until the watchdog timer
270 is called, or zero if the timer is disabled.
272 decremented by generic watchdog code.)
275 Flags describing operational parameters of this interface (see below).
276 (Manipulated by both driver and generic code.)
277 .\" .It Va if_ipending
278 .\" Interrupt-pending bits for polled operation:
280 .\" (transmit complete interrupt)
283 .\" (received packet ready interrupt).
287 .\" (Manipulated by driver.)
290 A pointer to an interface-specific MIB structure exported by
292 (Initialized by driver.)
295 The size of said structure.
296 (Initialized by driver.)
298 .Pq Vt "struct if_data"
299 More statistics and information; see
300 .Sx "The if_data structure" ,
302 (Initialized by driver, manipulated by both driver and generic
305 .Pq Vt "struct ifqueue"
307 (Manipulated by driver.)
308 .\".It Va if_poll_slowq
309 .\".Pq Vt "struct ifqueue *"
310 .\"A pointer to the input queue for devices which do not support polling
315 .\"(Initialized by driver.)
318 There are in addition a number of function pointers which the driver
319 must initialize to complete its interface with the generic interface
321 .Bl -ohang -offset indent
323 Output a packet on interface
325 or queue it on the output queue if the interface is already active.
327 Start queued output on an interface.
328 This function is exposed in
329 order to provide for some interface classes to share a
333 may only be called when the
338 does not literally mean that output is active, but rather that the
339 device's internal output queue is full.)
342 We are not even sure what it was ever for.
343 The prototype is faked.
345 Process interface-related
349 .Aq Pa sys/sockio.h ) .
350 Preliminary processing is done by the generic routine
352 to check for appropriate privileges, locate the interface being
353 manipulated, and perform certain generic operations like twiddling
354 flags and flushing queues.
355 See the description of
357 below for more information.
359 Routine called by the generic code when the watchdog timer,
362 Usually this will reset the interface.
363 .\" .It Fn if_poll_recv
364 .\" .It Fn if_poll_xmit
365 .\" .It Fn if_poll_slowinput
366 .\" .It Fn if_poll_intren
371 Initialize and bring up the hardware,
372 e.g., reset the chip and the watchdog timer and enable the receiver unit.
373 Should mark the interface running,
375 .Dv ( IFF_RUNNING , ~IIF_OACTIVE ) .
376 .It Fn if_resolvemulti
377 Check the requested multicast group membership,
379 for validity, and if necessary compute a link-layer group which
380 corresponds to that address which is returned in
382 Returns zero on success, or an error code on failure.
384 .Ss "Interface Flags"
385 Interface flags are used for a number of different purposes.
387 flags simply indicate information about the type of interface and its
388 capabilities; others are dynamically manipulated to reflect the
389 current state of the interface.
390 Flags of the former kind are marked
392 in this table; the latter are marked
395 .Bl -tag -width ".Dv IFF_POINTOPOINT" -offset indent -compact
398 The interface has been configured up by the user-level code.
401 The interface supports broadcast.
404 Used to enable/disable driver debugging code.
407 The interface is a loopback device.
408 .It Dv IFF_POINTOPOINT
410 The interface is point-to-point;
412 address is actually the address of the other end.
415 The interface has been configured and dynamic resources were
416 successfully allocated.
417 Probably only useful internal to the
421 Disable network address resolution on this interface.
424 This interface is in promiscuous mode.
427 This interface is in the permanently promiscuous mode (implies
431 This interface is in all-multicasts mode (used by multicast routers).
434 The interface's hardware output queue (if any) is full; output packets
438 The interface cannot hear its own transmissions.
443 Control flags for the link layer.
444 (Currently abused to select among
445 multiple physical layers on some devices.)
448 This interface supports multicast.
453 defines the bits which cannot be set by a user program using the
457 these are indicated by an asterisk in the listing above.
458 .Ss The Vt if_data Ss Structure
461 a subset of the interface information believed to be of interest to
462 management stations was segregated from the
464 structure and moved into its own
466 structure to facilitate its use by user programs.
467 The following elements of the
469 structure are initialized by the interface and are not expected to change
470 significantly over the course of normal operation:
471 .Bl -tag -width ".Va ifi_lastchange" -offset indent
474 The type of the interface, as defined in
475 .Aq Pa net/if_types.h
476 and described below in the
477 .Sx "Interface Types"
481 Intended to represent a selection of physical layers on devices which
482 support more than one; never implemented.
485 Length of a link-layer address on this device, or zero if there are
487 Used to initialized the address length field in
489 structures referring to this interface.
492 Maximum length of any link-layer header which might be prepended by
493 the driver to a packet before transmission.
494 The generic code computes
495 the maximum over all interfaces and uses that value to influence the
498 to attempt to ensure that there is always
499 sufficient space to prepend a link-layer header without allocating an
504 .\" .It Va ifi_recvquota
506 .\" Number of packets the interface is permitted to receive at one time
507 .\" when in polled mode.
508 .\" .It Va ifi_xmitquota
510 .\" Number of packets the interface is permitted to queue for transmission
511 .\" at one time when in polled mode.
512 .\" There is some controversy over
513 .\" whether such a restriction makes any sense at all.
516 The maximum transmission unit of the medium, exclusive of any
520 A dimensionless metric interpreted by a user-mode routing process.
523 The line rate of the interface, in bits per second.
526 The structure additionally contains generic statistics applicable to a
527 variety of different interface types (except as noted, all members are
530 .Bl -tag -width ".Va ifi_lastchange" -offset indent
532 Number of packets received.
534 Number of receive errors detected (e.g., FCS errors, DMA overruns,
536 More detailed breakdowns can often be had by way of a
539 Number of packets transmitted.
541 Number of output errors detected (e.g., late collisions, DMA overruns,
543 More detailed breakdowns can often be had by way of a
545 .It Va ifi_collisions
546 Total number of collisions detected on output for CSMA interfaces.
547 (This member is sometimes [ab]used by other types of interfaces for
548 other output error counts.)
550 Total traffic received, in bytes.
552 Total traffic transmitted, in bytes.
554 Number of packets received which were sent by link-layer multicast.
556 Number of packets sent by link-layer multicast.
558 Number of packets dropped on input.
561 Number of packets received for unknown network-layer protocol.
562 .\" .It Va ifi_recvtiming
563 .\" Amount of time, in microseconds, spent to receive an average packet on
568 .\" .It Va ifi_xmittiming
569 .\" Amount of time, in microseconds, spent to service a transmit-complete
570 .\" interrupt on this interface.
574 .It Va ifi_lastchange
575 .Pq Vt "struct timeval"
576 The time of the last administrative change to the interface (as required
582 .Aq Pa net/if_types.h
583 defines symbolic constants for a number of different types of
587 .Bl -tag -offset indent -width ".Dv IFT_PROPVIRTUAL" -compact
589 none of the following
597 ISO 8802-5 Token Ring
603 Internet Point-to-Point Protocol
615 Asynchronous Transfer Mode
617 .Ss The Vt ifaddr Ss Structure
618 Every interface is associated with a list
621 of addresses, rooted at the interface structure's
624 The first element in this list is always an
626 address representing the interface itself; multi-access network
627 drivers should complete this structure by filling in their link-layer
628 addresses after calling
630 Other members of the structure represent network-layer addresses which
631 have been configured by means of the
635 called on a socket of the appropriate protocol family.
636 The elements of this list consist of
639 Most protocols will declare their own protocol-specific
640 interface address structures, but all begin with a
642 which provides the most-commonly-needed functionality across all
644 Interface addresses are reference-counted.
649 .Bl -tag -width ".Va ifa_rtrequest" -offset indent
651 .Pq Vt "struct sockaddr *"
652 The local address of the interface.
654 .Pq Vt "struct sockaddr *"
655 The remote address of point-to-point interfaces, and the broadcast
656 address of broadcast interfaces.
661 .Pq Vt "struct sockaddr *"
662 The network mask for multi-access interfaces, and the confusion
663 generator for point-to-point interfaces.
665 .Pq Vt "struct ifnet *"
666 A link back to the interface structure.
668 .Pq Fn TAILQ_ENTRY ifaddr
670 glue for list of addresses on each interface.
675 Some of the flags which would be used for a route representing this
676 address in the route table.
682 A metric associated with this interface address, for the use of some
683 external routing protocol.
688 structures are gained manually, by incrementing the
691 References are released by calling either the
698 is a pointer to a function which receives callouts from the routing
701 to perform link-layer-specific actions upon requests to add, resolve,
705 argument indicates the request in question:
706 .Dv RTM_ADD , RTM_RESOLVE ,
711 argument is the route in question; the
713 argument is the specific destination being manipulated
716 or a null pointer otherwise.
718 The functions provided by the generic interface code can be divided
719 into two groups: those which manipulate interfaces, and those which
720 manipulate interface addresses.
721 In addition to these functions, there
722 may also be link-layer support routines which are used by a number of
723 drivers implementing a specific link layer over different hardware;
724 see the documentation for that link layer for more details.
725 .Ss The Vt ifmultiaddr Ss Structure
726 Every multicast-capable interface is associated with a list of
727 multicast group memberships, which indicate at a low level which
728 link-layer multicast addresses (if any) should be accepted, and at a
729 high level, in which network-layer multicast groups a user process has
732 The elements of the structure are as follows:
733 .Bl -tag -width ".Va ifma_refcount" -offset indent
735 .Pq Fn LIST_ENTRY ifmultiaddr
739 .Pq Vt "struct sockaddr *"
740 A pointer to the address which this record represents.
742 memberships for various address families are stored in arbitrary
745 .Pq Vt "struct sockaddr *"
746 A pointer to the link-layer multicast address, if any, to which the
747 network-layer multicast address in
749 is mapped, else a null pointer.
750 If this element is non-nil, this
751 membership also holds an invisible reference to another membership for
752 that link-layer address.
755 A reference count of requests for this particular membership.
757 .Ss Interface Manipulation Functions
758 .Bl -ohang -offset indent
760 Link the specified interface
762 into the list of network interfaces.
763 Also initialize the list of
764 addresses on that interface, and create a link-layer
766 structure to be the first element in that list.
768 this address structure is saved in the global array
776 flush its output queue, notify protocols of the transition,
777 and generate a message from the
783 as up, notify protocols of the transition,
784 and generate a message from the
788 Add or remove a promiscuous reference to
792 is true, add a reference;
793 if it is false, remove a reference.
794 On reference count transitions
795 from zero to one and one to zero, set the
797 flag appropriately and call
799 to set up the interface in the desired mode.
803 but for the all-multicasts
805 flag instead of the promiscuous flag.
809 pointer for the interface named
812 Process the ioctl request
820 This is the main routine for handling all interface configuration
821 requests from user mode.
822 It is ordinarily only called from the socket-layer
824 handler, and only for commands with class
826 Any unrecognized commands will be passed down to socket
829 further interpretation.
830 The following commands are handled by
833 .Bl -tag -width ".Dv OSIOCGIFNETMASK" -offset indent -compact
836 Get interface configuration.
837 (No call-down to driver.)
843 Get interface flags, metric, MTU, medium selection.
844 (No call-down to driver.)
847 Change interface flags.
848 Caller must have appropriate privilege.
855 is called as appropriate.
858 are masked off, and the driver
860 routine is called to perform any setup
865 Change interface metric or medium.
866 Caller must have appropriate privilege.
869 Change interface MTU.
870 Caller must have appropriate privilege.
872 values less than 72 or greater than 65535 are considered invalid.
875 routine is called to implement the change; it is responsible for any
876 additional sanity checking and for actually modifying the MTU in the
881 Add or delete permanent multicast group memberships on the interface.
882 Caller must have appropriate privilege.
887 function is called to perform the operation; qq.v.
889 .It Dv SIOCSIFDSTADDR
891 .It Dv SIOCSIFBRDADDR
892 .It Dv SIOCSIFNETMASK
893 The socket's protocol control routine is called to implement the
897 .It Dv OSIOCGIFDSTADDR
898 .It Dv OSIOCGIFBRDADDR
899 .It Dv OSIOCGIFNETMASK
900 The socket's protocol control routine is called to implement the
904 structures are converted into old-style (no
918 .Ss "Interface Address Functions"
919 Several functions exist to look up an interface address structure
922 returns an interface address with either a local address or a
923 broadcast address precisely matching the parameter
925 .Fn ifa_ifwithdstaddr
926 returns an interface address for a point-to-point interface whose
933 returns the most specific interface address which matches the
936 subject to its configured netmask, or a point-to-point interface
937 address whose remote address is
942 returns the most specific address configured on interface
944 which matches address
946 subject to its configured netmask.
948 point-to-point, only an interface address whose remote address is
953 All of these functions return a null pointer if no such address can be
955 .Ss "Interface Multicast Address Functions"
960 .Fn ifmaof_ifpforaddr
961 functions provide support for requesting and relinquishing multicast
962 group memberships, and for querying an interface's membership list,
966 function takes a pointer to an interface,
968 and a generic address,
970 It also takes a pointer to a
971 .Vt "struct ifmultiaddr *"
972 which is filled in on successful return with the address of the
973 group membership control block.
976 function performs the following four-step process:
977 .Bl -enum -offset indent
981 entry point to determine the link-layer address, if any, corresponding
982 to this membership request, and also to give the link layer an
983 opportunity to veto this membership request should it so desire.
985 Check the interface's group membership list for a pre-existing
986 membership for this group.
987 If one is not found, allocate a new one;
988 if one is, increment its reference count.
992 routine returned a link-layer address corresponding to the group,
993 repeat the previous step for that address as well.
995 If the interface's multicast address filter needs to be changed
996 because a new membership was added, call the interface's
1003 to request that it do so.
1008 function, given an interface
1012 reverses this process.
1013 Both functions return zero on success, or a
1014 standard error number on failure.
1017 .Fn ifmaof_ifpforaddr
1018 function examines the membership list of interface
1020 for an address matching
1022 and returns a pointer to that
1023 .Vt "struct ifmultiaddr"
1024 if one is found, else it returns a null pointer.
1042 .%A W. Richard Stevens
1043 .%B TCP/IP Illustrated
1045 .%O Addison-Wesley, ISBN 0-201-63354-X
1048 This manual page was written by
1049 .An Garrett A. Wollman .