totemip.c 15 KB

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