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