route.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. /*
  2. Copyright (c) 2007-2011 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 <string.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include <sys/time.h>
  25. #include "babeld.h"
  26. #include "util.h"
  27. #include "kernel.h"
  28. #include "interface.h"
  29. #include "source.h"
  30. #include "neighbour.h"
  31. #include "route.h"
  32. #include "xroute.h"
  33. #include "message.h"
  34. #include "resend.h"
  35. #include "configuration.h"
  36. #include "local.h"
  37. #include "disambiguation.h"
  38. #include "lorauth.h"
  39. struct babel_route **routes = NULL;
  40. static int route_slots = 0, max_route_slots = 0;
  41. int kernel_metric = 0, reflect_kernel_metric = 0;
  42. int allow_duplicates = -1;
  43. int diversity_kind = DIVERSITY_NONE;
  44. int diversity_factor = 256; /* in units of 1/256 */
  45. int keep_unfeasible = 0;
  46. static int smoothing_half_life = 0;
  47. static int two_to_the_one_over_hl = 0; /* 2^(1/hl) * 0x10000 */
  48. static int
  49. check_specific_first(void)
  50. {
  51. /* All source-specific routes are in front of the list */
  52. int specific = 1;
  53. int i;
  54. for(i = 0; i < route_slots; i++) {
  55. if(routes[i]->src->src_plen == 0) {
  56. specific = 0;
  57. } else if(!specific) {
  58. return 0;
  59. }
  60. }
  61. return 1;
  62. }
  63. /* We maintain a list of "slots", ordered by prefix. Every slot
  64. contains a linked list of the routes to this prefix, with the
  65. installed route, if any, at the head of the list. */
  66. static int
  67. route_compare(const unsigned char *prefix, unsigned char plen,
  68. const unsigned char *src_prefix, unsigned char src_plen,
  69. struct babel_route *route)
  70. {
  71. int i;
  72. /* Put all source-specific routes in the front of the list. */
  73. if(src_plen == 0 && route->src->src_plen > 0) {
  74. return 1;
  75. } else if(src_plen > 0 && route->src->src_plen == 0) {
  76. return -1;
  77. }
  78. i = memcmp(prefix, route->src->prefix, 16);
  79. if(i != 0)
  80. return i;
  81. if(plen < route->src->plen)
  82. return -1;
  83. if(plen > route->src->plen)
  84. return 1;
  85. if(src_plen == 0) {
  86. if(route->src->src_plen > 0)
  87. return -1;
  88. } else {
  89. i = memcmp(src_prefix, route->src->src_prefix, 16);
  90. if(i != 0)
  91. return i;
  92. if(src_plen < route->src->src_plen)
  93. return -1;
  94. if(src_plen > route->src->src_plen)
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. /* Performs binary search, returns -1 in case of failure. In the latter
  100. case, new_return is the place where to insert the new element. */
  101. static int
  102. find_route_slot(const unsigned char *prefix, unsigned char plen,
  103. const unsigned char *src_prefix, unsigned char src_plen,
  104. int *new_return)
  105. {
  106. int p, m, g, c;
  107. if(route_slots < 1) {
  108. if(new_return)
  109. *new_return = 0;
  110. return -1;
  111. }
  112. p = 0; g = route_slots - 1;
  113. do {
  114. m = (p + g) / 2;
  115. c = route_compare(prefix, plen, src_prefix, src_plen, routes[m]);
  116. if(c == 0)
  117. return m;
  118. else if(c < 0)
  119. g = m - 1;
  120. else
  121. p = m + 1;
  122. } while(p <= g);
  123. if(new_return)
  124. *new_return = p;
  125. return -1;
  126. }
  127. struct babel_route *
  128. find_route(const unsigned char *prefix, unsigned char plen,
  129. const unsigned char *src_prefix, unsigned char src_plen,
  130. struct neighbour *neigh, const unsigned char *nexthop)
  131. {
  132. struct babel_route *route;
  133. int i = find_route_slot(prefix, plen, src_prefix, src_plen, NULL);
  134. if(i < 0)
  135. return NULL;
  136. route = routes[i];
  137. while(route) {
  138. if(route->neigh == neigh && memcmp(route->nexthop, nexthop, 16) == 0)
  139. return route;
  140. route = route->next;
  141. }
  142. return NULL;
  143. }
  144. /*
  145. * NOTE: methods related to lorauth may contain 'auth' in it's name
  146. */
  147. struct babel_route *
  148. find_auth_route(const unsigned char *prefix, unsigned char plen,
  149. const unsigned char *src_prefix, unsigned char src_plen,
  150. const unsigned short clen, unsigned char *cipher,
  151. struct neighbour *neigh, const unsigned char *nexthop)
  152. {
  153. struct babel_route *route;
  154. int i = find_route_slot(prefix, plen, src_prefix, src_plen, NULL);
  155. if(i < 0)
  156. return NULL;
  157. route = routes[i];
  158. while(route) {
  159. if(route->neigh == neigh && memcmp(route->nexthop, nexthop, 16) == 0
  160. && route->src->clen == clen &&
  161. memcmp(route->src->cipher, cipher, sizeof(*cipher)) == 0)
  162. return route;
  163. route = route->next;
  164. }
  165. return NULL;
  166. }
  167. struct babel_route *
  168. find_installed_route(const unsigned char *prefix, unsigned char plen,
  169. const unsigned char *src_prefix, unsigned char src_plen)
  170. {
  171. int i = find_route_slot(prefix, plen, src_prefix, src_plen, NULL);
  172. if(i >= 0 && routes[i]->installed)
  173. return routes[i];
  174. return NULL;
  175. }
  176. /* Returns an overestimate of the number of installed routes. */
  177. int
  178. installed_routes_estimate(void)
  179. {
  180. return route_slots;
  181. }
  182. static int
  183. resize_route_table(int new_slots)
  184. {
  185. struct babel_route **new_routes;
  186. assert(new_slots >= route_slots);
  187. if(new_slots == 0) {
  188. new_routes = NULL;
  189. free(routes);
  190. } else {
  191. new_routes = realloc(routes, new_slots * sizeof(struct babel_route*));
  192. if(new_routes == NULL)
  193. return -1;
  194. }
  195. max_route_slots = new_slots;
  196. routes = new_routes;
  197. return 1;
  198. }
  199. /* Insert a route into the table. If successful, retains the route.
  200. On failure, caller must free the route. */
  201. static struct babel_route *
  202. insert_route(struct babel_route *route)
  203. {
  204. int i, n;
  205. assert(!route->installed);
  206. i = find_route_slot(route->src->prefix, route->src->plen,
  207. route->src->src_prefix, route->src->src_plen, &n);
  208. if(i < 0) {
  209. if(route_slots >= max_route_slots)
  210. resize_route_table(max_route_slots < 1 ? 8 : 2 * max_route_slots);
  211. if(route_slots >= max_route_slots)
  212. return NULL;
  213. route->next = NULL;
  214. if(n < route_slots)
  215. memmove(routes + n + 1, routes + n,
  216. (route_slots - n) * sizeof(struct babel_route*));
  217. route_slots++;
  218. routes[n] = route;
  219. } else {
  220. struct babel_route *r;
  221. r = routes[i];
  222. while(r->next)
  223. r = r->next;
  224. r->next = route;
  225. route->next = NULL;
  226. }
  227. return route;
  228. }
  229. static void
  230. destroy_route(struct babel_route *route)
  231. {
  232. free(route->channels);
  233. free(route);
  234. }
  235. void
  236. flush_route(struct babel_route *route)
  237. {
  238. int i;
  239. struct source *src;
  240. unsigned oldmetric;
  241. int lost = 0;
  242. oldmetric = route_metric(route);
  243. src = route->src;
  244. if(route->installed) {
  245. uninstall_route(route);
  246. lost = 1;
  247. }
  248. i = find_route_slot(route->src->prefix, route->src->plen,
  249. route->src->src_prefix, route->src->src_plen, NULL);
  250. assert(i >= 0 && i < route_slots);
  251. local_notify_route(route, LOCAL_FLUSH);
  252. if(route == routes[i]) {
  253. routes[i] = route->next;
  254. route->next = NULL;
  255. destroy_route(route);
  256. if(routes[i] == NULL) {
  257. if(i < route_slots - 1)
  258. memmove(routes + i, routes + i + 1,
  259. (route_slots - i - 1) * sizeof(struct babel_route*));
  260. routes[route_slots - 1] = NULL;
  261. route_slots--;
  262. VALGRIND_MAKE_MEM_UNDEFINED(routes + route_slots, sizeof(struct route *));
  263. }
  264. if(route_slots == 0)
  265. resize_route_table(0);
  266. else if(max_route_slots > 8 && route_slots < max_route_slots / 4)
  267. resize_route_table(max_route_slots / 2);
  268. } else {
  269. struct babel_route *r = routes[i];
  270. while(r->next != route)
  271. r = r->next;
  272. r->next = route->next;
  273. route->next = NULL;
  274. destroy_route(route);
  275. }
  276. if(lost)
  277. route_lost(src, oldmetric);
  278. release_source(src);
  279. }
  280. void
  281. flush_all_routes()
  282. {
  283. int i;
  284. /* Start from the end, to avoid shifting the table. */
  285. i = route_slots - 1;
  286. while(i >= 0) {
  287. while(i < route_slots) {
  288. /* Uninstall first, to avoid calling route_lost. */
  289. if(routes[i]->installed)
  290. uninstall_route(routes[i]);
  291. flush_route(routes[i]);
  292. }
  293. i--;
  294. }
  295. check_sources_released();
  296. }
  297. void
  298. flush_neighbour_routes(struct neighbour *neigh)
  299. {
  300. int i;
  301. i = 0;
  302. while(i < route_slots) {
  303. struct babel_route *r;
  304. r = routes[i];
  305. while(r) {
  306. if(r->neigh == neigh) {
  307. flush_route(r);
  308. goto again;
  309. }
  310. r = r->next;
  311. }
  312. i++;
  313. again:
  314. ;
  315. }
  316. }
  317. void
  318. flush_interface_routes(struct interface *ifp, int v4only)
  319. {
  320. int i;
  321. i = 0;
  322. while(i < route_slots) {
  323. struct babel_route *r;
  324. r = routes[i];
  325. while(r) {
  326. if(r->neigh->ifp == ifp &&
  327. (!v4only || v4mapped(r->nexthop))) {
  328. flush_route(r);
  329. goto again;
  330. }
  331. r = r->next;
  332. }
  333. i++;
  334. again:
  335. ;
  336. }
  337. }
  338. struct route_stream {
  339. int installed;
  340. int index;
  341. struct babel_route *next;
  342. };
  343. struct route_stream *
  344. route_stream(int which)
  345. {
  346. struct route_stream *stream;
  347. if(!check_specific_first())
  348. fprintf(stderr, "Invariant failed: specific routes first in RIB.\n");
  349. stream = calloc(1, sizeof(struct route_stream));
  350. if(stream == NULL)
  351. return NULL;
  352. stream->installed = which;
  353. stream->index = which == ROUTE_ALL ? -1 : 0;
  354. stream->next = NULL;
  355. return stream;
  356. }
  357. struct babel_route *
  358. route_stream_next(struct route_stream *stream)
  359. {
  360. if(stream->installed) {
  361. while(stream->index < route_slots)
  362. if(stream->installed == ROUTE_SS_INSTALLED &&
  363. routes[stream->index]->src->src_plen == 0)
  364. return NULL;
  365. else if(routes[stream->index]->installed)
  366. break;
  367. else
  368. stream->index++;
  369. if(stream->index < route_slots)
  370. return routes[stream->index++];
  371. else
  372. return NULL;
  373. } else {
  374. struct babel_route *next;
  375. if(!stream->next) {
  376. stream->index++;
  377. if(stream->index >= route_slots)
  378. return NULL;
  379. stream->next = routes[stream->index];
  380. }
  381. next = stream->next;
  382. stream->next = next->next;
  383. return next;
  384. }
  385. }
  386. void
  387. route_stream_done(struct route_stream *stream)
  388. {
  389. free(stream);
  390. }
  391. int
  392. metric_to_kernel(int metric)
  393. {
  394. if(metric >= INFINITY) {
  395. return KERNEL_INFINITY;
  396. } else if(reflect_kernel_metric) {
  397. int r = kernel_metric + metric;
  398. return r >= KERNEL_INFINITY ? KERNEL_INFINITY : r;
  399. } else {
  400. return kernel_metric;
  401. }
  402. }
  403. // --mark
  404. /* This is used to maintain the invariant that the installed route is at
  405. the head of the list. */
  406. static void
  407. move_installed_route(struct babel_route *route, int i)
  408. {
  409. assert(i >= 0 && i < route_slots);
  410. assert(route->installed);
  411. if(route != routes[i]) {
  412. struct babel_route *r = routes[i];
  413. while(r->next != route)
  414. r = r->next;
  415. r->next = route->next;
  416. route->next = routes[i];
  417. routes[i] = route;
  418. }
  419. }
  420. void
  421. install_route(struct babel_route *route)
  422. {
  423. int i, rc;
  424. if(route->installed)
  425. return;
  426. if(!route_feasible(route))
  427. fprintf(stderr, "WARNING: installing unfeasible route "
  428. "(this shouldn't happen).");
  429. // -- lorauth --
  430. if(route->src->clen < 0 ||
  431. strlen((char*)route->src->cipher)<4)
  432. {
  433. fprintf(stderr, "** Request to install route WITHOUT Lor-authentication, rejecting route install. **\n");
  434. return;
  435. }
  436. // ----
  437. i = find_route_slot(route->src->prefix, route->src->plen,
  438. route->src->src_prefix, route->src->src_plen, NULL);
  439. assert(i >= 0 && i < route_slots);
  440. if(routes[i] != route && routes[i]->installed) {
  441. fprintf(stderr, "WARNING: attempting to install duplicate route "
  442. "(this shouldn't happen).");
  443. return;
  444. }
  445. rc = kinstall_route(route);
  446. if(rc < 0 && errno != EEXIST)
  447. return;
  448. route->installed = 1;
  449. move_installed_route(route, i);
  450. local_notify_route(route, LOCAL_CHANGE);
  451. }
  452. void
  453. uninstall_route(struct babel_route *route)
  454. {
  455. if(!route->installed)
  456. return;
  457. route->installed = 0;
  458. kuninstall_route(route);
  459. local_notify_route(route, LOCAL_CHANGE);
  460. }
  461. /* This is equivalent to uninstall_route followed with install_route,
  462. but without the race condition. The destination of both routes
  463. must be the same. */
  464. static void
  465. switch_routes(struct babel_route *old, struct babel_route *new)
  466. {
  467. int rc;
  468. if(!old) {
  469. install_route(new);
  470. return;
  471. }
  472. if(!old->installed)
  473. return;
  474. if(!route_feasible(new))
  475. fprintf(stderr, "WARNING: switching to unfeasible route "
  476. "(this shouldn't happen).");
  477. rc = kswitch_routes(old, new);
  478. if(rc < 0)
  479. return;
  480. old->installed = 0;
  481. new->installed = 1;
  482. move_installed_route(new, find_route_slot(new->src->prefix, new->src->plen,
  483. new->src->src_prefix,
  484. new->src->src_plen,
  485. NULL));
  486. local_notify_route(old, LOCAL_CHANGE);
  487. local_notify_route(new, LOCAL_CHANGE);
  488. }
  489. static void
  490. change_route_metric(struct babel_route *route,
  491. unsigned refmetric, unsigned cost, unsigned add)
  492. {
  493. int old, new;
  494. int newmetric = MIN(refmetric + cost + add, INFINITY);
  495. old = metric_to_kernel(route_metric(route));
  496. new = metric_to_kernel(newmetric);
  497. if(route->installed && old != new) {
  498. int rc;
  499. rc = kchange_route_metric(route, refmetric, cost, add);
  500. if(rc < 0)
  501. return;
  502. }
  503. /* Update route->smoothed_metric using the old metric. */
  504. route_smoothed_metric(route);
  505. route->refmetric = refmetric;
  506. route->cost = cost;
  507. route->add_metric = add;
  508. /* printf("---change_route_metric (id: %s) add: %d \n", */
  509. /* myid, add); */
  510. if(smoothing_half_life == 0) {
  511. route->smoothed_metric = route_metric(route);
  512. route->smoothed_metric_time = now.tv_sec;
  513. }
  514. local_notify_route(route, LOCAL_CHANGE);
  515. }
  516. static void
  517. retract_route(struct babel_route *route)
  518. {
  519. /* We cannot simply remove the route from the kernel, as that might
  520. cause a routing loop -- see RFC 6126 Sections 2.8 and 3.5.5. */
  521. change_route_metric(route, INFINITY, INFINITY, 0);
  522. }
  523. int
  524. route_feasible(struct babel_route *route)
  525. {
  526. return update_feasible(route->src, route->seqno, route->refmetric);
  527. }
  528. int
  529. route_old(struct babel_route *route)
  530. {
  531. return route->time < now.tv_sec - route->hold_time * 7 / 8;
  532. }
  533. int
  534. route_expired(struct babel_route *route)
  535. {
  536. return route->time < now.tv_sec - route->hold_time;
  537. }
  538. static int
  539. channels_interfere(int ch1, int ch2)
  540. {
  541. if(ch1 == IF_CHANNEL_NONINTERFERING || ch2 == IF_CHANNEL_NONINTERFERING)
  542. return 0;
  543. if(ch1 == IF_CHANNEL_INTERFERING || ch2 == IF_CHANNEL_INTERFERING)
  544. return 1;
  545. return ch1 == ch2;
  546. }
  547. int
  548. route_interferes(struct babel_route *route, struct interface *ifp)
  549. {
  550. switch(diversity_kind) {
  551. case DIVERSITY_NONE:
  552. return 1;
  553. case DIVERSITY_INTERFACE_1:
  554. return route->neigh->ifp == ifp;
  555. case DIVERSITY_CHANNEL_1:
  556. case DIVERSITY_CHANNEL:
  557. if(route->neigh->ifp == ifp)
  558. return 1;
  559. if(channels_interfere(ifp->channel, route->neigh->ifp->channel))
  560. return 1;
  561. if(diversity_kind == DIVERSITY_CHANNEL) {
  562. int i;
  563. for(i = 0; i < route->channels_len; i++) {
  564. if(route->channels[i] != 0 &&
  565. channels_interfere(ifp->channel, route->channels[i]))
  566. return 1;
  567. }
  568. }
  569. return 0;
  570. default:
  571. fprintf(stderr, "Unknown kind of diversity.\n");
  572. return 1;
  573. }
  574. }
  575. int
  576. update_feasible(struct source *src,
  577. unsigned short seqno, unsigned short refmetric)
  578. {
  579. if(src == NULL)
  580. return 1;
  581. if(src->time < now.tv_sec - SOURCE_GC_TIME)
  582. /* Never mind what is probably stale data */
  583. return 1;
  584. if(refmetric >= INFINITY)
  585. /* Retractions are always feasible */
  586. return 1;
  587. return (seqno_compare(seqno, src->seqno) > 0 ||
  588. (src->seqno == seqno && refmetric < src->metric));
  589. }
  590. void
  591. change_smoothing_half_life(int half_life)
  592. {
  593. if(half_life <= 0) {
  594. smoothing_half_life = 0;
  595. two_to_the_one_over_hl = 0;
  596. return;
  597. }
  598. smoothing_half_life = half_life;
  599. switch(smoothing_half_life) {
  600. case 1: two_to_the_one_over_hl = 131072; break;
  601. case 2: two_to_the_one_over_hl = 92682; break;
  602. case 3: two_to_the_one_over_hl = 82570; break;
  603. case 4: two_to_the_one_over_hl = 77935; break;
  604. default:
  605. /* 2^(1/x) is 1 + log(2)/x + O(1/x^2) at infinity. */
  606. two_to_the_one_over_hl = 0x10000 + 45426 / half_life;
  607. }
  608. }
  609. /* Update the smoothed metric, return the new value. */
  610. int
  611. route_smoothed_metric(struct babel_route *route)
  612. {
  613. int metric = route_metric(route);
  614. if(smoothing_half_life <= 0 || /* no smoothing */
  615. metric >= INFINITY || /* route retracted */
  616. route->smoothed_metric_time > now.tv_sec || /* clock stepped */
  617. route->smoothed_metric == metric) { /* already converged */
  618. route->smoothed_metric = metric;
  619. route->smoothed_metric_time = now.tv_sec;
  620. } else {
  621. int diff;
  622. /* We randomise the computation, to minimise global synchronisation
  623. and hence oscillations. */
  624. while(route->smoothed_metric_time <= now.tv_sec - smoothing_half_life) {
  625. diff = metric - route->smoothed_metric;
  626. route->smoothed_metric += roughly(diff) / 2;
  627. route->smoothed_metric_time += smoothing_half_life;
  628. }
  629. while(route->smoothed_metric_time < now.tv_sec) {
  630. diff = metric - route->smoothed_metric;
  631. route->smoothed_metric +=
  632. roughly(diff) * (two_to_the_one_over_hl - 0x10000) / 0x10000;
  633. route->smoothed_metric_time++;
  634. }
  635. diff = metric - route->smoothed_metric;
  636. if(diff > -4 && diff < 4)
  637. route->smoothed_metric = metric;
  638. }
  639. /* change_route_metric relies on this */
  640. assert(route->smoothed_metric_time == now.tv_sec);
  641. return route->smoothed_metric;
  642. }
  643. static int
  644. route_acceptable(struct babel_route *route, int feasible,
  645. struct neighbour *exclude)
  646. {
  647. if(route_expired(route))
  648. return 0;
  649. if(feasible && !route_feasible(route))
  650. return 0;
  651. if(exclude && route->neigh == exclude)
  652. return 0;
  653. /* Here add code (mostly function calls) to check if a route has been authenticated.
  654. Then if a source entry has `clen', `cipher' containing data, means the route
  655. has been previously authenticated and announced in an update TLV.
  656. */
  657. return 1;
  658. }
  659. /* Find the best route according to the weak ordering. Any
  660. linearisation of the strong ordering (see consider_route) will do,
  661. we use sm <= sm'. We could probably use a lexical ordering, but
  662. that's probably overkill. */
  663. struct babel_route *
  664. find_best_route(const unsigned char *prefix, unsigned char plen,
  665. const unsigned char *src_prefix, unsigned char src_plen,
  666. int feasible, struct neighbour *exclude)
  667. {
  668. struct babel_route *route, *r;
  669. int i = find_route_slot(prefix, plen, src_prefix, src_plen, NULL);
  670. if(i < 0)
  671. return NULL;
  672. route = routes[i];
  673. while(route && !route_acceptable(route, feasible, exclude))
  674. route = route->next;
  675. if(!route)
  676. return NULL;
  677. r = route->next;
  678. while(r) {
  679. if(route_acceptable(r, feasible, exclude) &&
  680. (route_smoothed_metric(r) < route_smoothed_metric(route)))
  681. route = r;
  682. r = r->next;
  683. }
  684. return route;
  685. }
  686. void
  687. update_route_metric(struct babel_route *route)
  688. {
  689. int oldmetric = route_metric(route);
  690. int old_smoothed_metric = route_smoothed_metric(route);
  691. debugf("--Update_route_metric: %d\n", route->src->metric);
  692. if(route_expired(route)) {
  693. if(route->refmetric < INFINITY) {
  694. route->seqno = seqno_plus(route->src->seqno, 1);
  695. retract_route(route);
  696. if(oldmetric < INFINITY)
  697. route_changed(route, route->src, oldmetric);
  698. }
  699. } else {
  700. struct neighbour *neigh = route->neigh;
  701. int add_metric = input_filter(route->src->id,
  702. route->src->prefix, route->src->plen,
  703. route->src->src_prefix,
  704. route->src->src_plen,
  705. neigh->address,
  706. neigh->ifp->ifindex);
  707. change_route_metric(route, route->refmetric,
  708. neighbour_cost(route->neigh), add_metric);
  709. if(route_metric(route) != oldmetric ||
  710. route_smoothed_metric(route) != old_smoothed_metric)
  711. route_changed(route, route->src, oldmetric);
  712. }
  713. }
  714. /* Called whenever a neighbour's cost changes, to update the metric of
  715. all routes through that neighbour. Calls local_notify_neighbour. */
  716. void
  717. update_neighbour_metric(struct neighbour *neigh, int changed)
  718. {
  719. if(changed) {
  720. int i;
  721. for(i = 0; i < route_slots; i++) {
  722. struct babel_route *r = routes[i];
  723. while(r) {
  724. if(r->neigh == neigh)
  725. update_route_metric(r);
  726. r = r->next;
  727. }
  728. }
  729. }
  730. local_notify_neighbour(neigh, LOCAL_CHANGE);
  731. }
  732. void
  733. update_interface_metric(struct interface *ifp)
  734. {
  735. int i;
  736. for(i = 0; i < route_slots; i++) {
  737. struct babel_route *r = routes[i];
  738. while(r) {
  739. if(r->neigh->ifp == ifp)
  740. update_route_metric(r);
  741. r = r->next;
  742. }
  743. }
  744. }
  745. /* This is called whenever we receive an update. */
  746. struct babel_route *
  747. update_route(const unsigned char *id,
  748. const unsigned char *prefix, unsigned char plen,
  749. const unsigned char *src_prefix, unsigned char src_plen,
  750. const unsigned char *cipher, unsigned short clen,
  751. unsigned short seqno, unsigned short refmetric,
  752. unsigned short interval,
  753. struct neighbour *neigh, const unsigned char *nexthop,
  754. const unsigned char *channels, int channels_len)
  755. {
  756. struct babel_route *route;
  757. struct source *src;
  758. int metric, feasible;
  759. int add_metric;
  760. int hold_time = MAX((4 * interval) / 100 + interval / 50, 15);
  761. int is_v4;
  762. if(clen > 0)
  763. {
  764. debugf(" --update_route id:%s\n prefix:%s plen:%d\n seqno:%d refmetric: %d\n cipher: %s clen:%d\n myid:%s\n",
  765. format_eui64(id), format_prefix(prefix, plen), plen, seqno, refmetric, reduced_lorauth_token(cipher), clen,
  766. format_eui64(myid));
  767. }
  768. else
  769. {
  770. debugf(" --update_route id:%s\n prefix:%s plen:%d\n seqno:%d refmetric: %d\n cipher: -NULL- clen:0\n myid:%s\n",
  771. format_eui64(id), format_prefix(prefix, plen), plen, seqno, refmetric, format_eui64(myid));
  772. debugf("\n **** No LORAUTH token, rejecting update ***** \n");
  773. return NULL;
  774. }
  775. if(memcmp(id, myid, 8) == 0)
  776. return NULL;
  777. if(martian_prefix(prefix, plen)) {
  778. fprintf(stderr, "Rejecting martian route to %s through %s.\n",
  779. format_prefix(prefix, plen), format_address(nexthop));
  780. return NULL;
  781. }
  782. if(src_plen != 0 && martian_prefix(src_prefix, src_plen)) {
  783. fprintf(stderr, "Rejecting martian route to %s from %s through %s.\n",
  784. format_prefix(prefix, plen),
  785. format_prefix(src_prefix, src_plen), format_eui64(id));
  786. return NULL;
  787. }
  788. is_v4 = v4mapped(prefix);
  789. if(src_plen != 0 && is_v4 != v4mapped(src_prefix))
  790. return NULL;
  791. debugf(" add_metric");
  792. add_metric = input_filter(id, prefix, plen, src_prefix, src_plen,
  793. neigh->address, neigh->ifp->ifindex);
  794. debugf(" add_metric: %d\n", add_metric);
  795. if(add_metric >= INFINITY)
  796. return NULL;
  797. debugf(" find_route\n");
  798. route = find_route(prefix, plen, src_prefix, src_plen, neigh, nexthop);
  799. if(route && memcmp(route->src->id, id, 8) == 0)
  800. /* Avoid scanning the source table. */
  801. src = route->src;
  802. else
  803. src = find_source(id, prefix, plen, src_prefix, src_plen, cipher, clen, 1, seqno);
  804. if(src == NULL)
  805. return NULL;
  806. debugf(" source found: prefix:%s, seqno: %d, cipher: %s\n",
  807. format_prefix(src->prefix, src->plen), src->seqno,
  808. reduced_lorauth_token(src->cipher));
  809. debugf(" update_feasible\n");
  810. feasible = update_feasible(src, seqno, refmetric);
  811. metric = MIN((int)refmetric + neighbour_cost(neigh) + add_metric, INFINITY);
  812. debugf(" metric=%d\n",metric);
  813. if(route) {
  814. struct source *oldsrc;
  815. unsigned short oldmetric;
  816. int lost = 0;
  817. debugf(" route FOUND, src->prefix:%s route->src:%s\n",
  818. format_prefix(src->prefix, src->plen),
  819. format_prefix(route->src->prefix, route->src->plen));
  820. oldsrc = route->src;
  821. oldmetric = route_metric(route);
  822. /* If a successor switches sources, we must accept his update even
  823. if it makes a route unfeasible in order to break any routing loops
  824. in a timely manner. If the source remains the same, we ignore
  825. the update. */
  826. if(!feasible && route->installed) {
  827. debugf("Unfeasible update for installed route to %s "
  828. "(%s %d %d -> %s %d %d).\n",
  829. format_prefix(src->prefix, src->plen),
  830. format_eui64(route->src->id),
  831. route->seqno, route->refmetric,
  832. format_eui64(src->id), seqno, refmetric);
  833. if(src != route->src) {
  834. debugf(" unistall_route!\n");
  835. uninstall_route(route);
  836. lost = 1;
  837. }
  838. }
  839. route->src = retain_source(src);
  840. if((feasible || keep_unfeasible) && refmetric < INFINITY)
  841. route->time = now.tv_sec;
  842. route->seqno = seqno;
  843. if(channels_len == 0) {
  844. free(route->channels);
  845. route->channels = NULL;
  846. route->channels_len = 0;
  847. } else {
  848. if(channels_len != route->channels_len) {
  849. unsigned char *new_channels =
  850. realloc(route->channels, channels_len);
  851. if(new_channels == NULL) {
  852. perror("malloc(channels)");
  853. /* Truncate the data. */
  854. channels_len = MIN(channels_len, route->channels_len);
  855. } else {
  856. route->channels = new_channels;
  857. }
  858. }
  859. memcpy(route->channels, channels, channels_len);
  860. route->channels_len = channels_len;
  861. }
  862. change_route_metric(route,
  863. refmetric, neighbour_cost(neigh), add_metric);
  864. route->hold_time = hold_time;
  865. route_changed(route, oldsrc, oldmetric);
  866. if(lost)
  867. route_lost(oldsrc, oldmetric);
  868. if(!feasible)
  869. send_unfeasible_request(neigh, route->installed && route_old(route),
  870. seqno, metric, src);
  871. release_source(oldsrc);
  872. } else {
  873. struct babel_route *new_route;
  874. debugf(" route NOT FOUND, adding a new one\n");
  875. if(refmetric >= INFINITY)
  876. /* Somebody's retracting a route we never saw. */
  877. return NULL;
  878. if(!feasible) {
  879. debugf(" send_unfeasible request..");
  880. send_unfeasible_request(neigh, 0, seqno, metric, src);
  881. if(!keep_unfeasible)
  882. return NULL;
  883. }
  884. route = calloc(1, sizeof(struct babel_route));
  885. if(route == NULL) {
  886. perror("malloc(route)");
  887. return NULL;
  888. }
  889. route->src = retain_source(src);
  890. route->refmetric = refmetric;
  891. route->cost = neighbour_cost(neigh);
  892. route->add_metric = add_metric;
  893. route->seqno = seqno;
  894. route->neigh = neigh;
  895. memcpy(route->nexthop, nexthop, 16);
  896. route->time = now.tv_sec;
  897. route->hold_time = hold_time;
  898. route->smoothed_metric = MAX(route_metric(route), INFINITY / 2);
  899. route->smoothed_metric_time = now.tv_sec;
  900. if(channels_len > 0) {
  901. route->channels = malloc(channels_len);
  902. if(route->channels == NULL) {
  903. perror("malloc(channels)");
  904. } else {
  905. memcpy(route->channels, channels, channels_len);
  906. }
  907. }
  908. route->next = NULL;
  909. debugf(" insert_route");
  910. new_route = insert_route(route);
  911. debugf(" --route_inserted: prefix: %s, cipher: %s\n",
  912. format_prefix(new_route->src->prefix,
  913. new_route->src->plen),
  914. reduced_lorauth_token(new_route->src->cipher));
  915. if(new_route == NULL) {
  916. fprintf(stderr, "Couldn't insert route.\n");
  917. destroy_route(route);
  918. return NULL;
  919. }
  920. debugf(" local_notify_route\n");
  921. local_notify_route(route, LOCAL_ADD);
  922. debugf(" consider_route\n");
  923. consider_route(route);
  924. }
  925. return route;
  926. }
  927. /* We just received an unfeasible update. If it's any good, send
  928. a request for a new seqno. */
  929. void
  930. send_unfeasible_request(struct neighbour *neigh, int force,
  931. unsigned short seqno, unsigned short metric,
  932. struct source *src)
  933. {
  934. struct babel_route *route = find_installed_route(src->prefix, src->plen,
  935. src->src_prefix,
  936. src->src_plen);
  937. if(seqno_minus(src->seqno, seqno) > 100) {
  938. /* Probably a source that lost its seqno. Let it time-out. */
  939. return;
  940. }
  941. if(force || !route || route_metric(route) >= metric + 512) {
  942. send_unicast_multihop_request(neigh, src->prefix, src->plen,
  943. src->src_prefix, src->src_plen,
  944. src->metric >= INFINITY ?
  945. src->seqno :
  946. seqno_plus(src->seqno, 1),
  947. src->id, 127);
  948. }
  949. }
  950. /* This takes a feasible route and decides whether to install it.
  951. This uses the strong ordering, which is defined by sm <= sm' AND
  952. m <= m'. This ordering is not total, which is what causes
  953. hysteresis. */
  954. void
  955. consider_route(struct babel_route *route)
  956. {
  957. struct babel_route *installed;
  958. struct xroute *xroute;
  959. if(route->installed)
  960. return;
  961. if(!route_feasible(route))
  962. return;
  963. debugf(" consider_route:find_xroute\n");
  964. xroute = find_xroute(route->src->prefix, route->src->plen,
  965. route->src->src_prefix, route->src->src_plen);
  966. // testing
  967. if (xroute)
  968. debugf(" found xroute: xroute->prefix: %s, xroute->cipher:%s\n",
  969. format_prefix(xroute->prefix, xroute->plen),
  970. reduced_lorauth_token(xroute->cipher));
  971. else
  972. debugf(" not found xroute\n");
  973. if(xroute && (allow_duplicates < 0 || xroute->metric >= allow_duplicates))
  974. return;
  975. debugf(" consider_route: find_installed_route\n");
  976. installed = find_installed_route(route->src->prefix, route->src->plen,
  977. route->src->src_prefix,
  978. route->src->src_plen);
  979. if(installed == NULL)
  980. goto install;
  981. if(route_metric(route) >= INFINITY)
  982. return;
  983. if(route_metric(installed) >= INFINITY)
  984. goto install;
  985. if(route_metric(installed) >= route_metric(route) &&
  986. route_smoothed_metric(installed) > route_smoothed_metric(route))
  987. goto install;
  988. return;
  989. install:
  990. switch_routes(installed, route);
  991. if(installed && route->installed)
  992. send_triggered_update(route, installed->src, route_metric(installed));
  993. else
  994. send_update(NULL, 1, route->src->prefix, route->src->plen,
  995. route->src->src_prefix, route->src->src_plen,
  996. // -- lorauth --
  997. route->src->cipher, route->src->clen);
  998. return;
  999. }
  1000. void
  1001. retract_neighbour_routes(struct neighbour *neigh)
  1002. {
  1003. int i;
  1004. for(i = 0; i < route_slots; i++) {
  1005. struct babel_route *r = routes[i];
  1006. while(r) {
  1007. if(r->neigh == neigh) {
  1008. if(r->refmetric != INFINITY) {
  1009. unsigned short oldmetric = route_metric(r);
  1010. retract_route(r);
  1011. if(oldmetric != INFINITY)
  1012. route_changed(r, r->src, oldmetric);
  1013. }
  1014. }
  1015. r = r->next;
  1016. }
  1017. }
  1018. }
  1019. void
  1020. send_triggered_update(struct babel_route *route, struct source *oldsrc,
  1021. unsigned oldmetric)
  1022. {
  1023. unsigned newmetric, diff;
  1024. /* 1 means send speedily, 2 means resend */
  1025. int urgent;
  1026. if(!route->installed)
  1027. return;
  1028. newmetric = route_metric(route);
  1029. diff =
  1030. newmetric >= oldmetric ? newmetric - oldmetric : oldmetric - newmetric;
  1031. if(route->src != oldsrc || (oldmetric < INFINITY && newmetric >= INFINITY))
  1032. /* Switching sources can cause transient routing loops.
  1033. Retractions can cause blackholes. */
  1034. urgent = 2;
  1035. else if(newmetric > oldmetric && oldmetric < 6 * 256 && diff >= 512)
  1036. /* Route getting significantly worse */
  1037. urgent = 1;
  1038. else if(unsatisfied_request(route->src->prefix, route->src->plen,
  1039. route->src->src_prefix, route->src->src_plen,
  1040. route->seqno, route->src->id))
  1041. /* Make sure that requests are satisfied speedily */
  1042. urgent = 1;
  1043. else if(oldmetric >= INFINITY && newmetric < INFINITY)
  1044. /* New route */
  1045. urgent = 0;
  1046. else if(newmetric < oldmetric && diff < 1024)
  1047. /* Route getting better. This may be a transient fluctuation, so
  1048. don't advertise it to avoid making routes unfeasible later on. */
  1049. return;
  1050. else if(diff < 384)
  1051. /* Don't fret about trivialities */
  1052. return;
  1053. else
  1054. urgent = 0;
  1055. if(urgent >= 2)
  1056. send_update_resend(NULL, route->src->prefix, route->src->plen,
  1057. route->src->src_prefix, route->src->src_plen,
  1058. // -- lorauth --
  1059. route->src->cipher, route->src->clen);
  1060. else
  1061. send_update(NULL, urgent, route->src->prefix, route->src->plen,
  1062. route->src->src_prefix, route->src->src_plen,
  1063. // lorauth
  1064. route->src->cipher, route->src->clen);
  1065. if(oldmetric < INFINITY) {
  1066. if(newmetric >= oldmetric + 288) {
  1067. send_request(NULL, route->src->prefix, route->src->plen,
  1068. route->src->src_prefix, route->src->src_plen);
  1069. }
  1070. }
  1071. }
  1072. /* A route has just changed. Decide whether to switch to a different route or
  1073. send an update. */
  1074. void
  1075. route_changed(struct babel_route *route,
  1076. struct source *oldsrc, unsigned short oldmetric)
  1077. {
  1078. if(route->installed) {
  1079. struct babel_route *better_route;
  1080. /* Do this unconditionally -- microoptimisation is not worth it. */
  1081. better_route =
  1082. find_best_route(route->src->prefix, route->src->plen,
  1083. route->src->src_prefix, route->src->src_plen,
  1084. 1, NULL);
  1085. if(better_route && route_metric(better_route) < route_metric(route))
  1086. consider_route(better_route);
  1087. }
  1088. if(route->installed) {
  1089. /* We didn't change routes after all. */
  1090. send_triggered_update(route, oldsrc, oldmetric);
  1091. } else {
  1092. /* Reconsider routes even when their metric didn't decrease,
  1093. they may not have been feasible before. */
  1094. consider_route(route);
  1095. }
  1096. }
  1097. /* We just lost the installed route to a given destination. */
  1098. void
  1099. route_lost(struct source *src, unsigned oldmetric)
  1100. {
  1101. struct babel_route *new_route;
  1102. new_route = find_best_route(src->prefix, src->plen,
  1103. src->src_prefix, src->src_plen, 1, NULL);
  1104. if(new_route) {
  1105. consider_route(new_route);
  1106. } else if(oldmetric < INFINITY) {
  1107. /* Avoid creating a blackhole. */
  1108. send_update_resend(NULL, src->prefix, src->plen,
  1109. src->src_prefix, src->src_plen,
  1110. // -- lorauth --
  1111. src->cipher, src->clen);
  1112. /* If the route was usable enough, try to get an alternate one.
  1113. If it was not, we could be dealing with oscillations around
  1114. the value of INFINITY. */
  1115. if(oldmetric <= INFINITY / 2)
  1116. send_request_resend(NULL, src->prefix, src->plen,
  1117. src->src_prefix, src->src_plen,
  1118. src->metric >= INFINITY ?
  1119. src->seqno : seqno_plus(src->seqno, 1),
  1120. src->id);
  1121. }
  1122. }
  1123. /* This is called periodically to flush old routes. It will also send
  1124. requests for routes that are about to expire. */
  1125. void
  1126. expire_routes(void)
  1127. {
  1128. struct babel_route *r;
  1129. int i;
  1130. debugf("Expiring old routes.\n");
  1131. i = 0;
  1132. while(i < route_slots) {
  1133. r = routes[i];
  1134. while(r) {
  1135. /* Protect against clock being stepped. */
  1136. if(r->time > now.tv_sec || route_old(r)) {
  1137. flush_route(r);
  1138. goto again;
  1139. }
  1140. update_route_metric(r);
  1141. if(r->installed && r->refmetric < INFINITY) {
  1142. if(route_old(r))
  1143. /* Route about to expire, send a request. */
  1144. send_unicast_request(r->neigh,
  1145. r->src->prefix, r->src->plen,
  1146. r->src->src_prefix, r->src->src_plen);
  1147. }
  1148. r = r->next;
  1149. }
  1150. i++;
  1151. again:
  1152. ;
  1153. }
  1154. }