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 */
66 #include <netinet6/in6_var.h>
67 #include <netinet6/in6_ifattach.h>
72 * System initialization
75 static int ifconf __P((u_long, caddr_t));
76 static void ifinit __P((void *));
77 static void if_qflush __P((struct ifqueue *));
78 static void if_slowtimo __P((void *));
79 static void link_rtrequest __P((int, struct rtentry *, struct sockaddr *));
80 static int if_rtdel __P((struct radix_node *, void *));
82 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
84 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
85 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
87 int ifqmaxlen = IFQ_MAXLEN;
88 struct ifnethead ifnet; /* depend on static init XXX */
92 * XXX: declare here to avoid to include many inet6 related files..
93 * should be more generalized?
95 extern void nd6_setmtu __P((struct ifnet *));
99 * Network interface utility routines.
101 * Routines with ifa_ifwith* names take sockaddr *'s as
113 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
114 if (ifp->if_snd.ifq_maxlen == 0) {
115 printf("%s%d XXX: driver didn't set ifq_maxlen\n",
116 ifp->if_name, ifp->if_unit);
117 ifp->if_snd.ifq_maxlen = ifqmaxlen;
124 struct ifaddr **ifnet_addrs;
125 struct ifnet **ifindex2ifnet = NULL;
129 * Attach an interface to the
130 * list of "active" interfaces.
136 unsigned socksize, ifasize;
137 int namelen, masklen;
139 register struct sockaddr_dl *sdl;
140 register struct ifaddr *ifa;
141 static int if_indexlim = 8;
149 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
150 ifp->if_index = ++if_index;
153 * The old code would work if the interface passed a pre-existing
154 * chain of ifaddrs to this code. We don't trust our callers to
155 * properly initialize the tailq, however, so we no longer allow
156 * this unlikely case.
158 TAILQ_INIT(&ifp->if_addrhead);
159 TAILQ_INIT(&ifp->if_prefixhead);
160 LIST_INIT(&ifp->if_multiaddrs);
161 getmicrotime(&ifp->if_lastchange);
162 if (ifnet_addrs == 0 || if_index >= if_indexlim) {
163 unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
164 caddr_t q = malloc(n, M_IFADDR, M_WAITOK);
167 bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
168 free((caddr_t)ifnet_addrs, M_IFADDR);
170 ifnet_addrs = (struct ifaddr **)q;
172 /* grow ifindex2ifnet */
173 n = if_indexlim * sizeof(struct ifnet *);
174 q = malloc(n, M_IFADDR, M_WAITOK);
177 bcopy((caddr_t)ifindex2ifnet, q, n/2);
178 free((caddr_t)ifindex2ifnet, M_IFADDR);
180 ifindex2ifnet = (struct ifnet **)q;
183 ifindex2ifnet[if_index] = ifp;
186 * create a Link Level name for this device
188 namelen = snprintf(workbuf, sizeof(workbuf),
189 "%s%d", ifp->if_name, ifp->if_unit);
190 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
191 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
192 socksize = masklen + ifp->if_addrlen;
193 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
194 if (socksize < sizeof(*sdl))
195 socksize = sizeof(*sdl);
196 socksize = ROUNDUP(socksize);
197 ifasize = sizeof(*ifa) + 2 * socksize;
198 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
200 bzero((caddr_t)ifa, ifasize);
201 sdl = (struct sockaddr_dl *)(ifa + 1);
202 sdl->sdl_len = socksize;
203 sdl->sdl_family = AF_LINK;
204 bcopy(workbuf, sdl->sdl_data, namelen);
205 sdl->sdl_nlen = namelen;
206 sdl->sdl_index = ifp->if_index;
207 sdl->sdl_type = ifp->if_type;
208 ifnet_addrs[if_index - 1] = ifa;
210 ifa->ifa_rtrequest = link_rtrequest;
211 ifa->ifa_addr = (struct sockaddr *)sdl;
212 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
213 ifa->ifa_netmask = (struct sockaddr *)sdl;
214 sdl->sdl_len = masklen;
216 sdl->sdl_data[--namelen] = 0xff;
217 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
222 * Detach an interface, removing it from the
223 * list of "active" interfaces.
230 struct radix_node_head *rnh;
235 * Remove routes and flush queues.
241 * Remove address from ifnet_addrs[] and maybe decrement if_index.
242 * Clean up all addresses.
244 ifnet_addrs[ifp->if_index - 1] = 0;
245 while (if_index > 0 && ifnet_addrs[if_index - 1] == 0)
248 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
249 ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
251 /* XXX: Ugly!! ad hoc just for INET */
252 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
253 struct ifaliasreq ifr;
255 bzero(&ifr, sizeof(ifr));
256 ifr.ifra_addr = *ifa->ifa_addr;
257 if (ifa->ifa_dstaddr)
258 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
259 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
265 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
266 in6_purgeaddr(ifa, ifp);
267 /* ifp_addrhead is already updated */
271 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
276 * Delete all remaining routes using this interface
277 * Unfortuneatly the only way to do this is to slog through
278 * the entire routing table looking for routes which point
279 * to this interface...oh well...
281 for (i = 1; i <= AF_MAX; i++) {
282 if ((rnh = rt_tables[i]) == NULL)
284 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
288 /* nuke all IPv6 kernel structs related to ifp */
292 TAILQ_REMOVE(&ifnet, ifp, if_link);
297 * Delete Routes for a Network Interface
299 * Called for each routing entry via the rnh->rnh_walktree() call above
300 * to delete all route entries referencing a detaching network interface.
303 * rn pointer to node in the routing table
304 * arg argument passed to rnh->rnh_walktree() - detaching interface
308 * errno failed - reason indicated
313 struct radix_node *rn;
316 struct rtentry *rt = (struct rtentry *)rn;
317 struct ifnet *ifp = arg;
320 if (rt->rt_ifp == ifp) {
323 * Protect (sorta) against walktree recursion problems
326 if ((rt->rt_flags & RTF_UP) == 0)
329 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
330 rt_mask(rt), rt->rt_flags,
331 (struct rtentry **) NULL);
333 log(LOG_WARNING, "if_rtdel: error %d\n", err);
341 * Locate an interface based on a complete address.
346 register struct sockaddr *addr;
348 register struct ifnet *ifp;
349 register struct ifaddr *ifa;
351 #define equal(a1, a2) \
352 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
353 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
354 for (ifa = ifp->if_addrhead.tqh_first; ifa;
355 ifa = ifa->ifa_link.tqe_next) {
356 if (ifa->ifa_addr->sa_family != addr->sa_family)
358 if (equal(addr, ifa->ifa_addr))
360 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
361 /* IP6 doesn't have broadcast */
362 ifa->ifa_broadaddr->sa_len != 0 &&
363 equal(ifa->ifa_broadaddr, addr))
366 return ((struct ifaddr *)0);
369 * Locate the point to point interface with a given destination address.
373 ifa_ifwithdstaddr(addr)
374 register struct sockaddr *addr;
376 register struct ifnet *ifp;
377 register struct ifaddr *ifa;
379 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
380 if (ifp->if_flags & IFF_POINTOPOINT)
381 for (ifa = ifp->if_addrhead.tqh_first; ifa;
382 ifa = ifa->ifa_link.tqe_next) {
383 if (ifa->ifa_addr->sa_family != addr->sa_family)
385 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
388 return ((struct ifaddr *)0);
392 * Find an interface on a specific network. If many, choice
393 * is most specific found.
397 struct sockaddr *addr;
399 register struct ifnet *ifp;
400 register struct ifaddr *ifa;
401 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
402 u_int af = addr->sa_family;
403 char *addr_data = addr->sa_data, *cplim;
406 * AF_LINK addresses can be looked up directly by their index number,
407 * so do that if we can.
410 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
411 if (sdl->sdl_index && sdl->sdl_index <= if_index)
412 return (ifnet_addrs[sdl->sdl_index - 1]);
416 * Scan though each interface, looking for ones that have
417 * addresses in this address family.
419 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
420 for (ifa = ifp->if_addrhead.tqh_first; ifa;
421 ifa = ifa->ifa_link.tqe_next) {
422 register char *cp, *cp2, *cp3;
424 if (ifa->ifa_addr->sa_family != af)
427 #ifdef INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
428 addr->sa_family != AF_INET6 &&
430 ifp->if_flags & IFF_POINTOPOINT) {
432 * This is a bit broken as it doesn't
433 * take into account that the remote end may
434 * be a single node in the network we are
436 * The trouble is that we don't know the
437 * netmask for the remote end.
439 if (ifa->ifa_dstaddr != 0
440 && equal(addr, ifa->ifa_dstaddr))
444 * if we have a special address handler,
445 * then use it instead of the generic one.
447 if (ifa->ifa_claim_addr) {
448 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
456 * Scan all the bits in the ifa's address.
457 * If a bit dissagrees with what we are
458 * looking for, mask it with the netmask
459 * to see if it really matters.
462 if (ifa->ifa_netmask == 0)
465 cp2 = ifa->ifa_addr->sa_data;
466 cp3 = ifa->ifa_netmask->sa_data;
467 cplim = ifa->ifa_netmask->sa_len
468 + (char *)ifa->ifa_netmask;
470 if ((*cp++ ^ *cp2++) & *cp3++)
471 goto next; /* next address! */
473 * If the netmask of what we just found
474 * is more specific than what we had before
475 * (if we had one) then remember the new one
476 * before continuing to search
477 * for an even better one.
479 if (ifa_maybe == 0 ||
480 rn_refines((caddr_t)ifa->ifa_netmask,
481 (caddr_t)ifa_maybe->ifa_netmask))
490 * Find an interface address specific to an interface best matching
494 ifaof_ifpforaddr(addr, ifp)
495 struct sockaddr *addr;
496 register struct ifnet *ifp;
498 register struct ifaddr *ifa;
499 register char *cp, *cp2, *cp3;
500 register char *cplim;
501 struct ifaddr *ifa_maybe = 0;
502 u_int af = addr->sa_family;
506 for (ifa = ifp->if_addrhead.tqh_first; ifa;
507 ifa = ifa->ifa_link.tqe_next) {
508 if (ifa->ifa_addr->sa_family != af)
512 if (ifa->ifa_netmask == 0) {
513 if (equal(addr, ifa->ifa_addr) ||
514 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
518 if (ifp->if_flags & IFF_POINTOPOINT) {
519 if (equal(addr, ifa->ifa_dstaddr))
523 cp2 = ifa->ifa_addr->sa_data;
524 cp3 = ifa->ifa_netmask->sa_data;
525 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
526 for (; cp3 < cplim; cp3++)
527 if ((*cp++ ^ *cp2++) & *cp3)
536 #include <net/route.h>
539 * Default action when installing a route with a Link Level gateway.
540 * Lookup an appropriate real ifa to point to.
541 * This should be moved to /sys/net/link.c eventually.
544 link_rtrequest(cmd, rt, sa)
546 register struct rtentry *rt;
549 register struct ifaddr *ifa;
550 struct sockaddr *dst;
553 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
554 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
556 ifa = ifaof_ifpforaddr(dst, ifp);
561 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
562 ifa->ifa_rtrequest(cmd, rt, sa);
567 * Mark an interface down and notify protocols of
569 * NOTE: must be called at splnet or eqivalent.
572 if_unroute(ifp, flag, fam)
573 register struct ifnet *ifp;
576 register struct ifaddr *ifa;
578 ifp->if_flags &= ~flag;
579 getmicrotime(&ifp->if_lastchange);
580 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
581 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
582 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
583 if_qflush(&ifp->if_snd);
588 * Mark an interface up and notify protocols of
590 * NOTE: must be called at splnet or eqivalent.
593 if_route(ifp, flag, fam)
594 register struct ifnet *ifp;
597 register struct ifaddr *ifa;
599 ifp->if_flags |= flag;
600 getmicrotime(&ifp->if_lastchange);
601 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
602 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
603 pfctlinput(PRC_IFUP, ifa->ifa_addr);
611 * Mark an interface down and notify protocols of
613 * NOTE: must be called at splnet or eqivalent.
617 register struct ifnet *ifp;
620 if_unroute(ifp, IFF_UP, AF_UNSPEC);
624 * Mark an interface up and notify protocols of
626 * NOTE: must be called at splnet or eqivalent.
630 register struct ifnet *ifp;
633 if_route(ifp, IFF_UP, AF_UNSPEC);
637 * Flush an interface queue.
641 register struct ifqueue *ifq;
643 register struct mbuf *m, *n;
646 while ((m = n) != 0) {
656 * Handle interface watchdog timer routines. Called
657 * from softclock, we decrement timers (if set) and
658 * call the appropriate interface routine on expiration.
664 register struct ifnet *ifp;
667 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
668 if (ifp->if_timer == 0 || --ifp->if_timer)
670 if (ifp->if_watchdog)
671 (*ifp->if_watchdog)(ifp);
674 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
678 * Map interface name to
679 * interface structure pointer.
684 char namebuf[IFNAMSIZ + 1];
692 if (len < 2 || len > IFNAMSIZ)
696 if (c < '0' || c > '9')
697 return NULL; /* trailing garbage */
702 return NULL; /* no interface name */
703 unit += (c - '0') * m;
705 return NULL; /* number is unreasonable */
708 } while (c >= '0' && c <= '9');
710 bcopy(name, namebuf, len);
713 * Now search all the interfaces for this name/number
715 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
716 if (strcmp(ifp->if_name, namebuf))
718 if (unit == ifp->if_unit)
726 * Map interface name in a sockaddr_dl to
727 * interface structure pointer.
733 char ifname[IFNAMSIZ+1];
734 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
736 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
737 (sdl->sdl_nlen > IFNAMSIZ) )
741 * ifunit wants a null-terminated name. It may not be null-terminated
742 * in the sockaddr. We don't want to change the caller's sockaddr,
743 * and there might not be room to put the trailing null anyway, so we
744 * make a local copy that we know we can null terminate safely.
747 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
748 ifname[sdl->sdl_nlen] = '\0';
749 return ifunit(ifname);
757 ifioctl(so, cmd, data, p)
763 register struct ifnet *ifp;
764 register struct ifreq *ifr;
765 register struct ifaddr *ifa;
766 struct sockaddr_dl *sdl;
775 return (ifconf(cmd, data));
777 ifr = (struct ifreq *)data;
778 ifp = ifunit(ifr->ifr_name);
784 ifr->ifr_flags = ifp->if_flags;
788 ifr->ifr_metric = ifp->if_metric;
792 ifr->ifr_mtu = ifp->if_mtu;
796 ifr->ifr_phys = ifp->if_physical;
803 ifr->ifr_prevflags = ifp->if_flags;
804 if (ifp->if_flags & IFF_SMART) {
805 /* Smart drivers twiddle their own routes */
806 } else if (ifp->if_flags & IFF_UP &&
807 (ifr->ifr_flags & IFF_UP) == 0) {
811 } else if (ifr->ifr_flags & IFF_UP &&
812 (ifp->if_flags & IFF_UP) == 0) {
817 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
818 (ifr->ifr_flags &~ IFF_CANTCHANGE);
820 (void) (*ifp->if_ioctl)(ifp, cmd, data);
821 getmicrotime(&ifp->if_lastchange);
828 ifp->if_metric = ifr->ifr_metric;
829 getmicrotime(&ifp->if_lastchange);
838 error = (*ifp->if_ioctl)(ifp, cmd, data);
840 getmicrotime(&ifp->if_lastchange);
845 u_long oldmtu = ifp->if_mtu;
850 if (ifp->if_ioctl == NULL)
852 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
854 error = (*ifp->if_ioctl)(ifp, cmd, data);
856 getmicrotime(&ifp->if_lastchange);
860 * If the link MTU changed, do network layer specific procedure.
862 if (ifp->if_mtu != oldmtu) {
876 /* Don't allow group membership on non-multicast interfaces. */
877 if ((ifp->if_flags & IFF_MULTICAST) == 0)
880 /* Don't let users screw up protocols' entries. */
881 if (ifr->ifr_addr.sa_family != AF_LINK)
884 if (cmd == SIOCADDMULTI) {
885 struct ifmultiaddr *ifma;
886 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
888 error = if_delmulti(ifp, &ifr->ifr_addr);
891 getmicrotime(&ifp->if_lastchange);
899 if (ifp->if_ioctl == 0)
901 error = (*ifp->if_ioctl)(ifp, cmd, data);
903 getmicrotime(&ifp->if_lastchange);
907 ifs = (struct ifstat *)data;
908 ifs->ascii[0] = '\0';
912 if (ifp->if_ioctl == 0)
914 return ((*ifp->if_ioctl)(ifp, cmd, data));
920 ifa = ifnet_addrs[ifp->if_index - 1];
923 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
926 bcopy(ifr->ifr_addr.sa_data,
927 ((struct arpcom *)ifp->if_softc)->ac_enaddr,
928 ifr->ifr_addr.sa_len);
929 bcopy(ifr->ifr_addr.sa_data, LLADDR(sdl),
930 ifr->ifr_addr.sa_len);
932 * If the interface is already up, we need
933 * to re-init it in order to reprogram its
936 if (ifp->if_flags & IFF_UP) {
937 ifp->if_flags &= ~IFF_UP;
938 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
939 ifp->if_flags |= IFF_UP;
940 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
944 oif_flags = ifp->if_flags;
945 if (so->so_proto == 0)
948 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
961 #if BYTE_ORDER != BIG_ENDIAN
962 if (ifr->ifr_addr.sa_family == 0 &&
963 ifr->ifr_addr.sa_len < 16) {
964 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
965 ifr->ifr_addr.sa_len = 16;
968 if (ifr->ifr_addr.sa_len == 0)
969 ifr->ifr_addr.sa_len = 16;
977 case OSIOCGIFDSTADDR:
978 cmd = SIOCGIFDSTADDR;
981 case OSIOCGIFBRDADDR:
982 cmd = SIOCGIFBRDADDR;
985 case OSIOCGIFNETMASK:
986 cmd = SIOCGIFNETMASK;
988 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
995 case OSIOCGIFDSTADDR:
996 case OSIOCGIFBRDADDR:
997 case OSIOCGIFNETMASK:
998 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1002 #endif /* COMPAT_43 */
1004 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1006 DELAY(100);/* XXX: temporal workaround for fxp issue*/
1007 if (ifp->if_flags & IFF_UP) {
1021 * Set/clear promiscuous mode on interface ifp based on the truth value
1022 * of pswitch. The calls are reference counted so that only the first
1023 * "on" request actually has an effect, as does the final "off" request.
1024 * Results are undefined if the "off" and "on" requests are not matched.
1027 ifpromisc(ifp, pswitch)
1036 * If the device is not configured up, we cannot put it in
1039 if ((ifp->if_flags & IFF_UP) == 0)
1041 if (ifp->if_pcount++ != 0)
1043 ifp->if_flags |= IFF_PROMISC;
1044 log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
1045 ifp->if_name, ifp->if_unit);
1047 if (--ifp->if_pcount > 0)
1049 ifp->if_flags &= ~IFF_PROMISC;
1050 log(LOG_INFO, "%s%d: promiscuous mode disabled\n",
1051 ifp->if_name, ifp->if_unit);
1053 ifr.ifr_flags = ifp->if_flags;
1054 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1061 * Return interface configuration
1062 * of system. List may be used
1063 * in later ioctl's (above) to get
1064 * other information.
1072 register struct ifconf *ifc = (struct ifconf *)data;
1073 register struct ifnet *ifp = ifnet.tqh_first;
1074 register struct ifaddr *ifa;
1075 struct ifreq ifr, *ifrp;
1076 int space = ifc->ifc_len, error = 0;
1078 ifrp = ifc->ifc_req;
1079 for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) {
1083 ifnlen = snprintf(workbuf, sizeof(workbuf),
1084 "%s%d", ifp->if_name, ifp->if_unit);
1085 if(ifnlen + 1 > sizeof ifr.ifr_name) {
1086 error = ENAMETOOLONG;
1089 strcpy(ifr.ifr_name, workbuf);
1093 ifa = ifp->if_addrhead.tqh_first;
1094 for ( ; space > sizeof (ifr) && ifa;
1095 ifa = ifa->ifa_link.tqe_next) {
1096 register struct sockaddr *sa = ifa->ifa_addr;
1097 if (curproc->p_prison && prison_if(curproc, sa))
1101 if (cmd == OSIOCGIFCONF) {
1102 struct osockaddr *osa =
1103 (struct osockaddr *)&ifr.ifr_addr;
1105 osa->sa_family = sa->sa_family;
1106 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1111 if (sa->sa_len <= sizeof(*sa)) {
1113 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1117 if (space < sizeof (ifr) + sa->sa_len -
1120 space -= sa->sa_len - sizeof(*sa);
1121 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1122 sizeof (ifr.ifr_name));
1124 error = copyout((caddr_t)sa,
1125 (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1126 ifrp = (struct ifreq *)
1127 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1131 space -= sizeof (ifr);
1136 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1137 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1141 space -= sizeof (ifr);
1145 ifc->ifc_len -= space;
1150 * Just like if_promisc(), but for all-multicast-reception mode.
1153 if_allmulti(ifp, onswitch)
1161 if (ifp->if_amcount++ == 0) {
1162 ifp->if_flags |= IFF_ALLMULTI;
1163 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1166 if (ifp->if_amcount > 1) {
1169 ifp->if_amcount = 0;
1170 ifp->if_flags &= ~IFF_ALLMULTI;
1171 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1182 * Add a multicast listenership to the interface in question.
1183 * The link layer provides a routine which converts
1186 if_addmulti(ifp, sa, retifma)
1187 struct ifnet *ifp; /* interface to manipulate */
1188 struct sockaddr *sa; /* address to add */
1189 struct ifmultiaddr **retifma;
1191 struct sockaddr *llsa, *dupsa;
1193 struct ifmultiaddr *ifma;
1196 * If the matching multicast address already exists
1197 * then don't add a new one, just add a reference
1199 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1200 ifma = ifma->ifma_link.le_next) {
1201 if (equal(sa, ifma->ifma_addr)) {
1202 ifma->ifma_refcount++;
1210 * Give the link layer a chance to accept/reject it, and also
1211 * find out which AF_LINK address this maps to, if it isn't one
1214 if (ifp->if_resolvemulti) {
1215 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1216 if (error) return error;
1221 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1222 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1223 bcopy(sa, dupsa, sa->sa_len);
1225 ifma->ifma_addr = dupsa;
1226 ifma->ifma_lladdr = llsa;
1227 ifma->ifma_ifp = ifp;
1228 ifma->ifma_refcount = 1;
1229 ifma->ifma_protospec = 0;
1230 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1233 * Some network interfaces can scan the address list at
1234 * interrupt time; lock them out.
1237 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1242 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1243 ifma = ifma->ifma_link.le_next) {
1244 if (equal(ifma->ifma_addr, llsa))
1248 ifma->ifma_refcount++;
1250 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1251 M_IFMADDR, M_WAITOK);
1252 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1253 M_IFMADDR, M_WAITOK);
1254 bcopy(llsa, dupsa, llsa->sa_len);
1255 ifma->ifma_addr = dupsa;
1256 ifma->ifma_ifp = ifp;
1257 ifma->ifma_refcount = 1;
1259 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1264 * We are certain we have added something, so call down to the
1265 * interface to let them know about it.
1268 ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1275 * Remove a reference to a multicast address on this interface. Yell
1276 * if the request does not match an existing membership.
1279 if_delmulti(ifp, sa)
1281 struct sockaddr *sa;
1283 struct ifmultiaddr *ifma;
1286 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1287 ifma = ifma->ifma_link.le_next)
1288 if (equal(sa, ifma->ifma_addr))
1293 if (ifma->ifma_refcount > 1) {
1294 ifma->ifma_refcount--;
1298 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1299 sa = ifma->ifma_lladdr;
1301 LIST_REMOVE(ifma, ifma_link);
1303 free(ifma->ifma_addr, M_IFMADDR);
1304 free(ifma, M_IFMADDR);
1309 * Now look for the link-layer address which corresponds to
1310 * this network address. It had been squirreled away in
1311 * ifma->ifma_lladdr for this purpose (so we don't have
1312 * to call ifp->if_resolvemulti() again), and we saved that
1313 * value in sa above. If some nasty deleted the
1314 * link-layer address out from underneath us, we can deal because
1315 * the address we stored was is not the same as the one which was
1316 * in the record for the link-layer address. (So we don't complain
1319 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1320 ifma = ifma->ifma_link.le_next)
1321 if (equal(sa, ifma->ifma_addr))
1326 if (ifma->ifma_refcount > 1) {
1327 ifma->ifma_refcount--;
1332 LIST_REMOVE(ifma, ifma_link);
1333 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1335 free(ifma->ifma_addr, M_IFMADDR);
1336 free(sa, M_IFMADDR);
1337 free(ifma, M_IFMADDR);
1342 struct ifmultiaddr *
1343 ifmaof_ifpforaddr(sa, ifp)
1344 struct sockaddr *sa;
1347 struct ifmultiaddr *ifma;
1349 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1350 ifma = ifma->ifma_link.le_next)
1351 if (equal(ifma->ifma_addr, sa))
1357 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1358 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");