kernel_socket.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /*
  2. Copyright (c) 2007 by Grégoire Henry
  3. Copyright (c) 2008, 2009 by Juliusz Chroboczek
  4. Copyright (c) 2010 by Vincent Gross
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. */
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <time.h>
  28. #include <strings.h>
  29. #include <netinet/in.h>
  30. #include <netinet/icmp6.h>
  31. #include <arpa/inet.h>
  32. #include <sys/ioctl.h>
  33. #include <sys/types.h>
  34. #include <sys/sysctl.h>
  35. #include <sys/socket.h>
  36. #include <ifaddrs.h>
  37. #include <net/if.h>
  38. #include <net/if_dl.h>
  39. #include <net/if_media.h>
  40. #include <net/route.h>
  41. #include "babeld.h"
  42. #include "neighbour.h"
  43. #include "kernel.h"
  44. #include "util.h"
  45. static int get_sdl(struct sockaddr_dl *sdl, char *ifname);
  46. static const unsigned char v4prefix[16] =
  47. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
  48. int export_table = -1, import_table_count = 0, import_tables[MAX_IMPORT_TABLES];
  49. int
  50. if_eui64(char *ifname, int ifindex, unsigned char *eui)
  51. {
  52. struct sockaddr_dl sdl;
  53. char *tmp = NULL;
  54. if(get_sdl(&sdl, ifname) < 0) {
  55. return -1;
  56. }
  57. tmp = sdl.sdl_data + sdl.sdl_nlen;
  58. if(sdl.sdl_alen == 8) {
  59. memcpy(eui, tmp, 8);
  60. eui[0] ^= 2;
  61. } else if(sdl.sdl_alen == 6) {
  62. memcpy(eui, tmp, 3);
  63. eui[3] = 0xFF;
  64. eui[4] = 0xFE;
  65. memcpy(eui+5, tmp+3, 3);
  66. } else {
  67. return -1;
  68. }
  69. return 0;
  70. }
  71. /* fill sdl with the structure corresponding to ifname.
  72. Warning: make a syscall (and get all interfaces).
  73. return -1 if an error occurs, 0 otherwise. */
  74. static int
  75. get_sdl(struct sockaddr_dl *sdl, char *ifname)
  76. {
  77. int mib[6];
  78. size_t buf_len = 0;
  79. int offset = 0;
  80. char *buffer = NULL;
  81. struct if_msghdr *ifm = NULL;
  82. struct sockaddr_dl *tmp_sdl = NULL;
  83. int rc;
  84. mib[0] = CTL_NET;
  85. mib[1] = PF_ROUTE;
  86. mib[2] = 0;
  87. mib[3] = AF_LINK;
  88. mib[4] = NET_RT_IFLIST;
  89. mib[5] = 0;
  90. rc = sysctl(mib, 6, NULL, &buf_len, NULL, 0);
  91. if(rc < 0)
  92. return -1;
  93. buffer = (char *)malloc(buf_len);
  94. if(buffer == NULL)
  95. return -1;
  96. rc = sysctl(mib, 6, buffer, &buf_len, NULL, 0);
  97. if(rc < 0)
  98. goto fail;
  99. offset = 0;
  100. while(offset < (int) buf_len) {
  101. ifm = (struct if_msghdr *) &buffer[offset];
  102. switch(ifm->ifm_type) {
  103. case RTM_IFINFO:
  104. tmp_sdl = (struct sockaddr_dl *) (ifm + 1);
  105. if(strncmp(ifname, tmp_sdl->sdl_data, tmp_sdl->sdl_nlen) == 0
  106. && strlen(ifname) == tmp_sdl->sdl_nlen) {
  107. memcpy(sdl, tmp_sdl, sizeof(struct sockaddr_dl));
  108. return 0;
  109. }
  110. default:
  111. break;
  112. }
  113. offset += ifm->ifm_msglen;
  114. }
  115. fail:
  116. free(buffer);
  117. return -1;
  118. }
  119. /* KAME said : "Following two macros are highly depending on KAME Release" */
  120. #define IN6_LINKLOCAL_IFINDEX(a) ((a).s6_addr[2] << 8 | (a).s6_addr[3])
  121. #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
  122. do { \
  123. (a).s6_addr[2] = ((i) >> 8) & 0xff; \
  124. (a).s6_addr[3] = (i) & 0xff; \
  125. } while(0)
  126. #if defined(__APPLE__)
  127. #define ROUNDUP(a) \
  128. ((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t))
  129. #else
  130. #define ROUNDUP(a) \
  131. ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
  132. #endif
  133. static int old_forwarding = -1;
  134. static int old_accept_redirects = -1;
  135. static int ifindex_lo = -1;
  136. static int seq;
  137. static int
  138. mask2len(const unsigned char *p, const int size)
  139. {
  140. int i = 0, j;
  141. for(j = 0; j < size; j++, p++) {
  142. if(*p != 0xff)
  143. break;
  144. i += 8;
  145. }
  146. if(j < size) {
  147. switch(*p) {
  148. #define MASKLEN(m, l) case m: do { i += l; break; } while(0)
  149. MASKLEN(0xfe, 7); break;
  150. MASKLEN(0xfc, 6); break;
  151. MASKLEN(0xf8, 5); break;
  152. MASKLEN(0xf0, 4); break;
  153. MASKLEN(0xe0, 3); break;
  154. MASKLEN(0xc0, 2); break;
  155. MASKLEN(0x80, 1); break;
  156. #undef MASKLEN
  157. }
  158. }
  159. return i;
  160. }
  161. static void
  162. plen2mask(int n, struct in6_addr *dest)
  163. {
  164. unsigned char *p;
  165. int i;
  166. static const int pl2m[9] = {
  167. 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
  168. };
  169. memset(dest, 0, sizeof(struct in6_addr));
  170. p = (u_char *)dest;
  171. for(i = 0; i < 16; i++, p++, n -= 8) {
  172. if(n >= 8) {
  173. *p = 0xff;
  174. continue;
  175. }
  176. *p = pl2m[n];
  177. break;
  178. }
  179. return;
  180. }
  181. int
  182. kernel_setup(int setup)
  183. {
  184. int rc = 0;
  185. int forwarding = 1;
  186. int accept_redirects = 0;
  187. int mib[4];
  188. size_t datasize;
  189. if(skip_kernel_setup) return 1;
  190. mib[0] = CTL_NET;
  191. mib[1] = AF_INET6;
  192. seq = time(NULL);
  193. mib[2] = IPPROTO_IPV6;
  194. mib[3] = IPV6CTL_FORWARDING;
  195. datasize = sizeof(old_forwarding);
  196. if(setup) {
  197. rc = sysctl(mib, 4, &old_forwarding, &datasize, NULL, 0);
  198. if(rc == 0 && old_forwarding != forwarding) {
  199. rc = sysctl(mib, 4, &old_forwarding, &datasize,
  200. &forwarding, datasize);
  201. }
  202. }
  203. else if(old_forwarding >= 0 && old_forwarding != forwarding)
  204. rc = sysctl(mib, 4, NULL, NULL,
  205. &old_forwarding, datasize);
  206. if(rc == -1) {
  207. perror("Couldn't tweak forwarding knob.");
  208. return -1;
  209. }
  210. rc = 0;
  211. mib[2] = IPPROTO_ICMPV6;
  212. #if defined(IPV6CTL_SENDREDIRECTS)
  213. mib[3] = IPV6CTL_SENDREDIRECTS;
  214. #else
  215. mib[3] = ICMPV6CTL_REDIRACCEPT;
  216. #endif
  217. datasize = sizeof(old_accept_redirects);
  218. if(setup) {
  219. rc = sysctl(mib, 4, &old_accept_redirects, &datasize, NULL, 0);
  220. if(rc == 0 && old_accept_redirects != accept_redirects) {
  221. rc = sysctl(mib, 4, &old_accept_redirects, &datasize,
  222. &accept_redirects, datasize);
  223. }
  224. } else if(old_accept_redirects >= 0 && old_accept_redirects != accept_redirects)
  225. rc = sysctl(mib, 4, NULL, NULL,
  226. &old_accept_redirects, datasize);
  227. if(rc == -1) {
  228. perror("Couldn't tweak accept_redirects knob.");
  229. return -1;
  230. }
  231. return 1;
  232. }
  233. int
  234. kernel_setup_socket(int setup)
  235. {
  236. int rc;
  237. int zero = 0;
  238. if(setup) {
  239. if(kernel_socket < 0) {
  240. kernel_socket = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
  241. if(kernel_socket < 0)
  242. return -1;
  243. }
  244. rc = setsockopt(kernel_socket, SOL_SOCKET, SO_USELOOPBACK,
  245. &zero, sizeof(zero));
  246. if(rc < 0)
  247. goto error;
  248. return 1;
  249. } else {
  250. close(kernel_socket);
  251. kernel_socket = -1;
  252. return 1;
  253. }
  254. error: {
  255. int savederrno = errno;
  256. perror("setsockopt(kernel_socket)");
  257. close(kernel_socket);
  258. errno = savederrno;
  259. kernel_socket = -1;
  260. return -1;
  261. }
  262. }
  263. int
  264. kernel_setup_interface(int setup, const char *ifname, int ifindex)
  265. {
  266. return 1;
  267. }
  268. int
  269. kernel_interface_operational(const char *ifname, int ifindex)
  270. {
  271. struct ifreq req;
  272. int s, rc;
  273. int flags = link_detect ? (IFF_UP | IFF_RUNNING) : IFF_UP;
  274. s = socket(PF_INET, SOCK_DGRAM, 0);
  275. if(s < 0)
  276. return -1;
  277. memset(&req, 0, sizeof(req));
  278. memset(&req, 0, sizeof(req));
  279. strncpy(req.ifr_name, ifname, sizeof(req.ifr_name));
  280. rc = ioctl(s, SIOCGIFFLAGS, &req);
  281. close(s);
  282. if(rc < 0)
  283. return -1;
  284. return ((req.ifr_flags & flags) == flags);
  285. }
  286. int
  287. kernel_interface_ipv4(const char *ifname, int ifindex, unsigned char *addr_r)
  288. {
  289. struct ifreq req;
  290. int s, rc;
  291. s = socket(PF_INET, SOCK_DGRAM, 0);
  292. if(s < 0)
  293. return -1;
  294. memset(&req, 0, sizeof(req));
  295. strncpy(req.ifr_name, ifname, sizeof(req.ifr_name));
  296. req.ifr_addr.sa_family = AF_INET;
  297. rc = ioctl(s, SIOCGIFADDR, &req);
  298. close(s);
  299. if(rc < 0) {
  300. return -1;
  301. }
  302. memcpy(addr_r, &((struct sockaddr_in*)&req.ifr_addr)->sin_addr, 4);
  303. return 1;
  304. }
  305. int
  306. kernel_interface_mtu(const char *ifname, int ifindex)
  307. {
  308. struct ifreq req;
  309. int s, rc;
  310. s = socket(PF_INET, SOCK_DGRAM, 0);
  311. if(s < 0)
  312. return -1;
  313. memset(&req, 0, sizeof(req));
  314. strncpy(req.ifr_name, ifname, sizeof(req.ifr_name));
  315. rc = ioctl(s, SIOCGIFMTU, &req);
  316. if(rc < 0) {
  317. close(s);
  318. return -1;
  319. }
  320. return req.ifr_mtu;
  321. }
  322. int
  323. kernel_interface_wireless(const char *ifname, int ifindex)
  324. {
  325. struct ifmediareq ifmr;
  326. int s, rc;
  327. s = socket(PF_INET6, SOCK_DGRAM, 0);
  328. memset(&ifmr, 0, sizeof(ifmr));
  329. strncpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
  330. rc = ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr);
  331. close(s);
  332. if(rc < 0)
  333. return rc;
  334. if((ifmr.ifm_active & IFM_NMASK) == IFM_IEEE80211)
  335. return 1;
  336. else
  337. return 0;
  338. }
  339. int
  340. kernel_interface_channel(const char *ifname, int ifindex)
  341. {
  342. errno = ENOSYS;
  343. return -1;
  344. }
  345. int
  346. kernel_disambiguate(int v4)
  347. {
  348. return 0;
  349. }
  350. int
  351. kernel_has_ipv6_subtrees(void)
  352. {
  353. return 0;
  354. }
  355. int
  356. kernel_route(int operation, int table,
  357. const unsigned char *dest, unsigned short plen,
  358. const unsigned char *src, unsigned short src_plen,
  359. const unsigned char *gate, int ifindex, unsigned int metric,
  360. const unsigned char *newgate, int newifindex,
  361. unsigned int newmetric, int newtable)
  362. {
  363. struct {
  364. struct rt_msghdr m_rtm;
  365. char m_space[512];
  366. } msg;
  367. char *data = msg.m_space;
  368. int rc, ipv4;
  369. char local6[1][1][16] = IN6ADDR_LOOPBACK_INIT;
  370. char local4[1][1][16] =
  371. {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  372. 0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x01 }}};
  373. /* Source-specific routes are not implemented yet for BSD. */
  374. if(src_plen > 0) {
  375. errno = ENOSYS;
  376. return -1;
  377. }
  378. /* Check that the protocol family is consistent. */
  379. if(plen >= 96 && v4mapped(dest)) {
  380. if(!v4mapped(gate)) {
  381. errno = EINVAL;
  382. return -1;
  383. }
  384. ipv4 = 1;
  385. } else {
  386. if(v4mapped(gate)) {
  387. errno = EINVAL;
  388. return -1;
  389. }
  390. ipv4 = 0;
  391. }
  392. if(operation == ROUTE_MODIFY && newmetric == metric &&
  393. memcmp(newgate, gate, 16) == 0 && newifindex == ifindex)
  394. return 0;
  395. if(operation == ROUTE_MODIFY) {
  396. /* Avoid atomic route changes that is buggy on OS X. */
  397. kernel_route(ROUTE_FLUSH, table, dest, plen,
  398. src, src_plen,
  399. gate, ifindex, metric,
  400. NULL, 0, 0, 0);
  401. return kernel_route(ROUTE_ADD, table, dest, plen,
  402. src, src_plen,
  403. newgate, newifindex, newmetric,
  404. NULL, 0, 0, 0);
  405. }
  406. kdebugf("kernel_route: %s %s/%d metric %d dev %d nexthop %s\n",
  407. operation == ROUTE_ADD ? "add" :
  408. operation == ROUTE_FLUSH ? "flush" : "change",
  409. format_address(dest), plen, metric, ifindex,
  410. format_address(gate));
  411. if(kernel_socket < 0) kernel_setup_socket(1);
  412. memset(&msg, 0, sizeof(msg));
  413. msg.m_rtm.rtm_version = RTM_VERSION;
  414. switch(operation) {
  415. case ROUTE_FLUSH:
  416. msg.m_rtm.rtm_type = RTM_DELETE; break;
  417. case ROUTE_ADD:
  418. msg.m_rtm.rtm_type = RTM_ADD; break;
  419. case ROUTE_MODIFY:
  420. msg.m_rtm.rtm_type = RTM_CHANGE; break;
  421. default:
  422. return -1;
  423. };
  424. msg.m_rtm.rtm_index = ifindex;
  425. msg.m_rtm.rtm_flags = RTF_UP | RTF_PROTO2;
  426. if(plen == 128) msg.m_rtm.rtm_flags |= RTF_HOST;
  427. if(metric == KERNEL_INFINITY) {
  428. msg.m_rtm.rtm_flags |= RTF_BLACKHOLE;
  429. if(ifindex_lo < 0) {
  430. ifindex_lo = if_nametoindex("lo0");
  431. if(ifindex_lo <= 0)
  432. return -1;
  433. }
  434. msg.m_rtm.rtm_index = ifindex_lo;
  435. }
  436. msg.m_rtm.rtm_seq = ++seq;
  437. msg.m_rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
  438. if(plen != 128) msg.m_rtm.rtm_addrs |= RTA_NETMASK;
  439. #define PUSHEUI(ifindex) \
  440. do { char ifname[IFNAMSIZ]; \
  441. struct sockaddr_dl *sdl = (struct sockaddr_dl*) data; \
  442. if(!if_indextoname((ifindex), ifname)) \
  443. return -1; \
  444. if(get_sdl(sdl, ifname) < 0) \
  445. return -1; \
  446. data = data + ROUNDUP(sdl->sdl_len); \
  447. } while(0)
  448. #define PUSHADDR(src) \
  449. do { struct sockaddr_in *sin = (struct sockaddr_in*) data; \
  450. sin->sin_len = sizeof(struct sockaddr_in); \
  451. sin->sin_family = AF_INET; \
  452. memcpy(&sin->sin_addr, (src) + 12, 4); \
  453. data = data + ROUNDUP(sin->sin_len); \
  454. } while(0)
  455. #define PUSHADDR6(src) \
  456. do { struct sockaddr_in6 *sin6 = (struct sockaddr_in6*) data; \
  457. sin6->sin6_len = sizeof(struct sockaddr_in6); \
  458. sin6->sin6_family = AF_INET6; \
  459. memcpy(&sin6->sin6_addr, (src), 16); \
  460. if(IN6_IS_ADDR_LINKLOCAL (&sin6->sin6_addr)) \
  461. SET_IN6_LINKLOCAL_IFINDEX (sin6->sin6_addr, ifindex); \
  462. data = data + ROUNDUP(sin6->sin6_len); \
  463. } while(0)
  464. /* KAME ipv6 stack does not support IPv4 mapped IPv6, so we have to
  465. * duplicate the codepath */
  466. if(ipv4) {
  467. PUSHADDR(dest);
  468. if(metric == KERNEL_INFINITY) {
  469. PUSHADDR(**local4);
  470. } else if(plen == 128 && memcmp(dest+12, gate+12, 4) == 0) {
  471. #if defined(RTF_CLONING)
  472. msg.m_rtm.rtm_flags |= RTF_CLONING;
  473. #endif
  474. PUSHEUI(ifindex);
  475. } else {
  476. msg.m_rtm.rtm_flags |= RTF_GATEWAY;
  477. PUSHADDR(gate);
  478. }
  479. if((msg.m_rtm.rtm_addrs & RTA_NETMASK) != 0) {
  480. struct in6_addr tmp_sin6_addr;
  481. plen2mask(plen, &tmp_sin6_addr);
  482. PUSHADDR((char *)&tmp_sin6_addr);
  483. }
  484. } else {
  485. PUSHADDR6(dest);
  486. if(metric == KERNEL_INFINITY) {
  487. PUSHADDR6(**local6);
  488. } else {
  489. msg.m_rtm.rtm_flags |= RTF_GATEWAY;
  490. PUSHADDR6(gate);
  491. }
  492. if((msg.m_rtm.rtm_addrs & RTA_NETMASK) != 0) {
  493. struct in6_addr tmp_sin6_addr;
  494. plen2mask(plen, &tmp_sin6_addr);
  495. PUSHADDR6((char*)&tmp_sin6_addr);
  496. }
  497. }
  498. #undef PUSHEUI
  499. #undef PUSHADDR
  500. #undef PUSHADDR6
  501. msg.m_rtm.rtm_msglen = data - (char *)&msg;
  502. rc = write(kernel_socket, (char*)&msg, msg.m_rtm.rtm_msglen);
  503. if(rc < msg.m_rtm.rtm_msglen)
  504. return -1;
  505. return 1;
  506. }
  507. static void
  508. print_kernel_route(int add, struct kernel_route *route)
  509. {
  510. char ifname[IFNAMSIZ];
  511. if(!if_indextoname(route->ifindex, ifname))
  512. memcpy(ifname,"unk",4);
  513. fprintf(stderr,
  514. "%s kernel route: dest: %s gw: %s metric: %d if: %s(%d) \n",
  515. add == RTM_ADD ? "Add" :
  516. add == RTM_DELETE ? "Delete" : "Change",
  517. format_prefix(route->prefix, route->plen),
  518. format_address(route->gw),
  519. route->metric,
  520. ifname, route->ifindex
  521. );
  522. }
  523. static int
  524. parse_kernel_route(const struct rt_msghdr *rtm, struct kernel_route *route)
  525. {
  526. struct sockaddr *sa;
  527. char *rta = (char*)rtm + sizeof(struct rt_msghdr);
  528. uint32_t excluded_flags = 0;
  529. if(ifindex_lo < 0) {
  530. ifindex_lo = if_nametoindex("lo0");
  531. if(ifindex_lo <= 0)
  532. return -1;
  533. }
  534. memset(route, 0, sizeof(*route));
  535. route->metric = 0;
  536. route->ifindex = rtm->rtm_index;
  537. #if defined(RTF_IFSCOPE)
  538. /* Filter out kernel route on OS X */
  539. excluded_flags |= RTF_IFSCOPE;
  540. #endif
  541. #if defined(RTF_MULTICAST)
  542. /* Filter out multicast route on others BSD */
  543. excluded_flags |= RTF_MULTICAST;
  544. #endif
  545. /* Filter out our own route */
  546. excluded_flags |= RTF_PROTO2;
  547. if((rtm->rtm_flags & excluded_flags) != 0)
  548. return -1;
  549. /* Prefix */
  550. if(!(rtm->rtm_addrs & RTA_DST))
  551. return -1;
  552. sa = (struct sockaddr *)rta;
  553. rta += ROUNDUP(sa->sa_len);
  554. if(sa->sa_family == AF_INET6) {
  555. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
  556. memcpy(route->prefix, &sin6->sin6_addr, 16);
  557. if(IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)
  558. || IN6_IS_ADDR_MC_LINKLOCAL(&sin6->sin6_addr))
  559. return -1;
  560. } else if(sa->sa_family == AF_INET) {
  561. struct sockaddr_in *sin = (struct sockaddr_in *)sa;
  562. #if defined(IN_LINKLOCAL)
  563. if(IN_LINKLOCAL(ntohl(sin->sin_addr.s_addr)))
  564. return -1;
  565. #endif
  566. if(IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
  567. return -1;
  568. v4tov6(route->prefix, (unsigned char *)&sin->sin_addr);
  569. } else {
  570. return -1;
  571. }
  572. /* Gateway */
  573. if(!(rtm->rtm_addrs & RTA_GATEWAY))
  574. return -1;
  575. sa = (struct sockaddr *)rta;
  576. rta += ROUNDUP(sa->sa_len);
  577. if(sa->sa_family == AF_INET6) {
  578. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
  579. memcpy(route->gw, &sin6->sin6_addr, 16);
  580. if(IN6_IS_ADDR_LINKLOCAL (&sin6->sin6_addr)) {
  581. route->ifindex = IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr);
  582. SET_IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr, 0);
  583. }
  584. } else if(sa->sa_family == AF_INET) {
  585. struct sockaddr_in *sin = (struct sockaddr_in *)sa;
  586. v4tov6(route->gw, (unsigned char *)&sin->sin_addr);
  587. }
  588. if((int)route->ifindex == ifindex_lo)
  589. return -1;
  590. /* Netmask */
  591. if((rtm->rtm_addrs & RTA_NETMASK) != 0) {
  592. sa = (struct sockaddr *)rta;
  593. rta += ROUNDUP(sa->sa_len);
  594. if(!v4mapped(route->prefix)) {
  595. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
  596. route->plen = mask2len((unsigned char*)&sin6->sin6_addr, 16);
  597. } else {
  598. struct sockaddr_in *sin = (struct sockaddr_in *)sa;
  599. route->plen = mask2len((unsigned char*)&sin->sin_addr, 4);
  600. }
  601. }
  602. if(v4mapped(route->prefix)) route->plen += 96;
  603. if(rtm->rtm_flags & RTF_HOST) route->plen = 128;
  604. return 0;
  605. }
  606. static int
  607. kernel_routes(struct kernel_filter *filter) {
  608. int mib[6];
  609. char *buf, *p;
  610. size_t len;
  611. struct rt_msghdr *rtm;
  612. int rc;
  613. mib[0] = CTL_NET;
  614. mib[1] = PF_ROUTE;
  615. mib[2] = 0;
  616. mib[3] = AF_UNSPEC; /* Address family */
  617. mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
  618. mib[5] = 0; /* No flags */
  619. rc = sysctl(mib, 6, NULL, &len, NULL, 0);
  620. if(rc < 0) {
  621. perror("kernel_routes(len)");
  622. return -1;
  623. }
  624. buf = malloc(len);
  625. if(!buf) {
  626. perror("kernel_routes(malloc)");
  627. return -1;
  628. }
  629. rc = sysctl(mib, 6, buf, &len, NULL, 0);
  630. if(rc < 0) {
  631. perror("kernel_routes(dump)");
  632. goto fail;
  633. }
  634. for(p = buf; p < buf + len; p += rtm->rtm_msglen) {
  635. struct kernel_route route;
  636. rtm = (struct rt_msghdr*)p;
  637. rc = parse_kernel_route(rtm, &route);
  638. if(rc < 0)
  639. continue;
  640. if(debug > 2)
  641. print_kernel_route(1, &route);
  642. rc = filter->route(&route, filter->route_closure);
  643. if(rc < 0)
  644. break;
  645. }
  646. free(buf);
  647. return 0;
  648. fail:
  649. free(buf);
  650. return -1;
  651. }
  652. static int
  653. socket_read(int sock, struct kernel_filter *filter)
  654. {
  655. int rc;
  656. struct {
  657. struct rt_msghdr rtm;
  658. struct sockaddr_storage addr[RTAX_MAX];
  659. } buf;
  660. rc = read(sock, &buf, sizeof(buf));
  661. if(rc <= 0) {
  662. perror("kernel_callback(read)");
  663. return 0;
  664. }
  665. if(buf.rtm.rtm_msglen != rc) {
  666. kdebugf("kernel_callback(length)\n");
  667. return -1;
  668. }
  669. if(buf.rtm.rtm_type == RTM_ADD ||
  670. buf.rtm.rtm_type == RTM_DELETE ||
  671. buf.rtm.rtm_type == RTM_CHANGE) {
  672. struct kernel_route route;
  673. if(buf.rtm.rtm_errno)
  674. return 0;
  675. rc = parse_kernel_route(&buf.rtm, &route);
  676. if(rc < 0)
  677. return 0;
  678. filter->route(&route, filter->route_closure);
  679. if(debug > 2)
  680. print_kernel_route(1,&route);
  681. return 1;
  682. }
  683. return 0;
  684. }
  685. static int
  686. kernel_addresses(struct kernel_filter *filter)
  687. {
  688. struct ifaddrs *ifa, *ifap;
  689. int rc;
  690. rc = getifaddrs(&ifa);
  691. if(rc < 0)
  692. return -1;
  693. for(ifap = ifa; ifap != NULL; ifap = ifap->ifa_next) {
  694. struct kernel_addr addr;
  695. addr.ifindex = if_nametoindex(ifap->ifa_name);
  696. if(!addr.ifindex)
  697. continue;
  698. if(ifap->ifa_addr->sa_family == AF_INET6) {
  699. struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)ifap->ifa_addr;
  700. memcpy(&addr.addr, &sin6->sin6_addr, 16);
  701. if(IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
  702. /* This a perfect example of counter-productive optimisation :
  703. KAME encodes interface index onto bytes 2 and 3, so we have
  704. to reset those bytes to 0 before passing them to babeld. */
  705. memset(((char*)&addr.addr) + 2, 0, 2);
  706. } else if(ifap->ifa_addr->sa_family == AF_INET) {
  707. struct sockaddr_in *sin = (struct sockaddr_in*)ifap->ifa_addr;
  708. #if defined(IN_LINKLOCAL)
  709. if(IN_LINKLOCAL(htonl(sin->sin_addr.s_addr)))
  710. continue;
  711. #endif
  712. v4tov6((void*)&addr.addr, (void*) &sin->sin_addr);
  713. } else {
  714. continue;
  715. }
  716. filter->addr(&addr, filter->addr_closure);
  717. }
  718. freeifaddrs(ifa);
  719. return 0;
  720. }
  721. int
  722. kernel_dump(int operation, struct kernel_filter *filter)
  723. {
  724. switch(operation) {
  725. case CHANGE_ROUTE: return kernel_routes(filter);
  726. case CHANGE_ADDR: return kernel_addresses(filter);
  727. default: break;
  728. }
  729. return -1;
  730. }
  731. int
  732. kernel_callback(struct kernel_filter *filter)
  733. {
  734. if(kernel_socket < 0) kernel_setup_socket(1);
  735. kdebugf("Reading kernel table modification.");
  736. socket_read(kernel_socket, filter);
  737. return 0;
  738. }
  739. int
  740. add_rule(int prio, const unsigned char *src_prefix, int src_plen, int table)
  741. {
  742. errno = ENOSYS;
  743. return -1;
  744. }
  745. int
  746. flush_rule(int prio, int family)
  747. {
  748. errno = ENOSYS;
  749. return -1;
  750. }
  751. int
  752. change_rule(int new_prio, int old_prio,
  753. const unsigned char *src, int plen, int table)
  754. {
  755. errno = ENOSYS;
  756. return -1;
  757. }
  758. /* Local Variables: */
  759. /* c-basic-offset: 4 */
  760. /* indent-tabs-mode: nil */
  761. /* End: */