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