xref: /netbsd-src/usr.sbin/mrouted/inet.c (revision 72867025012398bd4faad4809aedb41baf5dde3f)
1*72867025Schristos /*	$NetBSD: inet.c,v 1.13 2020/09/07 18:37:21 christos Exp $	*/
2c60d41a9Swiz 
3c60d41a9Swiz /*
4c60d41a9Swiz  * The mrouted program is covered by the license in the accompanying file
5c60d41a9Swiz  * named "LICENSE".  Use of the mrouted program represents acceptance of
6c60d41a9Swiz  * the terms and conditions listed in that file.
7c60d41a9Swiz  *
8c60d41a9Swiz  * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
9c60d41a9Swiz  * Leland Stanford Junior University.
10c60d41a9Swiz  */
11c60d41a9Swiz 
12*72867025Schristos #include <assert.h>
13c60d41a9Swiz #include "defs.h"
14c60d41a9Swiz 
15c60d41a9Swiz 
16ee70b5d6Sdsl /* buffers to hold the string representations  */
17ee70b5d6Sdsl /* of IP addresses, returned by inet_fmt{s}() */
18ee70b5d6Sdsl #define SS_MASK	((1 << 3) - 1)
19ee70b5d6Sdsl static char ss[SS_MASK + 1][32];
20ee70b5d6Sdsl static int ss_index = 0;	/* index into above */
21c60d41a9Swiz 
22c60d41a9Swiz 
23c60d41a9Swiz /*
24c60d41a9Swiz  * Verify that a given IP address is credible as a host address.
25c60d41a9Swiz  * (Without a mask, cannot detect addresses of the form {subnet,0} or
26c60d41a9Swiz  * {subnet,-1}.)
27c60d41a9Swiz  */
28c60d41a9Swiz int
inet_valid_host(u_int32_t naddr)29c60d41a9Swiz inet_valid_host(u_int32_t naddr)
30c60d41a9Swiz {
31c60d41a9Swiz     u_int32_t addr;
32c60d41a9Swiz 
33c60d41a9Swiz     addr = ntohl(naddr);
34c60d41a9Swiz 
35c60d41a9Swiz     return (!(IN_MULTICAST(addr) ||
36c60d41a9Swiz 	      IN_BADCLASS (addr) ||
37c60d41a9Swiz 	      (addr & 0xff000000) == 0));
38c60d41a9Swiz }
39c60d41a9Swiz 
40c60d41a9Swiz /*
41c60d41a9Swiz  * Verify that a given netmask is plausible;
42c60d41a9Swiz  * make sure that it is a series of 1's followed by
43c60d41a9Swiz  * a series of 0's with no discontiguous 1's.
44c60d41a9Swiz  */
45c60d41a9Swiz int
inet_valid_mask(u_int32_t mask)46c60d41a9Swiz inet_valid_mask(u_int32_t mask)
47c60d41a9Swiz {
48c60d41a9Swiz     if (~(((mask & -mask) - 1) | mask) != 0) {
49c60d41a9Swiz 	/* Mask is not contiguous */
50c60d41a9Swiz 	return (FALSE);
51c60d41a9Swiz     }
52c60d41a9Swiz 
53c60d41a9Swiz     return (TRUE);
54c60d41a9Swiz }
55c60d41a9Swiz 
56c60d41a9Swiz /*
57c60d41a9Swiz  * Verify that a given subnet number and mask pair are credible.
58c60d41a9Swiz  *
59c60d41a9Swiz  * With CIDR, almost any subnet and mask are credible.  mrouted still
60c60d41a9Swiz  * can't handle aggregated class A's, so we still check that, but
61c60d41a9Swiz  * otherwise the only requirements are that the subnet address is
62c60d41a9Swiz  * within the [ABC] range and that the host bits of the subnet
63c60d41a9Swiz  * are all 0.
64c60d41a9Swiz  */
65c60d41a9Swiz int
inet_valid_subnet(u_int32_t nsubnet,u_int32_t nmask)66c60d41a9Swiz inet_valid_subnet(u_int32_t nsubnet, u_int32_t nmask)
67c60d41a9Swiz {
68c60d41a9Swiz     u_int32_t subnet, mask;
69c60d41a9Swiz 
70c60d41a9Swiz     subnet = ntohl(nsubnet);
71c60d41a9Swiz     mask   = ntohl(nmask);
72c60d41a9Swiz 
73c60d41a9Swiz     if ((subnet & mask) != subnet) return (FALSE);
74c60d41a9Swiz 
75c60d41a9Swiz     if (subnet == 0)
76c60d41a9Swiz 	return (mask == 0);
77c60d41a9Swiz 
78c60d41a9Swiz     if (IN_CLASSA(subnet)) {
79c60d41a9Swiz 	if (mask < 0xff000000 ||
80c60d41a9Swiz 	    (subnet & 0xff000000) == 0x7f000000 ||
81c60d41a9Swiz 	    (subnet & 0xff000000) == 0x00000000) return (FALSE);
82c60d41a9Swiz     }
83c60d41a9Swiz     else if (IN_CLASSD(subnet) || IN_BADCLASS(subnet)) {
84c60d41a9Swiz 	/* Above Class C address space */
85c60d41a9Swiz 	return (FALSE);
86c60d41a9Swiz     }
87c60d41a9Swiz     if (subnet & ~mask) {
88c60d41a9Swiz 	/* Host bits are set in the subnet */
89c60d41a9Swiz 	return (FALSE);
90c60d41a9Swiz     }
91c60d41a9Swiz     if (!inet_valid_mask(mask)) {
92c60d41a9Swiz 	/* Netmask is not contiguous */
93c60d41a9Swiz 	return (FALSE);
94c60d41a9Swiz     }
95c60d41a9Swiz 
96c60d41a9Swiz     return (TRUE);
97c60d41a9Swiz }
98c60d41a9Swiz 
99c60d41a9Swiz 
100c60d41a9Swiz /*
101c60d41a9Swiz  * Convert an IP address in u_long (network) format into a printable string.
102c60d41a9Swiz  */
103c60d41a9Swiz char *
inet_fmt(u_int32_t addr)104ee70b5d6Sdsl inet_fmt(u_int32_t addr)
105c60d41a9Swiz {
106c60d41a9Swiz     u_char *a;
107ee70b5d6Sdsl     char *s = ss[++ss_index & SS_MASK];
108c60d41a9Swiz 
109c60d41a9Swiz     a = (u_char *)&addr;
110ee70b5d6Sdsl     snprintf(s, sizeof ss[0], "%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
111c60d41a9Swiz     return (s);
112c60d41a9Swiz }
113c60d41a9Swiz 
114c60d41a9Swiz 
115c60d41a9Swiz /*
116c60d41a9Swiz  * Convert an IP subnet number in u_long (network) format into a printable
117c60d41a9Swiz  * string including the netmask as a number of bits.
118c60d41a9Swiz  */
119c60d41a9Swiz char *
inet_fmts(u_int32_t addr,u_int32_t mask)120ee70b5d6Sdsl inet_fmts(u_int32_t addr, u_int32_t mask)
121c60d41a9Swiz {
122c60d41a9Swiz     u_char *a, *m;
123c60d41a9Swiz     int bits;
124ee70b5d6Sdsl     char *s = ss[++ss_index & SS_MASK];
125c60d41a9Swiz 
126c60d41a9Swiz     if ((addr == 0) && (mask == 0)) {
127ee70b5d6Sdsl 	snprintf(s, sizeof ss[0], "default");
128c60d41a9Swiz 	return (s);
129c60d41a9Swiz     }
130c60d41a9Swiz     a = (u_char *)&addr;
131c60d41a9Swiz     m = (u_char *)&mask;
132c60d41a9Swiz     bits = 33 - ffs(ntohl(mask));
133c60d41a9Swiz 
134ee70b5d6Sdsl     if      (m[3] != 0) snprintf(s, sizeof ss[0], "%u.%u.%u.%u/%d", a[0], a[1], a[2], a[3],
135c60d41a9Swiz 						bits);
136ee70b5d6Sdsl     else if (m[2] != 0) snprintf(s, sizeof ss[0], "%u.%u.%u/%d",    a[0], a[1], a[2], bits);
137ee70b5d6Sdsl     else if (m[1] != 0) snprintf(s, sizeof ss[0], "%u.%u/%d",       a[0], a[1], bits);
138ee70b5d6Sdsl     else                snprintf(s, sizeof ss[0], "%u/%d",          a[0], bits);
139c60d41a9Swiz 
140c60d41a9Swiz     return (s);
141c60d41a9Swiz }
142c60d41a9Swiz 
143c60d41a9Swiz /*
144c60d41a9Swiz  * Convert the printable string representation of an IP address into the
145c60d41a9Swiz  * u_long (network) format.  Return 0xffffffff on error.  (To detect the
146c60d41a9Swiz  * legal address with that value, you must explicitly compare the string
147c60d41a9Swiz  * with "255.255.255.255".)
148c60d41a9Swiz  */
149c60d41a9Swiz u_int32_t
inet_parse(char * s,int * mask_p)150ee70b5d6Sdsl inet_parse(char *s, int *mask_p)
151c60d41a9Swiz {
152c60d41a9Swiz     u_int32_t a = 0;
153c60d41a9Swiz     u_int a0, a1, a2, a3;
154c60d41a9Swiz     char c;
155ee70b5d6Sdsl     int n;
156c60d41a9Swiz 
15735605a88Sdsl     if (sscanf(s, "%u.%u.%u.%u%n", &a0, &a1, &a2, &a3, &n) != 4)
15835605a88Sdsl 	return 0xffffffff;
159ee70b5d6Sdsl     if (a0 > 255 || a1 > 255 || a2 > 255 || a3 > 255)
16035605a88Sdsl 	return 0xffffffff;
16135605a88Sdsl 
16235605a88Sdsl     if (mask_p == 0) {
16335605a88Sdsl 	if (s[n] != 0)
16435605a88Sdsl 	    return 0xffffffff;
16535605a88Sdsl     } else {
16635605a88Sdsl 	if (sscanf(s + n, "/%u%c", &n, &c) != 1 || n > 32)
16735605a88Sdsl 	    return 0xffffffff;
16835605a88Sdsl 	*mask_p = n;
16935605a88Sdsl     }
170c60d41a9Swiz 
171c60d41a9Swiz     ((u_char *)&a)[0] = a0;
172c60d41a9Swiz     ((u_char *)&a)[1] = a1;
173c60d41a9Swiz     ((u_char *)&a)[2] = a2;
174c60d41a9Swiz     ((u_char *)&a)[3] = a3;
175c60d41a9Swiz 
176c60d41a9Swiz     return (a);
177c60d41a9Swiz }
178c60d41a9Swiz 
179c60d41a9Swiz 
180c60d41a9Swiz /*
181c60d41a9Swiz  * inet_cksum extracted from:
182c60d41a9Swiz  *			P I N G . C
183c60d41a9Swiz  *
184c60d41a9Swiz  * Author -
185c60d41a9Swiz  *	Mike Muuss
186c60d41a9Swiz  *	U. S. Army Ballistic Research Laboratory
187c60d41a9Swiz  *	December, 1983
188c60d41a9Swiz  * Modified at Uc Berkeley
189c60d41a9Swiz  *
190c60d41a9Swiz  * (ping.c) Status -
191c60d41a9Swiz  *	Public Domain.  Distribution Unlimited.
192c60d41a9Swiz  *
193c60d41a9Swiz  *			I N _ C K S U M
194c60d41a9Swiz  *
195c60d41a9Swiz  * Checksum routine for Internet Protocol family headers (C Version)
196c60d41a9Swiz  *
197c60d41a9Swiz  */
198c60d41a9Swiz int
inet_cksum(const void * addr,u_int len)199*72867025Schristos inet_cksum(const void *addr, u_int len)
200c60d41a9Swiz {
201c60d41a9Swiz 	int nleft = (int)len;
202*72867025Schristos 	const u_int16_t *w = addr;
203c60d41a9Swiz 	int32_t sum = 0;
204c60d41a9Swiz 	union {
205c60d41a9Swiz 		u_int16_t w;
206c60d41a9Swiz 		u_int8_t b[2];
207c60d41a9Swiz 	} answer;
208c60d41a9Swiz 
209*72867025Schristos 	assert(((uintptr_t)w & 1) == 0);
210*72867025Schristos 
211c60d41a9Swiz 	/*
212c60d41a9Swiz 	 *  Our algorithm is simple, using a 32 bit accumulator (sum),
213c60d41a9Swiz 	 *  we add sequential 16 bit words to it, and at the end, fold
214c60d41a9Swiz 	 *  back all the carry bits from the top 16 bits into the lower
215c60d41a9Swiz 	 *  16 bits.
216c60d41a9Swiz 	 */
217c60d41a9Swiz 	while (nleft > 1)  {
218c60d41a9Swiz 		sum += *w++;
219c60d41a9Swiz 		nleft -= 2;
220c60d41a9Swiz 	}
221c60d41a9Swiz 
222c60d41a9Swiz 	/* mop up an odd byte, if necessary */
223c60d41a9Swiz 	if (nleft == 1) {
224c60d41a9Swiz 		answer.w = 0;
225*72867025Schristos 		answer.b[0] = *(const u_char *)w;
226c60d41a9Swiz 		sum += answer.w;
227c60d41a9Swiz 	}
228c60d41a9Swiz 
229c60d41a9Swiz 	/*
230c60d41a9Swiz 	 * add back carry outs from top 16 bits to low 16 bits
231c60d41a9Swiz 	 */
232c60d41a9Swiz 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
233c60d41a9Swiz 	sum += (sum >> 16);			/* add carry */
234c60d41a9Swiz 	answer.w = ~sum;			/* truncate to 16 bits */
235c60d41a9Swiz 	return (answer.w);
236c60d41a9Swiz }
237