cidr.c 16 KB

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