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.2 2003/06/17 04:28:47 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>
77 * System initialization
80 static int ifconf __P((u_long, caddr_t));
81 static void ifinit __P((void *));
82 static void if_qflush __P((struct ifqueue *));
83 static void if_slowtimo __P((void *));
84 static void link_rtrequest __P((int, struct rtentry *, struct rt_addrinfo *));
85 static int if_rtdel __P((struct radix_node *, void *));
87 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
89 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
90 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
92 int ifqmaxlen = IFQ_MAXLEN;
93 struct ifnethead ifnet; /* depend on static init XXX */
97 * XXX: declare here to avoid to include many inet6 related files..
98 * should be more generalized?
100 extern void nd6_setmtu __P((struct ifnet *));
103 struct if_clone *if_clone_lookup __P((const char *, int *));
104 int if_clone_list __P((struct if_clonereq *));
106 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
107 int if_cloners_count;
110 * Network interface utility routines.
112 * Routines with ifa_ifwith* names take sockaddr *'s as
124 TAILQ_FOREACH(ifp, &ifnet, if_link) {
125 if (ifp->if_snd.ifq_maxlen == 0) {
126 printf("%s%d XXX: driver didn't set ifq_maxlen\n",
127 ifp->if_name, ifp->if_unit);
128 ifp->if_snd.ifq_maxlen = ifqmaxlen;
136 struct ifaddr **ifnet_addrs;
137 struct ifnet **ifindex2ifnet = NULL;
141 * Attach an interface to the
142 * list of "active" interfaces.
148 unsigned socksize, ifasize;
149 int namelen, masklen;
151 register struct sockaddr_dl *sdl;
152 register struct ifaddr *ifa;
153 static int if_indexlim = 8;
161 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
162 ifp->if_index = ++if_index;
165 * The old code would work if the interface passed a pre-existing
166 * chain of ifaddrs to this code. We don't trust our callers to
167 * properly initialize the tailq, however, so we no longer allow
168 * this unlikely case.
170 TAILQ_INIT(&ifp->if_addrhead);
171 TAILQ_INIT(&ifp->if_prefixhead);
172 LIST_INIT(&ifp->if_multiaddrs);
173 getmicrotime(&ifp->if_lastchange);
174 if (ifnet_addrs == 0 || if_index >= if_indexlim) {
175 unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
176 caddr_t q = malloc(n, M_IFADDR, M_WAITOK);
179 bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
180 free((caddr_t)ifnet_addrs, M_IFADDR);
182 ifnet_addrs = (struct ifaddr **)q;
184 /* grow ifindex2ifnet */
185 n = if_indexlim * sizeof(struct ifnet *);
186 q = malloc(n, M_IFADDR, M_WAITOK);
189 bcopy((caddr_t)ifindex2ifnet, q, n/2);
190 free((caddr_t)ifindex2ifnet, M_IFADDR);
192 ifindex2ifnet = (struct ifnet **)q;
195 ifindex2ifnet[if_index] = ifp;
198 * create a Link Level name for this device
200 namelen = snprintf(workbuf, sizeof(workbuf),
201 "%s%d", ifp->if_name, ifp->if_unit);
202 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
203 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
204 socksize = masklen + ifp->if_addrlen;
205 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
206 if (socksize < sizeof(*sdl))
207 socksize = sizeof(*sdl);
208 socksize = ROUNDUP(socksize);
209 ifasize = sizeof(*ifa) + 2 * socksize;
210 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
212 bzero((caddr_t)ifa, ifasize);
213 sdl = (struct sockaddr_dl *)(ifa + 1);
214 sdl->sdl_len = socksize;
215 sdl->sdl_family = AF_LINK;
216 bcopy(workbuf, sdl->sdl_data, namelen);
217 sdl->sdl_nlen = namelen;
218 sdl->sdl_index = ifp->if_index;
219 sdl->sdl_type = ifp->if_type;
220 ifnet_addrs[if_index - 1] = ifa;
222 ifa->ifa_rtrequest = link_rtrequest;
223 ifa->ifa_addr = (struct sockaddr *)sdl;
224 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
225 ifa->ifa_netmask = (struct sockaddr *)sdl;
226 sdl->sdl_len = masklen;
228 sdl->sdl_data[--namelen] = 0xff;
229 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
232 /* Announce the interface. */
233 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
237 * Detach an interface, removing it from the
238 * list of "active" interfaces.
245 struct radix_node_head *rnh;
250 * Remove routes and flush queues.
256 * Remove address from ifnet_addrs[] and maybe decrement if_index.
257 * Clean up all addresses.
259 ifnet_addrs[ifp->if_index - 1] = 0;
260 while (if_index > 0 && ifnet_addrs[if_index - 1] == 0)
263 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
264 ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
266 /* XXX: Ugly!! ad hoc just for INET */
267 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
268 struct ifaliasreq ifr;
270 bzero(&ifr, sizeof(ifr));
271 ifr.ifra_addr = *ifa->ifa_addr;
272 if (ifa->ifa_dstaddr)
273 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
274 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
280 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
282 /* ifp_addrhead is already updated */
286 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
292 * Remove all IPv6 kernel structs related to ifp. This should be done
293 * before removing routing entries below, since IPv6 interface direct
294 * routes are expected to be removed by the IPv6-specific kernel API.
295 * Otherwise, the kernel will detect some inconsistency and bark it.
301 * Delete all remaining routes using this interface
302 * Unfortuneatly the only way to do this is to slog through
303 * the entire routing table looking for routes which point
304 * to this interface...oh well...
306 for (i = 1; i <= AF_MAX; i++) {
307 if ((rnh = rt_tables[i]) == NULL)
309 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
312 /* Announce that the interface is gone. */
313 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
315 TAILQ_REMOVE(&ifnet, ifp, if_link);
320 * Delete Routes for a Network Interface
322 * Called for each routing entry via the rnh->rnh_walktree() call above
323 * to delete all route entries referencing a detaching network interface.
326 * rn pointer to node in the routing table
327 * arg argument passed to rnh->rnh_walktree() - detaching interface
331 * errno failed - reason indicated
336 struct radix_node *rn;
339 struct rtentry *rt = (struct rtentry *)rn;
340 struct ifnet *ifp = arg;
343 if (rt->rt_ifp == ifp) {
346 * Protect (sorta) against walktree recursion problems
349 if ((rt->rt_flags & RTF_UP) == 0)
352 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
353 rt_mask(rt), rt->rt_flags,
354 (struct rtentry **) NULL);
356 log(LOG_WARNING, "if_rtdel: error %d\n", err);
364 * Create a clone network interface.
367 if_clone_create(name, len)
371 struct if_clone *ifc;
377 ifc = if_clone_lookup(name, &unit);
381 if (ifunit(name) != NULL)
384 wildcard = (unit < 0);
386 err = (*ifc->ifc_create)(ifc, &unit);
390 /* In the wildcard case, we need to update the name. */
392 for (dp = name; *dp != '\0'; dp++);
393 if (snprintf(dp, len - (dp-name), "%d", unit) >
394 len - (dp-name) - 1) {
396 * This can only be a programmer error and
397 * there's no straightforward way to recover if
400 panic("if_clone_create(): interface name too long");
409 * Destroy a clone network interface.
412 if_clone_destroy(name)
415 struct if_clone *ifc;
418 ifc = if_clone_lookup(name, NULL);
426 if (ifc->ifc_destroy == NULL)
429 (*ifc->ifc_destroy)(ifp);
434 * Look up a network interface cloner.
437 if_clone_lookup(name, unitp)
441 struct if_clone *ifc;
445 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
446 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
447 if (ifc->ifc_name[i] != *cp)
452 ifc = LIST_NEXT(ifc, ifc_list);
456 return ((struct if_clone *)NULL);
462 for (i = 0; *cp != '\0'; cp++) {
463 if (*cp < '0' || *cp > '9') {
464 /* Bogus unit number. */
467 i = (i * 10) + (*cp - '0');
477 * Register a network interface cloner.
481 struct if_clone *ifc;
484 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
489 * Unregister a network interface cloner.
493 struct if_clone *ifc;
496 LIST_REMOVE(ifc, ifc_list);
501 * Provide list of interface cloners to userspace.
505 struct if_clonereq *ifcr;
507 char outbuf[IFNAMSIZ], *dst;
508 struct if_clone *ifc;
509 int count, error = 0;
511 ifcr->ifcr_total = if_cloners_count;
512 if ((dst = ifcr->ifcr_buffer) == NULL) {
513 /* Just asking how many there are. */
517 if (ifcr->ifcr_count < 0)
520 count = (if_cloners_count < ifcr->ifcr_count) ?
521 if_cloners_count : ifcr->ifcr_count;
523 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
524 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
525 strncpy(outbuf, ifc->ifc_name, IFNAMSIZ);
526 outbuf[IFNAMSIZ - 1] = '\0'; /* sanity */
527 error = copyout(outbuf, dst, IFNAMSIZ);
536 * Locate an interface based on a complete address.
541 register struct sockaddr *addr;
543 register struct ifnet *ifp;
544 register struct ifaddr *ifa;
546 #define equal(a1, a2) \
547 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
548 TAILQ_FOREACH(ifp, &ifnet, if_link)
549 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
550 if (ifa->ifa_addr->sa_family != addr->sa_family)
552 if (equal(addr, ifa->ifa_addr))
554 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
555 /* IP6 doesn't have broadcast */
556 ifa->ifa_broadaddr->sa_len != 0 &&
557 equal(ifa->ifa_broadaddr, addr))
560 return ((struct ifaddr *)0);
563 * Locate the point to point interface with a given destination address.
567 ifa_ifwithdstaddr(addr)
568 register struct sockaddr *addr;
570 register struct ifnet *ifp;
571 register struct ifaddr *ifa;
573 TAILQ_FOREACH(ifp, &ifnet, if_link)
574 if (ifp->if_flags & IFF_POINTOPOINT)
575 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
576 if (ifa->ifa_addr->sa_family != addr->sa_family)
578 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
581 return ((struct ifaddr *)0);
585 * Find an interface on a specific network. If many, choice
586 * is most specific found.
590 struct sockaddr *addr;
592 register struct ifnet *ifp;
593 register struct ifaddr *ifa;
594 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
595 u_int af = addr->sa_family;
596 char *addr_data = addr->sa_data, *cplim;
599 * AF_LINK addresses can be looked up directly by their index number,
600 * so do that if we can.
603 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
604 if (sdl->sdl_index && sdl->sdl_index <= if_index)
605 return (ifnet_addrs[sdl->sdl_index - 1]);
609 * Scan though each interface, looking for ones that have
610 * addresses in this address family.
612 TAILQ_FOREACH(ifp, &ifnet, if_link) {
613 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
614 register char *cp, *cp2, *cp3;
616 if (ifa->ifa_addr->sa_family != af)
618 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
620 * This is a bit broken as it doesn't
621 * take into account that the remote end may
622 * be a single node in the network we are
624 * The trouble is that we don't know the
625 * netmask for the remote end.
627 if (ifa->ifa_dstaddr != 0
628 && equal(addr, ifa->ifa_dstaddr))
632 * if we have a special address handler,
633 * then use it instead of the generic one.
635 if (ifa->ifa_claim_addr) {
636 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
644 * Scan all the bits in the ifa's address.
645 * If a bit dissagrees with what we are
646 * looking for, mask it with the netmask
647 * to see if it really matters.
650 if (ifa->ifa_netmask == 0)
653 cp2 = ifa->ifa_addr->sa_data;
654 cp3 = ifa->ifa_netmask->sa_data;
655 cplim = ifa->ifa_netmask->sa_len
656 + (char *)ifa->ifa_netmask;
658 if ((*cp++ ^ *cp2++) & *cp3++)
659 goto next; /* next address! */
661 * If the netmask of what we just found
662 * is more specific than what we had before
663 * (if we had one) then remember the new one
664 * before continuing to search
665 * for an even better one.
667 if (ifa_maybe == 0 ||
668 rn_refines((caddr_t)ifa->ifa_netmask,
669 (caddr_t)ifa_maybe->ifa_netmask))
678 * Find an interface address specific to an interface best matching
682 ifaof_ifpforaddr(addr, ifp)
683 struct sockaddr *addr;
684 register struct ifnet *ifp;
686 register struct ifaddr *ifa;
687 register char *cp, *cp2, *cp3;
688 register char *cplim;
689 struct ifaddr *ifa_maybe = 0;
690 u_int af = addr->sa_family;
694 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
695 if (ifa->ifa_addr->sa_family != af)
699 if (ifa->ifa_netmask == 0) {
700 if (equal(addr, ifa->ifa_addr) ||
701 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
705 if (ifp->if_flags & IFF_POINTOPOINT) {
706 if (equal(addr, ifa->ifa_dstaddr))
710 cp2 = ifa->ifa_addr->sa_data;
711 cp3 = ifa->ifa_netmask->sa_data;
712 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
713 for (; cp3 < cplim; cp3++)
714 if ((*cp++ ^ *cp2++) & *cp3)
723 #include <net/route.h>
726 * Default action when installing a route with a Link Level gateway.
727 * Lookup an appropriate real ifa to point to.
728 * This should be moved to /sys/net/link.c eventually.
731 link_rtrequest(cmd, rt, info)
733 register struct rtentry *rt;
734 struct rt_addrinfo *info;
736 register struct ifaddr *ifa;
737 struct sockaddr *dst;
740 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
741 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
743 ifa = ifaof_ifpforaddr(dst, ifp);
748 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
749 ifa->ifa_rtrequest(cmd, rt, info);
754 * Mark an interface down and notify protocols of
756 * NOTE: must be called at splnet or eqivalent.
759 if_unroute(ifp, flag, fam)
760 register struct ifnet *ifp;
763 register struct ifaddr *ifa;
765 ifp->if_flags &= ~flag;
766 getmicrotime(&ifp->if_lastchange);
767 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
768 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
769 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
770 if_qflush(&ifp->if_snd);
775 * Mark an interface up and notify protocols of
777 * NOTE: must be called at splnet or eqivalent.
780 if_route(ifp, flag, fam)
781 register struct ifnet *ifp;
784 register struct ifaddr *ifa;
786 ifp->if_flags |= flag;
787 getmicrotime(&ifp->if_lastchange);
788 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
789 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
790 pfctlinput(PRC_IFUP, ifa->ifa_addr);
798 * Mark an interface down and notify protocols of
800 * NOTE: must be called at splnet or eqivalent.
804 register struct ifnet *ifp;
807 if_unroute(ifp, IFF_UP, AF_UNSPEC);
811 * Mark an interface up and notify protocols of
813 * NOTE: must be called at splnet or eqivalent.
817 register struct ifnet *ifp;
820 if_route(ifp, IFF_UP, AF_UNSPEC);
824 * Flush an interface queue.
828 register struct ifqueue *ifq;
830 register struct mbuf *m, *n;
833 while ((m = n) != 0) {
843 * Handle interface watchdog timer routines. Called
844 * from softclock, we decrement timers (if set) and
845 * call the appropriate interface routine on expiration.
851 register struct ifnet *ifp;
854 TAILQ_FOREACH(ifp, &ifnet, if_link) {
855 if (ifp->if_timer == 0 || --ifp->if_timer)
857 if (ifp->if_watchdog)
858 (*ifp->if_watchdog)(ifp);
861 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
865 * Map interface name to
866 * interface structure pointer.
869 ifunit(const char *name)
871 char namebuf[IFNAMSIZ + 1];
879 if (len < 2 || len > IFNAMSIZ)
883 if (c < '0' || c > '9')
884 return NULL; /* trailing garbage */
889 return NULL; /* no interface name */
890 unit += (c - '0') * m;
892 return NULL; /* number is unreasonable */
895 } while (c >= '0' && c <= '9');
897 bcopy(name, namebuf, len);
900 * Now search all the interfaces for this name/number
902 TAILQ_FOREACH(ifp, &ifnet, if_link) {
903 if (strcmp(ifp->if_name, namebuf))
905 if (unit == ifp->if_unit)
913 * Map interface name in a sockaddr_dl to
914 * interface structure pointer.
920 char ifname[IFNAMSIZ+1];
921 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
923 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
924 (sdl->sdl_nlen > IFNAMSIZ) )
928 * ifunit wants a null-terminated name. It may not be null-terminated
929 * in the sockaddr. We don't want to change the caller's sockaddr,
930 * and there might not be room to put the trailing null anyway, so we
931 * make a local copy that we know we can null terminate safely.
934 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
935 ifname[sdl->sdl_nlen] = '\0';
936 return ifunit(ifname);
944 ifioctl(so, cmd, data, p)
950 register struct ifnet *ifp;
951 register struct ifreq *ifr;
961 return (ifconf(cmd, data));
963 ifr = (struct ifreq *)data;
968 if ((error = suser(p)) != 0)
970 return ((cmd == SIOCIFCREATE) ?
971 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
972 if_clone_destroy(ifr->ifr_name));
975 return (if_clone_list((struct if_clonereq *)data));
978 ifp = ifunit(ifr->ifr_name);
984 ifr->ifr_flags = ifp->if_flags;
985 ifr->ifr_flagshigh = ifp->if_ipending >> 16;
989 ifr->ifr_reqcap = ifp->if_capabilities;
990 ifr->ifr_curcap = ifp->if_capenable;
994 ifr->ifr_metric = ifp->if_metric;
998 ifr->ifr_mtu = ifp->if_mtu;
1002 ifr->ifr_phys = ifp->if_physical;
1009 new_flags = (ifr->ifr_flags & 0xffff) |
1010 (ifr->ifr_flagshigh << 16);
1011 if (ifp->if_flags & IFF_SMART) {
1012 /* Smart drivers twiddle their own routes */
1013 } else if (ifp->if_flags & IFF_UP &&
1014 (new_flags & IFF_UP) == 0) {
1018 } else if (new_flags & IFF_UP &&
1019 (ifp->if_flags & IFF_UP) == 0) {
1024 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1025 (new_flags &~ IFF_CANTCHANGE);
1026 ifp->if_ipending = (ifp->if_ipending & IFF_CANTCHANGE) |
1027 (new_flags &~ IFF_CANTCHANGE);
1028 if (new_flags & IFF_PPROMISC) {
1029 /* Permanently promiscuous mode requested */
1030 ifp->if_flags |= IFF_PROMISC;
1031 } else if (ifp->if_pcount == 0) {
1032 ifp->if_flags &= ~IFF_PROMISC;
1035 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1036 getmicrotime(&ifp->if_lastchange);
1043 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1045 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1052 ifp->if_metric = ifr->ifr_metric;
1053 getmicrotime(&ifp->if_lastchange);
1062 error = (*ifp->if_ioctl)(ifp, cmd, data);
1064 getmicrotime(&ifp->if_lastchange);
1069 u_long oldmtu = ifp->if_mtu;
1074 if (ifp->if_ioctl == NULL)
1075 return (EOPNOTSUPP);
1076 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1078 error = (*ifp->if_ioctl)(ifp, cmd, data);
1080 getmicrotime(&ifp->if_lastchange);
1084 * If the link MTU changed, do network layer specific procedure.
1086 if (ifp->if_mtu != oldmtu) {
1100 /* Don't allow group membership on non-multicast interfaces. */
1101 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1104 /* Don't let users screw up protocols' entries. */
1105 if (ifr->ifr_addr.sa_family != AF_LINK)
1108 if (cmd == SIOCADDMULTI) {
1109 struct ifmultiaddr *ifma;
1110 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1112 error = if_delmulti(ifp, &ifr->ifr_addr);
1115 getmicrotime(&ifp->if_lastchange);
1118 case SIOCSIFPHYADDR:
1119 case SIOCDIFPHYADDR:
1121 case SIOCSIFPHYADDR_IN6:
1123 case SIOCSLIFPHYADDR:
1125 case SIOCSIFGENERIC:
1129 if (ifp->if_ioctl == 0)
1130 return (EOPNOTSUPP);
1131 error = (*ifp->if_ioctl)(ifp, cmd, data);
1133 getmicrotime(&ifp->if_lastchange);
1137 ifs = (struct ifstat *)data;
1138 ifs->ascii[0] = '\0';
1140 case SIOCGIFPSRCADDR:
1141 case SIOCGIFPDSTADDR:
1142 case SIOCGLIFPHYADDR:
1144 case SIOCGIFGENERIC:
1145 if (ifp->if_ioctl == 0)
1146 return (EOPNOTSUPP);
1147 return ((*ifp->if_ioctl)(ifp, cmd, data));
1153 return if_setlladdr(ifp,
1154 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1157 oif_flags = ifp->if_flags;
1158 if (so->so_proto == 0)
1159 return (EOPNOTSUPP);
1161 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
1170 case SIOCSIFDSTADDR:
1172 case SIOCSIFBRDADDR:
1173 case SIOCSIFNETMASK:
1174 #if BYTE_ORDER != BIG_ENDIAN
1175 if (ifr->ifr_addr.sa_family == 0 &&
1176 ifr->ifr_addr.sa_len < 16) {
1177 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1178 ifr->ifr_addr.sa_len = 16;
1181 if (ifr->ifr_addr.sa_len == 0)
1182 ifr->ifr_addr.sa_len = 16;
1190 case OSIOCGIFDSTADDR:
1191 cmd = SIOCGIFDSTADDR;
1194 case OSIOCGIFBRDADDR:
1195 cmd = SIOCGIFBRDADDR;
1198 case OSIOCGIFNETMASK:
1199 cmd = SIOCGIFNETMASK;
1201 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
1208 case OSIOCGIFDSTADDR:
1209 case OSIOCGIFBRDADDR:
1210 case OSIOCGIFNETMASK:
1211 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1215 #endif /* COMPAT_43 */
1217 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1219 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1220 if (ifp->if_flags & IFF_UP) {
1234 * Set/clear promiscuous mode on interface ifp based on the truth value
1235 * of pswitch. The calls are reference counted so that only the first
1236 * "on" request actually has an effect, as does the final "off" request.
1237 * Results are undefined if the "off" and "on" requests are not matched.
1240 ifpromisc(ifp, pswitch)
1248 oldflags = ifp->if_flags;
1249 if (ifp->if_ipending & IFF_PPROMISC) {
1250 /* Do nothing if device is in permanently promiscuous mode */
1251 ifp->if_pcount += pswitch ? 1 : -1;
1256 * If the device is not configured up, we cannot put it in
1259 if ((ifp->if_flags & IFF_UP) == 0)
1261 if (ifp->if_pcount++ != 0)
1263 ifp->if_flags |= IFF_PROMISC;
1264 log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
1265 ifp->if_name, ifp->if_unit);
1267 if (--ifp->if_pcount > 0)
1269 ifp->if_flags &= ~IFF_PROMISC;
1270 log(LOG_INFO, "%s%d: promiscuous mode disabled\n",
1271 ifp->if_name, ifp->if_unit);
1273 ifr.ifr_flags = ifp->if_flags;
1274 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1275 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1279 ifp->if_flags = oldflags;
1284 * Return interface configuration
1285 * of system. List may be used
1286 * in later ioctl's (above) to get
1287 * other information.
1295 register struct ifconf *ifc = (struct ifconf *)data;
1296 register struct ifnet *ifp;
1297 register struct ifaddr *ifa;
1298 struct sockaddr *sa;
1299 struct ifreq ifr, *ifrp;
1300 int space = ifc->ifc_len, error = 0;
1302 ifrp = ifc->ifc_req;
1303 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1307 if (space <= sizeof (ifr))
1309 ifnlen = snprintf(workbuf, sizeof(workbuf),
1310 "%s%d", ifp->if_name, ifp->if_unit);
1311 if(ifnlen + 1 > sizeof ifr.ifr_name) {
1312 error = ENAMETOOLONG;
1315 strcpy(ifr.ifr_name, workbuf);
1319 ifa = ifp->if_addrhead.tqh_first;
1320 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1321 if (space <= sizeof(ifr))
1324 if (curproc->p_prison && prison_if(curproc, sa))
1328 if (cmd == OSIOCGIFCONF) {
1329 struct osockaddr *osa =
1330 (struct osockaddr *)&ifr.ifr_addr;
1332 osa->sa_family = sa->sa_family;
1333 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1338 if (sa->sa_len <= sizeof(*sa)) {
1340 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1344 if (space < sizeof (ifr) + sa->sa_len -
1347 space -= sa->sa_len - sizeof(*sa);
1348 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1349 sizeof (ifr.ifr_name));
1351 error = copyout((caddr_t)sa,
1352 (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1353 ifrp = (struct ifreq *)
1354 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1358 space -= sizeof (ifr);
1363 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1364 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1368 space -= sizeof (ifr);
1372 ifc->ifc_len -= space;
1377 * Just like if_promisc(), but for all-multicast-reception mode.
1380 if_allmulti(ifp, onswitch)
1389 if (ifp->if_amcount++ == 0) {
1390 ifp->if_flags |= IFF_ALLMULTI;
1391 ifr.ifr_flags = ifp->if_flags;
1392 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1393 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1396 if (ifp->if_amcount > 1) {
1399 ifp->if_amcount = 0;
1400 ifp->if_flags &= ~IFF_ALLMULTI;
1401 ifr.ifr_flags = ifp->if_flags;
1402 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1403 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1414 * Add a multicast listenership to the interface in question.
1415 * The link layer provides a routine which converts
1418 if_addmulti(ifp, sa, retifma)
1419 struct ifnet *ifp; /* interface to manipulate */
1420 struct sockaddr *sa; /* address to add */
1421 struct ifmultiaddr **retifma;
1423 struct sockaddr *llsa, *dupsa;
1425 struct ifmultiaddr *ifma;
1428 * If the matching multicast address already exists
1429 * then don't add a new one, just add a reference
1431 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1432 if (equal(sa, ifma->ifma_addr)) {
1433 ifma->ifma_refcount++;
1441 * Give the link layer a chance to accept/reject it, and also
1442 * find out which AF_LINK address this maps to, if it isn't one
1445 if (ifp->if_resolvemulti) {
1446 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1447 if (error) return error;
1452 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1453 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1454 bcopy(sa, dupsa, sa->sa_len);
1456 ifma->ifma_addr = dupsa;
1457 ifma->ifma_lladdr = llsa;
1458 ifma->ifma_ifp = ifp;
1459 ifma->ifma_refcount = 1;
1460 ifma->ifma_protospec = 0;
1461 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1464 * Some network interfaces can scan the address list at
1465 * interrupt time; lock them out.
1468 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1473 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1474 if (equal(ifma->ifma_addr, llsa))
1478 ifma->ifma_refcount++;
1480 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1481 M_IFMADDR, M_WAITOK);
1482 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1483 M_IFMADDR, M_WAITOK);
1484 bcopy(llsa, dupsa, llsa->sa_len);
1485 ifma->ifma_addr = dupsa;
1486 ifma->ifma_ifp = ifp;
1487 ifma->ifma_refcount = 1;
1489 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1494 * We are certain we have added something, so call down to the
1495 * interface to let them know about it.
1498 ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1505 * Remove a reference to a multicast address on this interface. Yell
1506 * if the request does not match an existing membership.
1509 if_delmulti(ifp, sa)
1511 struct sockaddr *sa;
1513 struct ifmultiaddr *ifma;
1516 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1517 if (equal(sa, ifma->ifma_addr))
1522 if (ifma->ifma_refcount > 1) {
1523 ifma->ifma_refcount--;
1527 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1528 sa = ifma->ifma_lladdr;
1530 LIST_REMOVE(ifma, ifma_link);
1532 * Make sure the interface driver is notified
1533 * in the case of a link layer mcast group being left.
1535 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0)
1536 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1538 free(ifma->ifma_addr, M_IFMADDR);
1539 free(ifma, M_IFMADDR);
1544 * Now look for the link-layer address which corresponds to
1545 * this network address. It had been squirreled away in
1546 * ifma->ifma_lladdr for this purpose (so we don't have
1547 * to call ifp->if_resolvemulti() again), and we saved that
1548 * value in sa above. If some nasty deleted the
1549 * link-layer address out from underneath us, we can deal because
1550 * the address we stored was is not the same as the one which was
1551 * in the record for the link-layer address. (So we don't complain
1554 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1555 if (equal(sa, ifma->ifma_addr))
1560 if (ifma->ifma_refcount > 1) {
1561 ifma->ifma_refcount--;
1566 LIST_REMOVE(ifma, ifma_link);
1567 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1569 free(ifma->ifma_addr, M_IFMADDR);
1570 free(sa, M_IFMADDR);
1571 free(ifma, M_IFMADDR);
1577 * Set the link layer address on an interface.
1579 * At this time we only support certain types of interfaces,
1580 * and we don't allow the length of the address to change.
1583 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1585 struct sockaddr_dl *sdl;
1589 ifa = ifnet_addrs[ifp->if_index - 1];
1592 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1595 if (len != sdl->sdl_alen) /* don't allow length to change */
1597 switch (ifp->if_type) {
1598 case IFT_ETHER: /* these types use struct arpcom */
1603 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1606 bcopy(lladdr, LLADDR(sdl), len);
1612 * If the interface is already up, we need
1613 * to re-init it in order to reprogram its
1616 if ((ifp->if_flags & IFF_UP) != 0) {
1617 ifp->if_flags &= ~IFF_UP;
1618 ifr.ifr_flags = ifp->if_flags;
1619 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1620 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1621 ifp->if_flags |= IFF_UP;
1622 ifr.ifr_flags = ifp->if_flags;
1623 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1624 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1627 * Also send gratuitous ARPs to notify other nodes about
1628 * the address change.
1630 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1631 if (ifa->ifa_addr != NULL &&
1632 ifa->ifa_addr->sa_family == AF_INET)
1633 arp_ifinit(ifp, ifa);
1640 struct ifmultiaddr *
1641 ifmaof_ifpforaddr(sa, ifp)
1642 struct sockaddr *sa;
1645 struct ifmultiaddr *ifma;
1647 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1648 if (equal(ifma->ifma_addr, sa))
1655 if_printf(struct ifnet *ifp, const char *fmt, ...)
1660 retval = printf("%s%d: ", ifp->if_name, ifp->if_unit);
1662 retval += vprintf(fmt, ap);
1667 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1668 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");