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