4
0

totemip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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 = 2;
  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. debug_ip_family = 4;
  346. } else {
  347. sa6 = (struct sockaddr_in6 *)ainfo_final->ai_addr;
  348. memcpy(totemip->addr, &sa6->sin6_addr, sizeof(struct in6_addr));
  349. debug_ip_family = 6;
  350. }
  351. log_printf(LOGSYS_LEVEL_DEBUG, "totemip_parse: IPv%u address of %s resolved as %s",
  352. debug_ip_family, addr, totemip_print(totemip));
  353. freeaddrinfo(ainfo);
  354. return (0);
  355. }
  356. /* Make a sockaddr_* into a totem_ip_address */
  357. int totemip_sockaddr_to_totemip_convert(const struct sockaddr_storage *saddr,
  358. struct totem_ip_address *ip_addr)
  359. {
  360. int ret = -1;
  361. ip_addr->family = saddr->ss_family;
  362. ip_addr->nodeid = 0;
  363. if (saddr->ss_family == AF_INET) {
  364. const struct sockaddr_in *sin = (const struct sockaddr_in *)saddr;
  365. memcpy(ip_addr->addr, &sin->sin_addr, sizeof(struct in_addr));
  366. ret = 0;
  367. }
  368. if (saddr->ss_family == AF_INET6) {
  369. const struct sockaddr_in6 *sin
  370. = (const struct sockaddr_in6 *)saddr;
  371. memcpy(ip_addr->addr, &sin->sin6_addr, sizeof(struct in6_addr));
  372. ret = 0;
  373. }
  374. return ret;
  375. }
  376. int totemip_getifaddrs(struct qb_list_head *addrs)
  377. {
  378. struct ifaddrs *ifap, *ifa;
  379. struct totem_ip_if_address *if_addr;
  380. if (getifaddrs(&ifap) != 0)
  381. return (-1);
  382. qb_list_init(addrs);
  383. for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
  384. if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
  385. continue ;
  386. if ((ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) ||
  387. (ifa->ifa_netmask->sa_family != AF_INET && ifa->ifa_netmask->sa_family != AF_INET6 &&
  388. ifa->ifa_netmask->sa_family != 0))
  389. continue ;
  390. if (ifa->ifa_netmask->sa_family == 0) {
  391. ifa->ifa_netmask->sa_family = ifa->ifa_addr->sa_family;
  392. }
  393. if_addr = malloc(sizeof(struct totem_ip_if_address));
  394. if (if_addr == NULL) {
  395. goto error_free_ifaddrs;
  396. }
  397. qb_list_init(&if_addr->list);
  398. memset(if_addr, 0, sizeof(struct totem_ip_if_address));
  399. if_addr->interface_up = ifa->ifa_flags & IFF_UP;
  400. if_addr->interface_num = if_nametoindex(ifa->ifa_name);
  401. if_addr->name = strdup(ifa->ifa_name);
  402. if (if_addr->name == NULL) {
  403. goto error_free_addr;
  404. }
  405. if (totemip_sockaddr_to_totemip_convert((const struct sockaddr_storage *)ifa->ifa_addr,
  406. &if_addr->ip_addr) == -1) {
  407. goto error_free_addr_name;
  408. }
  409. if (totemip_sockaddr_to_totemip_convert((const struct sockaddr_storage *)ifa->ifa_netmask,
  410. &if_addr->mask_addr) == -1) {
  411. goto error_free_addr_name;
  412. }
  413. qb_list_add_tail(&if_addr->list, addrs);
  414. }
  415. freeifaddrs(ifap);
  416. return (0);
  417. error_free_addr_name:
  418. free(if_addr->name);
  419. error_free_addr:
  420. free(if_addr);
  421. error_free_ifaddrs:
  422. totemip_freeifaddrs(addrs);
  423. freeifaddrs(ifap);
  424. return (-1);
  425. }
  426. void totemip_freeifaddrs(struct qb_list_head *addrs)
  427. {
  428. struct totem_ip_if_address *if_addr;
  429. struct qb_list_head *list, *tmp_iter;
  430. qb_list_for_each_safe(list, tmp_iter, addrs) {
  431. if_addr = qb_list_entry(list, struct totem_ip_if_address, list);
  432. free(if_addr->name);
  433. qb_list_del(&if_addr->list);
  434. free(if_addr);
  435. }
  436. qb_list_init(addrs);
  437. }
  438. int totemip_iface_check(struct totem_ip_address *bindnet,
  439. struct totem_ip_address *boundto,
  440. int *interface_up,
  441. int *interface_num,
  442. int mask_high_bit)
  443. {
  444. struct qb_list_head addrs;
  445. struct qb_list_head *list;
  446. struct totem_ip_if_address *if_addr;
  447. struct totem_ip_address bn_netaddr, if_netaddr;
  448. socklen_t addr_len;
  449. socklen_t si;
  450. int res = -1;
  451. int exact_match_found = 0;
  452. int net_match_found = 0;
  453. *interface_up = 0;
  454. *interface_num = 0;
  455. if (totemip_getifaddrs(&addrs) == -1) {
  456. return (-1);
  457. }
  458. qb_list_for_each(list, &addrs) {
  459. if_addr = qb_list_entry(list, struct totem_ip_if_address, list);
  460. if (bindnet->family != if_addr->ip_addr.family)
  461. continue ;
  462. addr_len = 0;
  463. switch (bindnet->family) {
  464. case AF_INET:
  465. addr_len = sizeof(struct in_addr);
  466. break;
  467. case AF_INET6:
  468. addr_len = sizeof(struct in6_addr);
  469. break;
  470. }
  471. if (addr_len == 0)
  472. continue ;
  473. totemip_copy(&bn_netaddr, bindnet);
  474. totemip_copy(&if_netaddr, &if_addr->ip_addr);
  475. if (totemip_equal(&bn_netaddr, &if_netaddr)) {
  476. exact_match_found = 1;
  477. }
  478. for (si = 0; si < addr_len; si++) {
  479. bn_netaddr.addr[si] = bn_netaddr.addr[si] & if_addr->mask_addr.addr[si];
  480. if_netaddr.addr[si] = if_netaddr.addr[si] & if_addr->mask_addr.addr[si];
  481. }
  482. if (exact_match_found || (!net_match_found && totemip_equal(&bn_netaddr, &if_netaddr))) {
  483. totemip_copy(boundto, &if_addr->ip_addr);
  484. boundto->nodeid = bindnet->nodeid;
  485. *interface_up = if_addr->interface_up;
  486. *interface_num = if_addr->interface_num;
  487. net_match_found = 1;
  488. res = 0;
  489. if (exact_match_found) {
  490. goto finished;
  491. }
  492. }
  493. }
  494. finished:
  495. totemip_freeifaddrs(&addrs);
  496. return (res);
  497. }
  498. #define TOTEMIP_UDP_HEADER_SIZE 8
  499. #define TOTEMIP_IPV4_HEADER_SIZE 20
  500. #define TOTEMIP_IPV6_HEADER_SIZE 40
  501. size_t totemip_udpip_header_size(int family)
  502. {
  503. size_t header_size;
  504. header_size = 0;
  505. switch (family) {
  506. case AF_INET:
  507. header_size = TOTEMIP_UDP_HEADER_SIZE + TOTEMIP_IPV4_HEADER_SIZE;
  508. break;
  509. case AF_INET6:
  510. header_size = TOTEMIP_UDP_HEADER_SIZE + TOTEMIP_IPV6_HEADER_SIZE;
  511. break;
  512. }
  513. return (header_size);
  514. }