in.c revision 46112
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 *	$Id: in.c,v 1.40 1999/04/24 12:28:51 luigi Exp $
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/proc.h>
42#include <sys/socket.h>
43#include <sys/kernel.h>
44#include <sys/sysctl.h>
45
46#include <net/if.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
54static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
55
56static void	in_socktrim __P((struct sockaddr_in *));
57static int	in_ifinit __P((struct ifnet *,
58	    struct in_ifaddr *, struct sockaddr_in *, int));
59
60static int subnetsarelocal = 0;
61SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
62	&subnetsarelocal, 0, "");
63
64struct in_multihead in_multihead; /* XXX BSS initialization */
65
66/*
67 * Return 1 if an internet address is for a ``local'' host
68 * (one to which we have a connection).  If subnetsarelocal
69 * is true, this includes other subnets of the local net.
70 * Otherwise, it includes only the directly-connected (sub)nets.
71 */
72int
73in_localaddr(in)
74	struct in_addr in;
75{
76	register u_long i = ntohl(in.s_addr);
77	register struct in_ifaddr *ia;
78
79	if (subnetsarelocal) {
80		for (ia = in_ifaddrhead.tqh_first; ia;
81		     ia = ia->ia_link.tqe_next)
82			if ((i & ia->ia_netmask) == ia->ia_net)
83				return (1);
84	} else {
85		for (ia = in_ifaddrhead.tqh_first; ia;
86		     ia = ia->ia_link.tqe_next)
87			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
88				return (1);
89	}
90	return (0);
91}
92
93/*
94 * Determine whether an IP address is in a reserved set of addresses
95 * that may not be forwarded, or whether datagrams to that destination
96 * may be forwarded.
97 */
98int
99in_canforward(in)
100	struct in_addr in;
101{
102	register u_long i = ntohl(in.s_addr);
103	register u_long net;
104
105	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
106		return (0);
107	if (IN_CLASSA(i)) {
108		net = i & IN_CLASSA_NET;
109		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
110			return (0);
111	}
112	return (1);
113}
114
115/*
116 * Trim a mask in a sockaddr
117 */
118static void
119in_socktrim(ap)
120struct sockaddr_in *ap;
121{
122    register char *cplim = (char *) &ap->sin_addr;
123    register char *cp = (char *) (&ap->sin_addr + 1);
124
125    ap->sin_len = 0;
126    while (--cp >= cplim)
127        if (*cp) {
128	    (ap)->sin_len = cp - (char *) (ap) + 1;
129	    break;
130	}
131}
132
133static int in_interfaces;	/* number of external internet interfaces */
134
135/*
136 * Generic internet control operations (ioctl's).
137 * Ifp is 0 if not an interface-specific ioctl.
138 */
139/* ARGSUSED */
140int
141in_control(so, cmd, data, ifp, p)
142	struct socket *so;
143	u_long cmd;
144	caddr_t data;
145	register struct ifnet *ifp;
146	struct proc *p;
147{
148	register struct ifreq *ifr = (struct ifreq *)data;
149	register struct in_ifaddr *ia = 0, *iap;
150	register struct ifaddr *ifa;
151	struct in_ifaddr *oia;
152	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
153	struct sockaddr_in oldaddr;
154	int error, hostIsNew, maskIsNew, s;
155	u_long i;
156
157	/*
158	 * Find address for this interface, if it exists.
159	 *
160	 * If an alias address was specified, find that one instead of
161	 * the first one on the interface.
162	 */
163	if (ifp)
164		for (iap = in_ifaddrhead.tqh_first; iap;
165		     iap = iap->ia_link.tqe_next)
166			if (iap->ia_ifp == ifp) {
167				if (((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr ==
168				    iap->ia_addr.sin_addr.s_addr) {
169					ia = iap;
170					break;
171				} else if (ia == NULL) {
172					ia = iap;
173					if (ifr->ifr_addr.sa_family != AF_INET)
174						break;
175				}
176			}
177
178	switch (cmd) {
179
180	case SIOCAIFADDR:
181	case SIOCDIFADDR:
182		if (ifp == 0)
183			return (EADDRNOTAVAIL);
184		if (ifra->ifra_addr.sin_family == AF_INET) {
185			for (oia = ia; ia; ia = ia->ia_link.tqe_next) {
186				if (ia->ia_ifp == ifp  &&
187				    ia->ia_addr.sin_addr.s_addr ==
188				    ifra->ifra_addr.sin_addr.s_addr)
189					break;
190			}
191			if ((ifp->if_flags & IFF_POINTOPOINT)
192			    && (cmd == SIOCAIFADDR)
193			    && (ifra->ifra_dstaddr.sin_addr.s_addr
194				== INADDR_ANY)) {
195				return EDESTADDRREQ;
196			}
197		}
198		if (cmd == SIOCDIFADDR && ia == 0)
199			return (EADDRNOTAVAIL);
200		/* FALLTHROUGH */
201	case SIOCSIFADDR:
202	case SIOCSIFNETMASK:
203	case SIOCSIFDSTADDR:
204		if (p && (error = suser(p)) != 0)
205			return error;
206
207		if (ifp == 0)
208			return (EADDRNOTAVAIL);
209		if (ia == (struct in_ifaddr *)0) {
210			ia = (struct in_ifaddr *)
211				malloc(sizeof *ia, M_IFADDR, M_WAITOK);
212			if (ia == (struct in_ifaddr *)NULL)
213				return (ENOBUFS);
214			bzero((caddr_t)ia, sizeof *ia);
215			/*
216			 * Protect from ipintr() traversing address list
217			 * while we're modifying it.
218			 */
219			s = splnet();
220
221			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
222			ifa = &ia->ia_ifa;
223			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
224
225			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
226			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
227			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
228			ia->ia_sockmask.sin_len = 8;
229			if (ifp->if_flags & IFF_BROADCAST) {
230				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
231				ia->ia_broadaddr.sin_family = AF_INET;
232			}
233			ia->ia_ifp = ifp;
234			if (!(ifp->if_flags & IFF_LOOPBACK))
235				in_interfaces++;
236			splx(s);
237		}
238		break;
239
240	case SIOCSIFBRDADDR:
241		if (p && (error = suser(p)) != 0)
242			return error;
243		/* FALLTHROUGH */
244
245	case SIOCGIFADDR:
246	case SIOCGIFNETMASK:
247	case SIOCGIFDSTADDR:
248	case SIOCGIFBRDADDR:
249		if (ia == (struct in_ifaddr *)0)
250			return (EADDRNOTAVAIL);
251		break;
252	}
253	switch (cmd) {
254
255	case SIOCGIFADDR:
256		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
257		break;
258
259	case SIOCGIFBRDADDR:
260		if ((ifp->if_flags & IFF_BROADCAST) == 0)
261			return (EINVAL);
262		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
263		break;
264
265	case SIOCGIFDSTADDR:
266		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
267			return (EINVAL);
268		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
269		break;
270
271	case SIOCGIFNETMASK:
272		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
273		break;
274
275	case SIOCSIFDSTADDR:
276		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
277			return (EINVAL);
278		oldaddr = ia->ia_dstaddr;
279		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
280		if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
281					(ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
282			ia->ia_dstaddr = oldaddr;
283			return (error);
284		}
285		if (ia->ia_flags & IFA_ROUTE) {
286			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
287			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
288			ia->ia_ifa.ifa_dstaddr =
289					(struct sockaddr *)&ia->ia_dstaddr;
290			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
291		}
292		break;
293
294	case SIOCSIFBRDADDR:
295		if ((ifp->if_flags & IFF_BROADCAST) == 0)
296			return (EINVAL);
297		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
298		break;
299
300	case SIOCSIFADDR:
301		return (in_ifinit(ifp, ia,
302		    (struct sockaddr_in *) &ifr->ifr_addr, 1));
303
304	case SIOCSIFNETMASK:
305		i = ifra->ifra_addr.sin_addr.s_addr;
306		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i);
307		break;
308
309	case SIOCAIFADDR:
310		maskIsNew = 0;
311		hostIsNew = 1;
312		error = 0;
313		if (ia->ia_addr.sin_family == AF_INET) {
314			if (ifra->ifra_addr.sin_len == 0) {
315				ifra->ifra_addr = ia->ia_addr;
316				hostIsNew = 0;
317			} else if (ifra->ifra_addr.sin_addr.s_addr ==
318					       ia->ia_addr.sin_addr.s_addr)
319				hostIsNew = 0;
320		}
321		if (ifra->ifra_mask.sin_len) {
322			in_ifscrub(ifp, ia);
323			ia->ia_sockmask = ifra->ifra_mask;
324			ia->ia_subnetmask =
325			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
326			maskIsNew = 1;
327		}
328		if ((ifp->if_flags & IFF_POINTOPOINT) &&
329		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
330			in_ifscrub(ifp, ia);
331			ia->ia_dstaddr = ifra->ifra_dstaddr;
332			maskIsNew  = 1; /* We lie; but the effect's the same */
333		}
334		if (ifra->ifra_addr.sin_family == AF_INET &&
335		    (hostIsNew || maskIsNew))
336			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
337		if ((ifp->if_flags & IFF_BROADCAST) &&
338		    (ifra->ifra_broadaddr.sin_family == AF_INET))
339			ia->ia_broadaddr = ifra->ifra_broadaddr;
340		return (error);
341
342	case SIOCDIFADDR:
343		in_ifscrub(ifp, ia);
344		/*
345		 * Protect from ipintr() traversing address list
346		 * while we're modifying it.
347		 */
348		s = splnet();
349
350		ifa = &ia->ia_ifa;
351		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
352		oia = ia;
353		TAILQ_REMOVE(&in_ifaddrhead, oia, ia_link);
354		IFAFREE(&oia->ia_ifa);
355		splx(s);
356		break;
357
358	default:
359		if (ifp == 0 || ifp->if_ioctl == 0)
360			return (EOPNOTSUPP);
361		return ((*ifp->if_ioctl)(ifp, cmd, data));
362	}
363	return (0);
364}
365
366/*
367 * Delete any existing route for an interface.
368 */
369void
370in_ifscrub(ifp, ia)
371	register struct ifnet *ifp;
372	register struct in_ifaddr *ia;
373{
374
375	if ((ia->ia_flags & IFA_ROUTE) == 0)
376		return;
377	if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
378		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
379	else
380		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
381	ia->ia_flags &= ~IFA_ROUTE;
382}
383
384/*
385 * Initialize an interface's internet address
386 * and routing table entry.
387 */
388static int
389in_ifinit(ifp, ia, sin, scrub)
390	register struct ifnet *ifp;
391	register struct in_ifaddr *ia;
392	struct sockaddr_in *sin;
393	int scrub;
394{
395	register u_long i = ntohl(sin->sin_addr.s_addr);
396	struct sockaddr_in oldaddr;
397	int s = splimp(), flags = RTF_UP, error;
398
399	oldaddr = ia->ia_addr;
400	ia->ia_addr = *sin;
401	/*
402	 * Give the interface a chance to initialize
403	 * if this is its first address,
404	 * and to validate the address if necessary.
405	 */
406	if (ifp->if_ioctl &&
407	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
408		splx(s);
409		ia->ia_addr = oldaddr;
410		return (error);
411	}
412	splx(s);
413	if (scrub) {
414		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
415		in_ifscrub(ifp, ia);
416		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
417	}
418	if (IN_CLASSA(i))
419		ia->ia_netmask = IN_CLASSA_NET;
420	else if (IN_CLASSB(i))
421		ia->ia_netmask = IN_CLASSB_NET;
422	else
423		ia->ia_netmask = IN_CLASSC_NET;
424	/*
425	 * The subnet mask usually includes at least the standard network part,
426	 * but may may be smaller in the case of supernetting.
427	 * If it is set, we believe it.
428	 */
429	if (ia->ia_subnetmask == 0) {
430		ia->ia_subnetmask = ia->ia_netmask;
431		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
432	} else
433		ia->ia_netmask &= ia->ia_subnetmask;
434	ia->ia_net = i & ia->ia_netmask;
435	ia->ia_subnet = i & ia->ia_subnetmask;
436	in_socktrim(&ia->ia_sockmask);
437	/*
438	 * Add route for the network.
439	 */
440	ia->ia_ifa.ifa_metric = ifp->if_metric;
441	if (ifp->if_flags & IFF_BROADCAST) {
442		ia->ia_broadaddr.sin_addr.s_addr =
443			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
444		ia->ia_netbroadcast.s_addr =
445			htonl(ia->ia_net | ~ ia->ia_netmask);
446	} else if (ifp->if_flags & IFF_LOOPBACK) {
447		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
448		flags |= RTF_HOST;
449	} else if (ifp->if_flags & IFF_POINTOPOINT) {
450		if (ia->ia_dstaddr.sin_family != AF_INET)
451			return (0);
452		flags |= RTF_HOST;
453	}
454	if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
455		ia->ia_flags |= IFA_ROUTE;
456
457	/*
458	 * If the interface supports multicast, join the "all hosts"
459	 * multicast group on that interface.
460	 */
461	if (ifp->if_flags & IFF_MULTICAST) {
462		struct in_addr addr;
463
464		addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
465		in_addmulti(&addr, ifp);
466	}
467	return (error);
468}
469
470
471/*
472 * Return 1 if the address might be a local broadcast address.
473 */
474int
475in_broadcast(in, ifp)
476	struct in_addr in;
477        struct ifnet *ifp;
478{
479	register struct ifaddr *ifa;
480	u_long t;
481
482	if (in.s_addr == INADDR_BROADCAST ||
483	    in.s_addr == INADDR_ANY)
484		return 1;
485	if ((ifp->if_flags & IFF_BROADCAST) == 0)
486		return 0;
487	t = ntohl(in.s_addr);
488	/*
489	 * Look through the list of addresses for a match
490	 * with a broadcast address.
491	 */
492#define ia ((struct in_ifaddr *)ifa)
493	for (ifa = ifp->if_addrhead.tqh_first; ifa;
494	     ifa = ifa->ifa_link.tqe_next)
495		if (ifa->ifa_addr->sa_family == AF_INET &&
496		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
497		     in.s_addr == ia->ia_netbroadcast.s_addr ||
498		     /*
499		      * Check for old-style (host 0) broadcast.
500		      */
501		     t == ia->ia_subnet || t == ia->ia_net) &&
502		     /*
503		      * Check for an all one subnetmask. These
504		      * only exist when an interface gets a secondary
505		      * address.
506		      */
507		     ia->ia_subnetmask != (u_long)0xffffffff)
508			    return 1;
509	return (0);
510#undef ia
511}
512/*
513 * Add an address to the list of IP multicast addresses for a given interface.
514 */
515struct in_multi *
516in_addmulti(ap, ifp)
517	register struct in_addr *ap;
518	register struct ifnet *ifp;
519{
520	register struct in_multi *inm;
521	int error;
522	struct sockaddr_in sin;
523	struct ifmultiaddr *ifma;
524	int s = splnet();
525
526	/*
527	 * Call generic routine to add membership or increment
528	 * refcount.  It wants addresses in the form of a sockaddr,
529	 * so we build one here (being careful to zero the unused bytes).
530	 */
531	bzero(&sin, sizeof sin);
532	sin.sin_family = AF_INET;
533	sin.sin_len = sizeof sin;
534	sin.sin_addr = *ap;
535	error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
536	if (error) {
537		splx(s);
538		return 0;
539	}
540
541	/*
542	 * If ifma->ifma_protospec is null, then if_addmulti() created
543	 * a new record.  Otherwise, we are done.
544	 */
545	if (ifma->ifma_protospec != 0)
546		return ifma->ifma_protospec;
547
548	/* XXX - if_addmulti uses M_WAITOK.  Can this really be called
549	   at interrupt time?  If so, need to fix if_addmulti. XXX */
550	inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT);
551	if (inm == NULL) {
552		splx(s);
553		return (NULL);
554	}
555
556	bzero(inm, sizeof *inm);
557	inm->inm_addr = *ap;
558	inm->inm_ifp = ifp;
559	inm->inm_ifma = ifma;
560	ifma->ifma_protospec = inm;
561	LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
562
563	/*
564	 * Let IGMP know that we have joined a new IP multicast group.
565	 */
566	igmp_joingroup(inm);
567	splx(s);
568	return (inm);
569}
570
571/*
572 * Delete a multicast address record.
573 */
574void
575in_delmulti(inm)
576	register struct in_multi *inm;
577{
578	struct ifmultiaddr *ifma = inm->inm_ifma;
579	struct in_multi my_inm;
580	int s = splnet();
581
582	my_inm.inm_ifp = NULL ; /* don't send the leave msg */
583	if (ifma->ifma_refcount == 1) {
584		/*
585		 * No remaining claims to this record; let IGMP know that
586		 * we are leaving the multicast group.
587		 * But do it after the if_delmulti() which might reset
588		 * the interface and nuke the packet.
589		 */
590		my_inm = *inm ;
591		ifma->ifma_protospec = 0;
592		LIST_REMOVE(inm, inm_link);
593		free(inm, M_IPMADDR);
594	}
595	/* XXX - should be separate API for when we have an ifma? */
596	if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
597	if (my_inm.inm_ifp != NULL)
598		igmp_leavegroup(&my_inm);
599	splx(s);
600}
601