totemip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. #include <net/if.h>
  44. #include <string.h>
  45. #include <stdio.h>
  46. #include <errno.h>
  47. #include <assert.h>
  48. #include <stdlib.h>
  49. #include <unistd.h>
  50. #ifdef HAVE_SYS_SOCKIO_H
  51. #include <sys/sockio.h>
  52. #endif
  53. #ifdef HAVE_NET_IF_VAR_H
  54. #include <net/if_var.h>
  55. #endif
  56. #ifdef HAVE_NETINET_IN_VAR_H
  57. #include <netinet/in_var.h>
  58. #endif
  59. #ifdef HAVE_NETINET_IN_H
  60. #include <netinet/in.h>
  61. #endif
  62. #ifdef HAVE_IFADDRS_H
  63. #include <ifaddrs.h>
  64. #endif
  65. #include <corosync/totem/totemip.h>
  66. #include <corosync/swab.h>
  67. #define LOCALHOST_IPV4 "127.0.0.1"
  68. #define LOCALHOST_IPV6 "::1"
  69. #define NETLINK_BUFSIZE 16384
  70. #ifdef SO_NOSIGPIPE
  71. void totemip_nosigpipe(int s)
  72. {
  73. int on = 1;
  74. setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
  75. }
  76. #endif
  77. /* Compare two addresses */
  78. int totemip_equal(const struct totem_ip_address *addr1,
  79. const struct totem_ip_address *addr2)
  80. {
  81. int addrlen = 0;
  82. if (addr1->family != addr2->family)
  83. return 0;
  84. if (addr1->family == AF_INET) {
  85. addrlen = sizeof(struct in_addr);
  86. }
  87. if (addr1->family == AF_INET6) {
  88. addrlen = sizeof(struct in6_addr);
  89. }
  90. assert(addrlen);
  91. if (memcmp(addr1->addr, addr2->addr, addrlen) == 0)
  92. return 1;
  93. else
  94. return 0;
  95. }
  96. /* Copy a totem_ip_address */
  97. void totemip_copy(struct totem_ip_address *addr1,
  98. const struct totem_ip_address *addr2)
  99. {
  100. memcpy(addr1, addr2, sizeof(struct totem_ip_address));
  101. }
  102. void totemip_copy_endian_convert(struct totem_ip_address *addr1,
  103. const struct totem_ip_address *addr2)
  104. {
  105. addr1->nodeid = swab32(addr2->nodeid);
  106. addr1->family = swab16(addr2->family);
  107. memcpy(addr1->addr, addr2->addr, TOTEMIP_ADDRLEN);
  108. }
  109. /*
  110. * Multicast address range is 224.0.0.0 to 239.255.255.255 this
  111. * translates to the first 4 bits == 1110 (0xE).
  112. * http://en.wikipedia.org/wiki/Multicast_address
  113. */
  114. int32_t totemip_is_mcast(struct totem_ip_address *ip_addr)
  115. {
  116. uint32_t addr = 0;
  117. memcpy (&addr, ip_addr->addr, sizeof (uint32_t));
  118. if (ip_addr->family == AF_INET) {
  119. addr = ntohl(addr);
  120. if ((addr >> 28) != 0xE) {
  121. return -1;
  122. }
  123. }
  124. return 0;
  125. }
  126. /* For sorting etc. params are void * for qsort's benefit */
  127. int totemip_compare(const void *a, const void *b)
  128. {
  129. int i;
  130. const struct totem_ip_address *totemip_a = (const struct totem_ip_address *)a;
  131. const struct totem_ip_address *totemip_b = (const struct totem_ip_address *)b;
  132. struct in_addr ipv4_a1;
  133. struct in_addr ipv4_a2;
  134. struct in6_addr ipv6_a1;
  135. struct in6_addr ipv6_a2;
  136. unsigned short family;
  137. /*
  138. * Use memcpy to align since totem_ip_address is unaligned on various archs
  139. */
  140. memcpy (&family, &totemip_a->family, sizeof (unsigned short));
  141. if (family == AF_INET) {
  142. memcpy (&ipv4_a1, totemip_a->addr, sizeof (struct in_addr));
  143. memcpy (&ipv4_a2, totemip_b->addr, sizeof (struct in_addr));
  144. if (ipv4_a1.s_addr == ipv4_a2.s_addr) {
  145. return (0);
  146. }
  147. if (htonl(ipv4_a1.s_addr) < htonl(ipv4_a2.s_addr)) {
  148. return -1;
  149. } else {
  150. return +1;
  151. }
  152. } else
  153. if (family == AF_INET6) {
  154. /*
  155. * We can only compare 8 bits at time for portability reasons
  156. */
  157. memcpy (&ipv6_a1, totemip_a->addr, sizeof (struct in6_addr));
  158. memcpy (&ipv6_a2, totemip_b->addr, sizeof (struct in6_addr));
  159. for (i = 0; i < 16; i++) {
  160. int res = ipv6_a1.s6_addr[i] -
  161. ipv6_a2.s6_addr[i];
  162. if (res) {
  163. return res;
  164. }
  165. }
  166. return 0;
  167. } else {
  168. /*
  169. * Family not set, should be!
  170. */
  171. assert (0);
  172. }
  173. return 0;
  174. }
  175. /* Build a localhost totem_ip_address */
  176. int totemip_localhost(int family, struct totem_ip_address *localhost)
  177. {
  178. const char *addr_text;
  179. memset (localhost, 0, sizeof (struct totem_ip_address));
  180. if (family == AF_INET) {
  181. addr_text = LOCALHOST_IPV4;
  182. if (inet_pton(family, addr_text, (char *)&localhost->nodeid) <= 0) {
  183. return -1;
  184. }
  185. } else {
  186. addr_text = LOCALHOST_IPV6;
  187. }
  188. if (inet_pton(family, addr_text, (char *)localhost->addr) <= 0)
  189. return -1;
  190. localhost->family = family;
  191. return 0;
  192. }
  193. int totemip_localhost_check(const struct totem_ip_address *addr)
  194. {
  195. struct totem_ip_address localhost;
  196. if (totemip_localhost(addr->family, &localhost))
  197. return 0;
  198. return totemip_equal(addr, &localhost);
  199. }
  200. const char *totemip_print(const struct totem_ip_address *addr)
  201. {
  202. static char buf[INET6_ADDRSTRLEN];
  203. return (inet_ntop(addr->family, addr->addr, buf, sizeof(buf)));
  204. }
  205. /* Make a totem_ip_address into a usable sockaddr_storage */
  206. int totemip_totemip_to_sockaddr_convert(struct totem_ip_address *ip_addr,
  207. uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
  208. {
  209. int ret = -1;
  210. if (ip_addr->family == AF_INET) {
  211. struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
  212. memset(sin, 0, sizeof(struct sockaddr_in));
  213. #ifdef HAVE_SOCK_SIN_LEN
  214. sin->sin_len = sizeof(struct sockaddr_in);
  215. #endif
  216. sin->sin_family = ip_addr->family;
  217. sin->sin_port = ntohs(port);
  218. memcpy(&sin->sin_addr, ip_addr->addr, sizeof(struct in_addr));
  219. *addrlen = sizeof(struct sockaddr_in);
  220. ret = 0;
  221. }
  222. if (ip_addr->family == AF_INET6) {
  223. struct sockaddr_in6 *sin = (struct sockaddr_in6 *)saddr;
  224. memset(sin, 0, sizeof(struct sockaddr_in6));
  225. #ifdef HAVE_SOCK_SIN6_LEN
  226. sin->sin6_len = sizeof(struct sockaddr_in6);
  227. #endif
  228. sin->sin6_family = ip_addr->family;
  229. sin->sin6_port = ntohs(port);
  230. sin->sin6_scope_id = 2;
  231. memcpy(&sin->sin6_addr, ip_addr->addr, sizeof(struct in6_addr));
  232. *addrlen = sizeof(struct sockaddr_in6);
  233. ret = 0;
  234. }
  235. return ret;
  236. }
  237. /* Converts an address string string into a totem_ip_address.
  238. family can be AF_INET, AF_INET6 or 0 ("for "don't care")
  239. */
  240. int totemip_parse(struct totem_ip_address *totemip, const char *addr, int family)
  241. {
  242. struct addrinfo *ainfo;
  243. struct addrinfo ahints;
  244. struct sockaddr_in *sa;
  245. struct sockaddr_in6 *sa6;
  246. int ret;
  247. memset(&ahints, 0, sizeof(ahints));
  248. ahints.ai_socktype = SOCK_DGRAM;
  249. ahints.ai_protocol = IPPROTO_UDP;
  250. ahints.ai_family = family;
  251. /* Lookup the nodename address */
  252. ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
  253. if (ret)
  254. return -1;
  255. sa = (struct sockaddr_in *)ainfo->ai_addr;
  256. sa6 = (struct sockaddr_in6 *)ainfo->ai_addr;
  257. totemip->family = ainfo->ai_family;
  258. if (ainfo->ai_family == AF_INET)
  259. memcpy(totemip->addr, &sa->sin_addr, sizeof(struct in_addr));
  260. else
  261. memcpy(totemip->addr, &sa6->sin6_addr, sizeof(struct in6_addr));
  262. freeaddrinfo(ainfo);
  263. return 0;
  264. }
  265. /* Make a sockaddr_* into a totem_ip_address */
  266. int totemip_sockaddr_to_totemip_convert(const struct sockaddr_storage *saddr,
  267. struct totem_ip_address *ip_addr)
  268. {
  269. int ret = -1;
  270. ip_addr->family = saddr->ss_family;
  271. ip_addr->nodeid = 0;
  272. if (saddr->ss_family == AF_INET) {
  273. const struct sockaddr_in *sin = (const struct sockaddr_in *)saddr;
  274. memcpy(ip_addr->addr, &sin->sin_addr, sizeof(struct in_addr));
  275. ret = 0;
  276. }
  277. if (saddr->ss_family == AF_INET6) {
  278. const struct sockaddr_in6 *sin
  279. = (const struct sockaddr_in6 *)saddr;
  280. memcpy(ip_addr->addr, &sin->sin6_addr, sizeof(struct in6_addr));
  281. ret = 0;
  282. }
  283. return ret;
  284. }
  285. #if defined(HAVE_GETIFADDRS)
  286. int totemip_getifaddrs(struct list_head *addrs)
  287. {
  288. struct ifaddrs *ifap, *ifa;
  289. struct totem_ip_if_address *if_addr;
  290. if (getifaddrs(&ifap) != 0)
  291. return (-1);
  292. list_init(addrs);
  293. for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
  294. if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
  295. continue ;
  296. if ((ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) ||
  297. (ifa->ifa_netmask->sa_family != AF_INET && ifa->ifa_netmask->sa_family != AF_INET6))
  298. continue ;
  299. if_addr = malloc(sizeof(struct totem_ip_if_address));
  300. if (if_addr == NULL) {
  301. goto error_free_ifaddrs;
  302. }
  303. list_init(&if_addr->list);
  304. memset(if_addr, 0, sizeof(struct totem_ip_if_address));
  305. if_addr->interface_up = ifa->ifa_flags & IFF_UP;
  306. if_addr->interface_num = if_nametoindex(ifa->ifa_name);
  307. if_addr->name = strdup(ifa->ifa_name);
  308. if (if_addr->name == NULL) {
  309. goto error_free_addr;
  310. }
  311. if (totemip_sockaddr_to_totemip_convert((const struct sockaddr_storage *)ifa->ifa_addr,
  312. &if_addr->ip_addr) == -1) {
  313. goto error_free_addr_name;
  314. }
  315. if (totemip_sockaddr_to_totemip_convert((const struct sockaddr_storage *)ifa->ifa_netmask,
  316. &if_addr->mask_addr) == -1) {
  317. goto error_free_addr_name;
  318. }
  319. list_add_tail(&if_addr->list, addrs);
  320. }
  321. freeifaddrs(ifap);
  322. return (0);
  323. error_free_addr_name:
  324. free(if_addr->name);
  325. error_free_addr:
  326. free(if_addr);
  327. error_free_ifaddrs:
  328. totemip_freeifaddrs(addrs);
  329. freeifaddrs(ifap);
  330. return (-1);
  331. }
  332. #elif defined(SOLARIS)
  333. /*
  334. * On Solaris, man if_tcp describes this method
  335. */
  336. int totemip_getifaddrs(struct list_head *addrs)
  337. {
  338. int id_fd;
  339. int numreqs;
  340. int res;
  341. struct lifconf lifconf;
  342. struct lifreq *lifreq;
  343. int i, j;
  344. struct totem_ip_if_address *if_addr;
  345. short family;
  346. list_init(addrs);
  347. for (family = 0; family < 2; family++) {
  348. numreqs = 0;
  349. res = -1;
  350. lifconf.lifc_family = (family == 0 ? AF_INET : AF_INET6);
  351. id_fd = socket (lifconf.lifc_family, SOCK_STREAM, 0);
  352. lifconf.lifc_flags = 0;
  353. lifconf.lifc_buf = NULL;
  354. lifconf.lifc_len = 0;
  355. do {
  356. numreqs += 32;
  357. lifconf.lifc_len = sizeof (struct lifreq) * numreqs;
  358. lifconf.lifc_buf = (void *)realloc(lifconf.lifc_buf, lifconf.lifc_len);
  359. res = ioctl (id_fd, SIOCGLIFCONF, &lifconf);
  360. if (res < 0) {
  361. close (id_fd);
  362. }
  363. } while (res >= 0 && lifconf.lifc_len == sizeof (struct lifconf) * numreqs);
  364. if (res < 0)
  365. continue ;
  366. lifreq = (struct lifreq *)lifconf.lifc_buf;
  367. for (i = 0; i < lifconf.lifc_len / sizeof (struct lifreq); i++) {
  368. if (ioctl(id_fd, SIOCGLIFADDR, &lifreq[i]) < 0)
  369. continue ;
  370. if (lifreq[i].lifr_addr.ss_family != AF_INET && lifreq[i].lifr_addr.ss_family != AF_INET6)
  371. continue ;
  372. if_addr = malloc(sizeof(struct totem_ip_if_address));
  373. if (if_addr == NULL) {
  374. goto error_free_ifaddrs;
  375. }
  376. memset(if_addr, 0, sizeof(struct totem_ip_if_address));
  377. if_addr->name = strdup(lifreq[i].lifr_name);
  378. if (if_addr->name == NULL) {
  379. goto error_free_addr;
  380. }
  381. if (totemip_sockaddr_to_totemip_convert(&lifreq[i].lifr_addr, &if_addr->ip_addr) == -1) {
  382. goto error_free_addr_name;
  383. }
  384. if (ioctl(id_fd, SIOCGLIFSUBNET, &lifreq[i]) < 0)
  385. continue ;
  386. /*
  387. * lifreq doesn't contain mask address. But It length of prefix in bits,
  388. * so we can generate mask.
  389. */
  390. memset(&if_addr->mask_addr, 0, sizeof(if_addr->mask_addr));
  391. if_addr->mask_addr.family = if_addr->ip_addr.family;
  392. for (j = 0; j < lifreq[i].lifr_addrlen; j++) {
  393. if_addr->mask_addr.addr[j / 8] |= 1 << (7 - (j % 8));
  394. }
  395. if (ioctl(id_fd, SIOCGLIFFLAGS, &lifreq[i]) >= 0) {
  396. if_addr->interface_up = lifreq[i].lifr_flags & IFF_UP;
  397. }
  398. if (ioctl(id_fd, SIOCGLIFINDEX, &lifreq[i]) >= 0) {
  399. if_addr->interface_num = lifreq[i].lifr_index;
  400. }
  401. list_add_tail(&if_addr->list, addrs);
  402. }
  403. free (lifconf.lifc_buf);
  404. close (id_fd);
  405. }
  406. return (0);
  407. error_free_addr_name:
  408. free(if_addr->name);
  409. error_free_addr:
  410. free(if_addr);
  411. error_free_ifaddrs:
  412. totemip_freeifaddrs(addrs);
  413. free (lifconf.lifc_buf);
  414. close (id_fd);
  415. return (-1);
  416. }
  417. #endif /* HAVE_GETIFADDRS */
  418. void totemip_freeifaddrs(struct list_head *addrs)
  419. {
  420. struct totem_ip_if_address *if_addr;
  421. struct list_head *list;
  422. for (list = addrs->next; list != addrs;) {
  423. if_addr = list_entry(list, struct totem_ip_if_address, list);
  424. list = list->next;
  425. free(if_addr->name);
  426. list_del(&if_addr->list);
  427. free(if_addr);
  428. }
  429. list_init(addrs);
  430. }
  431. int totemip_iface_check(struct totem_ip_address *bindnet,
  432. struct totem_ip_address *boundto,
  433. int *interface_up,
  434. int *interface_num,
  435. int mask_high_bit)
  436. {
  437. struct list_head addrs;
  438. struct list_head *list;
  439. struct totem_ip_if_address *if_addr;
  440. struct totem_ip_address bn_netaddr, if_netaddr;
  441. socklen_t addr_len;
  442. socklen_t si;
  443. int res = -1;
  444. int exact_match_found = 0;
  445. int net_match_found = 0;
  446. *interface_up = 0;
  447. *interface_num = 0;
  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. if (totemip_equal(&bn_netaddr, &if_netaddr)) {
  469. exact_match_found = 1;
  470. }
  471. for (si = 0; si < addr_len; si++) {
  472. bn_netaddr.addr[si] = bn_netaddr.addr[si] & if_addr->mask_addr.addr[si];
  473. if_netaddr.addr[si] = if_netaddr.addr[si] & if_addr->mask_addr.addr[si];
  474. }
  475. if (exact_match_found || (!net_match_found && totemip_equal(&bn_netaddr, &if_netaddr))) {
  476. totemip_copy(boundto, &if_addr->ip_addr);
  477. boundto->nodeid = bindnet->nodeid;
  478. *interface_up = if_addr->interface_up;
  479. *interface_num = if_addr->interface_num;
  480. if (boundto->family == AF_INET && boundto->nodeid == 0) {
  481. unsigned int nodeid = 0;
  482. memcpy (&nodeid, boundto->addr, sizeof (int));
  483. #if __BYTE_ORDER == __BIG_ENDIAN
  484. nodeid = swab32 (nodeid);
  485. #endif
  486. if (mask_high_bit) {
  487. nodeid &= 0x7FFFFFFF;
  488. }
  489. boundto->nodeid = nodeid;
  490. }
  491. net_match_found = 1;
  492. res = 0;
  493. if (exact_match_found) {
  494. goto finished;
  495. }
  496. }
  497. }
  498. finished:
  499. totemip_freeifaddrs(&addrs);
  500. return (res);
  501. }