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 $
37 #include "opt_compat.h"
38 #include "opt_inet6.h"
40 #include "opt_polling.h"
41 #include "opt_ifpoll.h"
43 #include <sys/param.h>
44 #include <sys/malloc.h>
46 #include <sys/systm.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/socketops.h>
53 #include <sys/protosw.h>
54 #include <sys/kernel.h>
56 #include <sys/mutex.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/serialize.h>
65 #include <sys/thread2.h>
66 #include <sys/msgport2.h>
67 #include <sys/mutex2.h>
70 #include <net/if_arp.h>
71 #include <net/if_dl.h>
72 #include <net/if_types.h>
73 #include <net/if_var.h>
74 #include <net/ifq_var.h>
75 #include <net/radix.h>
76 #include <net/route.h>
77 #include <net/if_clone.h>
78 #include <net/netisr.h>
79 #include <net/netmsg2.h>
81 #include <machine/atomic.h>
82 #include <machine/stdarg.h>
83 #include <machine/smp.h>
85 #if defined(INET) || defined(INET6)
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/if_ether.h>
91 #include <netinet6/in6_var.h>
92 #include <netinet6/in6_ifattach.h>
96 #if defined(COMPAT_43)
97 #include <emulation/43bsd/43bsd_socket.h>
98 #endif /* COMPAT_43 */
100 struct netmsg_ifaddr {
101 struct netmsg_base base;
108 * System initialization
110 static void if_attachdomain(void *);
111 static void if_attachdomain1(struct ifnet *);
112 static int ifconf(u_long, caddr_t, struct ucred *);
113 static void ifinit(void *);
114 static void ifnetinit(void *);
115 static void if_slowtimo(void *);
116 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
117 static int if_rtdel(struct radix_node *, void *);
121 * XXX: declare here to avoid to include many inet6 related files..
122 * should be more generalized?
124 extern void nd6_setmtu(struct ifnet *);
127 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
128 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
130 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
131 /* Must be after netisr_init */
132 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_SECOND, ifnetinit, NULL)
134 static if_com_alloc_t *if_com_alloc[256];
135 static if_com_free_t *if_com_free[256];
137 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
138 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
139 MALLOC_DEFINE(M_IFNET, "ifnet", "interface structure");
141 int ifqmaxlen = IFQ_MAXLEN;
142 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
144 /* In ifq_dispatch(), try to do direct ifnet.if_start first */
145 static int ifq_dispatch_schedonly = 0;
146 SYSCTL_INT(_net_link_generic, OID_AUTO, ifq_dispatch_schedonly, CTLFLAG_RW,
147 &ifq_dispatch_schedonly, 0, "");
149 /* In ifq_dispatch(), schedule ifnet.if_start without checking ifnet.if_snd */
150 static int ifq_dispatch_schednochk = 0;
151 SYSCTL_INT(_net_link_generic, OID_AUTO, ifq_dispatch_schednochk, CTLFLAG_RW,
152 &ifq_dispatch_schednochk, 0, "");
154 /* In if_devstart(), try to do direct ifnet.if_start first */
155 static int if_devstart_schedonly = 0;
156 SYSCTL_INT(_net_link_generic, OID_AUTO, if_devstart_schedonly, CTLFLAG_RW,
157 &if_devstart_schedonly, 0, "");
159 /* In if_devstart(), schedule ifnet.if_start without checking ifnet.if_snd */
160 static int if_devstart_schednochk = 0;
161 SYSCTL_INT(_net_link_generic, OID_AUTO, if_devstart_schednochk, CTLFLAG_RW,
162 &if_devstart_schednochk, 0, "");
165 /* Schedule ifnet.if_start on the current CPU */
166 static int if_start_oncpu_sched = 0;
167 SYSCTL_INT(_net_link_generic, OID_AUTO, if_start_oncpu_sched, CTLFLAG_RW,
168 &if_start_oncpu_sched, 0, "");
171 struct callout if_slowtimo_timer;
174 struct ifnet **ifindex2ifnet = NULL;
175 static struct thread ifnet_threads[MAXCPU];
177 #define IFQ_KTR_STRING "ifq=%p"
178 #define IFQ_KTR_ARG_SIZE (sizeof(void *))
180 #define KTR_IFQ KTR_ALL
182 KTR_INFO_MASTER(ifq);
183 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARG_SIZE);
184 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARG_SIZE);
185 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
187 #define IF_START_KTR_STRING "ifp=%p"
188 #define IF_START_KTR_ARG_SIZE (sizeof(void *))
190 #define KTR_IF_START KTR_ALL
192 KTR_INFO_MASTER(if_start);
193 KTR_INFO(KTR_IF_START, if_start, run, 0,
194 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
195 KTR_INFO(KTR_IF_START, if_start, sched, 1,
196 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
197 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
198 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
199 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
200 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
202 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
203 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
205 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
207 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
210 * Network interface utility routines.
212 * Routines with ifa_ifwith* names take sockaddr *'s as
221 callout_init(&if_slowtimo_timer);
224 TAILQ_FOREACH(ifp, &ifnet, if_link) {
225 if (ifp->if_snd.ifq_maxlen == 0) {
226 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
227 ifp->if_snd.ifq_maxlen = ifqmaxlen;
236 if_start_cpuid(struct ifnet *ifp)
238 return ifp->if_cpuid;
241 #ifdef DEVICE_POLLING
243 if_start_cpuid_poll(struct ifnet *ifp)
245 int poll_cpuid = ifp->if_poll_cpuid;
250 return ifp->if_cpuid;
255 if_start_ipifunc(void *arg)
257 struct ifnet *ifp = arg;
258 struct lwkt_msg *lmsg = &ifp->if_start_nmsg[mycpuid].lmsg;
261 if (lmsg->ms_flags & MSGF_DONE)
262 lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg);
267 * Schedule ifnet.if_start on ifnet's CPU
270 if_start_schedule(struct ifnet *ifp)
275 if (if_start_oncpu_sched)
278 cpu = ifp->if_start_cpuid(ifp);
281 lwkt_send_ipiq(globaldata_find(cpu), if_start_ipifunc, ifp);
284 if_start_ipifunc(ifp);
289 * This function will release ifnet.if_start interlock,
290 * if ifnet.if_start does not need to be scheduled
293 if_start_need_schedule(struct ifaltq *ifq, int running)
295 if (!running || ifq_is_empty(ifq)
297 || ifq->altq_tbr != NULL
302 * ifnet.if_start interlock is released, if:
303 * 1) Hardware can not take any packets, due to
304 * o interface is marked down
305 * o hardware queue is full (IFF_OACTIVE)
306 * Under the second situation, hardware interrupt
307 * or polling(4) will call/schedule ifnet.if_start
308 * when hardware queue is ready
309 * 2) There is not packet in the ifnet.if_snd.
310 * Further ifq_dispatch or ifq_handoff will call/
311 * schedule ifnet.if_start
312 * 3) TBR is used and it does not allow further
314 * TBR callout will call ifnet.if_start
316 if (!running || !ifq_data_ready(ifq)) {
317 ifq->altq_started = 0;
327 if_start_dispatch(netmsg_t msg)
329 struct lwkt_msg *lmsg = &msg->base.lmsg;
330 struct ifnet *ifp = lmsg->u.ms_resultp;
331 struct ifaltq *ifq = &ifp->if_snd;
335 lwkt_replymsg(lmsg, 0); /* reply ASAP */
339 if (!if_start_oncpu_sched && mycpuid != ifp->if_start_cpuid(ifp)) {
341 * If the ifnet is still up, we need to
342 * chase its CPU change.
344 if (ifp->if_flags & IFF_UP) {
345 logifstart(chase_sched, ifp);
346 if_start_schedule(ifp);
354 if (ifp->if_flags & IFF_UP) {
355 ifnet_serialize_tx(ifp); /* XXX try? */
356 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
357 logifstart(run, ifp);
360 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
363 ifnet_deserialize_tx(ifp);
368 if (if_start_need_schedule(ifq, running)) {
370 if (lmsg->ms_flags & MSGF_DONE) { /* XXX necessary? */
371 logifstart(sched, ifp);
372 lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg);
378 /* Device driver ifnet.if_start helper function */
380 if_devstart(struct ifnet *ifp)
382 struct ifaltq *ifq = &ifp->if_snd;
385 ASSERT_IFNET_SERIALIZED_TX(ifp);
388 if (ifq->altq_started || !ifq_data_ready(ifq)) {
389 logifstart(avoid, ifp);
393 ifq->altq_started = 1;
396 if (if_devstart_schedonly) {
398 * Always schedule ifnet.if_start on ifnet's CPU,
399 * short circuit the rest of this function.
401 logifstart(sched, ifp);
402 if_start_schedule(ifp);
406 logifstart(run, ifp);
409 if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
412 if (if_devstart_schednochk || if_start_need_schedule(ifq, running)) {
414 * More data need to be transmitted, ifnet.if_start is
415 * scheduled on ifnet's CPU, and we keep going.
416 * NOTE: ifnet.if_start interlock is not released.
418 logifstart(sched, ifp);
419 if_start_schedule(ifp);
424 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
426 lwkt_serialize_enter(ifp->if_serializer);
430 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
432 lwkt_serialize_exit(ifp->if_serializer);
436 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
438 return lwkt_serialize_try(ifp->if_serializer);
443 if_default_serialize_assert(struct ifnet *ifp,
444 enum ifnet_serialize slz __unused,
445 boolean_t serialized)
448 ASSERT_SERIALIZED(ifp->if_serializer);
450 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
455 * Attach an interface to the list of "active" interfaces.
457 * The serializer is optional. If non-NULL access to the interface
461 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
463 unsigned socksize, ifasize;
464 int namelen, masklen;
465 struct sockaddr_dl *sdl;
470 static int if_indexlim = 8;
472 if (ifp->if_serialize != NULL) {
473 KASSERT(ifp->if_deserialize != NULL &&
474 ifp->if_tryserialize != NULL &&
475 ifp->if_serialize_assert != NULL,
476 ("serialize functions are partially setup\n"));
479 * If the device supplies serialize functions,
480 * then clear if_serializer to catch any invalid
481 * usage of this field.
483 KASSERT(serializer == NULL,
484 ("both serialize functions and default serializer "
486 ifp->if_serializer = NULL;
488 KASSERT(ifp->if_deserialize == NULL &&
489 ifp->if_tryserialize == NULL &&
490 ifp->if_serialize_assert == NULL,
491 ("serialize functions are partially setup\n"));
492 ifp->if_serialize = if_default_serialize;
493 ifp->if_deserialize = if_default_deserialize;
494 ifp->if_tryserialize = if_default_tryserialize;
496 ifp->if_serialize_assert = if_default_serialize_assert;
500 * The serializer can be passed in from the device,
501 * allowing the same serializer to be used for both
502 * the interrupt interlock and the device queue.
503 * If not specified, the netif structure will use an
504 * embedded serializer.
506 if (serializer == NULL) {
507 serializer = &ifp->if_default_serializer;
508 lwkt_serialize_init(serializer);
510 ifp->if_serializer = serializer;
513 ifp->if_start_cpuid = if_start_cpuid;
516 #ifdef DEVICE_POLLING
517 /* Device is not in polling mode by default */
518 ifp->if_poll_cpuid = -1;
519 if (ifp->if_poll != NULL)
520 ifp->if_start_cpuid = if_start_cpuid_poll;
523 ifp->if_start_nmsg = kmalloc(ncpus * sizeof(*ifp->if_start_nmsg),
524 M_LWKTMSG, M_WAITOK);
525 for (i = 0; i < ncpus; ++i) {
526 netmsg_init(&ifp->if_start_nmsg[i], NULL, &netisr_adone_rport,
527 0, if_start_dispatch);
528 ifp->if_start_nmsg[i].lmsg.u.ms_resultp = ifp;
531 mtx_init(&ifp->if_ioctl_mtx);
532 mtx_lock(&ifp->if_ioctl_mtx);
534 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
535 ifp->if_index = ++if_index;
539 * The old code would work if the interface passed a pre-existing
540 * chain of ifaddrs to this code. We don't trust our callers to
541 * properly initialize the tailq, however, so we no longer allow
542 * this unlikely case.
544 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
545 M_IFADDR, M_WAITOK | M_ZERO);
546 for (i = 0; i < ncpus; ++i)
547 TAILQ_INIT(&ifp->if_addrheads[i]);
549 TAILQ_INIT(&ifp->if_prefixhead);
550 TAILQ_INIT(&ifp->if_multiaddrs);
551 TAILQ_INIT(&ifp->if_groups);
552 getmicrotime(&ifp->if_lastchange);
553 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
559 /* grow ifindex2ifnet */
560 n = if_indexlim * sizeof(*q);
561 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
563 bcopy(ifindex2ifnet, q, n/2);
564 kfree(ifindex2ifnet, M_IFADDR);
569 ifindex2ifnet[if_index] = ifp;
572 * create a Link Level name for this device
574 namelen = strlen(ifp->if_xname);
575 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
576 socksize = masklen + ifp->if_addrlen;
577 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
578 if (socksize < sizeof(*sdl))
579 socksize = sizeof(*sdl);
580 socksize = ROUNDUP(socksize);
582 ifasize = sizeof(struct ifaddr) + 2 * socksize;
583 ifa = ifa_create(ifasize, M_WAITOK);
584 sdl = (struct sockaddr_dl *)(ifa + 1);
585 sdl->sdl_len = socksize;
586 sdl->sdl_family = AF_LINK;
587 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
588 sdl->sdl_nlen = namelen;
589 sdl->sdl_index = ifp->if_index;
590 sdl->sdl_type = ifp->if_type;
591 ifp->if_lladdr = ifa;
593 ifa->ifa_rtrequest = link_rtrequest;
594 ifa->ifa_addr = (struct sockaddr *)sdl;
595 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
596 ifa->ifa_netmask = (struct sockaddr *)sdl;
597 sdl->sdl_len = masklen;
599 sdl->sdl_data[--namelen] = 0xff;
600 ifa_iflink(ifa, ifp, 0 /* Insert head */);
602 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
603 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
607 ifq->altq_disc = NULL;
608 ifq->altq_flags &= ALTQF_CANTCHANGE;
609 ifq->altq_tbr = NULL;
611 ifq->altq_started = 0;
612 ifq->altq_prepended = NULL;
614 ifq_set_classic(ifq);
616 if (!SLIST_EMPTY(&domains))
617 if_attachdomain1(ifp);
619 /* Announce the interface. */
620 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
622 mtx_unlock(&ifp->if_ioctl_mtx);
626 if_attachdomain(void *dummy)
631 TAILQ_FOREACH(ifp, &ifnet, if_list)
632 if_attachdomain1(ifp);
635 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
636 if_attachdomain, NULL);
639 if_attachdomain1(struct ifnet *ifp)
645 /* address family dependent data region */
646 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
647 SLIST_FOREACH(dp, &domains, dom_next)
648 if (dp->dom_ifattach)
649 ifp->if_afdata[dp->dom_family] =
650 (*dp->dom_ifattach)(ifp);
655 * Purge all addresses whose type is _not_ AF_LINK
658 if_purgeaddrs_nolink(struct ifnet *ifp)
660 struct ifaddr_container *ifac, *next;
662 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
664 struct ifaddr *ifa = ifac->ifa;
666 /* Leave link ifaddr as it is */
667 if (ifa->ifa_addr->sa_family == AF_LINK)
670 /* XXX: Ugly!! ad hoc just for INET */
671 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
672 struct ifaliasreq ifr;
673 #ifdef IFADDR_DEBUG_VERBOSE
676 kprintf("purge in4 addr %p: ", ifa);
677 for (i = 0; i < ncpus; ++i)
678 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
682 bzero(&ifr, sizeof ifr);
683 ifr.ifra_addr = *ifa->ifa_addr;
684 if (ifa->ifa_dstaddr)
685 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
686 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
692 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
693 #ifdef IFADDR_DEBUG_VERBOSE
696 kprintf("purge in6 addr %p: ", ifa);
697 for (i = 0; i < ncpus; ++i)
698 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
703 /* ifp_addrhead is already updated */
707 ifa_ifunlink(ifa, ifp);
713 * Detach an interface, removing it from the
714 * list of "active" interfaces.
717 if_detach(struct ifnet *ifp)
719 struct radix_node_head *rnh;
724 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
727 * Remove routes and flush queues.
730 #ifdef DEVICE_POLLING
731 if (ifp->if_flags & IFF_POLLING)
732 ether_poll_deregister(ifp);
735 if (ifp->if_flags & IFF_NPOLLING)
736 ifpoll_deregister(ifp);
741 if (ifq_is_enabled(&ifp->if_snd))
742 altq_disable(&ifp->if_snd);
743 if (ifq_is_attached(&ifp->if_snd))
744 altq_detach(&ifp->if_snd);
748 * Clean up all addresses.
750 ifp->if_lladdr = NULL;
752 if_purgeaddrs_nolink(ifp);
753 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
756 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
757 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
758 ("non-link ifaddr is left on if_addrheads"));
760 ifa_ifunlink(ifa, ifp);
762 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
763 ("there are still ifaddrs left on if_addrheads"));
768 * Remove all IPv4 kernel structures related to ifp.
775 * Remove all IPv6 kernel structs related to ifp. This should be done
776 * before removing routing entries below, since IPv6 interface direct
777 * routes are expected to be removed by the IPv6-specific kernel API.
778 * Otherwise, the kernel will detect some inconsistency and bark it.
784 * Delete all remaining routes using this interface
785 * Unfortuneatly the only way to do this is to slog through
786 * the entire routing table looking for routes which point
787 * to this interface...oh well...
790 for (cpu = 0; cpu < ncpus2; cpu++) {
791 lwkt_migratecpu(cpu);
792 for (i = 1; i <= AF_MAX; i++) {
793 if ((rnh = rt_tables[cpu][i]) == NULL)
795 rnh->rnh_walktree(rnh, if_rtdel, ifp);
798 lwkt_migratecpu(origcpu);
800 /* Announce that the interface is gone. */
801 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
802 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
804 SLIST_FOREACH(dp, &domains, dom_next)
805 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
806 (*dp->dom_ifdetach)(ifp,
807 ifp->if_afdata[dp->dom_family]);
810 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
812 ifindex2ifnet[ifp->if_index] = NULL;
813 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
816 TAILQ_REMOVE(&ifnet, ifp, if_link);
817 kfree(ifp->if_addrheads, M_IFADDR);
818 kfree(ifp->if_start_nmsg, M_LWKTMSG);
823 * Create interface group without members
826 if_creategroup(const char *groupname)
828 struct ifg_group *ifg = NULL;
830 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
831 M_TEMP, M_NOWAIT)) == NULL)
834 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
836 ifg->ifg_carp_demoted = 0;
837 TAILQ_INIT(&ifg->ifg_members);
839 pfi_attach_ifgroup(ifg);
841 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
847 * Add a group to an interface
850 if_addgroup(struct ifnet *ifp, const char *groupname)
852 struct ifg_list *ifgl;
853 struct ifg_group *ifg = NULL;
854 struct ifg_member *ifgm;
856 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
857 groupname[strlen(groupname) - 1] <= '9')
860 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
861 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
864 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
867 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
872 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
873 if (!strcmp(ifg->ifg_group, groupname))
876 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
883 ifgl->ifgl_group = ifg;
884 ifgm->ifgm_ifp = ifp;
886 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
887 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
890 pfi_group_change(groupname);
897 * Remove a group from an interface
900 if_delgroup(struct ifnet *ifp, const char *groupname)
902 struct ifg_list *ifgl;
903 struct ifg_member *ifgm;
905 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
906 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
911 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
913 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
914 if (ifgm->ifgm_ifp == ifp)
918 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
922 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
923 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
925 pfi_detach_ifgroup(ifgl->ifgl_group);
927 kfree(ifgl->ifgl_group, M_TEMP);
933 pfi_group_change(groupname);
940 * Stores all groups from an interface in memory pointed
944 if_getgroup(caddr_t data, struct ifnet *ifp)
947 struct ifg_list *ifgl;
948 struct ifg_req ifgrq, *ifgp;
949 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
951 if (ifgr->ifgr_len == 0) {
952 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
953 ifgr->ifgr_len += sizeof(struct ifg_req);
957 len = ifgr->ifgr_len;
958 ifgp = ifgr->ifgr_groups;
959 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
960 if (len < sizeof(ifgrq))
962 bzero(&ifgrq, sizeof ifgrq);
963 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
964 sizeof(ifgrq.ifgrq_group));
965 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
966 sizeof(struct ifg_req))))
968 len -= sizeof(ifgrq);
976 * Stores all members of a group in memory pointed to by data
979 if_getgroupmembers(caddr_t data)
981 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
982 struct ifg_group *ifg;
983 struct ifg_member *ifgm;
984 struct ifg_req ifgrq, *ifgp;
987 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
988 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
993 if (ifgr->ifgr_len == 0) {
994 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
995 ifgr->ifgr_len += sizeof(ifgrq);
999 len = ifgr->ifgr_len;
1000 ifgp = ifgr->ifgr_groups;
1001 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1002 if (len < sizeof(ifgrq))
1004 bzero(&ifgrq, sizeof ifgrq);
1005 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1006 sizeof(ifgrq.ifgrq_member));
1007 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1008 sizeof(struct ifg_req))))
1010 len -= sizeof(ifgrq);
1018 * Delete Routes for a Network Interface
1020 * Called for each routing entry via the rnh->rnh_walktree() call above
1021 * to delete all route entries referencing a detaching network interface.
1024 * rn pointer to node in the routing table
1025 * arg argument passed to rnh->rnh_walktree() - detaching interface
1029 * errno failed - reason indicated
1033 if_rtdel(struct radix_node *rn, void *arg)
1035 struct rtentry *rt = (struct rtentry *)rn;
1036 struct ifnet *ifp = arg;
1039 if (rt->rt_ifp == ifp) {
1042 * Protect (sorta) against walktree recursion problems
1043 * with cloned routes
1045 if (!(rt->rt_flags & RTF_UP))
1048 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1049 rt_mask(rt), rt->rt_flags,
1052 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1060 * Locate an interface based on a complete address.
1063 ifa_ifwithaddr(struct sockaddr *addr)
1067 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1068 struct ifaddr_container *ifac;
1070 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1071 struct ifaddr *ifa = ifac->ifa;
1073 if (ifa->ifa_addr->sa_family != addr->sa_family)
1075 if (sa_equal(addr, ifa->ifa_addr))
1077 if ((ifp->if_flags & IFF_BROADCAST) &&
1078 ifa->ifa_broadaddr &&
1079 /* IPv6 doesn't have broadcast */
1080 ifa->ifa_broadaddr->sa_len != 0 &&
1081 sa_equal(ifa->ifa_broadaddr, addr))
1088 * Locate the point to point interface with a given destination address.
1091 ifa_ifwithdstaddr(struct sockaddr *addr)
1095 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1096 struct ifaddr_container *ifac;
1098 if (!(ifp->if_flags & IFF_POINTOPOINT))
1101 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1102 struct ifaddr *ifa = ifac->ifa;
1104 if (ifa->ifa_addr->sa_family != addr->sa_family)
1106 if (ifa->ifa_dstaddr &&
1107 sa_equal(addr, ifa->ifa_dstaddr))
1115 * Find an interface on a specific network. If many, choice
1116 * is most specific found.
1119 ifa_ifwithnet(struct sockaddr *addr)
1122 struct ifaddr *ifa_maybe = NULL;
1123 u_int af = addr->sa_family;
1124 char *addr_data = addr->sa_data, *cplim;
1127 * AF_LINK addresses can be looked up directly by their index number,
1128 * so do that if we can.
1130 if (af == AF_LINK) {
1131 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1133 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1134 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1138 * Scan though each interface, looking for ones that have
1139 * addresses in this address family.
1141 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1142 struct ifaddr_container *ifac;
1144 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1145 struct ifaddr *ifa = ifac->ifa;
1146 char *cp, *cp2, *cp3;
1148 if (ifa->ifa_addr->sa_family != af)
1150 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1152 * This is a bit broken as it doesn't
1153 * take into account that the remote end may
1154 * be a single node in the network we are
1156 * The trouble is that we don't know the
1157 * netmask for the remote end.
1159 if (ifa->ifa_dstaddr != NULL &&
1160 sa_equal(addr, ifa->ifa_dstaddr))
1164 * if we have a special address handler,
1165 * then use it instead of the generic one.
1167 if (ifa->ifa_claim_addr) {
1168 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1176 * Scan all the bits in the ifa's address.
1177 * If a bit dissagrees with what we are
1178 * looking for, mask it with the netmask
1179 * to see if it really matters.
1180 * (A byte at a time)
1182 if (ifa->ifa_netmask == 0)
1185 cp2 = ifa->ifa_addr->sa_data;
1186 cp3 = ifa->ifa_netmask->sa_data;
1187 cplim = ifa->ifa_netmask->sa_len +
1188 (char *)ifa->ifa_netmask;
1190 if ((*cp++ ^ *cp2++) & *cp3++)
1191 goto next; /* next address! */
1193 * If the netmask of what we just found
1194 * is more specific than what we had before
1195 * (if we had one) then remember the new one
1196 * before continuing to search
1197 * for an even better one.
1199 if (ifa_maybe == 0 ||
1200 rn_refines((char *)ifa->ifa_netmask,
1201 (char *)ifa_maybe->ifa_netmask))
1210 * Find an interface address specific to an interface best matching
1214 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1216 struct ifaddr_container *ifac;
1217 char *cp, *cp2, *cp3;
1219 struct ifaddr *ifa_maybe = 0;
1220 u_int af = addr->sa_family;
1224 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1225 struct ifaddr *ifa = ifac->ifa;
1227 if (ifa->ifa_addr->sa_family != af)
1231 if (ifa->ifa_netmask == NULL) {
1232 if (sa_equal(addr, ifa->ifa_addr) ||
1233 (ifa->ifa_dstaddr != NULL &&
1234 sa_equal(addr, ifa->ifa_dstaddr)))
1238 if (ifp->if_flags & IFF_POINTOPOINT) {
1239 if (sa_equal(addr, ifa->ifa_dstaddr))
1243 cp2 = ifa->ifa_addr->sa_data;
1244 cp3 = ifa->ifa_netmask->sa_data;
1245 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1246 for (; cp3 < cplim; cp3++)
1247 if ((*cp++ ^ *cp2++) & *cp3)
1257 * Default action when installing a route with a Link Level gateway.
1258 * Lookup an appropriate real ifa to point to.
1259 * This should be moved to /sys/net/link.c eventually.
1262 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1265 struct sockaddr *dst;
1268 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1269 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1271 ifa = ifaof_ifpforaddr(dst, ifp);
1273 IFAFREE(rt->rt_ifa);
1276 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1277 ifa->ifa_rtrequest(cmd, rt, info);
1282 * Mark an interface down and notify protocols of
1284 * NOTE: must be called at splnet or eqivalent.
1287 if_unroute(struct ifnet *ifp, int flag, int fam)
1289 struct ifaddr_container *ifac;
1291 ifp->if_flags &= ~flag;
1292 getmicrotime(&ifp->if_lastchange);
1293 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1294 struct ifaddr *ifa = ifac->ifa;
1296 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1297 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1299 ifq_purge(&ifp->if_snd);
1304 * Mark an interface up and notify protocols of
1306 * NOTE: must be called at splnet or eqivalent.
1309 if_route(struct ifnet *ifp, int flag, int fam)
1311 struct ifaddr_container *ifac;
1313 ifq_purge(&ifp->if_snd);
1314 ifp->if_flags |= flag;
1315 getmicrotime(&ifp->if_lastchange);
1316 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1317 struct ifaddr *ifa = ifac->ifa;
1319 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1320 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1329 * Mark an interface down and notify protocols of the transition. An
1330 * interface going down is also considered to be a synchronizing event.
1331 * We must ensure that all packet processing related to the interface
1332 * has completed before we return so e.g. the caller can free the ifnet
1333 * structure that the mbufs may be referencing.
1335 * NOTE: must be called at splnet or eqivalent.
1338 if_down(struct ifnet *ifp)
1340 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1341 netmsg_service_sync();
1345 * Mark an interface up and notify protocols of
1347 * NOTE: must be called at splnet or eqivalent.
1350 if_up(struct ifnet *ifp)
1352 if_route(ifp, IFF_UP, AF_UNSPEC);
1356 * Process a link state change.
1357 * NOTE: must be called at splsoftnet or equivalent.
1360 if_link_state_change(struct ifnet *ifp)
1362 int link_state = ifp->if_link_state;
1365 devctl_notify("IFNET", ifp->if_xname,
1366 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1370 * Handle interface watchdog timer routines. Called
1371 * from softclock, we decrement timers (if set) and
1372 * call the appropriate interface routine on expiration.
1375 if_slowtimo(void *arg)
1381 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1382 if (ifp->if_timer == 0 || --ifp->if_timer)
1384 if (ifp->if_watchdog) {
1385 if (ifnet_tryserialize_all(ifp)) {
1386 (*ifp->if_watchdog)(ifp);
1387 ifnet_deserialize_all(ifp);
1389 /* try again next timeout */
1397 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1401 * Map interface name to
1402 * interface structure pointer.
1405 ifunit(const char *name)
1410 * Search all the interfaces for this name/number
1413 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1414 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1422 * Map interface name in a sockaddr_dl to
1423 * interface structure pointer.
1426 if_withname(struct sockaddr *sa)
1428 char ifname[IFNAMSIZ+1];
1429 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1431 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1432 (sdl->sdl_nlen > IFNAMSIZ) )
1436 * ifunit wants a null-terminated name. It may not be null-terminated
1437 * in the sockaddr. We don't want to change the caller's sockaddr,
1438 * and there might not be room to put the trailing null anyway, so we
1439 * make a local copy that we know we can null terminate safely.
1442 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1443 ifname[sdl->sdl_nlen] = '\0';
1444 return ifunit(ifname);
1452 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1463 size_t namelen, onamelen;
1464 char new_name[IFNAMSIZ];
1466 struct sockaddr_dl *sdl;
1471 return (ifconf(cmd, data, cred));
1476 ifr = (struct ifreq *)data;
1481 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1483 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1484 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1486 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1488 return (if_clone_destroy(ifr->ifr_name));
1489 case SIOCIFGCLONERS:
1490 return (if_clone_list((struct if_clonereq *)data));
1496 * Nominal ioctl through interface, lookup the ifp and obtain a
1497 * lock to serialize the ifconfig ioctl operation.
1499 ifp = ifunit(ifr->ifr_name);
1503 mtx_lock(&ifp->if_ioctl_mtx);
1507 ifr->ifr_index = ifp->if_index;
1511 ifr->ifr_flags = ifp->if_flags;
1512 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1516 ifr->ifr_reqcap = ifp->if_capabilities;
1517 ifr->ifr_curcap = ifp->if_capenable;
1521 ifr->ifr_metric = ifp->if_metric;
1525 ifr->ifr_mtu = ifp->if_mtu;
1529 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1530 sizeof(ifp->if_data));
1534 ifr->ifr_phys = ifp->if_physical;
1537 case SIOCGIFPOLLCPU:
1538 #ifdef DEVICE_POLLING
1539 ifr->ifr_pollcpu = ifp->if_poll_cpuid;
1541 ifr->ifr_pollcpu = -1;
1545 case SIOCSIFPOLLCPU:
1546 #ifdef DEVICE_POLLING
1547 if ((ifp->if_flags & IFF_POLLING) == 0)
1548 ether_pollcpu_register(ifp, ifr->ifr_pollcpu);
1553 error = priv_check_cred(cred, PRIV_ROOT, 0);
1556 new_flags = (ifr->ifr_flags & 0xffff) |
1557 (ifr->ifr_flagshigh << 16);
1558 if (ifp->if_flags & IFF_SMART) {
1559 /* Smart drivers twiddle their own routes */
1560 } else if (ifp->if_flags & IFF_UP &&
1561 (new_flags & IFF_UP) == 0) {
1565 } else if (new_flags & IFF_UP &&
1566 (ifp->if_flags & IFF_UP) == 0) {
1572 #ifdef DEVICE_POLLING
1573 if ((new_flags ^ ifp->if_flags) & IFF_POLLING) {
1574 if (new_flags & IFF_POLLING) {
1575 ether_poll_register(ifp);
1577 ether_poll_deregister(ifp);
1581 #ifdef IFPOLL_ENABLE
1582 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1583 if (new_flags & IFF_NPOLLING)
1584 ifpoll_register(ifp);
1586 ifpoll_deregister(ifp);
1590 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1591 (new_flags &~ IFF_CANTCHANGE);
1592 if (new_flags & IFF_PPROMISC) {
1593 /* Permanently promiscuous mode requested */
1594 ifp->if_flags |= IFF_PROMISC;
1595 } else if (ifp->if_pcount == 0) {
1596 ifp->if_flags &= ~IFF_PROMISC;
1598 if (ifp->if_ioctl) {
1599 ifnet_serialize_all(ifp);
1600 ifp->if_ioctl(ifp, cmd, data, cred);
1601 ifnet_deserialize_all(ifp);
1603 getmicrotime(&ifp->if_lastchange);
1607 error = priv_check_cred(cred, PRIV_ROOT, 0);
1610 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1614 ifnet_serialize_all(ifp);
1615 ifp->if_ioctl(ifp, cmd, data, cred);
1616 ifnet_deserialize_all(ifp);
1620 error = priv_check_cred(cred, PRIV_ROOT, 0);
1623 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1626 if (new_name[0] == '\0') {
1630 if (ifunit(new_name) != NULL) {
1635 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1637 /* Announce the departure of the interface. */
1638 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1640 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1641 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1642 /* XXX IFA_LOCK(ifa); */
1643 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1644 namelen = strlen(new_name);
1645 onamelen = sdl->sdl_nlen;
1647 * Move the address if needed. This is safe because we
1648 * allocate space for a name of length IFNAMSIZ when we
1649 * create this in if_attach().
1651 if (namelen != onamelen) {
1652 bcopy(sdl->sdl_data + onamelen,
1653 sdl->sdl_data + namelen, sdl->sdl_alen);
1655 bcopy(new_name, sdl->sdl_data, namelen);
1656 sdl->sdl_nlen = namelen;
1657 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1658 bzero(sdl->sdl_data, onamelen);
1659 while (namelen != 0)
1660 sdl->sdl_data[--namelen] = 0xff;
1661 /* XXX IFA_UNLOCK(ifa) */
1663 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1665 /* Announce the return of the interface. */
1666 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1670 error = priv_check_cred(cred, PRIV_ROOT, 0);
1673 ifp->if_metric = ifr->ifr_metric;
1674 getmicrotime(&ifp->if_lastchange);
1678 error = priv_check_cred(cred, PRIV_ROOT, 0);
1681 if (ifp->if_ioctl == NULL) {
1685 ifnet_serialize_all(ifp);
1686 error = ifp->if_ioctl(ifp, cmd, data, cred);
1687 ifnet_deserialize_all(ifp);
1689 getmicrotime(&ifp->if_lastchange);
1694 u_long oldmtu = ifp->if_mtu;
1696 error = priv_check_cred(cred, PRIV_ROOT, 0);
1699 if (ifp->if_ioctl == NULL) {
1703 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
1707 ifnet_serialize_all(ifp);
1708 error = ifp->if_ioctl(ifp, cmd, data, cred);
1709 ifnet_deserialize_all(ifp);
1711 getmicrotime(&ifp->if_lastchange);
1715 * If the link MTU changed, do network layer specific procedure.
1717 if (ifp->if_mtu != oldmtu) {
1727 error = priv_check_cred(cred, PRIV_ROOT, 0);
1731 /* Don't allow group membership on non-multicast interfaces. */
1732 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1737 /* Don't let users screw up protocols' entries. */
1738 if (ifr->ifr_addr.sa_family != AF_LINK) {
1743 if (cmd == SIOCADDMULTI) {
1744 struct ifmultiaddr *ifma;
1745 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1747 error = if_delmulti(ifp, &ifr->ifr_addr);
1750 getmicrotime(&ifp->if_lastchange);
1753 case SIOCSIFPHYADDR:
1754 case SIOCDIFPHYADDR:
1756 case SIOCSIFPHYADDR_IN6:
1758 case SIOCSLIFPHYADDR:
1760 case SIOCSIFGENERIC:
1761 error = priv_check_cred(cred, PRIV_ROOT, 0);
1764 if (ifp->if_ioctl == 0) {
1768 ifnet_serialize_all(ifp);
1769 error = ifp->if_ioctl(ifp, cmd, data, cred);
1770 ifnet_deserialize_all(ifp);
1772 getmicrotime(&ifp->if_lastchange);
1776 ifs = (struct ifstat *)data;
1777 ifs->ascii[0] = '\0';
1779 case SIOCGIFPSRCADDR:
1780 case SIOCGIFPDSTADDR:
1781 case SIOCGLIFPHYADDR:
1783 case SIOCGIFGENERIC:
1784 if (ifp->if_ioctl == NULL) {
1788 ifnet_serialize_all(ifp);
1789 error = ifp->if_ioctl(ifp, cmd, data, cred);
1790 ifnet_deserialize_all(ifp);
1794 error = priv_check_cred(cred, PRIV_ROOT, 0);
1797 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
1798 ifr->ifr_addr.sa_len);
1799 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
1803 oif_flags = ifp->if_flags;
1804 if (so->so_proto == 0) {
1809 error = so_pru_control_direct(so, cmd, data, ifp);
1814 case SIOCSIFDSTADDR:
1816 case SIOCSIFBRDADDR:
1817 case SIOCSIFNETMASK:
1818 #if BYTE_ORDER != BIG_ENDIAN
1819 if (ifr->ifr_addr.sa_family == 0 &&
1820 ifr->ifr_addr.sa_len < 16) {
1821 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1822 ifr->ifr_addr.sa_len = 16;
1825 if (ifr->ifr_addr.sa_len == 0)
1826 ifr->ifr_addr.sa_len = 16;
1832 case OSIOCGIFDSTADDR:
1833 cmd = SIOCGIFDSTADDR;
1835 case OSIOCGIFBRDADDR:
1836 cmd = SIOCGIFBRDADDR;
1838 case OSIOCGIFNETMASK:
1839 cmd = SIOCGIFNETMASK;
1845 error = so_pru_control_direct(so, cmd, data, ifp);
1849 case OSIOCGIFDSTADDR:
1850 case OSIOCGIFBRDADDR:
1851 case OSIOCGIFNETMASK:
1852 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1855 #endif /* COMPAT_43 */
1857 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1859 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1860 if (ifp->if_flags & IFF_UP) {
1870 mtx_unlock(&ifp->if_ioctl_mtx);
1875 * Set/clear promiscuous mode on interface ifp based on the truth value
1876 * of pswitch. The calls are reference counted so that only the first
1877 * "on" request actually has an effect, as does the final "off" request.
1878 * Results are undefined if the "off" and "on" requests are not matched.
1881 ifpromisc(struct ifnet *ifp, int pswitch)
1887 oldflags = ifp->if_flags;
1888 if (ifp->if_flags & IFF_PPROMISC) {
1889 /* Do nothing if device is in permanently promiscuous mode */
1890 ifp->if_pcount += pswitch ? 1 : -1;
1895 * If the device is not configured up, we cannot put it in
1898 if ((ifp->if_flags & IFF_UP) == 0)
1900 if (ifp->if_pcount++ != 0)
1902 ifp->if_flags |= IFF_PROMISC;
1903 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1906 if (--ifp->if_pcount > 0)
1908 ifp->if_flags &= ~IFF_PROMISC;
1909 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1912 ifr.ifr_flags = ifp->if_flags;
1913 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1914 ifnet_serialize_all(ifp);
1915 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
1916 ifnet_deserialize_all(ifp);
1920 ifp->if_flags = oldflags;
1925 * Return interface configuration
1926 * of system. List may be used
1927 * in later ioctl's (above) to get
1928 * other information.
1931 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
1933 struct ifconf *ifc = (struct ifconf *)data;
1935 struct sockaddr *sa;
1936 struct ifreq ifr, *ifrp;
1937 int space = ifc->ifc_len, error = 0;
1939 ifrp = ifc->ifc_req;
1940 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1941 struct ifaddr_container *ifac;
1944 if (space <= sizeof ifr)
1948 * Zero the stack declared structure first to prevent
1949 * memory disclosure.
1951 bzero(&ifr, sizeof(ifr));
1952 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1953 >= sizeof(ifr.ifr_name)) {
1954 error = ENAMETOOLONG;
1959 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1960 struct ifaddr *ifa = ifac->ifa;
1962 if (space <= sizeof ifr)
1965 if (cred->cr_prison &&
1966 prison_if(cred, sa))
1970 if (cmd == OSIOCGIFCONF) {
1971 struct osockaddr *osa =
1972 (struct osockaddr *)&ifr.ifr_addr;
1974 osa->sa_family = sa->sa_family;
1975 error = copyout(&ifr, ifrp, sizeof ifr);
1979 if (sa->sa_len <= sizeof(*sa)) {
1981 error = copyout(&ifr, ifrp, sizeof ifr);
1984 if (space < (sizeof ifr) + sa->sa_len -
1987 space -= sa->sa_len - sizeof(*sa);
1988 error = copyout(&ifr, ifrp,
1989 sizeof ifr.ifr_name);
1991 error = copyout(sa, &ifrp->ifr_addr,
1993 ifrp = (struct ifreq *)
1994 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1998 space -= sizeof ifr;
2003 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2004 error = copyout(&ifr, ifrp, sizeof ifr);
2007 space -= sizeof ifr;
2011 ifc->ifc_len -= space;
2016 * Just like if_promisc(), but for all-multicast-reception mode.
2019 if_allmulti(struct ifnet *ifp, int onswitch)
2027 if (ifp->if_amcount++ == 0) {
2028 ifp->if_flags |= IFF_ALLMULTI;
2029 ifr.ifr_flags = ifp->if_flags;
2030 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2031 ifnet_serialize_all(ifp);
2032 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2034 ifnet_deserialize_all(ifp);
2037 if (ifp->if_amcount > 1) {
2040 ifp->if_amcount = 0;
2041 ifp->if_flags &= ~IFF_ALLMULTI;
2042 ifr.ifr_flags = ifp->if_flags;
2043 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2044 ifnet_serialize_all(ifp);
2045 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2047 ifnet_deserialize_all(ifp);
2059 * Add a multicast listenership to the interface in question.
2060 * The link layer provides a routine which converts
2064 struct ifnet *ifp, /* interface to manipulate */
2065 struct sockaddr *sa, /* address to add */
2066 struct ifmultiaddr **retifma)
2068 struct sockaddr *llsa, *dupsa;
2070 struct ifmultiaddr *ifma;
2073 * If the matching multicast address already exists
2074 * then don't add a new one, just add a reference
2076 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2077 if (sa_equal(sa, ifma->ifma_addr)) {
2078 ifma->ifma_refcount++;
2086 * Give the link layer a chance to accept/reject it, and also
2087 * find out which AF_LINK address this maps to, if it isn't one
2090 if (ifp->if_resolvemulti) {
2091 ifnet_serialize_all(ifp);
2092 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2093 ifnet_deserialize_all(ifp);
2100 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
2101 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
2102 bcopy(sa, dupsa, sa->sa_len);
2104 ifma->ifma_addr = dupsa;
2105 ifma->ifma_lladdr = llsa;
2106 ifma->ifma_ifp = ifp;
2107 ifma->ifma_refcount = 1;
2108 ifma->ifma_protospec = 0;
2109 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2112 * Some network interfaces can scan the address list at
2113 * interrupt time; lock them out.
2116 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2122 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2123 if (sa_equal(ifma->ifma_addr, llsa))
2127 ifma->ifma_refcount++;
2129 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
2130 M_IFMADDR, M_WAITOK);
2131 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
2132 M_IFMADDR, M_WAITOK);
2133 bcopy(llsa, dupsa, llsa->sa_len);
2134 ifma->ifma_addr = dupsa;
2135 ifma->ifma_ifp = ifp;
2136 ifma->ifma_refcount = 1;
2138 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2143 * We are certain we have added something, so call down to the
2144 * interface to let them know about it.
2147 ifnet_serialize_all(ifp);
2149 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2150 ifnet_deserialize_all(ifp);
2157 * Remove a reference to a multicast address on this interface. Yell
2158 * if the request does not match an existing membership.
2161 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2163 struct ifmultiaddr *ifma;
2165 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2166 if (sa_equal(sa, ifma->ifma_addr))
2171 if (ifma->ifma_refcount > 1) {
2172 ifma->ifma_refcount--;
2176 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2177 sa = ifma->ifma_lladdr;
2179 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2181 * Make sure the interface driver is notified
2182 * in the case of a link layer mcast group being left.
2184 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) {
2185 ifnet_serialize_all(ifp);
2186 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2187 ifnet_deserialize_all(ifp);
2190 kfree(ifma->ifma_addr, M_IFMADDR);
2191 kfree(ifma, M_IFMADDR);
2196 * Now look for the link-layer address which corresponds to
2197 * this network address. It had been squirreled away in
2198 * ifma->ifma_lladdr for this purpose (so we don't have
2199 * to call ifp->if_resolvemulti() again), and we saved that
2200 * value in sa above. If some nasty deleted the
2201 * link-layer address out from underneath us, we can deal because
2202 * the address we stored was is not the same as the one which was
2203 * in the record for the link-layer address. (So we don't complain
2206 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2207 if (sa_equal(sa, ifma->ifma_addr))
2212 if (ifma->ifma_refcount > 1) {
2213 ifma->ifma_refcount--;
2218 ifnet_serialize_all(ifp);
2219 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2220 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2221 ifnet_deserialize_all(ifp);
2223 kfree(ifma->ifma_addr, M_IFMADDR);
2224 kfree(sa, M_IFMADDR);
2225 kfree(ifma, M_IFMADDR);
2231 * Delete all multicast group membership for an interface.
2232 * Should be used to quickly flush all multicast filters.
2235 if_delallmulti(struct ifnet *ifp)
2237 struct ifmultiaddr *ifma;
2238 struct ifmultiaddr *next;
2240 TAILQ_FOREACH_MUTABLE(ifma, &ifp->if_multiaddrs, ifma_link, next)
2241 if_delmulti(ifp, ifma->ifma_addr);
2246 * Set the link layer address on an interface.
2248 * At this time we only support certain types of interfaces,
2249 * and we don't allow the length of the address to change.
2252 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2254 struct sockaddr_dl *sdl;
2257 sdl = IF_LLSOCKADDR(ifp);
2260 if (len != sdl->sdl_alen) /* don't allow length to change */
2262 switch (ifp->if_type) {
2263 case IFT_ETHER: /* these types use struct arpcom */
2266 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2267 bcopy(lladdr, LLADDR(sdl), len);
2273 * If the interface is already up, we need
2274 * to re-init it in order to reprogram its
2277 ifnet_serialize_all(ifp);
2278 if ((ifp->if_flags & IFF_UP) != 0) {
2280 struct ifaddr_container *ifac;
2283 ifp->if_flags &= ~IFF_UP;
2284 ifr.ifr_flags = ifp->if_flags;
2285 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2286 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2288 ifp->if_flags |= IFF_UP;
2289 ifr.ifr_flags = ifp->if_flags;
2290 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2291 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2295 * Also send gratuitous ARPs to notify other nodes about
2296 * the address change.
2298 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2299 struct ifaddr *ifa = ifac->ifa;
2301 if (ifa->ifa_addr != NULL &&
2302 ifa->ifa_addr->sa_family == AF_INET)
2303 arp_ifinit(ifp, ifa);
2307 ifnet_deserialize_all(ifp);
2311 struct ifmultiaddr *
2312 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2314 struct ifmultiaddr *ifma;
2316 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2317 if (sa_equal(ifma->ifma_addr, sa))
2324 * This function locates the first real ethernet MAC from a network
2325 * card and loads it into node, returning 0 on success or ENOENT if
2326 * no suitable interfaces were found. It is used by the uuid code to
2327 * generate a unique 6-byte number.
2330 if_getanyethermac(uint16_t *node, int minlen)
2333 struct sockaddr_dl *sdl;
2335 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2336 if (ifp->if_type != IFT_ETHER)
2338 sdl = IF_LLSOCKADDR(ifp);
2339 if (sdl->sdl_alen < minlen)
2341 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2349 * The name argument must be a pointer to storage which will last as
2350 * long as the interface does. For physical devices, the result of
2351 * device_get_name(dev) is a good choice and for pseudo-devices a
2352 * static string works well.
2355 if_initname(struct ifnet *ifp, const char *name, int unit)
2357 ifp->if_dname = name;
2358 ifp->if_dunit = unit;
2359 if (unit != IF_DUNIT_NONE)
2360 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2362 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2366 if_printf(struct ifnet *ifp, const char *fmt, ...)
2371 retval = kprintf("%s: ", ifp->if_xname);
2372 __va_start(ap, fmt);
2373 retval += kvprintf(fmt, ap);
2379 if_alloc(uint8_t type)
2385 * XXX temporary hack until arpcom is setup in if_l2com
2387 if (type == IFT_ETHER)
2388 size = sizeof(struct arpcom);
2390 size = sizeof(struct ifnet);
2392 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2394 ifp->if_type = type;
2396 if (if_com_alloc[type] != NULL) {
2397 ifp->if_l2com = if_com_alloc[type](type, ifp);
2398 if (ifp->if_l2com == NULL) {
2399 kfree(ifp, M_IFNET);
2407 if_free(struct ifnet *ifp)
2409 kfree(ifp, M_IFNET);
2413 ifq_set_classic(struct ifaltq *ifq)
2415 ifq->altq_enqueue = ifq_classic_enqueue;
2416 ifq->altq_dequeue = ifq_classic_dequeue;
2417 ifq->altq_request = ifq_classic_request;
2421 ifq_classic_enqueue(struct ifaltq *ifq, struct mbuf *m,
2422 struct altq_pktattr *pa __unused)
2424 logifq(enqueue, ifq);
2425 if (IF_QFULL(ifq)) {
2435 ifq_classic_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
2444 logifq(dequeue, ifq);
2448 panic("unsupported ALTQ dequeue op: %d", op);
2450 KKASSERT(mpolled == NULL || mpolled == m);
2455 ifq_classic_request(struct ifaltq *ifq, int req, void *arg)
2462 panic("unsupported ALTQ request: %d", req);
2468 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2470 struct ifaltq *ifq = &ifp->if_snd;
2471 int running = 0, error, start = 0;
2473 ASSERT_IFNET_NOT_SERIALIZED_TX(ifp);
2476 error = ifq_enqueue_locked(ifq, m, pa);
2481 if (!ifq->altq_started) {
2483 * Hold the interlock of ifnet.if_start
2485 ifq->altq_started = 1;
2490 ifp->if_obytes += m->m_pkthdr.len;
2491 if (m->m_flags & M_MCAST)
2495 logifstart(avoid, ifp);
2499 if (ifq_dispatch_schedonly) {
2501 * Always schedule ifnet.if_start on ifnet's CPU,
2502 * short circuit the rest of this function.
2504 logifstart(sched, ifp);
2505 if_start_schedule(ifp);
2510 * Try to do direct ifnet.if_start first, if there is
2511 * contention on ifnet's serializer, ifnet.if_start will
2512 * be scheduled on ifnet's CPU.
2514 if (!ifnet_tryserialize_tx(ifp)) {
2516 * ifnet serializer contention happened,
2517 * ifnet.if_start is scheduled on ifnet's
2518 * CPU, and we keep going.
2520 logifstart(contend_sched, ifp);
2521 if_start_schedule(ifp);
2525 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
2526 logifstart(run, ifp);
2528 if ((ifp->if_flags &
2529 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
2533 ifnet_deserialize_tx(ifp);
2535 if (ifq_dispatch_schednochk || if_start_need_schedule(ifq, running)) {
2537 * More data need to be transmitted, ifnet.if_start is
2538 * scheduled on ifnet's CPU, and we keep going.
2539 * NOTE: ifnet.if_start interlock is not released.
2541 logifstart(sched, ifp);
2542 if_start_schedule(ifp);
2548 ifa_create(int size, int flags)
2553 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small\n"));
2555 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
2559 ifa->ifa_containers = kmalloc(ncpus * sizeof(struct ifaddr_container),
2560 M_IFADDR, M_WAITOK | M_ZERO);
2561 ifa->ifa_ncnt = ncpus;
2562 for (i = 0; i < ncpus; ++i) {
2563 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
2565 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
2567 ifac->ifa_refcnt = 1;
2570 kprintf("alloc ifa %p %d\n", ifa, size);
2576 ifac_free(struct ifaddr_container *ifac, int cpu_id)
2578 struct ifaddr *ifa = ifac->ifa;
2580 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
2581 KKASSERT(ifac->ifa_refcnt == 0);
2582 KASSERT(ifac->ifa_listmask == 0,
2583 ("ifa is still on %#x lists\n", ifac->ifa_listmask));
2585 ifac->ifa_magic = IFA_CONTAINER_DEAD;
2587 #ifdef IFADDR_DEBUG_VERBOSE
2588 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
2591 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
2592 ("invalid # of ifac, %d\n", ifa->ifa_ncnt));
2593 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
2595 kprintf("free ifa %p\n", ifa);
2597 kfree(ifa->ifa_containers, M_IFADDR);
2598 kfree(ifa, M_IFADDR);
2603 ifa_iflink_dispatch(netmsg_t nmsg)
2605 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2606 struct ifaddr *ifa = msg->ifa;
2607 struct ifnet *ifp = msg->ifp;
2609 struct ifaddr_container *ifac;
2613 ifac = &ifa->ifa_containers[cpu];
2614 ASSERT_IFAC_VALID(ifac);
2615 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
2616 ("ifaddr is on if_addrheads\n"));
2618 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
2620 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
2622 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
2626 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2630 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
2632 struct netmsg_ifaddr msg;
2634 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2635 0, ifa_iflink_dispatch);
2640 ifa_domsg(&msg.base.lmsg, 0);
2644 ifa_ifunlink_dispatch(netmsg_t nmsg)
2646 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2647 struct ifaddr *ifa = msg->ifa;
2648 struct ifnet *ifp = msg->ifp;
2650 struct ifaddr_container *ifac;
2654 ifac = &ifa->ifa_containers[cpu];
2655 ASSERT_IFAC_VALID(ifac);
2656 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
2657 ("ifaddr is not on if_addrhead\n"));
2659 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
2660 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
2664 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2668 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
2670 struct netmsg_ifaddr msg;
2672 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2673 0, ifa_ifunlink_dispatch);
2677 ifa_domsg(&msg.base.lmsg, 0);
2681 ifa_destroy_dispatch(netmsg_t nmsg)
2683 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2686 ifa_forwardmsg(&nmsg->lmsg, mycpuid + 1);
2690 ifa_destroy(struct ifaddr *ifa)
2692 struct netmsg_ifaddr msg;
2694 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2695 0, ifa_destroy_dispatch);
2698 ifa_domsg(&msg.base.lmsg, 0);
2702 ifnet_portfn(int cpu)
2704 return &ifnet_threads[cpu].td_msgport;
2708 ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
2710 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
2712 if (next_cpu < ncpus)
2713 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
2715 lwkt_replymsg(lmsg, 0);
2719 ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
2721 KKASSERT(cpu < ncpus);
2722 return lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
2726 ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
2728 KKASSERT(cpu < ncpus);
2729 lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
2733 * Generic netmsg service loop. Some protocols may roll their own but all
2734 * must do the basic command dispatch function call done here.
2737 ifnet_service_loop(void *arg __unused)
2741 while ((msg = lwkt_waitport(&curthread->td_msgport, 0))) {
2742 KASSERT(msg->base.nm_dispatch, ("ifnet_service: badmsg"));
2743 msg->base.nm_dispatch(msg);
2748 ifnetinit(void *dummy __unused)
2752 for (i = 0; i < ncpus; ++i) {
2753 struct thread *thr = &ifnet_threads[i];
2755 lwkt_create(ifnet_service_loop, NULL, NULL,
2756 thr, TDF_STOPREQ, i, "ifnet %d", i);
2757 netmsg_service_port_init(&thr->td_msgport);
2763 ifnet_byindex(unsigned short idx)
2767 return ifindex2ifnet[idx];
2771 ifaddr_byindex(unsigned short idx)
2775 ifp = ifnet_byindex(idx);
2778 return TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
2782 if_register_com_alloc(u_char type,
2783 if_com_alloc_t *a, if_com_free_t *f)
2786 KASSERT(if_com_alloc[type] == NULL,
2787 ("if_register_com_alloc: %d already registered", type));
2788 KASSERT(if_com_free[type] == NULL,
2789 ("if_register_com_alloc: %d free already registered", type));
2791 if_com_alloc[type] = a;
2792 if_com_free[type] = f;
2796 if_deregister_com_alloc(u_char type)
2799 KASSERT(if_com_alloc[type] != NULL,
2800 ("if_deregister_com_alloc: %d not registered", type));
2801 KASSERT(if_com_free[type] != NULL,
2802 ("if_deregister_com_alloc: %d free not registered", type));
2803 if_com_alloc[type] = NULL;
2804 if_com_free[type] = NULL;