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