RouteGen.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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* mkPseudoDefault4(struct Allocator* alloc)
  413. {
  414. struct Prefix4* pfxs = Allocator_calloc(alloc, sizeof(struct Prefix4), 2);
  415. pfxs[0].prefix = 1;
  416. pfxs[1].prefix = 1;
  417. pfxs[1].bits = 0x80000000;
  418. struct ArrayList_OfPrefix4* out = ArrayList_OfPrefix4_new(alloc);
  419. ArrayList_OfPrefix4_add(out, &pfxs[0]);
  420. ArrayList_OfPrefix4_add(out, &pfxs[1]);
  421. return out;
  422. }
  423. static struct ArrayList_OfPrefix6* mkPseudoDefault6(struct Allocator* alloc)
  424. {
  425. struct Prefix6* pfxs = Allocator_calloc(alloc, sizeof(struct Prefix6), 2);
  426. pfxs[0].prefix = 1;
  427. pfxs[1].prefix = 1;
  428. pfxs[1].highBits = 0x8000000000000000ull;
  429. struct ArrayList_OfPrefix6* out = ArrayList_OfPrefix6_new(alloc);
  430. ArrayList_OfPrefix6_add(out, &pfxs[0]);
  431. ArrayList_OfPrefix6_add(out, &pfxs[1]);
  432. return out;
  433. }
  434. static bool isDefaultRoute4(struct ArrayList_OfPrefix4* prefixes)
  435. {
  436. if (prefixes->length != 1) { return false; }
  437. struct Prefix4* pfx = ArrayList_OfPrefix4_get(prefixes, 0);
  438. return pfx->prefix == 0;
  439. }
  440. static bool isDefaultRoute6(struct ArrayList_OfPrefix6* prefixes)
  441. {
  442. if (prefixes->length != 1) { return false; }
  443. struct Prefix6* pfx = ArrayList_OfPrefix6_get(prefixes, 0);
  444. return pfx->prefix == 0;
  445. }
  446. static struct ArrayList_OfPrefix4* genPrefixes4(struct ArrayList_OfPrefix4* prefixes,
  447. struct ArrayList_OfPrefix4* exceptions,
  448. struct ArrayList_OfPrefix4* localPrefixes,
  449. struct Allocator* alloc)
  450. {
  451. struct Allocator* tempAlloc = Allocator_child(alloc);
  452. struct ArrayList_OfPrefix4* effectiveLocalPrefixes = ArrayList_OfPrefix4_new(tempAlloc);
  453. for (int i = 0; i < localPrefixes->length; i++) {
  454. bool add = true;
  455. struct Prefix4* localPfx = ArrayList_OfPrefix4_get(localPrefixes, i);
  456. for (int j = 0; j < prefixes->length; j++) {
  457. struct Prefix4* pfx = ArrayList_OfPrefix4_get(prefixes, j);
  458. if (isSubsetOf4(pfx, localPfx)) {
  459. add = false;
  460. break;
  461. }
  462. }
  463. if (add) {
  464. ArrayList_OfPrefix4_add(effectiveLocalPrefixes, localPfx);
  465. }
  466. }
  467. struct ArrayList_OfPrefix4* allPrefixes = ArrayList_OfPrefix4_new(tempAlloc);
  468. for (int i = 0; i < exceptions->length; i++) {
  469. struct Prefix4* pfxToInvert = ArrayList_OfPrefix4_get(exceptions, i);
  470. bool add = true;
  471. for (int j = 0; j < effectiveLocalPrefixes->length; j++) {
  472. struct Prefix4* localPfx = ArrayList_OfPrefix4_get(effectiveLocalPrefixes, j);
  473. if (isSubsetOf4(pfxToInvert, localPfx)) {
  474. add = false;
  475. break;
  476. }
  477. }
  478. if (add) {
  479. struct ArrayList_OfPrefix4* prefixes4 = invertPrefix4(pfxToInvert, tempAlloc);
  480. mergePrefixSets4(allPrefixes, prefixes4);
  481. }
  482. }
  483. for (int i = allPrefixes->length - 2; i >= 0; i--) {
  484. struct Prefix4* pfx = ArrayList_OfPrefix4_get(allPrefixes, i);
  485. struct Prefix4* pfx2 = ArrayList_OfPrefix4_get(allPrefixes, i+1);
  486. if (isSubsetOf4(pfx2, pfx)) {
  487. ArrayList_OfPrefix4_remove(allPrefixes, i+1);
  488. if (i < (allPrefixes->length - 2)) { i++; }
  489. }
  490. }
  491. for (int i = 0; i < prefixes->length; i++) {
  492. struct Prefix4* pfx = ArrayList_OfPrefix4_get(prefixes, i);
  493. int addPrefix = true;
  494. for (int j = allPrefixes->length - 1; j >= 0; j--) {
  495. struct Prefix4* pfx2 = ArrayList_OfPrefix4_get(allPrefixes, j);
  496. if (isSubsetOf4(pfx2, pfx)) {
  497. addPrefix = false;
  498. }
  499. }
  500. if (addPrefix) {
  501. ArrayList_OfPrefix4_add(allPrefixes, pfx);
  502. }
  503. }
  504. ArrayList_OfPrefix4_sort(allPrefixes);
  505. struct ArrayList_OfPrefix4* out = ArrayList_OfPrefix4_new(alloc);
  506. for (int i = 0; i < allPrefixes->length; i++) {
  507. struct Prefix4* pfx = ArrayList_OfPrefix4_get(allPrefixes, i);
  508. for (int j = 0; j < prefixes->length; j++) {
  509. struct Prefix4* pfx2 = ArrayList_OfPrefix4_get(prefixes, j);
  510. if (isSubsetOf4(pfx, pfx2)) {
  511. ArrayList_OfPrefix4_add(out, clonePrefix4(pfx, alloc));
  512. break;
  513. }
  514. }
  515. }
  516. Allocator_free(tempAlloc);
  517. return out;
  518. }
  519. // Annoyingly, this function is *exactly* the same content as genPrefixes4()
  520. // but with evert 4 converted to a 6...
  521. static struct ArrayList_OfPrefix6* genPrefixes6(struct ArrayList_OfPrefix6* prefixes,
  522. struct ArrayList_OfPrefix6* exceptions,
  523. struct ArrayList_OfPrefix6* localPrefixes,
  524. struct Allocator* alloc)
  525. {
  526. struct Allocator* tempAlloc = Allocator_child(alloc);
  527. struct ArrayList_OfPrefix6* effectiveLocalPrefixes = ArrayList_OfPrefix6_new(tempAlloc);
  528. for (int i = 0; i < localPrefixes->length; i++) {
  529. bool add = true;
  530. struct Prefix6* localPfx = ArrayList_OfPrefix6_get(localPrefixes, i);
  531. for (int j = 0; j < prefixes->length; j++) {
  532. struct Prefix6* pfx = ArrayList_OfPrefix6_get(prefixes, j);
  533. if (isSubsetOf6(pfx, localPfx)) {
  534. add = false;
  535. break;
  536. }
  537. }
  538. if (add) {
  539. ArrayList_OfPrefix6_add(effectiveLocalPrefixes, localPfx);
  540. }
  541. }
  542. struct ArrayList_OfPrefix6* allPrefixes = ArrayList_OfPrefix6_new(tempAlloc);
  543. for (int i = 0; i < exceptions->length; i++) {
  544. struct Prefix6* pfxToInvert = ArrayList_OfPrefix6_get(exceptions, i);
  545. bool add = true;
  546. for (int j = 0; j < effectiveLocalPrefixes->length; j++) {
  547. struct Prefix6* localPfx = ArrayList_OfPrefix6_get(effectiveLocalPrefixes, j);
  548. if (isSubsetOf6(pfxToInvert, localPfx)) {
  549. add = false;
  550. break;
  551. }
  552. }
  553. if (add) {
  554. struct ArrayList_OfPrefix6* prefixes6 = invertPrefix6(pfxToInvert, tempAlloc);
  555. mergePrefixSets6(allPrefixes, prefixes6, alloc);
  556. }
  557. }
  558. ArrayList_OfPrefix6_sort(allPrefixes);
  559. for (int i = allPrefixes->length - 2; i >= 0; i--) {
  560. struct Prefix6* pfx = ArrayList_OfPrefix6_get(allPrefixes, i);
  561. struct Prefix6* pfx2 = ArrayList_OfPrefix6_get(allPrefixes, i+1);
  562. if (isSubsetOf6(pfx2, pfx)) {
  563. ArrayList_OfPrefix6_remove(allPrefixes, i+1);
  564. if (i < (allPrefixes->length - 2)) { i++; }
  565. }
  566. }
  567. for (int i = 0; i < prefixes->length; i++) {
  568. struct Prefix6* pfx = ArrayList_OfPrefix6_get(prefixes, i);
  569. int addPrefix = true;
  570. for (int j = allPrefixes->length - 1; j >= 0; j--) {
  571. struct Prefix6* pfx2 = ArrayList_OfPrefix6_get(allPrefixes, j);
  572. if (isSubsetOf6(pfx2, pfx)) {
  573. addPrefix = false;
  574. }
  575. }
  576. if (addPrefix) {
  577. ArrayList_OfPrefix6_add(allPrefixes, pfx);
  578. }
  579. }
  580. ArrayList_OfPrefix6_sort(allPrefixes);
  581. struct ArrayList_OfPrefix6* out = ArrayList_OfPrefix6_new(alloc);
  582. for (int i = 0; i < allPrefixes->length; i++) {
  583. struct Prefix6* pfx = ArrayList_OfPrefix6_get(allPrefixes, i);
  584. for (int j = 0; j < prefixes->length; j++) {
  585. struct Prefix6* pfx2 = ArrayList_OfPrefix6_get(prefixes, j);
  586. if (isSubsetOf6(pfx, pfx2)) {
  587. ArrayList_OfPrefix6_add(out, clonePrefix6(pfx, alloc));
  588. break;
  589. }
  590. }
  591. }
  592. Allocator_free(tempAlloc);
  593. return out;
  594. }
  595. static struct Prefix46* getGeneratedRoutes(struct RouteGen_pvt* rp, struct Allocator* alloc)
  596. {
  597. struct Prefix46* out = Allocator_calloc(alloc, sizeof(struct Prefix46), 1);
  598. if (rp->prefixes4->length > 0) {
  599. out->prefix4 = genPrefixes4(rp->prefixes4, rp->exceptions4, rp->localPrefixes4, alloc);
  600. if (isDefaultRoute4(out->prefix4)) {
  601. out->prefix4 = mkPseudoDefault4(alloc);
  602. }
  603. } else {
  604. out->prefix4 = ArrayList_OfPrefix4_new(alloc);
  605. }
  606. if (rp->prefixes6->length > 0) {
  607. out->prefix6 = genPrefixes6(rp->prefixes6, rp->exceptions6, rp->localPrefixes6, alloc);
  608. if (isDefaultRoute6(out->prefix6)) {
  609. out->prefix6 = mkPseudoDefault6(alloc);
  610. }
  611. } else {
  612. out->prefix6 = ArrayList_OfPrefix6_new(alloc);
  613. }
  614. return out;
  615. }
  616. Dict* RouteGen_getGeneratedRoutes(struct RouteGen* rg, struct Allocator* alloc)
  617. {
  618. struct RouteGen_pvt* rp = Identity_check((struct RouteGen_pvt*) rg);
  619. struct Prefix46* p46 = getGeneratedRoutes(rp, alloc);
  620. return getSomething(rp, alloc, p46->prefix6, p46->prefix4);
  621. }
  622. Er_DEFUN(void RouteGen_commit(struct RouteGen* rg,
  623. const char* tunName,
  624. struct Allocator* tempAlloc))
  625. {
  626. struct RouteGen_pvt* rp = Identity_check((struct RouteGen_pvt*) rg);
  627. struct Prefix46* p46 = getGeneratedRoutes(rp, tempAlloc);
  628. struct Sockaddr** prefixSet =
  629. Allocator_calloc(tempAlloc, sizeof(char*), p46->prefix4->length + p46->prefix6->length);
  630. int prefixNum = 0;
  631. for (int i = 0; i < p46->prefix4->length; i++) {
  632. struct Prefix4* pfx4 = ArrayList_OfPrefix4_get(p46->prefix4, i);
  633. prefixSet[prefixNum++] = sockaddrForPrefix4(tempAlloc, pfx4);
  634. }
  635. for (int i = 0; i < p46->prefix6->length; i++) {
  636. struct Prefix6* pfx6 = ArrayList_OfPrefix6_get(p46->prefix6, i);
  637. prefixSet[prefixNum++] = sockaddrForPrefix6(tempAlloc, pfx6);
  638. }
  639. Assert_true(prefixNum == p46->prefix4->length + p46->prefix6->length);
  640. Er(NetDev_setRoutes(tunName, prefixSet, prefixNum, rp->log, tempAlloc));
  641. rp->pub.hasUncommittedChanges = false;
  642. Er_ret();
  643. }
  644. static void setupDefaultLocalPrefixes(struct RouteGen_pvt* rp)
  645. {
  646. struct Sockaddr_storage ss;
  647. #define ADD_PREFIX(str) \
  648. Assert_true(!Sockaddr_parse(str, &ss)); \
  649. RouteGen_addLocalPrefix(&rp->pub, &ss.addr)
  650. ADD_PREFIX("fe80::/10");
  651. ADD_PREFIX("fd00::/8");
  652. ADD_PREFIX("10.0.0.0/8");
  653. ADD_PREFIX("172.16.0.0/12");
  654. ADD_PREFIX("192.168.0.0/16");
  655. ADD_PREFIX("127.0.0.0/8");
  656. #undef ADD_PREFIX
  657. }
  658. struct RouteGen* RouteGen_new(struct Allocator* allocator, struct Log* log)
  659. {
  660. struct Allocator* alloc = Allocator_child(allocator);
  661. struct RouteGen_pvt* rp = Allocator_calloc(alloc, sizeof(struct RouteGen_pvt), 1);
  662. rp->prefixes6 = ArrayList_OfPrefix6_new(alloc);
  663. rp->localPrefixes6 = ArrayList_OfPrefix6_new(alloc);
  664. rp->exceptions6 = ArrayList_OfPrefix6_new(alloc);
  665. rp->prefixes4 = ArrayList_OfPrefix4_new(alloc);
  666. rp->localPrefixes4 = ArrayList_OfPrefix4_new(alloc);
  667. rp->exceptions4 = ArrayList_OfPrefix4_new(alloc);
  668. rp->log = log;
  669. rp->alloc = alloc;
  670. Identity_set(rp);
  671. setupDefaultLocalPrefixes(rp);
  672. return &rp->pub;
  673. }