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