1 /* $OpenBSD: if.c,v 1.436 2016/07/13 16:45:19 mpi Exp $ */ 2 /* $NetBSD: if.c,v 1.35 1996/05/07 05:26:04 thorpej Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1980, 1986, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)if.c 8.3 (Berkeley) 1/4/94 62 */ 63 64 #include "bpfilter.h" 65 #include "bridge.h" 66 #include "carp.h" 67 #include "ether.h" 68 #include "pf.h" 69 #include "pfsync.h" 70 #include "ppp.h" 71 #include "pppoe.h" 72 #include "trunk.h" 73 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/mbuf.h> 77 #include <sys/socket.h> 78 #include <sys/socketvar.h> 79 #include <sys/timeout.h> 80 #include <sys/protosw.h> 81 #include <sys/kernel.h> 82 #include <sys/ioctl.h> 83 #include <sys/domain.h> 84 #include <sys/sysctl.h> 85 #include <sys/task.h> 86 #include <sys/atomic.h> 87 #include <sys/proc.h> 88 89 #include <dev/rndvar.h> 90 91 #include <net/if.h> 92 #include <net/if_dl.h> 93 #include <net/if_types.h> 94 #include <net/route.h> 95 #include <net/netisr.h> 96 97 #include <netinet/in.h> 98 #include <netinet/if_ether.h> 99 #include <netinet/igmp.h> 100 #ifdef MROUTING 101 #include <netinet/ip_mroute.h> 102 #endif 103 104 #ifdef INET6 105 #include <netinet6/in6_var.h> 106 #include <netinet6/in6_ifattach.h> 107 #include <netinet6/nd6.h> 108 #include <netinet/ip6.h> 109 #include <netinet6/ip6_var.h> 110 #endif 111 112 #ifdef MPLS 113 #include <netmpls/mpls.h> 114 #endif 115 116 #if NBPFILTER > 0 117 #include <net/bpf.h> 118 #endif 119 120 #if NBRIDGE > 0 121 #include <net/if_bridge.h> 122 #endif 123 124 #if NCARP > 0 125 #include <netinet/ip_carp.h> 126 #endif 127 128 #if NPF > 0 129 #include <net/pfvar.h> 130 #endif 131 132 void if_attachsetup(struct ifnet *); 133 void if_attachdomain(struct ifnet *); 134 void if_attach_common(struct ifnet *); 135 136 void if_detached_start(struct ifnet *); 137 int if_detached_ioctl(struct ifnet *, u_long, caddr_t); 138 139 int if_getgroup(caddr_t, struct ifnet *); 140 int if_getgroupmembers(caddr_t); 141 int if_getgroupattribs(caddr_t); 142 int if_setgroupattribs(caddr_t); 143 144 void if_linkstate(void *); 145 146 int if_clone_list(struct if_clonereq *); 147 struct if_clone *if_clone_lookup(const char *, int *); 148 149 int if_group_egress_build(void); 150 151 void if_watchdog_task(void *); 152 153 void if_input_process(void *); 154 void if_netisr(void *); 155 156 #ifdef DDB 157 void ifa_print_all(void); 158 #endif 159 160 void if_start_locked(struct ifnet *ifp); 161 162 /* 163 * interface index map 164 * 165 * the kernel maintains a mapping of interface indexes to struct ifnet 166 * pointers. 167 * 168 * the map is an array of struct ifnet pointers prefixed by an if_map 169 * structure. the if_map structure stores the length of its array. 170 * 171 * as interfaces are attached to the system, the map is grown on demand 172 * up to USHRT_MAX entries. 173 * 174 * interface index 0 is reserved and represents no interface. this 175 * supports the use of the interface index as the scope for IPv6 link 176 * local addresses, where scope 0 means no scope has been specified. 177 * it also supports the use of interface index as the unique identifier 178 * for network interfaces in SNMP applications as per RFC2863. therefore 179 * if_get(0) returns NULL. 180 */ 181 182 void if_ifp_dtor(void *, void *); 183 void if_map_dtor(void *, void *); 184 struct ifnet *if_ref(struct ifnet *); 185 186 /* 187 * struct if_map 188 * 189 * bounded array of ifnet srp pointers used to fetch references of live 190 * interfaces with if_get(). 191 */ 192 193 struct if_map { 194 unsigned long limit; 195 /* followed by limit ifnet srp pointers */ 196 }; 197 198 /* 199 * struct if_idxmap 200 * 201 * infrastructure to manage updates and accesses to the current if_map. 202 */ 203 204 struct if_idxmap { 205 unsigned int serial; 206 unsigned int count; 207 struct srp map; 208 }; 209 210 void if_idxmap_init(unsigned int); 211 void if_idxmap_insert(struct ifnet *); 212 void if_idxmap_remove(struct ifnet *); 213 214 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head); 215 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners); 216 int if_cloners_count; 217 218 struct timeout net_tick_to; 219 void net_tick(void *); 220 int net_livelocked(void); 221 int ifq_congestion; 222 223 int netisr; 224 struct taskq *softnettq; 225 226 struct mbuf_queue if_input_queue = MBUF_QUEUE_INITIALIZER(8192, IPL_NET); 227 struct task if_input_task = TASK_INITIALIZER(if_input_process, &if_input_queue); 228 struct task if_input_task_locked = TASK_INITIALIZER(if_netisr, NULL); 229 230 /* 231 * Network interface utility routines. 232 */ 233 void 234 ifinit(void) 235 { 236 /* 237 * most machines boot with 4 or 5 interfaces, so size the initial map 238 * to accomodate this 239 */ 240 if_idxmap_init(8); 241 242 timeout_set(&net_tick_to, net_tick, &net_tick_to); 243 244 softnettq = taskq_create("softnet", 1, IPL_NET, 245 TASKQ_MPSAFE | TASKQ_CANTSLEEP); 246 if (softnettq == NULL) 247 panic("unable to create softnet taskq"); 248 249 net_tick(&net_tick_to); 250 } 251 252 static struct if_idxmap if_idxmap = { 253 0, 254 0, 255 SRP_INITIALIZER() 256 }; 257 258 struct srp_gc if_ifp_gc = SRP_GC_INITIALIZER(if_ifp_dtor, NULL); 259 struct srp_gc if_map_gc = SRP_GC_INITIALIZER(if_map_dtor, NULL); 260 261 struct ifnet_head ifnet = TAILQ_HEAD_INITIALIZER(ifnet); 262 unsigned int lo0ifidx; 263 264 void 265 if_idxmap_init(unsigned int limit) 266 { 267 struct if_map *if_map; 268 struct srp *map; 269 unsigned int i; 270 271 if_idxmap.serial = 1; /* skip ifidx 0 so it can return NULL */ 272 273 if_map = malloc(sizeof(*if_map) + limit * sizeof(*map), 274 M_IFADDR, M_WAITOK); 275 276 if_map->limit = limit; 277 map = (struct srp *)(if_map + 1); 278 for (i = 0; i < limit; i++) 279 srp_init(&map[i]); 280 281 /* this is called early so there's nothing to race with */ 282 srp_update_locked(&if_map_gc, &if_idxmap.map, if_map); 283 } 284 285 void 286 if_idxmap_insert(struct ifnet *ifp) 287 { 288 struct if_map *if_map; 289 struct srp *map; 290 unsigned int index, i; 291 292 refcnt_init(&ifp->if_refcnt); 293 294 /* the kernel lock guarantees serialised modifications to if_idxmap */ 295 KERNEL_ASSERT_LOCKED(); 296 297 if (++if_idxmap.count > USHRT_MAX) 298 panic("too many interfaces"); 299 300 if_map = srp_get_locked(&if_idxmap.map); 301 map = (struct srp *)(if_map + 1); 302 303 index = if_idxmap.serial++ & USHRT_MAX; 304 305 if (index >= if_map->limit) { 306 struct if_map *nif_map; 307 struct srp *nmap; 308 unsigned int nlimit; 309 struct ifnet *nifp; 310 311 nlimit = if_map->limit * 2; 312 nif_map = malloc(sizeof(*nif_map) + nlimit * sizeof(*nmap), 313 M_IFADDR, M_WAITOK); 314 nmap = (struct srp *)(nif_map + 1); 315 316 nif_map->limit = nlimit; 317 for (i = 0; i < if_map->limit; i++) { 318 srp_init(&nmap[i]); 319 nifp = srp_get_locked(&map[i]); 320 if (nifp != NULL) { 321 srp_update_locked(&if_ifp_gc, &nmap[i], 322 if_ref(nifp)); 323 } 324 } 325 326 while (i < nlimit) { 327 srp_init(&nmap[i]); 328 i++; 329 } 330 331 srp_update_locked(&if_map_gc, &if_idxmap.map, nif_map); 332 if_map = nif_map; 333 map = nmap; 334 } 335 336 /* pick the next free index */ 337 for (i = 0; i < USHRT_MAX; i++) { 338 if (index != 0 && srp_get_locked(&map[index]) == NULL) 339 break; 340 341 index = if_idxmap.serial++ & USHRT_MAX; 342 } 343 344 /* commit */ 345 ifp->if_index = index; 346 srp_update_locked(&if_ifp_gc, &map[index], if_ref(ifp)); 347 } 348 349 void 350 if_idxmap_remove(struct ifnet *ifp) 351 { 352 struct if_map *if_map; 353 struct srp *map; 354 unsigned int index; 355 356 index = ifp->if_index; 357 358 /* the kernel lock guarantees serialised modifications to if_idxmap */ 359 KERNEL_ASSERT_LOCKED(); 360 361 if_map = srp_get_locked(&if_idxmap.map); 362 KASSERT(index < if_map->limit); 363 364 map = (struct srp *)(if_map + 1); 365 KASSERT(ifp == (struct ifnet *)srp_get_locked(&map[index])); 366 367 srp_update_locked(&if_ifp_gc, &map[index], NULL); 368 if_idxmap.count--; 369 /* end of if_idxmap modifications */ 370 371 /* sleep until the last reference is released */ 372 refcnt_finalize(&ifp->if_refcnt, "ifidxrm"); 373 } 374 375 void 376 if_ifp_dtor(void *null, void *ifp) 377 { 378 if_put(ifp); 379 } 380 381 void 382 if_map_dtor(void *null, void *m) 383 { 384 struct if_map *if_map = m; 385 struct srp *map = (struct srp *)(if_map + 1); 386 unsigned int i; 387 388 /* 389 * dont need to serialize the use of update_locked since this is 390 * the last reference to this map. there's nothing to race against. 391 */ 392 for (i = 0; i < if_map->limit; i++) 393 srp_update_locked(&if_ifp_gc, &map[i], NULL); 394 395 free(if_map, M_IFADDR, sizeof(*if_map) + if_map->limit * sizeof(*map)); 396 } 397 398 /* 399 * Attach an interface to the 400 * list of "active" interfaces. 401 */ 402 void 403 if_attachsetup(struct ifnet *ifp) 404 { 405 TAILQ_INIT(&ifp->if_groups); 406 407 if_addgroup(ifp, IFG_ALL); 408 409 if_attachdomain(ifp); 410 #if NPF > 0 411 pfi_attach_ifnet(ifp); 412 #endif 413 414 task_set(ifp->if_watchdogtask, if_watchdog_task, ifp); 415 timeout_set(ifp->if_slowtimo, if_slowtimo, ifp); 416 if_slowtimo(ifp); 417 418 task_set(ifp->if_linkstatetask, if_linkstate, ifp); 419 420 if_idxmap_insert(ifp); 421 KASSERT(if_get(0) == NULL); 422 423 /* Announce the interface. */ 424 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 425 } 426 427 /* 428 * Allocate the link level name for the specified interface. This 429 * is an attachment helper. It must be called after ifp->if_addrlen 430 * is initialized, which may not be the case when if_attach() is 431 * called. 432 */ 433 void 434 if_alloc_sadl(struct ifnet *ifp) 435 { 436 unsigned int socksize; 437 int namelen, masklen; 438 struct sockaddr_dl *sdl; 439 440 /* 441 * If the interface already has a link name, release it 442 * now. This is useful for interfaces that can change 443 * link types, and thus switch link names often. 444 */ 445 if (ifp->if_sadl != NULL) 446 if_free_sadl(ifp); 447 448 namelen = strlen(ifp->if_xname); 449 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen; 450 socksize = masklen + ifp->if_addrlen; 451 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1))) 452 if (socksize < sizeof(*sdl)) 453 socksize = sizeof(*sdl); 454 socksize = ROUNDUP(socksize); 455 sdl = malloc(socksize, M_IFADDR, M_WAITOK|M_ZERO); 456 sdl->sdl_len = socksize; 457 sdl->sdl_family = AF_LINK; 458 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 459 sdl->sdl_nlen = namelen; 460 sdl->sdl_alen = ifp->if_addrlen; 461 sdl->sdl_index = ifp->if_index; 462 sdl->sdl_type = ifp->if_type; 463 ifp->if_sadl = sdl; 464 } 465 466 /* 467 * Free the link level name for the specified interface. This is 468 * a detach helper. This is called from if_detach() or from 469 * link layer type specific detach functions. 470 */ 471 void 472 if_free_sadl(struct ifnet *ifp) 473 { 474 free(ifp->if_sadl, M_IFADDR, 0); 475 ifp->if_sadl = NULL; 476 } 477 478 void 479 if_attachdomain(struct ifnet *ifp) 480 { 481 struct domain *dp; 482 int i, s; 483 484 s = splnet(); 485 486 /* address family dependent data region */ 487 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 488 for (i = 0; (dp = domains[i]) != NULL; i++) { 489 if (dp->dom_ifattach) 490 ifp->if_afdata[dp->dom_family] = 491 (*dp->dom_ifattach)(ifp); 492 } 493 494 splx(s); 495 } 496 497 void 498 if_attachhead(struct ifnet *ifp) 499 { 500 if_attach_common(ifp); 501 TAILQ_INSERT_HEAD(&ifnet, ifp, if_list); 502 if_attachsetup(ifp); 503 } 504 505 void 506 if_attach(struct ifnet *ifp) 507 { 508 if_attach_common(ifp); 509 TAILQ_INSERT_TAIL(&ifnet, ifp, if_list); 510 if_attachsetup(ifp); 511 } 512 513 void 514 if_attach_common(struct ifnet *ifp) 515 { 516 TAILQ_INIT(&ifp->if_addrlist); 517 TAILQ_INIT(&ifp->if_maddrlist); 518 519 ifq_init(&ifp->if_snd, ifp); 520 521 ifp->if_addrhooks = malloc(sizeof(*ifp->if_addrhooks), 522 M_TEMP, M_WAITOK); 523 TAILQ_INIT(ifp->if_addrhooks); 524 ifp->if_linkstatehooks = malloc(sizeof(*ifp->if_linkstatehooks), 525 M_TEMP, M_WAITOK); 526 TAILQ_INIT(ifp->if_linkstatehooks); 527 ifp->if_detachhooks = malloc(sizeof(*ifp->if_detachhooks), 528 M_TEMP, M_WAITOK); 529 TAILQ_INIT(ifp->if_detachhooks); 530 531 if (ifp->if_rtrequest == NULL) 532 ifp->if_rtrequest = if_rtrequest_dummy; 533 ifp->if_slowtimo = malloc(sizeof(*ifp->if_slowtimo), M_TEMP, 534 M_WAITOK|M_ZERO); 535 ifp->if_watchdogtask = malloc(sizeof(*ifp->if_watchdogtask), 536 M_TEMP, M_WAITOK|M_ZERO); 537 ifp->if_linkstatetask = malloc(sizeof(*ifp->if_linkstatetask), 538 M_TEMP, M_WAITOK|M_ZERO); 539 ifp->if_llprio = IFQ_DEFPRIO; 540 541 SRPL_INIT(&ifp->if_inputs); 542 } 543 544 void 545 if_start(struct ifnet *ifp) 546 { 547 if (ISSET(ifp->if_xflags, IFXF_MPSAFE)) 548 ifq_start(&ifp->if_snd); 549 else 550 if_start_locked(ifp); 551 } 552 553 void 554 if_start_locked(struct ifnet *ifp) 555 { 556 int s; 557 558 KERNEL_LOCK(); 559 s = splnet(); 560 ifp->if_start(ifp); 561 splx(s); 562 KERNEL_UNLOCK(); 563 } 564 565 int 566 if_enqueue(struct ifnet *ifp, struct mbuf *m) 567 { 568 int length, error = 0; 569 unsigned short mflags; 570 571 #if NBRIDGE > 0 572 if (ifp->if_bridgeport && (m->m_flags & M_PROTO1) == 0) { 573 KERNEL_LOCK(); 574 error = bridge_output(ifp, m, NULL, NULL); 575 KERNEL_UNLOCK(); 576 return (error); 577 } 578 #endif 579 580 length = m->m_pkthdr.len; 581 mflags = m->m_flags; 582 583 /* 584 * Queue message on interface, and start output if interface 585 * not yet active. 586 */ 587 IFQ_ENQUEUE(&ifp->if_snd, m, error); 588 if (error) 589 return (error); 590 591 ifp->if_obytes += length; 592 if (mflags & M_MCAST) 593 ifp->if_omcasts++; 594 595 if_start(ifp); 596 597 return (0); 598 } 599 600 void 601 if_input(struct ifnet *ifp, struct mbuf_list *ml) 602 { 603 struct mbuf *m; 604 size_t ibytes = 0; 605 #if NBPFILTER > 0 606 caddr_t if_bpf; 607 #endif 608 609 MBUF_LIST_FOREACH(ml, m) { 610 m->m_pkthdr.ph_ifidx = ifp->if_index; 611 m->m_pkthdr.ph_rtableid = ifp->if_rdomain; 612 ibytes += m->m_pkthdr.len; 613 } 614 615 ifp->if_ipackets += ml_len(ml); 616 ifp->if_ibytes += ibytes; 617 618 #if NBPFILTER > 0 619 if_bpf = ifp->if_bpf; 620 if (if_bpf) { 621 struct mbuf_list ml0; 622 623 ml_init(&ml0); 624 ml_enlist(&ml0, ml); 625 ml_init(ml); 626 627 while ((m = ml_dequeue(&ml0)) != NULL) { 628 if (bpf_mtap_ether(if_bpf, m, BPF_DIRECTION_IN)) 629 m_freem(m); 630 else 631 ml_enqueue(ml, m); 632 } 633 } 634 #endif 635 636 mq_enlist(&if_input_queue, ml); 637 task_add(softnettq, &if_input_task); 638 } 639 640 int 641 if_input_local(struct ifnet *ifp, struct mbuf *m, sa_family_t af) 642 { 643 struct niqueue *ifq = NULL; 644 645 #if NBPFILTER > 0 646 /* 647 * Only send packets to bpf if they are destinated to local 648 * addresses. 649 * 650 * if_input_local() is also called for SIMPLEX interfaces to 651 * duplicate packets for local use. But don't dup them to bpf. 652 */ 653 if (ifp->if_flags & IFF_LOOPBACK) { 654 caddr_t if_bpf = ifp->if_bpf; 655 656 if (if_bpf) 657 bpf_mtap_af(if_bpf, af, m, BPF_DIRECTION_OUT); 658 } 659 #endif 660 m->m_pkthdr.ph_ifidx = ifp->if_index; 661 662 ifp->if_opackets++; 663 ifp->if_obytes += m->m_pkthdr.len; 664 665 switch (af) { 666 case AF_INET: 667 ifq = &ipintrq; 668 break; 669 #ifdef INET6 670 case AF_INET6: 671 ifq = &ip6intrq; 672 break; 673 #endif /* INET6 */ 674 #ifdef MPLS 675 case AF_MPLS: 676 ifp->if_ipackets++; 677 ifp->if_ibytes += m->m_pkthdr.len; 678 mpls_input(m); 679 return (0); 680 #endif /* MPLS */ 681 default: 682 printf("%s: can't handle af%d\n", ifp->if_xname, af); 683 m_freem(m); 684 return (EAFNOSUPPORT); 685 } 686 687 if (niq_enqueue(ifq, m) != 0) 688 return (ENOBUFS); 689 690 ifp->if_ipackets++; 691 ifp->if_ibytes += m->m_pkthdr.len; 692 693 return (0); 694 } 695 696 struct ifih { 697 SRPL_ENTRY(ifih) ifih_next; 698 int (*ifih_input)(struct ifnet *, struct mbuf *, 699 void *); 700 void *ifih_cookie; 701 int ifih_refcnt; 702 struct refcnt ifih_srpcnt; 703 }; 704 705 void if_ih_ref(void *, void *); 706 void if_ih_unref(void *, void *); 707 708 struct srpl_rc ifih_rc = SRPL_RC_INITIALIZER(if_ih_ref, if_ih_unref, NULL); 709 710 void 711 if_ih_insert(struct ifnet *ifp, int (*input)(struct ifnet *, struct mbuf *, 712 void *), void *cookie) 713 { 714 struct ifih *ifih; 715 716 /* the kernel lock guarantees serialised modifications to if_inputs */ 717 KERNEL_ASSERT_LOCKED(); 718 719 SRPL_FOREACH_LOCKED(ifih, &ifp->if_inputs, ifih_next) { 720 if (ifih->ifih_input == input && ifih->ifih_cookie == cookie) { 721 ifih->ifih_refcnt++; 722 break; 723 } 724 } 725 726 if (ifih == NULL) { 727 ifih = malloc(sizeof(*ifih), M_DEVBUF, M_WAITOK); 728 729 ifih->ifih_input = input; 730 ifih->ifih_cookie = cookie; 731 ifih->ifih_refcnt = 1; 732 refcnt_init(&ifih->ifih_srpcnt); 733 SRPL_INSERT_HEAD_LOCKED(&ifih_rc, &ifp->if_inputs, 734 ifih, ifih_next); 735 } 736 } 737 738 void 739 if_ih_ref(void *null, void *i) 740 { 741 struct ifih *ifih = i; 742 743 refcnt_take(&ifih->ifih_srpcnt); 744 } 745 746 void 747 if_ih_unref(void *null, void *i) 748 { 749 struct ifih *ifih = i; 750 751 refcnt_rele_wake(&ifih->ifih_srpcnt); 752 } 753 754 void 755 if_ih_remove(struct ifnet *ifp, int (*input)(struct ifnet *, struct mbuf *, 756 void *), void *cookie) 757 { 758 struct ifih *ifih; 759 760 /* the kernel lock guarantees serialised modifications to if_inputs */ 761 KERNEL_ASSERT_LOCKED(); 762 763 SRPL_FOREACH_LOCKED(ifih, &ifp->if_inputs, ifih_next) { 764 if (ifih->ifih_input == input && ifih->ifih_cookie == cookie) 765 break; 766 } 767 768 KASSERT(ifih != NULL); 769 770 if (--ifih->ifih_refcnt == 0) { 771 SRPL_REMOVE_LOCKED(&ifih_rc, &ifp->if_inputs, ifih, 772 ifih, ifih_next); 773 774 refcnt_finalize(&ifih->ifih_srpcnt, "ifihrm"); 775 free(ifih, M_DEVBUF, sizeof(*ifih)); 776 } 777 } 778 779 void 780 if_input_process(void *xmq) 781 { 782 struct mbuf_queue *mq = xmq; 783 struct mbuf_list ml; 784 struct mbuf *m; 785 struct ifnet *ifp; 786 struct ifih *ifih; 787 struct srp_ref sr; 788 int s; 789 790 mq_delist(mq, &ml); 791 if (ml_empty(&ml)) 792 return; 793 794 add_net_randomness(ml_len(&ml)); 795 796 s = splnet(); 797 while ((m = ml_dequeue(&ml)) != NULL) { 798 ifp = if_get(m->m_pkthdr.ph_ifidx); 799 if (ifp == NULL) { 800 m_freem(m); 801 continue; 802 } 803 804 /* 805 * Pass this mbuf to all input handlers of its 806 * interface until it is consumed. 807 */ 808 SRPL_FOREACH(ifih, &sr, &ifp->if_inputs, ifih_next) { 809 if ((*ifih->ifih_input)(ifp, m, ifih->ifih_cookie)) 810 break; 811 } 812 SRPL_LEAVE(&sr); 813 814 if (ifih == NULL) 815 m_freem(m); 816 817 if_put(ifp); 818 } 819 splx(s); 820 } 821 822 void 823 if_netisr(void *unused) 824 { 825 int n, t = 0; 826 int s; 827 828 KERNEL_LOCK(); 829 s = splsoftnet(); 830 831 while ((n = netisr) != 0) { 832 sched_pause(); 833 834 atomic_clearbits_int(&netisr, n); 835 836 #if NETHER > 0 837 if (n & (1 << NETISR_ARP)) 838 arpintr(); 839 #endif 840 if (n & (1 << NETISR_IP)) 841 ipintr(); 842 #ifdef INET6 843 if (n & (1 << NETISR_IPV6)) 844 ip6intr(); 845 #endif 846 #if NPPP > 0 847 if (n & (1 << NETISR_PPP)) 848 pppintr(); 849 #endif 850 #if NBRIDGE > 0 851 if (n & (1 << NETISR_BRIDGE)) 852 bridgeintr(); 853 #endif 854 #if NPPPOE > 0 855 if (n & (1 << NETISR_PPPOE)) 856 pppoeintr(); 857 #endif 858 t |= n; 859 } 860 861 #if NPFSYNC > 0 862 if (t & (1 << NETISR_PFSYNC)) 863 pfsyncintr(); 864 #endif 865 866 splx(s); 867 KERNEL_UNLOCK(); 868 } 869 870 void 871 if_deactivate(struct ifnet *ifp) 872 { 873 int s; 874 875 s = splnet(); 876 877 /* 878 * Call detach hooks from head to tail. To make sure detach 879 * hooks are executed in the reverse order they were added, all 880 * the hooks have to be added to the head! 881 */ 882 dohooks(ifp->if_detachhooks, HOOK_REMOVE | HOOK_FREE); 883 884 #if NBRIDGE > 0 885 /* Remove the interface from any bridge it is part of. */ 886 if (ifp->if_bridgeport) 887 bridge_ifdetach(ifp); 888 #endif 889 890 #if NCARP > 0 891 /* Remove the interface from any carp group it is a part of. */ 892 if (ifp->if_carp && ifp->if_type != IFT_CARP) 893 carp_ifdetach(ifp); 894 #endif 895 896 splx(s); 897 } 898 899 /* 900 * Detach an interface from everything in the kernel. Also deallocate 901 * private resources. 902 */ 903 void 904 if_detach(struct ifnet *ifp) 905 { 906 struct ifaddr *ifa; 907 struct ifg_list *ifg; 908 struct domain *dp; 909 int i, s; 910 911 /* Undo pseudo-driver changes. */ 912 if_deactivate(ifp); 913 914 ifq_clr_oactive(&ifp->if_snd); 915 916 s = splnet(); 917 /* Other CPUs must not have a reference before we start destroying. */ 918 if_idxmap_remove(ifp); 919 920 ifp->if_start = if_detached_start; 921 ifp->if_ioctl = if_detached_ioctl; 922 ifp->if_watchdog = NULL; 923 924 /* Remove the watchdog timeout & task */ 925 timeout_del(ifp->if_slowtimo); 926 task_del(systq, ifp->if_watchdogtask); 927 928 /* Remove the link state task */ 929 task_del(systq, ifp->if_linkstatetask); 930 931 #if NBPFILTER > 0 932 bpfdetach(ifp); 933 #endif 934 rt_if_remove(ifp); 935 rti_delete(ifp); 936 #if NETHER > 0 && defined(NFSCLIENT) 937 if (ifp->if_index == revarp_ifidx) 938 revarp_ifidx = 0; 939 #endif 940 #ifdef MROUTING 941 vif_delete(ifp); 942 #endif 943 in_ifdetach(ifp); 944 #ifdef INET6 945 in6_ifdetach(ifp); 946 #endif 947 #if NPF > 0 948 pfi_detach_ifnet(ifp); 949 #endif 950 951 /* Remove the interface from the list of all interfaces. */ 952 TAILQ_REMOVE(&ifnet, ifp, if_list); 953 954 while ((ifg = TAILQ_FIRST(&ifp->if_groups)) != NULL) 955 if_delgroup(ifp, ifg->ifgl_group->ifg_group); 956 957 if_free_sadl(ifp); 958 959 /* We should not have any address left at this point. */ 960 if (!TAILQ_EMPTY(&ifp->if_addrlist)) { 961 #ifdef DIAGNOSTIC 962 printf("%s: address list non empty\n", ifp->if_xname); 963 #endif 964 while ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) != NULL) { 965 ifa_del(ifp, ifa); 966 ifa->ifa_ifp = NULL; 967 ifafree(ifa); 968 } 969 } 970 971 free(ifp->if_addrhooks, M_TEMP, 0); 972 free(ifp->if_linkstatehooks, M_TEMP, 0); 973 free(ifp->if_detachhooks, M_TEMP, 0); 974 975 free(ifp->if_slowtimo, M_TEMP, sizeof(*ifp->if_slowtimo)); 976 free(ifp->if_watchdogtask, M_TEMP, sizeof(*ifp->if_watchdogtask)); 977 free(ifp->if_linkstatetask, M_TEMP, sizeof(*ifp->if_linkstatetask)); 978 979 for (i = 0; (dp = domains[i]) != NULL; i++) { 980 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 981 (*dp->dom_ifdetach)(ifp, 982 ifp->if_afdata[dp->dom_family]); 983 } 984 985 /* Announce that the interface is gone. */ 986 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 987 splx(s); 988 989 ifq_destroy(&ifp->if_snd); 990 } 991 992 /* 993 * Returns true if ``ifp0'' is connected to the interface with index ``ifidx''. 994 */ 995 int 996 if_isconnected(const struct ifnet *ifp0, unsigned int ifidx) 997 { 998 struct ifnet *ifp; 999 int connected = 0; 1000 1001 ifp = if_get(ifidx); 1002 if (ifp == NULL) 1003 return (0); 1004 1005 if (ifp0->if_index == ifp->if_index) 1006 connected = 1; 1007 1008 #if NBRIDGE > 0 1009 if (SAME_BRIDGE(ifp0->if_bridgeport, ifp->if_bridgeport)) 1010 connected = 1; 1011 #endif 1012 #if NCARP > 0 1013 if ((ifp0->if_type == IFT_CARP && ifp0->if_carpdev == ifp) || 1014 (ifp->if_type == IFT_CARP && ifp->if_carpdev == ifp0)) 1015 connected = 1; 1016 #endif 1017 1018 if_put(ifp); 1019 return (connected); 1020 } 1021 1022 /* 1023 * Create a clone network interface. 1024 */ 1025 int 1026 if_clone_create(const char *name) 1027 { 1028 struct if_clone *ifc; 1029 struct ifnet *ifp; 1030 int unit, ret; 1031 1032 ifc = if_clone_lookup(name, &unit); 1033 if (ifc == NULL) 1034 return (EINVAL); 1035 1036 if (ifunit(name) != NULL) 1037 return (EEXIST); 1038 1039 if ((ret = (*ifc->ifc_create)(ifc, unit)) == 0 && 1040 (ifp = ifunit(name)) != NULL) 1041 if_addgroup(ifp, ifc->ifc_name); 1042 1043 return (ret); 1044 } 1045 1046 /* 1047 * Destroy a clone network interface. 1048 */ 1049 int 1050 if_clone_destroy(const char *name) 1051 { 1052 struct if_clone *ifc; 1053 struct ifnet *ifp; 1054 int s; 1055 1056 ifc = if_clone_lookup(name, NULL); 1057 if (ifc == NULL) 1058 return (EINVAL); 1059 1060 ifp = ifunit(name); 1061 if (ifp == NULL) 1062 return (ENXIO); 1063 1064 if (ifc->ifc_destroy == NULL) 1065 return (EOPNOTSUPP); 1066 1067 if (ifp->if_flags & IFF_UP) { 1068 s = splnet(); 1069 if_down(ifp); 1070 splx(s); 1071 } 1072 1073 return ((*ifc->ifc_destroy)(ifp)); 1074 } 1075 1076 /* 1077 * Look up a network interface cloner. 1078 */ 1079 struct if_clone * 1080 if_clone_lookup(const char *name, int *unitp) 1081 { 1082 struct if_clone *ifc; 1083 const char *cp; 1084 int unit; 1085 1086 /* separate interface name from unit */ 1087 for (cp = name; 1088 cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9'); 1089 cp++) 1090 continue; 1091 1092 if (cp == name || cp - name == IFNAMSIZ || !*cp) 1093 return (NULL); /* No name or unit number */ 1094 1095 if (cp - name < IFNAMSIZ-1 && *cp == '0' && cp[1] != '\0') 1096 return (NULL); /* unit number 0 padded */ 1097 1098 LIST_FOREACH(ifc, &if_cloners, ifc_list) { 1099 if (strlen(ifc->ifc_name) == cp - name && 1100 !strncmp(name, ifc->ifc_name, cp - name)) 1101 break; 1102 } 1103 1104 if (ifc == NULL) 1105 return (NULL); 1106 1107 unit = 0; 1108 while (cp - name < IFNAMSIZ && *cp) { 1109 if (*cp < '0' || *cp > '9' || 1110 unit > (INT_MAX - (*cp - '0')) / 10) { 1111 /* Bogus unit number. */ 1112 return (NULL); 1113 } 1114 unit = (unit * 10) + (*cp++ - '0'); 1115 } 1116 1117 if (unitp != NULL) 1118 *unitp = unit; 1119 return (ifc); 1120 } 1121 1122 /* 1123 * Register a network interface cloner. 1124 */ 1125 void 1126 if_clone_attach(struct if_clone *ifc) 1127 { 1128 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list); 1129 if_cloners_count++; 1130 } 1131 1132 /* 1133 * Unregister a network interface cloner. 1134 */ 1135 void 1136 if_clone_detach(struct if_clone *ifc) 1137 { 1138 1139 LIST_REMOVE(ifc, ifc_list); 1140 if_cloners_count--; 1141 } 1142 1143 /* 1144 * Provide list of interface cloners to userspace. 1145 */ 1146 int 1147 if_clone_list(struct if_clonereq *ifcr) 1148 { 1149 char outbuf[IFNAMSIZ], *dst; 1150 struct if_clone *ifc; 1151 int count, error = 0; 1152 1153 ifcr->ifcr_total = if_cloners_count; 1154 if ((dst = ifcr->ifcr_buffer) == NULL) { 1155 /* Just asking how many there are. */ 1156 return (0); 1157 } 1158 1159 if (ifcr->ifcr_count < 0) 1160 return (EINVAL); 1161 1162 count = (if_cloners_count < ifcr->ifcr_count) ? 1163 if_cloners_count : ifcr->ifcr_count; 1164 1165 LIST_FOREACH(ifc, &if_cloners, ifc_list) { 1166 if (count == 0) 1167 break; 1168 bzero(outbuf, sizeof outbuf); 1169 strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ); 1170 error = copyout(outbuf, dst, IFNAMSIZ); 1171 if (error) 1172 break; 1173 count--; 1174 dst += IFNAMSIZ; 1175 } 1176 1177 return (error); 1178 } 1179 1180 /* 1181 * set queue congestion marker 1182 */ 1183 void 1184 if_congestion(void) 1185 { 1186 extern int ticks; 1187 1188 ifq_congestion = ticks; 1189 } 1190 1191 int 1192 if_congested(void) 1193 { 1194 extern int ticks; 1195 int diff; 1196 1197 diff = ticks - ifq_congestion; 1198 if (diff < 0) { 1199 ifq_congestion = ticks - hz; 1200 return (0); 1201 } 1202 1203 return (diff <= (hz / 100)); 1204 } 1205 1206 #define equal(a1, a2) \ 1207 (bcmp((caddr_t)(a1), (caddr_t)(a2), \ 1208 ((struct sockaddr *)(a1))->sa_len) == 0) 1209 1210 /* 1211 * Locate an interface based on a complete address. 1212 */ 1213 struct ifaddr * 1214 ifa_ifwithaddr(struct sockaddr *addr, u_int rtableid) 1215 { 1216 struct ifnet *ifp; 1217 struct ifaddr *ifa; 1218 u_int rdomain; 1219 1220 KERNEL_ASSERT_LOCKED(); 1221 rdomain = rtable_l2(rtableid); 1222 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1223 if (ifp->if_rdomain != rdomain) 1224 continue; 1225 1226 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1227 if (ifa->ifa_addr->sa_family != addr->sa_family) 1228 continue; 1229 1230 if (equal(addr, ifa->ifa_addr)) 1231 return (ifa); 1232 } 1233 } 1234 return (NULL); 1235 } 1236 1237 /* 1238 * Locate the point to point interface with a given destination address. 1239 */ 1240 struct ifaddr * 1241 ifa_ifwithdstaddr(struct sockaddr *addr, u_int rdomain) 1242 { 1243 struct ifnet *ifp; 1244 struct ifaddr *ifa; 1245 1246 KERNEL_ASSERT_LOCKED(); 1247 rdomain = rtable_l2(rdomain); 1248 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1249 if (ifp->if_rdomain != rdomain) 1250 continue; 1251 if (ifp->if_flags & IFF_POINTOPOINT) 1252 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1253 if (ifa->ifa_addr->sa_family != 1254 addr->sa_family || ifa->ifa_dstaddr == NULL) 1255 continue; 1256 if (equal(addr, ifa->ifa_dstaddr)) 1257 return (ifa); 1258 } 1259 } 1260 return (NULL); 1261 } 1262 1263 /* 1264 * Find an interface on a specific network. If many, choice 1265 * is most specific found. 1266 */ 1267 struct ifaddr * 1268 ifa_ifwithnet(struct sockaddr *sa, u_int rtableid) 1269 { 1270 struct ifnet *ifp; 1271 struct ifaddr *ifa, *ifa_maybe = NULL; 1272 char *cplim, *addr_data = sa->sa_data; 1273 u_int rdomain; 1274 1275 KERNEL_ASSERT_LOCKED(); 1276 rdomain = rtable_l2(rtableid); 1277 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1278 if (ifp->if_rdomain != rdomain) 1279 continue; 1280 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1281 char *cp, *cp2, *cp3; 1282 1283 if (ifa->ifa_addr->sa_family != sa->sa_family || 1284 ifa->ifa_netmask == 0) 1285 next: continue; 1286 cp = addr_data; 1287 cp2 = ifa->ifa_addr->sa_data; 1288 cp3 = ifa->ifa_netmask->sa_data; 1289 cplim = (char *)ifa->ifa_netmask + 1290 ifa->ifa_netmask->sa_len; 1291 while (cp3 < cplim) 1292 if ((*cp++ ^ *cp2++) & *cp3++) 1293 /* want to continue for() loop */ 1294 goto next; 1295 if (ifa_maybe == 0 || 1296 rn_refines((caddr_t)ifa->ifa_netmask, 1297 (caddr_t)ifa_maybe->ifa_netmask)) 1298 ifa_maybe = ifa; 1299 } 1300 } 1301 return (ifa_maybe); 1302 } 1303 1304 /* 1305 * Find an interface address specific to an interface best matching 1306 * a given address. 1307 */ 1308 struct ifaddr * 1309 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 1310 { 1311 struct ifaddr *ifa; 1312 char *cp, *cp2, *cp3; 1313 char *cplim; 1314 struct ifaddr *ifa_maybe = NULL; 1315 u_int af = addr->sa_family; 1316 1317 if (af >= AF_MAX) 1318 return (NULL); 1319 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1320 if (ifa->ifa_addr->sa_family != af) 1321 continue; 1322 if (ifa_maybe == NULL) 1323 ifa_maybe = ifa; 1324 if (ifa->ifa_netmask == 0 || ifp->if_flags & IFF_POINTOPOINT) { 1325 if (equal(addr, ifa->ifa_addr) || 1326 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))) 1327 return (ifa); 1328 continue; 1329 } 1330 cp = addr->sa_data; 1331 cp2 = ifa->ifa_addr->sa_data; 1332 cp3 = ifa->ifa_netmask->sa_data; 1333 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1334 for (; cp3 < cplim; cp3++) 1335 if ((*cp++ ^ *cp2++) & *cp3) 1336 break; 1337 if (cp3 == cplim) 1338 return (ifa); 1339 } 1340 return (ifa_maybe); 1341 } 1342 1343 void 1344 if_rtrequest_dummy(struct ifnet *ifp, int req, struct rtentry *rt) 1345 { 1346 } 1347 1348 /* 1349 * Default action when installing a local route on a point-to-point 1350 * interface. 1351 */ 1352 void 1353 p2p_rtrequest(struct ifnet *ifp, int req, struct rtentry *rt) 1354 { 1355 struct ifnet *lo0ifp; 1356 struct ifaddr *ifa, *lo0ifa; 1357 1358 switch (req) { 1359 case RTM_ADD: 1360 if (!ISSET(rt->rt_flags, RTF_LOCAL)) 1361 break; 1362 1363 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1364 if (memcmp(rt_key(rt), ifa->ifa_addr, 1365 rt_key(rt)->sa_len) == 0) 1366 break; 1367 } 1368 1369 if (ifa == NULL) 1370 break; 1371 1372 KASSERT(ifa == rt->rt_ifa); 1373 1374 /* 1375 * XXX Since lo0 is in the default rdomain we should not 1376 * (ab)use it for any route related to an interface of a 1377 * different rdomain. 1378 */ 1379 lo0ifp = if_get(lo0ifidx); 1380 KASSERT(lo0ifp != NULL); 1381 TAILQ_FOREACH(lo0ifa, &lo0ifp->if_addrlist, ifa_list) { 1382 if (lo0ifa->ifa_addr->sa_family == 1383 ifa->ifa_addr->sa_family) 1384 break; 1385 } 1386 if_put(lo0ifp); 1387 1388 if (lo0ifa == NULL) 1389 break; 1390 1391 rt->rt_flags &= ~RTF_LLINFO; 1392 break; 1393 case RTM_DELETE: 1394 case RTM_RESOLVE: 1395 default: 1396 break; 1397 } 1398 } 1399 1400 1401 /* 1402 * Bring down all interfaces 1403 */ 1404 void 1405 if_downall(void) 1406 { 1407 struct ifreq ifrq; /* XXX only partly built */ 1408 struct ifnet *ifp; 1409 int s; 1410 1411 s = splnet(); 1412 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1413 if ((ifp->if_flags & IFF_UP) == 0) 1414 continue; 1415 if_down(ifp); 1416 ifp->if_flags &= ~IFF_UP; 1417 1418 if (ifp->if_ioctl) { 1419 ifrq.ifr_flags = ifp->if_flags; 1420 (void) (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, 1421 (caddr_t)&ifrq); 1422 } 1423 } 1424 splx(s); 1425 } 1426 1427 /* 1428 * Mark an interface down and notify protocols of 1429 * the transition. 1430 */ 1431 void 1432 if_down(struct ifnet *ifp) 1433 { 1434 struct ifaddr *ifa; 1435 1436 splsoftassert(IPL_SOFTNET); 1437 1438 ifp->if_flags &= ~IFF_UP; 1439 microtime(&ifp->if_lastchange); 1440 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1441 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1442 } 1443 IFQ_PURGE(&ifp->if_snd); 1444 1445 if_linkstate(ifp); 1446 } 1447 1448 /* 1449 * Mark an interface up and notify protocols of 1450 * the transition. 1451 */ 1452 void 1453 if_up(struct ifnet *ifp) 1454 { 1455 splsoftassert(IPL_SOFTNET); 1456 1457 ifp->if_flags |= IFF_UP; 1458 microtime(&ifp->if_lastchange); 1459 1460 #ifdef INET6 1461 /* Userland expects the kernel to set ::1 on lo0. */ 1462 if (ifp->if_index == lo0ifidx) 1463 in6_ifattach(ifp); 1464 #endif 1465 1466 if_linkstate(ifp); 1467 } 1468 1469 /* 1470 * Notify userland, the routing table and hooks owner of 1471 * a link-state transition. 1472 */ 1473 void 1474 if_linkstate(void *xifp) 1475 { 1476 struct ifnet *ifp = xifp; 1477 int s; 1478 1479 s = splsoftnet(); 1480 rt_ifmsg(ifp); 1481 #ifndef SMALL_KERNEL 1482 rt_if_track(ifp); 1483 #endif 1484 dohooks(ifp->if_linkstatehooks, 0); 1485 splx(s); 1486 } 1487 1488 /* 1489 * Schedule a link state change task. 1490 */ 1491 void 1492 if_link_state_change(struct ifnet *ifp) 1493 { 1494 task_add(systq, ifp->if_linkstatetask); 1495 } 1496 1497 /* 1498 * Handle interface watchdog timer routine. Called 1499 * from softclock, we decrement timer (if set) and 1500 * call the appropriate interface routine on expiration. 1501 */ 1502 void 1503 if_slowtimo(void *arg) 1504 { 1505 struct ifnet *ifp = arg; 1506 int s = splnet(); 1507 1508 if (ifp->if_watchdog) { 1509 if (ifp->if_timer > 0 && --ifp->if_timer == 0) 1510 task_add(systq, ifp->if_watchdogtask); 1511 timeout_add(ifp->if_slowtimo, hz / IFNET_SLOWHZ); 1512 } 1513 splx(s); 1514 } 1515 1516 void 1517 if_watchdog_task(void *arg) 1518 { 1519 struct ifnet *ifp = arg; 1520 int s; 1521 1522 s = splnet(); 1523 if (ifp->if_watchdog) 1524 (*ifp->if_watchdog)(ifp); 1525 splx(s); 1526 } 1527 1528 /* 1529 * Map interface name to interface structure pointer. 1530 */ 1531 struct ifnet * 1532 ifunit(const char *name) 1533 { 1534 struct ifnet *ifp; 1535 1536 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1537 if (strcmp(ifp->if_xname, name) == 0) 1538 return (ifp); 1539 } 1540 return (NULL); 1541 } 1542 1543 /* 1544 * Map interface index to interface structure pointer. 1545 */ 1546 struct ifnet * 1547 if_get(unsigned int index) 1548 { 1549 struct srp_ref sr; 1550 struct if_map *if_map; 1551 struct srp *map; 1552 struct ifnet *ifp = NULL; 1553 1554 if_map = srp_enter(&sr, &if_idxmap.map); 1555 if (index < if_map->limit) { 1556 map = (struct srp *)(if_map + 1); 1557 1558 ifp = srp_follow(&sr, &map[index]); 1559 if (ifp != NULL) { 1560 KASSERT(ifp->if_index == index); 1561 if_ref(ifp); 1562 } 1563 } 1564 srp_leave(&sr); 1565 1566 return (ifp); 1567 } 1568 1569 struct ifnet * 1570 if_ref(struct ifnet *ifp) 1571 { 1572 refcnt_take(&ifp->if_refcnt); 1573 1574 return (ifp); 1575 } 1576 1577 void 1578 if_put(struct ifnet *ifp) 1579 { 1580 if (ifp == NULL) 1581 return; 1582 1583 refcnt_rele_wake(&ifp->if_refcnt); 1584 } 1585 1586 int 1587 if_setlladdr(struct ifnet *ifp, const uint8_t *lladdr) 1588 { 1589 if (ifp->if_sadl == NULL) 1590 return (EINVAL); 1591 1592 memcpy(((struct arpcom *)ifp)->ac_enaddr, lladdr, ETHER_ADDR_LEN); 1593 memcpy(LLADDR(ifp->if_sadl), lladdr, ETHER_ADDR_LEN); 1594 1595 return (0); 1596 } 1597 1598 /* 1599 * Interface ioctls. 1600 */ 1601 int 1602 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p) 1603 { 1604 struct ifnet *ifp; 1605 struct ifreq *ifr; 1606 struct sockaddr_dl *sdl; 1607 struct ifgroupreq *ifgr; 1608 struct if_afreq *ifar; 1609 char ifdescrbuf[IFDESCRSIZE]; 1610 char ifrtlabelbuf[RTLABEL_LEN]; 1611 int s, error = 0; 1612 size_t bytesdone; 1613 short oif_flags; 1614 const char *label; 1615 short up = 0; 1616 1617 switch (cmd) { 1618 1619 case SIOCGIFCONF: 1620 return (ifconf(cmd, data)); 1621 } 1622 ifr = (struct ifreq *)data; 1623 1624 switch (cmd) { 1625 case SIOCIFCREATE: 1626 case SIOCIFDESTROY: 1627 if ((error = suser(p, 0)) != 0) 1628 return (error); 1629 return ((cmd == SIOCIFCREATE) ? 1630 if_clone_create(ifr->ifr_name) : 1631 if_clone_destroy(ifr->ifr_name)); 1632 case SIOCIFGCLONERS: 1633 return (if_clone_list((struct if_clonereq *)data)); 1634 case SIOCGIFGMEMB: 1635 return (if_getgroupmembers(data)); 1636 case SIOCGIFGATTR: 1637 return (if_getgroupattribs(data)); 1638 case SIOCSIFGATTR: 1639 if ((error = suser(p, 0)) != 0) 1640 return (error); 1641 return (if_setgroupattribs(data)); 1642 case SIOCIFAFATTACH: 1643 case SIOCIFAFDETACH: 1644 if ((error = suser(p, 0)) != 0) 1645 return (error); 1646 ifar = (struct if_afreq *)data; 1647 if ((ifp = ifunit(ifar->ifar_name)) == NULL) 1648 return (ENXIO); 1649 switch (ifar->ifar_af) { 1650 case AF_INET: 1651 /* attach is a noop for AF_INET */ 1652 if (cmd == SIOCIFAFDETACH) { 1653 s = splsoftnet(); 1654 in_ifdetach(ifp); 1655 splx(s); 1656 } 1657 return (0); 1658 #ifdef INET6 1659 case AF_INET6: 1660 s = splsoftnet(); 1661 if (cmd == SIOCIFAFATTACH) 1662 error = in6_ifattach(ifp); 1663 else 1664 in6_ifdetach(ifp); 1665 splx(s); 1666 return (error); 1667 #endif /* INET6 */ 1668 default: 1669 return (EAFNOSUPPORT); 1670 } 1671 } 1672 1673 ifp = ifunit(ifr->ifr_name); 1674 if (ifp == 0) 1675 return (ENXIO); 1676 oif_flags = ifp->if_flags; 1677 switch (cmd) { 1678 1679 case SIOCGIFFLAGS: 1680 ifr->ifr_flags = ifp->if_flags; 1681 if (ifq_is_oactive(&ifp->if_snd)) 1682 ifr->ifr_flags |= IFF_OACTIVE; 1683 break; 1684 1685 case SIOCGIFXFLAGS: 1686 ifr->ifr_flags = ifp->if_xflags & ~IFXF_MPSAFE; 1687 break; 1688 1689 case SIOCGIFMETRIC: 1690 ifr->ifr_metric = ifp->if_metric; 1691 break; 1692 1693 case SIOCGIFMTU: 1694 ifr->ifr_mtu = ifp->if_mtu; 1695 break; 1696 1697 case SIOCGIFHARDMTU: 1698 ifr->ifr_hardmtu = ifp->if_hardmtu; 1699 break; 1700 1701 case SIOCGIFDATA: 1702 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data, 1703 sizeof(ifp->if_data)); 1704 break; 1705 1706 case SIOCSIFFLAGS: 1707 if ((error = suser(p, 0)) != 0) 1708 return (error); 1709 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) { 1710 s = splnet(); 1711 if_down(ifp); 1712 splx(s); 1713 } 1714 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) { 1715 s = splnet(); 1716 if_up(ifp); 1717 splx(s); 1718 } 1719 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1720 (ifr->ifr_flags & ~IFF_CANTCHANGE); 1721 if (ifp->if_ioctl) 1722 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1723 break; 1724 1725 case SIOCSIFXFLAGS: 1726 if ((error = suser(p, 0)) != 0) 1727 return (error); 1728 1729 #ifdef INET6 1730 if (ISSET(ifr->ifr_flags, IFXF_AUTOCONF6)) { 1731 s = splsoftnet(); 1732 error = in6_ifattach(ifp); 1733 splx(s); 1734 if (error != 0) 1735 return (error); 1736 } 1737 1738 if ((ifr->ifr_flags & IFXF_AUTOCONF6) && 1739 !(ifp->if_xflags & IFXF_AUTOCONF6)) { 1740 nd6_rs_attach(ifp); 1741 } 1742 1743 if ((ifp->if_xflags & IFXF_AUTOCONF6) && 1744 !(ifr->ifr_flags & IFXF_AUTOCONF6)) { 1745 nd6_rs_detach(ifp); 1746 } 1747 #endif /* INET6 */ 1748 1749 #ifdef MPLS 1750 if (ISSET(ifr->ifr_flags, IFXF_MPLS) && 1751 !ISSET(ifp->if_xflags, IFXF_MPLS)) { 1752 s = splnet(); 1753 ifp->if_xflags |= IFXF_MPLS; 1754 ifp->if_ll_output = ifp->if_output; 1755 ifp->if_output = mpls_output; 1756 splx(s); 1757 } 1758 if (ISSET(ifp->if_xflags, IFXF_MPLS) && 1759 !ISSET(ifr->ifr_flags, IFXF_MPLS)) { 1760 s = splnet(); 1761 ifp->if_xflags &= ~IFXF_MPLS; 1762 ifp->if_output = ifp->if_ll_output; 1763 ifp->if_ll_output = NULL; 1764 splx(s); 1765 } 1766 #endif /* MPLS */ 1767 1768 #ifndef SMALL_KERNEL 1769 if (ifp->if_capabilities & IFCAP_WOL) { 1770 if (ISSET(ifr->ifr_flags, IFXF_WOL) && 1771 !ISSET(ifp->if_xflags, IFXF_WOL)) { 1772 s = splnet(); 1773 ifp->if_xflags |= IFXF_WOL; 1774 error = ifp->if_wol(ifp, 1); 1775 splx(s); 1776 if (error) 1777 return (error); 1778 } 1779 if (ISSET(ifp->if_xflags, IFXF_WOL) && 1780 !ISSET(ifr->ifr_flags, IFXF_WOL)) { 1781 s = splnet(); 1782 ifp->if_xflags &= ~IFXF_WOL; 1783 error = ifp->if_wol(ifp, 0); 1784 splx(s); 1785 if (error) 1786 return (error); 1787 } 1788 } else if (ISSET(ifr->ifr_flags, IFXF_WOL)) { 1789 ifr->ifr_flags &= ~IFXF_WOL; 1790 error = ENOTSUP; 1791 } 1792 #endif 1793 1794 ifp->if_xflags = (ifp->if_xflags & IFXF_CANTCHANGE) | 1795 (ifr->ifr_flags & ~IFXF_CANTCHANGE); 1796 rt_ifmsg(ifp); 1797 break; 1798 1799 case SIOCSIFMETRIC: 1800 if ((error = suser(p, 0)) != 0) 1801 return (error); 1802 ifp->if_metric = ifr->ifr_metric; 1803 break; 1804 1805 case SIOCSIFMTU: 1806 if ((error = suser(p, 0)) != 0) 1807 return (error); 1808 if (ifp->if_ioctl == NULL) 1809 return (EOPNOTSUPP); 1810 error = (*ifp->if_ioctl)(ifp, cmd, data); 1811 break; 1812 1813 case SIOCSIFPHYADDR: 1814 case SIOCDIFPHYADDR: 1815 #ifdef INET6 1816 case SIOCSIFPHYADDR_IN6: 1817 #endif 1818 case SIOCSLIFPHYADDR: 1819 case SIOCSLIFPHYRTABLE: 1820 case SIOCSLIFPHYTTL: 1821 case SIOCADDMULTI: 1822 case SIOCDELMULTI: 1823 case SIOCSIFMEDIA: 1824 case SIOCSVNETID: 1825 case SIOCSIFPAIR: 1826 case SIOCSIFPARENT: 1827 case SIOCDIFPARENT: 1828 if ((error = suser(p, 0)) != 0) 1829 return (error); 1830 /* FALLTHROUGH */ 1831 case SIOCGIFPSRCADDR: 1832 case SIOCGIFPDSTADDR: 1833 case SIOCGLIFPHYADDR: 1834 case SIOCGLIFPHYRTABLE: 1835 case SIOCGLIFPHYTTL: 1836 case SIOCGIFMEDIA: 1837 case SIOCGVNETID: 1838 case SIOCGIFPAIR: 1839 case SIOCGIFPARENT: 1840 if (ifp->if_ioctl == 0) 1841 return (EOPNOTSUPP); 1842 error = (*ifp->if_ioctl)(ifp, cmd, data); 1843 break; 1844 1845 case SIOCGIFDESCR: 1846 strlcpy(ifdescrbuf, ifp->if_description, IFDESCRSIZE); 1847 error = copyoutstr(ifdescrbuf, ifr->ifr_data, IFDESCRSIZE, 1848 &bytesdone); 1849 break; 1850 1851 case SIOCSIFDESCR: 1852 if ((error = suser(p, 0)) != 0) 1853 return (error); 1854 error = copyinstr(ifr->ifr_data, ifdescrbuf, 1855 IFDESCRSIZE, &bytesdone); 1856 if (error == 0) { 1857 (void)memset(ifp->if_description, 0, IFDESCRSIZE); 1858 strlcpy(ifp->if_description, ifdescrbuf, IFDESCRSIZE); 1859 } 1860 break; 1861 1862 case SIOCGIFRTLABEL: 1863 if (ifp->if_rtlabelid && 1864 (label = rtlabel_id2name(ifp->if_rtlabelid)) != NULL) { 1865 strlcpy(ifrtlabelbuf, label, RTLABEL_LEN); 1866 error = copyoutstr(ifrtlabelbuf, ifr->ifr_data, 1867 RTLABEL_LEN, &bytesdone); 1868 } else 1869 error = ENOENT; 1870 break; 1871 1872 case SIOCSIFRTLABEL: 1873 if ((error = suser(p, 0)) != 0) 1874 return (error); 1875 error = copyinstr(ifr->ifr_data, ifrtlabelbuf, 1876 RTLABEL_LEN, &bytesdone); 1877 if (error == 0) { 1878 rtlabel_unref(ifp->if_rtlabelid); 1879 ifp->if_rtlabelid = rtlabel_name2id(ifrtlabelbuf); 1880 } 1881 break; 1882 1883 case SIOCGIFPRIORITY: 1884 ifr->ifr_metric = ifp->if_priority; 1885 break; 1886 1887 case SIOCSIFPRIORITY: 1888 if ((error = suser(p, 0)) != 0) 1889 return (error); 1890 if (ifr->ifr_metric < 0 || ifr->ifr_metric > 15) 1891 return (EINVAL); 1892 ifp->if_priority = ifr->ifr_metric; 1893 break; 1894 1895 case SIOCGIFRDOMAIN: 1896 ifr->ifr_rdomainid = ifp->if_rdomain; 1897 break; 1898 1899 case SIOCSIFRDOMAIN: 1900 if ((error = suser(p, 0)) != 0) 1901 return (error); 1902 if (ifr->ifr_rdomainid < 0 || 1903 ifr->ifr_rdomainid > RT_TABLEID_MAX) 1904 return (EINVAL); 1905 1906 /* make sure that the routing table exists */ 1907 if (!rtable_exists(ifr->ifr_rdomainid)) { 1908 s = splsoftnet(); 1909 if ((error = rtable_add(ifr->ifr_rdomainid)) == 0) 1910 rtable_l2set(ifr->ifr_rdomainid, ifr->ifr_rdomainid); 1911 splx(s); 1912 if (error) 1913 return (error); 1914 } 1915 1916 /* make sure that the routing table is a real rdomain */ 1917 if (ifr->ifr_rdomainid != rtable_l2(ifr->ifr_rdomainid)) 1918 return (EINVAL); 1919 1920 /* remove all routing entries when switching domains */ 1921 /* XXX hell this is ugly */ 1922 if (ifr->ifr_rdomainid != ifp->if_rdomain) { 1923 s = splnet(); 1924 if (ifp->if_flags & IFF_UP) 1925 up = 1; 1926 /* 1927 * We are tearing down the world. 1928 * Take down the IF so: 1929 * 1. everything that cares gets a message 1930 * 2. the automagic IPv6 bits are recreated 1931 */ 1932 if (up) 1933 if_down(ifp); 1934 rt_if_remove(ifp); 1935 rti_delete(ifp); 1936 #ifdef MROUTING 1937 vif_delete(ifp); 1938 #endif 1939 #ifdef INET6 1940 in6_ifdetach(ifp); 1941 #endif 1942 in_ifdetach(ifp); 1943 splx(s); 1944 } 1945 1946 /* Let devices like enc(4) or mpe(4) know about the change */ 1947 if ((error = (*ifp->if_ioctl)(ifp, cmd, data)) != ENOTTY) 1948 return (error); 1949 error = 0; 1950 1951 /* Add interface to the specified rdomain */ 1952 ifp->if_rdomain = ifr->ifr_rdomainid; 1953 break; 1954 1955 case SIOCAIFGROUP: 1956 if ((error = suser(p, 0))) 1957 return (error); 1958 ifgr = (struct ifgroupreq *)data; 1959 if ((error = if_addgroup(ifp, ifgr->ifgr_group))) 1960 return (error); 1961 (*ifp->if_ioctl)(ifp, cmd, data); /* XXX error check */ 1962 break; 1963 1964 case SIOCGIFGROUP: 1965 if ((error = if_getgroup(data, ifp))) 1966 return (error); 1967 break; 1968 1969 case SIOCDIFGROUP: 1970 if ((error = suser(p, 0))) 1971 return (error); 1972 (*ifp->if_ioctl)(ifp, cmd, data); /* XXX error check */ 1973 ifgr = (struct ifgroupreq *)data; 1974 if ((error = if_delgroup(ifp, ifgr->ifgr_group))) 1975 return (error); 1976 break; 1977 1978 case SIOCSIFLLADDR: 1979 if ((error = suser(p, 0))) 1980 return (error); 1981 sdl = ifp->if_sadl; 1982 if (sdl == NULL) 1983 return (EINVAL); 1984 if (ifr->ifr_addr.sa_len != ETHER_ADDR_LEN) 1985 return (EINVAL); 1986 if (ETHER_IS_MULTICAST(ifr->ifr_addr.sa_data)) 1987 return (EINVAL); 1988 switch (ifp->if_type) { 1989 case IFT_ETHER: 1990 case IFT_CARP: 1991 case IFT_XETHER: 1992 case IFT_ISO88025: 1993 if_setlladdr(ifp, ifr->ifr_addr.sa_data); 1994 error = (*ifp->if_ioctl)(ifp, cmd, data); 1995 if (error == ENOTTY) 1996 error = 0; 1997 break; 1998 default: 1999 return (ENODEV); 2000 } 2001 2002 ifnewlladdr(ifp); 2003 break; 2004 2005 case SIOCGIFLLPRIO: 2006 ifr->ifr_llprio = ifp->if_llprio; 2007 break; 2008 2009 case SIOCSIFLLPRIO: 2010 if ((error = suser(p, 0))) 2011 return (error); 2012 if (ifr->ifr_llprio > UCHAR_MAX) 2013 return (EINVAL); 2014 ifp->if_llprio = ifr->ifr_llprio; 2015 break; 2016 2017 default: 2018 if (so->so_proto == 0) 2019 return (EOPNOTSUPP); 2020 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, 2021 (struct mbuf *) cmd, (struct mbuf *) data, 2022 (struct mbuf *) ifp, p)); 2023 break; 2024 } 2025 2026 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) 2027 microtime(&ifp->if_lastchange); 2028 2029 /* If we took down the IF, bring it back */ 2030 if (up) { 2031 s = splnet(); 2032 if_up(ifp); 2033 splx(s); 2034 } 2035 return (error); 2036 } 2037 2038 /* 2039 * Return interface configuration 2040 * of system. List may be used 2041 * in later ioctl's (above) to get 2042 * other information. 2043 */ 2044 int 2045 ifconf(u_long cmd, caddr_t data) 2046 { 2047 struct ifconf *ifc = (struct ifconf *)data; 2048 struct ifnet *ifp; 2049 struct ifaddr *ifa; 2050 struct ifreq ifr, *ifrp; 2051 int space = ifc->ifc_len, error = 0; 2052 2053 /* If ifc->ifc_len is 0, fill it in with the needed size and return. */ 2054 if (space == 0) { 2055 TAILQ_FOREACH(ifp, &ifnet, if_list) { 2056 struct sockaddr *sa; 2057 2058 if (TAILQ_EMPTY(&ifp->if_addrlist)) 2059 space += sizeof (ifr); 2060 else 2061 TAILQ_FOREACH(ifa, 2062 &ifp->if_addrlist, ifa_list) { 2063 sa = ifa->ifa_addr; 2064 if (sa->sa_len > sizeof(*sa)) 2065 space += sa->sa_len - 2066 sizeof(*sa); 2067 space += sizeof(ifr); 2068 } 2069 } 2070 ifc->ifc_len = space; 2071 return (0); 2072 } 2073 2074 ifrp = ifc->ifc_req; 2075 TAILQ_FOREACH(ifp, &ifnet, if_list) { 2076 if (space < sizeof(ifr)) 2077 break; 2078 bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ); 2079 if (TAILQ_EMPTY(&ifp->if_addrlist)) { 2080 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 2081 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 2082 sizeof(ifr)); 2083 if (error) 2084 break; 2085 space -= sizeof (ifr), ifrp++; 2086 } else 2087 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 2088 struct sockaddr *sa = ifa->ifa_addr; 2089 2090 if (space < sizeof(ifr)) 2091 break; 2092 if (sa->sa_len <= sizeof(*sa)) { 2093 ifr.ifr_addr = *sa; 2094 error = copyout((caddr_t)&ifr, 2095 (caddr_t)ifrp, sizeof (ifr)); 2096 ifrp++; 2097 } else { 2098 space -= sa->sa_len - sizeof(*sa); 2099 if (space < sizeof (ifr)) 2100 break; 2101 error = copyout((caddr_t)&ifr, 2102 (caddr_t)ifrp, 2103 sizeof(ifr.ifr_name)); 2104 if (error == 0) 2105 error = copyout((caddr_t)sa, 2106 (caddr_t)&ifrp->ifr_addr, 2107 sa->sa_len); 2108 ifrp = (struct ifreq *)(sa->sa_len + 2109 (caddr_t)&ifrp->ifr_addr); 2110 } 2111 if (error) 2112 break; 2113 space -= sizeof (ifr); 2114 } 2115 } 2116 ifc->ifc_len -= space; 2117 return (error); 2118 } 2119 2120 /* 2121 * Dummy functions replaced in ifnet during detach (if protocols decide to 2122 * fiddle with the if during detach. 2123 */ 2124 void 2125 if_detached_start(struct ifnet *ifp) 2126 { 2127 IFQ_PURGE(&ifp->if_snd); 2128 } 2129 2130 int 2131 if_detached_ioctl(struct ifnet *ifp, u_long a, caddr_t b) 2132 { 2133 return ENODEV; 2134 } 2135 2136 /* 2137 * Create interface group without members 2138 */ 2139 struct ifg_group * 2140 if_creategroup(const char *groupname) 2141 { 2142 struct ifg_group *ifg; 2143 2144 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) 2145 return (NULL); 2146 2147 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 2148 ifg->ifg_refcnt = 0; 2149 ifg->ifg_carp_demoted = 0; 2150 TAILQ_INIT(&ifg->ifg_members); 2151 #if NPF > 0 2152 pfi_attach_ifgroup(ifg); 2153 #endif 2154 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next); 2155 2156 return (ifg); 2157 } 2158 2159 /* 2160 * Add a group to an interface 2161 */ 2162 int 2163 if_addgroup(struct ifnet *ifp, const char *groupname) 2164 { 2165 struct ifg_list *ifgl; 2166 struct ifg_group *ifg = NULL; 2167 struct ifg_member *ifgm; 2168 2169 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 2170 groupname[strlen(groupname) - 1] <= '9') 2171 return (EINVAL); 2172 2173 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 2174 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 2175 return (EEXIST); 2176 2177 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) 2178 return (ENOMEM); 2179 2180 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) { 2181 free(ifgl, M_TEMP, sizeof(*ifgl)); 2182 return (ENOMEM); 2183 } 2184 2185 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) 2186 if (!strcmp(ifg->ifg_group, groupname)) 2187 break; 2188 2189 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) { 2190 free(ifgl, M_TEMP, sizeof(*ifgl)); 2191 free(ifgm, M_TEMP, sizeof(*ifgm)); 2192 return (ENOMEM); 2193 } 2194 2195 ifg->ifg_refcnt++; 2196 ifgl->ifgl_group = ifg; 2197 ifgm->ifgm_ifp = ifp; 2198 2199 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 2200 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 2201 2202 #if NPF > 0 2203 pfi_group_change(groupname); 2204 #endif 2205 2206 return (0); 2207 } 2208 2209 /* 2210 * Remove a group from an interface 2211 */ 2212 int 2213 if_delgroup(struct ifnet *ifp, const char *groupname) 2214 { 2215 struct ifg_list *ifgl; 2216 struct ifg_member *ifgm; 2217 2218 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 2219 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 2220 break; 2221 if (ifgl == NULL) 2222 return (ENOENT); 2223 2224 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 2225 2226 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 2227 if (ifgm->ifgm_ifp == ifp) 2228 break; 2229 2230 if (ifgm != NULL) { 2231 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); 2232 free(ifgm, M_TEMP, sizeof(*ifgm)); 2233 } 2234 2235 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 2236 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next); 2237 #if NPF > 0 2238 pfi_detach_ifgroup(ifgl->ifgl_group); 2239 #endif 2240 free(ifgl->ifgl_group, M_TEMP, 0); 2241 } 2242 2243 free(ifgl, M_TEMP, sizeof(*ifgl)); 2244 2245 #if NPF > 0 2246 pfi_group_change(groupname); 2247 #endif 2248 2249 return (0); 2250 } 2251 2252 /* 2253 * Stores all groups from an interface in memory pointed 2254 * to by data 2255 */ 2256 int 2257 if_getgroup(caddr_t data, struct ifnet *ifp) 2258 { 2259 int len, error; 2260 struct ifg_list *ifgl; 2261 struct ifg_req ifgrq, *ifgp; 2262 struct ifgroupreq *ifgr = (struct ifgroupreq *)data; 2263 2264 if (ifgr->ifgr_len == 0) { 2265 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 2266 ifgr->ifgr_len += sizeof(struct ifg_req); 2267 return (0); 2268 } 2269 2270 len = ifgr->ifgr_len; 2271 ifgp = ifgr->ifgr_groups; 2272 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 2273 if (len < sizeof(ifgrq)) 2274 return (EINVAL); 2275 bzero(&ifgrq, sizeof ifgrq); 2276 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 2277 sizeof(ifgrq.ifgrq_group)); 2278 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp, 2279 sizeof(struct ifg_req)))) 2280 return (error); 2281 len -= sizeof(ifgrq); 2282 ifgp++; 2283 } 2284 2285 return (0); 2286 } 2287 2288 /* 2289 * Stores all members of a group in memory pointed to by data 2290 */ 2291 int 2292 if_getgroupmembers(caddr_t data) 2293 { 2294 struct ifgroupreq *ifgr = (struct ifgroupreq *)data; 2295 struct ifg_group *ifg; 2296 struct ifg_member *ifgm; 2297 struct ifg_req ifgrq, *ifgp; 2298 int len, error; 2299 2300 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) 2301 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 2302 break; 2303 if (ifg == NULL) 2304 return (ENOENT); 2305 2306 if (ifgr->ifgr_len == 0) { 2307 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 2308 ifgr->ifgr_len += sizeof(ifgrq); 2309 return (0); 2310 } 2311 2312 len = ifgr->ifgr_len; 2313 ifgp = ifgr->ifgr_groups; 2314 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 2315 if (len < sizeof(ifgrq)) 2316 return (EINVAL); 2317 bzero(&ifgrq, sizeof ifgrq); 2318 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 2319 sizeof(ifgrq.ifgrq_member)); 2320 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp, 2321 sizeof(struct ifg_req)))) 2322 return (error); 2323 len -= sizeof(ifgrq); 2324 ifgp++; 2325 } 2326 2327 return (0); 2328 } 2329 2330 int 2331 if_getgroupattribs(caddr_t data) 2332 { 2333 struct ifgroupreq *ifgr = (struct ifgroupreq *)data; 2334 struct ifg_group *ifg; 2335 2336 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) 2337 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 2338 break; 2339 if (ifg == NULL) 2340 return (ENOENT); 2341 2342 ifgr->ifgr_attrib.ifg_carp_demoted = ifg->ifg_carp_demoted; 2343 2344 return (0); 2345 } 2346 2347 int 2348 if_setgroupattribs(caddr_t data) 2349 { 2350 struct ifgroupreq *ifgr = (struct ifgroupreq *)data; 2351 struct ifg_group *ifg; 2352 struct ifg_member *ifgm; 2353 int demote; 2354 2355 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) 2356 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 2357 break; 2358 if (ifg == NULL) 2359 return (ENOENT); 2360 2361 demote = ifgr->ifgr_attrib.ifg_carp_demoted; 2362 if (demote + ifg->ifg_carp_demoted > 0xff || 2363 demote + ifg->ifg_carp_demoted < 0) 2364 return (EINVAL); 2365 2366 ifg->ifg_carp_demoted += demote; 2367 2368 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 2369 if (ifgm->ifgm_ifp->if_ioctl) 2370 ifgm->ifgm_ifp->if_ioctl(ifgm->ifgm_ifp, 2371 SIOCSIFGATTR, data); 2372 return (0); 2373 } 2374 2375 void 2376 if_group_routechange(struct sockaddr *dst, struct sockaddr *mask) 2377 { 2378 switch (dst->sa_family) { 2379 case AF_INET: 2380 if (satosin(dst)->sin_addr.s_addr == INADDR_ANY && 2381 mask && (mask->sa_len == 0 || 2382 satosin(mask)->sin_addr.s_addr == INADDR_ANY)) 2383 if_group_egress_build(); 2384 break; 2385 #ifdef INET6 2386 case AF_INET6: 2387 if (IN6_ARE_ADDR_EQUAL(&(satosin6(dst))->sin6_addr, 2388 &in6addr_any) && mask && (mask->sa_len == 0 || 2389 IN6_ARE_ADDR_EQUAL(&(satosin6(mask))->sin6_addr, 2390 &in6addr_any))) 2391 if_group_egress_build(); 2392 break; 2393 #endif 2394 } 2395 } 2396 2397 int 2398 if_group_egress_build(void) 2399 { 2400 struct ifnet *ifp; 2401 struct ifg_group *ifg; 2402 struct ifg_member *ifgm, *next; 2403 struct sockaddr_in sa_in; 2404 #ifdef INET6 2405 struct sockaddr_in6 sa_in6; 2406 #endif 2407 struct rtentry *rt0, *rt; 2408 2409 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) 2410 if (!strcmp(ifg->ifg_group, IFG_EGRESS)) 2411 break; 2412 2413 if (ifg != NULL) 2414 TAILQ_FOREACH_SAFE(ifgm, &ifg->ifg_members, ifgm_next, next) 2415 if_delgroup(ifgm->ifgm_ifp, IFG_EGRESS); 2416 2417 bzero(&sa_in, sizeof(sa_in)); 2418 sa_in.sin_len = sizeof(sa_in); 2419 sa_in.sin_family = AF_INET; 2420 rt0 = rtable_lookup(0, sintosa(&sa_in), sintosa(&sa_in), NULL, RTP_ANY); 2421 if (rt0 != NULL) { 2422 rt = rt0; 2423 do { 2424 ifp = if_get(rt->rt_ifidx); 2425 if (ifp != NULL) { 2426 if_addgroup(ifp, IFG_EGRESS); 2427 if_put(ifp); 2428 } 2429 #ifndef SMALL_KERNEL 2430 rt = rtable_mpath_next(rt); 2431 #else 2432 rt = NULL; 2433 #endif 2434 } while (rt != NULL); 2435 } 2436 rtfree(rt0); 2437 2438 #ifdef INET6 2439 bcopy(&sa6_any, &sa_in6, sizeof(sa_in6)); 2440 rt0 = rtable_lookup(0, sin6tosa(&sa_in6), sin6tosa(&sa_in6), NULL, 2441 RTP_ANY); 2442 if (rt0 != NULL) { 2443 rt = rt0; 2444 do { 2445 ifp = if_get(rt->rt_ifidx); 2446 if (ifp != NULL) { 2447 if_addgroup(ifp, IFG_EGRESS); 2448 if_put(ifp); 2449 } 2450 #ifndef SMALL_KERNEL 2451 rt = rtable_mpath_next(rt); 2452 #else 2453 rt = NULL; 2454 #endif 2455 } while (rt != NULL); 2456 } 2457 rtfree(rt0); 2458 #endif /* INET6 */ 2459 2460 return (0); 2461 } 2462 2463 /* 2464 * Set/clear promiscuous mode on interface ifp based on the truth value 2465 * of pswitch. The calls are reference counted so that only the first 2466 * "on" request actually has an effect, as does the final "off" request. 2467 * Results are undefined if the "off" and "on" requests are not matched. 2468 */ 2469 int 2470 ifpromisc(struct ifnet *ifp, int pswitch) 2471 { 2472 struct ifreq ifr; 2473 2474 if (pswitch) { 2475 /* 2476 * If the device is not configured up, we cannot put it in 2477 * promiscuous mode. 2478 */ 2479 if ((ifp->if_flags & IFF_UP) == 0) 2480 return (ENETDOWN); 2481 if (ifp->if_pcount++ != 0) 2482 return (0); 2483 ifp->if_flags |= IFF_PROMISC; 2484 } else { 2485 if (--ifp->if_pcount > 0) 2486 return (0); 2487 ifp->if_flags &= ~IFF_PROMISC; 2488 /* 2489 * If the device is not configured up, we should not need to 2490 * turn off promiscuous mode (device should have turned it 2491 * off when interface went down; and will look at IFF_PROMISC 2492 * again next time interface comes up). 2493 */ 2494 if ((ifp->if_flags & IFF_UP) == 0) 2495 return (0); 2496 } 2497 ifr.ifr_flags = ifp->if_flags; 2498 return ((*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr)); 2499 } 2500 2501 int 2502 sysctl_mq(int *name, u_int namelen, void *oldp, size_t *oldlenp, 2503 void *newp, size_t newlen, struct mbuf_queue *mq) 2504 { 2505 /* All sysctl names at this level are terminal. */ 2506 if (namelen != 1) 2507 return (ENOTDIR); 2508 2509 switch (name[0]) { 2510 case IFQCTL_LEN: 2511 return (sysctl_rdint(oldp, oldlenp, newp, mq_len(mq))); 2512 case IFQCTL_MAXLEN: 2513 return (sysctl_int(oldp, oldlenp, newp, newlen, 2514 &mq->mq_maxlen)); /* XXX directly accessing maxlen */ 2515 case IFQCTL_DROPS: 2516 return (sysctl_rdint(oldp, oldlenp, newp, mq_drops(mq))); 2517 default: 2518 return (EOPNOTSUPP); 2519 } 2520 /* NOTREACHED */ 2521 } 2522 2523 void 2524 ifa_add(struct ifnet *ifp, struct ifaddr *ifa) 2525 { 2526 TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list); 2527 } 2528 2529 void 2530 ifa_del(struct ifnet *ifp, struct ifaddr *ifa) 2531 { 2532 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list); 2533 } 2534 2535 void 2536 ifa_update_broadaddr(struct ifnet *ifp, struct ifaddr *ifa, struct sockaddr *sa) 2537 { 2538 if (ifa->ifa_broadaddr->sa_len != sa->sa_len) 2539 panic("ifa_update_broadaddr does not support dynamic length"); 2540 bcopy(sa, ifa->ifa_broadaddr, sa->sa_len); 2541 } 2542 2543 #ifdef DDB 2544 /* debug function, can be called from ddb> */ 2545 void 2546 ifa_print_all(void) 2547 { 2548 struct ifnet *ifp; 2549 struct ifaddr *ifa; 2550 2551 TAILQ_FOREACH(ifp, &ifnet, if_list) { 2552 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 2553 char addr[INET6_ADDRSTRLEN]; 2554 2555 switch (ifa->ifa_addr->sa_family) { 2556 case AF_INET: 2557 printf("%s", inet_ntop(AF_INET, 2558 &satosin(ifa->ifa_addr)->sin_addr, 2559 addr, sizeof(addr))); 2560 break; 2561 #ifdef INET6 2562 case AF_INET6: 2563 printf("%s", inet_ntop(AF_INET6, 2564 &(satosin6(ifa->ifa_addr))->sin6_addr, 2565 addr, sizeof(addr))); 2566 break; 2567 #endif 2568 } 2569 printf(" on %s\n", ifp->if_xname); 2570 } 2571 } 2572 } 2573 #endif /* DDB */ 2574 2575 void 2576 ifnewlladdr(struct ifnet *ifp) 2577 { 2578 #ifdef INET6 2579 struct ifaddr *ifa; 2580 #endif 2581 struct ifreq ifrq; 2582 short up; 2583 int s; 2584 2585 s = splnet(); 2586 up = ifp->if_flags & IFF_UP; 2587 2588 if (up) { 2589 /* go down for a moment... */ 2590 ifp->if_flags &= ~IFF_UP; 2591 ifrq.ifr_flags = ifp->if_flags; 2592 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq); 2593 } 2594 2595 ifp->if_flags |= IFF_UP; 2596 ifrq.ifr_flags = ifp->if_flags; 2597 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq); 2598 2599 #ifdef INET6 2600 /* 2601 * Update the link-local address. Don't do it if we're 2602 * a router to avoid confusing hosts on the network. 2603 */ 2604 if (!ip6_forwarding) { 2605 ifa = &in6ifa_ifpforlinklocal(ifp, 0)->ia_ifa; 2606 if (ifa) { 2607 in6_purgeaddr(ifa); 2608 dohooks(ifp->if_addrhooks, 0); 2609 in6_ifattach(ifp); 2610 } 2611 } 2612 #endif 2613 if (!up) { 2614 /* go back down */ 2615 ifp->if_flags &= ~IFF_UP; 2616 ifrq.ifr_flags = ifp->if_flags; 2617 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq); 2618 } 2619 splx(s); 2620 } 2621 2622 int net_ticks; 2623 u_int net_livelocks; 2624 2625 void 2626 net_tick(void *null) 2627 { 2628 extern int ticks; 2629 2630 if (ticks - net_ticks > 1) 2631 net_livelocks++; 2632 2633 net_ticks = ticks; 2634 2635 timeout_add(&net_tick_to, 1); 2636 } 2637 2638 int 2639 net_livelocked(void) 2640 { 2641 extern int ticks; 2642 2643 return (ticks - net_ticks > 1); 2644 } 2645 2646 void 2647 if_rxr_init(struct if_rxring *rxr, u_int lwm, u_int hwm) 2648 { 2649 extern int ticks; 2650 2651 memset(rxr, 0, sizeof(*rxr)); 2652 2653 rxr->rxr_adjusted = ticks; 2654 rxr->rxr_cwm = rxr->rxr_lwm = lwm; 2655 rxr->rxr_hwm = hwm; 2656 } 2657 2658 static inline void 2659 if_rxr_adjust_cwm(struct if_rxring *rxr) 2660 { 2661 extern int ticks; 2662 2663 if (net_livelocked()) { 2664 if (rxr->rxr_cwm > rxr->rxr_lwm) 2665 rxr->rxr_cwm--; 2666 else 2667 return; 2668 } else if (rxr->rxr_alive >= rxr->rxr_lwm) 2669 return; 2670 else if (rxr->rxr_cwm < rxr->rxr_hwm) 2671 rxr->rxr_cwm++; 2672 2673 rxr->rxr_adjusted = ticks; 2674 } 2675 2676 u_int 2677 if_rxr_get(struct if_rxring *rxr, u_int max) 2678 { 2679 extern int ticks; 2680 u_int diff; 2681 2682 if (ticks - rxr->rxr_adjusted >= 1) { 2683 /* we're free to try for an adjustment */ 2684 if_rxr_adjust_cwm(rxr); 2685 } 2686 2687 if (rxr->rxr_alive >= rxr->rxr_cwm) 2688 return (0); 2689 2690 diff = min(rxr->rxr_cwm - rxr->rxr_alive, max); 2691 rxr->rxr_alive += diff; 2692 2693 return (diff); 2694 } 2695 2696 int 2697 if_rxr_info_ioctl(struct if_rxrinfo *uifri, u_int t, struct if_rxring_info *e) 2698 { 2699 struct if_rxrinfo kifri; 2700 int error; 2701 u_int n; 2702 2703 error = copyin(uifri, &kifri, sizeof(kifri)); 2704 if (error) 2705 return (error); 2706 2707 n = min(t, kifri.ifri_total); 2708 kifri.ifri_total = t; 2709 2710 if (n > 0) { 2711 error = copyout(e, kifri.ifri_entries, sizeof(*e) * n); 2712 if (error) 2713 return (error); 2714 } 2715 2716 return (copyout(&kifri, uifri, sizeof(kifri))); 2717 } 2718 2719 int 2720 if_rxr_ioctl(struct if_rxrinfo *ifri, const char *name, u_int size, 2721 struct if_rxring *rxr) 2722 { 2723 struct if_rxring_info ifr; 2724 2725 memset(&ifr, 0, sizeof(ifr)); 2726 2727 if (name != NULL) 2728 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 2729 2730 ifr.ifr_size = size; 2731 ifr.ifr_info = *rxr; 2732 2733 return (if_rxr_info_ioctl(ifri, 1, &ifr)); 2734 } 2735 2736 /* 2737 * Network stack input queues. 2738 */ 2739 2740 void 2741 niq_init(struct niqueue *niq, u_int maxlen, u_int isr) 2742 { 2743 mq_init(&niq->ni_q, maxlen, IPL_NET); 2744 niq->ni_isr = isr; 2745 } 2746 2747 int 2748 niq_enqueue(struct niqueue *niq, struct mbuf *m) 2749 { 2750 int rv; 2751 2752 rv = mq_enqueue(&niq->ni_q, m); 2753 if (rv == 0) 2754 schednetisr(niq->ni_isr); 2755 else 2756 if_congestion(); 2757 2758 return (rv); 2759 } 2760 2761 int 2762 niq_enlist(struct niqueue *niq, struct mbuf_list *ml) 2763 { 2764 int rv; 2765 2766 rv = mq_enlist(&niq->ni_q, ml); 2767 if (rv == 0) 2768 schednetisr(niq->ni_isr); 2769 else 2770 if_congestion(); 2771 2772 return (rv); 2773 } 2774 2775 __dead void 2776 unhandled_af(int af) 2777 { 2778 panic("unhandled af %d", af); 2779 } 2780