ifaddrs.c 22 KB

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