You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hook.c 43KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  1. /* This file is part of Netsukuku
  2. * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
  3. *
  4. * This source code is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * This source code is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * Please refer to the GNU Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Public License along with
  15. * this source code; if not, write to:
  16. * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * --
  19. *
  20. * hook.c:
  21. * This is code which handles the hooking of a new node in netsukuku, or the
  22. * creation of a new gnode.
  23. */
  24. #include "includes.h"
  25. #include "common.h"
  26. #include "libnetlink.h"
  27. #include "ll_map.h"
  28. #include "inet.h"
  29. #include "if.h"
  30. #include "krnl_route.h"
  31. #include "iptunnel.h"
  32. #include "endianness.h"
  33. #include "bmap.h"
  34. #include "route.h"
  35. #include "request.h"
  36. #include "pkts.h"
  37. #include "tracer.h"
  38. #include "qspn.h"
  39. #include "hook.h"
  40. #include "rehook.h"
  41. #include "radar.h"
  42. #include "netsukuku.h"
  43. #include "common.h"
  44. int free_the_tmp_cur_node;
  45. int we_are_rehooking; /* 1 if it is true */
  46. void hook_reset(void);
  47. /*
  48. * hook_fill_rq
  49. *
  50. * It's just a wrapper to rnl_fill_rq().
  51. */
  52. int
  53. hook_fill_rq(map_node * dst_rnode, PACKET * pkt, u_char rq)
  54. {
  55. if (rnl_fill_rq(dst_rnode, pkt) < 0)
  56. return -1;
  57. if (server_opt.dbg_lvl) {
  58. const char *ntop;
  59. ntop = inet_to_str(pkt->to);
  60. debug(DBG_INSANE, "Quest %s to %s", rq_to_str(rq), ntop);
  61. }
  62. return 0;
  63. }
  64. /*
  65. * verify_free_nodes_hdr: verifies the validity of the `fn_hdr'
  66. * free_nodes_hdr. `to' is the ip of the node which sent the
  67. * put_free_nodes reply.
  68. * If the header is valid 0 is returned.
  69. */
  70. int
  71. verify_free_nodes_hdr(inet_prefix * to, struct free_nodes_hdr *fn_hdr)
  72. {
  73. quadro_group qg_a, qg_b;
  74. inet_prefix ipstart;
  75. if (fn_hdr->max_levels > FAMILY_LVLS || !fn_hdr->level)
  76. return 1;
  77. if (fn_hdr->level >= fn_hdr->max_levels)
  78. return 1;
  79. /* If fn_hdr->ipstart != `to' there is an error */
  80. inet_setip(&ipstart, (u_int *) fn_hdr->ipstart, my_family);
  81. iptoquadg(ipstart, me.ext_map, &qg_a, QUADG_GID);
  82. iptoquadg(*to, me.ext_map, &qg_b, QUADG_GID);
  83. if (quadg_gids_cmp(qg_a, qg_b, fn_hdr->level))
  84. return 1;
  85. if (fn_hdr->nodes <= 0 || fn_hdr->nodes == (MAXGROUPNODE - 1))
  86. return 1;
  87. return 0;
  88. }
  89. /*\
  90. * * * put/get free_nodes * *
  91. \*/
  92. /*
  93. * get_free_nodes
  94. *
  95. * It send the GET_FREE_NODES request, used to retrieve the free_nodes pkt
  96. * (see hook.h), to rnode `dst_rnode'.
  97. * `fn_hdr' is the header of the received free_nodes packet.
  98. * `nodes' must be an u_char array with at least MAXGROUPNODES members. All the
  99. * members that go from `free_nodes[0]' to `free_nodes[fn_hdr.nodes]' will be
  100. * filled with the gids of the received free nodes.
  101. * -1 is returned if `to' said its quadro_group is full or if a generic error
  102. * occurred. In this case it is advised to ask to get the free_nodes list from
  103. * another rnode.
  104. * If -2 is returned, the whole Netsukuku net is full, so desist to retry, or
  105. * drop down your neighbors.
  106. */
  107. int
  108. get_free_nodes(map_node * dst_rnode,
  109. struct free_nodes_hdr *fn_hdr, u_char * nodes)
  110. {
  111. PACKET pkt, rpkt;
  112. ssize_t err;
  113. int ret = 0, e, i;
  114. char *buf = 0;
  115. setzero(&pkt, sizeof(PACKET));
  116. setzero(&rpkt, sizeof(PACKET));
  117. if(hook_fill_rq(dst_rnode, &pkt, GET_FREE_NODES) < 0) { _return(-1); }
  118. pkt_addtimeout(&pkt, HOOK_RQ_TIMEOUT, 1, 0);
  119. err =
  120. rnl_send_rq(dst_rnode, &pkt, 0, GET_FREE_NODES, 0, PUT_FREE_NODES,
  121. 1, &rpkt);
  122. if (err < 0) {
  123. if (rpkt.hdr.sz && (u_char) (*rpkt.msg) == E_NTK_FULL)
  124. ERROR_FINISH(ret, -2, finish);
  125. ERROR_FINISH(ret, -1, finish);
  126. }
  127. ints_network_to_host(rpkt.msg, free_nodes_hdr_iinfo);
  128. memcpy(fn_hdr, rpkt.msg, sizeof(struct free_nodes_hdr));
  129. if (verify_free_nodes_hdr(&pkt.to, fn_hdr)) {
  130. error("Malformed PUT_FREE_NODES request hdr from %s",
  131. inet_to_str(pkt.to));
  132. ERROR_FINISH(ret, -1, finish);
  133. }
  134. fn_hdr->nodes++;
  135. buf = rpkt.msg + sizeof(struct free_nodes_hdr);
  136. for (i = 0, e = 0; i < MAXGROUPNODE; i++) {
  137. if (TEST_BIT(buf, i)) {
  138. nodes[e] = i;
  139. e++;
  140. }
  141. }
  142. debug(DBG_NORMAL, "Received %d free %s", fn_hdr->nodes,
  143. fn_hdr->level == 1 ? "nodes" : "gnodes");
  144. finish:
  145. pkt_free(&pkt, 0);
  146. pkt_free(&rpkt, 0);
  147. return ret;
  148. }
  149. /*
  150. * put_free_nodes: It sends a free_nodes pkt to rq_pkt.from. To see what's a
  151. * free_nodes pkt go in hook.h.
  152. */
  153. int
  154. put_free_nodes(PACKET rq_pkt)
  155. {
  156. struct fn_pkt {
  157. struct free_nodes_hdr fn_hdr;
  158. u_char free_nodes[MAXGROUPNODE >> 3];
  159. } _PACKED_ fn_pkt;
  160. PACKET pkt;
  161. int ret = 0, i, e = 0, links;
  162. ssize_t err, pkt_sz;
  163. u_char level, err_reply;
  164. const char *ntop;
  165. char *p = 0;
  166. ntop = inet_to_str(rq_pkt.from);
  167. setzero(&pkt, sizeof(PACKET));
  168. pkt_addto(&pkt, &rq_pkt.from);
  169. pkt_addport(&pkt, ntk_tcp_port);
  170. pkt_addsk(&pkt, my_family, rq_pkt.sk, rq_pkt.sk_type);
  171. pkt_add_dev(&pkt, rq_pkt.dev, 1);
  172. pkt_addcompress(&pkt);
  173. /* We search in each level a gnode which is not full. */
  174. for (level = 1, e = 0; level < me.cur_quadg.levels; level++) {
  175. if (!(me.cur_quadg.gnode[_EL(level)]->flags & GMAP_FULL)) {
  176. e = 1;
  177. break;
  178. }
  179. }
  180. if (!e) {
  181. if (me.ext_map[_EL(me.cur_quadg.levels)][0].flags & GMAP_FULL)
  182. /* <<Netsukuku is completely full, sry>> */
  183. err_reply = E_NTK_FULL;
  184. else
  185. /* Our Quadro Group is full, bye */
  186. err_reply = E_QGROUP_FULL;
  187. pkt_fill_hdr(&pkt.hdr, HOOK_PKT, rq_pkt.hdr.id, PUT_FREE_NODES, 0);
  188. err = pkt_err(pkt, err_reply, 1);
  189. goto finish;
  190. }
  191. /* Ok, we've found one, so let's roll the pkt */
  192. setzero(&fn_pkt, sizeof(fn_pkt));
  193. /*
  194. * Fill the reply packet
  195. */
  196. fn_pkt.fn_hdr.max_levels = me.cur_quadg.levels;
  197. inet_copy_ipdata(fn_pkt.fn_hdr.ipstart, &me.cur_quadg.ipstart[level]);
  198. fn_pkt.fn_hdr.level = level;
  199. fn_pkt.fn_hdr.gid = me.cur_quadg.gid[level];
  200. /*
  201. * Update the hook_join_rate and stores give the stores in
  202. * `fn_pkt.fn_hdr.join_rate' the join_rate destined to `rq_pkt.from'
  203. */
  204. links = me.cur_node->links - rnodes_rehooked - 1;
  205. if (hook_join_rate >= links && links > 0)
  206. fn_pkt.fn_hdr.join_rate = hook_join_rate / links;
  207. else if (hook_join_rate > 0)
  208. fn_pkt.fn_hdr.join_rate = 1;
  209. else
  210. fn_pkt.fn_hdr.join_rate = 0;
  211. hook_join_rate -= fn_pkt.fn_hdr.join_rate;
  212. hook_join_rate = hook_join_rate < 0 ? 0 : hook_join_rate;
  213. rnodes_rehooked++;
  214. /*
  215. * Creates the list of the free nodes, which belongs to the gnode. If
  216. * the gnode level is 1 it scans the int_map to find all the MAP_VOID
  217. * nodes, otherwise it scans the gnode map at level-1 searching for
  218. * GMAP_VOID gnodes.
  219. */
  220. e = 0;
  221. if (level == 1) {
  222. for (i = 0; i < MAXGROUPNODE; i++)
  223. if (me.int_map[i].flags & MAP_VOID) {
  224. SET_BIT(fn_pkt.free_nodes, i);
  225. e++;
  226. }
  227. } else {
  228. for (i = 0; i < MAXGROUPNODE; i++)
  229. if (me.ext_map[_EL(level - 1)][i].flags & GMAP_VOID ||
  230. me.ext_map[_EL(level - 1)][i].g.flags & MAP_VOID) {
  231. SET_BIT(fn_pkt.free_nodes, i);
  232. e++;
  233. }
  234. }
  235. fn_pkt.fn_hdr.nodes = (u_char) e - 1;
  236. /* Go pkt, go! Follow your instinct */
  237. pkt_sz = FREE_NODES_SZ((fn_pkt.fn_hdr.nodes + 1));
  238. pkt_fill_hdr(&pkt.hdr, HOOK_PKT, rq_pkt.hdr.id, PUT_FREE_NODES,
  239. pkt_sz);
  240. pkt.msg = xzalloc(pkt_sz);
  241. p = pkt.msg;
  242. memcpy(p, &fn_pkt, sizeof(fn_pkt));
  243. ints_host_to_network(p, free_nodes_hdr_iinfo);
  244. debug(DBG_INSANE, "Reply %s to %s", re_to_str(pkt.hdr.op), ntop);
  245. err = pkt_send(&pkt);
  246. finish:
  247. if (err < 0) {
  248. error
  249. ("put_free_nodes(): Cannot send the PUT_FREE_NODES reply to %s.",
  250. ntop);
  251. ret = -1;
  252. }
  253. pkt_free(&pkt, 0);
  254. return ret;
  255. }
  256. /*\
  257. * * * put/get qspn_round * *
  258. \*/
  259. /*
  260. * get_qspn_round: It send the GET_QSPN_ROUND request, used to retrieve the
  261. * qspn ids and and qspn times. (see hook.h).
  262. */
  263. int
  264. get_qspn_round(map_node * dst_rnode, struct timeval to_rtt,
  265. struct timeval *qtime, int *qspn_id, int *qspn_gcount)
  266. {
  267. PACKET pkt, rpkt;
  268. struct timeval cur_t;
  269. ssize_t err;
  270. int ret = 0, level;
  271. char *buf = 0;
  272. u_char max_levels;
  273. int_info qr_pkt_iinfo;
  274. setzero(&pkt, sizeof(PACKET));
  275. setzero(&rpkt, sizeof(PACKET));
  276. if(hook_fill_rq(dst_rnode, &pkt, GET_QSPN_ROUND) < 0) { _return(-1); }
  277. pkt_addtimeout(&pkt, HOOK_RQ_TIMEOUT, 1, 0);
  278. err =
  279. rnl_send_rq(dst_rnode, &pkt, 0, GET_QSPN_ROUND, 0, PUT_QSPN_ROUND,
  280. 1, &rpkt);
  281. if (err < 0)
  282. ERROR_FINISH(ret, -1, finish);
  283. buf = rpkt.msg;
  284. bufget(&max_levels, sizeof(u_char));
  285. if (QSPN_ROUND_PKT_SZ(max_levels) != rpkt.hdr.sz ||
  286. max_levels > FAMILY_LVLS) {
  287. error("Malformed PUT_QSPN_ROUND request hdr from %s",
  288. inet_to_str(pkt.to));
  289. ERROR_FINISH(ret, -1, finish);
  290. }
  291. /* Convert the pkt from network to host order */
  292. int_info_copy(&qr_pkt_iinfo, &qspn_round_pkt_iinfo);
  293. qr_pkt_iinfo.int_offset[1] =
  294. me.cur_quadg.levels * sizeof(int) + sizeof(char);
  295. qr_pkt_iinfo.int_offset[2] =
  296. qr_pkt_iinfo.int_offset[1] + sizeof(struct timeval) * max_levels;
  297. qr_pkt_iinfo.int_nmemb[0] = max_levels;
  298. qr_pkt_iinfo.int_nmemb[1] = max_levels * 2;
  299. ints_network_to_host(rpkt.msg, qr_pkt_iinfo);
  300. /* Restoring the qspn_id and the qspn_round time */
  301. bufget(qspn_id, max_levels * sizeof(int));
  302. bufget(qtime, max_levels * sizeof(struct timeval));
  303. gettimeofday(&cur_t, 0);
  304. for (level = 0; level < max_levels; level++) {
  305. timeradd(&to_rtt, &qtime[level], &qtime[level]);
  306. timersub(&cur_t, &qtime[level], &qtime[level]);
  307. }
  308. /* Extracting the qspn_gnode_count */
  309. bufget(qspn_gcount, sizeof(u_int) * GCOUNT_LEVELS);
  310. finish:
  311. pkt_free(&pkt, 0);
  312. pkt_free(&rpkt, 0);
  313. return ret;
  314. }
  315. /*
  316. * put_qspn_round
  317. *
  318. * It sends the current qspn times and ids to rq_pkt.from.
  319. */
  320. int
  321. put_qspn_round(PACKET rq_pkt)
  322. {
  323. /*
  324. * We cannot use this elegant struct because gcc is bugged, -_.
  325. * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27945
  326. *
  327. * We have to wait some years, then when gcc4 will be obsolete (and
  328. * the bug will be solved), we'll activate it.
  329. *
  330. * struct qspn_round_pkt {
  331. * u_char max_levels;
  332. * int32_t qspn_id[me.cur_quadg.levels];
  333. * struct timeval _PACKED_ qtime[me.cur_quadg.levels];
  334. * u_int gcount[GCOUNT_LEVELS];
  335. * }_PACKED_ qr_pkt;
  336. */
  337. char qr_pkt[QSPN_ROUND_PKT_SZ(me.cur_quadg.levels)];
  338. int_info qr_pkt_iinfo;
  339. PACKET pkt;
  340. struct timeval cur_t, *tptr;
  341. int ret = 0;
  342. ssize_t err, pkt_sz;
  343. u_char level;
  344. const char *ntop;
  345. u_char max_levels;
  346. char *buf = 0;
  347. ntop = inet_to_str(rq_pkt.from);
  348. setzero(&pkt, sizeof(PACKET));
  349. pkt_addto(&pkt, &rq_pkt.from);
  350. pkt_addport(&pkt, ntk_tcp_port);
  351. pkt_addsk(&pkt, my_family, rq_pkt.sk, rq_pkt.sk_type);
  352. pkt_add_dev(&pkt, rq_pkt.dev, 1);
  353. pkt_addcompress(&pkt);
  354. /* We fill the qspn_id and the qspn round time */
  355. buf = qr_pkt;
  356. max_levels = me.cur_quadg.levels;
  357. bufput(&max_levels, sizeof(u_char));
  358. bufput(me.cur_qspn_id, sizeof(int) * max_levels);
  359. gettimeofday(&cur_t, 0);
  360. for (level = 0; level < max_levels; level++) {
  361. update_qspn_time(level, 0);
  362. tptr = (struct timeval *) buf;
  363. timersub(&cur_t, &me.cur_qspn_time[level], tptr);
  364. buf += sizeof(struct timeval);
  365. }
  366. /* copy in the pkt the qspn_gnode_count */
  367. bufput(qspn_gnode_count, sizeof(qspn_gnode_count));
  368. /* Convert the pkt from host to network order */
  369. int_info_copy(&qr_pkt_iinfo, &qspn_round_pkt_iinfo);
  370. qr_pkt_iinfo.int_offset[1] =
  371. me.cur_quadg.levels * sizeof(int) + sizeof(char);
  372. qr_pkt_iinfo.int_offset[2] =
  373. qr_pkt_iinfo.int_offset[1] + sizeof(struct timeval) * max_levels;
  374. qr_pkt_iinfo.int_nmemb[0] = me.cur_quadg.levels;
  375. qr_pkt_iinfo.int_nmemb[1] = me.cur_quadg.levels * 2;
  376. ints_host_to_network(qr_pkt, qr_pkt_iinfo);
  377. /* fill the pkt header */
  378. pkt_sz = sizeof(qr_pkt);
  379. pkt_fill_hdr(&pkt.hdr, HOOK_PKT, rq_pkt.hdr.id, PUT_QSPN_ROUND,
  380. pkt_sz);
  381. pkt.msg = xzalloc(pkt_sz);
  382. /* Go pkt, go! Follow your instinct */
  383. debug(DBG_INSANE, "Reply %s to %s", re_to_str(pkt.hdr.op), ntop);
  384. memcpy(pkt.msg, &qr_pkt, sizeof(qr_pkt));
  385. err = pkt_send(&pkt);
  386. if (err < 0) {
  387. error
  388. ("put_qspn_round(): Cannot send the PUT_QSPN_ROUND reply to %s.",
  389. ntop);
  390. ret = -1;
  391. }
  392. pkt_free(&pkt, 0);
  393. return ret;
  394. }
  395. /*\
  396. * * * put/get ext_map * *
  397. \*/
  398. int
  399. put_ext_map(PACKET rq_pkt)
  400. {
  401. PACKET pkt;
  402. const char *ntop;
  403. int ret = 0;
  404. ssize_t err;
  405. size_t pkt_sz = 0;
  406. ntop = inet_to_str(rq_pkt.from);
  407. setzero(&pkt, sizeof(PACKET));
  408. pkt_addsk(&pkt, my_family, rq_pkt.sk, rq_pkt.sk_type);
  409. pkt_addcompress(&pkt);
  410. pkt.msg =
  411. pack_extmap(me.ext_map, MAXGROUPNODE, &me.cur_quadg, &pkt_sz);
  412. pkt.hdr.sz = pkt_sz;
  413. debug(DBG_INSANE, "Reply %s to %s", re_to_str(PUT_EXT_MAP), ntop);
  414. err = send_rq(&pkt, 0, PUT_EXT_MAP, rq_pkt.hdr.id, 0, 0, 0);
  415. if (err < 0) {
  416. error("put_ext_maps(): Cannot send the PUT_EXT_MAP reply to %s.",
  417. ntop);
  418. ERROR_FINISH(ret, -1, finish);
  419. }
  420. finish:
  421. pkt_free(&pkt, 0);
  422. return ret;
  423. }
  424. /*
  425. * get_ext_map: It sends the GET_EXT_MAP request to retrieve the
  426. * dst_node's ext_map.
  427. */
  428. map_gnode **
  429. get_ext_map(map_node * dst_rnode, quadro_group * new_quadg)
  430. {
  431. PACKET pkt, rpkt;
  432. char *pack;
  433. int err;
  434. map_gnode **ext_map = 0, **ret = 0;
  435. setzero(&pkt, sizeof(PACKET));
  436. setzero(&rpkt, sizeof(PACKET));
  437. if(hook_fill_rq(dst_rnode, &pkt, GET_EXT_MAP) < 0) { _return(0); }
  438. pkt_addtimeout(&pkt, HOOK_RQ_TIMEOUT, 1, 0);
  439. err = rnl_send_rq(dst_rnode, &pkt, 0, GET_EXT_MAP, 0, PUT_EXT_MAP, 1,
  440. &rpkt);
  441. if (err < 0) {
  442. ret = 0;
  443. goto finish;
  444. }
  445. pack = rpkt.msg;
  446. ret = ext_map = unpack_extmap(pack, new_quadg);
  447. if (!ext_map)
  448. error
  449. ("get_ext_map: Malformed ext_map. Cannot unpack the ext_map.");
  450. finish:
  451. pkt_free(&pkt, 0);
  452. pkt_free(&rpkt, 0);
  453. return ret;
  454. }
  455. /*\
  456. * * * put/get int_map * *
  457. \*/
  458. int
  459. put_int_map(PACKET rq_pkt)
  460. {
  461. PACKET pkt;
  462. map_node *map = me.int_map;
  463. const char *ntop;
  464. int ret = 0;
  465. ssize_t err;
  466. size_t pkt_sz = 0;
  467. ntop = inet_to_str(rq_pkt.from);
  468. setzero(&pkt, sizeof(PACKET));
  469. pkt_addto(&pkt, &rq_pkt.from);
  470. pkt_addsk(&pkt, my_family, rq_pkt.sk, rq_pkt.sk_type);
  471. pkt_add_dev(&pkt, rq_pkt.dev, 1);
  472. pkt_addcompress(&pkt);
  473. pkt.msg = pack_map(map, 0, MAXGROUPNODE, me.cur_node, &pkt_sz);
  474. pkt.hdr.sz = pkt_sz;
  475. debug(DBG_INSANE, "Reply %s to %s", re_to_str(PUT_INT_MAP), ntop);
  476. err = send_rq(&pkt, 0, PUT_INT_MAP, rq_pkt.hdr.id, 0, 0, 0);
  477. if (err < 0) {
  478. error("put_int_map(): Cannot send the PUT_INT_MAP reply to %s.",
  479. ntop);
  480. ERROR_FINISH(ret, -1, finish);
  481. }
  482. finish:
  483. pkt_free(&pkt, 0);
  484. return ret;
  485. }
  486. /*
  487. * get_int_map: It sends the GET_INT_MAP request to retrieve the
  488. * dst_node's int_map.
  489. */
  490. map_node *
  491. get_int_map(map_node * dst_rnode, map_node ** new_root)
  492. {
  493. PACKET pkt, rpkt;
  494. map_node *int_map, *ret = 0;
  495. int err;
  496. char *pack;
  497. setzero(&pkt, sizeof(PACKET));
  498. setzero(&rpkt, sizeof(PACKET));
  499. if(hook_fill_rq(dst_rnode, &pkt, GET_INT_MAP) < 0) { _return(0); }
  500. pkt_addtimeout(&pkt, HOOK_RQ_TIMEOUT, 1, 0);
  501. err = rnl_send_rq(dst_rnode, &pkt, 0, GET_INT_MAP, 0, PUT_INT_MAP, 1,
  502. &rpkt);
  503. if (err < 0) {
  504. ret = 0;
  505. goto finish;
  506. }
  507. pack = rpkt.msg;
  508. ret = int_map = unpack_map(pack, 0, new_root, MAXGROUPNODE,
  509. MAXRNODEBLOCK_PACK_SZ);
  510. if (!int_map)
  511. error("get_int_map(): Malformed int_map. Cannot load it");
  512. /*Finished, yeah */
  513. finish:
  514. pkt_free(&pkt, 0);
  515. pkt_free(&rpkt, 0);
  516. return ret;
  517. }
  518. /*\
  519. * * * put/get bnode_map * *
  520. \*/
  521. int
  522. put_bnode_map(PACKET rq_pkt)
  523. {
  524. PACKET pkt;
  525. map_bnode **bmaps = me.bnode_map;
  526. const char *ntop;
  527. int ret = 0;
  528. ssize_t err;
  529. size_t pack_sz = 0;
  530. ntop = inet_to_str(rq_pkt.from);
  531. setzero(&pkt, sizeof(PACKET));
  532. pkt_addto(&pkt, &rq_pkt.from);
  533. pkt_addsk(&pkt, my_family, rq_pkt.sk, rq_pkt.sk_type);
  534. pkt_add_dev(&pkt, rq_pkt.dev, 1);
  535. pkt_addcompress(&pkt);
  536. pkt.msg =
  537. pack_all_bmaps(bmaps, me.bmap_nodes, me.ext_map, me.cur_quadg,
  538. &pack_sz);
  539. pkt.hdr.sz = pack_sz;
  540. debug(DBG_INSANE, "Reply %s to %s", re_to_str(PUT_BNODE_MAP), ntop);
  541. err = send_rq(&pkt, 0, PUT_BNODE_MAP, rq_pkt.hdr.id, 0, 0, 0);
  542. if (err < 0) {
  543. error
  544. ("put_bnode_maps(): Cannot send the PUT_BNODE_MAP reply to %s.",
  545. ntop);
  546. ERROR_FINISH(ret, -1, finish);
  547. }
  548. finish:
  549. pkt_free(&pkt, 0);
  550. return ret;
  551. }
  552. /*
  553. * get_bnode_map: It sends the GET_BNODE_MAP request to retrieve the
  554. * dst_node's bnode_map.
  555. */
  556. map_bnode **
  557. get_bnode_map(map_node * dst_rnode, u_int ** bmap_nodes)
  558. {
  559. PACKET pkt, rpkt;
  560. int err;
  561. map_bnode **bnode_map, **ret = 0;
  562. char *pack;
  563. setzero(&pkt, sizeof(PACKET));
  564. setzero(&rpkt, sizeof(PACKET));
  565. if(hook_fill_rq(dst_rnode, &pkt, GET_BNODE_MAP) < 0){ _return(0); }
  566. pkt_addtimeout(&pkt, HOOK_RQ_TIMEOUT, 1, 0);
  567. err = rnl_send_rq(dst_rnode, &pkt, 0, GET_BNODE_MAP, 0, PUT_BNODE_MAP,
  568. 1, &rpkt);
  569. if (err < 0) {
  570. ret = 0;
  571. goto finish;
  572. }
  573. /* Extracting the map... */
  574. pack = rpkt.msg;
  575. ret = bnode_map =
  576. unpack_all_bmaps(pack, FAMILY_LVLS, me.ext_map, bmap_nodes,
  577. MAXGROUPNODE, MAXBNODE_RNODEBLOCK);
  578. if (!bnode_map)
  579. error("get_bnode_map(): Malformed bnode_map. Cannot load it");
  580. finish:
  581. pkt_free(&pkt, 0);
  582. pkt_free(&rpkt, 0);
  583. return ret;
  584. }
  585. /*\
  586. * * * put/get internet gateways list * *
  587. \*/
  588. int
  589. put_internet_gws(PACKET rq_pkt)
  590. {
  591. PACKET pkt;
  592. const char *ntop;
  593. int ret = 0;
  594. ssize_t err;
  595. size_t pack_sz = 0;
  596. ntop = inet_to_str(rq_pkt.from);
  597. setzero(&pkt, sizeof(PACKET));
  598. pkt_addto(&pkt, &rq_pkt.from);
  599. pkt_addsk(&pkt, my_family, rq_pkt.sk, rq_pkt.sk_type);
  600. pkt_add_dev(&pkt, rq_pkt.dev, 1);
  601. pkt_addcompress(&pkt);
  602. pkt.msg = pack_igws(me.igws, me.igws_counter, me.cur_quadg.levels,
  603. (int *) &pack_sz);
  604. pkt.hdr.sz = pack_sz;
  605. debug(DBG_INSANE, "Reply %s to %s", re_to_str(PUT_INTERNET_GWS), ntop);
  606. err = send_rq(&pkt, 0, PUT_INTERNET_GWS, rq_pkt.hdr.id, 0, 0, 0);
  607. if (err < 0) {
  608. error("put_internet_gws(): Cannot send the PUT_INTERNET_GWS "
  609. "reply to %s.", ntop);
  610. ERROR_FINISH(ret, -1, finish);
  611. }
  612. finish:
  613. pkt_free(&pkt, 0);
  614. return ret;
  615. }
  616. /*
  617. * get_internet_gws: It sends the GET_INTERNET_GWS request to retrieve the
  618. * Internet Gateways list from `to'.
  619. */
  620. inet_gw **
  621. get_internet_gws(map_node * dst_rnode, int **igws_counter)
  622. {
  623. PACKET pkt, rpkt;
  624. int err, ret = 0;
  625. inet_gw **igws = 0;
  626. char *pack;
  627. setzero(&pkt, sizeof(PACKET));
  628. setzero(&rpkt, sizeof(PACKET));
  629. if(hook_fill_rq(dst_rnode, &pkt, GET_INTERNET_GWS) < 0) { _return(0); }
  630. pkt_addtimeout(&pkt, HOOK_RQ_TIMEOUT, 1, 0);
  631. err = rnl_send_rq(dst_rnode, &pkt, 0, GET_INTERNET_GWS, 0,
  632. PUT_INTERNET_GWS, 1, &rpkt);
  633. if (err < 0)
  634. ERROR_FINISH(ret, 0, finish);
  635. /* Extracting the list... */
  636. pack = rpkt.msg;
  637. ret =
  638. unpack_igws(pack, rpkt.hdr.sz, me.int_map, me.ext_map, FAMILY_LVLS,
  639. &igws, igws_counter);
  640. if (ret < 0) {
  641. error
  642. ("get_internet_gws(): Malformed internet_gws. Cannot load it");
  643. igws = 0;
  644. }
  645. finish:
  646. pkt_free(&pkt, 0);
  647. pkt_free(&rpkt, 0);
  648. return igws;
  649. }
  650. /*
  651. * hook_set_all_ips
  652. *
  653. * Sets the same `ip' to all the devices.
  654. */
  655. void
  656. hook_set_all_ips(inet_prefix ip, interface * ifs, int ifs_n)
  657. {
  658. const char *ntop;
  659. ntop = inet_to_str(ip);
  660. loginfo("Setting the %s ip to all the interfaces", ntop);
  661. if (my_family == AF_INET) {
  662. /* Down & Up: reset the configurations of all the interfaces */
  663. set_all_ifs(ifs, ifs_n, set_dev_down);
  664. set_all_ifs(ifs, ifs_n, set_dev_up);
  665. } else {
  666. ip_addr_flush_all_ifs(ifs, ifs_n, my_family, RT_SCOPE_UNIVERSE);
  667. ip_addr_flush_all_ifs(ifs, ifs_n, my_family, RT_SCOPE_SITE);
  668. }
  669. if (set_all_dev_ip(ip, ifs, ifs_n) < 0)
  670. fatal("Cannot set the %s ip to all the interfaces", ntop);
  671. if (restricted_mode && (server_opt.use_shared_inet ||
  672. server_opt.share_internet)) {
  673. set_dev_down(DEFAULT_TUNL_IF);
  674. set_dev_up(DEFAULT_TUNL_IF);
  675. if (set_dev_ip(ip, DEFAULT_TUNL_IF) < 0)
  676. fatal("Cannot assign an IP to the default tunnel");
  677. }
  678. }
  679. /*
  680. * create_gnodes
  681. *
  682. * This function is used to create a new gnode (or more) when we are the first
  683. * node in the area or when all the other gnodes are full.
  684. * Our ip will be set to `ip'. If `ip' is NULL, a random ip is chosen.
  685. * create_gnodes() sets also all the vital variables for the new gnode/gnodes
  686. * like me.cur_quadg, me.cur_ip, etc...
  687. * `final_level' is the highest level where we create the gnode, all the other
  688. * gnodes we create are in the sub-levels of `final_level'.
  689. */
  690. int
  691. create_gnodes(inet_prefix * ip, int final_level)
  692. {
  693. int i;
  694. if (!ip) {
  695. random_ip(0, 0, 0, FAMILY_LVLS, me.ext_map, 0, &me.cur_ip,
  696. my_family);
  697. } else
  698. inet_copy(&me.cur_ip, ip);
  699. if (restricted_mode)
  700. inet_setip_localaddr(&me.cur_ip, my_family, restricted_class);
  701. if (!final_level)
  702. final_level = FAMILY_LVLS;
  703. /*
  704. * We remove all the traces of the old gnodes in the ext_map to add the
  705. * new ones.
  706. */
  707. if (!(me.cur_node->flags & MAP_HNODE))
  708. for (i = 1; i < final_level; i++) {
  709. me.cur_quadg.gnode[_EL(i)]->flags &= ~GMAP_ME;
  710. me.cur_quadg.gnode[_EL(i)]->g.flags &= ~MAP_ME & ~MAP_GNODE;
  711. }
  712. /* Now, we update the ext_map with the new gnodes */
  713. me.cur_quadg.levels = FAMILY_LVLS;
  714. reset_extmap(me.ext_map, me.cur_quadg.levels, 0);
  715. iptoquadg(me.cur_ip, me.ext_map, &me.cur_quadg,
  716. QUADG_GID | QUADG_GNODE | QUADG_IPSTART);
  717. /* Set the new flags */
  718. for (i = 1; i < final_level; i++) {
  719. me.cur_quadg.gnode[_EL(i)]->flags &= ~GMAP_VOID;
  720. me.cur_quadg.gnode[_EL(i)]->flags |= GMAP_ME;
  721. me.cur_quadg.gnode[_EL(i)]->g.flags &= ~MAP_VOID;
  722. me.cur_quadg.gnode[_EL(i)]->g.flags |= MAP_ME | MAP_GNODE;
  723. /* Increment the gnode seeds counter */
  724. gnode_inc_seeds(&me.cur_quadg, i);
  725. }
  726. /* Reset the `qspn_gnode_count' counter */
  727. qspn_reset_gcount(qspn_gnode_count, final_level, 1);
  728. /* Tidying up the internal map */
  729. if (free_the_tmp_cur_node) {
  730. xfree(me.cur_node);
  731. free_the_tmp_cur_node = 0;
  732. }
  733. reset_int_map(me.int_map, 0);
  734. me.cur_node = &me.int_map[me.cur_quadg.gid[0]];
  735. me.cur_node->flags &= ~MAP_VOID;
  736. me.cur_node->flags |= MAP_ME;
  737. return 0;
  738. }
  739. /*
  740. * create_new_qgroup
  741. *
  742. * this is just a wrapper to create_gnodes(). It creates completely
  743. * random gnodes in all the levels.
  744. */
  745. void
  746. create_new_qgroup(int hook_level)
  747. {
  748. const char *ntop;
  749. if (we_are_rehooking)
  750. create_gnodes(&rk_gnode_ip, hook_level + 1);
  751. else
  752. create_gnodes(0, FAMILY_LVLS);
  753. ntop = inet_to_str(me.cur_ip);
  754. hook_set_all_ips(me.cur_ip, me.cur_ifs, me.cur_ifs_n);
  755. loginfo("Now we are in a brand new gnode. The ip %s is now"
  756. " used.", ntop);
  757. }
  758. /*
  759. * update_join_rate
  760. *
  761. * it updates the `hook_join_rate' according to `gnode_count', which has the
  762. * gnode count of `hook_gnode' and to `fn_hdr', which is the free_nodes_hdr
  763. * received from `hook_gnode'.
  764. * `old_gcount' is the gnode_count we had before the start of the rehook.
  765. * If a new_gnode has to be created 1 is returned, (and hook_join_rate will be
  766. * 0), otherwise 0 is the returned value.
  767. * If we aren't rehooking or if it isn't necessary to consider the join_rate,
  768. * -1 is returned.
  769. */
  770. int
  771. update_join_rate(map_gnode * hook_gnode, int hook_level,
  772. u_int * old_gcount, u_int * gnode_count,
  773. struct free_nodes_hdr *fn_hdr)
  774. {
  775. u_int free_nodes, total_bnodes;
  776. int new_gnode = 0, i;
  777. if (!hook_level || !hook_gnode || !we_are_rehooking)
  778. return -1;
  779. /*
  780. * `free_nodes' is the number of VOID nodes present at the
  781. * `hook_level' in `hook_gnode', and it is the difference
  782. * between the maximum number of nodes in that level and the
  783. * actual number of nodes in it (gnode_count).
  784. */
  785. free_nodes =
  786. NODES_PER_LEVEL(hook_level) - gnode_count[_EL(hook_level)];
  787. if (old_gcount[_EL(hook_level)] <= free_nodes)
  788. return -1;
  789. debug(DBG_SOFT,
  790. "update_join_rate: free_nodes %d, fn_hdr->join_rate %d",
  791. free_nodes, fn_hdr->join_rate);
  792. /* There aren't free nodes in `hook_gnode', so skip this function */
  793. if (free_nodes <= 0) {
  794. new_gnode = 1;
  795. goto finish;
  796. }
  797. if (map_find_bnode_rnode
  798. (me.bnode_map[hook_level - 1], me.bmap_nodes[hook_level - 1],
  799. hook_gnode) >= 0) {
  800. /* We border on `hook_gnode' so we initialize
  801. * `hook_join_rate' for this new re-hook session,
  802. * later we'll hook at `hook_gnode'. */
  803. hook_join_rate = free_nodes;
  804. for (i = hook_level - 1; i >= 0; i--) {
  805. /* `total_bnodes' = how many bnodes border to
  806. * `hook_gnode' in level `i'th */
  807. total_bnodes =
  808. map_count_bnode_rnode(me.bnode_map[i], me.bmap_nodes[i],
  809. hook_gnode);
  810. total_bnodes = total_bnodes ? total_bnodes : 1;
  811. hook_join_rate /= total_bnodes;
  812. }
  813. new_gnode = 0;
  814. } else if (fn_hdr->join_rate) {
  815. /* The join_rate we got from our rnode is > 0, so
  816. * we hook at `hook_gnode'. */
  817. hook_join_rate = fn_hdr->join_rate;
  818. new_gnode = 0;
  819. } else
  820. new_gnode = 1;
  821. if (!new_gnode) {
  822. /* Don't count us in the join_rate, 'cause we are rehooking */
  823. hook_join_rate--;
  824. hook_join_rate = hook_join_rate < 0 ? 0 : hook_join_rate;
  825. }
  826. debug(DBG_NOISE, "update_join_rate: new join_rate %u, new_gnode %d",
  827. hook_join_rate, new_gnode);
  828. finish:
  829. return new_gnode;
  830. }
  831. /*
  832. * hook_init
  833. *
  834. * inits the hook.c code. Call this function only once, at the start of the
  835. * daemon.
  836. */
  837. int
  838. hook_init(void)
  839. {
  840. /* register the hook's ops in the pkt_op_table */
  841. add_pkt_op(GET_FREE_NODES, SKT_TCP, ntk_tcp_port, put_free_nodes);
  842. add_pkt_op(PUT_FREE_NODES, SKT_TCP, ntk_tcp_port, 0);
  843. add_pkt_op(GET_QSPN_ROUND, SKT_TCP, ntk_tcp_port, put_qspn_round);
  844. add_pkt_op(PUT_QSPN_ROUND, SKT_TCP, ntk_tcp_port, 0);
  845. add_pkt_op(GET_INT_MAP, SKT_TCP, ntk_tcp_port, put_int_map);
  846. add_pkt_op(PUT_INT_MAP, SKT_TCP, ntk_tcp_port, 0);
  847. add_pkt_op(GET_EXT_MAP, SKT_TCP, ntk_tcp_port, put_ext_map);
  848. add_pkt_op(PUT_EXT_MAP, SKT_TCP, ntk_tcp_port, 0);
  849. add_pkt_op(GET_BNODE_MAP, SKT_TCP, ntk_tcp_port, put_bnode_map);
  850. add_pkt_op(PUT_BNODE_MAP, SKT_TCP, ntk_tcp_port, 0);
  851. add_pkt_op(GET_INTERNET_GWS, SKT_TCP, ntk_tcp_port, put_internet_gws);
  852. add_pkt_op(PUT_INTERNET_GWS, SKT_TCP, ntk_tcp_port, 0);
  853. total_hooks = 0;
  854. we_are_rehooking = 0;
  855. free_the_tmp_cur_node = 0;
  856. hook_reset();
  857. debug(DBG_NORMAL, "Activating ip_forward and disabling rp_filter");
  858. route_ip_forward(my_family, 1);
  859. route_rp_filter_all_dev(my_family, me.cur_ifs, me.cur_ifs_n, 0);
  860. if (restricted_mode && (server_opt.share_internet ||
  861. server_opt.share_internet))
  862. route_rp_filter(my_family, DEFAULT_TUNL_IF, 0);
  863. return 0;
  864. }
  865. /*
  866. * hook_reset: resets all the variables needed to hook. This function is
  867. * called at the beginning of netsukuku_hook().
  868. */
  869. void
  870. hook_reset(void)
  871. {
  872. u_int idata[MAX_IP_INT];
  873. /* We use a fake root_node for a while */
  874. if (free_the_tmp_cur_node)
  875. xfree(me.cur_node);
  876. free_the_tmp_cur_node = 1;
  877. me.cur_node = xzalloc(sizeof(map_node));
  878. me.cur_node->flags |= MAP_HNODE;
  879. rnodes_rehooked = hook_join_rate = 0;
  880. /*
  881. * Do not reply to any request while we are hooking, except the radar
  882. * ECHO_ME, ECHO_REPLY, and all the replies.
  883. */
  884. op_filter_reset(OP_FILTER_DROP);
  885. op_filter_reset_re(OP_FILTER_ALLOW);
  886. op_filter_clr(ECHO_ME);
  887. op_filter_clr(ECHO_REPLY);
  888. /*
  889. * We set the dev ip to HOOKING_IP+random_number to begin our
  890. * transaction.
  891. */
  892. setzero(idata, MAX_IP_SZ);
  893. if (my_family == AF_INET) {
  894. idata[0] =
  895. restricted_class ==
  896. RESTRICTED_10 ? HOOKING_IP_10 : HOOKING_IP_172;
  897. } else
  898. idata[0] = HOOKING_IPV6;
  899. if (my_family == AF_INET6)
  900. idata[0] += rand_range(0, MAXGROUPNODE - 2);
  901. else
  902. idata[0] += rand_range(0, MAXGROUPNODE - 2);
  903. inet_setip_raw(&me.cur_ip, idata, my_family);
  904. iptoquadg(me.cur_ip, me.ext_map, &me.cur_quadg,
  905. QUADG_GID | QUADG_GNODE | QUADG_IPSTART);
  906. hook_set_all_ips(me.cur_ip, me.cur_ifs, me.cur_ifs_n);
  907. }
  908. /*
  909. * hook_first_radar_scan: launches the first scan to know what rnodes we have
  910. * around us.
  911. * If a new gnode has to be created, 1 is returned.
  912. */
  913. int
  914. hook_first_radar_scan(map_gnode * hook_gnode, int hook_level,
  915. quadro_group * old_quadg)
  916. {
  917. int total_hooking_nodes, i;
  918. /*
  919. * If we are rehooking to `hook_gnode' tell the radar to ignore all
  920. * the other rnodes, which don't belong to it.
  921. */
  922. if (hook_gnode && we_are_rehooking) {
  923. int gid[NMEMB(old_quadg->gid)];
  924. memcpy(gid, old_quadg->gid, sizeof(old_quadg->gid));
  925. gid[hook_level] =
  926. pos_from_gnode(hook_gnode, me.ext_map[_EL(hook_level)]);
  927. new_rnode_allowed(&alwd_rnodes, &alwd_rnodes_counter, gid,
  928. hook_level, FAMILY_LVLS);
  929. }
  930. /*
  931. * If we are in restricted mode, ignore the restricted nodes which
  932. * belong to our opposite restricted class. So, if we are 10.0.0.1
  933. * ignore 172.x.x.x, and viceversa. This happens only in ipv4.
  934. */
  935. if (restricted_mode && my_family == AF_INET) {
  936. int gid[IPV4_LEVELS] = { 0, 0, 0, 0 };
  937. if (restricted_class == RESTRICTED_10)
  938. gid[3] = 10;
  939. else
  940. gid[3] = 172;
  941. new_rnode_allowed(&alwd_rnodes, &alwd_rnodes_counter,
  942. gid, 3, FAMILY_LVLS);
  943. }
  944. /*
  945. * We do our first scans to know what we've around us. The rnodes are
  946. * kept in me.cur_node->r_nodes.
  947. * The fastest one is in me.cur_node->r_nodes[0].
  948. *
  949. * If after MAX_FIRST_RADAR_SCANS# tries we haven't found any rnodes
  950. * we start as a new gnode.
  951. */
  952. for (i = 0; i < MAX_FIRST_RADAR_SCANS; i++) {
  953. me.cur_node->flags |= MAP_HNODE;
  954. loginfo("Launching radar_scan %d of %d", i + 1,
  955. MAX_FIRST_RADAR_SCANS);
  956. if (radar_scan(0))
  957. fatal("%s:%d: Scan of the area failed. Cannot continue.",
  958. ERROR_POS);
  959. total_hooking_nodes = count_hooking_nodes();
  960. if (!me.cur_node->links ||
  961. (me.cur_node->links == total_hooking_nodes && !hook_retry)) {
  962. /*
  963. * If we have 0 nodes around us, we are alone, so we create a
  964. * new gnode.
  965. * If all the nodes around us are hooking and we started hooking
  966. * before them, we create the new gnode.
  967. */
  968. if (!me.cur_node->links) {
  969. /*
  970. * We haven't found any rnodes. Let's retry the
  971. * radar_scan if i+1<MAX_FIRST_RADAR_SCANS
  972. */
  973. if (i + 1 < MAX_FIRST_RADAR_SCANS)
  974. goto hook_retry_scan;
  975. loginfo("No nodes found! This is a black zone. "
  976. "Creating a new_gnode.");
  977. } else
  978. loginfo("There are %d nodes around, which are hooking"
  979. " like us, but we came first so we have "
  980. "to create the new gnode", total_hooking_nodes);
  981. create_new_qgroup(hook_level);
  982. return 1;
  983. } else if (hook_retry) {
  984. /*
  985. * There are only hooking nodes, but we started the hooking
  986. * after them, so we wait until some of them create the new
  987. * gnode.
  988. */
  989. loginfo
  990. ("I've seen %d hooking nodes around us, and one of them "
  991. "is becoming a new gnode.\n"
  992. " We wait, then we'll restart the hook.",
  993. total_hooking_nodes);
  994. usleep(rand_range(0, 1024)); /* ++entropy, thx to katolaz :) */
  995. sleep(MAX_RADAR_WAIT);
  996. i--;
  997. } else
  998. break;
  999. hook_retry_scan:
  1000. reset_radar();
  1001. rnode_destroy(me.cur_node);
  1002. setzero(me.cur_node, sizeof(map_node));
  1003. me.cur_node->flags |= MAP_HNODE;
  1004. qspn_b_del_all_dead_rnodes();
  1005. }
  1006. if (me.cur_node->links < 1) {
  1007. loginfo("We have %d nodes around us. (%d are hooking)",
  1008. me.cur_node->links, total_hooking_nodes);
  1009. } else if (me.cur_node->links == 1) {
  1010. loginfo("We have %d node around us. (%d are hooking)",
  1011. me.cur_node->links, total_hooking_nodes);
  1012. }
  1013. return 0;
  1014. }
  1015. /*
  1016. * hook_get_free_nodes
  1017. *
  1018. * gets the free_nodes list and the qson_round info from our nearest rnode.
  1019. * In `fn_hdr', `fnodes', `gnode_ipstart' and `new_gcount' there will be stored the relative
  1020. * value.
  1021. * If a new gnode has to be created 1 is returned.
  1022. */
  1023. int
  1024. hook_get_free_nodes(int hook_level, struct free_nodes_hdr *fn_hdr,
  1025. u_char * fnodes, inet_prefix * gnode_ipstart,
  1026. u_int * new_gcount, struct rnode_list **ret_rnl)
  1027. {
  1028. struct radar_queue *rq = 0;
  1029. struct rnode_list *rnl = rlist;
  1030. int e = 0, err;
  1031. /*
  1032. * Now we choose the nearest rnode we found and we send it the
  1033. * GET_FREE_NODES request.
  1034. */
  1035. list_for(rnl) {
  1036. if (rnl->node->flags & MAP_HNODE)
  1037. continue;
  1038. err = get_free_nodes(rnl->node, fn_hdr, fnodes);
  1039. if (err == -2)
  1040. fatal("Netsukuku is full! Bring down some nodes and retry");
  1041. else if (err == -1)
  1042. continue;
  1043. /* Extract the ipstart of the gnode */
  1044. inet_setip(gnode_ipstart, (u_int *) fn_hdr->ipstart, my_family);
  1045. /* Get the qspn round info */
  1046. rq = find_node_radar_q(rnl->node);
  1047. if (!get_qspn_round(rnl->node, rq->final_rtt, me.cur_qspn_time,
  1048. me.cur_qspn_id, (int *) new_gcount)) {
  1049. e = 1;
  1050. break;
  1051. }
  1052. }
  1053. *ret_rnl = rnl;
  1054. if (!e) {
  1055. loginfo("It seems all the quadro_groups in this area are full "
  1056. "or are not cooperating.\n "
  1057. "We are going to create a new gnode");
  1058. create_new_qgroup(hook_level);
  1059. return 1;
  1060. }
  1061. return 0;
  1062. }
  1063. /*
  1064. * hook_choose_new_ip
  1065. *
  1066. * after reading the received `fn_hdr', it decides our new IP and if we have to
  1067. * create a new gnode it returns 1.
  1068. */
  1069. int
  1070. hook_choose_new_ip(map_gnode * hook_gnode, int hook_level,
  1071. struct free_nodes_hdr *fn_hdr, u_char * fnodes,
  1072. inet_prefix * gnode_ipstart)
  1073. {
  1074. int new_gnode, e;
  1075. /*
  1076. * Let's see if we can re-hook at `hook_gnode' or if we have
  1077. * to create a new gnode, in other words: update the join_rate.
  1078. */
  1079. new_gnode = update_join_rate(hook_gnode, hook_level, qspn_old_gcount,
  1080. qspn_gnode_count, fn_hdr);
  1081. if (new_gnode > 0) {
  1082. /*
  1083. * The `hook_gnode' gnode cannot take all the nodes of our
  1084. * gnode so we just give up and create a new gnode, which has
  1085. * a gid based on the hash of our current gid.
  1086. */
  1087. inet_copy(&me.cur_ip, &rk_gnode_ip);
  1088. debug(DBG_NORMAL, "rehook_create_gnode: %s is our new ip",
  1089. inet_to_str(me.cur_ip));
  1090. } else {
  1091. /*
  1092. * We are hooking fine,
  1093. * let's choose a random ip using the free nodes list we received.
  1094. */
  1095. e = rand_range(0, fn_hdr->nodes - 1);
  1096. if (fn_hdr->level == 1) {
  1097. new_gnode = 0;
  1098. postoip(fnodes[e], *gnode_ipstart, &me.cur_ip);
  1099. } else {
  1100. new_gnode = 1;
  1101. for (;;) {
  1102. random_ip(gnode_ipstart, fn_hdr->level, fn_hdr->gid,
  1103. FAMILY_LVLS, me.ext_map, 0,
  1104. &me.cur_ip, my_family);
  1105. if (!inet_validate_ip(me.cur_ip))
  1106. break;
  1107. }
  1108. }
  1109. }
  1110. if (restricted_mode)
  1111. inet_setip_localaddr(&me.cur_ip, my_family, restricted_class);
  1112. hook_set_all_ips(me.cur_ip, me.cur_ifs, me.cur_ifs_n);
  1113. /*
  1114. * Close all the rnl->tcp_sk sockets, 'cause we've changed IP and they
  1115. * aren't valid anymore
  1116. */
  1117. rnl_close_all_sk(rlist);
  1118. return new_gnode;
  1119. }
  1120. /*
  1121. * hook_get_ext_map
  1122. *
  1123. * gets the external map from the rnodes who sent us the free_nodes list.
  1124. * `rq' points to that rnode.
  1125. * `old_ext_map' is the currently used ext_map; it will be merged with the
  1126. * new received map.
  1127. *
  1128. * If a new gnode has been created, 1 is returned.
  1129. */
  1130. int
  1131. hook_get_ext_map(int hook_level, int new_gnode,
  1132. struct rnode_list *rnl, struct free_nodes_hdr *fn_hdr,
  1133. map_gnode ** old_ext_map, quadro_group * old_quadg)
  1134. {
  1135. map_gnode **new_ext_map;
  1136. /*
  1137. * Fetch the ext_map from the node who gave us the free nodes list.
  1138. */
  1139. if (!(new_ext_map = get_ext_map(rnl->node, &me.cur_quadg)))
  1140. fatal("None of the rnodes in this area gave me the extern map");
  1141. me.ext_map = new_ext_map;
  1142. if (we_are_rehooking && hook_level) {
  1143. int gcount, old_gid;
  1144. /*
  1145. * Since we are rehooking, our gnode will change and it will be
  1146. * dismantled, our old gcount has to be decremented from our
  1147. * old ext_map and the one we receive.
  1148. */
  1149. gcount =
  1150. new_ext_map[_EL(hook_level)][old_quadg->gid[hook_level]].
  1151. gcount;
  1152. qspn_dec_gcount((u_int *) qspn_gnode_count, hook_level + 1, gcount);
  1153. old_gid = old_quadg->gid[hook_level];
  1154. new_ext_map[_EL(hook_level)][old_gid].gcount = 0;
  1155. old_ext_map[_EL(hook_level)][old_gid].gcount = 0;
  1156. /*
  1157. * We can also delete our old gid, 'cause it doesn't exist
  1158. * anymore
  1159. */
  1160. gmap_node_del(&new_ext_map[_EL(hook_level)][old_gid]);
  1161. gmap_node_del(&old_ext_map[_EL(hook_level)][old_gid]);
  1162. }
  1163. /* If we have to create new gnodes, let's do it. */
  1164. if (new_gnode) {
  1165. me.ext_map = old_ext_map;
  1166. old_ext_map = new_ext_map;
  1167. memcpy(old_quadg, &me.cur_quadg, sizeof(quadro_group));
  1168. /* Create a new gnode. After this we have a new ip,
  1169. * ext_map and quadro_group */
  1170. create_gnodes(&me.cur_ip,
  1171. we_are_rehooking ? hook_level : fn_hdr->level);
  1172. /* Merge the received ext_map with our new empty ext_map */
  1173. merge_ext_maps(me.ext_map, new_ext_map, me.cur_quadg, *old_quadg);
  1174. free_extmap(old_ext_map, FAMILY_LVLS, 0);
  1175. return 1;
  1176. }
  1177. free_extmap(old_ext_map, FAMILY_LVLS, 0);
  1178. return 0;
  1179. }
  1180. /*
  1181. * hook_get_int_map
  1182. *
  1183. * fetch the internal map from a rnode which belongs to our same gnode.
  1184. */
  1185. void
  1186. hook_get_int_map(void)
  1187. {
  1188. struct radar_queue *rq = radar_q;
  1189. map_node **merg_map, *new_root;
  1190. int imaps = 0, i;
  1191. /*
  1192. * We want a new shiny traslucent internal map
  1193. */
  1194. reset_int_map(me.int_map, 0);
  1195. iptoquadg(me.cur_ip, me.ext_map, &me.cur_quadg,
  1196. QUADG_GID | QUADG_GNODE | QUADG_IPSTART);
  1197. /* Increment the gnode seeds counter of level one, since
  1198. * we are new in that gnode */
  1199. gnode_inc_seeds(&me.cur_quadg, 0);
  1200. /*
  1201. * Fetch the int_map from each rnode and merge them into a
  1202. * single, big, shiny map.
  1203. */
  1204. imaps = 0;
  1205. rq = radar_q;
  1206. merg_map = xzalloc(me.cur_node->links * sizeof(map_node *));
  1207. for (i = 0; i < me.cur_node->links; i++) {
  1208. rq = find_node_radar_q((map_node *) me.cur_node->r_node[i].r_node);
  1209. if (rq->node->flags & MAP_HNODE)
  1210. continue;
  1211. if (quadg_gids_cmp(rq->quadg, me.cur_quadg, 1))
  1212. /* This node isn't part of our gnode, let's skip it */
  1213. continue;
  1214. if ((merg_map[imaps] = get_int_map(rq->node, &new_root))) {
  1215. merge_maps(me.int_map, merg_map[imaps], me.cur_node, new_root);
  1216. imaps++;
  1217. }
  1218. }
  1219. if (!imaps)
  1220. fatal("None of the rnodes in this area gave me the int_map");
  1221. for (i = 0; i < imaps; i++)
  1222. free_map(merg_map[i], 0);
  1223. xfree(merg_map);
  1224. }
  1225. void
  1226. hook_get_bnode_map(void)
  1227. {
  1228. struct radar_queue *rq = radar_q;
  1229. map_bnode **old_bnode_map;
  1230. u_int *old_bnodes;
  1231. int e, i;
  1232. /*
  1233. * Let's get the bnode map. Fast, fast, quick quick!
  1234. */
  1235. e = 0;
  1236. for (i = 0; i < me.cur_node->links; i++) {
  1237. rq = find_node_radar_q((map_node *) me.cur_node->r_node[i].r_node);
  1238. if (rq->node->flags & MAP_HNODE)
  1239. continue;
  1240. if (quadg_gids_cmp(rq->quadg, me.cur_quadg, 1))
  1241. /* This node isn't part of our gnode, let's skip it */
  1242. continue;
  1243. old_bnode_map = me.bnode_map;
  1244. old_bnodes = me.bmap_nodes;
  1245. me.bnode_map = get_bnode_map(rq->node, &me.bmap_nodes);
  1246. if (me.bnode_map) {
  1247. bmap_levels_free(old_bnode_map, old_bnodes);
  1248. e = 1;
  1249. break;
  1250. } else {
  1251. me.bnode_map = old_bnode_map;
  1252. me.bmap_nodes = old_bnodes;
  1253. }
  1254. }
  1255. if (!e)
  1256. loginfo("None of the rnodes in this area gave me the bnode map.");
  1257. }
  1258. void
  1259. hook_get_igw(void)
  1260. {
  1261. struct radar_queue *rq = radar_q;
  1262. inet_gw **old_igws;
  1263. int *old_igws_counter;
  1264. int e, i;
  1265. /*
  1266. * Let's get the Internet Gateway list
  1267. */
  1268. e = 0;
  1269. for (i = 0; i < me.cur_node->links; i++) {
  1270. rq = find_node_radar_q((map_node *) me.cur_node->r_node[i].r_node);
  1271. if (rq->node->flags & MAP_HNODE)
  1272. continue;
  1273. if (quadg_gids_cmp(rq->quadg, me.cur_quadg, 1))
  1274. /* This node isn't part of our gnode, let's skip it */
  1275. continue;
  1276. old_igws = me.igws;
  1277. old_igws_counter = me.igws_counter;
  1278. me.igws = get_internet_gws(rq->node, &me.igws_counter);
  1279. if (me.igws) {
  1280. free_igws(old_igws, old_igws_counter, FAMILY_LVLS);
  1281. e = 1;
  1282. break;
  1283. } else {
  1284. me.igws = old_igws;
  1285. me.igws_counter = old_igws_counter;
  1286. }
  1287. }
  1288. if (!e) {
  1289. loginfo("None gave me the Internet Gateway list");
  1290. reset_igws(me.igws, me.igws_counter, FAMILY_LVLS);
  1291. }
  1292. }
  1293. /*
  1294. * hook_finish: final part of the netsukuku_hook process
  1295. */
  1296. void
  1297. hook_finish(int new_gnode, struct free_nodes_hdr *fn_hdr)
  1298. {
  1299. int tracer_levels, i;
  1300. /*
  1301. * We must reset the radar_queue because the first radar_scan, used while hooking,
  1302. * has to keep the list of the rnodes' "inet_prefix ip". In this way we know
  1303. * the rnodes' ips even if we haven't an int_map yet.
  1304. */
  1305. reset_radar();
  1306. /* Clear the allowed_rnode llist */
  1307. reset_rnode_allowed(&alwd_rnodes, &alwd_rnodes_counter);
  1308. /* We have finished the hook */
  1309. me.cur_node->flags &= ~MAP_HNODE;
  1310. /* Disable the filter */
  1311. op_filter_reset(OP_FILTER_ALLOW);
  1312. if (new_gnode) {
  1313. if (!me.cur_node->links)
  1314. /*
  1315. * We are a node lost in the desert, so we don't send
  1316. * anything because nobody is listening
  1317. */
  1318. tracer_levels = 0;
  1319. else
  1320. /*
  1321. * We are a new gnode, so we send the tracer in all higher
  1322. * levels
  1323. */
  1324. tracer_levels = fn_hdr->level;
  1325. } else {
  1326. /*
  1327. * We are just a normal node inside a gnode, let's notice only
  1328. * the other nodes in this gnode.
  1329. */
  1330. tracer_levels = 2;
  1331. }
  1332. /*
  1333. * Initialize me.my_igws
  1334. */
  1335. if (server_opt.share_internet) {
  1336. free_my_igws(&me.my_igws);
  1337. init_my_igws(me.igws, me.igws_counter, &me.my_igws,
  1338. me.my_bandwidth, me.cur_node, &me.cur_quadg);
  1339. }
  1340. loginfo("Starting the second radar scan before sending our"
  1341. " first tracer_pkt");
  1342. if (radar_scan(0))
  1343. fatal("%s:%d: Scan of the area failed. Cannot continue.",
  1344. ERROR_POS);
  1345. /*
  1346. * Now we send a simple tracer_pkt in all the level we have to. This pkt
  1347. * is just to say <<Hey there, I'm here, alive>>, thus the other nodes
  1348. * of the gnode will have the basic routes to reach us.
  1349. * Note that this is done only at the first time we hook.
  1350. */
  1351. if (!we_are_rehooking) {
  1352. usleep(rand_range(0, 999999));
  1353. tracer_pkt_start_mutex = 0;
  1354. for (i = 1; i < tracer_levels; i++)
  1355. tracer_pkt_start(i - 1);
  1356. }
  1357. /* Let's fill the krnl routing table */
  1358. loginfo("Filling the kernel routing table");
  1359. rt_full_update(0);
  1360. if (restricted_mode && (server_opt.use_shared_inet ||
  1361. server_opt.share_internet))
  1362. igw_replace_def_igws(me.igws, me.igws_counter,
  1363. me.my_igws, me.cur_quadg.levels, my_family);
  1364. /* (Re)Hook completed */
  1365. loginfo("%sook completed", we_are_rehooking ? "Reh" : "H");
  1366. we_are_rehooking = 0;
  1367. }
  1368. /*
  1369. * netsukuku_hook: hooks/rehooks at an existing gnode or creates a new one.
  1370. * `hook_level' specifies at what level we are hooking, generally it is 0.
  1371. * If `hook_gnode' is not null, netsukuku_hook will try to hook only to the
  1372. * rnodes which belongs to the `hook_gnode' at `hook_level' level.
  1373. */
  1374. int
  1375. netsukuku_hook(map_gnode * hook_gnode, int hook_level)
  1376. {
  1377. struct rnode_list *rnl = rlist;
  1378. struct free_nodes_hdr fn_hdr;
  1379. inet_prefix gnode_ipstart, old_ip;
  1380. quadro_group old_quadg;
  1381. int ret = 0, new_gnode = 0;
  1382. u_char fnodes[MAXGROUPNODE];
  1383. /* Save our current IP before resetting */
  1384. inet_copy(&old_ip, &me.cur_ip);
  1385. memcpy(&old_quadg, &me.cur_quadg, sizeof(quadro_group));
  1386. /* Reset the hook */
  1387. if (total_hooks) {
  1388. hook_reset();
  1389. we_are_rehooking = 1;
  1390. }
  1391. total_hooks++;
  1392. /*
  1393. * * The beginning * *
  1394. */
  1395. loginfo("The %s begins. Starting to scan the area",
  1396. we_are_rehooking ? "rehook" : "hook");
  1397. new_gnode = hook_first_radar_scan(hook_gnode, hook_level, &old_quadg);
  1398. if (new_gnode)
  1399. goto finish;
  1400. /*
  1401. * Get the free nodes list
  1402. */
  1403. qspn_backup_gcount(qspn_old_gcount, (int *) qspn_gnode_count);
  1404. new_gnode = hook_get_free_nodes(hook_level, &fn_hdr, fnodes,
  1405. &gnode_ipstart, qspn_gnode_count,
  1406. &rnl);
  1407. if (new_gnode)
  1408. goto finish;
  1409. /*
  1410. * Choose a new IP
  1411. */
  1412. new_gnode = hook_choose_new_ip(hook_gnode, hook_level, &fn_hdr, fnodes,
  1413. &gnode_ipstart);
  1414. /*
  1415. * Get the external map
  1416. */
  1417. new_gnode = hook_get_ext_map(hook_level, new_gnode, rnl, &fn_hdr,
  1418. me.ext_map, &old_quadg);
  1419. if (new_gnode)
  1420. goto finish;
  1421. /*
  1422. * Get the internal map
  1423. */
  1424. hook_get_int_map();
  1425. /*
  1426. * Fetch the bnode map
  1427. */
  1428. hook_get_bnode_map();
  1429. /*
  1430. * If we are in restricted mode, get the Internet Gateways
  1431. */
  1432. if (restricted_mode && (server_opt.use_shared_inet ||
  1433. server_opt.share_internet))
  1434. hook_get_igw();
  1435. /*
  1436. * And that's all, clean the mess
  1437. */
  1438. if (free_the_tmp_cur_node) {
  1439. xfree(me.cur_node);
  1440. free_the_tmp_cur_node = 0;
  1441. }
  1442. me.cur_node = &me.int_map[me.cur_quadg.gid[0]];
  1443. map_node_del(me.cur_node);
  1444. me.cur_node->flags &= ~MAP_VOID;
  1445. me.cur_node->flags |= MAP_ME;
  1446. /* We need a fresh me.cur_node */
  1447. refresh_hook_root_node();
  1448. finish:
  1449. hook_finish(new_gnode, &fn_hdr);
  1450. return ret;
  1451. }
  1452. /*
  1453. * And this is the end my dear.
  1454. */