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