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.85.2.23 2003/04/15 18:11:19 fjoe Exp $
35 * $DragonFly: src/sys/net/if.c,v 1.12 2004/01/06 03:17:25 dillon 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/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/protosw.h>
50 #include <sys/kernel.h>
51 #include <sys/sockio.h>
52 #include <sys/syslog.h>
53 #include <sys/sysctl.h>
56 #include <net/if_arp.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/if_var.h>
60 #include <net/radix.h>
61 #include <net/route.h>
62 #include <machine/stdarg.h>
64 #if defined(INET) || defined(INET6)
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/if_ether.h>
70 #include <machine/clock.h> /* XXX: temporal workaround for fxp issue */
71 #include <netinet6/in6_var.h>
72 #include <netinet6/in6_ifattach.h>
76 #if defined(COMPAT_43)
77 #include <emulation/43bsd/43bsd_socket.h>
78 #endif /* COMPAT_43 */
81 * System initialization
84 static int ifconf (u_long, caddr_t);
85 static void ifinit (void *);
86 static void if_qflush (struct ifqueue *);
87 static void if_slowtimo (void *);
88 static void link_rtrequest (int, struct rtentry *, struct rt_addrinfo *);
89 static int if_rtdel (struct radix_node *, void *);
91 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
93 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
94 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
95 MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
97 int ifqmaxlen = IFQ_MAXLEN;
98 struct ifnethead ifnet; /* depend on static init XXX */
102 * XXX: declare here to avoid to include many inet6 related files..
103 * should be more generalized?
105 extern void nd6_setmtu (struct ifnet *);
108 struct if_clone *if_clone_lookup (const char *, int *);
109 int if_clone_list (struct if_clonereq *);
111 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
112 int if_cloners_count;
115 * Network interface utility routines.
117 * Routines with ifa_ifwith* names take sockaddr *'s as
129 TAILQ_FOREACH(ifp, &ifnet, if_link) {
130 if (ifp->if_snd.ifq_maxlen == 0) {
131 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
132 ifp->if_snd.ifq_maxlen = ifqmaxlen;
140 struct ifaddr **ifnet_addrs;
141 struct ifnet **ifindex2ifnet = NULL;
145 * Attach an interface to the
146 * list of "active" interfaces.
152 unsigned socksize, ifasize;
153 int namelen, masklen;
154 struct sockaddr_dl *sdl;
156 static int if_indexlim = 8;
164 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
165 ifp->if_index = ++if_index;
168 * The old code would work if the interface passed a pre-existing
169 * chain of ifaddrs to this code. We don't trust our callers to
170 * properly initialize the tailq, however, so we no longer allow
171 * this unlikely case.
173 TAILQ_INIT(&ifp->if_addrhead);
174 TAILQ_INIT(&ifp->if_prefixhead);
175 LIST_INIT(&ifp->if_multiaddrs);
176 getmicrotime(&ifp->if_lastchange);
177 if (ifnet_addrs == 0 || if_index >= if_indexlim) {
178 unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
179 caddr_t q = malloc(n, M_IFADDR, M_WAITOK);
182 bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
183 free((caddr_t)ifnet_addrs, M_IFADDR);
185 ifnet_addrs = (struct ifaddr **)q;
187 /* grow ifindex2ifnet */
188 n = if_indexlim * sizeof(struct ifnet *);
189 q = malloc(n, M_IFADDR, M_WAITOK);
192 bcopy((caddr_t)ifindex2ifnet, q, n/2);
193 free((caddr_t)ifindex2ifnet, M_IFADDR);
195 ifindex2ifnet = (struct ifnet **)q;
198 ifindex2ifnet[if_index] = ifp;
201 * create a Link Level name for this device
203 namelen = strlen(ifp->if_xname);
204 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
205 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
206 socksize = masklen + ifp->if_addrlen;
207 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
208 if (socksize < sizeof(*sdl))
209 socksize = sizeof(*sdl);
210 socksize = ROUNDUP(socksize);
211 ifasize = sizeof(*ifa) + 2 * socksize;
212 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
214 bzero((caddr_t)ifa, ifasize);
215 sdl = (struct sockaddr_dl *)(ifa + 1);
216 sdl->sdl_len = socksize;
217 sdl->sdl_family = AF_LINK;
218 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
219 sdl->sdl_nlen = namelen;
220 sdl->sdl_index = ifp->if_index;
221 sdl->sdl_type = ifp->if_type;
222 ifnet_addrs[if_index - 1] = ifa;
224 ifa->ifa_rtrequest = link_rtrequest;
225 ifa->ifa_addr = (struct sockaddr *)sdl;
226 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
227 ifa->ifa_netmask = (struct sockaddr *)sdl;
228 sdl->sdl_len = masklen;
230 sdl->sdl_data[--namelen] = 0xff;
231 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
234 /* Announce the interface. */
235 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
239 * Detach an interface, removing it from the
240 * list of "active" interfaces.
247 struct radix_node_head *rnh;
252 * Remove routes and flush queues.
258 * Remove address from ifnet_addrs[] and maybe decrement if_index.
259 * Clean up all addresses.
261 ifnet_addrs[ifp->if_index - 1] = 0;
262 while (if_index > 0 && ifnet_addrs[if_index - 1] == 0)
265 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
266 ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
268 /* XXX: Ugly!! ad hoc just for INET */
269 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
270 struct ifaliasreq ifr;
272 bzero(&ifr, sizeof(ifr));
273 ifr.ifra_addr = *ifa->ifa_addr;
274 if (ifa->ifa_dstaddr)
275 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
276 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
282 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
284 /* ifp_addrhead is already updated */
288 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
294 * Remove all IPv6 kernel structs related to ifp. This should be done
295 * before removing routing entries below, since IPv6 interface direct
296 * routes are expected to be removed by the IPv6-specific kernel API.
297 * Otherwise, the kernel will detect some inconsistency and bark it.
303 * Delete all remaining routes using this interface
304 * Unfortuneatly the only way to do this is to slog through
305 * the entire routing table looking for routes which point
306 * to this interface...oh well...
308 for (i = 1; i <= AF_MAX; i++) {
309 if ((rnh = rt_tables[i]) == NULL)
311 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
314 /* Announce that the interface is gone. */
315 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
317 TAILQ_REMOVE(&ifnet, ifp, if_link);
322 * Delete Routes for a Network Interface
324 * Called for each routing entry via the rnh->rnh_walktree() call above
325 * to delete all route entries referencing a detaching network interface.
328 * rn pointer to node in the routing table
329 * arg argument passed to rnh->rnh_walktree() - detaching interface
333 * errno failed - reason indicated
338 struct radix_node *rn;
341 struct rtentry *rt = (struct rtentry *)rn;
342 struct ifnet *ifp = arg;
345 if (rt->rt_ifp == ifp) {
348 * Protect (sorta) against walktree recursion problems
351 if ((rt->rt_flags & RTF_UP) == 0)
354 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
355 rt_mask(rt), rt->rt_flags,
356 (struct rtentry **) NULL);
358 log(LOG_WARNING, "if_rtdel: error %d\n", err);
366 * Create a clone network interface.
369 if_clone_create(name, len)
373 struct if_clone *ifc;
375 int wildcard, bytoff, bitoff;
379 ifc = if_clone_lookup(name, &unit);
383 if (ifunit(name) != NULL)
387 wildcard = (unit < 0);
389 * Find a free unit if none was given.
392 while ((bytoff < ifc->ifc_bmlen)
393 && (ifc->ifc_units[bytoff] == 0xff))
395 if (bytoff >= ifc->ifc_bmlen)
397 while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
399 unit = (bytoff << 3) + bitoff;
402 if (unit > ifc->ifc_maxunit)
405 err = (*ifc->ifc_create)(ifc, unit);
411 bitoff = unit - (bytoff << 3);
415 * Allocate the unit in the bitmap.
417 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
418 ("%s: bit is already set", __func__));
419 ifc->ifc_units[bytoff] |= (1 << bitoff);
421 /* In the wildcard case, we need to update the name. */
423 for (dp = name; *dp != '\0'; dp++);
424 if (snprintf(dp, len - (dp-name), "%d", unit) >
425 len - (dp-name) - 1) {
427 * This can only be a programmer error and
428 * there's no straightforward way to recover if
431 panic("if_clone_create(): interface name too long");
440 * Destroy a clone network interface.
443 if_clone_destroy(name)
446 struct if_clone *ifc;
451 ifc = if_clone_lookup(name, &unit);
455 if (unit < ifc->ifc_minifs)
462 if (ifc->ifc_destroy == NULL)
465 (*ifc->ifc_destroy)(ifp);
468 * Compute offset in the bitmap and deallocate the unit.
471 bitoff = unit - (bytoff << 3);
472 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
473 ("%s: bit is already cleared", __func__));
474 ifc->ifc_units[bytoff] &= ~(1 << bitoff);
479 * Look up a network interface cloner.
482 if_clone_lookup(name, unitp)
486 struct if_clone *ifc;
490 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
491 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
492 if (ifc->ifc_name[i] != *cp)
497 ifc = LIST_NEXT(ifc, ifc_list);
501 return ((struct if_clone *)NULL);
507 for (i = 0; *cp != '\0'; cp++) {
508 if (*cp < '0' || *cp > '9') {
509 /* Bogus unit number. */
512 i = (i * 10) + (*cp - '0');
522 * Register a network interface cloner.
526 struct if_clone *ifc;
533 KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
534 ("%s: %s requested more units then allowed (%d > %d)",
535 __func__, ifc->ifc_name, ifc->ifc_minifs,
536 ifc->ifc_maxunit + 1));
538 * Compute bitmap size and allocate it.
540 maxclone = ifc->ifc_maxunit + 1;
542 if ((len << 3) < maxclone)
544 ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO);
545 ifc->ifc_bmlen = len;
547 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
550 for (unit = 0; unit < ifc->ifc_minifs; unit++) {
551 err = (*ifc->ifc_create)(ifc, unit);
553 ("%s: failed to create required interface %s%d",
554 __func__, ifc->ifc_name, unit));
556 /* Allocate the unit in the bitmap. */
558 bitoff = unit - (bytoff << 3);
559 ifc->ifc_units[bytoff] |= (1 << bitoff);
564 * Unregister a network interface cloner.
568 struct if_clone *ifc;
571 LIST_REMOVE(ifc, ifc_list);
572 free(ifc->ifc_units, M_CLONE);
577 * Provide list of interface cloners to userspace.
581 struct if_clonereq *ifcr;
583 char outbuf[IFNAMSIZ], *dst;
584 struct if_clone *ifc;
585 int count, error = 0;
587 ifcr->ifcr_total = if_cloners_count;
588 if ((dst = ifcr->ifcr_buffer) == NULL) {
589 /* Just asking how many there are. */
593 if (ifcr->ifcr_count < 0)
596 count = (if_cloners_count < ifcr->ifcr_count) ?
597 if_cloners_count : ifcr->ifcr_count;
599 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
600 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
601 strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
602 error = copyout(outbuf, dst, IFNAMSIZ);
611 * Locate an interface based on a complete address.
616 struct sockaddr *addr;
621 #define equal(a1, a2) \
622 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
623 TAILQ_FOREACH(ifp, &ifnet, if_link)
624 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
625 if (ifa->ifa_addr->sa_family != addr->sa_family)
627 if (equal(addr, ifa->ifa_addr))
629 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
630 /* IP6 doesn't have broadcast */
631 ifa->ifa_broadaddr->sa_len != 0 &&
632 equal(ifa->ifa_broadaddr, addr))
635 return ((struct ifaddr *)0);
638 * Locate the point to point interface with a given destination address.
642 ifa_ifwithdstaddr(addr)
643 struct sockaddr *addr;
648 TAILQ_FOREACH(ifp, &ifnet, if_link)
649 if (ifp->if_flags & IFF_POINTOPOINT)
650 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
651 if (ifa->ifa_addr->sa_family != addr->sa_family)
653 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
656 return ((struct ifaddr *)0);
660 * Find an interface on a specific network. If many, choice
661 * is most specific found.
665 struct sockaddr *addr;
669 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
670 u_int af = addr->sa_family;
671 char *addr_data = addr->sa_data, *cplim;
674 * AF_LINK addresses can be looked up directly by their index number,
675 * so do that if we can.
678 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
679 if (sdl->sdl_index && sdl->sdl_index <= if_index)
680 return (ifnet_addrs[sdl->sdl_index - 1]);
684 * Scan though each interface, looking for ones that have
685 * addresses in this address family.
687 TAILQ_FOREACH(ifp, &ifnet, if_link) {
688 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
689 char *cp, *cp2, *cp3;
691 if (ifa->ifa_addr->sa_family != af)
693 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
695 * This is a bit broken as it doesn't
696 * take into account that the remote end may
697 * be a single node in the network we are
699 * The trouble is that we don't know the
700 * netmask for the remote end.
702 if (ifa->ifa_dstaddr != 0
703 && equal(addr, ifa->ifa_dstaddr))
707 * if we have a special address handler,
708 * then use it instead of the generic one.
710 if (ifa->ifa_claim_addr) {
711 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
719 * Scan all the bits in the ifa's address.
720 * If a bit dissagrees with what we are
721 * looking for, mask it with the netmask
722 * to see if it really matters.
725 if (ifa->ifa_netmask == 0)
728 cp2 = ifa->ifa_addr->sa_data;
729 cp3 = ifa->ifa_netmask->sa_data;
730 cplim = ifa->ifa_netmask->sa_len
731 + (char *)ifa->ifa_netmask;
733 if ((*cp++ ^ *cp2++) & *cp3++)
734 goto next; /* next address! */
736 * If the netmask of what we just found
737 * is more specific than what we had before
738 * (if we had one) then remember the new one
739 * before continuing to search
740 * for an even better one.
742 if (ifa_maybe == 0 ||
743 rn_refines((caddr_t)ifa->ifa_netmask,
744 (caddr_t)ifa_maybe->ifa_netmask))
753 * Find an interface address specific to an interface best matching
757 ifaof_ifpforaddr(addr, ifp)
758 struct sockaddr *addr;
762 char *cp, *cp2, *cp3;
764 struct ifaddr *ifa_maybe = 0;
765 u_int af = addr->sa_family;
769 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
770 if (ifa->ifa_addr->sa_family != af)
774 if (ifa->ifa_netmask == 0) {
775 if (equal(addr, ifa->ifa_addr) ||
776 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
780 if (ifp->if_flags & IFF_POINTOPOINT) {
781 if (equal(addr, ifa->ifa_dstaddr))
785 cp2 = ifa->ifa_addr->sa_data;
786 cp3 = ifa->ifa_netmask->sa_data;
787 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
788 for (; cp3 < cplim; cp3++)
789 if ((*cp++ ^ *cp2++) & *cp3)
798 #include <net/route.h>
801 * Default action when installing a route with a Link Level gateway.
802 * Lookup an appropriate real ifa to point to.
803 * This should be moved to /sys/net/link.c eventually.
806 link_rtrequest(cmd, rt, info)
809 struct rt_addrinfo *info;
812 struct sockaddr *dst;
815 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
816 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
818 ifa = ifaof_ifpforaddr(dst, ifp);
823 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
824 ifa->ifa_rtrequest(cmd, rt, info);
829 * Mark an interface down and notify protocols of
831 * NOTE: must be called at splnet or eqivalent.
834 if_unroute(ifp, flag, fam)
840 ifp->if_flags &= ~flag;
841 getmicrotime(&ifp->if_lastchange);
842 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
843 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
844 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
845 if_qflush(&ifp->if_snd);
850 * Mark an interface up and notify protocols of
852 * NOTE: must be called at splnet or eqivalent.
855 if_route(ifp, flag, fam)
861 ifp->if_flags |= flag;
862 getmicrotime(&ifp->if_lastchange);
863 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
864 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
865 pfctlinput(PRC_IFUP, ifa->ifa_addr);
873 * Mark an interface down and notify protocols of
875 * NOTE: must be called at splnet or eqivalent.
882 if_unroute(ifp, IFF_UP, AF_UNSPEC);
886 * Mark an interface up and notify protocols of
888 * NOTE: must be called at splnet or eqivalent.
895 if_route(ifp, IFF_UP, AF_UNSPEC);
899 * Flush an interface queue.
908 while ((m = n) != 0) {
918 * Handle interface watchdog timer routines. Called
919 * from softclock, we decrement timers (if set) and
920 * call the appropriate interface routine on expiration.
929 TAILQ_FOREACH(ifp, &ifnet, if_link) {
930 if (ifp->if_timer == 0 || --ifp->if_timer)
932 if (ifp->if_watchdog)
933 (*ifp->if_watchdog)(ifp);
936 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
940 * Map interface name to
941 * interface structure pointer.
944 ifunit(const char *name)
949 * Search all the interfaces for this name/number
952 TAILQ_FOREACH(ifp, &ifnet, if_link) {
953 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
961 * Map interface name in a sockaddr_dl to
962 * interface structure pointer.
968 char ifname[IFNAMSIZ+1];
969 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
971 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
972 (sdl->sdl_nlen > IFNAMSIZ) )
976 * ifunit wants a null-terminated name. It may not be null-terminated
977 * in the sockaddr. We don't want to change the caller's sockaddr,
978 * and there might not be room to put the trailing null anyway, so we
979 * make a local copy that we know we can null terminate safely.
982 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
983 ifname[sdl->sdl_nlen] = '\0';
984 return ifunit(ifname);
992 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
1005 return (ifconf(cmd, data));
1007 ifr = (struct ifreq *)data;
1012 if ((error = suser(td)) != 0)
1014 return ((cmd == SIOCIFCREATE) ?
1015 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1016 if_clone_destroy(ifr->ifr_name));
1018 case SIOCIFGCLONERS:
1019 return (if_clone_list((struct if_clonereq *)data));
1022 ifp = ifunit(ifr->ifr_name);
1028 ifr->ifr_flags = ifp->if_flags;
1029 ifr->ifr_flagshigh = ifp->if_ipending >> 16;
1033 ifr->ifr_reqcap = ifp->if_capabilities;
1034 ifr->ifr_curcap = ifp->if_capenable;
1038 ifr->ifr_metric = ifp->if_metric;
1042 ifr->ifr_mtu = ifp->if_mtu;
1046 ifr->ifr_phys = ifp->if_physical;
1053 new_flags = (ifr->ifr_flags & 0xffff) |
1054 (ifr->ifr_flagshigh << 16);
1055 if (ifp->if_flags & IFF_SMART) {
1056 /* Smart drivers twiddle their own routes */
1057 } else if (ifp->if_flags & IFF_UP &&
1058 (new_flags & IFF_UP) == 0) {
1062 } else if (new_flags & IFF_UP &&
1063 (ifp->if_flags & IFF_UP) == 0) {
1068 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1069 (new_flags &~ IFF_CANTCHANGE);
1070 ifp->if_ipending = (ifp->if_ipending & IFF_CANTCHANGE) |
1071 (new_flags &~ IFF_CANTCHANGE);
1072 if (new_flags & IFF_PPROMISC) {
1073 /* Permanently promiscuous mode requested */
1074 ifp->if_flags |= IFF_PROMISC;
1075 } else if (ifp->if_pcount == 0) {
1076 ifp->if_flags &= ~IFF_PROMISC;
1079 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1080 getmicrotime(&ifp->if_lastchange);
1087 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1089 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1096 ifp->if_metric = ifr->ifr_metric;
1097 getmicrotime(&ifp->if_lastchange);
1106 error = (*ifp->if_ioctl)(ifp, cmd, data);
1108 getmicrotime(&ifp->if_lastchange);
1113 u_long oldmtu = ifp->if_mtu;
1118 if (ifp->if_ioctl == NULL)
1119 return (EOPNOTSUPP);
1120 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1122 error = (*ifp->if_ioctl)(ifp, cmd, data);
1124 getmicrotime(&ifp->if_lastchange);
1128 * If the link MTU changed, do network layer specific procedure.
1130 if (ifp->if_mtu != oldmtu) {
1144 /* Don't allow group membership on non-multicast interfaces. */
1145 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1148 /* Don't let users screw up protocols' entries. */
1149 if (ifr->ifr_addr.sa_family != AF_LINK)
1152 if (cmd == SIOCADDMULTI) {
1153 struct ifmultiaddr *ifma;
1154 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1156 error = if_delmulti(ifp, &ifr->ifr_addr);
1159 getmicrotime(&ifp->if_lastchange);
1162 case SIOCSIFPHYADDR:
1163 case SIOCDIFPHYADDR:
1165 case SIOCSIFPHYADDR_IN6:
1167 case SIOCSLIFPHYADDR:
1169 case SIOCSIFGENERIC:
1173 if (ifp->if_ioctl == 0)
1174 return (EOPNOTSUPP);
1175 error = (*ifp->if_ioctl)(ifp, cmd, data);
1177 getmicrotime(&ifp->if_lastchange);
1181 ifs = (struct ifstat *)data;
1182 ifs->ascii[0] = '\0';
1184 case SIOCGIFPSRCADDR:
1185 case SIOCGIFPDSTADDR:
1186 case SIOCGLIFPHYADDR:
1188 case SIOCGIFGENERIC:
1189 if (ifp->if_ioctl == 0)
1190 return (EOPNOTSUPP);
1191 return ((*ifp->if_ioctl)(ifp, cmd, data));
1197 return if_setlladdr(ifp,
1198 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1201 oif_flags = ifp->if_flags;
1202 if (so->so_proto == 0)
1203 return (EOPNOTSUPP);
1205 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
1214 case SIOCSIFDSTADDR:
1216 case SIOCSIFBRDADDR:
1217 case SIOCSIFNETMASK:
1218 #if BYTE_ORDER != BIG_ENDIAN
1219 if (ifr->ifr_addr.sa_family == 0 &&
1220 ifr->ifr_addr.sa_len < 16) {
1221 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1222 ifr->ifr_addr.sa_len = 16;
1225 if (ifr->ifr_addr.sa_len == 0)
1226 ifr->ifr_addr.sa_len = 16;
1234 case OSIOCGIFDSTADDR:
1235 cmd = SIOCGIFDSTADDR;
1238 case OSIOCGIFBRDADDR:
1239 cmd = SIOCGIFBRDADDR;
1242 case OSIOCGIFNETMASK:
1243 cmd = SIOCGIFNETMASK;
1245 error = ((*so->so_proto->pr_usrreqs->pru_control)
1246 (so, cmd, data, ifp, td));
1250 case OSIOCGIFDSTADDR:
1251 case OSIOCGIFBRDADDR:
1252 case OSIOCGIFNETMASK:
1253 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1257 #endif /* COMPAT_43 */
1259 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1261 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1262 if (ifp->if_flags & IFF_UP) {
1276 * Set/clear promiscuous mode on interface ifp based on the truth value
1277 * of pswitch. The calls are reference counted so that only the first
1278 * "on" request actually has an effect, as does the final "off" request.
1279 * Results are undefined if the "off" and "on" requests are not matched.
1282 ifpromisc(ifp, pswitch)
1290 oldflags = ifp->if_flags;
1291 if (ifp->if_ipending & IFF_PPROMISC) {
1292 /* Do nothing if device is in permanently promiscuous mode */
1293 ifp->if_pcount += pswitch ? 1 : -1;
1298 * If the device is not configured up, we cannot put it in
1301 if ((ifp->if_flags & IFF_UP) == 0)
1303 if (ifp->if_pcount++ != 0)
1305 ifp->if_flags |= IFF_PROMISC;
1306 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1309 if (--ifp->if_pcount > 0)
1311 ifp->if_flags &= ~IFF_PROMISC;
1312 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1315 ifr.ifr_flags = ifp->if_flags;
1316 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1317 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1321 ifp->if_flags = oldflags;
1326 * Return interface configuration
1327 * of system. List may be used
1328 * in later ioctl's (above) to get
1329 * other information.
1333 ifconf(u_long cmd, caddr_t data)
1335 struct ifconf *ifc = (struct ifconf *)data;
1338 struct sockaddr *sa;
1339 struct ifreq ifr, *ifrp;
1340 int space = ifc->ifc_len, error = 0;
1342 ifrp = ifc->ifc_req;
1343 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1346 if (space <= sizeof (ifr))
1348 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1349 >= sizeof(ifr.ifr_name)) {
1350 error = ENAMETOOLONG;
1355 ifa = ifp->if_addrhead.tqh_first;
1356 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1357 if (space <= sizeof(ifr))
1360 if (curproc->p_ucred->cr_prison && prison_if(curthread, sa))
1364 if (cmd == OSIOCGIFCONF) {
1365 struct osockaddr *osa =
1366 (struct osockaddr *)&ifr.ifr_addr;
1368 osa->sa_family = sa->sa_family;
1369 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1374 if (sa->sa_len <= sizeof(*sa)) {
1376 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1380 if (space < sizeof (ifr) + sa->sa_len -
1383 space -= sa->sa_len - sizeof(*sa);
1384 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1385 sizeof (ifr.ifr_name));
1387 error = copyout((caddr_t)sa,
1388 (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1389 ifrp = (struct ifreq *)
1390 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1394 space -= sizeof (ifr);
1399 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1400 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1404 space -= sizeof (ifr);
1408 ifc->ifc_len -= space;
1413 * Just like if_promisc(), but for all-multicast-reception mode.
1416 if_allmulti(ifp, onswitch)
1425 if (ifp->if_amcount++ == 0) {
1426 ifp->if_flags |= IFF_ALLMULTI;
1427 ifr.ifr_flags = ifp->if_flags;
1428 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1429 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1432 if (ifp->if_amcount > 1) {
1435 ifp->if_amcount = 0;
1436 ifp->if_flags &= ~IFF_ALLMULTI;
1437 ifr.ifr_flags = ifp->if_flags;
1438 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1439 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1450 * Add a multicast listenership to the interface in question.
1451 * The link layer provides a routine which converts
1454 if_addmulti(ifp, sa, retifma)
1455 struct ifnet *ifp; /* interface to manipulate */
1456 struct sockaddr *sa; /* address to add */
1457 struct ifmultiaddr **retifma;
1459 struct sockaddr *llsa, *dupsa;
1461 struct ifmultiaddr *ifma;
1464 * If the matching multicast address already exists
1465 * then don't add a new one, just add a reference
1467 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1468 if (equal(sa, ifma->ifma_addr)) {
1469 ifma->ifma_refcount++;
1477 * Give the link layer a chance to accept/reject it, and also
1478 * find out which AF_LINK address this maps to, if it isn't one
1481 if (ifp->if_resolvemulti) {
1482 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1483 if (error) return error;
1488 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1489 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1490 bcopy(sa, dupsa, sa->sa_len);
1492 ifma->ifma_addr = dupsa;
1493 ifma->ifma_lladdr = llsa;
1494 ifma->ifma_ifp = ifp;
1495 ifma->ifma_refcount = 1;
1496 ifma->ifma_protospec = 0;
1497 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1500 * Some network interfaces can scan the address list at
1501 * interrupt time; lock them out.
1504 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1509 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1510 if (equal(ifma->ifma_addr, llsa))
1514 ifma->ifma_refcount++;
1516 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1517 M_IFMADDR, M_WAITOK);
1518 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1519 M_IFMADDR, M_WAITOK);
1520 bcopy(llsa, dupsa, llsa->sa_len);
1521 ifma->ifma_addr = dupsa;
1522 ifma->ifma_ifp = ifp;
1523 ifma->ifma_refcount = 1;
1525 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1530 * We are certain we have added something, so call down to the
1531 * interface to let them know about it.
1534 ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1541 * Remove a reference to a multicast address on this interface. Yell
1542 * if the request does not match an existing membership.
1545 if_delmulti(ifp, sa)
1547 struct sockaddr *sa;
1549 struct ifmultiaddr *ifma;
1552 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1553 if (equal(sa, ifma->ifma_addr))
1558 if (ifma->ifma_refcount > 1) {
1559 ifma->ifma_refcount--;
1563 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1564 sa = ifma->ifma_lladdr;
1566 LIST_REMOVE(ifma, ifma_link);
1568 * Make sure the interface driver is notified
1569 * in the case of a link layer mcast group being left.
1571 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0)
1572 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1574 free(ifma->ifma_addr, M_IFMADDR);
1575 free(ifma, M_IFMADDR);
1580 * Now look for the link-layer address which corresponds to
1581 * this network address. It had been squirreled away in
1582 * ifma->ifma_lladdr for this purpose (so we don't have
1583 * to call ifp->if_resolvemulti() again), and we saved that
1584 * value in sa above. If some nasty deleted the
1585 * link-layer address out from underneath us, we can deal because
1586 * the address we stored was is not the same as the one which was
1587 * in the record for the link-layer address. (So we don't complain
1590 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1591 if (equal(sa, ifma->ifma_addr))
1596 if (ifma->ifma_refcount > 1) {
1597 ifma->ifma_refcount--;
1602 LIST_REMOVE(ifma, ifma_link);
1603 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1605 free(ifma->ifma_addr, M_IFMADDR);
1606 free(sa, M_IFMADDR);
1607 free(ifma, M_IFMADDR);
1613 * Set the link layer address on an interface.
1615 * At this time we only support certain types of interfaces,
1616 * and we don't allow the length of the address to change.
1619 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1621 struct sockaddr_dl *sdl;
1625 ifa = ifnet_addrs[ifp->if_index - 1];
1628 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1631 if (len != sdl->sdl_alen) /* don't allow length to change */
1633 switch (ifp->if_type) {
1634 case IFT_ETHER: /* these types use struct arpcom */
1639 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1642 bcopy(lladdr, LLADDR(sdl), len);
1648 * If the interface is already up, we need
1649 * to re-init it in order to reprogram its
1652 if ((ifp->if_flags & IFF_UP) != 0) {
1653 ifp->if_flags &= ~IFF_UP;
1654 ifr.ifr_flags = ifp->if_flags;
1655 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1656 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1657 ifp->if_flags |= IFF_UP;
1658 ifr.ifr_flags = ifp->if_flags;
1659 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1660 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1663 * Also send gratuitous ARPs to notify other nodes about
1664 * the address change.
1666 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1667 if (ifa->ifa_addr != NULL &&
1668 ifa->ifa_addr->sa_family == AF_INET)
1669 arp_ifinit(ifp, ifa);
1676 struct ifmultiaddr *
1677 ifmaof_ifpforaddr(sa, ifp)
1678 struct sockaddr *sa;
1681 struct ifmultiaddr *ifma;
1683 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1684 if (equal(ifma->ifma_addr, sa))
1691 * The name argument must be a pointer to storage which will last as
1692 * long as the interface does. For physical devices, the result of
1693 * device_get_name(dev) is a good choice and for pseudo-devices a
1694 * static string works well.
1697 if_initname(struct ifnet *ifp, const char *name, int unit)
1699 ifp->if_dname = name;
1700 ifp->if_dunit = unit;
1701 if (unit != IF_DUNIT_NONE)
1702 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
1704 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1708 if_printf(struct ifnet *ifp, const char *fmt, ...)
1713 retval = printf("%s: ", ifp->if_xname);
1714 __va_start(ap, fmt);
1715 retval += vprintf(fmt, ap);
1720 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1721 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");