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