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