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.2 2003/06/17 04:37:01 dillon 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 .Aq Pa netinet/if_ether.h .
159 Most references to those two files in particular can be replaced by
160 .Aq Pa 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.
211 .Ss The Vt ifnet Ss structure
215 .Bl -tag -width ".Va if_poll_slowq" -offset indent
218 A pointer to the driver's private state block.
219 (Initialized by driver.)
222 The name of the interface, not including the unit number
227 (Initialized by driver.)
229 .Pq Fn TAILQ_ENTRY ifnet
233 .Pq Vt "struct ifaddrhead"
237 containing the list of addresses assigned to this interface.
240 A count of promiscuous listeners on this interface, used to
245 .Pq Vt "struct bpf_if *"
246 Opaque per-interface data for the packet filter,
252 A unique number assigned to each interface in sequence as it is
254 This number can be used in a
255 .Vt "struct sockaddr_dl"
256 to refer to a particular interface by index
261 A unique number assigned to each interface managed by a particular
262 driver, usually related to the unit number of a physical device in the
263 kernel configuration file
266 (Initialized by driver.)
269 Number of seconds until the watchdog timer
271 is called, or zero if the timer is disabled.
273 decremented by generic watchdog code.)
276 Flags describing operational parameters of this interface (see below).
277 (Manipulated by both driver and generic code.)
278 .\" .It Va if_ipending
279 .\" Interrupt-pending bits for polled operation:
281 .\" (transmit complete interrupt)
284 .\" (received packet ready interrupt).
288 .\" (Manipulated by driver.)
291 A pointer to an interface-specific MIB structure exported by
293 (Initialized by driver.)
296 The size of said structure.
297 (Initialized by driver.)
299 .Pq Vt "struct if_data"
300 More statistics and information; see
301 .Sx "The if_data structure" ,
303 (Initialized by driver, manipulated by both driver and generic
306 .Pq Vt "struct ifqueue"
308 (Manipulated by driver.)
309 .\".It Va if_poll_slowq
310 .\".Pq Vt "struct ifqueue *"
311 .\"A pointer to the input queue for devices which do not support polling
316 .\"(Initialized by driver.)
319 There are in addition a number of function pointers which the driver
320 must initialize to complete its interface with the generic interface
322 .Bl -ohang -offset indent
324 Output a packet on interface
326 or queue it on the output queue if the interface is already active.
328 Start queued output on an interface.
329 This function is exposed in
330 order to provide for some interface classes to share a
334 may only be called when the
339 does not literally mean that output is active, but rather that the
340 device's internal output queue is full.)
343 We are not even sure what it was ever for.
344 The prototype is faked.
346 Process interface-related
350 .Aq Pa sys/sockio.h ) .
351 Preliminary processing is done by the generic routine
353 to check for appropriate privileges, locate the interface being
354 manipulated, and perform certain generic operations like twiddling
355 flags and flushing queues.
356 See the description of
358 below for more information.
360 Routine called by the generic code when the watchdog timer,
363 Usually this will reset the interface.
364 .\" .It Fn if_poll_recv
365 .\" .It Fn if_poll_xmit
366 .\" .It Fn if_poll_slowinput
367 .\" .It Fn if_poll_intren
372 Initialize and bring up the hardware,
373 e.g., reset the chip and the watchdog timer and enable the receiver unit.
374 Should mark the interface running,
376 .Dv ( IFF_RUNNING , ~IIF_OACTIVE ) .
377 .It Fn if_resolvemulti
378 Check the requested multicast group membership,
380 for validity, and if necessary compute a link-layer group which
381 corresponds to that address which is returned in
383 Returns zero on success, or an error code on failure.
385 .Ss "Interface Flags"
386 Interface flags are used for a number of different purposes.
388 flags simply indicate information about the type of interface and its
389 capabilities; others are dynamically manipulated to reflect the
390 current state of the interface.
391 Flags of the former kind are marked
393 in this table; the latter are marked
396 .Bl -tag -width ".Dv IFF_POINTOPOINT" -offset indent -compact
399 The interface has been configured up by the user-level code.
402 The interface supports broadcast.
405 Used to enable/disable driver debugging code.
408 The interface is a loopback device.
409 .It Dv IFF_POINTOPOINT
411 The interface is point-to-point;
413 address is actually the address of the other end.
416 The interface has been configured and dynamic resources were
417 successfully allocated.
418 Probably only useful internal to the
422 Disable network address resolution on this interface.
425 This interface is in promiscuous mode.
428 This interface is in the permanently promiscuous mode (implies
432 This interface is in all-multicasts mode (used by multicast routers).
435 The interface's hardware output queue (if any) is full; output packets
439 The interface cannot hear its own transmissions.
444 Control flags for the link layer.
445 (Currently abused to select among
446 multiple physical layers on some devices.)
449 This interface supports multicast.
454 defines the bits which cannot be set by a user program using the
458 these are indicated by an asterisk in the listing above.
459 .Ss The Vt if_data Ss Structure
462 a subset of the interface information believed to be of interest to
463 management stations was segregated from the
465 structure and moved into its own
467 structure to facilitate its use by user programs.
468 The following elements of the
470 structure are initialized by the interface and are not expected to change
471 significantly over the course of normal operation:
472 .Bl -tag -width ".Va ifi_lastchange" -offset indent
475 The type of the interface, as defined in
476 .Aq Pa net/if_types.h
477 and described below in the
478 .Sx "Interface Types"
482 Intended to represent a selection of physical layers on devices which
483 support more than one; never implemented.
486 Length of a link-layer address on this device, or zero if there are
488 Used to initialized the address length field in
490 structures referring to this interface.
493 Maximum length of any link-layer header which might be prepended by
494 the driver to a packet before transmission.
495 The generic code computes
496 the maximum over all interfaces and uses that value to influence the
499 to attempt to ensure that there is always
500 sufficient space to prepend a link-layer header without allocating an
505 .\" .It Va ifi_recvquota
507 .\" Number of packets the interface is permitted to receive at one time
508 .\" when in polled mode.
509 .\" .It Va ifi_xmitquota
511 .\" Number of packets the interface is permitted to queue for transmission
512 .\" at one time when in polled mode.
513 .\" There is some controversy over
514 .\" whether such a restriction makes any sense at all.
517 The maximum transmission unit of the medium, exclusive of any
521 A dimensionless metric interpreted by a user-mode routing process.
524 The line rate of the interface, in bits per second.
527 The structure additionally contains generic statistics applicable to a
528 variety of different interface types (except as noted, all members are
531 .Bl -tag -width ".Va ifi_lastchange" -offset indent
533 Number of packets received.
535 Number of receive errors detected (e.g., FCS errors, DMA overruns,
537 More detailed breakdowns can often be had by way of a
540 Number of packets transmitted.
542 Number of output errors detected (e.g., late collisions, DMA overruns,
544 More detailed breakdowns can often be had by way of a
546 .It Va ifi_collisions
547 Total number of collisions detected on output for CSMA interfaces.
548 (This member is sometimes [ab]used by other types of interfaces for
549 other output error counts.)
551 Total traffic received, in bytes.
553 Total traffic transmitted, in bytes.
555 Number of packets received which were sent by link-layer multicast.
557 Number of packets sent by link-layer multicast.
559 Number of packets dropped on input.
562 Number of packets received for unknown network-layer protocol.
563 .\" .It Va ifi_recvtiming
564 .\" Amount of time, in microseconds, spent to receive an average packet on
569 .\" .It Va ifi_xmittiming
570 .\" Amount of time, in microseconds, spent to service a transmit-complete
571 .\" interrupt on this interface.
575 .It Va ifi_lastchange
576 .Pq Vt "struct timeval"
577 The time of the last administrative change to the interface (as required
583 .Aq Pa net/if_types.h
584 defines symbolic constants for a number of different types of
588 .Bl -tag -offset indent -width ".Dv IFT_PROPVIRTUAL" -compact
590 none of the following
598 ISO 8802-5 Token Ring
604 Internet Point-to-Point Protocol
616 Asynchronous Transfer Mode
618 .Ss The Vt ifaddr Ss Structure
619 Every interface is associated with a list
622 of addresses, rooted at the interface structure's
625 The first element in this list is always an
627 address representing the interface itself; multi-access network
628 drivers should complete this structure by filling in their link-layer
629 addresses after calling
631 Other members of the structure represent network-layer addresses which
632 have been configured by means of the
636 called on a socket of the appropriate protocol family.
637 The elements of this list consist of
640 Most protocols will declare their own protocol-specific
641 interface address structures, but all begin with a
643 which provides the most-commonly-needed functionality across all
645 Interface addresses are reference-counted.
650 .Bl -tag -width ".Va ifa_rtrequest" -offset indent
652 .Pq Vt "struct sockaddr *"
653 The local address of the interface.
655 .Pq Vt "struct sockaddr *"
656 The remote address of point-to-point interfaces, and the broadcast
657 address of broadcast interfaces.
662 .Pq Vt "struct sockaddr *"
663 The network mask for multi-access interfaces, and the confusion
664 generator for point-to-point interfaces.
666 .Pq Vt "struct ifnet *"
667 A link back to the interface structure.
669 .Pq Fn TAILQ_ENTRY ifaddr
671 glue for list of addresses on each interface.
676 Some of the flags which would be used for a route representing this
677 address in the route table.
683 A metric associated with this interface address, for the use of some
684 external routing protocol.
689 structures are gained manually, by incrementing the
692 References are released by calling either the
699 is a pointer to a function which receives callouts from the routing
702 to perform link-layer-specific actions upon requests to add, resolve,
706 argument indicates the request in question:
707 .Dv RTM_ADD , RTM_RESOLVE ,
712 argument is the route in question; the
714 argument is the specific destination being manipulated
717 or a null pointer otherwise.
719 The functions provided by the generic interface code can be divided
720 into two groups: those which manipulate interfaces, and those which
721 manipulate interface addresses.
722 In addition to these functions, there
723 may also be link-layer support routines which are used by a number of
724 drivers implementing a specific link layer over different hardware;
725 see the documentation for that link layer for more details.
726 .Ss The Vt ifmultiaddr Ss Structure
727 Every multicast-capable interface is associated with a list of
728 multicast group memberships, which indicate at a low level which
729 link-layer multicast addresses (if any) should be accepted, and at a
730 high level, in which network-layer multicast groups a user process has
733 The elements of the structure are as follows:
734 .Bl -tag -width ".Va ifma_refcount" -offset indent
736 .Pq Fn LIST_ENTRY ifmultiaddr
740 .Pq Vt "struct sockaddr *"
741 A pointer to the address which this record represents.
743 memberships for various address families are stored in arbitrary
746 .Pq Vt "struct sockaddr *"
747 A pointer to the link-layer multicast address, if any, to which the
748 network-layer multicast address in
750 is mapped, else a null pointer.
751 If this element is non-nil, this
752 membership also holds an invisible reference to another membership for
753 that link-layer address.
756 A reference count of requests for this particular membership.
758 .Ss Interface Manipulation Functions
759 .Bl -ohang -offset indent
761 Link the specified interface
763 into the list of network interfaces.
764 Also initialize the list of
765 addresses on that interface, and create a link-layer
767 structure to be the first element in that list.
769 this address structure is saved in the global array
777 flush its output queue, notify protocols of the transition,
778 and generate a message from the
784 as up, notify protocols of the transition,
785 and generate a message from the
789 Add or remove a promiscuous reference to
793 is true, add a reference;
794 if it is false, remove a reference.
795 On reference count transitions
796 from zero to one and one to zero, set the
798 flag appropriately and call
800 to set up the interface in the desired mode.
804 but for the all-multicasts
806 flag instead of the promiscuous flag.
810 pointer for the interface named
813 Process the ioctl request
821 This is the main routine for handling all interface configuration
822 requests from user mode.
823 It is ordinarily only called from the socket-layer
825 handler, and only for commands with class
827 Any unrecognized commands will be passed down to socket
830 further interpretation.
831 The following commands are handled by
834 .Bl -tag -width ".Dv OSIOCGIFNETMASK" -offset indent -compact
837 Get interface configuration.
838 (No call-down to driver.)
844 Get interface flags, metric, MTU, medium selection.
845 (No call-down to driver.)
848 Change interface flags.
849 Caller must have appropriate privilege.
856 is called as appropriate.
859 are masked off, and the driver
861 routine is called to perform any setup
866 Change interface metric or medium.
867 Caller must have appropriate privilege.
870 Change interface MTU.
871 Caller must have appropriate privilege.
873 values less than 72 or greater than 65535 are considered invalid.
876 routine is called to implement the change; it is responsible for any
877 additional sanity checking and for actually modifying the MTU in the
882 Add or delete permanent multicast group memberships on the interface.
883 Caller must have appropriate privilege.
888 function is called to perform the operation; qq.v.
890 .It Dv SIOCSIFDSTADDR
892 .It Dv SIOCSIFBRDADDR
893 .It Dv SIOCSIFNETMASK
894 The socket's protocol control routine is called to implement the
898 .It Dv OSIOCGIFDSTADDR
899 .It Dv OSIOCGIFBRDADDR
900 .It Dv OSIOCGIFNETMASK
901 The socket's protocol control routine is called to implement the
905 structures are converted into old-style (no
919 .Ss "Interface Address Functions"
920 Several functions exist to look up an interface address structure
923 returns an interface address with either a local address or a
924 broadcast address precisely matching the parameter
926 .Fn ifa_ifwithdstaddr
927 returns an interface address for a point-to-point interface whose
934 returns the most specific interface address which matches the
937 subject to its configured netmask, or a point-to-point interface
938 address whose remote address is
943 returns the most specific address configured on interface
945 which matches address
947 subject to its configured netmask.
949 point-to-point, only an interface address whose remote address is
954 All of these functions return a null pointer if no such address can be
956 .Ss "Interface Multicast Address Functions"
961 .Fn ifmaof_ifpforaddr
962 functions provide support for requesting and relinquishing multicast
963 group memberships, and for querying an interface's membership list,
967 function takes a pointer to an interface,
969 and a generic address,
971 It also takes a pointer to a
972 .Vt "struct ifmultiaddr *"
973 which is filled in on successful return with the address of the
974 group membership control block.
977 function performs the following four-step process:
978 .Bl -enum -offset indent
982 entry point to determine the link-layer address, if any, corresponding
983 to this membership request, and also to give the link layer an
984 opportunity to veto this membership request should it so desire.
986 Check the interface's group membership list for a pre-existing
987 membership for this group.
988 If one is not found, allocate a new one;
989 if one is, increment its reference count.
993 routine returned a link-layer address corresponding to the group,
994 repeat the previous step for that address as well.
996 If the interface's multicast address filter needs to be changed
997 because a new membership was added, call the interface's
1004 to request that it do so.
1009 function, given an interface
1013 reverses this process.
1014 Both functions return zero on success, or a
1015 standard error number on failure.
1018 .Fn ifmaof_ifpforaddr
1019 function examines the membership list of interface
1021 for an address matching
1023 and returns a pointer to that
1024 .Vt "struct ifmultiaddr"
1025 if one is found, else it returns a null pointer.
1043 .%A W. Richard Stevens
1044 .%B TCP/IP Illustrated
1046 .%O Addison-Wesley, ISBN 0-201-63354-X
1049 This manual page was written by
1050 .An Garrett A. Wollman .