xref: /freebsd/sys/net/if.c (revision 0d0f9d1ed6c289d1487b5b4d99b024061fc2954e)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1980, 1986, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)if.c	8.3 (Berkeley) 1/4/94
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
375591b823SEivind Eklund #include "opt_compat.h"
38cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
390d0f9d1eSYoshinobu Inoue #include "opt_inet.h"
405591b823SEivind Eklund 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
424d1d4912SBruce Evans #include <sys/malloc.h>
43df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
44df8bae1dSRodney W. Grimes #include <sys/systm.h>
45df8bae1dSRodney W. Grimes #include <sys/proc.h>
46df8bae1dSRodney W. Grimes #include <sys/socket.h>
47df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
48df8bae1dSRodney W. Grimes #include <sys/protosw.h>
49df8bae1dSRodney W. Grimes #include <sys/kernel.h>
5051a53488SBruce Evans #include <sys/sockio.h>
51963e4c2aSGarrett Wollman #include <sys/syslog.h>
52602d513cSGarrett Wollman #include <sys/sysctl.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <net/if.h>
55df8bae1dSRodney W. Grimes #include <net/if_dl.h>
569448326fSPoul-Henning Kamp #include <net/radix.h>
575500d3beSWarner Losh #include <net/route.h>
58df8bae1dSRodney W. Grimes 
590d0f9d1eSYoshinobu Inoue #if defined(INET) || defined(INET6)
6082cd038dSYoshinobu Inoue /*XXX*/
6182cd038dSYoshinobu Inoue #include <netinet/in.h>
620d0f9d1eSYoshinobu Inoue #include <netinet/in_var.h>
6382cd038dSYoshinobu Inoue #endif
6482cd038dSYoshinobu Inoue 
652b14f991SJulian Elischer /*
662b14f991SJulian Elischer  * System initialization
672b14f991SJulian Elischer  */
682b14f991SJulian Elischer 
69ecbb00a2SDoug Rabson static int ifconf __P((u_long, caddr_t));
704590fd3aSDavid Greenman static void ifinit __P((void *));
713bda9f9bSPoul-Henning Kamp static void if_qflush __P((struct ifqueue *));
723bda9f9bSPoul-Henning Kamp static void if_slowtimo __P((void *));
733bda9f9bSPoul-Henning Kamp static void link_rtrequest __P((int, struct rtentry *, struct sockaddr *));
745500d3beSWarner Losh static int  if_rtdel __P((struct radix_node *, void *));
753bda9f9bSPoul-Henning Kamp 
762b14f991SJulian Elischer SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
772b14f991SJulian Elischer 
78a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
79a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
802b14f991SJulian Elischer 
81df8bae1dSRodney W. Grimes int	ifqmaxlen = IFQ_MAXLEN;
8229412182SGarrett Wollman struct	ifnethead ifnet;	/* depend on static init XXX */
83df8bae1dSRodney W. Grimes 
8482cd038dSYoshinobu Inoue #ifdef INET6
8582cd038dSYoshinobu Inoue /*
8682cd038dSYoshinobu Inoue  * XXX: declare here to avoid to include many inet6 related files..
8782cd038dSYoshinobu Inoue  * should be more generalized?
8882cd038dSYoshinobu Inoue  */
8982cd038dSYoshinobu Inoue extern void	nd6_setmtu __P((struct ifnet *));
9082cd038dSYoshinobu Inoue #endif
9182cd038dSYoshinobu Inoue 
92df8bae1dSRodney W. Grimes /*
93df8bae1dSRodney W. Grimes  * Network interface utility routines.
94df8bae1dSRodney W. Grimes  *
95df8bae1dSRodney W. Grimes  * Routines with ifa_ifwith* names take sockaddr *'s as
96df8bae1dSRodney W. Grimes  * parameters.
97df8bae1dSRodney W. Grimes  */
982b14f991SJulian Elischer /* ARGSUSED*/
99df8bae1dSRodney W. Grimes void
10027501cb6SBruce Evans ifinit(dummy)
10127501cb6SBruce Evans 	void *dummy;
102df8bae1dSRodney W. Grimes {
1038ba5bdaeSPeter Wemm 	struct ifnet *ifp;
1048ba5bdaeSPeter Wemm 	int s;
105df8bae1dSRodney W. Grimes 
1068ba5bdaeSPeter Wemm 	s = splimp();
10729412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
108e0ea20bcSPoul-Henning Kamp 		if (ifp->if_snd.ifq_maxlen == 0) {
109e0ea20bcSPoul-Henning Kamp 			printf("%s%d XXX: driver didn't set ifq_maxlen\n",
110e0ea20bcSPoul-Henning Kamp 			    ifp->if_name, ifp->if_unit);
111df8bae1dSRodney W. Grimes 			ifp->if_snd.ifq_maxlen = ifqmaxlen;
112e0ea20bcSPoul-Henning Kamp 		}
1138ba5bdaeSPeter Wemm 	splx(s);
114df8bae1dSRodney W. Grimes 	if_slowtimo(0);
115df8bae1dSRodney W. Grimes }
116df8bae1dSRodney W. Grimes 
117bbd17bf8SGarrett Wollman int if_index = 0;
118bbd17bf8SGarrett Wollman struct ifaddr **ifnet_addrs;
11982cd038dSYoshinobu Inoue struct ifnet **ifindex2ifnet = NULL;
1203bda9f9bSPoul-Henning Kamp 
121df8bae1dSRodney W. Grimes 
122df8bae1dSRodney W. Grimes /*
123df8bae1dSRodney W. Grimes  * Attach an interface to the
124df8bae1dSRodney W. Grimes  * list of "active" interfaces.
125df8bae1dSRodney W. Grimes  */
126df8bae1dSRodney W. Grimes void
127df8bae1dSRodney W. Grimes if_attach(ifp)
128df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
129df8bae1dSRodney W. Grimes {
130df8bae1dSRodney W. Grimes 	unsigned socksize, ifasize;
1311ce9bf88SPoul-Henning Kamp 	int namelen, masklen;
1321ce9bf88SPoul-Henning Kamp 	char workbuf[64];
133df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
134df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
135df8bae1dSRodney W. Grimes 	static int if_indexlim = 8;
13629412182SGarrett Wollman 	static int inited;
137f23b4c91SGarrett Wollman 
13829412182SGarrett Wollman 	if (!inited) {
13929412182SGarrett Wollman 		TAILQ_INIT(&ifnet);
14029412182SGarrett Wollman 		inited = 1;
14129412182SGarrett Wollman 	}
142df8bae1dSRodney W. Grimes 
14329412182SGarrett Wollman 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
144df8bae1dSRodney W. Grimes 	ifp->if_index = ++if_index;
14559562606SGarrett Wollman 	/*
14659562606SGarrett Wollman 	 * XXX -
14759562606SGarrett Wollman 	 * The old code would work if the interface passed a pre-existing
14859562606SGarrett Wollman 	 * chain of ifaddrs to this code.  We don't trust our callers to
14959562606SGarrett Wollman 	 * properly initialize the tailq, however, so we no longer allow
15059562606SGarrett Wollman 	 * this unlikely case.
15159562606SGarrett Wollman 	 */
15259562606SGarrett Wollman 	TAILQ_INIT(&ifp->if_addrhead);
15382cd038dSYoshinobu Inoue 	TAILQ_INIT(&ifp->if_prefixhead);
1541158dfb7SGarrett Wollman 	LIST_INIT(&ifp->if_multiaddrs);
15598b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
156df8bae1dSRodney W. Grimes 	if (ifnet_addrs == 0 || if_index >= if_indexlim) {
157df8bae1dSRodney W. Grimes 		unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
15882cd038dSYoshinobu Inoue 		caddr_t q = malloc(n, M_IFADDR, M_WAITOK);
15982cd038dSYoshinobu Inoue 		bzero(q, n);
160df8bae1dSRodney W. Grimes 		if (ifnet_addrs) {
161df8bae1dSRodney W. Grimes 			bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
162df8bae1dSRodney W. Grimes 			free((caddr_t)ifnet_addrs, M_IFADDR);
163df8bae1dSRodney W. Grimes 		}
16482cd038dSYoshinobu Inoue 		ifnet_addrs = (struct ifaddr **)q;
16582cd038dSYoshinobu Inoue 
16682cd038dSYoshinobu Inoue 		/* grow ifindex2ifnet */
16782cd038dSYoshinobu Inoue 		n = if_indexlim * sizeof(struct ifnet *);
16882cd038dSYoshinobu Inoue 		q = malloc(n, M_IFADDR, M_WAITOK);
16982cd038dSYoshinobu Inoue 		bzero(q, n);
17082cd038dSYoshinobu Inoue 		if (ifindex2ifnet) {
17182cd038dSYoshinobu Inoue 			bcopy((caddr_t)ifindex2ifnet, q, n/2);
17282cd038dSYoshinobu Inoue 			free((caddr_t)ifindex2ifnet, M_IFADDR);
173df8bae1dSRodney W. Grimes 		}
17482cd038dSYoshinobu Inoue 		ifindex2ifnet = (struct ifnet **)q;
17582cd038dSYoshinobu Inoue 	}
17682cd038dSYoshinobu Inoue 
17782cd038dSYoshinobu Inoue 	ifindex2ifnet[if_index] = ifp;
17882cd038dSYoshinobu Inoue 
179df8bae1dSRodney W. Grimes 	/*
180df8bae1dSRodney W. Grimes 	 * create a Link Level name for this device
181df8bae1dSRodney W. Grimes 	 */
1822127f260SArchie Cobbs 	namelen = snprintf(workbuf, sizeof(workbuf),
1832127f260SArchie Cobbs 	    "%s%d", ifp->if_name, ifp->if_unit);
184df8bae1dSRodney W. Grimes #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
1851ce9bf88SPoul-Henning Kamp 	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
186df8bae1dSRodney W. Grimes 	socksize = masklen + ifp->if_addrlen;
187df8bae1dSRodney W. Grimes #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
188df8bae1dSRodney W. Grimes 	if (socksize < sizeof(*sdl))
189df8bae1dSRodney W. Grimes 		socksize = sizeof(*sdl);
1908a261b8fSDoug Rabson 	socksize = ROUNDUP(socksize);
191df8bae1dSRodney W. Grimes 	ifasize = sizeof(*ifa) + 2 * socksize;
1929448326fSPoul-Henning Kamp 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
1939448326fSPoul-Henning Kamp 	if (ifa) {
194df8bae1dSRodney W. Grimes 		bzero((caddr_t)ifa, ifasize);
195df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(ifa + 1);
196df8bae1dSRodney W. Grimes 		sdl->sdl_len = socksize;
197df8bae1dSRodney W. Grimes 		sdl->sdl_family = AF_LINK;
1981ce9bf88SPoul-Henning Kamp 		bcopy(workbuf, sdl->sdl_data, namelen);
1991ce9bf88SPoul-Henning Kamp 		sdl->sdl_nlen = namelen;
200df8bae1dSRodney W. Grimes 		sdl->sdl_index = ifp->if_index;
201df8bae1dSRodney W. Grimes 		sdl->sdl_type = ifp->if_type;
202df8bae1dSRodney W. Grimes 		ifnet_addrs[if_index - 1] = ifa;
203df8bae1dSRodney W. Grimes 		ifa->ifa_ifp = ifp;
204df8bae1dSRodney W. Grimes 		ifa->ifa_rtrequest = link_rtrequest;
205df8bae1dSRodney W. Grimes 		ifa->ifa_addr = (struct sockaddr *)sdl;
206df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
207df8bae1dSRodney W. Grimes 		ifa->ifa_netmask = (struct sockaddr *)sdl;
208df8bae1dSRodney W. Grimes 		sdl->sdl_len = masklen;
209df8bae1dSRodney W. Grimes 		while (namelen != 0)
210df8bae1dSRodney W. Grimes 			sdl->sdl_data[--namelen] = 0xff;
21159562606SGarrett Wollman 		TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
212df8bae1dSRodney W. Grimes 	}
213df8bae1dSRodney W. Grimes }
2146182fdbdSPeter Wemm 
2156182fdbdSPeter Wemm /*
2166182fdbdSPeter Wemm  * Detach an interface, removing it from the
2176182fdbdSPeter Wemm  * list of "active" interfaces.
2186182fdbdSPeter Wemm  */
2196182fdbdSPeter Wemm void
2206182fdbdSPeter Wemm if_detach(ifp)
2216182fdbdSPeter Wemm 	struct ifnet *ifp;
2226182fdbdSPeter Wemm {
2236182fdbdSPeter Wemm 	struct ifaddr *ifa;
2245500d3beSWarner Losh 	struct radix_node_head	*rnh;
2255500d3beSWarner Losh 	int s;
2265500d3beSWarner Losh 	int i;
2276182fdbdSPeter Wemm 
2286182fdbdSPeter Wemm 	/*
2296182fdbdSPeter Wemm 	 * Remove routes and flush queues.
2306182fdbdSPeter Wemm 	 */
2315500d3beSWarner Losh 	s = splnet();
2326182fdbdSPeter Wemm 	if_down(ifp);
2336182fdbdSPeter Wemm 
2346182fdbdSPeter Wemm 	/*
2356182fdbdSPeter Wemm 	 * Remove address from ifnet_addrs[] and maybe decrement if_index.
2366182fdbdSPeter Wemm 	 * Clean up all addresses.
2376182fdbdSPeter Wemm 	 */
238aa6be122SWarner Losh 	ifnet_addrs[ifp->if_index - 1] = 0;
239aa6be122SWarner Losh 	while (if_index > 0 && ifnet_addrs[if_index - 1] == 0)
2406182fdbdSPeter Wemm 		if_index--;
2416182fdbdSPeter Wemm 
2426182fdbdSPeter Wemm 	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
2436182fdbdSPeter Wemm 	     ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
2440d0f9d1eSYoshinobu Inoue #ifdef INET
245aa6be122SWarner Losh 		/* XXX: Ugly!! ad hoc just for INET */
246aa6be122SWarner Losh 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
247aa6be122SWarner Losh 			struct ifaliasreq ifr;
248aa6be122SWarner Losh 
249aa6be122SWarner Losh 			bzero(&ifr, sizeof(ifr));
250aa6be122SWarner Losh 			ifr.ifra_addr = *ifa->ifa_addr;
251aa6be122SWarner Losh 			if (ifa->ifa_dstaddr)
252aa6be122SWarner Losh 				ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
253aa6be122SWarner Losh 			if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
254aa6be122SWarner Losh 			    NULL) == 0)
255aa6be122SWarner Losh 				continue;
256aa6be122SWarner Losh 		}
2570d0f9d1eSYoshinobu Inoue #endif /* INET */
2580d0f9d1eSYoshinobu Inoue #ifdef INET6
2590d0f9d1eSYoshinobu Inoue 		/* XXX: Ugly!! ad hoc just for INET6 */
2600d0f9d1eSYoshinobu Inoue 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
2610d0f9d1eSYoshinobu Inoue 			struct in6_aliasreq ifr;
2620d0f9d1eSYoshinobu Inoue 
2630d0f9d1eSYoshinobu Inoue 			bzero(&ifr, sizeof(ifr));
2640d0f9d1eSYoshinobu Inoue 			ifr.ifra_addr =
2650d0f9d1eSYoshinobu Inoue 				*((struct sockaddr_in6 *)ifa->ifa_addr);
2660d0f9d1eSYoshinobu Inoue 			if (ifa->ifa_dstaddr)
2670d0f9d1eSYoshinobu Inoue 				ifr.ifra_dstaddr =
2680d0f9d1eSYoshinobu Inoue 				    *((struct sockaddr_in6 *)ifa->ifa_dstaddr);
2690d0f9d1eSYoshinobu Inoue 			if (in6_control(NULL, SIOCDIFADDR_IN6, (caddr_t)&ifr,
2700d0f9d1eSYoshinobu Inoue 					ifp, NULL) == 0)
2710d0f9d1eSYoshinobu Inoue 				continue;
2720d0f9d1eSYoshinobu Inoue 		}
2730d0f9d1eSYoshinobu Inoue #endif /* INET6 */
2746182fdbdSPeter Wemm 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
2756182fdbdSPeter Wemm 		IFAFREE(ifa);
2766182fdbdSPeter Wemm 	}
2776182fdbdSPeter Wemm 
2785500d3beSWarner Losh 	/*
2795500d3beSWarner Losh 	 * Delete all remaining routes using this interface
2805500d3beSWarner Losh 	 * Unfortuneatly the only way to do this is to slog through
2815500d3beSWarner Losh 	 * the entire routing table looking for routes which point
2825500d3beSWarner Losh 	 * to this interface...oh well...
2835500d3beSWarner Losh 	 */
2845500d3beSWarner Losh 	for (i = 1; i <= AF_MAX; i++) {
2855500d3beSWarner Losh 		if ((rnh = rt_tables[i]) == NULL)
2865500d3beSWarner Losh 			continue;
2875500d3beSWarner Losh 		(void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
2885500d3beSWarner Losh 	}
2895500d3beSWarner Losh 
2906182fdbdSPeter Wemm 	TAILQ_REMOVE(&ifnet, ifp, if_link);
2915500d3beSWarner Losh 	splx(s);
2925500d3beSWarner Losh }
2935500d3beSWarner Losh 
2945500d3beSWarner Losh /*
2955500d3beSWarner Losh  * Delete Routes for a Network Interface
2965500d3beSWarner Losh  *
2975500d3beSWarner Losh  * Called for each routing entry via the rnh->rnh_walktree() call above
2985500d3beSWarner Losh  * to delete all route entries referencing a detaching network interface.
2995500d3beSWarner Losh  *
3005500d3beSWarner Losh  * Arguments:
3015500d3beSWarner Losh  *	rn	pointer to node in the routing table
3025500d3beSWarner Losh  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
3035500d3beSWarner Losh  *
3045500d3beSWarner Losh  * Returns:
3055500d3beSWarner Losh  *	0	successful
3065500d3beSWarner Losh  *	errno	failed - reason indicated
3075500d3beSWarner Losh  *
3085500d3beSWarner Losh  */
3095500d3beSWarner Losh static int
3105500d3beSWarner Losh if_rtdel(rn, arg)
3115500d3beSWarner Losh 	struct radix_node	*rn;
3125500d3beSWarner Losh 	void			*arg;
3135500d3beSWarner Losh {
3145500d3beSWarner Losh 	struct rtentry	*rt = (struct rtentry *)rn;
3155500d3beSWarner Losh 	struct ifnet	*ifp = arg;
3165500d3beSWarner Losh 	int		err;
3175500d3beSWarner Losh 
3185500d3beSWarner Losh 	if (rt->rt_ifp == ifp) {
3195500d3beSWarner Losh 
3205500d3beSWarner Losh 		/*
3215500d3beSWarner Losh 		 * Protect (sorta) against walktree recursion problems
3225500d3beSWarner Losh 		 * with cloned routes
3235500d3beSWarner Losh 		 */
3245500d3beSWarner Losh 		if ((rt->rt_flags & RTF_UP) == 0)
3255500d3beSWarner Losh 			return (0);
3265500d3beSWarner Losh 
3275500d3beSWarner Losh 		err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
3285500d3beSWarner Losh 				rt_mask(rt), rt->rt_flags,
3295500d3beSWarner Losh 				(struct rtentry **) NULL);
3305500d3beSWarner Losh 		if (err) {
3315500d3beSWarner Losh 			log(LOG_WARNING, "if_rtdel: error %d\n", err);
3325500d3beSWarner Losh 		}
3335500d3beSWarner Losh 	}
3345500d3beSWarner Losh 
3355500d3beSWarner Losh 	return (0);
3366182fdbdSPeter Wemm }
3376182fdbdSPeter Wemm 
338df8bae1dSRodney W. Grimes /*
339df8bae1dSRodney W. Grimes  * Locate an interface based on a complete address.
340df8bae1dSRodney W. Grimes  */
341df8bae1dSRodney W. Grimes /*ARGSUSED*/
342df8bae1dSRodney W. Grimes struct ifaddr *
343df8bae1dSRodney W. Grimes ifa_ifwithaddr(addr)
344df8bae1dSRodney W. Grimes 	register struct sockaddr *addr;
345df8bae1dSRodney W. Grimes {
346df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
347df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
348df8bae1dSRodney W. Grimes 
349df8bae1dSRodney W. Grimes #define	equal(a1, a2) \
350df8bae1dSRodney W. Grimes   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
35129412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
35259562606SGarrett Wollman 	    for (ifa = ifp->if_addrhead.tqh_first; ifa;
35359562606SGarrett Wollman 		 ifa = ifa->ifa_link.tqe_next) {
354df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != addr->sa_family)
355df8bae1dSRodney W. Grimes 			continue;
356df8bae1dSRodney W. Grimes 		if (equal(addr, ifa->ifa_addr))
357df8bae1dSRodney W. Grimes 			return (ifa);
358df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
35982cd038dSYoshinobu Inoue 		    /* IP6 doesn't have broadcast */
36082cd038dSYoshinobu Inoue 		    ifa->ifa_broadaddr->sa_len != 0 &&
361df8bae1dSRodney W. Grimes 		    equal(ifa->ifa_broadaddr, addr))
362df8bae1dSRodney W. Grimes 			return (ifa);
363df8bae1dSRodney W. Grimes 	}
364df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
365df8bae1dSRodney W. Grimes }
366df8bae1dSRodney W. Grimes /*
367df8bae1dSRodney W. Grimes  * Locate the point to point interface with a given destination address.
368df8bae1dSRodney W. Grimes  */
369df8bae1dSRodney W. Grimes /*ARGSUSED*/
370df8bae1dSRodney W. Grimes struct ifaddr *
371df8bae1dSRodney W. Grimes ifa_ifwithdstaddr(addr)
372df8bae1dSRodney W. Grimes 	register struct sockaddr *addr;
373df8bae1dSRodney W. Grimes {
374df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
375df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
376df8bae1dSRodney W. Grimes 
37729412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
378df8bae1dSRodney W. Grimes 	    if (ifp->if_flags & IFF_POINTOPOINT)
37959562606SGarrett Wollman 		for (ifa = ifp->if_addrhead.tqh_first; ifa;
38059562606SGarrett Wollman 		     ifa = ifa->ifa_link.tqe_next) {
381df8bae1dSRodney W. Grimes 			if (ifa->ifa_addr->sa_family != addr->sa_family)
382df8bae1dSRodney W. Grimes 				continue;
38355088a1cSDavid Greenman 			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
384df8bae1dSRodney W. Grimes 				return (ifa);
385df8bae1dSRodney W. Grimes 	}
386df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
387df8bae1dSRodney W. Grimes }
388df8bae1dSRodney W. Grimes 
389df8bae1dSRodney W. Grimes /*
390df8bae1dSRodney W. Grimes  * Find an interface on a specific network.  If many, choice
391df8bae1dSRodney W. Grimes  * is most specific found.
392df8bae1dSRodney W. Grimes  */
393df8bae1dSRodney W. Grimes struct ifaddr *
394df8bae1dSRodney W. Grimes ifa_ifwithnet(addr)
395df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
396df8bae1dSRodney W. Grimes {
397df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
398df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
399df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
400df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
401df8bae1dSRodney W. Grimes 	char *addr_data = addr->sa_data, *cplim;
402df8bae1dSRodney W. Grimes 
4037e2a6151SJulian Elischer 	/*
4047e2a6151SJulian Elischer 	 * AF_LINK addresses can be looked up directly by their index number,
4057e2a6151SJulian Elischer 	 * so do that if we can.
4067e2a6151SJulian Elischer 	 */
407df8bae1dSRodney W. Grimes 	if (af == AF_LINK) {
408df8bae1dSRodney W. Grimes 	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
409df8bae1dSRodney W. Grimes 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
410df8bae1dSRodney W. Grimes 		return (ifnet_addrs[sdl->sdl_index - 1]);
411df8bae1dSRodney W. Grimes 	}
4127e2a6151SJulian Elischer 
4137e2a6151SJulian Elischer 	/*
4147e2a6151SJulian Elischer 	 * Scan though each interface, looking for ones that have
4157e2a6151SJulian Elischer 	 * addresses in this address family.
4167e2a6151SJulian Elischer 	 */
41729412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
41859562606SGarrett Wollman 		for (ifa = ifp->if_addrhead.tqh_first; ifa;
41959562606SGarrett Wollman 		     ifa = ifa->ifa_link.tqe_next) {
420df8bae1dSRodney W. Grimes 			register char *cp, *cp2, *cp3;
421df8bae1dSRodney W. Grimes 
422523a02aaSDavid Greenman 			if (ifa->ifa_addr->sa_family != af)
423df8bae1dSRodney W. Grimes next:				continue;
42482cd038dSYoshinobu Inoue 			if (
42582cd038dSYoshinobu Inoue #ifdef INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
42682cd038dSYoshinobu Inoue 			    addr->sa_family != AF_INET6 &&
42782cd038dSYoshinobu Inoue #endif
42882cd038dSYoshinobu Inoue 			    ifp->if_flags & IFF_POINTOPOINT) {
4297e2a6151SJulian Elischer 				/*
4307e2a6151SJulian Elischer 				 * This is a bit broken as it doesn't
4317e2a6151SJulian Elischer 				 * take into account that the remote end may
4327e2a6151SJulian Elischer 				 * be a single node in the network we are
4337e2a6151SJulian Elischer 				 * looking for.
4347e2a6151SJulian Elischer 				 * The trouble is that we don't know the
4357e2a6151SJulian Elischer 				 * netmask for the remote end.
4367e2a6151SJulian Elischer 				 */
437fcd6781aSGarrett Wollman 				if (ifa->ifa_dstaddr != 0
438fcd6781aSGarrett Wollman 				    && equal(addr, ifa->ifa_dstaddr))
439b2af64fdSDavid Greenman  					return (ifa);
4403740e2adSDavid Greenman 			} else {
4417e2a6151SJulian Elischer 				/*
4427ed8f465SJulian Elischer 				 * if we have a special address handler,
4437ed8f465SJulian Elischer 				 * then use it instead of the generic one.
4447ed8f465SJulian Elischer 				 */
4457ed8f465SJulian Elischer 	          		if (ifa->ifa_claim_addr) {
4467ed8f465SJulian Elischer 					if ((*ifa->ifa_claim_addr)(ifa, addr)) {
4477ed8f465SJulian Elischer 						return (ifa);
4487ed8f465SJulian Elischer 					} else {
4497ed8f465SJulian Elischer 						continue;
4507ed8f465SJulian Elischer 					}
4517ed8f465SJulian Elischer 				}
4527ed8f465SJulian Elischer 
4537ed8f465SJulian Elischer 				/*
4547e2a6151SJulian Elischer 				 * Scan all the bits in the ifa's address.
4557e2a6151SJulian Elischer 				 * If a bit dissagrees with what we are
4567e2a6151SJulian Elischer 				 * looking for, mask it with the netmask
4577e2a6151SJulian Elischer 				 * to see if it really matters.
4587e2a6151SJulian Elischer 				 * (A byte at a time)
4597e2a6151SJulian Elischer 				 */
460523a02aaSDavid Greenman 				if (ifa->ifa_netmask == 0)
461523a02aaSDavid Greenman 					continue;
462df8bae1dSRodney W. Grimes 				cp = addr_data;
463df8bae1dSRodney W. Grimes 				cp2 = ifa->ifa_addr->sa_data;
464df8bae1dSRodney W. Grimes 				cp3 = ifa->ifa_netmask->sa_data;
4657e2a6151SJulian Elischer 				cplim = ifa->ifa_netmask->sa_len
4667e2a6151SJulian Elischer 					+ (char *)ifa->ifa_netmask;
467df8bae1dSRodney W. Grimes 				while (cp3 < cplim)
468df8bae1dSRodney W. Grimes 					if ((*cp++ ^ *cp2++) & *cp3++)
4697e2a6151SJulian Elischer 						goto next; /* next address! */
4707e2a6151SJulian Elischer 				/*
4717e2a6151SJulian Elischer 				 * If the netmask of what we just found
4727e2a6151SJulian Elischer 				 * is more specific than what we had before
4737e2a6151SJulian Elischer 				 * (if we had one) then remember the new one
4747e2a6151SJulian Elischer 				 * before continuing to search
4757e2a6151SJulian Elischer 				 * for an even better one.
4767e2a6151SJulian Elischer 				 */
477df8bae1dSRodney W. Grimes 				if (ifa_maybe == 0 ||
478df8bae1dSRodney W. Grimes 				    rn_refines((caddr_t)ifa->ifa_netmask,
479df8bae1dSRodney W. Grimes 				    (caddr_t)ifa_maybe->ifa_netmask))
480df8bae1dSRodney W. Grimes 					ifa_maybe = ifa;
481df8bae1dSRodney W. Grimes 			}
482b2af64fdSDavid Greenman 		}
483b2af64fdSDavid Greenman 	}
484df8bae1dSRodney W. Grimes 	return (ifa_maybe);
485df8bae1dSRodney W. Grimes }
486df8bae1dSRodney W. Grimes 
487df8bae1dSRodney W. Grimes /*
488df8bae1dSRodney W. Grimes  * Find an interface address specific to an interface best matching
489df8bae1dSRodney W. Grimes  * a given address.
490df8bae1dSRodney W. Grimes  */
491df8bae1dSRodney W. Grimes struct ifaddr *
492df8bae1dSRodney W. Grimes ifaof_ifpforaddr(addr, ifp)
493df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
494df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
495df8bae1dSRodney W. Grimes {
496df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
497df8bae1dSRodney W. Grimes 	register char *cp, *cp2, *cp3;
498df8bae1dSRodney W. Grimes 	register char *cplim;
499df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = 0;
500df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
501df8bae1dSRodney W. Grimes 
502df8bae1dSRodney W. Grimes 	if (af >= AF_MAX)
503df8bae1dSRodney W. Grimes 		return (0);
50459562606SGarrett Wollman 	for (ifa = ifp->if_addrhead.tqh_first; ifa;
50559562606SGarrett Wollman 	     ifa = ifa->ifa_link.tqe_next) {
506df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != af)
507df8bae1dSRodney W. Grimes 			continue;
508381dd1d2SJulian Elischer 		if (ifa_maybe == 0)
509df8bae1dSRodney W. Grimes 			ifa_maybe = ifa;
510df8bae1dSRodney W. Grimes 		if (ifa->ifa_netmask == 0) {
511df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_addr) ||
512df8bae1dSRodney W. Grimes 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
513df8bae1dSRodney W. Grimes 				return (ifa);
514df8bae1dSRodney W. Grimes 			continue;
515df8bae1dSRodney W. Grimes 		}
516b2af64fdSDavid Greenman 		if (ifp->if_flags & IFF_POINTOPOINT) {
517b2af64fdSDavid Greenman 			if (equal(addr, ifa->ifa_dstaddr))
518b2af64fdSDavid Greenman 				return (ifa);
5193740e2adSDavid Greenman 		} else {
520df8bae1dSRodney W. Grimes 			cp = addr->sa_data;
521df8bae1dSRodney W. Grimes 			cp2 = ifa->ifa_addr->sa_data;
522df8bae1dSRodney W. Grimes 			cp3 = ifa->ifa_netmask->sa_data;
523df8bae1dSRodney W. Grimes 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
524df8bae1dSRodney W. Grimes 			for (; cp3 < cplim; cp3++)
525df8bae1dSRodney W. Grimes 				if ((*cp++ ^ *cp2++) & *cp3)
526df8bae1dSRodney W. Grimes 					break;
527df8bae1dSRodney W. Grimes 			if (cp3 == cplim)
528df8bae1dSRodney W. Grimes 				return (ifa);
529df8bae1dSRodney W. Grimes 		}
530b2af64fdSDavid Greenman 	}
531df8bae1dSRodney W. Grimes 	return (ifa_maybe);
532df8bae1dSRodney W. Grimes }
533df8bae1dSRodney W. Grimes 
534df8bae1dSRodney W. Grimes #include <net/route.h>
535df8bae1dSRodney W. Grimes 
536df8bae1dSRodney W. Grimes /*
537df8bae1dSRodney W. Grimes  * Default action when installing a route with a Link Level gateway.
538df8bae1dSRodney W. Grimes  * Lookup an appropriate real ifa to point to.
539df8bae1dSRodney W. Grimes  * This should be moved to /sys/net/link.c eventually.
540df8bae1dSRodney W. Grimes  */
5413bda9f9bSPoul-Henning Kamp static void
542df8bae1dSRodney W. Grimes link_rtrequest(cmd, rt, sa)
543df8bae1dSRodney W. Grimes 	int cmd;
544df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
545df8bae1dSRodney W. Grimes 	struct sockaddr *sa;
546df8bae1dSRodney W. Grimes {
547df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
548df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
549df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
550df8bae1dSRodney W. Grimes 
551df8bae1dSRodney W. Grimes 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
552df8bae1dSRodney W. Grimes 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
553df8bae1dSRodney W. Grimes 		return;
5549448326fSPoul-Henning Kamp 	ifa = ifaof_ifpforaddr(dst, ifp);
5559448326fSPoul-Henning Kamp 	if (ifa) {
556df8bae1dSRodney W. Grimes 		IFAFREE(rt->rt_ifa);
557df8bae1dSRodney W. Grimes 		rt->rt_ifa = ifa;
558df8bae1dSRodney W. Grimes 		ifa->ifa_refcnt++;
559df8bae1dSRodney W. Grimes 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
560df8bae1dSRodney W. Grimes 			ifa->ifa_rtrequest(cmd, rt, sa);
561df8bae1dSRodney W. Grimes 	}
562df8bae1dSRodney W. Grimes }
563df8bae1dSRodney W. Grimes 
564df8bae1dSRodney W. Grimes /*
565df8bae1dSRodney W. Grimes  * Mark an interface down and notify protocols of
566df8bae1dSRodney W. Grimes  * the transition.
567df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
568df8bae1dSRodney W. Grimes  */
569df8bae1dSRodney W. Grimes void
570e8c2601dSPoul-Henning Kamp if_unroute(ifp, flag, fam)
571df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
572e8c2601dSPoul-Henning Kamp 	int flag, fam;
573df8bae1dSRodney W. Grimes {
574df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
575df8bae1dSRodney W. Grimes 
576e8c2601dSPoul-Henning Kamp 	ifp->if_flags &= ~flag;
57798b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
578e8c2601dSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
579e8c2601dSPoul-Henning Kamp 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
580df8bae1dSRodney W. Grimes 			pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
581df8bae1dSRodney W. Grimes 	if_qflush(&ifp->if_snd);
582df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
583df8bae1dSRodney W. Grimes }
584df8bae1dSRodney W. Grimes 
585df8bae1dSRodney W. Grimes /*
586df8bae1dSRodney W. Grimes  * Mark an interface up and notify protocols of
587df8bae1dSRodney W. Grimes  * the transition.
588df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
589df8bae1dSRodney W. Grimes  */
590df8bae1dSRodney W. Grimes void
591e8c2601dSPoul-Henning Kamp if_route(ifp, flag, fam)
592df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
593e8c2601dSPoul-Henning Kamp 	int flag, fam;
594df8bae1dSRodney W. Grimes {
595176395b2SGarrett Wollman 	register struct ifaddr *ifa;
596df8bae1dSRodney W. Grimes 
597e8c2601dSPoul-Henning Kamp 	ifp->if_flags |= flag;
59898b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
599e8c2601dSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
600e8c2601dSPoul-Henning Kamp 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
601df8bae1dSRodney W. Grimes 			pfctlinput(PRC_IFUP, ifa->ifa_addr);
602df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
60382cd038dSYoshinobu Inoue #ifdef INET6
60482cd038dSYoshinobu Inoue 	in6_if_up(ifp);
60582cd038dSYoshinobu Inoue #endif
606df8bae1dSRodney W. Grimes }
607df8bae1dSRodney W. Grimes 
608df8bae1dSRodney W. Grimes /*
609e8c2601dSPoul-Henning Kamp  * Mark an interface down and notify protocols of
610e8c2601dSPoul-Henning Kamp  * the transition.
611e8c2601dSPoul-Henning Kamp  * NOTE: must be called at splnet or eqivalent.
612e8c2601dSPoul-Henning Kamp  */
613e8c2601dSPoul-Henning Kamp void
614e8c2601dSPoul-Henning Kamp if_down(ifp)
615e8c2601dSPoul-Henning Kamp 	register struct ifnet *ifp;
616e8c2601dSPoul-Henning Kamp {
617e8c2601dSPoul-Henning Kamp 
618e8c2601dSPoul-Henning Kamp 	if_unroute(ifp, IFF_UP, AF_UNSPEC);
619e8c2601dSPoul-Henning Kamp }
620e8c2601dSPoul-Henning Kamp 
621e8c2601dSPoul-Henning Kamp /*
622e8c2601dSPoul-Henning Kamp  * Mark an interface up and notify protocols of
623e8c2601dSPoul-Henning Kamp  * the transition.
624e8c2601dSPoul-Henning Kamp  * NOTE: must be called at splnet or eqivalent.
625e8c2601dSPoul-Henning Kamp  */
626e8c2601dSPoul-Henning Kamp void
627e8c2601dSPoul-Henning Kamp if_up(ifp)
628e8c2601dSPoul-Henning Kamp 	register struct ifnet *ifp;
629e8c2601dSPoul-Henning Kamp {
630e8c2601dSPoul-Henning Kamp 
631e8c2601dSPoul-Henning Kamp 	if_route(ifp, IFF_UP, AF_UNSPEC);
632e8c2601dSPoul-Henning Kamp }
633e8c2601dSPoul-Henning Kamp 
634e8c2601dSPoul-Henning Kamp /*
635df8bae1dSRodney W. Grimes  * Flush an interface queue.
636df8bae1dSRodney W. Grimes  */
6373bda9f9bSPoul-Henning Kamp static void
638df8bae1dSRodney W. Grimes if_qflush(ifq)
639df8bae1dSRodney W. Grimes 	register struct ifqueue *ifq;
640df8bae1dSRodney W. Grimes {
641df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
642df8bae1dSRodney W. Grimes 
643df8bae1dSRodney W. Grimes 	n = ifq->ifq_head;
6449448326fSPoul-Henning Kamp 	while ((m = n) != 0) {
645df8bae1dSRodney W. Grimes 		n = m->m_act;
646df8bae1dSRodney W. Grimes 		m_freem(m);
647df8bae1dSRodney W. Grimes 	}
648df8bae1dSRodney W. Grimes 	ifq->ifq_head = 0;
649df8bae1dSRodney W. Grimes 	ifq->ifq_tail = 0;
650df8bae1dSRodney W. Grimes 	ifq->ifq_len = 0;
651df8bae1dSRodney W. Grimes }
652df8bae1dSRodney W. Grimes 
653df8bae1dSRodney W. Grimes /*
654df8bae1dSRodney W. Grimes  * Handle interface watchdog timer routines.  Called
655df8bae1dSRodney W. Grimes  * from softclock, we decrement timers (if set) and
656df8bae1dSRodney W. Grimes  * call the appropriate interface routine on expiration.
657df8bae1dSRodney W. Grimes  */
6583bda9f9bSPoul-Henning Kamp static void
659df8bae1dSRodney W. Grimes if_slowtimo(arg)
660df8bae1dSRodney W. Grimes 	void *arg;
661df8bae1dSRodney W. Grimes {
662df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
663df8bae1dSRodney W. Grimes 	int s = splimp();
664df8bae1dSRodney W. Grimes 
66529412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
666df8bae1dSRodney W. Grimes 		if (ifp->if_timer == 0 || --ifp->if_timer)
667df8bae1dSRodney W. Grimes 			continue;
668df8bae1dSRodney W. Grimes 		if (ifp->if_watchdog)
6694a5f1499SDavid Greenman 			(*ifp->if_watchdog)(ifp);
670df8bae1dSRodney W. Grimes 	}
671df8bae1dSRodney W. Grimes 	splx(s);
672df8bae1dSRodney W. Grimes 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
673df8bae1dSRodney W. Grimes }
674df8bae1dSRodney W. Grimes 
675df8bae1dSRodney W. Grimes /*
676df8bae1dSRodney W. Grimes  * Map interface name to
677df8bae1dSRodney W. Grimes  * interface structure pointer.
678df8bae1dSRodney W. Grimes  */
679df8bae1dSRodney W. Grimes struct ifnet *
6808b7805e4SBoris Popov ifunit(char *name)
681df8bae1dSRodney W. Grimes {
68201f0fef3SJulian Elischer 	char namebuf[IFNAMSIZ + 1];
6838b7805e4SBoris Popov 	char *cp;
6848b7805e4SBoris Popov 	struct ifnet *ifp;
685df8bae1dSRodney W. Grimes 	int unit;
6868b7805e4SBoris Popov 	unsigned len, m;
6878b7805e4SBoris Popov 	char c;
688df8bae1dSRodney W. Grimes 
6898b7805e4SBoris Popov 	len = strlen(name);
6908b7805e4SBoris Popov 	if (len < 2 || len > IFNAMSIZ)
6918b7805e4SBoris Popov 		return NULL;
6928b7805e4SBoris Popov 	cp = name + len - 1;
6938b7805e4SBoris Popov 	c = *cp;
6948b7805e4SBoris Popov 	if (c < '0' || c > '9')
6958b7805e4SBoris Popov 		return NULL;		/* trailing garbage */
6968b7805e4SBoris Popov 	unit = 0;
6978b7805e4SBoris Popov 	m = 1;
6988b7805e4SBoris Popov 	do {
6998b7805e4SBoris Popov 		if (cp == name)
7008b7805e4SBoris Popov 			return NULL;	/* no interface name */
7018b7805e4SBoris Popov 		unit += (c - '0') * m;
7028b7805e4SBoris Popov 		if (unit > 1000000)
7038b7805e4SBoris Popov 			return NULL;	/* number is unreasonable */
7048b7805e4SBoris Popov 		m *= 10;
7058b7805e4SBoris Popov 		c = *--cp;
7068b7805e4SBoris Popov 	} while (c >= '0' && c <= '9');
707df8bae1dSRodney W. Grimes 	len = cp - name + 1;
7088b7805e4SBoris Popov 	bcopy(name, namebuf, len);
7098b7805e4SBoris Popov 	namebuf[len] = '\0';
71001f0fef3SJulian Elischer 	/*
71101f0fef3SJulian Elischer 	 * Now search all the interfaces for this name/number
71201f0fef3SJulian Elischer 	 */
71329412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
7148b7805e4SBoris Popov 		if (strcmp(ifp->if_name, namebuf))
715df8bae1dSRodney W. Grimes 			continue;
716df8bae1dSRodney W. Grimes 		if (unit == ifp->if_unit)
717df8bae1dSRodney W. Grimes 			break;
718df8bae1dSRodney W. Grimes 	}
719df8bae1dSRodney W. Grimes 	return (ifp);
720df8bae1dSRodney W. Grimes }
721df8bae1dSRodney W. Grimes 
72282cd038dSYoshinobu Inoue 
72382cd038dSYoshinobu Inoue /*
72482cd038dSYoshinobu Inoue  * Map interface name in a sockaddr_dl to
72582cd038dSYoshinobu Inoue  * interface structure pointer.
72682cd038dSYoshinobu Inoue  */
72782cd038dSYoshinobu Inoue struct ifnet *
72882cd038dSYoshinobu Inoue if_withname(sa)
72982cd038dSYoshinobu Inoue 	struct sockaddr *sa;
73082cd038dSYoshinobu Inoue {
73182cd038dSYoshinobu Inoue 	char ifname[IFNAMSIZ+1];
73282cd038dSYoshinobu Inoue 	struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
73382cd038dSYoshinobu Inoue 
73482cd038dSYoshinobu Inoue 	if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
73582cd038dSYoshinobu Inoue 	     (sdl->sdl_nlen > IFNAMSIZ) )
73682cd038dSYoshinobu Inoue 		return NULL;
73782cd038dSYoshinobu Inoue 
73882cd038dSYoshinobu Inoue 	/*
73982cd038dSYoshinobu Inoue 	 * ifunit wants a null-terminated name.  It may not be null-terminated
74082cd038dSYoshinobu Inoue 	 * in the sockaddr.  We don't want to change the caller's sockaddr,
74182cd038dSYoshinobu Inoue 	 * and there might not be room to put the trailing null anyway, so we
74282cd038dSYoshinobu Inoue 	 * make a local copy that we know we can null terminate safely.
74382cd038dSYoshinobu Inoue 	 */
74482cd038dSYoshinobu Inoue 
74582cd038dSYoshinobu Inoue 	bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
74682cd038dSYoshinobu Inoue 	ifname[sdl->sdl_nlen] = '\0';
74782cd038dSYoshinobu Inoue 	return ifunit(ifname);
74882cd038dSYoshinobu Inoue }
74982cd038dSYoshinobu Inoue 
75082cd038dSYoshinobu Inoue 
751df8bae1dSRodney W. Grimes /*
752df8bae1dSRodney W. Grimes  * Interface ioctls.
753df8bae1dSRodney W. Grimes  */
754df8bae1dSRodney W. Grimes int
755df8bae1dSRodney W. Grimes ifioctl(so, cmd, data, p)
756df8bae1dSRodney W. Grimes 	struct socket *so;
757ecbb00a2SDoug Rabson 	u_long cmd;
758df8bae1dSRodney W. Grimes 	caddr_t data;
759df8bae1dSRodney W. Grimes 	struct proc *p;
760df8bae1dSRodney W. Grimes {
761df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
762df8bae1dSRodney W. Grimes 	register struct ifreq *ifr;
763413dd0baSPoul-Henning Kamp 	struct ifstat *ifs;
764df8bae1dSRodney W. Grimes 	int error;
76582cd038dSYoshinobu Inoue 	short oif_flags;
766df8bae1dSRodney W. Grimes 
767df8bae1dSRodney W. Grimes 	switch (cmd) {
768df8bae1dSRodney W. Grimes 
769df8bae1dSRodney W. Grimes 	case SIOCGIFCONF:
770df8bae1dSRodney W. Grimes 	case OSIOCGIFCONF:
771df8bae1dSRodney W. Grimes 		return (ifconf(cmd, data));
772df8bae1dSRodney W. Grimes 	}
773df8bae1dSRodney W. Grimes 	ifr = (struct ifreq *)data;
774df8bae1dSRodney W. Grimes 	ifp = ifunit(ifr->ifr_name);
775df8bae1dSRodney W. Grimes 	if (ifp == 0)
776df8bae1dSRodney W. Grimes 		return (ENXIO);
777df8bae1dSRodney W. Grimes 	switch (cmd) {
778df8bae1dSRodney W. Grimes 
779df8bae1dSRodney W. Grimes 	case SIOCGIFFLAGS:
780df8bae1dSRodney W. Grimes 		ifr->ifr_flags = ifp->if_flags;
781df8bae1dSRodney W. Grimes 		break;
782df8bae1dSRodney W. Grimes 
783df8bae1dSRodney W. Grimes 	case SIOCGIFMETRIC:
784df8bae1dSRodney W. Grimes 		ifr->ifr_metric = ifp->if_metric;
785df8bae1dSRodney W. Grimes 		break;
786df8bae1dSRodney W. Grimes 
787a7028af7SDavid Greenman 	case SIOCGIFMTU:
788a7028af7SDavid Greenman 		ifr->ifr_mtu = ifp->if_mtu;
789a7028af7SDavid Greenman 		break;
790a7028af7SDavid Greenman 
791074c4a4eSGarrett Wollman 	case SIOCGIFPHYS:
792074c4a4eSGarrett Wollman 		ifr->ifr_phys = ifp->if_physical;
793074c4a4eSGarrett Wollman 		break;
794074c4a4eSGarrett Wollman 
795df8bae1dSRodney W. Grimes 	case SIOCSIFFLAGS:
796f711d546SPoul-Henning Kamp 		error = suser(p);
7979448326fSPoul-Henning Kamp 		if (error)
798df8bae1dSRodney W. Grimes 			return (error);
7994add131eSPoul-Henning Kamp 		ifr->ifr_prevflags = ifp->if_flags;
800cf4b9371SPoul-Henning Kamp 		if (ifp->if_flags & IFF_SMART) {
801cf4b9371SPoul-Henning Kamp 			/* Smart drivers twiddle their own routes */
8022f55ead7SPoul-Henning Kamp 		} else if (ifp->if_flags & IFF_UP &&
803cf4b9371SPoul-Henning Kamp 		    (ifr->ifr_flags & IFF_UP) == 0) {
804df8bae1dSRodney W. Grimes 			int s = splimp();
805df8bae1dSRodney W. Grimes 			if_down(ifp);
806df8bae1dSRodney W. Grimes 			splx(s);
807cf4b9371SPoul-Henning Kamp 		} else if (ifr->ifr_flags & IFF_UP &&
808cf4b9371SPoul-Henning Kamp 		    (ifp->if_flags & IFF_UP) == 0) {
809df8bae1dSRodney W. Grimes 			int s = splimp();
810df8bae1dSRodney W. Grimes 			if_up(ifp);
811df8bae1dSRodney W. Grimes 			splx(s);
812df8bae1dSRodney W. Grimes 		}
813df8bae1dSRodney W. Grimes 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
814df8bae1dSRodney W. Grimes 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
815df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl)
816df8bae1dSRodney W. Grimes 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
81798b9590eSPoul-Henning Kamp 		getmicrotime(&ifp->if_lastchange);
818df8bae1dSRodney W. Grimes 		break;
819df8bae1dSRodney W. Grimes 
820df8bae1dSRodney W. Grimes 	case SIOCSIFMETRIC:
821f711d546SPoul-Henning Kamp 		error = suser(p);
8229448326fSPoul-Henning Kamp 		if (error)
823df8bae1dSRodney W. Grimes 			return (error);
824df8bae1dSRodney W. Grimes 		ifp->if_metric = ifr->ifr_metric;
82598b9590eSPoul-Henning Kamp 		getmicrotime(&ifp->if_lastchange);
826df8bae1dSRodney W. Grimes 		break;
827df8bae1dSRodney W. Grimes 
828074c4a4eSGarrett Wollman 	case SIOCSIFPHYS:
829f711d546SPoul-Henning Kamp 		error = suser(p);
830e39a0280SGary Palmer 		if (error)
831e39a0280SGary Palmer 			return error;
832e39a0280SGary Palmer 		if (!ifp->if_ioctl)
833e39a0280SGary Palmer 		        return EOPNOTSUPP;
834e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
835e39a0280SGary Palmer 		if (error == 0)
83698b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
837e39a0280SGary Palmer 		return(error);
838074c4a4eSGarrett Wollman 
839a7028af7SDavid Greenman 	case SIOCSIFMTU:
84082cd038dSYoshinobu Inoue 	{
84182cd038dSYoshinobu Inoue 		u_long oldmtu = ifp->if_mtu;
84282cd038dSYoshinobu Inoue 
843f711d546SPoul-Henning Kamp 		error = suser(p);
8449448326fSPoul-Henning Kamp 		if (error)
845a7028af7SDavid Greenman 			return (error);
846a7028af7SDavid Greenman 		if (ifp->if_ioctl == NULL)
847a7028af7SDavid Greenman 			return (EOPNOTSUPP);
848aab3beeeSBrian Somers 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
84975ee03cbSDavid Greenman 			return (EINVAL);
850e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
851e39a0280SGary Palmer 		if (error == 0)
85298b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
85382cd038dSYoshinobu Inoue 		/*
85482cd038dSYoshinobu Inoue 		 * If the link MTU changed, do network layer specific procedure.
85582cd038dSYoshinobu Inoue 		 */
85682cd038dSYoshinobu Inoue 		if (ifp->if_mtu != oldmtu) {
85782cd038dSYoshinobu Inoue #ifdef INET6
85882cd038dSYoshinobu Inoue 			nd6_setmtu(ifp);
85982cd038dSYoshinobu Inoue #endif
86082cd038dSYoshinobu Inoue 		}
861e39a0280SGary Palmer 		return (error);
86282cd038dSYoshinobu Inoue 	}
863a7028af7SDavid Greenman 
864df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
865df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
866f711d546SPoul-Henning Kamp 		error = suser(p);
8679448326fSPoul-Henning Kamp 		if (error)
868df8bae1dSRodney W. Grimes 			return (error);
869477180fbSGarrett Wollman 
870477180fbSGarrett Wollman 		/* Don't allow group membership on non-multicast interfaces. */
871477180fbSGarrett Wollman 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
872477180fbSGarrett Wollman 			return EOPNOTSUPP;
873477180fbSGarrett Wollman 
874477180fbSGarrett Wollman 		/* Don't let users screw up protocols' entries. */
875477180fbSGarrett Wollman 		if (ifr->ifr_addr.sa_family != AF_LINK)
876477180fbSGarrett Wollman 			return EINVAL;
877477180fbSGarrett Wollman 
878477180fbSGarrett Wollman 		if (cmd == SIOCADDMULTI) {
879477180fbSGarrett Wollman 			struct ifmultiaddr *ifma;
880477180fbSGarrett Wollman 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
881477180fbSGarrett Wollman 		} else {
882477180fbSGarrett Wollman 			error = if_delmulti(ifp, &ifr->ifr_addr);
883477180fbSGarrett Wollman 		}
884e39a0280SGary Palmer 		if (error == 0)
88598b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
886477180fbSGarrett Wollman 		return error;
887df8bae1dSRodney W. Grimes 
888a912e453SPeter Wemm         case SIOCSIFMEDIA:
889d7189ec6SJoerg Wunsch 	case SIOCSIFGENERIC:
890f711d546SPoul-Henning Kamp 		error = suser(p);
891a912e453SPeter Wemm 		if (error)
892a912e453SPeter Wemm 			return (error);
893a912e453SPeter Wemm 		if (ifp->if_ioctl == 0)
894a912e453SPeter Wemm 			return (EOPNOTSUPP);
895a912e453SPeter Wemm 		error = (*ifp->if_ioctl)(ifp, cmd, data);
896a912e453SPeter Wemm 		if (error == 0)
89798b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
898a912e453SPeter Wemm 		return error;
899a912e453SPeter Wemm 
900413dd0baSPoul-Henning Kamp 	case SIOCGIFSTATUS:
901413dd0baSPoul-Henning Kamp 		ifs = (struct ifstat *)data;
902413dd0baSPoul-Henning Kamp 		ifs->ascii[0] = '\0';
903413dd0baSPoul-Henning Kamp 
904a912e453SPeter Wemm 	case SIOCGIFMEDIA:
905d7189ec6SJoerg Wunsch 	case SIOCGIFGENERIC:
906a912e453SPeter Wemm 		if (ifp->if_ioctl == 0)
907a912e453SPeter Wemm 			return (EOPNOTSUPP);
908a912e453SPeter Wemm 		return ((*ifp->if_ioctl)(ifp, cmd, data));
909a912e453SPeter Wemm 
910df8bae1dSRodney W. Grimes 	default:
91182cd038dSYoshinobu Inoue 		oif_flags = ifp->if_flags;
912df8bae1dSRodney W. Grimes 		if (so->so_proto == 0)
913df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
914df8bae1dSRodney W. Grimes #ifndef COMPAT_43
91582cd038dSYoshinobu Inoue 		error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
9162c37256eSGarrett Wollman 								 data,
917bc6d9b80SJoerg Wunsch 								 ifp, p));
918df8bae1dSRodney W. Grimes #else
919df8bae1dSRodney W. Grimes 	    {
920df8bae1dSRodney W. Grimes 		int ocmd = cmd;
921df8bae1dSRodney W. Grimes 
922df8bae1dSRodney W. Grimes 		switch (cmd) {
923df8bae1dSRodney W. Grimes 
924df8bae1dSRodney W. Grimes 		case SIOCSIFDSTADDR:
925df8bae1dSRodney W. Grimes 		case SIOCSIFADDR:
926df8bae1dSRodney W. Grimes 		case SIOCSIFBRDADDR:
927df8bae1dSRodney W. Grimes 		case SIOCSIFNETMASK:
928df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN
929df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_family == 0 &&
930df8bae1dSRodney W. Grimes 			    ifr->ifr_addr.sa_len < 16) {
931df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
932df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
933df8bae1dSRodney W. Grimes 			}
934df8bae1dSRodney W. Grimes #else
935df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_len == 0)
936df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
937df8bae1dSRodney W. Grimes #endif
938df8bae1dSRodney W. Grimes 			break;
939df8bae1dSRodney W. Grimes 
940df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
941df8bae1dSRodney W. Grimes 			cmd = SIOCGIFADDR;
942df8bae1dSRodney W. Grimes 			break;
943df8bae1dSRodney W. Grimes 
944df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
945df8bae1dSRodney W. Grimes 			cmd = SIOCGIFDSTADDR;
946df8bae1dSRodney W. Grimes 			break;
947df8bae1dSRodney W. Grimes 
948df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
949df8bae1dSRodney W. Grimes 			cmd = SIOCGIFBRDADDR;
950df8bae1dSRodney W. Grimes 			break;
951df8bae1dSRodney W. Grimes 
952df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
953df8bae1dSRodney W. Grimes 			cmd = SIOCGIFNETMASK;
954df8bae1dSRodney W. Grimes 		}
9552c37256eSGarrett Wollman 		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
9562c37256eSGarrett Wollman 								   cmd,
9572c37256eSGarrett Wollman 								   data,
958a29f300eSGarrett Wollman 								   ifp, p));
959df8bae1dSRodney W. Grimes 		switch (ocmd) {
960df8bae1dSRodney W. Grimes 
961df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
962df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
963df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
964df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
965df8bae1dSRodney W. Grimes 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
96682cd038dSYoshinobu Inoue 
96782cd038dSYoshinobu Inoue 		}
96882cd038dSYoshinobu Inoue 	    }
96982cd038dSYoshinobu Inoue #endif /* COMPAT_43 */
97082cd038dSYoshinobu Inoue 
97182cd038dSYoshinobu Inoue 		if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
97282cd038dSYoshinobu Inoue #ifdef INET6
97382cd038dSYoshinobu Inoue 			if (ifp->if_flags & IFF_UP) {
97482cd038dSYoshinobu Inoue 				int s = splimp();
97582cd038dSYoshinobu Inoue 				in6_if_up(ifp);
97682cd038dSYoshinobu Inoue 				splx(s);
97782cd038dSYoshinobu Inoue 			}
97882cd038dSYoshinobu Inoue #endif
979df8bae1dSRodney W. Grimes 		}
980df8bae1dSRodney W. Grimes 		return (error);
981df8bae1dSRodney W. Grimes 
982df8bae1dSRodney W. Grimes 	}
983df8bae1dSRodney W. Grimes 	return (0);
984df8bae1dSRodney W. Grimes }
985df8bae1dSRodney W. Grimes 
986df8bae1dSRodney W. Grimes /*
987963e4c2aSGarrett Wollman  * Set/clear promiscuous mode on interface ifp based on the truth value
988963e4c2aSGarrett Wollman  * of pswitch.  The calls are reference counted so that only the first
989963e4c2aSGarrett Wollman  * "on" request actually has an effect, as does the final "off" request.
990963e4c2aSGarrett Wollman  * Results are undefined if the "off" and "on" requests are not matched.
991963e4c2aSGarrett Wollman  */
992963e4c2aSGarrett Wollman int
993963e4c2aSGarrett Wollman ifpromisc(ifp, pswitch)
994963e4c2aSGarrett Wollman 	struct ifnet *ifp;
995963e4c2aSGarrett Wollman 	int pswitch;
996963e4c2aSGarrett Wollman {
997963e4c2aSGarrett Wollman 	struct ifreq ifr;
9984a26224cSGarrett Wollman 	int error;
999963e4c2aSGarrett Wollman 
1000963e4c2aSGarrett Wollman 	if (pswitch) {
1001963e4c2aSGarrett Wollman 		/*
1002963e4c2aSGarrett Wollman 		 * If the device is not configured up, we cannot put it in
1003963e4c2aSGarrett Wollman 		 * promiscuous mode.
1004963e4c2aSGarrett Wollman 		 */
1005963e4c2aSGarrett Wollman 		if ((ifp->if_flags & IFF_UP) == 0)
1006963e4c2aSGarrett Wollman 			return (ENETDOWN);
1007963e4c2aSGarrett Wollman 		if (ifp->if_pcount++ != 0)
1008963e4c2aSGarrett Wollman 			return (0);
1009963e4c2aSGarrett Wollman 		ifp->if_flags |= IFF_PROMISC;
1010e9a30d00SGarrett Wollman 		log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
1011963e4c2aSGarrett Wollman 		    ifp->if_name, ifp->if_unit);
1012963e4c2aSGarrett Wollman 	} else {
1013963e4c2aSGarrett Wollman 		if (--ifp->if_pcount > 0)
1014963e4c2aSGarrett Wollman 			return (0);
1015963e4c2aSGarrett Wollman 		ifp->if_flags &= ~IFF_PROMISC;
1016cd9e4cabSSheldon Hearn 		log(LOG_INFO, "%s%d: promiscuous mode disabled\n",
1017cd9e4cabSSheldon Hearn 		    ifp->if_name, ifp->if_unit);
1018963e4c2aSGarrett Wollman 	}
1019963e4c2aSGarrett Wollman 	ifr.ifr_flags = ifp->if_flags;
10204a26224cSGarrett Wollman 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
10214a26224cSGarrett Wollman 	if (error == 0)
10224a26224cSGarrett Wollman 		rt_ifmsg(ifp);
10234a26224cSGarrett Wollman 	return error;
1024963e4c2aSGarrett Wollman }
1025963e4c2aSGarrett Wollman 
1026963e4c2aSGarrett Wollman /*
1027df8bae1dSRodney W. Grimes  * Return interface configuration
1028df8bae1dSRodney W. Grimes  * of system.  List may be used
1029df8bae1dSRodney W. Grimes  * in later ioctl's (above) to get
1030df8bae1dSRodney W. Grimes  * other information.
1031df8bae1dSRodney W. Grimes  */
1032df8bae1dSRodney W. Grimes /*ARGSUSED*/
10333bda9f9bSPoul-Henning Kamp static int
1034df8bae1dSRodney W. Grimes ifconf(cmd, data)
1035ecbb00a2SDoug Rabson 	u_long cmd;
1036df8bae1dSRodney W. Grimes 	caddr_t data;
1037df8bae1dSRodney W. Grimes {
1038df8bae1dSRodney W. Grimes 	register struct ifconf *ifc = (struct ifconf *)data;
103929412182SGarrett Wollman 	register struct ifnet *ifp = ifnet.tqh_first;
1040df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
1041df8bae1dSRodney W. Grimes 	struct ifreq ifr, *ifrp;
1042df8bae1dSRodney W. Grimes 	int space = ifc->ifc_len, error = 0;
1043df8bae1dSRodney W. Grimes 
1044df8bae1dSRodney W. Grimes 	ifrp = ifc->ifc_req;
104529412182SGarrett Wollman 	for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) {
10461ce9bf88SPoul-Henning Kamp 		char workbuf[64];
104775c13541SPoul-Henning Kamp 		int ifnlen, addrs;
10482624cf89SGarrett Wollman 
10492127f260SArchie Cobbs 		ifnlen = snprintf(workbuf, sizeof(workbuf),
10502127f260SArchie Cobbs 		    "%s%d", ifp->if_name, ifp->if_unit);
10511ce9bf88SPoul-Henning Kamp 		if(ifnlen + 1 > sizeof ifr.ifr_name) {
10522624cf89SGarrett Wollman 			error = ENAMETOOLONG;
10532624cf89SGarrett Wollman 		} else {
10541ce9bf88SPoul-Henning Kamp 			strcpy(ifr.ifr_name, workbuf);
10552624cf89SGarrett Wollman 		}
10562624cf89SGarrett Wollman 
105775c13541SPoul-Henning Kamp 		addrs = 0;
105875c13541SPoul-Henning Kamp 		ifa = ifp->if_addrhead.tqh_first;
105959562606SGarrett Wollman 		for ( ; space > sizeof (ifr) && ifa;
106059562606SGarrett Wollman 		    ifa = ifa->ifa_link.tqe_next) {
1061df8bae1dSRodney W. Grimes 			register struct sockaddr *sa = ifa->ifa_addr;
106275c13541SPoul-Henning Kamp 			if (curproc->p_prison && prison_if(curproc, sa))
106375c13541SPoul-Henning Kamp 				continue;
106475c13541SPoul-Henning Kamp 			addrs++;
1065df8bae1dSRodney W. Grimes #ifdef COMPAT_43
1066df8bae1dSRodney W. Grimes 			if (cmd == OSIOCGIFCONF) {
1067df8bae1dSRodney W. Grimes 				struct osockaddr *osa =
1068df8bae1dSRodney W. Grimes 					 (struct osockaddr *)&ifr.ifr_addr;
1069df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
1070df8bae1dSRodney W. Grimes 				osa->sa_family = sa->sa_family;
1071df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1072df8bae1dSRodney W. Grimes 						sizeof (ifr));
1073df8bae1dSRodney W. Grimes 				ifrp++;
1074df8bae1dSRodney W. Grimes 			} else
1075df8bae1dSRodney W. Grimes #endif
1076df8bae1dSRodney W. Grimes 			if (sa->sa_len <= sizeof(*sa)) {
1077df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
1078df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1079df8bae1dSRodney W. Grimes 						sizeof (ifr));
1080df8bae1dSRodney W. Grimes 				ifrp++;
1081df8bae1dSRodney W. Grimes 			} else {
1082df8bae1dSRodney W. Grimes 				space -= sa->sa_len - sizeof(*sa);
1083df8bae1dSRodney W. Grimes 				if (space < sizeof (ifr))
1084df8bae1dSRodney W. Grimes 					break;
1085df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1086df8bae1dSRodney W. Grimes 						sizeof (ifr.ifr_name));
1087df8bae1dSRodney W. Grimes 				if (error == 0)
1088df8bae1dSRodney W. Grimes 				    error = copyout((caddr_t)sa,
1089df8bae1dSRodney W. Grimes 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1090df8bae1dSRodney W. Grimes 				ifrp = (struct ifreq *)
1091df8bae1dSRodney W. Grimes 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1092df8bae1dSRodney W. Grimes 			}
1093df8bae1dSRodney W. Grimes 			if (error)
1094df8bae1dSRodney W. Grimes 				break;
1095df8bae1dSRodney W. Grimes 			space -= sizeof (ifr);
1096df8bae1dSRodney W. Grimes 		}
109775c13541SPoul-Henning Kamp 		if (!addrs) {
109875c13541SPoul-Henning Kamp 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
109975c13541SPoul-Henning Kamp 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
110075c13541SPoul-Henning Kamp 			    sizeof (ifr));
110175c13541SPoul-Henning Kamp 			if (error)
110275c13541SPoul-Henning Kamp 				break;
110375c13541SPoul-Henning Kamp 			space -= sizeof (ifr), ifrp++;
110475c13541SPoul-Henning Kamp 		}
1105df8bae1dSRodney W. Grimes 	}
1106df8bae1dSRodney W. Grimes 	ifc->ifc_len -= space;
1107df8bae1dSRodney W. Grimes 	return (error);
1108df8bae1dSRodney W. Grimes }
1109df8bae1dSRodney W. Grimes 
11101158dfb7SGarrett Wollman /*
11111158dfb7SGarrett Wollman  * Just like if_promisc(), but for all-multicast-reception mode.
11121158dfb7SGarrett Wollman  */
11131158dfb7SGarrett Wollman int
11141158dfb7SGarrett Wollman if_allmulti(ifp, onswitch)
11151158dfb7SGarrett Wollman 	struct ifnet *ifp;
11161158dfb7SGarrett Wollman 	int onswitch;
11171158dfb7SGarrett Wollman {
11181158dfb7SGarrett Wollman 	int error = 0;
11191158dfb7SGarrett Wollman 	int s = splimp();
11201158dfb7SGarrett Wollman 
11211158dfb7SGarrett Wollman 	if (onswitch) {
11221158dfb7SGarrett Wollman 		if (ifp->if_amcount++ == 0) {
11231158dfb7SGarrett Wollman 			ifp->if_flags |= IFF_ALLMULTI;
11241158dfb7SGarrett Wollman 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
11251158dfb7SGarrett Wollman 		}
11261158dfb7SGarrett Wollman 	} else {
11271158dfb7SGarrett Wollman 		if (ifp->if_amcount > 1) {
11281158dfb7SGarrett Wollman 			ifp->if_amcount--;
11291158dfb7SGarrett Wollman 		} else {
11301158dfb7SGarrett Wollman 			ifp->if_amcount = 0;
11311158dfb7SGarrett Wollman 			ifp->if_flags &= ~IFF_ALLMULTI;
11321158dfb7SGarrett Wollman 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
11331158dfb7SGarrett Wollman 		}
11341158dfb7SGarrett Wollman 	}
11351158dfb7SGarrett Wollman 	splx(s);
11364a26224cSGarrett Wollman 
11374a26224cSGarrett Wollman 	if (error == 0)
11384a26224cSGarrett Wollman 		rt_ifmsg(ifp);
11391158dfb7SGarrett Wollman 	return error;
11401158dfb7SGarrett Wollman }
11411158dfb7SGarrett Wollman 
11421158dfb7SGarrett Wollman /*
11431158dfb7SGarrett Wollman  * Add a multicast listenership to the interface in question.
11441158dfb7SGarrett Wollman  * The link layer provides a routine which converts
11451158dfb7SGarrett Wollman  */
11461158dfb7SGarrett Wollman int
1147373f88edSGarrett Wollman if_addmulti(ifp, sa, retifma)
11481158dfb7SGarrett Wollman 	struct ifnet *ifp;	/* interface to manipulate */
11491158dfb7SGarrett Wollman 	struct sockaddr *sa;	/* address to add */
1150b2053118SGarrett Wollman 	struct ifmultiaddr **retifma;
11511158dfb7SGarrett Wollman {
11521158dfb7SGarrett Wollman 	struct sockaddr *llsa, *dupsa;
11531158dfb7SGarrett Wollman 	int error, s;
11541158dfb7SGarrett Wollman 	struct ifmultiaddr *ifma;
11551158dfb7SGarrett Wollman 
115657af7922SJulian Elischer 	/*
115757af7922SJulian Elischer 	 * If the matching multicast address already exists
115857af7922SJulian Elischer 	 * then don't add a new one, just add a reference
115957af7922SJulian Elischer 	 */
11601158dfb7SGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
11611158dfb7SGarrett Wollman 	     ifma = ifma->ifma_link.le_next) {
116257af7922SJulian Elischer 		if (equal(sa, ifma->ifma_addr)) {
11631158dfb7SGarrett Wollman 			ifma->ifma_refcount++;
116457af7922SJulian Elischer 			if (retifma)
116557af7922SJulian Elischer 				*retifma = ifma;
11661158dfb7SGarrett Wollman 			return 0;
11671158dfb7SGarrett Wollman 		}
116857af7922SJulian Elischer 	}
11691158dfb7SGarrett Wollman 
11701158dfb7SGarrett Wollman 	/*
11711158dfb7SGarrett Wollman 	 * Give the link layer a chance to accept/reject it, and also
11721158dfb7SGarrett Wollman 	 * find out which AF_LINK address this maps to, if it isn't one
11731158dfb7SGarrett Wollman 	 * already.
11741158dfb7SGarrett Wollman 	 */
11751158dfb7SGarrett Wollman 	if (ifp->if_resolvemulti) {
11761158dfb7SGarrett Wollman 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
11771158dfb7SGarrett Wollman 		if (error) return error;
11781158dfb7SGarrett Wollman 	} else {
11791158dfb7SGarrett Wollman 		llsa = 0;
11801158dfb7SGarrett Wollman 	}
11811158dfb7SGarrett Wollman 
11821158dfb7SGarrett Wollman 	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
11831158dfb7SGarrett Wollman 	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
11841158dfb7SGarrett Wollman 	bcopy(sa, dupsa, sa->sa_len);
11851158dfb7SGarrett Wollman 
11861158dfb7SGarrett Wollman 	ifma->ifma_addr = dupsa;
11871158dfb7SGarrett Wollman 	ifma->ifma_lladdr = llsa;
11881158dfb7SGarrett Wollman 	ifma->ifma_ifp = ifp;
11891158dfb7SGarrett Wollman 	ifma->ifma_refcount = 1;
1190373f88edSGarrett Wollman 	ifma->ifma_protospec = 0;
1191477180fbSGarrett Wollman 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1192373f88edSGarrett Wollman 
11931158dfb7SGarrett Wollman 	/*
11941158dfb7SGarrett Wollman 	 * Some network interfaces can scan the address list at
11951158dfb7SGarrett Wollman 	 * interrupt time; lock them out.
11961158dfb7SGarrett Wollman 	 */
11971158dfb7SGarrett Wollman 	s = splimp();
11981158dfb7SGarrett Wollman 	LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
11991158dfb7SGarrett Wollman 	splx(s);
1200373f88edSGarrett Wollman 	*retifma = ifma;
12011158dfb7SGarrett Wollman 
12021158dfb7SGarrett Wollman 	if (llsa != 0) {
12031158dfb7SGarrett Wollman 		for (ifma = ifp->if_multiaddrs.lh_first; ifma;
12041158dfb7SGarrett Wollman 		     ifma = ifma->ifma_link.le_next) {
12051158dfb7SGarrett Wollman 			if (equal(ifma->ifma_addr, llsa))
12061158dfb7SGarrett Wollman 				break;
12071158dfb7SGarrett Wollman 		}
12081158dfb7SGarrett Wollman 		if (ifma) {
12091158dfb7SGarrett Wollman 			ifma->ifma_refcount++;
12101158dfb7SGarrett Wollman 		} else {
12111158dfb7SGarrett Wollman 			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
12121158dfb7SGarrett Wollman 			       M_IFMADDR, M_WAITOK);
1213477180fbSGarrett Wollman 			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1214477180fbSGarrett Wollman 			       M_IFMADDR, M_WAITOK);
1215477180fbSGarrett Wollman 			bcopy(llsa, dupsa, llsa->sa_len);
1216477180fbSGarrett Wollman 			ifma->ifma_addr = dupsa;
12171158dfb7SGarrett Wollman 			ifma->ifma_ifp = ifp;
12181158dfb7SGarrett Wollman 			ifma->ifma_refcount = 1;
12191158dfb7SGarrett Wollman 			s = splimp();
12201158dfb7SGarrett Wollman 			LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
12211158dfb7SGarrett Wollman 			splx(s);
12221158dfb7SGarrett Wollman 		}
122357af7922SJulian Elischer 	}
12241158dfb7SGarrett Wollman 	/*
12251158dfb7SGarrett Wollman 	 * We are certain we have added something, so call down to the
12261158dfb7SGarrett Wollman 	 * interface to let them know about it.
12271158dfb7SGarrett Wollman 	 */
12281158dfb7SGarrett Wollman 	s = splimp();
12291158dfb7SGarrett Wollman 	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
12301158dfb7SGarrett Wollman 	splx(s);
12311158dfb7SGarrett Wollman 
12321158dfb7SGarrett Wollman 	return 0;
12331158dfb7SGarrett Wollman }
12341158dfb7SGarrett Wollman 
12351158dfb7SGarrett Wollman /*
12361158dfb7SGarrett Wollman  * Remove a reference to a multicast address on this interface.  Yell
12371158dfb7SGarrett Wollman  * if the request does not match an existing membership.
12381158dfb7SGarrett Wollman  */
12391158dfb7SGarrett Wollman int
12401158dfb7SGarrett Wollman if_delmulti(ifp, sa)
12411158dfb7SGarrett Wollman 	struct ifnet *ifp;
12421158dfb7SGarrett Wollman 	struct sockaddr *sa;
12431158dfb7SGarrett Wollman {
12441158dfb7SGarrett Wollman 	struct ifmultiaddr *ifma;
12451158dfb7SGarrett Wollman 	int s;
12461158dfb7SGarrett Wollman 
12471158dfb7SGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
12481158dfb7SGarrett Wollman 	     ifma = ifma->ifma_link.le_next)
12491158dfb7SGarrett Wollman 		if (equal(sa, ifma->ifma_addr))
12501158dfb7SGarrett Wollman 			break;
12511158dfb7SGarrett Wollman 	if (ifma == 0)
12521158dfb7SGarrett Wollman 		return ENOENT;
12531158dfb7SGarrett Wollman 
12541158dfb7SGarrett Wollman 	if (ifma->ifma_refcount > 1) {
12551158dfb7SGarrett Wollman 		ifma->ifma_refcount--;
12561158dfb7SGarrett Wollman 		return 0;
12571158dfb7SGarrett Wollman 	}
12581158dfb7SGarrett Wollman 
1259477180fbSGarrett Wollman 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
12601158dfb7SGarrett Wollman 	sa = ifma->ifma_lladdr;
12611158dfb7SGarrett Wollman 	s = splimp();
12621158dfb7SGarrett Wollman 	LIST_REMOVE(ifma, ifma_link);
12631158dfb7SGarrett Wollman 	splx(s);
12641158dfb7SGarrett Wollman 	free(ifma->ifma_addr, M_IFMADDR);
12651158dfb7SGarrett Wollman 	free(ifma, M_IFMADDR);
12661158dfb7SGarrett Wollman 	if (sa == 0)
12671158dfb7SGarrett Wollman 		return 0;
12681158dfb7SGarrett Wollman 
12691158dfb7SGarrett Wollman 	/*
12701158dfb7SGarrett Wollman 	 * Now look for the link-layer address which corresponds to
12711158dfb7SGarrett Wollman 	 * this network address.  It had been squirreled away in
12721158dfb7SGarrett Wollman 	 * ifma->ifma_lladdr for this purpose (so we don't have
12731158dfb7SGarrett Wollman 	 * to call ifp->if_resolvemulti() again), and we saved that
12741158dfb7SGarrett Wollman 	 * value in sa above.  If some nasty deleted the
12751158dfb7SGarrett Wollman 	 * link-layer address out from underneath us, we can deal because
12761158dfb7SGarrett Wollman 	 * the address we stored was is not the same as the one which was
12771158dfb7SGarrett Wollman 	 * in the record for the link-layer address.  (So we don't complain
12781158dfb7SGarrett Wollman 	 * in that case.)
12791158dfb7SGarrett Wollman 	 */
12801158dfb7SGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
12811158dfb7SGarrett Wollman 	     ifma = ifma->ifma_link.le_next)
12821158dfb7SGarrett Wollman 		if (equal(sa, ifma->ifma_addr))
12831158dfb7SGarrett Wollman 			break;
12841158dfb7SGarrett Wollman 	if (ifma == 0)
12851158dfb7SGarrett Wollman 		return 0;
12861158dfb7SGarrett Wollman 
12871158dfb7SGarrett Wollman 	if (ifma->ifma_refcount > 1) {
12881158dfb7SGarrett Wollman 		ifma->ifma_refcount--;
12891158dfb7SGarrett Wollman 		return 0;
12901158dfb7SGarrett Wollman 	}
12911158dfb7SGarrett Wollman 
12921158dfb7SGarrett Wollman 	s = splimp();
12931158dfb7SGarrett Wollman 	LIST_REMOVE(ifma, ifma_link);
1294c7323482SBill Paul 	ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
12951158dfb7SGarrett Wollman 	splx(s);
12961158dfb7SGarrett Wollman 	free(ifma->ifma_addr, M_IFMADDR);
12971158dfb7SGarrett Wollman 	free(sa, M_IFMADDR);
12981158dfb7SGarrett Wollman 	free(ifma, M_IFMADDR);
12991158dfb7SGarrett Wollman 
13001158dfb7SGarrett Wollman 	return 0;
13011158dfb7SGarrett Wollman }
13021158dfb7SGarrett Wollman 
1303373f88edSGarrett Wollman struct ifmultiaddr *
1304373f88edSGarrett Wollman ifmaof_ifpforaddr(sa, ifp)
1305373f88edSGarrett Wollman 	struct sockaddr *sa;
1306373f88edSGarrett Wollman 	struct ifnet *ifp;
1307373f88edSGarrett Wollman {
1308373f88edSGarrett Wollman 	struct ifmultiaddr *ifma;
1309373f88edSGarrett Wollman 
1310373f88edSGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1311373f88edSGarrett Wollman 	     ifma = ifma->ifma_link.le_next)
1312373f88edSGarrett Wollman 		if (equal(ifma->ifma_addr, sa))
1313373f88edSGarrett Wollman 			break;
1314373f88edSGarrett Wollman 
1315373f88edSGarrett Wollman 	return ifma;
1316373f88edSGarrett Wollman }
1317373f88edSGarrett Wollman 
1318602d513cSGarrett Wollman SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
13192c37256eSGarrett Wollman SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
1320