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.83 2008/11/12 11:03:28 sephe Exp $
38 #include "opt_compat.h"
39 #include "opt_inet6.h"
41 #include "opt_polling.h"
43 #include <sys/param.h>
44 #include <sys/malloc.h>
46 #include <sys/systm.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/socketops.h>
52 #include <sys/protosw.h>
53 #include <sys/kernel.h>
55 #include <sys/sockio.h>
56 #include <sys/syslog.h>
57 #include <sys/sysctl.h>
58 #include <sys/domain.h>
59 #include <sys/thread.h>
60 #include <sys/thread2.h>
61 #include <sys/serialize.h>
62 #include <sys/msgport2.h>
66 #include <net/if_arp.h>
67 #include <net/if_dl.h>
68 #include <net/if_types.h>
69 #include <net/if_var.h>
70 #include <net/ifq_var.h>
71 #include <net/radix.h>
72 #include <net/route.h>
73 #include <net/if_clone.h>
74 #include <net/netisr.h>
75 #include <net/netmsg2.h>
77 #include <machine/atomic.h>
78 #include <machine/stdarg.h>
79 #include <machine/smp.h>
81 #if defined(INET) || defined(INET6)
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
85 #include <netinet/if_ether.h>
87 #include <netinet6/in6_var.h>
88 #include <netinet6/in6_ifattach.h>
92 #if defined(COMPAT_43)
93 #include <emulation/43bsd/43bsd_socket.h>
94 #endif /* COMPAT_43 */
96 struct netmsg_ifaddr {
104 * System initialization
106 static void if_attachdomain(void *);
107 static void if_attachdomain1(struct ifnet *);
108 static int ifconf(u_long, caddr_t, struct ucred *);
109 static void ifinit(void *);
110 static void ifnetinit(void *);
111 static void if_slowtimo(void *);
112 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
113 static int if_rtdel(struct radix_node *, void *);
117 * XXX: declare here to avoid to include many inet6 related files..
118 * should be more generalized?
120 extern void nd6_setmtu(struct ifnet *);
123 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
124 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
126 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
127 /* Must be after netisr_init */
128 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_SECOND, ifnetinit, NULL)
130 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
131 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
133 int ifqmaxlen = IFQ_MAXLEN;
134 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
136 /* In ifq_dispatch(), try to do direct ifnet.if_start first */
137 static int ifq_dispatch_schedonly = 0;
138 SYSCTL_INT(_net_link_generic, OID_AUTO, ifq_dispatch_schedonly, CTLFLAG_RW,
139 &ifq_dispatch_schedonly, 0, "");
141 /* In ifq_dispatch(), schedule ifnet.if_start without checking ifnet.if_snd */
142 static int ifq_dispatch_schednochk = 0;
143 SYSCTL_INT(_net_link_generic, OID_AUTO, ifq_dispatch_schednochk, CTLFLAG_RW,
144 &ifq_dispatch_schednochk, 0, "");
146 /* In if_devstart(), try to do direct ifnet.if_start first */
147 static int if_devstart_schedonly = 0;
148 SYSCTL_INT(_net_link_generic, OID_AUTO, if_devstart_schedonly, CTLFLAG_RW,
149 &if_devstart_schedonly, 0, "");
151 /* In if_devstart(), schedule ifnet.if_start without checking ifnet.if_snd */
152 static int if_devstart_schednochk = 0;
153 SYSCTL_INT(_net_link_generic, OID_AUTO, if_devstart_schednochk, CTLFLAG_RW,
154 &if_devstart_schednochk, 0, "");
157 /* Schedule ifnet.if_start on the current CPU */
158 static int if_start_oncpu_sched = 0;
159 SYSCTL_INT(_net_link_generic, OID_AUTO, if_start_oncpu_sched, CTLFLAG_RW,
160 &if_start_oncpu_sched, 0, "");
163 struct callout if_slowtimo_timer;
166 struct ifnet **ifindex2ifnet = NULL;
167 static struct thread ifnet_threads[MAXCPU];
168 static int ifnet_mpsafe_thread = NETMSG_SERVICE_MPSAFE;
170 #define IFQ_KTR_STRING "ifq=%p"
171 #define IFQ_KTR_ARG_SIZE (sizeof(void *))
173 #define KTR_IFQ KTR_ALL
175 KTR_INFO_MASTER(ifq);
176 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARG_SIZE);
177 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARG_SIZE);
178 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
180 #define IF_START_KTR_STRING "ifp=%p"
181 #define IF_START_KTR_ARG_SIZE (sizeof(void *))
183 #define KTR_IF_START KTR_ALL
185 KTR_INFO_MASTER(if_start);
186 KTR_INFO(KTR_IF_START, if_start, run, 0,
187 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
188 KTR_INFO(KTR_IF_START, if_start, sched, 1,
189 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
190 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
191 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
192 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
193 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
194 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
195 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
196 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
199 * Network interface utility routines.
201 * Routines with ifa_ifwith* names take sockaddr *'s as
210 callout_init(&if_slowtimo_timer);
213 TAILQ_FOREACH(ifp, &ifnet, if_link) {
214 if (ifp->if_snd.ifq_maxlen == 0) {
215 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
216 ifp->if_snd.ifq_maxlen = ifqmaxlen;
225 if_start_cpuid(struct ifnet *ifp)
227 return ifp->if_cpuid;
230 #ifdef DEVICE_POLLING
232 if_start_cpuid_poll(struct ifnet *ifp)
234 int poll_cpuid = ifp->if_poll_cpuid;
239 return ifp->if_cpuid;
244 if_start_ipifunc(void *arg)
246 struct ifnet *ifp = arg;
247 struct lwkt_msg *lmsg = &ifp->if_start_nmsg[mycpuid].nm_lmsg;
250 if (lmsg->ms_flags & MSGF_DONE)
251 lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg);
256 * Schedule ifnet.if_start on ifnet's CPU
259 if_start_schedule(struct ifnet *ifp)
264 if (if_start_oncpu_sched)
267 cpu = ifp->if_start_cpuid(ifp);
270 lwkt_send_ipiq(globaldata_find(cpu), if_start_ipifunc, ifp);
273 if_start_ipifunc(ifp);
278 * This function will release ifnet.if_start interlock,
279 * if ifnet.if_start does not need to be scheduled
282 if_start_need_schedule(struct ifaltq *ifq, int running)
284 if (!running || ifq_is_empty(ifq)
286 || ifq->altq_tbr != NULL
291 * ifnet.if_start interlock is released, if:
292 * 1) Hardware can not take any packets, due to
293 * o interface is marked down
294 * o hardware queue is full (IFF_OACTIVE)
295 * Under the second situation, hardware interrupt
296 * or polling(4) will call/schedule ifnet.if_start
297 * when hardware queue is ready
298 * 2) There is not packet in the ifnet.if_snd.
299 * Further ifq_dispatch or ifq_handoff will call/
300 * schedule ifnet.if_start
301 * 3) TBR is used and it does not allow further
303 * TBR callout will call ifnet.if_start
305 if (!running || !ifq_data_ready(ifq)) {
306 ifq->altq_started = 0;
316 if_start_dispatch(struct netmsg *nmsg)
318 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
319 struct ifnet *ifp = lmsg->u.ms_resultp;
320 struct ifaltq *ifq = &ifp->if_snd;
324 lwkt_replymsg(lmsg, 0); /* reply ASAP */
328 if (!if_start_oncpu_sched && mycpuid != ifp->if_start_cpuid(ifp)) {
330 * If the ifnet is still up, we need to
331 * chase its CPU change.
333 if (ifp->if_flags & IFF_UP) {
334 logifstart(chase_sched, ifp);
335 if_start_schedule(ifp);
343 if (ifp->if_flags & IFF_UP) {
344 lwkt_serialize_enter(ifp->if_serializer); /* XXX try? */
345 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
346 logifstart(run, ifp);
349 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
352 lwkt_serialize_exit(ifp->if_serializer);
357 if (if_start_need_schedule(ifq, running)) {
359 if (lmsg->ms_flags & MSGF_DONE) { /* XXX necessary? */
360 logifstart(sched, ifp);
361 lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg);
367 /* Device driver ifnet.if_start helper function */
369 if_devstart(struct ifnet *ifp)
371 struct ifaltq *ifq = &ifp->if_snd;
374 ASSERT_SERIALIZED(ifp->if_serializer);
377 if (ifq->altq_started || !ifq_data_ready(ifq)) {
378 logifstart(avoid, ifp);
382 ifq->altq_started = 1;
385 if (if_devstart_schedonly) {
387 * Always schedule ifnet.if_start on ifnet's CPU,
388 * short circuit the rest of this function.
390 logifstart(sched, ifp);
391 if_start_schedule(ifp);
395 logifstart(run, ifp);
398 if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
401 if (if_devstart_schednochk || if_start_need_schedule(ifq, running)) {
403 * More data need to be transmitted, ifnet.if_start is
404 * scheduled on ifnet's CPU, and we keep going.
405 * NOTE: ifnet.if_start interlock is not released.
407 logifstart(sched, ifp);
408 if_start_schedule(ifp);
413 * Attach an interface to the list of "active" interfaces.
415 * The serializer is optional. If non-NULL access to the interface
419 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
421 unsigned socksize, ifasize;
422 int namelen, masklen;
423 struct sockaddr_dl *sdl;
428 static int if_indexlim = 8;
431 * The serializer can be passed in from the device, allowing the
432 * same serializer to be used for both the interrupt interlock and
433 * the device queue. If not specified, the netif structure will
434 * use an embedded serializer.
436 if (serializer == NULL) {
437 serializer = &ifp->if_default_serializer;
438 lwkt_serialize_init(serializer);
440 ifp->if_serializer = serializer;
442 ifp->if_start_cpuid = if_start_cpuid;
445 #ifdef DEVICE_POLLING
446 /* Device is not in polling mode by default */
447 ifp->if_poll_cpuid = -1;
448 if (ifp->if_poll != NULL)
449 ifp->if_start_cpuid = if_start_cpuid_poll;
452 ifp->if_start_nmsg = kmalloc(ncpus * sizeof(struct netmsg),
453 M_LWKTMSG, M_WAITOK);
454 for (i = 0; i < ncpus; ++i) {
455 netmsg_init(&ifp->if_start_nmsg[i], &netisr_adone_rport, 0,
457 ifp->if_start_nmsg[i].nm_lmsg.u.ms_resultp = ifp;
460 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
461 ifp->if_index = ++if_index;
465 * The old code would work if the interface passed a pre-existing
466 * chain of ifaddrs to this code. We don't trust our callers to
467 * properly initialize the tailq, however, so we no longer allow
468 * this unlikely case.
470 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
471 M_IFADDR, M_WAITOK | M_ZERO);
472 for (i = 0; i < ncpus; ++i)
473 TAILQ_INIT(&ifp->if_addrheads[i]);
475 TAILQ_INIT(&ifp->if_prefixhead);
476 LIST_INIT(&ifp->if_multiaddrs);
477 getmicrotime(&ifp->if_lastchange);
478 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
484 /* grow ifindex2ifnet */
485 n = if_indexlim * sizeof(*q);
486 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
488 bcopy(ifindex2ifnet, q, n/2);
489 kfree(ifindex2ifnet, M_IFADDR);
494 ifindex2ifnet[if_index] = ifp;
497 * create a Link Level name for this device
499 namelen = strlen(ifp->if_xname);
500 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
501 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
502 socksize = masklen + ifp->if_addrlen;
503 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
504 if (socksize < sizeof(*sdl))
505 socksize = sizeof(*sdl);
506 socksize = ROUNDUP(socksize);
507 ifasize = sizeof(struct ifaddr) + 2 * socksize;
508 ifa = ifa_create(ifasize, M_WAITOK);
509 sdl = (struct sockaddr_dl *)(ifa + 1);
510 sdl->sdl_len = socksize;
511 sdl->sdl_family = AF_LINK;
512 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
513 sdl->sdl_nlen = namelen;
514 sdl->sdl_index = ifp->if_index;
515 sdl->sdl_type = ifp->if_type;
516 ifp->if_lladdr = ifa;
518 ifa->ifa_rtrequest = link_rtrequest;
519 ifa->ifa_addr = (struct sockaddr *)sdl;
520 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
521 ifa->ifa_netmask = (struct sockaddr *)sdl;
522 sdl->sdl_len = masklen;
524 sdl->sdl_data[--namelen] = 0xff;
525 ifa_iflink(ifa, ifp, 0 /* Insert head */);
527 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
528 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
532 ifq->altq_disc = NULL;
533 ifq->altq_flags &= ALTQF_CANTCHANGE;
534 ifq->altq_tbr = NULL;
536 ifq->altq_started = 0;
537 ifq->altq_prepended = NULL;
539 ifq_set_classic(ifq);
541 if (!SLIST_EMPTY(&domains))
542 if_attachdomain1(ifp);
544 /* Announce the interface. */
545 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
549 if_attachdomain(void *dummy)
554 TAILQ_FOREACH(ifp, &ifnet, if_list)
555 if_attachdomain1(ifp);
558 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
559 if_attachdomain, NULL);
562 if_attachdomain1(struct ifnet *ifp)
568 /* address family dependent data region */
569 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
570 SLIST_FOREACH(dp, &domains, dom_next)
571 if (dp->dom_ifattach)
572 ifp->if_afdata[dp->dom_family] =
573 (*dp->dom_ifattach)(ifp);
578 * Purge all addresses whose type is _not_ AF_LINK
581 if_purgeaddrs_nolink(struct ifnet *ifp)
583 struct ifaddr_container *ifac, *next;
585 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
587 struct ifaddr *ifa = ifac->ifa;
589 /* Leave link ifaddr as it is */
590 if (ifa->ifa_addr->sa_family == AF_LINK)
593 /* XXX: Ugly!! ad hoc just for INET */
594 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
595 struct ifaliasreq ifr;
596 #ifdef IFADDR_DEBUG_VERBOSE
599 kprintf("purge in4 addr %p: ", ifa);
600 for (i = 0; i < ncpus; ++i)
601 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
605 bzero(&ifr, sizeof ifr);
606 ifr.ifra_addr = *ifa->ifa_addr;
607 if (ifa->ifa_dstaddr)
608 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
609 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
615 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
616 #ifdef IFADDR_DEBUG_VERBOSE
619 kprintf("purge in6 addr %p: ", ifa);
620 for (i = 0; i < ncpus; ++i)
621 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
626 /* ifp_addrhead is already updated */
630 ifa_ifunlink(ifa, ifp);
636 * Detach an interface, removing it from the
637 * list of "active" interfaces.
640 if_detach(struct ifnet *ifp)
642 struct radix_node_head *rnh;
647 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
650 * Remove routes and flush queues.
653 #ifdef DEVICE_POLLING
654 if (ifp->if_flags & IFF_POLLING)
655 ether_poll_deregister(ifp);
659 if (ifq_is_enabled(&ifp->if_snd))
660 altq_disable(&ifp->if_snd);
661 if (ifq_is_attached(&ifp->if_snd))
662 altq_detach(&ifp->if_snd);
665 * Clean up all addresses.
667 ifp->if_lladdr = NULL;
669 if_purgeaddrs_nolink(ifp);
670 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
673 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
674 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
675 ("non-link ifaddr is left on if_addrheads"));
677 ifa_ifunlink(ifa, ifp);
679 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
680 ("there are still ifaddrs left on if_addrheads"));
685 * Remove all IPv4 kernel structures related to ifp.
692 * Remove all IPv6 kernel structs related to ifp. This should be done
693 * before removing routing entries below, since IPv6 interface direct
694 * routes are expected to be removed by the IPv6-specific kernel API.
695 * Otherwise, the kernel will detect some inconsistency and bark it.
701 * Delete all remaining routes using this interface
702 * Unfortuneatly the only way to do this is to slog through
703 * the entire routing table looking for routes which point
704 * to this interface...oh well...
707 for (cpu = 0; cpu < ncpus2; cpu++) {
708 lwkt_migratecpu(cpu);
709 for (i = 1; i <= AF_MAX; i++) {
710 if ((rnh = rt_tables[cpu][i]) == NULL)
712 rnh->rnh_walktree(rnh, if_rtdel, ifp);
715 lwkt_migratecpu(origcpu);
717 /* Announce that the interface is gone. */
718 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
719 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
721 SLIST_FOREACH(dp, &domains, dom_next)
722 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
723 (*dp->dom_ifdetach)(ifp,
724 ifp->if_afdata[dp->dom_family]);
727 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
729 ifindex2ifnet[ifp->if_index] = NULL;
730 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
733 TAILQ_REMOVE(&ifnet, ifp, if_link);
734 kfree(ifp->if_addrheads, M_IFADDR);
735 kfree(ifp->if_start_nmsg, M_LWKTMSG);
740 * Delete Routes for a Network Interface
742 * Called for each routing entry via the rnh->rnh_walktree() call above
743 * to delete all route entries referencing a detaching network interface.
746 * rn pointer to node in the routing table
747 * arg argument passed to rnh->rnh_walktree() - detaching interface
751 * errno failed - reason indicated
755 if_rtdel(struct radix_node *rn, void *arg)
757 struct rtentry *rt = (struct rtentry *)rn;
758 struct ifnet *ifp = arg;
761 if (rt->rt_ifp == ifp) {
764 * Protect (sorta) against walktree recursion problems
767 if (!(rt->rt_flags & RTF_UP))
770 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
771 rt_mask(rt), rt->rt_flags,
772 (struct rtentry **) NULL);
774 log(LOG_WARNING, "if_rtdel: error %d\n", err);
782 * Locate an interface based on a complete address.
785 ifa_ifwithaddr(struct sockaddr *addr)
789 TAILQ_FOREACH(ifp, &ifnet, if_link) {
790 struct ifaddr_container *ifac;
792 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
793 struct ifaddr *ifa = ifac->ifa;
795 if (ifa->ifa_addr->sa_family != addr->sa_family)
797 if (sa_equal(addr, ifa->ifa_addr))
799 if ((ifp->if_flags & IFF_BROADCAST) &&
800 ifa->ifa_broadaddr &&
801 /* IPv6 doesn't have broadcast */
802 ifa->ifa_broadaddr->sa_len != 0 &&
803 sa_equal(ifa->ifa_broadaddr, addr))
810 * Locate the point to point interface with a given destination address.
813 ifa_ifwithdstaddr(struct sockaddr *addr)
817 TAILQ_FOREACH(ifp, &ifnet, if_link) {
818 struct ifaddr_container *ifac;
820 if (!(ifp->if_flags & IFF_POINTOPOINT))
823 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
824 struct ifaddr *ifa = ifac->ifa;
826 if (ifa->ifa_addr->sa_family != addr->sa_family)
828 if (ifa->ifa_dstaddr &&
829 sa_equal(addr, ifa->ifa_dstaddr))
837 * Find an interface on a specific network. If many, choice
838 * is most specific found.
841 ifa_ifwithnet(struct sockaddr *addr)
844 struct ifaddr *ifa_maybe = NULL;
845 u_int af = addr->sa_family;
846 char *addr_data = addr->sa_data, *cplim;
849 * AF_LINK addresses can be looked up directly by their index number,
850 * so do that if we can.
853 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
855 if (sdl->sdl_index && sdl->sdl_index <= if_index)
856 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
860 * Scan though each interface, looking for ones that have
861 * addresses in this address family.
863 TAILQ_FOREACH(ifp, &ifnet, if_link) {
864 struct ifaddr_container *ifac;
866 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
867 struct ifaddr *ifa = ifac->ifa;
868 char *cp, *cp2, *cp3;
870 if (ifa->ifa_addr->sa_family != af)
872 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
874 * This is a bit broken as it doesn't
875 * take into account that the remote end may
876 * be a single node in the network we are
878 * The trouble is that we don't know the
879 * netmask for the remote end.
881 if (ifa->ifa_dstaddr != NULL &&
882 sa_equal(addr, ifa->ifa_dstaddr))
886 * if we have a special address handler,
887 * then use it instead of the generic one.
889 if (ifa->ifa_claim_addr) {
890 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
898 * Scan all the bits in the ifa's address.
899 * If a bit dissagrees with what we are
900 * looking for, mask it with the netmask
901 * to see if it really matters.
904 if (ifa->ifa_netmask == 0)
907 cp2 = ifa->ifa_addr->sa_data;
908 cp3 = ifa->ifa_netmask->sa_data;
909 cplim = ifa->ifa_netmask->sa_len +
910 (char *)ifa->ifa_netmask;
912 if ((*cp++ ^ *cp2++) & *cp3++)
913 goto next; /* next address! */
915 * If the netmask of what we just found
916 * is more specific than what we had before
917 * (if we had one) then remember the new one
918 * before continuing to search
919 * for an even better one.
921 if (ifa_maybe == 0 ||
922 rn_refines((char *)ifa->ifa_netmask,
923 (char *)ifa_maybe->ifa_netmask))
932 * Find an interface address specific to an interface best matching
936 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
938 struct ifaddr_container *ifac;
939 char *cp, *cp2, *cp3;
941 struct ifaddr *ifa_maybe = 0;
942 u_int af = addr->sa_family;
946 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
947 struct ifaddr *ifa = ifac->ifa;
949 if (ifa->ifa_addr->sa_family != af)
953 if (ifa->ifa_netmask == NULL) {
954 if (sa_equal(addr, ifa->ifa_addr) ||
955 (ifa->ifa_dstaddr != NULL &&
956 sa_equal(addr, ifa->ifa_dstaddr)))
960 if (ifp->if_flags & IFF_POINTOPOINT) {
961 if (sa_equal(addr, ifa->ifa_dstaddr))
965 cp2 = ifa->ifa_addr->sa_data;
966 cp3 = ifa->ifa_netmask->sa_data;
967 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
968 for (; cp3 < cplim; cp3++)
969 if ((*cp++ ^ *cp2++) & *cp3)
979 * Default action when installing a route with a Link Level gateway.
980 * Lookup an appropriate real ifa to point to.
981 * This should be moved to /sys/net/link.c eventually.
984 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
987 struct sockaddr *dst;
990 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
991 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
993 ifa = ifaof_ifpforaddr(dst, ifp);
998 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
999 ifa->ifa_rtrequest(cmd, rt, info);
1004 * Mark an interface down and notify protocols of
1006 * NOTE: must be called at splnet or eqivalent.
1009 if_unroute(struct ifnet *ifp, int flag, int fam)
1011 struct ifaddr_container *ifac;
1013 ifp->if_flags &= ~flag;
1014 getmicrotime(&ifp->if_lastchange);
1015 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1016 struct ifaddr *ifa = ifac->ifa;
1018 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1019 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1021 ifq_purge(&ifp->if_snd);
1026 * Mark an interface up and notify protocols of
1028 * NOTE: must be called at splnet or eqivalent.
1031 if_route(struct ifnet *ifp, int flag, int fam)
1033 struct ifaddr_container *ifac;
1035 ifq_purge(&ifp->if_snd);
1036 ifp->if_flags |= flag;
1037 getmicrotime(&ifp->if_lastchange);
1038 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1039 struct ifaddr *ifa = ifac->ifa;
1041 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1042 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1051 * Mark an interface down and notify protocols of the transition. An
1052 * interface going down is also considered to be a synchronizing event.
1053 * We must ensure that all packet processing related to the interface
1054 * has completed before we return so e.g. the caller can free the ifnet
1055 * structure that the mbufs may be referencing.
1057 * NOTE: must be called at splnet or eqivalent.
1060 if_down(struct ifnet *ifp)
1062 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1063 netmsg_service_sync();
1067 * Mark an interface up and notify protocols of
1069 * NOTE: must be called at splnet or eqivalent.
1072 if_up(struct ifnet *ifp)
1074 if_route(ifp, IFF_UP, AF_UNSPEC);
1078 * Process a link state change.
1079 * NOTE: must be called at splsoftnet or equivalent.
1082 if_link_state_change(struct ifnet *ifp)
1084 int link_state = ifp->if_link_state;
1087 devctl_notify("IFNET", ifp->if_xname,
1088 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1092 * Handle interface watchdog timer routines. Called
1093 * from softclock, we decrement timers (if set) and
1094 * call the appropriate interface routine on expiration.
1097 if_slowtimo(void *arg)
1103 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1104 if (ifp->if_timer == 0 || --ifp->if_timer)
1106 if (ifp->if_watchdog) {
1107 if (lwkt_serialize_try(ifp->if_serializer)) {
1108 (*ifp->if_watchdog)(ifp);
1109 lwkt_serialize_exit(ifp->if_serializer);
1111 /* try again next timeout */
1119 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1123 * Map interface name to
1124 * interface structure pointer.
1127 ifunit(const char *name)
1132 * Search all the interfaces for this name/number
1135 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1136 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1144 * Map interface name in a sockaddr_dl to
1145 * interface structure pointer.
1148 if_withname(struct sockaddr *sa)
1150 char ifname[IFNAMSIZ+1];
1151 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1153 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1154 (sdl->sdl_nlen > IFNAMSIZ) )
1158 * ifunit wants a null-terminated name. It may not be null-terminated
1159 * in the sockaddr. We don't want to change the caller's sockaddr,
1160 * and there might not be room to put the trailing null anyway, so we
1161 * make a local copy that we know we can null terminate safely.
1164 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1165 ifname[sdl->sdl_nlen] = '\0';
1166 return ifunit(ifname);
1174 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1182 size_t namelen, onamelen;
1183 char new_name[IFNAMSIZ];
1185 struct sockaddr_dl *sdl;
1191 return (ifconf(cmd, data, cred));
1193 ifr = (struct ifreq *)data;
1198 if ((error = suser_cred(cred, 0)) != 0)
1200 return ((cmd == SIOCIFCREATE) ?
1201 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1202 if_clone_destroy(ifr->ifr_name));
1204 case SIOCIFGCLONERS:
1205 return (if_clone_list((struct if_clonereq *)data));
1208 ifp = ifunit(ifr->ifr_name);
1214 ifr->ifr_flags = ifp->if_flags;
1215 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1219 ifr->ifr_reqcap = ifp->if_capabilities;
1220 ifr->ifr_curcap = ifp->if_capenable;
1224 ifr->ifr_metric = ifp->if_metric;
1228 ifr->ifr_mtu = ifp->if_mtu;
1232 ifr->ifr_phys = ifp->if_physical;
1235 case SIOCGIFPOLLCPU:
1236 #ifdef DEVICE_POLLING
1237 ifr->ifr_pollcpu = ifp->if_poll_cpuid;
1239 ifr->ifr_pollcpu = -1;
1243 case SIOCSIFPOLLCPU:
1244 #ifdef DEVICE_POLLING
1245 if ((ifp->if_flags & IFF_POLLING) == 0)
1246 ether_pollcpu_register(ifp, ifr->ifr_pollcpu);
1251 error = suser_cred(cred, 0);
1254 new_flags = (ifr->ifr_flags & 0xffff) |
1255 (ifr->ifr_flagshigh << 16);
1256 if (ifp->if_flags & IFF_SMART) {
1257 /* Smart drivers twiddle their own routes */
1258 } else if (ifp->if_flags & IFF_UP &&
1259 (new_flags & IFF_UP) == 0) {
1263 } else if (new_flags & IFF_UP &&
1264 (ifp->if_flags & IFF_UP) == 0) {
1270 #ifdef DEVICE_POLLING
1271 if ((new_flags ^ ifp->if_flags) & IFF_POLLING) {
1272 if (new_flags & IFF_POLLING) {
1273 ether_poll_register(ifp);
1275 ether_poll_deregister(ifp);
1280 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1281 (new_flags &~ IFF_CANTCHANGE);
1282 if (new_flags & IFF_PPROMISC) {
1283 /* Permanently promiscuous mode requested */
1284 ifp->if_flags |= IFF_PROMISC;
1285 } else if (ifp->if_pcount == 0) {
1286 ifp->if_flags &= ~IFF_PROMISC;
1288 if (ifp->if_ioctl) {
1289 lwkt_serialize_enter(ifp->if_serializer);
1290 ifp->if_ioctl(ifp, cmd, data, cred);
1291 lwkt_serialize_exit(ifp->if_serializer);
1293 getmicrotime(&ifp->if_lastchange);
1297 error = suser_cred(cred, 0);
1300 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1302 lwkt_serialize_enter(ifp->if_serializer);
1303 ifp->if_ioctl(ifp, cmd, data, cred);
1304 lwkt_serialize_exit(ifp->if_serializer);
1308 error = suser_cred(cred, 0);
1311 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1314 if (new_name[0] == '\0')
1316 if (ifunit(new_name) != NULL)
1319 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1321 /* Announce the departure of the interface. */
1322 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1324 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1325 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1326 /* XXX IFA_LOCK(ifa); */
1327 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1328 namelen = strlen(new_name);
1329 onamelen = sdl->sdl_nlen;
1331 * Move the address if needed. This is safe because we
1332 * allocate space for a name of length IFNAMSIZ when we
1333 * create this in if_attach().
1335 if (namelen != onamelen) {
1336 bcopy(sdl->sdl_data + onamelen,
1337 sdl->sdl_data + namelen, sdl->sdl_alen);
1339 bcopy(new_name, sdl->sdl_data, namelen);
1340 sdl->sdl_nlen = namelen;
1341 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1342 bzero(sdl->sdl_data, onamelen);
1343 while (namelen != 0)
1344 sdl->sdl_data[--namelen] = 0xff;
1345 /* XXX IFA_UNLOCK(ifa) */
1347 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1349 /* Announce the return of the interface. */
1350 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1354 error = suser_cred(cred, 0);
1357 ifp->if_metric = ifr->ifr_metric;
1358 getmicrotime(&ifp->if_lastchange);
1362 error = suser_cred(cred, 0);
1367 lwkt_serialize_enter(ifp->if_serializer);
1368 error = ifp->if_ioctl(ifp, cmd, data, cred);
1369 lwkt_serialize_exit(ifp->if_serializer);
1371 getmicrotime(&ifp->if_lastchange);
1376 u_long oldmtu = ifp->if_mtu;
1378 error = suser_cred(cred, 0);
1381 if (ifp->if_ioctl == NULL)
1382 return (EOPNOTSUPP);
1383 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1385 lwkt_serialize_enter(ifp->if_serializer);
1386 error = ifp->if_ioctl(ifp, cmd, data, cred);
1387 lwkt_serialize_exit(ifp->if_serializer);
1389 getmicrotime(&ifp->if_lastchange);
1393 * If the link MTU changed, do network layer specific procedure.
1395 if (ifp->if_mtu != oldmtu) {
1405 error = suser_cred(cred, 0);
1409 /* Don't allow group membership on non-multicast interfaces. */
1410 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1413 /* Don't let users screw up protocols' entries. */
1414 if (ifr->ifr_addr.sa_family != AF_LINK)
1417 if (cmd == SIOCADDMULTI) {
1418 struct ifmultiaddr *ifma;
1419 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1421 error = if_delmulti(ifp, &ifr->ifr_addr);
1424 getmicrotime(&ifp->if_lastchange);
1427 case SIOCSIFPHYADDR:
1428 case SIOCDIFPHYADDR:
1430 case SIOCSIFPHYADDR_IN6:
1432 case SIOCSLIFPHYADDR:
1434 case SIOCSIFGENERIC:
1435 error = suser_cred(cred, 0);
1438 if (ifp->if_ioctl == 0)
1439 return (EOPNOTSUPP);
1440 lwkt_serialize_enter(ifp->if_serializer);
1441 error = ifp->if_ioctl(ifp, cmd, data, cred);
1442 lwkt_serialize_exit(ifp->if_serializer);
1444 getmicrotime(&ifp->if_lastchange);
1448 ifs = (struct ifstat *)data;
1449 ifs->ascii[0] = '\0';
1451 case SIOCGIFPSRCADDR:
1452 case SIOCGIFPDSTADDR:
1453 case SIOCGLIFPHYADDR:
1455 case SIOCGIFGENERIC:
1456 if (ifp->if_ioctl == NULL)
1457 return (EOPNOTSUPP);
1458 lwkt_serialize_enter(ifp->if_serializer);
1459 error = ifp->if_ioctl(ifp, cmd, data, cred);
1460 lwkt_serialize_exit(ifp->if_serializer);
1464 error = suser_cred(cred, 0);
1467 return if_setlladdr(ifp,
1468 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1471 oif_flags = ifp->if_flags;
1472 if (so->so_proto == 0)
1473 return (EOPNOTSUPP);
1475 error = so_pru_control(so, cmd, data, ifp);
1482 case SIOCSIFDSTADDR:
1484 case SIOCSIFBRDADDR:
1485 case SIOCSIFNETMASK:
1486 #if BYTE_ORDER != BIG_ENDIAN
1487 if (ifr->ifr_addr.sa_family == 0 &&
1488 ifr->ifr_addr.sa_len < 16) {
1489 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1490 ifr->ifr_addr.sa_len = 16;
1493 if (ifr->ifr_addr.sa_len == 0)
1494 ifr->ifr_addr.sa_len = 16;
1502 case OSIOCGIFDSTADDR:
1503 cmd = SIOCGIFDSTADDR;
1506 case OSIOCGIFBRDADDR:
1507 cmd = SIOCGIFBRDADDR;
1510 case OSIOCGIFNETMASK:
1511 cmd = SIOCGIFNETMASK;
1513 error = so_pru_control(so, cmd, data, ifp);
1517 case OSIOCGIFDSTADDR:
1518 case OSIOCGIFBRDADDR:
1519 case OSIOCGIFNETMASK:
1520 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1524 #endif /* COMPAT_43 */
1526 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1528 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1529 if (ifp->if_flags & IFF_UP) {
1543 * Set/clear promiscuous mode on interface ifp based on the truth value
1544 * of pswitch. The calls are reference counted so that only the first
1545 * "on" request actually has an effect, as does the final "off" request.
1546 * Results are undefined if the "off" and "on" requests are not matched.
1549 ifpromisc(struct ifnet *ifp, int pswitch)
1555 oldflags = ifp->if_flags;
1556 if (ifp->if_flags & IFF_PPROMISC) {
1557 /* Do nothing if device is in permanently promiscuous mode */
1558 ifp->if_pcount += pswitch ? 1 : -1;
1563 * If the device is not configured up, we cannot put it in
1566 if ((ifp->if_flags & IFF_UP) == 0)
1568 if (ifp->if_pcount++ != 0)
1570 ifp->if_flags |= IFF_PROMISC;
1571 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1574 if (--ifp->if_pcount > 0)
1576 ifp->if_flags &= ~IFF_PROMISC;
1577 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1580 ifr.ifr_flags = ifp->if_flags;
1581 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1582 lwkt_serialize_enter(ifp->if_serializer);
1583 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1584 (struct ucred *)NULL);
1585 lwkt_serialize_exit(ifp->if_serializer);
1589 ifp->if_flags = oldflags;
1594 * Return interface configuration
1595 * of system. List may be used
1596 * in later ioctl's (above) to get
1597 * other information.
1600 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
1602 struct ifconf *ifc = (struct ifconf *)data;
1604 struct sockaddr *sa;
1605 struct ifreq ifr, *ifrp;
1606 int space = ifc->ifc_len, error = 0;
1608 ifrp = ifc->ifc_req;
1609 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1610 struct ifaddr_container *ifac;
1613 if (space <= sizeof ifr)
1617 * Zero the stack declared structure first to prevent
1618 * memory disclosure.
1620 bzero(&ifr, sizeof(ifr));
1621 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1622 >= sizeof(ifr.ifr_name)) {
1623 error = ENAMETOOLONG;
1628 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1629 struct ifaddr *ifa = ifac->ifa;
1631 if (space <= sizeof ifr)
1634 if (cred->cr_prison &&
1635 prison_if(cred, sa))
1639 if (cmd == OSIOCGIFCONF) {
1640 struct osockaddr *osa =
1641 (struct osockaddr *)&ifr.ifr_addr;
1643 osa->sa_family = sa->sa_family;
1644 error = copyout(&ifr, ifrp, sizeof ifr);
1648 if (sa->sa_len <= sizeof(*sa)) {
1650 error = copyout(&ifr, ifrp, sizeof ifr);
1653 if (space < (sizeof ifr) + sa->sa_len -
1656 space -= sa->sa_len - sizeof(*sa);
1657 error = copyout(&ifr, ifrp,
1658 sizeof ifr.ifr_name);
1660 error = copyout(sa, &ifrp->ifr_addr,
1662 ifrp = (struct ifreq *)
1663 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1667 space -= sizeof ifr;
1672 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
1673 error = copyout(&ifr, ifrp, sizeof ifr);
1676 space -= sizeof ifr;
1680 ifc->ifc_len -= space;
1685 * Just like if_promisc(), but for all-multicast-reception mode.
1688 if_allmulti(struct ifnet *ifp, int onswitch)
1696 if (ifp->if_amcount++ == 0) {
1697 ifp->if_flags |= IFF_ALLMULTI;
1698 ifr.ifr_flags = ifp->if_flags;
1699 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1700 lwkt_serialize_enter(ifp->if_serializer);
1701 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1702 (struct ucred *)NULL);
1703 lwkt_serialize_exit(ifp->if_serializer);
1706 if (ifp->if_amcount > 1) {
1709 ifp->if_amcount = 0;
1710 ifp->if_flags &= ~IFF_ALLMULTI;
1711 ifr.ifr_flags = ifp->if_flags;
1712 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1713 lwkt_serialize_enter(ifp->if_serializer);
1714 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1715 (struct ucred *)NULL);
1716 lwkt_serialize_exit(ifp->if_serializer);
1728 * Add a multicast listenership to the interface in question.
1729 * The link layer provides a routine which converts
1733 struct ifnet *ifp, /* interface to manipulate */
1734 struct sockaddr *sa, /* address to add */
1735 struct ifmultiaddr **retifma)
1737 struct sockaddr *llsa, *dupsa;
1739 struct ifmultiaddr *ifma;
1742 * If the matching multicast address already exists
1743 * then don't add a new one, just add a reference
1745 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1746 if (sa_equal(sa, ifma->ifma_addr)) {
1747 ifma->ifma_refcount++;
1755 * Give the link layer a chance to accept/reject it, and also
1756 * find out which AF_LINK address this maps to, if it isn't one
1759 if (ifp->if_resolvemulti) {
1760 lwkt_serialize_enter(ifp->if_serializer);
1761 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1762 lwkt_serialize_exit(ifp->if_serializer);
1769 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1770 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1771 bcopy(sa, dupsa, sa->sa_len);
1773 ifma->ifma_addr = dupsa;
1774 ifma->ifma_lladdr = llsa;
1775 ifma->ifma_ifp = ifp;
1776 ifma->ifma_refcount = 1;
1777 ifma->ifma_protospec = 0;
1778 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1781 * Some network interfaces can scan the address list at
1782 * interrupt time; lock them out.
1785 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1790 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1791 if (sa_equal(ifma->ifma_addr, llsa))
1795 ifma->ifma_refcount++;
1797 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1798 M_IFMADDR, M_WAITOK);
1799 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1800 M_IFMADDR, M_WAITOK);
1801 bcopy(llsa, dupsa, llsa->sa_len);
1802 ifma->ifma_addr = dupsa;
1803 ifma->ifma_ifp = ifp;
1804 ifma->ifma_refcount = 1;
1806 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1811 * We are certain we have added something, so call down to the
1812 * interface to let them know about it.
1815 lwkt_serialize_enter(ifp->if_serializer);
1816 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, (struct ucred *)NULL);
1817 lwkt_serialize_exit(ifp->if_serializer);
1824 * Remove a reference to a multicast address on this interface. Yell
1825 * if the request does not match an existing membership.
1828 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
1830 struct ifmultiaddr *ifma;
1832 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1833 if (sa_equal(sa, ifma->ifma_addr))
1838 if (ifma->ifma_refcount > 1) {
1839 ifma->ifma_refcount--;
1843 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1844 sa = ifma->ifma_lladdr;
1846 LIST_REMOVE(ifma, ifma_link);
1848 * Make sure the interface driver is notified
1849 * in the case of a link layer mcast group being left.
1851 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) {
1852 lwkt_serialize_enter(ifp->if_serializer);
1853 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1854 lwkt_serialize_exit(ifp->if_serializer);
1857 kfree(ifma->ifma_addr, M_IFMADDR);
1858 kfree(ifma, M_IFMADDR);
1863 * Now look for the link-layer address which corresponds to
1864 * this network address. It had been squirreled away in
1865 * ifma->ifma_lladdr for this purpose (so we don't have
1866 * to call ifp->if_resolvemulti() again), and we saved that
1867 * value in sa above. If some nasty deleted the
1868 * link-layer address out from underneath us, we can deal because
1869 * the address we stored was is not the same as the one which was
1870 * in the record for the link-layer address. (So we don't complain
1873 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1874 if (sa_equal(sa, ifma->ifma_addr))
1879 if (ifma->ifma_refcount > 1) {
1880 ifma->ifma_refcount--;
1885 lwkt_serialize_enter(ifp->if_serializer);
1886 LIST_REMOVE(ifma, ifma_link);
1887 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1888 lwkt_serialize_exit(ifp->if_serializer);
1890 kfree(ifma->ifma_addr, M_IFMADDR);
1891 kfree(sa, M_IFMADDR);
1892 kfree(ifma, M_IFMADDR);
1898 * Set the link layer address on an interface.
1900 * At this time we only support certain types of interfaces,
1901 * and we don't allow the length of the address to change.
1904 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1906 struct sockaddr_dl *sdl;
1909 sdl = IF_LLSOCKADDR(ifp);
1912 if (len != sdl->sdl_alen) /* don't allow length to change */
1914 switch (ifp->if_type) {
1915 case IFT_ETHER: /* these types use struct arpcom */
1918 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1919 bcopy(lladdr, LLADDR(sdl), len);
1925 * If the interface is already up, we need
1926 * to re-init it in order to reprogram its
1929 lwkt_serialize_enter(ifp->if_serializer);
1930 if ((ifp->if_flags & IFF_UP) != 0) {
1931 struct ifaddr_container *ifac;
1933 ifp->if_flags &= ~IFF_UP;
1934 ifr.ifr_flags = ifp->if_flags;
1935 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1936 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1937 (struct ucred *)NULL);
1938 ifp->if_flags |= IFF_UP;
1939 ifr.ifr_flags = ifp->if_flags;
1940 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1941 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1942 (struct ucred *)NULL);
1945 * Also send gratuitous ARPs to notify other nodes about
1946 * the address change.
1948 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1949 struct ifaddr *ifa = ifac->ifa;
1951 if (ifa->ifa_addr != NULL &&
1952 ifa->ifa_addr->sa_family == AF_INET)
1953 arp_ifinit(ifp, ifa);
1957 lwkt_serialize_exit(ifp->if_serializer);
1961 struct ifmultiaddr *
1962 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
1964 struct ifmultiaddr *ifma;
1966 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1967 if (sa_equal(ifma->ifma_addr, sa))
1974 * This function locates the first real ethernet MAC from a network
1975 * card and loads it into node, returning 0 on success or ENOENT if
1976 * no suitable interfaces were found. It is used by the uuid code to
1977 * generate a unique 6-byte number.
1980 if_getanyethermac(uint16_t *node, int minlen)
1983 struct sockaddr_dl *sdl;
1985 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1986 if (ifp->if_type != IFT_ETHER)
1988 sdl = IF_LLSOCKADDR(ifp);
1989 if (sdl->sdl_alen < minlen)
1991 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
1999 * The name argument must be a pointer to storage which will last as
2000 * long as the interface does. For physical devices, the result of
2001 * device_get_name(dev) is a good choice and for pseudo-devices a
2002 * static string works well.
2005 if_initname(struct ifnet *ifp, const char *name, int unit)
2007 ifp->if_dname = name;
2008 ifp->if_dunit = unit;
2009 if (unit != IF_DUNIT_NONE)
2010 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2012 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2016 if_printf(struct ifnet *ifp, const char *fmt, ...)
2021 retval = kprintf("%s: ", ifp->if_xname);
2022 __va_start(ap, fmt);
2023 retval += kvprintf(fmt, ap);
2029 ifq_set_classic(struct ifaltq *ifq)
2031 ifq->altq_enqueue = ifq_classic_enqueue;
2032 ifq->altq_dequeue = ifq_classic_dequeue;
2033 ifq->altq_request = ifq_classic_request;
2037 ifq_classic_enqueue(struct ifaltq *ifq, struct mbuf *m,
2038 struct altq_pktattr *pa __unused)
2040 logifq(enqueue, ifq);
2041 if (IF_QFULL(ifq)) {
2051 ifq_classic_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
2060 logifq(dequeue, ifq);
2064 panic("unsupported ALTQ dequeue op: %d", op);
2066 KKASSERT(mpolled == NULL || mpolled == m);
2071 ifq_classic_request(struct ifaltq *ifq, int req, void *arg)
2078 panic("unsupported ALTQ request: %d", req);
2084 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2086 struct ifaltq *ifq = &ifp->if_snd;
2087 int running = 0, error, start = 0;
2089 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
2092 error = ifq_enqueue_locked(ifq, m, pa);
2097 if (!ifq->altq_started) {
2099 * Hold the interlock of ifnet.if_start
2101 ifq->altq_started = 1;
2106 ifp->if_obytes += m->m_pkthdr.len;
2107 if (m->m_flags & M_MCAST)
2111 logifstart(avoid, ifp);
2115 if (ifq_dispatch_schedonly) {
2117 * Always schedule ifnet.if_start on ifnet's CPU,
2118 * short circuit the rest of this function.
2120 logifstart(sched, ifp);
2121 if_start_schedule(ifp);
2126 * Try to do direct ifnet.if_start first, if there is
2127 * contention on ifnet's serializer, ifnet.if_start will
2128 * be scheduled on ifnet's CPU.
2130 if (!lwkt_serialize_try(ifp->if_serializer)) {
2132 * ifnet serializer contention happened,
2133 * ifnet.if_start is scheduled on ifnet's
2134 * CPU, and we keep going.
2136 logifstart(contend_sched, ifp);
2137 if_start_schedule(ifp);
2141 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
2142 logifstart(run, ifp);
2144 if ((ifp->if_flags &
2145 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
2149 lwkt_serialize_exit(ifp->if_serializer);
2151 if (ifq_dispatch_schednochk || if_start_need_schedule(ifq, running)) {
2153 * More data need to be transmitted, ifnet.if_start is
2154 * scheduled on ifnet's CPU, and we keep going.
2155 * NOTE: ifnet.if_start interlock is not released.
2157 logifstart(sched, ifp);
2158 if_start_schedule(ifp);
2164 ifa_create(int size, int flags)
2169 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small\n"));
2171 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
2175 ifa->ifa_containers = kmalloc(ncpus * sizeof(struct ifaddr_container),
2176 M_IFADDR, M_WAITOK | M_ZERO);
2177 ifa->ifa_ncnt = ncpus;
2178 for (i = 0; i < ncpus; ++i) {
2179 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
2181 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
2183 ifac->ifa_refcnt = 1;
2186 kprintf("alloc ifa %p %d\n", ifa, size);
2192 ifac_free(struct ifaddr_container *ifac, int cpu_id)
2194 struct ifaddr *ifa = ifac->ifa;
2196 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
2197 KKASSERT(ifac->ifa_refcnt == 0);
2198 KASSERT(ifac->ifa_listmask == 0,
2199 ("ifa is still on %#x lists\n", ifac->ifa_listmask));
2201 ifac->ifa_magic = IFA_CONTAINER_DEAD;
2203 #ifdef IFADDR_DEBUG_VERBOSE
2204 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
2207 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
2208 ("invalid # of ifac, %d\n", ifa->ifa_ncnt));
2209 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
2211 kprintf("free ifa %p\n", ifa);
2213 kfree(ifa->ifa_containers, M_IFADDR);
2214 kfree(ifa, M_IFADDR);
2219 ifa_iflink_dispatch(struct netmsg *nmsg)
2221 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2222 struct ifaddr *ifa = msg->ifa;
2223 struct ifnet *ifp = msg->ifp;
2225 struct ifaddr_container *ifac;
2229 ifac = &ifa->ifa_containers[cpu];
2230 ASSERT_IFAC_VALID(ifac);
2231 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
2232 ("ifaddr is on if_addrheads\n"));
2234 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
2236 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
2238 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
2242 ifa_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
2246 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
2248 struct netmsg_ifaddr msg;
2250 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
2251 ifa_iflink_dispatch);
2256 ifa_domsg(&msg.netmsg.nm_lmsg, 0);
2260 ifa_ifunlink_dispatch(struct netmsg *nmsg)
2262 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2263 struct ifaddr *ifa = msg->ifa;
2264 struct ifnet *ifp = msg->ifp;
2266 struct ifaddr_container *ifac;
2270 ifac = &ifa->ifa_containers[cpu];
2271 ASSERT_IFAC_VALID(ifac);
2272 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
2273 ("ifaddr is not on if_addrhead\n"));
2275 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
2276 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
2280 ifa_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
2284 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
2286 struct netmsg_ifaddr msg;
2288 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
2289 ifa_ifunlink_dispatch);
2293 ifa_domsg(&msg.netmsg.nm_lmsg, 0);
2297 ifa_destroy_dispatch(struct netmsg *nmsg)
2299 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2302 ifa_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2306 ifa_destroy(struct ifaddr *ifa)
2308 struct netmsg_ifaddr msg;
2310 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
2311 ifa_destroy_dispatch);
2314 ifa_domsg(&msg.netmsg.nm_lmsg, 0);
2318 ifnet_portfn(int cpu)
2320 return &ifnet_threads[cpu].td_msgport;
2324 ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
2326 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
2328 if (next_cpu < ncpus)
2329 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
2331 lwkt_replymsg(lmsg, 0);
2335 ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
2337 KKASSERT(cpu < ncpus);
2338 lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
2342 ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
2344 KKASSERT(cpu < ncpus);
2345 lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
2349 ifnetinit(void *dummy __unused)
2353 for (i = 0; i < ncpus; ++i) {
2354 struct thread *thr = &ifnet_threads[i];
2356 lwkt_create(netmsg_service_loop, &ifnet_mpsafe_thread, NULL,
2357 thr, TDF_NETWORK | TDF_MPSAFE, i, "ifnet %d", i);
2358 netmsg_service_port_init(&thr->td_msgport);