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 $
37 #include "opt_compat.h"
38 #include "opt_inet6.h"
41 #include <sys/param.h>
42 #include <sys/malloc.h>
44 #include <sys/systm.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/protosw.h>
49 #include <sys/kernel.h>
50 #include <sys/sockio.h>
51 #include <sys/syslog.h>
52 #include <sys/sysctl.h>
55 #include <net/if_arp.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/if_var.h>
59 #include <net/radix.h>
60 #include <net/route.h>
61 #include <machine/stdarg.h>
63 #if defined(INET) || defined(INET6)
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <netinet/if_ether.h>
69 #include <machine/clock.h> /* XXX: temporal workaround for fxp issue */
70 #include <netinet6/in6_var.h>
71 #include <netinet6/in6_ifattach.h>
76 * System initialization
79 static int ifconf __P((u_long, caddr_t));
80 static void ifinit __P((void *));
81 static void if_qflush __P((struct ifqueue *));
82 static void if_slowtimo __P((void *));
83 static void link_rtrequest __P((int, struct rtentry *, struct rt_addrinfo *));
84 static int if_rtdel __P((struct radix_node *, void *));
86 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
88 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
89 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
91 int ifqmaxlen = IFQ_MAXLEN;
92 struct ifnethead ifnet; /* depend on static init XXX */
96 * XXX: declare here to avoid to include many inet6 related files..
97 * should be more generalized?
99 extern void nd6_setmtu __P((struct ifnet *));
102 struct if_clone *if_clone_lookup __P((const char *, int *));
103 int if_clone_list __P((struct if_clonereq *));
105 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
106 int if_cloners_count;
109 * Network interface utility routines.
111 * Routines with ifa_ifwith* names take sockaddr *'s as
123 TAILQ_FOREACH(ifp, &ifnet, if_link) {
124 if (ifp->if_snd.ifq_maxlen == 0) {
125 printf("%s%d XXX: driver didn't set ifq_maxlen\n",
126 ifp->if_name, ifp->if_unit);
127 ifp->if_snd.ifq_maxlen = ifqmaxlen;
135 struct ifaddr **ifnet_addrs;
136 struct ifnet **ifindex2ifnet = NULL;
140 * Attach an interface to the
141 * list of "active" interfaces.
147 unsigned socksize, ifasize;
148 int namelen, masklen;
150 register struct sockaddr_dl *sdl;
151 register struct ifaddr *ifa;
152 static int if_indexlim = 8;
160 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
161 ifp->if_index = ++if_index;
164 * The old code would work if the interface passed a pre-existing
165 * chain of ifaddrs to this code. We don't trust our callers to
166 * properly initialize the tailq, however, so we no longer allow
167 * this unlikely case.
169 TAILQ_INIT(&ifp->if_addrhead);
170 TAILQ_INIT(&ifp->if_prefixhead);
171 LIST_INIT(&ifp->if_multiaddrs);
172 getmicrotime(&ifp->if_lastchange);
173 if (ifnet_addrs == 0 || if_index >= if_indexlim) {
174 unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
175 caddr_t q = malloc(n, M_IFADDR, M_WAITOK);
178 bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
179 free((caddr_t)ifnet_addrs, M_IFADDR);
181 ifnet_addrs = (struct ifaddr **)q;
183 /* grow ifindex2ifnet */
184 n = if_indexlim * sizeof(struct ifnet *);
185 q = malloc(n, M_IFADDR, M_WAITOK);
188 bcopy((caddr_t)ifindex2ifnet, q, n/2);
189 free((caddr_t)ifindex2ifnet, M_IFADDR);
191 ifindex2ifnet = (struct ifnet **)q;
194 ifindex2ifnet[if_index] = ifp;
197 * create a Link Level name for this device
199 namelen = snprintf(workbuf, sizeof(workbuf),
200 "%s%d", ifp->if_name, ifp->if_unit);
201 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
202 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
203 socksize = masklen + ifp->if_addrlen;
204 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
205 if (socksize < sizeof(*sdl))
206 socksize = sizeof(*sdl);
207 socksize = ROUNDUP(socksize);
208 ifasize = sizeof(*ifa) + 2 * socksize;
209 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
211 bzero((caddr_t)ifa, ifasize);
212 sdl = (struct sockaddr_dl *)(ifa + 1);
213 sdl->sdl_len = socksize;
214 sdl->sdl_family = AF_LINK;
215 bcopy(workbuf, sdl->sdl_data, namelen);
216 sdl->sdl_nlen = namelen;
217 sdl->sdl_index = ifp->if_index;
218 sdl->sdl_type = ifp->if_type;
219 ifnet_addrs[if_index - 1] = ifa;
221 ifa->ifa_rtrequest = link_rtrequest;
222 ifa->ifa_addr = (struct sockaddr *)sdl;
223 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
224 ifa->ifa_netmask = (struct sockaddr *)sdl;
225 sdl->sdl_len = masklen;
227 sdl->sdl_data[--namelen] = 0xff;
228 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
231 /* Announce the interface. */
232 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
236 * Detach an interface, removing it from the
237 * list of "active" interfaces.
244 struct radix_node_head *rnh;
249 * Remove routes and flush queues.
255 * Remove address from ifnet_addrs[] and maybe decrement if_index.
256 * Clean up all addresses.
258 ifnet_addrs[ifp->if_index - 1] = 0;
259 while (if_index > 0 && ifnet_addrs[if_index - 1] == 0)
262 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
263 ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
265 /* XXX: Ugly!! ad hoc just for INET */
266 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
267 struct ifaliasreq ifr;
269 bzero(&ifr, sizeof(ifr));
270 ifr.ifra_addr = *ifa->ifa_addr;
271 if (ifa->ifa_dstaddr)
272 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
273 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
279 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
281 /* ifp_addrhead is already updated */
285 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
291 * Remove all IPv6 kernel structs related to ifp. This should be done
292 * before removing routing entries below, since IPv6 interface direct
293 * routes are expected to be removed by the IPv6-specific kernel API.
294 * Otherwise, the kernel will detect some inconsistency and bark it.
300 * Delete all remaining routes using this interface
301 * Unfortuneatly the only way to do this is to slog through
302 * the entire routing table looking for routes which point
303 * to this interface...oh well...
305 for (i = 1; i <= AF_MAX; i++) {
306 if ((rnh = rt_tables[i]) == NULL)
308 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
311 /* Announce that the interface is gone. */
312 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
314 TAILQ_REMOVE(&ifnet, ifp, if_link);
319 * Delete Routes for a Network Interface
321 * Called for each routing entry via the rnh->rnh_walktree() call above
322 * to delete all route entries referencing a detaching network interface.
325 * rn pointer to node in the routing table
326 * arg argument passed to rnh->rnh_walktree() - detaching interface
330 * errno failed - reason indicated
335 struct radix_node *rn;
338 struct rtentry *rt = (struct rtentry *)rn;
339 struct ifnet *ifp = arg;
342 if (rt->rt_ifp == ifp) {
345 * Protect (sorta) against walktree recursion problems
348 if ((rt->rt_flags & RTF_UP) == 0)
351 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
352 rt_mask(rt), rt->rt_flags,
353 (struct rtentry **) NULL);
355 log(LOG_WARNING, "if_rtdel: error %d\n", err);
363 * Create a clone network interface.
366 if_clone_create(name, len)
370 struct if_clone *ifc;
376 ifc = if_clone_lookup(name, &unit);
380 if (ifunit(name) != NULL)
383 wildcard = (unit < 0);
385 err = (*ifc->ifc_create)(ifc, &unit);
389 /* In the wildcard case, we need to update the name. */
391 for (dp = name; *dp != '\0'; dp++);
392 if (snprintf(dp, len - (dp-name), "%d", unit) >
393 len - (dp-name) - 1) {
395 * This can only be a programmer error and
396 * there's no straightforward way to recover if
399 panic("if_clone_create(): interface name too long");
408 * Destroy a clone network interface.
411 if_clone_destroy(name)
414 struct if_clone *ifc;
417 ifc = if_clone_lookup(name, NULL);
425 if (ifc->ifc_destroy == NULL)
428 (*ifc->ifc_destroy)(ifp);
433 * Look up a network interface cloner.
436 if_clone_lookup(name, unitp)
440 struct if_clone *ifc;
444 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
445 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
446 if (ifc->ifc_name[i] != *cp)
451 ifc = LIST_NEXT(ifc, ifc_list);
455 return ((struct if_clone *)NULL);
461 for (i = 0; *cp != '\0'; cp++) {
462 if (*cp < '0' || *cp > '9') {
463 /* Bogus unit number. */
466 i = (i * 10) + (*cp - '0');
476 * Register a network interface cloner.
480 struct if_clone *ifc;
483 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
488 * Unregister a network interface cloner.
492 struct if_clone *ifc;
495 LIST_REMOVE(ifc, ifc_list);
500 * Provide list of interface cloners to userspace.
504 struct if_clonereq *ifcr;
506 char outbuf[IFNAMSIZ], *dst;
507 struct if_clone *ifc;
508 int count, error = 0;
510 ifcr->ifcr_total = if_cloners_count;
511 if ((dst = ifcr->ifcr_buffer) == NULL) {
512 /* Just asking how many there are. */
516 if (ifcr->ifcr_count < 0)
519 count = (if_cloners_count < ifcr->ifcr_count) ?
520 if_cloners_count : ifcr->ifcr_count;
522 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
523 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
524 strncpy(outbuf, ifc->ifc_name, IFNAMSIZ);
525 outbuf[IFNAMSIZ - 1] = '\0'; /* sanity */
526 error = copyout(outbuf, dst, IFNAMSIZ);
535 * Locate an interface based on a complete address.
540 register struct sockaddr *addr;
542 register struct ifnet *ifp;
543 register struct ifaddr *ifa;
545 #define equal(a1, a2) \
546 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
547 TAILQ_FOREACH(ifp, &ifnet, if_link)
548 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
549 if (ifa->ifa_addr->sa_family != addr->sa_family)
551 if (equal(addr, ifa->ifa_addr))
553 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
554 /* IP6 doesn't have broadcast */
555 ifa->ifa_broadaddr->sa_len != 0 &&
556 equal(ifa->ifa_broadaddr, addr))
559 return ((struct ifaddr *)0);
562 * Locate the point to point interface with a given destination address.
566 ifa_ifwithdstaddr(addr)
567 register struct sockaddr *addr;
569 register struct ifnet *ifp;
570 register struct ifaddr *ifa;
572 TAILQ_FOREACH(ifp, &ifnet, if_link)
573 if (ifp->if_flags & IFF_POINTOPOINT)
574 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
575 if (ifa->ifa_addr->sa_family != addr->sa_family)
577 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
580 return ((struct ifaddr *)0);
584 * Find an interface on a specific network. If many, choice
585 * is most specific found.
589 struct sockaddr *addr;
591 register struct ifnet *ifp;
592 register struct ifaddr *ifa;
593 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
594 u_int af = addr->sa_family;
595 char *addr_data = addr->sa_data, *cplim;
598 * AF_LINK addresses can be looked up directly by their index number,
599 * so do that if we can.
602 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
603 if (sdl->sdl_index && sdl->sdl_index <= if_index)
604 return (ifnet_addrs[sdl->sdl_index - 1]);
608 * Scan though each interface, looking for ones that have
609 * addresses in this address family.
611 TAILQ_FOREACH(ifp, &ifnet, if_link) {
612 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
613 register char *cp, *cp2, *cp3;
615 if (ifa->ifa_addr->sa_family != af)
617 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
619 * This is a bit broken as it doesn't
620 * take into account that the remote end may
621 * be a single node in the network we are
623 * The trouble is that we don't know the
624 * netmask for the remote end.
626 if (ifa->ifa_dstaddr != 0
627 && equal(addr, ifa->ifa_dstaddr))
631 * if we have a special address handler,
632 * then use it instead of the generic one.
634 if (ifa->ifa_claim_addr) {
635 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
643 * Scan all the bits in the ifa's address.
644 * If a bit dissagrees with what we are
645 * looking for, mask it with the netmask
646 * to see if it really matters.
649 if (ifa->ifa_netmask == 0)
652 cp2 = ifa->ifa_addr->sa_data;
653 cp3 = ifa->ifa_netmask->sa_data;
654 cplim = ifa->ifa_netmask->sa_len
655 + (char *)ifa->ifa_netmask;
657 if ((*cp++ ^ *cp2++) & *cp3++)
658 goto next; /* next address! */
660 * If the netmask of what we just found
661 * is more specific than what we had before
662 * (if we had one) then remember the new one
663 * before continuing to search
664 * for an even better one.
666 if (ifa_maybe == 0 ||
667 rn_refines((caddr_t)ifa->ifa_netmask,
668 (caddr_t)ifa_maybe->ifa_netmask))
677 * Find an interface address specific to an interface best matching
681 ifaof_ifpforaddr(addr, ifp)
682 struct sockaddr *addr;
683 register struct ifnet *ifp;
685 register struct ifaddr *ifa;
686 register char *cp, *cp2, *cp3;
687 register char *cplim;
688 struct ifaddr *ifa_maybe = 0;
689 u_int af = addr->sa_family;
693 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
694 if (ifa->ifa_addr->sa_family != af)
698 if (ifa->ifa_netmask == 0) {
699 if (equal(addr, ifa->ifa_addr) ||
700 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
704 if (ifp->if_flags & IFF_POINTOPOINT) {
705 if (equal(addr, ifa->ifa_dstaddr))
709 cp2 = ifa->ifa_addr->sa_data;
710 cp3 = ifa->ifa_netmask->sa_data;
711 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
712 for (; cp3 < cplim; cp3++)
713 if ((*cp++ ^ *cp2++) & *cp3)
722 #include <net/route.h>
725 * Default action when installing a route with a Link Level gateway.
726 * Lookup an appropriate real ifa to point to.
727 * This should be moved to /sys/net/link.c eventually.
730 link_rtrequest(cmd, rt, info)
732 register struct rtentry *rt;
733 struct rt_addrinfo *info;
735 register struct ifaddr *ifa;
736 struct sockaddr *dst;
739 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
740 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
742 ifa = ifaof_ifpforaddr(dst, ifp);
747 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
748 ifa->ifa_rtrequest(cmd, rt, info);
753 * Mark an interface down and notify protocols of
755 * NOTE: must be called at splnet or eqivalent.
758 if_unroute(ifp, flag, fam)
759 register struct ifnet *ifp;
762 register struct ifaddr *ifa;
764 ifp->if_flags &= ~flag;
765 getmicrotime(&ifp->if_lastchange);
766 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
767 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
768 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
769 if_qflush(&ifp->if_snd);
774 * Mark an interface up and notify protocols of
776 * NOTE: must be called at splnet or eqivalent.
779 if_route(ifp, flag, fam)
780 register struct ifnet *ifp;
783 register struct ifaddr *ifa;
785 ifp->if_flags |= flag;
786 getmicrotime(&ifp->if_lastchange);
787 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
788 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
789 pfctlinput(PRC_IFUP, ifa->ifa_addr);
797 * Mark an interface down and notify protocols of
799 * NOTE: must be called at splnet or eqivalent.
803 register struct ifnet *ifp;
806 if_unroute(ifp, IFF_UP, AF_UNSPEC);
810 * Mark an interface up and notify protocols of
812 * NOTE: must be called at splnet or eqivalent.
816 register struct ifnet *ifp;
819 if_route(ifp, IFF_UP, AF_UNSPEC);
823 * Flush an interface queue.
827 register struct ifqueue *ifq;
829 register struct mbuf *m, *n;
832 while ((m = n) != 0) {
842 * Handle interface watchdog timer routines. Called
843 * from softclock, we decrement timers (if set) and
844 * call the appropriate interface routine on expiration.
850 register struct ifnet *ifp;
853 TAILQ_FOREACH(ifp, &ifnet, if_link) {
854 if (ifp->if_timer == 0 || --ifp->if_timer)
856 if (ifp->if_watchdog)
857 (*ifp->if_watchdog)(ifp);
860 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
864 * Map interface name to
865 * interface structure pointer.
868 ifunit(const char *name)
870 char namebuf[IFNAMSIZ + 1];
878 if (len < 2 || len > IFNAMSIZ)
882 if (c < '0' || c > '9')
883 return NULL; /* trailing garbage */
888 return NULL; /* no interface name */
889 unit += (c - '0') * m;
891 return NULL; /* number is unreasonable */
894 } while (c >= '0' && c <= '9');
896 bcopy(name, namebuf, len);
899 * Now search all the interfaces for this name/number
901 TAILQ_FOREACH(ifp, &ifnet, if_link) {
902 if (strcmp(ifp->if_name, namebuf))
904 if (unit == ifp->if_unit)
912 * Map interface name in a sockaddr_dl to
913 * interface structure pointer.
919 char ifname[IFNAMSIZ+1];
920 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
922 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
923 (sdl->sdl_nlen > IFNAMSIZ) )
927 * ifunit wants a null-terminated name. It may not be null-terminated
928 * in the sockaddr. We don't want to change the caller's sockaddr,
929 * and there might not be room to put the trailing null anyway, so we
930 * make a local copy that we know we can null terminate safely.
933 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
934 ifname[sdl->sdl_nlen] = '\0';
935 return ifunit(ifname);
943 ifioctl(so, cmd, data, p)
949 register struct ifnet *ifp;
950 register struct ifreq *ifr;
960 return (ifconf(cmd, data));
962 ifr = (struct ifreq *)data;
967 if ((error = suser(p)) != 0)
969 return ((cmd == SIOCIFCREATE) ?
970 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
971 if_clone_destroy(ifr->ifr_name));
974 return (if_clone_list((struct if_clonereq *)data));
977 ifp = ifunit(ifr->ifr_name);
983 ifr->ifr_flags = ifp->if_flags;
984 ifr->ifr_flagshigh = ifp->if_ipending >> 16;
988 ifr->ifr_reqcap = ifp->if_capabilities;
989 ifr->ifr_curcap = ifp->if_capenable;
993 ifr->ifr_metric = ifp->if_metric;
997 ifr->ifr_mtu = ifp->if_mtu;
1001 ifr->ifr_phys = ifp->if_physical;
1008 new_flags = (ifr->ifr_flags & 0xffff) |
1009 (ifr->ifr_flagshigh << 16);
1010 if (ifp->if_flags & IFF_SMART) {
1011 /* Smart drivers twiddle their own routes */
1012 } else if (ifp->if_flags & IFF_UP &&
1013 (new_flags & IFF_UP) == 0) {
1017 } else if (new_flags & IFF_UP &&
1018 (ifp->if_flags & IFF_UP) == 0) {
1023 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1024 (new_flags &~ IFF_CANTCHANGE);
1025 ifp->if_ipending = (ifp->if_ipending & IFF_CANTCHANGE) |
1026 (new_flags &~ IFF_CANTCHANGE);
1027 if (new_flags & IFF_PPROMISC) {
1028 /* Permanently promiscuous mode requested */
1029 ifp->if_flags |= IFF_PROMISC;
1030 } else if (ifp->if_pcount == 0) {
1031 ifp->if_flags &= ~IFF_PROMISC;
1034 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1035 getmicrotime(&ifp->if_lastchange);
1042 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1044 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1051 ifp->if_metric = ifr->ifr_metric;
1052 getmicrotime(&ifp->if_lastchange);
1061 error = (*ifp->if_ioctl)(ifp, cmd, data);
1063 getmicrotime(&ifp->if_lastchange);
1068 u_long oldmtu = ifp->if_mtu;
1073 if (ifp->if_ioctl == NULL)
1074 return (EOPNOTSUPP);
1075 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1077 error = (*ifp->if_ioctl)(ifp, cmd, data);
1079 getmicrotime(&ifp->if_lastchange);
1083 * If the link MTU changed, do network layer specific procedure.
1085 if (ifp->if_mtu != oldmtu) {
1099 /* Don't allow group membership on non-multicast interfaces. */
1100 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1103 /* Don't let users screw up protocols' entries. */
1104 if (ifr->ifr_addr.sa_family != AF_LINK)
1107 if (cmd == SIOCADDMULTI) {
1108 struct ifmultiaddr *ifma;
1109 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1111 error = if_delmulti(ifp, &ifr->ifr_addr);
1114 getmicrotime(&ifp->if_lastchange);
1117 case SIOCSIFPHYADDR:
1118 case SIOCDIFPHYADDR:
1120 case SIOCSIFPHYADDR_IN6:
1122 case SIOCSLIFPHYADDR:
1124 case SIOCSIFGENERIC:
1128 if (ifp->if_ioctl == 0)
1129 return (EOPNOTSUPP);
1130 error = (*ifp->if_ioctl)(ifp, cmd, data);
1132 getmicrotime(&ifp->if_lastchange);
1136 ifs = (struct ifstat *)data;
1137 ifs->ascii[0] = '\0';
1139 case SIOCGIFPSRCADDR:
1140 case SIOCGIFPDSTADDR:
1141 case SIOCGLIFPHYADDR:
1143 case SIOCGIFGENERIC:
1144 if (ifp->if_ioctl == 0)
1145 return (EOPNOTSUPP);
1146 return ((*ifp->if_ioctl)(ifp, cmd, data));
1152 return if_setlladdr(ifp,
1153 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1156 oif_flags = ifp->if_flags;
1157 if (so->so_proto == 0)
1158 return (EOPNOTSUPP);
1160 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
1169 case SIOCSIFDSTADDR:
1171 case SIOCSIFBRDADDR:
1172 case SIOCSIFNETMASK:
1173 #if BYTE_ORDER != BIG_ENDIAN
1174 if (ifr->ifr_addr.sa_family == 0 &&
1175 ifr->ifr_addr.sa_len < 16) {
1176 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1177 ifr->ifr_addr.sa_len = 16;
1180 if (ifr->ifr_addr.sa_len == 0)
1181 ifr->ifr_addr.sa_len = 16;
1189 case OSIOCGIFDSTADDR:
1190 cmd = SIOCGIFDSTADDR;
1193 case OSIOCGIFBRDADDR:
1194 cmd = SIOCGIFBRDADDR;
1197 case OSIOCGIFNETMASK:
1198 cmd = SIOCGIFNETMASK;
1200 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
1207 case OSIOCGIFDSTADDR:
1208 case OSIOCGIFBRDADDR:
1209 case OSIOCGIFNETMASK:
1210 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1214 #endif /* COMPAT_43 */
1216 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1218 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1219 if (ifp->if_flags & IFF_UP) {
1233 * Set/clear promiscuous mode on interface ifp based on the truth value
1234 * of pswitch. The calls are reference counted so that only the first
1235 * "on" request actually has an effect, as does the final "off" request.
1236 * Results are undefined if the "off" and "on" requests are not matched.
1239 ifpromisc(ifp, pswitch)
1247 oldflags = ifp->if_flags;
1248 if (ifp->if_ipending & IFF_PPROMISC) {
1249 /* Do nothing if device is in permanently promiscuous mode */
1250 ifp->if_pcount += pswitch ? 1 : -1;
1255 * If the device is not configured up, we cannot put it in
1258 if ((ifp->if_flags & IFF_UP) == 0)
1260 if (ifp->if_pcount++ != 0)
1262 ifp->if_flags |= IFF_PROMISC;
1263 log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
1264 ifp->if_name, ifp->if_unit);
1266 if (--ifp->if_pcount > 0)
1268 ifp->if_flags &= ~IFF_PROMISC;
1269 log(LOG_INFO, "%s%d: promiscuous mode disabled\n",
1270 ifp->if_name, ifp->if_unit);
1272 ifr.ifr_flags = ifp->if_flags;
1273 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1274 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1278 ifp->if_flags = oldflags;
1283 * Return interface configuration
1284 * of system. List may be used
1285 * in later ioctl's (above) to get
1286 * other information.
1294 register struct ifconf *ifc = (struct ifconf *)data;
1295 register struct ifnet *ifp;
1296 register struct ifaddr *ifa;
1297 struct sockaddr *sa;
1298 struct ifreq ifr, *ifrp;
1299 int space = ifc->ifc_len, error = 0;
1301 ifrp = ifc->ifc_req;
1302 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1306 if (space <= sizeof (ifr))
1308 ifnlen = snprintf(workbuf, sizeof(workbuf),
1309 "%s%d", ifp->if_name, ifp->if_unit);
1310 if(ifnlen + 1 > sizeof ifr.ifr_name) {
1311 error = ENAMETOOLONG;
1314 strcpy(ifr.ifr_name, workbuf);
1318 ifa = ifp->if_addrhead.tqh_first;
1319 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1320 if (space <= sizeof(ifr))
1323 if (curproc->p_prison && prison_if(curproc, sa))
1327 if (cmd == OSIOCGIFCONF) {
1328 struct osockaddr *osa =
1329 (struct osockaddr *)&ifr.ifr_addr;
1331 osa->sa_family = sa->sa_family;
1332 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1337 if (sa->sa_len <= sizeof(*sa)) {
1339 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1343 if (space < sizeof (ifr) + sa->sa_len -
1346 space -= sa->sa_len - sizeof(*sa);
1347 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1348 sizeof (ifr.ifr_name));
1350 error = copyout((caddr_t)sa,
1351 (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1352 ifrp = (struct ifreq *)
1353 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1357 space -= sizeof (ifr);
1362 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1363 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1367 space -= sizeof (ifr);
1371 ifc->ifc_len -= space;
1376 * Just like if_promisc(), but for all-multicast-reception mode.
1379 if_allmulti(ifp, onswitch)
1388 if (ifp->if_amcount++ == 0) {
1389 ifp->if_flags |= IFF_ALLMULTI;
1390 ifr.ifr_flags = ifp->if_flags;
1391 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1392 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1395 if (ifp->if_amcount > 1) {
1398 ifp->if_amcount = 0;
1399 ifp->if_flags &= ~IFF_ALLMULTI;
1400 ifr.ifr_flags = ifp->if_flags;
1401 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1402 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1413 * Add a multicast listenership to the interface in question.
1414 * The link layer provides a routine which converts
1417 if_addmulti(ifp, sa, retifma)
1418 struct ifnet *ifp; /* interface to manipulate */
1419 struct sockaddr *sa; /* address to add */
1420 struct ifmultiaddr **retifma;
1422 struct sockaddr *llsa, *dupsa;
1424 struct ifmultiaddr *ifma;
1427 * If the matching multicast address already exists
1428 * then don't add a new one, just add a reference
1430 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1431 if (equal(sa, ifma->ifma_addr)) {
1432 ifma->ifma_refcount++;
1440 * Give the link layer a chance to accept/reject it, and also
1441 * find out which AF_LINK address this maps to, if it isn't one
1444 if (ifp->if_resolvemulti) {
1445 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1446 if (error) return error;
1451 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1452 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1453 bcopy(sa, dupsa, sa->sa_len);
1455 ifma->ifma_addr = dupsa;
1456 ifma->ifma_lladdr = llsa;
1457 ifma->ifma_ifp = ifp;
1458 ifma->ifma_refcount = 1;
1459 ifma->ifma_protospec = 0;
1460 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1463 * Some network interfaces can scan the address list at
1464 * interrupt time; lock them out.
1467 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1472 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1473 if (equal(ifma->ifma_addr, llsa))
1477 ifma->ifma_refcount++;
1479 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1480 M_IFMADDR, M_WAITOK);
1481 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1482 M_IFMADDR, M_WAITOK);
1483 bcopy(llsa, dupsa, llsa->sa_len);
1484 ifma->ifma_addr = dupsa;
1485 ifma->ifma_ifp = ifp;
1486 ifma->ifma_refcount = 1;
1488 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1493 * We are certain we have added something, so call down to the
1494 * interface to let them know about it.
1497 ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1504 * Remove a reference to a multicast address on this interface. Yell
1505 * if the request does not match an existing membership.
1508 if_delmulti(ifp, sa)
1510 struct sockaddr *sa;
1512 struct ifmultiaddr *ifma;
1515 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1516 if (equal(sa, ifma->ifma_addr))
1521 if (ifma->ifma_refcount > 1) {
1522 ifma->ifma_refcount--;
1526 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1527 sa = ifma->ifma_lladdr;
1529 LIST_REMOVE(ifma, ifma_link);
1531 * Make sure the interface driver is notified
1532 * in the case of a link layer mcast group being left.
1534 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0)
1535 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1537 free(ifma->ifma_addr, M_IFMADDR);
1538 free(ifma, M_IFMADDR);
1543 * Now look for the link-layer address which corresponds to
1544 * this network address. It had been squirreled away in
1545 * ifma->ifma_lladdr for this purpose (so we don't have
1546 * to call ifp->if_resolvemulti() again), and we saved that
1547 * value in sa above. If some nasty deleted the
1548 * link-layer address out from underneath us, we can deal because
1549 * the address we stored was is not the same as the one which was
1550 * in the record for the link-layer address. (So we don't complain
1553 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1554 if (equal(sa, ifma->ifma_addr))
1559 if (ifma->ifma_refcount > 1) {
1560 ifma->ifma_refcount--;
1565 LIST_REMOVE(ifma, ifma_link);
1566 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1568 free(ifma->ifma_addr, M_IFMADDR);
1569 free(sa, M_IFMADDR);
1570 free(ifma, M_IFMADDR);
1576 * Set the link layer address on an interface.
1578 * At this time we only support certain types of interfaces,
1579 * and we don't allow the length of the address to change.
1582 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1584 struct sockaddr_dl *sdl;
1588 ifa = ifnet_addrs[ifp->if_index - 1];
1591 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1594 if (len != sdl->sdl_alen) /* don't allow length to change */
1596 switch (ifp->if_type) {
1597 case IFT_ETHER: /* these types use struct arpcom */
1602 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1605 bcopy(lladdr, LLADDR(sdl), len);
1611 * If the interface is already up, we need
1612 * to re-init it in order to reprogram its
1615 if ((ifp->if_flags & IFF_UP) != 0) {
1616 ifp->if_flags &= ~IFF_UP;
1617 ifr.ifr_flags = ifp->if_flags;
1618 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1619 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1620 ifp->if_flags |= IFF_UP;
1621 ifr.ifr_flags = ifp->if_flags;
1622 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1623 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1626 * Also send gratuitous ARPs to notify other nodes about
1627 * the address change.
1629 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1630 if (ifa->ifa_addr != NULL &&
1631 ifa->ifa_addr->sa_family == AF_INET)
1632 arp_ifinit(ifp, ifa);
1639 struct ifmultiaddr *
1640 ifmaof_ifpforaddr(sa, ifp)
1641 struct sockaddr *sa;
1644 struct ifmultiaddr *ifma;
1646 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1647 if (equal(ifma->ifma_addr, sa))
1654 if_printf(struct ifnet *ifp, const char *fmt, ...)
1659 retval = printf("%s%d: ", ifp->if_name, ifp->if_unit);
1661 retval += vprintf(fmt, ap);
1666 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1667 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");