xref: /netbsd-src/sys/netinet/in.c (revision d0fed6c87ddc40a8bffa6f99e7433ddfc864dd83)
1 /*	$NetBSD: in.c,v 1.34 1997/03/15 18:12:36 is Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)in.c	8.2 (Berkeley) 11/15/93
36  */
37 
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/errno.h>
41 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 
47 #include <net/if.h>
48 #include <net/route.h>
49 
50 #include <net/if_ether.h>
51 
52 #include <netinet/in_systm.h>
53 #include <netinet/in.h>
54 #include <netinet/in_var.h>
55 #include <netinet/if_inarp.h>
56 #include <netinet/ip_mroute.h>
57 #include <netinet/igmp_var.h>
58 
59 #include "ether.h"
60 
61 #ifdef INET
62 
63 #ifndef SUBNETSARELOCAL
64 #define	SUBNETSARELOCAL	1
65 #endif
66 int subnetsarelocal = SUBNETSARELOCAL;
67 
68 /*
69  * Return 1 if an internet address is for a ``local'' host
70  * (one to which we have a connection).  If subnetsarelocal
71  * is true, this includes other subnets of the local net.
72  * Otherwise, it includes only the directly-connected (sub)nets.
73  */
74 int
75 in_localaddr(in)
76 	struct in_addr in;
77 {
78 	register struct in_ifaddr *ia;
79 
80 	if (subnetsarelocal) {
81 		for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
82 			if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
83 				return (1);
84 	} else {
85 		for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
86 			if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
87 				return (1);
88 	}
89 	return (0);
90 }
91 
92 /*
93  * Determine whether an IP address is in a reserved set of addresses
94  * that may not be forwarded, or whether datagrams to that destination
95  * may be forwarded.
96  */
97 int
98 in_canforward(in)
99 	struct in_addr in;
100 {
101 	register u_int32_t net;
102 
103 	if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
104 		return (0);
105 	if (IN_CLASSA(in.s_addr)) {
106 		net = in.s_addr & IN_CLASSA_NET;
107 		if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
108 			return (0);
109 	}
110 	return (1);
111 }
112 
113 /*
114  * Trim a mask in a sockaddr
115  */
116 void
117 in_socktrim(ap)
118 	struct sockaddr_in *ap;
119 {
120 	register char *cplim = (char *) &ap->sin_addr;
121 	register char *cp = (char *) (&ap->sin_addr + 1);
122 
123 	ap->sin_len = 0;
124 	while (--cp >= cplim)
125 		if (*cp) {
126 			(ap)->sin_len = cp - (char *) (ap) + 1;
127 			break;
128 		}
129 }
130 
131 int	in_interfaces;		/* number of external internet interfaces */
132 
133 /*
134  * Generic internet control operations (ioctl's).
135  * Ifp is 0 if not an interface-specific ioctl.
136  */
137 /* ARGSUSED */
138 int
139 in_control(so, cmd, data, ifp, p)
140 	struct socket *so;
141 	u_long cmd;
142 	caddr_t data;
143 	register struct ifnet *ifp;
144 	struct proc *p;
145 {
146 	register struct ifreq *ifr = (struct ifreq *)data;
147 	register struct in_ifaddr *ia = 0;
148 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
149 	struct sockaddr_in oldaddr;
150 	int error, hostIsNew, maskIsNew;
151 
152 	/*
153 	 * Find address for this interface, if it exists.
154 	 */
155 	if (ifp)
156 		for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
157 			if (ia->ia_ifp == ifp)
158 				break;
159 
160 	switch (cmd) {
161 
162 	case SIOCAIFADDR:
163 	case SIOCDIFADDR:
164 		if (ifra->ifra_addr.sin_family == AF_INET)
165 			for (; ia != 0; ia = ia->ia_list.tqe_next) {
166 				if (ia->ia_ifp == ifp  &&
167 				    in_hosteq(ia->ia_addr.sin_addr, ifra->ifra_addr.sin_addr))
168 					break;
169 			}
170 		if (cmd == SIOCDIFADDR && ia == 0)
171 			return (EADDRNOTAVAIL);
172 		/* FALLTHROUGH */
173 	case SIOCSIFADDR:
174 	case SIOCSIFNETMASK:
175 	case SIOCSIFDSTADDR:
176 		if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
177 			return (EPERM);
178 
179 		if (ifp == 0)
180 			panic("in_control");
181 		if (ia == 0) {
182 			MALLOC(ia, struct in_ifaddr *, sizeof(*ia),
183 			       M_IFADDR, M_WAITOK);
184 			if (ia == 0)
185 				return (ENOBUFS);
186 			bzero((caddr_t)ia, sizeof *ia);
187 			TAILQ_INSERT_TAIL(&in_ifaddr, ia, ia_list);
188 			TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia,
189 			    ifa_list);
190 			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
191 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
192 			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
193 			ia->ia_sockmask.sin_len = 8;
194 			if (ifp->if_flags & IFF_BROADCAST) {
195 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
196 				ia->ia_broadaddr.sin_family = AF_INET;
197 			}
198 			ia->ia_ifp = ifp;
199 			LIST_INIT(&ia->ia_multiaddrs);
200 			if ((ifp->if_flags & IFF_LOOPBACK) == 0)
201 				in_interfaces++;
202 		}
203 		break;
204 
205 	case SIOCSIFBRDADDR:
206 		if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
207 			return (EPERM);
208 		/* FALLTHROUGH */
209 
210 	case SIOCGIFADDR:
211 	case SIOCGIFNETMASK:
212 	case SIOCGIFDSTADDR:
213 	case SIOCGIFBRDADDR:
214 		if (ia == 0)
215 			return (EADDRNOTAVAIL);
216 		break;
217 	}
218 	switch (cmd) {
219 
220 	case SIOCGIFADDR:
221 		*satosin(&ifr->ifr_addr) = ia->ia_addr;
222 		break;
223 
224 	case SIOCGIFBRDADDR:
225 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
226 			return (EINVAL);
227 		*satosin(&ifr->ifr_dstaddr) = ia->ia_broadaddr;
228 		break;
229 
230 	case SIOCGIFDSTADDR:
231 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
232 			return (EINVAL);
233 		*satosin(&ifr->ifr_dstaddr) = ia->ia_dstaddr;
234 		break;
235 
236 	case SIOCGIFNETMASK:
237 		*satosin(&ifr->ifr_addr) = ia->ia_sockmask;
238 		break;
239 
240 	case SIOCSIFDSTADDR:
241 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
242 			return (EINVAL);
243 		oldaddr = ia->ia_dstaddr;
244 		ia->ia_dstaddr = *satosin(&ifr->ifr_dstaddr);
245 		if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
246 					(ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
247 			ia->ia_dstaddr = oldaddr;
248 			return (error);
249 		}
250 		if (ia->ia_flags & IFA_ROUTE) {
251 			ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
252 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
253 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
254 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
255 		}
256 		break;
257 
258 	case SIOCSIFBRDADDR:
259 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
260 			return (EINVAL);
261 		ia->ia_broadaddr = *satosin(&ifr->ifr_broadaddr);
262 		break;
263 
264 	case SIOCSIFADDR:
265 		return (in_ifinit(ifp, ia, satosin(&ifr->ifr_addr), 1));
266 
267 	case SIOCSIFNETMASK:
268 		ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr =
269 		    ifra->ifra_addr.sin_addr.s_addr;
270 		break;
271 
272 	case SIOCAIFADDR:
273 		maskIsNew = 0;
274 		hostIsNew = 1;
275 		error = 0;
276 		if (ia->ia_addr.sin_family == AF_INET) {
277 			if (ifra->ifra_addr.sin_len == 0) {
278 				ifra->ifra_addr = ia->ia_addr;
279 				hostIsNew = 0;
280 			} else if (in_hosteq(ia->ia_addr.sin_addr, ifra->ifra_addr.sin_addr))
281 				hostIsNew = 0;
282 		}
283 		if (ifra->ifra_mask.sin_len) {
284 			in_ifscrub(ifp, ia);
285 			ia->ia_sockmask = ifra->ifra_mask;
286 			ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
287 			maskIsNew = 1;
288 		}
289 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
290 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
291 			in_ifscrub(ifp, ia);
292 			ia->ia_dstaddr = ifra->ifra_dstaddr;
293 			maskIsNew  = 1; /* We lie; but the effect's the same */
294 		}
295 		if (ifra->ifra_addr.sin_family == AF_INET &&
296 		    (hostIsNew || maskIsNew))
297 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
298 		if ((ifp->if_flags & IFF_BROADCAST) &&
299 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
300 			ia->ia_broadaddr = ifra->ifra_broadaddr;
301 		return (error);
302 
303 	case SIOCDIFADDR:
304 		in_ifscrub(ifp, ia);
305 		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
306 		TAILQ_REMOVE(&in_ifaddr, ia, ia_list);
307 		IFAFREE((&ia->ia_ifa));
308 		break;
309 
310 #ifdef MROUTING
311 	case SIOCGETVIFCNT:
312 	case SIOCGETSGCNT:
313 		return (mrt_ioctl(so, cmd, data));
314 #endif /* MROUTING */
315 
316 	default:
317 		if (ifp == 0 || ifp->if_ioctl == 0)
318 			return (EOPNOTSUPP);
319 		return ((*ifp->if_ioctl)(ifp, cmd, data));
320 	}
321 	return (0);
322 }
323 
324 /*
325  * Delete any existing route for an interface.
326  */
327 void
328 in_ifscrub(ifp, ia)
329 	register struct ifnet *ifp;
330 	register struct in_ifaddr *ia;
331 {
332 
333 	if ((ia->ia_flags & IFA_ROUTE) == 0)
334 		return;
335 	if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
336 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
337 	else
338 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
339 	ia->ia_flags &= ~IFA_ROUTE;
340 }
341 
342 /*
343  * Initialize an interface's internet address
344  * and routing table entry.
345  */
346 int
347 in_ifinit(ifp, ia, sin, scrub)
348 	register struct ifnet *ifp;
349 	register struct in_ifaddr *ia;
350 	struct sockaddr_in *sin;
351 	int scrub;
352 {
353 	register u_int32_t i = sin->sin_addr.s_addr;
354 	struct sockaddr_in oldaddr;
355 	int s = splimp(), flags = RTF_UP, error;
356 
357 	/*
358 	 * Set up new addresses.
359 	 */
360 	oldaddr = ia->ia_addr;
361 	ia->ia_addr = *sin;
362 	/*
363 	 * Give the interface a chance to initialize
364 	 * if this is its first address,
365 	 * and to validate the address if necessary.
366 	 */
367 	if (ifp->if_ioctl &&
368 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)))
369 		goto bad;
370 	splx(s);
371 	if (scrub) {
372 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
373 		in_ifscrub(ifp, ia);
374 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
375 	}
376 	if (IN_CLASSA(i))
377 		ia->ia_netmask = IN_CLASSA_NET;
378 	else if (IN_CLASSB(i))
379 		ia->ia_netmask = IN_CLASSB_NET;
380 	else
381 		ia->ia_netmask = IN_CLASSC_NET;
382 	/*
383 	 * The subnet mask usually includes at least the standard network part,
384 	 * but may may be smaller in the case of supernetting.
385 	 * If it is set, we believe it.
386 	 */
387 	if (ia->ia_subnetmask == 0) {
388 		ia->ia_subnetmask = ia->ia_netmask;
389 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
390 	} else
391 		ia->ia_netmask &= ia->ia_subnetmask;
392 	ia->ia_net = i & ia->ia_netmask;
393 	ia->ia_subnet = i & ia->ia_subnetmask;
394 	in_socktrim(&ia->ia_sockmask);
395 	/*
396 	 * Add route for the network.
397 	 */
398 	ia->ia_ifa.ifa_metric = ifp->if_metric;
399 	if (ifp->if_flags & IFF_BROADCAST) {
400 		ia->ia_broadaddr.sin_addr.s_addr =
401 			ia->ia_subnet | ~ia->ia_subnetmask;
402 		ia->ia_netbroadcast.s_addr =
403 			ia->ia_net | ~ia->ia_netmask;
404 	} else if (ifp->if_flags & IFF_LOOPBACK) {
405 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
406 		flags |= RTF_HOST;
407 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
408 		if (ia->ia_dstaddr.sin_family != AF_INET)
409 			return (0);
410 		flags |= RTF_HOST;
411 	}
412 	error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags);
413 	if (!error)
414 		ia->ia_flags |= IFA_ROUTE;
415 	/*
416 	 * If the interface supports multicast, join the "all hosts"
417 	 * multicast group on that interface.
418 	 */
419 	if (ifp->if_flags & IFF_MULTICAST) {
420 		struct in_addr addr;
421 
422 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
423 		in_addmulti(&addr, ifp);
424 	}
425 	return (error);
426 bad:
427 	splx(s);
428 	ia->ia_addr = oldaddr;
429 	return (error);
430 }
431 
432 /*
433  * Return 1 if the address might be a local broadcast address.
434  */
435 int
436 in_broadcast(in, ifp)
437 	struct in_addr in;
438 	struct ifnet *ifp;
439 {
440 	register struct ifaddr *ifa;
441 
442 	if (in.s_addr == INADDR_BROADCAST ||
443 	    in_nullhost(in))
444 		return 1;
445 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
446 		return 0;
447 	/*
448 	 * Look through the list of addresses for a match
449 	 * with a broadcast address.
450 	 */
451 #define ia (ifatoia(ifa))
452 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
453 		if (ifa->ifa_addr->sa_family == AF_INET &&
454 		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
455 		     in_hosteq(in, ia->ia_netbroadcast) ||
456 		     /*
457 		      * Check for old-style (host 0) broadcast.
458 		      */
459 		     in.s_addr == ia->ia_subnet ||
460 		     in.s_addr == ia->ia_net))
461 			    return 1;
462 	return (0);
463 #undef ia
464 }
465 
466 /*
467  * Add an address to the list of IP multicast addresses for a given interface.
468  */
469 struct in_multi *
470 in_addmulti(ap, ifp)
471 	register struct in_addr *ap;
472 	register struct ifnet *ifp;
473 {
474 	register struct in_multi *inm;
475 	struct ifreq ifr;
476 	struct in_ifaddr *ia;
477 	int s = splsoftnet();
478 
479 	/*
480 	 * See if address already in list.
481 	 */
482 	IN_LOOKUP_MULTI(*ap, ifp, inm);
483 	if (inm != NULL) {
484 		/*
485 		 * Found it; just increment the reference count.
486 		 */
487 		++inm->inm_refcount;
488 	} else {
489 		/*
490 		 * New address; allocate a new multicast record
491 		 * and link it into the interface's multicast list.
492 		 */
493 		inm = (struct in_multi *)malloc(sizeof(*inm),
494 		    M_IPMADDR, M_NOWAIT);
495 		if (inm == NULL) {
496 			splx(s);
497 			return (NULL);
498 		}
499 		inm->inm_addr = *ap;
500 		inm->inm_ifp = ifp;
501 		inm->inm_refcount = 1;
502 		IFP_TO_IA(ifp, ia);
503 		if (ia == NULL) {
504 			free(inm, M_IPMADDR);
505 			splx(s);
506 			return (NULL);
507 		}
508 		inm->inm_ia = ia;
509 		LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_list);
510 		/*
511 		 * Ask the network driver to update its multicast reception
512 		 * filter appropriately for the new address.
513 		 */
514 		satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
515 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
516 		satosin(&ifr.ifr_addr)->sin_addr = *ap;
517 		if ((ifp->if_ioctl == NULL) ||
518 		    (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
519 			LIST_REMOVE(inm, inm_list);
520 			free(inm, M_IPMADDR);
521 			splx(s);
522 			return (NULL);
523 		}
524 		/*
525 		 * Let IGMP know that we have joined a new IP multicast group.
526 		 */
527 		igmp_joingroup(inm);
528 	}
529 	splx(s);
530 	return (inm);
531 }
532 
533 /*
534  * Delete a multicast address record.
535  */
536 void
537 in_delmulti(inm)
538 	register struct in_multi *inm;
539 {
540 	struct ifreq ifr;
541 	int s = splsoftnet();
542 
543 	if (--inm->inm_refcount == 0) {
544 		/*
545 		 * No remaining claims to this record; let IGMP know that
546 		 * we are leaving the multicast group.
547 		 */
548 		igmp_leavegroup(inm);
549 		/*
550 		 * Unlink from list.
551 		 */
552 		LIST_REMOVE(inm, inm_list);
553 		/*
554 		 * Notify the network driver to update its multicast reception
555 		 * filter.
556 		 */
557 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
558 		satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr;
559 		(*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
560 							     (caddr_t)&ifr);
561 		free(inm, M_IPMADDR);
562 	}
563 	splx(s);
564 }
565 #endif
566