2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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 $
38 #include "opt_compat.h"
39 #include "opt_inet6.h"
41 #include "opt_polling.h"
42 #include "opt_ifpoll.h"
44 #include <sys/param.h>
45 #include <sys/malloc.h>
47 #include <sys/systm.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>
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>
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>
79 #include <machine/atomic.h>
80 #include <machine/stdarg.h>
81 #include <machine/smp.h>
83 #if defined(INET) || defined(INET6)
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet/if_ether.h>
89 #include <netinet6/in6_var.h>
90 #include <netinet6/in6_ifattach.h>
94 #if defined(COMPAT_43)
95 #include <emulation/43bsd/43bsd_socket.h>
96 #endif /* COMPAT_43 */
98 struct netmsg_ifaddr {
99 struct netmsg_base base;
106 * System initialization
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 *);
119 * XXX: declare here to avoid to include many inet6 related files..
120 * should be more generalized?
122 extern void nd6_setmtu(struct ifnet *);
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");
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)
132 static if_com_alloc_t *if_com_alloc[256];
133 static if_com_free_t *if_com_free[256];
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");
139 int ifqmaxlen = IFQ_MAXLEN;
140 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
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, "");
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, "");
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, "");
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, "");
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, "");
169 struct callout if_slowtimo_timer;
172 struct ifnet **ifindex2ifnet = NULL;
173 static struct thread ifnet_threads[MAXCPU];
175 #define IFQ_KTR_STRING "ifq=%p"
176 #define IFQ_KTR_ARG_SIZE (sizeof(void *))
178 #define KTR_IFQ KTR_ALL
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)
185 #define IF_START_KTR_STRING "ifp=%p"
186 #define IF_START_KTR_ARG_SIZE (sizeof(void *))
188 #define KTR_IF_START KTR_ALL
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);
200 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
201 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
203 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
205 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
208 * Network interface utility routines.
210 * Routines with ifa_ifwith* names take sockaddr *'s as
219 callout_init(&if_slowtimo_timer);
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;
234 if_start_cpuid(struct ifnet *ifp)
236 return ifp->if_cpuid;
239 #ifdef DEVICE_POLLING
241 if_start_cpuid_poll(struct ifnet *ifp)
243 int poll_cpuid = ifp->if_poll_cpuid;
248 return ifp->if_cpuid;
253 if_start_ipifunc(void *arg)
255 struct ifnet *ifp = arg;
256 struct lwkt_msg *lmsg = &ifp->if_start_nmsg[mycpuid].lmsg;
259 if (lmsg->ms_flags & MSGF_DONE)
260 lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg);
265 * Schedule ifnet.if_start on ifnet's CPU
268 if_start_schedule(struct ifnet *ifp)
273 if (if_start_oncpu_sched)
276 cpu = ifp->if_start_cpuid(ifp);
279 lwkt_send_ipiq(globaldata_find(cpu), if_start_ipifunc, ifp);
282 if_start_ipifunc(ifp);
287 * This function will release ifnet.if_start interlock,
288 * if ifnet.if_start does not need to be scheduled
291 if_start_need_schedule(struct ifaltq *ifq, int running)
293 if (!running || ifq_is_empty(ifq)
295 || ifq->altq_tbr != NULL
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
312 * TBR callout will call ifnet.if_start
314 if (!running || !ifq_data_ready(ifq)) {
315 ifq->altq_started = 0;
325 if_start_dispatch(netmsg_t msg)
327 struct lwkt_msg *lmsg = &msg->base.lmsg;
328 struct ifnet *ifp = lmsg->u.ms_resultp;
329 struct ifaltq *ifq = &ifp->if_snd;
333 lwkt_replymsg(lmsg, 0); /* reply ASAP */
337 if (!if_start_oncpu_sched && mycpuid != ifp->if_start_cpuid(ifp)) {
339 * If the ifnet is still up, we need to
340 * chase its CPU change.
342 if (ifp->if_flags & IFF_UP) {
343 logifstart(chase_sched, ifp);
344 if_start_schedule(ifp);
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);
358 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
361 ifnet_deserialize_tx(ifp);
366 if (if_start_need_schedule(ifq, running)) {
368 if (lmsg->ms_flags & MSGF_DONE) { /* XXX necessary? */
369 logifstart(sched, ifp);
370 lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg);
376 /* Device driver ifnet.if_start helper function */
378 if_devstart(struct ifnet *ifp)
380 struct ifaltq *ifq = &ifp->if_snd;
383 ASSERT_IFNET_SERIALIZED_TX(ifp);
386 if (ifq->altq_started || !ifq_data_ready(ifq)) {
387 logifstart(avoid, ifp);
391 ifq->altq_started = 1;
394 if (if_devstart_schedonly) {
396 * Always schedule ifnet.if_start on ifnet's CPU,
397 * short circuit the rest of this function.
399 logifstart(sched, ifp);
400 if_start_schedule(ifp);
404 logifstart(run, ifp);
407 if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
410 if (if_devstart_schednochk || if_start_need_schedule(ifq, running)) {
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.
416 logifstart(sched, ifp);
417 if_start_schedule(ifp);
422 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
424 lwkt_serialize_enter(ifp->if_serializer);
428 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
430 lwkt_serialize_exit(ifp->if_serializer);
434 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
436 return lwkt_serialize_try(ifp->if_serializer);
441 if_default_serialize_assert(struct ifnet *ifp,
442 enum ifnet_serialize slz __unused,
443 boolean_t serialized)
446 ASSERT_SERIALIZED(ifp->if_serializer);
448 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
453 * Attach an interface to the list of "active" interfaces.
455 * The serializer is optional. If non-NULL access to the interface
459 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
461 unsigned socksize, ifasize;
462 int namelen, masklen;
463 struct sockaddr_dl *sdl;
468 static int if_indexlim = 8;
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"));
477 * If the device supplies serialize functions,
478 * then clear if_serializer to catch any invalid
479 * usage of this field.
481 KASSERT(serializer == NULL,
482 ("both serialize functions and default serializer "
484 ifp->if_serializer = NULL;
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;
494 ifp->if_serialize_assert = if_default_serialize_assert;
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.
504 if (serializer == NULL) {
505 serializer = &ifp->if_default_serializer;
506 lwkt_serialize_init(serializer);
508 ifp->if_serializer = serializer;
511 ifp->if_start_cpuid = if_start_cpuid;
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;
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;
529 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
530 ifp->if_index = ++if_index;
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.
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]);
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) {
554 /* grow ifindex2ifnet */
555 n = if_indexlim * sizeof(*q);
556 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
558 bcopy(ifindex2ifnet, q, n/2);
559 kfree(ifindex2ifnet, M_IFADDR);
564 ifindex2ifnet[if_index] = ifp;
567 * create a Link Level name for this device
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;
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;
594 sdl->sdl_data[--namelen] = 0xff;
595 ifa_iflink(ifa, ifp, 0 /* Insert head */);
597 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
598 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
602 ifq->altq_disc = NULL;
603 ifq->altq_flags &= ALTQF_CANTCHANGE;
604 ifq->altq_tbr = NULL;
606 ifq->altq_started = 0;
607 ifq->altq_prepended = NULL;
609 ifq_set_classic(ifq);
611 if (!SLIST_EMPTY(&domains))
612 if_attachdomain1(ifp);
614 /* Announce the interface. */
615 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
619 if_attachdomain(void *dummy)
624 TAILQ_FOREACH(ifp, &ifnet, if_list)
625 if_attachdomain1(ifp);
628 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
629 if_attachdomain, NULL);
632 if_attachdomain1(struct ifnet *ifp)
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);
648 * Purge all addresses whose type is _not_ AF_LINK
651 if_purgeaddrs_nolink(struct ifnet *ifp)
653 struct ifaddr_container *ifac, *next;
655 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
657 struct ifaddr *ifa = ifac->ifa;
659 /* Leave link ifaddr as it is */
660 if (ifa->ifa_addr->sa_family == AF_LINK)
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
669 kprintf("purge in4 addr %p: ", ifa);
670 for (i = 0; i < ncpus; ++i)
671 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
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,
685 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
686 #ifdef IFADDR_DEBUG_VERBOSE
689 kprintf("purge in6 addr %p: ", ifa);
690 for (i = 0; i < ncpus; ++i)
691 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
696 /* ifp_addrhead is already updated */
700 ifa_ifunlink(ifa, ifp);
706 * Detach an interface, removing it from the
707 * list of "active" interfaces.
710 if_detach(struct ifnet *ifp)
712 struct radix_node_head *rnh;
717 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
720 * Remove routes and flush queues.
723 #ifdef DEVICE_POLLING
724 if (ifp->if_flags & IFF_POLLING)
725 ether_poll_deregister(ifp);
728 if (ifp->if_flags & IFF_NPOLLING)
729 ifpoll_deregister(ifp);
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);
741 * Clean up all addresses.
743 ifp->if_lladdr = NULL;
745 if_purgeaddrs_nolink(ifp);
746 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
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"));
753 ifa_ifunlink(ifa, ifp);
755 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
756 ("there are still ifaddrs left on if_addrheads"));
761 * Remove all IPv4 kernel structures related to ifp.
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.
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...
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)
788 rnh->rnh_walktree(rnh, if_rtdel, ifp);
791 lwkt_migratecpu(origcpu);
793 /* Announce that the interface is gone. */
794 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
795 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
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]);
803 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
805 ifindex2ifnet[ifp->if_index] = NULL;
806 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
809 TAILQ_REMOVE(&ifnet, ifp, if_link);
810 kfree(ifp->if_addrheads, M_IFADDR);
811 kfree(ifp->if_start_nmsg, M_LWKTMSG);
816 * Create interface group without members
819 if_creategroup(const char *groupname)
821 struct ifg_group *ifg = NULL;
823 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
824 M_TEMP, M_NOWAIT)) == NULL)
827 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
829 ifg->ifg_carp_demoted = 0;
830 TAILQ_INIT(&ifg->ifg_members);
832 pfi_attach_ifgroup(ifg);
834 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
840 * Add a group to an interface
843 if_addgroup(struct ifnet *ifp, const char *groupname)
845 struct ifg_list *ifgl;
846 struct ifg_group *ifg = NULL;
847 struct ifg_member *ifgm;
849 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
850 groupname[strlen(groupname) - 1] <= '9')
853 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
854 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
857 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
860 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
865 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
866 if (!strcmp(ifg->ifg_group, groupname))
869 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
876 ifgl->ifgl_group = ifg;
877 ifgm->ifgm_ifp = ifp;
879 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
880 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
883 pfi_group_change(groupname);
890 * Remove a group from an interface
893 if_delgroup(struct ifnet *ifp, const char *groupname)
895 struct ifg_list *ifgl;
896 struct ifg_member *ifgm;
898 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
899 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
904 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
906 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
907 if (ifgm->ifgm_ifp == ifp)
911 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
915 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
916 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
918 pfi_detach_ifgroup(ifgl->ifgl_group);
920 kfree(ifgl->ifgl_group, M_TEMP);
926 pfi_group_change(groupname);
933 * Stores all groups from an interface in memory pointed
937 if_getgroup(caddr_t data, struct ifnet *ifp)
940 struct ifg_list *ifgl;
941 struct ifg_req ifgrq, *ifgp;
942 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
944 if (ifgr->ifgr_len == 0) {
945 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
946 ifgr->ifgr_len += sizeof(struct ifg_req);
950 len = ifgr->ifgr_len;
951 ifgp = ifgr->ifgr_groups;
952 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
953 if (len < sizeof(ifgrq))
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))))
961 len -= sizeof(ifgrq);
969 * Stores all members of a group in memory pointed to by data
972 if_getgroupmembers(caddr_t data)
974 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
975 struct ifg_group *ifg;
976 struct ifg_member *ifgm;
977 struct ifg_req ifgrq, *ifgp;
980 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
981 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
986 if (ifgr->ifgr_len == 0) {
987 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
988 ifgr->ifgr_len += sizeof(ifgrq);
992 len = ifgr->ifgr_len;
993 ifgp = ifgr->ifgr_groups;
994 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
995 if (len < sizeof(ifgrq))
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))))
1003 len -= sizeof(ifgrq);
1011 * Delete Routes for a Network Interface
1013 * Called for each routing entry via the rnh->rnh_walktree() call above
1014 * to delete all route entries referencing a detaching network interface.
1017 * rn pointer to node in the routing table
1018 * arg argument passed to rnh->rnh_walktree() - detaching interface
1022 * errno failed - reason indicated
1026 if_rtdel(struct radix_node *rn, void *arg)
1028 struct rtentry *rt = (struct rtentry *)rn;
1029 struct ifnet *ifp = arg;
1032 if (rt->rt_ifp == ifp) {
1035 * Protect (sorta) against walktree recursion problems
1036 * with cloned routes
1038 if (!(rt->rt_flags & RTF_UP))
1041 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1042 rt_mask(rt), rt->rt_flags,
1045 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1053 * Locate an interface based on a complete address.
1056 ifa_ifwithaddr(struct sockaddr *addr)
1060 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1061 struct ifaddr_container *ifac;
1063 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1064 struct ifaddr *ifa = ifac->ifa;
1066 if (ifa->ifa_addr->sa_family != addr->sa_family)
1068 if (sa_equal(addr, ifa->ifa_addr))
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))
1081 * Locate the point to point interface with a given destination address.
1084 ifa_ifwithdstaddr(struct sockaddr *addr)
1088 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1089 struct ifaddr_container *ifac;
1091 if (!(ifp->if_flags & IFF_POINTOPOINT))
1094 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1095 struct ifaddr *ifa = ifac->ifa;
1097 if (ifa->ifa_addr->sa_family != addr->sa_family)
1099 if (ifa->ifa_dstaddr &&
1100 sa_equal(addr, ifa->ifa_dstaddr))
1108 * Find an interface on a specific network. If many, choice
1109 * is most specific found.
1112 ifa_ifwithnet(struct sockaddr *addr)
1115 struct ifaddr *ifa_maybe = NULL;
1116 u_int af = addr->sa_family;
1117 char *addr_data = addr->sa_data, *cplim;
1120 * AF_LINK addresses can be looked up directly by their index number,
1121 * so do that if we can.
1123 if (af == AF_LINK) {
1124 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1126 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1127 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1131 * Scan though each interface, looking for ones that have
1132 * addresses in this address family.
1134 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1135 struct ifaddr_container *ifac;
1137 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1138 struct ifaddr *ifa = ifac->ifa;
1139 char *cp, *cp2, *cp3;
1141 if (ifa->ifa_addr->sa_family != af)
1143 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
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
1149 * The trouble is that we don't know the
1150 * netmask for the remote end.
1152 if (ifa->ifa_dstaddr != NULL &&
1153 sa_equal(addr, ifa->ifa_dstaddr))
1157 * if we have a special address handler,
1158 * then use it instead of the generic one.
1160 if (ifa->ifa_claim_addr) {
1161 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
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)
1175 if (ifa->ifa_netmask == 0)
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;
1183 if ((*cp++ ^ *cp2++) & *cp3++)
1184 goto next; /* next address! */
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.
1192 if (ifa_maybe == 0 ||
1193 rn_refines((char *)ifa->ifa_netmask,
1194 (char *)ifa_maybe->ifa_netmask))
1203 * Find an interface address specific to an interface best matching
1207 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1209 struct ifaddr_container *ifac;
1210 char *cp, *cp2, *cp3;
1212 struct ifaddr *ifa_maybe = 0;
1213 u_int af = addr->sa_family;
1217 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1218 struct ifaddr *ifa = ifac->ifa;
1220 if (ifa->ifa_addr->sa_family != af)
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)))
1231 if (ifp->if_flags & IFF_POINTOPOINT) {
1232 if (sa_equal(addr, ifa->ifa_dstaddr))
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)
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.
1255 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1258 struct sockaddr *dst;
1261 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1262 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1264 ifa = ifaof_ifpforaddr(dst, ifp);
1266 IFAFREE(rt->rt_ifa);
1269 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1270 ifa->ifa_rtrequest(cmd, rt, info);
1275 * Mark an interface down and notify protocols of
1277 * NOTE: must be called at splnet or eqivalent.
1280 if_unroute(struct ifnet *ifp, int flag, int fam)
1282 struct ifaddr_container *ifac;
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;
1289 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1290 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1292 ifq_purge(&ifp->if_snd);
1297 * Mark an interface up and notify protocols of
1299 * NOTE: must be called at splnet or eqivalent.
1302 if_route(struct ifnet *ifp, int flag, int fam)
1304 struct ifaddr_container *ifac;
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;
1312 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1313 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
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.
1328 * NOTE: must be called at splnet or eqivalent.
1331 if_down(struct ifnet *ifp)
1333 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1334 netmsg_service_sync();
1338 * Mark an interface up and notify protocols of
1340 * NOTE: must be called at splnet or eqivalent.
1343 if_up(struct ifnet *ifp)
1345 if_route(ifp, IFF_UP, AF_UNSPEC);
1349 * Process a link state change.
1350 * NOTE: must be called at splsoftnet or equivalent.
1353 if_link_state_change(struct ifnet *ifp)
1355 int link_state = ifp->if_link_state;
1358 devctl_notify("IFNET", ifp->if_xname,
1359 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1363 * Handle interface watchdog timer routines. Called
1364 * from softclock, we decrement timers (if set) and
1365 * call the appropriate interface routine on expiration.
1368 if_slowtimo(void *arg)
1374 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1375 if (ifp->if_timer == 0 || --ifp->if_timer)
1377 if (ifp->if_watchdog) {
1378 if (ifnet_tryserialize_all(ifp)) {
1379 (*ifp->if_watchdog)(ifp);
1380 ifnet_deserialize_all(ifp);
1382 /* try again next timeout */
1390 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1394 * Map interface name to
1395 * interface structure pointer.
1398 ifunit(const char *name)
1403 * Search all the interfaces for this name/number
1406 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1407 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1415 * Map interface name in a sockaddr_dl to
1416 * interface structure pointer.
1419 if_withname(struct sockaddr *sa)
1421 char ifname[IFNAMSIZ+1];
1422 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1424 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1425 (sdl->sdl_nlen > IFNAMSIZ) )
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.
1435 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1436 ifname[sdl->sdl_nlen] = '\0';
1437 return ifunit(ifname);
1445 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1453 size_t namelen, onamelen;
1454 char new_name[IFNAMSIZ];
1456 struct sockaddr_dl *sdl;
1462 return (ifconf(cmd, data, cred));
1464 ifr = (struct ifreq *)data;
1469 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1471 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1472 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1474 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1476 return (if_clone_destroy(ifr->ifr_name));
1478 case SIOCIFGCLONERS:
1479 return (if_clone_list((struct if_clonereq *)data));
1482 ifp = ifunit(ifr->ifr_name);
1488 ifr->ifr_index = ifp->if_index;
1492 ifr->ifr_flags = ifp->if_flags;
1493 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1497 ifr->ifr_reqcap = ifp->if_capabilities;
1498 ifr->ifr_curcap = ifp->if_capenable;
1502 ifr->ifr_metric = ifp->if_metric;
1506 ifr->ifr_mtu = ifp->if_mtu;
1510 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1511 sizeof(ifp->if_data));
1516 ifr->ifr_phys = ifp->if_physical;
1519 case SIOCGIFPOLLCPU:
1520 #ifdef DEVICE_POLLING
1521 ifr->ifr_pollcpu = ifp->if_poll_cpuid;
1523 ifr->ifr_pollcpu = -1;
1527 case SIOCSIFPOLLCPU:
1528 #ifdef DEVICE_POLLING
1529 if ((ifp->if_flags & IFF_POLLING) == 0)
1530 ether_pollcpu_register(ifp, ifr->ifr_pollcpu);
1535 error = priv_check_cred(cred, PRIV_ROOT, 0);
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) {
1547 } else if (new_flags & IFF_UP &&
1548 (ifp->if_flags & IFF_UP) == 0) {
1554 #ifdef DEVICE_POLLING
1555 if ((new_flags ^ ifp->if_flags) & IFF_POLLING) {
1556 if (new_flags & IFF_POLLING) {
1557 ether_poll_register(ifp);
1559 ether_poll_deregister(ifp);
1563 #ifdef IFPOLL_ENABLE
1564 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1565 if (new_flags & IFF_NPOLLING)
1566 ifpoll_register(ifp);
1568 ifpoll_deregister(ifp);
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;
1580 if (ifp->if_ioctl) {
1581 ifnet_serialize_all(ifp);
1582 ifp->if_ioctl(ifp, cmd, data, cred);
1583 ifnet_deserialize_all(ifp);
1585 getmicrotime(&ifp->if_lastchange);
1589 error = priv_check_cred(cred, PRIV_ROOT, 0);
1592 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1594 ifnet_serialize_all(ifp);
1595 ifp->if_ioctl(ifp, cmd, data, cred);
1596 ifnet_deserialize_all(ifp);
1600 error = priv_check_cred(cred, PRIV_ROOT, 0);
1603 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1606 if (new_name[0] == '\0')
1608 if (ifunit(new_name) != NULL)
1611 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1613 /* Announce the departure of the interface. */
1614 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
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;
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().
1627 if (namelen != onamelen) {
1628 bcopy(sdl->sdl_data + onamelen,
1629 sdl->sdl_data + namelen, sdl->sdl_alen);
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) */
1639 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1641 /* Announce the return of the interface. */
1642 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1646 error = priv_check_cred(cred, PRIV_ROOT, 0);
1649 ifp->if_metric = ifr->ifr_metric;
1650 getmicrotime(&ifp->if_lastchange);
1654 error = priv_check_cred(cred, PRIV_ROOT, 0);
1659 ifnet_serialize_all(ifp);
1660 error = ifp->if_ioctl(ifp, cmd, data, cred);
1661 ifnet_deserialize_all(ifp);
1663 getmicrotime(&ifp->if_lastchange);
1668 u_long oldmtu = ifp->if_mtu;
1670 error = priv_check_cred(cred, PRIV_ROOT, 0);
1673 if (ifp->if_ioctl == NULL)
1674 return (EOPNOTSUPP);
1675 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1677 ifnet_serialize_all(ifp);
1678 error = ifp->if_ioctl(ifp, cmd, data, cred);
1679 ifnet_deserialize_all(ifp);
1681 getmicrotime(&ifp->if_lastchange);
1685 * If the link MTU changed, do network layer specific procedure.
1687 if (ifp->if_mtu != oldmtu) {
1697 error = priv_check_cred(cred, PRIV_ROOT, 0);
1701 /* Don't allow group membership on non-multicast interfaces. */
1702 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1705 /* Don't let users screw up protocols' entries. */
1706 if (ifr->ifr_addr.sa_family != AF_LINK)
1709 if (cmd == SIOCADDMULTI) {
1710 struct ifmultiaddr *ifma;
1711 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1713 error = if_delmulti(ifp, &ifr->ifr_addr);
1716 getmicrotime(&ifp->if_lastchange);
1719 case SIOCSIFPHYADDR:
1720 case SIOCDIFPHYADDR:
1722 case SIOCSIFPHYADDR_IN6:
1724 case SIOCSLIFPHYADDR:
1726 case SIOCSIFGENERIC:
1727 error = priv_check_cred(cred, PRIV_ROOT, 0);
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);
1736 getmicrotime(&ifp->if_lastchange);
1740 ifs = (struct ifstat *)data;
1741 ifs->ascii[0] = '\0';
1743 case SIOCGIFPSRCADDR:
1744 case SIOCGIFPDSTADDR:
1745 case SIOCGLIFPHYADDR:
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);
1756 error = priv_check_cred(cred, PRIV_ROOT, 0);
1759 error = if_setlladdr(ifp,
1760 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1761 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
1765 oif_flags = ifp->if_flags;
1766 if (so->so_proto == 0)
1767 return (EOPNOTSUPP);
1769 error = so_pru_control_direct(so, cmd, data, ifp);
1776 case SIOCSIFDSTADDR:
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;
1787 if (ifr->ifr_addr.sa_len == 0)
1788 ifr->ifr_addr.sa_len = 16;
1796 case OSIOCGIFDSTADDR:
1797 cmd = SIOCGIFDSTADDR;
1800 case OSIOCGIFBRDADDR:
1801 cmd = SIOCGIFBRDADDR;
1804 case OSIOCGIFNETMASK:
1805 cmd = SIOCGIFNETMASK;
1808 error = so_pru_control_direct(so, cmd, data, ifp);
1812 case OSIOCGIFDSTADDR:
1813 case OSIOCGIFBRDADDR:
1814 case OSIOCGIFNETMASK:
1815 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1819 #endif /* COMPAT_43 */
1821 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1823 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1824 if (ifp->if_flags & IFF_UP) {
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.
1844 ifpromisc(struct ifnet *ifp, int pswitch)
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;
1858 * If the device is not configured up, we cannot put it in
1861 if ((ifp->if_flags & IFF_UP) == 0)
1863 if (ifp->if_pcount++ != 0)
1865 ifp->if_flags |= IFF_PROMISC;
1866 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1869 if (--ifp->if_pcount > 0)
1871 ifp->if_flags &= ~IFF_PROMISC;
1872 log(LOG_INFO, "%s: promiscuous mode disabled\n",
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);
1883 ifp->if_flags = oldflags;
1888 * Return interface configuration
1889 * of system. List may be used
1890 * in later ioctl's (above) to get
1891 * other information.
1894 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
1896 struct ifconf *ifc = (struct ifconf *)data;
1898 struct sockaddr *sa;
1899 struct ifreq ifr, *ifrp;
1900 int space = ifc->ifc_len, error = 0;
1902 ifrp = ifc->ifc_req;
1903 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1904 struct ifaddr_container *ifac;
1907 if (space <= sizeof ifr)
1911 * Zero the stack declared structure first to prevent
1912 * memory disclosure.
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;
1922 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1923 struct ifaddr *ifa = ifac->ifa;
1925 if (space <= sizeof ifr)
1928 if (cred->cr_prison &&
1929 prison_if(cred, sa))
1933 if (cmd == OSIOCGIFCONF) {
1934 struct osockaddr *osa =
1935 (struct osockaddr *)&ifr.ifr_addr;
1937 osa->sa_family = sa->sa_family;
1938 error = copyout(&ifr, ifrp, sizeof ifr);
1942 if (sa->sa_len <= sizeof(*sa)) {
1944 error = copyout(&ifr, ifrp, sizeof ifr);
1947 if (space < (sizeof ifr) + sa->sa_len -
1950 space -= sa->sa_len - sizeof(*sa);
1951 error = copyout(&ifr, ifrp,
1952 sizeof ifr.ifr_name);
1954 error = copyout(sa, &ifrp->ifr_addr,
1956 ifrp = (struct ifreq *)
1957 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1961 space -= sizeof ifr;
1966 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
1967 error = copyout(&ifr, ifrp, sizeof ifr);
1970 space -= sizeof ifr;
1974 ifc->ifc_len -= space;
1979 * Just like if_promisc(), but for all-multicast-reception mode.
1982 if_allmulti(struct ifnet *ifp, int 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,
1997 ifnet_deserialize_all(ifp);
2000 if (ifp->if_amcount > 1) {
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,
2010 ifnet_deserialize_all(ifp);
2022 * Add a multicast listenership to the interface in question.
2023 * The link layer provides a routine which converts
2027 struct ifnet *ifp, /* interface to manipulate */
2028 struct sockaddr *sa, /* address to add */
2029 struct ifmultiaddr **retifma)
2031 struct sockaddr *llsa, *dupsa;
2033 struct ifmultiaddr *ifma;
2036 * If the matching multicast address already exists
2037 * then don't add a new one, just add a reference
2039 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2040 if (sa_equal(sa, ifma->ifma_addr)) {
2041 ifma->ifma_refcount++;
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
2053 if (ifp->if_resolvemulti) {
2054 ifnet_serialize_all(ifp);
2055 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2056 ifnet_deserialize_all(ifp);
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);
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);
2075 * Some network interfaces can scan the address list at
2076 * interrupt time; lock them out.
2079 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2085 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2086 if (sa_equal(ifma->ifma_addr, llsa))
2090 ifma->ifma_refcount++;
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;
2101 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2106 * We are certain we have added something, so call down to the
2107 * interface to let them know about it.
2110 ifnet_serialize_all(ifp);
2112 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2113 ifnet_deserialize_all(ifp);
2120 * Remove a reference to a multicast address on this interface. Yell
2121 * if the request does not match an existing membership.
2124 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2126 struct ifmultiaddr *ifma;
2128 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2129 if (sa_equal(sa, ifma->ifma_addr))
2134 if (ifma->ifma_refcount > 1) {
2135 ifma->ifma_refcount--;
2139 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2140 sa = ifma->ifma_lladdr;
2142 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2144 * Make sure the interface driver is notified
2145 * in the case of a link layer mcast group being left.
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);
2153 kfree(ifma->ifma_addr, M_IFMADDR);
2154 kfree(ifma, M_IFMADDR);
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
2169 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2170 if (sa_equal(sa, ifma->ifma_addr))
2175 if (ifma->ifma_refcount > 1) {
2176 ifma->ifma_refcount--;
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);
2186 kfree(ifma->ifma_addr, M_IFMADDR);
2187 kfree(sa, M_IFMADDR);
2188 kfree(ifma, M_IFMADDR);
2194 * Delete all multicast group membership for an interface.
2195 * Should be used to quickly flush all multicast filters.
2198 if_delallmulti(struct ifnet *ifp)
2200 struct ifmultiaddr *ifma;
2201 struct ifmultiaddr *next;
2203 TAILQ_FOREACH_MUTABLE(ifma, &ifp->if_multiaddrs, ifma_link, next)
2204 if_delmulti(ifp, ifma->ifma_addr);
2209 * Set the link layer address on an interface.
2211 * At this time we only support certain types of interfaces,
2212 * and we don't allow the length of the address to change.
2215 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2217 struct sockaddr_dl *sdl;
2220 sdl = IF_LLSOCKADDR(ifp);
2223 if (len != sdl->sdl_alen) /* don't allow length to change */
2225 switch (ifp->if_type) {
2226 case IFT_ETHER: /* these types use struct arpcom */
2229 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2230 bcopy(lladdr, LLADDR(sdl), len);
2236 * If the interface is already up, we need
2237 * to re-init it in order to reprogram its
2240 ifnet_serialize_all(ifp);
2241 if ((ifp->if_flags & IFF_UP) != 0) {
2242 struct ifaddr_container *ifac;
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,
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,
2256 * Also send gratuitous ARPs to notify other nodes about
2257 * the address change.
2259 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2260 struct ifaddr *ifa = ifac->ifa;
2262 if (ifa->ifa_addr != NULL &&
2263 ifa->ifa_addr->sa_family == AF_INET)
2264 arp_ifinit(ifp, ifa);
2268 ifnet_deserialize_all(ifp);
2272 struct ifmultiaddr *
2273 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2275 struct ifmultiaddr *ifma;
2277 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2278 if (sa_equal(ifma->ifma_addr, sa))
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.
2291 if_getanyethermac(uint16_t *node, int minlen)
2294 struct sockaddr_dl *sdl;
2296 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2297 if (ifp->if_type != IFT_ETHER)
2299 sdl = IF_LLSOCKADDR(ifp);
2300 if (sdl->sdl_alen < minlen)
2302 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
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.
2316 if_initname(struct ifnet *ifp, const char *name, int unit)
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);
2323 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2327 if_printf(struct ifnet *ifp, const char *fmt, ...)
2332 retval = kprintf("%s: ", ifp->if_xname);
2333 __va_start(ap, fmt);
2334 retval += kvprintf(fmt, ap);
2340 if_alloc(uint8_t type)
2346 * XXX temporary hack until arpcom is setup in if_l2com
2348 if (type == IFT_ETHER)
2349 size = sizeof(struct arpcom);
2351 size = sizeof(struct ifnet);
2353 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2355 ifp->if_type = type;
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);
2368 if_free(struct ifnet *ifp)
2370 kfree(ifp, M_IFNET);
2374 ifq_set_classic(struct ifaltq *ifq)
2376 ifq->altq_enqueue = ifq_classic_enqueue;
2377 ifq->altq_dequeue = ifq_classic_dequeue;
2378 ifq->altq_request = ifq_classic_request;
2382 ifq_classic_enqueue(struct ifaltq *ifq, struct mbuf *m,
2383 struct altq_pktattr *pa __unused)
2385 logifq(enqueue, ifq);
2386 if (IF_QFULL(ifq)) {
2396 ifq_classic_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
2405 logifq(dequeue, ifq);
2409 panic("unsupported ALTQ dequeue op: %d", op);
2411 KKASSERT(mpolled == NULL || mpolled == m);
2416 ifq_classic_request(struct ifaltq *ifq, int req, void *arg)
2423 panic("unsupported ALTQ request: %d", req);
2429 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2431 struct ifaltq *ifq = &ifp->if_snd;
2432 int running = 0, error, start = 0;
2434 ASSERT_IFNET_NOT_SERIALIZED_TX(ifp);
2437 error = ifq_enqueue_locked(ifq, m, pa);
2442 if (!ifq->altq_started) {
2444 * Hold the interlock of ifnet.if_start
2446 ifq->altq_started = 1;
2451 ifp->if_obytes += m->m_pkthdr.len;
2452 if (m->m_flags & M_MCAST)
2456 logifstart(avoid, ifp);
2460 if (ifq_dispatch_schedonly) {
2462 * Always schedule ifnet.if_start on ifnet's CPU,
2463 * short circuit the rest of this function.
2465 logifstart(sched, ifp);
2466 if_start_schedule(ifp);
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.
2475 if (!ifnet_tryserialize_tx(ifp)) {
2477 * ifnet serializer contention happened,
2478 * ifnet.if_start is scheduled on ifnet's
2479 * CPU, and we keep going.
2481 logifstart(contend_sched, ifp);
2482 if_start_schedule(ifp);
2486 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
2487 logifstart(run, ifp);
2489 if ((ifp->if_flags &
2490 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
2494 ifnet_deserialize_tx(ifp);
2496 if (ifq_dispatch_schednochk || if_start_need_schedule(ifq, running)) {
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.
2502 logifstart(sched, ifp);
2503 if_start_schedule(ifp);
2509 ifa_create(int size, int flags)
2514 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small\n"));
2516 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
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];
2526 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
2528 ifac->ifa_refcnt = 1;
2531 kprintf("alloc ifa %p %d\n", ifa, size);
2537 ifac_free(struct ifaddr_container *ifac, int cpu_id)
2539 struct ifaddr *ifa = ifac->ifa;
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));
2546 ifac->ifa_magic = IFA_CONTAINER_DEAD;
2548 #ifdef IFADDR_DEBUG_VERBOSE
2549 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
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) {
2556 kprintf("free ifa %p\n", ifa);
2558 kfree(ifa->ifa_containers, M_IFADDR);
2559 kfree(ifa, M_IFADDR);
2564 ifa_iflink_dispatch(netmsg_t nmsg)
2566 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2567 struct ifaddr *ifa = msg->ifa;
2568 struct ifnet *ifp = msg->ifp;
2570 struct ifaddr_container *ifac;
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"));
2579 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
2581 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
2583 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
2587 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2591 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
2593 struct netmsg_ifaddr msg;
2595 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2596 0, ifa_iflink_dispatch);
2601 ifa_domsg(&msg.base.lmsg, 0);
2605 ifa_ifunlink_dispatch(netmsg_t nmsg)
2607 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2608 struct ifaddr *ifa = msg->ifa;
2609 struct ifnet *ifp = msg->ifp;
2611 struct ifaddr_container *ifac;
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"));
2620 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
2621 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
2625 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2629 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
2631 struct netmsg_ifaddr msg;
2633 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2634 0, ifa_ifunlink_dispatch);
2638 ifa_domsg(&msg.base.lmsg, 0);
2642 ifa_destroy_dispatch(netmsg_t nmsg)
2644 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2647 ifa_forwardmsg(&nmsg->lmsg, mycpuid + 1);
2651 ifa_destroy(struct ifaddr *ifa)
2653 struct netmsg_ifaddr msg;
2655 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2656 0, ifa_destroy_dispatch);
2659 ifa_domsg(&msg.base.lmsg, 0);
2663 ifnet_portfn(int cpu)
2665 return &ifnet_threads[cpu].td_msgport;
2669 ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
2671 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
2673 if (next_cpu < ncpus)
2674 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
2676 lwkt_replymsg(lmsg, 0);
2680 ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
2682 KKASSERT(cpu < ncpus);
2683 return lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
2687 ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
2689 KKASSERT(cpu < ncpus);
2690 lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
2694 * Generic netmsg service loop. Some protocols may roll their own but all
2695 * must do the basic command dispatch function call done here.
2698 ifnet_service_loop(void *arg __unused)
2702 while ((msg = lwkt_waitport(&curthread->td_msgport, 0))) {
2703 KASSERT(msg->base.nm_dispatch, ("ifnet_service: badmsg"));
2704 msg->base.nm_dispatch(msg);
2709 ifnetinit(void *dummy __unused)
2713 for (i = 0; i < ncpus; ++i) {
2714 struct thread *thr = &ifnet_threads[i];
2716 lwkt_create(ifnet_service_loop, NULL, NULL,
2717 thr, TDF_STOPREQ, i, "ifnet %d", i);
2718 netmsg_service_port_init(&thr->td_msgport);
2724 ifnet_byindex(unsigned short idx)
2728 return ifindex2ifnet[idx];
2732 ifaddr_byindex(unsigned short idx)
2736 ifp = ifnet_byindex(idx);
2739 return TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
2743 if_register_com_alloc(u_char type,
2744 if_com_alloc_t *a, if_com_free_t *f)
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));
2752 if_com_alloc[type] = a;
2753 if_com_free[type] = f;
2757 if_deregister_com_alloc(u_char type)
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;