4
0

totemip.c 14 KB

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