1 /* $NetBSD: if.c,v 1.102 2002/02/09 05:56:34 atatat Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by William Studnemund and Jason R. Thorpe. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 41 * All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. Neither the name of the project nor the names of its contributors 52 * may be used to endorse or promote products derived from this software 53 * without specific prior written permission. 54 * 55 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65 * SUCH DAMAGE. 66 */ 67 68 /* 69 * Copyright (c) 1980, 1986, 1993 70 * The Regents of the University of California. All rights reserved. 71 * 72 * Redistribution and use in source and binary forms, with or without 73 * modification, are permitted provided that the following conditions 74 * are met: 75 * 1. Redistributions of source code must retain the above copyright 76 * notice, this list of conditions and the following disclaimer. 77 * 2. Redistributions in binary form must reproduce the above copyright 78 * notice, this list of conditions and the following disclaimer in the 79 * documentation and/or other materials provided with the distribution. 80 * 3. All advertising materials mentioning features or use of this software 81 * must display the following acknowledgement: 82 * This product includes software developed by the University of 83 * California, Berkeley and its contributors. 84 * 4. Neither the name of the University nor the names of its contributors 85 * may be used to endorse or promote products derived from this software 86 * without specific prior written permission. 87 * 88 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 89 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 90 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 91 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 92 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 93 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 94 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 95 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 96 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 97 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 98 * SUCH DAMAGE. 99 * 100 * @(#)if.c 8.5 (Berkeley) 1/9/95 101 */ 102 103 #include <sys/cdefs.h> 104 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.102 2002/02/09 05:56:34 atatat Exp $"); 105 106 #include "opt_inet.h" 107 108 #include "opt_compat_linux.h" 109 #include "opt_compat_svr4.h" 110 #include "opt_compat_43.h" 111 #include "opt_atalk.h" 112 #include "opt_pfil_hooks.h" 113 114 #include <sys/param.h> 115 #include <sys/mbuf.h> 116 #include <sys/systm.h> 117 #include <sys/callout.h> 118 #include <sys/proc.h> 119 #include <sys/socket.h> 120 #include <sys/socketvar.h> 121 #include <sys/domain.h> 122 #include <sys/protosw.h> 123 #include <sys/kernel.h> 124 #include <sys/ioctl.h> 125 126 #include <net/if.h> 127 #include <net/if_dl.h> 128 #include <net/if_ether.h> 129 #include <net/if_ieee80211.h> 130 #include <net/if_types.h> 131 #include <net/radix.h> 132 #include <net/route.h> 133 #include <net/netisr.h> 134 #ifdef NETATALK 135 #include <netatalk/at_extern.h> 136 #include <netatalk/at.h> 137 #endif 138 139 #ifdef INET6 140 /*XXX*/ 141 #include <netinet/in.h> 142 #include <netinet6/in6_var.h> 143 #endif 144 145 int ifqmaxlen = IFQ_MAXLEN; 146 struct callout if_slowtimo_ch; 147 148 #ifdef INET6 149 /* 150 * XXX: declare here to avoid to include many inet6 related files.. 151 * should be more generalized? 152 */ 153 extern void nd6_setmtu __P((struct ifnet *)); 154 #endif 155 156 int if_rt_walktree __P((struct radix_node *, void *)); 157 158 struct if_clone *if_clone_lookup __P((const char *, int *)); 159 int if_clone_list __P((struct if_clonereq *)); 160 161 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners); 162 int if_cloners_count; 163 164 #if defined(INET) || defined(INET6) || defined(NETATALK) || defined(NS) || \ 165 defined(ISO) || defined(CCITT) || defined(NATM) 166 static void if_detach_queues __P((struct ifnet *, struct ifqueue *)); 167 #endif 168 169 /* 170 * Network interface utility routines. 171 * 172 * Routines with ifa_ifwith* names take sockaddr *'s as 173 * parameters. 174 */ 175 void 176 ifinit() 177 { 178 179 callout_init(&if_slowtimo_ch); 180 if_slowtimo(NULL); 181 } 182 183 /* 184 * Null routines used while an interface is going away. These routines 185 * just return an error. 186 */ 187 188 int 189 if_nulloutput(ifp, m, so, rt) 190 struct ifnet *ifp; 191 struct mbuf *m; 192 struct sockaddr *so; 193 struct rtentry *rt; 194 { 195 196 return (ENXIO); 197 } 198 199 void 200 if_nullinput(ifp, m) 201 struct ifnet *ifp; 202 struct mbuf *m; 203 { 204 205 /* Nothing. */ 206 } 207 208 void 209 if_nullstart(ifp) 210 struct ifnet *ifp; 211 { 212 213 /* Nothing. */ 214 } 215 216 int 217 if_nullioctl(ifp, cmd, data) 218 struct ifnet *ifp; 219 u_long cmd; 220 caddr_t data; 221 { 222 223 return (ENXIO); 224 } 225 226 int 227 if_nullinit(ifp) 228 struct ifnet *ifp; 229 { 230 231 return (ENXIO); 232 } 233 234 void 235 if_nullstop(ifp, disable) 236 struct ifnet *ifp; 237 int disable; 238 { 239 240 /* Nothing. */ 241 } 242 243 void 244 if_nullwatchdog(ifp) 245 struct ifnet *ifp; 246 { 247 248 /* Nothing. */ 249 } 250 251 void 252 if_nulldrain(ifp) 253 struct ifnet *ifp; 254 { 255 256 /* Nothing. */ 257 } 258 259 int if_index = 1; 260 struct ifaddr **ifnet_addrs = NULL; 261 struct ifnet **ifindex2ifnet = NULL; 262 263 /* 264 * Allocate the link level name for the specified interface. This 265 * is an attachment helper. It must be called after ifp->if_addrlen 266 * is initialized, which may not be the case when if_attach() is 267 * called. 268 */ 269 void 270 if_alloc_sadl(struct ifnet *ifp) 271 { 272 unsigned socksize, ifasize; 273 int namelen, masklen; 274 struct sockaddr_dl *sdl; 275 struct ifaddr *ifa; 276 277 /* 278 * If the interface already has a link name, release it 279 * now. This is useful for interfaces that can change 280 * link types, and thus switch link names often. 281 */ 282 if (ifp->if_sadl != NULL) 283 if_free_sadl(ifp); 284 285 namelen = strlen(ifp->if_xname); 286 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen; 287 socksize = masklen + ifp->if_addrlen; 288 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1))) 289 if (socksize < sizeof(*sdl)) 290 socksize = sizeof(*sdl); 291 socksize = ROUNDUP(socksize); 292 ifasize = sizeof(*ifa) + 2 * socksize; 293 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK); 294 memset((caddr_t)ifa, 0, ifasize); 295 sdl = (struct sockaddr_dl *)(ifa + 1); 296 sdl->sdl_len = socksize; 297 sdl->sdl_family = AF_LINK; 298 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 299 sdl->sdl_nlen = namelen; 300 sdl->sdl_alen = ifp->if_addrlen; 301 sdl->sdl_index = ifp->if_index; 302 sdl->sdl_type = ifp->if_type; 303 ifnet_addrs[ifp->if_index] = ifa; 304 IFAREF(ifa); 305 ifa->ifa_ifp = ifp; 306 ifa->ifa_rtrequest = link_rtrequest; 307 TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list); 308 IFAREF(ifa); 309 ifa->ifa_addr = (struct sockaddr *)sdl; 310 ifp->if_sadl = sdl; 311 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 312 ifa->ifa_netmask = (struct sockaddr *)sdl; 313 sdl->sdl_len = masklen; 314 while (namelen != 0) 315 sdl->sdl_data[--namelen] = 0xff; 316 } 317 318 /* 319 * Free the link level name for the specified interface. This is 320 * a detach helper. This is called from if_detach() or from 321 * link layer type specific detach functions. 322 */ 323 void 324 if_free_sadl(struct ifnet *ifp) 325 { 326 struct ifaddr *ifa; 327 int s; 328 329 ifa = ifnet_addrs[ifp->if_index]; 330 if (ifa == NULL) { 331 KASSERT(ifp->if_sadl == NULL); 332 return; 333 } 334 335 KASSERT(ifp->if_sadl != NULL); 336 337 s = splnet(); 338 rtinit(ifa, RTM_DELETE, 0); 339 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list); 340 IFAFREE(ifa); 341 342 ifp->if_sadl = NULL; 343 344 ifnet_addrs[ifp->if_index] = NULL; 345 IFAFREE(ifa); 346 splx(s); 347 } 348 349 /* 350 * Attach an interface to the 351 * list of "active" interfaces. 352 */ 353 void 354 if_attach(ifp) 355 struct ifnet *ifp; 356 { 357 static size_t if_indexlim = 0; 358 int indexlim = 0; 359 360 if (if_indexlim == 0) { 361 TAILQ_INIT(&ifnet); 362 if_indexlim = 8; 363 } 364 TAILQ_INIT(&ifp->if_addrlist); 365 TAILQ_INSERT_TAIL(&ifnet, ifp, if_list); 366 ifp->if_index = if_index; 367 if (ifindex2ifnet == 0) 368 if_index++; 369 else 370 while (ifindex2ifnet[ifp->if_index] != NULL) { 371 ++if_index; 372 if (if_index == 0) 373 if_index = 1; 374 /* 375 * If we hit USHRT_MAX, we skip back to 0 since 376 * there are a number of places where the value 377 * of if_index or if_index itself is compared 378 * to to or stored in an unsigned short. By 379 * jumping back, we won't botch those assignments 380 * or comparisons. 381 */ 382 else if (if_index == USHRT_MAX) { 383 /* 384 * However, if we have to jump back to 385 * zero *twice* without finding an empty 386 * slot in ifindex2ifnet[], then there 387 * there are too many (>65535) interfaces. 388 */ 389 if (indexlim++) 390 panic("too many interfaces"); 391 else 392 if_index = 1; 393 } 394 ifp->if_index = if_index; 395 } 396 397 /* 398 * We have some arrays that should be indexed by if_index. 399 * since if_index will grow dynamically, they should grow too. 400 * struct ifadd **ifnet_addrs 401 * struct ifnet **ifindex2ifnet 402 */ 403 if (ifnet_addrs == 0 || ifindex2ifnet == 0 || 404 ifp->if_index >= if_indexlim) { 405 size_t n; 406 caddr_t q; 407 408 while (ifp->if_index >= if_indexlim) 409 if_indexlim <<= 1; 410 411 /* grow ifnet_addrs */ 412 n = if_indexlim * sizeof(struct ifaddr *); 413 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK); 414 memset(q, 0, n); 415 if (ifnet_addrs) { 416 bcopy((caddr_t)ifnet_addrs, q, n/2); 417 free((caddr_t)ifnet_addrs, M_IFADDR); 418 } 419 ifnet_addrs = (struct ifaddr **)q; 420 421 /* grow ifindex2ifnet */ 422 n = if_indexlim * sizeof(struct ifnet *); 423 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK); 424 memset(q, 0, n); 425 if (ifindex2ifnet) { 426 bcopy((caddr_t)ifindex2ifnet, q, n/2); 427 free((caddr_t)ifindex2ifnet, M_IFADDR); 428 } 429 ifindex2ifnet = (struct ifnet **)q; 430 } 431 432 ifindex2ifnet[ifp->if_index] = ifp; 433 434 /* 435 * Link level name is allocated later by a separate call to 436 * if_alloc_sadl(). 437 */ 438 439 if (ifp->if_snd.ifq_maxlen == 0) 440 ifp->if_snd.ifq_maxlen = ifqmaxlen; 441 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */ 442 443 ifp->if_link_state = LINK_STATE_UNKNOWN; 444 445 ifp->if_capenable = 0; 446 ifp->if_csum_flags_tx = 0; 447 ifp->if_csum_flags_rx = 0; 448 449 #ifdef ALTQ 450 ifp->if_snd.altq_type = 0; 451 ifp->if_snd.altq_disc = NULL; 452 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE; 453 ifp->if_snd.altq_tbr = NULL; 454 ifp->if_snd.altq_ifp = ifp; 455 #endif 456 457 #ifdef PFIL_HOOKS 458 ifp->if_pfil.ph_type = PFIL_TYPE_IFNET; 459 ifp->if_pfil.ph_ifnet = ifp; 460 if (pfil_head_register(&ifp->if_pfil) != 0) 461 printf("%s: WARNING: unable to register pfil hook\n", 462 ifp->if_xname); 463 #endif 464 465 /* Announce the interface. */ 466 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 467 } 468 469 /* 470 * Deactivate an interface. This points all of the procedure 471 * handles at error stubs. May be called from interrupt context. 472 */ 473 void 474 if_deactivate(ifp) 475 struct ifnet *ifp; 476 { 477 int s; 478 479 s = splnet(); 480 481 ifp->if_output = if_nulloutput; 482 ifp->if_input = if_nullinput; 483 ifp->if_start = if_nullstart; 484 ifp->if_ioctl = if_nullioctl; 485 ifp->if_init = if_nullinit; 486 ifp->if_stop = if_nullstop; 487 ifp->if_watchdog = if_nullwatchdog; 488 ifp->if_drain = if_nulldrain; 489 490 /* No more packets may be enqueued. */ 491 ifp->if_snd.ifq_maxlen = 0; 492 493 splx(s); 494 } 495 496 /* 497 * Detach an interface from the list of "active" interfaces, 498 * freeing any resources as we go along. 499 * 500 * NOTE: This routine must be called with a valid thread context, 501 * as it may block. 502 */ 503 void 504 if_detach(ifp) 505 struct ifnet *ifp; 506 { 507 struct socket so; 508 struct ifaddr *ifa; 509 #ifdef IFAREF_DEBUG 510 struct ifaddr *last_ifa = NULL; 511 #endif 512 struct domain *dp; 513 struct protosw *pr; 514 struct radix_node_head *rnh; 515 int s, i, family, purged; 516 517 /* 518 * XXX It's kind of lame that we have to have the 519 * XXX socket structure... 520 */ 521 memset(&so, 0, sizeof(so)); 522 523 s = splnet(); 524 525 /* 526 * Do an if_down() to give protocols a chance to do something. 527 */ 528 if_down(ifp); 529 530 #ifdef ALTQ 531 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 532 altq_disable(&ifp->if_snd); 533 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 534 altq_detach(&ifp->if_snd); 535 #endif 536 537 #ifdef PFIL_HOOKS 538 (void) pfil_head_unregister(&ifp->if_pfil); 539 #endif 540 541 if_free_sadl(ifp); 542 543 /* 544 * Rip all the addresses off the interface. This should make 545 * all of the routes go away. 546 */ 547 while ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) != NULL) { 548 family = ifa->ifa_addr->sa_family; 549 #ifdef IFAREF_DEBUG 550 printf("if_detach: ifaddr %p, family %d, refcnt %d\n", 551 ifa, family, ifa->ifa_refcnt); 552 if (last_ifa != NULL && ifa == last_ifa) 553 panic("if_detach: loop detected"); 554 last_ifa = ifa; 555 #endif 556 if (family == AF_LINK) { 557 /* 558 * XXX This case may now be obsolete by 559 * XXX the call to if_free_sadl(). 560 */ 561 rtinit(ifa, RTM_DELETE, 0); 562 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list); 563 IFAFREE(ifa); 564 } else { 565 dp = pffinddomain(family); 566 #ifdef DIAGNOSTIC 567 if (dp == NULL) 568 panic("if_detach: no domain for AF %d\n", 569 family); 570 #endif 571 purged = 0; 572 for (pr = dp->dom_protosw; 573 pr < dp->dom_protoswNPROTOSW; pr++) { 574 so.so_proto = pr; 575 if (pr->pr_usrreq != NULL) { 576 (void) (*pr->pr_usrreq)(&so, 577 PRU_PURGEIF, NULL, NULL, 578 (struct mbuf *) ifp, curproc); 579 purged = 1; 580 } 581 } 582 if (purged == 0) { 583 /* 584 * XXX What's really the best thing to do 585 * XXX here? --thorpej@netbsd.org 586 */ 587 printf("if_detach: WARNING: AF %d not purged\n", 588 family); 589 } 590 } 591 } 592 593 /* Walk the routing table looking for straglers. */ 594 for (i = 0; i <= AF_MAX; i++) { 595 if ((rnh = rt_tables[i]) != NULL) 596 (void) (*rnh->rnh_walktree)(rnh, if_rt_walktree, ifp); 597 } 598 599 /* Announce that the interface is gone. */ 600 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 601 602 ifindex2ifnet[ifp->if_index] = NULL; 603 604 TAILQ_REMOVE(&ifnet, ifp, if_list); 605 606 /* 607 * remove packets came from ifp, from software interrupt queues. 608 * net/netisr_dispatch.h is not usable, as some of them use 609 * strange queue names. 610 */ 611 #define IF_DETACH_QUEUES(x) \ 612 do { \ 613 extern struct ifqueue x; \ 614 if_detach_queues(ifp, & x); \ 615 } while (0) 616 #ifdef INET 617 #if NARP > 0 618 IF_DETACH_QUEUES(arpintrq); 619 #endif 620 IF_DETACH_QUEUES(ipintrq); 621 #endif 622 #ifdef INET6 623 IF_DETACH_QUEUES(ip6intrq); 624 #endif 625 #ifdef NETATALK 626 IF_DETACH_QUEUES(atintrq1); 627 IF_DETACH_QUEUES(atintrq2); 628 #endif 629 #ifdef NS 630 IF_DETACH_QUEUES(nsintrq); 631 #endif 632 #ifdef ISO 633 IF_DETACH_QUEUES(clnlintrq); 634 #endif 635 #ifdef CCITT 636 IF_DETACH_QUEUES(llcintrq); 637 IF_DETACH_QUEUES(hdintrq); 638 #endif 639 #ifdef NATM 640 IF_DETACH_QUEUES(natmintrq); 641 #endif 642 #undef IF_DETACH_QUEUES 643 644 splx(s); 645 } 646 647 #if defined(INET) || defined(INET6) || defined(NETATALK) || defined(NS) || \ 648 defined(ISO) || defined(CCITT) || defined(NATM) 649 static void 650 if_detach_queues(ifp, q) 651 struct ifnet *ifp; 652 struct ifqueue *q; 653 { 654 struct mbuf *m, *prev, *next; 655 656 prev = NULL; 657 for (m = q->ifq_head; m; m = next) { 658 next = m->m_nextpkt; 659 #ifdef DIAGNOSTIC 660 if ((m->m_flags & M_PKTHDR) == 0) { 661 prev = m; 662 continue; 663 } 664 #endif 665 if (m->m_pkthdr.rcvif != ifp) { 666 prev = m; 667 continue; 668 } 669 670 if (prev) 671 prev->m_nextpkt = m->m_nextpkt; 672 else 673 q->ifq_head = m->m_nextpkt; 674 if (q->ifq_tail == m) 675 q->ifq_tail = prev; 676 q->ifq_len--; 677 678 m->m_nextpkt = NULL; 679 m_freem(m); 680 IF_DROP(q); 681 } 682 } 683 #endif /* defined(INET) || ... */ 684 685 /* 686 * Callback for a radix tree walk to delete all references to an 687 * ifnet. 688 */ 689 int 690 if_rt_walktree(rn, v) 691 struct radix_node *rn; 692 void *v; 693 { 694 struct ifnet *ifp = (struct ifnet *)v; 695 struct rtentry *rt = (struct rtentry *)rn; 696 int error; 697 698 if (rt->rt_ifp == ifp) { 699 /* Delete the entry. */ 700 error = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 701 rt_mask(rt), rt->rt_flags, NULL); 702 if (error) 703 printf("%s: warning: unable to delete rtentry @ %p, " 704 "error = %d\n", ifp->if_xname, rt, error); 705 } 706 return (0); 707 } 708 709 /* 710 * Create a clone network interface. 711 */ 712 int 713 if_clone_create(name) 714 const char *name; 715 { 716 struct if_clone *ifc; 717 int unit; 718 719 ifc = if_clone_lookup(name, &unit); 720 if (ifc == NULL) 721 return (EINVAL); 722 723 if (ifunit(name) != NULL) 724 return (EEXIST); 725 726 return ((*ifc->ifc_create)(ifc, unit)); 727 } 728 729 /* 730 * Destroy a clone network interface. 731 */ 732 int 733 if_clone_destroy(name) 734 const char *name; 735 { 736 struct if_clone *ifc; 737 struct ifnet *ifp; 738 739 ifc = if_clone_lookup(name, NULL); 740 if (ifc == NULL) 741 return (EINVAL); 742 743 ifp = ifunit(name); 744 if (ifp == NULL) 745 return (ENXIO); 746 747 if (ifc->ifc_destroy == NULL) 748 return (EOPNOTSUPP); 749 750 (*ifc->ifc_destroy)(ifp); 751 return (0); 752 } 753 754 /* 755 * Look up a network interface cloner. 756 */ 757 struct if_clone * 758 if_clone_lookup(name, unitp) 759 const char *name; 760 int *unitp; 761 { 762 struct if_clone *ifc; 763 const char *cp; 764 int i; 765 766 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) { 767 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) { 768 if (ifc->ifc_name[i] != *cp) 769 goto next_ifc; 770 } 771 goto found_name; 772 next_ifc: 773 ifc = LIST_NEXT(ifc, ifc_list); 774 } 775 776 /* No match. */ 777 return (NULL); 778 779 found_name: 780 for (i = 0; *cp != '\0'; cp++) { 781 if (*cp < '0' || *cp > '9') { 782 /* Bogus unit number. */ 783 return (NULL); 784 } 785 i = (i * 10) + (*cp - '0'); 786 } 787 788 if (unitp != NULL) 789 *unitp = i; 790 return (ifc); 791 } 792 793 /* 794 * Register a network interface cloner. 795 */ 796 void 797 if_clone_attach(ifc) 798 struct if_clone *ifc; 799 { 800 801 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list); 802 if_cloners_count++; 803 } 804 805 /* 806 * Unregister a network interface cloner. 807 */ 808 void 809 if_clone_detach(ifc) 810 struct if_clone *ifc; 811 { 812 813 LIST_REMOVE(ifc, ifc_list); 814 if_cloners_count--; 815 } 816 817 /* 818 * Provide list of interface cloners to userspace. 819 */ 820 int 821 if_clone_list(ifcr) 822 struct if_clonereq *ifcr; 823 { 824 char outbuf[IFNAMSIZ], *dst; 825 struct if_clone *ifc; 826 int count, error = 0; 827 828 ifcr->ifcr_total = if_cloners_count; 829 if ((dst = ifcr->ifcr_buffer) == NULL) { 830 /* Just asking how many there are. */ 831 return (0); 832 } 833 834 if (ifcr->ifcr_count < 0) 835 return (EINVAL); 836 837 count = (if_cloners_count < ifcr->ifcr_count) ? 838 if_cloners_count : ifcr->ifcr_count; 839 840 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0; 841 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) { 842 strncpy(outbuf, ifc->ifc_name, IFNAMSIZ); 843 outbuf[IFNAMSIZ - 1] = '\0'; /* sanity */ 844 error = copyout(outbuf, dst, IFNAMSIZ); 845 if (error) 846 break; 847 } 848 849 return (error); 850 } 851 852 /* 853 * Locate an interface based on a complete address. 854 */ 855 /*ARGSUSED*/ 856 struct ifaddr * 857 ifa_ifwithaddr(addr) 858 struct sockaddr *addr; 859 { 860 struct ifnet *ifp; 861 struct ifaddr *ifa; 862 863 #define equal(a1, a2) \ 864 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0) 865 866 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL; 867 ifp = TAILQ_NEXT(ifp, if_list)) { 868 if (ifp->if_output == if_nulloutput) 869 continue; 870 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; 871 ifa = TAILQ_NEXT(ifa, ifa_list)) { 872 if (ifa->ifa_addr->sa_family != addr->sa_family) 873 continue; 874 if (equal(addr, ifa->ifa_addr)) 875 return (ifa); 876 if ((ifp->if_flags & IFF_BROADCAST) && 877 ifa->ifa_broadaddr && 878 /* IP6 doesn't have broadcast */ 879 ifa->ifa_broadaddr->sa_len != 0 && 880 equal(ifa->ifa_broadaddr, addr)) 881 return (ifa); 882 } 883 } 884 return (NULL); 885 } 886 887 /* 888 * Locate the point to point interface with a given destination address. 889 */ 890 /*ARGSUSED*/ 891 struct ifaddr * 892 ifa_ifwithdstaddr(addr) 893 struct sockaddr *addr; 894 { 895 struct ifnet *ifp; 896 struct ifaddr *ifa; 897 898 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL; 899 ifp = TAILQ_NEXT(ifp, if_list)) { 900 if (ifp->if_output == if_nulloutput) 901 continue; 902 if (ifp->if_flags & IFF_POINTOPOINT) { 903 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; 904 ifa = TAILQ_NEXT(ifa, ifa_list)) { 905 if (ifa->ifa_addr->sa_family != 906 addr->sa_family || 907 ifa->ifa_dstaddr == NULL) 908 continue; 909 if (equal(addr, ifa->ifa_dstaddr)) 910 return (ifa); 911 } 912 } 913 } 914 return (NULL); 915 } 916 917 /* 918 * Find an interface on a specific network. If many, choice 919 * is most specific found. 920 */ 921 struct ifaddr * 922 ifa_ifwithnet(addr) 923 struct sockaddr *addr; 924 { 925 struct ifnet *ifp; 926 struct ifaddr *ifa; 927 struct sockaddr_dl *sdl; 928 struct ifaddr *ifa_maybe = 0; 929 u_int af = addr->sa_family; 930 char *addr_data = addr->sa_data, *cplim; 931 932 if (af == AF_LINK) { 933 sdl = (struct sockaddr_dl *)addr; 934 if (sdl->sdl_index && sdl->sdl_index <= if_index && 935 ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput) 936 return (ifnet_addrs[sdl->sdl_index]); 937 } 938 #ifdef NETATALK 939 if (af == AF_APPLETALK) { 940 struct sockaddr_at *sat, *sat2; 941 sat = (struct sockaddr_at *)addr; 942 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL; 943 ifp = TAILQ_NEXT(ifp, if_list)) { 944 if (ifp->if_output == if_nulloutput) 945 continue; 946 ifa = at_ifawithnet((struct sockaddr_at *)addr, ifp); 947 if (ifa == NULL) 948 continue; 949 sat2 = (struct sockaddr_at *)ifa->ifa_addr; 950 if (sat2->sat_addr.s_net == sat->sat_addr.s_net) 951 return (ifa); /* exact match */ 952 if (ifa_maybe == NULL) { 953 /* else keep the if with the rigth range */ 954 ifa_maybe = ifa; 955 } 956 } 957 return (ifa_maybe); 958 } 959 #endif 960 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL; 961 ifp = TAILQ_NEXT(ifp, if_list)) { 962 if (ifp->if_output == if_nulloutput) 963 continue; 964 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; 965 ifa = TAILQ_NEXT(ifa, ifa_list)) { 966 char *cp, *cp2, *cp3; 967 968 if (ifa->ifa_addr->sa_family != af || 969 ifa->ifa_netmask == 0) 970 next: continue; 971 cp = addr_data; 972 cp2 = ifa->ifa_addr->sa_data; 973 cp3 = ifa->ifa_netmask->sa_data; 974 cplim = (char *)ifa->ifa_netmask + 975 ifa->ifa_netmask->sa_len; 976 while (cp3 < cplim) { 977 if ((*cp++ ^ *cp2++) & *cp3++) { 978 /* want to continue for() loop */ 979 goto next; 980 } 981 } 982 if (ifa_maybe == 0 || 983 rn_refines((caddr_t)ifa->ifa_netmask, 984 (caddr_t)ifa_maybe->ifa_netmask)) 985 ifa_maybe = ifa; 986 } 987 } 988 return (ifa_maybe); 989 } 990 991 /* 992 * Find the interface of the addresss. 993 */ 994 struct ifaddr * 995 ifa_ifwithladdr(addr) 996 struct sockaddr *addr; 997 { 998 struct ifaddr *ia; 999 1000 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) || 1001 (ia = ifa_ifwithnet(addr))) 1002 return (ia); 1003 return (NULL); 1004 } 1005 1006 /* 1007 * Find an interface using a specific address family 1008 */ 1009 struct ifaddr * 1010 ifa_ifwithaf(af) 1011 int af; 1012 { 1013 struct ifnet *ifp; 1014 struct ifaddr *ifa; 1015 1016 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL; 1017 ifp = TAILQ_NEXT(ifp, if_list)) { 1018 if (ifp->if_output == if_nulloutput) 1019 continue; 1020 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; 1021 ifa = TAILQ_NEXT(ifa, ifa_list)) { 1022 if (ifa->ifa_addr->sa_family == af) 1023 return (ifa); 1024 } 1025 } 1026 return (NULL); 1027 } 1028 1029 /* 1030 * Find an interface address specific to an interface best matching 1031 * a given address. 1032 */ 1033 struct ifaddr * 1034 ifaof_ifpforaddr(addr, ifp) 1035 struct sockaddr *addr; 1036 struct ifnet *ifp; 1037 { 1038 struct ifaddr *ifa; 1039 char *cp, *cp2, *cp3; 1040 char *cplim; 1041 struct ifaddr *ifa_maybe = 0; 1042 u_int af = addr->sa_family; 1043 1044 if (ifp->if_output == if_nulloutput) 1045 return (NULL); 1046 1047 if (af >= AF_MAX) 1048 return (NULL); 1049 1050 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; 1051 ifa = TAILQ_NEXT(ifa, ifa_list)) { 1052 if (ifa->ifa_addr->sa_family != af) 1053 continue; 1054 ifa_maybe = ifa; 1055 if (ifa->ifa_netmask == 0) { 1056 if (equal(addr, ifa->ifa_addr) || 1057 (ifa->ifa_dstaddr && 1058 equal(addr, ifa->ifa_dstaddr))) 1059 return (ifa); 1060 continue; 1061 } 1062 cp = addr->sa_data; 1063 cp2 = ifa->ifa_addr->sa_data; 1064 cp3 = ifa->ifa_netmask->sa_data; 1065 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1066 for (; cp3 < cplim; cp3++) { 1067 if ((*cp++ ^ *cp2++) & *cp3) 1068 break; 1069 } 1070 if (cp3 == cplim) 1071 return (ifa); 1072 } 1073 return (ifa_maybe); 1074 } 1075 1076 /* 1077 * Default action when installing a route with a Link Level gateway. 1078 * Lookup an appropriate real ifa to point to. 1079 * This should be moved to /sys/net/link.c eventually. 1080 */ 1081 void 1082 link_rtrequest(cmd, rt, info) 1083 int cmd; 1084 struct rtentry *rt; 1085 struct rt_addrinfo *info; 1086 { 1087 struct ifaddr *ifa; 1088 struct sockaddr *dst; 1089 struct ifnet *ifp; 1090 1091 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 1092 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 1093 return; 1094 if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) { 1095 IFAFREE(rt->rt_ifa); 1096 rt->rt_ifa = ifa; 1097 IFAREF(ifa); 1098 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 1099 ifa->ifa_rtrequest(cmd, rt, info); 1100 } 1101 } 1102 1103 /* 1104 * Mark an interface down and notify protocols of 1105 * the transition. 1106 * NOTE: must be called at splsoftnet or equivalent. 1107 */ 1108 void 1109 if_down(ifp) 1110 struct ifnet *ifp; 1111 { 1112 struct ifaddr *ifa; 1113 1114 ifp->if_flags &= ~IFF_UP; 1115 microtime(&ifp->if_lastchange); 1116 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; 1117 ifa = TAILQ_NEXT(ifa, ifa_list)) 1118 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1119 IFQ_PURGE(&ifp->if_snd); 1120 rt_ifmsg(ifp); 1121 } 1122 1123 /* 1124 * Mark an interface up and notify protocols of 1125 * the transition. 1126 * NOTE: must be called at splsoftnet or equivalent. 1127 */ 1128 void 1129 if_up(ifp) 1130 struct ifnet *ifp; 1131 { 1132 #ifdef notyet 1133 struct ifaddr *ifa; 1134 #endif 1135 1136 ifp->if_flags |= IFF_UP; 1137 microtime(&ifp->if_lastchange); 1138 #ifdef notyet 1139 /* this has no effect on IP, and will kill all ISO connections XXX */ 1140 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; 1141 ifa = TAILQ_NEXT(ifa, ifa_list)) 1142 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1143 #endif 1144 rt_ifmsg(ifp); 1145 #ifdef INET6 1146 in6_if_up(ifp); 1147 #endif 1148 } 1149 1150 /* 1151 * Handle interface watchdog timer routines. Called 1152 * from softclock, we decrement timers (if set) and 1153 * call the appropriate interface routine on expiration. 1154 */ 1155 void 1156 if_slowtimo(arg) 1157 void *arg; 1158 { 1159 struct ifnet *ifp; 1160 int s = splnet(); 1161 1162 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL; 1163 ifp = TAILQ_NEXT(ifp, if_list)) { 1164 if (ifp->if_timer == 0 || --ifp->if_timer) 1165 continue; 1166 if (ifp->if_watchdog) 1167 (*ifp->if_watchdog)(ifp); 1168 } 1169 splx(s); 1170 callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ, 1171 if_slowtimo, NULL); 1172 } 1173 1174 /* 1175 * Set/clear promiscuous mode on interface ifp based on the truth value 1176 * of pswitch. The calls are reference counted so that only the first 1177 * "on" request actually has an effect, as does the final "off" request. 1178 * Results are undefined if the "off" and "on" requests are not matched. 1179 */ 1180 int 1181 ifpromisc(ifp, pswitch) 1182 struct ifnet *ifp; 1183 int pswitch; 1184 { 1185 int pcount, ret; 1186 short flags; 1187 struct ifreq ifr; 1188 1189 pcount = ifp->if_pcount; 1190 flags = ifp->if_flags; 1191 if (pswitch) { 1192 /* 1193 * Allow the device to be "placed" into promiscuous 1194 * mode even if it is not configured up. It will 1195 * consult IFF_PROMISC when it is is brought up. 1196 */ 1197 if (ifp->if_pcount++ != 0) 1198 return (0); 1199 ifp->if_flags |= IFF_PROMISC; 1200 if ((ifp->if_flags & IFF_UP) == 0) 1201 return (0); 1202 } else { 1203 if (--ifp->if_pcount > 0) 1204 return (0); 1205 ifp->if_flags &= ~IFF_PROMISC; 1206 /* 1207 * If the device is not configured up, we should not need to 1208 * turn off promiscuous mode (device should have turned it 1209 * off when interface went down; and will look at IFF_PROMISC 1210 * again next time interface comes up). 1211 */ 1212 if ((ifp->if_flags & IFF_UP) == 0) 1213 return (0); 1214 } 1215 memset(&ifr, 0, sizeof(ifr)); 1216 ifr.ifr_flags = ifp->if_flags; 1217 ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t) &ifr); 1218 /* Restore interface state if not successful. */ 1219 if (ret != 0) { 1220 ifp->if_pcount = pcount; 1221 ifp->if_flags = flags; 1222 } 1223 return (ret); 1224 } 1225 1226 /* 1227 * Map interface name to 1228 * interface structure pointer. 1229 */ 1230 struct ifnet * 1231 ifunit(name) 1232 const char *name; 1233 { 1234 struct ifnet *ifp; 1235 1236 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL; 1237 ifp = TAILQ_NEXT(ifp, if_list)) { 1238 if (ifp->if_output == if_nulloutput) 1239 continue; 1240 if (strcmp(ifp->if_xname, name) == 0) 1241 return (ifp); 1242 } 1243 return (NULL); 1244 } 1245 1246 /* 1247 * Interface ioctls. 1248 */ 1249 int 1250 ifioctl(so, cmd, data, p) 1251 struct socket *so; 1252 u_long cmd; 1253 caddr_t data; 1254 struct proc *p; 1255 { 1256 struct ifnet *ifp; 1257 struct ifreq *ifr; 1258 struct ifcapreq *ifcr; 1259 int s, error = 0; 1260 short oif_flags; 1261 1262 switch (cmd) { 1263 1264 case SIOCGIFCONF: 1265 case OSIOCGIFCONF: 1266 return (ifconf(cmd, data)); 1267 } 1268 ifr = (struct ifreq *)data; 1269 ifcr = (struct ifcapreq *)data; 1270 1271 switch (cmd) { 1272 case SIOCIFCREATE: 1273 case SIOCIFDESTROY: 1274 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1275 return (error); 1276 return ((cmd == SIOCIFCREATE) ? 1277 if_clone_create(ifr->ifr_name) : 1278 if_clone_destroy(ifr->ifr_name)); 1279 1280 case SIOCIFGCLONERS: 1281 return (if_clone_list((struct if_clonereq *)data)); 1282 } 1283 1284 ifp = ifunit(ifr->ifr_name); 1285 if (ifp == 0) 1286 return (ENXIO); 1287 oif_flags = ifp->if_flags; 1288 switch (cmd) { 1289 1290 case SIOCGIFFLAGS: 1291 ifr->ifr_flags = ifp->if_flags; 1292 break; 1293 1294 case SIOCGIFMETRIC: 1295 ifr->ifr_metric = ifp->if_metric; 1296 break; 1297 1298 case SIOCGIFMTU: 1299 ifr->ifr_mtu = ifp->if_mtu; 1300 break; 1301 1302 case SIOCGIFDLT: 1303 ifr->ifr_dlt = ifp->if_dlt; 1304 break; 1305 1306 case SIOCSIFFLAGS: 1307 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1308 return (error); 1309 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) { 1310 s = splnet(); 1311 if_down(ifp); 1312 splx(s); 1313 } 1314 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) { 1315 s = splnet(); 1316 if_up(ifp); 1317 splx(s); 1318 } 1319 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1320 (ifr->ifr_flags &~ IFF_CANTCHANGE); 1321 if (ifp->if_ioctl) 1322 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1323 break; 1324 1325 case SIOCGIFCAP: 1326 ifcr->ifcr_capabilities = ifp->if_capabilities; 1327 ifcr->ifcr_capenable = ifp->if_capenable; 1328 break; 1329 1330 case SIOCSIFCAP: 1331 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1332 return (error); 1333 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0) 1334 return (EINVAL); 1335 if (ifp->if_ioctl == NULL) 1336 return (EOPNOTSUPP); 1337 1338 /* Must prevent race with packet reception here. */ 1339 s = splnet(); 1340 if (ifcr->ifcr_capenable != ifp->if_capenable) { 1341 struct ifreq ifrq; 1342 1343 ifrq.ifr_flags = ifp->if_flags; 1344 ifp->if_capenable = ifcr->ifcr_capenable; 1345 1346 /* Pre-compute the checksum flags mask. */ 1347 ifp->if_csum_flags_tx = 0; 1348 ifp->if_csum_flags_rx = 0; 1349 if (ifp->if_capenable & IFCAP_CSUM_IPv4) { 1350 ifp->if_csum_flags_tx |= M_CSUM_IPv4; 1351 ifp->if_csum_flags_rx |= M_CSUM_IPv4; 1352 } 1353 1354 if (ifp->if_capenable & IFCAP_CSUM_TCPv4) { 1355 ifp->if_csum_flags_tx |= M_CSUM_TCPv4; 1356 ifp->if_csum_flags_rx |= M_CSUM_TCPv4; 1357 } else if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) 1358 ifp->if_csum_flags_rx |= M_CSUM_TCPv4; 1359 1360 if (ifp->if_capenable & IFCAP_CSUM_UDPv4) { 1361 ifp->if_csum_flags_tx |= M_CSUM_UDPv4; 1362 ifp->if_csum_flags_rx |= M_CSUM_UDPv4; 1363 } else if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) 1364 ifp->if_csum_flags_rx |= M_CSUM_UDPv4; 1365 1366 if (ifp->if_capenable & IFCAP_CSUM_TCPv6) { 1367 ifp->if_csum_flags_tx |= M_CSUM_TCPv6; 1368 ifp->if_csum_flags_rx |= M_CSUM_TCPv6; 1369 } 1370 1371 if (ifp->if_capenable & IFCAP_CSUM_UDPv6) { 1372 ifp->if_csum_flags_tx |= M_CSUM_UDPv6; 1373 ifp->if_csum_flags_rx |= M_CSUM_UDPv6; 1374 } 1375 1376 /* 1377 * Only kick the interface if it's up. If it's 1378 * not up now, it will notice the cap enables 1379 * when it is brought up later. 1380 */ 1381 if (ifp->if_flags & IFF_UP) 1382 (void) (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, 1383 (caddr_t) &ifrq); 1384 } 1385 splx(s); 1386 break; 1387 1388 case SIOCSIFMETRIC: 1389 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1390 return (error); 1391 ifp->if_metric = ifr->ifr_metric; 1392 break; 1393 1394 case SIOCSIFMTU: 1395 { 1396 u_long oldmtu = ifp->if_mtu; 1397 1398 error = suser(p->p_ucred, &p->p_acflag); 1399 if (error) 1400 return (error); 1401 if (ifp->if_ioctl == NULL) 1402 return (EOPNOTSUPP); 1403 error = (*ifp->if_ioctl)(ifp, cmd, data); 1404 1405 /* 1406 * If the link MTU changed, do network layer specific procedure. 1407 */ 1408 if (ifp->if_mtu != oldmtu) { 1409 #ifdef INET6 1410 nd6_setmtu(ifp); 1411 #endif 1412 } 1413 break; 1414 } 1415 case SIOCSIFPHYADDR: 1416 case SIOCDIFPHYADDR: 1417 #ifdef INET6 1418 case SIOCSIFPHYADDR_IN6: 1419 #endif 1420 case SIOCSLIFPHYADDR: 1421 case SIOCADDMULTI: 1422 case SIOCDELMULTI: 1423 case SIOCSIFMEDIA: 1424 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1425 return (error); 1426 /* FALLTHROUGH */ 1427 case SIOCGIFPSRCADDR: 1428 case SIOCGIFPDSTADDR: 1429 case SIOCGLIFPHYADDR: 1430 case SIOCGIFMEDIA: 1431 if (ifp->if_ioctl == 0) 1432 return (EOPNOTSUPP); 1433 error = (*ifp->if_ioctl)(ifp, cmd, data); 1434 break; 1435 1436 case SIOCSDRVSPEC: 1437 case SIOCS80211NWID: 1438 case SIOCS80211NWKEY: 1439 case SIOCS80211POWER: 1440 /* XXX: need to pass proc pointer through to driver... */ 1441 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1442 return (error); 1443 /* FALLTHROUGH */ 1444 default: 1445 if (so->so_proto == 0) 1446 return (EOPNOTSUPP); 1447 #if !defined(COMPAT_43) && !defined(COMPAT_LINUX) && !defined(COMPAT_SVR4) && !defined(LKM) 1448 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, 1449 (struct mbuf *)cmd, (struct mbuf *)data, 1450 (struct mbuf *)ifp, p)); 1451 #else 1452 { 1453 int ocmd = cmd; 1454 1455 switch (cmd) { 1456 1457 case SIOCSIFADDR: 1458 case SIOCSIFDSTADDR: 1459 case SIOCSIFBRDADDR: 1460 case SIOCSIFNETMASK: 1461 #if BYTE_ORDER != BIG_ENDIAN 1462 if (ifr->ifr_addr.sa_family == 0 && 1463 ifr->ifr_addr.sa_len < 16) { 1464 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 1465 ifr->ifr_addr.sa_len = 16; 1466 } 1467 #else 1468 if (ifr->ifr_addr.sa_len == 0) 1469 ifr->ifr_addr.sa_len = 16; 1470 #endif 1471 break; 1472 1473 case OSIOCGIFADDR: 1474 cmd = SIOCGIFADDR; 1475 break; 1476 1477 case OSIOCGIFDSTADDR: 1478 cmd = SIOCGIFDSTADDR; 1479 break; 1480 1481 case OSIOCGIFBRDADDR: 1482 cmd = SIOCGIFBRDADDR; 1483 break; 1484 1485 case OSIOCGIFNETMASK: 1486 cmd = SIOCGIFNETMASK; 1487 } 1488 1489 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, 1490 (struct mbuf *)cmd, (struct mbuf *)data, 1491 (struct mbuf *)ifp, p)); 1492 1493 switch (ocmd) { 1494 case OSIOCGIFADDR: 1495 case OSIOCGIFDSTADDR: 1496 case OSIOCGIFBRDADDR: 1497 case OSIOCGIFNETMASK: 1498 *(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 1499 } 1500 } 1501 #endif /* COMPAT_43 */ 1502 break; 1503 } 1504 1505 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) { 1506 #ifdef INET6 1507 if ((ifp->if_flags & IFF_UP) != 0) { 1508 s = splnet(); 1509 in6_if_up(ifp); 1510 splx(s); 1511 } 1512 #endif 1513 } 1514 1515 return (error); 1516 } 1517 1518 /* 1519 * Return interface configuration 1520 * of system. List may be used 1521 * in later ioctl's (above) to get 1522 * other information. 1523 */ 1524 /*ARGSUSED*/ 1525 int 1526 ifconf(cmd, data) 1527 u_long cmd; 1528 caddr_t data; 1529 { 1530 struct ifconf *ifc = (struct ifconf *)data; 1531 struct ifnet *ifp; 1532 struct ifaddr *ifa; 1533 struct ifreq ifr, *ifrp; 1534 int space = ifc->ifc_len, error = 0; 1535 1536 ifrp = ifc->ifc_req; 1537 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1538 bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ); 1539 if ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) == 0) { 1540 memset((caddr_t)&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 1541 if (space >= (int)sizeof (ifr)) { 1542 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1543 sizeof(ifr)); 1544 if (error) 1545 break; 1546 } 1547 space -= sizeof (ifr), ifrp++; 1548 } else 1549 for (; ifa != 0; ifa = TAILQ_NEXT(ifa, ifa_list)) { 1550 struct sockaddr *sa = ifa->ifa_addr; 1551 #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4) 1552 if (cmd == OSIOCGIFCONF) { 1553 struct osockaddr *osa = 1554 (struct osockaddr *)&ifr.ifr_addr; 1555 ifr.ifr_addr = *sa; 1556 osa->sa_family = sa->sa_family; 1557 if (space >= (int)sizeof (ifr)) { 1558 error = copyout((caddr_t)&ifr, 1559 (caddr_t)ifrp, 1560 sizeof (ifr)); 1561 ifrp++; 1562 } 1563 } else 1564 #endif 1565 if (sa->sa_len <= sizeof(*sa)) { 1566 ifr.ifr_addr = *sa; 1567 if (space >= (int)sizeof (ifr)) { 1568 error = copyout((caddr_t)&ifr, 1569 (caddr_t)ifrp, 1570 sizeof (ifr)); 1571 ifrp++; 1572 } 1573 } else { 1574 space -= sa->sa_len - sizeof(*sa); 1575 if (space >= (int)sizeof (ifr)) { 1576 error = copyout((caddr_t)&ifr, 1577 (caddr_t)ifrp, 1578 sizeof (ifr.ifr_name)); 1579 if (error == 0) { 1580 error = copyout((caddr_t)sa, 1581 (caddr_t)&ifrp->ifr_addr, 1582 sa->sa_len); 1583 } 1584 ifrp = (struct ifreq *) 1585 (sa->sa_len + 1586 (caddr_t)&ifrp->ifr_addr); 1587 } 1588 } 1589 if (error) 1590 break; 1591 space -= sizeof (ifr); 1592 } 1593 } 1594 ifc->ifc_len -= space; 1595 return (error); 1596 } 1597