1178476Sjb/*	$NetBSD$	*/
2178476Sjb
3178476Sjb/*
4178476Sjb * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5178476Sjb * All rights reserved.
6178476Sjb *
7178476Sjb * Redistribution and use in source and binary forms, with or without
8178476Sjb * modification, are permitted provided that the following conditions
9178476Sjb * are met:
10178476Sjb * 1. Redistributions of source code must retain the above copyright
11178476Sjb *    notice, this list of conditions and the following disclaimer.
12178476Sjb * 2. Redistributions in binary form must reproduce the above copyright
13178476Sjb *    notice, this list of conditions and the following disclaimer in the
14178476Sjb *    documentation and/or other materials provided with the distribution.
15178476Sjb * 3. Neither the name of the project nor the names of its contributors
16178476Sjb *    may be used to endorse or promote products derived from this software
17178476Sjb *    without specific prior written permission.
18178476Sjb *
19178476Sjb * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20178476Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21178476Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22178476Sjb * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23178476Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24178476Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25178476Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26178476Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27178476Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28178476Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29178476Sjb * SUCH DAMAGE.
30178476Sjb */
31178476Sjb
32178476Sjb/*-
33178476Sjb * Copyright (c) 1998 The NetBSD Foundation, Inc.
34178476Sjb * All rights reserved.
35178476Sjb *
36178476Sjb * This code is derived from software contributed to The NetBSD Foundation
37178476Sjb * by Public Access Networks Corporation ("Panix").  It was developed under
38178476Sjb * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39178476Sjb *
40178476Sjb * Redistribution and use in source and binary forms, with or without
41178476Sjb * modification, are permitted provided that the following conditions
42178476Sjb * are met:
43178476Sjb * 1. Redistributions of source code must retain the above copyright
44178476Sjb *    notice, this list of conditions and the following disclaimer.
45178476Sjb * 2. Redistributions in binary form must reproduce the above copyright
46178476Sjb *    notice, this list of conditions and the following disclaimer in the
47178476Sjb *    documentation and/or other materials provided with the distribution.
48178476Sjb *
49178476Sjb * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50178476Sjb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51178476Sjb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52178476Sjb * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53178476Sjb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54178476Sjb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55178476Sjb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56178476Sjb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57178476Sjb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58178476Sjb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59178476Sjb * POSSIBILITY OF SUCH DAMAGE.
60178476Sjb */
61178476Sjb
62178476Sjb/*
63178476Sjb * Copyright (c) 1982, 1986, 1991, 1993
64178476Sjb *	The Regents of the University of California.  All rights reserved.
65178476Sjb *
66178476Sjb * Redistribution and use in source and binary forms, with or without
67178476Sjb * modification, are permitted provided that the following conditions
68178476Sjb * are met:
69178476Sjb * 1. Redistributions of source code must retain the above copyright
70178476Sjb *    notice, this list of conditions and the following disclaimer.
71178476Sjb * 2. Redistributions in binary form must reproduce the above copyright
72178476Sjb *    notice, this list of conditions and the following disclaimer in the
73178476Sjb *    documentation and/or other materials provided with the distribution.
74178476Sjb * 3. Neither the name of the University nor the names of its contributors
75178476Sjb *    may be used to endorse or promote products derived from this software
76178476Sjb *    without specific prior written permission.
77178476Sjb *
78178476Sjb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88 * SUCH DAMAGE.
89 *
90 *	@(#)in.c	8.4 (Berkeley) 1/9/95
91 */
92
93#include <sys/cdefs.h>
94__KERNEL_RCSID(0, "$NetBSD$");
95
96#include "opt_inet.h"
97#include "opt_inet_conf.h"
98#include "opt_mrouting.h"
99#include "opt_pfil_hooks.h"
100
101#include <sys/param.h>
102#include <sys/ioctl.h>
103#include <sys/errno.h>
104#include <sys/malloc.h>
105#include <sys/socket.h>
106#include <sys/socketvar.h>
107#include <sys/sysctl.h>
108#include <sys/systm.h>
109#include <sys/proc.h>
110#include <sys/syslog.h>
111#include <sys/kauth.h>
112
113#include <sys/cprng.h>
114
115#include <net/if.h>
116#include <net/route.h>
117
118#include <net/if_ether.h>
119
120#include <netinet/in_systm.h>
121#include <netinet/in.h>
122#include <netinet/in_var.h>
123#include <netinet/ip.h>
124#include <netinet/ip_var.h>
125#include <netinet/in_ifattach.h>
126#include <netinet/in_pcb.h>
127#include <netinet/if_inarp.h>
128#include <netinet/ip_mroute.h>
129#include <netinet/igmp_var.h>
130
131#ifdef IPSELSRC
132#include <netinet/in_selsrc.h>
133#endif
134
135#ifdef PFIL_HOOKS
136#include <net/pfil.h>
137#endif
138
139static u_int in_mask2len(struct in_addr *);
140static void in_len2mask(struct in_addr *, u_int);
141static int in_lifaddr_ioctl(struct socket *, u_long, void *,
142	struct ifnet *, struct lwp *);
143
144static int in_addprefix(struct in_ifaddr *, int);
145static int in_scrubprefix(struct in_ifaddr *);
146
147#ifndef SUBNETSARELOCAL
148#define	SUBNETSARELOCAL	1
149#endif
150
151#ifndef HOSTZEROBROADCAST
152#define HOSTZEROBROADCAST 1
153#endif
154
155int subnetsarelocal = SUBNETSARELOCAL;
156int hostzeroisbroadcast = HOSTZEROBROADCAST;
157
158/*
159 * This list is used to keep track of in_multi chains which belong to
160 * deleted interface addresses.  We use in_ifaddr so that a chain head
161 * won't be deallocated until all multicast address record are deleted.
162 */
163static TAILQ_HEAD(, in_ifaddr) in_mk = TAILQ_HEAD_INITIALIZER(in_mk);
164
165/*
166 * Return 1 if an internet address is for a ``local'' host
167 * (one to which we have a connection).  If subnetsarelocal
168 * is true, this includes other subnets of the local net.
169 * Otherwise, it includes only the directly-connected (sub)nets.
170 */
171int
172in_localaddr(struct in_addr in)
173{
174	struct in_ifaddr *ia;
175
176	if (subnetsarelocal) {
177		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list)
178			if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
179				return (1);
180	} else {
181		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list)
182			if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
183				return (1);
184	}
185	return (0);
186}
187
188/*
189 * Determine whether an IP address is in a reserved set of addresses
190 * that may not be forwarded, or whether datagrams to that destination
191 * may be forwarded.
192 */
193int
194in_canforward(struct in_addr in)
195{
196	u_int32_t net;
197
198	if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
199		return (0);
200	if (IN_CLASSA(in.s_addr)) {
201		net = in.s_addr & IN_CLASSA_NET;
202		if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
203			return (0);
204	}
205	return (1);
206}
207
208/*
209 * Trim a mask in a sockaddr
210 */
211void
212in_socktrim(struct sockaddr_in *ap)
213{
214	char *cplim = (char *) &ap->sin_addr;
215	char *cp = (char *) (&ap->sin_addr + 1);
216
217	ap->sin_len = 0;
218	while (--cp >= cplim)
219		if (*cp) {
220			(ap)->sin_len = cp - (char *) (ap) + 1;
221			break;
222		}
223}
224
225/*
226 *  Routine to take an Internet address and convert into a
227 *  "dotted quad" representation for printing.
228 */
229const char *
230in_fmtaddr(struct in_addr addr)
231{
232	static char buf[sizeof("123.456.789.123")];
233
234	addr.s_addr = ntohl(addr.s_addr);
235
236	snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
237		(addr.s_addr >> 24) & 0xFF,
238		(addr.s_addr >> 16) & 0xFF,
239		(addr.s_addr >>  8) & 0xFF,
240		(addr.s_addr >>  0) & 0xFF);
241	return buf;
242}
243
244/*
245 * Maintain the "in_maxmtu" variable, which is the largest
246 * mtu for non-local interfaces with AF_INET addresses assigned
247 * to them that are up.
248 */
249unsigned long in_maxmtu;
250
251void
252in_setmaxmtu(void)
253{
254	struct in_ifaddr *ia;
255	struct ifnet *ifp;
256	unsigned long maxmtu = 0;
257
258	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
259		if ((ifp = ia->ia_ifp) == 0)
260			continue;
261		if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
262			continue;
263		if (ifp->if_mtu > maxmtu)
264			maxmtu = ifp->if_mtu;
265	}
266	if (maxmtu)
267		in_maxmtu = maxmtu;
268}
269
270static u_int
271in_mask2len(struct in_addr *mask)
272{
273	u_int x, y;
274	u_char *p;
275
276	p = (u_char *)mask;
277	for (x = 0; x < sizeof(*mask); x++) {
278		if (p[x] != 0xff)
279			break;
280	}
281	y = 0;
282	if (x < sizeof(*mask)) {
283		for (y = 0; y < NBBY; y++) {
284			if ((p[x] & (0x80 >> y)) == 0)
285				break;
286		}
287	}
288	return x * NBBY + y;
289}
290
291static void
292in_len2mask(struct in_addr *mask, u_int len)
293{
294	u_int i;
295	u_char *p;
296
297	p = (u_char *)mask;
298	memset(mask, 0, sizeof(*mask));
299	for (i = 0; i < len / NBBY; i++)
300		p[i] = 0xff;
301	if (len % NBBY)
302		p[i] = (0xff00 >> (len % NBBY)) & 0xff;
303}
304
305/*
306 * Generic internet control operations (ioctl's).
307 * Ifp is 0 if not an interface-specific ioctl.
308 */
309/* ARGSUSED */
310int
311in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp,
312    struct lwp *l)
313{
314	struct ifreq *ifr = (struct ifreq *)data;
315	struct in_ifaddr *ia = NULL;
316	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
317	struct sockaddr_in oldaddr;
318	int error, hostIsNew, maskIsNew;
319	int newifaddr = 0;
320
321	switch (cmd) {
322	case SIOCALIFADDR:
323	case SIOCDLIFADDR:
324	case SIOCGLIFADDR:
325		if (ifp == NULL)
326			return EINVAL;
327		return in_lifaddr_ioctl(so, cmd, data, ifp, l);
328	case SIOCGIFADDRPREF:
329	case SIOCSIFADDRPREF:
330		if (ifp == NULL)
331			return EINVAL;
332		return ifaddrpref_ioctl(so, cmd, data, ifp, l);
333	}
334
335	/*
336	 * Find address for this interface, if it exists.
337	 */
338	if (ifp != NULL)
339		IFP_TO_IA(ifp, ia);
340
341	switch (cmd) {
342	case SIOCAIFADDR:
343	case SIOCDIFADDR:
344	case SIOCGIFALIAS:
345		if (ifra->ifra_addr.sin_family == AF_INET)
346			LIST_FOREACH(ia,
347			    &IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr),
348			    ia_hash) {
349				if (ia->ia_ifp == ifp &&
350				    in_hosteq(ia->ia_addr.sin_addr,
351				    ifra->ifra_addr.sin_addr))
352					break;
353			}
354		if ((cmd == SIOCDIFADDR || cmd == SIOCGIFALIAS) && ia == NULL)
355			return (EADDRNOTAVAIL);
356
357		if (cmd == SIOCDIFADDR &&
358		    ifra->ifra_addr.sin_family == AF_UNSPEC) {
359			ifra->ifra_addr.sin_family = AF_INET;
360		}
361		/* FALLTHROUGH */
362	case SIOCSIFADDR:
363	case SIOCSIFDSTADDR:
364		if (ifra->ifra_addr.sin_family != AF_INET)
365			return (EAFNOSUPPORT);
366		/* FALLTHROUGH */
367	case SIOCSIFNETMASK:
368		if (ifp == NULL)
369			panic("in_control");
370
371		if (cmd == SIOCGIFALIAS)
372			break;
373
374		if (ia == NULL &&
375		    (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR))
376			return (EADDRNOTAVAIL);
377
378		if (l == NULL)
379			return (EPERM);
380		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
381		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
382		    NULL) != 0)
383			return (EPERM);
384
385		if (ia == NULL) {
386			ia = malloc(sizeof(*ia), M_IFADDR, M_WAITOK|M_ZERO);
387			if (ia == NULL)
388				return (ENOBUFS);
389			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list);
390			IFAREF(&ia->ia_ifa);
391			ifa_insert(ifp, &ia->ia_ifa);
392			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
393			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
394			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
395#ifdef IPSELSRC
396			ia->ia_ifa.ifa_getifa = in_getifa;
397#else /* IPSELSRC */
398			ia->ia_ifa.ifa_getifa = NULL;
399#endif /* IPSELSRC */
400			ia->ia_sockmask.sin_len = 8;
401			if (ifp->if_flags & IFF_BROADCAST) {
402				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
403				ia->ia_broadaddr.sin_family = AF_INET;
404			}
405			ia->ia_ifp = ifp;
406			ia->ia_idsalt = cprng_fast32() % 65535;
407			LIST_INIT(&ia->ia_multiaddrs);
408			newifaddr = 1;
409		}
410		break;
411
412	case SIOCSIFBRDADDR:
413		if (l == NULL)
414			return (EPERM);
415		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
416		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
417		    NULL) != 0)
418			return (EPERM);
419		/* FALLTHROUGH */
420
421	case SIOCGIFADDR:
422	case SIOCGIFNETMASK:
423	case SIOCGIFDSTADDR:
424	case SIOCGIFBRDADDR:
425		if (ia == NULL)
426			return (EADDRNOTAVAIL);
427		break;
428	}
429	error = 0;
430	switch (cmd) {
431
432	case SIOCGIFADDR:
433		ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr));
434		break;
435
436	case SIOCGIFBRDADDR:
437		if ((ifp->if_flags & IFF_BROADCAST) == 0)
438			return (EINVAL);
439		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr));
440		break;
441
442	case SIOCGIFDSTADDR:
443		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
444			return (EINVAL);
445		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr));
446		break;
447
448	case SIOCGIFNETMASK:
449		ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_sockmask));
450		break;
451
452	case SIOCSIFDSTADDR:
453		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
454			return (EINVAL);
455		oldaddr = ia->ia_dstaddr;
456		ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr));
457		if ((error = if_addr_init(ifp, &ia->ia_ifa, false)) != 0) {
458			ia->ia_dstaddr = oldaddr;
459			return error;
460		}
461		if (ia->ia_flags & IFA_ROUTE) {
462			ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
463			rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
464			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
465			rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP);
466		}
467		break;
468
469	case SIOCSIFBRDADDR:
470		if ((ifp->if_flags & IFF_BROADCAST) == 0)
471			return EINVAL;
472		ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr));
473		break;
474
475	case SIOCSIFADDR:
476		error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)),
477		    1);
478#ifdef PFIL_HOOKS
479		if (error == 0)
480			(void)pfil_run_hooks(&if_pfil,
481			    (struct mbuf **)SIOCSIFADDR, ifp, PFIL_IFADDR);
482#endif
483		break;
484
485	case SIOCSIFNETMASK:
486		in_ifscrub(ifp, ia);
487		ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr));
488		ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
489		error = in_ifinit(ifp, ia, NULL, 0);
490		break;
491
492	case SIOCAIFADDR:
493		maskIsNew = 0;
494		hostIsNew = 1;
495		if (ia->ia_addr.sin_family != AF_INET)
496			;
497		else if (ifra->ifra_addr.sin_len == 0) {
498			ifra->ifra_addr = ia->ia_addr;
499			hostIsNew = 0;
500		} else if (in_hosteq(ia->ia_addr.sin_addr,
501		           ifra->ifra_addr.sin_addr))
502			hostIsNew = 0;
503		if (ifra->ifra_mask.sin_len) {
504			/* Only scrub if we control the prefix route,
505			 * otherwise userland gets a bogus message */
506			if ((ia->ia_flags & IFA_ROUTE))
507				in_ifscrub(ifp, ia);
508			ia->ia_sockmask = ifra->ifra_mask;
509			ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
510			maskIsNew = 1;
511		}
512		if ((ifp->if_flags & IFF_POINTOPOINT) &&
513		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
514			/* Only scrub if we control the prefix route,
515			 * otherwise userland gets a bogus message */
516			if ((ia->ia_flags & IFA_ROUTE))
517				in_ifscrub(ifp, ia);
518			ia->ia_dstaddr = ifra->ifra_dstaddr;
519			maskIsNew  = 1; /* We lie; but the effect's the same */
520		}
521		if (ifra->ifra_addr.sin_family == AF_INET &&
522		    (hostIsNew || maskIsNew)) {
523			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
524		}
525		if ((ifp->if_flags & IFF_BROADCAST) &&
526		    (ifra->ifra_broadaddr.sin_family == AF_INET))
527			ia->ia_broadaddr = ifra->ifra_broadaddr;
528#ifdef PFIL_HOOKS
529		if (error == 0)
530			(void)pfil_run_hooks(&if_pfil,
531			    (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR);
532#endif
533		break;
534
535	case SIOCGIFALIAS:
536		ifra->ifra_mask = ia->ia_sockmask;
537		if ((ifp->if_flags & IFF_POINTOPOINT) &&
538		    (ia->ia_dstaddr.sin_family == AF_INET))
539			ifra->ifra_dstaddr = ia->ia_dstaddr;
540		else if ((ifp->if_flags & IFF_BROADCAST) &&
541		    (ia->ia_broadaddr.sin_family == AF_INET))
542			ifra->ifra_broadaddr = ia->ia_broadaddr;
543		else
544			memset(&ifra->ifra_broadaddr, 0,
545			      sizeof(ifra->ifra_broadaddr));
546		break;
547
548	case SIOCDIFADDR:
549		in_purgeaddr(&ia->ia_ifa);
550#ifdef PFIL_HOOKS
551		(void)pfil_run_hooks(&if_pfil, (struct mbuf **)SIOCDIFADDR,
552		    ifp, PFIL_IFADDR);
553#endif
554		break;
555
556#ifdef MROUTING
557	case SIOCGETVIFCNT:
558	case SIOCGETSGCNT:
559		error = mrt_ioctl(so, cmd, data);
560		break;
561#endif /* MROUTING */
562
563	default:
564		return ENOTTY;
565	}
566
567	if (error != 0 && newifaddr) {
568		KASSERT(ia != NULL);
569		in_purgeaddr(&ia->ia_ifa);
570	}
571
572	return error;
573}
574
575void
576in_purgeaddr(struct ifaddr *ifa)
577{
578	struct ifnet *ifp = ifa->ifa_ifp;
579	struct in_ifaddr *ia = (void *) ifa;
580
581	in_ifscrub(ifp, ia);
582	LIST_REMOVE(ia, ia_hash);
583	ifa_remove(ifp, &ia->ia_ifa);
584	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list);
585	if (ia->ia_allhosts != NULL)
586		in_delmulti(ia->ia_allhosts);
587	IFAFREE(&ia->ia_ifa);
588	in_setmaxmtu();
589}
590
591void
592in_purgeif(struct ifnet *ifp)		/* MUST be called at splsoftnet() */
593{
594	if_purgeaddrs(ifp, AF_INET, in_purgeaddr);
595	igmp_purgeif(ifp);		/* manipulates pools */
596#ifdef MROUTING
597	ip_mrouter_detach(ifp);
598#endif
599}
600
601/*
602 * SIOC[GAD]LIFADDR.
603 *	SIOCGLIFADDR: get first address. (???)
604 *	SIOCGLIFADDR with IFLR_PREFIX:
605 *		get first address that matches the specified prefix.
606 *	SIOCALIFADDR: add the specified address.
607 *	SIOCALIFADDR with IFLR_PREFIX:
608 *		EINVAL since we can't deduce hostid part of the address.
609 *	SIOCDLIFADDR: delete the specified address.
610 *	SIOCDLIFADDR with IFLR_PREFIX:
611 *		delete the first address that matches the specified prefix.
612 * return values:
613 *	EINVAL on invalid parameters
614 *	EADDRNOTAVAIL on prefix match failed/specified address not found
615 *	other values may be returned from in_ioctl()
616 */
617static int
618in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data,
619    struct ifnet *ifp, struct lwp *l)
620{
621	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
622	struct ifaddr *ifa;
623	struct sockaddr *sa;
624
625	/* sanity checks */
626	if (data == NULL || ifp == NULL) {
627		panic("invalid argument to in_lifaddr_ioctl");
628		/*NOTRECHED*/
629	}
630
631	switch (cmd) {
632	case SIOCGLIFADDR:
633		/* address must be specified on GET with IFLR_PREFIX */
634		if ((iflr->flags & IFLR_PREFIX) == 0)
635			break;
636		/*FALLTHROUGH*/
637	case SIOCALIFADDR:
638	case SIOCDLIFADDR:
639		/* address must be specified on ADD and DELETE */
640		sa = (struct sockaddr *)&iflr->addr;
641		if (sa->sa_family != AF_INET)
642			return EINVAL;
643		if (sa->sa_len != sizeof(struct sockaddr_in))
644			return EINVAL;
645		/* XXX need improvement */
646		sa = (struct sockaddr *)&iflr->dstaddr;
647		if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
648			return EINVAL;
649		if (sa->sa_len != 0 && sa->sa_len != sizeof(struct sockaddr_in))
650			return EINVAL;
651		break;
652	default: /*shouldn't happen*/
653#if 0
654		panic("invalid cmd to in_lifaddr_ioctl");
655		/*NOTREACHED*/
656#else
657		return EOPNOTSUPP;
658#endif
659	}
660	if (sizeof(struct in_addr) * NBBY < iflr->prefixlen)
661		return EINVAL;
662
663	switch (cmd) {
664	case SIOCALIFADDR:
665	    {
666		struct in_aliasreq ifra;
667
668		if (iflr->flags & IFLR_PREFIX)
669			return EINVAL;
670
671		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
672		memset(&ifra, 0, sizeof(ifra));
673		memcpy(ifra.ifra_name, iflr->iflr_name,
674			sizeof(ifra.ifra_name));
675
676		memcpy(&ifra.ifra_addr, &iflr->addr,
677			((struct sockaddr *)&iflr->addr)->sa_len);
678
679		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
680			memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr,
681				((struct sockaddr *)&iflr->dstaddr)->sa_len);
682		}
683
684		ifra.ifra_mask.sin_family = AF_INET;
685		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
686		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
687
688		return in_control(so, SIOCAIFADDR, (void *)&ifra, ifp, l);
689	    }
690	case SIOCGLIFADDR:
691	case SIOCDLIFADDR:
692	    {
693		struct in_ifaddr *ia;
694		struct in_addr mask, candidate, match;
695		struct sockaddr_in *sin;
696		int cmp;
697
698		memset(&mask, 0, sizeof(mask));
699		memset(&match, 0, sizeof(match));	/* XXX gcc */
700		if (iflr->flags & IFLR_PREFIX) {
701			/* lookup a prefix rather than address. */
702			in_len2mask(&mask, iflr->prefixlen);
703
704			sin = (struct sockaddr_in *)&iflr->addr;
705			match.s_addr = sin->sin_addr.s_addr;
706			match.s_addr &= mask.s_addr;
707
708			/* if you set extra bits, that's wrong */
709			if (match.s_addr != sin->sin_addr.s_addr)
710				return EINVAL;
711
712			cmp = 1;
713		} else {
714			if (cmd == SIOCGLIFADDR) {
715				/* on getting an address, take the 1st match */
716				cmp = 0;	/*XXX*/
717			} else {
718				/* on deleting an address, do exact match */
719				in_len2mask(&mask, 32);
720				sin = (struct sockaddr_in *)&iflr->addr;
721				match.s_addr = sin->sin_addr.s_addr;
722
723				cmp = 1;
724			}
725		}
726
727		IFADDR_FOREACH(ifa, ifp) {
728			if (ifa->ifa_addr->sa_family != AF_INET)
729				continue;
730			if (cmp == 0)
731				break;
732			candidate.s_addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
733			candidate.s_addr &= mask.s_addr;
734			if (candidate.s_addr == match.s_addr)
735				break;
736		}
737		if (ifa == NULL)
738			return EADDRNOTAVAIL;
739		ia = (struct in_ifaddr *)ifa;
740
741		if (cmd == SIOCGLIFADDR) {
742			/* fill in the if_laddrreq structure */
743			memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin_len);
744
745			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
746				memcpy(&iflr->dstaddr, &ia->ia_dstaddr,
747					ia->ia_dstaddr.sin_len);
748			} else
749				memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr));
750
751			iflr->prefixlen =
752				in_mask2len(&ia->ia_sockmask.sin_addr);
753
754			iflr->flags = 0;	/*XXX*/
755
756			return 0;
757		} else {
758			struct in_aliasreq ifra;
759
760			/* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
761			memset(&ifra, 0, sizeof(ifra));
762			memcpy(ifra.ifra_name, iflr->iflr_name,
763				sizeof(ifra.ifra_name));
764
765			memcpy(&ifra.ifra_addr, &ia->ia_addr,
766				ia->ia_addr.sin_len);
767			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
768				memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr,
769					ia->ia_dstaddr.sin_len);
770			}
771			memcpy(&ifra.ifra_dstaddr, &ia->ia_sockmask,
772				ia->ia_sockmask.sin_len);
773
774			return in_control(so, SIOCDIFADDR, (void *)&ifra,
775				ifp, l);
776		}
777	    }
778	}
779
780	return EOPNOTSUPP;	/*just for safety*/
781}
782
783/*
784 * Delete any existing route for an interface.
785 */
786void
787in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
788{
789
790	in_scrubprefix(ia);
791}
792
793/*
794 * Initialize an interface's internet address
795 * and routing table entry.
796 */
797int
798in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
799    const struct sockaddr_in *sin, int scrub)
800{
801	u_int32_t i;
802	struct sockaddr_in oldaddr;
803	int s = splnet(), flags = RTF_UP, error;
804
805	if (sin == NULL)
806		sin = &ia->ia_addr;
807
808	/*
809	 * Set up new addresses.
810	 */
811	oldaddr = ia->ia_addr;
812	if (ia->ia_addr.sin_family == AF_INET)
813		LIST_REMOVE(ia, ia_hash);
814	ia->ia_addr = *sin;
815	LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
816
817	/*
818	 * Give the interface a chance to initialize
819	 * if this is its first address,
820	 * and to validate the address if necessary.
821	 */
822	if ((error = if_addr_init(ifp, &ia->ia_ifa, true)) != 0)
823		goto bad;
824	splx(s);
825	if (scrub) {
826		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
827		in_ifscrub(ifp, ia);
828		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
829	}
830
831	i = ia->ia_addr.sin_addr.s_addr;
832	if (IN_CLASSA(i))
833		ia->ia_netmask = IN_CLASSA_NET;
834	else if (IN_CLASSB(i))
835		ia->ia_netmask = IN_CLASSB_NET;
836	else
837		ia->ia_netmask = IN_CLASSC_NET;
838	/*
839	 * The subnet mask usually includes at least the standard network part,
840	 * but may may be smaller in the case of supernetting.
841	 * If it is set, we believe it.
842	 */
843	if (ia->ia_subnetmask == 0) {
844		ia->ia_subnetmask = ia->ia_netmask;
845		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
846	} else
847		ia->ia_netmask &= ia->ia_subnetmask;
848
849	ia->ia_net = i & ia->ia_netmask;
850	ia->ia_subnet = i & ia->ia_subnetmask;
851	in_socktrim(&ia->ia_sockmask);
852	/* re-calculate the "in_maxmtu" value */
853	in_setmaxmtu();
854	/*
855	 * Add route for the network.
856	 */
857	ia->ia_ifa.ifa_metric = ifp->if_metric;
858	if (ifp->if_flags & IFF_BROADCAST) {
859		ia->ia_broadaddr.sin_addr.s_addr =
860			ia->ia_subnet | ~ia->ia_subnetmask;
861		ia->ia_netbroadcast.s_addr =
862			ia->ia_net | ~ia->ia_netmask;
863	} else if (ifp->if_flags & IFF_LOOPBACK) {
864		ia->ia_dstaddr = ia->ia_addr;
865		flags |= RTF_HOST;
866	} else if (ifp->if_flags & IFF_POINTOPOINT) {
867		if (ia->ia_dstaddr.sin_family != AF_INET)
868			return (0);
869		flags |= RTF_HOST;
870	}
871	error = in_addprefix(ia, flags);
872	/*
873	 * If the interface supports multicast, join the "all hosts"
874	 * multicast group on that interface.
875	 */
876	if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
877		struct in_addr addr;
878
879		addr.s_addr = INADDR_ALLHOSTS_GROUP;
880		ia->ia_allhosts = in_addmulti(&addr, ifp);
881	}
882	return (error);
883bad:
884	splx(s);
885	LIST_REMOVE(ia, ia_hash);
886	ia->ia_addr = oldaddr;
887	if (ia->ia_addr.sin_family == AF_INET)
888		LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
889		    ia, ia_hash);
890	return (error);
891}
892
893#define rtinitflags(x) \
894	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
895	    ? RTF_HOST : 0)
896
897/*
898 * add a route to prefix ("connected route" in cisco terminology).
899 * does nothing if there's some interface address with the same prefix already.
900 */
901static int
902in_addprefix(struct in_ifaddr *target, int flags)
903{
904	struct in_ifaddr *ia;
905	struct in_addr prefix, mask, p;
906	int error;
907
908	if ((flags & RTF_HOST) != 0)
909		prefix = target->ia_dstaddr.sin_addr;
910	else {
911		prefix = target->ia_addr.sin_addr;
912		mask = target->ia_sockmask.sin_addr;
913		prefix.s_addr &= mask.s_addr;
914	}
915
916	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
917		if (rtinitflags(ia))
918			p = ia->ia_dstaddr.sin_addr;
919		else {
920			p = ia->ia_addr.sin_addr;
921			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
922		}
923
924		if (prefix.s_addr != p.s_addr)
925			continue;
926
927		/*
928		 * if we got a matching prefix route inserted by other
929		 * interface address, we don't need to bother
930		 *
931		 * XXX RADIX_MPATH implications here? -dyoung
932		 *
933		 * But we should still notify userland of the new address
934		 */
935		if (ia->ia_flags & IFA_ROUTE) {
936			rt_newaddrmsg(RTM_NEWADDR, &target->ia_ifa, 0, NULL);
937			return 0;
938		}
939	}
940
941	/*
942	 * noone seem to have prefix route.  insert it.
943	 */
944	error = rtinit(&target->ia_ifa, RTM_ADD, flags);
945	if (error == 0)
946		target->ia_flags |= IFA_ROUTE;
947	else if (error == EEXIST) {
948		/*
949		 * the fact the route already exists is not an error.
950		 */
951		error = 0;
952	}
953	return error;
954}
955
956/*
957 * remove a route to prefix ("connected route" in cisco terminology).
958 * re-installs the route by using another interface address, if there's one
959 * with the same prefix (otherwise we lose the route mistakenly).
960 */
961static int
962in_scrubprefix(struct in_ifaddr *target)
963{
964	struct in_ifaddr *ia;
965	struct in_addr prefix, mask, p;
966	int error;
967
968	/* If we don't have IFA_ROUTE we should still inform userland */
969	if ((target->ia_flags & IFA_ROUTE) == 0) {
970		rt_newaddrmsg(RTM_DELADDR, &target->ia_ifa, 0, NULL);
971		return 0;
972	}
973
974	if (rtinitflags(target))
975		prefix = target->ia_dstaddr.sin_addr;
976	else {
977		prefix = target->ia_addr.sin_addr;
978		mask = target->ia_sockmask.sin_addr;
979		prefix.s_addr &= mask.s_addr;
980	}
981
982	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
983		if (rtinitflags(ia))
984			p = ia->ia_dstaddr.sin_addr;
985		else {
986			p = ia->ia_addr.sin_addr;
987			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
988		}
989
990		if (prefix.s_addr != p.s_addr)
991			continue;
992
993		/*
994		 * if we got a matching prefix route, move IFA_ROUTE to him
995		 */
996		if ((ia->ia_flags & IFA_ROUTE) == 0) {
997			rtinit(&target->ia_ifa, RTM_DELETE,
998			    rtinitflags(target));
999			target->ia_flags &= ~IFA_ROUTE;
1000
1001			error = rtinit(&ia->ia_ifa, RTM_ADD,
1002			    rtinitflags(ia) | RTF_UP);
1003			if (error == 0)
1004				ia->ia_flags |= IFA_ROUTE;
1005			return error;
1006		}
1007	}
1008
1009	/*
1010	 * noone seem to have prefix route.  remove it.
1011	 */
1012	rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1013	target->ia_flags &= ~IFA_ROUTE;
1014	return 0;
1015}
1016
1017#undef rtinitflags
1018
1019/*
1020 * Return 1 if the address might be a local broadcast address.
1021 */
1022int
1023in_broadcast(struct in_addr in, struct ifnet *ifp)
1024{
1025	struct ifaddr *ifa;
1026
1027	if (in.s_addr == INADDR_BROADCAST ||
1028	    in_nullhost(in))
1029		return 1;
1030	if ((ifp->if_flags & IFF_BROADCAST) == 0)
1031		return 0;
1032	/*
1033	 * Look through the list of addresses for a match
1034	 * with a broadcast address.
1035	 */
1036#define ia (ifatoia(ifa))
1037	IFADDR_FOREACH(ifa, ifp)
1038		if (ifa->ifa_addr->sa_family == AF_INET &&
1039		    !in_hosteq(in, ia->ia_addr.sin_addr) &&
1040		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
1041		     in_hosteq(in, ia->ia_netbroadcast) ||
1042		     (hostzeroisbroadcast &&
1043		      /*
1044		       * Check for old-style (host 0) broadcast.
1045		       */
1046		      (in.s_addr == ia->ia_subnet ||
1047		       in.s_addr == ia->ia_net))))
1048			return 1;
1049	return (0);
1050#undef ia
1051}
1052
1053/*
1054 * Add an address to the list of IP multicast addresses for a given interface.
1055 */
1056struct in_multi *
1057in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1058{
1059	struct sockaddr_in sin;
1060	struct in_multi *inm;
1061	int s = splsoftnet();
1062
1063	/*
1064	 * See if address already in list.
1065	 */
1066	IN_LOOKUP_MULTI(*ap, ifp, inm);
1067	if (inm != NULL) {
1068		/*
1069		 * Found it; just increment the reference count.
1070		 */
1071		++inm->inm_refcount;
1072	} else {
1073		/*
1074		 * New address; allocate a new multicast record
1075		 * and link it into the interface's multicast list.
1076		 */
1077		inm = pool_get(&inmulti_pool, PR_NOWAIT);
1078		if (inm == NULL) {
1079			splx(s);
1080			return (NULL);
1081		}
1082		inm->inm_addr = *ap;
1083		inm->inm_ifp = ifp;
1084		inm->inm_refcount = 1;
1085		LIST_INSERT_HEAD(
1086		    &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp),
1087		    inm, inm_list);
1088		/*
1089		 * Ask the network driver to update its multicast reception
1090		 * filter appropriately for the new address.
1091		 */
1092		sockaddr_in_init(&sin, ap, 0);
1093		if (if_mcast_op(ifp, SIOCADDMULTI, sintosa(&sin)) != 0) {
1094			LIST_REMOVE(inm, inm_list);
1095			pool_put(&inmulti_pool, inm);
1096			splx(s);
1097			return (NULL);
1098		}
1099		/*
1100		 * Let IGMP know that we have joined a new IP multicast group.
1101		 */
1102		if (igmp_joingroup(inm) != 0) {
1103			LIST_REMOVE(inm, inm_list);
1104			pool_put(&inmulti_pool, inm);
1105			splx(s);
1106			return (NULL);
1107		}
1108		in_multientries++;
1109	}
1110	splx(s);
1111	return (inm);
1112}
1113
1114/*
1115 * Delete a multicast address record.
1116 */
1117void
1118in_delmulti(struct in_multi *inm)
1119{
1120	struct sockaddr_in sin;
1121	int s = splsoftnet();
1122
1123	if (--inm->inm_refcount == 0) {
1124		/*
1125		 * No remaining claims to this record; let IGMP know that
1126		 * we are leaving the multicast group.
1127		 */
1128		igmp_leavegroup(inm);
1129		/*
1130		 * Unlink from list.
1131		 */
1132		LIST_REMOVE(inm, inm_list);
1133		in_multientries--;
1134		/*
1135		 * Notify the network driver to update its multicast reception
1136		 * filter.
1137		 */
1138		sockaddr_in_init(&sin, &inm->inm_addr, 0);
1139		if_mcast_op(inm->inm_ifp, SIOCDELMULTI, sintosa(&sin));
1140		pool_put(&inmulti_pool, inm);
1141	}
1142	splx(s);
1143}
1144