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