1 /*- 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)if.c 8.5 (Berkeley) 1/9/95 30 * $FreeBSD$ 31 */ 32 33 #include "opt_compat.h" 34 #include "opt_inet6.h" 35 #include "opt_inet.h" 36 #include "opt_mac.h" 37 #include "opt_carp.h" 38 39 #include <sys/param.h> 40 #include <sys/types.h> 41 #include <sys/conf.h> 42 #include <sys/malloc.h> 43 #include <sys/sbuf.h> 44 #include <sys/bus.h> 45 #include <sys/mbuf.h> 46 #include <sys/systm.h> 47 #include <sys/priv.h> 48 #include <sys/proc.h> 49 #include <sys/socket.h> 50 #include <sys/socketvar.h> 51 #include <sys/protosw.h> 52 #include <sys/kernel.h> 53 #include <sys/sockio.h> 54 #include <sys/syslog.h> 55 #include <sys/sysctl.h> 56 #include <sys/taskqueue.h> 57 #include <sys/domain.h> 58 #include <sys/jail.h> 59 #include <sys/vimage.h> 60 #include <machine/stdarg.h> 61 62 #include <net/if.h> 63 #include <net/if_arp.h> 64 #include <net/if_clone.h> 65 #include <net/if_dl.h> 66 #include <net/if_types.h> 67 #include <net/if_var.h> 68 #include <net/radix.h> 69 #include <net/route.h> 70 71 #if defined(INET) || defined(INET6) 72 /*XXX*/ 73 #include <netinet/in.h> 74 #include <netinet/in_var.h> 75 #ifdef INET6 76 #include <netinet6/in6_var.h> 77 #include <netinet6/in6_ifattach.h> 78 #endif 79 #endif 80 #ifdef INET 81 #include <netinet/if_ether.h> 82 #endif 83 #ifdef DEV_CARP 84 #include <netinet/ip_carp.h> 85 #endif 86 87 #include <security/mac/mac_framework.h> 88 89 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 90 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 91 92 /* Log link state change events */ 93 static int log_link_state_change = 1; 94 95 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW, 96 &log_link_state_change, 0, 97 "log interface link state change events"); 98 99 void (*bstp_linkstate_p)(struct ifnet *ifp, int state); 100 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 101 void (*lagg_linkstate_p)(struct ifnet *ifp, int state); 102 103 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; 104 105 /* 106 * XXX: Style; these should be sorted alphabetically, and unprototyped 107 * static functions should be prototyped. Currently they are sorted by 108 * declaration order. 109 */ 110 static void if_attachdomain(void *); 111 static void if_attachdomain1(struct ifnet *); 112 static int ifconf(u_long, caddr_t); 113 static void if_freemulti(struct ifmultiaddr *); 114 static void if_grow(void); 115 static void if_init(void *); 116 static void if_qflush(struct ifaltq *); 117 static void if_route(struct ifnet *, int flag, int fam); 118 static int if_setflag(struct ifnet *, int, int, int *, int); 119 static void if_slowtimo(void *); 120 static void if_unroute(struct ifnet *, int flag, int fam); 121 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 122 static int if_rtdel(struct radix_node *, void *); 123 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *); 124 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int); 125 static void if_start_deferred(void *context, int pending); 126 static void do_link_state_change(void *, int); 127 static int if_getgroup(struct ifgroupreq *, struct ifnet *); 128 static int if_getgroupmembers(struct ifgroupreq *); 129 #ifdef INET6 130 /* 131 * XXX: declare here to avoid to include many inet6 related files.. 132 * should be more generalized? 133 */ 134 extern void nd6_setmtu(struct ifnet *); 135 #endif 136 137 int if_index = 0; 138 int ifqmaxlen = IFQ_MAXLEN; 139 struct ifnethead ifnet; /* depend on static init XXX */ 140 struct ifgrouphead ifg_head; 141 struct mtx ifnet_lock; 142 static if_com_alloc_t *if_com_alloc[256]; 143 static if_com_free_t *if_com_free[256]; 144 145 static int if_indexlim = 8; 146 static struct knlist ifklist; 147 148 /* 149 * Table of ifnet/cdev by index. Locked with ifnet_lock. 150 */ 151 static struct ifindex_entry *ifindex_table = NULL; 152 153 static void filt_netdetach(struct knote *kn); 154 static int filt_netdev(struct knote *kn, long hint); 155 156 static struct filterops netdev_filtops = 157 { 1, NULL, filt_netdetach, filt_netdev }; 158 159 /* 160 * System initialization 161 */ 162 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL); 163 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_slowtimo, NULL); 164 165 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals"); 166 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 167 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 168 169 struct ifnet * 170 ifnet_byindex(u_short idx) 171 { 172 INIT_VNET_NET(curvnet); 173 struct ifnet *ifp; 174 175 IFNET_RLOCK(); 176 ifp = V_ifindex_table[idx].ife_ifnet; 177 IFNET_RUNLOCK(); 178 return (ifp); 179 } 180 181 static void 182 ifnet_setbyindex(u_short idx, struct ifnet *ifp) 183 { 184 INIT_VNET_NET(curvnet); 185 186 IFNET_WLOCK_ASSERT(); 187 188 V_ifindex_table[idx].ife_ifnet = ifp; 189 } 190 191 struct ifaddr * 192 ifaddr_byindex(u_short idx) 193 { 194 INIT_VNET_NET(curvnet); 195 struct ifaddr *ifa; 196 197 IFNET_RLOCK(); 198 ifa = ifnet_byindex(idx)->if_addr; 199 IFNET_RUNLOCK(); 200 return (ifa); 201 } 202 203 struct cdev * 204 ifdev_byindex(u_short idx) 205 { 206 INIT_VNET_NET(curvnet); 207 struct cdev *cdev; 208 209 IFNET_RLOCK(); 210 cdev = V_ifindex_table[idx].ife_dev; 211 IFNET_RUNLOCK(); 212 return (cdev); 213 } 214 215 static void 216 ifdev_setbyindex(u_short idx, struct cdev *cdev) 217 { 218 INIT_VNET_NET(curvnet); 219 220 IFNET_WLOCK(); 221 V_ifindex_table[idx].ife_dev = cdev; 222 IFNET_WUNLOCK(); 223 } 224 225 static d_open_t netopen; 226 static d_close_t netclose; 227 static d_ioctl_t netioctl; 228 static d_kqfilter_t netkqfilter; 229 230 static struct cdevsw net_cdevsw = { 231 .d_version = D_VERSION, 232 .d_flags = D_NEEDGIANT, 233 .d_open = netopen, 234 .d_close = netclose, 235 .d_ioctl = netioctl, 236 .d_name = "net", 237 .d_kqfilter = netkqfilter, 238 }; 239 240 static int 241 netopen(struct cdev *dev, int flag, int mode, struct thread *td) 242 { 243 return (0); 244 } 245 246 static int 247 netclose(struct cdev *dev, int flags, int fmt, struct thread *td) 248 { 249 return (0); 250 } 251 252 static int 253 netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 254 { 255 struct ifnet *ifp; 256 int error, idx; 257 258 /* only support interface specific ioctls */ 259 if (IOCGROUP(cmd) != 'i') 260 return (EOPNOTSUPP); 261 idx = dev2unit(dev); 262 if (idx == 0) { 263 /* 264 * special network device, not interface. 265 */ 266 if (cmd == SIOCGIFCONF) 267 return (ifconf(cmd, data)); /* XXX remove cmd */ 268 #ifdef __amd64__ 269 if (cmd == SIOCGIFCONF32) 270 return (ifconf(cmd, data)); /* XXX remove cmd */ 271 #endif 272 return (EOPNOTSUPP); 273 } 274 275 ifp = ifnet_byindex(idx); 276 if (ifp == NULL) 277 return (ENXIO); 278 279 error = ifhwioctl(cmd, ifp, data, td); 280 if (error == ENOIOCTL) 281 error = EOPNOTSUPP; 282 return (error); 283 } 284 285 static int 286 netkqfilter(struct cdev *dev, struct knote *kn) 287 { 288 INIT_VNET_NET(curvnet); 289 struct knlist *klist; 290 struct ifnet *ifp; 291 int idx; 292 293 switch (kn->kn_filter) { 294 case EVFILT_NETDEV: 295 kn->kn_fop = &netdev_filtops; 296 break; 297 default: 298 return (EINVAL); 299 } 300 301 idx = dev2unit(dev); 302 if (idx == 0) { 303 klist = &V_ifklist; 304 } else { 305 ifp = ifnet_byindex(idx); 306 if (ifp == NULL) 307 return (1); 308 klist = &ifp->if_klist; 309 } 310 311 kn->kn_hook = (caddr_t)klist; 312 313 knlist_add(klist, kn, 0); 314 315 return (0); 316 } 317 318 static void 319 filt_netdetach(struct knote *kn) 320 { 321 struct knlist *klist = (struct knlist *)kn->kn_hook; 322 323 knlist_remove(klist, kn, 0); 324 } 325 326 static int 327 filt_netdev(struct knote *kn, long hint) 328 { 329 struct knlist *klist = (struct knlist *)kn->kn_hook; 330 331 /* 332 * Currently NOTE_EXIT is abused to indicate device detach. 333 */ 334 if (hint == NOTE_EXIT) { 335 kn->kn_data = NOTE_LINKINV; 336 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 337 knlist_remove_inevent(klist, kn); 338 return (1); 339 } 340 if (hint != 0) 341 kn->kn_data = hint; /* current status */ 342 if (kn->kn_sfflags & hint) 343 kn->kn_fflags |= hint; 344 return (kn->kn_fflags != 0); 345 } 346 347 /* 348 * Network interface utility routines. 349 * 350 * Routines with ifa_ifwith* names take sockaddr *'s as 351 * parameters. 352 */ 353 354 /* ARGSUSED*/ 355 static void 356 if_init(void *dummy __unused) 357 { 358 INIT_VNET_NET(curvnet); 359 360 IFNET_LOCK_INIT(); 361 TAILQ_INIT(&V_ifnet); 362 TAILQ_INIT(&V_ifg_head); 363 knlist_init(&V_ifklist, NULL, NULL, NULL, NULL); 364 if_grow(); /* create initial table */ 365 ifdev_setbyindex(0, make_dev(&net_cdevsw, 0, UID_ROOT, GID_WHEEL, 366 0600, "network")); 367 if_clone_init(); 368 } 369 370 static void 371 if_grow(void) 372 { 373 INIT_VNET_NET(curvnet); 374 u_int n; 375 struct ifindex_entry *e; 376 377 V_if_indexlim <<= 1; 378 n = V_if_indexlim * sizeof(*e); 379 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO); 380 if (V_ifindex_table != NULL) { 381 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2); 382 free((caddr_t)V_ifindex_table, M_IFNET); 383 } 384 V_ifindex_table = e; 385 } 386 387 /* 388 * Allocate a struct ifnet and an index for an interface. A layer 2 389 * common structure will also be allocated if an allocation routine is 390 * registered for the passed type. 391 */ 392 struct ifnet* 393 if_alloc(u_char type) 394 { 395 INIT_VNET_NET(curvnet); 396 struct ifnet *ifp; 397 398 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); 399 400 /* 401 * Try to find an empty slot below if_index. If we fail, take 402 * the next slot. 403 * 404 * XXX: should be locked! 405 */ 406 for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) { 407 if (ifnet_byindex(ifp->if_index) == NULL) 408 break; 409 } 410 /* Catch if_index overflow. */ 411 if (ifp->if_index < 1) { 412 free(ifp, M_IFNET); 413 return (NULL); 414 } 415 if (ifp->if_index > V_if_index) 416 V_if_index = ifp->if_index; 417 if (V_if_index >= V_if_indexlim) 418 if_grow(); 419 420 ifp->if_type = type; 421 422 if (if_com_alloc[type] != NULL) { 423 ifp->if_l2com = if_com_alloc[type](type, ifp); 424 if (ifp->if_l2com == NULL) { 425 free(ifp, M_IFNET); 426 return (NULL); 427 } 428 } 429 IFNET_WLOCK(); 430 ifnet_setbyindex(ifp->if_index, ifp); 431 IFNET_WUNLOCK(); 432 IF_ADDR_LOCK_INIT(ifp); 433 434 return (ifp); 435 } 436 437 /* 438 * Free the struct ifnet, the associated index, and the layer 2 common 439 * structure if needed. All the work is done in if_free_type(). 440 * 441 * Do not add code to this function! Add it to if_free_type(). 442 */ 443 void 444 if_free(struct ifnet *ifp) 445 { 446 447 if_free_type(ifp, ifp->if_type); 448 } 449 450 /* 451 * Do the actual work of freeing a struct ifnet, associated index, and 452 * layer 2 common structure. This version should only be called by 453 * intefaces that switch their type after calling if_alloc(). 454 */ 455 void 456 if_free_type(struct ifnet *ifp, u_char type) 457 { 458 INIT_VNET_NET(curvnet); /* ifp->if_vnet can be NULL here ! */ 459 460 if (ifp != ifnet_byindex(ifp->if_index)) { 461 if_printf(ifp, "%s: value was not if_alloced, skipping\n", 462 __func__); 463 return; 464 } 465 466 IFNET_WLOCK(); 467 ifnet_setbyindex(ifp->if_index, NULL); 468 469 /* XXX: should be locked with if_findindex() */ 470 while (V_if_index > 0 && ifnet_byindex(V_if_index) == NULL) 471 V_if_index--; 472 IFNET_WUNLOCK(); 473 474 if (if_com_free[type] != NULL) 475 if_com_free[type](ifp->if_l2com, type); 476 477 IF_ADDR_LOCK_DESTROY(ifp); 478 free(ifp, M_IFNET); 479 }; 480 481 /* 482 * Perform generic interface initalization tasks and attach the interface 483 * to the list of "active" interfaces. 484 * 485 * XXX: 486 * - The decision to return void and thus require this function to 487 * succeed is questionable. 488 * - We do more initialization here then is probably a good idea. 489 * Some of this should probably move to if_alloc(). 490 * - We should probably do more sanity checking. For instance we don't 491 * do anything to insure if_xname is unique or non-empty. 492 */ 493 void 494 if_attach(struct ifnet *ifp) 495 { 496 INIT_VNET_NET(curvnet); 497 unsigned socksize, ifasize; 498 int namelen, masklen; 499 struct sockaddr_dl *sdl; 500 struct ifaddr *ifa; 501 502 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index)) 503 panic ("%s: BUG: if_attach called without if_alloc'd input()\n", 504 ifp->if_xname); 505 506 TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp); 507 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp); 508 IF_AFDATA_LOCK_INIT(ifp); 509 ifp->if_afdata_initialized = 0; 510 511 TAILQ_INIT(&ifp->if_addrhead); 512 TAILQ_INIT(&ifp->if_prefixhead); 513 TAILQ_INIT(&ifp->if_multiaddrs); 514 TAILQ_INIT(&ifp->if_groups); 515 516 if_addgroup(ifp, IFG_ALL); 517 518 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL); 519 getmicrotime(&ifp->if_lastchange); 520 ifp->if_data.ifi_epoch = time_uptime; 521 ifp->if_data.ifi_datalen = sizeof(struct if_data); 522 523 #ifdef MAC 524 mac_ifnet_init(ifp); 525 mac_ifnet_create(ifp); 526 #endif 527 528 ifdev_setbyindex(ifp->if_index, make_dev(&net_cdevsw, 529 ifp->if_index, UID_ROOT, GID_WHEEL, 0600, "%s/%s", 530 net_cdevsw.d_name, ifp->if_xname)); 531 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d", 532 net_cdevsw.d_name, ifp->if_index); 533 534 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); 535 536 /* 537 * create a Link Level name for this device 538 */ 539 namelen = strlen(ifp->if_xname); 540 /* 541 * Always save enough space for any possiable name so we can do 542 * a rename in place later. 543 */ 544 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ; 545 socksize = masklen + ifp->if_addrlen; 546 if (socksize < sizeof(*sdl)) 547 socksize = sizeof(*sdl); 548 socksize = roundup2(socksize, sizeof(long)); 549 ifasize = sizeof(*ifa) + 2 * socksize; 550 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 551 IFA_LOCK_INIT(ifa); 552 sdl = (struct sockaddr_dl *)(ifa + 1); 553 sdl->sdl_len = socksize; 554 sdl->sdl_family = AF_LINK; 555 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 556 sdl->sdl_nlen = namelen; 557 sdl->sdl_index = ifp->if_index; 558 sdl->sdl_type = ifp->if_type; 559 ifp->if_addr = ifa; 560 ifa->ifa_ifp = ifp; 561 ifa->ifa_rtrequest = link_rtrequest; 562 ifa->ifa_addr = (struct sockaddr *)sdl; 563 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 564 ifa->ifa_netmask = (struct sockaddr *)sdl; 565 sdl->sdl_len = masklen; 566 while (namelen != 0) 567 sdl->sdl_data[--namelen] = 0xff; 568 ifa->ifa_refcnt = 1; 569 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 570 ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */ 571 572 /* 573 * XXX: why do we warn about this? We're correcting it and most 574 * drivers just set the value the way we do. 575 */ 576 if (ifp->if_snd.ifq_maxlen == 0) { 577 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n"); 578 ifp->if_snd.ifq_maxlen = ifqmaxlen; 579 } 580 ifp->if_snd.altq_type = 0; 581 ifp->if_snd.altq_disc = NULL; 582 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE; 583 ifp->if_snd.altq_tbr = NULL; 584 ifp->if_snd.altq_ifp = ifp; 585 586 IFNET_WLOCK(); 587 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link); 588 IFNET_WUNLOCK(); 589 590 if (domain_init_status >= 2) 591 if_attachdomain1(ifp); 592 593 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 594 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); 595 596 /* Announce the interface. */ 597 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 598 599 if (ifp->if_watchdog != NULL) 600 if_printf(ifp, 601 "WARNING: using obsoleted if_watchdog interface\n"); 602 if (ifp->if_flags & IFF_NEEDSGIANT) 603 if_printf(ifp, 604 "WARNING: using obsoleted IFF_NEEDSGIANT flag\n"); 605 } 606 607 static void 608 if_attachdomain(void *dummy) 609 { 610 INIT_VNET_NET(curvnet); 611 struct ifnet *ifp; 612 int s; 613 614 s = splnet(); 615 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 616 if_attachdomain1(ifp); 617 splx(s); 618 } 619 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 620 if_attachdomain, NULL); 621 622 static void 623 if_attachdomain1(struct ifnet *ifp) 624 { 625 struct domain *dp; 626 int s; 627 628 s = splnet(); 629 630 /* 631 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we 632 * cannot lock ifp->if_afdata initialization, entirely. 633 */ 634 if (IF_AFDATA_TRYLOCK(ifp) == 0) { 635 splx(s); 636 return; 637 } 638 if (ifp->if_afdata_initialized >= domain_init_status) { 639 IF_AFDATA_UNLOCK(ifp); 640 splx(s); 641 printf("if_attachdomain called more than once on %s\n", 642 ifp->if_xname); 643 return; 644 } 645 ifp->if_afdata_initialized = domain_init_status; 646 IF_AFDATA_UNLOCK(ifp); 647 648 /* address family dependent data region */ 649 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 650 for (dp = domains; dp; dp = dp->dom_next) { 651 if (dp->dom_ifattach) 652 ifp->if_afdata[dp->dom_family] = 653 (*dp->dom_ifattach)(ifp); 654 } 655 656 splx(s); 657 } 658 659 /* 660 * Remove any unicast or broadcast network addresses from an interface. 661 */ 662 void 663 if_purgeaddrs(struct ifnet *ifp) 664 { 665 struct ifaddr *ifa, *next; 666 667 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 668 if (ifa->ifa_addr->sa_family == AF_LINK) 669 continue; 670 #ifdef INET 671 /* XXX: Ugly!! ad hoc just for INET */ 672 if (ifa->ifa_addr->sa_family == AF_INET) { 673 struct ifaliasreq ifr; 674 675 bzero(&ifr, sizeof(ifr)); 676 ifr.ifra_addr = *ifa->ifa_addr; 677 if (ifa->ifa_dstaddr) 678 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 679 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 680 NULL) == 0) 681 continue; 682 } 683 #endif /* INET */ 684 #ifdef INET6 685 if (ifa->ifa_addr->sa_family == AF_INET6) { 686 in6_purgeaddr(ifa); 687 /* ifp_addrhead is already updated */ 688 continue; 689 } 690 #endif /* INET6 */ 691 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 692 IFAFREE(ifa); 693 } 694 } 695 696 /* 697 * Remove any multicast network addresses from an interface. 698 */ 699 void 700 if_purgemaddrs(struct ifnet *ifp) 701 { 702 struct ifmultiaddr *ifma; 703 struct ifmultiaddr *next; 704 705 IF_ADDR_LOCK(ifp); 706 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 707 if_delmulti_locked(ifp, ifma, 1); 708 IF_ADDR_UNLOCK(ifp); 709 } 710 711 /* 712 * Detach an interface, removing it from the 713 * list of "active" interfaces. 714 * 715 * XXXRW: There are some significant questions about event ordering, and 716 * how to prevent things from starting to use the interface during detach. 717 */ 718 void 719 if_detach(struct ifnet *ifp) 720 { 721 INIT_VNET_NET(ifp->if_vnet); 722 struct ifaddr *ifa; 723 struct radix_node_head *rnh; 724 int s; 725 int i; 726 struct domain *dp; 727 struct ifnet *iter; 728 int found = 0; 729 730 IFNET_WLOCK(); 731 TAILQ_FOREACH(iter, &V_ifnet, if_link) 732 if (iter == ifp) { 733 TAILQ_REMOVE(&V_ifnet, ifp, if_link); 734 found = 1; 735 break; 736 } 737 IFNET_WUNLOCK(); 738 if (!found) 739 return; 740 741 /* 742 * Remove/wait for pending events. 743 */ 744 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 745 746 /* 747 * Remove routes and flush queues. 748 */ 749 s = splnet(); 750 if_down(ifp); 751 #ifdef ALTQ 752 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 753 altq_disable(&ifp->if_snd); 754 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 755 altq_detach(&ifp->if_snd); 756 #endif 757 758 if_purgeaddrs(ifp); 759 760 #ifdef INET 761 in_ifdetach(ifp); 762 #endif 763 764 #ifdef INET6 765 /* 766 * Remove all IPv6 kernel structs related to ifp. This should be done 767 * before removing routing entries below, since IPv6 interface direct 768 * routes are expected to be removed by the IPv6-specific kernel API. 769 * Otherwise, the kernel will detect some inconsistency and bark it. 770 */ 771 in6_ifdetach(ifp); 772 #endif 773 if_purgemaddrs(ifp); 774 775 /* 776 * Remove link ifaddr pointer and maybe decrement if_index. 777 * Clean up all addresses. 778 */ 779 ifp->if_addr = NULL; 780 destroy_dev(ifdev_byindex(ifp->if_index)); 781 ifdev_setbyindex(ifp->if_index, NULL); 782 783 /* We can now free link ifaddr. */ 784 if (!TAILQ_EMPTY(&ifp->if_addrhead)) { 785 ifa = TAILQ_FIRST(&ifp->if_addrhead); 786 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 787 IFAFREE(ifa); 788 } 789 790 /* 791 * Delete all remaining routes using this interface 792 * Unfortuneatly the only way to do this is to slog through 793 * the entire routing table looking for routes which point 794 * to this interface...oh well... 795 */ 796 for (i = 1; i <= AF_MAX; i++) { 797 int j; 798 for (j = 0; j < rt_numfibs; j++) { 799 if ((rnh = V_rt_tables[j][i]) == NULL) 800 continue; 801 RADIX_NODE_HEAD_LOCK(rnh); 802 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 803 RADIX_NODE_HEAD_UNLOCK(rnh); 804 } 805 } 806 807 /* Announce that the interface is gone. */ 808 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 809 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 810 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); 811 812 IF_AFDATA_LOCK(ifp); 813 for (dp = domains; dp; dp = dp->dom_next) { 814 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 815 (*dp->dom_ifdetach)(ifp, 816 ifp->if_afdata[dp->dom_family]); 817 } 818 IF_AFDATA_UNLOCK(ifp); 819 820 #ifdef MAC 821 mac_ifnet_destroy(ifp); 822 #endif /* MAC */ 823 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT); 824 knlist_clear(&ifp->if_klist, 0); 825 knlist_destroy(&ifp->if_klist); 826 mtx_destroy(&ifp->if_snd.ifq_mtx); 827 IF_AFDATA_DESTROY(ifp); 828 splx(s); 829 } 830 831 /* 832 * Add a group to an interface 833 */ 834 int 835 if_addgroup(struct ifnet *ifp, const char *groupname) 836 { 837 INIT_VNET_NET(ifp->if_vnet); 838 struct ifg_list *ifgl; 839 struct ifg_group *ifg = NULL; 840 struct ifg_member *ifgm; 841 842 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 843 groupname[strlen(groupname) - 1] <= '9') 844 return (EINVAL); 845 846 IFNET_WLOCK(); 847 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 848 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) { 849 IFNET_WUNLOCK(); 850 return (EEXIST); 851 } 852 853 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP, 854 M_NOWAIT)) == NULL) { 855 IFNET_WUNLOCK(); 856 return (ENOMEM); 857 } 858 859 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member), 860 M_TEMP, M_NOWAIT)) == NULL) { 861 free(ifgl, M_TEMP); 862 IFNET_WUNLOCK(); 863 return (ENOMEM); 864 } 865 866 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 867 if (!strcmp(ifg->ifg_group, groupname)) 868 break; 869 870 if (ifg == NULL) { 871 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group), 872 M_TEMP, M_NOWAIT)) == NULL) { 873 free(ifgl, M_TEMP); 874 free(ifgm, M_TEMP); 875 IFNET_WUNLOCK(); 876 return (ENOMEM); 877 } 878 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 879 ifg->ifg_refcnt = 0; 880 TAILQ_INIT(&ifg->ifg_members); 881 EVENTHANDLER_INVOKE(group_attach_event, ifg); 882 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next); 883 } 884 885 ifg->ifg_refcnt++; 886 ifgl->ifgl_group = ifg; 887 ifgm->ifgm_ifp = ifp; 888 889 IF_ADDR_LOCK(ifp); 890 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 891 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 892 IF_ADDR_UNLOCK(ifp); 893 894 IFNET_WUNLOCK(); 895 896 EVENTHANDLER_INVOKE(group_change_event, groupname); 897 898 return (0); 899 } 900 901 /* 902 * Remove a group from an interface 903 */ 904 int 905 if_delgroup(struct ifnet *ifp, const char *groupname) 906 { 907 INIT_VNET_NET(ifp->if_vnet); 908 struct ifg_list *ifgl; 909 struct ifg_member *ifgm; 910 911 IFNET_WLOCK(); 912 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 913 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 914 break; 915 if (ifgl == NULL) { 916 IFNET_WUNLOCK(); 917 return (ENOENT); 918 } 919 920 IF_ADDR_LOCK(ifp); 921 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 922 IF_ADDR_UNLOCK(ifp); 923 924 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 925 if (ifgm->ifgm_ifp == ifp) 926 break; 927 928 if (ifgm != NULL) { 929 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); 930 free(ifgm, M_TEMP); 931 } 932 933 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 934 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 935 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group); 936 free(ifgl->ifgl_group, M_TEMP); 937 } 938 IFNET_WUNLOCK(); 939 940 free(ifgl, M_TEMP); 941 942 EVENTHANDLER_INVOKE(group_change_event, groupname); 943 944 return (0); 945 } 946 947 /* 948 * Stores all groups from an interface in memory pointed 949 * to by data 950 */ 951 static int 952 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp) 953 { 954 int len, error; 955 struct ifg_list *ifgl; 956 struct ifg_req ifgrq, *ifgp; 957 struct ifgroupreq *ifgr = data; 958 959 if (ifgr->ifgr_len == 0) { 960 IF_ADDR_LOCK(ifp); 961 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 962 ifgr->ifgr_len += sizeof(struct ifg_req); 963 IF_ADDR_UNLOCK(ifp); 964 return (0); 965 } 966 967 len = ifgr->ifgr_len; 968 ifgp = ifgr->ifgr_groups; 969 /* XXX: wire */ 970 IF_ADDR_LOCK(ifp); 971 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 972 if (len < sizeof(ifgrq)) { 973 IF_ADDR_UNLOCK(ifp); 974 return (EINVAL); 975 } 976 bzero(&ifgrq, sizeof ifgrq); 977 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 978 sizeof(ifgrq.ifgrq_group)); 979 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 980 IF_ADDR_UNLOCK(ifp); 981 return (error); 982 } 983 len -= sizeof(ifgrq); 984 ifgp++; 985 } 986 IF_ADDR_UNLOCK(ifp); 987 988 return (0); 989 } 990 991 /* 992 * Stores all members of a group in memory pointed to by data 993 */ 994 static int 995 if_getgroupmembers(struct ifgroupreq *data) 996 { 997 INIT_VNET_NET(curvnet); 998 struct ifgroupreq *ifgr = data; 999 struct ifg_group *ifg; 1000 struct ifg_member *ifgm; 1001 struct ifg_req ifgrq, *ifgp; 1002 int len, error; 1003 1004 IFNET_RLOCK(); 1005 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 1006 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 1007 break; 1008 if (ifg == NULL) { 1009 IFNET_RUNLOCK(); 1010 return (ENOENT); 1011 } 1012 1013 if (ifgr->ifgr_len == 0) { 1014 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 1015 ifgr->ifgr_len += sizeof(ifgrq); 1016 IFNET_RUNLOCK(); 1017 return (0); 1018 } 1019 1020 len = ifgr->ifgr_len; 1021 ifgp = ifgr->ifgr_groups; 1022 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 1023 if (len < sizeof(ifgrq)) { 1024 IFNET_RUNLOCK(); 1025 return (EINVAL); 1026 } 1027 bzero(&ifgrq, sizeof ifgrq); 1028 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 1029 sizeof(ifgrq.ifgrq_member)); 1030 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1031 IFNET_RUNLOCK(); 1032 return (error); 1033 } 1034 len -= sizeof(ifgrq); 1035 ifgp++; 1036 } 1037 IFNET_RUNLOCK(); 1038 1039 return (0); 1040 } 1041 1042 /* 1043 * Delete Routes for a Network Interface 1044 * 1045 * Called for each routing entry via the rnh->rnh_walktree() call above 1046 * to delete all route entries referencing a detaching network interface. 1047 * 1048 * Arguments: 1049 * rn pointer to node in the routing table 1050 * arg argument passed to rnh->rnh_walktree() - detaching interface 1051 * 1052 * Returns: 1053 * 0 successful 1054 * errno failed - reason indicated 1055 * 1056 */ 1057 static int 1058 if_rtdel(struct radix_node *rn, void *arg) 1059 { 1060 struct rtentry *rt = (struct rtentry *)rn; 1061 struct ifnet *ifp = arg; 1062 int err; 1063 1064 if (rt->rt_ifp == ifp) { 1065 1066 /* 1067 * Protect (sorta) against walktree recursion problems 1068 * with cloned routes 1069 */ 1070 if ((rt->rt_flags & RTF_UP) == 0) 1071 return (0); 1072 1073 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway, 1074 rt_mask(rt), rt->rt_flags, 1075 (struct rtentry **) NULL, rt->rt_fibnum); 1076 if (err) { 1077 log(LOG_WARNING, "if_rtdel: error %d\n", err); 1078 } 1079 } 1080 1081 return (0); 1082 } 1083 1084 /* 1085 * XXX: Because sockaddr_dl has deeper structure than the sockaddr 1086 * structs used to represent other address families, it is necessary 1087 * to perform a different comparison. 1088 */ 1089 1090 #define sa_equal(a1, a2) \ 1091 (bcmp((a1), (a2), ((a1))->sa_len) == 0) 1092 1093 #define sa_dl_equal(a1, a2) \ 1094 ((((struct sockaddr_dl *)(a1))->sdl_len == \ 1095 ((struct sockaddr_dl *)(a2))->sdl_len) && \ 1096 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \ 1097 LLADDR((struct sockaddr_dl *)(a2)), \ 1098 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0)) 1099 1100 /* 1101 * Locate an interface based on a complete address. 1102 */ 1103 /*ARGSUSED*/ 1104 struct ifaddr * 1105 ifa_ifwithaddr(struct sockaddr *addr) 1106 { 1107 INIT_VNET_NET(curvnet); 1108 struct ifnet *ifp; 1109 struct ifaddr *ifa; 1110 1111 IFNET_RLOCK(); 1112 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 1113 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1114 if (ifa->ifa_addr->sa_family != addr->sa_family) 1115 continue; 1116 if (sa_equal(addr, ifa->ifa_addr)) 1117 goto done; 1118 /* IP6 doesn't have broadcast */ 1119 if ((ifp->if_flags & IFF_BROADCAST) && 1120 ifa->ifa_broadaddr && 1121 ifa->ifa_broadaddr->sa_len != 0 && 1122 sa_equal(ifa->ifa_broadaddr, addr)) 1123 goto done; 1124 } 1125 ifa = NULL; 1126 done: 1127 IFNET_RUNLOCK(); 1128 return (ifa); 1129 } 1130 1131 /* 1132 * Locate an interface based on the broadcast address. 1133 */ 1134 /* ARGSUSED */ 1135 struct ifaddr * 1136 ifa_ifwithbroadaddr(struct sockaddr *addr) 1137 { 1138 INIT_VNET_NET(curvnet); 1139 struct ifnet *ifp; 1140 struct ifaddr *ifa; 1141 1142 IFNET_RLOCK(); 1143 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 1144 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1145 if (ifa->ifa_addr->sa_family != addr->sa_family) 1146 continue; 1147 if ((ifp->if_flags & IFF_BROADCAST) && 1148 ifa->ifa_broadaddr && 1149 ifa->ifa_broadaddr->sa_len != 0 && 1150 sa_equal(ifa->ifa_broadaddr, addr)) 1151 goto done; 1152 } 1153 ifa = NULL; 1154 done: 1155 IFNET_RUNLOCK(); 1156 return (ifa); 1157 } 1158 1159 /* 1160 * Locate the point to point interface with a given destination address. 1161 */ 1162 /*ARGSUSED*/ 1163 struct ifaddr * 1164 ifa_ifwithdstaddr(struct sockaddr *addr) 1165 { 1166 INIT_VNET_NET(curvnet); 1167 struct ifnet *ifp; 1168 struct ifaddr *ifa; 1169 1170 IFNET_RLOCK(); 1171 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1172 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1173 continue; 1174 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1175 if (ifa->ifa_addr->sa_family != addr->sa_family) 1176 continue; 1177 if (ifa->ifa_dstaddr != NULL && 1178 sa_equal(addr, ifa->ifa_dstaddr)) 1179 goto done; 1180 } 1181 } 1182 ifa = NULL; 1183 done: 1184 IFNET_RUNLOCK(); 1185 return (ifa); 1186 } 1187 1188 /* 1189 * Find an interface on a specific network. If many, choice 1190 * is most specific found. 1191 */ 1192 struct ifaddr * 1193 ifa_ifwithnet(struct sockaddr *addr) 1194 { 1195 INIT_VNET_NET(curvnet); 1196 struct ifnet *ifp; 1197 struct ifaddr *ifa; 1198 struct ifaddr *ifa_maybe = (struct ifaddr *) 0; 1199 u_int af = addr->sa_family; 1200 char *addr_data = addr->sa_data, *cplim; 1201 1202 /* 1203 * AF_LINK addresses can be looked up directly by their index number, 1204 * so do that if we can. 1205 */ 1206 if (af == AF_LINK) { 1207 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 1208 if (sdl->sdl_index && sdl->sdl_index <= V_if_index) 1209 return (ifaddr_byindex(sdl->sdl_index)); 1210 } 1211 1212 /* 1213 * Scan though each interface, looking for ones that have 1214 * addresses in this address family. 1215 */ 1216 IFNET_RLOCK(); 1217 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1218 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1219 char *cp, *cp2, *cp3; 1220 1221 if (ifa->ifa_addr->sa_family != af) 1222 next: continue; 1223 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) { 1224 /* 1225 * This is a bit broken as it doesn't 1226 * take into account that the remote end may 1227 * be a single node in the network we are 1228 * looking for. 1229 * The trouble is that we don't know the 1230 * netmask for the remote end. 1231 */ 1232 if (ifa->ifa_dstaddr != NULL && 1233 sa_equal(addr, ifa->ifa_dstaddr)) 1234 goto done; 1235 } else { 1236 /* 1237 * if we have a special address handler, 1238 * then use it instead of the generic one. 1239 */ 1240 if (ifa->ifa_claim_addr) { 1241 if ((*ifa->ifa_claim_addr)(ifa, addr)) 1242 goto done; 1243 continue; 1244 } 1245 1246 /* 1247 * Scan all the bits in the ifa's address. 1248 * If a bit dissagrees with what we are 1249 * looking for, mask it with the netmask 1250 * to see if it really matters. 1251 * (A byte at a time) 1252 */ 1253 if (ifa->ifa_netmask == 0) 1254 continue; 1255 cp = addr_data; 1256 cp2 = ifa->ifa_addr->sa_data; 1257 cp3 = ifa->ifa_netmask->sa_data; 1258 cplim = ifa->ifa_netmask->sa_len 1259 + (char *)ifa->ifa_netmask; 1260 while (cp3 < cplim) 1261 if ((*cp++ ^ *cp2++) & *cp3++) 1262 goto next; /* next address! */ 1263 /* 1264 * If the netmask of what we just found 1265 * is more specific than what we had before 1266 * (if we had one) then remember the new one 1267 * before continuing to search 1268 * for an even better one. 1269 */ 1270 if (ifa_maybe == 0 || 1271 rn_refines((caddr_t)ifa->ifa_netmask, 1272 (caddr_t)ifa_maybe->ifa_netmask)) 1273 ifa_maybe = ifa; 1274 } 1275 } 1276 } 1277 ifa = ifa_maybe; 1278 done: 1279 IFNET_RUNLOCK(); 1280 return (ifa); 1281 } 1282 1283 /* 1284 * Find an interface address specific to an interface best matching 1285 * a given address. 1286 */ 1287 struct ifaddr * 1288 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 1289 { 1290 struct ifaddr *ifa; 1291 char *cp, *cp2, *cp3; 1292 char *cplim; 1293 struct ifaddr *ifa_maybe = 0; 1294 u_int af = addr->sa_family; 1295 1296 if (af >= AF_MAX) 1297 return (0); 1298 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1299 if (ifa->ifa_addr->sa_family != af) 1300 continue; 1301 if (ifa_maybe == 0) 1302 ifa_maybe = ifa; 1303 if (ifa->ifa_netmask == 0) { 1304 if (sa_equal(addr, ifa->ifa_addr) || 1305 (ifa->ifa_dstaddr && 1306 sa_equal(addr, ifa->ifa_dstaddr))) 1307 goto done; 1308 continue; 1309 } 1310 if (ifp->if_flags & IFF_POINTOPOINT) { 1311 if (sa_equal(addr, ifa->ifa_dstaddr)) 1312 goto done; 1313 } else { 1314 cp = addr->sa_data; 1315 cp2 = ifa->ifa_addr->sa_data; 1316 cp3 = ifa->ifa_netmask->sa_data; 1317 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1318 for (; cp3 < cplim; cp3++) 1319 if ((*cp++ ^ *cp2++) & *cp3) 1320 break; 1321 if (cp3 == cplim) 1322 goto done; 1323 } 1324 } 1325 ifa = ifa_maybe; 1326 done: 1327 return (ifa); 1328 } 1329 1330 #include <net/route.h> 1331 1332 /* 1333 * Default action when installing a route with a Link Level gateway. 1334 * Lookup an appropriate real ifa to point to. 1335 * This should be moved to /sys/net/link.c eventually. 1336 */ 1337 static void 1338 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 1339 { 1340 struct ifaddr *ifa, *oifa; 1341 struct sockaddr *dst; 1342 struct ifnet *ifp; 1343 1344 RT_LOCK_ASSERT(rt); 1345 1346 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 1347 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 1348 return; 1349 ifa = ifaof_ifpforaddr(dst, ifp); 1350 if (ifa) { 1351 IFAREF(ifa); /* XXX */ 1352 oifa = rt->rt_ifa; 1353 rt->rt_ifa = ifa; 1354 IFAFREE(oifa); 1355 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 1356 ifa->ifa_rtrequest(cmd, rt, info); 1357 } 1358 } 1359 1360 /* 1361 * Mark an interface down and notify protocols of 1362 * the transition. 1363 * NOTE: must be called at splnet or eqivalent. 1364 */ 1365 static void 1366 if_unroute(struct ifnet *ifp, int flag, int fam) 1367 { 1368 struct ifaddr *ifa; 1369 1370 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 1371 1372 ifp->if_flags &= ~flag; 1373 getmicrotime(&ifp->if_lastchange); 1374 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1375 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1376 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1377 if_qflush(&ifp->if_snd); 1378 #ifdef DEV_CARP 1379 if (ifp->if_carp) 1380 carp_carpdev_state(ifp->if_carp); 1381 #endif 1382 rt_ifmsg(ifp); 1383 } 1384 1385 /* 1386 * Mark an interface up and notify protocols of 1387 * the transition. 1388 * NOTE: must be called at splnet or eqivalent. 1389 */ 1390 static void 1391 if_route(struct ifnet *ifp, int flag, int fam) 1392 { 1393 struct ifaddr *ifa; 1394 1395 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 1396 1397 ifp->if_flags |= flag; 1398 getmicrotime(&ifp->if_lastchange); 1399 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1400 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1401 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1402 #ifdef DEV_CARP 1403 if (ifp->if_carp) 1404 carp_carpdev_state(ifp->if_carp); 1405 #endif 1406 rt_ifmsg(ifp); 1407 #ifdef INET6 1408 in6_if_up(ifp); 1409 #endif 1410 } 1411 1412 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */ 1413 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 1414 1415 /* 1416 * Handle a change in the interface link state. To avoid LORs 1417 * between driver lock and upper layer locks, as well as possible 1418 * recursions, we post event to taskqueue, and all job 1419 * is done in static do_link_state_change(). 1420 */ 1421 void 1422 if_link_state_change(struct ifnet *ifp, int link_state) 1423 { 1424 /* Return if state hasn't changed. */ 1425 if (ifp->if_link_state == link_state) 1426 return; 1427 1428 ifp->if_link_state = link_state; 1429 1430 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 1431 } 1432 1433 static void 1434 do_link_state_change(void *arg, int pending) 1435 { 1436 struct ifnet *ifp = (struct ifnet *)arg; 1437 int link_state = ifp->if_link_state; 1438 int link; 1439 CURVNET_SET(ifp->if_vnet); 1440 1441 /* Notify that the link state has changed. */ 1442 rt_ifmsg(ifp); 1443 if (link_state == LINK_STATE_UP) 1444 link = NOTE_LINKUP; 1445 else if (link_state == LINK_STATE_DOWN) 1446 link = NOTE_LINKDOWN; 1447 else 1448 link = NOTE_LINKINV; 1449 KNOTE_UNLOCKED(&ifp->if_klist, link); 1450 if (ifp->if_vlantrunk != NULL) 1451 (*vlan_link_state_p)(ifp, link); 1452 1453 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 1454 IFP2AC(ifp)->ac_netgraph != NULL) 1455 (*ng_ether_link_state_p)(ifp, link_state); 1456 #ifdef DEV_CARP 1457 if (ifp->if_carp) 1458 carp_carpdev_state(ifp->if_carp); 1459 #endif 1460 if (ifp->if_bridge) { 1461 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!")); 1462 (*bstp_linkstate_p)(ifp, link_state); 1463 } 1464 if (ifp->if_lagg) { 1465 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!")); 1466 (*lagg_linkstate_p)(ifp, link_state); 1467 } 1468 1469 devctl_notify("IFNET", ifp->if_xname, 1470 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL); 1471 if (pending > 1) 1472 if_printf(ifp, "%d link states coalesced\n", pending); 1473 if (log_link_state_change) 1474 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname, 1475 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 1476 CURVNET_RESTORE(); 1477 } 1478 1479 /* 1480 * Mark an interface down and notify protocols of 1481 * the transition. 1482 * NOTE: must be called at splnet or eqivalent. 1483 */ 1484 void 1485 if_down(struct ifnet *ifp) 1486 { 1487 1488 if_unroute(ifp, IFF_UP, AF_UNSPEC); 1489 } 1490 1491 /* 1492 * Mark an interface up and notify protocols of 1493 * the transition. 1494 * NOTE: must be called at splnet or eqivalent. 1495 */ 1496 void 1497 if_up(struct ifnet *ifp) 1498 { 1499 1500 if_route(ifp, IFF_UP, AF_UNSPEC); 1501 } 1502 1503 /* 1504 * Flush an interface queue. 1505 */ 1506 static void 1507 if_qflush(struct ifaltq *ifq) 1508 { 1509 struct mbuf *m, *n; 1510 1511 IFQ_LOCK(ifq); 1512 #ifdef ALTQ 1513 if (ALTQ_IS_ENABLED(ifq)) 1514 ALTQ_PURGE(ifq); 1515 #endif 1516 n = ifq->ifq_head; 1517 while ((m = n) != 0) { 1518 n = m->m_act; 1519 m_freem(m); 1520 } 1521 ifq->ifq_head = 0; 1522 ifq->ifq_tail = 0; 1523 ifq->ifq_len = 0; 1524 IFQ_UNLOCK(ifq); 1525 } 1526 1527 /* 1528 * Handle interface watchdog timer routines. Called 1529 * from softclock, we decrement timers (if set) and 1530 * call the appropriate interface routine on expiration. 1531 * 1532 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called 1533 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab 1534 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface. 1535 */ 1536 static void 1537 if_slowtimo(void *arg) 1538 { 1539 VNET_ITERATOR_DECL(vnet_iter); 1540 struct ifnet *ifp; 1541 int s = splimp(); 1542 1543 IFNET_RLOCK(); 1544 VNET_LIST_RLOCK(); 1545 VNET_FOREACH(vnet_iter) { 1546 CURVNET_SET(vnet_iter); 1547 INIT_VNET_NET(vnet_iter); 1548 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1549 if (ifp->if_timer == 0 || --ifp->if_timer) 1550 continue; 1551 if (ifp->if_watchdog) 1552 (*ifp->if_watchdog)(ifp); 1553 } 1554 CURVNET_RESTORE(); 1555 } 1556 VNET_LIST_RUNLOCK(); 1557 IFNET_RUNLOCK(); 1558 splx(s); 1559 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 1560 } 1561 1562 /* 1563 * Map interface name to 1564 * interface structure pointer. 1565 */ 1566 struct ifnet * 1567 ifunit(const char *name) 1568 { 1569 INIT_VNET_NET(curvnet); 1570 struct ifnet *ifp; 1571 1572 IFNET_RLOCK(); 1573 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1574 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 1575 break; 1576 } 1577 IFNET_RUNLOCK(); 1578 return (ifp); 1579 } 1580 1581 /* 1582 * Hardware specific interface ioctls. 1583 */ 1584 static int 1585 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 1586 { 1587 struct ifreq *ifr; 1588 struct ifstat *ifs; 1589 int error = 0; 1590 int new_flags, temp_flags; 1591 size_t namelen, onamelen; 1592 char new_name[IFNAMSIZ]; 1593 struct ifaddr *ifa; 1594 struct sockaddr_dl *sdl; 1595 1596 ifr = (struct ifreq *)data; 1597 switch (cmd) { 1598 case SIOCGIFINDEX: 1599 ifr->ifr_index = ifp->if_index; 1600 break; 1601 1602 case SIOCGIFFLAGS: 1603 temp_flags = ifp->if_flags | ifp->if_drv_flags; 1604 ifr->ifr_flags = temp_flags & 0xffff; 1605 ifr->ifr_flagshigh = temp_flags >> 16; 1606 break; 1607 1608 case SIOCGIFCAP: 1609 ifr->ifr_reqcap = ifp->if_capabilities; 1610 ifr->ifr_curcap = ifp->if_capenable; 1611 break; 1612 1613 #ifdef MAC 1614 case SIOCGIFMAC: 1615 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp); 1616 break; 1617 #endif 1618 1619 case SIOCGIFMETRIC: 1620 ifr->ifr_metric = ifp->if_metric; 1621 break; 1622 1623 case SIOCGIFMTU: 1624 ifr->ifr_mtu = ifp->if_mtu; 1625 break; 1626 1627 case SIOCGIFPHYS: 1628 ifr->ifr_phys = ifp->if_physical; 1629 break; 1630 1631 case SIOCSIFFLAGS: 1632 error = priv_check(td, PRIV_NET_SETIFFLAGS); 1633 if (error) 1634 return (error); 1635 /* 1636 * Currently, no driver owned flags pass the IFF_CANTCHANGE 1637 * check, so we don't need special handling here yet. 1638 */ 1639 new_flags = (ifr->ifr_flags & 0xffff) | 1640 (ifr->ifr_flagshigh << 16); 1641 if (ifp->if_flags & IFF_SMART) { 1642 /* Smart drivers twiddle their own routes */ 1643 } else if (ifp->if_flags & IFF_UP && 1644 (new_flags & IFF_UP) == 0) { 1645 int s = splimp(); 1646 if_down(ifp); 1647 splx(s); 1648 } else if (new_flags & IFF_UP && 1649 (ifp->if_flags & IFF_UP) == 0) { 1650 int s = splimp(); 1651 if_up(ifp); 1652 splx(s); 1653 } 1654 /* See if permanently promiscuous mode bit is about to flip */ 1655 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 1656 if (new_flags & IFF_PPROMISC) 1657 ifp->if_flags |= IFF_PROMISC; 1658 else if (ifp->if_pcount == 0) 1659 ifp->if_flags &= ~IFF_PROMISC; 1660 log(LOG_INFO, "%s: permanently promiscuous mode %s\n", 1661 ifp->if_xname, 1662 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled"); 1663 } 1664 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1665 (new_flags &~ IFF_CANTCHANGE); 1666 if (ifp->if_ioctl) { 1667 IFF_LOCKGIANT(ifp); 1668 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1669 IFF_UNLOCKGIANT(ifp); 1670 } 1671 getmicrotime(&ifp->if_lastchange); 1672 break; 1673 1674 case SIOCSIFCAP: 1675 error = priv_check(td, PRIV_NET_SETIFCAP); 1676 if (error) 1677 return (error); 1678 if (ifp->if_ioctl == NULL) 1679 return (EOPNOTSUPP); 1680 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 1681 return (EINVAL); 1682 IFF_LOCKGIANT(ifp); 1683 error = (*ifp->if_ioctl)(ifp, cmd, data); 1684 IFF_UNLOCKGIANT(ifp); 1685 if (error == 0) 1686 getmicrotime(&ifp->if_lastchange); 1687 break; 1688 1689 #ifdef MAC 1690 case SIOCSIFMAC: 1691 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp); 1692 break; 1693 #endif 1694 1695 case SIOCSIFNAME: 1696 error = priv_check(td, PRIV_NET_SETIFNAME); 1697 if (error) 1698 return (error); 1699 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); 1700 if (error != 0) 1701 return (error); 1702 if (new_name[0] == '\0') 1703 return (EINVAL); 1704 if (ifunit(new_name) != NULL) 1705 return (EEXIST); 1706 1707 /* Announce the departure of the interface. */ 1708 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 1709 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 1710 1711 log(LOG_INFO, "%s: changing name to '%s'\n", 1712 ifp->if_xname, new_name); 1713 1714 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 1715 ifa = ifp->if_addr; 1716 IFA_LOCK(ifa); 1717 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1718 namelen = strlen(new_name); 1719 onamelen = sdl->sdl_nlen; 1720 /* 1721 * Move the address if needed. This is safe because we 1722 * allocate space for a name of length IFNAMSIZ when we 1723 * create this in if_attach(). 1724 */ 1725 if (namelen != onamelen) { 1726 bcopy(sdl->sdl_data + onamelen, 1727 sdl->sdl_data + namelen, sdl->sdl_alen); 1728 } 1729 bcopy(new_name, sdl->sdl_data, namelen); 1730 sdl->sdl_nlen = namelen; 1731 sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 1732 bzero(sdl->sdl_data, onamelen); 1733 while (namelen != 0) 1734 sdl->sdl_data[--namelen] = 0xff; 1735 IFA_UNLOCK(ifa); 1736 1737 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 1738 /* Announce the return of the interface. */ 1739 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 1740 break; 1741 1742 case SIOCSIFMETRIC: 1743 error = priv_check(td, PRIV_NET_SETIFMETRIC); 1744 if (error) 1745 return (error); 1746 ifp->if_metric = ifr->ifr_metric; 1747 getmicrotime(&ifp->if_lastchange); 1748 break; 1749 1750 case SIOCSIFPHYS: 1751 error = priv_check(td, PRIV_NET_SETIFPHYS); 1752 if (error) 1753 return (error); 1754 if (ifp->if_ioctl == NULL) 1755 return (EOPNOTSUPP); 1756 IFF_LOCKGIANT(ifp); 1757 error = (*ifp->if_ioctl)(ifp, cmd, data); 1758 IFF_UNLOCKGIANT(ifp); 1759 if (error == 0) 1760 getmicrotime(&ifp->if_lastchange); 1761 break; 1762 1763 case SIOCSIFMTU: 1764 { 1765 u_long oldmtu = ifp->if_mtu; 1766 1767 error = priv_check(td, PRIV_NET_SETIFMTU); 1768 if (error) 1769 return (error); 1770 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 1771 return (EINVAL); 1772 if (ifp->if_ioctl == NULL) 1773 return (EOPNOTSUPP); 1774 IFF_LOCKGIANT(ifp); 1775 error = (*ifp->if_ioctl)(ifp, cmd, data); 1776 IFF_UNLOCKGIANT(ifp); 1777 if (error == 0) { 1778 getmicrotime(&ifp->if_lastchange); 1779 rt_ifmsg(ifp); 1780 } 1781 /* 1782 * If the link MTU changed, do network layer specific procedure. 1783 */ 1784 if (ifp->if_mtu != oldmtu) { 1785 #ifdef INET6 1786 nd6_setmtu(ifp); 1787 #endif 1788 } 1789 break; 1790 } 1791 1792 case SIOCADDMULTI: 1793 case SIOCDELMULTI: 1794 if (cmd == SIOCADDMULTI) 1795 error = priv_check(td, PRIV_NET_ADDMULTI); 1796 else 1797 error = priv_check(td, PRIV_NET_DELMULTI); 1798 if (error) 1799 return (error); 1800 1801 /* Don't allow group membership on non-multicast interfaces. */ 1802 if ((ifp->if_flags & IFF_MULTICAST) == 0) 1803 return (EOPNOTSUPP); 1804 1805 /* Don't let users screw up protocols' entries. */ 1806 if (ifr->ifr_addr.sa_family != AF_LINK) 1807 return (EINVAL); 1808 1809 if (cmd == SIOCADDMULTI) { 1810 struct ifmultiaddr *ifma; 1811 1812 /* 1813 * Userland is only permitted to join groups once 1814 * via the if_addmulti() KPI, because it cannot hold 1815 * struct ifmultiaddr * between calls. It may also 1816 * lose a race while we check if the membership 1817 * already exists. 1818 */ 1819 IF_ADDR_LOCK(ifp); 1820 ifma = if_findmulti(ifp, &ifr->ifr_addr); 1821 IF_ADDR_UNLOCK(ifp); 1822 if (ifma != NULL) 1823 error = EADDRINUSE; 1824 else 1825 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 1826 } else { 1827 error = if_delmulti(ifp, &ifr->ifr_addr); 1828 } 1829 if (error == 0) 1830 getmicrotime(&ifp->if_lastchange); 1831 break; 1832 1833 case SIOCSIFPHYADDR: 1834 case SIOCDIFPHYADDR: 1835 #ifdef INET6 1836 case SIOCSIFPHYADDR_IN6: 1837 #endif 1838 case SIOCSLIFPHYADDR: 1839 case SIOCSIFMEDIA: 1840 case SIOCSIFGENERIC: 1841 error = priv_check(td, PRIV_NET_HWIOCTL); 1842 if (error) 1843 return (error); 1844 if (ifp->if_ioctl == NULL) 1845 return (EOPNOTSUPP); 1846 IFF_LOCKGIANT(ifp); 1847 error = (*ifp->if_ioctl)(ifp, cmd, data); 1848 IFF_UNLOCKGIANT(ifp); 1849 if (error == 0) 1850 getmicrotime(&ifp->if_lastchange); 1851 break; 1852 1853 case SIOCGIFSTATUS: 1854 ifs = (struct ifstat *)data; 1855 ifs->ascii[0] = '\0'; 1856 1857 case SIOCGIFPSRCADDR: 1858 case SIOCGIFPDSTADDR: 1859 case SIOCGLIFPHYADDR: 1860 case SIOCGIFMEDIA: 1861 case SIOCGIFGENERIC: 1862 if (ifp->if_ioctl == NULL) 1863 return (EOPNOTSUPP); 1864 IFF_LOCKGIANT(ifp); 1865 error = (*ifp->if_ioctl)(ifp, cmd, data); 1866 IFF_UNLOCKGIANT(ifp); 1867 break; 1868 1869 case SIOCSIFLLADDR: 1870 error = priv_check(td, PRIV_NET_SETLLADDR); 1871 if (error) 1872 return (error); 1873 error = if_setlladdr(ifp, 1874 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 1875 break; 1876 1877 case SIOCAIFGROUP: 1878 { 1879 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 1880 1881 error = priv_check(td, PRIV_NET_ADDIFGROUP); 1882 if (error) 1883 return (error); 1884 if ((error = if_addgroup(ifp, ifgr->ifgr_group))) 1885 return (error); 1886 break; 1887 } 1888 1889 case SIOCGIFGROUP: 1890 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp))) 1891 return (error); 1892 break; 1893 1894 case SIOCDIFGROUP: 1895 { 1896 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 1897 1898 error = priv_check(td, PRIV_NET_DELIFGROUP); 1899 if (error) 1900 return (error); 1901 if ((error = if_delgroup(ifp, ifgr->ifgr_group))) 1902 return (error); 1903 break; 1904 } 1905 1906 default: 1907 error = ENOIOCTL; 1908 break; 1909 } 1910 return (error); 1911 } 1912 1913 /* 1914 * Interface ioctls. 1915 */ 1916 int 1917 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 1918 { 1919 struct ifnet *ifp; 1920 struct ifreq *ifr; 1921 int error; 1922 int oif_flags; 1923 1924 switch (cmd) { 1925 case SIOCGIFCONF: 1926 case OSIOCGIFCONF: 1927 #ifdef __amd64__ 1928 case SIOCGIFCONF32: 1929 #endif 1930 return (ifconf(cmd, data)); 1931 } 1932 ifr = (struct ifreq *)data; 1933 1934 switch (cmd) { 1935 case SIOCIFCREATE: 1936 case SIOCIFCREATE2: 1937 error = priv_check(td, PRIV_NET_IFCREATE); 1938 if (error) 1939 return (error); 1940 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name), 1941 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL)); 1942 case SIOCIFDESTROY: 1943 error = priv_check(td, PRIV_NET_IFDESTROY); 1944 if (error) 1945 return (error); 1946 return if_clone_destroy(ifr->ifr_name); 1947 1948 case SIOCIFGCLONERS: 1949 return (if_clone_list((struct if_clonereq *)data)); 1950 case SIOCGIFGMEMB: 1951 return (if_getgroupmembers((struct ifgroupreq *)data)); 1952 } 1953 1954 ifp = ifunit(ifr->ifr_name); 1955 if (ifp == 0) 1956 return (ENXIO); 1957 1958 error = ifhwioctl(cmd, ifp, data, td); 1959 if (error != ENOIOCTL) 1960 return (error); 1961 1962 oif_flags = ifp->if_flags; 1963 if (so->so_proto == 0) 1964 return (EOPNOTSUPP); 1965 #ifndef COMPAT_43 1966 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 1967 data, 1968 ifp, td)); 1969 #else 1970 { 1971 int ocmd = cmd; 1972 1973 switch (cmd) { 1974 1975 case SIOCSIFDSTADDR: 1976 case SIOCSIFADDR: 1977 case SIOCSIFBRDADDR: 1978 case SIOCSIFNETMASK: 1979 #if BYTE_ORDER != BIG_ENDIAN 1980 if (ifr->ifr_addr.sa_family == 0 && 1981 ifr->ifr_addr.sa_len < 16) { 1982 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 1983 ifr->ifr_addr.sa_len = 16; 1984 } 1985 #else 1986 if (ifr->ifr_addr.sa_len == 0) 1987 ifr->ifr_addr.sa_len = 16; 1988 #endif 1989 break; 1990 1991 case OSIOCGIFADDR: 1992 cmd = SIOCGIFADDR; 1993 break; 1994 1995 case OSIOCGIFDSTADDR: 1996 cmd = SIOCGIFDSTADDR; 1997 break; 1998 1999 case OSIOCGIFBRDADDR: 2000 cmd = SIOCGIFBRDADDR; 2001 break; 2002 2003 case OSIOCGIFNETMASK: 2004 cmd = SIOCGIFNETMASK; 2005 } 2006 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 2007 cmd, 2008 data, 2009 ifp, td)); 2010 switch (ocmd) { 2011 2012 case OSIOCGIFADDR: 2013 case OSIOCGIFDSTADDR: 2014 case OSIOCGIFBRDADDR: 2015 case OSIOCGIFNETMASK: 2016 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 2017 2018 } 2019 } 2020 #endif /* COMPAT_43 */ 2021 2022 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 2023 #ifdef INET6 2024 DELAY(100);/* XXX: temporary workaround for fxp issue*/ 2025 if (ifp->if_flags & IFF_UP) { 2026 int s = splimp(); 2027 in6_if_up(ifp); 2028 splx(s); 2029 } 2030 #endif 2031 } 2032 return (error); 2033 } 2034 2035 /* 2036 * The code common to handling reference counted flags, 2037 * e.g., in ifpromisc() and if_allmulti(). 2038 * The "pflag" argument can specify a permanent mode flag to check, 2039 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 2040 * 2041 * Only to be used on stack-owned flags, not driver-owned flags. 2042 */ 2043 static int 2044 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 2045 { 2046 struct ifreq ifr; 2047 int error; 2048 int oldflags, oldcount; 2049 2050 /* Sanity checks to catch programming errors */ 2051 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 2052 ("%s: setting driver-owned flag %d", __func__, flag)); 2053 2054 if (onswitch) 2055 KASSERT(*refcount >= 0, 2056 ("%s: increment negative refcount %d for flag %d", 2057 __func__, *refcount, flag)); 2058 else 2059 KASSERT(*refcount > 0, 2060 ("%s: decrement non-positive refcount %d for flag %d", 2061 __func__, *refcount, flag)); 2062 2063 /* In case this mode is permanent, just touch refcount */ 2064 if (ifp->if_flags & pflag) { 2065 *refcount += onswitch ? 1 : -1; 2066 return (0); 2067 } 2068 2069 /* Save ifnet parameters for if_ioctl() may fail */ 2070 oldcount = *refcount; 2071 oldflags = ifp->if_flags; 2072 2073 /* 2074 * See if we aren't the only and touching refcount is enough. 2075 * Actually toggle interface flag if we are the first or last. 2076 */ 2077 if (onswitch) { 2078 if ((*refcount)++) 2079 return (0); 2080 ifp->if_flags |= flag; 2081 } else { 2082 if (--(*refcount)) 2083 return (0); 2084 ifp->if_flags &= ~flag; 2085 } 2086 2087 /* Call down the driver since we've changed interface flags */ 2088 if (ifp->if_ioctl == NULL) { 2089 error = EOPNOTSUPP; 2090 goto recover; 2091 } 2092 ifr.ifr_flags = ifp->if_flags & 0xffff; 2093 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2094 IFF_LOCKGIANT(ifp); 2095 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2096 IFF_UNLOCKGIANT(ifp); 2097 if (error) 2098 goto recover; 2099 /* Notify userland that interface flags have changed */ 2100 rt_ifmsg(ifp); 2101 return (0); 2102 2103 recover: 2104 /* Recover after driver error */ 2105 *refcount = oldcount; 2106 ifp->if_flags = oldflags; 2107 return (error); 2108 } 2109 2110 /* 2111 * Set/clear promiscuous mode on interface ifp based on the truth value 2112 * of pswitch. The calls are reference counted so that only the first 2113 * "on" request actually has an effect, as does the final "off" request. 2114 * Results are undefined if the "off" and "on" requests are not matched. 2115 */ 2116 int 2117 ifpromisc(struct ifnet *ifp, int pswitch) 2118 { 2119 int error; 2120 int oldflags = ifp->if_flags; 2121 2122 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 2123 &ifp->if_pcount, pswitch); 2124 /* If promiscuous mode status has changed, log a message */ 2125 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC)) 2126 log(LOG_INFO, "%s: promiscuous mode %s\n", 2127 ifp->if_xname, 2128 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 2129 return (error); 2130 } 2131 2132 /* 2133 * Return interface configuration 2134 * of system. List may be used 2135 * in later ioctl's (above) to get 2136 * other information. 2137 */ 2138 /*ARGSUSED*/ 2139 static int 2140 ifconf(u_long cmd, caddr_t data) 2141 { 2142 INIT_VNET_NET(curvnet); 2143 struct ifconf *ifc = (struct ifconf *)data; 2144 #ifdef __amd64__ 2145 struct ifconf32 *ifc32 = (struct ifconf32 *)data; 2146 struct ifconf ifc_swab; 2147 #endif 2148 struct ifnet *ifp; 2149 struct ifaddr *ifa; 2150 struct ifreq ifr; 2151 struct sbuf *sb; 2152 int error, full = 0, valid_len, max_len; 2153 2154 #ifdef __amd64__ 2155 if (cmd == SIOCGIFCONF32) { 2156 ifc_swab.ifc_len = ifc32->ifc_len; 2157 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf; 2158 ifc = &ifc_swab; 2159 } 2160 #endif 2161 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 2162 max_len = MAXPHYS - 1; 2163 2164 /* Prevent hostile input from being able to crash the system */ 2165 if (ifc->ifc_len <= 0) 2166 return (EINVAL); 2167 2168 again: 2169 if (ifc->ifc_len <= max_len) { 2170 max_len = ifc->ifc_len; 2171 full = 1; 2172 } 2173 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 2174 max_len = 0; 2175 valid_len = 0; 2176 2177 IFNET_RLOCK(); /* could sleep XXX */ 2178 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2179 int addrs; 2180 2181 /* 2182 * Zero the ifr_name buffer to make sure we don't 2183 * disclose the contents of the stack. 2184 */ 2185 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name)); 2186 2187 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 2188 >= sizeof(ifr.ifr_name)) { 2189 sbuf_delete(sb); 2190 IFNET_RUNLOCK(); 2191 return (ENAMETOOLONG); 2192 } 2193 2194 addrs = 0; 2195 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2196 struct sockaddr *sa = ifa->ifa_addr; 2197 2198 if (jailed(curthread->td_ucred) && 2199 prison_if(curthread->td_ucred, sa)) 2200 continue; 2201 addrs++; 2202 #ifdef COMPAT_43 2203 if (cmd == OSIOCGIFCONF) { 2204 struct osockaddr *osa = 2205 (struct osockaddr *)&ifr.ifr_addr; 2206 ifr.ifr_addr = *sa; 2207 osa->sa_family = sa->sa_family; 2208 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2209 max_len += sizeof(ifr); 2210 } else 2211 #endif 2212 if (sa->sa_len <= sizeof(*sa)) { 2213 ifr.ifr_addr = *sa; 2214 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2215 max_len += sizeof(ifr); 2216 } else { 2217 sbuf_bcat(sb, &ifr, 2218 offsetof(struct ifreq, ifr_addr)); 2219 max_len += offsetof(struct ifreq, ifr_addr); 2220 sbuf_bcat(sb, sa, sa->sa_len); 2221 max_len += sa->sa_len; 2222 } 2223 2224 if (!sbuf_overflowed(sb)) 2225 valid_len = sbuf_len(sb); 2226 } 2227 if (addrs == 0) { 2228 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 2229 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2230 max_len += sizeof(ifr); 2231 2232 if (!sbuf_overflowed(sb)) 2233 valid_len = sbuf_len(sb); 2234 } 2235 } 2236 IFNET_RUNLOCK(); 2237 2238 /* 2239 * If we didn't allocate enough space (uncommon), try again. If 2240 * we have already allocated as much space as we are allowed, 2241 * return what we've got. 2242 */ 2243 if (valid_len != max_len && !full) { 2244 sbuf_delete(sb); 2245 goto again; 2246 } 2247 2248 ifc->ifc_len = valid_len; 2249 #ifdef __amd64__ 2250 if (cmd == SIOCGIFCONF32) 2251 ifc32->ifc_len = valid_len; 2252 #endif 2253 sbuf_finish(sb); 2254 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 2255 sbuf_delete(sb); 2256 return (error); 2257 } 2258 2259 /* 2260 * Just like ifpromisc(), but for all-multicast-reception mode. 2261 */ 2262 int 2263 if_allmulti(struct ifnet *ifp, int onswitch) 2264 { 2265 2266 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 2267 } 2268 2269 struct ifmultiaddr * 2270 if_findmulti(struct ifnet *ifp, struct sockaddr *sa) 2271 { 2272 struct ifmultiaddr *ifma; 2273 2274 IF_ADDR_LOCK_ASSERT(ifp); 2275 2276 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2277 if (sa->sa_family == AF_LINK) { 2278 if (sa_dl_equal(ifma->ifma_addr, sa)) 2279 break; 2280 } else { 2281 if (sa_equal(ifma->ifma_addr, sa)) 2282 break; 2283 } 2284 } 2285 2286 return ifma; 2287 } 2288 2289 /* 2290 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 2291 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 2292 * the ifnet multicast address list here, so the caller must do that and 2293 * other setup work (such as notifying the device driver). The reference 2294 * count is initialized to 1. 2295 */ 2296 static struct ifmultiaddr * 2297 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 2298 int mflags) 2299 { 2300 struct ifmultiaddr *ifma; 2301 struct sockaddr *dupsa; 2302 2303 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags | 2304 M_ZERO); 2305 if (ifma == NULL) 2306 return (NULL); 2307 2308 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags); 2309 if (dupsa == NULL) { 2310 free(ifma, M_IFMADDR); 2311 return (NULL); 2312 } 2313 bcopy(sa, dupsa, sa->sa_len); 2314 ifma->ifma_addr = dupsa; 2315 2316 ifma->ifma_ifp = ifp; 2317 ifma->ifma_refcount = 1; 2318 ifma->ifma_protospec = NULL; 2319 2320 if (llsa == NULL) { 2321 ifma->ifma_lladdr = NULL; 2322 return (ifma); 2323 } 2324 2325 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags); 2326 if (dupsa == NULL) { 2327 free(ifma->ifma_addr, M_IFMADDR); 2328 free(ifma, M_IFMADDR); 2329 return (NULL); 2330 } 2331 bcopy(llsa, dupsa, llsa->sa_len); 2332 ifma->ifma_lladdr = dupsa; 2333 2334 return (ifma); 2335 } 2336 2337 /* 2338 * if_freemulti: free ifmultiaddr structure and possibly attached related 2339 * addresses. The caller is responsible for implementing reference 2340 * counting, notifying the driver, handling routing messages, and releasing 2341 * any dependent link layer state. 2342 */ 2343 static void 2344 if_freemulti(struct ifmultiaddr *ifma) 2345 { 2346 2347 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 2348 ifma->ifma_refcount)); 2349 KASSERT(ifma->ifma_protospec == NULL, 2350 ("if_freemulti: protospec not NULL")); 2351 2352 if (ifma->ifma_lladdr != NULL) 2353 free(ifma->ifma_lladdr, M_IFMADDR); 2354 free(ifma->ifma_addr, M_IFMADDR); 2355 free(ifma, M_IFMADDR); 2356 } 2357 2358 /* 2359 * Register an additional multicast address with a network interface. 2360 * 2361 * - If the address is already present, bump the reference count on the 2362 * address and return. 2363 * - If the address is not link-layer, look up a link layer address. 2364 * - Allocate address structures for one or both addresses, and attach to the 2365 * multicast address list on the interface. If automatically adding a link 2366 * layer address, the protocol address will own a reference to the link 2367 * layer address, to be freed when it is freed. 2368 * - Notify the network device driver of an addition to the multicast address 2369 * list. 2370 * 2371 * 'sa' points to caller-owned memory with the desired multicast address. 2372 * 2373 * 'retifma' will be used to return a pointer to the resulting multicast 2374 * address reference, if desired. 2375 */ 2376 int 2377 if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 2378 struct ifmultiaddr **retifma) 2379 { 2380 struct ifmultiaddr *ifma, *ll_ifma; 2381 struct sockaddr *llsa; 2382 int error; 2383 2384 /* 2385 * If the address is already present, return a new reference to it; 2386 * otherwise, allocate storage and set up a new address. 2387 */ 2388 IF_ADDR_LOCK(ifp); 2389 ifma = if_findmulti(ifp, sa); 2390 if (ifma != NULL) { 2391 ifma->ifma_refcount++; 2392 if (retifma != NULL) 2393 *retifma = ifma; 2394 IF_ADDR_UNLOCK(ifp); 2395 return (0); 2396 } 2397 2398 /* 2399 * The address isn't already present; resolve the protocol address 2400 * into a link layer address, and then look that up, bump its 2401 * refcount or allocate an ifma for that also. If 'llsa' was 2402 * returned, we will need to free it later. 2403 */ 2404 llsa = NULL; 2405 ll_ifma = NULL; 2406 if (ifp->if_resolvemulti != NULL) { 2407 error = ifp->if_resolvemulti(ifp, &llsa, sa); 2408 if (error) 2409 goto unlock_out; 2410 } 2411 2412 /* 2413 * Allocate the new address. Don't hook it up yet, as we may also 2414 * need to allocate a link layer multicast address. 2415 */ 2416 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 2417 if (ifma == NULL) { 2418 error = ENOMEM; 2419 goto free_llsa_out; 2420 } 2421 2422 /* 2423 * If a link layer address is found, we'll need to see if it's 2424 * already present in the address list, or allocate is as well. 2425 * When this block finishes, the link layer address will be on the 2426 * list. 2427 */ 2428 if (llsa != NULL) { 2429 ll_ifma = if_findmulti(ifp, llsa); 2430 if (ll_ifma == NULL) { 2431 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 2432 if (ll_ifma == NULL) { 2433 --ifma->ifma_refcount; 2434 if_freemulti(ifma); 2435 error = ENOMEM; 2436 goto free_llsa_out; 2437 } 2438 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 2439 ifma_link); 2440 } else 2441 ll_ifma->ifma_refcount++; 2442 ifma->ifma_llifma = ll_ifma; 2443 } 2444 2445 /* 2446 * We now have a new multicast address, ifma, and possibly a new or 2447 * referenced link layer address. Add the primary address to the 2448 * ifnet address list. 2449 */ 2450 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 2451 2452 if (retifma != NULL) 2453 *retifma = ifma; 2454 2455 /* 2456 * Must generate the message while holding the lock so that 'ifma' 2457 * pointer is still valid. 2458 */ 2459 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 2460 IF_ADDR_UNLOCK(ifp); 2461 2462 /* 2463 * We are certain we have added something, so call down to the 2464 * interface to let them know about it. 2465 */ 2466 if (ifp->if_ioctl != NULL) { 2467 IFF_LOCKGIANT(ifp); 2468 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 2469 IFF_UNLOCKGIANT(ifp); 2470 } 2471 2472 if (llsa != NULL) 2473 free(llsa, M_IFMADDR); 2474 2475 return (0); 2476 2477 free_llsa_out: 2478 if (llsa != NULL) 2479 free(llsa, M_IFMADDR); 2480 2481 unlock_out: 2482 IF_ADDR_UNLOCK(ifp); 2483 return (error); 2484 } 2485 2486 /* 2487 * Delete a multicast group membership by network-layer group address. 2488 * 2489 * Returns ENOENT if the entry could not be found. If ifp no longer 2490 * exists, results are undefined. This entry point should only be used 2491 * from subsystems which do appropriate locking to hold ifp for the 2492 * duration of the call. 2493 * Network-layer protocol domains must use if_delmulti_ifma(). 2494 */ 2495 int 2496 if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 2497 { 2498 struct ifmultiaddr *ifma; 2499 int lastref; 2500 #ifdef INVARIANTS 2501 struct ifnet *oifp; 2502 INIT_VNET_NET(ifp->if_vnet); 2503 2504 IFNET_RLOCK(); 2505 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 2506 if (ifp == oifp) 2507 break; 2508 if (ifp != oifp) 2509 ifp = NULL; 2510 IFNET_RUNLOCK(); 2511 2512 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__)); 2513 #endif 2514 if (ifp == NULL) 2515 return (ENOENT); 2516 2517 IF_ADDR_LOCK(ifp); 2518 lastref = 0; 2519 ifma = if_findmulti(ifp, sa); 2520 if (ifma != NULL) 2521 lastref = if_delmulti_locked(ifp, ifma, 0); 2522 IF_ADDR_UNLOCK(ifp); 2523 2524 if (ifma == NULL) 2525 return (ENOENT); 2526 2527 if (lastref && ifp->if_ioctl != NULL) { 2528 IFF_LOCKGIANT(ifp); 2529 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2530 IFF_UNLOCKGIANT(ifp); 2531 } 2532 2533 return (0); 2534 } 2535 2536 /* 2537 * Delete a multicast group membership by group membership pointer. 2538 * Network-layer protocol domains must use this routine. 2539 * 2540 * It is safe to call this routine if the ifp disappeared. Callers should 2541 * hold IFF_LOCKGIANT() to avoid a LOR in case the hardware needs to be 2542 * reconfigured. 2543 */ 2544 void 2545 if_delmulti_ifma(struct ifmultiaddr *ifma) 2546 { 2547 #ifdef DIAGNOSTIC 2548 INIT_VNET_NET(curvnet); 2549 #endif 2550 struct ifnet *ifp; 2551 int lastref; 2552 2553 ifp = ifma->ifma_ifp; 2554 #ifdef DIAGNOSTIC 2555 if (ifp == NULL) { 2556 printf("%s: ifma_ifp seems to be detached\n", __func__); 2557 } else { 2558 struct ifnet *oifp; 2559 2560 IFNET_RLOCK(); 2561 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 2562 if (ifp == oifp) 2563 break; 2564 if (ifp != oifp) { 2565 printf("%s: ifnet %p disappeared\n", __func__, ifp); 2566 ifp = NULL; 2567 } 2568 IFNET_RUNLOCK(); 2569 } 2570 #endif 2571 /* 2572 * If and only if the ifnet instance exists: Acquire the address lock. 2573 */ 2574 if (ifp != NULL) 2575 IF_ADDR_LOCK(ifp); 2576 2577 lastref = if_delmulti_locked(ifp, ifma, 0); 2578 2579 if (ifp != NULL) { 2580 /* 2581 * If and only if the ifnet instance exists: 2582 * Release the address lock. 2583 * If the group was left: update the hardware hash filter. 2584 */ 2585 IF_ADDR_UNLOCK(ifp); 2586 if (lastref && ifp->if_ioctl != NULL) { 2587 IFF_LOCKGIANT(ifp); 2588 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2589 IFF_UNLOCKGIANT(ifp); 2590 } 2591 } 2592 } 2593 2594 /* 2595 * Perform deletion of network-layer and/or link-layer multicast address. 2596 * 2597 * Return 0 if the reference count was decremented. 2598 * Return 1 if the final reference was released, indicating that the 2599 * hardware hash filter should be reprogrammed. 2600 */ 2601 static int 2602 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 2603 { 2604 struct ifmultiaddr *ll_ifma; 2605 2606 if (ifp != NULL && ifma->ifma_ifp != NULL) { 2607 KASSERT(ifma->ifma_ifp == ifp, 2608 ("%s: inconsistent ifp %p", __func__, ifp)); 2609 IF_ADDR_LOCK_ASSERT(ifp); 2610 } 2611 2612 ifp = ifma->ifma_ifp; 2613 2614 /* 2615 * If the ifnet is detaching, null out references to ifnet, 2616 * so that upper protocol layers will notice, and not attempt 2617 * to obtain locks for an ifnet which no longer exists. The 2618 * routing socket announcement must happen before the ifnet 2619 * instance is detached from the system. 2620 */ 2621 if (detaching) { 2622 #ifdef DIAGNOSTIC 2623 printf("%s: detaching ifnet instance %p\n", __func__, ifp); 2624 #endif 2625 /* 2626 * ifp may already be nulled out if we are being reentered 2627 * to delete the ll_ifma. 2628 */ 2629 if (ifp != NULL) { 2630 rt_newmaddrmsg(RTM_DELMADDR, ifma); 2631 ifma->ifma_ifp = NULL; 2632 } 2633 } 2634 2635 if (--ifma->ifma_refcount > 0) 2636 return 0; 2637 2638 /* 2639 * If this ifma is a network-layer ifma, a link-layer ifma may 2640 * have been associated with it. Release it first if so. 2641 */ 2642 ll_ifma = ifma->ifma_llifma; 2643 if (ll_ifma != NULL) { 2644 KASSERT(ifma->ifma_lladdr != NULL, 2645 ("%s: llifma w/o lladdr", __func__)); 2646 if (detaching) 2647 ll_ifma->ifma_ifp = NULL; /* XXX */ 2648 if (--ll_ifma->ifma_refcount == 0) { 2649 if (ifp != NULL) { 2650 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, 2651 ifma_link); 2652 } 2653 if_freemulti(ll_ifma); 2654 } 2655 } 2656 2657 if (ifp != NULL) 2658 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 2659 2660 if_freemulti(ifma); 2661 2662 /* 2663 * The last reference to this instance of struct ifmultiaddr 2664 * was released; the hardware should be notified of this change. 2665 */ 2666 return 1; 2667 } 2668 2669 /* 2670 * Set the link layer address on an interface. 2671 * 2672 * At this time we only support certain types of interfaces, 2673 * and we don't allow the length of the address to change. 2674 */ 2675 int 2676 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 2677 { 2678 struct sockaddr_dl *sdl; 2679 struct ifaddr *ifa; 2680 struct ifreq ifr; 2681 2682 ifa = ifp->if_addr; 2683 if (ifa == NULL) 2684 return (EINVAL); 2685 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 2686 if (sdl == NULL) 2687 return (EINVAL); 2688 if (len != sdl->sdl_alen) /* don't allow length to change */ 2689 return (EINVAL); 2690 switch (ifp->if_type) { 2691 case IFT_ETHER: 2692 case IFT_FDDI: 2693 case IFT_XETHER: 2694 case IFT_ISO88025: 2695 case IFT_L2VLAN: 2696 case IFT_BRIDGE: 2697 case IFT_ARCNET: 2698 case IFT_IEEE8023ADLAG: 2699 bcopy(lladdr, LLADDR(sdl), len); 2700 break; 2701 default: 2702 return (ENODEV); 2703 } 2704 /* 2705 * If the interface is already up, we need 2706 * to re-init it in order to reprogram its 2707 * address filter. 2708 */ 2709 if ((ifp->if_flags & IFF_UP) != 0) { 2710 if (ifp->if_ioctl) { 2711 IFF_LOCKGIANT(ifp); 2712 ifp->if_flags &= ~IFF_UP; 2713 ifr.ifr_flags = ifp->if_flags & 0xffff; 2714 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2715 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2716 ifp->if_flags |= IFF_UP; 2717 ifr.ifr_flags = ifp->if_flags & 0xffff; 2718 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2719 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2720 IFF_UNLOCKGIANT(ifp); 2721 } 2722 #ifdef INET 2723 /* 2724 * Also send gratuitous ARPs to notify other nodes about 2725 * the address change. 2726 */ 2727 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2728 if (ifa->ifa_addr->sa_family == AF_INET) 2729 arp_ifinit(ifp, ifa); 2730 } 2731 #endif 2732 } 2733 return (0); 2734 } 2735 2736 /* 2737 * The name argument must be a pointer to storage which will last as 2738 * long as the interface does. For physical devices, the result of 2739 * device_get_name(dev) is a good choice and for pseudo-devices a 2740 * static string works well. 2741 */ 2742 void 2743 if_initname(struct ifnet *ifp, const char *name, int unit) 2744 { 2745 ifp->if_dname = name; 2746 ifp->if_dunit = unit; 2747 if (unit != IF_DUNIT_NONE) 2748 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 2749 else 2750 strlcpy(ifp->if_xname, name, IFNAMSIZ); 2751 } 2752 2753 int 2754 if_printf(struct ifnet *ifp, const char * fmt, ...) 2755 { 2756 va_list ap; 2757 int retval; 2758 2759 retval = printf("%s: ", ifp->if_xname); 2760 va_start(ap, fmt); 2761 retval += vprintf(fmt, ap); 2762 va_end(ap); 2763 return (retval); 2764 } 2765 2766 /* 2767 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot 2768 * be called without Giant. However, we often can't acquire the Giant lock 2769 * at those points; instead, we run it via a task queue that holds Giant via 2770 * if_start_deferred. 2771 * 2772 * XXXRW: We need to make sure that the ifnet isn't fully detached until any 2773 * outstanding if_start_deferred() tasks that will run after the free. This 2774 * probably means waiting in if_detach(). 2775 */ 2776 void 2777 if_start(struct ifnet *ifp) 2778 { 2779 2780 if (ifp->if_flags & IFF_NEEDSGIANT) { 2781 if (mtx_owned(&Giant)) 2782 (*(ifp)->if_start)(ifp); 2783 else 2784 taskqueue_enqueue(taskqueue_swi_giant, 2785 &ifp->if_starttask); 2786 } else 2787 (*(ifp)->if_start)(ifp); 2788 } 2789 2790 static void 2791 if_start_deferred(void *context, int pending) 2792 { 2793 struct ifnet *ifp; 2794 2795 GIANT_REQUIRED; 2796 2797 ifp = context; 2798 (ifp->if_start)(ifp); 2799 } 2800 2801 int 2802 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 2803 { 2804 int active = 0; 2805 2806 IF_LOCK(ifq); 2807 if (_IF_QFULL(ifq)) { 2808 _IF_DROP(ifq); 2809 IF_UNLOCK(ifq); 2810 m_freem(m); 2811 return (0); 2812 } 2813 if (ifp != NULL) { 2814 ifp->if_obytes += m->m_pkthdr.len + adjust; 2815 if (m->m_flags & (M_BCAST|M_MCAST)) 2816 ifp->if_omcasts++; 2817 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 2818 } 2819 _IF_ENQUEUE(ifq, m); 2820 IF_UNLOCK(ifq); 2821 if (ifp != NULL && !active) 2822 if_start(ifp); 2823 return (1); 2824 } 2825 2826 void 2827 if_register_com_alloc(u_char type, 2828 if_com_alloc_t *a, if_com_free_t *f) 2829 { 2830 2831 KASSERT(if_com_alloc[type] == NULL, 2832 ("if_register_com_alloc: %d already registered", type)); 2833 KASSERT(if_com_free[type] == NULL, 2834 ("if_register_com_alloc: %d free already registered", type)); 2835 2836 if_com_alloc[type] = a; 2837 if_com_free[type] = f; 2838 } 2839 2840 void 2841 if_deregister_com_alloc(u_char type) 2842 { 2843 2844 KASSERT(if_com_alloc[type] != NULL, 2845 ("if_deregister_com_alloc: %d not registered", type)); 2846 KASSERT(if_com_free[type] != NULL, 2847 ("if_deregister_com_alloc: %d free not registered", type)); 2848 if_com_alloc[type] = NULL; 2849 if_com_free[type] = NULL; 2850 } 2851