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