ifaddrs.c 22 KB

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