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