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