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