totemip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Copyright (c) 2005 Red Hat Inc
  3. * Copyright (c) 2006 Sun Microsystems, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Patrick Caulfield (pcaulfie@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. /* IPv4/6 abstraction */
  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(OPENAIS_BSD) || defined(OPENAIS_DARWIN) || defined(OPENAIS_SOLARIS)
  44. #include <sys/sockio.h>
  45. #include <net/if.h>
  46. #ifndef OPENAIS_SOLARIS
  47. #include <net/if_var.h>
  48. #endif
  49. #include <netinet/in_var.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(OPENAIS_LINUX)
  58. #include <net/if.h>
  59. /* ARGH!! I hate netlink */
  60. #include <asm/types.h>
  61. #include <linux/rtnetlink.h>
  62. #endif
  63. #if ! defined(OPENAIS_SOLARIS) && ! defined(s6_addr16)
  64. #define s6_addr16 __u6_addr.__u6_addr16
  65. #endif
  66. #include "totemip.h"
  67. #include "swab.h"
  68. #define LOCALHOST_IPV4 "127.0.0.1"
  69. #define LOCALHOST_IPV6 "::1"
  70. #define NETLINK_BUFSIZE 16384
  71. #ifdef SO_NOSIGPIPE
  72. void totemip_nosigpipe(int s)
  73. {
  74. int on = 1;
  75. setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
  76. }
  77. #endif
  78. /* Compare two addresses */
  79. int totemip_equal(struct totem_ip_address *addr1, struct totem_ip_address *addr2)
  80. {
  81. int addrlen = 0;
  82. if (addr1->family != addr2->family)
  83. return 0;
  84. if (addr1->family == AF_INET) {
  85. addrlen = sizeof(struct in_addr);
  86. }
  87. if (addr1->family == AF_INET6) {
  88. addrlen = sizeof(struct in6_addr);
  89. }
  90. assert(addrlen);
  91. if (memcmp(addr1->addr, addr2->addr, addrlen) == 0)
  92. return 1;
  93. else
  94. return 0;
  95. }
  96. /* Copy a totem_ip_address */
  97. void totemip_copy(struct totem_ip_address *addr1, struct totem_ip_address *addr2)
  98. {
  99. memcpy(addr1, addr2, sizeof(struct totem_ip_address));
  100. }
  101. void totemip_copy_endian_convert(struct totem_ip_address *addr1, struct totem_ip_address *addr2)
  102. {
  103. addr1->nodeid = swab32(addr2->nodeid);
  104. addr1->family = swab16(addr2->family);
  105. if (addr1 != addr2) {
  106. memcpy(addr1->addr, addr2->addr, TOTEMIP_ADDRLEN);
  107. }
  108. }
  109. /* For sorting etc. params are void * for qsort's benefit */
  110. int totemip_compare(const void *a, const void *b)
  111. {
  112. int i;
  113. const struct totem_ip_address *addr1 = a;
  114. const struct totem_ip_address *addr2 = b;
  115. struct in6_addr *sin6a;
  116. struct in6_addr *sin6b;
  117. if (addr1->family != addr2->family)
  118. return (addr1->family > addr2->family);
  119. if (addr1->family == AF_INET) {
  120. #ifndef __sparc
  121. struct in_addr *in1 = (struct in_addr *)addr1->addr;
  122. struct in_addr *in2 = (struct in_addr *)addr2->addr;
  123. #else
  124. /* Deal with misalignment */
  125. struct in_addr i1, i2;
  126. struct in_addr *in1 = &i1;
  127. struct in_addr *in2 = &i2;
  128. memcpy(in1, addr1->addr, sizeof (*in1));
  129. memcpy(in2, addr2->addr, sizeof (*in2));
  130. #endif
  131. /* A bit clunky but avoids sign problems */
  132. if (in1->s_addr == in2->s_addr)
  133. return 0;
  134. if (htonl(in1->s_addr) < htonl(in2->s_addr))
  135. return -1;
  136. else
  137. return +1;
  138. }
  139. /* Compare IPv6 addresses */
  140. sin6a = (struct in6_addr *)addr1->addr;
  141. sin6b = (struct in6_addr *)addr2->addr;
  142. /* Remember, addresses are in big-endian format.
  143. We compare 16bits at a time rather than 32 to avoid sign problems */
  144. for (i = 0; i < 8; i++) {
  145. #ifndef OPENAIS_SOLARIS
  146. int res = htons(sin6a->s6_addr16[i]) -
  147. htons(sin6b->s6_addr16[i]);
  148. #else
  149. int res = htons(((uint16_t *)sin6a->s6_addr)[i]) -
  150. htons(((uint16_t *)sin6b->s6_addr)[i]);
  151. #endif
  152. if (res) {
  153. return res;
  154. }
  155. }
  156. return 0;
  157. }
  158. /* Build a localhost totem_ip_address */
  159. int totemip_localhost(int family, struct totem_ip_address *localhost)
  160. {
  161. char *addr_text;
  162. uint32_t nodeid;
  163. memset (localhost, 0, sizeof (struct totem_ip_address));
  164. if (family == AF_INET) {
  165. addr_text = LOCALHOST_IPV4;
  166. if (inet_pton(family, addr_text, (char *)&nodeid) <= 0) {
  167. return -1;
  168. }
  169. localhost->nodeid = ntohl(nodeid);
  170. } else {
  171. addr_text = LOCALHOST_IPV6;
  172. }
  173. if (inet_pton(family, addr_text, (char *)localhost->addr) <= 0)
  174. return -1;
  175. localhost->family = family;
  176. return 0;
  177. }
  178. int totemip_localhost_check(struct totem_ip_address *addr)
  179. {
  180. struct totem_ip_address localhost;
  181. if (totemip_localhost(addr->family, &localhost))
  182. return 0;
  183. return totemip_equal(addr, &localhost);
  184. }
  185. const char *totemip_print(struct totem_ip_address *addr)
  186. {
  187. static char buf[INET6_ADDRSTRLEN];
  188. return inet_ntop(addr->family, addr->addr, buf, sizeof(buf));
  189. }
  190. /* Make a totem_ip_address into a usable sockaddr_storage */
  191. int totemip_totemip_to_sockaddr_convert(struct totem_ip_address *ip_addr,
  192. uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
  193. {
  194. int ret = -1;
  195. if (ip_addr->family == AF_INET) {
  196. struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
  197. memset(sin, 0, sizeof(struct sockaddr_in));
  198. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  199. sin->sin_len = sizeof(struct sockaddr_in);
  200. #endif
  201. sin->sin_family = ip_addr->family;
  202. sin->sin_port = htons (port);
  203. memcpy(&sin->sin_addr, ip_addr->addr, sizeof(struct in_addr));
  204. *addrlen = sizeof(struct sockaddr_in);
  205. ret = 0;
  206. }
  207. if (ip_addr->family == AF_INET6) {
  208. struct sockaddr_in6 *sin = (struct sockaddr_in6 *)saddr;
  209. memset(sin, 0, sizeof(struct sockaddr_in6));
  210. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  211. sin->sin6_len = sizeof(struct sockaddr_in6);
  212. #endif
  213. sin->sin6_family = ip_addr->family;
  214. sin->sin6_port = htons (port);
  215. sin->sin6_scope_id = 2;
  216. memcpy(&sin->sin6_addr, ip_addr->addr, sizeof(struct in6_addr));
  217. *addrlen = sizeof(struct sockaddr_in6);
  218. ret = 0;
  219. }
  220. return ret;
  221. }
  222. /* Converts an address string string into a totem_ip_address.
  223. * family can be AF_INET, AF_INET6 or 0 (for "don't care")
  224. */
  225. int totemip_parse(struct totem_ip_address *totemip, char *addr, int family)
  226. {
  227. struct addrinfo *ainfo;
  228. struct addrinfo ahints;
  229. struct sockaddr_in *sa;
  230. struct sockaddr_in6 *sa6;
  231. int ret;
  232. memset(&ahints, 0, sizeof(ahints));
  233. ahints.ai_socktype = SOCK_DGRAM;
  234. ahints.ai_protocol = IPPROTO_UDP;
  235. ahints.ai_family = family;
  236. /* Lookup the nodename address */
  237. ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
  238. if (ret)
  239. return -1;
  240. sa = (struct sockaddr_in *)ainfo->ai_addr;
  241. sa6 = (struct sockaddr_in6 *)ainfo->ai_addr;
  242. totemip->family = ainfo->ai_family;
  243. if (ainfo->ai_family == AF_INET)
  244. memcpy(totemip->addr, &sa->sin_addr, sizeof(struct in_addr));
  245. else
  246. memcpy(totemip->addr, &sa6->sin6_addr, sizeof(struct in6_addr));
  247. freeaddrinfo(ainfo);
  248. return 0;
  249. }
  250. /* Make a sockaddr_* into a totem_ip_address */
  251. int totemip_sockaddr_to_totemip_convert(struct sockaddr_storage *saddr, struct totem_ip_address *ip_addr)
  252. {
  253. int ret = -1;
  254. ip_addr->family = saddr->ss_family;
  255. ip_addr->nodeid = 0;
  256. if (saddr->ss_family == AF_INET) {
  257. struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
  258. memcpy(ip_addr->addr, &sin->sin_addr, sizeof(struct in_addr));
  259. ret = 0;
  260. }
  261. if (saddr->ss_family == AF_INET6) {
  262. struct sockaddr_in6 *sin = (struct sockaddr_in6 *)saddr;
  263. memcpy(ip_addr->addr, &sin->sin6_addr, sizeof(struct in6_addr));
  264. ret = 0;
  265. }
  266. return ret;
  267. }
  268. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN) || defined(OPENAIS_SOLARIS)
  269. int totemip_iface_check(struct totem_ip_address *bindnet,
  270. struct totem_ip_address *boundto,
  271. int *interface_up,
  272. int *interface_num)
  273. {
  274. #ifndef OPENAIS_SOLARIS
  275. #define NEXT_IFR(a) ((struct ifreq *)((u_char *)&(a)->ifr_addr +\
  276. ((a)->ifr_addr.sa_len ? (a)->ifr_addr.sa_len : sizeof((a)->ifr_addr))))
  277. #else
  278. #define NEXT_IFR(a) ((struct ifreq *)((u_char *)&(a)->ifr_addr +\
  279. sizeof((a)->ifr_addr)))
  280. #endif
  281. struct sockaddr_in *intf_addr_mask;
  282. struct sockaddr_storage bindnet_ss, intf_addr_ss;
  283. struct sockaddr_in *intf_addr_sin = (struct sockaddr_in *)&intf_addr_ss;
  284. struct sockaddr_in *bindnet_sin = (struct sockaddr_in *)&bindnet_ss;
  285. struct ifreq *ifr, *lifr;
  286. int id_fd;
  287. struct ifconf ifc;
  288. struct ifreq ifrb;
  289. int numreqs = 0;
  290. int res;
  291. int addrlen;
  292. *interface_up = 0;
  293. *interface_num = 0;
  294. totemip_totemip_to_sockaddr_convert(bindnet,
  295. 0, &bindnet_ss, &addrlen);
  296. /*
  297. * Generate list of local interfaces in ifc.ifc_req structure
  298. */
  299. id_fd = socket (AF_INET, SOCK_DGRAM, 0);
  300. ifc.ifc_buf = NULL;
  301. do {
  302. void *ifc_buf_tmp;
  303. numreqs += 32;
  304. ifc.ifc_len = sizeof (struct ifreq) * numreqs;
  305. ifc_buf_tmp = realloc (ifc.ifc_buf, ifc.ifc_len);
  306. if (ifc_buf_tmp == NULL) {
  307. close (id_fd);
  308. if (ifc.ifc_buf != NULL) {
  309. free (ifc.ifc_buf);
  310. }
  311. return -1;
  312. }
  313. ifc.ifc_buf = ifc_buf_tmp;
  314. res = ioctl (id_fd, SIOCGIFCONF, &ifc);
  315. if (res < 0) {
  316. close (id_fd);
  317. free (ifc.ifc_buf);
  318. return -1;
  319. }
  320. } while (ifc.ifc_len == sizeof (struct ifreq) * numreqs);
  321. res = -1;
  322. /*
  323. * Find interface address to bind to
  324. */
  325. lifr = (struct ifreq *)ifc.ifc_buf + (ifc.ifc_len / sizeof(*lifr));
  326. for (ifr = ifc.ifc_req; ifr < lifr; ifr = NEXT_IFR(ifr)) {
  327. strcpy(ifrb.ifr_name, ifr->ifr_name);
  328. /* Skip if no address set
  329. */
  330. if (ioctl(id_fd, SIOCGIFADDR, &ifrb) < 0)
  331. continue;
  332. memcpy(&intf_addr_ss, &ifrb.ifr_addr, sizeof(intf_addr_ss));
  333. if (intf_addr_sin->sin_family == AF_INET) {
  334. /* Retrieve mask
  335. */
  336. if (ioctl(id_fd, SIOCGIFNETMASK, &ifrb) < 0) {
  337. break;
  338. }
  339. intf_addr_mask = (struct sockaddr_in *)&ifrb.ifr_addr;
  340. if ( bindnet_sin->sin_family == AF_INET &&
  341. (intf_addr_sin->sin_addr.s_addr & intf_addr_mask->sin_addr.s_addr) ==
  342. (bindnet_sin->sin_addr.s_addr & intf_addr_mask->sin_addr.s_addr)) {
  343. totemip_copy(boundto, bindnet);
  344. memcpy(boundto->addr, &intf_addr_sin->sin_addr, sizeof(intf_addr_sin->sin_addr));
  345. /* Get inteface state
  346. */
  347. if (ioctl(id_fd, SIOCGIFFLAGS, &ifrb) < 0) {
  348. break;
  349. }
  350. *interface_up = ifrb.ifr_flags & IFF_UP;
  351. /* Get interface index
  352. */
  353. #ifdef SIOCGIFINDEX
  354. if (ioctl(id_fd, SIOCGIFINDEX, &ifrb) < 0) {
  355. break;
  356. }
  357. *interface_num = ifrb.ifr_index;
  358. #else
  359. *interface_num = if_nametoindex(ifrb.ifr_name);
  360. #endif
  361. res = 0;
  362. break; /* for */
  363. }
  364. }
  365. }
  366. if (ifc.ifc_buf != NULL) {
  367. free (ifc.ifc_buf);
  368. }
  369. close (id_fd);
  370. return (res);
  371. }
  372. #elif defined(OPENAIS_LINUX)
  373. static void parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
  374. {
  375. while (RTA_OK(rta, len)) {
  376. if (rta->rta_type <= max)
  377. tb[rta->rta_type] = rta;
  378. rta = RTA_NEXT(rta,len);
  379. }
  380. }
  381. int totemip_iface_check(struct totem_ip_address *bindnet,
  382. struct totem_ip_address *boundto,
  383. int *interface_up,
  384. int *interface_num)
  385. {
  386. int fd;
  387. struct {
  388. struct nlmsghdr nlh;
  389. struct rtgenmsg g;
  390. } req;
  391. struct sockaddr_nl nladdr;
  392. struct totem_ip_address ipaddr;
  393. static char rcvbuf[NETLINK_BUFSIZE];
  394. *interface_up = 0;
  395. *interface_num = 0;
  396. memset(&ipaddr, 0, sizeof(ipaddr));
  397. /* Make sure we preserve these */
  398. ipaddr.family = bindnet->family;
  399. ipaddr.nodeid = bindnet->nodeid;
  400. /* Ask netlink for a list of interface addresses */
  401. fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  402. if (fd <0)
  403. return -1;
  404. setsockopt(fd,SOL_SOCKET,SO_RCVBUF,&rcvbuf,sizeof(rcvbuf));
  405. memset(&nladdr, 0, sizeof(nladdr));
  406. nladdr.nl_family = AF_NETLINK;
  407. req.nlh.nlmsg_len = sizeof(req);
  408. req.nlh.nlmsg_type = RTM_GETADDR;
  409. req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
  410. req.nlh.nlmsg_pid = 0;
  411. req.nlh.nlmsg_seq = 1;
  412. req.g.rtgen_family = bindnet->family;
  413. if (sendto(fd, (void *)&req, sizeof(req), 0,
  414. (struct sockaddr*)&nladdr, sizeof(nladdr)) < 0) {
  415. close(fd);
  416. return -1;
  417. }
  418. /* Look through the return buffer for our address */
  419. while (1)
  420. {
  421. int status;
  422. struct nlmsghdr *h;
  423. struct iovec iov = { rcvbuf, sizeof(rcvbuf) };
  424. struct msghdr msg = {
  425. (void*)&nladdr, sizeof(nladdr),
  426. &iov, 1,
  427. NULL, 0,
  428. 0
  429. };
  430. status = recvmsg(fd, &msg, 0);
  431. if (!status) {
  432. close(fd);
  433. return -1;
  434. }
  435. h = (struct nlmsghdr *)rcvbuf;
  436. if (h->nlmsg_type == NLMSG_DONE)
  437. break;
  438. if (h->nlmsg_type == NLMSG_ERROR) {
  439. close(fd);
  440. return -1;
  441. }
  442. while (NLMSG_OK(h, status)) {
  443. if (h->nlmsg_type == RTM_NEWADDR) {
  444. struct ifaddrmsg *ifa = NLMSG_DATA(h);
  445. struct rtattr *tb[IFA_MAX+1];
  446. int len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa));
  447. int found_if = 0;
  448. memset(tb, 0, sizeof(tb));
  449. parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
  450. memcpy(ipaddr.addr, RTA_DATA(tb[IFA_ADDRESS]), TOTEMIP_ADDRLEN);
  451. if (totemip_equal(&ipaddr, bindnet))
  452. found_if = 1;
  453. /* If the address we have is an IPv4 network address, then
  454. substitute the actual IP address of this interface */
  455. if (!found_if && tb[IFA_BROADCAST] && ifa->ifa_family == AF_INET) {
  456. uint32_t network;
  457. uint32_t addr;
  458. uint32_t netmask = htonl(~((1<<(32-ifa->ifa_prefixlen))-1));
  459. memcpy(&network, RTA_DATA(tb[IFA_BROADCAST]), sizeof(uint32_t));
  460. memcpy(&addr, bindnet->addr, sizeof(uint32_t));
  461. if (addr == (network & netmask)) {
  462. memcpy(ipaddr.addr, RTA_DATA(tb[IFA_ADDRESS]), TOTEMIP_ADDRLEN);
  463. found_if = 1;
  464. }
  465. }
  466. if (found_if) {
  467. /* Found it - check I/F is UP */
  468. struct ifreq ifr;
  469. int ioctl_fd; /* Can't do ioctls on netlink FDs */
  470. ioctl_fd = socket(AF_INET, SOCK_STREAM, 0);
  471. if (ioctl_fd < 0) {
  472. close(fd);
  473. return -1;
  474. }
  475. memset(&ifr, 0, sizeof(ifr));
  476. ifr.ifr_ifindex = ifa->ifa_index;
  477. /* SIOCGIFFLAGS needs an interface name */
  478. status = ioctl(ioctl_fd, SIOCGIFNAME, &ifr);
  479. status = ioctl(ioctl_fd, SIOCGIFFLAGS, &ifr);
  480. if (status) {
  481. close(ioctl_fd);
  482. close(fd);
  483. return -1;
  484. }
  485. if (ifr.ifr_flags & IFF_UP)
  486. *interface_up = 1;
  487. *interface_num = ifa->ifa_index;
  488. close(ioctl_fd);
  489. goto finished;
  490. }
  491. }
  492. h = NLMSG_NEXT(h, status);
  493. }
  494. }
  495. finished:
  496. totemip_copy (boundto, &ipaddr);
  497. close(fd);
  498. return 0;
  499. }
  500. #endif /* OPENAIS_LINUX */