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