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