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.62 2008/03/08 07:50:49 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>
54 #include <sys/sockio.h>
55 #include <sys/syslog.h>
56 #include <sys/sysctl.h>
57 #include <sys/domain.h>
58 #include <sys/thread.h>
59 #include <sys/thread2.h>
60 #include <sys/serialize.h>
61 #include <sys/msgport2.h>
64 #include <net/if_arp.h>
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 #include <net/if_var.h>
68 #include <net/ifq_var.h>
69 #include <net/radix.h>
70 #include <net/route.h>
71 #include <net/if_clone.h>
72 #include <net/netmsg2.h>
74 #include <machine/stdarg.h>
75 #include <machine/smp.h>
77 #if defined(INET) || defined(INET6)
79 #include <netinet/in.h>
80 #include <netinet/in_var.h>
81 #include <netinet/if_ether.h>
83 #include <netinet6/in6_var.h>
84 #include <netinet6/in6_ifattach.h>
88 #if defined(COMPAT_43)
89 #include <emulation/43bsd/43bsd_socket.h>
90 #endif /* COMPAT_43 */
92 struct netmsg_ifaddr {
100 * Support for non-ALTQ interfaces.
102 static int ifq_classic_enqueue(struct ifaltq *, struct mbuf *,
103 struct altq_pktattr *);
105 ifq_classic_dequeue(struct ifaltq *, struct mbuf *, int);
106 static int ifq_classic_request(struct ifaltq *, int, void *);
109 * System initialization
111 static void if_attachdomain(void *);
112 static void if_attachdomain1(struct ifnet *);
113 static int ifconf(u_long, caddr_t, struct ucred *);
114 static void ifinit(void *);
115 static void ifaddrinit(void *);
116 static void if_slowtimo(void *);
117 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
118 static int if_rtdel(struct radix_node *, void *);
122 * XXX: declare here to avoid to include many inet6 related files..
123 * should be more generalized?
125 extern void nd6_setmtu(struct ifnet *);
128 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
129 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
131 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
132 /* Must be after netisr_init */
133 SYSINIT(ifaddr, SI_SUB_PRE_DRIVERS, SI_ORDER_SECOND, ifaddrinit, NULL)
135 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
136 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
138 int ifqmaxlen = IFQ_MAXLEN;
139 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
141 struct callout if_slowtimo_timer;
144 struct ifnet **ifindex2ifnet = NULL;
145 static struct thread ifaddr_threads[MAXCPU];
148 * Network interface utility routines.
150 * Routines with ifa_ifwith* names take sockaddr *'s as
159 callout_init(&if_slowtimo_timer);
162 TAILQ_FOREACH(ifp, &ifnet, if_link) {
163 if (ifp->if_snd.ifq_maxlen == 0) {
164 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
165 ifp->if_snd.ifq_maxlen = ifqmaxlen;
174 * Attach an interface to the list of "active" interfaces.
176 * The serializer is optional. If non-NULL access to the interface
180 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
182 unsigned socksize, ifasize;
183 int namelen, masklen;
184 struct sockaddr_dl *sdl;
189 static int if_indexlim = 8;
192 * The serializer can be passed in from the device, allowing the
193 * same serializer to be used for both the interrupt interlock and
194 * the device queue. If not specified, the netif structure will
195 * use an embedded serializer.
197 if (serializer == NULL) {
198 serializer = &ifp->if_default_serializer;
199 lwkt_serialize_init(serializer);
201 ifp->if_serializer = serializer;
203 #ifdef DEVICE_POLLING
204 /* Device is not in polling mode by default */
205 ifp->if_poll_cpuid = -1;
208 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
209 ifp->if_index = ++if_index;
213 * The old code would work if the interface passed a pre-existing
214 * chain of ifaddrs to this code. We don't trust our callers to
215 * properly initialize the tailq, however, so we no longer allow
216 * this unlikely case.
218 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
219 M_IFADDR, M_WAITOK | M_ZERO);
220 for (i = 0; i < ncpus; ++i)
221 TAILQ_INIT(&ifp->if_addrheads[i]);
223 TAILQ_INIT(&ifp->if_prefixhead);
224 LIST_INIT(&ifp->if_multiaddrs);
225 getmicrotime(&ifp->if_lastchange);
226 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
232 /* grow ifindex2ifnet */
233 n = if_indexlim * sizeof(*q);
234 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
236 bcopy(ifindex2ifnet, q, n/2);
237 kfree(ifindex2ifnet, M_IFADDR);
242 ifindex2ifnet[if_index] = ifp;
245 * create a Link Level name for this device
247 namelen = strlen(ifp->if_xname);
248 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
249 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
250 socksize = masklen + ifp->if_addrlen;
251 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
252 if (socksize < sizeof(*sdl))
253 socksize = sizeof(*sdl);
254 socksize = ROUNDUP(socksize);
255 ifasize = sizeof(struct ifaddr) + 2 * socksize;
256 ifa = ifa_create(ifasize, M_WAITOK);
257 sdl = (struct sockaddr_dl *)(ifa + 1);
258 sdl->sdl_len = socksize;
259 sdl->sdl_family = AF_LINK;
260 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
261 sdl->sdl_nlen = namelen;
262 sdl->sdl_index = ifp->if_index;
263 sdl->sdl_type = ifp->if_type;
264 ifp->if_lladdr = ifa;
266 ifa->ifa_rtrequest = link_rtrequest;
267 ifa->ifa_addr = (struct sockaddr *)sdl;
268 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
269 ifa->ifa_netmask = (struct sockaddr *)sdl;
270 sdl->sdl_len = masklen;
272 sdl->sdl_data[--namelen] = 0xff;
273 ifa_iflink(ifa, ifp, 0 /* Insert head */);
275 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
279 ifq->altq_disc = NULL;
280 ifq->altq_flags &= ALTQF_CANTCHANGE;
281 ifq->altq_tbr = NULL;
283 ifq_set_classic(ifq);
285 if (!SLIST_EMPTY(&domains))
286 if_attachdomain1(ifp);
288 /* Announce the interface. */
289 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
293 if_attachdomain(void *dummy)
298 TAILQ_FOREACH(ifp, &ifnet, if_list)
299 if_attachdomain1(ifp);
302 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
303 if_attachdomain, NULL);
306 if_attachdomain1(struct ifnet *ifp)
312 /* address family dependent data region */
313 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
314 SLIST_FOREACH(dp, &domains, dom_next)
315 if (dp->dom_ifattach)
316 ifp->if_afdata[dp->dom_family] =
317 (*dp->dom_ifattach)(ifp);
322 * Purge all addresses whose type is _not_ AF_LINK
325 if_purgeaddrs_nolink(struct ifnet *ifp)
327 struct ifaddr_container *ifac, *next;
329 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
331 struct ifaddr *ifa = ifac->ifa;
333 /* Leave link ifaddr as it is */
334 if (ifa->ifa_addr->sa_family == AF_LINK)
337 /* XXX: Ugly!! ad hoc just for INET */
338 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
339 struct ifaliasreq ifr;
340 #ifdef IFADDR_DEBUG_VERBOSE
343 kprintf("purge in4 addr %p: ", ifa);
344 for (i = 0; i < ncpus; ++i)
345 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
349 bzero(&ifr, sizeof ifr);
350 ifr.ifra_addr = *ifa->ifa_addr;
351 if (ifa->ifa_dstaddr)
352 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
353 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
359 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
360 #ifdef IFADDR_DEBUG_VERBOSE
363 kprintf("purge in6 addr %p: ", ifa);
364 for (i = 0; i < ncpus; ++i)
365 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
370 /* ifp_addrhead is already updated */
374 ifa_ifunlink(ifa, ifp);
380 * Detach an interface, removing it from the
381 * list of "active" interfaces.
384 if_detach(struct ifnet *ifp)
386 struct radix_node_head *rnh;
391 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
394 * Remove routes and flush queues.
397 #ifdef DEVICE_POLLING
398 if (ifp->if_flags & IFF_POLLING)
399 ether_poll_deregister(ifp);
403 if (ifq_is_enabled(&ifp->if_snd))
404 altq_disable(&ifp->if_snd);
405 if (ifq_is_attached(&ifp->if_snd))
406 altq_detach(&ifp->if_snd);
409 * Clean up all addresses.
411 ifp->if_lladdr = NULL;
413 if_purgeaddrs_nolink(ifp);
414 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
417 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
418 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
419 ("non-link ifaddr is left on if_addrheads"));
421 ifa_ifunlink(ifa, ifp);
423 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
424 ("there are still ifaddrs left on if_addrheads"));
429 * Remove all IPv4 kernel structures related to ifp.
436 * Remove all IPv6 kernel structs related to ifp. This should be done
437 * before removing routing entries below, since IPv6 interface direct
438 * routes are expected to be removed by the IPv6-specific kernel API.
439 * Otherwise, the kernel will detect some inconsistency and bark it.
445 * Delete all remaining routes using this interface
446 * Unfortuneatly the only way to do this is to slog through
447 * the entire routing table looking for routes which point
448 * to this interface...oh well...
451 for (cpu = 0; cpu < ncpus2; cpu++) {
452 lwkt_migratecpu(cpu);
453 for (i = 1; i <= AF_MAX; i++) {
454 if ((rnh = rt_tables[cpu][i]) == NULL)
456 rnh->rnh_walktree(rnh, if_rtdel, ifp);
459 lwkt_migratecpu(origcpu);
461 /* Announce that the interface is gone. */
462 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
464 SLIST_FOREACH(dp, &domains, dom_next)
465 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
466 (*dp->dom_ifdetach)(ifp,
467 ifp->if_afdata[dp->dom_family]);
470 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
472 ifindex2ifnet[ifp->if_index] = NULL;
473 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
476 TAILQ_REMOVE(&ifnet, ifp, if_link);
477 kfree(ifp->if_addrheads, M_IFADDR);
482 * Delete Routes for a Network Interface
484 * Called for each routing entry via the rnh->rnh_walktree() call above
485 * to delete all route entries referencing a detaching network interface.
488 * rn pointer to node in the routing table
489 * arg argument passed to rnh->rnh_walktree() - detaching interface
493 * errno failed - reason indicated
497 if_rtdel(struct radix_node *rn, void *arg)
499 struct rtentry *rt = (struct rtentry *)rn;
500 struct ifnet *ifp = arg;
503 if (rt->rt_ifp == ifp) {
506 * Protect (sorta) against walktree recursion problems
509 if (!(rt->rt_flags & RTF_UP))
512 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
513 rt_mask(rt), rt->rt_flags,
514 (struct rtentry **) NULL);
516 log(LOG_WARNING, "if_rtdel: error %d\n", err);
524 * Locate an interface based on a complete address.
527 ifa_ifwithaddr(struct sockaddr *addr)
531 TAILQ_FOREACH(ifp, &ifnet, if_link) {
532 struct ifaddr_container *ifac;
534 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
535 struct ifaddr *ifa = ifac->ifa;
537 if (ifa->ifa_addr->sa_family != addr->sa_family)
539 if (sa_equal(addr, ifa->ifa_addr))
541 if ((ifp->if_flags & IFF_BROADCAST) &&
542 ifa->ifa_broadaddr &&
543 /* IPv6 doesn't have broadcast */
544 ifa->ifa_broadaddr->sa_len != 0 &&
545 sa_equal(ifa->ifa_broadaddr, addr))
552 * Locate the point to point interface with a given destination address.
555 ifa_ifwithdstaddr(struct sockaddr *addr)
559 TAILQ_FOREACH(ifp, &ifnet, if_link) {
560 struct ifaddr_container *ifac;
562 if (!(ifp->if_flags & IFF_POINTOPOINT))
565 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
566 struct ifaddr *ifa = ifac->ifa;
568 if (ifa->ifa_addr->sa_family != addr->sa_family)
570 if (ifa->ifa_dstaddr &&
571 sa_equal(addr, ifa->ifa_dstaddr))
579 * Find an interface on a specific network. If many, choice
580 * is most specific found.
583 ifa_ifwithnet(struct sockaddr *addr)
586 struct ifaddr *ifa_maybe = NULL;
587 u_int af = addr->sa_family;
588 char *addr_data = addr->sa_data, *cplim;
591 * AF_LINK addresses can be looked up directly by their index number,
592 * so do that if we can.
595 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
597 if (sdl->sdl_index && sdl->sdl_index <= if_index)
598 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
602 * Scan though each interface, looking for ones that have
603 * addresses in this address family.
605 TAILQ_FOREACH(ifp, &ifnet, if_link) {
606 struct ifaddr_container *ifac;
608 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
609 struct ifaddr *ifa = ifac->ifa;
610 char *cp, *cp2, *cp3;
612 if (ifa->ifa_addr->sa_family != af)
614 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
616 * This is a bit broken as it doesn't
617 * take into account that the remote end may
618 * be a single node in the network we are
620 * The trouble is that we don't know the
621 * netmask for the remote end.
623 if (ifa->ifa_dstaddr != NULL &&
624 sa_equal(addr, ifa->ifa_dstaddr))
628 * if we have a special address handler,
629 * then use it instead of the generic one.
631 if (ifa->ifa_claim_addr) {
632 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
640 * Scan all the bits in the ifa's address.
641 * If a bit dissagrees with what we are
642 * looking for, mask it with the netmask
643 * to see if it really matters.
646 if (ifa->ifa_netmask == 0)
649 cp2 = ifa->ifa_addr->sa_data;
650 cp3 = ifa->ifa_netmask->sa_data;
651 cplim = ifa->ifa_netmask->sa_len +
652 (char *)ifa->ifa_netmask;
654 if ((*cp++ ^ *cp2++) & *cp3++)
655 goto next; /* next address! */
657 * If the netmask of what we just found
658 * is more specific than what we had before
659 * (if we had one) then remember the new one
660 * before continuing to search
661 * for an even better one.
663 if (ifa_maybe == 0 ||
664 rn_refines((char *)ifa->ifa_netmask,
665 (char *)ifa_maybe->ifa_netmask))
674 * Find an interface address specific to an interface best matching
678 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
680 struct ifaddr_container *ifac;
681 char *cp, *cp2, *cp3;
683 struct ifaddr *ifa_maybe = 0;
684 u_int af = addr->sa_family;
688 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
689 struct ifaddr *ifa = ifac->ifa;
691 if (ifa->ifa_addr->sa_family != af)
695 if (ifa->ifa_netmask == NULL) {
696 if (sa_equal(addr, ifa->ifa_addr) ||
697 (ifa->ifa_dstaddr != NULL &&
698 sa_equal(addr, ifa->ifa_dstaddr)))
702 if (ifp->if_flags & IFF_POINTOPOINT) {
703 if (sa_equal(addr, ifa->ifa_dstaddr))
707 cp2 = ifa->ifa_addr->sa_data;
708 cp3 = ifa->ifa_netmask->sa_data;
709 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
710 for (; cp3 < cplim; cp3++)
711 if ((*cp++ ^ *cp2++) & *cp3)
721 * Default action when installing a route with a Link Level gateway.
722 * Lookup an appropriate real ifa to point to.
723 * This should be moved to /sys/net/link.c eventually.
726 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
729 struct sockaddr *dst;
732 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
733 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
735 ifa = ifaof_ifpforaddr(dst, ifp);
740 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
741 ifa->ifa_rtrequest(cmd, rt, info);
746 * Mark an interface down and notify protocols of
748 * NOTE: must be called at splnet or eqivalent.
751 if_unroute(struct ifnet *ifp, int flag, int fam)
753 struct ifaddr_container *ifac;
755 ifp->if_flags &= ~flag;
756 getmicrotime(&ifp->if_lastchange);
757 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
758 struct ifaddr *ifa = ifac->ifa;
760 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
761 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
763 ifq_purge(&ifp->if_snd);
768 * Mark an interface up and notify protocols of
770 * NOTE: must be called at splnet or eqivalent.
773 if_route(struct ifnet *ifp, int flag, int fam)
775 struct ifaddr_container *ifac;
777 ifp->if_flags |= flag;
778 getmicrotime(&ifp->if_lastchange);
779 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
780 struct ifaddr *ifa = ifac->ifa;
782 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
783 pfctlinput(PRC_IFUP, ifa->ifa_addr);
792 * Mark an interface down and notify protocols of the transition. An
793 * interface going down is also considered to be a synchronizing event.
794 * We must ensure that all packet processing related to the interface
795 * has completed before we return so e.g. the caller can free the ifnet
796 * structure that the mbufs may be referencing.
798 * NOTE: must be called at splnet or eqivalent.
801 if_down(struct ifnet *ifp)
803 if_unroute(ifp, IFF_UP, AF_UNSPEC);
804 netmsg_service_sync();
808 * Mark an interface up and notify protocols of
810 * NOTE: must be called at splnet or eqivalent.
813 if_up(struct ifnet *ifp)
816 if_route(ifp, IFF_UP, AF_UNSPEC);
820 * Process a link state change.
821 * NOTE: must be called at splsoftnet or equivalent.
824 if_link_state_change(struct ifnet *ifp)
830 * Handle interface watchdog timer routines. Called
831 * from softclock, we decrement timers (if set) and
832 * call the appropriate interface routine on expiration.
835 if_slowtimo(void *arg)
841 TAILQ_FOREACH(ifp, &ifnet, if_link) {
842 if (ifp->if_timer == 0 || --ifp->if_timer)
844 if (ifp->if_watchdog) {
845 if (lwkt_serialize_try(ifp->if_serializer)) {
846 (*ifp->if_watchdog)(ifp);
847 lwkt_serialize_exit(ifp->if_serializer);
849 /* try again next timeout */
857 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
861 * Map interface name to
862 * interface structure pointer.
865 ifunit(const char *name)
870 * Search all the interfaces for this name/number
873 TAILQ_FOREACH(ifp, &ifnet, if_link) {
874 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
882 * Map interface name in a sockaddr_dl to
883 * interface structure pointer.
886 if_withname(struct sockaddr *sa)
888 char ifname[IFNAMSIZ+1];
889 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
891 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
892 (sdl->sdl_nlen > IFNAMSIZ) )
896 * ifunit wants a null-terminated name. It may not be null-terminated
897 * in the sockaddr. We don't want to change the caller's sockaddr,
898 * and there might not be room to put the trailing null anyway, so we
899 * make a local copy that we know we can null terminate safely.
902 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
903 ifname[sdl->sdl_nlen] = '\0';
904 return ifunit(ifname);
912 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
920 size_t namelen, onamelen;
921 char new_name[IFNAMSIZ];
923 struct sockaddr_dl *sdl;
929 return (ifconf(cmd, data, cred));
931 ifr = (struct ifreq *)data;
936 if ((error = suser_cred(cred, 0)) != 0)
938 return ((cmd == SIOCIFCREATE) ?
939 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
940 if_clone_destroy(ifr->ifr_name));
943 return (if_clone_list((struct if_clonereq *)data));
946 ifp = ifunit(ifr->ifr_name);
952 ifr->ifr_flags = ifp->if_flags;
953 ifr->ifr_flagshigh = ifp->if_flags >> 16;
957 ifr->ifr_reqcap = ifp->if_capabilities;
958 ifr->ifr_curcap = ifp->if_capenable;
962 ifr->ifr_metric = ifp->if_metric;
966 ifr->ifr_mtu = ifp->if_mtu;
970 ifr->ifr_phys = ifp->if_physical;
974 #ifdef DEVICE_POLLING
975 ifr->ifr_pollcpu = ifp->if_poll_cpuid;
977 ifr->ifr_pollcpu = -1;
982 #ifdef DEVICE_POLLING
983 if ((ifp->if_flags & IFF_POLLING) == 0)
984 ether_pollcpu_register(ifp, ifr->ifr_pollcpu);
989 error = suser_cred(cred, 0);
992 new_flags = (ifr->ifr_flags & 0xffff) |
993 (ifr->ifr_flagshigh << 16);
994 if (ifp->if_flags & IFF_SMART) {
995 /* Smart drivers twiddle their own routes */
996 } else if (ifp->if_flags & IFF_UP &&
997 (new_flags & IFF_UP) == 0) {
1001 } else if (new_flags & IFF_UP &&
1002 (ifp->if_flags & IFF_UP) == 0) {
1008 #ifdef DEVICE_POLLING
1009 if ((new_flags ^ ifp->if_flags) & IFF_POLLING) {
1010 if (new_flags & IFF_POLLING) {
1011 ether_poll_register(ifp);
1013 ether_poll_deregister(ifp);
1018 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1019 (new_flags &~ IFF_CANTCHANGE);
1020 if (new_flags & IFF_PPROMISC) {
1021 /* Permanently promiscuous mode requested */
1022 ifp->if_flags |= IFF_PROMISC;
1023 } else if (ifp->if_pcount == 0) {
1024 ifp->if_flags &= ~IFF_PROMISC;
1026 if (ifp->if_ioctl) {
1027 lwkt_serialize_enter(ifp->if_serializer);
1028 ifp->if_ioctl(ifp, cmd, data, cred);
1029 lwkt_serialize_exit(ifp->if_serializer);
1031 getmicrotime(&ifp->if_lastchange);
1035 error = suser_cred(cred, 0);
1038 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1040 lwkt_serialize_enter(ifp->if_serializer);
1041 ifp->if_ioctl(ifp, cmd, data, cred);
1042 lwkt_serialize_exit(ifp->if_serializer);
1046 error = suser_cred(cred, 0);
1049 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1052 if (new_name[0] == '\0')
1054 if (ifunit(new_name) != NULL)
1057 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1059 /* Announce the departure of the interface. */
1060 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1062 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1063 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1064 /* XXX IFA_LOCK(ifa); */
1065 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1066 namelen = strlen(new_name);
1067 onamelen = sdl->sdl_nlen;
1069 * Move the address if needed. This is safe because we
1070 * allocate space for a name of length IFNAMSIZ when we
1071 * create this in if_attach().
1073 if (namelen != onamelen) {
1074 bcopy(sdl->sdl_data + onamelen,
1075 sdl->sdl_data + namelen, sdl->sdl_alen);
1077 bcopy(new_name, sdl->sdl_data, namelen);
1078 sdl->sdl_nlen = namelen;
1079 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1080 bzero(sdl->sdl_data, onamelen);
1081 while (namelen != 0)
1082 sdl->sdl_data[--namelen] = 0xff;
1083 /* XXX IFA_UNLOCK(ifa) */
1085 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1087 /* Announce the return of the interface. */
1088 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1092 error = suser_cred(cred, 0);
1095 ifp->if_metric = ifr->ifr_metric;
1096 getmicrotime(&ifp->if_lastchange);
1100 error = suser_cred(cred, 0);
1105 lwkt_serialize_enter(ifp->if_serializer);
1106 error = ifp->if_ioctl(ifp, cmd, data, cred);
1107 lwkt_serialize_exit(ifp->if_serializer);
1109 getmicrotime(&ifp->if_lastchange);
1114 u_long oldmtu = ifp->if_mtu;
1116 error = suser_cred(cred, 0);
1119 if (ifp->if_ioctl == NULL)
1120 return (EOPNOTSUPP);
1121 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1123 lwkt_serialize_enter(ifp->if_serializer);
1124 error = ifp->if_ioctl(ifp, cmd, data, cred);
1125 lwkt_serialize_exit(ifp->if_serializer);
1127 getmicrotime(&ifp->if_lastchange);
1131 * If the link MTU changed, do network layer specific procedure.
1133 if (ifp->if_mtu != oldmtu) {
1143 error = suser_cred(cred, 0);
1147 /* Don't allow group membership on non-multicast interfaces. */
1148 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1151 /* Don't let users screw up protocols' entries. */
1152 if (ifr->ifr_addr.sa_family != AF_LINK)
1155 if (cmd == SIOCADDMULTI) {
1156 struct ifmultiaddr *ifma;
1157 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1159 error = if_delmulti(ifp, &ifr->ifr_addr);
1162 getmicrotime(&ifp->if_lastchange);
1165 case SIOCSIFPHYADDR:
1166 case SIOCDIFPHYADDR:
1168 case SIOCSIFPHYADDR_IN6:
1170 case SIOCSLIFPHYADDR:
1172 case SIOCSIFGENERIC:
1173 error = suser_cred(cred, 0);
1176 if (ifp->if_ioctl == 0)
1177 return (EOPNOTSUPP);
1178 lwkt_serialize_enter(ifp->if_serializer);
1179 error = ifp->if_ioctl(ifp, cmd, data, cred);
1180 lwkt_serialize_exit(ifp->if_serializer);
1182 getmicrotime(&ifp->if_lastchange);
1186 ifs = (struct ifstat *)data;
1187 ifs->ascii[0] = '\0';
1189 case SIOCGIFPSRCADDR:
1190 case SIOCGIFPDSTADDR:
1191 case SIOCGLIFPHYADDR:
1193 case SIOCGIFGENERIC:
1194 if (ifp->if_ioctl == NULL)
1195 return (EOPNOTSUPP);
1196 lwkt_serialize_enter(ifp->if_serializer);
1197 error = ifp->if_ioctl(ifp, cmd, data, cred);
1198 lwkt_serialize_exit(ifp->if_serializer);
1202 error = suser_cred(cred, 0);
1205 return if_setlladdr(ifp,
1206 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1209 oif_flags = ifp->if_flags;
1210 if (so->so_proto == 0)
1211 return (EOPNOTSUPP);
1213 error = so_pru_control(so, cmd, data, ifp);
1220 case SIOCSIFDSTADDR:
1222 case SIOCSIFBRDADDR:
1223 case SIOCSIFNETMASK:
1224 #if BYTE_ORDER != BIG_ENDIAN
1225 if (ifr->ifr_addr.sa_family == 0 &&
1226 ifr->ifr_addr.sa_len < 16) {
1227 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1228 ifr->ifr_addr.sa_len = 16;
1231 if (ifr->ifr_addr.sa_len == 0)
1232 ifr->ifr_addr.sa_len = 16;
1240 case OSIOCGIFDSTADDR:
1241 cmd = SIOCGIFDSTADDR;
1244 case OSIOCGIFBRDADDR:
1245 cmd = SIOCGIFBRDADDR;
1248 case OSIOCGIFNETMASK:
1249 cmd = SIOCGIFNETMASK;
1251 error = so_pru_control(so, cmd, data, ifp);
1255 case OSIOCGIFDSTADDR:
1256 case OSIOCGIFBRDADDR:
1257 case OSIOCGIFNETMASK:
1258 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1262 #endif /* COMPAT_43 */
1264 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1266 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1267 if (ifp->if_flags & IFF_UP) {
1281 * Set/clear promiscuous mode on interface ifp based on the truth value
1282 * of pswitch. The calls are reference counted so that only the first
1283 * "on" request actually has an effect, as does the final "off" request.
1284 * Results are undefined if the "off" and "on" requests are not matched.
1287 ifpromisc(struct ifnet *ifp, int pswitch)
1293 oldflags = ifp->if_flags;
1294 if (ifp->if_flags & IFF_PPROMISC) {
1295 /* Do nothing if device is in permanently promiscuous mode */
1296 ifp->if_pcount += pswitch ? 1 : -1;
1301 * If the device is not configured up, we cannot put it in
1304 if ((ifp->if_flags & IFF_UP) == 0)
1306 if (ifp->if_pcount++ != 0)
1308 ifp->if_flags |= IFF_PROMISC;
1309 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1312 if (--ifp->if_pcount > 0)
1314 ifp->if_flags &= ~IFF_PROMISC;
1315 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1318 ifr.ifr_flags = ifp->if_flags;
1319 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1320 lwkt_serialize_enter(ifp->if_serializer);
1321 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1322 (struct ucred *)NULL);
1323 lwkt_serialize_exit(ifp->if_serializer);
1327 ifp->if_flags = oldflags;
1332 * Return interface configuration
1333 * of system. List may be used
1334 * in later ioctl's (above) to get
1335 * other information.
1338 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
1340 struct ifconf *ifc = (struct ifconf *)data;
1342 struct sockaddr *sa;
1343 struct ifreq ifr, *ifrp;
1344 int space = ifc->ifc_len, error = 0;
1346 ifrp = ifc->ifc_req;
1347 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1348 struct ifaddr_container *ifac;
1351 if (space <= sizeof ifr)
1355 * Zero the stack declared structure first to prevent
1356 * memory disclosure.
1358 bzero(&ifr, sizeof(ifr));
1359 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1360 >= sizeof(ifr.ifr_name)) {
1361 error = ENAMETOOLONG;
1366 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1367 struct ifaddr *ifa = ifac->ifa;
1369 if (space <= sizeof ifr)
1372 if (cred->cr_prison &&
1373 prison_if(cred, sa))
1377 if (cmd == OSIOCGIFCONF) {
1378 struct osockaddr *osa =
1379 (struct osockaddr *)&ifr.ifr_addr;
1381 osa->sa_family = sa->sa_family;
1382 error = copyout(&ifr, ifrp, sizeof ifr);
1386 if (sa->sa_len <= sizeof(*sa)) {
1388 error = copyout(&ifr, ifrp, sizeof ifr);
1391 if (space < (sizeof ifr) + sa->sa_len -
1394 space -= sa->sa_len - sizeof(*sa);
1395 error = copyout(&ifr, ifrp,
1396 sizeof ifr.ifr_name);
1398 error = copyout(sa, &ifrp->ifr_addr,
1400 ifrp = (struct ifreq *)
1401 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1405 space -= sizeof ifr;
1410 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
1411 error = copyout(&ifr, ifrp, sizeof ifr);
1414 space -= sizeof ifr;
1418 ifc->ifc_len -= space;
1423 * Just like if_promisc(), but for all-multicast-reception mode.
1426 if_allmulti(struct ifnet *ifp, int onswitch)
1434 if (ifp->if_amcount++ == 0) {
1435 ifp->if_flags |= IFF_ALLMULTI;
1436 ifr.ifr_flags = ifp->if_flags;
1437 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1438 lwkt_serialize_enter(ifp->if_serializer);
1439 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1440 (struct ucred *)NULL);
1441 lwkt_serialize_exit(ifp->if_serializer);
1444 if (ifp->if_amcount > 1) {
1447 ifp->if_amcount = 0;
1448 ifp->if_flags &= ~IFF_ALLMULTI;
1449 ifr.ifr_flags = ifp->if_flags;
1450 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1451 lwkt_serialize_enter(ifp->if_serializer);
1452 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1453 (struct ucred *)NULL);
1454 lwkt_serialize_exit(ifp->if_serializer);
1466 * Add a multicast listenership to the interface in question.
1467 * The link layer provides a routine which converts
1471 struct ifnet *ifp, /* interface to manipulate */
1472 struct sockaddr *sa, /* address to add */
1473 struct ifmultiaddr **retifma)
1475 struct sockaddr *llsa, *dupsa;
1477 struct ifmultiaddr *ifma;
1480 * If the matching multicast address already exists
1481 * then don't add a new one, just add a reference
1483 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1484 if (sa_equal(sa, ifma->ifma_addr)) {
1485 ifma->ifma_refcount++;
1493 * Give the link layer a chance to accept/reject it, and also
1494 * find out which AF_LINK address this maps to, if it isn't one
1497 if (ifp->if_resolvemulti) {
1498 lwkt_serialize_enter(ifp->if_serializer);
1499 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1500 lwkt_serialize_exit(ifp->if_serializer);
1507 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1508 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1509 bcopy(sa, dupsa, sa->sa_len);
1511 ifma->ifma_addr = dupsa;
1512 ifma->ifma_lladdr = llsa;
1513 ifma->ifma_ifp = ifp;
1514 ifma->ifma_refcount = 1;
1515 ifma->ifma_protospec = 0;
1516 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1519 * Some network interfaces can scan the address list at
1520 * interrupt time; lock them out.
1523 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1528 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1529 if (sa_equal(ifma->ifma_addr, llsa))
1533 ifma->ifma_refcount++;
1535 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1536 M_IFMADDR, M_WAITOK);
1537 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1538 M_IFMADDR, M_WAITOK);
1539 bcopy(llsa, dupsa, llsa->sa_len);
1540 ifma->ifma_addr = dupsa;
1541 ifma->ifma_ifp = ifp;
1542 ifma->ifma_refcount = 1;
1544 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1549 * We are certain we have added something, so call down to the
1550 * interface to let them know about it.
1553 lwkt_serialize_enter(ifp->if_serializer);
1554 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, (struct ucred *)NULL);
1555 lwkt_serialize_exit(ifp->if_serializer);
1562 * Remove a reference to a multicast address on this interface. Yell
1563 * if the request does not match an existing membership.
1566 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
1568 struct ifmultiaddr *ifma;
1570 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1571 if (sa_equal(sa, ifma->ifma_addr))
1576 if (ifma->ifma_refcount > 1) {
1577 ifma->ifma_refcount--;
1581 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1582 sa = ifma->ifma_lladdr;
1584 LIST_REMOVE(ifma, ifma_link);
1586 * Make sure the interface driver is notified
1587 * in the case of a link layer mcast group being left.
1589 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) {
1590 lwkt_serialize_enter(ifp->if_serializer);
1591 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1592 lwkt_serialize_exit(ifp->if_serializer);
1595 kfree(ifma->ifma_addr, M_IFMADDR);
1596 kfree(ifma, M_IFMADDR);
1601 * Now look for the link-layer address which corresponds to
1602 * this network address. It had been squirreled away in
1603 * ifma->ifma_lladdr for this purpose (so we don't have
1604 * to call ifp->if_resolvemulti() again), and we saved that
1605 * value in sa above. If some nasty deleted the
1606 * link-layer address out from underneath us, we can deal because
1607 * the address we stored was is not the same as the one which was
1608 * in the record for the link-layer address. (So we don't complain
1611 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1612 if (sa_equal(sa, ifma->ifma_addr))
1617 if (ifma->ifma_refcount > 1) {
1618 ifma->ifma_refcount--;
1623 lwkt_serialize_enter(ifp->if_serializer);
1624 LIST_REMOVE(ifma, ifma_link);
1625 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1626 lwkt_serialize_exit(ifp->if_serializer);
1628 kfree(ifma->ifma_addr, M_IFMADDR);
1629 kfree(sa, M_IFMADDR);
1630 kfree(ifma, M_IFMADDR);
1636 * Set the link layer address on an interface.
1638 * At this time we only support certain types of interfaces,
1639 * and we don't allow the length of the address to change.
1642 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1644 struct sockaddr_dl *sdl;
1647 sdl = IF_LLSOCKADDR(ifp);
1650 if (len != sdl->sdl_alen) /* don't allow length to change */
1652 switch (ifp->if_type) {
1653 case IFT_ETHER: /* these types use struct arpcom */
1656 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1657 bcopy(lladdr, LLADDR(sdl), len);
1663 * If the interface is already up, we need
1664 * to re-init it in order to reprogram its
1667 lwkt_serialize_enter(ifp->if_serializer);
1668 if ((ifp->if_flags & IFF_UP) != 0) {
1669 struct ifaddr_container *ifac;
1671 ifp->if_flags &= ~IFF_UP;
1672 ifr.ifr_flags = ifp->if_flags;
1673 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1674 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1675 (struct ucred *)NULL);
1676 ifp->if_flags |= IFF_UP;
1677 ifr.ifr_flags = ifp->if_flags;
1678 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1679 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1680 (struct ucred *)NULL);
1683 * Also send gratuitous ARPs to notify other nodes about
1684 * the address change.
1686 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1687 struct ifaddr *ifa = ifac->ifa;
1689 if (ifa->ifa_addr != NULL &&
1690 ifa->ifa_addr->sa_family == AF_INET)
1691 arp_ifinit(ifp, ifa);
1695 lwkt_serialize_exit(ifp->if_serializer);
1699 struct ifmultiaddr *
1700 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
1702 struct ifmultiaddr *ifma;
1704 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1705 if (sa_equal(ifma->ifma_addr, sa))
1712 * This function locates the first real ethernet MAC from a network
1713 * card and loads it into node, returning 0 on success or ENOENT if
1714 * no suitable interfaces were found. It is used by the uuid code to
1715 * generate a unique 6-byte number.
1718 if_getanyethermac(uint16_t *node, int minlen)
1721 struct sockaddr_dl *sdl;
1723 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1724 if (ifp->if_type != IFT_ETHER)
1726 sdl = IF_LLSOCKADDR(ifp);
1727 if (sdl->sdl_alen < minlen)
1729 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
1737 * The name argument must be a pointer to storage which will last as
1738 * long as the interface does. For physical devices, the result of
1739 * device_get_name(dev) is a good choice and for pseudo-devices a
1740 * static string works well.
1743 if_initname(struct ifnet *ifp, const char *name, int unit)
1745 ifp->if_dname = name;
1746 ifp->if_dunit = unit;
1747 if (unit != IF_DUNIT_NONE)
1748 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
1750 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1754 if_printf(struct ifnet *ifp, const char *fmt, ...)
1759 retval = kprintf("%s: ", ifp->if_xname);
1760 __va_start(ap, fmt);
1761 retval += kvprintf(fmt, ap);
1767 ifq_set_classic(struct ifaltq *ifq)
1769 ifq->altq_enqueue = ifq_classic_enqueue;
1770 ifq->altq_dequeue = ifq_classic_dequeue;
1771 ifq->altq_request = ifq_classic_request;
1775 ifq_classic_enqueue(struct ifaltq *ifq, struct mbuf *m,
1776 struct altq_pktattr *pa __unused)
1779 if (IF_QFULL(ifq)) {
1790 static struct mbuf *
1791 ifq_classic_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
1804 panic("unsupported ALTQ dequeue op: %d", op);
1807 KKASSERT(mpolled == NULL || mpolled == m);
1812 ifq_classic_request(struct ifaltq *ifq, int req, void *arg)
1820 panic("unsupported ALTQ request: %d", req);
1827 ifa_create(int size, int flags)
1832 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small\n"));
1834 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
1838 ifa->ifa_containers = kmalloc(ncpus * sizeof(struct ifaddr_container),
1839 M_IFADDR, M_WAITOK | M_ZERO);
1840 ifa->ifa_cpumask = smp_active_mask;
1841 for (i = 0; i < ncpus; ++i) {
1842 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
1844 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
1846 ifac->ifa_refcnt = 1;
1849 kprintf("alloc ifa %p %d\n", ifa, size);
1854 struct ifac_free_arg {
1860 ifac_free_dispatch(struct netmsg *nmsg)
1862 struct lwkt_msg *msg = &nmsg->nm_lmsg;
1863 struct ifac_free_arg *arg = msg->u.ms_resultp;
1864 struct ifaddr *ifa = arg->ifa;
1866 ifa->ifa_cpumask &= ~(1 << arg->cpuid);
1867 if (ifa->ifa_cpumask == 0) {
1869 kprintf("free ifa %p\n", ifa);
1871 kfree(ifa->ifa_containers, M_IFADDR);
1872 kfree(ifa, M_IFADDR);
1874 lwkt_replymsg(msg, 0);
1878 ifac_free(struct ifaddr_container *ifac, int cpu_id)
1880 struct ifac_free_arg arg;
1882 struct lwkt_msg *msg;
1884 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
1885 KKASSERT(ifac->ifa_refcnt == 0);
1887 ifac->ifa_magic = IFA_CONTAINER_DEAD;
1889 bzero(&arg, sizeof(arg));
1890 arg.ifa = ifac->ifa;
1892 #ifdef IFADDR_DEBUG_VERBOSE
1893 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, arg.cpuid);
1896 netmsg_init(&nmsg, &curthread->td_msgport, 0, ifac_free_dispatch);
1897 msg = &nmsg.nm_lmsg;
1898 msg->u.ms_resultp = &arg;
1900 lwkt_domsg(ifa_portfn(0), msg, 0);
1903 static __inline void
1904 ifa_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
1906 if (next_cpu < ncpus)
1907 lwkt_forwardmsg(ifa_portfn(next_cpu), lmsg);
1909 lwkt_replymsg(lmsg, 0);
1913 ifa_iflink_dispatch(struct netmsg *nmsg)
1915 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
1916 struct ifaddr *ifa = msg->ifa;
1917 struct ifnet *ifp = msg->ifp;
1922 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu],
1923 &ifa->ifa_containers[cpu], ifa_link);
1925 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu],
1926 &ifa->ifa_containers[cpu], ifa_link);
1930 ifa_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
1934 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
1936 struct netmsg_ifaddr msg;
1938 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
1939 ifa_iflink_dispatch);
1944 lwkt_domsg(ifa_portfn(0), &msg.netmsg.nm_lmsg, 0);
1948 ifa_ifunlink_dispatch(struct netmsg *nmsg)
1950 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
1951 struct ifaddr *ifa = msg->ifa;
1952 struct ifnet *ifp = msg->ifp;
1956 TAILQ_REMOVE(&ifp->if_addrheads[cpu],
1957 &ifa->ifa_containers[cpu], ifa_link);
1960 ifa_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
1964 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
1966 struct netmsg_ifaddr msg;
1968 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
1969 ifa_ifunlink_dispatch);
1973 lwkt_domsg(ifa_portfn(0), &msg.netmsg.nm_lmsg, 0);
1977 ifa_destroy_dispatch(struct netmsg *nmsg)
1979 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
1982 ifa_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
1986 ifa_destroy(struct ifaddr *ifa)
1988 struct netmsg_ifaddr msg;
1990 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
1991 ifa_destroy_dispatch);
1994 lwkt_domsg(ifa_portfn(0), &msg.netmsg.nm_lmsg, 0);
2000 return &ifaddr_threads[cpu].td_msgport;
2004 ifaddrinit(void *dummy __unused)
2008 for (i = 0; i < ncpus; ++i) {
2009 struct thread *thr = &ifaddr_threads[i];
2011 lwkt_create(netmsg_service_loop, NULL, NULL, thr, 0, i,
2013 netmsg_service_port_init(&thr->td_msgport);