ifaddrs.c 22 KB

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