cidr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2022 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "defines.h"
  20. #include "config.h"
  21. #include "common.h"
  22. #include <arpa/inet.h>
  23. #include <netinet/in.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/socket.h>
  28. static tcpr_cidr_t *cidr2cidr(char *);
  29. /**
  30. * prints to the given fd all the entries in mycidr
  31. */
  32. void
  33. print_cidr(tcpr_cidr_t *mycidr)
  34. {
  35. tcpr_cidr_t *cidr_ptr;
  36. fprintf(stderr, "Cidr List: ");
  37. cidr_ptr = mycidr;
  38. while (cidr_ptr != NULL) {
  39. /* print it */
  40. fprintf(stderr, "%s/%d, ", get_cidr2name(cidr_ptr, RESOLVE), cidr_ptr->masklen);
  41. /* go to the next */
  42. if (cidr_ptr->next != NULL) {
  43. cidr_ptr = cidr_ptr->next;
  44. } else {
  45. break;
  46. }
  47. }
  48. fprintf(stderr, "\n");
  49. }
  50. /**
  51. * deletes all entries in a cidr and destroys the datastructure
  52. */
  53. void
  54. destroy_cidr(tcpr_cidr_t *cidr)
  55. {
  56. if (cidr != NULL) {
  57. if (cidr->next != NULL)
  58. destroy_cidr(cidr->next);
  59. safe_free(cidr);
  60. }
  61. }
  62. /**
  63. * adds a new tcpr_cidr_t entry to cidrdata
  64. */
  65. void
  66. add_cidr(tcpr_cidr_t **cidrdata, tcpr_cidr_t **newcidr)
  67. {
  68. tcpr_cidr_t *cidr_ptr;
  69. dbg(1, "Running new_cidr()");
  70. if (*cidrdata == NULL) {
  71. *cidrdata = *newcidr;
  72. } else {
  73. cidr_ptr = *cidrdata;
  74. while (cidr_ptr->next != NULL)
  75. cidr_ptr = cidr_ptr->next;
  76. cidr_ptr->next = *newcidr;
  77. }
  78. }
  79. /**
  80. * Mallocs and sets to sane defaults a tcpr_cidr_t structure
  81. */
  82. tcpr_cidr_t *
  83. new_cidr(void)
  84. {
  85. tcpr_cidr_t *newcidr;
  86. newcidr = (tcpr_cidr_t *)safe_malloc(sizeof(tcpr_cidr_t));
  87. memset(newcidr, '\0', sizeof(tcpr_cidr_t));
  88. newcidr->masklen = 99;
  89. newcidr->next = NULL;
  90. return (newcidr);
  91. }
  92. /**
  93. * Creates a new tcpr_cidrmap_t structure. Malloc's memory
  94. */
  95. tcpr_cidrmap_t *
  96. new_cidr_map(void)
  97. {
  98. tcpr_cidrmap_t *new;
  99. new = (tcpr_cidrmap_t *)safe_malloc(sizeof(tcpr_cidrmap_t));
  100. memset(new, '\0', sizeof(tcpr_cidrmap_t));
  101. new->next = NULL;
  102. return (new);
  103. }
  104. /**
  105. * Converts a single cidr (string) in the form of x.x.x.x/y into a
  106. * tcpr_cidr_t structure. Will malloc the tcpr_cidr_t structure.
  107. */
  108. static tcpr_cidr_t *
  109. cidr2cidr(char *cidr)
  110. {
  111. int count;
  112. unsigned int octets[4]; /* used in sscanf */
  113. tcpr_cidr_t *newcidr;
  114. char networkip[16], tempoctet[4];
  115. int family;
  116. char *p;
  117. assert(cidr);
  118. newcidr = new_cidr();
  119. for (p = cidr; *p; ++p) {
  120. if (*p == '#') {
  121. *p = ':';
  122. } else if (*p == ']') {
  123. *p = 0;
  124. break;
  125. }
  126. }
  127. /*
  128. * scan it, and make sure it scanned correctly, also copy over the
  129. * masklen
  130. */
  131. count = sscanf(cidr, "%u.%u.%u.%u/%d", &octets[0], &octets[1], &octets[2], &octets[3], &newcidr->masklen);
  132. if (count == 4) {
  133. newcidr->masklen = 32;
  134. family = AF_INET;
  135. } else if (count == 5) {
  136. family = AF_INET;
  137. } else {
  138. p = strstr(cidr, "/");
  139. if (p) {
  140. *p = 0;
  141. ++p;
  142. sscanf(p, "%d", &newcidr->masklen);
  143. } else {
  144. newcidr->masklen = 128;
  145. }
  146. if (newcidr->masklen < 0 || newcidr->masklen > 128)
  147. goto error;
  148. /* skip past the opening [ */
  149. if (*cidr == '[')
  150. cidr++;
  151. if (get_name2addr6(cidr, RESOLVE, &newcidr->u.network6) > 0) {
  152. family = AF_INET6;
  153. } else {
  154. goto error;
  155. }
  156. }
  157. if (family == AF_INET) {
  158. /* masklen better be 0 =< masklen <= 32 */
  159. if (newcidr->masklen > 32)
  160. goto error;
  161. /* copy in the ip address */
  162. memset(networkip, '\0', 16);
  163. for (count = 0; count < 4; count++) {
  164. if (octets[count] > 255)
  165. goto error;
  166. snprintf(tempoctet, sizeof(octets[count]), "%u", octets[count]);
  167. strcat(networkip, tempoctet);
  168. /* we don't want a '.' at the end of the last octet */
  169. if (count < 3)
  170. strcat(networkip, ".");
  171. }
  172. /* copy over the network address and return */
  173. #ifdef HAVE_INET_ATON
  174. inet_aton(networkip, (struct in_addr *)&newcidr->u.network);
  175. #elif HAVE_INET_ADDR
  176. newcidr->network = inet_addr(networkip);
  177. #endif
  178. } else {
  179. /* Everything's done */
  180. }
  181. newcidr->family = family;
  182. return (newcidr);
  183. /* we only get here on error parsing input */
  184. error:
  185. errx(-1, "%s: %s", "Unable to parse as a valid CIDR", cidr);
  186. }
  187. static void
  188. mask_cidr6(char **cidrin, const char *delim)
  189. {
  190. if (**cidrin == '[' && *delim == ':') {
  191. char *p;
  192. ++*cidrin;
  193. /* make strtok happy */
  194. for (p = *cidrin; *p && *p != ']'; ++p) {
  195. if (*p == ':')
  196. *p = '#';
  197. }
  198. }
  199. }
  200. /**
  201. * parses a list of tcpr_cidr_t's input from the user which should be in the form
  202. * of x.x.x.x/y,x.x.x.x/y...
  203. * returns 1 for success, or fails to return on failure (exit 1)
  204. * since we use strtok to process cidr, it gets zeroed out.
  205. */
  206. int
  207. parse_cidr(tcpr_cidr_t **cidrdata, char *cidrin, char *delim)
  208. {
  209. tcpr_cidr_t *cidr_ptr; /* ptr to current cidr record */
  210. char *network;
  211. char *token = NULL;
  212. mask_cidr6(&cidrin, delim);
  213. /* first iteration of input using strtok */
  214. network = strtok_r(cidrin, delim, &token);
  215. if (network == NULL)
  216. return 0;
  217. *cidrdata = cidr2cidr(network);
  218. cidr_ptr = *cidrdata;
  219. /* do the same with the rest of the input */
  220. while (1) {
  221. if (token)
  222. mask_cidr6(&token, delim);
  223. network = strtok_r(NULL, delim, &token);
  224. /* if that was the last CIDR, then kickout */
  225. if (network == NULL)
  226. break;
  227. /* next record */
  228. cidr_ptr->next = cidr2cidr(network);
  229. cidr_ptr = cidr_ptr->next;
  230. }
  231. return 1;
  232. }
  233. /**
  234. * parses a pair of IP addresses: <IP1>:<IP2> and processes it like:
  235. * -N 0.0.0.0/0:<IP1> -N 0.0.0.0/0:<IP2>
  236. * returns 1 for success or returns 0 on failure
  237. * since we use strtok to process optarg, it gets zeroed out
  238. */
  239. int
  240. parse_endpoints(tcpr_cidrmap_t **cidrmap1, tcpr_cidrmap_t **cidrmap2, const char *optarg)
  241. {
  242. #define NEWMAP_LEN (INET6_ADDRSTRLEN * 2)
  243. char *map = NULL, newmap[NEWMAP_LEN];
  244. char *token = NULL;
  245. char *string;
  246. char *p;
  247. int res = 0;
  248. string = safe_strdup(optarg);
  249. if (*string == '[') {
  250. /* ipv6 mode */
  251. memset(newmap, '\0', NEWMAP_LEN);
  252. p = strstr(string, "]:[");
  253. if (!p)
  254. goto done;
  255. *p = 0;
  256. strlcpy(newmap, "[::/0]:", NEWMAP_LEN);
  257. strlcat(newmap, string, NEWMAP_LEN);
  258. strlcat(newmap, "]", NEWMAP_LEN);
  259. if (!parse_cidr_map(cidrmap1, newmap))
  260. goto done;
  261. /* do again with the second IP */
  262. memset(newmap, '\0', NEWMAP_LEN);
  263. strlcpy(newmap, "[::/0]:", NEWMAP_LEN);
  264. strlcat(newmap, p + 2, NEWMAP_LEN);
  265. if (!parse_cidr_map(cidrmap2, newmap))
  266. goto done;
  267. } else {
  268. /* ipv4 mode */
  269. memset(newmap, '\0', NEWMAP_LEN);
  270. map = strtok_r(string, ":", &token);
  271. if (map == NULL)
  272. goto done;
  273. strlcpy(newmap, "0.0.0.0/0:", NEWMAP_LEN);
  274. strlcat(newmap, map, NEWMAP_LEN);
  275. if (!parse_cidr_map(cidrmap1, newmap))
  276. goto done;
  277. /* do again with the second IP */
  278. memset(newmap, '\0', NEWMAP_LEN);
  279. map = strtok_r(NULL, ":", &token);
  280. strlcpy(newmap, "0.0.0.0/0:", NEWMAP_LEN);
  281. strlcat(newmap, map, NEWMAP_LEN);
  282. if (!parse_cidr_map(cidrmap2, newmap))
  283. goto done;
  284. }
  285. /* success */
  286. res = 1;
  287. done:
  288. safe_free(string);
  289. return res;
  290. }
  291. /**
  292. * parses a list of tcpr_cidrmap_t's input from the user which should be in the form
  293. * of x.x.x.x/y:x.x.x.x/y,...
  294. * IPv6 syntax: [addr/y]:[addr/y],...
  295. * returns 1 for success, or returns 0 on failure
  296. * since we use strtok to process optarg, it gets zeroed out.
  297. */
  298. int
  299. parse_cidr_map(tcpr_cidrmap_t **cidrmap, const char *optarg)
  300. {
  301. tcpr_cidr_t *cidr = NULL;
  302. char *map;
  303. char *token = NULL, *string;
  304. tcpr_cidrmap_t *ptr;
  305. int res = 0;
  306. string = safe_strdup(optarg);
  307. /* first iteration */
  308. map = strtok_r(string, ",", &token);
  309. if (!parse_cidr(&cidr, map, ":"))
  310. goto done;
  311. /* must return a linked list of two */
  312. if (cidr->next == NULL)
  313. goto done;
  314. /* copy over */
  315. *cidrmap = new_cidr_map();
  316. ptr = *cidrmap;
  317. ptr->from = cidr;
  318. ptr->to = cidr->next;
  319. ptr->from->next = NULL;
  320. /* do the same with the reset of the input */
  321. while (1) {
  322. map = strtok_r(NULL, ",", &token);
  323. if (map == NULL)
  324. break;
  325. if (!parse_cidr(&cidr, map, ":"))
  326. goto done;
  327. /* must return a linked list of two */
  328. if (cidr->next == NULL)
  329. goto done;
  330. /* copy over */
  331. ptr->next = new_cidr_map();
  332. ptr = ptr->next;
  333. ptr->from = cidr;
  334. ptr->to = cidr->next;
  335. ptr->from->next = NULL;
  336. }
  337. /* success */
  338. res = 1;
  339. done:
  340. safe_free(string);
  341. return res;
  342. }
  343. /**
  344. * checks to see if the ip address is in the cidr
  345. * returns 1 for true, 0 for false
  346. */
  347. int
  348. ip_in_cidr(const tcpr_cidr_t *mycidr, const unsigned long ip)
  349. {
  350. unsigned long ipaddr, network, mask;
  351. int ret;
  352. #ifdef DEBUG
  353. char netstr[20];
  354. #endif
  355. if (mycidr->family != AF_INET)
  356. return 0;
  357. /* always return 1 if 0.0.0.0/0 */
  358. if (mycidr->masklen == 0 && mycidr->u.network == 0)
  359. return 1;
  360. mask = ~0; /* turn on all the bits */
  361. /* shift over by the correct number of bits */
  362. mask = mask << (32 - mycidr->masklen);
  363. /* apply the mask to the network and ip */
  364. ipaddr = ntohl(ip) & mask;
  365. network = htonl(mycidr->u.network) & mask;
  366. #ifdef DEBUG
  367. /* copy this for debug purposes, since it's not re-entrant */
  368. strlcpy(netstr, get_addr2name4(mycidr->u.network, RESOLVE), 20);
  369. #endif
  370. /* if they're the same, then ip is in network */
  371. if (network == ipaddr) {
  372. #ifdef DEBUG
  373. dbgx(1, "The ip %s is inside of %s/%d", get_addr2name4(ip, RESOLVE), netstr, mycidr->masklen);
  374. #endif
  375. ret = 1;
  376. } else {
  377. #ifdef DEBUG
  378. dbgx(1, "The ip %s is not inside of %s/%d", get_addr2name4(ip, RESOLVE), netstr, mycidr->masklen);
  379. #endif
  380. ret = 0;
  381. }
  382. return ret;
  383. }
  384. static int
  385. ip6_addr_is_unspec(const struct tcpr_in6_addr *addr)
  386. {
  387. return addr->tcpr_s6_addr32[0] == 0 && addr->tcpr_s6_addr32[1] == 0 && addr->tcpr_s6_addr32[2] == 0 &&
  388. addr->tcpr_s6_addr32[3] == 0;
  389. }
  390. int
  391. ip6_in_cidr(const tcpr_cidr_t *mycidr, const struct tcpr_in6_addr *addr)
  392. {
  393. int ret;
  394. #ifdef DEBUG
  395. char netstr[INET6_ADDRSTRLEN];
  396. #endif
  397. uint32_t i, j, k;
  398. if (mycidr->family != AF_INET6)
  399. return 0;
  400. /* always return 1 if ::/0 */
  401. if (mycidr->masklen == 0 && ip6_addr_is_unspec(addr))
  402. return 1;
  403. j = mycidr->masklen / 8;
  404. for (i = 0; i < j; i++) {
  405. if (addr->tcpr_s6_addr[i] != mycidr->u.network6.tcpr_s6_addr[i]) {
  406. ret = 0;
  407. goto out;
  408. }
  409. }
  410. if ((k = mycidr->masklen % 8) == 0) {
  411. ret = 1;
  412. goto out;
  413. }
  414. k = (uint32_t)~0 << (8 - k);
  415. i = addr->tcpr_s6_addr[j] & k;
  416. j = mycidr->u.network6.tcpr_s6_addr[j] & k;
  417. ret = i == j;
  418. out:
  419. #ifdef DEBUG
  420. /* copy this for debug purposes, since it's not re-entrant */
  421. strlcpy(netstr, get_addr2name6(&mycidr->u.network6, RESOLVE), INET6_ADDRSTRLEN);
  422. #endif
  423. /* if they're the same, then ip is in network */
  424. if (ret) {
  425. #ifdef DEBUG
  426. dbgx(1, "The ip %s is inside of %s/%d", get_addr2name6(addr, RESOLVE), netstr, mycidr->masklen);
  427. #endif
  428. } else {
  429. #ifdef DEBUG
  430. dbgx(1, "The ip %s is not inside of %s/%d", get_addr2name6(addr, RESOLVE), netstr, mycidr->masklen);
  431. #endif
  432. }
  433. return ret;
  434. }
  435. /**
  436. * iterates over cidrdata to find if a given ip matches
  437. * returns 1 for true, 0 for false
  438. */
  439. int
  440. check_ip_cidr(tcpr_cidr_t *cidrdata, const unsigned long ip)
  441. {
  442. tcpr_cidr_t *mycidr;
  443. /* if we have no cidrdata, of course it isn't in there
  444. * this actually should happen occasionally, so don't put an assert here
  445. */
  446. if (cidrdata == NULL)
  447. return 1;
  448. mycidr = cidrdata;
  449. /* loop through cidr */
  450. while (1) {
  451. /* if match, return 1 */
  452. if (ip_in_cidr(mycidr, ip)) {
  453. dbgx(3, "Found %s in cidr", get_addr2name4(ip, RESOLVE));
  454. return 1;
  455. }
  456. /* check for next record */
  457. if (mycidr->next != NULL) {
  458. mycidr = mycidr->next;
  459. } else {
  460. break;
  461. }
  462. }
  463. /* if we get here, no match */
  464. dbgx(3, "Didn't find %s in cidr", get_addr2name4(ip, RESOLVE));
  465. return 0;
  466. }
  467. int
  468. check_ip6_cidr(tcpr_cidr_t *cidrdata, const struct tcpr_in6_addr *addr)
  469. {
  470. tcpr_cidr_t *mycidr;
  471. /* if we have no cidrdata, of course it isn't in there
  472. * this actually should happen occasionally, so don't put an assert here
  473. */
  474. if (cidrdata == NULL) {
  475. return 1;
  476. }
  477. mycidr = cidrdata;
  478. /* loop through cidr */
  479. while (1) {
  480. /* if match, return 1 */
  481. if (ip6_in_cidr(mycidr, addr)) {
  482. dbgx(3, "Found %s in cidr", get_addr2name6(addr, RESOLVE));
  483. return 1;
  484. }
  485. /* check for next record */
  486. if (mycidr->next != NULL) {
  487. mycidr = mycidr->next;
  488. } else {
  489. break;
  490. }
  491. }
  492. /* if we get here, no match */
  493. dbgx(3, "Didn't find %s in cidr", get_addr2name6(addr, RESOLVE));
  494. return 0;
  495. }