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 struct callout if_slowtimo_timer;
147 struct ifnet **ifindex2ifnet = NULL;
148 static struct thread ifnet_threads[MAXCPU];
150 #define IFQ_KTR_STRING "ifq=%p"
151 #define IFQ_KTR_ARGS struct ifaltq *ifq
153 #define KTR_IFQ KTR_ALL
155 KTR_INFO_MASTER(ifq);
156 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARGS);
157 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARGS);
158 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
160 #define IF_START_KTR_STRING "ifp=%p"
161 #define IF_START_KTR_ARGS struct ifnet *ifp
163 #define KTR_IF_START KTR_ALL
165 KTR_INFO_MASTER(if_start);
166 KTR_INFO(KTR_IF_START, if_start, run, 0,
167 IF_START_KTR_STRING, IF_START_KTR_ARGS);
168 KTR_INFO(KTR_IF_START, if_start, sched, 1,
169 IF_START_KTR_STRING, IF_START_KTR_ARGS);
170 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
171 IF_START_KTR_STRING, IF_START_KTR_ARGS);
172 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
173 IF_START_KTR_STRING, IF_START_KTR_ARGS);
175 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
176 IF_START_KTR_STRING, IF_START_KTR_ARGS);
178 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
180 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
183 * Network interface utility routines.
185 * Routines with ifa_ifwith* names take sockaddr *'s as
194 callout_init(&if_slowtimo_timer);
197 TAILQ_FOREACH(ifp, &ifnet, if_link) {
198 if (ifp->if_snd.ifq_maxlen == 0) {
199 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
200 ifp->if_snd.ifq_maxlen = ifqmaxlen;
209 if_start_cpuid(struct ifnet *ifp)
211 return ifp->if_cpuid;
214 #ifdef DEVICE_POLLING
216 if_start_cpuid_poll(struct ifnet *ifp)
218 int poll_cpuid = ifp->if_poll_cpuid;
223 return ifp->if_cpuid;
229 if_start_cpuid_npoll(struct ifnet *ifp)
231 int poll_cpuid = ifp->if_npoll_cpuid;
236 return ifp->if_cpuid;
241 if_start_ipifunc(void *arg)
243 struct ifnet *ifp = arg;
244 struct lwkt_msg *lmsg = &ifp->if_start_nmsg[mycpuid].lmsg;
247 if (lmsg->ms_flags & MSGF_DONE)
248 lwkt_sendmsg(netisr_portfn(mycpuid), lmsg);
253 * Schedule ifnet.if_start on ifnet's CPU
256 if_start_schedule(struct ifnet *ifp)
261 cpu = ifp->if_start_cpuid(ifp);
263 lwkt_send_ipiq(globaldata_find(cpu), if_start_ipifunc, ifp);
266 if_start_ipifunc(ifp);
271 * This function will release ifnet.if_start interlock,
272 * if ifnet.if_start does not need to be scheduled
275 if_start_need_schedule(struct ifaltq *ifq, int running)
277 if (!running || ifq_is_empty(ifq)
279 || ifq->altq_tbr != NULL
284 * ifnet.if_start interlock is released, if:
285 * 1) Hardware can not take any packets, due to
286 * o interface is marked down
287 * o hardware queue is full (IFF_OACTIVE)
288 * Under the second situation, hardware interrupt
289 * or polling(4) will call/schedule ifnet.if_start
290 * when hardware queue is ready
291 * 2) There is not packet in the ifnet.if_snd.
292 * Further ifq_dispatch or ifq_handoff will call/
293 * schedule ifnet.if_start
294 * 3) TBR is used and it does not allow further
296 * TBR callout will call ifnet.if_start
298 if (!running || !ifq_data_ready(ifq)) {
299 ifq->altq_started = 0;
309 if_start_dispatch(netmsg_t msg)
311 struct lwkt_msg *lmsg = &msg->base.lmsg;
312 struct ifnet *ifp = lmsg->u.ms_resultp;
313 struct ifaltq *ifq = &ifp->if_snd;
317 lwkt_replymsg(lmsg, 0); /* reply ASAP */
321 if (mycpuid != ifp->if_start_cpuid(ifp)) {
323 * If the ifnet is still up, we need to
324 * chase its CPU change.
326 if (ifp->if_flags & IFF_UP) {
327 logifstart(chase_sched, ifp);
328 if_start_schedule(ifp);
336 if (ifp->if_flags & IFF_UP) {
337 ifnet_serialize_tx(ifp); /* XXX try? */
338 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
339 logifstart(run, ifp);
342 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
345 ifnet_deserialize_tx(ifp);
350 if (if_start_need_schedule(ifq, running)) {
352 if (lmsg->ms_flags & MSGF_DONE) { /* XXX necessary? */
353 logifstart(sched, ifp);
354 lwkt_sendmsg(netisr_portfn(mycpuid), lmsg);
360 /* Device driver ifnet.if_start helper function */
362 if_devstart(struct ifnet *ifp)
364 struct ifaltq *ifq = &ifp->if_snd;
367 ASSERT_IFNET_SERIALIZED_TX(ifp);
370 if (ifq->altq_started || !ifq_data_ready(ifq)) {
371 logifstart(avoid, ifp);
375 ifq->altq_started = 1;
378 logifstart(run, ifp);
381 if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
384 if (if_start_need_schedule(ifq, running)) {
386 * More data need to be transmitted, ifnet.if_start is
387 * scheduled on ifnet's CPU, and we keep going.
388 * NOTE: ifnet.if_start interlock is not released.
390 logifstart(sched, ifp);
391 if_start_schedule(ifp);
396 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
398 lwkt_serialize_enter(ifp->if_serializer);
402 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
404 lwkt_serialize_exit(ifp->if_serializer);
408 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
410 return lwkt_serialize_try(ifp->if_serializer);
415 if_default_serialize_assert(struct ifnet *ifp,
416 enum ifnet_serialize slz __unused,
417 boolean_t serialized)
420 ASSERT_SERIALIZED(ifp->if_serializer);
422 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
427 * Attach an interface to the list of "active" interfaces.
429 * The serializer is optional. If non-NULL access to the interface
433 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
435 unsigned socksize, ifasize;
436 int namelen, masklen;
437 struct sockaddr_dl *sdl;
442 static int if_indexlim = 8;
444 if (ifp->if_serialize != NULL) {
445 KASSERT(ifp->if_deserialize != NULL &&
446 ifp->if_tryserialize != NULL &&
447 ifp->if_serialize_assert != NULL,
448 ("serialize functions are partially setup"));
451 * If the device supplies serialize functions,
452 * then clear if_serializer to catch any invalid
453 * usage of this field.
455 KASSERT(serializer == NULL,
456 ("both serialize functions and default serializer "
458 ifp->if_serializer = NULL;
460 KASSERT(ifp->if_deserialize == NULL &&
461 ifp->if_tryserialize == NULL &&
462 ifp->if_serialize_assert == NULL,
463 ("serialize functions are partially setup"));
464 ifp->if_serialize = if_default_serialize;
465 ifp->if_deserialize = if_default_deserialize;
466 ifp->if_tryserialize = if_default_tryserialize;
468 ifp->if_serialize_assert = if_default_serialize_assert;
472 * The serializer can be passed in from the device,
473 * allowing the same serializer to be used for both
474 * the interrupt interlock and the device queue.
475 * If not specified, the netif structure will use an
476 * embedded serializer.
478 if (serializer == NULL) {
479 serializer = &ifp->if_default_serializer;
480 lwkt_serialize_init(serializer);
482 ifp->if_serializer = serializer;
485 ifp->if_start_cpuid = if_start_cpuid;
488 #ifdef DEVICE_POLLING
489 /* Device is not in polling mode by default */
490 ifp->if_poll_cpuid = -1;
491 if (ifp->if_poll != NULL)
492 ifp->if_start_cpuid = if_start_cpuid_poll;
495 /* Device is not in polling mode by default */
496 ifp->if_npoll_cpuid = -1;
497 if (ifp->if_npoll != NULL)
498 ifp->if_start_cpuid = if_start_cpuid_npoll;
501 ifp->if_start_nmsg = kmalloc(ncpus * sizeof(*ifp->if_start_nmsg),
502 M_LWKTMSG, M_WAITOK);
503 for (i = 0; i < ncpus; ++i) {
504 netmsg_init(&ifp->if_start_nmsg[i], NULL, &netisr_adone_rport,
505 0, if_start_dispatch);
506 ifp->if_start_nmsg[i].lmsg.u.ms_resultp = ifp;
509 mtx_init(&ifp->if_ioctl_mtx);
510 mtx_lock(&ifp->if_ioctl_mtx);
512 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
513 ifp->if_index = ++if_index;
517 * The old code would work if the interface passed a pre-existing
518 * chain of ifaddrs to this code. We don't trust our callers to
519 * properly initialize the tailq, however, so we no longer allow
520 * this unlikely case.
522 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
523 M_IFADDR, M_WAITOK | M_ZERO);
524 for (i = 0; i < ncpus; ++i)
525 TAILQ_INIT(&ifp->if_addrheads[i]);
527 TAILQ_INIT(&ifp->if_prefixhead);
528 TAILQ_INIT(&ifp->if_multiaddrs);
529 TAILQ_INIT(&ifp->if_groups);
530 getmicrotime(&ifp->if_lastchange);
531 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
537 /* grow ifindex2ifnet */
538 n = if_indexlim * sizeof(*q);
539 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
541 bcopy(ifindex2ifnet, q, n/2);
542 kfree(ifindex2ifnet, M_IFADDR);
547 ifindex2ifnet[if_index] = ifp;
550 * create a Link Level name for this device
552 namelen = strlen(ifp->if_xname);
553 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
554 socksize = masklen + ifp->if_addrlen;
555 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
556 if (socksize < sizeof(*sdl))
557 socksize = sizeof(*sdl);
558 socksize = ROUNDUP(socksize);
560 ifasize = sizeof(struct ifaddr) + 2 * socksize;
561 ifa = ifa_create(ifasize, M_WAITOK);
562 sdl = (struct sockaddr_dl *)(ifa + 1);
563 sdl->sdl_len = socksize;
564 sdl->sdl_family = AF_LINK;
565 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
566 sdl->sdl_nlen = namelen;
567 sdl->sdl_index = ifp->if_index;
568 sdl->sdl_type = ifp->if_type;
569 ifp->if_lladdr = ifa;
571 ifa->ifa_rtrequest = link_rtrequest;
572 ifa->ifa_addr = (struct sockaddr *)sdl;
573 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
574 ifa->ifa_netmask = (struct sockaddr *)sdl;
575 sdl->sdl_len = masklen;
577 sdl->sdl_data[--namelen] = 0xff;
578 ifa_iflink(ifa, ifp, 0 /* Insert head */);
580 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
581 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
585 ifq->altq_disc = NULL;
586 ifq->altq_flags &= ALTQF_CANTCHANGE;
587 ifq->altq_tbr = NULL;
589 ifq->altq_started = 0;
590 ifq->altq_prepended = NULL;
592 ifq_set_classic(ifq);
594 if (!SLIST_EMPTY(&domains))
595 if_attachdomain1(ifp);
597 /* Announce the interface. */
598 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
600 mtx_unlock(&ifp->if_ioctl_mtx);
604 if_attachdomain(void *dummy)
609 TAILQ_FOREACH(ifp, &ifnet, if_list)
610 if_attachdomain1(ifp);
613 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
614 if_attachdomain, NULL);
617 if_attachdomain1(struct ifnet *ifp)
623 /* address family dependent data region */
624 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
625 SLIST_FOREACH(dp, &domains, dom_next)
626 if (dp->dom_ifattach)
627 ifp->if_afdata[dp->dom_family] =
628 (*dp->dom_ifattach)(ifp);
633 * Purge all addresses whose type is _not_ AF_LINK
636 if_purgeaddrs_nolink(struct ifnet *ifp)
638 struct ifaddr_container *ifac, *next;
640 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
642 struct ifaddr *ifa = ifac->ifa;
644 /* Leave link ifaddr as it is */
645 if (ifa->ifa_addr->sa_family == AF_LINK)
648 /* XXX: Ugly!! ad hoc just for INET */
649 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
650 struct ifaliasreq ifr;
651 #ifdef IFADDR_DEBUG_VERBOSE
654 kprintf("purge in4 addr %p: ", ifa);
655 for (i = 0; i < ncpus; ++i)
656 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
660 bzero(&ifr, sizeof ifr);
661 ifr.ifra_addr = *ifa->ifa_addr;
662 if (ifa->ifa_dstaddr)
663 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
664 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
670 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
671 #ifdef IFADDR_DEBUG_VERBOSE
674 kprintf("purge in6 addr %p: ", ifa);
675 for (i = 0; i < ncpus; ++i)
676 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
681 /* ifp_addrhead is already updated */
685 ifa_ifunlink(ifa, ifp);
691 * Detach an interface, removing it from the
692 * list of "active" interfaces.
695 if_detach(struct ifnet *ifp)
697 struct radix_node_head *rnh;
702 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
705 * Remove routes and flush queues.
708 #ifdef DEVICE_POLLING
709 if (ifp->if_flags & IFF_POLLING)
710 ether_poll_deregister(ifp);
713 if (ifp->if_flags & IFF_NPOLLING)
714 ifpoll_deregister(ifp);
719 if (ifq_is_enabled(&ifp->if_snd))
720 altq_disable(&ifp->if_snd);
721 if (ifq_is_attached(&ifp->if_snd))
722 altq_detach(&ifp->if_snd);
726 * Clean up all addresses.
728 ifp->if_lladdr = NULL;
730 if_purgeaddrs_nolink(ifp);
731 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
734 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
735 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
736 ("non-link ifaddr is left on if_addrheads"));
738 ifa_ifunlink(ifa, ifp);
740 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
741 ("there are still ifaddrs left on if_addrheads"));
746 * Remove all IPv4 kernel structures related to ifp.
753 * Remove all IPv6 kernel structs related to ifp. This should be done
754 * before removing routing entries below, since IPv6 interface direct
755 * routes are expected to be removed by the IPv6-specific kernel API.
756 * Otherwise, the kernel will detect some inconsistency and bark it.
762 * Delete all remaining routes using this interface
763 * Unfortuneatly the only way to do this is to slog through
764 * the entire routing table looking for routes which point
765 * to this interface...oh well...
768 for (cpu = 0; cpu < ncpus; cpu++) {
769 lwkt_migratecpu(cpu);
770 for (i = 1; i <= AF_MAX; i++) {
771 if ((rnh = rt_tables[cpu][i]) == NULL)
773 rnh->rnh_walktree(rnh, if_rtdel, ifp);
776 lwkt_migratecpu(origcpu);
778 /* Announce that the interface is gone. */
779 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
780 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
782 SLIST_FOREACH(dp, &domains, dom_next)
783 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
784 (*dp->dom_ifdetach)(ifp,
785 ifp->if_afdata[dp->dom_family]);
788 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
790 ifindex2ifnet[ifp->if_index] = NULL;
791 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
794 TAILQ_REMOVE(&ifnet, ifp, if_link);
795 kfree(ifp->if_addrheads, M_IFADDR);
796 kfree(ifp->if_start_nmsg, M_LWKTMSG);
801 * Create interface group without members
804 if_creategroup(const char *groupname)
806 struct ifg_group *ifg = NULL;
808 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
809 M_TEMP, M_NOWAIT)) == NULL)
812 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
814 ifg->ifg_carp_demoted = 0;
815 TAILQ_INIT(&ifg->ifg_members);
817 pfi_attach_ifgroup(ifg);
819 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
825 * Add a group to an interface
828 if_addgroup(struct ifnet *ifp, const char *groupname)
830 struct ifg_list *ifgl;
831 struct ifg_group *ifg = NULL;
832 struct ifg_member *ifgm;
834 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
835 groupname[strlen(groupname) - 1] <= '9')
838 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
839 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
842 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
845 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
850 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
851 if (!strcmp(ifg->ifg_group, groupname))
854 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
861 ifgl->ifgl_group = ifg;
862 ifgm->ifgm_ifp = ifp;
864 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
865 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
868 pfi_group_change(groupname);
875 * Remove a group from an interface
878 if_delgroup(struct ifnet *ifp, const char *groupname)
880 struct ifg_list *ifgl;
881 struct ifg_member *ifgm;
883 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
884 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
889 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
891 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
892 if (ifgm->ifgm_ifp == ifp)
896 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
900 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
901 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
903 pfi_detach_ifgroup(ifgl->ifgl_group);
905 kfree(ifgl->ifgl_group, M_TEMP);
911 pfi_group_change(groupname);
918 * Stores all groups from an interface in memory pointed
922 if_getgroup(caddr_t data, struct ifnet *ifp)
925 struct ifg_list *ifgl;
926 struct ifg_req ifgrq, *ifgp;
927 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
929 if (ifgr->ifgr_len == 0) {
930 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
931 ifgr->ifgr_len += sizeof(struct ifg_req);
935 len = ifgr->ifgr_len;
936 ifgp = ifgr->ifgr_groups;
937 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
938 if (len < sizeof(ifgrq))
940 bzero(&ifgrq, sizeof ifgrq);
941 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
942 sizeof(ifgrq.ifgrq_group));
943 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
944 sizeof(struct ifg_req))))
946 len -= sizeof(ifgrq);
954 * Stores all members of a group in memory pointed to by data
957 if_getgroupmembers(caddr_t data)
959 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
960 struct ifg_group *ifg;
961 struct ifg_member *ifgm;
962 struct ifg_req ifgrq, *ifgp;
965 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
966 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
971 if (ifgr->ifgr_len == 0) {
972 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
973 ifgr->ifgr_len += sizeof(ifgrq);
977 len = ifgr->ifgr_len;
978 ifgp = ifgr->ifgr_groups;
979 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
980 if (len < sizeof(ifgrq))
982 bzero(&ifgrq, sizeof ifgrq);
983 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
984 sizeof(ifgrq.ifgrq_member));
985 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
986 sizeof(struct ifg_req))))
988 len -= sizeof(ifgrq);
996 * Delete Routes for a Network Interface
998 * Called for each routing entry via the rnh->rnh_walktree() call above
999 * to delete all route entries referencing a detaching network interface.
1002 * rn pointer to node in the routing table
1003 * arg argument passed to rnh->rnh_walktree() - detaching interface
1007 * errno failed - reason indicated
1011 if_rtdel(struct radix_node *rn, void *arg)
1013 struct rtentry *rt = (struct rtentry *)rn;
1014 struct ifnet *ifp = arg;
1017 if (rt->rt_ifp == ifp) {
1020 * Protect (sorta) against walktree recursion problems
1021 * with cloned routes
1023 if (!(rt->rt_flags & RTF_UP))
1026 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1027 rt_mask(rt), rt->rt_flags,
1030 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1038 * Locate an interface based on a complete address.
1041 ifa_ifwithaddr(struct sockaddr *addr)
1045 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1046 struct ifaddr_container *ifac;
1048 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1049 struct ifaddr *ifa = ifac->ifa;
1051 if (ifa->ifa_addr->sa_family != addr->sa_family)
1053 if (sa_equal(addr, ifa->ifa_addr))
1055 if ((ifp->if_flags & IFF_BROADCAST) &&
1056 ifa->ifa_broadaddr &&
1057 /* IPv6 doesn't have broadcast */
1058 ifa->ifa_broadaddr->sa_len != 0 &&
1059 sa_equal(ifa->ifa_broadaddr, addr))
1066 * Locate the point to point interface with a given destination address.
1069 ifa_ifwithdstaddr(struct sockaddr *addr)
1073 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1074 struct ifaddr_container *ifac;
1076 if (!(ifp->if_flags & IFF_POINTOPOINT))
1079 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1080 struct ifaddr *ifa = ifac->ifa;
1082 if (ifa->ifa_addr->sa_family != addr->sa_family)
1084 if (ifa->ifa_dstaddr &&
1085 sa_equal(addr, ifa->ifa_dstaddr))
1093 * Find an interface on a specific network. If many, choice
1094 * is most specific found.
1097 ifa_ifwithnet(struct sockaddr *addr)
1100 struct ifaddr *ifa_maybe = NULL;
1101 u_int af = addr->sa_family;
1102 char *addr_data = addr->sa_data, *cplim;
1105 * AF_LINK addresses can be looked up directly by their index number,
1106 * so do that if we can.
1108 if (af == AF_LINK) {
1109 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1111 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1112 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1116 * Scan though each interface, looking for ones that have
1117 * addresses in this address family.
1119 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1120 struct ifaddr_container *ifac;
1122 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1123 struct ifaddr *ifa = ifac->ifa;
1124 char *cp, *cp2, *cp3;
1126 if (ifa->ifa_addr->sa_family != af)
1128 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1130 * This is a bit broken as it doesn't
1131 * take into account that the remote end may
1132 * be a single node in the network we are
1134 * The trouble is that we don't know the
1135 * netmask for the remote end.
1137 if (ifa->ifa_dstaddr != NULL &&
1138 sa_equal(addr, ifa->ifa_dstaddr))
1142 * if we have a special address handler,
1143 * then use it instead of the generic one.
1145 if (ifa->ifa_claim_addr) {
1146 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1154 * Scan all the bits in the ifa's address.
1155 * If a bit dissagrees with what we are
1156 * looking for, mask it with the netmask
1157 * to see if it really matters.
1158 * (A byte at a time)
1160 if (ifa->ifa_netmask == 0)
1163 cp2 = ifa->ifa_addr->sa_data;
1164 cp3 = ifa->ifa_netmask->sa_data;
1165 cplim = ifa->ifa_netmask->sa_len +
1166 (char *)ifa->ifa_netmask;
1168 if ((*cp++ ^ *cp2++) & *cp3++)
1169 goto next; /* next address! */
1171 * If the netmask of what we just found
1172 * is more specific than what we had before
1173 * (if we had one) then remember the new one
1174 * before continuing to search
1175 * for an even better one.
1177 if (ifa_maybe == NULL ||
1178 rn_refines((char *)ifa->ifa_netmask,
1179 (char *)ifa_maybe->ifa_netmask))
1188 * Find an interface address specific to an interface best matching
1192 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1194 struct ifaddr_container *ifac;
1195 char *cp, *cp2, *cp3;
1197 struct ifaddr *ifa_maybe = NULL;
1198 u_int af = addr->sa_family;
1202 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1203 struct ifaddr *ifa = ifac->ifa;
1205 if (ifa->ifa_addr->sa_family != af)
1207 if (ifa_maybe == NULL)
1209 if (ifa->ifa_netmask == NULL) {
1210 if (sa_equal(addr, ifa->ifa_addr) ||
1211 (ifa->ifa_dstaddr != NULL &&
1212 sa_equal(addr, ifa->ifa_dstaddr)))
1216 if (ifp->if_flags & IFF_POINTOPOINT) {
1217 if (sa_equal(addr, ifa->ifa_dstaddr))
1221 cp2 = ifa->ifa_addr->sa_data;
1222 cp3 = ifa->ifa_netmask->sa_data;
1223 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1224 for (; cp3 < cplim; cp3++)
1225 if ((*cp++ ^ *cp2++) & *cp3)
1235 * Default action when installing a route with a Link Level gateway.
1236 * Lookup an appropriate real ifa to point to.
1237 * This should be moved to /sys/net/link.c eventually.
1240 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1243 struct sockaddr *dst;
1246 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1247 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1249 ifa = ifaof_ifpforaddr(dst, ifp);
1251 IFAFREE(rt->rt_ifa);
1254 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1255 ifa->ifa_rtrequest(cmd, rt, info);
1260 * Mark an interface down and notify protocols of
1262 * NOTE: must be called at splnet or eqivalent.
1265 if_unroute(struct ifnet *ifp, int flag, int fam)
1267 struct ifaddr_container *ifac;
1269 ifp->if_flags &= ~flag;
1270 getmicrotime(&ifp->if_lastchange);
1271 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1272 struct ifaddr *ifa = ifac->ifa;
1274 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1275 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1277 ifq_purge(&ifp->if_snd);
1282 * Mark an interface up and notify protocols of
1284 * NOTE: must be called at splnet or eqivalent.
1287 if_route(struct ifnet *ifp, int flag, int fam)
1289 struct ifaddr_container *ifac;
1291 ifq_purge(&ifp->if_snd);
1292 ifp->if_flags |= flag;
1293 getmicrotime(&ifp->if_lastchange);
1294 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1295 struct ifaddr *ifa = ifac->ifa;
1297 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1298 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1307 * Mark an interface down and notify protocols of the transition. An
1308 * interface going down is also considered to be a synchronizing event.
1309 * We must ensure that all packet processing related to the interface
1310 * has completed before we return so e.g. the caller can free the ifnet
1311 * structure that the mbufs may be referencing.
1313 * NOTE: must be called at splnet or eqivalent.
1316 if_down(struct ifnet *ifp)
1318 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1319 netmsg_service_sync();
1323 * Mark an interface up and notify protocols of
1325 * NOTE: must be called at splnet or eqivalent.
1328 if_up(struct ifnet *ifp)
1330 if_route(ifp, IFF_UP, AF_UNSPEC);
1334 * Process a link state change.
1335 * NOTE: must be called at splsoftnet or equivalent.
1338 if_link_state_change(struct ifnet *ifp)
1340 int link_state = ifp->if_link_state;
1343 devctl_notify("IFNET", ifp->if_xname,
1344 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1348 * Handle interface watchdog timer routines. Called
1349 * from softclock, we decrement timers (if set) and
1350 * call the appropriate interface routine on expiration.
1353 if_slowtimo(void *arg)
1359 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1360 if (ifp->if_timer == 0 || --ifp->if_timer)
1362 if (ifp->if_watchdog) {
1363 if (ifnet_tryserialize_all(ifp)) {
1364 (*ifp->if_watchdog)(ifp);
1365 ifnet_deserialize_all(ifp);
1367 /* try again next timeout */
1375 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1379 * Map interface name to
1380 * interface structure pointer.
1383 ifunit(const char *name)
1388 * Search all the interfaces for this name/number
1391 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1392 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1400 * Map interface name in a sockaddr_dl to
1401 * interface structure pointer.
1404 if_withname(struct sockaddr *sa)
1406 char ifname[IFNAMSIZ+1];
1407 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1409 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1410 (sdl->sdl_nlen > IFNAMSIZ) )
1414 * ifunit wants a null-terminated name. It may not be null-terminated
1415 * in the sockaddr. We don't want to change the caller's sockaddr,
1416 * and there might not be room to put the trailing null anyway, so we
1417 * make a local copy that we know we can null terminate safely.
1420 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1421 ifname[sdl->sdl_nlen] = '\0';
1422 return ifunit(ifname);
1430 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1441 size_t namelen, onamelen;
1442 char new_name[IFNAMSIZ];
1444 struct sockaddr_dl *sdl;
1449 return (ifconf(cmd, data, cred));
1454 ifr = (struct ifreq *)data;
1459 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1461 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1462 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1464 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1466 return (if_clone_destroy(ifr->ifr_name));
1467 case SIOCIFGCLONERS:
1468 return (if_clone_list((struct if_clonereq *)data));
1474 * Nominal ioctl through interface, lookup the ifp and obtain a
1475 * lock to serialize the ifconfig ioctl operation.
1477 ifp = ifunit(ifr->ifr_name);
1481 mtx_lock(&ifp->if_ioctl_mtx);
1485 ifr->ifr_index = ifp->if_index;
1489 ifr->ifr_flags = ifp->if_flags;
1490 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1494 ifr->ifr_reqcap = ifp->if_capabilities;
1495 ifr->ifr_curcap = ifp->if_capenable;
1499 ifr->ifr_metric = ifp->if_metric;
1503 ifr->ifr_mtu = ifp->if_mtu;
1507 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1508 sizeof(ifp->if_data));
1512 ifr->ifr_phys = ifp->if_physical;
1515 case SIOCGIFPOLLCPU:
1516 #ifdef DEVICE_POLLING
1517 ifr->ifr_pollcpu = ifp->if_poll_cpuid;
1519 ifr->ifr_pollcpu = -1;
1523 case SIOCSIFPOLLCPU:
1524 #ifdef DEVICE_POLLING
1525 if ((ifp->if_flags & IFF_POLLING) == 0)
1526 ether_pollcpu_register(ifp, ifr->ifr_pollcpu);
1531 error = priv_check_cred(cred, PRIV_ROOT, 0);
1534 new_flags = (ifr->ifr_flags & 0xffff) |
1535 (ifr->ifr_flagshigh << 16);
1536 if (ifp->if_flags & IFF_SMART) {
1537 /* Smart drivers twiddle their own routes */
1538 } else if (ifp->if_flags & IFF_UP &&
1539 (new_flags & IFF_UP) == 0) {
1543 } else if (new_flags & IFF_UP &&
1544 (ifp->if_flags & IFF_UP) == 0) {
1550 #ifdef DEVICE_POLLING
1551 if ((new_flags ^ ifp->if_flags) & IFF_POLLING) {
1552 if (new_flags & IFF_POLLING) {
1553 ether_poll_register(ifp);
1555 ether_poll_deregister(ifp);
1559 #ifdef IFPOLL_ENABLE
1560 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1561 if (new_flags & IFF_NPOLLING)
1562 ifpoll_register(ifp);
1564 ifpoll_deregister(ifp);
1568 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1569 (new_flags &~ IFF_CANTCHANGE);
1570 if (new_flags & IFF_PPROMISC) {
1571 /* Permanently promiscuous mode requested */
1572 ifp->if_flags |= IFF_PROMISC;
1573 } else if (ifp->if_pcount == 0) {
1574 ifp->if_flags &= ~IFF_PROMISC;
1576 if (ifp->if_ioctl) {
1577 ifnet_serialize_all(ifp);
1578 ifp->if_ioctl(ifp, cmd, data, cred);
1579 ifnet_deserialize_all(ifp);
1581 getmicrotime(&ifp->if_lastchange);
1585 error = priv_check_cred(cred, PRIV_ROOT, 0);
1588 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1592 ifnet_serialize_all(ifp);
1593 ifp->if_ioctl(ifp, cmd, data, cred);
1594 ifnet_deserialize_all(ifp);
1598 error = priv_check_cred(cred, PRIV_ROOT, 0);
1601 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1604 if (new_name[0] == '\0') {
1608 if (ifunit(new_name) != NULL) {
1613 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1615 /* Announce the departure of the interface. */
1616 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1618 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1619 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1620 /* XXX IFA_LOCK(ifa); */
1621 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1622 namelen = strlen(new_name);
1623 onamelen = sdl->sdl_nlen;
1625 * Move the address if needed. This is safe because we
1626 * allocate space for a name of length IFNAMSIZ when we
1627 * create this in if_attach().
1629 if (namelen != onamelen) {
1630 bcopy(sdl->sdl_data + onamelen,
1631 sdl->sdl_data + namelen, sdl->sdl_alen);
1633 bcopy(new_name, sdl->sdl_data, namelen);
1634 sdl->sdl_nlen = namelen;
1635 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1636 bzero(sdl->sdl_data, onamelen);
1637 while (namelen != 0)
1638 sdl->sdl_data[--namelen] = 0xff;
1639 /* XXX IFA_UNLOCK(ifa) */
1641 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1643 /* Announce the return of the interface. */
1644 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1648 error = priv_check_cred(cred, PRIV_ROOT, 0);
1651 ifp->if_metric = ifr->ifr_metric;
1652 getmicrotime(&ifp->if_lastchange);
1656 error = priv_check_cred(cred, PRIV_ROOT, 0);
1659 if (ifp->if_ioctl == NULL) {
1663 ifnet_serialize_all(ifp);
1664 error = ifp->if_ioctl(ifp, cmd, data, cred);
1665 ifnet_deserialize_all(ifp);
1667 getmicrotime(&ifp->if_lastchange);
1672 u_long oldmtu = ifp->if_mtu;
1674 error = priv_check_cred(cred, PRIV_ROOT, 0);
1677 if (ifp->if_ioctl == NULL) {
1681 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
1685 ifnet_serialize_all(ifp);
1686 error = ifp->if_ioctl(ifp, cmd, data, cred);
1687 ifnet_deserialize_all(ifp);
1689 getmicrotime(&ifp->if_lastchange);
1693 * If the link MTU changed, do network layer specific procedure.
1695 if (ifp->if_mtu != oldmtu) {
1705 error = priv_check_cred(cred, PRIV_ROOT, 0);
1709 /* Don't allow group membership on non-multicast interfaces. */
1710 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1715 /* Don't let users screw up protocols' entries. */
1716 if (ifr->ifr_addr.sa_family != AF_LINK) {
1721 if (cmd == SIOCADDMULTI) {
1722 struct ifmultiaddr *ifma;
1723 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1725 error = if_delmulti(ifp, &ifr->ifr_addr);
1728 getmicrotime(&ifp->if_lastchange);
1731 case SIOCSIFPHYADDR:
1732 case SIOCDIFPHYADDR:
1734 case SIOCSIFPHYADDR_IN6:
1736 case SIOCSLIFPHYADDR:
1738 case SIOCSIFGENERIC:
1739 error = priv_check_cred(cred, PRIV_ROOT, 0);
1742 if (ifp->if_ioctl == 0) {
1746 ifnet_serialize_all(ifp);
1747 error = ifp->if_ioctl(ifp, cmd, data, cred);
1748 ifnet_deserialize_all(ifp);
1750 getmicrotime(&ifp->if_lastchange);
1754 ifs = (struct ifstat *)data;
1755 ifs->ascii[0] = '\0';
1757 case SIOCGIFPSRCADDR:
1758 case SIOCGIFPDSTADDR:
1759 case SIOCGLIFPHYADDR:
1761 case SIOCGIFGENERIC:
1762 if (ifp->if_ioctl == NULL) {
1766 ifnet_serialize_all(ifp);
1767 error = ifp->if_ioctl(ifp, cmd, data, cred);
1768 ifnet_deserialize_all(ifp);
1772 error = priv_check_cred(cred, PRIV_ROOT, 0);
1775 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
1776 ifr->ifr_addr.sa_len);
1777 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
1781 oif_flags = ifp->if_flags;
1782 if (so->so_proto == 0) {
1787 error = so_pru_control_direct(so, cmd, data, ifp);
1792 case SIOCSIFDSTADDR:
1794 case SIOCSIFBRDADDR:
1795 case SIOCSIFNETMASK:
1796 #if BYTE_ORDER != BIG_ENDIAN
1797 if (ifr->ifr_addr.sa_family == 0 &&
1798 ifr->ifr_addr.sa_len < 16) {
1799 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1800 ifr->ifr_addr.sa_len = 16;
1803 if (ifr->ifr_addr.sa_len == 0)
1804 ifr->ifr_addr.sa_len = 16;
1810 case OSIOCGIFDSTADDR:
1811 cmd = SIOCGIFDSTADDR;
1813 case OSIOCGIFBRDADDR:
1814 cmd = SIOCGIFBRDADDR;
1816 case OSIOCGIFNETMASK:
1817 cmd = SIOCGIFNETMASK;
1823 error = so_pru_control_direct(so, cmd, data, ifp);
1827 case OSIOCGIFDSTADDR:
1828 case OSIOCGIFBRDADDR:
1829 case OSIOCGIFNETMASK:
1830 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1833 #endif /* COMPAT_43 */
1835 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1837 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1838 if (ifp->if_flags & IFF_UP) {
1848 mtx_unlock(&ifp->if_ioctl_mtx);
1853 * Set/clear promiscuous mode on interface ifp based on the truth value
1854 * of pswitch. The calls are reference counted so that only the first
1855 * "on" request actually has an effect, as does the final "off" request.
1856 * Results are undefined if the "off" and "on" requests are not matched.
1859 ifpromisc(struct ifnet *ifp, int pswitch)
1865 oldflags = ifp->if_flags;
1866 if (ifp->if_flags & IFF_PPROMISC) {
1867 /* Do nothing if device is in permanently promiscuous mode */
1868 ifp->if_pcount += pswitch ? 1 : -1;
1873 * If the device is not configured up, we cannot put it in
1876 if ((ifp->if_flags & IFF_UP) == 0)
1878 if (ifp->if_pcount++ != 0)
1880 ifp->if_flags |= IFF_PROMISC;
1881 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1884 if (--ifp->if_pcount > 0)
1886 ifp->if_flags &= ~IFF_PROMISC;
1887 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1890 ifr.ifr_flags = ifp->if_flags;
1891 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1892 ifnet_serialize_all(ifp);
1893 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
1894 ifnet_deserialize_all(ifp);
1898 ifp->if_flags = oldflags;
1903 * Return interface configuration
1904 * of system. List may be used
1905 * in later ioctl's (above) to get
1906 * other information.
1909 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
1911 struct ifconf *ifc = (struct ifconf *)data;
1913 struct sockaddr *sa;
1914 struct ifreq ifr, *ifrp;
1915 int space = ifc->ifc_len, error = 0;
1917 ifrp = ifc->ifc_req;
1918 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1919 struct ifaddr_container *ifac;
1922 if (space <= sizeof ifr)
1926 * Zero the stack declared structure first to prevent
1927 * memory disclosure.
1929 bzero(&ifr, sizeof(ifr));
1930 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1931 >= sizeof(ifr.ifr_name)) {
1932 error = ENAMETOOLONG;
1937 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1938 struct ifaddr *ifa = ifac->ifa;
1940 if (space <= sizeof ifr)
1943 if (cred->cr_prison &&
1944 prison_if(cred, sa))
1948 if (cmd == OSIOCGIFCONF) {
1949 struct osockaddr *osa =
1950 (struct osockaddr *)&ifr.ifr_addr;
1952 osa->sa_family = sa->sa_family;
1953 error = copyout(&ifr, ifrp, sizeof ifr);
1957 if (sa->sa_len <= sizeof(*sa)) {
1959 error = copyout(&ifr, ifrp, sizeof ifr);
1962 if (space < (sizeof ifr) + sa->sa_len -
1965 space -= sa->sa_len - sizeof(*sa);
1966 error = copyout(&ifr, ifrp,
1967 sizeof ifr.ifr_name);
1969 error = copyout(sa, &ifrp->ifr_addr,
1971 ifrp = (struct ifreq *)
1972 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1976 space -= sizeof ifr;
1981 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
1982 error = copyout(&ifr, ifrp, sizeof ifr);
1985 space -= sizeof ifr;
1989 ifc->ifc_len -= space;
1994 * Just like if_promisc(), but for all-multicast-reception mode.
1997 if_allmulti(struct ifnet *ifp, int onswitch)
2005 if (ifp->if_amcount++ == 0) {
2006 ifp->if_flags |= IFF_ALLMULTI;
2007 ifr.ifr_flags = ifp->if_flags;
2008 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2009 ifnet_serialize_all(ifp);
2010 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2012 ifnet_deserialize_all(ifp);
2015 if (ifp->if_amcount > 1) {
2018 ifp->if_amcount = 0;
2019 ifp->if_flags &= ~IFF_ALLMULTI;
2020 ifr.ifr_flags = ifp->if_flags;
2021 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2022 ifnet_serialize_all(ifp);
2023 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2025 ifnet_deserialize_all(ifp);
2037 * Add a multicast listenership to the interface in question.
2038 * The link layer provides a routine which converts
2042 struct ifnet *ifp, /* interface to manipulate */
2043 struct sockaddr *sa, /* address to add */
2044 struct ifmultiaddr **retifma)
2046 struct sockaddr *llsa, *dupsa;
2048 struct ifmultiaddr *ifma;
2051 * If the matching multicast address already exists
2052 * then don't add a new one, just add a reference
2054 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2055 if (sa_equal(sa, ifma->ifma_addr)) {
2056 ifma->ifma_refcount++;
2064 * Give the link layer a chance to accept/reject it, and also
2065 * find out which AF_LINK address this maps to, if it isn't one
2068 if (ifp->if_resolvemulti) {
2069 ifnet_serialize_all(ifp);
2070 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2071 ifnet_deserialize_all(ifp);
2078 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2079 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_WAITOK);
2080 bcopy(sa, dupsa, sa->sa_len);
2082 ifma->ifma_addr = dupsa;
2083 ifma->ifma_lladdr = llsa;
2084 ifma->ifma_ifp = ifp;
2085 ifma->ifma_refcount = 1;
2086 ifma->ifma_protospec = 0;
2087 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2090 * Some network interfaces can scan the address list at
2091 * interrupt time; lock them out.
2094 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2100 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2101 if (sa_equal(ifma->ifma_addr, llsa))
2105 ifma->ifma_refcount++;
2107 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2108 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_WAITOK);
2109 bcopy(llsa, dupsa, llsa->sa_len);
2110 ifma->ifma_addr = dupsa;
2111 ifma->ifma_ifp = ifp;
2112 ifma->ifma_refcount = 1;
2114 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2119 * We are certain we have added something, so call down to the
2120 * interface to let them know about it.
2123 ifnet_serialize_all(ifp);
2125 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2126 ifnet_deserialize_all(ifp);
2133 * Remove a reference to a multicast address on this interface. Yell
2134 * if the request does not match an existing membership.
2137 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2139 struct ifmultiaddr *ifma;
2141 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2142 if (sa_equal(sa, ifma->ifma_addr))
2147 if (ifma->ifma_refcount > 1) {
2148 ifma->ifma_refcount--;
2152 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2153 sa = ifma->ifma_lladdr;
2155 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2157 * Make sure the interface driver is notified
2158 * in the case of a link layer mcast group being left.
2160 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL) {
2161 ifnet_serialize_all(ifp);
2162 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2163 ifnet_deserialize_all(ifp);
2166 kfree(ifma->ifma_addr, M_IFMADDR);
2167 kfree(ifma, M_IFMADDR);
2172 * Now look for the link-layer address which corresponds to
2173 * this network address. It had been squirreled away in
2174 * ifma->ifma_lladdr for this purpose (so we don't have
2175 * to call ifp->if_resolvemulti() again), and we saved that
2176 * value in sa above. If some nasty deleted the
2177 * link-layer address out from underneath us, we can deal because
2178 * the address we stored was is not the same as the one which was
2179 * in the record for the link-layer address. (So we don't complain
2182 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2183 if (sa_equal(sa, ifma->ifma_addr))
2188 if (ifma->ifma_refcount > 1) {
2189 ifma->ifma_refcount--;
2194 ifnet_serialize_all(ifp);
2195 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2196 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2197 ifnet_deserialize_all(ifp);
2199 kfree(ifma->ifma_addr, M_IFMADDR);
2200 kfree(sa, M_IFMADDR);
2201 kfree(ifma, M_IFMADDR);
2207 * Delete all multicast group membership for an interface.
2208 * Should be used to quickly flush all multicast filters.
2211 if_delallmulti(struct ifnet *ifp)
2213 struct ifmultiaddr *ifma;
2214 struct ifmultiaddr *next;
2216 TAILQ_FOREACH_MUTABLE(ifma, &ifp->if_multiaddrs, ifma_link, next)
2217 if_delmulti(ifp, ifma->ifma_addr);
2222 * Set the link layer address on an interface.
2224 * At this time we only support certain types of interfaces,
2225 * and we don't allow the length of the address to change.
2228 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2230 struct sockaddr_dl *sdl;
2233 sdl = IF_LLSOCKADDR(ifp);
2236 if (len != sdl->sdl_alen) /* don't allow length to change */
2238 switch (ifp->if_type) {
2239 case IFT_ETHER: /* these types use struct arpcom */
2242 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2243 bcopy(lladdr, LLADDR(sdl), len);
2249 * If the interface is already up, we need
2250 * to re-init it in order to reprogram its
2253 ifnet_serialize_all(ifp);
2254 if ((ifp->if_flags & IFF_UP) != 0) {
2256 struct ifaddr_container *ifac;
2259 ifp->if_flags &= ~IFF_UP;
2260 ifr.ifr_flags = ifp->if_flags;
2261 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2262 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2264 ifp->if_flags |= IFF_UP;
2265 ifr.ifr_flags = ifp->if_flags;
2266 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2267 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2271 * Also send gratuitous ARPs to notify other nodes about
2272 * the address change.
2274 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2275 struct ifaddr *ifa = ifac->ifa;
2277 if (ifa->ifa_addr != NULL &&
2278 ifa->ifa_addr->sa_family == AF_INET)
2279 arp_gratuitous(ifp, ifa);
2283 ifnet_deserialize_all(ifp);
2287 struct ifmultiaddr *
2288 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2290 struct ifmultiaddr *ifma;
2292 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2293 if (sa_equal(ifma->ifma_addr, sa))
2300 * This function locates the first real ethernet MAC from a network
2301 * card and loads it into node, returning 0 on success or ENOENT if
2302 * no suitable interfaces were found. It is used by the uuid code to
2303 * generate a unique 6-byte number.
2306 if_getanyethermac(uint16_t *node, int minlen)
2309 struct sockaddr_dl *sdl;
2311 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2312 if (ifp->if_type != IFT_ETHER)
2314 sdl = IF_LLSOCKADDR(ifp);
2315 if (sdl->sdl_alen < minlen)
2317 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2325 * The name argument must be a pointer to storage which will last as
2326 * long as the interface does. For physical devices, the result of
2327 * device_get_name(dev) is a good choice and for pseudo-devices a
2328 * static string works well.
2331 if_initname(struct ifnet *ifp, const char *name, int unit)
2333 ifp->if_dname = name;
2334 ifp->if_dunit = unit;
2335 if (unit != IF_DUNIT_NONE)
2336 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2338 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2342 if_printf(struct ifnet *ifp, const char *fmt, ...)
2347 retval = kprintf("%s: ", ifp->if_xname);
2348 __va_start(ap, fmt);
2349 retval += kvprintf(fmt, ap);
2355 if_alloc(uint8_t type)
2361 * XXX temporary hack until arpcom is setup in if_l2com
2363 if (type == IFT_ETHER)
2364 size = sizeof(struct arpcom);
2366 size = sizeof(struct ifnet);
2368 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2370 ifp->if_type = type;
2372 if (if_com_alloc[type] != NULL) {
2373 ifp->if_l2com = if_com_alloc[type](type, ifp);
2374 if (ifp->if_l2com == NULL) {
2375 kfree(ifp, M_IFNET);
2383 if_free(struct ifnet *ifp)
2385 kfree(ifp, M_IFNET);
2389 ifq_set_classic(struct ifaltq *ifq)
2391 ifq->altq_enqueue = ifq_classic_enqueue;
2392 ifq->altq_dequeue = ifq_classic_dequeue;
2393 ifq->altq_request = ifq_classic_request;
2397 ifq_classic_enqueue(struct ifaltq *ifq, struct mbuf *m,
2398 struct altq_pktattr *pa __unused)
2400 logifq(enqueue, ifq);
2401 if (IF_QFULL(ifq)) {
2411 ifq_classic_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
2420 logifq(dequeue, ifq);
2424 panic("unsupported ALTQ dequeue op: %d", op);
2426 KKASSERT(mpolled == NULL || mpolled == m);
2431 ifq_classic_request(struct ifaltq *ifq, int req, void *arg)
2438 panic("unsupported ALTQ request: %d", req);
2444 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2446 struct ifaltq *ifq = &ifp->if_snd;
2447 int running = 0, error, start = 0;
2449 ASSERT_IFNET_NOT_SERIALIZED_TX(ifp);
2452 error = ifq_enqueue_locked(ifq, m, pa);
2457 if (!ifq->altq_started) {
2459 * Hold the interlock of ifnet.if_start
2461 ifq->altq_started = 1;
2466 ifp->if_obytes += m->m_pkthdr.len;
2467 if (m->m_flags & M_MCAST)
2471 logifstart(avoid, ifp);
2476 * Try to do direct ifnet.if_start first, if there is
2477 * contention on ifnet's serializer, ifnet.if_start will
2478 * be scheduled on ifnet's CPU.
2480 if (!ifnet_tryserialize_tx(ifp)) {
2482 * ifnet serializer contention happened,
2483 * ifnet.if_start is scheduled on ifnet's
2484 * CPU, and we keep going.
2486 logifstart(contend_sched, ifp);
2487 if_start_schedule(ifp);
2491 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
2492 logifstart(run, ifp);
2494 if ((ifp->if_flags &
2495 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
2499 ifnet_deserialize_tx(ifp);
2501 if (if_start_need_schedule(ifq, running)) {
2503 * More data need to be transmitted, ifnet.if_start is
2504 * scheduled on ifnet's CPU, and we keep going.
2505 * NOTE: ifnet.if_start interlock is not released.
2507 logifstart(sched, ifp);
2508 if_start_schedule(ifp);
2514 ifa_create(int size, int flags)
2519 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
2521 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
2525 ifa->ifa_containers = kmalloc(ncpus * sizeof(struct ifaddr_container),
2526 M_IFADDR, M_WAITOK | M_ZERO);
2527 ifa->ifa_ncnt = ncpus;
2528 for (i = 0; i < ncpus; ++i) {
2529 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
2531 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
2533 ifac->ifa_refcnt = 1;
2536 kprintf("alloc ifa %p %d\n", ifa, size);
2542 ifac_free(struct ifaddr_container *ifac, int cpu_id)
2544 struct ifaddr *ifa = ifac->ifa;
2546 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
2547 KKASSERT(ifac->ifa_refcnt == 0);
2548 KASSERT(ifac->ifa_listmask == 0,
2549 ("ifa is still on %#x lists", ifac->ifa_listmask));
2551 ifac->ifa_magic = IFA_CONTAINER_DEAD;
2553 #ifdef IFADDR_DEBUG_VERBOSE
2554 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
2557 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
2558 ("invalid # of ifac, %d", ifa->ifa_ncnt));
2559 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
2561 kprintf("free ifa %p\n", ifa);
2563 kfree(ifa->ifa_containers, M_IFADDR);
2564 kfree(ifa, M_IFADDR);
2569 ifa_iflink_dispatch(netmsg_t nmsg)
2571 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2572 struct ifaddr *ifa = msg->ifa;
2573 struct ifnet *ifp = msg->ifp;
2575 struct ifaddr_container *ifac;
2579 ifac = &ifa->ifa_containers[cpu];
2580 ASSERT_IFAC_VALID(ifac);
2581 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
2582 ("ifaddr is on if_addrheads"));
2584 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
2586 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
2588 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
2592 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2596 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
2598 struct netmsg_ifaddr msg;
2600 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2601 0, ifa_iflink_dispatch);
2606 ifa_domsg(&msg.base.lmsg, 0);
2610 ifa_ifunlink_dispatch(netmsg_t nmsg)
2612 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2613 struct ifaddr *ifa = msg->ifa;
2614 struct ifnet *ifp = msg->ifp;
2616 struct ifaddr_container *ifac;
2620 ifac = &ifa->ifa_containers[cpu];
2621 ASSERT_IFAC_VALID(ifac);
2622 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
2623 ("ifaddr is not on if_addrhead"));
2625 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
2626 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
2630 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2634 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
2636 struct netmsg_ifaddr msg;
2638 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2639 0, ifa_ifunlink_dispatch);
2643 ifa_domsg(&msg.base.lmsg, 0);
2647 ifa_destroy_dispatch(netmsg_t nmsg)
2649 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2652 ifa_forwardmsg(&nmsg->lmsg, mycpuid + 1);
2656 ifa_destroy(struct ifaddr *ifa)
2658 struct netmsg_ifaddr msg;
2660 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2661 0, ifa_destroy_dispatch);
2664 ifa_domsg(&msg.base.lmsg, 0);
2668 ifnet_portfn(int cpu)
2670 return &ifnet_threads[cpu].td_msgport;
2674 ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
2676 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
2678 if (next_cpu < ncpus)
2679 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
2681 lwkt_replymsg(lmsg, 0);
2685 ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
2687 KKASSERT(cpu < ncpus);
2688 return lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
2692 ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
2694 KKASSERT(cpu < ncpus);
2695 lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
2699 * Generic netmsg service loop. Some protocols may roll their own but all
2700 * must do the basic command dispatch function call done here.
2703 ifnet_service_loop(void *arg __unused)
2707 while ((msg = lwkt_waitport(&curthread->td_msgport, 0))) {
2708 KASSERT(msg->base.nm_dispatch, ("ifnet_service: badmsg"));
2709 msg->base.nm_dispatch(msg);
2714 ifnetinit(void *dummy __unused)
2718 for (i = 0; i < ncpus; ++i) {
2719 struct thread *thr = &ifnet_threads[i];
2721 lwkt_create(ifnet_service_loop, NULL, NULL,
2722 thr, TDF_NOSTART|TDF_FORCE_SPINPORT,
2724 netmsg_service_port_init(&thr->td_msgport);
2730 ifnet_byindex(unsigned short idx)
2734 return ifindex2ifnet[idx];
2738 ifaddr_byindex(unsigned short idx)
2742 ifp = ifnet_byindex(idx);
2745 return TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
2749 if_register_com_alloc(u_char type,
2750 if_com_alloc_t *a, if_com_free_t *f)
2753 KASSERT(if_com_alloc[type] == NULL,
2754 ("if_register_com_alloc: %d already registered", type));
2755 KASSERT(if_com_free[type] == NULL,
2756 ("if_register_com_alloc: %d free already registered", type));
2758 if_com_alloc[type] = a;
2759 if_com_free[type] = f;
2763 if_deregister_com_alloc(u_char type)
2766 KASSERT(if_com_alloc[type] != NULL,
2767 ("if_deregister_com_alloc: %d not registered", type));
2768 KASSERT(if_com_free[type] != NULL,
2769 ("if_deregister_com_alloc: %d free not registered", type));
2770 if_com_alloc[type] = NULL;
2771 if_com_free[type] = NULL;
2775 if_ring_count2(int cnt, int cnt_max)
2779 KASSERT(cnt_max >= 1 && powerof2(cnt_max),
2780 ("invalid ring count max %d", cnt_max));
2789 while ((1 << (shift + 1)) <= cnt)
2793 KASSERT(cnt >= 1 && cnt <= ncpus2 && cnt <= cnt_max,
2794 ("calculate cnt %d, ncpus2 %d, cnt max %d",
2795 cnt, ncpus2, cnt_max));