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