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.21 2004/09/13 23:45:57 drhodus Exp $
38 #include "opt_compat.h"
39 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/malloc.h>
45 #include <sys/systm.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/socketops.h>
51 #include <sys/protosw.h>
52 #include <sys/kernel.h>
53 #include <sys/sockio.h>
54 #include <sys/syslog.h>
55 #include <sys/sysctl.h>
58 #include <net/if_arp.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
61 #include <net/if_var.h>
62 #include <net/radix.h>
63 #include <net/route.h>
64 #include <machine/stdarg.h>
66 #if defined(INET) || defined(INET6)
68 #include <netinet/in.h>
69 #include <netinet/in_var.h>
70 #include <netinet/if_ether.h>
72 #include <machine/clock.h> /* XXX: temporal workaround for fxp issue */
73 #include <netinet6/in6_var.h>
74 #include <netinet6/in6_ifattach.h>
78 #if defined(COMPAT_43)
79 #include <emulation/43bsd/43bsd_socket.h>
80 #endif /* COMPAT_43 */
83 * System initialization
86 static int ifconf (u_long, caddr_t, struct thread *);
87 static void ifinit (void *);
88 static void if_qflush (struct ifqueue *);
89 static void if_slowtimo (void *);
90 static void link_rtrequest (int, struct rtentry *, struct rt_addrinfo *);
91 static int if_rtdel (struct radix_node *, void *);
93 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
95 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
96 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
97 MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
99 int ifqmaxlen = IFQ_MAXLEN;
100 struct ifnethead ifnet; /* depend on static init XXX */
104 * XXX: declare here to avoid to include many inet6 related files..
105 * should be more generalized?
107 extern void nd6_setmtu (struct ifnet *);
110 struct if_clone *if_clone_lookup (const char *, int *);
111 int if_clone_list (struct if_clonereq *);
113 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
114 int if_cloners_count;
117 * Network interface utility routines.
119 * Routines with ifa_ifwith* names take sockaddr *'s as
131 TAILQ_FOREACH(ifp, &ifnet, if_link) {
132 if (ifp->if_snd.ifq_maxlen == 0) {
133 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
134 ifp->if_snd.ifq_maxlen = ifqmaxlen;
142 struct ifaddr **ifnet_addrs;
143 struct ifnet **ifindex2ifnet = NULL;
147 * Attach an interface to the
148 * list of "active" interfaces.
154 unsigned socksize, ifasize;
155 int namelen, masklen;
156 struct sockaddr_dl *sdl;
158 static int if_indexlim = 8;
166 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
167 ifp->if_index = ++if_index;
170 * The old code would work if the interface passed a pre-existing
171 * chain of ifaddrs to this code. We don't trust our callers to
172 * properly initialize the tailq, however, so we no longer allow
173 * this unlikely case.
175 TAILQ_INIT(&ifp->if_addrhead);
176 TAILQ_INIT(&ifp->if_prefixhead);
177 LIST_INIT(&ifp->if_multiaddrs);
178 getmicrotime(&ifp->if_lastchange);
179 if (ifnet_addrs == 0 || if_index >= if_indexlim) {
180 unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
181 caddr_t q = malloc(n, M_IFADDR, M_WAITOK);
184 bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
185 free((caddr_t)ifnet_addrs, M_IFADDR);
187 ifnet_addrs = (struct ifaddr **)q;
189 /* grow ifindex2ifnet */
190 n = if_indexlim * sizeof(struct ifnet *);
191 q = malloc(n, M_IFADDR, M_WAITOK);
194 bcopy((caddr_t)ifindex2ifnet, q, n/2);
195 free((caddr_t)ifindex2ifnet, M_IFADDR);
197 ifindex2ifnet = (struct ifnet **)q;
200 ifindex2ifnet[if_index] = ifp;
203 * create a Link Level name for this device
205 namelen = strlen(ifp->if_xname);
206 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
207 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
208 socksize = masklen + ifp->if_addrlen;
209 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
210 if (socksize < sizeof(*sdl))
211 socksize = sizeof(*sdl);
212 socksize = ROUNDUP(socksize);
213 ifasize = sizeof(*ifa) + 2 * socksize;
214 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
216 bzero((caddr_t)ifa, ifasize);
217 sdl = (struct sockaddr_dl *)(ifa + 1);
218 sdl->sdl_len = socksize;
219 sdl->sdl_family = AF_LINK;
220 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
221 sdl->sdl_nlen = namelen;
222 sdl->sdl_index = ifp->if_index;
223 sdl->sdl_type = ifp->if_type;
224 ifnet_addrs[if_index - 1] = ifa;
226 ifa->ifa_rtrequest = link_rtrequest;
227 ifa->ifa_addr = (struct sockaddr *)sdl;
228 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
229 ifa->ifa_netmask = (struct sockaddr *)sdl;
230 sdl->sdl_len = masklen;
232 sdl->sdl_data[--namelen] = 0xff;
233 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
236 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
238 /* Announce the interface. */
239 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
243 * Detach an interface, removing it from the
244 * list of "active" interfaces.
251 struct radix_node_head *rnh;
255 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
258 * Remove routes and flush queues.
264 * Remove address from ifnet_addrs[] and maybe decrement if_index.
265 * Clean up all addresses.
267 ifnet_addrs[ifp->if_index - 1] = 0;
268 while (if_index > 0 && ifnet_addrs[if_index - 1] == 0)
271 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
272 ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
274 /* XXX: Ugly!! ad hoc just for INET */
275 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
276 struct ifaliasreq ifr;
278 bzero(&ifr, sizeof(ifr));
279 ifr.ifra_addr = *ifa->ifa_addr;
280 if (ifa->ifa_dstaddr)
281 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
282 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
288 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
290 /* ifp_addrhead is already updated */
294 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
300 * Remove all IPv6 kernel structs related to ifp. This should be done
301 * before removing routing entries below, since IPv6 interface direct
302 * routes are expected to be removed by the IPv6-specific kernel API.
303 * Otherwise, the kernel will detect some inconsistency and bark it.
309 * Delete all remaining routes using this interface
310 * Unfortuneatly the only way to do this is to slog through
311 * the entire routing table looking for routes which point
312 * to this interface...oh well...
314 for (i = 1; i <= AF_MAX; i++) {
315 if ((rnh = rt_tables[i]) == NULL)
317 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
320 /* Announce that the interface is gone. */
321 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
323 ifindex2ifnet[ifp->if_index] = NULL;
325 TAILQ_REMOVE(&ifnet, ifp, if_link);
330 * Delete Routes for a Network Interface
332 * Called for each routing entry via the rnh->rnh_walktree() call above
333 * to delete all route entries referencing a detaching network interface.
336 * rn pointer to node in the routing table
337 * arg argument passed to rnh->rnh_walktree() - detaching interface
341 * errno failed - reason indicated
346 struct radix_node *rn;
349 struct rtentry *rt = (struct rtentry *)rn;
350 struct ifnet *ifp = arg;
353 if (rt->rt_ifp == ifp) {
356 * Protect (sorta) against walktree recursion problems
359 if ((rt->rt_flags & RTF_UP) == 0)
362 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
363 rt_mask(rt), rt->rt_flags,
364 (struct rtentry **) NULL);
366 log(LOG_WARNING, "if_rtdel: error %d\n", err);
374 * Create a clone network interface.
377 if_clone_create(name, len)
381 struct if_clone *ifc;
383 int wildcard, bytoff, bitoff;
387 ifc = if_clone_lookup(name, &unit);
391 if (ifunit(name) != NULL)
395 wildcard = (unit < 0);
397 * Find a free unit if none was given.
400 while ((bytoff < ifc->ifc_bmlen)
401 && (ifc->ifc_units[bytoff] == 0xff))
403 if (bytoff >= ifc->ifc_bmlen)
405 while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
407 unit = (bytoff << 3) + bitoff;
410 if (unit > ifc->ifc_maxunit)
413 err = (*ifc->ifc_create)(ifc, unit);
419 bitoff = unit - (bytoff << 3);
423 * Allocate the unit in the bitmap.
425 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
426 ("%s: bit is already set", __func__));
427 ifc->ifc_units[bytoff] |= (1 << bitoff);
429 /* In the wildcard case, we need to update the name. */
431 for (dp = name; *dp != '\0'; dp++);
432 if (snprintf(dp, len - (dp-name), "%d", unit) >
433 len - (dp-name) - 1) {
435 * This can only be a programmer error and
436 * there's no straightforward way to recover if
439 panic("if_clone_create(): interface name too long");
444 EVENTHANDLER_INVOKE(if_clone_event, ifc);
450 * Destroy a clone network interface.
453 if_clone_destroy(name)
456 struct if_clone *ifc;
461 ifc = if_clone_lookup(name, &unit);
465 if (unit < ifc->ifc_minifs)
472 if (ifc->ifc_destroy == NULL)
475 (*ifc->ifc_destroy)(ifp);
478 * Compute offset in the bitmap and deallocate the unit.
481 bitoff = unit - (bytoff << 3);
482 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
483 ("%s: bit is already cleared", __func__));
484 ifc->ifc_units[bytoff] &= ~(1 << bitoff);
489 * Look up a network interface cloner.
492 if_clone_lookup(name, unitp)
496 struct if_clone *ifc;
500 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
501 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
502 if (ifc->ifc_name[i] != *cp)
507 ifc = LIST_NEXT(ifc, ifc_list);
511 return ((struct if_clone *)NULL);
517 for (i = 0; *cp != '\0'; cp++) {
518 if (*cp < '0' || *cp > '9') {
519 /* Bogus unit number. */
522 i = (i * 10) + (*cp - '0');
532 * Register a network interface cloner.
536 struct if_clone *ifc;
543 KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
544 ("%s: %s requested more units then allowed (%d > %d)",
545 __func__, ifc->ifc_name, ifc->ifc_minifs,
546 ifc->ifc_maxunit + 1));
548 * Compute bitmap size and allocate it.
550 maxclone = ifc->ifc_maxunit + 1;
552 if ((len << 3) < maxclone)
554 ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO);
555 ifc->ifc_bmlen = len;
557 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
560 for (unit = 0; unit < ifc->ifc_minifs; unit++) {
561 err = (*ifc->ifc_create)(ifc, unit);
563 ("%s: failed to create required interface %s%d",
564 __func__, ifc->ifc_name, unit));
566 /* Allocate the unit in the bitmap. */
568 bitoff = unit - (bytoff << 3);
569 ifc->ifc_units[bytoff] |= (1 << bitoff);
574 * Unregister a network interface cloner.
578 struct if_clone *ifc;
581 LIST_REMOVE(ifc, ifc_list);
582 free(ifc->ifc_units, M_CLONE);
587 * Provide list of interface cloners to userspace.
591 struct if_clonereq *ifcr;
593 char outbuf[IFNAMSIZ], *dst;
594 struct if_clone *ifc;
595 int count, error = 0;
597 ifcr->ifcr_total = if_cloners_count;
598 if ((dst = ifcr->ifcr_buffer) == NULL) {
599 /* Just asking how many there are. */
603 if (ifcr->ifcr_count < 0)
606 count = (if_cloners_count < ifcr->ifcr_count) ?
607 if_cloners_count : ifcr->ifcr_count;
609 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
610 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
611 strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
612 error = copyout(outbuf, dst, IFNAMSIZ);
621 * Locate an interface based on a complete address.
626 struct sockaddr *addr;
631 #define equal(a1, a2) \
632 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
633 TAILQ_FOREACH(ifp, &ifnet, if_link)
634 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
635 if (ifa->ifa_addr->sa_family != addr->sa_family)
637 if (equal(addr, ifa->ifa_addr))
639 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
640 /* IP6 doesn't have broadcast */
641 ifa->ifa_broadaddr->sa_len != 0 &&
642 equal(ifa->ifa_broadaddr, addr))
645 return ((struct ifaddr *)0);
648 * Locate the point to point interface with a given destination address.
652 ifa_ifwithdstaddr(addr)
653 struct sockaddr *addr;
658 TAILQ_FOREACH(ifp, &ifnet, if_link)
659 if (ifp->if_flags & IFF_POINTOPOINT)
660 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
661 if (ifa->ifa_addr->sa_family != addr->sa_family)
663 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
666 return ((struct ifaddr *)0);
670 * Find an interface on a specific network. If many, choice
671 * is most specific found.
675 struct sockaddr *addr;
679 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
680 u_int af = addr->sa_family;
681 char *addr_data = addr->sa_data, *cplim;
684 * AF_LINK addresses can be looked up directly by their index number,
685 * so do that if we can.
688 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
689 if (sdl->sdl_index && sdl->sdl_index <= if_index)
690 return (ifnet_addrs[sdl->sdl_index - 1]);
694 * Scan though each interface, looking for ones that have
695 * addresses in this address family.
697 TAILQ_FOREACH(ifp, &ifnet, if_link) {
698 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
699 char *cp, *cp2, *cp3;
701 if (ifa->ifa_addr->sa_family != af)
703 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
705 * This is a bit broken as it doesn't
706 * take into account that the remote end may
707 * be a single node in the network we are
709 * The trouble is that we don't know the
710 * netmask for the remote end.
712 if (ifa->ifa_dstaddr != 0
713 && equal(addr, ifa->ifa_dstaddr))
717 * if we have a special address handler,
718 * then use it instead of the generic one.
720 if (ifa->ifa_claim_addr) {
721 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
729 * Scan all the bits in the ifa's address.
730 * If a bit dissagrees with what we are
731 * looking for, mask it with the netmask
732 * to see if it really matters.
735 if (ifa->ifa_netmask == 0)
738 cp2 = ifa->ifa_addr->sa_data;
739 cp3 = ifa->ifa_netmask->sa_data;
740 cplim = ifa->ifa_netmask->sa_len
741 + (char *)ifa->ifa_netmask;
743 if ((*cp++ ^ *cp2++) & *cp3++)
744 goto next; /* next address! */
746 * If the netmask of what we just found
747 * is more specific than what we had before
748 * (if we had one) then remember the new one
749 * before continuing to search
750 * for an even better one.
752 if (ifa_maybe == 0 ||
753 rn_refines((caddr_t)ifa->ifa_netmask,
754 (caddr_t)ifa_maybe->ifa_netmask))
763 * Find an interface address specific to an interface best matching
767 ifaof_ifpforaddr(addr, ifp)
768 struct sockaddr *addr;
772 char *cp, *cp2, *cp3;
774 struct ifaddr *ifa_maybe = 0;
775 u_int af = addr->sa_family;
779 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
780 if (ifa->ifa_addr->sa_family != af)
784 if (ifa->ifa_netmask == 0) {
785 if (equal(addr, ifa->ifa_addr) ||
786 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
790 if (ifp->if_flags & IFF_POINTOPOINT) {
791 if (equal(addr, ifa->ifa_dstaddr))
795 cp2 = ifa->ifa_addr->sa_data;
796 cp3 = ifa->ifa_netmask->sa_data;
797 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
798 for (; cp3 < cplim; cp3++)
799 if ((*cp++ ^ *cp2++) & *cp3)
808 #include <net/route.h>
811 * Default action when installing a route with a Link Level gateway.
812 * Lookup an appropriate real ifa to point to.
813 * This should be moved to /sys/net/link.c eventually.
816 link_rtrequest(cmd, rt, info)
819 struct rt_addrinfo *info;
822 struct sockaddr *dst;
825 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
826 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
828 ifa = ifaof_ifpforaddr(dst, ifp);
833 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
834 ifa->ifa_rtrequest(cmd, rt, info);
839 * Mark an interface down and notify protocols of
841 * NOTE: must be called at splnet or eqivalent.
844 if_unroute(ifp, flag, fam)
850 ifp->if_flags &= ~flag;
851 getmicrotime(&ifp->if_lastchange);
852 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
853 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
854 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
855 if_qflush(&ifp->if_snd);
860 * Mark an interface up and notify protocols of
862 * NOTE: must be called at splnet or eqivalent.
865 if_route(ifp, flag, fam)
871 ifp->if_flags |= flag;
872 getmicrotime(&ifp->if_lastchange);
873 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
874 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
875 pfctlinput(PRC_IFUP, ifa->ifa_addr);
883 * Mark an interface down and notify protocols of
885 * NOTE: must be called at splnet or eqivalent.
892 if_unroute(ifp, IFF_UP, AF_UNSPEC);
896 * Mark an interface up and notify protocols of
898 * NOTE: must be called at splnet or eqivalent.
905 if_route(ifp, IFF_UP, AF_UNSPEC);
909 * Flush an interface queue.
918 while ((m = n) != 0) {
928 * Handle interface watchdog timer routines. Called
929 * from softclock, we decrement timers (if set) and
930 * call the appropriate interface routine on expiration.
939 TAILQ_FOREACH(ifp, &ifnet, if_link) {
940 if (ifp->if_timer == 0 || --ifp->if_timer)
942 if (ifp->if_watchdog)
943 (*ifp->if_watchdog)(ifp);
946 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
950 * Map interface name to
951 * interface structure pointer.
954 ifunit(const char *name)
959 * Search all the interfaces for this name/number
962 TAILQ_FOREACH(ifp, &ifnet, if_link) {
963 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
971 * Map interface name in a sockaddr_dl to
972 * interface structure pointer.
978 char ifname[IFNAMSIZ+1];
979 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
981 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
982 (sdl->sdl_nlen > IFNAMSIZ) )
986 * ifunit wants a null-terminated name. It may not be null-terminated
987 * in the sockaddr. We don't want to change the caller's sockaddr,
988 * and there might not be room to put the trailing null anyway, so we
989 * make a local copy that we know we can null terminate safely.
992 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
993 ifname[sdl->sdl_nlen] = '\0';
994 return ifunit(ifname);
1002 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
1010 size_t namelen, onamelen;
1011 char new_name[IFNAMSIZ];
1013 struct sockaddr_dl *sdl;
1019 return (ifconf(cmd, data, td));
1021 ifr = (struct ifreq *)data;
1026 if ((error = suser(td)) != 0)
1028 return ((cmd == SIOCIFCREATE) ?
1029 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1030 if_clone_destroy(ifr->ifr_name));
1032 case SIOCIFGCLONERS:
1033 return (if_clone_list((struct if_clonereq *)data));
1036 ifp = ifunit(ifr->ifr_name);
1042 ifr->ifr_flags = ifp->if_flags;
1043 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1047 ifr->ifr_reqcap = ifp->if_capabilities;
1048 ifr->ifr_curcap = ifp->if_capenable;
1052 ifr->ifr_metric = ifp->if_metric;
1056 ifr->ifr_mtu = ifp->if_mtu;
1060 ifr->ifr_phys = ifp->if_physical;
1067 new_flags = (ifr->ifr_flags & 0xffff) |
1068 (ifr->ifr_flagshigh << 16);
1069 if (ifp->if_flags & IFF_SMART) {
1070 /* Smart drivers twiddle their own routes */
1071 } else if (ifp->if_flags & IFF_UP &&
1072 (new_flags & IFF_UP) == 0) {
1076 } else if (new_flags & IFF_UP &&
1077 (ifp->if_flags & IFF_UP) == 0) {
1082 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1083 (new_flags &~ IFF_CANTCHANGE);
1084 if (new_flags & IFF_PPROMISC) {
1085 /* Permanently promiscuous mode requested */
1086 ifp->if_flags |= IFF_PROMISC;
1087 } else if (ifp->if_pcount == 0) {
1088 ifp->if_flags &= ~IFF_PROMISC;
1091 (void) (*ifp->if_ioctl)(ifp, cmd, data,
1092 td->td_proc->p_ucred);
1093 getmicrotime(&ifp->if_lastchange);
1100 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1102 (void) (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
1109 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1112 if (new_name[0] == '\0')
1114 if (ifunit(new_name) != NULL)
1117 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1119 /* Announce the departure of the interface. */
1120 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1122 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1123 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1124 /* XXX IFA_LOCK(ifa); */
1125 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1126 namelen = strlen(new_name);
1127 onamelen = sdl->sdl_nlen;
1129 * Move the address if needed. This is safe because we
1130 * allocate space for a name of length IFNAMSIZ when we
1131 * create this in if_attach().
1133 if (namelen != onamelen) {
1134 bcopy(sdl->sdl_data + onamelen,
1135 sdl->sdl_data + namelen, sdl->sdl_alen);
1137 bcopy(new_name, sdl->sdl_data, namelen);
1138 sdl->sdl_nlen = namelen;
1139 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1140 bzero(sdl->sdl_data, onamelen);
1141 while (namelen != 0)
1142 sdl->sdl_data[--namelen] = 0xff;
1143 /* XXX IFA_UNLOCK(ifa) */
1145 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1147 /* Announce the return of the interface. */
1148 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1155 ifp->if_metric = ifr->ifr_metric;
1156 getmicrotime(&ifp->if_lastchange);
1165 error = (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
1167 getmicrotime(&ifp->if_lastchange);
1172 u_long oldmtu = ifp->if_mtu;
1177 if (ifp->if_ioctl == NULL)
1178 return (EOPNOTSUPP);
1179 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1181 error = (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
1183 getmicrotime(&ifp->if_lastchange);
1187 * If the link MTU changed, do network layer specific procedure.
1189 if (ifp->if_mtu != oldmtu) {
1203 /* Don't allow group membership on non-multicast interfaces. */
1204 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1207 /* Don't let users screw up protocols' entries. */
1208 if (ifr->ifr_addr.sa_family != AF_LINK)
1211 if (cmd == SIOCADDMULTI) {
1212 struct ifmultiaddr *ifma;
1213 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1215 error = if_delmulti(ifp, &ifr->ifr_addr);
1218 getmicrotime(&ifp->if_lastchange);
1221 case SIOCSIFPHYADDR:
1222 case SIOCDIFPHYADDR:
1224 case SIOCSIFPHYADDR_IN6:
1226 case SIOCSLIFPHYADDR:
1228 case SIOCSIFGENERIC:
1232 if (ifp->if_ioctl == 0)
1233 return (EOPNOTSUPP);
1234 error = (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
1236 getmicrotime(&ifp->if_lastchange);
1240 ifs = (struct ifstat *)data;
1241 ifs->ascii[0] = '\0';
1243 case SIOCGIFPSRCADDR:
1244 case SIOCGIFPDSTADDR:
1245 case SIOCGLIFPHYADDR:
1247 case SIOCGIFGENERIC:
1248 if (ifp->if_ioctl == 0)
1249 return (EOPNOTSUPP);
1250 return ((*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred));
1256 return if_setlladdr(ifp,
1257 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1260 oif_flags = ifp->if_flags;
1261 if (so->so_proto == 0)
1262 return (EOPNOTSUPP);
1264 error = so_pru_control(so, cmd, data, ifp, td);
1271 case SIOCSIFDSTADDR:
1273 case SIOCSIFBRDADDR:
1274 case SIOCSIFNETMASK:
1275 #if BYTE_ORDER != BIG_ENDIAN
1276 if (ifr->ifr_addr.sa_family == 0 &&
1277 ifr->ifr_addr.sa_len < 16) {
1278 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1279 ifr->ifr_addr.sa_len = 16;
1282 if (ifr->ifr_addr.sa_len == 0)
1283 ifr->ifr_addr.sa_len = 16;
1291 case OSIOCGIFDSTADDR:
1292 cmd = SIOCGIFDSTADDR;
1295 case OSIOCGIFBRDADDR:
1296 cmd = SIOCGIFBRDADDR;
1299 case OSIOCGIFNETMASK:
1300 cmd = SIOCGIFNETMASK;
1302 error = so_pru_control(so, cmd, data, ifp, td);
1306 case OSIOCGIFDSTADDR:
1307 case OSIOCGIFBRDADDR:
1308 case OSIOCGIFNETMASK:
1309 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1313 #endif /* COMPAT_43 */
1315 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1317 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1318 if (ifp->if_flags & IFF_UP) {
1332 * Set/clear promiscuous mode on interface ifp based on the truth value
1333 * of pswitch. The calls are reference counted so that only the first
1334 * "on" request actually has an effect, as does the final "off" request.
1335 * Results are undefined if the "off" and "on" requests are not matched.
1338 ifpromisc(ifp, pswitch)
1346 oldflags = ifp->if_flags;
1347 if (ifp->if_flags & IFF_PPROMISC) {
1348 /* Do nothing if device is in permanently promiscuous mode */
1349 ifp->if_pcount += pswitch ? 1 : -1;
1354 * If the device is not configured up, we cannot put it in
1357 if ((ifp->if_flags & IFF_UP) == 0)
1359 if (ifp->if_pcount++ != 0)
1361 ifp->if_flags |= IFF_PROMISC;
1362 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1365 if (--ifp->if_pcount > 0)
1367 ifp->if_flags &= ~IFF_PROMISC;
1368 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1371 ifr.ifr_flags = ifp->if_flags;
1372 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1373 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1374 (struct ucred *)NULL);
1378 ifp->if_flags = oldflags;
1383 * Return interface configuration
1384 * of system. List may be used
1385 * in later ioctl's (above) to get
1386 * other information.
1390 ifconf(u_long cmd, caddr_t data, struct thread *td)
1392 struct ifconf *ifc = (struct ifconf *)data;
1395 struct sockaddr *sa;
1396 struct ifreq ifr, *ifrp;
1397 int space = ifc->ifc_len, error = 0;
1399 ifrp = ifc->ifc_req;
1400 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1403 if (space <= sizeof (ifr))
1405 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1406 >= sizeof(ifr.ifr_name)) {
1407 error = ENAMETOOLONG;
1412 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1413 if (space <= sizeof(ifr))
1416 if (td->td_proc->p_ucred->cr_prison &&
1421 if (cmd == OSIOCGIFCONF) {
1422 struct osockaddr *osa =
1423 (struct osockaddr *)&ifr.ifr_addr;
1425 osa->sa_family = sa->sa_family;
1426 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1431 if (sa->sa_len <= sizeof(*sa)) {
1433 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1437 if (space < sizeof (ifr) + sa->sa_len -
1440 space -= sa->sa_len - sizeof(*sa);
1441 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1442 sizeof (ifr.ifr_name));
1444 error = copyout((caddr_t)sa,
1445 (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1446 ifrp = (struct ifreq *)
1447 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1451 space -= sizeof (ifr);
1456 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1457 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1461 space -= sizeof (ifr);
1465 ifc->ifc_len -= space;
1470 * Just like if_promisc(), but for all-multicast-reception mode.
1473 if_allmulti(ifp, onswitch)
1482 if (ifp->if_amcount++ == 0) {
1483 ifp->if_flags |= IFF_ALLMULTI;
1484 ifr.ifr_flags = ifp->if_flags;
1485 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1486 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1487 (struct ucred *)NULL);
1490 if (ifp->if_amcount > 1) {
1493 ifp->if_amcount = 0;
1494 ifp->if_flags &= ~IFF_ALLMULTI;
1495 ifr.ifr_flags = ifp->if_flags;
1496 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1497 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1498 (struct ucred *)NULL);
1509 * Add a multicast listenership to the interface in question.
1510 * The link layer provides a routine which converts
1513 if_addmulti(ifp, sa, retifma)
1514 struct ifnet *ifp; /* interface to manipulate */
1515 struct sockaddr *sa; /* address to add */
1516 struct ifmultiaddr **retifma;
1518 struct sockaddr *llsa, *dupsa;
1520 struct ifmultiaddr *ifma;
1523 * If the matching multicast address already exists
1524 * then don't add a new one, just add a reference
1526 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1527 if (equal(sa, ifma->ifma_addr)) {
1528 ifma->ifma_refcount++;
1536 * Give the link layer a chance to accept/reject it, and also
1537 * find out which AF_LINK address this maps to, if it isn't one
1540 if (ifp->if_resolvemulti) {
1541 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1542 if (error) return error;
1547 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1548 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1549 bcopy(sa, dupsa, sa->sa_len);
1551 ifma->ifma_addr = dupsa;
1552 ifma->ifma_lladdr = llsa;
1553 ifma->ifma_ifp = ifp;
1554 ifma->ifma_refcount = 1;
1555 ifma->ifma_protospec = 0;
1556 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1559 * Some network interfaces can scan the address list at
1560 * interrupt time; lock them out.
1563 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1568 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1569 if (equal(ifma->ifma_addr, llsa))
1573 ifma->ifma_refcount++;
1575 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1576 M_IFMADDR, M_WAITOK);
1577 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1578 M_IFMADDR, M_WAITOK);
1579 bcopy(llsa, dupsa, llsa->sa_len);
1580 ifma->ifma_addr = dupsa;
1581 ifma->ifma_ifp = ifp;
1582 ifma->ifma_refcount = 1;
1584 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1589 * We are certain we have added something, so call down to the
1590 * interface to let them know about it.
1593 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, (struct ucred *)NULL);
1600 * Remove a reference to a multicast address on this interface. Yell
1601 * if the request does not match an existing membership.
1604 if_delmulti(ifp, sa)
1606 struct sockaddr *sa;
1608 struct ifmultiaddr *ifma;
1611 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1612 if (equal(sa, ifma->ifma_addr))
1617 if (ifma->ifma_refcount > 1) {
1618 ifma->ifma_refcount--;
1622 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1623 sa = ifma->ifma_lladdr;
1625 LIST_REMOVE(ifma, ifma_link);
1627 * Make sure the interface driver is notified
1628 * in the case of a link layer mcast group being left.
1630 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0)
1631 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1633 free(ifma->ifma_addr, M_IFMADDR);
1634 free(ifma, M_IFMADDR);
1639 * Now look for the link-layer address which corresponds to
1640 * this network address. It had been squirreled away in
1641 * ifma->ifma_lladdr for this purpose (so we don't have
1642 * to call ifp->if_resolvemulti() again), and we saved that
1643 * value in sa above. If some nasty deleted the
1644 * link-layer address out from underneath us, we can deal because
1645 * the address we stored was is not the same as the one which was
1646 * in the record for the link-layer address. (So we don't complain
1649 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1650 if (equal(sa, ifma->ifma_addr))
1655 if (ifma->ifma_refcount > 1) {
1656 ifma->ifma_refcount--;
1661 LIST_REMOVE(ifma, ifma_link);
1662 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1664 free(ifma->ifma_addr, M_IFMADDR);
1665 free(sa, M_IFMADDR);
1666 free(ifma, M_IFMADDR);
1672 * Set the link layer address on an interface.
1674 * At this time we only support certain types of interfaces,
1675 * and we don't allow the length of the address to change.
1678 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1680 struct sockaddr_dl *sdl;
1684 ifa = ifnet_addrs[ifp->if_index - 1];
1687 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1690 if (len != sdl->sdl_alen) /* don't allow length to change */
1692 switch (ifp->if_type) {
1693 case IFT_ETHER: /* these types use struct arpcom */
1698 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1701 bcopy(lladdr, LLADDR(sdl), len);
1707 * If the interface is already up, we need
1708 * to re-init it in order to reprogram its
1711 if ((ifp->if_flags & IFF_UP) != 0) {
1712 ifp->if_flags &= ~IFF_UP;
1713 ifr.ifr_flags = ifp->if_flags;
1714 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1715 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1716 (struct ucred *)NULL);
1717 ifp->if_flags |= IFF_UP;
1718 ifr.ifr_flags = ifp->if_flags;
1719 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1720 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1721 (struct ucred *)NULL);
1724 * Also send gratuitous ARPs to notify other nodes about
1725 * the address change.
1727 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1728 if (ifa->ifa_addr != NULL &&
1729 ifa->ifa_addr->sa_family == AF_INET)
1730 arp_ifinit(ifp, ifa);
1737 struct ifmultiaddr *
1738 ifmaof_ifpforaddr(sa, ifp)
1739 struct sockaddr *sa;
1742 struct ifmultiaddr *ifma;
1744 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1745 if (equal(ifma->ifma_addr, sa))
1752 * The name argument must be a pointer to storage which will last as
1753 * long as the interface does. For physical devices, the result of
1754 * device_get_name(dev) is a good choice and for pseudo-devices a
1755 * static string works well.
1758 if_initname(struct ifnet *ifp, const char *name, int unit)
1760 ifp->if_dname = name;
1761 ifp->if_dunit = unit;
1762 if (unit != IF_DUNIT_NONE)
1763 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
1765 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1769 if_printf(struct ifnet *ifp, const char *fmt, ...)
1774 retval = printf("%s: ", ifp->if_xname);
1775 __va_start(ap, fmt);
1776 retval += vprintf(fmt, ap);
1781 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1782 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");