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