if.h revision 1.275 1 /* $NetBSD: if.h,v 1.275 2019/08/10 21:13:54 rmind 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 void *if_npf_private;/* ?: associated NPF context */
344
345 /*
346 * pf specific data, used only when #if NPF > 0.
347 */
348 void *if_pf_kif; /* ?: pf interface abstraction */
349 void *if_pf_groups; /* ?: pf interface groups */
350 /*
351 * During an ifnet's lifetime, it has only one if_index, but
352 * and if_index is not sufficient to identify an ifnet
353 * because during the lifetime of the system, many ifnets may occupy a
354 * given if_index. Let us tell different ifnets at the same
355 * if_index apart by their if_index_gen, a unique number that each ifnet
356 * is assigned when it if_attach()s. Now, the kernel can use the
357 * pair (if_index, if_index_gen) as a weak reference to an ifnet.
358 */
359 uint64_t if_index_gen; /* :: generation number for the ifnet
360 * at if_index: if two ifnets' index
361 * and generation number are both the
362 * same, they are the same ifnet.
363 */
364 struct sysctllog
365 *if_sysctl_log; /* :: */
366 int (*if_initaddr) /* :: */
367 (struct ifnet *, struct ifaddr *, bool);
368 int (*if_mcastop) /* :: */
369 (struct ifnet *, const unsigned long,
370 const struct sockaddr *);
371 int (*if_setflags) /* :: */
372 (struct ifnet *, const short);
373 kmutex_t *if_ioctl_lock; /* :: */
374 char *if_description; /* i: interface description */
375 #ifdef _KERNEL /* XXX kvm(3) */
376 struct callout *if_slowtimo_ch;/* :: */
377 struct krwlock *if_afdata_lock;/* :: */
378 struct if_percpuq
379 *if_percpuq; /* :: we should remove it in the future */
380 void *if_link_si; /* :: softint to handle link state changes */
381 uint16_t if_link_queue; /* q: masked link state change queue */
382 struct pslist_entry
383 if_pslist_entry;/* i: */
384 struct psref_target
385 if_psref; /* :: */
386 struct pslist_head
387 if_addr_pslist; /* i: */
388 struct if_deferred_start
389 *if_deferred_start;
390 /* :: */
391 /* XXX should be protocol independent */
392 LIST_HEAD(, in6_multi)
393 if_multiaddrs; /* 6: */
394 #endif
395 } ifnet_t;
396
397 #define if_mtu if_data.ifi_mtu
398 #define if_type if_data.ifi_type
399 #define if_addrlen if_data.ifi_addrlen
400 #define if_hdrlen if_data.ifi_hdrlen
401 #define if_metric if_data.ifi_metric
402 #define if_link_state if_data.ifi_link_state
403 #define if_baudrate if_data.ifi_baudrate
404 #define if_ipackets if_data.ifi_ipackets
405 #define if_ierrors if_data.ifi_ierrors
406 #define if_opackets if_data.ifi_opackets
407 #define if_oerrors if_data.ifi_oerrors
408 #define if_collisions if_data.ifi_collisions
409 #define if_ibytes if_data.ifi_ibytes
410 #define if_obytes if_data.ifi_obytes
411 #define if_imcasts if_data.ifi_imcasts
412 #define if_omcasts if_data.ifi_omcasts
413 #define if_iqdrops if_data.ifi_iqdrops
414 #define if_noproto if_data.ifi_noproto
415 #define if_lastchange if_data.ifi_lastchange
416 #define if_name(ifp) ((ifp)->if_xname)
417
418 #define IFF_UP 0x0001 /* interface is up */
419 #define IFF_BROADCAST 0x0002 /* broadcast address valid */
420 #define IFF_DEBUG 0x0004 /* turn on debugging */
421 #define IFF_LOOPBACK 0x0008 /* is a loopback net */
422 #define IFF_POINTOPOINT 0x0010 /* interface is point-to-point link */
423 /* 0x0020 was IFF_NOTRAILERS */
424 #define IFF_RUNNING 0x0040 /* resources allocated */
425 #define IFF_NOARP 0x0080 /* no address resolution protocol */
426 #define IFF_PROMISC 0x0100 /* receive all packets */
427 #define IFF_ALLMULTI 0x0200 /* receive all multicast packets */
428 #define IFF_OACTIVE 0x0400 /* transmission in progress */
429 #define IFF_SIMPLEX 0x0800 /* can't hear own transmissions */
430 #define IFF_LINK0 0x1000 /* per link layer defined bit */
431 #define IFF_LINK1 0x2000 /* per link layer defined bit */
432 #define IFF_LINK2 0x4000 /* per link layer defined bit */
433 #define IFF_MULTICAST 0x8000 /* supports multicast */
434
435 #define IFEF_MPSAFE __BIT(0) /* handlers can run in parallel (see below) */
436 #define IFEF_NO_LINK_STATE_CHANGE __BIT(1) /* doesn't use link state interrupts */
437
438 /*
439 * The guidelines for converting an interface to IFEF_MPSAFE are as follows
440 *
441 * Enabling IFEF_MPSAFE on an interface suppresses taking KERNEL_LOCK when
442 * calling the following handlers:
443 * - if_start
444 * - Note that if_transmit is always called without KERNEL_LOCK
445 * - if_output
446 * - if_ioctl
447 * - if_init
448 * - if_stop
449 *
450 * This means that an interface with IFEF_MPSAFE must make the above handlers
451 * MP-safe or take KERNEL_LOCK by itself inside handlers that aren't MP-safe
452 * yet.
453 *
454 * There are some additional restrictions to access member variables of struct
455 * ifnet:
456 * - if_flags
457 * - Must be updated with holding IFNET_LOCK
458 * - You cannot use the flag in Tx/Rx paths anymore because there is no
459 * synchronization on the flag except for IFNET_LOCK
460 * - Note that IFNET_LOCK can't be taken in softint because it's known
461 * that it causes a deadlock
462 * - Some synchronization mechanisms such as pserialize_perform are called
463 * with IFNET_LOCK and also require context switches on every CPUs
464 * that mean softints finish so trying to take IFNET_LOCK in softint
465 * might block on IFNET_LOCK and prevent such synchronization mechanisms
466 * from being completed
467 * - Currently the deadlock occurs only if NET_MPSAFE is enabled, however,
468 * we should deal with the restriction because NET_MPSAFE will be enabled
469 * by default in the future
470 * - if_watchdog and if_timer
471 * - The watchdog framework works only for non-IFEF_MPSAFE interfaces
472 * that rely on KERNEL_LOCK
473 * - Interfaces with IFEF_MPSAFE have to provide its own watchdog mechanism
474 * if needed
475 * - Keep if_watchdog NULL when calling if_attach
476 */
477
478 #ifdef _KERNEL
479 static __inline bool
480 if_is_mpsafe(struct ifnet *ifp)
481 {
482
483 return ((ifp->if_extflags & IFEF_MPSAFE) != 0);
484 }
485
486 static __inline int
487 if_output_lock(struct ifnet *cifp, struct ifnet *ifp, struct mbuf *m,
488 const struct sockaddr *dst, const struct rtentry *rt)
489 {
490
491 if (if_is_mpsafe(cifp)) {
492 return (*cifp->if_output)(ifp, m, dst, rt);
493 } else {
494 int ret;
495
496 KERNEL_LOCK(1, NULL);
497 ret = (*cifp->if_output)(ifp, m, dst, rt);
498 KERNEL_UNLOCK_ONE(NULL);
499 return ret;
500 }
501 }
502
503 static __inline void
504 if_start_lock(struct ifnet *ifp)
505 {
506
507 if (if_is_mpsafe(ifp)) {
508 (*ifp->if_start)(ifp);
509 } else {
510 KERNEL_LOCK(1, NULL);
511 (*ifp->if_start)(ifp);
512 KERNEL_UNLOCK_ONE(NULL);
513 }
514 }
515
516 static __inline bool
517 if_is_link_state_changeable(struct ifnet *ifp)
518 {
519
520 return ((ifp->if_extflags & IFEF_NO_LINK_STATE_CHANGE) == 0);
521 }
522
523 #define KERNEL_LOCK_IF_IFP_MPSAFE(ifp) \
524 do { if (if_is_mpsafe(ifp)) { KERNEL_LOCK(1, NULL); } } while (0)
525 #define KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp) \
526 do { if (if_is_mpsafe(ifp)) { KERNEL_UNLOCK_ONE(NULL); } } while (0)
527
528 #define KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp) \
529 do { if (!if_is_mpsafe(ifp)) { KERNEL_LOCK(1, NULL); } } while (0)
530 #define KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp) \
531 do { if (!if_is_mpsafe(ifp)) { KERNEL_UNLOCK_ONE(NULL); } } while (0)
532
533 #ifdef _KERNEL_OPT
534 #include "opt_net_mpsafe.h"
535 #endif
536
537 /* XXX explore a better place to define */
538 #ifdef NET_MPSAFE
539
540 #define KERNEL_LOCK_UNLESS_NET_MPSAFE() do { } while (0)
541 #define KERNEL_UNLOCK_UNLESS_NET_MPSAFE() do { } while (0)
542
543 #define SOFTNET_LOCK_UNLESS_NET_MPSAFE() do { } while (0)
544 #define SOFTNET_UNLOCK_UNLESS_NET_MPSAFE() do { } while (0)
545
546 #define SOFTNET_LOCK_IF_NET_MPSAFE() \
547 do { mutex_enter(softnet_lock); } while (0)
548 #define SOFTNET_UNLOCK_IF_NET_MPSAFE() \
549 do { mutex_exit(softnet_lock); } while (0)
550
551 #else /* NET_MPSAFE */
552
553 #define KERNEL_LOCK_UNLESS_NET_MPSAFE() \
554 do { KERNEL_LOCK(1, NULL); } while (0)
555 #define KERNEL_UNLOCK_UNLESS_NET_MPSAFE() \
556 do { KERNEL_UNLOCK_ONE(NULL); } while (0)
557
558 #define SOFTNET_LOCK_UNLESS_NET_MPSAFE() \
559 do { mutex_enter(softnet_lock); } while (0)
560 #define SOFTNET_UNLOCK_UNLESS_NET_MPSAFE() \
561 do { mutex_exit(softnet_lock); } while (0)
562
563 #define SOFTNET_LOCK_IF_NET_MPSAFE() do { } while (0)
564 #define SOFTNET_UNLOCK_IF_NET_MPSAFE() do { } while (0)
565
566 #endif /* NET_MPSAFE */
567
568 #define SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE() \
569 do { \
570 SOFTNET_LOCK_UNLESS_NET_MPSAFE(); \
571 KERNEL_LOCK_UNLESS_NET_MPSAFE(); \
572 } while (0)
573
574 #define SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE() \
575 do { \
576 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); \
577 SOFTNET_UNLOCK_UNLESS_NET_MPSAFE(); \
578 } while (0)
579
580 #endif /* _KERNEL */
581
582 #define IFFBITS \
583 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT" \
584 "\7RUNNING\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX" \
585 "\15LINK0\16LINK1\17LINK2\20MULTICAST"
586
587 /* flags set internally only: */
588 #define IFF_CANTCHANGE \
589 (IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\
590 IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI|IFF_PROMISC)
591
592 /*
593 * Some convenience macros used for setting ifi_baudrate.
594 */
595 #define IF_Kbps(x) ((x) * 1000ULL) /* kilobits/sec. */
596 #define IF_Mbps(x) (IF_Kbps((x) * 1000ULL)) /* megabits/sec. */
597 #define IF_Gbps(x) (IF_Mbps((x) * 1000ULL)) /* gigabits/sec. */
598
599 /* Capabilities that interfaces can advertise. */
600 /* 0x01 .. 0x40 were previously used */
601 #define IFCAP_TSOv4 0x00080 /* can do TCPv4 segmentation offload */
602 #define IFCAP_CSUM_IPv4_Rx 0x00100 /* can do IPv4 header checksums (Rx) */
603 #define IFCAP_CSUM_IPv4_Tx 0x00200 /* can do IPv4 header checksums (Tx) */
604 #define IFCAP_CSUM_TCPv4_Rx 0x00400 /* can do IPv4/TCP checksums (Rx) */
605 #define IFCAP_CSUM_TCPv4_Tx 0x00800 /* can do IPv4/TCP checksums (Tx) */
606 #define IFCAP_CSUM_UDPv4_Rx 0x01000 /* can do IPv4/UDP checksums (Rx) */
607 #define IFCAP_CSUM_UDPv4_Tx 0x02000 /* can do IPv4/UDP checksums (Tx) */
608 #define IFCAP_CSUM_TCPv6_Rx 0x04000 /* can do IPv6/TCP checksums (Rx) */
609 #define IFCAP_CSUM_TCPv6_Tx 0x08000 /* can do IPv6/TCP checksums (Tx) */
610 #define IFCAP_CSUM_UDPv6_Rx 0x10000 /* can do IPv6/UDP checksums (Rx) */
611 #define IFCAP_CSUM_UDPv6_Tx 0x20000 /* can do IPv6/UDP checksums (Tx) */
612 #define IFCAP_TSOv6 0x40000 /* can do TCPv6 segmentation offload */
613 #define IFCAP_LRO 0x80000 /* can do Large Receive Offload */
614 #define IFCAP_MASK 0xfff80 /* currently valid capabilities */
615
616 #define IFCAPBITS \
617 "\020" \
618 "\10TSO4" \
619 "\11IP4CSUM_Rx" \
620 "\12IP4CSUM_Tx" \
621 "\13TCP4CSUM_Rx" \
622 "\14TCP4CSUM_Tx" \
623 "\15UDP4CSUM_Rx" \
624 "\16UDP4CSUM_Tx" \
625 "\17TCP6CSUM_Rx" \
626 "\20TCP6CSUM_Tx" \
627 "\21UDP6CSUM_Rx" \
628 "\22UDP6CSUM_Tx" \
629 "\23TSO6" \
630 "\24LRO" \
631
632 #define IF_AFDATA_LOCK_INIT(ifp) \
633 do {(ifp)->if_afdata_lock = rw_obj_alloc();} while (0)
634
635 #define IF_AFDATA_LOCK_DESTROY(ifp) rw_obj_free((ifp)->if_afdata_lock)
636
637 #define IF_AFDATA_WLOCK(ifp) rw_enter((ifp)->if_afdata_lock, RW_WRITER)
638 #define IF_AFDATA_RLOCK(ifp) rw_enter((ifp)->if_afdata_lock, RW_READER)
639 #define IF_AFDATA_WUNLOCK(ifp) rw_exit((ifp)->if_afdata_lock)
640 #define IF_AFDATA_RUNLOCK(ifp) rw_exit((ifp)->if_afdata_lock)
641 #define IF_AFDATA_LOCK(ifp) IF_AFDATA_WLOCK(ifp)
642 #define IF_AFDATA_UNLOCK(ifp) IF_AFDATA_WUNLOCK(ifp)
643 #define IF_AFDATA_TRYLOCK(ifp) rw_tryenter((ifp)->if_afdata_lock, RW_WRITER)
644
645 #define IF_AFDATA_LOCK_ASSERT(ifp) \
646 KASSERT(rw_lock_held((ifp)->if_afdata_lock))
647 #define IF_AFDATA_RLOCK_ASSERT(ifp) \
648 KASSERT(rw_read_held((ifp)->if_afdata_lock))
649 #define IF_AFDATA_WLOCK_ASSERT(ifp) \
650 KASSERT(rw_write_held((ifp)->if_afdata_lock))
651
652 /*
653 * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
654 * input routines have queues of messages stored on ifqueue structures
655 * (defined above). Entries are added to and deleted from these structures
656 * by these macros, which should be called with ipl raised to splnet().
657 */
658 #define IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen)
659 #define IF_DROP(ifq) ((ifq)->ifq_drops++)
660 #define IF_ENQUEUE(ifq, m) do { \
661 (m)->m_nextpkt = 0; \
662 if ((ifq)->ifq_tail == 0) \
663 (ifq)->ifq_head = m; \
664 else \
665 (ifq)->ifq_tail->m_nextpkt = m; \
666 (ifq)->ifq_tail = m; \
667 (ifq)->ifq_len++; \
668 } while (/*CONSTCOND*/0)
669 #define IF_PREPEND(ifq, m) do { \
670 (m)->m_nextpkt = (ifq)->ifq_head; \
671 if ((ifq)->ifq_tail == 0) \
672 (ifq)->ifq_tail = (m); \
673 (ifq)->ifq_head = (m); \
674 (ifq)->ifq_len++; \
675 } while (/*CONSTCOND*/0)
676 #define IF_DEQUEUE(ifq, m) do { \
677 (m) = (ifq)->ifq_head; \
678 if (m) { \
679 if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \
680 (ifq)->ifq_tail = 0; \
681 (m)->m_nextpkt = 0; \
682 (ifq)->ifq_len--; \
683 } \
684 } while (/*CONSTCOND*/0)
685 #define IF_POLL(ifq, m) ((m) = (ifq)->ifq_head)
686 #define IF_PURGE(ifq) \
687 do { \
688 struct mbuf *__m0; \
689 \
690 for (;;) { \
691 IF_DEQUEUE((ifq), __m0); \
692 if (__m0 == NULL) \
693 break; \
694 else \
695 m_freem(__m0); \
696 } \
697 } while (/*CONSTCOND*/ 0)
698 #define IF_IS_EMPTY(ifq) ((ifq)->ifq_len == 0)
699
700 #ifndef IFQ_MAXLEN
701 #define IFQ_MAXLEN 256
702 #endif
703 #define IFNET_SLOWHZ 1 /* granularity is 1 second */
704
705 /*
706 * Structure defining statistics and other data kept regarding an address
707 * on a network interface.
708 */
709 struct ifaddr_data {
710 int64_t ifad_inbytes;
711 int64_t ifad_outbytes;
712 };
713
714 /*
715 * The ifaddr structure contains information about one address
716 * of an interface. They are maintained by the different address families,
717 * are allocated and attached when an address is set, and are linked
718 * together so all addresses for an interface can be located.
719 */
720 struct ifaddr {
721 struct sockaddr *ifa_addr; /* address of interface */
722 struct sockaddr *ifa_dstaddr; /* other end of p-to-p link */
723 #define ifa_broadaddr ifa_dstaddr /* broadcast address interface */
724 struct sockaddr *ifa_netmask; /* used to determine subnet */
725 struct ifnet *ifa_ifp; /* back-pointer to interface */
726 TAILQ_ENTRY(ifaddr) ifa_list; /* list of addresses for interface */
727 struct ifaddr_data ifa_data; /* statistics on the address */
728 void (*ifa_rtrequest) /* check or clean routes (+ or -)'d */
729 (int, struct rtentry *, const struct rt_addrinfo *);
730 u_int ifa_flags; /* mostly rt_flags for cloning */
731 int ifa_refcnt; /* count of references */
732 int ifa_metric; /* cost of going out this interface */
733 struct ifaddr *(*ifa_getifa)(struct ifaddr *,
734 const struct sockaddr *);
735 uint32_t *ifa_seqno;
736 int16_t ifa_preference; /* preference level for this address */
737 #ifdef _KERNEL
738 struct pslist_entry ifa_pslist_entry;
739 struct psref_target ifa_psref;
740 #endif
741 };
742 #define IFA_ROUTE RTF_UP /* (0x01) route installed */
743 #define IFA_DESTROYING 0x2
744
745 /*
746 * Message format for use in obtaining information about interfaces from
747 * sysctl and the routing socket. We need to force 64-bit alignment if we
748 * aren't using compatiblity definitons.
749 */
750 #if !defined(_KERNEL) || !defined(COMPAT_RTSOCK)
751 #define __align64 __aligned(sizeof(uint64_t))
752 #else
753 #define __align64
754 #endif
755 struct if_msghdr {
756 u_short ifm_msglen __align64;
757 /* to skip over non-understood messages */
758 u_char ifm_version; /* future binary compatibility */
759 u_char ifm_type; /* message type */
760 int ifm_addrs; /* like rtm_addrs */
761 int ifm_flags; /* value of if_flags */
762 u_short ifm_index; /* index for associated ifp */
763 struct if_data ifm_data __align64;
764 /* statistics and other data about if */
765 };
766
767 /*
768 * Message format for use in obtaining information about interface addresses
769 * from sysctl and the routing socket.
770 */
771 struct ifa_msghdr {
772 u_short ifam_msglen __align64;
773 /* to skip over non-understood messages */
774 u_char ifam_version; /* future binary compatibility */
775 u_char ifam_type; /* message type */
776 u_short ifam_index; /* index for associated ifp */
777 int ifam_flags; /* value of ifa_flags */
778 int ifam_addrs; /* like rtm_addrs */
779 pid_t ifam_pid; /* identify sender */
780 int ifam_addrflags; /* family specific address flags */
781 int ifam_metric; /* value of ifa_metric */
782 };
783
784 /*
785 * Message format announcing the arrival or departure of a network interface.
786 */
787 struct if_announcemsghdr {
788 u_short ifan_msglen __align64;
789 /* to skip over non-understood messages */
790 u_char ifan_version; /* future binary compatibility */
791 u_char ifan_type; /* message type */
792 u_short ifan_index; /* index for associated ifp */
793 char ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */
794 u_short ifan_what; /* what type of announcement */
795 };
796
797 #define IFAN_ARRIVAL 0 /* interface arrival */
798 #define IFAN_DEPARTURE 1 /* interface departure */
799
800 #undef __align64
801
802 /*
803 * Interface request structure used for socket
804 * ioctl's. All interface ioctl's must have parameter
805 * definitions which begin with ifr_name. The
806 * remainder may be interface specific.
807 */
808 struct ifreq {
809 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
810 union {
811 struct sockaddr ifru_addr;
812 struct sockaddr ifru_dstaddr;
813 struct sockaddr ifru_broadaddr;
814 struct sockaddr_storage ifru_space;
815 short ifru_flags;
816 int ifru_addrflags;
817 int ifru_metric;
818 int ifru_mtu;
819 int ifru_dlt;
820 u_int ifru_value;
821 void * ifru_data;
822 struct {
823 uint32_t b_buflen;
824 void *b_buf;
825 } ifru_b;
826 } ifr_ifru;
827 #define ifr_addr ifr_ifru.ifru_addr /* address */
828 #define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */
829 #define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
830 #define ifr_space ifr_ifru.ifru_space /* sockaddr_storage */
831 #define ifr_flags ifr_ifru.ifru_flags /* flags */
832 #define ifr_addrflags ifr_ifru.ifru_addrflags /* addr flags */
833 #define ifr_metric ifr_ifru.ifru_metric /* metric */
834 #define ifr_mtu ifr_ifru.ifru_mtu /* mtu */
835 #define ifr_dlt ifr_ifru.ifru_dlt /* data link type (DLT_*) */
836 #define ifr_value ifr_ifru.ifru_value /* generic value */
837 #define ifr_media ifr_ifru.ifru_metric /* media options (overload) */
838 #define ifr_data ifr_ifru.ifru_data /* for use by interface
839 * XXX deprecated
840 */
841 #define ifr_buf ifr_ifru.ifru_b.b_buf /* new interface ioctls */
842 #define ifr_buflen ifr_ifru.ifru_b.b_buflen
843 #define ifr_index ifr_ifru.ifru_value /* interface index, BSD */
844 #define ifr_ifindex ifr_index /* interface index, linux */
845 };
846
847 #ifdef _KERNEL
848 #define ifreq_setdstaddr ifreq_setaddr
849 #define ifreq_setbroadaddr ifreq_setaddr
850 #define ifreq_getdstaddr ifreq_getaddr
851 #define ifreq_getbroadaddr ifreq_getaddr
852
853 static __inline const struct sockaddr *
854 /*ARGSUSED*/
855 ifreq_getaddr(u_long cmd, const struct ifreq *ifr)
856 {
857 return &ifr->ifr_addr;
858 }
859 #endif /* _KERNEL */
860
861 struct ifcapreq {
862 char ifcr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
863 uint64_t ifcr_capabilities; /* supported capabiliites */
864 uint64_t ifcr_capenable; /* capabilities enabled */
865 };
866
867 struct ifaliasreq {
868 char ifra_name[IFNAMSIZ]; /* if name, e.g. "en0" */
869 struct sockaddr ifra_addr;
870 struct sockaddr ifra_dstaddr;
871 #define ifra_broadaddr ifra_dstaddr
872 struct sockaddr ifra_mask;
873 };
874
875 struct ifdatareq {
876 char ifdr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
877 struct if_data ifdr_data;
878 };
879
880 struct ifmediareq {
881 char ifm_name[IFNAMSIZ]; /* if name, e.g. "en0" */
882 int ifm_current; /* IFMWD: current media options */
883 int ifm_mask; /* IFMWD: don't care mask */
884 int ifm_status; /* media status */
885 int ifm_active; /* IFMWD: active options */
886 int ifm_count; /* # entries in ifm_ulist
887 array */
888 int *ifm_ulist; /* array of ifmedia word */
889 };
890
891
892 struct ifdrv {
893 char ifd_name[IFNAMSIZ]; /* if name, e.g. "en0" */
894 unsigned long ifd_cmd;
895 size_t ifd_len;
896 void *ifd_data;
897 };
898 #define IFLINKSTR_QUERYLEN 0x01
899 #define IFLINKSTR_UNSET 0x02
900
901 /*
902 * Structure used in SIOCGIFCONF request.
903 * Used to retrieve interface configuration
904 * for machine (useful for programs which
905 * must know all networks accessible).
906 */
907 struct ifconf {
908 int ifc_len; /* size of associated buffer */
909 union {
910 void * ifcu_buf;
911 struct ifreq *ifcu_req;
912 } ifc_ifcu;
913 #define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */
914 #define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */
915 };
916
917 /*
918 * Structure for SIOC[AGD]LIFADDR
919 */
920 struct if_laddrreq {
921 char iflr_name[IFNAMSIZ];
922 unsigned int flags;
923 #define IFLR_PREFIX 0x8000 /* in: prefix given out: kernel fills id */
924 #define IFLR_ACTIVE 0x4000 /* in/out: link-layer address activation */
925 #define IFLR_FACTORY 0x2000 /* in/out: factory link-layer address */
926 unsigned int prefixlen; /* in/out */
927 struct sockaddr_storage addr; /* in/out */
928 struct sockaddr_storage dstaddr; /* out */
929 };
930
931 /*
932 * Structure for SIOC[SG]IFADDRPREF
933 */
934 struct if_addrprefreq {
935 char ifap_name[IFNAMSIZ];
936 int16_t ifap_preference; /* in/out */
937 struct sockaddr_storage ifap_addr; /* in/out */
938 };
939
940 #include <net/if_arp.h>
941
942 #endif /* _NETBSD_SOURCE */
943
944 #ifdef _KERNEL
945 #ifdef ALTQ
946 #define IFQ_ENQUEUE(ifq, m, err) \
947 do { \
948 mutex_enter((ifq)->ifq_lock); \
949 if (ALTQ_IS_ENABLED(ifq)) \
950 ALTQ_ENQUEUE((ifq), (m), (err)); \
951 else { \
952 if (IF_QFULL(ifq)) { \
953 m_freem(m); \
954 (err) = ENOBUFS; \
955 } else { \
956 IF_ENQUEUE((ifq), (m)); \
957 (err) = 0; \
958 } \
959 } \
960 if ((err)) \
961 (ifq)->ifq_drops++; \
962 mutex_exit((ifq)->ifq_lock); \
963 } while (/*CONSTCOND*/ 0)
964
965 #define IFQ_DEQUEUE(ifq, m) \
966 do { \
967 mutex_enter((ifq)->ifq_lock); \
968 if (TBR_IS_ENABLED(ifq)) \
969 (m) = tbr_dequeue((ifq), ALTDQ_REMOVE); \
970 else if (ALTQ_IS_ENABLED(ifq)) \
971 ALTQ_DEQUEUE((ifq), (m)); \
972 else \
973 IF_DEQUEUE((ifq), (m)); \
974 mutex_exit((ifq)->ifq_lock); \
975 } while (/*CONSTCOND*/ 0)
976
977 #define IFQ_POLL(ifq, m) \
978 do { \
979 mutex_enter((ifq)->ifq_lock); \
980 if (TBR_IS_ENABLED(ifq)) \
981 (m) = tbr_dequeue((ifq), ALTDQ_POLL); \
982 else if (ALTQ_IS_ENABLED(ifq)) \
983 ALTQ_POLL((ifq), (m)); \
984 else \
985 IF_POLL((ifq), (m)); \
986 mutex_exit((ifq)->ifq_lock); \
987 } while (/*CONSTCOND*/ 0)
988
989 #define IFQ_PURGE(ifq) \
990 do { \
991 mutex_enter((ifq)->ifq_lock); \
992 if (ALTQ_IS_ENABLED(ifq)) \
993 ALTQ_PURGE(ifq); \
994 else \
995 IF_PURGE(ifq); \
996 mutex_exit((ifq)->ifq_lock); \
997 } while (/*CONSTCOND*/ 0)
998
999 #define IFQ_SET_READY(ifq) \
1000 do { \
1001 (ifq)->altq_flags |= ALTQF_READY; \
1002 } while (/*CONSTCOND*/ 0)
1003
1004 #define IFQ_CLASSIFY(ifq, m, af) \
1005 do { \
1006 KASSERT(((m)->m_flags & M_PKTHDR) != 0); \
1007 mutex_enter((ifq)->ifq_lock); \
1008 if (ALTQ_IS_ENABLED(ifq)) { \
1009 if (ALTQ_NEEDS_CLASSIFY(ifq)) \
1010 (m)->m_pkthdr.pattr_class = (*(ifq)->altq_classify) \
1011 ((ifq)->altq_clfier, (m), (af)); \
1012 (m)->m_pkthdr.pattr_af = (af); \
1013 (m)->m_pkthdr.pattr_hdr = mtod((m), void *); \
1014 } \
1015 mutex_exit((ifq)->ifq_lock); \
1016 } while (/*CONSTCOND*/ 0)
1017 #else /* ! ALTQ */
1018 #define IFQ_ENQUEUE(ifq, m, err) \
1019 do { \
1020 mutex_enter((ifq)->ifq_lock); \
1021 if (IF_QFULL(ifq)) { \
1022 m_freem(m); \
1023 (err) = ENOBUFS; \
1024 } else { \
1025 IF_ENQUEUE((ifq), (m)); \
1026 (err) = 0; \
1027 } \
1028 if (err) \
1029 (ifq)->ifq_drops++; \
1030 mutex_exit((ifq)->ifq_lock); \
1031 } while (/*CONSTCOND*/ 0)
1032
1033 #define IFQ_DEQUEUE(ifq, m) \
1034 do { \
1035 mutex_enter((ifq)->ifq_lock); \
1036 IF_DEQUEUE((ifq), (m)); \
1037 mutex_exit((ifq)->ifq_lock); \
1038 } while (/*CONSTCOND*/ 0)
1039
1040 #define IFQ_POLL(ifq, m) \
1041 do { \
1042 mutex_enter((ifq)->ifq_lock); \
1043 IF_POLL((ifq), (m)); \
1044 mutex_exit((ifq)->ifq_lock); \
1045 } while (/*CONSTCOND*/ 0)
1046
1047 #define IFQ_PURGE(ifq) \
1048 do { \
1049 mutex_enter((ifq)->ifq_lock); \
1050 IF_PURGE(ifq); \
1051 mutex_exit((ifq)->ifq_lock); \
1052 } while (/*CONSTCOND*/ 0)
1053
1054 #define IFQ_SET_READY(ifq) /* nothing */
1055
1056 #define IFQ_CLASSIFY(ifq, m, af) /* nothing */
1057
1058 #endif /* ALTQ */
1059
1060 #define IFQ_LOCK_INIT(ifq) (ifq)->ifq_lock = \
1061 mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET)
1062 #define IFQ_LOCK_DESTROY(ifq) mutex_obj_free((ifq)->ifq_lock)
1063 #define IFQ_LOCK(ifq) mutex_enter((ifq)->ifq_lock)
1064 #define IFQ_UNLOCK(ifq) mutex_exit((ifq)->ifq_lock)
1065
1066 #define IFQ_IS_EMPTY(ifq) IF_IS_EMPTY(ifq)
1067 #define IFQ_INC_LEN(ifq) ((ifq)->ifq_len++)
1068 #define IFQ_DEC_LEN(ifq) (--(ifq)->ifq_len)
1069 #define IFQ_INC_DROPS(ifq) ((ifq)->ifq_drops++)
1070 #define IFQ_SET_MAXLEN(ifq, len) ((ifq)->ifq_maxlen = (len))
1071
1072 #include <sys/mallocvar.h>
1073 MALLOC_DECLARE(M_IFADDR);
1074 MALLOC_DECLARE(M_IFMADDR);
1075
1076 int ifreq_setaddr(u_long, struct ifreq *, const struct sockaddr *);
1077
1078 struct ifnet *if_alloc(u_char);
1079 void if_free(struct ifnet *);
1080 void if_initname(struct ifnet *, const char *, int);
1081 struct ifaddr *if_dl_create(const struct ifnet *, const struct sockaddr_dl **);
1082 void if_activate_sadl(struct ifnet *, struct ifaddr *,
1083 const struct sockaddr_dl *);
1084 void if_set_sadl(struct ifnet *, const void *, u_char, bool);
1085 void if_alloc_sadl(struct ifnet *);
1086 void if_free_sadl(struct ifnet *, int);
1087 int if_initialize(struct ifnet *);
1088 void if_register(struct ifnet *);
1089 int if_attach(struct ifnet *); /* Deprecated. Use if_initialize and if_register */
1090 void if_attachdomain(void);
1091 void if_deactivate(struct ifnet *);
1092 bool if_is_deactivated(const struct ifnet *);
1093 void if_purgeaddrs(struct ifnet *, int, void (*)(struct ifaddr *));
1094 void if_detach(struct ifnet *);
1095 void if_down(struct ifnet *);
1096 void if_down_locked(struct ifnet *);
1097 void if_link_state_change(struct ifnet *, int);
1098 void if_link_state_change_softint(struct ifnet *, int);
1099 void if_up(struct ifnet *);
1100 void ifinit(void);
1101 void ifinit1(void);
1102 void ifinit_post(void);
1103 int ifaddrpref_ioctl(struct socket *, u_long, void *, struct ifnet *);
1104 extern int (*ifioctl)(struct socket *, u_long, void *, struct lwp *);
1105 int ifioctl_common(struct ifnet *, u_long, void *);
1106 int ifpromisc(struct ifnet *, int);
1107 int ifpromisc_locked(struct ifnet *, int);
1108 int if_addr_init(ifnet_t *, struct ifaddr *, bool);
1109 int if_do_dad(struct ifnet *);
1110 int if_mcast_op(ifnet_t *, const unsigned long, const struct sockaddr *);
1111 int if_flags_set(struct ifnet *, const short);
1112 int if_clone_list(int, char *, int *);
1113
1114 struct ifnet *ifunit(const char *);
1115 struct ifnet *if_get(const char *, struct psref *);
1116 ifnet_t *if_byindex(u_int);
1117 ifnet_t *_if_byindex(u_int);
1118 ifnet_t *if_get_byindex(u_int, struct psref *);
1119 ifnet_t *if_get_bylla(const void *, unsigned char, struct psref *);
1120 void if_put(const struct ifnet *, struct psref *);
1121 void if_acquire(struct ifnet *, struct psref *);
1122 #define if_release if_put
1123
1124 int if_tunnel_check_nesting(struct ifnet *, struct mbuf *, int);
1125
1126 static __inline if_index_t
1127 if_get_index(const struct ifnet *ifp)
1128 {
1129
1130 return ifp != NULL ? ifp->if_index : 0;
1131 }
1132
1133 bool if_held(struct ifnet *);
1134
1135 void if_input(struct ifnet *, struct mbuf *);
1136
1137 struct if_percpuq *
1138 if_percpuq_create(struct ifnet *);
1139 void if_percpuq_destroy(struct if_percpuq *);
1140 void
1141 if_percpuq_enqueue(struct if_percpuq *, struct mbuf *);
1142
1143 void if_deferred_start_init(struct ifnet *, void (*)(struct ifnet *));
1144 void if_schedule_deferred_start(struct ifnet *);
1145
1146 void ifa_insert(struct ifnet *, struct ifaddr *);
1147 void ifa_remove(struct ifnet *, struct ifaddr *);
1148
1149 void ifa_psref_init(struct ifaddr *);
1150 void ifa_acquire(struct ifaddr *, struct psref *);
1151 void ifa_release(struct ifaddr *, struct psref *);
1152 bool ifa_held(struct ifaddr *);
1153 bool ifa_is_destroying(struct ifaddr *);
1154
1155 void ifaref(struct ifaddr *);
1156 void ifafree(struct ifaddr *);
1157
1158 struct ifaddr *ifa_ifwithaddr(const struct sockaddr *);
1159 struct ifaddr *ifa_ifwithaddr_psref(const struct sockaddr *, struct psref *);
1160 struct ifaddr *ifa_ifwithaf(int);
1161 struct ifaddr *ifa_ifwithdstaddr(const struct sockaddr *);
1162 struct ifaddr *ifa_ifwithdstaddr_psref(const struct sockaddr *,
1163 struct psref *);
1164 struct ifaddr *ifa_ifwithnet(const struct sockaddr *);
1165 struct ifaddr *ifa_ifwithnet_psref(const struct sockaddr *, struct psref *);
1166 struct ifaddr *ifa_ifwithladdr(const struct sockaddr *);
1167 struct ifaddr *ifa_ifwithladdr_psref(const struct sockaddr *, struct psref *);
1168 struct ifaddr *ifaof_ifpforaddr(const struct sockaddr *, struct ifnet *);
1169 struct ifaddr *ifaof_ifpforaddr_psref(const struct sockaddr *, struct ifnet *,
1170 struct psref *);
1171 void link_rtrequest(int, struct rtentry *, const struct rt_addrinfo *);
1172 void p2p_rtrequest(int, struct rtentry *, const struct rt_addrinfo *);
1173
1174 void if_clone_attach(struct if_clone *);
1175 void if_clone_detach(struct if_clone *);
1176
1177 int if_transmit_lock(struct ifnet *, struct mbuf *);
1178
1179 int ifq_enqueue(struct ifnet *, struct mbuf *);
1180 int ifq_enqueue2(struct ifnet *, struct ifqueue *, struct mbuf *);
1181
1182 int loioctl(struct ifnet *, u_long, void *);
1183 void loopattach(int);
1184 void loopinit(void);
1185 int looutput(struct ifnet *,
1186 struct mbuf *, const struct sockaddr *, const struct rtentry *);
1187
1188 /*
1189 * These are exported because they're an easy way to tell if
1190 * an interface is going away without having to burn a flag.
1191 */
1192 int if_nulloutput(struct ifnet *, struct mbuf *,
1193 const struct sockaddr *, const struct rtentry *);
1194 void if_nullinput(struct ifnet *, struct mbuf *);
1195 void if_nullstart(struct ifnet *);
1196 int if_nulltransmit(struct ifnet *, struct mbuf *);
1197 int if_nullioctl(struct ifnet *, u_long, void *);
1198 int if_nullinit(struct ifnet *);
1199 void if_nullstop(struct ifnet *, int);
1200 void if_nullslowtimo(struct ifnet *);
1201 #define if_nullwatchdog if_nullslowtimo
1202 void if_nulldrain(struct ifnet *);
1203 #else
1204 struct if_nameindex {
1205 unsigned int if_index; /* 1, 2, ... */
1206 char *if_name; /* null terminated name: "le0", ... */
1207 };
1208
1209 #include <sys/cdefs.h>
1210 __BEGIN_DECLS
1211 unsigned int if_nametoindex(const char *);
1212 char * if_indextoname(unsigned int, char *);
1213 struct if_nameindex * if_nameindex(void);
1214 void if_freenameindex(struct if_nameindex *);
1215 __END_DECLS
1216 #endif /* _KERNEL */ /* XXX really ALTQ? */
1217
1218 #ifdef _KERNEL
1219
1220 #define IFADDR_FIRST(__ifp) TAILQ_FIRST(&(__ifp)->if_addrlist)
1221 #define IFADDR_NEXT(__ifa) TAILQ_NEXT((__ifa), ifa_list)
1222 #define IFADDR_FOREACH(__ifa, __ifp) TAILQ_FOREACH(__ifa, \
1223 &(__ifp)->if_addrlist, ifa_list)
1224 #define IFADDR_FOREACH_SAFE(__ifa, __ifp, __nifa) \
1225 TAILQ_FOREACH_SAFE(__ifa, \
1226 &(__ifp)->if_addrlist, ifa_list, __nifa)
1227 #define IFADDR_EMPTY(__ifp) TAILQ_EMPTY(&(__ifp)->if_addrlist)
1228
1229 #define IFADDR_ENTRY_INIT(__ifa) \
1230 PSLIST_ENTRY_INIT((__ifa), ifa_pslist_entry)
1231 #define IFADDR_ENTRY_DESTROY(__ifa) \
1232 PSLIST_ENTRY_DESTROY((__ifa), ifa_pslist_entry)
1233 #define IFADDR_READER_EMPTY(__ifp) \
1234 (PSLIST_READER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr, \
1235 ifa_pslist_entry) == NULL)
1236 #define IFADDR_READER_FIRST(__ifp) \
1237 PSLIST_READER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr, \
1238 ifa_pslist_entry)
1239 #define IFADDR_READER_NEXT(__ifa) \
1240 PSLIST_READER_NEXT((__ifa), struct ifaddr, ifa_pslist_entry)
1241 #define IFADDR_READER_FOREACH(__ifa, __ifp) \
1242 PSLIST_READER_FOREACH((__ifa), &(__ifp)->if_addr_pslist, struct ifaddr,\
1243 ifa_pslist_entry)
1244 #define IFADDR_WRITER_INSERT_HEAD(__ifp, __ifa) \
1245 PSLIST_WRITER_INSERT_HEAD(&(__ifp)->if_addr_pslist, (__ifa), \
1246 ifa_pslist_entry)
1247 #define IFADDR_WRITER_REMOVE(__ifa) \
1248 PSLIST_WRITER_REMOVE((__ifa), ifa_pslist_entry)
1249 #define IFADDR_WRITER_FOREACH(__ifa, __ifp) \
1250 PSLIST_WRITER_FOREACH((__ifa), &(__ifp)->if_addr_pslist, struct ifaddr,\
1251 ifa_pslist_entry)
1252 #define IFADDR_WRITER_NEXT(__ifp) \
1253 PSLIST_WRITER_NEXT((__ifp), struct ifaddr, ifa_pslist_entry)
1254 #define IFADDR_WRITER_INSERT_AFTER(__ifp, __new) \
1255 PSLIST_WRITER_INSERT_AFTER((__ifp), (__new), ifa_pslist_entry)
1256 #define IFADDR_WRITER_EMPTY(__ifp) \
1257 (PSLIST_WRITER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr, \
1258 ifa_pslist_entry) == NULL)
1259 #define IFADDR_WRITER_INSERT_TAIL(__ifp, __new) \
1260 do { \
1261 if (IFADDR_WRITER_EMPTY(__ifp)) { \
1262 IFADDR_WRITER_INSERT_HEAD((__ifp), (__new)); \
1263 } else { \
1264 struct ifaddr *__ifa; \
1265 IFADDR_WRITER_FOREACH(__ifa, (__ifp)) { \
1266 if (IFADDR_WRITER_NEXT(__ifa) == NULL) {\
1267 IFADDR_WRITER_INSERT_AFTER(__ifa,\
1268 (__new)); \
1269 break; \
1270 } \
1271 } \
1272 } \
1273 } while (0)
1274
1275 #define IFNET_GLOBAL_LOCK() mutex_enter(&ifnet_mtx)
1276 #define IFNET_GLOBAL_UNLOCK() mutex_exit(&ifnet_mtx)
1277 #define IFNET_GLOBAL_LOCKED() mutex_owned(&ifnet_mtx)
1278
1279 #define IFNET_READER_EMPTY() \
1280 (PSLIST_READER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry) == NULL)
1281 #define IFNET_READER_FIRST() \
1282 PSLIST_READER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry)
1283 #define IFNET_READER_NEXT(__ifp) \
1284 PSLIST_READER_NEXT((__ifp), struct ifnet, if_pslist_entry)
1285 #define IFNET_READER_FOREACH(__ifp) \
1286 PSLIST_READER_FOREACH((__ifp), &ifnet_pslist, struct ifnet, \
1287 if_pslist_entry)
1288 #define IFNET_WRITER_INSERT_HEAD(__ifp) \
1289 PSLIST_WRITER_INSERT_HEAD(&ifnet_pslist, (__ifp), if_pslist_entry)
1290 #define IFNET_WRITER_REMOVE(__ifp) \
1291 PSLIST_WRITER_REMOVE((__ifp), if_pslist_entry)
1292 #define IFNET_WRITER_FOREACH(__ifp) \
1293 PSLIST_WRITER_FOREACH((__ifp), &ifnet_pslist, struct ifnet, \
1294 if_pslist_entry)
1295 #define IFNET_WRITER_NEXT(__ifp) \
1296 PSLIST_WRITER_NEXT((__ifp), struct ifnet, if_pslist_entry)
1297 #define IFNET_WRITER_INSERT_AFTER(__ifp, __new) \
1298 PSLIST_WRITER_INSERT_AFTER((__ifp), (__new), if_pslist_entry)
1299 #define IFNET_WRITER_EMPTY() \
1300 (PSLIST_WRITER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry) == NULL)
1301 #define IFNET_WRITER_INSERT_TAIL(__new) \
1302 do { \
1303 if (IFNET_WRITER_EMPTY()) { \
1304 IFNET_WRITER_INSERT_HEAD(__new); \
1305 } else { \
1306 struct ifnet *__ifp; \
1307 IFNET_WRITER_FOREACH(__ifp) { \
1308 if (IFNET_WRITER_NEXT(__ifp) == NULL) { \
1309 IFNET_WRITER_INSERT_AFTER(__ifp,\
1310 (__new)); \
1311 break; \
1312 } \
1313 } \
1314 } \
1315 } while (0)
1316
1317 #define IFNET_LOCK(ifp) mutex_enter((ifp)->if_ioctl_lock)
1318 #define IFNET_UNLOCK(ifp) mutex_exit((ifp)->if_ioctl_lock)
1319 #define IFNET_LOCKED(ifp) mutex_owned((ifp)->if_ioctl_lock)
1320
1321 #define IFNET_ASSERT_UNLOCKED(ifp) \
1322 KDASSERT(mutex_ownable((ifp)->if_ioctl_lock))
1323
1324 extern struct pslist_head ifnet_pslist;
1325 extern kmutex_t ifnet_mtx;
1326
1327 extern struct ifnet *lo0ifp;
1328
1329 /*
1330 * ifq sysctl support
1331 */
1332 int sysctl_ifq(int *name, u_int namelen, void *oldp,
1333 size_t *oldlenp, void *newp, size_t newlen,
1334 struct ifqueue *ifq);
1335 /* symbolic names for terminal (per-protocol) CTL_IFQ_ nodes */
1336 #define IFQCTL_LEN 1
1337 #define IFQCTL_MAXLEN 2
1338 #define IFQCTL_PEAK 3
1339 #define IFQCTL_DROPS 4
1340
1341 /*
1342 * Hook for if_vlan - needed by if_agr
1343 */
1344 MODULE_HOOK(if_vlan_vlan_input_hook, void, (struct ifnet *, struct mbuf *));
1345
1346 #endif /* _KERNEL */
1347
1348 #endif /* !_NET_IF_H_ */
1349