ifaddrs.c 23 KB

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