totemip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * Copyright (c) 2005-2011 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Patrick Caulfield (pcaulfie@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /* IPv4/6 abstraction */
  35. #include <config.h>
  36. #include <sys/ioctl.h>
  37. #include <sys/types.h>
  38. #include <sys/socket.h>
  39. #include <arpa/inet.h>
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include <netdb.h>
  43. #if defined(COROSYNC_SOLARIS)
  44. #include <net/if.h>
  45. #include <sys/sockio.h>
  46. #endif
  47. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  48. #include <sys/sockio.h>
  49. #include <net/if.h>
  50. #include <net/if_var.h>
  51. #include <netinet/in_var.h>
  52. #include <netinet/in.h>
  53. #include <ifaddrs.h>
  54. #endif
  55. #include <string.h>
  56. #include <stdio.h>
  57. #include <errno.h>
  58. #include <assert.h>
  59. #include <stdlib.h>
  60. #include <unistd.h>
  61. #if defined(COROSYNC_LINUX)
  62. #include <net/if.h>
  63. #include <asm/types.h>
  64. #include <linux/rtnetlink.h>
  65. #endif
  66. #ifdef HAVE_GETIFADDRS
  67. #include <ifaddrs.h>
  68. #endif
  69. #include <corosync/totem/totemip.h>
  70. #include <corosync/swab.h>
  71. #define LOCALHOST_IPV4 "127.0.0.1"
  72. #define LOCALHOST_IPV6 "::1"
  73. #define NETLINK_BUFSIZE 16384
  74. #ifdef SO_NOSIGPIPE
  75. void totemip_nosigpipe(int s)
  76. {
  77. int on = 1;
  78. setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
  79. }
  80. #endif
  81. /* Compare two addresses */
  82. int totemip_equal(const struct totem_ip_address *addr1,
  83. const struct totem_ip_address *addr2)
  84. {
  85. int addrlen = 0;
  86. if (addr1->family != addr2->family)
  87. return 0;
  88. if (addr1->family == AF_INET) {
  89. addrlen = sizeof(struct in_addr);
  90. }
  91. if (addr1->family == AF_INET6) {
  92. addrlen = sizeof(struct in6_addr);
  93. }
  94. assert(addrlen);
  95. if (memcmp(addr1->addr, addr2->addr, addrlen) == 0)
  96. return 1;
  97. else
  98. return 0;
  99. }
  100. /* Copy a totem_ip_address */
  101. void totemip_copy(struct totem_ip_address *addr1,
  102. const struct totem_ip_address *addr2)
  103. {
  104. memcpy(addr1, addr2, sizeof(struct totem_ip_address));
  105. }
  106. void totemip_copy_endian_convert(struct totem_ip_address *addr1,
  107. const struct totem_ip_address *addr2)
  108. {
  109. addr1->nodeid = swab32(addr2->nodeid);
  110. addr1->family = swab16(addr2->family);
  111. memcpy(addr1->addr, addr2->addr, TOTEMIP_ADDRLEN);
  112. }
  113. /*
  114. * Multicast address range is 224.0.0.0 to 239.255.255.255 this
  115. * translates to the first 4 bits == 1110 (0xE).
  116. * http://en.wikipedia.org/wiki/Multicast_address
  117. */
  118. int32_t totemip_is_mcast(struct totem_ip_address *ip_addr)
  119. {
  120. uint32_t addr = 0;
  121. memcpy (&addr, ip_addr->addr, sizeof (uint32_t));
  122. if (ip_addr->family == AF_INET) {
  123. addr = ntohl(addr);
  124. if ((addr >> 28) != 0xE) {
  125. return -1;
  126. }
  127. }
  128. return 0;
  129. }
  130. /* For sorting etc. params are void * for qsort's benefit */
  131. int totemip_compare(const void *a, const void *b)
  132. {
  133. int i;
  134. const struct totem_ip_address *totemip_a = (const struct totem_ip_address *)a;
  135. const struct totem_ip_address *totemip_b = (const struct totem_ip_address *)b;
  136. struct in_addr ipv4_a1;
  137. struct in_addr ipv4_a2;
  138. struct in6_addr ipv6_a1;
  139. struct in6_addr ipv6_a2;
  140. unsigned short family;
  141. /*
  142. * Use memcpy to align since totem_ip_address is unaligned on various archs
  143. */
  144. memcpy (&family, &totemip_a->family, sizeof (unsigned short));
  145. if (family == AF_INET) {
  146. memcpy (&ipv4_a1, totemip_a->addr, sizeof (struct in_addr));
  147. memcpy (&ipv4_a2, totemip_b->addr, sizeof (struct in_addr));
  148. if (ipv4_a1.s_addr == ipv4_a2.s_addr) {
  149. return (0);
  150. }
  151. if (htonl(ipv4_a1.s_addr) < htonl(ipv4_a2.s_addr)) {
  152. return -1;
  153. } else {
  154. return +1;
  155. }
  156. } else
  157. if (family == AF_INET6) {
  158. /*
  159. * We can only compare 8 bits at time for portability reasons
  160. */
  161. memcpy (&ipv6_a1, totemip_a->addr, sizeof (struct in6_addr));
  162. memcpy (&ipv6_a2, totemip_b->addr, sizeof (struct in6_addr));
  163. for (i = 0; i < 16; i++) {
  164. int res = ipv6_a1.s6_addr[i] -
  165. ipv6_a2.s6_addr[i];
  166. if (res) {
  167. return res;
  168. }
  169. }
  170. return 0;
  171. } else {
  172. /*
  173. * Family not set, should be!
  174. */
  175. assert (0);
  176. }
  177. return 0;
  178. }
  179. /* Build a localhost totem_ip_address */
  180. int totemip_localhost(int family, struct totem_ip_address *localhost)
  181. {
  182. const char *addr_text;
  183. memset (localhost, 0, sizeof (struct totem_ip_address));
  184. if (family == AF_INET) {
  185. addr_text = LOCALHOST_IPV4;
  186. if (inet_pton(family, addr_text, (char *)&localhost->nodeid) <= 0) {
  187. return -1;
  188. }
  189. } else {
  190. addr_text = LOCALHOST_IPV6;
  191. }
  192. if (inet_pton(family, addr_text, (char *)localhost->addr) <= 0)
  193. return -1;
  194. localhost->family = family;
  195. return 0;
  196. }
  197. int totemip_localhost_check(const struct totem_ip_address *addr)
  198. {
  199. struct totem_ip_address localhost;
  200. if (totemip_localhost(addr->family, &localhost))
  201. return 0;
  202. return totemip_equal(addr, &localhost);
  203. }
  204. const char *totemip_print(const struct totem_ip_address *addr)
  205. {
  206. static char buf[INET6_ADDRSTRLEN];
  207. return (inet_ntop(addr->family, addr->addr, buf, sizeof(buf)));
  208. }
  209. /* Make a totem_ip_address into a usable sockaddr_storage */
  210. int totemip_totemip_to_sockaddr_convert(struct totem_ip_address *ip_addr,
  211. uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
  212. {
  213. int ret = -1;
  214. if (ip_addr->family == AF_INET) {
  215. struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
  216. memset(sin, 0, sizeof(struct sockaddr_in));
  217. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  218. sin->sin_len = sizeof(struct sockaddr_in);
  219. #endif
  220. sin->sin_family = ip_addr->family;
  221. sin->sin_port = ntohs(port);
  222. memcpy(&sin->sin_addr, ip_addr->addr, sizeof(struct in_addr));
  223. *addrlen = sizeof(struct sockaddr_in);
  224. ret = 0;
  225. }
  226. if (ip_addr->family == AF_INET6) {
  227. struct sockaddr_in6 *sin = (struct sockaddr_in6 *)saddr;
  228. memset(sin, 0, sizeof(struct sockaddr_in6));
  229. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  230. sin->sin6_len = sizeof(struct sockaddr_in6);
  231. #endif
  232. sin->sin6_family = ip_addr->family;
  233. sin->sin6_port = ntohs(port);
  234. sin->sin6_scope_id = 2;
  235. memcpy(&sin->sin6_addr, ip_addr->addr, sizeof(struct in6_addr));
  236. *addrlen = sizeof(struct sockaddr_in6);
  237. ret = 0;
  238. }
  239. return ret;
  240. }
  241. /* Converts an address string string into a totem_ip_address.
  242. family can be AF_INET, AF_INET6 or 0 ("for "don't care")
  243. */
  244. int totemip_parse(struct totem_ip_address *totemip, const char *addr, int family)
  245. {
  246. struct addrinfo *ainfo;
  247. struct addrinfo ahints;
  248. struct sockaddr_in *sa;
  249. struct sockaddr_in6 *sa6;
  250. int ret;
  251. memset(&ahints, 0, sizeof(ahints));
  252. ahints.ai_socktype = SOCK_DGRAM;
  253. ahints.ai_protocol = IPPROTO_UDP;
  254. ahints.ai_family = family;
  255. /* Lookup the nodename address */
  256. ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
  257. if (ret)
  258. return -1;
  259. sa = (struct sockaddr_in *)ainfo->ai_addr;
  260. sa6 = (struct sockaddr_in6 *)ainfo->ai_addr;
  261. totemip->family = ainfo->ai_family;
  262. if (ainfo->ai_family == AF_INET)
  263. memcpy(totemip->addr, &sa->sin_addr, sizeof(struct in_addr));
  264. else
  265. memcpy(totemip->addr, &sa6->sin6_addr, sizeof(struct in6_addr));
  266. freeaddrinfo(ainfo);
  267. return 0;
  268. }
  269. /* Make a sockaddr_* into a totem_ip_address */
  270. int totemip_sockaddr_to_totemip_convert(const struct sockaddr_storage *saddr,
  271. struct totem_ip_address *ip_addr)
  272. {
  273. int ret = -1;
  274. ip_addr->family = saddr->ss_family;
  275. ip_addr->nodeid = 0;
  276. if (saddr->ss_family == AF_INET) {
  277. const struct sockaddr_in *sin = (const struct sockaddr_in *)saddr;
  278. memcpy(ip_addr->addr, &sin->sin_addr, sizeof(struct in_addr));
  279. ret = 0;
  280. }
  281. if (saddr->ss_family == AF_INET6) {
  282. const struct sockaddr_in6 *sin
  283. = (const struct sockaddr_in6 *)saddr;
  284. memcpy(ip_addr->addr, &sin->sin6_addr, sizeof(struct in6_addr));
  285. ret = 0;
  286. }
  287. return ret;
  288. }
  289. #if defined(HAVE_GETIFADDRS)
  290. int totemip_getifaddrs(struct list_head *addrs)
  291. {
  292. struct ifaddrs *ifap, *ifa;
  293. struct totem_ip_if_address *if_addr;
  294. if (getifaddrs(&ifap) != 0)
  295. return (-1);
  296. list_init(addrs);
  297. for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
  298. if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
  299. continue ;
  300. if ((ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) ||
  301. (ifa->ifa_netmask->sa_family != AF_INET && ifa->ifa_netmask->sa_family != AF_INET6))
  302. continue ;
  303. if_addr = malloc(sizeof(struct totem_ip_if_address));
  304. if (if_addr == NULL) {
  305. goto error_free_ifaddrs;
  306. }
  307. list_init(&if_addr->list);
  308. memset(if_addr, 0, sizeof(struct totem_ip_if_address));
  309. if_addr->interface_up = ifa->ifa_flags & IFF_UP;
  310. if_addr->interface_num = if_nametoindex(ifa->ifa_name);
  311. if_addr->name = strdup(ifa->ifa_name);
  312. if (if_addr->name == NULL) {
  313. goto error_free_addr;
  314. }
  315. if (totemip_sockaddr_to_totemip_convert((const struct sockaddr_storage *)ifa->ifa_addr,
  316. &if_addr->ip_addr) == -1) {
  317. goto error_free_addr_name;
  318. }
  319. if (totemip_sockaddr_to_totemip_convert((const struct sockaddr_storage *)ifa->ifa_netmask,
  320. &if_addr->mask_addr) == -1) {
  321. goto error_free_addr_name;
  322. }
  323. list_add(&if_addr->list, addrs);
  324. }
  325. freeifaddrs(ifap);
  326. return (0);
  327. error_free_addr_name:
  328. free(if_addr->name);
  329. error_free_addr:
  330. free(if_addr);
  331. error_free_ifaddrs:
  332. totemip_freeifaddrs(addrs);
  333. freeifaddrs(ifap);
  334. return (-1);
  335. }
  336. #elif defined(SOLARIS)
  337. /*
  338. * On Solaris, man if_tcp describes this method
  339. */
  340. int totemip_getifaddrs(struct list_head *addrs)
  341. {
  342. int id_fd;
  343. int numreqs;
  344. int res;
  345. struct lifconf lifconf;
  346. struct lifreq *lifreq;
  347. int i, j;
  348. struct totem_ip_if_address *if_addr;
  349. short family;
  350. list_init(addrs);
  351. for (family = 0; family < 2; family++) {
  352. numreqs = 0;
  353. res = -1;
  354. lifconf.lifc_family = (family == 0 ? AF_INET : AF_INET6);
  355. id_fd = socket (lifconf.lifc_family, SOCK_STREAM, 0);
  356. lifconf.lifc_flags = 0;
  357. lifconf.lifc_buf = NULL;
  358. lifconf.lifc_len = 0;
  359. do {
  360. numreqs += 32;
  361. lifconf.lifc_len = sizeof (struct lifreq) * numreqs;
  362. lifconf.lifc_buf = (void *)realloc(lifconf.lifc_buf, lifconf.lifc_len);
  363. res = ioctl (id_fd, SIOCGLIFCONF, &lifconf);
  364. if (res < 0) {
  365. close (id_fd);
  366. }
  367. } while (res >= 0 && lifconf.lifc_len == sizeof (struct lifconf) * numreqs);
  368. if (res < 0)
  369. continue ;
  370. lifreq = (struct lifreq *)lifconf.lifc_buf;
  371. for (i = 0; i < lifconf.lifc_len / sizeof (struct lifreq); i++) {
  372. if (ioctl(id_fd, SIOCGLIFADDR, &lifreq[i]) < 0)
  373. continue ;
  374. if (lifreq[i].lifr_addr.ss_family != AF_INET && lifreq[i].lifr_addr.ss_family != AF_INET6)
  375. continue ;
  376. if_addr = malloc(sizeof(struct totem_ip_if_address));
  377. if (if_addr == NULL) {
  378. goto error_free_ifaddrs;
  379. }
  380. memset(if_addr, 0, sizeof(struct totem_ip_if_address));
  381. if_addr->name = strdup(lifreq[i].lifr_name);
  382. if (if_addr->name == NULL) {
  383. goto error_free_addr;
  384. }
  385. if (totemip_sockaddr_to_totemip_convert(&lifreq[i].lifr_addr, &if_addr->ip_addr) == -1) {
  386. goto error_free_addr_name;
  387. }
  388. if (ioctl(id_fd, SIOCGLIFSUBNET, &lifreq[i]) < 0)
  389. continue ;
  390. /*
  391. * lifreq doesn't contain mask address. But It length of prefix in bits,
  392. * so we can generate mask.
  393. */
  394. memset(&if_addr->mask_addr, 0, sizeof(if_addr->mask_addr));
  395. if_addr->mask_addr.family = if_addr->ip_addr.family;
  396. for (j = 0; j < lifreq[i].lifr_addrlen; j++) {
  397. if_addr->mask_addr.addr[j / 8] |= 1 << (7 - (j % 8));
  398. }
  399. if (ioctl(id_fd, SIOCGLIFFLAGS, &lifreq[i]) >= 0) {
  400. if_addr->interface_up = lifreq[i].lifr_flags & IFF_UP;
  401. }
  402. if (ioctl(id_fd, SIOCGLIFINDEX, &lifreq[i]) >= 0) {
  403. if_addr->interface_num = lifreq[i].lifr_index;
  404. }
  405. list_add(&if_addr->list, addrs);
  406. }
  407. free (lifconf.lifc_buf);
  408. close (id_fd);
  409. }
  410. return (0);
  411. error_free_addr_name:
  412. free(if_addr->name);
  413. error_free_addr:
  414. free(if_addr);
  415. error_free_ifaddrs:
  416. totemip_freeifaddrs(addrs);
  417. free (lifconf.lifc_buf);
  418. close (id_fd);
  419. return (-1);
  420. }
  421. #endif /* HAVE_GETIFADDRS */
  422. void totemip_freeifaddrs(struct list_head *addrs)
  423. {
  424. struct totem_ip_if_address *if_addr;
  425. struct list_head *list;
  426. for (list = addrs->next; list != addrs;) {
  427. if_addr = list_entry(list, struct totem_ip_if_address, list);
  428. list = list->next;
  429. free(if_addr->name);
  430. list_del(&if_addr->list);
  431. free(if_addr);
  432. }
  433. list_init(addrs);
  434. }
  435. int totemip_iface_check(struct totem_ip_address *bindnet,
  436. struct totem_ip_address *boundto,
  437. int *interface_up,
  438. int *interface_num,
  439. int mask_high_bit)
  440. {
  441. struct list_head addrs;
  442. struct list_head *list;
  443. struct totem_ip_if_address *if_addr;
  444. struct totem_ip_address bn_netaddr, if_netaddr;
  445. socklen_t addr_len;
  446. socklen_t si;
  447. int res = -1;
  448. if (totemip_getifaddrs(&addrs) == -1) {
  449. return (-1);
  450. }
  451. for (list = addrs.next; list != &addrs; list = list->next) {
  452. if_addr = list_entry(list, struct totem_ip_if_address, list);
  453. if (bindnet->family != if_addr->ip_addr.family)
  454. continue ;
  455. addr_len = 0;
  456. switch (bindnet->family) {
  457. case AF_INET:
  458. addr_len = sizeof(struct in_addr);
  459. break;
  460. case AF_INET6:
  461. addr_len = sizeof(struct in6_addr);
  462. break;
  463. }
  464. if (addr_len == 0)
  465. continue ;
  466. totemip_copy(&bn_netaddr, bindnet);
  467. totemip_copy(&if_netaddr, &if_addr->ip_addr);
  468. for (si = 0; si < addr_len; si++) {
  469. bn_netaddr.addr[si] = bn_netaddr.addr[si] & if_addr->mask_addr.addr[si];
  470. if_netaddr.addr[si] = if_netaddr.addr[si] & if_addr->mask_addr.addr[si];
  471. }
  472. if (totemip_equal(&bn_netaddr, &if_netaddr)) {
  473. totemip_copy(boundto, &if_addr->ip_addr);
  474. boundto->nodeid = bindnet->nodeid;
  475. *interface_up = if_addr->interface_up;
  476. *interface_num = if_addr->interface_num;
  477. if (boundto->family == AF_INET && boundto->nodeid == 0) {
  478. unsigned int nodeid = 0;
  479. memcpy (&nodeid, boundto->addr, sizeof (int));
  480. #if __BYTE_ORDER == __BIG_ENDIAN
  481. nodeid = swab32 (nodeid);
  482. #endif
  483. if (mask_high_bit) {
  484. nodeid &= 0x7FFFFFFF;
  485. }
  486. boundto->nodeid = nodeid;
  487. }
  488. res = 0;
  489. goto finished;
  490. }
  491. }
  492. finished:
  493. totemip_freeifaddrs(&addrs);
  494. return (res);
  495. }