totemip.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * Copyright (c) 2005-2007, 2009 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 <arpa/inet.h>
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include <netdb.h>
  43. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  44. #include <sys/sockio.h>
  45. #include <net/if.h>
  46. #include <net/if_var.h>
  47. #include <netinet/in_var.h>
  48. #include <netinet/in.h>
  49. #include <ifaddrs.h>
  50. #endif
  51. #include <string.h>
  52. #include <stdio.h>
  53. #include <errno.h>
  54. #include <assert.h>
  55. #include <stdlib.h>
  56. #include <unistd.h>
  57. #if defined(COROSYNC_LINUX)
  58. #include <net/if.h>
  59. #include <asm/types.h>
  60. #include <linux/rtnetlink.h>
  61. #endif
  62. #include <corosync/totem/totemip.h>
  63. #include <corosync/swab.h>
  64. #define LOCALHOST_IPV4 "127.0.0.1"
  65. #define LOCALHOST_IPV6 "::1"
  66. #define NETLINK_BUFSIZE 16384
  67. #ifdef SO_NOSIGPIPE
  68. void totemip_nosigpipe(int s)
  69. {
  70. int on = 1;
  71. setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
  72. }
  73. #endif
  74. /* Compare two addresses */
  75. int totemip_equal(const struct totem_ip_address *addr1,
  76. const struct totem_ip_address *addr2)
  77. {
  78. int addrlen = 0;
  79. if (addr1->family != addr2->family)
  80. return 0;
  81. if (addr1->family == AF_INET) {
  82. addrlen = sizeof(struct in_addr);
  83. }
  84. if (addr1->family == AF_INET6) {
  85. addrlen = sizeof(struct in6_addr);
  86. }
  87. assert(addrlen);
  88. if (memcmp(addr1->addr, addr2->addr, addrlen) == 0)
  89. return 1;
  90. else
  91. return 0;
  92. }
  93. /* Copy a totem_ip_address */
  94. void totemip_copy(struct totem_ip_address *addr1,
  95. const struct totem_ip_address *addr2)
  96. {
  97. memcpy(addr1, addr2, sizeof(struct totem_ip_address));
  98. }
  99. void totemip_copy_endian_convert(struct totem_ip_address *addr1,
  100. const struct totem_ip_address *addr2)
  101. {
  102. addr1->nodeid = swab32(addr2->nodeid);
  103. addr1->family = swab16(addr2->family);
  104. memcpy(addr1->addr, addr2->addr, TOTEMIP_ADDRLEN);
  105. }
  106. /* For sorting etc. params are void * for qsort's benefit */
  107. int totemip_compare(const void *a, const void *b)
  108. {
  109. int i;
  110. const struct totem_ip_address *totemip_a = (const struct totem_ip_address *)a;
  111. const struct totem_ip_address *totemip_b = (const struct totem_ip_address *)b;
  112. struct in_addr ipv4_a1;
  113. struct in_addr ipv4_a2;
  114. struct in6_addr ipv6_a1;
  115. struct in6_addr ipv6_a2;
  116. unsigned short family;
  117. /*
  118. * Use memcpy to align since totem_ip_address is unaligned on various archs
  119. */
  120. memcpy (&family, &totemip_a->family, sizeof (unsigned short));
  121. if (family == AF_INET) {
  122. memcpy (&ipv4_a1, totemip_a->addr, sizeof (struct in_addr));
  123. memcpy (&ipv4_a2, totemip_b->addr, sizeof (struct in_addr));
  124. if (ipv4_a1.s_addr == ipv4_a2.s_addr) {
  125. return (0);
  126. }
  127. if (htonl(ipv4_a1.s_addr) < htonl(ipv4_a2.s_addr)) {
  128. return -1;
  129. } else {
  130. return +1;
  131. }
  132. } else
  133. if (family == AF_INET6) {
  134. /*
  135. * We can only compare 8 bits at time for portability reasons
  136. */
  137. memcpy (&ipv6_a1, totemip_a->addr, sizeof (struct in6_addr));
  138. memcpy (&ipv6_a2, totemip_b->addr, sizeof (struct in6_addr));
  139. for (i = 0; i < 16; i++) {
  140. int res = ipv6_a1.s6_addr[i] -
  141. ipv6_a2.s6_addr[i];
  142. if (res) {
  143. return res;
  144. }
  145. }
  146. return 0;
  147. } else {
  148. /*
  149. * Family not set, should be!
  150. */
  151. assert (0);
  152. }
  153. return 0;
  154. }
  155. /* Build a localhost totem_ip_address */
  156. int totemip_localhost(int family, struct totem_ip_address *localhost)
  157. {
  158. const char *addr_text;
  159. memset (localhost, 0, sizeof (struct totem_ip_address));
  160. if (family == AF_INET) {
  161. addr_text = LOCALHOST_IPV4;
  162. if (inet_pton(family, addr_text, (char *)&localhost->nodeid) <= 0) {
  163. return -1;
  164. }
  165. } else {
  166. addr_text = LOCALHOST_IPV6;
  167. }
  168. if (inet_pton(family, addr_text, (char *)localhost->addr) <= 0)
  169. return -1;
  170. localhost->family = family;
  171. return 0;
  172. }
  173. int totemip_localhost_check(const struct totem_ip_address *addr)
  174. {
  175. struct totem_ip_address localhost;
  176. if (totemip_localhost(addr->family, &localhost))
  177. return 0;
  178. return totemip_equal(addr, &localhost);
  179. }
  180. const char *totemip_print(const struct totem_ip_address *addr)
  181. {
  182. static char buf[INET6_ADDRSTRLEN];
  183. return (inet_ntop(addr->family, addr->addr, buf, sizeof(buf)));
  184. }
  185. /* Make a totem_ip_address into a usable sockaddr_storage */
  186. int totemip_totemip_to_sockaddr_convert(struct totem_ip_address *ip_addr,
  187. uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
  188. {
  189. int ret = -1;
  190. if (ip_addr->family == AF_INET) {
  191. struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
  192. memset(sin, 0, sizeof(struct sockaddr_in));
  193. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  194. sin->sin_len = sizeof(struct sockaddr_in);
  195. #endif
  196. sin->sin_family = ip_addr->family;
  197. sin->sin_port = ntohs(port);
  198. memcpy(&sin->sin_addr, ip_addr->addr, sizeof(struct in_addr));
  199. *addrlen = sizeof(struct sockaddr_in);
  200. ret = 0;
  201. }
  202. if (ip_addr->family == AF_INET6) {
  203. struct sockaddr_in6 *sin = (struct sockaddr_in6 *)saddr;
  204. memset(sin, 0, sizeof(struct sockaddr_in6));
  205. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  206. sin->sin6_len = sizeof(struct sockaddr_in6);
  207. #endif
  208. sin->sin6_family = ip_addr->family;
  209. sin->sin6_port = ntohs(port);
  210. sin->sin6_scope_id = 2;
  211. memcpy(&sin->sin6_addr, ip_addr->addr, sizeof(struct in6_addr));
  212. *addrlen = sizeof(struct sockaddr_in6);
  213. ret = 0;
  214. }
  215. return ret;
  216. }
  217. /* Converts an address string string into a totem_ip_address.
  218. family can be AF_INET, AF_INET6 or 0 ("for "don't care")
  219. */
  220. int totemip_parse(struct totem_ip_address *totemip, const char *addr, int family)
  221. {
  222. struct addrinfo *ainfo;
  223. struct addrinfo ahints;
  224. struct sockaddr_in *sa;
  225. struct sockaddr_in6 *sa6;
  226. int ret;
  227. memset(&ahints, 0, sizeof(ahints));
  228. ahints.ai_socktype = SOCK_DGRAM;
  229. ahints.ai_protocol = IPPROTO_UDP;
  230. ahints.ai_family = family;
  231. /* Lookup the nodename address */
  232. ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
  233. if (ret)
  234. return -1;
  235. sa = (struct sockaddr_in *)ainfo->ai_addr;
  236. sa6 = (struct sockaddr_in6 *)ainfo->ai_addr;
  237. totemip->family = ainfo->ai_family;
  238. if (ainfo->ai_family == AF_INET)
  239. memcpy(totemip->addr, &sa->sin_addr, sizeof(struct in_addr));
  240. else
  241. memcpy(totemip->addr, &sa6->sin6_addr, sizeof(struct in6_addr));
  242. return 0;
  243. }
  244. /* Make a sockaddr_* into a totem_ip_address */
  245. int totemip_sockaddr_to_totemip_convert(const struct sockaddr_storage *saddr,
  246. struct totem_ip_address *ip_addr)
  247. {
  248. int ret = -1;
  249. ip_addr->family = saddr->ss_family;
  250. ip_addr->nodeid = 0;
  251. if (saddr->ss_family == AF_INET) {
  252. const struct sockaddr_in *sin = (const struct sockaddr_in *)saddr;
  253. memcpy(ip_addr->addr, &sin->sin_addr, sizeof(struct in_addr));
  254. ret = 0;
  255. }
  256. if (saddr->ss_family == AF_INET6) {
  257. const struct sockaddr_in6 *sin
  258. = (const struct sockaddr_in6 *)saddr;
  259. memcpy(ip_addr->addr, &sin->sin6_addr, sizeof(struct in6_addr));
  260. ret = 0;
  261. }
  262. return ret;
  263. }
  264. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  265. int totemip_iface_check(struct totem_ip_address *bindnet,
  266. struct totem_ip_address *boundto,
  267. int *interface_up,
  268. int *interface_num,
  269. int mask_high_bit)
  270. {
  271. #define NEXT_IFR(a) ((struct ifreq *)((u_char *)&(a)->ifr_addr +\
  272. ((a)->ifr_addr.sa_len ? (a)->ifr_addr.sa_len : sizeof((a)->ifr_addr))))
  273. struct sockaddr_in *intf_addr_mask;
  274. struct sockaddr_storage bindnet_ss;
  275. struct sockaddr_in *intf_addr_sin;
  276. struct sockaddr_in *bindnet_sin = (struct sockaddr_in *)&bindnet_ss;
  277. struct ifaddrs *ifap, *ifa;
  278. int res = -1;
  279. int addrlen;
  280. *interface_up = 0;
  281. *interface_num = 0;
  282. totemip_totemip_to_sockaddr_convert(bindnet,
  283. 0, &bindnet_ss, &addrlen);
  284. if (getifaddrs(&ifap) != 0)
  285. return -1;
  286. for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
  287. intf_addr_sin = (struct sockaddr_in *)ifa->ifa_addr;
  288. intf_addr_mask = (struct sockaddr_in *)ifa->ifa_netmask;
  289. if (intf_addr_sin->sin_family != AF_INET)
  290. continue;
  291. if ( bindnet_sin->sin_family == AF_INET &&
  292. (intf_addr_sin->sin_addr.s_addr & intf_addr_mask->sin_addr.s_addr) ==
  293. (bindnet_sin->sin_addr.s_addr & intf_addr_mask->sin_addr.s_addr)) {
  294. totemip_copy(boundto, bindnet);
  295. memcpy(boundto->addr, &intf_addr_sin->sin_addr, sizeof(intf_addr_sin->sin_addr));
  296. /* Get interface infos
  297. */
  298. *interface_up = ifa->ifa_flags & IFF_UP;
  299. *interface_num = if_nametoindex(ifa->ifa_name);
  300. res = 0;
  301. break; /* for */
  302. }
  303. }
  304. freeifaddrs(ifap);
  305. return (res);
  306. }
  307. #elif defined(COROSYNC_LINUX)
  308. static void parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
  309. {
  310. while (RTA_OK(rta, len)) {
  311. if (rta->rta_type <= max)
  312. tb[rta->rta_type] = rta;
  313. rta = RTA_NEXT(rta,len);
  314. }
  315. }
  316. int totemip_iface_check(struct totem_ip_address *bindnet,
  317. struct totem_ip_address *boundto,
  318. int *interface_up,
  319. int *interface_num,
  320. int mask_high_bit)
  321. {
  322. int fd;
  323. struct {
  324. struct nlmsghdr nlh;
  325. struct rtgenmsg g;
  326. } req;
  327. struct sockaddr_nl nladdr;
  328. struct totem_ip_address ipaddr;
  329. static char rcvbuf[NETLINK_BUFSIZE];
  330. *interface_up = 0;
  331. *interface_num = 0;
  332. memset(&ipaddr, 0, sizeof(ipaddr));
  333. /* Make sure we preserve these */
  334. ipaddr.family = bindnet->family;
  335. ipaddr.nodeid = bindnet->nodeid;
  336. /* Ask netlink for a list of interface addresses */
  337. fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  338. if (fd <0)
  339. return -1;
  340. setsockopt(fd,SOL_SOCKET,SO_RCVBUF,&rcvbuf,sizeof(rcvbuf));
  341. memset(&nladdr, 0, sizeof(nladdr));
  342. nladdr.nl_family = AF_NETLINK;
  343. memset(&req, 0, sizeof(req));
  344. req.nlh.nlmsg_len = sizeof(req);
  345. req.nlh.nlmsg_type = RTM_GETADDR;
  346. req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
  347. req.nlh.nlmsg_pid = 0;
  348. req.nlh.nlmsg_seq = 1;
  349. req.g.rtgen_family = bindnet->family;
  350. if (sendto(fd, (void *)&req, sizeof(req), 0,
  351. (struct sockaddr*)&nladdr, sizeof(nladdr)) < 0) {
  352. close(fd);
  353. return -1;
  354. }
  355. /* Look through the return buffer for our address */
  356. while (1)
  357. {
  358. int status;
  359. struct nlmsghdr *h;
  360. struct iovec iov = { rcvbuf, sizeof(rcvbuf) };
  361. struct msghdr msg = {
  362. (void*)&nladdr, sizeof(nladdr),
  363. &iov, 1,
  364. NULL, 0,
  365. 0
  366. };
  367. status = recvmsg(fd, &msg, 0);
  368. if (!status) {
  369. close(fd);
  370. return -1;
  371. }
  372. h = (struct nlmsghdr *)rcvbuf;
  373. if (h->nlmsg_type == NLMSG_DONE)
  374. break;
  375. if (h->nlmsg_type == NLMSG_ERROR) {
  376. close(fd);
  377. return -1;
  378. }
  379. while (NLMSG_OK(h, status)) {
  380. if (h->nlmsg_type == RTM_NEWADDR) {
  381. struct ifaddrmsg *ifa = NLMSG_DATA(h);
  382. struct rtattr *tb[IFA_MAX+1];
  383. int len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa));
  384. int found_if = 0;
  385. memset(tb, 0, sizeof(tb));
  386. parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
  387. memcpy(ipaddr.addr, RTA_DATA(tb[IFA_ADDRESS]), TOTEMIP_ADDRLEN);
  388. if (totemip_equal(&ipaddr, bindnet)) {
  389. found_if = 1;
  390. }
  391. /* If the address we have is an IPv4 network address, then
  392. substitute the actual IP address of this interface */
  393. if (!found_if && tb[IFA_BROADCAST] && ifa->ifa_family == AF_INET) {
  394. uint32_t network;
  395. uint32_t addr;
  396. uint32_t netmask = htonl(~((1<<(32-ifa->ifa_prefixlen))-1));
  397. memcpy(&network, RTA_DATA(tb[IFA_BROADCAST]), sizeof(uint32_t));
  398. memcpy(&addr, bindnet->addr, sizeof(uint32_t));
  399. if ((addr & netmask) == (network & netmask)) {
  400. memcpy(ipaddr.addr, RTA_DATA(tb[IFA_ADDRESS]), TOTEMIP_ADDRLEN);
  401. found_if = 1;
  402. }
  403. }
  404. if (found_if) {
  405. /* Found it - check I/F is UP */
  406. struct ifreq ifr;
  407. int ioctl_fd; /* Can't do ioctls on netlink FDs */
  408. ioctl_fd = socket(AF_INET, SOCK_STREAM, 0);
  409. if (ioctl_fd < 0) {
  410. close(fd);
  411. return -1;
  412. }
  413. memset(&ifr, 0, sizeof(ifr));
  414. ifr.ifr_ifindex = ifa->ifa_index;
  415. /* SIOCGIFFLAGS needs an interface name */
  416. status = ioctl(ioctl_fd, SIOCGIFNAME, &ifr);
  417. status = ioctl(ioctl_fd, SIOCGIFFLAGS, &ifr);
  418. if (status) {
  419. close(ioctl_fd);
  420. close(fd);
  421. return -1;
  422. }
  423. if (ifr.ifr_flags & IFF_UP)
  424. *interface_up = 1;
  425. *interface_num = ifa->ifa_index;
  426. close(ioctl_fd);
  427. goto finished;
  428. }
  429. }
  430. h = NLMSG_NEXT(h, status);
  431. }
  432. }
  433. finished:
  434. /*
  435. * Mask 32nd bit off to workaround bugs in other poeples code
  436. * if configuration requests it.
  437. */
  438. if (ipaddr.family == AF_INET && ipaddr.nodeid == 0) {
  439. unsigned int nodeid = 0;
  440. memcpy (&nodeid, ipaddr.addr, sizeof (int));
  441. if (mask_high_bit) {
  442. nodeid &= 0x7FFFFFFF;
  443. }
  444. ipaddr.nodeid = nodeid;
  445. }
  446. totemip_copy (boundto, &ipaddr);
  447. close(fd);
  448. return 0;
  449. }
  450. #endif /* COROSYNC_LINUX */