interface.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. Copyright (c) 2007, 2008 by Juliusz Chroboczek
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include <sys/time.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. #include <net/if.h>
  29. #include <arpa/inet.h>
  30. #include "babeld.h"
  31. #include "util.h"
  32. #include "kernel.h"
  33. #include "interface.h"
  34. #include "neighbour.h"
  35. #include "message.h"
  36. #include "route.h"
  37. #include "configuration.h"
  38. #include "local.h"
  39. #include "xroute.h"
  40. #include "lorauth.h"
  41. struct interface *interfaces = NULL;
  42. static struct interface *
  43. last_interface(void)
  44. {
  45. struct interface *ifp = interfaces;
  46. if(!ifp)
  47. return NULL;
  48. while(ifp->next)
  49. ifp = ifp->next;
  50. return ifp;
  51. }
  52. struct interface *
  53. add_interface(char *ifname, struct interface_conf *if_conf)
  54. {
  55. struct interface *ifp;
  56. FOR_ALL_INTERFACES(ifp) {
  57. if(strcmp(ifp->name, ifname) == 0) {
  58. if(if_conf)
  59. fprintf(stderr,
  60. "Warning: attempting to add existing interface, "
  61. "new configuration ignored.\n");
  62. return ifp;
  63. }
  64. }
  65. ifp = calloc(1, sizeof(struct interface));
  66. if(ifp == NULL)
  67. return NULL;
  68. strncpy(ifp->name, ifname, IF_NAMESIZE);
  69. ifp->conf = if_conf ? if_conf : default_interface_conf;
  70. ifp->bucket_time = now.tv_sec;
  71. ifp->bucket = BUCKET_TOKENS_MAX;
  72. ifp->hello_seqno = (random() & 0xFFFF);
  73. // -- lorauth --
  74. /* ifp->buffered_update->clen = LORAUTH_CIPHER_LEN; */
  75. /* unsigned char *cipher = calloc(1, LORAUTH_CIPHER_LEN + 1); */
  76. /* //lorauth_token(cipher, ifp) */
  77. /* ifp->have_buffered_cipher = 1; */
  78. // ----
  79. if(interfaces == NULL)
  80. interfaces = ifp;
  81. else
  82. last_interface()->next = ifp;
  83. local_notify_interface(ifp, LOCAL_ADD);
  84. return ifp;
  85. }
  86. int
  87. flush_interface(char *ifname)
  88. {
  89. struct interface *ifp, *prev;
  90. prev = NULL;
  91. ifp = interfaces;
  92. while(ifp) {
  93. if(strcmp(ifp->name, ifname) == 0)
  94. break;
  95. prev = ifp;
  96. ifp = ifp->next;
  97. }
  98. if(ifp == NULL)
  99. return 0;
  100. interface_up(ifp, 0);
  101. if(prev)
  102. prev->next = ifp->next;
  103. else
  104. interfaces = ifp->next;
  105. local_notify_interface(ifp, LOCAL_FLUSH);
  106. if(ifp->conf != NULL && ifp->conf != default_interface_conf)
  107. flush_ifconf(ifp->conf);
  108. local_notify_interface(ifp, LOCAL_FLUSH);
  109. free(ifp);
  110. return 1;
  111. }
  112. /* This should be no more than half the hello interval, so that hellos
  113. aren't sent late. The result is in milliseconds. */
  114. unsigned
  115. jitter(struct interface *ifp, int urgent)
  116. {
  117. unsigned interval = ifp->hello_interval;
  118. if(urgent)
  119. interval = MIN(interval, 100);
  120. else
  121. interval = MIN(interval, 4000);
  122. return roughly(interval) / 4;
  123. }
  124. unsigned
  125. update_jitter(struct interface *ifp, int urgent)
  126. {
  127. unsigned interval = ifp->hello_interval;
  128. if(urgent)
  129. interval = MIN(interval, 100);
  130. else
  131. interval = MIN(interval, 4000);
  132. return roughly(interval);
  133. }
  134. void
  135. set_timeout(struct timeval *timeout, int msecs)
  136. {
  137. timeval_add_msec(timeout, &now, roughly(msecs));
  138. }
  139. static int
  140. check_interface_ipv4(struct interface *ifp)
  141. {
  142. unsigned char ipv4[4];
  143. int rc;
  144. if(ifp->ifindex > 0)
  145. rc = kernel_interface_ipv4(ifp->name, ifp->ifindex, ipv4);
  146. else
  147. rc = 0;
  148. if(rc > 0) {
  149. if(!ifp->ipv4 || memcmp(ipv4, ifp->ipv4, 4) != 0) {
  150. debugf("Noticed IPv4 change for %s.\n", ifp->name);
  151. flush_interface_routes(ifp, 0);
  152. if(!ifp->ipv4)
  153. ifp->ipv4 = malloc(4);
  154. if(ifp->ipv4)
  155. memcpy(ifp->ipv4, ipv4, 4);
  156. local_notify_interface(ifp, LOCAL_CHANGE);
  157. return 1;
  158. }
  159. } else {
  160. if(ifp->ipv4) {
  161. debugf("Noticed IPv4 change for %s.\n", ifp->name);
  162. flush_interface_routes(ifp, 0);
  163. free(ifp->ipv4);
  164. ifp->ipv4 = NULL;
  165. local_notify_interface(ifp, LOCAL_CHANGE);
  166. return 1;
  167. }
  168. }
  169. return 0;
  170. }
  171. static int
  172. check_interface_channel(struct interface *ifp)
  173. {
  174. int channel = IF_CONF(ifp, channel);
  175. int rc = 1;
  176. if(channel == IF_CHANNEL_UNKNOWN) {
  177. /* IF_WIRELESS merely means that we know for sure that the
  178. interface is wireless, so check unconditionally. */
  179. channel = kernel_interface_channel(ifp->name, ifp->ifindex);
  180. if(channel < 0) {
  181. if((ifp->flags & IF_WIRELESS))
  182. rc = -1;
  183. channel = (ifp->flags & IF_WIRELESS) ?
  184. IF_CHANNEL_INTERFERING : IF_CHANNEL_NONINTERFERING;
  185. }
  186. }
  187. if(ifp->channel != channel) {
  188. ifp->channel = channel;
  189. return rc;
  190. }
  191. return 0;
  192. }
  193. static int
  194. check_link_local_addresses(struct interface *ifp)
  195. {
  196. struct kernel_route ll[32];
  197. int rc, i;
  198. rc = kernel_addresses(ifp->ifindex, 1, ll, 32);
  199. if(rc <= 0) {
  200. if(rc < 0)
  201. perror("kernel_addresses(link local)");
  202. else
  203. fprintf(stderr, "Interface %s has no link-local address.\n",
  204. ifp->name);
  205. if(ifp->ll) {
  206. free(ifp->ll);
  207. ifp->numll = 0;
  208. ifp->ll = NULL;
  209. }
  210. local_notify_interface(ifp, LOCAL_CHANGE);
  211. /* Most probably DAD hasn't finished yet. Reschedule us
  212. real soon. */
  213. schedule_interfaces_check(2000, 0);
  214. return -1;
  215. } else {
  216. int changed;
  217. if(rc == ifp->numll) {
  218. changed = 0;
  219. for(i = 0; i < rc; i++) {
  220. if(memcmp(ifp->ll[i], ll[i].prefix, 16) != 0) {
  221. changed = 1;
  222. break;
  223. }
  224. }
  225. } else {
  226. changed = 1;
  227. }
  228. if(changed) {
  229. free(ifp->ll);
  230. ifp->numll = 0;
  231. ifp->ll = malloc(16 * rc);
  232. if(ifp->ll == NULL) {
  233. perror("malloc(ll)");
  234. } else {
  235. for(i = 0; i < rc; i++)
  236. memcpy(ifp->ll[i], ll[i].prefix, 16);
  237. ifp->numll = rc;
  238. }
  239. local_notify_interface(ifp, LOCAL_CHANGE);
  240. }
  241. }
  242. return 0;
  243. }
  244. int
  245. interface_up(struct interface *ifp, int up)
  246. {
  247. int mtu, rc, type;
  248. struct ipv6_mreq mreq;
  249. if((!!up) == if_up(ifp))
  250. return 0;
  251. if(up)
  252. ifp->flags |= IF_UP;
  253. else
  254. ifp->flags &= ~IF_UP;
  255. if(up) {
  256. if(ifp->ifindex <= 0) {
  257. fprintf(stderr,
  258. "Upping unknown interface %s.\n", ifp->name);
  259. goto fail;
  260. }
  261. rc = kernel_setup_interface(1, ifp->name, ifp->ifindex);
  262. if(rc < 0) {
  263. fprintf(stderr, "kernel_setup_interface(%s, %d) failed.\n",
  264. ifp->name, ifp->ifindex);
  265. goto fail;
  266. }
  267. mtu = kernel_interface_mtu(ifp->name, ifp->ifindex);
  268. if(mtu < 0) {
  269. fprintf(stderr, "Warning: couldn't get MTU of interface %s (%d).\n",
  270. ifp->name, ifp->ifindex);
  271. mtu = 1280;
  272. }
  273. /* We need to be able to fit at least two messages into a packet,
  274. so MTUs below 116 require lower layer fragmentation. */
  275. /* In IPv6, the minimum MTU is 1280, and every host must be able
  276. to reassemble up to 1500 bytes, but I'd rather not rely on this. */
  277. if(mtu < 128) {
  278. fprintf(stderr, "Suspiciously low MTU %d on interface %s (%d).\n",
  279. mtu, ifp->name, ifp->ifindex);
  280. mtu = 128;
  281. }
  282. if(ifp->sendbuf)
  283. free(ifp->sendbuf);
  284. /* 40 for IPv6 header, 8 for UDP header, 12 for good luck. */
  285. ifp->bufsize = mtu - sizeof(packet_header) - 60;
  286. ifp->sendbuf = malloc(ifp->bufsize);
  287. if(ifp->sendbuf == NULL) {
  288. fprintf(stderr, "Couldn't allocate sendbuf.\n");
  289. ifp->bufsize = 0;
  290. goto fail;
  291. }
  292. rc = resize_receive_buffer(mtu);
  293. if(rc < 0)
  294. fprintf(stderr, "Warning: couldn't resize "
  295. "receive buffer for interface %s (%d) (%d bytes).\n",
  296. ifp->name, ifp->ifindex, mtu);
  297. type = IF_CONF(ifp, type);
  298. if(type == IF_TYPE_DEFAULT) {
  299. if(all_wireless) {
  300. type = IF_TYPE_WIRELESS;
  301. } else {
  302. rc = kernel_interface_wireless(ifp->name, ifp->ifindex);
  303. if(rc < 0) {
  304. fprintf(stderr,
  305. "Warning: couldn't determine whether %s (%d) "
  306. "is a wireless interface.\n",
  307. ifp->name, ifp->ifindex);
  308. } else if(rc) {
  309. type = IF_TYPE_WIRELESS;
  310. }
  311. }
  312. }
  313. printf("Type: %d\n", type);
  314. /* Type is CONFIG_TYPE_AUTO if interface is not known to be
  315. wireless, so provide sane defaults for that case. */
  316. if(type == IF_TYPE_WIRELESS)
  317. ifp->flags |= IF_WIRELESS;
  318. else
  319. ifp->flags &= ~IF_WIRELESS;
  320. ifp->cost = IF_CONF(ifp, cost);
  321. if(ifp->cost <= 0)
  322. ifp->cost = type == IF_TYPE_WIRELESS ? 256 : 96;
  323. if(IF_CONF(ifp, split_horizon) == CONFIG_YES)
  324. ifp->flags |= IF_SPLIT_HORIZON;
  325. else if(IF_CONF(ifp, split_horizon) == CONFIG_NO)
  326. ifp->flags &= ~IF_SPLIT_HORIZON;
  327. else if(type == IF_TYPE_WIRED)
  328. ifp->flags |= IF_SPLIT_HORIZON;
  329. else
  330. ifp->flags &= ~IF_SPLIT_HORIZON;
  331. if(IF_CONF(ifp, lq) == CONFIG_YES)
  332. ifp->flags |= IF_LQ;
  333. else if(IF_CONF(ifp, lq) == CONFIG_NO)
  334. ifp->flags &= ~IF_LQ;
  335. else if(type == IF_TYPE_WIRELESS)
  336. ifp->flags |= IF_LQ;
  337. else
  338. ifp->flags &= ~IF_LQ;
  339. if(IF_CONF(ifp, faraway) == CONFIG_YES)
  340. ifp->flags |= IF_FARAWAY;
  341. if(IF_CONF(ifp, hello_interval) > 0)
  342. ifp->hello_interval = IF_CONF(ifp, hello_interval);
  343. else if(type == IF_TYPE_WIRELESS)
  344. ifp->hello_interval = default_wireless_hello_interval;
  345. else
  346. ifp->hello_interval = default_wired_hello_interval;
  347. ifp->update_interval =
  348. IF_CONF(ifp, update_interval) > 0 ?
  349. IF_CONF(ifp, update_interval) :
  350. ifp->hello_interval * 4;
  351. ifp->rtt_decay =
  352. IF_CONF(ifp, rtt_decay) > 0 ?
  353. IF_CONF(ifp, rtt_decay) : 42;
  354. ifp->rtt_min =
  355. IF_CONF(ifp, rtt_min) > 0 ?
  356. IF_CONF(ifp, rtt_min) : 10000;
  357. ifp->rtt_max =
  358. IF_CONF(ifp, rtt_max) > 0 ?
  359. IF_CONF(ifp, rtt_max) : 120000;
  360. if(ifp->rtt_max <= ifp->rtt_min) {
  361. fprintf(stderr,
  362. "Uh, rtt-max is less than or equal to rtt-min (%d <= %d). "
  363. "Setting it to %d.\n", ifp->rtt_max, ifp->rtt_min,
  364. ifp->rtt_min + 10000);
  365. ifp->rtt_max = ifp->rtt_min + 10000;
  366. }
  367. ifp->max_rtt_penalty = IF_CONF(ifp, max_rtt_penalty);
  368. if(ifp->max_rtt_penalty == 0 && type == IF_TYPE_TUNNEL)
  369. ifp->max_rtt_penalty = 96;
  370. if(IF_CONF(ifp, enable_timestamps) == CONFIG_YES)
  371. ifp->flags |= IF_TIMESTAMPS;
  372. else if(IF_CONF(ifp, enable_timestamps) == CONFIG_NO)
  373. ifp->flags &= ~IF_TIMESTAMPS;
  374. else if(type == IF_TYPE_TUNNEL)
  375. ifp->flags |= IF_TIMESTAMPS;
  376. else
  377. ifp->flags &= ~IF_TIMESTAMPS;
  378. if(ifp->max_rtt_penalty > 0 && !(ifp->flags & IF_TIMESTAMPS))
  379. fprintf(stderr,
  380. "Warning: max_rtt_penalty is set "
  381. "but timestamps are disabled on interface %s.\n",
  382. ifp->name);
  383. rc = check_link_local_addresses(ifp);
  384. if(rc < 0) {
  385. goto fail;
  386. }
  387. memset(&mreq, 0, sizeof(mreq));
  388. memcpy(&mreq.ipv6mr_multiaddr, protocol_group, 16);
  389. mreq.ipv6mr_interface = ifp->ifindex;
  390. rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  391. (char*)&mreq, sizeof(mreq));
  392. if(rc < 0) {
  393. perror("setsockopt(IPV6_JOIN_GROUP)");
  394. goto fail;
  395. }
  396. rc = check_interface_channel(ifp);
  397. if(rc < 0)
  398. fprintf(stderr,
  399. "Warning: couldn't determine channel of interface %s.\n",
  400. ifp->name);
  401. update_interface_metric(ifp); // -- update metric
  402. rc = check_interface_ipv4(ifp);
  403. debugf("Upped interface %s (cost=%d, channel=%d%s).\n",
  404. ifp->name,
  405. ifp->cost,
  406. ifp->channel,
  407. ifp->ipv4 ? ", IPv4" : "");
  408. set_timeout(&ifp->hello_timeout, ifp->hello_interval);
  409. set_timeout(&ifp->update_timeout, ifp->update_interval);
  410. send_hello(ifp);
  411. if(rc > 0)
  412. send_update(ifp, 0, NULL, 0, NULL, 0, NULL, 0); //lorauth
  413. send_request(ifp, NULL, 0, NULL, 0);
  414. } else {
  415. flush_interface_routes(ifp, 0);
  416. ifp->buffered = 0;
  417. ifp->bufsize = 0;
  418. free(ifp->sendbuf);
  419. ifp->num_buffered_updates = 0;
  420. ifp->update_bufsize = 0;
  421. if(ifp->buffered_updates)
  422. free(ifp->buffered_updates);
  423. ifp->buffered_updates = NULL;
  424. ifp->sendbuf = NULL;
  425. if(ifp->ifindex > 0) {
  426. memset(&mreq, 0, sizeof(mreq));
  427. memcpy(&mreq.ipv6mr_multiaddr, protocol_group, 16);
  428. mreq.ipv6mr_interface = ifp->ifindex;
  429. rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
  430. (char*)&mreq, sizeof(mreq));
  431. if(rc < 0)
  432. perror("setsockopt(IPV6_LEAVE_GROUP)");
  433. kernel_setup_interface(0, ifp->name, ifp->ifindex);
  434. }
  435. if(ifp->ll)
  436. free(ifp->ll);
  437. ifp->ll = NULL;
  438. ifp->numll = 0;
  439. }
  440. local_notify_interface(ifp, LOCAL_CHANGE);
  441. return 1;
  442. fail:
  443. assert(up);
  444. interface_up(ifp, 0);
  445. local_notify_interface(ifp, LOCAL_CHANGE);
  446. return -1;
  447. }
  448. int
  449. interface_ll_address(struct interface *ifp, const unsigned char *address)
  450. {
  451. int i;
  452. if(!if_up(ifp))
  453. return 0;
  454. for(i = 0; i < ifp->numll; i++)
  455. if(memcmp(ifp->ll[i], address, 16) == 0)
  456. return 1;
  457. return 0;
  458. }
  459. void
  460. check_interfaces(void)
  461. {
  462. struct interface *ifp;
  463. int rc, ifindex_changed = 0;
  464. unsigned int ifindex;
  465. FOR_ALL_INTERFACES(ifp) {
  466. ifindex = if_nametoindex(ifp->name);
  467. if(ifindex != ifp->ifindex) {
  468. debugf("Noticed ifindex change for %s.\n", ifp->name);
  469. ifp->ifindex = 0;
  470. interface_up(ifp, 0);
  471. ifp->ifindex = ifindex;
  472. ifindex_changed = 1;
  473. }
  474. if(ifp->ifindex > 0)
  475. rc = kernel_interface_operational(ifp->name, ifp->ifindex);
  476. else
  477. rc = 0;
  478. if((rc > 0) != if_up(ifp)) {
  479. debugf("Noticed status change for %s.\n", ifp->name);
  480. interface_up(ifp, rc > 0);
  481. }
  482. if(if_up(ifp)) {
  483. /* Bother, said Pooh. We should probably check for a change
  484. in IPv4 addresses at this point. */
  485. check_link_local_addresses(ifp);
  486. check_interface_channel(ifp);
  487. rc = check_interface_ipv4(ifp);
  488. if(rc > 0) {
  489. send_request(ifp, NULL, 0, NULL, 0);
  490. send_update(ifp, 0, NULL, 0, NULL, 0, NULL, 0); // lorauth
  491. }
  492. }
  493. }
  494. if(ifindex_changed)
  495. renumber_filters();
  496. }