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