totemip.c 13 KB

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