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.20 2004/09/10 14:02:00 joerg 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 TAILQ_REMOVE(&ifnet, ifp, if_link);
328 * Delete Routes for a Network Interface
330 * Called for each routing entry via the rnh->rnh_walktree() call above
331 * to delete all route entries referencing a detaching network interface.
334 * rn pointer to node in the routing table
335 * arg argument passed to rnh->rnh_walktree() - detaching interface
339 * errno failed - reason indicated
344 struct radix_node *rn;
347 struct rtentry *rt = (struct rtentry *)rn;
348 struct ifnet *ifp = arg;
351 if (rt->rt_ifp == ifp) {
354 * Protect (sorta) against walktree recursion problems
357 if ((rt->rt_flags & RTF_UP) == 0)
360 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
361 rt_mask(rt), rt->rt_flags,
362 (struct rtentry **) NULL);
364 log(LOG_WARNING, "if_rtdel: error %d\n", err);
372 * Create a clone network interface.
375 if_clone_create(name, len)
379 struct if_clone *ifc;
381 int wildcard, bytoff, bitoff;
385 ifc = if_clone_lookup(name, &unit);
389 if (ifunit(name) != NULL)
393 wildcard = (unit < 0);
395 * Find a free unit if none was given.
398 while ((bytoff < ifc->ifc_bmlen)
399 && (ifc->ifc_units[bytoff] == 0xff))
401 if (bytoff >= ifc->ifc_bmlen)
403 while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
405 unit = (bytoff << 3) + bitoff;
408 if (unit > ifc->ifc_maxunit)
411 err = (*ifc->ifc_create)(ifc, unit);
417 bitoff = unit - (bytoff << 3);
421 * Allocate the unit in the bitmap.
423 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
424 ("%s: bit is already set", __func__));
425 ifc->ifc_units[bytoff] |= (1 << bitoff);
427 /* In the wildcard case, we need to update the name. */
429 for (dp = name; *dp != '\0'; dp++);
430 if (snprintf(dp, len - (dp-name), "%d", unit) >
431 len - (dp-name) - 1) {
433 * This can only be a programmer error and
434 * there's no straightforward way to recover if
437 panic("if_clone_create(): interface name too long");
442 EVENTHANDLER_INVOKE(if_clone_event, ifc);
448 * Destroy a clone network interface.
451 if_clone_destroy(name)
454 struct if_clone *ifc;
459 ifc = if_clone_lookup(name, &unit);
463 if (unit < ifc->ifc_minifs)
470 if (ifc->ifc_destroy == NULL)
473 (*ifc->ifc_destroy)(ifp);
476 * Compute offset in the bitmap and deallocate the unit.
479 bitoff = unit - (bytoff << 3);
480 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
481 ("%s: bit is already cleared", __func__));
482 ifc->ifc_units[bytoff] &= ~(1 << bitoff);
487 * Look up a network interface cloner.
490 if_clone_lookup(name, unitp)
494 struct if_clone *ifc;
498 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
499 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
500 if (ifc->ifc_name[i] != *cp)
505 ifc = LIST_NEXT(ifc, ifc_list);
509 return ((struct if_clone *)NULL);
515 for (i = 0; *cp != '\0'; cp++) {
516 if (*cp < '0' || *cp > '9') {
517 /* Bogus unit number. */
520 i = (i * 10) + (*cp - '0');
530 * Register a network interface cloner.
534 struct if_clone *ifc;
541 KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
542 ("%s: %s requested more units then allowed (%d > %d)",
543 __func__, ifc->ifc_name, ifc->ifc_minifs,
544 ifc->ifc_maxunit + 1));
546 * Compute bitmap size and allocate it.
548 maxclone = ifc->ifc_maxunit + 1;
550 if ((len << 3) < maxclone)
552 ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO);
553 ifc->ifc_bmlen = len;
555 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
558 for (unit = 0; unit < ifc->ifc_minifs; unit++) {
559 err = (*ifc->ifc_create)(ifc, unit);
561 ("%s: failed to create required interface %s%d",
562 __func__, ifc->ifc_name, unit));
564 /* Allocate the unit in the bitmap. */
566 bitoff = unit - (bytoff << 3);
567 ifc->ifc_units[bytoff] |= (1 << bitoff);
572 * Unregister a network interface cloner.
576 struct if_clone *ifc;
579 LIST_REMOVE(ifc, ifc_list);
580 free(ifc->ifc_units, M_CLONE);
585 * Provide list of interface cloners to userspace.
589 struct if_clonereq *ifcr;
591 char outbuf[IFNAMSIZ], *dst;
592 struct if_clone *ifc;
593 int count, error = 0;
595 ifcr->ifcr_total = if_cloners_count;
596 if ((dst = ifcr->ifcr_buffer) == NULL) {
597 /* Just asking how many there are. */
601 if (ifcr->ifcr_count < 0)
604 count = (if_cloners_count < ifcr->ifcr_count) ?
605 if_cloners_count : ifcr->ifcr_count;
607 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
608 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
609 strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
610 error = copyout(outbuf, dst, IFNAMSIZ);
619 * Locate an interface based on a complete address.
624 struct sockaddr *addr;
629 #define equal(a1, a2) \
630 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
631 TAILQ_FOREACH(ifp, &ifnet, if_link)
632 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
633 if (ifa->ifa_addr->sa_family != addr->sa_family)
635 if (equal(addr, ifa->ifa_addr))
637 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
638 /* IP6 doesn't have broadcast */
639 ifa->ifa_broadaddr->sa_len != 0 &&
640 equal(ifa->ifa_broadaddr, addr))
643 return ((struct ifaddr *)0);
646 * Locate the point to point interface with a given destination address.
650 ifa_ifwithdstaddr(addr)
651 struct sockaddr *addr;
656 TAILQ_FOREACH(ifp, &ifnet, if_link)
657 if (ifp->if_flags & IFF_POINTOPOINT)
658 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
659 if (ifa->ifa_addr->sa_family != addr->sa_family)
661 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
664 return ((struct ifaddr *)0);
668 * Find an interface on a specific network. If many, choice
669 * is most specific found.
673 struct sockaddr *addr;
677 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
678 u_int af = addr->sa_family;
679 char *addr_data = addr->sa_data, *cplim;
682 * AF_LINK addresses can be looked up directly by their index number,
683 * so do that if we can.
686 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
687 if (sdl->sdl_index && sdl->sdl_index <= if_index)
688 return (ifnet_addrs[sdl->sdl_index - 1]);
692 * Scan though each interface, looking for ones that have
693 * addresses in this address family.
695 TAILQ_FOREACH(ifp, &ifnet, if_link) {
696 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
697 char *cp, *cp2, *cp3;
699 if (ifa->ifa_addr->sa_family != af)
701 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
703 * This is a bit broken as it doesn't
704 * take into account that the remote end may
705 * be a single node in the network we are
707 * The trouble is that we don't know the
708 * netmask for the remote end.
710 if (ifa->ifa_dstaddr != 0
711 && equal(addr, ifa->ifa_dstaddr))
715 * if we have a special address handler,
716 * then use it instead of the generic one.
718 if (ifa->ifa_claim_addr) {
719 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
727 * Scan all the bits in the ifa's address.
728 * If a bit dissagrees with what we are
729 * looking for, mask it with the netmask
730 * to see if it really matters.
733 if (ifa->ifa_netmask == 0)
736 cp2 = ifa->ifa_addr->sa_data;
737 cp3 = ifa->ifa_netmask->sa_data;
738 cplim = ifa->ifa_netmask->sa_len
739 + (char *)ifa->ifa_netmask;
741 if ((*cp++ ^ *cp2++) & *cp3++)
742 goto next; /* next address! */
744 * If the netmask of what we just found
745 * is more specific than what we had before
746 * (if we had one) then remember the new one
747 * before continuing to search
748 * for an even better one.
750 if (ifa_maybe == 0 ||
751 rn_refines((caddr_t)ifa->ifa_netmask,
752 (caddr_t)ifa_maybe->ifa_netmask))
761 * Find an interface address specific to an interface best matching
765 ifaof_ifpforaddr(addr, ifp)
766 struct sockaddr *addr;
770 char *cp, *cp2, *cp3;
772 struct ifaddr *ifa_maybe = 0;
773 u_int af = addr->sa_family;
777 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
778 if (ifa->ifa_addr->sa_family != af)
782 if (ifa->ifa_netmask == 0) {
783 if (equal(addr, ifa->ifa_addr) ||
784 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
788 if (ifp->if_flags & IFF_POINTOPOINT) {
789 if (equal(addr, ifa->ifa_dstaddr))
793 cp2 = ifa->ifa_addr->sa_data;
794 cp3 = ifa->ifa_netmask->sa_data;
795 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
796 for (; cp3 < cplim; cp3++)
797 if ((*cp++ ^ *cp2++) & *cp3)
806 #include <net/route.h>
809 * Default action when installing a route with a Link Level gateway.
810 * Lookup an appropriate real ifa to point to.
811 * This should be moved to /sys/net/link.c eventually.
814 link_rtrequest(cmd, rt, info)
817 struct rt_addrinfo *info;
820 struct sockaddr *dst;
823 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
824 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
826 ifa = ifaof_ifpforaddr(dst, ifp);
831 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
832 ifa->ifa_rtrequest(cmd, rt, info);
837 * Mark an interface down and notify protocols of
839 * NOTE: must be called at splnet or eqivalent.
842 if_unroute(ifp, flag, fam)
848 ifp->if_flags &= ~flag;
849 getmicrotime(&ifp->if_lastchange);
850 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
851 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
852 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
853 if_qflush(&ifp->if_snd);
858 * Mark an interface up and notify protocols of
860 * NOTE: must be called at splnet or eqivalent.
863 if_route(ifp, flag, fam)
869 ifp->if_flags |= flag;
870 getmicrotime(&ifp->if_lastchange);
871 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
872 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
873 pfctlinput(PRC_IFUP, ifa->ifa_addr);
881 * Mark an interface down and notify protocols of
883 * NOTE: must be called at splnet or eqivalent.
890 if_unroute(ifp, IFF_UP, AF_UNSPEC);
894 * Mark an interface up and notify protocols of
896 * NOTE: must be called at splnet or eqivalent.
903 if_route(ifp, IFF_UP, AF_UNSPEC);
907 * Flush an interface queue.
916 while ((m = n) != 0) {
926 * Handle interface watchdog timer routines. Called
927 * from softclock, we decrement timers (if set) and
928 * call the appropriate interface routine on expiration.
937 TAILQ_FOREACH(ifp, &ifnet, if_link) {
938 if (ifp->if_timer == 0 || --ifp->if_timer)
940 if (ifp->if_watchdog)
941 (*ifp->if_watchdog)(ifp);
944 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
948 * Map interface name to
949 * interface structure pointer.
952 ifunit(const char *name)
957 * Search all the interfaces for this name/number
960 TAILQ_FOREACH(ifp, &ifnet, if_link) {
961 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
969 * Map interface name in a sockaddr_dl to
970 * interface structure pointer.
976 char ifname[IFNAMSIZ+1];
977 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
979 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
980 (sdl->sdl_nlen > IFNAMSIZ) )
984 * ifunit wants a null-terminated name. It may not be null-terminated
985 * in the sockaddr. We don't want to change the caller's sockaddr,
986 * and there might not be room to put the trailing null anyway, so we
987 * make a local copy that we know we can null terminate safely.
990 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
991 ifname[sdl->sdl_nlen] = '\0';
992 return ifunit(ifname);
1000 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
1008 size_t namelen, onamelen;
1009 char new_name[IFNAMSIZ];
1011 struct sockaddr_dl *sdl;
1017 return (ifconf(cmd, data, td));
1019 ifr = (struct ifreq *)data;
1024 if ((error = suser(td)) != 0)
1026 return ((cmd == SIOCIFCREATE) ?
1027 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1028 if_clone_destroy(ifr->ifr_name));
1030 case SIOCIFGCLONERS:
1031 return (if_clone_list((struct if_clonereq *)data));
1034 ifp = ifunit(ifr->ifr_name);
1040 ifr->ifr_flags = ifp->if_flags;
1041 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1045 ifr->ifr_reqcap = ifp->if_capabilities;
1046 ifr->ifr_curcap = ifp->if_capenable;
1050 ifr->ifr_metric = ifp->if_metric;
1054 ifr->ifr_mtu = ifp->if_mtu;
1058 ifr->ifr_phys = ifp->if_physical;
1065 new_flags = (ifr->ifr_flags & 0xffff) |
1066 (ifr->ifr_flagshigh << 16);
1067 if (ifp->if_flags & IFF_SMART) {
1068 /* Smart drivers twiddle their own routes */
1069 } else if (ifp->if_flags & IFF_UP &&
1070 (new_flags & IFF_UP) == 0) {
1074 } else if (new_flags & IFF_UP &&
1075 (ifp->if_flags & IFF_UP) == 0) {
1080 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1081 (new_flags &~ IFF_CANTCHANGE);
1082 if (new_flags & IFF_PPROMISC) {
1083 /* Permanently promiscuous mode requested */
1084 ifp->if_flags |= IFF_PROMISC;
1085 } else if (ifp->if_pcount == 0) {
1086 ifp->if_flags &= ~IFF_PROMISC;
1089 (void) (*ifp->if_ioctl)(ifp, cmd, data,
1090 td->td_proc->p_ucred);
1091 getmicrotime(&ifp->if_lastchange);
1098 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1100 (void) (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
1107 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1110 if (new_name[0] == '\0')
1112 if (ifunit(new_name) != NULL)
1115 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1117 /* Announce the departure of the interface. */
1118 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1120 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1121 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1122 /* XXX IFA_LOCK(ifa); */
1123 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1124 namelen = strlen(new_name);
1125 onamelen = sdl->sdl_nlen;
1127 * Move the address if needed. This is safe because we
1128 * allocate space for a name of length IFNAMSIZ when we
1129 * create this in if_attach().
1131 if (namelen != onamelen) {
1132 bcopy(sdl->sdl_data + onamelen,
1133 sdl->sdl_data + namelen, sdl->sdl_alen);
1135 bcopy(new_name, sdl->sdl_data, namelen);
1136 sdl->sdl_nlen = namelen;
1137 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1138 bzero(sdl->sdl_data, onamelen);
1139 while (namelen != 0)
1140 sdl->sdl_data[--namelen] = 0xff;
1141 /* XXX IFA_UNLOCK(ifa) */
1143 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1145 /* Announce the return of the interface. */
1146 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1153 ifp->if_metric = ifr->ifr_metric;
1154 getmicrotime(&ifp->if_lastchange);
1163 error = (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
1165 getmicrotime(&ifp->if_lastchange);
1170 u_long oldmtu = ifp->if_mtu;
1175 if (ifp->if_ioctl == NULL)
1176 return (EOPNOTSUPP);
1177 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1179 error = (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
1181 getmicrotime(&ifp->if_lastchange);
1185 * If the link MTU changed, do network layer specific procedure.
1187 if (ifp->if_mtu != oldmtu) {
1201 /* Don't allow group membership on non-multicast interfaces. */
1202 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1205 /* Don't let users screw up protocols' entries. */
1206 if (ifr->ifr_addr.sa_family != AF_LINK)
1209 if (cmd == SIOCADDMULTI) {
1210 struct ifmultiaddr *ifma;
1211 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1213 error = if_delmulti(ifp, &ifr->ifr_addr);
1216 getmicrotime(&ifp->if_lastchange);
1219 case SIOCSIFPHYADDR:
1220 case SIOCDIFPHYADDR:
1222 case SIOCSIFPHYADDR_IN6:
1224 case SIOCSLIFPHYADDR:
1226 case SIOCSIFGENERIC:
1230 if (ifp->if_ioctl == 0)
1231 return (EOPNOTSUPP);
1232 error = (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
1234 getmicrotime(&ifp->if_lastchange);
1238 ifs = (struct ifstat *)data;
1239 ifs->ascii[0] = '\0';
1241 case SIOCGIFPSRCADDR:
1242 case SIOCGIFPDSTADDR:
1243 case SIOCGLIFPHYADDR:
1245 case SIOCGIFGENERIC:
1246 if (ifp->if_ioctl == 0)
1247 return (EOPNOTSUPP);
1248 return ((*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred));
1254 return if_setlladdr(ifp,
1255 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1258 oif_flags = ifp->if_flags;
1259 if (so->so_proto == 0)
1260 return (EOPNOTSUPP);
1262 error = so_pru_control(so, cmd, data, ifp, td);
1269 case SIOCSIFDSTADDR:
1271 case SIOCSIFBRDADDR:
1272 case SIOCSIFNETMASK:
1273 #if BYTE_ORDER != BIG_ENDIAN
1274 if (ifr->ifr_addr.sa_family == 0 &&
1275 ifr->ifr_addr.sa_len < 16) {
1276 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1277 ifr->ifr_addr.sa_len = 16;
1280 if (ifr->ifr_addr.sa_len == 0)
1281 ifr->ifr_addr.sa_len = 16;
1289 case OSIOCGIFDSTADDR:
1290 cmd = SIOCGIFDSTADDR;
1293 case OSIOCGIFBRDADDR:
1294 cmd = SIOCGIFBRDADDR;
1297 case OSIOCGIFNETMASK:
1298 cmd = SIOCGIFNETMASK;
1300 error = so_pru_control(so, cmd, data, ifp, td);
1304 case OSIOCGIFDSTADDR:
1305 case OSIOCGIFBRDADDR:
1306 case OSIOCGIFNETMASK:
1307 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1311 #endif /* COMPAT_43 */
1313 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1315 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1316 if (ifp->if_flags & IFF_UP) {
1330 * Set/clear promiscuous mode on interface ifp based on the truth value
1331 * of pswitch. The calls are reference counted so that only the first
1332 * "on" request actually has an effect, as does the final "off" request.
1333 * Results are undefined if the "off" and "on" requests are not matched.
1336 ifpromisc(ifp, pswitch)
1344 oldflags = ifp->if_flags;
1345 if (ifp->if_flags & IFF_PPROMISC) {
1346 /* Do nothing if device is in permanently promiscuous mode */
1347 ifp->if_pcount += pswitch ? 1 : -1;
1352 * If the device is not configured up, we cannot put it in
1355 if ((ifp->if_flags & IFF_UP) == 0)
1357 if (ifp->if_pcount++ != 0)
1359 ifp->if_flags |= IFF_PROMISC;
1360 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1363 if (--ifp->if_pcount > 0)
1365 ifp->if_flags &= ~IFF_PROMISC;
1366 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1369 ifr.ifr_flags = ifp->if_flags;
1370 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1371 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1372 (struct ucred *)NULL);
1376 ifp->if_flags = oldflags;
1381 * Return interface configuration
1382 * of system. List may be used
1383 * in later ioctl's (above) to get
1384 * other information.
1388 ifconf(u_long cmd, caddr_t data, struct thread *td)
1390 struct ifconf *ifc = (struct ifconf *)data;
1393 struct sockaddr *sa;
1394 struct ifreq ifr, *ifrp;
1395 int space = ifc->ifc_len, error = 0;
1397 ifrp = ifc->ifc_req;
1398 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1401 if (space <= sizeof (ifr))
1403 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1404 >= sizeof(ifr.ifr_name)) {
1405 error = ENAMETOOLONG;
1410 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1411 if (space <= sizeof(ifr))
1414 if (td->td_proc->p_ucred->cr_prison &&
1419 if (cmd == OSIOCGIFCONF) {
1420 struct osockaddr *osa =
1421 (struct osockaddr *)&ifr.ifr_addr;
1423 osa->sa_family = sa->sa_family;
1424 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1429 if (sa->sa_len <= sizeof(*sa)) {
1431 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1435 if (space < sizeof (ifr) + sa->sa_len -
1438 space -= sa->sa_len - sizeof(*sa);
1439 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1440 sizeof (ifr.ifr_name));
1442 error = copyout((caddr_t)sa,
1443 (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1444 ifrp = (struct ifreq *)
1445 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1449 space -= sizeof (ifr);
1454 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1455 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1459 space -= sizeof (ifr);
1463 ifc->ifc_len -= space;
1468 * Just like if_promisc(), but for all-multicast-reception mode.
1471 if_allmulti(ifp, onswitch)
1480 if (ifp->if_amcount++ == 0) {
1481 ifp->if_flags |= IFF_ALLMULTI;
1482 ifr.ifr_flags = ifp->if_flags;
1483 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1484 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1485 (struct ucred *)NULL);
1488 if (ifp->if_amcount > 1) {
1491 ifp->if_amcount = 0;
1492 ifp->if_flags &= ~IFF_ALLMULTI;
1493 ifr.ifr_flags = ifp->if_flags;
1494 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1495 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1496 (struct ucred *)NULL);
1507 * Add a multicast listenership to the interface in question.
1508 * The link layer provides a routine which converts
1511 if_addmulti(ifp, sa, retifma)
1512 struct ifnet *ifp; /* interface to manipulate */
1513 struct sockaddr *sa; /* address to add */
1514 struct ifmultiaddr **retifma;
1516 struct sockaddr *llsa, *dupsa;
1518 struct ifmultiaddr *ifma;
1521 * If the matching multicast address already exists
1522 * then don't add a new one, just add a reference
1524 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1525 if (equal(sa, ifma->ifma_addr)) {
1526 ifma->ifma_refcount++;
1534 * Give the link layer a chance to accept/reject it, and also
1535 * find out which AF_LINK address this maps to, if it isn't one
1538 if (ifp->if_resolvemulti) {
1539 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1540 if (error) return error;
1545 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1546 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1547 bcopy(sa, dupsa, sa->sa_len);
1549 ifma->ifma_addr = dupsa;
1550 ifma->ifma_lladdr = llsa;
1551 ifma->ifma_ifp = ifp;
1552 ifma->ifma_refcount = 1;
1553 ifma->ifma_protospec = 0;
1554 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1557 * Some network interfaces can scan the address list at
1558 * interrupt time; lock them out.
1561 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1566 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1567 if (equal(ifma->ifma_addr, llsa))
1571 ifma->ifma_refcount++;
1573 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1574 M_IFMADDR, M_WAITOK);
1575 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1576 M_IFMADDR, M_WAITOK);
1577 bcopy(llsa, dupsa, llsa->sa_len);
1578 ifma->ifma_addr = dupsa;
1579 ifma->ifma_ifp = ifp;
1580 ifma->ifma_refcount = 1;
1582 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1587 * We are certain we have added something, so call down to the
1588 * interface to let them know about it.
1591 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, (struct ucred *)NULL);
1598 * Remove a reference to a multicast address on this interface. Yell
1599 * if the request does not match an existing membership.
1602 if_delmulti(ifp, sa)
1604 struct sockaddr *sa;
1606 struct ifmultiaddr *ifma;
1609 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1610 if (equal(sa, ifma->ifma_addr))
1615 if (ifma->ifma_refcount > 1) {
1616 ifma->ifma_refcount--;
1620 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1621 sa = ifma->ifma_lladdr;
1623 LIST_REMOVE(ifma, ifma_link);
1625 * Make sure the interface driver is notified
1626 * in the case of a link layer mcast group being left.
1628 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0)
1629 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1631 free(ifma->ifma_addr, M_IFMADDR);
1632 free(ifma, M_IFMADDR);
1637 * Now look for the link-layer address which corresponds to
1638 * this network address. It had been squirreled away in
1639 * ifma->ifma_lladdr for this purpose (so we don't have
1640 * to call ifp->if_resolvemulti() again), and we saved that
1641 * value in sa above. If some nasty deleted the
1642 * link-layer address out from underneath us, we can deal because
1643 * the address we stored was is not the same as the one which was
1644 * in the record for the link-layer address. (So we don't complain
1647 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1648 if (equal(sa, ifma->ifma_addr))
1653 if (ifma->ifma_refcount > 1) {
1654 ifma->ifma_refcount--;
1659 LIST_REMOVE(ifma, ifma_link);
1660 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1662 free(ifma->ifma_addr, M_IFMADDR);
1663 free(sa, M_IFMADDR);
1664 free(ifma, M_IFMADDR);
1670 * Set the link layer address on an interface.
1672 * At this time we only support certain types of interfaces,
1673 * and we don't allow the length of the address to change.
1676 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1678 struct sockaddr_dl *sdl;
1682 ifa = ifnet_addrs[ifp->if_index - 1];
1685 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1688 if (len != sdl->sdl_alen) /* don't allow length to change */
1690 switch (ifp->if_type) {
1691 case IFT_ETHER: /* these types use struct arpcom */
1696 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1699 bcopy(lladdr, LLADDR(sdl), len);
1705 * If the interface is already up, we need
1706 * to re-init it in order to reprogram its
1709 if ((ifp->if_flags & IFF_UP) != 0) {
1710 ifp->if_flags &= ~IFF_UP;
1711 ifr.ifr_flags = ifp->if_flags;
1712 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1713 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1714 (struct ucred *)NULL);
1715 ifp->if_flags |= IFF_UP;
1716 ifr.ifr_flags = ifp->if_flags;
1717 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1718 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1719 (struct ucred *)NULL);
1722 * Also send gratuitous ARPs to notify other nodes about
1723 * the address change.
1725 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1726 if (ifa->ifa_addr != NULL &&
1727 ifa->ifa_addr->sa_family == AF_INET)
1728 arp_ifinit(ifp, ifa);
1735 struct ifmultiaddr *
1736 ifmaof_ifpforaddr(sa, ifp)
1737 struct sockaddr *sa;
1740 struct ifmultiaddr *ifma;
1742 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1743 if (equal(ifma->ifma_addr, sa))
1750 * The name argument must be a pointer to storage which will last as
1751 * long as the interface does. For physical devices, the result of
1752 * device_get_name(dev) is a good choice and for pseudo-devices a
1753 * static string works well.
1756 if_initname(struct ifnet *ifp, const char *name, int unit)
1758 ifp->if_dname = name;
1759 ifp->if_dunit = unit;
1760 if (unit != IF_DUNIT_NONE)
1761 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
1763 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1767 if_printf(struct ifnet *ifp, const char *fmt, ...)
1772 retval = printf("%s: ", ifp->if_xname);
1773 __va_start(ap, fmt);
1774 retval += vprintf(fmt, ap);
1779 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1780 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");