1 /* $OpenBSD: if.c,v 1.640 2021/03/26 22:41:06 mvs 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 "switch.h" 72 #include "if_wg.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/task.h> 85 #include <sys/atomic.h> 86 #include <sys/percpu.h> 87 #include <sys/proc.h> 88 #include <sys/stdint.h> /* uintptr_t */ 89 #include <sys/rwlock.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 #include <sys/device.h> 133 134 void if_attachsetup(struct ifnet *); 135 void if_attachdomain(struct ifnet *); 136 void if_attach_common(struct ifnet *); 137 void if_remove(struct ifnet *); 138 int if_createrdomain(int, struct ifnet *); 139 int if_setrdomain(struct ifnet *, int); 140 void if_slowtimo(void *); 141 142 void if_detached_qstart(struct ifqueue *); 143 int if_detached_ioctl(struct ifnet *, u_long, caddr_t); 144 145 int ifioctl_get(u_long, caddr_t); 146 int ifconf(caddr_t); 147 static int 148 if_sffpage_check(const caddr_t); 149 150 int if_getgroup(caddr_t, struct ifnet *); 151 int if_getgroupmembers(caddr_t); 152 int if_getgroupattribs(caddr_t); 153 int if_setgroupattribs(caddr_t); 154 int if_getgrouplist(caddr_t); 155 156 void if_linkstate(struct ifnet *); 157 void if_linkstate_task(void *); 158 159 int if_clone_list(struct if_clonereq *); 160 struct if_clone *if_clone_lookup(const char *, int *); 161 162 int if_group_egress_build(void); 163 164 void if_watchdog_task(void *); 165 166 void if_netisr(void *); 167 168 #ifdef DDB 169 void ifa_print_all(void); 170 #endif 171 172 void if_qstart_compat(struct ifqueue *); 173 174 /* 175 * interface index map 176 * 177 * the kernel maintains a mapping of interface indexes to struct ifnet 178 * pointers. 179 * 180 * the map is an array of struct ifnet pointers prefixed by an if_map 181 * structure. the if_map structure stores the length of its array. 182 * 183 * as interfaces are attached to the system, the map is grown on demand 184 * up to USHRT_MAX entries. 185 * 186 * interface index 0 is reserved and represents no interface. this 187 * supports the use of the interface index as the scope for IPv6 link 188 * local addresses, where scope 0 means no scope has been specified. 189 * it also supports the use of interface index as the unique identifier 190 * for network interfaces in SNMP applications as per RFC2863. therefore 191 * if_get(0) returns NULL. 192 */ 193 194 void if_ifp_dtor(void *, void *); 195 void if_map_dtor(void *, void *); 196 struct ifnet *if_ref(struct ifnet *); 197 198 /* 199 * struct if_map 200 * 201 * bounded array of ifnet srp pointers used to fetch references of live 202 * interfaces with if_get(). 203 */ 204 205 struct if_map { 206 unsigned long limit; 207 /* followed by limit ifnet srp pointers */ 208 }; 209 210 /* 211 * struct if_idxmap 212 * 213 * infrastructure to manage updates and accesses to the current if_map. 214 */ 215 216 struct if_idxmap { 217 unsigned int serial; 218 unsigned int count; 219 struct srp map; 220 }; 221 222 void if_idxmap_init(unsigned int); 223 void if_idxmap_insert(struct ifnet *); 224 void if_idxmap_remove(struct ifnet *); 225 226 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head); 227 228 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners); 229 int if_cloners_count; 230 231 struct rwlock if_cloners_lock = RWLOCK_INITIALIZER("clonelk"); 232 233 /* hooks should only be added, deleted, and run from a process context */ 234 struct mutex if_hooks_mtx = MUTEX_INITIALIZER(IPL_NONE); 235 void if_hooks_run(struct task_list *); 236 237 int ifq_congestion; 238 239 int netisr; 240 241 #define NET_TASKQ 1 242 struct taskq *nettqmp[NET_TASKQ]; 243 244 struct task if_input_task_locked = TASK_INITIALIZER(if_netisr, NULL); 245 246 /* 247 * Serialize socket operations to ensure no new sleeping points 248 * are introduced in IP output paths. 249 */ 250 struct rwlock netlock = RWLOCK_INITIALIZER("netlock"); 251 252 /* 253 * Network interface utility routines. 254 */ 255 void 256 ifinit(void) 257 { 258 unsigned int i; 259 260 /* 261 * most machines boot with 4 or 5 interfaces, so size the initial map 262 * to accomodate this 263 */ 264 if_idxmap_init(8); 265 266 for (i = 0; i < NET_TASKQ; i++) { 267 nettqmp[i] = taskq_create("softnet", 1, IPL_NET, TASKQ_MPSAFE); 268 if (nettqmp[i] == NULL) 269 panic("unable to create network taskq %d", i); 270 } 271 } 272 273 static struct if_idxmap if_idxmap = { 274 0, 275 0, 276 SRP_INITIALIZER() 277 }; 278 279 struct srp_gc if_ifp_gc = SRP_GC_INITIALIZER(if_ifp_dtor, NULL); 280 struct srp_gc if_map_gc = SRP_GC_INITIALIZER(if_map_dtor, NULL); 281 282 struct ifnet_head ifnet = TAILQ_HEAD_INITIALIZER(ifnet); 283 284 void 285 if_idxmap_init(unsigned int limit) 286 { 287 struct if_map *if_map; 288 struct srp *map; 289 unsigned int i; 290 291 if_idxmap.serial = 1; /* skip ifidx 0 so it can return NULL */ 292 293 if_map = malloc(sizeof(*if_map) + limit * sizeof(*map), 294 M_IFADDR, M_WAITOK); 295 296 if_map->limit = limit; 297 map = (struct srp *)(if_map + 1); 298 for (i = 0; i < limit; i++) 299 srp_init(&map[i]); 300 301 /* this is called early so there's nothing to race with */ 302 srp_update_locked(&if_map_gc, &if_idxmap.map, if_map); 303 } 304 305 void 306 if_idxmap_insert(struct ifnet *ifp) 307 { 308 struct if_map *if_map; 309 struct srp *map; 310 unsigned int index, i; 311 312 refcnt_init(&ifp->if_refcnt); 313 314 /* the kernel lock guarantees serialised modifications to if_idxmap */ 315 KERNEL_ASSERT_LOCKED(); 316 317 if (++if_idxmap.count > USHRT_MAX) 318 panic("too many interfaces"); 319 320 if_map = srp_get_locked(&if_idxmap.map); 321 map = (struct srp *)(if_map + 1); 322 323 index = if_idxmap.serial++ & USHRT_MAX; 324 325 if (index >= if_map->limit) { 326 struct if_map *nif_map; 327 struct srp *nmap; 328 unsigned int nlimit; 329 struct ifnet *nifp; 330 331 nlimit = if_map->limit * 2; 332 nif_map = malloc(sizeof(*nif_map) + nlimit * sizeof(*nmap), 333 M_IFADDR, M_WAITOK); 334 nmap = (struct srp *)(nif_map + 1); 335 336 nif_map->limit = nlimit; 337 for (i = 0; i < if_map->limit; i++) { 338 srp_init(&nmap[i]); 339 nifp = srp_get_locked(&map[i]); 340 if (nifp != NULL) { 341 srp_update_locked(&if_ifp_gc, &nmap[i], 342 if_ref(nifp)); 343 } 344 } 345 346 while (i < nlimit) { 347 srp_init(&nmap[i]); 348 i++; 349 } 350 351 srp_update_locked(&if_map_gc, &if_idxmap.map, nif_map); 352 if_map = nif_map; 353 map = nmap; 354 } 355 356 /* pick the next free index */ 357 for (i = 0; i < USHRT_MAX; i++) { 358 if (index != 0 && srp_get_locked(&map[index]) == NULL) 359 break; 360 361 index = if_idxmap.serial++ & USHRT_MAX; 362 } 363 364 /* commit */ 365 ifp->if_index = index; 366 srp_update_locked(&if_ifp_gc, &map[index], if_ref(ifp)); 367 } 368 369 void 370 if_idxmap_remove(struct ifnet *ifp) 371 { 372 struct if_map *if_map; 373 struct srp *map; 374 unsigned int index; 375 376 index = ifp->if_index; 377 378 /* the kernel lock guarantees serialised modifications to if_idxmap */ 379 KERNEL_ASSERT_LOCKED(); 380 381 if_map = srp_get_locked(&if_idxmap.map); 382 KASSERT(index < if_map->limit); 383 384 map = (struct srp *)(if_map + 1); 385 KASSERT(ifp == (struct ifnet *)srp_get_locked(&map[index])); 386 387 srp_update_locked(&if_ifp_gc, &map[index], NULL); 388 if_idxmap.count--; 389 /* end of if_idxmap modifications */ 390 } 391 392 void 393 if_ifp_dtor(void *null, void *ifp) 394 { 395 if_put(ifp); 396 } 397 398 void 399 if_map_dtor(void *null, void *m) 400 { 401 struct if_map *if_map = m; 402 struct srp *map = (struct srp *)(if_map + 1); 403 unsigned int i; 404 405 /* 406 * dont need to serialize the use of update_locked since this is 407 * the last reference to this map. there's nothing to race against. 408 */ 409 for (i = 0; i < if_map->limit; i++) 410 srp_update_locked(&if_ifp_gc, &map[i], NULL); 411 412 free(if_map, M_IFADDR, sizeof(*if_map) + if_map->limit * sizeof(*map)); 413 } 414 415 /* 416 * Attach an interface to the 417 * list of "active" interfaces. 418 */ 419 void 420 if_attachsetup(struct ifnet *ifp) 421 { 422 unsigned long ifidx; 423 424 NET_ASSERT_LOCKED(); 425 426 if_addgroup(ifp, IFG_ALL); 427 428 if_attachdomain(ifp); 429 #if NPF > 0 430 pfi_attach_ifnet(ifp); 431 #endif 432 433 timeout_set(&ifp->if_slowtimo, if_slowtimo, ifp); 434 if_slowtimo(ifp); 435 436 if_idxmap_insert(ifp); 437 KASSERT(if_get(0) == NULL); 438 439 ifidx = ifp->if_index; 440 441 task_set(&ifp->if_watchdogtask, if_watchdog_task, (void *)ifidx); 442 task_set(&ifp->if_linkstatetask, if_linkstate_task, (void *)ifidx); 443 444 /* Announce the interface. */ 445 rtm_ifannounce(ifp, IFAN_ARRIVAL); 446 } 447 448 /* 449 * Allocate the link level name for the specified interface. This 450 * is an attachment helper. It must be called after ifp->if_addrlen 451 * is initialized, which may not be the case when if_attach() is 452 * called. 453 */ 454 void 455 if_alloc_sadl(struct ifnet *ifp) 456 { 457 unsigned int socksize; 458 int namelen, masklen; 459 struct sockaddr_dl *sdl; 460 461 /* 462 * If the interface already has a link name, release it 463 * now. This is useful for interfaces that can change 464 * link types, and thus switch link names often. 465 */ 466 if_free_sadl(ifp); 467 468 namelen = strlen(ifp->if_xname); 469 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen; 470 socksize = masklen + ifp->if_addrlen; 471 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1))) 472 if (socksize < sizeof(*sdl)) 473 socksize = sizeof(*sdl); 474 socksize = ROUNDUP(socksize); 475 sdl = malloc(socksize, M_IFADDR, M_WAITOK|M_ZERO); 476 sdl->sdl_len = socksize; 477 sdl->sdl_family = AF_LINK; 478 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 479 sdl->sdl_nlen = namelen; 480 sdl->sdl_alen = ifp->if_addrlen; 481 sdl->sdl_index = ifp->if_index; 482 sdl->sdl_type = ifp->if_type; 483 ifp->if_sadl = sdl; 484 } 485 486 /* 487 * Free the link level name for the specified interface. This is 488 * a detach helper. This is called from if_detach() or from 489 * link layer type specific detach functions. 490 */ 491 void 492 if_free_sadl(struct ifnet *ifp) 493 { 494 if (ifp->if_sadl == NULL) 495 return; 496 497 free(ifp->if_sadl, M_IFADDR, ifp->if_sadl->sdl_len); 498 ifp->if_sadl = NULL; 499 } 500 501 void 502 if_attachdomain(struct ifnet *ifp) 503 { 504 struct domain *dp; 505 int i, s; 506 507 s = splnet(); 508 509 /* address family dependent data region */ 510 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 511 for (i = 0; (dp = domains[i]) != NULL; i++) { 512 if (dp->dom_ifattach) 513 ifp->if_afdata[dp->dom_family] = 514 (*dp->dom_ifattach)(ifp); 515 } 516 517 splx(s); 518 } 519 520 void 521 if_attachhead(struct ifnet *ifp) 522 { 523 if_attach_common(ifp); 524 NET_LOCK(); 525 TAILQ_INSERT_HEAD(&ifnet, ifp, if_list); 526 if_attachsetup(ifp); 527 NET_UNLOCK(); 528 } 529 530 void 531 if_attach(struct ifnet *ifp) 532 { 533 if_attach_common(ifp); 534 NET_LOCK(); 535 TAILQ_INSERT_TAIL(&ifnet, ifp, if_list); 536 if_attachsetup(ifp); 537 NET_UNLOCK(); 538 } 539 540 void 541 if_attach_queues(struct ifnet *ifp, unsigned int nqs) 542 { 543 struct ifqueue **map; 544 struct ifqueue *ifq; 545 int i; 546 547 KASSERT(ifp->if_ifqs == ifp->if_snd.ifq_ifqs); 548 KASSERT(nqs != 0); 549 550 map = mallocarray(sizeof(*map), nqs, M_DEVBUF, M_WAITOK); 551 552 ifp->if_snd.ifq_softc = NULL; 553 map[0] = &ifp->if_snd; 554 555 for (i = 1; i < nqs; i++) { 556 ifq = malloc(sizeof(*ifq), M_DEVBUF, M_WAITOK|M_ZERO); 557 ifq_set_maxlen(ifq, ifp->if_snd.ifq_maxlen); 558 ifq_init(ifq, ifp, i); 559 map[i] = ifq; 560 } 561 562 ifp->if_ifqs = map; 563 ifp->if_nifqs = nqs; 564 } 565 566 void 567 if_attach_iqueues(struct ifnet *ifp, unsigned int niqs) 568 { 569 struct ifiqueue **map; 570 struct ifiqueue *ifiq; 571 unsigned int i; 572 573 KASSERT(niqs != 0); 574 575 map = mallocarray(niqs, sizeof(*map), M_DEVBUF, M_WAITOK); 576 577 ifp->if_rcv.ifiq_softc = NULL; 578 map[0] = &ifp->if_rcv; 579 580 for (i = 1; i < niqs; i++) { 581 ifiq = malloc(sizeof(*ifiq), M_DEVBUF, M_WAITOK|M_ZERO); 582 ifiq_init(ifiq, ifp, i); 583 map[i] = ifiq; 584 } 585 586 ifp->if_iqs = map; 587 ifp->if_niqs = niqs; 588 } 589 590 void 591 if_attach_common(struct ifnet *ifp) 592 { 593 KASSERT(ifp->if_ioctl != NULL); 594 595 TAILQ_INIT(&ifp->if_addrlist); 596 TAILQ_INIT(&ifp->if_maddrlist); 597 TAILQ_INIT(&ifp->if_groups); 598 599 if (!ISSET(ifp->if_xflags, IFXF_MPSAFE)) { 600 KASSERTMSG(ifp->if_qstart == NULL, 601 "%s: if_qstart set without MPSAFE set", ifp->if_xname); 602 ifp->if_qstart = if_qstart_compat; 603 } else { 604 KASSERTMSG(ifp->if_start == NULL, 605 "%s: if_start set with MPSAFE set", ifp->if_xname); 606 KASSERTMSG(ifp->if_qstart != NULL, 607 "%s: if_qstart not set with MPSAFE set", ifp->if_xname); 608 } 609 610 ifq_init(&ifp->if_snd, ifp, 0); 611 612 ifp->if_snd.ifq_ifqs[0] = &ifp->if_snd; 613 ifp->if_ifqs = ifp->if_snd.ifq_ifqs; 614 ifp->if_nifqs = 1; 615 if (ifp->if_txmit == 0) 616 ifp->if_txmit = IF_TXMIT_DEFAULT; 617 618 ifiq_init(&ifp->if_rcv, ifp, 0); 619 620 ifp->if_rcv.ifiq_ifiqs[0] = &ifp->if_rcv; 621 ifp->if_iqs = ifp->if_rcv.ifiq_ifiqs; 622 ifp->if_niqs = 1; 623 624 TAILQ_INIT(&ifp->if_addrhooks); 625 TAILQ_INIT(&ifp->if_linkstatehooks); 626 TAILQ_INIT(&ifp->if_detachhooks); 627 628 if (ifp->if_rtrequest == NULL) 629 ifp->if_rtrequest = if_rtrequest_dummy; 630 if (ifp->if_enqueue == NULL) 631 ifp->if_enqueue = if_enqueue_ifq; 632 #if NBPFILTER > 0 633 if (ifp->if_bpf_mtap == NULL) 634 ifp->if_bpf_mtap = bpf_mtap_ether; 635 #endif 636 ifp->if_llprio = IFQ_DEFPRIO; 637 } 638 639 void 640 if_attach_ifq(struct ifnet *ifp, const struct ifq_ops *newops, void *args) 641 { 642 /* 643 * only switch the ifq_ops on the first ifq on an interface. 644 * 645 * the only ifq_ops we provide priq and hfsc, and hfsc only 646 * works on a single ifq. because the code uses the ifq_ops 647 * on the first ifq (if_snd) to select a queue for an mbuf, 648 * by switching only the first one we change both the algorithm 649 * and force the routing of all new packets to it. 650 */ 651 ifq_attach(&ifp->if_snd, newops, args); 652 } 653 654 void 655 if_start(struct ifnet *ifp) 656 { 657 KASSERT(ifp->if_qstart == if_qstart_compat); 658 if_qstart_compat(&ifp->if_snd); 659 } 660 void 661 if_qstart_compat(struct ifqueue *ifq) 662 { 663 struct ifnet *ifp = ifq->ifq_if; 664 int s; 665 666 /* 667 * the stack assumes that an interface can have multiple 668 * transmit rings, but a lot of drivers are still written 669 * so that interfaces and send rings have a 1:1 mapping. 670 * this provides compatibility between the stack and the older 671 * drivers by translating from the only queue they have 672 * (ifp->if_snd) back to the interface and calling if_start. 673 */ 674 675 KERNEL_LOCK(); 676 s = splnet(); 677 (*ifp->if_start)(ifp); 678 splx(s); 679 KERNEL_UNLOCK(); 680 } 681 682 int 683 if_enqueue(struct ifnet *ifp, struct mbuf *m) 684 { 685 CLR(m->m_pkthdr.csum_flags, M_TIMESTAMP); 686 687 #if NPF > 0 688 if (m->m_pkthdr.pf.delay > 0) 689 return (pf_delay_pkt(m, ifp->if_index)); 690 #endif 691 692 #if NBRIDGE > 0 693 if (ifp->if_bridgeidx && (m->m_flags & M_PROTO1) == 0) { 694 int error; 695 696 error = bridge_enqueue(ifp, m); 697 return (error); 698 } 699 #endif 700 701 #if NPF > 0 702 pf_pkt_addr_changed(m); 703 #endif /* NPF > 0 */ 704 705 return ((*ifp->if_enqueue)(ifp, m)); 706 } 707 708 int 709 if_enqueue_ifq(struct ifnet *ifp, struct mbuf *m) 710 { 711 struct ifqueue *ifq = &ifp->if_snd; 712 int error; 713 714 if (ifp->if_nifqs > 1) { 715 unsigned int idx; 716 717 /* 718 * use the operations on the first ifq to pick which of 719 * the array gets this mbuf. 720 */ 721 722 idx = ifq_idx(&ifp->if_snd, ifp->if_nifqs, m); 723 ifq = ifp->if_ifqs[idx]; 724 } 725 726 error = ifq_enqueue(ifq, m); 727 if (error) 728 return (error); 729 730 ifq_start(ifq); 731 732 return (0); 733 } 734 735 void 736 if_input(struct ifnet *ifp, struct mbuf_list *ml) 737 { 738 ifiq_input(&ifp->if_rcv, ml); 739 } 740 741 int 742 if_input_local(struct ifnet *ifp, struct mbuf *m, sa_family_t af) 743 { 744 int keepflags; 745 746 #if NBPFILTER > 0 747 /* 748 * Only send packets to bpf if they are destinated to local 749 * addresses. 750 * 751 * if_input_local() is also called for SIMPLEX interfaces to 752 * duplicate packets for local use. But don't dup them to bpf. 753 */ 754 if (ifp->if_flags & IFF_LOOPBACK) { 755 caddr_t if_bpf = ifp->if_bpf; 756 757 if (if_bpf) 758 bpf_mtap_af(if_bpf, af, m, BPF_DIRECTION_OUT); 759 } 760 #endif 761 keepflags = m->m_flags & (M_BCAST|M_MCAST); 762 m_resethdr(m); 763 m->m_flags |= M_LOOP | keepflags; 764 m->m_pkthdr.ph_ifidx = ifp->if_index; 765 m->m_pkthdr.ph_rtableid = ifp->if_rdomain; 766 767 ifp->if_opackets++; 768 ifp->if_obytes += m->m_pkthdr.len; 769 770 ifp->if_ipackets++; 771 ifp->if_ibytes += m->m_pkthdr.len; 772 773 switch (af) { 774 case AF_INET: 775 ipv4_input(ifp, m); 776 break; 777 #ifdef INET6 778 case AF_INET6: 779 ipv6_input(ifp, m); 780 break; 781 #endif /* INET6 */ 782 #ifdef MPLS 783 case AF_MPLS: 784 mpls_input(ifp, m); 785 break; 786 #endif /* MPLS */ 787 default: 788 printf("%s: can't handle af%d\n", ifp->if_xname, af); 789 m_freem(m); 790 return (EAFNOSUPPORT); 791 } 792 793 return (0); 794 } 795 796 int 797 if_output_local(struct ifnet *ifp, struct mbuf *m, sa_family_t af) 798 { 799 struct ifiqueue *ifiq; 800 unsigned int flow = 0; 801 802 m->m_pkthdr.ph_family = af; 803 m->m_pkthdr.ph_ifidx = ifp->if_index; 804 m->m_pkthdr.ph_rtableid = ifp->if_rdomain; 805 806 if (ISSET(m->m_pkthdr.csum_flags, M_FLOWID)) 807 flow = m->m_pkthdr.ph_flowid; 808 809 ifiq = ifp->if_iqs[flow % ifp->if_niqs]; 810 811 return (ifiq_enqueue(ifiq, m) == 0 ? 0 : ENOBUFS); 812 } 813 814 void 815 if_input_process(struct ifnet *ifp, struct mbuf_list *ml) 816 { 817 struct mbuf *m; 818 819 if (ml_empty(ml)) 820 return; 821 822 if (!ISSET(ifp->if_xflags, IFXF_CLONED)) 823 enqueue_randomness(ml_len(ml) ^ (uintptr_t)MBUF_LIST_FIRST(ml)); 824 825 /* 826 * We grab the NET_LOCK() before processing any packet to 827 * ensure there's no contention on the routing table lock. 828 * 829 * Without it we could race with a userland thread to insert 830 * a L2 entry in ip{6,}_output(). Such race would result in 831 * one of the threads sleeping *inside* the IP output path. 832 * 833 * Since we have a NET_LOCK() we also use it to serialize access 834 * to PF globals, pipex globals, unicast and multicast addresses 835 * lists and the socket layer. 836 */ 837 NET_LOCK(); 838 while ((m = ml_dequeue(ml)) != NULL) 839 (*ifp->if_input)(ifp, m); 840 NET_UNLOCK(); 841 } 842 843 void 844 if_vinput(struct ifnet *ifp, struct mbuf *m) 845 { 846 #if NBPFILTER > 0 847 caddr_t if_bpf; 848 #endif 849 850 m->m_pkthdr.ph_ifidx = ifp->if_index; 851 m->m_pkthdr.ph_rtableid = ifp->if_rdomain; 852 853 counters_pkt(ifp->if_counters, 854 ifc_ipackets, ifc_ibytes, m->m_pkthdr.len); 855 856 #if NPF > 0 857 pf_pkt_addr_changed(m); 858 #endif 859 860 #if NBPFILTER > 0 861 if_bpf = ifp->if_bpf; 862 if (if_bpf) { 863 if ((*ifp->if_bpf_mtap)(if_bpf, m, BPF_DIRECTION_IN)) { 864 m_freem(m); 865 return; 866 } 867 } 868 #endif 869 870 if (__predict_true(!ISSET(ifp->if_xflags, IFXF_MONITOR))) 871 (*ifp->if_input)(ifp, m); 872 } 873 874 void 875 if_netisr(void *unused) 876 { 877 int n, t = 0; 878 879 NET_LOCK(); 880 881 while ((n = netisr) != 0) { 882 /* Like sched_pause() but with a rwlock dance. */ 883 if (curcpu()->ci_schedstate.spc_schedflags & SPCF_SHOULDYIELD) { 884 NET_UNLOCK(); 885 yield(); 886 NET_LOCK(); 887 } 888 889 atomic_clearbits_int(&netisr, n); 890 891 #if NETHER > 0 892 if (n & (1 << NETISR_ARP)) { 893 KERNEL_LOCK(); 894 arpintr(); 895 KERNEL_UNLOCK(); 896 } 897 #endif 898 #if NPPP > 0 899 if (n & (1 << NETISR_PPP)) { 900 KERNEL_LOCK(); 901 pppintr(); 902 KERNEL_UNLOCK(); 903 } 904 #endif 905 #if NBRIDGE > 0 906 if (n & (1 << NETISR_BRIDGE)) 907 bridgeintr(); 908 #endif 909 #if NSWITCH > 0 910 if (n & (1 << NETISR_SWITCH)) { 911 KERNEL_LOCK(); 912 switchintr(); 913 KERNEL_UNLOCK(); 914 } 915 #endif 916 t |= n; 917 } 918 919 #if NPFSYNC > 0 920 if (t & (1 << NETISR_PFSYNC)) { 921 KERNEL_LOCK(); 922 pfsyncintr(); 923 KERNEL_UNLOCK(); 924 } 925 #endif 926 927 NET_UNLOCK(); 928 } 929 930 void 931 if_hooks_run(struct task_list *hooks) 932 { 933 struct task *t, *nt; 934 struct task cursor = { .t_func = NULL }; 935 void (*func)(void *); 936 void *arg; 937 938 mtx_enter(&if_hooks_mtx); 939 for (t = TAILQ_FIRST(hooks); t != NULL; t = nt) { 940 if (t->t_func == NULL) { /* skip cursors */ 941 nt = TAILQ_NEXT(t, t_entry); 942 continue; 943 } 944 func = t->t_func; 945 arg = t->t_arg; 946 947 TAILQ_INSERT_AFTER(hooks, t, &cursor, t_entry); 948 mtx_leave(&if_hooks_mtx); 949 950 (*func)(arg); 951 952 mtx_enter(&if_hooks_mtx); 953 nt = TAILQ_NEXT(&cursor, t_entry); /* avoid _Q_INVALIDATE */ 954 TAILQ_REMOVE(hooks, &cursor, t_entry); 955 } 956 mtx_leave(&if_hooks_mtx); 957 } 958 959 void 960 if_remove(struct ifnet *ifp) 961 { 962 /* Remove the interface from the list of all interfaces. */ 963 NET_LOCK(); 964 TAILQ_REMOVE(&ifnet, ifp, if_list); 965 NET_UNLOCK(); 966 967 /* Remove the interface from the interface index map. */ 968 if_idxmap_remove(ifp); 969 970 /* Sleep until the last reference is released. */ 971 refcnt_finalize(&ifp->if_refcnt, "ifrm"); 972 } 973 974 void 975 if_deactivate(struct ifnet *ifp) 976 { 977 /* 978 * Call detach hooks from head to tail. To make sure detach 979 * hooks are executed in the reverse order they were added, all 980 * the hooks have to be added to the head! 981 */ 982 983 NET_LOCK(); 984 if_hooks_run(&ifp->if_detachhooks); 985 NET_UNLOCK(); 986 } 987 988 void 989 if_detachhook_add(struct ifnet *ifp, struct task *t) 990 { 991 mtx_enter(&if_hooks_mtx); 992 TAILQ_INSERT_HEAD(&ifp->if_detachhooks, t, t_entry); 993 mtx_leave(&if_hooks_mtx); 994 } 995 996 void 997 if_detachhook_del(struct ifnet *ifp, struct task *t) 998 { 999 mtx_enter(&if_hooks_mtx); 1000 TAILQ_REMOVE(&ifp->if_detachhooks, t, t_entry); 1001 mtx_leave(&if_hooks_mtx); 1002 } 1003 1004 /* 1005 * Detach an interface from everything in the kernel. Also deallocate 1006 * private resources. 1007 */ 1008 void 1009 if_detach(struct ifnet *ifp) 1010 { 1011 struct ifaddr *ifa; 1012 struct ifg_list *ifg; 1013 struct domain *dp; 1014 int i, s; 1015 1016 /* Undo pseudo-driver changes. */ 1017 if_deactivate(ifp); 1018 1019 /* Other CPUs must not have a reference before we start destroying. */ 1020 if_remove(ifp); 1021 1022 ifq_clr_oactive(&ifp->if_snd); 1023 1024 #if NBPFILTER > 0 1025 bpfdetach(ifp); 1026 #endif 1027 1028 NET_LOCK(); 1029 s = splnet(); 1030 ifp->if_qstart = if_detached_qstart; 1031 ifp->if_ioctl = if_detached_ioctl; 1032 ifp->if_watchdog = NULL; 1033 1034 /* Remove the watchdog timeout & task */ 1035 timeout_del(&ifp->if_slowtimo); 1036 task_del(net_tq(ifp->if_index), &ifp->if_watchdogtask); 1037 1038 /* Remove the link state task */ 1039 task_del(net_tq(ifp->if_index), &ifp->if_linkstatetask); 1040 1041 rti_delete(ifp); 1042 #if NETHER > 0 && defined(NFSCLIENT) 1043 if (ifp->if_index == revarp_ifidx) 1044 revarp_ifidx = 0; 1045 #endif 1046 #ifdef MROUTING 1047 vif_delete(ifp); 1048 #endif 1049 in_ifdetach(ifp); 1050 #ifdef INET6 1051 in6_ifdetach(ifp); 1052 #endif 1053 #if NPF > 0 1054 pfi_detach_ifnet(ifp); 1055 #endif 1056 1057 while ((ifg = TAILQ_FIRST(&ifp->if_groups)) != NULL) 1058 if_delgroup(ifp, ifg->ifgl_group->ifg_group); 1059 1060 if_free_sadl(ifp); 1061 1062 /* We should not have any address left at this point. */ 1063 if (!TAILQ_EMPTY(&ifp->if_addrlist)) { 1064 #ifdef DIAGNOSTIC 1065 printf("%s: address list non empty\n", ifp->if_xname); 1066 #endif 1067 while ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) != NULL) { 1068 ifa_del(ifp, ifa); 1069 ifa->ifa_ifp = NULL; 1070 ifafree(ifa); 1071 } 1072 } 1073 1074 KASSERT(TAILQ_EMPTY(&ifp->if_addrhooks)); 1075 KASSERT(TAILQ_EMPTY(&ifp->if_linkstatehooks)); 1076 KASSERT(TAILQ_EMPTY(&ifp->if_detachhooks)); 1077 1078 for (i = 0; (dp = domains[i]) != NULL; i++) { 1079 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 1080 (*dp->dom_ifdetach)(ifp, 1081 ifp->if_afdata[dp->dom_family]); 1082 } 1083 1084 /* Announce that the interface is gone. */ 1085 rtm_ifannounce(ifp, IFAN_DEPARTURE); 1086 splx(s); 1087 NET_UNLOCK(); 1088 1089 if (ifp->if_counters != NULL) 1090 if_counters_free(ifp); 1091 1092 for (i = 0; i < ifp->if_nifqs; i++) 1093 ifq_destroy(ifp->if_ifqs[i]); 1094 if (ifp->if_ifqs != ifp->if_snd.ifq_ifqs) { 1095 for (i = 1; i < ifp->if_nifqs; i++) { 1096 free(ifp->if_ifqs[i], M_DEVBUF, 1097 sizeof(struct ifqueue)); 1098 } 1099 free(ifp->if_ifqs, M_DEVBUF, 1100 sizeof(struct ifqueue *) * ifp->if_nifqs); 1101 } 1102 1103 for (i = 0; i < ifp->if_niqs; i++) 1104 ifiq_destroy(ifp->if_iqs[i]); 1105 if (ifp->if_iqs != ifp->if_rcv.ifiq_ifiqs) { 1106 for (i = 1; i < ifp->if_niqs; i++) { 1107 free(ifp->if_iqs[i], M_DEVBUF, 1108 sizeof(struct ifiqueue)); 1109 } 1110 free(ifp->if_iqs, M_DEVBUF, 1111 sizeof(struct ifiqueue *) * ifp->if_niqs); 1112 } 1113 } 1114 1115 /* 1116 * Returns true if ``ifp0'' is connected to the interface with index ``ifidx''. 1117 */ 1118 int 1119 if_isconnected(const struct ifnet *ifp0, unsigned int ifidx) 1120 { 1121 struct ifnet *ifp; 1122 int connected = 0; 1123 1124 ifp = if_get(ifidx); 1125 if (ifp == NULL) 1126 return (0); 1127 1128 if (ifp0->if_index == ifp->if_index) 1129 connected = 1; 1130 1131 #if NBRIDGE > 0 1132 if (ifp0->if_bridgeidx != 0 && ifp0->if_bridgeidx == ifp->if_bridgeidx) 1133 connected = 1; 1134 #endif 1135 #if NCARP > 0 1136 if ((ifp0->if_type == IFT_CARP && 1137 ifp0->if_carpdevidx == ifp->if_index) || 1138 (ifp->if_type == IFT_CARP && ifp->if_carpdevidx == ifp0->if_index)) 1139 connected = 1; 1140 #endif 1141 1142 if_put(ifp); 1143 return (connected); 1144 } 1145 1146 /* 1147 * Create a clone network interface. 1148 */ 1149 int 1150 if_clone_create(const char *name, int rdomain) 1151 { 1152 struct if_clone *ifc; 1153 struct ifnet *ifp; 1154 int unit, ret; 1155 1156 ifc = if_clone_lookup(name, &unit); 1157 if (ifc == NULL) 1158 return (EINVAL); 1159 1160 rw_enter_write(&if_cloners_lock); 1161 1162 if ((ifp = if_unit(name)) != NULL) { 1163 ret = EEXIST; 1164 goto unlock; 1165 } 1166 1167 ret = (*ifc->ifc_create)(ifc, unit); 1168 1169 if (ret != 0 || (ifp = if_unit(name)) == NULL) 1170 goto unlock; 1171 1172 NET_LOCK(); 1173 if_addgroup(ifp, ifc->ifc_name); 1174 if (rdomain != 0) 1175 if_setrdomain(ifp, rdomain); 1176 NET_UNLOCK(); 1177 unlock: 1178 rw_exit_write(&if_cloners_lock); 1179 if_put(ifp); 1180 1181 return (ret); 1182 } 1183 1184 /* 1185 * Destroy a clone network interface. 1186 */ 1187 int 1188 if_clone_destroy(const char *name) 1189 { 1190 struct if_clone *ifc; 1191 struct ifnet *ifp; 1192 int ret; 1193 1194 ifc = if_clone_lookup(name, NULL); 1195 if (ifc == NULL) 1196 return (EINVAL); 1197 1198 if (ifc->ifc_destroy == NULL) 1199 return (EOPNOTSUPP); 1200 1201 rw_enter_write(&if_cloners_lock); 1202 1203 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1204 if (strcmp(ifp->if_xname, name) == 0) 1205 break; 1206 } 1207 if (ifp == NULL) { 1208 rw_exit_write(&if_cloners_lock); 1209 return (ENXIO); 1210 } 1211 1212 NET_LOCK(); 1213 if (ifp->if_flags & IFF_UP) { 1214 int s; 1215 s = splnet(); 1216 if_down(ifp); 1217 splx(s); 1218 } 1219 NET_UNLOCK(); 1220 ret = (*ifc->ifc_destroy)(ifp); 1221 1222 rw_exit_write(&if_cloners_lock); 1223 1224 return (ret); 1225 } 1226 1227 /* 1228 * Look up a network interface cloner. 1229 */ 1230 struct if_clone * 1231 if_clone_lookup(const char *name, int *unitp) 1232 { 1233 struct if_clone *ifc; 1234 const char *cp; 1235 int unit; 1236 1237 /* separate interface name from unit */ 1238 for (cp = name; 1239 cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9'); 1240 cp++) 1241 continue; 1242 1243 if (cp == name || cp - name == IFNAMSIZ || !*cp) 1244 return (NULL); /* No name or unit number */ 1245 1246 if (cp - name < IFNAMSIZ-1 && *cp == '0' && cp[1] != '\0') 1247 return (NULL); /* unit number 0 padded */ 1248 1249 LIST_FOREACH(ifc, &if_cloners, ifc_list) { 1250 if (strlen(ifc->ifc_name) == cp - name && 1251 !strncmp(name, ifc->ifc_name, cp - name)) 1252 break; 1253 } 1254 1255 if (ifc == NULL) 1256 return (NULL); 1257 1258 unit = 0; 1259 while (cp - name < IFNAMSIZ && *cp) { 1260 if (*cp < '0' || *cp > '9' || 1261 unit > (INT_MAX - (*cp - '0')) / 10) { 1262 /* Bogus unit number. */ 1263 return (NULL); 1264 } 1265 unit = (unit * 10) + (*cp++ - '0'); 1266 } 1267 1268 if (unitp != NULL) 1269 *unitp = unit; 1270 return (ifc); 1271 } 1272 1273 /* 1274 * Register a network interface cloner. 1275 */ 1276 void 1277 if_clone_attach(struct if_clone *ifc) 1278 { 1279 /* 1280 * we are called at kernel boot by main(), when pseudo devices are 1281 * being attached. The main() is the only guy which may alter the 1282 * if_cloners. While system is running and main() is done with 1283 * initialization, the if_cloners becomes immutable. 1284 */ 1285 KASSERT(pdevinit_done == 0); 1286 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list); 1287 if_cloners_count++; 1288 } 1289 1290 /* 1291 * Provide list of interface cloners to userspace. 1292 */ 1293 int 1294 if_clone_list(struct if_clonereq *ifcr) 1295 { 1296 char outbuf[IFNAMSIZ], *dst; 1297 struct if_clone *ifc; 1298 int count, error = 0; 1299 1300 if ((dst = ifcr->ifcr_buffer) == NULL) { 1301 /* Just asking how many there are. */ 1302 ifcr->ifcr_total = if_cloners_count; 1303 return (0); 1304 } 1305 1306 if (ifcr->ifcr_count < 0) 1307 return (EINVAL); 1308 1309 ifcr->ifcr_total = if_cloners_count; 1310 count = MIN(if_cloners_count, ifcr->ifcr_count); 1311 1312 LIST_FOREACH(ifc, &if_cloners, ifc_list) { 1313 if (count == 0) 1314 break; 1315 bzero(outbuf, sizeof outbuf); 1316 strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ); 1317 error = copyout(outbuf, dst, IFNAMSIZ); 1318 if (error) 1319 break; 1320 count--; 1321 dst += IFNAMSIZ; 1322 } 1323 1324 return (error); 1325 } 1326 1327 /* 1328 * set queue congestion marker 1329 */ 1330 void 1331 if_congestion(void) 1332 { 1333 extern int ticks; 1334 1335 ifq_congestion = ticks; 1336 } 1337 1338 int 1339 if_congested(void) 1340 { 1341 extern int ticks; 1342 int diff; 1343 1344 diff = ticks - ifq_congestion; 1345 if (diff < 0) { 1346 ifq_congestion = ticks - hz; 1347 return (0); 1348 } 1349 1350 return (diff <= (hz / 100)); 1351 } 1352 1353 #define equal(a1, a2) \ 1354 (bcmp((caddr_t)(a1), (caddr_t)(a2), \ 1355 (a1)->sa_len) == 0) 1356 1357 /* 1358 * Locate an interface based on a complete address. 1359 */ 1360 struct ifaddr * 1361 ifa_ifwithaddr(struct sockaddr *addr, u_int rtableid) 1362 { 1363 struct ifnet *ifp; 1364 struct ifaddr *ifa; 1365 u_int rdomain; 1366 1367 rdomain = rtable_l2(rtableid); 1368 KERNEL_LOCK(); 1369 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1370 if (ifp->if_rdomain != rdomain) 1371 continue; 1372 1373 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1374 if (ifa->ifa_addr->sa_family != addr->sa_family) 1375 continue; 1376 1377 if (equal(addr, ifa->ifa_addr)) { 1378 KERNEL_UNLOCK(); 1379 return (ifa); 1380 } 1381 } 1382 } 1383 KERNEL_UNLOCK(); 1384 return (NULL); 1385 } 1386 1387 /* 1388 * Locate the point to point interface with a given destination address. 1389 */ 1390 struct ifaddr * 1391 ifa_ifwithdstaddr(struct sockaddr *addr, u_int rdomain) 1392 { 1393 struct ifnet *ifp; 1394 struct ifaddr *ifa; 1395 1396 rdomain = rtable_l2(rdomain); 1397 KERNEL_LOCK(); 1398 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1399 if (ifp->if_rdomain != rdomain) 1400 continue; 1401 if (ifp->if_flags & IFF_POINTOPOINT) { 1402 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1403 if (ifa->ifa_addr->sa_family != 1404 addr->sa_family || ifa->ifa_dstaddr == NULL) 1405 continue; 1406 if (equal(addr, ifa->ifa_dstaddr)) { 1407 KERNEL_UNLOCK(); 1408 return (ifa); 1409 } 1410 } 1411 } 1412 } 1413 KERNEL_UNLOCK(); 1414 return (NULL); 1415 } 1416 1417 /* 1418 * Find an interface address specific to an interface best matching 1419 * a given address. 1420 */ 1421 struct ifaddr * 1422 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 1423 { 1424 struct ifaddr *ifa; 1425 char *cp, *cp2, *cp3; 1426 char *cplim; 1427 struct ifaddr *ifa_maybe = NULL; 1428 u_int af = addr->sa_family; 1429 1430 if (af >= AF_MAX) 1431 return (NULL); 1432 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1433 if (ifa->ifa_addr->sa_family != af) 1434 continue; 1435 if (ifa_maybe == NULL) 1436 ifa_maybe = ifa; 1437 if (ifa->ifa_netmask == 0 || ifp->if_flags & IFF_POINTOPOINT) { 1438 if (equal(addr, ifa->ifa_addr) || 1439 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))) 1440 return (ifa); 1441 continue; 1442 } 1443 cp = addr->sa_data; 1444 cp2 = ifa->ifa_addr->sa_data; 1445 cp3 = ifa->ifa_netmask->sa_data; 1446 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1447 for (; cp3 < cplim; cp3++) 1448 if ((*cp++ ^ *cp2++) & *cp3) 1449 break; 1450 if (cp3 == cplim) 1451 return (ifa); 1452 } 1453 return (ifa_maybe); 1454 } 1455 1456 void 1457 if_rtrequest_dummy(struct ifnet *ifp, int req, struct rtentry *rt) 1458 { 1459 } 1460 1461 /* 1462 * Default action when installing a local route on a point-to-point 1463 * interface. 1464 */ 1465 void 1466 p2p_rtrequest(struct ifnet *ifp, int req, struct rtentry *rt) 1467 { 1468 struct ifnet *lo0ifp; 1469 struct ifaddr *ifa, *lo0ifa; 1470 1471 switch (req) { 1472 case RTM_ADD: 1473 if (!ISSET(rt->rt_flags, RTF_LOCAL)) 1474 break; 1475 1476 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1477 if (memcmp(rt_key(rt), ifa->ifa_addr, 1478 rt_key(rt)->sa_len) == 0) 1479 break; 1480 } 1481 1482 if (ifa == NULL) 1483 break; 1484 1485 KASSERT(ifa == rt->rt_ifa); 1486 1487 lo0ifp = if_get(rtable_loindex(ifp->if_rdomain)); 1488 KASSERT(lo0ifp != NULL); 1489 TAILQ_FOREACH(lo0ifa, &lo0ifp->if_addrlist, ifa_list) { 1490 if (lo0ifa->ifa_addr->sa_family == 1491 ifa->ifa_addr->sa_family) 1492 break; 1493 } 1494 if_put(lo0ifp); 1495 1496 if (lo0ifa == NULL) 1497 break; 1498 1499 rt->rt_flags &= ~RTF_LLINFO; 1500 break; 1501 case RTM_DELETE: 1502 case RTM_RESOLVE: 1503 default: 1504 break; 1505 } 1506 } 1507 1508 int 1509 p2p_bpf_mtap(caddr_t if_bpf, const struct mbuf *m, u_int dir) 1510 { 1511 #if NBPFILTER > 0 1512 return (bpf_mtap_af(if_bpf, m->m_pkthdr.ph_family, m, dir)); 1513 #else 1514 return (0); 1515 #endif 1516 } 1517 1518 void 1519 p2p_input(struct ifnet *ifp, struct mbuf *m) 1520 { 1521 void (*input)(struct ifnet *, struct mbuf *); 1522 1523 switch (m->m_pkthdr.ph_family) { 1524 case AF_INET: 1525 input = ipv4_input; 1526 break; 1527 #ifdef INET6 1528 case AF_INET6: 1529 input = ipv6_input; 1530 break; 1531 #endif 1532 #ifdef MPLS 1533 case AF_MPLS: 1534 input = mpls_input; 1535 break; 1536 #endif 1537 default: 1538 m_freem(m); 1539 return; 1540 } 1541 1542 (*input)(ifp, m); 1543 } 1544 1545 /* 1546 * Bring down all interfaces 1547 */ 1548 void 1549 if_downall(void) 1550 { 1551 struct ifreq ifrq; /* XXX only partly built */ 1552 struct ifnet *ifp; 1553 1554 NET_LOCK(); 1555 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1556 if ((ifp->if_flags & IFF_UP) == 0) 1557 continue; 1558 if_down(ifp); 1559 ifrq.ifr_flags = ifp->if_flags; 1560 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq); 1561 } 1562 NET_UNLOCK(); 1563 } 1564 1565 /* 1566 * Mark an interface down and notify protocols of 1567 * the transition. 1568 */ 1569 void 1570 if_down(struct ifnet *ifp) 1571 { 1572 NET_ASSERT_LOCKED(); 1573 1574 ifp->if_flags &= ~IFF_UP; 1575 getmicrotime(&ifp->if_lastchange); 1576 ifq_purge(&ifp->if_snd); 1577 1578 if_linkstate(ifp); 1579 } 1580 1581 /* 1582 * Mark an interface up and notify protocols of 1583 * the transition. 1584 */ 1585 void 1586 if_up(struct ifnet *ifp) 1587 { 1588 NET_ASSERT_LOCKED(); 1589 1590 ifp->if_flags |= IFF_UP; 1591 getmicrotime(&ifp->if_lastchange); 1592 1593 #ifdef INET6 1594 /* Userland expects the kernel to set ::1 on default lo(4). */ 1595 if (ifp->if_index == rtable_loindex(ifp->if_rdomain)) 1596 in6_ifattach(ifp); 1597 #endif 1598 1599 if_linkstate(ifp); 1600 } 1601 1602 /* 1603 * Notify userland, the routing table and hooks owner of 1604 * a link-state transition. 1605 */ 1606 void 1607 if_linkstate_task(void *xifidx) 1608 { 1609 unsigned int ifidx = (unsigned long)xifidx; 1610 struct ifnet *ifp; 1611 1612 KERNEL_LOCK(); 1613 NET_LOCK(); 1614 1615 ifp = if_get(ifidx); 1616 if (ifp != NULL) 1617 if_linkstate(ifp); 1618 if_put(ifp); 1619 1620 NET_UNLOCK(); 1621 KERNEL_UNLOCK(); 1622 } 1623 1624 void 1625 if_linkstate(struct ifnet *ifp) 1626 { 1627 NET_ASSERT_LOCKED(); 1628 1629 rtm_ifchg(ifp); 1630 rt_if_track(ifp); 1631 1632 if_hooks_run(&ifp->if_linkstatehooks); 1633 } 1634 1635 void 1636 if_linkstatehook_add(struct ifnet *ifp, struct task *t) 1637 { 1638 mtx_enter(&if_hooks_mtx); 1639 TAILQ_INSERT_HEAD(&ifp->if_linkstatehooks, t, t_entry); 1640 mtx_leave(&if_hooks_mtx); 1641 } 1642 1643 void 1644 if_linkstatehook_del(struct ifnet *ifp, struct task *t) 1645 { 1646 mtx_enter(&if_hooks_mtx); 1647 TAILQ_REMOVE(&ifp->if_linkstatehooks, t, t_entry); 1648 mtx_leave(&if_hooks_mtx); 1649 } 1650 1651 /* 1652 * Schedule a link state change task. 1653 */ 1654 void 1655 if_link_state_change(struct ifnet *ifp) 1656 { 1657 task_add(net_tq(ifp->if_index), &ifp->if_linkstatetask); 1658 } 1659 1660 /* 1661 * Handle interface watchdog timer routine. Called 1662 * from softclock, we decrement timer (if set) and 1663 * call the appropriate interface routine on expiration. 1664 */ 1665 void 1666 if_slowtimo(void *arg) 1667 { 1668 struct ifnet *ifp = arg; 1669 int s = splnet(); 1670 1671 if (ifp->if_watchdog) { 1672 if (ifp->if_timer > 0 && --ifp->if_timer == 0) 1673 task_add(net_tq(ifp->if_index), &ifp->if_watchdogtask); 1674 timeout_add_sec(&ifp->if_slowtimo, IFNET_SLOWTIMO); 1675 } 1676 splx(s); 1677 } 1678 1679 void 1680 if_watchdog_task(void *xifidx) 1681 { 1682 unsigned int ifidx = (unsigned long)xifidx; 1683 struct ifnet *ifp; 1684 int s; 1685 1686 ifp = if_get(ifidx); 1687 if (ifp == NULL) 1688 return; 1689 1690 KERNEL_LOCK(); 1691 s = splnet(); 1692 if (ifp->if_watchdog) 1693 (*ifp->if_watchdog)(ifp); 1694 splx(s); 1695 KERNEL_UNLOCK(); 1696 1697 if_put(ifp); 1698 } 1699 1700 /* 1701 * Map interface name to interface structure pointer. 1702 */ 1703 struct ifnet * 1704 if_unit(const char *name) 1705 { 1706 struct ifnet *ifp; 1707 1708 KERNEL_ASSERT_LOCKED(); 1709 1710 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1711 if (strcmp(ifp->if_xname, name) == 0) { 1712 if_ref(ifp); 1713 return (ifp); 1714 } 1715 } 1716 1717 return (NULL); 1718 } 1719 1720 /* 1721 * Map interface index to interface structure pointer. 1722 */ 1723 struct ifnet * 1724 if_get(unsigned int index) 1725 { 1726 struct srp_ref sr; 1727 struct if_map *if_map; 1728 struct srp *map; 1729 struct ifnet *ifp = NULL; 1730 1731 if_map = srp_enter(&sr, &if_idxmap.map); 1732 if (index < if_map->limit) { 1733 map = (struct srp *)(if_map + 1); 1734 1735 ifp = srp_follow(&sr, &map[index]); 1736 if (ifp != NULL) { 1737 KASSERT(ifp->if_index == index); 1738 if_ref(ifp); 1739 } 1740 } 1741 srp_leave(&sr); 1742 1743 return (ifp); 1744 } 1745 1746 struct ifnet * 1747 if_ref(struct ifnet *ifp) 1748 { 1749 refcnt_take(&ifp->if_refcnt); 1750 1751 return (ifp); 1752 } 1753 1754 void 1755 if_put(struct ifnet *ifp) 1756 { 1757 if (ifp == NULL) 1758 return; 1759 1760 refcnt_rele_wake(&ifp->if_refcnt); 1761 } 1762 1763 int 1764 if_setlladdr(struct ifnet *ifp, const uint8_t *lladdr) 1765 { 1766 if (ifp->if_sadl == NULL) 1767 return (EINVAL); 1768 1769 memcpy(((struct arpcom *)ifp)->ac_enaddr, lladdr, ETHER_ADDR_LEN); 1770 memcpy(LLADDR(ifp->if_sadl), lladdr, ETHER_ADDR_LEN); 1771 1772 return (0); 1773 } 1774 1775 int 1776 if_createrdomain(int rdomain, struct ifnet *ifp) 1777 { 1778 int error; 1779 struct ifnet *loifp; 1780 char loifname[IFNAMSIZ]; 1781 unsigned int unit = rdomain; 1782 1783 if ((error = rtable_add(rdomain)) != 0) 1784 return (error); 1785 if (!rtable_empty(rdomain)) 1786 return (EEXIST); 1787 1788 /* Create rdomain including its loopback if with unit == rdomain */ 1789 snprintf(loifname, sizeof(loifname), "lo%u", unit); 1790 error = if_clone_create(loifname, 0); 1791 if ((loifp = if_unit(loifname)) == NULL) 1792 return (ENXIO); 1793 if (error && (ifp != loifp || error != EEXIST)) { 1794 if_put(loifp); 1795 return (error); 1796 } 1797 1798 rtable_l2set(rdomain, rdomain, loifp->if_index); 1799 loifp->if_rdomain = rdomain; 1800 if_put(loifp); 1801 1802 return (0); 1803 } 1804 1805 int 1806 if_setrdomain(struct ifnet *ifp, int rdomain) 1807 { 1808 struct ifreq ifr; 1809 int error, up = 0, s; 1810 1811 if (rdomain < 0 || rdomain > RT_TABLEID_MAX) 1812 return (EINVAL); 1813 1814 if (rdomain != ifp->if_rdomain && 1815 (ifp->if_flags & IFF_LOOPBACK) && 1816 (ifp->if_index == rtable_loindex(ifp->if_rdomain))) 1817 return (EPERM); 1818 1819 if (!rtable_exists(rdomain)) 1820 return (ESRCH); 1821 1822 /* make sure that the routing table is a real rdomain */ 1823 if (rdomain != rtable_l2(rdomain)) 1824 return (EINVAL); 1825 1826 if (rdomain != ifp->if_rdomain) { 1827 s = splnet(); 1828 /* 1829 * We are tearing down the world. 1830 * Take down the IF so: 1831 * 1. everything that cares gets a message 1832 * 2. the automagic IPv6 bits are recreated 1833 */ 1834 if (ifp->if_flags & IFF_UP) { 1835 up = 1; 1836 if_down(ifp); 1837 } 1838 rti_delete(ifp); 1839 #ifdef MROUTING 1840 vif_delete(ifp); 1841 #endif 1842 in_ifdetach(ifp); 1843 #ifdef INET6 1844 in6_ifdetach(ifp); 1845 #endif 1846 splx(s); 1847 } 1848 1849 /* Let devices like enc(4) or mpe(4) know about the change */ 1850 ifr.ifr_rdomainid = rdomain; 1851 if ((error = (*ifp->if_ioctl)(ifp, SIOCSIFRDOMAIN, 1852 (caddr_t)&ifr)) != ENOTTY) 1853 return (error); 1854 error = 0; 1855 1856 /* Add interface to the specified rdomain */ 1857 ifp->if_rdomain = rdomain; 1858 1859 /* If we took down the IF, bring it back */ 1860 if (up) { 1861 s = splnet(); 1862 if_up(ifp); 1863 splx(s); 1864 } 1865 1866 return (0); 1867 } 1868 1869 /* 1870 * Interface ioctls. 1871 */ 1872 int 1873 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p) 1874 { 1875 struct ifnet *ifp; 1876 struct ifreq *ifr = (struct ifreq *)data; 1877 struct ifgroupreq *ifgr = (struct ifgroupreq *)data; 1878 struct if_afreq *ifar = (struct if_afreq *)data; 1879 char ifdescrbuf[IFDESCRSIZE]; 1880 char ifrtlabelbuf[RTLABEL_LEN]; 1881 int s, error = 0, oif_xflags; 1882 size_t bytesdone; 1883 unsigned short oif_flags; 1884 1885 switch (cmd) { 1886 case SIOCIFCREATE: 1887 if ((error = suser(p)) != 0) 1888 return (error); 1889 error = if_clone_create(ifr->ifr_name, 0); 1890 return (error); 1891 case SIOCIFDESTROY: 1892 if ((error = suser(p)) != 0) 1893 return (error); 1894 error = if_clone_destroy(ifr->ifr_name); 1895 return (error); 1896 case SIOCSIFGATTR: 1897 if ((error = suser(p)) != 0) 1898 return (error); 1899 NET_LOCK(); 1900 error = if_setgroupattribs(data); 1901 NET_UNLOCK(); 1902 return (error); 1903 case SIOCGIFCONF: 1904 case SIOCIFGCLONERS: 1905 case SIOCGIFGMEMB: 1906 case SIOCGIFGATTR: 1907 case SIOCGIFGLIST: 1908 case SIOCGIFFLAGS: 1909 case SIOCGIFXFLAGS: 1910 case SIOCGIFMETRIC: 1911 case SIOCGIFMTU: 1912 case SIOCGIFHARDMTU: 1913 case SIOCGIFDATA: 1914 case SIOCGIFDESCR: 1915 case SIOCGIFRTLABEL: 1916 case SIOCGIFPRIORITY: 1917 case SIOCGIFRDOMAIN: 1918 case SIOCGIFGROUP: 1919 case SIOCGIFLLPRIO: 1920 return (ifioctl_get(cmd, data)); 1921 } 1922 1923 ifp = if_unit(ifr->ifr_name); 1924 if (ifp == NULL) 1925 return (ENXIO); 1926 oif_flags = ifp->if_flags; 1927 oif_xflags = ifp->if_xflags; 1928 1929 switch (cmd) { 1930 case SIOCIFAFATTACH: 1931 case SIOCIFAFDETACH: 1932 if ((error = suser(p)) != 0) 1933 break; 1934 NET_LOCK(); 1935 switch (ifar->ifar_af) { 1936 case AF_INET: 1937 /* attach is a noop for AF_INET */ 1938 if (cmd == SIOCIFAFDETACH) 1939 in_ifdetach(ifp); 1940 break; 1941 #ifdef INET6 1942 case AF_INET6: 1943 if (cmd == SIOCIFAFATTACH) 1944 error = in6_ifattach(ifp); 1945 else 1946 in6_ifdetach(ifp); 1947 break; 1948 #endif /* INET6 */ 1949 default: 1950 error = EAFNOSUPPORT; 1951 } 1952 NET_UNLOCK(); 1953 break; 1954 1955 case SIOCSIFXFLAGS: 1956 if ((error = suser(p)) != 0) 1957 break; 1958 1959 NET_LOCK(); 1960 #ifdef INET6 1961 if ((ISSET(ifr->ifr_flags, IFXF_AUTOCONF6) || 1962 ISSET(ifr->ifr_flags, IFXF_AUTOCONF6TEMP)) && 1963 !ISSET(ifp->if_xflags, IFXF_AUTOCONF6) && 1964 !ISSET(ifp->if_xflags, IFXF_AUTOCONF6TEMP)) { 1965 error = in6_ifattach(ifp); 1966 if (error != 0) { 1967 NET_UNLOCK(); 1968 break; 1969 } 1970 } 1971 1972 if (ISSET(ifr->ifr_flags, IFXF_INET6_NOSOII) && 1973 !ISSET(ifp->if_xflags, IFXF_INET6_NOSOII)) 1974 ifp->if_xflags |= IFXF_INET6_NOSOII; 1975 1976 if (!ISSET(ifr->ifr_flags, IFXF_INET6_NOSOII) && 1977 ISSET(ifp->if_xflags, IFXF_INET6_NOSOII)) 1978 ifp->if_xflags &= ~IFXF_INET6_NOSOII; 1979 1980 #endif /* INET6 */ 1981 1982 #ifdef MPLS 1983 if (ISSET(ifr->ifr_flags, IFXF_MPLS) && 1984 !ISSET(ifp->if_xflags, IFXF_MPLS)) { 1985 s = splnet(); 1986 ifp->if_xflags |= IFXF_MPLS; 1987 ifp->if_ll_output = ifp->if_output; 1988 ifp->if_output = mpls_output; 1989 splx(s); 1990 } 1991 if (ISSET(ifp->if_xflags, IFXF_MPLS) && 1992 !ISSET(ifr->ifr_flags, IFXF_MPLS)) { 1993 s = splnet(); 1994 ifp->if_xflags &= ~IFXF_MPLS; 1995 ifp->if_output = ifp->if_ll_output; 1996 ifp->if_ll_output = NULL; 1997 splx(s); 1998 } 1999 #endif /* MPLS */ 2000 2001 #ifndef SMALL_KERNEL 2002 if (ifp->if_capabilities & IFCAP_WOL) { 2003 if (ISSET(ifr->ifr_flags, IFXF_WOL) && 2004 !ISSET(ifp->if_xflags, IFXF_WOL)) { 2005 s = splnet(); 2006 ifp->if_xflags |= IFXF_WOL; 2007 error = ifp->if_wol(ifp, 1); 2008 splx(s); 2009 } 2010 if (ISSET(ifp->if_xflags, IFXF_WOL) && 2011 !ISSET(ifr->ifr_flags, IFXF_WOL)) { 2012 s = splnet(); 2013 ifp->if_xflags &= ~IFXF_WOL; 2014 error = ifp->if_wol(ifp, 0); 2015 splx(s); 2016 } 2017 } else if (ISSET(ifr->ifr_flags, IFXF_WOL)) { 2018 ifr->ifr_flags &= ~IFXF_WOL; 2019 error = ENOTSUP; 2020 } 2021 #endif 2022 2023 if (error == 0) 2024 ifp->if_xflags = (ifp->if_xflags & IFXF_CANTCHANGE) | 2025 (ifr->ifr_flags & ~IFXF_CANTCHANGE); 2026 2027 if (!ISSET(ifp->if_flags, IFF_UP) && 2028 ((!ISSET(oif_xflags, IFXF_AUTOCONF4) && 2029 ISSET(ifp->if_xflags, IFXF_AUTOCONF4)) || 2030 (!ISSET(oif_xflags, IFXF_AUTOCONF6) && 2031 ISSET(ifp->if_xflags, IFXF_AUTOCONF6)) || 2032 (!ISSET(oif_xflags, IFXF_AUTOCONF6TEMP) && 2033 ISSET(ifp->if_xflags, IFXF_AUTOCONF6TEMP)))) { 2034 ifr->ifr_flags = ifp->if_flags | IFF_UP; 2035 cmd = SIOCSIFFLAGS; 2036 goto forceup; 2037 } 2038 2039 NET_UNLOCK(); 2040 break; 2041 2042 case SIOCSIFFLAGS: 2043 if ((error = suser(p)) != 0) 2044 break; 2045 2046 NET_LOCK(); 2047 forceup: 2048 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 2049 (ifr->ifr_flags & ~IFF_CANTCHANGE); 2050 error = (*ifp->if_ioctl)(ifp, cmd, data); 2051 if (error != 0) { 2052 ifp->if_flags = oif_flags; 2053 } else if (ISSET(oif_flags ^ ifp->if_flags, IFF_UP)) { 2054 s = splnet(); 2055 if (ISSET(ifp->if_flags, IFF_UP)) 2056 if_up(ifp); 2057 else 2058 if_down(ifp); 2059 splx(s); 2060 } 2061 NET_UNLOCK(); 2062 break; 2063 2064 case SIOCSIFMETRIC: 2065 if ((error = suser(p)) != 0) 2066 break; 2067 NET_LOCK(); 2068 ifp->if_metric = ifr->ifr_metric; 2069 NET_UNLOCK(); 2070 break; 2071 2072 case SIOCSIFMTU: 2073 if ((error = suser(p)) != 0) 2074 break; 2075 NET_LOCK(); 2076 error = (*ifp->if_ioctl)(ifp, cmd, data); 2077 NET_UNLOCK(); 2078 if (error == 0) 2079 rtm_ifchg(ifp); 2080 break; 2081 2082 case SIOCSIFDESCR: 2083 if ((error = suser(p)) != 0) 2084 break; 2085 error = copyinstr(ifr->ifr_data, ifdescrbuf, 2086 IFDESCRSIZE, &bytesdone); 2087 if (error == 0) { 2088 (void)memset(ifp->if_description, 0, IFDESCRSIZE); 2089 strlcpy(ifp->if_description, ifdescrbuf, IFDESCRSIZE); 2090 } 2091 break; 2092 2093 case SIOCSIFRTLABEL: 2094 if ((error = suser(p)) != 0) 2095 break; 2096 error = copyinstr(ifr->ifr_data, ifrtlabelbuf, 2097 RTLABEL_LEN, &bytesdone); 2098 if (error == 0) { 2099 rtlabel_unref(ifp->if_rtlabelid); 2100 ifp->if_rtlabelid = rtlabel_name2id(ifrtlabelbuf); 2101 } 2102 break; 2103 2104 case SIOCSIFPRIORITY: 2105 if ((error = suser(p)) != 0) 2106 break; 2107 if (ifr->ifr_metric < 0 || ifr->ifr_metric > 15) { 2108 error = EINVAL; 2109 break; 2110 } 2111 ifp->if_priority = ifr->ifr_metric; 2112 break; 2113 2114 case SIOCSIFRDOMAIN: 2115 if ((error = suser(p)) != 0) 2116 break; 2117 error = if_createrdomain(ifr->ifr_rdomainid, ifp); 2118 if (!error || error == EEXIST) { 2119 NET_LOCK(); 2120 error = if_setrdomain(ifp, ifr->ifr_rdomainid); 2121 NET_UNLOCK(); 2122 } 2123 break; 2124 2125 case SIOCAIFGROUP: 2126 if ((error = suser(p))) 2127 break; 2128 NET_LOCK(); 2129 error = if_addgroup(ifp, ifgr->ifgr_group); 2130 if (error == 0) { 2131 error = (*ifp->if_ioctl)(ifp, cmd, data); 2132 if (error == ENOTTY) 2133 error = 0; 2134 } 2135 NET_UNLOCK(); 2136 break; 2137 2138 case SIOCDIFGROUP: 2139 if ((error = suser(p))) 2140 break; 2141 NET_LOCK(); 2142 error = (*ifp->if_ioctl)(ifp, cmd, data); 2143 if (error == ENOTTY) 2144 error = 0; 2145 if (error == 0) 2146 error = if_delgroup(ifp, ifgr->ifgr_group); 2147 NET_UNLOCK(); 2148 break; 2149 2150 case SIOCSIFLLADDR: 2151 if ((error = suser(p))) 2152 break; 2153 if ((ifp->if_sadl == NULL) || 2154 (ifr->ifr_addr.sa_len != ETHER_ADDR_LEN) || 2155 (ETHER_IS_MULTICAST(ifr->ifr_addr.sa_data))) { 2156 error = EINVAL; 2157 break; 2158 } 2159 NET_LOCK(); 2160 switch (ifp->if_type) { 2161 case IFT_ETHER: 2162 case IFT_CARP: 2163 case IFT_XETHER: 2164 case IFT_ISO88025: 2165 error = (*ifp->if_ioctl)(ifp, cmd, data); 2166 if (error == ENOTTY) 2167 error = 0; 2168 if (error == 0) 2169 error = if_setlladdr(ifp, 2170 ifr->ifr_addr.sa_data); 2171 break; 2172 default: 2173 error = ENODEV; 2174 } 2175 2176 if (error == 0) 2177 ifnewlladdr(ifp); 2178 NET_UNLOCK(); 2179 if (error == 0) 2180 rtm_ifchg(ifp); 2181 break; 2182 2183 case SIOCSIFLLPRIO: 2184 if ((error = suser(p))) 2185 break; 2186 if (ifr->ifr_llprio < IFQ_MINPRIO || 2187 ifr->ifr_llprio > IFQ_MAXPRIO) { 2188 error = EINVAL; 2189 break; 2190 } 2191 NET_LOCK(); 2192 ifp->if_llprio = ifr->ifr_llprio; 2193 NET_UNLOCK(); 2194 break; 2195 2196 case SIOCGIFSFFPAGE: 2197 error = suser(p); 2198 if (error != 0) 2199 break; 2200 2201 error = if_sffpage_check(data); 2202 if (error != 0) 2203 break; 2204 2205 /* don't take NET_LOCK because i2c reads take a long time */ 2206 error = ((*ifp->if_ioctl)(ifp, cmd, data)); 2207 break; 2208 2209 case SIOCSETKALIVE: 2210 case SIOCDIFPHYADDR: 2211 case SIOCSLIFPHYADDR: 2212 case SIOCSLIFPHYRTABLE: 2213 case SIOCSLIFPHYTTL: 2214 case SIOCSLIFPHYDF: 2215 case SIOCSLIFPHYECN: 2216 case SIOCADDMULTI: 2217 case SIOCDELMULTI: 2218 case SIOCSIFMEDIA: 2219 case SIOCSVNETID: 2220 case SIOCDVNETID: 2221 case SIOCSVNETFLOWID: 2222 case SIOCSTXHPRIO: 2223 case SIOCSRXHPRIO: 2224 case SIOCSIFPAIR: 2225 case SIOCSIFPARENT: 2226 case SIOCDIFPARENT: 2227 case SIOCSETMPWCFG: 2228 case SIOCSETLABEL: 2229 case SIOCDELLABEL: 2230 case SIOCSPWE3CTRLWORD: 2231 case SIOCSPWE3FAT: 2232 case SIOCSPWE3NEIGHBOR: 2233 case SIOCDPWE3NEIGHBOR: 2234 #if NBRIDGE > 0 2235 case SIOCBRDGADD: 2236 case SIOCBRDGDEL: 2237 case SIOCBRDGSIFFLGS: 2238 case SIOCBRDGSCACHE: 2239 case SIOCBRDGADDS: 2240 case SIOCBRDGDELS: 2241 case SIOCBRDGSADDR: 2242 case SIOCBRDGSTO: 2243 case SIOCBRDGDADDR: 2244 case SIOCBRDGFLUSH: 2245 case SIOCBRDGADDL: 2246 case SIOCBRDGSIFPROT: 2247 case SIOCBRDGARL: 2248 case SIOCBRDGFRL: 2249 case SIOCBRDGSPRI: 2250 case SIOCBRDGSHT: 2251 case SIOCBRDGSFD: 2252 case SIOCBRDGSMA: 2253 case SIOCBRDGSIFPRIO: 2254 case SIOCBRDGSIFCOST: 2255 case SIOCBRDGSTXHC: 2256 case SIOCBRDGSPROTO: 2257 case SIOCSWSPORTNO: 2258 #endif 2259 if ((error = suser(p)) != 0) 2260 break; 2261 /* FALLTHROUGH */ 2262 default: 2263 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, 2264 (struct mbuf *) cmd, (struct mbuf *) data, 2265 (struct mbuf *) ifp, p)); 2266 if (error != EOPNOTSUPP) 2267 break; 2268 switch (cmd) { 2269 case SIOCAIFADDR: 2270 case SIOCDIFADDR: 2271 case SIOCSIFADDR: 2272 case SIOCSIFNETMASK: 2273 case SIOCSIFDSTADDR: 2274 case SIOCSIFBRDADDR: 2275 #ifdef INET6 2276 case SIOCAIFADDR_IN6: 2277 case SIOCDIFADDR_IN6: 2278 #endif 2279 error = suser(p); 2280 break; 2281 default: 2282 error = 0; 2283 break; 2284 } 2285 if (error) 2286 break; 2287 NET_LOCK(); 2288 error = ((*ifp->if_ioctl)(ifp, cmd, data)); 2289 NET_UNLOCK(); 2290 break; 2291 } 2292 2293 if (oif_flags != ifp->if_flags || oif_xflags != ifp->if_xflags) { 2294 /* if_up() and if_down() already sent an update, skip here */ 2295 if (((oif_flags ^ ifp->if_flags) & IFF_UP) == 0) 2296 rtm_ifchg(ifp); 2297 } 2298 2299 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) 2300 getmicrotime(&ifp->if_lastchange); 2301 2302 if_put(ifp); 2303 2304 return (error); 2305 } 2306 2307 int 2308 ifioctl_get(u_long cmd, caddr_t data) 2309 { 2310 struct ifnet *ifp; 2311 struct ifreq *ifr = (struct ifreq *)data; 2312 char ifdescrbuf[IFDESCRSIZE]; 2313 char ifrtlabelbuf[RTLABEL_LEN]; 2314 int error = 0; 2315 size_t bytesdone; 2316 const char *label; 2317 2318 switch(cmd) { 2319 case SIOCGIFCONF: 2320 NET_RLOCK_IN_IOCTL(); 2321 error = ifconf(data); 2322 NET_RUNLOCK_IN_IOCTL(); 2323 return (error); 2324 case SIOCIFGCLONERS: 2325 error = if_clone_list((struct if_clonereq *)data); 2326 return (error); 2327 case SIOCGIFGMEMB: 2328 NET_RLOCK_IN_IOCTL(); 2329 error = if_getgroupmembers(data); 2330 NET_RUNLOCK_IN_IOCTL(); 2331 return (error); 2332 case SIOCGIFGATTR: 2333 NET_RLOCK_IN_IOCTL(); 2334 error = if_getgroupattribs(data); 2335 NET_RUNLOCK_IN_IOCTL(); 2336 return (error); 2337 case SIOCGIFGLIST: 2338 NET_RLOCK_IN_IOCTL(); 2339 error = if_getgrouplist(data); 2340 NET_RUNLOCK_IN_IOCTL(); 2341 return (error); 2342 } 2343 2344 ifp = if_unit(ifr->ifr_name); 2345 if (ifp == NULL) 2346 return (ENXIO); 2347 2348 NET_RLOCK_IN_IOCTL(); 2349 2350 switch(cmd) { 2351 case SIOCGIFFLAGS: 2352 ifr->ifr_flags = ifp->if_flags; 2353 if (ifq_is_oactive(&ifp->if_snd)) 2354 ifr->ifr_flags |= IFF_OACTIVE; 2355 break; 2356 2357 case SIOCGIFXFLAGS: 2358 ifr->ifr_flags = ifp->if_xflags & ~(IFXF_MPSAFE|IFXF_CLONED); 2359 break; 2360 2361 case SIOCGIFMETRIC: 2362 ifr->ifr_metric = ifp->if_metric; 2363 break; 2364 2365 case SIOCGIFMTU: 2366 ifr->ifr_mtu = ifp->if_mtu; 2367 break; 2368 2369 case SIOCGIFHARDMTU: 2370 ifr->ifr_hardmtu = ifp->if_hardmtu; 2371 break; 2372 2373 case SIOCGIFDATA: { 2374 struct if_data ifdata; 2375 if_getdata(ifp, &ifdata); 2376 error = copyout(&ifdata, ifr->ifr_data, sizeof(ifdata)); 2377 break; 2378 } 2379 2380 case SIOCGIFDESCR: 2381 strlcpy(ifdescrbuf, ifp->if_description, IFDESCRSIZE); 2382 error = copyoutstr(ifdescrbuf, ifr->ifr_data, IFDESCRSIZE, 2383 &bytesdone); 2384 break; 2385 2386 case SIOCGIFRTLABEL: 2387 if (ifp->if_rtlabelid && 2388 (label = rtlabel_id2name(ifp->if_rtlabelid)) != NULL) { 2389 strlcpy(ifrtlabelbuf, label, RTLABEL_LEN); 2390 error = copyoutstr(ifrtlabelbuf, ifr->ifr_data, 2391 RTLABEL_LEN, &bytesdone); 2392 } else 2393 error = ENOENT; 2394 break; 2395 2396 case SIOCGIFPRIORITY: 2397 ifr->ifr_metric = ifp->if_priority; 2398 break; 2399 2400 case SIOCGIFRDOMAIN: 2401 ifr->ifr_rdomainid = ifp->if_rdomain; 2402 break; 2403 2404 case SIOCGIFGROUP: 2405 error = if_getgroup(data, ifp); 2406 break; 2407 2408 case SIOCGIFLLPRIO: 2409 ifr->ifr_llprio = ifp->if_llprio; 2410 break; 2411 2412 default: 2413 panic("invalid ioctl %lu", cmd); 2414 } 2415 2416 NET_RUNLOCK_IN_IOCTL(); 2417 2418 if_put(ifp); 2419 2420 return (error); 2421 } 2422 2423 static int 2424 if_sffpage_check(const caddr_t data) 2425 { 2426 const struct if_sffpage *sff = (const struct if_sffpage *)data; 2427 2428 switch (sff->sff_addr) { 2429 case IFSFF_ADDR_EEPROM: 2430 case IFSFF_ADDR_DDM: 2431 break; 2432 default: 2433 return (EINVAL); 2434 } 2435 2436 return (0); 2437 } 2438 2439 int 2440 if_txhprio_l2_check(int hdrprio) 2441 { 2442 switch (hdrprio) { 2443 case IF_HDRPRIO_PACKET: 2444 return (0); 2445 default: 2446 if (hdrprio >= IF_HDRPRIO_MIN && hdrprio <= IF_HDRPRIO_MAX) 2447 return (0); 2448 break; 2449 } 2450 2451 return (EINVAL); 2452 } 2453 2454 int 2455 if_txhprio_l3_check(int hdrprio) 2456 { 2457 switch (hdrprio) { 2458 case IF_HDRPRIO_PACKET: 2459 case IF_HDRPRIO_PAYLOAD: 2460 return (0); 2461 default: 2462 if (hdrprio >= IF_HDRPRIO_MIN && hdrprio <= IF_HDRPRIO_MAX) 2463 return (0); 2464 break; 2465 } 2466 2467 return (EINVAL); 2468 } 2469 2470 int 2471 if_rxhprio_l2_check(int hdrprio) 2472 { 2473 switch (hdrprio) { 2474 case IF_HDRPRIO_PACKET: 2475 case IF_HDRPRIO_OUTER: 2476 return (0); 2477 default: 2478 if (hdrprio >= IF_HDRPRIO_MIN && hdrprio <= IF_HDRPRIO_MAX) 2479 return (0); 2480 break; 2481 } 2482 2483 return (EINVAL); 2484 } 2485 2486 int 2487 if_rxhprio_l3_check(int hdrprio) 2488 { 2489 switch (hdrprio) { 2490 case IF_HDRPRIO_PACKET: 2491 case IF_HDRPRIO_PAYLOAD: 2492 case IF_HDRPRIO_OUTER: 2493 return (0); 2494 default: 2495 if (hdrprio >= IF_HDRPRIO_MIN && hdrprio <= IF_HDRPRIO_MAX) 2496 return (0); 2497 break; 2498 } 2499 2500 return (EINVAL); 2501 } 2502 2503 /* 2504 * Return interface configuration 2505 * of system. List may be used 2506 * in later ioctl's (above) to get 2507 * other information. 2508 */ 2509 int 2510 ifconf(caddr_t data) 2511 { 2512 struct ifconf *ifc = (struct ifconf *)data; 2513 struct ifnet *ifp; 2514 struct ifaddr *ifa; 2515 struct ifreq ifr, *ifrp; 2516 int space = ifc->ifc_len, error = 0; 2517 2518 /* If ifc->ifc_len is 0, fill it in with the needed size and return. */ 2519 if (space == 0) { 2520 TAILQ_FOREACH(ifp, &ifnet, if_list) { 2521 struct sockaddr *sa; 2522 2523 if (TAILQ_EMPTY(&ifp->if_addrlist)) 2524 space += sizeof (ifr); 2525 else 2526 TAILQ_FOREACH(ifa, 2527 &ifp->if_addrlist, ifa_list) { 2528 sa = ifa->ifa_addr; 2529 if (sa->sa_len > sizeof(*sa)) 2530 space += sa->sa_len - 2531 sizeof(*sa); 2532 space += sizeof(ifr); 2533 } 2534 } 2535 ifc->ifc_len = space; 2536 return (0); 2537 } 2538 2539 ifrp = ifc->ifc_req; 2540 TAILQ_FOREACH(ifp, &ifnet, if_list) { 2541 if (space < sizeof(ifr)) 2542 break; 2543 bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ); 2544 if (TAILQ_EMPTY(&ifp->if_addrlist)) { 2545 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 2546 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 2547 sizeof(ifr)); 2548 if (error) 2549 break; 2550 space -= sizeof (ifr), ifrp++; 2551 } else 2552 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 2553 struct sockaddr *sa = ifa->ifa_addr; 2554 2555 if (space < sizeof(ifr)) 2556 break; 2557 if (sa->sa_len <= sizeof(*sa)) { 2558 ifr.ifr_addr = *sa; 2559 error = copyout((caddr_t)&ifr, 2560 (caddr_t)ifrp, sizeof (ifr)); 2561 ifrp++; 2562 } else { 2563 space -= sa->sa_len - sizeof(*sa); 2564 if (space < sizeof (ifr)) 2565 break; 2566 error = copyout((caddr_t)&ifr, 2567 (caddr_t)ifrp, 2568 sizeof(ifr.ifr_name)); 2569 if (error == 0) 2570 error = copyout((caddr_t)sa, 2571 (caddr_t)&ifrp->ifr_addr, 2572 sa->sa_len); 2573 ifrp = (struct ifreq *)(sa->sa_len + 2574 (caddr_t)&ifrp->ifr_addr); 2575 } 2576 if (error) 2577 break; 2578 space -= sizeof (ifr); 2579 } 2580 } 2581 ifc->ifc_len -= space; 2582 return (error); 2583 } 2584 2585 void 2586 if_counters_alloc(struct ifnet *ifp) 2587 { 2588 KASSERT(ifp->if_counters == NULL); 2589 2590 ifp->if_counters = counters_alloc(ifc_ncounters); 2591 } 2592 2593 void 2594 if_counters_free(struct ifnet *ifp) 2595 { 2596 KASSERT(ifp->if_counters != NULL); 2597 2598 counters_free(ifp->if_counters, ifc_ncounters); 2599 ifp->if_counters = NULL; 2600 } 2601 2602 void 2603 if_getdata(struct ifnet *ifp, struct if_data *data) 2604 { 2605 unsigned int i; 2606 2607 *data = ifp->if_data; 2608 2609 if (ifp->if_counters != NULL) { 2610 uint64_t counters[ifc_ncounters]; 2611 2612 counters_read(ifp->if_counters, counters, nitems(counters)); 2613 2614 data->ifi_ipackets += counters[ifc_ipackets]; 2615 data->ifi_ierrors += counters[ifc_ierrors]; 2616 data->ifi_opackets += counters[ifc_opackets]; 2617 data->ifi_oerrors += counters[ifc_oerrors]; 2618 data->ifi_collisions += counters[ifc_collisions]; 2619 data->ifi_ibytes += counters[ifc_ibytes]; 2620 data->ifi_obytes += counters[ifc_obytes]; 2621 data->ifi_imcasts += counters[ifc_imcasts]; 2622 data->ifi_omcasts += counters[ifc_omcasts]; 2623 data->ifi_iqdrops += counters[ifc_iqdrops]; 2624 data->ifi_oqdrops += counters[ifc_oqdrops]; 2625 data->ifi_noproto += counters[ifc_noproto]; 2626 } 2627 2628 for (i = 0; i < ifp->if_nifqs; i++) { 2629 struct ifqueue *ifq = ifp->if_ifqs[i]; 2630 2631 ifq_add_data(ifq, data); 2632 } 2633 2634 for (i = 0; i < ifp->if_niqs; i++) { 2635 struct ifiqueue *ifiq = ifp->if_iqs[i]; 2636 2637 ifiq_add_data(ifiq, data); 2638 } 2639 } 2640 2641 /* 2642 * Dummy functions replaced in ifnet during detach (if protocols decide to 2643 * fiddle with the if during detach. 2644 */ 2645 void 2646 if_detached_qstart(struct ifqueue *ifq) 2647 { 2648 ifq_purge(ifq); 2649 } 2650 2651 int 2652 if_detached_ioctl(struct ifnet *ifp, u_long a, caddr_t b) 2653 { 2654 return ENODEV; 2655 } 2656 2657 /* 2658 * Create interface group without members 2659 */ 2660 struct ifg_group * 2661 if_creategroup(const char *groupname) 2662 { 2663 struct ifg_group *ifg; 2664 2665 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) 2666 return (NULL); 2667 2668 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 2669 ifg->ifg_refcnt = 1; 2670 ifg->ifg_carp_demoted = 0; 2671 TAILQ_INIT(&ifg->ifg_members); 2672 #if NPF > 0 2673 pfi_attach_ifgroup(ifg); 2674 #endif 2675 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next); 2676 2677 return (ifg); 2678 } 2679 2680 /* 2681 * Add a group to an interface 2682 */ 2683 int 2684 if_addgroup(struct ifnet *ifp, const char *groupname) 2685 { 2686 struct ifg_list *ifgl; 2687 struct ifg_group *ifg = NULL; 2688 struct ifg_member *ifgm; 2689 size_t namelen; 2690 2691 namelen = strlen(groupname); 2692 if (namelen == 0 || namelen >= IFNAMSIZ || 2693 (groupname[namelen - 1] >= '0' && groupname[namelen - 1] <= '9')) 2694 return (EINVAL); 2695 2696 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 2697 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 2698 return (EEXIST); 2699 2700 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) 2701 return (ENOMEM); 2702 2703 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) { 2704 free(ifgl, M_TEMP, sizeof(*ifgl)); 2705 return (ENOMEM); 2706 } 2707 2708 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) 2709 if (!strcmp(ifg->ifg_group, groupname)) 2710 break; 2711 2712 if (ifg == NULL) { 2713 ifg = if_creategroup(groupname); 2714 if (ifg == NULL) { 2715 free(ifgl, M_TEMP, sizeof(*ifgl)); 2716 free(ifgm, M_TEMP, sizeof(*ifgm)); 2717 return (ENOMEM); 2718 } 2719 } else 2720 ifg->ifg_refcnt++; 2721 KASSERT(ifg->ifg_refcnt != 0); 2722 2723 ifgl->ifgl_group = ifg; 2724 ifgm->ifgm_ifp = ifp; 2725 2726 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 2727 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 2728 2729 #if NPF > 0 2730 pfi_group_addmember(groupname, ifp); 2731 #endif 2732 2733 return (0); 2734 } 2735 2736 /* 2737 * Remove a group from an interface 2738 */ 2739 int 2740 if_delgroup(struct ifnet *ifp, const char *groupname) 2741 { 2742 struct ifg_list *ifgl; 2743 struct ifg_member *ifgm; 2744 2745 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 2746 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 2747 break; 2748 if (ifgl == NULL) 2749 return (ENOENT); 2750 2751 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 2752 2753 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 2754 if (ifgm->ifgm_ifp == ifp) 2755 break; 2756 2757 if (ifgm != NULL) { 2758 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); 2759 free(ifgm, M_TEMP, sizeof(*ifgm)); 2760 } 2761 2762 #if NPF > 0 2763 pfi_group_change(groupname); 2764 #endif 2765 2766 KASSERT(ifgl->ifgl_group->ifg_refcnt != 0); 2767 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 2768 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next); 2769 #if NPF > 0 2770 pfi_detach_ifgroup(ifgl->ifgl_group); 2771 #endif 2772 free(ifgl->ifgl_group, M_TEMP, sizeof(*ifgl->ifgl_group)); 2773 } 2774 2775 free(ifgl, M_TEMP, sizeof(*ifgl)); 2776 2777 return (0); 2778 } 2779 2780 /* 2781 * Stores all groups from an interface in memory pointed 2782 * to by data 2783 */ 2784 int 2785 if_getgroup(caddr_t data, struct ifnet *ifp) 2786 { 2787 int len, error; 2788 struct ifg_list *ifgl; 2789 struct ifg_req ifgrq, *ifgp; 2790 struct ifgroupreq *ifgr = (struct ifgroupreq *)data; 2791 2792 if (ifgr->ifgr_len == 0) { 2793 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 2794 ifgr->ifgr_len += sizeof(struct ifg_req); 2795 return (0); 2796 } 2797 2798 len = ifgr->ifgr_len; 2799 ifgp = ifgr->ifgr_groups; 2800 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 2801 if (len < sizeof(ifgrq)) 2802 return (EINVAL); 2803 bzero(&ifgrq, sizeof ifgrq); 2804 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 2805 sizeof(ifgrq.ifgrq_group)); 2806 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp, 2807 sizeof(struct ifg_req)))) 2808 return (error); 2809 len -= sizeof(ifgrq); 2810 ifgp++; 2811 } 2812 2813 return (0); 2814 } 2815 2816 /* 2817 * Stores all members of a group in memory pointed to by data 2818 */ 2819 int 2820 if_getgroupmembers(caddr_t data) 2821 { 2822 struct ifgroupreq *ifgr = (struct ifgroupreq *)data; 2823 struct ifg_group *ifg; 2824 struct ifg_member *ifgm; 2825 struct ifg_req ifgrq, *ifgp; 2826 int len, error; 2827 2828 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) 2829 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 2830 break; 2831 if (ifg == NULL) 2832 return (ENOENT); 2833 2834 if (ifgr->ifgr_len == 0) { 2835 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 2836 ifgr->ifgr_len += sizeof(ifgrq); 2837 return (0); 2838 } 2839 2840 len = ifgr->ifgr_len; 2841 ifgp = ifgr->ifgr_groups; 2842 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 2843 if (len < sizeof(ifgrq)) 2844 return (EINVAL); 2845 bzero(&ifgrq, sizeof ifgrq); 2846 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 2847 sizeof(ifgrq.ifgrq_member)); 2848 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp, 2849 sizeof(struct ifg_req)))) 2850 return (error); 2851 len -= sizeof(ifgrq); 2852 ifgp++; 2853 } 2854 2855 return (0); 2856 } 2857 2858 int 2859 if_getgroupattribs(caddr_t data) 2860 { 2861 struct ifgroupreq *ifgr = (struct ifgroupreq *)data; 2862 struct ifg_group *ifg; 2863 2864 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) 2865 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 2866 break; 2867 if (ifg == NULL) 2868 return (ENOENT); 2869 2870 ifgr->ifgr_attrib.ifg_carp_demoted = ifg->ifg_carp_demoted; 2871 2872 return (0); 2873 } 2874 2875 int 2876 if_setgroupattribs(caddr_t data) 2877 { 2878 struct ifgroupreq *ifgr = (struct ifgroupreq *)data; 2879 struct ifg_group *ifg; 2880 struct ifg_member *ifgm; 2881 int demote; 2882 2883 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) 2884 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 2885 break; 2886 if (ifg == NULL) 2887 return (ENOENT); 2888 2889 demote = ifgr->ifgr_attrib.ifg_carp_demoted; 2890 if (demote + ifg->ifg_carp_demoted > 0xff || 2891 demote + ifg->ifg_carp_demoted < 0) 2892 return (EINVAL); 2893 2894 ifg->ifg_carp_demoted += demote; 2895 2896 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 2897 ifgm->ifgm_ifp->if_ioctl(ifgm->ifgm_ifp, SIOCSIFGATTR, data); 2898 2899 return (0); 2900 } 2901 2902 /* 2903 * Stores all groups in memory pointed to by data 2904 */ 2905 int 2906 if_getgrouplist(caddr_t data) 2907 { 2908 struct ifgroupreq *ifgr = (struct ifgroupreq *)data; 2909 struct ifg_group *ifg; 2910 struct ifg_req ifgrq, *ifgp; 2911 int len, error; 2912 2913 if (ifgr->ifgr_len == 0) { 2914 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) 2915 ifgr->ifgr_len += sizeof(ifgrq); 2916 return (0); 2917 } 2918 2919 len = ifgr->ifgr_len; 2920 ifgp = ifgr->ifgr_groups; 2921 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) { 2922 if (len < sizeof(ifgrq)) 2923 return (EINVAL); 2924 bzero(&ifgrq, sizeof ifgrq); 2925 strlcpy(ifgrq.ifgrq_group, ifg->ifg_group, 2926 sizeof(ifgrq.ifgrq_group)); 2927 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp, 2928 sizeof(struct ifg_req)))) 2929 return (error); 2930 len -= sizeof(ifgrq); 2931 ifgp++; 2932 } 2933 2934 return (0); 2935 } 2936 2937 void 2938 if_group_routechange(struct sockaddr *dst, struct sockaddr *mask) 2939 { 2940 switch (dst->sa_family) { 2941 case AF_INET: 2942 if (satosin(dst)->sin_addr.s_addr == INADDR_ANY && 2943 mask && (mask->sa_len == 0 || 2944 satosin(mask)->sin_addr.s_addr == INADDR_ANY)) 2945 if_group_egress_build(); 2946 break; 2947 #ifdef INET6 2948 case AF_INET6: 2949 if (IN6_ARE_ADDR_EQUAL(&(satosin6(dst))->sin6_addr, 2950 &in6addr_any) && mask && (mask->sa_len == 0 || 2951 IN6_ARE_ADDR_EQUAL(&(satosin6(mask))->sin6_addr, 2952 &in6addr_any))) 2953 if_group_egress_build(); 2954 break; 2955 #endif 2956 } 2957 } 2958 2959 int 2960 if_group_egress_build(void) 2961 { 2962 struct ifnet *ifp; 2963 struct ifg_group *ifg; 2964 struct ifg_member *ifgm, *next; 2965 struct sockaddr_in sa_in; 2966 #ifdef INET6 2967 struct sockaddr_in6 sa_in6; 2968 #endif 2969 struct rtentry *rt; 2970 2971 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) 2972 if (!strcmp(ifg->ifg_group, IFG_EGRESS)) 2973 break; 2974 2975 if (ifg != NULL) 2976 TAILQ_FOREACH_SAFE(ifgm, &ifg->ifg_members, ifgm_next, next) 2977 if_delgroup(ifgm->ifgm_ifp, IFG_EGRESS); 2978 2979 bzero(&sa_in, sizeof(sa_in)); 2980 sa_in.sin_len = sizeof(sa_in); 2981 sa_in.sin_family = AF_INET; 2982 rt = rtable_lookup(0, sintosa(&sa_in), sintosa(&sa_in), NULL, RTP_ANY); 2983 while (rt != NULL) { 2984 ifp = if_get(rt->rt_ifidx); 2985 if (ifp != NULL) { 2986 if_addgroup(ifp, IFG_EGRESS); 2987 if_put(ifp); 2988 } 2989 rt = rtable_iterate(rt); 2990 } 2991 2992 #ifdef INET6 2993 bcopy(&sa6_any, &sa_in6, sizeof(sa_in6)); 2994 rt = rtable_lookup(0, sin6tosa(&sa_in6), sin6tosa(&sa_in6), NULL, 2995 RTP_ANY); 2996 while (rt != NULL) { 2997 ifp = if_get(rt->rt_ifidx); 2998 if (ifp != NULL) { 2999 if_addgroup(ifp, IFG_EGRESS); 3000 if_put(ifp); 3001 } 3002 rt = rtable_iterate(rt); 3003 } 3004 #endif /* INET6 */ 3005 3006 return (0); 3007 } 3008 3009 /* 3010 * Set/clear promiscuous mode on interface ifp based on the truth value 3011 * of pswitch. The calls are reference counted so that only the first 3012 * "on" request actually has an effect, as does the final "off" request. 3013 * Results are undefined if the "off" and "on" requests are not matched. 3014 */ 3015 int 3016 ifpromisc(struct ifnet *ifp, int pswitch) 3017 { 3018 struct ifreq ifr; 3019 unsigned short oif_flags; 3020 int oif_pcount, error; 3021 3022 NET_ASSERT_LOCKED(); /* modifying if_flags and if_pcount */ 3023 3024 oif_flags = ifp->if_flags; 3025 oif_pcount = ifp->if_pcount; 3026 if (pswitch) { 3027 if (ifp->if_pcount++ != 0) 3028 return (0); 3029 ifp->if_flags |= IFF_PROMISC; 3030 } else { 3031 if (--ifp->if_pcount > 0) 3032 return (0); 3033 ifp->if_flags &= ~IFF_PROMISC; 3034 } 3035 3036 if ((ifp->if_flags & IFF_UP) == 0) 3037 return (0); 3038 3039 memset(&ifr, 0, sizeof(ifr)); 3040 ifr.ifr_flags = ifp->if_flags; 3041 error = ((*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr)); 3042 if (error) { 3043 ifp->if_flags = oif_flags; 3044 ifp->if_pcount = oif_pcount; 3045 } 3046 3047 return (error); 3048 } 3049 3050 void 3051 ifa_add(struct ifnet *ifp, struct ifaddr *ifa) 3052 { 3053 TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list); 3054 } 3055 3056 void 3057 ifa_del(struct ifnet *ifp, struct ifaddr *ifa) 3058 { 3059 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list); 3060 } 3061 3062 void 3063 ifa_update_broadaddr(struct ifnet *ifp, struct ifaddr *ifa, struct sockaddr *sa) 3064 { 3065 if (ifa->ifa_broadaddr->sa_len != sa->sa_len) 3066 panic("ifa_update_broadaddr does not support dynamic length"); 3067 bcopy(sa, ifa->ifa_broadaddr, sa->sa_len); 3068 } 3069 3070 #ifdef DDB 3071 /* debug function, can be called from ddb> */ 3072 void 3073 ifa_print_all(void) 3074 { 3075 struct ifnet *ifp; 3076 struct ifaddr *ifa; 3077 3078 TAILQ_FOREACH(ifp, &ifnet, if_list) { 3079 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 3080 char addr[INET6_ADDRSTRLEN]; 3081 3082 switch (ifa->ifa_addr->sa_family) { 3083 case AF_INET: 3084 printf("%s", inet_ntop(AF_INET, 3085 &satosin(ifa->ifa_addr)->sin_addr, 3086 addr, sizeof(addr))); 3087 break; 3088 #ifdef INET6 3089 case AF_INET6: 3090 printf("%s", inet_ntop(AF_INET6, 3091 &(satosin6(ifa->ifa_addr))->sin6_addr, 3092 addr, sizeof(addr))); 3093 break; 3094 #endif 3095 } 3096 printf(" on %s\n", ifp->if_xname); 3097 } 3098 } 3099 } 3100 #endif /* DDB */ 3101 3102 void 3103 ifnewlladdr(struct ifnet *ifp) 3104 { 3105 #ifdef INET6 3106 struct ifaddr *ifa; 3107 #endif 3108 struct ifreq ifrq; 3109 short up; 3110 int s; 3111 3112 s = splnet(); 3113 up = ifp->if_flags & IFF_UP; 3114 3115 if (up) { 3116 /* go down for a moment... */ 3117 ifp->if_flags &= ~IFF_UP; 3118 ifrq.ifr_flags = ifp->if_flags; 3119 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq); 3120 } 3121 3122 ifp->if_flags |= IFF_UP; 3123 ifrq.ifr_flags = ifp->if_flags; 3124 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq); 3125 3126 #ifdef INET6 3127 /* 3128 * Update the link-local address. Don't do it if we're 3129 * a router to avoid confusing hosts on the network. 3130 */ 3131 if (!ip6_forwarding) { 3132 ifa = &in6ifa_ifpforlinklocal(ifp, 0)->ia_ifa; 3133 if (ifa) { 3134 in6_purgeaddr(ifa); 3135 if_hooks_run(&ifp->if_addrhooks); 3136 in6_ifattach(ifp); 3137 } 3138 } 3139 #endif 3140 if (!up) { 3141 /* go back down */ 3142 ifp->if_flags &= ~IFF_UP; 3143 ifrq.ifr_flags = ifp->if_flags; 3144 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq); 3145 } 3146 splx(s); 3147 } 3148 3149 void 3150 if_addrhook_add(struct ifnet *ifp, struct task *t) 3151 { 3152 mtx_enter(&if_hooks_mtx); 3153 TAILQ_INSERT_TAIL(&ifp->if_addrhooks, t, t_entry); 3154 mtx_leave(&if_hooks_mtx); 3155 } 3156 3157 void 3158 if_addrhook_del(struct ifnet *ifp, struct task *t) 3159 { 3160 mtx_enter(&if_hooks_mtx); 3161 TAILQ_REMOVE(&ifp->if_addrhooks, t, t_entry); 3162 mtx_leave(&if_hooks_mtx); 3163 } 3164 3165 void 3166 if_addrhooks_run(struct ifnet *ifp) 3167 { 3168 if_hooks_run(&ifp->if_addrhooks); 3169 } 3170 3171 void 3172 if_rxr_init(struct if_rxring *rxr, u_int lwm, u_int hwm) 3173 { 3174 extern int ticks; 3175 3176 memset(rxr, 0, sizeof(*rxr)); 3177 3178 rxr->rxr_adjusted = ticks; 3179 rxr->rxr_cwm = rxr->rxr_lwm = lwm; 3180 rxr->rxr_hwm = hwm; 3181 } 3182 3183 static inline void 3184 if_rxr_adjust_cwm(struct if_rxring *rxr) 3185 { 3186 extern int ticks; 3187 3188 if (rxr->rxr_alive >= rxr->rxr_lwm) 3189 return; 3190 else if (rxr->rxr_cwm < rxr->rxr_hwm) 3191 rxr->rxr_cwm++; 3192 3193 rxr->rxr_adjusted = ticks; 3194 } 3195 3196 void 3197 if_rxr_livelocked(struct if_rxring *rxr) 3198 { 3199 extern int ticks; 3200 3201 if (ticks - rxr->rxr_adjusted >= 1) { 3202 if (rxr->rxr_cwm > rxr->rxr_lwm) 3203 rxr->rxr_cwm--; 3204 3205 rxr->rxr_adjusted = ticks; 3206 } 3207 } 3208 3209 u_int 3210 if_rxr_get(struct if_rxring *rxr, u_int max) 3211 { 3212 extern int ticks; 3213 u_int diff; 3214 3215 if (ticks - rxr->rxr_adjusted >= 1) { 3216 /* we're free to try for an adjustment */ 3217 if_rxr_adjust_cwm(rxr); 3218 } 3219 3220 if (rxr->rxr_alive >= rxr->rxr_cwm) 3221 return (0); 3222 3223 diff = min(rxr->rxr_cwm - rxr->rxr_alive, max); 3224 rxr->rxr_alive += diff; 3225 3226 return (diff); 3227 } 3228 3229 int 3230 if_rxr_info_ioctl(struct if_rxrinfo *uifri, u_int t, struct if_rxring_info *e) 3231 { 3232 struct if_rxrinfo kifri; 3233 int error; 3234 u_int n; 3235 3236 error = copyin(uifri, &kifri, sizeof(kifri)); 3237 if (error) 3238 return (error); 3239 3240 n = min(t, kifri.ifri_total); 3241 kifri.ifri_total = t; 3242 3243 if (n > 0) { 3244 error = copyout(e, kifri.ifri_entries, sizeof(*e) * n); 3245 if (error) 3246 return (error); 3247 } 3248 3249 return (copyout(&kifri, uifri, sizeof(kifri))); 3250 } 3251 3252 int 3253 if_rxr_ioctl(struct if_rxrinfo *ifri, const char *name, u_int size, 3254 struct if_rxring *rxr) 3255 { 3256 struct if_rxring_info ifr; 3257 3258 memset(&ifr, 0, sizeof(ifr)); 3259 3260 if (name != NULL) 3261 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 3262 3263 ifr.ifr_size = size; 3264 ifr.ifr_info = *rxr; 3265 3266 return (if_rxr_info_ioctl(ifri, 1, &ifr)); 3267 } 3268 3269 /* 3270 * Network stack input queues. 3271 */ 3272 3273 void 3274 niq_init(struct niqueue *niq, u_int maxlen, u_int isr) 3275 { 3276 mq_init(&niq->ni_q, maxlen, IPL_NET); 3277 niq->ni_isr = isr; 3278 } 3279 3280 int 3281 niq_enqueue(struct niqueue *niq, struct mbuf *m) 3282 { 3283 int rv; 3284 3285 rv = mq_enqueue(&niq->ni_q, m); 3286 if (rv == 0) 3287 schednetisr(niq->ni_isr); 3288 else 3289 if_congestion(); 3290 3291 return (rv); 3292 } 3293 3294 int 3295 niq_enlist(struct niqueue *niq, struct mbuf_list *ml) 3296 { 3297 int rv; 3298 3299 rv = mq_enlist(&niq->ni_q, ml); 3300 if (rv == 0) 3301 schednetisr(niq->ni_isr); 3302 else 3303 if_congestion(); 3304 3305 return (rv); 3306 } 3307 3308 __dead void 3309 unhandled_af(int af) 3310 { 3311 panic("unhandled af %d", af); 3312 } 3313 3314 /* 3315 * XXXSMP This tunable is here to work around the fact that IPsec 3316 * globals aren't ready to be accessed by multiple threads in 3317 * parallel. 3318 */ 3319 int nettaskqs = NET_TASKQ; 3320 3321 struct taskq * 3322 net_tq(unsigned int ifindex) 3323 { 3324 struct taskq *t = NULL; 3325 3326 t = nettqmp[ifindex % nettaskqs]; 3327 3328 return (t); 3329 } 3330