1 //==========================================================================
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
23 * Copyright (c) 1980, 1986, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)if.c 8.3 (Berkeley) 1/4/94
55 * $FreeBSD: src/sys/net/if.c,v 1.85.2.9 2001/07/24 19:10:17 brooks Exp $
58 #include <sys/param.h>
59 #include <sys/malloc.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/protosw.h>
64 #include <sys/sockio.h>
65 #include <sys/sysctl.h>
68 #include <net/if_arp.h>
69 #include <net/if_dl.h>
70 #include <net/if_types.h>
71 #include <net/radix.h>
72 #include <net/route.h>
74 #if defined(INET) || defined(INET6)
76 #include <netinet/in.h>
77 #include <netinet/in_var.h>
79 #include <netinet6/in6_var.h>
80 #include <netinet6/in6_ifattach.h>
85 * System initialization
88 static int ifconf __P((u_long, caddr_t));
89 static void ifinit __P((void *));
90 static void if_qflush __P((struct ifqueue *));
91 static void if_slowtimo __P((void *));
92 static void link_rtrequest __P((int, struct rtentry *, struct sockaddr *));
93 static int if_rtdel __P((struct radix_node *, void *));
95 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
97 int ifqmaxlen = IFQ_MAXLEN;
98 struct ifnethead ifnet; /* depend on static init XXX */
102 * XXX: declare here to avoid to include many inet6 related files..
103 * should be more generalized?
105 extern void nd6_setmtu __P((struct ifnet *));
108 struct if_clone *if_clone_lookup __P((const char *, int *));
109 int if_clone_list __P((struct if_clonereq *));
111 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
112 int if_cloners_count;
115 * Network interface utility routines.
117 * Routines with ifa_ifwith* names take sockaddr *'s as
130 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
131 log(LOG_INIT, "IFP: %p, next: %p\n", ifp, ifp->if_link.tqe_next);
139 struct ifaddr **ifnet_addrs;
140 struct ifnet **ifindex2ifnet = NULL;
143 _sa(struct ifaddr *ifa)
145 struct sockaddr *sa = ifa->ifa_addr;
146 static char _unknown[128];
148 switch (sa->sa_family) {
150 return inet_ntoa((struct in_addr)((struct sockaddr_in *)(ifa->ifa_addr))->sin_addr);
153 diag_sprintf(_unknown, "%s/%d", ip6_sprintf(IFA_IN6(ifa)),
154 in6_mask2len(&((struct in6_ifaddr *)ifa)->ia_prefixmask.sin6_addr, NULL));
158 diag_sprintf(_unknown, "<<%p>>", sa);
161 diag_sprintf(_unknown, "[%d]", sa->sa_family);
167 _show_ifp(struct ifnet *ifp)
171 diag_printf("IFP: %p (%s%d)\n", ifp, ifp->if_name, ifp->if_unit);
172 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
173 diag_printf("IFA: %p - %s\n", ifa, _sa(ifa));
179 * Attach an interface to the
180 * list of "active" interfaces.
186 unsigned socksize, ifasize;
187 int namelen, masklen;
189 register struct sockaddr_dl *sdl;
190 register struct ifaddr *ifa;
191 static int if_indexlim = 8;
199 if (ifp->if_snd.ifq_maxlen == 0) {
200 ifp->if_snd.ifq_maxlen = ifqmaxlen;
203 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
204 ifp->if_index = ++if_index;
207 * The old code would work if the interface passed a pre-existing
208 * chain of ifaddrs to this code. We don't trust our callers to
209 * properly initialize the tailq, however, so we no longer allow
210 * this unlikely case.
212 TAILQ_INIT(&ifp->if_addrhead);
214 diag_printf("%s.%d - After initialize list %p\n",
215 __FUNCTION__, __LINE__,
219 TAILQ_INIT(&ifp->if_prefixhead);
220 LIST_INIT(&ifp->if_multiaddrs);
221 getmicrotime(&ifp->if_lastchange);
222 if (ifnet_addrs == 0 || if_index >= if_indexlim) {
223 unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
224 caddr_t q = malloc(n, M_IFADDR, M_WAITOK);
227 bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
228 free((caddr_t)ifnet_addrs, M_IFADDR);
230 ifnet_addrs = (struct ifaddr **)q;
232 /* grow ifindex2ifnet */
233 n = if_indexlim * sizeof(struct ifnet *);
234 q = malloc(n, M_IFADDR, M_WAITOK);
237 bcopy((caddr_t)ifindex2ifnet, q, n/2);
238 free((caddr_t)ifindex2ifnet, M_IFADDR);
240 ifindex2ifnet = (struct ifnet **)q;
243 ifindex2ifnet[if_index] = ifp;
246 * create a Link Level name for this device
248 namelen = snprintf(workbuf, sizeof(workbuf),
249 "%s%d", ifp->if_name, ifp->if_unit);
250 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
251 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
252 socksize = masklen + ifp->if_addrlen;
253 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
254 if (socksize < sizeof(*sdl))
255 socksize = sizeof(*sdl);
256 socksize = ROUNDUP(socksize);
257 ifasize = sizeof(*ifa) + 2 * socksize;
258 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
260 bzero((caddr_t)ifa, ifasize);
261 sdl = (struct sockaddr_dl *)(ifa + 1);
262 sdl->sdl_len = socksize;
263 sdl->sdl_family = AF_LINK;
264 bcopy(workbuf, sdl->sdl_data, namelen);
265 sdl->sdl_nlen = namelen;
266 sdl->sdl_index = ifp->if_index;
267 sdl->sdl_type = ifp->if_type;
268 ifnet_addrs[if_index - 1] = ifa;
270 ifa->ifa_rtrequest = link_rtrequest;
271 ifa->ifa_addr = (struct sockaddr *)sdl;
272 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
273 ifa->ifa_netmask = (struct sockaddr *)sdl;
274 sdl->sdl_len = masklen;
276 sdl->sdl_data[--namelen] = 0xff;
277 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
279 diag_printf("%s.%d - After inserting %p into list %p\n",
280 __FUNCTION__, __LINE__,
281 ifa, &ifp->if_addrlist);
286 ifp->if_snd.altq_type = 0;
287 ifp->if_snd.altq_disc = NULL;
288 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
289 ifp->if_snd.altq_tbr = NULL;
290 ifp->if_snd.altq_ifp = ifp;
295 * Detach an interface, removing it from the
296 * list of "active" interfaces.
303 struct radix_node_head *rnh;
308 * Remove routes and flush queues.
313 if (ALTQ_IS_ENABLED(&ifp->if_snd))
314 altq_disable(&ifp->if_snd);
315 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
316 altq_detach(&ifp->if_snd);
320 * Remove address from ifnet_addrs[] and maybe decrement if_index.
321 * Clean up all addresses.
323 ifnet_addrs[ifp->if_index - 1] = 0;
324 while (if_index > 0 && ifnet_addrs[if_index - 1] == 0)
327 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
328 ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
330 /* XXX: Ugly!! ad hoc just for INET */
331 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
332 struct ifaliasreq ifr;
334 bzero(&ifr, sizeof(ifr));
335 ifr.ifra_addr = *ifa->ifa_addr;
336 if (ifa->ifa_dstaddr)
337 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
338 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
344 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
346 /* ifp_addrhead is already updated */
350 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
356 * Remove all IPv6 kernel structs related to ifp. This should be done
357 * before removing routing entries below, since IPv6 interface direct
358 * routes are expected to be removed by the IPv6-specific kernel API.
359 * Otherwise, the kernel will detect some inconsistency and bark it.
365 * Delete all remaining routes using this interface
366 * Unfortuneatly the only way to do this is to slog through
367 * the entire routing table looking for routes which point
368 * to this interface...oh well...
370 for (i = 1; i <= AF_MAX; i++) {
371 if ((rnh = rt_tables[i]) == NULL)
373 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
376 TAILQ_REMOVE(&ifnet, ifp, if_link);
381 * Delete Routes for a Network Interface
383 * Called for each routing entry via the rnh->rnh_walktree() call above
384 * to delete all route entries referencing a detaching network interface.
387 * rn pointer to node in the routing table
388 * arg argument passed to rnh->rnh_walktree() - detaching interface
392 * errno failed - reason indicated
397 struct radix_node *rn;
400 struct rtentry *rt = (struct rtentry *)rn;
401 struct ifnet *ifp = arg;
404 if (rt->rt_ifp == ifp) {
407 * Protect (sorta) against walktree recursion problems
410 if ((rt->rt_flags & RTF_UP) == 0)
413 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
414 rt_mask(rt), rt->rt_flags,
415 (struct rtentry **) NULL);
417 log(LOG_WARNING, "if_rtdel: error %d\n", err);
425 * Create a clone network interface.
428 if_clone_create(name, len)
432 struct if_clone *ifc;
438 ifc = if_clone_lookup(name, &unit);
442 if (ifunit(name) != NULL)
445 wildcard = (unit < 0);
447 err = (*ifc->ifc_create)(ifc, &unit);
451 /* In the wildcard case, we need to update the name. */
453 for (dp = name; *dp != '\0'; dp++);
454 if (snprintf(dp, len - (dp-name), "%d", unit) >
455 len - (dp-name) - 1) {
457 * This can only be a programmer error and
458 * there's no straightforward way to recover if
461 panic("if_clone_create(): interface name too long");
470 * Destroy a clone network interface.
473 if_clone_destroy(name)
476 struct if_clone *ifc;
479 ifc = if_clone_lookup(name, NULL);
487 if (ifc->ifc_destroy == NULL)
490 (*ifc->ifc_destroy)(ifp);
495 * Look up a network interface cloner.
498 if_clone_lookup(name, unitp)
502 struct if_clone *ifc;
506 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
507 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
508 if (ifc->ifc_name[i] != *cp)
513 ifc = LIST_NEXT(ifc, ifc_list);
517 return ((struct if_clone *)NULL);
523 for (i = 0; *cp != '\0'; cp++) {
524 if (*cp < '0' || *cp > '9') {
525 /* Bogus unit number. */
528 i = (i * 10) + (*cp - '0');
538 * Register a network interface cloner.
542 struct if_clone *ifc;
545 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
550 * Unregister a network interface cloner.
554 struct if_clone *ifc;
557 LIST_REMOVE(ifc, ifc_list);
562 * Provide list of interface cloners to userspace.
566 struct if_clonereq *ifcr;
568 char outbuf[IFNAMSIZ], *dst;
569 struct if_clone *ifc;
570 int count, error = 0;
572 ifcr->ifcr_total = if_cloners_count;
573 if ((dst = ifcr->ifcr_buffer) == NULL) {
574 /* Just asking how many there are. */
578 if (ifcr->ifcr_count < 0)
581 count = (if_cloners_count < ifcr->ifcr_count) ?
582 if_cloners_count : ifcr->ifcr_count;
584 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
585 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
586 strncpy(outbuf, ifc->ifc_name, IFNAMSIZ);
587 outbuf[IFNAMSIZ - 1] = '\0'; /* sanity */
588 error = copyout(outbuf, dst, IFNAMSIZ);
597 * Locate an interface based on a complete address.
602 register struct sockaddr *addr;
604 register struct ifnet *ifp;
605 register struct ifaddr *ifa;
607 #define equal(a1, a2) \
608 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
609 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
610 for (ifa = ifp->if_addrhead.tqh_first; ifa;
611 ifa = ifa->ifa_link.tqe_next) {
612 if (ifa->ifa_addr->sa_family != addr->sa_family)
614 if (equal(addr, ifa->ifa_addr))
616 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
617 /* IP6 doesn't have broadcast */
618 ifa->ifa_broadaddr->sa_len != 0 &&
619 equal(ifa->ifa_broadaddr, addr))
622 return ((struct ifaddr *)0);
625 * Locate the point to point interface with a given destination address.
629 ifa_ifwithdstaddr(addr)
630 register struct sockaddr *addr;
632 register struct ifnet *ifp;
633 register struct ifaddr *ifa;
635 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
636 if (ifp->if_flags & IFF_POINTOPOINT)
637 for (ifa = ifp->if_addrhead.tqh_first; ifa;
638 ifa = ifa->ifa_link.tqe_next) {
639 if (ifa->ifa_addr->sa_family != addr->sa_family)
641 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
644 return ((struct ifaddr *)0);
648 * Find an interface on a specific network. If many, choice
649 * is most specific found.
653 struct sockaddr *addr;
655 register struct ifnet *ifp;
656 register struct ifaddr *ifa;
657 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
658 u_int af = addr->sa_family;
659 char *addr_data = addr->sa_data, *cplim;
662 * AF_LINK addresses can be looked up directly by their index number,
663 * so do that if we can.
666 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
667 if (sdl->sdl_index && sdl->sdl_index <= if_index)
668 return (ifnet_addrs[sdl->sdl_index - 1]);
672 * Scan though each interface, looking for ones that have
673 * addresses in this address family.
675 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
676 for (ifa = ifp->if_addrhead.tqh_first; ifa;
677 ifa = ifa->ifa_link.tqe_next) {
678 register char *cp, *cp2, *cp3;
680 if (ifa->ifa_addr->sa_family != af)
683 #ifdef INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
684 addr->sa_family != AF_INET6 &&
686 ifp->if_flags & IFF_POINTOPOINT) {
688 * This is a bit broken as it doesn't
689 * take into account that the remote end may
690 * be a single node in the network we are
692 * The trouble is that we don't know the
693 * netmask for the remote end.
695 if (ifa->ifa_dstaddr != 0
696 && equal(addr, ifa->ifa_dstaddr))
700 * if we have a special address handler,
701 * then use it instead of the generic one.
703 if (ifa->ifa_claim_addr) {
704 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
712 * Scan all the bits in the ifa's address.
713 * If a bit dissagrees with what we are
714 * looking for, mask it with the netmask
715 * to see if it really matters.
718 if (ifa->ifa_netmask == 0)
721 cp2 = ifa->ifa_addr->sa_data;
722 cp3 = ifa->ifa_netmask->sa_data;
723 cplim = ifa->ifa_netmask->sa_len
724 + (char *)ifa->ifa_netmask;
726 if ((*cp++ ^ *cp2++) & *cp3++)
727 goto next; /* next address! */
729 * If the netmask of what we just found
730 * is more specific than what we had before
731 * (if we had one) then remember the new one
732 * before continuing to search
733 * for an even better one.
735 if (ifa_maybe == 0 ||
736 rn_refines((caddr_t)ifa->ifa_netmask,
737 (caddr_t)ifa_maybe->ifa_netmask))
746 * Find an interface address specific to an interface best matching
750 ifaof_ifpforaddr(addr, ifp)
751 struct sockaddr *addr;
752 register struct ifnet *ifp;
754 register struct ifaddr *ifa;
755 register char *cp, *cp2, *cp3;
756 register char *cplim;
757 struct ifaddr *ifa_maybe = 0;
758 u_int af = addr->sa_family;
762 for (ifa = ifp->if_addrhead.tqh_first; ifa;
763 ifa = ifa->ifa_link.tqe_next) {
764 if (ifa->ifa_addr->sa_family != af)
768 if (ifa->ifa_netmask == 0) {
769 if (equal(addr, ifa->ifa_addr) ||
770 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
774 if (ifp->if_flags & IFF_POINTOPOINT) {
775 if (equal(addr, ifa->ifa_dstaddr))
779 cp2 = ifa->ifa_addr->sa_data;
780 cp3 = ifa->ifa_netmask->sa_data;
781 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
782 for (; cp3 < cplim; cp3++)
783 if ((*cp++ ^ *cp2++) & *cp3)
793 * Default action when installing a route with a Link Level gateway.
794 * Lookup an appropriate real ifa to point to.
795 * This should be moved to /sys/net/link.c eventually.
798 link_rtrequest(cmd, rt, sa)
800 register struct rtentry *rt;
803 register struct ifaddr *ifa;
804 struct sockaddr *dst;
807 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
808 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
810 ifa = ifaof_ifpforaddr(dst, ifp);
815 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
816 ifa->ifa_rtrequest(cmd, rt, sa);
821 * Mark an interface down and notify protocols of
823 * NOTE: must be called at splnet or eqivalent.
826 if_unroute(ifp, flag, fam)
827 register struct ifnet *ifp;
830 register struct ifaddr *ifa;
832 ifp->if_flags &= ~flag;
833 getmicrotime(&ifp->if_lastchange);
834 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
835 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
836 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
837 if_qflush(&ifp->if_snd);
842 * Mark an interface up and notify protocols of
844 * NOTE: must be called at splnet or eqivalent.
847 if_route(ifp, flag, fam)
848 register struct ifnet *ifp;
851 register struct ifaddr *ifa;
853 ifp->if_flags |= flag;
854 getmicrotime(&ifp->if_lastchange);
855 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
856 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
857 pfctlinput(PRC_IFUP, ifa->ifa_addr);
865 * Mark an interface down and notify protocols of
867 * NOTE: must be called at splnet or eqivalent.
871 register struct ifnet *ifp;
874 if_unroute(ifp, IFF_UP, AF_UNSPEC);
878 * Mark an interface up and notify protocols of
880 * NOTE: must be called at splnet or eqivalent.
884 register struct ifnet *ifp;
887 if_route(ifp, IFF_UP, AF_UNSPEC);
891 * Flush an interface queue.
898 register struct ifqueue *ifq;
901 register struct mbuf *m, *n;
904 if (ALTQ_IS_ENABLED(ifq))
908 while ((m = n) != 0) {
918 * Handle interface watchdog timer routines. Called
919 * from softclock, we decrement timers (if set) and
920 * call the appropriate interface routine on expiration.
926 register struct ifnet *ifp;
929 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
930 if (ifp->if_timer == 0 || --ifp->if_timer)
932 if (ifp->if_watchdog)
933 (*ifp->if_watchdog)(ifp);
936 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
940 * Map interface name to
941 * interface structure pointer.
944 ifunit(const char *name)
946 char namebuf[IFNAMSIZ + 1];
954 if (len < 2 || len > IFNAMSIZ)
958 if (c < '0' || c > '9')
959 return NULL; /* trailing garbage */
964 return NULL; /* no interface name */
965 unit += (c - '0') * m;
967 return NULL; /* number is unreasonable */
970 } while (c >= '0' && c <= '9');
972 bcopy(name, namebuf, len);
975 * Now search all the interfaces for this name/number
977 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
978 if (strcmp(ifp->if_name, namebuf))
980 if (unit == ifp->if_unit)
988 * Map interface name in a sockaddr_dl to
989 * interface structure pointer.
995 char ifname[IFNAMSIZ+1];
996 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
998 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
999 (sdl->sdl_nlen > IFNAMSIZ) )
1003 * ifunit wants a null-terminated name. It may not be null-terminated
1004 * in the sockaddr. We don't want to change the caller's sockaddr,
1005 * and there might not be room to put the trailing null anyway, so we
1006 * make a local copy that we know we can null terminate safely.
1009 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1010 ifname[sdl->sdl_nlen] = '\0';
1011 return ifunit(ifname);
1017 case s: return #s; break;
1019 _ioctl_name(u_long cmd)
1021 static char unknown[32];
1033 //_NAME(SIOCGETVIFCNT);
1034 //_NAME(SIOCGETSGCNT);
1036 _NAME(OSIOCGIFADDR);
1038 _NAME(SIOCSIFDSTADDR);
1039 _NAME(OSIOCGIFDSTADDR);
1040 _NAME(SIOCGIFDSTADDR);
1041 _NAME(SIOCSIFFLAGS);
1042 _NAME(SIOCGIFFLAGS);
1043 _NAME(OSIOCGIFBRDADDR);
1044 _NAME(SIOCGIFBRDADDR);
1045 _NAME(SIOCSIFBRDADDR);
1046 _NAME(OSIOCGIFCONF);
1048 _NAME(OSIOCGIFNETMASK);
1049 _NAME(SIOCGIFNETMASK);
1050 _NAME(SIOCSIFNETMASK);
1051 _NAME(SIOCGIFMETRIC);
1052 _NAME(SIOCSIFMETRIC);
1055 _NAME(SIOCALIFADDR);
1056 _NAME(SIOCGLIFADDR);
1057 _NAME(SIOCDLIFADDR);
1058 _NAME(SIOCADDMULTI);
1059 _NAME(SIOCDELMULTI);
1064 _NAME(SIOCSIFMEDIA);
1065 _NAME(SIOCGIFMEDIA);
1066 _NAME(SIOCSIFPHYADDR );
1067 _NAME(SIOCGIFPSRCADDR);
1068 _NAME(SIOCGIFPDSTADDR);
1069 _NAME(SIOCDIFPHYADDR);
1070 _NAME(SIOCSLIFPHYADDR);
1071 _NAME(SIOCGLIFPHYADDR);
1072 _NAME(SIOCSIFGENERIC);
1073 _NAME(SIOCGIFGENERIC);
1074 _NAME(SIOCGIFSTATUS);
1075 _NAME(SIOCSIFLLADDR);
1076 _NAME(SIOCIFCREATE);
1077 _NAME(SIOCIFDESTROY);
1078 _NAME(SIOCIFGCLONERS);
1082 _NAME(SIOCGIFHWADDR);
1083 _NAME(SIOCSIFHWADDR);
1084 _NAME(SIOCGIFSTATSUD);
1085 _NAME(SIOCGIFSTATS);
1087 diag_sprintf(unknown, "0x%08lx", cmd);
1097 ifioctl(so, cmd, data, p)
1103 register struct ifnet *ifp;
1104 register struct ifreq *ifr;
1109 log(LOG_IOCTL, "%s: cmd: %s, data:\n", __FUNCTION__, _ioctl_name(cmd));
1114 return (ifconf(cmd, data));
1116 ifr = (struct ifreq *)data;
1121 return ((cmd == SIOCIFCREATE) ?
1122 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1123 if_clone_destroy(ifr->ifr_name));
1125 case SIOCIFGCLONERS:
1126 return (if_clone_list((struct if_clonereq *)data));
1129 ifp = ifunit(ifr->ifr_name);
1135 ifr->ifr_flags = ifp->if_flags;
1139 ifr->ifr_metric = ifp->if_metric;
1143 ifr->ifr_mtu = ifp->if_mtu;
1147 ifr->ifr_phys = ifp->if_physical;
1151 ifr->ifr_prevflags = ifp->if_flags;
1152 if (ifp->if_flags & IFF_SMART) {
1153 /* Smart drivers twiddle their own routes */
1154 } else if (ifp->if_flags & IFF_UP &&
1155 (ifr->ifr_flags & IFF_UP) == 0) {
1159 } else if (ifr->ifr_flags & IFF_UP &&
1160 (ifp->if_flags & IFF_UP) == 0) {
1165 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1166 (ifr->ifr_flags &~ IFF_CANTCHANGE);
1168 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1169 getmicrotime(&ifp->if_lastchange);
1173 ifp->if_metric = ifr->ifr_metric;
1174 getmicrotime(&ifp->if_lastchange);
1180 error = (*ifp->if_ioctl)(ifp, cmd, data);
1182 getmicrotime(&ifp->if_lastchange);
1187 u_long oldmtu = ifp->if_mtu;
1189 if (ifp->if_ioctl == NULL)
1190 return (EOPNOTSUPP);
1191 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1193 error = (*ifp->if_ioctl)(ifp, cmd, data);
1195 getmicrotime(&ifp->if_lastchange);
1199 * If the link MTU changed, do network layer specific procedure.
1201 if (ifp->if_mtu != oldmtu) {
1212 /* Don't allow group membership on non-multicast interfaces. */
1213 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1216 /* Don't let users screw up protocols' entries. */
1217 if (ifr->ifr_addr.sa_family != AF_LINK)
1220 log(LOG_IOCTL, "%s: %s Multi\n", __FUNCTION__,
1221 (cmd == SIOCADDMULTI) ? "Add" : "Del");
1222 log_dump(LOG_IOCTL, &ifr->ifr_addr, 32);
1223 if (cmd == SIOCADDMULTI) {
1224 struct ifmultiaddr *ifma;
1225 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1227 error = if_delmulti(ifp, &ifr->ifr_addr);
1230 getmicrotime(&ifp->if_lastchange);
1233 case SIOCSIFPHYADDR:
1234 case SIOCDIFPHYADDR:
1236 case SIOCSIFPHYADDR_IN6:
1238 case SIOCSLIFPHYADDR:
1240 case SIOCSIFGENERIC:
1241 if (ifp->if_ioctl == 0)
1242 return (EOPNOTSUPP);
1243 error = (*ifp->if_ioctl)(ifp, cmd, data);
1245 getmicrotime(&ifp->if_lastchange);
1249 ifs = (struct ifstat *)data;
1250 ifs->ascii[0] = '\0';
1252 case SIOCGIFPSRCADDR:
1253 case SIOCGIFPDSTADDR:
1254 case SIOCGLIFPHYADDR:
1256 case SIOCGIFGENERIC:
1258 case SIOCGIFSTATSUD:
1259 if (ifp->if_ioctl == 0)
1260 return (EOPNOTSUPP);
1261 return ((*ifp->if_ioctl)(ifp, cmd, data));
1264 return if_setlladdr(ifp,
1265 (u_char *)ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1268 oif_flags = ifp->if_flags;
1269 if (so->so_proto == 0)
1270 return (EOPNOTSUPP);
1272 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
1281 case SIOCSIFDSTADDR:
1283 case SIOCSIFBRDADDR:
1284 case SIOCSIFNETMASK:
1285 #if BYTE_ORDER != BIG_ENDIAN
1286 if (ifr->ifr_addr.sa_family == 0 &&
1287 ifr->ifr_addr.sa_len < 16) {
1288 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1289 ifr->ifr_addr.sa_len = 16;
1292 if (ifr->ifr_addr.sa_len == 0)
1293 ifr->ifr_addr.sa_len = 16;
1301 case OSIOCGIFDSTADDR:
1302 cmd = SIOCGIFDSTADDR;
1305 case OSIOCGIFBRDADDR:
1306 cmd = SIOCGIFBRDADDR;
1309 case OSIOCGIFNETMASK:
1310 cmd = SIOCGIFNETMASK;
1312 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
1319 case OSIOCGIFDSTADDR:
1320 case OSIOCGIFBRDADDR:
1321 case OSIOCGIFNETMASK:
1322 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1326 #endif /* COMPAT_43 */
1328 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1330 #define DELAY cyg_thread_delay
1331 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1332 if (ifp->if_flags & IFF_UP) {
1346 * Set/clear promiscuous mode on interface ifp based on the truth value
1347 * of pswitch. The calls are reference counted so that only the first
1348 * "on" request actually has an effect, as does the final "off" request.
1349 * Results are undefined if the "off" and "on" requests are not matched.
1352 ifpromisc(ifp, pswitch)
1360 oldflags = ifp->if_flags;
1363 * If the device is not configured up, we cannot put it in
1366 if ((ifp->if_flags & IFF_UP) == 0)
1368 if (ifp->if_pcount++ != 0)
1370 ifp->if_flags |= IFF_PROMISC;
1371 log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
1372 ifp->if_name, ifp->if_unit);
1374 if (--ifp->if_pcount > 0)
1376 ifp->if_flags &= ~IFF_PROMISC;
1377 log(LOG_INFO, "%s%d: promiscuous mode disabled\n",
1378 ifp->if_name, ifp->if_unit);
1380 ifr.ifr_flags = ifp->if_flags;
1381 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1385 ifp->if_flags = oldflags;
1390 * Return interface configuration
1391 * of system. List may be used
1392 * in later ioctl's (above) to get
1393 * other information.
1401 register struct ifconf *ifc = (struct ifconf *)data;
1402 register struct ifnet *ifp = ifnet.tqh_first;
1403 register struct ifaddr *ifa;
1404 struct ifreq ifr, *ifrp;
1405 int space = ifc->ifc_len, error = 0;
1407 ifrp = ifc->ifc_req;
1408 for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) {
1412 ifnlen = snprintf(workbuf, sizeof(workbuf),
1413 "%s%d", ifp->if_name, ifp->if_unit);
1414 if(ifnlen + 1 > sizeof ifr.ifr_name) {
1415 error = ENAMETOOLONG;
1418 strcpy(ifr.ifr_name, workbuf);
1422 ifa = ifp->if_addrhead.tqh_first;
1423 for ( ; space > sizeof (ifr) && ifa;
1424 ifa = ifa->ifa_link.tqe_next) {
1425 register struct sockaddr *sa = ifa->ifa_addr;
1428 if (cmd == OSIOCGIFCONF) {
1429 struct osockaddr *osa =
1430 (struct osockaddr *)&ifr.ifr_addr;
1432 osa->sa_family = sa->sa_family;
1433 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1438 if (sa->sa_len <= sizeof(*sa)) {
1440 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1444 if (space < sizeof (ifr) + sa->sa_len -
1447 space -= sa->sa_len - sizeof(*sa);
1448 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1449 sizeof (ifr.ifr_name));
1451 error = copyout((caddr_t)sa,
1452 (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1453 ifrp = (struct ifreq *)
1454 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1458 space -= sizeof (ifr);
1463 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1464 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1468 space -= sizeof (ifr);
1472 ifc->ifc_len -= space;
1477 * Just like if_promisc(), but for all-multicast-reception mode.
1480 if_allmulti(ifp, onswitch)
1488 if (ifp->if_amcount++ == 0) {
1489 ifp->if_flags |= IFF_ALLMULTI;
1490 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1493 if (ifp->if_amcount > 1) {
1496 ifp->if_amcount = 0;
1497 ifp->if_flags &= ~IFF_ALLMULTI;
1498 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1509 * Add a multicast listenership to the interface in question.
1510 * The link layer provides a routine which converts
1513 if_addmulti(ifp, sa, retifma)
1514 struct ifnet *ifp; /* interface to manipulate */
1515 struct sockaddr *sa; /* address to add */
1516 struct ifmultiaddr **retifma;
1518 struct sockaddr *llsa, *dupsa;
1520 struct ifmultiaddr *ifma;
1523 * If the matching multicast address already exists
1524 * then don't add a new one, just add a reference
1526 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1527 ifma = ifma->ifma_link.le_next) {
1528 if (equal(sa, ifma->ifma_addr)) {
1529 ifma->ifma_refcount++;
1537 * Give the link layer a chance to accept/reject it, and also
1538 * find out which AF_LINK address this maps to, if it isn't one
1541 if (ifp->if_resolvemulti) {
1542 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1543 if (error) return error;
1548 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1549 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1550 bcopy(sa, dupsa, sa->sa_len);
1552 ifma->ifma_addr = dupsa;
1553 ifma->ifma_lladdr = llsa;
1554 ifma->ifma_ifp = ifp;
1555 ifma->ifma_refcount = 1;
1556 ifma->ifma_protospec = 0;
1557 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1560 * Some network interfaces can scan the address list at
1561 * interrupt time; lock them out.
1564 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1569 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1570 ifma = ifma->ifma_link.le_next) {
1571 if (equal(ifma->ifma_addr, llsa))
1575 ifma->ifma_refcount++;
1577 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1578 M_IFMADDR, M_WAITOK);
1579 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1580 M_IFMADDR, M_WAITOK);
1581 bcopy(llsa, dupsa, llsa->sa_len);
1582 ifma->ifma_addr = dupsa;
1583 ifma->ifma_ifp = ifp;
1584 ifma->ifma_refcount = 1;
1586 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1591 * We are certain we have added something, so call down to the
1592 * interface to let them know about it.
1595 ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1602 * Remove a reference to a multicast address on this interface. Yell
1603 * if the request does not match an existing membership.
1606 if_delmulti(ifp, sa)
1608 struct sockaddr *sa;
1610 struct ifmultiaddr *ifma;
1613 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1614 ifma = ifma->ifma_link.le_next)
1615 if (equal(sa, ifma->ifma_addr))
1620 if (ifma->ifma_refcount > 1) {
1621 ifma->ifma_refcount--;
1625 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1626 sa = ifma->ifma_lladdr;
1628 LIST_REMOVE(ifma, ifma_link);
1630 * Make sure the interface driver is notified
1631 * in the case of a link layer mcast group being left.
1633 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0)
1634 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1636 free(ifma->ifma_addr, M_IFMADDR);
1637 free(ifma, M_IFMADDR);
1642 * Now look for the link-layer address which corresponds to
1643 * this network address. It had been squirreled away in
1644 * ifma->ifma_lladdr for this purpose (so we don't have
1645 * to call ifp->if_resolvemulti() again), and we saved that
1646 * value in sa above. If some nasty deleted the
1647 * link-layer address out from underneath us, we can deal because
1648 * the address we stored was is not the same as the one which was
1649 * in the record for the link-layer address. (So we don't complain
1652 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1653 ifma = ifma->ifma_link.le_next)
1654 if (equal(sa, ifma->ifma_addr))
1659 if (ifma->ifma_refcount > 1) {
1660 ifma->ifma_refcount--;
1665 LIST_REMOVE(ifma, ifma_link);
1666 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1668 free(ifma->ifma_addr, M_IFMADDR);
1669 free(sa, M_IFMADDR);
1670 free(ifma, M_IFMADDR);
1676 * Set the link layer address on an interface.
1678 * At this time we only support certain types of interfaces,
1679 * and we don't allow the length of the address to change.
1682 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1684 struct sockaddr_dl *sdl;
1687 ifa = ifnet_addrs[ifp->if_index - 1];
1690 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1693 if (len != sdl->sdl_alen) /* don't allow length to change */
1695 switch (ifp->if_type) {
1696 case IFT_ETHER: /* these types use struct arpcom */
1701 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1702 bcopy(lladdr, LLADDR(sdl), len);
1708 * If the interface is already up, we need
1709 * to re-init it in order to reprogram its
1712 if ((ifp->if_flags & IFF_UP) != 0) {
1713 ifp->if_flags &= ~IFF_UP;
1714 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
1715 ifp->if_flags |= IFF_UP;
1716 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
1721 struct ifmultiaddr *
1722 ifmaof_ifpforaddr(sa, ifp)
1723 struct sockaddr *sa;
1726 struct ifmultiaddr *ifma;
1728 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1729 ifma = ifma->ifma_link.le_next)
1730 if (equal(ifma->ifma_addr, sa))
1736 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1737 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");