babeld.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /*
  2. Copyright (c) 2007, 2008 by Juliusz Chroboczek
  3. Copyright (c) 2010 by Vincent Gross
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include <string.h>
  21. #include <stdarg.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <sys/time.h>
  28. #include <time.h>
  29. #include <signal.h>
  30. #include <assert.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/socket.h>
  33. #include <netinet/in.h>
  34. #include <net/if.h>
  35. #include <arpa/inet.h>
  36. #include "babeld.h"
  37. #include "util.h"
  38. #include "net.h"
  39. #include "kernel.h"
  40. #include "interface.h"
  41. #include "source.h"
  42. #include "neighbour.h"
  43. #include "route.h"
  44. #include "xroute.h"
  45. #include "message.h"
  46. #include "resend.h"
  47. #include "configuration.h"
  48. #include "local.h"
  49. #include "rule.h"
  50. #include "version.h"
  51. #include "decrypt.h"
  52. #include "lorauth.h"
  53. struct timeval now;
  54. unsigned char myid[8];
  55. int have_id = 0;
  56. int debug = 0;
  57. int link_detect = 0;
  58. int all_wireless = 0;
  59. int has_ipv6_subtrees = 0;
  60. int default_wireless_hello_interval = -1;
  61. int default_wired_hello_interval = -1;
  62. int resend_delay = -1;
  63. int random_id = 0;
  64. int do_daemonise = 0;
  65. int skip_kernel_setup = 0;
  66. const char *logfile = NULL,
  67. *pidfile = "/var/run/babeld.pid",
  68. *state_file = "/var/lib/babel-state";
  69. unsigned char *receive_buffer = NULL;
  70. int receive_buffer_size = 0;
  71. const unsigned char zeroes[16] = {0};
  72. const unsigned char ones[16] =
  73. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  74. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  75. int protocol_port;
  76. unsigned char protocol_group[16];
  77. int protocol_socket = -1;
  78. int kernel_socket = -1;
  79. static int kernel_routes_changed = 0;
  80. static int kernel_rules_changed = 0;
  81. static int kernel_link_changed = 0;
  82. static int kernel_addr_changed = 0;
  83. struct timeval check_neighbours_timeout, check_interfaces_timeout;
  84. static volatile sig_atomic_t exiting = 0, dumping = 0, reopening = 0;
  85. static int accept_local_connections(void);
  86. static void init_signals(void);
  87. static void dump_tables(FILE *out);
  88. // -- rsa decryption --
  89. mbedtls_rsa_context rsa_context;
  90. mbedtls_entropy_context rsa_entropy;
  91. mbedtls_ctr_drbg_context rsa_ctr_drbg;
  92. // ----
  93. static int
  94. kernel_route_notify(struct kernel_route *route, void *closure)
  95. {
  96. kernel_routes_changed = 1;
  97. return -1;
  98. }
  99. static int
  100. kernel_addr_notify(struct kernel_addr *addr, void *closure)
  101. {
  102. kernel_addr_changed = 1;
  103. return -1;
  104. }
  105. static int
  106. kernel_link_notify(struct kernel_link *link, void *closure)
  107. {
  108. struct interface *ifp;
  109. FOR_ALL_INTERFACES(ifp) {
  110. if(strcmp(ifp->name, link->ifname) == 0) {
  111. kernel_link_changed = 1;
  112. return -1;
  113. }
  114. }
  115. return 0;
  116. }
  117. static int
  118. kernel_rule_notify(struct kernel_rule *rule, void *closure)
  119. {
  120. int i;
  121. if(martian_prefix(rule->src, rule->src_plen))
  122. return 0;
  123. i = rule->priority - src_table_prio;
  124. if(i < 0 || SRC_TABLE_NUM <= i)
  125. return 0;
  126. kernel_rules_changed = 1;
  127. return -1;
  128. }
  129. static void lorauth_testings(void)
  130. {
  131. // -- quick rsa decrypt tests --
  132. printf("\n---- testing decrypt module ----\n");
  133. printf("rsa_init: %d \n" , (int)rsa_module_init("rsa_pub.txt",
  134. &rsa_context,
  135. &rsa_entropy,
  136. &rsa_ctr_drbg));
  137. printf("rsa_context:");
  138. printf(" N: %d \n E: %d\n", rsa_context.N, rsa_context.E);
  139. printf(" len: %d \n", (int)rsa_context.len);
  140. char *encrypted_message = calloc(1, 514);
  141. strcpy(encrypted_message, "0D329E2B5CA35A5487A35FCF8B91586011B2B3EA38CDACFD2E5A14F4B3681A740A9346E85C7227795D11B61513481C776330535E6BAF1EC856EE69343FD84569C2F1F9202FD572B9AC099D61F4AC68E094F3EA31B77B0FC205B14805868263366849F2E783529BDB57BDBB213660C66FD3BC8E43EB4F05992467AFC7F2CB14854D4A7DDFA615FD56472E34CED2B675282E68E9E90A36549B9EE6284B7CF5D59D7BD8CE05D9FF15BA07E6A7CEBAFA10538E6ACD97AB8708ED0A76749B97A7C5A1C02E83EF227918B9CE7DC986A5109F4DEDDD2BBB38C4E97801EBC937446806CBC46707E71853517BFF9779C78B1F0B9ACAE72CDB509E5CA52FE7B08576B9624C");
  142. printf("rsa_decrypt: %d \n", (int)rsa_decrypt(&rsa_context,
  143. &rsa_entropy,
  144. &rsa_ctr_drbg,
  145. encrypted_message,
  146. (unsigned char *)&rsa_result));
  147. printf("decrypted message: %s\n", rsa_result);
  148. printf("rsa_decrypt: %d \n", (int)rsa_decrypt(&rsa_context,
  149. &rsa_entropy,
  150. &rsa_ctr_drbg,
  151. "293FBB480F423A43FAA0F794B54682EB9593E81CC286E8135AF4F135E2275C9B09DFF5FDAD6052876E47A998AFF56318F0986E5467BE26C7ACECF8799825D93EB03BE9BC41B86AE66A99551FDAC66D10821AA5FE27391A534846297378A75D7386EAA285EB4A18CB1F0CC2B3BA5903D0E7166DCD9BE4BFEEEEB820F0524C492748084535C5D28500D77382437BEFAA1AD8347913D2F71D7EC725B61B6FAABDB4C1EA77BAEBDE37ED997990E49193A9FDAC772A0EAFFB735B128C593D15953AD75A310BDB95AAACF39229DF3634422DB3F97FDA095F21982F73AD3723844AC59E93EA0FC7C16EFD6723D2385A476818B5A2AC9363F59B13C3D4DFBB82B2AC0676",
  152. (unsigned char *)&rsa_result));
  153. printf("decrypted message: %s\n", rsa_result);
  154. printf("\n-----------\n");
  155. printf("\n----- Testing lorauth token ------");
  156. unsigned char *token = calloc(1, LORAUTH_CIPHER_LEN + 1);
  157. unsigned char id_testing[8] = {0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88};
  158. printf("\nlorauth_token:\n");
  159. printf("ID: %s \n", format_eui64(id_testing));
  160. printf("seqno: 0x3214\n");
  161. printf("index: \t %d\n", lorauth_token_index(id_testing,
  162. 0x3214));
  163. lorauth_token(token, id_testing, 0x3214);
  164. printf("token:\n%s -- %d\n", token, strlen((char*)token));
  165. printf("reduced token: %s\n", reduced_lorauth_token(token));
  166. rsa_decrypt(&rsa_context, &rsa_entropy, &rsa_ctr_drbg, (char *)token,
  167. (unsigned char *)&rsa_result);
  168. printf("\n decrypted Token: %s\n", rsa_result);
  169. printf("\n ------ Tesiting lorauth check ----------- \n");
  170. unsigned char prefix_testing[16] = {80,0,2,1,0,0,0,0,0,0,0,0,0,0,0,0};
  171. printf("check: %d\n", check_lorauth_token(id_testing,
  172. prefix_testing,
  173. 0x3214,
  174. strlen((char*)token),
  175. (char*)token));
  176. printf("new check\n");
  177. printf("\ncheck: %d\n", check_lorauth_token(id_testing,
  178. prefix_testing,
  179. 0x3214,
  180. 512,
  181. (char *)encrypted_message));
  182. printf("\n-----------\n");
  183. free(token);
  184. // ----
  185. }
  186. int
  187. main(int argc, char **argv)
  188. {
  189. lorauth_testings();
  190. struct sockaddr_in6 sin6;
  191. int rc, fd, i, opt;
  192. time_t expiry_time, source_expiry_time, kernel_dump_time;
  193. const char **config_files = NULL;
  194. int num_config_files = 0;
  195. void *vrc;
  196. unsigned int seed;
  197. struct interface *ifp;
  198. gettime(&now);
  199. rc = read_random_bytes(&seed, sizeof(seed));
  200. if(rc < 0) {
  201. perror("read(random)");
  202. seed = 42;
  203. }
  204. seed ^= (now.tv_sec ^ now.tv_usec);
  205. srandom(seed);
  206. parse_address("ff02:0:0:0:0:0:1:6", protocol_group, NULL);
  207. protocol_port = 6696;
  208. change_smoothing_half_life(4);
  209. has_ipv6_subtrees = kernel_has_ipv6_subtrees();
  210. while(1) {
  211. opt = getopt(argc, argv,
  212. "m:p:h:H:i:k:A:sruS:d:g:G:lwz:M:t:T:c:C:DL:I:V");
  213. if(opt < 0)
  214. break;
  215. switch(opt) {
  216. case 'm':
  217. rc = parse_address(optarg, protocol_group, NULL);
  218. if(rc < 0)
  219. goto usage;
  220. if(protocol_group[0] != 0xff) {
  221. fprintf(stderr,
  222. "%s is not a multicast address\n", optarg);
  223. goto usage;
  224. }
  225. if(protocol_group[1] != 2) {
  226. fprintf(stderr,
  227. "Warning: %s is not a link-local multicast address\n",
  228. optarg);
  229. }
  230. break;
  231. case 'p':
  232. protocol_port = parse_nat(optarg);
  233. if(protocol_port <= 0 || protocol_port > 0xFFFF)
  234. goto usage;
  235. break;
  236. case 'h':
  237. default_wireless_hello_interval = parse_thousands(optarg);
  238. if(default_wireless_hello_interval <= 0 ||
  239. default_wireless_hello_interval > 0xFFFF * 10)
  240. goto usage;
  241. break;
  242. case 'H':
  243. default_wired_hello_interval = parse_thousands(optarg);
  244. if(default_wired_hello_interval <= 0 ||
  245. default_wired_hello_interval > 0xFFFF * 10)
  246. goto usage;
  247. break;
  248. case 'k':
  249. kernel_metric = parse_nat(optarg);
  250. if(kernel_metric < 0 || kernel_metric > 0xFFFF)
  251. goto usage;
  252. break;
  253. case 'A':
  254. allow_duplicates = parse_nat(optarg);
  255. if(allow_duplicates < 0 || allow_duplicates > 0xFFFF)
  256. goto usage;
  257. break;
  258. case 's':
  259. split_horizon = 0;
  260. break;
  261. case 'r':
  262. random_id = 1;
  263. break;
  264. case 'u':
  265. keep_unfeasible = 1;
  266. break;
  267. case 'S':
  268. state_file = optarg;
  269. break;
  270. case 'd':
  271. debug = parse_nat(optarg);
  272. if(debug < 0)
  273. goto usage;
  274. break;
  275. case 'g':
  276. case 'G':
  277. if(opt == 'g')
  278. local_server_write = 0;
  279. else
  280. local_server_write = 1;
  281. if(optarg[0] == '/') {
  282. local_server_port = -1;
  283. free(local_server_path);
  284. local_server_path = strdup(optarg);
  285. } else {
  286. local_server_port = parse_nat(optarg);
  287. free(local_server_path);
  288. local_server_path = NULL;
  289. if(local_server_port <= 0 || local_server_port > 0xFFFF)
  290. goto usage;
  291. }
  292. break;
  293. case 'l':
  294. link_detect = 1;
  295. break;
  296. case 'w':
  297. all_wireless = 1;
  298. break;
  299. case 'z':
  300. {
  301. char *comma;
  302. diversity_kind = (int)strtol(optarg, &comma, 0);
  303. if(*comma == '\0')
  304. diversity_factor = 128;
  305. else if(*comma == ',')
  306. diversity_factor = parse_nat(comma + 1);
  307. else
  308. goto usage;
  309. if(diversity_factor <= 0 || diversity_factor > 256)
  310. goto usage;
  311. }
  312. break;
  313. case 'M': {
  314. int l = parse_nat(optarg);
  315. if(l < 0 || l > 3600)
  316. goto usage;
  317. change_smoothing_half_life(l);
  318. break;
  319. }
  320. case 't':
  321. export_table = parse_nat(optarg);
  322. if(export_table < 0 || export_table > 0xFFFF)
  323. goto usage;
  324. break;
  325. case 'T':
  326. if(add_import_table(parse_nat(optarg)))
  327. goto usage;
  328. break;
  329. case 'c':
  330. config_files = realloc(config_files,
  331. (num_config_files + 1) * sizeof(char*));
  332. if(config_files == NULL) {
  333. fprintf(stderr, "Couldn't allocate config file.\n");
  334. exit(1);
  335. }
  336. config_files[num_config_files++] = optarg;
  337. break;
  338. case 'C':
  339. rc = parse_config_from_string(optarg, strlen(optarg), NULL);
  340. if(rc != CONFIG_ACTION_DONE) {
  341. fprintf(stderr,
  342. "Couldn't parse configuration from command line.\n");
  343. exit(1);
  344. }
  345. break;
  346. case 'D':
  347. do_daemonise = 1;
  348. break;
  349. case 'L':
  350. logfile = optarg;
  351. break;
  352. case 'I':
  353. pidfile = optarg;
  354. break;
  355. case 'V':
  356. fprintf(stderr, "%s\n", BABELD_VERSION);
  357. exit(0);
  358. break;
  359. default:
  360. goto usage;
  361. }
  362. }
  363. if(num_config_files == 0) {
  364. if(access("/etc/babeld.conf", F_OK) >= 0) {
  365. config_files = malloc(sizeof(char*));
  366. if(config_files == NULL) {
  367. fprintf(stderr, "Couldn't allocate config file.\n");
  368. exit(1);
  369. }
  370. config_files[num_config_files++] = "/etc/babeld.conf";
  371. }
  372. }
  373. for(i = 0; i < num_config_files; i++) {
  374. int line;
  375. rc = parse_config_from_file(config_files[i], &line);
  376. if(rc < 0) {
  377. fprintf(stderr,
  378. "Couldn't parse configuration from file %s "
  379. "(error at line %d).\n",
  380. config_files[i], line);
  381. exit(1);
  382. }
  383. }
  384. free(config_files);
  385. if(default_wireless_hello_interval <= 0)
  386. default_wireless_hello_interval = 4000;
  387. default_wireless_hello_interval = MAX(default_wireless_hello_interval, 5);
  388. if(default_wired_hello_interval <= 0)
  389. default_wired_hello_interval = 4000;
  390. default_wired_hello_interval = MAX(default_wired_hello_interval, 5);
  391. resend_delay = 2000;
  392. resend_delay = MIN(resend_delay, default_wireless_hello_interval / 2);
  393. resend_delay = MIN(resend_delay, default_wired_hello_interval / 2);
  394. resend_delay = MAX(resend_delay, 20);
  395. if(do_daemonise) {
  396. if(logfile == NULL)
  397. logfile = "/var/log/babeld.log";
  398. }
  399. rc = reopen_logfile();
  400. if(rc < 0) {
  401. perror("reopen_logfile()");
  402. exit(1);
  403. }
  404. fd = open("/dev/null", O_RDONLY);
  405. if(fd < 0) {
  406. perror("open(null)");
  407. exit(1);
  408. }
  409. rc = dup2(fd, 0);
  410. if(rc < 0) {
  411. perror("dup2(null, 0)");
  412. exit(1);
  413. }
  414. close(fd);
  415. if(do_daemonise) {
  416. rc = daemonise();
  417. if(rc < 0) {
  418. perror("daemonise");
  419. exit(1);
  420. }
  421. }
  422. if(pidfile && pidfile[0] != '\0') {
  423. int pfd, len;
  424. char buf[100];
  425. len = snprintf(buf, 100, "%lu", (unsigned long)getpid());
  426. if(len < 0 || len >= 100) {
  427. perror("snprintf(getpid)");
  428. exit(1);
  429. }
  430. pfd = open(pidfile, O_WRONLY | O_CREAT | O_EXCL, 0644);
  431. if(pfd < 0) {
  432. char buf[40];
  433. snprintf(buf, 40, "creat(%s)", pidfile);
  434. buf[39] = '\0';
  435. perror(buf);
  436. exit(1);
  437. }
  438. rc = write(pfd, buf, len);
  439. if(rc < len) {
  440. perror("write(pidfile)");
  441. goto fail_pid;
  442. }
  443. close(pfd);
  444. }
  445. rc = kernel_setup(1);
  446. if(rc < 0) {
  447. fprintf(stderr, "kernel_setup failed.\n");
  448. goto fail_pid;
  449. }
  450. rc = kernel_setup_socket(1);
  451. if(rc < 0) {
  452. fprintf(stderr, "kernel_setup_socket failed.\n");
  453. kernel_setup(0);
  454. goto fail_pid;
  455. }
  456. rc = finalise_config();
  457. if(rc < 0) {
  458. fprintf(stderr, "Couldn't finalise configuration.\n");
  459. goto fail;
  460. }
  461. for(i = optind; i < argc; i++) {
  462. vrc = add_interface(argv[i], NULL);
  463. if(vrc == NULL)
  464. goto fail;
  465. }
  466. if(interfaces == NULL) {
  467. fprintf(stderr, "Eek... asked to run on no interfaces!\n");
  468. goto fail;
  469. }
  470. if(!have_id && !random_id) {
  471. /* We use all available interfaces here, since this increases the
  472. chances of getting a stable router-id in case the set of Babel
  473. interfaces changes. */
  474. for(i = 1; i < 256; i++) {
  475. char buf[IF_NAMESIZE], *ifname;
  476. unsigned char eui[8];
  477. ifname = if_indextoname(i, buf);
  478. if(ifname == NULL)
  479. continue;
  480. rc = if_eui64(ifname, i, eui);
  481. if(rc < 0)
  482. continue;
  483. memcpy(myid, eui, 8);
  484. have_id = 1;
  485. break;
  486. }
  487. }
  488. if(!have_id) {
  489. if(!random_id)
  490. fprintf(stderr,
  491. "Warning: couldn't find router id -- "
  492. "using random value.\n");
  493. rc = read_random_bytes(myid, 8);
  494. if(rc < 0) {
  495. perror("read(random)");
  496. goto fail;
  497. }
  498. /* Clear group and global bits */
  499. myid[0] &= ~3;
  500. }
  501. myseqno = (random() & 0xFFFF);
  502. fd = open(state_file, O_RDONLY);
  503. if(fd < 0 && errno != ENOENT)
  504. perror("open(babel-state)");
  505. rc = unlink(state_file);
  506. if(fd >= 0 && rc < 0) {
  507. perror("unlink(babel-state)");
  508. /* If we couldn't unlink it, it's probably stale. */
  509. close(fd);
  510. fd = -1;
  511. }
  512. if(fd >= 0) {
  513. char buf[100];
  514. int s;
  515. rc = read(fd, buf, 99);
  516. if(rc < 0) {
  517. perror("read(babel-state)");
  518. } else {
  519. buf[rc] = '\0';
  520. rc = sscanf(buf, "%d\n", &s);
  521. if(rc == 1 && s >= 0 && s <= 0xFFFF) {
  522. myseqno = seqno_plus(s, 1);
  523. } else {
  524. fprintf(stderr, "Couldn't parse babel-state.\n");
  525. }
  526. }
  527. close(fd);
  528. fd = -1;
  529. }
  530. protocol_socket = babel_socket(protocol_port);
  531. if(protocol_socket < 0) {
  532. perror("Couldn't create link local socket");
  533. goto fail;
  534. }
  535. if(local_server_port >= 0) {
  536. local_server_socket = tcp_server_socket(local_server_port, 1);
  537. if(local_server_socket < 0) {
  538. perror("local_server_socket");
  539. goto fail;
  540. }
  541. } else if(local_server_path) {
  542. local_server_socket = unix_server_socket(local_server_path);
  543. if(local_server_socket < 0) {
  544. perror("local_server_socket");
  545. goto fail;
  546. }
  547. }
  548. init_signals();
  549. rc = resize_receive_buffer(1500);
  550. if(rc < 0)
  551. goto fail;
  552. if(receive_buffer == NULL)
  553. goto fail;
  554. debugf("before check interfaces..\n");
  555. check_interfaces();
  556. debugf("after check interfaces..\n");
  557. // first check_xroutes
  558. rc = check_xroutes(0);
  559. if(rc < 0)
  560. fprintf(stderr, "Warning: couldn't check exported routes.\n");
  561. rc = check_rules();
  562. if(rc < 0)
  563. fprintf(stderr, "Warning: couldn't check rules.\n");
  564. kernel_routes_changed = 0;
  565. kernel_rules_changed = 0;
  566. kernel_link_changed = 0;
  567. kernel_addr_changed = 0;
  568. kernel_dump_time = now.tv_sec + roughly(30);
  569. schedule_neighbours_check(5000, 1);
  570. schedule_interfaces_check(30000, 1);
  571. expiry_time = now.tv_sec + roughly(30);
  572. source_expiry_time = now.tv_sec + roughly(300);
  573. /* Make some noise so that others notice us, and send retractions in
  574. case we were restarted recently */
  575. FOR_ALL_INTERFACES(ifp) {
  576. if(!if_up(ifp))
  577. continue;
  578. /* Apply jitter before we send the first message. */
  579. usleep(roughly(10000));
  580. gettime(&now);
  581. send_hello(ifp);
  582. send_wildcard_retraction(ifp);
  583. }
  584. FOR_ALL_INTERFACES(ifp) {
  585. if(!if_up(ifp))
  586. continue;
  587. usleep(roughly(10000));
  588. gettime(&now);
  589. send_hello(ifp);
  590. send_wildcard_retraction(ifp);
  591. send_self_update(ifp);
  592. send_request(ifp, NULL, 0, NULL, 0);
  593. flushupdates(ifp);
  594. flushbuf(ifp);
  595. }
  596. debugf("Entering main loop.\n");
  597. while(1) {
  598. struct timeval tv;
  599. fd_set readfds;
  600. gettime(&now);
  601. tv = check_neighbours_timeout;
  602. timeval_min(&tv, &check_interfaces_timeout);
  603. timeval_min_sec(&tv, expiry_time);
  604. timeval_min_sec(&tv, source_expiry_time);
  605. timeval_min_sec(&tv, kernel_dump_time);
  606. timeval_min(&tv, &resend_time);
  607. FOR_ALL_INTERFACES(ifp) {
  608. if(!if_up(ifp))
  609. continue;
  610. timeval_min(&tv, &ifp->flush_timeout);
  611. timeval_min(&tv, &ifp->hello_timeout);
  612. timeval_min(&tv, &ifp->update_timeout);
  613. timeval_min(&tv, &ifp->update_flush_timeout);
  614. }
  615. timeval_min(&tv, &unicast_flush_timeout);
  616. FD_ZERO(&readfds);
  617. if(timeval_compare(&tv, &now) > 0) {
  618. int maxfd = 0;
  619. timeval_minus(&tv, &tv, &now);
  620. FD_SET(protocol_socket, &readfds);
  621. maxfd = MAX(maxfd, protocol_socket);
  622. if(kernel_socket < 0) kernel_setup_socket(1);
  623. if(kernel_socket >= 0) {
  624. FD_SET(kernel_socket, &readfds);
  625. maxfd = MAX(maxfd, kernel_socket);
  626. }
  627. if(local_server_socket >= 0 &&
  628. num_local_sockets < MAX_LOCAL_SOCKETS) {
  629. FD_SET(local_server_socket, &readfds);
  630. maxfd = MAX(maxfd, local_server_socket);
  631. }
  632. for(i = 0; i < num_local_sockets; i++) {
  633. FD_SET(local_sockets[i].fd, &readfds);
  634. maxfd = MAX(maxfd, local_sockets[i].fd);
  635. }
  636. rc = select(maxfd + 1, &readfds, NULL, NULL, &tv);
  637. if(rc < 0) {
  638. if(errno != EINTR) {
  639. perror("select");
  640. sleep(1);
  641. }
  642. rc = 0;
  643. FD_ZERO(&readfds);
  644. }
  645. }
  646. gettime(&now);
  647. if(exiting)
  648. break;
  649. if(kernel_socket >= 0 && FD_ISSET(kernel_socket, &readfds)) {
  650. struct kernel_filter filter = {0};
  651. filter.route = kernel_route_notify;
  652. filter.addr = kernel_addr_notify;
  653. filter.link = kernel_link_notify;
  654. filter.rule = kernel_rule_notify;
  655. kernel_callback(&filter);
  656. }
  657. if(FD_ISSET(protocol_socket, &readfds)) {
  658. rc = babel_recv(protocol_socket,
  659. receive_buffer, receive_buffer_size,
  660. (struct sockaddr*)&sin6, sizeof(sin6));
  661. if(rc < 0) {
  662. if(errno != EAGAIN && errno != EINTR) {
  663. perror("recv");
  664. sleep(1);
  665. }
  666. } else {
  667. FOR_ALL_INTERFACES(ifp) {
  668. if(!if_up(ifp))
  669. continue;
  670. if(ifp->ifindex == sin6.sin6_scope_id) {
  671. parse_packet((unsigned char*)&sin6.sin6_addr, ifp,
  672. receive_buffer, rc);
  673. VALGRIND_MAKE_MEM_UNDEFINED(receive_buffer,
  674. receive_buffer_size);
  675. break;
  676. }
  677. }
  678. }
  679. }
  680. if(local_server_socket >= 0 && FD_ISSET(local_server_socket, &readfds))
  681. accept_local_connections();
  682. i = 0;
  683. while(i < num_local_sockets) {
  684. if(FD_ISSET(local_sockets[i].fd, &readfds)) {
  685. rc = local_read(&local_sockets[i]);
  686. if(rc <= 0) {
  687. if(rc < 0) {
  688. if(errno == EINTR || errno == EAGAIN)
  689. continue;
  690. perror("read(local_socket)");
  691. }
  692. local_socket_destroy(i);
  693. }
  694. }
  695. i++;
  696. }
  697. if(reopening) {
  698. kernel_dump_time = now.tv_sec;
  699. check_neighbours_timeout = now;
  700. expiry_time = now.tv_sec;
  701. rc = reopen_logfile();
  702. if(rc < 0) {
  703. perror("reopen_logfile");
  704. break;
  705. }
  706. reopening = 0;
  707. }
  708. if(kernel_link_changed || kernel_addr_changed) {
  709. check_interfaces();
  710. kernel_link_changed = 0;
  711. }
  712. if(kernel_routes_changed || kernel_addr_changed ||
  713. kernel_rules_changed || now.tv_sec >= kernel_dump_time) {
  714. // --mark
  715. rc = check_xroutes(1);
  716. if(rc < 0)
  717. fprintf(stderr, "Warning: couldn't check exported routes.\n");
  718. rc = check_rules();
  719. if(rc < 0)
  720. fprintf(stderr, "Warning: couldn't check rules.\n");
  721. kernel_routes_changed = kernel_rules_changed =
  722. kernel_addr_changed = 0;
  723. if(kernel_socket >= 0)
  724. kernel_dump_time = now.tv_sec + roughly(300);
  725. else
  726. kernel_dump_time = now.tv_sec + roughly(30);
  727. }
  728. if(timeval_compare(&check_neighbours_timeout, &now) < 0) {
  729. int msecs;
  730. msecs = check_neighbours();
  731. /* Multiply by 3/2 to allow neighbours to expire. */
  732. msecs = MAX(3 * msecs / 2, 10);
  733. schedule_neighbours_check(msecs, 1);
  734. }
  735. if(timeval_compare(&check_interfaces_timeout, &now) < 0) {
  736. check_interfaces();
  737. schedule_interfaces_check(30000, 1);
  738. }
  739. if(now.tv_sec >= expiry_time) {
  740. expire_routes();
  741. expire_resend();
  742. expiry_time = now.tv_sec + roughly(30);
  743. }
  744. if(now.tv_sec >= source_expiry_time) {
  745. expire_sources();
  746. source_expiry_time = now.tv_sec + roughly(300);
  747. }
  748. FOR_ALL_INTERFACES(ifp) {
  749. if(!if_up(ifp))
  750. continue;
  751. if(timeval_compare(&now, &ifp->hello_timeout) >= 0)
  752. send_hello(ifp);
  753. if(timeval_compare(&now, &ifp->update_timeout) >= 0)
  754. send_update(ifp, 0, NULL, 0, NULL, 0, NULL, 0); // -- lorauth
  755. if(timeval_compare(&now, &ifp->update_flush_timeout) >= 0)
  756. flushupdates(ifp);
  757. }
  758. if(resend_time.tv_sec != 0) {
  759. if(timeval_compare(&now, &resend_time) >= 0)
  760. {
  761. printf("\n llamando a do_resen()\n");
  762. do_resend();
  763. }
  764. }
  765. if(unicast_flush_timeout.tv_sec != 0) {
  766. if(timeval_compare(&now, &unicast_flush_timeout) >= 0)
  767. flush_unicast(1);
  768. }
  769. FOR_ALL_INTERFACES(ifp) {
  770. if(!if_up(ifp))
  771. continue;
  772. if(ifp->flush_timeout.tv_sec != 0) {
  773. if(timeval_compare(&now, &ifp->flush_timeout) >= 0)
  774. flushbuf(ifp);
  775. }
  776. }
  777. if(UNLIKELY(debug || dumping)) {
  778. dump_tables(stdout);
  779. dumping = 0;
  780. }
  781. } // end while(1)
  782. debugf("Exiting...\n");
  783. usleep(roughly(10000));
  784. gettime(&now);
  785. /* We need to flush so interface_up won't try to reinstall. */
  786. flush_all_routes();
  787. // -- mark
  788. FOR_ALL_INTERFACES(ifp) {
  789. if(!if_up(ifp))
  790. continue;
  791. send_wildcard_retraction(ifp);
  792. /* Make sure that we expire quickly from our neighbours'
  793. association caches. */
  794. send_hello_noupdate(ifp, 10);
  795. flushbuf(ifp);
  796. usleep(roughly(1000));
  797. gettime(&now);
  798. }
  799. FOR_ALL_INTERFACES(ifp) {
  800. if(!if_up(ifp))
  801. continue;
  802. /* Make sure they got it. */
  803. send_wildcard_retraction(ifp);
  804. send_hello_noupdate(ifp, 1);
  805. flushbuf(ifp);
  806. usleep(roughly(10000));
  807. gettime(&now);
  808. interface_up(ifp, 0);
  809. }
  810. release_tables();
  811. kernel_setup_socket(0);
  812. kernel_setup(0);
  813. fd = open(state_file, O_WRONLY | O_TRUNC | O_CREAT, 0644);
  814. if(fd < 0) {
  815. perror("creat(babel-state)");
  816. unlink(state_file);
  817. } else {
  818. char buf[10];
  819. rc = snprintf(buf, 10, "%d\n", (int)myseqno);
  820. if(rc < 0 || rc >= 10) {
  821. fprintf(stderr, "write(babel-state): overflow.\n");
  822. unlink(state_file);
  823. } else {
  824. rc = write(fd, buf, rc);
  825. if(rc < 0) {
  826. perror("write(babel-state)");
  827. unlink(state_file);
  828. }
  829. fsync(fd);
  830. }
  831. close(fd);
  832. }
  833. if(local_server_socket >= 0 && local_server_path) {
  834. unlink(local_server_path);
  835. free(local_server_path);
  836. }
  837. if(pidfile)
  838. unlink(pidfile);
  839. debugf("Done.\n");
  840. return 0;
  841. usage:
  842. fprintf(stderr,
  843. "%s\n"
  844. "Syntax: babeld "
  845. "[-V] [-m multicast_address] [-p port] [-S state-file]\n"
  846. " "
  847. "[-h hello] [-H wired_hello] [-z kind[,factor]]\n"
  848. " "
  849. "[-g port] [-G port] [-k metric] [-A metric] [-s] [-l] [-w] [-r]\n"
  850. " "
  851. "[-u] [-t table] [-T table] [-c file] [-C statement]\n"
  852. " "
  853. "[-d level] [-D] [-L logfile] [-I pidfile]\n"
  854. " "
  855. "interface...\n",
  856. BABELD_VERSION);
  857. exit(1);
  858. fail:
  859. FOR_ALL_INTERFACES(ifp) {
  860. if(!if_up(ifp))
  861. continue;
  862. interface_up(ifp, 0);
  863. }
  864. kernel_setup_socket(0);
  865. kernel_setup(0);
  866. fail_pid:
  867. if(pidfile)
  868. unlink(pidfile);
  869. exit(1);
  870. }
  871. static int
  872. accept_local_connections()
  873. {
  874. int rc, s;
  875. struct local_socket *ls;
  876. if(local_server_socket < 0)
  877. return 0;
  878. s = accept(local_server_socket, NULL, NULL);
  879. if(s < 0) {
  880. if(errno != EINTR && errno != EAGAIN) {
  881. perror("accept(local_server_socket)");
  882. return -1;
  883. }
  884. return 0;
  885. }
  886. if(num_local_sockets >= MAX_LOCAL_SOCKETS) {
  887. /* This should never happen, since we don't select for
  888. the server socket in this case. But I'm paranoid. */
  889. fprintf(stderr, "Internal error: too many local sockets.\n");
  890. close(s);
  891. return -1;
  892. }
  893. rc = fcntl(s, F_GETFL, 0);
  894. if(rc < 0) {
  895. fprintf(stderr, "Unable to get flags of local socket.\n");
  896. close(s);
  897. return -1;
  898. }
  899. rc = fcntl(s, F_SETFL, (rc | O_NONBLOCK));
  900. if(rc < 0) {
  901. fprintf(stderr, "Unable to set flags of local socket.\n");
  902. close(s);
  903. return -1;
  904. }
  905. ls = local_socket_create(s);
  906. if(ls == NULL) {
  907. fprintf(stderr, "Unable create local socket.\n");
  908. close(s);
  909. return -1;
  910. }
  911. local_header(ls);
  912. return 1;
  913. }
  914. void
  915. schedule_neighbours_check(int msecs, int override)
  916. {
  917. struct timeval timeout;
  918. timeval_add_msec(&timeout, &now, roughly(msecs));
  919. if(override)
  920. check_neighbours_timeout = timeout;
  921. else
  922. timeval_min(&check_neighbours_timeout, &timeout);
  923. }
  924. void
  925. schedule_interfaces_check(int msecs, int override)
  926. {
  927. struct timeval timeout;
  928. timeval_add_msec(&timeout, &now, roughly(msecs));
  929. if(override)
  930. check_interfaces_timeout = timeout;
  931. else
  932. timeval_min(&check_interfaces_timeout, &timeout);
  933. }
  934. int
  935. resize_receive_buffer(int size)
  936. {
  937. unsigned char *new;
  938. if(size <= receive_buffer_size)
  939. return 0;
  940. new = realloc(receive_buffer, size);
  941. if(new == NULL) {
  942. perror("realloc(receive_buffer)");
  943. return -1;
  944. }
  945. receive_buffer = new;
  946. receive_buffer_size = size;
  947. return 1;
  948. }
  949. static void
  950. sigexit(int signo)
  951. {
  952. exiting = 1;
  953. }
  954. static void
  955. sigdump(int signo)
  956. {
  957. dumping = 1;
  958. }
  959. static void
  960. sigreopening(int signo)
  961. {
  962. reopening = 1;
  963. }
  964. static void
  965. init_signals(void)
  966. {
  967. struct sigaction sa;
  968. sigset_t ss;
  969. sigemptyset(&ss);
  970. sa.sa_handler = sigexit;
  971. sa.sa_mask = ss;
  972. sa.sa_flags = 0;
  973. sigaction(SIGTERM, &sa, NULL);
  974. sigemptyset(&ss);
  975. sa.sa_handler = sigexit;
  976. sa.sa_mask = ss;
  977. sa.sa_flags = 0;
  978. sigaction(SIGHUP, &sa, NULL);
  979. sigemptyset(&ss);
  980. sa.sa_handler = sigexit;
  981. sa.sa_mask = ss;
  982. sa.sa_flags = 0;
  983. sigaction(SIGINT, &sa, NULL);
  984. sigemptyset(&ss);
  985. sa.sa_handler = SIG_IGN;
  986. sa.sa_mask = ss;
  987. sa.sa_flags = 0;
  988. sigaction(SIGPIPE, &sa, NULL);
  989. sigemptyset(&ss);
  990. sa.sa_handler = sigdump;
  991. sa.sa_mask = ss;
  992. sa.sa_flags = 0;
  993. sigaction(SIGUSR1, &sa, NULL);
  994. sigemptyset(&ss);
  995. sa.sa_handler = sigreopening;
  996. sa.sa_mask = ss;
  997. sa.sa_flags = 0;
  998. sigaction(SIGUSR2, &sa, NULL);
  999. #ifdef SIGINFO
  1000. sigemptyset(&ss);
  1001. sa.sa_handler = sigdump;
  1002. sa.sa_mask = ss;
  1003. sa.sa_flags = 0;
  1004. sigaction(SIGINFO, &sa, NULL);
  1005. #endif
  1006. }
  1007. static void
  1008. dump_route(FILE *out, struct babel_route *route)
  1009. {
  1010. const unsigned char *nexthop =
  1011. memcmp(route->nexthop, route->neigh->address, 16) == 0 ?
  1012. NULL : route->nexthop;
  1013. char channels[100];
  1014. if(route->channels_len == 0) {
  1015. channels[0] = '\0';
  1016. } else {
  1017. int k, j = 0;
  1018. snprintf(channels, 100, " chan (");
  1019. j = strlen(channels);
  1020. for(k = 0; k < route->channels_len; k++) {
  1021. if(k > 0)
  1022. channels[j++] = ',';
  1023. snprintf(channels + j, 100 - j, "%u", (unsigned)route->channels[k]);
  1024. j = strlen(channels);
  1025. }
  1026. snprintf(channels + j, 100 - j, ")");
  1027. }
  1028. fprintf(out, "%s%s%s metric %d (%d) refmetric %d id %s "
  1029. "seqno %d%s age %d via %s neigh %s%s%s%s\n"
  1030. "cipher: %s\n",
  1031. format_prefix(route->src->prefix, route->src->plen),
  1032. route->src->src_plen > 0 ? " from " : "",
  1033. route->src->src_plen > 0 ?
  1034. format_prefix(route->src->src_prefix, route->src->src_plen) : "",
  1035. route_metric(route), route_smoothed_metric(route), route->refmetric,
  1036. format_eui64(route->src->id),
  1037. (int)route->seqno,
  1038. channels,
  1039. (int)(now.tv_sec - route->time),
  1040. route->neigh->ifp->name,
  1041. format_address(route->neigh->address),
  1042. nexthop ? " nexthop " : "",
  1043. nexthop ? format_address(nexthop) : "",
  1044. route->installed ? " (installed)" :
  1045. route_feasible(route) ? " (feasible)" : "",
  1046. // -- lorauth --
  1047. reduced_lorauth_token(route->src->cipher));
  1048. }
  1049. static void
  1050. dump_xroute(FILE *out, struct xroute *xroute)
  1051. {
  1052. fprintf(out, "%s%s%s metric %d cipher %c%c%c%c...%c%c (exported)\n",
  1053. format_prefix(xroute->prefix, xroute->plen),
  1054. xroute->src_plen > 0 ? " from " : "",
  1055. xroute->src_plen > 0 ?
  1056. format_prefix(xroute->src_prefix, xroute->src_plen) : "",
  1057. xroute->metric,
  1058. // -- lorauth --
  1059. xroute->cipher[0],xroute->cipher[1],xroute->cipher[2],xroute->cipher[3],
  1060. xroute->cipher[LORAUTH_CIPHER_LEN-2],
  1061. xroute->cipher[LORAUTH_CIPHER_LEN-1]);
  1062. }
  1063. static void
  1064. dump_tables(FILE *out)
  1065. {
  1066. struct neighbour *neigh;
  1067. struct xroute_stream *xroutes;
  1068. struct route_stream *routes;
  1069. fprintf(out, "\n");
  1070. fprintf(out, "My id %s seqno %d\n", format_eui64(myid), myseqno);
  1071. FOR_ALL_NEIGHBOURS(neigh) {
  1072. fprintf(out, "Neighbour %s dev %s reach %04x rxcost %d txcost %d "
  1073. "rtt %s rttcost %d chan %d%s.\n",
  1074. format_address(neigh->address),
  1075. neigh->ifp->name,
  1076. neigh->reach,
  1077. neighbour_rxcost(neigh),
  1078. neigh->txcost,
  1079. format_thousands(neigh->rtt),
  1080. neighbour_rttcost(neigh),
  1081. neigh->ifp->channel,
  1082. if_up(neigh->ifp) ? "" : " (down)");
  1083. }
  1084. xroutes = xroute_stream();
  1085. if(xroutes) {
  1086. while(1) {
  1087. struct xroute *xroute = xroute_stream_next(xroutes);
  1088. if(xroute == NULL) break;
  1089. dump_xroute(out, xroute);
  1090. }
  1091. xroute_stream_done(xroutes);
  1092. }
  1093. routes = route_stream(ROUTE_ALL);
  1094. if(routes) {
  1095. while(1) {
  1096. struct babel_route *route = route_stream_next(routes);
  1097. if(route == NULL) break;
  1098. dump_route(out, route);
  1099. }
  1100. route_stream_done(routes);
  1101. }
  1102. fflush(out);
  1103. }
  1104. int
  1105. reopen_logfile()
  1106. {
  1107. int lfd, rc;
  1108. if(logfile == NULL)
  1109. return 0;
  1110. lfd = open(logfile, O_CREAT | O_WRONLY | O_APPEND, 0644);
  1111. if(lfd < 0)
  1112. return -1;
  1113. fflush(stdout);
  1114. fflush(stderr);
  1115. rc = dup2(lfd, 1);
  1116. if(rc < 0)
  1117. return -1;
  1118. rc = dup2(lfd, 2);
  1119. if(rc < 0)
  1120. return -1;
  1121. if(lfd > 2)
  1122. close(lfd);
  1123. return 1;
  1124. }