xref: /freebsd/sys/net/if.c (revision d7189ec67c22447b42eeafe38015d2e93faae4c3)
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
34d7189ec6SJoerg Wunsch  *	$Id: if.c,v 1.53 1997/09/07 11:09:22 joerg Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
382ee45d7dSDavid Greenman #include <sys/queue.h>
394d1d4912SBruce Evans #include <sys/malloc.h>
40df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
41df8bae1dSRodney W. Grimes #include <sys/systm.h>
42df8bae1dSRodney W. Grimes #include <sys/proc.h>
43df8bae1dSRodney W. Grimes #include <sys/socket.h>
44df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
45df8bae1dSRodney W. Grimes #include <sys/protosw.h>
46df8bae1dSRodney W. Grimes #include <sys/kernel.h>
4751a53488SBruce Evans #include <sys/sockio.h>
482624cf89SGarrett Wollman #include <sys/errno.h>
49963e4c2aSGarrett Wollman #include <sys/syslog.h>
50602d513cSGarrett Wollman #include <sys/sysctl.h>
51df8bae1dSRodney W. Grimes 
52df8bae1dSRodney W. Grimes #include <net/if.h>
53df8bae1dSRodney W. Grimes #include <net/if_dl.h>
54df8bae1dSRodney W. Grimes #include <net/if_types.h>
559448326fSPoul-Henning Kamp #include <net/radix.h>
56df8bae1dSRodney W. Grimes 
572b14f991SJulian Elischer /*
582b14f991SJulian Elischer  * System initialization
592b14f991SJulian Elischer  */
602b14f991SJulian Elischer 
613bda9f9bSPoul-Henning Kamp static int ifconf __P((int, caddr_t));
624590fd3aSDavid Greenman static void ifinit __P((void *));
633bda9f9bSPoul-Henning Kamp static void if_qflush __P((struct ifqueue *));
643bda9f9bSPoul-Henning Kamp static void if_slowtimo __P((void *));
653bda9f9bSPoul-Henning Kamp static void link_rtrequest __P((int, struct rtentry *, struct sockaddr *));
663bda9f9bSPoul-Henning Kamp 
672b14f991SJulian Elischer SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
682b14f991SJulian Elischer 
692b14f991SJulian Elischer 
70df8bae1dSRodney W. Grimes int	ifqmaxlen = IFQ_MAXLEN;
7129412182SGarrett Wollman struct	ifnethead ifnet;	/* depend on static init XXX */
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes /*
74df8bae1dSRodney W. Grimes  * Network interface utility routines.
75df8bae1dSRodney W. Grimes  *
76df8bae1dSRodney W. Grimes  * Routines with ifa_ifwith* names take sockaddr *'s as
77df8bae1dSRodney W. Grimes  * parameters.
782b14f991SJulian Elischer  *
792b14f991SJulian Elischer  * This routine assumes that it will be called at splimp() or higher.
80df8bae1dSRodney W. Grimes  */
812b14f991SJulian Elischer /* ARGSUSED*/
82df8bae1dSRodney W. Grimes void
8327501cb6SBruce Evans ifinit(dummy)
8427501cb6SBruce Evans 	void *dummy;
85df8bae1dSRodney W. Grimes {
86df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
87df8bae1dSRodney W. Grimes 
8829412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
89df8bae1dSRodney W. Grimes 		if (ifp->if_snd.ifq_maxlen == 0)
90df8bae1dSRodney W. Grimes 			ifp->if_snd.ifq_maxlen = ifqmaxlen;
91df8bae1dSRodney W. Grimes 	if_slowtimo(0);
92df8bae1dSRodney W. Grimes }
93df8bae1dSRodney W. Grimes 
94bbd17bf8SGarrett Wollman int if_index = 0;
95bbd17bf8SGarrett Wollman struct ifaddr **ifnet_addrs;
963bda9f9bSPoul-Henning Kamp 
97df8bae1dSRodney W. Grimes 
98df8bae1dSRodney W. Grimes /*
99df8bae1dSRodney W. Grimes  * Attach an interface to the
100df8bae1dSRodney W. Grimes  * list of "active" interfaces.
101df8bae1dSRodney W. Grimes  */
102df8bae1dSRodney W. Grimes void
103df8bae1dSRodney W. Grimes if_attach(ifp)
104df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
105df8bae1dSRodney W. Grimes {
106df8bae1dSRodney W. Grimes 	unsigned socksize, ifasize;
1071ce9bf88SPoul-Henning Kamp 	int namelen, masklen;
1081ce9bf88SPoul-Henning Kamp 	char workbuf[64];
109df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
110df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
111df8bae1dSRodney W. Grimes 	static int if_indexlim = 8;
11229412182SGarrett Wollman 	static int inited;
113f23b4c91SGarrett Wollman 
11429412182SGarrett Wollman 	if (!inited) {
11529412182SGarrett Wollman 		TAILQ_INIT(&ifnet);
11629412182SGarrett Wollman 		inited = 1;
11729412182SGarrett Wollman 	}
118df8bae1dSRodney W. Grimes 
11929412182SGarrett Wollman 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
120df8bae1dSRodney W. Grimes 	ifp->if_index = ++if_index;
12159562606SGarrett Wollman 	/*
12259562606SGarrett Wollman 	 * XXX -
12359562606SGarrett Wollman 	 * The old code would work if the interface passed a pre-existing
12459562606SGarrett Wollman 	 * chain of ifaddrs to this code.  We don't trust our callers to
12559562606SGarrett Wollman 	 * properly initialize the tailq, however, so we no longer allow
12659562606SGarrett Wollman 	 * this unlikely case.
12759562606SGarrett Wollman 	 */
12859562606SGarrett Wollman 	TAILQ_INIT(&ifp->if_addrhead);
1291158dfb7SGarrett Wollman 	LIST_INIT(&ifp->if_multiaddrs);
130a614bfe0SGary Palmer 	microtime(&ifp->if_lastchange);
131df8bae1dSRodney W. Grimes 	if (ifnet_addrs == 0 || if_index >= if_indexlim) {
132df8bae1dSRodney W. Grimes 		unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
133df8bae1dSRodney W. Grimes 		struct ifaddr **q = (struct ifaddr **)
134df8bae1dSRodney W. Grimes 					malloc(n, M_IFADDR, M_WAITOK);
13573c2ab46SDavid Greenman 		bzero((caddr_t)q, n);
136df8bae1dSRodney W. Grimes 		if (ifnet_addrs) {
137df8bae1dSRodney W. Grimes 			bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
138df8bae1dSRodney W. Grimes 			free((caddr_t)ifnet_addrs, M_IFADDR);
139df8bae1dSRodney W. Grimes 		}
140df8bae1dSRodney W. Grimes 		ifnet_addrs = q;
141df8bae1dSRodney W. Grimes 	}
142df8bae1dSRodney W. Grimes 	/*
143df8bae1dSRodney W. Grimes 	 * create a Link Level name for this device
144df8bae1dSRodney W. Grimes 	 */
1451ce9bf88SPoul-Henning Kamp 	namelen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
146df8bae1dSRodney W. Grimes #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
1471ce9bf88SPoul-Henning Kamp 	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
148df8bae1dSRodney W. Grimes 	socksize = masklen + ifp->if_addrlen;
149df8bae1dSRodney W. Grimes #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
150df8bae1dSRodney W. Grimes 	socksize = ROUNDUP(socksize);
151df8bae1dSRodney W. Grimes 	if (socksize < sizeof(*sdl))
152df8bae1dSRodney W. Grimes 		socksize = sizeof(*sdl);
153df8bae1dSRodney W. Grimes 	ifasize = sizeof(*ifa) + 2 * socksize;
1549448326fSPoul-Henning Kamp 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
1559448326fSPoul-Henning Kamp 	if (ifa) {
156df8bae1dSRodney W. Grimes 		bzero((caddr_t)ifa, ifasize);
157df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(ifa + 1);
158df8bae1dSRodney W. Grimes 		sdl->sdl_len = socksize;
159df8bae1dSRodney W. Grimes 		sdl->sdl_family = AF_LINK;
1601ce9bf88SPoul-Henning Kamp 		bcopy(workbuf, sdl->sdl_data, namelen);
1611ce9bf88SPoul-Henning Kamp 		sdl->sdl_nlen = namelen;
162df8bae1dSRodney W. Grimes 		sdl->sdl_index = ifp->if_index;
163df8bae1dSRodney W. Grimes 		sdl->sdl_type = ifp->if_type;
164df8bae1dSRodney W. Grimes 		ifnet_addrs[if_index - 1] = ifa;
165df8bae1dSRodney W. Grimes 		ifa->ifa_ifp = ifp;
166df8bae1dSRodney W. Grimes 		ifa->ifa_rtrequest = link_rtrequest;
167df8bae1dSRodney W. Grimes 		ifa->ifa_addr = (struct sockaddr *)sdl;
168df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
169df8bae1dSRodney W. Grimes 		ifa->ifa_netmask = (struct sockaddr *)sdl;
170df8bae1dSRodney W. Grimes 		sdl->sdl_len = masklen;
171df8bae1dSRodney W. Grimes 		while (namelen != 0)
172df8bae1dSRodney W. Grimes 			sdl->sdl_data[--namelen] = 0xff;
17359562606SGarrett Wollman 		TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
174df8bae1dSRodney W. Grimes 	}
175df8bae1dSRodney W. Grimes }
176df8bae1dSRodney W. Grimes /*
177df8bae1dSRodney W. Grimes  * Locate an interface based on a complete address.
178df8bae1dSRodney W. Grimes  */
179df8bae1dSRodney W. Grimes /*ARGSUSED*/
180df8bae1dSRodney W. Grimes struct ifaddr *
181df8bae1dSRodney W. Grimes ifa_ifwithaddr(addr)
182df8bae1dSRodney W. Grimes 	register struct sockaddr *addr;
183df8bae1dSRodney W. Grimes {
184df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
185df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
186df8bae1dSRodney W. Grimes 
187df8bae1dSRodney W. Grimes #define	equal(a1, a2) \
188df8bae1dSRodney W. Grimes   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
18929412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
19059562606SGarrett Wollman 	    for (ifa = ifp->if_addrhead.tqh_first; ifa;
19159562606SGarrett Wollman 		 ifa = ifa->ifa_link.tqe_next) {
192df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != addr->sa_family)
193df8bae1dSRodney W. Grimes 			continue;
194df8bae1dSRodney W. Grimes 		if (equal(addr, ifa->ifa_addr))
195df8bae1dSRodney W. Grimes 			return (ifa);
196df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
197df8bae1dSRodney W. Grimes 		    equal(ifa->ifa_broadaddr, addr))
198df8bae1dSRodney W. Grimes 			return (ifa);
199df8bae1dSRodney W. Grimes 	}
200df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
201df8bae1dSRodney W. Grimes }
202df8bae1dSRodney W. Grimes /*
203df8bae1dSRodney W. Grimes  * Locate the point to point interface with a given destination address.
204df8bae1dSRodney W. Grimes  */
205df8bae1dSRodney W. Grimes /*ARGSUSED*/
206df8bae1dSRodney W. Grimes struct ifaddr *
207df8bae1dSRodney W. Grimes ifa_ifwithdstaddr(addr)
208df8bae1dSRodney W. Grimes 	register struct sockaddr *addr;
209df8bae1dSRodney W. Grimes {
210df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
211df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
212df8bae1dSRodney W. Grimes 
21329412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
214df8bae1dSRodney W. Grimes 	    if (ifp->if_flags & IFF_POINTOPOINT)
21559562606SGarrett Wollman 		for (ifa = ifp->if_addrhead.tqh_first; ifa;
21659562606SGarrett Wollman 		     ifa = ifa->ifa_link.tqe_next) {
217df8bae1dSRodney W. Grimes 			if (ifa->ifa_addr->sa_family != addr->sa_family)
218df8bae1dSRodney W. Grimes 				continue;
21955088a1cSDavid Greenman 			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
220df8bae1dSRodney W. Grimes 				return (ifa);
221df8bae1dSRodney W. Grimes 	}
222df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
223df8bae1dSRodney W. Grimes }
224df8bae1dSRodney W. Grimes 
225df8bae1dSRodney W. Grimes /*
226df8bae1dSRodney W. Grimes  * Find an interface on a specific network.  If many, choice
227df8bae1dSRodney W. Grimes  * is most specific found.
228df8bae1dSRodney W. Grimes  */
229df8bae1dSRodney W. Grimes struct ifaddr *
230df8bae1dSRodney W. Grimes ifa_ifwithnet(addr)
231df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
232df8bae1dSRodney W. Grimes {
233df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
234df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
235df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
236df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
237df8bae1dSRodney W. Grimes 	char *addr_data = addr->sa_data, *cplim;
238df8bae1dSRodney W. Grimes 
2397e2a6151SJulian Elischer 	/*
2407e2a6151SJulian Elischer 	 * AF_LINK addresses can be looked up directly by their index number,
2417e2a6151SJulian Elischer 	 * so do that if we can.
2427e2a6151SJulian Elischer 	 */
243df8bae1dSRodney W. Grimes 	if (af == AF_LINK) {
244df8bae1dSRodney W. Grimes 	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
245df8bae1dSRodney W. Grimes 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
246df8bae1dSRodney W. Grimes 		return (ifnet_addrs[sdl->sdl_index - 1]);
247df8bae1dSRodney W. Grimes 	}
2487e2a6151SJulian Elischer 
2497e2a6151SJulian Elischer 	/*
2507e2a6151SJulian Elischer 	 * Scan though each interface, looking for ones that have
2517e2a6151SJulian Elischer 	 * addresses in this address family.
2527e2a6151SJulian Elischer 	 */
25329412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
25459562606SGarrett Wollman 		for (ifa = ifp->if_addrhead.tqh_first; ifa;
25559562606SGarrett Wollman 		     ifa = ifa->ifa_link.tqe_next) {
256df8bae1dSRodney W. Grimes 			register char *cp, *cp2, *cp3;
257df8bae1dSRodney W. Grimes 
258523a02aaSDavid Greenman 			if (ifa->ifa_addr->sa_family != af)
259df8bae1dSRodney W. Grimes next:				continue;
260b2af64fdSDavid Greenman 			if (ifp->if_flags & IFF_POINTOPOINT) {
2617e2a6151SJulian Elischer 				/*
2627e2a6151SJulian Elischer 				 * This is a bit broken as it doesn't
2637e2a6151SJulian Elischer 				 * take into account that the remote end may
2647e2a6151SJulian Elischer 				 * be a single node in the network we are
2657e2a6151SJulian Elischer 				 * looking for.
2667e2a6151SJulian Elischer 				 * The trouble is that we don't know the
2677e2a6151SJulian Elischer 				 * netmask for the remote end.
2687e2a6151SJulian Elischer 				 */
269fcd6781aSGarrett Wollman 				if (ifa->ifa_dstaddr != 0
270fcd6781aSGarrett Wollman 				    && equal(addr, ifa->ifa_dstaddr))
271b2af64fdSDavid Greenman  					return (ifa);
2723740e2adSDavid Greenman 			} else {
2737e2a6151SJulian Elischer 				/*
2747ed8f465SJulian Elischer 				 * if we have a special address handler,
2757ed8f465SJulian Elischer 				 * then use it instead of the generic one.
2767ed8f465SJulian Elischer 				 */
2777ed8f465SJulian Elischer 	          		if (ifa->ifa_claim_addr) {
2787ed8f465SJulian Elischer 					if ((*ifa->ifa_claim_addr)(ifa, addr)) {
2797ed8f465SJulian Elischer 						return (ifa);
2807ed8f465SJulian Elischer 					} else {
2817ed8f465SJulian Elischer 						continue;
2827ed8f465SJulian Elischer 					}
2837ed8f465SJulian Elischer 				}
2847ed8f465SJulian Elischer 
2857ed8f465SJulian Elischer 				/*
2867e2a6151SJulian Elischer 				 * Scan all the bits in the ifa's address.
2877e2a6151SJulian Elischer 				 * If a bit dissagrees with what we are
2887e2a6151SJulian Elischer 				 * looking for, mask it with the netmask
2897e2a6151SJulian Elischer 				 * to see if it really matters.
2907e2a6151SJulian Elischer 				 * (A byte at a time)
2917e2a6151SJulian Elischer 				 */
292523a02aaSDavid Greenman 				if (ifa->ifa_netmask == 0)
293523a02aaSDavid Greenman 					continue;
294df8bae1dSRodney W. Grimes 				cp = addr_data;
295df8bae1dSRodney W. Grimes 				cp2 = ifa->ifa_addr->sa_data;
296df8bae1dSRodney W. Grimes 				cp3 = ifa->ifa_netmask->sa_data;
2977e2a6151SJulian Elischer 				cplim = ifa->ifa_netmask->sa_len
2987e2a6151SJulian Elischer 					+ (char *)ifa->ifa_netmask;
299df8bae1dSRodney W. Grimes 				while (cp3 < cplim)
300df8bae1dSRodney W. Grimes 					if ((*cp++ ^ *cp2++) & *cp3++)
3017e2a6151SJulian Elischer 						goto next; /* next address! */
3027e2a6151SJulian Elischer 				/*
3037e2a6151SJulian Elischer 				 * If the netmask of what we just found
3047e2a6151SJulian Elischer 				 * is more specific than what we had before
3057e2a6151SJulian Elischer 				 * (if we had one) then remember the new one
3067e2a6151SJulian Elischer 				 * before continuing to search
3077e2a6151SJulian Elischer 				 * for an even better one.
3087e2a6151SJulian Elischer 				 */
309df8bae1dSRodney W. Grimes 				if (ifa_maybe == 0 ||
310df8bae1dSRodney W. Grimes 				    rn_refines((caddr_t)ifa->ifa_netmask,
311df8bae1dSRodney W. Grimes 				    (caddr_t)ifa_maybe->ifa_netmask))
312df8bae1dSRodney W. Grimes 					ifa_maybe = ifa;
313df8bae1dSRodney W. Grimes 			}
314b2af64fdSDavid Greenman 		}
315b2af64fdSDavid Greenman 	}
316df8bae1dSRodney W. Grimes 	return (ifa_maybe);
317df8bae1dSRodney W. Grimes }
318df8bae1dSRodney W. Grimes 
319df8bae1dSRodney W. Grimes /*
320df8bae1dSRodney W. Grimes  * Find an interface address specific to an interface best matching
321df8bae1dSRodney W. Grimes  * a given address.
322df8bae1dSRodney W. Grimes  */
323df8bae1dSRodney W. Grimes struct ifaddr *
324df8bae1dSRodney W. Grimes ifaof_ifpforaddr(addr, ifp)
325df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
326df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
327df8bae1dSRodney W. Grimes {
328df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
329df8bae1dSRodney W. Grimes 	register char *cp, *cp2, *cp3;
330df8bae1dSRodney W. Grimes 	register char *cplim;
331df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = 0;
332df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
333df8bae1dSRodney W. Grimes 
334df8bae1dSRodney W. Grimes 	if (af >= AF_MAX)
335df8bae1dSRodney W. Grimes 		return (0);
33659562606SGarrett Wollman 	for (ifa = ifp->if_addrhead.tqh_first; ifa;
33759562606SGarrett Wollman 	     ifa = ifa->ifa_link.tqe_next) {
338df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != af)
339df8bae1dSRodney W. Grimes 			continue;
340381dd1d2SJulian Elischer 		if (ifa_maybe == 0)
341df8bae1dSRodney W. Grimes 			ifa_maybe = ifa;
342df8bae1dSRodney W. Grimes 		if (ifa->ifa_netmask == 0) {
343df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_addr) ||
344df8bae1dSRodney W. Grimes 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
345df8bae1dSRodney W. Grimes 				return (ifa);
346df8bae1dSRodney W. Grimes 			continue;
347df8bae1dSRodney W. Grimes 		}
348b2af64fdSDavid Greenman 		if (ifp->if_flags & IFF_POINTOPOINT) {
349b2af64fdSDavid Greenman 			if (equal(addr, ifa->ifa_dstaddr))
350b2af64fdSDavid Greenman 				return (ifa);
3513740e2adSDavid Greenman 		} else {
352df8bae1dSRodney W. Grimes 			cp = addr->sa_data;
353df8bae1dSRodney W. Grimes 			cp2 = ifa->ifa_addr->sa_data;
354df8bae1dSRodney W. Grimes 			cp3 = ifa->ifa_netmask->sa_data;
355df8bae1dSRodney W. Grimes 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
356df8bae1dSRodney W. Grimes 			for (; cp3 < cplim; cp3++)
357df8bae1dSRodney W. Grimes 				if ((*cp++ ^ *cp2++) & *cp3)
358df8bae1dSRodney W. Grimes 					break;
359df8bae1dSRodney W. Grimes 			if (cp3 == cplim)
360df8bae1dSRodney W. Grimes 				return (ifa);
361df8bae1dSRodney W. Grimes 		}
362b2af64fdSDavid Greenman 	}
363df8bae1dSRodney W. Grimes 	return (ifa_maybe);
364df8bae1dSRodney W. Grimes }
365df8bae1dSRodney W. Grimes 
366df8bae1dSRodney W. Grimes #include <net/route.h>
367df8bae1dSRodney W. Grimes 
368df8bae1dSRodney W. Grimes /*
369df8bae1dSRodney W. Grimes  * Default action when installing a route with a Link Level gateway.
370df8bae1dSRodney W. Grimes  * Lookup an appropriate real ifa to point to.
371df8bae1dSRodney W. Grimes  * This should be moved to /sys/net/link.c eventually.
372df8bae1dSRodney W. Grimes  */
3733bda9f9bSPoul-Henning Kamp static void
374df8bae1dSRodney W. Grimes link_rtrequest(cmd, rt, sa)
375df8bae1dSRodney W. Grimes 	int cmd;
376df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
377df8bae1dSRodney W. Grimes 	struct sockaddr *sa;
378df8bae1dSRodney W. Grimes {
379df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
380df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
381df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
382df8bae1dSRodney W. Grimes 
383df8bae1dSRodney W. Grimes 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
384df8bae1dSRodney W. Grimes 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
385df8bae1dSRodney W. Grimes 		return;
3869448326fSPoul-Henning Kamp 	ifa = ifaof_ifpforaddr(dst, ifp);
3879448326fSPoul-Henning Kamp 	if (ifa) {
388df8bae1dSRodney W. Grimes 		IFAFREE(rt->rt_ifa);
389df8bae1dSRodney W. Grimes 		rt->rt_ifa = ifa;
390df8bae1dSRodney W. Grimes 		ifa->ifa_refcnt++;
391df8bae1dSRodney W. Grimes 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
392df8bae1dSRodney W. Grimes 			ifa->ifa_rtrequest(cmd, rt, sa);
393df8bae1dSRodney W. Grimes 	}
394df8bae1dSRodney W. Grimes }
395df8bae1dSRodney W. Grimes 
396df8bae1dSRodney W. Grimes /*
397df8bae1dSRodney W. Grimes  * Mark an interface down and notify protocols of
398df8bae1dSRodney W. Grimes  * the transition.
399df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
400df8bae1dSRodney W. Grimes  */
401df8bae1dSRodney W. Grimes void
402df8bae1dSRodney W. Grimes if_down(ifp)
403df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
404df8bae1dSRodney W. Grimes {
405df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
406df8bae1dSRodney W. Grimes 
407df8bae1dSRodney W. Grimes 	ifp->if_flags &= ~IFF_UP;
408a614bfe0SGary Palmer 	microtime(&ifp->if_lastchange);
40959562606SGarrett Wollman 	for (ifa = ifp->if_addrhead.tqh_first; ifa;
41059562606SGarrett Wollman 	     ifa = ifa->ifa_link.tqe_next)
411df8bae1dSRodney W. Grimes 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
412df8bae1dSRodney W. Grimes 	if_qflush(&ifp->if_snd);
413df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
414df8bae1dSRodney W. Grimes }
415df8bae1dSRodney W. Grimes 
416df8bae1dSRodney W. Grimes /*
417df8bae1dSRodney W. Grimes  * Mark an interface up and notify protocols of
418df8bae1dSRodney W. Grimes  * the transition.
419df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
420df8bae1dSRodney W. Grimes  */
421df8bae1dSRodney W. Grimes void
422df8bae1dSRodney W. Grimes if_up(ifp)
423df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
424df8bae1dSRodney W. Grimes {
425176395b2SGarrett Wollman 	register struct ifaddr *ifa;
426df8bae1dSRodney W. Grimes 
427df8bae1dSRodney W. Grimes 	ifp->if_flags |= IFF_UP;
428a614bfe0SGary Palmer 	microtime(&ifp->if_lastchange);
429176395b2SGarrett Wollman 	for (ifa = ifp->if_addrhead.tqh_first; ifa;
430176395b2SGarrett Wollman 	     ifa = ifa->ifa_link.tqe_next)
431df8bae1dSRodney W. Grimes 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
432df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
433df8bae1dSRodney W. Grimes }
434df8bae1dSRodney W. Grimes 
435df8bae1dSRodney W. Grimes /*
436df8bae1dSRodney W. Grimes  * Flush an interface queue.
437df8bae1dSRodney W. Grimes  */
4383bda9f9bSPoul-Henning Kamp static void
439df8bae1dSRodney W. Grimes if_qflush(ifq)
440df8bae1dSRodney W. Grimes 	register struct ifqueue *ifq;
441df8bae1dSRodney W. Grimes {
442df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
443df8bae1dSRodney W. Grimes 
444df8bae1dSRodney W. Grimes 	n = ifq->ifq_head;
4459448326fSPoul-Henning Kamp 	while ((m = n) != 0) {
446df8bae1dSRodney W. Grimes 		n = m->m_act;
447df8bae1dSRodney W. Grimes 		m_freem(m);
448df8bae1dSRodney W. Grimes 	}
449df8bae1dSRodney W. Grimes 	ifq->ifq_head = 0;
450df8bae1dSRodney W. Grimes 	ifq->ifq_tail = 0;
451df8bae1dSRodney W. Grimes 	ifq->ifq_len = 0;
452df8bae1dSRodney W. Grimes }
453df8bae1dSRodney W. Grimes 
454df8bae1dSRodney W. Grimes /*
455df8bae1dSRodney W. Grimes  * Handle interface watchdog timer routines.  Called
456df8bae1dSRodney W. Grimes  * from softclock, we decrement timers (if set) and
457df8bae1dSRodney W. Grimes  * call the appropriate interface routine on expiration.
458df8bae1dSRodney W. Grimes  */
4593bda9f9bSPoul-Henning Kamp static void
460df8bae1dSRodney W. Grimes if_slowtimo(arg)
461df8bae1dSRodney W. Grimes 	void *arg;
462df8bae1dSRodney W. Grimes {
463df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
464df8bae1dSRodney W. Grimes 	int s = splimp();
465df8bae1dSRodney W. Grimes 
46629412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
467df8bae1dSRodney W. Grimes 		if (ifp->if_timer == 0 || --ifp->if_timer)
468df8bae1dSRodney W. Grimes 			continue;
469df8bae1dSRodney W. Grimes 		if (ifp->if_watchdog)
4704a5f1499SDavid Greenman 			(*ifp->if_watchdog)(ifp);
471df8bae1dSRodney W. Grimes 	}
472df8bae1dSRodney W. Grimes 	splx(s);
473df8bae1dSRodney W. Grimes 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
474df8bae1dSRodney W. Grimes }
475df8bae1dSRodney W. Grimes 
476df8bae1dSRodney W. Grimes /*
477df8bae1dSRodney W. Grimes  * Map interface name to
478df8bae1dSRodney W. Grimes  * interface structure pointer.
479df8bae1dSRodney W. Grimes  */
480df8bae1dSRodney W. Grimes struct ifnet *
481df8bae1dSRodney W. Grimes ifunit(name)
482df8bae1dSRodney W. Grimes 	register char *name;
483df8bae1dSRodney W. Grimes {
484df8bae1dSRodney W. Grimes 	register char *cp;
485df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
486df8bae1dSRodney W. Grimes 	int unit;
487df8bae1dSRodney W. Grimes 	unsigned len;
488df8bae1dSRodney W. Grimes 	char *ep, c;
489df8bae1dSRodney W. Grimes 
490df8bae1dSRodney W. Grimes 	for (cp = name; cp < name + IFNAMSIZ && *cp; cp++)
491df8bae1dSRodney W. Grimes 		if (*cp >= '0' && *cp <= '9')
492df8bae1dSRodney W. Grimes 			break;
493df8bae1dSRodney W. Grimes 	if (*cp == '\0' || cp == name + IFNAMSIZ)
494df8bae1dSRodney W. Grimes 		return ((struct ifnet *)0);
495df8bae1dSRodney W. Grimes 	/*
496df8bae1dSRodney W. Grimes 	 * Save first char of unit, and pointer to it,
497df8bae1dSRodney W. Grimes 	 * so we can put a null there to avoid matching
498df8bae1dSRodney W. Grimes 	 * initial substrings of interface names.
499df8bae1dSRodney W. Grimes 	 */
500df8bae1dSRodney W. Grimes 	len = cp - name + 1;
501df8bae1dSRodney W. Grimes 	c = *cp;
502df8bae1dSRodney W. Grimes 	ep = cp;
503df8bae1dSRodney W. Grimes 	for (unit = 0; *cp >= '0' && *cp <= '9'; )
504df8bae1dSRodney W. Grimes 		unit = unit * 10 + *cp++ - '0';
50558639ed3SGarrett Wollman 	if (*cp != '\0')
50658639ed3SGarrett Wollman 		return 0;	/* no trailing garbage allowed */
507df8bae1dSRodney W. Grimes 	*ep = 0;
50829412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
509df8bae1dSRodney W. Grimes 		if (bcmp(ifp->if_name, name, len))
510df8bae1dSRodney W. Grimes 			continue;
511df8bae1dSRodney W. Grimes 		if (unit == ifp->if_unit)
512df8bae1dSRodney W. Grimes 			break;
513df8bae1dSRodney W. Grimes 	}
514df8bae1dSRodney W. Grimes 	*ep = c;
515df8bae1dSRodney W. Grimes 	return (ifp);
516df8bae1dSRodney W. Grimes }
517df8bae1dSRodney W. Grimes 
518df8bae1dSRodney W. Grimes /*
519df8bae1dSRodney W. Grimes  * Interface ioctls.
520df8bae1dSRodney W. Grimes  */
521df8bae1dSRodney W. Grimes int
522df8bae1dSRodney W. Grimes ifioctl(so, cmd, data, p)
523df8bae1dSRodney W. Grimes 	struct socket *so;
524df8bae1dSRodney W. Grimes 	int cmd;
525df8bae1dSRodney W. Grimes 	caddr_t data;
526df8bae1dSRodney W. Grimes 	struct proc *p;
527df8bae1dSRodney W. Grimes {
528df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
529df8bae1dSRodney W. Grimes 	register struct ifreq *ifr;
530df8bae1dSRodney W. Grimes 	int error;
531df8bae1dSRodney W. Grimes 
532df8bae1dSRodney W. Grimes 	switch (cmd) {
533df8bae1dSRodney W. Grimes 
534df8bae1dSRodney W. Grimes 	case SIOCGIFCONF:
535df8bae1dSRodney W. Grimes 	case OSIOCGIFCONF:
536df8bae1dSRodney W. Grimes 		return (ifconf(cmd, data));
537df8bae1dSRodney W. Grimes 	}
538df8bae1dSRodney W. Grimes 	ifr = (struct ifreq *)data;
539df8bae1dSRodney W. Grimes 	ifp = ifunit(ifr->ifr_name);
540df8bae1dSRodney W. Grimes 	if (ifp == 0)
541df8bae1dSRodney W. Grimes 		return (ENXIO);
542df8bae1dSRodney W. Grimes 	switch (cmd) {
543df8bae1dSRodney W. Grimes 
544df8bae1dSRodney W. Grimes 	case SIOCGIFFLAGS:
545df8bae1dSRodney W. Grimes 		ifr->ifr_flags = ifp->if_flags;
546df8bae1dSRodney W. Grimes 		break;
547df8bae1dSRodney W. Grimes 
548df8bae1dSRodney W. Grimes 	case SIOCGIFMETRIC:
549df8bae1dSRodney W. Grimes 		ifr->ifr_metric = ifp->if_metric;
550df8bae1dSRodney W. Grimes 		break;
551df8bae1dSRodney W. Grimes 
552a7028af7SDavid Greenman 	case SIOCGIFMTU:
553a7028af7SDavid Greenman 		ifr->ifr_mtu = ifp->if_mtu;
554a7028af7SDavid Greenman 		break;
555a7028af7SDavid Greenman 
556074c4a4eSGarrett Wollman 	case SIOCGIFPHYS:
557074c4a4eSGarrett Wollman 		ifr->ifr_phys = ifp->if_physical;
558074c4a4eSGarrett Wollman 		break;
559074c4a4eSGarrett Wollman 
560df8bae1dSRodney W. Grimes 	case SIOCSIFFLAGS:
5619448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5629448326fSPoul-Henning Kamp 		if (error)
563df8bae1dSRodney W. Grimes 			return (error);
564df8bae1dSRodney W. Grimes 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
565df8bae1dSRodney W. Grimes 			int s = splimp();
566df8bae1dSRodney W. Grimes 			if_down(ifp);
567df8bae1dSRodney W. Grimes 			splx(s);
568df8bae1dSRodney W. Grimes 		}
569df8bae1dSRodney W. Grimes 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
570df8bae1dSRodney W. Grimes 			int s = splimp();
571df8bae1dSRodney W. Grimes 			if_up(ifp);
572df8bae1dSRodney W. Grimes 			splx(s);
573df8bae1dSRodney W. Grimes 		}
574df8bae1dSRodney W. Grimes 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
575df8bae1dSRodney W. Grimes 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
576df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl)
577df8bae1dSRodney W. Grimes 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
578a614bfe0SGary Palmer 		microtime(&ifp->if_lastchange);
579df8bae1dSRodney W. Grimes 		break;
580df8bae1dSRodney W. Grimes 
581df8bae1dSRodney W. Grimes 	case SIOCSIFMETRIC:
5829448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5839448326fSPoul-Henning Kamp 		if (error)
584df8bae1dSRodney W. Grimes 			return (error);
585df8bae1dSRodney W. Grimes 		ifp->if_metric = ifr->ifr_metric;
586a614bfe0SGary Palmer 		microtime(&ifp->if_lastchange);
587df8bae1dSRodney W. Grimes 		break;
588df8bae1dSRodney W. Grimes 
589074c4a4eSGarrett Wollman 	case SIOCSIFPHYS:
590074c4a4eSGarrett Wollman 		error = suser(p->p_ucred, &p->p_acflag);
591e39a0280SGary Palmer 		if (error)
592e39a0280SGary Palmer 			return error;
593e39a0280SGary Palmer 		if (!ifp->if_ioctl)
594e39a0280SGary Palmer 		        return EOPNOTSUPP;
595e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
596e39a0280SGary Palmer 		if (error == 0)
597a614bfe0SGary Palmer 			microtime(&ifp->if_lastchange);
598e39a0280SGary Palmer 		return(error);
599074c4a4eSGarrett Wollman 
600a7028af7SDavid Greenman 	case SIOCSIFMTU:
6019448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
6029448326fSPoul-Henning Kamp 		if (error)
603a7028af7SDavid Greenman 			return (error);
604a7028af7SDavid Greenman 		if (ifp->if_ioctl == NULL)
605a7028af7SDavid Greenman 			return (EOPNOTSUPP);
60666025569SDavid Greenman 		/*
60766025569SDavid Greenman 		 * 72 was chosen below because it is the size of a TCP/IP
60866025569SDavid Greenman 		 * header (40) + the minimum mss (32).
60966025569SDavid Greenman 		 */
61066025569SDavid Greenman 		if (ifr->ifr_mtu < 72 || ifr->ifr_mtu > 65535)
61175ee03cbSDavid Greenman 			return (EINVAL);
612e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
613e39a0280SGary Palmer 		if (error == 0)
614a614bfe0SGary Palmer 			microtime(&ifp->if_lastchange);
615e39a0280SGary Palmer 		return(error);
616a7028af7SDavid Greenman 
617df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
618df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
6199448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
6209448326fSPoul-Henning Kamp 		if (error)
621df8bae1dSRodney W. Grimes 			return (error);
622477180fbSGarrett Wollman 
623477180fbSGarrett Wollman 		/* Don't allow group membership on non-multicast interfaces. */
624477180fbSGarrett Wollman 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
625477180fbSGarrett Wollman 			return EOPNOTSUPP;
626477180fbSGarrett Wollman 
627477180fbSGarrett Wollman 		/* Don't let users screw up protocols' entries. */
628477180fbSGarrett Wollman 		if (ifr->ifr_addr.sa_family != AF_LINK)
629477180fbSGarrett Wollman 			return EINVAL;
630477180fbSGarrett Wollman 
631477180fbSGarrett Wollman 		if (cmd == SIOCADDMULTI) {
632477180fbSGarrett Wollman 			struct ifmultiaddr *ifma;
633477180fbSGarrett Wollman 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
634477180fbSGarrett Wollman 		} else {
635477180fbSGarrett Wollman 			error = if_delmulti(ifp, &ifr->ifr_addr);
636477180fbSGarrett Wollman 		}
637e39a0280SGary Palmer 		if (error == 0)
638a614bfe0SGary Palmer 			microtime(&ifp->if_lastchange);
639477180fbSGarrett Wollman 		return error;
640df8bae1dSRodney W. Grimes 
641a912e453SPeter Wemm         case SIOCSIFMEDIA:
642d7189ec6SJoerg Wunsch 	case SIOCSIFGENERIC:
643a912e453SPeter Wemm 		error = suser(p->p_ucred, &p->p_acflag);
644a912e453SPeter Wemm 		if (error)
645a912e453SPeter Wemm 			return (error);
646a912e453SPeter Wemm 		if (ifp->if_ioctl == 0)
647a912e453SPeter Wemm 			return (EOPNOTSUPP);
648a912e453SPeter Wemm 		error = (*ifp->if_ioctl)(ifp, cmd, data);
649a912e453SPeter Wemm 		if (error == 0)
650a912e453SPeter Wemm 			microtime(&ifp->if_lastchange);
651a912e453SPeter Wemm 		return error;
652a912e453SPeter Wemm 
653a912e453SPeter Wemm 	case SIOCGIFMEDIA:
654d7189ec6SJoerg Wunsch 	case SIOCGIFGENERIC:
655a912e453SPeter Wemm 		if (ifp->if_ioctl == 0)
656a912e453SPeter Wemm 			return (EOPNOTSUPP);
657a912e453SPeter Wemm 		return ((*ifp->if_ioctl)(ifp, cmd, data));
658a912e453SPeter Wemm 
659df8bae1dSRodney W. Grimes 	default:
660df8bae1dSRodney W. Grimes 		if (so->so_proto == 0)
661df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
662df8bae1dSRodney W. Grimes #ifndef COMPAT_43
6632c37256eSGarrett Wollman 		return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
6642c37256eSGarrett Wollman 								 data,
665bc6d9b80SJoerg Wunsch 								 ifp, p));
666df8bae1dSRodney W. Grimes #else
667df8bae1dSRodney W. Grimes 	    {
668df8bae1dSRodney W. Grimes 		int ocmd = cmd;
669df8bae1dSRodney W. Grimes 
670df8bae1dSRodney W. Grimes 		switch (cmd) {
671df8bae1dSRodney W. Grimes 
672df8bae1dSRodney W. Grimes 		case SIOCSIFDSTADDR:
673df8bae1dSRodney W. Grimes 		case SIOCSIFADDR:
674df8bae1dSRodney W. Grimes 		case SIOCSIFBRDADDR:
675df8bae1dSRodney W. Grimes 		case SIOCSIFNETMASK:
676df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN
677df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_family == 0 &&
678df8bae1dSRodney W. Grimes 			    ifr->ifr_addr.sa_len < 16) {
679df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
680df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
681df8bae1dSRodney W. Grimes 			}
682df8bae1dSRodney W. Grimes #else
683df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_len == 0)
684df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
685df8bae1dSRodney W. Grimes #endif
686df8bae1dSRodney W. Grimes 			break;
687df8bae1dSRodney W. Grimes 
688df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
689df8bae1dSRodney W. Grimes 			cmd = SIOCGIFADDR;
690df8bae1dSRodney W. Grimes 			break;
691df8bae1dSRodney W. Grimes 
692df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
693df8bae1dSRodney W. Grimes 			cmd = SIOCGIFDSTADDR;
694df8bae1dSRodney W. Grimes 			break;
695df8bae1dSRodney W. Grimes 
696df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
697df8bae1dSRodney W. Grimes 			cmd = SIOCGIFBRDADDR;
698df8bae1dSRodney W. Grimes 			break;
699df8bae1dSRodney W. Grimes 
700df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
701df8bae1dSRodney W. Grimes 			cmd = SIOCGIFNETMASK;
702df8bae1dSRodney W. Grimes 		}
7032c37256eSGarrett Wollman 		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
7042c37256eSGarrett Wollman 								   cmd,
7052c37256eSGarrett Wollman 								   data,
706a29f300eSGarrett Wollman 								   ifp, p));
707df8bae1dSRodney W. Grimes 		switch (ocmd) {
708df8bae1dSRodney W. Grimes 
709df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
710df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
711df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
712df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
713df8bae1dSRodney W. Grimes 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
714df8bae1dSRodney W. Grimes 		}
715df8bae1dSRodney W. Grimes 		return (error);
716df8bae1dSRodney W. Grimes 
717df8bae1dSRodney W. Grimes 	    }
718df8bae1dSRodney W. Grimes #endif
719df8bae1dSRodney W. Grimes 	}
720df8bae1dSRodney W. Grimes 	return (0);
721df8bae1dSRodney W. Grimes }
722df8bae1dSRodney W. Grimes 
723df8bae1dSRodney W. Grimes /*
724963e4c2aSGarrett Wollman  * Set/clear promiscuous mode on interface ifp based on the truth value
725963e4c2aSGarrett Wollman  * of pswitch.  The calls are reference counted so that only the first
726963e4c2aSGarrett Wollman  * "on" request actually has an effect, as does the final "off" request.
727963e4c2aSGarrett Wollman  * Results are undefined if the "off" and "on" requests are not matched.
728963e4c2aSGarrett Wollman  */
729963e4c2aSGarrett Wollman int
730963e4c2aSGarrett Wollman ifpromisc(ifp, pswitch)
731963e4c2aSGarrett Wollman 	struct ifnet *ifp;
732963e4c2aSGarrett Wollman 	int pswitch;
733963e4c2aSGarrett Wollman {
734963e4c2aSGarrett Wollman 	struct ifreq ifr;
7354a26224cSGarrett Wollman 	int error;
736963e4c2aSGarrett Wollman 
737963e4c2aSGarrett Wollman 	if (pswitch) {
738963e4c2aSGarrett Wollman 		/*
739963e4c2aSGarrett Wollman 		 * If the device is not configured up, we cannot put it in
740963e4c2aSGarrett Wollman 		 * promiscuous mode.
741963e4c2aSGarrett Wollman 		 */
742963e4c2aSGarrett Wollman 		if ((ifp->if_flags & IFF_UP) == 0)
743963e4c2aSGarrett Wollman 			return (ENETDOWN);
744963e4c2aSGarrett Wollman 		if (ifp->if_pcount++ != 0)
745963e4c2aSGarrett Wollman 			return (0);
746963e4c2aSGarrett Wollman 		ifp->if_flags |= IFF_PROMISC;
747e9a30d00SGarrett Wollman 		log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
748963e4c2aSGarrett Wollman 		    ifp->if_name, ifp->if_unit);
749963e4c2aSGarrett Wollman 	} else {
750963e4c2aSGarrett Wollman 		if (--ifp->if_pcount > 0)
751963e4c2aSGarrett Wollman 			return (0);
752963e4c2aSGarrett Wollman 		ifp->if_flags &= ~IFF_PROMISC;
753963e4c2aSGarrett Wollman 	}
754963e4c2aSGarrett Wollman 	ifr.ifr_flags = ifp->if_flags;
7554a26224cSGarrett Wollman 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
7564a26224cSGarrett Wollman 	if (error == 0)
7574a26224cSGarrett Wollman 		rt_ifmsg(ifp);
7584a26224cSGarrett Wollman 	return error;
759963e4c2aSGarrett Wollman }
760963e4c2aSGarrett Wollman 
761963e4c2aSGarrett Wollman /*
762df8bae1dSRodney W. Grimes  * Return interface configuration
763df8bae1dSRodney W. Grimes  * of system.  List may be used
764df8bae1dSRodney W. Grimes  * in later ioctl's (above) to get
765df8bae1dSRodney W. Grimes  * other information.
766df8bae1dSRodney W. Grimes  */
767df8bae1dSRodney W. Grimes /*ARGSUSED*/
7683bda9f9bSPoul-Henning Kamp static int
769df8bae1dSRodney W. Grimes ifconf(cmd, data)
770df8bae1dSRodney W. Grimes 	int cmd;
771df8bae1dSRodney W. Grimes 	caddr_t data;
772df8bae1dSRodney W. Grimes {
773df8bae1dSRodney W. Grimes 	register struct ifconf *ifc = (struct ifconf *)data;
77429412182SGarrett Wollman 	register struct ifnet *ifp = ifnet.tqh_first;
775df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
776df8bae1dSRodney W. Grimes 	struct ifreq ifr, *ifrp;
777df8bae1dSRodney W. Grimes 	int space = ifc->ifc_len, error = 0;
778df8bae1dSRodney W. Grimes 
779df8bae1dSRodney W. Grimes 	ifrp = ifc->ifc_req;
78029412182SGarrett Wollman 	for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) {
7811ce9bf88SPoul-Henning Kamp 		char workbuf[64];
7821ce9bf88SPoul-Henning Kamp 		int ifnlen;
7832624cf89SGarrett Wollman 
7841ce9bf88SPoul-Henning Kamp 		ifnlen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
7851ce9bf88SPoul-Henning Kamp 		if(ifnlen + 1 > sizeof ifr.ifr_name) {
7862624cf89SGarrett Wollman 			error = ENAMETOOLONG;
7872624cf89SGarrett Wollman 		} else {
7881ce9bf88SPoul-Henning Kamp 			strcpy(ifr.ifr_name, workbuf);
7892624cf89SGarrett Wollman 		}
7902624cf89SGarrett Wollman 
79159562606SGarrett Wollman 		if ((ifa = ifp->if_addrhead.tqh_first) == 0) {
792df8bae1dSRodney W. Grimes 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
793df8bae1dSRodney W. Grimes 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
794df8bae1dSRodney W. Grimes 			    sizeof (ifr));
795df8bae1dSRodney W. Grimes 			if (error)
796df8bae1dSRodney W. Grimes 				break;
797df8bae1dSRodney W. Grimes 			space -= sizeof (ifr), ifrp++;
798df8bae1dSRodney W. Grimes 		} else
79959562606SGarrett Wollman 		    for ( ; space > sizeof (ifr) && ifa;
80059562606SGarrett Wollman 			 ifa = ifa->ifa_link.tqe_next) {
801df8bae1dSRodney W. Grimes 			register struct sockaddr *sa = ifa->ifa_addr;
802df8bae1dSRodney W. Grimes #ifdef COMPAT_43
803df8bae1dSRodney W. Grimes 			if (cmd == OSIOCGIFCONF) {
804df8bae1dSRodney W. Grimes 				struct osockaddr *osa =
805df8bae1dSRodney W. Grimes 					 (struct osockaddr *)&ifr.ifr_addr;
806df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
807df8bae1dSRodney W. Grimes 				osa->sa_family = sa->sa_family;
808df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
809df8bae1dSRodney W. Grimes 						sizeof (ifr));
810df8bae1dSRodney W. Grimes 				ifrp++;
811df8bae1dSRodney W. Grimes 			} else
812df8bae1dSRodney W. Grimes #endif
813df8bae1dSRodney W. Grimes 			if (sa->sa_len <= sizeof(*sa)) {
814df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
815df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
816df8bae1dSRodney W. Grimes 						sizeof (ifr));
817df8bae1dSRodney W. Grimes 				ifrp++;
818df8bae1dSRodney W. Grimes 			} else {
819df8bae1dSRodney W. Grimes 				space -= sa->sa_len - sizeof(*sa);
820df8bae1dSRodney W. Grimes 				if (space < sizeof (ifr))
821df8bae1dSRodney W. Grimes 					break;
822df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
823df8bae1dSRodney W. Grimes 						sizeof (ifr.ifr_name));
824df8bae1dSRodney W. Grimes 				if (error == 0)
825df8bae1dSRodney W. Grimes 				    error = copyout((caddr_t)sa,
826df8bae1dSRodney W. Grimes 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
827df8bae1dSRodney W. Grimes 				ifrp = (struct ifreq *)
828df8bae1dSRodney W. Grimes 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
829df8bae1dSRodney W. Grimes 			}
830df8bae1dSRodney W. Grimes 			if (error)
831df8bae1dSRodney W. Grimes 				break;
832df8bae1dSRodney W. Grimes 			space -= sizeof (ifr);
833df8bae1dSRodney W. Grimes 		}
834df8bae1dSRodney W. Grimes 	}
835df8bae1dSRodney W. Grimes 	ifc->ifc_len -= space;
836df8bae1dSRodney W. Grimes 	return (error);
837df8bae1dSRodney W. Grimes }
838df8bae1dSRodney W. Grimes 
8391158dfb7SGarrett Wollman /*
8401158dfb7SGarrett Wollman  * Just like if_promisc(), but for all-multicast-reception mode.
8411158dfb7SGarrett Wollman  */
8421158dfb7SGarrett Wollman int
8431158dfb7SGarrett Wollman if_allmulti(ifp, onswitch)
8441158dfb7SGarrett Wollman 	struct ifnet *ifp;
8451158dfb7SGarrett Wollman 	int onswitch;
8461158dfb7SGarrett Wollman {
8471158dfb7SGarrett Wollman 	int error = 0;
8481158dfb7SGarrett Wollman 	int s = splimp();
8491158dfb7SGarrett Wollman 
8501158dfb7SGarrett Wollman 	if (onswitch) {
8511158dfb7SGarrett Wollman 		if (ifp->if_amcount++ == 0) {
8521158dfb7SGarrett Wollman 			ifp->if_flags |= IFF_ALLMULTI;
8531158dfb7SGarrett Wollman 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
8541158dfb7SGarrett Wollman 		}
8551158dfb7SGarrett Wollman 	} else {
8561158dfb7SGarrett Wollman 		if (ifp->if_amcount > 1) {
8571158dfb7SGarrett Wollman 			ifp->if_amcount--;
8581158dfb7SGarrett Wollman 		} else {
8591158dfb7SGarrett Wollman 			ifp->if_amcount = 0;
8601158dfb7SGarrett Wollman 			ifp->if_flags &= ~IFF_ALLMULTI;
8611158dfb7SGarrett Wollman 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
8621158dfb7SGarrett Wollman 		}
8631158dfb7SGarrett Wollman 	}
8641158dfb7SGarrett Wollman 	splx(s);
8654a26224cSGarrett Wollman 
8664a26224cSGarrett Wollman 	if (error == 0)
8674a26224cSGarrett Wollman 		rt_ifmsg(ifp);
8681158dfb7SGarrett Wollman 	return error;
8691158dfb7SGarrett Wollman }
8701158dfb7SGarrett Wollman 
8711158dfb7SGarrett Wollman /*
8721158dfb7SGarrett Wollman  * Add a multicast listenership to the interface in question.
8731158dfb7SGarrett Wollman  * The link layer provides a routine which converts
8741158dfb7SGarrett Wollman  */
8751158dfb7SGarrett Wollman int
876373f88edSGarrett Wollman if_addmulti(ifp, sa, retifma)
8771158dfb7SGarrett Wollman 	struct ifnet *ifp;	/* interface to manipulate */
8781158dfb7SGarrett Wollman 	struct sockaddr *sa;	/* address to add */
879b2053118SGarrett Wollman 	struct ifmultiaddr **retifma;
8801158dfb7SGarrett Wollman {
8811158dfb7SGarrett Wollman 	struct sockaddr *llsa, *dupsa;
8821158dfb7SGarrett Wollman 	int error, s;
8831158dfb7SGarrett Wollman 	struct ifmultiaddr *ifma;
8841158dfb7SGarrett Wollman 
88557af7922SJulian Elischer 	/*
88657af7922SJulian Elischer 	 * If the matching multicast address already exists
88757af7922SJulian Elischer 	 * then don't add a new one, just add a reference
88857af7922SJulian Elischer 	 */
8891158dfb7SGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
8901158dfb7SGarrett Wollman 	     ifma = ifma->ifma_link.le_next) {
89157af7922SJulian Elischer 		if (equal(sa, ifma->ifma_addr)) {
8921158dfb7SGarrett Wollman 			ifma->ifma_refcount++;
89357af7922SJulian Elischer 			if (retifma)
89457af7922SJulian Elischer 				*retifma = ifma;
8951158dfb7SGarrett Wollman 			return 0;
8961158dfb7SGarrett Wollman 		}
89757af7922SJulian Elischer 	}
8981158dfb7SGarrett Wollman 
8991158dfb7SGarrett Wollman 	/*
9001158dfb7SGarrett Wollman 	 * Give the link layer a chance to accept/reject it, and also
9011158dfb7SGarrett Wollman 	 * find out which AF_LINK address this maps to, if it isn't one
9021158dfb7SGarrett Wollman 	 * already.
9031158dfb7SGarrett Wollman 	 */
9041158dfb7SGarrett Wollman 	if (ifp->if_resolvemulti) {
9051158dfb7SGarrett Wollman 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
9061158dfb7SGarrett Wollman 		if (error) return error;
9071158dfb7SGarrett Wollman 	} else {
9081158dfb7SGarrett Wollman 		llsa = 0;
9091158dfb7SGarrett Wollman 	}
9101158dfb7SGarrett Wollman 
9111158dfb7SGarrett Wollman 	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
9121158dfb7SGarrett Wollman 	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
9131158dfb7SGarrett Wollman 	bcopy(sa, dupsa, sa->sa_len);
9141158dfb7SGarrett Wollman 
9151158dfb7SGarrett Wollman 	ifma->ifma_addr = dupsa;
9161158dfb7SGarrett Wollman 	ifma->ifma_lladdr = llsa;
9171158dfb7SGarrett Wollman 	ifma->ifma_ifp = ifp;
9181158dfb7SGarrett Wollman 	ifma->ifma_refcount = 1;
919373f88edSGarrett Wollman 	ifma->ifma_protospec = 0;
920477180fbSGarrett Wollman 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
921373f88edSGarrett Wollman 
9221158dfb7SGarrett Wollman 	/*
9231158dfb7SGarrett Wollman 	 * Some network interfaces can scan the address list at
9241158dfb7SGarrett Wollman 	 * interrupt time; lock them out.
9251158dfb7SGarrett Wollman 	 */
9261158dfb7SGarrett Wollman 	s = splimp();
9271158dfb7SGarrett Wollman 	LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
9281158dfb7SGarrett Wollman 	splx(s);
929373f88edSGarrett Wollman 	*retifma = ifma;
9301158dfb7SGarrett Wollman 
9311158dfb7SGarrett Wollman 	if (llsa != 0) {
9321158dfb7SGarrett Wollman 		for (ifma = ifp->if_multiaddrs.lh_first; ifma;
9331158dfb7SGarrett Wollman 		     ifma = ifma->ifma_link.le_next) {
9341158dfb7SGarrett Wollman 			if (equal(ifma->ifma_addr, llsa))
9351158dfb7SGarrett Wollman 				break;
9361158dfb7SGarrett Wollman 		}
9371158dfb7SGarrett Wollman 		if (ifma) {
9381158dfb7SGarrett Wollman 			ifma->ifma_refcount++;
9391158dfb7SGarrett Wollman 		} else {
9401158dfb7SGarrett Wollman 			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
9411158dfb7SGarrett Wollman 			       M_IFMADDR, M_WAITOK);
942477180fbSGarrett Wollman 			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
943477180fbSGarrett Wollman 			       M_IFMADDR, M_WAITOK);
944477180fbSGarrett Wollman 			bcopy(llsa, dupsa, llsa->sa_len);
945477180fbSGarrett Wollman 			ifma->ifma_addr = dupsa;
9461158dfb7SGarrett Wollman 			ifma->ifma_ifp = ifp;
9471158dfb7SGarrett Wollman 			ifma->ifma_refcount = 1;
9481158dfb7SGarrett Wollman 			s = splimp();
9491158dfb7SGarrett Wollman 			LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
9501158dfb7SGarrett Wollman 			splx(s);
9511158dfb7SGarrett Wollman 		}
95257af7922SJulian Elischer 	}
9531158dfb7SGarrett Wollman 	/*
9541158dfb7SGarrett Wollman 	 * We are certain we have added something, so call down to the
9551158dfb7SGarrett Wollman 	 * interface to let them know about it.
9561158dfb7SGarrett Wollman 	 */
9571158dfb7SGarrett Wollman 	s = splimp();
9581158dfb7SGarrett Wollman 	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
9591158dfb7SGarrett Wollman 	splx(s);
9601158dfb7SGarrett Wollman 
9611158dfb7SGarrett Wollman 	return 0;
9621158dfb7SGarrett Wollman }
9631158dfb7SGarrett Wollman 
9641158dfb7SGarrett Wollman /*
9651158dfb7SGarrett Wollman  * Remove a reference to a multicast address on this interface.  Yell
9661158dfb7SGarrett Wollman  * if the request does not match an existing membership.
9671158dfb7SGarrett Wollman  */
9681158dfb7SGarrett Wollman int
9691158dfb7SGarrett Wollman if_delmulti(ifp, sa)
9701158dfb7SGarrett Wollman 	struct ifnet *ifp;
9711158dfb7SGarrett Wollman 	struct sockaddr *sa;
9721158dfb7SGarrett Wollman {
9731158dfb7SGarrett Wollman 	struct ifmultiaddr *ifma;
9741158dfb7SGarrett Wollman 	int s;
9751158dfb7SGarrett Wollman 
9761158dfb7SGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
9771158dfb7SGarrett Wollman 	     ifma = ifma->ifma_link.le_next)
9781158dfb7SGarrett Wollman 		if (equal(sa, ifma->ifma_addr))
9791158dfb7SGarrett Wollman 			break;
9801158dfb7SGarrett Wollman 	if (ifma == 0)
9811158dfb7SGarrett Wollman 		return ENOENT;
9821158dfb7SGarrett Wollman 
9831158dfb7SGarrett Wollman 	if (ifma->ifma_refcount > 1) {
9841158dfb7SGarrett Wollman 		ifma->ifma_refcount--;
9851158dfb7SGarrett Wollman 		return 0;
9861158dfb7SGarrett Wollman 	}
9871158dfb7SGarrett Wollman 
988477180fbSGarrett Wollman 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
9891158dfb7SGarrett Wollman 	sa = ifma->ifma_lladdr;
9901158dfb7SGarrett Wollman 	s = splimp();
9911158dfb7SGarrett Wollman 	LIST_REMOVE(ifma, ifma_link);
9921158dfb7SGarrett Wollman 	splx(s);
9931158dfb7SGarrett Wollman 	free(ifma->ifma_addr, M_IFMADDR);
9941158dfb7SGarrett Wollman 	free(ifma, M_IFMADDR);
9951158dfb7SGarrett Wollman 	if (sa == 0)
9961158dfb7SGarrett Wollman 		return 0;
9971158dfb7SGarrett Wollman 
9981158dfb7SGarrett Wollman 	/*
9991158dfb7SGarrett Wollman 	 * Now look for the link-layer address which corresponds to
10001158dfb7SGarrett Wollman 	 * this network address.  It had been squirreled away in
10011158dfb7SGarrett Wollman 	 * ifma->ifma_lladdr for this purpose (so we don't have
10021158dfb7SGarrett Wollman 	 * to call ifp->if_resolvemulti() again), and we saved that
10031158dfb7SGarrett Wollman 	 * value in sa above.  If some nasty deleted the
10041158dfb7SGarrett Wollman 	 * link-layer address out from underneath us, we can deal because
10051158dfb7SGarrett Wollman 	 * the address we stored was is not the same as the one which was
10061158dfb7SGarrett Wollman 	 * in the record for the link-layer address.  (So we don't complain
10071158dfb7SGarrett Wollman 	 * in that case.)
10081158dfb7SGarrett Wollman 	 */
10091158dfb7SGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
10101158dfb7SGarrett Wollman 	     ifma = ifma->ifma_link.le_next)
10111158dfb7SGarrett Wollman 		if (equal(sa, ifma->ifma_addr))
10121158dfb7SGarrett Wollman 			break;
10131158dfb7SGarrett Wollman 	if (ifma == 0)
10141158dfb7SGarrett Wollman 		return 0;
10151158dfb7SGarrett Wollman 
10161158dfb7SGarrett Wollman 	if (ifma->ifma_refcount > 1) {
10171158dfb7SGarrett Wollman 		ifma->ifma_refcount--;
10181158dfb7SGarrett Wollman 		return 0;
10191158dfb7SGarrett Wollman 	}
10201158dfb7SGarrett Wollman 
10211158dfb7SGarrett Wollman 	s = splimp();
10221158dfb7SGarrett Wollman 	LIST_REMOVE(ifma, ifma_link);
10231158dfb7SGarrett Wollman 	splx(s);
10241158dfb7SGarrett Wollman 	free(ifma->ifma_addr, M_IFMADDR);
10251158dfb7SGarrett Wollman 	free(sa, M_IFMADDR);
10261158dfb7SGarrett Wollman 	free(ifma, M_IFMADDR);
10271158dfb7SGarrett Wollman 
10281158dfb7SGarrett Wollman 	return 0;
10291158dfb7SGarrett Wollman }
10301158dfb7SGarrett Wollman 
1031373f88edSGarrett Wollman struct ifmultiaddr *
1032373f88edSGarrett Wollman ifmaof_ifpforaddr(sa, ifp)
1033373f88edSGarrett Wollman 	struct sockaddr *sa;
1034373f88edSGarrett Wollman 	struct ifnet *ifp;
1035373f88edSGarrett Wollman {
1036373f88edSGarrett Wollman 	struct ifmultiaddr *ifma;
1037373f88edSGarrett Wollman 
1038373f88edSGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1039373f88edSGarrett Wollman 	     ifma = ifma->ifma_link.le_next)
1040373f88edSGarrett Wollman 		if (equal(ifma->ifma_addr, sa))
1041373f88edSGarrett Wollman 			break;
1042373f88edSGarrett Wollman 
1043373f88edSGarrett Wollman 	return ifma;
1044373f88edSGarrett Wollman }
1045373f88edSGarrett Wollman 
1046602d513cSGarrett Wollman SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
10472c37256eSGarrett Wollman SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
1048