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.52 2007/06/16 19:59:30 dillon 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 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
200 ifp->if_index = ++if_index;
203 * The old code would work if the interface passed a pre-existing
204 * chain of ifaddrs to this code. We don't trust our callers to
205 * properly initialize the tailq, however, so we no longer allow
206 * this unlikely case.
208 TAILQ_INIT(&ifp->if_addrhead);
209 TAILQ_INIT(&ifp->if_prefixhead);
210 LIST_INIT(&ifp->if_multiaddrs);
211 getmicrotime(&ifp->if_lastchange);
212 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
218 /* grow ifindex2ifnet */
219 n = if_indexlim * sizeof(*q);
220 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
222 bcopy(ifindex2ifnet, q, n/2);
223 kfree(ifindex2ifnet, M_IFADDR);
228 ifindex2ifnet[if_index] = ifp;
231 * create a Link Level name for this device
233 namelen = strlen(ifp->if_xname);
234 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
235 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
236 socksize = masklen + ifp->if_addrlen;
237 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
238 if (socksize < sizeof(*sdl))
239 socksize = sizeof(*sdl);
240 socksize = ROUNDUP(socksize);
241 ifasize = sizeof(struct ifaddr) + 2 * socksize;
242 ifa = kmalloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
243 sdl = (struct sockaddr_dl *)(ifa + 1);
244 sdl->sdl_len = socksize;
245 sdl->sdl_family = AF_LINK;
246 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
247 sdl->sdl_nlen = namelen;
248 sdl->sdl_index = ifp->if_index;
249 sdl->sdl_type = ifp->if_type;
250 ifp->if_lladdr = ifa;
252 ifa->ifa_rtrequest = link_rtrequest;
253 ifa->ifa_addr = (struct sockaddr *)sdl;
254 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
255 ifa->ifa_netmask = (struct sockaddr *)sdl;
256 sdl->sdl_len = masklen;
258 sdl->sdl_data[--namelen] = 0xff;
259 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
261 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
265 ifq->altq_disc = NULL;
266 ifq->altq_flags &= ALTQF_CANTCHANGE;
267 ifq->altq_tbr = NULL;
269 ifq_set_classic(ifq);
271 if (!SLIST_EMPTY(&domains))
272 if_attachdomain1(ifp);
274 /* Announce the interface. */
275 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
279 if_attachdomain(void *dummy)
284 TAILQ_FOREACH(ifp, &ifnet, if_list)
285 if_attachdomain1(ifp);
288 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
289 if_attachdomain, NULL);
292 if_attachdomain1(struct ifnet *ifp)
298 /* address family dependent data region */
299 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
300 SLIST_FOREACH(dp, &domains, dom_next)
301 if (dp->dom_ifattach)
302 ifp->if_afdata[dp->dom_family] =
303 (*dp->dom_ifattach)(ifp);
308 * Detach an interface, removing it from the
309 * list of "active" interfaces.
312 if_detach(struct ifnet *ifp)
315 struct radix_node_head *rnh;
320 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
323 * Remove routes and flush queues.
326 #ifdef DEVICE_POLLING
327 if (ifp->if_flags & IFF_POLLING)
328 ether_poll_deregister(ifp);
332 if (ifq_is_enabled(&ifp->if_snd))
333 altq_disable(&ifp->if_snd);
334 if (ifq_is_attached(&ifp->if_snd))
335 altq_detach(&ifp->if_snd);
338 * Clean up all addresses.
340 ifp->if_lladdr = NULL;
342 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
343 ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
345 /* XXX: Ugly!! ad hoc just for INET */
346 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
347 struct ifaliasreq ifr;
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) {
361 /* ifp_addrhead is already updated */
365 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
371 * Remove all IPv4 kernel structures related to ifp.
378 * Remove all IPv6 kernel structs related to ifp. This should be done
379 * before removing routing entries below, since IPv6 interface direct
380 * routes are expected to be removed by the IPv6-specific kernel API.
381 * Otherwise, the kernel will detect some inconsistency and bark it.
387 * Delete all remaining routes using this interface
388 * Unfortuneatly the only way to do this is to slog through
389 * the entire routing table looking for routes which point
390 * to this interface...oh well...
393 for (cpu = 0; cpu < ncpus2; cpu++) {
394 lwkt_migratecpu(cpu);
395 for (i = 1; i <= AF_MAX; i++) {
396 if ((rnh = rt_tables[mycpuid][i]) == NULL)
398 rnh->rnh_walktree(rnh, if_rtdel, ifp);
401 lwkt_migratecpu(origcpu);
403 /* Announce that the interface is gone. */
404 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
406 SLIST_FOREACH(dp, &domains, dom_next)
407 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
408 (*dp->dom_ifdetach)(ifp,
409 ifp->if_afdata[dp->dom_family]);
412 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
414 ifindex2ifnet[ifp->if_index] = NULL;
415 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
418 TAILQ_REMOVE(&ifnet, ifp, if_link);
423 * Delete Routes for a Network Interface
425 * Called for each routing entry via the rnh->rnh_walktree() call above
426 * to delete all route entries referencing a detaching network interface.
429 * rn pointer to node in the routing table
430 * arg argument passed to rnh->rnh_walktree() - detaching interface
434 * errno failed - reason indicated
438 if_rtdel(struct radix_node *rn, void *arg)
440 struct rtentry *rt = (struct rtentry *)rn;
441 struct ifnet *ifp = arg;
444 if (rt->rt_ifp == ifp) {
447 * Protect (sorta) against walktree recursion problems
450 if (!(rt->rt_flags & RTF_UP))
453 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
454 rt_mask(rt), rt->rt_flags,
455 (struct rtentry **) NULL);
457 log(LOG_WARNING, "if_rtdel: error %d\n", err);
465 * Create a clone network interface.
468 if_clone_create(char *name, int len)
470 struct if_clone *ifc;
472 int wildcard, bytoff, bitoff;
476 ifc = if_clone_lookup(name, &unit);
480 if (ifunit(name) != NULL)
484 wildcard = (unit < 0);
486 * Find a free unit if none was given.
489 while (bytoff < ifc->ifc_bmlen &&
490 ifc->ifc_units[bytoff] == 0xff)
492 if (bytoff >= ifc->ifc_bmlen)
494 while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
496 unit = (bytoff << 3) + bitoff;
499 if (unit > ifc->ifc_maxunit)
502 err = (*ifc->ifc_create)(ifc, unit);
508 bitoff = unit - (bytoff << 3);
512 * Allocate the unit in the bitmap.
514 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
515 ("%s: bit is already set", __func__));
516 ifc->ifc_units[bytoff] |= (1 << bitoff);
518 /* In the wildcard case, we need to update the name. */
520 for (dp = name; *dp != '\0'; dp++);
521 if (ksnprintf(dp, len - (dp-name), "%d", unit) >
522 len - (dp-name) - 1) {
524 * This can only be a programmer error and
525 * there's no straightforward way to recover if
528 panic("if_clone_create(): interface name too long");
533 EVENTHANDLER_INVOKE(if_clone_event, ifc);
539 * Destroy a clone network interface.
542 if_clone_destroy(const char *name)
544 struct if_clone *ifc;
549 ifc = if_clone_lookup(name, &unit);
553 if (unit < ifc->ifc_minifs)
560 if (ifc->ifc_destroy == NULL)
563 (*ifc->ifc_destroy)(ifp);
566 * Compute offset in the bitmap and deallocate the unit.
569 bitoff = unit - (bytoff << 3);
570 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
571 ("%s: bit is already cleared", __func__));
572 ifc->ifc_units[bytoff] &= ~(1 << bitoff);
577 * Look up a network interface cloner.
580 if_clone_lookup(const char *name, int *unitp)
582 struct if_clone *ifc;
586 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
587 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
588 if (ifc->ifc_name[i] != *cp)
593 ifc = LIST_NEXT(ifc, ifc_list);
597 return ((struct if_clone *)NULL);
603 for (i = 0; *cp != '\0'; cp++) {
604 if (*cp < '0' || *cp > '9') {
605 /* Bogus unit number. */
608 i = (i * 10) + (*cp - '0');
618 * Register a network interface cloner.
621 if_clone_attach(struct if_clone *ifc)
628 KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
629 ("%s: %s requested more units then allowed (%d > %d)",
630 __func__, ifc->ifc_name, ifc->ifc_minifs,
631 ifc->ifc_maxunit + 1));
633 * Compute bitmap size and allocate it.
635 maxclone = ifc->ifc_maxunit + 1;
637 if ((len << 3) < maxclone)
639 ifc->ifc_units = kmalloc(len, M_CLONE, M_WAITOK | M_ZERO);
640 ifc->ifc_bmlen = len;
642 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
645 for (unit = 0; unit < ifc->ifc_minifs; unit++) {
646 err = (*ifc->ifc_create)(ifc, unit);
648 ("%s: failed to create required interface %s%d",
649 __func__, ifc->ifc_name, unit));
651 /* Allocate the unit in the bitmap. */
653 bitoff = unit - (bytoff << 3);
654 ifc->ifc_units[bytoff] |= (1 << bitoff);
659 * Unregister a network interface cloner.
662 if_clone_detach(struct if_clone *ifc)
665 LIST_REMOVE(ifc, ifc_list);
666 kfree(ifc->ifc_units, M_CLONE);
671 * Provide list of interface cloners to userspace.
674 if_clone_list(struct if_clonereq *ifcr)
676 char outbuf[IFNAMSIZ], *dst;
677 struct if_clone *ifc;
678 int count, error = 0;
680 ifcr->ifcr_total = if_cloners_count;
681 if ((dst = ifcr->ifcr_buffer) == NULL) {
682 /* Just asking how many there are. */
686 if (ifcr->ifcr_count < 0)
689 count = (if_cloners_count < ifcr->ifcr_count) ?
690 if_cloners_count : ifcr->ifcr_count;
692 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
693 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
694 strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
695 error = copyout(outbuf, dst, IFNAMSIZ);
704 * Locate an interface based on a complete address.
707 ifa_ifwithaddr(struct sockaddr *addr)
712 TAILQ_FOREACH(ifp, &ifnet, if_link)
713 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
714 if (ifa->ifa_addr->sa_family != addr->sa_family)
716 if (sa_equal(addr, ifa->ifa_addr))
718 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
719 /* IPv6 doesn't have broadcast */
720 ifa->ifa_broadaddr->sa_len != 0 &&
721 sa_equal(ifa->ifa_broadaddr, addr))
724 return ((struct ifaddr *)NULL);
727 * Locate the point to point interface with a given destination address.
730 ifa_ifwithdstaddr(struct sockaddr *addr)
735 TAILQ_FOREACH(ifp, &ifnet, if_link)
736 if (ifp->if_flags & IFF_POINTOPOINT)
737 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
738 if (ifa->ifa_addr->sa_family != addr->sa_family)
740 if (ifa->ifa_dstaddr &&
741 sa_equal(addr, ifa->ifa_dstaddr))
744 return ((struct ifaddr *)NULL);
748 * Find an interface on a specific network. If many, choice
749 * is most specific found.
752 ifa_ifwithnet(struct sockaddr *addr)
756 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
757 u_int af = addr->sa_family;
758 char *addr_data = addr->sa_data, *cplim;
761 * AF_LINK addresses can be looked up directly by their index number,
762 * so do that if we can.
765 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
767 if (sdl->sdl_index && sdl->sdl_index <= if_index)
768 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
772 * Scan though each interface, looking for ones that have
773 * addresses in this address family.
775 TAILQ_FOREACH(ifp, &ifnet, if_link) {
776 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
777 char *cp, *cp2, *cp3;
779 if (ifa->ifa_addr->sa_family != af)
781 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
783 * This is a bit broken as it doesn't
784 * take into account that the remote end may
785 * be a single node in the network we are
787 * The trouble is that we don't know the
788 * netmask for the remote end.
790 if (ifa->ifa_dstaddr != NULL &&
791 sa_equal(addr, ifa->ifa_dstaddr))
795 * if we have a special address handler,
796 * then use it instead of the generic one.
798 if (ifa->ifa_claim_addr) {
799 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
807 * Scan all the bits in the ifa's address.
808 * If a bit dissagrees with what we are
809 * looking for, mask it with the netmask
810 * to see if it really matters.
813 if (ifa->ifa_netmask == 0)
816 cp2 = ifa->ifa_addr->sa_data;
817 cp3 = ifa->ifa_netmask->sa_data;
818 cplim = ifa->ifa_netmask->sa_len +
819 (char *)ifa->ifa_netmask;
821 if ((*cp++ ^ *cp2++) & *cp3++)
822 goto next; /* next address! */
824 * If the netmask of what we just found
825 * is more specific than what we had before
826 * (if we had one) then remember the new one
827 * before continuing to search
828 * for an even better one.
830 if (ifa_maybe == 0 ||
831 rn_refines((char *)ifa->ifa_netmask,
832 (char *)ifa_maybe->ifa_netmask))
841 * Find an interface address specific to an interface best matching
845 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
848 char *cp, *cp2, *cp3;
850 struct ifaddr *ifa_maybe = 0;
851 u_int af = addr->sa_family;
855 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
856 if (ifa->ifa_addr->sa_family != af)
860 if (ifa->ifa_netmask == NULL) {
861 if (sa_equal(addr, ifa->ifa_addr) ||
862 (ifa->ifa_dstaddr != NULL &&
863 sa_equal(addr, ifa->ifa_dstaddr)))
867 if (ifp->if_flags & IFF_POINTOPOINT) {
868 if (sa_equal(addr, ifa->ifa_dstaddr))
872 cp2 = ifa->ifa_addr->sa_data;
873 cp3 = ifa->ifa_netmask->sa_data;
874 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
875 for (; cp3 < cplim; cp3++)
876 if ((*cp++ ^ *cp2++) & *cp3)
885 #include <net/route.h>
888 * Default action when installing a route with a Link Level gateway.
889 * Lookup an appropriate real ifa to point to.
890 * This should be moved to /sys/net/link.c eventually.
893 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
896 struct sockaddr *dst;
899 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
900 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
902 ifa = ifaof_ifpforaddr(dst, ifp);
907 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
908 ifa->ifa_rtrequest(cmd, rt, info);
913 * Mark an interface down and notify protocols of
915 * NOTE: must be called at splnet or eqivalent.
918 if_unroute(struct ifnet *ifp, int flag, int fam)
922 ifp->if_flags &= ~flag;
923 getmicrotime(&ifp->if_lastchange);
924 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
925 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
926 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
927 ifq_purge(&ifp->if_snd);
932 * Mark an interface up and notify protocols of
934 * NOTE: must be called at splnet or eqivalent.
937 if_route(struct ifnet *ifp, int flag, int fam)
941 ifp->if_flags |= flag;
942 getmicrotime(&ifp->if_lastchange);
943 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
944 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
945 pfctlinput(PRC_IFUP, ifa->ifa_addr);
953 * Mark an interface down and notify protocols of the transition. An
954 * interface going down is also considered to be a synchronizing event.
955 * We must ensure that all packet processing related to the interface
956 * has completed before we return so e.g. the caller can free the ifnet
957 * structure that the mbufs may be referencing.
959 * NOTE: must be called at splnet or eqivalent.
962 if_down(struct ifnet *ifp)
964 if_unroute(ifp, IFF_UP, AF_UNSPEC);
965 netmsg_service_sync();
969 * Mark an interface up and notify protocols of
971 * NOTE: must be called at splnet or eqivalent.
974 if_up(struct ifnet *ifp)
977 if_route(ifp, IFF_UP, AF_UNSPEC);
981 * Process a link state change.
982 * NOTE: must be called at splsoftnet or equivalent.
985 if_link_state_change(struct ifnet *ifp)
991 * Handle interface watchdog timer routines. Called
992 * from softclock, we decrement timers (if set) and
993 * call the appropriate interface routine on expiration.
996 if_slowtimo(void *arg)
1002 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1003 if (ifp->if_timer == 0 || --ifp->if_timer)
1005 if (ifp->if_watchdog) {
1006 if (lwkt_serialize_try(ifp->if_serializer)) {
1007 (*ifp->if_watchdog)(ifp);
1008 lwkt_serialize_exit(ifp->if_serializer);
1010 /* try again next timeout */
1018 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1022 * Map interface name to
1023 * interface structure pointer.
1026 ifunit(const char *name)
1031 * Search all the interfaces for this name/number
1034 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1035 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1043 * Map interface name in a sockaddr_dl to
1044 * interface structure pointer.
1047 if_withname(struct sockaddr *sa)
1049 char ifname[IFNAMSIZ+1];
1050 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1052 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1053 (sdl->sdl_nlen > IFNAMSIZ) )
1057 * ifunit wants a null-terminated name. It may not be null-terminated
1058 * in the sockaddr. We don't want to change the caller's sockaddr,
1059 * and there might not be room to put the trailing null anyway, so we
1060 * make a local copy that we know we can null terminate safely.
1063 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1064 ifname[sdl->sdl_nlen] = '\0';
1065 return ifunit(ifname);
1073 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1081 size_t namelen, onamelen;
1082 char new_name[IFNAMSIZ];
1084 struct sockaddr_dl *sdl;
1090 return (ifconf(cmd, data, cred));
1092 ifr = (struct ifreq *)data;
1097 if ((error = suser_cred(cred, 0)) != 0)
1099 return ((cmd == SIOCIFCREATE) ?
1100 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1101 if_clone_destroy(ifr->ifr_name));
1103 case SIOCIFGCLONERS:
1104 return (if_clone_list((struct if_clonereq *)data));
1107 ifp = ifunit(ifr->ifr_name);
1113 ifr->ifr_flags = ifp->if_flags;
1114 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1118 ifr->ifr_reqcap = ifp->if_capabilities;
1119 ifr->ifr_curcap = ifp->if_capenable;
1123 ifr->ifr_metric = ifp->if_metric;
1127 ifr->ifr_mtu = ifp->if_mtu;
1131 ifr->ifr_phys = ifp->if_physical;
1135 error = suser_cred(cred, 0);
1138 new_flags = (ifr->ifr_flags & 0xffff) |
1139 (ifr->ifr_flagshigh << 16);
1140 if (ifp->if_flags & IFF_SMART) {
1141 /* Smart drivers twiddle their own routes */
1142 } else if (ifp->if_flags & IFF_UP &&
1143 (new_flags & IFF_UP) == 0) {
1147 } else if (new_flags & IFF_UP &&
1148 (ifp->if_flags & IFF_UP) == 0) {
1154 #ifdef DEVICE_POLLING
1155 if ((new_flags ^ ifp->if_flags) & IFF_POLLING) {
1156 if (new_flags & IFF_POLLING) {
1157 ether_poll_register(ifp);
1159 ether_poll_deregister(ifp);
1164 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1165 (new_flags &~ IFF_CANTCHANGE);
1166 if (new_flags & IFF_PPROMISC) {
1167 /* Permanently promiscuous mode requested */
1168 ifp->if_flags |= IFF_PROMISC;
1169 } else if (ifp->if_pcount == 0) {
1170 ifp->if_flags &= ~IFF_PROMISC;
1172 if (ifp->if_ioctl) {
1173 lwkt_serialize_enter(ifp->if_serializer);
1174 ifp->if_ioctl(ifp, cmd, data, cred);
1175 lwkt_serialize_exit(ifp->if_serializer);
1177 getmicrotime(&ifp->if_lastchange);
1181 error = suser_cred(cred, 0);
1184 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1186 lwkt_serialize_enter(ifp->if_serializer);
1187 ifp->if_ioctl(ifp, cmd, data, cred);
1188 lwkt_serialize_exit(ifp->if_serializer);
1192 error = suser_cred(cred, 0);
1195 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1198 if (new_name[0] == '\0')
1200 if (ifunit(new_name) != NULL)
1203 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1205 /* Announce the departure of the interface. */
1206 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1208 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1209 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1210 /* XXX IFA_LOCK(ifa); */
1211 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1212 namelen = strlen(new_name);
1213 onamelen = sdl->sdl_nlen;
1215 * Move the address if needed. This is safe because we
1216 * allocate space for a name of length IFNAMSIZ when we
1217 * create this in if_attach().
1219 if (namelen != onamelen) {
1220 bcopy(sdl->sdl_data + onamelen,
1221 sdl->sdl_data + namelen, sdl->sdl_alen);
1223 bcopy(new_name, sdl->sdl_data, namelen);
1224 sdl->sdl_nlen = namelen;
1225 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1226 bzero(sdl->sdl_data, onamelen);
1227 while (namelen != 0)
1228 sdl->sdl_data[--namelen] = 0xff;
1229 /* XXX IFA_UNLOCK(ifa) */
1231 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1233 /* Announce the return of the interface. */
1234 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1238 error = suser_cred(cred, 0);
1241 ifp->if_metric = ifr->ifr_metric;
1242 getmicrotime(&ifp->if_lastchange);
1246 error = suser_cred(cred, 0);
1251 lwkt_serialize_enter(ifp->if_serializer);
1252 error = ifp->if_ioctl(ifp, cmd, data, cred);
1253 lwkt_serialize_exit(ifp->if_serializer);
1255 getmicrotime(&ifp->if_lastchange);
1260 u_long oldmtu = ifp->if_mtu;
1262 error = suser_cred(cred, 0);
1265 if (ifp->if_ioctl == NULL)
1266 return (EOPNOTSUPP);
1267 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1269 lwkt_serialize_enter(ifp->if_serializer);
1270 error = ifp->if_ioctl(ifp, cmd, data, cred);
1271 lwkt_serialize_exit(ifp->if_serializer);
1273 getmicrotime(&ifp->if_lastchange);
1277 * If the link MTU changed, do network layer specific procedure.
1279 if (ifp->if_mtu != oldmtu) {
1289 error = suser_cred(cred, 0);
1293 /* Don't allow group membership on non-multicast interfaces. */
1294 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1297 /* Don't let users screw up protocols' entries. */
1298 if (ifr->ifr_addr.sa_family != AF_LINK)
1301 if (cmd == SIOCADDMULTI) {
1302 struct ifmultiaddr *ifma;
1303 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1305 error = if_delmulti(ifp, &ifr->ifr_addr);
1308 getmicrotime(&ifp->if_lastchange);
1311 case SIOCSIFPHYADDR:
1312 case SIOCDIFPHYADDR:
1314 case SIOCSIFPHYADDR_IN6:
1316 case SIOCSLIFPHYADDR:
1318 case SIOCSIFGENERIC:
1319 error = suser_cred(cred, 0);
1322 if (ifp->if_ioctl == 0)
1323 return (EOPNOTSUPP);
1324 lwkt_serialize_enter(ifp->if_serializer);
1325 error = ifp->if_ioctl(ifp, cmd, data, cred);
1326 lwkt_serialize_exit(ifp->if_serializer);
1328 getmicrotime(&ifp->if_lastchange);
1332 ifs = (struct ifstat *)data;
1333 ifs->ascii[0] = '\0';
1335 case SIOCGIFPSRCADDR:
1336 case SIOCGIFPDSTADDR:
1337 case SIOCGLIFPHYADDR:
1339 case SIOCGIFGENERIC:
1340 if (ifp->if_ioctl == NULL)
1341 return (EOPNOTSUPP);
1342 lwkt_serialize_enter(ifp->if_serializer);
1343 error = ifp->if_ioctl(ifp, cmd, data, cred);
1344 lwkt_serialize_exit(ifp->if_serializer);
1348 error = suser_cred(cred, 0);
1351 return if_setlladdr(ifp,
1352 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1355 oif_flags = ifp->if_flags;
1356 if (so->so_proto == 0)
1357 return (EOPNOTSUPP);
1359 error = so_pru_control(so, cmd, data, ifp);
1366 case SIOCSIFDSTADDR:
1368 case SIOCSIFBRDADDR:
1369 case SIOCSIFNETMASK:
1370 #if BYTE_ORDER != BIG_ENDIAN
1371 if (ifr->ifr_addr.sa_family == 0 &&
1372 ifr->ifr_addr.sa_len < 16) {
1373 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1374 ifr->ifr_addr.sa_len = 16;
1377 if (ifr->ifr_addr.sa_len == 0)
1378 ifr->ifr_addr.sa_len = 16;
1386 case OSIOCGIFDSTADDR:
1387 cmd = SIOCGIFDSTADDR;
1390 case OSIOCGIFBRDADDR:
1391 cmd = SIOCGIFBRDADDR;
1394 case OSIOCGIFNETMASK:
1395 cmd = SIOCGIFNETMASK;
1397 error = so_pru_control(so, cmd, data, ifp);
1401 case OSIOCGIFDSTADDR:
1402 case OSIOCGIFBRDADDR:
1403 case OSIOCGIFNETMASK:
1404 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1408 #endif /* COMPAT_43 */
1410 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1412 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1413 if (ifp->if_flags & IFF_UP) {
1427 * Set/clear promiscuous mode on interface ifp based on the truth value
1428 * of pswitch. The calls are reference counted so that only the first
1429 * "on" request actually has an effect, as does the final "off" request.
1430 * Results are undefined if the "off" and "on" requests are not matched.
1433 ifpromisc(struct ifnet *ifp, int pswitch)
1439 oldflags = ifp->if_flags;
1440 if (ifp->if_flags & IFF_PPROMISC) {
1441 /* Do nothing if device is in permanently promiscuous mode */
1442 ifp->if_pcount += pswitch ? 1 : -1;
1447 * If the device is not configured up, we cannot put it in
1450 if ((ifp->if_flags & IFF_UP) == 0)
1452 if (ifp->if_pcount++ != 0)
1454 ifp->if_flags |= IFF_PROMISC;
1455 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1458 if (--ifp->if_pcount > 0)
1460 ifp->if_flags &= ~IFF_PROMISC;
1461 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1464 ifr.ifr_flags = ifp->if_flags;
1465 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1466 lwkt_serialize_enter(ifp->if_serializer);
1467 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1468 (struct ucred *)NULL);
1469 lwkt_serialize_exit(ifp->if_serializer);
1473 ifp->if_flags = oldflags;
1478 * Return interface configuration
1479 * of system. List may be used
1480 * in later ioctl's (above) to get
1481 * other information.
1484 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
1486 struct ifconf *ifc = (struct ifconf *)data;
1489 struct sockaddr *sa;
1490 struct ifreq ifr, *ifrp;
1491 int space = ifc->ifc_len, error = 0;
1493 ifrp = ifc->ifc_req;
1494 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1497 if (space <= sizeof ifr)
1501 * Zero the stack declared structure first to prevent
1502 * memory disclosure.
1504 bzero(&ifr, sizeof(ifr));
1505 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1506 >= sizeof(ifr.ifr_name)) {
1507 error = ENAMETOOLONG;
1512 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1513 if (space <= sizeof ifr)
1516 if (cred->cr_prison &&
1517 prison_if(cred, sa))
1521 if (cmd == OSIOCGIFCONF) {
1522 struct osockaddr *osa =
1523 (struct osockaddr *)&ifr.ifr_addr;
1525 osa->sa_family = sa->sa_family;
1526 error = copyout(&ifr, ifrp, sizeof ifr);
1530 if (sa->sa_len <= sizeof(*sa)) {
1532 error = copyout(&ifr, ifrp, sizeof ifr);
1535 if (space < (sizeof ifr) + sa->sa_len -
1538 space -= sa->sa_len - sizeof(*sa);
1539 error = copyout(&ifr, ifrp,
1540 sizeof ifr.ifr_name);
1542 error = copyout(sa, &ifrp->ifr_addr,
1544 ifrp = (struct ifreq *)
1545 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1549 space -= sizeof ifr;
1554 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
1555 error = copyout(&ifr, ifrp, sizeof ifr);
1558 space -= sizeof ifr;
1562 ifc->ifc_len -= space;
1567 * Just like if_promisc(), but for all-multicast-reception mode.
1570 if_allmulti(struct ifnet *ifp, int onswitch)
1578 if (ifp->if_amcount++ == 0) {
1579 ifp->if_flags |= IFF_ALLMULTI;
1580 ifr.ifr_flags = ifp->if_flags;
1581 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1582 lwkt_serialize_enter(ifp->if_serializer);
1583 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1584 (struct ucred *)NULL);
1585 lwkt_serialize_exit(ifp->if_serializer);
1588 if (ifp->if_amcount > 1) {
1591 ifp->if_amcount = 0;
1592 ifp->if_flags &= ~IFF_ALLMULTI;
1593 ifr.ifr_flags = ifp->if_flags;
1594 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1595 lwkt_serialize_enter(ifp->if_serializer);
1596 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1597 (struct ucred *)NULL);
1598 lwkt_serialize_exit(ifp->if_serializer);
1610 * Add a multicast listenership to the interface in question.
1611 * The link layer provides a routine which converts
1615 struct ifnet *ifp, /* interface to manipulate */
1616 struct sockaddr *sa, /* address to add */
1617 struct ifmultiaddr **retifma)
1619 struct sockaddr *llsa, *dupsa;
1621 struct ifmultiaddr *ifma;
1624 * If the matching multicast address already exists
1625 * then don't add a new one, just add a reference
1627 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1628 if (sa_equal(sa, ifma->ifma_addr)) {
1629 ifma->ifma_refcount++;
1637 * Give the link layer a chance to accept/reject it, and also
1638 * find out which AF_LINK address this maps to, if it isn't one
1641 if (ifp->if_resolvemulti) {
1642 lwkt_serialize_enter(ifp->if_serializer);
1643 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1644 lwkt_serialize_exit(ifp->if_serializer);
1651 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1652 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1653 bcopy(sa, dupsa, sa->sa_len);
1655 ifma->ifma_addr = dupsa;
1656 ifma->ifma_lladdr = llsa;
1657 ifma->ifma_ifp = ifp;
1658 ifma->ifma_refcount = 1;
1659 ifma->ifma_protospec = 0;
1660 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1663 * Some network interfaces can scan the address list at
1664 * interrupt time; lock them out.
1667 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1672 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1673 if (sa_equal(ifma->ifma_addr, llsa))
1677 ifma->ifma_refcount++;
1679 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1680 M_IFMADDR, M_WAITOK);
1681 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1682 M_IFMADDR, M_WAITOK);
1683 bcopy(llsa, dupsa, llsa->sa_len);
1684 ifma->ifma_addr = dupsa;
1685 ifma->ifma_ifp = ifp;
1686 ifma->ifma_refcount = 1;
1688 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1693 * We are certain we have added something, so call down to the
1694 * interface to let them know about it.
1697 lwkt_serialize_enter(ifp->if_serializer);
1698 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, (struct ucred *)NULL);
1699 lwkt_serialize_exit(ifp->if_serializer);
1706 * Remove a reference to a multicast address on this interface. Yell
1707 * if the request does not match an existing membership.
1710 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
1712 struct ifmultiaddr *ifma;
1714 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1715 if (sa_equal(sa, ifma->ifma_addr))
1720 if (ifma->ifma_refcount > 1) {
1721 ifma->ifma_refcount--;
1725 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1726 sa = ifma->ifma_lladdr;
1728 LIST_REMOVE(ifma, ifma_link);
1730 * Make sure the interface driver is notified
1731 * in the case of a link layer mcast group being left.
1733 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) {
1734 lwkt_serialize_enter(ifp->if_serializer);
1735 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1736 lwkt_serialize_exit(ifp->if_serializer);
1739 kfree(ifma->ifma_addr, M_IFMADDR);
1740 kfree(ifma, M_IFMADDR);
1745 * Now look for the link-layer address which corresponds to
1746 * this network address. It had been squirreled away in
1747 * ifma->ifma_lladdr for this purpose (so we don't have
1748 * to call ifp->if_resolvemulti() again), and we saved that
1749 * value in sa above. If some nasty deleted the
1750 * link-layer address out from underneath us, we can deal because
1751 * the address we stored was is not the same as the one which was
1752 * in the record for the link-layer address. (So we don't complain
1755 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1756 if (sa_equal(sa, ifma->ifma_addr))
1761 if (ifma->ifma_refcount > 1) {
1762 ifma->ifma_refcount--;
1767 lwkt_serialize_enter(ifp->if_serializer);
1768 LIST_REMOVE(ifma, ifma_link);
1769 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1770 lwkt_serialize_exit(ifp->if_serializer);
1772 kfree(ifma->ifma_addr, M_IFMADDR);
1773 kfree(sa, M_IFMADDR);
1774 kfree(ifma, M_IFMADDR);
1780 * Set the link layer address on an interface.
1782 * At this time we only support certain types of interfaces,
1783 * and we don't allow the length of the address to change.
1786 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1788 struct sockaddr_dl *sdl;
1792 sdl = IF_LLSOCKADDR(ifp);
1795 if (len != sdl->sdl_alen) /* don't allow length to change */
1797 switch (ifp->if_type) {
1798 case IFT_ETHER: /* these types use struct arpcom */
1803 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1806 bcopy(lladdr, LLADDR(sdl), len);
1812 * If the interface is already up, we need
1813 * to re-init it in order to reprogram its
1816 lwkt_serialize_enter(ifp->if_serializer);
1817 if ((ifp->if_flags & IFF_UP) != 0) {
1818 ifp->if_flags &= ~IFF_UP;
1819 ifr.ifr_flags = ifp->if_flags;
1820 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1821 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1822 (struct ucred *)NULL);
1823 ifp->if_flags |= IFF_UP;
1824 ifr.ifr_flags = ifp->if_flags;
1825 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1826 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1827 (struct ucred *)NULL);
1830 * Also send gratuitous ARPs to notify other nodes about
1831 * the address change.
1833 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1834 if (ifa->ifa_addr != NULL &&
1835 ifa->ifa_addr->sa_family == AF_INET)
1836 arp_ifinit(ifp, ifa);
1840 lwkt_serialize_exit(ifp->if_serializer);
1844 struct ifmultiaddr *
1845 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
1847 struct ifmultiaddr *ifma;
1849 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1850 if (sa_equal(ifma->ifma_addr, sa))
1857 * This function locates the first real ethernet MAC from a network
1858 * card and loads it into node, returning 0 on success or ENOENT if
1859 * no suitable interfaces were found. It is used by the uuid code to
1860 * generate a unique 6-byte number.
1863 if_getanyethermac(uint16_t *node, int minlen)
1866 struct sockaddr_dl *sdl;
1868 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1869 if (ifp->if_type != IFT_ETHER)
1871 sdl = IF_LLSOCKADDR(ifp);
1872 if (sdl->sdl_alen < minlen)
1874 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
1882 * The name argument must be a pointer to storage which will last as
1883 * long as the interface does. For physical devices, the result of
1884 * device_get_name(dev) is a good choice and for pseudo-devices a
1885 * static string works well.
1888 if_initname(struct ifnet *ifp, const char *name, int unit)
1890 ifp->if_dname = name;
1891 ifp->if_dunit = unit;
1892 if (unit != IF_DUNIT_NONE)
1893 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
1895 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1899 if_printf(struct ifnet *ifp, const char *fmt, ...)
1904 retval = kprintf("%s: ", ifp->if_xname);
1905 __va_start(ap, fmt);
1906 retval += kvprintf(fmt, ap);
1911 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1912 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
1915 ifq_set_classic(struct ifaltq *ifq)
1917 ifq->altq_enqueue = ifq_classic_enqueue;
1918 ifq->altq_dequeue = ifq_classic_dequeue;
1919 ifq->altq_request = ifq_classic_request;
1923 ifq_classic_enqueue(struct ifaltq *ifq, struct mbuf *m,
1924 struct altq_pktattr *pa __unused)
1927 if (IF_QFULL(ifq)) {
1938 static struct mbuf *
1939 ifq_classic_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
1952 panic("unsupported ALTQ dequeue op: %d", op);
1955 KKASSERT(mpolled == NULL || mpolled == m);
1960 ifq_classic_request(struct ifaltq *ifq, int req, void *arg)
1968 panic("unsupported ALTQ request: %d", req);