if.h revision 1.242 1 /* $NetBSD: if.h,v 1.242 2017/11/16 03:07:18 ozaki-r 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 #if !defined(_KERNEL) && !defined(_STANDALONE)
67 #include <stdbool.h>
68 #endif
69
70 #include <sys/featuretest.h>
71
72 /*
73 * Length of interface external name, including terminating '\0'.
74 * Note: this is the same size as a generic device's external name.
75 */
76 #define IF_NAMESIZE 16
77
78 #if defined(_NETBSD_SOURCE)
79
80 #include <sys/socket.h>
81 #include <sys/queue.h>
82 #include <sys/mutex.h>
83
84 #include <net/dlt.h>
85 #include <net/pfil.h>
86 #ifdef _KERNEL
87 #include <net/pktqueue.h>
88 #include <sys/pslist.h>
89 #include <sys/pserialize.h>
90 #include <sys/psref.h>
91 #endif
92
93 /*
94 * Always include ALTQ glue here -- we use the ALTQ interface queue
95 * structure even when ALTQ is not configured into the kernel so that
96 * the size of struct ifnet does not changed based on the option. The
97 * ALTQ queue structure is API-compatible with the legacy ifqueue.
98 */
99 #include <altq/if_altq.h>
100
101 /*
102 * Structures defining a network interface, providing a packet
103 * transport mechanism (ala level 0 of the PUP protocols).
104 *
105 * Each interface accepts output datagrams of a specified maximum
106 * length, and provides higher level routines with input datagrams
107 * received from its medium.
108 *
109 * Output occurs when the routine if_output is called, with four parameters:
110 * (*ifp->if_output)(ifp, m, dst, rt)
111 * Here m is the mbuf chain to be sent and dst is the destination address.
112 * The output routine encapsulates the supplied datagram if necessary,
113 * and then transmits it on its medium.
114 *
115 * On input, each interface unwraps the data received by it, and either
116 * places it on the input queue of a internetwork datagram routine
117 * and posts the associated software interrupt, or passes the datagram to a raw
118 * packet input routine.
119 *
120 * Routines exist for locating interfaces by their addresses
121 * or for locating a interface on a certain network, as well as more general
122 * routing and gateway routines maintaining information used to locate
123 * interfaces. These routines live in the files if.c and route.c
124 */
125 #include <sys/time.h>
126
127 #if defined(_KERNEL_OPT)
128 #include "opt_compat_netbsd.h"
129 #include "opt_gateway.h"
130 #endif
131
132 struct mbuf;
133 struct proc;
134 struct rtentry;
135 struct socket;
136 struct ether_header;
137 struct ifaddr;
138 struct ifnet;
139 struct rt_addrinfo;
140
141 #define IFNAMSIZ IF_NAMESIZE
142
143 /*
144 * Structure describing a `cloning' interface.
145 */
146 struct if_clone {
147 LIST_ENTRY(if_clone) ifc_list; /* on list of cloners */
148 const char *ifc_name; /* name of device, e.g. `gif' */
149 size_t ifc_namelen; /* length of name */
150
151 int (*ifc_create)(struct if_clone *, int);
152 int (*ifc_destroy)(struct ifnet *);
153 };
154
155 #define IF_CLONE_INITIALIZER(name, create, destroy) \
156 { { NULL, NULL }, name, sizeof(name) - 1, create, destroy }
157
158 /*
159 * Structure used to query names of interface cloners.
160 */
161 struct if_clonereq {
162 int ifcr_total; /* total cloners (out) */
163 int ifcr_count; /* room for this many in user buffer */
164 char *ifcr_buffer; /* buffer for cloner names */
165 };
166
167 /*
168 * Structure defining statistics and other data kept regarding a network
169 * interface.
170 */
171 struct if_data {
172 /* generic interface information */
173 u_char ifi_type; /* ethernet, tokenring, etc. */
174 u_char ifi_addrlen; /* media address length */
175 u_char ifi_hdrlen; /* media header length */
176 int ifi_link_state; /* current link state */
177 uint64_t ifi_mtu; /* maximum transmission unit */
178 uint64_t ifi_metric; /* routing metric (external only) */
179 uint64_t ifi_baudrate; /* linespeed */
180 /* volatile statistics */
181 uint64_t ifi_ipackets; /* packets received on interface */
182 uint64_t ifi_ierrors; /* input errors on interface */
183 uint64_t ifi_opackets; /* packets sent on interface */
184 uint64_t ifi_oerrors; /* output errors on interface */
185 uint64_t ifi_collisions; /* collisions on csma interfaces */
186 uint64_t ifi_ibytes; /* total number of octets received */
187 uint64_t ifi_obytes; /* total number of octets sent */
188 uint64_t ifi_imcasts; /* packets received via multicast */
189 uint64_t ifi_omcasts; /* packets sent via multicast */
190 uint64_t ifi_iqdrops; /* dropped on input, this interface */
191 uint64_t ifi_noproto; /* destined for unsupported protocol */
192 struct timespec ifi_lastchange;/* last operational state change */
193 };
194
195 /*
196 * Values for if_link_state.
197 */
198 #define LINK_STATE_UNKNOWN 0 /* link invalid/unknown */
199 #define LINK_STATE_DOWN 1 /* link is down */
200 #define LINK_STATE_UP 2 /* link is up */
201
202 /*
203 * Structure defining a queue for a network interface.
204 */
205 struct ifqueue {
206 struct mbuf *ifq_head;
207 struct mbuf *ifq_tail;
208 int ifq_len;
209 int ifq_maxlen;
210 int ifq_drops;
211 kmutex_t *ifq_lock;
212 };
213
214 #ifdef _KERNEL
215 #include <sys/percpu.h>
216 #include <sys/callout.h>
217 #include <sys/rwlock.h>
218
219 #endif /* _KERNEL */
220
221 /*
222 * Structure defining a queue for a network interface.
223 *
224 * (Would like to call this struct ``if'', but C isn't PL/1.)
225 */
226 TAILQ_HEAD(ifnet_head, ifnet); /* the actual queue head */
227
228 struct bridge_softc;
229 struct bridge_iflist;
230 struct callout;
231 struct krwlock;
232 struct if_percpuq;
233 struct if_deferred_start;
234 struct in6_multi;
235
236 typedef unsigned short if_index_t;
237
238 typedef struct ifnet {
239 void *if_softc; /* lower-level data for this if */
240 /* DEPRECATED. Keep it to avoid breaking kvm(3) users */
241 TAILQ_ENTRY(ifnet) if_list; /* all struct ifnets are chained */
242 TAILQ_HEAD(, ifaddr) if_addrlist; /* linked list of addresses per if */
243 char if_xname[IFNAMSIZ]; /* external name (name + unit) */
244 int if_pcount; /* number of promiscuous listeners */
245 struct bpf_if *if_bpf; /* packet filter structure */
246 if_index_t if_index; /* numeric abbreviation for this if */
247 short if_timer; /* time 'til if_slowtimo called */
248 unsigned short if_flags; /* up/down, broadcast, etc. */
249 short if_extflags; /* if_output MP-safe, etc. */
250 struct if_data if_data; /* statistics and other data about if */
251 /*
252 * Procedure handles. If you add more of these, don't forget the
253 * corresponding NULL stub in if.c.
254 */
255 int (*if_output) /* output routine (enqueue) */
256 (struct ifnet *, struct mbuf *, const struct sockaddr *,
257 const struct rtentry *);
258 void (*_if_input) /* input routine (from h/w driver) */
259 (struct ifnet *, struct mbuf *);
260 void (*if_start) /* initiate output routine */
261 (struct ifnet *);
262 int (*if_transmit) /* output routine, must be MP-safe */
263 (struct ifnet *, struct mbuf *);
264 int (*if_ioctl) /* ioctl routine */
265 (struct ifnet *, u_long, void *);
266 int (*if_init) /* init routine */
267 (struct ifnet *);
268 void (*if_stop) /* stop routine */
269 (struct ifnet *, int);
270 void (*if_slowtimo) /* timer routine */
271 (struct ifnet *);
272 #define if_watchdog if_slowtimo
273 void (*if_drain) /* routine to release resources */
274 (struct ifnet *);
275 struct ifaltq if_snd; /* output queue (includes altq) */
276 struct ifaddr *if_dl; /* identity of this interface. */
277 const struct sockaddr_dl *if_sadl; /* pointer to sockaddr_dl
278 * of if_dl
279 */
280 /* if_hwdl: h/w identity
281 *
282 * May be NULL. If not NULL, it is the address assigned
283 * to the interface by the manufacturer, so it very likely
284 * to be unique. It MUST NOT be deleted. It is highly
285 * suitable for deriving the EUI64 for the interface.
286 */
287 struct ifaddr *if_hwdl;
288 const uint8_t *if_broadcastaddr;/* linklevel broadcast bytestring */
289 struct bridge_softc *if_bridge; /* bridge glue */
290 struct bridge_iflist *if_bridgeif; /* shortcut to interface list entry */
291 int if_dlt; /* data link type (<net/dlt.h>) */
292 pfil_head_t * if_pfil; /* filtering point */
293 uint64_t if_capabilities; /* interface capabilities */
294 uint64_t if_capenable; /* capabilities enabled */
295 union {
296 void * carp_s; /* carp structure (used by !carp ifs) */
297 struct ifnet *carp_d;/* ptr to carpdev (used by carp ifs) */
298 } if_carp_ptr;
299 #define if_carp if_carp_ptr.carp_s
300 #define if_carpdev if_carp_ptr.carp_d
301 /*
302 * These are pre-computed based on an interfaces enabled
303 * capabilities, for speed elsewhere.
304 */
305 int if_csum_flags_tx; /* M_CSUM_* flags for Tx */
306 int if_csum_flags_rx; /* M_CSUM_* flags for Rx */
307
308 void *if_afdata[AF_MAX];
309 struct mowner *if_mowner; /* who owns mbufs for this interface */
310
311 void *if_agrprivate; /* used only when #if NAGR > 0 */
312
313 /*
314 * pf specific data, used only when #if NPF > 0.
315 */
316 void *if_pf_kif; /* pf interface abstraction */
317 void *if_pf_groups; /* pf interface groups */
318 /*
319 * During an ifnet's lifetime, it has only one if_index, but
320 * and if_index is not sufficient to identify an ifnet
321 * because during the lifetime of the system, many ifnets may occupy a
322 * given if_index. Let us tell different ifnets at the same
323 * if_index apart by their if_index_gen, a unique number that each ifnet
324 * is assigned when it if_attach()s. Now, the kernel can use the
325 * pair (if_index, if_index_gen) as a weak reference to an ifnet.
326 */
327 uint64_t if_index_gen; /* generation number for the ifnet
328 * at if_index: if two ifnets' index
329 * and generation number are both the
330 * same, they are the same ifnet.
331 */
332 struct sysctllog *if_sysctl_log;
333 int (*if_initaddr)(struct ifnet *, struct ifaddr *, bool);
334 int (*if_mcastop)(struct ifnet *, const unsigned long,
335 const struct sockaddr *);
336 int (*if_setflags)(struct ifnet *, const short);
337 kmutex_t *if_ioctl_lock;
338 #ifdef _KERNEL /* XXX kvm(3) */
339 struct callout *if_slowtimo_ch;
340 struct krwlock *if_afdata_lock;
341 struct if_percpuq *if_percpuq; /* We should remove it in the future */
342 void *if_link_si; /* softint to handle link state changes */
343 uint16_t if_link_queue; /* masked link state change queue */
344 struct pslist_entry if_pslist_entry;
345 struct psref_target if_psref;
346 struct pslist_head if_addr_pslist;
347 struct if_deferred_start *if_deferred_start;
348 /* XXX should be protocol independent */
349 LIST_HEAD(, in6_multi) if_multiaddrs;
350 #endif
351 } ifnet_t;
352
353 #define if_mtu if_data.ifi_mtu
354 #define if_type if_data.ifi_type
355 #define if_addrlen if_data.ifi_addrlen
356 #define if_hdrlen if_data.ifi_hdrlen
357 #define if_metric if_data.ifi_metric
358 #define if_link_state if_data.ifi_link_state
359 #define if_baudrate if_data.ifi_baudrate
360 #define if_ipackets if_data.ifi_ipackets
361 #define if_ierrors if_data.ifi_ierrors
362 #define if_opackets if_data.ifi_opackets
363 #define if_oerrors if_data.ifi_oerrors
364 #define if_collisions if_data.ifi_collisions
365 #define if_ibytes if_data.ifi_ibytes
366 #define if_obytes if_data.ifi_obytes
367 #define if_imcasts if_data.ifi_imcasts
368 #define if_omcasts if_data.ifi_omcasts
369 #define if_iqdrops if_data.ifi_iqdrops
370 #define if_noproto if_data.ifi_noproto
371 #define if_lastchange if_data.ifi_lastchange
372
373 #define IFF_UP 0x0001 /* interface is up */
374 #define IFF_BROADCAST 0x0002 /* broadcast address valid */
375 #define IFF_DEBUG 0x0004 /* turn on debugging */
376 #define IFF_LOOPBACK 0x0008 /* is a loopback net */
377 #define IFF_POINTOPOINT 0x0010 /* interface is point-to-point link */
378 #define IFF_NOTRAILERS 0x0020 /* avoid use of trailers */
379 #define IFF_RUNNING 0x0040 /* resources allocated */
380 #define IFF_NOARP 0x0080 /* no address resolution protocol */
381 #define IFF_PROMISC 0x0100 /* receive all packets */
382 #define IFF_ALLMULTI 0x0200 /* receive all multicast packets */
383 #define IFF_OACTIVE 0x0400 /* transmission in progress */
384 #define IFF_SIMPLEX 0x0800 /* can't hear own transmissions */
385 #define IFF_LINK0 0x1000 /* per link layer defined bit */
386 #define IFF_LINK1 0x2000 /* per link layer defined bit */
387 #define IFF_LINK2 0x4000 /* per link layer defined bit */
388 #define IFF_MULTICAST 0x8000 /* supports multicast */
389
390 #define IFEF_MPSAFE __BIT(0) /* handlers can run in parallel (see below) */
391 #define IFEF_NO_LINK_STATE_CHANGE __BIT(1) /* doesn't use link state interrupts */
392
393 /*
394 * if_XXX() handlers that IFEF_MPSAFE suppresses KERNEL_LOCK:
395 * - if_start
396 * - if_output
397 */
398
399 #ifdef _KERNEL
400 static inline bool
401 if_is_mpsafe(struct ifnet *ifp)
402 {
403
404 return ((ifp->if_extflags & IFEF_MPSAFE) != 0);
405 }
406
407 static inline int
408 if_output_lock(struct ifnet *cifp, struct ifnet *ifp, struct mbuf *m,
409 const struct sockaddr *dst, const struct rtentry *rt)
410 {
411
412 if (if_is_mpsafe(cifp)) {
413 return (*cifp->if_output)(ifp, m, dst, rt);
414 } else {
415 int ret;
416
417 KERNEL_LOCK(1, NULL);
418 ret = (*cifp->if_output)(ifp, m, dst, rt);
419 KERNEL_UNLOCK_ONE(NULL);
420 return ret;
421 }
422 }
423
424 static inline void
425 if_start_lock(struct ifnet *ifp)
426 {
427
428 if (if_is_mpsafe(ifp)) {
429 (*ifp->if_start)(ifp);
430 } else {
431 KERNEL_LOCK(1, NULL);
432 (*ifp->if_start)(ifp);
433 KERNEL_UNLOCK_ONE(NULL);
434 }
435 }
436
437 static inline bool
438 if_is_link_state_changeable(struct ifnet *ifp)
439 {
440
441 return ((ifp->if_extflags & IFEF_NO_LINK_STATE_CHANGE) == 0);
442 }
443 #endif /* _KERNEL */
444
445 #define IFFBITS \
446 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS" \
447 "\7RUNNING\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX" \
448 "\15LINK0\16LINK1\17LINK2\20MULTICAST"
449
450 /* flags set internally only: */
451 #define IFF_CANTCHANGE \
452 (IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\
453 IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI|IFF_PROMISC)
454
455 /*
456 * Some convenience macros used for setting ifi_baudrate.
457 */
458 #define IF_Kbps(x) ((x) * 1000ULL) /* kilobits/sec. */
459 #define IF_Mbps(x) (IF_Kbps((x) * 1000ULL)) /* megabits/sec. */
460 #define IF_Gbps(x) (IF_Mbps((x) * 1000ULL)) /* gigabits/sec. */
461
462 /* Capabilities that interfaces can advertise. */
463 /* 0x01 .. 0x40 were previously used */
464 #define IFCAP_TSOv4 0x00080 /* can do TCPv4 segmentation offload */
465 #define IFCAP_CSUM_IPv4_Rx 0x00100 /* can do IPv4 header checksums (Rx) */
466 #define IFCAP_CSUM_IPv4_Tx 0x00200 /* can do IPv4 header checksums (Tx) */
467 #define IFCAP_CSUM_TCPv4_Rx 0x00400 /* can do IPv4/TCP checksums (Rx) */
468 #define IFCAP_CSUM_TCPv4_Tx 0x00800 /* can do IPv4/TCP checksums (Tx) */
469 #define IFCAP_CSUM_UDPv4_Rx 0x01000 /* can do IPv4/UDP checksums (Rx) */
470 #define IFCAP_CSUM_UDPv4_Tx 0x02000 /* can do IPv4/UDP checksums (Tx) */
471 #define IFCAP_CSUM_TCPv6_Rx 0x04000 /* can do IPv6/TCP checksums (Rx) */
472 #define IFCAP_CSUM_TCPv6_Tx 0x08000 /* can do IPv6/TCP checksums (Tx) */
473 #define IFCAP_CSUM_UDPv6_Rx 0x10000 /* can do IPv6/UDP checksums (Rx) */
474 #define IFCAP_CSUM_UDPv6_Tx 0x20000 /* can do IPv6/UDP checksums (Tx) */
475 #define IFCAP_TSOv6 0x40000 /* can do TCPv6 segmentation offload */
476 #define IFCAP_LRO 0x80000 /* can do Large Receive Offload */
477 #define IFCAP_MASK 0xfff80 /* currently valid capabilities */
478
479 #define IFCAPBITS \
480 "\020" \
481 "\10TSO4" \
482 "\11IP4CSUM_Rx" \
483 "\12IP4CSUM_Tx" \
484 "\13TCP4CSUM_Rx" \
485 "\14TCP4CSUM_Tx" \
486 "\15UDP4CSUM_Rx" \
487 "\16UDP4CSUM_Tx" \
488 "\17TCP6CSUM_Rx" \
489 "\20TCP6CSUM_Tx" \
490 "\21UDP6CSUM_Rx" \
491 "\22UDP6CSUM_Tx" \
492 "\23TSO6" \
493 "\24LRO" \
494
495 #define IF_AFDATA_LOCK_INIT(ifp) \
496 do {(ifp)->if_afdata_lock = rw_obj_alloc();} while (0)
497
498 #define IF_AFDATA_LOCK_DESTROY(ifp) rw_obj_free((ifp)->if_afdata_lock)
499
500 #define IF_AFDATA_WLOCK(ifp) rw_enter((ifp)->if_afdata_lock, RW_WRITER)
501 #define IF_AFDATA_RLOCK(ifp) rw_enter((ifp)->if_afdata_lock, RW_READER)
502 #define IF_AFDATA_WUNLOCK(ifp) rw_exit((ifp)->if_afdata_lock)
503 #define IF_AFDATA_RUNLOCK(ifp) rw_exit((ifp)->if_afdata_lock)
504 #define IF_AFDATA_LOCK(ifp) IF_AFDATA_WLOCK(ifp)
505 #define IF_AFDATA_UNLOCK(ifp) IF_AFDATA_WUNLOCK(ifp)
506 #define IF_AFDATA_TRYLOCK(ifp) rw_tryenter((ifp)->if_afdata_lock, RW_WRITER)
507
508 #define IF_AFDATA_LOCK_ASSERT(ifp) \
509 KASSERT(rw_lock_held((ifp)->if_afdata_lock))
510 #define IF_AFDATA_RLOCK_ASSERT(ifp) \
511 KASSERT(rw_read_held((ifp)->if_afdata_lock))
512 #define IF_AFDATA_WLOCK_ASSERT(ifp) \
513 KASSERT(rw_write_held((ifp)->if_afdata_lock))
514
515 /*
516 * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
517 * input routines have queues of messages stored on ifqueue structures
518 * (defined above). Entries are added to and deleted from these structures
519 * by these macros, which should be called with ipl raised to splnet().
520 */
521 #define IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen)
522 #define IF_DROP(ifq) ((ifq)->ifq_drops++)
523 #define IF_ENQUEUE(ifq, m) do { \
524 (m)->m_nextpkt = 0; \
525 if ((ifq)->ifq_tail == 0) \
526 (ifq)->ifq_head = m; \
527 else \
528 (ifq)->ifq_tail->m_nextpkt = m; \
529 (ifq)->ifq_tail = m; \
530 (ifq)->ifq_len++; \
531 } while (/*CONSTCOND*/0)
532 #define IF_PREPEND(ifq, m) do { \
533 (m)->m_nextpkt = (ifq)->ifq_head; \
534 if ((ifq)->ifq_tail == 0) \
535 (ifq)->ifq_tail = (m); \
536 (ifq)->ifq_head = (m); \
537 (ifq)->ifq_len++; \
538 } while (/*CONSTCOND*/0)
539 #define IF_DEQUEUE(ifq, m) do { \
540 (m) = (ifq)->ifq_head; \
541 if (m) { \
542 if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \
543 (ifq)->ifq_tail = 0; \
544 (m)->m_nextpkt = 0; \
545 (ifq)->ifq_len--; \
546 } \
547 } while (/*CONSTCOND*/0)
548 #define IF_POLL(ifq, m) ((m) = (ifq)->ifq_head)
549 #define IF_PURGE(ifq) \
550 do { \
551 struct mbuf *__m0; \
552 \
553 for (;;) { \
554 IF_DEQUEUE((ifq), __m0); \
555 if (__m0 == NULL) \
556 break; \
557 else \
558 m_freem(__m0); \
559 } \
560 } while (/*CONSTCOND*/ 0)
561 #define IF_IS_EMPTY(ifq) ((ifq)->ifq_len == 0)
562
563 #ifndef IFQ_MAXLEN
564 #define IFQ_MAXLEN 256
565 #endif
566 #define IFNET_SLOWHZ 1 /* granularity is 1 second */
567
568 /*
569 * Structure defining statistics and other data kept regarding an address
570 * on a network interface.
571 */
572 struct ifaddr_data {
573 int64_t ifad_inbytes;
574 int64_t ifad_outbytes;
575 };
576
577 /*
578 * The ifaddr structure contains information about one address
579 * of an interface. They are maintained by the different address families,
580 * are allocated and attached when an address is set, and are linked
581 * together so all addresses for an interface can be located.
582 */
583 struct ifaddr {
584 struct sockaddr *ifa_addr; /* address of interface */
585 struct sockaddr *ifa_dstaddr; /* other end of p-to-p link */
586 #define ifa_broadaddr ifa_dstaddr /* broadcast address interface */
587 struct sockaddr *ifa_netmask; /* used to determine subnet */
588 struct ifnet *ifa_ifp; /* back-pointer to interface */
589 TAILQ_ENTRY(ifaddr) ifa_list; /* list of addresses for interface */
590 struct ifaddr_data ifa_data; /* statistics on the address */
591 void (*ifa_rtrequest) /* check or clean routes (+ or -)'d */
592 (int, struct rtentry *, const struct rt_addrinfo *);
593 u_int ifa_flags; /* mostly rt_flags for cloning */
594 int ifa_refcnt; /* count of references */
595 int ifa_metric; /* cost of going out this interface */
596 struct ifaddr *(*ifa_getifa)(struct ifaddr *,
597 const struct sockaddr *);
598 uint32_t *ifa_seqno;
599 int16_t ifa_preference; /* preference level for this address */
600 #ifdef _KERNEL
601 struct pslist_entry ifa_pslist_entry;
602 struct psref_target ifa_psref;
603 #endif
604 };
605 #define IFA_ROUTE RTF_UP /* (0x01) route installed */
606 #define IFA_DESTROYING 0x2
607
608 /*
609 * Message format for use in obtaining information about interfaces from
610 * sysctl and the routing socket. We need to force 64-bit alignment if we
611 * aren't using compatiblity definitons.
612 */
613 #if !defined(_KERNEL) || !defined(COMPAT_RTSOCK)
614 #define __align64 __aligned(sizeof(uint64_t))
615 #else
616 #define __align64
617 #endif
618 struct if_msghdr {
619 u_short ifm_msglen __align64;
620 /* to skip over non-understood messages */
621 u_char ifm_version; /* future binary compatibility */
622 u_char ifm_type; /* message type */
623 int ifm_addrs; /* like rtm_addrs */
624 int ifm_flags; /* value of if_flags */
625 u_short ifm_index; /* index for associated ifp */
626 struct if_data ifm_data __align64;
627 /* statistics and other data about if */
628 };
629
630 /*
631 * Message format for use in obtaining information about interface addresses
632 * from sysctl and the routing socket.
633 */
634 struct ifa_msghdr {
635 u_short ifam_msglen __align64;
636 /* to skip over non-understood messages */
637 u_char ifam_version; /* future binary compatibility */
638 u_char ifam_type; /* message type */
639 u_short ifam_index; /* index for associated ifp */
640 int ifam_flags; /* value of ifa_flags */
641 int ifam_addrs; /* like rtm_addrs */
642 pid_t ifam_pid; /* identify sender */
643 int ifam_addrflags; /* family specific address flags */
644 int ifam_metric; /* value of ifa_metric */
645 };
646
647 /*
648 * Message format announcing the arrival or departure of a network interface.
649 */
650 struct if_announcemsghdr {
651 u_short ifan_msglen __align64;
652 /* to skip over non-understood messages */
653 u_char ifan_version; /* future binary compatibility */
654 u_char ifan_type; /* message type */
655 u_short ifan_index; /* index for associated ifp */
656 char ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */
657 u_short ifan_what; /* what type of announcement */
658 };
659
660 #define IFAN_ARRIVAL 0 /* interface arrival */
661 #define IFAN_DEPARTURE 1 /* interface departure */
662
663 #undef __align64
664
665 /*
666 * Interface request structure used for socket
667 * ioctl's. All interface ioctl's must have parameter
668 * definitions which begin with ifr_name. The
669 * remainder may be interface specific.
670 */
671 struct ifreq {
672 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
673 union {
674 struct sockaddr ifru_addr;
675 struct sockaddr ifru_dstaddr;
676 struct sockaddr ifru_broadaddr;
677 struct sockaddr_storage ifru_space;
678 short ifru_flags;
679 int ifru_addrflags;
680 int ifru_metric;
681 int ifru_mtu;
682 int ifru_dlt;
683 u_int ifru_value;
684 void * ifru_data;
685 struct {
686 uint32_t b_buflen;
687 void *b_buf;
688 } ifru_b;
689 } ifr_ifru;
690 #define ifr_addr ifr_ifru.ifru_addr /* address */
691 #define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */
692 #define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
693 #define ifr_space ifr_ifru.ifru_space /* sockaddr_storage */
694 #define ifr_flags ifr_ifru.ifru_flags /* flags */
695 #define ifr_addrflags ifr_ifru.ifru_addrflags /* addr flags */
696 #define ifr_metric ifr_ifru.ifru_metric /* metric */
697 #define ifr_mtu ifr_ifru.ifru_mtu /* mtu */
698 #define ifr_dlt ifr_ifru.ifru_dlt /* data link type (DLT_*) */
699 #define ifr_value ifr_ifru.ifru_value /* generic value */
700 #define ifr_media ifr_ifru.ifru_metric /* media options (overload) */
701 #define ifr_data ifr_ifru.ifru_data /* for use by interface
702 * XXX deprecated
703 */
704 #define ifr_buf ifr_ifru.ifru_b.b_buf /* new interface ioctls */
705 #define ifr_buflen ifr_ifru.ifru_b.b_buflen
706 #define ifr_index ifr_ifru.ifru_value /* interface index, BSD */
707 #define ifr_ifindex ifr_index /* interface index, linux */
708 };
709
710 #ifdef _KERNEL
711 #define ifreq_setdstaddr ifreq_setaddr
712 #define ifreq_setbroadaddr ifreq_setaddr
713 #define ifreq_getdstaddr ifreq_getaddr
714 #define ifreq_getbroadaddr ifreq_getaddr
715
716 static inline const struct sockaddr *
717 /*ARGSUSED*/
718 ifreq_getaddr(u_long cmd, const struct ifreq *ifr)
719 {
720 return &ifr->ifr_addr;
721 }
722 #endif /* _KERNEL */
723
724 struct ifcapreq {
725 char ifcr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
726 uint64_t ifcr_capabilities; /* supported capabiliites */
727 uint64_t ifcr_capenable; /* capabilities enabled */
728 };
729
730 struct ifaliasreq {
731 char ifra_name[IFNAMSIZ]; /* if name, e.g. "en0" */
732 struct sockaddr ifra_addr;
733 struct sockaddr ifra_dstaddr;
734 #define ifra_broadaddr ifra_dstaddr
735 struct sockaddr ifra_mask;
736 };
737
738 struct ifdatareq {
739 char ifdr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
740 struct if_data ifdr_data;
741 };
742
743 struct ifmediareq {
744 char ifm_name[IFNAMSIZ]; /* if name, e.g. "en0" */
745 int ifm_current; /* current media options */
746 int ifm_mask; /* don't care mask */
747 int ifm_status; /* media status */
748 int ifm_active; /* active options */
749 int ifm_count; /* # entries in ifm_ulist
750 array */
751 int *ifm_ulist; /* media words */
752 };
753
754
755 struct ifdrv {
756 char ifd_name[IFNAMSIZ]; /* if name, e.g. "en0" */
757 unsigned long ifd_cmd;
758 size_t ifd_len;
759 void *ifd_data;
760 };
761 #define IFLINKSTR_QUERYLEN 0x01
762 #define IFLINKSTR_UNSET 0x02
763
764 /*
765 * Structure used in SIOCGIFCONF request.
766 * Used to retrieve interface configuration
767 * for machine (useful for programs which
768 * must know all networks accessible).
769 */
770 struct ifconf {
771 int ifc_len; /* size of associated buffer */
772 union {
773 void * ifcu_buf;
774 struct ifreq *ifcu_req;
775 } ifc_ifcu;
776 #define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */
777 #define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */
778 };
779
780 /*
781 * Structure for SIOC[AGD]LIFADDR
782 */
783 struct if_laddrreq {
784 char iflr_name[IFNAMSIZ];
785 unsigned int flags;
786 #define IFLR_PREFIX 0x8000 /* in: prefix given out: kernel fills id */
787 #define IFLR_ACTIVE 0x4000 /* in/out: link-layer address activation */
788 #define IFLR_FACTORY 0x2000 /* in/out: factory link-layer address */
789 unsigned int prefixlen; /* in/out */
790 struct sockaddr_storage addr; /* in/out */
791 struct sockaddr_storage dstaddr; /* out */
792 };
793
794 /*
795 * Structure for SIOC[SG]IFADDRPREF
796 */
797 struct if_addrprefreq {
798 char ifap_name[IFNAMSIZ];
799 int16_t ifap_preference; /* in/out */
800 struct sockaddr_storage ifap_addr; /* in/out */
801 };
802
803 #include <net/if_arp.h>
804
805 #endif /* _NETBSD_SOURCE */
806
807 #ifdef _KERNEL
808 #ifdef ALTQ
809 #define IFQ_ENQUEUE(ifq, m, err) \
810 do { \
811 mutex_enter((ifq)->ifq_lock); \
812 if (ALTQ_IS_ENABLED((ifq))) \
813 ALTQ_ENQUEUE((ifq), (m), (err)); \
814 else { \
815 if (IF_QFULL((ifq))) { \
816 m_freem((m)); \
817 (err) = ENOBUFS; \
818 } else { \
819 IF_ENQUEUE((ifq), (m)); \
820 (err) = 0; \
821 } \
822 } \
823 if ((err)) \
824 (ifq)->ifq_drops++; \
825 mutex_exit((ifq)->ifq_lock); \
826 } while (/*CONSTCOND*/ 0)
827
828 #define IFQ_DEQUEUE(ifq, m) \
829 do { \
830 mutex_enter((ifq)->ifq_lock); \
831 if (TBR_IS_ENABLED((ifq))) \
832 (m) = tbr_dequeue((ifq), ALTDQ_REMOVE); \
833 else if (ALTQ_IS_ENABLED((ifq))) \
834 ALTQ_DEQUEUE((ifq), (m)); \
835 else \
836 IF_DEQUEUE((ifq), (m)); \
837 mutex_exit((ifq)->ifq_lock); \
838 } while (/*CONSTCOND*/ 0)
839
840 #define IFQ_POLL(ifq, m) \
841 do { \
842 mutex_enter((ifq)->ifq_lock); \
843 if (TBR_IS_ENABLED((ifq))) \
844 (m) = tbr_dequeue((ifq), ALTDQ_POLL); \
845 else if (ALTQ_IS_ENABLED((ifq))) \
846 ALTQ_POLL((ifq), (m)); \
847 else \
848 IF_POLL((ifq), (m)); \
849 mutex_exit((ifq)->ifq_lock); \
850 } while (/*CONSTCOND*/ 0)
851
852 #define IFQ_PURGE(ifq) \
853 do { \
854 mutex_enter((ifq)->ifq_lock); \
855 if (ALTQ_IS_ENABLED((ifq))) \
856 ALTQ_PURGE((ifq)); \
857 else \
858 IF_PURGE((ifq)); \
859 mutex_exit((ifq)->ifq_lock); \
860 } while (/*CONSTCOND*/ 0)
861
862 #define IFQ_SET_READY(ifq) \
863 do { \
864 (ifq)->altq_flags |= ALTQF_READY; \
865 } while (/*CONSTCOND*/ 0)
866
867 #define IFQ_CLASSIFY(ifq, m, af) \
868 do { \
869 mutex_enter((ifq)->ifq_lock); \
870 if (ALTQ_IS_ENABLED((ifq))) { \
871 if (ALTQ_NEEDS_CLASSIFY((ifq))) \
872 m->m_pkthdr.pattr_class = (*(ifq)->altq_classify) \
873 ((ifq)->altq_clfier, (m), (af)); \
874 m->m_pkthdr.pattr_af = (af); \
875 m->m_pkthdr.pattr_hdr = mtod((m), void *); \
876 } \
877 mutex_exit((ifq)->ifq_lock); \
878 } while (/*CONSTCOND*/ 0)
879 #else /* ! ALTQ */
880 #define IFQ_ENQUEUE(ifq, m, err) \
881 do { \
882 mutex_enter((ifq)->ifq_lock); \
883 if (IF_QFULL((ifq))) { \
884 m_freem((m)); \
885 (err) = ENOBUFS; \
886 } else { \
887 IF_ENQUEUE((ifq), (m)); \
888 (err) = 0; \
889 } \
890 if ((err)) \
891 (ifq)->ifq_drops++; \
892 mutex_exit((ifq)->ifq_lock); \
893 } while (/*CONSTCOND*/ 0)
894
895 #define IFQ_DEQUEUE(ifq, m) \
896 do { \
897 mutex_enter((ifq)->ifq_lock); \
898 IF_DEQUEUE((ifq), (m)); \
899 mutex_exit((ifq)->ifq_lock); \
900 } while (/*CONSTCOND*/ 0)
901
902 #define IFQ_POLL(ifq, m) \
903 do { \
904 mutex_enter((ifq)->ifq_lock); \
905 IF_POLL((ifq), (m)); \
906 mutex_exit((ifq)->ifq_lock); \
907 } while (/*CONSTCOND*/ 0)
908
909 #define IFQ_PURGE(ifq) \
910 do { \
911 mutex_enter((ifq)->ifq_lock); \
912 IF_PURGE((ifq)); \
913 mutex_exit((ifq)->ifq_lock); \
914 } while (/*CONSTCOND*/ 0)
915
916 #define IFQ_SET_READY(ifq) /* nothing */
917
918 #define IFQ_CLASSIFY(ifq, m, af) /* nothing */
919
920 #endif /* ALTQ */
921
922 #define IFQ_LOCK_INIT(ifq) (ifq)->ifq_lock = \
923 mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET)
924 #define IFQ_LOCK_DESTROY(ifq) mutex_obj_free((ifq)->ifq_lock)
925 #define IFQ_LOCK(ifq) mutex_enter((ifq)->ifq_lock)
926 #define IFQ_UNLOCK(ifq) mutex_exit((ifq)->ifq_lock)
927
928 #define IFQ_IS_EMPTY(ifq) IF_IS_EMPTY((ifq))
929 #define IFQ_INC_LEN(ifq) ((ifq)->ifq_len++)
930 #define IFQ_DEC_LEN(ifq) (--(ifq)->ifq_len)
931 #define IFQ_INC_DROPS(ifq) ((ifq)->ifq_drops++)
932 #define IFQ_SET_MAXLEN(ifq, len) ((ifq)->ifq_maxlen = (len))
933
934 #include <sys/mallocvar.h>
935 MALLOC_DECLARE(M_IFADDR);
936 MALLOC_DECLARE(M_IFMADDR);
937
938 int ifreq_setaddr(u_long, struct ifreq *, const struct sockaddr *);
939
940 struct ifnet *if_alloc(u_char);
941 void if_free(struct ifnet *);
942 void if_initname(struct ifnet *, const char *, int);
943 struct ifaddr *if_dl_create(const struct ifnet *, const struct sockaddr_dl **);
944 void if_activate_sadl(struct ifnet *, struct ifaddr *,
945 const struct sockaddr_dl *);
946 void if_set_sadl(struct ifnet *, const void *, u_char, bool);
947 void if_alloc_sadl(struct ifnet *);
948 int if_initialize(struct ifnet *);
949 void if_register(struct ifnet *);
950 int if_attach(struct ifnet *); /* Deprecated. Use if_initialize and if_register */
951 void if_attachdomain(void);
952 void if_deactivate(struct ifnet *);
953 bool if_is_deactivated(const struct ifnet *);
954 void if_purgeaddrs(struct ifnet *, int, void (*)(struct ifaddr *));
955 void if_detach(struct ifnet *);
956 void if_down(struct ifnet *);
957 void if_link_state_change(struct ifnet *, int);
958 void if_link_state_change_softint(struct ifnet *, int);
959 void if_up(struct ifnet *);
960 void ifinit(void);
961 void ifinit1(void);
962 int ifaddrpref_ioctl(struct socket *, u_long, void *, struct ifnet *);
963 extern int (*ifioctl)(struct socket *, u_long, void *, struct lwp *);
964 int ifioctl_common(struct ifnet *, u_long, void *);
965 int ifpromisc(struct ifnet *, int);
966 int if_addr_init(ifnet_t *, struct ifaddr *, bool);
967 int if_do_dad(struct ifnet *);
968 int if_mcast_op(ifnet_t *, const unsigned long, const struct sockaddr *);
969 int if_flags_set(struct ifnet *, const short);
970 int if_clone_list(int, char *, int *);
971
972 struct ifnet *ifunit(const char *);
973 struct ifnet *if_get(const char *, struct psref *);
974 ifnet_t *if_byindex(u_int);
975 ifnet_t *if_get_byindex(u_int, struct psref *);
976 ifnet_t *if_get_bylla(const void *, unsigned char, struct psref *);
977 void if_put(const struct ifnet *, struct psref *);
978 void if_acquire(struct ifnet *, struct psref *);
979 #define if_release if_put
980
981 static inline if_index_t
982 if_get_index(const struct ifnet *ifp)
983 {
984
985 return ifp != NULL ? ifp->if_index : 0;
986 }
987
988 bool if_held(struct ifnet *);
989
990 void if_input(struct ifnet *, struct mbuf *);
991
992 struct if_percpuq *
993 if_percpuq_create(struct ifnet *);
994 void if_percpuq_destroy(struct if_percpuq *);
995 void
996 if_percpuq_enqueue(struct if_percpuq *, struct mbuf *);
997
998 void if_deferred_start_init(struct ifnet *, void (*)(struct ifnet *));
999 void if_schedule_deferred_start(struct ifnet *);
1000
1001 void ifa_insert(struct ifnet *, struct ifaddr *);
1002 void ifa_remove(struct ifnet *, struct ifaddr *);
1003
1004 void ifa_psref_init(struct ifaddr *);
1005 void ifa_acquire(struct ifaddr *, struct psref *);
1006 void ifa_release(struct ifaddr *, struct psref *);
1007 bool ifa_held(struct ifaddr *);
1008 bool ifa_is_destroying(struct ifaddr *);
1009
1010 void ifaref(struct ifaddr *);
1011 void ifafree(struct ifaddr *);
1012
1013 struct ifaddr *ifa_ifwithaddr(const struct sockaddr *);
1014 struct ifaddr *ifa_ifwithaddr_psref(const struct sockaddr *, struct psref *);
1015 struct ifaddr *ifa_ifwithaf(int);
1016 struct ifaddr *ifa_ifwithdstaddr(const struct sockaddr *);
1017 struct ifaddr *ifa_ifwithdstaddr_psref(const struct sockaddr *,
1018 struct psref *);
1019 struct ifaddr *ifa_ifwithnet(const struct sockaddr *);
1020 struct ifaddr *ifa_ifwithnet_psref(const struct sockaddr *, struct psref *);
1021 struct ifaddr *ifa_ifwithladdr(const struct sockaddr *);
1022 struct ifaddr *ifa_ifwithladdr_psref(const struct sockaddr *, struct psref *);
1023 struct ifaddr *ifa_ifwithroute_psref(int, const struct sockaddr *,
1024 const struct sockaddr *, struct psref *);
1025 struct ifaddr *ifaof_ifpforaddr(const struct sockaddr *, struct ifnet *);
1026 struct ifaddr *ifaof_ifpforaddr_psref(const struct sockaddr *, struct ifnet *,
1027 struct psref *);
1028 void link_rtrequest(int, struct rtentry *, const struct rt_addrinfo *);
1029 void p2p_rtrequest(int, struct rtentry *, const struct rt_addrinfo *);
1030
1031 void if_clone_attach(struct if_clone *);
1032 void if_clone_detach(struct if_clone *);
1033
1034 int if_transmit_lock(struct ifnet *, struct mbuf *);
1035
1036 int ifq_enqueue(struct ifnet *, struct mbuf *);
1037 int ifq_enqueue2(struct ifnet *, struct ifqueue *, struct mbuf *);
1038
1039 int loioctl(struct ifnet *, u_long, void *);
1040 void loopattach(int);
1041 void loopinit(void);
1042 int looutput(struct ifnet *,
1043 struct mbuf *, const struct sockaddr *, const struct rtentry *);
1044
1045 /*
1046 * These are exported because they're an easy way to tell if
1047 * an interface is going away without having to burn a flag.
1048 */
1049 int if_nulloutput(struct ifnet *, struct mbuf *,
1050 const struct sockaddr *, const struct rtentry *);
1051 void if_nullinput(struct ifnet *, struct mbuf *);
1052 void if_nullstart(struct ifnet *);
1053 int if_nulltransmit(struct ifnet *, struct mbuf *);
1054 int if_nullioctl(struct ifnet *, u_long, void *);
1055 int if_nullinit(struct ifnet *);
1056 void if_nullstop(struct ifnet *, int);
1057 void if_nullslowtimo(struct ifnet *);
1058 #define if_nullwatchdog if_nullslowtimo
1059 void if_nulldrain(struct ifnet *);
1060 #else
1061 struct if_nameindex {
1062 unsigned int if_index; /* 1, 2, ... */
1063 char *if_name; /* null terminated name: "le0", ... */
1064 };
1065
1066 #include <sys/cdefs.h>
1067 __BEGIN_DECLS
1068 unsigned int if_nametoindex(const char *);
1069 char * if_indextoname(unsigned int, char *);
1070 struct if_nameindex * if_nameindex(void);
1071 void if_freenameindex(struct if_nameindex *);
1072 __END_DECLS
1073 #endif /* _KERNEL */ /* XXX really ALTQ? */
1074
1075 #ifdef _KERNEL
1076
1077 #define IFADDR_FIRST(__ifp) TAILQ_FIRST(&(__ifp)->if_addrlist)
1078 #define IFADDR_NEXT(__ifa) TAILQ_NEXT((__ifa), ifa_list)
1079 #define IFADDR_FOREACH(__ifa, __ifp) TAILQ_FOREACH(__ifa, \
1080 &(__ifp)->if_addrlist, ifa_list)
1081 #define IFADDR_FOREACH_SAFE(__ifa, __ifp, __nifa) \
1082 TAILQ_FOREACH_SAFE(__ifa, \
1083 &(__ifp)->if_addrlist, ifa_list, __nifa)
1084 #define IFADDR_EMPTY(__ifp) TAILQ_EMPTY(&(__ifp)->if_addrlist)
1085
1086 #define IFADDR_ENTRY_INIT(__ifa) \
1087 PSLIST_ENTRY_INIT((__ifa), ifa_pslist_entry)
1088 #define IFADDR_ENTRY_DESTROY(__ifa) \
1089 PSLIST_ENTRY_DESTROY((__ifa), ifa_pslist_entry)
1090 #define IFADDR_READER_EMPTY(__ifp) \
1091 (PSLIST_READER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr, \
1092 ifa_pslist_entry) == NULL)
1093 #define IFADDR_READER_FIRST(__ifp) \
1094 PSLIST_READER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr, \
1095 ifa_pslist_entry)
1096 #define IFADDR_READER_NEXT(__ifa) \
1097 PSLIST_READER_NEXT((__ifa), struct ifaddr, ifa_pslist_entry)
1098 #define IFADDR_READER_FOREACH(__ifa, __ifp) \
1099 PSLIST_READER_FOREACH((__ifa), &(__ifp)->if_addr_pslist, struct ifaddr,\
1100 ifa_pslist_entry)
1101 #define IFADDR_WRITER_INSERT_HEAD(__ifp, __ifa) \
1102 PSLIST_WRITER_INSERT_HEAD(&(__ifp)->if_addr_pslist, (__ifa), \
1103 ifa_pslist_entry)
1104 #define IFADDR_WRITER_REMOVE(__ifa) \
1105 PSLIST_WRITER_REMOVE((__ifa), ifa_pslist_entry)
1106 #define IFADDR_WRITER_FOREACH(__ifa, __ifp) \
1107 PSLIST_WRITER_FOREACH((__ifa), &(__ifp)->if_addr_pslist, struct ifaddr,\
1108 ifa_pslist_entry)
1109 #define IFADDR_WRITER_NEXT(__ifp) \
1110 PSLIST_WRITER_NEXT((__ifp), struct ifaddr, ifa_pslist_entry)
1111 #define IFADDR_WRITER_INSERT_AFTER(__ifp, __new) \
1112 PSLIST_WRITER_INSERT_AFTER((__ifp), (__new), ifa_pslist_entry)
1113 #define IFADDR_WRITER_EMPTY(__ifp) \
1114 (PSLIST_WRITER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr, \
1115 ifa_pslist_entry) == NULL)
1116 #define IFADDR_WRITER_INSERT_TAIL(__ifp, __new) \
1117 do { \
1118 if (IFADDR_WRITER_EMPTY((__ifp))) { \
1119 IFADDR_WRITER_INSERT_HEAD((__ifp), (__new)); \
1120 } else { \
1121 struct ifaddr *__ifa; \
1122 IFADDR_WRITER_FOREACH(__ifa, (__ifp)) { \
1123 if (IFADDR_WRITER_NEXT(__ifa) == NULL) {\
1124 IFADDR_WRITER_INSERT_AFTER(__ifa,\
1125 (__new)); \
1126 break; \
1127 } \
1128 } \
1129 } \
1130 } while (0)
1131
1132 #define IFNET_LOCK() mutex_enter(&ifnet_mtx)
1133 #define IFNET_UNLOCK() mutex_exit(&ifnet_mtx)
1134 #define IFNET_LOCKED() mutex_owned(&ifnet_mtx)
1135
1136 #define IFNET_READER_EMPTY() \
1137 (PSLIST_READER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry) == NULL)
1138 #define IFNET_READER_FIRST() \
1139 PSLIST_READER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry)
1140 #define IFNET_READER_NEXT(__ifp) \
1141 PSLIST_READER_NEXT((__ifp), struct ifnet, if_pslist_entry)
1142 #define IFNET_READER_FOREACH(__ifp) \
1143 PSLIST_READER_FOREACH((__ifp), &ifnet_pslist, struct ifnet, \
1144 if_pslist_entry)
1145 #define IFNET_WRITER_INSERT_HEAD(__ifp) \
1146 PSLIST_WRITER_INSERT_HEAD(&ifnet_pslist, (__ifp), if_pslist_entry)
1147 #define IFNET_WRITER_REMOVE(__ifp) \
1148 PSLIST_WRITER_REMOVE((__ifp), if_pslist_entry)
1149 #define IFNET_WRITER_FOREACH(__ifp) \
1150 PSLIST_WRITER_FOREACH((__ifp), &ifnet_pslist, struct ifnet, \
1151 if_pslist_entry)
1152 #define IFNET_WRITER_NEXT(__ifp) \
1153 PSLIST_WRITER_NEXT((__ifp), struct ifnet, if_pslist_entry)
1154 #define IFNET_WRITER_INSERT_AFTER(__ifp, __new) \
1155 PSLIST_WRITER_INSERT_AFTER((__ifp), (__new), if_pslist_entry)
1156 #define IFNET_WRITER_EMPTY() \
1157 (PSLIST_WRITER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry) == NULL)
1158 #define IFNET_WRITER_INSERT_TAIL(__new) \
1159 do { \
1160 if (IFNET_WRITER_EMPTY()) { \
1161 IFNET_WRITER_INSERT_HEAD((__new)); \
1162 } else { \
1163 struct ifnet *__ifp; \
1164 IFNET_WRITER_FOREACH(__ifp) { \
1165 if (IFNET_WRITER_NEXT(__ifp) == NULL) { \
1166 IFNET_WRITER_INSERT_AFTER(__ifp,\
1167 (__new)); \
1168 break; \
1169 } \
1170 } \
1171 } \
1172 } while (0)
1173
1174 extern struct pslist_head ifnet_pslist;
1175 extern kmutex_t ifnet_mtx;
1176
1177 extern struct ifnet *lo0ifp;
1178
1179 /*
1180 * ifq sysctl support
1181 */
1182 int sysctl_ifq(int *name, u_int namelen, void *oldp,
1183 size_t *oldlenp, void *newp, size_t newlen,
1184 struct ifqueue *ifq);
1185 /* symbolic names for terminal (per-protocol) CTL_IFQ_ nodes */
1186 #define IFQCTL_LEN 1
1187 #define IFQCTL_MAXLEN 2
1188 #define IFQCTL_PEAK 3
1189 #define IFQCTL_DROPS 4
1190 #define IFQCTL_MAXID 5
1191
1192 #endif /* _KERNEL */
1193
1194 #ifdef _NETBSD_SOURCE
1195 /*
1196 * sysctl for ifq (per-protocol packet input queue variant of ifqueue)
1197 */
1198 #define CTL_IFQ_NAMES { \
1199 { 0, 0 }, \
1200 { "len", CTLTYPE_INT }, \
1201 { "maxlen", CTLTYPE_INT }, \
1202 { "peak", CTLTYPE_INT }, \
1203 { "drops", CTLTYPE_INT }, \
1204 }
1205 #endif /* _NETBSD_SOURCE */
1206 #endif /* !_NET_IF_H_ */
1207