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.81 2008/10/03 00:26:21 hasso 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/stdarg.h>
78 #include <machine/smp.h>
80 #if defined(INET) || defined(INET6)
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84 #include <netinet/if_ether.h>
86 #include <netinet6/in6_var.h>
87 #include <netinet6/in6_ifattach.h>
91 #if defined(COMPAT_43)
92 #include <emulation/43bsd/43bsd_socket.h>
93 #endif /* COMPAT_43 */
95 struct netmsg_ifaddr {
103 * System initialization
105 static void if_attachdomain(void *);
106 static void if_attachdomain1(struct ifnet *);
107 static int ifconf(u_long, caddr_t, struct ucred *);
108 static void ifinit(void *);
109 static void ifnetinit(void *);
110 static void if_slowtimo(void *);
111 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
112 static int if_rtdel(struct radix_node *, void *);
116 * XXX: declare here to avoid to include many inet6 related files..
117 * should be more generalized?
119 extern void nd6_setmtu(struct ifnet *);
122 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
123 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
125 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
126 /* Must be after netisr_init */
127 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_SECOND, ifnetinit, NULL)
129 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
130 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
132 int ifqmaxlen = IFQ_MAXLEN;
133 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
135 /* In ifq_dispatch(), try to do direct ifnet.if_start first */
136 static int ifq_dispatch_schedonly = 0;
137 SYSCTL_INT(_net_link_generic, OID_AUTO, ifq_dispatch_schedonly, CTLFLAG_RW,
138 &ifq_dispatch_schedonly, 0, "");
140 /* In ifq_dispatch(), schedule ifnet.if_start without checking ifnet.if_snd */
141 static int ifq_dispatch_schednochk = 0;
142 SYSCTL_INT(_net_link_generic, OID_AUTO, ifq_dispatch_schednochk, CTLFLAG_RW,
143 &ifq_dispatch_schednochk, 0, "");
145 /* In if_devstart(), try to do direct ifnet.if_start first */
146 static int if_devstart_schedonly = 0;
147 SYSCTL_INT(_net_link_generic, OID_AUTO, if_devstart_schedonly, CTLFLAG_RW,
148 &if_devstart_schedonly, 0, "");
150 /* In if_devstart(), schedule ifnet.if_start without checking ifnet.if_snd */
151 static int if_devstart_schednochk = 0;
152 SYSCTL_INT(_net_link_generic, OID_AUTO, if_devstart_schednochk, CTLFLAG_RW,
153 &if_devstart_schednochk, 0, "");
156 /* Schedule ifnet.if_start on the current CPU */
157 static int if_start_oncpu_sched = 0;
158 SYSCTL_INT(_net_link_generic, OID_AUTO, if_start_oncpu_sched, CTLFLAG_RW,
159 &if_start_oncpu_sched, 0, "");
162 struct callout if_slowtimo_timer;
165 struct ifnet **ifindex2ifnet = NULL;
166 static struct thread ifnet_threads[MAXCPU];
167 static int ifnet_mpsafe_thread = NETMSG_SERVICE_MPSAFE;
169 #define IFQ_KTR_STRING "ifq=%p"
170 #define IFQ_KTR_ARG_SIZE (sizeof(void *))
172 #define KTR_IFQ KTR_ALL
174 KTR_INFO_MASTER(ifq);
175 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARG_SIZE);
176 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARG_SIZE);
177 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
179 #define IF_START_KTR_STRING "ifp=%p"
180 #define IF_START_KTR_ARG_SIZE (sizeof(void *))
182 #define KTR_IF_START KTR_ALL
184 KTR_INFO_MASTER(if_start);
185 KTR_INFO(KTR_IF_START, if_start, run, 0,
186 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
187 KTR_INFO(KTR_IF_START, if_start, sched, 1,
188 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
189 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
190 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
191 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
192 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
193 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
194 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
195 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
198 * Network interface utility routines.
200 * Routines with ifa_ifwith* names take sockaddr *'s as
209 callout_init(&if_slowtimo_timer);
212 TAILQ_FOREACH(ifp, &ifnet, if_link) {
213 if (ifp->if_snd.ifq_maxlen == 0) {
214 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
215 ifp->if_snd.ifq_maxlen = ifqmaxlen;
224 if_start_cpuid(struct ifnet *ifp)
226 return ifp->if_cpuid;
229 #ifdef DEVICE_POLLING
231 if_start_cpuid_poll(struct ifnet *ifp)
233 int poll_cpuid = ifp->if_poll_cpuid;
238 return ifp->if_cpuid;
243 if_start_ipifunc(void *arg)
245 struct ifnet *ifp = arg;
246 struct lwkt_msg *lmsg = &ifp->if_start_nmsg[mycpuid].nm_lmsg;
249 if (lmsg->ms_flags & MSGF_DONE)
250 lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg);
255 * Schedule ifnet.if_start on ifnet's CPU
258 if_start_schedule(struct ifnet *ifp)
263 if (if_start_oncpu_sched)
266 cpu = ifp->if_start_cpuid(ifp);
269 lwkt_send_ipiq(globaldata_find(cpu), if_start_ipifunc, ifp);
272 if_start_ipifunc(ifp);
277 * This function will release ifnet.if_start interlock,
278 * if ifnet.if_start does not need to be scheduled
281 if_start_need_schedule(struct ifaltq *ifq, int running)
283 if (!running || ifq_is_empty(ifq)
285 || ifq->altq_tbr != NULL
290 * ifnet.if_start interlock is released, if:
291 * 1) Hardware can not take any packets, due to
292 * o interface is marked down
293 * o hardware queue is full (IFF_OACTIVE)
294 * Under the second situation, hardware interrupt
295 * or polling(4) will call/schedule ifnet.if_start
296 * when hardware queue is ready
297 * 2) There is not packet in the ifnet.if_snd.
298 * Further ifq_dispatch or ifq_handoff will call/
299 * schedule ifnet.if_start
300 * 3) TBR is used and it does not allow further
302 * TBR callout will call ifnet.if_start
304 if (!running || !ifq_data_ready(ifq)) {
305 ifq->altq_started = 0;
315 if_start_dispatch(struct netmsg *nmsg)
317 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
318 struct ifnet *ifp = lmsg->u.ms_resultp;
319 struct ifaltq *ifq = &ifp->if_snd;
323 lwkt_replymsg(lmsg, 0); /* reply ASAP */
327 if (!if_start_oncpu_sched && mycpuid != ifp->if_start_cpuid(ifp)) {
329 * If the ifnet is still up, we need to
330 * chase its CPU change.
332 if (ifp->if_flags & IFF_UP) {
333 logifstart(chase_sched, ifp);
334 if_start_schedule(ifp);
342 if (ifp->if_flags & IFF_UP) {
343 lwkt_serialize_enter(ifp->if_serializer); /* XXX try? */
344 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
345 logifstart(run, ifp);
348 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
351 lwkt_serialize_exit(ifp->if_serializer);
356 if (if_start_need_schedule(ifq, running)) {
358 if (lmsg->ms_flags & MSGF_DONE) { /* XXX necessary? */
359 logifstart(sched, ifp);
360 lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg);
366 /* Device driver ifnet.if_start helper function */
368 if_devstart(struct ifnet *ifp)
370 struct ifaltq *ifq = &ifp->if_snd;
373 ASSERT_SERIALIZED(ifp->if_serializer);
376 if (ifq->altq_started || !ifq_data_ready(ifq)) {
377 logifstart(avoid, ifp);
381 ifq->altq_started = 1;
384 if (if_devstart_schedonly) {
386 * Always schedule ifnet.if_start on ifnet's CPU,
387 * short circuit the rest of this function.
389 logifstart(sched, ifp);
390 if_start_schedule(ifp);
394 logifstart(run, ifp);
397 if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
400 if (if_devstart_schednochk || if_start_need_schedule(ifq, running)) {
402 * More data need to be transmitted, ifnet.if_start is
403 * scheduled on ifnet's CPU, and we keep going.
404 * NOTE: ifnet.if_start interlock is not released.
406 logifstart(sched, ifp);
407 if_start_schedule(ifp);
412 * Attach an interface to the list of "active" interfaces.
414 * The serializer is optional. If non-NULL access to the interface
418 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
420 unsigned socksize, ifasize;
421 int namelen, masklen;
422 struct sockaddr_dl *sdl;
427 static int if_indexlim = 8;
430 * The serializer can be passed in from the device, allowing the
431 * same serializer to be used for both the interrupt interlock and
432 * the device queue. If not specified, the netif structure will
433 * use an embedded serializer.
435 if (serializer == NULL) {
436 serializer = &ifp->if_default_serializer;
437 lwkt_serialize_init(serializer);
439 ifp->if_serializer = serializer;
441 ifp->if_start_cpuid = if_start_cpuid;
444 #ifdef DEVICE_POLLING
445 /* Device is not in polling mode by default */
446 ifp->if_poll_cpuid = -1;
447 if (ifp->if_poll != NULL)
448 ifp->if_start_cpuid = if_start_cpuid_poll;
451 ifp->if_start_nmsg = kmalloc(ncpus * sizeof(struct netmsg),
452 M_IFADDR /* XXX */, M_WAITOK);
453 for (i = 0; i < ncpus; ++i) {
454 netmsg_init(&ifp->if_start_nmsg[i], &netisr_adone_rport, 0,
456 ifp->if_start_nmsg[i].nm_lmsg.u.ms_resultp = ifp;
459 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
460 ifp->if_index = ++if_index;
464 * The old code would work if the interface passed a pre-existing
465 * chain of ifaddrs to this code. We don't trust our callers to
466 * properly initialize the tailq, however, so we no longer allow
467 * this unlikely case.
469 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
470 M_IFADDR, M_WAITOK | M_ZERO);
471 for (i = 0; i < ncpus; ++i)
472 TAILQ_INIT(&ifp->if_addrheads[i]);
474 TAILQ_INIT(&ifp->if_prefixhead);
475 LIST_INIT(&ifp->if_multiaddrs);
476 getmicrotime(&ifp->if_lastchange);
477 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
483 /* grow ifindex2ifnet */
484 n = if_indexlim * sizeof(*q);
485 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
487 bcopy(ifindex2ifnet, q, n/2);
488 kfree(ifindex2ifnet, M_IFADDR);
493 ifindex2ifnet[if_index] = ifp;
496 * create a Link Level name for this device
498 namelen = strlen(ifp->if_xname);
499 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
500 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
501 socksize = masklen + ifp->if_addrlen;
502 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
503 if (socksize < sizeof(*sdl))
504 socksize = sizeof(*sdl);
505 socksize = ROUNDUP(socksize);
506 ifasize = sizeof(struct ifaddr) + 2 * socksize;
507 ifa = ifa_create(ifasize, M_WAITOK);
508 sdl = (struct sockaddr_dl *)(ifa + 1);
509 sdl->sdl_len = socksize;
510 sdl->sdl_family = AF_LINK;
511 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
512 sdl->sdl_nlen = namelen;
513 sdl->sdl_index = ifp->if_index;
514 sdl->sdl_type = ifp->if_type;
515 ifp->if_lladdr = ifa;
517 ifa->ifa_rtrequest = link_rtrequest;
518 ifa->ifa_addr = (struct sockaddr *)sdl;
519 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
520 ifa->ifa_netmask = (struct sockaddr *)sdl;
521 sdl->sdl_len = masklen;
523 sdl->sdl_data[--namelen] = 0xff;
524 ifa_iflink(ifa, ifp, 0 /* Insert head */);
526 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
527 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
531 ifq->altq_disc = NULL;
532 ifq->altq_flags &= ALTQF_CANTCHANGE;
533 ifq->altq_tbr = NULL;
535 ifq->altq_started = 0;
536 ifq->altq_prepended = NULL;
538 ifq_set_classic(ifq);
540 if (!SLIST_EMPTY(&domains))
541 if_attachdomain1(ifp);
543 /* Announce the interface. */
544 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
548 if_attachdomain(void *dummy)
553 TAILQ_FOREACH(ifp, &ifnet, if_list)
554 if_attachdomain1(ifp);
557 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
558 if_attachdomain, NULL);
561 if_attachdomain1(struct ifnet *ifp)
567 /* address family dependent data region */
568 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
569 SLIST_FOREACH(dp, &domains, dom_next)
570 if (dp->dom_ifattach)
571 ifp->if_afdata[dp->dom_family] =
572 (*dp->dom_ifattach)(ifp);
577 * Purge all addresses whose type is _not_ AF_LINK
580 if_purgeaddrs_nolink(struct ifnet *ifp)
582 struct ifaddr_container *ifac, *next;
584 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
586 struct ifaddr *ifa = ifac->ifa;
588 /* Leave link ifaddr as it is */
589 if (ifa->ifa_addr->sa_family == AF_LINK)
592 /* XXX: Ugly!! ad hoc just for INET */
593 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
594 struct ifaliasreq ifr;
595 #ifdef IFADDR_DEBUG_VERBOSE
598 kprintf("purge in4 addr %p: ", ifa);
599 for (i = 0; i < ncpus; ++i)
600 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
604 bzero(&ifr, sizeof ifr);
605 ifr.ifra_addr = *ifa->ifa_addr;
606 if (ifa->ifa_dstaddr)
607 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
608 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
614 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
615 #ifdef IFADDR_DEBUG_VERBOSE
618 kprintf("purge in6 addr %p: ", ifa);
619 for (i = 0; i < ncpus; ++i)
620 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
625 /* ifp_addrhead is already updated */
629 ifa_ifunlink(ifa, ifp);
635 * Detach an interface, removing it from the
636 * list of "active" interfaces.
639 if_detach(struct ifnet *ifp)
641 struct radix_node_head *rnh;
646 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
649 * Remove routes and flush queues.
652 #ifdef DEVICE_POLLING
653 if (ifp->if_flags & IFF_POLLING)
654 ether_poll_deregister(ifp);
658 if (ifq_is_enabled(&ifp->if_snd))
659 altq_disable(&ifp->if_snd);
660 if (ifq_is_attached(&ifp->if_snd))
661 altq_detach(&ifp->if_snd);
664 * Clean up all addresses.
666 ifp->if_lladdr = NULL;
668 if_purgeaddrs_nolink(ifp);
669 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
672 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
673 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
674 ("non-link ifaddr is left on if_addrheads"));
676 ifa_ifunlink(ifa, ifp);
678 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
679 ("there are still ifaddrs left on if_addrheads"));
684 * Remove all IPv4 kernel structures related to ifp.
691 * Remove all IPv6 kernel structs related to ifp. This should be done
692 * before removing routing entries below, since IPv6 interface direct
693 * routes are expected to be removed by the IPv6-specific kernel API.
694 * Otherwise, the kernel will detect some inconsistency and bark it.
700 * Delete all remaining routes using this interface
701 * Unfortuneatly the only way to do this is to slog through
702 * the entire routing table looking for routes which point
703 * to this interface...oh well...
706 for (cpu = 0; cpu < ncpus2; cpu++) {
707 lwkt_migratecpu(cpu);
708 for (i = 1; i <= AF_MAX; i++) {
709 if ((rnh = rt_tables[cpu][i]) == NULL)
711 rnh->rnh_walktree(rnh, if_rtdel, ifp);
714 lwkt_migratecpu(origcpu);
716 /* Announce that the interface is gone. */
717 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
718 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
720 SLIST_FOREACH(dp, &domains, dom_next)
721 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
722 (*dp->dom_ifdetach)(ifp,
723 ifp->if_afdata[dp->dom_family]);
726 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
728 ifindex2ifnet[ifp->if_index] = NULL;
729 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
732 TAILQ_REMOVE(&ifnet, ifp, if_link);
733 kfree(ifp->if_addrheads, M_IFADDR);
734 kfree(ifp->if_start_nmsg, M_IFADDR);
739 * Delete Routes for a Network Interface
741 * Called for each routing entry via the rnh->rnh_walktree() call above
742 * to delete all route entries referencing a detaching network interface.
745 * rn pointer to node in the routing table
746 * arg argument passed to rnh->rnh_walktree() - detaching interface
750 * errno failed - reason indicated
754 if_rtdel(struct radix_node *rn, void *arg)
756 struct rtentry *rt = (struct rtentry *)rn;
757 struct ifnet *ifp = arg;
760 if (rt->rt_ifp == ifp) {
763 * Protect (sorta) against walktree recursion problems
766 if (!(rt->rt_flags & RTF_UP))
769 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
770 rt_mask(rt), rt->rt_flags,
771 (struct rtentry **) NULL);
773 log(LOG_WARNING, "if_rtdel: error %d\n", err);
781 * Locate an interface based on a complete address.
784 ifa_ifwithaddr(struct sockaddr *addr)
788 TAILQ_FOREACH(ifp, &ifnet, if_link) {
789 struct ifaddr_container *ifac;
791 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
792 struct ifaddr *ifa = ifac->ifa;
794 if (ifa->ifa_addr->sa_family != addr->sa_family)
796 if (sa_equal(addr, ifa->ifa_addr))
798 if ((ifp->if_flags & IFF_BROADCAST) &&
799 ifa->ifa_broadaddr &&
800 /* IPv6 doesn't have broadcast */
801 ifa->ifa_broadaddr->sa_len != 0 &&
802 sa_equal(ifa->ifa_broadaddr, addr))
809 * Locate the point to point interface with a given destination address.
812 ifa_ifwithdstaddr(struct sockaddr *addr)
816 TAILQ_FOREACH(ifp, &ifnet, if_link) {
817 struct ifaddr_container *ifac;
819 if (!(ifp->if_flags & IFF_POINTOPOINT))
822 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
823 struct ifaddr *ifa = ifac->ifa;
825 if (ifa->ifa_addr->sa_family != addr->sa_family)
827 if (ifa->ifa_dstaddr &&
828 sa_equal(addr, ifa->ifa_dstaddr))
836 * Find an interface on a specific network. If many, choice
837 * is most specific found.
840 ifa_ifwithnet(struct sockaddr *addr)
843 struct ifaddr *ifa_maybe = NULL;
844 u_int af = addr->sa_family;
845 char *addr_data = addr->sa_data, *cplim;
848 * AF_LINK addresses can be looked up directly by their index number,
849 * so do that if we can.
852 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
854 if (sdl->sdl_index && sdl->sdl_index <= if_index)
855 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
859 * Scan though each interface, looking for ones that have
860 * addresses in this address family.
862 TAILQ_FOREACH(ifp, &ifnet, if_link) {
863 struct ifaddr_container *ifac;
865 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
866 struct ifaddr *ifa = ifac->ifa;
867 char *cp, *cp2, *cp3;
869 if (ifa->ifa_addr->sa_family != af)
871 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
873 * This is a bit broken as it doesn't
874 * take into account that the remote end may
875 * be a single node in the network we are
877 * The trouble is that we don't know the
878 * netmask for the remote end.
880 if (ifa->ifa_dstaddr != NULL &&
881 sa_equal(addr, ifa->ifa_dstaddr))
885 * if we have a special address handler,
886 * then use it instead of the generic one.
888 if (ifa->ifa_claim_addr) {
889 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
897 * Scan all the bits in the ifa's address.
898 * If a bit dissagrees with what we are
899 * looking for, mask it with the netmask
900 * to see if it really matters.
903 if (ifa->ifa_netmask == 0)
906 cp2 = ifa->ifa_addr->sa_data;
907 cp3 = ifa->ifa_netmask->sa_data;
908 cplim = ifa->ifa_netmask->sa_len +
909 (char *)ifa->ifa_netmask;
911 if ((*cp++ ^ *cp2++) & *cp3++)
912 goto next; /* next address! */
914 * If the netmask of what we just found
915 * is more specific than what we had before
916 * (if we had one) then remember the new one
917 * before continuing to search
918 * for an even better one.
920 if (ifa_maybe == 0 ||
921 rn_refines((char *)ifa->ifa_netmask,
922 (char *)ifa_maybe->ifa_netmask))
931 * Find an interface address specific to an interface best matching
935 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
937 struct ifaddr_container *ifac;
938 char *cp, *cp2, *cp3;
940 struct ifaddr *ifa_maybe = 0;
941 u_int af = addr->sa_family;
945 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
946 struct ifaddr *ifa = ifac->ifa;
948 if (ifa->ifa_addr->sa_family != af)
952 if (ifa->ifa_netmask == NULL) {
953 if (sa_equal(addr, ifa->ifa_addr) ||
954 (ifa->ifa_dstaddr != NULL &&
955 sa_equal(addr, ifa->ifa_dstaddr)))
959 if (ifp->if_flags & IFF_POINTOPOINT) {
960 if (sa_equal(addr, ifa->ifa_dstaddr))
964 cp2 = ifa->ifa_addr->sa_data;
965 cp3 = ifa->ifa_netmask->sa_data;
966 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
967 for (; cp3 < cplim; cp3++)
968 if ((*cp++ ^ *cp2++) & *cp3)
978 * Default action when installing a route with a Link Level gateway.
979 * Lookup an appropriate real ifa to point to.
980 * This should be moved to /sys/net/link.c eventually.
983 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
986 struct sockaddr *dst;
989 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
990 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
992 ifa = ifaof_ifpforaddr(dst, ifp);
997 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
998 ifa->ifa_rtrequest(cmd, rt, info);
1003 * Mark an interface down and notify protocols of
1005 * NOTE: must be called at splnet or eqivalent.
1008 if_unroute(struct ifnet *ifp, int flag, int fam)
1010 struct ifaddr_container *ifac;
1012 ifp->if_flags &= ~flag;
1013 getmicrotime(&ifp->if_lastchange);
1014 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1015 struct ifaddr *ifa = ifac->ifa;
1017 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1018 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1020 ifq_purge(&ifp->if_snd);
1025 * Mark an interface up and notify protocols of
1027 * NOTE: must be called at splnet or eqivalent.
1030 if_route(struct ifnet *ifp, int flag, int fam)
1032 struct ifaddr_container *ifac;
1034 ifq_purge(&ifp->if_snd);
1035 ifp->if_flags |= flag;
1036 getmicrotime(&ifp->if_lastchange);
1037 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1038 struct ifaddr *ifa = ifac->ifa;
1040 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1041 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1050 * Mark an interface down and notify protocols of the transition. An
1051 * interface going down is also considered to be a synchronizing event.
1052 * We must ensure that all packet processing related to the interface
1053 * has completed before we return so e.g. the caller can free the ifnet
1054 * structure that the mbufs may be referencing.
1056 * NOTE: must be called at splnet or eqivalent.
1059 if_down(struct ifnet *ifp)
1061 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1062 netmsg_service_sync();
1066 * Mark an interface up and notify protocols of
1068 * NOTE: must be called at splnet or eqivalent.
1071 if_up(struct ifnet *ifp)
1073 if_route(ifp, IFF_UP, AF_UNSPEC);
1077 * Process a link state change.
1078 * NOTE: must be called at splsoftnet or equivalent.
1081 if_link_state_change(struct ifnet *ifp)
1083 int link_state = ifp->if_link_state;
1086 devctl_notify("IFNET", ifp->if_xname,
1087 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1091 * Handle interface watchdog timer routines. Called
1092 * from softclock, we decrement timers (if set) and
1093 * call the appropriate interface routine on expiration.
1096 if_slowtimo(void *arg)
1102 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1103 if (ifp->if_timer == 0 || --ifp->if_timer)
1105 if (ifp->if_watchdog) {
1106 if (lwkt_serialize_try(ifp->if_serializer)) {
1107 (*ifp->if_watchdog)(ifp);
1108 lwkt_serialize_exit(ifp->if_serializer);
1110 /* try again next timeout */
1118 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1122 * Map interface name to
1123 * interface structure pointer.
1126 ifunit(const char *name)
1131 * Search all the interfaces for this name/number
1134 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1135 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1143 * Map interface name in a sockaddr_dl to
1144 * interface structure pointer.
1147 if_withname(struct sockaddr *sa)
1149 char ifname[IFNAMSIZ+1];
1150 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1152 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1153 (sdl->sdl_nlen > IFNAMSIZ) )
1157 * ifunit wants a null-terminated name. It may not be null-terminated
1158 * in the sockaddr. We don't want to change the caller's sockaddr,
1159 * and there might not be room to put the trailing null anyway, so we
1160 * make a local copy that we know we can null terminate safely.
1163 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1164 ifname[sdl->sdl_nlen] = '\0';
1165 return ifunit(ifname);
1173 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1181 size_t namelen, onamelen;
1182 char new_name[IFNAMSIZ];
1184 struct sockaddr_dl *sdl;
1190 return (ifconf(cmd, data, cred));
1192 ifr = (struct ifreq *)data;
1197 if ((error = suser_cred(cred, 0)) != 0)
1199 return ((cmd == SIOCIFCREATE) ?
1200 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1201 if_clone_destroy(ifr->ifr_name));
1203 case SIOCIFGCLONERS:
1204 return (if_clone_list((struct if_clonereq *)data));
1207 ifp = ifunit(ifr->ifr_name);
1213 ifr->ifr_flags = ifp->if_flags;
1214 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1218 ifr->ifr_reqcap = ifp->if_capabilities;
1219 ifr->ifr_curcap = ifp->if_capenable;
1223 ifr->ifr_metric = ifp->if_metric;
1227 ifr->ifr_mtu = ifp->if_mtu;
1231 ifr->ifr_phys = ifp->if_physical;
1234 case SIOCGIFPOLLCPU:
1235 #ifdef DEVICE_POLLING
1236 ifr->ifr_pollcpu = ifp->if_poll_cpuid;
1238 ifr->ifr_pollcpu = -1;
1242 case SIOCSIFPOLLCPU:
1243 #ifdef DEVICE_POLLING
1244 if ((ifp->if_flags & IFF_POLLING) == 0)
1245 ether_pollcpu_register(ifp, ifr->ifr_pollcpu);
1250 error = suser_cred(cred, 0);
1253 new_flags = (ifr->ifr_flags & 0xffff) |
1254 (ifr->ifr_flagshigh << 16);
1255 if (ifp->if_flags & IFF_SMART) {
1256 /* Smart drivers twiddle their own routes */
1257 } else if (ifp->if_flags & IFF_UP &&
1258 (new_flags & IFF_UP) == 0) {
1262 } else if (new_flags & IFF_UP &&
1263 (ifp->if_flags & IFF_UP) == 0) {
1269 #ifdef DEVICE_POLLING
1270 if ((new_flags ^ ifp->if_flags) & IFF_POLLING) {
1271 if (new_flags & IFF_POLLING) {
1272 ether_poll_register(ifp);
1274 ether_poll_deregister(ifp);
1279 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1280 (new_flags &~ IFF_CANTCHANGE);
1281 if (new_flags & IFF_PPROMISC) {
1282 /* Permanently promiscuous mode requested */
1283 ifp->if_flags |= IFF_PROMISC;
1284 } else if (ifp->if_pcount == 0) {
1285 ifp->if_flags &= ~IFF_PROMISC;
1287 if (ifp->if_ioctl) {
1288 lwkt_serialize_enter(ifp->if_serializer);
1289 ifp->if_ioctl(ifp, cmd, data, cred);
1290 lwkt_serialize_exit(ifp->if_serializer);
1292 getmicrotime(&ifp->if_lastchange);
1296 error = suser_cred(cred, 0);
1299 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1301 lwkt_serialize_enter(ifp->if_serializer);
1302 ifp->if_ioctl(ifp, cmd, data, cred);
1303 lwkt_serialize_exit(ifp->if_serializer);
1307 error = suser_cred(cred, 0);
1310 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1313 if (new_name[0] == '\0')
1315 if (ifunit(new_name) != NULL)
1318 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1320 /* Announce the departure of the interface. */
1321 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1323 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1324 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1325 /* XXX IFA_LOCK(ifa); */
1326 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1327 namelen = strlen(new_name);
1328 onamelen = sdl->sdl_nlen;
1330 * Move the address if needed. This is safe because we
1331 * allocate space for a name of length IFNAMSIZ when we
1332 * create this in if_attach().
1334 if (namelen != onamelen) {
1335 bcopy(sdl->sdl_data + onamelen,
1336 sdl->sdl_data + namelen, sdl->sdl_alen);
1338 bcopy(new_name, sdl->sdl_data, namelen);
1339 sdl->sdl_nlen = namelen;
1340 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1341 bzero(sdl->sdl_data, onamelen);
1342 while (namelen != 0)
1343 sdl->sdl_data[--namelen] = 0xff;
1344 /* XXX IFA_UNLOCK(ifa) */
1346 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1348 /* Announce the return of the interface. */
1349 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1353 error = suser_cred(cred, 0);
1356 ifp->if_metric = ifr->ifr_metric;
1357 getmicrotime(&ifp->if_lastchange);
1361 error = suser_cred(cred, 0);
1366 lwkt_serialize_enter(ifp->if_serializer);
1367 error = ifp->if_ioctl(ifp, cmd, data, cred);
1368 lwkt_serialize_exit(ifp->if_serializer);
1370 getmicrotime(&ifp->if_lastchange);
1375 u_long oldmtu = ifp->if_mtu;
1377 error = suser_cred(cred, 0);
1380 if (ifp->if_ioctl == NULL)
1381 return (EOPNOTSUPP);
1382 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1384 lwkt_serialize_enter(ifp->if_serializer);
1385 error = ifp->if_ioctl(ifp, cmd, data, cred);
1386 lwkt_serialize_exit(ifp->if_serializer);
1388 getmicrotime(&ifp->if_lastchange);
1392 * If the link MTU changed, do network layer specific procedure.
1394 if (ifp->if_mtu != oldmtu) {
1404 error = suser_cred(cred, 0);
1408 /* Don't allow group membership on non-multicast interfaces. */
1409 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1412 /* Don't let users screw up protocols' entries. */
1413 if (ifr->ifr_addr.sa_family != AF_LINK)
1416 if (cmd == SIOCADDMULTI) {
1417 struct ifmultiaddr *ifma;
1418 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1420 error = if_delmulti(ifp, &ifr->ifr_addr);
1423 getmicrotime(&ifp->if_lastchange);
1426 case SIOCSIFPHYADDR:
1427 case SIOCDIFPHYADDR:
1429 case SIOCSIFPHYADDR_IN6:
1431 case SIOCSLIFPHYADDR:
1433 case SIOCSIFGENERIC:
1434 error = suser_cred(cred, 0);
1437 if (ifp->if_ioctl == 0)
1438 return (EOPNOTSUPP);
1439 lwkt_serialize_enter(ifp->if_serializer);
1440 error = ifp->if_ioctl(ifp, cmd, data, cred);
1441 lwkt_serialize_exit(ifp->if_serializer);
1443 getmicrotime(&ifp->if_lastchange);
1447 ifs = (struct ifstat *)data;
1448 ifs->ascii[0] = '\0';
1450 case SIOCGIFPSRCADDR:
1451 case SIOCGIFPDSTADDR:
1452 case SIOCGLIFPHYADDR:
1454 case SIOCGIFGENERIC:
1455 if (ifp->if_ioctl == NULL)
1456 return (EOPNOTSUPP);
1457 lwkt_serialize_enter(ifp->if_serializer);
1458 error = ifp->if_ioctl(ifp, cmd, data, cred);
1459 lwkt_serialize_exit(ifp->if_serializer);
1463 error = suser_cred(cred, 0);
1466 return if_setlladdr(ifp,
1467 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1470 oif_flags = ifp->if_flags;
1471 if (so->so_proto == 0)
1472 return (EOPNOTSUPP);
1474 error = so_pru_control(so, cmd, data, ifp);
1481 case SIOCSIFDSTADDR:
1483 case SIOCSIFBRDADDR:
1484 case SIOCSIFNETMASK:
1485 #if BYTE_ORDER != BIG_ENDIAN
1486 if (ifr->ifr_addr.sa_family == 0 &&
1487 ifr->ifr_addr.sa_len < 16) {
1488 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1489 ifr->ifr_addr.sa_len = 16;
1492 if (ifr->ifr_addr.sa_len == 0)
1493 ifr->ifr_addr.sa_len = 16;
1501 case OSIOCGIFDSTADDR:
1502 cmd = SIOCGIFDSTADDR;
1505 case OSIOCGIFBRDADDR:
1506 cmd = SIOCGIFBRDADDR;
1509 case OSIOCGIFNETMASK:
1510 cmd = SIOCGIFNETMASK;
1512 error = so_pru_control(so, cmd, data, ifp);
1516 case OSIOCGIFDSTADDR:
1517 case OSIOCGIFBRDADDR:
1518 case OSIOCGIFNETMASK:
1519 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1523 #endif /* COMPAT_43 */
1525 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1527 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1528 if (ifp->if_flags & IFF_UP) {
1542 * Set/clear promiscuous mode on interface ifp based on the truth value
1543 * of pswitch. The calls are reference counted so that only the first
1544 * "on" request actually has an effect, as does the final "off" request.
1545 * Results are undefined if the "off" and "on" requests are not matched.
1548 ifpromisc(struct ifnet *ifp, int pswitch)
1554 oldflags = ifp->if_flags;
1555 if (ifp->if_flags & IFF_PPROMISC) {
1556 /* Do nothing if device is in permanently promiscuous mode */
1557 ifp->if_pcount += pswitch ? 1 : -1;
1562 * If the device is not configured up, we cannot put it in
1565 if ((ifp->if_flags & IFF_UP) == 0)
1567 if (ifp->if_pcount++ != 0)
1569 ifp->if_flags |= IFF_PROMISC;
1570 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1573 if (--ifp->if_pcount > 0)
1575 ifp->if_flags &= ~IFF_PROMISC;
1576 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1579 ifr.ifr_flags = ifp->if_flags;
1580 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1581 lwkt_serialize_enter(ifp->if_serializer);
1582 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1583 (struct ucred *)NULL);
1584 lwkt_serialize_exit(ifp->if_serializer);
1588 ifp->if_flags = oldflags;
1593 * Return interface configuration
1594 * of system. List may be used
1595 * in later ioctl's (above) to get
1596 * other information.
1599 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
1601 struct ifconf *ifc = (struct ifconf *)data;
1603 struct sockaddr *sa;
1604 struct ifreq ifr, *ifrp;
1605 int space = ifc->ifc_len, error = 0;
1607 ifrp = ifc->ifc_req;
1608 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1609 struct ifaddr_container *ifac;
1612 if (space <= sizeof ifr)
1616 * Zero the stack declared structure first to prevent
1617 * memory disclosure.
1619 bzero(&ifr, sizeof(ifr));
1620 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1621 >= sizeof(ifr.ifr_name)) {
1622 error = ENAMETOOLONG;
1627 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1628 struct ifaddr *ifa = ifac->ifa;
1630 if (space <= sizeof ifr)
1633 if (cred->cr_prison &&
1634 prison_if(cred, sa))
1638 if (cmd == OSIOCGIFCONF) {
1639 struct osockaddr *osa =
1640 (struct osockaddr *)&ifr.ifr_addr;
1642 osa->sa_family = sa->sa_family;
1643 error = copyout(&ifr, ifrp, sizeof ifr);
1647 if (sa->sa_len <= sizeof(*sa)) {
1649 error = copyout(&ifr, ifrp, sizeof ifr);
1652 if (space < (sizeof ifr) + sa->sa_len -
1655 space -= sa->sa_len - sizeof(*sa);
1656 error = copyout(&ifr, ifrp,
1657 sizeof ifr.ifr_name);
1659 error = copyout(sa, &ifrp->ifr_addr,
1661 ifrp = (struct ifreq *)
1662 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1666 space -= sizeof ifr;
1671 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
1672 error = copyout(&ifr, ifrp, sizeof ifr);
1675 space -= sizeof ifr;
1679 ifc->ifc_len -= space;
1684 * Just like if_promisc(), but for all-multicast-reception mode.
1687 if_allmulti(struct ifnet *ifp, int onswitch)
1695 if (ifp->if_amcount++ == 0) {
1696 ifp->if_flags |= IFF_ALLMULTI;
1697 ifr.ifr_flags = ifp->if_flags;
1698 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1699 lwkt_serialize_enter(ifp->if_serializer);
1700 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1701 (struct ucred *)NULL);
1702 lwkt_serialize_exit(ifp->if_serializer);
1705 if (ifp->if_amcount > 1) {
1708 ifp->if_amcount = 0;
1709 ifp->if_flags &= ~IFF_ALLMULTI;
1710 ifr.ifr_flags = ifp->if_flags;
1711 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1712 lwkt_serialize_enter(ifp->if_serializer);
1713 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1714 (struct ucred *)NULL);
1715 lwkt_serialize_exit(ifp->if_serializer);
1727 * Add a multicast listenership to the interface in question.
1728 * The link layer provides a routine which converts
1732 struct ifnet *ifp, /* interface to manipulate */
1733 struct sockaddr *sa, /* address to add */
1734 struct ifmultiaddr **retifma)
1736 struct sockaddr *llsa, *dupsa;
1738 struct ifmultiaddr *ifma;
1741 * If the matching multicast address already exists
1742 * then don't add a new one, just add a reference
1744 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1745 if (sa_equal(sa, ifma->ifma_addr)) {
1746 ifma->ifma_refcount++;
1754 * Give the link layer a chance to accept/reject it, and also
1755 * find out which AF_LINK address this maps to, if it isn't one
1758 if (ifp->if_resolvemulti) {
1759 lwkt_serialize_enter(ifp->if_serializer);
1760 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1761 lwkt_serialize_exit(ifp->if_serializer);
1768 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1769 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1770 bcopy(sa, dupsa, sa->sa_len);
1772 ifma->ifma_addr = dupsa;
1773 ifma->ifma_lladdr = llsa;
1774 ifma->ifma_ifp = ifp;
1775 ifma->ifma_refcount = 1;
1776 ifma->ifma_protospec = 0;
1777 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1780 * Some network interfaces can scan the address list at
1781 * interrupt time; lock them out.
1784 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1789 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1790 if (sa_equal(ifma->ifma_addr, llsa))
1794 ifma->ifma_refcount++;
1796 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1797 M_IFMADDR, M_WAITOK);
1798 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1799 M_IFMADDR, M_WAITOK);
1800 bcopy(llsa, dupsa, llsa->sa_len);
1801 ifma->ifma_addr = dupsa;
1802 ifma->ifma_ifp = ifp;
1803 ifma->ifma_refcount = 1;
1805 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1810 * We are certain we have added something, so call down to the
1811 * interface to let them know about it.
1814 lwkt_serialize_enter(ifp->if_serializer);
1815 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, (struct ucred *)NULL);
1816 lwkt_serialize_exit(ifp->if_serializer);
1823 * Remove a reference to a multicast address on this interface. Yell
1824 * if the request does not match an existing membership.
1827 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
1829 struct ifmultiaddr *ifma;
1831 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1832 if (sa_equal(sa, ifma->ifma_addr))
1837 if (ifma->ifma_refcount > 1) {
1838 ifma->ifma_refcount--;
1842 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1843 sa = ifma->ifma_lladdr;
1845 LIST_REMOVE(ifma, ifma_link);
1847 * Make sure the interface driver is notified
1848 * in the case of a link layer mcast group being left.
1850 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) {
1851 lwkt_serialize_enter(ifp->if_serializer);
1852 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1853 lwkt_serialize_exit(ifp->if_serializer);
1856 kfree(ifma->ifma_addr, M_IFMADDR);
1857 kfree(ifma, M_IFMADDR);
1862 * Now look for the link-layer address which corresponds to
1863 * this network address. It had been squirreled away in
1864 * ifma->ifma_lladdr for this purpose (so we don't have
1865 * to call ifp->if_resolvemulti() again), and we saved that
1866 * value in sa above. If some nasty deleted the
1867 * link-layer address out from underneath us, we can deal because
1868 * the address we stored was is not the same as the one which was
1869 * in the record for the link-layer address. (So we don't complain
1872 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1873 if (sa_equal(sa, ifma->ifma_addr))
1878 if (ifma->ifma_refcount > 1) {
1879 ifma->ifma_refcount--;
1884 lwkt_serialize_enter(ifp->if_serializer);
1885 LIST_REMOVE(ifma, ifma_link);
1886 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1887 lwkt_serialize_exit(ifp->if_serializer);
1889 kfree(ifma->ifma_addr, M_IFMADDR);
1890 kfree(sa, M_IFMADDR);
1891 kfree(ifma, M_IFMADDR);
1897 * Set the link layer address on an interface.
1899 * At this time we only support certain types of interfaces,
1900 * and we don't allow the length of the address to change.
1903 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1905 struct sockaddr_dl *sdl;
1908 sdl = IF_LLSOCKADDR(ifp);
1911 if (len != sdl->sdl_alen) /* don't allow length to change */
1913 switch (ifp->if_type) {
1914 case IFT_ETHER: /* these types use struct arpcom */
1917 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1918 bcopy(lladdr, LLADDR(sdl), len);
1924 * If the interface is already up, we need
1925 * to re-init it in order to reprogram its
1928 lwkt_serialize_enter(ifp->if_serializer);
1929 if ((ifp->if_flags & IFF_UP) != 0) {
1930 struct ifaddr_container *ifac;
1932 ifp->if_flags &= ~IFF_UP;
1933 ifr.ifr_flags = ifp->if_flags;
1934 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1935 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1936 (struct ucred *)NULL);
1937 ifp->if_flags |= IFF_UP;
1938 ifr.ifr_flags = ifp->if_flags;
1939 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1940 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1941 (struct ucred *)NULL);
1944 * Also send gratuitous ARPs to notify other nodes about
1945 * the address change.
1947 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1948 struct ifaddr *ifa = ifac->ifa;
1950 if (ifa->ifa_addr != NULL &&
1951 ifa->ifa_addr->sa_family == AF_INET)
1952 arp_ifinit(ifp, ifa);
1956 lwkt_serialize_exit(ifp->if_serializer);
1960 struct ifmultiaddr *
1961 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
1963 struct ifmultiaddr *ifma;
1965 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1966 if (sa_equal(ifma->ifma_addr, sa))
1973 * This function locates the first real ethernet MAC from a network
1974 * card and loads it into node, returning 0 on success or ENOENT if
1975 * no suitable interfaces were found. It is used by the uuid code to
1976 * generate a unique 6-byte number.
1979 if_getanyethermac(uint16_t *node, int minlen)
1982 struct sockaddr_dl *sdl;
1984 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1985 if (ifp->if_type != IFT_ETHER)
1987 sdl = IF_LLSOCKADDR(ifp);
1988 if (sdl->sdl_alen < minlen)
1990 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
1998 * The name argument must be a pointer to storage which will last as
1999 * long as the interface does. For physical devices, the result of
2000 * device_get_name(dev) is a good choice and for pseudo-devices a
2001 * static string works well.
2004 if_initname(struct ifnet *ifp, const char *name, int unit)
2006 ifp->if_dname = name;
2007 ifp->if_dunit = unit;
2008 if (unit != IF_DUNIT_NONE)
2009 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2011 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2015 if_printf(struct ifnet *ifp, const char *fmt, ...)
2020 retval = kprintf("%s: ", ifp->if_xname);
2021 __va_start(ap, fmt);
2022 retval += kvprintf(fmt, ap);
2028 ifq_set_classic(struct ifaltq *ifq)
2030 ifq->altq_enqueue = ifq_classic_enqueue;
2031 ifq->altq_dequeue = ifq_classic_dequeue;
2032 ifq->altq_request = ifq_classic_request;
2036 ifq_classic_enqueue(struct ifaltq *ifq, struct mbuf *m,
2037 struct altq_pktattr *pa __unused)
2039 logifq(enqueue, ifq);
2040 if (IF_QFULL(ifq)) {
2050 ifq_classic_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
2059 logifq(dequeue, ifq);
2063 panic("unsupported ALTQ dequeue op: %d", op);
2065 KKASSERT(mpolled == NULL || mpolled == m);
2070 ifq_classic_request(struct ifaltq *ifq, int req, void *arg)
2077 panic("unsupported ALTQ request: %d", req);
2083 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2085 struct ifaltq *ifq = &ifp->if_snd;
2086 int running = 0, error, start = 0;
2088 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
2091 error = ifq_enqueue_locked(ifq, m, pa);
2096 if (!ifq->altq_started) {
2098 * Hold the interlock of ifnet.if_start
2100 ifq->altq_started = 1;
2105 ifp->if_obytes += m->m_pkthdr.len;
2106 if (m->m_flags & M_MCAST)
2110 logifstart(avoid, ifp);
2114 if (ifq_dispatch_schedonly) {
2116 * Always schedule ifnet.if_start on ifnet's CPU,
2117 * short circuit the rest of this function.
2119 logifstart(sched, ifp);
2120 if_start_schedule(ifp);
2125 * Try to do direct ifnet.if_start first, if there is
2126 * contention on ifnet's serializer, ifnet.if_start will
2127 * be scheduled on ifnet's CPU.
2129 if (!lwkt_serialize_try(ifp->if_serializer)) {
2131 * ifnet serializer contention happened,
2132 * ifnet.if_start is scheduled on ifnet's
2133 * CPU, and we keep going.
2135 logifstart(contend_sched, ifp);
2136 if_start_schedule(ifp);
2140 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
2141 logifstart(run, ifp);
2143 if ((ifp->if_flags &
2144 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
2148 lwkt_serialize_exit(ifp->if_serializer);
2150 if (ifq_dispatch_schednochk || if_start_need_schedule(ifq, running)) {
2152 * More data need to be transmitted, ifnet.if_start is
2153 * scheduled on ifnet's CPU, and we keep going.
2154 * NOTE: ifnet.if_start interlock is not released.
2156 logifstart(sched, ifp);
2157 if_start_schedule(ifp);
2163 ifa_create(int size, int flags)
2168 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small\n"));
2170 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
2174 ifa->ifa_containers = kmalloc(ncpus * sizeof(struct ifaddr_container),
2175 M_IFADDR, M_WAITOK | M_ZERO);
2176 ifa->ifa_cpumask = smp_active_mask;
2177 for (i = 0; i < ncpus; ++i) {
2178 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
2180 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
2182 ifac->ifa_refcnt = 1;
2185 kprintf("alloc ifa %p %d\n", ifa, size);
2191 ifac_free_dispatch(struct netmsg *nmsg)
2193 struct netmsg_ifaddr_free *fmsg = (struct netmsg_ifaddr_free *)nmsg;
2194 struct ifaddr *ifa = fmsg->nm_ifaddr;
2196 KKASSERT(ifa->ifa_cpumask & (1 << fmsg->nm_cpuid));
2197 ifa->ifa_cpumask &= ~(1 << fmsg->nm_cpuid);
2198 if (ifa->ifa_cpumask == 0) {
2200 kprintf("free ifa %p\n", ifa);
2202 kfree(ifa->ifa_containers, M_IFADDR);
2203 kfree(ifa, M_IFADDR);
2205 /* Don't reply, 'nmsg' is embedded in ifaddr_container */
2209 ifac_free(struct ifaddr_container *ifac, int cpu_id)
2211 struct netmsg_ifaddr_free *fmsg;
2213 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
2214 KKASSERT(ifac->ifa_refcnt == 0);
2215 KASSERT(ifac->ifa_listmask == 0,
2216 ("ifa is still on %#x lists\n", ifac->ifa_listmask));
2218 ifac->ifa_magic = IFA_CONTAINER_DEAD;
2220 #ifdef IFADDR_DEBUG_VERBOSE
2221 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
2224 fmsg = &ifac->ifa_freemsg;
2225 netmsg_init(&fmsg->nm_netmsg, &netisr_apanic_rport, 0,
2226 ifac_free_dispatch);
2227 fmsg->nm_ifaddr = ifac->ifa;
2228 fmsg->nm_cpuid = cpu_id;
2230 ifa_sendmsg(&fmsg->nm_netmsg.nm_lmsg, 0);
2234 ifa_iflink_dispatch(struct netmsg *nmsg)
2236 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2237 struct ifaddr *ifa = msg->ifa;
2238 struct ifnet *ifp = msg->ifp;
2240 struct ifaddr_container *ifac;
2244 ifac = &ifa->ifa_containers[cpu];
2245 ASSERT_IFAC_VALID(ifac);
2246 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
2247 ("ifaddr is on if_addrheads\n"));
2249 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
2251 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
2253 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
2257 ifa_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
2261 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
2263 struct netmsg_ifaddr msg;
2265 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
2266 ifa_iflink_dispatch);
2271 ifa_domsg(&msg.netmsg.nm_lmsg, 0);
2275 ifa_ifunlink_dispatch(struct netmsg *nmsg)
2277 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2278 struct ifaddr *ifa = msg->ifa;
2279 struct ifnet *ifp = msg->ifp;
2281 struct ifaddr_container *ifac;
2285 ifac = &ifa->ifa_containers[cpu];
2286 ASSERT_IFAC_VALID(ifac);
2287 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
2288 ("ifaddr is not on if_addrhead\n"));
2290 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
2291 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
2295 ifa_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
2299 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
2301 struct netmsg_ifaddr msg;
2303 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
2304 ifa_ifunlink_dispatch);
2308 ifa_domsg(&msg.netmsg.nm_lmsg, 0);
2312 ifa_destroy_dispatch(struct netmsg *nmsg)
2314 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2317 ifa_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2321 ifa_destroy(struct ifaddr *ifa)
2323 struct netmsg_ifaddr msg;
2325 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
2326 ifa_destroy_dispatch);
2329 ifa_domsg(&msg.netmsg.nm_lmsg, 0);
2333 ifnet_portfn(int cpu)
2335 return &ifnet_threads[cpu].td_msgport;
2339 ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
2341 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
2343 if (next_cpu < ncpus)
2344 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
2346 lwkt_replymsg(lmsg, 0);
2350 ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
2352 KKASSERT(cpu < ncpus);
2353 lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
2357 ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
2359 KKASSERT(cpu < ncpus);
2360 lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
2364 ifnetinit(void *dummy __unused)
2368 for (i = 0; i < ncpus; ++i) {
2369 struct thread *thr = &ifnet_threads[i];
2371 lwkt_create(netmsg_service_loop, &ifnet_mpsafe_thread, NULL,
2372 thr, TDF_NETWORK | TDF_MPSAFE, i, "ifnet %d", i);
2373 netmsg_service_port_init(&thr->td_msgport);