mapcalc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * mapcalc - MAP parameter calculation
  3. *
  4. * Author: Steven Barth <cyrus@openwrt.org>
  5. * Copyright (c) 2014-2015 cisco Systems, Inc.
  6. * Copyright (c) 2015 Steven Barth <cyrus@openwrt.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2
  10. * as published by the Free Software Foundation
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <arpa/inet.h>
  20. #include <errno.h>
  21. #include <libubus.h>
  22. #include <libubox/utils.h>
  23. struct blob_attr *dump = NULL;
  24. enum {
  25. DUMP_ATTR_INTERFACE,
  26. DUMP_ATTR_MAX
  27. };
  28. static const struct blobmsg_policy dump_attrs[DUMP_ATTR_MAX] = {
  29. [DUMP_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_ARRAY },
  30. };
  31. enum {
  32. IFACE_ATTR_INTERFACE,
  33. IFACE_ATTR_PREFIX,
  34. IFACE_ATTR_ADDRESS,
  35. IFACE_ATTR_MAX,
  36. };
  37. static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
  38. [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
  39. [IFACE_ATTR_PREFIX] = { .name = "ipv6-prefix", .type = BLOBMSG_TYPE_ARRAY },
  40. [IFACE_ATTR_ADDRESS] = { .name = "ipv6-address", .type = BLOBMSG_TYPE_ARRAY },
  41. };
  42. enum {
  43. PREFIX_ATTR_ADDRESS,
  44. PREFIX_ATTR_MASK,
  45. PREFIX_ATTR_MAX,
  46. };
  47. static const struct blobmsg_policy prefix_attrs[PREFIX_ATTR_MAX] = {
  48. [PREFIX_ATTR_ADDRESS] = { .name = "address", .type = BLOBMSG_TYPE_STRING },
  49. [PREFIX_ATTR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_INT32 },
  50. };
  51. static int bmemcmp(const void *av, const void *bv, size_t bits)
  52. {
  53. const uint8_t *a = av, *b = bv;
  54. size_t bytes = bits / 8;
  55. bits %= 8;
  56. int res = memcmp(a, b, bytes);
  57. if (res == 0 && bits > 0)
  58. res = (a[bytes] >> (8 - bits)) - (b[bytes] >> (8 - bits));
  59. return res;
  60. }
  61. static void bmemcpy(void *av, const void *bv, size_t bits)
  62. {
  63. uint8_t *a = av;
  64. const uint8_t *b = bv;
  65. size_t bytes = bits / 8;
  66. bits %= 8;
  67. memcpy(a, b, bytes);
  68. if (bits > 0) {
  69. uint8_t mask = (1 << (8 - bits)) - 1;
  70. a[bytes] = (a[bytes] & mask) | ((~mask) & b[bytes]);
  71. }
  72. }
  73. static void bmemcpys64(void *av, const void *bv, size_t frombits, size_t nbits)
  74. {
  75. uint64_t buf = 0;
  76. const uint8_t *b = bv;
  77. size_t frombyte = frombits / 8, tobyte = (frombits + nbits) / 8;
  78. memcpy(&buf, &b[frombyte], tobyte - frombyte + 1);
  79. buf = cpu_to_be64(be64_to_cpu(buf) << (frombits % 8));
  80. bmemcpy(av, &buf, nbits);
  81. }
  82. static void handle_dump(struct ubus_request *req __attribute__((unused)),
  83. int type __attribute__((unused)), struct blob_attr *msg)
  84. {
  85. struct blob_attr *tb[DUMP_ATTR_INTERFACE];
  86. blobmsg_parse(dump_attrs, DUMP_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
  87. if (!tb[DUMP_ATTR_INTERFACE])
  88. return;
  89. dump = blob_memdup(tb[DUMP_ATTR_INTERFACE]);
  90. }
  91. static void match_prefix(int *pdlen, struct in6_addr *pd, struct blob_attr *cur,
  92. const struct in6_addr *ipv6prefix, int prefix6len, bool lw4o6)
  93. {
  94. struct blob_attr *d;
  95. unsigned drem;
  96. if (!cur || blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY || !blobmsg_check_attr(cur, NULL))
  97. return;
  98. blobmsg_for_each_attr(d, cur, drem) {
  99. struct blob_attr *ptb[PREFIX_ATTR_MAX];
  100. blobmsg_parse(prefix_attrs, PREFIX_ATTR_MAX, ptb,
  101. blobmsg_data(d), blobmsg_data_len(d));
  102. if (!ptb[PREFIX_ATTR_ADDRESS] || !ptb[PREFIX_ATTR_MASK])
  103. continue;
  104. struct in6_addr prefix = IN6ADDR_ANY_INIT;
  105. int mask = blobmsg_get_u32(ptb[PREFIX_ATTR_MASK]);
  106. inet_pton(AF_INET6, blobmsg_get_string(ptb[PREFIX_ATTR_ADDRESS]), &prefix);
  107. // lw4over6 /128-address-as-PD matching madness workaround
  108. if (lw4o6 && mask == 128)
  109. mask = 64;
  110. if (*pdlen < mask && mask >= prefix6len &&
  111. !bmemcmp(&prefix, ipv6prefix, prefix6len)) {
  112. bmemcpy(pd, &prefix, mask);
  113. *pdlen = mask;
  114. } else if (lw4o6 && *pdlen < prefix6len && mask < prefix6len &&
  115. !bmemcmp(&prefix, ipv6prefix, mask)) {
  116. bmemcpy(pd, ipv6prefix, prefix6len);
  117. *pdlen = prefix6len;
  118. }
  119. }
  120. }
  121. enum {
  122. OPT_TYPE,
  123. OPT_FMR,
  124. OPT_EALEN,
  125. OPT_PREFIX4LEN,
  126. OPT_PREFIX6LEN,
  127. OPT_IPV6PREFIX,
  128. OPT_IPV4PREFIX,
  129. OPT_OFFSET,
  130. OPT_PSIDLEN,
  131. OPT_PSID,
  132. OPT_BR,
  133. OPT_DMR,
  134. OPT_PD,
  135. OPT_PDLEN,
  136. OPT_MAX
  137. };
  138. static char *const token[] = {
  139. [OPT_TYPE] = "type",
  140. [OPT_FMR] = "fmr",
  141. [OPT_EALEN] = "ealen",
  142. [OPT_PREFIX4LEN] = "prefix4len",
  143. [OPT_PREFIX6LEN] = "prefix6len",
  144. [OPT_IPV6PREFIX] = "ipv6prefix",
  145. [OPT_IPV4PREFIX] = "ipv4prefix",
  146. [OPT_OFFSET] = "offset",
  147. [OPT_PSIDLEN] = "psidlen",
  148. [OPT_PSID] = "psid",
  149. [OPT_BR] = "br",
  150. [OPT_DMR] = "dmr",
  151. [OPT_PD] = "pd",
  152. [OPT_PDLEN] = "pdlen",
  153. [OPT_MAX] = NULL
  154. };
  155. int main(int argc, char *argv[])
  156. {
  157. int status = 0;
  158. const char *iface = argv[1];
  159. const char *legacy_env = getenv("LEGACY");
  160. bool legacy = legacy_env && atoi(legacy_env);
  161. if (argc < 3) {
  162. fprintf(stderr, "Usage: %s <interface|*> <rule1> [rule2] [...]\n", argv[0]);
  163. return 1;
  164. }
  165. uint32_t network_interface;
  166. struct ubus_context *ubus = ubus_connect(NULL);
  167. if (ubus) {
  168. ubus_lookup_id(ubus, "network.interface", &network_interface);
  169. ubus_invoke(ubus, network_interface, "dump", NULL, handle_dump, NULL, 5000);
  170. }
  171. int rulecnt = 0;
  172. for (int i = 2; i < argc; ++i) {
  173. bool lw4o6 = false;
  174. bool fmr = false;
  175. int ealen = -1;
  176. int addr4len = 32;
  177. int prefix4len = 32;
  178. int prefix6len = -1;
  179. int pdlen = -1;
  180. struct in_addr ipv4prefix = {INADDR_ANY};
  181. struct in_addr ipv4addr = {INADDR_ANY};
  182. struct in6_addr ipv6addr = IN6ADDR_ANY_INIT;
  183. struct in6_addr ipv6prefix = IN6ADDR_ANY_INIT;
  184. struct in6_addr pd = IN6ADDR_ANY_INIT;
  185. int offset = -1;
  186. int psidlen = -1;
  187. int psid = -1;
  188. uint16_t psid16 = 0;
  189. const char *dmr = NULL;
  190. const char *br = NULL;
  191. for (char *rule = strdup(argv[i]); *rule; ) {
  192. char *value;
  193. int intval;
  194. int idx = getsubopt(&rule, token, &value);
  195. errno = 0;
  196. if (idx == OPT_TYPE) {
  197. lw4o6 = (value && !strcmp(value, "lw4o6"));
  198. } else if (idx == OPT_FMR) {
  199. fmr = true;
  200. } else if (idx == OPT_EALEN && (intval = strtoul(value, NULL, 0)) <= 48 && !errno) {
  201. ealen = intval;
  202. } else if (idx == OPT_PREFIX4LEN && (intval = strtoul(value, NULL, 0)) <= 32 && !errno) {
  203. prefix4len = intval;
  204. } else if (idx == OPT_PREFIX6LEN && (intval = strtoul(value, NULL, 0)) <= 128 && !errno) {
  205. prefix6len = intval;
  206. } else if (idx == OPT_IPV4PREFIX && inet_pton(AF_INET, value, &ipv4prefix) == 1) {
  207. // dummy
  208. } else if (idx == OPT_IPV6PREFIX && inet_pton(AF_INET6, value, &ipv6prefix) == 1) {
  209. // dummy
  210. } else if (idx == OPT_PD && inet_pton(AF_INET6, value, &pd) == 1) {
  211. // dummy
  212. } else if (idx == OPT_OFFSET && (intval = strtoul(value, NULL, 0)) <= 16 && !errno) {
  213. offset = intval;
  214. } else if (idx == OPT_PSIDLEN && (intval = strtoul(value, NULL, 0)) <= 16 && !errno) {
  215. psidlen = intval;
  216. } else if (idx == OPT_PDLEN && (intval = strtoul(value, NULL, 0)) <= 128 && !errno) {
  217. pdlen = intval;
  218. } else if (idx == OPT_PSID && (intval = strtoul(value, NULL, 0)) <= 65535 && !errno) {
  219. psid = intval;
  220. } else if (idx == OPT_DMR) {
  221. dmr = value;
  222. } else if (idx == OPT_BR) {
  223. br = value;
  224. } else {
  225. if (idx == -1 || idx >= OPT_MAX)
  226. fprintf(stderr, "Skipped invalid option: %s\n", value);
  227. else
  228. fprintf(stderr, "Skipped invalid value %s for option %s\n",
  229. value, token[idx]);
  230. }
  231. }
  232. if (offset < 0)
  233. offset = (lw4o6) ? 0 : (legacy) ? 4 : 6;
  234. // LW4over6 doesn't have an EALEN and has no psid-autodetect
  235. if (lw4o6) {
  236. if (psidlen < 0)
  237. psidlen = 0;
  238. ealen = psidlen;
  239. }
  240. // Find PD
  241. if (pdlen < 0) {
  242. struct blob_attr *c;
  243. unsigned rem;
  244. blobmsg_for_each_attr(c, dump, rem) {
  245. struct blob_attr *tb[IFACE_ATTR_MAX];
  246. blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
  247. if (!tb[IFACE_ATTR_INTERFACE] || (strcmp(argv[1], "*") && strcmp(argv[1],
  248. blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]))))
  249. continue;
  250. match_prefix(&pdlen, &pd, tb[IFACE_ATTR_PREFIX], &ipv6prefix, prefix6len, lw4o6);
  251. if (lw4o6)
  252. match_prefix(&pdlen, &pd, tb[IFACE_ATTR_ADDRESS], &ipv6prefix, prefix6len, lw4o6);
  253. if (pdlen >= 0) {
  254. iface = blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]);
  255. break;
  256. }
  257. }
  258. }
  259. if (ealen < 0 && pdlen >= 0)
  260. ealen = pdlen - prefix6len;
  261. if (psidlen <= 0) {
  262. psidlen = ealen - (32 - prefix4len);
  263. psid = -1;
  264. }
  265. if (psid < 0 && psidlen <= 16 && psidlen >= 0 && pdlen >= 0 && ealen >= psidlen) {
  266. bmemcpys64(&psid16, &pd, prefix6len + ealen - psidlen, psidlen);
  267. psid = be16_to_cpu(psid16);
  268. }
  269. psid = psid >> (16 - psidlen);
  270. psid16 = cpu_to_be16(psid);
  271. psid = psid << (16 - psidlen);
  272. if (prefix4len < 0 || prefix6len < 0 || ealen < 0 || ealen < psidlen) {
  273. fprintf(stderr, "Skipping invalid or incomplete rule: %s\n", argv[i]);
  274. status = 1;
  275. continue;
  276. }
  277. if ((pdlen >= 0 || ealen == psidlen) && ealen >= psidlen) {
  278. bmemcpys64(&ipv4addr, &pd, prefix6len, ealen - psidlen);
  279. ipv4addr.s_addr = htonl(ntohl(ipv4addr.s_addr) >> prefix4len);
  280. bmemcpy(&ipv4addr, &ipv4prefix, prefix4len);
  281. if (prefix4len + ealen < 32)
  282. addr4len = prefix4len + ealen;
  283. }
  284. if (pdlen < 0 && !fmr) {
  285. fprintf(stderr, "Skipping non-FMR without matching PD: %s\n", argv[i]);
  286. status = 1;
  287. continue;
  288. } else if (pdlen >= 0) {
  289. size_t v4offset = (legacy) ? 9 : 10;
  290. memcpy(&ipv6addr.s6_addr[v4offset], &ipv4addr, 4);
  291. memcpy(&ipv6addr.s6_addr[v4offset + 4], &psid16, 2);
  292. bmemcpy(&ipv6addr, &pd, pdlen);
  293. }
  294. ++rulecnt;
  295. char ipv4addrbuf[INET_ADDRSTRLEN];
  296. char ipv4prefixbuf[INET_ADDRSTRLEN];
  297. char ipv6prefixbuf[INET6_ADDRSTRLEN];
  298. char ipv6addrbuf[INET6_ADDRSTRLEN];
  299. char pdbuf[INET6_ADDRSTRLEN];
  300. inet_ntop(AF_INET, &ipv4addr, ipv4addrbuf, sizeof(ipv4addrbuf));
  301. inet_ntop(AF_INET, &ipv4prefix, ipv4prefixbuf, sizeof(ipv4prefixbuf));
  302. inet_ntop(AF_INET6, &ipv6prefix, ipv6prefixbuf, sizeof(ipv6prefixbuf));
  303. inet_ntop(AF_INET6, &ipv6addr, ipv6addrbuf, sizeof(ipv6addrbuf));
  304. inet_ntop(AF_INET6, &pd, pdbuf, sizeof(pdbuf));
  305. printf("RULE_%d_FMR=%d\n", rulecnt, fmr);
  306. printf("RULE_%d_EALEN=%d\n", rulecnt, ealen);
  307. printf("RULE_%d_PSIDLEN=%d\n", rulecnt, psidlen);
  308. printf("RULE_%d_OFFSET=%d\n", rulecnt, offset);
  309. printf("RULE_%d_PREFIX4LEN=%d\n", rulecnt, prefix4len);
  310. printf("RULE_%d_PREFIX6LEN=%d\n", rulecnt, prefix6len);
  311. printf("RULE_%d_IPV4PREFIX=%s\n", rulecnt, ipv4prefixbuf);
  312. printf("RULE_%d_IPV6PREFIX=%s\n", rulecnt, ipv6prefixbuf);
  313. if (pdlen >= 0) {
  314. printf("RULE_%d_IPV6PD=%s\n", rulecnt, pdbuf);
  315. printf("RULE_%d_PD6LEN=%d\n", rulecnt, pdlen);
  316. printf("RULE_%d_PD6IFACE=%s\n", rulecnt, iface);
  317. printf("RULE_%d_IPV6ADDR=%s\n", rulecnt, ipv6addrbuf);
  318. printf("RULE_BMR=%d\n", rulecnt);
  319. }
  320. if (ipv4addr.s_addr) {
  321. printf("RULE_%d_IPV4ADDR=%s\n", rulecnt, ipv4addrbuf);
  322. printf("RULE_%d_ADDR4LEN=%d\n", rulecnt, addr4len);
  323. }
  324. if (psidlen > 0 && psid >= 0) {
  325. printf("RULE_%d_PORTSETS='", rulecnt);
  326. for (int k = (offset) ? 1 : 0; k < (1 << offset); ++k) {
  327. int start = (k << (16 - offset)) | (psid >> offset);
  328. int end = start + (1 << (16 - offset - psidlen)) - 1;
  329. if (start == 0)
  330. start = 1;
  331. if (start <= end)
  332. printf("%d-%d ", start, end);
  333. }
  334. printf("'\n");
  335. }
  336. if (dmr)
  337. printf("RULE_%d_DMR=%s\n", rulecnt, dmr);
  338. if (br)
  339. printf("RULE_%d_BR=%s\n", rulecnt, br);
  340. }
  341. printf("RULE_COUNT=%d\n", rulecnt);
  342. return status;
  343. }