1 /* $NetBSD: inet.c,v 1.4 1995/12/10 10:07:03 mycroft 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 #define SNAMLEN 19 20 char s1[SNAMLEN]; /* buffers to hold the string representations */ 21 char s2[SNAMLEN]; /* of IP addresses, to be passed to inet_fmt() */ 22 char s3[SNAMLEN]; /* or inet_fmts(). */ 23 char s4[SNAMLEN]; 24 25 26 /* 27 * Verify that a given IP address is credible as a host address. 28 * (Without a mask, cannot detect addresses of the form {subnet,0} or 29 * {subnet,-1}.) 30 */ 31 int 32 inet_valid_host(u_int32_t naddr) 33 { 34 u_int32_t addr; 35 36 addr = ntohl(naddr); 37 38 return (!(IN_MULTICAST(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)) { 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 snprintf(s, SNAMLEN, "%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 snprintf(s, SNAMLEN, "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) 135 snprintf(s, SNAMLEN, "%u.%u.%u.%u/%d", a[0], a[1], a[2], a[3], bits); 136 else if (m[2] != 0) 137 snprintf(s, SNAMLEN, "%u.%u.%u/%d", a[0], a[1], a[2], bits); 138 else if (m[1] != 0) 139 snprintf(s, SNAMLEN, "%u.%u/%d", a[0], a[1], bits); 140 else 141 snprintf(s, SNAMLEN, "%u/%d", a[0], bits); 142 143 return (s); 144 } 145 146 /* 147 * Convert the printable string representation of an IP address into the 148 * u_long (network) format. Return 0xffffffff on error. (To detect the 149 * legal address with that value, you must explicitly compare the string 150 * with "255.255.255.255".) 151 */ 152 u_int32_t 153 inet_parse(char *s) 154 { 155 u_int32_t a = 0; 156 u_int a0, a1, a2, a3; 157 char c; 158 159 if (sscanf(s, "%u.%u.%u.%u%c", &a0, &a1, &a2, &a3, &c) != 4 || 160 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_int16_t *addr, u_int len) 192 { 193 int nleft = (int)len; 194 u_int16_t *w = addr; 195 u_int16_t answer = 0; 196 int32_t 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 } 223