2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)if.c 8.3 (Berkeley) 1/4/94
34 * $FreeBSD: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $
35 * $DragonFly: src/sys/net/if.c,v 1.84 2008/11/15 11:58:16 sephe Exp $
38 #include "opt_compat.h"
39 #include "opt_inet6.h"
41 #include "opt_polling.h"
42 #include "opt_ifpoll.h"
44 #include <sys/param.h>
45 #include <sys/malloc.h>
47 #include <sys/systm.h>
50 #include <sys/protosw.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/socketops.h>
54 #include <sys/protosw.h>
55 #include <sys/kernel.h>
57 #include <sys/sockio.h>
58 #include <sys/syslog.h>
59 #include <sys/sysctl.h>
60 #include <sys/domain.h>
61 #include <sys/thread.h>
62 #include <sys/thread2.h>
63 #include <sys/serialize.h>
64 #include <sys/msgport2.h>
68 #include <net/if_arp.h>
69 #include <net/if_dl.h>
70 #include <net/if_types.h>
71 #include <net/if_var.h>
72 #include <net/ifq_var.h>
73 #include <net/radix.h>
74 #include <net/route.h>
75 #include <net/if_clone.h>
76 #include <net/netisr.h>
77 #include <net/netmsg2.h>
79 #include <machine/atomic.h>
80 #include <machine/stdarg.h>
81 #include <machine/smp.h>
83 #if defined(INET) || defined(INET6)
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet/if_ether.h>
89 #include <netinet6/in6_var.h>
90 #include <netinet6/in6_ifattach.h>
94 #if defined(COMPAT_43)
95 #include <emulation/43bsd/43bsd_socket.h>
96 #endif /* COMPAT_43 */
98 struct netmsg_ifaddr {
106 * System initialization
108 static void if_attachdomain(void *);
109 static void if_attachdomain1(struct ifnet *);
110 static int ifconf(u_long, caddr_t, struct ucred *);
111 static void ifinit(void *);
112 static void ifnetinit(void *);
113 static void if_slowtimo(void *);
114 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
115 static int if_rtdel(struct radix_node *, void *);
119 * XXX: declare here to avoid to include many inet6 related files..
120 * should be more generalized?
122 extern void nd6_setmtu(struct ifnet *);
125 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
126 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
128 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
129 /* Must be after netisr_init */
130 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_SECOND, ifnetinit, NULL)
132 static if_com_alloc_t *if_com_alloc[256];
133 static if_com_free_t *if_com_free[256];
135 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
136 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
137 MALLOC_DEFINE(M_IFNET, "ifnet", "interface structure");
139 int ifqmaxlen = IFQ_MAXLEN;
140 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
142 /* In ifq_dispatch(), try to do direct ifnet.if_start first */
143 static int ifq_dispatch_schedonly = 0;
144 SYSCTL_INT(_net_link_generic, OID_AUTO, ifq_dispatch_schedonly, CTLFLAG_RW,
145 &ifq_dispatch_schedonly, 0, "");
147 /* In ifq_dispatch(), schedule ifnet.if_start without checking ifnet.if_snd */
148 static int ifq_dispatch_schednochk = 0;
149 SYSCTL_INT(_net_link_generic, OID_AUTO, ifq_dispatch_schednochk, CTLFLAG_RW,
150 &ifq_dispatch_schednochk, 0, "");
152 /* In if_devstart(), try to do direct ifnet.if_start first */
153 static int if_devstart_schedonly = 0;
154 SYSCTL_INT(_net_link_generic, OID_AUTO, if_devstart_schedonly, CTLFLAG_RW,
155 &if_devstart_schedonly, 0, "");
157 /* In if_devstart(), schedule ifnet.if_start without checking ifnet.if_snd */
158 static int if_devstart_schednochk = 0;
159 SYSCTL_INT(_net_link_generic, OID_AUTO, if_devstart_schednochk, CTLFLAG_RW,
160 &if_devstart_schednochk, 0, "");
163 /* Schedule ifnet.if_start on the current CPU */
164 static int if_start_oncpu_sched = 0;
165 SYSCTL_INT(_net_link_generic, OID_AUTO, if_start_oncpu_sched, CTLFLAG_RW,
166 &if_start_oncpu_sched, 0, "");
169 struct callout if_slowtimo_timer;
172 struct ifnet **ifindex2ifnet = NULL;
173 static struct thread ifnet_threads[MAXCPU];
174 static int ifnet_mpsafe_thread = NETMSG_SERVICE_MPSAFE;
176 #define IFQ_KTR_STRING "ifq=%p"
177 #define IFQ_KTR_ARG_SIZE (sizeof(void *))
179 #define KTR_IFQ KTR_ALL
181 KTR_INFO_MASTER(ifq);
182 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARG_SIZE);
183 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARG_SIZE);
184 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
186 #define IF_START_KTR_STRING "ifp=%p"
187 #define IF_START_KTR_ARG_SIZE (sizeof(void *))
189 #define KTR_IF_START KTR_ALL
191 KTR_INFO_MASTER(if_start);
192 KTR_INFO(KTR_IF_START, if_start, run, 0,
193 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
194 KTR_INFO(KTR_IF_START, if_start, sched, 1,
195 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
196 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
197 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
198 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
199 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
201 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
202 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
204 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
207 * Network interface utility routines.
209 * Routines with ifa_ifwith* names take sockaddr *'s as
218 callout_init(&if_slowtimo_timer);
221 TAILQ_FOREACH(ifp, &ifnet, if_link) {
222 if (ifp->if_snd.ifq_maxlen == 0) {
223 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
224 ifp->if_snd.ifq_maxlen = ifqmaxlen;
233 if_start_cpuid(struct ifnet *ifp)
235 return ifp->if_cpuid;
238 #ifdef DEVICE_POLLING
240 if_start_cpuid_poll(struct ifnet *ifp)
242 int poll_cpuid = ifp->if_poll_cpuid;
247 return ifp->if_cpuid;
252 if_start_ipifunc(void *arg)
254 struct ifnet *ifp = arg;
255 struct lwkt_msg *lmsg = &ifp->if_start_nmsg[mycpuid].nm_lmsg;
258 if (lmsg->ms_flags & MSGF_DONE)
259 lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg);
264 * Schedule ifnet.if_start on ifnet's CPU
267 if_start_schedule(struct ifnet *ifp)
272 if (if_start_oncpu_sched)
275 cpu = ifp->if_start_cpuid(ifp);
278 lwkt_send_ipiq(globaldata_find(cpu), if_start_ipifunc, ifp);
281 if_start_ipifunc(ifp);
286 * This function will release ifnet.if_start interlock,
287 * if ifnet.if_start does not need to be scheduled
290 if_start_need_schedule(struct ifaltq *ifq, int running)
292 if (!running || ifq_is_empty(ifq)
294 || ifq->altq_tbr != NULL
299 * ifnet.if_start interlock is released, if:
300 * 1) Hardware can not take any packets, due to
301 * o interface is marked down
302 * o hardware queue is full (IFF_OACTIVE)
303 * Under the second situation, hardware interrupt
304 * or polling(4) will call/schedule ifnet.if_start
305 * when hardware queue is ready
306 * 2) There is not packet in the ifnet.if_snd.
307 * Further ifq_dispatch or ifq_handoff will call/
308 * schedule ifnet.if_start
309 * 3) TBR is used and it does not allow further
311 * TBR callout will call ifnet.if_start
313 if (!running || !ifq_data_ready(ifq)) {
314 ifq->altq_started = 0;
324 if_start_dispatch(struct netmsg *nmsg)
326 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
327 struct ifnet *ifp = lmsg->u.ms_resultp;
328 struct ifaltq *ifq = &ifp->if_snd;
332 lwkt_replymsg(lmsg, 0); /* reply ASAP */
336 if (!if_start_oncpu_sched && mycpuid != ifp->if_start_cpuid(ifp)) {
338 * If the ifnet is still up, we need to
339 * chase its CPU change.
341 if (ifp->if_flags & IFF_UP) {
342 logifstart(chase_sched, ifp);
343 if_start_schedule(ifp);
351 if (ifp->if_flags & IFF_UP) {
352 ifnet_serialize_tx(ifp); /* XXX try? */
353 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
354 logifstart(run, ifp);
357 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
360 ifnet_deserialize_tx(ifp);
365 if (if_start_need_schedule(ifq, running)) {
367 if (lmsg->ms_flags & MSGF_DONE) { /* XXX necessary? */
368 logifstart(sched, ifp);
369 lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg);
375 /* Device driver ifnet.if_start helper function */
377 if_devstart(struct ifnet *ifp)
379 struct ifaltq *ifq = &ifp->if_snd;
382 ASSERT_IFNET_SERIALIZED_TX(ifp);
385 if (ifq->altq_started || !ifq_data_ready(ifq)) {
386 logifstart(avoid, ifp);
390 ifq->altq_started = 1;
393 if (if_devstart_schedonly) {
395 * Always schedule ifnet.if_start on ifnet's CPU,
396 * short circuit the rest of this function.
398 logifstart(sched, ifp);
399 if_start_schedule(ifp);
403 logifstart(run, ifp);
406 if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
409 if (if_devstart_schednochk || if_start_need_schedule(ifq, running)) {
411 * More data need to be transmitted, ifnet.if_start is
412 * scheduled on ifnet's CPU, and we keep going.
413 * NOTE: ifnet.if_start interlock is not released.
415 logifstart(sched, ifp);
416 if_start_schedule(ifp);
421 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
423 lwkt_serialize_enter(ifp->if_serializer);
427 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
429 lwkt_serialize_exit(ifp->if_serializer);
433 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
435 return lwkt_serialize_try(ifp->if_serializer);
440 if_default_serialize_assert(struct ifnet *ifp,
441 enum ifnet_serialize slz __unused,
442 boolean_t serialized)
445 ASSERT_SERIALIZED(ifp->if_serializer);
447 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
452 * Attach an interface to the list of "active" interfaces.
454 * The serializer is optional. If non-NULL access to the interface
458 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
460 unsigned socksize, ifasize;
461 int namelen, masklen;
462 struct sockaddr_dl *sdl;
467 static int if_indexlim = 8;
469 if (ifp->if_serialize != NULL) {
470 KASSERT(ifp->if_deserialize != NULL &&
471 ifp->if_tryserialize != NULL &&
472 ifp->if_serialize_assert != NULL,
473 ("serialize functions are partially setup\n"));
476 * If the device supplies serialize functions,
477 * then clear if_serializer to catch any invalid
478 * usage of this field.
480 KASSERT(serializer == NULL,
481 ("both serialize functions and default serializer "
483 ifp->if_serializer = NULL;
485 KASSERT(ifp->if_deserialize == NULL &&
486 ifp->if_tryserialize == NULL &&
487 ifp->if_serialize_assert == NULL,
488 ("serialize functions are partially setup\n"));
489 ifp->if_serialize = if_default_serialize;
490 ifp->if_deserialize = if_default_deserialize;
491 ifp->if_tryserialize = if_default_tryserialize;
493 ifp->if_serialize_assert = if_default_serialize_assert;
497 * The serializer can be passed in from the device,
498 * allowing the same serializer to be used for both
499 * the interrupt interlock and the device queue.
500 * If not specified, the netif structure will use an
501 * embedded serializer.
503 if (serializer == NULL) {
504 serializer = &ifp->if_default_serializer;
505 lwkt_serialize_init(serializer);
507 ifp->if_serializer = serializer;
510 ifp->if_start_cpuid = if_start_cpuid;
513 #ifdef DEVICE_POLLING
514 /* Device is not in polling mode by default */
515 ifp->if_poll_cpuid = -1;
516 if (ifp->if_poll != NULL)
517 ifp->if_start_cpuid = if_start_cpuid_poll;
520 ifp->if_start_nmsg = kmalloc(ncpus * sizeof(struct netmsg),
521 M_LWKTMSG, M_WAITOK);
522 for (i = 0; i < ncpus; ++i) {
523 netmsg_init(&ifp->if_start_nmsg[i], NULL, &netisr_adone_rport,
524 0, if_start_dispatch);
525 ifp->if_start_nmsg[i].nm_lmsg.u.ms_resultp = ifp;
528 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
529 ifp->if_index = ++if_index;
533 * The old code would work if the interface passed a pre-existing
534 * chain of ifaddrs to this code. We don't trust our callers to
535 * properly initialize the tailq, however, so we no longer allow
536 * this unlikely case.
538 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
539 M_IFADDR, M_WAITOK | M_ZERO);
540 for (i = 0; i < ncpus; ++i)
541 TAILQ_INIT(&ifp->if_addrheads[i]);
543 TAILQ_INIT(&ifp->if_prefixhead);
544 LIST_INIT(&ifp->if_multiaddrs);
545 getmicrotime(&ifp->if_lastchange);
546 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
552 /* grow ifindex2ifnet */
553 n = if_indexlim * sizeof(*q);
554 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
556 bcopy(ifindex2ifnet, q, n/2);
557 kfree(ifindex2ifnet, M_IFADDR);
562 ifindex2ifnet[if_index] = ifp;
565 * create a Link Level name for this device
567 namelen = strlen(ifp->if_xname);
568 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
569 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
570 socksize = masklen + ifp->if_addrlen;
571 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
572 if (socksize < sizeof(*sdl))
573 socksize = sizeof(*sdl);
574 socksize = ROUNDUP(socksize);
575 ifasize = sizeof(struct ifaddr) + 2 * socksize;
576 ifa = ifa_create(ifasize, M_WAITOK);
577 sdl = (struct sockaddr_dl *)(ifa + 1);
578 sdl->sdl_len = socksize;
579 sdl->sdl_family = AF_LINK;
580 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
581 sdl->sdl_nlen = namelen;
582 sdl->sdl_index = ifp->if_index;
583 sdl->sdl_type = ifp->if_type;
584 ifp->if_lladdr = ifa;
586 ifa->ifa_rtrequest = link_rtrequest;
587 ifa->ifa_addr = (struct sockaddr *)sdl;
588 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
589 ifa->ifa_netmask = (struct sockaddr *)sdl;
590 sdl->sdl_len = masklen;
592 sdl->sdl_data[--namelen] = 0xff;
593 ifa_iflink(ifa, ifp, 0 /* Insert head */);
595 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
596 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
600 ifq->altq_disc = NULL;
601 ifq->altq_flags &= ALTQF_CANTCHANGE;
602 ifq->altq_tbr = NULL;
604 ifq->altq_started = 0;
605 ifq->altq_prepended = NULL;
607 ifq_set_classic(ifq);
609 if (!SLIST_EMPTY(&domains))
610 if_attachdomain1(ifp);
612 /* Announce the interface. */
613 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
617 if_attachdomain(void *dummy)
622 TAILQ_FOREACH(ifp, &ifnet, if_list)
623 if_attachdomain1(ifp);
626 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
627 if_attachdomain, NULL);
630 if_attachdomain1(struct ifnet *ifp)
636 /* address family dependent data region */
637 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
638 SLIST_FOREACH(dp, &domains, dom_next)
639 if (dp->dom_ifattach)
640 ifp->if_afdata[dp->dom_family] =
641 (*dp->dom_ifattach)(ifp);
646 * Purge all addresses whose type is _not_ AF_LINK
649 if_purgeaddrs_nolink(struct ifnet *ifp)
651 struct ifaddr_container *ifac, *next;
653 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
655 struct ifaddr *ifa = ifac->ifa;
657 /* Leave link ifaddr as it is */
658 if (ifa->ifa_addr->sa_family == AF_LINK)
661 /* XXX: Ugly!! ad hoc just for INET */
662 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
663 struct ifaliasreq ifr;
664 #ifdef IFADDR_DEBUG_VERBOSE
667 kprintf("purge in4 addr %p: ", ifa);
668 for (i = 0; i < ncpus; ++i)
669 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
673 bzero(&ifr, sizeof ifr);
674 ifr.ifra_addr = *ifa->ifa_addr;
675 if (ifa->ifa_dstaddr)
676 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
677 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
683 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
684 #ifdef IFADDR_DEBUG_VERBOSE
687 kprintf("purge in6 addr %p: ", ifa);
688 for (i = 0; i < ncpus; ++i)
689 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
694 /* ifp_addrhead is already updated */
698 ifa_ifunlink(ifa, ifp);
704 * Detach an interface, removing it from the
705 * list of "active" interfaces.
708 if_detach(struct ifnet *ifp)
710 struct radix_node_head *rnh;
715 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
718 * Remove routes and flush queues.
721 #ifdef DEVICE_POLLING
722 if (ifp->if_flags & IFF_POLLING)
723 ether_poll_deregister(ifp);
726 if (ifp->if_flags & IFF_NPOLLING)
727 ifpoll_deregister(ifp);
731 if (ifq_is_enabled(&ifp->if_snd))
732 altq_disable(&ifp->if_snd);
733 if (ifq_is_attached(&ifp->if_snd))
734 altq_detach(&ifp->if_snd);
737 * Clean up all addresses.
739 ifp->if_lladdr = NULL;
741 if_purgeaddrs_nolink(ifp);
742 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
745 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
746 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
747 ("non-link ifaddr is left on if_addrheads"));
749 ifa_ifunlink(ifa, ifp);
751 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
752 ("there are still ifaddrs left on if_addrheads"));
757 * Remove all IPv4 kernel structures related to ifp.
764 * Remove all IPv6 kernel structs related to ifp. This should be done
765 * before removing routing entries below, since IPv6 interface direct
766 * routes are expected to be removed by the IPv6-specific kernel API.
767 * Otherwise, the kernel will detect some inconsistency and bark it.
773 * Delete all remaining routes using this interface
774 * Unfortuneatly the only way to do this is to slog through
775 * the entire routing table looking for routes which point
776 * to this interface...oh well...
779 for (cpu = 0; cpu < ncpus2; cpu++) {
780 lwkt_migratecpu(cpu);
781 for (i = 1; i <= AF_MAX; i++) {
782 if ((rnh = rt_tables[cpu][i]) == NULL)
784 rnh->rnh_walktree(rnh, if_rtdel, ifp);
787 lwkt_migratecpu(origcpu);
789 /* Announce that the interface is gone. */
790 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
791 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
793 SLIST_FOREACH(dp, &domains, dom_next)
794 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
795 (*dp->dom_ifdetach)(ifp,
796 ifp->if_afdata[dp->dom_family]);
799 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
801 ifindex2ifnet[ifp->if_index] = NULL;
802 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
805 TAILQ_REMOVE(&ifnet, ifp, if_link);
806 kfree(ifp->if_addrheads, M_IFADDR);
807 kfree(ifp->if_start_nmsg, M_LWKTMSG);
812 * Delete Routes for a Network Interface
814 * Called for each routing entry via the rnh->rnh_walktree() call above
815 * to delete all route entries referencing a detaching network interface.
818 * rn pointer to node in the routing table
819 * arg argument passed to rnh->rnh_walktree() - detaching interface
823 * errno failed - reason indicated
827 if_rtdel(struct radix_node *rn, void *arg)
829 struct rtentry *rt = (struct rtentry *)rn;
830 struct ifnet *ifp = arg;
833 if (rt->rt_ifp == ifp) {
836 * Protect (sorta) against walktree recursion problems
839 if (!(rt->rt_flags & RTF_UP))
842 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
843 rt_mask(rt), rt->rt_flags,
846 log(LOG_WARNING, "if_rtdel: error %d\n", err);
854 * Locate an interface based on a complete address.
857 ifa_ifwithaddr(struct sockaddr *addr)
861 TAILQ_FOREACH(ifp, &ifnet, if_link) {
862 struct ifaddr_container *ifac;
864 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
865 struct ifaddr *ifa = ifac->ifa;
867 if (ifa->ifa_addr->sa_family != addr->sa_family)
869 if (sa_equal(addr, ifa->ifa_addr))
871 if ((ifp->if_flags & IFF_BROADCAST) &&
872 ifa->ifa_broadaddr &&
873 /* IPv6 doesn't have broadcast */
874 ifa->ifa_broadaddr->sa_len != 0 &&
875 sa_equal(ifa->ifa_broadaddr, addr))
882 * Locate the point to point interface with a given destination address.
885 ifa_ifwithdstaddr(struct sockaddr *addr)
889 TAILQ_FOREACH(ifp, &ifnet, if_link) {
890 struct ifaddr_container *ifac;
892 if (!(ifp->if_flags & IFF_POINTOPOINT))
895 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
896 struct ifaddr *ifa = ifac->ifa;
898 if (ifa->ifa_addr->sa_family != addr->sa_family)
900 if (ifa->ifa_dstaddr &&
901 sa_equal(addr, ifa->ifa_dstaddr))
909 * Find an interface on a specific network. If many, choice
910 * is most specific found.
913 ifa_ifwithnet(struct sockaddr *addr)
916 struct ifaddr *ifa_maybe = NULL;
917 u_int af = addr->sa_family;
918 char *addr_data = addr->sa_data, *cplim;
921 * AF_LINK addresses can be looked up directly by their index number,
922 * so do that if we can.
925 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
927 if (sdl->sdl_index && sdl->sdl_index <= if_index)
928 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
932 * Scan though each interface, looking for ones that have
933 * addresses in this address family.
935 TAILQ_FOREACH(ifp, &ifnet, if_link) {
936 struct ifaddr_container *ifac;
938 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
939 struct ifaddr *ifa = ifac->ifa;
940 char *cp, *cp2, *cp3;
942 if (ifa->ifa_addr->sa_family != af)
944 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
946 * This is a bit broken as it doesn't
947 * take into account that the remote end may
948 * be a single node in the network we are
950 * The trouble is that we don't know the
951 * netmask for the remote end.
953 if (ifa->ifa_dstaddr != NULL &&
954 sa_equal(addr, ifa->ifa_dstaddr))
958 * if we have a special address handler,
959 * then use it instead of the generic one.
961 if (ifa->ifa_claim_addr) {
962 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
970 * Scan all the bits in the ifa's address.
971 * If a bit dissagrees with what we are
972 * looking for, mask it with the netmask
973 * to see if it really matters.
976 if (ifa->ifa_netmask == 0)
979 cp2 = ifa->ifa_addr->sa_data;
980 cp3 = ifa->ifa_netmask->sa_data;
981 cplim = ifa->ifa_netmask->sa_len +
982 (char *)ifa->ifa_netmask;
984 if ((*cp++ ^ *cp2++) & *cp3++)
985 goto next; /* next address! */
987 * If the netmask of what we just found
988 * is more specific than what we had before
989 * (if we had one) then remember the new one
990 * before continuing to search
991 * for an even better one.
993 if (ifa_maybe == 0 ||
994 rn_refines((char *)ifa->ifa_netmask,
995 (char *)ifa_maybe->ifa_netmask))
1004 * Find an interface address specific to an interface best matching
1008 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1010 struct ifaddr_container *ifac;
1011 char *cp, *cp2, *cp3;
1013 struct ifaddr *ifa_maybe = 0;
1014 u_int af = addr->sa_family;
1018 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1019 struct ifaddr *ifa = ifac->ifa;
1021 if (ifa->ifa_addr->sa_family != af)
1025 if (ifa->ifa_netmask == NULL) {
1026 if (sa_equal(addr, ifa->ifa_addr) ||
1027 (ifa->ifa_dstaddr != NULL &&
1028 sa_equal(addr, ifa->ifa_dstaddr)))
1032 if (ifp->if_flags & IFF_POINTOPOINT) {
1033 if (sa_equal(addr, ifa->ifa_dstaddr))
1037 cp2 = ifa->ifa_addr->sa_data;
1038 cp3 = ifa->ifa_netmask->sa_data;
1039 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1040 for (; cp3 < cplim; cp3++)
1041 if ((*cp++ ^ *cp2++) & *cp3)
1051 * Default action when installing a route with a Link Level gateway.
1052 * Lookup an appropriate real ifa to point to.
1053 * This should be moved to /sys/net/link.c eventually.
1056 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1059 struct sockaddr *dst;
1062 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1063 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1065 ifa = ifaof_ifpforaddr(dst, ifp);
1067 IFAFREE(rt->rt_ifa);
1070 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1071 ifa->ifa_rtrequest(cmd, rt, info);
1076 * Mark an interface down and notify protocols of
1078 * NOTE: must be called at splnet or eqivalent.
1081 if_unroute(struct ifnet *ifp, int flag, int fam)
1083 struct ifaddr_container *ifac;
1085 ifp->if_flags &= ~flag;
1086 getmicrotime(&ifp->if_lastchange);
1087 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1088 struct ifaddr *ifa = ifac->ifa;
1090 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1091 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1093 ifq_purge(&ifp->if_snd);
1098 * Mark an interface up and notify protocols of
1100 * NOTE: must be called at splnet or eqivalent.
1103 if_route(struct ifnet *ifp, int flag, int fam)
1105 struct ifaddr_container *ifac;
1107 ifq_purge(&ifp->if_snd);
1108 ifp->if_flags |= flag;
1109 getmicrotime(&ifp->if_lastchange);
1110 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1111 struct ifaddr *ifa = ifac->ifa;
1113 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1114 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1123 * Mark an interface down and notify protocols of the transition. An
1124 * interface going down is also considered to be a synchronizing event.
1125 * We must ensure that all packet processing related to the interface
1126 * has completed before we return so e.g. the caller can free the ifnet
1127 * structure that the mbufs may be referencing.
1129 * NOTE: must be called at splnet or eqivalent.
1132 if_down(struct ifnet *ifp)
1134 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1135 netmsg_service_sync();
1139 * Mark an interface up and notify protocols of
1141 * NOTE: must be called at splnet or eqivalent.
1144 if_up(struct ifnet *ifp)
1146 if_route(ifp, IFF_UP, AF_UNSPEC);
1150 * Process a link state change.
1151 * NOTE: must be called at splsoftnet or equivalent.
1154 if_link_state_change(struct ifnet *ifp)
1156 int link_state = ifp->if_link_state;
1159 devctl_notify("IFNET", ifp->if_xname,
1160 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1164 * Handle interface watchdog timer routines. Called
1165 * from softclock, we decrement timers (if set) and
1166 * call the appropriate interface routine on expiration.
1169 if_slowtimo(void *arg)
1175 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1176 if (ifp->if_timer == 0 || --ifp->if_timer)
1178 if (ifp->if_watchdog) {
1179 if (ifnet_tryserialize_all(ifp)) {
1180 (*ifp->if_watchdog)(ifp);
1181 ifnet_deserialize_all(ifp);
1183 /* try again next timeout */
1191 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1195 * Map interface name to
1196 * interface structure pointer.
1199 ifunit(const char *name)
1204 * Search all the interfaces for this name/number
1207 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1208 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1216 * Map interface name in a sockaddr_dl to
1217 * interface structure pointer.
1220 if_withname(struct sockaddr *sa)
1222 char ifname[IFNAMSIZ+1];
1223 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1225 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1226 (sdl->sdl_nlen > IFNAMSIZ) )
1230 * ifunit wants a null-terminated name. It may not be null-terminated
1231 * in the sockaddr. We don't want to change the caller's sockaddr,
1232 * and there might not be room to put the trailing null anyway, so we
1233 * make a local copy that we know we can null terminate safely.
1236 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1237 ifname[sdl->sdl_nlen] = '\0';
1238 return ifunit(ifname);
1246 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1254 size_t namelen, onamelen;
1255 char new_name[IFNAMSIZ];
1257 struct sockaddr_dl *sdl;
1263 return (ifconf(cmd, data, cred));
1265 ifr = (struct ifreq *)data;
1270 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1272 return ((cmd == SIOCIFCREATE) ?
1273 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1274 if_clone_destroy(ifr->ifr_name));
1276 case SIOCIFGCLONERS:
1277 return (if_clone_list((struct if_clonereq *)data));
1280 ifp = ifunit(ifr->ifr_name);
1286 ifr->ifr_index = ifp->if_index;
1290 ifr->ifr_flags = ifp->if_flags;
1291 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1295 ifr->ifr_reqcap = ifp->if_capabilities;
1296 ifr->ifr_curcap = ifp->if_capenable;
1300 ifr->ifr_metric = ifp->if_metric;
1304 ifr->ifr_mtu = ifp->if_mtu;
1308 ifr->ifr_phys = ifp->if_physical;
1311 case SIOCGIFPOLLCPU:
1312 #ifdef DEVICE_POLLING
1313 ifr->ifr_pollcpu = ifp->if_poll_cpuid;
1315 ifr->ifr_pollcpu = -1;
1319 case SIOCSIFPOLLCPU:
1320 #ifdef DEVICE_POLLING
1321 if ((ifp->if_flags & IFF_POLLING) == 0)
1322 ether_pollcpu_register(ifp, ifr->ifr_pollcpu);
1327 error = priv_check_cred(cred, PRIV_ROOT, 0);
1330 new_flags = (ifr->ifr_flags & 0xffff) |
1331 (ifr->ifr_flagshigh << 16);
1332 if (ifp->if_flags & IFF_SMART) {
1333 /* Smart drivers twiddle their own routes */
1334 } else if (ifp->if_flags & IFF_UP &&
1335 (new_flags & IFF_UP) == 0) {
1339 } else if (new_flags & IFF_UP &&
1340 (ifp->if_flags & IFF_UP) == 0) {
1346 #ifdef DEVICE_POLLING
1347 if ((new_flags ^ ifp->if_flags) & IFF_POLLING) {
1348 if (new_flags & IFF_POLLING) {
1349 ether_poll_register(ifp);
1351 ether_poll_deregister(ifp);
1355 #ifdef IFPOLL_ENABLE
1356 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1357 if (new_flags & IFF_NPOLLING)
1358 ifpoll_register(ifp);
1360 ifpoll_deregister(ifp);
1364 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1365 (new_flags &~ IFF_CANTCHANGE);
1366 if (new_flags & IFF_PPROMISC) {
1367 /* Permanently promiscuous mode requested */
1368 ifp->if_flags |= IFF_PROMISC;
1369 } else if (ifp->if_pcount == 0) {
1370 ifp->if_flags &= ~IFF_PROMISC;
1372 if (ifp->if_ioctl) {
1373 ifnet_serialize_all(ifp);
1374 ifp->if_ioctl(ifp, cmd, data, cred);
1375 ifnet_deserialize_all(ifp);
1377 getmicrotime(&ifp->if_lastchange);
1381 error = priv_check_cred(cred, PRIV_ROOT, 0);
1384 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1386 ifnet_serialize_all(ifp);
1387 ifp->if_ioctl(ifp, cmd, data, cred);
1388 ifnet_deserialize_all(ifp);
1392 error = priv_check_cred(cred, PRIV_ROOT, 0);
1395 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1398 if (new_name[0] == '\0')
1400 if (ifunit(new_name) != NULL)
1403 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1405 /* Announce the departure of the interface. */
1406 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1408 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1409 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1410 /* XXX IFA_LOCK(ifa); */
1411 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1412 namelen = strlen(new_name);
1413 onamelen = sdl->sdl_nlen;
1415 * Move the address if needed. This is safe because we
1416 * allocate space for a name of length IFNAMSIZ when we
1417 * create this in if_attach().
1419 if (namelen != onamelen) {
1420 bcopy(sdl->sdl_data + onamelen,
1421 sdl->sdl_data + namelen, sdl->sdl_alen);
1423 bcopy(new_name, sdl->sdl_data, namelen);
1424 sdl->sdl_nlen = namelen;
1425 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1426 bzero(sdl->sdl_data, onamelen);
1427 while (namelen != 0)
1428 sdl->sdl_data[--namelen] = 0xff;
1429 /* XXX IFA_UNLOCK(ifa) */
1431 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1433 /* Announce the return of the interface. */
1434 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1438 error = priv_check_cred(cred, PRIV_ROOT, 0);
1441 ifp->if_metric = ifr->ifr_metric;
1442 getmicrotime(&ifp->if_lastchange);
1446 error = priv_check_cred(cred, PRIV_ROOT, 0);
1451 ifnet_serialize_all(ifp);
1452 error = ifp->if_ioctl(ifp, cmd, data, cred);
1453 ifnet_deserialize_all(ifp);
1455 getmicrotime(&ifp->if_lastchange);
1460 u_long oldmtu = ifp->if_mtu;
1462 error = priv_check_cred(cred, PRIV_ROOT, 0);
1465 if (ifp->if_ioctl == NULL)
1466 return (EOPNOTSUPP);
1467 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1469 ifnet_serialize_all(ifp);
1470 error = ifp->if_ioctl(ifp, cmd, data, cred);
1471 ifnet_deserialize_all(ifp);
1473 getmicrotime(&ifp->if_lastchange);
1477 * If the link MTU changed, do network layer specific procedure.
1479 if (ifp->if_mtu != oldmtu) {
1489 error = priv_check_cred(cred, PRIV_ROOT, 0);
1493 /* Don't allow group membership on non-multicast interfaces. */
1494 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1497 /* Don't let users screw up protocols' entries. */
1498 if (ifr->ifr_addr.sa_family != AF_LINK)
1501 if (cmd == SIOCADDMULTI) {
1502 struct ifmultiaddr *ifma;
1503 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1505 error = if_delmulti(ifp, &ifr->ifr_addr);
1508 getmicrotime(&ifp->if_lastchange);
1511 case SIOCSIFPHYADDR:
1512 case SIOCDIFPHYADDR:
1514 case SIOCSIFPHYADDR_IN6:
1516 case SIOCSLIFPHYADDR:
1518 case SIOCSIFGENERIC:
1519 error = priv_check_cred(cred, PRIV_ROOT, 0);
1522 if (ifp->if_ioctl == 0)
1523 return (EOPNOTSUPP);
1524 ifnet_serialize_all(ifp);
1525 error = ifp->if_ioctl(ifp, cmd, data, cred);
1526 ifnet_deserialize_all(ifp);
1528 getmicrotime(&ifp->if_lastchange);
1532 ifs = (struct ifstat *)data;
1533 ifs->ascii[0] = '\0';
1535 case SIOCGIFPSRCADDR:
1536 case SIOCGIFPDSTADDR:
1537 case SIOCGLIFPHYADDR:
1539 case SIOCGIFGENERIC:
1540 if (ifp->if_ioctl == NULL)
1541 return (EOPNOTSUPP);
1542 ifnet_serialize_all(ifp);
1543 error = ifp->if_ioctl(ifp, cmd, data, cred);
1544 ifnet_deserialize_all(ifp);
1548 error = priv_check_cred(cred, PRIV_ROOT, 0);
1551 return if_setlladdr(ifp,
1552 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1555 oif_flags = ifp->if_flags;
1556 if (so->so_proto == 0)
1557 return (EOPNOTSUPP);
1559 error = so_pru_control(so, cmd, data, ifp);
1566 case SIOCSIFDSTADDR:
1568 case SIOCSIFBRDADDR:
1569 case SIOCSIFNETMASK:
1570 #if BYTE_ORDER != BIG_ENDIAN
1571 if (ifr->ifr_addr.sa_family == 0 &&
1572 ifr->ifr_addr.sa_len < 16) {
1573 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1574 ifr->ifr_addr.sa_len = 16;
1577 if (ifr->ifr_addr.sa_len == 0)
1578 ifr->ifr_addr.sa_len = 16;
1586 case OSIOCGIFDSTADDR:
1587 cmd = SIOCGIFDSTADDR;
1590 case OSIOCGIFBRDADDR:
1591 cmd = SIOCGIFBRDADDR;
1594 case OSIOCGIFNETMASK:
1595 cmd = SIOCGIFNETMASK;
1597 error = so_pru_control(so, cmd, data, ifp);
1601 case OSIOCGIFDSTADDR:
1602 case OSIOCGIFBRDADDR:
1603 case OSIOCGIFNETMASK:
1604 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1608 #endif /* COMPAT_43 */
1610 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1612 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1613 if (ifp->if_flags & IFF_UP) {
1627 * Set/clear promiscuous mode on interface ifp based on the truth value
1628 * of pswitch. The calls are reference counted so that only the first
1629 * "on" request actually has an effect, as does the final "off" request.
1630 * Results are undefined if the "off" and "on" requests are not matched.
1633 ifpromisc(struct ifnet *ifp, int pswitch)
1639 oldflags = ifp->if_flags;
1640 if (ifp->if_flags & IFF_PPROMISC) {
1641 /* Do nothing if device is in permanently promiscuous mode */
1642 ifp->if_pcount += pswitch ? 1 : -1;
1647 * If the device is not configured up, we cannot put it in
1650 if ((ifp->if_flags & IFF_UP) == 0)
1652 if (ifp->if_pcount++ != 0)
1654 ifp->if_flags |= IFF_PROMISC;
1655 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1658 if (--ifp->if_pcount > 0)
1660 ifp->if_flags &= ~IFF_PROMISC;
1661 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1664 ifr.ifr_flags = ifp->if_flags;
1665 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1666 ifnet_serialize_all(ifp);
1667 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
1668 ifnet_deserialize_all(ifp);
1672 ifp->if_flags = oldflags;
1677 * Return interface configuration
1678 * of system. List may be used
1679 * in later ioctl's (above) to get
1680 * other information.
1683 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
1685 struct ifconf *ifc = (struct ifconf *)data;
1687 struct sockaddr *sa;
1688 struct ifreq ifr, *ifrp;
1689 int space = ifc->ifc_len, error = 0;
1691 ifrp = ifc->ifc_req;
1692 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1693 struct ifaddr_container *ifac;
1696 if (space <= sizeof ifr)
1700 * Zero the stack declared structure first to prevent
1701 * memory disclosure.
1703 bzero(&ifr, sizeof(ifr));
1704 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1705 >= sizeof(ifr.ifr_name)) {
1706 error = ENAMETOOLONG;
1711 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1712 struct ifaddr *ifa = ifac->ifa;
1714 if (space <= sizeof ifr)
1717 if (cred->cr_prison &&
1718 prison_if(cred, sa))
1722 if (cmd == OSIOCGIFCONF) {
1723 struct osockaddr *osa =
1724 (struct osockaddr *)&ifr.ifr_addr;
1726 osa->sa_family = sa->sa_family;
1727 error = copyout(&ifr, ifrp, sizeof ifr);
1731 if (sa->sa_len <= sizeof(*sa)) {
1733 error = copyout(&ifr, ifrp, sizeof ifr);
1736 if (space < (sizeof ifr) + sa->sa_len -
1739 space -= sa->sa_len - sizeof(*sa);
1740 error = copyout(&ifr, ifrp,
1741 sizeof ifr.ifr_name);
1743 error = copyout(sa, &ifrp->ifr_addr,
1745 ifrp = (struct ifreq *)
1746 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1750 space -= sizeof ifr;
1755 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
1756 error = copyout(&ifr, ifrp, sizeof ifr);
1759 space -= sizeof ifr;
1763 ifc->ifc_len -= space;
1768 * Just like if_promisc(), but for all-multicast-reception mode.
1771 if_allmulti(struct ifnet *ifp, int onswitch)
1779 if (ifp->if_amcount++ == 0) {
1780 ifp->if_flags |= IFF_ALLMULTI;
1781 ifr.ifr_flags = ifp->if_flags;
1782 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1783 ifnet_serialize_all(ifp);
1784 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1786 ifnet_deserialize_all(ifp);
1789 if (ifp->if_amcount > 1) {
1792 ifp->if_amcount = 0;
1793 ifp->if_flags &= ~IFF_ALLMULTI;
1794 ifr.ifr_flags = ifp->if_flags;
1795 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1796 ifnet_serialize_all(ifp);
1797 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1799 ifnet_deserialize_all(ifp);
1811 * Add a multicast listenership to the interface in question.
1812 * The link layer provides a routine which converts
1816 struct ifnet *ifp, /* interface to manipulate */
1817 struct sockaddr *sa, /* address to add */
1818 struct ifmultiaddr **retifma)
1820 struct sockaddr *llsa, *dupsa;
1822 struct ifmultiaddr *ifma;
1825 * If the matching multicast address already exists
1826 * then don't add a new one, just add a reference
1828 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1829 if (sa_equal(sa, ifma->ifma_addr)) {
1830 ifma->ifma_refcount++;
1838 * Give the link layer a chance to accept/reject it, and also
1839 * find out which AF_LINK address this maps to, if it isn't one
1842 if (ifp->if_resolvemulti) {
1843 ifnet_serialize_all(ifp);
1844 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1845 ifnet_deserialize_all(ifp);
1852 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1853 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1854 bcopy(sa, dupsa, sa->sa_len);
1856 ifma->ifma_addr = dupsa;
1857 ifma->ifma_lladdr = llsa;
1858 ifma->ifma_ifp = ifp;
1859 ifma->ifma_refcount = 1;
1860 ifma->ifma_protospec = 0;
1861 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1864 * Some network interfaces can scan the address list at
1865 * interrupt time; lock them out.
1868 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1873 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1874 if (sa_equal(ifma->ifma_addr, llsa))
1878 ifma->ifma_refcount++;
1880 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1881 M_IFMADDR, M_WAITOK);
1882 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1883 M_IFMADDR, M_WAITOK);
1884 bcopy(llsa, dupsa, llsa->sa_len);
1885 ifma->ifma_addr = dupsa;
1886 ifma->ifma_ifp = ifp;
1887 ifma->ifma_refcount = 1;
1889 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1894 * We are certain we have added something, so call down to the
1895 * interface to let them know about it.
1898 ifnet_serialize_all(ifp);
1899 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
1900 ifnet_deserialize_all(ifp);
1907 * Remove a reference to a multicast address on this interface. Yell
1908 * if the request does not match an existing membership.
1911 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
1913 struct ifmultiaddr *ifma;
1915 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1916 if (sa_equal(sa, ifma->ifma_addr))
1921 if (ifma->ifma_refcount > 1) {
1922 ifma->ifma_refcount--;
1926 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1927 sa = ifma->ifma_lladdr;
1929 LIST_REMOVE(ifma, ifma_link);
1931 * Make sure the interface driver is notified
1932 * in the case of a link layer mcast group being left.
1934 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) {
1935 ifnet_serialize_all(ifp);
1936 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
1937 ifnet_deserialize_all(ifp);
1940 kfree(ifma->ifma_addr, M_IFMADDR);
1941 kfree(ifma, M_IFMADDR);
1946 * Now look for the link-layer address which corresponds to
1947 * this network address. It had been squirreled away in
1948 * ifma->ifma_lladdr for this purpose (so we don't have
1949 * to call ifp->if_resolvemulti() again), and we saved that
1950 * value in sa above. If some nasty deleted the
1951 * link-layer address out from underneath us, we can deal because
1952 * the address we stored was is not the same as the one which was
1953 * in the record for the link-layer address. (So we don't complain
1956 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1957 if (sa_equal(sa, ifma->ifma_addr))
1962 if (ifma->ifma_refcount > 1) {
1963 ifma->ifma_refcount--;
1968 ifnet_serialize_all(ifp);
1969 LIST_REMOVE(ifma, ifma_link);
1970 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
1971 ifnet_deserialize_all(ifp);
1973 kfree(ifma->ifma_addr, M_IFMADDR);
1974 kfree(sa, M_IFMADDR);
1975 kfree(ifma, M_IFMADDR);
1981 * Delete all multicast group membership for an interface.
1982 * Should be used to quickly flush all multicast filters.
1985 if_delallmulti(struct ifnet *ifp)
1987 struct ifmultiaddr *ifma;
1988 struct ifmultiaddr *next;
1990 LIST_FOREACH_MUTABLE(ifma, &ifp->if_multiaddrs, ifma_link, next)
1991 if_delmulti(ifp, ifma->ifma_addr);
1996 * Set the link layer address on an interface.
1998 * At this time we only support certain types of interfaces,
1999 * and we don't allow the length of the address to change.
2002 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2004 struct sockaddr_dl *sdl;
2007 sdl = IF_LLSOCKADDR(ifp);
2010 if (len != sdl->sdl_alen) /* don't allow length to change */
2012 switch (ifp->if_type) {
2013 case IFT_ETHER: /* these types use struct arpcom */
2016 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2017 bcopy(lladdr, LLADDR(sdl), len);
2023 * If the interface is already up, we need
2024 * to re-init it in order to reprogram its
2027 ifnet_serialize_all(ifp);
2028 if ((ifp->if_flags & IFF_UP) != 0) {
2029 struct ifaddr_container *ifac;
2031 ifp->if_flags &= ~IFF_UP;
2032 ifr.ifr_flags = ifp->if_flags;
2033 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2034 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2036 ifp->if_flags |= IFF_UP;
2037 ifr.ifr_flags = ifp->if_flags;
2038 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2039 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2043 * Also send gratuitous ARPs to notify other nodes about
2044 * the address change.
2046 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2047 struct ifaddr *ifa = ifac->ifa;
2049 if (ifa->ifa_addr != NULL &&
2050 ifa->ifa_addr->sa_family == AF_INET)
2051 arp_ifinit(ifp, ifa);
2055 ifnet_deserialize_all(ifp);
2059 struct ifmultiaddr *
2060 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2062 struct ifmultiaddr *ifma;
2064 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2065 if (sa_equal(ifma->ifma_addr, sa))
2072 * This function locates the first real ethernet MAC from a network
2073 * card and loads it into node, returning 0 on success or ENOENT if
2074 * no suitable interfaces were found. It is used by the uuid code to
2075 * generate a unique 6-byte number.
2078 if_getanyethermac(uint16_t *node, int minlen)
2081 struct sockaddr_dl *sdl;
2083 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2084 if (ifp->if_type != IFT_ETHER)
2086 sdl = IF_LLSOCKADDR(ifp);
2087 if (sdl->sdl_alen < minlen)
2089 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2097 * The name argument must be a pointer to storage which will last as
2098 * long as the interface does. For physical devices, the result of
2099 * device_get_name(dev) is a good choice and for pseudo-devices a
2100 * static string works well.
2103 if_initname(struct ifnet *ifp, const char *name, int unit)
2105 ifp->if_dname = name;
2106 ifp->if_dunit = unit;
2107 if (unit != IF_DUNIT_NONE)
2108 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2110 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2114 if_printf(struct ifnet *ifp, const char *fmt, ...)
2119 retval = kprintf("%s: ", ifp->if_xname);
2120 __va_start(ap, fmt);
2121 retval += kvprintf(fmt, ap);
2127 if_alloc(uint8_t type)
2131 ifp = kmalloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
2133 ifp->if_type = type;
2135 if (if_com_alloc[type] != NULL) {
2136 ifp->if_l2com = if_com_alloc[type](type, ifp);
2137 if (ifp->if_l2com == NULL) {
2138 kfree(ifp, M_IFNET);
2146 if_free(struct ifnet *ifp)
2148 kfree(ifp, M_IFNET);
2152 ifq_set_classic(struct ifaltq *ifq)
2154 ifq->altq_enqueue = ifq_classic_enqueue;
2155 ifq->altq_dequeue = ifq_classic_dequeue;
2156 ifq->altq_request = ifq_classic_request;
2160 ifq_classic_enqueue(struct ifaltq *ifq, struct mbuf *m,
2161 struct altq_pktattr *pa __unused)
2163 logifq(enqueue, ifq);
2164 if (IF_QFULL(ifq)) {
2174 ifq_classic_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
2183 logifq(dequeue, ifq);
2187 panic("unsupported ALTQ dequeue op: %d", op);
2189 KKASSERT(mpolled == NULL || mpolled == m);
2194 ifq_classic_request(struct ifaltq *ifq, int req, void *arg)
2201 panic("unsupported ALTQ request: %d", req);
2207 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2209 struct ifaltq *ifq = &ifp->if_snd;
2210 int running = 0, error, start = 0;
2212 ASSERT_IFNET_NOT_SERIALIZED_TX(ifp);
2215 error = ifq_enqueue_locked(ifq, m, pa);
2220 if (!ifq->altq_started) {
2222 * Hold the interlock of ifnet.if_start
2224 ifq->altq_started = 1;
2229 ifp->if_obytes += m->m_pkthdr.len;
2230 if (m->m_flags & M_MCAST)
2234 logifstart(avoid, ifp);
2238 if (ifq_dispatch_schedonly) {
2240 * Always schedule ifnet.if_start on ifnet's CPU,
2241 * short circuit the rest of this function.
2243 logifstart(sched, ifp);
2244 if_start_schedule(ifp);
2249 * Try to do direct ifnet.if_start first, if there is
2250 * contention on ifnet's serializer, ifnet.if_start will
2251 * be scheduled on ifnet's CPU.
2253 if (!ifnet_tryserialize_tx(ifp)) {
2255 * ifnet serializer contention happened,
2256 * ifnet.if_start is scheduled on ifnet's
2257 * CPU, and we keep going.
2259 logifstart(contend_sched, ifp);
2260 if_start_schedule(ifp);
2264 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
2265 logifstart(run, ifp);
2267 if ((ifp->if_flags &
2268 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
2272 ifnet_deserialize_tx(ifp);
2274 if (ifq_dispatch_schednochk || if_start_need_schedule(ifq, running)) {
2276 * More data need to be transmitted, ifnet.if_start is
2277 * scheduled on ifnet's CPU, and we keep going.
2278 * NOTE: ifnet.if_start interlock is not released.
2280 logifstart(sched, ifp);
2281 if_start_schedule(ifp);
2287 ifa_create(int size, int flags)
2292 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small\n"));
2294 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
2298 ifa->ifa_containers = kmalloc(ncpus * sizeof(struct ifaddr_container),
2299 M_IFADDR, M_WAITOK | M_ZERO);
2300 ifa->ifa_ncnt = ncpus;
2301 for (i = 0; i < ncpus; ++i) {
2302 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
2304 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
2306 ifac->ifa_refcnt = 1;
2309 kprintf("alloc ifa %p %d\n", ifa, size);
2315 ifac_free(struct ifaddr_container *ifac, int cpu_id)
2317 struct ifaddr *ifa = ifac->ifa;
2319 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
2320 KKASSERT(ifac->ifa_refcnt == 0);
2321 KASSERT(ifac->ifa_listmask == 0,
2322 ("ifa is still on %#x lists\n", ifac->ifa_listmask));
2324 ifac->ifa_magic = IFA_CONTAINER_DEAD;
2326 #ifdef IFADDR_DEBUG_VERBOSE
2327 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
2330 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
2331 ("invalid # of ifac, %d\n", ifa->ifa_ncnt));
2332 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
2334 kprintf("free ifa %p\n", ifa);
2336 kfree(ifa->ifa_containers, M_IFADDR);
2337 kfree(ifa, M_IFADDR);
2342 ifa_iflink_dispatch(struct netmsg *nmsg)
2344 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2345 struct ifaddr *ifa = msg->ifa;
2346 struct ifnet *ifp = msg->ifp;
2348 struct ifaddr_container *ifac;
2352 ifac = &ifa->ifa_containers[cpu];
2353 ASSERT_IFAC_VALID(ifac);
2354 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
2355 ("ifaddr is on if_addrheads\n"));
2357 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
2359 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
2361 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
2365 ifa_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
2369 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
2371 struct netmsg_ifaddr msg;
2373 netmsg_init(&msg.netmsg, NULL, &curthread->td_msgport,
2374 0, ifa_iflink_dispatch);
2379 ifa_domsg(&msg.netmsg.nm_lmsg, 0);
2383 ifa_ifunlink_dispatch(struct netmsg *nmsg)
2385 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2386 struct ifaddr *ifa = msg->ifa;
2387 struct ifnet *ifp = msg->ifp;
2389 struct ifaddr_container *ifac;
2393 ifac = &ifa->ifa_containers[cpu];
2394 ASSERT_IFAC_VALID(ifac);
2395 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
2396 ("ifaddr is not on if_addrhead\n"));
2398 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
2399 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
2403 ifa_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
2407 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
2409 struct netmsg_ifaddr msg;
2411 netmsg_init(&msg.netmsg, NULL, &curthread->td_msgport,
2412 0, ifa_ifunlink_dispatch);
2416 ifa_domsg(&msg.netmsg.nm_lmsg, 0);
2420 ifa_destroy_dispatch(struct netmsg *nmsg)
2422 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2425 ifa_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2429 ifa_destroy(struct ifaddr *ifa)
2431 struct netmsg_ifaddr msg;
2433 netmsg_init(&msg.netmsg, NULL, &curthread->td_msgport,
2434 0, ifa_destroy_dispatch);
2437 ifa_domsg(&msg.netmsg.nm_lmsg, 0);
2441 ifnet_portfn(int cpu)
2443 return &ifnet_threads[cpu].td_msgport;
2447 ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
2449 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
2451 if (next_cpu < ncpus)
2452 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
2454 lwkt_replymsg(lmsg, 0);
2458 ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
2460 KKASSERT(cpu < ncpus);
2461 return lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
2465 ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
2467 KKASSERT(cpu < ncpus);
2468 lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
2472 ifnetinit(void *dummy __unused)
2476 for (i = 0; i < ncpus; ++i) {
2477 struct thread *thr = &ifnet_threads[i];
2479 lwkt_create(netmsg_service_loop, &ifnet_mpsafe_thread, NULL,
2480 thr, TDF_NETWORK | TDF_MPSAFE, i, "ifnet %d", i);
2481 netmsg_service_port_init(&thr->td_msgport);
2486 ifnet_byindex(unsigned short idx)
2490 return ifindex2ifnet[idx];
2494 ifaddr_byindex(unsigned short idx)
2498 ifp = ifnet_byindex(idx);
2501 return TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
2505 if_register_com_alloc(u_char type,
2506 if_com_alloc_t *a, if_com_free_t *f)
2509 KASSERT(if_com_alloc[type] == NULL,
2510 ("if_register_com_alloc: %d already registered", type));
2511 KASSERT(if_com_free[type] == NULL,
2512 ("if_register_com_alloc: %d free already registered", type));
2514 if_com_alloc[type] = a;
2515 if_com_free[type] = f;
2519 if_deregister_com_alloc(u_char type)
2522 KASSERT(if_com_alloc[type] != NULL,
2523 ("if_deregister_com_alloc: %d not registered", type));
2524 KASSERT(if_com_free[type] != NULL,
2525 ("if_deregister_com_alloc: %d free not registered", type));
2526 if_com_alloc[type] = NULL;
2527 if_com_free[type] = NULL;