Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.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: src/share/man/man9/ifnet.9,v 1.9.2.10 2003/06/15 02:22:30 hmp Exp $
30 .\"
31 .Dd May 10, 2008
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/types.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 void
50 .Fn if_attach "struct ifnet *ifp"
51 .Ft void
52 .Fn if_down "struct ifnet *ifp"
53 .Ft int
54 .Fn ifioctl "struct socket *so" "u_long cmd" "caddr_t data" "struct proc *p"
55 .Ft int
56 .Fn ifpromisc "struct ifnet *ifp" "int pswitch"
57 .Ft int
58 .Fn if_allmulti "struct ifnet *ifp" "int amswitch"
59 .Ft "struct ifnet *"
60 .Fn ifunit "const char *name"
61 .Ft void
62 .Fn if_up "struct ifnet *ifp"
63 .\"
64 .Ss "Interface Address Functions"
65 .Ft "struct ifaddr *"
66 .Fn ifa_ifwithaddr "struct sockaddr *addr"
67 .Ft "struct ifaddr *"
68 .Fn ifa_ifwithdstaddr "struct sockaddr *addr"
69 .Ft "struct ifaddr *"
70 .Fn ifa_ifwithnet "struct sockaddr *addr"
71 .Ft "struct ifaddr *"
72 .Fn ifaof_ifpforaddr "struct sockaddr *addr" "struct ifnet *ifp"
73 .Fn IFAFREE "struct ifaddr *ifa"
74 .\"
75 .Ss "Interface Multicast Address Functions"
76 .Ft int
77 .Fn if_addmulti "struct ifnet *ifp" "struct sockaddr *sa" "struct ifmultiaddr **ifmap"
78 .Ft int
79 .Fn if_delmulti "struct ifnet *ifp" "struct sockaddr *sa"
80 .Ft "struct ifmultiaddr *"
81 .Fn ifmaof_ifpforaddr "struct sockaddr *addr" "struct ifnet *ifp"
82 .Ss "Output queue macros"
83 .Fn IF_DEQUEUE "struct ifqueue *ifq" "struct mbuf *m"
84 .\"
85 .Ss "struct ifnet Member Functions"
86 .Ft int
87 .Fo \*(lp*if_output\*(rp
88 .Fa "struct ifnet *ifp" "struct mbuf *m"
89 .Fa "struct sockaddr *dst" "struct rtentry *rt"
90 .Fc
91 .Ft void
92 .Fn \*(lp*if_input\*(rp "struct ifnet *ifp" "struct mbuf *m"
93 .Ft void
94 .Fn \*(lp*if_start\*(rp "struct ifnet *ifp"
95 .Ft int
96 .Fo \*(lp*if_ioctl\*(rp
97 .Fa "struct ifnet *ifp" "u_long command" "caddr_t data" "struct ucred *cr"
98 .Fc
99 .Ft void
100 .Fn \*(lp*if_watchdog\*(rp "struct ifnet *ifp"
101 .Ft void
102 .Fn \*(lp*if_init\*(rp "void *if_softc"
103 .Ft int
104 .Fo \*(lp*if_resolvemulti\*(rp
105 .Fa "struct ifnet *ifp" "struct sockaddr **retsa" "struct sockaddr *addr"
106 .Fc
107 .Ft void
108 .Fn \*(lp*if_poll\*(rp "struct ifnet *ifp" "enum poll_cmd cmd" "int count"
109 .Ss "struct ifaddr member function"
110 .Ft void
111 .Fo \*(lp*ifa_rtrequest\*(rp
112 .Fa "int cmd" "struct rtentry *rt" "struct sockaddr *dst"
113 .Fc
114 .\"
115 .Ss "Global Variables"
116 .Vt extern struct ifnethead ifnet ;
117 .Vt extern int if_index ;
118 .Vt extern int ifqmaxlen ;
119 .Sh DATA STRUCTURES
120 The kernel mechanisms for handling network interfaces reside primarily
121 in the
122 .Vt ifnet , if_data , ifaddr ,
123 and
124 .Vt ifmultiaddr
125 structures in
126 .In net/if.h
127 and
128 .In net/if_var.h
129 and the functions named above and defined in
130 .Pa sys/net/if.c .
131 Those interfaces which are intended to be used by user programs
132 are defined in
133 .In net/if.h ;
134 these include the interface flags, the
135 .Vt if_data
136 structure, and the structures defining the appearance of
137 interface-related messages on the
138 .Xr route 4
139 routing socket and in
140 .Xr sysctl 3 .
141 The header file
142 .In net/if_var.h
143 defines the kernel-internal interfaces, including the
144 .Vt ifnet , ifaddr ,
145 and
146 .Vt ifmultiaddr
147 structures and the functions which manipulate them.
148 (A few user programs will need
149 .In net/if_var.h
150 because it is the prerequisite of some other header file like
151 .In netinet/if_ether.h .
152 Most references to those two files in particular can be replaced by
153 .In net/ethernet.h . )
154 .Pp
155 The system keeps a linked list of interfaces using the
156 .Li TAILQ
157 macros defined in
158 .Xr queue 3 ;
159 this list is headed by a
160 .Vt "struct ifnethead"
161 called
162 .Va ifnet .
163 The elements of this list are of type
164 .Vt "struct ifnet" ,
165 and most kernel routines which manipulate interface as such accept or
166 return pointers to these structures.
167 Each interface structure
168 contains an
169 .Vt if_data
170 structure, which contains statistics and identifying information used
171 by management programs, and which is exported to user programs by way
172 of the
173 .Xr ifmib 4
174 branch of the
175 .Xr sysctl 3
176 MIB.
177 Each interface also has a
178 .Li TAILQ
179 of interface addresses, described by
180 .Vt ifaddr
181 structures; the head of the queue is always an
182 .Dv AF_LINK
183 address
184 (see
185 .Xr link_addr 3 )
186 describing the link layer implemented by the interface (if any).
187 (Some trivial interfaces do not provide any link layer addresses;
188 this structure, while still present, serves only to identify the
189 interface name and index.)
190 .Pp
191 Finally, those interfaces supporting reception of multicast datagrams
192 have a
193 .Li LIST
194 of multicast group memberships, described by
195 .Vt ifmultiaddr
196 structures.
197 These memberships are reference-counted.
198 .Pp
199 Interfaces are also associated with an output queue, defined as a
200 .Vt "struct ifqueue" ;
201 this structure is used to hold packets while the interface is in the
202 process of sending another.
203 .Ss The Vt ifnet Ss structure
204 The fields of
205 .Vt "struct ifnet"
206 are as follows:
207 .Bl -tag -width ".Va if_poll_slowq" -offset indent
208 .It Va if_softc
209 .Pq Vt "void *"
210 A pointer to the driver's private state block.
211 (Initialized by driver.)
212 .It Va if_link
213 .Pq Fn TAILQ_ENTRY ifnet
214 .Xr queue 3
215 macro glue.
216 .It Va if_xname
217 .Pq Vt "char *"
218 The name of the interface,
219 (e.g.,
220 .Dq Li fxp0
221 or
222 .Dq Li lo0) .
223 (Initialized by driver.)
224 .It Va if_dname
225 .Pq Vt "const char *"
226 The name of the driver.
227 (Initialized by driver.)
228 .It Va if_dunit
229 .Pq Vt int
230 A unique number assigned to each interface managed by a particular
231 driver.
232 Drivers may choose to set this to
233 .Dv IF_DUNIT_NONE
234 if a unit number is not associated with the device.
235 (Initialized by driver.)
236 .\" .It Va if_vlantrunks
237 .\" .Pq Vt "void *"
238 .\" ...
239 .It Va if_addrhead
240 .Pq Vt "struct ifaddrhead"
241 The head of the
242 .Xr queue 3
243 .Li TAILQ
244 containing the list of addresses assigned to this interface.
245 .It Va if_pcount
246 .Pq Vt int
247 A count of promiscuous listeners on this interface, used to
248 reference-count the
249 .Dv IFF_PROMISC
250 flag.
251 .It Va if_carp
252 .Pq Vt "struct carp_if *"
253 Per-interface data for
254 .Xr carp 4 .
255 .It Va if_bpf
256 .Pq Vt "struct bpf_if *"
257 Opaque per-interface data for the packet filter,
258 .Xr bpf 4 .
259 (Initialized by
260 .Fn bpfattach . )
261 .It Va if_index
262 .Pq Vt u_short
263 A unique number assigned to each interface in sequence as it is
264 attached.
265 This number can be used in a
266 .Vt "struct sockaddr_dl"
267 to refer to a particular interface by index
268 (see
269 .Xr link_addr 3 ) .
270 .It Va if_timer
271 .Pq Vt short
272 Number of seconds until the watchdog timer
273 .Fn if_watchdog
274 is called, or zero if the timer is disabled.
275 (Set by driver,
276 decremented by generic watchdog code.)
277 .It Va if_flags
278 .Pq Vt int
279 Flags describing operational parameters of this interface (see below).
280 (Manipulated by both driver and generic code.)
281 .It Va if_linkmib
282 .Pq Vt "void *"
283 A pointer to an interface-specific MIB structure exported by
284 .Xr ifmib 4 .
285 (Initialized by driver.)
286 .It Va if_linkmiblen
287 .Pq Vt size_t
288 The size of said structure.
289 (Initialized by driver.)
290 .It Va if_data
291 .Pq Vt "struct if_data"
292 More statistics and information; see
293 .Sx "The if_data structure" ,
294 below.
295 (Initialized by driver, manipulated by both driver and generic
296 code.)
297 .\" .It Va if_poll_cpuid
298 .\" .Pq Vt int
299 .\" ...
300 .It Va if_snd
301 .Pq Vt "struct ifaltq"
302 The output queue including
303 .Xr altq 4 .
304 (Manipulated by driver.)
305 .\" .It Va if_broadcastaddr
306 .\" .Pq Vt "const uint8_t"
307 .\" ...
308 .\" .It Va if_bridge
309 .\" .Pq Vt "void *"
310 .\" ...
311 .\" .It Va if_afdata
312 .\" .Pq Vt "void *"
313 .\" ...
314 .\" .It Va if_lladdr
315 .\" .Pq Vt "struct ifaddr"
316 .\" ...
317 .\" .It Va if_serializer
318 .\" .Pq Vt "struct lwkt_serialize"
319 .\" ...
320 .\" .It Va if_default_serializer
321 .\" .Pq Vt "struct lwkt_serialize"
322 .\" ...
323 .El
324 .Pp
325 There are in addition a number of function pointers which the driver
326 must initialize to complete its interface with the generic interface
327 layer:
328 .Bl -ohang -offset indent
329 .It Fn if_output
330 Output a packet on interface
331 .Fa ifp ,
332 or queue it on the output queue if the interface is already active.
333 .It Fn if_start
334 Start queued output on an interface.
335 This function is exposed in
336 order to provide for some interface classes to share a
337 .Fn if_output
338 among all drivers.
339 .Fn if_start
340 may only be called when the
341 .Dv IFF_OACTIVE
342 flag is not set.
343 (Thus,
344 .Dv IFF_OACTIVE
345 does not literally mean that output is active, but rather that the
346 device's internal output queue is full.)
347 .It Fn if_ioctl
348 Process interface-related
349 .Xr ioctl 2
350 requests
351 (defined in
352 .In sys/sockio.h ) .
353 Preliminary processing is done by the generic routine
354 .Fn ifioctl
355 to check for appropriate privileges, locate the interface being
356 manipulated, and perform certain generic operations like twiddling
357 flags and flushing queues.
358 See the description of
359 .Fn ifioctl
360 below for more information.
361 .It Fn if_watchdog
362 Routine called by the generic code when the watchdog timer,
363 .Va if_timer ,
364 expires.
365 Usually this will reset the interface.
366 .\".It Fn if_poll
367 .\" See the
368 .\" .Sx POLLING
369 .\" section, below.
370 .It Fn if_init
371 Initialize and bring up the hardware,
372 e.g., reset the chip and the watchdog timer and enable the receiver unit.
373 Should mark the interface running,
374 but not active
375 .Dv ( IFF_RUNNING , ~IFF_OACTIVE ) .
376 .It Fn if_resolvemulti
377 Check the requested multicast group membership,
378 .Fa addr ,
379 for validity, and if necessary compute a link-layer group which
380 corresponds to that address which is returned in
381 .Fa *retsa .
382 Returns zero on success, or an error code on failure.
383 .El
384 .Ss "Interface Flags"
385 Interface flags are used for a number of different purposes.
386 Some
387 flags simply indicate information about the type of interface and its
388 capabilities; others are dynamically manipulated to reflect the
389 current state of the interface.
390 Flags of the former kind are marked
391 .Aq S
392 in this table; the latter are marked
393 .Aq D .
394 .Pp
395 .Bl -tag -width ".Dv IFF_POINTOPOINT" -offset indent -compact
396 .It Dv IFF_UP
397 .Aq D
398 The interface has been configured up by the user-level code.
399 .It Dv IFF_BROADCAST
400 .Aq S*
401 The interface supports broadcast.
402 .It Dv IFF_DEBUG
403 .Aq D
404 Used to enable/disable driver debugging code.
405 .It Dv IFF_LOOPBACK
406 .Aq S
407 The interface is a loopback device.
408 .It Dv IFF_POINTOPOINT
409 .Aq S*
410 The interface is point-to-point;
411 .Dq broadcast
412 address is actually the address of the other end.
413 .It Dv IFF_RUNNING
414 .Aq D*
415 The interface has been configured and dynamic resources were
416 successfully allocated.
417 Probably only useful internal to the
418 interface.
419 .It Dv IFF_NOARP
420 .Aq D
421 Disable network address resolution on this interface.
422 .It Dv IFF_PROMISC
423 .Aq D*
424 This interface is in promiscuous mode.
425 .It Dv IFF_PPROMISC
426 .Aq D
427 This interface is in the permanently promiscuous mode (implies
428 .Dv IFF_PROMISC ) .
429 .It Dv IFF_ALLMULTI
430 .Aq D*
431 This interface is in all-multicasts mode (used by multicast routers).
432 .It Dv IFF_OACTIVE
433 .Aq D*
434 The interface's hardware output queue (if any) is full; output packets
435 are to be queued.
436 .It Dv IFF_SIMPLEX
437 .Aq S*
438 The interface cannot hear its own transmissions.
439 .It Dv IFF_LINK0
440 .It Dv IFF_LINK1
441 .It Dv IFF_LINK2
442 .Aq D
443 Control flags for the link layer.
444 (Currently abused to select among
445 multiple physical layers on some devices.)
446 .It Dv IFF_MULTICAST
447 .Aq S*
448 This interface supports multicast.
449 .It Dv IFF_POLLING
450 The interface is in polling mode.
451 .\" .It Dv IFF_MONITOR
452 .\" ...
453 .El
454 .Pp
455 The macro
456 .Dv IFF_CANTCHANGE
457 defines the bits which cannot be set by a user program using the
458 .Dv SIOCSIFFLAGS
459 command to
460 .Xr ioctl 2 ;
461 these are indicated by an asterisk in the listing above.
462 .Ss The Vt if_data Ss Structure
463 In
464 .Bx 4.4 ,
465 a subset of the interface information believed to be of interest to
466 management stations was segregated from the
467 .Vt ifnet
468 structure and moved into its own
469 .Vt if_data
470 structure to facilitate its use by user programs.
471 The following elements of the
472 .Vt if_data
473 structure are initialized by the interface and are not expected to change
474 significantly over the course of normal operation:
475 .Bl -tag -width ".Va ifi_lastchange" -offset indent
476 .It Va ifi_type
477 .Pq Vt u_char
478 The type of the interface, as defined in
479 .In net/if_types.h
480 and described below in the
481 .Sx "Interface Types"
482 section.
483 .It Va ifi_physical
484 .Pq Vt u_char
485 Intended to represent a selection of physical layers on devices which
486 support more than one; never implemented.
487 .It Va ifi_addrlen
488 .Pq Vt u_char
489 Length of a link-layer address on this device, or zero if there are
490 none.
491 Used to initialize the address length field in
492 .Vt sockaddr_dl
493 structures referring to this interface.
494 .It Va ifi_hdrlen
495 .Pq Vt u_char
496 Maximum length of any link-layer header which might be prepended by
497 the driver to a packet before transmission.
498 The generic code computes
499 the maximum over all interfaces and uses that value to influence the
500 placement of data in
501 .Vt mbuf Ns s
502 to attempt to ensure that there is always
503 sufficient space to prepend a link-layer header without allocating an
504 additional
505 .Vt mbuf .
506 .\" (See
507 .\" .Xr mbuf 9 . )
508 .\" .It Va ifi_recvquota
509 .\" .Pq Vt u_char
510 .\" Number of packets the interface is permitted to receive at one time
511 .\" when in polled mode.
512 .\" .It Va ifi_xmitquota
513 .\" .Pq Vt u_char
514 .\" Number of packets the interface is permitted to queue for transmission
515 .\" at one time when in polled mode.
516 .\" There is some controversy over
517 .\" whether such a restriction makes any sense at all.
518 .It Va ifi_mtu
519 .Pq Vt u_long
520 The maximum transmission unit of the medium, exclusive of any
521 link-layer overhead.
522 .It Va ifi_metric
523 .Pq Vt u_long
524 A dimensionless metric interpreted by a user-mode routing process.
525 .It Va ifi_link_state
526 .Pq Vt u_long
527 The link state of the interface, either
528 .Dv LINK_STATE_UNKNOWN ,
529 .Dv LINK_STATE_DOWN ,
530 or
531 .Dv LINK_STATE_UP .
532 .It Va ifi_baudrate
533 .Pq Vt u_long
534 The line rate of the interface, in bits per second.
535 .El
536 .Pp
537 The structure additionally contains generic statistics applicable to a
538 variety of different interface types (except as noted, all members are
539 of type
540 .Vt u_long ) :
541 .Bl -tag -width ".Va ifi_lastchange" -offset indent
542 .It Va ifi_ipackets
543 Number of packets received.
544 .It Va ifi_ierrors
545 Number of receive errors detected (e.g., FCS errors, DMA overruns,
546 etc.).
547 More detailed breakdowns can often be had by way of a
548 link-specific MIB.
549 .It Va ifi_opackets
550 Number of packets transmitted.
551 .It Va ifi_oerrors
552 Number of output errors detected (e.g., late collisions, DMA overruns,
553 etc.).
554 More detailed breakdowns can often be had by way of a
555 link-specific MIB.
556 .It Va ifi_collisions
557 Total number of collisions detected on output for CSMA interfaces.
558 (This member is sometimes [ab]used by other types of interfaces for
559 other output error counts.)
560 .It Va ifi_ibytes
561 Total traffic received, in bytes.
562 .It Va ifi_obytes
563 Total traffic transmitted, in bytes.
564 .It Va ifi_imcasts
565 Number of packets received which were sent by link-layer multicast.
566 .It Va ifi_omcasts
567 Number of packets sent by link-layer multicast.
568 .It Va ifi_iqdrops
569 Number of packets dropped on input.
570 Rarely implemented.
571 .It Va ifi_noproto
572 Number of packets received for unknown network-layer protocol.
573 .It Va ifi_lastchange
574 .Pq Vt "struct timeval"
575 The time of the last administrative change to the interface (as required
576 for
577 .Tn SNMP ) .
578 .El
579 .Ss Interface Types
580 The header file
581 .In net/if_types.h
582 defines symbolic constants for a number of different types of
583 interfaces.
584 The most common are:
585 .Pp
586 .Bl -tag -offset indent -width ".Dv IFT_PROPVIRTUAL" -compact
587 .It Dv IFT_OTHER
588 none of the following
589 .It Dv IFT_ETHER
590 Ethernet
591 .It Dv IFT_ISO88023
592 ISO 8802-3 CSMA/CD
593 .It Dv IFT_ISO88024
594 ISO 8802-4 Token Bus
595 .It Dv IFT_ISO88025
596 ISO 8802-5 Token Ring
597 .It Dv IFT_ISO88026
598 ISO 8802-6 DQDB MAN
599 .It Dv IFT_FDDI
600 FDDI
601 .It Dv IFT_PPP
602 Internet Point-to-Point Protocol
603 .Pq Xr ppp 8
604 .It Dv IFT_LOOP
605 The loopback
606 .Pq Xr lo 4
607 interface
608 .It Dv IFT_SLIP
609 Serial Line IP
610 .It Dv IFT_PARA
611 Parallel-port IP
612 .Pq Dq Tn PLIP
613 .It Dv IFT_ATM
614 Asynchronous Transfer Mode
615 .El
616 .Ss The Vt ifaddr Ss Structure
617 Every interface is associated with a list
618 (or, rather, a
619 .Li TAILQ )
620 of addresses, rooted at the interface structure's
621 .Va if_addrlist
622 member.
623 The first element in this list is always an
624 .Dv AF_LINK
625 address representing the interface itself; multi-access network
626 drivers should complete this structure by filling in their link-layer
627 addresses after calling
628 .Fn if_attach .
629 Other members of the structure represent network-layer addresses which
630 have been configured by means of the
631 .Dv SIOCAIFADDR
632 command to
633 .Xr ioctl 2 ,
634 called on a socket of the appropriate protocol family.
635 The elements of this list consist of
636 .Vt ifaddr
637 structures.
638 Most protocols will declare their own protocol-specific
639 interface address structures, but all begin with a
640 .Vt "struct ifaddr"
641 which provides the most-commonly-needed functionality across all
642 protocols.
643 Interface addresses are reference-counted.
644 .Pp
645 The members of
646 .Vt "struct ifaddr"
647 are as follows:
648 .Bl -tag -width ".Va ifa_rtrequest" -offset indent
649 .It Va ifa_addr
650 .Pq Vt "struct sockaddr *"
651 The local address of the interface.
652 .It Va ifa_dstaddr
653 .Pq Vt "struct sockaddr *"
654 The remote address of point-to-point interfaces, and the broadcast
655 address of broadcast interfaces.
656 .Va ( ifa_broadaddr
657 is a macro for
658 .Va ifa_dstaddr . )
659 .It Va ifa_netmask
660 .Pq Vt "struct sockaddr *"
661 The network mask for multi-access interfaces, and the confusion
662 generator for point-to-point interfaces.
663 .\" .It Va if_data
664 .\" .Pq Vt "struct if_data"
665 .\" ...
666 .It Va ifa_ifp
667 .Pq Vt "struct ifnet *"
668 A link back to the interface structure.
669 .It Va ifa_containers
670 .Pq Vt "struct ifaddr_container *"
671 A pointer to an array of
672 .Vt ifaddr_container
673 structures which hold per-CPU data.
674 .It Va ifa_rtrequest
675 See below.
676 .It Va ifa_flags
677 .Pq Vt u_short
678 Some of the flags which would be used for a route representing this
679 address in the route table.
680 .\" .It Va ifa_cpumask
681 .\" .Pq Vt cpumask_t
682 .\" ...
683 .It Va ifa_metric
684 .Pq Vt int
685 A metric associated with this interface address, for the use of some
686 external routing protocol.
687 .El
688 .Pp
689 References to
690 .Vt ifaddr
691 structures are gained manually, by incrementing the
692 .Va ifa_refcnt
693 member of the according
694 .Va ifa_containers
695 structure (such as by calling the
696 .Fn IFAREF
697 macro).
698 References are released by calling the
699 .Fn IFAFREE
700 macro.
701 .Pp
702 .Fn ifa_rtrequest
703 is a pointer to a function which receives callouts from the routing
704 code
705 .Pq Fn rtrequest
706 to perform link-layer-specific actions upon requests to add, resolve,
707 or delete routes.
708 The
709 .Fa cmd
710 argument indicates the request in question:
711 .Dv RTM_ADD , RTM_RESOLVE ,
712 or
713 .Dv RTM_DELETE .
714 The
715 .Fa rt
716 argument is the route in question; the
717 .Fa dst
718 argument is the specific destination being manipulated
719 for
720 .Dv RTM_RESOLVE ,
721 or a null pointer otherwise.
722 .Sh FUNCTIONS
723 The functions provided by the generic interface code can be divided
724 into two groups: those which manipulate interfaces, and those which
725 manipulate interface addresses.
726 In addition to these functions, there
727 may also be link-layer support routines which are used by a number of
728 drivers implementing a specific link layer over different hardware;
729 see the documentation for that link layer for more details.
730 .Ss The Vt ifmultiaddr Ss Structure
731 Every multicast-capable interface is associated with a list of
732 multicast group memberships, which indicate at a low level which
733 link-layer multicast addresses (if any) should be accepted, and at a
734 high level, in which network-layer multicast groups a user process has
735 expressed interest.
736 .Pp
737 The elements of the structure are as follows:
738 .Bl -tag -width ".Va ifma_refcount" -offset indent
739 .It Va ifma_link
740 .Pq Fn LIST_ENTRY ifmultiaddr
741 .Xr queue 3
742 macro glue.
743 .It Va ifma_addr
744 .Pq Vt "struct sockaddr *"
745 A pointer to the address which this record represents.
746 The
747 memberships for various address families are stored in arbitrary
748 order.
749 .It Va ifma_lladdr
750 .Pq Vt "struct sockaddr *"
751 A pointer to the link-layer multicast address, if any, to which the
752 network-layer multicast address in
753 .Va ifma_addr
754 is mapped, else a null pointer.
755 If this element is non-nil, this
756 membership also holds an invisible reference to another membership for
757 that link-layer address.
758 .It Va ifma_refcount
759 .Pq Vt u_int
760 A reference count of requests for this particular membership.
761 .El
762 .Ss Interface Manipulation Functions
763 .Bl -ohang -offset indent
764 .It Fn if_attach
765 Link the specified interface
766 .Fa ifp
767 into the list of network interfaces.
768 Also initialize the list of
769 addresses on that interface, and create a link-layer
770 .Vt ifaddr
771 structure to be the first element in that list.
772 (A pointer to
773 this address structure is saved in the global array
774 .Va ifnet_addrs . )
775 .It Fn if_down
776 Mark the interface
777 .Fa ifp
778 as down (i.e.,
779 .Dv IFF_UP
780 is not set),
781 flush its output queue, notify protocols of the transition,
782 and generate a message from the
783 .Xr route 4
784 routing socket.
785 .It Fn if_up
786 Mark the interface
787 .Fa ifp
788 as up, notify protocols of the transition,
789 and generate a message from the
790 .Xr route 4
791 routing socket.
792 .It Fn ifpromisc
793 Add or remove a promiscuous reference to
794 .Fa ifp .
795 If
796 .Fa pswitch
797 is true, add a reference;
798 if it is false, remove a reference.
799 On reference count transitions
800 from zero to one and one to zero, set the
801 .Dv IFF_PROMISC
802 flag appropriately and call
803 .Fn if_ioctl
804 to set up the interface in the desired mode.
805 .It Fn if_allmulti
806 As
807 .Fn ifpromisc ,
808 but for the all-multicasts
809 .Pq Dv IFF_ALLMULTI
810 flag instead of the promiscuous flag.
811 .It Fn ifunit
812 Return an
813 .Vt ifnet
814 pointer for the interface named
815 .Fa name .
816 .It Fn ifioctl
817 Process the ioctl request
818 .Fa cmd ,
819 issued on socket
820 .Fa so
821 by process
822 .Fa p ,
823 with data parameter
824 .Fa data .
825 This is the main routine for handling all interface configuration
826 requests from user mode.
827 It is ordinarily only called from the socket-layer
828 .Xr ioctl 2
829 handler, and only for commands with class
830 .Sq Li i .
831 Any unrecognized commands will be passed down to socket
832 .Fa so Ns 's
833 protocol for
834 further interpretation.
835 The following commands are handled by
836 .Fn ifioctl :
837 .Pp
838 .Bl -tag -width ".Dv OSIOCGIFNETMASK" -offset indent -compact
839 .It Dv SIOCGIFCONF
840 .It Dv OSIOCGIFCONF
841 Get interface configuration.
842 (No call-down to driver.)
843 .Pp
844 .It Dv SIOCGIFFLAGS
845 .It Dv SIOCGIFMETRIC
846 .It Dv SIOCGIFMTU
847 .It Dv SIOCGIFPHYS
848 Get interface flags, metric, MTU, medium selection.
849 (No call-down to driver.)
850 .Pp
851 .It Dv SIOCSIFFLAGS
852 Change interface flags.
853 Caller must have appropriate privilege.
854 If a change to the
855 .Dv IFF_UP
856 flag is requested,
857 .Fn if_up
858 or
859 .Fn if_down
860 is called as appropriate.
861 Flags listed in
862 .Dv IFF_CANTCHANGE
863 are masked off, and the driver
864 .Fn if_ioctl
865 routine is called to perform any setup
866 requested.
867 .Pp
868 .It Dv SIOCSIFMETRIC
869 .It Dv SIOCSIFPHYS
870 Change interface metric or medium.
871 Caller must have appropriate privilege.
872 .Pp
873 .It Dv SIOCSIFMTU
874 Change interface MTU.
875 Caller must have appropriate privilege.
876 MTU
877 values less than 72 or greater than 65535 are considered invalid.
878 The driver
879 .Fn if_ioctl
880 routine is called to implement the change; it is responsible for any
881 additional sanity checking and for actually modifying the MTU in the
882 interface structure.
883 .Pp
884 .It Dv SIOCADDMULTI
885 .It Dv SIOCDELMULTI
886 Add or delete permanent multicast group memberships on the interface.
887 Caller must have appropriate privilege.
888 The
889 .Fn if_addmulti
890 or
891 .Fn if_delmulti
892 function is called to perform the operation; qq.v.
893 .Pp
894 .It Dv SIOCSIFDSTADDR
895 .It Dv SIOCSIFADDR
896 .It Dv SIOCSIFBRDADDR
897 .It Dv SIOCSIFNETMASK
898 The socket's protocol control routine is called to implement the
899 requested action.
900 .Pp
901 .It Dv OSIOCGIFADDR
902 .It Dv OSIOCGIFDSTADDR
903 .It Dv OSIOCGIFBRDADDR
904 .It Dv OSIOCGIFNETMASK
905 The socket's protocol control routine is called to implement the
906 requested action.
907 On return,
908 .Vt sockaddr
909 structures are converted into old-style (no
910 .Va sa_len
911 member).
912 .El
913 .El
914 .Pp
915 .Fn if_down ,
916 .Fn ifioctl ,
917 .Fn ifpromisc ,
918 and
919 .Fn if_up
920 must be called inside a critical section.
921 .Ss "Interface Address Functions"
922 Several functions exist to look up an interface address structure
923 given an address.
924 .Fn ifa_ifwithaddr
925 returns an interface address with either a local address or a
926 broadcast address precisely matching the parameter
927 .Fa addr .
928 .Fn ifa_ifwithdstaddr
929 returns an interface address for a point-to-point interface whose
930 remote
931 .Pq Dq destination
932 address is
933 .Fa addr .
934 .Pp
935 .Fn ifa_ifwithnet
936 returns the most specific interface address which matches the
937 specified address,
938 .Fa addr ,
939 subject to its configured netmask, or a point-to-point interface
940 address whose remote address is
941 .Fa addr
942 if one is found.
943 .Pp
944 .Fn ifaof_ifpforaddr
945 returns the most specific address configured on interface
946 .Fa ifp
947 which matches address
948 .Fa addr ,
949 subject to its configured netmask.
950 If the interface is
951 point-to-point, only an interface address whose remote address is
952 precisely
953 .Fa addr
954 will be returned.
955 .Pp
956 All of these functions return a null pointer if no such address can be
957 found.
958 .Ss "Interface Multicast Address Functions"
959 The
960 .Fn if_addmulti ,
961 .Fn if_delmulti ,
962 and
963 .Fn ifmaof_ifpforaddr
964 functions provide support for requesting and relinquishing multicast
965 group memberships, and for querying an interface's membership list,
966 respectively.
967 The
968 .Fn if_addmulti
969 function takes a pointer to an interface,
970 .Fa ifp ,
971 and a generic address,
972 .Fa sa .
973 It also takes a pointer to a
974 .Vt "struct ifmultiaddr *"
975 which is filled in on successful return with the address of the
976 group membership control block.
977 The
978 .Fn if_addmulti
979 function performs the following four-step process:
980 .Bl -enum -offset indent
981 .It
982 Call the interface's
983 .Fn if_resolvemulti
984 entry point to determine the link-layer address, if any, corresponding
985 to this membership request, and also to give the link layer an
986 opportunity to veto this membership request should it so desire.
987 .It
988 Check the interface's group membership list for a pre-existing
989 membership for this group.
990 If one is not found, allocate a new one;
991 if one is, increment its reference count.
992 .It
993 If the
994 .Fn if_resolvemulti
995 routine returned a link-layer address corresponding to the group,
996 repeat the previous step for that address as well.
997 .It
998 If the interface's multicast address filter needs to be changed
999 because a new membership was added, call the interface's
1000 .Fn if_ioctl
1001 routine
1002 (with a
1003 .Fa cmd
1004 argument of
1005 .Dv SIOCADDMULTI )
1006 to request that it do so.
1007 .El
1008 .Pp
1009 The
1010 .Fn if_delmulti
1011 function, given an interface
1012 .Fa ifp
1013 and an address,
1014 .Fa sa ,
1015 reverses this process.
1016 Both functions return zero on success, or a
1017 standard error number on failure.
1018 .Pp
1019 The
1020 .Fn ifmaof_ifpforaddr
1021 function examines the membership list of interface
1022 .Fa ifp
1023 for an address matching
1024 .Fa addr ,
1025 and returns a pointer to that
1026 .Vt "struct ifmultiaddr"
1027 if one is found, else it returns a null pointer.
1028 .\" .Sh POLLING
1029 .\" XXX write me!
1030 .Sh SEE ALSO
1031 .Xr ioctl 2 ,
1032 .Xr link_addr 3 ,
1033 .Xr queue 3 ,
1034 .Xr sysctl 3 ,
1035 .Xr bpf 4 ,
1036 .Xr ifmib 4 ,
1037 .Xr lo 4 ,
1038 .Xr netintro 4 ,
1039 .Xr config 8 ,
1040 .Xr ppp 8 ,
1041 .\" .Xr mbuf 9 ,
1042 .Xr rtentry 9
1043 .Rs
1044 .%A Gary R. Wright
1045 .%A W. Richard Stevens
1046 .%B TCP/IP Illustrated
1047 .%V Vol. 2
1048 .%O Addison-Wesley, ISBN 0-201-63354-X
1049 .Re
1050 .Sh AUTHORS
1051 This manual page was written by
1052 .An Garrett A. Wollman .