ifaddrs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /* getifaddrs -- get names and addresses of all network interfaces
  2. Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <alloca.h>
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <ifaddrs.h>
  19. #include <net/if.h>
  20. #include <netinet/in.h>
  21. #include <netpacket/packet.h>
  22. #include <stdbool.h>
  23. #include <stdint.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <sys/ioctl.h>
  28. #include <sys/socket.h>
  29. #include <time.h>
  30. #include <unistd.h>
  31. #include "netlinkaccess.h"
  32. #ifndef __libc_use_alloca
  33. # define __libc_use_alloca(x) (x < __MAX_ALLOCA_CUTOFF)
  34. #endif
  35. #if __ASSUME_NETLINK_SUPPORT
  36. #ifdef __UCLIBC_SUPPORT_AI_ADDRCONFIG__
  37. /* struct to hold the data for one ifaddrs entry, so we can allocate
  38. everything at once. */
  39. struct ifaddrs_storage
  40. {
  41. struct ifaddrs ifa;
  42. union
  43. {
  44. /* Save space for the biggest of the four used sockaddr types and
  45. avoid a lot of casts. */
  46. struct sockaddr sa;
  47. struct sockaddr_ll sl;
  48. struct sockaddr_in s4;
  49. #ifdef __UCLIBC_HAS_IPV6__
  50. struct sockaddr_in6 s6;
  51. #endif
  52. } addr, netmask, broadaddr;
  53. char name[IF_NAMESIZE + 1];
  54. };
  55. #endif /* __UCLIBC_SUPPORT_AI_ADDRCONFIG__ */
  56. void
  57. __netlink_free_handle (struct netlink_handle *h)
  58. {
  59. struct netlink_res *ptr;
  60. ptr = h->nlm_list;
  61. while (ptr != NULL)
  62. {
  63. struct netlink_res *tmpptr;
  64. tmpptr = ptr->next;
  65. free (ptr); /* doesn't affect errno */
  66. ptr = tmpptr;
  67. }
  68. }
  69. static int
  70. __netlink_sendreq (struct netlink_handle *h, int type)
  71. {
  72. struct
  73. {
  74. struct nlmsghdr nlh;
  75. struct rtgenmsg g;
  76. } req;
  77. struct sockaddr_nl nladdr;
  78. if (h->seq == 0)
  79. h->seq = time (NULL);
  80. req.nlh.nlmsg_len = sizeof (req);
  81. req.nlh.nlmsg_type = type;
  82. req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
  83. req.nlh.nlmsg_pid = 0;
  84. req.nlh.nlmsg_seq = h->seq;
  85. req.g.rtgen_family = AF_UNSPEC;
  86. memset (&nladdr, '\0', sizeof (nladdr));
  87. nladdr.nl_family = AF_NETLINK;
  88. return TEMP_FAILURE_RETRY (sendto (h->fd, (void *) &req, sizeof (req), 0,
  89. (struct sockaddr *) &nladdr,
  90. sizeof (nladdr)));
  91. }
  92. int
  93. __netlink_request (struct netlink_handle *h, int type)
  94. {
  95. struct netlink_res *nlm_next;
  96. struct netlink_res **new_nlm_list;
  97. static volatile size_t buf_size = 0;
  98. size_t this_buf_size;
  99. char *buf;
  100. struct sockaddr_nl nladdr;
  101. struct nlmsghdr *nlmh;
  102. ssize_t read_len;
  103. bool done = false;
  104. bool use_malloc = false;
  105. if (__netlink_sendreq (h, type) < 0)
  106. return -1;
  107. if (buf_size)
  108. this_buf_size = buf_size;
  109. else {
  110. #ifdef PAGE_SIZE
  111. this_buf_size = PAGE_SIZE;
  112. #else
  113. this_buf_size = __pagesize;
  114. #endif
  115. }
  116. if (__libc_use_alloca (this_buf_size))
  117. buf = alloca (this_buf_size);
  118. else
  119. {
  120. buf = malloc (this_buf_size);
  121. if (buf != NULL)
  122. use_malloc = true;
  123. else
  124. goto out_fail;
  125. }
  126. struct iovec iov = { buf, this_buf_size };
  127. if (h->nlm_list != NULL)
  128. new_nlm_list = &h->end_ptr->next;
  129. else
  130. new_nlm_list = &h->nlm_list;
  131. while (! done)
  132. {
  133. struct msghdr msg =
  134. {
  135. (void *) &nladdr, sizeof (nladdr),
  136. &iov, 1,
  137. NULL, 0,
  138. 0
  139. };
  140. read_len = TEMP_FAILURE_RETRY (recvmsg (h->fd, &msg, 0));
  141. if (read_len < 0)
  142. goto out_fail;
  143. if (nladdr.nl_pid != 0)
  144. continue;
  145. if (__builtin_expect (msg.msg_flags & MSG_TRUNC, 0))
  146. {
  147. if (this_buf_size >= SIZE_MAX / 2)
  148. goto out_fail;
  149. nlm_next = *new_nlm_list;
  150. while (nlm_next != NULL)
  151. {
  152. struct netlink_res *tmpptr;
  153. tmpptr = nlm_next->next;
  154. free (nlm_next);
  155. nlm_next = tmpptr;
  156. }
  157. *new_nlm_list = NULL;
  158. if (__libc_use_alloca (2 * this_buf_size))
  159. buf = extend_alloca (buf, this_buf_size, 2 * this_buf_size);
  160. else
  161. {
  162. this_buf_size *= 2;
  163. char *new_buf = realloc (use_malloc ? buf : NULL, this_buf_size);
  164. if (new_buf == NULL)
  165. goto out_fail;
  166. new_buf = buf;
  167. use_malloc = true;
  168. }
  169. buf_size = this_buf_size;
  170. iov.iov_base = buf;
  171. iov.iov_len = this_buf_size;
  172. /* Increase sequence number, so that we can distinguish
  173. between old and new request messages. */
  174. h->seq++;
  175. if (__netlink_sendreq (h, type) < 0)
  176. goto out_fail;
  177. continue;
  178. }
  179. size_t count = 0;
  180. size_t remaining_len = read_len;
  181. for (nlmh = (struct nlmsghdr *) buf;
  182. NLMSG_OK (nlmh, remaining_len);
  183. nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, remaining_len))
  184. {
  185. if ((pid_t) nlmh->nlmsg_pid != h->pid
  186. || nlmh->nlmsg_seq != h->seq)
  187. continue;
  188. ++count;
  189. if (nlmh->nlmsg_type == NLMSG_DONE)
  190. {
  191. /* We found the end, leave the loop. */
  192. done = true;
  193. break;
  194. }
  195. if (nlmh->nlmsg_type == NLMSG_ERROR)
  196. {
  197. struct nlmsgerr *nlerr = (struct nlmsgerr *) NLMSG_DATA (nlmh);
  198. if (nlmh->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr)))
  199. errno = EIO;
  200. else
  201. errno = -nlerr->error;
  202. goto out_fail;
  203. }
  204. }
  205. /* If there was nothing with the expected nlmsg_pid and nlmsg_seq,
  206. there is no point to record it. */
  207. if (count == 0)
  208. continue;
  209. nlm_next = (struct netlink_res *) malloc (sizeof (struct netlink_res)
  210. + read_len);
  211. if (nlm_next == NULL)
  212. goto out_fail;
  213. nlm_next->next = NULL;
  214. nlm_next->nlh = memcpy (nlm_next + 1, buf, read_len);
  215. nlm_next->size = read_len;
  216. nlm_next->seq = h->seq;
  217. if (h->nlm_list == NULL)
  218. h->nlm_list = nlm_next;
  219. else
  220. h->end_ptr->next = nlm_next;
  221. h->end_ptr = nlm_next;
  222. }
  223. if (use_malloc)
  224. free (buf);
  225. return 0;
  226. out_fail:
  227. if (use_malloc)
  228. free (buf);
  229. return -1;
  230. }
  231. void
  232. __netlink_close (struct netlink_handle *h)
  233. {
  234. /* Don't modify errno. */
  235. int serrno = errno;
  236. close(h->fd);
  237. __set_errno(serrno);
  238. }
  239. /* Open a NETLINK socket. */
  240. int
  241. __netlink_open (struct netlink_handle *h)
  242. {
  243. struct sockaddr_nl nladdr;
  244. h->fd = socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  245. if (h->fd < 0)
  246. goto out;
  247. memset (&nladdr, '\0', sizeof (nladdr));
  248. nladdr.nl_family = AF_NETLINK;
  249. if (bind (h->fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) < 0)
  250. {
  251. close_and_out:
  252. __netlink_close (h);
  253. out:
  254. return -1;
  255. }
  256. /* Determine the ID the kernel assigned for this netlink connection.
  257. It is not necessarily the PID if there is more than one socket
  258. open. */
  259. socklen_t addr_len = sizeof (nladdr);
  260. if (getsockname (h->fd, (struct sockaddr *) &nladdr, &addr_len) < 0)
  261. goto close_and_out;
  262. h->pid = nladdr.nl_pid;
  263. return 0;
  264. }
  265. #ifdef __UCLIBC_SUPPORT_AI_ADDRCONFIG__
  266. /* We know the number of RTM_NEWLINK entries, so we reserve the first
  267. # of entries for this type. All RTM_NEWADDR entries have an index
  268. pointer to the RTM_NEWLINK entry. To find the entry, create
  269. a table to map kernel index entries to our index numbers.
  270. Since we get at first all RTM_NEWLINK entries, it can never happen
  271. that a RTM_NEWADDR index is not known to this map. */
  272. static int
  273. internal_function
  274. map_newlink (int idx, struct ifaddrs_storage *ifas, int *map, int max)
  275. {
  276. int i;
  277. for (i = 0; i < max; i++)
  278. {
  279. if (map[i] == -1)
  280. {
  281. map[i] = idx;
  282. if (i > 0)
  283. ifas[i - 1].ifa.ifa_next = &ifas[i].ifa;
  284. return i;
  285. }
  286. else if (map[i] == idx)
  287. return i;
  288. }
  289. /* This means interfaces changed inbetween the reading of the
  290. RTM_GETLINK and RTM_GETADDR information. We have to repeat
  291. everything. */
  292. return -1;
  293. }
  294. /* Create a linked list of `struct ifaddrs' structures, one for each
  295. network interface on the host machine. If successful, store the
  296. list in *IFAP and return 0. On errors, return -1 and set `errno'. */
  297. static int
  298. getifaddrs_internal (struct ifaddrs **ifap)
  299. {
  300. struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
  301. struct netlink_res *nlp;
  302. struct ifaddrs_storage *ifas;
  303. unsigned int i, newlink, newaddr, newaddr_idx;
  304. int *map_newlink_data;
  305. size_t ifa_data_size = 0; /* Size to allocate for all ifa_data. */
  306. char *ifa_data_ptr; /* Pointer to the unused part of memory for
  307. ifa_data. */
  308. int result = 0;
  309. if (ifap)
  310. *ifap = NULL;
  311. if (__netlink_open (&nh) < 0)
  312. {
  313. return -1;
  314. }
  315. /* Tell the kernel that we wish to get a list of all
  316. active interfaces, collect all data for every interface. */
  317. if (__netlink_request (&nh, RTM_GETLINK) < 0)
  318. {
  319. result = -1;
  320. goto exit_free;
  321. }
  322. /* Now ask the kernel for all addresses which are assigned
  323. to an interface and collect all data for every interface.
  324. Since we store the addresses after the interfaces in the
  325. list, we will later always find the interface before the
  326. corresponding addresses. */
  327. ++nh.seq;
  328. if (__netlink_request (&nh, RTM_GETADDR) < 0)
  329. {
  330. result = -1;
  331. goto exit_free;
  332. }
  333. /* Count all RTM_NEWLINK and RTM_NEWADDR entries to allocate
  334. enough memory. */
  335. newlink = newaddr = 0;
  336. for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
  337. {
  338. struct nlmsghdr *nlh;
  339. size_t size = nlp->size;
  340. if (nlp->nlh == NULL)
  341. continue;
  342. /* Walk through all entries we got from the kernel and look, which
  343. message type they contain. */
  344. for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
  345. {
  346. /* Check if the message is what we want. */
  347. if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
  348. continue;
  349. if (nlh->nlmsg_type == NLMSG_DONE)
  350. break; /* ok */
  351. if (nlh->nlmsg_type == RTM_NEWLINK)
  352. {
  353. /* A RTM_NEWLINK message can have IFLA_STATS data. We need to
  354. know the size before creating the list to allocate enough
  355. memory. */
  356. struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
  357. struct rtattr *rta = IFLA_RTA (ifim);
  358. size_t rtasize = IFLA_PAYLOAD (nlh);
  359. while (RTA_OK (rta, rtasize))
  360. {
  361. size_t rta_payload = RTA_PAYLOAD (rta);
  362. if (rta->rta_type == IFLA_STATS)
  363. {
  364. ifa_data_size += rta_payload;
  365. break;
  366. }
  367. else
  368. rta = RTA_NEXT (rta, rtasize);
  369. }
  370. ++newlink;
  371. }
  372. else if (nlh->nlmsg_type == RTM_NEWADDR)
  373. ++newaddr;
  374. }
  375. }
  376. /* Return if no interface is up. */
  377. if ((newlink + newaddr) == 0)
  378. goto exit_free;
  379. /* Allocate memory for all entries we have and initialize next
  380. pointer. */
  381. ifas = calloc (1, (newlink + newaddr) * sizeof (ifas[0]) + ifa_data_size);
  382. if (ifas == NULL)
  383. {
  384. result = -1;
  385. goto exit_free;
  386. }
  387. /* Table for mapping kernel index to entry in our list. */
  388. map_newlink_data = alloca (newlink * sizeof (int));
  389. memset (map_newlink_data, '\xff', newlink * sizeof (int));
  390. ifa_data_ptr = (char *) &ifas[newlink + newaddr];
  391. newaddr_idx = 0; /* Counter for newaddr index. */
  392. /* Walk through the list of data we got from the kernel. */
  393. for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
  394. {
  395. struct nlmsghdr *nlh;
  396. size_t size = nlp->size;
  397. if (nlp->nlh == NULL)
  398. continue;
  399. /* Walk through one message and look at the type: If it is our
  400. message, we need RTM_NEWLINK/RTM_NEWADDR and stop if we reach
  401. the end or we find the end marker (in this case we ignore the
  402. following data. */
  403. for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
  404. {
  405. int ifa_index = 0;
  406. /* Check if the message is the one we want */
  407. if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
  408. continue;
  409. if (nlh->nlmsg_type == NLMSG_DONE)
  410. break; /* ok */
  411. if (nlh->nlmsg_type == RTM_NEWLINK)
  412. {
  413. /* We found a new interface. Now extract everything from the
  414. interface data we got and need. */
  415. struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
  416. struct rtattr *rta = IFLA_RTA (ifim);
  417. size_t rtasize = IFLA_PAYLOAD (nlh);
  418. /* Interfaces are stored in the first "newlink" entries
  419. of our list, starting in the order as we got from the
  420. kernel. */
  421. ifa_index = map_newlink (ifim->ifi_index - 1, ifas,
  422. map_newlink_data, newlink);
  423. if (__builtin_expect (ifa_index == -1, 0))
  424. {
  425. try_again:
  426. result = -EAGAIN;
  427. free (ifas);
  428. goto exit_free;
  429. }
  430. ifas[ifa_index].ifa.ifa_flags = ifim->ifi_flags;
  431. while (RTA_OK (rta, rtasize))
  432. {
  433. char *rta_data = RTA_DATA (rta);
  434. size_t rta_payload = RTA_PAYLOAD (rta);
  435. switch (rta->rta_type)
  436. {
  437. case IFLA_ADDRESS:
  438. if (rta_payload <= sizeof (ifas[ifa_index].addr))
  439. {
  440. ifas[ifa_index].addr.sl.sll_family = AF_PACKET;
  441. memcpy (ifas[ifa_index].addr.sl.sll_addr,
  442. (char *) rta_data, rta_payload);
  443. ifas[ifa_index].addr.sl.sll_halen = rta_payload;
  444. ifas[ifa_index].addr.sl.sll_ifindex
  445. = ifim->ifi_index;
  446. ifas[ifa_index].addr.sl.sll_hatype = ifim->ifi_type;
  447. ifas[ifa_index].ifa.ifa_addr
  448. = &ifas[ifa_index].addr.sa;
  449. }
  450. break;
  451. case IFLA_BROADCAST:
  452. if (rta_payload <= sizeof (ifas[ifa_index].broadaddr))
  453. {
  454. ifas[ifa_index].broadaddr.sl.sll_family = AF_PACKET;
  455. memcpy (ifas[ifa_index].broadaddr.sl.sll_addr,
  456. (char *) rta_data, rta_payload);
  457. ifas[ifa_index].broadaddr.sl.sll_halen = rta_payload;
  458. ifas[ifa_index].broadaddr.sl.sll_ifindex
  459. = ifim->ifi_index;
  460. ifas[ifa_index].broadaddr.sl.sll_hatype
  461. = ifim->ifi_type;
  462. ifas[ifa_index].ifa.ifa_broadaddr
  463. = &ifas[ifa_index].broadaddr.sa;
  464. }
  465. break;
  466. case IFLA_IFNAME: /* Name of Interface */
  467. if ((rta_payload + 1) <= sizeof (ifas[ifa_index].name))
  468. {
  469. ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
  470. *(char *) mempcpy (ifas[ifa_index].name, rta_data,
  471. rta_payload) = '\0';
  472. }
  473. break;
  474. case IFLA_STATS: /* Statistics of Interface */
  475. ifas[ifa_index].ifa.ifa_data = ifa_data_ptr;
  476. ifa_data_ptr += rta_payload;
  477. memcpy (ifas[ifa_index].ifa.ifa_data, rta_data,
  478. rta_payload);
  479. break;
  480. case IFLA_UNSPEC:
  481. break;
  482. case IFLA_MTU:
  483. break;
  484. case IFLA_LINK:
  485. break;
  486. case IFLA_QDISC:
  487. break;
  488. default:
  489. break;
  490. }
  491. rta = RTA_NEXT (rta, rtasize);
  492. }
  493. }
  494. else if (nlh->nlmsg_type == RTM_NEWADDR)
  495. {
  496. struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlh);
  497. struct rtattr *rta = IFA_RTA (ifam);
  498. size_t rtasize = IFA_PAYLOAD (nlh);
  499. /* New Addresses are stored in the order we got them from
  500. the kernel after the interfaces. Theoretically it is possible
  501. that we have holes in the interface part of the list,
  502. but we always have already the interface for this address. */
  503. ifa_index = newlink + newaddr_idx;
  504. int idx = map_newlink (ifam->ifa_index - 1, ifas,
  505. map_newlink_data, newlink);
  506. if (__builtin_expect (idx == -1, 0))
  507. goto try_again;
  508. ifas[ifa_index].ifa.ifa_flags = ifas[idx].ifa.ifa_flags;
  509. if (ifa_index > 0)
  510. ifas[ifa_index - 1].ifa.ifa_next = &ifas[ifa_index].ifa;
  511. ++newaddr_idx;
  512. while (RTA_OK (rta, rtasize))
  513. {
  514. char *rta_data = RTA_DATA (rta);
  515. size_t rta_payload = RTA_PAYLOAD (rta);
  516. switch (rta->rta_type)
  517. {
  518. case IFA_ADDRESS:
  519. {
  520. struct sockaddr *sa;
  521. if (ifas[ifa_index].ifa.ifa_addr != NULL)
  522. {
  523. /* In a point-to-poing network IFA_ADDRESS
  524. contains the destination address, local
  525. address is supplied in IFA_LOCAL attribute.
  526. destination address and broadcast address
  527. are stored in an union, so it doesn't matter
  528. which name we use. */
  529. ifas[ifa_index].ifa.ifa_broadaddr
  530. = &ifas[ifa_index].broadaddr.sa;
  531. sa = &ifas[ifa_index].broadaddr.sa;
  532. }
  533. else
  534. {
  535. ifas[ifa_index].ifa.ifa_addr
  536. = &ifas[ifa_index].addr.sa;
  537. sa = &ifas[ifa_index].addr.sa;
  538. }
  539. sa->sa_family = ifam->ifa_family;
  540. switch (ifam->ifa_family)
  541. {
  542. case AF_INET:
  543. /* Size must match that of an address for IPv4. */
  544. if (rta_payload == 4)
  545. memcpy (&((struct sockaddr_in *) sa)->sin_addr,
  546. rta_data, rta_payload);
  547. break;
  548. #ifdef __UCLIBC_HAS_IPV6__
  549. case AF_INET6:
  550. /* Size must match that of an address for IPv6. */
  551. if (rta_payload == 16)
  552. {
  553. memcpy (&((struct sockaddr_in6 *) sa)->sin6_addr,
  554. rta_data, rta_payload);
  555. if (IN6_IS_ADDR_LINKLOCAL (rta_data)
  556. || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
  557. ((struct sockaddr_in6 *) sa)->sin6_scope_id
  558. = ifam->ifa_index;
  559. }
  560. break;
  561. #endif
  562. default:
  563. if (rta_payload <= sizeof (ifas[ifa_index].addr))
  564. memcpy (sa->sa_data, rta_data, rta_payload);
  565. break;
  566. }
  567. }
  568. break;
  569. case IFA_LOCAL:
  570. if (ifas[ifa_index].ifa.ifa_addr != NULL)
  571. {
  572. /* If ifa_addr is set and we get IFA_LOCAL,
  573. assume we have a point-to-point network.
  574. Move address to correct field. */
  575. ifas[ifa_index].broadaddr = ifas[ifa_index].addr;
  576. ifas[ifa_index].ifa.ifa_broadaddr
  577. = &ifas[ifa_index].broadaddr.sa;
  578. memset (&ifas[ifa_index].addr, '\0',
  579. sizeof (ifas[ifa_index].addr));
  580. }
  581. ifas[ifa_index].ifa.ifa_addr = &ifas[ifa_index].addr.sa;
  582. ifas[ifa_index].ifa.ifa_addr->sa_family
  583. = ifam->ifa_family;
  584. switch (ifam->ifa_family)
  585. {
  586. case AF_INET:
  587. /* Size must match that of an address for IPv4. */
  588. if (rta_payload == 4)
  589. memcpy (&ifas[ifa_index].addr.s4.sin_addr,
  590. rta_data, rta_payload);
  591. break;
  592. #ifdef __UCLIBC_HAS_IPV6__
  593. case AF_INET6:
  594. /* Size must match that of an address for IPv6. */
  595. if (rta_payload == 16)
  596. {
  597. memcpy (&ifas[ifa_index].addr.s6.sin6_addr,
  598. rta_data, rta_payload);
  599. if (IN6_IS_ADDR_LINKLOCAL (rta_data)
  600. || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
  601. ifas[ifa_index].addr.s6.sin6_scope_id =
  602. ifam->ifa_index;
  603. }
  604. break;
  605. #endif
  606. default:
  607. if (rta_payload <= sizeof (ifas[ifa_index].addr))
  608. memcpy (ifas[ifa_index].addr.sa.sa_data,
  609. rta_data, rta_payload);
  610. break;
  611. }
  612. break;
  613. case IFA_BROADCAST:
  614. /* We get IFA_BROADCAST, so IFA_LOCAL was too much. */
  615. if (ifas[ifa_index].ifa.ifa_broadaddr != NULL)
  616. memset (&ifas[ifa_index].broadaddr, '\0',
  617. sizeof (ifas[ifa_index].broadaddr));
  618. ifas[ifa_index].ifa.ifa_broadaddr
  619. = &ifas[ifa_index].broadaddr.sa;
  620. ifas[ifa_index].ifa.ifa_broadaddr->sa_family
  621. = ifam->ifa_family;
  622. switch (ifam->ifa_family)
  623. {
  624. case AF_INET:
  625. /* Size must match that of an address for IPv4. */
  626. if (rta_payload == 4)
  627. memcpy (&ifas[ifa_index].broadaddr.s4.sin_addr,
  628. rta_data, rta_payload);
  629. break;
  630. #ifdef __UCLIBC_HAS_IPV6__
  631. case AF_INET6:
  632. /* Size must match that of an address for IPv6. */
  633. if (rta_payload == 16)
  634. {
  635. memcpy (&ifas[ifa_index].broadaddr.s6.sin6_addr,
  636. rta_data, rta_payload);
  637. if (IN6_IS_ADDR_LINKLOCAL (rta_data)
  638. || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
  639. ifas[ifa_index].broadaddr.s6.sin6_scope_id
  640. = ifam->ifa_index;
  641. }
  642. break;
  643. #endif
  644. default:
  645. if (rta_payload <= sizeof (ifas[ifa_index].addr))
  646. memcpy (&ifas[ifa_index].broadaddr.sa.sa_data,
  647. rta_data, rta_payload);
  648. break;
  649. }
  650. break;
  651. case IFA_LABEL:
  652. if (rta_payload + 1 <= sizeof (ifas[ifa_index].name))
  653. {
  654. ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
  655. *(char *) mempcpy (ifas[ifa_index].name, rta_data,
  656. rta_payload) = '\0';
  657. }
  658. else
  659. abort ();
  660. break;
  661. case IFA_UNSPEC:
  662. break;
  663. case IFA_CACHEINFO:
  664. break;
  665. default:
  666. break;
  667. }
  668. rta = RTA_NEXT (rta, rtasize);
  669. }
  670. /* If we didn't get the interface name with the
  671. address, use the name from the interface entry. */
  672. if (ifas[ifa_index].ifa.ifa_name == NULL)
  673. {
  674. int idx = map_newlink (ifam->ifa_index - 1, ifas,
  675. map_newlink_data, newlink);
  676. if (__builtin_expect (idx == -1, 0))
  677. goto try_again;
  678. ifas[ifa_index].ifa.ifa_name = ifas[idx].ifa.ifa_name;
  679. }
  680. /* Calculate the netmask. */
  681. if (ifas[ifa_index].ifa.ifa_addr
  682. && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_UNSPEC
  683. && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_PACKET)
  684. {
  685. uint32_t max_prefixlen = 0;
  686. char *cp = NULL;
  687. ifas[ifa_index].ifa.ifa_netmask
  688. = &ifas[ifa_index].netmask.sa;
  689. switch (ifas[ifa_index].ifa.ifa_addr->sa_family)
  690. {
  691. case AF_INET:
  692. cp = (char *) &ifas[ifa_index].netmask.s4.sin_addr;
  693. max_prefixlen = 32;
  694. break;
  695. #ifdef __UCLIBC_HAS_IPV6__
  696. case AF_INET6:
  697. cp = (char *) &ifas[ifa_index].netmask.s6.sin6_addr;
  698. max_prefixlen = 128;
  699. break;
  700. #endif
  701. }
  702. ifas[ifa_index].ifa.ifa_netmask->sa_family
  703. = ifas[ifa_index].ifa.ifa_addr->sa_family;
  704. if (cp != NULL)
  705. {
  706. char c;
  707. unsigned int preflen;
  708. if ((max_prefixlen > 0) &&
  709. (ifam->ifa_prefixlen > max_prefixlen))
  710. preflen = max_prefixlen;
  711. else
  712. preflen = ifam->ifa_prefixlen;
  713. for (i = 0; i < (preflen / 8); i++)
  714. *cp++ = 0xff;
  715. c = 0xff;
  716. c <<= (8 - (preflen % 8));
  717. *cp = c;
  718. }
  719. }
  720. }
  721. }
  722. }
  723. assert (ifa_data_ptr <= (char *) &ifas[newlink + newaddr] + ifa_data_size);
  724. if (newaddr_idx > 0)
  725. {
  726. for (i = 0; i < newlink; ++i)
  727. if (map_newlink_data[i] == -1)
  728. {
  729. /* We have fewer links then we anticipated. Adjust the
  730. forward pointer to the first address entry. */
  731. ifas[i - 1].ifa.ifa_next = &ifas[newlink].ifa;
  732. }
  733. if (i == 0 && newlink > 0)
  734. /* No valid link, but we allocated memory. We have to
  735. populate the first entry. */
  736. memmove (ifas, &ifas[newlink], sizeof (struct ifaddrs_storage));
  737. }
  738. if (ifap != NULL)
  739. *ifap = &ifas[0].ifa;
  740. exit_free:
  741. __netlink_free_handle (&nh);
  742. __netlink_close (&nh);
  743. return result;
  744. }
  745. /* Create a linked list of `struct ifaddrs' structures, one for each
  746. network interface on the host machine. If successful, store the
  747. list in *IFAP and return 0. On errors, return -1 and set `errno'. */
  748. int
  749. getifaddrs (struct ifaddrs **ifap)
  750. {
  751. int res;
  752. do
  753. res = getifaddrs_internal (ifap);
  754. while (res == -EAGAIN);
  755. return res;
  756. }
  757. libc_hidden_def(getifaddrs)
  758. void
  759. freeifaddrs (struct ifaddrs *ifa)
  760. {
  761. free (ifa);
  762. }
  763. libc_hidden_def(freeifaddrs)
  764. #endif /* __UCLIBC_SUPPORT_AI_ADDRCONFIG__ */
  765. #endif /* __ASSUME_NETLINK_SUPPORT */