ifaddrs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. return -1;
  261. }
  262. /* Determine the ID the kernel assigned for this netlink connection.
  263. It is not necessarily the PID if there is more than one socket
  264. open. */
  265. socklen_t addr_len = sizeof (nladdr);
  266. if (getsockname (h->fd, (struct sockaddr *) &nladdr, &addr_len) < 0)
  267. goto close_and_out;
  268. h->pid = nladdr.nl_pid;
  269. return 0;
  270. }
  271. #ifdef __UCLIBC_SUPPORT_AI_ADDRCONFIG__
  272. /* We know the number of RTM_NEWLINK entries, so we reserve the first
  273. # of entries for this type. All RTM_NEWADDR entries have an index
  274. pointer to the RTM_NEWLINK entry. To find the entry, create
  275. a table to map kernel index entries to our index numbers.
  276. Since we get at first all RTM_NEWLINK entries, it can never happen
  277. that a RTM_NEWADDR index is not known to this map. */
  278. static int
  279. internal_function
  280. map_newlink (int idx, struct ifaddrs_storage *ifas, int *map, int max)
  281. {
  282. int i;
  283. for (i = 0; i < max; i++)
  284. {
  285. if (map[i] == -1)
  286. {
  287. map[i] = idx;
  288. if (i > 0)
  289. ifas[i - 1].ifa.ifa_next = &ifas[i].ifa;
  290. return i;
  291. }
  292. else if (map[i] == idx)
  293. return i;
  294. }
  295. /* This should never be reached. If this will be reached, we have
  296. a very big problem. */
  297. abort ();
  298. }
  299. /* Create a linked list of `struct ifaddrs' structures, one for each
  300. network interface on the host machine. If successful, store the
  301. list in *IFAP and return 0. On errors, return -1 and set `errno'. */
  302. int
  303. getifaddrs (struct ifaddrs **ifap)
  304. {
  305. struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
  306. struct netlink_res *nlp;
  307. struct ifaddrs_storage *ifas;
  308. unsigned int i, newlink, newaddr, newaddr_idx;
  309. int *map_newlink_data;
  310. size_t ifa_data_size = 0; /* Size to allocate for all ifa_data. */
  311. char *ifa_data_ptr; /* Pointer to the unused part of memory for
  312. ifa_data. */
  313. int result = 0;
  314. if (ifap)
  315. *ifap = NULL;
  316. if (__netlink_open (&nh) < 0)
  317. {
  318. return -1;
  319. }
  320. /* Tell the kernel that we wish to get a list of all
  321. active interfaces, collect all data for every interface. */
  322. if (__netlink_request (&nh, RTM_GETLINK) < 0)
  323. {
  324. result = -1;
  325. goto exit_free;
  326. }
  327. /* Now ask the kernel for all addresses which are assigned
  328. to an interface and collect all data for every interface.
  329. Since we store the addresses after the interfaces in the
  330. list, we will later always find the interface before the
  331. corresponding addresses. */
  332. ++nh.seq;
  333. if (__netlink_request (&nh, RTM_GETADDR) < 0)
  334. {
  335. result = -1;
  336. goto exit_free;
  337. }
  338. /* Count all RTM_NEWLINK and RTM_NEWADDR entries to allocate
  339. enough memory. */
  340. newlink = newaddr = 0;
  341. for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
  342. {
  343. struct nlmsghdr *nlh;
  344. size_t size = nlp->size;
  345. if (nlp->nlh == NULL)
  346. continue;
  347. /* Walk through all entries we got from the kernel and look, which
  348. message type they contain. */
  349. for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
  350. {
  351. /* Check if the message is what we want. */
  352. if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
  353. continue;
  354. if (nlh->nlmsg_type == NLMSG_DONE)
  355. break; /* ok */
  356. if (nlh->nlmsg_type == RTM_NEWLINK)
  357. {
  358. /* A RTM_NEWLINK message can have IFLA_STATS data. We need to
  359. know the size before creating the list to allocate enough
  360. memory. */
  361. struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
  362. struct rtattr *rta = IFLA_RTA (ifim);
  363. size_t rtasize = IFLA_PAYLOAD (nlh);
  364. while (RTA_OK (rta, rtasize))
  365. {
  366. size_t rta_payload = RTA_PAYLOAD (rta);
  367. if (rta->rta_type == IFLA_STATS)
  368. {
  369. ifa_data_size += rta_payload;
  370. break;
  371. }
  372. else
  373. rta = RTA_NEXT (rta, rtasize);
  374. }
  375. ++newlink;
  376. }
  377. else if (nlh->nlmsg_type == RTM_NEWADDR)
  378. ++newaddr;
  379. }
  380. }
  381. /* Return if no interface is up. */
  382. if ((newlink + newaddr) == 0)
  383. goto exit_free;
  384. /* Allocate memory for all entries we have and initialize next
  385. pointer. */
  386. ifas = (struct ifaddrs_storage *) calloc (1,
  387. (newlink + newaddr)
  388. * sizeof (struct ifaddrs_storage)
  389. + ifa_data_size);
  390. if (ifas == NULL)
  391. {
  392. result = -1;
  393. goto exit_free;
  394. }
  395. /* Table for mapping kernel index to entry in our list. */
  396. map_newlink_data = alloca (newlink * sizeof (int));
  397. memset (map_newlink_data, '\xff', newlink * sizeof (int));
  398. ifa_data_ptr = (char *) &ifas[newlink + newaddr];
  399. newaddr_idx = 0; /* Counter for newaddr index. */
  400. /* Walk through the list of data we got from the kernel. */
  401. for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
  402. {
  403. struct nlmsghdr *nlh;
  404. size_t size = nlp->size;
  405. if (nlp->nlh == NULL)
  406. continue;
  407. /* Walk through one message and look at the type: If it is our
  408. message, we need RTM_NEWLINK/RTM_NEWADDR and stop if we reach
  409. the end or we find the end marker (in this case we ignore the
  410. following data. */
  411. for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
  412. {
  413. int ifa_index = 0;
  414. /* Check if the message is the one we want */
  415. if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
  416. continue;
  417. if (nlh->nlmsg_type == NLMSG_DONE)
  418. break; /* ok */
  419. if (nlh->nlmsg_type == RTM_NEWLINK)
  420. {
  421. /* We found a new interface. Now extract everything from the
  422. interface data we got and need. */
  423. struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
  424. struct rtattr *rta = IFLA_RTA (ifim);
  425. size_t rtasize = IFLA_PAYLOAD (nlh);
  426. /* Interfaces are stored in the first "newlink" entries
  427. of our list, starting in the order as we got from the
  428. kernel. */
  429. ifa_index = map_newlink (ifim->ifi_index - 1, ifas,
  430. map_newlink_data, newlink);
  431. ifas[ifa_index].ifa.ifa_flags = ifim->ifi_flags;
  432. while (RTA_OK (rta, rtasize))
  433. {
  434. char *rta_data = RTA_DATA (rta);
  435. size_t rta_payload = RTA_PAYLOAD (rta);
  436. switch (rta->rta_type)
  437. {
  438. case IFLA_ADDRESS:
  439. if (rta_payload <= sizeof (ifas[ifa_index].addr))
  440. {
  441. ifas[ifa_index].addr.sl.sll_family = AF_PACKET;
  442. memcpy (ifas[ifa_index].addr.sl.sll_addr,
  443. (char *) rta_data, rta_payload);
  444. ifas[ifa_index].addr.sl.sll_halen = rta_payload;
  445. ifas[ifa_index].addr.sl.sll_ifindex
  446. = ifim->ifi_index;
  447. ifas[ifa_index].addr.sl.sll_hatype = ifim->ifi_type;
  448. ifas[ifa_index].ifa.ifa_addr
  449. = &ifas[ifa_index].addr.sa;
  450. }
  451. break;
  452. case IFLA_BROADCAST:
  453. if (rta_payload <= sizeof (ifas[ifa_index].broadaddr))
  454. {
  455. ifas[ifa_index].broadaddr.sl.sll_family = AF_PACKET;
  456. memcpy (ifas[ifa_index].broadaddr.sl.sll_addr,
  457. (char *) rta_data, rta_payload);
  458. ifas[ifa_index].broadaddr.sl.sll_halen = rta_payload;
  459. ifas[ifa_index].broadaddr.sl.sll_ifindex
  460. = ifim->ifi_index;
  461. ifas[ifa_index].broadaddr.sl.sll_hatype
  462. = ifim->ifi_type;
  463. ifas[ifa_index].ifa.ifa_broadaddr
  464. = &ifas[ifa_index].broadaddr.sa;
  465. }
  466. break;
  467. case IFLA_IFNAME: /* Name of Interface */
  468. if ((rta_payload + 1) <= sizeof (ifas[ifa_index].name))
  469. {
  470. ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
  471. *(char *) mempcpy (ifas[ifa_index].name, rta_data,
  472. rta_payload) = '\0';
  473. }
  474. break;
  475. case IFLA_STATS: /* Statistics of Interface */
  476. ifas[ifa_index].ifa.ifa_data = ifa_data_ptr;
  477. ifa_data_ptr += rta_payload;
  478. memcpy (ifas[ifa_index].ifa.ifa_data, rta_data,
  479. rta_payload);
  480. break;
  481. case IFLA_UNSPEC:
  482. break;
  483. case IFLA_MTU:
  484. break;
  485. case IFLA_LINK:
  486. break;
  487. case IFLA_QDISC:
  488. break;
  489. default:
  490. break;
  491. }
  492. rta = RTA_NEXT (rta, rtasize);
  493. }
  494. }
  495. else if (nlh->nlmsg_type == RTM_NEWADDR)
  496. {
  497. struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlh);
  498. struct rtattr *rta = IFA_RTA (ifam);
  499. size_t rtasize = IFA_PAYLOAD (nlh);
  500. /* New Addresses are stored in the order we got them from
  501. the kernel after the interfaces. Theoretically it is possible
  502. that we have holes in the interface part of the list,
  503. but we always have already the interface for this address. */
  504. ifa_index = newlink + newaddr_idx;
  505. ifas[ifa_index].ifa.ifa_flags
  506. = ifas[map_newlink (ifam->ifa_index - 1, ifas,
  507. map_newlink_data, newlink)].ifa.ifa_flags;
  508. if (ifa_index > 0)
  509. ifas[ifa_index - 1].ifa.ifa_next = &ifas[ifa_index].ifa;
  510. ++newaddr_idx;
  511. while (RTA_OK (rta, rtasize))
  512. {
  513. char *rta_data = RTA_DATA (rta);
  514. size_t rta_payload = RTA_PAYLOAD (rta);
  515. switch (rta->rta_type)
  516. {
  517. case IFA_ADDRESS:
  518. {
  519. struct sockaddr *sa;
  520. if (ifas[ifa_index].ifa.ifa_addr != NULL)
  521. {
  522. /* In a point-to-poing network IFA_ADDRESS
  523. contains the destination address, local
  524. address is supplied in IFA_LOCAL attribute.
  525. destination address and broadcast address
  526. are stored in an union, so it doesn't matter
  527. which name we use. */
  528. ifas[ifa_index].ifa.ifa_broadaddr
  529. = &ifas[ifa_index].broadaddr.sa;
  530. sa = &ifas[ifa_index].broadaddr.sa;
  531. }
  532. else
  533. {
  534. ifas[ifa_index].ifa.ifa_addr
  535. = &ifas[ifa_index].addr.sa;
  536. sa = &ifas[ifa_index].addr.sa;
  537. }
  538. sa->sa_family = ifam->ifa_family;
  539. switch (ifam->ifa_family)
  540. {
  541. case AF_INET:
  542. /* Size must match that of an address for IPv4. */
  543. if (rta_payload == 4)
  544. memcpy (&((struct sockaddr_in *) sa)->sin_addr,
  545. rta_data, rta_payload);
  546. break;
  547. case AF_INET6:
  548. /* Size must match that of an address for IPv6. */
  549. if (rta_payload == 16)
  550. {
  551. memcpy (&((struct sockaddr_in6 *) sa)->sin6_addr,
  552. rta_data, rta_payload);
  553. if (IN6_IS_ADDR_LINKLOCAL (rta_data)
  554. || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
  555. ((struct sockaddr_in6 *) sa)->sin6_scope_id
  556. = ifam->ifa_index;
  557. }
  558. break;
  559. default:
  560. if (rta_payload <= sizeof (ifas[ifa_index].addr))
  561. memcpy (sa->sa_data, rta_data, rta_payload);
  562. break;
  563. }
  564. }
  565. break;
  566. case IFA_LOCAL:
  567. if (ifas[ifa_index].ifa.ifa_addr != NULL)
  568. {
  569. /* If ifa_addr is set and we get IFA_LOCAL,
  570. assume we have a point-to-point network.
  571. Move address to correct field. */
  572. ifas[ifa_index].broadaddr = ifas[ifa_index].addr;
  573. ifas[ifa_index].ifa.ifa_broadaddr
  574. = &ifas[ifa_index].broadaddr.sa;
  575. memset (&ifas[ifa_index].addr, '\0',
  576. sizeof (ifas[ifa_index].addr));
  577. }
  578. ifas[ifa_index].ifa.ifa_addr = &ifas[ifa_index].addr.sa;
  579. ifas[ifa_index].ifa.ifa_addr->sa_family
  580. = ifam->ifa_family;
  581. switch (ifam->ifa_family)
  582. {
  583. case AF_INET:
  584. /* Size must match that of an address for IPv4. */
  585. if (rta_payload == 4)
  586. memcpy (&ifas[ifa_index].addr.s4.sin_addr,
  587. rta_data, rta_payload);
  588. break;
  589. case AF_INET6:
  590. /* Size must match that of an address for IPv6. */
  591. if (rta_payload == 16)
  592. {
  593. memcpy (&ifas[ifa_index].addr.s6.sin6_addr,
  594. rta_data, rta_payload);
  595. if (IN6_IS_ADDR_LINKLOCAL (rta_data)
  596. || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
  597. ifas[ifa_index].addr.s6.sin6_scope_id =
  598. ifam->ifa_index;
  599. }
  600. break;
  601. default:
  602. if (rta_payload <= sizeof (ifas[ifa_index].addr))
  603. memcpy (ifas[ifa_index].addr.sa.sa_data,
  604. rta_data, rta_payload);
  605. break;
  606. }
  607. break;
  608. case IFA_BROADCAST:
  609. /* We get IFA_BROADCAST, so IFA_LOCAL was too much. */
  610. if (ifas[ifa_index].ifa.ifa_broadaddr != NULL)
  611. memset (&ifas[ifa_index].broadaddr, '\0',
  612. sizeof (ifas[ifa_index].broadaddr));
  613. ifas[ifa_index].ifa.ifa_broadaddr
  614. = &ifas[ifa_index].broadaddr.sa;
  615. ifas[ifa_index].ifa.ifa_broadaddr->sa_family
  616. = ifam->ifa_family;
  617. switch (ifam->ifa_family)
  618. {
  619. case AF_INET:
  620. /* Size must match that of an address for IPv4. */
  621. if (rta_payload == 4)
  622. memcpy (&ifas[ifa_index].broadaddr.s4.sin_addr,
  623. rta_data, rta_payload);
  624. break;
  625. case AF_INET6:
  626. /* Size must match that of an address for IPv6. */
  627. if (rta_payload == 16)
  628. {
  629. memcpy (&ifas[ifa_index].broadaddr.s6.sin6_addr,
  630. rta_data, rta_payload);
  631. if (IN6_IS_ADDR_LINKLOCAL (rta_data)
  632. || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
  633. ifas[ifa_index].broadaddr.s6.sin6_scope_id
  634. = ifam->ifa_index;
  635. }
  636. break;
  637. default:
  638. if (rta_payload <= sizeof (ifas[ifa_index].addr))
  639. memcpy (&ifas[ifa_index].broadaddr.sa.sa_data,
  640. rta_data, rta_payload);
  641. break;
  642. }
  643. break;
  644. case IFA_LABEL:
  645. if (rta_payload + 1 <= sizeof (ifas[ifa_index].name))
  646. {
  647. ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
  648. *(char *) mempcpy (ifas[ifa_index].name, rta_data,
  649. rta_payload) = '\0';
  650. }
  651. else
  652. abort ();
  653. break;
  654. case IFA_UNSPEC:
  655. break;
  656. case IFA_CACHEINFO:
  657. break;
  658. default:
  659. break;
  660. }
  661. rta = RTA_NEXT (rta, rtasize);
  662. }
  663. /* If we didn't get the interface name with the
  664. address, use the name from the interface entry. */
  665. if (ifas[ifa_index].ifa.ifa_name == NULL)
  666. ifas[ifa_index].ifa.ifa_name
  667. = ifas[map_newlink (ifam->ifa_index - 1, ifas,
  668. map_newlink_data, newlink)].ifa.ifa_name;
  669. /* Calculate the netmask. */
  670. if (ifas[ifa_index].ifa.ifa_addr
  671. && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_UNSPEC
  672. && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_PACKET)
  673. {
  674. uint32_t max_prefixlen = 0;
  675. char *cp = NULL;
  676. ifas[ifa_index].ifa.ifa_netmask
  677. = &ifas[ifa_index].netmask.sa;
  678. switch (ifas[ifa_index].ifa.ifa_addr->sa_family)
  679. {
  680. case AF_INET:
  681. cp = (char *) &ifas[ifa_index].netmask.s4.sin_addr;
  682. max_prefixlen = 32;
  683. break;
  684. case AF_INET6:
  685. cp = (char *) &ifas[ifa_index].netmask.s6.sin6_addr;
  686. max_prefixlen = 128;
  687. break;
  688. }
  689. ifas[ifa_index].ifa.ifa_netmask->sa_family
  690. = ifas[ifa_index].ifa.ifa_addr->sa_family;
  691. if (cp != NULL)
  692. {
  693. char c;
  694. unsigned int preflen;
  695. if ((max_prefixlen > 0) &&
  696. (ifam->ifa_prefixlen > max_prefixlen))
  697. preflen = max_prefixlen;
  698. else
  699. preflen = ifam->ifa_prefixlen;
  700. for (i = 0; i < (preflen / 8); i++)
  701. *cp++ = 0xff;
  702. c = 0xff;
  703. c <<= (8 - (preflen % 8));
  704. *cp = c;
  705. }
  706. }
  707. }
  708. }
  709. }
  710. assert (ifa_data_ptr <= (char *) &ifas[newlink + newaddr] + ifa_data_size);
  711. if (newaddr_idx > 0)
  712. {
  713. for (i = 0; i < newlink; ++i)
  714. if (map_newlink_data[i] == -1)
  715. {
  716. /* We have fewer links then we anticipated. Adjust the
  717. forward pointer to the first address entry. */
  718. ifas[i - 1].ifa.ifa_next = &ifas[newlink].ifa;
  719. }
  720. if (i == 0 && newlink > 0)
  721. /* No valid link, but we allocated memory. We have to
  722. populate the first entry. */
  723. memmove (ifas, &ifas[newlink], sizeof (struct ifaddrs_storage));
  724. }
  725. if (ifap != NULL)
  726. *ifap = &ifas[0].ifa;
  727. exit_free:
  728. __netlink_free_handle (&nh);
  729. __netlink_close (&nh);
  730. return result;
  731. }
  732. void
  733. freeifaddrs (struct ifaddrs *ifa)
  734. {
  735. free (ifa);
  736. }
  737. #endif /* __UCLIBC_SUPPORT_AI_ADDRCONFIG__ */
  738. #endif /* __ASSUME_NETLINK_SUPPORT */