1 /* $NetBSD: if.c,v 1.262 2013/03/10 19:46:12 christos 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.262 2013/03/10 19:46:12 christos Exp $"); 94 95 #include "opt_inet.h" 96 97 #include "opt_atalk.h" 98 #include "opt_natm.h" 99 #include "opt_pfil_hooks.h" 100 101 #include <sys/param.h> 102 #include <sys/mbuf.h> 103 #include <sys/systm.h> 104 #include <sys/callout.h> 105 #include <sys/proc.h> 106 #include <sys/socket.h> 107 #include <sys/socketvar.h> 108 #include <sys/domain.h> 109 #include <sys/protosw.h> 110 #include <sys/kernel.h> 111 #include <sys/ioctl.h> 112 #include <sys/sysctl.h> 113 #include <sys/syslog.h> 114 #include <sys/kauth.h> 115 #include <sys/kmem.h> 116 117 #include <net/if.h> 118 #include <net/if_dl.h> 119 #include <net/if_ether.h> 120 #include <net/if_media.h> 121 #include <net80211/ieee80211.h> 122 #include <net80211/ieee80211_ioctl.h> 123 #include <net/if_types.h> 124 #include <net/radix.h> 125 #include <net/route.h> 126 #include <net/netisr.h> 127 #include <sys/module.h> 128 #ifdef NETATALK 129 #include <netatalk/at_extern.h> 130 #include <netatalk/at.h> 131 #endif 132 #include <net/pfil.h> 133 134 #ifdef INET6 135 #include <netinet/in.h> 136 #include <netinet6/in6_var.h> 137 #include <netinet6/nd6.h> 138 #endif 139 140 #include "carp.h" 141 #if NCARP > 0 142 #include <netinet/ip_carp.h> 143 #endif 144 145 #include <compat/sys/sockio.h> 146 #include <compat/sys/socket.h> 147 148 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 149 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 150 151 int ifqmaxlen = IFQ_MAXLEN; 152 callout_t if_slowtimo_ch; 153 154 int netisr; /* scheduling bits for network */ 155 156 static int if_rt_walktree(struct rtentry *, void *); 157 158 static struct if_clone *if_clone_lookup(const char *, int *); 159 static int if_clone_list(struct if_clonereq *); 160 161 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners); 162 static int if_cloners_count; 163 164 static uint64_t index_gen; 165 static kmutex_t index_gen_mtx; 166 167 #ifdef PFIL_HOOKS 168 struct pfil_head if_pfil; /* packet filtering hook for interfaces */ 169 #endif 170 171 static kauth_listener_t if_listener; 172 173 static int ifioctl_attach(struct ifnet *); 174 static void ifioctl_detach(struct ifnet *); 175 static void ifnet_lock_enter(struct ifnet_lock *); 176 static void ifnet_lock_exit(struct ifnet_lock *); 177 static void if_detach_queues(struct ifnet *, struct ifqueue *); 178 static void sysctl_sndq_setup(struct sysctllog **, const char *, 179 struct ifaltq *); 180 181 #if defined(INET) || defined(INET6) 182 static void sysctl_net_ifq_setup(struct sysctllog **, int, const char *, 183 int, const char *, int, struct ifqueue *); 184 #endif 185 186 static int 187 if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, 188 void *arg0, void *arg1, void *arg2, void *arg3) 189 { 190 int result; 191 enum kauth_network_req req; 192 193 result = KAUTH_RESULT_DEFER; 194 req = (enum kauth_network_req)arg1; 195 196 if (action != KAUTH_NETWORK_INTERFACE) 197 return result; 198 199 if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) || 200 (req == KAUTH_REQ_NETWORK_INTERFACE_SET)) 201 result = KAUTH_RESULT_ALLOW; 202 203 return result; 204 } 205 206 /* 207 * Network interface utility routines. 208 * 209 * Routines with ifa_ifwith* names take sockaddr *'s as 210 * parameters. 211 */ 212 void 213 ifinit(void) 214 { 215 #ifdef INET 216 {extern struct ifqueue ipintrq; 217 sysctl_net_ifq_setup(NULL, PF_INET, "inet", IPPROTO_IP, "ip", 218 IPCTL_IFQ, &ipintrq);} 219 #endif /* INET */ 220 #ifdef INET6 221 {extern struct ifqueue ip6intrq; 222 sysctl_net_ifq_setup(NULL, PF_INET6, "inet6", IPPROTO_IPV6, "ip6", 223 IPV6CTL_IFQ, &ip6intrq);} 224 #endif /* INET6 */ 225 226 callout_init(&if_slowtimo_ch, 0); 227 if_slowtimo(NULL); 228 229 if_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK, 230 if_listener_cb, NULL); 231 } 232 233 /* 234 * XXX Initialization before configure(). 235 * XXX hack to get pfil_add_hook working in autoconf. 236 */ 237 void 238 ifinit1(void) 239 { 240 241 mutex_init(&index_gen_mtx, MUTEX_DEFAULT, IPL_NONE); 242 243 #ifdef PFIL_HOOKS 244 if_pfil.ph_type = PFIL_TYPE_IFNET; 245 if_pfil.ph_ifnet = NULL; 246 if (pfil_head_register(&if_pfil) != 0) 247 printf("WARNING: unable to register pfil hook\n"); 248 #endif 249 } 250 251 struct ifnet * 252 if_alloc(u_char type) 253 { 254 return malloc(sizeof(struct ifnet), M_DEVBUF, M_WAITOK|M_ZERO); 255 } 256 257 void 258 if_free(struct ifnet *ifp) 259 { 260 free(ifp, M_DEVBUF); 261 } 262 263 void 264 if_initname(struct ifnet *ifp, const char *name, int unit) 265 { 266 (void)snprintf(ifp->if_xname, sizeof(ifp->if_xname), 267 "%s%d", name, unit); 268 } 269 270 /* 271 * Null routines used while an interface is going away. These routines 272 * just return an error. 273 */ 274 275 int 276 if_nulloutput(struct ifnet *ifp, struct mbuf *m, 277 const struct sockaddr *so, struct rtentry *rt) 278 { 279 280 return ENXIO; 281 } 282 283 void 284 if_nullinput(struct ifnet *ifp, struct mbuf *m) 285 { 286 287 /* Nothing. */ 288 } 289 290 void 291 if_nullstart(struct ifnet *ifp) 292 { 293 294 /* Nothing. */ 295 } 296 297 int 298 if_nullioctl(struct ifnet *ifp, u_long cmd, void *data) 299 { 300 301 /* Wake ifioctl_detach(), who may wait for all threads to 302 * quit the critical section. 303 */ 304 cv_signal(&ifp->if_ioctl_lock->il_emptied); 305 return ENXIO; 306 } 307 308 int 309 if_nullinit(struct ifnet *ifp) 310 { 311 312 return ENXIO; 313 } 314 315 void 316 if_nullstop(struct ifnet *ifp, int disable) 317 { 318 319 /* Nothing. */ 320 } 321 322 void 323 if_nullwatchdog(struct ifnet *ifp) 324 { 325 326 /* Nothing. */ 327 } 328 329 void 330 if_nulldrain(struct ifnet *ifp) 331 { 332 333 /* Nothing. */ 334 } 335 336 static u_int if_index = 1; 337 struct ifnet_head ifnet; 338 size_t if_indexlim = 0; 339 struct ifaddr **ifnet_addrs = NULL; 340 struct ifnet **ifindex2ifnet = NULL; 341 struct ifnet *lo0ifp; 342 343 void 344 if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory) 345 { 346 struct ifaddr *ifa; 347 struct sockaddr_dl *sdl; 348 349 ifp->if_addrlen = addrlen; 350 if_alloc_sadl(ifp); 351 ifa = ifp->if_dl; 352 sdl = satosdl(ifa->ifa_addr); 353 354 (void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen); 355 if (factory) { 356 ifp->if_hwdl = ifp->if_dl; 357 IFAREF(ifp->if_hwdl); 358 } 359 /* TBD routing socket */ 360 } 361 362 struct ifaddr * 363 if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp) 364 { 365 unsigned socksize, ifasize; 366 int addrlen, namelen; 367 struct sockaddr_dl *mask, *sdl; 368 struct ifaddr *ifa; 369 370 namelen = strlen(ifp->if_xname); 371 addrlen = ifp->if_addrlen; 372 socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long)); 373 ifasize = sizeof(*ifa) + 2 * socksize; 374 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK|M_ZERO); 375 376 sdl = (struct sockaddr_dl *)(ifa + 1); 377 mask = (struct sockaddr_dl *)(socksize + (char *)sdl); 378 379 sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type, 380 ifp->if_xname, namelen, NULL, addrlen); 381 mask->sdl_len = sockaddr_dl_measure(namelen, 0); 382 memset(&mask->sdl_data[0], 0xff, namelen); 383 ifa->ifa_rtrequest = link_rtrequest; 384 ifa->ifa_addr = (struct sockaddr *)sdl; 385 ifa->ifa_netmask = (struct sockaddr *)mask; 386 387 *sdlp = sdl; 388 389 return ifa; 390 } 391 392 static void 393 if_sadl_setrefs(struct ifnet *ifp, struct ifaddr *ifa) 394 { 395 const struct sockaddr_dl *sdl; 396 ifnet_addrs[ifp->if_index] = ifa; 397 IFAREF(ifa); 398 ifp->if_dl = ifa; 399 IFAREF(ifa); 400 sdl = satosdl(ifa->ifa_addr); 401 ifp->if_sadl = sdl; 402 } 403 404 /* 405 * Allocate the link level name for the specified interface. This 406 * is an attachment helper. It must be called after ifp->if_addrlen 407 * is initialized, which may not be the case when if_attach() is 408 * called. 409 */ 410 void 411 if_alloc_sadl(struct ifnet *ifp) 412 { 413 struct ifaddr *ifa; 414 const struct sockaddr_dl *sdl; 415 416 /* 417 * If the interface already has a link name, release it 418 * now. This is useful for interfaces that can change 419 * link types, and thus switch link names often. 420 */ 421 if (ifp->if_sadl != NULL) 422 if_free_sadl(ifp); 423 424 ifa = if_dl_create(ifp, &sdl); 425 426 ifa_insert(ifp, ifa); 427 if_sadl_setrefs(ifp, ifa); 428 } 429 430 static void 431 if_deactivate_sadl(struct ifnet *ifp) 432 { 433 struct ifaddr *ifa; 434 435 KASSERT(ifp->if_dl != NULL); 436 437 ifa = ifp->if_dl; 438 439 ifp->if_sadl = NULL; 440 441 ifnet_addrs[ifp->if_index] = NULL; 442 IFAFREE(ifa); 443 ifp->if_dl = NULL; 444 IFAFREE(ifa); 445 } 446 447 void 448 if_activate_sadl(struct ifnet *ifp, struct ifaddr *ifa, 449 const struct sockaddr_dl *sdl) 450 { 451 int s; 452 453 s = splnet(); 454 455 if_deactivate_sadl(ifp); 456 457 if_sadl_setrefs(ifp, ifa); 458 IFADDR_FOREACH(ifa, ifp) 459 rtinit(ifa, RTM_LLINFO_UPD, 0); 460 splx(s); 461 } 462 463 /* 464 * Free the link level name for the specified interface. This is 465 * a detach helper. This is called from if_detach() or from 466 * link layer type specific detach functions. 467 */ 468 void 469 if_free_sadl(struct ifnet *ifp) 470 { 471 struct ifaddr *ifa; 472 int s; 473 474 ifa = ifnet_addrs[ifp->if_index]; 475 if (ifa == NULL) { 476 KASSERT(ifp->if_sadl == NULL); 477 KASSERT(ifp->if_dl == NULL); 478 return; 479 } 480 481 KASSERT(ifp->if_sadl != NULL); 482 KASSERT(ifp->if_dl != NULL); 483 484 s = splnet(); 485 rtinit(ifa, RTM_DELETE, 0); 486 ifa_remove(ifp, ifa); 487 if_deactivate_sadl(ifp); 488 if (ifp->if_hwdl == ifa) { 489 IFAFREE(ifa); 490 ifp->if_hwdl = NULL; 491 } 492 splx(s); 493 } 494 495 /* 496 * Attach an interface to the 497 * list of "active" interfaces. 498 */ 499 void 500 if_attach(struct ifnet *ifp) 501 { 502 int indexlim = 0; 503 504 if (if_indexlim == 0) { 505 TAILQ_INIT(&ifnet); 506 if_indexlim = 8; 507 } 508 TAILQ_INIT(&ifp->if_addrlist); 509 TAILQ_INSERT_TAIL(&ifnet, ifp, if_list); 510 511 if (ifioctl_attach(ifp) != 0) 512 panic("%s: ifioctl_attach() failed", __func__); 513 514 mutex_enter(&index_gen_mtx); 515 ifp->if_index_gen = index_gen++; 516 mutex_exit(&index_gen_mtx); 517 518 ifp->if_index = if_index; 519 if (ifindex2ifnet == NULL) 520 if_index++; 521 else 522 while (ifp->if_index < if_indexlim && 523 ifindex2ifnet[ifp->if_index] != NULL) { 524 ++if_index; 525 if (if_index == 0) 526 if_index = 1; 527 /* 528 * If we hit USHRT_MAX, we skip back to 0 since 529 * there are a number of places where the value 530 * of if_index or if_index itself is compared 531 * to or stored in an unsigned short. By 532 * jumping back, we won't botch those assignments 533 * or comparisons. 534 */ 535 else if (if_index == USHRT_MAX) { 536 /* 537 * However, if we have to jump back to 538 * zero *twice* without finding an empty 539 * slot in ifindex2ifnet[], then there 540 * there are too many (>65535) interfaces. 541 */ 542 if (indexlim++) 543 panic("too many interfaces"); 544 else 545 if_index = 1; 546 } 547 ifp->if_index = if_index; 548 } 549 550 /* 551 * We have some arrays that should be indexed by if_index. 552 * since if_index will grow dynamically, they should grow too. 553 * struct ifadd **ifnet_addrs 554 * struct ifnet **ifindex2ifnet 555 */ 556 if (ifnet_addrs == NULL || ifindex2ifnet == NULL || 557 ifp->if_index >= if_indexlim) { 558 size_t m, n, oldlim; 559 void *q; 560 561 oldlim = if_indexlim; 562 while (ifp->if_index >= if_indexlim) 563 if_indexlim <<= 1; 564 565 /* grow ifnet_addrs */ 566 m = oldlim * sizeof(struct ifaddr *); 567 n = if_indexlim * sizeof(struct ifaddr *); 568 q = malloc(n, M_IFADDR, M_WAITOK|M_ZERO); 569 if (ifnet_addrs != NULL) { 570 memcpy(q, ifnet_addrs, m); 571 free(ifnet_addrs, M_IFADDR); 572 } 573 ifnet_addrs = (struct ifaddr **)q; 574 575 /* grow ifindex2ifnet */ 576 m = oldlim * sizeof(struct ifnet *); 577 n = if_indexlim * sizeof(struct ifnet *); 578 q = malloc(n, M_IFADDR, M_WAITOK|M_ZERO); 579 if (ifindex2ifnet != NULL) { 580 memcpy(q, ifindex2ifnet, m); 581 free(ifindex2ifnet, M_IFADDR); 582 } 583 ifindex2ifnet = (struct ifnet **)q; 584 } 585 586 ifindex2ifnet[ifp->if_index] = ifp; 587 588 /* 589 * Link level name is allocated later by a separate call to 590 * if_alloc_sadl(). 591 */ 592 593 if (ifp->if_snd.ifq_maxlen == 0) 594 ifp->if_snd.ifq_maxlen = ifqmaxlen; 595 596 sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd); 597 598 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */ 599 600 ifp->if_link_state = LINK_STATE_UNKNOWN; 601 602 ifp->if_capenable = 0; 603 ifp->if_csum_flags_tx = 0; 604 ifp->if_csum_flags_rx = 0; 605 606 #ifdef ALTQ 607 ifp->if_snd.altq_type = 0; 608 ifp->if_snd.altq_disc = NULL; 609 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE; 610 ifp->if_snd.altq_tbr = NULL; 611 ifp->if_snd.altq_ifp = ifp; 612 #endif 613 614 #ifdef PFIL_HOOKS 615 ifp->if_pfil.ph_type = PFIL_TYPE_IFNET; 616 ifp->if_pfil.ph_ifnet = ifp; 617 if (pfil_head_register(&ifp->if_pfil) != 0) 618 printf("%s: WARNING: unable to register pfil hook\n", 619 ifp->if_xname); 620 (void)pfil_run_hooks(&if_pfil, 621 (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET); 622 #endif 623 624 if (!STAILQ_EMPTY(&domains)) 625 if_attachdomain1(ifp); 626 627 /* Announce the interface. */ 628 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 629 } 630 631 void 632 if_attachdomain(void) 633 { 634 struct ifnet *ifp; 635 int s; 636 637 s = splnet(); 638 IFNET_FOREACH(ifp) 639 if_attachdomain1(ifp); 640 splx(s); 641 } 642 643 void 644 if_attachdomain1(struct ifnet *ifp) 645 { 646 struct domain *dp; 647 int s; 648 649 s = splnet(); 650 651 /* address family dependent data region */ 652 memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata)); 653 DOMAIN_FOREACH(dp) { 654 if (dp->dom_ifattach != NULL) 655 ifp->if_afdata[dp->dom_family] = 656 (*dp->dom_ifattach)(ifp); 657 } 658 659 splx(s); 660 } 661 662 /* 663 * Deactivate an interface. This points all of the procedure 664 * handles at error stubs. May be called from interrupt context. 665 */ 666 void 667 if_deactivate(struct ifnet *ifp) 668 { 669 int s; 670 671 s = splnet(); 672 673 ifp->if_output = if_nulloutput; 674 ifp->if_input = if_nullinput; 675 ifp->if_start = if_nullstart; 676 ifp->if_ioctl = if_nullioctl; 677 ifp->if_init = if_nullinit; 678 ifp->if_stop = if_nullstop; 679 ifp->if_watchdog = if_nullwatchdog; 680 ifp->if_drain = if_nulldrain; 681 682 /* No more packets may be enqueued. */ 683 ifp->if_snd.ifq_maxlen = 0; 684 685 splx(s); 686 } 687 688 void 689 if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *)) 690 { 691 struct ifaddr *ifa, *nifa; 692 693 for (ifa = IFADDR_FIRST(ifp); ifa != NULL; ifa = nifa) { 694 nifa = IFADDR_NEXT(ifa); 695 if (ifa->ifa_addr->sa_family != family) 696 continue; 697 (*purgeaddr)(ifa); 698 } 699 } 700 701 /* 702 * Detach an interface from the list of "active" interfaces, 703 * freeing any resources as we go along. 704 * 705 * NOTE: This routine must be called with a valid thread context, 706 * as it may block. 707 */ 708 void 709 if_detach(struct ifnet *ifp) 710 { 711 struct socket so; 712 struct ifaddr *ifa; 713 #ifdef IFAREF_DEBUG 714 struct ifaddr *last_ifa = NULL; 715 #endif 716 struct domain *dp; 717 const struct protosw *pr; 718 int s, i, family, purged; 719 720 /* 721 * XXX It's kind of lame that we have to have the 722 * XXX socket structure... 723 */ 724 memset(&so, 0, sizeof(so)); 725 726 s = splnet(); 727 728 /* 729 * Do an if_down() to give protocols a chance to do something. 730 */ 731 if_down(ifp); 732 733 #ifdef ALTQ 734 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 735 altq_disable(&ifp->if_snd); 736 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 737 altq_detach(&ifp->if_snd); 738 #endif 739 740 sysctl_teardown(&ifp->if_sysctl_log); 741 742 #if NCARP > 0 743 /* Remove the interface from any carp group it is a part of. */ 744 if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP) 745 carp_ifdetach(ifp); 746 #endif 747 748 /* 749 * Rip all the addresses off the interface. This should make 750 * all of the routes go away. 751 * 752 * pr_usrreq calls can remove an arbitrary number of ifaddrs 753 * from the list, including our "cursor", ifa. For safety, 754 * and to honor the TAILQ abstraction, I just restart the 755 * loop after each removal. Note that the loop will exit 756 * when all of the remaining ifaddrs belong to the AF_LINK 757 * family. I am counting on the historical fact that at 758 * least one pr_usrreq in each address domain removes at 759 * least one ifaddr. 760 */ 761 again: 762 IFADDR_FOREACH(ifa, ifp) { 763 family = ifa->ifa_addr->sa_family; 764 #ifdef IFAREF_DEBUG 765 printf("if_detach: ifaddr %p, family %d, refcnt %d\n", 766 ifa, family, ifa->ifa_refcnt); 767 if (last_ifa != NULL && ifa == last_ifa) 768 panic("if_detach: loop detected"); 769 last_ifa = ifa; 770 #endif 771 if (family == AF_LINK) 772 continue; 773 dp = pffinddomain(family); 774 #ifdef DIAGNOSTIC 775 if (dp == NULL) 776 panic("if_detach: no domain for AF %d", 777 family); 778 #endif 779 /* 780 * XXX These PURGEIF calls are redundant with the 781 * purge-all-families calls below, but are left in for 782 * now both to make a smaller change, and to avoid 783 * unplanned interactions with clearing of 784 * ifp->if_addrlist. 785 */ 786 purged = 0; 787 for (pr = dp->dom_protosw; 788 pr < dp->dom_protoswNPROTOSW; pr++) { 789 so.so_proto = pr; 790 if (pr->pr_usrreq != NULL) { 791 (void) (*pr->pr_usrreq)(&so, 792 PRU_PURGEIF, NULL, NULL, 793 (struct mbuf *) ifp, curlwp); 794 purged = 1; 795 } 796 } 797 if (purged == 0) { 798 /* 799 * XXX What's really the best thing to do 800 * XXX here? --thorpej@NetBSD.org 801 */ 802 printf("if_detach: WARNING: AF %d not purged\n", 803 family); 804 ifa_remove(ifp, ifa); 805 } 806 goto again; 807 } 808 809 if_free_sadl(ifp); 810 811 /* Walk the routing table looking for stragglers. */ 812 for (i = 0; i <= AF_MAX; i++) { 813 while (rt_walktree(i, if_rt_walktree, ifp) == ERESTART) 814 continue; 815 } 816 817 DOMAIN_FOREACH(dp) { 818 if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family]) 819 { 820 void *p = ifp->if_afdata[dp->dom_family]; 821 if (p) { 822 ifp->if_afdata[dp->dom_family] = NULL; 823 (*dp->dom_ifdetach)(ifp, p); 824 } 825 } 826 827 /* 828 * One would expect multicast memberships (INET and 829 * INET6) on UDP sockets to be purged by the PURGEIF 830 * calls above, but if all addresses were removed from 831 * the interface prior to destruction, the calls will 832 * not be made (e.g. ppp, for which pppd(8) generally 833 * removes addresses before destroying the interface). 834 * Because there is no invariant that multicast 835 * memberships only exist for interfaces with IPv4 836 * addresses, we must call PURGEIF regardless of 837 * addresses. (Protocols which might store ifnet 838 * pointers are marked with PR_PURGEIF.) 839 */ 840 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { 841 so.so_proto = pr; 842 if (pr->pr_usrreq != NULL && pr->pr_flags & PR_PURGEIF) 843 (void)(*pr->pr_usrreq)(&so, PRU_PURGEIF, NULL, 844 NULL, (struct mbuf *)ifp, curlwp); 845 } 846 } 847 848 #ifdef PFIL_HOOKS 849 (void)pfil_run_hooks(&if_pfil, 850 (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET); 851 (void)pfil_head_unregister(&ifp->if_pfil); 852 #endif 853 854 /* Announce that the interface is gone. */ 855 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 856 857 ifindex2ifnet[ifp->if_index] = NULL; 858 859 TAILQ_REMOVE(&ifnet, ifp, if_list); 860 861 ifioctl_detach(ifp); 862 863 /* 864 * remove packets that came from ifp, from software interrupt queues. 865 */ 866 DOMAIN_FOREACH(dp) { 867 for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) { 868 struct ifqueue *iq = dp->dom_ifqueues[i]; 869 if (iq == NULL) 870 break; 871 dp->dom_ifqueues[i] = NULL; 872 if_detach_queues(ifp, iq); 873 } 874 } 875 876 splx(s); 877 } 878 879 static void 880 if_detach_queues(struct ifnet *ifp, struct ifqueue *q) 881 { 882 struct mbuf *m, *prev, *next; 883 884 prev = NULL; 885 for (m = q->ifq_head; m != NULL; m = next) { 886 next = m->m_nextpkt; 887 #ifdef DIAGNOSTIC 888 if ((m->m_flags & M_PKTHDR) == 0) { 889 prev = m; 890 continue; 891 } 892 #endif 893 if (m->m_pkthdr.rcvif != ifp) { 894 prev = m; 895 continue; 896 } 897 898 if (prev != NULL) 899 prev->m_nextpkt = m->m_nextpkt; 900 else 901 q->ifq_head = m->m_nextpkt; 902 if (q->ifq_tail == m) 903 q->ifq_tail = prev; 904 q->ifq_len--; 905 906 m->m_nextpkt = NULL; 907 m_freem(m); 908 IF_DROP(q); 909 } 910 } 911 912 /* 913 * Callback for a radix tree walk to delete all references to an 914 * ifnet. 915 */ 916 static int 917 if_rt_walktree(struct rtentry *rt, void *v) 918 { 919 struct ifnet *ifp = (struct ifnet *)v; 920 int error; 921 922 if (rt->rt_ifp != ifp) 923 return 0; 924 925 /* Delete the entry. */ 926 ++rt->rt_refcnt; 927 error = rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway, 928 rt_mask(rt), rt->rt_flags, NULL); 929 KASSERT((rt->rt_flags & RTF_UP) == 0); 930 rt->rt_ifp = NULL; 931 RTFREE(rt); 932 if (error != 0) 933 printf("%s: warning: unable to delete rtentry @ %p, " 934 "error = %d\n", ifp->if_xname, rt, error); 935 return ERESTART; 936 } 937 938 /* 939 * Create a clone network interface. 940 */ 941 int 942 if_clone_create(const char *name) 943 { 944 struct if_clone *ifc; 945 int unit; 946 947 ifc = if_clone_lookup(name, &unit); 948 if (ifc == NULL) 949 return EINVAL; 950 951 if (ifunit(name) != NULL) 952 return EEXIST; 953 954 return (*ifc->ifc_create)(ifc, unit); 955 } 956 957 /* 958 * Destroy a clone network interface. 959 */ 960 int 961 if_clone_destroy(const char *name) 962 { 963 struct if_clone *ifc; 964 struct ifnet *ifp; 965 966 ifc = if_clone_lookup(name, NULL); 967 if (ifc == NULL) 968 return EINVAL; 969 970 ifp = ifunit(name); 971 if (ifp == NULL) 972 return ENXIO; 973 974 if (ifc->ifc_destroy == NULL) 975 return EOPNOTSUPP; 976 977 return (*ifc->ifc_destroy)(ifp); 978 } 979 980 /* 981 * Look up a network interface cloner. 982 */ 983 static struct if_clone * 984 if_clone_lookup(const char *name, int *unitp) 985 { 986 struct if_clone *ifc; 987 const char *cp; 988 char *dp, ifname[IFNAMSIZ + 3]; 989 int unit; 990 991 strcpy(ifname, "if_"); 992 /* separate interface name from unit */ 993 for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ && 994 *cp && (*cp < '0' || *cp > '9');) 995 *dp++ = *cp++; 996 997 if (cp == name || cp - name == IFNAMSIZ || !*cp) 998 return NULL; /* No name or unit number */ 999 *dp++ = '\0'; 1000 1001 again: 1002 LIST_FOREACH(ifc, &if_cloners, ifc_list) { 1003 if (strcmp(ifname + 3, ifc->ifc_name) == 0) 1004 break; 1005 } 1006 1007 if (ifc == NULL) { 1008 if (*ifname == '\0' || 1009 module_autoload(ifname, MODULE_CLASS_DRIVER)) 1010 return NULL; 1011 *ifname = '\0'; 1012 goto again; 1013 } 1014 1015 unit = 0; 1016 while (cp - name < IFNAMSIZ && *cp) { 1017 if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) { 1018 /* Bogus unit number. */ 1019 return NULL; 1020 } 1021 unit = (unit * 10) + (*cp++ - '0'); 1022 } 1023 1024 if (unitp != NULL) 1025 *unitp = unit; 1026 return ifc; 1027 } 1028 1029 /* 1030 * Register a network interface cloner. 1031 */ 1032 void 1033 if_clone_attach(struct if_clone *ifc) 1034 { 1035 1036 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list); 1037 if_cloners_count++; 1038 } 1039 1040 /* 1041 * Unregister a network interface cloner. 1042 */ 1043 void 1044 if_clone_detach(struct if_clone *ifc) 1045 { 1046 1047 LIST_REMOVE(ifc, ifc_list); 1048 if_cloners_count--; 1049 } 1050 1051 /* 1052 * Provide list of interface cloners to userspace. 1053 */ 1054 static int 1055 if_clone_list(struct if_clonereq *ifcr) 1056 { 1057 char outbuf[IFNAMSIZ], *dst; 1058 struct if_clone *ifc; 1059 int count, error = 0; 1060 1061 ifcr->ifcr_total = if_cloners_count; 1062 if ((dst = ifcr->ifcr_buffer) == NULL) { 1063 /* Just asking how many there are. */ 1064 return 0; 1065 } 1066 1067 if (ifcr->ifcr_count < 0) 1068 return EINVAL; 1069 1070 count = (if_cloners_count < ifcr->ifcr_count) ? 1071 if_cloners_count : ifcr->ifcr_count; 1072 1073 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0; 1074 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) { 1075 (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf)); 1076 if (outbuf[sizeof(outbuf) - 1] != '\0') 1077 return ENAMETOOLONG; 1078 error = copyout(outbuf, dst, sizeof(outbuf)); 1079 if (error != 0) 1080 break; 1081 } 1082 1083 return error; 1084 } 1085 1086 void 1087 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa) 1088 { 1089 ifa->ifa_ifp = ifp; 1090 TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list); 1091 IFAREF(ifa); 1092 } 1093 1094 void 1095 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa) 1096 { 1097 KASSERT(ifa->ifa_ifp == ifp); 1098 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list); 1099 IFAFREE(ifa); 1100 } 1101 1102 static inline int 1103 equal(const struct sockaddr *sa1, const struct sockaddr *sa2) 1104 { 1105 return sockaddr_cmp(sa1, sa2) == 0; 1106 } 1107 1108 /* 1109 * Locate an interface based on a complete address. 1110 */ 1111 /*ARGSUSED*/ 1112 struct ifaddr * 1113 ifa_ifwithaddr(const struct sockaddr *addr) 1114 { 1115 struct ifnet *ifp; 1116 struct ifaddr *ifa; 1117 1118 IFNET_FOREACH(ifp) { 1119 if (ifp->if_output == if_nulloutput) 1120 continue; 1121 IFADDR_FOREACH(ifa, ifp) { 1122 if (ifa->ifa_addr->sa_family != addr->sa_family) 1123 continue; 1124 if (equal(addr, ifa->ifa_addr)) 1125 return ifa; 1126 if ((ifp->if_flags & IFF_BROADCAST) && 1127 ifa->ifa_broadaddr && 1128 /* IP6 doesn't have broadcast */ 1129 ifa->ifa_broadaddr->sa_len != 0 && 1130 equal(ifa->ifa_broadaddr, addr)) 1131 return ifa; 1132 } 1133 } 1134 return NULL; 1135 } 1136 1137 /* 1138 * Locate the point to point interface with a given destination address. 1139 */ 1140 /*ARGSUSED*/ 1141 struct ifaddr * 1142 ifa_ifwithdstaddr(const struct sockaddr *addr) 1143 { 1144 struct ifnet *ifp; 1145 struct ifaddr *ifa; 1146 1147 IFNET_FOREACH(ifp) { 1148 if (ifp->if_output == if_nulloutput) 1149 continue; 1150 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1151 continue; 1152 IFADDR_FOREACH(ifa, ifp) { 1153 if (ifa->ifa_addr->sa_family != addr->sa_family || 1154 ifa->ifa_dstaddr == NULL) 1155 continue; 1156 if (equal(addr, ifa->ifa_dstaddr)) 1157 return ifa; 1158 } 1159 } 1160 return NULL; 1161 } 1162 1163 /* 1164 * Find an interface on a specific network. If many, choice 1165 * is most specific found. 1166 */ 1167 struct ifaddr * 1168 ifa_ifwithnet(const struct sockaddr *addr) 1169 { 1170 struct ifnet *ifp; 1171 struct ifaddr *ifa; 1172 const struct sockaddr_dl *sdl; 1173 struct ifaddr *ifa_maybe = 0; 1174 u_int af = addr->sa_family; 1175 const char *addr_data = addr->sa_data, *cplim; 1176 1177 if (af == AF_LINK) { 1178 sdl = satocsdl(addr); 1179 if (sdl->sdl_index && sdl->sdl_index < if_indexlim && 1180 ifindex2ifnet[sdl->sdl_index] && 1181 ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput) 1182 return ifnet_addrs[sdl->sdl_index]; 1183 } 1184 #ifdef NETATALK 1185 if (af == AF_APPLETALK) { 1186 const struct sockaddr_at *sat, *sat2; 1187 sat = (const struct sockaddr_at *)addr; 1188 IFNET_FOREACH(ifp) { 1189 if (ifp->if_output == if_nulloutput) 1190 continue; 1191 ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp); 1192 if (ifa == NULL) 1193 continue; 1194 sat2 = (struct sockaddr_at *)ifa->ifa_addr; 1195 if (sat2->sat_addr.s_net == sat->sat_addr.s_net) 1196 return ifa; /* exact match */ 1197 if (ifa_maybe == NULL) { 1198 /* else keep the if with the right range */ 1199 ifa_maybe = ifa; 1200 } 1201 } 1202 return ifa_maybe; 1203 } 1204 #endif 1205 IFNET_FOREACH(ifp) { 1206 if (ifp->if_output == if_nulloutput) 1207 continue; 1208 IFADDR_FOREACH(ifa, ifp) { 1209 const char *cp, *cp2, *cp3; 1210 1211 if (ifa->ifa_addr->sa_family != af || 1212 ifa->ifa_netmask == NULL) 1213 next: continue; 1214 cp = addr_data; 1215 cp2 = ifa->ifa_addr->sa_data; 1216 cp3 = ifa->ifa_netmask->sa_data; 1217 cplim = (const char *)ifa->ifa_netmask + 1218 ifa->ifa_netmask->sa_len; 1219 while (cp3 < cplim) { 1220 if ((*cp++ ^ *cp2++) & *cp3++) { 1221 /* want to continue for() loop */ 1222 goto next; 1223 } 1224 } 1225 if (ifa_maybe == NULL || 1226 rn_refines((void *)ifa->ifa_netmask, 1227 (void *)ifa_maybe->ifa_netmask)) 1228 ifa_maybe = ifa; 1229 } 1230 } 1231 return ifa_maybe; 1232 } 1233 1234 /* 1235 * Find the interface of the addresss. 1236 */ 1237 struct ifaddr * 1238 ifa_ifwithladdr(const struct sockaddr *addr) 1239 { 1240 struct ifaddr *ia; 1241 1242 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) || 1243 (ia = ifa_ifwithnet(addr))) 1244 return ia; 1245 return NULL; 1246 } 1247 1248 /* 1249 * Find an interface using a specific address family 1250 */ 1251 struct ifaddr * 1252 ifa_ifwithaf(int af) 1253 { 1254 struct ifnet *ifp; 1255 struct ifaddr *ifa; 1256 1257 IFNET_FOREACH(ifp) { 1258 if (ifp->if_output == if_nulloutput) 1259 continue; 1260 IFADDR_FOREACH(ifa, ifp) { 1261 if (ifa->ifa_addr->sa_family == af) 1262 return ifa; 1263 } 1264 } 1265 return NULL; 1266 } 1267 1268 /* 1269 * Find an interface address specific to an interface best matching 1270 * a given address. 1271 */ 1272 struct ifaddr * 1273 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp) 1274 { 1275 struct ifaddr *ifa; 1276 const char *cp, *cp2, *cp3; 1277 const char *cplim; 1278 struct ifaddr *ifa_maybe = 0; 1279 u_int af = addr->sa_family; 1280 1281 if (ifp->if_output == if_nulloutput) 1282 return NULL; 1283 1284 if (af >= AF_MAX) 1285 return NULL; 1286 1287 IFADDR_FOREACH(ifa, ifp) { 1288 if (ifa->ifa_addr->sa_family != af) 1289 continue; 1290 ifa_maybe = ifa; 1291 if (ifa->ifa_netmask == NULL) { 1292 if (equal(addr, ifa->ifa_addr) || 1293 (ifa->ifa_dstaddr && 1294 equal(addr, ifa->ifa_dstaddr))) 1295 return ifa; 1296 continue; 1297 } 1298 cp = addr->sa_data; 1299 cp2 = ifa->ifa_addr->sa_data; 1300 cp3 = ifa->ifa_netmask->sa_data; 1301 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1302 for (; cp3 < cplim; cp3++) { 1303 if ((*cp++ ^ *cp2++) & *cp3) 1304 break; 1305 } 1306 if (cp3 == cplim) 1307 return ifa; 1308 } 1309 return ifa_maybe; 1310 } 1311 1312 /* 1313 * Default action when installing a route with a Link Level gateway. 1314 * Lookup an appropriate real ifa to point to. 1315 * This should be moved to /sys/net/link.c eventually. 1316 */ 1317 void 1318 link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info) 1319 { 1320 struct ifaddr *ifa; 1321 const struct sockaddr *dst; 1322 struct ifnet *ifp; 1323 1324 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL || 1325 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_getkey(rt)) == NULL) 1326 return; 1327 if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) { 1328 rt_replace_ifa(rt, ifa); 1329 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 1330 ifa->ifa_rtrequest(cmd, rt, info); 1331 } 1332 } 1333 1334 /* 1335 * Handle a change in the interface link state. 1336 */ 1337 void 1338 if_link_state_change(struct ifnet *ifp, int link_state) 1339 { 1340 if (ifp->if_link_state == link_state) 1341 return; 1342 ifp->if_link_state = link_state; 1343 /* Notify that the link state has changed. */ 1344 rt_ifmsg(ifp); 1345 #if NCARP > 0 1346 if (ifp->if_carp) 1347 carp_carpdev_state(ifp); 1348 #endif 1349 } 1350 1351 /* 1352 * Mark an interface down and notify protocols of 1353 * the transition. 1354 * NOTE: must be called at splsoftnet or equivalent. 1355 */ 1356 void 1357 if_down(struct ifnet *ifp) 1358 { 1359 struct ifaddr *ifa; 1360 1361 ifp->if_flags &= ~IFF_UP; 1362 nanotime(&ifp->if_lastchange); 1363 IFADDR_FOREACH(ifa, ifp) 1364 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1365 IFQ_PURGE(&ifp->if_snd); 1366 #if NCARP > 0 1367 if (ifp->if_carp) 1368 carp_carpdev_state(ifp); 1369 #endif 1370 rt_ifmsg(ifp); 1371 } 1372 1373 /* 1374 * Mark an interface up and notify protocols of 1375 * the transition. 1376 * NOTE: must be called at splsoftnet or equivalent. 1377 */ 1378 void 1379 if_up(struct ifnet *ifp) 1380 { 1381 #ifdef notyet 1382 struct ifaddr *ifa; 1383 #endif 1384 1385 ifp->if_flags |= IFF_UP; 1386 nanotime(&ifp->if_lastchange); 1387 #ifdef notyet 1388 /* this has no effect on IP, and will kill all ISO connections XXX */ 1389 IFADDR_FOREACH(ifa, ifp) 1390 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1391 #endif 1392 #if NCARP > 0 1393 if (ifp->if_carp) 1394 carp_carpdev_state(ifp); 1395 #endif 1396 rt_ifmsg(ifp); 1397 #ifdef INET6 1398 in6_if_up(ifp); 1399 #endif 1400 } 1401 1402 /* 1403 * Handle interface watchdog timer routines. Called 1404 * from softclock, we decrement timers (if set) and 1405 * call the appropriate interface routine on expiration. 1406 */ 1407 void 1408 if_slowtimo(void *arg) 1409 { 1410 struct ifnet *ifp; 1411 int s = splnet(); 1412 1413 IFNET_FOREACH(ifp) { 1414 if (ifp->if_timer == 0 || --ifp->if_timer) 1415 continue; 1416 if (ifp->if_watchdog != NULL) 1417 (*ifp->if_watchdog)(ifp); 1418 } 1419 splx(s); 1420 callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ, if_slowtimo, NULL); 1421 } 1422 1423 /* 1424 * Set/clear promiscuous mode on interface ifp based on the truth value 1425 * of pswitch. The calls are reference counted so that only the first 1426 * "on" request actually has an effect, as does the final "off" request. 1427 * Results are undefined if the "off" and "on" requests are not matched. 1428 */ 1429 int 1430 ifpromisc(struct ifnet *ifp, int pswitch) 1431 { 1432 int pcount, ret; 1433 short nflags; 1434 1435 pcount = ifp->if_pcount; 1436 if (pswitch) { 1437 /* 1438 * Allow the device to be "placed" into promiscuous 1439 * mode even if it is not configured up. It will 1440 * consult IFF_PROMISC when it is brought up. 1441 */ 1442 if (ifp->if_pcount++ != 0) 1443 return 0; 1444 nflags = ifp->if_flags | IFF_PROMISC; 1445 } else { 1446 if (--ifp->if_pcount > 0) 1447 return 0; 1448 nflags = ifp->if_flags & ~IFF_PROMISC; 1449 } 1450 ret = if_flags_set(ifp, nflags); 1451 /* Restore interface state if not successful. */ 1452 if (ret != 0) { 1453 ifp->if_pcount = pcount; 1454 } 1455 return ret; 1456 } 1457 1458 /* 1459 * Map interface name to 1460 * interface structure pointer. 1461 */ 1462 struct ifnet * 1463 ifunit(const char *name) 1464 { 1465 struct ifnet *ifp; 1466 const char *cp = name; 1467 u_int unit = 0; 1468 u_int i; 1469 1470 /* 1471 * If the entire name is a number, treat it as an ifindex. 1472 */ 1473 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) { 1474 unit = unit * 10 + (*cp - '0'); 1475 } 1476 1477 /* 1478 * If the number took all of the name, then it's a valid ifindex. 1479 */ 1480 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) { 1481 if (unit >= if_indexlim) 1482 return NULL; 1483 ifp = ifindex2ifnet[unit]; 1484 if (ifp == NULL || ifp->if_output == if_nulloutput) 1485 return NULL; 1486 return ifp; 1487 } 1488 1489 IFNET_FOREACH(ifp) { 1490 if (ifp->if_output == if_nulloutput) 1491 continue; 1492 if (strcmp(ifp->if_xname, name) == 0) 1493 return ifp; 1494 } 1495 return NULL; 1496 } 1497 1498 ifnet_t * 1499 if_byindex(u_int idx) 1500 { 1501 1502 return (idx < if_indexlim) ? ifindex2ifnet[idx] : NULL; 1503 } 1504 1505 /* common */ 1506 int 1507 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data) 1508 { 1509 int s; 1510 struct ifreq *ifr; 1511 struct ifcapreq *ifcr; 1512 struct ifdatareq *ifdr; 1513 1514 switch (cmd) { 1515 case SIOCSIFCAP: 1516 ifcr = data; 1517 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0) 1518 return EINVAL; 1519 1520 if (ifcr->ifcr_capenable == ifp->if_capenable) 1521 return 0; 1522 1523 ifp->if_capenable = ifcr->ifcr_capenable; 1524 1525 /* Pre-compute the checksum flags mask. */ 1526 ifp->if_csum_flags_tx = 0; 1527 ifp->if_csum_flags_rx = 0; 1528 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) { 1529 ifp->if_csum_flags_tx |= M_CSUM_IPv4; 1530 } 1531 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) { 1532 ifp->if_csum_flags_rx |= M_CSUM_IPv4; 1533 } 1534 1535 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) { 1536 ifp->if_csum_flags_tx |= M_CSUM_TCPv4; 1537 } 1538 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) { 1539 ifp->if_csum_flags_rx |= M_CSUM_TCPv4; 1540 } 1541 1542 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) { 1543 ifp->if_csum_flags_tx |= M_CSUM_UDPv4; 1544 } 1545 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) { 1546 ifp->if_csum_flags_rx |= M_CSUM_UDPv4; 1547 } 1548 1549 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) { 1550 ifp->if_csum_flags_tx |= M_CSUM_TCPv6; 1551 } 1552 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) { 1553 ifp->if_csum_flags_rx |= M_CSUM_TCPv6; 1554 } 1555 1556 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) { 1557 ifp->if_csum_flags_tx |= M_CSUM_UDPv6; 1558 } 1559 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) { 1560 ifp->if_csum_flags_rx |= M_CSUM_UDPv6; 1561 } 1562 if (ifp->if_flags & IFF_UP) 1563 return ENETRESET; 1564 return 0; 1565 case SIOCSIFFLAGS: 1566 ifr = data; 1567 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) { 1568 s = splnet(); 1569 if_down(ifp); 1570 splx(s); 1571 } 1572 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) { 1573 s = splnet(); 1574 if_up(ifp); 1575 splx(s); 1576 } 1577 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1578 (ifr->ifr_flags &~ IFF_CANTCHANGE); 1579 break; 1580 case SIOCGIFFLAGS: 1581 ifr = data; 1582 ifr->ifr_flags = ifp->if_flags; 1583 break; 1584 1585 case SIOCGIFMETRIC: 1586 ifr = data; 1587 ifr->ifr_metric = ifp->if_metric; 1588 break; 1589 1590 case SIOCGIFMTU: 1591 ifr = data; 1592 ifr->ifr_mtu = ifp->if_mtu; 1593 break; 1594 1595 case SIOCGIFDLT: 1596 ifr = data; 1597 ifr->ifr_dlt = ifp->if_dlt; 1598 break; 1599 1600 case SIOCGIFCAP: 1601 ifcr = data; 1602 ifcr->ifcr_capabilities = ifp->if_capabilities; 1603 ifcr->ifcr_capenable = ifp->if_capenable; 1604 break; 1605 1606 case SIOCSIFMETRIC: 1607 ifr = data; 1608 ifp->if_metric = ifr->ifr_metric; 1609 break; 1610 1611 case SIOCGIFDATA: 1612 ifdr = data; 1613 ifdr->ifdr_data = ifp->if_data; 1614 break; 1615 1616 case SIOCZIFDATA: 1617 ifdr = data; 1618 ifdr->ifdr_data = ifp->if_data; 1619 /* 1620 * Assumes that the volatile counters that can be 1621 * zero'ed are at the end of if_data. 1622 */ 1623 memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) - 1624 offsetof(struct if_data, ifi_ipackets)); 1625 /* 1626 * The memset() clears to the bottm of if_data. In the area, 1627 * if_lastchange is included. Please be careful if new entry 1628 * will be added into if_data or rewite this. 1629 * 1630 * And also, update if_lastchnage. 1631 */ 1632 getnanotime(&ifp->if_lastchange); 1633 break; 1634 case SIOCSIFMTU: 1635 ifr = data; 1636 if (ifp->if_mtu == ifr->ifr_mtu) 1637 break; 1638 ifp->if_mtu = ifr->ifr_mtu; 1639 /* 1640 * If the link MTU changed, do network layer specific procedure. 1641 */ 1642 #ifdef INET6 1643 nd6_setmtu(ifp); 1644 #endif 1645 return ENETRESET; 1646 default: 1647 return ENOTTY; 1648 } 1649 return 0; 1650 } 1651 1652 int 1653 ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp, 1654 lwp_t *l) 1655 { 1656 struct if_addrprefreq *ifap = (struct if_addrprefreq *)data; 1657 struct ifaddr *ifa; 1658 const struct sockaddr *any, *sa; 1659 union { 1660 struct sockaddr sa; 1661 struct sockaddr_storage ss; 1662 } u, v; 1663 1664 switch (cmd) { 1665 case SIOCSIFADDRPREF: 1666 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE, 1667 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 1668 NULL) != 0) 1669 return EPERM; 1670 case SIOCGIFADDRPREF: 1671 break; 1672 default: 1673 return EOPNOTSUPP; 1674 } 1675 1676 /* sanity checks */ 1677 if (data == NULL || ifp == NULL) { 1678 panic("invalid argument to %s", __func__); 1679 /*NOTREACHED*/ 1680 } 1681 1682 /* address must be specified on ADD and DELETE */ 1683 sa = sstocsa(&ifap->ifap_addr); 1684 if (sa->sa_family != sofamily(so)) 1685 return EINVAL; 1686 if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len) 1687 return EINVAL; 1688 1689 sockaddr_externalize(&v.sa, sizeof(v.ss), sa); 1690 1691 IFADDR_FOREACH(ifa, ifp) { 1692 if (ifa->ifa_addr->sa_family != sa->sa_family) 1693 continue; 1694 sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr); 1695 if (sockaddr_cmp(&u.sa, &v.sa) == 0) 1696 break; 1697 } 1698 if (ifa == NULL) 1699 return EADDRNOTAVAIL; 1700 1701 switch (cmd) { 1702 case SIOCSIFADDRPREF: 1703 ifa->ifa_preference = ifap->ifap_preference; 1704 return 0; 1705 case SIOCGIFADDRPREF: 1706 /* fill in the if_laddrreq structure */ 1707 (void)sockaddr_copy(sstosa(&ifap->ifap_addr), 1708 sizeof(ifap->ifap_addr), ifa->ifa_addr); 1709 ifap->ifap_preference = ifa->ifa_preference; 1710 return 0; 1711 default: 1712 return EOPNOTSUPP; 1713 } 1714 } 1715 1716 static void 1717 ifnet_lock_enter(struct ifnet_lock *il) 1718 { 1719 uint64_t *nenter; 1720 1721 /* Before trying to acquire the mutex, increase the count of threads 1722 * who have entered or who wait to enter the critical section. 1723 * Avoid one costly locked memory transaction by keeping a count for 1724 * each CPU. 1725 */ 1726 nenter = percpu_getref(il->il_nenter); 1727 (*nenter)++; 1728 percpu_putref(il->il_nenter); 1729 mutex_enter(&il->il_lock); 1730 } 1731 1732 static void 1733 ifnet_lock_exit(struct ifnet_lock *il) 1734 { 1735 /* Increase the count of threads who have exited the critical 1736 * section. Increase while we still hold the lock. 1737 */ 1738 il->il_nexit++; 1739 mutex_exit(&il->il_lock); 1740 } 1741 1742 /* 1743 * Interface ioctls. 1744 */ 1745 int 1746 ifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l) 1747 { 1748 struct ifnet *ifp; 1749 struct ifreq *ifr; 1750 int error = 0; 1751 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ) 1752 u_long ocmd = cmd; 1753 #endif 1754 short oif_flags; 1755 #ifdef COMPAT_OIFREQ 1756 struct ifreq ifrb; 1757 struct oifreq *oifr = NULL; 1758 #endif 1759 1760 switch (cmd) { 1761 #ifdef COMPAT_OIFREQ 1762 case OSIOCGIFCONF: 1763 case OOSIOCGIFCONF: 1764 return compat_ifconf(cmd, data); 1765 #endif 1766 #ifdef COMPAT_OIFDATA 1767 case OSIOCGIFDATA: 1768 case OSIOCZIFDATA: 1769 return compat_ifdatareq(l, cmd, data); 1770 #endif 1771 case SIOCGIFCONF: 1772 return ifconf(cmd, data); 1773 case SIOCINITIFADDR: 1774 return EPERM; 1775 } 1776 1777 #ifdef COMPAT_OIFREQ 1778 cmd = compat_cvtcmd(cmd); 1779 if (cmd != ocmd) { 1780 oifr = data; 1781 data = ifr = &ifrb; 1782 ifreqo2n(oifr, ifr); 1783 } else 1784 #endif 1785 ifr = data; 1786 1787 ifp = ifunit(ifr->ifr_name); 1788 1789 switch (cmd) { 1790 case SIOCIFCREATE: 1791 case SIOCIFDESTROY: 1792 if (l != NULL) { 1793 error = kauth_authorize_network(l->l_cred, 1794 KAUTH_NETWORK_INTERFACE, 1795 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, 1796 (void *)cmd, NULL); 1797 if (error != 0) 1798 return error; 1799 } 1800 return (cmd == SIOCIFCREATE) ? 1801 if_clone_create(ifr->ifr_name) : 1802 if_clone_destroy(ifr->ifr_name); 1803 1804 case SIOCIFGCLONERS: 1805 return if_clone_list((struct if_clonereq *)data); 1806 } 1807 1808 if (ifp == NULL) 1809 return ENXIO; 1810 1811 switch (cmd) { 1812 case SIOCALIFADDR: 1813 case SIOCDLIFADDR: 1814 case SIOCSIFADDRPREF: 1815 case SIOCSIFFLAGS: 1816 case SIOCSIFCAP: 1817 case SIOCSIFMETRIC: 1818 case SIOCZIFDATA: 1819 case SIOCSIFMTU: 1820 case SIOCSIFPHYADDR: 1821 case SIOCDIFPHYADDR: 1822 #ifdef INET6 1823 case SIOCSIFPHYADDR_IN6: 1824 #endif 1825 case SIOCSLIFPHYADDR: 1826 case SIOCADDMULTI: 1827 case SIOCDELMULTI: 1828 case SIOCSIFMEDIA: 1829 case SIOCSDRVSPEC: 1830 case SIOCG80211: 1831 case SIOCS80211: 1832 case SIOCS80211NWID: 1833 case SIOCS80211NWKEY: 1834 case SIOCS80211POWER: 1835 case SIOCS80211BSSID: 1836 case SIOCS80211CHANNEL: 1837 case SIOCSLINKSTR: 1838 if (l != NULL) { 1839 error = kauth_authorize_network(l->l_cred, 1840 KAUTH_NETWORK_INTERFACE, 1841 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, 1842 (void *)cmd, NULL); 1843 if (error != 0) 1844 return error; 1845 } 1846 } 1847 1848 oif_flags = ifp->if_flags; 1849 1850 ifnet_lock_enter(ifp->if_ioctl_lock); 1851 error = (*ifp->if_ioctl)(ifp, cmd, data); 1852 if (error != ENOTTY) 1853 ; 1854 else if (so->so_proto == NULL) 1855 error = EOPNOTSUPP; 1856 else { 1857 #ifdef COMPAT_OSOCK 1858 error = compat_ifioctl(so, ocmd, cmd, data, l); 1859 #else 1860 error = (*so->so_proto->pr_usrreq)(so, PRU_CONTROL, 1861 (struct mbuf *)cmd, (struct mbuf *)data, 1862 (struct mbuf *)ifp, l); 1863 #endif 1864 } 1865 1866 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) { 1867 #ifdef INET6 1868 if ((ifp->if_flags & IFF_UP) != 0) { 1869 int s = splnet(); 1870 in6_if_up(ifp); 1871 splx(s); 1872 } 1873 #endif 1874 } 1875 #ifdef COMPAT_OIFREQ 1876 if (cmd != ocmd) 1877 ifreqn2o(oifr, ifr); 1878 #endif 1879 1880 ifnet_lock_exit(ifp->if_ioctl_lock); 1881 return error; 1882 } 1883 1884 /* This callback adds to the sum in `arg' the number of 1885 * threads on `ci' who have entered or who wait to enter the 1886 * critical section. 1887 */ 1888 static void 1889 ifnet_lock_sum(void *p, void *arg, struct cpu_info *ci) 1890 { 1891 uint64_t *sum = arg, *nenter = p; 1892 1893 *sum += *nenter; 1894 } 1895 1896 /* Return the number of threads who have entered or who wait 1897 * to enter the critical section on all CPUs. 1898 */ 1899 static uint64_t 1900 ifnet_lock_entrances(struct ifnet_lock *il) 1901 { 1902 uint64_t sum = 0; 1903 1904 percpu_foreach(il->il_nenter, ifnet_lock_sum, &sum); 1905 1906 return sum; 1907 } 1908 1909 static int 1910 ifioctl_attach(struct ifnet *ifp) 1911 { 1912 struct ifnet_lock *il; 1913 1914 /* If the driver has not supplied its own if_ioctl, then 1915 * supply the default. 1916 */ 1917 if (ifp->if_ioctl == NULL) 1918 ifp->if_ioctl = ifioctl_common; 1919 1920 /* Create an ifnet_lock for synchronizing ifioctls. */ 1921 if ((il = kmem_zalloc(sizeof(*il), KM_SLEEP)) == NULL) 1922 return ENOMEM; 1923 1924 il->il_nenter = percpu_alloc(sizeof(uint64_t)); 1925 if (il->il_nenter == NULL) { 1926 kmem_free(il, sizeof(*il)); 1927 return ENOMEM; 1928 } 1929 1930 mutex_init(&il->il_lock, MUTEX_DEFAULT, IPL_NONE); 1931 cv_init(&il->il_emptied, ifp->if_xname); 1932 1933 ifp->if_ioctl_lock = il; 1934 1935 return 0; 1936 } 1937 1938 /* 1939 * This must not be called until after `ifp' has been withdrawn from the 1940 * ifnet tables so that ifioctl() cannot get a handle on it by calling 1941 * ifunit(). 1942 */ 1943 static void 1944 ifioctl_detach(struct ifnet *ifp) 1945 { 1946 struct ifnet_lock *il; 1947 1948 il = ifp->if_ioctl_lock; 1949 mutex_enter(&il->il_lock); 1950 /* Install if_nullioctl to make sure that any thread that 1951 * subsequently enters the critical section will quit it 1952 * immediately and signal the condition variable that we 1953 * wait on, below. 1954 */ 1955 ifp->if_ioctl = if_nullioctl; 1956 /* Sleep while threads are still in the critical section or 1957 * wait to enter it. 1958 */ 1959 while (ifnet_lock_entrances(il) != il->il_nexit) 1960 cv_wait(&il->il_emptied, &il->il_lock); 1961 /* At this point, we are the only thread still in the critical 1962 * section, and no new thread can get a handle on the ifioctl 1963 * lock, so it is safe to free its memory. 1964 */ 1965 mutex_exit(&il->il_lock); 1966 ifp->if_ioctl_lock = NULL; 1967 percpu_free(il->il_nenter, sizeof(uint64_t)); 1968 il->il_nenter = NULL; 1969 cv_destroy(&il->il_emptied); 1970 mutex_destroy(&il->il_lock); 1971 kmem_free(il, sizeof(*il)); 1972 } 1973 1974 /* 1975 * Return interface configuration 1976 * of system. List may be used 1977 * in later ioctl's (above) to get 1978 * other information. 1979 * 1980 * Each record is a struct ifreq. Before the addition of 1981 * sockaddr_storage, the API rule was that sockaddr flavors that did 1982 * not fit would extend beyond the struct ifreq, with the next struct 1983 * ifreq starting sa_len beyond the struct sockaddr. Because the 1984 * union in struct ifreq includes struct sockaddr_storage, every kind 1985 * of sockaddr must fit. Thus, there are no longer any overlength 1986 * records. 1987 * 1988 * Records are added to the user buffer if they fit, and ifc_len is 1989 * adjusted to the length that was written. Thus, the user is only 1990 * assured of getting the complete list if ifc_len on return is at 1991 * least sizeof(struct ifreq) less than it was on entry. 1992 * 1993 * If the user buffer pointer is NULL, this routine copies no data and 1994 * returns the amount of space that would be needed. 1995 * 1996 * Invariants: 1997 * ifrp points to the next part of the user's buffer to be used. If 1998 * ifrp != NULL, space holds the number of bytes remaining that we may 1999 * write at ifrp. Otherwise, space holds the number of bytes that 2000 * would have been written had there been adequate space. 2001 */ 2002 /*ARGSUSED*/ 2003 int 2004 ifconf(u_long cmd, void *data) 2005 { 2006 struct ifconf *ifc = (struct ifconf *)data; 2007 struct ifnet *ifp; 2008 struct ifaddr *ifa; 2009 struct ifreq ifr, *ifrp; 2010 int space, error = 0; 2011 const int sz = (int)sizeof(struct ifreq); 2012 2013 if ((ifrp = ifc->ifc_req) == NULL) 2014 space = 0; 2015 else 2016 space = ifc->ifc_len; 2017 IFNET_FOREACH(ifp) { 2018 (void)strncpy(ifr.ifr_name, ifp->if_xname, 2019 sizeof(ifr.ifr_name)); 2020 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') 2021 return ENAMETOOLONG; 2022 if (IFADDR_EMPTY(ifp)) { 2023 /* Interface with no addresses - send zero sockaddr. */ 2024 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 2025 if (ifrp == NULL) { 2026 space += sz; 2027 continue; 2028 } 2029 if (space >= sz) { 2030 error = copyout(&ifr, ifrp, sz); 2031 if (error != 0) 2032 return error; 2033 ifrp++; 2034 space -= sz; 2035 } 2036 } 2037 2038 IFADDR_FOREACH(ifa, ifp) { 2039 struct sockaddr *sa = ifa->ifa_addr; 2040 /* all sockaddrs must fit in sockaddr_storage */ 2041 KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru)); 2042 2043 if (ifrp == NULL) { 2044 space += sz; 2045 continue; 2046 } 2047 memcpy(&ifr.ifr_space, sa, sa->sa_len); 2048 if (space >= sz) { 2049 error = copyout(&ifr, ifrp, sz); 2050 if (error != 0) 2051 return (error); 2052 ifrp++; space -= sz; 2053 } 2054 } 2055 } 2056 if (ifrp != NULL) { 2057 KASSERT(0 <= space && space <= ifc->ifc_len); 2058 ifc->ifc_len -= space; 2059 } else { 2060 KASSERT(space >= 0); 2061 ifc->ifc_len = space; 2062 } 2063 return (0); 2064 } 2065 2066 int 2067 ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa) 2068 { 2069 uint8_t len; 2070 #ifdef COMPAT_OIFREQ 2071 struct ifreq ifrb; 2072 struct oifreq *oifr = NULL; 2073 u_long ocmd = cmd; 2074 cmd = compat_cvtcmd(cmd); 2075 if (cmd != ocmd) { 2076 oifr = (struct oifreq *)(void *)ifr; 2077 ifr = &ifrb; 2078 ifreqo2n(oifr, ifr); 2079 len = sizeof(oifr->ifr_addr); 2080 } else 2081 #endif 2082 len = sizeof(ifr->ifr_ifru.ifru_space); 2083 2084 if (len < sa->sa_len) 2085 return EFBIG; 2086 2087 memset(&ifr->ifr_addr, 0, len); 2088 sockaddr_copy(&ifr->ifr_addr, len, sa); 2089 2090 #ifdef COMPAT_OIFREQ 2091 if (cmd != ocmd) 2092 ifreqn2o(oifr, ifr); 2093 #endif 2094 return 0; 2095 } 2096 2097 /* 2098 * Queue message on interface, and start output if interface 2099 * not yet active. 2100 */ 2101 int 2102 ifq_enqueue(struct ifnet *ifp, struct mbuf *m 2103 ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr)) 2104 { 2105 int len = m->m_pkthdr.len; 2106 int mflags = m->m_flags; 2107 int s = splnet(); 2108 int error; 2109 2110 IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error); 2111 if (error != 0) 2112 goto out; 2113 ifp->if_obytes += len; 2114 if (mflags & M_MCAST) 2115 ifp->if_omcasts++; 2116 if ((ifp->if_flags & IFF_OACTIVE) == 0) 2117 (*ifp->if_start)(ifp); 2118 out: 2119 splx(s); 2120 return error; 2121 } 2122 2123 /* 2124 * Queue message on interface, possibly using a second fast queue 2125 */ 2126 int 2127 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m 2128 ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr)) 2129 { 2130 int error = 0; 2131 2132 if (ifq != NULL 2133 #ifdef ALTQ 2134 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0 2135 #endif 2136 ) { 2137 if (IF_QFULL(ifq)) { 2138 IF_DROP(&ifp->if_snd); 2139 m_freem(m); 2140 if (error == 0) 2141 error = ENOBUFS; 2142 } else 2143 IF_ENQUEUE(ifq, m); 2144 } else 2145 IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error); 2146 if (error != 0) { 2147 ++ifp->if_oerrors; 2148 return error; 2149 } 2150 return 0; 2151 } 2152 2153 int 2154 if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src) 2155 { 2156 int rc; 2157 2158 if (ifp->if_initaddr != NULL) 2159 rc = (*ifp->if_initaddr)(ifp, ifa, src); 2160 else if (src || 2161 (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY) 2162 rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa); 2163 2164 return rc; 2165 } 2166 2167 int 2168 if_flags_set(ifnet_t *ifp, const short flags) 2169 { 2170 int rc; 2171 2172 if (ifp->if_setflags != NULL) 2173 rc = (*ifp->if_setflags)(ifp, flags); 2174 else { 2175 short cantflags, chgdflags; 2176 struct ifreq ifr; 2177 2178 chgdflags = ifp->if_flags ^ flags; 2179 cantflags = chgdflags & IFF_CANTCHANGE; 2180 2181 if (cantflags != 0) 2182 ifp->if_flags ^= cantflags; 2183 2184 /* Traditionally, we do not call if_ioctl after 2185 * setting/clearing only IFF_PROMISC if the interface 2186 * isn't IFF_UP. Uphold that tradition. 2187 */ 2188 if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0) 2189 return 0; 2190 2191 memset(&ifr, 0, sizeof(ifr)); 2192 2193 ifr.ifr_flags = flags & ~IFF_CANTCHANGE; 2194 rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr); 2195 2196 if (rc != 0 && cantflags != 0) 2197 ifp->if_flags ^= cantflags; 2198 } 2199 2200 return rc; 2201 } 2202 2203 int 2204 if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa) 2205 { 2206 int rc; 2207 struct ifreq ifr; 2208 2209 if (ifp->if_mcastop != NULL) 2210 rc = (*ifp->if_mcastop)(ifp, cmd, sa); 2211 else { 2212 ifreq_setaddr(cmd, &ifr, sa); 2213 rc = (*ifp->if_ioctl)(ifp, cmd, &ifr); 2214 } 2215 2216 return rc; 2217 } 2218 2219 static void 2220 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname, 2221 struct ifaltq *ifq) 2222 { 2223 const struct sysctlnode *cnode, *rnode; 2224 2225 if (sysctl_createv(clog, 0, NULL, &rnode, 2226 CTLFLAG_PERMANENT, 2227 CTLTYPE_NODE, "net", NULL, 2228 NULL, 0, NULL, 0, 2229 CTL_NET, CTL_EOL) != 0) 2230 goto bad; 2231 2232 if (sysctl_createv(clog, 0, &rnode, &rnode, 2233 CTLFLAG_PERMANENT, 2234 CTLTYPE_NODE, "interfaces", 2235 SYSCTL_DESCR("Per-interface controls"), 2236 NULL, 0, NULL, 0, 2237 CTL_CREATE, CTL_EOL) != 0) 2238 goto bad; 2239 2240 if (sysctl_createv(clog, 0, &rnode, &rnode, 2241 CTLFLAG_PERMANENT, 2242 CTLTYPE_NODE, ifname, 2243 SYSCTL_DESCR("Interface controls"), 2244 NULL, 0, NULL, 0, 2245 CTL_CREATE, CTL_EOL) != 0) 2246 goto bad; 2247 2248 if (sysctl_createv(clog, 0, &rnode, &rnode, 2249 CTLFLAG_PERMANENT, 2250 CTLTYPE_NODE, "sndq", 2251 SYSCTL_DESCR("Interface output queue controls"), 2252 NULL, 0, NULL, 0, 2253 CTL_CREATE, CTL_EOL) != 0) 2254 goto bad; 2255 2256 if (sysctl_createv(clog, 0, &rnode, &cnode, 2257 CTLFLAG_PERMANENT, 2258 CTLTYPE_INT, "len", 2259 SYSCTL_DESCR("Current output queue length"), 2260 NULL, 0, &ifq->ifq_len, 0, 2261 CTL_CREATE, CTL_EOL) != 0) 2262 goto bad; 2263 2264 if (sysctl_createv(clog, 0, &rnode, &cnode, 2265 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2266 CTLTYPE_INT, "maxlen", 2267 SYSCTL_DESCR("Maximum allowed output queue length"), 2268 NULL, 0, &ifq->ifq_maxlen, 0, 2269 CTL_CREATE, CTL_EOL) != 0) 2270 goto bad; 2271 2272 if (sysctl_createv(clog, 0, &rnode, &cnode, 2273 CTLFLAG_PERMANENT, 2274 CTLTYPE_INT, "drops", 2275 SYSCTL_DESCR("Packets dropped due to full output queue"), 2276 NULL, 0, &ifq->ifq_drops, 0, 2277 CTL_CREATE, CTL_EOL) != 0) 2278 goto bad; 2279 2280 return; 2281 bad: 2282 printf("%s: could not attach sysctl nodes\n", ifname); 2283 return; 2284 } 2285 2286 #if defined(INET) || defined(INET6) 2287 static void 2288 sysctl_net_ifq_setup(struct sysctllog **clog, 2289 int pf, const char *pfname, 2290 int ipn, const char *ipname, 2291 int qid, struct ifqueue *ifq) 2292 { 2293 2294 sysctl_createv(clog, 0, NULL, NULL, 2295 CTLFLAG_PERMANENT, 2296 CTLTYPE_NODE, "net", NULL, 2297 NULL, 0, NULL, 0, 2298 CTL_NET, CTL_EOL); 2299 sysctl_createv(clog, 0, NULL, NULL, 2300 CTLFLAG_PERMANENT, 2301 CTLTYPE_NODE, pfname, NULL, 2302 NULL, 0, NULL, 0, 2303 CTL_NET, pf, CTL_EOL); 2304 sysctl_createv(clog, 0, NULL, NULL, 2305 CTLFLAG_PERMANENT, 2306 CTLTYPE_NODE, ipname, NULL, 2307 NULL, 0, NULL, 0, 2308 CTL_NET, pf, ipn, CTL_EOL); 2309 sysctl_createv(clog, 0, NULL, NULL, 2310 CTLFLAG_PERMANENT, 2311 CTLTYPE_NODE, "ifq", 2312 SYSCTL_DESCR("Protocol input queue controls"), 2313 NULL, 0, NULL, 0, 2314 CTL_NET, pf, ipn, qid, CTL_EOL); 2315 2316 sysctl_createv(clog, 0, NULL, NULL, 2317 CTLFLAG_PERMANENT, 2318 CTLTYPE_INT, "len", 2319 SYSCTL_DESCR("Current input queue length"), 2320 NULL, 0, &ifq->ifq_len, 0, 2321 CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL); 2322 sysctl_createv(clog, 0, NULL, NULL, 2323 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2324 CTLTYPE_INT, "maxlen", 2325 SYSCTL_DESCR("Maximum allowed input queue length"), 2326 NULL, 0, &ifq->ifq_maxlen, 0, 2327 CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL); 2328 #ifdef notyet 2329 sysctl_createv(clog, 0, NULL, NULL, 2330 CTLFLAG_PERMANENT, 2331 CTLTYPE_INT, "peak", 2332 SYSCTL_DESCR("Highest input queue length"), 2333 NULL, 0, &ifq->ifq_peak, 0, 2334 CTL_NET, pf, ipn, qid, IFQCTL_PEAK, CTL_EOL); 2335 #endif 2336 sysctl_createv(clog, 0, NULL, NULL, 2337 CTLFLAG_PERMANENT, 2338 CTLTYPE_INT, "drops", 2339 SYSCTL_DESCR("Packets dropped due to full input queue"), 2340 NULL, 0, &ifq->ifq_drops, 0, 2341 CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL); 2342 } 2343 #endif /* INET || INET6 */ 2344