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