if.h revision 1.141 1 /* $NetBSD: if.h,v 1.141 2008/11/07 00:20:13 dyoung Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by William Studenmund and Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1989, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)if.h 8.3 (Berkeley) 2/9/95
61 */
62
63 #ifndef _NET_IF_H_
64 #define _NET_IF_H_
65
66 #include <sys/featuretest.h>
67
68 /*
69 * Length of interface external name, including terminating '\0'.
70 * Note: this is the same size as a generic device's external name.
71 */
72 #define IF_NAMESIZE 16
73
74 #if defined(_NETBSD_SOURCE)
75
76 #include <sys/mutex.h>
77 #include <sys/condvar.h>
78 #include <sys/socket.h>
79 #include <sys/queue.h>
80 #include <net/dlt.h>
81 #include <net/pfil.h>
82
83 /*
84 * Always include ALTQ glue here -- we use the ALTQ interface queue
85 * structure even when ALTQ is not configured into the kernel so that
86 * the size of struct ifnet does not changed based on the option. The
87 * ALTQ queue structure is API-compatible with the legacy ifqueue.
88 */
89 #include <altq/if_altq.h>
90
91 /*
92 * Structures defining a network interface, providing a packet
93 * transport mechanism (ala level 0 of the PUP protocols).
94 *
95 * Each interface accepts output datagrams of a specified maximum
96 * length, and provides higher level routines with input datagrams
97 * received from its medium.
98 *
99 * Output occurs when the routine if_output is called, with four parameters:
100 * (*ifp->if_output)(ifp, m, dst, rt)
101 * Here m is the mbuf chain to be sent and dst is the destination address.
102 * The output routine encapsulates the supplied datagram if necessary,
103 * and then transmits it on its medium.
104 *
105 * On input, each interface unwraps the data received by it, and either
106 * places it on the input queue of a internetwork datagram routine
107 * and posts the associated software interrupt, or passes the datagram to a raw
108 * packet input routine.
109 *
110 * Routines exist for locating interfaces by their addresses
111 * or for locating a interface on a certain network, as well as more general
112 * routing and gateway routines maintaining information used to locate
113 * interfaces. These routines live in the files if.c and route.c
114 */
115 /* XXX fast fix for SNMP, going away soon */
116 #include <sys/time.h>
117
118 #if defined(_KERNEL_OPT)
119 #include "opt_compat_netbsd.h"
120 #endif
121
122 struct mbuf;
123 struct proc;
124 struct rtentry;
125 struct socket;
126 struct ether_header;
127 struct ifaddr;
128 struct ifnet;
129 struct rt_addrinfo;
130
131 #define IFNAMSIZ IF_NAMESIZE
132
133 /*
134 * Structure describing a `cloning' interface.
135 */
136 struct if_clone {
137 LIST_ENTRY(if_clone) ifc_list; /* on list of cloners */
138 const char *ifc_name; /* name of device, e.g. `gif' */
139 size_t ifc_namelen; /* length of name */
140
141 int (*ifc_create)(struct if_clone *, int);
142 int (*ifc_destroy)(struct ifnet *);
143 };
144
145 #define IF_CLONE_INITIALIZER(name, create, destroy) \
146 { { NULL, NULL }, name, sizeof(name) - 1, create, destroy }
147
148 /*
149 * Structure used to query names of interface cloners.
150 */
151 struct if_clonereq {
152 int ifcr_total; /* total cloners (out) */
153 int ifcr_count; /* room for this many in user buffer */
154 char *ifcr_buffer; /* buffer for cloner names */
155 };
156
157 /*
158 * Structure defining statistics and other data kept regarding a network
159 * interface.
160 */
161 struct if_data {
162 /* generic interface information */
163 u_char ifi_type; /* ethernet, tokenring, etc. */
164 u_char ifi_addrlen; /* media address length */
165 u_char ifi_hdrlen; /* media header length */
166 int ifi_link_state; /* current link state */
167 u_quad_t ifi_mtu; /* maximum transmission unit */
168 u_quad_t ifi_metric; /* routing metric (external only) */
169 u_quad_t ifi_baudrate; /* linespeed */
170 /* volatile statistics */
171 u_quad_t ifi_ipackets; /* packets received on interface */
172 u_quad_t ifi_ierrors; /* input errors on interface */
173 u_quad_t ifi_opackets; /* packets sent on interface */
174 u_quad_t ifi_oerrors; /* output errors on interface */
175 u_quad_t ifi_collisions; /* collisions on csma interfaces */
176 u_quad_t ifi_ibytes; /* total number of octets received */
177 u_quad_t ifi_obytes; /* total number of octets sent */
178 u_quad_t ifi_imcasts; /* packets received via multicast */
179 u_quad_t ifi_omcasts; /* packets sent via multicast */
180 u_quad_t ifi_iqdrops; /* dropped on input, this interface */
181 u_quad_t ifi_noproto; /* destined for unsupported protocol */
182 struct timeval ifi_lastchange; /* last operational state change */
183 };
184
185 /*
186 * Values for if_link_state.
187 */
188 #define LINK_STATE_UNKNOWN 0 /* link invalid/unknown */
189 #define LINK_STATE_DOWN 1 /* link is down */
190 #define LINK_STATE_UP 2 /* link is up */
191
192 #if defined(_KERNEL) && defined(COMPAT_14)
193 /* Pre-1.5 if_data struct */
194 struct if_data14 {
195 /* generic interface information */
196 u_char ifi_type; /* ethernet, tokenring, etc. */
197 u_char ifi_addrlen; /* media address length */
198 u_char ifi_hdrlen; /* media header length */
199 u_long ifi_mtu; /* maximum transmission unit */
200 u_long ifi_metric; /* routing metric (external only) */
201 u_long ifi_baudrate; /* linespeed */
202 /* volatile statistics */
203 u_long ifi_ipackets; /* packets received on interface */
204 u_long ifi_ierrors; /* input errors on interface */
205 u_long ifi_opackets; /* packets sent on interface */
206 u_long ifi_oerrors; /* output errors on interface */
207 u_long ifi_collisions; /* collisions on csma interfaces */
208 u_long ifi_ibytes; /* total number of octets received */
209 u_long ifi_obytes; /* total number of octets sent */
210 u_long ifi_imcasts; /* packets received via multicast */
211 u_long ifi_omcasts; /* packets sent via multicast */
212 u_long ifi_iqdrops; /* dropped on input, this interface */
213 u_long ifi_noproto; /* destined for unsupported protocol */
214 struct timeval ifi_lastchange; /* last operational state change */
215 };
216 #endif /* _KERNEL && COMPAT_14 */
217
218 /*
219 * Structure defining a queue for a network interface.
220 */
221 struct ifqueue {
222 struct mbuf *ifq_head;
223 struct mbuf *ifq_tail;
224 int ifq_len;
225 int ifq_maxlen;
226 int ifq_drops;
227 };
228
229 /*
230 * Structure defining a queue for a network interface.
231 *
232 * (Would like to call this struct ``if'', but C isn't PL/1.)
233 */
234 TAILQ_HEAD(ifnet_head, ifnet); /* the actual queue head */
235
236 struct ifnet { /* and the entries */
237 void *if_softc; /* lower-level data for this if */
238 TAILQ_ENTRY(ifnet) if_list; /* all struct ifnets are chained */
239 TAILQ_HEAD(, ifaddr) if_addrlist; /* linked list of addresses per if */
240 char if_xname[IFNAMSIZ]; /* external name (name + unit) */
241 int if_pcount; /* number of promiscuous listeners */
242 void * if_bpf; /* packet filter structure */
243 u_short if_index; /* numeric abbreviation for this if */
244 short if_timer; /* time 'til if_watchdog called */
245 short if_flags; /* up/down, broadcast, etc. */
246 short if__pad1; /* be nice to m68k ports */
247 struct if_data if_data; /* statistics and other data about if */
248 /*
249 * Procedure handles. If you add more of these, don't forget the
250 * corresponding NULL stub in if.c.
251 */
252 int (*if_output) /* output routine (enqueue) */
253 (struct ifnet *, struct mbuf *, const struct sockaddr *,
254 struct rtentry *);
255 void (*if_input) /* input routine (from h/w driver) */
256 (struct ifnet *, struct mbuf *);
257 void (*if_start) /* initiate output routine */
258 (struct ifnet *);
259 int (*if_ioctl) /* ioctl routine */
260 (struct ifnet *, u_long, void *);
261 int (*if_init) /* init routine */
262 (struct ifnet *);
263 void (*if_stop) /* stop routine */
264 (struct ifnet *, int);
265 void (*if_watchdog) /* timer routine */
266 (struct ifnet *);
267 void (*if_drain) /* routine to release resources */
268 (struct ifnet *);
269 struct ifaltq if_snd; /* output queue (includes altq) */
270 struct ifaddr *if_dl; /* identity of this interface. */
271 const struct sockaddr_dl *if_sadl; /* pointer to sockaddr_dl
272 * of if_dl
273 */
274 /* if_hwdl: h/w identity
275 *
276 * May be NULL. If not NULL, it is the address assigned
277 * to the interface by the manufacturer, so it very likely
278 * to be unique. It MUST NOT be deleted. It is highly
279 * suitable for deriving the EUI64 for the interface.
280 */
281 struct ifaddr *if_hwdl;
282 const uint8_t *if_broadcastaddr;/* linklevel broadcast bytestring */
283 void *if_bridge; /* bridge glue */
284 int if_dlt; /* data link type (<net/dlt.h>) */
285 struct pfil_head if_pfil; /* filtering point */
286 uint64_t if_capabilities; /* interface capabilities */
287 uint64_t if_capenable; /* capabilities enabled */
288 union {
289 void * carp_s; /* carp structure (used by !carp ifs) */
290 struct ifnet *carp_d;/* ptr to carpdev (used by carp ifs) */
291 } if_carp_ptr;
292 #define if_carp if_carp_ptr.carp_s
293 #define if_carpdev if_carp_ptr.carp_d
294 /*
295 * These are pre-computed based on an interfaces enabled
296 * capabilities, for speed elsewhere.
297 */
298 int if_csum_flags_tx; /* M_CSUM_* flags for Tx */
299 int if_csum_flags_rx; /* M_CSUM_* flags for Rx */
300
301 void *if_afdata[AF_MAX];
302 struct mowner *if_mowner; /* who owns mbufs for this interface */
303
304 void *if_agrprivate; /* used only when #if NAGR > 0 */
305
306 /*
307 * pf specific data, used only when #if NPF > 0.
308 */
309 void *if_pf_kif; /* pf interface abstraction */
310 void *if_pf_groups; /* pf interface groups */
311 };
312 #define if_mtu if_data.ifi_mtu
313 #define if_type if_data.ifi_type
314 #define if_addrlen if_data.ifi_addrlen
315 #define if_hdrlen if_data.ifi_hdrlen
316 #define if_metric if_data.ifi_metric
317 #define if_link_state if_data.ifi_link_state
318 #define if_baudrate if_data.ifi_baudrate
319 #define if_ipackets if_data.ifi_ipackets
320 #define if_ierrors if_data.ifi_ierrors
321 #define if_opackets if_data.ifi_opackets
322 #define if_oerrors if_data.ifi_oerrors
323 #define if_collisions if_data.ifi_collisions
324 #define if_ibytes if_data.ifi_ibytes
325 #define if_obytes if_data.ifi_obytes
326 #define if_imcasts if_data.ifi_imcasts
327 #define if_omcasts if_data.ifi_omcasts
328 #define if_iqdrops if_data.ifi_iqdrops
329 #define if_noproto if_data.ifi_noproto
330 #define if_lastchange if_data.ifi_lastchange
331
332 #define IFF_UP 0x0001 /* interface is up */
333 #define IFF_BROADCAST 0x0002 /* broadcast address valid */
334 #define IFF_DEBUG 0x0004 /* turn on debugging */
335 #define IFF_LOOPBACK 0x0008 /* is a loopback net */
336 #define IFF_POINTOPOINT 0x0010 /* interface is point-to-point link */
337 #define IFF_NOTRAILERS 0x0020 /* avoid use of trailers */
338 #define IFF_RUNNING 0x0040 /* resources allocated */
339 #define IFF_NOARP 0x0080 /* no address resolution protocol */
340 #define IFF_PROMISC 0x0100 /* receive all packets */
341 #define IFF_ALLMULTI 0x0200 /* receive all multicast packets */
342 #define IFF_OACTIVE 0x0400 /* transmission in progress */
343 #define IFF_SIMPLEX 0x0800 /* can't hear own transmissions */
344 #define IFF_LINK0 0x1000 /* per link layer defined bit */
345 #define IFF_LINK1 0x2000 /* per link layer defined bit */
346 #define IFF_LINK2 0x4000 /* per link layer defined bit */
347 #define IFF_MULTICAST 0x8000 /* supports multicast */
348
349 #define IFFBITS \
350 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS" \
351 "\7RUNNING\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX" \
352 "\15LINK0\16LINK1\17LINK2\20MULTICAST"
353
354 /* flags set internally only: */
355 #define IFF_CANTCHANGE \
356 (IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\
357 IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI|IFF_PROMISC)
358
359 /*
360 * Some convenience macros used for setting ifi_baudrate.
361 * XXX 1000 vs. 1024? --thorpej (at) NetBSD.org
362 */
363 #define IF_Kbps(x) ((x) * 1000) /* kilobits/sec. */
364 #define IF_Mbps(x) (IF_Kbps((x) * 1000)) /* megabits/sec. */
365 #define IF_Gbps(x) (IF_Mbps((x) * 1000)) /* gigabits/sec. */
366
367 /* Capabilities that interfaces can advertise. */
368 #define IFCAP_TSOv4 0x00080 /* can do TCPv4 segmentation offload */
369 #define IFCAP_CSUM_IPv4_Rx 0x00100 /* can do IPv4 header checksums (Rx) */
370 #define IFCAP_CSUM_IPv4_Tx 0x00200 /* can do IPv4 header checksums (Tx) */
371 #define IFCAP_CSUM_TCPv4_Rx 0x00400 /* can do IPv4/TCP checksums (Rx) */
372 #define IFCAP_CSUM_TCPv4_Tx 0x00800 /* can do IPv4/TCP checksums (Tx) */
373 #define IFCAP_CSUM_UDPv4_Rx 0x01000 /* can do IPv4/UDP checksums (Rx) */
374 #define IFCAP_CSUM_UDPv4_Tx 0x02000 /* can do IPv4/UDP checksums (Tx) */
375 #define IFCAP_CSUM_TCPv6_Rx 0x04000 /* can do IPv6/TCP checksums (Rx) */
376 #define IFCAP_CSUM_TCPv6_Tx 0x08000 /* can do IPv6/TCP checksums (Tx) */
377 #define IFCAP_CSUM_UDPv6_Rx 0x10000 /* can do IPv6/UDP checksums (Rx) */
378 #define IFCAP_CSUM_UDPv6_Tx 0x20000 /* can do IPv6/UDP checksums (Tx) */
379 #define IFCAP_TSOv6 0x40000 /* can do TCPv6 segmentation offload */
380
381 #define IFCAPBITS \
382 "\020" \
383 "\10TSO4" \
384 "\11IP4CSUM_Rx" \
385 "\12IP4CSUM_Tx" \
386 "\13TCP4CSUM_Rx" \
387 "\14TCP4CSUM_Tx" \
388 "\15UDP4CSUM_Rx" \
389 "\16UDP4CSUM_Tx" \
390 "\17TCP6CSUM_Rx" \
391 "\20TCP6CSUM_Tx" \
392 "\21UDP6CSUM_Rx" \
393 "\22UDP6CSUM_Tx" \
394 "\23TSO6"
395
396 /*
397 * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
398 * input routines have queues of messages stored on ifqueue structures
399 * (defined above). Entries are added to and deleted from these structures
400 * by these macros, which should be called with ipl raised to splnet().
401 */
402 #define IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen)
403 #define IF_DROP(ifq) ((ifq)->ifq_drops++)
404 #define IF_ENQUEUE(ifq, m) do { \
405 (m)->m_nextpkt = 0; \
406 if ((ifq)->ifq_tail == 0) \
407 (ifq)->ifq_head = m; \
408 else \
409 (ifq)->ifq_tail->m_nextpkt = m; \
410 (ifq)->ifq_tail = m; \
411 (ifq)->ifq_len++; \
412 } while (/*CONSTCOND*/0)
413 #define IF_PREPEND(ifq, m) do { \
414 (m)->m_nextpkt = (ifq)->ifq_head; \
415 if ((ifq)->ifq_tail == 0) \
416 (ifq)->ifq_tail = (m); \
417 (ifq)->ifq_head = (m); \
418 (ifq)->ifq_len++; \
419 } while (/*CONSTCOND*/0)
420 #define IF_DEQUEUE(ifq, m) do { \
421 (m) = (ifq)->ifq_head; \
422 if (m) { \
423 if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \
424 (ifq)->ifq_tail = 0; \
425 (m)->m_nextpkt = 0; \
426 (ifq)->ifq_len--; \
427 } \
428 } while (/*CONSTCOND*/0)
429 #define IF_POLL(ifq, m) ((m) = (ifq)->ifq_head)
430 #define IF_PURGE(ifq) \
431 do { \
432 struct mbuf *__m0; \
433 \
434 for (;;) { \
435 IF_DEQUEUE((ifq), __m0); \
436 if (__m0 == NULL) \
437 break; \
438 else \
439 m_freem(__m0); \
440 } \
441 } while (/*CONSTCOND*/ 0)
442 #define IF_IS_EMPTY(ifq) ((ifq)->ifq_len == 0)
443
444 #ifndef IFQ_MAXLEN
445 #define IFQ_MAXLEN 256
446 #endif
447 #define IFNET_SLOWHZ 1 /* granularity is 1 second */
448
449 /*
450 * Structure defining statistics and other data kept regarding an address
451 * on a network interface.
452 */
453 struct ifaddr_data {
454 int64_t ifad_inbytes;
455 int64_t ifad_outbytes;
456 };
457
458 /*
459 * The ifaddr structure contains information about one address
460 * of an interface. They are maintained by the different address families,
461 * are allocated and attached when an address is set, and are linked
462 * together so all addresses for an interface can be located.
463 */
464 struct ifaddr {
465 struct sockaddr *ifa_addr; /* address of interface */
466 struct sockaddr *ifa_dstaddr; /* other end of p-to-p link */
467 #define ifa_broadaddr ifa_dstaddr /* broadcast address interface */
468 struct sockaddr *ifa_netmask; /* used to determine subnet */
469 struct ifnet *ifa_ifp; /* back-pointer to interface */
470 TAILQ_ENTRY(ifaddr) ifa_list; /* list of addresses for interface */
471 struct ifaddr_data ifa_data; /* statistics on the address */
472 void (*ifa_rtrequest) /* check or clean routes (+ or -)'d */
473 (int, struct rtentry *, const struct rt_addrinfo *);
474 u_int ifa_flags; /* mostly rt_flags for cloning */
475 int ifa_refcnt; /* count of references */
476 int ifa_metric; /* cost of going out this interface */
477 struct ifaddr *(*ifa_getifa)(struct ifaddr *,
478 const struct sockaddr *);
479 uint32_t *ifa_seqno;
480 int16_t ifa_preference; /* preference level for this address */
481 };
482 #define IFA_ROUTE RTF_UP /* (0x01) route installed */
483
484 /*
485 * Message format for use in obtaining information about interfaces
486 * from sysctl and the routing socket.
487 */
488 struct if_msghdr {
489 u_short ifm_msglen; /* to skip over non-understood messages */
490 u_char ifm_version; /* future binary compatibility */
491 u_char ifm_type; /* message type */
492 int ifm_addrs; /* like rtm_addrs */
493 int ifm_flags; /* value of if_flags */
494 u_short ifm_index; /* index for associated ifp */
495 struct if_data ifm_data;/* statistics and other data about if */
496 };
497
498 #if defined(_KERNEL) && defined(COMPAT_14)
499 /* pre-1.5 if_msghdr (ifm_data changed) */
500 struct if_msghdr14 {
501 u_short ifm_msglen; /* to skip over non-understood messages */
502 u_char ifm_version; /* future binary compatibility */
503 u_char ifm_type; /* message type */
504 int ifm_addrs; /* like rtm_addrs */
505 int ifm_flags; /* value of if_flags */
506 u_short ifm_index; /* index for associated ifp */
507 struct if_data14 ifm_data; /* statistics and other data about if */
508 };
509 #endif /* _KERNEL && COMPAT_14 */
510
511 /*
512 * Message format for use in obtaining information about interface addresses
513 * from sysctl and the routing socket.
514 */
515 struct ifa_msghdr {
516 u_short ifam_msglen; /* to skip over non-understood messages */
517 u_char ifam_version; /* future binary compatibility */
518 u_char ifam_type; /* message type */
519 int ifam_addrs; /* like rtm_addrs */
520 int ifam_flags; /* value of ifa_flags */
521 u_short ifam_index; /* index for associated ifp */
522 int ifam_metric; /* value of ifa_metric */
523 };
524
525 /*
526 * Message format announcing the arrival or departure of a network interface.
527 */
528 struct if_announcemsghdr {
529 u_short ifan_msglen; /* to skip over non-understood messages */
530 u_char ifan_version; /* future binary compatibility */
531 u_char ifan_type; /* message type */
532 u_short ifan_index; /* index for associated ifp */
533 char ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */
534 u_short ifan_what; /* what type of announcement */
535 };
536
537 #define IFAN_ARRIVAL 0 /* interface arrival */
538 #define IFAN_DEPARTURE 1 /* interface departure */
539
540 /*
541 * Interface request structure used for socket
542 * ioctl's. All interface ioctl's must have parameter
543 * definitions which begin with ifr_name. The
544 * remainder may be interface specific.
545 */
546 struct ifreq {
547 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
548 union {
549 struct sockaddr ifru_addr;
550 struct sockaddr ifru_dstaddr;
551 struct sockaddr ifru_broadaddr;
552 struct sockaddr_storage ifru_space;
553 short ifru_flags;
554 int ifru_metric;
555 int ifru_mtu;
556 int ifru_dlt;
557 u_int ifru_value;
558 void * ifru_data;
559 struct {
560 uint32_t b_buflen;
561 void *b_buf;
562 } ifru_b;
563 } ifr_ifru;
564 #define ifr_addr ifr_ifru.ifru_addr /* address */
565 #define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */
566 #define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
567 #define ifr_space ifr_ifru.ifru_space /* sockaddr_storage */
568 #define ifr_flags ifr_ifru.ifru_flags /* flags */
569 #define ifr_metric ifr_ifru.ifru_metric /* metric */
570 #define ifr_mtu ifr_ifru.ifru_mtu /* mtu */
571 #define ifr_dlt ifr_ifru.ifru_dlt /* data link type (DLT_*) */
572 #define ifr_value ifr_ifru.ifru_value /* generic value */
573 #define ifr_media ifr_ifru.ifru_metric /* media options (overload) */
574 #define ifr_data ifr_ifru.ifru_data /* for use by interface
575 * XXX deprecated
576 */
577 #define ifr_buf ifr_ifru.ifru_b.b_buf /* new interface ioctls */
578 #define ifr_buflen ifr_ifru.ifru_b.b_buflen
579 };
580
581 #ifdef _KERNEL
582 #define ifreq_setdstaddr ifreq_setaddr
583 #define ifreq_setbroadaddr ifreq_setaddr
584 #define ifreq_getdstaddr ifreq_getaddr
585 #define ifreq_getbroadaddr ifreq_getaddr
586
587 static inline const struct sockaddr *
588 ifreq_getaddr(u_long cmd, const struct ifreq *ifr)
589 {
590 return &ifr->ifr_addr;
591 }
592 #endif /* _KERNEL */
593
594 struct ifcapreq {
595 char ifcr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
596 uint64_t ifcr_capabilities; /* supported capabiliites */
597 uint64_t ifcr_capenable; /* capabilities enabled */
598 };
599
600 struct ifaliasreq {
601 char ifra_name[IFNAMSIZ]; /* if name, e.g. "en0" */
602 struct sockaddr ifra_addr;
603 struct sockaddr ifra_dstaddr;
604 #define ifra_broadaddr ifra_dstaddr
605 struct sockaddr ifra_mask;
606 };
607
608 struct ifdatareq {
609 char ifdr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
610 struct if_data ifdr_data;
611 };
612
613 struct ifmediareq {
614 char ifm_name[IFNAMSIZ]; /* if name, e.g. "en0" */
615 int ifm_current; /* current media options */
616 int ifm_mask; /* don't care mask */
617 int ifm_status; /* media status */
618 int ifm_active; /* active options */
619 int ifm_count; /* # entries in ifm_ulist
620 array */
621 int *ifm_ulist; /* media words */
622 };
623
624
625 struct ifdrv {
626 char ifd_name[IFNAMSIZ]; /* if name, e.g. "en0" */
627 unsigned long ifd_cmd;
628 size_t ifd_len;
629 void *ifd_data;
630 };
631
632 /*
633 * Structure used in SIOCGIFCONF request.
634 * Used to retrieve interface configuration
635 * for machine (useful for programs which
636 * must know all networks accessible).
637 */
638 struct ifconf {
639 int ifc_len; /* size of associated buffer */
640 union {
641 void * ifcu_buf;
642 struct ifreq *ifcu_req;
643 } ifc_ifcu;
644 #define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */
645 #define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */
646 };
647
648 /*
649 * Structure for SIOC[AGD]LIFADDR
650 */
651 struct if_laddrreq {
652 char iflr_name[IFNAMSIZ];
653 unsigned int flags;
654 #define IFLR_PREFIX 0x8000 /* in: prefix given out: kernel fills id */
655 #define IFLR_ACTIVE 0x4000 /* in/out: link-layer address activation */
656 #define IFLR_FACTORY 0x2000 /* in/out: factory link-layer address */
657 unsigned int prefixlen; /* in/out */
658 struct sockaddr_storage addr; /* in/out */
659 struct sockaddr_storage dstaddr; /* out */
660 };
661
662 /*
663 * Structure for SIOC[SG]IFADDRPREF
664 */
665 struct if_addrprefreq {
666 char ifap_name[IFNAMSIZ];
667 int16_t ifap_preference; /* in/out */
668 struct sockaddr_storage ifap_addr; /* in/out */
669 };
670
671 #include <net/if_arp.h>
672
673 #endif /* _NETBSD_SOURCE */
674
675 #ifdef _KERNEL
676 #ifdef IFAREF_DEBUG
677 #define IFAREF(ifa) \
678 do { \
679 printf("IFAREF: %s:%d %p -> %d\n", __FILE__, __LINE__, \
680 (ifa), ++(ifa)->ifa_refcnt); \
681 } while (/*CONSTCOND*/ 0)
682
683 #define IFAFREE(ifa) \
684 do { \
685 if ((ifa)->ifa_refcnt <= 0) \
686 panic("%s:%d: %p ifa_refcnt <= 0", __FILE__, \
687 __LINE__, (ifa)); \
688 printf("IFAFREE: %s:%d %p -> %d\n", __FILE__, __LINE__, \
689 (ifa), --(ifa)->ifa_refcnt); \
690 if ((ifa)->ifa_refcnt == 0) \
691 ifafree(ifa); \
692 } while (/*CONSTCOND*/ 0)
693 #else
694 #define IFAREF(ifa) (ifa)->ifa_refcnt++
695
696 #ifdef DIAGNOSTIC
697 #define IFAFREE(ifa) \
698 do { \
699 if ((ifa)->ifa_refcnt <= 0) \
700 panic("%s:%d: %p ifa_refcnt <= 0", __FILE__, \
701 __LINE__, (ifa)); \
702 if (--(ifa)->ifa_refcnt == 0) \
703 ifafree(ifa); \
704 } while (/*CONSTCOND*/ 0)
705 #else
706 #define IFAFREE(ifa) \
707 do { \
708 if (--(ifa)->ifa_refcnt == 0) \
709 ifafree(ifa); \
710 } while (/*CONSTCOND*/ 0)
711 #endif /* DIAGNOSTIC */
712 #endif /* IFAREF_DEBUG */
713
714 #ifdef ALTQ
715 #define ALTQ_DECL(x) x
716 #define ALTQ_COMMA ,
717
718 #define IFQ_ENQUEUE(ifq, m, pattr, err) \
719 do { \
720 if (ALTQ_IS_ENABLED((ifq))) \
721 ALTQ_ENQUEUE((ifq), (m), (pattr), (err)); \
722 else { \
723 if (IF_QFULL((ifq))) { \
724 m_freem((m)); \
725 (err) = ENOBUFS; \
726 } else { \
727 IF_ENQUEUE((ifq), (m)); \
728 (err) = 0; \
729 } \
730 } \
731 if ((err)) \
732 (ifq)->ifq_drops++; \
733 } while (/*CONSTCOND*/ 0)
734
735 #define IFQ_DEQUEUE(ifq, m) \
736 do { \
737 if (TBR_IS_ENABLED((ifq))) \
738 (m) = tbr_dequeue((ifq), ALTDQ_REMOVE); \
739 else if (ALTQ_IS_ENABLED((ifq))) \
740 ALTQ_DEQUEUE((ifq), (m)); \
741 else \
742 IF_DEQUEUE((ifq), (m)); \
743 } while (/*CONSTCOND*/ 0)
744
745 #define IFQ_POLL(ifq, m) \
746 do { \
747 if (TBR_IS_ENABLED((ifq))) \
748 (m) = tbr_dequeue((ifq), ALTDQ_POLL); \
749 else if (ALTQ_IS_ENABLED((ifq))) \
750 ALTQ_POLL((ifq), (m)); \
751 else \
752 IF_POLL((ifq), (m)); \
753 } while (/*CONSTCOND*/ 0)
754
755 #define IFQ_PURGE(ifq) \
756 do { \
757 if (ALTQ_IS_ENABLED((ifq))) \
758 ALTQ_PURGE((ifq)); \
759 else \
760 IF_PURGE((ifq)); \
761 } while (/*CONSTCOND*/ 0)
762
763 #define IFQ_SET_READY(ifq) \
764 do { \
765 (ifq)->altq_flags |= ALTQF_READY; \
766 } while (/*CONSTCOND*/ 0)
767
768 #define IFQ_CLASSIFY(ifq, m, af, pattr) \
769 do { \
770 if (ALTQ_IS_ENABLED((ifq))) { \
771 if (ALTQ_NEEDS_CLASSIFY((ifq))) \
772 (pattr)->pattr_class = (*(ifq)->altq_classify) \
773 ((ifq)->altq_clfier, (m), (af)); \
774 (pattr)->pattr_af = (af); \
775 (pattr)->pattr_hdr = mtod((m), void *); \
776 } \
777 } while (/*CONSTCOND*/ 0)
778 #else /* ! ALTQ */
779 #define ALTQ_DECL(x) /* nothing */
780 #define ALTQ_COMMA
781
782 #define IFQ_ENQUEUE(ifq, m, pattr, err) \
783 do { \
784 if (IF_QFULL((ifq))) { \
785 m_freem((m)); \
786 (err) = ENOBUFS; \
787 } else { \
788 IF_ENQUEUE((ifq), (m)); \
789 (err) = 0; \
790 } \
791 if ((err)) \
792 (ifq)->ifq_drops++; \
793 } while (/*CONSTCOND*/ 0)
794
795 #define IFQ_DEQUEUE(ifq, m) IF_DEQUEUE((ifq), (m))
796
797 #define IFQ_POLL(ifq, m) IF_POLL((ifq), (m))
798
799 #define IFQ_PURGE(ifq) IF_PURGE((ifq))
800
801 #define IFQ_SET_READY(ifq) /* nothing */
802
803 #define IFQ_CLASSIFY(ifq, m, af, pattr) /* nothing */
804
805 #endif /* ALTQ */
806
807 #define IFQ_IS_EMPTY(ifq) IF_IS_EMPTY((ifq))
808 #define IFQ_INC_LEN(ifq) ((ifq)->ifq_len++)
809 #define IFQ_DEC_LEN(ifq) (--(ifq)->ifq_len)
810 #define IFQ_INC_DROPS(ifq) ((ifq)->ifq_drops++)
811 #define IFQ_SET_MAXLEN(ifq, len) ((ifq)->ifq_maxlen = (len))
812
813 #include <sys/mallocvar.h>
814 MALLOC_DECLARE(M_IFADDR);
815 MALLOC_DECLARE(M_IFMADDR);
816
817 #define IFNET_FIRST() TAILQ_FIRST(&ifnet)
818 #define IFNET_NEXT(__ifp) TAILQ_NEXT((__ifp), if_list)
819 #define IFNET_FOREACH(__ifp) TAILQ_FOREACH(__ifp, &ifnet, if_list)
820 #define IFADDR_FIRST(__ifp) TAILQ_FIRST(&(__ifp)->if_addrlist)
821 #define IFADDR_NEXT(__ifa) TAILQ_NEXT((__ifa), ifa_list)
822 #define IFADDR_FOREACH(__ifa, __ifp) TAILQ_FOREACH(__ifa, \
823 &(__ifp)->if_addrlist, ifa_list)
824 #define IFADDR_EMPTY(__ifp) TAILQ_EMPTY(&(__ifp)->if_addrlist)
825
826 extern struct ifnet_head ifnet;
827 extern struct ifnet **ifindex2ifnet;
828 extern struct ifnet *lo0ifp;
829 extern size_t if_indexlim;
830
831 void ether_input(struct ifnet *, struct mbuf *);
832
833 int ifreq_setaddr(u_long, struct ifreq *, const struct sockaddr *);
834
835 struct ifnet *if_alloc(u_char);
836 void if_initname(struct ifnet *, const char *, int);
837 struct ifaddr *if_dl_create(const struct ifnet *, const struct sockaddr_dl **);
838 void if_activate_sadl(struct ifnet *, struct ifaddr *,
839 const struct sockaddr_dl *);
840 void if_set_sadl(struct ifnet *, const void *, u_char, bool);
841 void if_alloc_sadl(struct ifnet *);
842 void if_free_sadl(struct ifnet *);
843 void if_attach(struct ifnet *);
844 void if_attachdomain(void);
845 void if_attachdomain1(struct ifnet *);
846 void if_deactivate(struct ifnet *);
847 void if_purgeaddrs(struct ifnet *, int, void (*)(struct ifaddr *));
848 void if_detach(struct ifnet *);
849 void if_down(struct ifnet *);
850 void if_link_state_change(struct ifnet *, int);
851 void if_slowtimo(void *);
852 void if_up(struct ifnet *);
853 int ifconf(u_long, void *);
854 void ifinit(void);
855 void ifinit1(void);
856 int ifioctl(struct socket *, u_long, void *, struct lwp *);
857 int ifioctl_common(struct ifnet *, u_long, void *);
858 int ifpromisc(struct ifnet *, int);
859 struct ifnet *ifunit(const char *);
860
861 void ifa_insert(struct ifnet *, struct ifaddr *);
862 void ifa_remove(struct ifnet *, struct ifaddr *);
863
864 struct ifaddr *ifa_ifwithaddr(const struct sockaddr *);
865 struct ifaddr *ifa_ifwithaf(int);
866 struct ifaddr *ifa_ifwithdstaddr(const struct sockaddr *);
867 struct ifaddr *ifa_ifwithnet(const struct sockaddr *);
868 struct ifaddr *ifa_ifwithladdr(const struct sockaddr *);
869 struct ifaddr *ifa_ifwithroute(int, const struct sockaddr *,
870 const struct sockaddr *);
871 struct ifaddr *ifaof_ifpforaddr(const struct sockaddr *, struct ifnet *);
872 void ifafree(struct ifaddr *);
873 void link_rtrequest(int, struct rtentry *, const struct rt_addrinfo *);
874
875 void if_clone_attach(struct if_clone *);
876 void if_clone_detach(struct if_clone *);
877
878 int if_clone_create(const char *);
879 int if_clone_destroy(const char *);
880
881 int ifq_enqueue(struct ifnet *, struct mbuf * ALTQ_COMMA
882 ALTQ_DECL(struct altq_pktattr *));
883 int ifq_enqueue2(struct ifnet *, struct ifqueue *, struct mbuf * ALTQ_COMMA
884 ALTQ_DECL(struct altq_pktattr *));
885
886 int loioctl(struct ifnet *, u_long, void *);
887 void loopattach(int);
888 int looutput(struct ifnet *,
889 struct mbuf *, const struct sockaddr *, struct rtentry *);
890 void lortrequest(int, struct rtentry *, const struct rt_addrinfo *);
891
892 /*
893 * These are exported because they're an easy way to tell if
894 * an interface is going away without having to burn a flag.
895 */
896 int if_nulloutput(struct ifnet *, struct mbuf *,
897 const struct sockaddr *, struct rtentry *);
898 void if_nullinput(struct ifnet *, struct mbuf *);
899 void if_nullstart(struct ifnet *);
900 int if_nullioctl(struct ifnet *, u_long, void *);
901 int if_nullinit(struct ifnet *);
902 void if_nullstop(struct ifnet *, int);
903 void if_nullwatchdog(struct ifnet *);
904 void if_nulldrain(struct ifnet *);
905 #else
906 struct if_nameindex {
907 unsigned int if_index; /* 1, 2, ... */
908 char *if_name; /* null terminated name: "le0", ... */
909 };
910
911 #include <sys/cdefs.h>
912 __BEGIN_DECLS
913 unsigned int if_nametoindex(const char *);
914 char * if_indextoname(unsigned int, char *);
915 struct if_nameindex * if_nameindex(void);
916 void if_freenameindex(struct if_nameindex *);
917 __END_DECLS
918 #endif /* _KERNEL */ /* XXX really ALTQ? */
919
920 #ifdef _KERNEL
921 /*
922 * ifq sysctl support
923 */
924 int sysctl_ifq(int *name, u_int namelen, void *oldp,
925 size_t *oldlenp, void *newp, size_t newlen,
926 struct ifqueue *ifq);
927 /* symbolic names for terminal (per-protocol) CTL_IFQ_ nodes */
928 #define IFQCTL_LEN 1
929 #define IFQCTL_MAXLEN 2
930 #define IFQCTL_PEAK 3
931 #define IFQCTL_DROPS 4
932 #define IFQCTL_MAXID 5
933
934 #endif /* _KERNEL */
935
936 #ifdef _NETBSD_SOURCE
937 /*
938 * sysctl for ifq (per-protocol packet input queue variant of ifqueue)
939 */
940 #define CTL_IFQ_NAMES { \
941 { 0, 0 }, \
942 { "len", CTLTYPE_INT }, \
943 { "maxlen", CTLTYPE_INT }, \
944 { "peak", CTLTYPE_INT }, \
945 { "drops", CTLTYPE_INT }, \
946 }
947 #endif /* _NETBSD_SOURCE */
948 #endif /* !_NET_IF_H_ */
949