in.c revision 21666
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.27 1996/12/15 22:44:00 wollman Exp $
35 */
36
37#include <sys/param.h>
38#include <sys/queue.h>
39#include <sys/systm.h>
40#include <sys/ioctl.h>
41#include <sys/errno.h>
42#include <sys/malloc.h>
43#include <sys/socket.h>
44#include <sys/socketvar.h>
45#include <sys/kernel.h>
46#include <sys/sysctl.h>
47
48#include <net/if.h>
49#include <net/route.h>
50
51#include <netinet/in_systm.h>
52#include <netinet/in.h>
53#include <netinet/in_var.h>
54#include <netinet/if_ether.h>
55
56#include <netinet/igmp_var.h>
57
58static void	in_socktrim __P((struct sockaddr_in *));
59static int	in_ifinit __P((struct ifnet *,
60	    struct in_ifaddr *, struct sockaddr_in *, int));
61static void	in_ifscrub __P((struct ifnet *, struct in_ifaddr *));
62
63static int subnetsarelocal = 0;
64SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
65	&subnetsarelocal, 0, "");
66
67struct in_multihead in_multihead; /* XXX BSS initialization */
68
69/*
70 * Return 1 if an internet address is for a ``local'' host
71 * (one to which we have a connection).  If subnetsarelocal
72 * is true, this includes other subnets of the local net.
73 * Otherwise, it includes only the directly-connected (sub)nets.
74 */
75int
76in_localaddr(in)
77	struct in_addr in;
78{
79	register u_long i = ntohl(in.s_addr);
80	register struct in_ifaddr *ia;
81
82	if (subnetsarelocal) {
83		for (ia = in_ifaddrhead.tqh_first; ia;
84		     ia = ia->ia_link.tqe_next)
85			if ((i & ia->ia_netmask) == ia->ia_net)
86				return (1);
87	} else {
88		for (ia = in_ifaddrhead.tqh_first; ia;
89		     ia = ia->ia_link.tqe_next)
90			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
91				return (1);
92	}
93	return (0);
94}
95
96/*
97 * Determine whether an IP address is in a reserved set of addresses
98 * that may not be forwarded, or whether datagrams to that destination
99 * may be forwarded.
100 */
101int
102in_canforward(in)
103	struct in_addr in;
104{
105	register u_long i = ntohl(in.s_addr);
106	register u_long net;
107
108	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
109		return (0);
110	if (IN_CLASSA(i)) {
111		net = i & IN_CLASSA_NET;
112		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
113			return (0);
114	}
115	return (1);
116}
117
118/*
119 * Trim a mask in a sockaddr
120 */
121static void
122in_socktrim(ap)
123struct sockaddr_in *ap;
124{
125    register char *cplim = (char *) &ap->sin_addr;
126    register char *cp = (char *) (&ap->sin_addr + 1);
127
128    ap->sin_len = 0;
129    while (--cp >= cplim)
130        if (*cp) {
131	    (ap)->sin_len = cp - (char *) (ap) + 1;
132	    break;
133	}
134}
135
136static int in_interfaces;	/* number of external internet interfaces */
137
138/*
139 * Generic internet control operations (ioctl's).
140 * Ifp is 0 if not an interface-specific ioctl.
141 */
142/* ARGSUSED */
143int
144in_control(so, cmd, data, ifp)
145	struct socket *so;
146	u_long cmd;
147	caddr_t data;
148	register struct ifnet *ifp;
149{
150	register struct ifreq *ifr = (struct ifreq *)data;
151	register struct in_ifaddr *ia = 0, *iap;
152	register struct ifaddr *ifa;
153	struct in_ifaddr *oia;
154	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
155	struct sockaddr_in oldaddr;
156	int error, hostIsNew, maskIsNew, s;
157	u_long i;
158
159	/*
160	 * Find address for this interface, if it exists.
161	 *
162	 * If an alias address was specified, find that one instead of
163	 * the first one on the interface.
164	 */
165	if (ifp)
166		for (iap = in_ifaddrhead.tqh_first; iap;
167		     iap = iap->ia_link.tqe_next)
168			if (iap->ia_ifp == ifp) {
169				if (((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr ==
170				    iap->ia_addr.sin_addr.s_addr) {
171					ia = iap;
172					break;
173				} else if (ia == NULL) {
174					ia = iap;
175					if (ifr->ifr_addr.sa_family != AF_INET)
176						break;
177				}
178			}
179
180	switch (cmd) {
181
182	case SIOCAIFADDR:
183	case SIOCDIFADDR:
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 ((so->so_state & SS_PRIV) == 0)
205			return (EPERM);
206
207		if (ifp == 0)
208			panic("in_control");
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 ((so->so_state & SS_PRIV) == 0)
242			return (EPERM);
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 */
369static void
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	int s = splnet();
580
581	if (ifma->ifma_refcount == 1) {
582		/*
583		 * No remaining claims to this record; let IGMP know that
584		 * we are leaving the multicast group.
585		 */
586		igmp_leavegroup(inm);
587		ifma->ifma_protospec = 0;
588		LIST_REMOVE(inm, inm_link);
589		free(inm, M_IPMADDR);
590	}
591	/* XXX - should be separate API for when we have an ifma? */
592	if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
593	splx(s);
594}
595