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