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 $
30 .\" $DragonFly: src/share/man/man9/ifnet.9,v 1.8 2007/11/23 23:03:57 swildner Exp $
39 .Nd kernel interfaces for manipulating network interfaces
48 .Ss "Interface Manipulation Functions"
50 .Fn if_attach "struct ifnet *ifp"
52 .Fn if_down "struct ifnet *ifp"
54 .Fn ifioctl "struct socket *so" "u_long cmd" "caddr_t data" "struct proc *p"
56 .Fn ifpromisc "struct ifnet *ifp" "int pswitch"
58 .Fn if_allmulti "struct ifnet *ifp" "int amswitch"
60 .Fn ifunit "const char *name"
62 .Fn if_up "struct ifnet *ifp"
64 .Ss "Interface Address Functions"
66 .Fn ifa_ifwithaddr "struct sockaddr *addr"
68 .Fn ifa_ifwithdstaddr "struct sockaddr *addr"
70 .Fn ifa_ifwithnet "struct sockaddr *addr"
72 .Fn ifaof_ifpforaddr "struct sockaddr *addr" "struct ifnet *ifp"
74 .Fn ifafree "struct ifaddr *ifa"
75 .Fn IFAFREE "struct ifaddr *ifa"
77 .Ss "Interface Multicast Address Functions"
79 .Fn if_addmulti "struct ifnet *ifp" "struct sockaddr *sa" "struct ifmultiaddr **ifmap"
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 .Fn IF_DEQUEUE "struct ifqueue *ifq" "struct mbuf *m"
87 .Ss "struct ifnet Member Functions"
89 .Fo \*(lp*if_output\*(rp
90 .Fa "struct ifnet *ifp" "struct mbuf *m"
91 .Fa "struct sockaddr *dst" "struct rtentry *rt"
94 .Fn \*(lp*if_start\*(rp "struct ifnet *ifp"
96 .Fn \*(lp*if_done\*(rp "struct ifnet *ifp"
98 .Fn \*(lp*if_ioctl\*(rp "struct ifnet *ifp" "int cmd" "caddr_t data"
100 .Fn \*(lp*if_watchdog\*(rp "struct ifnet *ifp"
102 .Fn \*(lp*if_poll_recv\*(rp "struct ifnet *ifp" "int *quotap"
104 .Fn \*(lp*if_poll_xmit\*(rp "struct ifnet *ifp" "int *quotap"
106 .Fn \*(lp*if_poll_inttrn\*(rp "struct ifnet *ifp"
108 .Fn \*(lp*if_poll_slowinput\*(rp "struct ifnet *ifp" "struct mbuf *m"
110 .Fn \*(lp*if_init\*(rp "void *if_softc"
112 .Fo \*(lp*if_resolvemulti\*(rp
113 .Fa "struct ifnet *ifp" "struct sockaddr **retsa" "struct sockaddr *addr"
115 .Ss "struct ifaddr member function"
117 .Fo \*(lp*ifa_rtrequest\*(rp
118 .Fa "int cmd" "struct rtentry *rt" "struct sockaddr *dst"
121 .Ss "Global Variables"
122 .Vt extern struct ifnethead ifnet ;
123 .Vt extern struct ifaddr **ifnet_addrs ;
124 .Vt extern int if_index ;
125 .Vt extern int ifqmaxlen ;
127 The kernel mechanisms for handling network interfaces reside primarily
129 .Vt ifnet , if_data , ifaddr ,
136 and the functions named above and defined in
138 Those interfaces which are intended to be used by user programs
141 these include the interface flags, the
143 structure, and the structures defining the appearance of
144 interface-related messages on the
146 routing socket and in
150 defines the kernel-internal interfaces, including the
154 structures and the functions which manipulate them.
155 (A few user programs will need
157 because it is the prerequisite of some other header file like
158 .In netinet/if_ether.h .
159 Most references to those two files in particular can be replaced by
160 .In net/ethernet.h . )
162 The system keeps a linked list of interfaces using the
166 this list is headed by a
167 .Vt "struct ifnethead"
170 The elements of this list are of type
172 and most kernel routines which manipulate interface as such accept or
173 return pointers to these structures.
174 Each interface structure
177 structure, which contains statistics and identifying information used
178 by management programs, and which is exported to user programs by way
184 Each interface also has a
186 of interface addresses, described by
188 structures; the head of the queue is always an
193 describing the link layer implemented by the interface (if any).
194 (Some trivial interfaces do not provide any link layer addresses;
195 this structure, while still present, serves only to identify the
196 interface name and index.)
198 Finally, those interfaces supporting reception of multicast datagrams
201 of multicast group memberships, described by
204 These memberships are reference-counted.
206 Interfaces are also associated with an output queue, defined as a
207 .Vt "struct ifqueue" ;
208 this structure is used to hold packets while the interface is in the
209 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.)
220 .Pq Fn TAILQ_ENTRY ifnet
225 The name of the interface,
230 (Initialized by driver.)
232 .Pq Vt "const char *"
233 The name of the driver.
234 (Initialized by driver.)
237 A unique number assigned to each interface managed by a particular
239 Drivers may choose to set this to
241 if a unit number is not associated with the device.
242 (Initialized by driver.)
244 .Pq Vt "struct ifaddrhead"
248 containing the list of addresses assigned to this interface.
251 A count of promiscuous listeners on this interface, used to
256 .Pq Vt "struct bpf_if *"
257 Opaque per-interface data for the packet filter,
263 A unique number assigned to each interface in sequence as it is
265 This number can be used in a
266 .Vt "struct sockaddr_dl"
267 to refer to a particular interface by index
272 Number of seconds until the watchdog timer
274 is called, or zero if the timer is disabled.
276 decremented by generic watchdog code.)
279 Flags describing operational parameters of this interface (see below).
280 (Manipulated by both driver and generic code.)
281 .\" .It Va if_ipending
282 .\" Interrupt-pending bits for polled operation:
284 .\" (transmit complete interrupt)
287 .\" (received packet ready interrupt).
291 .\" (Manipulated by driver.)
294 A pointer to an interface-specific MIB structure exported by
296 (Initialized by driver.)
299 The size of said structure.
300 (Initialized by driver.)
302 .Pq Vt "struct if_data"
303 More statistics and information; see
304 .Sx "The if_data structure" ,
306 (Initialized by driver, manipulated by both driver and generic
309 .Pq Vt "struct ifqueue"
311 (Manipulated by driver.)
312 .\".It Va if_poll_slowq
313 .\".Pq Vt "struct ifqueue *"
314 .\"A pointer to the input queue for devices which do not support polling
319 .\"(Initialized by driver.)
322 There are in addition a number of function pointers which the driver
323 must initialize to complete its interface with the generic interface
325 .Bl -ohang -offset indent
327 Output a packet on interface
329 or queue it on the output queue if the interface is already active.
331 Start queued output on an interface.
332 This function is exposed in
333 order to provide for some interface classes to share a
337 may only be called when the
342 does not literally mean that output is active, but rather that the
343 device's internal output queue is full.)
346 We are not even sure what it was ever for.
347 The prototype is faked.
349 Process interface-related
354 Preliminary processing is done by the generic routine
356 to check for appropriate privileges, locate the interface being
357 manipulated, and perform certain generic operations like twiddling
358 flags and flushing queues.
359 See the description of
361 below for more information.
363 Routine called by the generic code when the watchdog timer,
366 Usually this will reset the interface.
367 .\" .It Fn if_poll_recv
368 .\" .It Fn if_poll_xmit
369 .\" .It Fn if_poll_slowinput
370 .\" .It Fn if_poll_intren
375 Initialize and bring up the hardware,
376 e.g., reset the chip and the watchdog timer and enable the receiver unit.
377 Should mark the interface running,
379 .Dv ( IFF_RUNNING , ~IFF_OACTIVE ) .
380 .It Fn if_resolvemulti
381 Check the requested multicast group membership,
383 for validity, and if necessary compute a link-layer group which
384 corresponds to that address which is returned in
386 Returns zero on success, or an error code on failure.
388 .Ss "Interface Flags"
389 Interface flags are used for a number of different purposes.
391 flags simply indicate information about the type of interface and its
392 capabilities; others are dynamically manipulated to reflect the
393 current state of the interface.
394 Flags of the former kind are marked
396 in this table; the latter are marked
399 .Bl -tag -width ".Dv IFF_POINTOPOINT" -offset indent -compact
402 The interface has been configured up by the user-level code.
405 The interface supports broadcast.
408 Used to enable/disable driver debugging code.
411 The interface is a loopback device.
412 .It Dv IFF_POINTOPOINT
414 The interface is point-to-point;
416 address is actually the address of the other end.
419 The interface has been configured and dynamic resources were
420 successfully allocated.
421 Probably only useful internal to the
425 Disable network address resolution on this interface.
428 This interface is in promiscuous mode.
431 This interface is in the permanently promiscuous mode (implies
435 This interface is in all-multicasts mode (used by multicast routers).
438 The interface's hardware output queue (if any) is full; output packets
442 The interface cannot hear its own transmissions.
447 Control flags for the link layer.
448 (Currently abused to select among
449 multiple physical layers on some devices.)
452 This interface supports multicast.
457 defines the bits which cannot be set by a user program using the
461 these are indicated by an asterisk in the listing above.
462 .Ss The Vt if_data Ss Structure
465 a subset of the interface information believed to be of interest to
466 management stations was segregated from the
468 structure and moved into its own
470 structure to facilitate its use by user programs.
471 The following elements of the
473 structure are initialized by the interface and are not expected to change
474 significantly over the course of normal operation:
475 .Bl -tag -width ".Va ifi_lastchange" -offset indent
478 The type of the interface, as defined in
480 and described below in the
481 .Sx "Interface Types"
485 Intended to represent a selection of physical layers on devices which
486 support more than one; never implemented.
489 Length of a link-layer address on this device, or zero if there are
491 Used to initialized the address length field in
493 structures referring to this interface.
496 Maximum length of any link-layer header which might be prepended by
497 the driver to a packet before transmission.
498 The generic code computes
499 the maximum over all interfaces and uses that value to influence the
502 to attempt to ensure that there is always
503 sufficient space to prepend a link-layer header without allocating an
508 .\" .It Va ifi_recvquota
510 .\" Number of packets the interface is permitted to receive at one time
511 .\" when in polled mode.
512 .\" .It Va ifi_xmitquota
514 .\" Number of packets the interface is permitted to queue for transmission
515 .\" at one time when in polled mode.
516 .\" There is some controversy over
517 .\" whether such a restriction makes any sense at all.
520 The maximum transmission unit of the medium, exclusive of any
524 A dimensionless metric interpreted by a user-mode routing process.
527 The line rate of the interface, in bits per second.
530 The structure additionally contains generic statistics applicable to a
531 variety of different interface types (except as noted, all members are
534 .Bl -tag -width ".Va ifi_lastchange" -offset indent
536 Number of packets received.
538 Number of receive errors detected (e.g., FCS errors, DMA overruns,
540 More detailed breakdowns can often be had by way of a
543 Number of packets transmitted.
545 Number of output errors detected (e.g., late collisions, DMA overruns,
547 More detailed breakdowns can often be had by way of a
549 .It Va ifi_collisions
550 Total number of collisions detected on output for CSMA interfaces.
551 (This member is sometimes [ab]used by other types of interfaces for
552 other output error counts.)
554 Total traffic received, in bytes.
556 Total traffic transmitted, in bytes.
558 Number of packets received which were sent by link-layer multicast.
560 Number of packets sent by link-layer multicast.
562 Number of packets dropped on input.
565 Number of packets received for unknown network-layer protocol.
566 .\" .It Va ifi_recvtiming
567 .\" Amount of time, in microseconds, spent to receive an average packet on
572 .\" .It Va ifi_xmittiming
573 .\" Amount of time, in microseconds, spent to service a transmit-complete
574 .\" interrupt on this interface.
578 .It Va ifi_lastchange
579 .Pq Vt "struct timeval"
580 The time of the last administrative change to the interface (as required
587 defines symbolic constants for a number of different types of
591 .Bl -tag -offset indent -width ".Dv IFT_PROPVIRTUAL" -compact
593 none of the following
601 ISO 8802-5 Token Ring
607 Internet Point-to-Point Protocol
619 Asynchronous Transfer Mode
621 .Ss The Vt ifaddr Ss Structure
622 Every interface is associated with a list
625 of addresses, rooted at the interface structure's
628 The first element in this list is always an
630 address representing the interface itself; multi-access network
631 drivers should complete this structure by filling in their link-layer
632 addresses after calling
634 Other members of the structure represent network-layer addresses which
635 have been configured by means of the
639 called on a socket of the appropriate protocol family.
640 The elements of this list consist of
643 Most protocols will declare their own protocol-specific
644 interface address structures, but all begin with a
646 which provides the most-commonly-needed functionality across all
648 Interface addresses are reference-counted.
653 .Bl -tag -width ".Va ifa_rtrequest" -offset indent
655 .Pq Vt "struct sockaddr *"
656 The local address of the interface.
658 .Pq Vt "struct sockaddr *"
659 The remote address of point-to-point interfaces, and the broadcast
660 address of broadcast interfaces.
665 .Pq Vt "struct sockaddr *"
666 The network mask for multi-access interfaces, and the confusion
667 generator for point-to-point interfaces.
669 .Pq Vt "struct ifnet *"
670 A link back to the interface structure.
672 .Pq Fn TAILQ_ENTRY ifaddr
674 glue for list of addresses on each interface.
679 Some of the flags which would be used for a route representing this
680 address in the route table.
686 A metric associated with this interface address, for the use of some
687 external routing protocol.
692 structures are gained manually, by incrementing the
695 References are released by calling either the
702 is a pointer to a function which receives callouts from the routing
705 to perform link-layer-specific actions upon requests to add, resolve,
709 argument indicates the request in question:
710 .Dv RTM_ADD , RTM_RESOLVE ,
715 argument is the route in question; the
717 argument is the specific destination being manipulated
720 or a null pointer otherwise.
722 The functions provided by the generic interface code can be divided
723 into two groups: those which manipulate interfaces, and those which
724 manipulate interface addresses.
725 In addition to these functions, there
726 may also be link-layer support routines which are used by a number of
727 drivers implementing a specific link layer over different hardware;
728 see the documentation for that link layer for more details.
729 .Ss The Vt ifmultiaddr Ss Structure
730 Every multicast-capable interface is associated with a list of
731 multicast group memberships, which indicate at a low level which
732 link-layer multicast addresses (if any) should be accepted, and at a
733 high level, in which network-layer multicast groups a user process has
736 The elements of the structure are as follows:
737 .Bl -tag -width ".Va ifma_refcount" -offset indent
739 .Pq Fn LIST_ENTRY ifmultiaddr
743 .Pq Vt "struct sockaddr *"
744 A pointer to the address which this record represents.
746 memberships for various address families are stored in arbitrary
749 .Pq Vt "struct sockaddr *"
750 A pointer to the link-layer multicast address, if any, to which the
751 network-layer multicast address in
753 is mapped, else a null pointer.
754 If this element is non-nil, this
755 membership also holds an invisible reference to another membership for
756 that link-layer address.
759 A reference count of requests for this particular membership.
761 .Ss Interface Manipulation Functions
762 .Bl -ohang -offset indent
764 Link the specified interface
766 into the list of network interfaces.
767 Also initialize the list of
768 addresses on that interface, and create a link-layer
770 structure to be the first element in that list.
772 this address structure is saved in the global array
780 flush its output queue, notify protocols of the transition,
781 and generate a message from the
787 as up, notify protocols of the transition,
788 and generate a message from the
792 Add or remove a promiscuous reference to
796 is true, add a reference;
797 if it is false, remove a reference.
798 On reference count transitions
799 from zero to one and one to zero, set the
801 flag appropriately and call
803 to set up the interface in the desired mode.
807 but for the all-multicasts
809 flag instead of the promiscuous flag.
813 pointer for the interface named
816 Process the ioctl request
824 This is the main routine for handling all interface configuration
825 requests from user mode.
826 It is ordinarily only called from the socket-layer
828 handler, and only for commands with class
830 Any unrecognized commands will be passed down to socket
833 further interpretation.
834 The following commands are handled by
837 .Bl -tag -width ".Dv OSIOCGIFNETMASK" -offset indent -compact
840 Get interface configuration.
841 (No call-down to driver.)
847 Get interface flags, metric, MTU, medium selection.
848 (No call-down to driver.)
851 Change interface flags.
852 Caller must have appropriate privilege.
859 is called as appropriate.
862 are masked off, and the driver
864 routine is called to perform any setup
869 Change interface metric or medium.
870 Caller must have appropriate privilege.
873 Change interface MTU.
874 Caller must have appropriate privilege.
876 values less than 72 or greater than 65535 are considered invalid.
879 routine is called to implement the change; it is responsible for any
880 additional sanity checking and for actually modifying the MTU in the
885 Add or delete permanent multicast group memberships on the interface.
886 Caller must have appropriate privilege.
891 function is called to perform the operation; qq.v.
893 .It Dv SIOCSIFDSTADDR
895 .It Dv SIOCSIFBRDADDR
896 .It Dv SIOCSIFNETMASK
897 The socket's protocol control routine is called to implement the
901 .It Dv OSIOCGIFDSTADDR
902 .It Dv OSIOCGIFBRDADDR
903 .It Dv OSIOCGIFNETMASK
904 The socket's protocol control routine is called to implement the
908 structures are converted into old-style (no
919 must be called inside a critical section.
920 .Ss "Interface Address Functions"
921 Several functions exist to look up an interface address structure
924 returns an interface address with either a local address or a
925 broadcast address precisely matching the parameter
927 .Fn ifa_ifwithdstaddr
928 returns an interface address for a point-to-point interface whose
935 returns the most specific interface address which matches the
938 subject to its configured netmask, or a point-to-point interface
939 address whose remote address is
944 returns the most specific address configured on interface
946 which matches address
948 subject to its configured netmask.
950 point-to-point, only an interface address whose remote address is
955 All of these functions return a null pointer if no such address can be
957 .Ss "Interface Multicast Address Functions"
962 .Fn ifmaof_ifpforaddr
963 functions provide support for requesting and relinquishing multicast
964 group memberships, and for querying an interface's membership list,
968 function takes a pointer to an interface,
970 and a generic address,
972 It also takes a pointer to a
973 .Vt "struct ifmultiaddr *"
974 which is filled in on successful return with the address of the
975 group membership control block.
978 function performs the following four-step process:
979 .Bl -enum -offset indent
983 entry point to determine the link-layer address, if any, corresponding
984 to this membership request, and also to give the link layer an
985 opportunity to veto this membership request should it so desire.
987 Check the interface's group membership list for a pre-existing
988 membership for this group.
989 If one is not found, allocate a new one;
990 if one is, increment its reference count.
994 routine returned a link-layer address corresponding to the group,
995 repeat the previous step for that address as well.
997 If the interface's multicast address filter needs to be changed
998 because a new membership was added, call the interface's
1005 to request that it do so.
1010 function, given an interface
1014 reverses this process.
1015 Both functions return zero on success, or a
1016 standard error number on failure.
1019 .Fn ifmaof_ifpforaddr
1020 function examines the membership list of interface
1022 for an address matching
1024 and returns a pointer to that
1025 .Vt "struct ifmultiaddr"
1026 if one is found, else it returns a null pointer.
1044 .%A W. Richard Stevens
1045 .%B TCP/IP Illustrated
1047 .%O Addison-Wesley, ISBN 0-201-63354-X
1050 This manual page was written by
1051 .An Garrett A. Wollman .