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
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/radix.h>
58 #include <net/route.h>
60 #if defined(INET) || defined(INET6)
62 #include <netinet/in.h>
63 #include <netinet/in_var.h>
65 #include <machine/clock.h> /* XXX: temporal workaround for fxp issue */
70 * System initialization
73 static int ifconf __P((u_long, caddr_t));
74 static void ifinit __P((void *));
75 static void if_qflush __P((struct ifqueue *));
76 static void if_slowtimo __P((void *));
77 static void link_rtrequest __P((int, struct rtentry *, struct sockaddr *));
78 static int if_rtdel __P((struct radix_node *, void *));
80 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
82 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
83 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
85 int ifqmaxlen = IFQ_MAXLEN;
86 struct ifnethead ifnet; /* depend on static init XXX */
90 * XXX: declare here to avoid to include many inet6 related files..
91 * should be more generalized?
93 extern void nd6_setmtu __P((struct ifnet *));
97 * Network interface utility routines.
99 * Routines with ifa_ifwith* names take sockaddr *'s as
111 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
112 if (ifp->if_snd.ifq_maxlen == 0) {
113 printf("%s%d XXX: driver didn't set ifq_maxlen\n",
114 ifp->if_name, ifp->if_unit);
115 ifp->if_snd.ifq_maxlen = ifqmaxlen;
122 struct ifaddr **ifnet_addrs;
123 struct ifnet **ifindex2ifnet = NULL;
127 * Attach an interface to the
128 * list of "active" interfaces.
134 unsigned socksize, ifasize;
135 int namelen, masklen;
137 register struct sockaddr_dl *sdl;
138 register struct ifaddr *ifa;
139 static int if_indexlim = 8;
147 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
148 ifp->if_index = ++if_index;
151 * The old code would work if the interface passed a pre-existing
152 * chain of ifaddrs to this code. We don't trust our callers to
153 * properly initialize the tailq, however, so we no longer allow
154 * this unlikely case.
156 TAILQ_INIT(&ifp->if_addrhead);
157 TAILQ_INIT(&ifp->if_prefixhead);
158 LIST_INIT(&ifp->if_multiaddrs);
159 getmicrotime(&ifp->if_lastchange);
160 if (ifnet_addrs == 0 || if_index >= if_indexlim) {
161 unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
162 caddr_t q = malloc(n, M_IFADDR, M_WAITOK);
165 bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
166 free((caddr_t)ifnet_addrs, M_IFADDR);
168 ifnet_addrs = (struct ifaddr **)q;
170 /* grow ifindex2ifnet */
171 n = if_indexlim * sizeof(struct ifnet *);
172 q = malloc(n, M_IFADDR, M_WAITOK);
175 bcopy((caddr_t)ifindex2ifnet, q, n/2);
176 free((caddr_t)ifindex2ifnet, M_IFADDR);
178 ifindex2ifnet = (struct ifnet **)q;
181 ifindex2ifnet[if_index] = ifp;
184 * create a Link Level name for this device
186 namelen = snprintf(workbuf, sizeof(workbuf),
187 "%s%d", ifp->if_name, ifp->if_unit);
188 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
189 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
190 socksize = masklen + ifp->if_addrlen;
191 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
192 if (socksize < sizeof(*sdl))
193 socksize = sizeof(*sdl);
194 socksize = ROUNDUP(socksize);
195 ifasize = sizeof(*ifa) + 2 * socksize;
196 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
198 bzero((caddr_t)ifa, ifasize);
199 sdl = (struct sockaddr_dl *)(ifa + 1);
200 sdl->sdl_len = socksize;
201 sdl->sdl_family = AF_LINK;
202 bcopy(workbuf, sdl->sdl_data, namelen);
203 sdl->sdl_nlen = namelen;
204 sdl->sdl_index = ifp->if_index;
205 sdl->sdl_type = ifp->if_type;
206 ifnet_addrs[if_index - 1] = ifa;
208 ifa->ifa_rtrequest = link_rtrequest;
209 ifa->ifa_addr = (struct sockaddr *)sdl;
210 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
211 ifa->ifa_netmask = (struct sockaddr *)sdl;
212 sdl->sdl_len = masklen;
214 sdl->sdl_data[--namelen] = 0xff;
215 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
220 * Detach an interface, removing it from the
221 * list of "active" interfaces.
228 struct radix_node_head *rnh;
233 * Remove routes and flush queues.
239 * Remove address from ifnet_addrs[] and maybe decrement if_index.
240 * Clean up all addresses.
242 ifnet_addrs[ifp->if_index - 1] = 0;
243 while (if_index > 0 && ifnet_addrs[if_index - 1] == 0)
246 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
247 ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
249 /* XXX: Ugly!! ad hoc just for INET */
250 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
251 struct ifaliasreq ifr;
253 bzero(&ifr, sizeof(ifr));
254 ifr.ifra_addr = *ifa->ifa_addr;
255 if (ifa->ifa_dstaddr)
256 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
257 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
263 /* XXX: Ugly!! ad hoc just for INET6 */
264 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
265 struct in6_aliasreq ifr;
267 bzero(&ifr, sizeof(ifr));
269 *((struct sockaddr_in6 *)ifa->ifa_addr);
270 if (ifa->ifa_dstaddr)
272 *((struct sockaddr_in6 *)ifa->ifa_dstaddr);
273 if (in6_control(NULL, SIOCDIFADDR_IN6, (caddr_t)&ifr,
278 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
283 * Delete all remaining routes using this interface
284 * Unfortuneatly the only way to do this is to slog through
285 * the entire routing table looking for routes which point
286 * to this interface...oh well...
288 for (i = 1; i <= AF_MAX; i++) {
289 if ((rnh = rt_tables[i]) == NULL)
291 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
294 TAILQ_REMOVE(&ifnet, ifp, if_link);
299 * Delete Routes for a Network Interface
301 * Called for each routing entry via the rnh->rnh_walktree() call above
302 * to delete all route entries referencing a detaching network interface.
305 * rn pointer to node in the routing table
306 * arg argument passed to rnh->rnh_walktree() - detaching interface
310 * errno failed - reason indicated
315 struct radix_node *rn;
318 struct rtentry *rt = (struct rtentry *)rn;
319 struct ifnet *ifp = arg;
322 if (rt->rt_ifp == ifp) {
325 * Protect (sorta) against walktree recursion problems
328 if ((rt->rt_flags & RTF_UP) == 0)
331 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
332 rt_mask(rt), rt->rt_flags,
333 (struct rtentry **) NULL);
335 log(LOG_WARNING, "if_rtdel: error %d\n", err);
343 * Locate an interface based on a complete address.
348 register struct sockaddr *addr;
350 register struct ifnet *ifp;
351 register struct ifaddr *ifa;
353 #define equal(a1, a2) \
354 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
355 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
356 for (ifa = ifp->if_addrhead.tqh_first; ifa;
357 ifa = ifa->ifa_link.tqe_next) {
358 if (ifa->ifa_addr->sa_family != addr->sa_family)
360 if (equal(addr, ifa->ifa_addr))
362 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
363 /* IP6 doesn't have broadcast */
364 ifa->ifa_broadaddr->sa_len != 0 &&
365 equal(ifa->ifa_broadaddr, addr))
368 return ((struct ifaddr *)0);
371 * Locate the point to point interface with a given destination address.
375 ifa_ifwithdstaddr(addr)
376 register struct sockaddr *addr;
378 register struct ifnet *ifp;
379 register struct ifaddr *ifa;
381 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
382 if (ifp->if_flags & IFF_POINTOPOINT)
383 for (ifa = ifp->if_addrhead.tqh_first; ifa;
384 ifa = ifa->ifa_link.tqe_next) {
385 if (ifa->ifa_addr->sa_family != addr->sa_family)
387 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
390 return ((struct ifaddr *)0);
394 * Find an interface on a specific network. If many, choice
395 * is most specific found.
399 struct sockaddr *addr;
401 register struct ifnet *ifp;
402 register struct ifaddr *ifa;
403 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
404 u_int af = addr->sa_family;
405 char *addr_data = addr->sa_data, *cplim;
408 * AF_LINK addresses can be looked up directly by their index number,
409 * so do that if we can.
412 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
413 if (sdl->sdl_index && sdl->sdl_index <= if_index)
414 return (ifnet_addrs[sdl->sdl_index - 1]);
418 * Scan though each interface, looking for ones that have
419 * addresses in this address family.
421 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
422 for (ifa = ifp->if_addrhead.tqh_first; ifa;
423 ifa = ifa->ifa_link.tqe_next) {
424 register char *cp, *cp2, *cp3;
426 if (ifa->ifa_addr->sa_family != af)
429 #ifdef INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
430 addr->sa_family != AF_INET6 &&
432 ifp->if_flags & IFF_POINTOPOINT) {
434 * This is a bit broken as it doesn't
435 * take into account that the remote end may
436 * be a single node in the network we are
438 * The trouble is that we don't know the
439 * netmask for the remote end.
441 if (ifa->ifa_dstaddr != 0
442 && equal(addr, ifa->ifa_dstaddr))
446 * if we have a special address handler,
447 * then use it instead of the generic one.
449 if (ifa->ifa_claim_addr) {
450 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
458 * Scan all the bits in the ifa's address.
459 * If a bit dissagrees with what we are
460 * looking for, mask it with the netmask
461 * to see if it really matters.
464 if (ifa->ifa_netmask == 0)
467 cp2 = ifa->ifa_addr->sa_data;
468 cp3 = ifa->ifa_netmask->sa_data;
469 cplim = ifa->ifa_netmask->sa_len
470 + (char *)ifa->ifa_netmask;
472 if ((*cp++ ^ *cp2++) & *cp3++)
473 goto next; /* next address! */
475 * If the netmask of what we just found
476 * is more specific than what we had before
477 * (if we had one) then remember the new one
478 * before continuing to search
479 * for an even better one.
481 if (ifa_maybe == 0 ||
482 rn_refines((caddr_t)ifa->ifa_netmask,
483 (caddr_t)ifa_maybe->ifa_netmask))
492 * Find an interface address specific to an interface best matching
496 ifaof_ifpforaddr(addr, ifp)
497 struct sockaddr *addr;
498 register struct ifnet *ifp;
500 register struct ifaddr *ifa;
501 register char *cp, *cp2, *cp3;
502 register char *cplim;
503 struct ifaddr *ifa_maybe = 0;
504 u_int af = addr->sa_family;
508 for (ifa = ifp->if_addrhead.tqh_first; ifa;
509 ifa = ifa->ifa_link.tqe_next) {
510 if (ifa->ifa_addr->sa_family != af)
514 if (ifa->ifa_netmask == 0) {
515 if (equal(addr, ifa->ifa_addr) ||
516 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
520 if (ifp->if_flags & IFF_POINTOPOINT) {
521 if (equal(addr, ifa->ifa_dstaddr))
525 cp2 = ifa->ifa_addr->sa_data;
526 cp3 = ifa->ifa_netmask->sa_data;
527 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
528 for (; cp3 < cplim; cp3++)
529 if ((*cp++ ^ *cp2++) & *cp3)
538 #include <net/route.h>
541 * Default action when installing a route with a Link Level gateway.
542 * Lookup an appropriate real ifa to point to.
543 * This should be moved to /sys/net/link.c eventually.
546 link_rtrequest(cmd, rt, sa)
548 register struct rtentry *rt;
551 register struct ifaddr *ifa;
552 struct sockaddr *dst;
555 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
556 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
558 ifa = ifaof_ifpforaddr(dst, ifp);
563 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
564 ifa->ifa_rtrequest(cmd, rt, sa);
569 * Mark an interface down and notify protocols of
571 * NOTE: must be called at splnet or eqivalent.
574 if_unroute(ifp, flag, fam)
575 register struct ifnet *ifp;
578 register struct ifaddr *ifa;
580 ifp->if_flags &= ~flag;
581 getmicrotime(&ifp->if_lastchange);
582 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
583 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
584 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
585 if_qflush(&ifp->if_snd);
590 * Mark an interface up and notify protocols of
592 * NOTE: must be called at splnet or eqivalent.
595 if_route(ifp, flag, fam)
596 register struct ifnet *ifp;
599 register struct ifaddr *ifa;
601 ifp->if_flags |= flag;
602 getmicrotime(&ifp->if_lastchange);
603 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
604 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
605 pfctlinput(PRC_IFUP, ifa->ifa_addr);
613 * Mark an interface down and notify protocols of
615 * NOTE: must be called at splnet or eqivalent.
619 register struct ifnet *ifp;
622 if_unroute(ifp, IFF_UP, AF_UNSPEC);
626 * Mark an interface up and notify protocols of
628 * NOTE: must be called at splnet or eqivalent.
632 register struct ifnet *ifp;
635 if_route(ifp, IFF_UP, AF_UNSPEC);
639 * Flush an interface queue.
643 register struct ifqueue *ifq;
645 register struct mbuf *m, *n;
648 while ((m = n) != 0) {
658 * Handle interface watchdog timer routines. Called
659 * from softclock, we decrement timers (if set) and
660 * call the appropriate interface routine on expiration.
666 register struct ifnet *ifp;
669 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
670 if (ifp->if_timer == 0 || --ifp->if_timer)
672 if (ifp->if_watchdog)
673 (*ifp->if_watchdog)(ifp);
676 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
680 * Map interface name to
681 * interface structure pointer.
686 char namebuf[IFNAMSIZ + 1];
694 if (len < 2 || len > IFNAMSIZ)
698 if (c < '0' || c > '9')
699 return NULL; /* trailing garbage */
704 return NULL; /* no interface name */
705 unit += (c - '0') * m;
707 return NULL; /* number is unreasonable */
710 } while (c >= '0' && c <= '9');
712 bcopy(name, namebuf, len);
715 * Now search all the interfaces for this name/number
717 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
718 if (strcmp(ifp->if_name, namebuf))
720 if (unit == ifp->if_unit)
728 * Map interface name in a sockaddr_dl to
729 * interface structure pointer.
735 char ifname[IFNAMSIZ+1];
736 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
738 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
739 (sdl->sdl_nlen > IFNAMSIZ) )
743 * ifunit wants a null-terminated name. It may not be null-terminated
744 * in the sockaddr. We don't want to change the caller's sockaddr,
745 * and there might not be room to put the trailing null anyway, so we
746 * make a local copy that we know we can null terminate safely.
749 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
750 ifname[sdl->sdl_nlen] = '\0';
751 return ifunit(ifname);
759 ifioctl(so, cmd, data, p)
765 register struct ifnet *ifp;
766 register struct ifreq *ifr;
767 register struct ifaddr *ifa;
768 struct sockaddr_dl *sdl;
777 return (ifconf(cmd, data));
779 ifr = (struct ifreq *)data;
780 ifp = ifunit(ifr->ifr_name);
786 ifr->ifr_flags = ifp->if_flags;
790 ifr->ifr_metric = ifp->if_metric;
794 ifr->ifr_mtu = ifp->if_mtu;
798 ifr->ifr_phys = ifp->if_physical;
805 ifr->ifr_prevflags = ifp->if_flags;
806 if (ifp->if_flags & IFF_SMART) {
807 /* Smart drivers twiddle their own routes */
808 } else if (ifp->if_flags & IFF_UP &&
809 (ifr->ifr_flags & IFF_UP) == 0) {
813 } else if (ifr->ifr_flags & IFF_UP &&
814 (ifp->if_flags & IFF_UP) == 0) {
819 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
820 (ifr->ifr_flags &~ IFF_CANTCHANGE);
822 (void) (*ifp->if_ioctl)(ifp, cmd, data);
823 getmicrotime(&ifp->if_lastchange);
830 ifp->if_metric = ifr->ifr_metric;
831 getmicrotime(&ifp->if_lastchange);
840 error = (*ifp->if_ioctl)(ifp, cmd, data);
842 getmicrotime(&ifp->if_lastchange);
847 u_long oldmtu = ifp->if_mtu;
852 if (ifp->if_ioctl == NULL)
854 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
856 error = (*ifp->if_ioctl)(ifp, cmd, data);
858 getmicrotime(&ifp->if_lastchange);
862 * If the link MTU changed, do network layer specific procedure.
864 if (ifp->if_mtu != oldmtu) {
878 /* Don't allow group membership on non-multicast interfaces. */
879 if ((ifp->if_flags & IFF_MULTICAST) == 0)
882 /* Don't let users screw up protocols' entries. */
883 if (ifr->ifr_addr.sa_family != AF_LINK)
886 if (cmd == SIOCADDMULTI) {
887 struct ifmultiaddr *ifma;
888 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
890 error = if_delmulti(ifp, &ifr->ifr_addr);
893 getmicrotime(&ifp->if_lastchange);
901 if (ifp->if_ioctl == 0)
903 error = (*ifp->if_ioctl)(ifp, cmd, data);
905 getmicrotime(&ifp->if_lastchange);
909 ifs = (struct ifstat *)data;
910 ifs->ascii[0] = '\0';
914 if (ifp->if_ioctl == 0)
916 return ((*ifp->if_ioctl)(ifp, cmd, data));
922 ifa = ifnet_addrs[ifp->if_index - 1];
925 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
928 bcopy(ifr->ifr_addr.sa_data,
929 ((struct arpcom *)ifp->if_softc)->ac_enaddr,
930 ifr->ifr_addr.sa_len);
931 bcopy(ifr->ifr_addr.sa_data, LLADDR(sdl),
932 ifr->ifr_addr.sa_len);
934 * If the interface is already up, we need
935 * to re-init it in order to reprogram its
938 if (ifp->if_flags & IFF_UP) {
939 ifp->if_flags &= ~IFF_UP;
940 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
941 ifp->if_flags |= IFF_UP;
942 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
946 oif_flags = ifp->if_flags;
947 if (so->so_proto == 0)
950 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
963 #if BYTE_ORDER != BIG_ENDIAN
964 if (ifr->ifr_addr.sa_family == 0 &&
965 ifr->ifr_addr.sa_len < 16) {
966 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
967 ifr->ifr_addr.sa_len = 16;
970 if (ifr->ifr_addr.sa_len == 0)
971 ifr->ifr_addr.sa_len = 16;
979 case OSIOCGIFDSTADDR:
980 cmd = SIOCGIFDSTADDR;
983 case OSIOCGIFBRDADDR:
984 cmd = SIOCGIFBRDADDR;
987 case OSIOCGIFNETMASK:
988 cmd = SIOCGIFNETMASK;
990 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
997 case OSIOCGIFDSTADDR:
998 case OSIOCGIFBRDADDR:
999 case OSIOCGIFNETMASK:
1000 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1004 #endif /* COMPAT_43 */
1006 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1008 DELAY(100);/* XXX: temporal workaround for fxp issue*/
1009 if (ifp->if_flags & IFF_UP) {
1023 * Set/clear promiscuous mode on interface ifp based on the truth value
1024 * of pswitch. The calls are reference counted so that only the first
1025 * "on" request actually has an effect, as does the final "off" request.
1026 * Results are undefined if the "off" and "on" requests are not matched.
1029 ifpromisc(ifp, pswitch)
1038 * If the device is not configured up, we cannot put it in
1041 if ((ifp->if_flags & IFF_UP) == 0)
1043 if (ifp->if_pcount++ != 0)
1045 ifp->if_flags |= IFF_PROMISC;
1046 log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
1047 ifp->if_name, ifp->if_unit);
1049 if (--ifp->if_pcount > 0)
1051 ifp->if_flags &= ~IFF_PROMISC;
1052 log(LOG_INFO, "%s%d: promiscuous mode disabled\n",
1053 ifp->if_name, ifp->if_unit);
1055 ifr.ifr_flags = ifp->if_flags;
1056 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1063 * Return interface configuration
1064 * of system. List may be used
1065 * in later ioctl's (above) to get
1066 * other information.
1074 register struct ifconf *ifc = (struct ifconf *)data;
1075 register struct ifnet *ifp = ifnet.tqh_first;
1076 register struct ifaddr *ifa;
1077 struct ifreq ifr, *ifrp;
1078 int space = ifc->ifc_len, error = 0;
1080 ifrp = ifc->ifc_req;
1081 for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) {
1085 ifnlen = snprintf(workbuf, sizeof(workbuf),
1086 "%s%d", ifp->if_name, ifp->if_unit);
1087 if(ifnlen + 1 > sizeof ifr.ifr_name) {
1088 error = ENAMETOOLONG;
1091 strcpy(ifr.ifr_name, workbuf);
1095 ifa = ifp->if_addrhead.tqh_first;
1096 for ( ; space > sizeof (ifr) && ifa;
1097 ifa = ifa->ifa_link.tqe_next) {
1098 register struct sockaddr *sa = ifa->ifa_addr;
1099 if (curproc->p_prison && prison_if(curproc, sa))
1103 if (cmd == OSIOCGIFCONF) {
1104 struct osockaddr *osa =
1105 (struct osockaddr *)&ifr.ifr_addr;
1107 osa->sa_family = sa->sa_family;
1108 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1113 if (sa->sa_len <= sizeof(*sa)) {
1115 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1119 if (space < sizeof (ifr) + sa->sa_len -
1122 space -= sa->sa_len - sizeof(*sa);
1123 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1124 sizeof (ifr.ifr_name));
1126 error = copyout((caddr_t)sa,
1127 (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1128 ifrp = (struct ifreq *)
1129 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1133 space -= sizeof (ifr);
1138 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1139 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1143 space -= sizeof (ifr);
1147 ifc->ifc_len -= space;
1152 * Just like if_promisc(), but for all-multicast-reception mode.
1155 if_allmulti(ifp, onswitch)
1163 if (ifp->if_amcount++ == 0) {
1164 ifp->if_flags |= IFF_ALLMULTI;
1165 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1168 if (ifp->if_amcount > 1) {
1171 ifp->if_amcount = 0;
1172 ifp->if_flags &= ~IFF_ALLMULTI;
1173 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1184 * Add a multicast listenership to the interface in question.
1185 * The link layer provides a routine which converts
1188 if_addmulti(ifp, sa, retifma)
1189 struct ifnet *ifp; /* interface to manipulate */
1190 struct sockaddr *sa; /* address to add */
1191 struct ifmultiaddr **retifma;
1193 struct sockaddr *llsa, *dupsa;
1195 struct ifmultiaddr *ifma;
1198 * If the matching multicast address already exists
1199 * then don't add a new one, just add a reference
1201 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1202 ifma = ifma->ifma_link.le_next) {
1203 if (equal(sa, ifma->ifma_addr)) {
1204 ifma->ifma_refcount++;
1212 * Give the link layer a chance to accept/reject it, and also
1213 * find out which AF_LINK address this maps to, if it isn't one
1216 if (ifp->if_resolvemulti) {
1217 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1218 if (error) return error;
1223 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1224 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1225 bcopy(sa, dupsa, sa->sa_len);
1227 ifma->ifma_addr = dupsa;
1228 ifma->ifma_lladdr = llsa;
1229 ifma->ifma_ifp = ifp;
1230 ifma->ifma_refcount = 1;
1231 ifma->ifma_protospec = 0;
1232 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1235 * Some network interfaces can scan the address list at
1236 * interrupt time; lock them out.
1239 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1244 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1245 ifma = ifma->ifma_link.le_next) {
1246 if (equal(ifma->ifma_addr, llsa))
1250 ifma->ifma_refcount++;
1252 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1253 M_IFMADDR, M_WAITOK);
1254 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1255 M_IFMADDR, M_WAITOK);
1256 bcopy(llsa, dupsa, llsa->sa_len);
1257 ifma->ifma_addr = dupsa;
1258 ifma->ifma_ifp = ifp;
1259 ifma->ifma_refcount = 1;
1261 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1266 * We are certain we have added something, so call down to the
1267 * interface to let them know about it.
1270 ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1277 * Remove a reference to a multicast address on this interface. Yell
1278 * if the request does not match an existing membership.
1281 if_delmulti(ifp, sa)
1283 struct sockaddr *sa;
1285 struct ifmultiaddr *ifma;
1288 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1289 ifma = ifma->ifma_link.le_next)
1290 if (equal(sa, ifma->ifma_addr))
1295 if (ifma->ifma_refcount > 1) {
1296 ifma->ifma_refcount--;
1300 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1301 sa = ifma->ifma_lladdr;
1303 LIST_REMOVE(ifma, ifma_link);
1305 free(ifma->ifma_addr, M_IFMADDR);
1306 free(ifma, M_IFMADDR);
1311 * Now look for the link-layer address which corresponds to
1312 * this network address. It had been squirreled away in
1313 * ifma->ifma_lladdr for this purpose (so we don't have
1314 * to call ifp->if_resolvemulti() again), and we saved that
1315 * value in sa above. If some nasty deleted the
1316 * link-layer address out from underneath us, we can deal because
1317 * the address we stored was is not the same as the one which was
1318 * in the record for the link-layer address. (So we don't complain
1321 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1322 ifma = ifma->ifma_link.le_next)
1323 if (equal(sa, ifma->ifma_addr))
1328 if (ifma->ifma_refcount > 1) {
1329 ifma->ifma_refcount--;
1334 LIST_REMOVE(ifma, ifma_link);
1335 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1337 free(ifma->ifma_addr, M_IFMADDR);
1338 free(sa, M_IFMADDR);
1339 free(ifma, M_IFMADDR);
1344 struct ifmultiaddr *
1345 ifmaof_ifpforaddr(sa, ifp)
1346 struct sockaddr *sa;
1349 struct ifmultiaddr *ifma;
1351 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1352 ifma = ifma->ifma_link.le_next)
1353 if (equal(ifma->ifma_addr, sa))
1359 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1360 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");