totemip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 <netinet/in.h>
  36. #include <string.h>
  37. #include <stdio.h>
  38. #include <errno.h>
  39. #include <assert.h>
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. #include <netdb.h>
  43. #include <sys/ioctl.h>
  44. #include <arpa/inet.h>
  45. #include <net/if.h>
  46. /* ARGH!! I hate netlink */
  47. #include <asm/types.h>
  48. #include <linux/rtnetlink.h>
  49. #include "totemip.h"
  50. #include "swab.h"
  51. #define LOCALHOST_IPV4 "127.0.0.1"
  52. #define LOCALHOST_IPV6 "::1"
  53. #define NETLINK_BUFSIZE 16384
  54. /* Compare two addresses */
  55. int totemip_equal(struct totem_ip_address *addr1, struct totem_ip_address *addr2)
  56. {
  57. int addrlen = 0;
  58. if (addr1->family != addr2->family)
  59. return 0;
  60. if (addr1->family == AF_INET) {
  61. addrlen = sizeof(struct in_addr);
  62. }
  63. if (addr1->family == AF_INET6) {
  64. addrlen = sizeof(struct in6_addr);
  65. }
  66. assert(addrlen);
  67. if (memcmp(addr1->addr, addr2->addr, addrlen) == 0)
  68. return 1;
  69. else
  70. return 0;
  71. }
  72. /* Copy a totem_ip_address */
  73. void totemip_copy(struct totem_ip_address *addr1, struct totem_ip_address *addr2)
  74. {
  75. memcpy(addr1, addr2, sizeof(struct totem_ip_address));
  76. }
  77. void totemip_copy_endian_convert(struct totem_ip_address *addr1, struct totem_ip_address *addr2)
  78. {
  79. addr1->nodeid = swab32(addr2->nodeid);
  80. addr1->family = swab16(addr2->family);
  81. memcpy(addr1->addr, addr2->addr, TOTEMIP_ADDRLEN);
  82. }
  83. /* For sorting etc. params are void * for qsort's benefit */
  84. int totemip_compare(const void *a, const void *b)
  85. {
  86. int i;
  87. const struct totem_ip_address *addr1 = a;
  88. const struct totem_ip_address *addr2 = b;
  89. struct in6_addr *sin6a;
  90. struct in6_addr *sin6b;
  91. if (addr1->family != addr2->family)
  92. return (addr1->family > addr2->family);
  93. if (addr1->family == AF_INET) {
  94. struct in_addr *in1 = (struct in_addr *)addr1->addr;
  95. struct in_addr *in2 = (struct in_addr *)addr2->addr;
  96. /* A bit clunky but avoids sign problems */
  97. if (in1->s_addr == in2->s_addr)
  98. return 0;
  99. if (htonl(in1->s_addr) < htonl(in2->s_addr))
  100. return -1;
  101. else
  102. return +1;
  103. }
  104. /* Compare IPv6 addresses */
  105. sin6a = (struct in6_addr *)addr1->addr;
  106. sin6b = (struct in6_addr *)addr2->addr;
  107. /* Remember, addresses are in big-endian format.
  108. We compare 16bits at a time rather than 32 to avoid sign problems */
  109. for (i = 0; i < 8; i++) {
  110. int res = htons(sin6a->s6_addr16[i]) -
  111. htons(sin6b->s6_addr16[i]);
  112. if (res) {
  113. return res;
  114. }
  115. }
  116. return 0;
  117. }
  118. /* Build a localhost totem_ip_address */
  119. int totemip_localhost(int family, struct totem_ip_address *localhost)
  120. {
  121. char *addr_text;
  122. memset (localhost, 0, sizeof (struct totem_ip_address));
  123. if (family == AF_INET)
  124. addr_text = LOCALHOST_IPV4;
  125. else
  126. addr_text = LOCALHOST_IPV6;
  127. if (inet_pton(family, addr_text, (char *)localhost->addr) <= 0)
  128. return -1;
  129. return 0;
  130. }
  131. int totemip_localhost_check(struct totem_ip_address *addr)
  132. {
  133. struct totem_ip_address localhost;
  134. if (totemip_localhost(addr->family, &localhost))
  135. return 0;
  136. return totemip_equal(addr, &localhost);
  137. }
  138. const char *totemip_print(struct totem_ip_address *addr)
  139. {
  140. static char buf[INET6_ADDRSTRLEN];
  141. return inet_ntop(addr->family, addr->addr, buf, sizeof(buf));
  142. }
  143. /* Make a totem_ip_address into a usable sockaddr_storage */
  144. int totemip_totemip_to_sockaddr_convert(struct totem_ip_address *ip_addr,
  145. uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
  146. {
  147. int ret = -1;
  148. if (ip_addr->family == AF_INET) {
  149. struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
  150. memset(sin, 0, sizeof(struct sockaddr_in));
  151. sin->sin_family = ip_addr->family;
  152. sin->sin_port = port;
  153. memcpy(&sin->sin_addr, ip_addr->addr, sizeof(struct in_addr));
  154. *addrlen = sizeof(struct sockaddr_in);
  155. ret = 0;
  156. }
  157. if (ip_addr->family == AF_INET6) {
  158. struct sockaddr_in6 *sin = (struct sockaddr_in6 *)saddr;
  159. memset(sin, 0, sizeof(struct sockaddr_in6));
  160. sin->sin6_family = ip_addr->family;
  161. sin->sin6_port = port;
  162. sin->sin6_scope_id = 2;
  163. memcpy(&sin->sin6_addr, ip_addr->addr, sizeof(struct in6_addr));
  164. *addrlen = sizeof(struct sockaddr_in6);
  165. ret = 0;
  166. }
  167. return ret;
  168. }
  169. /* Converts an address string string into a totem_ip_address */
  170. int totemip_parse(struct totem_ip_address *totemip, char *addr)
  171. {
  172. struct addrinfo *ainfo;
  173. struct addrinfo ahints;
  174. struct sockaddr_in *sa;
  175. struct sockaddr_in6 *sa6;
  176. int ret;
  177. memset(&ahints, 0, sizeof(ahints));
  178. ahints.ai_socktype = SOCK_DGRAM;
  179. ahints.ai_protocol = IPPROTO_UDP;
  180. /* Lookup the nodename address */
  181. ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
  182. if (ret)
  183. return -errno;
  184. sa = (struct sockaddr_in *)ainfo->ai_addr;
  185. sa6 = (struct sockaddr_in6 *)ainfo->ai_addr;
  186. totemip->family = ainfo->ai_family;
  187. if (ainfo->ai_family == AF_INET)
  188. memcpy(totemip->addr, &sa->sin_addr, sizeof(struct in_addr));
  189. else
  190. memcpy(totemip->addr, &sa6->sin6_addr, sizeof(struct in6_addr));
  191. return 0;
  192. }
  193. /* Make a sockaddr_* into a totem_ip_address */
  194. int totemip_sockaddr_to_totemip_convert(struct sockaddr_storage *saddr, struct totem_ip_address *ip_addr)
  195. {
  196. int ret = -1;
  197. ip_addr->family = saddr->ss_family;
  198. ip_addr->nodeid = 0;
  199. if (saddr->ss_family == AF_INET) {
  200. struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
  201. memcpy(ip_addr->addr, &sin->sin_addr, sizeof(struct in_addr));
  202. ret = 0;
  203. }
  204. if (saddr->ss_family == AF_INET6) {
  205. struct sockaddr_in6 *sin = (struct sockaddr_in6 *)saddr;
  206. memcpy(ip_addr->addr, &sin->sin6_addr, sizeof(struct in6_addr));
  207. ret = 0;
  208. }
  209. return ret;
  210. }
  211. static void parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
  212. {
  213. while (RTA_OK(rta, len)) {
  214. if (rta->rta_type <= max)
  215. tb[rta->rta_type] = rta;
  216. rta = RTA_NEXT(rta,len);
  217. }
  218. }
  219. int totemip_iface_check(struct totem_ip_address *bindnet,
  220. struct totem_ip_address *boundto,
  221. int *interface_up,
  222. int *interface_num)
  223. {
  224. int fd;
  225. struct {
  226. struct nlmsghdr nlh;
  227. struct rtgenmsg g;
  228. } req;
  229. struct sockaddr_nl nladdr;
  230. struct totem_ip_address ipaddr;
  231. static char rcvbuf[NETLINK_BUFSIZE];
  232. *interface_up = 0;
  233. *interface_num = 0;
  234. memset(&ipaddr, 0, sizeof(ipaddr));
  235. /* Make sure we preserve these */
  236. ipaddr.family = bindnet->family;
  237. ipaddr.nodeid = bindnet->nodeid;
  238. /* Ask netlink for a list of interface addresses */
  239. fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  240. if (fd <0)
  241. return -1;
  242. setsockopt(fd,SOL_SOCKET,SO_RCVBUF,&rcvbuf,sizeof(rcvbuf));
  243. memset(&nladdr, 0, sizeof(nladdr));
  244. nladdr.nl_family = AF_NETLINK;
  245. req.nlh.nlmsg_len = sizeof(req);
  246. req.nlh.nlmsg_type = RTM_GETADDR;
  247. req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
  248. req.nlh.nlmsg_pid = 0;
  249. req.nlh.nlmsg_seq = 1;
  250. req.g.rtgen_family = bindnet->family;
  251. if (sendto(fd, (void *)&req, sizeof(req), 0,
  252. (struct sockaddr*)&nladdr, sizeof(nladdr)) < 0) {
  253. close(fd);
  254. return -1;
  255. }
  256. /* Look through the return buffer for our address */
  257. while (1)
  258. {
  259. int status;
  260. struct nlmsghdr *h;
  261. struct iovec iov = { rcvbuf, sizeof(rcvbuf) };
  262. struct msghdr msg = {
  263. (void*)&nladdr, sizeof(nladdr),
  264. &iov, 1,
  265. NULL, 0,
  266. 0
  267. };
  268. status = recvmsg(fd, &msg, 0);
  269. if (!status) {
  270. close(fd);
  271. return -1;
  272. }
  273. h = (struct nlmsghdr *)rcvbuf;
  274. if (h->nlmsg_type == NLMSG_DONE)
  275. break;
  276. if (h->nlmsg_type == NLMSG_ERROR) {
  277. close(fd);
  278. return -1;
  279. }
  280. while (NLMSG_OK(h, status)) {
  281. if (h->nlmsg_type == RTM_NEWADDR) {
  282. struct ifaddrmsg *ifa = NLMSG_DATA(h);
  283. struct rtattr *tb[IFA_MAX+1];
  284. int len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa));
  285. int found_if = 0;
  286. memset(tb, 0, sizeof(tb));
  287. parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
  288. memcpy(ipaddr.addr, RTA_DATA(tb[IFA_ADDRESS]), TOTEMIP_ADDRLEN);
  289. if (totemip_equal(&ipaddr, bindnet))
  290. found_if = 1;
  291. /* If the address we have is an IPv4 network address, then
  292. substitute the actual IP address of this interface */
  293. if (!found_if && tb[IFA_BROADCAST] && ifa->ifa_family == AF_INET) {
  294. uint32_t network;
  295. uint32_t addr;
  296. uint32_t netmask = htonl(~((1<<(32-ifa->ifa_prefixlen))-1));
  297. memcpy(&network, RTA_DATA(tb[IFA_BROADCAST]), sizeof(uint32_t));
  298. memcpy(&addr, bindnet->addr, sizeof(uint32_t));
  299. if (addr == (network & netmask)) {
  300. memcpy(ipaddr.addr, RTA_DATA(tb[IFA_ADDRESS]), TOTEMIP_ADDRLEN);
  301. found_if = 1;
  302. }
  303. }
  304. if (found_if) {
  305. /* Found it - check I/F is UP */
  306. struct ifreq ifr;
  307. int ioctl_fd; /* Can't do ioctls on netlink FDs */
  308. ioctl_fd = socket(AF_INET, SOCK_STREAM, 0);
  309. if (ioctl_fd < 0) {
  310. close(fd);
  311. return -1;
  312. }
  313. memset(&ifr, 0, sizeof(ifr));
  314. ifr.ifr_ifindex = ifa->ifa_index;
  315. /* SIOCGIFFLAGS needs an interface name */
  316. status = ioctl(ioctl_fd, SIOCGIFNAME, &ifr);
  317. status = ioctl(ioctl_fd, SIOCGIFFLAGS, &ifr);
  318. if (status) {
  319. close(ioctl_fd);
  320. close(fd);
  321. return -1;
  322. }
  323. if (ifr.ifr_flags & IFF_UP)
  324. *interface_up = 1;
  325. *interface_num = ifa->ifa_index;
  326. close(ioctl_fd);
  327. goto finished;
  328. }
  329. }
  330. h = NLMSG_NEXT(h, status);
  331. }
  332. }
  333. finished:
  334. totemip_copy (boundto, &ipaddr);
  335. close(fd);
  336. return 0;
  337. }