4
0

totemip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. 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. /* Make a totem_ip_address into a usable sockaddr_storage */
  232. int totemip_totemip_to_sockaddr_convert(struct totem_ip_address *ip_addr,
  233. uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
  234. {
  235. int ret = -1;
  236. if (ip_addr->family == AF_INET) {
  237. struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
  238. memset(sin, 0, sizeof(struct sockaddr_in));
  239. #ifdef HAVE_SOCK_SIN_LEN
  240. sin->sin_len = sizeof(struct sockaddr_in);
  241. #endif
  242. sin->sin_family = ip_addr->family;
  243. sin->sin_port = ntohs(port);
  244. memcpy(&sin->sin_addr, ip_addr->addr, sizeof(struct in_addr));
  245. *addrlen = sizeof(struct sockaddr_in);
  246. ret = 0;
  247. }
  248. if (ip_addr->family == AF_INET6) {
  249. struct sockaddr_in6 *sin = (struct sockaddr_in6 *)saddr;
  250. memset(sin, 0, sizeof(struct sockaddr_in6));
  251. #ifdef HAVE_SOCK_SIN6_LEN
  252. sin->sin6_len = sizeof(struct sockaddr_in6);
  253. #endif
  254. sin->sin6_family = ip_addr->family;
  255. sin->sin6_port = ntohs(port);
  256. sin->sin6_scope_id = 2;
  257. memcpy(&sin->sin6_addr, ip_addr->addr, sizeof(struct in6_addr));
  258. *addrlen = sizeof(struct sockaddr_in6);
  259. ret = 0;
  260. }
  261. return ret;
  262. }
  263. /*
  264. * Converts an address string string into a totem_ip_address. ip_version enum
  265. * defines order.
  266. */
  267. int totemip_parse(struct totem_ip_address *totemip, const char *addr,
  268. enum totem_ip_version_enum ip_version)
  269. {
  270. struct addrinfo *ainfo;
  271. struct addrinfo *ainfo_iter;
  272. struct addrinfo *ainfo_ipv4;
  273. struct addrinfo *ainfo_ipv6;
  274. struct addrinfo *ainfo_final;
  275. struct addrinfo ahints;
  276. struct sockaddr_in *sa;
  277. struct sockaddr_in6 *sa6;
  278. int ret;
  279. int debug_ip_family;
  280. int ai_family;
  281. memset(&ahints, 0, sizeof(ahints));
  282. ahints.ai_socktype = SOCK_DGRAM;
  283. ahints.ai_protocol = IPPROTO_UDP;
  284. ai_family = AF_UNSPEC;
  285. debug_ip_family = 0;
  286. switch (ip_version) {
  287. case TOTEM_IP_VERSION_4:
  288. ai_family = AF_INET;
  289. debug_ip_family = 4;
  290. break;
  291. case TOTEM_IP_VERSION_6:
  292. ai_family = AF_INET6;
  293. debug_ip_family = 6;
  294. break;
  295. case TOTEM_IP_VERSION_6_4:
  296. case TOTEM_IP_VERSION_4_6:
  297. /*
  298. * ai_family and debug_ip_family are already set correctly
  299. */
  300. break;
  301. }
  302. ahints.ai_family = ai_family;
  303. ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
  304. if (ret == 0 && ai_family == AF_UNSPEC) {
  305. ainfo_ipv4 = ainfo_ipv6 = NULL;
  306. /*
  307. * Walk thru results and store first AF_INET and AF_INET6
  308. */
  309. for (ainfo_iter = ainfo; ainfo_iter != NULL; ainfo_iter = ainfo_iter->ai_next) {
  310. if (ainfo_iter->ai_family == AF_INET && ainfo_ipv4 == NULL) {
  311. ainfo_ipv4 = ainfo_iter;
  312. }
  313. if (ainfo_iter->ai_family == AF_INET6 && ainfo_ipv6 == NULL) {
  314. ainfo_ipv6 = ainfo_iter;
  315. }
  316. }
  317. if (ip_version == TOTEM_IP_VERSION_6_4) {
  318. if (ainfo_ipv6 != NULL) {
  319. ainfo_final = ainfo_ipv6;
  320. } else {
  321. ainfo_final = ainfo_ipv4;
  322. }
  323. } else {
  324. if (ainfo_ipv4 != NULL) {
  325. ainfo_final = ainfo_ipv4;
  326. } else {
  327. ainfo_final = ainfo_ipv6;
  328. }
  329. }
  330. } else if (ret == 0) {
  331. ainfo_final = ainfo;
  332. } else {
  333. ainfo_final = NULL;
  334. }
  335. if (ainfo_final == NULL) {
  336. if (ret == 0) {
  337. freeaddrinfo(ainfo);
  338. }
  339. if (debug_ip_family == 0) {
  340. log_printf(LOGSYS_LEVEL_DEBUG, "totemip_parse: IP address of %s not resolvable",
  341. addr);
  342. } else {
  343. log_printf(LOGSYS_LEVEL_DEBUG, "totemip_parse: IPv%u address of %s not resolvable",
  344. debug_ip_family, addr);
  345. }
  346. return (-1);
  347. }
  348. totemip->family = ainfo_final->ai_family;
  349. if (ainfo_final->ai_family == AF_INET) {
  350. sa = (struct sockaddr_in *)ainfo_final->ai_addr;
  351. memcpy(totemip->addr, &sa->sin_addr, sizeof(struct in_addr));
  352. debug_ip_family = 4;
  353. } else {
  354. sa6 = (struct sockaddr_in6 *)ainfo_final->ai_addr;
  355. memcpy(totemip->addr, &sa6->sin6_addr, sizeof(struct in6_addr));
  356. debug_ip_family = 6;
  357. }
  358. log_printf(LOGSYS_LEVEL_DEBUG, "totemip_parse: IPv%u address of %s resolved as %s",
  359. debug_ip_family, addr, totemip_print(totemip));
  360. freeaddrinfo(ainfo);
  361. return (0);
  362. }
  363. /* Make a sockaddr_* into a totem_ip_address */
  364. int totemip_sockaddr_to_totemip_convert(const struct sockaddr_storage *saddr,
  365. struct totem_ip_address *ip_addr)
  366. {
  367. int ret = -1;
  368. ip_addr->family = saddr->ss_family;
  369. ip_addr->nodeid = 0;
  370. if (saddr->ss_family == AF_INET) {
  371. const struct sockaddr_in *sin = (const struct sockaddr_in *)saddr;
  372. memcpy(ip_addr->addr, &sin->sin_addr, sizeof(struct in_addr));
  373. ret = 0;
  374. }
  375. if (saddr->ss_family == AF_INET6) {
  376. const struct sockaddr_in6 *sin
  377. = (const struct sockaddr_in6 *)saddr;
  378. memcpy(ip_addr->addr, &sin->sin6_addr, sizeof(struct in6_addr));
  379. ret = 0;
  380. }
  381. return ret;
  382. }
  383. int totemip_getifaddrs(struct qb_list_head *addrs)
  384. {
  385. struct ifaddrs *ifap, *ifa;
  386. struct totem_ip_if_address *if_addr;
  387. if (getifaddrs(&ifap) != 0)
  388. return (-1);
  389. qb_list_init(addrs);
  390. for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
  391. if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
  392. continue ;
  393. if ((ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) ||
  394. (ifa->ifa_netmask->sa_family != AF_INET && ifa->ifa_netmask->sa_family != AF_INET6 &&
  395. ifa->ifa_netmask->sa_family != 0))
  396. continue ;
  397. if (ifa->ifa_netmask->sa_family == 0) {
  398. ifa->ifa_netmask->sa_family = ifa->ifa_addr->sa_family;
  399. }
  400. if_addr = malloc(sizeof(struct totem_ip_if_address));
  401. if (if_addr == NULL) {
  402. goto error_free_ifaddrs;
  403. }
  404. qb_list_init(&if_addr->list);
  405. memset(if_addr, 0, sizeof(struct totem_ip_if_address));
  406. if_addr->interface_up = ifa->ifa_flags & IFF_UP;
  407. if_addr->interface_num = if_nametoindex(ifa->ifa_name);
  408. if_addr->name = strdup(ifa->ifa_name);
  409. if (if_addr->name == NULL) {
  410. goto error_free_addr;
  411. }
  412. if (totemip_sockaddr_to_totemip_convert((const struct sockaddr_storage *)ifa->ifa_addr,
  413. &if_addr->ip_addr) == -1) {
  414. goto error_free_addr_name;
  415. }
  416. if (totemip_sockaddr_to_totemip_convert((const struct sockaddr_storage *)ifa->ifa_netmask,
  417. &if_addr->mask_addr) == -1) {
  418. goto error_free_addr_name;
  419. }
  420. qb_list_add_tail(&if_addr->list, addrs);
  421. }
  422. freeifaddrs(ifap);
  423. return (0);
  424. error_free_addr_name:
  425. free(if_addr->name);
  426. error_free_addr:
  427. free(if_addr);
  428. error_free_ifaddrs:
  429. totemip_freeifaddrs(addrs);
  430. freeifaddrs(ifap);
  431. return (-1);
  432. }
  433. void totemip_freeifaddrs(struct qb_list_head *addrs)
  434. {
  435. struct totem_ip_if_address *if_addr;
  436. struct qb_list_head *list, *tmp_iter;
  437. qb_list_for_each_safe(list, tmp_iter, addrs) {
  438. if_addr = qb_list_entry(list, struct totem_ip_if_address, list);
  439. free(if_addr->name);
  440. qb_list_del(&if_addr->list);
  441. free(if_addr);
  442. }
  443. qb_list_init(addrs);
  444. }
  445. int totemip_iface_check(struct totem_ip_address *bindnet,
  446. struct totem_ip_address *boundto,
  447. int *interface_up,
  448. int *interface_num,
  449. int mask_high_bit)
  450. {
  451. struct qb_list_head addrs;
  452. struct qb_list_head *list;
  453. struct totem_ip_if_address *if_addr;
  454. struct totem_ip_address bn_netaddr, if_netaddr;
  455. socklen_t addr_len;
  456. socklen_t si;
  457. int res = -1;
  458. int exact_match_found = 0;
  459. int net_match_found = 0;
  460. *interface_up = 0;
  461. *interface_num = 0;
  462. if (totemip_getifaddrs(&addrs) == -1) {
  463. return (-1);
  464. }
  465. qb_list_for_each(list, &addrs) {
  466. if_addr = qb_list_entry(list, struct totem_ip_if_address, list);
  467. if (bindnet->family != if_addr->ip_addr.family)
  468. continue ;
  469. addr_len = 0;
  470. switch (bindnet->family) {
  471. case AF_INET:
  472. addr_len = sizeof(struct in_addr);
  473. break;
  474. case AF_INET6:
  475. addr_len = sizeof(struct in6_addr);
  476. break;
  477. }
  478. if (addr_len == 0)
  479. continue ;
  480. totemip_copy(&bn_netaddr, bindnet);
  481. totemip_copy(&if_netaddr, &if_addr->ip_addr);
  482. if (totemip_equal(&bn_netaddr, &if_netaddr)) {
  483. exact_match_found = 1;
  484. }
  485. for (si = 0; si < addr_len; si++) {
  486. bn_netaddr.addr[si] = bn_netaddr.addr[si] & if_addr->mask_addr.addr[si];
  487. if_netaddr.addr[si] = if_netaddr.addr[si] & if_addr->mask_addr.addr[si];
  488. }
  489. if (exact_match_found || (!net_match_found && totemip_equal(&bn_netaddr, &if_netaddr))) {
  490. totemip_copy(boundto, &if_addr->ip_addr);
  491. boundto->nodeid = bindnet->nodeid;
  492. *interface_up = if_addr->interface_up;
  493. *interface_num = if_addr->interface_num;
  494. net_match_found = 1;
  495. res = 0;
  496. if (exact_match_found) {
  497. goto finished;
  498. }
  499. }
  500. }
  501. finished:
  502. totemip_freeifaddrs(&addrs);
  503. return (res);
  504. }
  505. #define TOTEMIP_UDP_HEADER_SIZE 8
  506. #define TOTEMIP_IPV4_HEADER_SIZE 20
  507. #define TOTEMIP_IPV6_HEADER_SIZE 40
  508. size_t totemip_udpip_header_size(int family)
  509. {
  510. size_t header_size;
  511. header_size = 0;
  512. switch (family) {
  513. case AF_INET:
  514. header_size = TOTEMIP_UDP_HEADER_SIZE + TOTEMIP_IPV4_HEADER_SIZE;
  515. break;
  516. case AF_INET6:
  517. header_size = TOTEMIP_UDP_HEADER_SIZE + TOTEMIP_IPV6_HEADER_SIZE;
  518. break;
  519. }
  520. return (header_size);
  521. }