xref: /netbsd-src/usr.sbin/mrouted/inet.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: inet.c,v 1.7 2002/08/01 08:33:14 itojun 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 
13 #include "defs.h"
14 
15 
16 /*
17  * Exported variables.
18  */
19 char s1[19];		/* buffers to hold the string representations  */
20 char s2[19];		/* of IP addresses, to be passed to inet_fmt() */
21 char s3[19];		/* or inet_fmts().                             */
22 char s4[19];
23 
24 
25 /*
26  * Verify that a given IP address is credible as a host address.
27  * (Without a mask, cannot detect addresses of the form {subnet,0} or
28  * {subnet,-1}.)
29  */
30 int
31 inet_valid_host(u_int32_t naddr)
32 {
33     u_int32_t addr;
34 
35     addr = ntohl(naddr);
36 
37     return (!(IN_MULTICAST(addr) ||
38 	      IN_BADCLASS (addr) ||
39 	      (addr & 0xff000000) == 0));
40 }
41 
42 /*
43  * Verify that a given netmask is plausible;
44  * make sure that it is a series of 1's followed by
45  * a series of 0's with no discontiguous 1's.
46  */
47 int
48 inet_valid_mask(u_int32_t mask)
49 {
50     if (~(((mask & -mask) - 1) | mask) != 0) {
51 	/* Mask is not contiguous */
52 	return (FALSE);
53     }
54 
55     return (TRUE);
56 }
57 
58 /*
59  * Verify that a given subnet number and mask pair are credible.
60  *
61  * With CIDR, almost any subnet and mask are credible.  mrouted still
62  * can't handle aggregated class A's, so we still check that, but
63  * otherwise the only requirements are that the subnet address is
64  * within the [ABC] range and that the host bits of the subnet
65  * are all 0.
66  */
67 int
68 inet_valid_subnet(u_int32_t nsubnet, u_int32_t nmask)
69 {
70     u_int32_t subnet, mask;
71 
72     subnet = ntohl(nsubnet);
73     mask   = ntohl(nmask);
74 
75     if ((subnet & mask) != subnet) return (FALSE);
76 
77     if (subnet == 0)
78 	return (mask == 0);
79 
80     if (IN_CLASSA(subnet)) {
81 	if (mask < 0xff000000 ||
82 	    (subnet & 0xff000000) == 0x7f000000 ||
83 	    (subnet & 0xff000000) == 0x00000000) return (FALSE);
84     }
85     else if (IN_CLASSD(subnet) || IN_BADCLASS(subnet)) {
86 	/* Above Class C address space */
87 	return (FALSE);
88     }
89     if (subnet & ~mask) {
90 	/* Host bits are set in the subnet */
91 	return (FALSE);
92     }
93     if (!inet_valid_mask(mask)) {
94 	/* Netmask is not contiguous */
95 	return (FALSE);
96     }
97 
98     return (TRUE);
99 }
100 
101 
102 /*
103  * Convert an IP address in u_long (network) format into a printable string.
104  */
105 char *
106 inet_fmt(u_int32_t addr, char *s)
107 {
108     u_char *a;
109 
110     a = (u_char *)&addr;
111     sprintf(s, "%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
112     return (s);
113 }
114 
115 
116 /*
117  * Convert an IP subnet number in u_long (network) format into a printable
118  * string including the netmask as a number of bits.
119  */
120 char *
121 inet_fmts(u_int32_t addr, u_int32_t mask, char *s)
122 {
123     u_char *a, *m;
124     int bits;
125 
126     if ((addr == 0) && (mask == 0)) {
127 	sprintf(s, "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) sprintf(s, "%u.%u.%u.%u/%d", a[0], a[1], a[2], a[3],
135 						bits);
136     else if (m[2] != 0) sprintf(s, "%u.%u.%u/%d",    a[0], a[1], a[2], bits);
137     else if (m[1] != 0) sprintf(s, "%u.%u/%d",       a[0], a[1], bits);
138     else                sprintf(s, "%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
150 inet_parse(char *s)
151 {
152     u_int32_t a = 0;
153     u_int a0, a1, a2, a3;
154     char c;
155 
156     if (sscanf(s, "%u.%u.%u.%u%c", &a0, &a1, &a2, &a3, &c) != 4 ||
157 	a0 > 255 || a1 > 255 || a2 > 255 || a3 > 255)
158 	return (0xffffffff);
159 
160     ((u_char *)&a)[0] = a0;
161     ((u_char *)&a)[1] = a1;
162     ((u_char *)&a)[2] = a2;
163     ((u_char *)&a)[3] = a3;
164 
165     return (a);
166 }
167 
168 
169 /*
170  * inet_cksum extracted from:
171  *			P I N G . C
172  *
173  * Author -
174  *	Mike Muuss
175  *	U. S. Army Ballistic Research Laboratory
176  *	December, 1983
177  * Modified at Uc Berkeley
178  *
179  * (ping.c) Status -
180  *	Public Domain.  Distribution Unlimited.
181  *
182  *			I N _ C K S U M
183  *
184  * Checksum routine for Internet Protocol family headers (C Version)
185  *
186  */
187 int
188 inet_cksum(u_int16_t *addr, u_int len)
189 {
190 	int nleft = (int)len;
191 	u_int16_t *w = addr;
192 	int32_t sum = 0;
193 	union {
194 		u_int16_t w;
195 		u_int8_t b[2];
196 	} answer;
197 
198 	/*
199 	 *  Our algorithm is simple, using a 32 bit accumulator (sum),
200 	 *  we add sequential 16 bit words to it, and at the end, fold
201 	 *  back all the carry bits from the top 16 bits into the lower
202 	 *  16 bits.
203 	 */
204 	while (nleft > 1)  {
205 		sum += *w++;
206 		nleft -= 2;
207 	}
208 
209 	/* mop up an odd byte, if necessary */
210 	if (nleft == 1) {
211 		answer.w = 0;
212 		answer.b[0] = *(u_char *)w ;
213 		sum += answer.w;
214 	}
215 
216 	/*
217 	 * add back carry outs from top 16 bits to low 16 bits
218 	 */
219 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
220 	sum += (sum >> 16);			/* add carry */
221 	answer.w = ~sum;			/* truncate to 16 bits */
222 	return (answer.w);
223 }
224