totemip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * Copyright (c) 2005-2019 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/logsys.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. /* Copy a totem_ip_address */
  83. void totemip_copy(struct totem_ip_address *addr1,
  84. const struct totem_ip_address *addr2)
  85. {
  86. memcpy(addr1, addr2, sizeof(struct totem_ip_address));
  87. }
  88. void totemip_copy_endian_convert(struct totem_ip_address *addr1,
  89. const struct totem_ip_address *addr2)
  90. {
  91. addr1->nodeid = swab32(addr2->nodeid);
  92. addr1->family = swab16(addr2->family);
  93. memcpy(addr1->addr, addr2->addr, TOTEMIP_ADDRLEN);
  94. }
  95. /*
  96. * Multicast address range is 224.0.0.0 to 239.255.255.255 this
  97. * translates to the first 4 bits == 1110 (0xE).
  98. * http://en.wikipedia.org/wiki/Multicast_address
  99. */
  100. int32_t totemip_is_mcast(struct totem_ip_address *ip_addr)
  101. {
  102. uint32_t addr = 0;
  103. memcpy (&addr, ip_addr->addr, sizeof (uint32_t));
  104. if (ip_addr->family == AF_INET) {
  105. addr = ntohl(addr);
  106. if ((addr >> 28) != 0xE) {
  107. return -1;
  108. }
  109. }
  110. return 0;
  111. }
  112. /* For sorting etc. params are void * for qsort's benefit */
  113. int totemip_compare(const void *a, const void *b)
  114. {
  115. int i;
  116. const struct totem_ip_address *totemip_a = (const struct totem_ip_address *)a;
  117. const struct totem_ip_address *totemip_b = (const struct totem_ip_address *)b;
  118. struct in_addr ipv4_a1;
  119. struct in_addr ipv4_a2;
  120. struct in6_addr ipv6_a1;
  121. struct in6_addr ipv6_a2;
  122. unsigned short family;
  123. /*
  124. * Use memcpy to align since totem_ip_address is unaligned on various archs
  125. */
  126. memcpy (&family, &totemip_a->family, sizeof (unsigned short));
  127. if (family == AF_INET) {
  128. memcpy (&ipv4_a1, totemip_a->addr, sizeof (struct in_addr));
  129. memcpy (&ipv4_a2, totemip_b->addr, sizeof (struct in_addr));
  130. if (ipv4_a1.s_addr == ipv4_a2.s_addr) {
  131. return (0);
  132. }
  133. if (htonl(ipv4_a1.s_addr) < htonl(ipv4_a2.s_addr)) {
  134. return -1;
  135. } else {
  136. return +1;
  137. }
  138. } else
  139. if (family == AF_INET6) {
  140. /*
  141. * We can only compare 8 bits at time for portability reasons
  142. */
  143. memcpy (&ipv6_a1, totemip_a->addr, sizeof (struct in6_addr));
  144. memcpy (&ipv6_a2, totemip_b->addr, sizeof (struct in6_addr));
  145. for (i = 0; i < 16; i++) {
  146. int res = ipv6_a1.s6_addr[i] -
  147. ipv6_a2.s6_addr[i];
  148. if (res) {
  149. return res;
  150. }
  151. }
  152. return 0;
  153. } else {
  154. /*
  155. * Family not set, should be!
  156. */
  157. assert (0);
  158. }
  159. return 0;
  160. }
  161. /* Build a localhost totem_ip_address */
  162. int totemip_localhost(int family, struct totem_ip_address *localhost)
  163. {
  164. const char *addr_text;
  165. memset (localhost, 0, sizeof (struct totem_ip_address));
  166. if (family == AF_INET) {
  167. addr_text = LOCALHOST_IPV4;
  168. if (inet_pton(family, addr_text, (char *)&localhost->nodeid) <= 0) {
  169. return -1;
  170. }
  171. } else {
  172. addr_text = LOCALHOST_IPV6;
  173. }
  174. if (inet_pton(family, addr_text, (char *)localhost->addr) <= 0)
  175. return -1;
  176. localhost->family = family;
  177. return 0;
  178. }
  179. int totemip_localhost_check(const struct totem_ip_address *addr)
  180. {
  181. struct totem_ip_address localhost;
  182. if (totemip_localhost(addr->family, &localhost))
  183. return 0;
  184. return totemip_equal(addr, &localhost);
  185. }
  186. const char *totemip_sa_print(const struct sockaddr *sa)
  187. {
  188. static char buf[INET6_ADDRSTRLEN];
  189. buf[0] = 0;
  190. switch (sa->sa_family) {
  191. case AF_INET:
  192. inet_ntop(sa->sa_family, &((struct sockaddr_in *)(sa))->sin_addr, buf,
  193. INET6_ADDRSTRLEN);
  194. break;
  195. case AF_INET6:
  196. inet_ntop(sa->sa_family, &((struct sockaddr_in6 *)(sa))->sin6_addr, buf,
  197. INET6_ADDRSTRLEN);
  198. break;
  199. default:
  200. return (NULL);
  201. }
  202. return (buf);
  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. #ifdef HAVE_SOCK_SIN_LEN
  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. #ifdef HAVE_SOCK_SIN6_LEN
  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. /*
  242. * Converts an address string string into a totem_ip_address. ip_version enum
  243. * defines order.
  244. */
  245. int totemip_parse(struct totem_ip_address *totemip, const char *addr,
  246. enum totem_ip_version_enum ip_version)
  247. {
  248. struct addrinfo *ainfo;
  249. struct addrinfo *ainfo_iter;
  250. struct addrinfo *ainfo_ipv4;
  251. struct addrinfo *ainfo_ipv6;
  252. struct addrinfo *ainfo_final;
  253. struct addrinfo ahints;
  254. struct sockaddr_in *sa;
  255. struct sockaddr_in6 *sa6;
  256. int ret;
  257. int debug_ip_family;
  258. int ai_family;
  259. memset(&ahints, 0, sizeof(ahints));
  260. ahints.ai_socktype = SOCK_DGRAM;
  261. ahints.ai_protocol = IPPROTO_UDP;
  262. ai_family = AF_UNSPEC;
  263. debug_ip_family = 0;
  264. switch (ip_version) {
  265. case TOTEM_IP_VERSION_4:
  266. ai_family = AF_INET;
  267. debug_ip_family = 4;
  268. break;
  269. case TOTEM_IP_VERSION_6:
  270. ai_family = AF_INET6;
  271. debug_ip_family = 6;
  272. break;
  273. case TOTEM_IP_VERSION_6_4:
  274. case TOTEM_IP_VERSION_4_6:
  275. /*
  276. * ai_family and debug_ip_family are already set correctly
  277. */
  278. break;
  279. }
  280. ahints.ai_family = ai_family;
  281. ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
  282. if (ret == 0 && ai_family == AF_UNSPEC) {
  283. ainfo_ipv4 = ainfo_ipv6 = NULL;
  284. /*
  285. * Walk thru results and store first AF_INET and AF_INET6
  286. */
  287. for (ainfo_iter = ainfo; ainfo_iter != NULL; ainfo_iter = ainfo_iter->ai_next) {
  288. if (ainfo_iter->ai_family == AF_INET && ainfo_ipv4 == NULL) {
  289. ainfo_ipv4 = ainfo_iter;
  290. }
  291. if (ainfo_iter->ai_family == AF_INET6 && ainfo_ipv6 == NULL) {
  292. ainfo_ipv6 = ainfo_iter;
  293. }
  294. }
  295. if (ip_version == TOTEM_IP_VERSION_6_4) {
  296. if (ainfo_ipv6 != NULL) {
  297. ainfo_final = ainfo_ipv6;
  298. } else {
  299. ainfo_final = ainfo_ipv4;
  300. }
  301. } else {
  302. if (ainfo_ipv4 != NULL) {
  303. ainfo_final = ainfo_ipv4;
  304. } else {
  305. ainfo_final = ainfo_ipv6;
  306. }
  307. }
  308. } else if (ret == 0) {
  309. ainfo_final = ainfo;
  310. } else {
  311. ainfo_final = NULL;
  312. }
  313. if (ainfo_final == NULL) {
  314. if (ret == 0) {
  315. freeaddrinfo(ainfo);
  316. }
  317. if (debug_ip_family == 0) {
  318. log_printf(LOGSYS_LEVEL_DEBUG, "totemip_parse: IP address of %s not resolvable",
  319. addr);
  320. } else {
  321. log_printf(LOGSYS_LEVEL_DEBUG, "totemip_parse: IPv%u address of %s not resolvable",
  322. debug_ip_family, addr);
  323. }
  324. return (-1);
  325. }
  326. totemip->family = ainfo_final->ai_family;
  327. if (ainfo_final->ai_family == AF_INET) {
  328. sa = (struct sockaddr_in *)ainfo_final->ai_addr;
  329. memcpy(totemip->addr, &sa->sin_addr, sizeof(struct in_addr));
  330. debug_ip_family = 4;
  331. } else {
  332. sa6 = (struct sockaddr_in6 *)ainfo_final->ai_addr;
  333. memcpy(totemip->addr, &sa6->sin6_addr, sizeof(struct in6_addr));
  334. debug_ip_family = 6;
  335. }
  336. log_printf(LOGSYS_LEVEL_DEBUG, "totemip_parse: IPv%u address of %s resolved as %s",
  337. debug_ip_family, addr, totemip_print(totemip));
  338. freeaddrinfo(ainfo);
  339. return (0);
  340. }
  341. /* Make a sockaddr_* into a totem_ip_address */
  342. int totemip_sockaddr_to_totemip_convert(const struct sockaddr_storage *saddr,
  343. struct totem_ip_address *ip_addr)
  344. {
  345. int ret = -1;
  346. ip_addr->family = saddr->ss_family;
  347. ip_addr->nodeid = 0;
  348. if (saddr->ss_family == AF_INET) {
  349. const struct sockaddr_in *sin = (const struct sockaddr_in *)saddr;
  350. memcpy(ip_addr->addr, &sin->sin_addr, sizeof(struct in_addr));
  351. ret = 0;
  352. }
  353. if (saddr->ss_family == AF_INET6) {
  354. const struct sockaddr_in6 *sin
  355. = (const struct sockaddr_in6 *)saddr;
  356. memcpy(ip_addr->addr, &sin->sin6_addr, sizeof(struct in6_addr));
  357. ret = 0;
  358. }
  359. return ret;
  360. }
  361. int totemip_getifaddrs(struct qb_list_head *addrs)
  362. {
  363. struct ifaddrs *ifap, *ifa;
  364. struct totem_ip_if_address *if_addr;
  365. if (getifaddrs(&ifap) != 0)
  366. return (-1);
  367. qb_list_init(addrs);
  368. for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
  369. if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
  370. continue ;
  371. if ((ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) ||
  372. (ifa->ifa_netmask->sa_family != AF_INET && ifa->ifa_netmask->sa_family != AF_INET6 &&
  373. ifa->ifa_netmask->sa_family != 0))
  374. continue ;
  375. if (ifa->ifa_netmask->sa_family == 0) {
  376. ifa->ifa_netmask->sa_family = ifa->ifa_addr->sa_family;
  377. }
  378. if_addr = malloc(sizeof(struct totem_ip_if_address));
  379. if (if_addr == NULL) {
  380. goto error_free_ifaddrs;
  381. }
  382. qb_list_init(&if_addr->list);
  383. memset(if_addr, 0, sizeof(struct totem_ip_if_address));
  384. if_addr->interface_up = ifa->ifa_flags & IFF_UP;
  385. if_addr->interface_num = if_nametoindex(ifa->ifa_name);
  386. if_addr->name = strdup(ifa->ifa_name);
  387. if (if_addr->name == NULL) {
  388. goto error_free_addr;
  389. }
  390. if (totemip_sockaddr_to_totemip_convert((const struct sockaddr_storage *)ifa->ifa_addr,
  391. &if_addr->ip_addr) == -1) {
  392. goto error_free_addr_name;
  393. }
  394. if (totemip_sockaddr_to_totemip_convert((const struct sockaddr_storage *)ifa->ifa_netmask,
  395. &if_addr->mask_addr) == -1) {
  396. goto error_free_addr_name;
  397. }
  398. qb_list_add_tail(&if_addr->list, addrs);
  399. }
  400. freeifaddrs(ifap);
  401. return (0);
  402. error_free_addr_name:
  403. free(if_addr->name);
  404. error_free_addr:
  405. free(if_addr);
  406. error_free_ifaddrs:
  407. totemip_freeifaddrs(addrs);
  408. freeifaddrs(ifap);
  409. return (-1);
  410. }
  411. void totemip_freeifaddrs(struct qb_list_head *addrs)
  412. {
  413. struct totem_ip_if_address *if_addr;
  414. struct qb_list_head *list, *tmp_iter;
  415. qb_list_for_each_safe(list, tmp_iter, addrs) {
  416. if_addr = qb_list_entry(list, struct totem_ip_if_address, list);
  417. free(if_addr->name);
  418. qb_list_del(&if_addr->list);
  419. free(if_addr);
  420. }
  421. qb_list_init(addrs);
  422. }
  423. int totemip_iface_check(struct totem_ip_address *bindnet,
  424. struct totem_ip_address *boundto,
  425. int *interface_up,
  426. int *interface_num,
  427. int mask_high_bit)
  428. {
  429. struct qb_list_head addrs;
  430. struct qb_list_head *list;
  431. struct totem_ip_if_address *if_addr;
  432. struct totem_ip_address bn_netaddr, if_netaddr;
  433. socklen_t addr_len;
  434. socklen_t si;
  435. int res = -1;
  436. int exact_match_found = 0;
  437. int net_match_found = 0;
  438. *interface_up = 0;
  439. *interface_num = 0;
  440. if (totemip_getifaddrs(&addrs) == -1) {
  441. return (-1);
  442. }
  443. qb_list_for_each(list, &addrs) {
  444. if_addr = qb_list_entry(list, struct totem_ip_if_address, list);
  445. if (bindnet->family != if_addr->ip_addr.family)
  446. continue ;
  447. addr_len = 0;
  448. switch (bindnet->family) {
  449. case AF_INET:
  450. addr_len = sizeof(struct in_addr);
  451. break;
  452. case AF_INET6:
  453. addr_len = sizeof(struct in6_addr);
  454. break;
  455. }
  456. if (addr_len == 0)
  457. continue ;
  458. totemip_copy(&bn_netaddr, bindnet);
  459. totemip_copy(&if_netaddr, &if_addr->ip_addr);
  460. if (totemip_equal(&bn_netaddr, &if_netaddr)) {
  461. exact_match_found = 1;
  462. }
  463. for (si = 0; si < addr_len; si++) {
  464. bn_netaddr.addr[si] = bn_netaddr.addr[si] & if_addr->mask_addr.addr[si];
  465. if_netaddr.addr[si] = if_netaddr.addr[si] & if_addr->mask_addr.addr[si];
  466. }
  467. if (exact_match_found || (!net_match_found && totemip_equal(&bn_netaddr, &if_netaddr))) {
  468. totemip_copy(boundto, &if_addr->ip_addr);
  469. boundto->nodeid = bindnet->nodeid;
  470. *interface_up = if_addr->interface_up;
  471. *interface_num = if_addr->interface_num;
  472. net_match_found = 1;
  473. res = 0;
  474. if (exact_match_found) {
  475. goto finished;
  476. }
  477. }
  478. }
  479. finished:
  480. totemip_freeifaddrs(&addrs);
  481. return (res);
  482. }
  483. #define TOTEMIP_UDP_HEADER_SIZE 8
  484. #define TOTEMIP_IPV4_HEADER_SIZE 20
  485. #define TOTEMIP_IPV6_HEADER_SIZE 40
  486. size_t totemip_udpip_header_size(int family)
  487. {
  488. size_t header_size;
  489. header_size = 0;
  490. switch (family) {
  491. case AF_INET:
  492. header_size = TOTEMIP_UDP_HEADER_SIZE + TOTEMIP_IPV4_HEADER_SIZE;
  493. break;
  494. case AF_INET6:
  495. header_size = TOTEMIP_UDP_HEADER_SIZE + TOTEMIP_IPV6_HEADER_SIZE;
  496. break;
  497. }
  498. return (header_size);
  499. }