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;
228 if_start_ipifunc(void *arg)
230 struct ifnet *ifp = arg;
231 struct lwkt_msg *lmsg = &ifp->if_start_nmsg[mycpuid].lmsg;
234 if (lmsg->ms_flags & MSGF_DONE)
235 lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg);
240 * Schedule ifnet.if_start on ifnet's CPU
243 if_start_schedule(struct ifnet *ifp)
248 cpu = ifp->if_start_cpuid(ifp);
250 lwkt_send_ipiq(globaldata_find(cpu), if_start_ipifunc, ifp);
253 if_start_ipifunc(ifp);
258 * This function will release ifnet.if_start interlock,
259 * if ifnet.if_start does not need to be scheduled
262 if_start_need_schedule(struct ifaltq *ifq, int running)
264 if (!running || ifq_is_empty(ifq)
266 || ifq->altq_tbr != NULL
271 * ifnet.if_start interlock is released, if:
272 * 1) Hardware can not take any packets, due to
273 * o interface is marked down
274 * o hardware queue is full (IFF_OACTIVE)
275 * Under the second situation, hardware interrupt
276 * or polling(4) will call/schedule ifnet.if_start
277 * when hardware queue is ready
278 * 2) There is not packet in the ifnet.if_snd.
279 * Further ifq_dispatch or ifq_handoff will call/
280 * schedule ifnet.if_start
281 * 3) TBR is used and it does not allow further
283 * TBR callout will call ifnet.if_start
285 if (!running || !ifq_data_ready(ifq)) {
286 ifq->altq_started = 0;
296 if_start_dispatch(netmsg_t msg)
298 struct lwkt_msg *lmsg = &msg->base.lmsg;
299 struct ifnet *ifp = lmsg->u.ms_resultp;
300 struct ifaltq *ifq = &ifp->if_snd;
304 lwkt_replymsg(lmsg, 0); /* reply ASAP */
308 if (mycpuid != ifp->if_start_cpuid(ifp)) {
310 * If the ifnet is still up, we need to
311 * chase its CPU change.
313 if (ifp->if_flags & IFF_UP) {
314 logifstart(chase_sched, ifp);
315 if_start_schedule(ifp);
323 if (ifp->if_flags & IFF_UP) {
324 ifnet_serialize_tx(ifp); /* XXX try? */
325 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
326 logifstart(run, ifp);
329 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
332 ifnet_deserialize_tx(ifp);
337 if (if_start_need_schedule(ifq, running)) {
339 if (lmsg->ms_flags & MSGF_DONE) { /* XXX necessary? */
340 logifstart(sched, ifp);
341 lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg);
347 /* Device driver ifnet.if_start helper function */
349 if_devstart(struct ifnet *ifp)
351 struct ifaltq *ifq = &ifp->if_snd;
354 ASSERT_IFNET_SERIALIZED_TX(ifp);
357 if (ifq->altq_started || !ifq_data_ready(ifq)) {
358 logifstart(avoid, ifp);
362 ifq->altq_started = 1;
365 logifstart(run, ifp);
368 if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
371 if (if_start_need_schedule(ifq, running)) {
373 * More data need to be transmitted, ifnet.if_start is
374 * scheduled on ifnet's CPU, and we keep going.
375 * NOTE: ifnet.if_start interlock is not released.
377 logifstart(sched, ifp);
378 if_start_schedule(ifp);
383 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
385 lwkt_serialize_enter(ifp->if_serializer);
389 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
391 lwkt_serialize_exit(ifp->if_serializer);
395 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
397 return lwkt_serialize_try(ifp->if_serializer);
402 if_default_serialize_assert(struct ifnet *ifp,
403 enum ifnet_serialize slz __unused,
404 boolean_t serialized)
407 ASSERT_SERIALIZED(ifp->if_serializer);
409 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
414 * Attach an interface to the list of "active" interfaces.
416 * The serializer is optional. If non-NULL access to the interface
420 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
422 unsigned socksize, ifasize;
423 int namelen, masklen;
424 struct sockaddr_dl *sdl;
429 static int if_indexlim = 8;
431 if (ifp->if_serialize != NULL) {
432 KASSERT(ifp->if_deserialize != NULL &&
433 ifp->if_tryserialize != NULL &&
434 ifp->if_serialize_assert != NULL,
435 ("serialize functions are partially setup"));
438 * If the device supplies serialize functions,
439 * then clear if_serializer to catch any invalid
440 * usage of this field.
442 KASSERT(serializer == NULL,
443 ("both serialize functions and default serializer "
445 ifp->if_serializer = NULL;
447 KASSERT(ifp->if_deserialize == NULL &&
448 ifp->if_tryserialize == NULL &&
449 ifp->if_serialize_assert == NULL,
450 ("serialize functions are partially setup"));
451 ifp->if_serialize = if_default_serialize;
452 ifp->if_deserialize = if_default_deserialize;
453 ifp->if_tryserialize = if_default_tryserialize;
455 ifp->if_serialize_assert = if_default_serialize_assert;
459 * The serializer can be passed in from the device,
460 * allowing the same serializer to be used for both
461 * the interrupt interlock and the device queue.
462 * If not specified, the netif structure will use an
463 * embedded serializer.
465 if (serializer == NULL) {
466 serializer = &ifp->if_default_serializer;
467 lwkt_serialize_init(serializer);
469 ifp->if_serializer = serializer;
472 ifp->if_start_cpuid = if_start_cpuid;
475 #ifdef DEVICE_POLLING
476 /* Device is not in polling mode by default */
477 ifp->if_poll_cpuid = -1;
478 if (ifp->if_poll != NULL)
479 ifp->if_start_cpuid = if_start_cpuid_poll;
482 ifp->if_start_nmsg = kmalloc(ncpus * sizeof(*ifp->if_start_nmsg),
483 M_LWKTMSG, M_WAITOK);
484 for (i = 0; i < ncpus; ++i) {
485 netmsg_init(&ifp->if_start_nmsg[i], NULL, &netisr_adone_rport,
486 0, if_start_dispatch);
487 ifp->if_start_nmsg[i].lmsg.u.ms_resultp = ifp;
490 mtx_init(&ifp->if_ioctl_mtx);
491 mtx_lock(&ifp->if_ioctl_mtx);
493 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
494 ifp->if_index = ++if_index;
498 * The old code would work if the interface passed a pre-existing
499 * chain of ifaddrs to this code. We don't trust our callers to
500 * properly initialize the tailq, however, so we no longer allow
501 * this unlikely case.
503 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
504 M_IFADDR, M_WAITOK | M_ZERO);
505 for (i = 0; i < ncpus; ++i)
506 TAILQ_INIT(&ifp->if_addrheads[i]);
508 TAILQ_INIT(&ifp->if_prefixhead);
509 TAILQ_INIT(&ifp->if_multiaddrs);
510 TAILQ_INIT(&ifp->if_groups);
511 getmicrotime(&ifp->if_lastchange);
512 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
518 /* grow ifindex2ifnet */
519 n = if_indexlim * sizeof(*q);
520 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
522 bcopy(ifindex2ifnet, q, n/2);
523 kfree(ifindex2ifnet, M_IFADDR);
528 ifindex2ifnet[if_index] = ifp;
531 * create a Link Level name for this device
533 namelen = strlen(ifp->if_xname);
534 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
535 socksize = masklen + ifp->if_addrlen;
536 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
537 if (socksize < sizeof(*sdl))
538 socksize = sizeof(*sdl);
539 socksize = ROUNDUP(socksize);
541 ifasize = sizeof(struct ifaddr) + 2 * socksize;
542 ifa = ifa_create(ifasize, M_WAITOK);
543 sdl = (struct sockaddr_dl *)(ifa + 1);
544 sdl->sdl_len = socksize;
545 sdl->sdl_family = AF_LINK;
546 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
547 sdl->sdl_nlen = namelen;
548 sdl->sdl_index = ifp->if_index;
549 sdl->sdl_type = ifp->if_type;
550 ifp->if_lladdr = ifa;
552 ifa->ifa_rtrequest = link_rtrequest;
553 ifa->ifa_addr = (struct sockaddr *)sdl;
554 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
555 ifa->ifa_netmask = (struct sockaddr *)sdl;
556 sdl->sdl_len = masklen;
558 sdl->sdl_data[--namelen] = 0xff;
559 ifa_iflink(ifa, ifp, 0 /* Insert head */);
561 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
562 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
566 ifq->altq_disc = NULL;
567 ifq->altq_flags &= ALTQF_CANTCHANGE;
568 ifq->altq_tbr = NULL;
570 ifq->altq_started = 0;
571 ifq->altq_prepended = NULL;
573 ifq_set_classic(ifq);
575 if (!SLIST_EMPTY(&domains))
576 if_attachdomain1(ifp);
578 /* Announce the interface. */
579 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
581 mtx_unlock(&ifp->if_ioctl_mtx);
585 if_attachdomain(void *dummy)
590 TAILQ_FOREACH(ifp, &ifnet, if_list)
591 if_attachdomain1(ifp);
594 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
595 if_attachdomain, NULL);
598 if_attachdomain1(struct ifnet *ifp)
604 /* address family dependent data region */
605 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
606 SLIST_FOREACH(dp, &domains, dom_next)
607 if (dp->dom_ifattach)
608 ifp->if_afdata[dp->dom_family] =
609 (*dp->dom_ifattach)(ifp);
614 * Purge all addresses whose type is _not_ AF_LINK
617 if_purgeaddrs_nolink(struct ifnet *ifp)
619 struct ifaddr_container *ifac, *next;
621 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
623 struct ifaddr *ifa = ifac->ifa;
625 /* Leave link ifaddr as it is */
626 if (ifa->ifa_addr->sa_family == AF_LINK)
629 /* XXX: Ugly!! ad hoc just for INET */
630 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
631 struct ifaliasreq ifr;
632 #ifdef IFADDR_DEBUG_VERBOSE
635 kprintf("purge in4 addr %p: ", ifa);
636 for (i = 0; i < ncpus; ++i)
637 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
641 bzero(&ifr, sizeof ifr);
642 ifr.ifra_addr = *ifa->ifa_addr;
643 if (ifa->ifa_dstaddr)
644 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
645 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
651 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
652 #ifdef IFADDR_DEBUG_VERBOSE
655 kprintf("purge in6 addr %p: ", ifa);
656 for (i = 0; i < ncpus; ++i)
657 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
662 /* ifp_addrhead is already updated */
666 ifa_ifunlink(ifa, ifp);
672 * Detach an interface, removing it from the
673 * list of "active" interfaces.
676 if_detach(struct ifnet *ifp)
678 struct radix_node_head *rnh;
683 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
686 * Remove routes and flush queues.
689 #ifdef DEVICE_POLLING
690 if (ifp->if_flags & IFF_POLLING)
691 ether_poll_deregister(ifp);
694 if (ifp->if_flags & IFF_NPOLLING)
695 ifpoll_deregister(ifp);
700 if (ifq_is_enabled(&ifp->if_snd))
701 altq_disable(&ifp->if_snd);
702 if (ifq_is_attached(&ifp->if_snd))
703 altq_detach(&ifp->if_snd);
707 * Clean up all addresses.
709 ifp->if_lladdr = NULL;
711 if_purgeaddrs_nolink(ifp);
712 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
715 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
716 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
717 ("non-link ifaddr is left on if_addrheads"));
719 ifa_ifunlink(ifa, ifp);
721 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
722 ("there are still ifaddrs left on if_addrheads"));
727 * Remove all IPv4 kernel structures related to ifp.
734 * Remove all IPv6 kernel structs related to ifp. This should be done
735 * before removing routing entries below, since IPv6 interface direct
736 * routes are expected to be removed by the IPv6-specific kernel API.
737 * Otherwise, the kernel will detect some inconsistency and bark it.
743 * Delete all remaining routes using this interface
744 * Unfortuneatly the only way to do this is to slog through
745 * the entire routing table looking for routes which point
746 * to this interface...oh well...
749 for (cpu = 0; cpu < ncpus2; cpu++) {
750 lwkt_migratecpu(cpu);
751 for (i = 1; i <= AF_MAX; i++) {
752 if ((rnh = rt_tables[cpu][i]) == NULL)
754 rnh->rnh_walktree(rnh, if_rtdel, ifp);
757 lwkt_migratecpu(origcpu);
759 /* Announce that the interface is gone. */
760 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
761 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
763 SLIST_FOREACH(dp, &domains, dom_next)
764 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
765 (*dp->dom_ifdetach)(ifp,
766 ifp->if_afdata[dp->dom_family]);
769 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
771 ifindex2ifnet[ifp->if_index] = NULL;
772 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
775 TAILQ_REMOVE(&ifnet, ifp, if_link);
776 kfree(ifp->if_addrheads, M_IFADDR);
777 kfree(ifp->if_start_nmsg, M_LWKTMSG);
782 * Create interface group without members
785 if_creategroup(const char *groupname)
787 struct ifg_group *ifg = NULL;
789 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
790 M_TEMP, M_NOWAIT)) == NULL)
793 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
795 ifg->ifg_carp_demoted = 0;
796 TAILQ_INIT(&ifg->ifg_members);
798 pfi_attach_ifgroup(ifg);
800 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
806 * Add a group to an interface
809 if_addgroup(struct ifnet *ifp, const char *groupname)
811 struct ifg_list *ifgl;
812 struct ifg_group *ifg = NULL;
813 struct ifg_member *ifgm;
815 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
816 groupname[strlen(groupname) - 1] <= '9')
819 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
820 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
823 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
826 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
831 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
832 if (!strcmp(ifg->ifg_group, groupname))
835 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
842 ifgl->ifgl_group = ifg;
843 ifgm->ifgm_ifp = ifp;
845 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
846 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
849 pfi_group_change(groupname);
856 * Remove a group from an interface
859 if_delgroup(struct ifnet *ifp, const char *groupname)
861 struct ifg_list *ifgl;
862 struct ifg_member *ifgm;
864 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
865 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
870 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
872 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
873 if (ifgm->ifgm_ifp == ifp)
877 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
881 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
882 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
884 pfi_detach_ifgroup(ifgl->ifgl_group);
886 kfree(ifgl->ifgl_group, M_TEMP);
892 pfi_group_change(groupname);
899 * Stores all groups from an interface in memory pointed
903 if_getgroup(caddr_t data, struct ifnet *ifp)
906 struct ifg_list *ifgl;
907 struct ifg_req ifgrq, *ifgp;
908 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
910 if (ifgr->ifgr_len == 0) {
911 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
912 ifgr->ifgr_len += sizeof(struct ifg_req);
916 len = ifgr->ifgr_len;
917 ifgp = ifgr->ifgr_groups;
918 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
919 if (len < sizeof(ifgrq))
921 bzero(&ifgrq, sizeof ifgrq);
922 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
923 sizeof(ifgrq.ifgrq_group));
924 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
925 sizeof(struct ifg_req))))
927 len -= sizeof(ifgrq);
935 * Stores all members of a group in memory pointed to by data
938 if_getgroupmembers(caddr_t data)
940 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
941 struct ifg_group *ifg;
942 struct ifg_member *ifgm;
943 struct ifg_req ifgrq, *ifgp;
946 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
947 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
952 if (ifgr->ifgr_len == 0) {
953 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
954 ifgr->ifgr_len += sizeof(ifgrq);
958 len = ifgr->ifgr_len;
959 ifgp = ifgr->ifgr_groups;
960 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
961 if (len < sizeof(ifgrq))
963 bzero(&ifgrq, sizeof ifgrq);
964 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
965 sizeof(ifgrq.ifgrq_member));
966 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
967 sizeof(struct ifg_req))))
969 len -= sizeof(ifgrq);
977 * Delete Routes for a Network Interface
979 * Called for each routing entry via the rnh->rnh_walktree() call above
980 * to delete all route entries referencing a detaching network interface.
983 * rn pointer to node in the routing table
984 * arg argument passed to rnh->rnh_walktree() - detaching interface
988 * errno failed - reason indicated
992 if_rtdel(struct radix_node *rn, void *arg)
994 struct rtentry *rt = (struct rtentry *)rn;
995 struct ifnet *ifp = arg;
998 if (rt->rt_ifp == ifp) {
1001 * Protect (sorta) against walktree recursion problems
1002 * with cloned routes
1004 if (!(rt->rt_flags & RTF_UP))
1007 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1008 rt_mask(rt), rt->rt_flags,
1011 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1019 * Locate an interface based on a complete address.
1022 ifa_ifwithaddr(struct sockaddr *addr)
1026 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1027 struct ifaddr_container *ifac;
1029 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1030 struct ifaddr *ifa = ifac->ifa;
1032 if (ifa->ifa_addr->sa_family != addr->sa_family)
1034 if (sa_equal(addr, ifa->ifa_addr))
1036 if ((ifp->if_flags & IFF_BROADCAST) &&
1037 ifa->ifa_broadaddr &&
1038 /* IPv6 doesn't have broadcast */
1039 ifa->ifa_broadaddr->sa_len != 0 &&
1040 sa_equal(ifa->ifa_broadaddr, addr))
1047 * Locate the point to point interface with a given destination address.
1050 ifa_ifwithdstaddr(struct sockaddr *addr)
1054 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1055 struct ifaddr_container *ifac;
1057 if (!(ifp->if_flags & IFF_POINTOPOINT))
1060 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1061 struct ifaddr *ifa = ifac->ifa;
1063 if (ifa->ifa_addr->sa_family != addr->sa_family)
1065 if (ifa->ifa_dstaddr &&
1066 sa_equal(addr, ifa->ifa_dstaddr))
1074 * Find an interface on a specific network. If many, choice
1075 * is most specific found.
1078 ifa_ifwithnet(struct sockaddr *addr)
1081 struct ifaddr *ifa_maybe = NULL;
1082 u_int af = addr->sa_family;
1083 char *addr_data = addr->sa_data, *cplim;
1086 * AF_LINK addresses can be looked up directly by their index number,
1087 * so do that if we can.
1089 if (af == AF_LINK) {
1090 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1092 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1093 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1097 * Scan though each interface, looking for ones that have
1098 * addresses in this address family.
1100 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1101 struct ifaddr_container *ifac;
1103 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1104 struct ifaddr *ifa = ifac->ifa;
1105 char *cp, *cp2, *cp3;
1107 if (ifa->ifa_addr->sa_family != af)
1109 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1111 * This is a bit broken as it doesn't
1112 * take into account that the remote end may
1113 * be a single node in the network we are
1115 * The trouble is that we don't know the
1116 * netmask for the remote end.
1118 if (ifa->ifa_dstaddr != NULL &&
1119 sa_equal(addr, ifa->ifa_dstaddr))
1123 * if we have a special address handler,
1124 * then use it instead of the generic one.
1126 if (ifa->ifa_claim_addr) {
1127 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1135 * Scan all the bits in the ifa's address.
1136 * If a bit dissagrees with what we are
1137 * looking for, mask it with the netmask
1138 * to see if it really matters.
1139 * (A byte at a time)
1141 if (ifa->ifa_netmask == 0)
1144 cp2 = ifa->ifa_addr->sa_data;
1145 cp3 = ifa->ifa_netmask->sa_data;
1146 cplim = ifa->ifa_netmask->sa_len +
1147 (char *)ifa->ifa_netmask;
1149 if ((*cp++ ^ *cp2++) & *cp3++)
1150 goto next; /* next address! */
1152 * If the netmask of what we just found
1153 * is more specific than what we had before
1154 * (if we had one) then remember the new one
1155 * before continuing to search
1156 * for an even better one.
1158 if (ifa_maybe == NULL ||
1159 rn_refines((char *)ifa->ifa_netmask,
1160 (char *)ifa_maybe->ifa_netmask))
1169 * Find an interface address specific to an interface best matching
1173 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1175 struct ifaddr_container *ifac;
1176 char *cp, *cp2, *cp3;
1178 struct ifaddr *ifa_maybe = NULL;
1179 u_int af = addr->sa_family;
1183 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1184 struct ifaddr *ifa = ifac->ifa;
1186 if (ifa->ifa_addr->sa_family != af)
1188 if (ifa_maybe == NULL)
1190 if (ifa->ifa_netmask == NULL) {
1191 if (sa_equal(addr, ifa->ifa_addr) ||
1192 (ifa->ifa_dstaddr != NULL &&
1193 sa_equal(addr, ifa->ifa_dstaddr)))
1197 if (ifp->if_flags & IFF_POINTOPOINT) {
1198 if (sa_equal(addr, ifa->ifa_dstaddr))
1202 cp2 = ifa->ifa_addr->sa_data;
1203 cp3 = ifa->ifa_netmask->sa_data;
1204 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1205 for (; cp3 < cplim; cp3++)
1206 if ((*cp++ ^ *cp2++) & *cp3)
1216 * Default action when installing a route with a Link Level gateway.
1217 * Lookup an appropriate real ifa to point to.
1218 * This should be moved to /sys/net/link.c eventually.
1221 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1224 struct sockaddr *dst;
1227 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1228 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1230 ifa = ifaof_ifpforaddr(dst, ifp);
1232 IFAFREE(rt->rt_ifa);
1235 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1236 ifa->ifa_rtrequest(cmd, rt, info);
1241 * Mark an interface down and notify protocols of
1243 * NOTE: must be called at splnet or eqivalent.
1246 if_unroute(struct ifnet *ifp, int flag, int fam)
1248 struct ifaddr_container *ifac;
1250 ifp->if_flags &= ~flag;
1251 getmicrotime(&ifp->if_lastchange);
1252 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1253 struct ifaddr *ifa = ifac->ifa;
1255 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1256 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1258 ifq_purge(&ifp->if_snd);
1263 * Mark an interface up and notify protocols of
1265 * NOTE: must be called at splnet or eqivalent.
1268 if_route(struct ifnet *ifp, int flag, int fam)
1270 struct ifaddr_container *ifac;
1272 ifq_purge(&ifp->if_snd);
1273 ifp->if_flags |= flag;
1274 getmicrotime(&ifp->if_lastchange);
1275 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1276 struct ifaddr *ifa = ifac->ifa;
1278 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1279 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1288 * Mark an interface down and notify protocols of the transition. An
1289 * interface going down is also considered to be a synchronizing event.
1290 * We must ensure that all packet processing related to the interface
1291 * has completed before we return so e.g. the caller can free the ifnet
1292 * structure that the mbufs may be referencing.
1294 * NOTE: must be called at splnet or eqivalent.
1297 if_down(struct ifnet *ifp)
1299 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1300 netmsg_service_sync();
1304 * Mark an interface up and notify protocols of
1306 * NOTE: must be called at splnet or eqivalent.
1309 if_up(struct ifnet *ifp)
1311 if_route(ifp, IFF_UP, AF_UNSPEC);
1315 * Process a link state change.
1316 * NOTE: must be called at splsoftnet or equivalent.
1319 if_link_state_change(struct ifnet *ifp)
1321 int link_state = ifp->if_link_state;
1324 devctl_notify("IFNET", ifp->if_xname,
1325 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1329 * Handle interface watchdog timer routines. Called
1330 * from softclock, we decrement timers (if set) and
1331 * call the appropriate interface routine on expiration.
1334 if_slowtimo(void *arg)
1340 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1341 if (ifp->if_timer == 0 || --ifp->if_timer)
1343 if (ifp->if_watchdog) {
1344 if (ifnet_tryserialize_all(ifp)) {
1345 (*ifp->if_watchdog)(ifp);
1346 ifnet_deserialize_all(ifp);
1348 /* try again next timeout */
1356 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1360 * Map interface name to
1361 * interface structure pointer.
1364 ifunit(const char *name)
1369 * Search all the interfaces for this name/number
1372 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1373 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1381 * Map interface name in a sockaddr_dl to
1382 * interface structure pointer.
1385 if_withname(struct sockaddr *sa)
1387 char ifname[IFNAMSIZ+1];
1388 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1390 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1391 (sdl->sdl_nlen > IFNAMSIZ) )
1395 * ifunit wants a null-terminated name. It may not be null-terminated
1396 * in the sockaddr. We don't want to change the caller's sockaddr,
1397 * and there might not be room to put the trailing null anyway, so we
1398 * make a local copy that we know we can null terminate safely.
1401 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1402 ifname[sdl->sdl_nlen] = '\0';
1403 return ifunit(ifname);
1411 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1422 size_t namelen, onamelen;
1423 char new_name[IFNAMSIZ];
1425 struct sockaddr_dl *sdl;
1430 return (ifconf(cmd, data, cred));
1435 ifr = (struct ifreq *)data;
1440 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1442 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1443 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1445 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1447 return (if_clone_destroy(ifr->ifr_name));
1448 case SIOCIFGCLONERS:
1449 return (if_clone_list((struct if_clonereq *)data));
1455 * Nominal ioctl through interface, lookup the ifp and obtain a
1456 * lock to serialize the ifconfig ioctl operation.
1458 ifp = ifunit(ifr->ifr_name);
1462 mtx_lock(&ifp->if_ioctl_mtx);
1466 ifr->ifr_index = ifp->if_index;
1470 ifr->ifr_flags = ifp->if_flags;
1471 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1475 ifr->ifr_reqcap = ifp->if_capabilities;
1476 ifr->ifr_curcap = ifp->if_capenable;
1480 ifr->ifr_metric = ifp->if_metric;
1484 ifr->ifr_mtu = ifp->if_mtu;
1488 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1489 sizeof(ifp->if_data));
1493 ifr->ifr_phys = ifp->if_physical;
1496 case SIOCGIFPOLLCPU:
1497 #ifdef DEVICE_POLLING
1498 ifr->ifr_pollcpu = ifp->if_poll_cpuid;
1500 ifr->ifr_pollcpu = -1;
1504 case SIOCSIFPOLLCPU:
1505 #ifdef DEVICE_POLLING
1506 if ((ifp->if_flags & IFF_POLLING) == 0)
1507 ether_pollcpu_register(ifp, ifr->ifr_pollcpu);
1512 error = priv_check_cred(cred, PRIV_ROOT, 0);
1515 new_flags = (ifr->ifr_flags & 0xffff) |
1516 (ifr->ifr_flagshigh << 16);
1517 if (ifp->if_flags & IFF_SMART) {
1518 /* Smart drivers twiddle their own routes */
1519 } else if (ifp->if_flags & IFF_UP &&
1520 (new_flags & IFF_UP) == 0) {
1524 } else if (new_flags & IFF_UP &&
1525 (ifp->if_flags & IFF_UP) == 0) {
1531 #ifdef DEVICE_POLLING
1532 if ((new_flags ^ ifp->if_flags) & IFF_POLLING) {
1533 if (new_flags & IFF_POLLING) {
1534 ether_poll_register(ifp);
1536 ether_poll_deregister(ifp);
1540 #ifdef IFPOLL_ENABLE
1541 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1542 if (new_flags & IFF_NPOLLING)
1543 ifpoll_register(ifp);
1545 ifpoll_deregister(ifp);
1549 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1550 (new_flags &~ IFF_CANTCHANGE);
1551 if (new_flags & IFF_PPROMISC) {
1552 /* Permanently promiscuous mode requested */
1553 ifp->if_flags |= IFF_PROMISC;
1554 } else if (ifp->if_pcount == 0) {
1555 ifp->if_flags &= ~IFF_PROMISC;
1557 if (ifp->if_ioctl) {
1558 ifnet_serialize_all(ifp);
1559 ifp->if_ioctl(ifp, cmd, data, cred);
1560 ifnet_deserialize_all(ifp);
1562 getmicrotime(&ifp->if_lastchange);
1566 error = priv_check_cred(cred, PRIV_ROOT, 0);
1569 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1573 ifnet_serialize_all(ifp);
1574 ifp->if_ioctl(ifp, cmd, data, cred);
1575 ifnet_deserialize_all(ifp);
1579 error = priv_check_cred(cred, PRIV_ROOT, 0);
1582 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1585 if (new_name[0] == '\0') {
1589 if (ifunit(new_name) != NULL) {
1594 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1596 /* Announce the departure of the interface. */
1597 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1599 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1600 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1601 /* XXX IFA_LOCK(ifa); */
1602 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1603 namelen = strlen(new_name);
1604 onamelen = sdl->sdl_nlen;
1606 * Move the address if needed. This is safe because we
1607 * allocate space for a name of length IFNAMSIZ when we
1608 * create this in if_attach().
1610 if (namelen != onamelen) {
1611 bcopy(sdl->sdl_data + onamelen,
1612 sdl->sdl_data + namelen, sdl->sdl_alen);
1614 bcopy(new_name, sdl->sdl_data, namelen);
1615 sdl->sdl_nlen = namelen;
1616 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1617 bzero(sdl->sdl_data, onamelen);
1618 while (namelen != 0)
1619 sdl->sdl_data[--namelen] = 0xff;
1620 /* XXX IFA_UNLOCK(ifa) */
1622 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1624 /* Announce the return of the interface. */
1625 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1629 error = priv_check_cred(cred, PRIV_ROOT, 0);
1632 ifp->if_metric = ifr->ifr_metric;
1633 getmicrotime(&ifp->if_lastchange);
1637 error = priv_check_cred(cred, PRIV_ROOT, 0);
1640 if (ifp->if_ioctl == NULL) {
1644 ifnet_serialize_all(ifp);
1645 error = ifp->if_ioctl(ifp, cmd, data, cred);
1646 ifnet_deserialize_all(ifp);
1648 getmicrotime(&ifp->if_lastchange);
1653 u_long oldmtu = ifp->if_mtu;
1655 error = priv_check_cred(cred, PRIV_ROOT, 0);
1658 if (ifp->if_ioctl == NULL) {
1662 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
1666 ifnet_serialize_all(ifp);
1667 error = ifp->if_ioctl(ifp, cmd, data, cred);
1668 ifnet_deserialize_all(ifp);
1670 getmicrotime(&ifp->if_lastchange);
1674 * If the link MTU changed, do network layer specific procedure.
1676 if (ifp->if_mtu != oldmtu) {
1686 error = priv_check_cred(cred, PRIV_ROOT, 0);
1690 /* Don't allow group membership on non-multicast interfaces. */
1691 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1696 /* Don't let users screw up protocols' entries. */
1697 if (ifr->ifr_addr.sa_family != AF_LINK) {
1702 if (cmd == SIOCADDMULTI) {
1703 struct ifmultiaddr *ifma;
1704 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1706 error = if_delmulti(ifp, &ifr->ifr_addr);
1709 getmicrotime(&ifp->if_lastchange);
1712 case SIOCSIFPHYADDR:
1713 case SIOCDIFPHYADDR:
1715 case SIOCSIFPHYADDR_IN6:
1717 case SIOCSLIFPHYADDR:
1719 case SIOCSIFGENERIC:
1720 error = priv_check_cred(cred, PRIV_ROOT, 0);
1723 if (ifp->if_ioctl == 0) {
1727 ifnet_serialize_all(ifp);
1728 error = ifp->if_ioctl(ifp, cmd, data, cred);
1729 ifnet_deserialize_all(ifp);
1731 getmicrotime(&ifp->if_lastchange);
1735 ifs = (struct ifstat *)data;
1736 ifs->ascii[0] = '\0';
1738 case SIOCGIFPSRCADDR:
1739 case SIOCGIFPDSTADDR:
1740 case SIOCGLIFPHYADDR:
1742 case SIOCGIFGENERIC:
1743 if (ifp->if_ioctl == NULL) {
1747 ifnet_serialize_all(ifp);
1748 error = ifp->if_ioctl(ifp, cmd, data, cred);
1749 ifnet_deserialize_all(ifp);
1753 error = priv_check_cred(cred, PRIV_ROOT, 0);
1756 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
1757 ifr->ifr_addr.sa_len);
1758 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
1762 oif_flags = ifp->if_flags;
1763 if (so->so_proto == 0) {
1768 error = so_pru_control_direct(so, cmd, data, ifp);
1773 case SIOCSIFDSTADDR:
1775 case SIOCSIFBRDADDR:
1776 case SIOCSIFNETMASK:
1777 #if BYTE_ORDER != BIG_ENDIAN
1778 if (ifr->ifr_addr.sa_family == 0 &&
1779 ifr->ifr_addr.sa_len < 16) {
1780 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1781 ifr->ifr_addr.sa_len = 16;
1784 if (ifr->ifr_addr.sa_len == 0)
1785 ifr->ifr_addr.sa_len = 16;
1791 case OSIOCGIFDSTADDR:
1792 cmd = SIOCGIFDSTADDR;
1794 case OSIOCGIFBRDADDR:
1795 cmd = SIOCGIFBRDADDR;
1797 case OSIOCGIFNETMASK:
1798 cmd = SIOCGIFNETMASK;
1804 error = so_pru_control_direct(so, cmd, data, ifp);
1808 case OSIOCGIFDSTADDR:
1809 case OSIOCGIFBRDADDR:
1810 case OSIOCGIFNETMASK:
1811 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1814 #endif /* COMPAT_43 */
1816 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1818 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1819 if (ifp->if_flags & IFF_UP) {
1829 mtx_unlock(&ifp->if_ioctl_mtx);
1834 * Set/clear promiscuous mode on interface ifp based on the truth value
1835 * of pswitch. The calls are reference counted so that only the first
1836 * "on" request actually has an effect, as does the final "off" request.
1837 * Results are undefined if the "off" and "on" requests are not matched.
1840 ifpromisc(struct ifnet *ifp, int pswitch)
1846 oldflags = ifp->if_flags;
1847 if (ifp->if_flags & IFF_PPROMISC) {
1848 /* Do nothing if device is in permanently promiscuous mode */
1849 ifp->if_pcount += pswitch ? 1 : -1;
1854 * If the device is not configured up, we cannot put it in
1857 if ((ifp->if_flags & IFF_UP) == 0)
1859 if (ifp->if_pcount++ != 0)
1861 ifp->if_flags |= IFF_PROMISC;
1862 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1865 if (--ifp->if_pcount > 0)
1867 ifp->if_flags &= ~IFF_PROMISC;
1868 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1871 ifr.ifr_flags = ifp->if_flags;
1872 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1873 ifnet_serialize_all(ifp);
1874 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
1875 ifnet_deserialize_all(ifp);
1879 ifp->if_flags = oldflags;
1884 * Return interface configuration
1885 * of system. List may be used
1886 * in later ioctl's (above) to get
1887 * other information.
1890 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
1892 struct ifconf *ifc = (struct ifconf *)data;
1894 struct sockaddr *sa;
1895 struct ifreq ifr, *ifrp;
1896 int space = ifc->ifc_len, error = 0;
1898 ifrp = ifc->ifc_req;
1899 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1900 struct ifaddr_container *ifac;
1903 if (space <= sizeof ifr)
1907 * Zero the stack declared structure first to prevent
1908 * memory disclosure.
1910 bzero(&ifr, sizeof(ifr));
1911 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1912 >= sizeof(ifr.ifr_name)) {
1913 error = ENAMETOOLONG;
1918 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1919 struct ifaddr *ifa = ifac->ifa;
1921 if (space <= sizeof ifr)
1924 if (cred->cr_prison &&
1925 prison_if(cred, sa))
1929 if (cmd == OSIOCGIFCONF) {
1930 struct osockaddr *osa =
1931 (struct osockaddr *)&ifr.ifr_addr;
1933 osa->sa_family = sa->sa_family;
1934 error = copyout(&ifr, ifrp, sizeof ifr);
1938 if (sa->sa_len <= sizeof(*sa)) {
1940 error = copyout(&ifr, ifrp, sizeof ifr);
1943 if (space < (sizeof ifr) + sa->sa_len -
1946 space -= sa->sa_len - sizeof(*sa);
1947 error = copyout(&ifr, ifrp,
1948 sizeof ifr.ifr_name);
1950 error = copyout(sa, &ifrp->ifr_addr,
1952 ifrp = (struct ifreq *)
1953 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1957 space -= sizeof ifr;
1962 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
1963 error = copyout(&ifr, ifrp, sizeof ifr);
1966 space -= sizeof ifr;
1970 ifc->ifc_len -= space;
1975 * Just like if_promisc(), but for all-multicast-reception mode.
1978 if_allmulti(struct ifnet *ifp, int onswitch)
1986 if (ifp->if_amcount++ == 0) {
1987 ifp->if_flags |= IFF_ALLMULTI;
1988 ifr.ifr_flags = ifp->if_flags;
1989 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1990 ifnet_serialize_all(ifp);
1991 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1993 ifnet_deserialize_all(ifp);
1996 if (ifp->if_amcount > 1) {
1999 ifp->if_amcount = 0;
2000 ifp->if_flags &= ~IFF_ALLMULTI;
2001 ifr.ifr_flags = ifp->if_flags;
2002 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2003 ifnet_serialize_all(ifp);
2004 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2006 ifnet_deserialize_all(ifp);
2018 * Add a multicast listenership to the interface in question.
2019 * The link layer provides a routine which converts
2023 struct ifnet *ifp, /* interface to manipulate */
2024 struct sockaddr *sa, /* address to add */
2025 struct ifmultiaddr **retifma)
2027 struct sockaddr *llsa, *dupsa;
2029 struct ifmultiaddr *ifma;
2032 * If the matching multicast address already exists
2033 * then don't add a new one, just add a reference
2035 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2036 if (sa_equal(sa, ifma->ifma_addr)) {
2037 ifma->ifma_refcount++;
2045 * Give the link layer a chance to accept/reject it, and also
2046 * find out which AF_LINK address this maps to, if it isn't one
2049 if (ifp->if_resolvemulti) {
2050 ifnet_serialize_all(ifp);
2051 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2052 ifnet_deserialize_all(ifp);
2059 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2060 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_WAITOK);
2061 bcopy(sa, dupsa, sa->sa_len);
2063 ifma->ifma_addr = dupsa;
2064 ifma->ifma_lladdr = llsa;
2065 ifma->ifma_ifp = ifp;
2066 ifma->ifma_refcount = 1;
2067 ifma->ifma_protospec = 0;
2068 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2071 * Some network interfaces can scan the address list at
2072 * interrupt time; lock them out.
2075 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2081 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2082 if (sa_equal(ifma->ifma_addr, llsa))
2086 ifma->ifma_refcount++;
2088 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2089 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_WAITOK);
2090 bcopy(llsa, dupsa, llsa->sa_len);
2091 ifma->ifma_addr = dupsa;
2092 ifma->ifma_ifp = ifp;
2093 ifma->ifma_refcount = 1;
2095 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2100 * We are certain we have added something, so call down to the
2101 * interface to let them know about it.
2104 ifnet_serialize_all(ifp);
2106 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2107 ifnet_deserialize_all(ifp);
2114 * Remove a reference to a multicast address on this interface. Yell
2115 * if the request does not match an existing membership.
2118 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2120 struct ifmultiaddr *ifma;
2122 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2123 if (sa_equal(sa, ifma->ifma_addr))
2128 if (ifma->ifma_refcount > 1) {
2129 ifma->ifma_refcount--;
2133 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2134 sa = ifma->ifma_lladdr;
2136 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2138 * Make sure the interface driver is notified
2139 * in the case of a link layer mcast group being left.
2141 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL) {
2142 ifnet_serialize_all(ifp);
2143 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2144 ifnet_deserialize_all(ifp);
2147 kfree(ifma->ifma_addr, M_IFMADDR);
2148 kfree(ifma, M_IFMADDR);
2153 * Now look for the link-layer address which corresponds to
2154 * this network address. It had been squirreled away in
2155 * ifma->ifma_lladdr for this purpose (so we don't have
2156 * to call ifp->if_resolvemulti() again), and we saved that
2157 * value in sa above. If some nasty deleted the
2158 * link-layer address out from underneath us, we can deal because
2159 * the address we stored was is not the same as the one which was
2160 * in the record for the link-layer address. (So we don't complain
2163 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2164 if (sa_equal(sa, ifma->ifma_addr))
2169 if (ifma->ifma_refcount > 1) {
2170 ifma->ifma_refcount--;
2175 ifnet_serialize_all(ifp);
2176 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2177 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2178 ifnet_deserialize_all(ifp);
2180 kfree(ifma->ifma_addr, M_IFMADDR);
2181 kfree(sa, M_IFMADDR);
2182 kfree(ifma, M_IFMADDR);
2188 * Delete all multicast group membership for an interface.
2189 * Should be used to quickly flush all multicast filters.
2192 if_delallmulti(struct ifnet *ifp)
2194 struct ifmultiaddr *ifma;
2195 struct ifmultiaddr *next;
2197 TAILQ_FOREACH_MUTABLE(ifma, &ifp->if_multiaddrs, ifma_link, next)
2198 if_delmulti(ifp, ifma->ifma_addr);
2203 * Set the link layer address on an interface.
2205 * At this time we only support certain types of interfaces,
2206 * and we don't allow the length of the address to change.
2209 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2211 struct sockaddr_dl *sdl;
2214 sdl = IF_LLSOCKADDR(ifp);
2217 if (len != sdl->sdl_alen) /* don't allow length to change */
2219 switch (ifp->if_type) {
2220 case IFT_ETHER: /* these types use struct arpcom */
2223 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2224 bcopy(lladdr, LLADDR(sdl), len);
2230 * If the interface is already up, we need
2231 * to re-init it in order to reprogram its
2234 ifnet_serialize_all(ifp);
2235 if ((ifp->if_flags & IFF_UP) != 0) {
2237 struct ifaddr_container *ifac;
2240 ifp->if_flags &= ~IFF_UP;
2241 ifr.ifr_flags = ifp->if_flags;
2242 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2243 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2245 ifp->if_flags |= IFF_UP;
2246 ifr.ifr_flags = ifp->if_flags;
2247 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2248 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2252 * Also send gratuitous ARPs to notify other nodes about
2253 * the address change.
2255 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2256 struct ifaddr *ifa = ifac->ifa;
2258 if (ifa->ifa_addr != NULL &&
2259 ifa->ifa_addr->sa_family == AF_INET)
2260 arp_gratuitous(ifp, ifa);
2264 ifnet_deserialize_all(ifp);
2268 struct ifmultiaddr *
2269 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2271 struct ifmultiaddr *ifma;
2273 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2274 if (sa_equal(ifma->ifma_addr, sa))
2281 * This function locates the first real ethernet MAC from a network
2282 * card and loads it into node, returning 0 on success or ENOENT if
2283 * no suitable interfaces were found. It is used by the uuid code to
2284 * generate a unique 6-byte number.
2287 if_getanyethermac(uint16_t *node, int minlen)
2290 struct sockaddr_dl *sdl;
2292 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2293 if (ifp->if_type != IFT_ETHER)
2295 sdl = IF_LLSOCKADDR(ifp);
2296 if (sdl->sdl_alen < minlen)
2298 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2306 * The name argument must be a pointer to storage which will last as
2307 * long as the interface does. For physical devices, the result of
2308 * device_get_name(dev) is a good choice and for pseudo-devices a
2309 * static string works well.
2312 if_initname(struct ifnet *ifp, const char *name, int unit)
2314 ifp->if_dname = name;
2315 ifp->if_dunit = unit;
2316 if (unit != IF_DUNIT_NONE)
2317 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2319 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2323 if_printf(struct ifnet *ifp, const char *fmt, ...)
2328 retval = kprintf("%s: ", ifp->if_xname);
2329 __va_start(ap, fmt);
2330 retval += kvprintf(fmt, ap);
2336 if_alloc(uint8_t type)
2342 * XXX temporary hack until arpcom is setup in if_l2com
2344 if (type == IFT_ETHER)
2345 size = sizeof(struct arpcom);
2347 size = sizeof(struct ifnet);
2349 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2351 ifp->if_type = type;
2353 if (if_com_alloc[type] != NULL) {
2354 ifp->if_l2com = if_com_alloc[type](type, ifp);
2355 if (ifp->if_l2com == NULL) {
2356 kfree(ifp, M_IFNET);
2364 if_free(struct ifnet *ifp)
2366 kfree(ifp, M_IFNET);
2370 ifq_set_classic(struct ifaltq *ifq)
2372 ifq->altq_enqueue = ifq_classic_enqueue;
2373 ifq->altq_dequeue = ifq_classic_dequeue;
2374 ifq->altq_request = ifq_classic_request;
2378 ifq_classic_enqueue(struct ifaltq *ifq, struct mbuf *m,
2379 struct altq_pktattr *pa __unused)
2381 logifq(enqueue, ifq);
2382 if (IF_QFULL(ifq)) {
2392 ifq_classic_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
2401 logifq(dequeue, ifq);
2405 panic("unsupported ALTQ dequeue op: %d", op);
2407 KKASSERT(mpolled == NULL || mpolled == m);
2412 ifq_classic_request(struct ifaltq *ifq, int req, void *arg)
2419 panic("unsupported ALTQ request: %d", req);
2425 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2427 struct ifaltq *ifq = &ifp->if_snd;
2428 int running = 0, error, start = 0;
2430 ASSERT_IFNET_NOT_SERIALIZED_TX(ifp);
2433 error = ifq_enqueue_locked(ifq, m, pa);
2438 if (!ifq->altq_started) {
2440 * Hold the interlock of ifnet.if_start
2442 ifq->altq_started = 1;
2447 ifp->if_obytes += m->m_pkthdr.len;
2448 if (m->m_flags & M_MCAST)
2452 logifstart(avoid, ifp);
2457 * Try to do direct ifnet.if_start first, if there is
2458 * contention on ifnet's serializer, ifnet.if_start will
2459 * be scheduled on ifnet's CPU.
2461 if (!ifnet_tryserialize_tx(ifp)) {
2463 * ifnet serializer contention happened,
2464 * ifnet.if_start is scheduled on ifnet's
2465 * CPU, and we keep going.
2467 logifstart(contend_sched, ifp);
2468 if_start_schedule(ifp);
2472 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
2473 logifstart(run, ifp);
2475 if ((ifp->if_flags &
2476 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
2480 ifnet_deserialize_tx(ifp);
2482 if (if_start_need_schedule(ifq, running)) {
2484 * More data need to be transmitted, ifnet.if_start is
2485 * scheduled on ifnet's CPU, and we keep going.
2486 * NOTE: ifnet.if_start interlock is not released.
2488 logifstart(sched, ifp);
2489 if_start_schedule(ifp);
2495 ifa_create(int size, int flags)
2500 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
2502 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
2506 ifa->ifa_containers = kmalloc(ncpus * sizeof(struct ifaddr_container),
2507 M_IFADDR, M_WAITOK | M_ZERO);
2508 ifa->ifa_ncnt = ncpus;
2509 for (i = 0; i < ncpus; ++i) {
2510 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
2512 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
2514 ifac->ifa_refcnt = 1;
2517 kprintf("alloc ifa %p %d\n", ifa, size);
2523 ifac_free(struct ifaddr_container *ifac, int cpu_id)
2525 struct ifaddr *ifa = ifac->ifa;
2527 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
2528 KKASSERT(ifac->ifa_refcnt == 0);
2529 KASSERT(ifac->ifa_listmask == 0,
2530 ("ifa is still on %#x lists", ifac->ifa_listmask));
2532 ifac->ifa_magic = IFA_CONTAINER_DEAD;
2534 #ifdef IFADDR_DEBUG_VERBOSE
2535 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
2538 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
2539 ("invalid # of ifac, %d", ifa->ifa_ncnt));
2540 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
2542 kprintf("free ifa %p\n", ifa);
2544 kfree(ifa->ifa_containers, M_IFADDR);
2545 kfree(ifa, M_IFADDR);
2550 ifa_iflink_dispatch(netmsg_t nmsg)
2552 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2553 struct ifaddr *ifa = msg->ifa;
2554 struct ifnet *ifp = msg->ifp;
2556 struct ifaddr_container *ifac;
2560 ifac = &ifa->ifa_containers[cpu];
2561 ASSERT_IFAC_VALID(ifac);
2562 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
2563 ("ifaddr is on if_addrheads"));
2565 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
2567 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
2569 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
2573 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2577 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
2579 struct netmsg_ifaddr msg;
2581 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2582 0, ifa_iflink_dispatch);
2587 ifa_domsg(&msg.base.lmsg, 0);
2591 ifa_ifunlink_dispatch(netmsg_t nmsg)
2593 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2594 struct ifaddr *ifa = msg->ifa;
2595 struct ifnet *ifp = msg->ifp;
2597 struct ifaddr_container *ifac;
2601 ifac = &ifa->ifa_containers[cpu];
2602 ASSERT_IFAC_VALID(ifac);
2603 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
2604 ("ifaddr is not on if_addrhead"));
2606 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
2607 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
2611 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2615 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
2617 struct netmsg_ifaddr msg;
2619 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2620 0, ifa_ifunlink_dispatch);
2624 ifa_domsg(&msg.base.lmsg, 0);
2628 ifa_destroy_dispatch(netmsg_t nmsg)
2630 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2633 ifa_forwardmsg(&nmsg->lmsg, mycpuid + 1);
2637 ifa_destroy(struct ifaddr *ifa)
2639 struct netmsg_ifaddr msg;
2641 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2642 0, ifa_destroy_dispatch);
2645 ifa_domsg(&msg.base.lmsg, 0);
2649 ifnet_portfn(int cpu)
2651 return &ifnet_threads[cpu].td_msgport;
2655 ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
2657 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
2659 if (next_cpu < ncpus)
2660 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
2662 lwkt_replymsg(lmsg, 0);
2666 ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
2668 KKASSERT(cpu < ncpus);
2669 return lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
2673 ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
2675 KKASSERT(cpu < ncpus);
2676 lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
2680 * Generic netmsg service loop. Some protocols may roll their own but all
2681 * must do the basic command dispatch function call done here.
2684 ifnet_service_loop(void *arg __unused)
2688 while ((msg = lwkt_waitport(&curthread->td_msgport, 0))) {
2689 KASSERT(msg->base.nm_dispatch, ("ifnet_service: badmsg"));
2690 msg->base.nm_dispatch(msg);
2695 ifnetinit(void *dummy __unused)
2699 for (i = 0; i < ncpus; ++i) {
2700 struct thread *thr = &ifnet_threads[i];
2702 lwkt_create(ifnet_service_loop, NULL, NULL,
2703 thr, TDF_NOSTART|TDF_FORCE_SPINPORT,
2705 netmsg_service_port_init(&thr->td_msgport);
2711 ifnet_byindex(unsigned short idx)
2715 return ifindex2ifnet[idx];
2719 ifaddr_byindex(unsigned short idx)
2723 ifp = ifnet_byindex(idx);
2726 return TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
2730 if_register_com_alloc(u_char type,
2731 if_com_alloc_t *a, if_com_free_t *f)
2734 KASSERT(if_com_alloc[type] == NULL,
2735 ("if_register_com_alloc: %d already registered", type));
2736 KASSERT(if_com_free[type] == NULL,
2737 ("if_register_com_alloc: %d free already registered", type));
2739 if_com_alloc[type] = a;
2740 if_com_free[type] = f;
2744 if_deregister_com_alloc(u_char type)
2747 KASSERT(if_com_alloc[type] != NULL,
2748 ("if_deregister_com_alloc: %d not registered", type));
2749 KASSERT(if_com_free[type] != NULL,
2750 ("if_deregister_com_alloc: %d free not registered", type));
2751 if_com_alloc[type] = NULL;
2752 if_com_free[type] = NULL;
2756 if_ring_count2(int cnt, int cnt_max)
2760 KASSERT(cnt_max >= 1 && powerof2(cnt_max),
2761 ("invalid ring count max %d", cnt_max));
2770 while ((1 << (shift + 1)) <= cnt)
2774 KASSERT(cnt >= 1 && cnt <= ncpus2 && cnt <= cnt_max,
2775 ("calculate cnt %d, ncpus2 %d, cnt max %d",
2776 cnt, ncpus2, cnt_max));