message.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  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 <string.h>
  21. #include <stdio.h>
  22. #include <assert.h>
  23. #include <sys/time.h>
  24. #include <netinet/in.h>
  25. #include <arpa/inet.h>
  26. #include "babeld.h"
  27. #include "util.h"
  28. #include "net.h"
  29. #include "interface.h"
  30. #include "source.h"
  31. #include "neighbour.h"
  32. #include "route.h"
  33. #include "kernel.h"
  34. #include "xroute.h"
  35. #include "resend.h"
  36. #include "message.h"
  37. #include "configuration.h"
  38. #include "lorauth.h"
  39. unsigned char packet_header[4] = {42, 2};
  40. int split_horizon = 1;
  41. unsigned short myseqno = 0;
  42. struct timeval seqno_time = {0, 0};
  43. #define UNICAST_BUFSIZE 1024
  44. int unicast_buffered = 0;
  45. unsigned char *unicast_buffer = NULL;
  46. struct neighbour *unicast_neighbour = NULL;
  47. struct timeval unicast_flush_timeout = {0, 0};
  48. extern const unsigned char v4prefix[16];
  49. // -- lorauth --
  50. unsigned char buffered_cipher[514];
  51. // ----
  52. #define MAX_CHANNEL_HOPS 20
  53. /* Parse a network prefix, encoded in the somewhat baroque compressed
  54. representation used by Babel. Return the number of bytes parsed. */
  55. static int
  56. network_prefix(int ae, int plen, unsigned int omitted,
  57. const unsigned char *p, const unsigned char *dp,
  58. unsigned int len, unsigned char *p_r)
  59. {
  60. unsigned pb;
  61. unsigned char prefix[16];
  62. int ret = -1;
  63. if(plen >= 0)
  64. pb = (plen + 7) / 8;
  65. else if(ae == 1)
  66. pb = 4;
  67. else
  68. pb = 16;
  69. if(pb > 16)
  70. return -1;
  71. memset(prefix, 0, 16);
  72. switch(ae) {
  73. case 0:
  74. ret = 0;
  75. break;
  76. case 1:
  77. if(omitted > 4 || pb > 4 || (pb > omitted && len < pb - omitted))
  78. return -1;
  79. memcpy(prefix, v4prefix, 12);
  80. if(omitted) {
  81. if(dp == NULL || !v4mapped(dp)) return -1;
  82. memcpy(prefix, dp, 12 + omitted);
  83. }
  84. if(pb > omitted) memcpy(prefix + 12 + omitted, p, pb - omitted);
  85. ret = pb - omitted;
  86. break;
  87. case 2:
  88. if(omitted > 16 || (pb > omitted && len < pb - omitted)) return -1;
  89. if(omitted) {
  90. if(dp == NULL || v4mapped(dp)) return -1;
  91. memcpy(prefix, dp, omitted);
  92. }
  93. if(pb > omitted) memcpy(prefix + omitted, p, pb - omitted);
  94. ret = pb - omitted;
  95. break;
  96. case 3:
  97. if(pb > 8 && len < pb - 8) return -1;
  98. prefix[0] = 0xfe;
  99. prefix[1] = 0x80;
  100. if(pb > 8) memcpy(prefix + 8, p, pb - 8);
  101. ret = pb - 8;
  102. break;
  103. default:
  104. return -1;
  105. }
  106. normalize_prefix(p_r, prefix, plen < 0 ? 128 : ae == 1 ? plen + 96 : plen);
  107. return ret;
  108. }
  109. static void
  110. parse_update_subtlv(struct interface *ifp, int metric,
  111. const unsigned char *a, int alen,
  112. unsigned char *channels, int *channels_len_return)
  113. {
  114. int type, len, i = 0;
  115. int channels_len;
  116. /* This will be overwritten if there's a DIVERSITY_HOPS sub-TLV. */
  117. if(*channels_len_return < 1 || (ifp->flags & IF_FARAWAY)) {
  118. channels_len = 0;
  119. } else {
  120. if(metric < 256) {
  121. /* Assume non-interfering (wired) link. */
  122. channels_len = 0;
  123. } else {
  124. /* Assume interfering. */
  125. channels[0] = IF_CHANNEL_INTERFERING;
  126. channels_len = 1;
  127. }
  128. }
  129. while(i < alen) {
  130. type = a[i];
  131. if(type == SUBTLV_PAD1) {
  132. i++;
  133. continue;
  134. }
  135. if(i + 1 > alen) {
  136. fprintf(stderr, "Received truncated attributes.\n");
  137. return;
  138. }
  139. len = a[i + 1];
  140. if(i + len > alen) {
  141. fprintf(stderr, "Received truncated attributes.\n");
  142. return;
  143. }
  144. if(type == SUBTLV_PADN) {
  145. /* Nothing. */
  146. } else if(type == SUBTLV_DIVERSITY) {
  147. memcpy(channels, a + i + 2, MIN(len, *channels_len_return));
  148. channels_len = MIN(len, *channels_len_return);
  149. } else {
  150. debugf("Received unknown update sub-TLV %d.\n", type);
  151. }
  152. i += len + 2;
  153. }
  154. *channels_len_return = channels_len;
  155. }
  156. static int
  157. parse_hello_subtlv(const unsigned char *a, int alen,
  158. unsigned int *hello_send_us)
  159. {
  160. int type, len, i = 0, ret = 0;
  161. while(i < alen) {
  162. type = a[0];
  163. if(type == SUBTLV_PAD1) {
  164. i++;
  165. continue;
  166. }
  167. if(i + 1 > alen) {
  168. fprintf(stderr, "Received truncated sub-TLV on Hello message.\n");
  169. return -1;
  170. }
  171. len = a[i + 1];
  172. if(i + len > alen) {
  173. fprintf(stderr, "Received truncated sub-TLV on Hello message.\n");
  174. return -1;
  175. }
  176. if(type == SUBTLV_PADN) {
  177. /* Nothing to do. */
  178. } else if(type == SUBTLV_TIMESTAMP) {
  179. if(len >= 4) {
  180. DO_NTOHL(*hello_send_us, a + i + 2);
  181. ret = 1;
  182. } else {
  183. fprintf(stderr,
  184. "Received incorrect RTT sub-TLV on Hello message.\n");
  185. }
  186. } else {
  187. debugf("Received unknown Hello sub-TLV type %d.\n", type);
  188. }
  189. i += len + 2;
  190. }
  191. return ret;
  192. }
  193. static int
  194. parse_ihu_subtlv(const unsigned char *a, int alen,
  195. unsigned int *hello_send_us,
  196. unsigned int *hello_rtt_receive_time)
  197. {
  198. int type, len, i = 0, ret = 0;
  199. while(i < alen) {
  200. type = a[0];
  201. if(type == SUBTLV_PAD1) {
  202. i++;
  203. continue;
  204. }
  205. if(i + 1 > alen) {
  206. fprintf(stderr, "Received truncated sub-TLV on IHU message.\n");
  207. return -1;
  208. }
  209. len = a[i + 1];
  210. if(i + len > alen) {
  211. fprintf(stderr, "Received truncated sub-TLV on IHU message.\n");
  212. return -1;
  213. }
  214. if(type == SUBTLV_PADN) {
  215. /* Nothing to do. */
  216. } else if(type == SUBTLV_TIMESTAMP) {
  217. if(len >= 8) {
  218. DO_NTOHL(*hello_send_us, a + i + 2);
  219. DO_NTOHL(*hello_rtt_receive_time, a + i + 6);
  220. ret = 1;
  221. }
  222. else {
  223. fprintf(stderr,
  224. "Received incorrect RTT sub-TLV on IHU message.\n");
  225. }
  226. } else {
  227. debugf("Received unknown IHU sub-TLV type %d.\n", type);
  228. }
  229. i += len + 2;
  230. }
  231. return ret;
  232. }
  233. static int
  234. network_address(int ae, const unsigned char *a, unsigned int len,
  235. unsigned char *a_r)
  236. {
  237. return network_prefix(ae, -1, 0, a, NULL, len, a_r);
  238. }
  239. void
  240. parse_packet(const unsigned char *from, struct interface *ifp,
  241. const unsigned char *packet, int packetlen)
  242. {
  243. int i;
  244. const unsigned char *message;
  245. unsigned char type, len;
  246. int bodylen;
  247. struct neighbour *neigh;
  248. int have_router_id = 0, have_v4_prefix = 0, have_v6_prefix = 0,
  249. have_v4_nh = 0, have_v6_nh = 0;
  250. unsigned char router_id[8], v4_prefix[16], v6_prefix[16],
  251. v4_nh[16], v6_nh[16];
  252. int have_hello_rtt = 0;
  253. /* Content of the RTT sub-TLV on IHU messages. */
  254. unsigned int hello_send_us = 0, hello_rtt_receive_time = 0;
  255. // -- lorauth --
  256. unsigned short clen = 0;
  257. clean_cipher(buffered_cipher);
  258. // ----
  259. if(ifp->flags & IF_TIMESTAMPS) {
  260. /* We want to track exactly when we received this packet. */
  261. gettime(&now);
  262. }
  263. if(!linklocal(from)) {
  264. fprintf(stderr, "Received packet from non-local address %s.\n",
  265. format_address(from));
  266. return;
  267. }
  268. if(packet[0] != 42) {
  269. fprintf(stderr, "Received malformed packet on %s from %s.\n",
  270. ifp->name, format_address(from));
  271. return;
  272. }
  273. if(packet[1] != 2) {
  274. fprintf(stderr,
  275. "Received packet with unknown version %d on %s from %s.\n",
  276. packet[1], ifp->name, format_address(from));
  277. return;
  278. }
  279. neigh = find_neighbour(from, ifp);
  280. if(neigh == NULL) {
  281. fprintf(stderr, "Couldn't allocate neighbour.\n");
  282. return;
  283. }
  284. DO_NTOHS(bodylen, packet + 2);
  285. if(bodylen + 4 > packetlen) {
  286. fprintf(stderr, "Received truncated packet (%d + 4 > %d).\n",
  287. bodylen, packetlen);
  288. bodylen = packetlen - 4;
  289. }
  290. i = 0;
  291. while(i < bodylen) {
  292. message = packet + 4 + i;
  293. type = message[0];
  294. if(type == MESSAGE_PAD1) {
  295. debugf("Received pad1 from %s on %s.\n",
  296. format_address(from), ifp->name);
  297. i++;
  298. continue;
  299. }
  300. if(i + 1 > bodylen) {
  301. fprintf(stderr, "Received truncated message.\n");
  302. break;
  303. }
  304. len = message[1];
  305. if(i + len > bodylen) {
  306. fprintf(stderr, "Received truncated message.\n");
  307. break;
  308. }
  309. if(type == MESSAGE_PADN) {
  310. debugf("Received pad%d from %s on %s.\n",
  311. len, format_address(from), ifp->name);
  312. } else if(type == MESSAGE_ACK_REQ) {
  313. unsigned short nonce, interval;
  314. if(len < 6) goto fail;
  315. DO_NTOHS(nonce, message + 4);
  316. DO_NTOHS(interval, message + 6);
  317. debugf("Received ack-req (%04X %d) from %s on %s.\n",
  318. nonce, interval, format_address(from), ifp->name);
  319. send_ack(neigh, nonce, interval);
  320. } else if(type == MESSAGE_ACK) {
  321. debugf("Received ack from %s on %s.\n",
  322. format_address(from), ifp->name);
  323. /* Nothing right now */
  324. } else if(type == MESSAGE_HELLO) {
  325. unsigned short seqno, interval;
  326. int changed;
  327. unsigned int timestamp;
  328. if(len < 6) goto fail;
  329. DO_NTOHS(seqno, message + 4);
  330. DO_NTOHS(interval, message + 6);
  331. debugf("Received hello %d (%d) from %s on %s.\n",
  332. seqno, interval,
  333. format_address(from), ifp->name);
  334. changed = update_neighbour(neigh, seqno, interval);
  335. update_neighbour_metric(neigh, changed);
  336. if(interval > 0)
  337. /* Multiply by 3/2 to allow hellos to expire. */
  338. schedule_neighbours_check(interval * 15, 0);
  339. /* Sub-TLV handling. */
  340. if(len > 8) {
  341. if(parse_hello_subtlv(message + 8, len - 6, &timestamp) > 0) {
  342. neigh->hello_send_us = timestamp;
  343. neigh->hello_rtt_receive_time = now;
  344. have_hello_rtt = 1;
  345. }
  346. }
  347. } else if(type == MESSAGE_IHU) {
  348. unsigned short txcost, interval;
  349. unsigned char address[16];
  350. int rc;
  351. if(len < 6) goto fail;
  352. DO_NTOHS(txcost, message + 4);
  353. DO_NTOHS(interval, message + 6);
  354. rc = network_address(message[2], message + 8, len - 6, address);
  355. if(rc < 0) goto fail;
  356. debugf("Received ihu %d (%d) from %s on %s for %s.\n",
  357. txcost, interval,
  358. format_address(from), ifp->name,
  359. format_address(address));
  360. if(message[2] == 0 || interface_ll_address(ifp, address)) {
  361. int changed = txcost != neigh->txcost;
  362. neigh->txcost = txcost;
  363. neigh->ihu_time = now;
  364. neigh->ihu_interval = interval;
  365. update_neighbour_metric(neigh, changed);
  366. if(interval > 0)
  367. /* Multiply by 3/2 to allow neighbours to expire. */
  368. schedule_neighbours_check(interval * 45, 0);
  369. /* RTT sub-TLV. */
  370. if(len > 10 + rc)
  371. parse_ihu_subtlv(message + 8 + rc, len - 6 - rc,
  372. &hello_send_us, &hello_rtt_receive_time);
  373. }
  374. } else if(type == MESSAGE_ROUTER_ID) {
  375. if(len < 10) {
  376. have_router_id = 0;
  377. goto fail;
  378. }
  379. memcpy(router_id, message + 4, 8);
  380. have_router_id = 1;
  381. debugf("Received router-id %s from %s on %s.\n",
  382. format_eui64(router_id), format_address(from), ifp->name);
  383. } else if(type == MESSAGE_NH) {
  384. unsigned char nh[16];
  385. int rc;
  386. if(len < 2) {
  387. have_v4_nh = 0;
  388. have_v6_nh = 0;
  389. goto fail;
  390. }
  391. rc = network_address(message[2], message + 4, len - 2,
  392. nh);
  393. if(rc < 0) {
  394. have_v4_nh = 0;
  395. have_v6_nh = 0;
  396. goto fail;
  397. }
  398. debugf("Received nh %s (%d) from %s on %s.\n",
  399. format_address(nh), message[2],
  400. format_address(from), ifp->name);
  401. if(message[2] == 1) {
  402. memcpy(v4_nh, nh, 16);
  403. have_v4_nh = 1;
  404. } else {
  405. memcpy(v6_nh, nh, 16);
  406. have_v6_nh = 1;
  407. }
  408. } else if(type == MESSAGE_UPDATE) {
  409. unsigned char prefix[16], *nh;
  410. unsigned char plen;
  411. unsigned char channels[MAX_CHANNEL_HOPS];
  412. int channels_len = MAX_CHANNEL_HOPS;
  413. unsigned short interval, seqno, metric;
  414. int rc, parsed_len;
  415. if(len < 10) {
  416. if(len < 2 || message[3] & 0x80)
  417. have_v4_prefix = have_v6_prefix = 0;
  418. goto fail;
  419. }
  420. DO_NTOHS(interval, message + 6);
  421. DO_NTOHS(seqno, message + 8);
  422. DO_NTOHS(metric, message + 10);
  423. if(message[5] == 0 ||
  424. (message[2] == 1 ? have_v4_prefix : have_v6_prefix))
  425. rc = network_prefix(message[2], message[4], message[5],
  426. message + 12,
  427. message[2] == 1 ? v4_prefix : v6_prefix,
  428. len - 10, prefix);
  429. else
  430. rc = -1;
  431. if(rc < 0) {
  432. if(message[3] & 0x80)
  433. have_v4_prefix = have_v6_prefix = 0;
  434. goto fail;
  435. }
  436. parsed_len = 10 + rc;
  437. plen = message[4] + (message[2] == 1 ? 96 : 0);
  438. if(message[3] & 0x80) {
  439. if(message[2] == 1) {
  440. memcpy(v4_prefix, prefix, 16);
  441. have_v4_prefix = 1;
  442. } else {
  443. memcpy(v6_prefix, prefix, 16);
  444. have_v6_prefix = 1;
  445. }
  446. }
  447. if(message[3] & 0x40) {
  448. if(message[2] == 1) {
  449. memset(router_id, 0, 4);
  450. memcpy(router_id + 4, prefix + 12, 4);
  451. } else {
  452. memcpy(router_id, prefix + 8, 8);
  453. }
  454. have_router_id = 1;
  455. }
  456. if(!have_router_id && message[2] != 0) {
  457. fprintf(stderr, "Received prefix with no router id.\n");
  458. goto fail;
  459. }
  460. debugf("Received update%s%s for %s from %s on %s.\n",
  461. (message[3] & 0x80) ? "/prefix" : "",
  462. (message[3] & 0x40) ? "/id" : "",
  463. format_prefix(prefix, plen),
  464. format_address(from), ifp->name);
  465. // -- lorauth --
  466. debugf("--trying update_route\n");
  467. if(strlen((char *)buffered_cipher)>3){
  468. debugf(" --Cipher on update\n");
  469. debugf(" buffered_cipher:%s size: %d\n" ,
  470. reduced_lorauth_token(buffered_cipher),
  471. (unsigned int)strlen((char *)buffered_cipher));
  472. /* Checking authentication (see: doc-lorauth/README.es.md)
  473. The rule here is, if the route entry exists (meaning that the
  474. update has alread been authenticated) there is no need to do
  475. the authentication process.
  476. If the route entry does not exists, then the authentication process
  477. has to be done, it means decrypting the received cipher, then get
  478. the prefix and last octets to check (id, seqno). The decrypted
  479. prefix is compared with the announced prefix, then the decrypted
  480. last octets are used to check for the (id, seqno) announced.
  481. If the authentication is complete the update is considered, if not
  482. it is ignored.
  483. */
  484. printf(" --- lorauth AUTHENTICATION ---\n");
  485. struct babel_route *a_route;
  486. a_route = find_installed_route(prefix, plen, zeroes, 0);
  487. if(a_route){
  488. debugf(" announced route already authenticated, skiping.\n");
  489. }
  490. else{
  491. rc = check_lorauth_token(router_id,
  492. prefix,
  493. seqno,
  494. clen,
  495. buffered_cipher);
  496. if(rc == -1) {
  497. // some logs
  498. printf("\n\tprefix: %s\n\tid:%s",
  499. format_prefix(prefix, plen),
  500. format_eui64(router_id));
  501. printf("\n\t\t*** FAILED ***\n\n");
  502. goto done;
  503. }
  504. else if(rc==0){
  505. printf("\n\tprefix: %s\n\tid:%s\n\tcipher: %s",
  506. format_prefix(prefix, plen),
  507. format_eui64(router_id),
  508. reduced_lorauth_token(buffered_cipher));
  509. printf("\n\t\t~ ~ ~ ~ ~\n\t\t PASSED \n\t\t~ ~ ~ ~ ~ ~\n");
  510. }
  511. }
  512. }
  513. else{
  514. printf("\n\tprefix: %s\n\tid:%s",
  515. format_prefix(prefix, plen),
  516. format_eui64(router_id));
  517. printf(" \n\tNo Cipher on update\n\n\t\t***** REJECTING UPDATE *****\n");
  518. goto done;
  519. }
  520. // ----
  521. if(message[2] == 0) {
  522. if(metric < 0xFFFF) {
  523. fprintf(stderr,
  524. "Received wildcard update with finite metric.\n");
  525. goto done;
  526. }
  527. retract_neighbour_routes(neigh);
  528. goto done;
  529. } else if(message[2] == 1) {
  530. if(!have_v4_nh)
  531. goto fail;
  532. nh = v4_nh;
  533. } else if(have_v6_nh) {
  534. nh = v6_nh;
  535. } else {
  536. nh = neigh->address;
  537. }
  538. if(message[2] == 1) {
  539. if(!ifp->ipv4)
  540. goto done;
  541. }
  542. parse_update_subtlv(ifp, metric, message + 2 + parsed_len,
  543. len - parsed_len, channels, &channels_len);
  544. update_route(router_id, prefix, plen, zeroes, 0,
  545. buffered_cipher, clen,
  546. seqno,
  547. metric, interval, neigh, nh,
  548. channels, channels_len);
  549. } else if(type == MESSAGE_REQUEST) {
  550. unsigned char prefix[16], plen;
  551. int rc;
  552. if(len < 2) goto fail;
  553. rc = network_prefix(message[2], message[3], 0,
  554. message + 4, NULL, len - 2, prefix);
  555. if(rc < 0) goto fail;
  556. plen = message[3] + (message[2] == 1 ? 96 : 0);
  557. debugf("Received request for %s from %s on %s.\n",
  558. message[2] == 0 ? "any" : format_prefix(prefix, plen),
  559. format_address(from), ifp->name);
  560. if(message[2] == 0) {
  561. /* If a neighbour is requesting a full route dump from us,
  562. we might as well send it an IHU. */
  563. send_ihu(neigh, NULL);
  564. /* Since nodes send wildcard requests on boot, booting
  565. a large number of nodes at the same time may cause an
  566. update storm. Ignore a wildcard request that happens
  567. shortly after we sent a full update. */
  568. if(neigh->ifp->last_update_time <
  569. now.tv_sec - MAX(neigh->ifp->hello_interval / 100, 1))
  570. /* lorauth note: Here check if it is correct to send a
  571. proably empty buffered_cipher.*/
  572. send_update(neigh->ifp, 0, NULL, 0, zeroes, 0,
  573. // -- lorauth --
  574. buffered_cipher, clen);
  575. } else {
  576. send_update(neigh->ifp, 0, prefix, plen, zeroes, 0,
  577. // -- lorauth --
  578. buffered_cipher, clen);
  579. }
  580. } else if(type == MESSAGE_MH_REQUEST) {
  581. unsigned char prefix[16], plen;
  582. unsigned short seqno;
  583. int rc;
  584. if(len < 14) goto fail;
  585. DO_NTOHS(seqno, message + 4);
  586. rc = network_prefix(message[2], message[3], 0,
  587. message + 16, NULL, len - 14, prefix);
  588. if(rc < 0) goto fail;
  589. plen = message[3] + (message[2] == 1 ? 96 : 0);
  590. debugf("Received request (%d) for %s from %s on %s (%s, %d).\n",
  591. message[6],
  592. format_prefix(prefix, plen),
  593. format_address(from), ifp->name,
  594. format_eui64(message + 8), seqno);
  595. /* lorauth note: Here handle_request checks whether it is needed to change
  596. lorauth token when the seqno is changed
  597. */
  598. handle_request(neigh, prefix, plen, zeroes, 0, message[6], seqno, message + 8);
  599. } else if(type == MESSAGE_UPDATE_SRC_SPECIFIC) {
  600. unsigned char prefix[16], src_prefix[16], *nh;
  601. unsigned char ae, plen, src_plen, omitted;
  602. unsigned char channels[MAX_CHANNEL_HOPS];
  603. int channels_len = MAX_CHANNEL_HOPS;
  604. unsigned short interval, seqno, metric;
  605. const unsigned char *src_prefix_beginning = NULL;
  606. int rc, parsed_len = 0;
  607. if(len < 10)
  608. goto fail;
  609. ae = message[2];
  610. src_plen = message[3];
  611. plen = message[4];
  612. omitted = message[5];
  613. DO_NTOHS(interval, message + 6);
  614. DO_NTOHS(seqno, message + 8);
  615. DO_NTOHS(metric, message + 10);
  616. if(omitted == 0 || (ae == 1 ? have_v4_prefix : have_v6_prefix))
  617. rc = network_prefix(ae, plen, omitted, message + 12,
  618. ae == 1 ? v4_prefix : v6_prefix,
  619. len - 10, prefix);
  620. else
  621. rc = -1;
  622. if(rc < 0)
  623. goto fail;
  624. parsed_len = 10 + rc;
  625. src_prefix_beginning = message + 2 + parsed_len;
  626. rc = network_prefix(ae, src_plen, 0, src_prefix_beginning, NULL,
  627. len - parsed_len, src_prefix);
  628. if(rc < 0)
  629. goto fail;
  630. parsed_len += rc;
  631. if(ae == 1) {
  632. plen += 96;
  633. src_plen += 96;
  634. }
  635. if(!have_router_id) {
  636. fprintf(stderr, "Received prefix with no router id.\n");
  637. goto fail;
  638. }
  639. debugf("Received ss-update for (%s from %s) from %s on %s.\n",
  640. format_prefix(prefix, plen),
  641. format_prefix(src_prefix, src_plen),
  642. format_address(from), ifp->name);
  643. if(ae == 0) {
  644. debugf("Received invalid Source-Specific wildcard update.\n");
  645. retract_neighbour_routes(neigh);
  646. goto done;
  647. } else if(ae == 1) {
  648. if(!have_v4_nh)
  649. goto fail;
  650. nh = v4_nh;
  651. } else if(have_v6_nh) {
  652. nh = v6_nh;
  653. } else {
  654. nh = neigh->address;
  655. }
  656. if(ae == 1) {
  657. if(!ifp->ipv4)
  658. goto done;
  659. }
  660. parse_update_subtlv(ifp, metric, message + 2 + parsed_len,
  661. len - parsed_len, channels, &channels_len);
  662. update_route(router_id, prefix, plen, src_prefix, src_plen,
  663. buffered_cipher, clen,
  664. seqno, metric, interval, neigh, nh,
  665. channels, channels_len);
  666. } else if(type == MESSAGE_REQUEST_SRC_SPECIFIC) {
  667. unsigned char prefix[16], plen, ae, src_prefix[16], src_plen;
  668. int rc, parsed = 5;
  669. if(len < 3) goto fail;
  670. ae = message[2];
  671. plen = message[3];
  672. src_plen = message[4];
  673. rc = network_prefix(ae, plen, 0, message + parsed,
  674. NULL, len + 2 - parsed, prefix);
  675. if(rc < 0) goto fail;
  676. if(ae == 1)
  677. plen += 96;
  678. parsed += rc;
  679. rc = network_prefix(ae, src_plen, 0, message + parsed,
  680. NULL, len + 2 - parsed, src_prefix);
  681. if(rc < 0) goto fail;
  682. if(ae == 1)
  683. src_plen += 96;
  684. parsed += rc;
  685. if(ae == 0) {
  686. debugf("Received request for any source-specific "
  687. "from %s on %s.\n",
  688. format_address(from), ifp->name);
  689. /* See comments for std requests. */
  690. send_ihu(neigh, NULL);
  691. if(neigh->ifp->last_specific_update_time <
  692. now.tv_sec - MAX(neigh->ifp->hello_interval / 100, 1))
  693. send_update(neigh->ifp, 0, zeroes, 0, NULL, 0,
  694. // -- lorauth --
  695. buffered_cipher, clen);
  696. } else {
  697. debugf("Received request for (%s from %s) from %s on %s.\n",
  698. format_prefix(prefix, plen),
  699. format_prefix(src_prefix, src_plen),
  700. format_address(from), ifp->name);
  701. /* send_update(neigh->ifp, 0, prefix, plen, src_prefix, src_plen, */
  702. /* NULL, 0); */
  703. /* check wether to update cipher and clen in this case.*/
  704. send_update(neigh->ifp, 0, prefix, plen, src_prefix, src_plen,
  705. // -- lorauth --
  706. buffered_cipher, clen);
  707. }
  708. } else if(type == MESSAGE_MH_REQUEST_SRC_SPECIFIC) {
  709. unsigned char prefix[16], plen, ae, src_prefix[16], src_plen, hopc;
  710. const unsigned char *router_id;
  711. unsigned short seqno;
  712. int rc, parsed = 16;
  713. if(len < 14) goto fail;
  714. ae = message[2];
  715. plen = message[3];
  716. DO_NTOHS(seqno, message + 4);
  717. hopc = message[6];
  718. src_plen = message[7];
  719. router_id = message + 8;
  720. rc = network_prefix(ae, plen, 0, message + parsed,
  721. NULL, len + 2 - parsed, prefix);
  722. if(rc < 0) goto fail;
  723. if(ae == 1)
  724. plen += 96;
  725. parsed += rc;
  726. rc = network_prefix(ae, src_plen, 0, message + parsed,
  727. NULL, len + 2 - parsed, src_prefix);
  728. if(rc < 0) goto fail;
  729. if(ae == 1)
  730. src_plen += 96;
  731. debugf("Received request (%d) for (%s, %s)"
  732. " from %s on %s (%s, %d).\n",
  733. message[6],
  734. format_prefix(prefix, plen),
  735. format_prefix(src_prefix, src_plen),
  736. format_address(from), ifp->name,
  737. format_eui64(router_id), seqno);
  738. handle_request(neigh, prefix, plen, src_prefix, src_plen,
  739. hopc, seqno, router_id);
  740. }
  741. // -- lorauth ---
  742. else if(type == MESSAGE_LORAUTH) {
  743. /* parsing lorauth TLV
  744. see doc-lorauth/babel-integration.es.md */
  745. unsigned char lorauth_packet_number = message[2];
  746. debugf("-- lorauth packet received Np:%d\n", lorauth_packet_number);
  747. if((lorauth_packet_number == 3 && len != 8) ||
  748. (lorauth_packet_number != 3 && len != 255) ||
  749. len > 255) {
  750. debugf("incorrect length in lorauth packet: %d\n",len);
  751. clen = 0;
  752. goto fail;
  753. }
  754. if((lorauth_packet_number == 3 && message[3] != 6) ||
  755. (lorauth_packet_number != 3 && message[3] != 253) ||
  756. message[3] > 253) {
  757. debugf("incorrect clen in lorauth packet: %c\n", message[3]);
  758. clen = 0;
  759. goto fail;
  760. }
  761. strncpy((char *)buffered_cipher + ((lorauth_packet_number-1)*253) ,
  762. (char *)message + 4, message[3]);
  763. if(strlen((char *)buffered_cipher)==LORAUTH_CIPHER_LEN) {
  764. clen = LORAUTH_CIPHER_LEN;
  765. debugf(" -- received cipher: %s\n", buffered_cipher);
  766. }
  767. /* Checking authentication (see: doc-lorauth/README.es.md)
  768. (it is done when receiving an UPDATE TLV)
  769. The rule here is, if the route entry exists (meaning that the
  770. update has alread been authenticated) there is no need to do
  771. the authentication process.
  772. If the route entry does not exists, then the authentication process
  773. has to be done, it means decrypting the received cipher, then get
  774. the prefix and last octets to check (id, seqno). The decrypted
  775. prefix is compared with the announced prefix, then the decrypted
  776. last octets are used to check for the (id, seqno) announced.
  777. If the authentication is complete the update is considered, if not
  778. it is ignored.
  779. */
  780. }
  781. // ----
  782. else {
  783. debugf("Received unknown packet type %d from %s on %s.\n",
  784. type, format_address(from), ifp->name);
  785. }
  786. done:
  787. i += len + 2;
  788. continue;
  789. fail:
  790. clean_cipher(buffered_cipher);
  791. fprintf(stderr, "Couldn't parse packet (%d, %d) from %s on %s.\n",
  792. message[0], message[1], format_address(from), ifp->name);
  793. goto done;
  794. }
  795. /* We can calculate the RTT to this neighbour. */
  796. if(have_hello_rtt && hello_send_us && hello_rtt_receive_time) {
  797. int remote_waiting_us, local_waiting_us;
  798. unsigned int rtt, smoothed_rtt;
  799. unsigned int old_rttcost;
  800. int changed = 0;
  801. remote_waiting_us = neigh->hello_send_us - hello_rtt_receive_time;
  802. local_waiting_us = time_us(neigh->hello_rtt_receive_time) -
  803. hello_send_us;
  804. /* Sanity checks (validity window of 10 minutes). */
  805. if(remote_waiting_us < 0 || local_waiting_us < 0 ||
  806. remote_waiting_us > 600000000 || local_waiting_us > 600000000)
  807. return;
  808. rtt = MAX(0, local_waiting_us - remote_waiting_us);
  809. debugf("RTT to %s on %s sample result: %d us.\n",
  810. format_address(from), ifp->name, rtt);
  811. old_rttcost = neighbour_rttcost(neigh);
  812. if(valid_rtt(neigh)) {
  813. /* Running exponential average. */
  814. smoothed_rtt = (ifp->rtt_decay * rtt +
  815. (256 - ifp->rtt_decay) * neigh->rtt);
  816. /* Rounding (up or down) to get closer to the sample. */
  817. neigh->rtt = (neigh->rtt >= rtt) ? smoothed_rtt / 256 :
  818. (smoothed_rtt + 255) / 256;
  819. } else {
  820. /* We prefer to be conservative with new neighbours
  821. (higher RTT) */
  822. assert(rtt <= 0x7FFFFFFF);
  823. neigh->rtt = 2*rtt;
  824. }
  825. changed = (neighbour_rttcost(neigh) == old_rttcost ? 0 : 1);
  826. update_neighbour_metric(neigh, changed);
  827. neigh->rtt_time = now;
  828. }
  829. clean_cipher(buffered_cipher);
  830. return;
  831. }
  832. /* Under normal circumstances, there are enough moderation mechanisms
  833. elsewhere in the protocol to make sure that this last-ditch check
  834. should never trigger. But I'm superstitious. */
  835. static int
  836. check_bucket(struct interface *ifp)
  837. {
  838. if(ifp->bucket <= 0) {
  839. int seconds = now.tv_sec - ifp->bucket_time;
  840. if(seconds > 0) {
  841. ifp->bucket = MIN(BUCKET_TOKENS_MAX,
  842. seconds * BUCKET_TOKENS_PER_SEC);
  843. }
  844. /* Reset bucket time unconditionally, in case clock is stepped. */
  845. ifp->bucket_time = now.tv_sec;
  846. }
  847. if(ifp->bucket > 0) {
  848. ifp->bucket--;
  849. return 1;
  850. } else {
  851. return 0;
  852. }
  853. }
  854. static int
  855. fill_rtt_message(struct interface *ifp)
  856. {
  857. if((ifp->flags & IF_TIMESTAMPS) && (ifp->buffered_hello >= 0)) {
  858. if(ifp->sendbuf[ifp->buffered_hello + 8] == SUBTLV_PADN &&
  859. ifp->sendbuf[ifp->buffered_hello + 9] == 4) {
  860. unsigned int time;
  861. /* Change the type of sub-TLV. */
  862. ifp->sendbuf[ifp->buffered_hello + 8] = SUBTLV_TIMESTAMP;
  863. gettime(&now);
  864. time = time_us(now);
  865. DO_HTONL(ifp->sendbuf + ifp->buffered_hello + 10, time);
  866. return 1;
  867. } else {
  868. fprintf(stderr,
  869. "No space left for timestamp sub-TLV "
  870. "(this shouldn't happen)\n");
  871. return -1;
  872. }
  873. }
  874. return 0;
  875. }
  876. void
  877. flushbuf(struct interface *ifp)
  878. {
  879. int rc;
  880. struct sockaddr_in6 sin6;
  881. assert(ifp->buffered <= ifp->bufsize);
  882. flushupdates(ifp);
  883. if(ifp->buffered > 0) {
  884. debugf(" (flushing %d buffered bytes on %s)\n",
  885. ifp->buffered, ifp->name);
  886. if(check_bucket(ifp)) {
  887. memset(&sin6, 0, sizeof(sin6));
  888. sin6.sin6_family = AF_INET6;
  889. memcpy(&sin6.sin6_addr, protocol_group, 16);
  890. sin6.sin6_port = htons(protocol_port);
  891. sin6.sin6_scope_id = ifp->ifindex;
  892. DO_HTONS(packet_header + 2, ifp->buffered);
  893. fill_rtt_message(ifp);
  894. rc = babel_send(protocol_socket,
  895. packet_header, sizeof(packet_header),
  896. ifp->sendbuf, ifp->buffered,
  897. (struct sockaddr*)&sin6, sizeof(sin6));
  898. if(rc < 0)
  899. perror("send");
  900. } else {
  901. fprintf(stderr, "Warning: bucket full, dropping packet to %s.\n",
  902. ifp->name);
  903. }
  904. }
  905. VALGRIND_MAKE_MEM_UNDEFINED(ifp->sendbuf, ifp->bufsize);
  906. ifp->buffered = 0;
  907. ifp->buffered_hello = -1;
  908. ifp->have_buffered_id = 0;
  909. ifp->have_buffered_nh = 0;
  910. // -- lorauth --
  911. ifp-> have_buffered_cipher = 0;
  912. // ----
  913. ifp->have_buffered_prefix = 0;
  914. ifp->flush_timeout.tv_sec = 0;
  915. ifp->flush_timeout.tv_usec = 0;
  916. }
  917. static void
  918. schedule_flush(struct interface *ifp)
  919. {
  920. unsigned msecs = jitter(ifp, 0);
  921. if(ifp->flush_timeout.tv_sec != 0 &&
  922. timeval_minus_msec(&ifp->flush_timeout, &now) < msecs)
  923. return;
  924. set_timeout(&ifp->flush_timeout, msecs);
  925. }
  926. static void
  927. schedule_flush_now(struct interface *ifp)
  928. {
  929. /* Almost now */
  930. unsigned msecs = roughly(10);
  931. if(ifp->flush_timeout.tv_sec != 0 &&
  932. timeval_minus_msec(&ifp->flush_timeout, &now) < msecs)
  933. return;
  934. set_timeout(&ifp->flush_timeout, msecs);
  935. }
  936. static void
  937. schedule_unicast_flush(unsigned msecs)
  938. {
  939. if(!unicast_neighbour)
  940. return;
  941. if(unicast_flush_timeout.tv_sec != 0 &&
  942. timeval_minus_msec(&unicast_flush_timeout, &now) < msecs)
  943. return;
  944. unicast_flush_timeout.tv_usec = (now.tv_usec + msecs * 1000) % 1000000;
  945. unicast_flush_timeout.tv_sec =
  946. now.tv_sec + (now.tv_usec / 1000 + msecs) / 1000;
  947. }
  948. static void
  949. ensure_space(struct interface *ifp, int space)
  950. {
  951. if(ifp->bufsize - ifp->buffered < space)
  952. flushbuf(ifp);
  953. }
  954. static void
  955. start_message(struct interface *ifp, int type, int len)
  956. {
  957. if(ifp->bufsize - ifp->buffered < len + 2)
  958. flushbuf(ifp);
  959. ifp->sendbuf[ifp->buffered++] = type;
  960. ifp->sendbuf[ifp->buffered++] = len;
  961. }
  962. static void
  963. end_message(struct interface *ifp, int type, int bytes)
  964. {
  965. /*
  966. debugf(" ifp->buffered %d >= bytes+2=%d --> %d\n ifp->sendbuf[ifp->buffered- bytes -2 ]=%d(%d) == type=%d --> %d\n ifp->sendbuf[ifp->buffered - bytes -1]=%d(%d) == bytes --> %d\n",
  967. ifp->buffered,
  968. bytes+2,
  969. ifp->buffered >= bytes + 2,
  970. ifp->buffered - bytes - 2,
  971. ifp->sendbuf[ifp->buffered - bytes - 2],
  972. type,
  973. ifp->sendbuf[ifp->buffered - bytes - 2] == type,
  974. ifp->buffered - bytes - 1 ,
  975. ifp->sendbuf[ifp->buffered - bytes - 1],
  976. ifp->sendbuf[ifp->buffered - bytes - 1] == bytes);
  977. */
  978. // -- lorauth --
  979. /* if(type==MESSAGE_LORAUTH) */
  980. /* debugf(" lorauth case ^ \n"); */
  981. assert(ifp->buffered >= bytes + 2 &&
  982. ifp->sendbuf[ifp->buffered - bytes - 2] == type &&
  983. ifp->sendbuf[ifp->buffered - bytes - 1] == bytes);
  984. // --
  985. schedule_flush(ifp);
  986. }
  987. static void
  988. accumulate_byte(struct interface *ifp, unsigned char value)
  989. {
  990. ifp->sendbuf[ifp->buffered++] = value;
  991. }
  992. static void
  993. accumulate_short(struct interface *ifp, unsigned short value)
  994. {
  995. DO_HTONS(ifp->sendbuf + ifp->buffered, value);
  996. ifp->buffered += 2;
  997. }
  998. static void
  999. accumulate_int(struct interface *ifp, unsigned int value)
  1000. {
  1001. DO_HTONL(ifp->sendbuf + ifp->buffered, value);
  1002. ifp->buffered += 4;
  1003. }
  1004. static void
  1005. accumulate_bytes(struct interface *ifp,
  1006. const unsigned char *value, unsigned len)
  1007. {
  1008. memcpy(ifp->sendbuf + ifp->buffered, value, len);
  1009. ifp->buffered += len;
  1010. }
  1011. static int
  1012. start_unicast_message(struct neighbour *neigh, int type, int len)
  1013. {
  1014. if(unicast_neighbour) {
  1015. if(neigh != unicast_neighbour ||
  1016. unicast_buffered + len + 2 >=
  1017. MIN(UNICAST_BUFSIZE, neigh->ifp->bufsize))
  1018. flush_unicast(0);
  1019. }
  1020. if(!unicast_buffer)
  1021. unicast_buffer = malloc(UNICAST_BUFSIZE);
  1022. if(!unicast_buffer) {
  1023. perror("malloc(unicast_buffer)");
  1024. return -1;
  1025. }
  1026. unicast_neighbour = neigh;
  1027. unicast_buffer[unicast_buffered++] = type;
  1028. unicast_buffer[unicast_buffered++] = len;
  1029. return 1;
  1030. }
  1031. static void
  1032. end_unicast_message(struct neighbour *neigh, int type, int bytes)
  1033. {
  1034. assert(unicast_neighbour == neigh && unicast_buffered >= bytes + 2 &&
  1035. unicast_buffer[unicast_buffered - bytes - 2] == type &&
  1036. unicast_buffer[unicast_buffered - bytes - 1] == bytes);
  1037. schedule_unicast_flush(jitter(neigh->ifp, 0));
  1038. }
  1039. static void
  1040. accumulate_unicast_byte(struct neighbour *neigh, unsigned char value)
  1041. {
  1042. unicast_buffer[unicast_buffered++] = value;
  1043. }
  1044. static void
  1045. accumulate_unicast_short(struct neighbour *neigh, unsigned short value)
  1046. {
  1047. DO_HTONS(unicast_buffer + unicast_buffered, value);
  1048. unicast_buffered += 2;
  1049. }
  1050. static void
  1051. accumulate_unicast_int(struct neighbour *neigh, unsigned int value)
  1052. {
  1053. DO_HTONL(unicast_buffer + unicast_buffered, value);
  1054. unicast_buffered += 4;
  1055. }
  1056. static void
  1057. accumulate_unicast_bytes(struct neighbour *neigh,
  1058. const unsigned char *value, unsigned len)
  1059. {
  1060. memcpy(unicast_buffer + unicast_buffered, value, len);
  1061. unicast_buffered += len;
  1062. }
  1063. void
  1064. send_ack(struct neighbour *neigh, unsigned short nonce, unsigned short interval)
  1065. {
  1066. int rc;
  1067. debugf("Sending ack (%04x) to %s on %s.\n",
  1068. nonce, format_address(neigh->address), neigh->ifp->name);
  1069. rc = start_unicast_message(neigh, MESSAGE_ACK, 2); if(rc < 0) return;
  1070. accumulate_unicast_short(neigh, nonce);
  1071. end_unicast_message(neigh, MESSAGE_ACK, 2);
  1072. /* Roughly yields a value no larger than 3/2, so this meets the deadline */
  1073. schedule_unicast_flush(roughly(interval * 6));
  1074. }
  1075. void
  1076. send_hello_noupdate(struct interface *ifp, unsigned interval)
  1077. {
  1078. /* This avoids sending multiple hellos in a single packet, which breaks
  1079. link quality estimation. */
  1080. if(ifp->buffered_hello >= 0)
  1081. flushbuf(ifp);
  1082. ifp->hello_seqno = seqno_plus(ifp->hello_seqno, 1);
  1083. set_timeout(&ifp->hello_timeout, ifp->hello_interval);
  1084. if(!if_up(ifp))
  1085. return;
  1086. debugf("Sending hello %d (%d) to %s.\n",
  1087. ifp->hello_seqno, interval, ifp->name);
  1088. start_message(ifp, MESSAGE_HELLO, (ifp->flags & IF_TIMESTAMPS) ? 12 : 6);
  1089. ifp->buffered_hello = ifp->buffered - 2;
  1090. accumulate_short(ifp, 0);
  1091. accumulate_short(ifp, ifp->hello_seqno);
  1092. accumulate_short(ifp, interval > 0xFFFF ? 0xFFFF : interval);
  1093. if(ifp->flags & IF_TIMESTAMPS) {
  1094. /* Sub-TLV containing the local time of emission. We use a
  1095. Pad4 sub-TLV, which we'll fill just before sending. */
  1096. accumulate_byte(ifp, SUBTLV_PADN);
  1097. accumulate_byte(ifp, 4);
  1098. accumulate_int(ifp, 0);
  1099. }
  1100. end_message(ifp, MESSAGE_HELLO, (ifp->flags & IF_TIMESTAMPS) ? 12 : 6);
  1101. }
  1102. void
  1103. send_hello(struct interface *ifp)
  1104. {
  1105. send_hello_noupdate(ifp, (ifp->hello_interval + 9) / 10);
  1106. /* Send full IHU every 3 hellos, and marginal IHU each time */
  1107. if(ifp->hello_seqno % 3 == 0)
  1108. send_ihu(NULL, ifp);
  1109. else
  1110. send_marginal_ihu(ifp);
  1111. }
  1112. void
  1113. flush_unicast(int dofree)
  1114. {
  1115. struct sockaddr_in6 sin6;
  1116. int rc;
  1117. if(unicast_buffered == 0)
  1118. goto done;
  1119. if(!if_up(unicast_neighbour->ifp))
  1120. goto done;
  1121. /* Preserve ordering of messages */
  1122. flushbuf(unicast_neighbour->ifp);
  1123. if(check_bucket(unicast_neighbour->ifp)) {
  1124. memset(&sin6, 0, sizeof(sin6));
  1125. sin6.sin6_family = AF_INET6;
  1126. memcpy(&sin6.sin6_addr, unicast_neighbour->address, 16);
  1127. sin6.sin6_port = htons(protocol_port);
  1128. sin6.sin6_scope_id = unicast_neighbour->ifp->ifindex;
  1129. DO_HTONS(packet_header + 2, unicast_buffered);
  1130. fill_rtt_message(unicast_neighbour->ifp);
  1131. rc = babel_send(protocol_socket,
  1132. packet_header, sizeof(packet_header),
  1133. unicast_buffer, unicast_buffered,
  1134. (struct sockaddr*)&sin6, sizeof(sin6));
  1135. if(rc < 0)
  1136. perror("send(unicast)");
  1137. } else {
  1138. fprintf(stderr,
  1139. "Warning: bucket full, dropping unicast packet "
  1140. "to %s if %s.\n",
  1141. format_address(unicast_neighbour->address),
  1142. unicast_neighbour->ifp->name);
  1143. }
  1144. done:
  1145. VALGRIND_MAKE_MEM_UNDEFINED(unicast_buffer, UNICAST_BUFSIZE);
  1146. unicast_buffered = 0;
  1147. if(dofree && unicast_buffer) {
  1148. free(unicast_buffer);
  1149. unicast_buffer = NULL;
  1150. }
  1151. unicast_neighbour = NULL;
  1152. unicast_flush_timeout.tv_sec = 0;
  1153. unicast_flush_timeout.tv_usec = 0;
  1154. }
  1155. static void
  1156. really_send_update(struct interface *ifp,
  1157. const unsigned char *id,
  1158. const unsigned char *prefix, unsigned char plen,
  1159. const unsigned char *src_prefix, unsigned char src_plen,
  1160. // -- lorauth --
  1161. const unsigned char *cipher, unsigned short clen,
  1162. // ----
  1163. unsigned short seqno, unsigned short metric,
  1164. unsigned char *channels, int channels_len)
  1165. {
  1166. int add_metric, v4, real_plen, omit = 0;
  1167. const unsigned char *real_prefix;
  1168. const unsigned char *real_src_prefix = NULL;
  1169. int real_src_plen = 0;
  1170. unsigned short flags = 0;
  1171. int channels_size;
  1172. if(diversity_kind != DIVERSITY_CHANNEL)
  1173. channels_len = -1;
  1174. channels_size = channels_len >= 0 ? channels_len + 2 : 0;
  1175. if(!if_up(ifp))
  1176. return;
  1177. add_metric = output_filter(id, prefix, plen, src_prefix,
  1178. src_plen, ifp->ifindex);
  1179. if(add_metric >= INFINITY)
  1180. return;
  1181. metric = MIN(metric + add_metric, INFINITY);
  1182. /* Worst case */
  1183. ensure_space(ifp, 20 + 12 + 28 + 18);
  1184. v4 = plen >= 96 && v4mapped(prefix);
  1185. if(v4) {
  1186. if(!ifp->ipv4)
  1187. return;
  1188. if(!ifp->have_buffered_nh ||
  1189. memcmp(ifp->buffered_nh, ifp->ipv4, 4) != 0) {
  1190. // bufs NH as the interface's prefix
  1191. start_message(ifp, MESSAGE_NH, 6);
  1192. accumulate_byte(ifp, 1);
  1193. accumulate_byte(ifp, 0);
  1194. accumulate_bytes(ifp, ifp->ipv4, 4);
  1195. end_message(ifp, MESSAGE_NH, 6);
  1196. memcpy(ifp->buffered_nh, ifp->ipv4, 4);
  1197. ifp->have_buffered_nh = 1;
  1198. }
  1199. real_prefix = prefix + 12;
  1200. real_plen = plen - 96;
  1201. if(src_plen != 0 /* it should never be 96 */) {
  1202. real_src_prefix = src_prefix + 12;
  1203. real_src_plen = src_plen - 96;
  1204. }
  1205. } else {
  1206. if(ifp->have_buffered_prefix) {
  1207. while(omit < plen / 8 &&
  1208. ifp->buffered_prefix[omit] == prefix[omit])
  1209. omit++;
  1210. }
  1211. if(src_plen == 0 && (!ifp->have_buffered_prefix || plen >= 48))
  1212. flags |= 0x80;
  1213. real_prefix = prefix;
  1214. real_plen = plen;
  1215. real_src_prefix = src_prefix;
  1216. real_src_plen = src_plen;
  1217. }
  1218. if(!ifp->have_buffered_id || memcmp(id, ifp->buffered_id, 8) != 0) {
  1219. // bufs router_id TLV
  1220. if(src_plen == 0 && real_plen == 128 &&
  1221. memcmp(real_prefix + 8, id, 8) == 0) {
  1222. flags |= 0x40;
  1223. } else {
  1224. start_message(ifp, MESSAGE_ROUTER_ID, 10);
  1225. accumulate_short(ifp, 0);
  1226. accumulate_bytes(ifp, id, 8);
  1227. end_message(ifp, MESSAGE_ROUTER_ID, 10);
  1228. }
  1229. memcpy(ifp->buffered_id, id, 8);
  1230. ifp->have_buffered_id = 1;
  1231. }
  1232. // -- lorauth --
  1233. /* Buffering LORAUTH TLV in case there is cipher text to send*/
  1234. if (clen > 0 && strlen((char *)cipher) > 3)
  1235. {
  1236. /* Spliting in 3 packets (see doc-lorauth/babel-integration.es.md)*/
  1237. // packet 1
  1238. start_message(ifp, MESSAGE_LORAUTH, 255);
  1239. accumulate_byte(ifp, 1);
  1240. accumulate_byte(ifp, 253);
  1241. accumulate_bytes(ifp, cipher, 253);
  1242. //debugf(" sendbuf size: %d\n", ifp->buffered);
  1243. end_message(ifp, MESSAGE_LORAUTH, 255);
  1244. ifp->have_buffered_cipher = 1;
  1245. //packet 2
  1246. start_message(ifp, MESSAGE_LORAUTH, 255);
  1247. accumulate_byte(ifp, 2);
  1248. accumulate_byte(ifp, 253);
  1249. accumulate_bytes(ifp, cipher+253, 253);
  1250. //debugf(" sendbuf size: %d\n", ifp->buffered);
  1251. end_message(ifp, MESSAGE_LORAUTH, 255);
  1252. ifp->have_buffered_cipher = 1;
  1253. //packet 3
  1254. start_message(ifp, MESSAGE_LORAUTH, 8);
  1255. accumulate_byte(ifp, 3);
  1256. accumulate_byte(ifp, 6);
  1257. accumulate_bytes(ifp, cipher+506, 6);
  1258. //debugf(" sendbuf size: %d\n", ifp->buffered);
  1259. end_message(ifp, MESSAGE_LORAUTH, 8);
  1260. debugf(" --Done sent lorauth TLV.\n");
  1261. ifp->have_buffered_cipher = 1;
  1262. }
  1263. // ----
  1264. // buffering UPDATE TLV
  1265. if(src_plen == 0)
  1266. start_message(ifp, MESSAGE_UPDATE, 10 + (real_plen + 7) / 8 - omit +
  1267. channels_size);
  1268. else
  1269. start_message(ifp, MESSAGE_UPDATE_SRC_SPECIFIC,
  1270. 10 + (real_plen + 7) / 8 - omit +
  1271. (real_src_plen + 7) / 8 + channels_size);
  1272. accumulate_byte(ifp, v4 ? 1 : 2);
  1273. if(src_plen != 0)
  1274. accumulate_byte(ifp, real_src_plen);
  1275. else
  1276. accumulate_byte(ifp, flags);
  1277. accumulate_byte(ifp, real_plen);
  1278. accumulate_byte(ifp, omit);
  1279. accumulate_short(ifp, (ifp->update_interval + 5) / 10);
  1280. accumulate_short(ifp, seqno);
  1281. accumulate_short(ifp, metric);
  1282. accumulate_bytes(ifp, real_prefix + omit, (real_plen + 7) / 8 - omit);
  1283. if(src_plen != 0)
  1284. accumulate_bytes(ifp, real_src_prefix, (real_src_plen + 7) / 8);
  1285. /* Note that an empty channels TLV is different from no such TLV. */
  1286. if(channels_len >= 0) {
  1287. accumulate_byte(ifp, 2);
  1288. accumulate_byte(ifp, channels_len);
  1289. accumulate_bytes(ifp, channels, channels_len);
  1290. }
  1291. if(src_plen == 0)
  1292. end_message(ifp, MESSAGE_UPDATE, 10 + (real_plen + 7) / 8 - omit +
  1293. channels_size);
  1294. else
  1295. end_message(ifp, MESSAGE_UPDATE_SRC_SPECIFIC,
  1296. 10 + (real_plen + 7) / 8 - omit +
  1297. (real_src_plen + 7) / 8 + channels_size);
  1298. if(flags & 0x80) {
  1299. memcpy(ifp->buffered_prefix, prefix, 16);
  1300. ifp->have_buffered_prefix = 1;
  1301. }
  1302. }
  1303. static int
  1304. compare_buffered_updates(const void *av, const void *bv)
  1305. {
  1306. const struct buffered_update *a = av, *b = bv;
  1307. int rc, v4a, v4b, ma, mb;
  1308. rc = memcmp(a->id, b->id, 8);
  1309. if(rc != 0)
  1310. return rc;
  1311. v4a = (a->plen >= 96 && v4mapped(a->prefix));
  1312. v4b = (b->plen >= 96 && v4mapped(b->prefix));
  1313. if(v4a > v4b)
  1314. return 1;
  1315. else if(v4a < v4b)
  1316. return -1;
  1317. ma = (!v4a && a->plen == 128 && memcmp(a->prefix + 8, a->id, 8) == 0);
  1318. mb = (!v4b && b->plen == 128 && memcmp(b->prefix + 8, b->id, 8) == 0);
  1319. if(ma > mb)
  1320. return -1;
  1321. else if(mb > ma)
  1322. return 1;
  1323. if(a->plen < b->plen)
  1324. return 1;
  1325. else if(a->plen > b->plen)
  1326. return -1;
  1327. rc = memcmp(a->prefix, b->prefix, 16);
  1328. if(rc != 0)
  1329. return rc;
  1330. if(a->src_plen < b->src_plen)
  1331. return -1;
  1332. else if(a->src_plen > b->src_plen)
  1333. return 1;
  1334. return memcmp(a->src_prefix, b->src_prefix, 16);
  1335. }
  1336. void
  1337. flushupdates(struct interface *ifp)
  1338. {
  1339. struct xroute *xroute;
  1340. struct babel_route *route;
  1341. const unsigned char *last_prefix = NULL;
  1342. const unsigned char *last_src_prefix = NULL;
  1343. unsigned char last_plen = 0xFF;
  1344. unsigned char last_src_plen = 0xFF;
  1345. int i;
  1346. if(ifp == NULL) {
  1347. struct interface *ifp_aux;
  1348. FOR_ALL_INTERFACES(ifp_aux)
  1349. flushupdates(ifp_aux);
  1350. return;
  1351. }
  1352. if(ifp->num_buffered_updates > 0) {
  1353. struct buffered_update *b = ifp->buffered_updates;
  1354. int n = ifp->num_buffered_updates;
  1355. ifp->buffered_updates = NULL;
  1356. ifp->update_bufsize = 0;
  1357. ifp->num_buffered_updates = 0;
  1358. if(!if_up(ifp))
  1359. goto done;
  1360. debugf(" (flushing %d buffered updates on %s (%d))\n",
  1361. n, ifp->name, ifp->ifindex);
  1362. /* In order to send fewer update messages, we want to send updates
  1363. with the same router-id together, with IPv6 going out before IPv4. */
  1364. for(i = 0; i < n; i++) {
  1365. route = find_installed_route(b[i].prefix, b[i].plen,
  1366. b[i].src_prefix, b[i].src_plen);
  1367. if(route)
  1368. memcpy(b[i].id, route->src->id, 8);
  1369. else
  1370. memcpy(b[i].id, myid, 8);
  1371. }
  1372. qsort(b, n, sizeof(struct buffered_update), compare_buffered_updates);
  1373. for(i = 0; i < n; i++) {
  1374. /* The same update may be scheduled multiple times before it is
  1375. sent out. Since our buffer is now sorted, it is enough to
  1376. compare with the previous update. */
  1377. if(last_prefix &&
  1378. b[i].plen == last_plen &&
  1379. b[i].src_plen == last_src_plen &&
  1380. memcmp(b[i].prefix, last_prefix, 16) == 0 &&
  1381. memcmp(b[i].src_prefix, last_src_prefix, 16) == 0)
  1382. continue;
  1383. //debugf("--b[%d], prefix %s, src_prefix %s\n", i, b[i].prefix, b[i].src_prefix);
  1384. xroute = find_xroute(b[i].prefix, b[i].plen,
  1385. b[i].src_prefix, b[i].src_plen);
  1386. route = find_installed_route(b[i].prefix, b[i].plen,
  1387. b[i].src_prefix, b[i].src_plen);
  1388. //debugf("--route, prefix %s, src_prefix %s\n", route->src->prefix, route->src->src_prefix);
  1389. if(xroute && (!route || xroute->metric <= kernel_metric)) {
  1390. really_send_update(ifp, myid,
  1391. xroute->prefix, xroute->plen,
  1392. xroute->src_prefix, xroute->src_plen,
  1393. // -- lorauth --
  1394. xroute->cipher, xroute->clen,
  1395. // ----
  1396. myseqno, xroute->metric,
  1397. NULL, 0);
  1398. last_prefix = xroute->prefix;
  1399. last_plen = xroute->plen;
  1400. last_src_prefix = xroute->src_prefix;
  1401. last_src_plen = xroute->src_plen;
  1402. } else if(route) {
  1403. unsigned char channels[MAX_CHANNEL_HOPS];
  1404. int chlen;
  1405. struct interface *route_ifp = route->neigh->ifp;
  1406. unsigned short metric;
  1407. unsigned short seqno;
  1408. seqno = route->seqno;
  1409. metric =
  1410. route_interferes(route, ifp) ?
  1411. route_metric(route) :
  1412. route_metric_noninterfering(route);
  1413. if(metric < INFINITY)
  1414. satisfy_request(route->src->prefix, route->src->plen,
  1415. route->src->src_prefix,
  1416. route->src->src_plen,
  1417. seqno, route->src->id, ifp);
  1418. if((ifp->flags & IF_SPLIT_HORIZON) &&
  1419. route->neigh->ifp == ifp)
  1420. continue;
  1421. if(route_ifp->channel == IF_CHANNEL_NONINTERFERING) {
  1422. memcpy(channels, route->channels,
  1423. MIN(route->channels_len, MAX_CHANNEL_HOPS));
  1424. chlen = MIN(route->channels_len, MAX_CHANNEL_HOPS);
  1425. } else {
  1426. if(route_ifp->channel == IF_CHANNEL_UNKNOWN)
  1427. channels[0] = IF_CHANNEL_INTERFERING;
  1428. else {
  1429. assert(route_ifp->channel > 0 &&
  1430. route_ifp->channel <= 255);
  1431. channels[0] = route_ifp->channel;
  1432. }
  1433. memcpy(channels + 1, route->channels,
  1434. MIN(route->channels_len, MAX_CHANNEL_HOPS - 1));
  1435. chlen = 1 + MIN(route->channels_len, MAX_CHANNEL_HOPS - 1);
  1436. }
  1437. //lorauth note: Check if the following is correct
  1438. really_send_update(ifp, route->src->id,
  1439. route->src->prefix, route->src->plen,
  1440. route->src->src_prefix, route->src->src_plen,
  1441. // -- lorauth --
  1442. //xroute->cipher, xroute->clen,
  1443. NULL, 0, //??
  1444. // ----
  1445. seqno, metric,
  1446. channels, chlen);
  1447. update_source(route->src, seqno, metric);
  1448. last_prefix = route->src->prefix;
  1449. last_plen = route->src->plen;
  1450. last_src_prefix = route->src->src_prefix;
  1451. last_src_plen = route->src->src_plen;
  1452. } else {
  1453. /* There's no route for this prefix. This can happen shortly
  1454. after an xroute has been retracted, so send a retraction. */
  1455. really_send_update(ifp, myid, b[i].prefix, b[i].plen,
  1456. b[i].src_prefix, b[i].src_plen,
  1457. // -- lorauth --
  1458. NULL, 0,
  1459. // ----
  1460. myseqno, INFINITY, NULL, -1);
  1461. }
  1462. }
  1463. schedule_flush_now(ifp);
  1464. done:
  1465. free(b);
  1466. }
  1467. ifp->update_flush_timeout.tv_sec = 0;
  1468. ifp->update_flush_timeout.tv_usec = 0;
  1469. }
  1470. static void
  1471. schedule_update_flush(struct interface *ifp, int urgent)
  1472. {
  1473. unsigned msecs;
  1474. msecs = update_jitter(ifp, urgent);
  1475. if(ifp->update_flush_timeout.tv_sec != 0 &&
  1476. timeval_minus_msec(&ifp->update_flush_timeout, &now) < msecs)
  1477. return;
  1478. set_timeout(&ifp->update_flush_timeout, msecs);
  1479. }
  1480. static void
  1481. buffer_update(struct interface *ifp,
  1482. const unsigned char *prefix, unsigned char plen,
  1483. const unsigned char *src_prefix, unsigned char src_plen,
  1484. // -- lorauth --
  1485. const unsigned char *cipher, unsigned short clen)
  1486. {
  1487. if(ifp->num_buffered_updates > 0 &&
  1488. ifp->num_buffered_updates >= ifp->update_bufsize)
  1489. flushupdates(ifp);
  1490. if(ifp->update_bufsize == 0) {
  1491. int n;
  1492. assert(ifp->buffered_updates == NULL);
  1493. /* Allocate enough space to hold a full update. Since the
  1494. number of installed routes will grow over time, make sure we
  1495. have enough space to send a full-ish frame. */
  1496. n = installed_routes_estimate() + xroutes_estimate() + 4;
  1497. n = MAX(n, ifp->bufsize / 16);
  1498. again:
  1499. ifp->buffered_updates = malloc(n * sizeof(struct buffered_update));
  1500. if(ifp->buffered_updates == NULL) {
  1501. perror("malloc(buffered_updates)");
  1502. if(n > 4) {
  1503. /* Try again with a tiny buffer. */
  1504. n = 4;
  1505. goto again;
  1506. }
  1507. return;
  1508. }
  1509. ifp->update_bufsize = n;
  1510. ifp->num_buffered_updates = 0;
  1511. }
  1512. memcpy(ifp->buffered_updates[ifp->num_buffered_updates].prefix,
  1513. prefix, 16);
  1514. ifp->buffered_updates[ifp->num_buffered_updates].plen = plen;
  1515. memcpy(ifp->buffered_updates[ifp->num_buffered_updates].src_prefix,
  1516. src_prefix, 16);
  1517. ifp->buffered_updates[ifp->num_buffered_updates].src_plen = src_plen;
  1518. // -- lorauth --
  1519. if(clen > 0) {
  1520. ifp->buffered_updates[ifp->num_buffered_updates].clen = clen;
  1521. memcpy(ifp->buffered_updates[ifp->num_buffered_updates].cipher,
  1522. cipher, clen);
  1523. ifp->have_buffered_cipher = 1;
  1524. debugf(" done buffering clen, cipher (%d bytes)\n", clen);
  1525. }
  1526. else{
  1527. debugf(" not buffering cipher token\n");
  1528. }
  1529. // ----
  1530. ifp->num_buffered_updates++;
  1531. }
  1532. /* Full wildcard update with prefix == src_prefix == NULL,
  1533. Standard wildcard update with prefix == NULL && src_prefix != NULL,
  1534. Specific wildcard update with prefix != NULL && src_prefix == NULL. */
  1535. void
  1536. send_update(struct interface *ifp, int urgent,
  1537. const unsigned char *prefix, unsigned char plen,
  1538. const unsigned char *src_prefix, unsigned char src_plen,
  1539. // -- lorauth --
  1540. const unsigned char *cipher, unsigned short clen
  1541. // ----
  1542. )
  1543. {
  1544. if(ifp == NULL) {
  1545. struct interface *ifp_aux;
  1546. struct babel_route *route;
  1547. FOR_ALL_INTERFACES(ifp_aux)
  1548. send_update(ifp_aux, urgent, prefix, plen, src_prefix, src_plen,
  1549. cipher, clen);
  1550. if(prefix) {
  1551. /* Since flushupdates only deals with non-wildcard interfaces, we
  1552. need to do this now. */
  1553. route = find_installed_route(prefix, plen, src_prefix, src_plen);
  1554. if(route && route_metric(route) < INFINITY)
  1555. satisfy_request(prefix, plen, src_prefix, src_plen,
  1556. route->src->seqno, route->src->id, NULL);
  1557. }
  1558. return;
  1559. }
  1560. if(!if_up(ifp))
  1561. return;
  1562. if(prefix && src_prefix) {
  1563. debugf("Sending update to %s for %s from %s \ncipher: %s\n",
  1564. ifp->name, format_prefix(prefix, plen),
  1565. format_prefix(src_prefix, src_plen),
  1566. // -- lorauth --
  1567. strlen((char *)cipher)<4 ? (char *)"--":
  1568. (char *)reduced_lorauth_token(cipher));
  1569. buffer_update(ifp, prefix, plen, src_prefix, src_plen,
  1570. // -- lorauth --
  1571. cipher, clen
  1572. // ----
  1573. );
  1574. } else if(prefix || src_prefix) {
  1575. struct route_stream *routes;
  1576. send_self_update(ifp);
  1577. debugf("Sending update to %s for any.\n", ifp->name);
  1578. routes = route_stream(ROUTE_INSTALLED);
  1579. if(routes) {
  1580. while(1) {
  1581. struct babel_route *route = route_stream_next(routes);
  1582. if(route == NULL)
  1583. break;
  1584. if((src_prefix && route->src->src_plen != 0) ||
  1585. (prefix && route->src->src_plen == 0))
  1586. continue;
  1587. buffer_update(ifp, route->src->prefix, route->src->plen,
  1588. route->src->src_prefix, route->src->src_plen,
  1589. // -- lorauth --
  1590. route->src->cipher,
  1591. route->src->clen
  1592. // ----
  1593. );
  1594. }
  1595. route_stream_done(routes);
  1596. } else {
  1597. fprintf(stderr, "Couldn't allocate route stream.\n");
  1598. }
  1599. set_timeout(&ifp->update_timeout, ifp->update_interval);
  1600. if(!prefix)
  1601. ifp->last_update_time = now.tv_sec;
  1602. else
  1603. ifp->last_specific_update_time = now.tv_sec;
  1604. } else {
  1605. send_update(ifp, urgent, NULL, 0, zeroes, 0,
  1606. cipher, clen); // standard_wildcard
  1607. send_update(ifp, urgent, zeroes, 0, NULL, 0,
  1608. cipher, clen); // specific wildcard
  1609. }
  1610. schedule_update_flush(ifp, urgent);
  1611. }
  1612. void
  1613. send_update_resend(struct interface *ifp,
  1614. const unsigned char *prefix, unsigned char plen,
  1615. const unsigned char *src_prefix, unsigned char src_plen,
  1616. const unsigned char *cipher, unsigned short clen)
  1617. {
  1618. assert(prefix != NULL);
  1619. send_update(ifp, 1, prefix, plen, src_prefix, src_plen,
  1620. cipher, clen);
  1621. record_resend(RESEND_UPDATE, prefix, plen, src_prefix, src_plen,
  1622. // -- lorauth --
  1623. cipher, clen,
  1624. // ----
  1625. 0, NULL, NULL, resend_delay);
  1626. }
  1627. void
  1628. send_wildcard_retraction(struct interface *ifp)
  1629. {
  1630. if(ifp == NULL) {
  1631. struct interface *ifp_aux;
  1632. FOR_ALL_INTERFACES(ifp_aux)
  1633. send_wildcard_retraction(ifp_aux);
  1634. return;
  1635. }
  1636. if(!if_up(ifp))
  1637. return;
  1638. start_message(ifp, MESSAGE_UPDATE, 10);
  1639. accumulate_byte(ifp, 0);
  1640. accumulate_byte(ifp, 0);
  1641. accumulate_byte(ifp, 0);
  1642. accumulate_byte(ifp, 0);
  1643. accumulate_short(ifp, 0xFFFF);
  1644. accumulate_short(ifp, myseqno);
  1645. accumulate_short(ifp, 0xFFFF);
  1646. end_message(ifp, MESSAGE_UPDATE, 10);
  1647. ifp->have_buffered_id = 0;
  1648. }
  1649. void
  1650. update_myseqno()
  1651. {
  1652. myseqno = seqno_plus(myseqno, 1);
  1653. seqno_time = now;
  1654. }
  1655. void
  1656. send_self_update(struct interface *ifp)
  1657. {
  1658. struct xroute_stream *xroutes;
  1659. if(ifp == NULL) {
  1660. struct interface *ifp_aux;
  1661. FOR_ALL_INTERFACES(ifp_aux) {
  1662. if(!if_up(ifp_aux))
  1663. continue;
  1664. send_self_update(ifp_aux);
  1665. }
  1666. return;
  1667. }
  1668. debugf("Sending self update to %s.\n", ifp->name);
  1669. xroutes = xroute_stream();
  1670. if(xroutes) {
  1671. while(1) {
  1672. struct xroute *xroute = xroute_stream_next(xroutes);
  1673. if(xroute == NULL) break;
  1674. send_update(ifp, 0, xroute->prefix, xroute->plen,
  1675. xroute->src_prefix, xroute->src_plen,
  1676. // -- lorauth --
  1677. xroute->cipher, xroute->clen);
  1678. }
  1679. xroute_stream_done(xroutes);
  1680. } else {
  1681. fprintf(stderr, "Couldn't allocate xroute stream.\n");
  1682. }
  1683. }
  1684. void
  1685. send_ihu(struct neighbour *neigh, struct interface *ifp)
  1686. {
  1687. int rxcost, interval;
  1688. int ll;
  1689. int send_rtt_data;
  1690. int msglen;
  1691. if(neigh == NULL && ifp == NULL) {
  1692. struct interface *ifp_aux;
  1693. FOR_ALL_INTERFACES(ifp_aux) {
  1694. if(if_up(ifp_aux))
  1695. send_ihu(NULL, ifp_aux);
  1696. }
  1697. return;
  1698. }
  1699. if(neigh == NULL) {
  1700. struct neighbour *ngh;
  1701. FOR_ALL_NEIGHBOURS(ngh) {
  1702. if(ngh->ifp == ifp)
  1703. send_ihu(ngh, ifp);
  1704. }
  1705. return;
  1706. }
  1707. if(ifp && neigh->ifp != ifp)
  1708. return;
  1709. ifp = neigh->ifp;
  1710. if(!if_up(ifp))
  1711. return;
  1712. rxcost = neighbour_rxcost(neigh);
  1713. interval = (ifp->hello_interval * 3 + 9) / 10;
  1714. /* Conceptually, an IHU is a unicast message. We usually send them as
  1715. multicast, since this allows aggregation into a single packet and
  1716. avoids an ARP exchange. If we already have a unicast message queued
  1717. for this neighbour, however, we might as well piggyback the IHU. */
  1718. debugf("Sending %sihu %d on %s to %s.\n",
  1719. unicast_neighbour == neigh ? "unicast " : "",
  1720. rxcost,
  1721. neigh->ifp->name,
  1722. format_address(neigh->address));
  1723. ll = linklocal(neigh->address);
  1724. if((ifp->flags & IF_TIMESTAMPS) && neigh->hello_send_us &&
  1725. /* Checks whether the RTT data is not too old to be sent. */
  1726. timeval_minus_msec(&now, &neigh->hello_rtt_receive_time) < 1000000) {
  1727. send_rtt_data = 1;
  1728. } else {
  1729. neigh->hello_send_us = 0;
  1730. send_rtt_data = 0;
  1731. }
  1732. /* The length depends on the format of the address, and then an
  1733. optional 10-bytes sub-TLV for timestamps (used to compute a RTT). */
  1734. msglen = (ll ? 14 : 22) + (send_rtt_data ? 10 : 0);
  1735. if(unicast_neighbour != neigh) {
  1736. start_message(ifp, MESSAGE_IHU, msglen);
  1737. accumulate_byte(ifp, ll ? 3 : 2); // AE
  1738. accumulate_byte(ifp, 0); // Reserved
  1739. accumulate_short(ifp, rxcost); // Rxcost
  1740. accumulate_short(ifp, interval); // Interval
  1741. if(ll)
  1742. accumulate_bytes(ifp, neigh->address + 8, 8);
  1743. else
  1744. accumulate_bytes(ifp, neigh->address, 16);
  1745. if(send_rtt_data) {
  1746. accumulate_byte(ifp, SUBTLV_TIMESTAMP);
  1747. accumulate_byte(ifp, 8);
  1748. accumulate_int(ifp, neigh->hello_send_us);
  1749. accumulate_int(ifp, time_us(neigh->hello_rtt_receive_time));
  1750. }
  1751. end_message(ifp, MESSAGE_IHU, msglen);
  1752. } else {
  1753. int rc;
  1754. rc = start_unicast_message(neigh, MESSAGE_IHU, msglen);
  1755. if(rc < 0) return;
  1756. accumulate_unicast_byte(neigh, ll ? 3 : 2);
  1757. accumulate_unicast_byte(neigh, 0);
  1758. accumulate_unicast_short(neigh, rxcost);
  1759. accumulate_unicast_short(neigh, interval);
  1760. if(ll)
  1761. accumulate_unicast_bytes(neigh, neigh->address + 8, 8);
  1762. else
  1763. accumulate_unicast_bytes(neigh, neigh->address, 16);
  1764. if(send_rtt_data) {
  1765. accumulate_unicast_byte(neigh, SUBTLV_TIMESTAMP);
  1766. accumulate_unicast_byte(neigh, 8);
  1767. accumulate_unicast_int(neigh, neigh->hello_send_us);
  1768. accumulate_unicast_int(neigh,
  1769. time_us(neigh->hello_rtt_receive_time));
  1770. }
  1771. end_unicast_message(neigh, MESSAGE_IHU, msglen);
  1772. }
  1773. }
  1774. /* Send IHUs to all marginal neighbours */
  1775. void
  1776. send_marginal_ihu(struct interface *ifp)
  1777. {
  1778. struct neighbour *neigh;
  1779. FOR_ALL_NEIGHBOURS(neigh) {
  1780. if(ifp && neigh->ifp != ifp)
  1781. continue;
  1782. if(neigh->txcost >= 384 || (neigh->reach & 0xF000) != 0xF000)
  1783. send_ihu(neigh, ifp);
  1784. }
  1785. }
  1786. /* Standard wildcard request with prefix == NULL && src_prefix == zeroes,
  1787. Specific wildcard request with prefix == zeroes && src_prefix == NULL. */
  1788. void
  1789. send_request(struct interface *ifp,
  1790. const unsigned char *prefix, unsigned char plen,
  1791. const unsigned char *src_prefix, unsigned char src_plen)
  1792. {
  1793. int v4, pb, spb, len;
  1794. if(ifp == NULL) {
  1795. struct interface *ifp_auxn;
  1796. FOR_ALL_INTERFACES(ifp_auxn) {
  1797. if(if_up(ifp_auxn))
  1798. continue;
  1799. send_request(ifp_auxn, prefix, plen, src_prefix, src_plen);
  1800. }
  1801. return;
  1802. }
  1803. /* make sure any buffered updates go out before this request. */
  1804. flushupdates(ifp);
  1805. if(!if_up(ifp))
  1806. return;
  1807. if(prefix && src_prefix) {
  1808. debugf("sending request to %s for %s from %s.\n", ifp->name,
  1809. format_prefix(prefix, plen),
  1810. format_prefix(src_prefix, src_plen));
  1811. } else if(prefix) {
  1812. debugf("sending request to %s for any specific.\n", ifp->name);
  1813. start_message(ifp, MESSAGE_REQUEST_SRC_SPECIFIC, 3);
  1814. accumulate_byte(ifp, 0);
  1815. accumulate_byte(ifp, 0);
  1816. accumulate_byte(ifp, 0);
  1817. end_message(ifp, MESSAGE_REQUEST_SRC_SPECIFIC, 3);
  1818. return;
  1819. } else if(src_prefix) {
  1820. debugf("sending request to %s for any.\n", ifp->name);
  1821. start_message(ifp, MESSAGE_REQUEST, 2);
  1822. accumulate_byte(ifp, 0);
  1823. accumulate_byte(ifp, 0);
  1824. end_message(ifp, MESSAGE_REQUEST, 2);
  1825. return;
  1826. } else {
  1827. send_request(ifp, NULL, 0, zeroes, 0);
  1828. send_request(ifp, zeroes, 0, NULL, 0);
  1829. return;
  1830. }
  1831. v4 = plen >= 96 && v4mapped(prefix);
  1832. pb = v4 ? ((plen - 96) + 7) / 8 : (plen + 7) / 8;
  1833. len = 2 + pb;
  1834. if(src_plen != 0) {
  1835. spb = v4 ? ((src_plen - 96) + 7) / 8 : (src_plen + 7) / 8;
  1836. len += spb + 1;
  1837. start_message(ifp, MESSAGE_REQUEST_SRC_SPECIFIC, len);
  1838. } else {
  1839. spb = 0;
  1840. start_message(ifp, MESSAGE_REQUEST, len);
  1841. }
  1842. accumulate_byte(ifp, v4 ? 1 : 2);
  1843. accumulate_byte(ifp, v4 ? plen - 96 : plen);
  1844. if(src_plen != 0)
  1845. accumulate_byte(ifp, v4 ? src_plen - 96 : src_plen);
  1846. if(v4)
  1847. accumulate_bytes(ifp, prefix + 12, pb);
  1848. else
  1849. accumulate_bytes(ifp, prefix, pb);
  1850. if(src_plen != 0) {
  1851. if(v4)
  1852. accumulate_bytes(ifp, src_prefix + 12, spb);
  1853. else
  1854. accumulate_bytes(ifp, src_prefix, spb);
  1855. end_message(ifp, MESSAGE_REQUEST_SRC_SPECIFIC, len);
  1856. } else {
  1857. end_message(ifp, MESSAGE_REQUEST, len);
  1858. }
  1859. }
  1860. void
  1861. send_unicast_request(struct neighbour *neigh,
  1862. const unsigned char *prefix, unsigned char plen,
  1863. const unsigned char *src_prefix, unsigned char src_plen)
  1864. {
  1865. int rc, v4, pb, spb, len;
  1866. /* make sure any buffered updates go out before this request. */
  1867. flushupdates(neigh->ifp);
  1868. if(prefix && src_prefix) {
  1869. debugf("sending unicast request to %s for %s from %s.\n",
  1870. format_address(neigh->address),
  1871. format_prefix(prefix, plen),
  1872. format_prefix(src_prefix, src_plen));
  1873. } else if(prefix) {
  1874. debugf("sending unicast request to %s for any specific.\n",
  1875. format_address(neigh->address));
  1876. rc = start_unicast_message(neigh, MESSAGE_REQUEST_SRC_SPECIFIC, 3);
  1877. if(rc < 0) return;
  1878. accumulate_unicast_byte(neigh, 0);
  1879. accumulate_unicast_byte(neigh, 0);
  1880. accumulate_unicast_byte(neigh, 0);
  1881. end_unicast_message(neigh, MESSAGE_REQUEST_SRC_SPECIFIC, 3);
  1882. return;
  1883. } else if(src_prefix) {
  1884. debugf("sending unicast request to %s for any.\n",
  1885. format_address(neigh->address));
  1886. rc = start_unicast_message(neigh, MESSAGE_REQUEST, 2);
  1887. if(rc < 0) return;
  1888. accumulate_unicast_byte(neigh, 0);
  1889. accumulate_unicast_byte(neigh, 0);
  1890. end_unicast_message(neigh, MESSAGE_REQUEST, 2);
  1891. return;
  1892. } else {
  1893. send_unicast_request(neigh, NULL, 0, zeroes, 0);
  1894. send_unicast_request(neigh, zeroes, 0, NULL, 0);
  1895. return;
  1896. }
  1897. v4 = plen >= 96 && v4mapped(prefix);
  1898. pb = v4 ? ((plen - 96) + 7) / 8 : (plen + 7) / 8;
  1899. len = 2 + pb;
  1900. if(src_plen != 0) {
  1901. spb = v4 ? ((src_plen - 96) + 7) / 8 : (src_plen + 7) / 8;
  1902. len += spb + 1;
  1903. rc = start_unicast_message(neigh, MESSAGE_REQUEST_SRC_SPECIFIC, len);
  1904. } else {
  1905. spb = 0;
  1906. rc = start_unicast_message(neigh, MESSAGE_REQUEST, len);
  1907. }
  1908. if(rc < 0) return;
  1909. accumulate_unicast_byte(neigh, v4 ? 1 : 2);
  1910. accumulate_unicast_byte(neigh, v4 ? plen - 96 : plen);
  1911. if(src_plen != 0)
  1912. accumulate_unicast_byte(neigh, v4 ? src_plen - 96 : src_plen);
  1913. if(v4)
  1914. accumulate_unicast_bytes(neigh, prefix + 12, pb);
  1915. else
  1916. accumulate_unicast_bytes(neigh, prefix, pb);
  1917. if(src_plen != 0) {
  1918. if(v4)
  1919. accumulate_unicast_bytes(neigh, src_prefix + 12, spb);
  1920. else
  1921. accumulate_unicast_bytes(neigh, src_prefix, spb);
  1922. end_unicast_message(neigh, MESSAGE_REQUEST_SRC_SPECIFIC, len);
  1923. } else {
  1924. end_unicast_message(neigh, MESSAGE_REQUEST, len);
  1925. }
  1926. }
  1927. void
  1928. send_multihop_request(struct interface *ifp,
  1929. const unsigned char *prefix, unsigned char plen,
  1930. const unsigned char *src_prefix, unsigned char src_plen,
  1931. unsigned short seqno, const unsigned char *id,
  1932. unsigned short hop_count)
  1933. {
  1934. int v4, pb, spb, len;
  1935. /* Make sure any buffered updates go out before this request. */
  1936. flushupdates(ifp);
  1937. if(ifp == NULL) {
  1938. struct interface *ifp_aux;
  1939. FOR_ALL_INTERFACES(ifp_aux) {
  1940. if(!if_up(ifp_aux))
  1941. continue;
  1942. send_multihop_request(ifp_aux, prefix, plen, src_prefix, src_plen,
  1943. seqno, id, hop_count);
  1944. }
  1945. return;
  1946. }
  1947. if(!if_up(ifp))
  1948. return;
  1949. debugf("Sending request (%d) on %s for %s from %s.\n",
  1950. hop_count, ifp->name, format_prefix(prefix, plen),
  1951. format_prefix(src_prefix, src_plen));
  1952. v4 = plen >= 96 && v4mapped(prefix);
  1953. pb = v4 ? ((plen - 96) + 7) / 8 : (plen + 7) / 8;
  1954. len = 6 + 8 + pb;
  1955. if(src_plen != 0) {
  1956. spb = v4 ? ((src_plen - 96) + 7) / 8 : (src_plen + 7) / 8;
  1957. len += spb;
  1958. start_message(ifp, MESSAGE_MH_REQUEST_SRC_SPECIFIC, len);
  1959. } else {
  1960. spb = 0;
  1961. start_message(ifp, MESSAGE_MH_REQUEST, len);
  1962. }
  1963. accumulate_byte(ifp, v4 ? 1 : 2);
  1964. accumulate_byte(ifp, v4 ? plen - 96 : plen);
  1965. accumulate_short(ifp, seqno);
  1966. accumulate_byte(ifp, hop_count);
  1967. accumulate_byte(ifp, v4 ? src_plen - 96 : src_plen);
  1968. accumulate_bytes(ifp, id, 8);
  1969. if(prefix) {
  1970. if(v4)
  1971. accumulate_bytes(ifp, prefix + 12, pb);
  1972. else
  1973. accumulate_bytes(ifp, prefix, pb);
  1974. }
  1975. if(src_plen != 0) {
  1976. if(v4)
  1977. accumulate_bytes(ifp, src_prefix + 12, spb);
  1978. else
  1979. accumulate_bytes(ifp, src_prefix, spb);
  1980. end_message(ifp, MESSAGE_MH_REQUEST_SRC_SPECIFIC, len);
  1981. } else {
  1982. end_message(ifp, MESSAGE_MH_REQUEST, len);
  1983. }
  1984. }
  1985. void
  1986. send_unicast_multihop_request(struct neighbour *neigh,
  1987. const unsigned char *prefix, unsigned char plen,
  1988. const unsigned char *src_prefix,
  1989. unsigned char src_plen,
  1990. unsigned short seqno, const unsigned char *id,
  1991. unsigned short hop_count)
  1992. {
  1993. int rc, v4, pb, spb, len;
  1994. /* Make sure any buffered updates go out before this request. */
  1995. flushupdates(neigh->ifp);
  1996. debugf("Sending multi-hop request to %s for %s from %s (%d hops).\n",
  1997. format_address(neigh->address),
  1998. format_prefix(prefix, plen),
  1999. format_prefix(src_prefix, src_plen), hop_count);
  2000. v4 = plen >= 96 && v4mapped(prefix);
  2001. pb = v4 ? ((plen - 96) + 7) / 8 : (plen + 7) / 8;
  2002. len = 6 + 8 + pb;
  2003. if(src_plen != 0) {
  2004. spb = v4 ? ((src_plen - 96) + 7) / 8 : (src_plen + 7) / 8;
  2005. len += spb;
  2006. rc = start_unicast_message(neigh, MESSAGE_MH_REQUEST_SRC_SPECIFIC, len);
  2007. } else {
  2008. spb = 0;
  2009. rc = start_unicast_message(neigh, MESSAGE_MH_REQUEST, len);
  2010. }
  2011. if(rc < 0) return;
  2012. accumulate_unicast_byte(neigh, v4 ? 1 : 2);
  2013. accumulate_unicast_byte(neigh, v4 ? plen - 96 : plen);
  2014. accumulate_unicast_short(neigh, seqno);
  2015. accumulate_unicast_byte(neigh, hop_count);
  2016. accumulate_unicast_byte(neigh, v4 ? src_plen - 96 : src_plen);
  2017. accumulate_unicast_bytes(neigh, id, 8);
  2018. if(prefix) {
  2019. if(v4)
  2020. accumulate_unicast_bytes(neigh, prefix + 12, pb);
  2021. else
  2022. accumulate_unicast_bytes(neigh, prefix, pb);
  2023. }
  2024. if(src_plen != 0) {
  2025. if(v4)
  2026. accumulate_unicast_bytes(neigh, src_prefix + 12, spb);
  2027. else
  2028. accumulate_unicast_bytes(neigh, src_prefix, spb);
  2029. end_unicast_message(neigh, MESSAGE_MH_REQUEST_SRC_SPECIFIC, len);
  2030. } else {
  2031. end_unicast_message(neigh, MESSAGE_MH_REQUEST, len);
  2032. }
  2033. }
  2034. void
  2035. send_request_resend(struct neighbour *neigh,
  2036. const unsigned char *prefix, unsigned char plen,
  2037. const unsigned char *src_prefix, unsigned char src_plen,
  2038. unsigned short seqno, unsigned char *id)
  2039. {
  2040. if(neigh)
  2041. send_unicast_multihop_request(neigh, prefix, plen, src_prefix, src_plen,
  2042. seqno, id, 127);
  2043. else
  2044. send_multihop_request(NULL, prefix, plen, src_prefix, src_plen,
  2045. seqno, id, 127);
  2046. record_resend(RESEND_REQUEST, prefix, plen, src_prefix, src_plen,
  2047. // -- lorauth --
  2048. NULL, 0,
  2049. // ---
  2050. seqno, id,
  2051. neigh ? neigh->ifp : NULL, resend_delay);
  2052. }
  2053. void
  2054. handle_request(struct neighbour *neigh, const unsigned char *prefix,
  2055. unsigned char plen,
  2056. const unsigned char *src_prefix, unsigned char src_plen,
  2057. unsigned char hop_count,
  2058. unsigned short seqno, const unsigned char *id)
  2059. {
  2060. struct xroute *xroute;
  2061. struct babel_route *route;
  2062. struct neighbour *successor = NULL;
  2063. xroute = find_xroute(prefix, plen, src_prefix, src_plen);
  2064. route = find_installed_route(prefix, plen, src_prefix, src_plen);
  2065. if(xroute && (!route || xroute->metric <= kernel_metric)) {
  2066. if(hop_count > 0 && memcmp(id, myid, 8) == 0) {
  2067. if(seqno_compare(seqno, myseqno) > 0) {
  2068. if(seqno_minus(seqno, myseqno) > 100) {
  2069. /* Hopelessly out-of-date request */
  2070. return;
  2071. }
  2072. update_myseqno();
  2073. // -- lorauth --
  2074. if(memcmp(id, myid, 8) == 0){
  2075. /* since the seqno may have changed the lorauth cipher
  2076. should be updated too.
  2077. */
  2078. if(route){
  2079. if(lorauth_token(route->src->cipher, myid, myseqno) == -1)
  2080. debugf("-- Error updating lorauth token: myid: %s, seqno: %d",
  2081. format_eui64(myid), myseqno);
  2082. }
  2083. }
  2084. // ----
  2085. }
  2086. }
  2087. //send_update(neigh->ifp, 1, prefix, plen, src_prefix, src_plen);
  2088. //send_update(neigh->ifp, 1, prefix, plen, src_prefix, src_plen, NULL, 0);
  2089. // -- lorauth --
  2090. if(route){
  2091. /* send the update using cipher an clen found */
  2092. send_update(neigh->ifp, 1, prefix, plen, src_prefix, src_plen,
  2093. route->src->cipher, route->src->clen);
  2094. }
  2095. /* else */
  2096. /* send_update(neigh->ifp, 1, prefix, plen, src_prefix, src_plen, NULL, 0); */
  2097. // ----
  2098. return;
  2099. }
  2100. if(route &&
  2101. (memcmp(id, route->src->id, 8) != 0 ||
  2102. seqno_compare(seqno, route->seqno) <= 0))
  2103. {
  2104. //send_update(neigh->ifp, 1, prefix, plen, src_prefix, src_plen);
  2105. //send_update(neigh->ifp, 1, prefix, plen, src_prefix, src_plen, NULL, 0);
  2106. send_update(neigh->ifp, 1, prefix, plen, src_prefix, src_plen,
  2107. route->src->cipher, route->src->clen);
  2108. return;
  2109. }
  2110. if(hop_count <= 1)
  2111. return;
  2112. if(route && memcmp(id, route->src->id, 8) == 0 &&
  2113. seqno_minus(seqno, route->seqno) > 100) {
  2114. /* Hopelessly out-of-date */
  2115. return;
  2116. }
  2117. if(request_redundant(neigh->ifp, prefix, plen, src_prefix, src_plen,
  2118. seqno, id))
  2119. return;
  2120. /* Let's try to forward this request. */
  2121. if(route && route_metric(route) < INFINITY)
  2122. successor = route->neigh;
  2123. if(!successor || successor == neigh) {
  2124. /* We were about to forward a request to its requestor. Try to
  2125. find a different neighbour to forward the request to. */
  2126. struct babel_route *other_route;
  2127. other_route = find_best_route(prefix, plen, src_prefix, src_plen,
  2128. 0, neigh);
  2129. if(other_route && route_metric(other_route) < INFINITY)
  2130. successor = other_route->neigh;
  2131. }
  2132. if(!successor || successor == neigh)
  2133. /* Give up */
  2134. return;
  2135. send_unicast_multihop_request(successor, prefix, plen, src_prefix, src_plen,
  2136. seqno, id, hop_count - 1);
  2137. /* lorauth note: check here for clen and cipher */
  2138. if(route)
  2139. record_resend(RESEND_REQUEST, prefix, plen, src_prefix, src_plen,
  2140. // -- lorauth --
  2141. route->src->cipher, route->src->clen,
  2142. // ----
  2143. seqno, id,
  2144. neigh->ifp, 0);
  2145. record_resend(RESEND_REQUEST, prefix, plen, src_prefix, src_plen,
  2146. // -- lorauth --
  2147. NULL, 0,
  2148. // ----
  2149. seqno, id,
  2150. neigh->ifp, 0);
  2151. }