cidr.c 16 KB

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