ifaddrs.c 22 KB

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