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