1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1980, 1986, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)if.c 8.5 (Berkeley) 1/9/95 32 * $FreeBSD$ 33 */ 34 35 #include "opt_inet6.h" 36 #include "opt_inet.h" 37 38 #include <sys/param.h> 39 #include <sys/types.h> 40 #include <sys/conf.h> 41 #include <sys/malloc.h> 42 #include <sys/sbuf.h> 43 #include <sys/bus.h> 44 #include <sys/epoch.h> 45 #include <sys/mbuf.h> 46 #include <sys/systm.h> 47 #include <sys/priv.h> 48 #include <sys/proc.h> 49 #include <sys/socket.h> 50 #include <sys/socketvar.h> 51 #include <sys/protosw.h> 52 #include <sys/kernel.h> 53 #include <sys/lock.h> 54 #include <sys/refcount.h> 55 #include <sys/module.h> 56 #include <sys/rwlock.h> 57 #include <sys/sockio.h> 58 #include <sys/syslog.h> 59 #include <sys/sysctl.h> 60 #include <sys/sysent.h> 61 #include <sys/taskqueue.h> 62 #include <sys/domain.h> 63 #include <sys/jail.h> 64 #include <sys/priv.h> 65 66 #include <machine/stdarg.h> 67 #include <vm/uma.h> 68 69 #include <net/bpf.h> 70 #include <net/ethernet.h> 71 #include <net/if.h> 72 #include <net/if_arp.h> 73 #include <net/if_clone.h> 74 #include <net/if_dl.h> 75 #include <net/if_types.h> 76 #include <net/if_var.h> 77 #include <net/if_media.h> 78 #include <net/if_vlan_var.h> 79 #include <net/radix.h> 80 #include <net/route.h> 81 #include <net/vnet.h> 82 83 #if defined(INET) || defined(INET6) 84 #include <net/ethernet.h> 85 #include <netinet/in.h> 86 #include <netinet/in_var.h> 87 #include <netinet/ip.h> 88 #include <netinet/ip_carp.h> 89 #ifdef INET 90 #include <netinet/if_ether.h> 91 #include <netinet/netdump/netdump.h> 92 #endif /* INET */ 93 #ifdef INET6 94 #include <netinet6/in6_var.h> 95 #include <netinet6/in6_ifattach.h> 96 #endif /* INET6 */ 97 #endif /* INET || INET6 */ 98 99 #include <security/mac/mac_framework.h> 100 101 /* 102 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name 103 * and ifr_ifru when it is used in SIOCGIFCONF. 104 */ 105 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) == 106 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru"); 107 108 __read_mostly epoch_t net_epoch_preempt; 109 __read_mostly epoch_t net_epoch; 110 #ifdef COMPAT_FREEBSD32 111 #include <sys/mount.h> 112 #include <compat/freebsd32/freebsd32.h> 113 114 struct ifreq_buffer32 { 115 uint32_t length; /* (size_t) */ 116 uint32_t buffer; /* (void *) */ 117 }; 118 119 /* 120 * Interface request structure used for socket 121 * ioctl's. All interface ioctl's must have parameter 122 * definitions which begin with ifr_name. The 123 * remainder may be interface specific. 124 */ 125 struct ifreq32 { 126 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 127 union { 128 struct sockaddr ifru_addr; 129 struct sockaddr ifru_dstaddr; 130 struct sockaddr ifru_broadaddr; 131 struct ifreq_buffer32 ifru_buffer; 132 short ifru_flags[2]; 133 short ifru_index; 134 int ifru_jid; 135 int ifru_metric; 136 int ifru_mtu; 137 int ifru_phys; 138 int ifru_media; 139 uint32_t ifru_data; 140 int ifru_cap[2]; 141 u_int ifru_fib; 142 u_char ifru_vlan_pcp; 143 } ifr_ifru; 144 }; 145 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32)); 146 CTASSERT(__offsetof(struct ifreq, ifr_ifru) == 147 __offsetof(struct ifreq32, ifr_ifru)); 148 149 struct ifgroupreq32 { 150 char ifgr_name[IFNAMSIZ]; 151 u_int ifgr_len; 152 union { 153 char ifgru_group[IFNAMSIZ]; 154 uint32_t ifgru_groups; 155 } ifgr_ifgru; 156 }; 157 158 struct ifmediareq32 { 159 char ifm_name[IFNAMSIZ]; 160 int ifm_current; 161 int ifm_mask; 162 int ifm_status; 163 int ifm_active; 164 int ifm_count; 165 uint32_t ifm_ulist; /* (int *) */ 166 }; 167 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32) 168 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32) 169 170 #define _CASE_IOC_IFGROUPREQ_32(cmd) \ 171 case _IOC_NEWTYPE((cmd), struct ifgroupreq32): 172 #else /* !COMPAT_FREEBSD32 */ 173 #define _CASE_IOC_IFGROUPREQ_32(cmd) 174 #endif /* !COMPAT_FREEBSD32 */ 175 176 #define CASE_IOC_IFGROUPREQ(cmd) \ 177 _CASE_IOC_IFGROUPREQ_32(cmd) \ 178 case (cmd) 179 180 union ifreq_union { 181 struct ifreq ifr; 182 #ifdef COMPAT_FREEBSD32 183 struct ifreq32 ifr32; 184 #endif 185 }; 186 187 union ifgroupreq_union { 188 struct ifgroupreq ifgr; 189 #ifdef COMPAT_FREEBSD32 190 struct ifgroupreq32 ifgr32; 191 #endif 192 }; 193 194 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 195 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 196 197 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN, 198 &ifqmaxlen, 0, "max send queue size"); 199 200 /* Log link state change events */ 201 static int log_link_state_change = 1; 202 203 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW, 204 &log_link_state_change, 0, 205 "log interface link state change events"); 206 207 /* Log promiscuous mode change events */ 208 static int log_promisc_mode_change = 1; 209 210 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN, 211 &log_promisc_mode_change, 1, 212 "log promiscuous mode change events"); 213 214 /* Interface description */ 215 static unsigned int ifdescr_maxlen = 1024; 216 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW, 217 &ifdescr_maxlen, 0, 218 "administrative maximum length for interface description"); 219 220 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions"); 221 222 /* global sx for non-critical path ifdescr */ 223 static struct sx ifdescr_sx; 224 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr"); 225 226 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 227 void (*lagg_linkstate_p)(struct ifnet *ifp, int state); 228 /* These are external hooks for CARP. */ 229 void (*carp_linkstate_p)(struct ifnet *ifp); 230 void (*carp_demote_adj_p)(int, char *); 231 int (*carp_master_p)(struct ifaddr *); 232 #if defined(INET) || defined(INET6) 233 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost); 234 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m, 235 const struct sockaddr *sa); 236 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *); 237 int (*carp_attach_p)(struct ifaddr *, int); 238 void (*carp_detach_p)(struct ifaddr *, bool); 239 #endif 240 #ifdef INET 241 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **); 242 #endif 243 #ifdef INET6 244 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6); 245 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m, 246 const struct in6_addr *taddr); 247 #endif 248 249 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; 250 251 /* 252 * XXX: Style; these should be sorted alphabetically, and unprototyped 253 * static functions should be prototyped. Currently they are sorted by 254 * declaration order. 255 */ 256 static void if_attachdomain(void *); 257 static void if_attachdomain1(struct ifnet *); 258 static int ifconf(u_long, caddr_t); 259 static void *if_grow(void); 260 static void if_input_default(struct ifnet *, struct mbuf *); 261 static int if_requestencap_default(struct ifnet *, struct if_encap_req *); 262 static void if_route(struct ifnet *, int flag, int fam); 263 static int if_setflag(struct ifnet *, int, int, int *, int); 264 static int if_transmit(struct ifnet *ifp, struct mbuf *m); 265 static void if_unroute(struct ifnet *, int flag, int fam); 266 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 267 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int); 268 static void do_link_state_change(void *, int); 269 static int if_getgroup(struct ifgroupreq *, struct ifnet *); 270 static int if_getgroupmembers(struct ifgroupreq *); 271 static void if_delgroups(struct ifnet *); 272 static void if_attach_internal(struct ifnet *, int, struct if_clone *); 273 static int if_detach_internal(struct ifnet *, int, struct if_clone **); 274 #ifdef VIMAGE 275 static void if_vmove(struct ifnet *, struct vnet *); 276 #endif 277 278 #ifdef INET6 279 /* 280 * XXX: declare here to avoid to include many inet6 related files.. 281 * should be more generalized? 282 */ 283 extern void nd6_setmtu(struct ifnet *); 284 #endif 285 286 /* ipsec helper hooks */ 287 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]); 288 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]); 289 290 VNET_DEFINE(int, if_index); 291 int ifqmaxlen = IFQ_MAXLEN; 292 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */ 293 VNET_DEFINE(struct ifgrouphead, ifg_head); 294 295 VNET_DEFINE_STATIC(int, if_indexlim) = 8; 296 297 /* Table of ifnet by index. */ 298 VNET_DEFINE(struct ifnet **, ifindex_table); 299 300 #define V_if_indexlim VNET(if_indexlim) 301 #define V_ifindex_table VNET(ifindex_table) 302 303 /* 304 * The global network interface list (V_ifnet) and related state (such as 305 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and 306 * an rwlock. Either may be acquired shared to stablize the list, but both 307 * must be acquired writable to modify the list. This model allows us to 308 * both stablize the interface list during interrupt thread processing, but 309 * also to stablize it over long-running ioctls, without introducing priority 310 * inversions and deadlocks. 311 */ 312 struct rwlock ifnet_rwlock; 313 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE); 314 struct sx ifnet_sxlock; 315 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE); 316 317 /* 318 * The allocation of network interfaces is a rather non-atomic affair; we 319 * need to select an index before we are ready to expose the interface for 320 * use, so will use this pointer value to indicate reservation. 321 */ 322 #define IFNET_HOLD (void *)(uintptr_t)(-1) 323 324 static if_com_alloc_t *if_com_alloc[256]; 325 static if_com_free_t *if_com_free[256]; 326 327 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals"); 328 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 329 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 330 331 struct ifnet * 332 ifnet_byindex_locked(u_short idx) 333 { 334 335 if (idx > V_if_index) 336 return (NULL); 337 if (V_ifindex_table[idx] == IFNET_HOLD) 338 return (NULL); 339 return (V_ifindex_table[idx]); 340 } 341 342 struct ifnet * 343 ifnet_byindex(u_short idx) 344 { 345 struct ifnet *ifp; 346 347 ifp = ifnet_byindex_locked(idx); 348 return (ifp); 349 } 350 351 struct ifnet * 352 ifnet_byindex_ref(u_short idx) 353 { 354 struct ifnet *ifp; 355 356 IFNET_RLOCK_NOSLEEP(); 357 ifp = ifnet_byindex_locked(idx); 358 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) { 359 IFNET_RUNLOCK_NOSLEEP(); 360 return (NULL); 361 } 362 if_ref(ifp); 363 IFNET_RUNLOCK_NOSLEEP(); 364 return (ifp); 365 } 366 367 /* 368 * Allocate an ifindex array entry; return 0 on success or an error on 369 * failure. 370 */ 371 static u_short 372 ifindex_alloc(void **old) 373 { 374 u_short idx; 375 376 IFNET_WLOCK_ASSERT(); 377 /* 378 * Try to find an empty slot below V_if_index. If we fail, take the 379 * next slot. 380 */ 381 for (idx = 1; idx <= V_if_index; idx++) { 382 if (V_ifindex_table[idx] == NULL) 383 break; 384 } 385 386 /* Catch if_index overflow. */ 387 if (idx >= V_if_indexlim) { 388 *old = if_grow(); 389 return (USHRT_MAX); 390 } 391 if (idx > V_if_index) 392 V_if_index = idx; 393 return (idx); 394 } 395 396 static void 397 ifindex_free_locked(u_short idx) 398 { 399 400 IFNET_WLOCK_ASSERT(); 401 402 V_ifindex_table[idx] = NULL; 403 while (V_if_index > 0 && 404 V_ifindex_table[V_if_index] == NULL) 405 V_if_index--; 406 } 407 408 static void 409 ifindex_free(u_short idx) 410 { 411 412 IFNET_WLOCK(); 413 ifindex_free_locked(idx); 414 IFNET_WUNLOCK(); 415 } 416 417 static void 418 ifnet_setbyindex(u_short idx, struct ifnet *ifp) 419 { 420 421 V_ifindex_table[idx] = ifp; 422 } 423 424 struct ifaddr * 425 ifaddr_byindex(u_short idx) 426 { 427 struct ifnet *ifp; 428 struct ifaddr *ifa = NULL; 429 430 IFNET_RLOCK_NOSLEEP(); 431 ifp = ifnet_byindex_locked(idx); 432 if (ifp != NULL && (ifa = ifp->if_addr) != NULL) 433 ifa_ref(ifa); 434 IFNET_RUNLOCK_NOSLEEP(); 435 return (ifa); 436 } 437 438 /* 439 * Network interface utility routines. 440 * 441 * Routines with ifa_ifwith* names take sockaddr *'s as 442 * parameters. 443 */ 444 445 static void 446 vnet_if_init(const void *unused __unused) 447 { 448 void *old; 449 450 CK_STAILQ_INIT(&V_ifnet); 451 CK_STAILQ_INIT(&V_ifg_head); 452 IFNET_WLOCK(); 453 old = if_grow(); /* create initial table */ 454 IFNET_WUNLOCK(); 455 epoch_wait_preempt(net_epoch_preempt); 456 free(old, M_IFNET); 457 vnet_if_clone_init(); 458 } 459 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init, 460 NULL); 461 462 #ifdef VIMAGE 463 static void 464 vnet_if_uninit(const void *unused __unused) 465 { 466 467 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p " 468 "not empty", __func__, __LINE__, &V_ifnet)); 469 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p " 470 "not empty", __func__, __LINE__, &V_ifg_head)); 471 472 free((caddr_t)V_ifindex_table, M_IFNET); 473 } 474 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST, 475 vnet_if_uninit, NULL); 476 477 static void 478 vnet_if_return(const void *unused __unused) 479 { 480 struct ifnet *ifp, *nifp; 481 482 /* Return all inherited interfaces to their parent vnets. */ 483 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) { 484 if (ifp->if_home_vnet != ifp->if_vnet) 485 if_vmove(ifp, ifp->if_home_vnet); 486 } 487 } 488 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY, 489 vnet_if_return, NULL); 490 #endif 491 492 493 static void * 494 if_grow(void) 495 { 496 int oldlim; 497 u_int n; 498 struct ifnet **e; 499 void *old; 500 501 old = NULL; 502 IFNET_WLOCK_ASSERT(); 503 oldlim = V_if_indexlim; 504 IFNET_WUNLOCK(); 505 n = (oldlim << 1) * sizeof(*e); 506 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO); 507 IFNET_WLOCK(); 508 if (V_if_indexlim != oldlim) { 509 free(e, M_IFNET); 510 return (NULL); 511 } 512 if (V_ifindex_table != NULL) { 513 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2); 514 old = V_ifindex_table; 515 } 516 V_if_indexlim <<= 1; 517 V_ifindex_table = e; 518 return (old); 519 } 520 521 /* 522 * Allocate a struct ifnet and an index for an interface. A layer 2 523 * common structure will also be allocated if an allocation routine is 524 * registered for the passed type. 525 */ 526 struct ifnet * 527 if_alloc(u_char type) 528 { 529 struct ifnet *ifp; 530 u_short idx; 531 void *old; 532 533 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); 534 restart: 535 IFNET_WLOCK(); 536 idx = ifindex_alloc(&old); 537 if (__predict_false(idx == USHRT_MAX)) { 538 IFNET_WUNLOCK(); 539 epoch_wait_preempt(net_epoch_preempt); 540 free(old, M_IFNET); 541 goto restart; 542 } 543 ifnet_setbyindex(idx, IFNET_HOLD); 544 IFNET_WUNLOCK(); 545 ifp->if_index = idx; 546 ifp->if_type = type; 547 ifp->if_alloctype = type; 548 #ifdef VIMAGE 549 ifp->if_vnet = curvnet; 550 #endif 551 if (if_com_alloc[type] != NULL) { 552 ifp->if_l2com = if_com_alloc[type](type, ifp); 553 if (ifp->if_l2com == NULL) { 554 free(ifp, M_IFNET); 555 ifindex_free(idx); 556 return (NULL); 557 } 558 } 559 560 IF_ADDR_LOCK_INIT(ifp); 561 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp); 562 ifp->if_afdata_initialized = 0; 563 IF_AFDATA_LOCK_INIT(ifp); 564 CK_STAILQ_INIT(&ifp->if_addrhead); 565 CK_STAILQ_INIT(&ifp->if_multiaddrs); 566 CK_STAILQ_INIT(&ifp->if_groups); 567 #ifdef MAC 568 mac_ifnet_init(ifp); 569 #endif 570 ifq_init(&ifp->if_snd, ifp); 571 572 refcount_init(&ifp->if_refcount, 1); /* Index reference. */ 573 for (int i = 0; i < IFCOUNTERS; i++) 574 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK); 575 ifp->if_get_counter = if_get_counter_default; 576 ifp->if_pcp = IFNET_PCP_NONE; 577 ifnet_setbyindex(ifp->if_index, ifp); 578 return (ifp); 579 } 580 581 /* 582 * Do the actual work of freeing a struct ifnet, and layer 2 common 583 * structure. This call is made when the last reference to an 584 * interface is released. 585 */ 586 static void 587 if_free_internal(struct ifnet *ifp) 588 { 589 590 KASSERT((ifp->if_flags & IFF_DYING), 591 ("if_free_internal: interface not dying")); 592 593 if (if_com_free[ifp->if_alloctype] != NULL) 594 if_com_free[ifp->if_alloctype](ifp->if_l2com, 595 ifp->if_alloctype); 596 597 #ifdef MAC 598 mac_ifnet_destroy(ifp); 599 #endif /* MAC */ 600 IF_AFDATA_DESTROY(ifp); 601 IF_ADDR_LOCK_DESTROY(ifp); 602 ifq_delete(&ifp->if_snd); 603 604 for (int i = 0; i < IFCOUNTERS; i++) 605 counter_u64_free(ifp->if_counters[i]); 606 607 free(ifp->if_description, M_IFDESCR); 608 free(ifp->if_hw_addr, M_IFADDR); 609 free(ifp, M_IFNET); 610 } 611 612 static void 613 if_destroy(epoch_context_t ctx) 614 { 615 struct ifnet *ifp; 616 617 ifp = __containerof(ctx, struct ifnet, if_epoch_ctx); 618 if_free_internal(ifp); 619 } 620 621 /* 622 * Deregister an interface and free the associated storage. 623 */ 624 void 625 if_free(struct ifnet *ifp) 626 { 627 628 ifp->if_flags |= IFF_DYING; /* XXX: Locking */ 629 630 CURVNET_SET_QUIET(ifp->if_vnet); 631 IFNET_WLOCK(); 632 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index), 633 ("%s: freeing unallocated ifnet", ifp->if_xname)); 634 635 ifindex_free_locked(ifp->if_index); 636 IFNET_WUNLOCK(); 637 638 if (refcount_release(&ifp->if_refcount)) 639 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy); 640 CURVNET_RESTORE(); 641 } 642 643 /* 644 * Interfaces to keep an ifnet type-stable despite the possibility of the 645 * driver calling if_free(). If there are additional references, we defer 646 * freeing the underlying data structure. 647 */ 648 void 649 if_ref(struct ifnet *ifp) 650 { 651 652 /* We don't assert the ifnet list lock here, but arguably should. */ 653 refcount_acquire(&ifp->if_refcount); 654 } 655 656 void 657 if_rele(struct ifnet *ifp) 658 { 659 660 if (!refcount_release(&ifp->if_refcount)) 661 return; 662 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy); 663 } 664 665 void 666 ifq_init(struct ifaltq *ifq, struct ifnet *ifp) 667 { 668 669 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); 670 671 if (ifq->ifq_maxlen == 0) 672 ifq->ifq_maxlen = ifqmaxlen; 673 674 ifq->altq_type = 0; 675 ifq->altq_disc = NULL; 676 ifq->altq_flags &= ALTQF_CANTCHANGE; 677 ifq->altq_tbr = NULL; 678 ifq->altq_ifp = ifp; 679 } 680 681 void 682 ifq_delete(struct ifaltq *ifq) 683 { 684 mtx_destroy(&ifq->ifq_mtx); 685 } 686 687 /* 688 * Perform generic interface initialization tasks and attach the interface 689 * to the list of "active" interfaces. If vmove flag is set on entry 690 * to if_attach_internal(), perform only a limited subset of initialization 691 * tasks, given that we are moving from one vnet to another an ifnet which 692 * has already been fully initialized. 693 * 694 * Note that if_detach_internal() removes group membership unconditionally 695 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL. 696 * Thus, when if_vmove() is applied to a cloned interface, group membership 697 * is lost while a cloned one always joins a group whose name is 698 * ifc->ifc_name. To recover this after if_detach_internal() and 699 * if_attach_internal(), the cloner should be specified to 700 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal() 701 * attempts to join a group whose name is ifc->ifc_name. 702 * 703 * XXX: 704 * - The decision to return void and thus require this function to 705 * succeed is questionable. 706 * - We should probably do more sanity checking. For instance we don't 707 * do anything to insure if_xname is unique or non-empty. 708 */ 709 void 710 if_attach(struct ifnet *ifp) 711 { 712 713 if_attach_internal(ifp, 0, NULL); 714 } 715 716 /* 717 * Compute the least common TSO limit. 718 */ 719 void 720 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax) 721 { 722 /* 723 * 1) If there is no limit currently, take the limit from 724 * the network adapter. 725 * 726 * 2) If the network adapter has a limit below the current 727 * limit, apply it. 728 */ 729 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 && 730 ifp->if_hw_tsomax < pmax->tsomaxbytes)) { 731 pmax->tsomaxbytes = ifp->if_hw_tsomax; 732 } 733 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 && 734 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) { 735 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount; 736 } 737 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 && 738 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) { 739 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize; 740 } 741 } 742 743 /* 744 * Update TSO limit of a network adapter. 745 * 746 * Returns zero if no change. Else non-zero. 747 */ 748 int 749 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax) 750 { 751 int retval = 0; 752 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) { 753 ifp->if_hw_tsomax = pmax->tsomaxbytes; 754 retval++; 755 } 756 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) { 757 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize; 758 retval++; 759 } 760 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) { 761 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount; 762 retval++; 763 } 764 return (retval); 765 } 766 767 static void 768 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc) 769 { 770 unsigned socksize, ifasize; 771 int namelen, masklen; 772 struct sockaddr_dl *sdl; 773 struct ifaddr *ifa; 774 775 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index)) 776 panic ("%s: BUG: if_attach called without if_alloc'd input()\n", 777 ifp->if_xname); 778 779 #ifdef VIMAGE 780 ifp->if_vnet = curvnet; 781 if (ifp->if_home_vnet == NULL) 782 ifp->if_home_vnet = curvnet; 783 #endif 784 785 if_addgroup(ifp, IFG_ALL); 786 787 /* Restore group membership for cloned interfaces. */ 788 if (vmove && ifc != NULL) 789 if_clone_addgroup(ifp, ifc); 790 791 getmicrotime(&ifp->if_lastchange); 792 ifp->if_epoch = time_uptime; 793 794 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) || 795 (ifp->if_transmit != NULL && ifp->if_qflush != NULL), 796 ("transmit and qflush must both either be set or both be NULL")); 797 if (ifp->if_transmit == NULL) { 798 ifp->if_transmit = if_transmit; 799 ifp->if_qflush = if_qflush; 800 } 801 if (ifp->if_input == NULL) 802 ifp->if_input = if_input_default; 803 804 if (ifp->if_requestencap == NULL) 805 ifp->if_requestencap = if_requestencap_default; 806 807 if (!vmove) { 808 #ifdef MAC 809 mac_ifnet_create(ifp); 810 #endif 811 812 /* 813 * Create a Link Level name for this device. 814 */ 815 namelen = strlen(ifp->if_xname); 816 /* 817 * Always save enough space for any possiable name so we 818 * can do a rename in place later. 819 */ 820 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ; 821 socksize = masklen + ifp->if_addrlen; 822 if (socksize < sizeof(*sdl)) 823 socksize = sizeof(*sdl); 824 socksize = roundup2(socksize, sizeof(long)); 825 ifasize = sizeof(*ifa) + 2 * socksize; 826 ifa = ifa_alloc(ifasize, M_WAITOK); 827 sdl = (struct sockaddr_dl *)(ifa + 1); 828 sdl->sdl_len = socksize; 829 sdl->sdl_family = AF_LINK; 830 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 831 sdl->sdl_nlen = namelen; 832 sdl->sdl_index = ifp->if_index; 833 sdl->sdl_type = ifp->if_type; 834 ifp->if_addr = ifa; 835 ifa->ifa_ifp = ifp; 836 ifa->ifa_rtrequest = link_rtrequest; 837 ifa->ifa_addr = (struct sockaddr *)sdl; 838 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 839 ifa->ifa_netmask = (struct sockaddr *)sdl; 840 sdl->sdl_len = masklen; 841 while (namelen != 0) 842 sdl->sdl_data[--namelen] = 0xff; 843 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 844 /* Reliably crash if used uninitialized. */ 845 ifp->if_broadcastaddr = NULL; 846 847 if (ifp->if_type == IFT_ETHER) { 848 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR, 849 M_WAITOK | M_ZERO); 850 } 851 852 #if defined(INET) || defined(INET6) 853 /* Use defaults for TSO, if nothing is set */ 854 if (ifp->if_hw_tsomax == 0 && 855 ifp->if_hw_tsomaxsegcount == 0 && 856 ifp->if_hw_tsomaxsegsize == 0) { 857 /* 858 * The TSO defaults needs to be such that an 859 * NFS mbuf list of 35 mbufs totalling just 860 * below 64K works and that a chain of mbufs 861 * can be defragged into at most 32 segments: 862 */ 863 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) - 864 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN)); 865 ifp->if_hw_tsomaxsegcount = 35; 866 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */ 867 868 /* XXX some drivers set IFCAP_TSO after ethernet attach */ 869 if (ifp->if_capabilities & IFCAP_TSO) { 870 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n", 871 ifp->if_hw_tsomax, 872 ifp->if_hw_tsomaxsegcount, 873 ifp->if_hw_tsomaxsegsize); 874 } 875 } 876 #endif 877 } 878 #ifdef VIMAGE 879 else { 880 /* 881 * Update the interface index in the link layer address 882 * of the interface. 883 */ 884 for (ifa = ifp->if_addr; ifa != NULL; 885 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) { 886 if (ifa->ifa_addr->sa_family == AF_LINK) { 887 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 888 sdl->sdl_index = ifp->if_index; 889 } 890 } 891 } 892 #endif 893 894 IFNET_WLOCK(); 895 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link); 896 #ifdef VIMAGE 897 curvnet->vnet_ifcnt++; 898 #endif 899 IFNET_WUNLOCK(); 900 901 if (domain_init_status >= 2) 902 if_attachdomain1(ifp); 903 904 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 905 if (IS_DEFAULT_VNET(curvnet)) 906 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); 907 908 /* Announce the interface. */ 909 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 910 } 911 912 static void 913 if_epochalloc(void *dummy __unused) 914 { 915 916 net_epoch_preempt = epoch_alloc(EPOCH_PREEMPT); 917 net_epoch = epoch_alloc(0); 918 } 919 SYSINIT(ifepochalloc, SI_SUB_TASKQ + 1, SI_ORDER_ANY, 920 if_epochalloc, NULL); 921 922 static void 923 if_attachdomain(void *dummy) 924 { 925 struct ifnet *ifp; 926 927 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) 928 if_attachdomain1(ifp); 929 } 930 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 931 if_attachdomain, NULL); 932 933 static void 934 if_attachdomain1(struct ifnet *ifp) 935 { 936 struct domain *dp; 937 938 /* 939 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we 940 * cannot lock ifp->if_afdata initialization, entirely. 941 */ 942 IF_AFDATA_LOCK(ifp); 943 if (ifp->if_afdata_initialized >= domain_init_status) { 944 IF_AFDATA_UNLOCK(ifp); 945 log(LOG_WARNING, "%s called more than once on %s\n", 946 __func__, ifp->if_xname); 947 return; 948 } 949 ifp->if_afdata_initialized = domain_init_status; 950 IF_AFDATA_UNLOCK(ifp); 951 952 /* address family dependent data region */ 953 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 954 for (dp = domains; dp; dp = dp->dom_next) { 955 if (dp->dom_ifattach) 956 ifp->if_afdata[dp->dom_family] = 957 (*dp->dom_ifattach)(ifp); 958 } 959 } 960 961 /* 962 * Remove any unicast or broadcast network addresses from an interface. 963 */ 964 void 965 if_purgeaddrs(struct ifnet *ifp) 966 { 967 struct ifaddr *ifa; 968 969 while (1) { 970 NET_EPOCH_ENTER(); 971 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 972 if (ifa->ifa_addr->sa_family != AF_LINK) 973 break; 974 } 975 NET_EPOCH_EXIT(); 976 977 if (ifa == NULL) 978 break; 979 #ifdef INET 980 /* XXX: Ugly!! ad hoc just for INET */ 981 if (ifa->ifa_addr->sa_family == AF_INET) { 982 struct ifaliasreq ifr; 983 984 bzero(&ifr, sizeof(ifr)); 985 ifr.ifra_addr = *ifa->ifa_addr; 986 if (ifa->ifa_dstaddr) 987 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 988 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 989 NULL) == 0) 990 continue; 991 } 992 #endif /* INET */ 993 #ifdef INET6 994 if (ifa->ifa_addr->sa_family == AF_INET6) { 995 in6_purgeaddr(ifa); 996 /* ifp_addrhead is already updated */ 997 continue; 998 } 999 #endif /* INET6 */ 1000 IF_ADDR_WLOCK(ifp); 1001 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link); 1002 IF_ADDR_WUNLOCK(ifp); 1003 ifa_free(ifa); 1004 } 1005 } 1006 1007 /* 1008 * Remove any multicast network addresses from an interface when an ifnet 1009 * is going away. 1010 */ 1011 static void 1012 if_purgemaddrs(struct ifnet *ifp) 1013 { 1014 struct ifmultiaddr *ifma; 1015 1016 IF_ADDR_WLOCK(ifp); 1017 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) { 1018 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs); 1019 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link); 1020 if_delmulti_locked(ifp, ifma, 1); 1021 } 1022 IF_ADDR_WUNLOCK(ifp); 1023 } 1024 1025 /* 1026 * Detach an interface, removing it from the list of "active" interfaces. 1027 * If vmove flag is set on entry to if_detach_internal(), perform only a 1028 * limited subset of cleanup tasks, given that we are moving an ifnet from 1029 * one vnet to another, where it must be fully operational. 1030 * 1031 * XXXRW: There are some significant questions about event ordering, and 1032 * how to prevent things from starting to use the interface during detach. 1033 */ 1034 void 1035 if_detach(struct ifnet *ifp) 1036 { 1037 1038 CURVNET_SET_QUIET(ifp->if_vnet); 1039 if_detach_internal(ifp, 0, NULL); 1040 CURVNET_RESTORE(); 1041 } 1042 1043 /* 1044 * The vmove flag, if set, indicates that we are called from a callpath 1045 * that is moving an interface to a different vnet instance. 1046 * 1047 * The shutdown flag, if set, indicates that we are called in the 1048 * process of shutting down a vnet instance. Currently only the 1049 * vnet_if_return SYSUNINIT function sets it. Note: we can be called 1050 * on a vnet instance shutdown without this flag being set, e.g., when 1051 * the cloned interfaces are destoyed as first thing of teardown. 1052 */ 1053 static int 1054 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp) 1055 { 1056 struct ifaddr *ifa; 1057 int i; 1058 struct domain *dp; 1059 struct ifnet *iter; 1060 int found = 0; 1061 #ifdef VIMAGE 1062 int shutdown; 1063 1064 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET && 1065 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0; 1066 #endif 1067 IFNET_WLOCK(); 1068 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link) 1069 if (iter == ifp) { 1070 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link); 1071 if (!vmove) 1072 ifp->if_flags |= IFF_DYING; 1073 found = 1; 1074 break; 1075 } 1076 IFNET_WUNLOCK(); 1077 if (!found) { 1078 /* 1079 * While we would want to panic here, we cannot 1080 * guarantee that the interface is indeed still on 1081 * the list given we don't hold locks all the way. 1082 */ 1083 return (ENOENT); 1084 #if 0 1085 if (vmove) 1086 panic("%s: ifp=%p not on the ifnet tailq %p", 1087 __func__, ifp, &V_ifnet); 1088 else 1089 return; /* XXX this should panic as well? */ 1090 #endif 1091 } 1092 1093 /* 1094 * At this point we know the interface still was on the ifnet list 1095 * and we removed it so we are in a stable state. 1096 */ 1097 #ifdef VIMAGE 1098 curvnet->vnet_ifcnt--; 1099 #endif 1100 epoch_wait_preempt(net_epoch_preempt); 1101 /* 1102 * In any case (destroy or vmove) detach us from the groups 1103 * and remove/wait for pending events on the taskq. 1104 * XXX-BZ in theory an interface could still enqueue a taskq change? 1105 */ 1106 if_delgroups(ifp); 1107 1108 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 1109 1110 /* 1111 * Check if this is a cloned interface or not. Must do even if 1112 * shutting down as a if_vmove_reclaim() would move the ifp and 1113 * the if_clone_addgroup() will have a corrupted string overwise 1114 * from a gibberish pointer. 1115 */ 1116 if (vmove && ifcp != NULL) 1117 *ifcp = if_clone_findifc(ifp); 1118 1119 if_down(ifp); 1120 1121 #ifdef VIMAGE 1122 /* 1123 * On VNET shutdown abort here as the stack teardown will do all 1124 * the work top-down for us. 1125 */ 1126 if (shutdown) { 1127 /* Give interface users the chance to clean up. */ 1128 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 1129 1130 /* 1131 * In case of a vmove we are done here without error. 1132 * If we would signal an error it would lead to the same 1133 * abort as if we did not find the ifnet anymore. 1134 * if_detach() calls us in void context and does not care 1135 * about an early abort notification, so life is splendid :) 1136 */ 1137 goto finish_vnet_shutdown; 1138 } 1139 #endif 1140 1141 /* 1142 * At this point we are not tearing down a VNET and are either 1143 * going to destroy or vmove the interface and have to cleanup 1144 * accordingly. 1145 */ 1146 1147 /* 1148 * Remove routes and flush queues. 1149 */ 1150 #ifdef ALTQ 1151 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 1152 altq_disable(&ifp->if_snd); 1153 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 1154 altq_detach(&ifp->if_snd); 1155 #endif 1156 1157 if_purgeaddrs(ifp); 1158 1159 #ifdef INET 1160 in_ifdetach(ifp); 1161 #endif 1162 1163 #ifdef INET6 1164 /* 1165 * Remove all IPv6 kernel structs related to ifp. This should be done 1166 * before removing routing entries below, since IPv6 interface direct 1167 * routes are expected to be removed by the IPv6-specific kernel API. 1168 * Otherwise, the kernel will detect some inconsistency and bark it. 1169 */ 1170 in6_ifdetach(ifp); 1171 #endif 1172 if_purgemaddrs(ifp); 1173 1174 /* Announce that the interface is gone. */ 1175 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 1176 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 1177 if (IS_DEFAULT_VNET(curvnet)) 1178 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); 1179 1180 if (!vmove) { 1181 /* 1182 * Prevent further calls into the device driver via ifnet. 1183 */ 1184 if_dead(ifp); 1185 1186 /* 1187 * Clean up all addresses. 1188 */ 1189 IF_ADDR_WLOCK(ifp); 1190 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) { 1191 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead); 1192 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link); 1193 IF_ADDR_WUNLOCK(ifp); 1194 ifa_free(ifa); 1195 } else 1196 IF_ADDR_WUNLOCK(ifp); 1197 } 1198 1199 rt_flushifroutes(ifp); 1200 1201 #ifdef VIMAGE 1202 finish_vnet_shutdown: 1203 #endif 1204 /* 1205 * We cannot hold the lock over dom_ifdetach calls as they might 1206 * sleep, for example trying to drain a callout, thus open up the 1207 * theoretical race with re-attaching. 1208 */ 1209 IF_AFDATA_LOCK(ifp); 1210 i = ifp->if_afdata_initialized; 1211 ifp->if_afdata_initialized = 0; 1212 IF_AFDATA_UNLOCK(ifp); 1213 for (dp = domains; i > 0 && dp; dp = dp->dom_next) { 1214 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) { 1215 (*dp->dom_ifdetach)(ifp, 1216 ifp->if_afdata[dp->dom_family]); 1217 ifp->if_afdata[dp->dom_family] = NULL; 1218 } 1219 } 1220 1221 return (0); 1222 } 1223 1224 #ifdef VIMAGE 1225 /* 1226 * if_vmove() performs a limited version of if_detach() in current 1227 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg. 1228 * An attempt is made to shrink if_index in current vnet, find an 1229 * unused if_index in target vnet and calls if_grow() if necessary, 1230 * and finally find an unused if_xname for the target vnet. 1231 */ 1232 static void 1233 if_vmove(struct ifnet *ifp, struct vnet *new_vnet) 1234 { 1235 struct if_clone *ifc; 1236 u_int bif_dlt, bif_hdrlen; 1237 void *old; 1238 int rc; 1239 1240 /* 1241 * if_detach_internal() will call the eventhandler to notify 1242 * interface departure. That will detach if_bpf. We need to 1243 * safe the dlt and hdrlen so we can re-attach it later. 1244 */ 1245 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen); 1246 1247 /* 1248 * Detach from current vnet, but preserve LLADDR info, do not 1249 * mark as dead etc. so that the ifnet can be reattached later. 1250 * If we cannot find it, we lost the race to someone else. 1251 */ 1252 rc = if_detach_internal(ifp, 1, &ifc); 1253 if (rc != 0) 1254 return; 1255 1256 /* 1257 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink 1258 * the if_index for that vnet if possible. 1259 * 1260 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized, 1261 * or we'd lock on one vnet and unlock on another. 1262 */ 1263 IFNET_WLOCK(); 1264 ifindex_free_locked(ifp->if_index); 1265 IFNET_WUNLOCK(); 1266 1267 /* 1268 * Perform interface-specific reassignment tasks, if provided by 1269 * the driver. 1270 */ 1271 if (ifp->if_reassign != NULL) 1272 ifp->if_reassign(ifp, new_vnet, NULL); 1273 1274 /* 1275 * Switch to the context of the target vnet. 1276 */ 1277 CURVNET_SET_QUIET(new_vnet); 1278 restart: 1279 IFNET_WLOCK(); 1280 ifp->if_index = ifindex_alloc(&old); 1281 if (__predict_false(ifp->if_index == USHRT_MAX)) { 1282 IFNET_WUNLOCK(); 1283 epoch_wait_preempt(net_epoch_preempt); 1284 free(old, M_IFNET); 1285 goto restart; 1286 } 1287 ifnet_setbyindex(ifp->if_index, ifp); 1288 IFNET_WUNLOCK(); 1289 1290 if_attach_internal(ifp, 1, ifc); 1291 1292 if (ifp->if_bpf == NULL) 1293 bpfattach(ifp, bif_dlt, bif_hdrlen); 1294 1295 CURVNET_RESTORE(); 1296 } 1297 1298 /* 1299 * Move an ifnet to or from another child prison/vnet, specified by the jail id. 1300 */ 1301 static int 1302 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid) 1303 { 1304 struct prison *pr; 1305 struct ifnet *difp; 1306 int shutdown; 1307 1308 /* Try to find the prison within our visibility. */ 1309 sx_slock(&allprison_lock); 1310 pr = prison_find_child(td->td_ucred->cr_prison, jid); 1311 sx_sunlock(&allprison_lock); 1312 if (pr == NULL) 1313 return (ENXIO); 1314 prison_hold_locked(pr); 1315 mtx_unlock(&pr->pr_mtx); 1316 1317 /* Do not try to move the iface from and to the same prison. */ 1318 if (pr->pr_vnet == ifp->if_vnet) { 1319 prison_free(pr); 1320 return (EEXIST); 1321 } 1322 1323 /* Make sure the named iface does not exists in the dst. prison/vnet. */ 1324 /* XXX Lock interfaces to avoid races. */ 1325 CURVNET_SET_QUIET(pr->pr_vnet); 1326 difp = ifunit(ifname); 1327 if (difp != NULL) { 1328 CURVNET_RESTORE(); 1329 prison_free(pr); 1330 return (EEXIST); 1331 } 1332 1333 /* Make sure the VNET is stable. */ 1334 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET && 1335 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0; 1336 if (shutdown) { 1337 CURVNET_RESTORE(); 1338 prison_free(pr); 1339 return (EBUSY); 1340 } 1341 CURVNET_RESTORE(); 1342 1343 /* Move the interface into the child jail/vnet. */ 1344 if_vmove(ifp, pr->pr_vnet); 1345 1346 /* Report the new if_xname back to the userland. */ 1347 sprintf(ifname, "%s", ifp->if_xname); 1348 1349 prison_free(pr); 1350 return (0); 1351 } 1352 1353 static int 1354 if_vmove_reclaim(struct thread *td, char *ifname, int jid) 1355 { 1356 struct prison *pr; 1357 struct vnet *vnet_dst; 1358 struct ifnet *ifp; 1359 int shutdown; 1360 1361 /* Try to find the prison within our visibility. */ 1362 sx_slock(&allprison_lock); 1363 pr = prison_find_child(td->td_ucred->cr_prison, jid); 1364 sx_sunlock(&allprison_lock); 1365 if (pr == NULL) 1366 return (ENXIO); 1367 prison_hold_locked(pr); 1368 mtx_unlock(&pr->pr_mtx); 1369 1370 /* Make sure the named iface exists in the source prison/vnet. */ 1371 CURVNET_SET(pr->pr_vnet); 1372 ifp = ifunit(ifname); /* XXX Lock to avoid races. */ 1373 if (ifp == NULL) { 1374 CURVNET_RESTORE(); 1375 prison_free(pr); 1376 return (ENXIO); 1377 } 1378 1379 /* Do not try to move the iface from and to the same prison. */ 1380 vnet_dst = TD_TO_VNET(td); 1381 if (vnet_dst == ifp->if_vnet) { 1382 CURVNET_RESTORE(); 1383 prison_free(pr); 1384 return (EEXIST); 1385 } 1386 1387 /* Make sure the VNET is stable. */ 1388 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET && 1389 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0; 1390 if (shutdown) { 1391 CURVNET_RESTORE(); 1392 prison_free(pr); 1393 return (EBUSY); 1394 } 1395 1396 /* Get interface back from child jail/vnet. */ 1397 if_vmove(ifp, vnet_dst); 1398 CURVNET_RESTORE(); 1399 1400 /* Report the new if_xname back to the userland. */ 1401 sprintf(ifname, "%s", ifp->if_xname); 1402 1403 prison_free(pr); 1404 return (0); 1405 } 1406 #endif /* VIMAGE */ 1407 1408 /* 1409 * Add a group to an interface 1410 */ 1411 int 1412 if_addgroup(struct ifnet *ifp, const char *groupname) 1413 { 1414 struct ifg_list *ifgl; 1415 struct ifg_group *ifg = NULL; 1416 struct ifg_member *ifgm; 1417 int new = 0; 1418 1419 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 1420 groupname[strlen(groupname) - 1] <= '9') 1421 return (EINVAL); 1422 1423 IFNET_WLOCK(); 1424 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1425 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) { 1426 IFNET_WUNLOCK(); 1427 return (EEXIST); 1428 } 1429 1430 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP, 1431 M_NOWAIT)) == NULL) { 1432 IFNET_WUNLOCK(); 1433 return (ENOMEM); 1434 } 1435 1436 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member), 1437 M_TEMP, M_NOWAIT)) == NULL) { 1438 free(ifgl, M_TEMP); 1439 IFNET_WUNLOCK(); 1440 return (ENOMEM); 1441 } 1442 1443 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 1444 if (!strcmp(ifg->ifg_group, groupname)) 1445 break; 1446 1447 if (ifg == NULL) { 1448 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group), 1449 M_TEMP, M_NOWAIT)) == NULL) { 1450 free(ifgl, M_TEMP); 1451 free(ifgm, M_TEMP); 1452 IFNET_WUNLOCK(); 1453 return (ENOMEM); 1454 } 1455 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 1456 ifg->ifg_refcnt = 0; 1457 CK_STAILQ_INIT(&ifg->ifg_members); 1458 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next); 1459 new = 1; 1460 } 1461 1462 ifg->ifg_refcnt++; 1463 ifgl->ifgl_group = ifg; 1464 ifgm->ifgm_ifp = ifp; 1465 1466 IF_ADDR_WLOCK(ifp); 1467 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 1468 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 1469 IF_ADDR_WUNLOCK(ifp); 1470 1471 IFNET_WUNLOCK(); 1472 1473 if (new) 1474 EVENTHANDLER_INVOKE(group_attach_event, ifg); 1475 EVENTHANDLER_INVOKE(group_change_event, groupname); 1476 1477 return (0); 1478 } 1479 1480 /* 1481 * Remove a group from an interface 1482 */ 1483 int 1484 if_delgroup(struct ifnet *ifp, const char *groupname) 1485 { 1486 struct ifg_list *ifgl; 1487 struct ifg_member *ifgm; 1488 int freeifgl; 1489 1490 IFNET_WLOCK(); 1491 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1492 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 1493 break; 1494 if (ifgl == NULL) { 1495 IFNET_WUNLOCK(); 1496 return (ENOENT); 1497 } 1498 1499 freeifgl = 0; 1500 IF_ADDR_WLOCK(ifp); 1501 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next); 1502 IF_ADDR_WUNLOCK(ifp); 1503 1504 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 1505 if (ifgm->ifgm_ifp == ifp) 1506 break; 1507 1508 if (ifgm != NULL) 1509 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifg_member, ifgm_next); 1510 1511 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 1512 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group, ifg_next); 1513 freeifgl = 1; 1514 } 1515 IFNET_WUNLOCK(); 1516 1517 epoch_wait_preempt(net_epoch_preempt); 1518 if (freeifgl) { 1519 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group); 1520 free(ifgl->ifgl_group, M_TEMP); 1521 } 1522 free(ifgm, M_TEMP); 1523 free(ifgl, M_TEMP); 1524 1525 EVENTHANDLER_INVOKE(group_change_event, groupname); 1526 1527 return (0); 1528 } 1529 1530 /* 1531 * Remove an interface from all groups 1532 */ 1533 static void 1534 if_delgroups(struct ifnet *ifp) 1535 { 1536 struct ifg_list *ifgl; 1537 struct ifg_member *ifgm; 1538 char groupname[IFNAMSIZ]; 1539 int ifglfree; 1540 1541 IFNET_WLOCK(); 1542 while (!CK_STAILQ_EMPTY(&ifp->if_groups)) { 1543 ifgl = CK_STAILQ_FIRST(&ifp->if_groups); 1544 1545 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ); 1546 1547 IF_ADDR_WLOCK(ifp); 1548 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next); 1549 IF_ADDR_WUNLOCK(ifp); 1550 1551 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 1552 if (ifgm->ifgm_ifp == ifp) 1553 break; 1554 1555 if (ifgm != NULL) 1556 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifg_member, 1557 ifgm_next); 1558 ifglfree = 0; 1559 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 1560 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group, ifg_next); 1561 ifglfree = 1; 1562 } 1563 1564 IFNET_WUNLOCK(); 1565 epoch_wait_preempt(net_epoch_preempt); 1566 free(ifgm, M_TEMP); 1567 if (ifglfree) { 1568 EVENTHANDLER_INVOKE(group_detach_event, 1569 ifgl->ifgl_group); 1570 free(ifgl->ifgl_group, M_TEMP); 1571 } 1572 EVENTHANDLER_INVOKE(group_change_event, groupname); 1573 1574 IFNET_WLOCK(); 1575 } 1576 IFNET_WUNLOCK(); 1577 } 1578 1579 static char * 1580 ifgr_group_get(void *ifgrp) 1581 { 1582 union ifgroupreq_union *ifgrup; 1583 1584 ifgrup = ifgrp; 1585 #ifdef COMPAT_FREEBSD32 1586 if (SV_CURPROC_FLAG(SV_ILP32)) 1587 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]); 1588 #endif 1589 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]); 1590 } 1591 1592 static struct ifg_req * 1593 ifgr_groups_get(void *ifgrp) 1594 { 1595 union ifgroupreq_union *ifgrup; 1596 1597 ifgrup = ifgrp; 1598 #ifdef COMPAT_FREEBSD32 1599 if (SV_CURPROC_FLAG(SV_ILP32)) 1600 return ((struct ifg_req *)(uintptr_t) 1601 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups); 1602 #endif 1603 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups); 1604 } 1605 1606 /* 1607 * Stores all groups from an interface in memory pointed to by ifgr. 1608 */ 1609 static int 1610 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp) 1611 { 1612 int len, error; 1613 struct ifg_list *ifgl; 1614 struct ifg_req ifgrq, *ifgp; 1615 1616 if (ifgr->ifgr_len == 0) { 1617 IF_ADDR_RLOCK(ifp); 1618 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1619 ifgr->ifgr_len += sizeof(struct ifg_req); 1620 IF_ADDR_RUNLOCK(ifp); 1621 return (0); 1622 } 1623 1624 len = ifgr->ifgr_len; 1625 ifgp = ifgr_groups_get(ifgr); 1626 /* XXX: wire */ 1627 IF_ADDR_RLOCK(ifp); 1628 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 1629 if (len < sizeof(ifgrq)) { 1630 IF_ADDR_RUNLOCK(ifp); 1631 return (EINVAL); 1632 } 1633 bzero(&ifgrq, sizeof ifgrq); 1634 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 1635 sizeof(ifgrq.ifgrq_group)); 1636 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1637 IF_ADDR_RUNLOCK(ifp); 1638 return (error); 1639 } 1640 len -= sizeof(ifgrq); 1641 ifgp++; 1642 } 1643 IF_ADDR_RUNLOCK(ifp); 1644 1645 return (0); 1646 } 1647 1648 /* 1649 * Stores all members of a group in memory pointed to by igfr 1650 */ 1651 static int 1652 if_getgroupmembers(struct ifgroupreq *ifgr) 1653 { 1654 struct ifg_group *ifg; 1655 struct ifg_member *ifgm; 1656 struct ifg_req ifgrq, *ifgp; 1657 int len, error; 1658 1659 IFNET_RLOCK(); 1660 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 1661 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 1662 break; 1663 if (ifg == NULL) { 1664 IFNET_RUNLOCK(); 1665 return (ENOENT); 1666 } 1667 1668 if (ifgr->ifgr_len == 0) { 1669 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 1670 ifgr->ifgr_len += sizeof(ifgrq); 1671 IFNET_RUNLOCK(); 1672 return (0); 1673 } 1674 1675 len = ifgr->ifgr_len; 1676 ifgp = ifgr_groups_get(ifgr); 1677 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 1678 if (len < sizeof(ifgrq)) { 1679 IFNET_RUNLOCK(); 1680 return (EINVAL); 1681 } 1682 bzero(&ifgrq, sizeof ifgrq); 1683 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 1684 sizeof(ifgrq.ifgrq_member)); 1685 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1686 IFNET_RUNLOCK(); 1687 return (error); 1688 } 1689 len -= sizeof(ifgrq); 1690 ifgp++; 1691 } 1692 IFNET_RUNLOCK(); 1693 1694 return (0); 1695 } 1696 1697 /* 1698 * Return counter values from counter(9)s stored in ifnet. 1699 */ 1700 uint64_t 1701 if_get_counter_default(struct ifnet *ifp, ift_counter cnt) 1702 { 1703 1704 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt)); 1705 1706 return (counter_u64_fetch(ifp->if_counters[cnt])); 1707 } 1708 1709 /* 1710 * Increase an ifnet counter. Usually used for counters shared 1711 * between the stack and a driver, but function supports them all. 1712 */ 1713 void 1714 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc) 1715 { 1716 1717 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt)); 1718 1719 counter_u64_add(ifp->if_counters[cnt], inc); 1720 } 1721 1722 /* 1723 * Copy data from ifnet to userland API structure if_data. 1724 */ 1725 void 1726 if_data_copy(struct ifnet *ifp, struct if_data *ifd) 1727 { 1728 1729 ifd->ifi_type = ifp->if_type; 1730 ifd->ifi_physical = 0; 1731 ifd->ifi_addrlen = ifp->if_addrlen; 1732 ifd->ifi_hdrlen = ifp->if_hdrlen; 1733 ifd->ifi_link_state = ifp->if_link_state; 1734 ifd->ifi_vhid = 0; 1735 ifd->ifi_datalen = sizeof(struct if_data); 1736 ifd->ifi_mtu = ifp->if_mtu; 1737 ifd->ifi_metric = ifp->if_metric; 1738 ifd->ifi_baudrate = ifp->if_baudrate; 1739 ifd->ifi_hwassist = ifp->if_hwassist; 1740 ifd->ifi_epoch = ifp->if_epoch; 1741 ifd->ifi_lastchange = ifp->if_lastchange; 1742 1743 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS); 1744 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS); 1745 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS); 1746 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS); 1747 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS); 1748 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES); 1749 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES); 1750 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS); 1751 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS); 1752 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS); 1753 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS); 1754 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO); 1755 } 1756 1757 /* 1758 * Wrapper functions for struct ifnet address list locking macros. These are 1759 * used by kernel modules to avoid encoding programming interface or binary 1760 * interface assumptions that may be violated when kernel-internal locking 1761 * approaches change. 1762 */ 1763 void 1764 if_addr_rlock(struct ifnet *ifp) 1765 { 1766 1767 epoch_enter_preempt(net_epoch_preempt, curthread->td_et); 1768 } 1769 1770 void 1771 if_addr_runlock(struct ifnet *ifp) 1772 { 1773 1774 epoch_exit_preempt(net_epoch_preempt, curthread->td_et); 1775 } 1776 1777 void 1778 if_maddr_rlock(if_t ifp) 1779 { 1780 1781 epoch_enter_preempt(net_epoch_preempt, curthread->td_et); 1782 } 1783 1784 void 1785 if_maddr_runlock(if_t ifp) 1786 { 1787 1788 epoch_exit_preempt(net_epoch_preempt, curthread->td_et); 1789 } 1790 1791 /* 1792 * Initialization, destruction and refcounting functions for ifaddrs. 1793 */ 1794 struct ifaddr * 1795 ifa_alloc(size_t size, int flags) 1796 { 1797 struct ifaddr *ifa; 1798 1799 KASSERT(size >= sizeof(struct ifaddr), 1800 ("%s: invalid size %zu", __func__, size)); 1801 1802 ifa = malloc(size, M_IFADDR, M_ZERO | flags); 1803 if (ifa == NULL) 1804 return (NULL); 1805 1806 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL) 1807 goto fail; 1808 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL) 1809 goto fail; 1810 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL) 1811 goto fail; 1812 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL) 1813 goto fail; 1814 1815 refcount_init(&ifa->ifa_refcnt, 1); 1816 1817 return (ifa); 1818 1819 fail: 1820 /* free(NULL) is okay */ 1821 counter_u64_free(ifa->ifa_opackets); 1822 counter_u64_free(ifa->ifa_ipackets); 1823 counter_u64_free(ifa->ifa_obytes); 1824 counter_u64_free(ifa->ifa_ibytes); 1825 free(ifa, M_IFADDR); 1826 1827 return (NULL); 1828 } 1829 1830 void 1831 ifa_ref(struct ifaddr *ifa) 1832 { 1833 1834 refcount_acquire(&ifa->ifa_refcnt); 1835 } 1836 1837 static void 1838 ifa_destroy(epoch_context_t ctx) 1839 { 1840 struct ifaddr *ifa; 1841 1842 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx); 1843 counter_u64_free(ifa->ifa_opackets); 1844 counter_u64_free(ifa->ifa_ipackets); 1845 counter_u64_free(ifa->ifa_obytes); 1846 counter_u64_free(ifa->ifa_ibytes); 1847 free(ifa, M_IFADDR); 1848 } 1849 1850 void 1851 ifa_free(struct ifaddr *ifa) 1852 { 1853 1854 if (refcount_release(&ifa->ifa_refcnt)) 1855 epoch_call(net_epoch_preempt, &ifa->ifa_epoch_ctx, ifa_destroy); 1856 } 1857 1858 1859 static int 1860 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa, 1861 struct sockaddr *ia) 1862 { 1863 int error; 1864 struct rt_addrinfo info; 1865 struct sockaddr_dl null_sdl; 1866 struct ifnet *ifp; 1867 1868 ifp = ifa->ifa_ifp; 1869 1870 bzero(&info, sizeof(info)); 1871 if (cmd != RTM_DELETE) 1872 info.rti_ifp = V_loif; 1873 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED; 1874 info.rti_info[RTAX_DST] = ia; 1875 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 1876 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type); 1877 1878 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib); 1879 1880 if (error != 0 && 1881 !(cmd == RTM_ADD && error == EEXIST) && 1882 !(cmd == RTM_DELETE && error == ENOENT)) 1883 if_printf(ifp, "%s failed: %d\n", otype, error); 1884 1885 return (error); 1886 } 1887 1888 int 1889 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) 1890 { 1891 1892 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia)); 1893 } 1894 1895 int 1896 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) 1897 { 1898 1899 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia)); 1900 } 1901 1902 int 1903 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) 1904 { 1905 1906 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia)); 1907 } 1908 1909 /* 1910 * XXX: Because sockaddr_dl has deeper structure than the sockaddr 1911 * structs used to represent other address families, it is necessary 1912 * to perform a different comparison. 1913 */ 1914 1915 #define sa_dl_equal(a1, a2) \ 1916 ((((const struct sockaddr_dl *)(a1))->sdl_len == \ 1917 ((const struct sockaddr_dl *)(a2))->sdl_len) && \ 1918 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \ 1919 CLLADDR((const struct sockaddr_dl *)(a2)), \ 1920 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0)) 1921 1922 /* 1923 * Locate an interface based on a complete address. 1924 */ 1925 /*ARGSUSED*/ 1926 struct ifaddr * 1927 ifa_ifwithaddr(const struct sockaddr *addr) 1928 { 1929 struct ifnet *ifp; 1930 struct ifaddr *ifa; 1931 1932 MPASS(in_epoch(net_epoch_preempt)); 1933 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1934 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1935 if (ifa->ifa_addr->sa_family != addr->sa_family) 1936 continue; 1937 if (sa_equal(addr, ifa->ifa_addr)) { 1938 goto done; 1939 } 1940 /* IP6 doesn't have broadcast */ 1941 if ((ifp->if_flags & IFF_BROADCAST) && 1942 ifa->ifa_broadaddr && 1943 ifa->ifa_broadaddr->sa_len != 0 && 1944 sa_equal(ifa->ifa_broadaddr, addr)) { 1945 goto done; 1946 } 1947 } 1948 } 1949 ifa = NULL; 1950 done: 1951 return (ifa); 1952 } 1953 1954 int 1955 ifa_ifwithaddr_check(const struct sockaddr *addr) 1956 { 1957 int rc; 1958 1959 NET_EPOCH_ENTER(); 1960 rc = (ifa_ifwithaddr(addr) != NULL); 1961 NET_EPOCH_EXIT(); 1962 return (rc); 1963 } 1964 1965 /* 1966 * Locate an interface based on the broadcast address. 1967 */ 1968 /* ARGSUSED */ 1969 struct ifaddr * 1970 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum) 1971 { 1972 struct ifnet *ifp; 1973 struct ifaddr *ifa; 1974 1975 MPASS(in_epoch(net_epoch_preempt)); 1976 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1977 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum)) 1978 continue; 1979 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1980 if (ifa->ifa_addr->sa_family != addr->sa_family) 1981 continue; 1982 if ((ifp->if_flags & IFF_BROADCAST) && 1983 ifa->ifa_broadaddr && 1984 ifa->ifa_broadaddr->sa_len != 0 && 1985 sa_equal(ifa->ifa_broadaddr, addr)) { 1986 goto done; 1987 } 1988 } 1989 } 1990 ifa = NULL; 1991 done: 1992 return (ifa); 1993 } 1994 1995 /* 1996 * Locate the point to point interface with a given destination address. 1997 */ 1998 /*ARGSUSED*/ 1999 struct ifaddr * 2000 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum) 2001 { 2002 struct ifnet *ifp; 2003 struct ifaddr *ifa; 2004 2005 MPASS(in_epoch(net_epoch_preempt)); 2006 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2007 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 2008 continue; 2009 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum)) 2010 continue; 2011 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2012 if (ifa->ifa_addr->sa_family != addr->sa_family) 2013 continue; 2014 if (ifa->ifa_dstaddr != NULL && 2015 sa_equal(addr, ifa->ifa_dstaddr)) { 2016 goto done; 2017 } 2018 } 2019 } 2020 ifa = NULL; 2021 done: 2022 return (ifa); 2023 } 2024 2025 /* 2026 * Find an interface on a specific network. If many, choice 2027 * is most specific found. 2028 */ 2029 struct ifaddr * 2030 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum) 2031 { 2032 struct ifnet *ifp; 2033 struct ifaddr *ifa; 2034 struct ifaddr *ifa_maybe = NULL; 2035 u_int af = addr->sa_family; 2036 const char *addr_data = addr->sa_data, *cplim; 2037 2038 MPASS(in_epoch(net_epoch_preempt)); 2039 /* 2040 * AF_LINK addresses can be looked up directly by their index number, 2041 * so do that if we can. 2042 */ 2043 if (af == AF_LINK) { 2044 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr; 2045 if (sdl->sdl_index && sdl->sdl_index <= V_if_index) 2046 return (ifaddr_byindex(sdl->sdl_index)); 2047 } 2048 2049 /* 2050 * Scan though each interface, looking for ones that have addresses 2051 * in this address family and the requested fib. Maintain a reference 2052 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that 2053 * kept it stable when we move onto the next interface. 2054 */ 2055 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2056 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum)) 2057 continue; 2058 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2059 const char *cp, *cp2, *cp3; 2060 2061 if (ifa->ifa_addr->sa_family != af) 2062 next: continue; 2063 if (af == AF_INET && 2064 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) { 2065 /* 2066 * This is a bit broken as it doesn't 2067 * take into account that the remote end may 2068 * be a single node in the network we are 2069 * looking for. 2070 * The trouble is that we don't know the 2071 * netmask for the remote end. 2072 */ 2073 if (ifa->ifa_dstaddr != NULL && 2074 sa_equal(addr, ifa->ifa_dstaddr)) { 2075 goto done; 2076 } 2077 } else { 2078 /* 2079 * Scan all the bits in the ifa's address. 2080 * If a bit dissagrees with what we are 2081 * looking for, mask it with the netmask 2082 * to see if it really matters. 2083 * (A byte at a time) 2084 */ 2085 if (ifa->ifa_netmask == 0) 2086 continue; 2087 cp = addr_data; 2088 cp2 = ifa->ifa_addr->sa_data; 2089 cp3 = ifa->ifa_netmask->sa_data; 2090 cplim = ifa->ifa_netmask->sa_len 2091 + (char *)ifa->ifa_netmask; 2092 while (cp3 < cplim) 2093 if ((*cp++ ^ *cp2++) & *cp3++) 2094 goto next; /* next address! */ 2095 /* 2096 * If the netmask of what we just found 2097 * is more specific than what we had before 2098 * (if we had one), or if the virtual status 2099 * of new prefix is better than of the old one, 2100 * then remember the new one before continuing 2101 * to search for an even better one. 2102 */ 2103 if (ifa_maybe == NULL || 2104 ifa_preferred(ifa_maybe, ifa) || 2105 rn_refines((caddr_t)ifa->ifa_netmask, 2106 (caddr_t)ifa_maybe->ifa_netmask)) { 2107 ifa_maybe = ifa; 2108 } 2109 } 2110 } 2111 } 2112 ifa = ifa_maybe; 2113 ifa_maybe = NULL; 2114 done: 2115 return (ifa); 2116 } 2117 2118 /* 2119 * Find an interface address specific to an interface best matching 2120 * a given address. 2121 */ 2122 struct ifaddr * 2123 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp) 2124 { 2125 struct ifaddr *ifa; 2126 const char *cp, *cp2, *cp3; 2127 char *cplim; 2128 struct ifaddr *ifa_maybe = NULL; 2129 u_int af = addr->sa_family; 2130 2131 if (af >= AF_MAX) 2132 return (NULL); 2133 2134 MPASS(in_epoch(net_epoch_preempt)); 2135 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2136 if (ifa->ifa_addr->sa_family != af) 2137 continue; 2138 if (ifa_maybe == NULL) 2139 ifa_maybe = ifa; 2140 if (ifa->ifa_netmask == 0) { 2141 if (sa_equal(addr, ifa->ifa_addr) || 2142 (ifa->ifa_dstaddr && 2143 sa_equal(addr, ifa->ifa_dstaddr))) 2144 goto done; 2145 continue; 2146 } 2147 if (ifp->if_flags & IFF_POINTOPOINT) { 2148 if (sa_equal(addr, ifa->ifa_dstaddr)) 2149 goto done; 2150 } else { 2151 cp = addr->sa_data; 2152 cp2 = ifa->ifa_addr->sa_data; 2153 cp3 = ifa->ifa_netmask->sa_data; 2154 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 2155 for (; cp3 < cplim; cp3++) 2156 if ((*cp++ ^ *cp2++) & *cp3) 2157 break; 2158 if (cp3 == cplim) 2159 goto done; 2160 } 2161 } 2162 ifa = ifa_maybe; 2163 done: 2164 return (ifa); 2165 } 2166 2167 /* 2168 * See whether new ifa is better than current one: 2169 * 1) A non-virtual one is preferred over virtual. 2170 * 2) A virtual in master state preferred over any other state. 2171 * 2172 * Used in several address selecting functions. 2173 */ 2174 int 2175 ifa_preferred(struct ifaddr *cur, struct ifaddr *next) 2176 { 2177 2178 return (cur->ifa_carp && (!next->ifa_carp || 2179 ((*carp_master_p)(next) && !(*carp_master_p)(cur)))); 2180 } 2181 2182 #include <net/if_llatbl.h> 2183 2184 /* 2185 * Default action when installing a route with a Link Level gateway. 2186 * Lookup an appropriate real ifa to point to. 2187 * This should be moved to /sys/net/link.c eventually. 2188 */ 2189 static void 2190 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 2191 { 2192 struct ifaddr *ifa, *oifa; 2193 struct sockaddr *dst; 2194 struct ifnet *ifp; 2195 2196 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) || 2197 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL)) 2198 return; 2199 NET_EPOCH_ENTER(); 2200 ifa = ifaof_ifpforaddr(dst, ifp); 2201 if (ifa) { 2202 oifa = rt->rt_ifa; 2203 if (oifa != ifa) { 2204 ifa_free(oifa); 2205 ifa_ref(ifa); 2206 } 2207 rt->rt_ifa = ifa; 2208 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 2209 ifa->ifa_rtrequest(cmd, rt, info); 2210 } 2211 NET_EPOCH_EXIT(); 2212 } 2213 2214 struct sockaddr_dl * 2215 link_alloc_sdl(size_t size, int flags) 2216 { 2217 2218 return (malloc(size, M_TEMP, flags)); 2219 } 2220 2221 void 2222 link_free_sdl(struct sockaddr *sa) 2223 { 2224 free(sa, M_TEMP); 2225 } 2226 2227 /* 2228 * Fills in given sdl with interface basic info. 2229 * Returns pointer to filled sdl. 2230 */ 2231 struct sockaddr_dl * 2232 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype) 2233 { 2234 struct sockaddr_dl *sdl; 2235 2236 sdl = (struct sockaddr_dl *)paddr; 2237 memset(sdl, 0, sizeof(struct sockaddr_dl)); 2238 sdl->sdl_len = sizeof(struct sockaddr_dl); 2239 sdl->sdl_family = AF_LINK; 2240 sdl->sdl_index = ifp->if_index; 2241 sdl->sdl_type = iftype; 2242 2243 return (sdl); 2244 } 2245 2246 /* 2247 * Mark an interface down and notify protocols of 2248 * the transition. 2249 */ 2250 static void 2251 if_unroute(struct ifnet *ifp, int flag, int fam) 2252 { 2253 struct ifaddr *ifa; 2254 2255 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 2256 2257 ifp->if_flags &= ~flag; 2258 getmicrotime(&ifp->if_lastchange); 2259 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 2260 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 2261 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 2262 ifp->if_qflush(ifp); 2263 2264 if (ifp->if_carp) 2265 (*carp_linkstate_p)(ifp); 2266 rt_ifmsg(ifp); 2267 } 2268 2269 /* 2270 * Mark an interface up and notify protocols of 2271 * the transition. 2272 */ 2273 static void 2274 if_route(struct ifnet *ifp, int flag, int fam) 2275 { 2276 struct ifaddr *ifa; 2277 2278 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 2279 2280 ifp->if_flags |= flag; 2281 getmicrotime(&ifp->if_lastchange); 2282 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 2283 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 2284 pfctlinput(PRC_IFUP, ifa->ifa_addr); 2285 if (ifp->if_carp) 2286 (*carp_linkstate_p)(ifp); 2287 rt_ifmsg(ifp); 2288 #ifdef INET6 2289 in6_if_up(ifp); 2290 #endif 2291 } 2292 2293 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */ 2294 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 2295 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *); 2296 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t); 2297 int (*vlan_tag_p)(struct ifnet *, uint16_t *); 2298 int (*vlan_pcp_p)(struct ifnet *, uint16_t *); 2299 int (*vlan_setcookie_p)(struct ifnet *, void *); 2300 void *(*vlan_cookie_p)(struct ifnet *); 2301 2302 /* 2303 * Handle a change in the interface link state. To avoid LORs 2304 * between driver lock and upper layer locks, as well as possible 2305 * recursions, we post event to taskqueue, and all job 2306 * is done in static do_link_state_change(). 2307 */ 2308 void 2309 if_link_state_change(struct ifnet *ifp, int link_state) 2310 { 2311 /* Return if state hasn't changed. */ 2312 if (ifp->if_link_state == link_state) 2313 return; 2314 2315 ifp->if_link_state = link_state; 2316 2317 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 2318 } 2319 2320 static void 2321 do_link_state_change(void *arg, int pending) 2322 { 2323 struct ifnet *ifp = (struct ifnet *)arg; 2324 int link_state = ifp->if_link_state; 2325 CURVNET_SET(ifp->if_vnet); 2326 2327 /* Notify that the link state has changed. */ 2328 rt_ifmsg(ifp); 2329 if (ifp->if_vlantrunk != NULL) 2330 (*vlan_link_state_p)(ifp); 2331 2332 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 2333 ifp->if_l2com != NULL) 2334 (*ng_ether_link_state_p)(ifp, link_state); 2335 if (ifp->if_carp) 2336 (*carp_linkstate_p)(ifp); 2337 if (ifp->if_bridge) 2338 ifp->if_bridge_linkstate(ifp); 2339 if (ifp->if_lagg) 2340 (*lagg_linkstate_p)(ifp, link_state); 2341 2342 if (IS_DEFAULT_VNET(curvnet)) 2343 devctl_notify("IFNET", ifp->if_xname, 2344 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", 2345 NULL); 2346 if (pending > 1) 2347 if_printf(ifp, "%d link states coalesced\n", pending); 2348 if (log_link_state_change) 2349 if_printf(ifp, "link state changed to %s\n", 2350 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 2351 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state); 2352 CURVNET_RESTORE(); 2353 } 2354 2355 /* 2356 * Mark an interface down and notify protocols of 2357 * the transition. 2358 */ 2359 void 2360 if_down(struct ifnet *ifp) 2361 { 2362 2363 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN); 2364 if_unroute(ifp, IFF_UP, AF_UNSPEC); 2365 } 2366 2367 /* 2368 * Mark an interface up and notify protocols of 2369 * the transition. 2370 */ 2371 void 2372 if_up(struct ifnet *ifp) 2373 { 2374 2375 if_route(ifp, IFF_UP, AF_UNSPEC); 2376 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP); 2377 } 2378 2379 /* 2380 * Flush an interface queue. 2381 */ 2382 void 2383 if_qflush(struct ifnet *ifp) 2384 { 2385 struct mbuf *m, *n; 2386 struct ifaltq *ifq; 2387 2388 ifq = &ifp->if_snd; 2389 IFQ_LOCK(ifq); 2390 #ifdef ALTQ 2391 if (ALTQ_IS_ENABLED(ifq)) 2392 ALTQ_PURGE(ifq); 2393 #endif 2394 n = ifq->ifq_head; 2395 while ((m = n) != NULL) { 2396 n = m->m_nextpkt; 2397 m_freem(m); 2398 } 2399 ifq->ifq_head = 0; 2400 ifq->ifq_tail = 0; 2401 ifq->ifq_len = 0; 2402 IFQ_UNLOCK(ifq); 2403 } 2404 2405 /* 2406 * Map interface name to interface structure pointer, with or without 2407 * returning a reference. 2408 */ 2409 struct ifnet * 2410 ifunit_ref(const char *name) 2411 { 2412 struct ifnet *ifp; 2413 2414 IFNET_RLOCK_NOSLEEP(); 2415 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2416 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 && 2417 !(ifp->if_flags & IFF_DYING)) 2418 break; 2419 } 2420 if (ifp != NULL) 2421 if_ref(ifp); 2422 IFNET_RUNLOCK_NOSLEEP(); 2423 return (ifp); 2424 } 2425 2426 struct ifnet * 2427 ifunit(const char *name) 2428 { 2429 struct ifnet *ifp; 2430 2431 IFNET_RLOCK_NOSLEEP(); 2432 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2433 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 2434 break; 2435 } 2436 IFNET_RUNLOCK_NOSLEEP(); 2437 return (ifp); 2438 } 2439 2440 static void * 2441 ifr_buffer_get_buffer(void *data) 2442 { 2443 union ifreq_union *ifrup; 2444 2445 ifrup = data; 2446 #ifdef COMPAT_FREEBSD32 2447 if (SV_CURPROC_FLAG(SV_ILP32)) 2448 return ((void *)(uintptr_t) 2449 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer); 2450 #endif 2451 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer); 2452 } 2453 2454 static void 2455 ifr_buffer_set_buffer_null(void *data) 2456 { 2457 union ifreq_union *ifrup; 2458 2459 ifrup = data; 2460 #ifdef COMPAT_FREEBSD32 2461 if (SV_CURPROC_FLAG(SV_ILP32)) 2462 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0; 2463 else 2464 #endif 2465 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL; 2466 } 2467 2468 static size_t 2469 ifr_buffer_get_length(void *data) 2470 { 2471 union ifreq_union *ifrup; 2472 2473 ifrup = data; 2474 #ifdef COMPAT_FREEBSD32 2475 if (SV_CURPROC_FLAG(SV_ILP32)) 2476 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length); 2477 #endif 2478 return (ifrup->ifr.ifr_ifru.ifru_buffer.length); 2479 } 2480 2481 static void 2482 ifr_buffer_set_length(void *data, size_t len) 2483 { 2484 union ifreq_union *ifrup; 2485 2486 ifrup = data; 2487 #ifdef COMPAT_FREEBSD32 2488 if (SV_CURPROC_FLAG(SV_ILP32)) 2489 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len; 2490 else 2491 #endif 2492 ifrup->ifr.ifr_ifru.ifru_buffer.length = len; 2493 } 2494 2495 void * 2496 ifr_data_get_ptr(void *ifrp) 2497 { 2498 union ifreq_union *ifrup; 2499 2500 ifrup = ifrp; 2501 #ifdef COMPAT_FREEBSD32 2502 if (SV_CURPROC_FLAG(SV_ILP32)) 2503 return ((void *)(uintptr_t) 2504 ifrup->ifr32.ifr_ifru.ifru_data); 2505 #endif 2506 return (ifrup->ifr.ifr_ifru.ifru_data); 2507 } 2508 2509 /* 2510 * Hardware specific interface ioctls. 2511 */ 2512 int 2513 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 2514 { 2515 struct ifreq *ifr; 2516 int error = 0, do_ifup = 0; 2517 int new_flags, temp_flags; 2518 size_t namelen, onamelen; 2519 size_t descrlen; 2520 char *descrbuf, *odescrbuf; 2521 char new_name[IFNAMSIZ]; 2522 struct ifaddr *ifa; 2523 struct sockaddr_dl *sdl; 2524 2525 ifr = (struct ifreq *)data; 2526 switch (cmd) { 2527 case SIOCGIFINDEX: 2528 ifr->ifr_index = ifp->if_index; 2529 break; 2530 2531 case SIOCGIFFLAGS: 2532 temp_flags = ifp->if_flags | ifp->if_drv_flags; 2533 ifr->ifr_flags = temp_flags & 0xffff; 2534 ifr->ifr_flagshigh = temp_flags >> 16; 2535 break; 2536 2537 case SIOCGIFCAP: 2538 ifr->ifr_reqcap = ifp->if_capabilities; 2539 ifr->ifr_curcap = ifp->if_capenable; 2540 break; 2541 2542 #ifdef MAC 2543 case SIOCGIFMAC: 2544 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp); 2545 break; 2546 #endif 2547 2548 case SIOCGIFMETRIC: 2549 ifr->ifr_metric = ifp->if_metric; 2550 break; 2551 2552 case SIOCGIFMTU: 2553 ifr->ifr_mtu = ifp->if_mtu; 2554 break; 2555 2556 case SIOCGIFPHYS: 2557 /* XXXGL: did this ever worked? */ 2558 ifr->ifr_phys = 0; 2559 break; 2560 2561 case SIOCGIFDESCR: 2562 error = 0; 2563 sx_slock(&ifdescr_sx); 2564 if (ifp->if_description == NULL) 2565 error = ENOMSG; 2566 else { 2567 /* space for terminating nul */ 2568 descrlen = strlen(ifp->if_description) + 1; 2569 if (ifr_buffer_get_length(ifr) < descrlen) 2570 ifr_buffer_set_buffer_null(ifr); 2571 else 2572 error = copyout(ifp->if_description, 2573 ifr_buffer_get_buffer(ifr), descrlen); 2574 ifr_buffer_set_length(ifr, descrlen); 2575 } 2576 sx_sunlock(&ifdescr_sx); 2577 break; 2578 2579 case SIOCSIFDESCR: 2580 error = priv_check(td, PRIV_NET_SETIFDESCR); 2581 if (error) 2582 return (error); 2583 2584 /* 2585 * Copy only (length-1) bytes to make sure that 2586 * if_description is always nul terminated. The 2587 * length parameter is supposed to count the 2588 * terminating nul in. 2589 */ 2590 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen) 2591 return (ENAMETOOLONG); 2592 else if (ifr_buffer_get_length(ifr) == 0) 2593 descrbuf = NULL; 2594 else { 2595 descrbuf = malloc(ifr_buffer_get_length(ifr), 2596 M_IFDESCR, M_WAITOK | M_ZERO); 2597 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf, 2598 ifr_buffer_get_length(ifr) - 1); 2599 if (error) { 2600 free(descrbuf, M_IFDESCR); 2601 break; 2602 } 2603 } 2604 2605 sx_xlock(&ifdescr_sx); 2606 odescrbuf = ifp->if_description; 2607 ifp->if_description = descrbuf; 2608 sx_xunlock(&ifdescr_sx); 2609 2610 getmicrotime(&ifp->if_lastchange); 2611 free(odescrbuf, M_IFDESCR); 2612 break; 2613 2614 case SIOCGIFFIB: 2615 ifr->ifr_fib = ifp->if_fib; 2616 break; 2617 2618 case SIOCSIFFIB: 2619 error = priv_check(td, PRIV_NET_SETIFFIB); 2620 if (error) 2621 return (error); 2622 if (ifr->ifr_fib >= rt_numfibs) 2623 return (EINVAL); 2624 2625 ifp->if_fib = ifr->ifr_fib; 2626 break; 2627 2628 case SIOCSIFFLAGS: 2629 error = priv_check(td, PRIV_NET_SETIFFLAGS); 2630 if (error) 2631 return (error); 2632 /* 2633 * Currently, no driver owned flags pass the IFF_CANTCHANGE 2634 * check, so we don't need special handling here yet. 2635 */ 2636 new_flags = (ifr->ifr_flags & 0xffff) | 2637 (ifr->ifr_flagshigh << 16); 2638 if (ifp->if_flags & IFF_UP && 2639 (new_flags & IFF_UP) == 0) { 2640 if_down(ifp); 2641 } else if (new_flags & IFF_UP && 2642 (ifp->if_flags & IFF_UP) == 0) { 2643 do_ifup = 1; 2644 } 2645 /* See if permanently promiscuous mode bit is about to flip */ 2646 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 2647 if (new_flags & IFF_PPROMISC) 2648 ifp->if_flags |= IFF_PROMISC; 2649 else if (ifp->if_pcount == 0) 2650 ifp->if_flags &= ~IFF_PROMISC; 2651 if (log_promisc_mode_change) 2652 if_printf(ifp, "permanently promiscuous mode %s\n", 2653 ((new_flags & IFF_PPROMISC) ? 2654 "enabled" : "disabled")); 2655 } 2656 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 2657 (new_flags &~ IFF_CANTCHANGE); 2658 if (ifp->if_ioctl) { 2659 (void) (*ifp->if_ioctl)(ifp, cmd, data); 2660 } 2661 if (do_ifup) 2662 if_up(ifp); 2663 getmicrotime(&ifp->if_lastchange); 2664 break; 2665 2666 case SIOCSIFCAP: 2667 error = priv_check(td, PRIV_NET_SETIFCAP); 2668 if (error) 2669 return (error); 2670 if (ifp->if_ioctl == NULL) 2671 return (EOPNOTSUPP); 2672 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 2673 return (EINVAL); 2674 error = (*ifp->if_ioctl)(ifp, cmd, data); 2675 if (error == 0) 2676 getmicrotime(&ifp->if_lastchange); 2677 break; 2678 2679 #ifdef MAC 2680 case SIOCSIFMAC: 2681 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp); 2682 break; 2683 #endif 2684 2685 case SIOCSIFNAME: 2686 error = priv_check(td, PRIV_NET_SETIFNAME); 2687 if (error) 2688 return (error); 2689 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ, 2690 NULL); 2691 if (error != 0) 2692 return (error); 2693 if (new_name[0] == '\0') 2694 return (EINVAL); 2695 if (new_name[IFNAMSIZ-1] != '\0') { 2696 new_name[IFNAMSIZ-1] = '\0'; 2697 if (strlen(new_name) == IFNAMSIZ-1) 2698 return (EINVAL); 2699 } 2700 if (ifunit(new_name) != NULL) 2701 return (EEXIST); 2702 2703 /* 2704 * XXX: Locking. Nothing else seems to lock if_flags, 2705 * and there are numerous other races with the 2706 * ifunit() checks not being atomic with namespace 2707 * changes (renames, vmoves, if_attach, etc). 2708 */ 2709 ifp->if_flags |= IFF_RENAMING; 2710 2711 /* Announce the departure of the interface. */ 2712 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 2713 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 2714 2715 if_printf(ifp, "changing name to '%s'\n", new_name); 2716 2717 IF_ADDR_WLOCK(ifp); 2718 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 2719 ifa = ifp->if_addr; 2720 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 2721 namelen = strlen(new_name); 2722 onamelen = sdl->sdl_nlen; 2723 /* 2724 * Move the address if needed. This is safe because we 2725 * allocate space for a name of length IFNAMSIZ when we 2726 * create this in if_attach(). 2727 */ 2728 if (namelen != onamelen) { 2729 bcopy(sdl->sdl_data + onamelen, 2730 sdl->sdl_data + namelen, sdl->sdl_alen); 2731 } 2732 bcopy(new_name, sdl->sdl_data, namelen); 2733 sdl->sdl_nlen = namelen; 2734 sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 2735 bzero(sdl->sdl_data, onamelen); 2736 while (namelen != 0) 2737 sdl->sdl_data[--namelen] = 0xff; 2738 IF_ADDR_WUNLOCK(ifp); 2739 2740 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 2741 /* Announce the return of the interface. */ 2742 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 2743 2744 ifp->if_flags &= ~IFF_RENAMING; 2745 break; 2746 2747 #ifdef VIMAGE 2748 case SIOCSIFVNET: 2749 error = priv_check(td, PRIV_NET_SETIFVNET); 2750 if (error) 2751 return (error); 2752 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid); 2753 break; 2754 #endif 2755 2756 case SIOCSIFMETRIC: 2757 error = priv_check(td, PRIV_NET_SETIFMETRIC); 2758 if (error) 2759 return (error); 2760 ifp->if_metric = ifr->ifr_metric; 2761 getmicrotime(&ifp->if_lastchange); 2762 break; 2763 2764 case SIOCSIFPHYS: 2765 error = priv_check(td, PRIV_NET_SETIFPHYS); 2766 if (error) 2767 return (error); 2768 if (ifp->if_ioctl == NULL) 2769 return (EOPNOTSUPP); 2770 error = (*ifp->if_ioctl)(ifp, cmd, data); 2771 if (error == 0) 2772 getmicrotime(&ifp->if_lastchange); 2773 break; 2774 2775 case SIOCSIFMTU: 2776 { 2777 u_long oldmtu = ifp->if_mtu; 2778 2779 error = priv_check(td, PRIV_NET_SETIFMTU); 2780 if (error) 2781 return (error); 2782 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 2783 return (EINVAL); 2784 if (ifp->if_ioctl == NULL) 2785 return (EOPNOTSUPP); 2786 error = (*ifp->if_ioctl)(ifp, cmd, data); 2787 if (error == 0) { 2788 getmicrotime(&ifp->if_lastchange); 2789 rt_ifmsg(ifp); 2790 #ifdef INET 2791 NETDUMP_REINIT(ifp); 2792 #endif 2793 } 2794 /* 2795 * If the link MTU changed, do network layer specific procedure. 2796 */ 2797 if (ifp->if_mtu != oldmtu) { 2798 #ifdef INET6 2799 nd6_setmtu(ifp); 2800 #endif 2801 rt_updatemtu(ifp); 2802 } 2803 break; 2804 } 2805 2806 case SIOCADDMULTI: 2807 case SIOCDELMULTI: 2808 if (cmd == SIOCADDMULTI) 2809 error = priv_check(td, PRIV_NET_ADDMULTI); 2810 else 2811 error = priv_check(td, PRIV_NET_DELMULTI); 2812 if (error) 2813 return (error); 2814 2815 /* Don't allow group membership on non-multicast interfaces. */ 2816 if ((ifp->if_flags & IFF_MULTICAST) == 0) 2817 return (EOPNOTSUPP); 2818 2819 /* Don't let users screw up protocols' entries. */ 2820 if (ifr->ifr_addr.sa_family != AF_LINK) 2821 return (EINVAL); 2822 2823 if (cmd == SIOCADDMULTI) { 2824 struct ifmultiaddr *ifma; 2825 2826 /* 2827 * Userland is only permitted to join groups once 2828 * via the if_addmulti() KPI, because it cannot hold 2829 * struct ifmultiaddr * between calls. It may also 2830 * lose a race while we check if the membership 2831 * already exists. 2832 */ 2833 IF_ADDR_RLOCK(ifp); 2834 ifma = if_findmulti(ifp, &ifr->ifr_addr); 2835 IF_ADDR_RUNLOCK(ifp); 2836 if (ifma != NULL) 2837 error = EADDRINUSE; 2838 else 2839 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 2840 } else { 2841 error = if_delmulti(ifp, &ifr->ifr_addr); 2842 } 2843 if (error == 0) 2844 getmicrotime(&ifp->if_lastchange); 2845 break; 2846 2847 case SIOCSIFPHYADDR: 2848 case SIOCDIFPHYADDR: 2849 #ifdef INET6 2850 case SIOCSIFPHYADDR_IN6: 2851 #endif 2852 case SIOCSIFMEDIA: 2853 case SIOCSIFGENERIC: 2854 error = priv_check(td, PRIV_NET_HWIOCTL); 2855 if (error) 2856 return (error); 2857 if (ifp->if_ioctl == NULL) 2858 return (EOPNOTSUPP); 2859 error = (*ifp->if_ioctl)(ifp, cmd, data); 2860 if (error == 0) 2861 getmicrotime(&ifp->if_lastchange); 2862 break; 2863 2864 case SIOCGIFSTATUS: 2865 case SIOCGIFPSRCADDR: 2866 case SIOCGIFPDSTADDR: 2867 case SIOCGIFMEDIA: 2868 case SIOCGIFXMEDIA: 2869 case SIOCGIFGENERIC: 2870 case SIOCGIFRSSKEY: 2871 case SIOCGIFRSSHASH: 2872 if (ifp->if_ioctl == NULL) 2873 return (EOPNOTSUPP); 2874 error = (*ifp->if_ioctl)(ifp, cmd, data); 2875 break; 2876 2877 case SIOCSIFLLADDR: 2878 error = priv_check(td, PRIV_NET_SETLLADDR); 2879 if (error) 2880 return (error); 2881 error = if_setlladdr(ifp, 2882 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 2883 break; 2884 2885 case SIOCGHWADDR: 2886 error = if_gethwaddr(ifp, ifr); 2887 break; 2888 2889 CASE_IOC_IFGROUPREQ(SIOCAIFGROUP): 2890 error = priv_check(td, PRIV_NET_ADDIFGROUP); 2891 if (error) 2892 return (error); 2893 if ((error = if_addgroup(ifp, 2894 ifgr_group_get((struct ifgroupreq *)data)))) 2895 return (error); 2896 break; 2897 2898 CASE_IOC_IFGROUPREQ(SIOCGIFGROUP): 2899 if ((error = if_getgroup((struct ifgroupreq *)data, ifp))) 2900 return (error); 2901 break; 2902 2903 CASE_IOC_IFGROUPREQ(SIOCDIFGROUP): 2904 error = priv_check(td, PRIV_NET_DELIFGROUP); 2905 if (error) 2906 return (error); 2907 if ((error = if_delgroup(ifp, 2908 ifgr_group_get((struct ifgroupreq *)data)))) 2909 return (error); 2910 break; 2911 2912 default: 2913 error = ENOIOCTL; 2914 break; 2915 } 2916 return (error); 2917 } 2918 2919 #ifdef COMPAT_FREEBSD32 2920 struct ifconf32 { 2921 int32_t ifc_len; 2922 union { 2923 uint32_t ifcu_buf; 2924 uint32_t ifcu_req; 2925 } ifc_ifcu; 2926 }; 2927 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32) 2928 #endif 2929 2930 #ifdef COMPAT_FREEBSD32 2931 static void 2932 ifmr_init(struct ifmediareq *ifmr, caddr_t data) 2933 { 2934 struct ifmediareq32 *ifmr32; 2935 2936 ifmr32 = (struct ifmediareq32 *)data; 2937 memcpy(ifmr->ifm_name, ifmr32->ifm_name, 2938 sizeof(ifmr->ifm_name)); 2939 ifmr->ifm_current = ifmr32->ifm_current; 2940 ifmr->ifm_mask = ifmr32->ifm_mask; 2941 ifmr->ifm_status = ifmr32->ifm_status; 2942 ifmr->ifm_active = ifmr32->ifm_active; 2943 ifmr->ifm_count = ifmr32->ifm_count; 2944 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist; 2945 } 2946 2947 static void 2948 ifmr_update(const struct ifmediareq *ifmr, caddr_t data) 2949 { 2950 struct ifmediareq32 *ifmr32; 2951 2952 ifmr32 = (struct ifmediareq32 *)data; 2953 ifmr32->ifm_current = ifmr->ifm_current; 2954 ifmr32->ifm_mask = ifmr->ifm_mask; 2955 ifmr32->ifm_status = ifmr->ifm_status; 2956 ifmr32->ifm_active = ifmr->ifm_active; 2957 ifmr32->ifm_count = ifmr->ifm_count; 2958 } 2959 #endif 2960 2961 /* 2962 * Interface ioctls. 2963 */ 2964 int 2965 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 2966 { 2967 #ifdef COMPAT_FREEBSD32 2968 caddr_t saved_data = NULL; 2969 struct ifmediareq ifmr; 2970 struct ifmediareq *ifmrp; 2971 #endif 2972 struct ifnet *ifp; 2973 struct ifreq *ifr; 2974 int error; 2975 int oif_flags; 2976 #ifdef VIMAGE 2977 int shutdown; 2978 #endif 2979 2980 CURVNET_SET(so->so_vnet); 2981 #ifdef VIMAGE 2982 /* Make sure the VNET is stable. */ 2983 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET && 2984 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0; 2985 if (shutdown) { 2986 CURVNET_RESTORE(); 2987 return (EBUSY); 2988 } 2989 #endif 2990 2991 2992 switch (cmd) { 2993 case SIOCGIFCONF: 2994 error = ifconf(cmd, data); 2995 CURVNET_RESTORE(); 2996 return (error); 2997 2998 #ifdef COMPAT_FREEBSD32 2999 case SIOCGIFCONF32: 3000 { 3001 struct ifconf32 *ifc32; 3002 struct ifconf ifc; 3003 3004 ifc32 = (struct ifconf32 *)data; 3005 ifc.ifc_len = ifc32->ifc_len; 3006 ifc.ifc_buf = PTRIN(ifc32->ifc_buf); 3007 3008 error = ifconf(SIOCGIFCONF, (void *)&ifc); 3009 CURVNET_RESTORE(); 3010 if (error == 0) 3011 ifc32->ifc_len = ifc.ifc_len; 3012 return (error); 3013 } 3014 #endif 3015 } 3016 3017 #ifdef COMPAT_FREEBSD32 3018 ifmrp = NULL; 3019 switch (cmd) { 3020 case SIOCGIFMEDIA32: 3021 case SIOCGIFXMEDIA32: 3022 ifmrp = &ifmr; 3023 ifmr_init(ifmrp, data); 3024 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq); 3025 saved_data = data; 3026 data = (caddr_t)ifmrp; 3027 } 3028 #endif 3029 3030 ifr = (struct ifreq *)data; 3031 switch (cmd) { 3032 #ifdef VIMAGE 3033 case SIOCSIFRVNET: 3034 error = priv_check(td, PRIV_NET_SETIFVNET); 3035 if (error == 0) 3036 error = if_vmove_reclaim(td, ifr->ifr_name, 3037 ifr->ifr_jid); 3038 goto out_noref; 3039 #endif 3040 case SIOCIFCREATE: 3041 case SIOCIFCREATE2: 3042 error = priv_check(td, PRIV_NET_IFCREATE); 3043 if (error == 0) 3044 error = if_clone_create(ifr->ifr_name, 3045 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ? 3046 ifr_data_get_ptr(ifr) : NULL); 3047 goto out_noref; 3048 case SIOCIFDESTROY: 3049 error = priv_check(td, PRIV_NET_IFDESTROY); 3050 if (error == 0) 3051 error = if_clone_destroy(ifr->ifr_name); 3052 goto out_noref; 3053 3054 case SIOCIFGCLONERS: 3055 error = if_clone_list((struct if_clonereq *)data); 3056 goto out_noref; 3057 3058 CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB): 3059 error = if_getgroupmembers((struct ifgroupreq *)data); 3060 goto out_noref; 3061 3062 #if defined(INET) || defined(INET6) 3063 case SIOCSVH: 3064 case SIOCGVH: 3065 if (carp_ioctl_p == NULL) 3066 error = EPROTONOSUPPORT; 3067 else 3068 error = (*carp_ioctl_p)(ifr, cmd, td); 3069 goto out_noref; 3070 #endif 3071 } 3072 3073 ifp = ifunit_ref(ifr->ifr_name); 3074 if (ifp == NULL) { 3075 error = ENXIO; 3076 goto out_noref; 3077 } 3078 3079 error = ifhwioctl(cmd, ifp, data, td); 3080 if (error != ENOIOCTL) 3081 goto out_ref; 3082 3083 oif_flags = ifp->if_flags; 3084 if (so->so_proto == NULL) { 3085 error = EOPNOTSUPP; 3086 goto out_ref; 3087 } 3088 3089 /* 3090 * Pass the request on to the socket control method, and if the 3091 * latter returns EOPNOTSUPP, directly to the interface. 3092 * 3093 * Make an exception for the legacy SIOCSIF* requests. Drivers 3094 * trust SIOCSIFADDR et al to come from an already privileged 3095 * layer, and do not perform any credentials checks or input 3096 * validation. 3097 */ 3098 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, 3099 ifp, td)); 3100 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL && 3101 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR && 3102 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK) 3103 error = (*ifp->if_ioctl)(ifp, cmd, data); 3104 3105 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 3106 #ifdef INET6 3107 if (ifp->if_flags & IFF_UP) 3108 in6_if_up(ifp); 3109 #endif 3110 } 3111 3112 out_ref: 3113 if_rele(ifp); 3114 out_noref: 3115 #ifdef COMPAT_FREEBSD32 3116 if (ifmrp != NULL) { 3117 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA), 3118 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx", 3119 cmd)); 3120 data = saved_data; 3121 ifmr_update(ifmrp, data); 3122 } 3123 #endif 3124 CURVNET_RESTORE(); 3125 return (error); 3126 } 3127 3128 /* 3129 * The code common to handling reference counted flags, 3130 * e.g., in ifpromisc() and if_allmulti(). 3131 * The "pflag" argument can specify a permanent mode flag to check, 3132 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 3133 * 3134 * Only to be used on stack-owned flags, not driver-owned flags. 3135 */ 3136 static int 3137 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 3138 { 3139 struct ifreq ifr; 3140 int error; 3141 int oldflags, oldcount; 3142 3143 /* Sanity checks to catch programming errors */ 3144 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 3145 ("%s: setting driver-owned flag %d", __func__, flag)); 3146 3147 if (onswitch) 3148 KASSERT(*refcount >= 0, 3149 ("%s: increment negative refcount %d for flag %d", 3150 __func__, *refcount, flag)); 3151 else 3152 KASSERT(*refcount > 0, 3153 ("%s: decrement non-positive refcount %d for flag %d", 3154 __func__, *refcount, flag)); 3155 3156 /* In case this mode is permanent, just touch refcount */ 3157 if (ifp->if_flags & pflag) { 3158 *refcount += onswitch ? 1 : -1; 3159 return (0); 3160 } 3161 3162 /* Save ifnet parameters for if_ioctl() may fail */ 3163 oldcount = *refcount; 3164 oldflags = ifp->if_flags; 3165 3166 /* 3167 * See if we aren't the only and touching refcount is enough. 3168 * Actually toggle interface flag if we are the first or last. 3169 */ 3170 if (onswitch) { 3171 if ((*refcount)++) 3172 return (0); 3173 ifp->if_flags |= flag; 3174 } else { 3175 if (--(*refcount)) 3176 return (0); 3177 ifp->if_flags &= ~flag; 3178 } 3179 3180 /* Call down the driver since we've changed interface flags */ 3181 if (ifp->if_ioctl == NULL) { 3182 error = EOPNOTSUPP; 3183 goto recover; 3184 } 3185 ifr.ifr_flags = ifp->if_flags & 0xffff; 3186 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3187 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3188 if (error) 3189 goto recover; 3190 /* Notify userland that interface flags have changed */ 3191 rt_ifmsg(ifp); 3192 return (0); 3193 3194 recover: 3195 /* Recover after driver error */ 3196 *refcount = oldcount; 3197 ifp->if_flags = oldflags; 3198 return (error); 3199 } 3200 3201 /* 3202 * Set/clear promiscuous mode on interface ifp based on the truth value 3203 * of pswitch. The calls are reference counted so that only the first 3204 * "on" request actually has an effect, as does the final "off" request. 3205 * Results are undefined if the "off" and "on" requests are not matched. 3206 */ 3207 int 3208 ifpromisc(struct ifnet *ifp, int pswitch) 3209 { 3210 int error; 3211 int oldflags = ifp->if_flags; 3212 3213 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 3214 &ifp->if_pcount, pswitch); 3215 /* If promiscuous mode status has changed, log a message */ 3216 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) && 3217 log_promisc_mode_change) 3218 if_printf(ifp, "promiscuous mode %s\n", 3219 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 3220 return (error); 3221 } 3222 3223 /* 3224 * Return interface configuration 3225 * of system. List may be used 3226 * in later ioctl's (above) to get 3227 * other information. 3228 */ 3229 /*ARGSUSED*/ 3230 static int 3231 ifconf(u_long cmd, caddr_t data) 3232 { 3233 struct ifconf *ifc = (struct ifconf *)data; 3234 struct ifnet *ifp; 3235 struct ifaddr *ifa; 3236 struct ifreq ifr; 3237 struct sbuf *sb; 3238 int error, full = 0, valid_len, max_len; 3239 3240 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 3241 max_len = MAXPHYS - 1; 3242 3243 /* Prevent hostile input from being able to crash the system */ 3244 if (ifc->ifc_len <= 0) 3245 return (EINVAL); 3246 3247 again: 3248 if (ifc->ifc_len <= max_len) { 3249 max_len = ifc->ifc_len; 3250 full = 1; 3251 } 3252 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 3253 max_len = 0; 3254 valid_len = 0; 3255 3256 IFNET_RLOCK(); 3257 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 3258 int addrs; 3259 3260 /* 3261 * Zero the ifr to make sure we don't disclose the contents 3262 * of the stack. 3263 */ 3264 memset(&ifr, 0, sizeof(ifr)); 3265 3266 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 3267 >= sizeof(ifr.ifr_name)) { 3268 sbuf_delete(sb); 3269 IFNET_RUNLOCK(); 3270 return (ENAMETOOLONG); 3271 } 3272 3273 addrs = 0; 3274 IF_ADDR_RLOCK(ifp); 3275 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 3276 struct sockaddr *sa = ifa->ifa_addr; 3277 3278 if (prison_if(curthread->td_ucred, sa) != 0) 3279 continue; 3280 addrs++; 3281 if (sa->sa_len <= sizeof(*sa)) { 3282 if (sa->sa_len < sizeof(*sa)) { 3283 memset(&ifr.ifr_ifru.ifru_addr, 0, 3284 sizeof(ifr.ifr_ifru.ifru_addr)); 3285 memcpy(&ifr.ifr_ifru.ifru_addr, sa, 3286 sa->sa_len); 3287 } else 3288 ifr.ifr_ifru.ifru_addr = *sa; 3289 sbuf_bcat(sb, &ifr, sizeof(ifr)); 3290 max_len += sizeof(ifr); 3291 } else { 3292 sbuf_bcat(sb, &ifr, 3293 offsetof(struct ifreq, ifr_addr)); 3294 max_len += offsetof(struct ifreq, ifr_addr); 3295 sbuf_bcat(sb, sa, sa->sa_len); 3296 max_len += sa->sa_len; 3297 } 3298 3299 if (sbuf_error(sb) == 0) 3300 valid_len = sbuf_len(sb); 3301 } 3302 IF_ADDR_RUNLOCK(ifp); 3303 if (addrs == 0) { 3304 sbuf_bcat(sb, &ifr, sizeof(ifr)); 3305 max_len += sizeof(ifr); 3306 3307 if (sbuf_error(sb) == 0) 3308 valid_len = sbuf_len(sb); 3309 } 3310 } 3311 IFNET_RUNLOCK(); 3312 3313 /* 3314 * If we didn't allocate enough space (uncommon), try again. If 3315 * we have already allocated as much space as we are allowed, 3316 * return what we've got. 3317 */ 3318 if (valid_len != max_len && !full) { 3319 sbuf_delete(sb); 3320 goto again; 3321 } 3322 3323 ifc->ifc_len = valid_len; 3324 sbuf_finish(sb); 3325 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 3326 sbuf_delete(sb); 3327 return (error); 3328 } 3329 3330 /* 3331 * Just like ifpromisc(), but for all-multicast-reception mode. 3332 */ 3333 int 3334 if_allmulti(struct ifnet *ifp, int onswitch) 3335 { 3336 3337 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 3338 } 3339 3340 struct ifmultiaddr * 3341 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa) 3342 { 3343 struct ifmultiaddr *ifma; 3344 3345 IF_ADDR_LOCK_ASSERT(ifp); 3346 3347 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 3348 if (sa->sa_family == AF_LINK) { 3349 if (sa_dl_equal(ifma->ifma_addr, sa)) 3350 break; 3351 } else { 3352 if (sa_equal(ifma->ifma_addr, sa)) 3353 break; 3354 } 3355 } 3356 3357 return ifma; 3358 } 3359 3360 /* 3361 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 3362 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 3363 * the ifnet multicast address list here, so the caller must do that and 3364 * other setup work (such as notifying the device driver). The reference 3365 * count is initialized to 1. 3366 */ 3367 static struct ifmultiaddr * 3368 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 3369 int mflags) 3370 { 3371 struct ifmultiaddr *ifma; 3372 struct sockaddr *dupsa; 3373 3374 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags | 3375 M_ZERO); 3376 if (ifma == NULL) 3377 return (NULL); 3378 3379 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags); 3380 if (dupsa == NULL) { 3381 free(ifma, M_IFMADDR); 3382 return (NULL); 3383 } 3384 bcopy(sa, dupsa, sa->sa_len); 3385 ifma->ifma_addr = dupsa; 3386 3387 ifma->ifma_ifp = ifp; 3388 ifma->ifma_refcount = 1; 3389 ifma->ifma_protospec = NULL; 3390 3391 if (llsa == NULL) { 3392 ifma->ifma_lladdr = NULL; 3393 return (ifma); 3394 } 3395 3396 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags); 3397 if (dupsa == NULL) { 3398 free(ifma->ifma_addr, M_IFMADDR); 3399 free(ifma, M_IFMADDR); 3400 return (NULL); 3401 } 3402 bcopy(llsa, dupsa, llsa->sa_len); 3403 ifma->ifma_lladdr = dupsa; 3404 3405 return (ifma); 3406 } 3407 3408 /* 3409 * if_freemulti: free ifmultiaddr structure and possibly attached related 3410 * addresses. The caller is responsible for implementing reference 3411 * counting, notifying the driver, handling routing messages, and releasing 3412 * any dependent link layer state. 3413 */ 3414 #ifdef MCAST_VERBOSE 3415 extern void kdb_backtrace(void); 3416 #endif 3417 static void 3418 if_freemulti_internal(struct ifmultiaddr *ifma) 3419 { 3420 3421 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 3422 ifma->ifma_refcount)); 3423 3424 if (ifma->ifma_lladdr != NULL) 3425 free(ifma->ifma_lladdr, M_IFMADDR); 3426 #ifdef MCAST_VERBOSE 3427 kdb_backtrace(); 3428 printf("%s freeing ifma: %p\n", __func__, ifma); 3429 #endif 3430 free(ifma->ifma_addr, M_IFMADDR); 3431 free(ifma, M_IFMADDR); 3432 } 3433 3434 static void 3435 if_destroymulti(epoch_context_t ctx) 3436 { 3437 struct ifmultiaddr *ifma; 3438 3439 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx); 3440 if_freemulti_internal(ifma); 3441 } 3442 3443 void 3444 if_freemulti(struct ifmultiaddr *ifma) 3445 { 3446 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d", 3447 ifma->ifma_refcount)); 3448 3449 epoch_call(net_epoch_preempt, &ifma->ifma_epoch_ctx, if_destroymulti); 3450 } 3451 3452 3453 /* 3454 * Register an additional multicast address with a network interface. 3455 * 3456 * - If the address is already present, bump the reference count on the 3457 * address and return. 3458 * - If the address is not link-layer, look up a link layer address. 3459 * - Allocate address structures for one or both addresses, and attach to the 3460 * multicast address list on the interface. If automatically adding a link 3461 * layer address, the protocol address will own a reference to the link 3462 * layer address, to be freed when it is freed. 3463 * - Notify the network device driver of an addition to the multicast address 3464 * list. 3465 * 3466 * 'sa' points to caller-owned memory with the desired multicast address. 3467 * 3468 * 'retifma' will be used to return a pointer to the resulting multicast 3469 * address reference, if desired. 3470 */ 3471 int 3472 if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 3473 struct ifmultiaddr **retifma) 3474 { 3475 struct ifmultiaddr *ifma, *ll_ifma; 3476 struct sockaddr *llsa; 3477 struct sockaddr_dl sdl; 3478 int error; 3479 3480 #ifdef INET 3481 IN_MULTI_LIST_UNLOCK_ASSERT(); 3482 #endif 3483 #ifdef INET6 3484 IN6_MULTI_LIST_UNLOCK_ASSERT(); 3485 #endif 3486 /* 3487 * If the address is already present, return a new reference to it; 3488 * otherwise, allocate storage and set up a new address. 3489 */ 3490 IF_ADDR_WLOCK(ifp); 3491 ifma = if_findmulti(ifp, sa); 3492 if (ifma != NULL) { 3493 ifma->ifma_refcount++; 3494 if (retifma != NULL) 3495 *retifma = ifma; 3496 IF_ADDR_WUNLOCK(ifp); 3497 return (0); 3498 } 3499 3500 /* 3501 * The address isn't already present; resolve the protocol address 3502 * into a link layer address, and then look that up, bump its 3503 * refcount or allocate an ifma for that also. 3504 * Most link layer resolving functions returns address data which 3505 * fits inside default sockaddr_dl structure. However callback 3506 * can allocate another sockaddr structure, in that case we need to 3507 * free it later. 3508 */ 3509 llsa = NULL; 3510 ll_ifma = NULL; 3511 if (ifp->if_resolvemulti != NULL) { 3512 /* Provide called function with buffer size information */ 3513 sdl.sdl_len = sizeof(sdl); 3514 llsa = (struct sockaddr *)&sdl; 3515 error = ifp->if_resolvemulti(ifp, &llsa, sa); 3516 if (error) 3517 goto unlock_out; 3518 } 3519 3520 /* 3521 * Allocate the new address. Don't hook it up yet, as we may also 3522 * need to allocate a link layer multicast address. 3523 */ 3524 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 3525 if (ifma == NULL) { 3526 error = ENOMEM; 3527 goto free_llsa_out; 3528 } 3529 3530 /* 3531 * If a link layer address is found, we'll need to see if it's 3532 * already present in the address list, or allocate is as well. 3533 * When this block finishes, the link layer address will be on the 3534 * list. 3535 */ 3536 if (llsa != NULL) { 3537 ll_ifma = if_findmulti(ifp, llsa); 3538 if (ll_ifma == NULL) { 3539 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 3540 if (ll_ifma == NULL) { 3541 --ifma->ifma_refcount; 3542 if_freemulti(ifma); 3543 error = ENOMEM; 3544 goto free_llsa_out; 3545 } 3546 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED; 3547 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 3548 ifma_link); 3549 } else 3550 ll_ifma->ifma_refcount++; 3551 ifma->ifma_llifma = ll_ifma; 3552 } 3553 3554 /* 3555 * We now have a new multicast address, ifma, and possibly a new or 3556 * referenced link layer address. Add the primary address to the 3557 * ifnet address list. 3558 */ 3559 ifma->ifma_flags |= IFMA_F_ENQUEUED; 3560 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 3561 3562 if (retifma != NULL) 3563 *retifma = ifma; 3564 3565 /* 3566 * Must generate the message while holding the lock so that 'ifma' 3567 * pointer is still valid. 3568 */ 3569 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 3570 IF_ADDR_WUNLOCK(ifp); 3571 3572 /* 3573 * We are certain we have added something, so call down to the 3574 * interface to let them know about it. 3575 */ 3576 if (ifp->if_ioctl != NULL) { 3577 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 3578 } 3579 3580 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl)) 3581 link_free_sdl(llsa); 3582 3583 return (0); 3584 3585 free_llsa_out: 3586 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl)) 3587 link_free_sdl(llsa); 3588 3589 unlock_out: 3590 IF_ADDR_WUNLOCK(ifp); 3591 return (error); 3592 } 3593 3594 /* 3595 * Delete a multicast group membership by network-layer group address. 3596 * 3597 * Returns ENOENT if the entry could not be found. If ifp no longer 3598 * exists, results are undefined. This entry point should only be used 3599 * from subsystems which do appropriate locking to hold ifp for the 3600 * duration of the call. 3601 * Network-layer protocol domains must use if_delmulti_ifma(). 3602 */ 3603 int 3604 if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 3605 { 3606 struct ifmultiaddr *ifma; 3607 int lastref; 3608 #ifdef INVARIANTS 3609 struct ifnet *oifp; 3610 3611 IFNET_RLOCK_NOSLEEP(); 3612 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link) 3613 if (ifp == oifp) 3614 break; 3615 if (ifp != oifp) 3616 ifp = NULL; 3617 IFNET_RUNLOCK_NOSLEEP(); 3618 3619 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__)); 3620 #endif 3621 if (ifp == NULL) 3622 return (ENOENT); 3623 3624 IF_ADDR_WLOCK(ifp); 3625 lastref = 0; 3626 ifma = if_findmulti(ifp, sa); 3627 if (ifma != NULL) 3628 lastref = if_delmulti_locked(ifp, ifma, 0); 3629 IF_ADDR_WUNLOCK(ifp); 3630 3631 if (ifma == NULL) 3632 return (ENOENT); 3633 3634 if (lastref && ifp->if_ioctl != NULL) { 3635 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3636 } 3637 3638 return (0); 3639 } 3640 3641 /* 3642 * Delete all multicast group membership for an interface. 3643 * Should be used to quickly flush all multicast filters. 3644 */ 3645 void 3646 if_delallmulti(struct ifnet *ifp) 3647 { 3648 struct ifmultiaddr *ifma; 3649 struct ifmultiaddr *next; 3650 3651 IF_ADDR_WLOCK(ifp); 3652 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 3653 if_delmulti_locked(ifp, ifma, 0); 3654 IF_ADDR_WUNLOCK(ifp); 3655 } 3656 3657 void 3658 if_delmulti_ifma(struct ifmultiaddr *ifma) 3659 { 3660 if_delmulti_ifma_flags(ifma, 0); 3661 } 3662 3663 /* 3664 * Delete a multicast group membership by group membership pointer. 3665 * Network-layer protocol domains must use this routine. 3666 * 3667 * It is safe to call this routine if the ifp disappeared. 3668 */ 3669 void 3670 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags) 3671 { 3672 struct ifnet *ifp; 3673 int lastref; 3674 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma); 3675 #ifdef INET 3676 IN_MULTI_LIST_UNLOCK_ASSERT(); 3677 #endif 3678 ifp = ifma->ifma_ifp; 3679 #ifdef DIAGNOSTIC 3680 if (ifp == NULL) { 3681 printf("%s: ifma_ifp seems to be detached\n", __func__); 3682 } else { 3683 struct ifnet *oifp; 3684 3685 IFNET_RLOCK_NOSLEEP(); 3686 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link) 3687 if (ifp == oifp) 3688 break; 3689 if (ifp != oifp) 3690 ifp = NULL; 3691 IFNET_RUNLOCK_NOSLEEP(); 3692 } 3693 #endif 3694 /* 3695 * If and only if the ifnet instance exists: Acquire the address lock. 3696 */ 3697 if (ifp != NULL) 3698 IF_ADDR_WLOCK(ifp); 3699 3700 lastref = if_delmulti_locked(ifp, ifma, flags); 3701 3702 if (ifp != NULL) { 3703 /* 3704 * If and only if the ifnet instance exists: 3705 * Release the address lock. 3706 * If the group was left: update the hardware hash filter. 3707 */ 3708 IF_ADDR_WUNLOCK(ifp); 3709 if (lastref && ifp->if_ioctl != NULL) { 3710 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3711 } 3712 } 3713 } 3714 3715 /* 3716 * Perform deletion of network-layer and/or link-layer multicast address. 3717 * 3718 * Return 0 if the reference count was decremented. 3719 * Return 1 if the final reference was released, indicating that the 3720 * hardware hash filter should be reprogrammed. 3721 */ 3722 static int 3723 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 3724 { 3725 struct ifmultiaddr *ll_ifma; 3726 3727 if (ifp != NULL && ifma->ifma_ifp != NULL) { 3728 KASSERT(ifma->ifma_ifp == ifp, 3729 ("%s: inconsistent ifp %p", __func__, ifp)); 3730 IF_ADDR_WLOCK_ASSERT(ifp); 3731 } 3732 3733 ifp = ifma->ifma_ifp; 3734 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : ""); 3735 3736 /* 3737 * If the ifnet is detaching, null out references to ifnet, 3738 * so that upper protocol layers will notice, and not attempt 3739 * to obtain locks for an ifnet which no longer exists. The 3740 * routing socket announcement must happen before the ifnet 3741 * instance is detached from the system. 3742 */ 3743 if (detaching) { 3744 #ifdef DIAGNOSTIC 3745 printf("%s: detaching ifnet instance %p\n", __func__, ifp); 3746 #endif 3747 /* 3748 * ifp may already be nulled out if we are being reentered 3749 * to delete the ll_ifma. 3750 */ 3751 if (ifp != NULL) { 3752 rt_newmaddrmsg(RTM_DELMADDR, ifma); 3753 ifma->ifma_ifp = NULL; 3754 } 3755 } 3756 3757 if (--ifma->ifma_refcount > 0) 3758 return 0; 3759 3760 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) { 3761 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link); 3762 ifma->ifma_flags &= ~IFMA_F_ENQUEUED; 3763 } 3764 /* 3765 * If this ifma is a network-layer ifma, a link-layer ifma may 3766 * have been associated with it. Release it first if so. 3767 */ 3768 ll_ifma = ifma->ifma_llifma; 3769 if (ll_ifma != NULL) { 3770 KASSERT(ifma->ifma_lladdr != NULL, 3771 ("%s: llifma w/o lladdr", __func__)); 3772 if (detaching) 3773 ll_ifma->ifma_ifp = NULL; /* XXX */ 3774 if (--ll_ifma->ifma_refcount == 0) { 3775 if (ifp != NULL) { 3776 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) { 3777 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, 3778 ifma_link); 3779 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED; 3780 } 3781 } 3782 if_freemulti(ll_ifma); 3783 } 3784 } 3785 #ifdef INVARIANTS 3786 if (ifp) { 3787 struct ifmultiaddr *ifmatmp; 3788 3789 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link) 3790 MPASS(ifma != ifmatmp); 3791 } 3792 #endif 3793 if_freemulti(ifma); 3794 /* 3795 * The last reference to this instance of struct ifmultiaddr 3796 * was released; the hardware should be notified of this change. 3797 */ 3798 return 1; 3799 } 3800 3801 /* 3802 * Set the link layer address on an interface. 3803 * 3804 * At this time we only support certain types of interfaces, 3805 * and we don't allow the length of the address to change. 3806 * 3807 * Set noinline to be dtrace-friendly 3808 */ 3809 __noinline int 3810 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 3811 { 3812 struct sockaddr_dl *sdl; 3813 struct ifaddr *ifa; 3814 struct ifreq ifr; 3815 int rc; 3816 3817 rc = 0; 3818 NET_EPOCH_ENTER(); 3819 ifa = ifp->if_addr; 3820 if (ifa == NULL) { 3821 rc = EINVAL; 3822 goto out; 3823 } 3824 3825 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 3826 if (sdl == NULL) { 3827 rc = EINVAL; 3828 goto out; 3829 } 3830 if (len != sdl->sdl_alen) { /* don't allow length to change */ 3831 rc = EINVAL; 3832 goto out; 3833 } 3834 switch (ifp->if_type) { 3835 case IFT_ETHER: 3836 case IFT_XETHER: 3837 case IFT_L2VLAN: 3838 case IFT_BRIDGE: 3839 case IFT_IEEE8023ADLAG: 3840 bcopy(lladdr, LLADDR(sdl), len); 3841 break; 3842 default: 3843 rc = ENODEV; 3844 goto out; 3845 } 3846 3847 /* 3848 * If the interface is already up, we need 3849 * to re-init it in order to reprogram its 3850 * address filter. 3851 */ 3852 NET_EPOCH_EXIT(); 3853 if ((ifp->if_flags & IFF_UP) != 0) { 3854 if (ifp->if_ioctl) { 3855 ifp->if_flags &= ~IFF_UP; 3856 ifr.ifr_flags = ifp->if_flags & 0xffff; 3857 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3858 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3859 ifp->if_flags |= IFF_UP; 3860 ifr.ifr_flags = ifp->if_flags & 0xffff; 3861 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3862 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3863 } 3864 } 3865 EVENTHANDLER_INVOKE(iflladdr_event, ifp); 3866 return (0); 3867 out: 3868 NET_EPOCH_EXIT(); 3869 return (rc); 3870 } 3871 3872 /* 3873 * Compat function for handling basic encapsulation requests. 3874 * Not converted stacks (FDDI, IB, ..) supports traditional 3875 * output model: ARP (and other similar L2 protocols) are handled 3876 * inside output routine, arpresolve/nd6_resolve() returns MAC 3877 * address instead of full prepend. 3878 * 3879 * This function creates calculated header==MAC for IPv4/IPv6 and 3880 * returns EAFNOSUPPORT (which is then handled in ARP code) for other 3881 * address families. 3882 */ 3883 static int 3884 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req) 3885 { 3886 3887 if (req->rtype != IFENCAP_LL) 3888 return (EOPNOTSUPP); 3889 3890 if (req->bufsize < req->lladdr_len) 3891 return (ENOMEM); 3892 3893 switch (req->family) { 3894 case AF_INET: 3895 case AF_INET6: 3896 break; 3897 default: 3898 return (EAFNOSUPPORT); 3899 } 3900 3901 /* Copy lladdr to storage as is */ 3902 memmove(req->buf, req->lladdr, req->lladdr_len); 3903 req->bufsize = req->lladdr_len; 3904 req->lladdr_off = 0; 3905 3906 return (0); 3907 } 3908 3909 /* 3910 * Tunnel interfaces can nest, also they may cause infinite recursion 3911 * calls when misconfigured. We'll prevent this by detecting loops. 3912 * High nesting level may cause stack exhaustion. We'll prevent this 3913 * by introducing upper limit. 3914 * 3915 * Return 0, if tunnel nesting count is equal or less than limit. 3916 */ 3917 int 3918 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie, 3919 int limit) 3920 { 3921 struct m_tag *mtag; 3922 int count; 3923 3924 count = 1; 3925 mtag = NULL; 3926 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) { 3927 if (*(struct ifnet **)(mtag + 1) == ifp) { 3928 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp)); 3929 return (EIO); 3930 } 3931 count++; 3932 } 3933 if (count > limit) { 3934 log(LOG_NOTICE, 3935 "%s: if_output recursively called too many times(%d)\n", 3936 if_name(ifp), count); 3937 return (EIO); 3938 } 3939 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT); 3940 if (mtag == NULL) 3941 return (ENOMEM); 3942 *(struct ifnet **)(mtag + 1) = ifp; 3943 m_tag_prepend(m, mtag); 3944 return (0); 3945 } 3946 3947 /* 3948 * Get the link layer address that was read from the hardware at attach. 3949 * 3950 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type 3951 * their component interfaces as IFT_IEEE8023ADLAG. 3952 */ 3953 int 3954 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr) 3955 { 3956 3957 if (ifp->if_hw_addr == NULL) 3958 return (ENODEV); 3959 3960 switch (ifp->if_type) { 3961 case IFT_ETHER: 3962 case IFT_IEEE8023ADLAG: 3963 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen); 3964 return (0); 3965 default: 3966 return (ENODEV); 3967 } 3968 } 3969 3970 /* 3971 * The name argument must be a pointer to storage which will last as 3972 * long as the interface does. For physical devices, the result of 3973 * device_get_name(dev) is a good choice and for pseudo-devices a 3974 * static string works well. 3975 */ 3976 void 3977 if_initname(struct ifnet *ifp, const char *name, int unit) 3978 { 3979 ifp->if_dname = name; 3980 ifp->if_dunit = unit; 3981 if (unit != IF_DUNIT_NONE) 3982 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 3983 else 3984 strlcpy(ifp->if_xname, name, IFNAMSIZ); 3985 } 3986 3987 int 3988 if_printf(struct ifnet *ifp, const char *fmt, ...) 3989 { 3990 char if_fmt[256]; 3991 va_list ap; 3992 3993 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt); 3994 va_start(ap, fmt); 3995 vlog(LOG_INFO, if_fmt, ap); 3996 va_end(ap); 3997 return (0); 3998 } 3999 4000 void 4001 if_start(struct ifnet *ifp) 4002 { 4003 4004 (*(ifp)->if_start)(ifp); 4005 } 4006 4007 /* 4008 * Backwards compatibility interface for drivers 4009 * that have not implemented it 4010 */ 4011 static int 4012 if_transmit(struct ifnet *ifp, struct mbuf *m) 4013 { 4014 int error; 4015 4016 IFQ_HANDOFF(ifp, m, error); 4017 return (error); 4018 } 4019 4020 static void 4021 if_input_default(struct ifnet *ifp __unused, struct mbuf *m) 4022 { 4023 4024 m_freem(m); 4025 } 4026 4027 int 4028 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 4029 { 4030 int active = 0; 4031 4032 IF_LOCK(ifq); 4033 if (_IF_QFULL(ifq)) { 4034 IF_UNLOCK(ifq); 4035 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); 4036 m_freem(m); 4037 return (0); 4038 } 4039 if (ifp != NULL) { 4040 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust); 4041 if (m->m_flags & (M_BCAST|M_MCAST)) 4042 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 4043 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 4044 } 4045 _IF_ENQUEUE(ifq, m); 4046 IF_UNLOCK(ifq); 4047 if (ifp != NULL && !active) 4048 (*(ifp)->if_start)(ifp); 4049 return (1); 4050 } 4051 4052 void 4053 if_register_com_alloc(u_char type, 4054 if_com_alloc_t *a, if_com_free_t *f) 4055 { 4056 4057 KASSERT(if_com_alloc[type] == NULL, 4058 ("if_register_com_alloc: %d already registered", type)); 4059 KASSERT(if_com_free[type] == NULL, 4060 ("if_register_com_alloc: %d free already registered", type)); 4061 4062 if_com_alloc[type] = a; 4063 if_com_free[type] = f; 4064 } 4065 4066 void 4067 if_deregister_com_alloc(u_char type) 4068 { 4069 4070 KASSERT(if_com_alloc[type] != NULL, 4071 ("if_deregister_com_alloc: %d not registered", type)); 4072 KASSERT(if_com_free[type] != NULL, 4073 ("if_deregister_com_alloc: %d free not registered", type)); 4074 if_com_alloc[type] = NULL; 4075 if_com_free[type] = NULL; 4076 } 4077 4078 /* API for driver access to network stack owned ifnet.*/ 4079 uint64_t 4080 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate) 4081 { 4082 uint64_t oldbrate; 4083 4084 oldbrate = ifp->if_baudrate; 4085 ifp->if_baudrate = baudrate; 4086 return (oldbrate); 4087 } 4088 4089 uint64_t 4090 if_getbaudrate(if_t ifp) 4091 { 4092 4093 return (((struct ifnet *)ifp)->if_baudrate); 4094 } 4095 4096 int 4097 if_setcapabilities(if_t ifp, int capabilities) 4098 { 4099 ((struct ifnet *)ifp)->if_capabilities = capabilities; 4100 return (0); 4101 } 4102 4103 int 4104 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit) 4105 { 4106 ((struct ifnet *)ifp)->if_capabilities |= setbit; 4107 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit; 4108 4109 return (0); 4110 } 4111 4112 int 4113 if_getcapabilities(if_t ifp) 4114 { 4115 return ((struct ifnet *)ifp)->if_capabilities; 4116 } 4117 4118 int 4119 if_setcapenable(if_t ifp, int capabilities) 4120 { 4121 ((struct ifnet *)ifp)->if_capenable = capabilities; 4122 return (0); 4123 } 4124 4125 int 4126 if_setcapenablebit(if_t ifp, int setcap, int clearcap) 4127 { 4128 if(setcap) 4129 ((struct ifnet *)ifp)->if_capenable |= setcap; 4130 if(clearcap) 4131 ((struct ifnet *)ifp)->if_capenable &= ~clearcap; 4132 4133 return (0); 4134 } 4135 4136 const char * 4137 if_getdname(if_t ifp) 4138 { 4139 return ((struct ifnet *)ifp)->if_dname; 4140 } 4141 4142 int 4143 if_togglecapenable(if_t ifp, int togglecap) 4144 { 4145 ((struct ifnet *)ifp)->if_capenable ^= togglecap; 4146 return (0); 4147 } 4148 4149 int 4150 if_getcapenable(if_t ifp) 4151 { 4152 return ((struct ifnet *)ifp)->if_capenable; 4153 } 4154 4155 /* 4156 * This is largely undesirable because it ties ifnet to a device, but does 4157 * provide flexiblity for an embedded product vendor. Should be used with 4158 * the understanding that it violates the interface boundaries, and should be 4159 * a last resort only. 4160 */ 4161 int 4162 if_setdev(if_t ifp, void *dev) 4163 { 4164 return (0); 4165 } 4166 4167 int 4168 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags) 4169 { 4170 ((struct ifnet *)ifp)->if_drv_flags |= set_flags; 4171 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags; 4172 4173 return (0); 4174 } 4175 4176 int 4177 if_getdrvflags(if_t ifp) 4178 { 4179 return ((struct ifnet *)ifp)->if_drv_flags; 4180 } 4181 4182 int 4183 if_setdrvflags(if_t ifp, int flags) 4184 { 4185 ((struct ifnet *)ifp)->if_drv_flags = flags; 4186 return (0); 4187 } 4188 4189 4190 int 4191 if_setflags(if_t ifp, int flags) 4192 { 4193 ((struct ifnet *)ifp)->if_flags = flags; 4194 return (0); 4195 } 4196 4197 int 4198 if_setflagbits(if_t ifp, int set, int clear) 4199 { 4200 ((struct ifnet *)ifp)->if_flags |= set; 4201 ((struct ifnet *)ifp)->if_flags &= ~clear; 4202 4203 return (0); 4204 } 4205 4206 int 4207 if_getflags(if_t ifp) 4208 { 4209 return ((struct ifnet *)ifp)->if_flags; 4210 } 4211 4212 int 4213 if_clearhwassist(if_t ifp) 4214 { 4215 ((struct ifnet *)ifp)->if_hwassist = 0; 4216 return (0); 4217 } 4218 4219 int 4220 if_sethwassistbits(if_t ifp, int toset, int toclear) 4221 { 4222 ((struct ifnet *)ifp)->if_hwassist |= toset; 4223 ((struct ifnet *)ifp)->if_hwassist &= ~toclear; 4224 4225 return (0); 4226 } 4227 4228 int 4229 if_sethwassist(if_t ifp, int hwassist_bit) 4230 { 4231 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit; 4232 return (0); 4233 } 4234 4235 int 4236 if_gethwassist(if_t ifp) 4237 { 4238 return ((struct ifnet *)ifp)->if_hwassist; 4239 } 4240 4241 int 4242 if_setmtu(if_t ifp, int mtu) 4243 { 4244 ((struct ifnet *)ifp)->if_mtu = mtu; 4245 return (0); 4246 } 4247 4248 int 4249 if_getmtu(if_t ifp) 4250 { 4251 return ((struct ifnet *)ifp)->if_mtu; 4252 } 4253 4254 int 4255 if_getmtu_family(if_t ifp, int family) 4256 { 4257 struct domain *dp; 4258 4259 for (dp = domains; dp; dp = dp->dom_next) { 4260 if (dp->dom_family == family && dp->dom_ifmtu != NULL) 4261 return (dp->dom_ifmtu((struct ifnet *)ifp)); 4262 } 4263 4264 return (((struct ifnet *)ifp)->if_mtu); 4265 } 4266 4267 int 4268 if_setsoftc(if_t ifp, void *softc) 4269 { 4270 ((struct ifnet *)ifp)->if_softc = softc; 4271 return (0); 4272 } 4273 4274 void * 4275 if_getsoftc(if_t ifp) 4276 { 4277 return ((struct ifnet *)ifp)->if_softc; 4278 } 4279 4280 void 4281 if_setrcvif(struct mbuf *m, if_t ifp) 4282 { 4283 m->m_pkthdr.rcvif = (struct ifnet *)ifp; 4284 } 4285 4286 void 4287 if_setvtag(struct mbuf *m, uint16_t tag) 4288 { 4289 m->m_pkthdr.ether_vtag = tag; 4290 } 4291 4292 uint16_t 4293 if_getvtag(struct mbuf *m) 4294 { 4295 4296 return (m->m_pkthdr.ether_vtag); 4297 } 4298 4299 int 4300 if_sendq_empty(if_t ifp) 4301 { 4302 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd); 4303 } 4304 4305 struct ifaddr * 4306 if_getifaddr(if_t ifp) 4307 { 4308 return ((struct ifnet *)ifp)->if_addr; 4309 } 4310 4311 int 4312 if_getamcount(if_t ifp) 4313 { 4314 return ((struct ifnet *)ifp)->if_amcount; 4315 } 4316 4317 4318 int 4319 if_setsendqready(if_t ifp) 4320 { 4321 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd); 4322 return (0); 4323 } 4324 4325 int 4326 if_setsendqlen(if_t ifp, int tx_desc_count) 4327 { 4328 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count); 4329 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count; 4330 4331 return (0); 4332 } 4333 4334 int 4335 if_vlantrunkinuse(if_t ifp) 4336 { 4337 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0; 4338 } 4339 4340 int 4341 if_input(if_t ifp, struct mbuf* sendmp) 4342 { 4343 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp); 4344 return (0); 4345 4346 } 4347 4348 /* XXX */ 4349 #ifndef ETH_ADDR_LEN 4350 #define ETH_ADDR_LEN 6 4351 #endif 4352 4353 int 4354 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max) 4355 { 4356 struct ifmultiaddr *ifma; 4357 uint8_t *lmta = (uint8_t *)mta; 4358 int mcnt = 0; 4359 4360 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) { 4361 if (ifma->ifma_addr->sa_family != AF_LINK) 4362 continue; 4363 4364 if (mcnt == max) 4365 break; 4366 4367 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 4368 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN); 4369 mcnt++; 4370 } 4371 *cnt = mcnt; 4372 4373 return (0); 4374 } 4375 4376 int 4377 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max) 4378 { 4379 int error; 4380 4381 if_maddr_rlock(ifp); 4382 error = if_setupmultiaddr(ifp, mta, cnt, max); 4383 if_maddr_runlock(ifp); 4384 return (error); 4385 } 4386 4387 int 4388 if_multiaddr_count(if_t ifp, int max) 4389 { 4390 struct ifmultiaddr *ifma; 4391 int count; 4392 4393 count = 0; 4394 if_maddr_rlock(ifp); 4395 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) { 4396 if (ifma->ifma_addr->sa_family != AF_LINK) 4397 continue; 4398 count++; 4399 if (count == max) 4400 break; 4401 } 4402 if_maddr_runlock(ifp); 4403 return (count); 4404 } 4405 4406 int 4407 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg) 4408 { 4409 struct ifmultiaddr *ifma; 4410 int cnt = 0; 4411 4412 if_maddr_rlock(ifp); 4413 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 4414 cnt += filter(arg, ifma, cnt); 4415 if_maddr_runlock(ifp); 4416 return (cnt); 4417 } 4418 4419 struct mbuf * 4420 if_dequeue(if_t ifp) 4421 { 4422 struct mbuf *m; 4423 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m); 4424 4425 return (m); 4426 } 4427 4428 int 4429 if_sendq_prepend(if_t ifp, struct mbuf *m) 4430 { 4431 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m); 4432 return (0); 4433 } 4434 4435 int 4436 if_setifheaderlen(if_t ifp, int len) 4437 { 4438 ((struct ifnet *)ifp)->if_hdrlen = len; 4439 return (0); 4440 } 4441 4442 caddr_t 4443 if_getlladdr(if_t ifp) 4444 { 4445 return (IF_LLADDR((struct ifnet *)ifp)); 4446 } 4447 4448 void * 4449 if_gethandle(u_char type) 4450 { 4451 return (if_alloc(type)); 4452 } 4453 4454 void 4455 if_bpfmtap(if_t ifh, struct mbuf *m) 4456 { 4457 struct ifnet *ifp = (struct ifnet *)ifh; 4458 4459 BPF_MTAP(ifp, m); 4460 } 4461 4462 void 4463 if_etherbpfmtap(if_t ifh, struct mbuf *m) 4464 { 4465 struct ifnet *ifp = (struct ifnet *)ifh; 4466 4467 ETHER_BPF_MTAP(ifp, m); 4468 } 4469 4470 void 4471 if_vlancap(if_t ifh) 4472 { 4473 struct ifnet *ifp = (struct ifnet *)ifh; 4474 VLAN_CAPABILITIES(ifp); 4475 } 4476 4477 int 4478 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax) 4479 { 4480 4481 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax; 4482 return (0); 4483 } 4484 4485 int 4486 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount) 4487 { 4488 4489 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount; 4490 return (0); 4491 } 4492 4493 int 4494 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize) 4495 { 4496 4497 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize; 4498 return (0); 4499 } 4500 4501 u_int 4502 if_gethwtsomax(if_t ifp) 4503 { 4504 4505 return (((struct ifnet *)ifp)->if_hw_tsomax); 4506 } 4507 4508 u_int 4509 if_gethwtsomaxsegcount(if_t ifp) 4510 { 4511 4512 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount); 4513 } 4514 4515 u_int 4516 if_gethwtsomaxsegsize(if_t ifp) 4517 { 4518 4519 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize); 4520 } 4521 4522 void 4523 if_setinitfn(if_t ifp, void (*init_fn)(void *)) 4524 { 4525 ((struct ifnet *)ifp)->if_init = init_fn; 4526 } 4527 4528 void 4529 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t)) 4530 { 4531 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn; 4532 } 4533 4534 void 4535 if_setstartfn(if_t ifp, void (*start_fn)(if_t)) 4536 { 4537 ((struct ifnet *)ifp)->if_start = (void *)start_fn; 4538 } 4539 4540 void 4541 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn) 4542 { 4543 ((struct ifnet *)ifp)->if_transmit = start_fn; 4544 } 4545 4546 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn) 4547 { 4548 ((struct ifnet *)ifp)->if_qflush = flush_fn; 4549 4550 } 4551 4552 void 4553 if_setgetcounterfn(if_t ifp, if_get_counter_t fn) 4554 { 4555 4556 ifp->if_get_counter = fn; 4557 } 4558 4559 /* Revisit these - These are inline functions originally. */ 4560 int 4561 drbr_inuse_drv(if_t ifh, struct buf_ring *br) 4562 { 4563 return drbr_inuse(ifh, br); 4564 } 4565 4566 struct mbuf* 4567 drbr_dequeue_drv(if_t ifh, struct buf_ring *br) 4568 { 4569 return drbr_dequeue(ifh, br); 4570 } 4571 4572 int 4573 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br) 4574 { 4575 return drbr_needs_enqueue(ifh, br); 4576 } 4577 4578 int 4579 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m) 4580 { 4581 return drbr_enqueue(ifh, br, m); 4582 4583 } 4584