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