in.c revision 228313
1139823Simp/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
4137668Smlaier * Copyright (C) 2001 WIDE Project.  All rights reserved.
51541Srgrimes *
61541Srgrimes * Redistribution and use in source and binary forms, with or without
71541Srgrimes * modification, are permitted provided that the following conditions
81541Srgrimes * are met:
91541Srgrimes * 1. Redistributions of source code must retain the above copyright
101541Srgrimes *    notice, this list of conditions and the following disclaimer.
111541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer in the
131541Srgrimes *    documentation and/or other materials provided with the distribution.
141541Srgrimes * 4. Neither the name of the University nor the names of its contributors
151541Srgrimes *    may be used to endorse or promote products derived from this software
161541Srgrimes *    without specific prior written permission.
171541Srgrimes *
181541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
191541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
221541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281541Srgrimes * SUCH DAMAGE.
291541Srgrimes *
3010939Swollman *	@(#)in.c	8.4 (Berkeley) 1/9/95
311541Srgrimes */
321541Srgrimes
33172467Ssilby#include <sys/cdefs.h>
34172467Ssilby__FBSDID("$FreeBSD: head/sys/netinet/in.c 228313 2011-12-06 20:55:20Z glebius $");
35172467Ssilby
36204902Sqingli#include "opt_mpath.h"
37143868Sglebius
381541Srgrimes#include <sys/param.h>
391549Srgrimes#include <sys/systm.h>
4024204Sbde#include <sys/sockio.h>
411541Srgrimes#include <sys/malloc.h>
42164033Srwatson#include <sys/priv.h>
431541Srgrimes#include <sys/socket.h>
44186948Sbz#include <sys/jail.h>
4512704Sphk#include <sys/kernel.h>
46186948Sbz#include <sys/proc.h>
4712704Sphk#include <sys/sysctl.h>
48192011Sqingli#include <sys/syslog.h>
491541Srgrimes
501541Srgrimes#include <net/if.h>
51195914Sqingli#include <net/if_var.h>
52215207Sgnn#include <net/if_arp.h>
53192011Sqingli#include <net/if_dl.h>
54186119Sqingli#include <net/if_llatbl.h>
5555009Sshin#include <net/if_types.h>
561541Srgrimes#include <net/route.h>
57192011Sqingli#include <net/vnet.h>
581541Srgrimes
591541Srgrimes#include <netinet/in.h>
601541Srgrimes#include <netinet/in_var.h>
6181127Sume#include <netinet/in_pcb.h>
62170613Sbms#include <netinet/ip_var.h>
63189592Sbms#include <netinet/igmp_var.h>
64195699Srwatson#include <netinet/udp.h>
65195699Srwatson#include <netinet/udp_var.h>
661541Srgrimes
6792723Salfredstatic int in_mask2len(struct in_addr *);
6892723Salfredstatic void in_len2mask(struct in_addr *, int);
6992723Salfredstatic int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
7092723Salfred	struct ifnet *, struct thread *);
7155009Sshin
72137628Smlaierstatic int	in_addprefix(struct in_ifaddr *, int);
73222143Sqinglistatic int	in_scrubprefix(struct in_ifaddr *, u_int);
7492723Salfredstatic void	in_socktrim(struct sockaddr_in *);
7592723Salfredstatic int	in_ifinit(struct ifnet *,
76228313Sglebius	    struct in_ifaddr *, struct sockaddr_in *, int, int);
77167729Sbmsstatic void	in_purgemaddrs(struct ifnet *);
781541Srgrimes
79215701Sdimstatic VNET_DEFINE(int, sameprefixcarponly);
80207369Sbz#define	V_sameprefixcarponly		VNET(sameprefixcarponly)
81195699SrwatsonSYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
82195699Srwatson	&VNET_NAME(sameprefixcarponly), 0,
83149221Sglebius	"Refuse to create same prefixes on different interfaces");
8421666Swollman
85207369SbzVNET_DECLARE(struct inpcbinfo, ripcbinfo);
86207369Sbz#define	V_ripcbinfo			VNET(ripcbinfo)
87207369Sbz
88215207SgnnVNET_DECLARE(struct arpstat, arpstat);  /* ARP statistics, see if_arp.h */
89215207Sgnn#define	V_arpstat		VNET(arpstat)
90215207Sgnn
911541Srgrimes/*
921541Srgrimes * Return 1 if an internet address is for a ``local'' host
93226401Sglebius * (one to which we have a connection).
941541Srgrimes */
951549Srgrimesint
96169454Srwatsonin_localaddr(struct in_addr in)
971541Srgrimes{
981541Srgrimes	register u_long i = ntohl(in.s_addr);
991541Srgrimes	register struct in_ifaddr *ia;
1001541Srgrimes
101194951Srwatson	IN_IFADDR_RLOCK();
102226401Sglebius	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
103226401Sglebius		if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
104226401Sglebius			IN_IFADDR_RUNLOCK();
105226401Sglebius			return (1);
106194951Srwatson		}
1071541Srgrimes	}
108194951Srwatson	IN_IFADDR_RUNLOCK();
1091541Srgrimes	return (0);
1101541Srgrimes}
1111541Srgrimes
1121541Srgrimes/*
113133486Sandre * Return 1 if an internet address is for the local host and configured
114133486Sandre * on one of its interfaces.
115133486Sandre */
116133486Sandreint
117169454Srwatsonin_localip(struct in_addr in)
118133486Sandre{
119133486Sandre	struct in_ifaddr *ia;
120133486Sandre
121194951Srwatson	IN_IFADDR_RLOCK();
122133486Sandre	LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
123194951Srwatson		if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
124194951Srwatson			IN_IFADDR_RUNLOCK();
125184295Sbz			return (1);
126194951Srwatson		}
127133486Sandre	}
128194951Srwatson	IN_IFADDR_RUNLOCK();
129184295Sbz	return (0);
130133486Sandre}
131133486Sandre
132133486Sandre/*
1331541Srgrimes * Determine whether an IP address is in a reserved set of addresses
1341541Srgrimes * that may not be forwarded, or whether datagrams to that destination
1351541Srgrimes * may be forwarded.
1361541Srgrimes */
1371549Srgrimesint
138169454Srwatsonin_canforward(struct in_addr in)
1391541Srgrimes{
1401541Srgrimes	register u_long i = ntohl(in.s_addr);
1411541Srgrimes	register u_long net;
1421541Srgrimes
143166450Sbms	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
1441541Srgrimes		return (0);
1451541Srgrimes	if (IN_CLASSA(i)) {
1461541Srgrimes		net = i & IN_CLASSA_NET;
1471541Srgrimes		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
1481541Srgrimes			return (0);
1491541Srgrimes	}
1501541Srgrimes	return (1);
1511541Srgrimes}
1521541Srgrimes
1531541Srgrimes/*
1541541Srgrimes * Trim a mask in a sockaddr
1551541Srgrimes */
15612296Sphkstatic void
157169454Srwatsonin_socktrim(struct sockaddr_in *ap)
1581541Srgrimes{
1591541Srgrimes    register char *cplim = (char *) &ap->sin_addr;
1601541Srgrimes    register char *cp = (char *) (&ap->sin_addr + 1);
1611541Srgrimes
1621541Srgrimes    ap->sin_len = 0;
1634127Swollman    while (--cp >= cplim)
164133874Srwatson	if (*cp) {
1651541Srgrimes	    (ap)->sin_len = cp - (char *) (ap) + 1;
1661541Srgrimes	    break;
1671541Srgrimes	}
1681541Srgrimes}
1691541Srgrimes
17055009Sshinstatic int
17155009Sshinin_mask2len(mask)
17255009Sshin	struct in_addr *mask;
17355009Sshin{
17455009Sshin	int x, y;
17555009Sshin	u_char *p;
17655009Sshin
17755009Sshin	p = (u_char *)mask;
17855009Sshin	for (x = 0; x < sizeof(*mask); x++) {
17955009Sshin		if (p[x] != 0xff)
18055009Sshin			break;
18155009Sshin	}
18255009Sshin	y = 0;
18355009Sshin	if (x < sizeof(*mask)) {
18455009Sshin		for (y = 0; y < 8; y++) {
18555009Sshin			if ((p[x] & (0x80 >> y)) == 0)
18655009Sshin				break;
18755009Sshin		}
18855009Sshin	}
189184295Sbz	return (x * 8 + y);
19055009Sshin}
19155009Sshin
19255009Sshinstatic void
193169454Srwatsonin_len2mask(struct in_addr *mask, int len)
19455009Sshin{
19555009Sshin	int i;
19655009Sshin	u_char *p;
19755009Sshin
19855009Sshin	p = (u_char *)mask;
19955009Sshin	bzero(mask, sizeof(*mask));
20055009Sshin	for (i = 0; i < len / 8; i++)
20155009Sshin		p[i] = 0xff;
20255009Sshin	if (len % 8)
20355009Sshin		p[i] = (0xff00 >> (len % 8)) & 0xff;
20455009Sshin}
20555009Sshin
2061541Srgrimes/*
2071541Srgrimes * Generic internet control operations (ioctl's).
208191443Srwatson *
209191443Srwatson * ifp is NULL if not an interface-specific ioctl.
2101541Srgrimes */
2111541Srgrimes/* ARGSUSED */
2121549Srgrimesint
213169454Srwatsonin_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
214169454Srwatson    struct thread *td)
2151541Srgrimes{
2161541Srgrimes	register struct ifreq *ifr = (struct ifreq *)data;
217184295Sbz	register struct in_ifaddr *ia, *iap;
2181541Srgrimes	register struct ifaddr *ifa;
219168032Sbms	struct in_addr allhosts_addr;
22084102Sjlemon	struct in_addr dst;
221189592Sbms	struct in_ifinfo *ii;
2221541Srgrimes	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
2231541Srgrimes	struct sockaddr_in oldaddr;
224194951Srwatson	int error, hostIsNew, iaIsNew, maskIsNew;
225168032Sbms	int iaIsFirst;
2261541Srgrimes
227184295Sbz	ia = NULL;
228168032Sbms	iaIsFirst = 0;
22987124Sbrian	iaIsNew = 0;
230168032Sbms	allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
23187124Sbrian
232191443Srwatson	/*
233191443Srwatson	 * Filter out ioctls we implement directly; forward the rest on to
234191443Srwatson	 * in_lifaddr_ioctl() and ifp->if_ioctl().
235191443Srwatson	 */
23655009Sshin	switch (cmd) {
237191443Srwatson	case SIOCGIFADDR:
238191443Srwatson	case SIOCGIFBRDADDR:
239191443Srwatson	case SIOCGIFDSTADDR:
240191443Srwatson	case SIOCGIFNETMASK:
241227791Sglebius	case SIOCDIFADDR:
242227791Sglebius		break;
243227791Sglebius	case SIOCAIFADDR:
244227791Sglebius		/*
245227791Sglebius		 * ifra_addr must be present and be of INET family.
246227791Sglebius		 * ifra_broadaddr and ifra_mask are optional.
247227791Sglebius		 */
248227791Sglebius		if (ifra->ifra_addr.sin_len != sizeof(struct sockaddr_in) ||
249227791Sglebius		    ifra->ifra_addr.sin_family != AF_INET)
250227791Sglebius			return (EINVAL);
251227791Sglebius		if (ifra->ifra_broadaddr.sin_len != 0 &&
252227831Sglebius		    (ifra->ifra_broadaddr.sin_len !=
253227831Sglebius		    sizeof(struct sockaddr_in) ||
254227791Sglebius		    ifra->ifra_broadaddr.sin_family != AF_INET))
255227791Sglebius			return (EINVAL);
256227791Sglebius#if 0
257227791Sglebius		/*
258227791Sglebius		 * ifconfig(8) historically doesn't set af_family for mask
259227791Sglebius		 * for unknown reason.
260227791Sglebius		 */
261227791Sglebius		if (ifra->ifra_mask.sin_len != 0 &&
262227791Sglebius		    (ifra->ifra_mask.sin_len != sizeof(struct sockaddr_in) ||
263227791Sglebius		    ifra->ifra_mask.sin_family != AF_INET))
264227791Sglebius			return (EINVAL);
265227791Sglebius#endif
266227791Sglebius		break;
267191443Srwatson	case SIOCSIFADDR:
268191443Srwatson	case SIOCSIFBRDADDR:
269191443Srwatson	case SIOCSIFDSTADDR:
270191443Srwatson	case SIOCSIFNETMASK:
271227791Sglebius		if (ifr->ifr_addr.sa_family != AF_INET ||
272227791Sglebius		    ifr->ifr_addr.sa_len != sizeof(struct sockaddr_in))
273227791Sglebius			return (EINVAL);
274191443Srwatson		break;
275191443Srwatson
27655009Sshin	case SIOCALIFADDR:
277164033Srwatson		if (td != NULL) {
278164033Srwatson			error = priv_check(td, PRIV_NET_ADDIFADDR);
279164033Srwatson			if (error)
280164033Srwatson				return (error);
281164033Srwatson		}
282184295Sbz		if (ifp == NULL)
283184295Sbz			return (EINVAL);
284164033Srwatson		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
285164033Srwatson
28655009Sshin	case SIOCDLIFADDR:
287164033Srwatson		if (td != NULL) {
288164033Srwatson			error = priv_check(td, PRIV_NET_DELIFADDR);
289164033Srwatson			if (error)
290164033Srwatson				return (error);
291164033Srwatson		}
292184295Sbz		if (ifp == NULL)
293184295Sbz			return (EINVAL);
294164033Srwatson		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
295164033Srwatson
29655009Sshin	case SIOCGLIFADDR:
297184295Sbz		if (ifp == NULL)
298184295Sbz			return (EINVAL);
29983366Sjulian		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
300191443Srwatson
301191443Srwatson	default:
302191443Srwatson		if (ifp == NULL || ifp->if_ioctl == NULL)
303191443Srwatson			return (EOPNOTSUPP);
304191443Srwatson		return ((*ifp->if_ioctl)(ifp, cmd, data));
30555009Sshin	}
30655009Sshin
307191443Srwatson	if (ifp == NULL)
308191443Srwatson		return (EADDRNOTAVAIL);
309191443Srwatson
3101541Srgrimes	/*
311191456Srwatson	 * Security checks before we get involved in any work.
312191456Srwatson	 */
313191456Srwatson	switch (cmd) {
314191456Srwatson	case SIOCAIFADDR:
315191456Srwatson	case SIOCSIFADDR:
316191456Srwatson	case SIOCSIFBRDADDR:
317191456Srwatson	case SIOCSIFNETMASK:
318191456Srwatson	case SIOCSIFDSTADDR:
319191456Srwatson		if (td != NULL) {
320191456Srwatson			error = priv_check(td, PRIV_NET_ADDIFADDR);
321191456Srwatson			if (error)
322191456Srwatson				return (error);
323191456Srwatson		}
324191456Srwatson		break;
325191456Srwatson
326191456Srwatson	case SIOCDIFADDR:
327191456Srwatson		if (td != NULL) {
328191456Srwatson			error = priv_check(td, PRIV_NET_DELIFADDR);
329191456Srwatson			if (error)
330191456Srwatson				return (error);
331191456Srwatson		}
332191456Srwatson		break;
333191456Srwatson	}
334191456Srwatson
335191456Srwatson	/*
3361541Srgrimes	 * Find address for this interface, if it exists.
33714632Sfenner	 *
338191443Srwatson	 * If an alias address was specified, find that one instead of the
339191443Srwatson	 * first one on the interface, if possible.
3401541Srgrimes	 */
341191443Srwatson	dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
342194951Srwatson	IN_IFADDR_RLOCK();
343191443Srwatson	LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) {
344191443Srwatson		if (iap->ia_ifp == ifp &&
345191443Srwatson		    iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
346191443Srwatson			if (td == NULL || prison_check_ip4(td->td_ucred,
347191443Srwatson			    &dst) == 0)
348191443Srwatson				ia = iap;
349191443Srwatson			break;
350191443Srwatson		}
351191443Srwatson	}
352194760Srwatson	if (ia != NULL)
353194760Srwatson		ifa_ref(&ia->ia_ifa);
354194951Srwatson	IN_IFADDR_RUNLOCK();
355191443Srwatson	if (ia == NULL) {
356194760Srwatson		IF_ADDR_LOCK(ifp);
357191443Srwatson		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
358191443Srwatson			iap = ifatoia(ifa);
359191443Srwatson			if (iap->ia_addr.sin_family == AF_INET) {
360191443Srwatson				if (td != NULL &&
361191443Srwatson				    prison_check_ip4(td->td_ucred,
362191443Srwatson				    &iap->ia_addr.sin_addr) != 0)
363191443Srwatson					continue;
364191443Srwatson				ia = iap;
36584102Sjlemon				break;
36684102Sjlemon			}
367191443Srwatson		}
368194760Srwatson		if (ia != NULL)
369194760Srwatson			ifa_ref(&ia->ia_ifa);
370194760Srwatson		IF_ADDR_UNLOCK(ifp);
37184102Sjlemon	}
372191443Srwatson	if (ia == NULL)
373191443Srwatson		iaIsFirst = 1;
3741541Srgrimes
375191500Srwatson	error = 0;
3761541Srgrimes	switch (cmd) {
3771541Srgrimes	case SIOCAIFADDR:
3781541Srgrimes	case SIOCDIFADDR:
379227958Sglebius		if (ifra->ifra_addr.sin_family == AF_INET) {
380194760Srwatson			struct in_ifaddr *oia;
381194760Srwatson
382194951Srwatson			IN_IFADDR_RLOCK();
38371999Sphk			for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
3848071Swollman				if (ia->ia_ifp == ifp  &&
3858071Swollman				    ia->ia_addr.sin_addr.s_addr ==
3868071Swollman				    ifra->ifra_addr.sin_addr.s_addr)
3878071Swollman					break;
3888071Swollman			}
389194760Srwatson			if (ia != NULL && ia != oia)
390194760Srwatson				ifa_ref(&ia->ia_ifa);
391194760Srwatson			if (oia != NULL && ia != oia)
392194760Srwatson				ifa_free(&oia->ia_ifa);
393194951Srwatson			IN_IFADDR_RUNLOCK();
3948876Srgrimes			if ((ifp->if_flags & IFF_POINTOPOINT)
3958071Swollman			    && (cmd == SIOCAIFADDR)
3968071Swollman			    && (ifra->ifra_dstaddr.sin_addr.s_addr
3978071Swollman				== INADDR_ANY)) {
398191500Srwatson				error = EDESTADDRREQ;
399194760Srwatson				goto out;
4008071Swollman			}
4011541Srgrimes		}
402191500Srwatson		if (cmd == SIOCDIFADDR && ia == NULL) {
403191500Srwatson			error = EADDRNOTAVAIL;
404194760Srwatson			goto out;
405191500Srwatson		}
4061541Srgrimes		/* FALLTHROUGH */
4071541Srgrimes	case SIOCSIFADDR:
4081541Srgrimes	case SIOCSIFNETMASK:
4091541Srgrimes	case SIOCSIFDSTADDR:
410184295Sbz		if (ia == NULL) {
41120407Swollman			ia = (struct in_ifaddr *)
412191500Srwatson				malloc(sizeof *ia, M_IFADDR, M_NOWAIT |
413191500Srwatson				    M_ZERO);
414191500Srwatson			if (ia == NULL) {
415191500Srwatson				error = ENOBUFS;
416194760Srwatson				goto out;
417191500Srwatson			}
418191500Srwatson
41920407Swollman			ifa = &ia->ia_ifa;
420194602Srwatson			ifa_init(ifa);
42120407Swollman			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
42220407Swollman			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
42320407Swollman			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
424108033Shsu
4251541Srgrimes			ia->ia_sockmask.sin_len = 8;
42685740Sdes			ia->ia_sockmask.sin_family = AF_INET;
4271541Srgrimes			if (ifp->if_flags & IFF_BROADCAST) {
4281541Srgrimes				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
4291541Srgrimes				ia->ia_broadaddr.sin_family = AF_INET;
4301541Srgrimes			}
4311541Srgrimes			ia->ia_ifp = ifp;
432151824Sglebius
433194760Srwatson			ifa_ref(ifa);			/* if_addrhead */
434194760Srwatson			IF_ADDR_LOCK(ifp);
435191285Srwatson			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
436194760Srwatson			IF_ADDR_UNLOCK(ifp);
437194760Srwatson			ifa_ref(ifa);			/* in_ifaddrhead */
438194951Srwatson			IN_IFADDR_WLOCK();
439181803Sbz			TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
440194951Srwatson			IN_IFADDR_WUNLOCK();
44187124Sbrian			iaIsNew = 1;
4421541Srgrimes		}
4431541Srgrimes		break;
4441541Srgrimes
4451541Srgrimes	case SIOCSIFBRDADDR:
4461541Srgrimes	case SIOCGIFADDR:
4471541Srgrimes	case SIOCGIFNETMASK:
4481541Srgrimes	case SIOCGIFDSTADDR:
4491541Srgrimes	case SIOCGIFBRDADDR:
450191500Srwatson		if (ia == NULL) {
451191500Srwatson			error = EADDRNOTAVAIL;
452194760Srwatson			goto out;
453191500Srwatson		}
4541541Srgrimes		break;
4551541Srgrimes	}
456191500Srwatson
457191500Srwatson	/*
458194760Srwatson	 * Most paths in this switch return directly or via out.  Only paths
459194760Srwatson	 * that remove the address break in order to hit common removal code.
460191500Srwatson	 */
4611541Srgrimes	switch (cmd) {
4621541Srgrimes	case SIOCGIFADDR:
4631541Srgrimes		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
464194760Srwatson		goto out;
4651541Srgrimes
4661541Srgrimes	case SIOCGIFBRDADDR:
467191500Srwatson		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
468191500Srwatson			error = EINVAL;
469194760Srwatson			goto out;
470191500Srwatson		}
4711541Srgrimes		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
472194760Srwatson		goto out;
4731541Srgrimes
4741541Srgrimes	case SIOCGIFDSTADDR:
475191500Srwatson		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
476191500Srwatson			error = EINVAL;
477194760Srwatson			goto out;
478191500Srwatson		}
4791541Srgrimes		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
480194760Srwatson		goto out;
4811541Srgrimes
4821541Srgrimes	case SIOCGIFNETMASK:
4831541Srgrimes		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
484194760Srwatson		goto out;
4851541Srgrimes
4861541Srgrimes	case SIOCSIFDSTADDR:
487191500Srwatson		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
488191500Srwatson			error = EINVAL;
489194760Srwatson			goto out;
490191500Srwatson		}
4911541Srgrimes		oldaddr = ia->ia_dstaddr;
4921541Srgrimes		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
493184295Sbz		if (ifp->if_ioctl != NULL) {
494146883Siedowse			error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
495146883Siedowse			    (caddr_t)ia);
496146883Siedowse			if (error) {
497146883Siedowse				ia->ia_dstaddr = oldaddr;
498194760Srwatson				goto out;
499146883Siedowse			}
5001541Srgrimes		}
5011541Srgrimes		if (ia->ia_flags & IFA_ROUTE) {
5021541Srgrimes			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
5031541Srgrimes			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
5041541Srgrimes			ia->ia_ifa.ifa_dstaddr =
5051541Srgrimes					(struct sockaddr *)&ia->ia_dstaddr;
5061541Srgrimes			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
5071541Srgrimes		}
508194760Srwatson		goto out;
5091541Srgrimes
5101541Srgrimes	case SIOCSIFBRDADDR:
511191500Srwatson		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
512191500Srwatson			error = EINVAL;
513194760Srwatson			goto out;
514191500Srwatson		}
5151541Srgrimes		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
516194760Srwatson		goto out;
5171541Srgrimes
5181541Srgrimes	case SIOCSIFADDR:
51987124Sbrian		error = in_ifinit(ifp, ia,
520228313Sglebius		    (struct sockaddr_in *) &ifr->ifr_addr, 1, 0);
52187124Sbrian		if (error != 0 && iaIsNew)
52287124Sbrian			break;
523168032Sbms		if (error == 0) {
524189603Sbms			ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
525189592Sbms			if (iaIsFirst &&
526189592Sbms			    (ifp->if_flags & IFF_MULTICAST) != 0) {
527189592Sbms				error = in_joingroup(ifp, &allhosts_addr,
528189592Sbms				    NULL, &ii->ii_allhosts);
529189592Sbms			}
530126264Smlaier			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
531168032Sbms		}
532194760Srwatson		error = 0;
533194760Srwatson		goto out;
5341541Srgrimes
5351541Srgrimes	case SIOCSIFNETMASK:
536228062Sglebius		ia->ia_sockmask.sin_addr = ((struct sockaddr_in *)
537228062Sglebius		    &ifr->ifr_addr)->sin_addr;
53885740Sdes		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
539194760Srwatson		goto out;
5401541Srgrimes
5411541Srgrimes	case SIOCAIFADDR:
5421541Srgrimes		maskIsNew = 0;
5431541Srgrimes		hostIsNew = 1;
5441541Srgrimes		error = 0;
545227959Sglebius		if (ifra->ifra_addr.sin_addr.s_addr ==
546227791Sglebius			    ia->ia_addr.sin_addr.s_addr)
547227791Sglebius			hostIsNew = 0;
5481541Srgrimes		if (ifra->ifra_mask.sin_len) {
549197210Sqingli			/*
550197210Sqingli			 * QL: XXX
551197210Sqingli			 * Need to scrub the prefix here in case
552197210Sqingli			 * the issued command is SIOCAIFADDR with
553197210Sqingli			 * the same address, but with a different
554197210Sqingli			 * prefix length. And if the prefix length
555197210Sqingli			 * is the same as before, then the call is
556197210Sqingli			 * un-necessarily executed here.
557197210Sqingli			 */
558222438Sqingli			in_ifscrub(ifp, ia, LLE_STATIC);
5591541Srgrimes			ia->ia_sockmask = ifra->ifra_mask;
56085740Sdes			ia->ia_sockmask.sin_family = AF_INET;
5611541Srgrimes			ia->ia_subnetmask =
5621541Srgrimes			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
5631541Srgrimes			maskIsNew = 1;
5641541Srgrimes		}
5651541Srgrimes		if ((ifp->if_flags & IFF_POINTOPOINT) &&
5661541Srgrimes		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
567222438Sqingli			in_ifscrub(ifp, ia, LLE_STATIC);
5681541Srgrimes			ia->ia_dstaddr = ifra->ifra_dstaddr;
5691541Srgrimes			maskIsNew  = 1; /* We lie; but the effect's the same */
5701541Srgrimes		}
571227801Sglebius		if (hostIsNew || maskIsNew)
572228313Sglebius			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0,
573228313Sglebius			    maskIsNew);
57487124Sbrian		if (error != 0 && iaIsNew)
575201811Sqingli			break;
57687124Sbrian
5771541Srgrimes		if ((ifp->if_flags & IFF_BROADCAST) &&
578227791Sglebius		    ifra->ifra_broadaddr.sin_len)
5791541Srgrimes			ia->ia_broadaddr = ifra->ifra_broadaddr;
580168032Sbms		if (error == 0) {
581189603Sbms			ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
582189592Sbms			if (iaIsFirst &&
583189592Sbms			    (ifp->if_flags & IFF_MULTICAST) != 0) {
584189592Sbms				error = in_joingroup(ifp, &allhosts_addr,
585189592Sbms				    NULL, &ii->ii_allhosts);
586189592Sbms			}
587126264Smlaier			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
588168032Sbms		}
589194760Srwatson		goto out;
5901541Srgrimes
5911541Srgrimes	case SIOCDIFADDR:
59274299Sru		/*
59374299Sru		 * in_ifscrub kills the interface route.
59474299Sru		 */
595222143Sqingli		in_ifscrub(ifp, ia, LLE_STATIC);
596191500Srwatson
59715092Sdg		/*
59874299Sru		 * in_ifadown gets rid of all the rest of
59974299Sru		 * the routes.  This is not quite the right
60074299Sru		 * thing to do, but at least if we are running
60174299Sru		 * a routing process they will come back.
60274299Sru		 */
60376469Sru		in_ifadown(&ia->ia_ifa, 1);
604126264Smlaier		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
60587124Sbrian		error = 0;
6061541Srgrimes		break;
6071541Srgrimes
6081541Srgrimes	default:
609191443Srwatson		panic("in_control: unsupported ioctl");
6101541Srgrimes	}
61187124Sbrian
612191285Srwatson	IF_ADDR_LOCK(ifp);
613213932Sbz	/* Re-check that ia is still part of the list. */
614213932Sbz	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
615213932Sbz		if (ifa == &ia->ia_ifa)
616213932Sbz			break;
617213932Sbz	}
618213932Sbz	if (ifa == NULL) {
619213932Sbz		/*
620213932Sbz		 * If we lost the race with another thread, there is no need to
621213932Sbz		 * try it again for the next loop as there is no other exit
622213932Sbz		 * path between here and out.
623213932Sbz		 */
624213932Sbz		IF_ADDR_UNLOCK(ifp);
625213932Sbz		error = EADDRNOTAVAIL;
626213932Sbz		goto out;
627213932Sbz	}
628191285Srwatson	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
629191285Srwatson	IF_ADDR_UNLOCK(ifp);
630194760Srwatson	ifa_free(&ia->ia_ifa);				/* if_addrhead */
631194951Srwatson
632194951Srwatson	IN_IFADDR_WLOCK();
633181803Sbz	TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
634194760Srwatson
635227791Sglebius	LIST_REMOVE(ia, ia_hash);
636227791Sglebius	IN_IFADDR_WUNLOCK();
637227791Sglebius	/*
638227791Sglebius	 * If this is the last IPv4 address configured on this
639227791Sglebius	 * interface, leave the all-hosts group.
640227791Sglebius	 * No state-change report need be transmitted.
641227791Sglebius	 */
642227791Sglebius	IFP_TO_IA(ifp, iap);
643227791Sglebius	if (iap == NULL) {
644227791Sglebius		ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
645227791Sglebius		IN_MULTI_LOCK();
646227791Sglebius		if (ii->ii_allhosts) {
647227791Sglebius			(void)in_leavegroup_locked(ii->ii_allhosts, NULL);
648227791Sglebius			ii->ii_allhosts = NULL;
649227791Sglebius		}
650227791Sglebius		IN_MULTI_UNLOCK();
651194951Srwatson	} else
652227791Sglebius		ifa_free(&iap->ia_ifa);
653227791Sglebius
654194951Srwatson	ifa_free(&ia->ia_ifa);				/* in_ifaddrhead */
655194760Srwatsonout:
656194760Srwatson	if (ia != NULL)
657194760Srwatson		ifa_free(&ia->ia_ifa);
65887124Sbrian	return (error);
6591541Srgrimes}
6601541Srgrimes
6611541Srgrimes/*
66255009Sshin * SIOC[GAD]LIFADDR.
66355009Sshin *	SIOCGLIFADDR: get first address. (?!?)
66455009Sshin *	SIOCGLIFADDR with IFLR_PREFIX:
66555009Sshin *		get first address that matches the specified prefix.
66655009Sshin *	SIOCALIFADDR: add the specified address.
66755009Sshin *	SIOCALIFADDR with IFLR_PREFIX:
66855009Sshin *		EINVAL since we can't deduce hostid part of the address.
66955009Sshin *	SIOCDLIFADDR: delete the specified address.
67055009Sshin *	SIOCDLIFADDR with IFLR_PREFIX:
67155009Sshin *		delete the first address that matches the specified prefix.
67255009Sshin * return values:
67355009Sshin *	EINVAL on invalid parameters
67455009Sshin *	EADDRNOTAVAIL on prefix match failed/specified address not found
67555009Sshin *	other values may be returned from in_ioctl()
67655009Sshin */
67755009Sshinstatic int
678169454Srwatsonin_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
679169454Srwatson    struct ifnet *ifp, struct thread *td)
68055009Sshin{
68155009Sshin	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
68255009Sshin	struct ifaddr *ifa;
68355009Sshin
68455009Sshin	/* sanity checks */
685184295Sbz	if (data == NULL || ifp == NULL) {
68655009Sshin		panic("invalid argument to in_lifaddr_ioctl");
68755009Sshin		/*NOTRECHED*/
68855009Sshin	}
68955009Sshin
69055009Sshin	switch (cmd) {
69155009Sshin	case SIOCGLIFADDR:
69255009Sshin		/* address must be specified on GET with IFLR_PREFIX */
69355009Sshin		if ((iflr->flags & IFLR_PREFIX) == 0)
69455009Sshin			break;
69555009Sshin		/*FALLTHROUGH*/
69655009Sshin	case SIOCALIFADDR:
69755009Sshin	case SIOCDLIFADDR:
69855009Sshin		/* address must be specified on ADD and DELETE */
69955917Sshin		if (iflr->addr.ss_family != AF_INET)
700184295Sbz			return (EINVAL);
70155917Sshin		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
702184295Sbz			return (EINVAL);
70355009Sshin		/* XXX need improvement */
70455917Sshin		if (iflr->dstaddr.ss_family
70555917Sshin		 && iflr->dstaddr.ss_family != AF_INET)
706184295Sbz			return (EINVAL);
70755917Sshin		if (iflr->dstaddr.ss_family
70855917Sshin		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
709184295Sbz			return (EINVAL);
71055009Sshin		break;
71155009Sshin	default: /*shouldn't happen*/
712184295Sbz		return (EOPNOTSUPP);
71355009Sshin	}
71455009Sshin	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
715184295Sbz		return (EINVAL);
71655009Sshin
71755009Sshin	switch (cmd) {
71855009Sshin	case SIOCALIFADDR:
71955009Sshin	    {
72055009Sshin		struct in_aliasreq ifra;
72155009Sshin
72255009Sshin		if (iflr->flags & IFLR_PREFIX)
723184295Sbz			return (EINVAL);
72455009Sshin
72555009Sshin		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
72655009Sshin		bzero(&ifra, sizeof(ifra));
72755009Sshin		bcopy(iflr->iflr_name, ifra.ifra_name,
72855009Sshin			sizeof(ifra.ifra_name));
72955009Sshin
73055917Sshin		bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
73155009Sshin
73255917Sshin		if (iflr->dstaddr.ss_family) {	/*XXX*/
73355009Sshin			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
73455917Sshin				iflr->dstaddr.ss_len);
73555009Sshin		}
73655009Sshin
73755009Sshin		ifra.ifra_mask.sin_family = AF_INET;
73855009Sshin		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
73955009Sshin		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
74055009Sshin
741184295Sbz		return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td));
74255009Sshin	    }
74355009Sshin	case SIOCGLIFADDR:
74455009Sshin	case SIOCDLIFADDR:
74555009Sshin	    {
74655009Sshin		struct in_ifaddr *ia;
74755009Sshin		struct in_addr mask, candidate, match;
74855009Sshin		struct sockaddr_in *sin;
74955009Sshin
75055009Sshin		bzero(&mask, sizeof(mask));
751170855Smjacob		bzero(&match, sizeof(match));
75255009Sshin		if (iflr->flags & IFLR_PREFIX) {
75355009Sshin			/* lookup a prefix rather than address. */
75455009Sshin			in_len2mask(&mask, iflr->prefixlen);
75555009Sshin
75655009Sshin			sin = (struct sockaddr_in *)&iflr->addr;
75755009Sshin			match.s_addr = sin->sin_addr.s_addr;
75855009Sshin			match.s_addr &= mask.s_addr;
75955009Sshin
76055009Sshin			/* if you set extra bits, that's wrong */
76155009Sshin			if (match.s_addr != sin->sin_addr.s_addr)
762184295Sbz				return (EINVAL);
76355009Sshin
76455009Sshin		} else {
765170855Smjacob			/* on getting an address, take the 1st match */
766170855Smjacob			/* on deleting an address, do exact match */
767170855Smjacob			if (cmd != SIOCGLIFADDR) {
76855009Sshin				in_len2mask(&mask, 32);
76955009Sshin				sin = (struct sockaddr_in *)&iflr->addr;
77055009Sshin				match.s_addr = sin->sin_addr.s_addr;
77155009Sshin			}
77255009Sshin		}
77355009Sshin
77455009Sshin		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)	{
77555009Sshin			if (ifa->ifa_addr->sa_family != AF_INET6)
77655009Sshin				continue;
777170855Smjacob			if (match.s_addr == 0)
77855009Sshin				break;
77955009Sshin			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
78055009Sshin			candidate.s_addr &= mask.s_addr;
78155009Sshin			if (candidate.s_addr == match.s_addr)
78255009Sshin				break;
78355009Sshin		}
784184295Sbz		if (ifa == NULL)
785184295Sbz			return (EADDRNOTAVAIL);
78655009Sshin		ia = (struct in_ifaddr *)ifa;
78755009Sshin
78855009Sshin		if (cmd == SIOCGLIFADDR) {
78955009Sshin			/* fill in the if_laddrreq structure */
79055009Sshin			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
79155009Sshin
79255009Sshin			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
79355009Sshin				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
79455009Sshin					ia->ia_dstaddr.sin_len);
79555009Sshin			} else
79655009Sshin				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
79755009Sshin
79855009Sshin			iflr->prefixlen =
79955009Sshin				in_mask2len(&ia->ia_sockmask.sin_addr);
80055009Sshin
80155009Sshin			iflr->flags = 0;	/*XXX*/
80255009Sshin
803184295Sbz			return (0);
80455009Sshin		} else {
80555009Sshin			struct in_aliasreq ifra;
80655009Sshin
80755009Sshin			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
80855009Sshin			bzero(&ifra, sizeof(ifra));
80955009Sshin			bcopy(iflr->iflr_name, ifra.ifra_name,
81055009Sshin				sizeof(ifra.ifra_name));
81155009Sshin
81255009Sshin			bcopy(&ia->ia_addr, &ifra.ifra_addr,
81355009Sshin				ia->ia_addr.sin_len);
81455009Sshin			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
81555009Sshin				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
81655009Sshin					ia->ia_dstaddr.sin_len);
81755009Sshin			}
81855009Sshin			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
81955009Sshin				ia->ia_sockmask.sin_len);
82055009Sshin
821184295Sbz			return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
822184295Sbz			    ifp, td));
82355009Sshin		}
82455009Sshin	    }
82555009Sshin	}
82655009Sshin
827184295Sbz	return (EOPNOTSUPP);	/*just for safety*/
82855009Sshin}
82955009Sshin
83055009Sshin/*
8311541Srgrimes * Delete any existing route for an interface.
8321541Srgrimes */
83322672Swollmanvoid
834222143Sqingliin_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia, u_int flags)
8351541Srgrimes{
836169454Srwatson
837222143Sqingli	in_scrubprefix(ia, flags);
8381541Srgrimes}
8391541Srgrimes
8401541Srgrimes/*
8411541Srgrimes * Initialize an interface's internet address
8421541Srgrimes * and routing table entry.
8431541Srgrimes */
84412296Sphkstatic int
845169454Srwatsonin_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
846228313Sglebius    int scrub, int masksupplied)
8471541Srgrimes{
8481541Srgrimes	register u_long i = ntohl(sin->sin_addr.s_addr);
849226339Sglebius	int flags = RTF_UP, error = 0;
8501541Srgrimes
851227791Sglebius	if (scrub)
852227791Sglebius		in_scrubprefix(ia, LLE_STATIC);
853227791Sglebius
854227791Sglebius	IN_IFADDR_WLOCK();
855227791Sglebius	if (ia->ia_addr.sin_family == AF_INET)
856105748Ssuz		LIST_REMOVE(ia, ia_hash);
8571541Srgrimes	ia->ia_addr = *sin;
858227791Sglebius	LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
859227791Sglebius	    ia, ia_hash);
860227791Sglebius	IN_IFADDR_WUNLOCK();
861227791Sglebius
8621541Srgrimes	/*
8631541Srgrimes	 * Give the interface a chance to initialize
8641541Srgrimes	 * if this is its first address,
8651541Srgrimes	 * and to validate the address if necessary.
8661541Srgrimes	 */
867227791Sglebius	if (ifp->if_ioctl != NULL &&
868227791Sglebius	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)) != 0)
869146883Siedowse			/* LIST_REMOVE(ia, ia_hash) is done in in_control */
870146883Siedowse			return (error);
871227791Sglebius
8721541Srgrimes	/*
873226401Sglebius	 * Be compatible with network classes, if netmask isn't supplied,
874226401Sglebius	 * guess it based on classes.
8751541Srgrimes	 */
876228313Sglebius	if (!masksupplied) {
877226401Sglebius		if (IN_CLASSA(i))
878226401Sglebius			ia->ia_subnetmask = IN_CLASSA_NET;
879226401Sglebius		else if (IN_CLASSB(i))
880226401Sglebius			ia->ia_subnetmask = IN_CLASSB_NET;
881226401Sglebius		else
882226401Sglebius			ia->ia_subnetmask = IN_CLASSC_NET;
8831541Srgrimes		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
884226401Sglebius	}
8851541Srgrimes	ia->ia_subnet = i & ia->ia_subnetmask;
8861541Srgrimes	in_socktrim(&ia->ia_sockmask);
8871541Srgrimes	/*
888143868Sglebius	 * XXX: carp(4) does not have interface route
889143868Sglebius	 */
890143868Sglebius	if (ifp->if_type == IFT_CARP)
891143868Sglebius		return (0);
892143868Sglebius	/*
8931541Srgrimes	 * Add route for the network.
8941541Srgrimes	 */
8951541Srgrimes	ia->ia_ifa.ifa_metric = ifp->if_metric;
8961541Srgrimes	if (ifp->if_flags & IFF_BROADCAST) {
897226402Sglebius		if (ia->ia_subnetmask == IN_RFC3021_MASK)
898226402Sglebius			ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
899226402Sglebius		else
900226402Sglebius			ia->ia_broadaddr.sin_addr.s_addr =
901226402Sglebius			    htonl(ia->ia_subnet | ~ia->ia_subnetmask);
9021541Srgrimes	} else if (ifp->if_flags & IFF_LOOPBACK) {
903137833Smlaier		ia->ia_dstaddr = ia->ia_addr;
9041541Srgrimes		flags |= RTF_HOST;
9051541Srgrimes	} else if (ifp->if_flags & IFF_POINTOPOINT) {
9061541Srgrimes		if (ia->ia_dstaddr.sin_family != AF_INET)
9071541Srgrimes			return (0);
9081541Srgrimes		flags |= RTF_HOST;
9091541Srgrimes	}
910137628Smlaier	if ((error = in_addprefix(ia, flags)) != 0)
911137628Smlaier		return (error);
91294326Sbrian
913192085Sqingli	if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY)
914192085Sqingli		return (0);
915192085Sqingli
916227791Sglebius	if (ifp->if_flags & IFF_POINTOPOINT &&
917227791Sglebius	    ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
918203401Sqingli			return (0);
919203401Sqingli
920192011Sqingli	/*
921192011Sqingli	 * add a loopback route to self
922192011Sqingli	 */
923201282Sqingli	if (V_useloopback && !(ifp->if_flags & IFF_LOOPBACK)) {
924201282Sqingli		struct route ia_ro;
925201282Sqingli
926201282Sqingli		bzero(&ia_ro, sizeof(ia_ro));
927201282Sqingli		*((struct sockaddr_in *)(&ia_ro.ro_dst)) = ia->ia_addr;
928201282Sqingli		rtalloc_ign_fib(&ia_ro, 0, 0);
929201282Sqingli		if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
930201282Sqingli		    (ia_ro.ro_rt->rt_ifp == V_loif)) {
931201282Sqingli			RT_LOCK(ia_ro.ro_rt);
932201282Sqingli			RT_ADDREF(ia_ro.ro_rt);
933201282Sqingli			RTFREE_LOCKED(ia_ro.ro_rt);
934201282Sqingli		} else
935201282Sqingli			error = ifa_add_loopback_route((struct ifaddr *)ia,
936197227Sqingli				       (struct sockaddr *)&ia->ia_addr);
937201282Sqingli		if (error == 0)
938201282Sqingli			ia->ia_flags |= IFA_RTSELF;
939201282Sqingli		if (ia_ro.ro_rt != NULL)
940201282Sqingli			RTFREE(ia_ro.ro_rt);
941201282Sqingli	}
942192011Sqingli
9431541Srgrimes	return (error);
9441541Srgrimes}
9451541Srgrimes
946137628Smlaier#define rtinitflags(x) \
947137628Smlaier	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
948137628Smlaier	    ? RTF_HOST : 0)
949201285Sqingli
950137628Smlaier/*
951201285Sqingli * Generate a routing message when inserting or deleting
952201285Sqingli * an interface address alias.
953201285Sqingli */
954201285Sqinglistatic void in_addralias_rtmsg(int cmd, struct in_addr *prefix,
955201285Sqingli    struct in_ifaddr *target)
956201285Sqingli{
957201285Sqingli	struct route pfx_ro;
958201285Sqingli	struct sockaddr_in *pfx_addr;
959201285Sqingli	struct rtentry msg_rt;
960201285Sqingli
961201285Sqingli	/* QL: XXX
962201285Sqingli	 * This is a bit questionable because there is no
963201285Sqingli	 * additional route entry added/deleted for an address
964201285Sqingli	 * alias. Therefore this route report is inaccurate.
965201285Sqingli	 */
966201285Sqingli	bzero(&pfx_ro, sizeof(pfx_ro));
967201285Sqingli	pfx_addr = (struct sockaddr_in *)(&pfx_ro.ro_dst);
968201285Sqingli	pfx_addr->sin_len = sizeof(*pfx_addr);
969201285Sqingli	pfx_addr->sin_family = AF_INET;
970201285Sqingli	pfx_addr->sin_addr = *prefix;
971201285Sqingli	rtalloc_ign_fib(&pfx_ro, 0, 0);
972201285Sqingli	if (pfx_ro.ro_rt != NULL) {
973201285Sqingli		msg_rt = *pfx_ro.ro_rt;
974201285Sqingli
975201285Sqingli		/* QL: XXX
976201285Sqingli		 * Point the gateway to the new interface
977201285Sqingli		 * address as if a new prefix route entry has
978201285Sqingli		 * been added through the new address alias.
979201285Sqingli		 * All other parts of the rtentry is accurate,
980201285Sqingli		 * e.g., rt_key, rt_mask, rt_ifp etc.
981201285Sqingli		 */
982201285Sqingli		msg_rt.rt_gateway =
983201285Sqingli			(struct sockaddr *)&target->ia_addr;
984201285Sqingli		rt_newaddrmsg(cmd,
985201285Sqingli			      (struct ifaddr *)target,
986201285Sqingli			      0, &msg_rt);
987201285Sqingli		RTFREE(pfx_ro.ro_rt);
988201285Sqingli	}
989201285Sqingli	return;
990201285Sqingli}
991201285Sqingli
992201285Sqingli/*
993170855Smjacob * Check if we have a route for the given prefix already or add one accordingly.
994137628Smlaier */
995137628Smlaierstatic int
996169454Srwatsonin_addprefix(struct in_ifaddr *target, int flags)
997137628Smlaier{
998137628Smlaier	struct in_ifaddr *ia;
999151555Sglebius	struct in_addr prefix, mask, p, m;
1000137628Smlaier	int error;
10011541Srgrimes
1002170855Smjacob	if ((flags & RTF_HOST) != 0) {
1003137628Smlaier		prefix = target->ia_dstaddr.sin_addr;
1004170855Smjacob		mask.s_addr = 0;
1005170855Smjacob	} else {
1006137628Smlaier		prefix = target->ia_addr.sin_addr;
1007137628Smlaier		mask = target->ia_sockmask.sin_addr;
1008137628Smlaier		prefix.s_addr &= mask.s_addr;
1009137628Smlaier	}
1010137628Smlaier
1011194951Srwatson	IN_IFADDR_RLOCK();
1012181803Sbz	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1013151555Sglebius		if (rtinitflags(ia)) {
1014224747Skevlo			p = ia->ia_dstaddr.sin_addr;
1015151555Sglebius
1016151555Sglebius			if (prefix.s_addr != p.s_addr)
1017151555Sglebius				continue;
1018151555Sglebius		} else {
1019151555Sglebius			p = ia->ia_addr.sin_addr;
1020151555Sglebius			m = ia->ia_sockmask.sin_addr;
1021151555Sglebius			p.s_addr &= m.s_addr;
1022151555Sglebius
1023151555Sglebius			if (prefix.s_addr != p.s_addr ||
1024151555Sglebius			    mask.s_addr != m.s_addr)
1025151555Sglebius				continue;
1026137628Smlaier		}
1027137628Smlaier
1028137628Smlaier		/*
1029137628Smlaier		 * If we got a matching prefix route inserted by other
1030137628Smlaier		 * interface address, we are done here.
1031137628Smlaier		 */
1032149221Sglebius		if (ia->ia_flags & IFA_ROUTE) {
1033204902Sqingli#ifdef RADIX_MPATH
1034204902Sqingli			if (ia->ia_addr.sin_addr.s_addr ==
1035212209Sbz			    target->ia_addr.sin_addr.s_addr) {
1036212209Sbz				IN_IFADDR_RUNLOCK();
1037204902Sqingli				return (EEXIST);
1038212209Sbz			} else
1039204902Sqingli				break;
1040204902Sqingli#endif
1041181803Sbz			if (V_sameprefixcarponly &&
1042149221Sglebius			    target->ia_ifp->if_type != IFT_CARP &&
1043194951Srwatson			    ia->ia_ifp->if_type != IFT_CARP) {
1044194951Srwatson				IN_IFADDR_RUNLOCK();
1045149221Sglebius				return (EEXIST);
1046194951Srwatson			} else {
1047201285Sqingli				in_addralias_rtmsg(RTM_ADD, &prefix, target);
1048194951Srwatson				IN_IFADDR_RUNLOCK();
1049149221Sglebius				return (0);
1050194951Srwatson			}
1051149221Sglebius		}
1052137628Smlaier	}
1053194951Srwatson	IN_IFADDR_RUNLOCK();
1054137628Smlaier
1055137628Smlaier	/*
1056137628Smlaier	 * No-one seem to have this prefix route, so we try to insert it.
1057137628Smlaier	 */
1058137628Smlaier	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
1059137628Smlaier	if (!error)
1060137628Smlaier		target->ia_flags |= IFA_ROUTE;
1061184295Sbz	return (error);
1062137628Smlaier}
1063137628Smlaier
1064186119Sqingliextern void arp_ifscrub(struct ifnet *ifp, uint32_t addr);
1065186119Sqingli
10661541Srgrimes/*
1067137628Smlaier * If there is no other address in the system that can serve a route to the
1068137628Smlaier * same prefix, remove the route.  Hand over the route to the new address
1069137628Smlaier * otherwise.
1070137628Smlaier */
1071137628Smlaierstatic int
1072222143Sqingliin_scrubprefix(struct in_ifaddr *target, u_int flags)
1073137628Smlaier{
1074137628Smlaier	struct in_ifaddr *ia;
1075137628Smlaier	struct in_addr prefix, mask, p;
1076201282Sqingli	int error = 0;
1077192476Sqingli	struct sockaddr_in prefix0, mask0;
1078137628Smlaier
1079195914Sqingli	/*
1080195914Sqingli	 * Remove the loopback route to the interface address.
1081195914Sqingli	 * The "useloopback" setting is not consulted because if the
1082195914Sqingli	 * user configures an interface address, turns off this
1083195914Sqingli	 * setting, and then tries to delete that interface address,
1084195914Sqingli	 * checking the current setting of "useloopback" would leave
1085195914Sqingli	 * that interface address loopback route untouched, which
1086195914Sqingli	 * would be wrong. Therefore the interface address loopback route
1087195914Sqingli	 * deletion is unconditional.
1088195914Sqingli	 */
1089192085Sqingli	if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
1090201282Sqingli	    !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
1091201282Sqingli	    (target->ia_flags & IFA_RTSELF)) {
1092201282Sqingli		struct route ia_ro;
1093201282Sqingli		int freeit = 0;
1094201282Sqingli
1095201282Sqingli		bzero(&ia_ro, sizeof(ia_ro));
1096201282Sqingli		*((struct sockaddr_in *)(&ia_ro.ro_dst)) = target->ia_addr;
1097201282Sqingli		rtalloc_ign_fib(&ia_ro, 0, 0);
1098201282Sqingli		if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
1099201282Sqingli		    (ia_ro.ro_rt->rt_ifp == V_loif)) {
1100201282Sqingli			RT_LOCK(ia_ro.ro_rt);
1101201282Sqingli			if (ia_ro.ro_rt->rt_refcnt <= 1)
1102201282Sqingli				freeit = 1;
1103226114Sqingli			else if (flags & LLE_STATIC) {
1104201282Sqingli				RT_REMREF(ia_ro.ro_rt);
1105226114Sqingli				target->ia_flags &= ~IFA_RTSELF;
1106226114Sqingli			}
1107201282Sqingli			RTFREE_LOCKED(ia_ro.ro_rt);
1108201282Sqingli		}
1109222143Sqingli		if (freeit && (flags & LLE_STATIC)) {
1110201282Sqingli			error = ifa_del_loopback_route((struct ifaddr *)target,
1111197227Sqingli				       (struct sockaddr *)&target->ia_addr);
1112222143Sqingli			if (error == 0)
1113222143Sqingli				target->ia_flags &= ~IFA_RTSELF;
1114222143Sqingli		}
1115226120Sqingli		if ((flags & LLE_STATIC) &&
1116226120Sqingli			!(target->ia_ifp->if_flags & IFF_NOARP))
1117222143Sqingli			/* remove arp cache */
1118222143Sqingli			arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr);
1119192011Sqingli	}
1120192011Sqingli
1121137628Smlaier	if (rtinitflags(target))
1122137628Smlaier		prefix = target->ia_dstaddr.sin_addr;
1123137628Smlaier	else {
1124137628Smlaier		prefix = target->ia_addr.sin_addr;
1125137628Smlaier		mask = target->ia_sockmask.sin_addr;
1126137628Smlaier		prefix.s_addr &= mask.s_addr;
1127137628Smlaier	}
1128137628Smlaier
1129201285Sqingli	if ((target->ia_flags & IFA_ROUTE) == 0) {
1130201285Sqingli		in_addralias_rtmsg(RTM_DELETE, &prefix, target);
1131201285Sqingli		return (0);
1132201285Sqingli	}
1133201285Sqingli
1134194951Srwatson	IN_IFADDR_RLOCK();
1135181803Sbz	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1136137628Smlaier		if (rtinitflags(ia))
1137137628Smlaier			p = ia->ia_dstaddr.sin_addr;
1138137628Smlaier		else {
1139137628Smlaier			p = ia->ia_addr.sin_addr;
1140137628Smlaier			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1141137628Smlaier		}
1142137628Smlaier
1143225223Sqingli		if ((prefix.s_addr != p.s_addr) ||
1144225223Sqingli		    !(ia->ia_ifp->if_flags & IFF_UP))
1145137628Smlaier			continue;
1146137628Smlaier
1147137628Smlaier		/*
1148137628Smlaier		 * If we got a matching prefix address, move IFA_ROUTE and
1149137628Smlaier		 * the route itself to it.  Make sure that routing daemons
1150137628Smlaier		 * get a heads-up.
1151143868Sglebius		 *
1152211157Swill		 * XXX: a special case for carp(4) interface - this should
1153211157Swill		 *      be more generally specified as an interface that
1154211157Swill		 *      doesn't support such action.
1155137628Smlaier		 */
1156143868Sglebius		if ((ia->ia_flags & IFA_ROUTE) == 0
1157219828Spluknet		    && (ia->ia_ifp->if_type != IFT_CARP)) {
1158219828Spluknet			ifa_ref(&ia->ia_ifa);
1159194951Srwatson			IN_IFADDR_RUNLOCK();
1160222438Sqingli			error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
1161137628Smlaier			    rtinitflags(target));
1162222438Sqingli			if (error == 0)
1163222438Sqingli				target->ia_flags &= ~IFA_ROUTE;
1164222438Sqingli			else
1165222438Sqingli				log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
1166222438Sqingli					error);
1167137628Smlaier			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
1168137628Smlaier			    rtinitflags(ia) | RTF_UP);
1169137628Smlaier			if (error == 0)
1170137628Smlaier				ia->ia_flags |= IFA_ROUTE;
1171222438Sqingli			else
1172222438Sqingli				log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
1173222438Sqingli					error);
1174219828Spluknet			ifa_free(&ia->ia_ifa);
1175184295Sbz			return (error);
1176137628Smlaier		}
1177137628Smlaier	}
1178194951Srwatson	IN_IFADDR_RUNLOCK();
1179137628Smlaier
1180137628Smlaier	/*
1181192476Sqingli	 * remove all L2 entries on the given prefix
1182192476Sqingli	 */
1183192476Sqingli	bzero(&prefix0, sizeof(prefix0));
1184192476Sqingli	prefix0.sin_len = sizeof(prefix0);
1185192476Sqingli	prefix0.sin_family = AF_INET;
1186192476Sqingli	prefix0.sin_addr.s_addr = target->ia_subnet;
1187192476Sqingli	bzero(&mask0, sizeof(mask0));
1188192476Sqingli	mask0.sin_len = sizeof(mask0);
1189192476Sqingli	mask0.sin_family = AF_INET;
1190192476Sqingli	mask0.sin_addr.s_addr = target->ia_subnetmask;
1191192476Sqingli	lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0,
1192222143Sqingli			    (struct sockaddr *)&mask0, flags);
1193192476Sqingli
1194192476Sqingli	/*
1195137628Smlaier	 * As no-one seem to have this prefix, we can remove the route.
1196137628Smlaier	 */
1197222438Sqingli	error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
1198222438Sqingli	if (error == 0)
1199222438Sqingli		target->ia_flags &= ~IFA_ROUTE;
1200222438Sqingli	else
1201222438Sqingli		log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
1202222438Sqingli	return (error);
1203137628Smlaier}
1204137628Smlaier
1205137628Smlaier#undef rtinitflags
1206137628Smlaier
1207137628Smlaier/*
12081541Srgrimes * Return 1 if the address might be a local broadcast address.
12091541Srgrimes */
12101549Srgrimesint
1211169454Srwatsonin_broadcast(struct in_addr in, struct ifnet *ifp)
12121541Srgrimes{
12131541Srgrimes	register struct ifaddr *ifa;
12141541Srgrimes	u_long t;
12151541Srgrimes
12161541Srgrimes	if (in.s_addr == INADDR_BROADCAST ||
12171541Srgrimes	    in.s_addr == INADDR_ANY)
1218184295Sbz		return (1);
12191541Srgrimes	if ((ifp->if_flags & IFF_BROADCAST) == 0)
1220184295Sbz		return (0);
12211541Srgrimes	t = ntohl(in.s_addr);
12221541Srgrimes	/*
12231541Srgrimes	 * Look through the list of addresses for a match
12241541Srgrimes	 * with a broadcast address.
12251541Srgrimes	 */
12261541Srgrimes#define ia ((struct in_ifaddr *)ifa)
122774362Sphk	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
12281541Srgrimes		if (ifa->ifa_addr->sa_family == AF_INET &&
12291541Srgrimes		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
12301541Srgrimes		     /*
1231226402Sglebius		      * Check for old-style (host 0) broadcast, but
1232226402Sglebius		      * taking into account that RFC 3021 obsoletes it.
12331541Srgrimes		      */
1234226402Sglebius		     (ia->ia_subnetmask != IN_RFC3021_MASK &&
1235226402Sglebius		     t == ia->ia_subnet)) &&
123613351Sguido		     /*
123713351Sguido		      * Check for an all one subnetmask. These
123813351Sguido		      * only exist when an interface gets a secondary
123913351Sguido		      * address.
124013351Sguido		      */
124113351Sguido		     ia->ia_subnetmask != (u_long)0xffffffff)
1242184295Sbz			    return (1);
12431541Srgrimes	return (0);
12441541Srgrimes#undef ia
12451541Srgrimes}
1246167729Sbms
12471541Srgrimes/*
1248189592Sbms * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1249189592Sbms */
1250189592Sbmsvoid
1251189592Sbmsin_ifdetach(struct ifnet *ifp)
1252189592Sbms{
1253189592Sbms
1254189592Sbms	in_pcbpurgeif0(&V_ripcbinfo, ifp);
1255189592Sbms	in_pcbpurgeif0(&V_udbinfo, ifp);
1256189592Sbms	in_purgemaddrs(ifp);
1257189592Sbms}
1258189592Sbms
1259189592Sbms/*
1260167729Sbms * Delete all IPv4 multicast address records, and associated link-layer
1261167729Sbms * multicast address records, associated with ifp.
1262189592Sbms * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1263189931Sbms * XXX This should not race with ifma_protospec being set during
1264189931Sbms * a new allocation, if it does, we have bigger problems.
1265162718Sbms */
1266167729Sbmsstatic void
1267167729Sbmsin_purgemaddrs(struct ifnet *ifp)
1268162718Sbms{
1269189592Sbms	LIST_HEAD(,in_multi) purgeinms;
1270189592Sbms	struct in_multi		*inm, *tinm;
1271189592Sbms	struct ifmultiaddr	*ifma;
1272162718Sbms
1273189592Sbms	LIST_INIT(&purgeinms);
1274162718Sbms	IN_MULTI_LOCK();
1275189592Sbms
1276189592Sbms	/*
1277189592Sbms	 * Extract list of in_multi associated with the detaching ifp
1278189592Sbms	 * which the PF_INET layer is about to release.
1279189592Sbms	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1280189592Sbms	 * by code further down.
1281189592Sbms	 */
1282189592Sbms	IF_ADDR_LOCK(ifp);
1283189592Sbms	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1284189931Sbms		if (ifma->ifma_addr->sa_family != AF_INET ||
1285189931Sbms		    ifma->ifma_protospec == NULL)
1286189592Sbms			continue;
1287189931Sbms#if 0
1288189931Sbms		KASSERT(ifma->ifma_protospec != NULL,
1289189931Sbms		    ("%s: ifma_protospec is NULL", __func__));
1290189931Sbms#endif
1291189592Sbms		inm = (struct in_multi *)ifma->ifma_protospec;
1292189592Sbms		LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
1293162718Sbms	}
1294189592Sbms	IF_ADDR_UNLOCK(ifp);
1295150296Srwatson
1296189592Sbms	LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
1297191476Srwatson		LIST_REMOVE(inm, inm_link);
1298189592Sbms		inm_release_locked(inm);
1299189592Sbms	}
1300189592Sbms	igmp_ifdetach(ifp);
1301150296Srwatson
1302189592Sbms	IN_MULTI_UNLOCK();
1303150296Srwatson}
1304186119Sqingli
1305186119Sqingli#include <net/if_dl.h>
1306186119Sqingli#include <netinet/if_ether.h>
1307186119Sqingli
1308186119Sqinglistruct in_llentry {
1309186119Sqingli	struct llentry		base;
1310186119Sqingli	struct sockaddr_in	l3_addr4;
1311186119Sqingli};
1312186119Sqingli
1313186119Sqinglistatic struct llentry *
1314186119Sqingliin_lltable_new(const struct sockaddr *l3addr, u_int flags)
1315186119Sqingli{
1316186119Sqingli	struct in_llentry *lle;
1317186119Sqingli
1318186119Sqingli	lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO);
1319186119Sqingli	if (lle == NULL)		/* NB: caller generates msg */
1320186119Sqingli		return NULL;
1321186119Sqingli
1322186119Sqingli	callout_init(&lle->base.la_timer, CALLOUT_MPSAFE);
1323186119Sqingli	/*
1324186119Sqingli	 * For IPv4 this will trigger "arpresolve" to generate
1325186119Sqingli	 * an ARP request.
1326186119Sqingli	 */
1327216075Sglebius	lle->base.la_expire = time_uptime; /* mark expired */
1328186119Sqingli	lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
1329186119Sqingli	lle->base.lle_refcnt = 1;
1330186119Sqingli	LLE_LOCK_INIT(&lle->base);
1331186119Sqingli	return &lle->base;
1332186119Sqingli}
1333186119Sqingli
1334186119Sqingli/*
1335186119Sqingli * Deletes an address from the address table.
1336186119Sqingli * This function is called by the timer functions
1337186119Sqingli * such as arptimer() and nd6_llinfo_timer(), and
1338186119Sqingli * the caller does the locking.
1339186119Sqingli */
1340186119Sqinglistatic void
1341186119Sqingliin_lltable_free(struct lltable *llt, struct llentry *lle)
1342186119Sqingli{
1343186150Skmacy	LLE_WUNLOCK(lle);
1344186150Skmacy	LLE_LOCK_DESTROY(lle);
1345186119Sqingli	free(lle, M_LLTABLE);
1346186119Sqingli}
1347186119Sqingli
1348192476Sqingli
1349192476Sqingli#define IN_ARE_MASKED_ADDR_EQUAL(d, a, m)	(			\
1350192476Sqingli	    (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
1351192476Sqingli
1352192476Sqinglistatic void
1353192476Sqingliin_lltable_prefix_free(struct lltable *llt,
1354192476Sqingli		       const struct sockaddr *prefix,
1355222143Sqingli		       const struct sockaddr *mask,
1356222143Sqingli		       u_int flags)
1357192476Sqingli{
1358192476Sqingli	const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
1359192476Sqingli	const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
1360192476Sqingli	struct llentry *lle, *next;
1361192476Sqingli	register int i;
1362215207Sgnn	size_t pkts_dropped;
1363192476Sqingli
1364192476Sqingli	for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
1365192476Sqingli		LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
1366192476Sqingli
1367222143Sqingli		        /*
1368222143Sqingli			 * (flags & LLE_STATIC) means deleting all entries
1369222143Sqingli			 * including static ARP entries
1370222143Sqingli			 */
1371192476Sqingli			if (IN_ARE_MASKED_ADDR_EQUAL((struct sockaddr_in *)L3_ADDR(lle),
1372222143Sqingli						     pfx, msk) &&
1373222143Sqingli			    ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))) {
1374206481Sbz				int canceled;
1375206481Sbz
1376206481Sbz				canceled = callout_drain(&lle->la_timer);
1377192476Sqingli				LLE_WLOCK(lle);
1378206481Sbz				if (canceled)
1379206481Sbz					LLE_REMREF(lle);
1380215207Sgnn				pkts_dropped = llentry_free(lle);
1381215207Sgnn				ARPSTAT_ADD(dropped, pkts_dropped);
1382192476Sqingli			}
1383192476Sqingli		}
1384192476Sqingli	}
1385192476Sqingli}
1386192476Sqingli
1387192476Sqingli
1388186119Sqinglistatic int
1389201282Sqingliin_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1390186119Sqingli{
1391186119Sqingli	struct rtentry *rt;
1392186119Sqingli
1393186119Sqingli	KASSERT(l3addr->sa_family == AF_INET,
1394186119Sqingli	    ("sin_family %d", l3addr->sa_family));
1395186119Sqingli
1396186119Sqingli	/* XXX rtalloc1 should take a const param */
1397186119Sqingli	rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
1398223862Szec
1399225946Sqingli	if (rt == NULL)
1400225946Sqingli		return (EINVAL);
1401225946Sqingli
1402223862Szec	/*
1403223862Szec	 * If the gateway for an existing host route matches the target L3
1404225946Sqingli	 * address, which is a special route inserted by some implementation
1405225946Sqingli	 * such as MANET, and the interface is of the correct type, then
1406225946Sqingli	 * allow for ARP to proceed.
1407223862Szec	 */
1408225947Sqingli	if (rt->rt_flags & RTF_GATEWAY) {
1409226224Sqingli		if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
1410226224Sqingli			rt->rt_ifp->if_type != IFT_ETHER ||
1411226224Sqingli			  (rt->rt_ifp->if_flags &
1412226224Sqingli			   (IFF_NOARP | IFF_STATICARP)) != 0 ||
1413226224Sqingli			  memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
1414226224Sqingli				 sizeof(in_addr_t)) != 0) {
1415226224Sqingli			RTFREE_LOCKED(rt);
1416226224Sqingli			return (EINVAL);
1417226224Sqingli		}
1418225947Sqingli	}
1419225947Sqingli
1420225947Sqingli	/*
1421225947Sqingli	 * Make sure that at least the destination address is covered
1422225947Sqingli	 * by the route. This is for handling the case where 2 or more
1423225947Sqingli	 * interfaces have the same prefix. An incoming packet arrives
1424225947Sqingli	 * on one interface and the corresponding outgoing packet leaves
1425225947Sqingli	 * another interface.
1426225947Sqingli	 */
1427226713Sqingli	if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
1428226224Sqingli		const char *sa, *mask, *addr, *lim;
1429225947Sqingli		int len;
1430225947Sqingli
1431226713Sqingli		mask = (const char *)rt_mask(rt);
1432226713Sqingli		/*
1433226713Sqingli		 * Just being extra cautious to avoid some custom
1434226713Sqingli		 * code getting into trouble.
1435226713Sqingli		 */
1436226713Sqingli		if (mask == NULL) {
1437226713Sqingli			RTFREE_LOCKED(rt);
1438226713Sqingli			return (EINVAL);
1439226713Sqingli		}
1440226713Sqingli
1441226224Sqingli		sa = (const char *)rt_key(rt);
1442226224Sqingli		addr = (const char *)l3addr;
1443226224Sqingli		len = ((const struct sockaddr_in *)l3addr)->sin_len;
1444225947Sqingli		lim = addr + len;
1445225947Sqingli
1446225947Sqingli		for ( ; addr < lim; sa++, mask++, addr++) {
1447225947Sqingli			if ((*sa ^ *addr) & *mask) {
1448198418Sqingli#ifdef DIAGNOSTIC
1449225947Sqingli				log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
1450225947Sqingli				    inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1451197696Sqingli#endif
1452226224Sqingli				RTFREE_LOCKED(rt);
1453226224Sqingli				return (EINVAL);
1454225947Sqingli			}
1455225947Sqingli		}
1456186119Sqingli	}
1457225947Sqingli
1458186119Sqingli	RTFREE_LOCKED(rt);
1459226224Sqingli	return (0);
1460186119Sqingli}
1461186119Sqingli
1462186119Sqingli/*
1463186119Sqingli * Return NULL if not found or marked for deletion.
1464186119Sqingli * If found return lle read locked.
1465186119Sqingli */
1466186119Sqinglistatic struct llentry *
1467186119Sqingliin_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1468186119Sqingli{
1469186119Sqingli	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1470186119Sqingli	struct ifnet *ifp = llt->llt_ifp;
1471186119Sqingli	struct llentry *lle;
1472186119Sqingli	struct llentries *lleh;
1473186119Sqingli	u_int hashkey;
1474186119Sqingli
1475186119Sqingli	IF_AFDATA_LOCK_ASSERT(ifp);
1476186119Sqingli	KASSERT(l3addr->sa_family == AF_INET,
1477186119Sqingli	    ("sin_family %d", l3addr->sa_family));
1478186119Sqingli
1479186119Sqingli	hashkey = sin->sin_addr.s_addr;
1480186119Sqingli	lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
1481186119Sqingli	LIST_FOREACH(lle, lleh, lle_next) {
1482186708Sqingli		struct sockaddr_in *sa2 = (struct sockaddr_in *)L3_ADDR(lle);
1483186119Sqingli		if (lle->la_flags & LLE_DELETED)
1484186119Sqingli			continue;
1485186708Sqingli		if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
1486186119Sqingli			break;
1487186119Sqingli	}
1488186119Sqingli	if (lle == NULL) {
1489198418Sqingli#ifdef DIAGNOSTIC
1490186119Sqingli		if (flags & LLE_DELETE)
1491186119Sqingli			log(LOG_INFO, "interface address is missing from cache = %p  in delete\n", lle);
1492186119Sqingli#endif
1493186119Sqingli		if (!(flags & LLE_CREATE))
1494186119Sqingli			return (NULL);
1495186119Sqingli		/*
1496186119Sqingli		 * A route that covers the given address must have
1497186119Sqingli		 * been installed 1st because we are doing a resolution,
1498186119Sqingli		 * verify this.
1499186119Sqingli		 */
1500186119Sqingli		if (!(flags & LLE_IFADDR) &&
1501201282Sqingli		    in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1502186119Sqingli			goto done;
1503186119Sqingli
1504186119Sqingli		lle = in_lltable_new(l3addr, flags);
1505186119Sqingli		if (lle == NULL) {
1506186119Sqingli			log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1507186119Sqingli			goto done;
1508186119Sqingli		}
1509186119Sqingli		lle->la_flags = flags & ~LLE_CREATE;
1510186119Sqingli		if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
1511186119Sqingli			bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
1512186119Sqingli			lle->la_flags |= (LLE_VALID | LLE_STATIC);
1513186119Sqingli		}
1514186119Sqingli
1515186119Sqingli		lle->lle_tbl  = llt;
1516186119Sqingli		lle->lle_head = lleh;
1517186119Sqingli		LIST_INSERT_HEAD(lleh, lle, lle_next);
1518186119Sqingli	} else if (flags & LLE_DELETE) {
1519186119Sqingli		if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
1520186119Sqingli			LLE_WLOCK(lle);
1521186119Sqingli			lle->la_flags = LLE_DELETED;
1522196995Snp			EVENTHANDLER_INVOKE(arp_update_event, lle);
1523186119Sqingli			LLE_WUNLOCK(lle);
1524198418Sqingli#ifdef DIAGNOSTIC
1525186119Sqingli			log(LOG_INFO, "ifaddr cache = %p  is deleted\n", lle);
1526186119Sqingli#endif
1527186119Sqingli		}
1528186119Sqingli		lle = (void *)-1;
1529186119Sqingli
1530186119Sqingli	}
1531186544Sbz	if (LLE_IS_VALID(lle)) {
1532186119Sqingli		if (flags & LLE_EXCLUSIVE)
1533186119Sqingli			LLE_WLOCK(lle);
1534186119Sqingli		else
1535186119Sqingli			LLE_RLOCK(lle);
1536186119Sqingli	}
1537186119Sqinglidone:
1538186119Sqingli	return (lle);
1539186119Sqingli}
1540186119Sqingli
1541186119Sqinglistatic int
1542186119Sqingliin_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
1543186119Sqingli{
1544186119Sqingli#define	SIN(lle)	((struct sockaddr_in *) L3_ADDR(lle))
1545186119Sqingli	struct ifnet *ifp = llt->llt_ifp;
1546186119Sqingli	struct llentry *lle;
1547186119Sqingli	/* XXX stack use */
1548186119Sqingli	struct {
1549186119Sqingli		struct rt_msghdr	rtm;
1550186119Sqingli		struct sockaddr_inarp	sin;
1551186119Sqingli		struct sockaddr_dl	sdl;
1552186119Sqingli	} arpc;
1553186119Sqingli	int error, i;
1554186119Sqingli
1555196535Srwatson	LLTABLE_LOCK_ASSERT();
1556186119Sqingli
1557186119Sqingli	error = 0;
1558186119Sqingli	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1559186119Sqingli		LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
1560186119Sqingli			struct sockaddr_dl *sdl;
1561186119Sqingli
1562186119Sqingli			/* skip deleted entries */
1563198111Sqingli			if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1564186119Sqingli				continue;
1565186980Sbz			/* Skip if jailed and not a valid IP of the prison. */
1566188144Sjamie			if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1567186980Sbz				continue;
1568186119Sqingli			/*
1569186119Sqingli			 * produce a msg made of:
1570186119Sqingli			 *  struct rt_msghdr;
1571186119Sqingli			 *  struct sockaddr_inarp; (IPv4)
1572186119Sqingli			 *  struct sockaddr_dl;
1573186119Sqingli			 */
1574186119Sqingli			bzero(&arpc, sizeof(arpc));
1575186119Sqingli			arpc.rtm.rtm_msglen = sizeof(arpc);
1576186935Sharti			arpc.rtm.rtm_version = RTM_VERSION;
1577186935Sharti			arpc.rtm.rtm_type = RTM_GET;
1578186935Sharti			arpc.rtm.rtm_flags = RTF_UP;
1579186935Sharti			arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1580186119Sqingli			arpc.sin.sin_family = AF_INET;
1581186119Sqingli			arpc.sin.sin_len = sizeof(arpc.sin);
1582186119Sqingli			arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
1583186119Sqingli
1584186119Sqingli			/* publish */
1585186119Sqingli			if (lle->la_flags & LLE_PUB) {
1586186119Sqingli				arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1587186119Sqingli				/* proxy only */
1588186119Sqingli				if (lle->la_flags & LLE_PROXY)
1589186119Sqingli					arpc.sin.sin_other = SIN_PROXY;
1590186119Sqingli			}
1591186119Sqingli
1592186119Sqingli			sdl = &arpc.sdl;
1593186119Sqingli			sdl->sdl_family = AF_LINK;
1594186119Sqingli			sdl->sdl_len = sizeof(*sdl);
1595186119Sqingli			sdl->sdl_index = ifp->if_index;
1596186119Sqingli			sdl->sdl_type = ifp->if_type;
1597198111Sqingli			if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1598198111Sqingli				sdl->sdl_alen = ifp->if_addrlen;
1599198111Sqingli				bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1600198111Sqingli			} else {
1601198111Sqingli				sdl->sdl_alen = 0;
1602198111Sqingli				bzero(LLADDR(sdl), ifp->if_addrlen);
1603198111Sqingli			}
1604186119Sqingli
1605186119Sqingli			arpc.rtm.rtm_rmx.rmx_expire =
1606186119Sqingli			    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1607186500Sqingli			arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1608186119Sqingli			if (lle->la_flags & LLE_STATIC)
1609186119Sqingli				arpc.rtm.rtm_flags |= RTF_STATIC;
1610186119Sqingli			arpc.rtm.rtm_index = ifp->if_index;
1611186119Sqingli			error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1612186119Sqingli			if (error)
1613186119Sqingli				break;
1614186119Sqingli		}
1615186119Sqingli	}
1616186119Sqingli	return error;
1617186119Sqingli#undef SIN
1618186119Sqingli}
1619186119Sqingli
1620186119Sqinglivoid *
1621186119Sqingliin_domifattach(struct ifnet *ifp)
1622189592Sbms{
1623189592Sbms	struct in_ifinfo *ii;
1624189592Sbms	struct lltable *llt;
1625189592Sbms
1626189592Sbms	ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1627189592Sbms
1628189592Sbms	llt = lltable_init(ifp, AF_INET);
1629186119Sqingli	if (llt != NULL) {
1630186119Sqingli		llt->llt_free = in_lltable_free;
1631192476Sqingli		llt->llt_prefix_free = in_lltable_prefix_free;
1632186119Sqingli		llt->llt_lookup = in_lltable_lookup;
1633186119Sqingli		llt->llt_dump = in_lltable_dump;
1634186119Sqingli	}
1635189592Sbms	ii->ii_llt = llt;
1636189592Sbms
1637189592Sbms	ii->ii_igmp = igmp_domifattach(ifp);
1638189592Sbms
1639189592Sbms	return ii;
1640186119Sqingli}
1641186119Sqingli
1642186119Sqinglivoid
1643189592Sbmsin_domifdetach(struct ifnet *ifp, void *aux)
1644186119Sqingli{
1645189592Sbms	struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1646186119Sqingli
1647189592Sbms	igmp_domifdetach(ifp);
1648189592Sbms	lltable_free(ii->ii_llt);
1649189592Sbms	free(ii, M_IFADDR);
1650186119Sqingli}
1651