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