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 * $Id: if.c,v 1.73 1999/06/19 18:42:26 phk Exp $ 35 */ 36 37 #include "opt_compat.h" 38 39 #include <sys/param.h> 40 #include <sys/malloc.h> 41 #include <sys/mbuf.h> 42 #include <sys/systm.h> 43 #include <sys/proc.h> 44 #include <sys/socket.h> 45 #include <sys/socketvar.h> 46 #include <sys/protosw.h> 47 #include <sys/kernel.h> 48 #include <sys/sockio.h> 49 #include <sys/syslog.h> 50 #include <sys/sysctl.h> 51 52 #include <net/if.h> 53 #include <net/if_dl.h> 54 #include <net/radix.h> 55 56 /* 57 * System initialization 58 */ 59 60 static int ifconf __P((u_long, caddr_t)); 61 static void ifinit __P((void *)); 62 static void if_qflush __P((struct ifqueue *)); 63 static void if_slowtimo __P((void *)); 64 static void link_rtrequest __P((int, struct rtentry *, struct sockaddr *)); 65 66 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL) 67 68 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 69 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 70 71 int ifqmaxlen = IFQ_MAXLEN; 72 struct ifnethead ifnet; /* depend on static init XXX */ 73 74 /* 75 * Network interface utility routines. 76 * 77 * Routines with ifa_ifwith* names take sockaddr *'s as 78 * parameters. 79 */ 80 /* ARGSUSED*/ 81 void 82 ifinit(dummy) 83 void *dummy; 84 { 85 struct ifnet *ifp; 86 int s; 87 88 s = splimp(); 89 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) 90 if (ifp->if_snd.ifq_maxlen == 0) { 91 printf("%s%d XXX: driver didn't set ifq_maxlen\n", 92 ifp->if_name, ifp->if_unit); 93 ifp->if_snd.ifq_maxlen = ifqmaxlen; 94 } 95 splx(s); 96 if_slowtimo(0); 97 } 98 99 int if_index = 0; 100 struct ifaddr **ifnet_addrs; 101 102 103 /* 104 * Attach an interface to the 105 * list of "active" interfaces. 106 */ 107 void 108 if_attach(ifp) 109 struct ifnet *ifp; 110 { 111 unsigned socksize, ifasize; 112 int namelen, masklen; 113 char workbuf[64]; 114 register struct sockaddr_dl *sdl; 115 register struct ifaddr *ifa; 116 static int if_indexlim = 8; 117 static int inited; 118 119 if (!inited) { 120 TAILQ_INIT(&ifnet); 121 inited = 1; 122 } 123 124 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link); 125 ifp->if_index = ++if_index; 126 /* 127 * XXX - 128 * The old code would work if the interface passed a pre-existing 129 * chain of ifaddrs to this code. We don't trust our callers to 130 * properly initialize the tailq, however, so we no longer allow 131 * this unlikely case. 132 */ 133 TAILQ_INIT(&ifp->if_addrhead); 134 LIST_INIT(&ifp->if_multiaddrs); 135 getmicrotime(&ifp->if_lastchange); 136 if (ifnet_addrs == 0 || if_index >= if_indexlim) { 137 unsigned n = (if_indexlim <<= 1) * sizeof(ifa); 138 struct ifaddr **q = (struct ifaddr **) 139 malloc(n, M_IFADDR, M_WAITOK); 140 bzero((caddr_t)q, n); 141 if (ifnet_addrs) { 142 bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2); 143 free((caddr_t)ifnet_addrs, M_IFADDR); 144 } 145 ifnet_addrs = q; 146 } 147 /* 148 * create a Link Level name for this device 149 */ 150 namelen = snprintf(workbuf, sizeof(workbuf), 151 "%s%d", ifp->if_name, ifp->if_unit); 152 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m)) 153 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen; 154 socksize = masklen + ifp->if_addrlen; 155 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1))) 156 if (socksize < sizeof(*sdl)) 157 socksize = sizeof(*sdl); 158 socksize = ROUNDUP(socksize); 159 ifasize = sizeof(*ifa) + 2 * socksize; 160 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK); 161 if (ifa) { 162 bzero((caddr_t)ifa, ifasize); 163 sdl = (struct sockaddr_dl *)(ifa + 1); 164 sdl->sdl_len = socksize; 165 sdl->sdl_family = AF_LINK; 166 bcopy(workbuf, sdl->sdl_data, namelen); 167 sdl->sdl_nlen = namelen; 168 sdl->sdl_index = ifp->if_index; 169 sdl->sdl_type = ifp->if_type; 170 ifnet_addrs[if_index - 1] = ifa; 171 ifa->ifa_ifp = ifp; 172 ifa->ifa_rtrequest = link_rtrequest; 173 ifa->ifa_addr = (struct sockaddr *)sdl; 174 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 175 ifa->ifa_netmask = (struct sockaddr *)sdl; 176 sdl->sdl_len = masklen; 177 while (namelen != 0) 178 sdl->sdl_data[--namelen] = 0xff; 179 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 180 } 181 } 182 183 /* 184 * Detach an interface, removing it from the 185 * list of "active" interfaces. 186 */ 187 void 188 if_detach(ifp) 189 struct ifnet *ifp; 190 { 191 struct ifaddr *ifa; 192 193 /* 194 * Remove routes and flush queues. 195 */ 196 if_down(ifp); 197 198 /* 199 * Remove address from ifnet_addrs[] and maybe decrement if_index. 200 * Clean up all addresses. 201 */ 202 ifnet_addrs[ifp->if_index] = 0; 203 while (ifnet_addrs[if_index] == 0) 204 if_index--; 205 206 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 207 ifa = TAILQ_FIRST(&ifp->if_addrhead)) { 208 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 209 IFAFREE(ifa); 210 } 211 212 TAILQ_REMOVE(&ifnet, ifp, if_link); 213 } 214 215 /* 216 * Locate an interface based on a complete address. 217 */ 218 /*ARGSUSED*/ 219 struct ifaddr * 220 ifa_ifwithaddr(addr) 221 register struct sockaddr *addr; 222 { 223 register struct ifnet *ifp; 224 register struct ifaddr *ifa; 225 226 #define equal(a1, a2) \ 227 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0) 228 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) 229 for (ifa = ifp->if_addrhead.tqh_first; ifa; 230 ifa = ifa->ifa_link.tqe_next) { 231 if (ifa->ifa_addr->sa_family != addr->sa_family) 232 continue; 233 if (equal(addr, ifa->ifa_addr)) 234 return (ifa); 235 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr && 236 equal(ifa->ifa_broadaddr, addr)) 237 return (ifa); 238 } 239 return ((struct ifaddr *)0); 240 } 241 /* 242 * Locate the point to point interface with a given destination address. 243 */ 244 /*ARGSUSED*/ 245 struct ifaddr * 246 ifa_ifwithdstaddr(addr) 247 register struct sockaddr *addr; 248 { 249 register struct ifnet *ifp; 250 register struct ifaddr *ifa; 251 252 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) 253 if (ifp->if_flags & IFF_POINTOPOINT) 254 for (ifa = ifp->if_addrhead.tqh_first; ifa; 255 ifa = ifa->ifa_link.tqe_next) { 256 if (ifa->ifa_addr->sa_family != addr->sa_family) 257 continue; 258 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)) 259 return (ifa); 260 } 261 return ((struct ifaddr *)0); 262 } 263 264 /* 265 * Find an interface on a specific network. If many, choice 266 * is most specific found. 267 */ 268 struct ifaddr * 269 ifa_ifwithnet(addr) 270 struct sockaddr *addr; 271 { 272 register struct ifnet *ifp; 273 register struct ifaddr *ifa; 274 struct ifaddr *ifa_maybe = (struct ifaddr *) 0; 275 u_int af = addr->sa_family; 276 char *addr_data = addr->sa_data, *cplim; 277 278 /* 279 * AF_LINK addresses can be looked up directly by their index number, 280 * so do that if we can. 281 */ 282 if (af == AF_LINK) { 283 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 284 if (sdl->sdl_index && sdl->sdl_index <= if_index) 285 return (ifnet_addrs[sdl->sdl_index - 1]); 286 } 287 288 /* 289 * Scan though each interface, looking for ones that have 290 * addresses in this address family. 291 */ 292 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { 293 for (ifa = ifp->if_addrhead.tqh_first; ifa; 294 ifa = ifa->ifa_link.tqe_next) { 295 register char *cp, *cp2, *cp3; 296 297 if (ifa->ifa_addr->sa_family != af) 298 next: continue; 299 if (ifp->if_flags & IFF_POINTOPOINT) { 300 /* 301 * This is a bit broken as it doesn't 302 * take into account that the remote end may 303 * be a single node in the network we are 304 * looking for. 305 * The trouble is that we don't know the 306 * netmask for the remote end. 307 */ 308 if (ifa->ifa_dstaddr != 0 309 && equal(addr, ifa->ifa_dstaddr)) 310 return (ifa); 311 } else { 312 /* 313 * if we have a special address handler, 314 * then use it instead of the generic one. 315 */ 316 if (ifa->ifa_claim_addr) { 317 if ((*ifa->ifa_claim_addr)(ifa, addr)) { 318 return (ifa); 319 } else { 320 continue; 321 } 322 } 323 324 /* 325 * Scan all the bits in the ifa's address. 326 * If a bit dissagrees with what we are 327 * looking for, mask it with the netmask 328 * to see if it really matters. 329 * (A byte at a time) 330 */ 331 if (ifa->ifa_netmask == 0) 332 continue; 333 cp = addr_data; 334 cp2 = ifa->ifa_addr->sa_data; 335 cp3 = ifa->ifa_netmask->sa_data; 336 cplim = ifa->ifa_netmask->sa_len 337 + (char *)ifa->ifa_netmask; 338 while (cp3 < cplim) 339 if ((*cp++ ^ *cp2++) & *cp3++) 340 goto next; /* next address! */ 341 /* 342 * If the netmask of what we just found 343 * is more specific than what we had before 344 * (if we had one) then remember the new one 345 * before continuing to search 346 * for an even better one. 347 */ 348 if (ifa_maybe == 0 || 349 rn_refines((caddr_t)ifa->ifa_netmask, 350 (caddr_t)ifa_maybe->ifa_netmask)) 351 ifa_maybe = ifa; 352 } 353 } 354 } 355 return (ifa_maybe); 356 } 357 358 /* 359 * Find an interface address specific to an interface best matching 360 * a given address. 361 */ 362 struct ifaddr * 363 ifaof_ifpforaddr(addr, ifp) 364 struct sockaddr *addr; 365 register struct ifnet *ifp; 366 { 367 register struct ifaddr *ifa; 368 register char *cp, *cp2, *cp3; 369 register char *cplim; 370 struct ifaddr *ifa_maybe = 0; 371 u_int af = addr->sa_family; 372 373 if (af >= AF_MAX) 374 return (0); 375 for (ifa = ifp->if_addrhead.tqh_first; ifa; 376 ifa = ifa->ifa_link.tqe_next) { 377 if (ifa->ifa_addr->sa_family != af) 378 continue; 379 if (ifa_maybe == 0) 380 ifa_maybe = ifa; 381 if (ifa->ifa_netmask == 0) { 382 if (equal(addr, ifa->ifa_addr) || 383 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))) 384 return (ifa); 385 continue; 386 } 387 if (ifp->if_flags & IFF_POINTOPOINT) { 388 if (equal(addr, ifa->ifa_dstaddr)) 389 return (ifa); 390 } else { 391 cp = addr->sa_data; 392 cp2 = ifa->ifa_addr->sa_data; 393 cp3 = ifa->ifa_netmask->sa_data; 394 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 395 for (; cp3 < cplim; cp3++) 396 if ((*cp++ ^ *cp2++) & *cp3) 397 break; 398 if (cp3 == cplim) 399 return (ifa); 400 } 401 } 402 return (ifa_maybe); 403 } 404 405 #include <net/route.h> 406 407 /* 408 * Default action when installing a route with a Link Level gateway. 409 * Lookup an appropriate real ifa to point to. 410 * This should be moved to /sys/net/link.c eventually. 411 */ 412 static void 413 link_rtrequest(cmd, rt, sa) 414 int cmd; 415 register struct rtentry *rt; 416 struct sockaddr *sa; 417 { 418 register struct ifaddr *ifa; 419 struct sockaddr *dst; 420 struct ifnet *ifp; 421 422 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 423 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 424 return; 425 ifa = ifaof_ifpforaddr(dst, ifp); 426 if (ifa) { 427 IFAFREE(rt->rt_ifa); 428 rt->rt_ifa = ifa; 429 ifa->ifa_refcnt++; 430 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 431 ifa->ifa_rtrequest(cmd, rt, sa); 432 } 433 } 434 435 /* 436 * Mark an interface down and notify protocols of 437 * the transition. 438 * NOTE: must be called at splnet or eqivalent. 439 */ 440 void 441 if_unroute(ifp, flag, fam) 442 register struct ifnet *ifp; 443 int flag, fam; 444 { 445 register struct ifaddr *ifa; 446 447 ifp->if_flags &= ~flag; 448 getmicrotime(&ifp->if_lastchange); 449 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 450 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 451 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 452 if_qflush(&ifp->if_snd); 453 rt_ifmsg(ifp); 454 } 455 456 /* 457 * Mark an interface up and notify protocols of 458 * the transition. 459 * NOTE: must be called at splnet or eqivalent. 460 */ 461 void 462 if_route(ifp, flag, fam) 463 register struct ifnet *ifp; 464 int flag, fam; 465 { 466 register struct ifaddr *ifa; 467 468 ifp->if_flags |= flag; 469 getmicrotime(&ifp->if_lastchange); 470 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 471 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 472 pfctlinput(PRC_IFUP, ifa->ifa_addr); 473 rt_ifmsg(ifp); 474 } 475 476 /* 477 * Mark an interface down and notify protocols of 478 * the transition. 479 * NOTE: must be called at splnet or eqivalent. 480 */ 481 void 482 if_down(ifp) 483 register struct ifnet *ifp; 484 { 485 486 if_unroute(ifp, IFF_UP, AF_UNSPEC); 487 } 488 489 /* 490 * Mark an interface up and notify protocols of 491 * the transition. 492 * NOTE: must be called at splnet or eqivalent. 493 */ 494 void 495 if_up(ifp) 496 register struct ifnet *ifp; 497 { 498 499 if_route(ifp, IFF_UP, AF_UNSPEC); 500 } 501 502 /* 503 * Flush an interface queue. 504 */ 505 static void 506 if_qflush(ifq) 507 register struct ifqueue *ifq; 508 { 509 register struct mbuf *m, *n; 510 511 n = ifq->ifq_head; 512 while ((m = n) != 0) { 513 n = m->m_act; 514 m_freem(m); 515 } 516 ifq->ifq_head = 0; 517 ifq->ifq_tail = 0; 518 ifq->ifq_len = 0; 519 } 520 521 /* 522 * Handle interface watchdog timer routines. Called 523 * from softclock, we decrement timers (if set) and 524 * call the appropriate interface routine on expiration. 525 */ 526 static void 527 if_slowtimo(arg) 528 void *arg; 529 { 530 register struct ifnet *ifp; 531 int s = splimp(); 532 533 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { 534 if (ifp->if_timer == 0 || --ifp->if_timer) 535 continue; 536 if (ifp->if_watchdog) 537 (*ifp->if_watchdog)(ifp); 538 } 539 splx(s); 540 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 541 } 542 543 /* 544 * Map interface name to 545 * interface structure pointer. 546 */ 547 struct ifnet * 548 ifunit(name) 549 register char *name; 550 { 551 char namebuf[IFNAMSIZ + 1]; 552 register char *cp, *cp2; 553 char *end; 554 register struct ifnet *ifp; 555 int unit; 556 unsigned len; 557 register char c = '\0'; 558 559 /* 560 * Look for a non numeric part 561 */ 562 end = name + IFNAMSIZ; 563 cp2 = namebuf; 564 cp = name; 565 while ((cp < end) && (c = *cp)) { 566 if (c >= '0' && c <= '9') 567 break; 568 *cp2++ = c; 569 cp++; 570 } 571 if ((cp == end) || (c == '\0') || (cp == name)) 572 return ((struct ifnet *)0); 573 *cp2 = '\0'; 574 /* 575 * check we have a legal number (limit to 7 digits?) 576 */ 577 len = cp - name + 1; 578 for (unit = 0; 579 ((c = *cp) >= '0') && (c <= '9') && (unit < 1000000); cp++ ) 580 unit = (unit * 10) + (c - '0'); 581 if (*cp != '\0') 582 return 0; /* no trailing garbage allowed */ 583 /* 584 * Now search all the interfaces for this name/number 585 */ 586 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { 587 if (bcmp(ifp->if_name, namebuf, len)) 588 continue; 589 if (unit == ifp->if_unit) 590 break; 591 } 592 return (ifp); 593 } 594 595 /* 596 * Interface ioctls. 597 */ 598 int 599 ifioctl(so, cmd, data, p) 600 struct socket *so; 601 u_long cmd; 602 caddr_t data; 603 struct proc *p; 604 { 605 register struct ifnet *ifp; 606 register struct ifreq *ifr; 607 struct ifstat *ifs; 608 int error; 609 610 switch (cmd) { 611 612 case SIOCGIFCONF: 613 case OSIOCGIFCONF: 614 return (ifconf(cmd, data)); 615 } 616 ifr = (struct ifreq *)data; 617 ifp = ifunit(ifr->ifr_name); 618 if (ifp == 0) 619 return (ENXIO); 620 switch (cmd) { 621 622 case SIOCGIFFLAGS: 623 ifr->ifr_flags = ifp->if_flags; 624 break; 625 626 case SIOCGIFMETRIC: 627 ifr->ifr_metric = ifp->if_metric; 628 break; 629 630 case SIOCGIFMTU: 631 ifr->ifr_mtu = ifp->if_mtu; 632 break; 633 634 case SIOCGIFPHYS: 635 ifr->ifr_phys = ifp->if_physical; 636 break; 637 638 case SIOCSIFFLAGS: 639 error = suser(p); 640 if (error) 641 return (error); 642 ifr->ifr_prevflags = ifp->if_flags; 643 if (ifp->if_flags & IFF_SMART) { 644 /* Smart drivers twiddle their own routes */ 645 } else if (ifp->if_flags & IFF_UP && 646 (ifr->ifr_flags & IFF_UP) == 0) { 647 int s = splimp(); 648 if_down(ifp); 649 splx(s); 650 } else if (ifr->ifr_flags & IFF_UP && 651 (ifp->if_flags & IFF_UP) == 0) { 652 int s = splimp(); 653 if_up(ifp); 654 splx(s); 655 } 656 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 657 (ifr->ifr_flags &~ IFF_CANTCHANGE); 658 if (ifp->if_ioctl) 659 (void) (*ifp->if_ioctl)(ifp, cmd, data); 660 getmicrotime(&ifp->if_lastchange); 661 break; 662 663 case SIOCSIFMETRIC: 664 error = suser(p); 665 if (error) 666 return (error); 667 ifp->if_metric = ifr->ifr_metric; 668 getmicrotime(&ifp->if_lastchange); 669 break; 670 671 case SIOCSIFPHYS: 672 error = suser(p); 673 if (error) 674 return error; 675 if (!ifp->if_ioctl) 676 return EOPNOTSUPP; 677 error = (*ifp->if_ioctl)(ifp, cmd, data); 678 if (error == 0) 679 getmicrotime(&ifp->if_lastchange); 680 return(error); 681 682 case SIOCSIFMTU: 683 error = suser(p); 684 if (error) 685 return (error); 686 if (ifp->if_ioctl == NULL) 687 return (EOPNOTSUPP); 688 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 689 return (EINVAL); 690 error = (*ifp->if_ioctl)(ifp, cmd, data); 691 if (error == 0) 692 getmicrotime(&ifp->if_lastchange); 693 return(error); 694 695 case SIOCADDMULTI: 696 case SIOCDELMULTI: 697 error = suser(p); 698 if (error) 699 return (error); 700 701 /* Don't allow group membership on non-multicast interfaces. */ 702 if ((ifp->if_flags & IFF_MULTICAST) == 0) 703 return EOPNOTSUPP; 704 705 /* Don't let users screw up protocols' entries. */ 706 if (ifr->ifr_addr.sa_family != AF_LINK) 707 return EINVAL; 708 709 if (cmd == SIOCADDMULTI) { 710 struct ifmultiaddr *ifma; 711 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 712 } else { 713 error = if_delmulti(ifp, &ifr->ifr_addr); 714 } 715 if (error == 0) 716 getmicrotime(&ifp->if_lastchange); 717 return error; 718 719 case SIOCSIFMEDIA: 720 case SIOCSIFGENERIC: 721 error = suser(p); 722 if (error) 723 return (error); 724 if (ifp->if_ioctl == 0) 725 return (EOPNOTSUPP); 726 error = (*ifp->if_ioctl)(ifp, cmd, data); 727 if (error == 0) 728 getmicrotime(&ifp->if_lastchange); 729 return error; 730 731 case SIOCGIFSTATUS: 732 ifs = (struct ifstat *)data; 733 ifs->ascii[0] = '\0'; 734 735 case SIOCGIFMEDIA: 736 case SIOCGIFGENERIC: 737 if (ifp->if_ioctl == 0) 738 return (EOPNOTSUPP); 739 return ((*ifp->if_ioctl)(ifp, cmd, data)); 740 741 default: 742 if (so->so_proto == 0) 743 return (EOPNOTSUPP); 744 #ifndef COMPAT_43 745 return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 746 data, 747 ifp, p)); 748 #else 749 { 750 int ocmd = cmd; 751 752 switch (cmd) { 753 754 case SIOCSIFDSTADDR: 755 case SIOCSIFADDR: 756 case SIOCSIFBRDADDR: 757 case SIOCSIFNETMASK: 758 #if BYTE_ORDER != BIG_ENDIAN 759 if (ifr->ifr_addr.sa_family == 0 && 760 ifr->ifr_addr.sa_len < 16) { 761 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 762 ifr->ifr_addr.sa_len = 16; 763 } 764 #else 765 if (ifr->ifr_addr.sa_len == 0) 766 ifr->ifr_addr.sa_len = 16; 767 #endif 768 break; 769 770 case OSIOCGIFADDR: 771 cmd = SIOCGIFADDR; 772 break; 773 774 case OSIOCGIFDSTADDR: 775 cmd = SIOCGIFDSTADDR; 776 break; 777 778 case OSIOCGIFBRDADDR: 779 cmd = SIOCGIFBRDADDR; 780 break; 781 782 case OSIOCGIFNETMASK: 783 cmd = SIOCGIFNETMASK; 784 } 785 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 786 cmd, 787 data, 788 ifp, p)); 789 switch (ocmd) { 790 791 case OSIOCGIFADDR: 792 case OSIOCGIFDSTADDR: 793 case OSIOCGIFBRDADDR: 794 case OSIOCGIFNETMASK: 795 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 796 } 797 return (error); 798 799 } 800 #endif 801 } 802 return (0); 803 } 804 805 /* 806 * Set/clear promiscuous mode on interface ifp based on the truth value 807 * of pswitch. The calls are reference counted so that only the first 808 * "on" request actually has an effect, as does the final "off" request. 809 * Results are undefined if the "off" and "on" requests are not matched. 810 */ 811 int 812 ifpromisc(ifp, pswitch) 813 struct ifnet *ifp; 814 int pswitch; 815 { 816 struct ifreq ifr; 817 int error; 818 819 if (pswitch) { 820 /* 821 * If the device is not configured up, we cannot put it in 822 * promiscuous mode. 823 */ 824 if ((ifp->if_flags & IFF_UP) == 0) 825 return (ENETDOWN); 826 if (ifp->if_pcount++ != 0) 827 return (0); 828 ifp->if_flags |= IFF_PROMISC; 829 log(LOG_INFO, "%s%d: promiscuous mode enabled\n", 830 ifp->if_name, ifp->if_unit); 831 } else { 832 if (--ifp->if_pcount > 0) 833 return (0); 834 ifp->if_flags &= ~IFF_PROMISC; 835 } 836 ifr.ifr_flags = ifp->if_flags; 837 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 838 if (error == 0) 839 rt_ifmsg(ifp); 840 return error; 841 } 842 843 /* 844 * Return interface configuration 845 * of system. List may be used 846 * in later ioctl's (above) to get 847 * other information. 848 */ 849 /*ARGSUSED*/ 850 static int 851 ifconf(cmd, data) 852 u_long cmd; 853 caddr_t data; 854 { 855 register struct ifconf *ifc = (struct ifconf *)data; 856 register struct ifnet *ifp = ifnet.tqh_first; 857 register struct ifaddr *ifa; 858 struct ifreq ifr, *ifrp; 859 int space = ifc->ifc_len, error = 0; 860 861 ifrp = ifc->ifc_req; 862 for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) { 863 char workbuf[64]; 864 int ifnlen, addrs; 865 866 ifnlen = snprintf(workbuf, sizeof(workbuf), 867 "%s%d", ifp->if_name, ifp->if_unit); 868 if(ifnlen + 1 > sizeof ifr.ifr_name) { 869 error = ENAMETOOLONG; 870 } else { 871 strcpy(ifr.ifr_name, workbuf); 872 } 873 874 addrs = 0; 875 ifa = ifp->if_addrhead.tqh_first; 876 for ( ; space > sizeof (ifr) && ifa; 877 ifa = ifa->ifa_link.tqe_next) { 878 register struct sockaddr *sa = ifa->ifa_addr; 879 if (curproc->p_prison && prison_if(curproc, sa)) 880 continue; 881 addrs++; 882 #ifdef COMPAT_43 883 if (cmd == OSIOCGIFCONF) { 884 struct osockaddr *osa = 885 (struct osockaddr *)&ifr.ifr_addr; 886 ifr.ifr_addr = *sa; 887 osa->sa_family = sa->sa_family; 888 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 889 sizeof (ifr)); 890 ifrp++; 891 } else 892 #endif 893 if (sa->sa_len <= sizeof(*sa)) { 894 ifr.ifr_addr = *sa; 895 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 896 sizeof (ifr)); 897 ifrp++; 898 } else { 899 space -= sa->sa_len - sizeof(*sa); 900 if (space < sizeof (ifr)) 901 break; 902 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 903 sizeof (ifr.ifr_name)); 904 if (error == 0) 905 error = copyout((caddr_t)sa, 906 (caddr_t)&ifrp->ifr_addr, sa->sa_len); 907 ifrp = (struct ifreq *) 908 (sa->sa_len + (caddr_t)&ifrp->ifr_addr); 909 } 910 if (error) 911 break; 912 space -= sizeof (ifr); 913 } 914 if (!addrs) { 915 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 916 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 917 sizeof (ifr)); 918 if (error) 919 break; 920 space -= sizeof (ifr), ifrp++; 921 } 922 } 923 ifc->ifc_len -= space; 924 return (error); 925 } 926 927 /* 928 * Just like if_promisc(), but for all-multicast-reception mode. 929 */ 930 int 931 if_allmulti(ifp, onswitch) 932 struct ifnet *ifp; 933 int onswitch; 934 { 935 int error = 0; 936 int s = splimp(); 937 938 if (onswitch) { 939 if (ifp->if_amcount++ == 0) { 940 ifp->if_flags |= IFF_ALLMULTI; 941 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0); 942 } 943 } else { 944 if (ifp->if_amcount > 1) { 945 ifp->if_amcount--; 946 } else { 947 ifp->if_amcount = 0; 948 ifp->if_flags &= ~IFF_ALLMULTI; 949 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0); 950 } 951 } 952 splx(s); 953 954 if (error == 0) 955 rt_ifmsg(ifp); 956 return error; 957 } 958 959 /* 960 * Add a multicast listenership to the interface in question. 961 * The link layer provides a routine which converts 962 */ 963 int 964 if_addmulti(ifp, sa, retifma) 965 struct ifnet *ifp; /* interface to manipulate */ 966 struct sockaddr *sa; /* address to add */ 967 struct ifmultiaddr **retifma; 968 { 969 struct sockaddr *llsa, *dupsa; 970 int error, s; 971 struct ifmultiaddr *ifma; 972 973 /* 974 * If the matching multicast address already exists 975 * then don't add a new one, just add a reference 976 */ 977 for (ifma = ifp->if_multiaddrs.lh_first; ifma; 978 ifma = ifma->ifma_link.le_next) { 979 if (equal(sa, ifma->ifma_addr)) { 980 ifma->ifma_refcount++; 981 if (retifma) 982 *retifma = ifma; 983 return 0; 984 } 985 } 986 987 /* 988 * Give the link layer a chance to accept/reject it, and also 989 * find out which AF_LINK address this maps to, if it isn't one 990 * already. 991 */ 992 if (ifp->if_resolvemulti) { 993 error = ifp->if_resolvemulti(ifp, &llsa, sa); 994 if (error) return error; 995 } else { 996 llsa = 0; 997 } 998 999 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK); 1000 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK); 1001 bcopy(sa, dupsa, sa->sa_len); 1002 1003 ifma->ifma_addr = dupsa; 1004 ifma->ifma_lladdr = llsa; 1005 ifma->ifma_ifp = ifp; 1006 ifma->ifma_refcount = 1; 1007 ifma->ifma_protospec = 0; 1008 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 1009 1010 /* 1011 * Some network interfaces can scan the address list at 1012 * interrupt time; lock them out. 1013 */ 1014 s = splimp(); 1015 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 1016 splx(s); 1017 *retifma = ifma; 1018 1019 if (llsa != 0) { 1020 for (ifma = ifp->if_multiaddrs.lh_first; ifma; 1021 ifma = ifma->ifma_link.le_next) { 1022 if (equal(ifma->ifma_addr, llsa)) 1023 break; 1024 } 1025 if (ifma) { 1026 ifma->ifma_refcount++; 1027 } else { 1028 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, 1029 M_IFMADDR, M_WAITOK); 1030 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, 1031 M_IFMADDR, M_WAITOK); 1032 bcopy(llsa, dupsa, llsa->sa_len); 1033 ifma->ifma_addr = dupsa; 1034 ifma->ifma_ifp = ifp; 1035 ifma->ifma_refcount = 1; 1036 s = splimp(); 1037 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 1038 splx(s); 1039 } 1040 } 1041 /* 1042 * We are certain we have added something, so call down to the 1043 * interface to let them know about it. 1044 */ 1045 s = splimp(); 1046 ifp->if_ioctl(ifp, SIOCADDMULTI, 0); 1047 splx(s); 1048 1049 return 0; 1050 } 1051 1052 /* 1053 * Remove a reference to a multicast address on this interface. Yell 1054 * if the request does not match an existing membership. 1055 */ 1056 int 1057 if_delmulti(ifp, sa) 1058 struct ifnet *ifp; 1059 struct sockaddr *sa; 1060 { 1061 struct ifmultiaddr *ifma; 1062 int s; 1063 1064 for (ifma = ifp->if_multiaddrs.lh_first; ifma; 1065 ifma = ifma->ifma_link.le_next) 1066 if (equal(sa, ifma->ifma_addr)) 1067 break; 1068 if (ifma == 0) 1069 return ENOENT; 1070 1071 if (ifma->ifma_refcount > 1) { 1072 ifma->ifma_refcount--; 1073 return 0; 1074 } 1075 1076 rt_newmaddrmsg(RTM_DELMADDR, ifma); 1077 sa = ifma->ifma_lladdr; 1078 s = splimp(); 1079 LIST_REMOVE(ifma, ifma_link); 1080 splx(s); 1081 free(ifma->ifma_addr, M_IFMADDR); 1082 free(ifma, M_IFMADDR); 1083 if (sa == 0) 1084 return 0; 1085 1086 /* 1087 * Now look for the link-layer address which corresponds to 1088 * this network address. It had been squirreled away in 1089 * ifma->ifma_lladdr for this purpose (so we don't have 1090 * to call ifp->if_resolvemulti() again), and we saved that 1091 * value in sa above. If some nasty deleted the 1092 * link-layer address out from underneath us, we can deal because 1093 * the address we stored was is not the same as the one which was 1094 * in the record for the link-layer address. (So we don't complain 1095 * in that case.) 1096 */ 1097 for (ifma = ifp->if_multiaddrs.lh_first; ifma; 1098 ifma = ifma->ifma_link.le_next) 1099 if (equal(sa, ifma->ifma_addr)) 1100 break; 1101 if (ifma == 0) 1102 return 0; 1103 1104 if (ifma->ifma_refcount > 1) { 1105 ifma->ifma_refcount--; 1106 return 0; 1107 } 1108 1109 s = splimp(); 1110 LIST_REMOVE(ifma, ifma_link); 1111 ifp->if_ioctl(ifp, SIOCDELMULTI, 0); 1112 splx(s); 1113 free(ifma->ifma_addr, M_IFMADDR); 1114 free(sa, M_IFMADDR); 1115 free(ifma, M_IFMADDR); 1116 1117 return 0; 1118 } 1119 1120 struct ifmultiaddr * 1121 ifmaof_ifpforaddr(sa, ifp) 1122 struct sockaddr *sa; 1123 struct ifnet *ifp; 1124 { 1125 struct ifmultiaddr *ifma; 1126 1127 for (ifma = ifp->if_multiaddrs.lh_first; ifma; 1128 ifma = ifma->ifma_link.le_next) 1129 if (equal(ifma->ifma_addr, sa)) 1130 break; 1131 1132 return ifma; 1133 } 1134 1135 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 1136 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 1137