ifaddrs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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 should never be reached. If this will be reached, we have
  290. a very big problem. */
  291. abort ();
  292. }
  293. /* Create a linked list of `struct ifaddrs' structures, one for each
  294. network interface on the host machine. If successful, store the
  295. list in *IFAP and return 0. On errors, return -1 and set `errno'. */
  296. int
  297. getifaddrs (struct ifaddrs **ifap)
  298. {
  299. struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
  300. struct netlink_res *nlp;
  301. struct ifaddrs_storage *ifas;
  302. unsigned int i, newlink, newaddr, newaddr_idx;
  303. int *map_newlink_data;
  304. size_t ifa_data_size = 0; /* Size to allocate for all ifa_data. */
  305. char *ifa_data_ptr; /* Pointer to the unused part of memory for
  306. ifa_data. */
  307. int result = 0;
  308. if (ifap)
  309. *ifap = NULL;
  310. if (__netlink_open (&nh) < 0)
  311. {
  312. return -1;
  313. }
  314. /* Tell the kernel that we wish to get a list of all
  315. active interfaces, collect all data for every interface. */
  316. if (__netlink_request (&nh, RTM_GETLINK) < 0)
  317. {
  318. result = -1;
  319. goto exit_free;
  320. }
  321. /* Now ask the kernel for all addresses which are assigned
  322. to an interface and collect all data for every interface.
  323. Since we store the addresses after the interfaces in the
  324. list, we will later always find the interface before the
  325. corresponding addresses. */
  326. ++nh.seq;
  327. if (__netlink_request (&nh, RTM_GETADDR) < 0)
  328. {
  329. result = -1;
  330. goto exit_free;
  331. }
  332. /* Count all RTM_NEWLINK and RTM_NEWADDR entries to allocate
  333. enough memory. */
  334. newlink = newaddr = 0;
  335. for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
  336. {
  337. struct nlmsghdr *nlh;
  338. size_t size = nlp->size;
  339. if (nlp->nlh == NULL)
  340. continue;
  341. /* Walk through all entries we got from the kernel and look, which
  342. message type they contain. */
  343. for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
  344. {
  345. /* Check if the message is what we want. */
  346. if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
  347. continue;
  348. if (nlh->nlmsg_type == NLMSG_DONE)
  349. break; /* ok */
  350. if (nlh->nlmsg_type == RTM_NEWLINK)
  351. {
  352. /* A RTM_NEWLINK message can have IFLA_STATS data. We need to
  353. know the size before creating the list to allocate enough
  354. memory. */
  355. struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
  356. struct rtattr *rta = IFLA_RTA (ifim);
  357. size_t rtasize = IFLA_PAYLOAD (nlh);
  358. while (RTA_OK (rta, rtasize))
  359. {
  360. size_t rta_payload = RTA_PAYLOAD (rta);
  361. if (rta->rta_type == IFLA_STATS)
  362. {
  363. ifa_data_size += rta_payload;
  364. break;
  365. }
  366. else
  367. rta = RTA_NEXT (rta, rtasize);
  368. }
  369. ++newlink;
  370. }
  371. else if (nlh->nlmsg_type == RTM_NEWADDR)
  372. ++newaddr;
  373. }
  374. }
  375. /* Return if no interface is up. */
  376. if ((newlink + newaddr) == 0)
  377. goto exit_free;
  378. /* Allocate memory for all entries we have and initialize next
  379. pointer. */
  380. ifas = calloc (1, (newlink + newaddr) * sizeof (ifas[0]) + ifa_data_size);
  381. if (ifas == NULL)
  382. {
  383. result = -1;
  384. goto exit_free;
  385. }
  386. /* Table for mapping kernel index to entry in our list. */
  387. map_newlink_data = alloca (newlink * sizeof (int));
  388. memset (map_newlink_data, '\xff', newlink * sizeof (int));
  389. ifa_data_ptr = (char *) &ifas[newlink + newaddr];
  390. newaddr_idx = 0; /* Counter for newaddr index. */
  391. /* Walk through the list of data we got from the kernel. */
  392. for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
  393. {
  394. struct nlmsghdr *nlh;
  395. size_t size = nlp->size;
  396. if (nlp->nlh == NULL)
  397. continue;
  398. /* Walk through one message and look at the type: If it is our
  399. message, we need RTM_NEWLINK/RTM_NEWADDR and stop if we reach
  400. the end or we find the end marker (in this case we ignore the
  401. following data. */
  402. for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
  403. {
  404. int ifa_index = 0;
  405. /* Check if the message is the one we want */
  406. if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
  407. continue;
  408. if (nlh->nlmsg_type == NLMSG_DONE)
  409. break; /* ok */
  410. if (nlh->nlmsg_type == RTM_NEWLINK)
  411. {
  412. /* We found a new interface. Now extract everything from the
  413. interface data we got and need. */
  414. struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
  415. struct rtattr *rta = IFLA_RTA (ifim);
  416. size_t rtasize = IFLA_PAYLOAD (nlh);
  417. /* Interfaces are stored in the first "newlink" entries
  418. of our list, starting in the order as we got from the
  419. kernel. */
  420. ifa_index = map_newlink (ifim->ifi_index - 1, ifas,
  421. map_newlink_data, newlink);
  422. ifas[ifa_index].ifa.ifa_flags = ifim->ifi_flags;
  423. while (RTA_OK (rta, rtasize))
  424. {
  425. char *rta_data = RTA_DATA (rta);
  426. size_t rta_payload = RTA_PAYLOAD (rta);
  427. switch (rta->rta_type)
  428. {
  429. case IFLA_ADDRESS:
  430. if (rta_payload <= sizeof (ifas[ifa_index].addr))
  431. {
  432. ifas[ifa_index].addr.sl.sll_family = AF_PACKET;
  433. memcpy (ifas[ifa_index].addr.sl.sll_addr,
  434. (char *) rta_data, rta_payload);
  435. ifas[ifa_index].addr.sl.sll_halen = rta_payload;
  436. ifas[ifa_index].addr.sl.sll_ifindex
  437. = ifim->ifi_index;
  438. ifas[ifa_index].addr.sl.sll_hatype = ifim->ifi_type;
  439. ifas[ifa_index].ifa.ifa_addr
  440. = &ifas[ifa_index].addr.sa;
  441. }
  442. break;
  443. case IFLA_BROADCAST:
  444. if (rta_payload <= sizeof (ifas[ifa_index].broadaddr))
  445. {
  446. ifas[ifa_index].broadaddr.sl.sll_family = AF_PACKET;
  447. memcpy (ifas[ifa_index].broadaddr.sl.sll_addr,
  448. (char *) rta_data, rta_payload);
  449. ifas[ifa_index].broadaddr.sl.sll_halen = rta_payload;
  450. ifas[ifa_index].broadaddr.sl.sll_ifindex
  451. = ifim->ifi_index;
  452. ifas[ifa_index].broadaddr.sl.sll_hatype
  453. = ifim->ifi_type;
  454. ifas[ifa_index].ifa.ifa_broadaddr
  455. = &ifas[ifa_index].broadaddr.sa;
  456. }
  457. break;
  458. case IFLA_IFNAME: /* Name of Interface */
  459. if ((rta_payload + 1) <= sizeof (ifas[ifa_index].name))
  460. {
  461. ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
  462. *(char *) mempcpy (ifas[ifa_index].name, rta_data,
  463. rta_payload) = '\0';
  464. }
  465. break;
  466. case IFLA_STATS: /* Statistics of Interface */
  467. ifas[ifa_index].ifa.ifa_data = ifa_data_ptr;
  468. ifa_data_ptr += rta_payload;
  469. memcpy (ifas[ifa_index].ifa.ifa_data, rta_data,
  470. rta_payload);
  471. break;
  472. case IFLA_UNSPEC:
  473. break;
  474. case IFLA_MTU:
  475. break;
  476. case IFLA_LINK:
  477. break;
  478. case IFLA_QDISC:
  479. break;
  480. default:
  481. break;
  482. }
  483. rta = RTA_NEXT (rta, rtasize);
  484. }
  485. }
  486. else if (nlh->nlmsg_type == RTM_NEWADDR)
  487. {
  488. struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlh);
  489. struct rtattr *rta = IFA_RTA (ifam);
  490. size_t rtasize = IFA_PAYLOAD (nlh);
  491. /* New Addresses are stored in the order we got them from
  492. the kernel after the interfaces. Theoretically it is possible
  493. that we have holes in the interface part of the list,
  494. but we always have already the interface for this address. */
  495. ifa_index = newlink + newaddr_idx;
  496. ifas[ifa_index].ifa.ifa_flags
  497. = ifas[map_newlink (ifam->ifa_index - 1, ifas,
  498. map_newlink_data, newlink)].ifa.ifa_flags;
  499. if (ifa_index > 0)
  500. ifas[ifa_index - 1].ifa.ifa_next = &ifas[ifa_index].ifa;
  501. ++newaddr_idx;
  502. while (RTA_OK (rta, rtasize))
  503. {
  504. char *rta_data = RTA_DATA (rta);
  505. size_t rta_payload = RTA_PAYLOAD (rta);
  506. switch (rta->rta_type)
  507. {
  508. case IFA_ADDRESS:
  509. {
  510. struct sockaddr *sa;
  511. if (ifas[ifa_index].ifa.ifa_addr != NULL)
  512. {
  513. /* In a point-to-poing network IFA_ADDRESS
  514. contains the destination address, local
  515. address is supplied in IFA_LOCAL attribute.
  516. destination address and broadcast address
  517. are stored in an union, so it doesn't matter
  518. which name we use. */
  519. ifas[ifa_index].ifa.ifa_broadaddr
  520. = &ifas[ifa_index].broadaddr.sa;
  521. sa = &ifas[ifa_index].broadaddr.sa;
  522. }
  523. else
  524. {
  525. ifas[ifa_index].ifa.ifa_addr
  526. = &ifas[ifa_index].addr.sa;
  527. sa = &ifas[ifa_index].addr.sa;
  528. }
  529. sa->sa_family = ifam->ifa_family;
  530. switch (ifam->ifa_family)
  531. {
  532. case AF_INET:
  533. /* Size must match that of an address for IPv4. */
  534. if (rta_payload == 4)
  535. memcpy (&((struct sockaddr_in *) sa)->sin_addr,
  536. rta_data, rta_payload);
  537. break;
  538. #ifdef __UCLIBC_HAS_IPV6__
  539. case AF_INET6:
  540. /* Size must match that of an address for IPv6. */
  541. if (rta_payload == 16)
  542. {
  543. memcpy (&((struct sockaddr_in6 *) sa)->sin6_addr,
  544. rta_data, rta_payload);
  545. if (IN6_IS_ADDR_LINKLOCAL (rta_data)
  546. || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
  547. ((struct sockaddr_in6 *) sa)->sin6_scope_id
  548. = ifam->ifa_index;
  549. }
  550. break;
  551. #endif
  552. default:
  553. if (rta_payload <= sizeof (ifas[ifa_index].addr))
  554. memcpy (sa->sa_data, rta_data, rta_payload);
  555. break;
  556. }
  557. }
  558. break;
  559. case IFA_LOCAL:
  560. if (ifas[ifa_index].ifa.ifa_addr != NULL)
  561. {
  562. /* If ifa_addr is set and we get IFA_LOCAL,
  563. assume we have a point-to-point network.
  564. Move address to correct field. */
  565. ifas[ifa_index].broadaddr = ifas[ifa_index].addr;
  566. ifas[ifa_index].ifa.ifa_broadaddr
  567. = &ifas[ifa_index].broadaddr.sa;
  568. memset (&ifas[ifa_index].addr, '\0',
  569. sizeof (ifas[ifa_index].addr));
  570. }
  571. ifas[ifa_index].ifa.ifa_addr = &ifas[ifa_index].addr.sa;
  572. ifas[ifa_index].ifa.ifa_addr->sa_family
  573. = ifam->ifa_family;
  574. switch (ifam->ifa_family)
  575. {
  576. case AF_INET:
  577. /* Size must match that of an address for IPv4. */
  578. if (rta_payload == 4)
  579. memcpy (&ifas[ifa_index].addr.s4.sin_addr,
  580. rta_data, rta_payload);
  581. break;
  582. #ifdef __UCLIBC_HAS_IPV6__
  583. case AF_INET6:
  584. /* Size must match that of an address for IPv6. */
  585. if (rta_payload == 16)
  586. {
  587. memcpy (&ifas[ifa_index].addr.s6.sin6_addr,
  588. rta_data, rta_payload);
  589. if (IN6_IS_ADDR_LINKLOCAL (rta_data)
  590. || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
  591. ifas[ifa_index].addr.s6.sin6_scope_id =
  592. ifam->ifa_index;
  593. }
  594. break;
  595. #endif
  596. default:
  597. if (rta_payload <= sizeof (ifas[ifa_index].addr))
  598. memcpy (ifas[ifa_index].addr.sa.sa_data,
  599. rta_data, rta_payload);
  600. break;
  601. }
  602. break;
  603. case IFA_BROADCAST:
  604. /* We get IFA_BROADCAST, so IFA_LOCAL was too much. */
  605. if (ifas[ifa_index].ifa.ifa_broadaddr != NULL)
  606. memset (&ifas[ifa_index].broadaddr, '\0',
  607. sizeof (ifas[ifa_index].broadaddr));
  608. ifas[ifa_index].ifa.ifa_broadaddr
  609. = &ifas[ifa_index].broadaddr.sa;
  610. ifas[ifa_index].ifa.ifa_broadaddr->sa_family
  611. = ifam->ifa_family;
  612. switch (ifam->ifa_family)
  613. {
  614. case AF_INET:
  615. /* Size must match that of an address for IPv4. */
  616. if (rta_payload == 4)
  617. memcpy (&ifas[ifa_index].broadaddr.s4.sin_addr,
  618. rta_data, rta_payload);
  619. break;
  620. #ifdef __UCLIBC_HAS_IPV6__
  621. case AF_INET6:
  622. /* Size must match that of an address for IPv6. */
  623. if (rta_payload == 16)
  624. {
  625. memcpy (&ifas[ifa_index].broadaddr.s6.sin6_addr,
  626. rta_data, rta_payload);
  627. if (IN6_IS_ADDR_LINKLOCAL (rta_data)
  628. || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
  629. ifas[ifa_index].broadaddr.s6.sin6_scope_id
  630. = ifam->ifa_index;
  631. }
  632. break;
  633. #endif
  634. default:
  635. if (rta_payload <= sizeof (ifas[ifa_index].addr))
  636. memcpy (&ifas[ifa_index].broadaddr.sa.sa_data,
  637. rta_data, rta_payload);
  638. break;
  639. }
  640. break;
  641. case IFA_LABEL:
  642. if (rta_payload + 1 <= sizeof (ifas[ifa_index].name))
  643. {
  644. ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
  645. *(char *) mempcpy (ifas[ifa_index].name, rta_data,
  646. rta_payload) = '\0';
  647. }
  648. else
  649. abort ();
  650. break;
  651. case IFA_UNSPEC:
  652. break;
  653. case IFA_CACHEINFO:
  654. break;
  655. default:
  656. break;
  657. }
  658. rta = RTA_NEXT (rta, rtasize);
  659. }
  660. /* If we didn't get the interface name with the
  661. address, use the name from the interface entry. */
  662. if (ifas[ifa_index].ifa.ifa_name == NULL)
  663. ifas[ifa_index].ifa.ifa_name
  664. = ifas[map_newlink (ifam->ifa_index - 1, ifas,
  665. map_newlink_data, newlink)].ifa.ifa_name;
  666. /* Calculate the netmask. */
  667. if (ifas[ifa_index].ifa.ifa_addr
  668. && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_UNSPEC
  669. && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_PACKET)
  670. {
  671. uint32_t max_prefixlen = 0;
  672. char *cp = NULL;
  673. ifas[ifa_index].ifa.ifa_netmask
  674. = &ifas[ifa_index].netmask.sa;
  675. switch (ifas[ifa_index].ifa.ifa_addr->sa_family)
  676. {
  677. case AF_INET:
  678. cp = (char *) &ifas[ifa_index].netmask.s4.sin_addr;
  679. max_prefixlen = 32;
  680. break;
  681. #ifdef __UCLIBC_HAS_IPV6__
  682. case AF_INET6:
  683. cp = (char *) &ifas[ifa_index].netmask.s6.sin6_addr;
  684. max_prefixlen = 128;
  685. break;
  686. #endif
  687. }
  688. ifas[ifa_index].ifa.ifa_netmask->sa_family
  689. = ifas[ifa_index].ifa.ifa_addr->sa_family;
  690. if (cp != NULL)
  691. {
  692. char c;
  693. unsigned int preflen;
  694. if ((max_prefixlen > 0) &&
  695. (ifam->ifa_prefixlen > max_prefixlen))
  696. preflen = max_prefixlen;
  697. else
  698. preflen = ifam->ifa_prefixlen;
  699. for (i = 0; i < (preflen / 8); i++)
  700. *cp++ = 0xff;
  701. c = 0xff;
  702. c <<= (8 - (preflen % 8));
  703. *cp = c;
  704. }
  705. }
  706. }
  707. }
  708. }
  709. assert (ifa_data_ptr <= (char *) &ifas[newlink + newaddr] + ifa_data_size);
  710. if (newaddr_idx > 0)
  711. {
  712. for (i = 0; i < newlink; ++i)
  713. if (map_newlink_data[i] == -1)
  714. {
  715. /* We have fewer links then we anticipated. Adjust the
  716. forward pointer to the first address entry. */
  717. ifas[i - 1].ifa.ifa_next = &ifas[newlink].ifa;
  718. }
  719. if (i == 0 && newlink > 0)
  720. /* No valid link, but we allocated memory. We have to
  721. populate the first entry. */
  722. memmove (ifas, &ifas[newlink], sizeof (struct ifaddrs_storage));
  723. }
  724. if (ifap != NULL)
  725. *ifap = &ifas[0].ifa;
  726. exit_free:
  727. __netlink_free_handle (&nh);
  728. __netlink_close (&nh);
  729. return result;
  730. }
  731. libc_hidden_def(getifaddrs)
  732. void
  733. freeifaddrs (struct ifaddrs *ifa)
  734. {
  735. free (ifa);
  736. }
  737. libc_hidden_def(freeifaddrs)
  738. #endif /* __UCLIBC_SUPPORT_AI_ADDRCONFIG__ */
  739. #endif /* __ASSUME_NETLINK_SUPPORT */