cidr.c 14 KB

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