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/if_types.h>
58 #include <net/radix.h>
59 #include <net/route.h>
61 #if defined(INET) || defined(INET6)
63 #include <netinet/in.h>
64 #include <netinet/in_var.h>
66 #include <machine/clock.h> /* XXX: temporal workaround for fxp issue */
67 #include <netinet6/in6_var.h>
68 #include <netinet6/in6_ifattach.h>
73 * System initialization
76 static int ifconf __P((u_long, caddr_t));
77 static void ifinit __P((void *));
78 static void if_qflush __P((struct ifqueue *));
79 static void if_slowtimo __P((void *));
80 static void link_rtrequest __P((int, struct rtentry *, struct sockaddr *));
81 static int if_rtdel __P((struct radix_node *, void *));
83 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
85 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
86 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
88 int ifqmaxlen = IFQ_MAXLEN;
89 struct ifnethead ifnet; /* depend on static init XXX */
93 * XXX: declare here to avoid to include many inet6 related files..
94 * should be more generalized?
96 extern void nd6_setmtu __P((struct ifnet *));
100 * Network interface utility routines.
102 * Routines with ifa_ifwith* names take sockaddr *'s as
114 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
115 if (ifp->if_snd.ifq_maxlen == 0) {
116 printf("%s%d XXX: driver didn't set ifq_maxlen\n",
117 ifp->if_name, ifp->if_unit);
118 ifp->if_snd.ifq_maxlen = ifqmaxlen;
125 struct ifaddr **ifnet_addrs;
126 struct ifnet **ifindex2ifnet = NULL;
130 * Attach an interface to the
131 * list of "active" interfaces.
137 unsigned socksize, ifasize;
138 int namelen, masklen;
140 register struct sockaddr_dl *sdl;
141 register struct ifaddr *ifa;
142 static int if_indexlim = 8;
150 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
151 ifp->if_index = ++if_index;
154 * The old code would work if the interface passed a pre-existing
155 * chain of ifaddrs to this code. We don't trust our callers to
156 * properly initialize the tailq, however, so we no longer allow
157 * this unlikely case.
159 TAILQ_INIT(&ifp->if_addrhead);
160 TAILQ_INIT(&ifp->if_prefixhead);
161 LIST_INIT(&ifp->if_multiaddrs);
162 getmicrotime(&ifp->if_lastchange);
163 if (ifnet_addrs == 0 || if_index >= if_indexlim) {
164 unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
165 caddr_t q = malloc(n, M_IFADDR, M_WAITOK);
168 bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
169 free((caddr_t)ifnet_addrs, M_IFADDR);
171 ifnet_addrs = (struct ifaddr **)q;
173 /* grow ifindex2ifnet */
174 n = if_indexlim * sizeof(struct ifnet *);
175 q = malloc(n, M_IFADDR, M_WAITOK);
178 bcopy((caddr_t)ifindex2ifnet, q, n/2);
179 free((caddr_t)ifindex2ifnet, M_IFADDR);
181 ifindex2ifnet = (struct ifnet **)q;
184 ifindex2ifnet[if_index] = ifp;
187 * create a Link Level name for this device
189 namelen = snprintf(workbuf, sizeof(workbuf),
190 "%s%d", ifp->if_name, ifp->if_unit);
191 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
192 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
193 socksize = masklen + ifp->if_addrlen;
194 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
195 if (socksize < sizeof(*sdl))
196 socksize = sizeof(*sdl);
197 socksize = ROUNDUP(socksize);
198 ifasize = sizeof(*ifa) + 2 * socksize;
199 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
201 bzero((caddr_t)ifa, ifasize);
202 sdl = (struct sockaddr_dl *)(ifa + 1);
203 sdl->sdl_len = socksize;
204 sdl->sdl_family = AF_LINK;
205 bcopy(workbuf, sdl->sdl_data, namelen);
206 sdl->sdl_nlen = namelen;
207 sdl->sdl_index = ifp->if_index;
208 sdl->sdl_type = ifp->if_type;
209 ifnet_addrs[if_index - 1] = ifa;
211 ifa->ifa_rtrequest = link_rtrequest;
212 ifa->ifa_addr = (struct sockaddr *)sdl;
213 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
214 ifa->ifa_netmask = (struct sockaddr *)sdl;
215 sdl->sdl_len = masklen;
217 sdl->sdl_data[--namelen] = 0xff;
218 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
223 * Detach an interface, removing it from the
224 * list of "active" interfaces.
231 struct radix_node_head *rnh;
236 * Remove routes and flush queues.
242 * Remove address from ifnet_addrs[] and maybe decrement if_index.
243 * Clean up all addresses.
245 ifnet_addrs[ifp->if_index - 1] = 0;
246 while (if_index > 0 && ifnet_addrs[if_index - 1] == 0)
249 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
250 ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
252 /* XXX: Ugly!! ad hoc just for INET */
253 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
254 struct ifaliasreq ifr;
256 bzero(&ifr, sizeof(ifr));
257 ifr.ifra_addr = *ifa->ifa_addr;
258 if (ifa->ifa_dstaddr)
259 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
260 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
266 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
267 in6_purgeaddr(ifa, ifp);
268 /* ifp_addrhead is already updated */
272 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
277 * Delete all remaining routes using this interface
278 * Unfortuneatly the only way to do this is to slog through
279 * the entire routing table looking for routes which point
280 * to this interface...oh well...
282 for (i = 1; i <= AF_MAX; i++) {
283 if ((rnh = rt_tables[i]) == NULL)
285 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
289 /* nuke all IPv6 kernel structs related to ifp */
293 TAILQ_REMOVE(&ifnet, ifp, if_link);
298 * Delete Routes for a Network Interface
300 * Called for each routing entry via the rnh->rnh_walktree() call above
301 * to delete all route entries referencing a detaching network interface.
304 * rn pointer to node in the routing table
305 * arg argument passed to rnh->rnh_walktree() - detaching interface
309 * errno failed - reason indicated
314 struct radix_node *rn;
317 struct rtentry *rt = (struct rtentry *)rn;
318 struct ifnet *ifp = arg;
321 if (rt->rt_ifp == ifp) {
324 * Protect (sorta) against walktree recursion problems
327 if ((rt->rt_flags & RTF_UP) == 0)
330 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
331 rt_mask(rt), rt->rt_flags,
332 (struct rtentry **) NULL);
334 log(LOG_WARNING, "if_rtdel: error %d\n", err);
342 * Locate an interface based on a complete address.
347 register struct sockaddr *addr;
349 register struct ifnet *ifp;
350 register struct ifaddr *ifa;
352 #define equal(a1, a2) \
353 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
354 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
355 for (ifa = ifp->if_addrhead.tqh_first; ifa;
356 ifa = ifa->ifa_link.tqe_next) {
357 if (ifa->ifa_addr->sa_family != addr->sa_family)
359 if (equal(addr, ifa->ifa_addr))
361 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
362 /* IP6 doesn't have broadcast */
363 ifa->ifa_broadaddr->sa_len != 0 &&
364 equal(ifa->ifa_broadaddr, addr))
367 return ((struct ifaddr *)0);
370 * Locate the point to point interface with a given destination address.
374 ifa_ifwithdstaddr(addr)
375 register struct sockaddr *addr;
377 register struct ifnet *ifp;
378 register struct ifaddr *ifa;
380 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
381 if (ifp->if_flags & IFF_POINTOPOINT)
382 for (ifa = ifp->if_addrhead.tqh_first; ifa;
383 ifa = ifa->ifa_link.tqe_next) {
384 if (ifa->ifa_addr->sa_family != addr->sa_family)
386 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
389 return ((struct ifaddr *)0);
393 * Find an interface on a specific network. If many, choice
394 * is most specific found.
398 struct sockaddr *addr;
400 register struct ifnet *ifp;
401 register struct ifaddr *ifa;
402 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
403 u_int af = addr->sa_family;
404 char *addr_data = addr->sa_data, *cplim;
407 * AF_LINK addresses can be looked up directly by their index number,
408 * so do that if we can.
411 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
412 if (sdl->sdl_index && sdl->sdl_index <= if_index)
413 return (ifnet_addrs[sdl->sdl_index - 1]);
417 * Scan though each interface, looking for ones that have
418 * addresses in this address family.
420 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
421 for (ifa = ifp->if_addrhead.tqh_first; ifa;
422 ifa = ifa->ifa_link.tqe_next) {
423 register char *cp, *cp2, *cp3;
425 if (ifa->ifa_addr->sa_family != af)
428 #ifdef INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
429 addr->sa_family != AF_INET6 &&
431 ifp->if_flags & IFF_POINTOPOINT) {
433 * This is a bit broken as it doesn't
434 * take into account that the remote end may
435 * be a single node in the network we are
437 * The trouble is that we don't know the
438 * netmask for the remote end.
440 if (ifa->ifa_dstaddr != 0
441 && equal(addr, ifa->ifa_dstaddr))
445 * if we have a special address handler,
446 * then use it instead of the generic one.
448 if (ifa->ifa_claim_addr) {
449 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
457 * Scan all the bits in the ifa's address.
458 * If a bit dissagrees with what we are
459 * looking for, mask it with the netmask
460 * to see if it really matters.
463 if (ifa->ifa_netmask == 0)
466 cp2 = ifa->ifa_addr->sa_data;
467 cp3 = ifa->ifa_netmask->sa_data;
468 cplim = ifa->ifa_netmask->sa_len
469 + (char *)ifa->ifa_netmask;
471 if ((*cp++ ^ *cp2++) & *cp3++)
472 goto next; /* next address! */
474 * If the netmask of what we just found
475 * is more specific than what we had before
476 * (if we had one) then remember the new one
477 * before continuing to search
478 * for an even better one.
480 if (ifa_maybe == 0 ||
481 rn_refines((caddr_t)ifa->ifa_netmask,
482 (caddr_t)ifa_maybe->ifa_netmask))
491 * Find an interface address specific to an interface best matching
495 ifaof_ifpforaddr(addr, ifp)
496 struct sockaddr *addr;
497 register struct ifnet *ifp;
499 register struct ifaddr *ifa;
500 register char *cp, *cp2, *cp3;
501 register char *cplim;
502 struct ifaddr *ifa_maybe = 0;
503 u_int af = addr->sa_family;
507 for (ifa = ifp->if_addrhead.tqh_first; ifa;
508 ifa = ifa->ifa_link.tqe_next) {
509 if (ifa->ifa_addr->sa_family != af)
513 if (ifa->ifa_netmask == 0) {
514 if (equal(addr, ifa->ifa_addr) ||
515 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
519 if (ifp->if_flags & IFF_POINTOPOINT) {
520 if (equal(addr, ifa->ifa_dstaddr))
524 cp2 = ifa->ifa_addr->sa_data;
525 cp3 = ifa->ifa_netmask->sa_data;
526 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
527 for (; cp3 < cplim; cp3++)
528 if ((*cp++ ^ *cp2++) & *cp3)
537 #include <net/route.h>
540 * Default action when installing a route with a Link Level gateway.
541 * Lookup an appropriate real ifa to point to.
542 * This should be moved to /sys/net/link.c eventually.
545 link_rtrequest(cmd, rt, sa)
547 register struct rtentry *rt;
550 register struct ifaddr *ifa;
551 struct sockaddr *dst;
554 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
555 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
557 ifa = ifaof_ifpforaddr(dst, ifp);
562 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
563 ifa->ifa_rtrequest(cmd, rt, sa);
568 * Mark an interface down and notify protocols of
570 * NOTE: must be called at splnet or eqivalent.
573 if_unroute(ifp, flag, fam)
574 register struct ifnet *ifp;
577 register struct ifaddr *ifa;
579 ifp->if_flags &= ~flag;
580 getmicrotime(&ifp->if_lastchange);
581 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
582 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
583 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
584 if_qflush(&ifp->if_snd);
589 * Mark an interface up and notify protocols of
591 * NOTE: must be called at splnet or eqivalent.
594 if_route(ifp, flag, fam)
595 register struct ifnet *ifp;
598 register struct ifaddr *ifa;
600 ifp->if_flags |= flag;
601 getmicrotime(&ifp->if_lastchange);
602 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
603 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
604 pfctlinput(PRC_IFUP, ifa->ifa_addr);
612 * Mark an interface down and notify protocols of
614 * NOTE: must be called at splnet or eqivalent.
618 register struct ifnet *ifp;
621 if_unroute(ifp, IFF_UP, AF_UNSPEC);
625 * Mark an interface up and notify protocols of
627 * NOTE: must be called at splnet or eqivalent.
631 register struct ifnet *ifp;
634 if_route(ifp, IFF_UP, AF_UNSPEC);
638 * Flush an interface queue.
642 register struct ifqueue *ifq;
644 register struct mbuf *m, *n;
647 while ((m = n) != 0) {
657 * Handle interface watchdog timer routines. Called
658 * from softclock, we decrement timers (if set) and
659 * call the appropriate interface routine on expiration.
665 register struct ifnet *ifp;
668 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
669 if (ifp->if_timer == 0 || --ifp->if_timer)
671 if (ifp->if_watchdog)
672 (*ifp->if_watchdog)(ifp);
675 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
679 * Map interface name to
680 * interface structure pointer.
685 char namebuf[IFNAMSIZ + 1];
693 if (len < 2 || len > IFNAMSIZ)
697 if (c < '0' || c > '9')
698 return NULL; /* trailing garbage */
703 return NULL; /* no interface name */
704 unit += (c - '0') * m;
706 return NULL; /* number is unreasonable */
709 } while (c >= '0' && c <= '9');
711 bcopy(name, namebuf, len);
714 * Now search all the interfaces for this name/number
716 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
717 if (strcmp(ifp->if_name, namebuf))
719 if (unit == ifp->if_unit)
727 * Map interface name in a sockaddr_dl to
728 * interface structure pointer.
734 char ifname[IFNAMSIZ+1];
735 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
737 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
738 (sdl->sdl_nlen > IFNAMSIZ) )
742 * ifunit wants a null-terminated name. It may not be null-terminated
743 * in the sockaddr. We don't want to change the caller's sockaddr,
744 * and there might not be room to put the trailing null anyway, so we
745 * make a local copy that we know we can null terminate safely.
748 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
749 ifname[sdl->sdl_nlen] = '\0';
750 return ifunit(ifname);
758 ifioctl(so, cmd, data, p)
764 register struct ifnet *ifp;
765 register struct ifreq *ifr;
774 return (ifconf(cmd, data));
776 ifr = (struct ifreq *)data;
777 ifp = ifunit(ifr->ifr_name);
783 ifr->ifr_flags = ifp->if_flags;
787 ifr->ifr_metric = ifp->if_metric;
791 ifr->ifr_mtu = ifp->if_mtu;
795 ifr->ifr_phys = ifp->if_physical;
802 ifr->ifr_prevflags = ifp->if_flags;
803 if (ifp->if_flags & IFF_SMART) {
804 /* Smart drivers twiddle their own routes */
805 } else if (ifp->if_flags & IFF_UP &&
806 (ifr->ifr_flags & IFF_UP) == 0) {
810 } else if (ifr->ifr_flags & IFF_UP &&
811 (ifp->if_flags & IFF_UP) == 0) {
816 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
817 (ifr->ifr_flags &~ IFF_CANTCHANGE);
819 (void) (*ifp->if_ioctl)(ifp, cmd, data);
820 getmicrotime(&ifp->if_lastchange);
827 ifp->if_metric = ifr->ifr_metric;
828 getmicrotime(&ifp->if_lastchange);
837 error = (*ifp->if_ioctl)(ifp, cmd, data);
839 getmicrotime(&ifp->if_lastchange);
844 u_long oldmtu = ifp->if_mtu;
849 if (ifp->if_ioctl == NULL)
851 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
853 error = (*ifp->if_ioctl)(ifp, cmd, data);
855 getmicrotime(&ifp->if_lastchange);
859 * If the link MTU changed, do network layer specific procedure.
861 if (ifp->if_mtu != oldmtu) {
875 /* Don't allow group membership on non-multicast interfaces. */
876 if ((ifp->if_flags & IFF_MULTICAST) == 0)
879 /* Don't let users screw up protocols' entries. */
880 if (ifr->ifr_addr.sa_family != AF_LINK)
883 if (cmd == SIOCADDMULTI) {
884 struct ifmultiaddr *ifma;
885 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
887 error = if_delmulti(ifp, &ifr->ifr_addr);
890 getmicrotime(&ifp->if_lastchange);
898 if (ifp->if_ioctl == 0)
900 error = (*ifp->if_ioctl)(ifp, cmd, data);
902 getmicrotime(&ifp->if_lastchange);
906 ifs = (struct ifstat *)data;
907 ifs->ascii[0] = '\0';
911 if (ifp->if_ioctl == 0)
913 return ((*ifp->if_ioctl)(ifp, cmd, data));
919 return if_setlladdr(ifp,
920 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
923 oif_flags = ifp->if_flags;
924 if (so->so_proto == 0)
927 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
940 #if BYTE_ORDER != BIG_ENDIAN
941 if (ifr->ifr_addr.sa_family == 0 &&
942 ifr->ifr_addr.sa_len < 16) {
943 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
944 ifr->ifr_addr.sa_len = 16;
947 if (ifr->ifr_addr.sa_len == 0)
948 ifr->ifr_addr.sa_len = 16;
956 case OSIOCGIFDSTADDR:
957 cmd = SIOCGIFDSTADDR;
960 case OSIOCGIFBRDADDR:
961 cmd = SIOCGIFBRDADDR;
964 case OSIOCGIFNETMASK:
965 cmd = SIOCGIFNETMASK;
967 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
974 case OSIOCGIFDSTADDR:
975 case OSIOCGIFBRDADDR:
976 case OSIOCGIFNETMASK:
977 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
981 #endif /* COMPAT_43 */
983 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
985 DELAY(100);/* XXX: temporal workaround for fxp issue*/
986 if (ifp->if_flags & IFF_UP) {
1000 * Set/clear promiscuous mode on interface ifp based on the truth value
1001 * of pswitch. The calls are reference counted so that only the first
1002 * "on" request actually has an effect, as does the final "off" request.
1003 * Results are undefined if the "off" and "on" requests are not matched.
1006 ifpromisc(ifp, pswitch)
1015 * If the device is not configured up, we cannot put it in
1018 if ((ifp->if_flags & IFF_UP) == 0)
1020 if (ifp->if_pcount++ != 0)
1022 ifp->if_flags |= IFF_PROMISC;
1023 log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
1024 ifp->if_name, ifp->if_unit);
1026 if (--ifp->if_pcount > 0)
1028 ifp->if_flags &= ~IFF_PROMISC;
1029 log(LOG_INFO, "%s%d: promiscuous mode disabled\n",
1030 ifp->if_name, ifp->if_unit);
1032 ifr.ifr_flags = ifp->if_flags;
1033 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1040 * Return interface configuration
1041 * of system. List may be used
1042 * in later ioctl's (above) to get
1043 * other information.
1051 register struct ifconf *ifc = (struct ifconf *)data;
1052 register struct ifnet *ifp = ifnet.tqh_first;
1053 register struct ifaddr *ifa;
1054 struct ifreq ifr, *ifrp;
1055 int space = ifc->ifc_len, error = 0;
1057 ifrp = ifc->ifc_req;
1058 for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) {
1062 ifnlen = snprintf(workbuf, sizeof(workbuf),
1063 "%s%d", ifp->if_name, ifp->if_unit);
1064 if(ifnlen + 1 > sizeof ifr.ifr_name) {
1065 error = ENAMETOOLONG;
1068 strcpy(ifr.ifr_name, workbuf);
1072 ifa = ifp->if_addrhead.tqh_first;
1073 for ( ; space > sizeof (ifr) && ifa;
1074 ifa = ifa->ifa_link.tqe_next) {
1075 register struct sockaddr *sa = ifa->ifa_addr;
1076 if (curproc->p_prison && prison_if(curproc, sa))
1080 if (cmd == OSIOCGIFCONF) {
1081 struct osockaddr *osa =
1082 (struct osockaddr *)&ifr.ifr_addr;
1084 osa->sa_family = sa->sa_family;
1085 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1090 if (sa->sa_len <= sizeof(*sa)) {
1092 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1096 if (space < sizeof (ifr) + sa->sa_len -
1099 space -= sa->sa_len - sizeof(*sa);
1100 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1101 sizeof (ifr.ifr_name));
1103 error = copyout((caddr_t)sa,
1104 (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1105 ifrp = (struct ifreq *)
1106 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1110 space -= sizeof (ifr);
1115 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1116 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1120 space -= sizeof (ifr);
1124 ifc->ifc_len -= space;
1129 * Just like if_promisc(), but for all-multicast-reception mode.
1132 if_allmulti(ifp, onswitch)
1140 if (ifp->if_amcount++ == 0) {
1141 ifp->if_flags |= IFF_ALLMULTI;
1142 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1145 if (ifp->if_amcount > 1) {
1148 ifp->if_amcount = 0;
1149 ifp->if_flags &= ~IFF_ALLMULTI;
1150 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1161 * Add a multicast listenership to the interface in question.
1162 * The link layer provides a routine which converts
1165 if_addmulti(ifp, sa, retifma)
1166 struct ifnet *ifp; /* interface to manipulate */
1167 struct sockaddr *sa; /* address to add */
1168 struct ifmultiaddr **retifma;
1170 struct sockaddr *llsa, *dupsa;
1172 struct ifmultiaddr *ifma;
1175 * If the matching multicast address already exists
1176 * then don't add a new one, just add a reference
1178 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1179 ifma = ifma->ifma_link.le_next) {
1180 if (equal(sa, ifma->ifma_addr)) {
1181 ifma->ifma_refcount++;
1189 * Give the link layer a chance to accept/reject it, and also
1190 * find out which AF_LINK address this maps to, if it isn't one
1193 if (ifp->if_resolvemulti) {
1194 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1195 if (error) return error;
1200 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1201 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1202 bcopy(sa, dupsa, sa->sa_len);
1204 ifma->ifma_addr = dupsa;
1205 ifma->ifma_lladdr = llsa;
1206 ifma->ifma_ifp = ifp;
1207 ifma->ifma_refcount = 1;
1208 ifma->ifma_protospec = 0;
1209 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1212 * Some network interfaces can scan the address list at
1213 * interrupt time; lock them out.
1216 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1221 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1222 ifma = ifma->ifma_link.le_next) {
1223 if (equal(ifma->ifma_addr, llsa))
1227 ifma->ifma_refcount++;
1229 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1230 M_IFMADDR, M_WAITOK);
1231 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1232 M_IFMADDR, M_WAITOK);
1233 bcopy(llsa, dupsa, llsa->sa_len);
1234 ifma->ifma_addr = dupsa;
1235 ifma->ifma_ifp = ifp;
1236 ifma->ifma_refcount = 1;
1238 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1243 * We are certain we have added something, so call down to the
1244 * interface to let them know about it.
1247 ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1254 * Remove a reference to a multicast address on this interface. Yell
1255 * if the request does not match an existing membership.
1258 if_delmulti(ifp, sa)
1260 struct sockaddr *sa;
1262 struct ifmultiaddr *ifma;
1265 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1266 ifma = ifma->ifma_link.le_next)
1267 if (equal(sa, ifma->ifma_addr))
1272 if (ifma->ifma_refcount > 1) {
1273 ifma->ifma_refcount--;
1277 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1278 sa = ifma->ifma_lladdr;
1280 LIST_REMOVE(ifma, ifma_link);
1282 free(ifma->ifma_addr, M_IFMADDR);
1283 free(ifma, M_IFMADDR);
1288 * Now look for the link-layer address which corresponds to
1289 * this network address. It had been squirreled away in
1290 * ifma->ifma_lladdr for this purpose (so we don't have
1291 * to call ifp->if_resolvemulti() again), and we saved that
1292 * value in sa above. If some nasty deleted the
1293 * link-layer address out from underneath us, we can deal because
1294 * the address we stored was is not the same as the one which was
1295 * in the record for the link-layer address. (So we don't complain
1298 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1299 ifma = ifma->ifma_link.le_next)
1300 if (equal(sa, ifma->ifma_addr))
1305 if (ifma->ifma_refcount > 1) {
1306 ifma->ifma_refcount--;
1311 LIST_REMOVE(ifma, ifma_link);
1312 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1314 free(ifma->ifma_addr, M_IFMADDR);
1315 free(sa, M_IFMADDR);
1316 free(ifma, M_IFMADDR);
1322 * Set the link layer address on an interface.
1324 * At this time we only support certain types of interfaces,
1325 * and we don't allow the length of the address to change.
1328 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1330 struct sockaddr_dl *sdl;
1333 ifa = ifnet_addrs[ifp->if_index - 1];
1336 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1339 if (len != sdl->sdl_alen) /* don't allow length to change */
1341 switch (ifp->if_type) {
1342 case IFT_ETHER: /* these types use struct arpcom */
1346 case IFT_PROPVIRTUAL: /* XXX waiting for IFT_8021_VLAN */
1347 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1348 bcopy(lladdr, LLADDR(sdl), len);
1354 * If the interface is already up, we need
1355 * to re-init it in order to reprogram its
1358 if ((ifp->if_flags & IFF_UP) != 0) {
1359 ifp->if_flags &= ~IFF_UP;
1360 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
1361 ifp->if_flags |= IFF_UP;
1362 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
1367 struct ifmultiaddr *
1368 ifmaof_ifpforaddr(sa, ifp)
1369 struct sockaddr *sa;
1372 struct ifmultiaddr *ifma;
1374 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1375 ifma = ifma->ifma_link.le_next)
1376 if (equal(ifma->ifma_addr, sa))
1382 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1383 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");