totemip.c 14 KB

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