if.h revision 1.134.8.2 1 /* $NetBSD: if.h,v 1.134.8.2 2008/11/01 21:22:28 christos 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 timespec 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 /*
193 * Structure defining a queue for a network interface.
194 */
195 struct ifqueue {
196 struct mbuf *ifq_head;
197 struct mbuf *ifq_tail;
198 int ifq_len;
199 int ifq_maxlen;
200 int ifq_drops;
201 };
202
203 /*
204 * Structure defining a queue for a network interface.
205 *
206 * (Would like to call this struct ``if'', but C isn't PL/1.)
207 */
208 TAILQ_HEAD(ifnet_head, ifnet); /* the actual queue head */
209
210 struct ifnet { /* and the entries */
211 void *if_softc; /* lower-level data for this if */
212 TAILQ_ENTRY(ifnet) if_list; /* all struct ifnets are chained */
213 TAILQ_HEAD(, ifaddr) if_addrlist; /* linked list of addresses per if */
214 char if_xname[IFNAMSIZ]; /* external name (name + unit) */
215 int if_pcount; /* number of promiscuous listeners */
216 void * if_bpf; /* packet filter structure */
217 u_short if_index; /* numeric abbreviation for this if */
218 short if_timer; /* time 'til if_watchdog called */
219 short if_flags; /* up/down, broadcast, etc. */
220 short if__pad1; /* be nice to m68k ports */
221 struct if_data if_data; /* statistics and other data about if */
222 /*
223 * Procedure handles. If you add more of these, don't forget the
224 * corresponding NULL stub in if.c.
225 */
226 int (*if_output) /* output routine (enqueue) */
227 (struct ifnet *, struct mbuf *, const struct sockaddr *,
228 struct rtentry *);
229 void (*if_input) /* input routine (from h/w driver) */
230 (struct ifnet *, struct mbuf *);
231 void (*if_start) /* initiate output routine */
232 (struct ifnet *);
233 int (*if_ioctl) /* ioctl routine */
234 (struct ifnet *, u_long, void *);
235 int (*if_init) /* init routine */
236 (struct ifnet *);
237 void (*if_stop) /* stop routine */
238 (struct ifnet *, int);
239 void (*if_watchdog) /* timer routine */
240 (struct ifnet *);
241 void (*if_drain) /* routine to release resources */
242 (struct ifnet *);
243 struct ifaltq if_snd; /* output queue (includes altq) */
244 struct ifaddr *if_dl; /* identity of this interface. */
245 const struct sockaddr_dl *if_sadl; /* pointer to our sockaddr_dl */
246 const uint8_t *if_broadcastaddr;/* linklevel broadcast bytestring */
247 void *if_bridge; /* bridge glue */
248 int if_dlt; /* data link type (<net/dlt.h>) */
249 struct pfil_head if_pfil; /* filtering point */
250 uint64_t if_capabilities; /* interface capabilities */
251 uint64_t if_capenable; /* capabilities enabled */
252 union {
253 void * carp_s; /* carp structure (used by !carp ifs) */
254 struct ifnet *carp_d;/* ptr to carpdev (used by carp ifs) */
255 } if_carp_ptr;
256 #define if_carp if_carp_ptr.carp_s
257 #define if_carpdev if_carp_ptr.carp_d
258 /*
259 * These are pre-computed based on an interfaces enabled
260 * capabilities, for speed elsewhere.
261 */
262 int if_csum_flags_tx; /* M_CSUM_* flags for Tx */
263 int if_csum_flags_rx; /* M_CSUM_* flags for Rx */
264
265 void *if_afdata[AF_MAX];
266 struct mowner *if_mowner; /* who owns mbufs for this interface */
267
268 void *if_agrprivate; /* used only when #if NAGR > 0 */
269
270 /*
271 * pf specific data, used only when #if NPF > 0.
272 */
273 void *if_pf_kif; /* pf interface abstraction */
274 void *if_pf_groups; /* pf interface groups */
275 };
276 #define if_mtu if_data.ifi_mtu
277 #define if_type if_data.ifi_type
278 #define if_addrlen if_data.ifi_addrlen
279 #define if_hdrlen if_data.ifi_hdrlen
280 #define if_metric if_data.ifi_metric
281 #define if_link_state if_data.ifi_link_state
282 #define if_baudrate if_data.ifi_baudrate
283 #define if_ipackets if_data.ifi_ipackets
284 #define if_ierrors if_data.ifi_ierrors
285 #define if_opackets if_data.ifi_opackets
286 #define if_oerrors if_data.ifi_oerrors
287 #define if_collisions if_data.ifi_collisions
288 #define if_ibytes if_data.ifi_ibytes
289 #define if_obytes if_data.ifi_obytes
290 #define if_imcasts if_data.ifi_imcasts
291 #define if_omcasts if_data.ifi_omcasts
292 #define if_iqdrops if_data.ifi_iqdrops
293 #define if_noproto if_data.ifi_noproto
294 #define if_lastchange if_data.ifi_lastchange
295
296 #define IFF_UP 0x0001 /* interface is up */
297 #define IFF_BROADCAST 0x0002 /* broadcast address valid */
298 #define IFF_DEBUG 0x0004 /* turn on debugging */
299 #define IFF_LOOPBACK 0x0008 /* is a loopback net */
300 #define IFF_POINTOPOINT 0x0010 /* interface is point-to-point link */
301 #define IFF_NOTRAILERS 0x0020 /* avoid use of trailers */
302 #define IFF_RUNNING 0x0040 /* resources allocated */
303 #define IFF_NOARP 0x0080 /* no address resolution protocol */
304 #define IFF_PROMISC 0x0100 /* receive all packets */
305 #define IFF_ALLMULTI 0x0200 /* receive all multicast packets */
306 #define IFF_OACTIVE 0x0400 /* transmission in progress */
307 #define IFF_SIMPLEX 0x0800 /* can't hear own transmissions */
308 #define IFF_LINK0 0x1000 /* per link layer defined bit */
309 #define IFF_LINK1 0x2000 /* per link layer defined bit */
310 #define IFF_LINK2 0x4000 /* per link layer defined bit */
311 #define IFF_MULTICAST 0x8000 /* supports multicast */
312
313 #define IFFBITS \
314 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS" \
315 "\7RUNNING\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX" \
316 "\15LINK0\16LINK1\17LINK2\20MULTICAST"
317
318 /* flags set internally only: */
319 #define IFF_CANTCHANGE \
320 (IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\
321 IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI|IFF_PROMISC)
322
323 /*
324 * Some convenience macros used for setting ifi_baudrate.
325 * XXX 1000 vs. 1024? --thorpej (at) NetBSD.org
326 */
327 #define IF_Kbps(x) ((x) * 1000) /* kilobits/sec. */
328 #define IF_Mbps(x) (IF_Kbps((x) * 1000)) /* megabits/sec. */
329 #define IF_Gbps(x) (IF_Mbps((x) * 1000)) /* gigabits/sec. */
330
331 /* Capabilities that interfaces can advertise. */
332 #define IFCAP_TSOv4 0x00080 /* can do TCPv4 segmentation offload */
333 #define IFCAP_CSUM_IPv4_Rx 0x00100 /* can do IPv4 header checksums (Rx) */
334 #define IFCAP_CSUM_IPv4_Tx 0x00200 /* can do IPv4 header checksums (Tx) */
335 #define IFCAP_CSUM_TCPv4_Rx 0x00400 /* can do IPv4/TCP checksums (Rx) */
336 #define IFCAP_CSUM_TCPv4_Tx 0x00800 /* can do IPv4/TCP checksums (Tx) */
337 #define IFCAP_CSUM_UDPv4_Rx 0x01000 /* can do IPv4/UDP checksums (Rx) */
338 #define IFCAP_CSUM_UDPv4_Tx 0x02000 /* can do IPv4/UDP checksums (Tx) */
339 #define IFCAP_CSUM_TCPv6_Rx 0x04000 /* can do IPv6/TCP checksums (Rx) */
340 #define IFCAP_CSUM_TCPv6_Tx 0x08000 /* can do IPv6/TCP checksums (Tx) */
341 #define IFCAP_CSUM_UDPv6_Rx 0x10000 /* can do IPv6/UDP checksums (Rx) */
342 #define IFCAP_CSUM_UDPv6_Tx 0x20000 /* can do IPv6/UDP checksums (Tx) */
343 #define IFCAP_TSOv6 0x40000 /* can do TCPv6 segmentation offload */
344
345 #define IFCAPBITS \
346 "\020" \
347 "\10TSO4" \
348 "\11IP4CSUM_Rx" \
349 "\12IP4CSUM_Tx" \
350 "\13TCP4CSUM_Rx" \
351 "\14TCP4CSUM_Tx" \
352 "\15UDP4CSUM_Rx" \
353 "\16UDP4CSUM_Tx" \
354 "\17TCP6CSUM_Rx" \
355 "\20TCP6CSUM_Tx" \
356 "\21UDP6CSUM_Rx" \
357 "\22UDP6CSUM_Tx" \
358 "\23TSO6"
359
360 /*
361 * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
362 * input routines have queues of messages stored on ifqueue structures
363 * (defined above). Entries are added to and deleted from these structures
364 * by these macros, which should be called with ipl raised to splnet().
365 */
366 #define IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen)
367 #define IF_DROP(ifq) ((ifq)->ifq_drops++)
368 #define IF_ENQUEUE(ifq, m) do { \
369 (m)->m_nextpkt = 0; \
370 if ((ifq)->ifq_tail == 0) \
371 (ifq)->ifq_head = m; \
372 else \
373 (ifq)->ifq_tail->m_nextpkt = m; \
374 (ifq)->ifq_tail = m; \
375 (ifq)->ifq_len++; \
376 } while (/*CONSTCOND*/0)
377 #define IF_PREPEND(ifq, m) do { \
378 (m)->m_nextpkt = (ifq)->ifq_head; \
379 if ((ifq)->ifq_tail == 0) \
380 (ifq)->ifq_tail = (m); \
381 (ifq)->ifq_head = (m); \
382 (ifq)->ifq_len++; \
383 } while (/*CONSTCOND*/0)
384 #define IF_DEQUEUE(ifq, m) do { \
385 (m) = (ifq)->ifq_head; \
386 if (m) { \
387 if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \
388 (ifq)->ifq_tail = 0; \
389 (m)->m_nextpkt = 0; \
390 (ifq)->ifq_len--; \
391 } \
392 } while (/*CONSTCOND*/0)
393 #define IF_POLL(ifq, m) ((m) = (ifq)->ifq_head)
394 #define IF_PURGE(ifq) \
395 do { \
396 struct mbuf *__m0; \
397 \
398 for (;;) { \
399 IF_DEQUEUE((ifq), __m0); \
400 if (__m0 == NULL) \
401 break; \
402 else \
403 m_freem(__m0); \
404 } \
405 } while (/*CONSTCOND*/ 0)
406 #define IF_IS_EMPTY(ifq) ((ifq)->ifq_len == 0)
407
408 #ifndef IFQ_MAXLEN
409 #define IFQ_MAXLEN 256
410 #endif
411 #define IFNET_SLOWHZ 1 /* granularity is 1 second */
412
413 /*
414 * Structure defining statistics and other data kept regarding an address
415 * on a network interface.
416 */
417 struct ifaddr_data {
418 int64_t ifad_inbytes;
419 int64_t ifad_outbytes;
420 };
421
422 /*
423 * The ifaddr structure contains information about one address
424 * of an interface. They are maintained by the different address families,
425 * are allocated and attached when an address is set, and are linked
426 * together so all addresses for an interface can be located.
427 */
428 struct ifaddr {
429 struct sockaddr *ifa_addr; /* address of interface */
430 struct sockaddr *ifa_dstaddr; /* other end of p-to-p link */
431 #define ifa_broadaddr ifa_dstaddr /* broadcast address interface */
432 struct sockaddr *ifa_netmask; /* used to determine subnet */
433 struct ifnet *ifa_ifp; /* back-pointer to interface */
434 TAILQ_ENTRY(ifaddr) ifa_list; /* list of addresses for interface */
435 struct ifaddr_data ifa_data; /* statistics on the address */
436 void (*ifa_rtrequest) /* check or clean routes (+ or -)'d */
437 (int, struct rtentry *, const struct rt_addrinfo *);
438 u_int ifa_flags; /* mostly rt_flags for cloning */
439 int ifa_refcnt; /* count of references */
440 int ifa_metric; /* cost of going out this interface */
441 struct ifaddr *(*ifa_getifa)(struct ifaddr *,
442 const struct sockaddr *);
443 uint32_t *ifa_seqno;
444 int16_t ifa_preference; /* preference level for this address */
445 };
446 #define IFA_ROUTE RTF_UP /* (0x01) route installed */
447
448 /*
449 * Message format for use in obtaining information about interfaces
450 * from sysctl and the routing socket.
451 */
452 struct if_msghdr {
453 u_short ifm_msglen; /* to skip over non-understood messages */
454 u_char ifm_version; /* future binary compatibility */
455 u_char ifm_type; /* message type */
456 int ifm_addrs; /* like rtm_addrs */
457 int ifm_flags; /* value of if_flags */
458 u_short ifm_index; /* index for associated ifp */
459 struct if_data ifm_data;/* statistics and other data about if */
460 };
461
462 /*
463 * Message format for use in obtaining information about interface addresses
464 * from sysctl and the routing socket.
465 */
466 struct ifa_msghdr {
467 u_short ifam_msglen; /* to skip over non-understood messages */
468 u_char ifam_version; /* future binary compatibility */
469 u_char ifam_type; /* message type */
470 int ifam_addrs; /* like rtm_addrs */
471 int ifam_flags; /* value of ifa_flags */
472 u_short ifam_index; /* index for associated ifp */
473 int ifam_metric; /* value of ifa_metric */
474 };
475
476 /*
477 * Message format announcing the arrival or departure of a network interface.
478 */
479 struct if_announcemsghdr {
480 u_short ifan_msglen; /* to skip over non-understood messages */
481 u_char ifan_version; /* future binary compatibility */
482 u_char ifan_type; /* message type */
483 u_short ifan_index; /* index for associated ifp */
484 char ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */
485 u_short ifan_what; /* what type of announcement */
486 };
487
488 #define IFAN_ARRIVAL 0 /* interface arrival */
489 #define IFAN_DEPARTURE 1 /* interface departure */
490
491 /*
492 * Interface request structure used for socket
493 * ioctl's. All interface ioctl's must have parameter
494 * definitions which begin with ifr_name. The
495 * remainder may be interface specific.
496 */
497 struct ifreq {
498 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
499 union {
500 struct sockaddr ifru_addr;
501 struct sockaddr ifru_dstaddr;
502 struct sockaddr ifru_broadaddr;
503 struct sockaddr_storage ifru_space;
504 short ifru_flags;
505 int ifru_metric;
506 int ifru_mtu;
507 int ifru_dlt;
508 u_int ifru_value;
509 void * ifru_data;
510 struct {
511 uint32_t b_buflen;
512 void *b_buf;
513 } ifru_b;
514 } ifr_ifru;
515 #define ifr_addr ifr_ifru.ifru_addr /* address */
516 #define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */
517 #define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
518 #define ifr_space ifr_ifru.ifru_space /* sockaddr_storage */
519 #define ifr_flags ifr_ifru.ifru_flags /* flags */
520 #define ifr_metric ifr_ifru.ifru_metric /* metric */
521 #define ifr_mtu ifr_ifru.ifru_mtu /* mtu */
522 #define ifr_dlt ifr_ifru.ifru_dlt /* data link type (DLT_*) */
523 #define ifr_value ifr_ifru.ifru_value /* generic value */
524 #define ifr_media ifr_ifru.ifru_metric /* media options (overload) */
525 #define ifr_data ifr_ifru.ifru_data /* for use by interface
526 * XXX deprecated
527 */
528 #define ifr_buf ifr_ifru.ifru_b.b_buf /* new interface ioctls */
529 #define ifr_buflen ifr_ifru.ifru_b.b_buflen
530 };
531
532 #ifdef _KERNEL
533 #define ifreq_setdstaddr ifreq_setaddr
534 #define ifreq_setbroadaddr ifreq_setaddr
535 #define ifreq_getdstaddr ifreq_getaddr
536 #define ifreq_getbroadaddr ifreq_getaddr
537
538 static inline const struct sockaddr *
539 ifreq_getaddr(u_long cmd, const struct ifreq *ifr)
540 {
541 return &ifr->ifr_addr;
542 }
543 #endif /* _KERNEL */
544
545 struct ifcapreq {
546 char ifcr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
547 uint64_t ifcr_capabilities; /* supported capabiliites */
548 uint64_t ifcr_capenable; /* capabilities enabled */
549 };
550
551 struct ifaliasreq {
552 char ifra_name[IFNAMSIZ]; /* if name, e.g. "en0" */
553 struct sockaddr ifra_addr;
554 struct sockaddr ifra_dstaddr;
555 #define ifra_broadaddr ifra_dstaddr
556 struct sockaddr ifra_mask;
557 };
558
559 struct ifdatareq {
560 char ifdr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
561 struct if_data ifdr_data;
562 };
563
564 struct ifmediareq {
565 char ifm_name[IFNAMSIZ]; /* if name, e.g. "en0" */
566 int ifm_current; /* current media options */
567 int ifm_mask; /* don't care mask */
568 int ifm_status; /* media status */
569 int ifm_active; /* active options */
570 int ifm_count; /* # entries in ifm_ulist
571 array */
572 int *ifm_ulist; /* media words */
573 };
574
575
576 struct ifdrv {
577 char ifd_name[IFNAMSIZ]; /* if name, e.g. "en0" */
578 unsigned long ifd_cmd;
579 size_t ifd_len;
580 void *ifd_data;
581 };
582
583 /*
584 * Structure used in SIOCGIFCONF request.
585 * Used to retrieve interface configuration
586 * for machine (useful for programs which
587 * must know all networks accessible).
588 */
589 struct ifconf {
590 int ifc_len; /* size of associated buffer */
591 union {
592 void * ifcu_buf;
593 struct ifreq *ifcu_req;
594 } ifc_ifcu;
595 #define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */
596 #define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */
597 };
598
599 /*
600 * Structure for SIOC[AGD]LIFADDR
601 */
602 struct if_laddrreq {
603 char iflr_name[IFNAMSIZ];
604 unsigned int flags;
605 #define IFLR_PREFIX 0x8000 /* in: prefix given out: kernel fills id */
606 #define IFLR_ACTIVE 0x4000 /* in/out: link-layer address activation */
607 unsigned int prefixlen; /* in/out */
608 struct sockaddr_storage addr; /* in/out */
609 struct sockaddr_storage dstaddr; /* out */
610 };
611
612 /*
613 * Structure for SIOC[SG]IFADDRPREF
614 */
615 struct if_addrprefreq {
616 char ifap_name[IFNAMSIZ];
617 int16_t ifap_preference; /* in/out */
618 struct sockaddr_storage ifap_addr; /* in/out */
619 };
620
621 #include <net/if_arp.h>
622
623 #endif /* _NETBSD_SOURCE */
624
625 #ifdef _KERNEL
626 #ifdef IFAREF_DEBUG
627 #define IFAREF(ifa) \
628 do { \
629 printf("IFAREF: %s:%d %p -> %d\n", __FILE__, __LINE__, \
630 (ifa), ++(ifa)->ifa_refcnt); \
631 } while (/*CONSTCOND*/ 0)
632
633 #define IFAFREE(ifa) \
634 do { \
635 if ((ifa)->ifa_refcnt <= 0) \
636 panic("%s:%d: %p ifa_refcnt <= 0", __FILE__, \
637 __LINE__, (ifa)); \
638 printf("IFAFREE: %s:%d %p -> %d\n", __FILE__, __LINE__, \
639 (ifa), --(ifa)->ifa_refcnt); \
640 if ((ifa)->ifa_refcnt == 0) \
641 ifafree(ifa); \
642 } while (/*CONSTCOND*/ 0)
643 #else
644 #define IFAREF(ifa) (ifa)->ifa_refcnt++
645
646 #ifdef DIAGNOSTIC
647 #define IFAFREE(ifa) \
648 do { \
649 if ((ifa)->ifa_refcnt <= 0) \
650 panic("%s:%d: %p ifa_refcnt <= 0", __FILE__, \
651 __LINE__, (ifa)); \
652 if (--(ifa)->ifa_refcnt == 0) \
653 ifafree(ifa); \
654 } while (/*CONSTCOND*/ 0)
655 #else
656 #define IFAFREE(ifa) \
657 do { \
658 if (--(ifa)->ifa_refcnt == 0) \
659 ifafree(ifa); \
660 } while (/*CONSTCOND*/ 0)
661 #endif /* DIAGNOSTIC */
662 #endif /* IFAREF_DEBUG */
663
664 #ifdef ALTQ
665 #define ALTQ_DECL(x) x
666 #define ALTQ_COMMA ,
667
668 #define IFQ_ENQUEUE(ifq, m, pattr, err) \
669 do { \
670 if (ALTQ_IS_ENABLED((ifq))) \
671 ALTQ_ENQUEUE((ifq), (m), (pattr), (err)); \
672 else { \
673 if (IF_QFULL((ifq))) { \
674 m_freem((m)); \
675 (err) = ENOBUFS; \
676 } else { \
677 IF_ENQUEUE((ifq), (m)); \
678 (err) = 0; \
679 } \
680 } \
681 if ((err)) \
682 (ifq)->ifq_drops++; \
683 } while (/*CONSTCOND*/ 0)
684
685 #define IFQ_DEQUEUE(ifq, m) \
686 do { \
687 if (TBR_IS_ENABLED((ifq))) \
688 (m) = tbr_dequeue((ifq), ALTDQ_REMOVE); \
689 else if (ALTQ_IS_ENABLED((ifq))) \
690 ALTQ_DEQUEUE((ifq), (m)); \
691 else \
692 IF_DEQUEUE((ifq), (m)); \
693 } while (/*CONSTCOND*/ 0)
694
695 #define IFQ_POLL(ifq, m) \
696 do { \
697 if (TBR_IS_ENABLED((ifq))) \
698 (m) = tbr_dequeue((ifq), ALTDQ_POLL); \
699 else if (ALTQ_IS_ENABLED((ifq))) \
700 ALTQ_POLL((ifq), (m)); \
701 else \
702 IF_POLL((ifq), (m)); \
703 } while (/*CONSTCOND*/ 0)
704
705 #define IFQ_PURGE(ifq) \
706 do { \
707 if (ALTQ_IS_ENABLED((ifq))) \
708 ALTQ_PURGE((ifq)); \
709 else \
710 IF_PURGE((ifq)); \
711 } while (/*CONSTCOND*/ 0)
712
713 #define IFQ_SET_READY(ifq) \
714 do { \
715 (ifq)->altq_flags |= ALTQF_READY; \
716 } while (/*CONSTCOND*/ 0)
717
718 #define IFQ_CLASSIFY(ifq, m, af, pattr) \
719 do { \
720 if (ALTQ_IS_ENABLED((ifq))) { \
721 if (ALTQ_NEEDS_CLASSIFY((ifq))) \
722 (pattr)->pattr_class = (*(ifq)->altq_classify) \
723 ((ifq)->altq_clfier, (m), (af)); \
724 (pattr)->pattr_af = (af); \
725 (pattr)->pattr_hdr = mtod((m), void *); \
726 } \
727 } while (/*CONSTCOND*/ 0)
728 #else /* ! ALTQ */
729 #define ALTQ_DECL(x) /* nothing */
730 #define ALTQ_COMMA
731
732 #define IFQ_ENQUEUE(ifq, m, pattr, err) \
733 do { \
734 if (IF_QFULL((ifq))) { \
735 m_freem((m)); \
736 (err) = ENOBUFS; \
737 } else { \
738 IF_ENQUEUE((ifq), (m)); \
739 (err) = 0; \
740 } \
741 if ((err)) \
742 (ifq)->ifq_drops++; \
743 } while (/*CONSTCOND*/ 0)
744
745 #define IFQ_DEQUEUE(ifq, m) IF_DEQUEUE((ifq), (m))
746
747 #define IFQ_POLL(ifq, m) IF_POLL((ifq), (m))
748
749 #define IFQ_PURGE(ifq) IF_PURGE((ifq))
750
751 #define IFQ_SET_READY(ifq) /* nothing */
752
753 #define IFQ_CLASSIFY(ifq, m, af, pattr) /* nothing */
754
755 #endif /* ALTQ */
756
757 #define IFQ_IS_EMPTY(ifq) IF_IS_EMPTY((ifq))
758 #define IFQ_INC_LEN(ifq) ((ifq)->ifq_len++)
759 #define IFQ_DEC_LEN(ifq) (--(ifq)->ifq_len)
760 #define IFQ_INC_DROPS(ifq) ((ifq)->ifq_drops++)
761 #define IFQ_SET_MAXLEN(ifq, len) ((ifq)->ifq_maxlen = (len))
762
763 #include <sys/mallocvar.h>
764 MALLOC_DECLARE(M_IFADDR);
765 MALLOC_DECLARE(M_IFMADDR);
766
767 #define IFNET_FIRST() TAILQ_FIRST(&ifnet)
768 #define IFNET_NEXT(__ifp) TAILQ_NEXT((__ifp), if_list)
769 #define IFNET_FOREACH(__ifp) TAILQ_FOREACH(__ifp, &ifnet, if_list)
770 #define IFADDR_FIRST(__ifp) TAILQ_FIRST(&(__ifp)->if_addrlist)
771 #define IFADDR_NEXT(__ifa) TAILQ_NEXT((__ifa), ifa_list)
772 #define IFADDR_FOREACH(__ifa, __ifp) TAILQ_FOREACH(__ifa, \
773 &(__ifp)->if_addrlist, ifa_list)
774 #define IFADDR_EMPTY(__ifp) TAILQ_EMPTY(&(__ifp)->if_addrlist)
775
776 extern struct ifnet_head ifnet;
777 extern struct ifnet **ifindex2ifnet;
778 extern struct ifnet *lo0ifp;
779 extern size_t if_indexlim;
780
781 void ether_input(struct ifnet *, struct mbuf *);
782
783 int ifreq_setaddr(u_long, struct ifreq *, const struct sockaddr *);
784
785 struct ifnet *if_alloc(u_char);
786 void if_initname(struct ifnet *, const char *, int);
787 struct ifaddr *if_dl_create(const struct ifnet *, const struct sockaddr_dl **);
788 void if_activate_sadl(struct ifnet *, struct ifaddr *,
789 const struct sockaddr_dl *);
790 void if_set_sadl(struct ifnet *, const void *, u_char);
791 void if_alloc_sadl(struct ifnet *);
792 void if_free_sadl(struct ifnet *);
793 void if_attach(struct ifnet *);
794 void if_attachdomain(void);
795 void if_attachdomain1(struct ifnet *);
796 void if_deactivate(struct ifnet *);
797 void if_purgeaddrs(struct ifnet *, int, void (*)(struct ifaddr *));
798 void if_detach(struct ifnet *);
799 void if_down(struct ifnet *);
800 void if_link_state_change(struct ifnet *, int);
801 void if_slowtimo(void *);
802 void if_up(struct ifnet *);
803 int ifconf(u_long, void *);
804 void ifinit(void);
805 void ifinit1(void);
806 int ifioctl(struct socket *, u_long, void *, struct lwp *);
807 int ifioctl_common(struct ifnet *, u_long, void *);
808 int ifpromisc(struct ifnet *, int);
809 struct ifnet *ifunit(const char *);
810
811 void ifa_insert(struct ifnet *, struct ifaddr *);
812 void ifa_remove(struct ifnet *, struct ifaddr *);
813
814 struct ifaddr *ifa_ifwithaddr(const struct sockaddr *);
815 struct ifaddr *ifa_ifwithaf(int);
816 struct ifaddr *ifa_ifwithdstaddr(const struct sockaddr *);
817 struct ifaddr *ifa_ifwithnet(const struct sockaddr *);
818 struct ifaddr *ifa_ifwithladdr(const struct sockaddr *);
819 struct ifaddr *ifa_ifwithroute(int, const struct sockaddr *,
820 const struct sockaddr *);
821 struct ifaddr *ifaof_ifpforaddr(const struct sockaddr *, struct ifnet *);
822 void ifafree(struct ifaddr *);
823 void link_rtrequest(int, struct rtentry *, const struct rt_addrinfo *);
824
825 void if_clone_attach(struct if_clone *);
826 void if_clone_detach(struct if_clone *);
827
828 int if_clone_create(const char *);
829 int if_clone_destroy(const char *);
830
831 int ifq_enqueue(struct ifnet *, struct mbuf * ALTQ_COMMA
832 ALTQ_DECL(struct altq_pktattr *));
833 int ifq_enqueue2(struct ifnet *, struct ifqueue *, struct mbuf * ALTQ_COMMA
834 ALTQ_DECL(struct altq_pktattr *));
835
836 int loioctl(struct ifnet *, u_long, void *);
837 void loopattach(int);
838 int looutput(struct ifnet *,
839 struct mbuf *, const struct sockaddr *, struct rtentry *);
840 void lortrequest(int, struct rtentry *, const struct rt_addrinfo *);
841
842 /*
843 * These are exported because they're an easy way to tell if
844 * an interface is going away without having to burn a flag.
845 */
846 int if_nulloutput(struct ifnet *, struct mbuf *,
847 const struct sockaddr *, struct rtentry *);
848 void if_nullinput(struct ifnet *, struct mbuf *);
849 void if_nullstart(struct ifnet *);
850 int if_nullioctl(struct ifnet *, u_long, void *);
851 int if_nullinit(struct ifnet *);
852 void if_nullstop(struct ifnet *, int);
853 void if_nullwatchdog(struct ifnet *);
854 void if_nulldrain(struct ifnet *);
855 #else
856 struct if_nameindex {
857 unsigned int if_index; /* 1, 2, ... */
858 char *if_name; /* null terminated name: "le0", ... */
859 };
860
861 #include <sys/cdefs.h>
862 __BEGIN_DECLS
863 unsigned int if_nametoindex(const char *);
864 char * if_indextoname(unsigned int, char *);
865 struct if_nameindex * if_nameindex(void);
866 void if_freenameindex(struct if_nameindex *);
867 __END_DECLS
868 #endif /* _KERNEL */ /* XXX really ALTQ? */
869
870 #ifdef _KERNEL
871 /*
872 * ifq sysctl support
873 */
874 int sysctl_ifq(int *name, u_int namelen, void *oldp,
875 size_t *oldlenp, void *newp, size_t newlen,
876 struct ifqueue *ifq);
877 /* symbolic names for terminal (per-protocol) CTL_IFQ_ nodes */
878 #define IFQCTL_LEN 1
879 #define IFQCTL_MAXLEN 2
880 #define IFQCTL_PEAK 3
881 #define IFQCTL_DROPS 4
882 #define IFQCTL_MAXID 5
883
884 #endif /* _KERNEL */
885
886 #ifdef _NETBSD_SOURCE
887 /*
888 * sysctl for ifq (per-protocol packet input queue variant of ifqueue)
889 */
890 #define CTL_IFQ_NAMES { \
891 { 0, 0 }, \
892 { "len", CTLTYPE_INT }, \
893 { "maxlen", CTLTYPE_INT }, \
894 { "peak", CTLTYPE_INT }, \
895 { "drops", CTLTYPE_INT }, \
896 }
897 #endif /* _NETBSD_SOURCE */
898 #endif /* !_NET_IF_H_ */
899