1 /* 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)if.c 8.3 (Berkeley) 1/4/94 30 * $FreeBSD: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $ 31 */ 32 33 #include "opt_compat.h" 34 #include "opt_inet6.h" 35 #include "opt_inet.h" 36 #include "opt_ifpoll.h" 37 38 #include <sys/param.h> 39 #include <sys/malloc.h> 40 #include <sys/mbuf.h> 41 #include <sys/systm.h> 42 #include <sys/proc.h> 43 #include <sys/priv.h> 44 #include <sys/protosw.h> 45 #include <sys/socket.h> 46 #include <sys/socketvar.h> 47 #include <sys/socketops.h> 48 #include <sys/kernel.h> 49 #include <sys/ktr.h> 50 #include <sys/mutex.h> 51 #include <sys/sockio.h> 52 #include <sys/syslog.h> 53 #include <sys/sysctl.h> 54 #include <sys/domain.h> 55 #include <sys/thread.h> 56 #include <sys/serialize.h> 57 #include <sys/bus.h> 58 59 #include <sys/thread2.h> 60 #include <sys/msgport2.h> 61 #include <sys/mutex2.h> 62 63 #include <net/if.h> 64 #include <net/if_arp.h> 65 #include <net/if_dl.h> 66 #include <net/if_types.h> 67 #include <net/if_var.h> 68 #include <net/ifq_var.h> 69 #include <net/radix.h> 70 #include <net/route.h> 71 #include <net/if_clone.h> 72 #include <net/netisr2.h> 73 #include <net/netmsg2.h> 74 75 #include <machine/atomic.h> 76 #include <machine/stdarg.h> 77 #include <machine/smp.h> 78 79 #if defined(INET) || defined(INET6) 80 /*XXX*/ 81 #include <netinet/in.h> 82 #include <netinet/in_var.h> 83 #include <netinet/if_ether.h> 84 #ifdef INET6 85 #include <netinet6/in6_var.h> 86 #include <netinet6/in6_ifattach.h> 87 #endif 88 #endif 89 90 #if defined(COMPAT_43) 91 #include <emulation/43bsd/43bsd_socket.h> 92 #endif /* COMPAT_43 */ 93 94 struct netmsg_ifaddr { 95 struct netmsg_base base; 96 struct ifaddr *ifa; 97 struct ifnet *ifp; 98 int tail; 99 }; 100 101 struct ifsubq_stage_head { 102 TAILQ_HEAD(, ifsubq_stage) stg_head; 103 } __cachealign; 104 105 /* 106 * System initialization 107 */ 108 static void if_attachdomain(void *); 109 static void if_attachdomain1(struct ifnet *); 110 static int ifconf(u_long, caddr_t, struct ucred *); 111 static void ifinit(void *); 112 static void ifnetinit(void *); 113 static void if_slowtimo(void *); 114 static void link_rtrequest(int, struct rtentry *); 115 static int if_rtdel(struct radix_node *, void *); 116 static void if_slowtimo_dispatch(netmsg_t); 117 118 /* Helper functions */ 119 static void ifsq_watchdog_reset(struct ifsubq_watchdog *); 120 static int if_delmulti_serialized(struct ifnet *, struct sockaddr *); 121 static struct ifnet_array *ifnet_array_alloc(int); 122 static void ifnet_array_free(struct ifnet_array *); 123 static struct ifnet_array *ifnet_array_add(struct ifnet *, 124 const struct ifnet_array *); 125 static struct ifnet_array *ifnet_array_del(struct ifnet *, 126 const struct ifnet_array *); 127 128 #ifdef INET6 129 /* 130 * XXX: declare here to avoid to include many inet6 related files.. 131 * should be more generalized? 132 */ 133 extern void nd6_setmtu(struct ifnet *); 134 #endif 135 136 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 137 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 138 139 static int ifsq_stage_cntmax = 4; 140 TUNABLE_INT("net.link.stage_cntmax", &ifsq_stage_cntmax); 141 SYSCTL_INT(_net_link, OID_AUTO, stage_cntmax, CTLFLAG_RW, 142 &ifsq_stage_cntmax, 0, "ifq staging packet count max"); 143 144 static int if_stats_compat = 0; 145 SYSCTL_INT(_net_link, OID_AUTO, stats_compat, CTLFLAG_RW, 146 &if_stats_compat, 0, "Compat the old ifnet stats"); 147 148 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL) 149 /* Must be after netisr_init */ 150 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_SECOND, ifnetinit, NULL) 151 152 static if_com_alloc_t *if_com_alloc[256]; 153 static if_com_free_t *if_com_free[256]; 154 155 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 156 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 157 MALLOC_DEFINE(M_IFNET, "ifnet", "interface structure"); 158 159 int ifqmaxlen = IFQ_MAXLEN; 160 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet); 161 162 static struct ifnet_array ifnet_array0; 163 static struct ifnet_array *ifnet_array = &ifnet_array0; 164 165 static struct callout if_slowtimo_timer; 166 static struct netmsg_base if_slowtimo_netmsg; 167 168 int if_index = 0; 169 struct ifnet **ifindex2ifnet = NULL; 170 static struct thread ifnet_threads[MAXCPU]; 171 static struct mtx ifnet_mtx = MTX_INITIALIZER; 172 173 static struct ifsubq_stage_head ifsubq_stage_heads[MAXCPU]; 174 175 #ifdef notyet 176 #define IFQ_KTR_STRING "ifq=%p" 177 #define IFQ_KTR_ARGS struct ifaltq *ifq 178 #ifndef KTR_IFQ 179 #define KTR_IFQ KTR_ALL 180 #endif 181 KTR_INFO_MASTER(ifq); 182 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARGS); 183 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARGS); 184 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg) 185 186 #define IF_START_KTR_STRING "ifp=%p" 187 #define IF_START_KTR_ARGS struct ifnet *ifp 188 #ifndef KTR_IF_START 189 #define KTR_IF_START KTR_ALL 190 #endif 191 KTR_INFO_MASTER(if_start); 192 KTR_INFO(KTR_IF_START, if_start, run, 0, 193 IF_START_KTR_STRING, IF_START_KTR_ARGS); 194 KTR_INFO(KTR_IF_START, if_start, sched, 1, 195 IF_START_KTR_STRING, IF_START_KTR_ARGS); 196 KTR_INFO(KTR_IF_START, if_start, avoid, 2, 197 IF_START_KTR_STRING, IF_START_KTR_ARGS); 198 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3, 199 IF_START_KTR_STRING, IF_START_KTR_ARGS); 200 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4, 201 IF_START_KTR_STRING, IF_START_KTR_ARGS); 202 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg) 203 #endif 204 205 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head); 206 207 /* 208 * Network interface utility routines. 209 * 210 * Routines with ifa_ifwith* names take sockaddr *'s as 211 * parameters. 212 */ 213 /* ARGSUSED*/ 214 void 215 ifinit(void *dummy) 216 { 217 struct ifnet *ifp; 218 219 callout_init_mp(&if_slowtimo_timer); 220 netmsg_init(&if_slowtimo_netmsg, NULL, &netisr_adone_rport, 221 MSGF_PRIORITY, if_slowtimo_dispatch); 222 223 /* XXX is this necessary? */ 224 ifnet_lock(); 225 TAILQ_FOREACH(ifp, &ifnetlist, if_link) { 226 if (ifp->if_snd.altq_maxlen == 0) { 227 if_printf(ifp, "XXX: driver didn't set altq_maxlen\n"); 228 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen); 229 } 230 } 231 ifnet_unlock(); 232 233 /* Start if_slowtimo */ 234 lwkt_sendmsg(netisr_cpuport(0), &if_slowtimo_netmsg.lmsg); 235 } 236 237 static void 238 ifsq_ifstart_ipifunc(void *arg) 239 { 240 struct ifaltq_subque *ifsq = arg; 241 struct lwkt_msg *lmsg = ifsq_get_ifstart_lmsg(ifsq, mycpuid); 242 243 crit_enter(); 244 if (lmsg->ms_flags & MSGF_DONE) 245 lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid), lmsg); 246 crit_exit(); 247 } 248 249 static __inline void 250 ifsq_stage_remove(struct ifsubq_stage_head *head, struct ifsubq_stage *stage) 251 { 252 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED); 253 TAILQ_REMOVE(&head->stg_head, stage, stg_link); 254 stage->stg_flags &= ~(IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED); 255 stage->stg_cnt = 0; 256 stage->stg_len = 0; 257 } 258 259 static __inline void 260 ifsq_stage_insert(struct ifsubq_stage_head *head, struct ifsubq_stage *stage) 261 { 262 KKASSERT((stage->stg_flags & 263 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0); 264 stage->stg_flags |= IFSQ_STAGE_FLAG_QUED; 265 TAILQ_INSERT_TAIL(&head->stg_head, stage, stg_link); 266 } 267 268 /* 269 * Schedule ifnet.if_start on the subqueue owner CPU 270 */ 271 static void 272 ifsq_ifstart_schedule(struct ifaltq_subque *ifsq, int force) 273 { 274 int cpu; 275 276 if (!force && curthread->td_type == TD_TYPE_NETISR && 277 ifsq_stage_cntmax > 0) { 278 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid); 279 280 stage->stg_cnt = 0; 281 stage->stg_len = 0; 282 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0) 283 ifsq_stage_insert(&ifsubq_stage_heads[mycpuid], stage); 284 stage->stg_flags |= IFSQ_STAGE_FLAG_SCHED; 285 return; 286 } 287 288 cpu = ifsq_get_cpuid(ifsq); 289 if (cpu != mycpuid) 290 lwkt_send_ipiq(globaldata_find(cpu), ifsq_ifstart_ipifunc, ifsq); 291 else 292 ifsq_ifstart_ipifunc(ifsq); 293 } 294 295 /* 296 * NOTE: 297 * This function will release ifnet.if_start subqueue interlock, 298 * if ifnet.if_start for the subqueue does not need to be scheduled 299 */ 300 static __inline int 301 ifsq_ifstart_need_schedule(struct ifaltq_subque *ifsq, int running) 302 { 303 if (!running || ifsq_is_empty(ifsq) 304 #ifdef ALTQ 305 || ifsq->ifsq_altq->altq_tbr != NULL 306 #endif 307 ) { 308 ALTQ_SQ_LOCK(ifsq); 309 /* 310 * ifnet.if_start subqueue interlock is released, if: 311 * 1) Hardware can not take any packets, due to 312 * o interface is marked down 313 * o hardware queue is full (ifsq_is_oactive) 314 * Under the second situation, hardware interrupt 315 * or polling(4) will call/schedule ifnet.if_start 316 * on the subqueue when hardware queue is ready 317 * 2) There is no packet in the subqueue. 318 * Further ifq_dispatch or ifq_handoff will call/ 319 * schedule ifnet.if_start on the subqueue. 320 * 3) TBR is used and it does not allow further 321 * dequeueing. 322 * TBR callout will call ifnet.if_start on the 323 * subqueue. 324 */ 325 if (!running || !ifsq_data_ready(ifsq)) { 326 ifsq_clr_started(ifsq); 327 ALTQ_SQ_UNLOCK(ifsq); 328 return 0; 329 } 330 ALTQ_SQ_UNLOCK(ifsq); 331 } 332 return 1; 333 } 334 335 static void 336 ifsq_ifstart_dispatch(netmsg_t msg) 337 { 338 struct lwkt_msg *lmsg = &msg->base.lmsg; 339 struct ifaltq_subque *ifsq = lmsg->u.ms_resultp; 340 struct ifnet *ifp = ifsq_get_ifp(ifsq); 341 struct globaldata *gd = mycpu; 342 int running = 0, need_sched; 343 344 crit_enter_gd(gd); 345 346 lwkt_replymsg(lmsg, 0); /* reply ASAP */ 347 348 if (gd->gd_cpuid != ifsq_get_cpuid(ifsq)) { 349 /* 350 * We need to chase the subqueue owner CPU change. 351 */ 352 ifsq_ifstart_schedule(ifsq, 1); 353 crit_exit_gd(gd); 354 return; 355 } 356 357 ifsq_serialize_hw(ifsq); 358 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) { 359 ifp->if_start(ifp, ifsq); 360 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) 361 running = 1; 362 } 363 need_sched = ifsq_ifstart_need_schedule(ifsq, running); 364 ifsq_deserialize_hw(ifsq); 365 366 if (need_sched) { 367 /* 368 * More data need to be transmitted, ifnet.if_start is 369 * scheduled on the subqueue owner CPU, and we keep going. 370 * NOTE: ifnet.if_start subqueue interlock is not released. 371 */ 372 ifsq_ifstart_schedule(ifsq, 0); 373 } 374 375 crit_exit_gd(gd); 376 } 377 378 /* Device driver ifnet.if_start helper function */ 379 void 380 ifsq_devstart(struct ifaltq_subque *ifsq) 381 { 382 struct ifnet *ifp = ifsq_get_ifp(ifsq); 383 int running = 0; 384 385 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq); 386 387 ALTQ_SQ_LOCK(ifsq); 388 if (ifsq_is_started(ifsq) || !ifsq_data_ready(ifsq)) { 389 ALTQ_SQ_UNLOCK(ifsq); 390 return; 391 } 392 ifsq_set_started(ifsq); 393 ALTQ_SQ_UNLOCK(ifsq); 394 395 ifp->if_start(ifp, ifsq); 396 397 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) 398 running = 1; 399 400 if (ifsq_ifstart_need_schedule(ifsq, running)) { 401 /* 402 * More data need to be transmitted, ifnet.if_start is 403 * scheduled on ifnet's CPU, and we keep going. 404 * NOTE: ifnet.if_start interlock is not released. 405 */ 406 ifsq_ifstart_schedule(ifsq, 0); 407 } 408 } 409 410 void 411 if_devstart(struct ifnet *ifp) 412 { 413 ifsq_devstart(ifq_get_subq_default(&ifp->if_snd)); 414 } 415 416 /* Device driver ifnet.if_start schedule helper function */ 417 void 418 ifsq_devstart_sched(struct ifaltq_subque *ifsq) 419 { 420 ifsq_ifstart_schedule(ifsq, 1); 421 } 422 423 void 424 if_devstart_sched(struct ifnet *ifp) 425 { 426 ifsq_devstart_sched(ifq_get_subq_default(&ifp->if_snd)); 427 } 428 429 static void 430 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused) 431 { 432 lwkt_serialize_enter(ifp->if_serializer); 433 } 434 435 static void 436 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused) 437 { 438 lwkt_serialize_exit(ifp->if_serializer); 439 } 440 441 static int 442 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused) 443 { 444 return lwkt_serialize_try(ifp->if_serializer); 445 } 446 447 #ifdef INVARIANTS 448 static void 449 if_default_serialize_assert(struct ifnet *ifp, 450 enum ifnet_serialize slz __unused, 451 boolean_t serialized) 452 { 453 if (serialized) 454 ASSERT_SERIALIZED(ifp->if_serializer); 455 else 456 ASSERT_NOT_SERIALIZED(ifp->if_serializer); 457 } 458 #endif 459 460 /* 461 * Attach an interface to the list of "active" interfaces. 462 * 463 * The serializer is optional. 464 */ 465 void 466 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer) 467 { 468 unsigned socksize, ifasize; 469 int namelen, masklen; 470 struct sockaddr_dl *sdl, *sdl_addr; 471 struct ifaddr *ifa; 472 struct ifaltq *ifq; 473 struct ifnet **old_ifindex2ifnet = NULL; 474 struct ifnet_array *old_ifnet_array; 475 int i, q; 476 477 static int if_indexlim = 8; 478 479 if (ifp->if_serialize != NULL) { 480 KASSERT(ifp->if_deserialize != NULL && 481 ifp->if_tryserialize != NULL && 482 ifp->if_serialize_assert != NULL, 483 ("serialize functions are partially setup")); 484 485 /* 486 * If the device supplies serialize functions, 487 * then clear if_serializer to catch any invalid 488 * usage of this field. 489 */ 490 KASSERT(serializer == NULL, 491 ("both serialize functions and default serializer " 492 "are supplied")); 493 ifp->if_serializer = NULL; 494 } else { 495 KASSERT(ifp->if_deserialize == NULL && 496 ifp->if_tryserialize == NULL && 497 ifp->if_serialize_assert == NULL, 498 ("serialize functions are partially setup")); 499 ifp->if_serialize = if_default_serialize; 500 ifp->if_deserialize = if_default_deserialize; 501 ifp->if_tryserialize = if_default_tryserialize; 502 #ifdef INVARIANTS 503 ifp->if_serialize_assert = if_default_serialize_assert; 504 #endif 505 506 /* 507 * The serializer can be passed in from the device, 508 * allowing the same serializer to be used for both 509 * the interrupt interlock and the device queue. 510 * If not specified, the netif structure will use an 511 * embedded serializer. 512 */ 513 if (serializer == NULL) { 514 serializer = &ifp->if_default_serializer; 515 lwkt_serialize_init(serializer); 516 } 517 ifp->if_serializer = serializer; 518 } 519 520 mtx_init(&ifp->if_ioctl_mtx); 521 522 /* 523 * XXX - 524 * The old code would work if the interface passed a pre-existing 525 * chain of ifaddrs to this code. We don't trust our callers to 526 * properly initialize the tailq, however, so we no longer allow 527 * this unlikely case. 528 */ 529 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead), 530 M_IFADDR, M_WAITOK | M_ZERO); 531 for (i = 0; i < ncpus; ++i) 532 TAILQ_INIT(&ifp->if_addrheads[i]); 533 534 TAILQ_INIT(&ifp->if_multiaddrs); 535 TAILQ_INIT(&ifp->if_groups); 536 getmicrotime(&ifp->if_lastchange); 537 538 /* 539 * create a Link Level name for this device 540 */ 541 namelen = strlen(ifp->if_xname); 542 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen; 543 socksize = masklen + ifp->if_addrlen; 544 if (socksize < sizeof(*sdl)) 545 socksize = sizeof(*sdl); 546 socksize = RT_ROUNDUP(socksize); 547 ifasize = sizeof(struct ifaddr) + 2 * socksize; 548 ifa = ifa_create(ifasize, M_WAITOK); 549 sdl = sdl_addr = (struct sockaddr_dl *)(ifa + 1); 550 sdl->sdl_len = socksize; 551 sdl->sdl_family = AF_LINK; 552 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 553 sdl->sdl_nlen = namelen; 554 sdl->sdl_type = ifp->if_type; 555 ifp->if_lladdr = ifa; 556 ifa->ifa_ifp = ifp; 557 ifa->ifa_rtrequest = link_rtrequest; 558 ifa->ifa_addr = (struct sockaddr *)sdl; 559 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 560 ifa->ifa_netmask = (struct sockaddr *)sdl; 561 sdl->sdl_len = masklen; 562 while (namelen != 0) 563 sdl->sdl_data[--namelen] = 0xff; 564 ifa_iflink(ifa, ifp, 0 /* Insert head */); 565 566 ifp->if_data_pcpu = kmalloc_cachealign( 567 ncpus * sizeof(struct ifdata_pcpu), M_DEVBUF, M_WAITOK | M_ZERO); 568 569 if (ifp->if_mapsubq == NULL) 570 ifp->if_mapsubq = ifq_mapsubq_default; 571 572 ifq = &ifp->if_snd; 573 ifq->altq_type = 0; 574 ifq->altq_disc = NULL; 575 ifq->altq_flags &= ALTQF_CANTCHANGE; 576 ifq->altq_tbr = NULL; 577 ifq->altq_ifp = ifp; 578 579 if (ifq->altq_subq_cnt <= 0) 580 ifq->altq_subq_cnt = 1; 581 ifq->altq_subq = kmalloc_cachealign( 582 ifq->altq_subq_cnt * sizeof(struct ifaltq_subque), 583 M_DEVBUF, M_WAITOK | M_ZERO); 584 585 if (ifq->altq_maxlen == 0) { 586 if_printf(ifp, "driver didn't set altq_maxlen\n"); 587 ifq_set_maxlen(ifq, ifqmaxlen); 588 } 589 590 for (q = 0; q < ifq->altq_subq_cnt; ++q) { 591 struct ifaltq_subque *ifsq = &ifq->altq_subq[q]; 592 593 ALTQ_SQ_LOCK_INIT(ifsq); 594 ifsq->ifsq_index = q; 595 596 ifsq->ifsq_altq = ifq; 597 ifsq->ifsq_ifp = ifp; 598 599 ifsq->ifsq_maxlen = ifq->altq_maxlen; 600 ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen * MCLBYTES; 601 ifsq->ifsq_prepended = NULL; 602 ifsq->ifsq_started = 0; 603 ifsq->ifsq_hw_oactive = 0; 604 ifsq_set_cpuid(ifsq, 0); 605 if (ifp->if_serializer != NULL) 606 ifsq_set_hw_serialize(ifsq, ifp->if_serializer); 607 608 ifsq->ifsq_stage = 609 kmalloc_cachealign(ncpus * sizeof(struct ifsubq_stage), 610 M_DEVBUF, M_WAITOK | M_ZERO); 611 for (i = 0; i < ncpus; ++i) 612 ifsq->ifsq_stage[i].stg_subq = ifsq; 613 614 ifsq->ifsq_ifstart_nmsg = 615 kmalloc(ncpus * sizeof(struct netmsg_base), 616 M_LWKTMSG, M_WAITOK); 617 for (i = 0; i < ncpus; ++i) { 618 netmsg_init(&ifsq->ifsq_ifstart_nmsg[i], NULL, 619 &netisr_adone_rport, 0, ifsq_ifstart_dispatch); 620 ifsq->ifsq_ifstart_nmsg[i].lmsg.u.ms_resultp = ifsq; 621 } 622 } 623 ifq_set_classic(ifq); 624 625 /* 626 * Install this ifp into ifindex2inet, ifnet queue and ifnet 627 * array after it is setup. 628 * 629 * Protect ifindex2ifnet, ifnet queue and ifnet array changes 630 * by ifnet lock, so that non-netisr threads could get a 631 * consistent view. 632 */ 633 ifnet_lock(); 634 635 /* Don't update if_index until ifindex2ifnet is setup */ 636 ifp->if_index = if_index + 1; 637 sdl_addr->sdl_index = ifp->if_index; 638 639 /* 640 * Install this ifp into ifindex2ifnet 641 */ 642 if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) { 643 unsigned int n; 644 struct ifnet **q; 645 646 /* 647 * Grow ifindex2ifnet 648 */ 649 if_indexlim <<= 1; 650 n = if_indexlim * sizeof(*q); 651 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO); 652 if (ifindex2ifnet != NULL) { 653 bcopy(ifindex2ifnet, q, n/2); 654 /* Free old ifindex2ifnet after sync all netisrs */ 655 old_ifindex2ifnet = ifindex2ifnet; 656 } 657 ifindex2ifnet = q; 658 } 659 ifindex2ifnet[ifp->if_index] = ifp; 660 /* 661 * Update if_index after this ifp is installed into ifindex2ifnet, 662 * so that netisrs could get a consistent view of ifindex2ifnet. 663 */ 664 cpu_sfence(); 665 if_index = ifp->if_index; 666 667 /* 668 * Install this ifp into ifnet array. 669 */ 670 /* Free old ifnet array after sync all netisrs */ 671 old_ifnet_array = ifnet_array; 672 ifnet_array = ifnet_array_add(ifp, old_ifnet_array); 673 674 /* 675 * Install this ifp into ifnet queue. 676 */ 677 TAILQ_INSERT_TAIL(&ifnetlist, ifp, if_link); 678 679 ifnet_unlock(); 680 681 /* 682 * Sync all netisrs so that the old ifindex2ifnet and ifnet array 683 * are no longer accessed and we can free them safely later on. 684 */ 685 netmsg_service_sync(); 686 if (old_ifindex2ifnet != NULL) 687 kfree(old_ifindex2ifnet, M_IFADDR); 688 ifnet_array_free(old_ifnet_array); 689 690 if (!SLIST_EMPTY(&domains)) 691 if_attachdomain1(ifp); 692 693 /* Announce the interface. */ 694 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp); 695 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); 696 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 697 } 698 699 static void 700 if_attachdomain(void *dummy) 701 { 702 struct ifnet *ifp; 703 704 ifnet_lock(); 705 TAILQ_FOREACH(ifp, &ifnetlist, if_list) 706 if_attachdomain1(ifp); 707 ifnet_unlock(); 708 } 709 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, 710 if_attachdomain, NULL); 711 712 static void 713 if_attachdomain1(struct ifnet *ifp) 714 { 715 struct domain *dp; 716 717 crit_enter(); 718 719 /* address family dependent data region */ 720 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 721 SLIST_FOREACH(dp, &domains, dom_next) 722 if (dp->dom_ifattach) 723 ifp->if_afdata[dp->dom_family] = 724 (*dp->dom_ifattach)(ifp); 725 crit_exit(); 726 } 727 728 /* 729 * Purge all addresses whose type is _not_ AF_LINK 730 */ 731 void 732 if_purgeaddrs_nolink(struct ifnet *ifp) 733 { 734 struct ifaddr_container *ifac, *next; 735 736 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid], 737 ifa_link, next) { 738 struct ifaddr *ifa = ifac->ifa; 739 740 /* Leave link ifaddr as it is */ 741 if (ifa->ifa_addr->sa_family == AF_LINK) 742 continue; 743 #ifdef INET 744 /* XXX: Ugly!! ad hoc just for INET */ 745 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { 746 struct ifaliasreq ifr; 747 #ifdef IFADDR_DEBUG_VERBOSE 748 int i; 749 750 kprintf("purge in4 addr %p: ", ifa); 751 for (i = 0; i < ncpus; ++i) 752 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt); 753 kprintf("\n"); 754 #endif 755 756 bzero(&ifr, sizeof ifr); 757 ifr.ifra_addr = *ifa->ifa_addr; 758 if (ifa->ifa_dstaddr) 759 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 760 if (in_control(SIOCDIFADDR, (caddr_t)&ifr, ifp, 761 NULL) == 0) 762 continue; 763 } 764 #endif /* INET */ 765 #ifdef INET6 766 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) { 767 #ifdef IFADDR_DEBUG_VERBOSE 768 int i; 769 770 kprintf("purge in6 addr %p: ", ifa); 771 for (i = 0; i < ncpus; ++i) 772 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt); 773 kprintf("\n"); 774 #endif 775 776 in6_purgeaddr(ifa); 777 /* ifp_addrhead is already updated */ 778 continue; 779 } 780 #endif /* INET6 */ 781 ifa_ifunlink(ifa, ifp); 782 ifa_destroy(ifa); 783 } 784 } 785 786 static void 787 ifq_stage_detach_handler(netmsg_t nmsg) 788 { 789 struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp; 790 int q; 791 792 for (q = 0; q < ifq->altq_subq_cnt; ++q) { 793 struct ifaltq_subque *ifsq = &ifq->altq_subq[q]; 794 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid); 795 796 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) 797 ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage); 798 } 799 lwkt_replymsg(&nmsg->lmsg, 0); 800 } 801 802 static void 803 ifq_stage_detach(struct ifaltq *ifq) 804 { 805 struct netmsg_base base; 806 int cpu; 807 808 netmsg_init(&base, NULL, &curthread->td_msgport, 0, 809 ifq_stage_detach_handler); 810 base.lmsg.u.ms_resultp = ifq; 811 812 for (cpu = 0; cpu < ncpus; ++cpu) 813 lwkt_domsg(netisr_cpuport(cpu), &base.lmsg, 0); 814 } 815 816 struct netmsg_if_rtdel { 817 struct netmsg_base base; 818 struct ifnet *ifp; 819 }; 820 821 static void 822 if_rtdel_dispatch(netmsg_t msg) 823 { 824 struct netmsg_if_rtdel *rmsg = (void *)msg; 825 int i, nextcpu, cpu; 826 827 cpu = mycpuid; 828 for (i = 1; i <= AF_MAX; i++) { 829 struct radix_node_head *rnh; 830 831 if ((rnh = rt_tables[cpu][i]) == NULL) 832 continue; 833 rnh->rnh_walktree(rnh, if_rtdel, rmsg->ifp); 834 } 835 836 nextcpu = cpu + 1; 837 if (nextcpu < ncpus) 838 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg); 839 else 840 lwkt_replymsg(&rmsg->base.lmsg, 0); 841 } 842 843 /* 844 * Detach an interface, removing it from the 845 * list of "active" interfaces. 846 */ 847 void 848 if_detach(struct ifnet *ifp) 849 { 850 struct ifnet_array *old_ifnet_array; 851 struct netmsg_if_rtdel msg; 852 struct domain *dp; 853 int q; 854 855 /* Announce that the interface is gone. */ 856 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp); 857 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 858 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); 859 860 /* 861 * Remove this ifp from ifindex2inet, ifnet queue and ifnet 862 * array before it is whacked. 863 * 864 * Protect ifindex2ifnet, ifnet queue and ifnet array changes 865 * by ifnet lock, so that non-netisr threads could get a 866 * consistent view. 867 */ 868 ifnet_lock(); 869 870 /* 871 * Remove this ifp from ifindex2ifnet and maybe decrement if_index. 872 */ 873 ifindex2ifnet[ifp->if_index] = NULL; 874 while (if_index > 0 && ifindex2ifnet[if_index] == NULL) 875 if_index--; 876 877 /* 878 * Remove this ifp from ifnet queue. 879 */ 880 TAILQ_REMOVE(&ifnetlist, ifp, if_link); 881 882 /* 883 * Remove this ifp from ifnet array. 884 */ 885 /* Free old ifnet array after sync all netisrs */ 886 old_ifnet_array = ifnet_array; 887 ifnet_array = ifnet_array_del(ifp, old_ifnet_array); 888 889 ifnet_unlock(); 890 891 /* 892 * Sync all netisrs so that the old ifnet array is no longer 893 * accessed and we can free it safely later on. 894 */ 895 netmsg_service_sync(); 896 ifnet_array_free(old_ifnet_array); 897 898 /* 899 * Remove routes and flush queues. 900 */ 901 crit_enter(); 902 #ifdef IFPOLL_ENABLE 903 if (ifp->if_flags & IFF_NPOLLING) 904 ifpoll_deregister(ifp); 905 #endif 906 if_down(ifp); 907 908 #ifdef ALTQ 909 if (ifq_is_enabled(&ifp->if_snd)) 910 altq_disable(&ifp->if_snd); 911 if (ifq_is_attached(&ifp->if_snd)) 912 altq_detach(&ifp->if_snd); 913 #endif 914 915 /* 916 * Clean up all addresses. 917 */ 918 ifp->if_lladdr = NULL; 919 920 if_purgeaddrs_nolink(ifp); 921 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) { 922 struct ifaddr *ifa; 923 924 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa; 925 KASSERT(ifa->ifa_addr->sa_family == AF_LINK, 926 ("non-link ifaddr is left on if_addrheads")); 927 928 ifa_ifunlink(ifa, ifp); 929 ifa_destroy(ifa); 930 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]), 931 ("there are still ifaddrs left on if_addrheads")); 932 } 933 934 #ifdef INET 935 /* 936 * Remove all IPv4 kernel structures related to ifp. 937 */ 938 in_ifdetach(ifp); 939 #endif 940 941 #ifdef INET6 942 /* 943 * Remove all IPv6 kernel structs related to ifp. This should be done 944 * before removing routing entries below, since IPv6 interface direct 945 * routes are expected to be removed by the IPv6-specific kernel API. 946 * Otherwise, the kernel will detect some inconsistency and bark it. 947 */ 948 in6_ifdetach(ifp); 949 #endif 950 951 /* 952 * Delete all remaining routes using this interface 953 */ 954 netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY, 955 if_rtdel_dispatch); 956 msg.ifp = ifp; 957 rt_domsg_global(&msg.base); 958 959 SLIST_FOREACH(dp, &domains, dom_next) 960 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 961 (*dp->dom_ifdetach)(ifp, 962 ifp->if_afdata[dp->dom_family]); 963 964 kfree(ifp->if_addrheads, M_IFADDR); 965 966 lwkt_synchronize_ipiqs("if_detach"); 967 ifq_stage_detach(&ifp->if_snd); 968 969 for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) { 970 struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q]; 971 972 kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG); 973 kfree(ifsq->ifsq_stage, M_DEVBUF); 974 } 975 kfree(ifp->if_snd.altq_subq, M_DEVBUF); 976 977 kfree(ifp->if_data_pcpu, M_DEVBUF); 978 979 crit_exit(); 980 } 981 982 /* 983 * Create interface group without members 984 */ 985 struct ifg_group * 986 if_creategroup(const char *groupname) 987 { 988 struct ifg_group *ifg = NULL; 989 990 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group), 991 M_TEMP, M_NOWAIT)) == NULL) 992 return (NULL); 993 994 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 995 ifg->ifg_refcnt = 0; 996 ifg->ifg_carp_demoted = 0; 997 TAILQ_INIT(&ifg->ifg_members); 998 #if NPF > 0 999 pfi_attach_ifgroup(ifg); 1000 #endif 1001 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next); 1002 1003 return (ifg); 1004 } 1005 1006 /* 1007 * Add a group to an interface 1008 */ 1009 int 1010 if_addgroup(struct ifnet *ifp, const char *groupname) 1011 { 1012 struct ifg_list *ifgl; 1013 struct ifg_group *ifg = NULL; 1014 struct ifg_member *ifgm; 1015 1016 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 1017 groupname[strlen(groupname) - 1] <= '9') 1018 return (EINVAL); 1019 1020 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1021 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 1022 return (EEXIST); 1023 1024 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) 1025 return (ENOMEM); 1026 1027 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) { 1028 kfree(ifgl, M_TEMP); 1029 return (ENOMEM); 1030 } 1031 1032 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) 1033 if (!strcmp(ifg->ifg_group, groupname)) 1034 break; 1035 1036 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) { 1037 kfree(ifgl, M_TEMP); 1038 kfree(ifgm, M_TEMP); 1039 return (ENOMEM); 1040 } 1041 1042 ifg->ifg_refcnt++; 1043 ifgl->ifgl_group = ifg; 1044 ifgm->ifgm_ifp = ifp; 1045 1046 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 1047 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 1048 1049 #if NPF > 0 1050 pfi_group_change(groupname); 1051 #endif 1052 1053 return (0); 1054 } 1055 1056 /* 1057 * Remove a group from an interface 1058 */ 1059 int 1060 if_delgroup(struct ifnet *ifp, const char *groupname) 1061 { 1062 struct ifg_list *ifgl; 1063 struct ifg_member *ifgm; 1064 1065 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1066 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 1067 break; 1068 if (ifgl == NULL) 1069 return (ENOENT); 1070 1071 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 1072 1073 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 1074 if (ifgm->ifgm_ifp == ifp) 1075 break; 1076 1077 if (ifgm != NULL) { 1078 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); 1079 kfree(ifgm, M_TEMP); 1080 } 1081 1082 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 1083 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next); 1084 #if NPF > 0 1085 pfi_detach_ifgroup(ifgl->ifgl_group); 1086 #endif 1087 kfree(ifgl->ifgl_group, M_TEMP); 1088 } 1089 1090 kfree(ifgl, M_TEMP); 1091 1092 #if NPF > 0 1093 pfi_group_change(groupname); 1094 #endif 1095 1096 return (0); 1097 } 1098 1099 /* 1100 * Stores all groups from an interface in memory pointed 1101 * to by data 1102 */ 1103 int 1104 if_getgroup(caddr_t data, struct ifnet *ifp) 1105 { 1106 int len, error; 1107 struct ifg_list *ifgl; 1108 struct ifg_req ifgrq, *ifgp; 1109 struct ifgroupreq *ifgr = (struct ifgroupreq *)data; 1110 1111 if (ifgr->ifgr_len == 0) { 1112 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1113 ifgr->ifgr_len += sizeof(struct ifg_req); 1114 return (0); 1115 } 1116 1117 len = ifgr->ifgr_len; 1118 ifgp = ifgr->ifgr_groups; 1119 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 1120 if (len < sizeof(ifgrq)) 1121 return (EINVAL); 1122 bzero(&ifgrq, sizeof ifgrq); 1123 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 1124 sizeof(ifgrq.ifgrq_group)); 1125 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp, 1126 sizeof(struct ifg_req)))) 1127 return (error); 1128 len -= sizeof(ifgrq); 1129 ifgp++; 1130 } 1131 1132 return (0); 1133 } 1134 1135 /* 1136 * Stores all members of a group in memory pointed to by data 1137 */ 1138 int 1139 if_getgroupmembers(caddr_t data) 1140 { 1141 struct ifgroupreq *ifgr = (struct ifgroupreq *)data; 1142 struct ifg_group *ifg; 1143 struct ifg_member *ifgm; 1144 struct ifg_req ifgrq, *ifgp; 1145 int len, error; 1146 1147 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) 1148 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 1149 break; 1150 if (ifg == NULL) 1151 return (ENOENT); 1152 1153 if (ifgr->ifgr_len == 0) { 1154 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 1155 ifgr->ifgr_len += sizeof(ifgrq); 1156 return (0); 1157 } 1158 1159 len = ifgr->ifgr_len; 1160 ifgp = ifgr->ifgr_groups; 1161 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 1162 if (len < sizeof(ifgrq)) 1163 return (EINVAL); 1164 bzero(&ifgrq, sizeof ifgrq); 1165 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 1166 sizeof(ifgrq.ifgrq_member)); 1167 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp, 1168 sizeof(struct ifg_req)))) 1169 return (error); 1170 len -= sizeof(ifgrq); 1171 ifgp++; 1172 } 1173 1174 return (0); 1175 } 1176 1177 /* 1178 * Delete Routes for a Network Interface 1179 * 1180 * Called for each routing entry via the rnh->rnh_walktree() call above 1181 * to delete all route entries referencing a detaching network interface. 1182 * 1183 * Arguments: 1184 * rn pointer to node in the routing table 1185 * arg argument passed to rnh->rnh_walktree() - detaching interface 1186 * 1187 * Returns: 1188 * 0 successful 1189 * errno failed - reason indicated 1190 * 1191 */ 1192 static int 1193 if_rtdel(struct radix_node *rn, void *arg) 1194 { 1195 struct rtentry *rt = (struct rtentry *)rn; 1196 struct ifnet *ifp = arg; 1197 int err; 1198 1199 if (rt->rt_ifp == ifp) { 1200 1201 /* 1202 * Protect (sorta) against walktree recursion problems 1203 * with cloned routes 1204 */ 1205 if (!(rt->rt_flags & RTF_UP)) 1206 return (0); 1207 1208 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 1209 rt_mask(rt), rt->rt_flags, 1210 NULL); 1211 if (err) { 1212 log(LOG_WARNING, "if_rtdel: error %d\n", err); 1213 } 1214 } 1215 1216 return (0); 1217 } 1218 1219 /* 1220 * Locate an interface based on a complete address. 1221 */ 1222 struct ifaddr * 1223 ifa_ifwithaddr(struct sockaddr *addr) 1224 { 1225 const struct ifnet_array *arr; 1226 int i; 1227 1228 arr = ifnet_array_get(); 1229 for (i = 0; i < arr->ifnet_count; ++i) { 1230 struct ifnet *ifp = arr->ifnet_arr[i]; 1231 struct ifaddr_container *ifac; 1232 1233 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 1234 struct ifaddr *ifa = ifac->ifa; 1235 1236 if (ifa->ifa_addr->sa_family != addr->sa_family) 1237 continue; 1238 if (sa_equal(addr, ifa->ifa_addr)) 1239 return (ifa); 1240 if ((ifp->if_flags & IFF_BROADCAST) && 1241 ifa->ifa_broadaddr && 1242 /* IPv6 doesn't have broadcast */ 1243 ifa->ifa_broadaddr->sa_len != 0 && 1244 sa_equal(ifa->ifa_broadaddr, addr)) 1245 return (ifa); 1246 } 1247 } 1248 return (NULL); 1249 } 1250 /* 1251 * Locate the point to point interface with a given destination address. 1252 */ 1253 struct ifaddr * 1254 ifa_ifwithdstaddr(struct sockaddr *addr) 1255 { 1256 const struct ifnet_array *arr; 1257 int i; 1258 1259 arr = ifnet_array_get(); 1260 for (i = 0; i < arr->ifnet_count; ++i) { 1261 struct ifnet *ifp = arr->ifnet_arr[i]; 1262 struct ifaddr_container *ifac; 1263 1264 if (!(ifp->if_flags & IFF_POINTOPOINT)) 1265 continue; 1266 1267 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 1268 struct ifaddr *ifa = ifac->ifa; 1269 1270 if (ifa->ifa_addr->sa_family != addr->sa_family) 1271 continue; 1272 if (ifa->ifa_dstaddr && 1273 sa_equal(addr, ifa->ifa_dstaddr)) 1274 return (ifa); 1275 } 1276 } 1277 return (NULL); 1278 } 1279 1280 /* 1281 * Find an interface on a specific network. If many, choice 1282 * is most specific found. 1283 */ 1284 struct ifaddr * 1285 ifa_ifwithnet(struct sockaddr *addr) 1286 { 1287 struct ifaddr *ifa_maybe = NULL; 1288 u_int af = addr->sa_family; 1289 char *addr_data = addr->sa_data, *cplim; 1290 const struct ifnet_array *arr; 1291 int i; 1292 1293 /* 1294 * AF_LINK addresses can be looked up directly by their index number, 1295 * so do that if we can. 1296 */ 1297 if (af == AF_LINK) { 1298 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 1299 1300 if (sdl->sdl_index && sdl->sdl_index <= if_index) 1301 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr); 1302 } 1303 1304 /* 1305 * Scan though each interface, looking for ones that have 1306 * addresses in this address family. 1307 */ 1308 arr = ifnet_array_get(); 1309 for (i = 0; i < arr->ifnet_count; ++i) { 1310 struct ifnet *ifp = arr->ifnet_arr[i]; 1311 struct ifaddr_container *ifac; 1312 1313 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 1314 struct ifaddr *ifa = ifac->ifa; 1315 char *cp, *cp2, *cp3; 1316 1317 if (ifa->ifa_addr->sa_family != af) 1318 next: continue; 1319 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) { 1320 /* 1321 * This is a bit broken as it doesn't 1322 * take into account that the remote end may 1323 * be a single node in the network we are 1324 * looking for. 1325 * The trouble is that we don't know the 1326 * netmask for the remote end. 1327 */ 1328 if (ifa->ifa_dstaddr != NULL && 1329 sa_equal(addr, ifa->ifa_dstaddr)) 1330 return (ifa); 1331 } else { 1332 /* 1333 * if we have a special address handler, 1334 * then use it instead of the generic one. 1335 */ 1336 if (ifa->ifa_claim_addr) { 1337 if ((*ifa->ifa_claim_addr)(ifa, addr)) { 1338 return (ifa); 1339 } else { 1340 continue; 1341 } 1342 } 1343 1344 /* 1345 * Scan all the bits in the ifa's address. 1346 * If a bit dissagrees with what we are 1347 * looking for, mask it with the netmask 1348 * to see if it really matters. 1349 * (A byte at a time) 1350 */ 1351 if (ifa->ifa_netmask == 0) 1352 continue; 1353 cp = addr_data; 1354 cp2 = ifa->ifa_addr->sa_data; 1355 cp3 = ifa->ifa_netmask->sa_data; 1356 cplim = ifa->ifa_netmask->sa_len + 1357 (char *)ifa->ifa_netmask; 1358 while (cp3 < cplim) 1359 if ((*cp++ ^ *cp2++) & *cp3++) 1360 goto next; /* next address! */ 1361 /* 1362 * If the netmask of what we just found 1363 * is more specific than what we had before 1364 * (if we had one) then remember the new one 1365 * before continuing to search 1366 * for an even better one. 1367 */ 1368 if (ifa_maybe == NULL || 1369 rn_refines((char *)ifa->ifa_netmask, 1370 (char *)ifa_maybe->ifa_netmask)) 1371 ifa_maybe = ifa; 1372 } 1373 } 1374 } 1375 return (ifa_maybe); 1376 } 1377 1378 /* 1379 * Find an interface address specific to an interface best matching 1380 * a given address. 1381 */ 1382 struct ifaddr * 1383 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 1384 { 1385 struct ifaddr_container *ifac; 1386 char *cp, *cp2, *cp3; 1387 char *cplim; 1388 struct ifaddr *ifa_maybe = NULL; 1389 u_int af = addr->sa_family; 1390 1391 if (af >= AF_MAX) 1392 return (0); 1393 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 1394 struct ifaddr *ifa = ifac->ifa; 1395 1396 if (ifa->ifa_addr->sa_family != af) 1397 continue; 1398 if (ifa_maybe == NULL) 1399 ifa_maybe = ifa; 1400 if (ifa->ifa_netmask == NULL) { 1401 if (sa_equal(addr, ifa->ifa_addr) || 1402 (ifa->ifa_dstaddr != NULL && 1403 sa_equal(addr, ifa->ifa_dstaddr))) 1404 return (ifa); 1405 continue; 1406 } 1407 if (ifp->if_flags & IFF_POINTOPOINT) { 1408 if (sa_equal(addr, ifa->ifa_dstaddr)) 1409 return (ifa); 1410 } else { 1411 cp = addr->sa_data; 1412 cp2 = ifa->ifa_addr->sa_data; 1413 cp3 = ifa->ifa_netmask->sa_data; 1414 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1415 for (; cp3 < cplim; cp3++) 1416 if ((*cp++ ^ *cp2++) & *cp3) 1417 break; 1418 if (cp3 == cplim) 1419 return (ifa); 1420 } 1421 } 1422 return (ifa_maybe); 1423 } 1424 1425 /* 1426 * Default action when installing a route with a Link Level gateway. 1427 * Lookup an appropriate real ifa to point to. 1428 * This should be moved to /sys/net/link.c eventually. 1429 */ 1430 static void 1431 link_rtrequest(int cmd, struct rtentry *rt) 1432 { 1433 struct ifaddr *ifa; 1434 struct sockaddr *dst; 1435 struct ifnet *ifp; 1436 1437 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL || 1438 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL) 1439 return; 1440 ifa = ifaof_ifpforaddr(dst, ifp); 1441 if (ifa != NULL) { 1442 IFAFREE(rt->rt_ifa); 1443 IFAREF(ifa); 1444 rt->rt_ifa = ifa; 1445 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 1446 ifa->ifa_rtrequest(cmd, rt); 1447 } 1448 } 1449 1450 /* 1451 * Mark an interface down and notify protocols of 1452 * the transition. 1453 * NOTE: must be called at splnet or eqivalent. 1454 */ 1455 void 1456 if_unroute(struct ifnet *ifp, int flag, int fam) 1457 { 1458 struct ifaddr_container *ifac; 1459 1460 ifp->if_flags &= ~flag; 1461 getmicrotime(&ifp->if_lastchange); 1462 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 1463 struct ifaddr *ifa = ifac->ifa; 1464 1465 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1466 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1467 } 1468 ifq_purge_all(&ifp->if_snd); 1469 rt_ifmsg(ifp); 1470 } 1471 1472 /* 1473 * Mark an interface up and notify protocols of 1474 * the transition. 1475 * NOTE: must be called at splnet or eqivalent. 1476 */ 1477 void 1478 if_route(struct ifnet *ifp, int flag, int fam) 1479 { 1480 struct ifaddr_container *ifac; 1481 1482 ifq_purge_all(&ifp->if_snd); 1483 ifp->if_flags |= flag; 1484 getmicrotime(&ifp->if_lastchange); 1485 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 1486 struct ifaddr *ifa = ifac->ifa; 1487 1488 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1489 kpfctlinput(PRC_IFUP, ifa->ifa_addr); 1490 } 1491 rt_ifmsg(ifp); 1492 #ifdef INET6 1493 in6_if_up(ifp); 1494 #endif 1495 } 1496 1497 /* 1498 * Mark an interface down and notify protocols of the transition. An 1499 * interface going down is also considered to be a synchronizing event. 1500 * We must ensure that all packet processing related to the interface 1501 * has completed before we return so e.g. the caller can free the ifnet 1502 * structure that the mbufs may be referencing. 1503 * 1504 * NOTE: must be called at splnet or eqivalent. 1505 */ 1506 void 1507 if_down(struct ifnet *ifp) 1508 { 1509 if_unroute(ifp, IFF_UP, AF_UNSPEC); 1510 netmsg_service_sync(); 1511 } 1512 1513 /* 1514 * Mark an interface up and notify protocols of 1515 * the transition. 1516 * NOTE: must be called at splnet or eqivalent. 1517 */ 1518 void 1519 if_up(struct ifnet *ifp) 1520 { 1521 if_route(ifp, IFF_UP, AF_UNSPEC); 1522 } 1523 1524 /* 1525 * Process a link state change. 1526 * NOTE: must be called at splsoftnet or equivalent. 1527 */ 1528 void 1529 if_link_state_change(struct ifnet *ifp) 1530 { 1531 int link_state = ifp->if_link_state; 1532 1533 rt_ifmsg(ifp); 1534 devctl_notify("IFNET", ifp->if_xname, 1535 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL); 1536 } 1537 1538 /* 1539 * Handle interface watchdog timer routines. Called 1540 * from softclock, we decrement timers (if set) and 1541 * call the appropriate interface routine on expiration. 1542 */ 1543 static void 1544 if_slowtimo_dispatch(netmsg_t nmsg) 1545 { 1546 struct globaldata *gd = mycpu; 1547 const struct ifnet_array *arr; 1548 int i; 1549 1550 KASSERT(&curthread->td_msgport == netisr_cpuport(0), 1551 ("not in netisr0")); 1552 1553 crit_enter_gd(gd); 1554 lwkt_replymsg(&nmsg->lmsg, 0); /* reply ASAP */ 1555 crit_exit_gd(gd); 1556 1557 arr = ifnet_array_get(); 1558 for (i = 0; i < arr->ifnet_count; ++i) { 1559 struct ifnet *ifp = arr->ifnet_arr[i]; 1560 1561 crit_enter_gd(gd); 1562 1563 if (if_stats_compat) { 1564 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets); 1565 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors); 1566 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets); 1567 IFNET_STAT_GET(ifp, oerrors, ifp->if_oerrors); 1568 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions); 1569 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes); 1570 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes); 1571 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts); 1572 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts); 1573 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops); 1574 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto); 1575 } 1576 1577 if (ifp->if_timer == 0 || --ifp->if_timer) { 1578 crit_exit_gd(gd); 1579 continue; 1580 } 1581 if (ifp->if_watchdog) { 1582 if (ifnet_tryserialize_all(ifp)) { 1583 (*ifp->if_watchdog)(ifp); 1584 ifnet_deserialize_all(ifp); 1585 } else { 1586 /* try again next timeout */ 1587 ++ifp->if_timer; 1588 } 1589 } 1590 1591 crit_exit_gd(gd); 1592 } 1593 1594 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL); 1595 } 1596 1597 static void 1598 if_slowtimo(void *arg __unused) 1599 { 1600 struct lwkt_msg *lmsg = &if_slowtimo_netmsg.lmsg; 1601 1602 KASSERT(mycpuid == 0, ("not on cpu0")); 1603 crit_enter(); 1604 if (lmsg->ms_flags & MSGF_DONE) 1605 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg); 1606 crit_exit(); 1607 } 1608 1609 /* 1610 * Map interface name to 1611 * interface structure pointer. 1612 */ 1613 struct ifnet * 1614 ifunit(const char *name) 1615 { 1616 struct ifnet *ifp; 1617 1618 /* 1619 * Search all the interfaces for this name/number 1620 */ 1621 KASSERT(mtx_owned(&ifnet_mtx), ("ifnet is not locked")); 1622 1623 TAILQ_FOREACH(ifp, &ifnetlist, if_link) { 1624 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0) 1625 break; 1626 } 1627 return (ifp); 1628 } 1629 1630 struct ifnet * 1631 ifunit_netisr(const char *name) 1632 { 1633 const struct ifnet_array *arr; 1634 int i; 1635 1636 /* 1637 * Search all the interfaces for this name/number 1638 */ 1639 1640 arr = ifnet_array_get(); 1641 for (i = 0; i < arr->ifnet_count; ++i) { 1642 struct ifnet *ifp = arr->ifnet_arr[i]; 1643 1644 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0) 1645 return ifp; 1646 } 1647 return NULL; 1648 } 1649 1650 /* 1651 * Interface ioctls. 1652 */ 1653 int 1654 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred) 1655 { 1656 struct ifnet *ifp; 1657 struct ifreq *ifr; 1658 struct ifstat *ifs; 1659 int error; 1660 short oif_flags; 1661 int new_flags; 1662 #ifdef COMPAT_43 1663 int ocmd; 1664 #endif 1665 size_t namelen, onamelen; 1666 char new_name[IFNAMSIZ]; 1667 struct ifaddr *ifa; 1668 struct sockaddr_dl *sdl; 1669 1670 switch (cmd) { 1671 case SIOCGIFCONF: 1672 case OSIOCGIFCONF: 1673 return (ifconf(cmd, data, cred)); 1674 default: 1675 break; 1676 } 1677 1678 ifr = (struct ifreq *)data; 1679 1680 switch (cmd) { 1681 case SIOCIFCREATE: 1682 case SIOCIFCREATE2: 1683 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0) 1684 return (error); 1685 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name), 1686 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL)); 1687 case SIOCIFDESTROY: 1688 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0) 1689 return (error); 1690 return (if_clone_destroy(ifr->ifr_name)); 1691 case SIOCIFGCLONERS: 1692 return (if_clone_list((struct if_clonereq *)data)); 1693 default: 1694 break; 1695 } 1696 1697 /* 1698 * Nominal ioctl through interface, lookup the ifp and obtain a 1699 * lock to serialize the ifconfig ioctl operation. 1700 */ 1701 ifnet_lock(); 1702 1703 ifp = ifunit(ifr->ifr_name); 1704 if (ifp == NULL) { 1705 ifnet_unlock(); 1706 return (ENXIO); 1707 } 1708 error = 0; 1709 1710 switch (cmd) { 1711 case SIOCGIFINDEX: 1712 ifr->ifr_index = ifp->if_index; 1713 break; 1714 1715 case SIOCGIFFLAGS: 1716 ifr->ifr_flags = ifp->if_flags; 1717 ifr->ifr_flagshigh = ifp->if_flags >> 16; 1718 break; 1719 1720 case SIOCGIFCAP: 1721 ifr->ifr_reqcap = ifp->if_capabilities; 1722 ifr->ifr_curcap = ifp->if_capenable; 1723 break; 1724 1725 case SIOCGIFMETRIC: 1726 ifr->ifr_metric = ifp->if_metric; 1727 break; 1728 1729 case SIOCGIFMTU: 1730 ifr->ifr_mtu = ifp->if_mtu; 1731 break; 1732 1733 case SIOCGIFTSOLEN: 1734 ifr->ifr_tsolen = ifp->if_tsolen; 1735 break; 1736 1737 case SIOCGIFDATA: 1738 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data, 1739 sizeof(ifp->if_data)); 1740 break; 1741 1742 case SIOCGIFPHYS: 1743 ifr->ifr_phys = ifp->if_physical; 1744 break; 1745 1746 case SIOCGIFPOLLCPU: 1747 ifr->ifr_pollcpu = -1; 1748 break; 1749 1750 case SIOCSIFPOLLCPU: 1751 break; 1752 1753 case SIOCSIFFLAGS: 1754 error = priv_check_cred(cred, PRIV_ROOT, 0); 1755 if (error) 1756 break; 1757 new_flags = (ifr->ifr_flags & 0xffff) | 1758 (ifr->ifr_flagshigh << 16); 1759 if (ifp->if_flags & IFF_SMART) { 1760 /* Smart drivers twiddle their own routes */ 1761 } else if (ifp->if_flags & IFF_UP && 1762 (new_flags & IFF_UP) == 0) { 1763 crit_enter(); 1764 if_down(ifp); 1765 crit_exit(); 1766 } else if (new_flags & IFF_UP && 1767 (ifp->if_flags & IFF_UP) == 0) { 1768 crit_enter(); 1769 if_up(ifp); 1770 crit_exit(); 1771 } 1772 1773 #ifdef IFPOLL_ENABLE 1774 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) { 1775 if (new_flags & IFF_NPOLLING) 1776 ifpoll_register(ifp); 1777 else 1778 ifpoll_deregister(ifp); 1779 } 1780 #endif 1781 1782 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1783 (new_flags &~ IFF_CANTCHANGE); 1784 if (new_flags & IFF_PPROMISC) { 1785 /* Permanently promiscuous mode requested */ 1786 ifp->if_flags |= IFF_PROMISC; 1787 } else if (ifp->if_pcount == 0) { 1788 ifp->if_flags &= ~IFF_PROMISC; 1789 } 1790 if (ifp->if_ioctl) { 1791 ifnet_serialize_all(ifp); 1792 ifp->if_ioctl(ifp, cmd, data, cred); 1793 ifnet_deserialize_all(ifp); 1794 } 1795 getmicrotime(&ifp->if_lastchange); 1796 break; 1797 1798 case SIOCSIFCAP: 1799 error = priv_check_cred(cred, PRIV_ROOT, 0); 1800 if (error) 1801 break; 1802 if (ifr->ifr_reqcap & ~ifp->if_capabilities) { 1803 error = EINVAL; 1804 break; 1805 } 1806 ifnet_serialize_all(ifp); 1807 ifp->if_ioctl(ifp, cmd, data, cred); 1808 ifnet_deserialize_all(ifp); 1809 break; 1810 1811 case SIOCSIFNAME: 1812 error = priv_check_cred(cred, PRIV_ROOT, 0); 1813 if (error) 1814 break; 1815 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); 1816 if (error) 1817 break; 1818 if (new_name[0] == '\0') { 1819 error = EINVAL; 1820 break; 1821 } 1822 if (ifunit(new_name) != NULL) { 1823 error = EEXIST; 1824 break; 1825 } 1826 1827 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp); 1828 1829 /* Announce the departure of the interface. */ 1830 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 1831 1832 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 1833 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa; 1834 /* XXX IFA_LOCK(ifa); */ 1835 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1836 namelen = strlen(new_name); 1837 onamelen = sdl->sdl_nlen; 1838 /* 1839 * Move the address if needed. This is safe because we 1840 * allocate space for a name of length IFNAMSIZ when we 1841 * create this in if_attach(). 1842 */ 1843 if (namelen != onamelen) { 1844 bcopy(sdl->sdl_data + onamelen, 1845 sdl->sdl_data + namelen, sdl->sdl_alen); 1846 } 1847 bcopy(new_name, sdl->sdl_data, namelen); 1848 sdl->sdl_nlen = namelen; 1849 sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 1850 bzero(sdl->sdl_data, onamelen); 1851 while (namelen != 0) 1852 sdl->sdl_data[--namelen] = 0xff; 1853 /* XXX IFA_UNLOCK(ifa) */ 1854 1855 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp); 1856 1857 /* Announce the return of the interface. */ 1858 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 1859 break; 1860 1861 case SIOCSIFMETRIC: 1862 error = priv_check_cred(cred, PRIV_ROOT, 0); 1863 if (error) 1864 break; 1865 ifp->if_metric = ifr->ifr_metric; 1866 getmicrotime(&ifp->if_lastchange); 1867 break; 1868 1869 case SIOCSIFPHYS: 1870 error = priv_check_cred(cred, PRIV_ROOT, 0); 1871 if (error) 1872 break; 1873 if (ifp->if_ioctl == NULL) { 1874 error = EOPNOTSUPP; 1875 break; 1876 } 1877 ifnet_serialize_all(ifp); 1878 error = ifp->if_ioctl(ifp, cmd, data, cred); 1879 ifnet_deserialize_all(ifp); 1880 if (error == 0) 1881 getmicrotime(&ifp->if_lastchange); 1882 break; 1883 1884 case SIOCSIFMTU: 1885 { 1886 u_long oldmtu = ifp->if_mtu; 1887 1888 error = priv_check_cred(cred, PRIV_ROOT, 0); 1889 if (error) 1890 break; 1891 if (ifp->if_ioctl == NULL) { 1892 error = EOPNOTSUPP; 1893 break; 1894 } 1895 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) { 1896 error = EINVAL; 1897 break; 1898 } 1899 ifnet_serialize_all(ifp); 1900 error = ifp->if_ioctl(ifp, cmd, data, cred); 1901 ifnet_deserialize_all(ifp); 1902 if (error == 0) { 1903 getmicrotime(&ifp->if_lastchange); 1904 rt_ifmsg(ifp); 1905 } 1906 /* 1907 * If the link MTU changed, do network layer specific procedure. 1908 */ 1909 if (ifp->if_mtu != oldmtu) { 1910 #ifdef INET6 1911 nd6_setmtu(ifp); 1912 #endif 1913 } 1914 break; 1915 } 1916 1917 case SIOCSIFTSOLEN: 1918 error = priv_check_cred(cred, PRIV_ROOT, 0); 1919 if (error) 1920 break; 1921 1922 /* XXX need driver supplied upper limit */ 1923 if (ifr->ifr_tsolen <= 0) { 1924 error = EINVAL; 1925 break; 1926 } 1927 ifp->if_tsolen = ifr->ifr_tsolen; 1928 break; 1929 1930 case SIOCADDMULTI: 1931 case SIOCDELMULTI: 1932 error = priv_check_cred(cred, PRIV_ROOT, 0); 1933 if (error) 1934 break; 1935 1936 /* Don't allow group membership on non-multicast interfaces. */ 1937 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 1938 error = EOPNOTSUPP; 1939 break; 1940 } 1941 1942 /* Don't let users screw up protocols' entries. */ 1943 if (ifr->ifr_addr.sa_family != AF_LINK) { 1944 error = EINVAL; 1945 break; 1946 } 1947 1948 if (cmd == SIOCADDMULTI) { 1949 struct ifmultiaddr *ifma; 1950 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 1951 } else { 1952 error = if_delmulti(ifp, &ifr->ifr_addr); 1953 } 1954 if (error == 0) 1955 getmicrotime(&ifp->if_lastchange); 1956 break; 1957 1958 case SIOCSIFPHYADDR: 1959 case SIOCDIFPHYADDR: 1960 #ifdef INET6 1961 case SIOCSIFPHYADDR_IN6: 1962 #endif 1963 case SIOCSLIFPHYADDR: 1964 case SIOCSIFMEDIA: 1965 case SIOCSIFGENERIC: 1966 error = priv_check_cred(cred, PRIV_ROOT, 0); 1967 if (error) 1968 break; 1969 if (ifp->if_ioctl == 0) { 1970 error = EOPNOTSUPP; 1971 break; 1972 } 1973 ifnet_serialize_all(ifp); 1974 error = ifp->if_ioctl(ifp, cmd, data, cred); 1975 ifnet_deserialize_all(ifp); 1976 if (error == 0) 1977 getmicrotime(&ifp->if_lastchange); 1978 break; 1979 1980 case SIOCGIFSTATUS: 1981 ifs = (struct ifstat *)data; 1982 ifs->ascii[0] = '\0'; 1983 /* fall through */ 1984 case SIOCGIFPSRCADDR: 1985 case SIOCGIFPDSTADDR: 1986 case SIOCGLIFPHYADDR: 1987 case SIOCGIFMEDIA: 1988 case SIOCGIFGENERIC: 1989 if (ifp->if_ioctl == NULL) { 1990 error = EOPNOTSUPP; 1991 break; 1992 } 1993 ifnet_serialize_all(ifp); 1994 error = ifp->if_ioctl(ifp, cmd, data, cred); 1995 ifnet_deserialize_all(ifp); 1996 break; 1997 1998 case SIOCSIFLLADDR: 1999 error = priv_check_cred(cred, PRIV_ROOT, 0); 2000 if (error) 2001 break; 2002 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data, 2003 ifr->ifr_addr.sa_len); 2004 EVENTHANDLER_INVOKE(iflladdr_event, ifp); 2005 break; 2006 2007 default: 2008 oif_flags = ifp->if_flags; 2009 if (so->so_proto == 0) { 2010 error = EOPNOTSUPP; 2011 break; 2012 } 2013 #ifndef COMPAT_43 2014 error = so_pru_control_direct(so, cmd, data, ifp); 2015 #else 2016 ocmd = cmd; 2017 2018 switch (cmd) { 2019 case SIOCSIFDSTADDR: 2020 case SIOCSIFADDR: 2021 case SIOCSIFBRDADDR: 2022 case SIOCSIFNETMASK: 2023 #if BYTE_ORDER != BIG_ENDIAN 2024 if (ifr->ifr_addr.sa_family == 0 && 2025 ifr->ifr_addr.sa_len < 16) { 2026 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 2027 ifr->ifr_addr.sa_len = 16; 2028 } 2029 #else 2030 if (ifr->ifr_addr.sa_len == 0) 2031 ifr->ifr_addr.sa_len = 16; 2032 #endif 2033 break; 2034 case OSIOCGIFADDR: 2035 cmd = SIOCGIFADDR; 2036 break; 2037 case OSIOCGIFDSTADDR: 2038 cmd = SIOCGIFDSTADDR; 2039 break; 2040 case OSIOCGIFBRDADDR: 2041 cmd = SIOCGIFBRDADDR; 2042 break; 2043 case OSIOCGIFNETMASK: 2044 cmd = SIOCGIFNETMASK; 2045 break; 2046 default: 2047 break; 2048 } 2049 2050 error = so_pru_control_direct(so, cmd, data, ifp); 2051 2052 switch (ocmd) { 2053 case OSIOCGIFADDR: 2054 case OSIOCGIFDSTADDR: 2055 case OSIOCGIFBRDADDR: 2056 case OSIOCGIFNETMASK: 2057 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 2058 break; 2059 } 2060 #endif /* COMPAT_43 */ 2061 2062 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 2063 #ifdef INET6 2064 DELAY(100);/* XXX: temporary workaround for fxp issue*/ 2065 if (ifp->if_flags & IFF_UP) { 2066 crit_enter(); 2067 in6_if_up(ifp); 2068 crit_exit(); 2069 } 2070 #endif 2071 } 2072 break; 2073 } 2074 2075 ifnet_unlock(); 2076 return (error); 2077 } 2078 2079 /* 2080 * Set/clear promiscuous mode on interface ifp based on the truth value 2081 * of pswitch. The calls are reference counted so that only the first 2082 * "on" request actually has an effect, as does the final "off" request. 2083 * Results are undefined if the "off" and "on" requests are not matched. 2084 */ 2085 int 2086 ifpromisc(struct ifnet *ifp, int pswitch) 2087 { 2088 struct ifreq ifr; 2089 int error; 2090 int oldflags; 2091 2092 oldflags = ifp->if_flags; 2093 if (ifp->if_flags & IFF_PPROMISC) { 2094 /* Do nothing if device is in permanently promiscuous mode */ 2095 ifp->if_pcount += pswitch ? 1 : -1; 2096 return (0); 2097 } 2098 if (pswitch) { 2099 /* 2100 * If the device is not configured up, we cannot put it in 2101 * promiscuous mode. 2102 */ 2103 if ((ifp->if_flags & IFF_UP) == 0) 2104 return (ENETDOWN); 2105 if (ifp->if_pcount++ != 0) 2106 return (0); 2107 ifp->if_flags |= IFF_PROMISC; 2108 log(LOG_INFO, "%s: promiscuous mode enabled\n", 2109 ifp->if_xname); 2110 } else { 2111 if (--ifp->if_pcount > 0) 2112 return (0); 2113 ifp->if_flags &= ~IFF_PROMISC; 2114 log(LOG_INFO, "%s: promiscuous mode disabled\n", 2115 ifp->if_xname); 2116 } 2117 ifr.ifr_flags = ifp->if_flags; 2118 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2119 ifnet_serialize_all(ifp); 2120 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL); 2121 ifnet_deserialize_all(ifp); 2122 if (error == 0) 2123 rt_ifmsg(ifp); 2124 else 2125 ifp->if_flags = oldflags; 2126 return error; 2127 } 2128 2129 /* 2130 * Return interface configuration 2131 * of system. List may be used 2132 * in later ioctl's (above) to get 2133 * other information. 2134 */ 2135 static int 2136 ifconf(u_long cmd, caddr_t data, struct ucred *cred) 2137 { 2138 struct ifconf *ifc = (struct ifconf *)data; 2139 struct ifnet *ifp; 2140 struct sockaddr *sa; 2141 struct ifreq ifr, *ifrp; 2142 int space = ifc->ifc_len, error = 0; 2143 2144 ifrp = ifc->ifc_req; 2145 2146 ifnet_lock(); 2147 TAILQ_FOREACH(ifp, &ifnetlist, if_link) { 2148 struct ifaddr_container *ifac; 2149 int addrs; 2150 2151 if (space <= sizeof ifr) 2152 break; 2153 2154 /* 2155 * Zero the stack declared structure first to prevent 2156 * memory disclosure. 2157 */ 2158 bzero(&ifr, sizeof(ifr)); 2159 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 2160 >= sizeof(ifr.ifr_name)) { 2161 error = ENAMETOOLONG; 2162 break; 2163 } 2164 2165 addrs = 0; 2166 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 2167 struct ifaddr *ifa = ifac->ifa; 2168 2169 if (space <= sizeof ifr) 2170 break; 2171 sa = ifa->ifa_addr; 2172 if (cred->cr_prison && 2173 prison_if(cred, sa)) 2174 continue; 2175 addrs++; 2176 #ifdef COMPAT_43 2177 if (cmd == OSIOCGIFCONF) { 2178 struct osockaddr *osa = 2179 (struct osockaddr *)&ifr.ifr_addr; 2180 ifr.ifr_addr = *sa; 2181 osa->sa_family = sa->sa_family; 2182 error = copyout(&ifr, ifrp, sizeof ifr); 2183 ifrp++; 2184 } else 2185 #endif 2186 if (sa->sa_len <= sizeof(*sa)) { 2187 ifr.ifr_addr = *sa; 2188 error = copyout(&ifr, ifrp, sizeof ifr); 2189 ifrp++; 2190 } else { 2191 if (space < (sizeof ifr) + sa->sa_len - 2192 sizeof(*sa)) 2193 break; 2194 space -= sa->sa_len - sizeof(*sa); 2195 error = copyout(&ifr, ifrp, 2196 sizeof ifr.ifr_name); 2197 if (error == 0) 2198 error = copyout(sa, &ifrp->ifr_addr, 2199 sa->sa_len); 2200 ifrp = (struct ifreq *) 2201 (sa->sa_len + (caddr_t)&ifrp->ifr_addr); 2202 } 2203 if (error) 2204 break; 2205 space -= sizeof ifr; 2206 } 2207 if (error) 2208 break; 2209 if (!addrs) { 2210 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr); 2211 error = copyout(&ifr, ifrp, sizeof ifr); 2212 if (error) 2213 break; 2214 space -= sizeof ifr; 2215 ifrp++; 2216 } 2217 } 2218 ifnet_unlock(); 2219 2220 ifc->ifc_len -= space; 2221 return (error); 2222 } 2223 2224 /* 2225 * Just like if_promisc(), but for all-multicast-reception mode. 2226 */ 2227 int 2228 if_allmulti(struct ifnet *ifp, int onswitch) 2229 { 2230 int error = 0; 2231 struct ifreq ifr; 2232 2233 crit_enter(); 2234 2235 if (onswitch) { 2236 if (ifp->if_amcount++ == 0) { 2237 ifp->if_flags |= IFF_ALLMULTI; 2238 ifr.ifr_flags = ifp->if_flags; 2239 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2240 ifnet_serialize_all(ifp); 2241 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, 2242 NULL); 2243 ifnet_deserialize_all(ifp); 2244 } 2245 } else { 2246 if (ifp->if_amcount > 1) { 2247 ifp->if_amcount--; 2248 } else { 2249 ifp->if_amcount = 0; 2250 ifp->if_flags &= ~IFF_ALLMULTI; 2251 ifr.ifr_flags = ifp->if_flags; 2252 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2253 ifnet_serialize_all(ifp); 2254 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, 2255 NULL); 2256 ifnet_deserialize_all(ifp); 2257 } 2258 } 2259 2260 crit_exit(); 2261 2262 if (error == 0) 2263 rt_ifmsg(ifp); 2264 return error; 2265 } 2266 2267 /* 2268 * Add a multicast listenership to the interface in question. 2269 * The link layer provides a routine which converts 2270 */ 2271 int 2272 if_addmulti_serialized(struct ifnet *ifp, struct sockaddr *sa, 2273 struct ifmultiaddr **retifma) 2274 { 2275 struct sockaddr *llsa, *dupsa; 2276 int error; 2277 struct ifmultiaddr *ifma; 2278 2279 ASSERT_IFNET_SERIALIZED_ALL(ifp); 2280 2281 /* 2282 * If the matching multicast address already exists 2283 * then don't add a new one, just add a reference 2284 */ 2285 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2286 if (sa_equal(sa, ifma->ifma_addr)) { 2287 ifma->ifma_refcount++; 2288 if (retifma) 2289 *retifma = ifma; 2290 return 0; 2291 } 2292 } 2293 2294 /* 2295 * Give the link layer a chance to accept/reject it, and also 2296 * find out which AF_LINK address this maps to, if it isn't one 2297 * already. 2298 */ 2299 if (ifp->if_resolvemulti) { 2300 error = ifp->if_resolvemulti(ifp, &llsa, sa); 2301 if (error) 2302 return error; 2303 } else { 2304 llsa = NULL; 2305 } 2306 2307 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK); 2308 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_WAITOK); 2309 bcopy(sa, dupsa, sa->sa_len); 2310 2311 ifma->ifma_addr = dupsa; 2312 ifma->ifma_lladdr = llsa; 2313 ifma->ifma_ifp = ifp; 2314 ifma->ifma_refcount = 1; 2315 ifma->ifma_protospec = NULL; 2316 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 2317 2318 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 2319 if (retifma) 2320 *retifma = ifma; 2321 2322 if (llsa != NULL) { 2323 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2324 if (sa_equal(ifma->ifma_addr, llsa)) 2325 break; 2326 } 2327 if (ifma) { 2328 ifma->ifma_refcount++; 2329 } else { 2330 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK); 2331 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_WAITOK); 2332 bcopy(llsa, dupsa, llsa->sa_len); 2333 ifma->ifma_addr = dupsa; 2334 ifma->ifma_ifp = ifp; 2335 ifma->ifma_refcount = 1; 2336 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 2337 } 2338 } 2339 /* 2340 * We are certain we have added something, so call down to the 2341 * interface to let them know about it. 2342 */ 2343 if (ifp->if_ioctl) 2344 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL); 2345 2346 return 0; 2347 } 2348 2349 int 2350 if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 2351 struct ifmultiaddr **retifma) 2352 { 2353 int error; 2354 2355 ifnet_serialize_all(ifp); 2356 error = if_addmulti_serialized(ifp, sa, retifma); 2357 ifnet_deserialize_all(ifp); 2358 2359 return error; 2360 } 2361 2362 /* 2363 * Remove a reference to a multicast address on this interface. Yell 2364 * if the request does not match an existing membership. 2365 */ 2366 static int 2367 if_delmulti_serialized(struct ifnet *ifp, struct sockaddr *sa) 2368 { 2369 struct ifmultiaddr *ifma; 2370 2371 ASSERT_IFNET_SERIALIZED_ALL(ifp); 2372 2373 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 2374 if (sa_equal(sa, ifma->ifma_addr)) 2375 break; 2376 if (ifma == NULL) 2377 return ENOENT; 2378 2379 if (ifma->ifma_refcount > 1) { 2380 ifma->ifma_refcount--; 2381 return 0; 2382 } 2383 2384 rt_newmaddrmsg(RTM_DELMADDR, ifma); 2385 sa = ifma->ifma_lladdr; 2386 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 2387 /* 2388 * Make sure the interface driver is notified 2389 * in the case of a link layer mcast group being left. 2390 */ 2391 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL) 2392 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL); 2393 kfree(ifma->ifma_addr, M_IFMADDR); 2394 kfree(ifma, M_IFMADDR); 2395 if (sa == NULL) 2396 return 0; 2397 2398 /* 2399 * Now look for the link-layer address which corresponds to 2400 * this network address. It had been squirreled away in 2401 * ifma->ifma_lladdr for this purpose (so we don't have 2402 * to call ifp->if_resolvemulti() again), and we saved that 2403 * value in sa above. If some nasty deleted the 2404 * link-layer address out from underneath us, we can deal because 2405 * the address we stored was is not the same as the one which was 2406 * in the record for the link-layer address. (So we don't complain 2407 * in that case.) 2408 */ 2409 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 2410 if (sa_equal(sa, ifma->ifma_addr)) 2411 break; 2412 if (ifma == NULL) 2413 return 0; 2414 2415 if (ifma->ifma_refcount > 1) { 2416 ifma->ifma_refcount--; 2417 return 0; 2418 } 2419 2420 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 2421 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL); 2422 kfree(ifma->ifma_addr, M_IFMADDR); 2423 kfree(sa, M_IFMADDR); 2424 kfree(ifma, M_IFMADDR); 2425 2426 return 0; 2427 } 2428 2429 int 2430 if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 2431 { 2432 int error; 2433 2434 ifnet_serialize_all(ifp); 2435 error = if_delmulti_serialized(ifp, sa); 2436 ifnet_deserialize_all(ifp); 2437 2438 return error; 2439 } 2440 2441 /* 2442 * Delete all multicast group membership for an interface. 2443 * Should be used to quickly flush all multicast filters. 2444 */ 2445 void 2446 if_delallmulti_serialized(struct ifnet *ifp) 2447 { 2448 struct ifmultiaddr *ifma, mark; 2449 struct sockaddr sa; 2450 2451 ASSERT_IFNET_SERIALIZED_ALL(ifp); 2452 2453 bzero(&sa, sizeof(sa)); 2454 sa.sa_family = AF_UNSPEC; 2455 sa.sa_len = sizeof(sa); 2456 2457 bzero(&mark, sizeof(mark)); 2458 mark.ifma_addr = &sa; 2459 2460 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, &mark, ifma_link); 2461 while ((ifma = TAILQ_NEXT(&mark, ifma_link)) != NULL) { 2462 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link); 2463 TAILQ_INSERT_AFTER(&ifp->if_multiaddrs, ifma, &mark, 2464 ifma_link); 2465 2466 if (ifma->ifma_addr->sa_family == AF_UNSPEC) 2467 continue; 2468 2469 if_delmulti_serialized(ifp, ifma->ifma_addr); 2470 } 2471 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link); 2472 } 2473 2474 2475 /* 2476 * Set the link layer address on an interface. 2477 * 2478 * At this time we only support certain types of interfaces, 2479 * and we don't allow the length of the address to change. 2480 */ 2481 int 2482 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 2483 { 2484 struct sockaddr_dl *sdl; 2485 struct ifreq ifr; 2486 2487 sdl = IF_LLSOCKADDR(ifp); 2488 if (sdl == NULL) 2489 return (EINVAL); 2490 if (len != sdl->sdl_alen) /* don't allow length to change */ 2491 return (EINVAL); 2492 switch (ifp->if_type) { 2493 case IFT_ETHER: /* these types use struct arpcom */ 2494 case IFT_XETHER: 2495 case IFT_L2VLAN: 2496 case IFT_IEEE8023ADLAG: 2497 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len); 2498 bcopy(lladdr, LLADDR(sdl), len); 2499 break; 2500 default: 2501 return (ENODEV); 2502 } 2503 /* 2504 * If the interface is already up, we need 2505 * to re-init it in order to reprogram its 2506 * address filter. 2507 */ 2508 ifnet_serialize_all(ifp); 2509 if ((ifp->if_flags & IFF_UP) != 0) { 2510 #ifdef INET 2511 struct ifaddr_container *ifac; 2512 #endif 2513 2514 ifp->if_flags &= ~IFF_UP; 2515 ifr.ifr_flags = ifp->if_flags; 2516 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2517 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, 2518 NULL); 2519 ifp->if_flags |= IFF_UP; 2520 ifr.ifr_flags = ifp->if_flags; 2521 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2522 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, 2523 NULL); 2524 #ifdef INET 2525 /* 2526 * Also send gratuitous ARPs to notify other nodes about 2527 * the address change. 2528 */ 2529 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 2530 struct ifaddr *ifa = ifac->ifa; 2531 2532 if (ifa->ifa_addr != NULL && 2533 ifa->ifa_addr->sa_family == AF_INET) 2534 arp_gratuitous(ifp, ifa); 2535 } 2536 #endif 2537 } 2538 ifnet_deserialize_all(ifp); 2539 return (0); 2540 } 2541 2542 struct ifmultiaddr * 2543 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp) 2544 { 2545 struct ifmultiaddr *ifma; 2546 2547 /* TODO: need ifnet_serialize_main */ 2548 ifnet_serialize_all(ifp); 2549 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 2550 if (sa_equal(ifma->ifma_addr, sa)) 2551 break; 2552 ifnet_deserialize_all(ifp); 2553 2554 return ifma; 2555 } 2556 2557 /* 2558 * This function locates the first real ethernet MAC from a network 2559 * card and loads it into node, returning 0 on success or ENOENT if 2560 * no suitable interfaces were found. It is used by the uuid code to 2561 * generate a unique 6-byte number. 2562 */ 2563 int 2564 if_getanyethermac(uint16_t *node, int minlen) 2565 { 2566 struct ifnet *ifp; 2567 struct sockaddr_dl *sdl; 2568 2569 ifnet_lock(); 2570 TAILQ_FOREACH(ifp, &ifnetlist, if_link) { 2571 if (ifp->if_type != IFT_ETHER) 2572 continue; 2573 sdl = IF_LLSOCKADDR(ifp); 2574 if (sdl->sdl_alen < minlen) 2575 continue; 2576 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node, 2577 minlen); 2578 ifnet_unlock(); 2579 return(0); 2580 } 2581 ifnet_unlock(); 2582 return (ENOENT); 2583 } 2584 2585 /* 2586 * The name argument must be a pointer to storage which will last as 2587 * long as the interface does. For physical devices, the result of 2588 * device_get_name(dev) is a good choice and for pseudo-devices a 2589 * static string works well. 2590 */ 2591 void 2592 if_initname(struct ifnet *ifp, const char *name, int unit) 2593 { 2594 ifp->if_dname = name; 2595 ifp->if_dunit = unit; 2596 if (unit != IF_DUNIT_NONE) 2597 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 2598 else 2599 strlcpy(ifp->if_xname, name, IFNAMSIZ); 2600 } 2601 2602 int 2603 if_printf(struct ifnet *ifp, const char *fmt, ...) 2604 { 2605 __va_list ap; 2606 int retval; 2607 2608 retval = kprintf("%s: ", ifp->if_xname); 2609 __va_start(ap, fmt); 2610 retval += kvprintf(fmt, ap); 2611 __va_end(ap); 2612 return (retval); 2613 } 2614 2615 struct ifnet * 2616 if_alloc(uint8_t type) 2617 { 2618 struct ifnet *ifp; 2619 size_t size; 2620 2621 /* 2622 * XXX temporary hack until arpcom is setup in if_l2com 2623 */ 2624 if (type == IFT_ETHER) 2625 size = sizeof(struct arpcom); 2626 else 2627 size = sizeof(struct ifnet); 2628 2629 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO); 2630 2631 ifp->if_type = type; 2632 2633 if (if_com_alloc[type] != NULL) { 2634 ifp->if_l2com = if_com_alloc[type](type, ifp); 2635 if (ifp->if_l2com == NULL) { 2636 kfree(ifp, M_IFNET); 2637 return (NULL); 2638 } 2639 } 2640 return (ifp); 2641 } 2642 2643 void 2644 if_free(struct ifnet *ifp) 2645 { 2646 kfree(ifp, M_IFNET); 2647 } 2648 2649 void 2650 ifq_set_classic(struct ifaltq *ifq) 2651 { 2652 ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq, 2653 ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request); 2654 } 2655 2656 void 2657 ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq, 2658 ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request) 2659 { 2660 int q; 2661 2662 KASSERT(mapsubq != NULL, ("mapsubq is not specified")); 2663 KASSERT(enqueue != NULL, ("enqueue is not specified")); 2664 KASSERT(dequeue != NULL, ("dequeue is not specified")); 2665 KASSERT(request != NULL, ("request is not specified")); 2666 2667 ifq->altq_mapsubq = mapsubq; 2668 for (q = 0; q < ifq->altq_subq_cnt; ++q) { 2669 struct ifaltq_subque *ifsq = &ifq->altq_subq[q]; 2670 2671 ifsq->ifsq_enqueue = enqueue; 2672 ifsq->ifsq_dequeue = dequeue; 2673 ifsq->ifsq_request = request; 2674 } 2675 } 2676 2677 static void 2678 ifsq_norm_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m) 2679 { 2680 m->m_nextpkt = NULL; 2681 if (ifsq->ifsq_norm_tail == NULL) 2682 ifsq->ifsq_norm_head = m; 2683 else 2684 ifsq->ifsq_norm_tail->m_nextpkt = m; 2685 ifsq->ifsq_norm_tail = m; 2686 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len); 2687 } 2688 2689 static void 2690 ifsq_prio_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m) 2691 { 2692 m->m_nextpkt = NULL; 2693 if (ifsq->ifsq_prio_tail == NULL) 2694 ifsq->ifsq_prio_head = m; 2695 else 2696 ifsq->ifsq_prio_tail->m_nextpkt = m; 2697 ifsq->ifsq_prio_tail = m; 2698 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len); 2699 ALTQ_SQ_PRIO_CNTR_INC(ifsq, m->m_pkthdr.len); 2700 } 2701 2702 static struct mbuf * 2703 ifsq_norm_dequeue(struct ifaltq_subque *ifsq) 2704 { 2705 struct mbuf *m; 2706 2707 m = ifsq->ifsq_norm_head; 2708 if (m != NULL) { 2709 if ((ifsq->ifsq_norm_head = m->m_nextpkt) == NULL) 2710 ifsq->ifsq_norm_tail = NULL; 2711 m->m_nextpkt = NULL; 2712 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len); 2713 } 2714 return m; 2715 } 2716 2717 static struct mbuf * 2718 ifsq_prio_dequeue(struct ifaltq_subque *ifsq) 2719 { 2720 struct mbuf *m; 2721 2722 m = ifsq->ifsq_prio_head; 2723 if (m != NULL) { 2724 if ((ifsq->ifsq_prio_head = m->m_nextpkt) == NULL) 2725 ifsq->ifsq_prio_tail = NULL; 2726 m->m_nextpkt = NULL; 2727 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len); 2728 ALTQ_SQ_PRIO_CNTR_DEC(ifsq, m->m_pkthdr.len); 2729 } 2730 return m; 2731 } 2732 2733 int 2734 ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m, 2735 struct altq_pktattr *pa __unused) 2736 { 2737 M_ASSERTPKTHDR(m); 2738 if (ifsq->ifsq_len >= ifsq->ifsq_maxlen || 2739 ifsq->ifsq_bcnt >= ifsq->ifsq_maxbcnt) { 2740 if ((m->m_flags & M_PRIO) && 2741 ifsq->ifsq_prio_len < (ifsq->ifsq_maxlen / 2) && 2742 ifsq->ifsq_prio_bcnt < (ifsq->ifsq_maxbcnt / 2)) { 2743 struct mbuf *m_drop; 2744 2745 /* 2746 * Perform drop-head on normal queue 2747 */ 2748 m_drop = ifsq_norm_dequeue(ifsq); 2749 if (m_drop != NULL) { 2750 m_freem(m_drop); 2751 ifsq_prio_enqueue(ifsq, m); 2752 return 0; 2753 } 2754 /* XXX nothing could be dropped? */ 2755 } 2756 m_freem(m); 2757 return ENOBUFS; 2758 } else { 2759 if (m->m_flags & M_PRIO) 2760 ifsq_prio_enqueue(ifsq, m); 2761 else 2762 ifsq_norm_enqueue(ifsq, m); 2763 return 0; 2764 } 2765 } 2766 2767 struct mbuf * 2768 ifsq_classic_dequeue(struct ifaltq_subque *ifsq, int op) 2769 { 2770 struct mbuf *m; 2771 2772 switch (op) { 2773 case ALTDQ_POLL: 2774 m = ifsq->ifsq_prio_head; 2775 if (m == NULL) 2776 m = ifsq->ifsq_norm_head; 2777 break; 2778 2779 case ALTDQ_REMOVE: 2780 m = ifsq_prio_dequeue(ifsq); 2781 if (m == NULL) 2782 m = ifsq_norm_dequeue(ifsq); 2783 break; 2784 2785 default: 2786 panic("unsupported ALTQ dequeue op: %d", op); 2787 } 2788 return m; 2789 } 2790 2791 int 2792 ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg) 2793 { 2794 switch (req) { 2795 case ALTRQ_PURGE: 2796 for (;;) { 2797 struct mbuf *m; 2798 2799 m = ifsq_classic_dequeue(ifsq, ALTDQ_REMOVE); 2800 if (m == NULL) 2801 break; 2802 m_freem(m); 2803 } 2804 break; 2805 2806 default: 2807 panic("unsupported ALTQ request: %d", req); 2808 } 2809 return 0; 2810 } 2811 2812 static void 2813 ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched) 2814 { 2815 struct ifnet *ifp = ifsq_get_ifp(ifsq); 2816 int running = 0, need_sched; 2817 2818 /* 2819 * Try to do direct ifnet.if_start on the subqueue first, if there is 2820 * contention on the subqueue hardware serializer, ifnet.if_start on 2821 * the subqueue will be scheduled on the subqueue owner CPU. 2822 */ 2823 if (!ifsq_tryserialize_hw(ifsq)) { 2824 /* 2825 * Subqueue hardware serializer contention happened, 2826 * ifnet.if_start on the subqueue is scheduled on 2827 * the subqueue owner CPU, and we keep going. 2828 */ 2829 ifsq_ifstart_schedule(ifsq, 1); 2830 return; 2831 } 2832 2833 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) { 2834 ifp->if_start(ifp, ifsq); 2835 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) 2836 running = 1; 2837 } 2838 need_sched = ifsq_ifstart_need_schedule(ifsq, running); 2839 2840 ifsq_deserialize_hw(ifsq); 2841 2842 if (need_sched) { 2843 /* 2844 * More data need to be transmitted, ifnet.if_start on the 2845 * subqueue is scheduled on the subqueue owner CPU, and we 2846 * keep going. 2847 * NOTE: ifnet.if_start subqueue interlock is not released. 2848 */ 2849 ifsq_ifstart_schedule(ifsq, force_sched); 2850 } 2851 } 2852 2853 /* 2854 * Subqeue packets staging mechanism: 2855 * 2856 * The packets enqueued into the subqueue are staged to a certain amount 2857 * before the ifnet.if_start on the subqueue is called. In this way, the 2858 * driver could avoid writing to hardware registers upon every packet, 2859 * instead, hardware registers could be written when certain amount of 2860 * packets are put onto hardware TX ring. The measurement on several modern 2861 * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware 2862 * registers writing aggregation could save ~20% CPU time when 18bytes UDP 2863 * datagrams are transmitted at 1.48Mpps. The performance improvement by 2864 * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's 2865 * netmap paper (http://info.iet.unipi.it/~luigi/netmap/). 2866 * 2867 * Subqueue packets staging is performed for two entry points into drivers' 2868 * transmission function: 2869 * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try() 2870 * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule() 2871 * 2872 * Subqueue packets staging will be stopped upon any of the following 2873 * conditions: 2874 * - If the count of packets enqueued on the current CPU is great than or 2875 * equal to ifsq_stage_cntmax. (XXX this should be per-interface) 2876 * - If the total length of packets enqueued on the current CPU is great 2877 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is 2878 * cut from the hardware's MTU mainly bacause a full TCP segment's size 2879 * is usually less than hardware's MTU. 2880 * - ifsq_ifstart_schedule() is not pending on the current CPU and 2881 * ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not 2882 * released. 2883 * - The if_start_rollup(), which is registered as low priority netisr 2884 * rollup function, is called; probably because no more work is pending 2885 * for netisr. 2886 * 2887 * NOTE: 2888 * Currently subqueue packet staging is only performed in netisr threads. 2889 */ 2890 int 2891 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa) 2892 { 2893 struct ifaltq *ifq = &ifp->if_snd; 2894 struct ifaltq_subque *ifsq; 2895 int error, start = 0, len, mcast = 0, avoid_start = 0; 2896 struct ifsubq_stage_head *head = NULL; 2897 struct ifsubq_stage *stage = NULL; 2898 struct globaldata *gd = mycpu; 2899 struct thread *td = gd->gd_curthread; 2900 2901 crit_enter_quick(td); 2902 2903 ifsq = ifq_map_subq(ifq, gd->gd_cpuid); 2904 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq); 2905 2906 len = m->m_pkthdr.len; 2907 if (m->m_flags & M_MCAST) 2908 mcast = 1; 2909 2910 if (td->td_type == TD_TYPE_NETISR) { 2911 head = &ifsubq_stage_heads[mycpuid]; 2912 stage = ifsq_get_stage(ifsq, mycpuid); 2913 2914 stage->stg_cnt++; 2915 stage->stg_len += len; 2916 if (stage->stg_cnt < ifsq_stage_cntmax && 2917 stage->stg_len < (ifp->if_mtu - max_protohdr)) 2918 avoid_start = 1; 2919 } 2920 2921 ALTQ_SQ_LOCK(ifsq); 2922 error = ifsq_enqueue_locked(ifsq, m, pa); 2923 if (error) { 2924 if (!ifsq_data_ready(ifsq)) { 2925 ALTQ_SQ_UNLOCK(ifsq); 2926 crit_exit_quick(td); 2927 return error; 2928 } 2929 avoid_start = 0; 2930 } 2931 if (!ifsq_is_started(ifsq)) { 2932 if (avoid_start) { 2933 ALTQ_SQ_UNLOCK(ifsq); 2934 2935 KKASSERT(!error); 2936 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0) 2937 ifsq_stage_insert(head, stage); 2938 2939 IFNET_STAT_INC(ifp, obytes, len); 2940 if (mcast) 2941 IFNET_STAT_INC(ifp, omcasts, 1); 2942 crit_exit_quick(td); 2943 return error; 2944 } 2945 2946 /* 2947 * Hold the subqueue interlock of ifnet.if_start 2948 */ 2949 ifsq_set_started(ifsq); 2950 start = 1; 2951 } 2952 ALTQ_SQ_UNLOCK(ifsq); 2953 2954 if (!error) { 2955 IFNET_STAT_INC(ifp, obytes, len); 2956 if (mcast) 2957 IFNET_STAT_INC(ifp, omcasts, 1); 2958 } 2959 2960 if (stage != NULL) { 2961 if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) { 2962 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED); 2963 if (!avoid_start) { 2964 ifsq_stage_remove(head, stage); 2965 ifsq_ifstart_schedule(ifsq, 1); 2966 } 2967 crit_exit_quick(td); 2968 return error; 2969 } 2970 2971 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) { 2972 ifsq_stage_remove(head, stage); 2973 } else { 2974 stage->stg_cnt = 0; 2975 stage->stg_len = 0; 2976 } 2977 } 2978 2979 if (!start) { 2980 crit_exit_quick(td); 2981 return error; 2982 } 2983 2984 ifsq_ifstart_try(ifsq, 0); 2985 2986 crit_exit_quick(td); 2987 return error; 2988 } 2989 2990 void * 2991 ifa_create(int size, int flags) 2992 { 2993 struct ifaddr *ifa; 2994 int i; 2995 2996 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small")); 2997 2998 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO); 2999 if (ifa == NULL) 3000 return NULL; 3001 3002 ifa->ifa_containers = 3003 kmalloc_cachealign(ncpus * sizeof(struct ifaddr_container), 3004 M_IFADDR, M_WAITOK | M_ZERO); 3005 ifa->ifa_ncnt = ncpus; 3006 for (i = 0; i < ncpus; ++i) { 3007 struct ifaddr_container *ifac = &ifa->ifa_containers[i]; 3008 3009 ifac->ifa_magic = IFA_CONTAINER_MAGIC; 3010 ifac->ifa = ifa; 3011 ifac->ifa_refcnt = 1; 3012 } 3013 #ifdef IFADDR_DEBUG 3014 kprintf("alloc ifa %p %d\n", ifa, size); 3015 #endif 3016 return ifa; 3017 } 3018 3019 void 3020 ifac_free(struct ifaddr_container *ifac, int cpu_id) 3021 { 3022 struct ifaddr *ifa = ifac->ifa; 3023 3024 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC); 3025 KKASSERT(ifac->ifa_refcnt == 0); 3026 KASSERT(ifac->ifa_listmask == 0, 3027 ("ifa is still on %#x lists", ifac->ifa_listmask)); 3028 3029 ifac->ifa_magic = IFA_CONTAINER_DEAD; 3030 3031 #ifdef IFADDR_DEBUG_VERBOSE 3032 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id); 3033 #endif 3034 3035 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus, 3036 ("invalid # of ifac, %d", ifa->ifa_ncnt)); 3037 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) { 3038 #ifdef IFADDR_DEBUG 3039 kprintf("free ifa %p\n", ifa); 3040 #endif 3041 kfree(ifa->ifa_containers, M_IFADDR); 3042 kfree(ifa, M_IFADDR); 3043 } 3044 } 3045 3046 static void 3047 ifa_iflink_dispatch(netmsg_t nmsg) 3048 { 3049 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg; 3050 struct ifaddr *ifa = msg->ifa; 3051 struct ifnet *ifp = msg->ifp; 3052 int cpu = mycpuid; 3053 struct ifaddr_container *ifac; 3054 3055 crit_enter(); 3056 3057 ifac = &ifa->ifa_containers[cpu]; 3058 ASSERT_IFAC_VALID(ifac); 3059 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0, 3060 ("ifaddr is on if_addrheads")); 3061 3062 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD; 3063 if (msg->tail) 3064 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link); 3065 else 3066 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link); 3067 3068 crit_exit(); 3069 3070 ifa_forwardmsg(&nmsg->lmsg, cpu + 1); 3071 } 3072 3073 void 3074 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail) 3075 { 3076 struct netmsg_ifaddr msg; 3077 3078 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 3079 0, ifa_iflink_dispatch); 3080 msg.ifa = ifa; 3081 msg.ifp = ifp; 3082 msg.tail = tail; 3083 3084 ifa_domsg(&msg.base.lmsg, 0); 3085 } 3086 3087 static void 3088 ifa_ifunlink_dispatch(netmsg_t nmsg) 3089 { 3090 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg; 3091 struct ifaddr *ifa = msg->ifa; 3092 struct ifnet *ifp = msg->ifp; 3093 int cpu = mycpuid; 3094 struct ifaddr_container *ifac; 3095 3096 crit_enter(); 3097 3098 ifac = &ifa->ifa_containers[cpu]; 3099 ASSERT_IFAC_VALID(ifac); 3100 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD, 3101 ("ifaddr is not on if_addrhead")); 3102 3103 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link); 3104 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD; 3105 3106 crit_exit(); 3107 3108 ifa_forwardmsg(&nmsg->lmsg, cpu + 1); 3109 } 3110 3111 void 3112 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp) 3113 { 3114 struct netmsg_ifaddr msg; 3115 3116 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 3117 0, ifa_ifunlink_dispatch); 3118 msg.ifa = ifa; 3119 msg.ifp = ifp; 3120 3121 ifa_domsg(&msg.base.lmsg, 0); 3122 } 3123 3124 static void 3125 ifa_destroy_dispatch(netmsg_t nmsg) 3126 { 3127 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg; 3128 3129 IFAFREE(msg->ifa); 3130 ifa_forwardmsg(&nmsg->lmsg, mycpuid + 1); 3131 } 3132 3133 void 3134 ifa_destroy(struct ifaddr *ifa) 3135 { 3136 struct netmsg_ifaddr msg; 3137 3138 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 3139 0, ifa_destroy_dispatch); 3140 msg.ifa = ifa; 3141 3142 ifa_domsg(&msg.base.lmsg, 0); 3143 } 3144 3145 struct lwkt_port * 3146 ifnet_portfn(int cpu) 3147 { 3148 return &ifnet_threads[cpu].td_msgport; 3149 } 3150 3151 void 3152 ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu) 3153 { 3154 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus); 3155 3156 if (next_cpu < ncpus) 3157 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg); 3158 else 3159 lwkt_replymsg(lmsg, 0); 3160 } 3161 3162 int 3163 ifnet_domsg(struct lwkt_msg *lmsg, int cpu) 3164 { 3165 KKASSERT(cpu < ncpus); 3166 return lwkt_domsg(ifnet_portfn(cpu), lmsg, 0); 3167 } 3168 3169 void 3170 ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu) 3171 { 3172 KKASSERT(cpu < ncpus); 3173 lwkt_sendmsg(ifnet_portfn(cpu), lmsg); 3174 } 3175 3176 /* 3177 * Generic netmsg service loop. Some protocols may roll their own but all 3178 * must do the basic command dispatch function call done here. 3179 */ 3180 static void 3181 ifnet_service_loop(void *arg __unused) 3182 { 3183 netmsg_t msg; 3184 3185 while ((msg = lwkt_waitport(&curthread->td_msgport, 0))) { 3186 KASSERT(msg->base.nm_dispatch, ("ifnet_service: badmsg")); 3187 msg->base.nm_dispatch(msg); 3188 } 3189 } 3190 3191 static void 3192 if_start_rollup(void) 3193 { 3194 struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid]; 3195 struct ifsubq_stage *stage; 3196 3197 crit_enter(); 3198 3199 while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) { 3200 struct ifaltq_subque *ifsq = stage->stg_subq; 3201 int is_sched = 0; 3202 3203 if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED) 3204 is_sched = 1; 3205 ifsq_stage_remove(head, stage); 3206 3207 if (is_sched) { 3208 ifsq_ifstart_schedule(ifsq, 1); 3209 } else { 3210 int start = 0; 3211 3212 ALTQ_SQ_LOCK(ifsq); 3213 if (!ifsq_is_started(ifsq)) { 3214 /* 3215 * Hold the subqueue interlock of 3216 * ifnet.if_start 3217 */ 3218 ifsq_set_started(ifsq); 3219 start = 1; 3220 } 3221 ALTQ_SQ_UNLOCK(ifsq); 3222 3223 if (start) 3224 ifsq_ifstart_try(ifsq, 1); 3225 } 3226 KKASSERT((stage->stg_flags & 3227 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0); 3228 } 3229 3230 crit_exit(); 3231 } 3232 3233 static void 3234 ifnetinit(void *dummy __unused) 3235 { 3236 int i; 3237 3238 for (i = 0; i < ncpus; ++i) { 3239 struct thread *thr = &ifnet_threads[i]; 3240 3241 lwkt_create(ifnet_service_loop, NULL, NULL, 3242 thr, TDF_NOSTART|TDF_FORCE_SPINPORT|TDF_FIXEDCPU, 3243 i, "ifnet %d", i); 3244 netmsg_service_port_init(&thr->td_msgport); 3245 lwkt_schedule(thr); 3246 } 3247 3248 for (i = 0; i < ncpus; ++i) 3249 TAILQ_INIT(&ifsubq_stage_heads[i].stg_head); 3250 netisr_register_rollup(if_start_rollup, NETISR_ROLLUP_PRIO_IFSTART); 3251 } 3252 3253 void 3254 if_register_com_alloc(u_char type, 3255 if_com_alloc_t *a, if_com_free_t *f) 3256 { 3257 3258 KASSERT(if_com_alloc[type] == NULL, 3259 ("if_register_com_alloc: %d already registered", type)); 3260 KASSERT(if_com_free[type] == NULL, 3261 ("if_register_com_alloc: %d free already registered", type)); 3262 3263 if_com_alloc[type] = a; 3264 if_com_free[type] = f; 3265 } 3266 3267 void 3268 if_deregister_com_alloc(u_char type) 3269 { 3270 3271 KASSERT(if_com_alloc[type] != NULL, 3272 ("if_deregister_com_alloc: %d not registered", type)); 3273 KASSERT(if_com_free[type] != NULL, 3274 ("if_deregister_com_alloc: %d free not registered", type)); 3275 if_com_alloc[type] = NULL; 3276 if_com_free[type] = NULL; 3277 } 3278 3279 int 3280 if_ring_count2(int cnt, int cnt_max) 3281 { 3282 int shift = 0; 3283 3284 KASSERT(cnt_max >= 1 && powerof2(cnt_max), 3285 ("invalid ring count max %d", cnt_max)); 3286 3287 if (cnt <= 0) 3288 cnt = cnt_max; 3289 if (cnt > ncpus2) 3290 cnt = ncpus2; 3291 if (cnt > cnt_max) 3292 cnt = cnt_max; 3293 3294 while ((1 << (shift + 1)) <= cnt) 3295 ++shift; 3296 cnt = 1 << shift; 3297 3298 KASSERT(cnt >= 1 && cnt <= ncpus2 && cnt <= cnt_max, 3299 ("calculate cnt %d, ncpus2 %d, cnt max %d", 3300 cnt, ncpus2, cnt_max)); 3301 return cnt; 3302 } 3303 3304 void 3305 ifq_set_maxlen(struct ifaltq *ifq, int len) 3306 { 3307 ifq->altq_maxlen = len + (ncpus * ifsq_stage_cntmax); 3308 } 3309 3310 int 3311 ifq_mapsubq_default(struct ifaltq *ifq __unused, int cpuid __unused) 3312 { 3313 return ALTQ_SUBQ_INDEX_DEFAULT; 3314 } 3315 3316 int 3317 ifq_mapsubq_mask(struct ifaltq *ifq, int cpuid) 3318 { 3319 return (cpuid & ifq->altq_subq_mask); 3320 } 3321 3322 static void 3323 ifsq_watchdog(void *arg) 3324 { 3325 struct ifsubq_watchdog *wd = arg; 3326 struct ifnet *ifp; 3327 3328 if (__predict_true(wd->wd_timer == 0 || --wd->wd_timer)) 3329 goto done; 3330 3331 ifp = ifsq_get_ifp(wd->wd_subq); 3332 if (ifnet_tryserialize_all(ifp)) { 3333 wd->wd_watchdog(wd->wd_subq); 3334 ifnet_deserialize_all(ifp); 3335 } else { 3336 /* try again next timeout */ 3337 wd->wd_timer = 1; 3338 } 3339 done: 3340 ifsq_watchdog_reset(wd); 3341 } 3342 3343 static void 3344 ifsq_watchdog_reset(struct ifsubq_watchdog *wd) 3345 { 3346 callout_reset_bycpu(&wd->wd_callout, hz, ifsq_watchdog, wd, 3347 ifsq_get_cpuid(wd->wd_subq)); 3348 } 3349 3350 void 3351 ifsq_watchdog_init(struct ifsubq_watchdog *wd, struct ifaltq_subque *ifsq, 3352 ifsq_watchdog_t watchdog) 3353 { 3354 callout_init_mp(&wd->wd_callout); 3355 wd->wd_timer = 0; 3356 wd->wd_subq = ifsq; 3357 wd->wd_watchdog = watchdog; 3358 } 3359 3360 void 3361 ifsq_watchdog_start(struct ifsubq_watchdog *wd) 3362 { 3363 wd->wd_timer = 0; 3364 ifsq_watchdog_reset(wd); 3365 } 3366 3367 void 3368 ifsq_watchdog_stop(struct ifsubq_watchdog *wd) 3369 { 3370 wd->wd_timer = 0; 3371 callout_stop(&wd->wd_callout); 3372 } 3373 3374 void 3375 ifnet_lock(void) 3376 { 3377 KASSERT(curthread->td_type != TD_TYPE_NETISR, 3378 ("try holding ifnet lock in netisr")); 3379 mtx_lock(&ifnet_mtx); 3380 } 3381 3382 void 3383 ifnet_unlock(void) 3384 { 3385 KASSERT(curthread->td_type != TD_TYPE_NETISR, 3386 ("try holding ifnet lock in netisr")); 3387 mtx_unlock(&ifnet_mtx); 3388 } 3389 3390 static struct ifnet_array * 3391 ifnet_array_alloc(int count) 3392 { 3393 struct ifnet_array *arr; 3394 3395 arr = kmalloc(__offsetof(struct ifnet_array, ifnet_arr[count]), 3396 M_IFNET, M_WAITOK); 3397 arr->ifnet_count = count; 3398 3399 return arr; 3400 } 3401 3402 static void 3403 ifnet_array_free(struct ifnet_array *arr) 3404 { 3405 if (arr == &ifnet_array0) 3406 return; 3407 kfree(arr, M_IFNET); 3408 } 3409 3410 static struct ifnet_array * 3411 ifnet_array_add(struct ifnet *ifp, const struct ifnet_array *old_arr) 3412 { 3413 struct ifnet_array *arr; 3414 int count, i; 3415 3416 KASSERT(old_arr->ifnet_count >= 0, 3417 ("invalid ifnet array count %d", old_arr->ifnet_count)); 3418 count = old_arr->ifnet_count + 1; 3419 arr = ifnet_array_alloc(count); 3420 3421 /* 3422 * Save the old ifnet array and append this ifp to the end of 3423 * the new ifnet array. 3424 */ 3425 for (i = 0; i < old_arr->ifnet_count; ++i) { 3426 KASSERT(old_arr->ifnet_arr[i] != ifp, 3427 ("%s is already in ifnet array", ifp->if_xname)); 3428 arr->ifnet_arr[i] = old_arr->ifnet_arr[i]; 3429 } 3430 KASSERT(i == count - 1, 3431 ("add %s, ifnet array index mismatch, should be %d, but got %d", 3432 ifp->if_xname, count - 1, i)); 3433 arr->ifnet_arr[i] = ifp; 3434 3435 return arr; 3436 } 3437 3438 static struct ifnet_array * 3439 ifnet_array_del(struct ifnet *ifp, const struct ifnet_array *old_arr) 3440 { 3441 struct ifnet_array *arr; 3442 int count, i, idx, found = 0; 3443 3444 KASSERT(old_arr->ifnet_count > 0, 3445 ("invalid ifnet array count %d", old_arr->ifnet_count)); 3446 count = old_arr->ifnet_count - 1; 3447 arr = ifnet_array_alloc(count); 3448 3449 /* 3450 * Save the old ifnet array, but skip this ifp. 3451 */ 3452 idx = 0; 3453 for (i = 0; i < old_arr->ifnet_count; ++i) { 3454 if (old_arr->ifnet_arr[i] == ifp) { 3455 KASSERT(!found, 3456 ("dup %s is in ifnet array", ifp->if_xname)); 3457 found = 1; 3458 continue; 3459 } 3460 KASSERT(idx < count, 3461 ("invalid ifnet array index %d, count %d", idx, count)); 3462 arr->ifnet_arr[idx] = old_arr->ifnet_arr[i]; 3463 ++idx; 3464 } 3465 KASSERT(found, ("%s is not in ifnet array", ifp->if_xname)); 3466 KASSERT(idx == count, 3467 ("del %s, ifnet array count mismatch, should be %d, but got %d ", 3468 ifp->if_xname, count, idx)); 3469 3470 return arr; 3471 } 3472 3473 const struct ifnet_array * 3474 ifnet_array_get(void) 3475 { 3476 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr")); 3477 return ifnet_array; 3478 } 3479 3480 int 3481 ifnet_array_isempty(void) 3482 { 3483 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr")); 3484 if (ifnet_array->ifnet_count == 0) 3485 return 1; 3486 else 3487 return 0; 3488 } 3489