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