in.c revision 137628
1/*
2 * Copyright (C) 2001 WIDE Project.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the project nor the names of its contributors
13 *    may be used to endorse or promote products derived from this software
14 *    without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29/*
30 * Copyright (c) 1982, 1986, 1991, 1993
31 *	The Regents of the University of California.  All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 *    notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 *    notice, this list of conditions and the following disclaimer in the
40 *    documentation and/or other materials provided with the distribution.
41 * 4. Neither the name of the University nor the names of its contributors
42 *    may be used to endorse or promote products derived from this software
43 *    without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 *	@(#)in.c	8.4 (Berkeley) 1/9/95
58 * $FreeBSD: head/sys/netinet/in.c 137628 2004-11-12 20:53:51Z mlaier $
59 */
60
61#include <sys/param.h>
62#include <sys/systm.h>
63#include <sys/sockio.h>
64#include <sys/malloc.h>
65#include <sys/socket.h>
66#include <sys/kernel.h>
67#include <sys/sysctl.h>
68
69#include <net/if.h>
70#include <net/if_types.h>
71#include <net/route.h>
72
73#include <netinet/in.h>
74#include <netinet/in_var.h>
75#include <netinet/in_pcb.h>
76
77#include <netinet/igmp_var.h>
78
79static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
80
81static int in_mask2len(struct in_addr *);
82static void in_len2mask(struct in_addr *, int);
83static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
84	struct ifnet *, struct thread *);
85
86static int	in_addprefix(struct in_ifaddr *, int);
87static int	in_scrubprefix(struct in_ifaddr *);
88static void	in_socktrim(struct sockaddr_in *);
89static int	in_ifinit(struct ifnet *,
90	    struct in_ifaddr *, struct sockaddr_in *, int);
91
92static int subnetsarelocal = 0;
93SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
94	&subnetsarelocal, 0, "Treat all subnets as directly connected");
95
96struct in_multihead in_multihead; /* XXX BSS initialization */
97
98extern struct inpcbinfo ripcbinfo;
99extern struct inpcbinfo udbinfo;
100
101/*
102 * Return 1 if an internet address is for a ``local'' host
103 * (one to which we have a connection).  If subnetsarelocal
104 * is true, this includes other subnets of the local net.
105 * Otherwise, it includes only the directly-connected (sub)nets.
106 */
107int
108in_localaddr(in)
109	struct in_addr in;
110{
111	register u_long i = ntohl(in.s_addr);
112	register struct in_ifaddr *ia;
113
114	if (subnetsarelocal) {
115		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
116			if ((i & ia->ia_netmask) == ia->ia_net)
117				return (1);
118	} else {
119		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
120			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
121				return (1);
122	}
123	return (0);
124}
125
126/*
127 * Return 1 if an internet address is for the local host and configured
128 * on one of its interfaces.
129 */
130int
131in_localip(in)
132	struct in_addr in;
133{
134	struct in_ifaddr *ia;
135
136	LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
137		if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
138			return 1;
139	}
140	return 0;
141}
142
143/*
144 * Determine whether an IP address is in a reserved set of addresses
145 * that may not be forwarded, or whether datagrams to that destination
146 * may be forwarded.
147 */
148int
149in_canforward(in)
150	struct in_addr in;
151{
152	register u_long i = ntohl(in.s_addr);
153	register u_long net;
154
155	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
156		return (0);
157	if (IN_CLASSA(i)) {
158		net = i & IN_CLASSA_NET;
159		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
160			return (0);
161	}
162	return (1);
163}
164
165/*
166 * Trim a mask in a sockaddr
167 */
168static void
169in_socktrim(ap)
170struct sockaddr_in *ap;
171{
172    register char *cplim = (char *) &ap->sin_addr;
173    register char *cp = (char *) (&ap->sin_addr + 1);
174
175    ap->sin_len = 0;
176    while (--cp >= cplim)
177	if (*cp) {
178	    (ap)->sin_len = cp - (char *) (ap) + 1;
179	    break;
180	}
181}
182
183static int
184in_mask2len(mask)
185	struct in_addr *mask;
186{
187	int x, y;
188	u_char *p;
189
190	p = (u_char *)mask;
191	for (x = 0; x < sizeof(*mask); x++) {
192		if (p[x] != 0xff)
193			break;
194	}
195	y = 0;
196	if (x < sizeof(*mask)) {
197		for (y = 0; y < 8; y++) {
198			if ((p[x] & (0x80 >> y)) == 0)
199				break;
200		}
201	}
202	return x * 8 + y;
203}
204
205static void
206in_len2mask(mask, len)
207	struct in_addr *mask;
208	int len;
209{
210	int i;
211	u_char *p;
212
213	p = (u_char *)mask;
214	bzero(mask, sizeof(*mask));
215	for (i = 0; i < len / 8; i++)
216		p[i] = 0xff;
217	if (len % 8)
218		p[i] = (0xff00 >> (len % 8)) & 0xff;
219}
220
221/*
222 * Generic internet control operations (ioctl's).
223 * Ifp is 0 if not an interface-specific ioctl.
224 */
225/* ARGSUSED */
226int
227in_control(so, cmd, data, ifp, td)
228	struct socket *so;
229	u_long cmd;
230	caddr_t data;
231	register struct ifnet *ifp;
232	struct thread *td;
233{
234	register struct ifreq *ifr = (struct ifreq *)data;
235	register struct in_ifaddr *ia = 0, *iap;
236	register struct ifaddr *ifa;
237	struct in_addr dst;
238	struct in_ifaddr *oia;
239	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
240	struct sockaddr_in oldaddr;
241	int error, hostIsNew, iaIsNew, maskIsNew, s;
242
243	iaIsNew = 0;
244
245	switch (cmd) {
246	case SIOCALIFADDR:
247	case SIOCDLIFADDR:
248		if (td && (error = suser(td)) != 0)
249			return error;
250		/*fall through*/
251	case SIOCGLIFADDR:
252		if (!ifp)
253			return EINVAL;
254		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
255	}
256
257	/*
258	 * Find address for this interface, if it exists.
259	 *
260	 * If an alias address was specified, find that one instead of
261	 * the first one on the interface, if possible.
262	 */
263	if (ifp) {
264		dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
265		LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
266			if (iap->ia_ifp == ifp &&
267			    iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
268				ia = iap;
269				break;
270			}
271		if (ia == NULL)
272			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
273				iap = ifatoia(ifa);
274				if (iap->ia_addr.sin_family == AF_INET) {
275					ia = iap;
276					break;
277				}
278			}
279	}
280
281	switch (cmd) {
282
283	case SIOCAIFADDR:
284	case SIOCDIFADDR:
285		if (ifp == 0)
286			return (EADDRNOTAVAIL);
287		if (ifra->ifra_addr.sin_family == AF_INET) {
288			for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
289				if (ia->ia_ifp == ifp  &&
290				    ia->ia_addr.sin_addr.s_addr ==
291				    ifra->ifra_addr.sin_addr.s_addr)
292					break;
293			}
294			if ((ifp->if_flags & IFF_POINTOPOINT)
295			    && (cmd == SIOCAIFADDR)
296			    && (ifra->ifra_dstaddr.sin_addr.s_addr
297				== INADDR_ANY)) {
298				return EDESTADDRREQ;
299			}
300		}
301		if (cmd == SIOCDIFADDR && ia == 0)
302			return (EADDRNOTAVAIL);
303		/* FALLTHROUGH */
304	case SIOCSIFADDR:
305	case SIOCSIFNETMASK:
306	case SIOCSIFDSTADDR:
307		if (td && (error = suser(td)) != 0)
308			return error;
309
310		if (ifp == 0)
311			return (EADDRNOTAVAIL);
312		if (ia == (struct in_ifaddr *)0) {
313			ia = (struct in_ifaddr *)
314				malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
315			if (ia == (struct in_ifaddr *)NULL)
316				return (ENOBUFS);
317			/*
318			 * Protect from ipintr() traversing address list
319			 * while we're modifying it.
320			 */
321			s = splnet();
322			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
323
324			ifa = &ia->ia_ifa;
325			IFA_LOCK_INIT(ifa);
326			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
327			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
328			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
329			ifa->ifa_refcnt = 1;
330			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
331
332			ia->ia_sockmask.sin_len = 8;
333			ia->ia_sockmask.sin_family = AF_INET;
334			if (ifp->if_flags & IFF_BROADCAST) {
335				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
336				ia->ia_broadaddr.sin_family = AF_INET;
337			}
338			ia->ia_ifp = ifp;
339			splx(s);
340			iaIsNew = 1;
341		}
342		break;
343
344	case SIOCSIFBRDADDR:
345		if (td && (error = suser(td)) != 0)
346			return error;
347		/* FALLTHROUGH */
348
349	case SIOCGIFADDR:
350	case SIOCGIFNETMASK:
351	case SIOCGIFDSTADDR:
352	case SIOCGIFBRDADDR:
353		if (ia == (struct in_ifaddr *)0)
354			return (EADDRNOTAVAIL);
355		break;
356	}
357	switch (cmd) {
358
359	case SIOCGIFADDR:
360		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
361		return (0);
362
363	case SIOCGIFBRDADDR:
364		if ((ifp->if_flags & IFF_BROADCAST) == 0)
365			return (EINVAL);
366		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
367		return (0);
368
369	case SIOCGIFDSTADDR:
370		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
371			return (EINVAL);
372		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
373		return (0);
374
375	case SIOCGIFNETMASK:
376		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
377		return (0);
378
379	case SIOCSIFDSTADDR:
380		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
381			return (EINVAL);
382		oldaddr = ia->ia_dstaddr;
383		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
384		if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
385					(ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
386			ia->ia_dstaddr = oldaddr;
387			return (error);
388		}
389		if (ia->ia_flags & IFA_ROUTE) {
390			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
391			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
392			ia->ia_ifa.ifa_dstaddr =
393					(struct sockaddr *)&ia->ia_dstaddr;
394			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
395		}
396		return (0);
397
398	case SIOCSIFBRDADDR:
399		if ((ifp->if_flags & IFF_BROADCAST) == 0)
400			return (EINVAL);
401		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
402		return (0);
403
404	case SIOCSIFADDR:
405		error = in_ifinit(ifp, ia,
406		    (struct sockaddr_in *) &ifr->ifr_addr, 1);
407		if (error != 0 && iaIsNew)
408			break;
409		if (error == 0)
410			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
411		return (0);
412
413	case SIOCSIFNETMASK:
414		ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
415		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
416		return (0);
417
418	case SIOCAIFADDR:
419		maskIsNew = 0;
420		hostIsNew = 1;
421		error = 0;
422		if (ia->ia_addr.sin_family == AF_INET) {
423			if (ifra->ifra_addr.sin_len == 0) {
424				ifra->ifra_addr = ia->ia_addr;
425				hostIsNew = 0;
426			} else if (ifra->ifra_addr.sin_addr.s_addr ==
427					       ia->ia_addr.sin_addr.s_addr)
428				hostIsNew = 0;
429		}
430		if (ifra->ifra_mask.sin_len) {
431			in_ifscrub(ifp, ia);
432			ia->ia_sockmask = ifra->ifra_mask;
433			ia->ia_sockmask.sin_family = AF_INET;
434			ia->ia_subnetmask =
435			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
436			maskIsNew = 1;
437		}
438		if ((ifp->if_flags & IFF_POINTOPOINT) &&
439		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
440			in_ifscrub(ifp, ia);
441			ia->ia_dstaddr = ifra->ifra_dstaddr;
442			maskIsNew  = 1; /* We lie; but the effect's the same */
443		}
444		if (ifra->ifra_addr.sin_family == AF_INET &&
445		    (hostIsNew || maskIsNew))
446			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
447		if (error != 0 && iaIsNew)
448			break;
449
450		if ((ifp->if_flags & IFF_BROADCAST) &&
451		    (ifra->ifra_broadaddr.sin_family == AF_INET))
452			ia->ia_broadaddr = ifra->ifra_broadaddr;
453		if (error == 0)
454			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
455		return (error);
456
457	case SIOCDIFADDR:
458		/*
459		 * in_ifscrub kills the interface route.
460		 */
461		in_ifscrub(ifp, ia);
462		/*
463		 * in_ifadown gets rid of all the rest of
464		 * the routes.  This is not quite the right
465		 * thing to do, but at least if we are running
466		 * a routing process they will come back.
467		 */
468		in_ifadown(&ia->ia_ifa, 1);
469		/*
470		 * XXX horrible hack to detect that we are being called
471		 * from if_detach()
472		 */
473		if (ifaddr_byindex(ifp->if_index) == NULL) {
474			in_pcbpurgeif0(&ripcbinfo, ifp);
475			in_pcbpurgeif0(&udbinfo, ifp);
476		}
477		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
478		error = 0;
479		break;
480
481	default:
482		if (ifp == 0 || ifp->if_ioctl == 0)
483			return (EOPNOTSUPP);
484		return ((*ifp->if_ioctl)(ifp, cmd, data));
485	}
486
487	/*
488	 * Protect from ipintr() traversing address list while we're modifying
489	 * it.
490	 */
491	s = splnet();
492	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
493	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
494	LIST_REMOVE(ia, ia_hash);
495	IFAFREE(&ia->ia_ifa);
496	splx(s);
497
498	return (error);
499}
500
501/*
502 * SIOC[GAD]LIFADDR.
503 *	SIOCGLIFADDR: get first address. (?!?)
504 *	SIOCGLIFADDR with IFLR_PREFIX:
505 *		get first address that matches the specified prefix.
506 *	SIOCALIFADDR: add the specified address.
507 *	SIOCALIFADDR with IFLR_PREFIX:
508 *		EINVAL since we can't deduce hostid part of the address.
509 *	SIOCDLIFADDR: delete the specified address.
510 *	SIOCDLIFADDR with IFLR_PREFIX:
511 *		delete the first address that matches the specified prefix.
512 * return values:
513 *	EINVAL on invalid parameters
514 *	EADDRNOTAVAIL on prefix match failed/specified address not found
515 *	other values may be returned from in_ioctl()
516 */
517static int
518in_lifaddr_ioctl(so, cmd, data, ifp, td)
519	struct socket *so;
520	u_long cmd;
521	caddr_t	data;
522	struct ifnet *ifp;
523	struct thread *td;
524{
525	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
526	struct ifaddr *ifa;
527
528	/* sanity checks */
529	if (!data || !ifp) {
530		panic("invalid argument to in_lifaddr_ioctl");
531		/*NOTRECHED*/
532	}
533
534	switch (cmd) {
535	case SIOCGLIFADDR:
536		/* address must be specified on GET with IFLR_PREFIX */
537		if ((iflr->flags & IFLR_PREFIX) == 0)
538			break;
539		/*FALLTHROUGH*/
540	case SIOCALIFADDR:
541	case SIOCDLIFADDR:
542		/* address must be specified on ADD and DELETE */
543		if (iflr->addr.ss_family != AF_INET)
544			return EINVAL;
545		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
546			return EINVAL;
547		/* XXX need improvement */
548		if (iflr->dstaddr.ss_family
549		 && iflr->dstaddr.ss_family != AF_INET)
550			return EINVAL;
551		if (iflr->dstaddr.ss_family
552		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
553			return EINVAL;
554		break;
555	default: /*shouldn't happen*/
556		return EOPNOTSUPP;
557	}
558	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
559		return EINVAL;
560
561	switch (cmd) {
562	case SIOCALIFADDR:
563	    {
564		struct in_aliasreq ifra;
565
566		if (iflr->flags & IFLR_PREFIX)
567			return EINVAL;
568
569		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
570		bzero(&ifra, sizeof(ifra));
571		bcopy(iflr->iflr_name, ifra.ifra_name,
572			sizeof(ifra.ifra_name));
573
574		bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
575
576		if (iflr->dstaddr.ss_family) {	/*XXX*/
577			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
578				iflr->dstaddr.ss_len);
579		}
580
581		ifra.ifra_mask.sin_family = AF_INET;
582		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
583		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
584
585		return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
586	    }
587	case SIOCGLIFADDR:
588	case SIOCDLIFADDR:
589	    {
590		struct in_ifaddr *ia;
591		struct in_addr mask, candidate, match;
592		struct sockaddr_in *sin;
593		int cmp;
594
595		bzero(&mask, sizeof(mask));
596		if (iflr->flags & IFLR_PREFIX) {
597			/* lookup a prefix rather than address. */
598			in_len2mask(&mask, iflr->prefixlen);
599
600			sin = (struct sockaddr_in *)&iflr->addr;
601			match.s_addr = sin->sin_addr.s_addr;
602			match.s_addr &= mask.s_addr;
603
604			/* if you set extra bits, that's wrong */
605			if (match.s_addr != sin->sin_addr.s_addr)
606				return EINVAL;
607
608			cmp = 1;
609		} else {
610			if (cmd == SIOCGLIFADDR) {
611				/* on getting an address, take the 1st match */
612				cmp = 0;	/*XXX*/
613			} else {
614				/* on deleting an address, do exact match */
615				in_len2mask(&mask, 32);
616				sin = (struct sockaddr_in *)&iflr->addr;
617				match.s_addr = sin->sin_addr.s_addr;
618
619				cmp = 1;
620			}
621		}
622
623		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)	{
624			if (ifa->ifa_addr->sa_family != AF_INET6)
625				continue;
626			if (!cmp)
627				break;
628			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
629			candidate.s_addr &= mask.s_addr;
630			if (candidate.s_addr == match.s_addr)
631				break;
632		}
633		if (!ifa)
634			return EADDRNOTAVAIL;
635		ia = (struct in_ifaddr *)ifa;
636
637		if (cmd == SIOCGLIFADDR) {
638			/* fill in the if_laddrreq structure */
639			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
640
641			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
642				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
643					ia->ia_dstaddr.sin_len);
644			} else
645				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
646
647			iflr->prefixlen =
648				in_mask2len(&ia->ia_sockmask.sin_addr);
649
650			iflr->flags = 0;	/*XXX*/
651
652			return 0;
653		} else {
654			struct in_aliasreq ifra;
655
656			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
657			bzero(&ifra, sizeof(ifra));
658			bcopy(iflr->iflr_name, ifra.ifra_name,
659				sizeof(ifra.ifra_name));
660
661			bcopy(&ia->ia_addr, &ifra.ifra_addr,
662				ia->ia_addr.sin_len);
663			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
664				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
665					ia->ia_dstaddr.sin_len);
666			}
667			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
668				ia->ia_sockmask.sin_len);
669
670			return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
671					  ifp, td);
672		}
673	    }
674	}
675
676	return EOPNOTSUPP;	/*just for safety*/
677}
678
679/*
680 * Delete any existing route for an interface.
681 */
682void
683in_ifscrub(ifp, ia)
684	register struct ifnet *ifp;
685	register struct in_ifaddr *ia;
686{
687	in_scrubprefix(ia);
688}
689
690/*
691 * Initialize an interface's internet address
692 * and routing table entry.
693 */
694static int
695in_ifinit(ifp, ia, sin, scrub)
696	register struct ifnet *ifp;
697	register struct in_ifaddr *ia;
698	struct sockaddr_in *sin;
699	int scrub;
700{
701	register u_long i = ntohl(sin->sin_addr.s_addr);
702	struct sockaddr_in oldaddr;
703	int s = splimp(), flags = RTF_UP, error = 0;
704
705	oldaddr = ia->ia_addr;
706	if (oldaddr.sin_family == AF_INET)
707		LIST_REMOVE(ia, ia_hash);
708	ia->ia_addr = *sin;
709	if (ia->ia_addr.sin_family == AF_INET)
710		LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
711		    ia, ia_hash);
712	/*
713	 * Give the interface a chance to initialize
714	 * if this is its first address,
715	 * and to validate the address if necessary.
716	 */
717	if (ifp->if_ioctl &&
718	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
719		splx(s);
720		/* LIST_REMOVE(ia, ia_hash) is done in in_control */
721		ia->ia_addr = oldaddr;
722		if (ia->ia_addr.sin_family == AF_INET)
723			LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
724			    ia, ia_hash);
725		return (error);
726	}
727	splx(s);
728	if (scrub) {
729		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
730		in_ifscrub(ifp, ia);
731		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
732	}
733	if (IN_CLASSA(i))
734		ia->ia_netmask = IN_CLASSA_NET;
735	else if (IN_CLASSB(i))
736		ia->ia_netmask = IN_CLASSB_NET;
737	else
738		ia->ia_netmask = IN_CLASSC_NET;
739	/*
740	 * The subnet mask usually includes at least the standard network part,
741	 * but may may be smaller in the case of supernetting.
742	 * If it is set, we believe it.
743	 */
744	if (ia->ia_subnetmask == 0) {
745		ia->ia_subnetmask = ia->ia_netmask;
746		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
747	} else
748		ia->ia_netmask &= ia->ia_subnetmask;
749	ia->ia_net = i & ia->ia_netmask;
750	ia->ia_subnet = i & ia->ia_subnetmask;
751	in_socktrim(&ia->ia_sockmask);
752	/*
753	 * Add route for the network.
754	 */
755	ia->ia_ifa.ifa_metric = ifp->if_metric;
756	if (ifp->if_flags & IFF_BROADCAST) {
757		ia->ia_broadaddr.sin_addr.s_addr =
758			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
759		ia->ia_netbroadcast.s_addr =
760			htonl(ia->ia_net | ~ ia->ia_netmask);
761	} else if (ifp->if_flags & IFF_LOOPBACK) {
762		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
763		flags |= RTF_HOST;
764	} else if (ifp->if_flags & IFF_POINTOPOINT) {
765		if (ia->ia_dstaddr.sin_family != AF_INET)
766			return (0);
767		flags |= RTF_HOST;
768	}
769	if ((error = in_addprefix(ia, flags)) != 0)
770		return (error);
771
772	/*
773	 * If the interface supports multicast, join the "all hosts"
774	 * multicast group on that interface.
775	 */
776	if (ifp->if_flags & IFF_MULTICAST) {
777		struct in_addr addr;
778
779		addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
780		in_addmulti(&addr, ifp);
781	}
782	return (error);
783}
784
785#define rtinitflags(x) \
786	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
787	    ? RTF_HOST : 0)
788/*
789 * Check if we have a route for the given prefix already or add a one
790 * accordingly.
791 */
792static int
793in_addprefix(target, flags)
794	struct in_ifaddr *target;
795	int flags;
796{
797	struct in_ifaddr *ia;
798	struct in_addr prefix, mask, p;
799	int error;
800
801	if ((flags & RTF_HOST) != 0)
802		prefix = target->ia_dstaddr.sin_addr;
803	else {
804		prefix = target->ia_addr.sin_addr;
805		mask = target->ia_sockmask.sin_addr;
806		prefix.s_addr &= mask.s_addr;
807	}
808
809	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
810		if (rtinitflags(ia))
811			p = ia->ia_dstaddr.sin_addr;
812		else {
813			p = ia->ia_addr.sin_addr;
814			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
815		}
816
817		if (prefix.s_addr != p.s_addr)
818			continue;
819
820		/*
821		 * If we got a matching prefix route inserted by other
822		 * interface address, we are done here.
823		 */
824		if (ia->ia_flags & IFA_ROUTE)
825			return 0;
826	}
827
828	/*
829	 * No-one seem to have this prefix route, so we try to insert it.
830	 */
831	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
832	if (!error)
833		target->ia_flags |= IFA_ROUTE;
834	return error;
835}
836
837/*
838 * If there is no other address in the system that can serve a route to the
839 * same prefix, remove the route.  Hand over the route to the new address
840 * otherwise.
841 */
842static int
843in_scrubprefix(target)
844	struct in_ifaddr *target;
845{
846	struct in_ifaddr *ia;
847	struct in_addr prefix, mask, p;
848	int error;
849
850	if ((target->ia_flags & IFA_ROUTE) == 0)
851		return 0;
852
853	if (rtinitflags(target))
854		prefix = target->ia_dstaddr.sin_addr;
855	else {
856		prefix = target->ia_addr.sin_addr;
857		mask = target->ia_sockmask.sin_addr;
858		prefix.s_addr &= mask.s_addr;
859	}
860
861	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
862		if (rtinitflags(ia))
863			p = ia->ia_dstaddr.sin_addr;
864		else {
865			p = ia->ia_addr.sin_addr;
866			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
867		}
868
869		if (prefix.s_addr != p.s_addr)
870			continue;
871
872		/*
873		 * If we got a matching prefix address, move IFA_ROUTE and
874		 * the route itself to it.  Make sure that routing daemons
875		 * get a heads-up.
876		 */
877		if ((ia->ia_flags & IFA_ROUTE) == 0) {
878			rtinit(&(target->ia_ifa), (int)RTM_DELETE,
879			    rtinitflags(target));
880			target->ia_flags &= ~IFA_ROUTE;
881
882			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
883			    rtinitflags(ia) | RTF_UP);
884			if (error == 0)
885				ia->ia_flags |= IFA_ROUTE;
886			return error;
887		}
888	}
889
890	/*
891	 * As no-one seem to have this prefix, we can remove the route.
892	 */
893	rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
894	target->ia_flags &= ~IFA_ROUTE;
895	return 0;
896}
897
898#undef rtinitflags
899
900/*
901 * Return 1 if the address might be a local broadcast address.
902 */
903int
904in_broadcast(in, ifp)
905	struct in_addr in;
906	struct ifnet *ifp;
907{
908	register struct ifaddr *ifa;
909	u_long t;
910
911	if (in.s_addr == INADDR_BROADCAST ||
912	    in.s_addr == INADDR_ANY)
913		return 1;
914	if ((ifp->if_flags & IFF_BROADCAST) == 0)
915		return 0;
916	t = ntohl(in.s_addr);
917	/*
918	 * Look through the list of addresses for a match
919	 * with a broadcast address.
920	 */
921#define ia ((struct in_ifaddr *)ifa)
922	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
923		if (ifa->ifa_addr->sa_family == AF_INET &&
924		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
925		     in.s_addr == ia->ia_netbroadcast.s_addr ||
926		     /*
927		      * Check for old-style (host 0) broadcast.
928		      */
929		     t == ia->ia_subnet || t == ia->ia_net) &&
930		     /*
931		      * Check for an all one subnetmask. These
932		      * only exist when an interface gets a secondary
933		      * address.
934		      */
935		     ia->ia_subnetmask != (u_long)0xffffffff)
936			    return 1;
937	return (0);
938#undef ia
939}
940/*
941 * Add an address to the list of IP multicast addresses for a given interface.
942 */
943struct in_multi *
944in_addmulti(ap, ifp)
945	register struct in_addr *ap;
946	register struct ifnet *ifp;
947{
948	register struct in_multi *inm;
949	int error;
950	struct sockaddr_in sin;
951	struct ifmultiaddr *ifma;
952	int s = splnet();
953
954	/*
955	 * Call generic routine to add membership or increment
956	 * refcount.  It wants addresses in the form of a sockaddr,
957	 * so we build one here (being careful to zero the unused bytes).
958	 */
959	bzero(&sin, sizeof sin);
960	sin.sin_family = AF_INET;
961	sin.sin_len = sizeof sin;
962	sin.sin_addr = *ap;
963	error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
964	if (error) {
965		splx(s);
966		return 0;
967	}
968
969	/*
970	 * If ifma->ifma_protospec is null, then if_addmulti() created
971	 * a new record.  Otherwise, we are done.
972	 */
973	if (ifma->ifma_protospec != 0) {
974		splx(s);
975		return ifma->ifma_protospec;
976	}
977
978	/* XXX - if_addmulti uses M_WAITOK.  Can this really be called
979	   at interrupt time?  If so, need to fix if_addmulti. XXX */
980	inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR,
981	    M_NOWAIT | M_ZERO);
982	if (inm == NULL) {
983		splx(s);
984		return (NULL);
985	}
986
987	inm->inm_addr = *ap;
988	inm->inm_ifp = ifp;
989	inm->inm_ifma = ifma;
990	ifma->ifma_protospec = inm;
991	LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
992
993	/*
994	 * Let IGMP know that we have joined a new IP multicast group.
995	 */
996	igmp_joingroup(inm);
997	splx(s);
998	return (inm);
999}
1000
1001/*
1002 * Delete a multicast address record.
1003 */
1004void
1005in_delmulti(inm)
1006	register struct in_multi *inm;
1007{
1008	struct ifmultiaddr *ifma = inm->inm_ifma;
1009	struct in_multi my_inm;
1010	int s = splnet();
1011
1012	my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1013	if (ifma->ifma_refcount == 1) {
1014		/*
1015		 * No remaining claims to this record; let IGMP know that
1016		 * we are leaving the multicast group.
1017		 * But do it after the if_delmulti() which might reset
1018		 * the interface and nuke the packet.
1019		 */
1020		my_inm = *inm ;
1021		ifma->ifma_protospec = 0;
1022		LIST_REMOVE(inm, inm_link);
1023		free(inm, M_IPMADDR);
1024	}
1025	/* XXX - should be separate API for when we have an ifma? */
1026	if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1027	if (my_inm.inm_ifp != NULL)
1028		igmp_leavegroup(&my_inm);
1029	splx(s);
1030}
1031