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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)if.c 8.3 (Berkeley) 1/4/94 34 * $FreeBSD$ 35 */ 36 37 #include "opt_compat.h" 38 #include "opt_inet6.h" 39 #include "opt_inet.h" 40 41 #include <sys/param.h> 42 #include <sys/malloc.h> 43 #include <sys/mbuf.h> 44 #include <sys/systm.h> 45 #include <sys/proc.h> 46 #include <sys/socket.h> 47 #include <sys/socketvar.h> 48 #include <sys/protosw.h> 49 #include <sys/kernel.h> 50 #include <sys/sockio.h> 51 #include <sys/syslog.h> 52 #include <sys/sysctl.h> 53 54 #include <net/if.h> 55 #include <net/if_dl.h> 56 #include <net/radix.h> 57 #include <net/route.h> 58 59 #if defined(INET) || defined(INET6) 60 /*XXX*/ 61 #include <netinet/in.h> 62 #include <netinet/in_var.h> 63 #ifdef INET6 64 #include <machine/clock.h> /* XXX: temporal workaround for fxp issue */ 65 #endif 66 #endif 67 68 /* 69 * System initialization 70 */ 71 72 static int ifconf __P((u_long, caddr_t)); 73 static void ifinit __P((void *)); 74 static void if_qflush __P((struct ifqueue *)); 75 static void if_slowtimo __P((void *)); 76 static void link_rtrequest __P((int, struct rtentry *, struct sockaddr *)); 77 static int if_rtdel __P((struct radix_node *, void *)); 78 79 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL) 80 81 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 82 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 83 84 int ifqmaxlen = IFQ_MAXLEN; 85 struct ifnethead ifnet; /* depend on static init XXX */ 86 87 #ifdef INET6 88 /* 89 * XXX: declare here to avoid to include many inet6 related files.. 90 * should be more generalized? 91 */ 92 extern void nd6_setmtu __P((struct ifnet *)); 93 #endif 94 95 /* 96 * Network interface utility routines. 97 * 98 * Routines with ifa_ifwith* names take sockaddr *'s as 99 * parameters. 100 */ 101 /* ARGSUSED*/ 102 void 103 ifinit(dummy) 104 void *dummy; 105 { 106 struct ifnet *ifp; 107 int s; 108 109 s = splimp(); 110 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) 111 if (ifp->if_snd.ifq_maxlen == 0) { 112 printf("%s%d XXX: driver didn't set ifq_maxlen\n", 113 ifp->if_name, ifp->if_unit); 114 ifp->if_snd.ifq_maxlen = ifqmaxlen; 115 } 116 splx(s); 117 if_slowtimo(0); 118 } 119 120 int if_index = 0; 121 struct ifaddr **ifnet_addrs; 122 struct ifnet **ifindex2ifnet = NULL; 123 124 125 /* 126 * Attach an interface to the 127 * list of "active" interfaces. 128 */ 129 void 130 if_attach(ifp) 131 struct ifnet *ifp; 132 { 133 unsigned socksize, ifasize; 134 int namelen, masklen; 135 char workbuf[64]; 136 register struct sockaddr_dl *sdl; 137 register struct ifaddr *ifa; 138 static int if_indexlim = 8; 139 static int inited; 140 141 if (!inited) { 142 TAILQ_INIT(&ifnet); 143 inited = 1; 144 } 145 146 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link); 147 ifp->if_index = ++if_index; 148 /* 149 * XXX - 150 * The old code would work if the interface passed a pre-existing 151 * chain of ifaddrs to this code. We don't trust our callers to 152 * properly initialize the tailq, however, so we no longer allow 153 * this unlikely case. 154 */ 155 TAILQ_INIT(&ifp->if_addrhead); 156 TAILQ_INIT(&ifp->if_prefixhead); 157 LIST_INIT(&ifp->if_multiaddrs); 158 getmicrotime(&ifp->if_lastchange); 159 if (ifnet_addrs == 0 || if_index >= if_indexlim) { 160 unsigned n = (if_indexlim <<= 1) * sizeof(ifa); 161 caddr_t q = malloc(n, M_IFADDR, M_WAITOK); 162 bzero(q, n); 163 if (ifnet_addrs) { 164 bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2); 165 free((caddr_t)ifnet_addrs, M_IFADDR); 166 } 167 ifnet_addrs = (struct ifaddr **)q; 168 169 /* grow ifindex2ifnet */ 170 n = if_indexlim * sizeof(struct ifnet *); 171 q = malloc(n, M_IFADDR, M_WAITOK); 172 bzero(q, n); 173 if (ifindex2ifnet) { 174 bcopy((caddr_t)ifindex2ifnet, q, n/2); 175 free((caddr_t)ifindex2ifnet, M_IFADDR); 176 } 177 ifindex2ifnet = (struct ifnet **)q; 178 } 179 180 ifindex2ifnet[if_index] = ifp; 181 182 /* 183 * create a Link Level name for this device 184 */ 185 namelen = snprintf(workbuf, sizeof(workbuf), 186 "%s%d", ifp->if_name, ifp->if_unit); 187 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m)) 188 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen; 189 socksize = masklen + ifp->if_addrlen; 190 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1))) 191 if (socksize < sizeof(*sdl)) 192 socksize = sizeof(*sdl); 193 socksize = ROUNDUP(socksize); 194 ifasize = sizeof(*ifa) + 2 * socksize; 195 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK); 196 if (ifa) { 197 bzero((caddr_t)ifa, ifasize); 198 sdl = (struct sockaddr_dl *)(ifa + 1); 199 sdl->sdl_len = socksize; 200 sdl->sdl_family = AF_LINK; 201 bcopy(workbuf, sdl->sdl_data, namelen); 202 sdl->sdl_nlen = namelen; 203 sdl->sdl_index = ifp->if_index; 204 sdl->sdl_type = ifp->if_type; 205 ifnet_addrs[if_index - 1] = ifa; 206 ifa->ifa_ifp = ifp; 207 ifa->ifa_rtrequest = link_rtrequest; 208 ifa->ifa_addr = (struct sockaddr *)sdl; 209 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 210 ifa->ifa_netmask = (struct sockaddr *)sdl; 211 sdl->sdl_len = masklen; 212 while (namelen != 0) 213 sdl->sdl_data[--namelen] = 0xff; 214 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 215 } 216 } 217 218 /* 219 * Detach an interface, removing it from the 220 * list of "active" interfaces. 221 */ 222 void 223 if_detach(ifp) 224 struct ifnet *ifp; 225 { 226 struct ifaddr *ifa; 227 struct radix_node_head *rnh; 228 int s; 229 int i; 230 231 /* 232 * Remove routes and flush queues. 233 */ 234 s = splnet(); 235 if_down(ifp); 236 237 /* 238 * Remove address from ifnet_addrs[] and maybe decrement if_index. 239 * Clean up all addresses. 240 */ 241 ifnet_addrs[ifp->if_index - 1] = 0; 242 while (if_index > 0 && ifnet_addrs[if_index - 1] == 0) 243 if_index--; 244 245 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 246 ifa = TAILQ_FIRST(&ifp->if_addrhead)) { 247 #ifdef INET 248 /* XXX: Ugly!! ad hoc just for INET */ 249 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { 250 struct ifaliasreq ifr; 251 252 bzero(&ifr, sizeof(ifr)); 253 ifr.ifra_addr = *ifa->ifa_addr; 254 if (ifa->ifa_dstaddr) 255 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 256 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 257 NULL) == 0) 258 continue; 259 } 260 #endif /* INET */ 261 #ifdef INET6 262 /* XXX: Ugly!! ad hoc just for INET6 */ 263 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) { 264 struct in6_aliasreq ifr; 265 266 bzero(&ifr, sizeof(ifr)); 267 ifr.ifra_addr = 268 *((struct sockaddr_in6 *)ifa->ifa_addr); 269 if (ifa->ifa_dstaddr) 270 ifr.ifra_dstaddr = 271 *((struct sockaddr_in6 *)ifa->ifa_dstaddr); 272 if (in6_control(NULL, SIOCDIFADDR_IN6, (caddr_t)&ifr, 273 ifp, NULL) == 0) 274 continue; 275 } 276 #endif /* INET6 */ 277 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 278 IFAFREE(ifa); 279 } 280 281 /* 282 * Delete all remaining routes using this interface 283 * Unfortuneatly the only way to do this is to slog through 284 * the entire routing table looking for routes which point 285 * to this interface...oh well... 286 */ 287 for (i = 1; i <= AF_MAX; i++) { 288 if ((rnh = rt_tables[i]) == NULL) 289 continue; 290 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 291 } 292 293 TAILQ_REMOVE(&ifnet, ifp, if_link); 294 splx(s); 295 } 296 297 /* 298 * Delete Routes for a Network Interface 299 * 300 * Called for each routing entry via the rnh->rnh_walktree() call above 301 * to delete all route entries referencing a detaching network interface. 302 * 303 * Arguments: 304 * rn pointer to node in the routing table 305 * arg argument passed to rnh->rnh_walktree() - detaching interface 306 * 307 * Returns: 308 * 0 successful 309 * errno failed - reason indicated 310 * 311 */ 312 static int 313 if_rtdel(rn, arg) 314 struct radix_node *rn; 315 void *arg; 316 { 317 struct rtentry *rt = (struct rtentry *)rn; 318 struct ifnet *ifp = arg; 319 int err; 320 321 if (rt->rt_ifp == ifp) { 322 323 /* 324 * Protect (sorta) against walktree recursion problems 325 * with cloned routes 326 */ 327 if ((rt->rt_flags & RTF_UP) == 0) 328 return (0); 329 330 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 331 rt_mask(rt), rt->rt_flags, 332 (struct rtentry **) NULL); 333 if (err) { 334 log(LOG_WARNING, "if_rtdel: error %d\n", err); 335 } 336 } 337 338 return (0); 339 } 340 341 /* 342 * Locate an interface based on a complete address. 343 */ 344 /*ARGSUSED*/ 345 struct ifaddr * 346 ifa_ifwithaddr(addr) 347 register struct sockaddr *addr; 348 { 349 register struct ifnet *ifp; 350 register struct ifaddr *ifa; 351 352 #define equal(a1, a2) \ 353 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0) 354 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) 355 for (ifa = ifp->if_addrhead.tqh_first; ifa; 356 ifa = ifa->ifa_link.tqe_next) { 357 if (ifa->ifa_addr->sa_family != addr->sa_family) 358 continue; 359 if (equal(addr, ifa->ifa_addr)) 360 return (ifa); 361 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr && 362 /* IP6 doesn't have broadcast */ 363 ifa->ifa_broadaddr->sa_len != 0 && 364 equal(ifa->ifa_broadaddr, addr)) 365 return (ifa); 366 } 367 return ((struct ifaddr *)0); 368 } 369 /* 370 * Locate the point to point interface with a given destination address. 371 */ 372 /*ARGSUSED*/ 373 struct ifaddr * 374 ifa_ifwithdstaddr(addr) 375 register struct sockaddr *addr; 376 { 377 register struct ifnet *ifp; 378 register struct ifaddr *ifa; 379 380 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) 381 if (ifp->if_flags & IFF_POINTOPOINT) 382 for (ifa = ifp->if_addrhead.tqh_first; ifa; 383 ifa = ifa->ifa_link.tqe_next) { 384 if (ifa->ifa_addr->sa_family != addr->sa_family) 385 continue; 386 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)) 387 return (ifa); 388 } 389 return ((struct ifaddr *)0); 390 } 391 392 /* 393 * Find an interface on a specific network. If many, choice 394 * is most specific found. 395 */ 396 struct ifaddr * 397 ifa_ifwithnet(addr) 398 struct sockaddr *addr; 399 { 400 register struct ifnet *ifp; 401 register struct ifaddr *ifa; 402 struct ifaddr *ifa_maybe = (struct ifaddr *) 0; 403 u_int af = addr->sa_family; 404 char *addr_data = addr->sa_data, *cplim; 405 406 /* 407 * AF_LINK addresses can be looked up directly by their index number, 408 * so do that if we can. 409 */ 410 if (af == AF_LINK) { 411 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 412 if (sdl->sdl_index && sdl->sdl_index <= if_index) 413 return (ifnet_addrs[sdl->sdl_index - 1]); 414 } 415 416 /* 417 * Scan though each interface, looking for ones that have 418 * addresses in this address family. 419 */ 420 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { 421 for (ifa = ifp->if_addrhead.tqh_first; ifa; 422 ifa = ifa->ifa_link.tqe_next) { 423 register char *cp, *cp2, *cp3; 424 425 if (ifa->ifa_addr->sa_family != af) 426 next: continue; 427 if ( 428 #ifdef INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */ 429 addr->sa_family != AF_INET6 && 430 #endif 431 ifp->if_flags & IFF_POINTOPOINT) { 432 /* 433 * This is a bit broken as it doesn't 434 * take into account that the remote end may 435 * be a single node in the network we are 436 * looking for. 437 * The trouble is that we don't know the 438 * netmask for the remote end. 439 */ 440 if (ifa->ifa_dstaddr != 0 441 && equal(addr, ifa->ifa_dstaddr)) 442 return (ifa); 443 } else { 444 /* 445 * if we have a special address handler, 446 * then use it instead of the generic one. 447 */ 448 if (ifa->ifa_claim_addr) { 449 if ((*ifa->ifa_claim_addr)(ifa, addr)) { 450 return (ifa); 451 } else { 452 continue; 453 } 454 } 455 456 /* 457 * Scan all the bits in the ifa's address. 458 * If a bit dissagrees with what we are 459 * looking for, mask it with the netmask 460 * to see if it really matters. 461 * (A byte at a time) 462 */ 463 if (ifa->ifa_netmask == 0) 464 continue; 465 cp = addr_data; 466 cp2 = ifa->ifa_addr->sa_data; 467 cp3 = ifa->ifa_netmask->sa_data; 468 cplim = ifa->ifa_netmask->sa_len 469 + (char *)ifa->ifa_netmask; 470 while (cp3 < cplim) 471 if ((*cp++ ^ *cp2++) & *cp3++) 472 goto next; /* next address! */ 473 /* 474 * If the netmask of what we just found 475 * is more specific than what we had before 476 * (if we had one) then remember the new one 477 * before continuing to search 478 * for an even better one. 479 */ 480 if (ifa_maybe == 0 || 481 rn_refines((caddr_t)ifa->ifa_netmask, 482 (caddr_t)ifa_maybe->ifa_netmask)) 483 ifa_maybe = ifa; 484 } 485 } 486 } 487 return (ifa_maybe); 488 } 489 490 /* 491 * Find an interface address specific to an interface best matching 492 * a given address. 493 */ 494 struct ifaddr * 495 ifaof_ifpforaddr(addr, ifp) 496 struct sockaddr *addr; 497 register struct ifnet *ifp; 498 { 499 register struct ifaddr *ifa; 500 register char *cp, *cp2, *cp3; 501 register char *cplim; 502 struct ifaddr *ifa_maybe = 0; 503 u_int af = addr->sa_family; 504 505 if (af >= AF_MAX) 506 return (0); 507 for (ifa = ifp->if_addrhead.tqh_first; ifa; 508 ifa = ifa->ifa_link.tqe_next) { 509 if (ifa->ifa_addr->sa_family != af) 510 continue; 511 if (ifa_maybe == 0) 512 ifa_maybe = ifa; 513 if (ifa->ifa_netmask == 0) { 514 if (equal(addr, ifa->ifa_addr) || 515 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))) 516 return (ifa); 517 continue; 518 } 519 if (ifp->if_flags & IFF_POINTOPOINT) { 520 if (equal(addr, ifa->ifa_dstaddr)) 521 return (ifa); 522 } else { 523 cp = addr->sa_data; 524 cp2 = ifa->ifa_addr->sa_data; 525 cp3 = ifa->ifa_netmask->sa_data; 526 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 527 for (; cp3 < cplim; cp3++) 528 if ((*cp++ ^ *cp2++) & *cp3) 529 break; 530 if (cp3 == cplim) 531 return (ifa); 532 } 533 } 534 return (ifa_maybe); 535 } 536 537 #include <net/route.h> 538 539 /* 540 * Default action when installing a route with a Link Level gateway. 541 * Lookup an appropriate real ifa to point to. 542 * This should be moved to /sys/net/link.c eventually. 543 */ 544 static void 545 link_rtrequest(cmd, rt, sa) 546 int cmd; 547 register struct rtentry *rt; 548 struct sockaddr *sa; 549 { 550 register struct ifaddr *ifa; 551 struct sockaddr *dst; 552 struct ifnet *ifp; 553 554 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 555 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 556 return; 557 ifa = ifaof_ifpforaddr(dst, ifp); 558 if (ifa) { 559 IFAFREE(rt->rt_ifa); 560 rt->rt_ifa = ifa; 561 ifa->ifa_refcnt++; 562 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 563 ifa->ifa_rtrequest(cmd, rt, sa); 564 } 565 } 566 567 /* 568 * Mark an interface down and notify protocols of 569 * the transition. 570 * NOTE: must be called at splnet or eqivalent. 571 */ 572 void 573 if_unroute(ifp, flag, fam) 574 register struct ifnet *ifp; 575 int flag, fam; 576 { 577 register struct ifaddr *ifa; 578 579 ifp->if_flags &= ~flag; 580 getmicrotime(&ifp->if_lastchange); 581 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 582 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 583 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 584 if_qflush(&ifp->if_snd); 585 rt_ifmsg(ifp); 586 } 587 588 /* 589 * Mark an interface up and notify protocols of 590 * the transition. 591 * NOTE: must be called at splnet or eqivalent. 592 */ 593 void 594 if_route(ifp, flag, fam) 595 register struct ifnet *ifp; 596 int flag, fam; 597 { 598 register struct ifaddr *ifa; 599 600 ifp->if_flags |= flag; 601 getmicrotime(&ifp->if_lastchange); 602 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 603 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 604 pfctlinput(PRC_IFUP, ifa->ifa_addr); 605 rt_ifmsg(ifp); 606 #ifdef INET6 607 in6_if_up(ifp); 608 #endif 609 } 610 611 /* 612 * Mark an interface down and notify protocols of 613 * the transition. 614 * NOTE: must be called at splnet or eqivalent. 615 */ 616 void 617 if_down(ifp) 618 register struct ifnet *ifp; 619 { 620 621 if_unroute(ifp, IFF_UP, AF_UNSPEC); 622 } 623 624 /* 625 * Mark an interface up and notify protocols of 626 * the transition. 627 * NOTE: must be called at splnet or eqivalent. 628 */ 629 void 630 if_up(ifp) 631 register struct ifnet *ifp; 632 { 633 634 if_route(ifp, IFF_UP, AF_UNSPEC); 635 } 636 637 /* 638 * Flush an interface queue. 639 */ 640 static void 641 if_qflush(ifq) 642 register struct ifqueue *ifq; 643 { 644 register struct mbuf *m, *n; 645 646 n = ifq->ifq_head; 647 while ((m = n) != 0) { 648 n = m->m_act; 649 m_freem(m); 650 } 651 ifq->ifq_head = 0; 652 ifq->ifq_tail = 0; 653 ifq->ifq_len = 0; 654 } 655 656 /* 657 * Handle interface watchdog timer routines. Called 658 * from softclock, we decrement timers (if set) and 659 * call the appropriate interface routine on expiration. 660 */ 661 static void 662 if_slowtimo(arg) 663 void *arg; 664 { 665 register struct ifnet *ifp; 666 int s = splimp(); 667 668 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { 669 if (ifp->if_timer == 0 || --ifp->if_timer) 670 continue; 671 if (ifp->if_watchdog) 672 (*ifp->if_watchdog)(ifp); 673 } 674 splx(s); 675 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 676 } 677 678 /* 679 * Map interface name to 680 * interface structure pointer. 681 */ 682 struct ifnet * 683 ifunit(char *name) 684 { 685 char namebuf[IFNAMSIZ + 1]; 686 char *cp; 687 struct ifnet *ifp; 688 int unit; 689 unsigned len, m; 690 char c; 691 692 len = strlen(name); 693 if (len < 2 || len > IFNAMSIZ) 694 return NULL; 695 cp = name + len - 1; 696 c = *cp; 697 if (c < '0' || c > '9') 698 return NULL; /* trailing garbage */ 699 unit = 0; 700 m = 1; 701 do { 702 if (cp == name) 703 return NULL; /* no interface name */ 704 unit += (c - '0') * m; 705 if (unit > 1000000) 706 return NULL; /* number is unreasonable */ 707 m *= 10; 708 c = *--cp; 709 } while (c >= '0' && c <= '9'); 710 len = cp - name + 1; 711 bcopy(name, namebuf, len); 712 namebuf[len] = '\0'; 713 /* 714 * Now search all the interfaces for this name/number 715 */ 716 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { 717 if (strcmp(ifp->if_name, namebuf)) 718 continue; 719 if (unit == ifp->if_unit) 720 break; 721 } 722 return (ifp); 723 } 724 725 726 /* 727 * Map interface name in a sockaddr_dl to 728 * interface structure pointer. 729 */ 730 struct ifnet * 731 if_withname(sa) 732 struct sockaddr *sa; 733 { 734 char ifname[IFNAMSIZ+1]; 735 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa; 736 737 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) || 738 (sdl->sdl_nlen > IFNAMSIZ) ) 739 return NULL; 740 741 /* 742 * ifunit wants a null-terminated name. It may not be null-terminated 743 * in the sockaddr. We don't want to change the caller's sockaddr, 744 * and there might not be room to put the trailing null anyway, so we 745 * make a local copy that we know we can null terminate safely. 746 */ 747 748 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen); 749 ifname[sdl->sdl_nlen] = '\0'; 750 return ifunit(ifname); 751 } 752 753 754 /* 755 * Interface ioctls. 756 */ 757 int 758 ifioctl(so, cmd, data, p) 759 struct socket *so; 760 u_long cmd; 761 caddr_t data; 762 struct proc *p; 763 { 764 register struct ifnet *ifp; 765 register struct ifreq *ifr; 766 struct ifstat *ifs; 767 int error; 768 short oif_flags; 769 770 switch (cmd) { 771 772 case SIOCGIFCONF: 773 case OSIOCGIFCONF: 774 return (ifconf(cmd, data)); 775 } 776 ifr = (struct ifreq *)data; 777 ifp = ifunit(ifr->ifr_name); 778 if (ifp == 0) 779 return (ENXIO); 780 switch (cmd) { 781 782 case SIOCGIFFLAGS: 783 ifr->ifr_flags = ifp->if_flags; 784 break; 785 786 case SIOCGIFMETRIC: 787 ifr->ifr_metric = ifp->if_metric; 788 break; 789 790 case SIOCGIFMTU: 791 ifr->ifr_mtu = ifp->if_mtu; 792 break; 793 794 case SIOCGIFPHYS: 795 ifr->ifr_phys = ifp->if_physical; 796 break; 797 798 case SIOCSIFFLAGS: 799 error = suser(p); 800 if (error) 801 return (error); 802 ifr->ifr_prevflags = ifp->if_flags; 803 if (ifp->if_flags & IFF_SMART) { 804 /* Smart drivers twiddle their own routes */ 805 } else if (ifp->if_flags & IFF_UP && 806 (ifr->ifr_flags & IFF_UP) == 0) { 807 int s = splimp(); 808 if_down(ifp); 809 splx(s); 810 } else if (ifr->ifr_flags & IFF_UP && 811 (ifp->if_flags & IFF_UP) == 0) { 812 int s = splimp(); 813 if_up(ifp); 814 splx(s); 815 } 816 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 817 (ifr->ifr_flags &~ IFF_CANTCHANGE); 818 if (ifp->if_ioctl) 819 (void) (*ifp->if_ioctl)(ifp, cmd, data); 820 getmicrotime(&ifp->if_lastchange); 821 break; 822 823 case SIOCSIFMETRIC: 824 error = suser(p); 825 if (error) 826 return (error); 827 ifp->if_metric = ifr->ifr_metric; 828 getmicrotime(&ifp->if_lastchange); 829 break; 830 831 case SIOCSIFPHYS: 832 error = suser(p); 833 if (error) 834 return error; 835 if (!ifp->if_ioctl) 836 return EOPNOTSUPP; 837 error = (*ifp->if_ioctl)(ifp, cmd, data); 838 if (error == 0) 839 getmicrotime(&ifp->if_lastchange); 840 return(error); 841 842 case SIOCSIFMTU: 843 { 844 u_long oldmtu = ifp->if_mtu; 845 846 error = suser(p); 847 if (error) 848 return (error); 849 if (ifp->if_ioctl == NULL) 850 return (EOPNOTSUPP); 851 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 852 return (EINVAL); 853 error = (*ifp->if_ioctl)(ifp, cmd, data); 854 if (error == 0) { 855 getmicrotime(&ifp->if_lastchange); 856 rt_ifmsg(ifp); 857 } 858 /* 859 * If the link MTU changed, do network layer specific procedure. 860 */ 861 if (ifp->if_mtu != oldmtu) { 862 #ifdef INET6 863 nd6_setmtu(ifp); 864 #endif 865 } 866 return (error); 867 } 868 869 case SIOCADDMULTI: 870 case SIOCDELMULTI: 871 error = suser(p); 872 if (error) 873 return (error); 874 875 /* Don't allow group membership on non-multicast interfaces. */ 876 if ((ifp->if_flags & IFF_MULTICAST) == 0) 877 return EOPNOTSUPP; 878 879 /* Don't let users screw up protocols' entries. */ 880 if (ifr->ifr_addr.sa_family != AF_LINK) 881 return EINVAL; 882 883 if (cmd == SIOCADDMULTI) { 884 struct ifmultiaddr *ifma; 885 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 886 } else { 887 error = if_delmulti(ifp, &ifr->ifr_addr); 888 } 889 if (error == 0) 890 getmicrotime(&ifp->if_lastchange); 891 return error; 892 893 case SIOCSIFMEDIA: 894 case SIOCSIFGENERIC: 895 error = suser(p); 896 if (error) 897 return (error); 898 if (ifp->if_ioctl == 0) 899 return (EOPNOTSUPP); 900 error = (*ifp->if_ioctl)(ifp, cmd, data); 901 if (error == 0) 902 getmicrotime(&ifp->if_lastchange); 903 return error; 904 905 case SIOCGIFSTATUS: 906 ifs = (struct ifstat *)data; 907 ifs->ascii[0] = '\0'; 908 909 case SIOCGIFMEDIA: 910 case SIOCGIFGENERIC: 911 if (ifp->if_ioctl == 0) 912 return (EOPNOTSUPP); 913 return ((*ifp->if_ioctl)(ifp, cmd, data)); 914 915 default: 916 oif_flags = ifp->if_flags; 917 if (so->so_proto == 0) 918 return (EOPNOTSUPP); 919 #ifndef COMPAT_43 920 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 921 data, 922 ifp, p)); 923 #else 924 { 925 int ocmd = cmd; 926 927 switch (cmd) { 928 929 case SIOCSIFDSTADDR: 930 case SIOCSIFADDR: 931 case SIOCSIFBRDADDR: 932 case SIOCSIFNETMASK: 933 #if BYTE_ORDER != BIG_ENDIAN 934 if (ifr->ifr_addr.sa_family == 0 && 935 ifr->ifr_addr.sa_len < 16) { 936 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 937 ifr->ifr_addr.sa_len = 16; 938 } 939 #else 940 if (ifr->ifr_addr.sa_len == 0) 941 ifr->ifr_addr.sa_len = 16; 942 #endif 943 break; 944 945 case OSIOCGIFADDR: 946 cmd = SIOCGIFADDR; 947 break; 948 949 case OSIOCGIFDSTADDR: 950 cmd = SIOCGIFDSTADDR; 951 break; 952 953 case OSIOCGIFBRDADDR: 954 cmd = SIOCGIFBRDADDR; 955 break; 956 957 case OSIOCGIFNETMASK: 958 cmd = SIOCGIFNETMASK; 959 } 960 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 961 cmd, 962 data, 963 ifp, p)); 964 switch (ocmd) { 965 966 case OSIOCGIFADDR: 967 case OSIOCGIFDSTADDR: 968 case OSIOCGIFBRDADDR: 969 case OSIOCGIFNETMASK: 970 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 971 972 } 973 } 974 #endif /* COMPAT_43 */ 975 976 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 977 #ifdef INET6 978 DELAY(100);/* XXX: temporal workaround for fxp issue*/ 979 if (ifp->if_flags & IFF_UP) { 980 int s = splimp(); 981 in6_if_up(ifp); 982 splx(s); 983 } 984 #endif 985 } 986 return (error); 987 988 } 989 return (0); 990 } 991 992 /* 993 * Set/clear promiscuous mode on interface ifp based on the truth value 994 * of pswitch. The calls are reference counted so that only the first 995 * "on" request actually has an effect, as does the final "off" request. 996 * Results are undefined if the "off" and "on" requests are not matched. 997 */ 998 int 999 ifpromisc(ifp, pswitch) 1000 struct ifnet *ifp; 1001 int pswitch; 1002 { 1003 struct ifreq ifr; 1004 int error; 1005 1006 if (pswitch) { 1007 /* 1008 * If the device is not configured up, we cannot put it in 1009 * promiscuous mode. 1010 */ 1011 if ((ifp->if_flags & IFF_UP) == 0) 1012 return (ENETDOWN); 1013 if (ifp->if_pcount++ != 0) 1014 return (0); 1015 ifp->if_flags |= IFF_PROMISC; 1016 log(LOG_INFO, "%s%d: promiscuous mode enabled\n", 1017 ifp->if_name, ifp->if_unit); 1018 } else { 1019 if (--ifp->if_pcount > 0) 1020 return (0); 1021 ifp->if_flags &= ~IFF_PROMISC; 1022 log(LOG_INFO, "%s%d: promiscuous mode disabled\n", 1023 ifp->if_name, ifp->if_unit); 1024 } 1025 ifr.ifr_flags = ifp->if_flags; 1026 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1027 if (error == 0) 1028 rt_ifmsg(ifp); 1029 return error; 1030 } 1031 1032 /* 1033 * Return interface configuration 1034 * of system. List may be used 1035 * in later ioctl's (above) to get 1036 * other information. 1037 */ 1038 /*ARGSUSED*/ 1039 static int 1040 ifconf(cmd, data) 1041 u_long cmd; 1042 caddr_t data; 1043 { 1044 register struct ifconf *ifc = (struct ifconf *)data; 1045 register struct ifnet *ifp = ifnet.tqh_first; 1046 register struct ifaddr *ifa; 1047 struct ifreq ifr, *ifrp; 1048 int space = ifc->ifc_len, error = 0; 1049 1050 ifrp = ifc->ifc_req; 1051 for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) { 1052 char workbuf[64]; 1053 int ifnlen, addrs; 1054 1055 ifnlen = snprintf(workbuf, sizeof(workbuf), 1056 "%s%d", ifp->if_name, ifp->if_unit); 1057 if(ifnlen + 1 > sizeof ifr.ifr_name) { 1058 error = ENAMETOOLONG; 1059 break; 1060 } else { 1061 strcpy(ifr.ifr_name, workbuf); 1062 } 1063 1064 addrs = 0; 1065 ifa = ifp->if_addrhead.tqh_first; 1066 for ( ; space > sizeof (ifr) && ifa; 1067 ifa = ifa->ifa_link.tqe_next) { 1068 register struct sockaddr *sa = ifa->ifa_addr; 1069 if (curproc->p_prison && prison_if(curproc, sa)) 1070 continue; 1071 addrs++; 1072 #ifdef COMPAT_43 1073 if (cmd == OSIOCGIFCONF) { 1074 struct osockaddr *osa = 1075 (struct osockaddr *)&ifr.ifr_addr; 1076 ifr.ifr_addr = *sa; 1077 osa->sa_family = sa->sa_family; 1078 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1079 sizeof (ifr)); 1080 ifrp++; 1081 } else 1082 #endif 1083 if (sa->sa_len <= sizeof(*sa)) { 1084 ifr.ifr_addr = *sa; 1085 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1086 sizeof (ifr)); 1087 ifrp++; 1088 } else { 1089 if (space < sa->sa_len - sizeof(*sa)) 1090 break; 1091 space -= sa->sa_len - sizeof(*sa); 1092 if (space < sizeof (ifr)) 1093 break; 1094 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1095 sizeof (ifr.ifr_name)); 1096 if (error == 0) 1097 error = copyout((caddr_t)sa, 1098 (caddr_t)&ifrp->ifr_addr, sa->sa_len); 1099 ifrp = (struct ifreq *) 1100 (sa->sa_len + (caddr_t)&ifrp->ifr_addr); 1101 } 1102 if (error) 1103 break; 1104 space -= sizeof (ifr); 1105 } 1106 if (error) 1107 break; 1108 if (!addrs) { 1109 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 1110 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1111 sizeof (ifr)); 1112 if (error) 1113 break; 1114 space -= sizeof (ifr); 1115 ifrp++; 1116 } 1117 } 1118 if (space < 0) 1119 panic("ifconf: space < 0"); 1120 ifc->ifc_len -= space; 1121 return (error); 1122 } 1123 1124 /* 1125 * Just like if_promisc(), but for all-multicast-reception mode. 1126 */ 1127 int 1128 if_allmulti(ifp, onswitch) 1129 struct ifnet *ifp; 1130 int onswitch; 1131 { 1132 int error = 0; 1133 int s = splimp(); 1134 1135 if (onswitch) { 1136 if (ifp->if_amcount++ == 0) { 1137 ifp->if_flags |= IFF_ALLMULTI; 1138 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0); 1139 } 1140 } else { 1141 if (ifp->if_amcount > 1) { 1142 ifp->if_amcount--; 1143 } else { 1144 ifp->if_amcount = 0; 1145 ifp->if_flags &= ~IFF_ALLMULTI; 1146 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0); 1147 } 1148 } 1149 splx(s); 1150 1151 if (error == 0) 1152 rt_ifmsg(ifp); 1153 return error; 1154 } 1155 1156 /* 1157 * Add a multicast listenership to the interface in question. 1158 * The link layer provides a routine which converts 1159 */ 1160 int 1161 if_addmulti(ifp, sa, retifma) 1162 struct ifnet *ifp; /* interface to manipulate */ 1163 struct sockaddr *sa; /* address to add */ 1164 struct ifmultiaddr **retifma; 1165 { 1166 struct sockaddr *llsa, *dupsa; 1167 int error, s; 1168 struct ifmultiaddr *ifma; 1169 1170 /* 1171 * If the matching multicast address already exists 1172 * then don't add a new one, just add a reference 1173 */ 1174 for (ifma = ifp->if_multiaddrs.lh_first; ifma; 1175 ifma = ifma->ifma_link.le_next) { 1176 if (equal(sa, ifma->ifma_addr)) { 1177 ifma->ifma_refcount++; 1178 if (retifma) 1179 *retifma = ifma; 1180 return 0; 1181 } 1182 } 1183 1184 /* 1185 * Give the link layer a chance to accept/reject it, and also 1186 * find out which AF_LINK address this maps to, if it isn't one 1187 * already. 1188 */ 1189 if (ifp->if_resolvemulti) { 1190 error = ifp->if_resolvemulti(ifp, &llsa, sa); 1191 if (error) return error; 1192 } else { 1193 llsa = 0; 1194 } 1195 1196 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK); 1197 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK); 1198 bcopy(sa, dupsa, sa->sa_len); 1199 1200 ifma->ifma_addr = dupsa; 1201 ifma->ifma_lladdr = llsa; 1202 ifma->ifma_ifp = ifp; 1203 ifma->ifma_refcount = 1; 1204 ifma->ifma_protospec = 0; 1205 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 1206 1207 /* 1208 * Some network interfaces can scan the address list at 1209 * interrupt time; lock them out. 1210 */ 1211 s = splimp(); 1212 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 1213 splx(s); 1214 *retifma = ifma; 1215 1216 if (llsa != 0) { 1217 for (ifma = ifp->if_multiaddrs.lh_first; ifma; 1218 ifma = ifma->ifma_link.le_next) { 1219 if (equal(ifma->ifma_addr, llsa)) 1220 break; 1221 } 1222 if (ifma) { 1223 ifma->ifma_refcount++; 1224 } else { 1225 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, 1226 M_IFMADDR, M_WAITOK); 1227 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, 1228 M_IFMADDR, M_WAITOK); 1229 bcopy(llsa, dupsa, llsa->sa_len); 1230 ifma->ifma_addr = dupsa; 1231 ifma->ifma_ifp = ifp; 1232 ifma->ifma_refcount = 1; 1233 s = splimp(); 1234 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 1235 splx(s); 1236 } 1237 } 1238 /* 1239 * We are certain we have added something, so call down to the 1240 * interface to let them know about it. 1241 */ 1242 s = splimp(); 1243 ifp->if_ioctl(ifp, SIOCADDMULTI, 0); 1244 splx(s); 1245 1246 return 0; 1247 } 1248 1249 /* 1250 * Remove a reference to a multicast address on this interface. Yell 1251 * if the request does not match an existing membership. 1252 */ 1253 int 1254 if_delmulti(ifp, sa) 1255 struct ifnet *ifp; 1256 struct sockaddr *sa; 1257 { 1258 struct ifmultiaddr *ifma; 1259 int s; 1260 1261 for (ifma = ifp->if_multiaddrs.lh_first; ifma; 1262 ifma = ifma->ifma_link.le_next) 1263 if (equal(sa, ifma->ifma_addr)) 1264 break; 1265 if (ifma == 0) 1266 return ENOENT; 1267 1268 if (ifma->ifma_refcount > 1) { 1269 ifma->ifma_refcount--; 1270 return 0; 1271 } 1272 1273 rt_newmaddrmsg(RTM_DELMADDR, ifma); 1274 sa = ifma->ifma_lladdr; 1275 s = splimp(); 1276 LIST_REMOVE(ifma, ifma_link); 1277 splx(s); 1278 free(ifma->ifma_addr, M_IFMADDR); 1279 free(ifma, M_IFMADDR); 1280 if (sa == 0) 1281 return 0; 1282 1283 /* 1284 * Now look for the link-layer address which corresponds to 1285 * this network address. It had been squirreled away in 1286 * ifma->ifma_lladdr for this purpose (so we don't have 1287 * to call ifp->if_resolvemulti() again), and we saved that 1288 * value in sa above. If some nasty deleted the 1289 * link-layer address out from underneath us, we can deal because 1290 * the address we stored was is not the same as the one which was 1291 * in the record for the link-layer address. (So we don't complain 1292 * in that case.) 1293 */ 1294 for (ifma = ifp->if_multiaddrs.lh_first; ifma; 1295 ifma = ifma->ifma_link.le_next) 1296 if (equal(sa, ifma->ifma_addr)) 1297 break; 1298 if (ifma == 0) 1299 return 0; 1300 1301 if (ifma->ifma_refcount > 1) { 1302 ifma->ifma_refcount--; 1303 return 0; 1304 } 1305 1306 s = splimp(); 1307 LIST_REMOVE(ifma, ifma_link); 1308 ifp->if_ioctl(ifp, SIOCDELMULTI, 0); 1309 splx(s); 1310 free(ifma->ifma_addr, M_IFMADDR); 1311 free(sa, M_IFMADDR); 1312 free(ifma, M_IFMADDR); 1313 1314 return 0; 1315 } 1316 1317 struct ifmultiaddr * 1318 ifmaof_ifpforaddr(sa, ifp) 1319 struct sockaddr *sa; 1320 struct ifnet *ifp; 1321 { 1322 struct ifmultiaddr *ifma; 1323 1324 for (ifma = ifp->if_multiaddrs.lh_first; ifma; 1325 ifma = ifma->ifma_link.le_next) 1326 if (equal(ifma->ifma_addr, sa)) 1327 break; 1328 1329 return ifma; 1330 } 1331 1332 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 1333 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 1334