Home | History | Annotate | Line # | Download | only in net
if.c revision 1.492
      1  1.492  yamaguch /*	$NetBSD: if.c,v 1.492 2021/09/30 03:23:48 yamaguchi Exp $	*/
      2   1.53   thorpej 
      3   1.53   thorpej /*-
      4  1.219        ad  * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
      5   1.53   thorpej  * All rights reserved.
      6   1.53   thorpej  *
      7   1.53   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.150     peter  * by William Studenmund and Jason R. Thorpe.
      9   1.53   thorpej  *
     10   1.53   thorpej  * Redistribution and use in source and binary forms, with or without
     11   1.53   thorpej  * modification, are permitted provided that the following conditions
     12   1.53   thorpej  * are met:
     13   1.53   thorpej  * 1. Redistributions of source code must retain the above copyright
     14   1.53   thorpej  *    notice, this list of conditions and the following disclaimer.
     15   1.53   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.53   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17   1.53   thorpej  *    documentation and/or other materials provided with the distribution.
     18   1.53   thorpej  *
     19   1.53   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.53   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.53   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.53   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.53   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.53   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.53   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.53   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.53   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.53   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.53   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30   1.53   thorpej  */
     31   1.49    itojun 
     32   1.49    itojun /*
     33   1.49    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
     34   1.49    itojun  * All rights reserved.
     35  1.154     perry  *
     36   1.49    itojun  * Redistribution and use in source and binary forms, with or without
     37   1.49    itojun  * modification, are permitted provided that the following conditions
     38   1.49    itojun  * are met:
     39   1.49    itojun  * 1. Redistributions of source code must retain the above copyright
     40   1.49    itojun  *    notice, this list of conditions and the following disclaimer.
     41   1.49    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     42   1.49    itojun  *    notice, this list of conditions and the following disclaimer in the
     43   1.49    itojun  *    documentation and/or other materials provided with the distribution.
     44   1.49    itojun  * 3. Neither the name of the project nor the names of its contributors
     45   1.49    itojun  *    may be used to endorse or promote products derived from this software
     46   1.49    itojun  *    without specific prior written permission.
     47  1.154     perry  *
     48   1.49    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     49   1.49    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50   1.49    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51   1.49    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     52   1.49    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53   1.49    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54   1.49    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55   1.49    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56   1.49    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57   1.49    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58   1.49    itojun  * SUCH DAMAGE.
     59   1.49    itojun  */
     60   1.16       cgd 
     61    1.1       cgd /*
     62   1.15   mycroft  * Copyright (c) 1980, 1986, 1993
     63   1.15   mycroft  *	The Regents of the University of California.  All rights reserved.
     64    1.1       cgd  *
     65    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     66    1.1       cgd  * modification, are permitted provided that the following conditions
     67    1.1       cgd  * are met:
     68    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     69    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     70    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     71    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     72    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     73  1.126       agc  * 3. Neither the name of the University nor the names of its contributors
     74    1.1       cgd  *    may be used to endorse or promote products derived from this software
     75    1.1       cgd  *    without specific prior written permission.
     76    1.1       cgd  *
     77    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     78    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     79    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     80    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     81    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     82    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     83    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     84    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     85    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     86    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     87    1.1       cgd  * SUCH DAMAGE.
     88    1.1       cgd  *
     89   1.44      fvdl  *	@(#)if.c	8.5 (Berkeley) 1/9/95
     90    1.1       cgd  */
     91   1.99     lukem 
     92   1.99     lukem #include <sys/cdefs.h>
     93  1.492  yamaguch __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.492 2021/09/30 03:23:48 yamaguchi Exp $");
     94   1.50   thorpej 
     95  1.308     ozaki #if defined(_KERNEL_OPT)
     96   1.50   thorpej #include "opt_inet.h"
     97  1.352  knakahar #include "opt_ipsec.h"
     98   1.51    bouyer #include "opt_atalk.h"
     99  1.288     ozaki #include "opt_wlan.h"
    100  1.308     ozaki #include "opt_net_mpsafe.h"
    101  1.417     ozaki #include "opt_mrouting.h"
    102  1.308     ozaki #endif
    103    1.1       cgd 
    104    1.8   mycroft #include <sys/param.h>
    105    1.8   mycroft #include <sys/mbuf.h>
    106    1.8   mycroft #include <sys/systm.h>
    107   1.59   thorpej #include <sys/callout.h>
    108   1.15   mycroft #include <sys/proc.h>
    109    1.8   mycroft #include <sys/socket.h>
    110    1.8   mycroft #include <sys/socketvar.h>
    111   1.56   thorpej #include <sys/domain.h>
    112    1.8   mycroft #include <sys/protosw.h>
    113    1.8   mycroft #include <sys/kernel.h>
    114    1.8   mycroft #include <sys/ioctl.h>
    115  1.133  jonathan #include <sys/sysctl.h>
    116  1.159    dyoung #include <sys/syslog.h>
    117  1.165      elad #include <sys/kauth.h>
    118  1.254    dyoung #include <sys/kmem.h>
    119  1.276     rmind #include <sys/xcall.h>
    120  1.323     ozaki #include <sys/cpu.h>
    121  1.323     ozaki #include <sys/intr.h>
    122  1.447  pgoyette #include <sys/module_hook.h>
    123  1.444  pgoyette #include <sys/compat_stub.h>
    124  1.465      maxv #include <sys/msan.h>
    125  1.492  yamaguch #include <sys/hook.h>
    126    1.1       cgd 
    127    1.8   mycroft #include <net/if.h>
    128    1.8   mycroft #include <net/if_dl.h>
    129   1.66      onoe #include <net/if_ether.h>
    130  1.124    dyoung #include <net/if_media.h>
    131  1.132    dyoung #include <net80211/ieee80211.h>
    132  1.132    dyoung #include <net80211/ieee80211_ioctl.h>
    133    1.8   mycroft #include <net/if_types.h>
    134   1.53   thorpej #include <net/route.h>
    135   1.95    itojun #include <net/netisr.h>
    136  1.262  christos #include <sys/module.h>
    137   1.51    bouyer #ifdef NETATALK
    138   1.51    bouyer #include <netatalk/at_extern.h>
    139   1.51    bouyer #include <netatalk/at.h>
    140   1.51    bouyer #endif
    141  1.143    itojun #include <net/pfil.h>
    142  1.278        he #include <netinet/in.h>
    143  1.276     rmind #include <netinet/in_var.h>
    144  1.352  knakahar #include <netinet/ip_encap.h>
    145  1.368     ozaki #include <net/bpf.h>
    146    1.1       cgd 
    147   1.49    itojun #ifdef INET6
    148   1.72   thorpej #include <netinet6/in6_var.h>
    149  1.108    itojun #include <netinet6/nd6.h>
    150   1.49    itojun #endif
    151  1.117   thorpej 
    152  1.288     ozaki #include "ether.h"
    153  1.288     ozaki 
    154  1.442       rin #include "bridge.h"
    155  1.442       rin #if NBRIDGE > 0
    156  1.442       rin #include <net/if_bridgevar.h>
    157  1.442       rin #endif
    158  1.442       rin 
    159  1.166  liamjfoy #include "carp.h"
    160  1.166  liamjfoy #if NCARP > 0
    161  1.166  liamjfoy #include <netinet/ip_carp.h>
    162  1.166  liamjfoy #endif
    163  1.166  liamjfoy 
    164  1.485  yamaguch #include "lagg.h"
    165  1.485  yamaguch #if NLAGG > 0
    166  1.485  yamaguch #include <net/lagg/if_laggvar.h>
    167  1.485  yamaguch #endif
    168  1.485  yamaguch 
    169  1.186  christos #include <compat/sys/sockio.h>
    170  1.161  christos 
    171  1.117   thorpej MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
    172  1.117   thorpej MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
    173   1.49    itojun 
    174  1.274     rmind /*
    175  1.480       roy  * XXX reusing (ifp)->if_snd->ifq_lock rather than having another spin mutex
    176  1.480       roy  * for each ifnet.  It doesn't matter because:
    177  1.480       roy  * - if IFEF_MPSAFE is enabled, if_snd isn't used and lock contentions on
    178  1.480       roy  *   ifq_lock don't happen
    179  1.480       roy  * - if IFEF_MPSAFE is disabled, there is no lock contention on ifq_lock
    180  1.480       roy  *   because if_snd, if_link_state_change and if_link_state_change_process
    181  1.480       roy  *   are all called with KERNEL_LOCK
    182  1.480       roy  */
    183  1.480       roy #define IF_LINK_STATE_CHANGE_LOCK(ifp)		\
    184  1.480       roy 	mutex_enter((ifp)->if_snd.ifq_lock)
    185  1.480       roy #define IF_LINK_STATE_CHANGE_UNLOCK(ifp)	\
    186  1.480       roy 	mutex_exit((ifp)->if_snd.ifq_lock)
    187  1.480       roy 
    188  1.480       roy /*
    189  1.274     rmind  * Global list of interfaces.
    190  1.274     rmind  */
    191  1.334     ozaki /* DEPRECATED. Remove it once kvm(3) users disappeared */
    192  1.274     rmind struct ifnet_head		ifnet_list;
    193  1.334     ozaki 
    194  1.334     ozaki struct pslist_head		ifnet_pslist;
    195  1.274     rmind static ifnet_t **		ifindex2ifnet = NULL;
    196  1.274     rmind static u_int			if_index = 1;
    197  1.274     rmind static size_t			if_indexlim = 0;
    198  1.274     rmind static uint64_t			index_gen;
    199  1.334     ozaki /* Mutex to protect the above objects. */
    200  1.334     ozaki kmutex_t			ifnet_mtx __cacheline_aligned;
    201  1.385     ozaki static struct psref_class	*ifnet_psref_class __read_mostly;
    202  1.334     ozaki static pserialize_t		ifnet_psz;
    203  1.471   thorpej static struct workqueue		*ifnet_link_state_wq __read_mostly;
    204  1.334     ozaki 
    205  1.292  christos static kmutex_t			if_clone_mtx;
    206  1.274     rmind 
    207  1.274     rmind struct ifnet *lo0ifp;
    208    1.1       cgd int	ifqmaxlen = IFQ_MAXLEN;
    209  1.104      matt 
    210  1.357     ozaki struct psref_class		*ifa_psref_class __read_mostly;
    211  1.357     ozaki 
    212  1.362     ozaki static int	if_delroute_matcher(struct rtentry *, void *);
    213   1.53   thorpej 
    214  1.379  knakahar static bool if_is_unit(const char *);
    215  1.163   thorpej static struct if_clone *if_clone_lookup(const char *, int *);
    216   1.63   thorpej 
    217  1.163   thorpej static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
    218  1.163   thorpej static int if_cloners_count;
    219   1.63   thorpej 
    220  1.265     rmind /* Packet filtering hook for interfaces. */
    221  1.369  christos pfil_head_t *			if_pfil __read_mostly;
    222  1.143    itojun 
    223  1.239      elad static kauth_listener_t if_listener;
    224  1.239      elad 
    225  1.273     pooka static int doifioctl(struct socket *, u_long, void *, struct lwp *);
    226  1.163   thorpej static void if_detach_queues(struct ifnet *, struct ifqueue *);
    227  1.234    dyoung static void sysctl_sndq_setup(struct sysctllog **, const char *,
    228  1.234    dyoung     struct ifaltq *);
    229  1.294     ozaki static void if_slowtimo(void *);
    230  1.302     ozaki static void if_attachdomain1(struct ifnet *);
    231  1.302     ozaki static int ifconf(u_long, void *);
    232  1.345  knakahar static int if_transmit(struct ifnet *, struct mbuf *);
    233  1.302     ozaki static int if_clone_create(const char *);
    234  1.302     ozaki static int if_clone_destroy(const char *);
    235  1.471   thorpej static void if_link_state_change_work(struct work *, void *);
    236  1.403     ozaki static void if_up_locked(struct ifnet *);
    237  1.403     ozaki static void _if_down(struct ifnet *);
    238  1.403     ozaki static void if_down_deactivated(struct ifnet *);
    239   1.95    itojun 
    240  1.323     ozaki struct if_percpuq {
    241  1.323     ozaki 	struct ifnet	*ipq_ifp;
    242  1.323     ozaki 	void		*ipq_si;
    243  1.323     ozaki 	struct percpu	*ipq_ifqs;	/* struct ifqueue */
    244  1.323     ozaki };
    245  1.323     ozaki 
    246  1.323     ozaki static struct mbuf *if_percpuq_dequeue(struct if_percpuq *);
    247  1.323     ozaki 
    248  1.327  knakahar static void if_percpuq_drops(void *, void *, struct cpu_info *);
    249  1.327  knakahar static int sysctl_percpuq_drops_handler(SYSCTLFN_PROTO);
    250  1.327  knakahar static void sysctl_percpuq_setup(struct sysctllog **, const char *,
    251  1.327  knakahar     struct if_percpuq *);
    252  1.327  knakahar 
    253  1.364     ozaki struct if_deferred_start {
    254  1.364     ozaki 	struct ifnet	*ids_ifp;
    255  1.364     ozaki 	void		(*ids_if_start)(struct ifnet *);
    256  1.364     ozaki 	void		*ids_si;
    257  1.364     ozaki };
    258  1.364     ozaki 
    259  1.364     ozaki static void if_deferred_start_softint(void *);
    260  1.364     ozaki static void if_deferred_start_common(struct ifnet *);
    261  1.364     ozaki static void if_deferred_start_destroy(struct ifnet *);
    262  1.364     ozaki 
    263  1.240    cegger #if defined(INET) || defined(INET6)
    264  1.276     rmind static void sysctl_net_pktq_setup(struct sysctllog **, int);
    265  1.240    cegger #endif
    266  1.237     pooka 
    267  1.447  pgoyette /*
    268  1.447  pgoyette  * Hook for if_vlan - needed by if_agr
    269  1.447  pgoyette  */
    270  1.447  pgoyette struct if_vlan_vlan_input_hook_t if_vlan_vlan_input_hook;
    271  1.447  pgoyette 
    272  1.370     ozaki static void if_sysctl_setup(struct sysctllog **);
    273  1.370     ozaki 
    274  1.239      elad static int
    275  1.239      elad if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    276  1.239      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    277  1.239      elad {
    278  1.239      elad 	int result;
    279  1.239      elad 	enum kauth_network_req req;
    280  1.239      elad 
    281  1.239      elad 	result = KAUTH_RESULT_DEFER;
    282  1.473     joerg 	req = (enum kauth_network_req)(uintptr_t)arg1;
    283  1.239      elad 
    284  1.239      elad 	if (action != KAUTH_NETWORK_INTERFACE)
    285  1.239      elad 		return result;
    286  1.239      elad 
    287  1.239      elad 	if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) ||
    288  1.239      elad 	    (req == KAUTH_REQ_NETWORK_INTERFACE_SET))
    289  1.239      elad 		result = KAUTH_RESULT_ALLOW;
    290  1.239      elad 
    291  1.239      elad 	return result;
    292  1.239      elad }
    293  1.239      elad 
    294    1.1       cgd /*
    295    1.1       cgd  * Network interface utility routines.
    296    1.1       cgd  *
    297    1.1       cgd  * Routines with ifa_ifwith* names take sockaddr *'s as
    298    1.1       cgd  * parameters.
    299    1.1       cgd  */
    300    1.4    andrew void
    301  1.163   thorpej ifinit(void)
    302    1.1       cgd {
    303  1.370     ozaki 
    304  1.392     ozaki #if (defined(INET) || defined(INET6))
    305  1.352  knakahar 	encapinit();
    306  1.352  knakahar #endif
    307  1.352  knakahar 
    308  1.239      elad 	if_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
    309  1.239      elad 	    if_listener_cb, NULL);
    310  1.273     pooka 
    311  1.273     pooka 	/* interfaces are available, inform socket code */
    312  1.273     pooka 	ifioctl = doifioctl;
    313  1.227      yamt }
    314  1.227      yamt 
    315  1.227      yamt /*
    316  1.227      yamt  * XXX Initialization before configure().
    317  1.227      yamt  * XXX hack to get pfil_add_hook working in autoconf.
    318  1.227      yamt  */
    319  1.227      yamt void
    320  1.227      yamt ifinit1(void)
    321  1.227      yamt {
    322  1.471   thorpej 	int error __diagused;
    323  1.424   msaitoh 
    324  1.424   msaitoh #ifdef NET_MPSAFE
    325  1.424   msaitoh 	printf("NET_MPSAFE enabled\n");
    326  1.424   msaitoh #endif
    327  1.424   msaitoh 
    328  1.292  christos 	mutex_init(&if_clone_mtx, MUTEX_DEFAULT, IPL_NONE);
    329  1.357     ozaki 
    330  1.274     rmind 	TAILQ_INIT(&ifnet_list);
    331  1.334     ozaki 	mutex_init(&ifnet_mtx, MUTEX_DEFAULT, IPL_NONE);
    332  1.334     ozaki 	ifnet_psz = pserialize_create();
    333  1.334     ozaki 	ifnet_psref_class = psref_class_create("ifnet", IPL_SOFTNET);
    334  1.357     ozaki 	ifa_psref_class = psref_class_create("ifa", IPL_SOFTNET);
    335  1.471   thorpej 	error = workqueue_create(&ifnet_link_state_wq, "iflnkst",
    336  1.472   thorpej 	    if_link_state_change_work, NULL, PRI_SOFTNET, IPL_NET,
    337  1.471   thorpej 	    WQ_MPSAFE);
    338  1.471   thorpej 	KASSERT(error == 0);
    339  1.334     ozaki 	PSLIST_INIT(&ifnet_pslist);
    340  1.357     ozaki 
    341  1.274     rmind 	if_indexlim = 8;
    342  1.274     rmind 
    343  1.265     rmind 	if_pfil = pfil_head_create(PFIL_TYPE_IFNET, NULL);
    344  1.265     rmind 	KASSERT(if_pfil != NULL);
    345  1.286     ozaki 
    346  1.468   thorpej #if NETHER > 0 || defined(NETATALK) || defined(WLAN)
    347  1.286     ozaki 	etherinit();
    348  1.288     ozaki #endif
    349    1.1       cgd }
    350    1.1       cgd 
    351  1.429     ozaki /* XXX must be after domaininit() */
    352  1.429     ozaki void
    353  1.429     ozaki ifinit_post(void)
    354  1.429     ozaki {
    355  1.429     ozaki 
    356  1.429     ozaki 	if_sysctl_setup(NULL);
    357  1.429     ozaki }
    358  1.429     ozaki 
    359  1.274     rmind ifnet_t *
    360  1.226  christos if_alloc(u_char type)
    361  1.226  christos {
    362  1.274     rmind 	return kmem_zalloc(sizeof(ifnet_t), KM_SLEEP);
    363  1.226  christos }
    364  1.226  christos 
    365  1.226  christos void
    366  1.274     rmind if_free(ifnet_t *ifp)
    367  1.251    dyoung {
    368  1.274     rmind 	kmem_free(ifp, sizeof(ifnet_t));
    369  1.251    dyoung }
    370  1.251    dyoung 
    371  1.251    dyoung void
    372  1.226  christos if_initname(struct ifnet *ifp, const char *name, int unit)
    373  1.226  christos {
    374  1.226  christos 	(void)snprintf(ifp->if_xname, sizeof(ifp->if_xname),
    375  1.226  christos 	    "%s%d", name, unit);
    376  1.226  christos }
    377  1.226  christos 
    378   1.53   thorpej /*
    379   1.53   thorpej  * Null routines used while an interface is going away.  These routines
    380   1.53   thorpej  * just return an error.
    381   1.53   thorpej  */
    382   1.53   thorpej 
    383   1.53   thorpej int
    384  1.177  christos if_nulloutput(struct ifnet *ifp, struct mbuf *m,
    385  1.331     ozaki     const struct sockaddr *so, const struct rtentry *rt)
    386   1.53   thorpej {
    387   1.53   thorpej 
    388  1.185    dyoung 	return ENXIO;
    389   1.53   thorpej }
    390   1.53   thorpej 
    391   1.53   thorpej void
    392  1.177  christos if_nullinput(struct ifnet *ifp, struct mbuf *m)
    393   1.53   thorpej {
    394   1.53   thorpej 
    395   1.53   thorpej 	/* Nothing. */
    396   1.53   thorpej }
    397   1.53   thorpej 
    398   1.53   thorpej void
    399  1.177  christos if_nullstart(struct ifnet *ifp)
    400   1.53   thorpej {
    401   1.53   thorpej 
    402   1.53   thorpej 	/* Nothing. */
    403   1.53   thorpej }
    404   1.53   thorpej 
    405   1.53   thorpej int
    406  1.332  knakahar if_nulltransmit(struct ifnet *ifp, struct mbuf *m)
    407  1.332  knakahar {
    408  1.332  knakahar 
    409  1.383  knakahar 	m_freem(m);
    410  1.332  knakahar 	return ENXIO;
    411  1.332  knakahar }
    412  1.332  knakahar 
    413  1.332  knakahar int
    414  1.183  christos if_nullioctl(struct ifnet *ifp, u_long cmd, void *data)
    415   1.53   thorpej {
    416   1.53   thorpej 
    417  1.185    dyoung 	return ENXIO;
    418   1.53   thorpej }
    419   1.53   thorpej 
    420   1.53   thorpej int
    421  1.177  christos if_nullinit(struct ifnet *ifp)
    422   1.53   thorpej {
    423   1.53   thorpej 
    424  1.185    dyoung 	return ENXIO;
    425   1.53   thorpej }
    426   1.53   thorpej 
    427   1.53   thorpej void
    428  1.177  christos if_nullstop(struct ifnet *ifp, int disable)
    429   1.75   thorpej {
    430   1.75   thorpej 
    431   1.75   thorpej 	/* Nothing. */
    432   1.75   thorpej }
    433   1.75   thorpej 
    434   1.75   thorpej void
    435  1.295     ozaki if_nullslowtimo(struct ifnet *ifp)
    436   1.53   thorpej {
    437   1.53   thorpej 
    438   1.53   thorpej 	/* Nothing. */
    439   1.53   thorpej }
    440   1.53   thorpej 
    441   1.53   thorpej void
    442  1.177  christos if_nulldrain(struct ifnet *ifp)
    443   1.53   thorpej {
    444   1.53   thorpej 
    445   1.53   thorpej 	/* Nothing. */
    446   1.53   thorpej }
    447   1.53   thorpej 
    448  1.210    dyoung void
    449  1.231    dyoung if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory)
    450  1.210    dyoung {
    451  1.210    dyoung 	struct ifaddr *ifa;
    452  1.210    dyoung 	struct sockaddr_dl *sdl;
    453  1.210    dyoung 
    454  1.210    dyoung 	ifp->if_addrlen = addrlen;
    455  1.210    dyoung 	if_alloc_sadl(ifp);
    456  1.210    dyoung 	ifa = ifp->if_dl;
    457  1.210    dyoung 	sdl = satosdl(ifa->ifa_addr);
    458  1.210    dyoung 
    459  1.210    dyoung 	(void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen);
    460  1.231    dyoung 	if (factory) {
    461  1.426     ozaki 		KASSERT(ifp->if_hwdl == NULL);
    462  1.231    dyoung 		ifp->if_hwdl = ifp->if_dl;
    463  1.291     rmind 		ifaref(ifp->if_hwdl);
    464  1.231    dyoung 	}
    465  1.223    dyoung 	/* TBD routing socket */
    466  1.210    dyoung }
    467  1.210    dyoung 
    468  1.211    dyoung struct ifaddr *
    469  1.211    dyoung if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp)
    470  1.211    dyoung {
    471  1.211    dyoung 	unsigned socksize, ifasize;
    472  1.211    dyoung 	int addrlen, namelen;
    473  1.211    dyoung 	struct sockaddr_dl *mask, *sdl;
    474  1.211    dyoung 	struct ifaddr *ifa;
    475  1.211    dyoung 
    476  1.211    dyoung 	namelen = strlen(ifp->if_xname);
    477  1.211    dyoung 	addrlen = ifp->if_addrlen;
    478  1.211    dyoung 	socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long));
    479  1.211    dyoung 	ifasize = sizeof(*ifa) + 2 * socksize;
    480  1.455   msaitoh 	ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
    481  1.211    dyoung 
    482  1.211    dyoung 	sdl = (struct sockaddr_dl *)(ifa + 1);
    483  1.211    dyoung 	mask = (struct sockaddr_dl *)(socksize + (char *)sdl);
    484  1.211    dyoung 
    485  1.211    dyoung 	sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type,
    486  1.211    dyoung 	    ifp->if_xname, namelen, NULL, addrlen);
    487  1.398  christos 	mask->sdl_family = AF_LINK;
    488  1.211    dyoung 	mask->sdl_len = sockaddr_dl_measure(namelen, 0);
    489  1.211    dyoung 	memset(&mask->sdl_data[0], 0xff, namelen);
    490  1.211    dyoung 	ifa->ifa_rtrequest = link_rtrequest;
    491  1.211    dyoung 	ifa->ifa_addr = (struct sockaddr *)sdl;
    492  1.211    dyoung 	ifa->ifa_netmask = (struct sockaddr *)mask;
    493  1.357     ozaki 	ifa_psref_init(ifa);
    494  1.211    dyoung 
    495  1.211    dyoung 	*sdlp = sdl;
    496  1.211    dyoung 
    497  1.211    dyoung 	return ifa;
    498  1.211    dyoung }
    499  1.211    dyoung 
    500  1.223    dyoung static void
    501  1.223    dyoung if_sadl_setrefs(struct ifnet *ifp, struct ifaddr *ifa)
    502  1.223    dyoung {
    503  1.223    dyoung 	const struct sockaddr_dl *sdl;
    504  1.316     ozaki 
    505  1.223    dyoung 	ifp->if_dl = ifa;
    506  1.291     rmind 	ifaref(ifa);
    507  1.223    dyoung 	sdl = satosdl(ifa->ifa_addr);
    508  1.223    dyoung 	ifp->if_sadl = sdl;
    509  1.223    dyoung }
    510  1.223    dyoung 
    511    1.1       cgd /*
    512   1.81   thorpej  * Allocate the link level name for the specified interface.  This
    513   1.81   thorpej  * is an attachment helper.  It must be called after ifp->if_addrlen
    514   1.81   thorpej  * is initialized, which may not be the case when if_attach() is
    515   1.81   thorpej  * called.
    516   1.81   thorpej  */
    517   1.81   thorpej void
    518   1.81   thorpej if_alloc_sadl(struct ifnet *ifp)
    519   1.81   thorpej {
    520   1.81   thorpej 	struct ifaddr *ifa;
    521  1.211    dyoung 	const struct sockaddr_dl *sdl;
    522   1.84   thorpej 
    523   1.84   thorpej 	/*
    524   1.84   thorpej 	 * If the interface already has a link name, release it
    525   1.84   thorpej 	 * now.  This is useful for interfaces that can change
    526   1.84   thorpej 	 * link types, and thus switch link names often.
    527   1.84   thorpej 	 */
    528   1.84   thorpej 	if (ifp->if_sadl != NULL)
    529  1.426     ozaki 		if_free_sadl(ifp, 0);
    530   1.81   thorpej 
    531  1.211    dyoung 	ifa = if_dl_create(ifp, &sdl);
    532  1.195    dyoung 
    533  1.207    dyoung 	ifa_insert(ifp, ifa);
    534  1.223    dyoung 	if_sadl_setrefs(ifp, ifa);
    535  1.223    dyoung }
    536  1.223    dyoung 
    537  1.223    dyoung static void
    538  1.223    dyoung if_deactivate_sadl(struct ifnet *ifp)
    539  1.223    dyoung {
    540  1.223    dyoung 	struct ifaddr *ifa;
    541  1.223    dyoung 
    542  1.223    dyoung 	KASSERT(ifp->if_dl != NULL);
    543  1.223    dyoung 
    544  1.223    dyoung 	ifa = ifp->if_dl;
    545  1.223    dyoung 
    546  1.223    dyoung 	ifp->if_sadl = NULL;
    547  1.223    dyoung 
    548  1.223    dyoung 	ifp->if_dl = NULL;
    549  1.291     rmind 	ifafree(ifa);
    550  1.223    dyoung }
    551  1.223    dyoung 
    552  1.378     ozaki static void
    553  1.378     ozaki if_replace_sadl(struct ifnet *ifp, struct ifaddr *ifa)
    554  1.378     ozaki {
    555  1.378     ozaki 	struct ifaddr *old;
    556  1.378     ozaki 
    557  1.378     ozaki 	KASSERT(ifp->if_dl != NULL);
    558  1.378     ozaki 
    559  1.378     ozaki 	old = ifp->if_dl;
    560  1.378     ozaki 
    561  1.378     ozaki 	ifaref(ifa);
    562  1.378     ozaki 	/* XXX Update if_dl and if_sadl atomically */
    563  1.378     ozaki 	ifp->if_dl = ifa;
    564  1.378     ozaki 	ifp->if_sadl = satosdl(ifa->ifa_addr);
    565  1.378     ozaki 
    566  1.378     ozaki 	ifafree(old);
    567  1.378     ozaki }
    568  1.378     ozaki 
    569  1.224    dyoung void
    570  1.357     ozaki if_activate_sadl(struct ifnet *ifp, struct ifaddr *ifa0,
    571  1.223    dyoung     const struct sockaddr_dl *sdl)
    572  1.223    dyoung {
    573  1.357     ozaki 	int s, ss;
    574  1.357     ozaki 	struct ifaddr *ifa;
    575  1.357     ozaki 	int bound = curlwp_bind();
    576  1.223    dyoung 
    577  1.378     ozaki 	KASSERT(ifa_held(ifa0));
    578  1.378     ozaki 
    579  1.373     ozaki 	s = splsoftnet();
    580  1.223    dyoung 
    581  1.378     ozaki 	if_replace_sadl(ifp, ifa0);
    582  1.357     ozaki 
    583  1.357     ozaki 	ss = pserialize_read_enter();
    584  1.357     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
    585  1.357     ozaki 		struct psref psref;
    586  1.357     ozaki 		ifa_acquire(ifa, &psref);
    587  1.357     ozaki 		pserialize_read_exit(ss);
    588  1.357     ozaki 
    589  1.231    dyoung 		rtinit(ifa, RTM_LLINFO_UPD, 0);
    590  1.354     ozaki 
    591  1.357     ozaki 		ss = pserialize_read_enter();
    592  1.357     ozaki 		ifa_release(ifa, &psref);
    593  1.357     ozaki 	}
    594  1.357     ozaki 	pserialize_read_exit(ss);
    595  1.357     ozaki 
    596  1.223    dyoung 	splx(s);
    597  1.357     ozaki 	curlwp_bindx(bound);
    598   1.81   thorpej }
    599   1.81   thorpej 
    600   1.81   thorpej /*
    601   1.81   thorpej  * Free the link level name for the specified interface.  This is
    602  1.300     ozaki  * a detach helper.  This is called from if_detach().
    603   1.81   thorpej  */
    604  1.437  knakahar void
    605  1.426     ozaki if_free_sadl(struct ifnet *ifp, int factory)
    606   1.81   thorpej {
    607   1.81   thorpej 	struct ifaddr *ifa;
    608   1.81   thorpej 	int s;
    609   1.81   thorpej 
    610  1.426     ozaki 	if (factory && ifp->if_hwdl != NULL) {
    611  1.426     ozaki 		ifa = ifp->if_hwdl;
    612  1.426     ozaki 		ifp->if_hwdl = NULL;
    613  1.426     ozaki 		ifafree(ifa);
    614  1.426     ozaki 	}
    615  1.426     ozaki 
    616  1.316     ozaki 	ifa = ifp->if_dl;
    617   1.81   thorpej 	if (ifa == NULL) {
    618   1.81   thorpej 		KASSERT(ifp->if_sadl == NULL);
    619   1.81   thorpej 		return;
    620   1.81   thorpej 	}
    621   1.81   thorpej 
    622   1.81   thorpej 	KASSERT(ifp->if_sadl != NULL);
    623   1.81   thorpej 
    624  1.373     ozaki 	s = splsoftnet();
    625  1.430  christos 	KASSERT(ifa->ifa_addr->sa_family == AF_LINK);
    626  1.207    dyoung 	ifa_remove(ifp, ifa);
    627  1.223    dyoung 	if_deactivate_sadl(ifp);
    628   1.81   thorpej 	splx(s);
    629   1.81   thorpej }
    630   1.81   thorpej 
    631  1.274     rmind static void
    632  1.274     rmind if_getindex(ifnet_t *ifp)
    633    1.1       cgd {
    634  1.274     rmind 	bool hitlimit = false;
    635  1.492  yamaguch 	char xnamebuf[HOOKNAMSIZ];
    636  1.231    dyoung 
    637  1.234    dyoung 	ifp->if_index_gen = index_gen++;
    638  1.492  yamaguch 	snprintf(xnamebuf, sizeof(xnamebuf),
    639  1.492  yamaguch 	    "%s-lshk", ifp->if_xname);
    640  1.492  yamaguch 	ifp->if_linkstate_hooks = simplehook_create(IPL_NET,
    641  1.492  yamaguch 	    xnamebuf);
    642  1.234    dyoung 
    643  1.102    atatat 	ifp->if_index = if_index;
    644  1.274     rmind 	if (ifindex2ifnet == NULL) {
    645  1.102    atatat 		if_index++;
    646  1.274     rmind 		goto skip;
    647  1.274     rmind 	}
    648  1.274     rmind 	while (if_byindex(ifp->if_index)) {
    649  1.274     rmind 		/*
    650  1.274     rmind 		 * If we hit USHRT_MAX, we skip back to 0 since
    651  1.274     rmind 		 * there are a number of places where the value
    652  1.274     rmind 		 * of if_index or if_index itself is compared
    653  1.274     rmind 		 * to or stored in an unsigned short.  By
    654  1.274     rmind 		 * jumping back, we won't botch those assignments
    655  1.274     rmind 		 * or comparisons.
    656  1.274     rmind 		 */
    657  1.274     rmind 		if (++if_index == 0) {
    658  1.274     rmind 			if_index = 1;
    659  1.274     rmind 		} else if (if_index == USHRT_MAX) {
    660  1.102    atatat 			/*
    661  1.274     rmind 			 * However, if we have to jump back to
    662  1.274     rmind 			 * zero *twice* without finding an empty
    663  1.274     rmind 			 * slot in ifindex2ifnet[], then there
    664  1.274     rmind 			 * there are too many (>65535) interfaces.
    665  1.102    atatat 			 */
    666  1.274     rmind 			if (hitlimit) {
    667  1.274     rmind 				panic("too many interfaces");
    668  1.102    atatat 			}
    669  1.274     rmind 			hitlimit = true;
    670  1.274     rmind 			if_index = 1;
    671  1.102    atatat 		}
    672  1.274     rmind 		ifp->if_index = if_index;
    673  1.274     rmind 	}
    674  1.274     rmind skip:
    675   1.49    itojun 	/*
    676  1.316     ozaki 	 * ifindex2ifnet is indexed by if_index. Since if_index will
    677  1.316     ozaki 	 * grow dynamically, it should grow too.
    678   1.49    itojun 	 */
    679  1.316     ozaki 	if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
    680  1.131    itojun 		size_t m, n, oldlim;
    681  1.183  christos 		void *q;
    682  1.154     perry 
    683  1.131    itojun 		oldlim = if_indexlim;
    684   1.53   thorpej 		while (ifp->if_index >= if_indexlim)
    685   1.49    itojun 			if_indexlim <<= 1;
    686   1.49    itojun 
    687   1.49    itojun 		/* grow ifindex2ifnet */
    688  1.131    itojun 		m = oldlim * sizeof(struct ifnet *);
    689   1.49    itojun 		n = if_indexlim * sizeof(struct ifnet *);
    690  1.455   msaitoh 		q = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
    691  1.185    dyoung 		if (ifindex2ifnet != NULL) {
    692  1.230    dyoung 			memcpy(q, ifindex2ifnet, m);
    693  1.230    dyoung 			free(ifindex2ifnet, M_IFADDR);
    694   1.49    itojun 		}
    695   1.49    itojun 		ifindex2ifnet = (struct ifnet **)q;
    696    1.1       cgd 	}
    697  1.274     rmind 	ifindex2ifnet[ifp->if_index] = ifp;
    698  1.274     rmind }
    699  1.274     rmind 
    700  1.274     rmind /*
    701  1.307     ozaki  * Initialize an interface and assign an index for it.
    702  1.307     ozaki  *
    703  1.307     ozaki  * It must be called prior to a device specific attach routine
    704  1.307     ozaki  * (e.g., ether_ifattach and ieee80211_ifattach) or if_alloc_sadl,
    705  1.307     ozaki  * and be followed by if_register:
    706  1.307     ozaki  *
    707  1.307     ozaki  *     if_initialize(ifp);
    708  1.307     ozaki  *     ether_ifattach(ifp, enaddr);
    709  1.307     ozaki  *     if_register(ifp);
    710  1.274     rmind  */
    711  1.486  riastrad void
    712  1.307     ozaki if_initialize(ifnet_t *ifp)
    713  1.274     rmind {
    714  1.396   msaitoh 
    715  1.274     rmind 	KASSERT(if_indexlim > 0);
    716  1.274     rmind 	TAILQ_INIT(&ifp->if_addrlist);
    717   1.49    itojun 
    718    1.1       cgd 	/*
    719   1.81   thorpej 	 * Link level name is allocated later by a separate call to
    720   1.81   thorpej 	 * if_alloc_sadl().
    721    1.1       cgd 	 */
    722   1.81   thorpej 
    723   1.40   thorpej 	if (ifp->if_snd.ifq_maxlen == 0)
    724   1.94    itojun 		ifp->if_snd.ifq_maxlen = ifqmaxlen;
    725  1.234    dyoung 
    726   1.42        is 	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
    727   1.57   thorpej 
    728   1.57   thorpej 	ifp->if_link_state = LINK_STATE_UNKNOWN;
    729  1.325       roy 	ifp->if_link_queue = -1; /* all bits set, see link_state_change() */
    730  1.480       roy 	ifp->if_link_scheduled = false;
    731   1.57   thorpej 
    732   1.89   thorpej 	ifp->if_capenable = 0;
    733   1.97   thorpej 	ifp->if_csum_flags_tx = 0;
    734   1.97   thorpej 	ifp->if_csum_flags_rx = 0;
    735   1.89   thorpej 
    736   1.86   thorpej #ifdef ALTQ
    737   1.86   thorpej 	ifp->if_snd.altq_type = 0;
    738   1.86   thorpej 	ifp->if_snd.altq_disc = NULL;
    739   1.86   thorpej 	ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
    740   1.86   thorpej 	ifp->if_snd.altq_tbr  = NULL;
    741   1.86   thorpej 	ifp->if_snd.altq_ifp  = ifp;
    742   1.86   thorpej #endif
    743   1.86   thorpej 
    744  1.358     ozaki 	IFQ_LOCK_INIT(&ifp->if_snd);
    745  1.285     ozaki 
    746  1.265     rmind 	ifp->if_pfil = pfil_head_create(PFIL_TYPE_IFNET, ifp);
    747  1.369  christos 	pfil_run_ifhooks(if_pfil, PFIL_IFNET_ATTACH, ifp);
    748   1.87   thorpej 
    749  1.318     ozaki 	IF_AFDATA_LOCK_INIT(ifp);
    750  1.318     ozaki 
    751  1.334     ozaki 	PSLIST_ENTRY_INIT(ifp, if_pslist_entry);
    752  1.354     ozaki 	PSLIST_INIT(&ifp->if_addr_pslist);
    753  1.334     ozaki 	psref_target_init(&ifp->if_psref, ifnet_psref_class);
    754  1.336     ozaki 	ifp->if_ioctl_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    755  1.381     ozaki 	LIST_INIT(&ifp->if_multiaddrs);
    756  1.486  riastrad 	if_stats_init(ifp);
    757  1.334     ozaki 
    758  1.412     ozaki 	IFNET_GLOBAL_LOCK();
    759  1.307     ozaki 	if_getindex(ifp);
    760  1.412     ozaki 	IFNET_GLOBAL_UNLOCK();
    761  1.307     ozaki }
    762  1.307     ozaki 
    763  1.307     ozaki /*
    764  1.307     ozaki  * Register an interface to the list of "active" interfaces.
    765  1.307     ozaki  */
    766  1.307     ozaki void
    767  1.307     ozaki if_register(ifnet_t *ifp)
    768  1.307     ozaki {
    769  1.336     ozaki 	/*
    770  1.487     blymn 	 * If the driver has not supplied its own if_ioctl or if_stop,
    771  1.487     blymn 	 * then supply the default.
    772  1.336     ozaki 	 */
    773  1.336     ozaki 	if (ifp->if_ioctl == NULL)
    774  1.336     ozaki 		ifp->if_ioctl = ifioctl_common;
    775  1.487     blymn 	if (ifp->if_stop == NULL)
    776  1.487     blymn 		ifp->if_stop = if_nullstop;
    777  1.307     ozaki 
    778  1.307     ozaki 	sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd);
    779  1.307     ozaki 
    780  1.152      matt 	if (!STAILQ_EMPTY(&domains))
    781  1.147      tron 		if_attachdomain1(ifp);
    782  1.147      tron 
    783  1.107    itojun 	/* Announce the interface. */
    784  1.107    itojun 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
    785  1.296     ozaki 
    786  1.296     ozaki 	if (ifp->if_slowtimo != NULL) {
    787  1.297     ozaki 		ifp->if_slowtimo_ch =
    788  1.297     ozaki 		    kmem_zalloc(sizeof(*ifp->if_slowtimo_ch), KM_SLEEP);
    789  1.411     ozaki 		callout_init(ifp->if_slowtimo_ch, 0);
    790  1.297     ozaki 		callout_setfunc(ifp->if_slowtimo_ch, if_slowtimo, ifp);
    791  1.296     ozaki 		if_slowtimo(ifp);
    792  1.296     ozaki 	}
    793  1.307     ozaki 
    794  1.332  knakahar 	if (ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit)
    795  1.332  knakahar 		ifp->if_transmit = if_transmit;
    796  1.332  knakahar 
    797  1.412     ozaki 	IFNET_GLOBAL_LOCK();
    798  1.307     ozaki 	TAILQ_INSERT_TAIL(&ifnet_list, ifp, if_list);
    799  1.334     ozaki 	IFNET_WRITER_INSERT_TAIL(ifp);
    800  1.412     ozaki 	IFNET_GLOBAL_UNLOCK();
    801  1.307     ozaki }
    802  1.307     ozaki 
    803  1.307     ozaki /*
    804  1.323     ozaki  * The if_percpuq framework
    805  1.323     ozaki  *
    806  1.323     ozaki  * It allows network device drivers to execute the network stack
    807  1.323     ozaki  * in softint (so called softint-based if_input). It utilizes
    808  1.323     ozaki  * softint and percpu ifqueue. It doesn't distribute any packets
    809  1.323     ozaki  * between CPUs, unlike pktqueue(9).
    810  1.323     ozaki  *
    811  1.323     ozaki  * Currently we support two options for device drivers to apply the framework:
    812  1.323     ozaki  * - Use it implicitly with less changes
    813  1.323     ozaki  *   - If you use if_attach in driver's _attach function and if_input in
    814  1.323     ozaki  *     driver's Rx interrupt handler, a packet is queued and a softint handles
    815  1.323     ozaki  *     the packet implicitly
    816  1.323     ozaki  * - Use it explicitly in each driver (recommended)
    817  1.323     ozaki  *   - You can use if_percpuq_* directly in your driver
    818  1.323     ozaki  *   - In this case, you need to allocate struct if_percpuq in driver's softc
    819  1.323     ozaki  *   - See wm(4) as a reference implementation
    820  1.323     ozaki  */
    821  1.323     ozaki 
    822  1.323     ozaki static void
    823  1.323     ozaki if_percpuq_softint(void *arg)
    824  1.323     ozaki {
    825  1.323     ozaki 	struct if_percpuq *ipq = arg;
    826  1.323     ozaki 	struct ifnet *ifp = ipq->ipq_ifp;
    827  1.323     ozaki 	struct mbuf *m;
    828  1.323     ozaki 
    829  1.368     ozaki 	while ((m = if_percpuq_dequeue(ipq)) != NULL) {
    830  1.469   thorpej 		if_statinc(ifp, if_ipackets);
    831  1.428   msaitoh 		bpf_mtap(ifp, m, BPF_D_IN);
    832  1.368     ozaki 
    833  1.323     ozaki 		ifp->_if_input(ifp, m);
    834  1.368     ozaki 	}
    835  1.323     ozaki }
    836  1.323     ozaki 
    837  1.323     ozaki static void
    838  1.323     ozaki if_percpuq_init_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
    839  1.323     ozaki {
    840  1.323     ozaki 	struct ifqueue *const ifq = p;
    841  1.323     ozaki 
    842  1.323     ozaki 	memset(ifq, 0, sizeof(*ifq));
    843  1.323     ozaki 	ifq->ifq_maxlen = IFQ_MAXLEN;
    844  1.323     ozaki }
    845  1.323     ozaki 
    846  1.323     ozaki struct if_percpuq *
    847  1.323     ozaki if_percpuq_create(struct ifnet *ifp)
    848  1.323     ozaki {
    849  1.323     ozaki 	struct if_percpuq *ipq;
    850  1.422     ozaki 	u_int flags = SOFTINT_NET;
    851  1.422     ozaki 
    852  1.422     ozaki 	flags |= if_is_mpsafe(ifp) ? SOFTINT_MPSAFE : 0;
    853  1.323     ozaki 
    854  1.323     ozaki 	ipq = kmem_zalloc(sizeof(*ipq), KM_SLEEP);
    855  1.323     ozaki 	ipq->ipq_ifp = ifp;
    856  1.422     ozaki 	ipq->ipq_si = softint_establish(flags, if_percpuq_softint, ipq);
    857  1.323     ozaki 	ipq->ipq_ifqs = percpu_alloc(sizeof(struct ifqueue));
    858  1.323     ozaki 	percpu_foreach(ipq->ipq_ifqs, &if_percpuq_init_ifq, NULL);
    859  1.323     ozaki 
    860  1.327  knakahar 	sysctl_percpuq_setup(&ifp->if_sysctl_log, ifp->if_xname, ipq);
    861  1.327  knakahar 
    862  1.323     ozaki 	return ipq;
    863  1.323     ozaki }
    864  1.323     ozaki 
    865  1.323     ozaki static struct mbuf *
    866  1.323     ozaki if_percpuq_dequeue(struct if_percpuq *ipq)
    867  1.323     ozaki {
    868  1.323     ozaki 	struct mbuf *m;
    869  1.323     ozaki 	struct ifqueue *ifq;
    870  1.323     ozaki 	int s;
    871  1.323     ozaki 
    872  1.323     ozaki 	s = splnet();
    873  1.323     ozaki 	ifq = percpu_getref(ipq->ipq_ifqs);
    874  1.323     ozaki 	IF_DEQUEUE(ifq, m);
    875  1.323     ozaki 	percpu_putref(ipq->ipq_ifqs);
    876  1.323     ozaki 	splx(s);
    877  1.323     ozaki 
    878  1.323     ozaki 	return m;
    879  1.323     ozaki }
    880  1.323     ozaki 
    881  1.323     ozaki static void
    882  1.323     ozaki if_percpuq_purge_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
    883  1.323     ozaki {
    884  1.323     ozaki 	struct ifqueue *const ifq = p;
    885  1.323     ozaki 
    886  1.323     ozaki 	IF_PURGE(ifq);
    887  1.323     ozaki }
    888  1.323     ozaki 
    889  1.323     ozaki void
    890  1.323     ozaki if_percpuq_destroy(struct if_percpuq *ipq)
    891  1.323     ozaki {
    892  1.323     ozaki 
    893  1.323     ozaki 	/* if_detach may already destroy it */
    894  1.323     ozaki 	if (ipq == NULL)
    895  1.323     ozaki 		return;
    896  1.323     ozaki 
    897  1.323     ozaki 	softint_disestablish(ipq->ipq_si);
    898  1.323     ozaki 	percpu_foreach(ipq->ipq_ifqs, &if_percpuq_purge_ifq, NULL);
    899  1.323     ozaki 	percpu_free(ipq->ipq_ifqs, sizeof(struct ifqueue));
    900  1.363     ozaki 	kmem_free(ipq, sizeof(*ipq));
    901  1.323     ozaki }
    902  1.323     ozaki 
    903  1.323     ozaki void
    904  1.323     ozaki if_percpuq_enqueue(struct if_percpuq *ipq, struct mbuf *m)
    905  1.323     ozaki {
    906  1.323     ozaki 	struct ifqueue *ifq;
    907  1.323     ozaki 	int s;
    908  1.323     ozaki 
    909  1.323     ozaki 	KASSERT(ipq != NULL);
    910  1.323     ozaki 
    911  1.323     ozaki 	s = splnet();
    912  1.323     ozaki 	ifq = percpu_getref(ipq->ipq_ifqs);
    913  1.323     ozaki 	if (IF_QFULL(ifq)) {
    914  1.323     ozaki 		IF_DROP(ifq);
    915  1.326     ozaki 		percpu_putref(ipq->ipq_ifqs);
    916  1.323     ozaki 		m_freem(m);
    917  1.323     ozaki 		goto out;
    918  1.323     ozaki 	}
    919  1.323     ozaki 	IF_ENQUEUE(ifq, m);
    920  1.323     ozaki 	percpu_putref(ipq->ipq_ifqs);
    921  1.323     ozaki 
    922  1.323     ozaki 	softint_schedule(ipq->ipq_si);
    923  1.323     ozaki out:
    924  1.323     ozaki 	splx(s);
    925  1.323     ozaki }
    926  1.323     ozaki 
    927  1.327  knakahar static void
    928  1.327  knakahar if_percpuq_drops(void *p, void *arg, struct cpu_info *ci __unused)
    929  1.327  knakahar {
    930  1.327  knakahar 	struct ifqueue *const ifq = p;
    931  1.327  knakahar 	int *sum = arg;
    932  1.327  knakahar 
    933  1.327  knakahar 	*sum += ifq->ifq_drops;
    934  1.327  knakahar }
    935  1.327  knakahar 
    936  1.327  knakahar static int
    937  1.327  knakahar sysctl_percpuq_drops_handler(SYSCTLFN_ARGS)
    938  1.327  knakahar {
    939  1.327  knakahar 	struct sysctlnode node;
    940  1.327  knakahar 	struct if_percpuq *ipq;
    941  1.327  knakahar 	int sum = 0;
    942  1.327  knakahar 	int error;
    943  1.327  knakahar 
    944  1.327  knakahar 	node = *rnode;
    945  1.327  knakahar 	ipq = node.sysctl_data;
    946  1.327  knakahar 
    947  1.327  knakahar 	percpu_foreach(ipq->ipq_ifqs, if_percpuq_drops, &sum);
    948  1.327  knakahar 
    949  1.327  knakahar 	node.sysctl_data = &sum;
    950  1.327  knakahar 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    951  1.327  knakahar 	if (error != 0 || newp == NULL)
    952  1.327  knakahar 		return error;
    953  1.327  knakahar 
    954  1.327  knakahar 	return 0;
    955  1.327  knakahar }
    956  1.327  knakahar 
    957  1.327  knakahar static void
    958  1.327  knakahar sysctl_percpuq_setup(struct sysctllog **clog, const char* ifname,
    959  1.327  knakahar     struct if_percpuq *ipq)
    960  1.327  knakahar {
    961  1.327  knakahar 	const struct sysctlnode *cnode, *rnode;
    962  1.327  knakahar 
    963  1.327  knakahar 	if (sysctl_createv(clog, 0, NULL, &rnode,
    964  1.327  knakahar 		       CTLFLAG_PERMANENT,
    965  1.327  knakahar 		       CTLTYPE_NODE, "interfaces",
    966  1.327  knakahar 		       SYSCTL_DESCR("Per-interface controls"),
    967  1.327  knakahar 		       NULL, 0, NULL, 0,
    968  1.327  knakahar 		       CTL_NET, CTL_CREATE, CTL_EOL) != 0)
    969  1.327  knakahar 		goto bad;
    970  1.327  knakahar 
    971  1.327  knakahar 	if (sysctl_createv(clog, 0, &rnode, &rnode,
    972  1.327  knakahar 		       CTLFLAG_PERMANENT,
    973  1.327  knakahar 		       CTLTYPE_NODE, ifname,
    974  1.327  knakahar 		       SYSCTL_DESCR("Interface controls"),
    975  1.327  knakahar 		       NULL, 0, NULL, 0,
    976  1.327  knakahar 		       CTL_CREATE, CTL_EOL) != 0)
    977  1.327  knakahar 		goto bad;
    978  1.327  knakahar 
    979  1.327  knakahar 	if (sysctl_createv(clog, 0, &rnode, &rnode,
    980  1.327  knakahar 		       CTLFLAG_PERMANENT,
    981  1.327  knakahar 		       CTLTYPE_NODE, "rcvq",
    982  1.327  knakahar 		       SYSCTL_DESCR("Interface input queue controls"),
    983  1.327  knakahar 		       NULL, 0, NULL, 0,
    984  1.327  knakahar 		       CTL_CREATE, CTL_EOL) != 0)
    985  1.327  knakahar 		goto bad;
    986  1.327  knakahar 
    987  1.327  knakahar #ifdef NOTYET
    988  1.327  knakahar 	/* XXX Should show each per-CPU queue length? */
    989  1.327  knakahar 	if (sysctl_createv(clog, 0, &rnode, &rnode,
    990  1.327  knakahar 		       CTLFLAG_PERMANENT,
    991  1.327  knakahar 		       CTLTYPE_INT, "len",
    992  1.327  knakahar 		       SYSCTL_DESCR("Current input queue length"),
    993  1.327  knakahar 		       sysctl_percpuq_len, 0, NULL, 0,
    994  1.327  knakahar 		       CTL_CREATE, CTL_EOL) != 0)
    995  1.327  knakahar 		goto bad;
    996  1.327  knakahar 
    997  1.327  knakahar 	if (sysctl_createv(clog, 0, &rnode, &cnode,
    998  1.455   msaitoh 		       CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    999  1.327  knakahar 		       CTLTYPE_INT, "maxlen",
   1000  1.327  knakahar 		       SYSCTL_DESCR("Maximum allowed input queue length"),
   1001  1.327  knakahar 		       sysctl_percpuq_maxlen_handler, 0, (void *)ipq, 0,
   1002  1.327  knakahar 		       CTL_CREATE, CTL_EOL) != 0)
   1003  1.327  knakahar 		goto bad;
   1004  1.327  knakahar #endif
   1005  1.327  knakahar 
   1006  1.327  knakahar 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   1007  1.327  knakahar 		       CTLFLAG_PERMANENT,
   1008  1.327  knakahar 		       CTLTYPE_INT, "drops",
   1009  1.327  knakahar 		       SYSCTL_DESCR("Total packets dropped due to full input queue"),
   1010  1.327  knakahar 		       sysctl_percpuq_drops_handler, 0, (void *)ipq, 0,
   1011  1.327  knakahar 		       CTL_CREATE, CTL_EOL) != 0)
   1012  1.327  knakahar 		goto bad;
   1013  1.327  knakahar 
   1014  1.327  knakahar 	return;
   1015  1.327  knakahar bad:
   1016  1.327  knakahar 	printf("%s: could not attach sysctl nodes\n", ifname);
   1017  1.327  knakahar 	return;
   1018  1.327  knakahar }
   1019  1.327  knakahar 
   1020  1.364     ozaki /*
   1021  1.364     ozaki  * The deferred if_start framework
   1022  1.364     ozaki  *
   1023  1.364     ozaki  * The common APIs to defer if_start to softint when if_start is requested
   1024  1.364     ozaki  * from a device driver running in hardware interrupt context.
   1025  1.364     ozaki  */
   1026  1.364     ozaki /*
   1027  1.364     ozaki  * Call ifp->if_start (or equivalent) in a dedicated softint for
   1028  1.364     ozaki  * deferred if_start.
   1029  1.364     ozaki  */
   1030  1.364     ozaki static void
   1031  1.364     ozaki if_deferred_start_softint(void *arg)
   1032  1.364     ozaki {
   1033  1.364     ozaki 	struct if_deferred_start *ids = arg;
   1034  1.364     ozaki 	struct ifnet *ifp = ids->ids_ifp;
   1035  1.364     ozaki 
   1036  1.364     ozaki 	ids->ids_if_start(ifp);
   1037  1.364     ozaki }
   1038  1.364     ozaki 
   1039  1.364     ozaki /*
   1040  1.364     ozaki  * The default callback function for deferred if_start.
   1041  1.364     ozaki  */
   1042  1.364     ozaki static void
   1043  1.364     ozaki if_deferred_start_common(struct ifnet *ifp)
   1044  1.364     ozaki {
   1045  1.382     ozaki 	int s;
   1046  1.364     ozaki 
   1047  1.382     ozaki 	s = splnet();
   1048  1.364     ozaki 	if_start_lock(ifp);
   1049  1.382     ozaki 	splx(s);
   1050  1.364     ozaki }
   1051  1.364     ozaki 
   1052  1.364     ozaki static inline bool
   1053  1.364     ozaki if_snd_is_used(struct ifnet *ifp)
   1054  1.364     ozaki {
   1055  1.364     ozaki 
   1056  1.457  knakahar 	return ALTQ_IS_ENABLED(&ifp->if_snd) ||
   1057  1.457  knakahar 		ifp->if_transmit == if_transmit ||
   1058  1.457  knakahar 		ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit;
   1059  1.364     ozaki }
   1060  1.364     ozaki 
   1061  1.364     ozaki /*
   1062  1.364     ozaki  * Schedule deferred if_start.
   1063  1.364     ozaki  */
   1064  1.364     ozaki void
   1065  1.364     ozaki if_schedule_deferred_start(struct ifnet *ifp)
   1066  1.364     ozaki {
   1067  1.364     ozaki 
   1068  1.364     ozaki 	KASSERT(ifp->if_deferred_start != NULL);
   1069  1.364     ozaki 
   1070  1.364     ozaki 	if (if_snd_is_used(ifp) && IFQ_IS_EMPTY(&ifp->if_snd))
   1071  1.364     ozaki 		return;
   1072  1.364     ozaki 
   1073  1.364     ozaki 	softint_schedule(ifp->if_deferred_start->ids_si);
   1074  1.364     ozaki }
   1075  1.364     ozaki 
   1076  1.364     ozaki /*
   1077  1.364     ozaki  * Create an instance of deferred if_start. A driver should call the function
   1078  1.364     ozaki  * only if the driver needs deferred if_start. Drivers can setup their own
   1079  1.364     ozaki  * deferred if_start function via 2nd argument.
   1080  1.364     ozaki  */
   1081  1.364     ozaki void
   1082  1.364     ozaki if_deferred_start_init(struct ifnet *ifp, void (*func)(struct ifnet *))
   1083  1.364     ozaki {
   1084  1.364     ozaki 	struct if_deferred_start *ids;
   1085  1.423     ozaki 	u_int flags = SOFTINT_NET;
   1086  1.423     ozaki 
   1087  1.423     ozaki 	flags |= if_is_mpsafe(ifp) ? SOFTINT_MPSAFE : 0;
   1088  1.364     ozaki 
   1089  1.364     ozaki 	ids = kmem_zalloc(sizeof(*ids), KM_SLEEP);
   1090  1.364     ozaki 	ids->ids_ifp = ifp;
   1091  1.423     ozaki 	ids->ids_si = softint_establish(flags, if_deferred_start_softint, ids);
   1092  1.364     ozaki 	if (func != NULL)
   1093  1.364     ozaki 		ids->ids_if_start = func;
   1094  1.364     ozaki 	else
   1095  1.364     ozaki 		ids->ids_if_start = if_deferred_start_common;
   1096  1.364     ozaki 
   1097  1.364     ozaki 	ifp->if_deferred_start = ids;
   1098  1.364     ozaki }
   1099  1.364     ozaki 
   1100  1.364     ozaki static void
   1101  1.364     ozaki if_deferred_start_destroy(struct ifnet *ifp)
   1102  1.364     ozaki {
   1103  1.364     ozaki 
   1104  1.364     ozaki 	if (ifp->if_deferred_start == NULL)
   1105  1.364     ozaki 		return;
   1106  1.364     ozaki 
   1107  1.364     ozaki 	softint_disestablish(ifp->if_deferred_start->ids_si);
   1108  1.364     ozaki 	kmem_free(ifp->if_deferred_start, sizeof(*ifp->if_deferred_start));
   1109  1.364     ozaki 	ifp->if_deferred_start = NULL;
   1110  1.364     ozaki }
   1111  1.327  knakahar 
   1112  1.323     ozaki /*
   1113  1.323     ozaki  * The common interface input routine that is called by device drivers,
   1114  1.323     ozaki  * which should be used only when the driver's rx handler already runs
   1115  1.323     ozaki  * in softint.
   1116  1.323     ozaki  */
   1117  1.323     ozaki void
   1118  1.323     ozaki if_input(struct ifnet *ifp, struct mbuf *m)
   1119  1.323     ozaki {
   1120  1.323     ozaki 
   1121  1.323     ozaki 	KASSERT(ifp->if_percpuq == NULL);
   1122  1.323     ozaki 	KASSERT(!cpu_intr_p());
   1123  1.323     ozaki 
   1124  1.469   thorpej 	if_statinc(ifp, if_ipackets);
   1125  1.428   msaitoh 	bpf_mtap(ifp, m, BPF_D_IN);
   1126  1.368     ozaki 
   1127  1.323     ozaki 	ifp->_if_input(ifp, m);
   1128  1.323     ozaki }
   1129  1.323     ozaki 
   1130  1.323     ozaki /*
   1131  1.323     ozaki  * DEPRECATED. Use if_initialize and if_register instead.
   1132  1.307     ozaki  * See the above comment of if_initialize.
   1133  1.323     ozaki  *
   1134  1.323     ozaki  * Note that it implicitly enables if_percpuq to make drivers easy to
   1135  1.333     skrll  * migrate softint-based if_input without much changes. If you don't
   1136  1.323     ozaki  * want to enable it, use if_initialize instead.
   1137  1.307     ozaki  */
   1138  1.486  riastrad void
   1139  1.307     ozaki if_attach(ifnet_t *ifp)
   1140  1.307     ozaki {
   1141  1.323     ozaki 
   1142  1.486  riastrad 	if_initialize(ifp);
   1143  1.323     ozaki 	ifp->if_percpuq = if_percpuq_create(ifp);
   1144  1.307     ozaki 	if_register(ifp);
   1145  1.107    itojun }
   1146  1.107    itojun 
   1147  1.107    itojun void
   1148  1.163   thorpej if_attachdomain(void)
   1149  1.107    itojun {
   1150  1.107    itojun 	struct ifnet *ifp;
   1151  1.110    itojun 	int s;
   1152  1.339     ozaki 	int bound = curlwp_bind();
   1153  1.107    itojun 
   1154  1.334     ozaki 	s = pserialize_read_enter();
   1155  1.334     ozaki 	IFNET_READER_FOREACH(ifp) {
   1156  1.334     ozaki 		struct psref psref;
   1157  1.334     ozaki 		psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
   1158  1.334     ozaki 		pserialize_read_exit(s);
   1159  1.107    itojun 		if_attachdomain1(ifp);
   1160  1.334     ozaki 		s = pserialize_read_enter();
   1161  1.334     ozaki 		psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
   1162  1.334     ozaki 	}
   1163  1.334     ozaki 	pserialize_read_exit(s);
   1164  1.339     ozaki 	curlwp_bindx(bound);
   1165  1.107    itojun }
   1166  1.107    itojun 
   1167  1.302     ozaki static void
   1168  1.163   thorpej if_attachdomain1(struct ifnet *ifp)
   1169  1.107    itojun {
   1170  1.107    itojun 	struct domain *dp;
   1171  1.109    itojun 	int s;
   1172  1.109    itojun 
   1173  1.373     ozaki 	s = splsoftnet();
   1174  1.107    itojun 
   1175  1.106    itojun 	/* address family dependent data region */
   1176  1.106    itojun 	memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
   1177  1.152      matt 	DOMAIN_FOREACH(dp) {
   1178  1.185    dyoung 		if (dp->dom_ifattach != NULL)
   1179  1.106    itojun 			ifp->if_afdata[dp->dom_family] =
   1180  1.106    itojun 			    (*dp->dom_ifattach)(ifp);
   1181  1.106    itojun 	}
   1182  1.109    itojun 
   1183  1.109    itojun 	splx(s);
   1184    1.1       cgd }
   1185   1.53   thorpej 
   1186   1.53   thorpej /*
   1187   1.53   thorpej  * Deactivate an interface.  This points all of the procedure
   1188   1.53   thorpej  * handles at error stubs.  May be called from interrupt context.
   1189   1.53   thorpej  */
   1190   1.53   thorpej void
   1191  1.163   thorpej if_deactivate(struct ifnet *ifp)
   1192   1.53   thorpej {
   1193   1.53   thorpej 	int s;
   1194   1.53   thorpej 
   1195  1.373     ozaki 	s = splsoftnet();
   1196   1.53   thorpej 
   1197   1.53   thorpej 	ifp->if_output	 = if_nulloutput;
   1198  1.323     ozaki 	ifp->_if_input	 = if_nullinput;
   1199   1.53   thorpej 	ifp->if_start	 = if_nullstart;
   1200  1.332  knakahar 	ifp->if_transmit = if_nulltransmit;
   1201   1.53   thorpej 	ifp->if_ioctl	 = if_nullioctl;
   1202   1.75   thorpej 	ifp->if_init	 = if_nullinit;
   1203   1.75   thorpej 	ifp->if_stop	 = if_nullstop;
   1204  1.295     ozaki 	ifp->if_slowtimo = if_nullslowtimo;
   1205   1.53   thorpej 	ifp->if_drain	 = if_nulldrain;
   1206   1.53   thorpej 
   1207  1.481       roy 	ifp->if_link_state_changed = NULL;
   1208  1.481       roy 
   1209   1.53   thorpej 	/* No more packets may be enqueued. */
   1210   1.53   thorpej 	ifp->if_snd.ifq_maxlen = 0;
   1211   1.53   thorpej 
   1212   1.53   thorpej 	splx(s);
   1213   1.53   thorpej }
   1214   1.53   thorpej 
   1215  1.348     ozaki bool
   1216  1.367     ozaki if_is_deactivated(const struct ifnet *ifp)
   1217  1.348     ozaki {
   1218  1.348     ozaki 
   1219  1.348     ozaki 	return ifp->if_output == if_nulloutput;
   1220  1.348     ozaki }
   1221  1.348     ozaki 
   1222  1.206    dyoung void
   1223  1.218    dyoung if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *))
   1224  1.206    dyoung {
   1225  1.289     ozaki 	struct ifaddr *ifa, *nifa;
   1226  1.357     ozaki 	int s;
   1227  1.206    dyoung 
   1228  1.357     ozaki 	s = pserialize_read_enter();
   1229  1.354     ozaki 	for (ifa = IFADDR_READER_FIRST(ifp); ifa; ifa = nifa) {
   1230  1.354     ozaki 		nifa = IFADDR_READER_NEXT(ifa);
   1231  1.206    dyoung 		if (ifa->ifa_addr->sa_family != family)
   1232  1.206    dyoung 			continue;
   1233  1.357     ozaki 		pserialize_read_exit(s);
   1234  1.357     ozaki 
   1235  1.206    dyoung 		(*purgeaddr)(ifa);
   1236  1.357     ozaki 
   1237  1.357     ozaki 		s = pserialize_read_enter();
   1238  1.206    dyoung 	}
   1239  1.357     ozaki 	pserialize_read_exit(s);
   1240  1.206    dyoung }
   1241  1.206    dyoung 
   1242  1.349     ozaki #ifdef IFAREF_DEBUG
   1243  1.349     ozaki static struct ifaddr **ifa_list;
   1244  1.349     ozaki static int ifa_list_size;
   1245  1.349     ozaki 
   1246  1.349     ozaki /* Depends on only one if_attach runs at once */
   1247  1.349     ozaki static void
   1248  1.349     ozaki if_build_ifa_list(struct ifnet *ifp)
   1249  1.349     ozaki {
   1250  1.349     ozaki 	struct ifaddr *ifa;
   1251  1.349     ozaki 	int i;
   1252  1.349     ozaki 
   1253  1.349     ozaki 	KASSERT(ifa_list == NULL);
   1254  1.349     ozaki 	KASSERT(ifa_list_size == 0);
   1255  1.349     ozaki 
   1256  1.354     ozaki 	IFADDR_READER_FOREACH(ifa, ifp)
   1257  1.349     ozaki 		ifa_list_size++;
   1258  1.349     ozaki 
   1259  1.349     ozaki 	ifa_list = kmem_alloc(sizeof(*ifa) * ifa_list_size, KM_SLEEP);
   1260  1.349     ozaki 	i = 0;
   1261  1.354     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
   1262  1.349     ozaki 		ifa_list[i++] = ifa;
   1263  1.349     ozaki 		ifaref(ifa);
   1264  1.349     ozaki 	}
   1265  1.349     ozaki }
   1266  1.349     ozaki 
   1267  1.349     ozaki static void
   1268  1.349     ozaki if_check_and_free_ifa_list(struct ifnet *ifp)
   1269  1.349     ozaki {
   1270  1.349     ozaki 	int i;
   1271  1.349     ozaki 	struct ifaddr *ifa;
   1272  1.349     ozaki 
   1273  1.349     ozaki 	if (ifa_list == NULL)
   1274  1.349     ozaki 		return;
   1275  1.349     ozaki 
   1276  1.349     ozaki 	for (i = 0; i < ifa_list_size; i++) {
   1277  1.349     ozaki 		char buf[64];
   1278  1.349     ozaki 
   1279  1.349     ozaki 		ifa = ifa_list[i];
   1280  1.349     ozaki 		sockaddr_format(ifa->ifa_addr, buf, sizeof(buf));
   1281  1.349     ozaki 		if (ifa->ifa_refcnt > 1) {
   1282  1.349     ozaki 			log(LOG_WARNING,
   1283  1.349     ozaki 			    "ifa(%s) still referenced (refcnt=%d)\n",
   1284  1.349     ozaki 			    buf, ifa->ifa_refcnt - 1);
   1285  1.349     ozaki 		} else
   1286  1.349     ozaki 			log(LOG_DEBUG,
   1287  1.349     ozaki 			    "ifa(%s) not referenced (refcnt=%d)\n",
   1288  1.349     ozaki 			    buf, ifa->ifa_refcnt - 1);
   1289  1.349     ozaki 		ifafree(ifa);
   1290  1.349     ozaki 	}
   1291  1.349     ozaki 
   1292  1.349     ozaki 	kmem_free(ifa_list, sizeof(*ifa) * ifa_list_size);
   1293  1.349     ozaki 	ifa_list = NULL;
   1294  1.349     ozaki 	ifa_list_size = 0;
   1295  1.349     ozaki }
   1296  1.349     ozaki #endif
   1297  1.349     ozaki 
   1298   1.53   thorpej /*
   1299   1.53   thorpej  * Detach an interface from the list of "active" interfaces,
   1300   1.53   thorpej  * freeing any resources as we go along.
   1301   1.53   thorpej  *
   1302   1.53   thorpej  * NOTE: This routine must be called with a valid thread context,
   1303   1.53   thorpej  * as it may block.
   1304   1.53   thorpej  */
   1305   1.53   thorpej void
   1306  1.163   thorpej if_detach(struct ifnet *ifp)
   1307   1.53   thorpej {
   1308   1.56   thorpej 	struct socket so;
   1309  1.178    dyoung 	struct ifaddr *ifa;
   1310   1.53   thorpej #ifdef IFAREF_DEBUG
   1311   1.53   thorpej 	struct ifaddr *last_ifa = NULL;
   1312   1.53   thorpej #endif
   1313   1.56   thorpej 	struct domain *dp;
   1314  1.141      matt 	const struct protosw *pr;
   1315  1.322  riastrad 	int s, i, family, purged;
   1316   1.53   thorpej 
   1317  1.349     ozaki #ifdef IFAREF_DEBUG
   1318  1.349     ozaki 	if_build_ifa_list(ifp);
   1319  1.349     ozaki #endif
   1320   1.56   thorpej 	/*
   1321   1.56   thorpej 	 * XXX It's kind of lame that we have to have the
   1322   1.56   thorpej 	 * XXX socket structure...
   1323   1.56   thorpej 	 */
   1324   1.56   thorpej 	memset(&so, 0, sizeof(so));
   1325   1.53   thorpej 
   1326   1.88   thorpej 	s = splnet();
   1327   1.53   thorpej 
   1328  1.334     ozaki 	sysctl_teardown(&ifp->if_sysctl_log);
   1329  1.480       roy 
   1330  1.413     ozaki 	IFNET_LOCK(ifp);
   1331  1.480       roy 
   1332  1.480       roy 	/*
   1333  1.480       roy 	 * Unset all queued link states and pretend a
   1334  1.480       roy 	 * link state change is scheduled.
   1335  1.488    andvar 	 * This stops any more link state changes occurring for this
   1336  1.480       roy 	 * interface while it's being detached so it's safe
   1337  1.480       roy 	 * to drain the workqueue.
   1338  1.480       roy 	 */
   1339  1.480       roy 	IF_LINK_STATE_CHANGE_LOCK(ifp);
   1340  1.480       roy 	ifp->if_link_queue = -1; /* all bits set, see link_state_change() */
   1341  1.480       roy 	ifp->if_link_scheduled = true;
   1342  1.480       roy 	IF_LINK_STATE_CHANGE_UNLOCK(ifp);
   1343  1.480       roy 	workqueue_wait(ifnet_link_state_wq, &ifp->if_link_work);
   1344  1.480       roy 
   1345  1.350     ozaki 	if_deactivate(ifp);
   1346  1.413     ozaki 	IFNET_UNLOCK(ifp);
   1347  1.334     ozaki 
   1348  1.436  christos 	/*
   1349  1.436  christos 	 * Unlink from the list and wait for all readers to leave
   1350  1.436  christos 	 * from pserialize read sections.  Note that we can't do
   1351  1.436  christos 	 * psref_target_destroy here.  See below.
   1352  1.436  christos 	 */
   1353  1.436  christos 	IFNET_GLOBAL_LOCK();
   1354  1.436  christos 	ifindex2ifnet[ifp->if_index] = NULL;
   1355  1.436  christos 	TAILQ_REMOVE(&ifnet_list, ifp, if_list);
   1356  1.436  christos 	IFNET_WRITER_REMOVE(ifp);
   1357  1.436  christos 	pserialize_perform(ifnet_psz);
   1358  1.436  christos 	IFNET_GLOBAL_UNLOCK();
   1359  1.436  christos 
   1360  1.350     ozaki 	if (ifp->if_slowtimo != NULL && ifp->if_slowtimo_ch != NULL) {
   1361  1.305    martin 		ifp->if_slowtimo = NULL;
   1362  1.297     ozaki 		callout_halt(ifp->if_slowtimo_ch, NULL);
   1363  1.297     ozaki 		callout_destroy(ifp->if_slowtimo_ch);
   1364  1.297     ozaki 		kmem_free(ifp->if_slowtimo_ch, sizeof(*ifp->if_slowtimo_ch));
   1365  1.296     ozaki 	}
   1366  1.364     ozaki 	if_deferred_start_destroy(ifp);
   1367  1.296     ozaki 
   1368   1.53   thorpej 	/*
   1369   1.53   thorpej 	 * Do an if_down() to give protocols a chance to do something.
   1370   1.53   thorpej 	 */
   1371  1.403     ozaki 	if_down_deactivated(ifp);
   1372   1.86   thorpej 
   1373   1.86   thorpej #ifdef ALTQ
   1374   1.86   thorpej 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
   1375   1.86   thorpej 		altq_disable(&ifp->if_snd);
   1376   1.86   thorpej 	if (ALTQ_IS_ATTACHED(&ifp->if_snd))
   1377   1.86   thorpej 		altq_detach(&ifp->if_snd);
   1378   1.87   thorpej #endif
   1379   1.87   thorpej 
   1380  1.166  liamjfoy #if NCARP > 0
   1381  1.166  liamjfoy 	/* Remove the interface from any carp group it is a part of.  */
   1382  1.185    dyoung 	if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
   1383  1.166  liamjfoy 		carp_ifdetach(ifp);
   1384  1.166  liamjfoy #endif
   1385  1.166  liamjfoy 
   1386   1.53   thorpej 	/*
   1387   1.53   thorpej 	 * Rip all the addresses off the interface.  This should make
   1388   1.53   thorpej 	 * all of the routes go away.
   1389  1.178    dyoung 	 *
   1390  1.178    dyoung 	 * pr_usrreq calls can remove an arbitrary number of ifaddrs
   1391  1.178    dyoung 	 * from the list, including our "cursor", ifa.  For safety,
   1392  1.178    dyoung 	 * and to honor the TAILQ abstraction, I just restart the
   1393  1.178    dyoung 	 * loop after each removal.  Note that the loop will exit
   1394  1.178    dyoung 	 * when all of the remaining ifaddrs belong to the AF_LINK
   1395  1.178    dyoung 	 * family.  I am counting on the historical fact that at
   1396  1.178    dyoung 	 * least one pr_usrreq in each address domain removes at
   1397  1.178    dyoung 	 * least one ifaddr.
   1398   1.53   thorpej 	 */
   1399  1.178    dyoung again:
   1400  1.357     ozaki 	/*
   1401  1.357     ozaki 	 * At this point, no other one tries to remove ifa in the list,
   1402  1.407     ozaki 	 * so we don't need to take a lock or psref.  Avoid using
   1403  1.407     ozaki 	 * IFADDR_READER_FOREACH to pass over an inspection of contract
   1404  1.407     ozaki 	 * violations of pserialize.
   1405  1.357     ozaki 	 */
   1406  1.407     ozaki 	IFADDR_WRITER_FOREACH(ifa, ifp) {
   1407   1.56   thorpej 		family = ifa->ifa_addr->sa_family;
   1408   1.53   thorpej #ifdef IFAREF_DEBUG
   1409   1.53   thorpej 		printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
   1410   1.56   thorpej 		    ifa, family, ifa->ifa_refcnt);
   1411   1.53   thorpej 		if (last_ifa != NULL && ifa == last_ifa)
   1412   1.56   thorpej 			panic("if_detach: loop detected");
   1413   1.53   thorpej 		last_ifa = ifa;
   1414   1.53   thorpej #endif
   1415  1.178    dyoung 		if (family == AF_LINK)
   1416  1.118    itojun 			continue;
   1417  1.118    itojun 		dp = pffinddomain(family);
   1418  1.384     ozaki 		KASSERTMSG(dp != NULL, "no domain for AF %d", family);
   1419  1.160       gdt 		/*
   1420  1.160       gdt 		 * XXX These PURGEIF calls are redundant with the
   1421  1.160       gdt 		 * purge-all-families calls below, but are left in for
   1422  1.160       gdt 		 * now both to make a smaller change, and to avoid
   1423  1.160       gdt 		 * unplanned interactions with clearing of
   1424  1.160       gdt 		 * ifp->if_addrlist.
   1425  1.160       gdt 		 */
   1426  1.118    itojun 		purged = 0;
   1427  1.322  riastrad 		for (pr = dp->dom_protosw;
   1428  1.322  riastrad 		     pr < dp->dom_protoswNPROTOSW; pr++) {
   1429  1.118    itojun 			so.so_proto = pr;
   1430  1.275     rmind 			if (pr->pr_usrreqs) {
   1431  1.290       rtr 				(void) (*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
   1432  1.118    itojun 				purged = 1;
   1433   1.53   thorpej 			}
   1434  1.118    itojun 		}
   1435  1.118    itojun 		if (purged == 0) {
   1436  1.118    itojun 			/*
   1437  1.118    itojun 			 * XXX What's really the best thing to do
   1438  1.135    keihan 			 * XXX here?  --thorpej (at) NetBSD.org
   1439  1.118    itojun 			 */
   1440  1.118    itojun 			printf("if_detach: WARNING: AF %d not purged\n",
   1441  1.118    itojun 			    family);
   1442  1.207    dyoung 			ifa_remove(ifp, ifa);
   1443   1.53   thorpej 		}
   1444  1.178    dyoung 		goto again;
   1445   1.53   thorpej 	}
   1446  1.118    itojun 
   1447  1.426     ozaki 	if_free_sadl(ifp, 1);
   1448   1.53   thorpej 
   1449  1.427     ozaki restart:
   1450  1.427     ozaki 	IFADDR_WRITER_FOREACH(ifa, ifp) {
   1451  1.427     ozaki 		family = ifa->ifa_addr->sa_family;
   1452  1.427     ozaki 		KASSERT(family == AF_LINK);
   1453  1.427     ozaki 		ifa_remove(ifp, ifa);
   1454  1.427     ozaki 		goto restart;
   1455  1.427     ozaki 	}
   1456  1.427     ozaki 
   1457  1.362     ozaki 	/* Delete stray routes from the routing table. */
   1458  1.362     ozaki 	for (i = 0; i <= AF_MAX; i++)
   1459  1.362     ozaki 		rt_delete_matched_entries(i, if_delroute_matcher, ifp);
   1460  1.106    itojun 
   1461  1.152      matt 	DOMAIN_FOREACH(dp) {
   1462  1.185    dyoung 		if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
   1463  1.260  christos 		{
   1464  1.260  christos 			void *p = ifp->if_afdata[dp->dom_family];
   1465  1.260  christos 			if (p) {
   1466  1.260  christos 				ifp->if_afdata[dp->dom_family] = NULL;
   1467  1.260  christos 				(*dp->dom_ifdetach)(ifp, p);
   1468  1.260  christos 			}
   1469  1.260  christos 		}
   1470  1.160       gdt 
   1471  1.160       gdt 		/*
   1472  1.160       gdt 		 * One would expect multicast memberships (INET and
   1473  1.160       gdt 		 * INET6) on UDP sockets to be purged by the PURGEIF
   1474  1.160       gdt 		 * calls above, but if all addresses were removed from
   1475  1.160       gdt 		 * the interface prior to destruction, the calls will
   1476  1.160       gdt 		 * not be made (e.g. ppp, for which pppd(8) generally
   1477  1.160       gdt 		 * removes addresses before destroying the interface).
   1478  1.160       gdt 		 * Because there is no invariant that multicast
   1479  1.160       gdt 		 * memberships only exist for interfaces with IPv4
   1480  1.160       gdt 		 * addresses, we must call PURGEIF regardless of
   1481  1.160       gdt 		 * addresses.  (Protocols which might store ifnet
   1482  1.160       gdt 		 * pointers are marked with PR_PURGEIF.)
   1483  1.160       gdt 		 */
   1484  1.185    dyoung 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
   1485  1.160       gdt 			so.so_proto = pr;
   1486  1.275     rmind 			if (pr->pr_usrreqs && pr->pr_flags & PR_PURGEIF)
   1487  1.290       rtr 				(void)(*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
   1488  1.160       gdt 		}
   1489   1.53   thorpej 	}
   1490   1.57   thorpej 
   1491  1.436  christos 	/*
   1492  1.436  christos 	 * Must be done after the above pr_purgeif because if_psref may be
   1493  1.436  christos 	 * still used in pr_purgeif.
   1494  1.436  christos 	 */
   1495  1.414     ozaki 	psref_target_destroy(&ifp->if_psref, ifnet_psref_class);
   1496  1.414     ozaki 	PSLIST_ENTRY_DESTROY(ifp, if_pslist_entry);
   1497  1.414     ozaki 
   1498  1.369  christos 	pfil_run_ifhooks(if_pfil, PFIL_IFNET_DETACH, ifp);
   1499  1.265     rmind 	(void)pfil_head_destroy(ifp->if_pfil);
   1500  1.184    dyoung 
   1501   1.57   thorpej 	/* Announce that the interface is gone. */
   1502   1.57   thorpej 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
   1503   1.93    itojun 
   1504  1.320     ozaki 	IF_AFDATA_LOCK_DESTROY(ifp);
   1505  1.320     ozaki 
   1506   1.95    itojun 	/*
   1507  1.168      matt 	 * remove packets that came from ifp, from software interrupt queues.
   1508   1.95    itojun 	 */
   1509  1.168      matt 	DOMAIN_FOREACH(dp) {
   1510  1.168      matt 		for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
   1511  1.260  christos 			struct ifqueue *iq = dp->dom_ifqueues[i];
   1512  1.260  christos 			if (iq == NULL)
   1513  1.168      matt 				break;
   1514  1.260  christos 			dp->dom_ifqueues[i] = NULL;
   1515  1.260  christos 			if_detach_queues(ifp, iq);
   1516  1.168      matt 		}
   1517  1.168      matt 	}
   1518   1.95    itojun 
   1519  1.276     rmind 	/*
   1520  1.276     rmind 	 * IP queues have to be processed separately: net-queue barrier
   1521  1.276     rmind 	 * ensures that the packets are dequeued while a cross-call will
   1522  1.276     rmind 	 * ensure that the interrupts have completed. FIXME: not quite..
   1523  1.276     rmind 	 */
   1524  1.278        he #ifdef INET
   1525  1.276     rmind 	pktq_barrier(ip_pktq);
   1526  1.278        he #endif
   1527  1.281     rmind #ifdef INET6
   1528  1.293     pooka 	if (in6_present)
   1529  1.293     pooka 		pktq_barrier(ip6_pktq);
   1530  1.281     rmind #endif
   1531  1.463       uwe 	xc_barrier(0);
   1532  1.276     rmind 
   1533  1.323     ozaki 	if (ifp->if_percpuq != NULL) {
   1534  1.323     ozaki 		if_percpuq_destroy(ifp->if_percpuq);
   1535  1.323     ozaki 		ifp->if_percpuq = NULL;
   1536  1.323     ozaki 	}
   1537  1.323     ozaki 
   1538  1.414     ozaki 	mutex_obj_free(ifp->if_ioctl_lock);
   1539  1.414     ozaki 	ifp->if_ioctl_lock = NULL;
   1540  1.419     ozaki 	mutex_obj_free(ifp->if_snd.ifq_lock);
   1541  1.469   thorpej 	if_stats_fini(ifp);
   1542  1.492  yamaguch 	KASSERT(!simplehook_has_hooks(ifp->if_linkstate_hooks));
   1543  1.492  yamaguch 	simplehook_destroy(ifp->if_linkstate_hooks);
   1544  1.414     ozaki 
   1545   1.53   thorpej 	splx(s);
   1546  1.349     ozaki 
   1547  1.349     ozaki #ifdef IFAREF_DEBUG
   1548  1.349     ozaki 	if_check_and_free_ifa_list(ifp);
   1549  1.349     ozaki #endif
   1550   1.95    itojun }
   1551   1.95    itojun 
   1552   1.95    itojun static void
   1553  1.163   thorpej if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
   1554   1.95    itojun {
   1555   1.95    itojun 	struct mbuf *m, *prev, *next;
   1556   1.95    itojun 
   1557   1.95    itojun 	prev = NULL;
   1558  1.185    dyoung 	for (m = q->ifq_head; m != NULL; m = next) {
   1559  1.274     rmind 		KASSERT((m->m_flags & M_PKTHDR) != 0);
   1560  1.274     rmind 
   1561   1.95    itojun 		next = m->m_nextpkt;
   1562  1.338     ozaki 		if (m->m_pkthdr.rcvif_index != ifp->if_index) {
   1563   1.96    itojun 			prev = m;
   1564   1.95    itojun 			continue;
   1565   1.96    itojun 		}
   1566   1.95    itojun 
   1567  1.185    dyoung 		if (prev != NULL)
   1568   1.95    itojun 			prev->m_nextpkt = m->m_nextpkt;
   1569   1.95    itojun 		else
   1570   1.95    itojun 			q->ifq_head = m->m_nextpkt;
   1571   1.95    itojun 		if (q->ifq_tail == m)
   1572   1.95    itojun 			q->ifq_tail = prev;
   1573   1.95    itojun 		q->ifq_len--;
   1574   1.95    itojun 
   1575   1.95    itojun 		m->m_nextpkt = NULL;
   1576   1.95    itojun 		m_freem(m);
   1577   1.95    itojun 		IF_DROP(q);
   1578   1.95    itojun 	}
   1579   1.53   thorpej }
   1580   1.53   thorpej 
   1581   1.53   thorpej /*
   1582   1.53   thorpej  * Callback for a radix tree walk to delete all references to an
   1583   1.53   thorpej  * ifnet.
   1584   1.53   thorpej  */
   1585  1.163   thorpej static int
   1586  1.362     ozaki if_delroute_matcher(struct rtentry *rt, void *v)
   1587   1.53   thorpej {
   1588   1.55    itojun 	struct ifnet *ifp = (struct ifnet *)v;
   1589   1.53   thorpej 
   1590  1.362     ozaki 	if (rt->rt_ifp == ifp)
   1591  1.362     ozaki 		return 1;
   1592  1.362     ozaki 	else
   1593  1.185    dyoung 		return 0;
   1594   1.53   thorpej }
   1595   1.53   thorpej 
   1596    1.1       cgd /*
   1597   1.63   thorpej  * Create a clone network interface.
   1598   1.63   thorpej  */
   1599  1.302     ozaki static int
   1600  1.163   thorpej if_clone_create(const char *name)
   1601   1.63   thorpej {
   1602   1.63   thorpej 	struct if_clone *ifc;
   1603   1.63   thorpej 	int unit;
   1604  1.336     ozaki 	struct ifnet *ifp;
   1605  1.336     ozaki 	struct psref psref;
   1606   1.63   thorpej 
   1607  1.372     ozaki 	KASSERT(mutex_owned(&if_clone_mtx));
   1608  1.372     ozaki 
   1609   1.63   thorpej 	ifc = if_clone_lookup(name, &unit);
   1610   1.63   thorpej 	if (ifc == NULL)
   1611  1.185    dyoung 		return EINVAL;
   1612   1.63   thorpej 
   1613  1.336     ozaki 	ifp = if_get(name, &psref);
   1614  1.336     ozaki 	if (ifp != NULL) {
   1615  1.336     ozaki 		if_put(ifp, &psref);
   1616  1.185    dyoung 		return EEXIST;
   1617  1.336     ozaki 	}
   1618   1.63   thorpej 
   1619  1.185    dyoung 	return (*ifc->ifc_create)(ifc, unit);
   1620   1.63   thorpej }
   1621   1.63   thorpej 
   1622   1.63   thorpej /*
   1623   1.63   thorpej  * Destroy a clone network interface.
   1624   1.63   thorpej  */
   1625  1.302     ozaki static int
   1626  1.163   thorpej if_clone_destroy(const char *name)
   1627   1.63   thorpej {
   1628   1.63   thorpej 	struct if_clone *ifc;
   1629   1.63   thorpej 	struct ifnet *ifp;
   1630  1.336     ozaki 	struct psref psref;
   1631  1.458     ozaki 	int error;
   1632  1.458     ozaki 	int (*if_ioctl)(struct ifnet *, u_long, void *);
   1633   1.63   thorpej 
   1634  1.372     ozaki 	KASSERT(mutex_owned(&if_clone_mtx));
   1635  1.372     ozaki 
   1636   1.63   thorpej 	ifc = if_clone_lookup(name, NULL);
   1637   1.63   thorpej 	if (ifc == NULL)
   1638  1.185    dyoung 		return EINVAL;
   1639   1.63   thorpej 
   1640  1.336     ozaki 	if (ifc->ifc_destroy == NULL)
   1641  1.336     ozaki 		return EOPNOTSUPP;
   1642  1.336     ozaki 
   1643  1.336     ozaki 	ifp = if_get(name, &psref);
   1644   1.63   thorpej 	if (ifp == NULL)
   1645  1.185    dyoung 		return ENXIO;
   1646   1.63   thorpej 
   1647  1.336     ozaki 	/* We have to disable ioctls here */
   1648  1.413     ozaki 	IFNET_LOCK(ifp);
   1649  1.458     ozaki 	if_ioctl = ifp->if_ioctl;
   1650  1.336     ozaki 	ifp->if_ioctl = if_nullioctl;
   1651  1.413     ozaki 	IFNET_UNLOCK(ifp);
   1652  1.336     ozaki 
   1653  1.336     ozaki 	/*
   1654  1.336     ozaki 	 * We cannot call ifc_destroy with holding ifp.
   1655  1.336     ozaki 	 * Releasing ifp here is safe thanks to if_clone_mtx.
   1656  1.336     ozaki 	 */
   1657  1.336     ozaki 	if_put(ifp, &psref);
   1658   1.63   thorpej 
   1659  1.458     ozaki 	error = (*ifc->ifc_destroy)(ifp);
   1660  1.458     ozaki 
   1661  1.458     ozaki 	if (error != 0) {
   1662  1.458     ozaki 		/* We have to restore if_ioctl on error */
   1663  1.458     ozaki 		IFNET_LOCK(ifp);
   1664  1.458     ozaki 		ifp->if_ioctl = if_ioctl;
   1665  1.458     ozaki 		IFNET_UNLOCK(ifp);
   1666  1.458     ozaki 	}
   1667  1.458     ozaki 
   1668  1.458     ozaki 	return error;
   1669   1.63   thorpej }
   1670   1.63   thorpej 
   1671  1.379  knakahar static bool
   1672  1.379  knakahar if_is_unit(const char *name)
   1673  1.379  knakahar {
   1674  1.379  knakahar 
   1675  1.455   msaitoh 	while (*name != '\0') {
   1676  1.379  knakahar 		if (*name < '0' || *name > '9')
   1677  1.379  knakahar 			return false;
   1678  1.379  knakahar 		name++;
   1679  1.379  knakahar 	}
   1680  1.379  knakahar 
   1681  1.379  knakahar 	return true;
   1682  1.379  knakahar }
   1683  1.379  knakahar 
   1684   1.63   thorpej /*
   1685   1.63   thorpej  * Look up a network interface cloner.
   1686   1.63   thorpej  */
   1687  1.163   thorpej static struct if_clone *
   1688  1.163   thorpej if_clone_lookup(const char *name, int *unitp)
   1689   1.63   thorpej {
   1690   1.63   thorpej 	struct if_clone *ifc;
   1691   1.63   thorpej 	const char *cp;
   1692  1.262  christos 	char *dp, ifname[IFNAMSIZ + 3];
   1693  1.128    itojun 	int unit;
   1694   1.63   thorpej 
   1695  1.372     ozaki 	KASSERT(mutex_owned(&if_clone_mtx));
   1696  1.372     ozaki 
   1697  1.262  christos 	strcpy(ifname, "if_");
   1698  1.128    itojun 	/* separate interface name from unit */
   1699  1.379  knakahar 	/* TODO: search unit number from backward */
   1700  1.262  christos 	for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ &&
   1701  1.379  knakahar 	    *cp && !if_is_unit(cp);)
   1702  1.262  christos 		*dp++ = *cp++;
   1703  1.128    itojun 
   1704  1.128    itojun 	if (cp == name || cp - name == IFNAMSIZ || !*cp)
   1705  1.185    dyoung 		return NULL;	/* No name or unit number */
   1706  1.262  christos 	*dp++ = '\0';
   1707  1.128    itojun 
   1708  1.262  christos again:
   1709  1.128    itojun 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
   1710  1.262  christos 		if (strcmp(ifname + 3, ifc->ifc_name) == 0)
   1711  1.128    itojun 			break;
   1712   1.63   thorpej 	}
   1713   1.63   thorpej 
   1714  1.262  christos 	if (ifc == NULL) {
   1715  1.375  christos 		int error;
   1716  1.375  christos 		if (*ifname == '\0')
   1717  1.375  christos 			return NULL;
   1718  1.375  christos 		mutex_exit(&if_clone_mtx);
   1719  1.375  christos 		error = module_autoload(ifname, MODULE_CLASS_DRIVER);
   1720  1.375  christos 		mutex_enter(&if_clone_mtx);
   1721  1.375  christos 		if (error)
   1722  1.262  christos 			return NULL;
   1723  1.262  christos 		*ifname = '\0';
   1724  1.262  christos 		goto again;
   1725  1.262  christos 	}
   1726   1.63   thorpej 
   1727  1.128    itojun 	unit = 0;
   1728  1.129    itojun 	while (cp - name < IFNAMSIZ && *cp) {
   1729  1.245  christos 		if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) {
   1730   1.63   thorpej 			/* Bogus unit number. */
   1731  1.185    dyoung 			return NULL;
   1732   1.63   thorpej 		}
   1733  1.128    itojun 		unit = (unit * 10) + (*cp++ - '0');
   1734   1.63   thorpej 	}
   1735   1.63   thorpej 
   1736   1.63   thorpej 	if (unitp != NULL)
   1737  1.128    itojun 		*unitp = unit;
   1738  1.185    dyoung 	return ifc;
   1739   1.63   thorpej }
   1740   1.63   thorpej 
   1741   1.63   thorpej /*
   1742   1.63   thorpej  * Register a network interface cloner.
   1743   1.63   thorpej  */
   1744   1.63   thorpej void
   1745  1.163   thorpej if_clone_attach(struct if_clone *ifc)
   1746   1.63   thorpej {
   1747   1.63   thorpej 
   1748  1.372     ozaki 	mutex_enter(&if_clone_mtx);
   1749   1.63   thorpej 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
   1750   1.67   thorpej 	if_cloners_count++;
   1751  1.372     ozaki 	mutex_exit(&if_clone_mtx);
   1752   1.63   thorpej }
   1753   1.63   thorpej 
   1754   1.63   thorpej /*
   1755   1.63   thorpej  * Unregister a network interface cloner.
   1756   1.63   thorpej  */
   1757   1.63   thorpej void
   1758  1.163   thorpej if_clone_detach(struct if_clone *ifc)
   1759   1.63   thorpej {
   1760   1.63   thorpej 
   1761  1.377  christos 	mutex_enter(&if_clone_mtx);
   1762   1.63   thorpej 	LIST_REMOVE(ifc, ifc_list);
   1763   1.67   thorpej 	if_cloners_count--;
   1764  1.377  christos 	mutex_exit(&if_clone_mtx);
   1765   1.67   thorpej }
   1766   1.67   thorpej 
   1767   1.67   thorpej /*
   1768   1.67   thorpej  * Provide list of interface cloners to userspace.
   1769   1.67   thorpej  */
   1770  1.315    martin int
   1771  1.315    martin if_clone_list(int buf_count, char *buffer, int *total)
   1772   1.67   thorpej {
   1773   1.67   thorpej 	char outbuf[IFNAMSIZ], *dst;
   1774   1.67   thorpej 	struct if_clone *ifc;
   1775   1.67   thorpej 	int count, error = 0;
   1776   1.67   thorpej 
   1777  1.372     ozaki 	mutex_enter(&if_clone_mtx);
   1778  1.315    martin 	*total = if_cloners_count;
   1779  1.315    martin 	if ((dst = buffer) == NULL) {
   1780   1.67   thorpej 		/* Just asking how many there are. */
   1781  1.372     ozaki 		goto out;
   1782   1.67   thorpej 	}
   1783   1.67   thorpej 
   1784  1.372     ozaki 	if (buf_count < 0) {
   1785  1.372     ozaki 		error = EINVAL;
   1786  1.372     ozaki 		goto out;
   1787  1.372     ozaki 	}
   1788   1.67   thorpej 
   1789  1.315    martin 	count = (if_cloners_count < buf_count) ?
   1790  1.315    martin 	    if_cloners_count : buf_count;
   1791   1.67   thorpej 
   1792   1.67   thorpej 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
   1793   1.67   thorpej 	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
   1794  1.175  christos 		(void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
   1795  1.372     ozaki 		if (outbuf[sizeof(outbuf) - 1] != '\0') {
   1796  1.372     ozaki 			error = ENAMETOOLONG;
   1797  1.372     ozaki 			goto out;
   1798  1.372     ozaki 		}
   1799  1.172  christos 		error = copyout(outbuf, dst, sizeof(outbuf));
   1800  1.185    dyoung 		if (error != 0)
   1801   1.67   thorpej 			break;
   1802   1.67   thorpej 	}
   1803   1.67   thorpej 
   1804  1.372     ozaki out:
   1805  1.372     ozaki 	mutex_exit(&if_clone_mtx);
   1806  1.185    dyoung 	return error;
   1807   1.63   thorpej }
   1808   1.63   thorpej 
   1809  1.207    dyoung void
   1810  1.357     ozaki ifa_psref_init(struct ifaddr *ifa)
   1811  1.357     ozaki {
   1812  1.357     ozaki 
   1813  1.357     ozaki 	psref_target_init(&ifa->ifa_psref, ifa_psref_class);
   1814  1.357     ozaki }
   1815  1.357     ozaki 
   1816  1.357     ozaki void
   1817  1.291     rmind ifaref(struct ifaddr *ifa)
   1818  1.291     rmind {
   1819  1.438     ozaki 
   1820  1.439     ozaki 	atomic_inc_uint(&ifa->ifa_refcnt);
   1821  1.291     rmind }
   1822  1.291     rmind 
   1823  1.291     rmind void
   1824  1.291     rmind ifafree(struct ifaddr *ifa)
   1825  1.291     rmind {
   1826  1.291     rmind 	KASSERT(ifa != NULL);
   1827  1.462     ozaki 	KASSERTMSG(ifa->ifa_refcnt > 0, "ifa_refcnt=%d", ifa->ifa_refcnt);
   1828  1.291     rmind 
   1829  1.439     ozaki 	if (atomic_dec_uint_nv(&ifa->ifa_refcnt) == 0) {
   1830  1.291     rmind 		free(ifa, M_IFADDR);
   1831  1.291     rmind 	}
   1832  1.291     rmind }
   1833  1.291     rmind 
   1834  1.366     ozaki bool
   1835  1.366     ozaki ifa_is_destroying(struct ifaddr *ifa)
   1836  1.366     ozaki {
   1837  1.366     ozaki 
   1838  1.366     ozaki 	return ISSET(ifa->ifa_flags, IFA_DESTROYING);
   1839  1.366     ozaki }
   1840  1.366     ozaki 
   1841  1.291     rmind void
   1842  1.207    dyoung ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
   1843  1.207    dyoung {
   1844  1.357     ozaki 
   1845  1.207    dyoung 	ifa->ifa_ifp = ifp;
   1846  1.357     ozaki 
   1847  1.416     ozaki 	/*
   1848  1.418     ozaki 	 * Check MP-safety for IFEF_MPSAFE drivers.
   1849  1.416     ozaki 	 * Check !IFF_RUNNING for initialization routines that normally don't
   1850  1.416     ozaki 	 * take IFNET_LOCK but it's safe because there is no competitor.
   1851  1.416     ozaki 	 * XXX there are false positive cases because IFF_RUNNING can be off on
   1852  1.416     ozaki 	 * if_stop.
   1853  1.416     ozaki 	 */
   1854  1.418     ozaki 	KASSERT(!if_is_mpsafe(ifp) || !ISSET(ifp->if_flags, IFF_RUNNING) ||
   1855  1.416     ozaki 	    IFNET_LOCKED(ifp));
   1856  1.416     ozaki 
   1857  1.208    dyoung 	TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
   1858  1.354     ozaki 	IFADDR_ENTRY_INIT(ifa);
   1859  1.354     ozaki 	IFADDR_WRITER_INSERT_TAIL(ifp, ifa);
   1860  1.357     ozaki 
   1861  1.291     rmind 	ifaref(ifa);
   1862  1.207    dyoung }
   1863  1.207    dyoung 
   1864  1.207    dyoung void
   1865  1.207    dyoung ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
   1866  1.207    dyoung {
   1867  1.357     ozaki 
   1868  1.207    dyoung 	KASSERT(ifa->ifa_ifp == ifp);
   1869  1.416     ozaki 	/*
   1870  1.418     ozaki 	 * Check MP-safety for IFEF_MPSAFE drivers.
   1871  1.416     ozaki 	 * if_is_deactivated indicates ifa_remove is called form if_detach
   1872  1.416     ozaki 	 * where is safe even if IFNET_LOCK isn't held.
   1873  1.416     ozaki 	 */
   1874  1.418     ozaki 	KASSERT(!if_is_mpsafe(ifp) || if_is_deactivated(ifp) || IFNET_LOCKED(ifp));
   1875  1.357     ozaki 
   1876  1.207    dyoung 	TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
   1877  1.354     ozaki 	IFADDR_WRITER_REMOVE(ifa);
   1878  1.359     ozaki #ifdef NET_MPSAFE
   1879  1.416     ozaki 	IFNET_GLOBAL_LOCK();
   1880  1.357     ozaki 	pserialize_perform(ifnet_psz);
   1881  1.416     ozaki 	IFNET_GLOBAL_UNLOCK();
   1882  1.357     ozaki #endif
   1883  1.357     ozaki 
   1884  1.359     ozaki #ifdef NET_MPSAFE
   1885  1.357     ozaki 	psref_target_destroy(&ifa->ifa_psref, ifa_psref_class);
   1886  1.357     ozaki #endif
   1887  1.360     ozaki 	IFADDR_ENTRY_DESTROY(ifa);
   1888  1.291     rmind 	ifafree(ifa);
   1889  1.207    dyoung }
   1890  1.207    dyoung 
   1891  1.357     ozaki void
   1892  1.357     ozaki ifa_acquire(struct ifaddr *ifa, struct psref *psref)
   1893  1.357     ozaki {
   1894  1.357     ozaki 
   1895  1.453     ozaki 	PSREF_DEBUG_FILL_RETURN_ADDRESS(psref);
   1896  1.357     ozaki 	psref_acquire(psref, &ifa->ifa_psref, ifa_psref_class);
   1897  1.357     ozaki }
   1898  1.357     ozaki 
   1899  1.357     ozaki void
   1900  1.357     ozaki ifa_release(struct ifaddr *ifa, struct psref *psref)
   1901  1.357     ozaki {
   1902  1.357     ozaki 
   1903  1.357     ozaki 	if (ifa == NULL)
   1904  1.357     ozaki 		return;
   1905  1.357     ozaki 
   1906  1.357     ozaki 	psref_release(psref, &ifa->ifa_psref, ifa_psref_class);
   1907  1.357     ozaki }
   1908  1.357     ozaki 
   1909  1.357     ozaki bool
   1910  1.357     ozaki ifa_held(struct ifaddr *ifa)
   1911  1.357     ozaki {
   1912  1.357     ozaki 
   1913  1.357     ozaki 	return psref_held(&ifa->ifa_psref, ifa_psref_class);
   1914  1.357     ozaki }
   1915  1.357     ozaki 
   1916  1.194    dyoung static inline int
   1917  1.194    dyoung equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
   1918  1.194    dyoung {
   1919  1.194    dyoung 	return sockaddr_cmp(sa1, sa2) == 0;
   1920  1.194    dyoung }
   1921  1.194    dyoung 
   1922   1.63   thorpej /*
   1923    1.1       cgd  * Locate an interface based on a complete address.
   1924    1.1       cgd  */
   1925    1.1       cgd /*ARGSUSED*/
   1926    1.1       cgd struct ifaddr *
   1927  1.163   thorpej ifa_ifwithaddr(const struct sockaddr *addr)
   1928    1.1       cgd {
   1929   1.61  augustss 	struct ifnet *ifp;
   1930   1.61  augustss 	struct ifaddr *ifa;
   1931    1.1       cgd 
   1932  1.334     ozaki 	IFNET_READER_FOREACH(ifp) {
   1933  1.348     ozaki 		if (if_is_deactivated(ifp))
   1934    1.1       cgd 			continue;
   1935  1.354     ozaki 		IFADDR_READER_FOREACH(ifa, ifp) {
   1936   1.53   thorpej 			if (ifa->ifa_addr->sa_family != addr->sa_family)
   1937   1.53   thorpej 				continue;
   1938   1.53   thorpej 			if (equal(addr, ifa->ifa_addr))
   1939  1.185    dyoung 				return ifa;
   1940   1.53   thorpej 			if ((ifp->if_flags & IFF_BROADCAST) &&
   1941   1.53   thorpej 			    ifa->ifa_broadaddr &&
   1942   1.53   thorpej 			    /* IP6 doesn't have broadcast */
   1943   1.53   thorpej 			    ifa->ifa_broadaddr->sa_len != 0 &&
   1944   1.53   thorpej 			    equal(ifa->ifa_broadaddr, addr))
   1945  1.185    dyoung 				return ifa;
   1946   1.53   thorpej 		}
   1947    1.1       cgd 	}
   1948  1.357     ozaki 	return NULL;
   1949  1.357     ozaki }
   1950  1.357     ozaki 
   1951  1.357     ozaki struct ifaddr *
   1952  1.357     ozaki ifa_ifwithaddr_psref(const struct sockaddr *addr, struct psref *psref)
   1953  1.357     ozaki {
   1954  1.357     ozaki 	struct ifaddr *ifa;
   1955  1.357     ozaki 	int s = pserialize_read_enter();
   1956  1.357     ozaki 
   1957  1.357     ozaki 	ifa = ifa_ifwithaddr(addr);
   1958  1.357     ozaki 	if (ifa != NULL)
   1959  1.357     ozaki 		ifa_acquire(ifa, psref);
   1960  1.334     ozaki 	pserialize_read_exit(s);
   1961  1.357     ozaki 
   1962  1.357     ozaki 	return ifa;
   1963    1.1       cgd }
   1964   1.49    itojun 
   1965    1.1       cgd /*
   1966    1.1       cgd  * Locate the point to point interface with a given destination address.
   1967    1.1       cgd  */
   1968    1.1       cgd /*ARGSUSED*/
   1969    1.1       cgd struct ifaddr *
   1970  1.163   thorpej ifa_ifwithdstaddr(const struct sockaddr *addr)
   1971    1.1       cgd {
   1972   1.61  augustss 	struct ifnet *ifp;
   1973   1.61  augustss 	struct ifaddr *ifa;
   1974    1.1       cgd 
   1975  1.334     ozaki 	IFNET_READER_FOREACH(ifp) {
   1976  1.348     ozaki 		if (if_is_deactivated(ifp))
   1977   1.53   thorpej 			continue;
   1978  1.185    dyoung 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
   1979  1.185    dyoung 			continue;
   1980  1.354     ozaki 		IFADDR_READER_FOREACH(ifa, ifp) {
   1981  1.185    dyoung 			if (ifa->ifa_addr->sa_family != addr->sa_family ||
   1982  1.185    dyoung 			    ifa->ifa_dstaddr == NULL)
   1983  1.185    dyoung 				continue;
   1984  1.185    dyoung 			if (equal(addr, ifa->ifa_dstaddr))
   1985  1.185    dyoung 				return ifa;
   1986   1.53   thorpej 		}
   1987    1.1       cgd 	}
   1988  1.357     ozaki 
   1989  1.357     ozaki 	return NULL;
   1990  1.357     ozaki }
   1991  1.357     ozaki 
   1992  1.357     ozaki struct ifaddr *
   1993  1.357     ozaki ifa_ifwithdstaddr_psref(const struct sockaddr *addr, struct psref *psref)
   1994  1.357     ozaki {
   1995  1.357     ozaki 	struct ifaddr *ifa;
   1996  1.357     ozaki 	int s;
   1997  1.357     ozaki 
   1998  1.357     ozaki 	s = pserialize_read_enter();
   1999  1.357     ozaki 	ifa = ifa_ifwithdstaddr(addr);
   2000  1.357     ozaki 	if (ifa != NULL)
   2001  1.357     ozaki 		ifa_acquire(ifa, psref);
   2002  1.334     ozaki 	pserialize_read_exit(s);
   2003  1.357     ozaki 
   2004  1.357     ozaki 	return ifa;
   2005    1.1       cgd }
   2006    1.1       cgd 
   2007    1.1       cgd /*
   2008    1.1       cgd  * Find an interface on a specific network.  If many, choice
   2009   1.15   mycroft  * is most specific found.
   2010    1.1       cgd  */
   2011    1.1       cgd struct ifaddr *
   2012  1.163   thorpej ifa_ifwithnet(const struct sockaddr *addr)
   2013    1.1       cgd {
   2014   1.61  augustss 	struct ifnet *ifp;
   2015  1.357     ozaki 	struct ifaddr *ifa, *ifa_maybe = NULL;
   2016  1.140      matt 	const struct sockaddr_dl *sdl;
   2017    1.1       cgd 	u_int af = addr->sa_family;
   2018  1.171     pooka 	const char *addr_data = addr->sa_data, *cplim;
   2019    1.1       cgd 
   2020    1.1       cgd 	if (af == AF_LINK) {
   2021  1.195    dyoung 		sdl = satocsdl(addr);
   2022  1.137    itojun 		if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
   2023  1.137    itojun 		    ifindex2ifnet[sdl->sdl_index] &&
   2024  1.348     ozaki 		    !if_is_deactivated(ifindex2ifnet[sdl->sdl_index])) {
   2025  1.316     ozaki 			return ifindex2ifnet[sdl->sdl_index]->if_dl;
   2026  1.316     ozaki 		}
   2027    1.1       cgd 	}
   2028   1.51    bouyer #ifdef NETATALK
   2029   1.51    bouyer 	if (af == AF_APPLETALK) {
   2030  1.140      matt 		const struct sockaddr_at *sat, *sat2;
   2031  1.158  christos 		sat = (const struct sockaddr_at *)addr;
   2032  1.334     ozaki 		IFNET_READER_FOREACH(ifp) {
   2033  1.348     ozaki 			if (if_is_deactivated(ifp))
   2034   1.53   thorpej 				continue;
   2035  1.158  christos 			ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
   2036   1.62    bouyer 			if (ifa == NULL)
   2037   1.62    bouyer 				continue;
   2038   1.62    bouyer 			sat2 = (struct sockaddr_at *)ifa->ifa_addr;
   2039   1.62    bouyer 			if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
   2040  1.185    dyoung 				return ifa; /* exact match */
   2041   1.62    bouyer 			if (ifa_maybe == NULL) {
   2042  1.112       wiz 				/* else keep the if with the right range */
   2043   1.62    bouyer 				ifa_maybe = ifa;
   2044   1.62    bouyer 			}
   2045   1.51    bouyer 		}
   2046  1.185    dyoung 		return ifa_maybe;
   2047   1.51    bouyer 	}
   2048   1.51    bouyer #endif
   2049  1.334     ozaki 	IFNET_READER_FOREACH(ifp) {
   2050  1.348     ozaki 		if (if_is_deactivated(ifp))
   2051   1.53   thorpej 			continue;
   2052  1.354     ozaki 		IFADDR_READER_FOREACH(ifa, ifp) {
   2053  1.171     pooka 			const char *cp, *cp2, *cp3;
   2054   1.15   mycroft 
   2055   1.15   mycroft 			if (ifa->ifa_addr->sa_family != af ||
   2056  1.185    dyoung 			    ifa->ifa_netmask == NULL)
   2057   1.53   thorpej  next:				continue;
   2058   1.15   mycroft 			cp = addr_data;
   2059   1.15   mycroft 			cp2 = ifa->ifa_addr->sa_data;
   2060   1.15   mycroft 			cp3 = ifa->ifa_netmask->sa_data;
   2061  1.171     pooka 			cplim = (const char *)ifa->ifa_netmask +
   2062   1.53   thorpej 			    ifa->ifa_netmask->sa_len;
   2063   1.53   thorpej 			while (cp3 < cplim) {
   2064   1.53   thorpej 				if ((*cp++ ^ *cp2++) & *cp3++) {
   2065   1.53   thorpej 					/* want to continue for() loop */
   2066   1.32       mrg 					goto next;
   2067   1.53   thorpej 				}
   2068   1.53   thorpej 			}
   2069  1.185    dyoung 			if (ifa_maybe == NULL ||
   2070  1.329     ozaki 			    rt_refines(ifa->ifa_netmask,
   2071  1.329     ozaki 			               ifa_maybe->ifa_netmask))
   2072   1.15   mycroft 				ifa_maybe = ifa;
   2073   1.15   mycroft 		}
   2074   1.53   thorpej 	}
   2075  1.357     ozaki 	return ifa_maybe;
   2076  1.357     ozaki }
   2077  1.357     ozaki 
   2078  1.357     ozaki struct ifaddr *
   2079  1.357     ozaki ifa_ifwithnet_psref(const struct sockaddr *addr, struct psref *psref)
   2080  1.357     ozaki {
   2081  1.357     ozaki 	struct ifaddr *ifa;
   2082  1.357     ozaki 	int s;
   2083  1.357     ozaki 
   2084  1.357     ozaki 	s = pserialize_read_enter();
   2085  1.357     ozaki 	ifa = ifa_ifwithnet(addr);
   2086  1.357     ozaki 	if (ifa != NULL)
   2087  1.357     ozaki 		ifa_acquire(ifa, psref);
   2088  1.334     ozaki 	pserialize_read_exit(s);
   2089  1.357     ozaki 
   2090  1.357     ozaki 	return ifa;
   2091   1.26       mrg }
   2092   1.53   thorpej 
   2093   1.26       mrg /*
   2094   1.26       mrg  * Find the interface of the addresss.
   2095   1.26       mrg  */
   2096   1.26       mrg struct ifaddr *
   2097  1.163   thorpej ifa_ifwithladdr(const struct sockaddr *addr)
   2098   1.26       mrg {
   2099   1.26       mrg 	struct ifaddr *ia;
   2100   1.26       mrg 
   2101   1.53   thorpej 	if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
   2102   1.53   thorpej 	    (ia = ifa_ifwithnet(addr)))
   2103  1.185    dyoung 		return ia;
   2104  1.185    dyoung 	return NULL;
   2105    1.1       cgd }
   2106    1.1       cgd 
   2107  1.357     ozaki struct ifaddr *
   2108  1.357     ozaki ifa_ifwithladdr_psref(const struct sockaddr *addr, struct psref *psref)
   2109  1.357     ozaki {
   2110  1.357     ozaki 	struct ifaddr *ifa;
   2111  1.357     ozaki 	int s;
   2112  1.357     ozaki 
   2113  1.357     ozaki 	s = pserialize_read_enter();
   2114  1.357     ozaki 	ifa = ifa_ifwithladdr(addr);
   2115  1.357     ozaki 	if (ifa != NULL)
   2116  1.357     ozaki 		ifa_acquire(ifa, psref);
   2117  1.357     ozaki 	pserialize_read_exit(s);
   2118  1.357     ozaki 
   2119  1.357     ozaki 	return ifa;
   2120  1.357     ozaki }
   2121  1.357     ozaki 
   2122    1.1       cgd /*
   2123    1.1       cgd  * Find an interface using a specific address family
   2124    1.1       cgd  */
   2125    1.1       cgd struct ifaddr *
   2126  1.163   thorpej ifa_ifwithaf(int af)
   2127    1.1       cgd {
   2128   1.61  augustss 	struct ifnet *ifp;
   2129  1.334     ozaki 	struct ifaddr *ifa = NULL;
   2130  1.334     ozaki 	int s;
   2131    1.1       cgd 
   2132  1.334     ozaki 	s = pserialize_read_enter();
   2133  1.334     ozaki 	IFNET_READER_FOREACH(ifp) {
   2134  1.348     ozaki 		if (if_is_deactivated(ifp))
   2135   1.53   thorpej 			continue;
   2136  1.354     ozaki 		IFADDR_READER_FOREACH(ifa, ifp) {
   2137   1.21   mycroft 			if (ifa->ifa_addr->sa_family == af)
   2138  1.334     ozaki 				goto out;
   2139   1.53   thorpej 		}
   2140   1.53   thorpej 	}
   2141  1.334     ozaki out:
   2142  1.334     ozaki 	pserialize_read_exit(s);
   2143  1.334     ozaki 	return ifa;
   2144    1.1       cgd }
   2145    1.1       cgd 
   2146    1.1       cgd /*
   2147    1.1       cgd  * Find an interface address specific to an interface best matching
   2148    1.1       cgd  * a given address.
   2149    1.1       cgd  */
   2150    1.1       cgd struct ifaddr *
   2151  1.163   thorpej ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
   2152    1.1       cgd {
   2153   1.61  augustss 	struct ifaddr *ifa;
   2154  1.140      matt 	const char *cp, *cp2, *cp3;
   2155  1.140      matt 	const char *cplim;
   2156    1.1       cgd 	struct ifaddr *ifa_maybe = 0;
   2157    1.1       cgd 	u_int af = addr->sa_family;
   2158    1.1       cgd 
   2159  1.348     ozaki 	if (if_is_deactivated(ifp))
   2160  1.185    dyoung 		return NULL;
   2161   1.53   thorpej 
   2162    1.1       cgd 	if (af >= AF_MAX)
   2163  1.185    dyoung 		return NULL;
   2164   1.53   thorpej 
   2165  1.354     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
   2166    1.1       cgd 		if (ifa->ifa_addr->sa_family != af)
   2167    1.1       cgd 			continue;
   2168    1.1       cgd 		ifa_maybe = ifa;
   2169  1.185    dyoung 		if (ifa->ifa_netmask == NULL) {
   2170    1.1       cgd 			if (equal(addr, ifa->ifa_addr) ||
   2171   1.53   thorpej 			    (ifa->ifa_dstaddr &&
   2172   1.53   thorpej 			     equal(addr, ifa->ifa_dstaddr)))
   2173  1.185    dyoung 				return ifa;
   2174    1.1       cgd 			continue;
   2175    1.1       cgd 		}
   2176    1.1       cgd 		cp = addr->sa_data;
   2177    1.1       cgd 		cp2 = ifa->ifa_addr->sa_data;
   2178    1.1       cgd 		cp3 = ifa->ifa_netmask->sa_data;
   2179    1.1       cgd 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
   2180   1.53   thorpej 		for (; cp3 < cplim; cp3++) {
   2181    1.1       cgd 			if ((*cp++ ^ *cp2++) & *cp3)
   2182    1.1       cgd 				break;
   2183   1.53   thorpej 		}
   2184    1.1       cgd 		if (cp3 == cplim)
   2185  1.185    dyoung 			return ifa;
   2186    1.1       cgd 	}
   2187  1.185    dyoung 	return ifa_maybe;
   2188    1.1       cgd }
   2189    1.9   mycroft 
   2190  1.357     ozaki struct ifaddr *
   2191  1.357     ozaki ifaof_ifpforaddr_psref(const struct sockaddr *addr, struct ifnet *ifp,
   2192  1.357     ozaki     struct psref *psref)
   2193  1.357     ozaki {
   2194  1.357     ozaki 	struct ifaddr *ifa;
   2195  1.357     ozaki 	int s;
   2196  1.357     ozaki 
   2197  1.357     ozaki 	s = pserialize_read_enter();
   2198  1.357     ozaki 	ifa = ifaof_ifpforaddr(addr, ifp);
   2199  1.357     ozaki 	if (ifa != NULL)
   2200  1.357     ozaki 		ifa_acquire(ifa, psref);
   2201  1.357     ozaki 	pserialize_read_exit(s);
   2202  1.357     ozaki 
   2203  1.357     ozaki 	return ifa;
   2204  1.357     ozaki }
   2205  1.357     ozaki 
   2206    1.1       cgd /*
   2207    1.1       cgd  * Default action when installing a route with a Link Level gateway.
   2208    1.1       cgd  * Lookup an appropriate real ifa to point to.
   2209    1.1       cgd  * This should be moved to /sys/net/link.c eventually.
   2210    1.1       cgd  */
   2211   1.15   mycroft void
   2212  1.228    dyoung link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
   2213    1.1       cgd {
   2214   1.61  augustss 	struct ifaddr *ifa;
   2215  1.194    dyoung 	const struct sockaddr *dst;
   2216   1.15   mycroft 	struct ifnet *ifp;
   2217  1.357     ozaki 	struct psref psref;
   2218    1.1       cgd 
   2219  1.464     ozaki 	if (cmd != RTM_ADD || ISSET(info->rti_flags, RTF_DONTCHANGEIFA))
   2220    1.1       cgd 		return;
   2221  1.464     ozaki 	ifp = rt->rt_ifa->ifa_ifp;
   2222  1.464     ozaki 	dst = rt_getkey(rt);
   2223  1.357     ozaki 	if ((ifa = ifaof_ifpforaddr_psref(dst, ifp, &psref)) != NULL) {
   2224  1.176    dyoung 		rt_replace_ifa(rt, ifa);
   2225    1.1       cgd 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
   2226   1.82    itojun 			ifa->ifa_rtrequest(cmd, rt, info);
   2227  1.357     ozaki 		ifa_release(ifa, &psref);
   2228    1.1       cgd 	}
   2229    1.1       cgd }
   2230    1.1       cgd 
   2231    1.1       cgd /*
   2232  1.325       roy  * bitmask macros to manage a densely packed link_state change queue.
   2233  1.325       roy  * Because we need to store LINK_STATE_UNKNOWN(0), LINK_STATE_DOWN(1) and
   2234  1.325       roy  * LINK_STATE_UP(2) we need 2 bits for each state change.
   2235  1.325       roy  * As a state change to store is 0, treat all bits set as an unset item.
   2236  1.325       roy  */
   2237  1.325       roy #define LQ_ITEM_BITS		2
   2238  1.325       roy #define LQ_ITEM_MASK		((1 << LQ_ITEM_BITS) - 1)
   2239  1.325       roy #define LQ_MASK(i)		(LQ_ITEM_MASK << (i) * LQ_ITEM_BITS)
   2240  1.325       roy #define LINK_STATE_UNSET	LQ_ITEM_MASK
   2241  1.325       roy #define LQ_ITEM(q, i)		(((q) & LQ_MASK((i))) >> (i) * LQ_ITEM_BITS)
   2242  1.325       roy #define LQ_STORE(q, i, v)						      \
   2243  1.325       roy 	do {								      \
   2244  1.325       roy 		(q) &= ~LQ_MASK((i));					      \
   2245  1.325       roy 		(q) |= (v) << (i) * LQ_ITEM_BITS;			      \
   2246  1.325       roy 	} while (0 /* CONSTCOND */)
   2247  1.325       roy #define LQ_MAX(q)		((sizeof((q)) * NBBY) / LQ_ITEM_BITS)
   2248  1.325       roy #define LQ_POP(q, v)							      \
   2249  1.325       roy 	do {								      \
   2250  1.325       roy 		(v) = LQ_ITEM((q), 0);					      \
   2251  1.325       roy 		(q) >>= LQ_ITEM_BITS;					      \
   2252  1.325       roy 		(q) |= LINK_STATE_UNSET << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS;  \
   2253  1.325       roy 	} while (0 /* CONSTCOND */)
   2254  1.325       roy #define LQ_PUSH(q, v)							      \
   2255  1.325       roy 	do {								      \
   2256  1.325       roy 		(q) >>= LQ_ITEM_BITS;					      \
   2257  1.325       roy 		(q) |= (v) << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS;		      \
   2258  1.325       roy 	} while (0 /* CONSTCOND */)
   2259  1.325       roy #define LQ_FIND_UNSET(q, i)						      \
   2260  1.325       roy 	for ((i) = 0; i < LQ_MAX((q)); (i)++) {				      \
   2261  1.325       roy 		if (LQ_ITEM((q), (i)) == LINK_STATE_UNSET)		      \
   2262  1.325       roy 			break;						      \
   2263  1.325       roy 	}
   2264  1.406     ozaki 
   2265  1.406     ozaki /*
   2266  1.325       roy  * Handle a change in the interface link state and
   2267  1.325       roy  * queue notifications.
   2268  1.159    dyoung  */
   2269  1.159    dyoung void
   2270  1.159    dyoung if_link_state_change(struct ifnet *ifp, int link_state)
   2271  1.159    dyoung {
   2272  1.406     ozaki 	int idx;
   2273  1.263       roy 
   2274  1.325       roy 	/* Ensure change is to a valid state */
   2275  1.325       roy 	switch (link_state) {
   2276  1.325       roy 	case LINK_STATE_UNKNOWN:	/* FALLTHROUGH */
   2277  1.325       roy 	case LINK_STATE_DOWN:		/* FALLTHROUGH */
   2278  1.325       roy 	case LINK_STATE_UP:
   2279  1.325       roy 		break;
   2280  1.325       roy 	default:
   2281  1.325       roy #ifdef DEBUG
   2282  1.325       roy 		printf("%s: invalid link state %d\n",
   2283  1.325       roy 		    ifp->if_xname, link_state);
   2284  1.325       roy #endif
   2285  1.185    dyoung 		return;
   2286  1.264       roy 	}
   2287  1.263       roy 
   2288  1.406     ozaki 	IF_LINK_STATE_CHANGE_LOCK(ifp);
   2289  1.325       roy 
   2290  1.325       roy 	/* Find the last unset event in the queue. */
   2291  1.325       roy 	LQ_FIND_UNSET(ifp->if_link_queue, idx);
   2292  1.325       roy 
   2293  1.480       roy 	if (idx == 0) {
   2294  1.480       roy 		/*
   2295  1.480       roy 		 * There is no queue of link state changes.
   2296  1.480       roy 		 * As we have the lock we can safely compare against the
   2297  1.480       roy 		 * current link state and return if the same.
   2298  1.480       roy 		 * Otherwise, if scheduled is true then the interface is being
   2299  1.480       roy 		 * detached and the queue is being drained so we need
   2300  1.480       roy 		 * to avoid queuing more work.
   2301  1.480       roy 		 */
   2302  1.480       roy 		 if (ifp->if_link_state == link_state || ifp->if_link_scheduled)
   2303  1.480       roy 			goto out;
   2304  1.480       roy 	} else {
   2305  1.480       roy 		/* Ensure link_state doesn't match the last queued state. */
   2306  1.480       roy 		if (LQ_ITEM(ifp->if_link_queue, idx - 1) == (uint8_t)link_state)
   2307  1.480       roy 			goto out;
   2308  1.480       roy 	}
   2309  1.325       roy 
   2310  1.325       roy 	/* Handle queue overflow. */
   2311  1.325       roy 	if (idx == LQ_MAX(ifp->if_link_queue)) {
   2312  1.325       roy 		uint8_t lost;
   2313  1.325       roy 
   2314  1.325       roy 		/*
   2315  1.325       roy 		 * The DOWN state must be protected from being pushed off
   2316  1.325       roy 		 * the queue to ensure that userland will always be
   2317  1.325       roy 		 * in a sane state.
   2318  1.325       roy 		 * Because DOWN is protected, there is no need to protect
   2319  1.325       roy 		 * UNKNOWN.
   2320  1.325       roy 		 * It should be invalid to change from any other state to
   2321  1.325       roy 		 * UNKNOWN anyway ...
   2322  1.325       roy 		 */
   2323  1.325       roy 		lost = LQ_ITEM(ifp->if_link_queue, 0);
   2324  1.325       roy 		LQ_PUSH(ifp->if_link_queue, (uint8_t)link_state);
   2325  1.325       roy 		if (lost == LINK_STATE_DOWN) {
   2326  1.325       roy 			lost = LQ_ITEM(ifp->if_link_queue, 0);
   2327  1.325       roy 			LQ_STORE(ifp->if_link_queue, 0, LINK_STATE_DOWN);
   2328  1.325       roy 		}
   2329  1.325       roy 		printf("%s: lost link state change %s\n",
   2330  1.325       roy 		    ifp->if_xname,
   2331  1.325       roy 		    lost == LINK_STATE_UP ? "UP" :
   2332  1.325       roy 		    lost == LINK_STATE_DOWN ? "DOWN" :
   2333  1.325       roy 		    "UNKNOWN");
   2334  1.325       roy 	} else
   2335  1.325       roy 		LQ_STORE(ifp->if_link_queue, idx, (uint8_t)link_state);
   2336  1.325       roy 
   2337  1.480       roy 	if (ifp->if_link_scheduled)
   2338  1.480       roy 		goto out;
   2339  1.480       roy 
   2340  1.480       roy 	ifp->if_link_scheduled = true;
   2341  1.480       roy 	workqueue_enqueue(ifnet_link_state_wq, &ifp->if_link_work, NULL);
   2342  1.324     ozaki 
   2343  1.325       roy out:
   2344  1.406     ozaki 	IF_LINK_STATE_CHANGE_UNLOCK(ifp);
   2345  1.324     ozaki }
   2346  1.324     ozaki 
   2347  1.325       roy /*
   2348  1.325       roy  * Handle interface link state change notifications.
   2349  1.325       roy  */
   2350  1.471   thorpej static void
   2351  1.471   thorpej if_link_state_change_process(struct ifnet *ifp, int link_state)
   2352  1.324     ozaki {
   2353  1.324     ozaki 	struct domain *dp;
   2354  1.393     ozaki 	int s = splnet();
   2355  1.406     ozaki 	bool notify;
   2356  1.393     ozaki 
   2357  1.393     ozaki 	KASSERT(!cpu_intr_p());
   2358  1.324     ozaki 
   2359  1.406     ozaki 	IF_LINK_STATE_CHANGE_LOCK(ifp);
   2360  1.406     ozaki 
   2361  1.325       roy 	/* Ensure the change is still valid. */
   2362  1.393     ozaki 	if (ifp->if_link_state == link_state) {
   2363  1.406     ozaki 		IF_LINK_STATE_CHANGE_UNLOCK(ifp);
   2364  1.434     ozaki 		splx(s);
   2365  1.325       roy 		return;
   2366  1.393     ozaki 	}
   2367  1.324     ozaki 
   2368  1.263       roy #ifdef DEBUG
   2369  1.263       roy 	log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
   2370  1.263       roy 		link_state == LINK_STATE_UP ? "UP" :
   2371  1.263       roy 		link_state == LINK_STATE_DOWN ? "DOWN" :
   2372  1.263       roy 		"UNKNOWN",
   2373  1.325       roy 		ifp->if_link_state == LINK_STATE_UP ? "UP" :
   2374  1.325       roy 		ifp->if_link_state == LINK_STATE_DOWN ? "DOWN" :
   2375  1.263       roy 		"UNKNOWN");
   2376  1.263       roy #endif
   2377  1.263       roy 
   2378  1.263       roy 	/*
   2379  1.263       roy 	 * When going from UNKNOWN to UP, we need to mark existing
   2380  1.314       roy 	 * addresses as tentative and restart DAD as we may have
   2381  1.263       roy 	 * erroneously not found a duplicate.
   2382  1.263       roy 	 *
   2383  1.263       roy 	 * This needs to happen before rt_ifmsg to avoid a race where
   2384  1.263       roy 	 * listeners would have an address and expect it to work right
   2385  1.263       roy 	 * away.
   2386  1.263       roy 	 */
   2387  1.406     ozaki 	notify = (link_state == LINK_STATE_UP &&
   2388  1.406     ozaki 	    ifp->if_link_state == LINK_STATE_UNKNOWN);
   2389  1.406     ozaki 	ifp->if_link_state = link_state;
   2390  1.406     ozaki 	/* The following routines may sleep so release the spin mutex */
   2391  1.406     ozaki 	IF_LINK_STATE_CHANGE_UNLOCK(ifp);
   2392  1.406     ozaki 
   2393  1.406     ozaki 	KERNEL_LOCK_UNLESS_NET_MPSAFE();
   2394  1.406     ozaki 	if (notify) {
   2395  1.312       roy 		DOMAIN_FOREACH(dp) {
   2396  1.312       roy 			if (dp->dom_if_link_state_change != NULL)
   2397  1.312       roy 				dp->dom_if_link_state_change(ifp,
   2398  1.312       roy 				    LINK_STATE_DOWN);
   2399  1.312       roy 		}
   2400  1.312       roy 	}
   2401  1.263       roy 
   2402  1.159    dyoung 	/* Notify that the link state has changed. */
   2403  1.185    dyoung 	rt_ifmsg(ifp);
   2404  1.263       roy 
   2405  1.166  liamjfoy #if NCARP > 0
   2406  1.185    dyoung 	if (ifp->if_carp)
   2407  1.185    dyoung 		carp_carpdev_state(ifp);
   2408  1.166  liamjfoy #endif
   2409  1.263       roy 
   2410  1.481       roy 	if (ifp->if_link_state_changed != NULL)
   2411  1.481       roy 		ifp->if_link_state_changed(ifp, link_state);
   2412  1.481       roy 
   2413  1.482       roy #if NBRIDGE > 0
   2414  1.482       roy 	if (ifp->if_bridge != NULL)
   2415  1.482       roy 		bridge_calc_link_state(ifp->if_bridge);
   2416  1.482       roy #endif
   2417  1.482       roy 
   2418  1.485  yamaguch #if NLAGG > 0
   2419  1.491  yamaguch 	if (ifp->if_type == IFT_IEEE8023ADLAG)
   2420  1.485  yamaguch 		lagg_linkstate_changed(ifp);
   2421  1.485  yamaguch #endif
   2422  1.485  yamaguch 
   2423  1.492  yamaguch 	simplehook_dohooks(ifp->if_linkstate_hooks);
   2424  1.492  yamaguch 
   2425  1.312       roy 	DOMAIN_FOREACH(dp) {
   2426  1.312       roy 		if (dp->dom_if_link_state_change != NULL)
   2427  1.312       roy 			dp->dom_if_link_state_change(ifp, link_state);
   2428  1.270     pooka 	}
   2429  1.406     ozaki 	KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
   2430  1.393     ozaki 	splx(s);
   2431  1.325       roy }
   2432  1.325       roy 
   2433  1.325       roy /*
   2434  1.325       roy  * Process the interface link state change queue.
   2435  1.325       roy  */
   2436  1.325       roy static void
   2437  1.471   thorpej if_link_state_change_work(struct work *work, void *arg)
   2438  1.325       roy {
   2439  1.471   thorpej 	struct ifnet *ifp = container_of(work, struct ifnet, if_link_work);
   2440  1.325       roy 	int s;
   2441  1.325       roy 	uint8_t state;
   2442  1.325       roy 
   2443  1.471   thorpej 	KERNEL_LOCK_UNLESS_NET_MPSAFE();
   2444  1.325       roy 	s = splnet();
   2445  1.325       roy 
   2446  1.480       roy 	/* Pop a link state change from the queue and process it.
   2447  1.480       roy 	 * If there is nothing to process then if_detach() has been called.
   2448  1.480       roy 	 * We keep if_link_scheduled = true so the queue can safely drain
   2449  1.480       roy 	 * without more work being queued. */
   2450  1.406     ozaki 	IF_LINK_STATE_CHANGE_LOCK(ifp);
   2451  1.325       roy 	LQ_POP(ifp->if_link_queue, state);
   2452  1.406     ozaki 	IF_LINK_STATE_CHANGE_UNLOCK(ifp);
   2453  1.480       roy 	if (state == LINK_STATE_UNSET)
   2454  1.480       roy 		goto out;
   2455  1.406     ozaki 
   2456  1.471   thorpej 	if_link_state_change_process(ifp, state);
   2457  1.325       roy 
   2458  1.325       roy 	/* If there is a link state change to come, schedule it. */
   2459  1.406     ozaki 	IF_LINK_STATE_CHANGE_LOCK(ifp);
   2460  1.480       roy 	if (LQ_ITEM(ifp->if_link_queue, 0) != LINK_STATE_UNSET) {
   2461  1.480       roy 		ifp->if_link_scheduled = true;
   2462  1.480       roy 		workqueue_enqueue(ifnet_link_state_wq, &ifp->if_link_work, NULL);
   2463  1.480       roy 	} else
   2464  1.480       roy 		ifp->if_link_scheduled = false;
   2465  1.406     ozaki 	IF_LINK_STATE_CHANGE_UNLOCK(ifp);
   2466  1.471   thorpej 
   2467  1.480       roy out:
   2468  1.264       roy 	splx(s);
   2469  1.471   thorpej 	KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
   2470  1.159    dyoung }
   2471  1.159    dyoung 
   2472  1.492  yamaguch void *
   2473  1.492  yamaguch if_linkstate_change_establish(struct ifnet *ifp, void (*fn)(void *), void *arg)
   2474  1.492  yamaguch {
   2475  1.492  yamaguch 	khook_t *hk;
   2476  1.492  yamaguch 
   2477  1.492  yamaguch 	hk = simplehook_establish(ifp->if_linkstate_hooks, fn, arg);
   2478  1.492  yamaguch 
   2479  1.492  yamaguch 	return (void *)hk;
   2480  1.492  yamaguch }
   2481  1.492  yamaguch 
   2482  1.492  yamaguch void
   2483  1.492  yamaguch if_linkstate_change_disestablish(struct ifnet *ifp, void *vhook, kmutex_t *lock)
   2484  1.492  yamaguch {
   2485  1.492  yamaguch 
   2486  1.492  yamaguch 	simplehook_disestablish(ifp->if_linkstate_hooks, vhook, lock);
   2487  1.492  yamaguch }
   2488  1.492  yamaguch 
   2489  1.159    dyoung /*
   2490  1.483       roy  * Used to mark addresses on an interface as DETATCHED or TENTATIVE
   2491  1.483       roy  * and thus start Duplicate Address Detection without changing the
   2492  1.483       roy  * real link state.
   2493  1.483       roy  */
   2494  1.483       roy void
   2495  1.483       roy if_domain_link_state_change(struct ifnet *ifp, int link_state)
   2496  1.483       roy {
   2497  1.483       roy 	struct domain *dp;
   2498  1.483       roy 	int s = splnet();
   2499  1.483       roy 
   2500  1.483       roy 	KERNEL_LOCK_UNLESS_NET_MPSAFE();
   2501  1.483       roy 
   2502  1.483       roy 	DOMAIN_FOREACH(dp) {
   2503  1.483       roy 		if (dp->dom_if_link_state_change != NULL)
   2504  1.483       roy 			dp->dom_if_link_state_change(ifp, link_state);
   2505  1.483       roy 	}
   2506  1.483       roy 
   2507  1.483       roy 	splx(s);
   2508  1.483       roy 	KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
   2509  1.483       roy }
   2510  1.483       roy 
   2511  1.483       roy /*
   2512  1.310       roy  * Default action when installing a local route on a point-to-point
   2513  1.310       roy  * interface.
   2514  1.310       roy  */
   2515  1.310       roy void
   2516  1.310       roy p2p_rtrequest(int req, struct rtentry *rt,
   2517  1.310       roy     __unused const struct rt_addrinfo *info)
   2518  1.310       roy {
   2519  1.310       roy 	struct ifnet *ifp = rt->rt_ifp;
   2520  1.310       roy 	struct ifaddr *ifa, *lo0ifa;
   2521  1.357     ozaki 	int s = pserialize_read_enter();
   2522  1.310       roy 
   2523  1.310       roy 	switch (req) {
   2524  1.310       roy 	case RTM_ADD:
   2525  1.310       roy 		if ((rt->rt_flags & RTF_LOCAL) == 0)
   2526  1.310       roy 			break;
   2527  1.310       roy 
   2528  1.351     ozaki 		rt->rt_ifp = lo0ifp;
   2529  1.351     ozaki 
   2530  1.440     ozaki 		if (ISSET(info->rti_flags, RTF_DONTCHANGEIFA))
   2531  1.440     ozaki 			break;
   2532  1.440     ozaki 
   2533  1.354     ozaki 		IFADDR_READER_FOREACH(ifa, ifp) {
   2534  1.310       roy 			if (equal(rt_getkey(rt), ifa->ifa_addr))
   2535  1.310       roy 				break;
   2536  1.310       roy 		}
   2537  1.310       roy 		if (ifa == NULL)
   2538  1.310       roy 			break;
   2539  1.310       roy 
   2540  1.310       roy 		/*
   2541  1.310       roy 		 * Ensure lo0 has an address of the same family.
   2542  1.310       roy 		 */
   2543  1.354     ozaki 		IFADDR_READER_FOREACH(lo0ifa, lo0ifp) {
   2544  1.310       roy 			if (lo0ifa->ifa_addr->sa_family ==
   2545  1.310       roy 			    ifa->ifa_addr->sa_family)
   2546  1.310       roy 				break;
   2547  1.310       roy 		}
   2548  1.310       roy 		if (lo0ifa == NULL)
   2549  1.310       roy 			break;
   2550  1.310       roy 
   2551  1.310       roy 		/*
   2552  1.310       roy 		 * Make sure to set rt->rt_ifa to the interface
   2553  1.310       roy 		 * address we are using, otherwise we will have trouble
   2554  1.310       roy 		 * with source address selection.
   2555  1.310       roy 		 */
   2556  1.310       roy 		if (ifa != rt->rt_ifa)
   2557  1.310       roy 			rt_replace_ifa(rt, ifa);
   2558  1.310       roy 		break;
   2559  1.310       roy 	case RTM_DELETE:
   2560  1.310       roy 	default:
   2561  1.310       roy 		break;
   2562  1.310       roy 	}
   2563  1.357     ozaki 	pserialize_read_exit(s);
   2564  1.310       roy }
   2565  1.310       roy 
   2566  1.403     ozaki static void
   2567  1.403     ozaki _if_down(struct ifnet *ifp)
   2568    1.1       cgd {
   2569   1.61  augustss 	struct ifaddr *ifa;
   2570  1.312       roy 	struct domain *dp;
   2571  1.357     ozaki 	int s, bound;
   2572  1.357     ozaki 	struct psref psref;
   2573    1.1       cgd 
   2574    1.1       cgd 	ifp->if_flags &= ~IFF_UP;
   2575  1.232  christos 	nanotime(&ifp->if_lastchange);
   2576  1.357     ozaki 
   2577  1.357     ozaki 	bound = curlwp_bind();
   2578  1.357     ozaki 	s = pserialize_read_enter();
   2579  1.357     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
   2580  1.357     ozaki 		ifa_acquire(ifa, &psref);
   2581  1.357     ozaki 		pserialize_read_exit(s);
   2582  1.357     ozaki 
   2583    1.1       cgd 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
   2584  1.357     ozaki 
   2585  1.357     ozaki 		s = pserialize_read_enter();
   2586  1.357     ozaki 		ifa_release(ifa, &psref);
   2587  1.357     ozaki 	}
   2588  1.357     ozaki 	pserialize_read_exit(s);
   2589  1.357     ozaki 	curlwp_bindx(bound);
   2590  1.357     ozaki 
   2591   1.78   thorpej 	IFQ_PURGE(&ifp->if_snd);
   2592  1.166  liamjfoy #if NCARP > 0
   2593  1.166  liamjfoy 	if (ifp->if_carp)
   2594  1.166  liamjfoy 		carp_carpdev_state(ifp);
   2595  1.166  liamjfoy #endif
   2596   1.15   mycroft 	rt_ifmsg(ifp);
   2597  1.312       roy 	DOMAIN_FOREACH(dp) {
   2598  1.312       roy 		if (dp->dom_if_down)
   2599  1.312       roy 			dp->dom_if_down(ifp);
   2600  1.312       roy 	}
   2601   1.15   mycroft }
   2602   1.15   mycroft 
   2603  1.403     ozaki static void
   2604  1.403     ozaki if_down_deactivated(struct ifnet *ifp)
   2605  1.403     ozaki {
   2606  1.403     ozaki 
   2607  1.403     ozaki 	KASSERT(if_is_deactivated(ifp));
   2608  1.403     ozaki 	_if_down(ifp);
   2609  1.403     ozaki }
   2610  1.403     ozaki 
   2611  1.403     ozaki void
   2612  1.403     ozaki if_down_locked(struct ifnet *ifp)
   2613  1.403     ozaki {
   2614  1.403     ozaki 
   2615  1.413     ozaki 	KASSERT(IFNET_LOCKED(ifp));
   2616  1.403     ozaki 	_if_down(ifp);
   2617  1.403     ozaki }
   2618  1.403     ozaki 
   2619   1.15   mycroft /*
   2620  1.403     ozaki  * Mark an interface down and notify protocols of
   2621   1.15   mycroft  * the transition.
   2622   1.23   mycroft  * NOTE: must be called at splsoftnet or equivalent.
   2623   1.15   mycroft  */
   2624   1.15   mycroft void
   2625  1.403     ozaki if_down(struct ifnet *ifp)
   2626  1.403     ozaki {
   2627  1.403     ozaki 
   2628  1.413     ozaki 	IFNET_LOCK(ifp);
   2629  1.403     ozaki 	if_down_locked(ifp);
   2630  1.413     ozaki 	IFNET_UNLOCK(ifp);
   2631  1.403     ozaki }
   2632  1.403     ozaki 
   2633  1.403     ozaki /*
   2634  1.403     ozaki  * Must be called with holding if_ioctl_lock.
   2635  1.403     ozaki  */
   2636  1.403     ozaki static void
   2637  1.403     ozaki if_up_locked(struct ifnet *ifp)
   2638   1.15   mycroft {
   2639   1.24  christos #ifdef notyet
   2640   1.61  augustss 	struct ifaddr *ifa;
   2641   1.24  christos #endif
   2642  1.312       roy 	struct domain *dp;
   2643   1.15   mycroft 
   2644  1.413     ozaki 	KASSERT(IFNET_LOCKED(ifp));
   2645  1.403     ozaki 
   2646  1.403     ozaki 	KASSERT(!if_is_deactivated(ifp));
   2647   1.15   mycroft 	ifp->if_flags |= IFF_UP;
   2648  1.232  christos 	nanotime(&ifp->if_lastchange);
   2649   1.15   mycroft #ifdef notyet
   2650   1.15   mycroft 	/* this has no effect on IP, and will kill all ISO connections XXX */
   2651  1.354     ozaki 	IFADDR_READER_FOREACH(ifa, ifp)
   2652   1.15   mycroft 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
   2653   1.15   mycroft #endif
   2654  1.166  liamjfoy #if NCARP > 0
   2655  1.166  liamjfoy 	if (ifp->if_carp)
   2656  1.166  liamjfoy 		carp_carpdev_state(ifp);
   2657  1.166  liamjfoy #endif
   2658   1.15   mycroft 	rt_ifmsg(ifp);
   2659  1.312       roy 	DOMAIN_FOREACH(dp) {
   2660  1.313       roy 		if (dp->dom_if_up)
   2661  1.313       roy 			dp->dom_if_up(ifp);
   2662  1.312       roy 	}
   2663    1.1       cgd }
   2664    1.1       cgd 
   2665    1.1       cgd /*
   2666  1.296     ozaki  * Handle interface slowtimo timer routine.  Called
   2667  1.296     ozaki  * from softclock, we decrement timer (if set) and
   2668    1.1       cgd  * call the appropriate interface routine on expiration.
   2669    1.1       cgd  */
   2670  1.294     ozaki static void
   2671  1.177  christos if_slowtimo(void *arg)
   2672    1.1       cgd {
   2673  1.306    martin 	void (*slowtimo)(struct ifnet *);
   2674  1.296     ozaki 	struct ifnet *ifp = arg;
   2675  1.305    martin 	int s;
   2676    1.1       cgd 
   2677  1.306    martin 	slowtimo = ifp->if_slowtimo;
   2678  1.306    martin 	if (__predict_false(slowtimo == NULL))
   2679  1.305    martin 		return;
   2680  1.296     ozaki 
   2681  1.374     ozaki 	s = splnet();
   2682  1.411     ozaki 	if (ifp->if_timer != 0 && --ifp->if_timer == 0)
   2683  1.306    martin 		(*slowtimo)(ifp);
   2684  1.411     ozaki 
   2685    1.1       cgd 	splx(s);
   2686  1.305    martin 
   2687  1.305    martin 	if (__predict_true(ifp->if_slowtimo != NULL))
   2688  1.305    martin 		callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ);
   2689   1.65   thorpej }
   2690   1.65   thorpej 
   2691   1.65   thorpej /*
   2692  1.403     ozaki  * Mark an interface up and notify protocols of
   2693  1.403     ozaki  * the transition.
   2694  1.403     ozaki  * NOTE: must be called at splsoftnet or equivalent.
   2695  1.403     ozaki  */
   2696  1.403     ozaki void
   2697  1.403     ozaki if_up(struct ifnet *ifp)
   2698  1.403     ozaki {
   2699  1.403     ozaki 
   2700  1.413     ozaki 	IFNET_LOCK(ifp);
   2701  1.403     ozaki 	if_up_locked(ifp);
   2702  1.413     ozaki 	IFNET_UNLOCK(ifp);
   2703  1.403     ozaki }
   2704  1.403     ozaki 
   2705  1.403     ozaki /*
   2706   1.65   thorpej  * Set/clear promiscuous mode on interface ifp based on the truth value
   2707   1.65   thorpej  * of pswitch.  The calls are reference counted so that only the first
   2708   1.65   thorpej  * "on" request actually has an effect, as does the final "off" request.
   2709   1.65   thorpej  * Results are undefined if the "off" and "on" requests are not matched.
   2710   1.65   thorpej  */
   2711   1.65   thorpej int
   2712  1.402     ozaki ifpromisc_locked(struct ifnet *ifp, int pswitch)
   2713   1.65   thorpej {
   2714  1.401     ozaki 	int pcount, ret = 0;
   2715  1.460   msaitoh 	u_short nflags;
   2716   1.65   thorpej 
   2717  1.413     ozaki 	KASSERT(IFNET_LOCKED(ifp));
   2718  1.401     ozaki 
   2719   1.65   thorpej 	pcount = ifp->if_pcount;
   2720   1.65   thorpej 	if (pswitch) {
   2721   1.65   thorpej 		/*
   2722   1.71   thorpej 		 * Allow the device to be "placed" into promiscuous
   2723   1.71   thorpej 		 * mode even if it is not configured up.  It will
   2724  1.242   mbalmer 		 * consult IFF_PROMISC when it is brought up.
   2725   1.65   thorpej 		 */
   2726   1.68        pk 		if (ifp->if_pcount++ != 0)
   2727  1.401     ozaki 			goto out;
   2728  1.252    dyoung 		nflags = ifp->if_flags | IFF_PROMISC;
   2729   1.65   thorpej 	} else {
   2730   1.65   thorpej 		if (--ifp->if_pcount > 0)
   2731  1.401     ozaki 			goto out;
   2732  1.252    dyoung 		nflags = ifp->if_flags & ~IFF_PROMISC;
   2733   1.65   thorpej 	}
   2734  1.252    dyoung 	ret = if_flags_set(ifp, nflags);
   2735   1.65   thorpej 	/* Restore interface state if not successful. */
   2736   1.65   thorpej 	if (ret != 0) {
   2737   1.65   thorpej 		ifp->if_pcount = pcount;
   2738   1.65   thorpej 	}
   2739  1.401     ozaki out:
   2740  1.402     ozaki 	return ret;
   2741  1.402     ozaki }
   2742  1.402     ozaki 
   2743  1.402     ozaki int
   2744  1.402     ozaki ifpromisc(struct ifnet *ifp, int pswitch)
   2745  1.402     ozaki {
   2746  1.402     ozaki 	int e;
   2747  1.402     ozaki 
   2748  1.413     ozaki 	IFNET_LOCK(ifp);
   2749  1.402     ozaki 	e = ifpromisc_locked(ifp, pswitch);
   2750  1.413     ozaki 	IFNET_UNLOCK(ifp);
   2751  1.402     ozaki 
   2752  1.402     ozaki 	return e;
   2753    1.1       cgd }
   2754    1.1       cgd 
   2755    1.1       cgd /*
   2756    1.1       cgd  * Map interface name to
   2757    1.1       cgd  * interface structure pointer.
   2758    1.1       cgd  */
   2759    1.1       cgd struct ifnet *
   2760  1.163   thorpej ifunit(const char *name)
   2761    1.1       cgd {
   2762   1.61  augustss 	struct ifnet *ifp;
   2763  1.105      matt 	const char *cp = name;
   2764  1.105      matt 	u_int unit = 0;
   2765  1.105      matt 	u_int i;
   2766  1.334     ozaki 	int s;
   2767  1.105      matt 
   2768  1.105      matt 	/*
   2769  1.105      matt 	 * If the entire name is a number, treat it as an ifindex.
   2770  1.105      matt 	 */
   2771  1.105      matt 	for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
   2772  1.105      matt 		unit = unit * 10 + (*cp - '0');
   2773  1.105      matt 	}
   2774  1.105      matt 
   2775  1.105      matt 	/*
   2776  1.105      matt 	 * If the number took all of the name, then it's a valid ifindex.
   2777  1.105      matt 	 */
   2778  1.387     ozaki 	if (i == IFNAMSIZ || (cp != name && *cp == '\0'))
   2779  1.387     ozaki 		return if_byindex(unit);
   2780   1.34   thorpej 
   2781  1.334     ozaki 	ifp = NULL;
   2782  1.334     ozaki 	s = pserialize_read_enter();
   2783  1.334     ozaki 	IFNET_READER_FOREACH(ifp) {
   2784  1.348     ozaki 		if (if_is_deactivated(ifp))
   2785   1.53   thorpej 			continue;
   2786   1.53   thorpej 	 	if (strcmp(ifp->if_xname, name) == 0)
   2787  1.334     ozaki 			goto out;
   2788   1.53   thorpej 	}
   2789  1.334     ozaki out:
   2790  1.334     ozaki 	pserialize_read_exit(s);
   2791  1.334     ozaki 	return ifp;
   2792    1.1       cgd }
   2793   1.49    itojun 
   2794  1.335     ozaki /*
   2795  1.335     ozaki  * Get a reference of an ifnet object by an interface name.
   2796  1.335     ozaki  * The returned reference is protected by psref(9). The caller
   2797  1.335     ozaki  * must release a returned reference by if_put after use.
   2798  1.335     ozaki  */
   2799  1.335     ozaki struct ifnet *
   2800  1.335     ozaki if_get(const char *name, struct psref *psref)
   2801  1.335     ozaki {
   2802  1.335     ozaki 	struct ifnet *ifp;
   2803  1.335     ozaki 	const char *cp = name;
   2804  1.335     ozaki 	u_int unit = 0;
   2805  1.335     ozaki 	u_int i;
   2806  1.335     ozaki 	int s;
   2807  1.335     ozaki 
   2808  1.335     ozaki 	/*
   2809  1.335     ozaki 	 * If the entire name is a number, treat it as an ifindex.
   2810  1.335     ozaki 	 */
   2811  1.335     ozaki 	for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
   2812  1.335     ozaki 		unit = unit * 10 + (*cp - '0');
   2813  1.335     ozaki 	}
   2814  1.335     ozaki 
   2815  1.335     ozaki 	/*
   2816  1.335     ozaki 	 * If the number took all of the name, then it's a valid ifindex.
   2817  1.335     ozaki 	 */
   2818  1.387     ozaki 	if (i == IFNAMSIZ || (cp != name && *cp == '\0'))
   2819  1.387     ozaki 		return if_get_byindex(unit, psref);
   2820  1.335     ozaki 
   2821  1.335     ozaki 	ifp = NULL;
   2822  1.335     ozaki 	s = pserialize_read_enter();
   2823  1.335     ozaki 	IFNET_READER_FOREACH(ifp) {
   2824  1.348     ozaki 		if (if_is_deactivated(ifp))
   2825  1.335     ozaki 			continue;
   2826  1.335     ozaki 		if (strcmp(ifp->if_xname, name) == 0) {
   2827  1.453     ozaki 			PSREF_DEBUG_FILL_RETURN_ADDRESS(psref);
   2828  1.335     ozaki 			psref_acquire(psref, &ifp->if_psref,
   2829  1.335     ozaki 			    ifnet_psref_class);
   2830  1.335     ozaki 			goto out;
   2831  1.335     ozaki 		}
   2832  1.335     ozaki 	}
   2833  1.335     ozaki out:
   2834  1.335     ozaki 	pserialize_read_exit(s);
   2835  1.335     ozaki 	return ifp;
   2836  1.335     ozaki }
   2837  1.335     ozaki 
   2838  1.335     ozaki /*
   2839  1.395       roy  * Release a reference of an ifnet object given by if_get, if_get_byindex
   2840  1.395       roy  * or if_get_bylla.
   2841  1.335     ozaki  */
   2842  1.335     ozaki void
   2843  1.335     ozaki if_put(const struct ifnet *ifp, struct psref *psref)
   2844  1.335     ozaki {
   2845  1.335     ozaki 
   2846  1.344     ozaki 	if (ifp == NULL)
   2847  1.344     ozaki 		return;
   2848  1.344     ozaki 
   2849  1.335     ozaki 	psref_release(psref, &ifp->if_psref, ifnet_psref_class);
   2850  1.335     ozaki }
   2851  1.335     ozaki 
   2852  1.415     ozaki /*
   2853  1.415     ozaki  * Return ifp having idx. Return NULL if not found.  Normally if_byindex
   2854  1.415     ozaki  * should be used.
   2855  1.415     ozaki  */
   2856  1.415     ozaki ifnet_t *
   2857  1.415     ozaki _if_byindex(u_int idx)
   2858  1.415     ozaki {
   2859  1.415     ozaki 
   2860  1.415     ozaki 	return (__predict_true(idx < if_indexlim)) ? ifindex2ifnet[idx] : NULL;
   2861  1.415     ozaki }
   2862  1.415     ozaki 
   2863  1.415     ozaki /*
   2864  1.415     ozaki  * Return ifp having idx. Return NULL if not found or the found ifp is
   2865  1.415     ozaki  * already deactivated.
   2866  1.415     ozaki  */
   2867  1.250     rmind ifnet_t *
   2868  1.250     rmind if_byindex(u_int idx)
   2869  1.250     rmind {
   2870  1.357     ozaki 	ifnet_t *ifp;
   2871  1.357     ozaki 
   2872  1.415     ozaki 	ifp = _if_byindex(idx);
   2873  1.357     ozaki 	if (ifp != NULL && if_is_deactivated(ifp))
   2874  1.357     ozaki 		ifp = NULL;
   2875  1.357     ozaki 	return ifp;
   2876  1.250     rmind }
   2877  1.250     rmind 
   2878  1.335     ozaki /*
   2879  1.335     ozaki  * Get a reference of an ifnet object by an interface index.
   2880  1.335     ozaki  * The returned reference is protected by psref(9). The caller
   2881  1.335     ozaki  * must release a returned reference by if_put after use.
   2882  1.335     ozaki  */
   2883  1.335     ozaki ifnet_t *
   2884  1.335     ozaki if_get_byindex(u_int idx, struct psref *psref)
   2885  1.335     ozaki {
   2886  1.335     ozaki 	ifnet_t *ifp;
   2887  1.335     ozaki 	int s;
   2888  1.335     ozaki 
   2889  1.335     ozaki 	s = pserialize_read_enter();
   2890  1.387     ozaki 	ifp = if_byindex(idx);
   2891  1.453     ozaki 	if (__predict_true(ifp != NULL)) {
   2892  1.453     ozaki 		PSREF_DEBUG_FILL_RETURN_ADDRESS(psref);
   2893  1.335     ozaki 		psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
   2894  1.453     ozaki 	}
   2895  1.335     ozaki 	pserialize_read_exit(s);
   2896  1.335     ozaki 
   2897  1.335     ozaki 	return ifp;
   2898  1.335     ozaki }
   2899  1.335     ozaki 
   2900  1.395       roy ifnet_t *
   2901  1.395       roy if_get_bylla(const void *lla, unsigned char lla_len, struct psref *psref)
   2902  1.395       roy {
   2903  1.395       roy 	ifnet_t *ifp;
   2904  1.395       roy 	int s;
   2905  1.395       roy 
   2906  1.395       roy 	s = pserialize_read_enter();
   2907  1.395       roy 	IFNET_READER_FOREACH(ifp) {
   2908  1.395       roy 		if (if_is_deactivated(ifp))
   2909  1.395       roy 			continue;
   2910  1.395       roy 		if (ifp->if_addrlen != lla_len)
   2911  1.395       roy 			continue;
   2912  1.395       roy 		if (memcmp(lla, CLLADDR(ifp->if_sadl), lla_len) == 0) {
   2913  1.395       roy 			psref_acquire(psref, &ifp->if_psref,
   2914  1.395       roy 			    ifnet_psref_class);
   2915  1.395       roy 			break;
   2916  1.395       roy 		}
   2917  1.395       roy 	}
   2918  1.395       roy 	pserialize_read_exit(s);
   2919  1.395       roy 
   2920  1.395       roy 	return ifp;
   2921  1.395       roy }
   2922  1.395       roy 
   2923  1.338     ozaki /*
   2924  1.380     ozaki  * Note that it's safe only if the passed ifp is guaranteed to not be freed,
   2925  1.380     ozaki  * for example using pserialize or the ifp is already held or some other
   2926  1.380     ozaki  * object is held which guarantes the ifp to not be freed indirectly.
   2927  1.338     ozaki  */
   2928  1.338     ozaki void
   2929  1.380     ozaki if_acquire(struct ifnet *ifp, struct psref *psref)
   2930  1.338     ozaki {
   2931  1.338     ozaki 
   2932  1.338     ozaki 	KASSERT(ifp->if_index != 0);
   2933  1.338     ozaki 	psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
   2934  1.338     ozaki }
   2935  1.338     ozaki 
   2936  1.338     ozaki bool
   2937  1.338     ozaki if_held(struct ifnet *ifp)
   2938  1.338     ozaki {
   2939  1.338     ozaki 
   2940  1.338     ozaki 	return psref_held(&ifp->if_psref, ifnet_psref_class);
   2941  1.338     ozaki }
   2942  1.338     ozaki 
   2943  1.404  knakahar /*
   2944  1.404  knakahar  * Some tunnel interfaces can nest, e.g. IPv4 over IPv4 gif(4) tunnel over IPv4.
   2945  1.404  knakahar  * Check the tunnel nesting count.
   2946  1.404  knakahar  * Return > 0, if tunnel nesting count is more than limit.
   2947  1.404  knakahar  * Return 0, if tunnel nesting count is equal or less than limit.
   2948  1.404  knakahar  */
   2949  1.404  knakahar int
   2950  1.404  knakahar if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, int limit)
   2951  1.404  knakahar {
   2952  1.404  knakahar 	struct m_tag *mtag;
   2953  1.404  knakahar 	int *count;
   2954  1.404  knakahar 
   2955  1.441      maxv 	mtag = m_tag_find(m, PACKET_TAG_TUNNEL_INFO);
   2956  1.404  knakahar 	if (mtag != NULL) {
   2957  1.404  knakahar 		count = (int *)(mtag + 1);
   2958  1.404  knakahar 		if (++(*count) > limit) {
   2959  1.404  knakahar 			log(LOG_NOTICE,
   2960  1.404  knakahar 			    "%s: recursively called too many times(%d)\n",
   2961  1.404  knakahar 			    ifp->if_xname, *count);
   2962  1.404  knakahar 			return EIO;
   2963  1.404  knakahar 		}
   2964  1.404  knakahar 	} else {
   2965  1.404  knakahar 		mtag = m_tag_get(PACKET_TAG_TUNNEL_INFO, sizeof(*count),
   2966  1.404  knakahar 		    M_NOWAIT);
   2967  1.404  knakahar 		if (mtag != NULL) {
   2968  1.404  knakahar 			m_tag_prepend(m, mtag);
   2969  1.404  knakahar 			count = (int *)(mtag + 1);
   2970  1.404  knakahar 			*count = 0;
   2971  1.404  knakahar 		} else {
   2972  1.404  knakahar 			log(LOG_DEBUG,
   2973  1.404  knakahar 			    "%s: m_tag_get() failed, recursion calls are not prevented.\n",
   2974  1.404  knakahar 			    ifp->if_xname);
   2975  1.404  knakahar 		}
   2976  1.404  knakahar 	}
   2977  1.404  knakahar 
   2978  1.404  knakahar 	return 0;
   2979  1.404  knakahar }
   2980  1.338     ozaki 
   2981  1.461  knakahar static void
   2982  1.461  knakahar if_tunnel_ro_init_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
   2983  1.461  knakahar {
   2984  1.461  knakahar 	struct tunnel_ro *tro = p;
   2985  1.461  knakahar 
   2986  1.461  knakahar 	tro->tr_ro = kmem_zalloc(sizeof(*tro->tr_ro), KM_SLEEP);
   2987  1.461  knakahar 	tro->tr_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
   2988  1.461  knakahar }
   2989  1.461  knakahar 
   2990  1.461  knakahar static void
   2991  1.461  knakahar if_tunnel_ro_fini_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
   2992  1.461  knakahar {
   2993  1.461  knakahar 	struct tunnel_ro *tro = p;
   2994  1.461  knakahar 
   2995  1.461  knakahar 	rtcache_free(tro->tr_ro);
   2996  1.461  knakahar 	kmem_free(tro->tr_ro, sizeof(*tro->tr_ro));
   2997  1.461  knakahar 
   2998  1.461  knakahar 	mutex_obj_free(tro->tr_lock);
   2999  1.461  knakahar }
   3000  1.461  knakahar 
   3001  1.470  riastrad percpu_t *
   3002  1.470  riastrad if_tunnel_alloc_ro_percpu(void)
   3003  1.470  riastrad {
   3004  1.470  riastrad 
   3005  1.470  riastrad 	return percpu_create(sizeof(struct tunnel_ro),
   3006  1.470  riastrad 	    if_tunnel_ro_init_pc, if_tunnel_ro_fini_pc, NULL);
   3007  1.470  riastrad }
   3008  1.470  riastrad 
   3009  1.461  knakahar void
   3010  1.461  knakahar if_tunnel_free_ro_percpu(percpu_t *ro_percpu)
   3011  1.461  knakahar {
   3012  1.461  knakahar 
   3013  1.461  knakahar 	percpu_free(ro_percpu, sizeof(struct tunnel_ro));
   3014  1.461  knakahar }
   3015  1.461  knakahar 
   3016  1.461  knakahar 
   3017  1.461  knakahar static void
   3018  1.461  knakahar if_tunnel_rtcache_free_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
   3019  1.461  knakahar {
   3020  1.461  knakahar 	struct tunnel_ro *tro = p;
   3021  1.461  knakahar 
   3022  1.461  knakahar 	mutex_enter(tro->tr_lock);
   3023  1.461  knakahar 	rtcache_free(tro->tr_ro);
   3024  1.461  knakahar 	mutex_exit(tro->tr_lock);
   3025  1.461  knakahar }
   3026  1.461  knakahar 
   3027  1.461  knakahar void if_tunnel_ro_percpu_rtcache_free(percpu_t *ro_percpu)
   3028  1.461  knakahar {
   3029  1.461  knakahar 
   3030  1.461  knakahar 	percpu_foreach(ro_percpu, if_tunnel_rtcache_free_pc, NULL);
   3031  1.461  knakahar }
   3032  1.461  knakahar 
   3033  1.469   thorpej void
   3034  1.469   thorpej if_export_if_data(ifnet_t * const ifp, struct if_data *ifi, bool zero_stats)
   3035  1.469   thorpej {
   3036  1.469   thorpej 
   3037  1.469   thorpej 	/* Collet the volatile stats first; this zeros *ifi. */
   3038  1.469   thorpej 	if_stats_to_if_data(ifp, ifi, zero_stats);
   3039  1.469   thorpej 
   3040  1.469   thorpej 	ifi->ifi_type = ifp->if_type;
   3041  1.469   thorpej 	ifi->ifi_addrlen = ifp->if_addrlen;
   3042  1.469   thorpej 	ifi->ifi_hdrlen = ifp->if_hdrlen;
   3043  1.469   thorpej 	ifi->ifi_link_state = ifp->if_link_state;
   3044  1.469   thorpej 	ifi->ifi_mtu = ifp->if_mtu;
   3045  1.469   thorpej 	ifi->ifi_metric = ifp->if_metric;
   3046  1.469   thorpej 	ifi->ifi_baudrate = ifp->if_baudrate;
   3047  1.469   thorpej 	ifi->ifi_lastchange = ifp->if_lastchange;
   3048  1.469   thorpej }
   3049  1.461  knakahar 
   3050  1.211    dyoung /* common */
   3051  1.215    dyoung int
   3052  1.215    dyoung ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
   3053  1.211    dyoung {
   3054  1.224    dyoung 	int s;
   3055  1.215    dyoung 	struct ifreq *ifr;
   3056  1.215    dyoung 	struct ifcapreq *ifcr;
   3057  1.215    dyoung 	struct ifdatareq *ifdr;
   3058  1.459       roy 	unsigned short flags;
   3059  1.466  christos 	char *descr;
   3060  1.466  christos 	int error;
   3061  1.211    dyoung 
   3062  1.211    dyoung 	switch (cmd) {
   3063  1.211    dyoung 	case SIOCSIFCAP:
   3064  1.215    dyoung 		ifcr = data;
   3065  1.211    dyoung 		if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
   3066  1.211    dyoung 			return EINVAL;
   3067  1.211    dyoung 
   3068  1.213    dyoung 		if (ifcr->ifcr_capenable == ifp->if_capenable)
   3069  1.213    dyoung 			return 0;
   3070  1.211    dyoung 
   3071  1.213    dyoung 		ifp->if_capenable = ifcr->ifcr_capenable;
   3072  1.211    dyoung 
   3073  1.213    dyoung 		/* Pre-compute the checksum flags mask. */
   3074  1.213    dyoung 		ifp->if_csum_flags_tx = 0;
   3075  1.213    dyoung 		ifp->if_csum_flags_rx = 0;
   3076  1.442       rin 		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx)
   3077  1.213    dyoung 			ifp->if_csum_flags_tx |= M_CSUM_IPv4;
   3078  1.442       rin 		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx)
   3079  1.213    dyoung 			ifp->if_csum_flags_rx |= M_CSUM_IPv4;
   3080  1.211    dyoung 
   3081  1.442       rin 		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx)
   3082  1.213    dyoung 			ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
   3083  1.442       rin 		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx)
   3084  1.213    dyoung 			ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
   3085  1.211    dyoung 
   3086  1.442       rin 		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx)
   3087  1.213    dyoung 			ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
   3088  1.442       rin 		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx)
   3089  1.213    dyoung 			ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
   3090  1.211    dyoung 
   3091  1.442       rin 		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx)
   3092  1.213    dyoung 			ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
   3093  1.442       rin 		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx)
   3094  1.213    dyoung 			ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
   3095  1.211    dyoung 
   3096  1.442       rin 		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx)
   3097  1.213    dyoung 			ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
   3098  1.442       rin 		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx)
   3099  1.213    dyoung 			ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
   3100  1.442       rin 
   3101  1.442       rin 		if (ifp->if_capenable & IFCAP_TSOv4)
   3102  1.442       rin 			ifp->if_csum_flags_tx |= M_CSUM_TSOv4;
   3103  1.442       rin 		if (ifp->if_capenable & IFCAP_TSOv6)
   3104  1.442       rin 			ifp->if_csum_flags_tx |= M_CSUM_TSOv6;
   3105  1.442       rin 
   3106  1.442       rin #if NBRIDGE > 0
   3107  1.442       rin 		if (ifp->if_bridge != NULL)
   3108  1.442       rin 			bridge_calc_csum_flags(ifp->if_bridge);
   3109  1.442       rin #endif
   3110  1.442       rin 
   3111  1.215    dyoung 		if (ifp->if_flags & IFF_UP)
   3112  1.215    dyoung 			return ENETRESET;
   3113  1.215    dyoung 		return 0;
   3114  1.211    dyoung 	case SIOCSIFFLAGS:
   3115  1.215    dyoung 		ifr = data;
   3116  1.399     ozaki 		/*
   3117  1.399     ozaki 		 * If if_is_mpsafe(ifp), KERNEL_LOCK isn't held here, but if_up
   3118  1.399     ozaki 		 * and if_down aren't MP-safe yet, so we must hold the lock.
   3119  1.399     ozaki 		 */
   3120  1.399     ozaki 		KERNEL_LOCK_IF_IFP_MPSAFE(ifp);
   3121  1.211    dyoung 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
   3122  1.373     ozaki 			s = splsoftnet();
   3123  1.403     ozaki 			if_down_locked(ifp);
   3124  1.211    dyoung 			splx(s);
   3125  1.211    dyoung 		}
   3126  1.211    dyoung 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
   3127  1.373     ozaki 			s = splsoftnet();
   3128  1.403     ozaki 			if_up_locked(ifp);
   3129  1.211    dyoung 			splx(s);
   3130  1.211    dyoung 		}
   3131  1.399     ozaki 		KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp);
   3132  1.459       roy 		flags = (ifp->if_flags & IFF_CANTCHANGE) |
   3133  1.459       roy 		    (ifr->ifr_flags &~ IFF_CANTCHANGE);
   3134  1.459       roy 		if (ifp->if_flags != flags) {
   3135  1.459       roy 			ifp->if_flags = flags;
   3136  1.459       roy 			/* Notify that the flags have changed. */
   3137  1.459       roy 			rt_ifmsg(ifp);
   3138  1.459       roy 		}
   3139  1.211    dyoung 		break;
   3140  1.211    dyoung 	case SIOCGIFFLAGS:
   3141  1.215    dyoung 		ifr = data;
   3142  1.211    dyoung 		ifr->ifr_flags = ifp->if_flags;
   3143  1.211    dyoung 		break;
   3144  1.211    dyoung 
   3145  1.211    dyoung 	case SIOCGIFMETRIC:
   3146  1.215    dyoung 		ifr = data;
   3147  1.211    dyoung 		ifr->ifr_metric = ifp->if_metric;
   3148  1.211    dyoung 		break;
   3149  1.211    dyoung 
   3150  1.211    dyoung 	case SIOCGIFMTU:
   3151  1.215    dyoung 		ifr = data;
   3152  1.211    dyoung 		ifr->ifr_mtu = ifp->if_mtu;
   3153  1.211    dyoung 		break;
   3154  1.211    dyoung 
   3155  1.211    dyoung 	case SIOCGIFDLT:
   3156  1.215    dyoung 		ifr = data;
   3157  1.211    dyoung 		ifr->ifr_dlt = ifp->if_dlt;
   3158  1.211    dyoung 		break;
   3159  1.211    dyoung 
   3160  1.211    dyoung 	case SIOCGIFCAP:
   3161  1.215    dyoung 		ifcr = data;
   3162  1.211    dyoung 		ifcr->ifcr_capabilities = ifp->if_capabilities;
   3163  1.211    dyoung 		ifcr->ifcr_capenable = ifp->if_capenable;
   3164  1.211    dyoung 		break;
   3165  1.211    dyoung 
   3166  1.211    dyoung 	case SIOCSIFMETRIC:
   3167  1.215    dyoung 		ifr = data;
   3168  1.211    dyoung 		ifp->if_metric = ifr->ifr_metric;
   3169  1.211    dyoung 		break;
   3170  1.211    dyoung 
   3171  1.211    dyoung 	case SIOCGIFDATA:
   3172  1.215    dyoung 		ifdr = data;
   3173  1.469   thorpej 		if_export_if_data(ifp, &ifdr->ifdr_data, false);
   3174  1.211    dyoung 		break;
   3175  1.211    dyoung 
   3176  1.266  christos 	case SIOCGIFINDEX:
   3177  1.266  christos 		ifr = data;
   3178  1.266  christos 		ifr->ifr_index = ifp->if_index;
   3179  1.266  christos 		break;
   3180  1.266  christos 
   3181  1.211    dyoung 	case SIOCZIFDATA:
   3182  1.215    dyoung 		ifdr = data;
   3183  1.469   thorpej 		if_export_if_data(ifp, &ifdr->ifdr_data, true);
   3184  1.261   msaitoh 		getnanotime(&ifp->if_lastchange);
   3185  1.211    dyoung 		break;
   3186  1.215    dyoung 	case SIOCSIFMTU:
   3187  1.215    dyoung 		ifr = data;
   3188  1.215    dyoung 		if (ifp->if_mtu == ifr->ifr_mtu)
   3189  1.215    dyoung 			break;
   3190  1.215    dyoung 		ifp->if_mtu = ifr->ifr_mtu;
   3191  1.215    dyoung 		return ENETRESET;
   3192  1.456     ozaki 	case SIOCSIFDESCR:
   3193  1.489  christos 		error = kauth_authorize_network(kauth_cred_get(),
   3194  1.466  christos 		    KAUTH_NETWORK_INTERFACE,
   3195  1.466  christos 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd),
   3196  1.466  christos 		    NULL);
   3197  1.466  christos 		if (error)
   3198  1.466  christos 			return error;
   3199  1.456     ozaki 
   3200  1.466  christos 		ifr = data;
   3201  1.456     ozaki 
   3202  1.466  christos 		if (ifr->ifr_buflen > IFDESCRSIZE)
   3203  1.466  christos 			return ENAMETOOLONG;
   3204  1.456     ozaki 
   3205  1.466  christos 		if (ifr->ifr_buf == NULL || ifr->ifr_buflen == 0) {
   3206  1.466  christos 			/* unset description */
   3207  1.466  christos 			descr = NULL;
   3208  1.466  christos 		} else {
   3209  1.466  christos 			descr = kmem_zalloc(IFDESCRSIZE, KM_SLEEP);
   3210  1.466  christos 			/*
   3211  1.466  christos 			 * copy (IFDESCRSIZE - 1) bytes to ensure
   3212  1.466  christos 			 * terminating nul
   3213  1.466  christos 			 */
   3214  1.466  christos 			error = copyin(ifr->ifr_buf, descr, IFDESCRSIZE - 1);
   3215  1.466  christos 			if (error) {
   3216  1.466  christos 				kmem_free(descr, IFDESCRSIZE);
   3217  1.466  christos 				return error;
   3218  1.456     ozaki 			}
   3219  1.466  christos 		}
   3220  1.456     ozaki 
   3221  1.466  christos 		if (ifp->if_description != NULL)
   3222  1.466  christos 			kmem_free(ifp->if_description, IFDESCRSIZE);
   3223  1.456     ozaki 
   3224  1.466  christos 		ifp->if_description = descr;
   3225  1.456     ozaki 		break;
   3226  1.456     ozaki 
   3227  1.456     ozaki  	case SIOCGIFDESCR:
   3228  1.466  christos 		ifr = data;
   3229  1.466  christos 		descr = ifp->if_description;
   3230  1.456     ozaki 
   3231  1.466  christos 		if (descr == NULL)
   3232  1.466  christos 			return ENOMSG;
   3233  1.456     ozaki 
   3234  1.466  christos 		if (ifr->ifr_buflen < IFDESCRSIZE)
   3235  1.466  christos 			return EINVAL;
   3236  1.456     ozaki 
   3237  1.466  christos 		error = copyout(descr, ifr->ifr_buf, IFDESCRSIZE);
   3238  1.466  christos 		if (error)
   3239  1.466  christos 			return error;
   3240  1.456     ozaki  		break;
   3241  1.456     ozaki 
   3242  1.211    dyoung 	default:
   3243  1.223    dyoung 		return ENOTTY;
   3244  1.211    dyoung 	}
   3245  1.211    dyoung 	return 0;
   3246  1.211    dyoung }
   3247  1.211    dyoung 
   3248  1.235    dyoung int
   3249  1.284       rtr ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
   3250  1.235    dyoung {
   3251  1.235    dyoung 	struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
   3252  1.235    dyoung 	struct ifaddr *ifa;
   3253  1.235    dyoung 	const struct sockaddr *any, *sa;
   3254  1.235    dyoung 	union {
   3255  1.235    dyoung 		struct sockaddr sa;
   3256  1.235    dyoung 		struct sockaddr_storage ss;
   3257  1.236  jakllsch 	} u, v;
   3258  1.357     ozaki 	int s, error = 0;
   3259  1.235    dyoung 
   3260  1.235    dyoung 	switch (cmd) {
   3261  1.235    dyoung 	case SIOCSIFADDRPREF:
   3262  1.489  christos 		error = kauth_authorize_network(kauth_cred_get(),
   3263  1.466  christos 		    KAUTH_NETWORK_INTERFACE,
   3264  1.466  christos 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd),
   3265  1.466  christos 		    NULL);
   3266  1.466  christos 		if (error)
   3267  1.466  christos 			return error;
   3268  1.466  christos 		break;
   3269  1.235    dyoung 	case SIOCGIFADDRPREF:
   3270  1.235    dyoung 		break;
   3271  1.235    dyoung 	default:
   3272  1.235    dyoung 		return EOPNOTSUPP;
   3273  1.235    dyoung 	}
   3274  1.235    dyoung 
   3275  1.235    dyoung 	/* sanity checks */
   3276  1.235    dyoung 	if (data == NULL || ifp == NULL) {
   3277  1.235    dyoung 		panic("invalid argument to %s", __func__);
   3278  1.235    dyoung 		/*NOTREACHED*/
   3279  1.235    dyoung 	}
   3280  1.235    dyoung 
   3281  1.235    dyoung 	/* address must be specified on ADD and DELETE */
   3282  1.235    dyoung 	sa = sstocsa(&ifap->ifap_addr);
   3283  1.235    dyoung 	if (sa->sa_family != sofamily(so))
   3284  1.235    dyoung 		return EINVAL;
   3285  1.235    dyoung 	if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
   3286  1.235    dyoung 		return EINVAL;
   3287  1.235    dyoung 
   3288  1.236  jakllsch 	sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
   3289  1.236  jakllsch 
   3290  1.357     ozaki 	s = pserialize_read_enter();
   3291  1.354     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
   3292  1.235    dyoung 		if (ifa->ifa_addr->sa_family != sa->sa_family)
   3293  1.235    dyoung 			continue;
   3294  1.235    dyoung 		sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
   3295  1.236  jakllsch 		if (sockaddr_cmp(&u.sa, &v.sa) == 0)
   3296  1.235    dyoung 			break;
   3297  1.235    dyoung 	}
   3298  1.357     ozaki 	if (ifa == NULL) {
   3299  1.357     ozaki 		error = EADDRNOTAVAIL;
   3300  1.357     ozaki 		goto out;
   3301  1.357     ozaki 	}
   3302  1.235    dyoung 
   3303  1.235    dyoung 	switch (cmd) {
   3304  1.235    dyoung 	case SIOCSIFADDRPREF:
   3305  1.235    dyoung 		ifa->ifa_preference = ifap->ifap_preference;
   3306  1.357     ozaki 		goto out;
   3307  1.235    dyoung 	case SIOCGIFADDRPREF:
   3308  1.235    dyoung 		/* fill in the if_laddrreq structure */
   3309  1.235    dyoung 		(void)sockaddr_copy(sstosa(&ifap->ifap_addr),
   3310  1.235    dyoung 		    sizeof(ifap->ifap_addr), ifa->ifa_addr);
   3311  1.235    dyoung 		ifap->ifap_preference = ifa->ifa_preference;
   3312  1.357     ozaki 		goto out;
   3313  1.235    dyoung 	default:
   3314  1.357     ozaki 		error = EOPNOTSUPP;
   3315  1.235    dyoung 	}
   3316  1.357     ozaki out:
   3317  1.357     ozaki 	pserialize_read_exit(s);
   3318  1.357     ozaki 	return error;
   3319  1.235    dyoung }
   3320  1.235    dyoung 
   3321    1.1       cgd /*
   3322    1.1       cgd  * Interface ioctls.
   3323    1.1       cgd  */
   3324  1.273     pooka static int
   3325  1.273     pooka doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
   3326    1.1       cgd {
   3327   1.61  augustss 	struct ifnet *ifp;
   3328   1.61  augustss 	struct ifreq *ifr;
   3329  1.454   msaitoh 	int error = 0;
   3330  1.191  christos 	u_long ocmd = cmd;
   3331  1.460   msaitoh 	u_short oif_flags;
   3332  1.186  christos 	struct ifreq ifrb;
   3333  1.187   xtraeme 	struct oifreq *oifr = NULL;
   3334  1.292  christos 	int r;
   3335  1.336     ozaki 	struct psref psref;
   3336  1.339     ozaki 	int bound;
   3337  1.454   msaitoh 	bool do_if43_post = false;
   3338  1.454   msaitoh 	bool do_ifm80_post = false;
   3339    1.1       cgd 
   3340    1.1       cgd 	switch (cmd) {
   3341    1.1       cgd 	case SIOCGIFCONF:
   3342  1.185    dyoung 		return ifconf(cmd, data);
   3343  1.231    dyoung 	case SIOCINITIFADDR:
   3344  1.231    dyoung 		return EPERM;
   3345  1.420  christos 	default:
   3346  1.446  pgoyette 		MODULE_HOOK_CALL(uipc_syscalls_40_hook, (cmd, data), enosys(),
   3347  1.444  pgoyette 		    error);
   3348  1.420  christos 		if (error != ENOSYS)
   3349  1.420  christos 			return error;
   3350  1.446  pgoyette 		MODULE_HOOK_CALL(uipc_syscalls_50_hook, (l, cmd, data),
   3351  1.444  pgoyette 		    enosys(), error);
   3352  1.420  christos 		if (error != ENOSYS)
   3353  1.420  christos 			return error;
   3354  1.444  pgoyette 		error = 0;
   3355  1.420  christos 		break;
   3356    1.1       cgd 	}
   3357  1.191  christos 
   3358  1.420  christos 	ifr = data;
   3359  1.451  pgoyette 	/* Pre-conversion */
   3360  1.454   msaitoh 	MODULE_HOOK_CALL(if_cvtcmd_43_hook, (&cmd, ocmd), enosys(), error);
   3361  1.454   msaitoh 	if (cmd != ocmd) {
   3362  1.454   msaitoh 		oifr = data;
   3363  1.454   msaitoh 		data = ifr = &ifrb;
   3364  1.454   msaitoh 		IFREQO2N_43(oifr, ifr);
   3365  1.454   msaitoh 		do_if43_post = true;
   3366  1.420  christos 	}
   3367  1.454   msaitoh 	MODULE_HOOK_CALL(ifmedia_80_pre_hook, (ifr, &cmd, &do_ifm80_post),
   3368  1.454   msaitoh 	    enosys(), error);
   3369   1.63   thorpej 
   3370   1.63   thorpej 	switch (cmd) {
   3371   1.63   thorpej 	case SIOCIFCREATE:
   3372   1.63   thorpej 	case SIOCIFDESTROY:
   3373  1.339     ozaki 		bound = curlwp_bind();
   3374  1.185    dyoung 		if (l != NULL) {
   3375  1.336     ozaki 			ifp = if_get(ifr->ifr_name, &psref);
   3376  1.490  christos 			error = kauth_authorize_network(l->l_cred,
   3377  1.174      elad 			    KAUTH_NETWORK_INTERFACE,
   3378  1.174      elad 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
   3379  1.466  christos 			    KAUTH_ARG(cmd), NULL);
   3380  1.336     ozaki 			if (ifp != NULL)
   3381  1.336     ozaki 				if_put(ifp, &psref);
   3382  1.336     ozaki 			if (error != 0) {
   3383  1.339     ozaki 				curlwp_bindx(bound);
   3384  1.151      yamt 				return error;
   3385  1.336     ozaki 			}
   3386  1.151      yamt 		}
   3387  1.399     ozaki 		KERNEL_LOCK_UNLESS_NET_MPSAFE();
   3388  1.292  christos 		mutex_enter(&if_clone_mtx);
   3389  1.292  christos 		r = (cmd == SIOCIFCREATE) ?
   3390   1.64   thorpej 			if_clone_create(ifr->ifr_name) :
   3391  1.185    dyoung 			if_clone_destroy(ifr->ifr_name);
   3392  1.292  christos 		mutex_exit(&if_clone_mtx);
   3393  1.399     ozaki 		KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
   3394  1.339     ozaki 		curlwp_bindx(bound);
   3395  1.292  christos 		return r;
   3396   1.67   thorpej 
   3397   1.67   thorpej 	case SIOCIFGCLONERS:
   3398  1.315    martin 		{
   3399  1.315    martin 			struct if_clonereq *req = (struct if_clonereq *)data;
   3400  1.315    martin 			return if_clone_list(req->ifcr_count, req->ifcr_buffer,
   3401  1.315    martin 			    &req->ifcr_total);
   3402  1.315    martin 		}
   3403   1.63   thorpej 	}
   3404   1.63   thorpej 
   3405  1.339     ozaki 	bound = curlwp_bind();
   3406  1.336     ozaki 	ifp = if_get(ifr->ifr_name, &psref);
   3407  1.336     ozaki 	if (ifp == NULL) {
   3408  1.339     ozaki 		curlwp_bindx(bound);
   3409  1.185    dyoung 		return ENXIO;
   3410  1.336     ozaki 	}
   3411  1.151      yamt 
   3412  1.151      yamt 	switch (cmd) {
   3413  1.233  christos 	case SIOCALIFADDR:
   3414  1.233  christos 	case SIOCDLIFADDR:
   3415  1.233  christos 	case SIOCSIFADDRPREF:
   3416  1.151      yamt 	case SIOCSIFFLAGS:
   3417  1.151      yamt 	case SIOCSIFCAP:
   3418  1.151      yamt 	case SIOCSIFMETRIC:
   3419  1.151      yamt 	case SIOCZIFDATA:
   3420  1.151      yamt 	case SIOCSIFMTU:
   3421  1.151      yamt 	case SIOCSIFPHYADDR:
   3422  1.151      yamt 	case SIOCDIFPHYADDR:
   3423  1.151      yamt #ifdef INET6
   3424  1.151      yamt 	case SIOCSIFPHYADDR_IN6:
   3425  1.151      yamt #endif
   3426  1.151      yamt 	case SIOCSLIFPHYADDR:
   3427  1.151      yamt 	case SIOCADDMULTI:
   3428  1.151      yamt 	case SIOCDELMULTI:
   3429  1.443   msaitoh 	case SIOCSETHERCAP:
   3430  1.151      yamt 	case SIOCSIFMEDIA:
   3431  1.154     perry 	case SIOCSDRVSPEC:
   3432  1.196       skd 	case SIOCG80211:
   3433  1.196       skd 	case SIOCS80211:
   3434  1.151      yamt 	case SIOCS80211NWID:
   3435  1.151      yamt 	case SIOCS80211NWKEY:
   3436  1.151      yamt 	case SIOCS80211POWER:
   3437  1.151      yamt 	case SIOCS80211BSSID:
   3438  1.151      yamt 	case SIOCS80211CHANNEL:
   3439  1.249     pooka 	case SIOCSLINKSTR:
   3440  1.185    dyoung 		if (l != NULL) {
   3441  1.490  christos 			error = kauth_authorize_network(l->l_cred,
   3442  1.174      elad 			    KAUTH_NETWORK_INTERFACE,
   3443  1.174      elad 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
   3444  1.466  christos 			    KAUTH_ARG(cmd), NULL);
   3445  1.185    dyoung 			if (error != 0)
   3446  1.336     ozaki 				goto out;
   3447  1.151      yamt 		}
   3448  1.151      yamt 	}
   3449  1.151      yamt 
   3450   1.49    itojun 	oif_flags = ifp->if_flags;
   3451    1.1       cgd 
   3452  1.399     ozaki 	KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp);
   3453  1.413     ozaki 	IFNET_LOCK(ifp);
   3454  1.336     ozaki 
   3455  1.231    dyoung 	error = (*ifp->if_ioctl)(ifp, cmd, data);
   3456  1.231    dyoung 	if (error != ENOTTY)
   3457  1.231    dyoung 		;
   3458  1.231    dyoung 	else if (so->so_proto == NULL)
   3459  1.252    dyoung 		error = EOPNOTSUPP;
   3460  1.231    dyoung 	else {
   3461  1.399     ozaki 		KERNEL_LOCK_IF_IFP_MPSAFE(ifp);
   3462  1.446  pgoyette 		MODULE_HOOK_CALL(if_ifioctl_43_hook,
   3463  1.444  pgoyette 			     (so, ocmd, cmd, data, l), enosys(), error);
   3464  1.444  pgoyette 		if (error == ENOSYS)
   3465  1.361  pgoyette 			error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
   3466  1.361  pgoyette 			    cmd, data, ifp);
   3467  1.399     ozaki 		KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp);
   3468   1.49    itojun 	}
   3469    1.1       cgd 
   3470   1.49    itojun 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
   3471  1.312       roy 		if ((ifp->if_flags & IFF_UP) != 0) {
   3472  1.373     ozaki 			int s = splsoftnet();
   3473  1.403     ozaki 			if_up_locked(ifp);
   3474   1.49    itojun 			splx(s);
   3475   1.49    itojun 		}
   3476    1.1       cgd 	}
   3477  1.450   msaitoh 
   3478  1.451  pgoyette 	/* Post-conversion */
   3479  1.454   msaitoh 	if (do_ifm80_post && (error == 0))
   3480  1.454   msaitoh 		MODULE_HOOK_CALL(ifmedia_80_post_hook, (ifr, cmd),
   3481  1.454   msaitoh 		    enosys(), error);
   3482  1.454   msaitoh 	if (do_if43_post)
   3483  1.450   msaitoh 		IFREQN2O_43(oifr, ifr);
   3484   1.49    itojun 
   3485  1.413     ozaki 	IFNET_UNLOCK(ifp);
   3486  1.399     ozaki 	KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp);
   3487  1.336     ozaki out:
   3488  1.336     ozaki 	if_put(ifp, &psref);
   3489  1.339     ozaki 	curlwp_bindx(bound);
   3490  1.185    dyoung 	return error;
   3491    1.1       cgd }
   3492    1.1       cgd 
   3493    1.1       cgd /*
   3494    1.1       cgd  * Return interface configuration
   3495    1.1       cgd  * of system.  List may be used
   3496    1.1       cgd  * in later ioctl's (above) to get
   3497    1.1       cgd  * other information.
   3498  1.200       gdt  *
   3499  1.200       gdt  * Each record is a struct ifreq.  Before the addition of
   3500  1.200       gdt  * sockaddr_storage, the API rule was that sockaddr flavors that did
   3501  1.200       gdt  * not fit would extend beyond the struct ifreq, with the next struct
   3502  1.200       gdt  * ifreq starting sa_len beyond the struct sockaddr.  Because the
   3503  1.200       gdt  * union in struct ifreq includes struct sockaddr_storage, every kind
   3504  1.200       gdt  * of sockaddr must fit.  Thus, there are no longer any overlength
   3505  1.200       gdt  * records.
   3506  1.200       gdt  *
   3507  1.200       gdt  * Records are added to the user buffer if they fit, and ifc_len is
   3508  1.200       gdt  * adjusted to the length that was written.  Thus, the user is only
   3509  1.200       gdt  * assured of getting the complete list if ifc_len on return is at
   3510  1.200       gdt  * least sizeof(struct ifreq) less than it was on entry.
   3511  1.200       gdt  *
   3512  1.200       gdt  * If the user buffer pointer is NULL, this routine copies no data and
   3513  1.200       gdt  * returns the amount of space that would be needed.
   3514  1.200       gdt  *
   3515  1.200       gdt  * Invariants:
   3516  1.200       gdt  * ifrp points to the next part of the user's buffer to be used.  If
   3517  1.200       gdt  * ifrp != NULL, space holds the number of bytes remaining that we may
   3518  1.200       gdt  * write at ifrp.  Otherwise, space holds the number of bytes that
   3519  1.200       gdt  * would have been written had there been adequate space.
   3520    1.1       cgd  */
   3521    1.1       cgd /*ARGSUSED*/
   3522  1.302     ozaki static int
   3523  1.183  christos ifconf(u_long cmd, void *data)
   3524    1.1       cgd {
   3525   1.61  augustss 	struct ifconf *ifc = (struct ifconf *)data;
   3526   1.61  augustss 	struct ifnet *ifp;
   3527   1.61  augustss 	struct ifaddr *ifa;
   3528  1.304     ozaki 	struct ifreq ifr, *ifrp = NULL;
   3529  1.304     ozaki 	int space = 0, error = 0;
   3530  1.200       gdt 	const int sz = (int)sizeof(struct ifreq);
   3531  1.304     ozaki 	const bool docopy = ifc->ifc_req != NULL;
   3532  1.334     ozaki 	int s;
   3533  1.339     ozaki 	int bound;
   3534  1.334     ozaki 	struct psref psref;
   3535    1.1       cgd 
   3536  1.304     ozaki 	if (docopy) {
   3537  1.479   msaitoh 		if (ifc->ifc_len < 0)
   3538  1.479   msaitoh 			return EINVAL;
   3539  1.479   msaitoh 
   3540  1.190     enami 		space = ifc->ifc_len;
   3541  1.304     ozaki 		ifrp = ifc->ifc_req;
   3542  1.304     ozaki 	}
   3543  1.479   msaitoh 	memset(&ifr, 0, sizeof(ifr));
   3544  1.304     ozaki 
   3545  1.339     ozaki 	bound = curlwp_bind();
   3546  1.334     ozaki 	s = pserialize_read_enter();
   3547  1.334     ozaki 	IFNET_READER_FOREACH(ifp) {
   3548  1.334     ozaki 		psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
   3549  1.334     ozaki 		pserialize_read_exit(s);
   3550  1.334     ozaki 
   3551  1.175  christos 		(void)strncpy(ifr.ifr_name, ifp->if_xname,
   3552  1.173  christos 		    sizeof(ifr.ifr_name));
   3553  1.334     ozaki 		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
   3554  1.334     ozaki 			error = ENAMETOOLONG;
   3555  1.334     ozaki 			goto release_exit;
   3556  1.334     ozaki 		}
   3557  1.354     ozaki 		if (IFADDR_READER_EMPTY(ifp)) {
   3558  1.200       gdt 			/* Interface with no addresses - send zero sockaddr. */
   3559  1.127  christos 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   3560  1.304     ozaki 			if (!docopy) {
   3561  1.218    dyoung 				space += sz;
   3562  1.400     ozaki 				goto next;
   3563  1.218    dyoung 			}
   3564  1.218    dyoung 			if (space >= sz) {
   3565  1.218    dyoung 				error = copyout(&ifr, ifrp, sz);
   3566  1.218    dyoung 				if (error != 0)
   3567  1.334     ozaki 					goto release_exit;
   3568  1.218    dyoung 				ifrp++;
   3569  1.218    dyoung 				space -= sz;
   3570   1.70    mellon 			}
   3571  1.127  christos 		}
   3572  1.127  christos 
   3573  1.400     ozaki 		s = pserialize_read_enter();
   3574  1.354     ozaki 		IFADDR_READER_FOREACH(ifa, ifp) {
   3575   1.61  augustss 			struct sockaddr *sa = ifa->ifa_addr;
   3576  1.200       gdt 			/* all sockaddrs must fit in sockaddr_storage */
   3577  1.200       gdt 			KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
   3578  1.200       gdt 
   3579  1.304     ozaki 			if (!docopy) {
   3580  1.218    dyoung 				space += sz;
   3581  1.218    dyoung 				continue;
   3582  1.218    dyoung 			}
   3583  1.218    dyoung 			memcpy(&ifr.ifr_space, sa, sa->sa_len);
   3584  1.400     ozaki 			pserialize_read_exit(s);
   3585  1.400     ozaki 
   3586  1.218    dyoung 			if (space >= sz) {
   3587  1.218    dyoung 				error = copyout(&ifr, ifrp, sz);
   3588  1.218    dyoung 				if (error != 0)
   3589  1.334     ozaki 					goto release_exit;
   3590  1.218    dyoung 				ifrp++; space -= sz;
   3591    1.1       cgd 			}
   3592  1.400     ozaki 			s = pserialize_read_enter();
   3593    1.1       cgd 		}
   3594  1.400     ozaki 		pserialize_read_exit(s);
   3595  1.334     ozaki 
   3596  1.400     ozaki         next:
   3597  1.334     ozaki 		s = pserialize_read_enter();
   3598  1.334     ozaki 		psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
   3599    1.1       cgd 	}
   3600  1.334     ozaki 	pserialize_read_exit(s);
   3601  1.339     ozaki 	curlwp_bindx(bound);
   3602  1.334     ozaki 
   3603  1.304     ozaki 	if (docopy) {
   3604  1.200       gdt 		KASSERT(0 <= space && space <= ifc->ifc_len);
   3605  1.127  christos 		ifc->ifc_len -= space;
   3606  1.218    dyoung 	} else {
   3607  1.200       gdt 		KASSERT(space >= 0);
   3608  1.200       gdt 		ifc->ifc_len = space;
   3609  1.200       gdt 	}
   3610  1.190     enami 	return (0);
   3611  1.334     ozaki 
   3612  1.334     ozaki release_exit:
   3613  1.334     ozaki 	psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
   3614  1.339     ozaki 	curlwp_bindx(bound);
   3615  1.334     ozaki 	return error;
   3616    1.1       cgd }
   3617  1.133  jonathan 
   3618  1.198    dyoung int
   3619  1.247  christos ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
   3620  1.198    dyoung {
   3621  1.420  christos 	uint8_t len = sizeof(ifr->ifr_ifru.ifru_space);
   3622  1.247  christos 	struct ifreq ifrb;
   3623  1.247  christos 	struct oifreq *oifr = NULL;
   3624  1.247  christos 	u_long ocmd = cmd;
   3625  1.444  pgoyette 	int hook;
   3626  1.420  christos 
   3627  1.446  pgoyette 	MODULE_HOOK_CALL(if_cvtcmd_43_hook, (&cmd, ocmd), enosys(), hook);
   3628  1.444  pgoyette 	if (hook != ENOSYS) {
   3629  1.444  pgoyette 		if (cmd != ocmd) {
   3630  1.444  pgoyette 			oifr = (struct oifreq *)(void *)ifr;
   3631  1.444  pgoyette 			ifr = &ifrb;
   3632  1.450   msaitoh 			IFREQO2N_43(oifr, ifr);
   3633  1.444  pgoyette 				len = sizeof(oifr->ifr_addr);
   3634  1.444  pgoyette 		}
   3635  1.420  christos 	}
   3636  1.444  pgoyette 
   3637  1.198    dyoung 	if (len < sa->sa_len)
   3638  1.198    dyoung 		return EFBIG;
   3639  1.247  christos 
   3640  1.241     joerg 	memset(&ifr->ifr_addr, 0, len);
   3641  1.202    dyoung 	sockaddr_copy(&ifr->ifr_addr, len, sa);
   3642  1.247  christos 
   3643  1.247  christos 	if (cmd != ocmd)
   3644  1.450   msaitoh 		IFREQN2O_43(oifr, ifr);
   3645  1.198    dyoung 	return 0;
   3646  1.198    dyoung }
   3647  1.198    dyoung 
   3648  1.155  christos /*
   3649  1.332  knakahar  * wrapper function for the drivers which doesn't have if_transmit().
   3650  1.155  christos  */
   3651  1.345  knakahar static int
   3652  1.332  knakahar if_transmit(struct ifnet *ifp, struct mbuf *m)
   3653  1.155  christos {
   3654  1.332  knakahar 	int s, error;
   3655  1.389     ozaki 	size_t pktlen = m->m_pkthdr.len;
   3656  1.389     ozaki 	bool mcast = (m->m_flags & M_MCAST) != 0;
   3657  1.332  knakahar 
   3658  1.332  knakahar 	s = splnet();
   3659  1.155  christos 
   3660  1.330  knakahar 	IFQ_ENQUEUE(&ifp->if_snd, m, error);
   3661  1.332  knakahar 	if (error != 0) {
   3662  1.332  knakahar 		/* mbuf is already freed */
   3663  1.185    dyoung 		goto out;
   3664  1.332  knakahar 	}
   3665  1.332  knakahar 
   3666  1.469   thorpej 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   3667  1.469   thorpej 	if_statadd_ref(nsr, if_obytes, pktlen);
   3668  1.389     ozaki 	if (mcast)
   3669  1.469   thorpej 		if_statinc_ref(nsr, if_omcasts);
   3670  1.469   thorpej 	IF_STAT_PUTREF(ifp);
   3671  1.332  knakahar 
   3672  1.155  christos 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
   3673  1.343  knakahar 		if_start_lock(ifp);
   3674  1.185    dyoung out:
   3675  1.155  christos 	splx(s);
   3676  1.332  knakahar 
   3677  1.155  christos 	return error;
   3678  1.155  christos }
   3679  1.155  christos 
   3680  1.345  knakahar int
   3681  1.345  knakahar if_transmit_lock(struct ifnet *ifp, struct mbuf *m)
   3682  1.345  knakahar {
   3683  1.345  knakahar 	int error;
   3684  1.345  knakahar 
   3685  1.465      maxv 	kmsan_check_mbuf(m);
   3686  1.465      maxv 
   3687  1.345  knakahar #ifdef ALTQ
   3688  1.345  knakahar 	KERNEL_LOCK(1, NULL);
   3689  1.345  knakahar 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
   3690  1.345  knakahar 		error = if_transmit(ifp, m);
   3691  1.345  knakahar 		KERNEL_UNLOCK_ONE(NULL);
   3692  1.345  knakahar 	} else {
   3693  1.345  knakahar 		KERNEL_UNLOCK_ONE(NULL);
   3694  1.345  knakahar 		error = (*ifp->if_transmit)(ifp, m);
   3695  1.383  knakahar 		/* mbuf is alredy freed */
   3696  1.345  knakahar 	}
   3697  1.345  knakahar #else /* !ALTQ */
   3698  1.345  knakahar 	error = (*ifp->if_transmit)(ifp, m);
   3699  1.383  knakahar 	/* mbuf is alredy freed */
   3700  1.345  knakahar #endif /* !ALTQ */
   3701  1.345  knakahar 
   3702  1.345  knakahar 	return error;
   3703  1.345  knakahar }
   3704  1.345  knakahar 
   3705  1.155  christos /*
   3706  1.332  knakahar  * Queue message on interface, and start output if interface
   3707  1.332  knakahar  * not yet active.
   3708  1.332  knakahar  */
   3709  1.332  knakahar int
   3710  1.332  knakahar ifq_enqueue(struct ifnet *ifp, struct mbuf *m)
   3711  1.332  knakahar {
   3712  1.332  knakahar 
   3713  1.345  knakahar 	return if_transmit_lock(ifp, m);
   3714  1.332  knakahar }
   3715  1.332  knakahar 
   3716  1.332  knakahar /*
   3717  1.155  christos  * Queue message on interface, possibly using a second fast queue
   3718  1.155  christos  */
   3719  1.155  christos int
   3720  1.330  knakahar ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m)
   3721  1.155  christos {
   3722  1.155  christos 	int error = 0;
   3723  1.155  christos 
   3724  1.155  christos 	if (ifq != NULL
   3725  1.155  christos #ifdef ALTQ
   3726  1.155  christos 	    && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
   3727  1.155  christos #endif
   3728  1.155  christos 	    ) {
   3729  1.155  christos 		if (IF_QFULL(ifq)) {
   3730  1.155  christos 			IF_DROP(&ifp->if_snd);
   3731  1.155  christos 			m_freem(m);
   3732  1.155  christos 			if (error == 0)
   3733  1.155  christos 				error = ENOBUFS;
   3734  1.185    dyoung 		} else
   3735  1.155  christos 			IF_ENQUEUE(ifq, m);
   3736  1.155  christos 	} else
   3737  1.330  knakahar 		IFQ_ENQUEUE(&ifp->if_snd, m, error);
   3738  1.155  christos 	if (error != 0) {
   3739  1.469   thorpej 		if_statinc(ifp, if_oerrors);
   3740  1.155  christos 		return error;
   3741  1.155  christos 	}
   3742  1.155  christos 	return 0;
   3743  1.155  christos }
   3744  1.155  christos 
   3745  1.252    dyoung int
   3746  1.252    dyoung if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
   3747  1.252    dyoung {
   3748  1.252    dyoung 	int rc;
   3749  1.252    dyoung 
   3750  1.413     ozaki 	KASSERT(IFNET_LOCKED(ifp));
   3751  1.252    dyoung 	if (ifp->if_initaddr != NULL)
   3752  1.252    dyoung 		rc = (*ifp->if_initaddr)(ifp, ifa, src);
   3753  1.252    dyoung 	else if (src ||
   3754  1.252    dyoung 	         (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
   3755  1.252    dyoung 		rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa);
   3756  1.252    dyoung 
   3757  1.252    dyoung 	return rc;
   3758  1.252    dyoung }
   3759  1.252    dyoung 
   3760  1.252    dyoung int
   3761  1.309       roy if_do_dad(struct ifnet *ifp)
   3762  1.309       roy {
   3763  1.309       roy 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
   3764  1.309       roy 		return 0;
   3765  1.309       roy 
   3766  1.309       roy 	switch (ifp->if_type) {
   3767  1.309       roy 	case IFT_FAITH:
   3768  1.309       roy 		/*
   3769  1.309       roy 		 * These interfaces do not have the IFF_LOOPBACK flag,
   3770  1.309       roy 		 * but loop packets back.  We do not have to do DAD on such
   3771  1.309       roy 		 * interfaces.  We should even omit it, because loop-backed
   3772  1.309       roy 		 * responses would confuse the DAD procedure.
   3773  1.309       roy 		 */
   3774  1.309       roy 		return 0;
   3775  1.309       roy 	default:
   3776  1.309       roy 		/*
   3777  1.309       roy 		 * Our DAD routine requires the interface up and running.
   3778  1.309       roy 		 * However, some interfaces can be up before the RUNNING
   3779  1.309       roy 		 * status.  Additionaly, users may try to assign addresses
   3780  1.309       roy 		 * before the interface becomes up (or running).
   3781  1.309       roy 		 * We simply skip DAD in such a case as a work around.
   3782  1.309       roy 		 * XXX: we should rather mark "tentative" on such addresses,
   3783  1.309       roy 		 * and do DAD after the interface becomes ready.
   3784  1.309       roy 		 */
   3785  1.455   msaitoh 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
   3786  1.455   msaitoh 		    (IFF_UP | IFF_RUNNING))
   3787  1.309       roy 			return 0;
   3788  1.309       roy 
   3789  1.309       roy 		return 1;
   3790  1.309       roy 	}
   3791  1.309       roy }
   3792  1.309       roy 
   3793  1.309       roy int
   3794  1.460   msaitoh if_flags_set(ifnet_t *ifp, const u_short flags)
   3795  1.252    dyoung {
   3796  1.252    dyoung 	int rc;
   3797  1.252    dyoung 
   3798  1.413     ozaki 	KASSERT(IFNET_LOCKED(ifp));
   3799  1.401     ozaki 
   3800  1.252    dyoung 	if (ifp->if_setflags != NULL)
   3801  1.252    dyoung 		rc = (*ifp->if_setflags)(ifp, flags);
   3802  1.252    dyoung 	else {
   3803  1.460   msaitoh 		u_short cantflags, chgdflags;
   3804  1.256    dyoung 		struct ifreq ifr;
   3805  1.256    dyoung 
   3806  1.259    dyoung 		chgdflags = ifp->if_flags ^ flags;
   3807  1.259    dyoung 		cantflags = chgdflags & IFF_CANTCHANGE;
   3808  1.256    dyoung 
   3809  1.256    dyoung 		if (cantflags != 0)
   3810  1.256    dyoung 			ifp->if_flags ^= cantflags;
   3811  1.256    dyoung 
   3812  1.259    dyoung                 /* Traditionally, we do not call if_ioctl after
   3813  1.259    dyoung                  * setting/clearing only IFF_PROMISC if the interface
   3814  1.259    dyoung                  * isn't IFF_UP.  Uphold that tradition.
   3815  1.259    dyoung 		 */
   3816  1.391     ozaki 		if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
   3817  1.391     ozaki 			return 0;
   3818  1.259    dyoung 
   3819  1.259    dyoung 		memset(&ifr, 0, sizeof(ifr));
   3820  1.259    dyoung 
   3821  1.256    dyoung 		ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
   3822  1.252    dyoung 		rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
   3823  1.256    dyoung 
   3824  1.256    dyoung 		if (rc != 0 && cantflags != 0)
   3825  1.256    dyoung 			ifp->if_flags ^= cantflags;
   3826  1.252    dyoung 	}
   3827  1.252    dyoung 
   3828  1.252    dyoung 	return rc;
   3829  1.252    dyoung }
   3830  1.252    dyoung 
   3831  1.252    dyoung int
   3832  1.252    dyoung if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
   3833  1.252    dyoung {
   3834  1.252    dyoung 	int rc;
   3835  1.252    dyoung 	struct ifreq ifr;
   3836  1.252    dyoung 
   3837  1.476  jdolecek 	/*
   3838  1.476  jdolecek 	 * XXX NOMPSAFE - this calls if_ioctl without holding IFNET_LOCK()
   3839  1.476  jdolecek 	 * in some cases - e.g. when called from vlan/netinet/netinet6 code
   3840  1.477  jdolecek 	 * directly rather than via doifoictl()
   3841  1.476  jdolecek 	 */
   3842  1.475  jdolecek 	ifreq_setaddr(cmd, &ifr, sa);
   3843  1.475  jdolecek 	rc = (*ifp->if_ioctl)(ifp, cmd, &ifr);
   3844  1.252    dyoung 
   3845  1.252    dyoung 	return rc;
   3846  1.252    dyoung }
   3847  1.155  christos 
   3848  1.234    dyoung static void
   3849  1.234    dyoung sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
   3850  1.234    dyoung     struct ifaltq *ifq)
   3851  1.234    dyoung {
   3852  1.234    dyoung 	const struct sysctlnode *cnode, *rnode;
   3853  1.234    dyoung 
   3854  1.234    dyoung 	if (sysctl_createv(clog, 0, NULL, &rnode,
   3855  1.234    dyoung 		       CTLFLAG_PERMANENT,
   3856  1.234    dyoung 		       CTLTYPE_NODE, "interfaces",
   3857  1.234    dyoung 		       SYSCTL_DESCR("Per-interface controls"),
   3858  1.234    dyoung 		       NULL, 0, NULL, 0,
   3859  1.272     pooka 		       CTL_NET, CTL_CREATE, CTL_EOL) != 0)
   3860  1.234    dyoung 		goto bad;
   3861  1.234    dyoung 
   3862  1.234    dyoung 	if (sysctl_createv(clog, 0, &rnode, &rnode,
   3863  1.234    dyoung 		       CTLFLAG_PERMANENT,
   3864  1.234    dyoung 		       CTLTYPE_NODE, ifname,
   3865  1.234    dyoung 		       SYSCTL_DESCR("Interface controls"),
   3866  1.234    dyoung 		       NULL, 0, NULL, 0,
   3867  1.234    dyoung 		       CTL_CREATE, CTL_EOL) != 0)
   3868  1.234    dyoung 		goto bad;
   3869  1.234    dyoung 
   3870  1.234    dyoung 	if (sysctl_createv(clog, 0, &rnode, &rnode,
   3871  1.234    dyoung 		       CTLFLAG_PERMANENT,
   3872  1.234    dyoung 		       CTLTYPE_NODE, "sndq",
   3873  1.234    dyoung 		       SYSCTL_DESCR("Interface output queue controls"),
   3874  1.234    dyoung 		       NULL, 0, NULL, 0,
   3875  1.234    dyoung 		       CTL_CREATE, CTL_EOL) != 0)
   3876  1.234    dyoung 		goto bad;
   3877  1.234    dyoung 
   3878  1.234    dyoung 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   3879  1.234    dyoung 		       CTLFLAG_PERMANENT,
   3880  1.234    dyoung 		       CTLTYPE_INT, "len",
   3881  1.234    dyoung 		       SYSCTL_DESCR("Current output queue length"),
   3882  1.234    dyoung 		       NULL, 0, &ifq->ifq_len, 0,
   3883  1.234    dyoung 		       CTL_CREATE, CTL_EOL) != 0)
   3884  1.234    dyoung 		goto bad;
   3885  1.234    dyoung 
   3886  1.234    dyoung 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   3887  1.455   msaitoh 		       CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   3888  1.234    dyoung 		       CTLTYPE_INT, "maxlen",
   3889  1.234    dyoung 		       SYSCTL_DESCR("Maximum allowed output queue length"),
   3890  1.234    dyoung 		       NULL, 0, &ifq->ifq_maxlen, 0,
   3891  1.234    dyoung 		       CTL_CREATE, CTL_EOL) != 0)
   3892  1.234    dyoung 		goto bad;
   3893  1.234    dyoung 
   3894  1.234    dyoung 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   3895  1.234    dyoung 		       CTLFLAG_PERMANENT,
   3896  1.234    dyoung 		       CTLTYPE_INT, "drops",
   3897  1.234    dyoung 		       SYSCTL_DESCR("Packets dropped due to full output queue"),
   3898  1.234    dyoung 		       NULL, 0, &ifq->ifq_drops, 0,
   3899  1.234    dyoung 		       CTL_CREATE, CTL_EOL) != 0)
   3900  1.234    dyoung 		goto bad;
   3901  1.234    dyoung 
   3902  1.234    dyoung 	return;
   3903  1.234    dyoung bad:
   3904  1.234    dyoung 	printf("%s: could not attach sysctl nodes\n", ifname);
   3905  1.234    dyoung 	return;
   3906  1.234    dyoung }
   3907  1.234    dyoung 
   3908  1.138  drochner #if defined(INET) || defined(INET6)
   3909  1.276     rmind 
   3910  1.276     rmind #define	SYSCTL_NET_PKTQ(q, cn, c)					\
   3911  1.276     rmind 	static int							\
   3912  1.276     rmind 	sysctl_net_##q##_##cn(SYSCTLFN_ARGS)				\
   3913  1.276     rmind 	{								\
   3914  1.276     rmind 		return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c);	\
   3915  1.276     rmind 	}
   3916  1.276     rmind 
   3917  1.276     rmind #if defined(INET)
   3918  1.279     rmind static int
   3919  1.279     rmind sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS)
   3920  1.279     rmind {
   3921  1.279     rmind 	return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq);
   3922  1.279     rmind }
   3923  1.276     rmind SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS)
   3924  1.276     rmind SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS)
   3925  1.276     rmind #endif
   3926  1.279     rmind 
   3927  1.276     rmind #if defined(INET6)
   3928  1.279     rmind static int
   3929  1.279     rmind sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS)
   3930  1.279     rmind {
   3931  1.279     rmind 	return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq);
   3932  1.279     rmind }
   3933  1.276     rmind SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS)
   3934  1.276     rmind SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS)
   3935  1.276     rmind #endif
   3936  1.276     rmind 
   3937  1.136    atatat static void
   3938  1.276     rmind sysctl_net_pktq_setup(struct sysctllog **clog, int pf)
   3939  1.136    atatat {
   3940  1.276     rmind 	sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL;
   3941  1.276     rmind 	const char *pfname = NULL, *ipname = NULL;
   3942  1.276     rmind 	int ipn = 0, qid = 0;
   3943  1.276     rmind 
   3944  1.276     rmind 	switch (pf) {
   3945  1.276     rmind #if defined(INET)
   3946  1.276     rmind 	case PF_INET:
   3947  1.276     rmind 		len_func = sysctl_net_ip_pktq_items;
   3948  1.276     rmind 		maxlen_func = sysctl_net_ip_pktq_maxlen;
   3949  1.276     rmind 		drops_func = sysctl_net_ip_pktq_drops;
   3950  1.276     rmind 		pfname = "inet", ipn = IPPROTO_IP;
   3951  1.276     rmind 		ipname = "ip", qid = IPCTL_IFQ;
   3952  1.276     rmind 		break;
   3953  1.276     rmind #endif
   3954  1.276     rmind #if defined(INET6)
   3955  1.276     rmind 	case PF_INET6:
   3956  1.276     rmind 		len_func = sysctl_net_ip6_pktq_items;
   3957  1.276     rmind 		maxlen_func = sysctl_net_ip6_pktq_maxlen;
   3958  1.276     rmind 		drops_func = sysctl_net_ip6_pktq_drops;
   3959  1.276     rmind 		pfname = "inet6", ipn = IPPROTO_IPV6;
   3960  1.276     rmind 		ipname = "ip6", qid = IPV6CTL_IFQ;
   3961  1.276     rmind 		break;
   3962  1.276     rmind #endif
   3963  1.276     rmind 	default:
   3964  1.276     rmind 		KASSERT(false);
   3965  1.276     rmind 	}
   3966  1.136    atatat 
   3967  1.139    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   3968  1.139    atatat 		       CTLFLAG_PERMANENT,
   3969  1.136    atatat 		       CTLTYPE_NODE, pfname, NULL,
   3970  1.136    atatat 		       NULL, 0, NULL, 0,
   3971  1.136    atatat 		       CTL_NET, pf, CTL_EOL);
   3972  1.139    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   3973  1.139    atatat 		       CTLFLAG_PERMANENT,
   3974  1.136    atatat 		       CTLTYPE_NODE, ipname, NULL,
   3975  1.136    atatat 		       NULL, 0, NULL, 0,
   3976  1.136    atatat 		       CTL_NET, pf, ipn, CTL_EOL);
   3977  1.139    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   3978  1.139    atatat 		       CTLFLAG_PERMANENT,
   3979  1.142    atatat 		       CTLTYPE_NODE, "ifq",
   3980  1.142    atatat 		       SYSCTL_DESCR("Protocol input queue controls"),
   3981  1.139    atatat 		       NULL, 0, NULL, 0,
   3982  1.139    atatat 		       CTL_NET, pf, ipn, qid, CTL_EOL);
   3983  1.136    atatat 
   3984  1.139    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   3985  1.139    atatat 		       CTLFLAG_PERMANENT,
   3986  1.432   msaitoh 		       CTLTYPE_QUAD, "len",
   3987  1.142    atatat 		       SYSCTL_DESCR("Current input queue length"),
   3988  1.276     rmind 		       len_func, 0, NULL, 0,
   3989  1.136    atatat 		       CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
   3990  1.139    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   3991  1.455   msaitoh 		       CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   3992  1.142    atatat 		       CTLTYPE_INT, "maxlen",
   3993  1.142    atatat 		       SYSCTL_DESCR("Maximum allowed input queue length"),
   3994  1.276     rmind 		       maxlen_func, 0, NULL, 0,
   3995  1.136    atatat 		       CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
   3996  1.139    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   3997  1.139    atatat 		       CTLFLAG_PERMANENT,
   3998  1.431   msaitoh 		       CTLTYPE_QUAD, "drops",
   3999  1.142    atatat 		       SYSCTL_DESCR("Packets dropped due to full input queue"),
   4000  1.276     rmind 		       drops_func, 0, NULL, 0,
   4001  1.136    atatat 		       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
   4002  1.136    atatat }
   4003  1.138  drochner #endif /* INET || INET6 */
   4004  1.280     joerg 
   4005  1.280     joerg static int
   4006  1.280     joerg if_sdl_sysctl(SYSCTLFN_ARGS)
   4007  1.280     joerg {
   4008  1.280     joerg 	struct ifnet *ifp;
   4009  1.280     joerg 	const struct sockaddr_dl *sdl;
   4010  1.340     ozaki 	struct psref psref;
   4011  1.340     ozaki 	int error = 0;
   4012  1.340     ozaki 	int bound;
   4013  1.280     joerg 
   4014  1.280     joerg 	if (namelen != 1)
   4015  1.280     joerg 		return EINVAL;
   4016  1.280     joerg 
   4017  1.340     ozaki 	bound = curlwp_bind();
   4018  1.340     ozaki 	ifp = if_get_byindex(name[0], &psref);
   4019  1.340     ozaki 	if (ifp == NULL) {
   4020  1.340     ozaki 		error = ENODEV;
   4021  1.341  riastrad 		goto out0;
   4022  1.340     ozaki 	}
   4023  1.280     joerg 
   4024  1.280     joerg 	sdl = ifp->if_sadl;
   4025  1.280     joerg 	if (sdl == NULL) {
   4026  1.280     joerg 		*oldlenp = 0;
   4027  1.341  riastrad 		goto out1;
   4028  1.280     joerg 	}
   4029  1.280     joerg 
   4030  1.280     joerg 	if (oldp == NULL) {
   4031  1.280     joerg 		*oldlenp = sdl->sdl_alen;
   4032  1.341  riastrad 		goto out1;
   4033  1.280     joerg 	}
   4034  1.280     joerg 
   4035  1.280     joerg 	if (*oldlenp >= sdl->sdl_alen)
   4036  1.280     joerg 		*oldlenp = sdl->sdl_alen;
   4037  1.340     ozaki 	error = sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp);
   4038  1.341  riastrad out1:
   4039  1.340     ozaki 	if_put(ifp, &psref);
   4040  1.341  riastrad out0:
   4041  1.340     ozaki 	curlwp_bindx(bound);
   4042  1.340     ozaki 	return error;
   4043  1.280     joerg }
   4044  1.280     joerg 
   4045  1.370     ozaki static void
   4046  1.370     ozaki if_sysctl_setup(struct sysctllog **clog)
   4047  1.280     joerg {
   4048  1.280     joerg 	const struct sysctlnode *rnode = NULL;
   4049  1.280     joerg 
   4050  1.280     joerg 	sysctl_createv(clog, 0, NULL, &rnode,
   4051  1.280     joerg 		       CTLFLAG_PERMANENT,
   4052  1.280     joerg 		       CTLTYPE_NODE, "sdl",
   4053  1.280     joerg 		       SYSCTL_DESCR("Get active link-layer address"),
   4054  1.280     joerg 		       if_sdl_sysctl, 0, NULL, 0,
   4055  1.280     joerg 		       CTL_NET, CTL_CREATE, CTL_EOL);
   4056  1.370     ozaki 
   4057  1.370     ozaki #if defined(INET)
   4058  1.370     ozaki 	sysctl_net_pktq_setup(NULL, PF_INET);
   4059  1.370     ozaki #endif
   4060  1.370     ozaki #ifdef INET6
   4061  1.370     ozaki 	if (in6_present)
   4062  1.370     ozaki 		sysctl_net_pktq_setup(NULL, PF_INET6);
   4063  1.370     ozaki #endif
   4064  1.280     joerg }
   4065