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