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