xref: /freebsd/sys/net/if.c (revision 98b9590ef33d4ba936cf41d75e1d29036ab87ca5)
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
3498b9590eSPoul-Henning Kamp  *	$Id: if.c,v 1.57 1997/12/16 17:40:34 eivind Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
375591b823SEivind Eklund #include "opt_compat.h"
385591b823SEivind Eklund 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
404d1d4912SBruce Evans #include <sys/malloc.h>
41df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43df8bae1dSRodney W. Grimes #include <sys/proc.h>
44df8bae1dSRodney W. Grimes #include <sys/socket.h>
45df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
46df8bae1dSRodney W. Grimes #include <sys/protosw.h>
47df8bae1dSRodney W. Grimes #include <sys/kernel.h>
4851a53488SBruce Evans #include <sys/sockio.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>
549448326fSPoul-Henning Kamp #include <net/radix.h>
55df8bae1dSRodney W. Grimes 
562b14f991SJulian Elischer /*
572b14f991SJulian Elischer  * System initialization
582b14f991SJulian Elischer  */
592b14f991SJulian Elischer 
603bda9f9bSPoul-Henning Kamp static int ifconf __P((int, caddr_t));
614590fd3aSDavid Greenman static void ifinit __P((void *));
623bda9f9bSPoul-Henning Kamp static void if_qflush __P((struct ifqueue *));
633bda9f9bSPoul-Henning Kamp static void if_slowtimo __P((void *));
643bda9f9bSPoul-Henning Kamp static void link_rtrequest __P((int, struct rtentry *, struct sockaddr *));
653bda9f9bSPoul-Henning Kamp 
662b14f991SJulian Elischer SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
672b14f991SJulian Elischer 
68a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
69a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
702b14f991SJulian Elischer 
71df8bae1dSRodney W. Grimes int	ifqmaxlen = IFQ_MAXLEN;
7229412182SGarrett Wollman struct	ifnethead ifnet;	/* depend on static init XXX */
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes /*
75df8bae1dSRodney W. Grimes  * Network interface utility routines.
76df8bae1dSRodney W. Grimes  *
77df8bae1dSRodney W. Grimes  * Routines with ifa_ifwith* names take sockaddr *'s as
78df8bae1dSRodney W. Grimes  * parameters.
792b14f991SJulian Elischer  *
802b14f991SJulian Elischer  * This routine assumes that it will be called at splimp() or higher.
81df8bae1dSRodney W. Grimes  */
822b14f991SJulian Elischer /* ARGSUSED*/
83df8bae1dSRodney W. Grimes void
8427501cb6SBruce Evans ifinit(dummy)
8527501cb6SBruce Evans 	void *dummy;
86df8bae1dSRodney W. Grimes {
87df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
88df8bae1dSRodney W. Grimes 
8929412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
90df8bae1dSRodney W. Grimes 		if (ifp->if_snd.ifq_maxlen == 0)
91df8bae1dSRodney W. Grimes 			ifp->if_snd.ifq_maxlen = ifqmaxlen;
92df8bae1dSRodney W. Grimes 	if_slowtimo(0);
93df8bae1dSRodney W. Grimes }
94df8bae1dSRodney W. Grimes 
95bbd17bf8SGarrett Wollman int if_index = 0;
96bbd17bf8SGarrett Wollman struct ifaddr **ifnet_addrs;
973bda9f9bSPoul-Henning Kamp 
98df8bae1dSRodney W. Grimes 
99df8bae1dSRodney W. Grimes /*
100df8bae1dSRodney W. Grimes  * Attach an interface to the
101df8bae1dSRodney W. Grimes  * list of "active" interfaces.
102df8bae1dSRodney W. Grimes  */
103df8bae1dSRodney W. Grimes void
104df8bae1dSRodney W. Grimes if_attach(ifp)
105df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
106df8bae1dSRodney W. Grimes {
107df8bae1dSRodney W. Grimes 	unsigned socksize, ifasize;
1081ce9bf88SPoul-Henning Kamp 	int namelen, masklen;
1091ce9bf88SPoul-Henning Kamp 	char workbuf[64];
110df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
111df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
112df8bae1dSRodney W. Grimes 	static int if_indexlim = 8;
11329412182SGarrett Wollman 	static int inited;
114f23b4c91SGarrett Wollman 
11529412182SGarrett Wollman 	if (!inited) {
11629412182SGarrett Wollman 		TAILQ_INIT(&ifnet);
11729412182SGarrett Wollman 		inited = 1;
11829412182SGarrett Wollman 	}
119df8bae1dSRodney W. Grimes 
12029412182SGarrett Wollman 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
121df8bae1dSRodney W. Grimes 	ifp->if_index = ++if_index;
12259562606SGarrett Wollman 	/*
12359562606SGarrett Wollman 	 * XXX -
12459562606SGarrett Wollman 	 * The old code would work if the interface passed a pre-existing
12559562606SGarrett Wollman 	 * chain of ifaddrs to this code.  We don't trust our callers to
12659562606SGarrett Wollman 	 * properly initialize the tailq, however, so we no longer allow
12759562606SGarrett Wollman 	 * this unlikely case.
12859562606SGarrett Wollman 	 */
12959562606SGarrett Wollman 	TAILQ_INIT(&ifp->if_addrhead);
1301158dfb7SGarrett Wollman 	LIST_INIT(&ifp->if_multiaddrs);
13198b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
132df8bae1dSRodney W. Grimes 	if (ifnet_addrs == 0 || if_index >= if_indexlim) {
133df8bae1dSRodney W. Grimes 		unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
134df8bae1dSRodney W. Grimes 		struct ifaddr **q = (struct ifaddr **)
135df8bae1dSRodney W. Grimes 					malloc(n, M_IFADDR, M_WAITOK);
13673c2ab46SDavid Greenman 		bzero((caddr_t)q, n);
137df8bae1dSRodney W. Grimes 		if (ifnet_addrs) {
138df8bae1dSRodney W. Grimes 			bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
139df8bae1dSRodney W. Grimes 			free((caddr_t)ifnet_addrs, M_IFADDR);
140df8bae1dSRodney W. Grimes 		}
141df8bae1dSRodney W. Grimes 		ifnet_addrs = q;
142df8bae1dSRodney W. Grimes 	}
143df8bae1dSRodney W. Grimes 	/*
144df8bae1dSRodney W. Grimes 	 * create a Link Level name for this device
145df8bae1dSRodney W. Grimes 	 */
1461ce9bf88SPoul-Henning Kamp 	namelen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
147df8bae1dSRodney W. Grimes #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
1481ce9bf88SPoul-Henning Kamp 	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
149df8bae1dSRodney W. Grimes 	socksize = masklen + ifp->if_addrlen;
150df8bae1dSRodney W. Grimes #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
151df8bae1dSRodney W. Grimes 	socksize = ROUNDUP(socksize);
152df8bae1dSRodney W. Grimes 	if (socksize < sizeof(*sdl))
153df8bae1dSRodney W. Grimes 		socksize = sizeof(*sdl);
154df8bae1dSRodney W. Grimes 	ifasize = sizeof(*ifa) + 2 * socksize;
1559448326fSPoul-Henning Kamp 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
1569448326fSPoul-Henning Kamp 	if (ifa) {
157df8bae1dSRodney W. Grimes 		bzero((caddr_t)ifa, ifasize);
158df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(ifa + 1);
159df8bae1dSRodney W. Grimes 		sdl->sdl_len = socksize;
160df8bae1dSRodney W. Grimes 		sdl->sdl_family = AF_LINK;
1611ce9bf88SPoul-Henning Kamp 		bcopy(workbuf, sdl->sdl_data, namelen);
1621ce9bf88SPoul-Henning Kamp 		sdl->sdl_nlen = namelen;
163df8bae1dSRodney W. Grimes 		sdl->sdl_index = ifp->if_index;
164df8bae1dSRodney W. Grimes 		sdl->sdl_type = ifp->if_type;
165df8bae1dSRodney W. Grimes 		ifnet_addrs[if_index - 1] = ifa;
166df8bae1dSRodney W. Grimes 		ifa->ifa_ifp = ifp;
167df8bae1dSRodney W. Grimes 		ifa->ifa_rtrequest = link_rtrequest;
168df8bae1dSRodney W. Grimes 		ifa->ifa_addr = (struct sockaddr *)sdl;
169df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
170df8bae1dSRodney W. Grimes 		ifa->ifa_netmask = (struct sockaddr *)sdl;
171df8bae1dSRodney W. Grimes 		sdl->sdl_len = masklen;
172df8bae1dSRodney W. Grimes 		while (namelen != 0)
173df8bae1dSRodney W. Grimes 			sdl->sdl_data[--namelen] = 0xff;
17459562606SGarrett Wollman 		TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
175df8bae1dSRodney W. Grimes 	}
176df8bae1dSRodney W. Grimes }
177df8bae1dSRodney W. Grimes /*
178df8bae1dSRodney W. Grimes  * Locate an interface based on a complete address.
179df8bae1dSRodney W. Grimes  */
180df8bae1dSRodney W. Grimes /*ARGSUSED*/
181df8bae1dSRodney W. Grimes struct ifaddr *
182df8bae1dSRodney W. Grimes ifa_ifwithaddr(addr)
183df8bae1dSRodney W. Grimes 	register struct sockaddr *addr;
184df8bae1dSRodney W. Grimes {
185df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
186df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
187df8bae1dSRodney W. Grimes 
188df8bae1dSRodney W. Grimes #define	equal(a1, a2) \
189df8bae1dSRodney W. Grimes   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
19029412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
19159562606SGarrett Wollman 	    for (ifa = ifp->if_addrhead.tqh_first; ifa;
19259562606SGarrett Wollman 		 ifa = ifa->ifa_link.tqe_next) {
193df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != addr->sa_family)
194df8bae1dSRodney W. Grimes 			continue;
195df8bae1dSRodney W. Grimes 		if (equal(addr, ifa->ifa_addr))
196df8bae1dSRodney W. Grimes 			return (ifa);
197df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
198df8bae1dSRodney W. Grimes 		    equal(ifa->ifa_broadaddr, addr))
199df8bae1dSRodney W. Grimes 			return (ifa);
200df8bae1dSRodney W. Grimes 	}
201df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
202df8bae1dSRodney W. Grimes }
203df8bae1dSRodney W. Grimes /*
204df8bae1dSRodney W. Grimes  * Locate the point to point interface with a given destination address.
205df8bae1dSRodney W. Grimes  */
206df8bae1dSRodney W. Grimes /*ARGSUSED*/
207df8bae1dSRodney W. Grimes struct ifaddr *
208df8bae1dSRodney W. Grimes ifa_ifwithdstaddr(addr)
209df8bae1dSRodney W. Grimes 	register struct sockaddr *addr;
210df8bae1dSRodney W. Grimes {
211df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
212df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
213df8bae1dSRodney W. Grimes 
21429412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
215df8bae1dSRodney W. Grimes 	    if (ifp->if_flags & IFF_POINTOPOINT)
21659562606SGarrett Wollman 		for (ifa = ifp->if_addrhead.tqh_first; ifa;
21759562606SGarrett Wollman 		     ifa = ifa->ifa_link.tqe_next) {
218df8bae1dSRodney W. Grimes 			if (ifa->ifa_addr->sa_family != addr->sa_family)
219df8bae1dSRodney W. Grimes 				continue;
22055088a1cSDavid Greenman 			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
221df8bae1dSRodney W. Grimes 				return (ifa);
222df8bae1dSRodney W. Grimes 	}
223df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
224df8bae1dSRodney W. Grimes }
225df8bae1dSRodney W. Grimes 
226df8bae1dSRodney W. Grimes /*
227df8bae1dSRodney W. Grimes  * Find an interface on a specific network.  If many, choice
228df8bae1dSRodney W. Grimes  * is most specific found.
229df8bae1dSRodney W. Grimes  */
230df8bae1dSRodney W. Grimes struct ifaddr *
231df8bae1dSRodney W. Grimes ifa_ifwithnet(addr)
232df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
233df8bae1dSRodney W. Grimes {
234df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
235df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
236df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
237df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
238df8bae1dSRodney W. Grimes 	char *addr_data = addr->sa_data, *cplim;
239df8bae1dSRodney W. Grimes 
2407e2a6151SJulian Elischer 	/*
2417e2a6151SJulian Elischer 	 * AF_LINK addresses can be looked up directly by their index number,
2427e2a6151SJulian Elischer 	 * so do that if we can.
2437e2a6151SJulian Elischer 	 */
244df8bae1dSRodney W. Grimes 	if (af == AF_LINK) {
245df8bae1dSRodney W. Grimes 	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
246df8bae1dSRodney W. Grimes 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
247df8bae1dSRodney W. Grimes 		return (ifnet_addrs[sdl->sdl_index - 1]);
248df8bae1dSRodney W. Grimes 	}
2497e2a6151SJulian Elischer 
2507e2a6151SJulian Elischer 	/*
2517e2a6151SJulian Elischer 	 * Scan though each interface, looking for ones that have
2527e2a6151SJulian Elischer 	 * addresses in this address family.
2537e2a6151SJulian Elischer 	 */
25429412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
25559562606SGarrett Wollman 		for (ifa = ifp->if_addrhead.tqh_first; ifa;
25659562606SGarrett Wollman 		     ifa = ifa->ifa_link.tqe_next) {
257df8bae1dSRodney W. Grimes 			register char *cp, *cp2, *cp3;
258df8bae1dSRodney W. Grimes 
259523a02aaSDavid Greenman 			if (ifa->ifa_addr->sa_family != af)
260df8bae1dSRodney W. Grimes next:				continue;
261b2af64fdSDavid Greenman 			if (ifp->if_flags & IFF_POINTOPOINT) {
2627e2a6151SJulian Elischer 				/*
2637e2a6151SJulian Elischer 				 * This is a bit broken as it doesn't
2647e2a6151SJulian Elischer 				 * take into account that the remote end may
2657e2a6151SJulian Elischer 				 * be a single node in the network we are
2667e2a6151SJulian Elischer 				 * looking for.
2677e2a6151SJulian Elischer 				 * The trouble is that we don't know the
2687e2a6151SJulian Elischer 				 * netmask for the remote end.
2697e2a6151SJulian Elischer 				 */
270fcd6781aSGarrett Wollman 				if (ifa->ifa_dstaddr != 0
271fcd6781aSGarrett Wollman 				    && equal(addr, ifa->ifa_dstaddr))
272b2af64fdSDavid Greenman  					return (ifa);
2733740e2adSDavid Greenman 			} else {
2747e2a6151SJulian Elischer 				/*
2757ed8f465SJulian Elischer 				 * if we have a special address handler,
2767ed8f465SJulian Elischer 				 * then use it instead of the generic one.
2777ed8f465SJulian Elischer 				 */
2787ed8f465SJulian Elischer 	          		if (ifa->ifa_claim_addr) {
2797ed8f465SJulian Elischer 					if ((*ifa->ifa_claim_addr)(ifa, addr)) {
2807ed8f465SJulian Elischer 						return (ifa);
2817ed8f465SJulian Elischer 					} else {
2827ed8f465SJulian Elischer 						continue;
2837ed8f465SJulian Elischer 					}
2847ed8f465SJulian Elischer 				}
2857ed8f465SJulian Elischer 
2867ed8f465SJulian Elischer 				/*
2877e2a6151SJulian Elischer 				 * Scan all the bits in the ifa's address.
2887e2a6151SJulian Elischer 				 * If a bit dissagrees with what we are
2897e2a6151SJulian Elischer 				 * looking for, mask it with the netmask
2907e2a6151SJulian Elischer 				 * to see if it really matters.
2917e2a6151SJulian Elischer 				 * (A byte at a time)
2927e2a6151SJulian Elischer 				 */
293523a02aaSDavid Greenman 				if (ifa->ifa_netmask == 0)
294523a02aaSDavid Greenman 					continue;
295df8bae1dSRodney W. Grimes 				cp = addr_data;
296df8bae1dSRodney W. Grimes 				cp2 = ifa->ifa_addr->sa_data;
297df8bae1dSRodney W. Grimes 				cp3 = ifa->ifa_netmask->sa_data;
2987e2a6151SJulian Elischer 				cplim = ifa->ifa_netmask->sa_len
2997e2a6151SJulian Elischer 					+ (char *)ifa->ifa_netmask;
300df8bae1dSRodney W. Grimes 				while (cp3 < cplim)
301df8bae1dSRodney W. Grimes 					if ((*cp++ ^ *cp2++) & *cp3++)
3027e2a6151SJulian Elischer 						goto next; /* next address! */
3037e2a6151SJulian Elischer 				/*
3047e2a6151SJulian Elischer 				 * If the netmask of what we just found
3057e2a6151SJulian Elischer 				 * is more specific than what we had before
3067e2a6151SJulian Elischer 				 * (if we had one) then remember the new one
3077e2a6151SJulian Elischer 				 * before continuing to search
3087e2a6151SJulian Elischer 				 * for an even better one.
3097e2a6151SJulian Elischer 				 */
310df8bae1dSRodney W. Grimes 				if (ifa_maybe == 0 ||
311df8bae1dSRodney W. Grimes 				    rn_refines((caddr_t)ifa->ifa_netmask,
312df8bae1dSRodney W. Grimes 				    (caddr_t)ifa_maybe->ifa_netmask))
313df8bae1dSRodney W. Grimes 					ifa_maybe = ifa;
314df8bae1dSRodney W. Grimes 			}
315b2af64fdSDavid Greenman 		}
316b2af64fdSDavid Greenman 	}
317df8bae1dSRodney W. Grimes 	return (ifa_maybe);
318df8bae1dSRodney W. Grimes }
319df8bae1dSRodney W. Grimes 
320df8bae1dSRodney W. Grimes /*
321df8bae1dSRodney W. Grimes  * Find an interface address specific to an interface best matching
322df8bae1dSRodney W. Grimes  * a given address.
323df8bae1dSRodney W. Grimes  */
324df8bae1dSRodney W. Grimes struct ifaddr *
325df8bae1dSRodney W. Grimes ifaof_ifpforaddr(addr, ifp)
326df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
327df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
328df8bae1dSRodney W. Grimes {
329df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
330df8bae1dSRodney W. Grimes 	register char *cp, *cp2, *cp3;
331df8bae1dSRodney W. Grimes 	register char *cplim;
332df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = 0;
333df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
334df8bae1dSRodney W. Grimes 
335df8bae1dSRodney W. Grimes 	if (af >= AF_MAX)
336df8bae1dSRodney W. Grimes 		return (0);
33759562606SGarrett Wollman 	for (ifa = ifp->if_addrhead.tqh_first; ifa;
33859562606SGarrett Wollman 	     ifa = ifa->ifa_link.tqe_next) {
339df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != af)
340df8bae1dSRodney W. Grimes 			continue;
341381dd1d2SJulian Elischer 		if (ifa_maybe == 0)
342df8bae1dSRodney W. Grimes 			ifa_maybe = ifa;
343df8bae1dSRodney W. Grimes 		if (ifa->ifa_netmask == 0) {
344df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_addr) ||
345df8bae1dSRodney W. Grimes 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
346df8bae1dSRodney W. Grimes 				return (ifa);
347df8bae1dSRodney W. Grimes 			continue;
348df8bae1dSRodney W. Grimes 		}
349b2af64fdSDavid Greenman 		if (ifp->if_flags & IFF_POINTOPOINT) {
350b2af64fdSDavid Greenman 			if (equal(addr, ifa->ifa_dstaddr))
351b2af64fdSDavid Greenman 				return (ifa);
3523740e2adSDavid Greenman 		} else {
353df8bae1dSRodney W. Grimes 			cp = addr->sa_data;
354df8bae1dSRodney W. Grimes 			cp2 = ifa->ifa_addr->sa_data;
355df8bae1dSRodney W. Grimes 			cp3 = ifa->ifa_netmask->sa_data;
356df8bae1dSRodney W. Grimes 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
357df8bae1dSRodney W. Grimes 			for (; cp3 < cplim; cp3++)
358df8bae1dSRodney W. Grimes 				if ((*cp++ ^ *cp2++) & *cp3)
359df8bae1dSRodney W. Grimes 					break;
360df8bae1dSRodney W. Grimes 			if (cp3 == cplim)
361df8bae1dSRodney W. Grimes 				return (ifa);
362df8bae1dSRodney W. Grimes 		}
363b2af64fdSDavid Greenman 	}
364df8bae1dSRodney W. Grimes 	return (ifa_maybe);
365df8bae1dSRodney W. Grimes }
366df8bae1dSRodney W. Grimes 
367df8bae1dSRodney W. Grimes #include <net/route.h>
368df8bae1dSRodney W. Grimes 
369df8bae1dSRodney W. Grimes /*
370df8bae1dSRodney W. Grimes  * Default action when installing a route with a Link Level gateway.
371df8bae1dSRodney W. Grimes  * Lookup an appropriate real ifa to point to.
372df8bae1dSRodney W. Grimes  * This should be moved to /sys/net/link.c eventually.
373df8bae1dSRodney W. Grimes  */
3743bda9f9bSPoul-Henning Kamp static void
375df8bae1dSRodney W. Grimes link_rtrequest(cmd, rt, sa)
376df8bae1dSRodney W. Grimes 	int cmd;
377df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
378df8bae1dSRodney W. Grimes 	struct sockaddr *sa;
379df8bae1dSRodney W. Grimes {
380df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
381df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
382df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
383df8bae1dSRodney W. Grimes 
384df8bae1dSRodney W. Grimes 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
385df8bae1dSRodney W. Grimes 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
386df8bae1dSRodney W. Grimes 		return;
3879448326fSPoul-Henning Kamp 	ifa = ifaof_ifpforaddr(dst, ifp);
3889448326fSPoul-Henning Kamp 	if (ifa) {
389df8bae1dSRodney W. Grimes 		IFAFREE(rt->rt_ifa);
390df8bae1dSRodney W. Grimes 		rt->rt_ifa = ifa;
391df8bae1dSRodney W. Grimes 		ifa->ifa_refcnt++;
392df8bae1dSRodney W. Grimes 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
393df8bae1dSRodney W. Grimes 			ifa->ifa_rtrequest(cmd, rt, sa);
394df8bae1dSRodney W. Grimes 	}
395df8bae1dSRodney W. Grimes }
396df8bae1dSRodney W. Grimes 
397df8bae1dSRodney W. Grimes /*
398df8bae1dSRodney W. Grimes  * Mark an interface down and notify protocols of
399df8bae1dSRodney W. Grimes  * the transition.
400df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
401df8bae1dSRodney W. Grimes  */
402df8bae1dSRodney W. Grimes void
403df8bae1dSRodney W. Grimes if_down(ifp)
404df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
405df8bae1dSRodney W. Grimes {
406df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
407df8bae1dSRodney W. Grimes 
408df8bae1dSRodney W. Grimes 	ifp->if_flags &= ~IFF_UP;
40998b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
41059562606SGarrett Wollman 	for (ifa = ifp->if_addrhead.tqh_first; ifa;
41159562606SGarrett Wollman 	     ifa = ifa->ifa_link.tqe_next)
412df8bae1dSRodney W. Grimes 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
413df8bae1dSRodney W. Grimes 	if_qflush(&ifp->if_snd);
414df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
415df8bae1dSRodney W. Grimes }
416df8bae1dSRodney W. Grimes 
417df8bae1dSRodney W. Grimes /*
418df8bae1dSRodney W. Grimes  * Mark an interface up and notify protocols of
419df8bae1dSRodney W. Grimes  * the transition.
420df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
421df8bae1dSRodney W. Grimes  */
422df8bae1dSRodney W. Grimes void
423df8bae1dSRodney W. Grimes if_up(ifp)
424df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
425df8bae1dSRodney W. Grimes {
426176395b2SGarrett Wollman 	register struct ifaddr *ifa;
427df8bae1dSRodney W. Grimes 
428df8bae1dSRodney W. Grimes 	ifp->if_flags |= IFF_UP;
42998b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
430176395b2SGarrett Wollman 	for (ifa = ifp->if_addrhead.tqh_first; ifa;
431176395b2SGarrett Wollman 	     ifa = ifa->ifa_link.tqe_next)
432df8bae1dSRodney W. Grimes 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
433df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
434df8bae1dSRodney W. Grimes }
435df8bae1dSRodney W. Grimes 
436df8bae1dSRodney W. Grimes /*
437df8bae1dSRodney W. Grimes  * Flush an interface queue.
438df8bae1dSRodney W. Grimes  */
4393bda9f9bSPoul-Henning Kamp static void
440df8bae1dSRodney W. Grimes if_qflush(ifq)
441df8bae1dSRodney W. Grimes 	register struct ifqueue *ifq;
442df8bae1dSRodney W. Grimes {
443df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
444df8bae1dSRodney W. Grimes 
445df8bae1dSRodney W. Grimes 	n = ifq->ifq_head;
4469448326fSPoul-Henning Kamp 	while ((m = n) != 0) {
447df8bae1dSRodney W. Grimes 		n = m->m_act;
448df8bae1dSRodney W. Grimes 		m_freem(m);
449df8bae1dSRodney W. Grimes 	}
450df8bae1dSRodney W. Grimes 	ifq->ifq_head = 0;
451df8bae1dSRodney W. Grimes 	ifq->ifq_tail = 0;
452df8bae1dSRodney W. Grimes 	ifq->ifq_len = 0;
453df8bae1dSRodney W. Grimes }
454df8bae1dSRodney W. Grimes 
455df8bae1dSRodney W. Grimes /*
456df8bae1dSRodney W. Grimes  * Handle interface watchdog timer routines.  Called
457df8bae1dSRodney W. Grimes  * from softclock, we decrement timers (if set) and
458df8bae1dSRodney W. Grimes  * call the appropriate interface routine on expiration.
459df8bae1dSRodney W. Grimes  */
4603bda9f9bSPoul-Henning Kamp static void
461df8bae1dSRodney W. Grimes if_slowtimo(arg)
462df8bae1dSRodney W. Grimes 	void *arg;
463df8bae1dSRodney W. Grimes {
464df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
465df8bae1dSRodney W. Grimes 	int s = splimp();
466df8bae1dSRodney W. Grimes 
46729412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
468df8bae1dSRodney W. Grimes 		if (ifp->if_timer == 0 || --ifp->if_timer)
469df8bae1dSRodney W. Grimes 			continue;
470df8bae1dSRodney W. Grimes 		if (ifp->if_watchdog)
4714a5f1499SDavid Greenman 			(*ifp->if_watchdog)(ifp);
472df8bae1dSRodney W. Grimes 	}
473df8bae1dSRodney W. Grimes 	splx(s);
474df8bae1dSRodney W. Grimes 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
475df8bae1dSRodney W. Grimes }
476df8bae1dSRodney W. Grimes 
477df8bae1dSRodney W. Grimes /*
478df8bae1dSRodney W. Grimes  * Map interface name to
479df8bae1dSRodney W. Grimes  * interface structure pointer.
480df8bae1dSRodney W. Grimes  */
481df8bae1dSRodney W. Grimes struct ifnet *
482df8bae1dSRodney W. Grimes ifunit(name)
483df8bae1dSRodney W. Grimes 	register char *name;
484df8bae1dSRodney W. Grimes {
485df8bae1dSRodney W. Grimes 	register char *cp;
486df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
487df8bae1dSRodney W. Grimes 	int unit;
488df8bae1dSRodney W. Grimes 	unsigned len;
489df8bae1dSRodney W. Grimes 	char *ep, c;
490df8bae1dSRodney W. Grimes 
491df8bae1dSRodney W. Grimes 	for (cp = name; cp < name + IFNAMSIZ && *cp; cp++)
492df8bae1dSRodney W. Grimes 		if (*cp >= '0' && *cp <= '9')
493df8bae1dSRodney W. Grimes 			break;
494df8bae1dSRodney W. Grimes 	if (*cp == '\0' || cp == name + IFNAMSIZ)
495df8bae1dSRodney W. Grimes 		return ((struct ifnet *)0);
496df8bae1dSRodney W. Grimes 	/*
497df8bae1dSRodney W. Grimes 	 * Save first char of unit, and pointer to it,
498df8bae1dSRodney W. Grimes 	 * so we can put a null there to avoid matching
499df8bae1dSRodney W. Grimes 	 * initial substrings of interface names.
500df8bae1dSRodney W. Grimes 	 */
501df8bae1dSRodney W. Grimes 	len = cp - name + 1;
502df8bae1dSRodney W. Grimes 	c = *cp;
503df8bae1dSRodney W. Grimes 	ep = cp;
504df8bae1dSRodney W. Grimes 	for (unit = 0; *cp >= '0' && *cp <= '9'; )
505df8bae1dSRodney W. Grimes 		unit = unit * 10 + *cp++ - '0';
50658639ed3SGarrett Wollman 	if (*cp != '\0')
50758639ed3SGarrett Wollman 		return 0;	/* no trailing garbage allowed */
508df8bae1dSRodney W. Grimes 	*ep = 0;
50929412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
510df8bae1dSRodney W. Grimes 		if (bcmp(ifp->if_name, name, len))
511df8bae1dSRodney W. Grimes 			continue;
512df8bae1dSRodney W. Grimes 		if (unit == ifp->if_unit)
513df8bae1dSRodney W. Grimes 			break;
514df8bae1dSRodney W. Grimes 	}
515df8bae1dSRodney W. Grimes 	*ep = c;
516df8bae1dSRodney W. Grimes 	return (ifp);
517df8bae1dSRodney W. Grimes }
518df8bae1dSRodney W. Grimes 
519df8bae1dSRodney W. Grimes /*
520df8bae1dSRodney W. Grimes  * Interface ioctls.
521df8bae1dSRodney W. Grimes  */
522df8bae1dSRodney W. Grimes int
523df8bae1dSRodney W. Grimes ifioctl(so, cmd, data, p)
524df8bae1dSRodney W. Grimes 	struct socket *so;
525df8bae1dSRodney W. Grimes 	int cmd;
526df8bae1dSRodney W. Grimes 	caddr_t data;
527df8bae1dSRodney W. Grimes 	struct proc *p;
528df8bae1dSRodney W. Grimes {
529df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
530df8bae1dSRodney W. Grimes 	register struct ifreq *ifr;
531df8bae1dSRodney W. Grimes 	int error;
532df8bae1dSRodney W. Grimes 
533df8bae1dSRodney W. Grimes 	switch (cmd) {
534df8bae1dSRodney W. Grimes 
535df8bae1dSRodney W. Grimes 	case SIOCGIFCONF:
536df8bae1dSRodney W. Grimes 	case OSIOCGIFCONF:
537df8bae1dSRodney W. Grimes 		return (ifconf(cmd, data));
538df8bae1dSRodney W. Grimes 	}
539df8bae1dSRodney W. Grimes 	ifr = (struct ifreq *)data;
540df8bae1dSRodney W. Grimes 	ifp = ifunit(ifr->ifr_name);
541df8bae1dSRodney W. Grimes 	if (ifp == 0)
542df8bae1dSRodney W. Grimes 		return (ENXIO);
543df8bae1dSRodney W. Grimes 	switch (cmd) {
544df8bae1dSRodney W. Grimes 
545df8bae1dSRodney W. Grimes 	case SIOCGIFFLAGS:
546df8bae1dSRodney W. Grimes 		ifr->ifr_flags = ifp->if_flags;
547df8bae1dSRodney W. Grimes 		break;
548df8bae1dSRodney W. Grimes 
549df8bae1dSRodney W. Grimes 	case SIOCGIFMETRIC:
550df8bae1dSRodney W. Grimes 		ifr->ifr_metric = ifp->if_metric;
551df8bae1dSRodney W. Grimes 		break;
552df8bae1dSRodney W. Grimes 
553a7028af7SDavid Greenman 	case SIOCGIFMTU:
554a7028af7SDavid Greenman 		ifr->ifr_mtu = ifp->if_mtu;
555a7028af7SDavid Greenman 		break;
556a7028af7SDavid Greenman 
557074c4a4eSGarrett Wollman 	case SIOCGIFPHYS:
558074c4a4eSGarrett Wollman 		ifr->ifr_phys = ifp->if_physical;
559074c4a4eSGarrett Wollman 		break;
560074c4a4eSGarrett Wollman 
561df8bae1dSRodney W. Grimes 	case SIOCSIFFLAGS:
5629448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5639448326fSPoul-Henning Kamp 		if (error)
564df8bae1dSRodney W. Grimes 			return (error);
565df8bae1dSRodney W. Grimes 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
566df8bae1dSRodney W. Grimes 			int s = splimp();
567df8bae1dSRodney W. Grimes 			if_down(ifp);
568df8bae1dSRodney W. Grimes 			splx(s);
569df8bae1dSRodney W. Grimes 		}
570df8bae1dSRodney W. Grimes 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
571df8bae1dSRodney W. Grimes 			int s = splimp();
572df8bae1dSRodney W. Grimes 			if_up(ifp);
573df8bae1dSRodney W. Grimes 			splx(s);
574df8bae1dSRodney W. Grimes 		}
575df8bae1dSRodney W. Grimes 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
576df8bae1dSRodney W. Grimes 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
577df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl)
578df8bae1dSRodney W. Grimes 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
57998b9590eSPoul-Henning Kamp 		getmicrotime(&ifp->if_lastchange);
580df8bae1dSRodney W. Grimes 		break;
581df8bae1dSRodney W. Grimes 
582df8bae1dSRodney W. Grimes 	case SIOCSIFMETRIC:
5839448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5849448326fSPoul-Henning Kamp 		if (error)
585df8bae1dSRodney W. Grimes 			return (error);
586df8bae1dSRodney W. Grimes 		ifp->if_metric = ifr->ifr_metric;
58798b9590eSPoul-Henning Kamp 		getmicrotime(&ifp->if_lastchange);
588df8bae1dSRodney W. Grimes 		break;
589df8bae1dSRodney W. Grimes 
590074c4a4eSGarrett Wollman 	case SIOCSIFPHYS:
591074c4a4eSGarrett Wollman 		error = suser(p->p_ucred, &p->p_acflag);
592e39a0280SGary Palmer 		if (error)
593e39a0280SGary Palmer 			return error;
594e39a0280SGary Palmer 		if (!ifp->if_ioctl)
595e39a0280SGary Palmer 		        return EOPNOTSUPP;
596e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
597e39a0280SGary Palmer 		if (error == 0)
59898b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
599e39a0280SGary Palmer 		return(error);
600074c4a4eSGarrett Wollman 
601a7028af7SDavid Greenman 	case SIOCSIFMTU:
6029448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
6039448326fSPoul-Henning Kamp 		if (error)
604a7028af7SDavid Greenman 			return (error);
605a7028af7SDavid Greenman 		if (ifp->if_ioctl == NULL)
606a7028af7SDavid Greenman 			return (EOPNOTSUPP);
60766025569SDavid Greenman 		/*
60866025569SDavid Greenman 		 * 72 was chosen below because it is the size of a TCP/IP
60966025569SDavid Greenman 		 * header (40) + the minimum mss (32).
61066025569SDavid Greenman 		 */
61166025569SDavid Greenman 		if (ifr->ifr_mtu < 72 || ifr->ifr_mtu > 65535)
61275ee03cbSDavid Greenman 			return (EINVAL);
613e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
614e39a0280SGary Palmer 		if (error == 0)
61598b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
616e39a0280SGary Palmer 		return(error);
617a7028af7SDavid Greenman 
618df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
619df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
6209448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
6219448326fSPoul-Henning Kamp 		if (error)
622df8bae1dSRodney W. Grimes 			return (error);
623477180fbSGarrett Wollman 
624477180fbSGarrett Wollman 		/* Don't allow group membership on non-multicast interfaces. */
625477180fbSGarrett Wollman 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
626477180fbSGarrett Wollman 			return EOPNOTSUPP;
627477180fbSGarrett Wollman 
628477180fbSGarrett Wollman 		/* Don't let users screw up protocols' entries. */
629477180fbSGarrett Wollman 		if (ifr->ifr_addr.sa_family != AF_LINK)
630477180fbSGarrett Wollman 			return EINVAL;
631477180fbSGarrett Wollman 
632477180fbSGarrett Wollman 		if (cmd == SIOCADDMULTI) {
633477180fbSGarrett Wollman 			struct ifmultiaddr *ifma;
634477180fbSGarrett Wollman 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
635477180fbSGarrett Wollman 		} else {
636477180fbSGarrett Wollman 			error = if_delmulti(ifp, &ifr->ifr_addr);
637477180fbSGarrett Wollman 		}
638e39a0280SGary Palmer 		if (error == 0)
63998b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
640477180fbSGarrett Wollman 		return error;
641df8bae1dSRodney W. Grimes 
642a912e453SPeter Wemm         case SIOCSIFMEDIA:
643d7189ec6SJoerg Wunsch 	case SIOCSIFGENERIC:
644a912e453SPeter Wemm 		error = suser(p->p_ucred, &p->p_acflag);
645a912e453SPeter Wemm 		if (error)
646a912e453SPeter Wemm 			return (error);
647a912e453SPeter Wemm 		if (ifp->if_ioctl == 0)
648a912e453SPeter Wemm 			return (EOPNOTSUPP);
649a912e453SPeter Wemm 		error = (*ifp->if_ioctl)(ifp, cmd, data);
650a912e453SPeter Wemm 		if (error == 0)
65198b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
652a912e453SPeter Wemm 		return error;
653a912e453SPeter Wemm 
654a912e453SPeter Wemm 	case SIOCGIFMEDIA:
655d7189ec6SJoerg Wunsch 	case SIOCGIFGENERIC:
656a912e453SPeter Wemm 		if (ifp->if_ioctl == 0)
657a912e453SPeter Wemm 			return (EOPNOTSUPP);
658a912e453SPeter Wemm 		return ((*ifp->if_ioctl)(ifp, cmd, data));
659a912e453SPeter Wemm 
660df8bae1dSRodney W. Grimes 	default:
661df8bae1dSRodney W. Grimes 		if (so->so_proto == 0)
662df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
663df8bae1dSRodney W. Grimes #ifndef COMPAT_43
6642c37256eSGarrett Wollman 		return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
6652c37256eSGarrett Wollman 								 data,
666bc6d9b80SJoerg Wunsch 								 ifp, p));
667df8bae1dSRodney W. Grimes #else
668df8bae1dSRodney W. Grimes 	    {
669df8bae1dSRodney W. Grimes 		int ocmd = cmd;
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes 		switch (cmd) {
672df8bae1dSRodney W. Grimes 
673df8bae1dSRodney W. Grimes 		case SIOCSIFDSTADDR:
674df8bae1dSRodney W. Grimes 		case SIOCSIFADDR:
675df8bae1dSRodney W. Grimes 		case SIOCSIFBRDADDR:
676df8bae1dSRodney W. Grimes 		case SIOCSIFNETMASK:
677df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN
678df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_family == 0 &&
679df8bae1dSRodney W. Grimes 			    ifr->ifr_addr.sa_len < 16) {
680df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
681df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
682df8bae1dSRodney W. Grimes 			}
683df8bae1dSRodney W. Grimes #else
684df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_len == 0)
685df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
686df8bae1dSRodney W. Grimes #endif
687df8bae1dSRodney W. Grimes 			break;
688df8bae1dSRodney W. Grimes 
689df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
690df8bae1dSRodney W. Grimes 			cmd = SIOCGIFADDR;
691df8bae1dSRodney W. Grimes 			break;
692df8bae1dSRodney W. Grimes 
693df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
694df8bae1dSRodney W. Grimes 			cmd = SIOCGIFDSTADDR;
695df8bae1dSRodney W. Grimes 			break;
696df8bae1dSRodney W. Grimes 
697df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
698df8bae1dSRodney W. Grimes 			cmd = SIOCGIFBRDADDR;
699df8bae1dSRodney W. Grimes 			break;
700df8bae1dSRodney W. Grimes 
701df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
702df8bae1dSRodney W. Grimes 			cmd = SIOCGIFNETMASK;
703df8bae1dSRodney W. Grimes 		}
7042c37256eSGarrett Wollman 		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
7052c37256eSGarrett Wollman 								   cmd,
7062c37256eSGarrett Wollman 								   data,
707a29f300eSGarrett Wollman 								   ifp, p));
708df8bae1dSRodney W. Grimes 		switch (ocmd) {
709df8bae1dSRodney W. Grimes 
710df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
711df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
712df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
713df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
714df8bae1dSRodney W. Grimes 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
715df8bae1dSRodney W. Grimes 		}
716df8bae1dSRodney W. Grimes 		return (error);
717df8bae1dSRodney W. Grimes 
718df8bae1dSRodney W. Grimes 	    }
719df8bae1dSRodney W. Grimes #endif
720df8bae1dSRodney W. Grimes 	}
721df8bae1dSRodney W. Grimes 	return (0);
722df8bae1dSRodney W. Grimes }
723df8bae1dSRodney W. Grimes 
724df8bae1dSRodney W. Grimes /*
725963e4c2aSGarrett Wollman  * Set/clear promiscuous mode on interface ifp based on the truth value
726963e4c2aSGarrett Wollman  * of pswitch.  The calls are reference counted so that only the first
727963e4c2aSGarrett Wollman  * "on" request actually has an effect, as does the final "off" request.
728963e4c2aSGarrett Wollman  * Results are undefined if the "off" and "on" requests are not matched.
729963e4c2aSGarrett Wollman  */
730963e4c2aSGarrett Wollman int
731963e4c2aSGarrett Wollman ifpromisc(ifp, pswitch)
732963e4c2aSGarrett Wollman 	struct ifnet *ifp;
733963e4c2aSGarrett Wollman 	int pswitch;
734963e4c2aSGarrett Wollman {
735963e4c2aSGarrett Wollman 	struct ifreq ifr;
7364a26224cSGarrett Wollman 	int error;
737963e4c2aSGarrett Wollman 
738963e4c2aSGarrett Wollman 	if (pswitch) {
739963e4c2aSGarrett Wollman 		/*
740963e4c2aSGarrett Wollman 		 * If the device is not configured up, we cannot put it in
741963e4c2aSGarrett Wollman 		 * promiscuous mode.
742963e4c2aSGarrett Wollman 		 */
743963e4c2aSGarrett Wollman 		if ((ifp->if_flags & IFF_UP) == 0)
744963e4c2aSGarrett Wollman 			return (ENETDOWN);
745963e4c2aSGarrett Wollman 		if (ifp->if_pcount++ != 0)
746963e4c2aSGarrett Wollman 			return (0);
747963e4c2aSGarrett Wollman 		ifp->if_flags |= IFF_PROMISC;
748e9a30d00SGarrett Wollman 		log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
749963e4c2aSGarrett Wollman 		    ifp->if_name, ifp->if_unit);
750963e4c2aSGarrett Wollman 	} else {
751963e4c2aSGarrett Wollman 		if (--ifp->if_pcount > 0)
752963e4c2aSGarrett Wollman 			return (0);
753963e4c2aSGarrett Wollman 		ifp->if_flags &= ~IFF_PROMISC;
754963e4c2aSGarrett Wollman 	}
755963e4c2aSGarrett Wollman 	ifr.ifr_flags = ifp->if_flags;
7564a26224cSGarrett Wollman 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
7574a26224cSGarrett Wollman 	if (error == 0)
7584a26224cSGarrett Wollman 		rt_ifmsg(ifp);
7594a26224cSGarrett Wollman 	return error;
760963e4c2aSGarrett Wollman }
761963e4c2aSGarrett Wollman 
762963e4c2aSGarrett Wollman /*
763df8bae1dSRodney W. Grimes  * Return interface configuration
764df8bae1dSRodney W. Grimes  * of system.  List may be used
765df8bae1dSRodney W. Grimes  * in later ioctl's (above) to get
766df8bae1dSRodney W. Grimes  * other information.
767df8bae1dSRodney W. Grimes  */
768df8bae1dSRodney W. Grimes /*ARGSUSED*/
7693bda9f9bSPoul-Henning Kamp static int
770df8bae1dSRodney W. Grimes ifconf(cmd, data)
771df8bae1dSRodney W. Grimes 	int cmd;
772df8bae1dSRodney W. Grimes 	caddr_t data;
773df8bae1dSRodney W. Grimes {
774df8bae1dSRodney W. Grimes 	register struct ifconf *ifc = (struct ifconf *)data;
77529412182SGarrett Wollman 	register struct ifnet *ifp = ifnet.tqh_first;
776df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
777df8bae1dSRodney W. Grimes 	struct ifreq ifr, *ifrp;
778df8bae1dSRodney W. Grimes 	int space = ifc->ifc_len, error = 0;
779df8bae1dSRodney W. Grimes 
780df8bae1dSRodney W. Grimes 	ifrp = ifc->ifc_req;
78129412182SGarrett Wollman 	for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) {
7821ce9bf88SPoul-Henning Kamp 		char workbuf[64];
7831ce9bf88SPoul-Henning Kamp 		int ifnlen;
7842624cf89SGarrett Wollman 
7851ce9bf88SPoul-Henning Kamp 		ifnlen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
7861ce9bf88SPoul-Henning Kamp 		if(ifnlen + 1 > sizeof ifr.ifr_name) {
7872624cf89SGarrett Wollman 			error = ENAMETOOLONG;
7882624cf89SGarrett Wollman 		} else {
7891ce9bf88SPoul-Henning Kamp 			strcpy(ifr.ifr_name, workbuf);
7902624cf89SGarrett Wollman 		}
7912624cf89SGarrett Wollman 
79259562606SGarrett Wollman 		if ((ifa = ifp->if_addrhead.tqh_first) == 0) {
793df8bae1dSRodney W. Grimes 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
794df8bae1dSRodney W. Grimes 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
795df8bae1dSRodney W. Grimes 			    sizeof (ifr));
796df8bae1dSRodney W. Grimes 			if (error)
797df8bae1dSRodney W. Grimes 				break;
798df8bae1dSRodney W. Grimes 			space -= sizeof (ifr), ifrp++;
799df8bae1dSRodney W. Grimes 		} else
80059562606SGarrett Wollman 		    for ( ; space > sizeof (ifr) && ifa;
80159562606SGarrett Wollman 			 ifa = ifa->ifa_link.tqe_next) {
802df8bae1dSRodney W. Grimes 			register struct sockaddr *sa = ifa->ifa_addr;
803df8bae1dSRodney W. Grimes #ifdef COMPAT_43
804df8bae1dSRodney W. Grimes 			if (cmd == OSIOCGIFCONF) {
805df8bae1dSRodney W. Grimes 				struct osockaddr *osa =
806df8bae1dSRodney W. Grimes 					 (struct osockaddr *)&ifr.ifr_addr;
807df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
808df8bae1dSRodney W. Grimes 				osa->sa_family = sa->sa_family;
809df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
810df8bae1dSRodney W. Grimes 						sizeof (ifr));
811df8bae1dSRodney W. Grimes 				ifrp++;
812df8bae1dSRodney W. Grimes 			} else
813df8bae1dSRodney W. Grimes #endif
814df8bae1dSRodney W. Grimes 			if (sa->sa_len <= sizeof(*sa)) {
815df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
816df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
817df8bae1dSRodney W. Grimes 						sizeof (ifr));
818df8bae1dSRodney W. Grimes 				ifrp++;
819df8bae1dSRodney W. Grimes 			} else {
820df8bae1dSRodney W. Grimes 				space -= sa->sa_len - sizeof(*sa);
821df8bae1dSRodney W. Grimes 				if (space < sizeof (ifr))
822df8bae1dSRodney W. Grimes 					break;
823df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
824df8bae1dSRodney W. Grimes 						sizeof (ifr.ifr_name));
825df8bae1dSRodney W. Grimes 				if (error == 0)
826df8bae1dSRodney W. Grimes 				    error = copyout((caddr_t)sa,
827df8bae1dSRodney W. Grimes 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
828df8bae1dSRodney W. Grimes 				ifrp = (struct ifreq *)
829df8bae1dSRodney W. Grimes 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
830df8bae1dSRodney W. Grimes 			}
831df8bae1dSRodney W. Grimes 			if (error)
832df8bae1dSRodney W. Grimes 				break;
833df8bae1dSRodney W. Grimes 			space -= sizeof (ifr);
834df8bae1dSRodney W. Grimes 		}
835df8bae1dSRodney W. Grimes 	}
836df8bae1dSRodney W. Grimes 	ifc->ifc_len -= space;
837df8bae1dSRodney W. Grimes 	return (error);
838df8bae1dSRodney W. Grimes }
839df8bae1dSRodney W. Grimes 
8401158dfb7SGarrett Wollman /*
8411158dfb7SGarrett Wollman  * Just like if_promisc(), but for all-multicast-reception mode.
8421158dfb7SGarrett Wollman  */
8431158dfb7SGarrett Wollman int
8441158dfb7SGarrett Wollman if_allmulti(ifp, onswitch)
8451158dfb7SGarrett Wollman 	struct ifnet *ifp;
8461158dfb7SGarrett Wollman 	int onswitch;
8471158dfb7SGarrett Wollman {
8481158dfb7SGarrett Wollman 	int error = 0;
8491158dfb7SGarrett Wollman 	int s = splimp();
8501158dfb7SGarrett Wollman 
8511158dfb7SGarrett Wollman 	if (onswitch) {
8521158dfb7SGarrett Wollman 		if (ifp->if_amcount++ == 0) {
8531158dfb7SGarrett Wollman 			ifp->if_flags |= IFF_ALLMULTI;
8541158dfb7SGarrett Wollman 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
8551158dfb7SGarrett Wollman 		}
8561158dfb7SGarrett Wollman 	} else {
8571158dfb7SGarrett Wollman 		if (ifp->if_amcount > 1) {
8581158dfb7SGarrett Wollman 			ifp->if_amcount--;
8591158dfb7SGarrett Wollman 		} else {
8601158dfb7SGarrett Wollman 			ifp->if_amcount = 0;
8611158dfb7SGarrett Wollman 			ifp->if_flags &= ~IFF_ALLMULTI;
8621158dfb7SGarrett Wollman 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
8631158dfb7SGarrett Wollman 		}
8641158dfb7SGarrett Wollman 	}
8651158dfb7SGarrett Wollman 	splx(s);
8664a26224cSGarrett Wollman 
8674a26224cSGarrett Wollman 	if (error == 0)
8684a26224cSGarrett Wollman 		rt_ifmsg(ifp);
8691158dfb7SGarrett Wollman 	return error;
8701158dfb7SGarrett Wollman }
8711158dfb7SGarrett Wollman 
8721158dfb7SGarrett Wollman /*
8731158dfb7SGarrett Wollman  * Add a multicast listenership to the interface in question.
8741158dfb7SGarrett Wollman  * The link layer provides a routine which converts
8751158dfb7SGarrett Wollman  */
8761158dfb7SGarrett Wollman int
877373f88edSGarrett Wollman if_addmulti(ifp, sa, retifma)
8781158dfb7SGarrett Wollman 	struct ifnet *ifp;	/* interface to manipulate */
8791158dfb7SGarrett Wollman 	struct sockaddr *sa;	/* address to add */
880b2053118SGarrett Wollman 	struct ifmultiaddr **retifma;
8811158dfb7SGarrett Wollman {
8821158dfb7SGarrett Wollman 	struct sockaddr *llsa, *dupsa;
8831158dfb7SGarrett Wollman 	int error, s;
8841158dfb7SGarrett Wollman 	struct ifmultiaddr *ifma;
8851158dfb7SGarrett Wollman 
88657af7922SJulian Elischer 	/*
88757af7922SJulian Elischer 	 * If the matching multicast address already exists
88857af7922SJulian Elischer 	 * then don't add a new one, just add a reference
88957af7922SJulian Elischer 	 */
8901158dfb7SGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
8911158dfb7SGarrett Wollman 	     ifma = ifma->ifma_link.le_next) {
89257af7922SJulian Elischer 		if (equal(sa, ifma->ifma_addr)) {
8931158dfb7SGarrett Wollman 			ifma->ifma_refcount++;
89457af7922SJulian Elischer 			if (retifma)
89557af7922SJulian Elischer 				*retifma = ifma;
8961158dfb7SGarrett Wollman 			return 0;
8971158dfb7SGarrett Wollman 		}
89857af7922SJulian Elischer 	}
8991158dfb7SGarrett Wollman 
9001158dfb7SGarrett Wollman 	/*
9011158dfb7SGarrett Wollman 	 * Give the link layer a chance to accept/reject it, and also
9021158dfb7SGarrett Wollman 	 * find out which AF_LINK address this maps to, if it isn't one
9031158dfb7SGarrett Wollman 	 * already.
9041158dfb7SGarrett Wollman 	 */
9051158dfb7SGarrett Wollman 	if (ifp->if_resolvemulti) {
9061158dfb7SGarrett Wollman 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
9071158dfb7SGarrett Wollman 		if (error) return error;
9081158dfb7SGarrett Wollman 	} else {
9091158dfb7SGarrett Wollman 		llsa = 0;
9101158dfb7SGarrett Wollman 	}
9111158dfb7SGarrett Wollman 
9121158dfb7SGarrett Wollman 	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
9131158dfb7SGarrett Wollman 	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
9141158dfb7SGarrett Wollman 	bcopy(sa, dupsa, sa->sa_len);
9151158dfb7SGarrett Wollman 
9161158dfb7SGarrett Wollman 	ifma->ifma_addr = dupsa;
9171158dfb7SGarrett Wollman 	ifma->ifma_lladdr = llsa;
9181158dfb7SGarrett Wollman 	ifma->ifma_ifp = ifp;
9191158dfb7SGarrett Wollman 	ifma->ifma_refcount = 1;
920373f88edSGarrett Wollman 	ifma->ifma_protospec = 0;
921477180fbSGarrett Wollman 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
922373f88edSGarrett Wollman 
9231158dfb7SGarrett Wollman 	/*
9241158dfb7SGarrett Wollman 	 * Some network interfaces can scan the address list at
9251158dfb7SGarrett Wollman 	 * interrupt time; lock them out.
9261158dfb7SGarrett Wollman 	 */
9271158dfb7SGarrett Wollman 	s = splimp();
9281158dfb7SGarrett Wollman 	LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
9291158dfb7SGarrett Wollman 	splx(s);
930373f88edSGarrett Wollman 	*retifma = ifma;
9311158dfb7SGarrett Wollman 
9321158dfb7SGarrett Wollman 	if (llsa != 0) {
9331158dfb7SGarrett Wollman 		for (ifma = ifp->if_multiaddrs.lh_first; ifma;
9341158dfb7SGarrett Wollman 		     ifma = ifma->ifma_link.le_next) {
9351158dfb7SGarrett Wollman 			if (equal(ifma->ifma_addr, llsa))
9361158dfb7SGarrett Wollman 				break;
9371158dfb7SGarrett Wollman 		}
9381158dfb7SGarrett Wollman 		if (ifma) {
9391158dfb7SGarrett Wollman 			ifma->ifma_refcount++;
9401158dfb7SGarrett Wollman 		} else {
9411158dfb7SGarrett Wollman 			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
9421158dfb7SGarrett Wollman 			       M_IFMADDR, M_WAITOK);
943477180fbSGarrett Wollman 			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
944477180fbSGarrett Wollman 			       M_IFMADDR, M_WAITOK);
945477180fbSGarrett Wollman 			bcopy(llsa, dupsa, llsa->sa_len);
946477180fbSGarrett Wollman 			ifma->ifma_addr = dupsa;
9471158dfb7SGarrett Wollman 			ifma->ifma_ifp = ifp;
9481158dfb7SGarrett Wollman 			ifma->ifma_refcount = 1;
9491158dfb7SGarrett Wollman 			s = splimp();
9501158dfb7SGarrett Wollman 			LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
9511158dfb7SGarrett Wollman 			splx(s);
9521158dfb7SGarrett Wollman 		}
95357af7922SJulian Elischer 	}
9541158dfb7SGarrett Wollman 	/*
9551158dfb7SGarrett Wollman 	 * We are certain we have added something, so call down to the
9561158dfb7SGarrett Wollman 	 * interface to let them know about it.
9571158dfb7SGarrett Wollman 	 */
9581158dfb7SGarrett Wollman 	s = splimp();
9591158dfb7SGarrett Wollman 	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
9601158dfb7SGarrett Wollman 	splx(s);
9611158dfb7SGarrett Wollman 
9621158dfb7SGarrett Wollman 	return 0;
9631158dfb7SGarrett Wollman }
9641158dfb7SGarrett Wollman 
9651158dfb7SGarrett Wollman /*
9661158dfb7SGarrett Wollman  * Remove a reference to a multicast address on this interface.  Yell
9671158dfb7SGarrett Wollman  * if the request does not match an existing membership.
9681158dfb7SGarrett Wollman  */
9691158dfb7SGarrett Wollman int
9701158dfb7SGarrett Wollman if_delmulti(ifp, sa)
9711158dfb7SGarrett Wollman 	struct ifnet *ifp;
9721158dfb7SGarrett Wollman 	struct sockaddr *sa;
9731158dfb7SGarrett Wollman {
9741158dfb7SGarrett Wollman 	struct ifmultiaddr *ifma;
9751158dfb7SGarrett Wollman 	int s;
9761158dfb7SGarrett Wollman 
9771158dfb7SGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
9781158dfb7SGarrett Wollman 	     ifma = ifma->ifma_link.le_next)
9791158dfb7SGarrett Wollman 		if (equal(sa, ifma->ifma_addr))
9801158dfb7SGarrett Wollman 			break;
9811158dfb7SGarrett Wollman 	if (ifma == 0)
9821158dfb7SGarrett Wollman 		return ENOENT;
9831158dfb7SGarrett Wollman 
9841158dfb7SGarrett Wollman 	if (ifma->ifma_refcount > 1) {
9851158dfb7SGarrett Wollman 		ifma->ifma_refcount--;
9861158dfb7SGarrett Wollman 		return 0;
9871158dfb7SGarrett Wollman 	}
9881158dfb7SGarrett Wollman 
989477180fbSGarrett Wollman 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
9901158dfb7SGarrett Wollman 	sa = ifma->ifma_lladdr;
9911158dfb7SGarrett Wollman 	s = splimp();
9921158dfb7SGarrett Wollman 	LIST_REMOVE(ifma, ifma_link);
9931158dfb7SGarrett Wollman 	splx(s);
9941158dfb7SGarrett Wollman 	free(ifma->ifma_addr, M_IFMADDR);
9951158dfb7SGarrett Wollman 	free(ifma, M_IFMADDR);
9961158dfb7SGarrett Wollman 	if (sa == 0)
9971158dfb7SGarrett Wollman 		return 0;
9981158dfb7SGarrett Wollman 
9991158dfb7SGarrett Wollman 	/*
10001158dfb7SGarrett Wollman 	 * Now look for the link-layer address which corresponds to
10011158dfb7SGarrett Wollman 	 * this network address.  It had been squirreled away in
10021158dfb7SGarrett Wollman 	 * ifma->ifma_lladdr for this purpose (so we don't have
10031158dfb7SGarrett Wollman 	 * to call ifp->if_resolvemulti() again), and we saved that
10041158dfb7SGarrett Wollman 	 * value in sa above.  If some nasty deleted the
10051158dfb7SGarrett Wollman 	 * link-layer address out from underneath us, we can deal because
10061158dfb7SGarrett Wollman 	 * the address we stored was is not the same as the one which was
10071158dfb7SGarrett Wollman 	 * in the record for the link-layer address.  (So we don't complain
10081158dfb7SGarrett Wollman 	 * in that case.)
10091158dfb7SGarrett Wollman 	 */
10101158dfb7SGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
10111158dfb7SGarrett Wollman 	     ifma = ifma->ifma_link.le_next)
10121158dfb7SGarrett Wollman 		if (equal(sa, ifma->ifma_addr))
10131158dfb7SGarrett Wollman 			break;
10141158dfb7SGarrett Wollman 	if (ifma == 0)
10151158dfb7SGarrett Wollman 		return 0;
10161158dfb7SGarrett Wollman 
10171158dfb7SGarrett Wollman 	if (ifma->ifma_refcount > 1) {
10181158dfb7SGarrett Wollman 		ifma->ifma_refcount--;
10191158dfb7SGarrett Wollman 		return 0;
10201158dfb7SGarrett Wollman 	}
10211158dfb7SGarrett Wollman 
10221158dfb7SGarrett Wollman 	s = splimp();
10231158dfb7SGarrett Wollman 	LIST_REMOVE(ifma, ifma_link);
10241158dfb7SGarrett Wollman 	splx(s);
10251158dfb7SGarrett Wollman 	free(ifma->ifma_addr, M_IFMADDR);
10261158dfb7SGarrett Wollman 	free(sa, M_IFMADDR);
10271158dfb7SGarrett Wollman 	free(ifma, M_IFMADDR);
10281158dfb7SGarrett Wollman 
10291158dfb7SGarrett Wollman 	return 0;
10301158dfb7SGarrett Wollman }
10311158dfb7SGarrett Wollman 
1032373f88edSGarrett Wollman struct ifmultiaddr *
1033373f88edSGarrett Wollman ifmaof_ifpforaddr(sa, ifp)
1034373f88edSGarrett Wollman 	struct sockaddr *sa;
1035373f88edSGarrett Wollman 	struct ifnet *ifp;
1036373f88edSGarrett Wollman {
1037373f88edSGarrett Wollman 	struct ifmultiaddr *ifma;
1038373f88edSGarrett Wollman 
1039373f88edSGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1040373f88edSGarrett Wollman 	     ifma = ifma->ifma_link.le_next)
1041373f88edSGarrett Wollman 		if (equal(ifma->ifma_addr, sa))
1042373f88edSGarrett Wollman 			break;
1043373f88edSGarrett Wollman 
1044373f88edSGarrett Wollman 	return ifma;
1045373f88edSGarrett Wollman }
1046373f88edSGarrett Wollman 
1047602d513cSGarrett Wollman SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
10482c37256eSGarrett Wollman SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
1049