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