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 $
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"
73 .Fn IFAFREE "struct ifaddr *ifa"
75 .Ss "Interface Multicast Address Functions"
77 .Fn if_addmulti "struct ifnet *ifp" "struct sockaddr *sa" "struct ifmultiaddr **ifmap"
79 .Fn if_delmulti "struct ifnet *ifp" "struct sockaddr *sa"
80 .Ft "struct ifmultiaddr *"
81 .Fn ifmaof_ifpforaddr "struct sockaddr *addr" "struct ifnet *ifp"
82 .Ss "Output queue macros"
83 .Fn IF_DEQUEUE "struct ifqueue *ifq" "struct mbuf *m"
85 .Ss "struct ifnet Member Functions"
88 .Fa "struct ifnet *ifp" "struct mbuf *m"
89 .Fa "struct sockaddr *dst" "struct rtentry *rt"
93 .Fa "struct ifnet *ifp" "struct mbuf *m"
94 .Fa "const struct pktinfo *pi" "int cpuid"
97 .Fn (*if_start) "struct ifnet *ifp" "struct ifaltq_subque *ifsq"
100 .Fa "struct ifnet *ifp" "u_long command" "caddr_t data" "struct ucred *cr"
103 .Fn (*if_watchdog) "struct ifnet *ifp"
105 .Fn (*if_init) "void *if_softc"
107 .Fo (*if_resolvemulti)
108 .Fa "struct ifnet *ifp" "struct sockaddr **retsa" "struct sockaddr *addr"
111 .Fn (*if_poll) "struct ifnet *ifp" "enum poll_cmd cmd" "int count"
112 .Ss "struct ifaddr member function"
115 .Fa "int cmd" "struct rtentry *rt" "struct sockaddr *dst"
118 .Ss "Global Variables"
119 .Vt extern struct ifnethead ifnet ;
120 .Vt extern int if_index ;
121 .Vt extern int ifqmaxlen ;
123 The kernel mechanisms for handling network interfaces reside primarily
125 .Vt ifnet , if_data , ifaddr ,
132 and the functions named above and defined in
134 Those interfaces which are intended to be used by user programs
137 these include the interface flags, the
139 structure, and the structures defining the appearance of
140 interface-related messages on the
142 routing socket and in
146 defines the kernel-internal interfaces, including the
150 structures and the functions which manipulate them.
151 (A few user programs will need
153 because it is the prerequisite of some other header file like
154 .In netinet/if_ether.h .
155 Most references to those two files in particular can be replaced by
156 .In net/ethernet.h . )
158 The system keeps a linked list of interfaces using the
162 this list is headed by a
163 .Vt "struct ifnethead"
166 The elements of this list are of type
168 and most kernel routines which manipulate interface as such accept or
169 return pointers to these structures.
170 Each interface structure
173 structure, which contains statistics and identifying information used
174 by management programs, and which is exported to user programs by way
180 Each interface also has a
182 of interface addresses, described by
184 structures; the head of the queue is always an
189 describing the link layer implemented by the interface (if any).
190 (Some trivial interfaces do not provide any link layer addresses;
191 this structure, while still present, serves only to identify the
192 interface name and index.)
194 Finally, those interfaces supporting reception of multicast datagrams
197 of multicast group memberships, described by
200 These memberships are reference-counted.
202 Interfaces are also associated with an output queue, defined as a
203 .Vt "struct ifqueue" ;
204 this structure is used to hold packets while the interface is in the
205 process of sending another.
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.)
243 .\" .It Va if_vlantrunks
247 .Pq Vt "struct ifaddrhead"
251 containing the list of addresses assigned to this interface.
254 A count of promiscuous listeners on this interface, used to
259 .Pq Vt "struct carp_if *"
260 Per-interface data for
263 .Pq Vt "struct bpf_if *"
264 Opaque per-interface data for the packet filter,
270 A unique number assigned to each interface in sequence as it is
272 This number can be used in a
273 .Vt "struct sockaddr_dl"
274 to refer to a particular interface by index
279 Number of seconds until the watchdog timer
281 is called, or zero if the timer is disabled.
283 decremented by generic watchdog code.)
286 Flags describing operational parameters of this interface (see below).
287 (Manipulated by both driver and generic code.)
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
304 .\" .It Va if_poll_cpuid
308 .Pq Vt "struct ifaltq"
309 The output queue including
311 (Manipulated by driver.)
312 .\" .It Va if_broadcastaddr
313 .\" .Pq Vt "const uint8_t"
322 .\" .Pq Vt "struct ifaddr"
324 .\" .It Va if_serializer
325 .\" .Pq Vt "struct lwkt_serialize"
327 .\" .It Va if_default_serializer
328 .\" .Pq Vt "struct lwkt_serialize"
332 There are in addition a number of function pointers which the driver
333 must initialize to complete its interface with the generic interface
335 .Bl -ohang -offset indent
337 Output a packet on interface
339 or queue it on the output queue if the interface is already active.
341 Start queued output on an interface.
342 This function is exposed in
343 order to provide for some interface classes to share a
347 may only be called when the
352 does not literally mean that output is active, but rather that the
353 device's internal output queue is full.)
355 Process interface-related
360 Preliminary processing is done by the generic routine
362 to check for appropriate privileges, locate the interface being
363 manipulated, and perform certain generic operations like twiddling
364 flags and flushing queues.
365 See the description of
367 below for more information.
369 Routine called by the generic code when the watchdog timer,
372 Usually this will reset the interface.
378 Initialize and bring up the hardware,
379 e.g., reset the chip and the watchdog timer and enable the receiver unit.
380 Should mark the interface running,
382 .Dv ( IFF_RUNNING , ~IFF_OACTIVE ) .
383 .It Fn if_resolvemulti
384 Check the requested multicast group membership,
386 for validity, and if necessary compute a link-layer group which
387 corresponds to that address which is returned in
389 Returns zero on success, or an error code on failure.
391 .Ss "Interface Flags"
392 Interface flags are used for a number of different purposes.
394 flags simply indicate information about the type of interface and its
395 capabilities; others are dynamically manipulated to reflect the
396 current state of the interface.
397 Flags of the former kind are marked
399 in this table; the latter are marked
402 .Bl -tag -width ".Dv IFF_POINTOPOINT" -offset indent -compact
405 The interface has been configured up by the user-level code.
408 The interface supports broadcast.
411 Used to enable/disable driver debugging code.
414 The interface is a loopback device.
415 .It Dv IFF_POINTOPOINT
417 The interface is point-to-point;
419 address is actually the address of the other end.
422 The interface has been configured and dynamic resources were
423 successfully allocated.
424 Probably only useful internal to the
428 Disable network address resolution on this interface.
431 This interface is in promiscuous mode.
434 This interface is in the permanently promiscuous mode (implies
438 This interface is in all-multicasts mode (used by multicast routers).
441 The interface's hardware output queue (if any) is full; output packets
445 The interface cannot hear its own transmissions.
450 Control flags for the link layer.
451 (Currently abused to select among
452 multiple physical layers on some devices.)
455 This interface supports multicast.
457 The interface is in polling mode.
458 .\" .It Dv IFF_MONITOR
464 defines the bits which cannot be set by a user program using the
468 these are indicated by an asterisk in the listing above.
476 a subset of the interface information believed to be of interest to
477 management stations was segregated from the
479 structure and moved into its own
481 structure to facilitate its use by user programs.
482 The following elements of the
484 structure are initialized by the interface and are not expected to change
485 significantly over the course of normal operation:
486 .Bl -tag -width ".Va ifi_lastchange" -offset indent
489 The type of the interface, as defined in
491 and described below in the
492 .Sx "Interface Types"
496 Intended to represent a selection of physical layers on devices which
497 support more than one; never implemented.
500 Length of a link-layer address on this device, or zero if there are
502 Used to initialize the address length field in
504 structures referring to this interface.
507 Maximum length of any link-layer header which might be prepended by
508 the driver to a packet before transmission.
509 The generic code computes
510 the maximum over all interfaces and uses that value to influence the
513 to attempt to ensure that there is always
514 sufficient space to prepend a link-layer header without allocating an
519 .\" .It Va ifi_recvquota
521 .\" Number of packets the interface is permitted to receive at one time
522 .\" when in polled mode.
523 .\" .It Va ifi_xmitquota
525 .\" Number of packets the interface is permitted to queue for transmission
526 .\" at one time when in polled mode.
527 .\" There is some controversy over
528 .\" whether such a restriction makes any sense at all.
531 The maximum transmission unit of the medium, exclusive of any
535 A dimensionless metric interpreted by a user-mode routing process.
536 .It Va ifi_link_state
538 The link state of the interface, either
539 .Dv LINK_STATE_UNKNOWN ,
540 .Dv LINK_STATE_DOWN ,
545 The line rate of the interface, in bits per second.
548 The structure additionally contains generic statistics applicable to a
549 variety of different interface types (except as noted, all members are
552 .Bl -tag -width ".Va ifi_lastchange" -offset indent
554 Number of packets received.
556 Number of receive errors detected (e.g., FCS errors, DMA overruns,
558 More detailed breakdowns can often be had by way of a
561 Number of packets transmitted.
563 Number of output errors detected (e.g., late collisions, DMA overruns,
565 More detailed breakdowns can often be had by way of a
567 .It Va ifi_collisions
568 Total number of collisions detected on output for CSMA interfaces.
569 (This member is sometimes [ab]used by other types of interfaces for
570 other output error counts.)
572 Total traffic received, in bytes.
574 Total traffic transmitted, in bytes.
576 Number of packets received which were sent by link-layer multicast.
578 Number of packets sent by link-layer multicast.
580 Number of packets dropped on input.
583 Number of packets received for unknown network-layer protocol.
584 .It Va ifi_lastchange
585 .Pq Vt "struct timeval"
586 The time of the last administrative change to the interface (as required
593 defines symbolic constants for a number of different types of
597 .Bl -tag -offset indent -width ".Dv IFT_PROPVIRTUAL" -compact
599 none of the following
607 ISO 8802-5 Token Ring
613 Internet Point-to-Point Protocol
625 Asynchronous Transfer Mode
632 Every interface is associated with a list
635 of addresses, rooted at the interface structure's
638 The first element in this list is always an
640 address representing the interface itself; multi-access network
641 drivers should complete this structure by filling in their link-layer
642 addresses after calling
644 Other members of the structure represent network-layer addresses which
645 have been configured by means of the
649 called on a socket of the appropriate protocol family.
650 The elements of this list consist of
653 Most protocols will declare their own protocol-specific
654 interface address structures, but all begin with a
656 which provides the most-commonly-needed functionality across all
658 Interface addresses are reference-counted.
663 .Bl -tag -width ".Va ifa_rtrequest" -offset indent
665 .Pq Vt "struct sockaddr *"
666 The local address of the interface.
668 .Pq Vt "struct sockaddr *"
669 The remote address of point-to-point interfaces, and the broadcast
670 address of broadcast interfaces.
675 .Pq Vt "struct sockaddr *"
676 The network mask for multi-access interfaces, and the confusion
677 generator for point-to-point interfaces.
679 .\" .Pq Vt "struct if_data"
682 .Pq Vt "struct ifnet *"
683 A link back to the interface structure.
684 .It Va ifa_containers
685 .Pq Vt "struct ifaddr_container *"
686 A pointer to an array of
688 structures which hold per-CPU data.
693 Some of the flags which would be used for a route representing this
694 address in the route table.
695 .\" .It Va ifa_cpumask
700 A metric associated with this interface address, for the use of some
701 external routing protocol.
706 structures are gained manually, by incrementing the
708 member of the according
710 structure (such as by calling the
713 References are released by calling the
718 is a pointer to a function which receives callouts from the routing
721 to perform link-layer-specific actions upon requests to add, resolve,
725 argument indicates the request in question:
726 .Dv RTM_ADD , RTM_RESOLVE ,
731 argument is the route in question; the
733 argument is the specific destination being manipulated
736 or a null pointer otherwise.
738 The functions provided by the generic interface code can be divided
739 into two groups: those which manipulate interfaces, and those which
740 manipulate interface addresses.
741 In addition to these functions, there
742 may also be link-layer support routines which are used by a number of
743 drivers implementing a specific link layer over different hardware;
744 see the documentation for that link layer for more details.
750 Every multicast-capable interface is associated with a list of
751 multicast group memberships, which indicate at a low level which
752 link-layer multicast addresses (if any) should be accepted, and at a
753 high level, in which network-layer multicast groups a user process has
756 The elements of the structure are as follows:
757 .Bl -tag -width ".Va ifma_refcount" -offset indent
759 .Pq Fn LIST_ENTRY ifmultiaddr
763 .Pq Vt "struct sockaddr *"
764 A pointer to the address which this record represents.
766 memberships for various address families are stored in arbitrary
769 .Pq Vt "struct sockaddr *"
770 A pointer to the link-layer multicast address, if any, to which the
771 network-layer multicast address in
773 is mapped, else a null pointer.
774 If this element is non-NULL, this
775 membership also holds an invisible reference to another membership for
776 that link-layer address.
779 A reference count of requests for this particular membership.
781 .Ss Interface Manipulation Functions
782 .Bl -ohang -offset indent
784 Link the specified interface
786 into the list of network interfaces.
787 Also initialize the list of
788 addresses on that interface, and create a link-layer
790 structure to be the first element in that list.
792 this address structure is saved in the global array
800 flush its output queue, notify protocols of the transition,
801 and generate a message from the
807 as up, notify protocols of the transition,
808 and generate a message from the
812 Add or remove a promiscuous reference to
816 is true, add a reference;
817 if it is false, remove a reference.
818 On reference count transitions
819 from zero to one and one to zero, set the
821 flag appropriately and call
823 to set up the interface in the desired mode.
827 but for the all-multicasts
829 flag instead of the promiscuous flag.
833 pointer for the interface named
836 Process the ioctl request
844 This is the main routine for handling all interface configuration
845 requests from user mode.
846 It is ordinarily only called from the socket-layer
848 handler, and only for commands with class
850 Any unrecognized commands will be passed down to socket
853 further interpretation.
854 The following commands are handled by
857 .Bl -tag -width ".Dv SIOCSIFDSTADDR" -offset indent -compact
859 Get interface configuration.
860 (No call-down to driver.)
866 Get interface flags, metric, MTU, medium selection.
867 (No call-down to driver.)
870 Change interface flags.
871 Caller must have appropriate privilege.
878 is called as appropriate.
881 are masked off, and the driver
883 routine is called to perform any setup
888 Change interface metric or medium.
889 Caller must have appropriate privilege.
892 Change interface MTU.
893 Caller must have appropriate privilege.
895 values less than 72 or greater than 65535 are considered invalid.
898 routine is called to implement the change; it is responsible for any
899 additional sanity checking and for actually modifying the MTU in the
904 Add or delete permanent multicast group memberships on the interface.
905 Caller must have appropriate privilege.
910 function is called to perform the operation; qq.v.
912 .It Dv SIOCSIFDSTADDR
914 .It Dv SIOCSIFBRDADDR
915 .It Dv SIOCSIFNETMASK
916 The socket's protocol control routine is called to implement the
926 must be called inside a critical section.
927 .Ss "Interface Address Functions"
928 Several functions exist to look up an interface address structure
931 returns an interface address with either a local address or a
932 broadcast address precisely matching the parameter
934 .Fn ifa_ifwithdstaddr
935 returns an interface address for a point-to-point interface whose
942 returns the most specific interface address which matches the
945 subject to its configured netmask, or a point-to-point interface
946 address whose remote address is
951 returns the most specific address configured on interface
953 which matches address
955 subject to its configured netmask.
957 point-to-point, only an interface address whose remote address is
962 All of these functions return a null pointer if no such address can be
964 .Ss "Interface Multicast Address Functions"
969 .Fn ifmaof_ifpforaddr
970 functions provide support for requesting and relinquishing multicast
971 group memberships, and for querying an interface's membership list,
975 function takes a pointer to an interface,
977 and a generic address,
979 It also takes a pointer to a
980 .Vt "struct ifmultiaddr *"
981 which is filled in on successful return with the address of the
982 group membership control block.
985 function performs the following four-step process:
986 .Bl -enum -offset indent
990 entry point to determine the link-layer address, if any, corresponding
991 to this membership request, and also to give the link layer an
992 opportunity to veto this membership request should it so desire.
994 Check the interface's group membership list for a pre-existing
995 membership for this group.
996 If one is not found, allocate a new one;
997 if one is, increment its reference count.
1001 routine returned a link-layer address corresponding to the group,
1002 repeat the previous step for that address as well.
1004 If the interface's multicast address filter needs to be changed
1005 because a new membership was added, call the interface's
1012 to request that it do so.
1017 function, given an interface
1021 reverses this process.
1022 Both functions return zero on success, or a
1023 standard error number on failure.
1026 .Fn ifmaof_ifpforaddr
1027 function examines the membership list of interface
1029 for an address matching
1031 and returns a pointer to that
1032 .Vt "struct ifmultiaddr"
1033 if one is found, else it returns a null pointer.
1050 .%A W. Richard Stevens
1051 .%B TCP/IP Illustrated
1053 .%O Addison-Wesley, ISBN 0-201-63354-X
1056 This manual page was written by
1057 .An Garrett A. Wollman .