totemip.c 15 KB

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