in.c revision 227831
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 227831 2011-11-22 19:39:27Z 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 *,
7692723Salfred	    struct in_ifaddr *, struct sockaddr_in *, 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:
379227830Sglebius		if (ifra->ifra_addr.sin_len == 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,
52087124Sbrian		    (struct sockaddr_in *) &ifr->ifr_addr, 1);
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:
536227791Sglebius		ia->ia_sockmask = *(struct sockaddr_in *)&ifr->ifr_addr;
53785740Sdes		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
538194760Srwatson		goto out;
5391541Srgrimes
5401541Srgrimes	case SIOCAIFADDR:
5411541Srgrimes		maskIsNew = 0;
5421541Srgrimes		hostIsNew = 1;
5431541Srgrimes		error = 0;
544227791Sglebius		if (ifra->ifra_addr.sin_len == 0) {
545227791Sglebius			ifra->ifra_addr = ia->ia_addr;
546227791Sglebius			hostIsNew = 0;
547227791Sglebius		} else if (ifra->ifra_addr.sin_addr.s_addr ==
548227791Sglebius			    ia->ia_addr.sin_addr.s_addr)
549227791Sglebius			hostIsNew = 0;
5501541Srgrimes		if (ifra->ifra_mask.sin_len) {
551197210Sqingli			/*
552197210Sqingli			 * QL: XXX
553197210Sqingli			 * Need to scrub the prefix here in case
554197210Sqingli			 * the issued command is SIOCAIFADDR with
555197210Sqingli			 * the same address, but with a different
556197210Sqingli			 * prefix length. And if the prefix length
557197210Sqingli			 * is the same as before, then the call is
558197210Sqingli			 * un-necessarily executed here.
559197210Sqingli			 */
560222438Sqingli			in_ifscrub(ifp, ia, LLE_STATIC);
5611541Srgrimes			ia->ia_sockmask = ifra->ifra_mask;
56285740Sdes			ia->ia_sockmask.sin_family = AF_INET;
5631541Srgrimes			ia->ia_subnetmask =
5641541Srgrimes			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
5651541Srgrimes			maskIsNew = 1;
5661541Srgrimes		}
5671541Srgrimes		if ((ifp->if_flags & IFF_POINTOPOINT) &&
5681541Srgrimes		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
569222438Sqingli			in_ifscrub(ifp, ia, LLE_STATIC);
5701541Srgrimes			ia->ia_dstaddr = ifra->ifra_dstaddr;
5711541Srgrimes			maskIsNew  = 1; /* We lie; but the effect's the same */
5721541Srgrimes		}
573227801Sglebius		if (hostIsNew || maskIsNew)
5741541Srgrimes			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
57587124Sbrian		if (error != 0 && iaIsNew)
576201811Sqingli			break;
57787124Sbrian
5781541Srgrimes		if ((ifp->if_flags & IFF_BROADCAST) &&
579227791Sglebius		    ifra->ifra_broadaddr.sin_len)
5801541Srgrimes			ia->ia_broadaddr = ifra->ifra_broadaddr;
581168032Sbms		if (error == 0) {
582189603Sbms			ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
583189592Sbms			if (iaIsFirst &&
584189592Sbms			    (ifp->if_flags & IFF_MULTICAST) != 0) {
585189592Sbms				error = in_joingroup(ifp, &allhosts_addr,
586189592Sbms				    NULL, &ii->ii_allhosts);
587189592Sbms			}
588126264Smlaier			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
589168032Sbms		}
590194760Srwatson		goto out;
5911541Srgrimes
5921541Srgrimes	case SIOCDIFADDR:
59374299Sru		/*
59474299Sru		 * in_ifscrub kills the interface route.
59574299Sru		 */
596222143Sqingli		in_ifscrub(ifp, ia, LLE_STATIC);
597191500Srwatson
59815092Sdg		/*
59974299Sru		 * in_ifadown gets rid of all the rest of
60074299Sru		 * the routes.  This is not quite the right
60174299Sru		 * thing to do, but at least if we are running
60274299Sru		 * a routing process they will come back.
60374299Sru		 */
60476469Sru		in_ifadown(&ia->ia_ifa, 1);
605126264Smlaier		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
60687124Sbrian		error = 0;
6071541Srgrimes		break;
6081541Srgrimes
6091541Srgrimes	default:
610191443Srwatson		panic("in_control: unsupported ioctl");
6111541Srgrimes	}
61287124Sbrian
613191285Srwatson	IF_ADDR_LOCK(ifp);
614213932Sbz	/* Re-check that ia is still part of the list. */
615213932Sbz	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
616213932Sbz		if (ifa == &ia->ia_ifa)
617213932Sbz			break;
618213932Sbz	}
619213932Sbz	if (ifa == NULL) {
620213932Sbz		/*
621213932Sbz		 * If we lost the race with another thread, there is no need to
622213932Sbz		 * try it again for the next loop as there is no other exit
623213932Sbz		 * path between here and out.
624213932Sbz		 */
625213932Sbz		IF_ADDR_UNLOCK(ifp);
626213932Sbz		error = EADDRNOTAVAIL;
627213932Sbz		goto out;
628213932Sbz	}
629191285Srwatson	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
630191285Srwatson	IF_ADDR_UNLOCK(ifp);
631194760Srwatson	ifa_free(&ia->ia_ifa);				/* if_addrhead */
632194951Srwatson
633194951Srwatson	IN_IFADDR_WLOCK();
634181803Sbz	TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
635194760Srwatson
636227791Sglebius	LIST_REMOVE(ia, ia_hash);
637227791Sglebius	IN_IFADDR_WUNLOCK();
638227791Sglebius	/*
639227791Sglebius	 * If this is the last IPv4 address configured on this
640227791Sglebius	 * interface, leave the all-hosts group.
641227791Sglebius	 * No state-change report need be transmitted.
642227791Sglebius	 */
643227791Sglebius	IFP_TO_IA(ifp, iap);
644227791Sglebius	if (iap == NULL) {
645227791Sglebius		ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
646227791Sglebius		IN_MULTI_LOCK();
647227791Sglebius		if (ii->ii_allhosts) {
648227791Sglebius			(void)in_leavegroup_locked(ii->ii_allhosts, NULL);
649227791Sglebius			ii->ii_allhosts = NULL;
650227791Sglebius		}
651227791Sglebius		IN_MULTI_UNLOCK();
652194951Srwatson	} else
653227791Sglebius		ifa_free(&iap->ia_ifa);
654227791Sglebius
655194951Srwatson	ifa_free(&ia->ia_ifa);				/* in_ifaddrhead */
656194760Srwatsonout:
657194760Srwatson	if (ia != NULL)
658194760Srwatson		ifa_free(&ia->ia_ifa);
65987124Sbrian	return (error);
6601541Srgrimes}
6611541Srgrimes
6621541Srgrimes/*
66355009Sshin * SIOC[GAD]LIFADDR.
66455009Sshin *	SIOCGLIFADDR: get first address. (?!?)
66555009Sshin *	SIOCGLIFADDR with IFLR_PREFIX:
66655009Sshin *		get first address that matches the specified prefix.
66755009Sshin *	SIOCALIFADDR: add the specified address.
66855009Sshin *	SIOCALIFADDR with IFLR_PREFIX:
66955009Sshin *		EINVAL since we can't deduce hostid part of the address.
67055009Sshin *	SIOCDLIFADDR: delete the specified address.
67155009Sshin *	SIOCDLIFADDR with IFLR_PREFIX:
67255009Sshin *		delete the first address that matches the specified prefix.
67355009Sshin * return values:
67455009Sshin *	EINVAL on invalid parameters
67555009Sshin *	EADDRNOTAVAIL on prefix match failed/specified address not found
67655009Sshin *	other values may be returned from in_ioctl()
67755009Sshin */
67855009Sshinstatic int
679169454Srwatsonin_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
680169454Srwatson    struct ifnet *ifp, struct thread *td)
68155009Sshin{
68255009Sshin	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
68355009Sshin	struct ifaddr *ifa;
68455009Sshin
68555009Sshin	/* sanity checks */
686184295Sbz	if (data == NULL || ifp == NULL) {
68755009Sshin		panic("invalid argument to in_lifaddr_ioctl");
68855009Sshin		/*NOTRECHED*/
68955009Sshin	}
69055009Sshin
69155009Sshin	switch (cmd) {
69255009Sshin	case SIOCGLIFADDR:
69355009Sshin		/* address must be specified on GET with IFLR_PREFIX */
69455009Sshin		if ((iflr->flags & IFLR_PREFIX) == 0)
69555009Sshin			break;
69655009Sshin		/*FALLTHROUGH*/
69755009Sshin	case SIOCALIFADDR:
69855009Sshin	case SIOCDLIFADDR:
69955009Sshin		/* address must be specified on ADD and DELETE */
70055917Sshin		if (iflr->addr.ss_family != AF_INET)
701184295Sbz			return (EINVAL);
70255917Sshin		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
703184295Sbz			return (EINVAL);
70455009Sshin		/* XXX need improvement */
70555917Sshin		if (iflr->dstaddr.ss_family
70655917Sshin		 && iflr->dstaddr.ss_family != AF_INET)
707184295Sbz			return (EINVAL);
70855917Sshin		if (iflr->dstaddr.ss_family
70955917Sshin		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
710184295Sbz			return (EINVAL);
71155009Sshin		break;
71255009Sshin	default: /*shouldn't happen*/
713184295Sbz		return (EOPNOTSUPP);
71455009Sshin	}
71555009Sshin	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
716184295Sbz		return (EINVAL);
71755009Sshin
71855009Sshin	switch (cmd) {
71955009Sshin	case SIOCALIFADDR:
72055009Sshin	    {
72155009Sshin		struct in_aliasreq ifra;
72255009Sshin
72355009Sshin		if (iflr->flags & IFLR_PREFIX)
724184295Sbz			return (EINVAL);
72555009Sshin
72655009Sshin		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
72755009Sshin		bzero(&ifra, sizeof(ifra));
72855009Sshin		bcopy(iflr->iflr_name, ifra.ifra_name,
72955009Sshin			sizeof(ifra.ifra_name));
73055009Sshin
73155917Sshin		bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
73255009Sshin
73355917Sshin		if (iflr->dstaddr.ss_family) {	/*XXX*/
73455009Sshin			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
73555917Sshin				iflr->dstaddr.ss_len);
73655009Sshin		}
73755009Sshin
73855009Sshin		ifra.ifra_mask.sin_family = AF_INET;
73955009Sshin		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
74055009Sshin		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
74155009Sshin
742184295Sbz		return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td));
74355009Sshin	    }
74455009Sshin	case SIOCGLIFADDR:
74555009Sshin	case SIOCDLIFADDR:
74655009Sshin	    {
74755009Sshin		struct in_ifaddr *ia;
74855009Sshin		struct in_addr mask, candidate, match;
74955009Sshin		struct sockaddr_in *sin;
75055009Sshin
75155009Sshin		bzero(&mask, sizeof(mask));
752170855Smjacob		bzero(&match, sizeof(match));
75355009Sshin		if (iflr->flags & IFLR_PREFIX) {
75455009Sshin			/* lookup a prefix rather than address. */
75555009Sshin			in_len2mask(&mask, iflr->prefixlen);
75655009Sshin
75755009Sshin			sin = (struct sockaddr_in *)&iflr->addr;
75855009Sshin			match.s_addr = sin->sin_addr.s_addr;
75955009Sshin			match.s_addr &= mask.s_addr;
76055009Sshin
76155009Sshin			/* if you set extra bits, that's wrong */
76255009Sshin			if (match.s_addr != sin->sin_addr.s_addr)
763184295Sbz				return (EINVAL);
76455009Sshin
76555009Sshin		} else {
766170855Smjacob			/* on getting an address, take the 1st match */
767170855Smjacob			/* on deleting an address, do exact match */
768170855Smjacob			if (cmd != SIOCGLIFADDR) {
76955009Sshin				in_len2mask(&mask, 32);
77055009Sshin				sin = (struct sockaddr_in *)&iflr->addr;
77155009Sshin				match.s_addr = sin->sin_addr.s_addr;
77255009Sshin			}
77355009Sshin		}
77455009Sshin
77555009Sshin		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)	{
77655009Sshin			if (ifa->ifa_addr->sa_family != AF_INET6)
77755009Sshin				continue;
778170855Smjacob			if (match.s_addr == 0)
77955009Sshin				break;
78055009Sshin			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
78155009Sshin			candidate.s_addr &= mask.s_addr;
78255009Sshin			if (candidate.s_addr == match.s_addr)
78355009Sshin				break;
78455009Sshin		}
785184295Sbz		if (ifa == NULL)
786184295Sbz			return (EADDRNOTAVAIL);
78755009Sshin		ia = (struct in_ifaddr *)ifa;
78855009Sshin
78955009Sshin		if (cmd == SIOCGLIFADDR) {
79055009Sshin			/* fill in the if_laddrreq structure */
79155009Sshin			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
79255009Sshin
79355009Sshin			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
79455009Sshin				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
79555009Sshin					ia->ia_dstaddr.sin_len);
79655009Sshin			} else
79755009Sshin				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
79855009Sshin
79955009Sshin			iflr->prefixlen =
80055009Sshin				in_mask2len(&ia->ia_sockmask.sin_addr);
80155009Sshin
80255009Sshin			iflr->flags = 0;	/*XXX*/
80355009Sshin
804184295Sbz			return (0);
80555009Sshin		} else {
80655009Sshin			struct in_aliasreq ifra;
80755009Sshin
80855009Sshin			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
80955009Sshin			bzero(&ifra, sizeof(ifra));
81055009Sshin			bcopy(iflr->iflr_name, ifra.ifra_name,
81155009Sshin				sizeof(ifra.ifra_name));
81255009Sshin
81355009Sshin			bcopy(&ia->ia_addr, &ifra.ifra_addr,
81455009Sshin				ia->ia_addr.sin_len);
81555009Sshin			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
81655009Sshin				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
81755009Sshin					ia->ia_dstaddr.sin_len);
81855009Sshin			}
81955009Sshin			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
82055009Sshin				ia->ia_sockmask.sin_len);
82155009Sshin
822184295Sbz			return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
823184295Sbz			    ifp, td));
82455009Sshin		}
82555009Sshin	    }
82655009Sshin	}
82755009Sshin
828184295Sbz	return (EOPNOTSUPP);	/*just for safety*/
82955009Sshin}
83055009Sshin
83155009Sshin/*
8321541Srgrimes * Delete any existing route for an interface.
8331541Srgrimes */
83422672Swollmanvoid
835222143Sqingliin_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia, u_int flags)
8361541Srgrimes{
837169454Srwatson
838222143Sqingli	in_scrubprefix(ia, flags);
8391541Srgrimes}
8401541Srgrimes
8411541Srgrimes/*
8421541Srgrimes * Initialize an interface's internet address
8431541Srgrimes * and routing table entry.
8441541Srgrimes */
84512296Sphkstatic int
846169454Srwatsonin_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
847169454Srwatson    int scrub)
8481541Srgrimes{
8491541Srgrimes	register u_long i = ntohl(sin->sin_addr.s_addr);
850226339Sglebius	int flags = RTF_UP, error = 0;
8511541Srgrimes
852227791Sglebius	if (scrub)
853227791Sglebius		in_scrubprefix(ia, LLE_STATIC);
854227791Sglebius
855227791Sglebius	IN_IFADDR_WLOCK();
856227791Sglebius	if (ia->ia_addr.sin_family == AF_INET)
857105748Ssuz		LIST_REMOVE(ia, ia_hash);
8581541Srgrimes	ia->ia_addr = *sin;
859227791Sglebius	LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
860227791Sglebius	    ia, ia_hash);
861227791Sglebius	IN_IFADDR_WUNLOCK();
862227791Sglebius
8631541Srgrimes	/*
8641541Srgrimes	 * Give the interface a chance to initialize
8651541Srgrimes	 * if this is its first address,
8661541Srgrimes	 * and to validate the address if necessary.
8671541Srgrimes	 */
868227791Sglebius	if (ifp->if_ioctl != NULL &&
869227791Sglebius	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)) != 0)
870146883Siedowse			/* LIST_REMOVE(ia, ia_hash) is done in in_control */
871146883Siedowse			return (error);
872227791Sglebius
8731541Srgrimes	/*
874226401Sglebius	 * Be compatible with network classes, if netmask isn't supplied,
875226401Sglebius	 * guess it based on classes.
8761541Srgrimes	 */
8771541Srgrimes	if (ia->ia_subnetmask == 0) {
878226401Sglebius		if (IN_CLASSA(i))
879226401Sglebius			ia->ia_subnetmask = IN_CLASSA_NET;
880226401Sglebius		else if (IN_CLASSB(i))
881226401Sglebius			ia->ia_subnetmask = IN_CLASSB_NET;
882226401Sglebius		else
883226401Sglebius			ia->ia_subnetmask = IN_CLASSC_NET;
8841541Srgrimes		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
885226401Sglebius	}
8861541Srgrimes	ia->ia_subnet = i & ia->ia_subnetmask;
8871541Srgrimes	in_socktrim(&ia->ia_sockmask);
8881541Srgrimes	/*
889143868Sglebius	 * XXX: carp(4) does not have interface route
890143868Sglebius	 */
891143868Sglebius	if (ifp->if_type == IFT_CARP)
892143868Sglebius		return (0);
893143868Sglebius	/*
8941541Srgrimes	 * Add route for the network.
8951541Srgrimes	 */
8961541Srgrimes	ia->ia_ifa.ifa_metric = ifp->if_metric;
8971541Srgrimes	if (ifp->if_flags & IFF_BROADCAST) {
898226402Sglebius		if (ia->ia_subnetmask == IN_RFC3021_MASK)
899226402Sglebius			ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
900226402Sglebius		else
901226402Sglebius			ia->ia_broadaddr.sin_addr.s_addr =
902226402Sglebius			    htonl(ia->ia_subnet | ~ia->ia_subnetmask);
9031541Srgrimes	} else if (ifp->if_flags & IFF_LOOPBACK) {
904137833Smlaier		ia->ia_dstaddr = ia->ia_addr;
9051541Srgrimes		flags |= RTF_HOST;
9061541Srgrimes	} else if (ifp->if_flags & IFF_POINTOPOINT) {
9071541Srgrimes		if (ia->ia_dstaddr.sin_family != AF_INET)
9081541Srgrimes			return (0);
9091541Srgrimes		flags |= RTF_HOST;
9101541Srgrimes	}
911137628Smlaier	if ((error = in_addprefix(ia, flags)) != 0)
912137628Smlaier		return (error);
91394326Sbrian
914192085Sqingli	if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY)
915192085Sqingli		return (0);
916192085Sqingli
917227791Sglebius	if (ifp->if_flags & IFF_POINTOPOINT &&
918227791Sglebius	    ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
919203401Sqingli			return (0);
920203401Sqingli
921192011Sqingli	/*
922192011Sqingli	 * add a loopback route to self
923192011Sqingli	 */
924201282Sqingli	if (V_useloopback && !(ifp->if_flags & IFF_LOOPBACK)) {
925201282Sqingli		struct route ia_ro;
926201282Sqingli
927201282Sqingli		bzero(&ia_ro, sizeof(ia_ro));
928201282Sqingli		*((struct sockaddr_in *)(&ia_ro.ro_dst)) = ia->ia_addr;
929201282Sqingli		rtalloc_ign_fib(&ia_ro, 0, 0);
930201282Sqingli		if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
931201282Sqingli		    (ia_ro.ro_rt->rt_ifp == V_loif)) {
932201282Sqingli			RT_LOCK(ia_ro.ro_rt);
933201282Sqingli			RT_ADDREF(ia_ro.ro_rt);
934201282Sqingli			RTFREE_LOCKED(ia_ro.ro_rt);
935201282Sqingli		} else
936201282Sqingli			error = ifa_add_loopback_route((struct ifaddr *)ia,
937197227Sqingli				       (struct sockaddr *)&ia->ia_addr);
938201282Sqingli		if (error == 0)
939201282Sqingli			ia->ia_flags |= IFA_RTSELF;
940201282Sqingli		if (ia_ro.ro_rt != NULL)
941201282Sqingli			RTFREE(ia_ro.ro_rt);
942201282Sqingli	}
943192011Sqingli
9441541Srgrimes	return (error);
9451541Srgrimes}
9461541Srgrimes
947137628Smlaier#define rtinitflags(x) \
948137628Smlaier	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
949137628Smlaier	    ? RTF_HOST : 0)
950201285Sqingli
951137628Smlaier/*
952201285Sqingli * Generate a routing message when inserting or deleting
953201285Sqingli * an interface address alias.
954201285Sqingli */
955201285Sqinglistatic void in_addralias_rtmsg(int cmd, struct in_addr *prefix,
956201285Sqingli    struct in_ifaddr *target)
957201285Sqingli{
958201285Sqingli	struct route pfx_ro;
959201285Sqingli	struct sockaddr_in *pfx_addr;
960201285Sqingli	struct rtentry msg_rt;
961201285Sqingli
962201285Sqingli	/* QL: XXX
963201285Sqingli	 * This is a bit questionable because there is no
964201285Sqingli	 * additional route entry added/deleted for an address
965201285Sqingli	 * alias. Therefore this route report is inaccurate.
966201285Sqingli	 */
967201285Sqingli	bzero(&pfx_ro, sizeof(pfx_ro));
968201285Sqingli	pfx_addr = (struct sockaddr_in *)(&pfx_ro.ro_dst);
969201285Sqingli	pfx_addr->sin_len = sizeof(*pfx_addr);
970201285Sqingli	pfx_addr->sin_family = AF_INET;
971201285Sqingli	pfx_addr->sin_addr = *prefix;
972201285Sqingli	rtalloc_ign_fib(&pfx_ro, 0, 0);
973201285Sqingli	if (pfx_ro.ro_rt != NULL) {
974201285Sqingli		msg_rt = *pfx_ro.ro_rt;
975201285Sqingli
976201285Sqingli		/* QL: XXX
977201285Sqingli		 * Point the gateway to the new interface
978201285Sqingli		 * address as if a new prefix route entry has
979201285Sqingli		 * been added through the new address alias.
980201285Sqingli		 * All other parts of the rtentry is accurate,
981201285Sqingli		 * e.g., rt_key, rt_mask, rt_ifp etc.
982201285Sqingli		 */
983201285Sqingli		msg_rt.rt_gateway =
984201285Sqingli			(struct sockaddr *)&target->ia_addr;
985201285Sqingli		rt_newaddrmsg(cmd,
986201285Sqingli			      (struct ifaddr *)target,
987201285Sqingli			      0, &msg_rt);
988201285Sqingli		RTFREE(pfx_ro.ro_rt);
989201285Sqingli	}
990201285Sqingli	return;
991201285Sqingli}
992201285Sqingli
993201285Sqingli/*
994170855Smjacob * Check if we have a route for the given prefix already or add one accordingly.
995137628Smlaier */
996137628Smlaierstatic int
997169454Srwatsonin_addprefix(struct in_ifaddr *target, int flags)
998137628Smlaier{
999137628Smlaier	struct in_ifaddr *ia;
1000151555Sglebius	struct in_addr prefix, mask, p, m;
1001137628Smlaier	int error;
10021541Srgrimes
1003170855Smjacob	if ((flags & RTF_HOST) != 0) {
1004137628Smlaier		prefix = target->ia_dstaddr.sin_addr;
1005170855Smjacob		mask.s_addr = 0;
1006170855Smjacob	} else {
1007137628Smlaier		prefix = target->ia_addr.sin_addr;
1008137628Smlaier		mask = target->ia_sockmask.sin_addr;
1009137628Smlaier		prefix.s_addr &= mask.s_addr;
1010137628Smlaier	}
1011137628Smlaier
1012194951Srwatson	IN_IFADDR_RLOCK();
1013181803Sbz	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1014151555Sglebius		if (rtinitflags(ia)) {
1015224747Skevlo			p = ia->ia_dstaddr.sin_addr;
1016151555Sglebius
1017151555Sglebius			if (prefix.s_addr != p.s_addr)
1018151555Sglebius				continue;
1019151555Sglebius		} else {
1020151555Sglebius			p = ia->ia_addr.sin_addr;
1021151555Sglebius			m = ia->ia_sockmask.sin_addr;
1022151555Sglebius			p.s_addr &= m.s_addr;
1023151555Sglebius
1024151555Sglebius			if (prefix.s_addr != p.s_addr ||
1025151555Sglebius			    mask.s_addr != m.s_addr)
1026151555Sglebius				continue;
1027137628Smlaier		}
1028137628Smlaier
1029137628Smlaier		/*
1030137628Smlaier		 * If we got a matching prefix route inserted by other
1031137628Smlaier		 * interface address, we are done here.
1032137628Smlaier		 */
1033149221Sglebius		if (ia->ia_flags & IFA_ROUTE) {
1034204902Sqingli#ifdef RADIX_MPATH
1035204902Sqingli			if (ia->ia_addr.sin_addr.s_addr ==
1036212209Sbz			    target->ia_addr.sin_addr.s_addr) {
1037212209Sbz				IN_IFADDR_RUNLOCK();
1038204902Sqingli				return (EEXIST);
1039212209Sbz			} else
1040204902Sqingli				break;
1041204902Sqingli#endif
1042181803Sbz			if (V_sameprefixcarponly &&
1043149221Sglebius			    target->ia_ifp->if_type != IFT_CARP &&
1044194951Srwatson			    ia->ia_ifp->if_type != IFT_CARP) {
1045194951Srwatson				IN_IFADDR_RUNLOCK();
1046149221Sglebius				return (EEXIST);
1047194951Srwatson			} else {
1048201285Sqingli				in_addralias_rtmsg(RTM_ADD, &prefix, target);
1049194951Srwatson				IN_IFADDR_RUNLOCK();
1050149221Sglebius				return (0);
1051194951Srwatson			}
1052149221Sglebius		}
1053137628Smlaier	}
1054194951Srwatson	IN_IFADDR_RUNLOCK();
1055137628Smlaier
1056137628Smlaier	/*
1057137628Smlaier	 * No-one seem to have this prefix route, so we try to insert it.
1058137628Smlaier	 */
1059137628Smlaier	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
1060137628Smlaier	if (!error)
1061137628Smlaier		target->ia_flags |= IFA_ROUTE;
1062184295Sbz	return (error);
1063137628Smlaier}
1064137628Smlaier
1065186119Sqingliextern void arp_ifscrub(struct ifnet *ifp, uint32_t addr);
1066186119Sqingli
10671541Srgrimes/*
1068137628Smlaier * If there is no other address in the system that can serve a route to the
1069137628Smlaier * same prefix, remove the route.  Hand over the route to the new address
1070137628Smlaier * otherwise.
1071137628Smlaier */
1072137628Smlaierstatic int
1073222143Sqingliin_scrubprefix(struct in_ifaddr *target, u_int flags)
1074137628Smlaier{
1075137628Smlaier	struct in_ifaddr *ia;
1076137628Smlaier	struct in_addr prefix, mask, p;
1077201282Sqingli	int error = 0;
1078192476Sqingli	struct sockaddr_in prefix0, mask0;
1079137628Smlaier
1080195914Sqingli	/*
1081195914Sqingli	 * Remove the loopback route to the interface address.
1082195914Sqingli	 * The "useloopback" setting is not consulted because if the
1083195914Sqingli	 * user configures an interface address, turns off this
1084195914Sqingli	 * setting, and then tries to delete that interface address,
1085195914Sqingli	 * checking the current setting of "useloopback" would leave
1086195914Sqingli	 * that interface address loopback route untouched, which
1087195914Sqingli	 * would be wrong. Therefore the interface address loopback route
1088195914Sqingli	 * deletion is unconditional.
1089195914Sqingli	 */
1090192085Sqingli	if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
1091201282Sqingli	    !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
1092201282Sqingli	    (target->ia_flags & IFA_RTSELF)) {
1093201282Sqingli		struct route ia_ro;
1094201282Sqingli		int freeit = 0;
1095201282Sqingli
1096201282Sqingli		bzero(&ia_ro, sizeof(ia_ro));
1097201282Sqingli		*((struct sockaddr_in *)(&ia_ro.ro_dst)) = target->ia_addr;
1098201282Sqingli		rtalloc_ign_fib(&ia_ro, 0, 0);
1099201282Sqingli		if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
1100201282Sqingli		    (ia_ro.ro_rt->rt_ifp == V_loif)) {
1101201282Sqingli			RT_LOCK(ia_ro.ro_rt);
1102201282Sqingli			if (ia_ro.ro_rt->rt_refcnt <= 1)
1103201282Sqingli				freeit = 1;
1104226114Sqingli			else if (flags & LLE_STATIC) {
1105201282Sqingli				RT_REMREF(ia_ro.ro_rt);
1106226114Sqingli				target->ia_flags &= ~IFA_RTSELF;
1107226114Sqingli			}
1108201282Sqingli			RTFREE_LOCKED(ia_ro.ro_rt);
1109201282Sqingli		}
1110222143Sqingli		if (freeit && (flags & LLE_STATIC)) {
1111201282Sqingli			error = ifa_del_loopback_route((struct ifaddr *)target,
1112197227Sqingli				       (struct sockaddr *)&target->ia_addr);
1113222143Sqingli			if (error == 0)
1114222143Sqingli				target->ia_flags &= ~IFA_RTSELF;
1115222143Sqingli		}
1116226120Sqingli		if ((flags & LLE_STATIC) &&
1117226120Sqingli			!(target->ia_ifp->if_flags & IFF_NOARP))
1118222143Sqingli			/* remove arp cache */
1119222143Sqingli			arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr);
1120192011Sqingli	}
1121192011Sqingli
1122137628Smlaier	if (rtinitflags(target))
1123137628Smlaier		prefix = target->ia_dstaddr.sin_addr;
1124137628Smlaier	else {
1125137628Smlaier		prefix = target->ia_addr.sin_addr;
1126137628Smlaier		mask = target->ia_sockmask.sin_addr;
1127137628Smlaier		prefix.s_addr &= mask.s_addr;
1128137628Smlaier	}
1129137628Smlaier
1130201285Sqingli	if ((target->ia_flags & IFA_ROUTE) == 0) {
1131201285Sqingli		in_addralias_rtmsg(RTM_DELETE, &prefix, target);
1132201285Sqingli		return (0);
1133201285Sqingli	}
1134201285Sqingli
1135194951Srwatson	IN_IFADDR_RLOCK();
1136181803Sbz	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1137137628Smlaier		if (rtinitflags(ia))
1138137628Smlaier			p = ia->ia_dstaddr.sin_addr;
1139137628Smlaier		else {
1140137628Smlaier			p = ia->ia_addr.sin_addr;
1141137628Smlaier			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1142137628Smlaier		}
1143137628Smlaier
1144225223Sqingli		if ((prefix.s_addr != p.s_addr) ||
1145225223Sqingli		    !(ia->ia_ifp->if_flags & IFF_UP))
1146137628Smlaier			continue;
1147137628Smlaier
1148137628Smlaier		/*
1149137628Smlaier		 * If we got a matching prefix address, move IFA_ROUTE and
1150137628Smlaier		 * the route itself to it.  Make sure that routing daemons
1151137628Smlaier		 * get a heads-up.
1152143868Sglebius		 *
1153211157Swill		 * XXX: a special case for carp(4) interface - this should
1154211157Swill		 *      be more generally specified as an interface that
1155211157Swill		 *      doesn't support such action.
1156137628Smlaier		 */
1157143868Sglebius		if ((ia->ia_flags & IFA_ROUTE) == 0
1158219828Spluknet		    && (ia->ia_ifp->if_type != IFT_CARP)) {
1159219828Spluknet			ifa_ref(&ia->ia_ifa);
1160194951Srwatson			IN_IFADDR_RUNLOCK();
1161222438Sqingli			error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
1162137628Smlaier			    rtinitflags(target));
1163222438Sqingli			if (error == 0)
1164222438Sqingli				target->ia_flags &= ~IFA_ROUTE;
1165222438Sqingli			else
1166222438Sqingli				log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
1167222438Sqingli					error);
1168137628Smlaier			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
1169137628Smlaier			    rtinitflags(ia) | RTF_UP);
1170137628Smlaier			if (error == 0)
1171137628Smlaier				ia->ia_flags |= IFA_ROUTE;
1172222438Sqingli			else
1173222438Sqingli				log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
1174222438Sqingli					error);
1175219828Spluknet			ifa_free(&ia->ia_ifa);
1176184295Sbz			return (error);
1177137628Smlaier		}
1178137628Smlaier	}
1179194951Srwatson	IN_IFADDR_RUNLOCK();
1180137628Smlaier
1181137628Smlaier	/*
1182192476Sqingli	 * remove all L2 entries on the given prefix
1183192476Sqingli	 */
1184192476Sqingli	bzero(&prefix0, sizeof(prefix0));
1185192476Sqingli	prefix0.sin_len = sizeof(prefix0);
1186192476Sqingli	prefix0.sin_family = AF_INET;
1187192476Sqingli	prefix0.sin_addr.s_addr = target->ia_subnet;
1188192476Sqingli	bzero(&mask0, sizeof(mask0));
1189192476Sqingli	mask0.sin_len = sizeof(mask0);
1190192476Sqingli	mask0.sin_family = AF_INET;
1191192476Sqingli	mask0.sin_addr.s_addr = target->ia_subnetmask;
1192192476Sqingli	lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0,
1193222143Sqingli			    (struct sockaddr *)&mask0, flags);
1194192476Sqingli
1195192476Sqingli	/*
1196137628Smlaier	 * As no-one seem to have this prefix, we can remove the route.
1197137628Smlaier	 */
1198222438Sqingli	error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
1199222438Sqingli	if (error == 0)
1200222438Sqingli		target->ia_flags &= ~IFA_ROUTE;
1201222438Sqingli	else
1202222438Sqingli		log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
1203222438Sqingli	return (error);
1204137628Smlaier}
1205137628Smlaier
1206137628Smlaier#undef rtinitflags
1207137628Smlaier
1208137628Smlaier/*
12091541Srgrimes * Return 1 if the address might be a local broadcast address.
12101541Srgrimes */
12111549Srgrimesint
1212169454Srwatsonin_broadcast(struct in_addr in, struct ifnet *ifp)
12131541Srgrimes{
12141541Srgrimes	register struct ifaddr *ifa;
12151541Srgrimes	u_long t;
12161541Srgrimes
12171541Srgrimes	if (in.s_addr == INADDR_BROADCAST ||
12181541Srgrimes	    in.s_addr == INADDR_ANY)
1219184295Sbz		return (1);
12201541Srgrimes	if ((ifp->if_flags & IFF_BROADCAST) == 0)
1221184295Sbz		return (0);
12221541Srgrimes	t = ntohl(in.s_addr);
12231541Srgrimes	/*
12241541Srgrimes	 * Look through the list of addresses for a match
12251541Srgrimes	 * with a broadcast address.
12261541Srgrimes	 */
12271541Srgrimes#define ia ((struct in_ifaddr *)ifa)
122874362Sphk	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
12291541Srgrimes		if (ifa->ifa_addr->sa_family == AF_INET &&
12301541Srgrimes		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
12311541Srgrimes		     /*
1232226402Sglebius		      * Check for old-style (host 0) broadcast, but
1233226402Sglebius		      * taking into account that RFC 3021 obsoletes it.
12341541Srgrimes		      */
1235226402Sglebius		     (ia->ia_subnetmask != IN_RFC3021_MASK &&
1236226402Sglebius		     t == ia->ia_subnet)) &&
123713351Sguido		     /*
123813351Sguido		      * Check for an all one subnetmask. These
123913351Sguido		      * only exist when an interface gets a secondary
124013351Sguido		      * address.
124113351Sguido		      */
124213351Sguido		     ia->ia_subnetmask != (u_long)0xffffffff)
1243184295Sbz			    return (1);
12441541Srgrimes	return (0);
12451541Srgrimes#undef ia
12461541Srgrimes}
1247167729Sbms
12481541Srgrimes/*
1249189592Sbms * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1250189592Sbms */
1251189592Sbmsvoid
1252189592Sbmsin_ifdetach(struct ifnet *ifp)
1253189592Sbms{
1254189592Sbms
1255189592Sbms	in_pcbpurgeif0(&V_ripcbinfo, ifp);
1256189592Sbms	in_pcbpurgeif0(&V_udbinfo, ifp);
1257189592Sbms	in_purgemaddrs(ifp);
1258189592Sbms}
1259189592Sbms
1260189592Sbms/*
1261167729Sbms * Delete all IPv4 multicast address records, and associated link-layer
1262167729Sbms * multicast address records, associated with ifp.
1263189592Sbms * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1264189931Sbms * XXX This should not race with ifma_protospec being set during
1265189931Sbms * a new allocation, if it does, we have bigger problems.
1266162718Sbms */
1267167729Sbmsstatic void
1268167729Sbmsin_purgemaddrs(struct ifnet *ifp)
1269162718Sbms{
1270189592Sbms	LIST_HEAD(,in_multi) purgeinms;
1271189592Sbms	struct in_multi		*inm, *tinm;
1272189592Sbms	struct ifmultiaddr	*ifma;
1273162718Sbms
1274189592Sbms	LIST_INIT(&purgeinms);
1275162718Sbms	IN_MULTI_LOCK();
1276189592Sbms
1277189592Sbms	/*
1278189592Sbms	 * Extract list of in_multi associated with the detaching ifp
1279189592Sbms	 * which the PF_INET layer is about to release.
1280189592Sbms	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1281189592Sbms	 * by code further down.
1282189592Sbms	 */
1283189592Sbms	IF_ADDR_LOCK(ifp);
1284189592Sbms	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1285189931Sbms		if (ifma->ifma_addr->sa_family != AF_INET ||
1286189931Sbms		    ifma->ifma_protospec == NULL)
1287189592Sbms			continue;
1288189931Sbms#if 0
1289189931Sbms		KASSERT(ifma->ifma_protospec != NULL,
1290189931Sbms		    ("%s: ifma_protospec is NULL", __func__));
1291189931Sbms#endif
1292189592Sbms		inm = (struct in_multi *)ifma->ifma_protospec;
1293189592Sbms		LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
1294162718Sbms	}
1295189592Sbms	IF_ADDR_UNLOCK(ifp);
1296150296Srwatson
1297189592Sbms	LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
1298191476Srwatson		LIST_REMOVE(inm, inm_link);
1299189592Sbms		inm_release_locked(inm);
1300189592Sbms	}
1301189592Sbms	igmp_ifdetach(ifp);
1302150296Srwatson
1303189592Sbms	IN_MULTI_UNLOCK();
1304150296Srwatson}
1305186119Sqingli
1306186119Sqingli#include <net/if_dl.h>
1307186119Sqingli#include <netinet/if_ether.h>
1308186119Sqingli
1309186119Sqinglistruct in_llentry {
1310186119Sqingli	struct llentry		base;
1311186119Sqingli	struct sockaddr_in	l3_addr4;
1312186119Sqingli};
1313186119Sqingli
1314186119Sqinglistatic struct llentry *
1315186119Sqingliin_lltable_new(const struct sockaddr *l3addr, u_int flags)
1316186119Sqingli{
1317186119Sqingli	struct in_llentry *lle;
1318186119Sqingli
1319186119Sqingli	lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO);
1320186119Sqingli	if (lle == NULL)		/* NB: caller generates msg */
1321186119Sqingli		return NULL;
1322186119Sqingli
1323186119Sqingli	callout_init(&lle->base.la_timer, CALLOUT_MPSAFE);
1324186119Sqingli	/*
1325186119Sqingli	 * For IPv4 this will trigger "arpresolve" to generate
1326186119Sqingli	 * an ARP request.
1327186119Sqingli	 */
1328216075Sglebius	lle->base.la_expire = time_uptime; /* mark expired */
1329186119Sqingli	lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
1330186119Sqingli	lle->base.lle_refcnt = 1;
1331186119Sqingli	LLE_LOCK_INIT(&lle->base);
1332186119Sqingli	return &lle->base;
1333186119Sqingli}
1334186119Sqingli
1335186119Sqingli/*
1336186119Sqingli * Deletes an address from the address table.
1337186119Sqingli * This function is called by the timer functions
1338186119Sqingli * such as arptimer() and nd6_llinfo_timer(), and
1339186119Sqingli * the caller does the locking.
1340186119Sqingli */
1341186119Sqinglistatic void
1342186119Sqingliin_lltable_free(struct lltable *llt, struct llentry *lle)
1343186119Sqingli{
1344186150Skmacy	LLE_WUNLOCK(lle);
1345186150Skmacy	LLE_LOCK_DESTROY(lle);
1346186119Sqingli	free(lle, M_LLTABLE);
1347186119Sqingli}
1348186119Sqingli
1349192476Sqingli
1350192476Sqingli#define IN_ARE_MASKED_ADDR_EQUAL(d, a, m)	(			\
1351192476Sqingli	    (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
1352192476Sqingli
1353192476Sqinglistatic void
1354192476Sqingliin_lltable_prefix_free(struct lltable *llt,
1355192476Sqingli		       const struct sockaddr *prefix,
1356222143Sqingli		       const struct sockaddr *mask,
1357222143Sqingli		       u_int flags)
1358192476Sqingli{
1359192476Sqingli	const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
1360192476Sqingli	const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
1361192476Sqingli	struct llentry *lle, *next;
1362192476Sqingli	register int i;
1363215207Sgnn	size_t pkts_dropped;
1364192476Sqingli
1365192476Sqingli	for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
1366192476Sqingli		LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
1367192476Sqingli
1368222143Sqingli		        /*
1369222143Sqingli			 * (flags & LLE_STATIC) means deleting all entries
1370222143Sqingli			 * including static ARP entries
1371222143Sqingli			 */
1372192476Sqingli			if (IN_ARE_MASKED_ADDR_EQUAL((struct sockaddr_in *)L3_ADDR(lle),
1373222143Sqingli						     pfx, msk) &&
1374222143Sqingli			    ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))) {
1375206481Sbz				int canceled;
1376206481Sbz
1377206481Sbz				canceled = callout_drain(&lle->la_timer);
1378192476Sqingli				LLE_WLOCK(lle);
1379206481Sbz				if (canceled)
1380206481Sbz					LLE_REMREF(lle);
1381215207Sgnn				pkts_dropped = llentry_free(lle);
1382215207Sgnn				ARPSTAT_ADD(dropped, pkts_dropped);
1383192476Sqingli			}
1384192476Sqingli		}
1385192476Sqingli	}
1386192476Sqingli}
1387192476Sqingli
1388192476Sqingli
1389186119Sqinglistatic int
1390201282Sqingliin_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1391186119Sqingli{
1392186119Sqingli	struct rtentry *rt;
1393186119Sqingli
1394186119Sqingli	KASSERT(l3addr->sa_family == AF_INET,
1395186119Sqingli	    ("sin_family %d", l3addr->sa_family));
1396186119Sqingli
1397186119Sqingli	/* XXX rtalloc1 should take a const param */
1398186119Sqingli	rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
1399223862Szec
1400225946Sqingli	if (rt == NULL)
1401225946Sqingli		return (EINVAL);
1402225946Sqingli
1403223862Szec	/*
1404223862Szec	 * If the gateway for an existing host route matches the target L3
1405225946Sqingli	 * address, which is a special route inserted by some implementation
1406225946Sqingli	 * such as MANET, and the interface is of the correct type, then
1407225946Sqingli	 * allow for ARP to proceed.
1408223862Szec	 */
1409225947Sqingli	if (rt->rt_flags & RTF_GATEWAY) {
1410226224Sqingli		if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
1411226224Sqingli			rt->rt_ifp->if_type != IFT_ETHER ||
1412226224Sqingli			  (rt->rt_ifp->if_flags &
1413226224Sqingli			   (IFF_NOARP | IFF_STATICARP)) != 0 ||
1414226224Sqingli			  memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
1415226224Sqingli				 sizeof(in_addr_t)) != 0) {
1416226224Sqingli			RTFREE_LOCKED(rt);
1417226224Sqingli			return (EINVAL);
1418226224Sqingli		}
1419225947Sqingli	}
1420225947Sqingli
1421225947Sqingli	/*
1422225947Sqingli	 * Make sure that at least the destination address is covered
1423225947Sqingli	 * by the route. This is for handling the case where 2 or more
1424225947Sqingli	 * interfaces have the same prefix. An incoming packet arrives
1425225947Sqingli	 * on one interface and the corresponding outgoing packet leaves
1426225947Sqingli	 * another interface.
1427225947Sqingli	 */
1428226713Sqingli	if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
1429226224Sqingli		const char *sa, *mask, *addr, *lim;
1430225947Sqingli		int len;
1431225947Sqingli
1432226713Sqingli		mask = (const char *)rt_mask(rt);
1433226713Sqingli		/*
1434226713Sqingli		 * Just being extra cautious to avoid some custom
1435226713Sqingli		 * code getting into trouble.
1436226713Sqingli		 */
1437226713Sqingli		if (mask == NULL) {
1438226713Sqingli			RTFREE_LOCKED(rt);
1439226713Sqingli			return (EINVAL);
1440226713Sqingli		}
1441226713Sqingli
1442226224Sqingli		sa = (const char *)rt_key(rt);
1443226224Sqingli		addr = (const char *)l3addr;
1444226224Sqingli		len = ((const struct sockaddr_in *)l3addr)->sin_len;
1445225947Sqingli		lim = addr + len;
1446225947Sqingli
1447225947Sqingli		for ( ; addr < lim; sa++, mask++, addr++) {
1448225947Sqingli			if ((*sa ^ *addr) & *mask) {
1449198418Sqingli#ifdef DIAGNOSTIC
1450225947Sqingli				log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
1451225947Sqingli				    inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1452197696Sqingli#endif
1453226224Sqingli				RTFREE_LOCKED(rt);
1454226224Sqingli				return (EINVAL);
1455225947Sqingli			}
1456225947Sqingli		}
1457186119Sqingli	}
1458225947Sqingli
1459186119Sqingli	RTFREE_LOCKED(rt);
1460226224Sqingli	return (0);
1461186119Sqingli}
1462186119Sqingli
1463186119Sqingli/*
1464186119Sqingli * Return NULL if not found or marked for deletion.
1465186119Sqingli * If found return lle read locked.
1466186119Sqingli */
1467186119Sqinglistatic struct llentry *
1468186119Sqingliin_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1469186119Sqingli{
1470186119Sqingli	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1471186119Sqingli	struct ifnet *ifp = llt->llt_ifp;
1472186119Sqingli	struct llentry *lle;
1473186119Sqingli	struct llentries *lleh;
1474186119Sqingli	u_int hashkey;
1475186119Sqingli
1476186119Sqingli	IF_AFDATA_LOCK_ASSERT(ifp);
1477186119Sqingli	KASSERT(l3addr->sa_family == AF_INET,
1478186119Sqingli	    ("sin_family %d", l3addr->sa_family));
1479186119Sqingli
1480186119Sqingli	hashkey = sin->sin_addr.s_addr;
1481186119Sqingli	lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
1482186119Sqingli	LIST_FOREACH(lle, lleh, lle_next) {
1483186708Sqingli		struct sockaddr_in *sa2 = (struct sockaddr_in *)L3_ADDR(lle);
1484186119Sqingli		if (lle->la_flags & LLE_DELETED)
1485186119Sqingli			continue;
1486186708Sqingli		if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
1487186119Sqingli			break;
1488186119Sqingli	}
1489186119Sqingli	if (lle == NULL) {
1490198418Sqingli#ifdef DIAGNOSTIC
1491186119Sqingli		if (flags & LLE_DELETE)
1492186119Sqingli			log(LOG_INFO, "interface address is missing from cache = %p  in delete\n", lle);
1493186119Sqingli#endif
1494186119Sqingli		if (!(flags & LLE_CREATE))
1495186119Sqingli			return (NULL);
1496186119Sqingli		/*
1497186119Sqingli		 * A route that covers the given address must have
1498186119Sqingli		 * been installed 1st because we are doing a resolution,
1499186119Sqingli		 * verify this.
1500186119Sqingli		 */
1501186119Sqingli		if (!(flags & LLE_IFADDR) &&
1502201282Sqingli		    in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1503186119Sqingli			goto done;
1504186119Sqingli
1505186119Sqingli		lle = in_lltable_new(l3addr, flags);
1506186119Sqingli		if (lle == NULL) {
1507186119Sqingli			log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1508186119Sqingli			goto done;
1509186119Sqingli		}
1510186119Sqingli		lle->la_flags = flags & ~LLE_CREATE;
1511186119Sqingli		if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
1512186119Sqingli			bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
1513186119Sqingli			lle->la_flags |= (LLE_VALID | LLE_STATIC);
1514186119Sqingli		}
1515186119Sqingli
1516186119Sqingli		lle->lle_tbl  = llt;
1517186119Sqingli		lle->lle_head = lleh;
1518186119Sqingli		LIST_INSERT_HEAD(lleh, lle, lle_next);
1519186119Sqingli	} else if (flags & LLE_DELETE) {
1520186119Sqingli		if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
1521186119Sqingli			LLE_WLOCK(lle);
1522186119Sqingli			lle->la_flags = LLE_DELETED;
1523196995Snp			EVENTHANDLER_INVOKE(arp_update_event, lle);
1524186119Sqingli			LLE_WUNLOCK(lle);
1525198418Sqingli#ifdef DIAGNOSTIC
1526186119Sqingli			log(LOG_INFO, "ifaddr cache = %p  is deleted\n", lle);
1527186119Sqingli#endif
1528186119Sqingli		}
1529186119Sqingli		lle = (void *)-1;
1530186119Sqingli
1531186119Sqingli	}
1532186544Sbz	if (LLE_IS_VALID(lle)) {
1533186119Sqingli		if (flags & LLE_EXCLUSIVE)
1534186119Sqingli			LLE_WLOCK(lle);
1535186119Sqingli		else
1536186119Sqingli			LLE_RLOCK(lle);
1537186119Sqingli	}
1538186119Sqinglidone:
1539186119Sqingli	return (lle);
1540186119Sqingli}
1541186119Sqingli
1542186119Sqinglistatic int
1543186119Sqingliin_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
1544186119Sqingli{
1545186119Sqingli#define	SIN(lle)	((struct sockaddr_in *) L3_ADDR(lle))
1546186119Sqingli	struct ifnet *ifp = llt->llt_ifp;
1547186119Sqingli	struct llentry *lle;
1548186119Sqingli	/* XXX stack use */
1549186119Sqingli	struct {
1550186119Sqingli		struct rt_msghdr	rtm;
1551186119Sqingli		struct sockaddr_inarp	sin;
1552186119Sqingli		struct sockaddr_dl	sdl;
1553186119Sqingli	} arpc;
1554186119Sqingli	int error, i;
1555186119Sqingli
1556196535Srwatson	LLTABLE_LOCK_ASSERT();
1557186119Sqingli
1558186119Sqingli	error = 0;
1559186119Sqingli	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1560186119Sqingli		LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
1561186119Sqingli			struct sockaddr_dl *sdl;
1562186119Sqingli
1563186119Sqingli			/* skip deleted entries */
1564198111Sqingli			if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1565186119Sqingli				continue;
1566186980Sbz			/* Skip if jailed and not a valid IP of the prison. */
1567188144Sjamie			if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1568186980Sbz				continue;
1569186119Sqingli			/*
1570186119Sqingli			 * produce a msg made of:
1571186119Sqingli			 *  struct rt_msghdr;
1572186119Sqingli			 *  struct sockaddr_inarp; (IPv4)
1573186119Sqingli			 *  struct sockaddr_dl;
1574186119Sqingli			 */
1575186119Sqingli			bzero(&arpc, sizeof(arpc));
1576186119Sqingli			arpc.rtm.rtm_msglen = sizeof(arpc);
1577186935Sharti			arpc.rtm.rtm_version = RTM_VERSION;
1578186935Sharti			arpc.rtm.rtm_type = RTM_GET;
1579186935Sharti			arpc.rtm.rtm_flags = RTF_UP;
1580186935Sharti			arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1581186119Sqingli			arpc.sin.sin_family = AF_INET;
1582186119Sqingli			arpc.sin.sin_len = sizeof(arpc.sin);
1583186119Sqingli			arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
1584186119Sqingli
1585186119Sqingli			/* publish */
1586186119Sqingli			if (lle->la_flags & LLE_PUB) {
1587186119Sqingli				arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1588186119Sqingli				/* proxy only */
1589186119Sqingli				if (lle->la_flags & LLE_PROXY)
1590186119Sqingli					arpc.sin.sin_other = SIN_PROXY;
1591186119Sqingli			}
1592186119Sqingli
1593186119Sqingli			sdl = &arpc.sdl;
1594186119Sqingli			sdl->sdl_family = AF_LINK;
1595186119Sqingli			sdl->sdl_len = sizeof(*sdl);
1596186119Sqingli			sdl->sdl_index = ifp->if_index;
1597186119Sqingli			sdl->sdl_type = ifp->if_type;
1598198111Sqingli			if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1599198111Sqingli				sdl->sdl_alen = ifp->if_addrlen;
1600198111Sqingli				bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1601198111Sqingli			} else {
1602198111Sqingli				sdl->sdl_alen = 0;
1603198111Sqingli				bzero(LLADDR(sdl), ifp->if_addrlen);
1604198111Sqingli			}
1605186119Sqingli
1606186119Sqingli			arpc.rtm.rtm_rmx.rmx_expire =
1607186119Sqingli			    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1608186500Sqingli			arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1609186119Sqingli			if (lle->la_flags & LLE_STATIC)
1610186119Sqingli				arpc.rtm.rtm_flags |= RTF_STATIC;
1611186119Sqingli			arpc.rtm.rtm_index = ifp->if_index;
1612186119Sqingli			error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1613186119Sqingli			if (error)
1614186119Sqingli				break;
1615186119Sqingli		}
1616186119Sqingli	}
1617186119Sqingli	return error;
1618186119Sqingli#undef SIN
1619186119Sqingli}
1620186119Sqingli
1621186119Sqinglivoid *
1622186119Sqingliin_domifattach(struct ifnet *ifp)
1623189592Sbms{
1624189592Sbms	struct in_ifinfo *ii;
1625189592Sbms	struct lltable *llt;
1626189592Sbms
1627189592Sbms	ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1628189592Sbms
1629189592Sbms	llt = lltable_init(ifp, AF_INET);
1630186119Sqingli	if (llt != NULL) {
1631186119Sqingli		llt->llt_free = in_lltable_free;
1632192476Sqingli		llt->llt_prefix_free = in_lltable_prefix_free;
1633186119Sqingli		llt->llt_lookup = in_lltable_lookup;
1634186119Sqingli		llt->llt_dump = in_lltable_dump;
1635186119Sqingli	}
1636189592Sbms	ii->ii_llt = llt;
1637189592Sbms
1638189592Sbms	ii->ii_igmp = igmp_domifattach(ifp);
1639189592Sbms
1640189592Sbms	return ii;
1641186119Sqingli}
1642186119Sqingli
1643186119Sqinglivoid
1644189592Sbmsin_domifdetach(struct ifnet *ifp, void *aux)
1645186119Sqingli{
1646189592Sbms	struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1647186119Sqingli
1648189592Sbms	igmp_domifdetach(ifp);
1649189592Sbms	lltable_free(ii->ii_llt);
1650189592Sbms	free(ii, M_IFADDR);
1651186119Sqingli}
1652