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