xref: /freebsd/sys/net/if.c (revision e8c2601dbf89c2b2f4b50f66e6684faf32852aec)
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
34e8c2601dSPoul-Henning Kamp  *	$Id: if.c,v 1.63 1998/12/04 22:54:52 archie 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 
60ecbb00a2SDoug Rabson static int ifconf __P((u_long, 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 	 */
1462127f260SArchie Cobbs 	namelen = snprintf(workbuf, sizeof(workbuf),
1472127f260SArchie Cobbs 	    "%s%d", ifp->if_name, ifp->if_unit);
148df8bae1dSRodney W. Grimes #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
1491ce9bf88SPoul-Henning Kamp 	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
150df8bae1dSRodney W. Grimes 	socksize = masklen + ifp->if_addrlen;
151df8bae1dSRodney W. Grimes #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
152df8bae1dSRodney W. Grimes 	if (socksize < sizeof(*sdl))
153df8bae1dSRodney W. Grimes 		socksize = sizeof(*sdl);
1548a261b8fSDoug Rabson 	socksize = ROUNDUP(socksize);
155df8bae1dSRodney W. Grimes 	ifasize = sizeof(*ifa) + 2 * socksize;
1569448326fSPoul-Henning Kamp 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
1579448326fSPoul-Henning Kamp 	if (ifa) {
158df8bae1dSRodney W. Grimes 		bzero((caddr_t)ifa, ifasize);
159df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(ifa + 1);
160df8bae1dSRodney W. Grimes 		sdl->sdl_len = socksize;
161df8bae1dSRodney W. Grimes 		sdl->sdl_family = AF_LINK;
1621ce9bf88SPoul-Henning Kamp 		bcopy(workbuf, sdl->sdl_data, namelen);
1631ce9bf88SPoul-Henning Kamp 		sdl->sdl_nlen = namelen;
164df8bae1dSRodney W. Grimes 		sdl->sdl_index = ifp->if_index;
165df8bae1dSRodney W. Grimes 		sdl->sdl_type = ifp->if_type;
166df8bae1dSRodney W. Grimes 		ifnet_addrs[if_index - 1] = ifa;
167df8bae1dSRodney W. Grimes 		ifa->ifa_ifp = ifp;
168df8bae1dSRodney W. Grimes 		ifa->ifa_rtrequest = link_rtrequest;
169df8bae1dSRodney W. Grimes 		ifa->ifa_addr = (struct sockaddr *)sdl;
170df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
171df8bae1dSRodney W. Grimes 		ifa->ifa_netmask = (struct sockaddr *)sdl;
172df8bae1dSRodney W. Grimes 		sdl->sdl_len = masklen;
173df8bae1dSRodney W. Grimes 		while (namelen != 0)
174df8bae1dSRodney W. Grimes 			sdl->sdl_data[--namelen] = 0xff;
17559562606SGarrett Wollman 		TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
176df8bae1dSRodney W. Grimes 	}
177df8bae1dSRodney W. Grimes }
178df8bae1dSRodney W. Grimes /*
179df8bae1dSRodney W. Grimes  * Locate an interface based on a complete address.
180df8bae1dSRodney W. Grimes  */
181df8bae1dSRodney W. Grimes /*ARGSUSED*/
182df8bae1dSRodney W. Grimes struct ifaddr *
183df8bae1dSRodney W. Grimes ifa_ifwithaddr(addr)
184df8bae1dSRodney W. Grimes 	register struct sockaddr *addr;
185df8bae1dSRodney W. Grimes {
186df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
187df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
188df8bae1dSRodney W. Grimes 
189df8bae1dSRodney W. Grimes #define	equal(a1, a2) \
190df8bae1dSRodney W. Grimes   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
19129412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
19259562606SGarrett Wollman 	    for (ifa = ifp->if_addrhead.tqh_first; ifa;
19359562606SGarrett Wollman 		 ifa = ifa->ifa_link.tqe_next) {
194df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != addr->sa_family)
195df8bae1dSRodney W. Grimes 			continue;
196df8bae1dSRodney W. Grimes 		if (equal(addr, ifa->ifa_addr))
197df8bae1dSRodney W. Grimes 			return (ifa);
198df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
199df8bae1dSRodney W. Grimes 		    equal(ifa->ifa_broadaddr, addr))
200df8bae1dSRodney W. Grimes 			return (ifa);
201df8bae1dSRodney W. Grimes 	}
202df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
203df8bae1dSRodney W. Grimes }
204df8bae1dSRodney W. Grimes /*
205df8bae1dSRodney W. Grimes  * Locate the point to point interface with a given destination address.
206df8bae1dSRodney W. Grimes  */
207df8bae1dSRodney W. Grimes /*ARGSUSED*/
208df8bae1dSRodney W. Grimes struct ifaddr *
209df8bae1dSRodney W. Grimes ifa_ifwithdstaddr(addr)
210df8bae1dSRodney W. Grimes 	register struct sockaddr *addr;
211df8bae1dSRodney W. Grimes {
212df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
213df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
214df8bae1dSRodney W. Grimes 
21529412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
216df8bae1dSRodney W. Grimes 	    if (ifp->if_flags & IFF_POINTOPOINT)
21759562606SGarrett Wollman 		for (ifa = ifp->if_addrhead.tqh_first; ifa;
21859562606SGarrett Wollman 		     ifa = ifa->ifa_link.tqe_next) {
219df8bae1dSRodney W. Grimes 			if (ifa->ifa_addr->sa_family != addr->sa_family)
220df8bae1dSRodney W. Grimes 				continue;
22155088a1cSDavid Greenman 			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
222df8bae1dSRodney W. Grimes 				return (ifa);
223df8bae1dSRodney W. Grimes 	}
224df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
225df8bae1dSRodney W. Grimes }
226df8bae1dSRodney W. Grimes 
227df8bae1dSRodney W. Grimes /*
228df8bae1dSRodney W. Grimes  * Find an interface on a specific network.  If many, choice
229df8bae1dSRodney W. Grimes  * is most specific found.
230df8bae1dSRodney W. Grimes  */
231df8bae1dSRodney W. Grimes struct ifaddr *
232df8bae1dSRodney W. Grimes ifa_ifwithnet(addr)
233df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
234df8bae1dSRodney W. Grimes {
235df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
236df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
237df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
238df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
239df8bae1dSRodney W. Grimes 	char *addr_data = addr->sa_data, *cplim;
240df8bae1dSRodney W. Grimes 
2417e2a6151SJulian Elischer 	/*
2427e2a6151SJulian Elischer 	 * AF_LINK addresses can be looked up directly by their index number,
2437e2a6151SJulian Elischer 	 * so do that if we can.
2447e2a6151SJulian Elischer 	 */
245df8bae1dSRodney W. Grimes 	if (af == AF_LINK) {
246df8bae1dSRodney W. Grimes 	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
247df8bae1dSRodney W. Grimes 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
248df8bae1dSRodney W. Grimes 		return (ifnet_addrs[sdl->sdl_index - 1]);
249df8bae1dSRodney W. Grimes 	}
2507e2a6151SJulian Elischer 
2517e2a6151SJulian Elischer 	/*
2527e2a6151SJulian Elischer 	 * Scan though each interface, looking for ones that have
2537e2a6151SJulian Elischer 	 * addresses in this address family.
2547e2a6151SJulian Elischer 	 */
25529412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
25659562606SGarrett Wollman 		for (ifa = ifp->if_addrhead.tqh_first; ifa;
25759562606SGarrett Wollman 		     ifa = ifa->ifa_link.tqe_next) {
258df8bae1dSRodney W. Grimes 			register char *cp, *cp2, *cp3;
259df8bae1dSRodney W. Grimes 
260523a02aaSDavid Greenman 			if (ifa->ifa_addr->sa_family != af)
261df8bae1dSRodney W. Grimes next:				continue;
262b2af64fdSDavid Greenman 			if (ifp->if_flags & IFF_POINTOPOINT) {
2637e2a6151SJulian Elischer 				/*
2647e2a6151SJulian Elischer 				 * This is a bit broken as it doesn't
2657e2a6151SJulian Elischer 				 * take into account that the remote end may
2667e2a6151SJulian Elischer 				 * be a single node in the network we are
2677e2a6151SJulian Elischer 				 * looking for.
2687e2a6151SJulian Elischer 				 * The trouble is that we don't know the
2697e2a6151SJulian Elischer 				 * netmask for the remote end.
2707e2a6151SJulian Elischer 				 */
271fcd6781aSGarrett Wollman 				if (ifa->ifa_dstaddr != 0
272fcd6781aSGarrett Wollman 				    && equal(addr, ifa->ifa_dstaddr))
273b2af64fdSDavid Greenman  					return (ifa);
2743740e2adSDavid Greenman 			} else {
2757e2a6151SJulian Elischer 				/*
2767ed8f465SJulian Elischer 				 * if we have a special address handler,
2777ed8f465SJulian Elischer 				 * then use it instead of the generic one.
2787ed8f465SJulian Elischer 				 */
2797ed8f465SJulian Elischer 	          		if (ifa->ifa_claim_addr) {
2807ed8f465SJulian Elischer 					if ((*ifa->ifa_claim_addr)(ifa, addr)) {
2817ed8f465SJulian Elischer 						return (ifa);
2827ed8f465SJulian Elischer 					} else {
2837ed8f465SJulian Elischer 						continue;
2847ed8f465SJulian Elischer 					}
2857ed8f465SJulian Elischer 				}
2867ed8f465SJulian Elischer 
2877ed8f465SJulian Elischer 				/*
2887e2a6151SJulian Elischer 				 * Scan all the bits in the ifa's address.
2897e2a6151SJulian Elischer 				 * If a bit dissagrees with what we are
2907e2a6151SJulian Elischer 				 * looking for, mask it with the netmask
2917e2a6151SJulian Elischer 				 * to see if it really matters.
2927e2a6151SJulian Elischer 				 * (A byte at a time)
2937e2a6151SJulian Elischer 				 */
294523a02aaSDavid Greenman 				if (ifa->ifa_netmask == 0)
295523a02aaSDavid Greenman 					continue;
296df8bae1dSRodney W. Grimes 				cp = addr_data;
297df8bae1dSRodney W. Grimes 				cp2 = ifa->ifa_addr->sa_data;
298df8bae1dSRodney W. Grimes 				cp3 = ifa->ifa_netmask->sa_data;
2997e2a6151SJulian Elischer 				cplim = ifa->ifa_netmask->sa_len
3007e2a6151SJulian Elischer 					+ (char *)ifa->ifa_netmask;
301df8bae1dSRodney W. Grimes 				while (cp3 < cplim)
302df8bae1dSRodney W. Grimes 					if ((*cp++ ^ *cp2++) & *cp3++)
3037e2a6151SJulian Elischer 						goto next; /* next address! */
3047e2a6151SJulian Elischer 				/*
3057e2a6151SJulian Elischer 				 * If the netmask of what we just found
3067e2a6151SJulian Elischer 				 * is more specific than what we had before
3077e2a6151SJulian Elischer 				 * (if we had one) then remember the new one
3087e2a6151SJulian Elischer 				 * before continuing to search
3097e2a6151SJulian Elischer 				 * for an even better one.
3107e2a6151SJulian Elischer 				 */
311df8bae1dSRodney W. Grimes 				if (ifa_maybe == 0 ||
312df8bae1dSRodney W. Grimes 				    rn_refines((caddr_t)ifa->ifa_netmask,
313df8bae1dSRodney W. Grimes 				    (caddr_t)ifa_maybe->ifa_netmask))
314df8bae1dSRodney W. Grimes 					ifa_maybe = ifa;
315df8bae1dSRodney W. Grimes 			}
316b2af64fdSDavid Greenman 		}
317b2af64fdSDavid Greenman 	}
318df8bae1dSRodney W. Grimes 	return (ifa_maybe);
319df8bae1dSRodney W. Grimes }
320df8bae1dSRodney W. Grimes 
321df8bae1dSRodney W. Grimes /*
322df8bae1dSRodney W. Grimes  * Find an interface address specific to an interface best matching
323df8bae1dSRodney W. Grimes  * a given address.
324df8bae1dSRodney W. Grimes  */
325df8bae1dSRodney W. Grimes struct ifaddr *
326df8bae1dSRodney W. Grimes ifaof_ifpforaddr(addr, ifp)
327df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
328df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
329df8bae1dSRodney W. Grimes {
330df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
331df8bae1dSRodney W. Grimes 	register char *cp, *cp2, *cp3;
332df8bae1dSRodney W. Grimes 	register char *cplim;
333df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = 0;
334df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
335df8bae1dSRodney W. Grimes 
336df8bae1dSRodney W. Grimes 	if (af >= AF_MAX)
337df8bae1dSRodney W. Grimes 		return (0);
33859562606SGarrett Wollman 	for (ifa = ifp->if_addrhead.tqh_first; ifa;
33959562606SGarrett Wollman 	     ifa = ifa->ifa_link.tqe_next) {
340df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != af)
341df8bae1dSRodney W. Grimes 			continue;
342381dd1d2SJulian Elischer 		if (ifa_maybe == 0)
343df8bae1dSRodney W. Grimes 			ifa_maybe = ifa;
344df8bae1dSRodney W. Grimes 		if (ifa->ifa_netmask == 0) {
345df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_addr) ||
346df8bae1dSRodney W. Grimes 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
347df8bae1dSRodney W. Grimes 				return (ifa);
348df8bae1dSRodney W. Grimes 			continue;
349df8bae1dSRodney W. Grimes 		}
350b2af64fdSDavid Greenman 		if (ifp->if_flags & IFF_POINTOPOINT) {
351b2af64fdSDavid Greenman 			if (equal(addr, ifa->ifa_dstaddr))
352b2af64fdSDavid Greenman 				return (ifa);
3533740e2adSDavid Greenman 		} else {
354df8bae1dSRodney W. Grimes 			cp = addr->sa_data;
355df8bae1dSRodney W. Grimes 			cp2 = ifa->ifa_addr->sa_data;
356df8bae1dSRodney W. Grimes 			cp3 = ifa->ifa_netmask->sa_data;
357df8bae1dSRodney W. Grimes 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
358df8bae1dSRodney W. Grimes 			for (; cp3 < cplim; cp3++)
359df8bae1dSRodney W. Grimes 				if ((*cp++ ^ *cp2++) & *cp3)
360df8bae1dSRodney W. Grimes 					break;
361df8bae1dSRodney W. Grimes 			if (cp3 == cplim)
362df8bae1dSRodney W. Grimes 				return (ifa);
363df8bae1dSRodney W. Grimes 		}
364b2af64fdSDavid Greenman 	}
365df8bae1dSRodney W. Grimes 	return (ifa_maybe);
366df8bae1dSRodney W. Grimes }
367df8bae1dSRodney W. Grimes 
368df8bae1dSRodney W. Grimes #include <net/route.h>
369df8bae1dSRodney W. Grimes 
370df8bae1dSRodney W. Grimes /*
371df8bae1dSRodney W. Grimes  * Default action when installing a route with a Link Level gateway.
372df8bae1dSRodney W. Grimes  * Lookup an appropriate real ifa to point to.
373df8bae1dSRodney W. Grimes  * This should be moved to /sys/net/link.c eventually.
374df8bae1dSRodney W. Grimes  */
3753bda9f9bSPoul-Henning Kamp static void
376df8bae1dSRodney W. Grimes link_rtrequest(cmd, rt, sa)
377df8bae1dSRodney W. Grimes 	int cmd;
378df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
379df8bae1dSRodney W. Grimes 	struct sockaddr *sa;
380df8bae1dSRodney W. Grimes {
381df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
382df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
383df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
384df8bae1dSRodney W. Grimes 
385df8bae1dSRodney W. Grimes 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
386df8bae1dSRodney W. Grimes 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
387df8bae1dSRodney W. Grimes 		return;
3889448326fSPoul-Henning Kamp 	ifa = ifaof_ifpforaddr(dst, ifp);
3899448326fSPoul-Henning Kamp 	if (ifa) {
390df8bae1dSRodney W. Grimes 		IFAFREE(rt->rt_ifa);
391df8bae1dSRodney W. Grimes 		rt->rt_ifa = ifa;
392df8bae1dSRodney W. Grimes 		ifa->ifa_refcnt++;
393df8bae1dSRodney W. Grimes 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
394df8bae1dSRodney W. Grimes 			ifa->ifa_rtrequest(cmd, rt, sa);
395df8bae1dSRodney W. Grimes 	}
396df8bae1dSRodney W. Grimes }
397df8bae1dSRodney W. Grimes 
398df8bae1dSRodney W. Grimes /*
399df8bae1dSRodney W. Grimes  * Mark an interface down and notify protocols of
400df8bae1dSRodney W. Grimes  * the transition.
401df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
402df8bae1dSRodney W. Grimes  */
403df8bae1dSRodney W. Grimes void
404e8c2601dSPoul-Henning Kamp if_unroute(ifp, flag, fam)
405df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
406e8c2601dSPoul-Henning Kamp 	int flag, fam;
407df8bae1dSRodney W. Grimes {
408df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
409df8bae1dSRodney W. Grimes 
410e8c2601dSPoul-Henning Kamp 	ifp->if_flags &= ~flag;
41198b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
412e8c2601dSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
413e8c2601dSPoul-Henning Kamp 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
414df8bae1dSRodney W. Grimes 			pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
415df8bae1dSRodney W. Grimes 	if_qflush(&ifp->if_snd);
416df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
417df8bae1dSRodney W. Grimes }
418df8bae1dSRodney W. Grimes 
419df8bae1dSRodney W. Grimes /*
420df8bae1dSRodney W. Grimes  * Mark an interface up and notify protocols of
421df8bae1dSRodney W. Grimes  * the transition.
422df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
423df8bae1dSRodney W. Grimes  */
424df8bae1dSRodney W. Grimes void
425e8c2601dSPoul-Henning Kamp if_route(ifp, flag, fam)
426df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
427e8c2601dSPoul-Henning Kamp 	int flag, fam;
428df8bae1dSRodney W. Grimes {
429176395b2SGarrett Wollman 	register struct ifaddr *ifa;
430df8bae1dSRodney W. Grimes 
431e8c2601dSPoul-Henning Kamp 	ifp->if_flags |= flag;
43298b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
433e8c2601dSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
434e8c2601dSPoul-Henning Kamp 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
435df8bae1dSRodney W. Grimes 			pfctlinput(PRC_IFUP, ifa->ifa_addr);
436df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
437df8bae1dSRodney W. Grimes }
438df8bae1dSRodney W. Grimes 
439df8bae1dSRodney W. Grimes /*
440e8c2601dSPoul-Henning Kamp  * Mark an interface down and notify protocols of
441e8c2601dSPoul-Henning Kamp  * the transition.
442e8c2601dSPoul-Henning Kamp  * NOTE: must be called at splnet or eqivalent.
443e8c2601dSPoul-Henning Kamp  */
444e8c2601dSPoul-Henning Kamp void
445e8c2601dSPoul-Henning Kamp if_down(ifp)
446e8c2601dSPoul-Henning Kamp 	register struct ifnet *ifp;
447e8c2601dSPoul-Henning Kamp {
448e8c2601dSPoul-Henning Kamp 
449e8c2601dSPoul-Henning Kamp 	if_unroute(ifp, IFF_UP, AF_UNSPEC);
450e8c2601dSPoul-Henning Kamp }
451e8c2601dSPoul-Henning Kamp 
452e8c2601dSPoul-Henning Kamp /*
453e8c2601dSPoul-Henning Kamp  * Mark an interface up and notify protocols of
454e8c2601dSPoul-Henning Kamp  * the transition.
455e8c2601dSPoul-Henning Kamp  * NOTE: must be called at splnet or eqivalent.
456e8c2601dSPoul-Henning Kamp  */
457e8c2601dSPoul-Henning Kamp void
458e8c2601dSPoul-Henning Kamp if_up(ifp)
459e8c2601dSPoul-Henning Kamp 	register struct ifnet *ifp;
460e8c2601dSPoul-Henning Kamp {
461e8c2601dSPoul-Henning Kamp 
462e8c2601dSPoul-Henning Kamp 	if_route(ifp, IFF_UP, AF_UNSPEC);
463e8c2601dSPoul-Henning Kamp }
464e8c2601dSPoul-Henning Kamp 
465e8c2601dSPoul-Henning Kamp /*
466df8bae1dSRodney W. Grimes  * Flush an interface queue.
467df8bae1dSRodney W. Grimes  */
4683bda9f9bSPoul-Henning Kamp static void
469df8bae1dSRodney W. Grimes if_qflush(ifq)
470df8bae1dSRodney W. Grimes 	register struct ifqueue *ifq;
471df8bae1dSRodney W. Grimes {
472df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
473df8bae1dSRodney W. Grimes 
474df8bae1dSRodney W. Grimes 	n = ifq->ifq_head;
4759448326fSPoul-Henning Kamp 	while ((m = n) != 0) {
476df8bae1dSRodney W. Grimes 		n = m->m_act;
477df8bae1dSRodney W. Grimes 		m_freem(m);
478df8bae1dSRodney W. Grimes 	}
479df8bae1dSRodney W. Grimes 	ifq->ifq_head = 0;
480df8bae1dSRodney W. Grimes 	ifq->ifq_tail = 0;
481df8bae1dSRodney W. Grimes 	ifq->ifq_len = 0;
482df8bae1dSRodney W. Grimes }
483df8bae1dSRodney W. Grimes 
484df8bae1dSRodney W. Grimes /*
485df8bae1dSRodney W. Grimes  * Handle interface watchdog timer routines.  Called
486df8bae1dSRodney W. Grimes  * from softclock, we decrement timers (if set) and
487df8bae1dSRodney W. Grimes  * call the appropriate interface routine on expiration.
488df8bae1dSRodney W. Grimes  */
4893bda9f9bSPoul-Henning Kamp static void
490df8bae1dSRodney W. Grimes if_slowtimo(arg)
491df8bae1dSRodney W. Grimes 	void *arg;
492df8bae1dSRodney W. Grimes {
493df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
494df8bae1dSRodney W. Grimes 	int s = splimp();
495df8bae1dSRodney W. Grimes 
49629412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
497df8bae1dSRodney W. Grimes 		if (ifp->if_timer == 0 || --ifp->if_timer)
498df8bae1dSRodney W. Grimes 			continue;
499df8bae1dSRodney W. Grimes 		if (ifp->if_watchdog)
5004a5f1499SDavid Greenman 			(*ifp->if_watchdog)(ifp);
501df8bae1dSRodney W. Grimes 	}
502df8bae1dSRodney W. Grimes 	splx(s);
503df8bae1dSRodney W. Grimes 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
504df8bae1dSRodney W. Grimes }
505df8bae1dSRodney W. Grimes 
506df8bae1dSRodney W. Grimes /*
507df8bae1dSRodney W. Grimes  * Map interface name to
508df8bae1dSRodney W. Grimes  * interface structure pointer.
509df8bae1dSRodney W. Grimes  */
510df8bae1dSRodney W. Grimes struct ifnet *
511df8bae1dSRodney W. Grimes ifunit(name)
512df8bae1dSRodney W. Grimes 	register char *name;
513df8bae1dSRodney W. Grimes {
51401f0fef3SJulian Elischer 	char namebuf[IFNAMSIZ + 1];
51501f0fef3SJulian Elischer 	register char *cp, *cp2;
51601f0fef3SJulian Elischer 	char *end;
517df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
518df8bae1dSRodney W. Grimes 	int unit;
519df8bae1dSRodney W. Grimes 	unsigned len;
52001f0fef3SJulian Elischer 	register char c = '\0';
521df8bae1dSRodney W. Grimes 
522df8bae1dSRodney W. Grimes 	/*
52301f0fef3SJulian Elischer 	 * Look for a non numeric part
52401f0fef3SJulian Elischer 	 */
52501f0fef3SJulian Elischer 	end = name + IFNAMSIZ;
52601f0fef3SJulian Elischer 	cp2 = namebuf;
52701f0fef3SJulian Elischer 	cp = name;
52801f0fef3SJulian Elischer 	while ((cp < end) && (c = *cp)) {
52901f0fef3SJulian Elischer 		if (c >= '0' && c <= '9')
53001f0fef3SJulian Elischer 			break;
53101f0fef3SJulian Elischer 		*cp2++ = c;
53201f0fef3SJulian Elischer 		cp++;
53301f0fef3SJulian Elischer 	}
53401f0fef3SJulian Elischer 	if ((cp == end) || (c == '\0') || (cp == name))
53501f0fef3SJulian Elischer 		return ((struct ifnet *)0);
53601f0fef3SJulian Elischer 	*cp2 = '\0';
53701f0fef3SJulian Elischer 	/*
53801f0fef3SJulian Elischer 	 * check we have a legal number (limit to 7 digits?)
539df8bae1dSRodney W. Grimes 	 */
540df8bae1dSRodney W. Grimes 	len = cp - name + 1;
54101f0fef3SJulian Elischer 	for (unit = 0;
54201f0fef3SJulian Elischer 	    ((c = *cp) >= '0') && (c <= '9') && (unit < 1000000); cp++ )
54301f0fef3SJulian Elischer 		unit = (unit * 10) + (c - '0');
54458639ed3SGarrett Wollman 	if (*cp != '\0')
54558639ed3SGarrett Wollman 		return 0;	/* no trailing garbage allowed */
54601f0fef3SJulian Elischer 	/*
54701f0fef3SJulian Elischer 	 * Now search all the interfaces for this name/number
54801f0fef3SJulian Elischer 	 */
54929412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
55001f0fef3SJulian Elischer 		if (bcmp(ifp->if_name, namebuf, len))
551df8bae1dSRodney W. Grimes 			continue;
552df8bae1dSRodney W. Grimes 		if (unit == ifp->if_unit)
553df8bae1dSRodney W. Grimes 			break;
554df8bae1dSRodney W. Grimes 	}
555df8bae1dSRodney W. Grimes 	return (ifp);
556df8bae1dSRodney W. Grimes }
557df8bae1dSRodney W. Grimes 
558df8bae1dSRodney W. Grimes /*
559df8bae1dSRodney W. Grimes  * Interface ioctls.
560df8bae1dSRodney W. Grimes  */
561df8bae1dSRodney W. Grimes int
562df8bae1dSRodney W. Grimes ifioctl(so, cmd, data, p)
563df8bae1dSRodney W. Grimes 	struct socket *so;
564ecbb00a2SDoug Rabson 	u_long cmd;
565df8bae1dSRodney W. Grimes 	caddr_t data;
566df8bae1dSRodney W. Grimes 	struct proc *p;
567df8bae1dSRodney W. Grimes {
568df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
569df8bae1dSRodney W. Grimes 	register struct ifreq *ifr;
570df8bae1dSRodney W. Grimes 	int error;
571df8bae1dSRodney W. Grimes 
572df8bae1dSRodney W. Grimes 	switch (cmd) {
573df8bae1dSRodney W. Grimes 
574df8bae1dSRodney W. Grimes 	case SIOCGIFCONF:
575df8bae1dSRodney W. Grimes 	case OSIOCGIFCONF:
576df8bae1dSRodney W. Grimes 		return (ifconf(cmd, data));
577df8bae1dSRodney W. Grimes 	}
578df8bae1dSRodney W. Grimes 	ifr = (struct ifreq *)data;
579df8bae1dSRodney W. Grimes 	ifp = ifunit(ifr->ifr_name);
580df8bae1dSRodney W. Grimes 	if (ifp == 0)
581df8bae1dSRodney W. Grimes 		return (ENXIO);
582df8bae1dSRodney W. Grimes 	switch (cmd) {
583df8bae1dSRodney W. Grimes 
584df8bae1dSRodney W. Grimes 	case SIOCGIFFLAGS:
585df8bae1dSRodney W. Grimes 		ifr->ifr_flags = ifp->if_flags;
586df8bae1dSRodney W. Grimes 		break;
587df8bae1dSRodney W. Grimes 
588df8bae1dSRodney W. Grimes 	case SIOCGIFMETRIC:
589df8bae1dSRodney W. Grimes 		ifr->ifr_metric = ifp->if_metric;
590df8bae1dSRodney W. Grimes 		break;
591df8bae1dSRodney W. Grimes 
592a7028af7SDavid Greenman 	case SIOCGIFMTU:
593a7028af7SDavid Greenman 		ifr->ifr_mtu = ifp->if_mtu;
594a7028af7SDavid Greenman 		break;
595a7028af7SDavid Greenman 
596074c4a4eSGarrett Wollman 	case SIOCGIFPHYS:
597074c4a4eSGarrett Wollman 		ifr->ifr_phys = ifp->if_physical;
598074c4a4eSGarrett Wollman 		break;
599074c4a4eSGarrett Wollman 
600df8bae1dSRodney W. Grimes 	case SIOCSIFFLAGS:
6019448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
6029448326fSPoul-Henning Kamp 		if (error)
603df8bae1dSRodney W. Grimes 			return (error);
604df8bae1dSRodney W. Grimes 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
605df8bae1dSRodney W. Grimes 			int s = splimp();
606df8bae1dSRodney W. Grimes 			if_down(ifp);
607df8bae1dSRodney W. Grimes 			splx(s);
608df8bae1dSRodney W. Grimes 		}
609df8bae1dSRodney W. Grimes 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
610df8bae1dSRodney W. Grimes 			int s = splimp();
611df8bae1dSRodney W. Grimes 			if_up(ifp);
612df8bae1dSRodney W. Grimes 			splx(s);
613df8bae1dSRodney W. Grimes 		}
614df8bae1dSRodney W. Grimes 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
615df8bae1dSRodney W. Grimes 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
616df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl)
617df8bae1dSRodney W. Grimes 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
61898b9590eSPoul-Henning Kamp 		getmicrotime(&ifp->if_lastchange);
619df8bae1dSRodney W. Grimes 		break;
620df8bae1dSRodney W. Grimes 
621df8bae1dSRodney W. Grimes 	case SIOCSIFMETRIC:
6229448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
6239448326fSPoul-Henning Kamp 		if (error)
624df8bae1dSRodney W. Grimes 			return (error);
625df8bae1dSRodney W. Grimes 		ifp->if_metric = ifr->ifr_metric;
62698b9590eSPoul-Henning Kamp 		getmicrotime(&ifp->if_lastchange);
627df8bae1dSRodney W. Grimes 		break;
628df8bae1dSRodney W. Grimes 
629074c4a4eSGarrett Wollman 	case SIOCSIFPHYS:
630074c4a4eSGarrett Wollman 		error = suser(p->p_ucred, &p->p_acflag);
631e39a0280SGary Palmer 		if (error)
632e39a0280SGary Palmer 			return error;
633e39a0280SGary Palmer 		if (!ifp->if_ioctl)
634e39a0280SGary Palmer 		        return EOPNOTSUPP;
635e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
636e39a0280SGary Palmer 		if (error == 0)
63798b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
638e39a0280SGary Palmer 		return(error);
639074c4a4eSGarrett Wollman 
640a7028af7SDavid Greenman 	case SIOCSIFMTU:
6419448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
6429448326fSPoul-Henning Kamp 		if (error)
643a7028af7SDavid Greenman 			return (error);
644a7028af7SDavid Greenman 		if (ifp->if_ioctl == NULL)
645a7028af7SDavid Greenman 			return (EOPNOTSUPP);
64666025569SDavid Greenman 		/*
64766025569SDavid Greenman 		 * 72 was chosen below because it is the size of a TCP/IP
64866025569SDavid Greenman 		 * header (40) + the minimum mss (32).
64966025569SDavid Greenman 		 */
65066025569SDavid Greenman 		if (ifr->ifr_mtu < 72 || ifr->ifr_mtu > 65535)
65175ee03cbSDavid Greenman 			return (EINVAL);
652e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
653e39a0280SGary Palmer 		if (error == 0)
65498b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
655e39a0280SGary Palmer 		return(error);
656a7028af7SDavid Greenman 
657df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
658df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
6599448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
6609448326fSPoul-Henning Kamp 		if (error)
661df8bae1dSRodney W. Grimes 			return (error);
662477180fbSGarrett Wollman 
663477180fbSGarrett Wollman 		/* Don't allow group membership on non-multicast interfaces. */
664477180fbSGarrett Wollman 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
665477180fbSGarrett Wollman 			return EOPNOTSUPP;
666477180fbSGarrett Wollman 
667477180fbSGarrett Wollman 		/* Don't let users screw up protocols' entries. */
668477180fbSGarrett Wollman 		if (ifr->ifr_addr.sa_family != AF_LINK)
669477180fbSGarrett Wollman 			return EINVAL;
670477180fbSGarrett Wollman 
671477180fbSGarrett Wollman 		if (cmd == SIOCADDMULTI) {
672477180fbSGarrett Wollman 			struct ifmultiaddr *ifma;
673477180fbSGarrett Wollman 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
674477180fbSGarrett Wollman 		} else {
675477180fbSGarrett Wollman 			error = if_delmulti(ifp, &ifr->ifr_addr);
676477180fbSGarrett Wollman 		}
677e39a0280SGary Palmer 		if (error == 0)
67898b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
679477180fbSGarrett Wollman 		return error;
680df8bae1dSRodney W. Grimes 
681a912e453SPeter Wemm         case SIOCSIFMEDIA:
682d7189ec6SJoerg Wunsch 	case SIOCSIFGENERIC:
683a912e453SPeter Wemm 		error = suser(p->p_ucred, &p->p_acflag);
684a912e453SPeter Wemm 		if (error)
685a912e453SPeter Wemm 			return (error);
686a912e453SPeter Wemm 		if (ifp->if_ioctl == 0)
687a912e453SPeter Wemm 			return (EOPNOTSUPP);
688a912e453SPeter Wemm 		error = (*ifp->if_ioctl)(ifp, cmd, data);
689a912e453SPeter Wemm 		if (error == 0)
69098b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
691a912e453SPeter Wemm 		return error;
692a912e453SPeter Wemm 
693a912e453SPeter Wemm 	case SIOCGIFMEDIA:
694d7189ec6SJoerg Wunsch 	case SIOCGIFGENERIC:
695a912e453SPeter Wemm 		if (ifp->if_ioctl == 0)
696a912e453SPeter Wemm 			return (EOPNOTSUPP);
697a912e453SPeter Wemm 		return ((*ifp->if_ioctl)(ifp, cmd, data));
698a912e453SPeter Wemm 
699df8bae1dSRodney W. Grimes 	default:
700df8bae1dSRodney W. Grimes 		if (so->so_proto == 0)
701df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
702df8bae1dSRodney W. Grimes #ifndef COMPAT_43
7032c37256eSGarrett Wollman 		return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
7042c37256eSGarrett Wollman 								 data,
705bc6d9b80SJoerg Wunsch 								 ifp, p));
706df8bae1dSRodney W. Grimes #else
707df8bae1dSRodney W. Grimes 	    {
708df8bae1dSRodney W. Grimes 		int ocmd = cmd;
709df8bae1dSRodney W. Grimes 
710df8bae1dSRodney W. Grimes 		switch (cmd) {
711df8bae1dSRodney W. Grimes 
712df8bae1dSRodney W. Grimes 		case SIOCSIFDSTADDR:
713df8bae1dSRodney W. Grimes 		case SIOCSIFADDR:
714df8bae1dSRodney W. Grimes 		case SIOCSIFBRDADDR:
715df8bae1dSRodney W. Grimes 		case SIOCSIFNETMASK:
716df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN
717df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_family == 0 &&
718df8bae1dSRodney W. Grimes 			    ifr->ifr_addr.sa_len < 16) {
719df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
720df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
721df8bae1dSRodney W. Grimes 			}
722df8bae1dSRodney W. Grimes #else
723df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_len == 0)
724df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
725df8bae1dSRodney W. Grimes #endif
726df8bae1dSRodney W. Grimes 			break;
727df8bae1dSRodney W. Grimes 
728df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
729df8bae1dSRodney W. Grimes 			cmd = SIOCGIFADDR;
730df8bae1dSRodney W. Grimes 			break;
731df8bae1dSRodney W. Grimes 
732df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
733df8bae1dSRodney W. Grimes 			cmd = SIOCGIFDSTADDR;
734df8bae1dSRodney W. Grimes 			break;
735df8bae1dSRodney W. Grimes 
736df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
737df8bae1dSRodney W. Grimes 			cmd = SIOCGIFBRDADDR;
738df8bae1dSRodney W. Grimes 			break;
739df8bae1dSRodney W. Grimes 
740df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
741df8bae1dSRodney W. Grimes 			cmd = SIOCGIFNETMASK;
742df8bae1dSRodney W. Grimes 		}
7432c37256eSGarrett Wollman 		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
7442c37256eSGarrett Wollman 								   cmd,
7452c37256eSGarrett Wollman 								   data,
746a29f300eSGarrett Wollman 								   ifp, p));
747df8bae1dSRodney W. Grimes 		switch (ocmd) {
748df8bae1dSRodney W. Grimes 
749df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
750df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
751df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
752df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
753df8bae1dSRodney W. Grimes 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
754df8bae1dSRodney W. Grimes 		}
755df8bae1dSRodney W. Grimes 		return (error);
756df8bae1dSRodney W. Grimes 
757df8bae1dSRodney W. Grimes 	    }
758df8bae1dSRodney W. Grimes #endif
759df8bae1dSRodney W. Grimes 	}
760df8bae1dSRodney W. Grimes 	return (0);
761df8bae1dSRodney W. Grimes }
762df8bae1dSRodney W. Grimes 
763df8bae1dSRodney W. Grimes /*
764963e4c2aSGarrett Wollman  * Set/clear promiscuous mode on interface ifp based on the truth value
765963e4c2aSGarrett Wollman  * of pswitch.  The calls are reference counted so that only the first
766963e4c2aSGarrett Wollman  * "on" request actually has an effect, as does the final "off" request.
767963e4c2aSGarrett Wollman  * Results are undefined if the "off" and "on" requests are not matched.
768963e4c2aSGarrett Wollman  */
769963e4c2aSGarrett Wollman int
770963e4c2aSGarrett Wollman ifpromisc(ifp, pswitch)
771963e4c2aSGarrett Wollman 	struct ifnet *ifp;
772963e4c2aSGarrett Wollman 	int pswitch;
773963e4c2aSGarrett Wollman {
774963e4c2aSGarrett Wollman 	struct ifreq ifr;
7754a26224cSGarrett Wollman 	int error;
776963e4c2aSGarrett Wollman 
777963e4c2aSGarrett Wollman 	if (pswitch) {
778963e4c2aSGarrett Wollman 		/*
779963e4c2aSGarrett Wollman 		 * If the device is not configured up, we cannot put it in
780963e4c2aSGarrett Wollman 		 * promiscuous mode.
781963e4c2aSGarrett Wollman 		 */
782963e4c2aSGarrett Wollman 		if ((ifp->if_flags & IFF_UP) == 0)
783963e4c2aSGarrett Wollman 			return (ENETDOWN);
784963e4c2aSGarrett Wollman 		if (ifp->if_pcount++ != 0)
785963e4c2aSGarrett Wollman 			return (0);
786963e4c2aSGarrett Wollman 		ifp->if_flags |= IFF_PROMISC;
787e9a30d00SGarrett Wollman 		log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
788963e4c2aSGarrett Wollman 		    ifp->if_name, ifp->if_unit);
789963e4c2aSGarrett Wollman 	} else {
790963e4c2aSGarrett Wollman 		if (--ifp->if_pcount > 0)
791963e4c2aSGarrett Wollman 			return (0);
792963e4c2aSGarrett Wollman 		ifp->if_flags &= ~IFF_PROMISC;
793963e4c2aSGarrett Wollman 	}
794963e4c2aSGarrett Wollman 	ifr.ifr_flags = ifp->if_flags;
7954a26224cSGarrett Wollman 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
7964a26224cSGarrett Wollman 	if (error == 0)
7974a26224cSGarrett Wollman 		rt_ifmsg(ifp);
7984a26224cSGarrett Wollman 	return error;
799963e4c2aSGarrett Wollman }
800963e4c2aSGarrett Wollman 
801963e4c2aSGarrett Wollman /*
802df8bae1dSRodney W. Grimes  * Return interface configuration
803df8bae1dSRodney W. Grimes  * of system.  List may be used
804df8bae1dSRodney W. Grimes  * in later ioctl's (above) to get
805df8bae1dSRodney W. Grimes  * other information.
806df8bae1dSRodney W. Grimes  */
807df8bae1dSRodney W. Grimes /*ARGSUSED*/
8083bda9f9bSPoul-Henning Kamp static int
809df8bae1dSRodney W. Grimes ifconf(cmd, data)
810ecbb00a2SDoug Rabson 	u_long cmd;
811df8bae1dSRodney W. Grimes 	caddr_t data;
812df8bae1dSRodney W. Grimes {
813df8bae1dSRodney W. Grimes 	register struct ifconf *ifc = (struct ifconf *)data;
81429412182SGarrett Wollman 	register struct ifnet *ifp = ifnet.tqh_first;
815df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
816df8bae1dSRodney W. Grimes 	struct ifreq ifr, *ifrp;
817df8bae1dSRodney W. Grimes 	int space = ifc->ifc_len, error = 0;
818df8bae1dSRodney W. Grimes 
819df8bae1dSRodney W. Grimes 	ifrp = ifc->ifc_req;
82029412182SGarrett Wollman 	for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) {
8211ce9bf88SPoul-Henning Kamp 		char workbuf[64];
8221ce9bf88SPoul-Henning Kamp 		int ifnlen;
8232624cf89SGarrett Wollman 
8242127f260SArchie Cobbs 		ifnlen = snprintf(workbuf, sizeof(workbuf),
8252127f260SArchie Cobbs 		    "%s%d", ifp->if_name, ifp->if_unit);
8261ce9bf88SPoul-Henning Kamp 		if(ifnlen + 1 > sizeof ifr.ifr_name) {
8272624cf89SGarrett Wollman 			error = ENAMETOOLONG;
8282624cf89SGarrett Wollman 		} else {
8291ce9bf88SPoul-Henning Kamp 			strcpy(ifr.ifr_name, workbuf);
8302624cf89SGarrett Wollman 		}
8312624cf89SGarrett Wollman 
83259562606SGarrett Wollman 		if ((ifa = ifp->if_addrhead.tqh_first) == 0) {
833df8bae1dSRodney W. Grimes 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
834df8bae1dSRodney W. Grimes 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
835df8bae1dSRodney W. Grimes 			    sizeof (ifr));
836df8bae1dSRodney W. Grimes 			if (error)
837df8bae1dSRodney W. Grimes 				break;
838df8bae1dSRodney W. Grimes 			space -= sizeof (ifr), ifrp++;
839df8bae1dSRodney W. Grimes 		} else
84059562606SGarrett Wollman 		    for ( ; space > sizeof (ifr) && ifa;
84159562606SGarrett Wollman 			 ifa = ifa->ifa_link.tqe_next) {
842df8bae1dSRodney W. Grimes 			register struct sockaddr *sa = ifa->ifa_addr;
843df8bae1dSRodney W. Grimes #ifdef COMPAT_43
844df8bae1dSRodney W. Grimes 			if (cmd == OSIOCGIFCONF) {
845df8bae1dSRodney W. Grimes 				struct osockaddr *osa =
846df8bae1dSRodney W. Grimes 					 (struct osockaddr *)&ifr.ifr_addr;
847df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
848df8bae1dSRodney W. Grimes 				osa->sa_family = sa->sa_family;
849df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
850df8bae1dSRodney W. Grimes 						sizeof (ifr));
851df8bae1dSRodney W. Grimes 				ifrp++;
852df8bae1dSRodney W. Grimes 			} else
853df8bae1dSRodney W. Grimes #endif
854df8bae1dSRodney W. Grimes 			if (sa->sa_len <= sizeof(*sa)) {
855df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
856df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
857df8bae1dSRodney W. Grimes 						sizeof (ifr));
858df8bae1dSRodney W. Grimes 				ifrp++;
859df8bae1dSRodney W. Grimes 			} else {
860df8bae1dSRodney W. Grimes 				space -= sa->sa_len - sizeof(*sa);
861df8bae1dSRodney W. Grimes 				if (space < sizeof (ifr))
862df8bae1dSRodney W. Grimes 					break;
863df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
864df8bae1dSRodney W. Grimes 						sizeof (ifr.ifr_name));
865df8bae1dSRodney W. Grimes 				if (error == 0)
866df8bae1dSRodney W. Grimes 				    error = copyout((caddr_t)sa,
867df8bae1dSRodney W. Grimes 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
868df8bae1dSRodney W. Grimes 				ifrp = (struct ifreq *)
869df8bae1dSRodney W. Grimes 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
870df8bae1dSRodney W. Grimes 			}
871df8bae1dSRodney W. Grimes 			if (error)
872df8bae1dSRodney W. Grimes 				break;
873df8bae1dSRodney W. Grimes 			space -= sizeof (ifr);
874df8bae1dSRodney W. Grimes 		}
875df8bae1dSRodney W. Grimes 	}
876df8bae1dSRodney W. Grimes 	ifc->ifc_len -= space;
877df8bae1dSRodney W. Grimes 	return (error);
878df8bae1dSRodney W. Grimes }
879df8bae1dSRodney W. Grimes 
8801158dfb7SGarrett Wollman /*
8811158dfb7SGarrett Wollman  * Just like if_promisc(), but for all-multicast-reception mode.
8821158dfb7SGarrett Wollman  */
8831158dfb7SGarrett Wollman int
8841158dfb7SGarrett Wollman if_allmulti(ifp, onswitch)
8851158dfb7SGarrett Wollman 	struct ifnet *ifp;
8861158dfb7SGarrett Wollman 	int onswitch;
8871158dfb7SGarrett Wollman {
8881158dfb7SGarrett Wollman 	int error = 0;
8891158dfb7SGarrett Wollman 	int s = splimp();
8901158dfb7SGarrett Wollman 
8911158dfb7SGarrett Wollman 	if (onswitch) {
8921158dfb7SGarrett Wollman 		if (ifp->if_amcount++ == 0) {
8931158dfb7SGarrett Wollman 			ifp->if_flags |= IFF_ALLMULTI;
8941158dfb7SGarrett Wollman 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
8951158dfb7SGarrett Wollman 		}
8961158dfb7SGarrett Wollman 	} else {
8971158dfb7SGarrett Wollman 		if (ifp->if_amcount > 1) {
8981158dfb7SGarrett Wollman 			ifp->if_amcount--;
8991158dfb7SGarrett Wollman 		} else {
9001158dfb7SGarrett Wollman 			ifp->if_amcount = 0;
9011158dfb7SGarrett Wollman 			ifp->if_flags &= ~IFF_ALLMULTI;
9021158dfb7SGarrett Wollman 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
9031158dfb7SGarrett Wollman 		}
9041158dfb7SGarrett Wollman 	}
9051158dfb7SGarrett Wollman 	splx(s);
9064a26224cSGarrett Wollman 
9074a26224cSGarrett Wollman 	if (error == 0)
9084a26224cSGarrett Wollman 		rt_ifmsg(ifp);
9091158dfb7SGarrett Wollman 	return error;
9101158dfb7SGarrett Wollman }
9111158dfb7SGarrett Wollman 
9121158dfb7SGarrett Wollman /*
9131158dfb7SGarrett Wollman  * Add a multicast listenership to the interface in question.
9141158dfb7SGarrett Wollman  * The link layer provides a routine which converts
9151158dfb7SGarrett Wollman  */
9161158dfb7SGarrett Wollman int
917373f88edSGarrett Wollman if_addmulti(ifp, sa, retifma)
9181158dfb7SGarrett Wollman 	struct ifnet *ifp;	/* interface to manipulate */
9191158dfb7SGarrett Wollman 	struct sockaddr *sa;	/* address to add */
920b2053118SGarrett Wollman 	struct ifmultiaddr **retifma;
9211158dfb7SGarrett Wollman {
9221158dfb7SGarrett Wollman 	struct sockaddr *llsa, *dupsa;
9231158dfb7SGarrett Wollman 	int error, s;
9241158dfb7SGarrett Wollman 	struct ifmultiaddr *ifma;
9251158dfb7SGarrett Wollman 
92657af7922SJulian Elischer 	/*
92757af7922SJulian Elischer 	 * If the matching multicast address already exists
92857af7922SJulian Elischer 	 * then don't add a new one, just add a reference
92957af7922SJulian Elischer 	 */
9301158dfb7SGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
9311158dfb7SGarrett Wollman 	     ifma = ifma->ifma_link.le_next) {
93257af7922SJulian Elischer 		if (equal(sa, ifma->ifma_addr)) {
9331158dfb7SGarrett Wollman 			ifma->ifma_refcount++;
93457af7922SJulian Elischer 			if (retifma)
93557af7922SJulian Elischer 				*retifma = ifma;
9361158dfb7SGarrett Wollman 			return 0;
9371158dfb7SGarrett Wollman 		}
93857af7922SJulian Elischer 	}
9391158dfb7SGarrett Wollman 
9401158dfb7SGarrett Wollman 	/*
9411158dfb7SGarrett Wollman 	 * Give the link layer a chance to accept/reject it, and also
9421158dfb7SGarrett Wollman 	 * find out which AF_LINK address this maps to, if it isn't one
9431158dfb7SGarrett Wollman 	 * already.
9441158dfb7SGarrett Wollman 	 */
9451158dfb7SGarrett Wollman 	if (ifp->if_resolvemulti) {
9461158dfb7SGarrett Wollman 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
9471158dfb7SGarrett Wollman 		if (error) return error;
9481158dfb7SGarrett Wollman 	} else {
9491158dfb7SGarrett Wollman 		llsa = 0;
9501158dfb7SGarrett Wollman 	}
9511158dfb7SGarrett Wollman 
9521158dfb7SGarrett Wollman 	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
9531158dfb7SGarrett Wollman 	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
9541158dfb7SGarrett Wollman 	bcopy(sa, dupsa, sa->sa_len);
9551158dfb7SGarrett Wollman 
9561158dfb7SGarrett Wollman 	ifma->ifma_addr = dupsa;
9571158dfb7SGarrett Wollman 	ifma->ifma_lladdr = llsa;
9581158dfb7SGarrett Wollman 	ifma->ifma_ifp = ifp;
9591158dfb7SGarrett Wollman 	ifma->ifma_refcount = 1;
960373f88edSGarrett Wollman 	ifma->ifma_protospec = 0;
961477180fbSGarrett Wollman 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
962373f88edSGarrett Wollman 
9631158dfb7SGarrett Wollman 	/*
9641158dfb7SGarrett Wollman 	 * Some network interfaces can scan the address list at
9651158dfb7SGarrett Wollman 	 * interrupt time; lock them out.
9661158dfb7SGarrett Wollman 	 */
9671158dfb7SGarrett Wollman 	s = splimp();
9681158dfb7SGarrett Wollman 	LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
9691158dfb7SGarrett Wollman 	splx(s);
970373f88edSGarrett Wollman 	*retifma = ifma;
9711158dfb7SGarrett Wollman 
9721158dfb7SGarrett Wollman 	if (llsa != 0) {
9731158dfb7SGarrett Wollman 		for (ifma = ifp->if_multiaddrs.lh_first; ifma;
9741158dfb7SGarrett Wollman 		     ifma = ifma->ifma_link.le_next) {
9751158dfb7SGarrett Wollman 			if (equal(ifma->ifma_addr, llsa))
9761158dfb7SGarrett Wollman 				break;
9771158dfb7SGarrett Wollman 		}
9781158dfb7SGarrett Wollman 		if (ifma) {
9791158dfb7SGarrett Wollman 			ifma->ifma_refcount++;
9801158dfb7SGarrett Wollman 		} else {
9811158dfb7SGarrett Wollman 			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
9821158dfb7SGarrett Wollman 			       M_IFMADDR, M_WAITOK);
983477180fbSGarrett Wollman 			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
984477180fbSGarrett Wollman 			       M_IFMADDR, M_WAITOK);
985477180fbSGarrett Wollman 			bcopy(llsa, dupsa, llsa->sa_len);
986477180fbSGarrett Wollman 			ifma->ifma_addr = dupsa;
9871158dfb7SGarrett Wollman 			ifma->ifma_ifp = ifp;
9881158dfb7SGarrett Wollman 			ifma->ifma_refcount = 1;
9891158dfb7SGarrett Wollman 			s = splimp();
9901158dfb7SGarrett Wollman 			LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
9911158dfb7SGarrett Wollman 			splx(s);
9921158dfb7SGarrett Wollman 		}
99357af7922SJulian Elischer 	}
9941158dfb7SGarrett Wollman 	/*
9951158dfb7SGarrett Wollman 	 * We are certain we have added something, so call down to the
9961158dfb7SGarrett Wollman 	 * interface to let them know about it.
9971158dfb7SGarrett Wollman 	 */
9981158dfb7SGarrett Wollman 	s = splimp();
9991158dfb7SGarrett Wollman 	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
10001158dfb7SGarrett Wollman 	splx(s);
10011158dfb7SGarrett Wollman 
10021158dfb7SGarrett Wollman 	return 0;
10031158dfb7SGarrett Wollman }
10041158dfb7SGarrett Wollman 
10051158dfb7SGarrett Wollman /*
10061158dfb7SGarrett Wollman  * Remove a reference to a multicast address on this interface.  Yell
10071158dfb7SGarrett Wollman  * if the request does not match an existing membership.
10081158dfb7SGarrett Wollman  */
10091158dfb7SGarrett Wollman int
10101158dfb7SGarrett Wollman if_delmulti(ifp, sa)
10111158dfb7SGarrett Wollman 	struct ifnet *ifp;
10121158dfb7SGarrett Wollman 	struct sockaddr *sa;
10131158dfb7SGarrett Wollman {
10141158dfb7SGarrett Wollman 	struct ifmultiaddr *ifma;
10151158dfb7SGarrett Wollman 	int s;
10161158dfb7SGarrett Wollman 
10171158dfb7SGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
10181158dfb7SGarrett Wollman 	     ifma = ifma->ifma_link.le_next)
10191158dfb7SGarrett Wollman 		if (equal(sa, ifma->ifma_addr))
10201158dfb7SGarrett Wollman 			break;
10211158dfb7SGarrett Wollman 	if (ifma == 0)
10221158dfb7SGarrett Wollman 		return ENOENT;
10231158dfb7SGarrett Wollman 
10241158dfb7SGarrett Wollman 	if (ifma->ifma_refcount > 1) {
10251158dfb7SGarrett Wollman 		ifma->ifma_refcount--;
10261158dfb7SGarrett Wollman 		return 0;
10271158dfb7SGarrett Wollman 	}
10281158dfb7SGarrett Wollman 
1029477180fbSGarrett Wollman 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
10301158dfb7SGarrett Wollman 	sa = ifma->ifma_lladdr;
10311158dfb7SGarrett Wollman 	s = splimp();
10321158dfb7SGarrett Wollman 	LIST_REMOVE(ifma, ifma_link);
10331158dfb7SGarrett Wollman 	splx(s);
10341158dfb7SGarrett Wollman 	free(ifma->ifma_addr, M_IFMADDR);
10351158dfb7SGarrett Wollman 	free(ifma, M_IFMADDR);
10361158dfb7SGarrett Wollman 	if (sa == 0)
10371158dfb7SGarrett Wollman 		return 0;
10381158dfb7SGarrett Wollman 
10391158dfb7SGarrett Wollman 	/*
10401158dfb7SGarrett Wollman 	 * Now look for the link-layer address which corresponds to
10411158dfb7SGarrett Wollman 	 * this network address.  It had been squirreled away in
10421158dfb7SGarrett Wollman 	 * ifma->ifma_lladdr for this purpose (so we don't have
10431158dfb7SGarrett Wollman 	 * to call ifp->if_resolvemulti() again), and we saved that
10441158dfb7SGarrett Wollman 	 * value in sa above.  If some nasty deleted the
10451158dfb7SGarrett Wollman 	 * link-layer address out from underneath us, we can deal because
10461158dfb7SGarrett Wollman 	 * the address we stored was is not the same as the one which was
10471158dfb7SGarrett Wollman 	 * in the record for the link-layer address.  (So we don't complain
10481158dfb7SGarrett Wollman 	 * in that case.)
10491158dfb7SGarrett Wollman 	 */
10501158dfb7SGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
10511158dfb7SGarrett Wollman 	     ifma = ifma->ifma_link.le_next)
10521158dfb7SGarrett Wollman 		if (equal(sa, ifma->ifma_addr))
10531158dfb7SGarrett Wollman 			break;
10541158dfb7SGarrett Wollman 	if (ifma == 0)
10551158dfb7SGarrett Wollman 		return 0;
10561158dfb7SGarrett Wollman 
10571158dfb7SGarrett Wollman 	if (ifma->ifma_refcount > 1) {
10581158dfb7SGarrett Wollman 		ifma->ifma_refcount--;
10591158dfb7SGarrett Wollman 		return 0;
10601158dfb7SGarrett Wollman 	}
10611158dfb7SGarrett Wollman 
10621158dfb7SGarrett Wollman 	s = splimp();
10631158dfb7SGarrett Wollman 	LIST_REMOVE(ifma, ifma_link);
1064c7323482SBill Paul 	ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
10651158dfb7SGarrett Wollman 	splx(s);
10661158dfb7SGarrett Wollman 	free(ifma->ifma_addr, M_IFMADDR);
10671158dfb7SGarrett Wollman 	free(sa, M_IFMADDR);
10681158dfb7SGarrett Wollman 	free(ifma, M_IFMADDR);
10691158dfb7SGarrett Wollman 
10701158dfb7SGarrett Wollman 	return 0;
10711158dfb7SGarrett Wollman }
10721158dfb7SGarrett Wollman 
1073373f88edSGarrett Wollman struct ifmultiaddr *
1074373f88edSGarrett Wollman ifmaof_ifpforaddr(sa, ifp)
1075373f88edSGarrett Wollman 	struct sockaddr *sa;
1076373f88edSGarrett Wollman 	struct ifnet *ifp;
1077373f88edSGarrett Wollman {
1078373f88edSGarrett Wollman 	struct ifmultiaddr *ifma;
1079373f88edSGarrett Wollman 
1080373f88edSGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1081373f88edSGarrett Wollman 	     ifma = ifma->ifma_link.le_next)
1082373f88edSGarrett Wollman 		if (equal(ifma->ifma_addr, sa))
1083373f88edSGarrett Wollman 			break;
1084373f88edSGarrett Wollman 
1085373f88edSGarrett Wollman 	return ifma;
1086373f88edSGarrett Wollman }
1087373f88edSGarrett Wollman 
1088602d513cSGarrett Wollman SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
10892c37256eSGarrett Wollman SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
1090