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.56 2007/09/30 04:37:27 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/serialize.h>
62 #include <net/if_arp.h>
63 #include <net/if_dl.h>
64 #include <net/if_types.h>
65 #include <net/if_var.h>
66 #include <net/ifq_var.h>
67 #include <net/radix.h>
68 #include <net/route.h>
69 #include <machine/stdarg.h>
71 #include <sys/thread2.h>
73 #if defined(INET) || defined(INET6)
75 #include <netinet/in.h>
76 #include <netinet/in_var.h>
77 #include <netinet/if_ether.h>
79 #include <machine/clock.h> /* XXX: temporal workaround for fxp issue */
80 #include <netinet6/in6_var.h>
81 #include <netinet6/in6_ifattach.h>
85 #if defined(COMPAT_43)
86 #include <emulation/43bsd/43bsd_socket.h>
87 #endif /* COMPAT_43 */
90 * Support for non-ALTQ interfaces.
92 static int ifq_classic_enqueue(struct ifaltq *, struct mbuf *,
93 struct altq_pktattr *);
95 ifq_classic_dequeue(struct ifaltq *, struct mbuf *, int);
96 static int ifq_classic_request(struct ifaltq *, int, void *);
99 * System initialization
102 static void if_attachdomain(void *);
103 static void if_attachdomain1(struct ifnet *);
104 static int ifconf (u_long, caddr_t, struct ucred *);
105 static void ifinit (void *);
106 static void if_slowtimo (void *);
107 static void link_rtrequest (int, struct rtentry *, struct rt_addrinfo *);
108 static int if_rtdel (struct radix_node *, void *);
110 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
112 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
113 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
114 MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
116 int ifqmaxlen = IFQ_MAXLEN;
117 struct ifnethead ifnet; /* depend on static init XXX */
121 * XXX: declare here to avoid to include many inet6 related files..
122 * should be more generalized?
124 extern void nd6_setmtu (struct ifnet *);
127 struct if_clone *if_clone_lookup (const char *, int *);
128 int if_clone_list (struct if_clonereq *);
130 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
131 int if_cloners_count;
133 struct callout if_slowtimo_timer;
136 * Network interface utility routines.
138 * Routines with ifa_ifwith* names take sockaddr *'s as
147 callout_init(&if_slowtimo_timer);
150 TAILQ_FOREACH(ifp, &ifnet, if_link) {
151 if (ifp->if_snd.ifq_maxlen == 0) {
152 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
153 ifp->if_snd.ifq_maxlen = ifqmaxlen;
162 struct ifnet **ifindex2ifnet = NULL;
165 * Attach an interface to the list of "active" interfaces.
167 * The serializer is optional. If non-NULL access to the interface
171 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
173 unsigned socksize, ifasize;
174 int namelen, masklen;
175 struct sockaddr_dl *sdl;
179 static int if_indexlim = 8;
180 static boolean_t inited;
188 * The serializer can be passed in from the device, allowing the
189 * same serializer to be used for both the interrupt interlock and
190 * the device queue. If not specified, the netif structure will
191 * use an embedded serializer.
193 if (serializer == NULL) {
194 serializer = &ifp->if_default_serializer;
195 lwkt_serialize_init(serializer);
197 ifp->if_serializer = serializer;
199 #ifdef DEVICE_POLLING
200 /* Device is not in polling mode by default */
201 ifp->if_poll_cpuid = -1;
204 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
205 ifp->if_index = ++if_index;
208 * The old code would work if the interface passed a pre-existing
209 * chain of ifaddrs to this code. We don't trust our callers to
210 * properly initialize the tailq, however, so we no longer allow
211 * this unlikely case.
213 TAILQ_INIT(&ifp->if_addrhead);
214 TAILQ_INIT(&ifp->if_prefixhead);
215 LIST_INIT(&ifp->if_multiaddrs);
216 getmicrotime(&ifp->if_lastchange);
217 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
223 /* grow ifindex2ifnet */
224 n = if_indexlim * sizeof(*q);
225 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
227 bcopy(ifindex2ifnet, q, n/2);
228 kfree(ifindex2ifnet, M_IFADDR);
233 ifindex2ifnet[if_index] = ifp;
236 * create a Link Level name for this device
238 namelen = strlen(ifp->if_xname);
239 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
240 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
241 socksize = masklen + ifp->if_addrlen;
242 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
243 if (socksize < sizeof(*sdl))
244 socksize = sizeof(*sdl);
245 socksize = ROUNDUP(socksize);
246 ifasize = sizeof(struct ifaddr) + 2 * socksize;
247 ifa = kmalloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
248 sdl = (struct sockaddr_dl *)(ifa + 1);
249 sdl->sdl_len = socksize;
250 sdl->sdl_family = AF_LINK;
251 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
252 sdl->sdl_nlen = namelen;
253 sdl->sdl_index = ifp->if_index;
254 sdl->sdl_type = ifp->if_type;
255 ifp->if_lladdr = ifa;
257 ifa->ifa_rtrequest = link_rtrequest;
258 ifa->ifa_addr = (struct sockaddr *)sdl;
259 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
260 ifa->ifa_netmask = (struct sockaddr *)sdl;
261 sdl->sdl_len = masklen;
263 sdl->sdl_data[--namelen] = 0xff;
264 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
266 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
270 ifq->altq_disc = NULL;
271 ifq->altq_flags &= ALTQF_CANTCHANGE;
272 ifq->altq_tbr = NULL;
274 ifq_set_classic(ifq);
276 if (!SLIST_EMPTY(&domains))
277 if_attachdomain1(ifp);
279 /* Announce the interface. */
280 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
284 if_attachdomain(void *dummy)
289 TAILQ_FOREACH(ifp, &ifnet, if_list)
290 if_attachdomain1(ifp);
293 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
294 if_attachdomain, NULL);
297 if_attachdomain1(struct ifnet *ifp)
303 /* address family dependent data region */
304 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
305 SLIST_FOREACH(dp, &domains, dom_next)
306 if (dp->dom_ifattach)
307 ifp->if_afdata[dp->dom_family] =
308 (*dp->dom_ifattach)(ifp);
313 * Detach an interface, removing it from the
314 * list of "active" interfaces.
317 if_detach(struct ifnet *ifp)
320 struct radix_node_head *rnh;
325 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
328 * Remove routes and flush queues.
331 #ifdef DEVICE_POLLING
332 if (ifp->if_flags & IFF_POLLING)
333 ether_poll_deregister(ifp);
337 if (ifq_is_enabled(&ifp->if_snd))
338 altq_disable(&ifp->if_snd);
339 if (ifq_is_attached(&ifp->if_snd))
340 altq_detach(&ifp->if_snd);
343 * Clean up all addresses.
345 ifp->if_lladdr = NULL;
347 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
348 ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
350 /* XXX: Ugly!! ad hoc just for INET */
351 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
352 struct ifaliasreq ifr;
354 bzero(&ifr, sizeof ifr);
355 ifr.ifra_addr = *ifa->ifa_addr;
356 if (ifa->ifa_dstaddr)
357 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
358 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
364 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
366 /* ifp_addrhead is already updated */
370 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
376 * Remove all IPv4 kernel structures related to ifp.
383 * Remove all IPv6 kernel structs related to ifp. This should be done
384 * before removing routing entries below, since IPv6 interface direct
385 * routes are expected to be removed by the IPv6-specific kernel API.
386 * Otherwise, the kernel will detect some inconsistency and bark it.
392 * Delete all remaining routes using this interface
393 * Unfortuneatly the only way to do this is to slog through
394 * the entire routing table looking for routes which point
395 * to this interface...oh well...
398 for (cpu = 0; cpu < ncpus2; cpu++) {
399 lwkt_migratecpu(cpu);
400 for (i = 1; i <= AF_MAX; i++) {
401 if ((rnh = rt_tables[mycpuid][i]) == NULL)
403 rnh->rnh_walktree(rnh, if_rtdel, ifp);
406 lwkt_migratecpu(origcpu);
408 /* Announce that the interface is gone. */
409 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
411 SLIST_FOREACH(dp, &domains, dom_next)
412 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
413 (*dp->dom_ifdetach)(ifp,
414 ifp->if_afdata[dp->dom_family]);
417 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
419 ifindex2ifnet[ifp->if_index] = NULL;
420 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
423 TAILQ_REMOVE(&ifnet, ifp, if_link);
428 * Delete Routes for a Network Interface
430 * Called for each routing entry via the rnh->rnh_walktree() call above
431 * to delete all route entries referencing a detaching network interface.
434 * rn pointer to node in the routing table
435 * arg argument passed to rnh->rnh_walktree() - detaching interface
439 * errno failed - reason indicated
443 if_rtdel(struct radix_node *rn, void *arg)
445 struct rtentry *rt = (struct rtentry *)rn;
446 struct ifnet *ifp = arg;
449 if (rt->rt_ifp == ifp) {
452 * Protect (sorta) against walktree recursion problems
455 if (!(rt->rt_flags & RTF_UP))
458 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
459 rt_mask(rt), rt->rt_flags,
460 (struct rtentry **) NULL);
462 log(LOG_WARNING, "if_rtdel: error %d\n", err);
470 * Create a clone network interface.
473 if_clone_create(char *name, int len)
475 struct if_clone *ifc;
477 int wildcard, bytoff, bitoff;
481 ifc = if_clone_lookup(name, &unit);
485 if (ifunit(name) != NULL)
489 wildcard = (unit < 0);
491 * Find a free unit if none was given.
494 while (bytoff < ifc->ifc_bmlen &&
495 ifc->ifc_units[bytoff] == 0xff)
497 if (bytoff >= ifc->ifc_bmlen)
499 while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
501 unit = (bytoff << 3) + bitoff;
504 if (unit > ifc->ifc_maxunit)
507 err = (*ifc->ifc_create)(ifc, unit);
513 bitoff = unit - (bytoff << 3);
517 * Allocate the unit in the bitmap.
519 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
520 ("%s: bit is already set", __func__));
521 ifc->ifc_units[bytoff] |= (1 << bitoff);
523 /* In the wildcard case, we need to update the name. */
525 for (dp = name; *dp != '\0'; dp++);
526 if (ksnprintf(dp, len - (dp-name), "%d", unit) >
527 len - (dp-name) - 1) {
529 * This can only be a programmer error and
530 * there's no straightforward way to recover if
533 panic("if_clone_create(): interface name too long");
538 EVENTHANDLER_INVOKE(if_clone_event, ifc);
544 * Destroy a clone network interface.
547 if_clone_destroy(const char *name)
549 struct if_clone *ifc;
554 ifc = if_clone_lookup(name, &unit);
558 if (unit < ifc->ifc_minifs)
565 if (ifc->ifc_destroy == NULL)
568 (*ifc->ifc_destroy)(ifp);
571 * Compute offset in the bitmap and deallocate the unit.
574 bitoff = unit - (bytoff << 3);
575 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
576 ("%s: bit is already cleared", __func__));
577 ifc->ifc_units[bytoff] &= ~(1 << bitoff);
582 * Look up a network interface cloner.
585 if_clone_lookup(const char *name, int *unitp)
587 struct if_clone *ifc;
591 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
592 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
593 if (ifc->ifc_name[i] != *cp)
598 ifc = LIST_NEXT(ifc, ifc_list);
602 return ((struct if_clone *)NULL);
608 for (i = 0; *cp != '\0'; cp++) {
609 if (*cp < '0' || *cp > '9') {
610 /* Bogus unit number. */
613 i = (i * 10) + (*cp - '0');
623 * Register a network interface cloner.
626 if_clone_attach(struct if_clone *ifc)
633 KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
634 ("%s: %s requested more units then allowed (%d > %d)",
635 __func__, ifc->ifc_name, ifc->ifc_minifs,
636 ifc->ifc_maxunit + 1));
638 * Compute bitmap size and allocate it.
640 maxclone = ifc->ifc_maxunit + 1;
642 if ((len << 3) < maxclone)
644 ifc->ifc_units = kmalloc(len, M_CLONE, M_WAITOK | M_ZERO);
645 ifc->ifc_bmlen = len;
647 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
650 for (unit = 0; unit < ifc->ifc_minifs; unit++) {
651 err = (*ifc->ifc_create)(ifc, unit);
653 ("%s: failed to create required interface %s%d",
654 __func__, ifc->ifc_name, unit));
656 /* Allocate the unit in the bitmap. */
658 bitoff = unit - (bytoff << 3);
659 ifc->ifc_units[bytoff] |= (1 << bitoff);
664 * Unregister a network interface cloner.
667 if_clone_detach(struct if_clone *ifc)
670 LIST_REMOVE(ifc, ifc_list);
671 kfree(ifc->ifc_units, M_CLONE);
676 * Provide list of interface cloners to userspace.
679 if_clone_list(struct if_clonereq *ifcr)
681 char outbuf[IFNAMSIZ], *dst;
682 struct if_clone *ifc;
683 int count, error = 0;
685 ifcr->ifcr_total = if_cloners_count;
686 if ((dst = ifcr->ifcr_buffer) == NULL) {
687 /* Just asking how many there are. */
691 if (ifcr->ifcr_count < 0)
694 count = (if_cloners_count < ifcr->ifcr_count) ?
695 if_cloners_count : ifcr->ifcr_count;
697 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
698 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
699 strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
700 error = copyout(outbuf, dst, IFNAMSIZ);
709 * Locate an interface based on a complete address.
712 ifa_ifwithaddr(struct sockaddr *addr)
717 TAILQ_FOREACH(ifp, &ifnet, if_link)
718 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
719 if (ifa->ifa_addr->sa_family != addr->sa_family)
721 if (sa_equal(addr, ifa->ifa_addr))
723 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
724 /* IPv6 doesn't have broadcast */
725 ifa->ifa_broadaddr->sa_len != 0 &&
726 sa_equal(ifa->ifa_broadaddr, addr))
729 return ((struct ifaddr *)NULL);
732 * Locate the point to point interface with a given destination address.
735 ifa_ifwithdstaddr(struct sockaddr *addr)
740 TAILQ_FOREACH(ifp, &ifnet, if_link)
741 if (ifp->if_flags & IFF_POINTOPOINT)
742 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
743 if (ifa->ifa_addr->sa_family != addr->sa_family)
745 if (ifa->ifa_dstaddr &&
746 sa_equal(addr, ifa->ifa_dstaddr))
749 return ((struct ifaddr *)NULL);
753 * Find an interface on a specific network. If many, choice
754 * is most specific found.
757 ifa_ifwithnet(struct sockaddr *addr)
761 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
762 u_int af = addr->sa_family;
763 char *addr_data = addr->sa_data, *cplim;
766 * AF_LINK addresses can be looked up directly by their index number,
767 * so do that if we can.
770 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
772 if (sdl->sdl_index && sdl->sdl_index <= if_index)
773 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
777 * Scan though each interface, looking for ones that have
778 * addresses in this address family.
780 TAILQ_FOREACH(ifp, &ifnet, if_link) {
781 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
782 char *cp, *cp2, *cp3;
784 if (ifa->ifa_addr->sa_family != af)
786 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
788 * This is a bit broken as it doesn't
789 * take into account that the remote end may
790 * be a single node in the network we are
792 * The trouble is that we don't know the
793 * netmask for the remote end.
795 if (ifa->ifa_dstaddr != NULL &&
796 sa_equal(addr, ifa->ifa_dstaddr))
800 * if we have a special address handler,
801 * then use it instead of the generic one.
803 if (ifa->ifa_claim_addr) {
804 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
812 * Scan all the bits in the ifa's address.
813 * If a bit dissagrees with what we are
814 * looking for, mask it with the netmask
815 * to see if it really matters.
818 if (ifa->ifa_netmask == 0)
821 cp2 = ifa->ifa_addr->sa_data;
822 cp3 = ifa->ifa_netmask->sa_data;
823 cplim = ifa->ifa_netmask->sa_len +
824 (char *)ifa->ifa_netmask;
826 if ((*cp++ ^ *cp2++) & *cp3++)
827 goto next; /* next address! */
829 * If the netmask of what we just found
830 * is more specific than what we had before
831 * (if we had one) then remember the new one
832 * before continuing to search
833 * for an even better one.
835 if (ifa_maybe == 0 ||
836 rn_refines((char *)ifa->ifa_netmask,
837 (char *)ifa_maybe->ifa_netmask))
846 * Find an interface address specific to an interface best matching
850 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
853 char *cp, *cp2, *cp3;
855 struct ifaddr *ifa_maybe = 0;
856 u_int af = addr->sa_family;
860 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
861 if (ifa->ifa_addr->sa_family != af)
865 if (ifa->ifa_netmask == NULL) {
866 if (sa_equal(addr, ifa->ifa_addr) ||
867 (ifa->ifa_dstaddr != NULL &&
868 sa_equal(addr, ifa->ifa_dstaddr)))
872 if (ifp->if_flags & IFF_POINTOPOINT) {
873 if (sa_equal(addr, ifa->ifa_dstaddr))
877 cp2 = ifa->ifa_addr->sa_data;
878 cp3 = ifa->ifa_netmask->sa_data;
879 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
880 for (; cp3 < cplim; cp3++)
881 if ((*cp++ ^ *cp2++) & *cp3)
890 #include <net/route.h>
893 * Default action when installing a route with a Link Level gateway.
894 * Lookup an appropriate real ifa to point to.
895 * This should be moved to /sys/net/link.c eventually.
898 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
901 struct sockaddr *dst;
904 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
905 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
907 ifa = ifaof_ifpforaddr(dst, ifp);
912 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
913 ifa->ifa_rtrequest(cmd, rt, info);
918 * Mark an interface down and notify protocols of
920 * NOTE: must be called at splnet or eqivalent.
923 if_unroute(struct ifnet *ifp, int flag, int fam)
927 ifp->if_flags &= ~flag;
928 getmicrotime(&ifp->if_lastchange);
929 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
930 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
931 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
932 ifq_purge(&ifp->if_snd);
937 * Mark an interface up and notify protocols of
939 * NOTE: must be called at splnet or eqivalent.
942 if_route(struct ifnet *ifp, int flag, int fam)
946 ifp->if_flags |= flag;
947 getmicrotime(&ifp->if_lastchange);
948 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
949 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
950 pfctlinput(PRC_IFUP, ifa->ifa_addr);
958 * Mark an interface down and notify protocols of the transition. An
959 * interface going down is also considered to be a synchronizing event.
960 * We must ensure that all packet processing related to the interface
961 * has completed before we return so e.g. the caller can free the ifnet
962 * structure that the mbufs may be referencing.
964 * NOTE: must be called at splnet or eqivalent.
967 if_down(struct ifnet *ifp)
969 if_unroute(ifp, IFF_UP, AF_UNSPEC);
970 netmsg_service_sync();
974 * Mark an interface up and notify protocols of
976 * NOTE: must be called at splnet or eqivalent.
979 if_up(struct ifnet *ifp)
982 if_route(ifp, IFF_UP, AF_UNSPEC);
986 * Process a link state change.
987 * NOTE: must be called at splsoftnet or equivalent.
990 if_link_state_change(struct ifnet *ifp)
996 * Handle interface watchdog timer routines. Called
997 * from softclock, we decrement timers (if set) and
998 * call the appropriate interface routine on expiration.
1001 if_slowtimo(void *arg)
1007 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1008 if (ifp->if_timer == 0 || --ifp->if_timer)
1010 if (ifp->if_watchdog) {
1011 if (lwkt_serialize_try(ifp->if_serializer)) {
1012 (*ifp->if_watchdog)(ifp);
1013 lwkt_serialize_exit(ifp->if_serializer);
1015 /* try again next timeout */
1023 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1027 * Map interface name to
1028 * interface structure pointer.
1031 ifunit(const char *name)
1036 * Search all the interfaces for this name/number
1039 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1040 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1048 * Map interface name in a sockaddr_dl to
1049 * interface structure pointer.
1052 if_withname(struct sockaddr *sa)
1054 char ifname[IFNAMSIZ+1];
1055 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1057 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1058 (sdl->sdl_nlen > IFNAMSIZ) )
1062 * ifunit wants a null-terminated name. It may not be null-terminated
1063 * in the sockaddr. We don't want to change the caller's sockaddr,
1064 * and there might not be room to put the trailing null anyway, so we
1065 * make a local copy that we know we can null terminate safely.
1068 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1069 ifname[sdl->sdl_nlen] = '\0';
1070 return ifunit(ifname);
1078 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1086 size_t namelen, onamelen;
1087 char new_name[IFNAMSIZ];
1089 struct sockaddr_dl *sdl;
1095 return (ifconf(cmd, data, cred));
1097 ifr = (struct ifreq *)data;
1102 if ((error = suser_cred(cred, 0)) != 0)
1104 return ((cmd == SIOCIFCREATE) ?
1105 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1106 if_clone_destroy(ifr->ifr_name));
1108 case SIOCIFGCLONERS:
1109 return (if_clone_list((struct if_clonereq *)data));
1112 ifp = ifunit(ifr->ifr_name);
1118 ifr->ifr_flags = ifp->if_flags;
1119 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1123 ifr->ifr_reqcap = ifp->if_capabilities;
1124 ifr->ifr_curcap = ifp->if_capenable;
1128 ifr->ifr_metric = ifp->if_metric;
1132 ifr->ifr_mtu = ifp->if_mtu;
1136 ifr->ifr_phys = ifp->if_physical;
1139 case SIOCGIFPOLLCPU:
1140 #ifdef DEVICE_POLLING
1141 ifr->ifr_pollcpu = ifp->if_poll_cpuid;
1143 ifr->ifr_pollcpu = -1;
1147 case SIOCSIFPOLLCPU:
1148 #ifdef DEVICE_POLLING
1149 if ((ifp->if_flags & IFF_POLLING) == 0)
1150 ether_pollcpu_register(ifp, ifr->ifr_pollcpu);
1155 error = suser_cred(cred, 0);
1158 new_flags = (ifr->ifr_flags & 0xffff) |
1159 (ifr->ifr_flagshigh << 16);
1160 if (ifp->if_flags & IFF_SMART) {
1161 /* Smart drivers twiddle their own routes */
1162 } else if (ifp->if_flags & IFF_UP &&
1163 (new_flags & IFF_UP) == 0) {
1167 } else if (new_flags & IFF_UP &&
1168 (ifp->if_flags & IFF_UP) == 0) {
1174 #ifdef DEVICE_POLLING
1175 if ((new_flags ^ ifp->if_flags) & IFF_POLLING) {
1176 if (new_flags & IFF_POLLING) {
1177 ether_poll_register(ifp);
1179 ether_poll_deregister(ifp);
1184 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1185 (new_flags &~ IFF_CANTCHANGE);
1186 if (new_flags & IFF_PPROMISC) {
1187 /* Permanently promiscuous mode requested */
1188 ifp->if_flags |= IFF_PROMISC;
1189 } else if (ifp->if_pcount == 0) {
1190 ifp->if_flags &= ~IFF_PROMISC;
1192 if (ifp->if_ioctl) {
1193 lwkt_serialize_enter(ifp->if_serializer);
1194 ifp->if_ioctl(ifp, cmd, data, cred);
1195 lwkt_serialize_exit(ifp->if_serializer);
1197 getmicrotime(&ifp->if_lastchange);
1201 error = suser_cred(cred, 0);
1204 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1206 lwkt_serialize_enter(ifp->if_serializer);
1207 ifp->if_ioctl(ifp, cmd, data, cred);
1208 lwkt_serialize_exit(ifp->if_serializer);
1212 error = suser_cred(cred, 0);
1215 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1218 if (new_name[0] == '\0')
1220 if (ifunit(new_name) != NULL)
1223 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1225 /* Announce the departure of the interface. */
1226 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1228 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1229 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1230 /* XXX IFA_LOCK(ifa); */
1231 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1232 namelen = strlen(new_name);
1233 onamelen = sdl->sdl_nlen;
1235 * Move the address if needed. This is safe because we
1236 * allocate space for a name of length IFNAMSIZ when we
1237 * create this in if_attach().
1239 if (namelen != onamelen) {
1240 bcopy(sdl->sdl_data + onamelen,
1241 sdl->sdl_data + namelen, sdl->sdl_alen);
1243 bcopy(new_name, sdl->sdl_data, namelen);
1244 sdl->sdl_nlen = namelen;
1245 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1246 bzero(sdl->sdl_data, onamelen);
1247 while (namelen != 0)
1248 sdl->sdl_data[--namelen] = 0xff;
1249 /* XXX IFA_UNLOCK(ifa) */
1251 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1253 /* Announce the return of the interface. */
1254 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1258 error = suser_cred(cred, 0);
1261 ifp->if_metric = ifr->ifr_metric;
1262 getmicrotime(&ifp->if_lastchange);
1266 error = suser_cred(cred, 0);
1271 lwkt_serialize_enter(ifp->if_serializer);
1272 error = ifp->if_ioctl(ifp, cmd, data, cred);
1273 lwkt_serialize_exit(ifp->if_serializer);
1275 getmicrotime(&ifp->if_lastchange);
1280 u_long oldmtu = ifp->if_mtu;
1282 error = suser_cred(cred, 0);
1285 if (ifp->if_ioctl == NULL)
1286 return (EOPNOTSUPP);
1287 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1289 lwkt_serialize_enter(ifp->if_serializer);
1290 error = ifp->if_ioctl(ifp, cmd, data, cred);
1291 lwkt_serialize_exit(ifp->if_serializer);
1293 getmicrotime(&ifp->if_lastchange);
1297 * If the link MTU changed, do network layer specific procedure.
1299 if (ifp->if_mtu != oldmtu) {
1309 error = suser_cred(cred, 0);
1313 /* Don't allow group membership on non-multicast interfaces. */
1314 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1317 /* Don't let users screw up protocols' entries. */
1318 if (ifr->ifr_addr.sa_family != AF_LINK)
1321 if (cmd == SIOCADDMULTI) {
1322 struct ifmultiaddr *ifma;
1323 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1325 error = if_delmulti(ifp, &ifr->ifr_addr);
1328 getmicrotime(&ifp->if_lastchange);
1331 case SIOCSIFPHYADDR:
1332 case SIOCDIFPHYADDR:
1334 case SIOCSIFPHYADDR_IN6:
1336 case SIOCSLIFPHYADDR:
1338 case SIOCSIFGENERIC:
1339 error = suser_cred(cred, 0);
1342 if (ifp->if_ioctl == 0)
1343 return (EOPNOTSUPP);
1344 lwkt_serialize_enter(ifp->if_serializer);
1345 error = ifp->if_ioctl(ifp, cmd, data, cred);
1346 lwkt_serialize_exit(ifp->if_serializer);
1348 getmicrotime(&ifp->if_lastchange);
1352 ifs = (struct ifstat *)data;
1353 ifs->ascii[0] = '\0';
1355 case SIOCGIFPSRCADDR:
1356 case SIOCGIFPDSTADDR:
1357 case SIOCGLIFPHYADDR:
1359 case SIOCGIFGENERIC:
1360 if (ifp->if_ioctl == NULL)
1361 return (EOPNOTSUPP);
1362 lwkt_serialize_enter(ifp->if_serializer);
1363 error = ifp->if_ioctl(ifp, cmd, data, cred);
1364 lwkt_serialize_exit(ifp->if_serializer);
1368 error = suser_cred(cred, 0);
1371 return if_setlladdr(ifp,
1372 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1375 oif_flags = ifp->if_flags;
1376 if (so->so_proto == 0)
1377 return (EOPNOTSUPP);
1379 error = so_pru_control(so, cmd, data, ifp);
1386 case SIOCSIFDSTADDR:
1388 case SIOCSIFBRDADDR:
1389 case SIOCSIFNETMASK:
1390 #if BYTE_ORDER != BIG_ENDIAN
1391 if (ifr->ifr_addr.sa_family == 0 &&
1392 ifr->ifr_addr.sa_len < 16) {
1393 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1394 ifr->ifr_addr.sa_len = 16;
1397 if (ifr->ifr_addr.sa_len == 0)
1398 ifr->ifr_addr.sa_len = 16;
1406 case OSIOCGIFDSTADDR:
1407 cmd = SIOCGIFDSTADDR;
1410 case OSIOCGIFBRDADDR:
1411 cmd = SIOCGIFBRDADDR;
1414 case OSIOCGIFNETMASK:
1415 cmd = SIOCGIFNETMASK;
1417 error = so_pru_control(so, cmd, data, ifp);
1421 case OSIOCGIFDSTADDR:
1422 case OSIOCGIFBRDADDR:
1423 case OSIOCGIFNETMASK:
1424 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1428 #endif /* COMPAT_43 */
1430 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1432 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1433 if (ifp->if_flags & IFF_UP) {
1447 * Set/clear promiscuous mode on interface ifp based on the truth value
1448 * of pswitch. The calls are reference counted so that only the first
1449 * "on" request actually has an effect, as does the final "off" request.
1450 * Results are undefined if the "off" and "on" requests are not matched.
1453 ifpromisc(struct ifnet *ifp, int pswitch)
1459 oldflags = ifp->if_flags;
1460 if (ifp->if_flags & IFF_PPROMISC) {
1461 /* Do nothing if device is in permanently promiscuous mode */
1462 ifp->if_pcount += pswitch ? 1 : -1;
1467 * If the device is not configured up, we cannot put it in
1470 if ((ifp->if_flags & IFF_UP) == 0)
1472 if (ifp->if_pcount++ != 0)
1474 ifp->if_flags |= IFF_PROMISC;
1475 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1478 if (--ifp->if_pcount > 0)
1480 ifp->if_flags &= ~IFF_PROMISC;
1481 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1484 ifr.ifr_flags = ifp->if_flags;
1485 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1486 lwkt_serialize_enter(ifp->if_serializer);
1487 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1488 (struct ucred *)NULL);
1489 lwkt_serialize_exit(ifp->if_serializer);
1493 ifp->if_flags = oldflags;
1498 * Return interface configuration
1499 * of system. List may be used
1500 * in later ioctl's (above) to get
1501 * other information.
1504 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
1506 struct ifconf *ifc = (struct ifconf *)data;
1509 struct sockaddr *sa;
1510 struct ifreq ifr, *ifrp;
1511 int space = ifc->ifc_len, error = 0;
1513 ifrp = ifc->ifc_req;
1514 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1517 if (space <= sizeof ifr)
1521 * Zero the stack declared structure first to prevent
1522 * memory disclosure.
1524 bzero(&ifr, sizeof(ifr));
1525 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1526 >= sizeof(ifr.ifr_name)) {
1527 error = ENAMETOOLONG;
1532 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1533 if (space <= sizeof ifr)
1536 if (cred->cr_prison &&
1537 prison_if(cred, sa))
1541 if (cmd == OSIOCGIFCONF) {
1542 struct osockaddr *osa =
1543 (struct osockaddr *)&ifr.ifr_addr;
1545 osa->sa_family = sa->sa_family;
1546 error = copyout(&ifr, ifrp, sizeof ifr);
1550 if (sa->sa_len <= sizeof(*sa)) {
1552 error = copyout(&ifr, ifrp, sizeof ifr);
1555 if (space < (sizeof ifr) + sa->sa_len -
1558 space -= sa->sa_len - sizeof(*sa);
1559 error = copyout(&ifr, ifrp,
1560 sizeof ifr.ifr_name);
1562 error = copyout(sa, &ifrp->ifr_addr,
1564 ifrp = (struct ifreq *)
1565 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1569 space -= sizeof ifr;
1574 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
1575 error = copyout(&ifr, ifrp, sizeof ifr);
1578 space -= sizeof ifr;
1582 ifc->ifc_len -= space;
1587 * Just like if_promisc(), but for all-multicast-reception mode.
1590 if_allmulti(struct ifnet *ifp, int onswitch)
1598 if (ifp->if_amcount++ == 0) {
1599 ifp->if_flags |= IFF_ALLMULTI;
1600 ifr.ifr_flags = ifp->if_flags;
1601 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1602 lwkt_serialize_enter(ifp->if_serializer);
1603 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1604 (struct ucred *)NULL);
1605 lwkt_serialize_exit(ifp->if_serializer);
1608 if (ifp->if_amcount > 1) {
1611 ifp->if_amcount = 0;
1612 ifp->if_flags &= ~IFF_ALLMULTI;
1613 ifr.ifr_flags = ifp->if_flags;
1614 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1615 lwkt_serialize_enter(ifp->if_serializer);
1616 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1617 (struct ucred *)NULL);
1618 lwkt_serialize_exit(ifp->if_serializer);
1630 * Add a multicast listenership to the interface in question.
1631 * The link layer provides a routine which converts
1635 struct ifnet *ifp, /* interface to manipulate */
1636 struct sockaddr *sa, /* address to add */
1637 struct ifmultiaddr **retifma)
1639 struct sockaddr *llsa, *dupsa;
1641 struct ifmultiaddr *ifma;
1644 * If the matching multicast address already exists
1645 * then don't add a new one, just add a reference
1647 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1648 if (sa_equal(sa, ifma->ifma_addr)) {
1649 ifma->ifma_refcount++;
1657 * Give the link layer a chance to accept/reject it, and also
1658 * find out which AF_LINK address this maps to, if it isn't one
1661 if (ifp->if_resolvemulti) {
1662 lwkt_serialize_enter(ifp->if_serializer);
1663 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1664 lwkt_serialize_exit(ifp->if_serializer);
1671 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1672 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1673 bcopy(sa, dupsa, sa->sa_len);
1675 ifma->ifma_addr = dupsa;
1676 ifma->ifma_lladdr = llsa;
1677 ifma->ifma_ifp = ifp;
1678 ifma->ifma_refcount = 1;
1679 ifma->ifma_protospec = 0;
1680 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1683 * Some network interfaces can scan the address list at
1684 * interrupt time; lock them out.
1687 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1692 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1693 if (sa_equal(ifma->ifma_addr, llsa))
1697 ifma->ifma_refcount++;
1699 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1700 M_IFMADDR, M_WAITOK);
1701 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1702 M_IFMADDR, M_WAITOK);
1703 bcopy(llsa, dupsa, llsa->sa_len);
1704 ifma->ifma_addr = dupsa;
1705 ifma->ifma_ifp = ifp;
1706 ifma->ifma_refcount = 1;
1708 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1713 * We are certain we have added something, so call down to the
1714 * interface to let them know about it.
1717 lwkt_serialize_enter(ifp->if_serializer);
1718 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, (struct ucred *)NULL);
1719 lwkt_serialize_exit(ifp->if_serializer);
1726 * Remove a reference to a multicast address on this interface. Yell
1727 * if the request does not match an existing membership.
1730 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
1732 struct ifmultiaddr *ifma;
1734 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1735 if (sa_equal(sa, ifma->ifma_addr))
1740 if (ifma->ifma_refcount > 1) {
1741 ifma->ifma_refcount--;
1745 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1746 sa = ifma->ifma_lladdr;
1748 LIST_REMOVE(ifma, ifma_link);
1750 * Make sure the interface driver is notified
1751 * in the case of a link layer mcast group being left.
1753 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) {
1754 lwkt_serialize_enter(ifp->if_serializer);
1755 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1756 lwkt_serialize_exit(ifp->if_serializer);
1759 kfree(ifma->ifma_addr, M_IFMADDR);
1760 kfree(ifma, M_IFMADDR);
1765 * Now look for the link-layer address which corresponds to
1766 * this network address. It had been squirreled away in
1767 * ifma->ifma_lladdr for this purpose (so we don't have
1768 * to call ifp->if_resolvemulti() again), and we saved that
1769 * value in sa above. If some nasty deleted the
1770 * link-layer address out from underneath us, we can deal because
1771 * the address we stored was is not the same as the one which was
1772 * in the record for the link-layer address. (So we don't complain
1775 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1776 if (sa_equal(sa, ifma->ifma_addr))
1781 if (ifma->ifma_refcount > 1) {
1782 ifma->ifma_refcount--;
1787 lwkt_serialize_enter(ifp->if_serializer);
1788 LIST_REMOVE(ifma, ifma_link);
1789 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1790 lwkt_serialize_exit(ifp->if_serializer);
1792 kfree(ifma->ifma_addr, M_IFMADDR);
1793 kfree(sa, M_IFMADDR);
1794 kfree(ifma, M_IFMADDR);
1800 * Set the link layer address on an interface.
1802 * At this time we only support certain types of interfaces,
1803 * and we don't allow the length of the address to change.
1806 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1808 struct sockaddr_dl *sdl;
1812 sdl = IF_LLSOCKADDR(ifp);
1815 if (len != sdl->sdl_alen) /* don't allow length to change */
1817 switch (ifp->if_type) {
1818 case IFT_ETHER: /* these types use struct arpcom */
1821 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1822 bcopy(lladdr, LLADDR(sdl), len);
1828 * If the interface is already up, we need
1829 * to re-init it in order to reprogram its
1832 lwkt_serialize_enter(ifp->if_serializer);
1833 if ((ifp->if_flags & IFF_UP) != 0) {
1834 ifp->if_flags &= ~IFF_UP;
1835 ifr.ifr_flags = ifp->if_flags;
1836 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1837 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1838 (struct ucred *)NULL);
1839 ifp->if_flags |= IFF_UP;
1840 ifr.ifr_flags = ifp->if_flags;
1841 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1842 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1843 (struct ucred *)NULL);
1846 * Also send gratuitous ARPs to notify other nodes about
1847 * the address change.
1849 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1850 if (ifa->ifa_addr != NULL &&
1851 ifa->ifa_addr->sa_family == AF_INET)
1852 arp_ifinit(ifp, ifa);
1856 lwkt_serialize_exit(ifp->if_serializer);
1860 struct ifmultiaddr *
1861 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
1863 struct ifmultiaddr *ifma;
1865 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1866 if (sa_equal(ifma->ifma_addr, sa))
1873 * This function locates the first real ethernet MAC from a network
1874 * card and loads it into node, returning 0 on success or ENOENT if
1875 * no suitable interfaces were found. It is used by the uuid code to
1876 * generate a unique 6-byte number.
1879 if_getanyethermac(uint16_t *node, int minlen)
1882 struct sockaddr_dl *sdl;
1884 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1885 if (ifp->if_type != IFT_ETHER)
1887 sdl = IF_LLSOCKADDR(ifp);
1888 if (sdl->sdl_alen < minlen)
1890 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
1898 * The name argument must be a pointer to storage which will last as
1899 * long as the interface does. For physical devices, the result of
1900 * device_get_name(dev) is a good choice and for pseudo-devices a
1901 * static string works well.
1904 if_initname(struct ifnet *ifp, const char *name, int unit)
1906 ifp->if_dname = name;
1907 ifp->if_dunit = unit;
1908 if (unit != IF_DUNIT_NONE)
1909 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
1911 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1915 if_printf(struct ifnet *ifp, const char *fmt, ...)
1920 retval = kprintf("%s: ", ifp->if_xname);
1921 __va_start(ap, fmt);
1922 retval += kvprintf(fmt, ap);
1927 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1928 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
1931 ifq_set_classic(struct ifaltq *ifq)
1933 ifq->altq_enqueue = ifq_classic_enqueue;
1934 ifq->altq_dequeue = ifq_classic_dequeue;
1935 ifq->altq_request = ifq_classic_request;
1939 ifq_classic_enqueue(struct ifaltq *ifq, struct mbuf *m,
1940 struct altq_pktattr *pa __unused)
1943 if (IF_QFULL(ifq)) {
1954 static struct mbuf *
1955 ifq_classic_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
1968 panic("unsupported ALTQ dequeue op: %d", op);
1971 KKASSERT(mpolled == NULL || mpolled == m);
1976 ifq_classic_request(struct ifaltq *ifq, int req, void *arg)
1984 panic("unsupported ALTQ request: %d", req);