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