cidr.c 17 KB

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