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