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.22 2004/09/15 19:34:55 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;
116 struct callout if_slowtimo_timer;
119 * Network interface utility routines.
121 * Routines with ifa_ifwith* names take sockaddr *'s as
132 callout_init(&if_slowtimo_timer);
135 TAILQ_FOREACH(ifp, &ifnet, if_link) {
136 if (ifp->if_snd.ifq_maxlen == 0) {
137 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
138 ifp->if_snd.ifq_maxlen = ifqmaxlen;
147 struct ifaddr **ifnet_addrs;
148 struct ifnet **ifindex2ifnet = NULL;
152 * Attach an interface to the
153 * list of "active" interfaces.
159 unsigned socksize, ifasize;
160 int namelen, masklen;
161 struct sockaddr_dl *sdl;
163 static int if_indexlim = 8;
171 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
172 ifp->if_index = ++if_index;
175 * The old code would work if the interface passed a pre-existing
176 * chain of ifaddrs to this code. We don't trust our callers to
177 * properly initialize the tailq, however, so we no longer allow
178 * this unlikely case.
180 TAILQ_INIT(&ifp->if_addrhead);
181 TAILQ_INIT(&ifp->if_prefixhead);
182 LIST_INIT(&ifp->if_multiaddrs);
183 getmicrotime(&ifp->if_lastchange);
184 if (ifnet_addrs == 0 || if_index >= if_indexlim) {
185 unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
186 caddr_t q = malloc(n, M_IFADDR, M_WAITOK);
189 bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
190 free((caddr_t)ifnet_addrs, M_IFADDR);
192 ifnet_addrs = (struct ifaddr **)q;
194 /* grow ifindex2ifnet */
195 n = if_indexlim * sizeof(struct ifnet *);
196 q = malloc(n, M_IFADDR, M_WAITOK);
199 bcopy((caddr_t)ifindex2ifnet, q, n/2);
200 free((caddr_t)ifindex2ifnet, M_IFADDR);
202 ifindex2ifnet = (struct ifnet **)q;
205 ifindex2ifnet[if_index] = ifp;
208 * create a Link Level name for this device
210 namelen = strlen(ifp->if_xname);
211 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
212 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
213 socksize = masklen + ifp->if_addrlen;
214 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
215 if (socksize < sizeof(*sdl))
216 socksize = sizeof(*sdl);
217 socksize = ROUNDUP(socksize);
218 ifasize = sizeof(*ifa) + 2 * socksize;
219 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
221 bzero((caddr_t)ifa, ifasize);
222 sdl = (struct sockaddr_dl *)(ifa + 1);
223 sdl->sdl_len = socksize;
224 sdl->sdl_family = AF_LINK;
225 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
226 sdl->sdl_nlen = namelen;
227 sdl->sdl_index = ifp->if_index;
228 sdl->sdl_type = ifp->if_type;
229 ifnet_addrs[if_index - 1] = ifa;
231 ifa->ifa_rtrequest = link_rtrequest;
232 ifa->ifa_addr = (struct sockaddr *)sdl;
233 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
234 ifa->ifa_netmask = (struct sockaddr *)sdl;
235 sdl->sdl_len = masklen;
237 sdl->sdl_data[--namelen] = 0xff;
238 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
241 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
243 /* Announce the interface. */
244 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
248 * Detach an interface, removing it from the
249 * list of "active" interfaces.
256 struct radix_node_head *rnh;
260 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
263 * Remove routes and flush queues.
269 * Remove address from ifnet_addrs[] and maybe decrement if_index.
270 * Clean up all addresses.
272 ifnet_addrs[ifp->if_index - 1] = 0;
273 while (if_index > 0 && ifnet_addrs[if_index - 1] == 0)
276 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
277 ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
279 /* XXX: Ugly!! ad hoc just for INET */
280 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
281 struct ifaliasreq ifr;
283 bzero(&ifr, sizeof(ifr));
284 ifr.ifra_addr = *ifa->ifa_addr;
285 if (ifa->ifa_dstaddr)
286 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
287 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
293 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
295 /* ifp_addrhead is already updated */
299 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
305 * Remove all IPv6 kernel structs related to ifp. This should be done
306 * before removing routing entries below, since IPv6 interface direct
307 * routes are expected to be removed by the IPv6-specific kernel API.
308 * Otherwise, the kernel will detect some inconsistency and bark it.
314 * Delete all remaining routes using this interface
315 * Unfortuneatly the only way to do this is to slog through
316 * the entire routing table looking for routes which point
317 * to this interface...oh well...
319 for (i = 1; i <= AF_MAX; i++) {
320 if ((rnh = rt_tables[i]) == NULL)
322 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
325 /* Announce that the interface is gone. */
326 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
328 ifindex2ifnet[ifp->if_index] = NULL;
330 TAILQ_REMOVE(&ifnet, ifp, if_link);
335 * Delete Routes for a Network Interface
337 * Called for each routing entry via the rnh->rnh_walktree() call above
338 * to delete all route entries referencing a detaching network interface.
341 * rn pointer to node in the routing table
342 * arg argument passed to rnh->rnh_walktree() - detaching interface
346 * errno failed - reason indicated
351 struct radix_node *rn;
354 struct rtentry *rt = (struct rtentry *)rn;
355 struct ifnet *ifp = arg;
358 if (rt->rt_ifp == ifp) {
361 * Protect (sorta) against walktree recursion problems
364 if ((rt->rt_flags & RTF_UP) == 0)
367 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
368 rt_mask(rt), rt->rt_flags,
369 (struct rtentry **) NULL);
371 log(LOG_WARNING, "if_rtdel: error %d\n", err);
379 * Create a clone network interface.
382 if_clone_create(name, len)
386 struct if_clone *ifc;
388 int wildcard, bytoff, bitoff;
392 ifc = if_clone_lookup(name, &unit);
396 if (ifunit(name) != NULL)
400 wildcard = (unit < 0);
402 * Find a free unit if none was given.
405 while ((bytoff < ifc->ifc_bmlen)
406 && (ifc->ifc_units[bytoff] == 0xff))
408 if (bytoff >= ifc->ifc_bmlen)
410 while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
412 unit = (bytoff << 3) + bitoff;
415 if (unit > ifc->ifc_maxunit)
418 err = (*ifc->ifc_create)(ifc, unit);
424 bitoff = unit - (bytoff << 3);
428 * Allocate the unit in the bitmap.
430 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
431 ("%s: bit is already set", __func__));
432 ifc->ifc_units[bytoff] |= (1 << bitoff);
434 /* In the wildcard case, we need to update the name. */
436 for (dp = name; *dp != '\0'; dp++);
437 if (snprintf(dp, len - (dp-name), "%d", unit) >
438 len - (dp-name) - 1) {
440 * This can only be a programmer error and
441 * there's no straightforward way to recover if
444 panic("if_clone_create(): interface name too long");
449 EVENTHANDLER_INVOKE(if_clone_event, ifc);
455 * Destroy a clone network interface.
458 if_clone_destroy(name)
461 struct if_clone *ifc;
466 ifc = if_clone_lookup(name, &unit);
470 if (unit < ifc->ifc_minifs)
477 if (ifc->ifc_destroy == NULL)
480 (*ifc->ifc_destroy)(ifp);
483 * Compute offset in the bitmap and deallocate the unit.
486 bitoff = unit - (bytoff << 3);
487 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
488 ("%s: bit is already cleared", __func__));
489 ifc->ifc_units[bytoff] &= ~(1 << bitoff);
494 * Look up a network interface cloner.
497 if_clone_lookup(name, unitp)
501 struct if_clone *ifc;
505 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
506 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
507 if (ifc->ifc_name[i] != *cp)
512 ifc = LIST_NEXT(ifc, ifc_list);
516 return ((struct if_clone *)NULL);
522 for (i = 0; *cp != '\0'; cp++) {
523 if (*cp < '0' || *cp > '9') {
524 /* Bogus unit number. */
527 i = (i * 10) + (*cp - '0');
537 * Register a network interface cloner.
541 struct if_clone *ifc;
548 KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
549 ("%s: %s requested more units then allowed (%d > %d)",
550 __func__, ifc->ifc_name, ifc->ifc_minifs,
551 ifc->ifc_maxunit + 1));
553 * Compute bitmap size and allocate it.
555 maxclone = ifc->ifc_maxunit + 1;
557 if ((len << 3) < maxclone)
559 ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO);
560 ifc->ifc_bmlen = len;
562 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
565 for (unit = 0; unit < ifc->ifc_minifs; unit++) {
566 err = (*ifc->ifc_create)(ifc, unit);
568 ("%s: failed to create required interface %s%d",
569 __func__, ifc->ifc_name, unit));
571 /* Allocate the unit in the bitmap. */
573 bitoff = unit - (bytoff << 3);
574 ifc->ifc_units[bytoff] |= (1 << bitoff);
579 * Unregister a network interface cloner.
583 struct if_clone *ifc;
586 LIST_REMOVE(ifc, ifc_list);
587 free(ifc->ifc_units, M_CLONE);
592 * Provide list of interface cloners to userspace.
596 struct if_clonereq *ifcr;
598 char outbuf[IFNAMSIZ], *dst;
599 struct if_clone *ifc;
600 int count, error = 0;
602 ifcr->ifcr_total = if_cloners_count;
603 if ((dst = ifcr->ifcr_buffer) == NULL) {
604 /* Just asking how many there are. */
608 if (ifcr->ifcr_count < 0)
611 count = (if_cloners_count < ifcr->ifcr_count) ?
612 if_cloners_count : ifcr->ifcr_count;
614 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
615 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
616 strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
617 error = copyout(outbuf, dst, IFNAMSIZ);
626 * Locate an interface based on a complete address.
631 struct sockaddr *addr;
636 #define equal(a1, a2) \
637 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
638 TAILQ_FOREACH(ifp, &ifnet, if_link)
639 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
640 if (ifa->ifa_addr->sa_family != addr->sa_family)
642 if (equal(addr, ifa->ifa_addr))
644 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
645 /* IP6 doesn't have broadcast */
646 ifa->ifa_broadaddr->sa_len != 0 &&
647 equal(ifa->ifa_broadaddr, addr))
650 return ((struct ifaddr *)0);
653 * Locate the point to point interface with a given destination address.
657 ifa_ifwithdstaddr(addr)
658 struct sockaddr *addr;
663 TAILQ_FOREACH(ifp, &ifnet, if_link)
664 if (ifp->if_flags & IFF_POINTOPOINT)
665 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
666 if (ifa->ifa_addr->sa_family != addr->sa_family)
668 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
671 return ((struct ifaddr *)0);
675 * Find an interface on a specific network. If many, choice
676 * is most specific found.
680 struct sockaddr *addr;
684 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
685 u_int af = addr->sa_family;
686 char *addr_data = addr->sa_data, *cplim;
689 * AF_LINK addresses can be looked up directly by their index number,
690 * so do that if we can.
693 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
694 if (sdl->sdl_index && sdl->sdl_index <= if_index)
695 return (ifnet_addrs[sdl->sdl_index - 1]);
699 * Scan though each interface, looking for ones that have
700 * addresses in this address family.
702 TAILQ_FOREACH(ifp, &ifnet, if_link) {
703 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
704 char *cp, *cp2, *cp3;
706 if (ifa->ifa_addr->sa_family != af)
708 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
710 * This is a bit broken as it doesn't
711 * take into account that the remote end may
712 * be a single node in the network we are
714 * The trouble is that we don't know the
715 * netmask for the remote end.
717 if (ifa->ifa_dstaddr != 0
718 && equal(addr, ifa->ifa_dstaddr))
722 * if we have a special address handler,
723 * then use it instead of the generic one.
725 if (ifa->ifa_claim_addr) {
726 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
734 * Scan all the bits in the ifa's address.
735 * If a bit dissagrees with what we are
736 * looking for, mask it with the netmask
737 * to see if it really matters.
740 if (ifa->ifa_netmask == 0)
743 cp2 = ifa->ifa_addr->sa_data;
744 cp3 = ifa->ifa_netmask->sa_data;
745 cplim = ifa->ifa_netmask->sa_len
746 + (char *)ifa->ifa_netmask;
748 if ((*cp++ ^ *cp2++) & *cp3++)
749 goto next; /* next address! */
751 * If the netmask of what we just found
752 * is more specific than what we had before
753 * (if we had one) then remember the new one
754 * before continuing to search
755 * for an even better one.
757 if (ifa_maybe == 0 ||
758 rn_refines((caddr_t)ifa->ifa_netmask,
759 (caddr_t)ifa_maybe->ifa_netmask))
768 * Find an interface address specific to an interface best matching
772 ifaof_ifpforaddr(addr, ifp)
773 struct sockaddr *addr;
777 char *cp, *cp2, *cp3;
779 struct ifaddr *ifa_maybe = 0;
780 u_int af = addr->sa_family;
784 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
785 if (ifa->ifa_addr->sa_family != af)
789 if (ifa->ifa_netmask == 0) {
790 if (equal(addr, ifa->ifa_addr) ||
791 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
795 if (ifp->if_flags & IFF_POINTOPOINT) {
796 if (equal(addr, ifa->ifa_dstaddr))
800 cp2 = ifa->ifa_addr->sa_data;
801 cp3 = ifa->ifa_netmask->sa_data;
802 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
803 for (; cp3 < cplim; cp3++)
804 if ((*cp++ ^ *cp2++) & *cp3)
813 #include <net/route.h>
816 * Default action when installing a route with a Link Level gateway.
817 * Lookup an appropriate real ifa to point to.
818 * This should be moved to /sys/net/link.c eventually.
821 link_rtrequest(cmd, rt, info)
824 struct rt_addrinfo *info;
827 struct sockaddr *dst;
830 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
831 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
833 ifa = ifaof_ifpforaddr(dst, ifp);
838 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
839 ifa->ifa_rtrequest(cmd, rt, info);
844 * Mark an interface down and notify protocols of
846 * NOTE: must be called at splnet or eqivalent.
849 if_unroute(ifp, flag, fam)
855 ifp->if_flags &= ~flag;
856 getmicrotime(&ifp->if_lastchange);
857 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
858 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
859 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
860 if_qflush(&ifp->if_snd);
865 * Mark an interface up and notify protocols of
867 * NOTE: must be called at splnet or eqivalent.
870 if_route(ifp, flag, fam)
876 ifp->if_flags |= flag;
877 getmicrotime(&ifp->if_lastchange);
878 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
879 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
880 pfctlinput(PRC_IFUP, ifa->ifa_addr);
888 * Mark an interface down and notify protocols of
890 * NOTE: must be called at splnet or eqivalent.
897 if_unroute(ifp, IFF_UP, AF_UNSPEC);
901 * Mark an interface up and notify protocols of
903 * NOTE: must be called at splnet or eqivalent.
910 if_route(ifp, IFF_UP, AF_UNSPEC);
914 * Flush an interface queue.
923 while ((m = n) != 0) {
933 * Handle interface watchdog timer routines. Called
934 * from softclock, we decrement timers (if set) and
935 * call the appropriate interface routine on expiration.
944 TAILQ_FOREACH(ifp, &ifnet, if_link) {
945 if (ifp->if_timer == 0 || --ifp->if_timer)
947 if (ifp->if_watchdog)
948 (*ifp->if_watchdog)(ifp);
951 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
955 * Map interface name to
956 * interface structure pointer.
959 ifunit(const char *name)
964 * Search all the interfaces for this name/number
967 TAILQ_FOREACH(ifp, &ifnet, if_link) {
968 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
976 * Map interface name in a sockaddr_dl to
977 * interface structure pointer.
983 char ifname[IFNAMSIZ+1];
984 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
986 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
987 (sdl->sdl_nlen > IFNAMSIZ) )
991 * ifunit wants a null-terminated name. It may not be null-terminated
992 * in the sockaddr. We don't want to change the caller's sockaddr,
993 * and there might not be room to put the trailing null anyway, so we
994 * make a local copy that we know we can null terminate safely.
997 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
998 ifname[sdl->sdl_nlen] = '\0';
999 return ifunit(ifname);
1007 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
1015 size_t namelen, onamelen;
1016 char new_name[IFNAMSIZ];
1018 struct sockaddr_dl *sdl;
1024 return (ifconf(cmd, data, td));
1026 ifr = (struct ifreq *)data;
1031 if ((error = suser(td)) != 0)
1033 return ((cmd == SIOCIFCREATE) ?
1034 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1035 if_clone_destroy(ifr->ifr_name));
1037 case SIOCIFGCLONERS:
1038 return (if_clone_list((struct if_clonereq *)data));
1041 ifp = ifunit(ifr->ifr_name);
1047 ifr->ifr_flags = ifp->if_flags;
1048 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1052 ifr->ifr_reqcap = ifp->if_capabilities;
1053 ifr->ifr_curcap = ifp->if_capenable;
1057 ifr->ifr_metric = ifp->if_metric;
1061 ifr->ifr_mtu = ifp->if_mtu;
1065 ifr->ifr_phys = ifp->if_physical;
1072 new_flags = (ifr->ifr_flags & 0xffff) |
1073 (ifr->ifr_flagshigh << 16);
1074 if (ifp->if_flags & IFF_SMART) {
1075 /* Smart drivers twiddle their own routes */
1076 } else if (ifp->if_flags & IFF_UP &&
1077 (new_flags & IFF_UP) == 0) {
1081 } else if (new_flags & IFF_UP &&
1082 (ifp->if_flags & IFF_UP) == 0) {
1087 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1088 (new_flags &~ IFF_CANTCHANGE);
1089 if (new_flags & IFF_PPROMISC) {
1090 /* Permanently promiscuous mode requested */
1091 ifp->if_flags |= IFF_PROMISC;
1092 } else if (ifp->if_pcount == 0) {
1093 ifp->if_flags &= ~IFF_PROMISC;
1096 (void) (*ifp->if_ioctl)(ifp, cmd, data,
1097 td->td_proc->p_ucred);
1098 getmicrotime(&ifp->if_lastchange);
1105 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1107 (void) (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
1114 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1117 if (new_name[0] == '\0')
1119 if (ifunit(new_name) != NULL)
1122 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1124 /* Announce the departure of the interface. */
1125 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1127 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1128 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1129 /* XXX IFA_LOCK(ifa); */
1130 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1131 namelen = strlen(new_name);
1132 onamelen = sdl->sdl_nlen;
1134 * Move the address if needed. This is safe because we
1135 * allocate space for a name of length IFNAMSIZ when we
1136 * create this in if_attach().
1138 if (namelen != onamelen) {
1139 bcopy(sdl->sdl_data + onamelen,
1140 sdl->sdl_data + namelen, sdl->sdl_alen);
1142 bcopy(new_name, sdl->sdl_data, namelen);
1143 sdl->sdl_nlen = namelen;
1144 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1145 bzero(sdl->sdl_data, onamelen);
1146 while (namelen != 0)
1147 sdl->sdl_data[--namelen] = 0xff;
1148 /* XXX IFA_UNLOCK(ifa) */
1150 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1152 /* Announce the return of the interface. */
1153 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1160 ifp->if_metric = ifr->ifr_metric;
1161 getmicrotime(&ifp->if_lastchange);
1170 error = (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
1172 getmicrotime(&ifp->if_lastchange);
1177 u_long oldmtu = ifp->if_mtu;
1182 if (ifp->if_ioctl == NULL)
1183 return (EOPNOTSUPP);
1184 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1186 error = (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
1188 getmicrotime(&ifp->if_lastchange);
1192 * If the link MTU changed, do network layer specific procedure.
1194 if (ifp->if_mtu != oldmtu) {
1208 /* Don't allow group membership on non-multicast interfaces. */
1209 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1212 /* Don't let users screw up protocols' entries. */
1213 if (ifr->ifr_addr.sa_family != AF_LINK)
1216 if (cmd == SIOCADDMULTI) {
1217 struct ifmultiaddr *ifma;
1218 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1220 error = if_delmulti(ifp, &ifr->ifr_addr);
1223 getmicrotime(&ifp->if_lastchange);
1226 case SIOCSIFPHYADDR:
1227 case SIOCDIFPHYADDR:
1229 case SIOCSIFPHYADDR_IN6:
1231 case SIOCSLIFPHYADDR:
1233 case SIOCSIFGENERIC:
1237 if (ifp->if_ioctl == 0)
1238 return (EOPNOTSUPP);
1239 error = (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
1241 getmicrotime(&ifp->if_lastchange);
1245 ifs = (struct ifstat *)data;
1246 ifs->ascii[0] = '\0';
1248 case SIOCGIFPSRCADDR:
1249 case SIOCGIFPDSTADDR:
1250 case SIOCGLIFPHYADDR:
1252 case SIOCGIFGENERIC:
1253 if (ifp->if_ioctl == 0)
1254 return (EOPNOTSUPP);
1255 return ((*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred));
1261 return if_setlladdr(ifp,
1262 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1265 oif_flags = ifp->if_flags;
1266 if (so->so_proto == 0)
1267 return (EOPNOTSUPP);
1269 error = so_pru_control(so, cmd, data, ifp, td);
1276 case SIOCSIFDSTADDR:
1278 case SIOCSIFBRDADDR:
1279 case SIOCSIFNETMASK:
1280 #if BYTE_ORDER != BIG_ENDIAN
1281 if (ifr->ifr_addr.sa_family == 0 &&
1282 ifr->ifr_addr.sa_len < 16) {
1283 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1284 ifr->ifr_addr.sa_len = 16;
1287 if (ifr->ifr_addr.sa_len == 0)
1288 ifr->ifr_addr.sa_len = 16;
1296 case OSIOCGIFDSTADDR:
1297 cmd = SIOCGIFDSTADDR;
1300 case OSIOCGIFBRDADDR:
1301 cmd = SIOCGIFBRDADDR;
1304 case OSIOCGIFNETMASK:
1305 cmd = SIOCGIFNETMASK;
1307 error = so_pru_control(so, cmd, data, ifp, td);
1311 case OSIOCGIFDSTADDR:
1312 case OSIOCGIFBRDADDR:
1313 case OSIOCGIFNETMASK:
1314 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1318 #endif /* COMPAT_43 */
1320 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1322 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1323 if (ifp->if_flags & IFF_UP) {
1337 * Set/clear promiscuous mode on interface ifp based on the truth value
1338 * of pswitch. The calls are reference counted so that only the first
1339 * "on" request actually has an effect, as does the final "off" request.
1340 * Results are undefined if the "off" and "on" requests are not matched.
1343 ifpromisc(ifp, pswitch)
1351 oldflags = ifp->if_flags;
1352 if (ifp->if_flags & IFF_PPROMISC) {
1353 /* Do nothing if device is in permanently promiscuous mode */
1354 ifp->if_pcount += pswitch ? 1 : -1;
1359 * If the device is not configured up, we cannot put it in
1362 if ((ifp->if_flags & IFF_UP) == 0)
1364 if (ifp->if_pcount++ != 0)
1366 ifp->if_flags |= IFF_PROMISC;
1367 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1370 if (--ifp->if_pcount > 0)
1372 ifp->if_flags &= ~IFF_PROMISC;
1373 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1376 ifr.ifr_flags = ifp->if_flags;
1377 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1378 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1379 (struct ucred *)NULL);
1383 ifp->if_flags = oldflags;
1388 * Return interface configuration
1389 * of system. List may be used
1390 * in later ioctl's (above) to get
1391 * other information.
1395 ifconf(u_long cmd, caddr_t data, struct thread *td)
1397 struct ifconf *ifc = (struct ifconf *)data;
1400 struct sockaddr *sa;
1401 struct ifreq ifr, *ifrp;
1402 int space = ifc->ifc_len, error = 0;
1404 ifrp = ifc->ifc_req;
1405 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1408 if (space <= sizeof (ifr))
1410 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1411 >= sizeof(ifr.ifr_name)) {
1412 error = ENAMETOOLONG;
1417 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1418 if (space <= sizeof(ifr))
1421 if (td->td_proc->p_ucred->cr_prison &&
1426 if (cmd == OSIOCGIFCONF) {
1427 struct osockaddr *osa =
1428 (struct osockaddr *)&ifr.ifr_addr;
1430 osa->sa_family = sa->sa_family;
1431 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1436 if (sa->sa_len <= sizeof(*sa)) {
1438 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1442 if (space < sizeof (ifr) + sa->sa_len -
1445 space -= sa->sa_len - sizeof(*sa);
1446 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1447 sizeof (ifr.ifr_name));
1449 error = copyout((caddr_t)sa,
1450 (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1451 ifrp = (struct ifreq *)
1452 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1456 space -= sizeof (ifr);
1461 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1462 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1466 space -= sizeof (ifr);
1470 ifc->ifc_len -= space;
1475 * Just like if_promisc(), but for all-multicast-reception mode.
1478 if_allmulti(ifp, onswitch)
1487 if (ifp->if_amcount++ == 0) {
1488 ifp->if_flags |= IFF_ALLMULTI;
1489 ifr.ifr_flags = ifp->if_flags;
1490 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1491 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1492 (struct ucred *)NULL);
1495 if (ifp->if_amcount > 1) {
1498 ifp->if_amcount = 0;
1499 ifp->if_flags &= ~IFF_ALLMULTI;
1500 ifr.ifr_flags = ifp->if_flags;
1501 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1502 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1503 (struct ucred *)NULL);
1514 * Add a multicast listenership to the interface in question.
1515 * The link layer provides a routine which converts
1518 if_addmulti(ifp, sa, retifma)
1519 struct ifnet *ifp; /* interface to manipulate */
1520 struct sockaddr *sa; /* address to add */
1521 struct ifmultiaddr **retifma;
1523 struct sockaddr *llsa, *dupsa;
1525 struct ifmultiaddr *ifma;
1528 * If the matching multicast address already exists
1529 * then don't add a new one, just add a reference
1531 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1532 if (equal(sa, ifma->ifma_addr)) {
1533 ifma->ifma_refcount++;
1541 * Give the link layer a chance to accept/reject it, and also
1542 * find out which AF_LINK address this maps to, if it isn't one
1545 if (ifp->if_resolvemulti) {
1546 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1547 if (error) return error;
1552 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1553 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1554 bcopy(sa, dupsa, sa->sa_len);
1556 ifma->ifma_addr = dupsa;
1557 ifma->ifma_lladdr = llsa;
1558 ifma->ifma_ifp = ifp;
1559 ifma->ifma_refcount = 1;
1560 ifma->ifma_protospec = 0;
1561 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1564 * Some network interfaces can scan the address list at
1565 * interrupt time; lock them out.
1568 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1573 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1574 if (equal(ifma->ifma_addr, llsa))
1578 ifma->ifma_refcount++;
1580 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1581 M_IFMADDR, M_WAITOK);
1582 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1583 M_IFMADDR, M_WAITOK);
1584 bcopy(llsa, dupsa, llsa->sa_len);
1585 ifma->ifma_addr = dupsa;
1586 ifma->ifma_ifp = ifp;
1587 ifma->ifma_refcount = 1;
1589 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1594 * We are certain we have added something, so call down to the
1595 * interface to let them know about it.
1598 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, (struct ucred *)NULL);
1605 * Remove a reference to a multicast address on this interface. Yell
1606 * if the request does not match an existing membership.
1609 if_delmulti(ifp, sa)
1611 struct sockaddr *sa;
1613 struct ifmultiaddr *ifma;
1616 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1617 if (equal(sa, ifma->ifma_addr))
1622 if (ifma->ifma_refcount > 1) {
1623 ifma->ifma_refcount--;
1627 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1628 sa = ifma->ifma_lladdr;
1630 LIST_REMOVE(ifma, ifma_link);
1632 * Make sure the interface driver is notified
1633 * in the case of a link layer mcast group being left.
1635 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0)
1636 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1638 free(ifma->ifma_addr, M_IFMADDR);
1639 free(ifma, M_IFMADDR);
1644 * Now look for the link-layer address which corresponds to
1645 * this network address. It had been squirreled away in
1646 * ifma->ifma_lladdr for this purpose (so we don't have
1647 * to call ifp->if_resolvemulti() again), and we saved that
1648 * value in sa above. If some nasty deleted the
1649 * link-layer address out from underneath us, we can deal because
1650 * the address we stored was is not the same as the one which was
1651 * in the record for the link-layer address. (So we don't complain
1654 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1655 if (equal(sa, ifma->ifma_addr))
1660 if (ifma->ifma_refcount > 1) {
1661 ifma->ifma_refcount--;
1666 LIST_REMOVE(ifma, ifma_link);
1667 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1669 free(ifma->ifma_addr, M_IFMADDR);
1670 free(sa, M_IFMADDR);
1671 free(ifma, M_IFMADDR);
1677 * Set the link layer address on an interface.
1679 * At this time we only support certain types of interfaces,
1680 * and we don't allow the length of the address to change.
1683 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1685 struct sockaddr_dl *sdl;
1689 ifa = ifnet_addrs[ifp->if_index - 1];
1692 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1695 if (len != sdl->sdl_alen) /* don't allow length to change */
1697 switch (ifp->if_type) {
1698 case IFT_ETHER: /* these types use struct arpcom */
1703 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1706 bcopy(lladdr, LLADDR(sdl), len);
1712 * If the interface is already up, we need
1713 * to re-init it in order to reprogram its
1716 if ((ifp->if_flags & IFF_UP) != 0) {
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);
1722 ifp->if_flags |= IFF_UP;
1723 ifr.ifr_flags = ifp->if_flags;
1724 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1725 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1726 (struct ucred *)NULL);
1729 * Also send gratuitous ARPs to notify other nodes about
1730 * the address change.
1732 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1733 if (ifa->ifa_addr != NULL &&
1734 ifa->ifa_addr->sa_family == AF_INET)
1735 arp_ifinit(ifp, ifa);
1742 struct ifmultiaddr *
1743 ifmaof_ifpforaddr(sa, ifp)
1744 struct sockaddr *sa;
1747 struct ifmultiaddr *ifma;
1749 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1750 if (equal(ifma->ifma_addr, sa))
1757 * The name argument must be a pointer to storage which will last as
1758 * long as the interface does. For physical devices, the result of
1759 * device_get_name(dev) is a good choice and for pseudo-devices a
1760 * static string works well.
1763 if_initname(struct ifnet *ifp, const char *name, int unit)
1765 ifp->if_dname = name;
1766 ifp->if_dunit = unit;
1767 if (unit != IF_DUNIT_NONE)
1768 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
1770 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1774 if_printf(struct ifnet *ifp, const char *fmt, ...)
1779 retval = printf("%s: ", ifp->if_xname);
1780 __va_start(ap, fmt);
1781 retval += vprintf(fmt, ap);
1786 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1787 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");