xroute.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. Copyright (c) 2007, 2008 by Juliusz Chroboczek
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include <sys/time.h>
  25. #include <netinet/in.h>
  26. #include "babeld.h"
  27. #include "kernel.h"
  28. #include "neighbour.h"
  29. #include "message.h"
  30. #include "route.h"
  31. #include "xroute.h"
  32. #include "util.h"
  33. #include "configuration.h"
  34. #include "interface.h"
  35. #include "local.h"
  36. #include "lorauth.h"
  37. static struct xroute *xroutes;
  38. static int numxroutes = 0, maxxroutes = 0;
  39. struct xroute *
  40. find_xroute(const unsigned char *prefix, unsigned char plen,
  41. const unsigned char *src_prefix, unsigned char src_plen)
  42. {
  43. int i;
  44. debugf(" -- find_xroute(): numxroutes:%d\n", numxroutes);
  45. debugf(" prefix:%s, src_prefix:%s\n",
  46. format_prefix(prefix, plen),
  47. format_prefix(src_prefix, src_plen));
  48. for(i = 0; i < numxroutes; i++) {
  49. debugf("\t xroutes[%d].prefix: %s\n", i,
  50. format_prefix(xroutes[i].prefix, xroutes[i].plen));
  51. debugf("\t xroutes[%d].cipher: %s\n", i,
  52. reduced_lorauth_token(xroutes[i].cipher));
  53. if(xroutes[i].plen == plen &&
  54. memcmp(xroutes[i].prefix, prefix, 16) == 0 &&
  55. xroutes[i].src_plen == src_plen &&
  56. memcmp(xroutes[i].src_prefix, src_prefix, 16) == 0)
  57. return &xroutes[i];
  58. }
  59. return NULL;
  60. }
  61. void
  62. flush_xroute(struct xroute *xroute)
  63. {
  64. int i;
  65. i = xroute - xroutes;
  66. assert(i >= 0 && i < numxroutes);
  67. local_notify_xroute(xroute, LOCAL_FLUSH);
  68. if(i != numxroutes - 1)
  69. memcpy(xroutes + i, xroutes + numxroutes - 1, sizeof(struct xroute));
  70. numxroutes--;
  71. VALGRIND_MAKE_MEM_UNDEFINED(xroutes + numxroutes, sizeof(struct xroute));
  72. if(numxroutes == 0) {
  73. free(xroutes);
  74. xroutes = NULL;
  75. maxxroutes = 0;
  76. } else if(maxxroutes > 8 && numxroutes < maxxroutes / 4) {
  77. struct xroute *new_xroutes;
  78. int n = maxxroutes / 2;
  79. new_xroutes = realloc(xroutes, n * sizeof(struct xroute));
  80. if(new_xroutes == NULL)
  81. return;
  82. xroutes = new_xroutes;
  83. maxxroutes = n;
  84. }
  85. }
  86. int
  87. add_xroute(unsigned char prefix[16], unsigned char plen,
  88. unsigned char src_prefix[16], unsigned char src_plen,
  89. unsigned short metric, unsigned int ifindex, int proto,
  90. unsigned short clen, unsigned char cipher[514])
  91. {
  92. debugf(" add_xroute:\n");
  93. struct xroute *xroute = find_xroute(prefix, plen, src_prefix, src_plen);
  94. if(xroute) { // xroute exists
  95. if(xroute->metric <= metric)
  96. return 0;
  97. xroute->metric = metric;
  98. local_notify_xroute(xroute, LOCAL_CHANGE);
  99. return 1;
  100. }
  101. if(numxroutes >= maxxroutes) {
  102. struct xroute *new_xroutes;
  103. int n = maxxroutes < 1 ? 8 : 2 * maxxroutes;
  104. new_xroutes = realloc(xroutes, n * sizeof(struct xroute));
  105. if(new_xroutes == NULL)
  106. return -1;
  107. maxxroutes = n;
  108. xroutes = new_xroutes;
  109. }
  110. debugf(" -- adding new xroute\n");
  111. memcpy(xroutes[numxroutes].prefix, prefix, 16);
  112. xroutes[numxroutes].plen = plen;
  113. memcpy(xroutes[numxroutes].src_prefix, src_prefix, 16);
  114. xroutes[numxroutes].src_plen = src_plen;
  115. xroutes[numxroutes].metric = metric;
  116. xroutes[numxroutes].ifindex = ifindex;
  117. xroutes[numxroutes].proto = proto;
  118. // -- lorauth --
  119. xroutes[numxroutes].clen = clen;
  120. if(strlen((char *)xroutes[numxroutes].cipher)>3)
  121. {
  122. debugf(" adding cipher\n");
  123. strncpy((char *)xroutes[numxroutes].cipher, (char *)cipher, LORAUTH_CIPHER_LEN);
  124. //memcpy(xroutes[numxroutes].cipher, cipher, LORAUTH_CIPHER_LEN);
  125. }
  126. else
  127. {
  128. debugf(" empty cipher to xroute.\n");
  129. memset(xroutes[numxroutes].cipher, 0, LORAUTH_CIPHER_LEN+1);
  130. }
  131. // ----
  132. numxroutes++;
  133. local_notify_xroute(&xroutes[numxroutes - 1], LOCAL_ADD);
  134. return 1;
  135. }
  136. /* Returns an overestimate of the number of xroutes. */
  137. int
  138. xroutes_estimate()
  139. {
  140. return numxroutes;
  141. }
  142. struct xroute_stream {
  143. int index;
  144. };
  145. struct
  146. xroute_stream *
  147. xroute_stream()
  148. {
  149. struct xroute_stream *stream = calloc(1, sizeof(struct xroute_stream));
  150. if(stream == NULL)
  151. return NULL;
  152. return stream;
  153. }
  154. struct xroute *
  155. xroute_stream_next(struct xroute_stream *stream)
  156. {
  157. if(stream->index < numxroutes)
  158. return &xroutes[stream->index++];
  159. else
  160. return NULL;
  161. }
  162. void
  163. xroute_stream_done(struct xroute_stream *stream)
  164. {
  165. free(stream);
  166. }
  167. static int
  168. filter_route(struct kernel_route *route, void *data) {
  169. void **args = (void**)data;
  170. int maxroutes = *(int*)args[0];
  171. struct kernel_route *routes = (struct kernel_route *)args[1];
  172. int *found = (int*)args[2];
  173. debugf(" filter_route: found %d, maxroutes %d \n",
  174. found[0], maxroutes);
  175. if(*found >= maxroutes)
  176. return -1;
  177. if(martian_prefix(route->prefix, route->plen) ||
  178. martian_prefix(route->src_prefix, route->src_plen))
  179. return 0;
  180. routes[*found] = *route;
  181. ++ *found;
  182. return 0;
  183. }
  184. static int
  185. kernel_routes(struct kernel_route *routes, int maxroutes)
  186. {
  187. int found = 0;
  188. void *data[3] = { &maxroutes, routes, &found };
  189. struct kernel_filter filter = {0};
  190. filter.route = filter_route;
  191. filter.route_closure = data;
  192. kernel_dump(CHANGE_ROUTE, &filter);
  193. return found;
  194. }
  195. static int
  196. filter_address(struct kernel_addr *addr, void *data) {
  197. void **args = (void **)data;
  198. int maxroutes = *(int *)args[0];
  199. struct kernel_route *routes = (struct kernel_route*)args[1];
  200. int *found = (int *)args[2];
  201. int ifindex = *(int*)args[3];
  202. int ll = args[4] ? !!*(int*)args[4] : 0;
  203. struct kernel_route *route = NULL;
  204. if(*found >= maxroutes)
  205. return 0;
  206. if(ll == !IN6_IS_ADDR_LINKLOCAL(&addr->addr))
  207. return 0;
  208. /* ifindex may be 0 -- see kernel_addresses */
  209. if(ifindex && addr->ifindex != ifindex)
  210. return 0;
  211. route = &routes[*found];
  212. memcpy(route->prefix, addr->addr.s6_addr, 16);
  213. route->plen = 128;
  214. route->metric = 0;
  215. route->ifindex = addr->ifindex;
  216. route->proto = RTPROT_BABEL_LOCAL;
  217. memset(route->gw, 0, 16);
  218. ++ *found;
  219. return 1;
  220. }
  221. /* ifindex is 0 for all interfaces. ll indicates whether we are
  222. interested in link-local or global addresses. */
  223. int
  224. kernel_addresses(int ifindex, int ll, struct kernel_route *routes,
  225. int maxroutes)
  226. {
  227. int found = 0;
  228. void *data[5] = { &maxroutes, routes, &found, &ifindex, &ll };
  229. struct kernel_filter filter = {0};
  230. filter.addr = filter_address;
  231. filter.addr_closure = data;
  232. kernel_dump(CHANGE_ADDR, &filter);
  233. return found;
  234. }
  235. int
  236. check_xroutes(int send_updates)
  237. {
  238. int i, j, metric, export, change = 0, rc;
  239. struct kernel_route *routes;
  240. struct filter_result filter_result = {0};
  241. int numroutes, numaddresses;
  242. static int maxroutes = 8;
  243. const int maxmaxroutes = 16 * 1024;
  244. debugf("\nChecking kernel routes, send_updates=%d.\n",
  245. send_updates);
  246. again:
  247. routes = calloc(maxroutes, sizeof(struct kernel_route));
  248. if(routes == NULL)
  249. return -1;
  250. rc = kernel_addresses(0, 0, routes, maxroutes);
  251. if(rc < 0) {
  252. perror("kernel_addresses");
  253. numroutes = 0;
  254. } else {
  255. numroutes = rc;
  256. }
  257. if(numroutes >= maxroutes)
  258. goto resize;
  259. numaddresses = numroutes;
  260. rc = kernel_routes(routes + numroutes, maxroutes - numroutes);
  261. if(rc < 0)
  262. fprintf(stderr, "Couldn't get kernel routes.\n");
  263. else
  264. numroutes += rc;
  265. if(numroutes >= maxroutes)
  266. goto resize;
  267. /* Apply filter to kernel routes (e.g. change the source prefix). */
  268. for(i = numaddresses; i < numroutes; i++) {
  269. filter_result.src_prefix = NULL;
  270. redistribute_filter(routes[i].prefix, routes[i].plen,
  271. routes[i].src_prefix, routes[i].src_plen,
  272. routes[i].ifindex, routes[i].proto,
  273. &filter_result);
  274. if(filter_result.src_prefix) {
  275. memcpy(routes[i].src_prefix, filter_result.src_prefix, 16);
  276. routes[i].src_plen = filter_result.src_plen;
  277. }
  278. }
  279. /* Check for any routes that need to be flushed */
  280. i = 0;
  281. while(i < numxroutes) {
  282. export = 0;
  283. metric = redistribute_filter(xroutes[i].prefix, xroutes[i].plen,
  284. xroutes[i].src_prefix, xroutes[i].src_plen,
  285. xroutes[i].ifindex, xroutes[i].proto,
  286. NULL);
  287. //-- lorauth note: metric, should here need to be added cipher and clen? --
  288. debugf("----checking xroutes metric: %d\n", metric);
  289. if (metric == 0)
  290. {
  291. debugf(" :: metric==0 ::\n");
  292. debugf(" myid: %s \n", format_eui64(myid));
  293. int rc;
  294. unsigned char *cipher = calloc(1, LORAUTH_CIPHER_LEN + 1);
  295. debugf(" lorauth_index: %d\n", lorauth_token_index(myid, myseqno));
  296. rc = lorauth_token(cipher,
  297. myid,
  298. myseqno);
  299. if (rc != 0)
  300. debugf(" Couldn't find loruauth token, rc %d\n", rc);
  301. else {
  302. strncpy((char *)xroutes[i].cipher, (char *)cipher, LORAUTH_CIPHER_LEN);
  303. xroutes[i].clen = LORAUTH_CIPHER_LEN;
  304. debugf(" lorauth token found: %s\n",
  305. reduced_lorauth_token(xroutes[i].cipher));
  306. }
  307. free(cipher);
  308. }
  309. // ----
  310. if(metric < INFINITY && metric == xroutes[i].metric) {
  311. for(j = 0; j < numroutes; j++) {
  312. if(xroutes[i].plen == routes[j].plen &&
  313. memcmp(xroutes[i].prefix, routes[j].prefix, 16) == 0 &&
  314. xroutes[i].ifindex == routes[j].ifindex &&
  315. xroutes[i].proto == routes[j].proto) {
  316. export = 1;
  317. break;
  318. }
  319. }
  320. }
  321. if(!export) {
  322. unsigned char prefix[16], plen;
  323. unsigned char src_prefix[16], src_plen;
  324. struct babel_route *route;
  325. // -- lorauth --
  326. unsigned short clen;
  327. unsigned char *cipher = calloc(1, LORAUTH_CIPHER_LEN + 1);
  328. // ----
  329. memcpy(prefix, xroutes[i].prefix, 16);
  330. plen = xroutes[i].plen;
  331. memcpy(src_prefix, xroutes[i].src_prefix, 16);
  332. src_plen = xroutes[i].src_plen;
  333. flush_xroute(&xroutes[i]);
  334. route = find_best_route(prefix, plen, src_prefix, src_plen, 1,NULL);
  335. // testing
  336. if(route)
  337. debugf(" best_route_found: cost:%d", route->cost );
  338. else
  339. debugf(" not found best route");
  340. // -- lorauth --
  341. clen = xroutes[i].clen;
  342. memcpy(cipher, xroutes[i].cipher, LORAUTH_CIPHER_LEN);
  343. // ----
  344. if(route)
  345. install_route(route);
  346. /* send_update_resend only records the prefix, so the update
  347. will only be sent after we perform all of the changes. */
  348. if(send_updates)
  349. send_update_resend(NULL, prefix, plen, src_prefix, src_plen,
  350. // -- lorauth --
  351. cipher, clen);
  352. // NULL, 0);
  353. free(cipher);
  354. change = 1;
  355. } else {
  356. i++;
  357. }
  358. } // end while(i < numxroutes)
  359. /* Add any new routes */
  360. for(i = 0; i < numroutes; i++) {
  361. if(martian_prefix(routes[i].prefix, routes[i].plen))
  362. continue;
  363. metric = redistribute_filter(routes[i].prefix, routes[i].plen,
  364. routes[i].src_prefix, routes[i].src_plen,
  365. routes[i].ifindex, routes[i].proto, NULL);
  366. if(metric < INFINITY) {
  367. /* lorauth note: here if metric == 0 add own cipher and clen to xroute */
  368. /* according to line 185 routes is now storing kernel_route struct
  369. So in order to not changing that struct i will try to get the
  370. cipher, clen related to that (prefix, plen) from xroutes
  371. */
  372. if (metric == 0) { // own route
  373. debugf(" -- metric == 0\n");
  374. struct xroute *a_xroute =
  375. find_xroute(routes[i].prefix,
  376. (unsigned char)routes[i].plen,
  377. routes[i].src_prefix,
  378. (unsigned char)routes[i].src_plen);
  379. if(a_xroute) {
  380. debugf(" -- found_xroute: prefix: %s cipher: %s\n",
  381. format_prefix(a_xroute->prefix, a_xroute->plen),
  382. reduced_lorauth_token(a_xroute->cipher));
  383. rc = add_xroute(routes[i].prefix, routes[i].plen,
  384. routes[i].src_prefix, routes[i].src_plen,
  385. metric, routes[i].ifindex, routes[i].proto,
  386. a_xroute->clen, a_xroute->cipher);
  387. }
  388. else{
  389. debugf(" -- not found xroute\n");
  390. rc = add_xroute(routes[i].prefix, routes[i].plen,
  391. routes[i].src_prefix, routes[i].src_plen,
  392. metric, routes[i].ifindex, routes[i].proto,
  393. 0, NULL);
  394. }
  395. }
  396. else {
  397. debugf(" [[--- metric %d \n", metric);
  398. // here should look for cipher, clen from babel_routes?
  399. /* rc = add_xroute(routes[i].prefix, routes[i].plen, */
  400. /* routes[i],src_prefix, routes[i].src_plen, */
  401. /* metric, routes[i].ifindex, routes[i].proto, */
  402. /* routes[i].src.clen, routes[i].src.cipher); */
  403. }
  404. // -- lorauth --
  405. /* lorauth note: is the following necessary? */
  406. if(rc > 0) {
  407. debugf(" rc > 0\n");
  408. struct babel_route *route;
  409. route = find_installed_route(routes[i].prefix, routes[i].plen,
  410. routes[i].src_prefix,
  411. routes[i].src_plen);
  412. if(route) {
  413. debugf(" found route\n");
  414. if(allow_duplicates < 0 ||
  415. routes[i].metric < allow_duplicates)
  416. uninstall_route(route);
  417. } else
  418. debugf(" not found installed route\n");
  419. change = 1;
  420. if(send_updates){
  421. debugf(" - - - not found installed route, using kernel route\n prefix:%s",
  422. format_prefix(routes[i].prefix, routes[i].plen));
  423. send_update(NULL, 0, routes[i].prefix, routes[i].plen,
  424. routes[i].src_prefix, routes[i].src_plen,
  425. // lorauth
  426. //TODO: check the following
  427. NULL, 0);
  428. }
  429. }
  430. }
  431. }
  432. free(routes);
  433. /* Set up maxroutes for the next call. */
  434. maxroutes = MIN(numroutes + 8, maxmaxroutes);
  435. return change;
  436. resize:
  437. free(routes);
  438. if(maxroutes >= maxmaxroutes)
  439. return -1;
  440. maxroutes = MIN(maxmaxroutes, 2 * maxroutes);
  441. goto again;
  442. }