]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/ifnet.9
This commit was generated by cvs2svn to compensate for changes in r155511,
[FreeBSD/FreeBSD.git] / share / man / man9 / ifnet.9
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 .\"
31 .Dd June 10, 2005
32 .Os
33 .Dt IFNET 9
34 .Sh NAME
35 .Nm ifnet ,
36 .Nm ifaddr ,
37 .Nm ifqueue ,
38 .Nm if_data
39 .Nd kernel interfaces for manipulating network interfaces
40 .Sh SYNOPSIS
41 .In sys/param.h
42 .In sys/time.h
43 .In sys/socket.h
44 .In net/if.h
45 .In net/if_var.h
46 .In net/if_types.h
47 .\"
48 .Ss "Interface Manipulation Functions"
49 .Ft "struct ifnet *"
50 .Fn if_alloc "u_char type"
51 .Ft void
52 .Fn if_attach "struct ifnet *ifp"
53 .Ft void
54 .Fn if_detach "struct ifnet *ifp"
55 .Ft void
56 .Fn if_free "struct ifnet *ifp"
57 .Ft void
58 .Fn if_free_type "struct ifnet *ifp" "u_char type"
59 .Ft void
60 .Fn if_down "struct ifnet *ifp"
61 .Ft int
62 .Fn ifioctl "struct socket *so" "u_long cmd" "caddr_t data" "struct thread *td"
63 .Ft int
64 .Fn ifpromisc "struct ifnet *ifp" "int pswitch"
65 .Ft int
66 .Fn if_allmulti "struct ifnet *ifp" "int amswitch"
67 .Ft "struct ifnet *"
68 .Fn ifunit "const char *name"
69 .Ft void
70 .Fn if_up "struct ifnet *ifp"
71 .\"
72 .Ss "Interface Address Functions"
73 .Ft "struct ifaddr *"
74 .Fn ifa_ifwithaddr "struct sockaddr *addr"
75 .Ft "struct ifaddr *"
76 .Fn ifa_ifwithdstaddr "struct sockaddr *addr"
77 .Ft "struct ifaddr *"
78 .Fn ifa_ifwithnet "struct sockaddr *addr"
79 .Ft "struct ifaddr *"
80 .Fn ifaof_ifpforaddr "struct sockaddr *addr" "struct ifnet *ifp"
81 .Ft void
82 .Fn ifafree "struct ifaddr *ifa"
83 .Fn IFAFREE "struct ifaddr *ifa"
84 .\"
85 .Ss "Interface Multicast Address Functions"
86 .Ft int
87 .Fn if_addmulti "struct ifnet *ifp" "struct sockaddr *sa" "struct ifmultiaddr **ifmap"
88 .Ft int
89 .Fn if_delmulti "struct ifnet *ifp" "struct sockaddr *sa"
90 .Ft "struct ifmultiaddr *"
91 .Fn ifmaof_ifpforaddr "struct sockaddr *addr" "struct ifnet *ifp"
92 .Ss "Output queue macros"
93 .Fn IF_DEQUEUE "struct ifqueue *ifq" "struct mbuf *m"
94 .\"
95 .Ss "struct ifnet Member Functions"
96 .Ft void
97 .Fn \*(lp*if_input\*(rp "struct ifnet *ifp" "struct mbuf *m"
98 .Ft int
99 .Fo \*(lp*if_output\*(rp
100 .Fa "struct ifnet *ifp" "struct mbuf *m"
101 .Fa "struct sockaddr *dst" "struct rtentry *rt"
102 .Fc
103 .Ft void
104 .Fn \*(lp*if_start\*(rp "struct ifnet *ifp"
105 .Ft int
106 .Fn \*(lp*if_done\*(rp "struct ifnet *ifp"
107 .Ft int
108 .Fn \*(lp*if_ioctl\*(rp "struct ifnet *ifp" "int cmd" "caddr_t data"
109 .Ft void
110 .Fn \*(lp*if_watchdog\*(rp "struct ifnet *ifp"
111 .Ft int
112 .Fn \*(lp*if_poll_recv\*(rp "struct ifnet *ifp" "int *quotap"
113 .Ft int
114 .Fn \*(lp*if_poll_xmit\*(rp "struct ifnet *ifp" "int *quotap"
115 .Ft void
116 .Fn \*(lp*if_poll_inttrn\*(rp "struct ifnet *ifp"
117 .Ft void
118 .Fn \*(lp*if_poll_slowinput\*(rp "struct ifnet *ifp" "struct mbuf *m"
119 .Ft void
120 .Fn \*(lp*if_init\*(rp "void *if_softc"
121 .Ft int
122 .Fo \*(lp*if_resolvemulti\*(rp
123 .Fa "struct ifnet *ifp" "struct sockaddr **retsa" "struct sockaddr *addr"
124 .Fc
125 .Ss "struct ifaddr member function"
126 .Ft void
127 .Fo \*(lp*ifa_rtrequest\*(rp
128 .Fa "int cmd" "struct rtentry *rt" "struct sockaddr *dst"
129 .Fc
130 .\"
131 .Ss "Global Variables"
132 .Vt extern struct ifnethead ifnet ;
133 .Vt extern struct ifaddr **ifnet_addrs ;
134 .Vt extern int if_index ;
135 .Vt extern int ifqmaxlen ;
136 .Sh DATA STRUCTURES
137 The kernel mechanisms for handling network interfaces reside primarily
138 in the
139 .Vt ifnet , if_data , ifaddr ,
140 and
141 .Vt ifmultiaddr
142 structures in
143 .In net/if.h
144 and
145 .In net/if_var.h
146 and the functions named above and defined in
147 .Pa /sys/net/if.c .
148 Those interfaces which are intended to be used by user programs
149 are defined in
150 .In net/if.h ;
151 these include the interface flags, the
152 .Vt if_data
153 structure, and the structures defining the appearance of
154 interface-related messages on the
155 .Xr route 4
156 routing socket and in
157 .Xr sysctl 3 .
158 The header file
159 .In net/if_var.h
160 defines the kernel-internal interfaces, including the
161 .Vt ifnet , ifaddr ,
162 and
163 .Vt ifmultiaddr
164 structures and the functions which manipulate them.
165 (A few user programs will need
166 .In net/if_var.h
167 because it is the prerequisite of some other header file like
168 .In netinet/if_ether.h .
169 Most references to those two files in particular can be replaced by
170 .In net/ethernet.h . )
171 .Pp
172 The system keeps a linked list of interfaces using the
173 .Li TAILQ
174 macros defined in
175 .Xr queue 3 ;
176 this list is headed by a
177 .Vt "struct ifnethead"
178 called
179 .Va ifnet .
180 The elements of this list are of type
181 .Vt "struct ifnet" ,
182 and most kernel routines which manipulate interface as such accept or
183 return pointers to these structures.
184 Each interface structure
185 contains an
186 .Vt if_data
187 structure, which contains statistics and identifying information used
188 by management programs, and which is exported to user programs by way
189 of the
190 .Xr ifmib 4
191 branch of the
192 .Xr sysctl 3
193 MIB.
194 Each interface also has a
195 .Li TAILQ
196 of interface addresses, described by
197 .Vt ifaddr
198 structures; the head of the queue is always an
199 .Dv AF_LINK
200 address
201 (see
202 .Xr link_addr 3 )
203 describing the link layer implemented by the interface (if any).
204 (Some trivial interfaces do not provide any link layer addresses;
205 this structure, while still present, serves only to identify the
206 interface name and index.)
207 .Pp
208 Finally, those interfaces supporting reception of multicast datagrams
209 have a
210 .Li TAILQ
211 of multicast group memberships, described by
212 .Vt ifmultiaddr
213 structures.
214 These memberships are reference-counted.
215 .Pp
216 Interfaces are also associated with an output queue, defined as a
217 .Vt "struct ifqueue" ;
218 this structure is used to hold packets while the interface is in the
219 process of sending another.
220 .Pp
221 .Ss The Vt ifnet Ss structure
222 The fields of
223 .Vt "struct ifnet"
224 are as follows:
225 .Bl -tag -width ".Va if_capabilities" -offset indent
226 .It Va if_softc
227 .Pq Vt "void *"
228 A pointer to the driver's private state block.
229 (Initialized by driver.)
230 .It Va if_l2com
231 .Pq Vt "void *"
232 A pointer to the common data for the interface's layer 2 protocol.
233 (Initialized by
234 .Fn if_alloc . )
235 .It Va if_link
236 .Pq Fn TAILQ_ENTRY ifnet
237 .Xr queue 3
238 macro glue.
239 .It Va if_xname
240 .Pq Vt "char *"
241 The name of the interface,
242 (e.g.,
243 .Dq Li fxp0
244 or
245 .Dq Li lo0 ) .
246 (Initialized by driver.)
247 .It Va if_dname
248 .Pq Vt "const char *"
249 The name of the driver.
250 (Initialized by driver.)
251 .It Va if_dunit
252 .Pq Vt int
253 A unique number assigned to each interface managed by a particular
254 driver.
255 Drivers may choose to set this to
256 .Dv IF_DUNIT_NONE
257 if a unit number is not associated with the device.
258 (Initialized by driver.)
259 .It Va if_addrhead
260 .Pq Vt "struct ifaddrhead"
261 The head of the
262 .Xr queue 3
263 .Li TAILQ
264 containing the list of addresses assigned to this interface.
265 .It Va if_pcount
266 .Pq Vt int
267 A count of promiscuous listeners on this interface, used to
268 reference-count the
269 .Dv IFF_PROMISC
270 flag.
271 .It Va if_bpf
272 .Pq Vt "struct bpf_if *"
273 Opaque per-interface data for the packet filter,
274 .Xr bpf 4 .
275 (Initialized by
276 .Fn bpf_attach . )
277 .It Va if_index
278 .Pq Vt u_short
279 A unique number assigned to each interface in sequence as it is
280 attached.
281 This number can be used in a
282 .Vt "struct sockaddr_dl"
283 to refer to a particular interface by index
284 (see
285 .Xr link_addr 3 ) .
286 (Initialized by
287 .Fn if_alloc . )
288 .It Va if_timer
289 .Pq Vt short
290 Number of seconds until the watchdog timer
291 .Fn if_watchdog
292 is called, or zero if the timer is disabled.
293 (Set by driver,
294 decremented by generic watchdog code.)
295 .It Va if_flags
296 .Pq Vt int
297 Flags describing operational parameters of this interface (see below).
298 (Manipulated by both driver and generic code.)
299 .It Va if_capabilities
300 .Pq Vt int
301 Flags describing the capabilities the interface supports (see below).
302 .It Va if_capenable
303 .Pq Vt int
304 Flags describing the enabled capabilities of the interface (see below).
305 .\" .It Va if_ipending
306 .\" Interrupt-pending bits for polled operation:
307 .\" .Dv IFI_XMIT
308 .\" (transmit complete interrupt)
309 .\" and
310 .\" .Dv IFI_RECV
311 .\" (received packet ready interrupt).
312 .\" See the
313 .\" .Sx Polling
314 .\" section, below.
315 .\" (Manipulated by driver.)
316 .It Va if_linkmib
317 .Pq Vt "void *"
318 A pointer to an interface-specific MIB structure exported by
319 .Xr ifmib 4 .
320 (Initialized by driver.)
321 .It Va if_linkmiblen
322 .Pq Vt size_t
323 The size of said structure.
324 (Initialized by driver.)
325 .It Va if_data
326 .Pq Vt "struct if_data"
327 More statistics and information; see
328 .Sx "The if_data structure" ,
329 below.
330 (Initialized by driver, manipulated by both driver and generic
331 code.)
332 .It Va if_snd
333 .Pq Vt "struct ifqueue"
334 The output queue.
335 (Manipulated by driver.)
336 .\".It Va if_poll_slowq
337 .\".Pq Vt "struct ifqueue *"
338 .\"A pointer to the input queue for devices which do not support polling
339 .\"well.
340 .\"See the
341 .\".Sx Polling
342 .\"section, below.
343 .\"(Initialized by driver.)
344 .El
345 .Pp
346 There are in addition a number of function pointers which the driver
347 must initialize to complete its interface with the generic interface
348 layer:
349 .Bl -ohang -offset indent
350 .It Fn if_input
351 Pass a packet to an appropriate upper layer as determined
352 from the link-layer header of the packet.
353 This routine is to be called from an interrupt handler or
354 used to emulate reception of a packet on this interface.
355 A single function implementing
356 .Fn if_input
357 can be shared among multiple drivers utilizing the same link-layer
358 framing, e.g., Ethernet.
359 .It Fn if_output
360 Output a packet on interface
361 .Fa ifp ,
362 or queue it on the output queue if the interface is already active.
363 .It Fn if_start
364 Start queued output on an interface.
365 This function is exposed in
366 order to provide for some interface classes to share a
367 .Fn if_output
368 among all drivers.
369 .Fn if_start
370 may only be called when the
371 .Dv IFF_OACTIVE
372 flag is not set.
373 (Thus,
374 .Dv IFF_OACTIVE
375 does not literally mean that output is active, but rather that the
376 device's internal output queue is full.)
377 .It Fn if_done
378 Not used.
379 We are not even sure what it was ever for.
380 The prototype is faked.
381 .It Fn if_ioctl
382 Process interface-related
383 .Xr ioctl 2
384 requests
385 (defined in
386 .In sys/sockio.h ) .
387 Preliminary processing is done by the generic routine
388 .Fn ifioctl
389 to check for appropriate privileges, locate the interface being
390 manipulated, and perform certain generic operations like twiddling
391 flags and flushing queues.
392 See the description of
393 .Fn ifioctl
394 below for more information.
395 .It Fn if_watchdog
396 Routine called by the generic code when the watchdog timer,
397 .Va if_timer ,
398 expires.
399 Usually this will reset the interface.
400 .\" .It Fn if_poll_recv
401 .\" .It Fn if_poll_xmit
402 .\" .It Fn if_poll_slowinput
403 .\" .It Fn if_poll_intren
404 .\" See the
405 .\" .Sx Polling
406 .\" section, below.
407 .It Fn if_init
408 Initialize and bring up the hardware,
409 e.g., reset the chip and the watchdog timer and enable the receiver unit.
410 Should mark the interface running,
411 but not active
412 .Dv ( IFF_RUNNING , ~IIF_OACTIVE ) .
413 .It Fn if_resolvemulti
414 Check the requested multicast group membership,
415 .Fa addr ,
416 for validity, and if necessary compute a link-layer group which
417 corresponds to that address which is returned in
418 .Fa *retsa .
419 Returns zero on success, or an error code on failure.
420 .El
421 .Ss "Interface Flags"
422 Interface flags are used for a number of different purposes.
423 Some
424 flags simply indicate information about the type of interface and its
425 capabilities; others are dynamically manipulated to reflect the
426 current state of the interface.
427 Flags of the former kind are marked
428 .Aq S
429 in this table; the latter are marked
430 .Aq D .
431 .Pp
432 The macro
433 .Dv IFF_CANTCHANGE
434 defines the bits which cannot be set by a user program using the
435 .Dv SIOCSIFFLAGS
436 command to
437 .Xr ioctl 2 ;
438 these are indicated by an asterisk
439 .Pq Ql *
440 in the following listing.
441 .Pp
442 .Bl -tag -width ".Dv IFF_POINTOPOINT" -offset indent -compact
443 .It Dv IFF_UP
444 .Aq D
445 The interface has been configured up by the user-level code.
446 .It Dv IFF_BROADCAST
447 .Aq S*
448 The interface supports broadcast.
449 .It Dv IFF_DEBUG
450 .Aq D
451 Used to enable/disable driver debugging code.
452 .It Dv IFF_LOOPBACK
453 .Aq S
454 The interface is a loopback device.
455 .It Dv IFF_POINTOPOINT
456 .Aq S*
457 The interface is point-to-point;
458 .Dq broadcast
459 address is actually the address of the other end.
460 .It Dv IFF_RUNNING
461 .Aq D*
462 The interface has been configured and dynamic resources were
463 successfully allocated.
464 Probably only useful internal to the
465 interface.
466 .It Dv IFF_NOARP
467 .Aq D
468 Disable network address resolution on this interface.
469 .It Dv IFF_PROMISC
470 .Aq D*
471 This interface is in promiscuous mode.
472 .It Dv IFF_PPROMISC
473 .Aq D
474 This interface is in the permanently promiscuous mode (implies
475 .Dv IFF_PROMISC ) .
476 .It Dv IFF_ALLMULTI
477 .Aq D*
478 This interface is in all-multicasts mode (used by multicast routers).
479 .It Dv IFF_OACTIVE
480 .Aq D*
481 The interface's hardware output queue (if any) is full; output packets
482 are to be queued.
483 .It Dv IFF_SIMPLEX
484 .Aq S*
485 The interface cannot hear its own transmissions.
486 .It Dv IFF_LINK0
487 .It Dv IFF_LINK1
488 .It Dv IFF_LINK2
489 .Aq D
490 Control flags for the link layer.
491 (Currently abused to select among
492 multiple physical layers on some devices.)
493 .It Dv IFF_MULTICAST
494 .Aq S*
495 This interface supports multicast.
496 .It Dv IFF_POLLING
497 .Aq D*
498 The interface is in
499 .Xr polling 4
500 mode.
501 See
502 .Sx Interface Capabilities Flags
503 for details.
504 .El
505 .Ss "Interface Capabilities Flags"
506 Interface capabilities are specialized features an interface may
507 or may not support.
508 These capabilities are very hardware-specific
509 and allow, when enabled,
510 to offload specific network processing to the interface
511 or to offer a particular feature for use by other kernel parts.
512 .Pp
513 It should be stressed that a capability can be completely
514 uncontrolled (i.e., stay always enabled with no way to disable it)
515 or allow limited control over itself (e.g., depend on another
516 capability's state.)
517 Such peculiarities are determined solely by the hardware and driver
518 of a particular interface.
519 Only the driver possesses
520 the knowledge on whether and how the interface capabilities
521 can be controlled.
522 Consequently, capabilities flags in
523 .Va if_capenable
524 should never be modified directly by kernel code other than
525 the interface driver.
526 The command
527 .Dv SIOCSIFCAP
528 to
529 .Fn ifioctl
530 is the dedicated means to attempt altering
531 .Va if_capenable
532 on an interface.
533 Userland code shall use
534 .Xr ioctl 2 .
535 .Pp
536 The following capabilities are currently supported by the system:
537 .Bl -tag -width ".Dv IFCAP_VLAN_HWTAGGING" -offset indent
538 .It Dv IFCAP_NETCONS
539 This interface can be a network console.
540 .It Dv IFCAP_POLLING
541 This interface supports
542 .Xr polling 4 .
543 See below for details.
544 .It Dv IFCAP_RXCSUM
545 This interface can do checksum validation on receiving data.
546 Some interfaces do not have sufficient buffer storage to store frames
547 above a certain MTU-size completely.
548 The driver for the interface might disable hardware checksum validation
549 if the MTU is set above the hardcoded limit.
550 .It Dv IFCAP_TXCSUM
551 This interface can do checksum calculation on transmitting data.
552 .It Dv IFCAP_HWCSUM
553 A shorthand for
554 .Pq Dv IFCAP_RXCSUM | IFCAP_TXCSUM .
555 .It Dv IFCAP_VLAN_HWTAGGING
556 This interface can do VLAN tagging on output and
557 demultiplex frames by their VLAN tag on input.
558 .It Dv IFCAP_VLAN_MTU
559 The
560 .Xr vlan 4
561 driver can operate over this interface in software tagging mode
562 without having to decrease MTU on
563 .Xr vlan 4
564 interfaces below 1500 bytes.
565 This implies the ability of this interface to cope with frames somewhat
566 longer than permitted by the Ethernet specification.
567 .It Dv IFCAP_JUMBO_MTU
568 This Ethernet interface can transmit and receive frames up to
569 9000 bytes long.
570 .El
571 .Pp
572 The ability of advanced network interfaces to offload certain
573 computational tasks from the host CPU to the board is limited
574 mostly to TCP/IP.
575 Therefore a separate field associated with an interface
576 (see
577 .Va ifnet.if_data.ifi_hwassist
578 below)
579 keeps a detailed description of its enabled capabilities
580 specific to TCP/IP processing.
581 The TCP/IP module consults the field to see which tasks
582 can be done on an
583 .Em outgoing
584 packet by the interface.
585 The flags defined for that field are a superset of those for
586 .Va mbuf.m_pkthdr.csum_flags ,
587 namely:
588 .Bl -tag -width ".Dv CSUM_FRAGMENT" -offset indent
589 .It Dv CSUM_IP
590 The interface will compute IP checksums.
591 .It Dv CSUM_TCP
592 The interface will compute TCP checksums.
593 .It Dv CSUM_UDP
594 The interface will compute UDP checksums.
595 .It Dv CSUM_IP_FRAGS
596 The interface can compute a TCP or UDP checksum for a packet
597 fragmented by the host CPU.
598 Makes sense only along with
599 .Dv CSUM_TCP
600 or
601 .Dv CSUM_UDP .
602 .It Dv CSUM_FRAGMENT
603 The interface will do the fragmentation of IP packets if necessary.
604 The host CPU does not need to care about MTU on this interface
605 as long as a packet to transmit through it is an IP one and it
606 does not exceed the size of the hardware buffer.
607 .El
608 .Pp
609 An interface notifies the TCP/IP module about the tasks
610 the former has performed on an
611 .Em incoming
612 packet by setting the corresponding flags in the field
613 .Va mbuf.m_pkthdr.csum_flags
614 of the
615 .Vt mbuf chain
616 containing the packet.
617 See
618 .Xr mbuf 9
619 for details.
620 .Pp
621 The capability of a network interface to operate in
622 .Xr polling 4
623 mode involves several flags in different
624 global variables and per-interface fields.
625 First, there is a system-wide
626 .Xr sysctl 8
627 master switch named
628 .Va kern.polling.enable ,
629 which can toggle
630 .Xr polling 4
631 globally.
632 If that variable is set to non-zero,
633 .Xr polling 4
634 will be used on those devices where it is enabled individually.
635 Otherwise,
636 .Xr polling 4
637 will not be used in the system.
638 Second, the capability flag
639 .Dv IFCAP_POLLING
640 set in interface's
641 .Va if_capabilities
642 indicates support for
643 .Xr polling 4
644 on the particular interface.
645 If set in
646 .Va if_capabilities ,
647 the same flag can be marked or cleared in the interface's
648 .Va if_capenable ,
649 thus initiating switch of the interface to
650 .Xr polling 4
651 mode or interrupt
652 mode, respectively.
653 The actual mode change will occur at an implementation-specific moment
654 in the future, e.g., during the next interrupt or
655 .Xr polling 4
656 cycle.
657 And finally, if the mode transition has been successful, the flag
658 .Dv IFF_POLLING
659 is marked or cleared in the interface's
660 .Va if_flags
661 to indicate the current mode of the interface.
662 .Ss The Vt if_data Ss Structure
663 In
664 .Bx 4.4 ,
665 a subset of the interface information believed to be of interest to
666 management stations was segregated from the
667 .Vt ifnet
668 structure and moved into its own
669 .Vt if_data
670 structure to facilitate its use by user programs.
671 The following elements of the
672 .Vt if_data
673 structure are initialized by the interface and are not expected to change
674 significantly over the course of normal operation:
675 .Bl -tag -width ".Va ifi_lastchange" -offset indent
676 .It Va ifi_type
677 .Pq Vt u_char
678 The type of the interface, as defined in
679 .In net/if_types.h
680 and described below in the
681 .Sx "Interface Types"
682 section.
683 .It Va ifi_physical
684 .Pq Vt u_char
685 Intended to represent a selection of physical layers on devices which
686 support more than one; never implemented.
687 .It Va ifi_addrlen
688 .Pq Vt u_char
689 Length of a link-layer address on this device, or zero if there are
690 none.
691 Used to initialized the address length field in
692 .Vt sockaddr_dl
693 structures referring to this interface.
694 .It Va ifi_hdrlen
695 .Pq Vt u_char
696 Maximum length of any link-layer header which might be prepended by
697 the driver to a packet before transmission.
698 The generic code computes
699 the maximum over all interfaces and uses that value to influence the
700 placement of data in
701 .Vt mbuf Ns s
702 to attempt to ensure that there is always
703 sufficient space to prepend a link-layer header without allocating an
704 additional
705 .Vt mbuf .
706 .\" (See
707 .\" .Xr mbuf 9 . )
708 .\" .It Va ifi_recvquota
709 .\" .Pq Vt u_char
710 .\" Number of packets the interface is permitted to receive at one time
711 .\" when in polled mode.
712 .\" .It Va ifi_xmitquota
713 .\" .Pq Vt u_char
714 .\" Number of packets the interface is permitted to queue for transmission
715 .\" at one time when in polled mode.
716 .\" There is some controversy over
717 .\" whether such a restriction makes any sense at all.
718 .It Va ifi_datalen
719 .Pq Vt u_char
720 Length of the
721 .Vt if_data
722 structure.
723 Allows some stabilization of the routing socket ABI in the face of
724 increases in the length of
725 .Vt struct ifdata .
726 .It Va ifi_mtu
727 .Pq Vt u_long
728 The maximum transmission unit of the medium, exclusive of any
729 link-layer overhead.
730 .It Va ifi_metric
731 .Pq Vt u_long
732 A dimensionless metric interpreted by a user-mode routing process.
733 .It Va ifi_baudrate
734 .Pq Vt u_long
735 The line rate of the interface, in bits per second.
736 .It Va ifi_hwassist
737 .Pq Vt u_long
738 A detailed interpretation of the capabilities
739 to offload computational tasks for
740 .Em outgoing
741 packets.
742 The interface driver must keep this field in accord with
743 the current value of
744 .Va if_capenable .
745 .It Va ifi_epoch
746 .Pq Vt time_t
747 The system uptime when interface was attached or the statistics
748 below were reset.
749 This is intended to be used to set the SNMP variable
750 .Va ifCounterDiscontinuityTime .
751 It may also be used to determine if two successive queries for an
752 interface of the same index have returned results for the same
753 interface.
754 .El
755 .Pp
756 The structure additionally contains generic statistics applicable to a
757 variety of different interface types (except as noted, all members are
758 of type
759 .Vt u_long ) :
760 .Bl -tag -width ".Va ifi_lastchange" -offset indent
761 .It Va ifi_link_state
762 .Pq Vt u_char
763 The current link state of Ethernet interfaces.
764 See the
765 .Sx Interface Link States
766 section for possible values.
767 .It Va ifi_ipackets
768 Number of packets received.
769 .It Va ifi_ierrors
770 Number of receive errors detected (e.g., FCS errors, DMA overruns,
771 etc.).
772 More detailed breakdowns can often be had by way of a
773 link-specific MIB.
774 .It Va ifi_opackets
775 Number of packets transmitted.
776 .It Va ifi_oerrors
777 Number of output errors detected (e.g., late collisions, DMA overruns,
778 etc.).
779 More detailed breakdowns can often be had by way of a
780 link-specific MIB.
781 .It Va ifi_collisions
782 Total number of collisions detected on output for CSMA interfaces.
783 (This member is sometimes [ab]used by other types of interfaces for
784 other output error counts.)
785 .It Va ifi_ibytes
786 Total traffic received, in bytes.
787 .It Va ifi_obytes
788 Total traffic transmitted, in bytes.
789 .It Va ifi_imcasts
790 Number of packets received which were sent by link-layer multicast.
791 .It Va ifi_omcasts
792 Number of packets sent by link-layer multicast.
793 .It Va ifi_iqdrops
794 Number of packets dropped on input.
795 Rarely implemented.
796 .It Va ifi_noproto
797 Number of packets received for unknown network-layer protocol.
798 .\" .It Va ifi_recvtiming
799 .\" Amount of time, in microseconds, spent to receive an average packet on
800 .\" this interface.
801 .\" See the
802 .\" .Sx Polling
803 .\" section, below.
804 .\" .It Va ifi_xmittiming
805 .\" Amount of time, in microseconds, spent to service a transmit-complete
806 .\" interrupt on this interface.
807 .\" See the
808 .\" .Sx Polling
809 .\" section, below.
810 .It Va ifi_lastchange
811 .Pq Vt "struct timeval"
812 The time of the last administrative change to the interface (as required
813 for
814 .Tn SNMP ) .
815 .El
816 .Ss Interface Types
817 The header file
818 .In net/if_types.h
819 defines symbolic constants for a number of different types of
820 interfaces.
821 The most common are:
822 .Pp
823 .Bl -tag -offset indent -width ".Dv IFT_PROPVIRTUAL" -compact
824 .It Dv IFT_OTHER
825 none of the following
826 .It Dv IFT_ETHER
827 Ethernet
828 .It Dv IFT_ISO88023
829 ISO 8802-3 CSMA/CD
830 .It Dv IFT_ISO88024
831 ISO 8802-4 Token Bus
832 .It Dv IFT_ISO88025
833 ISO 8802-5 Token Ring
834 .It Dv IFT_ISO88026
835 ISO 8802-6 DQDB MAN
836 .It Dv IFT_FDDI
837 FDDI
838 .It Dv IFT_PPP
839 Internet Point-to-Point Protocol
840 .Pq Xr ppp 8
841 .It Dv IFT_LOOP
842 The loopback
843 .Pq Xr lo 4
844 interface
845 .It Dv IFT_SLIP
846 Serial Line IP
847 .It Dv IFT_PARA
848 Parallel-port IP
849 .Pq Dq Tn PLIP
850 .It Dv IFT_ATM
851 Asynchronous Transfer Mode
852 .El
853 .Ss Interface Link States
854 The following link states are currently defined:
855 .Pp
856 .Bl -tag -offset indent -width ".Dv LINK_STATE_UNKNOWN" -compact
857 .It Dv LINK_STATE_UNKNOWN
858 The link is in an invalid or unknown state.
859 .It Dv LINK_STATE_DOWN
860 The link is down.
861 .It Dv LINK_STATE_UP
862 The link is up.
863 .El
864 .Ss The Vt ifaddr Ss Structure
865 Every interface is associated with a list
866 (or, rather, a
867 .Li TAILQ )
868 of addresses, rooted at the interface structure's
869 .Va if_addrlist
870 member.
871 The first element in this list is always an
872 .Dv AF_LINK
873 address representing the interface itself; multi-access network
874 drivers should complete this structure by filling in their link-layer
875 addresses after calling
876 .Fn if_attach .
877 Other members of the structure represent network-layer addresses which
878 have been configured by means of the
879 .Dv SIOCAIFADDR
880 command to
881 .Xr ioctl 2 ,
882 called on a socket of the appropriate protocol family.
883 The elements of this list consist of
884 .Vt ifaddr
885 structures.
886 Most protocols will declare their own protocol-specific
887 interface address structures, but all begin with a
888 .Vt "struct ifaddr"
889 which provides the most-commonly-needed functionality across all
890 protocols.
891 Interface addresses are reference-counted.
892 .Pp
893 The members of
894 .Vt "struct ifaddr"
895 are as follows:
896 .Bl -tag -width ".Va ifa_rtrequest" -offset indent
897 .It Va ifa_addr
898 .Pq Vt "struct sockaddr *"
899 The local address of the interface.
900 .It Va ifa_dstaddr
901 .Pq Vt "struct sockaddr *"
902 The remote address of point-to-point interfaces, and the broadcast
903 address of broadcast interfaces.
904 .Va ( ifa_broadaddr
905 is a macro for
906 .Va ifa_dstaddr . )
907 .It Va ifa_netmask
908 .Pq Vt "struct sockaddr *"
909 The network mask for multi-access interfaces, and the confusion
910 generator for point-to-point interfaces.
911 .It Va ifa_ifp
912 .Pq Vt "struct ifnet *"
913 A link back to the interface structure.
914 .It Va ifa_link
915 .Pq Fn TAILQ_ENTRY ifaddr
916 .Xr queue 3
917 glue for list of addresses on each interface.
918 .It Va ifa_rtrequest
919 See below.
920 .It Va ifa_flags
921 .Pq Vt u_short
922 Some of the flags which would be used for a route representing this
923 address in the route table.
924 .It Va ifa_refcnt
925 .Pq Vt short
926 The reference count.
927 .It Va ifa_metric
928 .Pq Vt int
929 A metric associated with this interface address, for the use of some
930 external routing protocol.
931 .El
932 .Pp
933 References to
934 .Vt ifaddr
935 structures are gained manually, by incrementing the
936 .Va ifa_refcnt
937 member.
938 References are released by calling either the
939 .Fn ifafree
940 function or the
941 .Fn IFAFREE
942 macro.
943 .Pp
944 .Fn ifa_rtrequest
945 is a pointer to a function which receives callouts from the routing
946 code
947 .Pq Fn rtrequest
948 to perform link-layer-specific actions upon requests to add, resolve,
949 or delete routes.
950 The
951 .Fa cmd
952 argument indicates the request in question:
953 .Dv RTM_ADD , RTM_RESOLVE ,
954 or
955 .Dv RTM_DELETE .
956 The
957 .Fa rt
958 argument is the route in question; the
959 .Fa dst
960 argument is the specific destination being manipulated
961 for
962 .Dv RTM_RESOLVE ,
963 or a null pointer otherwise.
964 .Sh FUNCTIONS
965 The functions provided by the generic interface code can be divided
966 into two groups: those which manipulate interfaces, and those which
967 manipulate interface addresses.
968 In addition to these functions, there
969 may also be link-layer support routines which are used by a number of
970 drivers implementing a specific link layer over different hardware;
971 see the documentation for that link layer for more details.
972 .Ss The Vt ifmultiaddr Ss Structure
973 Every multicast-capable interface is associated with a list of
974 multicast group memberships, which indicate at a low level which
975 link-layer multicast addresses (if any) should be accepted, and at a
976 high level, in which network-layer multicast groups a user process has
977 expressed interest.
978 .Pp
979 The elements of the structure are as follows:
980 .Bl -tag -width ".Va ifma_refcount" -offset indent
981 .It Va ifma_link
982 .Pq Fn LIST_ENTRY ifmultiaddr
983 .Xr queue 3
984 macro glue.
985 .It Va ifma_addr
986 .Pq Vt "struct sockaddr *"
987 A pointer to the address which this record represents.
988 The
989 memberships for various address families are stored in arbitrary
990 order.
991 .It Va ifma_lladdr
992 .Pq Vt "struct sockaddr *"
993 A pointer to the link-layer multicast address, if any, to which the
994 network-layer multicast address in
995 .Va ifma_addr
996 is mapped, else a null pointer.
997 If this element is non-nil, this
998 membership also holds an invisible reference to another membership for
999 that link-layer address.
1000 .It Va ifma_refcount
1001 .Pq Vt u_int
1002 A reference count of requests for this particular membership.
1003 .El
1004 .Ss Interface Manipulation Functions
1005 .Bl -ohang -offset indent
1006 .It Fn if_alloc
1007 Allocate and initialize
1008 .Vt "struct ifnet" .
1009 Initialization includes the allocation of an interface index and may
1010 include the allocation of a
1011 .Fa type
1012 specific structure in
1013 .Va if_l2com .
1014 .It Fn if_attach
1015 Link the specified interface
1016 .Fa ifp
1017 into the list of network interfaces.
1018 Also initialize the list of
1019 addresses on that interface, and create a link-layer
1020 .Vt ifaddr
1021 structure to be the first element in that list.
1022 (A pointer to
1023 this address structure is saved in the global array
1024 .Va ifnet_addrs . )
1025 The
1026 .Fa ifp
1027 must have been allocted by
1028 .Fn if_alloc .
1029 .It Fn if_detach
1030 Shut down and unlink the specified
1031 .Fa ifp
1032 from the interface list.
1033 .It Fn if_free
1034 Free the given
1035 .Fa ifp
1036 back to the system.
1037 The interface must have been previously detached if it was ever attached.
1038 .It Fn if_free_type
1039 Identical to
1040 .Fn if_free
1041 except that the given
1042 .Fa type
1043 is used to free
1044 .Va if_l2com
1045 instead of the type in
1046 .Va if_type .
1047 This is intended for use with drivers that change their interface type.
1048 .It Fn if_down
1049 Mark the interface
1050 .Fa ifp
1051 as down (i.e.,
1052 .Dv IFF_UP
1053 is not set),
1054 flush its output queue, notify protocols of the transition,
1055 and generate a message from the
1056 .Xr route 4
1057 routing socket.
1058 .It Fn if_up
1059 Mark the interface
1060 .Fa ifp
1061 as up, notify protocols of the transition,
1062 and generate a message from the
1063 .Xr route 4
1064 routing socket.
1065 .It Fn ifpromisc
1066 Add or remove a promiscuous reference to
1067 .Fa ifp .
1068 If
1069 .Fa pswitch
1070 is true, add a reference;
1071 if it is false, remove a reference.
1072 On reference count transitions
1073 from zero to one and one to zero, set the
1074 .Dv IFF_PROMISC
1075 flag appropriately and call
1076 .Fn if_ioctl
1077 to set up the interface in the desired mode.
1078 .It Fn if_allmulti
1079 As
1080 .Fn ifpromisc ,
1081 but for the all-multicasts
1082 .Pq Dv IFF_ALLMULTI
1083 flag instead of the promiscuous flag.
1084 .It Fn ifunit
1085 Return an
1086 .Vt ifnet
1087 pointer for the interface named
1088 .Fa name .
1089 .It Fn ifioctl
1090 Process the ioctl request
1091 .Fa cmd ,
1092 issued on socket
1093 .Fa so
1094 by thread
1095 .Fa td ,
1096 with data parameter
1097 .Fa data .
1098 This is the main routine for handling all interface configuration
1099 requests from user mode.
1100 It is ordinarily only called from the socket-layer
1101 .Xr ioctl 2
1102 handler, and only for commands with class
1103 .Sq Li i .
1104 Any unrecognized commands will be passed down to socket
1105 .Fa so Ns 's
1106 protocol for
1107 further interpretation.
1108 The following commands are handled by
1109 .Fn ifioctl :
1110 .Pp
1111 .Bl -tag -width ".Dv OSIOCGIFNETMASK" -offset indent -compact
1112 .It Dv SIOCGIFCONF
1113 .It Dv OSIOCGIFCONF
1114 Get interface configuration.
1115 (No call-down to driver.)
1116 .Pp
1117 .It Dv SIOCSIFNAME
1118 Set the interface name.
1119 .Dv RTM_IFANNOUNCE
1120 departure and arrival messages are sent so that
1121 routing code that relies on the interface name will update its interface
1122 list.
1123 Caller must have appropriate privilege.
1124 (No call-down to driver.)
1125 .It Dv SIOCGIFCAP
1126 .It Dv SIOCGIFFLAGS
1127 .It Dv SIOCGIFMETRIC
1128 .It Dv SIOCGIFMTU
1129 .It Dv SIOCGIFPHYS
1130 Get interface capabilities, flags, metric, MTU, medium selection.
1131 (No call-down to driver.)
1132 .Pp
1133 .It Dv SIOCSIFCAP
1134 Enable or disable interface capabilities.
1135 Caller must have appropriate privilege.
1136 Before a call to the driver-specific
1137 .Fn if_ioctl
1138 routine, the requested mask for enabled capabilities is checked
1139 against the mask of capabilities supported by the interface,
1140 .Va if_capabilities .
1141 Requesting to enable an unsupported capability is invalid.
1142 The rest is supposed to be done by the driver,
1143 which includes updating
1144 .Va if_capenable
1145 and
1146 .Va if_data.ifi_hwassist
1147 appropriately.
1148 .Pp
1149 .It Dv SIOCSIFFLAGS
1150 Change interface flags.
1151 Caller must have appropriate privilege.
1152 If a change to the
1153 .Dv IFF_UP
1154 flag is requested,
1155 .Fn if_up
1156 or
1157 .Fn if_down
1158 is called as appropriate.
1159 Flags listed in
1160 .Dv IFF_CANTCHANGE
1161 are masked off, and the field
1162 .Va if_flags
1163 in the interface structure is updated.
1164 Finally, the driver
1165 .Fn if_ioctl
1166 routine is called to perform any setup
1167 requested.
1168 .Pp
1169 .It Dv SIOCSIFMETRIC
1170 .It Dv SIOCSIFPHYS
1171 Change interface metric or medium.
1172 Caller must have appropriate privilege.
1173 .Pp
1174 .It Dv SIOCSIFMTU
1175 Change interface MTU.
1176 Caller must have appropriate privilege.
1177 MTU
1178 values less than 72 or greater than 65535 are considered invalid.
1179 The driver
1180 .Fn if_ioctl
1181 routine is called to implement the change; it is responsible for any
1182 additional sanity checking and for actually modifying the MTU in the
1183 interface structure.
1184 .Pp
1185 .It Dv SIOCADDMULTI
1186 .It Dv SIOCDELMULTI
1187 Add or delete permanent multicast group memberships on the interface.
1188 Caller must have appropriate privilege.
1189 The
1190 .Fn if_addmulti
1191 or
1192 .Fn if_delmulti
1193 function is called to perform the operation; qq.v.
1194 .Pp
1195 .It Dv SIOCSIFDSTADDR
1196 .It Dv SIOCSIFADDR
1197 .It Dv SIOCSIFBRDADDR
1198 .It Dv SIOCSIFNETMASK
1199 The socket's protocol control routine is called to implement the
1200 requested action.
1201 .Pp
1202 .It Dv OSIOGIFADDR
1203 .It Dv OSIOCGIFDSTADDR
1204 .It Dv OSIOCGIFBRDADDR
1205 .It Dv OSIOCGIFNETMASK
1206 The socket's protocol control routine is called to implement the
1207 requested action.
1208 On return,
1209 .Vt sockaddr
1210 structures are converted into old-style (no
1211 .Va sa_len
1212 member).
1213 .El
1214 .El
1215 .Pp
1216 .Fn if_down ,
1217 .Fn ifioctl ,
1218 .Fn ifpromisc ,
1219 and
1220 .Fn if_up
1221 must be called at
1222 .Fn splnet
1223 or higher.
1224 .Ss "Interface Address Functions"
1225 Several functions exist to look up an interface address structure
1226 given an address.
1227 .Fn ifa_ifwithaddr
1228 returns an interface address with either a local address or a
1229 broadcast address precisely matching the parameter
1230 .Fa addr .
1231 .Fn ifa_ifwithdstaddr
1232 returns an interface address for a point-to-point interface whose
1233 remote
1234 .Pq Dq destination
1235 address is
1236 .Fa addr .
1237 .Pp
1238 .Fn ifa_ifwithnet
1239 returns the most specific interface address which matches the
1240 specified address,
1241 .Fa addr ,
1242 subject to its configured netmask, or a point-to-point interface
1243 address whose remote address is
1244 .Fa addr
1245 if one is found.
1246 .Pp
1247 .Fn ifaof_ifpforaddr
1248 returns the most specific address configured on interface
1249 .Fa ifp
1250 which matches address
1251 .Fa addr ,
1252 subject to its configured netmask.
1253 If the interface is
1254 point-to-point, only an interface address whose remote address is
1255 precisely
1256 .Fa addr
1257 will be returned.
1258 .Pp
1259 All of these functions return a null pointer if no such address can be
1260 found.
1261 .Ss "Interface Multicast Address Functions"
1262 The
1263 .Fn if_addmulti ,
1264 .Fn if_delmulti ,
1265 and
1266 .Fn ifmaof_ifpforaddr
1267 functions provide support for requesting and relinquishing multicast
1268 group memberships, and for querying an interface's membership list,
1269 respectively.
1270 The
1271 .Fn if_addmulti
1272 function takes a pointer to an interface,
1273 .Fa ifp ,
1274 and a generic address,
1275 .Fa sa .
1276 It also takes a pointer to a
1277 .Vt "struct ifmultiaddr *"
1278 which is filled in on successful return with the address of the
1279 group membership control block.
1280 The
1281 .Fn if_addmulti
1282 function performs the following four-step process:
1283 .Bl -enum -offset indent
1284 .It
1285 Call the interface's
1286 .Fn if_resolvemulti
1287 entry point to determine the link-layer address, if any, corresponding
1288 to this membership request, and also to give the link layer an
1289 opportunity to veto this membership request should it so desire.
1290 .It
1291 Check the interface's group membership list for a pre-existing
1292 membership for this group.
1293 If one is not found, allocate a new one;
1294 if one is, increment its reference count.
1295 .It
1296 If the
1297 .Fn if_resolvemulti
1298 routine returned a link-layer address corresponding to the group,
1299 repeat the previous step for that address as well.
1300 .It
1301 If the interface's multicast address filter needs to be changed
1302 because a new membership was added, call the interface's
1303 .Fn if_ioctl
1304 routine
1305 (with a
1306 .Fa cmd
1307 argument of
1308 .Dv SIOCADDMULTI )
1309 to request that it do so.
1310 .El
1311 .Pp
1312 The
1313 .Fn if_delmulti
1314 function, given an interface
1315 .Fa ifp
1316 and an address,
1317 .Fa sa ,
1318 reverses this process.
1319 Both functions return zero on success, or a
1320 standard error number on failure.
1321 .Pp
1322 The
1323 .Fn ifmaof_ifpforaddr
1324 function examines the membership list of interface
1325 .Fa ifp
1326 for an address matching
1327 .Fa addr ,
1328 and returns a pointer to that
1329 .Vt "struct ifmultiaddr"
1330 if one is found, else it returns a null pointer.
1331 .Sh SEE ALSO
1332 .Xr ioctl 2 ,
1333 .Xr link_addr 3 ,
1334 .Xr queue 3 ,
1335 .Xr sysctl 3 ,
1336 .Xr bpf 4 ,
1337 .Xr ifmib 4 ,
1338 .Xr lo 4 ,
1339 .Xr netintro 4 ,
1340 .Xr polling 4 ,
1341 .Xr config 8 ,
1342 .Xr ppp 8 ,
1343 .Xr mbuf 9 ,
1344 .Xr rtentry 9
1345 .Rs
1346 .%A Gary R. Wright
1347 .%A W. Richard Stevens
1348 .%B TCP/IP Illustrated
1349 .%V Vol. 2
1350 .%O Addison-Wesley, ISBN 0-201-63354-X
1351 .Re
1352 .Sh AUTHORS
1353 This manual page was written by
1354 .An Garrett A. Wollman .