RouteGen.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. if (prefixes->length == 0) {
  328. return;
  329. }
  330. for (int j = 0; j < prefixes->length; j++) {
  331. struct Prefix4* result = ArrayList_OfPrefix4_get(prefixes, j);
  332. Assert_true(result);
  333. if (!highestPrefix || highestPrefix->prefix < result->prefix) {
  334. highestPrefix = result;
  335. }
  336. }
  337. struct Prefix4 target;
  338. Bits_memcpy(&target, highestPrefix, sizeof(struct Prefix4));
  339. target.bits ^= (target.prefix) ? ((uint32_t)1 << (32 - target.prefix)) : 0;
  340. for (int i = mergeInto->length - 1; i >= 0; i--) {
  341. struct Prefix4* result = ArrayList_OfPrefix4_get(mergeInto, i);
  342. Assert_true(result);
  343. if (isSubsetOf4(&target, result)) {
  344. ArrayList_OfPrefix4_remove(mergeInto, i);
  345. }
  346. }
  347. for (int i = 0; i < prefixes->length; i++) {
  348. bool include = true;
  349. struct Prefix4* toInclude = ArrayList_OfPrefix4_get(prefixes, i);
  350. for (int j = 0; j < mergeInto->length; j++) {
  351. struct Prefix4* test = ArrayList_OfPrefix4_get(mergeInto, j);
  352. if (isSubsetOf4(test, toInclude)) {
  353. include = false;
  354. break;
  355. }
  356. }
  357. if (include) {
  358. ArrayList_OfPrefix4_add(mergeInto, toInclude);
  359. }
  360. }
  361. }
  362. static void mergePrefixSets6(struct ArrayList_OfPrefix6* mergeInto,
  363. struct ArrayList_OfPrefix6* prefixes, struct Allocator* alloc)
  364. {
  365. struct Prefix6* highestPrefix = NULL;
  366. for (int j = 0; j < prefixes->length; j++) {
  367. struct Prefix6* result = ArrayList_OfPrefix6_get(prefixes, j);
  368. Assert_true(result);
  369. if (!highestPrefix || highestPrefix->prefix < result->prefix) {
  370. highestPrefix = result;
  371. }
  372. }
  373. struct Prefix6 target;
  374. Bits_memcpy(&target, highestPrefix, sizeof(struct Prefix6));
  375. if (target.prefix > 64) {
  376. target.lowBits ^= (((uint64_t)1) << (128 - target.prefix));
  377. } else if (target.prefix) {
  378. target.highBits ^= (((uint64_t)1) << (64 - target.prefix));
  379. target.lowBits = 0;
  380. }
  381. for (int i = mergeInto->length - 1; i >= 0; i--) {
  382. struct Prefix6* result = ArrayList_OfPrefix6_get(mergeInto, i);
  383. Assert_true(result);
  384. if (isSubsetOf6(&target, result)) {
  385. ArrayList_OfPrefix6_remove(mergeInto, i);
  386. }
  387. }
  388. for (int i = 0; i < prefixes->length; i++) {
  389. bool include = true;
  390. struct Prefix6* toInclude = ArrayList_OfPrefix6_get(prefixes, i);
  391. for (int j = 0; j < mergeInto->length; j++) {
  392. struct Prefix6* test = ArrayList_OfPrefix6_get(mergeInto, j);
  393. if (isSubsetOf6(test, toInclude)) {
  394. include = false;
  395. break;
  396. }
  397. }
  398. if (include) {
  399. ArrayList_OfPrefix6_add(mergeInto, toInclude);
  400. }
  401. }
  402. }
  403. static struct Prefix4* clonePrefix4(struct Prefix4* original, struct Allocator* alloc)
  404. {
  405. struct Prefix4* clone = Allocator_clone(alloc, original);
  406. clone->alloc = alloc;
  407. return clone;
  408. }
  409. static struct Prefix6* clonePrefix6(struct Prefix6* original, struct Allocator* alloc)
  410. {
  411. struct Prefix6* clone = Allocator_clone(alloc, original);
  412. clone->alloc = alloc;
  413. return clone;
  414. }
  415. static struct ArrayList_OfPrefix4* mkPseudoDefault4(struct Allocator* alloc)
  416. {
  417. struct Prefix4* pfxs = Allocator_calloc(alloc, sizeof(struct Prefix4), 2);
  418. pfxs[0].prefix = 1;
  419. pfxs[1].prefix = 1;
  420. pfxs[1].bits = 0x80000000;
  421. struct ArrayList_OfPrefix4* out = ArrayList_OfPrefix4_new(alloc);
  422. ArrayList_OfPrefix4_add(out, &pfxs[0]);
  423. ArrayList_OfPrefix4_add(out, &pfxs[1]);
  424. return out;
  425. }
  426. static struct ArrayList_OfPrefix6* mkPseudoDefault6(struct Allocator* alloc)
  427. {
  428. struct Prefix6* pfxs = Allocator_calloc(alloc, sizeof(struct Prefix6), 2);
  429. pfxs[0].prefix = 1;
  430. pfxs[1].prefix = 1;
  431. pfxs[1].highBits = 0x8000000000000000ull;
  432. struct ArrayList_OfPrefix6* out = ArrayList_OfPrefix6_new(alloc);
  433. ArrayList_OfPrefix6_add(out, &pfxs[0]);
  434. ArrayList_OfPrefix6_add(out, &pfxs[1]);
  435. return out;
  436. }
  437. static bool isDefaultRoute4(struct ArrayList_OfPrefix4* prefixes)
  438. {
  439. if (prefixes->length != 1) { return false; }
  440. struct Prefix4* pfx = ArrayList_OfPrefix4_get(prefixes, 0);
  441. return pfx->prefix == 0;
  442. }
  443. static bool isDefaultRoute6(struct ArrayList_OfPrefix6* prefixes)
  444. {
  445. if (prefixes->length != 1) { return false; }
  446. struct Prefix6* pfx = ArrayList_OfPrefix6_get(prefixes, 0);
  447. return pfx->prefix == 0;
  448. }
  449. static struct ArrayList_OfPrefix4* genPrefixes4(struct ArrayList_OfPrefix4* prefixes,
  450. struct ArrayList_OfPrefix4* exceptions,
  451. struct ArrayList_OfPrefix4* localPrefixes,
  452. struct Allocator* alloc)
  453. {
  454. struct Allocator* tempAlloc = Allocator_child(alloc);
  455. struct ArrayList_OfPrefix4* effectiveLocalPrefixes = ArrayList_OfPrefix4_new(tempAlloc);
  456. for (int i = 0; i < localPrefixes->length; i++) {
  457. bool add = true;
  458. struct Prefix4* localPfx = ArrayList_OfPrefix4_get(localPrefixes, i);
  459. for (int j = 0; j < prefixes->length; j++) {
  460. struct Prefix4* pfx = ArrayList_OfPrefix4_get(prefixes, j);
  461. if (isSubsetOf4(pfx, localPfx)) {
  462. add = false;
  463. break;
  464. }
  465. }
  466. if (add) {
  467. ArrayList_OfPrefix4_add(effectiveLocalPrefixes, localPfx);
  468. }
  469. }
  470. struct ArrayList_OfPrefix4* allPrefixes = ArrayList_OfPrefix4_new(tempAlloc);
  471. for (int i = 0; i < exceptions->length; i++) {
  472. struct Prefix4* pfxToInvert = ArrayList_OfPrefix4_get(exceptions, i);
  473. bool add = true;
  474. for (int j = 0; j < effectiveLocalPrefixes->length; j++) {
  475. struct Prefix4* localPfx = ArrayList_OfPrefix4_get(effectiveLocalPrefixes, j);
  476. if (isSubsetOf4(pfxToInvert, localPfx)) {
  477. add = false;
  478. break;
  479. }
  480. }
  481. if (add) {
  482. struct ArrayList_OfPrefix4* prefixes4 = invertPrefix4(pfxToInvert, tempAlloc);
  483. mergePrefixSets4(allPrefixes, prefixes4);
  484. }
  485. }
  486. for (int i = allPrefixes->length - 2; i >= 0; i--) {
  487. struct Prefix4* pfx = ArrayList_OfPrefix4_get(allPrefixes, i);
  488. struct Prefix4* pfx2 = ArrayList_OfPrefix4_get(allPrefixes, i+1);
  489. if (isSubsetOf4(pfx2, pfx)) {
  490. ArrayList_OfPrefix4_remove(allPrefixes, i+1);
  491. if (i < (allPrefixes->length - 2)) { i++; }
  492. }
  493. }
  494. for (int i = 0; i < prefixes->length; i++) {
  495. struct Prefix4* pfx = ArrayList_OfPrefix4_get(prefixes, i);
  496. int addPrefix = true;
  497. for (int j = allPrefixes->length - 1; j >= 0; j--) {
  498. struct Prefix4* pfx2 = ArrayList_OfPrefix4_get(allPrefixes, j);
  499. if (isSubsetOf4(pfx2, pfx)) {
  500. addPrefix = false;
  501. }
  502. }
  503. if (addPrefix) {
  504. ArrayList_OfPrefix4_add(allPrefixes, pfx);
  505. }
  506. }
  507. ArrayList_OfPrefix4_sort(allPrefixes);
  508. struct ArrayList_OfPrefix4* out = ArrayList_OfPrefix4_new(alloc);
  509. for (int i = 0; i < allPrefixes->length; i++) {
  510. struct Prefix4* pfx = ArrayList_OfPrefix4_get(allPrefixes, i);
  511. for (int j = 0; j < prefixes->length; j++) {
  512. struct Prefix4* pfx2 = ArrayList_OfPrefix4_get(prefixes, j);
  513. if (isSubsetOf4(pfx, pfx2)) {
  514. ArrayList_OfPrefix4_add(out, clonePrefix4(pfx, alloc));
  515. break;
  516. }
  517. }
  518. }
  519. Allocator_free(tempAlloc);
  520. return out;
  521. }
  522. // Annoyingly, this function is *exactly* the same content as genPrefixes4()
  523. // but with evert 4 converted to a 6...
  524. static struct ArrayList_OfPrefix6* genPrefixes6(struct ArrayList_OfPrefix6* prefixes,
  525. struct ArrayList_OfPrefix6* exceptions,
  526. struct ArrayList_OfPrefix6* localPrefixes,
  527. struct Allocator* alloc)
  528. {
  529. struct Allocator* tempAlloc = Allocator_child(alloc);
  530. struct ArrayList_OfPrefix6* effectiveLocalPrefixes = ArrayList_OfPrefix6_new(tempAlloc);
  531. for (int i = 0; i < localPrefixes->length; i++) {
  532. bool add = true;
  533. struct Prefix6* localPfx = ArrayList_OfPrefix6_get(localPrefixes, i);
  534. for (int j = 0; j < prefixes->length; j++) {
  535. struct Prefix6* pfx = ArrayList_OfPrefix6_get(prefixes, j);
  536. if (isSubsetOf6(pfx, localPfx)) {
  537. add = false;
  538. break;
  539. }
  540. }
  541. if (add) {
  542. ArrayList_OfPrefix6_add(effectiveLocalPrefixes, localPfx);
  543. }
  544. }
  545. struct ArrayList_OfPrefix6* allPrefixes = ArrayList_OfPrefix6_new(tempAlloc);
  546. for (int i = 0; i < exceptions->length; i++) {
  547. struct Prefix6* pfxToInvert = ArrayList_OfPrefix6_get(exceptions, i);
  548. bool add = true;
  549. for (int j = 0; j < effectiveLocalPrefixes->length; j++) {
  550. struct Prefix6* localPfx = ArrayList_OfPrefix6_get(effectiveLocalPrefixes, j);
  551. if (isSubsetOf6(pfxToInvert, localPfx)) {
  552. add = false;
  553. break;
  554. }
  555. }
  556. if (add) {
  557. struct ArrayList_OfPrefix6* prefixes6 = invertPrefix6(pfxToInvert, tempAlloc);
  558. mergePrefixSets6(allPrefixes, prefixes6, alloc);
  559. }
  560. }
  561. ArrayList_OfPrefix6_sort(allPrefixes);
  562. for (int i = allPrefixes->length - 2; i >= 0; i--) {
  563. struct Prefix6* pfx = ArrayList_OfPrefix6_get(allPrefixes, i);
  564. struct Prefix6* pfx2 = ArrayList_OfPrefix6_get(allPrefixes, i+1);
  565. if (isSubsetOf6(pfx2, pfx)) {
  566. ArrayList_OfPrefix6_remove(allPrefixes, i+1);
  567. if (i < (allPrefixes->length - 2)) { i++; }
  568. }
  569. }
  570. for (int i = 0; i < prefixes->length; i++) {
  571. struct Prefix6* pfx = ArrayList_OfPrefix6_get(prefixes, i);
  572. int addPrefix = true;
  573. for (int j = allPrefixes->length - 1; j >= 0; j--) {
  574. struct Prefix6* pfx2 = ArrayList_OfPrefix6_get(allPrefixes, j);
  575. if (isSubsetOf6(pfx2, pfx)) {
  576. addPrefix = false;
  577. }
  578. }
  579. if (addPrefix) {
  580. ArrayList_OfPrefix6_add(allPrefixes, pfx);
  581. }
  582. }
  583. ArrayList_OfPrefix6_sort(allPrefixes);
  584. struct ArrayList_OfPrefix6* out = ArrayList_OfPrefix6_new(alloc);
  585. for (int i = 0; i < allPrefixes->length; i++) {
  586. struct Prefix6* pfx = ArrayList_OfPrefix6_get(allPrefixes, i);
  587. for (int j = 0; j < prefixes->length; j++) {
  588. struct Prefix6* pfx2 = ArrayList_OfPrefix6_get(prefixes, j);
  589. if (isSubsetOf6(pfx, pfx2)) {
  590. ArrayList_OfPrefix6_add(out, clonePrefix6(pfx, alloc));
  591. break;
  592. }
  593. }
  594. }
  595. Allocator_free(tempAlloc);
  596. return out;
  597. }
  598. static struct Prefix46* getGeneratedRoutes(struct RouteGen_pvt* rp, struct Allocator* alloc)
  599. {
  600. struct Prefix46* out = Allocator_calloc(alloc, sizeof(struct Prefix46), 1);
  601. if (rp->prefixes4->length > 0) {
  602. out->prefix4 = genPrefixes4(rp->prefixes4, rp->exceptions4, rp->localPrefixes4, alloc);
  603. if (isDefaultRoute4(out->prefix4)) {
  604. out->prefix4 = mkPseudoDefault4(alloc);
  605. }
  606. } else {
  607. out->prefix4 = ArrayList_OfPrefix4_new(alloc);
  608. }
  609. if (rp->prefixes6->length > 0) {
  610. out->prefix6 = genPrefixes6(rp->prefixes6, rp->exceptions6, rp->localPrefixes6, alloc);
  611. if (isDefaultRoute6(out->prefix6)) {
  612. out->prefix6 = mkPseudoDefault6(alloc);
  613. }
  614. } else {
  615. out->prefix6 = ArrayList_OfPrefix6_new(alloc);
  616. }
  617. return out;
  618. }
  619. Dict* RouteGen_getGeneratedRoutes(struct RouteGen* rg, struct Allocator* alloc)
  620. {
  621. struct RouteGen_pvt* rp = Identity_check((struct RouteGen_pvt*) rg);
  622. struct Prefix46* p46 = getGeneratedRoutes(rp, alloc);
  623. return getSomething(rp, alloc, p46->prefix6, p46->prefix4);
  624. }
  625. Er_DEFUN(void RouteGen_commit(struct RouteGen* rg,
  626. const char* tunName,
  627. struct Allocator* tempAlloc))
  628. {
  629. struct RouteGen_pvt* rp = Identity_check((struct RouteGen_pvt*) rg);
  630. struct Prefix46* p46 = getGeneratedRoutes(rp, tempAlloc);
  631. struct Sockaddr** prefixSet =
  632. Allocator_calloc(tempAlloc, sizeof(char*), p46->prefix4->length + p46->prefix6->length);
  633. int prefixNum = 0;
  634. for (int i = 0; i < p46->prefix4->length; i++) {
  635. struct Prefix4* pfx4 = ArrayList_OfPrefix4_get(p46->prefix4, i);
  636. prefixSet[prefixNum++] = sockaddrForPrefix4(tempAlloc, pfx4);
  637. }
  638. for (int i = 0; i < p46->prefix6->length; i++) {
  639. struct Prefix6* pfx6 = ArrayList_OfPrefix6_get(p46->prefix6, i);
  640. prefixSet[prefixNum++] = sockaddrForPrefix6(tempAlloc, pfx6);
  641. }
  642. Assert_true(prefixNum == p46->prefix4->length + p46->prefix6->length);
  643. Er(NetDev_setRoutes(tunName, prefixSet, prefixNum, rp->log, tempAlloc));
  644. rp->pub.hasUncommittedChanges = false;
  645. Er_ret();
  646. }
  647. static void setupDefaultLocalPrefixes(struct RouteGen_pvt* rp)
  648. {
  649. struct Sockaddr_storage ss;
  650. #define ADD_PREFIX(str) \
  651. Assert_true(!Sockaddr_parse(str, &ss)); \
  652. RouteGen_addLocalPrefix(&rp->pub, &ss.addr)
  653. ADD_PREFIX("fe80::/10");
  654. ADD_PREFIX("fd00::/8");
  655. ADD_PREFIX("10.0.0.0/8");
  656. ADD_PREFIX("172.16.0.0/12");
  657. ADD_PREFIX("192.168.0.0/16");
  658. ADD_PREFIX("127.0.0.0/8");
  659. #undef ADD_PREFIX
  660. }
  661. struct RouteGen* RouteGen_new(struct Allocator* allocator, struct Log* log)
  662. {
  663. struct Allocator* alloc = Allocator_child(allocator);
  664. struct RouteGen_pvt* rp = Allocator_calloc(alloc, sizeof(struct RouteGen_pvt), 1);
  665. rp->prefixes6 = ArrayList_OfPrefix6_new(alloc);
  666. rp->localPrefixes6 = ArrayList_OfPrefix6_new(alloc);
  667. rp->exceptions6 = ArrayList_OfPrefix6_new(alloc);
  668. rp->prefixes4 = ArrayList_OfPrefix4_new(alloc);
  669. rp->localPrefixes4 = ArrayList_OfPrefix4_new(alloc);
  670. rp->exceptions4 = ArrayList_OfPrefix4_new(alloc);
  671. rp->log = log;
  672. rp->alloc = alloc;
  673. Identity_set(rp);
  674. setupDefaultLocalPrefixes(rp);
  675. return &rp->pub;
  676. }