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 <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;
119 if (ifp->if_snd.ifq_mtx.mtx_description == NULL) {
120 printf("%s%d XXX: driver didn't initialize queue mtx\n",
121 ifp->if_name, ifp->if_unit);
122 mtx_init(&ifp->if_snd.ifq_mtx, "unknown", MTX_DEF);
130 struct ifaddr **ifnet_addrs;
131 struct ifnet **ifindex2ifnet = NULL;
135 * Attach an interface to the
136 * list of "active" interfaces.
142 unsigned socksize, ifasize;
143 int namelen, masklen;
145 register struct sockaddr_dl *sdl;
146 register struct ifaddr *ifa;
147 static int if_indexlim = 8;
155 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
156 ifp->if_index = ++if_index;
159 * The old code would work if the interface passed a pre-existing
160 * chain of ifaddrs to this code. We don't trust our callers to
161 * properly initialize the tailq, however, so we no longer allow
162 * this unlikely case.
164 TAILQ_INIT(&ifp->if_addrhead);
165 TAILQ_INIT(&ifp->if_prefixhead);
166 LIST_INIT(&ifp->if_multiaddrs);
167 getmicrotime(&ifp->if_lastchange);
168 if (ifnet_addrs == 0 || if_index >= if_indexlim) {
169 unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
170 caddr_t q = malloc(n, M_IFADDR, M_WAITOK);
173 bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
174 free((caddr_t)ifnet_addrs, M_IFADDR);
176 ifnet_addrs = (struct ifaddr **)q;
178 /* grow ifindex2ifnet */
179 n = if_indexlim * sizeof(struct ifnet *);
180 q = malloc(n, M_IFADDR, M_WAITOK);
183 bcopy((caddr_t)ifindex2ifnet, q, n/2);
184 free((caddr_t)ifindex2ifnet, M_IFADDR);
186 ifindex2ifnet = (struct ifnet **)q;
189 ifindex2ifnet[if_index] = ifp;
191 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_name, MTX_DEF);
194 * create a Link Level name for this device
196 namelen = snprintf(workbuf, sizeof(workbuf),
197 "%s%d", ifp->if_name, ifp->if_unit);
198 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
199 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
200 socksize = masklen + ifp->if_addrlen;
201 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
202 if (socksize < sizeof(*sdl))
203 socksize = sizeof(*sdl);
204 socksize = ROUNDUP(socksize);
205 ifasize = sizeof(*ifa) + 2 * socksize;
206 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
208 bzero((caddr_t)ifa, ifasize);
209 sdl = (struct sockaddr_dl *)(ifa + 1);
210 sdl->sdl_len = socksize;
211 sdl->sdl_family = AF_LINK;
212 bcopy(workbuf, sdl->sdl_data, namelen);
213 sdl->sdl_nlen = namelen;
214 sdl->sdl_index = ifp->if_index;
215 sdl->sdl_type = ifp->if_type;
216 ifnet_addrs[if_index - 1] = ifa;
218 ifa->ifa_rtrequest = link_rtrequest;
219 ifa->ifa_addr = (struct sockaddr *)sdl;
220 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
221 ifa->ifa_netmask = (struct sockaddr *)sdl;
222 sdl->sdl_len = masklen;
224 sdl->sdl_data[--namelen] = 0xff;
225 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
230 * Detach an interface, removing it from the
231 * list of "active" interfaces.
238 struct radix_node_head *rnh;
243 * Remove routes and flush queues.
249 * Remove address from ifnet_addrs[] and maybe decrement if_index.
250 * Clean up all addresses.
252 ifnet_addrs[ifp->if_index - 1] = 0;
253 while (if_index > 0 && ifnet_addrs[if_index - 1] == 0)
256 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
257 ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
259 /* XXX: Ugly!! ad hoc just for INET */
260 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
261 struct ifaliasreq ifr;
263 bzero(&ifr, sizeof(ifr));
264 ifr.ifra_addr = *ifa->ifa_addr;
265 if (ifa->ifa_dstaddr)
266 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
267 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
273 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
274 in6_purgeaddr(ifa, ifp);
275 /* ifp_addrhead is already updated */
279 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
284 * Delete all remaining routes using this interface
285 * Unfortuneatly the only way to do this is to slog through
286 * the entire routing table looking for routes which point
287 * to this interface...oh well...
289 for (i = 1; i <= AF_MAX; i++) {
290 if ((rnh = rt_tables[i]) == NULL)
292 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
296 /* nuke all IPv6 kernel structs related to ifp */
300 TAILQ_REMOVE(&ifnet, ifp, if_link);
301 mtx_destroy(&ifp->if_snd.ifq_mtx);
306 * Delete Routes for a Network Interface
308 * Called for each routing entry via the rnh->rnh_walktree() call above
309 * to delete all route entries referencing a detaching network interface.
312 * rn pointer to node in the routing table
313 * arg argument passed to rnh->rnh_walktree() - detaching interface
317 * errno failed - reason indicated
322 struct radix_node *rn;
325 struct rtentry *rt = (struct rtentry *)rn;
326 struct ifnet *ifp = arg;
329 if (rt->rt_ifp == ifp) {
332 * Protect (sorta) against walktree recursion problems
335 if ((rt->rt_flags & RTF_UP) == 0)
338 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
339 rt_mask(rt), rt->rt_flags,
340 (struct rtentry **) NULL);
342 log(LOG_WARNING, "if_rtdel: error %d\n", err);
350 * Locate an interface based on a complete address.
355 register struct sockaddr *addr;
357 register struct ifnet *ifp;
358 register struct ifaddr *ifa;
360 #define equal(a1, a2) \
361 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
362 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
363 for (ifa = ifp->if_addrhead.tqh_first; ifa;
364 ifa = ifa->ifa_link.tqe_next) {
365 if (ifa->ifa_addr->sa_family != addr->sa_family)
367 if (equal(addr, ifa->ifa_addr))
369 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
370 /* IP6 doesn't have broadcast */
371 ifa->ifa_broadaddr->sa_len != 0 &&
372 equal(ifa->ifa_broadaddr, addr))
375 return ((struct ifaddr *)0);
378 * Locate the point to point interface with a given destination address.
382 ifa_ifwithdstaddr(addr)
383 register struct sockaddr *addr;
385 register struct ifnet *ifp;
386 register struct ifaddr *ifa;
388 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
389 if (ifp->if_flags & IFF_POINTOPOINT)
390 for (ifa = ifp->if_addrhead.tqh_first; ifa;
391 ifa = ifa->ifa_link.tqe_next) {
392 if (ifa->ifa_addr->sa_family != addr->sa_family)
394 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
397 return ((struct ifaddr *)0);
401 * Find an interface on a specific network. If many, choice
402 * is most specific found.
406 struct sockaddr *addr;
408 register struct ifnet *ifp;
409 register struct ifaddr *ifa;
410 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
411 u_int af = addr->sa_family;
412 char *addr_data = addr->sa_data, *cplim;
415 * AF_LINK addresses can be looked up directly by their index number,
416 * so do that if we can.
419 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
420 if (sdl->sdl_index && sdl->sdl_index <= if_index)
421 return (ifnet_addrs[sdl->sdl_index - 1]);
425 * Scan though each interface, looking for ones that have
426 * addresses in this address family.
428 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
429 for (ifa = ifp->if_addrhead.tqh_first; ifa;
430 ifa = ifa->ifa_link.tqe_next) {
431 register char *cp, *cp2, *cp3;
433 if (ifa->ifa_addr->sa_family != af)
436 #ifdef INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
437 addr->sa_family != AF_INET6 &&
439 ifp->if_flags & IFF_POINTOPOINT) {
441 * This is a bit broken as it doesn't
442 * take into account that the remote end may
443 * be a single node in the network we are
445 * The trouble is that we don't know the
446 * netmask for the remote end.
448 if (ifa->ifa_dstaddr != 0
449 && equal(addr, ifa->ifa_dstaddr))
453 * if we have a special address handler,
454 * then use it instead of the generic one.
456 if (ifa->ifa_claim_addr) {
457 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
465 * Scan all the bits in the ifa's address.
466 * If a bit dissagrees with what we are
467 * looking for, mask it with the netmask
468 * to see if it really matters.
471 if (ifa->ifa_netmask == 0)
474 cp2 = ifa->ifa_addr->sa_data;
475 cp3 = ifa->ifa_netmask->sa_data;
476 cplim = ifa->ifa_netmask->sa_len
477 + (char *)ifa->ifa_netmask;
479 if ((*cp++ ^ *cp2++) & *cp3++)
480 goto next; /* next address! */
482 * If the netmask of what we just found
483 * is more specific than what we had before
484 * (if we had one) then remember the new one
485 * before continuing to search
486 * for an even better one.
488 if (ifa_maybe == 0 ||
489 rn_refines((caddr_t)ifa->ifa_netmask,
490 (caddr_t)ifa_maybe->ifa_netmask))
499 * Find an interface address specific to an interface best matching
503 ifaof_ifpforaddr(addr, ifp)
504 struct sockaddr *addr;
505 register struct ifnet *ifp;
507 register struct ifaddr *ifa;
508 register char *cp, *cp2, *cp3;
509 register char *cplim;
510 struct ifaddr *ifa_maybe = 0;
511 u_int af = addr->sa_family;
515 for (ifa = ifp->if_addrhead.tqh_first; ifa;
516 ifa = ifa->ifa_link.tqe_next) {
517 if (ifa->ifa_addr->sa_family != af)
521 if (ifa->ifa_netmask == 0) {
522 if (equal(addr, ifa->ifa_addr) ||
523 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
527 if (ifp->if_flags & IFF_POINTOPOINT) {
528 if (equal(addr, ifa->ifa_dstaddr))
532 cp2 = ifa->ifa_addr->sa_data;
533 cp3 = ifa->ifa_netmask->sa_data;
534 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
535 for (; cp3 < cplim; cp3++)
536 if ((*cp++ ^ *cp2++) & *cp3)
545 #include <net/route.h>
548 * Default action when installing a route with a Link Level gateway.
549 * Lookup an appropriate real ifa to point to.
550 * This should be moved to /sys/net/link.c eventually.
553 link_rtrequest(cmd, rt, sa)
555 register struct rtentry *rt;
558 register struct ifaddr *ifa;
559 struct sockaddr *dst;
562 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
563 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
565 ifa = ifaof_ifpforaddr(dst, ifp);
570 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
571 ifa->ifa_rtrequest(cmd, rt, sa);
576 * Mark an interface down and notify protocols of
578 * NOTE: must be called at splnet or eqivalent.
581 if_unroute(ifp, flag, fam)
582 register struct ifnet *ifp;
585 register struct ifaddr *ifa;
587 ifp->if_flags &= ~flag;
588 getmicrotime(&ifp->if_lastchange);
589 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
590 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
591 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
592 if_qflush(&ifp->if_snd);
597 * Mark an interface up and notify protocols of
599 * NOTE: must be called at splnet or eqivalent.
602 if_route(ifp, flag, fam)
603 register struct ifnet *ifp;
606 register struct ifaddr *ifa;
608 ifp->if_flags |= flag;
609 getmicrotime(&ifp->if_lastchange);
610 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
611 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
612 pfctlinput(PRC_IFUP, ifa->ifa_addr);
620 * Mark an interface down and notify protocols of
622 * NOTE: must be called at splnet or eqivalent.
626 register struct ifnet *ifp;
629 if_unroute(ifp, IFF_UP, AF_UNSPEC);
633 * Mark an interface up and notify protocols of
635 * NOTE: must be called at splnet or eqivalent.
639 register struct ifnet *ifp;
642 if_route(ifp, IFF_UP, AF_UNSPEC);
646 * Flush an interface queue.
650 register struct ifqueue *ifq;
652 register struct mbuf *m, *n;
655 while ((m = n) != 0) {
665 * Handle interface watchdog timer routines. Called
666 * from softclock, we decrement timers (if set) and
667 * call the appropriate interface routine on expiration.
673 register struct ifnet *ifp;
676 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
677 if (ifp->if_timer == 0 || --ifp->if_timer)
679 if (ifp->if_watchdog)
680 (*ifp->if_watchdog)(ifp);
683 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
687 * Map interface name to
688 * interface structure pointer.
693 char namebuf[IFNAMSIZ + 1];
701 if (len < 2 || len > IFNAMSIZ)
705 if (c < '0' || c > '9')
706 return NULL; /* trailing garbage */
711 return NULL; /* no interface name */
712 unit += (c - '0') * m;
714 return NULL; /* number is unreasonable */
717 } while (c >= '0' && c <= '9');
719 bcopy(name, namebuf, len);
722 * Now search all the interfaces for this name/number
724 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
725 if (strcmp(ifp->if_name, namebuf))
727 if (unit == ifp->if_unit)
735 * Map interface name in a sockaddr_dl to
736 * interface structure pointer.
742 char ifname[IFNAMSIZ+1];
743 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
745 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
746 (sdl->sdl_nlen > IFNAMSIZ) )
750 * ifunit wants a null-terminated name. It may not be null-terminated
751 * in the sockaddr. We don't want to change the caller's sockaddr,
752 * and there might not be room to put the trailing null anyway, so we
753 * make a local copy that we know we can null terminate safely.
756 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
757 ifname[sdl->sdl_nlen] = '\0';
758 return ifunit(ifname);
766 ifioctl(so, cmd, data, p)
772 register struct ifnet *ifp;
773 register struct ifreq *ifr;
782 return (ifconf(cmd, data));
784 ifr = (struct ifreq *)data;
785 ifp = ifunit(ifr->ifr_name);
791 ifr->ifr_flags = ifp->if_flags;
795 ifr->ifr_metric = ifp->if_metric;
799 ifr->ifr_mtu = ifp->if_mtu;
803 ifr->ifr_phys = ifp->if_physical;
810 ifr->ifr_prevflags = ifp->if_flags;
811 if (ifp->if_flags & IFF_SMART) {
812 /* Smart drivers twiddle their own routes */
813 } else if (ifp->if_flags & IFF_UP &&
814 (ifr->ifr_flags & IFF_UP) == 0) {
818 } else if (ifr->ifr_flags & IFF_UP &&
819 (ifp->if_flags & IFF_UP) == 0) {
824 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
825 (ifr->ifr_flags &~ IFF_CANTCHANGE);
827 (void) (*ifp->if_ioctl)(ifp, cmd, data);
828 getmicrotime(&ifp->if_lastchange);
835 ifp->if_metric = ifr->ifr_metric;
836 getmicrotime(&ifp->if_lastchange);
845 error = (*ifp->if_ioctl)(ifp, cmd, data);
847 getmicrotime(&ifp->if_lastchange);
852 u_long oldmtu = ifp->if_mtu;
857 if (ifp->if_ioctl == NULL)
859 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
861 error = (*ifp->if_ioctl)(ifp, cmd, data);
863 getmicrotime(&ifp->if_lastchange);
867 * If the link MTU changed, do network layer specific procedure.
869 if (ifp->if_mtu != oldmtu) {
883 /* Don't allow group membership on non-multicast interfaces. */
884 if ((ifp->if_flags & IFF_MULTICAST) == 0)
887 /* Don't let users screw up protocols' entries. */
888 if (ifr->ifr_addr.sa_family != AF_LINK)
891 if (cmd == SIOCADDMULTI) {
892 struct ifmultiaddr *ifma;
893 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
895 error = if_delmulti(ifp, &ifr->ifr_addr);
898 getmicrotime(&ifp->if_lastchange);
904 case SIOCSIFPHYADDR_IN6:
911 if (ifp->if_ioctl == 0)
913 error = (*ifp->if_ioctl)(ifp, cmd, data);
915 getmicrotime(&ifp->if_lastchange);
919 ifs = (struct ifstat *)data;
920 ifs->ascii[0] = '\0';
924 if (ifp->if_ioctl == 0)
926 return ((*ifp->if_ioctl)(ifp, cmd, data));
932 return if_setlladdr(ifp,
933 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
936 oif_flags = ifp->if_flags;
937 if (so->so_proto == 0)
940 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
953 #if BYTE_ORDER != BIG_ENDIAN
954 if (ifr->ifr_addr.sa_family == 0 &&
955 ifr->ifr_addr.sa_len < 16) {
956 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
957 ifr->ifr_addr.sa_len = 16;
960 if (ifr->ifr_addr.sa_len == 0)
961 ifr->ifr_addr.sa_len = 16;
969 case OSIOCGIFDSTADDR:
970 cmd = SIOCGIFDSTADDR;
973 case OSIOCGIFBRDADDR:
974 cmd = SIOCGIFBRDADDR;
977 case OSIOCGIFNETMASK:
978 cmd = SIOCGIFNETMASK;
980 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
987 case OSIOCGIFDSTADDR:
988 case OSIOCGIFBRDADDR:
989 case OSIOCGIFNETMASK:
990 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
994 #endif /* COMPAT_43 */
996 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
998 DELAY(100);/* XXX: temporal workaround for fxp issue*/
999 if (ifp->if_flags & IFF_UP) {
1013 * Set/clear promiscuous mode on interface ifp based on the truth value
1014 * of pswitch. The calls are reference counted so that only the first
1015 * "on" request actually has an effect, as does the final "off" request.
1016 * Results are undefined if the "off" and "on" requests are not matched.
1019 ifpromisc(ifp, pswitch)
1028 * If the device is not configured up, we cannot put it in
1031 if ((ifp->if_flags & IFF_UP) == 0)
1033 if (ifp->if_pcount++ != 0)
1035 ifp->if_flags |= IFF_PROMISC;
1036 log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
1037 ifp->if_name, ifp->if_unit);
1039 if (--ifp->if_pcount > 0)
1041 ifp->if_flags &= ~IFF_PROMISC;
1042 log(LOG_INFO, "%s%d: promiscuous mode disabled\n",
1043 ifp->if_name, ifp->if_unit);
1045 ifr.ifr_flags = ifp->if_flags;
1046 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1053 * Return interface configuration
1054 * of system. List may be used
1055 * in later ioctl's (above) to get
1056 * other information.
1064 register struct ifconf *ifc = (struct ifconf *)data;
1065 register struct ifnet *ifp = ifnet.tqh_first;
1066 register struct ifaddr *ifa;
1067 struct ifreq ifr, *ifrp;
1068 int space = ifc->ifc_len, error = 0;
1070 ifrp = ifc->ifc_req;
1071 for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) {
1075 ifnlen = snprintf(workbuf, sizeof(workbuf),
1076 "%s%d", ifp->if_name, ifp->if_unit);
1077 if(ifnlen + 1 > sizeof ifr.ifr_name) {
1078 error = ENAMETOOLONG;
1081 strcpy(ifr.ifr_name, workbuf);
1085 ifa = ifp->if_addrhead.tqh_first;
1086 for ( ; space > sizeof (ifr) && ifa;
1087 ifa = ifa->ifa_link.tqe_next) {
1088 register struct sockaddr *sa = ifa->ifa_addr;
1089 if (curproc->p_prison && prison_if(curproc, sa))
1093 if (cmd == OSIOCGIFCONF) {
1094 struct osockaddr *osa =
1095 (struct osockaddr *)&ifr.ifr_addr;
1097 osa->sa_family = sa->sa_family;
1098 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1103 if (sa->sa_len <= sizeof(*sa)) {
1105 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1109 if (space < sizeof (ifr) + sa->sa_len -
1112 space -= sa->sa_len - sizeof(*sa);
1113 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1114 sizeof (ifr.ifr_name));
1116 error = copyout((caddr_t)sa,
1117 (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1118 ifrp = (struct ifreq *)
1119 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1123 space -= sizeof (ifr);
1128 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1129 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1133 space -= sizeof (ifr);
1137 ifc->ifc_len -= space;
1142 * Just like if_promisc(), but for all-multicast-reception mode.
1145 if_allmulti(ifp, onswitch)
1153 if (ifp->if_amcount++ == 0) {
1154 ifp->if_flags |= IFF_ALLMULTI;
1155 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1158 if (ifp->if_amcount > 1) {
1161 ifp->if_amcount = 0;
1162 ifp->if_flags &= ~IFF_ALLMULTI;
1163 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1174 * Add a multicast listenership to the interface in question.
1175 * The link layer provides a routine which converts
1178 if_addmulti(ifp, sa, retifma)
1179 struct ifnet *ifp; /* interface to manipulate */
1180 struct sockaddr *sa; /* address to add */
1181 struct ifmultiaddr **retifma;
1183 struct sockaddr *llsa, *dupsa;
1185 struct ifmultiaddr *ifma;
1188 * If the matching multicast address already exists
1189 * then don't add a new one, just add a reference
1191 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1192 ifma = ifma->ifma_link.le_next) {
1193 if (equal(sa, ifma->ifma_addr)) {
1194 ifma->ifma_refcount++;
1202 * Give the link layer a chance to accept/reject it, and also
1203 * find out which AF_LINK address this maps to, if it isn't one
1206 if (ifp->if_resolvemulti) {
1207 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1208 if (error) return error;
1213 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1214 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1215 bcopy(sa, dupsa, sa->sa_len);
1217 ifma->ifma_addr = dupsa;
1218 ifma->ifma_lladdr = llsa;
1219 ifma->ifma_ifp = ifp;
1220 ifma->ifma_refcount = 1;
1221 ifma->ifma_protospec = 0;
1222 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1225 * Some network interfaces can scan the address list at
1226 * interrupt time; lock them out.
1229 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1234 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1235 ifma = ifma->ifma_link.le_next) {
1236 if (equal(ifma->ifma_addr, llsa))
1240 ifma->ifma_refcount++;
1242 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1243 M_IFMADDR, M_WAITOK);
1244 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1245 M_IFMADDR, M_WAITOK);
1246 bcopy(llsa, dupsa, llsa->sa_len);
1247 ifma->ifma_addr = dupsa;
1248 ifma->ifma_ifp = ifp;
1249 ifma->ifma_refcount = 1;
1251 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1256 * We are certain we have added something, so call down to the
1257 * interface to let them know about it.
1260 ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1267 * Remove a reference to a multicast address on this interface. Yell
1268 * if the request does not match an existing membership.
1271 if_delmulti(ifp, sa)
1273 struct sockaddr *sa;
1275 struct ifmultiaddr *ifma;
1278 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1279 ifma = ifma->ifma_link.le_next)
1280 if (equal(sa, ifma->ifma_addr))
1285 if (ifma->ifma_refcount > 1) {
1286 ifma->ifma_refcount--;
1290 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1291 sa = ifma->ifma_lladdr;
1293 LIST_REMOVE(ifma, ifma_link);
1295 free(ifma->ifma_addr, M_IFMADDR);
1296 free(ifma, M_IFMADDR);
1301 * Now look for the link-layer address which corresponds to
1302 * this network address. It had been squirreled away in
1303 * ifma->ifma_lladdr for this purpose (so we don't have
1304 * to call ifp->if_resolvemulti() again), and we saved that
1305 * value in sa above. If some nasty deleted the
1306 * link-layer address out from underneath us, we can deal because
1307 * the address we stored was is not the same as the one which was
1308 * in the record for the link-layer address. (So we don't complain
1311 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1312 ifma = ifma->ifma_link.le_next)
1313 if (equal(sa, ifma->ifma_addr))
1318 if (ifma->ifma_refcount > 1) {
1319 ifma->ifma_refcount--;
1324 LIST_REMOVE(ifma, ifma_link);
1325 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1327 free(ifma->ifma_addr, M_IFMADDR);
1328 free(sa, M_IFMADDR);
1329 free(ifma, M_IFMADDR);
1335 * Set the link layer address on an interface.
1337 * At this time we only support certain types of interfaces,
1338 * and we don't allow the length of the address to change.
1341 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1343 struct sockaddr_dl *sdl;
1346 ifa = ifnet_addrs[ifp->if_index - 1];
1349 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1352 if (len != sdl->sdl_alen) /* don't allow length to change */
1354 switch (ifp->if_type) {
1355 case IFT_ETHER: /* these types use struct arpcom */
1359 case IFT_PROPVIRTUAL: /* XXX waiting for IFT_8021_VLAN */
1360 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1361 bcopy(lladdr, LLADDR(sdl), len);
1367 * If the interface is already up, we need
1368 * to re-init it in order to reprogram its
1371 if ((ifp->if_flags & IFF_UP) != 0) {
1372 ifp->if_flags &= ~IFF_UP;
1373 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
1374 ifp->if_flags |= IFF_UP;
1375 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
1380 struct ifmultiaddr *
1381 ifmaof_ifpforaddr(sa, ifp)
1382 struct sockaddr *sa;
1385 struct ifmultiaddr *ifma;
1387 for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1388 ifma = ifma->ifma_link.le_next)
1389 if (equal(ifma->ifma_addr, sa))
1395 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1396 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");