xref: /freebsd/sys/net/if.c (revision aab3beeede3032eef69a9bf581707f4ea77affe3)
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
34aab3beeeSBrian Somers  *	$Id: if.c,v 1.73 1999/06/19 18:42:26 phk 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.
79df8bae1dSRodney W. Grimes  */
802b14f991SJulian Elischer /* ARGSUSED*/
81df8bae1dSRodney W. Grimes void
8227501cb6SBruce Evans ifinit(dummy)
8327501cb6SBruce Evans 	void *dummy;
84df8bae1dSRodney W. Grimes {
858ba5bdaeSPeter Wemm 	struct ifnet *ifp;
868ba5bdaeSPeter Wemm 	int s;
87df8bae1dSRodney W. Grimes 
888ba5bdaeSPeter Wemm 	s = splimp();
8929412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
90e0ea20bcSPoul-Henning Kamp 		if (ifp->if_snd.ifq_maxlen == 0) {
91e0ea20bcSPoul-Henning Kamp 			printf("%s%d XXX: driver didn't set ifq_maxlen\n",
92e0ea20bcSPoul-Henning Kamp 			    ifp->if_name, ifp->if_unit);
93df8bae1dSRodney W. Grimes 			ifp->if_snd.ifq_maxlen = ifqmaxlen;
94e0ea20bcSPoul-Henning Kamp 		}
958ba5bdaeSPeter Wemm 	splx(s);
96df8bae1dSRodney W. Grimes 	if_slowtimo(0);
97df8bae1dSRodney W. Grimes }
98df8bae1dSRodney W. Grimes 
99bbd17bf8SGarrett Wollman int if_index = 0;
100bbd17bf8SGarrett Wollman struct ifaddr **ifnet_addrs;
1013bda9f9bSPoul-Henning Kamp 
102df8bae1dSRodney W. Grimes 
103df8bae1dSRodney W. Grimes /*
104df8bae1dSRodney W. Grimes  * Attach an interface to the
105df8bae1dSRodney W. Grimes  * list of "active" interfaces.
106df8bae1dSRodney W. Grimes  */
107df8bae1dSRodney W. Grimes void
108df8bae1dSRodney W. Grimes if_attach(ifp)
109df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
110df8bae1dSRodney W. Grimes {
111df8bae1dSRodney W. Grimes 	unsigned socksize, ifasize;
1121ce9bf88SPoul-Henning Kamp 	int namelen, masklen;
1131ce9bf88SPoul-Henning Kamp 	char workbuf[64];
114df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
115df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
116df8bae1dSRodney W. Grimes 	static int if_indexlim = 8;
11729412182SGarrett Wollman 	static int inited;
118f23b4c91SGarrett Wollman 
11929412182SGarrett Wollman 	if (!inited) {
12029412182SGarrett Wollman 		TAILQ_INIT(&ifnet);
12129412182SGarrett Wollman 		inited = 1;
12229412182SGarrett Wollman 	}
123df8bae1dSRodney W. Grimes 
12429412182SGarrett Wollman 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
125df8bae1dSRodney W. Grimes 	ifp->if_index = ++if_index;
12659562606SGarrett Wollman 	/*
12759562606SGarrett Wollman 	 * XXX -
12859562606SGarrett Wollman 	 * The old code would work if the interface passed a pre-existing
12959562606SGarrett Wollman 	 * chain of ifaddrs to this code.  We don't trust our callers to
13059562606SGarrett Wollman 	 * properly initialize the tailq, however, so we no longer allow
13159562606SGarrett Wollman 	 * this unlikely case.
13259562606SGarrett Wollman 	 */
13359562606SGarrett Wollman 	TAILQ_INIT(&ifp->if_addrhead);
1341158dfb7SGarrett Wollman 	LIST_INIT(&ifp->if_multiaddrs);
13598b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
136df8bae1dSRodney W. Grimes 	if (ifnet_addrs == 0 || if_index >= if_indexlim) {
137df8bae1dSRodney W. Grimes 		unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
138df8bae1dSRodney W. Grimes 		struct ifaddr **q = (struct ifaddr **)
139df8bae1dSRodney W. Grimes 					malloc(n, M_IFADDR, M_WAITOK);
14073c2ab46SDavid Greenman 		bzero((caddr_t)q, n);
141df8bae1dSRodney W. Grimes 		if (ifnet_addrs) {
142df8bae1dSRodney W. Grimes 			bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
143df8bae1dSRodney W. Grimes 			free((caddr_t)ifnet_addrs, M_IFADDR);
144df8bae1dSRodney W. Grimes 		}
145df8bae1dSRodney W. Grimes 		ifnet_addrs = q;
146df8bae1dSRodney W. Grimes 	}
147df8bae1dSRodney W. Grimes 	/*
148df8bae1dSRodney W. Grimes 	 * create a Link Level name for this device
149df8bae1dSRodney W. Grimes 	 */
1502127f260SArchie Cobbs 	namelen = snprintf(workbuf, sizeof(workbuf),
1512127f260SArchie Cobbs 	    "%s%d", ifp->if_name, ifp->if_unit);
152df8bae1dSRodney W. Grimes #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
1531ce9bf88SPoul-Henning Kamp 	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
154df8bae1dSRodney W. Grimes 	socksize = masklen + ifp->if_addrlen;
155df8bae1dSRodney W. Grimes #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
156df8bae1dSRodney W. Grimes 	if (socksize < sizeof(*sdl))
157df8bae1dSRodney W. Grimes 		socksize = sizeof(*sdl);
1588a261b8fSDoug Rabson 	socksize = ROUNDUP(socksize);
159df8bae1dSRodney W. Grimes 	ifasize = sizeof(*ifa) + 2 * socksize;
1609448326fSPoul-Henning Kamp 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
1619448326fSPoul-Henning Kamp 	if (ifa) {
162df8bae1dSRodney W. Grimes 		bzero((caddr_t)ifa, ifasize);
163df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(ifa + 1);
164df8bae1dSRodney W. Grimes 		sdl->sdl_len = socksize;
165df8bae1dSRodney W. Grimes 		sdl->sdl_family = AF_LINK;
1661ce9bf88SPoul-Henning Kamp 		bcopy(workbuf, sdl->sdl_data, namelen);
1671ce9bf88SPoul-Henning Kamp 		sdl->sdl_nlen = namelen;
168df8bae1dSRodney W. Grimes 		sdl->sdl_index = ifp->if_index;
169df8bae1dSRodney W. Grimes 		sdl->sdl_type = ifp->if_type;
170df8bae1dSRodney W. Grimes 		ifnet_addrs[if_index - 1] = ifa;
171df8bae1dSRodney W. Grimes 		ifa->ifa_ifp = ifp;
172df8bae1dSRodney W. Grimes 		ifa->ifa_rtrequest = link_rtrequest;
173df8bae1dSRodney W. Grimes 		ifa->ifa_addr = (struct sockaddr *)sdl;
174df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
175df8bae1dSRodney W. Grimes 		ifa->ifa_netmask = (struct sockaddr *)sdl;
176df8bae1dSRodney W. Grimes 		sdl->sdl_len = masklen;
177df8bae1dSRodney W. Grimes 		while (namelen != 0)
178df8bae1dSRodney W. Grimes 			sdl->sdl_data[--namelen] = 0xff;
17959562606SGarrett Wollman 		TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
180df8bae1dSRodney W. Grimes 	}
181df8bae1dSRodney W. Grimes }
1826182fdbdSPeter Wemm 
1836182fdbdSPeter Wemm /*
1846182fdbdSPeter Wemm  * Detach an interface, removing it from the
1856182fdbdSPeter Wemm  * list of "active" interfaces.
1866182fdbdSPeter Wemm  */
1876182fdbdSPeter Wemm void
1886182fdbdSPeter Wemm if_detach(ifp)
1896182fdbdSPeter Wemm 	struct ifnet *ifp;
1906182fdbdSPeter Wemm {
1916182fdbdSPeter Wemm 	struct ifaddr *ifa;
1926182fdbdSPeter Wemm 
1936182fdbdSPeter Wemm 	/*
1946182fdbdSPeter Wemm 	 * Remove routes and flush queues.
1956182fdbdSPeter Wemm 	 */
1966182fdbdSPeter Wemm 	if_down(ifp);
1976182fdbdSPeter Wemm 
1986182fdbdSPeter Wemm 	/*
1996182fdbdSPeter Wemm 	 * Remove address from ifnet_addrs[] and maybe decrement if_index.
2006182fdbdSPeter Wemm 	 * Clean up all addresses.
2016182fdbdSPeter Wemm 	 */
2026182fdbdSPeter Wemm 	ifnet_addrs[ifp->if_index] = 0;
2036182fdbdSPeter Wemm 	while (ifnet_addrs[if_index] == 0)
2046182fdbdSPeter Wemm 		if_index--;
2056182fdbdSPeter Wemm 
2066182fdbdSPeter Wemm 	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
2076182fdbdSPeter Wemm 	     ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
2086182fdbdSPeter Wemm 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
2096182fdbdSPeter Wemm 		IFAFREE(ifa);
2106182fdbdSPeter Wemm 	}
2116182fdbdSPeter Wemm 
2126182fdbdSPeter Wemm 	TAILQ_REMOVE(&ifnet, ifp, if_link);
2136182fdbdSPeter Wemm }
2146182fdbdSPeter Wemm 
215df8bae1dSRodney W. Grimes /*
216df8bae1dSRodney W. Grimes  * Locate an interface based on a complete address.
217df8bae1dSRodney W. Grimes  */
218df8bae1dSRodney W. Grimes /*ARGSUSED*/
219df8bae1dSRodney W. Grimes struct ifaddr *
220df8bae1dSRodney W. Grimes ifa_ifwithaddr(addr)
221df8bae1dSRodney W. Grimes 	register struct sockaddr *addr;
222df8bae1dSRodney W. Grimes {
223df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
224df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
225df8bae1dSRodney W. Grimes 
226df8bae1dSRodney W. Grimes #define	equal(a1, a2) \
227df8bae1dSRodney W. Grimes   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
22829412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
22959562606SGarrett Wollman 	    for (ifa = ifp->if_addrhead.tqh_first; ifa;
23059562606SGarrett Wollman 		 ifa = ifa->ifa_link.tqe_next) {
231df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != addr->sa_family)
232df8bae1dSRodney W. Grimes 			continue;
233df8bae1dSRodney W. Grimes 		if (equal(addr, ifa->ifa_addr))
234df8bae1dSRodney W. Grimes 			return (ifa);
235df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
236df8bae1dSRodney W. Grimes 		    equal(ifa->ifa_broadaddr, addr))
237df8bae1dSRodney W. Grimes 			return (ifa);
238df8bae1dSRodney W. Grimes 	}
239df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
240df8bae1dSRodney W. Grimes }
241df8bae1dSRodney W. Grimes /*
242df8bae1dSRodney W. Grimes  * Locate the point to point interface with a given destination address.
243df8bae1dSRodney W. Grimes  */
244df8bae1dSRodney W. Grimes /*ARGSUSED*/
245df8bae1dSRodney W. Grimes struct ifaddr *
246df8bae1dSRodney W. Grimes ifa_ifwithdstaddr(addr)
247df8bae1dSRodney W. Grimes 	register struct sockaddr *addr;
248df8bae1dSRodney W. Grimes {
249df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
250df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
251df8bae1dSRodney W. Grimes 
25229412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
253df8bae1dSRodney W. Grimes 	    if (ifp->if_flags & IFF_POINTOPOINT)
25459562606SGarrett Wollman 		for (ifa = ifp->if_addrhead.tqh_first; ifa;
25559562606SGarrett Wollman 		     ifa = ifa->ifa_link.tqe_next) {
256df8bae1dSRodney W. Grimes 			if (ifa->ifa_addr->sa_family != addr->sa_family)
257df8bae1dSRodney W. Grimes 				continue;
25855088a1cSDavid Greenman 			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
259df8bae1dSRodney W. Grimes 				return (ifa);
260df8bae1dSRodney W. Grimes 	}
261df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
262df8bae1dSRodney W. Grimes }
263df8bae1dSRodney W. Grimes 
264df8bae1dSRodney W. Grimes /*
265df8bae1dSRodney W. Grimes  * Find an interface on a specific network.  If many, choice
266df8bae1dSRodney W. Grimes  * is most specific found.
267df8bae1dSRodney W. Grimes  */
268df8bae1dSRodney W. Grimes struct ifaddr *
269df8bae1dSRodney W. Grimes ifa_ifwithnet(addr)
270df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
271df8bae1dSRodney W. Grimes {
272df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
273df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
274df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
275df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
276df8bae1dSRodney W. Grimes 	char *addr_data = addr->sa_data, *cplim;
277df8bae1dSRodney W. Grimes 
2787e2a6151SJulian Elischer 	/*
2797e2a6151SJulian Elischer 	 * AF_LINK addresses can be looked up directly by their index number,
2807e2a6151SJulian Elischer 	 * so do that if we can.
2817e2a6151SJulian Elischer 	 */
282df8bae1dSRodney W. Grimes 	if (af == AF_LINK) {
283df8bae1dSRodney W. Grimes 	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
284df8bae1dSRodney W. Grimes 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
285df8bae1dSRodney W. Grimes 		return (ifnet_addrs[sdl->sdl_index - 1]);
286df8bae1dSRodney W. Grimes 	}
2877e2a6151SJulian Elischer 
2887e2a6151SJulian Elischer 	/*
2897e2a6151SJulian Elischer 	 * Scan though each interface, looking for ones that have
2907e2a6151SJulian Elischer 	 * addresses in this address family.
2917e2a6151SJulian Elischer 	 */
29229412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
29359562606SGarrett Wollman 		for (ifa = ifp->if_addrhead.tqh_first; ifa;
29459562606SGarrett Wollman 		     ifa = ifa->ifa_link.tqe_next) {
295df8bae1dSRodney W. Grimes 			register char *cp, *cp2, *cp3;
296df8bae1dSRodney W. Grimes 
297523a02aaSDavid Greenman 			if (ifa->ifa_addr->sa_family != af)
298df8bae1dSRodney W. Grimes next:				continue;
299b2af64fdSDavid Greenman 			if (ifp->if_flags & IFF_POINTOPOINT) {
3007e2a6151SJulian Elischer 				/*
3017e2a6151SJulian Elischer 				 * This is a bit broken as it doesn't
3027e2a6151SJulian Elischer 				 * take into account that the remote end may
3037e2a6151SJulian Elischer 				 * be a single node in the network we are
3047e2a6151SJulian Elischer 				 * looking for.
3057e2a6151SJulian Elischer 				 * The trouble is that we don't know the
3067e2a6151SJulian Elischer 				 * netmask for the remote end.
3077e2a6151SJulian Elischer 				 */
308fcd6781aSGarrett Wollman 				if (ifa->ifa_dstaddr != 0
309fcd6781aSGarrett Wollman 				    && equal(addr, ifa->ifa_dstaddr))
310b2af64fdSDavid Greenman  					return (ifa);
3113740e2adSDavid Greenman 			} else {
3127e2a6151SJulian Elischer 				/*
3137ed8f465SJulian Elischer 				 * if we have a special address handler,
3147ed8f465SJulian Elischer 				 * then use it instead of the generic one.
3157ed8f465SJulian Elischer 				 */
3167ed8f465SJulian Elischer 	          		if (ifa->ifa_claim_addr) {
3177ed8f465SJulian Elischer 					if ((*ifa->ifa_claim_addr)(ifa, addr)) {
3187ed8f465SJulian Elischer 						return (ifa);
3197ed8f465SJulian Elischer 					} else {
3207ed8f465SJulian Elischer 						continue;
3217ed8f465SJulian Elischer 					}
3227ed8f465SJulian Elischer 				}
3237ed8f465SJulian Elischer 
3247ed8f465SJulian Elischer 				/*
3257e2a6151SJulian Elischer 				 * Scan all the bits in the ifa's address.
3267e2a6151SJulian Elischer 				 * If a bit dissagrees with what we are
3277e2a6151SJulian Elischer 				 * looking for, mask it with the netmask
3287e2a6151SJulian Elischer 				 * to see if it really matters.
3297e2a6151SJulian Elischer 				 * (A byte at a time)
3307e2a6151SJulian Elischer 				 */
331523a02aaSDavid Greenman 				if (ifa->ifa_netmask == 0)
332523a02aaSDavid Greenman 					continue;
333df8bae1dSRodney W. Grimes 				cp = addr_data;
334df8bae1dSRodney W. Grimes 				cp2 = ifa->ifa_addr->sa_data;
335df8bae1dSRodney W. Grimes 				cp3 = ifa->ifa_netmask->sa_data;
3367e2a6151SJulian Elischer 				cplim = ifa->ifa_netmask->sa_len
3377e2a6151SJulian Elischer 					+ (char *)ifa->ifa_netmask;
338df8bae1dSRodney W. Grimes 				while (cp3 < cplim)
339df8bae1dSRodney W. Grimes 					if ((*cp++ ^ *cp2++) & *cp3++)
3407e2a6151SJulian Elischer 						goto next; /* next address! */
3417e2a6151SJulian Elischer 				/*
3427e2a6151SJulian Elischer 				 * If the netmask of what we just found
3437e2a6151SJulian Elischer 				 * is more specific than what we had before
3447e2a6151SJulian Elischer 				 * (if we had one) then remember the new one
3457e2a6151SJulian Elischer 				 * before continuing to search
3467e2a6151SJulian Elischer 				 * for an even better one.
3477e2a6151SJulian Elischer 				 */
348df8bae1dSRodney W. Grimes 				if (ifa_maybe == 0 ||
349df8bae1dSRodney W. Grimes 				    rn_refines((caddr_t)ifa->ifa_netmask,
350df8bae1dSRodney W. Grimes 				    (caddr_t)ifa_maybe->ifa_netmask))
351df8bae1dSRodney W. Grimes 					ifa_maybe = ifa;
352df8bae1dSRodney W. Grimes 			}
353b2af64fdSDavid Greenman 		}
354b2af64fdSDavid Greenman 	}
355df8bae1dSRodney W. Grimes 	return (ifa_maybe);
356df8bae1dSRodney W. Grimes }
357df8bae1dSRodney W. Grimes 
358df8bae1dSRodney W. Grimes /*
359df8bae1dSRodney W. Grimes  * Find an interface address specific to an interface best matching
360df8bae1dSRodney W. Grimes  * a given address.
361df8bae1dSRodney W. Grimes  */
362df8bae1dSRodney W. Grimes struct ifaddr *
363df8bae1dSRodney W. Grimes ifaof_ifpforaddr(addr, ifp)
364df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
365df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
366df8bae1dSRodney W. Grimes {
367df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
368df8bae1dSRodney W. Grimes 	register char *cp, *cp2, *cp3;
369df8bae1dSRodney W. Grimes 	register char *cplim;
370df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = 0;
371df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
372df8bae1dSRodney W. Grimes 
373df8bae1dSRodney W. Grimes 	if (af >= AF_MAX)
374df8bae1dSRodney W. Grimes 		return (0);
37559562606SGarrett Wollman 	for (ifa = ifp->if_addrhead.tqh_first; ifa;
37659562606SGarrett Wollman 	     ifa = ifa->ifa_link.tqe_next) {
377df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != af)
378df8bae1dSRodney W. Grimes 			continue;
379381dd1d2SJulian Elischer 		if (ifa_maybe == 0)
380df8bae1dSRodney W. Grimes 			ifa_maybe = ifa;
381df8bae1dSRodney W. Grimes 		if (ifa->ifa_netmask == 0) {
382df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_addr) ||
383df8bae1dSRodney W. Grimes 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
384df8bae1dSRodney W. Grimes 				return (ifa);
385df8bae1dSRodney W. Grimes 			continue;
386df8bae1dSRodney W. Grimes 		}
387b2af64fdSDavid Greenman 		if (ifp->if_flags & IFF_POINTOPOINT) {
388b2af64fdSDavid Greenman 			if (equal(addr, ifa->ifa_dstaddr))
389b2af64fdSDavid Greenman 				return (ifa);
3903740e2adSDavid Greenman 		} else {
391df8bae1dSRodney W. Grimes 			cp = addr->sa_data;
392df8bae1dSRodney W. Grimes 			cp2 = ifa->ifa_addr->sa_data;
393df8bae1dSRodney W. Grimes 			cp3 = ifa->ifa_netmask->sa_data;
394df8bae1dSRodney W. Grimes 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
395df8bae1dSRodney W. Grimes 			for (; cp3 < cplim; cp3++)
396df8bae1dSRodney W. Grimes 				if ((*cp++ ^ *cp2++) & *cp3)
397df8bae1dSRodney W. Grimes 					break;
398df8bae1dSRodney W. Grimes 			if (cp3 == cplim)
399df8bae1dSRodney W. Grimes 				return (ifa);
400df8bae1dSRodney W. Grimes 		}
401b2af64fdSDavid Greenman 	}
402df8bae1dSRodney W. Grimes 	return (ifa_maybe);
403df8bae1dSRodney W. Grimes }
404df8bae1dSRodney W. Grimes 
405df8bae1dSRodney W. Grimes #include <net/route.h>
406df8bae1dSRodney W. Grimes 
407df8bae1dSRodney W. Grimes /*
408df8bae1dSRodney W. Grimes  * Default action when installing a route with a Link Level gateway.
409df8bae1dSRodney W. Grimes  * Lookup an appropriate real ifa to point to.
410df8bae1dSRodney W. Grimes  * This should be moved to /sys/net/link.c eventually.
411df8bae1dSRodney W. Grimes  */
4123bda9f9bSPoul-Henning Kamp static void
413df8bae1dSRodney W. Grimes link_rtrequest(cmd, rt, sa)
414df8bae1dSRodney W. Grimes 	int cmd;
415df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
416df8bae1dSRodney W. Grimes 	struct sockaddr *sa;
417df8bae1dSRodney W. Grimes {
418df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
419df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
420df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
421df8bae1dSRodney W. Grimes 
422df8bae1dSRodney W. Grimes 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
423df8bae1dSRodney W. Grimes 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
424df8bae1dSRodney W. Grimes 		return;
4259448326fSPoul-Henning Kamp 	ifa = ifaof_ifpforaddr(dst, ifp);
4269448326fSPoul-Henning Kamp 	if (ifa) {
427df8bae1dSRodney W. Grimes 		IFAFREE(rt->rt_ifa);
428df8bae1dSRodney W. Grimes 		rt->rt_ifa = ifa;
429df8bae1dSRodney W. Grimes 		ifa->ifa_refcnt++;
430df8bae1dSRodney W. Grimes 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
431df8bae1dSRodney W. Grimes 			ifa->ifa_rtrequest(cmd, rt, sa);
432df8bae1dSRodney W. Grimes 	}
433df8bae1dSRodney W. Grimes }
434df8bae1dSRodney W. Grimes 
435df8bae1dSRodney W. Grimes /*
436df8bae1dSRodney W. Grimes  * Mark an interface down and notify protocols of
437df8bae1dSRodney W. Grimes  * the transition.
438df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
439df8bae1dSRodney W. Grimes  */
440df8bae1dSRodney W. Grimes void
441e8c2601dSPoul-Henning Kamp if_unroute(ifp, flag, fam)
442df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
443e8c2601dSPoul-Henning Kamp 	int flag, fam;
444df8bae1dSRodney W. Grimes {
445df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
446df8bae1dSRodney W. Grimes 
447e8c2601dSPoul-Henning Kamp 	ifp->if_flags &= ~flag;
44898b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
449e8c2601dSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
450e8c2601dSPoul-Henning Kamp 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
451df8bae1dSRodney W. Grimes 			pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
452df8bae1dSRodney W. Grimes 	if_qflush(&ifp->if_snd);
453df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
454df8bae1dSRodney W. Grimes }
455df8bae1dSRodney W. Grimes 
456df8bae1dSRodney W. Grimes /*
457df8bae1dSRodney W. Grimes  * Mark an interface up and notify protocols of
458df8bae1dSRodney W. Grimes  * the transition.
459df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
460df8bae1dSRodney W. Grimes  */
461df8bae1dSRodney W. Grimes void
462e8c2601dSPoul-Henning Kamp if_route(ifp, flag, fam)
463df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
464e8c2601dSPoul-Henning Kamp 	int flag, fam;
465df8bae1dSRodney W. Grimes {
466176395b2SGarrett Wollman 	register struct ifaddr *ifa;
467df8bae1dSRodney W. Grimes 
468e8c2601dSPoul-Henning Kamp 	ifp->if_flags |= flag;
46998b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
470e8c2601dSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
471e8c2601dSPoul-Henning Kamp 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
472df8bae1dSRodney W. Grimes 			pfctlinput(PRC_IFUP, ifa->ifa_addr);
473df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
474df8bae1dSRodney W. Grimes }
475df8bae1dSRodney W. Grimes 
476df8bae1dSRodney W. Grimes /*
477e8c2601dSPoul-Henning Kamp  * Mark an interface down and notify protocols of
478e8c2601dSPoul-Henning Kamp  * the transition.
479e8c2601dSPoul-Henning Kamp  * NOTE: must be called at splnet or eqivalent.
480e8c2601dSPoul-Henning Kamp  */
481e8c2601dSPoul-Henning Kamp void
482e8c2601dSPoul-Henning Kamp if_down(ifp)
483e8c2601dSPoul-Henning Kamp 	register struct ifnet *ifp;
484e8c2601dSPoul-Henning Kamp {
485e8c2601dSPoul-Henning Kamp 
486e8c2601dSPoul-Henning Kamp 	if_unroute(ifp, IFF_UP, AF_UNSPEC);
487e8c2601dSPoul-Henning Kamp }
488e8c2601dSPoul-Henning Kamp 
489e8c2601dSPoul-Henning Kamp /*
490e8c2601dSPoul-Henning Kamp  * Mark an interface up and notify protocols of
491e8c2601dSPoul-Henning Kamp  * the transition.
492e8c2601dSPoul-Henning Kamp  * NOTE: must be called at splnet or eqivalent.
493e8c2601dSPoul-Henning Kamp  */
494e8c2601dSPoul-Henning Kamp void
495e8c2601dSPoul-Henning Kamp if_up(ifp)
496e8c2601dSPoul-Henning Kamp 	register struct ifnet *ifp;
497e8c2601dSPoul-Henning Kamp {
498e8c2601dSPoul-Henning Kamp 
499e8c2601dSPoul-Henning Kamp 	if_route(ifp, IFF_UP, AF_UNSPEC);
500e8c2601dSPoul-Henning Kamp }
501e8c2601dSPoul-Henning Kamp 
502e8c2601dSPoul-Henning Kamp /*
503df8bae1dSRodney W. Grimes  * Flush an interface queue.
504df8bae1dSRodney W. Grimes  */
5053bda9f9bSPoul-Henning Kamp static void
506df8bae1dSRodney W. Grimes if_qflush(ifq)
507df8bae1dSRodney W. Grimes 	register struct ifqueue *ifq;
508df8bae1dSRodney W. Grimes {
509df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
510df8bae1dSRodney W. Grimes 
511df8bae1dSRodney W. Grimes 	n = ifq->ifq_head;
5129448326fSPoul-Henning Kamp 	while ((m = n) != 0) {
513df8bae1dSRodney W. Grimes 		n = m->m_act;
514df8bae1dSRodney W. Grimes 		m_freem(m);
515df8bae1dSRodney W. Grimes 	}
516df8bae1dSRodney W. Grimes 	ifq->ifq_head = 0;
517df8bae1dSRodney W. Grimes 	ifq->ifq_tail = 0;
518df8bae1dSRodney W. Grimes 	ifq->ifq_len = 0;
519df8bae1dSRodney W. Grimes }
520df8bae1dSRodney W. Grimes 
521df8bae1dSRodney W. Grimes /*
522df8bae1dSRodney W. Grimes  * Handle interface watchdog timer routines.  Called
523df8bae1dSRodney W. Grimes  * from softclock, we decrement timers (if set) and
524df8bae1dSRodney W. Grimes  * call the appropriate interface routine on expiration.
525df8bae1dSRodney W. Grimes  */
5263bda9f9bSPoul-Henning Kamp static void
527df8bae1dSRodney W. Grimes if_slowtimo(arg)
528df8bae1dSRodney W. Grimes 	void *arg;
529df8bae1dSRodney W. Grimes {
530df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
531df8bae1dSRodney W. Grimes 	int s = splimp();
532df8bae1dSRodney W. Grimes 
53329412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
534df8bae1dSRodney W. Grimes 		if (ifp->if_timer == 0 || --ifp->if_timer)
535df8bae1dSRodney W. Grimes 			continue;
536df8bae1dSRodney W. Grimes 		if (ifp->if_watchdog)
5374a5f1499SDavid Greenman 			(*ifp->if_watchdog)(ifp);
538df8bae1dSRodney W. Grimes 	}
539df8bae1dSRodney W. Grimes 	splx(s);
540df8bae1dSRodney W. Grimes 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
541df8bae1dSRodney W. Grimes }
542df8bae1dSRodney W. Grimes 
543df8bae1dSRodney W. Grimes /*
544df8bae1dSRodney W. Grimes  * Map interface name to
545df8bae1dSRodney W. Grimes  * interface structure pointer.
546df8bae1dSRodney W. Grimes  */
547df8bae1dSRodney W. Grimes struct ifnet *
548df8bae1dSRodney W. Grimes ifunit(name)
549df8bae1dSRodney W. Grimes 	register char *name;
550df8bae1dSRodney W. Grimes {
55101f0fef3SJulian Elischer 	char namebuf[IFNAMSIZ + 1];
55201f0fef3SJulian Elischer 	register char *cp, *cp2;
55301f0fef3SJulian Elischer 	char *end;
554df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
555df8bae1dSRodney W. Grimes 	int unit;
556df8bae1dSRodney W. Grimes 	unsigned len;
55701f0fef3SJulian Elischer 	register char c = '\0';
558df8bae1dSRodney W. Grimes 
559df8bae1dSRodney W. Grimes 	/*
56001f0fef3SJulian Elischer 	 * Look for a non numeric part
56101f0fef3SJulian Elischer 	 */
56201f0fef3SJulian Elischer 	end = name + IFNAMSIZ;
56301f0fef3SJulian Elischer 	cp2 = namebuf;
56401f0fef3SJulian Elischer 	cp = name;
56501f0fef3SJulian Elischer 	while ((cp < end) && (c = *cp)) {
56601f0fef3SJulian Elischer 		if (c >= '0' && c <= '9')
56701f0fef3SJulian Elischer 			break;
56801f0fef3SJulian Elischer 		*cp2++ = c;
56901f0fef3SJulian Elischer 		cp++;
57001f0fef3SJulian Elischer 	}
57101f0fef3SJulian Elischer 	if ((cp == end) || (c == '\0') || (cp == name))
57201f0fef3SJulian Elischer 		return ((struct ifnet *)0);
57301f0fef3SJulian Elischer 	*cp2 = '\0';
57401f0fef3SJulian Elischer 	/*
57501f0fef3SJulian Elischer 	 * check we have a legal number (limit to 7 digits?)
576df8bae1dSRodney W. Grimes 	 */
577df8bae1dSRodney W. Grimes 	len = cp - name + 1;
57801f0fef3SJulian Elischer 	for (unit = 0;
57901f0fef3SJulian Elischer 	    ((c = *cp) >= '0') && (c <= '9') && (unit < 1000000); cp++ )
58001f0fef3SJulian Elischer 		unit = (unit * 10) + (c - '0');
58158639ed3SGarrett Wollman 	if (*cp != '\0')
58258639ed3SGarrett Wollman 		return 0;	/* no trailing garbage allowed */
58301f0fef3SJulian Elischer 	/*
58401f0fef3SJulian Elischer 	 * Now search all the interfaces for this name/number
58501f0fef3SJulian Elischer 	 */
58629412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
58701f0fef3SJulian Elischer 		if (bcmp(ifp->if_name, namebuf, len))
588df8bae1dSRodney W. Grimes 			continue;
589df8bae1dSRodney W. Grimes 		if (unit == ifp->if_unit)
590df8bae1dSRodney W. Grimes 			break;
591df8bae1dSRodney W. Grimes 	}
592df8bae1dSRodney W. Grimes 	return (ifp);
593df8bae1dSRodney W. Grimes }
594df8bae1dSRodney W. Grimes 
595df8bae1dSRodney W. Grimes /*
596df8bae1dSRodney W. Grimes  * Interface ioctls.
597df8bae1dSRodney W. Grimes  */
598df8bae1dSRodney W. Grimes int
599df8bae1dSRodney W. Grimes ifioctl(so, cmd, data, p)
600df8bae1dSRodney W. Grimes 	struct socket *so;
601ecbb00a2SDoug Rabson 	u_long cmd;
602df8bae1dSRodney W. Grimes 	caddr_t data;
603df8bae1dSRodney W. Grimes 	struct proc *p;
604df8bae1dSRodney W. Grimes {
605df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
606df8bae1dSRodney W. Grimes 	register struct ifreq *ifr;
607413dd0baSPoul-Henning Kamp 	struct ifstat *ifs;
608df8bae1dSRodney W. Grimes 	int error;
609df8bae1dSRodney W. Grimes 
610df8bae1dSRodney W. Grimes 	switch (cmd) {
611df8bae1dSRodney W. Grimes 
612df8bae1dSRodney W. Grimes 	case SIOCGIFCONF:
613df8bae1dSRodney W. Grimes 	case OSIOCGIFCONF:
614df8bae1dSRodney W. Grimes 		return (ifconf(cmd, data));
615df8bae1dSRodney W. Grimes 	}
616df8bae1dSRodney W. Grimes 	ifr = (struct ifreq *)data;
617df8bae1dSRodney W. Grimes 	ifp = ifunit(ifr->ifr_name);
618df8bae1dSRodney W. Grimes 	if (ifp == 0)
619df8bae1dSRodney W. Grimes 		return (ENXIO);
620df8bae1dSRodney W. Grimes 	switch (cmd) {
621df8bae1dSRodney W. Grimes 
622df8bae1dSRodney W. Grimes 	case SIOCGIFFLAGS:
623df8bae1dSRodney W. Grimes 		ifr->ifr_flags = ifp->if_flags;
624df8bae1dSRodney W. Grimes 		break;
625df8bae1dSRodney W. Grimes 
626df8bae1dSRodney W. Grimes 	case SIOCGIFMETRIC:
627df8bae1dSRodney W. Grimes 		ifr->ifr_metric = ifp->if_metric;
628df8bae1dSRodney W. Grimes 		break;
629df8bae1dSRodney W. Grimes 
630a7028af7SDavid Greenman 	case SIOCGIFMTU:
631a7028af7SDavid Greenman 		ifr->ifr_mtu = ifp->if_mtu;
632a7028af7SDavid Greenman 		break;
633a7028af7SDavid Greenman 
634074c4a4eSGarrett Wollman 	case SIOCGIFPHYS:
635074c4a4eSGarrett Wollman 		ifr->ifr_phys = ifp->if_physical;
636074c4a4eSGarrett Wollman 		break;
637074c4a4eSGarrett Wollman 
638df8bae1dSRodney W. Grimes 	case SIOCSIFFLAGS:
639f711d546SPoul-Henning Kamp 		error = suser(p);
6409448326fSPoul-Henning Kamp 		if (error)
641df8bae1dSRodney W. Grimes 			return (error);
6424add131eSPoul-Henning Kamp 		ifr->ifr_prevflags = ifp->if_flags;
643cf4b9371SPoul-Henning Kamp 		if (ifp->if_flags & IFF_SMART) {
644cf4b9371SPoul-Henning Kamp 			/* Smart drivers twiddle their own routes */
6452f55ead7SPoul-Henning Kamp 		} else if (ifp->if_flags & IFF_UP &&
646cf4b9371SPoul-Henning Kamp 		    (ifr->ifr_flags & IFF_UP) == 0) {
647df8bae1dSRodney W. Grimes 			int s = splimp();
648df8bae1dSRodney W. Grimes 			if_down(ifp);
649df8bae1dSRodney W. Grimes 			splx(s);
650cf4b9371SPoul-Henning Kamp 		} else if (ifr->ifr_flags & IFF_UP &&
651cf4b9371SPoul-Henning Kamp 		    (ifp->if_flags & IFF_UP) == 0) {
652df8bae1dSRodney W. Grimes 			int s = splimp();
653df8bae1dSRodney W. Grimes 			if_up(ifp);
654df8bae1dSRodney W. Grimes 			splx(s);
655df8bae1dSRodney W. Grimes 		}
656df8bae1dSRodney W. Grimes 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
657df8bae1dSRodney W. Grimes 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
658df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl)
659df8bae1dSRodney W. Grimes 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
66098b9590eSPoul-Henning Kamp 		getmicrotime(&ifp->if_lastchange);
661df8bae1dSRodney W. Grimes 		break;
662df8bae1dSRodney W. Grimes 
663df8bae1dSRodney W. Grimes 	case SIOCSIFMETRIC:
664f711d546SPoul-Henning Kamp 		error = suser(p);
6659448326fSPoul-Henning Kamp 		if (error)
666df8bae1dSRodney W. Grimes 			return (error);
667df8bae1dSRodney W. Grimes 		ifp->if_metric = ifr->ifr_metric;
66898b9590eSPoul-Henning Kamp 		getmicrotime(&ifp->if_lastchange);
669df8bae1dSRodney W. Grimes 		break;
670df8bae1dSRodney W. Grimes 
671074c4a4eSGarrett Wollman 	case SIOCSIFPHYS:
672f711d546SPoul-Henning Kamp 		error = suser(p);
673e39a0280SGary Palmer 		if (error)
674e39a0280SGary Palmer 			return error;
675e39a0280SGary Palmer 		if (!ifp->if_ioctl)
676e39a0280SGary Palmer 		        return EOPNOTSUPP;
677e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
678e39a0280SGary Palmer 		if (error == 0)
67998b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
680e39a0280SGary Palmer 		return(error);
681074c4a4eSGarrett Wollman 
682a7028af7SDavid Greenman 	case SIOCSIFMTU:
683f711d546SPoul-Henning Kamp 		error = suser(p);
6849448326fSPoul-Henning Kamp 		if (error)
685a7028af7SDavid Greenman 			return (error);
686a7028af7SDavid Greenman 		if (ifp->if_ioctl == NULL)
687a7028af7SDavid Greenman 			return (EOPNOTSUPP);
688aab3beeeSBrian Somers 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
68975ee03cbSDavid Greenman 			return (EINVAL);
690e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
691e39a0280SGary Palmer 		if (error == 0)
69298b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
693e39a0280SGary Palmer 		return(error);
694a7028af7SDavid Greenman 
695df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
696df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
697f711d546SPoul-Henning Kamp 		error = suser(p);
6989448326fSPoul-Henning Kamp 		if (error)
699df8bae1dSRodney W. Grimes 			return (error);
700477180fbSGarrett Wollman 
701477180fbSGarrett Wollman 		/* Don't allow group membership on non-multicast interfaces. */
702477180fbSGarrett Wollman 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
703477180fbSGarrett Wollman 			return EOPNOTSUPP;
704477180fbSGarrett Wollman 
705477180fbSGarrett Wollman 		/* Don't let users screw up protocols' entries. */
706477180fbSGarrett Wollman 		if (ifr->ifr_addr.sa_family != AF_LINK)
707477180fbSGarrett Wollman 			return EINVAL;
708477180fbSGarrett Wollman 
709477180fbSGarrett Wollman 		if (cmd == SIOCADDMULTI) {
710477180fbSGarrett Wollman 			struct ifmultiaddr *ifma;
711477180fbSGarrett Wollman 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
712477180fbSGarrett Wollman 		} else {
713477180fbSGarrett Wollman 			error = if_delmulti(ifp, &ifr->ifr_addr);
714477180fbSGarrett Wollman 		}
715e39a0280SGary Palmer 		if (error == 0)
71698b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
717477180fbSGarrett Wollman 		return error;
718df8bae1dSRodney W. Grimes 
719a912e453SPeter Wemm         case SIOCSIFMEDIA:
720d7189ec6SJoerg Wunsch 	case SIOCSIFGENERIC:
721f711d546SPoul-Henning Kamp 		error = suser(p);
722a912e453SPeter Wemm 		if (error)
723a912e453SPeter Wemm 			return (error);
724a912e453SPeter Wemm 		if (ifp->if_ioctl == 0)
725a912e453SPeter Wemm 			return (EOPNOTSUPP);
726a912e453SPeter Wemm 		error = (*ifp->if_ioctl)(ifp, cmd, data);
727a912e453SPeter Wemm 		if (error == 0)
72898b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
729a912e453SPeter Wemm 		return error;
730a912e453SPeter Wemm 
731413dd0baSPoul-Henning Kamp 	case SIOCGIFSTATUS:
732413dd0baSPoul-Henning Kamp 		ifs = (struct ifstat *)data;
733413dd0baSPoul-Henning Kamp 		ifs->ascii[0] = '\0';
734413dd0baSPoul-Henning Kamp 
735a912e453SPeter Wemm 	case SIOCGIFMEDIA:
736d7189ec6SJoerg Wunsch 	case SIOCGIFGENERIC:
737a912e453SPeter Wemm 		if (ifp->if_ioctl == 0)
738a912e453SPeter Wemm 			return (EOPNOTSUPP);
739a912e453SPeter Wemm 		return ((*ifp->if_ioctl)(ifp, cmd, data));
740a912e453SPeter Wemm 
741df8bae1dSRodney W. Grimes 	default:
742df8bae1dSRodney W. Grimes 		if (so->so_proto == 0)
743df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
744df8bae1dSRodney W. Grimes #ifndef COMPAT_43
7452c37256eSGarrett Wollman 		return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
7462c37256eSGarrett Wollman 								 data,
747bc6d9b80SJoerg Wunsch 								 ifp, p));
748df8bae1dSRodney W. Grimes #else
749df8bae1dSRodney W. Grimes 	    {
750df8bae1dSRodney W. Grimes 		int ocmd = cmd;
751df8bae1dSRodney W. Grimes 
752df8bae1dSRodney W. Grimes 		switch (cmd) {
753df8bae1dSRodney W. Grimes 
754df8bae1dSRodney W. Grimes 		case SIOCSIFDSTADDR:
755df8bae1dSRodney W. Grimes 		case SIOCSIFADDR:
756df8bae1dSRodney W. Grimes 		case SIOCSIFBRDADDR:
757df8bae1dSRodney W. Grimes 		case SIOCSIFNETMASK:
758df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN
759df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_family == 0 &&
760df8bae1dSRodney W. Grimes 			    ifr->ifr_addr.sa_len < 16) {
761df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
762df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
763df8bae1dSRodney W. Grimes 			}
764df8bae1dSRodney W. Grimes #else
765df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_len == 0)
766df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
767df8bae1dSRodney W. Grimes #endif
768df8bae1dSRodney W. Grimes 			break;
769df8bae1dSRodney W. Grimes 
770df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
771df8bae1dSRodney W. Grimes 			cmd = SIOCGIFADDR;
772df8bae1dSRodney W. Grimes 			break;
773df8bae1dSRodney W. Grimes 
774df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
775df8bae1dSRodney W. Grimes 			cmd = SIOCGIFDSTADDR;
776df8bae1dSRodney W. Grimes 			break;
777df8bae1dSRodney W. Grimes 
778df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
779df8bae1dSRodney W. Grimes 			cmd = SIOCGIFBRDADDR;
780df8bae1dSRodney W. Grimes 			break;
781df8bae1dSRodney W. Grimes 
782df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
783df8bae1dSRodney W. Grimes 			cmd = SIOCGIFNETMASK;
784df8bae1dSRodney W. Grimes 		}
7852c37256eSGarrett Wollman 		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
7862c37256eSGarrett Wollman 								   cmd,
7872c37256eSGarrett Wollman 								   data,
788a29f300eSGarrett Wollman 								   ifp, p));
789df8bae1dSRodney W. Grimes 		switch (ocmd) {
790df8bae1dSRodney W. Grimes 
791df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
792df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
793df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
794df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
795df8bae1dSRodney W. Grimes 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
796df8bae1dSRodney W. Grimes 		}
797df8bae1dSRodney W. Grimes 		return (error);
798df8bae1dSRodney W. Grimes 
799df8bae1dSRodney W. Grimes 	    }
800df8bae1dSRodney W. Grimes #endif
801df8bae1dSRodney W. Grimes 	}
802df8bae1dSRodney W. Grimes 	return (0);
803df8bae1dSRodney W. Grimes }
804df8bae1dSRodney W. Grimes 
805df8bae1dSRodney W. Grimes /*
806963e4c2aSGarrett Wollman  * Set/clear promiscuous mode on interface ifp based on the truth value
807963e4c2aSGarrett Wollman  * of pswitch.  The calls are reference counted so that only the first
808963e4c2aSGarrett Wollman  * "on" request actually has an effect, as does the final "off" request.
809963e4c2aSGarrett Wollman  * Results are undefined if the "off" and "on" requests are not matched.
810963e4c2aSGarrett Wollman  */
811963e4c2aSGarrett Wollman int
812963e4c2aSGarrett Wollman ifpromisc(ifp, pswitch)
813963e4c2aSGarrett Wollman 	struct ifnet *ifp;
814963e4c2aSGarrett Wollman 	int pswitch;
815963e4c2aSGarrett Wollman {
816963e4c2aSGarrett Wollman 	struct ifreq ifr;
8174a26224cSGarrett Wollman 	int error;
818963e4c2aSGarrett Wollman 
819963e4c2aSGarrett Wollman 	if (pswitch) {
820963e4c2aSGarrett Wollman 		/*
821963e4c2aSGarrett Wollman 		 * If the device is not configured up, we cannot put it in
822963e4c2aSGarrett Wollman 		 * promiscuous mode.
823963e4c2aSGarrett Wollman 		 */
824963e4c2aSGarrett Wollman 		if ((ifp->if_flags & IFF_UP) == 0)
825963e4c2aSGarrett Wollman 			return (ENETDOWN);
826963e4c2aSGarrett Wollman 		if (ifp->if_pcount++ != 0)
827963e4c2aSGarrett Wollman 			return (0);
828963e4c2aSGarrett Wollman 		ifp->if_flags |= IFF_PROMISC;
829e9a30d00SGarrett Wollman 		log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
830963e4c2aSGarrett Wollman 		    ifp->if_name, ifp->if_unit);
831963e4c2aSGarrett Wollman 	} else {
832963e4c2aSGarrett Wollman 		if (--ifp->if_pcount > 0)
833963e4c2aSGarrett Wollman 			return (0);
834963e4c2aSGarrett Wollman 		ifp->if_flags &= ~IFF_PROMISC;
835963e4c2aSGarrett Wollman 	}
836963e4c2aSGarrett Wollman 	ifr.ifr_flags = ifp->if_flags;
8374a26224cSGarrett Wollman 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
8384a26224cSGarrett Wollman 	if (error == 0)
8394a26224cSGarrett Wollman 		rt_ifmsg(ifp);
8404a26224cSGarrett Wollman 	return error;
841963e4c2aSGarrett Wollman }
842963e4c2aSGarrett Wollman 
843963e4c2aSGarrett Wollman /*
844df8bae1dSRodney W. Grimes  * Return interface configuration
845df8bae1dSRodney W. Grimes  * of system.  List may be used
846df8bae1dSRodney W. Grimes  * in later ioctl's (above) to get
847df8bae1dSRodney W. Grimes  * other information.
848df8bae1dSRodney W. Grimes  */
849df8bae1dSRodney W. Grimes /*ARGSUSED*/
8503bda9f9bSPoul-Henning Kamp static int
851df8bae1dSRodney W. Grimes ifconf(cmd, data)
852ecbb00a2SDoug Rabson 	u_long cmd;
853df8bae1dSRodney W. Grimes 	caddr_t data;
854df8bae1dSRodney W. Grimes {
855df8bae1dSRodney W. Grimes 	register struct ifconf *ifc = (struct ifconf *)data;
85629412182SGarrett Wollman 	register struct ifnet *ifp = ifnet.tqh_first;
857df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
858df8bae1dSRodney W. Grimes 	struct ifreq ifr, *ifrp;
859df8bae1dSRodney W. Grimes 	int space = ifc->ifc_len, error = 0;
860df8bae1dSRodney W. Grimes 
861df8bae1dSRodney W. Grimes 	ifrp = ifc->ifc_req;
86229412182SGarrett Wollman 	for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) {
8631ce9bf88SPoul-Henning Kamp 		char workbuf[64];
86475c13541SPoul-Henning Kamp 		int ifnlen, addrs;
8652624cf89SGarrett Wollman 
8662127f260SArchie Cobbs 		ifnlen = snprintf(workbuf, sizeof(workbuf),
8672127f260SArchie Cobbs 		    "%s%d", ifp->if_name, ifp->if_unit);
8681ce9bf88SPoul-Henning Kamp 		if(ifnlen + 1 > sizeof ifr.ifr_name) {
8692624cf89SGarrett Wollman 			error = ENAMETOOLONG;
8702624cf89SGarrett Wollman 		} else {
8711ce9bf88SPoul-Henning Kamp 			strcpy(ifr.ifr_name, workbuf);
8722624cf89SGarrett Wollman 		}
8732624cf89SGarrett Wollman 
87475c13541SPoul-Henning Kamp 		addrs = 0;
87575c13541SPoul-Henning Kamp 		ifa = ifp->if_addrhead.tqh_first;
87659562606SGarrett Wollman 		for ( ; space > sizeof (ifr) && ifa;
87759562606SGarrett Wollman 		    ifa = ifa->ifa_link.tqe_next) {
878df8bae1dSRodney W. Grimes 			register struct sockaddr *sa = ifa->ifa_addr;
87975c13541SPoul-Henning Kamp 			if (curproc->p_prison && prison_if(curproc, sa))
88075c13541SPoul-Henning Kamp 				continue;
88175c13541SPoul-Henning Kamp 			addrs++;
882df8bae1dSRodney W. Grimes #ifdef COMPAT_43
883df8bae1dSRodney W. Grimes 			if (cmd == OSIOCGIFCONF) {
884df8bae1dSRodney W. Grimes 				struct osockaddr *osa =
885df8bae1dSRodney W. Grimes 					 (struct osockaddr *)&ifr.ifr_addr;
886df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
887df8bae1dSRodney W. Grimes 				osa->sa_family = sa->sa_family;
888df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
889df8bae1dSRodney W. Grimes 						sizeof (ifr));
890df8bae1dSRodney W. Grimes 				ifrp++;
891df8bae1dSRodney W. Grimes 			} else
892df8bae1dSRodney W. Grimes #endif
893df8bae1dSRodney W. Grimes 			if (sa->sa_len <= sizeof(*sa)) {
894df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
895df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
896df8bae1dSRodney W. Grimes 						sizeof (ifr));
897df8bae1dSRodney W. Grimes 				ifrp++;
898df8bae1dSRodney W. Grimes 			} else {
899df8bae1dSRodney W. Grimes 				space -= sa->sa_len - sizeof(*sa);
900df8bae1dSRodney W. Grimes 				if (space < sizeof (ifr))
901df8bae1dSRodney W. Grimes 					break;
902df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
903df8bae1dSRodney W. Grimes 						sizeof (ifr.ifr_name));
904df8bae1dSRodney W. Grimes 				if (error == 0)
905df8bae1dSRodney W. Grimes 				    error = copyout((caddr_t)sa,
906df8bae1dSRodney W. Grimes 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
907df8bae1dSRodney W. Grimes 				ifrp = (struct ifreq *)
908df8bae1dSRodney W. Grimes 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
909df8bae1dSRodney W. Grimes 			}
910df8bae1dSRodney W. Grimes 			if (error)
911df8bae1dSRodney W. Grimes 				break;
912df8bae1dSRodney W. Grimes 			space -= sizeof (ifr);
913df8bae1dSRodney W. Grimes 		}
91475c13541SPoul-Henning Kamp 		if (!addrs) {
91575c13541SPoul-Henning Kamp 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
91675c13541SPoul-Henning Kamp 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
91775c13541SPoul-Henning Kamp 			    sizeof (ifr));
91875c13541SPoul-Henning Kamp 			if (error)
91975c13541SPoul-Henning Kamp 				break;
92075c13541SPoul-Henning Kamp 			space -= sizeof (ifr), ifrp++;
92175c13541SPoul-Henning Kamp 		}
922df8bae1dSRodney W. Grimes 	}
923df8bae1dSRodney W. Grimes 	ifc->ifc_len -= space;
924df8bae1dSRodney W. Grimes 	return (error);
925df8bae1dSRodney W. Grimes }
926df8bae1dSRodney W. Grimes 
9271158dfb7SGarrett Wollman /*
9281158dfb7SGarrett Wollman  * Just like if_promisc(), but for all-multicast-reception mode.
9291158dfb7SGarrett Wollman  */
9301158dfb7SGarrett Wollman int
9311158dfb7SGarrett Wollman if_allmulti(ifp, onswitch)
9321158dfb7SGarrett Wollman 	struct ifnet *ifp;
9331158dfb7SGarrett Wollman 	int onswitch;
9341158dfb7SGarrett Wollman {
9351158dfb7SGarrett Wollman 	int error = 0;
9361158dfb7SGarrett Wollman 	int s = splimp();
9371158dfb7SGarrett Wollman 
9381158dfb7SGarrett Wollman 	if (onswitch) {
9391158dfb7SGarrett Wollman 		if (ifp->if_amcount++ == 0) {
9401158dfb7SGarrett Wollman 			ifp->if_flags |= IFF_ALLMULTI;
9411158dfb7SGarrett Wollman 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
9421158dfb7SGarrett Wollman 		}
9431158dfb7SGarrett Wollman 	} else {
9441158dfb7SGarrett Wollman 		if (ifp->if_amcount > 1) {
9451158dfb7SGarrett Wollman 			ifp->if_amcount--;
9461158dfb7SGarrett Wollman 		} else {
9471158dfb7SGarrett Wollman 			ifp->if_amcount = 0;
9481158dfb7SGarrett Wollman 			ifp->if_flags &= ~IFF_ALLMULTI;
9491158dfb7SGarrett Wollman 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
9501158dfb7SGarrett Wollman 		}
9511158dfb7SGarrett Wollman 	}
9521158dfb7SGarrett Wollman 	splx(s);
9534a26224cSGarrett Wollman 
9544a26224cSGarrett Wollman 	if (error == 0)
9554a26224cSGarrett Wollman 		rt_ifmsg(ifp);
9561158dfb7SGarrett Wollman 	return error;
9571158dfb7SGarrett Wollman }
9581158dfb7SGarrett Wollman 
9591158dfb7SGarrett Wollman /*
9601158dfb7SGarrett Wollman  * Add a multicast listenership to the interface in question.
9611158dfb7SGarrett Wollman  * The link layer provides a routine which converts
9621158dfb7SGarrett Wollman  */
9631158dfb7SGarrett Wollman int
964373f88edSGarrett Wollman if_addmulti(ifp, sa, retifma)
9651158dfb7SGarrett Wollman 	struct ifnet *ifp;	/* interface to manipulate */
9661158dfb7SGarrett Wollman 	struct sockaddr *sa;	/* address to add */
967b2053118SGarrett Wollman 	struct ifmultiaddr **retifma;
9681158dfb7SGarrett Wollman {
9691158dfb7SGarrett Wollman 	struct sockaddr *llsa, *dupsa;
9701158dfb7SGarrett Wollman 	int error, s;
9711158dfb7SGarrett Wollman 	struct ifmultiaddr *ifma;
9721158dfb7SGarrett Wollman 
97357af7922SJulian Elischer 	/*
97457af7922SJulian Elischer 	 * If the matching multicast address already exists
97557af7922SJulian Elischer 	 * then don't add a new one, just add a reference
97657af7922SJulian Elischer 	 */
9771158dfb7SGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
9781158dfb7SGarrett Wollman 	     ifma = ifma->ifma_link.le_next) {
97957af7922SJulian Elischer 		if (equal(sa, ifma->ifma_addr)) {
9801158dfb7SGarrett Wollman 			ifma->ifma_refcount++;
98157af7922SJulian Elischer 			if (retifma)
98257af7922SJulian Elischer 				*retifma = ifma;
9831158dfb7SGarrett Wollman 			return 0;
9841158dfb7SGarrett Wollman 		}
98557af7922SJulian Elischer 	}
9861158dfb7SGarrett Wollman 
9871158dfb7SGarrett Wollman 	/*
9881158dfb7SGarrett Wollman 	 * Give the link layer a chance to accept/reject it, and also
9891158dfb7SGarrett Wollman 	 * find out which AF_LINK address this maps to, if it isn't one
9901158dfb7SGarrett Wollman 	 * already.
9911158dfb7SGarrett Wollman 	 */
9921158dfb7SGarrett Wollman 	if (ifp->if_resolvemulti) {
9931158dfb7SGarrett Wollman 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
9941158dfb7SGarrett Wollman 		if (error) return error;
9951158dfb7SGarrett Wollman 	} else {
9961158dfb7SGarrett Wollman 		llsa = 0;
9971158dfb7SGarrett Wollman 	}
9981158dfb7SGarrett Wollman 
9991158dfb7SGarrett Wollman 	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
10001158dfb7SGarrett Wollman 	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
10011158dfb7SGarrett Wollman 	bcopy(sa, dupsa, sa->sa_len);
10021158dfb7SGarrett Wollman 
10031158dfb7SGarrett Wollman 	ifma->ifma_addr = dupsa;
10041158dfb7SGarrett Wollman 	ifma->ifma_lladdr = llsa;
10051158dfb7SGarrett Wollman 	ifma->ifma_ifp = ifp;
10061158dfb7SGarrett Wollman 	ifma->ifma_refcount = 1;
1007373f88edSGarrett Wollman 	ifma->ifma_protospec = 0;
1008477180fbSGarrett Wollman 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1009373f88edSGarrett Wollman 
10101158dfb7SGarrett Wollman 	/*
10111158dfb7SGarrett Wollman 	 * Some network interfaces can scan the address list at
10121158dfb7SGarrett Wollman 	 * interrupt time; lock them out.
10131158dfb7SGarrett Wollman 	 */
10141158dfb7SGarrett Wollman 	s = splimp();
10151158dfb7SGarrett Wollman 	LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
10161158dfb7SGarrett Wollman 	splx(s);
1017373f88edSGarrett Wollman 	*retifma = ifma;
10181158dfb7SGarrett Wollman 
10191158dfb7SGarrett Wollman 	if (llsa != 0) {
10201158dfb7SGarrett Wollman 		for (ifma = ifp->if_multiaddrs.lh_first; ifma;
10211158dfb7SGarrett Wollman 		     ifma = ifma->ifma_link.le_next) {
10221158dfb7SGarrett Wollman 			if (equal(ifma->ifma_addr, llsa))
10231158dfb7SGarrett Wollman 				break;
10241158dfb7SGarrett Wollman 		}
10251158dfb7SGarrett Wollman 		if (ifma) {
10261158dfb7SGarrett Wollman 			ifma->ifma_refcount++;
10271158dfb7SGarrett Wollman 		} else {
10281158dfb7SGarrett Wollman 			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
10291158dfb7SGarrett Wollman 			       M_IFMADDR, M_WAITOK);
1030477180fbSGarrett Wollman 			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1031477180fbSGarrett Wollman 			       M_IFMADDR, M_WAITOK);
1032477180fbSGarrett Wollman 			bcopy(llsa, dupsa, llsa->sa_len);
1033477180fbSGarrett Wollman 			ifma->ifma_addr = dupsa;
10341158dfb7SGarrett Wollman 			ifma->ifma_ifp = ifp;
10351158dfb7SGarrett Wollman 			ifma->ifma_refcount = 1;
10361158dfb7SGarrett Wollman 			s = splimp();
10371158dfb7SGarrett Wollman 			LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
10381158dfb7SGarrett Wollman 			splx(s);
10391158dfb7SGarrett Wollman 		}
104057af7922SJulian Elischer 	}
10411158dfb7SGarrett Wollman 	/*
10421158dfb7SGarrett Wollman 	 * We are certain we have added something, so call down to the
10431158dfb7SGarrett Wollman 	 * interface to let them know about it.
10441158dfb7SGarrett Wollman 	 */
10451158dfb7SGarrett Wollman 	s = splimp();
10461158dfb7SGarrett Wollman 	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
10471158dfb7SGarrett Wollman 	splx(s);
10481158dfb7SGarrett Wollman 
10491158dfb7SGarrett Wollman 	return 0;
10501158dfb7SGarrett Wollman }
10511158dfb7SGarrett Wollman 
10521158dfb7SGarrett Wollman /*
10531158dfb7SGarrett Wollman  * Remove a reference to a multicast address on this interface.  Yell
10541158dfb7SGarrett Wollman  * if the request does not match an existing membership.
10551158dfb7SGarrett Wollman  */
10561158dfb7SGarrett Wollman int
10571158dfb7SGarrett Wollman if_delmulti(ifp, sa)
10581158dfb7SGarrett Wollman 	struct ifnet *ifp;
10591158dfb7SGarrett Wollman 	struct sockaddr *sa;
10601158dfb7SGarrett Wollman {
10611158dfb7SGarrett Wollman 	struct ifmultiaddr *ifma;
10621158dfb7SGarrett Wollman 	int s;
10631158dfb7SGarrett Wollman 
10641158dfb7SGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
10651158dfb7SGarrett Wollman 	     ifma = ifma->ifma_link.le_next)
10661158dfb7SGarrett Wollman 		if (equal(sa, ifma->ifma_addr))
10671158dfb7SGarrett Wollman 			break;
10681158dfb7SGarrett Wollman 	if (ifma == 0)
10691158dfb7SGarrett Wollman 		return ENOENT;
10701158dfb7SGarrett Wollman 
10711158dfb7SGarrett Wollman 	if (ifma->ifma_refcount > 1) {
10721158dfb7SGarrett Wollman 		ifma->ifma_refcount--;
10731158dfb7SGarrett Wollman 		return 0;
10741158dfb7SGarrett Wollman 	}
10751158dfb7SGarrett Wollman 
1076477180fbSGarrett Wollman 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
10771158dfb7SGarrett Wollman 	sa = ifma->ifma_lladdr;
10781158dfb7SGarrett Wollman 	s = splimp();
10791158dfb7SGarrett Wollman 	LIST_REMOVE(ifma, ifma_link);
10801158dfb7SGarrett Wollman 	splx(s);
10811158dfb7SGarrett Wollman 	free(ifma->ifma_addr, M_IFMADDR);
10821158dfb7SGarrett Wollman 	free(ifma, M_IFMADDR);
10831158dfb7SGarrett Wollman 	if (sa == 0)
10841158dfb7SGarrett Wollman 		return 0;
10851158dfb7SGarrett Wollman 
10861158dfb7SGarrett Wollman 	/*
10871158dfb7SGarrett Wollman 	 * Now look for the link-layer address which corresponds to
10881158dfb7SGarrett Wollman 	 * this network address.  It had been squirreled away in
10891158dfb7SGarrett Wollman 	 * ifma->ifma_lladdr for this purpose (so we don't have
10901158dfb7SGarrett Wollman 	 * to call ifp->if_resolvemulti() again), and we saved that
10911158dfb7SGarrett Wollman 	 * value in sa above.  If some nasty deleted the
10921158dfb7SGarrett Wollman 	 * link-layer address out from underneath us, we can deal because
10931158dfb7SGarrett Wollman 	 * the address we stored was is not the same as the one which was
10941158dfb7SGarrett Wollman 	 * in the record for the link-layer address.  (So we don't complain
10951158dfb7SGarrett Wollman 	 * in that case.)
10961158dfb7SGarrett Wollman 	 */
10971158dfb7SGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
10981158dfb7SGarrett Wollman 	     ifma = ifma->ifma_link.le_next)
10991158dfb7SGarrett Wollman 		if (equal(sa, ifma->ifma_addr))
11001158dfb7SGarrett Wollman 			break;
11011158dfb7SGarrett Wollman 	if (ifma == 0)
11021158dfb7SGarrett Wollman 		return 0;
11031158dfb7SGarrett Wollman 
11041158dfb7SGarrett Wollman 	if (ifma->ifma_refcount > 1) {
11051158dfb7SGarrett Wollman 		ifma->ifma_refcount--;
11061158dfb7SGarrett Wollman 		return 0;
11071158dfb7SGarrett Wollman 	}
11081158dfb7SGarrett Wollman 
11091158dfb7SGarrett Wollman 	s = splimp();
11101158dfb7SGarrett Wollman 	LIST_REMOVE(ifma, ifma_link);
1111c7323482SBill Paul 	ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
11121158dfb7SGarrett Wollman 	splx(s);
11131158dfb7SGarrett Wollman 	free(ifma->ifma_addr, M_IFMADDR);
11141158dfb7SGarrett Wollman 	free(sa, M_IFMADDR);
11151158dfb7SGarrett Wollman 	free(ifma, M_IFMADDR);
11161158dfb7SGarrett Wollman 
11171158dfb7SGarrett Wollman 	return 0;
11181158dfb7SGarrett Wollman }
11191158dfb7SGarrett Wollman 
1120373f88edSGarrett Wollman struct ifmultiaddr *
1121373f88edSGarrett Wollman ifmaof_ifpforaddr(sa, ifp)
1122373f88edSGarrett Wollman 	struct sockaddr *sa;
1123373f88edSGarrett Wollman 	struct ifnet *ifp;
1124373f88edSGarrett Wollman {
1125373f88edSGarrett Wollman 	struct ifmultiaddr *ifma;
1126373f88edSGarrett Wollman 
1127373f88edSGarrett Wollman 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1128373f88edSGarrett Wollman 	     ifma = ifma->ifma_link.le_next)
1129373f88edSGarrett Wollman 		if (equal(ifma->ifma_addr, sa))
1130373f88edSGarrett Wollman 			break;
1131373f88edSGarrett Wollman 
1132373f88edSGarrett Wollman 	return ifma;
1133373f88edSGarrett Wollman }
1134373f88edSGarrett Wollman 
1135602d513cSGarrett Wollman SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
11362c37256eSGarrett Wollman SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
1137