RouteGen.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "benc/String.h"
  16. #include "benc/Dict.h"
  17. #include "util/platform/Sockaddr.h"
  18. #include "exception/Except.h"
  19. #include "benc/List.h"
  20. #include "tunnel/RouteGen.h"
  21. #include "util/log/Log.h"
  22. #include "util/Identity.h"
  23. #include "util/Bits.h"
  24. #include "util/platform/netdev/NetDev.h"
  25. struct Prefix6
  26. {
  27. uint64_t highBits;
  28. uint64_t lowBits;
  29. int prefix;
  30. struct Allocator* alloc;
  31. };
  32. static int comparePrefixes6(struct Prefix6* a, struct Prefix6* b)
  33. {
  34. if (a->prefix != b->prefix) {
  35. return (a->prefix < b->prefix) ? -1 : 1;
  36. }
  37. if (a->highBits != b->highBits) {
  38. return (a->highBits < b->highBits) ? 1 : -1;
  39. }
  40. if (a->lowBits != b->lowBits) {
  41. return (a->lowBits < b->lowBits) ? 1 : -1;
  42. }
  43. return 0;
  44. }
  45. #define ArrayList_COMPARE comparePrefixes6
  46. #define ArrayList_TYPE struct Prefix6
  47. #define ArrayList_NAME OfPrefix6
  48. #include "util/ArrayList.h"
  49. struct Prefix4
  50. {
  51. uint32_t bits;
  52. int prefix;
  53. struct Allocator* alloc;
  54. };
  55. static int comparePrefixes4(struct Prefix4* a, struct Prefix4* b)
  56. {
  57. if (a->prefix != b->prefix) {
  58. return (a->prefix < b->prefix) ? -1 : 1;
  59. }
  60. if (a->bits != b->bits) {
  61. return (a->bits < b->bits) ? 1 : -1;
  62. }
  63. return 0;
  64. }
  65. #define ArrayList_COMPARE comparePrefixes4
  66. #define ArrayList_TYPE struct Prefix4
  67. #define ArrayList_NAME OfPrefix4
  68. #include "util/ArrayList.h"
  69. struct Prefix46 {
  70. struct ArrayList_OfPrefix4* prefix4;
  71. struct ArrayList_OfPrefix6* prefix6;
  72. };
  73. struct RouteGen_pvt
  74. {
  75. struct RouteGen pub;
  76. struct ArrayList_OfPrefix6* prefixes6;
  77. struct ArrayList_OfPrefix6* localPrefixes6;
  78. struct ArrayList_OfPrefix6* exceptions6;
  79. struct ArrayList_OfPrefix4* prefixes4;
  80. struct ArrayList_OfPrefix4* localPrefixes4;
  81. struct ArrayList_OfPrefix4* exceptions4;
  82. struct Allocator* alloc;
  83. struct Log* log;
  84. Identity
  85. };
  86. static struct Sockaddr* sockaddrForPrefix4(struct Allocator* alloc, struct Prefix4* pfx4)
  87. {
  88. union {
  89. uint32_t addr_be;
  90. uint8_t bytes[4];
  91. } un;
  92. un.addr_be = Endian_hostToBigEndian32(pfx4->bits);
  93. struct Sockaddr* out = Sockaddr_fromBytes(un.bytes, Sockaddr_AF_INET, alloc);
  94. out->flags |= Sockaddr_flags_PREFIX;
  95. out->prefix = pfx4->prefix;
  96. return out;
  97. }
  98. static String* printPrefix4(struct Allocator* alloc, struct Prefix4* pfx4)
  99. {
  100. return String_new(Sockaddr_print(sockaddrForPrefix4(alloc, pfx4), alloc), alloc);
  101. }
  102. static struct Sockaddr* sockaddrForPrefix6(struct Allocator* alloc, struct Prefix6* pfx6)
  103. {
  104. union {
  105. struct {
  106. uint64_t highBits_be;
  107. uint64_t lowBits_be;
  108. } longs;
  109. uint8_t bytes[16];
  110. } un;
  111. un.longs.highBits_be = Endian_hostToBigEndian64(pfx6->highBits);
  112. un.longs.lowBits_be = Endian_hostToBigEndian64(pfx6->lowBits);
  113. struct Sockaddr* out = Sockaddr_fromBytes(un.bytes, Sockaddr_AF_INET6, alloc);
  114. out->flags |= Sockaddr_flags_PREFIX;
  115. out->prefix = pfx6->prefix;
  116. return out;
  117. }
  118. static String* printPrefix6(struct Allocator* alloc, struct Prefix6* pfx6)
  119. {
  120. return String_new(Sockaddr_print(sockaddrForPrefix6(alloc, pfx6), alloc), alloc);
  121. }
  122. static struct Prefix4* sockaddrToPrefix4(struct Sockaddr* sa, struct Allocator* allocator)
  123. {
  124. uint32_t addrNum;
  125. uint8_t* addr;
  126. Assert_true(Sockaddr_getAddress(sa, &addr) == 4);
  127. Bits_memcpy(&addrNum, addr, 4);
  128. struct Allocator* alloc = Allocator_child(allocator);
  129. struct Prefix4* out = Allocator_calloc(alloc, sizeof(struct Prefix4), 1);
  130. out->bits = Endian_bigEndianToHost32(addrNum);
  131. int pfx = Sockaddr_getPrefix(sa);
  132. Assert_true(pfx > -1);
  133. out->prefix = pfx;
  134. out->alloc = alloc;
  135. return out;
  136. }
  137. static struct Prefix6* sockaddrToPrefix6(struct Sockaddr* sa, struct Allocator* allocator)
  138. {
  139. struct {
  140. uint64_t highBits_be;
  141. uint64_t lowBits_be;
  142. } longs;
  143. uint8_t* addr;
  144. Assert_true(Sockaddr_getAddress(sa, &addr) == 16);
  145. Bits_memcpy(&longs, addr, 16);
  146. struct Allocator* alloc = Allocator_child(allocator);
  147. struct Prefix6* out = Allocator_calloc(alloc, sizeof(struct Prefix6), 1);
  148. out->highBits = Endian_bigEndianToHost64(longs.highBits_be);
  149. out->lowBits = Endian_bigEndianToHost64(longs.lowBits_be);
  150. int pfx = Sockaddr_getPrefix(sa);
  151. Assert_true(pfx > -1);
  152. out->prefix = pfx;
  153. out->alloc = alloc;
  154. return out;
  155. }
  156. static void addSomething(struct RouteGen_pvt* rp,
  157. struct Sockaddr* exempt,
  158. struct ArrayList_OfPrefix6* list6,
  159. struct ArrayList_OfPrefix4* list4)
  160. {
  161. if (Sockaddr_getFamily(exempt) == Sockaddr_AF_INET) {
  162. struct Prefix4* p4 = sockaddrToPrefix4(exempt, rp->alloc);
  163. ArrayList_OfPrefix4_add(list4, p4);
  164. } else if (Sockaddr_getFamily(exempt) == Sockaddr_AF_INET6) {
  165. struct Prefix6* p6 = sockaddrToPrefix6(exempt, rp->alloc);
  166. ArrayList_OfPrefix6_add(list6, p6);
  167. } else {
  168. Assert_failure("unexpected addr type");
  169. }
  170. rp->pub.hasUncommittedChanges = true;
  171. }
  172. void RouteGen_addException(struct RouteGen* rg, struct Sockaddr* destination)
  173. {
  174. struct RouteGen_pvt* rp = Identity_check((struct RouteGen_pvt*) rg);
  175. addSomething(rp, destination, rp->exceptions6, rp->exceptions4);
  176. }
  177. void RouteGen_addPrefix(struct RouteGen* rg, struct Sockaddr* destination)
  178. {
  179. struct RouteGen_pvt* rp = Identity_check((struct RouteGen_pvt*) rg);
  180. addSomething(rp, destination, rp->prefixes6, rp->prefixes4);
  181. }
  182. void RouteGen_addLocalPrefix(struct RouteGen* rg, struct Sockaddr* destination)
  183. {
  184. struct RouteGen_pvt* rp = Identity_check((struct RouteGen_pvt*) rg);
  185. addSomething(rp, destination, rp->localPrefixes6, rp->localPrefixes4);
  186. }
  187. static Dict* getSomething(struct RouteGen_pvt* rp,
  188. struct Allocator* alloc,
  189. struct ArrayList_OfPrefix6* list6,
  190. struct ArrayList_OfPrefix4* list4)
  191. {
  192. ArrayList_OfPrefix6_sort(list6);
  193. ArrayList_OfPrefix4_sort(list4);
  194. List* prefixes4 = List_new(alloc);
  195. for (int i = 0; i < list4->length; i++) {
  196. struct Prefix4* pfx4 = ArrayList_OfPrefix4_get(list4, i);
  197. List_addString(prefixes4, printPrefix4(alloc, pfx4), alloc);
  198. }
  199. List* prefixes6 = List_new(alloc);
  200. for (int i = 0; i < list6->length; i++) {
  201. struct Prefix6* pfx6 = ArrayList_OfPrefix6_get(list6, i);
  202. List_addString(prefixes6, printPrefix6(alloc, pfx6), alloc);
  203. }
  204. Dict* out = Dict_new(alloc);
  205. Dict_putList(out, String_new("ipv4", alloc), prefixes4, alloc);
  206. Dict_putList(out, String_new("ipv6", alloc), prefixes6, alloc);
  207. return out;
  208. }
  209. Dict* RouteGen_getPrefixes(struct RouteGen* rg, struct Allocator* alloc)
  210. {
  211. struct RouteGen_pvt* rp = Identity_check((struct RouteGen_pvt*) rg);
  212. return getSomething(rp, alloc, rp->prefixes6, rp->prefixes4);
  213. }
  214. Dict* RouteGen_getLocalPrefixes(struct RouteGen* rg, struct Allocator* alloc)
  215. {
  216. struct RouteGen_pvt* rp = Identity_check((struct RouteGen_pvt*) rg);
  217. return getSomething(rp, alloc, rp->localPrefixes6, rp->localPrefixes4);
  218. }
  219. Dict* RouteGen_getExceptions(struct RouteGen* rg, struct Allocator* alloc)
  220. {
  221. struct RouteGen_pvt* rp = Identity_check((struct RouteGen_pvt*) rg);
  222. return getSomething(rp, alloc, rp->exceptions6, rp->exceptions4);
  223. }
  224. static bool removeSomething(struct RouteGen_pvt* rp,
  225. struct Sockaddr* toRemove,
  226. struct ArrayList_OfPrefix6* list6,
  227. struct ArrayList_OfPrefix4* list4)
  228. {
  229. struct Allocator* tempAlloc = Allocator_child(rp->alloc);
  230. bool ret = false;
  231. if (Sockaddr_getFamily(toRemove) == Sockaddr_AF_INET) {
  232. struct Prefix4* p4 = sockaddrToPrefix4(toRemove, tempAlloc);
  233. for (int i = list4->length - 1; i >= 0; i--) {
  234. struct Prefix4* p42 = ArrayList_OfPrefix4_get(list4, i);
  235. if (!comparePrefixes4(p4, p42)) {
  236. ArrayList_OfPrefix4_remove(list4, i);
  237. ret = true;
  238. }
  239. }
  240. } else if (Sockaddr_getFamily(toRemove) == Sockaddr_AF_INET6) {
  241. struct Prefix6* p6 = sockaddrToPrefix6(toRemove, tempAlloc);
  242. for (int i = list6->length - 1; i >= 0; i--) {
  243. struct Prefix6* p62 = ArrayList_OfPrefix6_get(list6, i);
  244. if (!comparePrefixes6(p6, p62)) {
  245. ArrayList_OfPrefix6_remove(list6, i);
  246. ret = true;
  247. }
  248. }
  249. } else {
  250. Assert_failure("unexpected addr type");
  251. }
  252. Allocator_free(tempAlloc);
  253. return ret;
  254. }
  255. bool RouteGen_removePrefix(struct RouteGen* rg, struct Sockaddr* toRemove)
  256. {
  257. struct RouteGen_pvt* rp = Identity_check((struct RouteGen_pvt*) rg);
  258. return removeSomething(rp, toRemove, rp->prefixes6, rp->prefixes4);
  259. }
  260. bool RouteGen_removeLocalPrefix(struct RouteGen* rg, struct Sockaddr* toRemove)
  261. {
  262. struct RouteGen_pvt* rp = Identity_check((struct RouteGen_pvt*) rg);
  263. return removeSomething(rp, toRemove, rp->localPrefixes6, rp->localPrefixes4);
  264. }
  265. bool RouteGen_removeException(struct RouteGen* rg, struct Sockaddr* toRemove)
  266. {
  267. struct RouteGen_pvt* rp = Identity_check((struct RouteGen_pvt*) rg);
  268. return removeSomething(rp, toRemove, rp->exceptions6, rp->exceptions4);
  269. }
  270. static struct ArrayList_OfPrefix4* invertPrefix4(struct Prefix4* toInvert, struct Allocator* alloc)
  271. {
  272. struct ArrayList_OfPrefix4* result = ArrayList_OfPrefix4_new(alloc);
  273. for (int i = 32 - toInvert->prefix; i < 32; i++) {
  274. struct Prefix4* pfx = Allocator_calloc(alloc, sizeof(struct Prefix4), 1);
  275. pfx->bits = ( toInvert->bits & ((uint32_t)~0 << i) ) ^ ((uint32_t)1 << i);
  276. pfx->prefix = 32 - i;
  277. ArrayList_OfPrefix4_add(result, pfx);
  278. }
  279. return result;
  280. }
  281. static struct ArrayList_OfPrefix6* invertPrefix6(struct Prefix6* toInvert, struct Allocator* alloc)
  282. {
  283. struct ArrayList_OfPrefix6* result = ArrayList_OfPrefix6_new(alloc);
  284. for (int i = 128 - toInvert->prefix; i < 128; i++) {
  285. struct Prefix6* pfx = Allocator_calloc(alloc, sizeof(struct Prefix6), 1);
  286. if (i >= 64) {
  287. pfx->highBits = ( toInvert->highBits & (~((uint64_t)0) << (i-64)) ) ^
  288. (((uint64_t)1) << (i-64));
  289. pfx->lowBits = 0;
  290. } else {
  291. pfx->highBits = toInvert->highBits;
  292. pfx->lowBits = ( toInvert->lowBits & (~((uint64_t)0) << i) ) ^ (((uint64_t)1) << i);
  293. }
  294. pfx->prefix = 128 - i;
  295. ArrayList_OfPrefix6_add(result, pfx);
  296. }
  297. return result;
  298. }
  299. static bool isSubsetOf4(struct Prefix4* isSubset, struct Prefix4* isSuperset)
  300. {
  301. if (isSuperset->prefix > isSubset->prefix) { return false; }
  302. if (isSuperset->prefix >= 32) {
  303. return isSuperset->bits == isSubset->bits;
  304. }
  305. if (!isSuperset->prefix) { return true; }
  306. uint32_t shift = 32 - isSuperset->prefix;
  307. return (isSuperset->bits >> shift) == (isSubset->bits >> shift);
  308. }
  309. static bool isSubsetOf6(struct Prefix6* isSubset, struct Prefix6* isSuperset)
  310. {
  311. if (isSuperset->prefix > isSubset->prefix) { return false; }
  312. if (isSuperset->prefix > 64) {
  313. uint64_t shift = 128 - isSuperset->prefix;
  314. return isSuperset->highBits == isSubset->highBits &&
  315. (isSuperset->lowBits >> shift) == (isSubset->lowBits >> shift);
  316. } else if (isSuperset->prefix) {
  317. uint64_t shift = 64 - isSuperset->prefix;
  318. return (isSuperset->highBits >> shift) == (isSubset->highBits >> shift);
  319. } else {
  320. return true;
  321. }
  322. }
  323. static void mergePrefixSets4(struct ArrayList_OfPrefix4* mergeInto,
  324. struct ArrayList_OfPrefix4* prefixes)
  325. {
  326. struct Prefix4* highestPrefix = NULL;
  327. for (int j = 0; j < prefixes->length; j++) {
  328. struct Prefix4* result = ArrayList_OfPrefix4_get(prefixes, j);
  329. Assert_true(result);
  330. if (!highestPrefix || highestPrefix->prefix < result->prefix) {
  331. highestPrefix = result;
  332. }
  333. }
  334. struct Prefix4 target;
  335. Bits_memcpy(&target, highestPrefix, sizeof(struct Prefix4));
  336. target.bits ^= (target.prefix) ? ((uint32_t)1 << (32 - target.prefix)) : 0;
  337. for (int i = mergeInto->length - 1; i >= 0; i--) {
  338. struct Prefix4* result = ArrayList_OfPrefix4_get(mergeInto, i);
  339. Assert_true(result);
  340. if (isSubsetOf4(&target, result)) {
  341. ArrayList_OfPrefix4_remove(mergeInto, i);
  342. }
  343. }
  344. for (int i = 0; i < prefixes->length; i++) {
  345. bool include = true;
  346. struct Prefix4* toInclude = ArrayList_OfPrefix4_get(prefixes, i);
  347. for (int j = 0; j < mergeInto->length; j++) {
  348. struct Prefix4* test = ArrayList_OfPrefix4_get(mergeInto, j);
  349. if (isSubsetOf4(test, toInclude)) {
  350. include = false;
  351. break;
  352. }
  353. }
  354. if (include) {
  355. ArrayList_OfPrefix4_add(mergeInto, toInclude);
  356. }
  357. }
  358. }
  359. static void mergePrefixSets6(struct ArrayList_OfPrefix6* mergeInto,
  360. struct ArrayList_OfPrefix6* prefixes, struct Allocator* alloc)
  361. {
  362. struct Prefix6* highestPrefix = NULL;
  363. for (int j = 0; j < prefixes->length; j++) {
  364. struct Prefix6* result = ArrayList_OfPrefix6_get(prefixes, j);
  365. Assert_true(result);
  366. if (!highestPrefix || highestPrefix->prefix < result->prefix) {
  367. highestPrefix = result;
  368. }
  369. }
  370. struct Prefix6 target;
  371. Bits_memcpy(&target, highestPrefix, sizeof(struct Prefix6));
  372. if (target.prefix > 64) {
  373. target.lowBits ^= (((uint64_t)1) << (128 - target.prefix));
  374. } else if (target.prefix) {
  375. target.highBits ^= (((uint64_t)1) << (64 - target.prefix));
  376. target.lowBits = 0;
  377. }
  378. for (int i = mergeInto->length - 1; i >= 0; i--) {
  379. struct Prefix6* result = ArrayList_OfPrefix6_get(mergeInto, i);
  380. Assert_true(result);
  381. if (isSubsetOf6(&target, result)) {
  382. ArrayList_OfPrefix6_remove(mergeInto, i);
  383. }
  384. }
  385. for (int i = 0; i < prefixes->length; i++) {
  386. bool include = true;
  387. struct Prefix6* toInclude = ArrayList_OfPrefix6_get(prefixes, i);
  388. for (int j = 0; j < mergeInto->length; j++) {
  389. struct Prefix6* test = ArrayList_OfPrefix6_get(mergeInto, j);
  390. if (isSubsetOf6(test, toInclude)) {
  391. include = false;
  392. break;
  393. }
  394. }
  395. if (include) {
  396. ArrayList_OfPrefix6_add(mergeInto, toInclude);
  397. }
  398. }
  399. }
  400. static struct Prefix4* clonePrefix4(struct Prefix4* original, struct Allocator* alloc)
  401. {
  402. struct Prefix4* clone = Allocator_clone(alloc, original);
  403. clone->alloc = alloc;
  404. return clone;
  405. }
  406. static struct Prefix6* clonePrefix6(struct Prefix6* original, struct Allocator* alloc)
  407. {
  408. struct Prefix6* clone = Allocator_clone(alloc, original);
  409. clone->alloc = alloc;
  410. return clone;
  411. }
  412. static struct ArrayList_OfPrefix4* genPrefixes4(struct ArrayList_OfPrefix4* prefixes,
  413. struct ArrayList_OfPrefix4* exceptions,
  414. struct ArrayList_OfPrefix4* localPrefixes,
  415. struct Allocator* alloc)
  416. {
  417. struct Allocator* tempAlloc = Allocator_child(alloc);
  418. struct ArrayList_OfPrefix4* effectiveLocalPrefixes = ArrayList_OfPrefix4_new(tempAlloc);
  419. for (int i = 0; i < localPrefixes->length; i++) {
  420. bool add = true;
  421. struct Prefix4* localPfx = ArrayList_OfPrefix4_get(localPrefixes, i);
  422. for (int j = 0; j < prefixes->length; j++) {
  423. struct Prefix4* pfx = ArrayList_OfPrefix4_get(prefixes, j);
  424. if (isSubsetOf4(pfx, localPfx)) {
  425. add = false;
  426. break;
  427. }
  428. }
  429. if (add) {
  430. ArrayList_OfPrefix4_add(effectiveLocalPrefixes, localPfx);
  431. }
  432. }
  433. struct ArrayList_OfPrefix4* allPrefixes = ArrayList_OfPrefix4_new(tempAlloc);
  434. for (int i = 0; i < exceptions->length; i++) {
  435. struct Prefix4* pfxToInvert = ArrayList_OfPrefix4_get(exceptions, i);
  436. bool add = true;
  437. for (int j = 0; j < effectiveLocalPrefixes->length; j++) {
  438. struct Prefix4* localPfx = ArrayList_OfPrefix4_get(effectiveLocalPrefixes, j);
  439. if (isSubsetOf4(pfxToInvert, localPfx)) {
  440. add = false;
  441. break;
  442. }
  443. }
  444. if (add) {
  445. struct ArrayList_OfPrefix4* prefixes4 = invertPrefix4(pfxToInvert, tempAlloc);
  446. mergePrefixSets4(allPrefixes, prefixes4);
  447. }
  448. }
  449. for (int i = allPrefixes->length - 2; i >= 0; i--) {
  450. struct Prefix4* pfx = ArrayList_OfPrefix4_get(allPrefixes, i);
  451. struct Prefix4* pfx2 = ArrayList_OfPrefix4_get(allPrefixes, i+1);
  452. if (isSubsetOf4(pfx2, pfx)) {
  453. ArrayList_OfPrefix4_remove(allPrefixes, i+1);
  454. if (i < (allPrefixes->length - 2)) { i++; }
  455. }
  456. }
  457. for (int i = 0; i < prefixes->length; i++) {
  458. struct Prefix4* pfx = ArrayList_OfPrefix4_get(prefixes, i);
  459. int addPrefix = true;
  460. for (int j = allPrefixes->length - 1; j >= 0; j--) {
  461. struct Prefix4* pfx2 = ArrayList_OfPrefix4_get(allPrefixes, j);
  462. if (isSubsetOf4(pfx2, pfx)) {
  463. addPrefix = false;
  464. }
  465. }
  466. if (addPrefix) {
  467. ArrayList_OfPrefix4_add(allPrefixes, pfx);
  468. }
  469. }
  470. ArrayList_OfPrefix4_sort(allPrefixes);
  471. struct ArrayList_OfPrefix4* out = ArrayList_OfPrefix4_new(alloc);
  472. for (int i = 0; i < allPrefixes->length; i++) {
  473. struct Prefix4* pfx = ArrayList_OfPrefix4_get(allPrefixes, i);
  474. for (int j = 0; j < prefixes->length; j++) {
  475. struct Prefix4* pfx2 = ArrayList_OfPrefix4_get(prefixes, j);
  476. if (isSubsetOf4(pfx, pfx2)) {
  477. ArrayList_OfPrefix4_add(out, clonePrefix4(pfx, alloc));
  478. break;
  479. }
  480. }
  481. }
  482. Allocator_free(tempAlloc);
  483. return out;
  484. }
  485. // Annoyingly, this function is *exactly* the same content as genPrefixes4()
  486. // but with evert 4 converted to a 6...
  487. static struct ArrayList_OfPrefix6* genPrefixes6(struct ArrayList_OfPrefix6* prefixes,
  488. struct ArrayList_OfPrefix6* exceptions,
  489. struct ArrayList_OfPrefix6* localPrefixes,
  490. struct Allocator* alloc)
  491. {
  492. struct Allocator* tempAlloc = Allocator_child(alloc);
  493. struct ArrayList_OfPrefix6* effectiveLocalPrefixes = ArrayList_OfPrefix6_new(tempAlloc);
  494. for (int i = 0; i < localPrefixes->length; i++) {
  495. bool add = true;
  496. struct Prefix6* localPfx = ArrayList_OfPrefix6_get(localPrefixes, i);
  497. for (int j = 0; j < prefixes->length; j++) {
  498. struct Prefix6* pfx = ArrayList_OfPrefix6_get(prefixes, j);
  499. if (isSubsetOf6(pfx, localPfx)) {
  500. add = false;
  501. break;
  502. }
  503. }
  504. if (add) {
  505. ArrayList_OfPrefix6_add(effectiveLocalPrefixes, localPfx);
  506. }
  507. }
  508. struct ArrayList_OfPrefix6* allPrefixes = ArrayList_OfPrefix6_new(tempAlloc);
  509. for (int i = 0; i < exceptions->length; i++) {
  510. struct Prefix6* pfxToInvert = ArrayList_OfPrefix6_get(exceptions, i);
  511. bool add = true;
  512. for (int j = 0; j < effectiveLocalPrefixes->length; j++) {
  513. struct Prefix6* localPfx = ArrayList_OfPrefix6_get(effectiveLocalPrefixes, j);
  514. if (isSubsetOf6(pfxToInvert, localPfx)) {
  515. add = false;
  516. break;
  517. }
  518. }
  519. if (add) {
  520. struct ArrayList_OfPrefix6* prefixes6 = invertPrefix6(pfxToInvert, tempAlloc);
  521. mergePrefixSets6(allPrefixes, prefixes6, alloc);
  522. }
  523. }
  524. ArrayList_OfPrefix6_sort(allPrefixes);
  525. for (int i = allPrefixes->length - 2; i >= 0; i--) {
  526. struct Prefix6* pfx = ArrayList_OfPrefix6_get(allPrefixes, i);
  527. struct Prefix6* pfx2 = ArrayList_OfPrefix6_get(allPrefixes, i+1);
  528. if (isSubsetOf6(pfx2, pfx)) {
  529. ArrayList_OfPrefix6_remove(allPrefixes, i+1);
  530. if (i < (allPrefixes->length - 2)) { i++; }
  531. }
  532. }
  533. for (int i = 0; i < prefixes->length; i++) {
  534. struct Prefix6* pfx = ArrayList_OfPrefix6_get(prefixes, i);
  535. int addPrefix = true;
  536. for (int j = allPrefixes->length - 1; j >= 0; j--) {
  537. struct Prefix6* pfx2 = ArrayList_OfPrefix6_get(allPrefixes, j);
  538. if (isSubsetOf6(pfx2, pfx)) {
  539. addPrefix = false;
  540. }
  541. }
  542. if (addPrefix) {
  543. ArrayList_OfPrefix6_add(allPrefixes, pfx);
  544. }
  545. }
  546. ArrayList_OfPrefix6_sort(allPrefixes);
  547. struct ArrayList_OfPrefix6* out = ArrayList_OfPrefix6_new(alloc);
  548. for (int i = 0; i < allPrefixes->length; i++) {
  549. struct Prefix6* pfx = ArrayList_OfPrefix6_get(allPrefixes, i);
  550. for (int j = 0; j < prefixes->length; j++) {
  551. struct Prefix6* pfx2 = ArrayList_OfPrefix6_get(prefixes, j);
  552. if (isSubsetOf6(pfx, pfx2)) {
  553. ArrayList_OfPrefix6_add(out, clonePrefix6(pfx, alloc));
  554. break;
  555. }
  556. }
  557. }
  558. Allocator_free(tempAlloc);
  559. return out;
  560. }
  561. static struct Prefix46* getGeneratedRoutes(struct RouteGen_pvt* rp, struct Allocator* alloc)
  562. {
  563. struct Prefix46* out = Allocator_calloc(alloc, sizeof(struct Prefix46), 1);
  564. if (rp->prefixes4->length > 0) {
  565. out->prefix4 = genPrefixes4(rp->prefixes4, rp->exceptions4, rp->localPrefixes4, alloc);
  566. } else {
  567. out->prefix4 = ArrayList_OfPrefix4_new(alloc);
  568. }
  569. if (rp->prefixes6->length > 0) {
  570. out->prefix6 = genPrefixes6(rp->prefixes6, rp->exceptions6, rp->localPrefixes6, alloc);
  571. } else {
  572. out->prefix6 = ArrayList_OfPrefix6_new(alloc);
  573. }
  574. return out;
  575. }
  576. Dict* RouteGen_getGeneratedRoutes(struct RouteGen* rg, struct Allocator* alloc)
  577. {
  578. struct RouteGen_pvt* rp = Identity_check((struct RouteGen_pvt*) rg);
  579. struct Prefix46* p46 = getGeneratedRoutes(rp, alloc);
  580. return getSomething(rp, alloc, p46->prefix6, p46->prefix4);
  581. }
  582. Er_DEFUN(void RouteGen_commit(struct RouteGen* rg,
  583. const char* tunName,
  584. struct Allocator* tempAlloc))
  585. {
  586. struct RouteGen_pvt* rp = Identity_check((struct RouteGen_pvt*) rg);
  587. struct Prefix46* p46 = getGeneratedRoutes(rp, tempAlloc);
  588. struct Sockaddr** prefixSet =
  589. Allocator_calloc(tempAlloc, sizeof(char*), p46->prefix4->length + p46->prefix6->length);
  590. int prefixNum = 0;
  591. for (int i = 0; i < p46->prefix4->length; i++) {
  592. struct Prefix4* pfx4 = ArrayList_OfPrefix4_get(p46->prefix4, i);
  593. prefixSet[prefixNum++] = sockaddrForPrefix4(tempAlloc, pfx4);
  594. }
  595. for (int i = 0; i < p46->prefix6->length; i++) {
  596. struct Prefix6* pfx6 = ArrayList_OfPrefix6_get(p46->prefix6, i);
  597. prefixSet[prefixNum++] = sockaddrForPrefix6(tempAlloc, pfx6);
  598. }
  599. Assert_true(prefixNum == p46->prefix4->length + p46->prefix6->length);
  600. Er(NetDev_setRoutes(tunName, prefixSet, prefixNum, rp->log, tempAlloc));
  601. rp->pub.hasUncommittedChanges = false;
  602. Er_ret();
  603. }
  604. static void setupDefaultLocalPrefixes(struct RouteGen_pvt* rp)
  605. {
  606. struct Sockaddr_storage ss;
  607. #define ADD_PREFIX(str) \
  608. Assert_true(!Sockaddr_parse(str, &ss)); \
  609. RouteGen_addLocalPrefix(&rp->pub, &ss.addr)
  610. ADD_PREFIX("fe80::/10");
  611. ADD_PREFIX("fd00::/8");
  612. ADD_PREFIX("10.0.0.0/8");
  613. ADD_PREFIX("172.16.0.0/12");
  614. ADD_PREFIX("192.168.0.0/16");
  615. ADD_PREFIX("127.0.0.0/8");
  616. #undef ADD_PREFIX
  617. }
  618. struct RouteGen* RouteGen_new(struct Allocator* allocator, struct Log* log)
  619. {
  620. struct Allocator* alloc = Allocator_child(allocator);
  621. struct RouteGen_pvt* rp = Allocator_calloc(alloc, sizeof(struct RouteGen_pvt), 1);
  622. rp->prefixes6 = ArrayList_OfPrefix6_new(alloc);
  623. rp->localPrefixes6 = ArrayList_OfPrefix6_new(alloc);
  624. rp->exceptions6 = ArrayList_OfPrefix6_new(alloc);
  625. rp->prefixes4 = ArrayList_OfPrefix4_new(alloc);
  626. rp->localPrefixes4 = ArrayList_OfPrefix4_new(alloc);
  627. rp->exceptions4 = ArrayList_OfPrefix4_new(alloc);
  628. rp->log = log;
  629. rp->alloc = alloc;
  630. Identity_set(rp);
  631. setupDefaultLocalPrefixes(rp);
  632. return &rp->pub;
  633. }