1 | /* getifaddrs -- get names and addresses of all network interfaces |
2 | Copyright (C) 2003-2017 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <alloca.h> |
20 | #include <assert.h> |
21 | #include <errno.h> |
22 | #include <ifaddrs.h> |
23 | #include <net/if.h> |
24 | #include <netinet/in.h> |
25 | #include <netpacket/packet.h> |
26 | #include <stdbool.h> |
27 | #include <stdint.h> |
28 | #include <stdlib.h> |
29 | #include <string.h> |
30 | #include <sys/ioctl.h> |
31 | #include <sys/socket.h> |
32 | #include <sysdep.h> |
33 | #include <time.h> |
34 | #include <unistd.h> |
35 | |
36 | #include "netlinkaccess.h" |
37 | |
38 | |
39 | /* There is a problem with this type. The address length for |
40 | Infiniband sockets is much longer than the 8 bytes allocated in the |
41 | sockaddr_ll definition. Hence we use here a special |
42 | definition. */ |
43 | struct sockaddr_ll_max |
44 | { |
45 | unsigned short int sll_family; |
46 | unsigned short int sll_protocol; |
47 | int sll_ifindex; |
48 | unsigned short int sll_hatype; |
49 | unsigned char sll_pkttype; |
50 | unsigned char sll_halen; |
51 | unsigned char sll_addr[24]; |
52 | }; |
53 | |
54 | |
55 | /* struct to hold the data for one ifaddrs entry, so we can allocate |
56 | everything at once. */ |
57 | struct ifaddrs_storage |
58 | { |
59 | struct ifaddrs ifa; |
60 | union |
61 | { |
62 | /* Save space for the biggest of the four used sockaddr types and |
63 | avoid a lot of casts. */ |
64 | struct sockaddr sa; |
65 | struct sockaddr_ll_max sl; |
66 | struct sockaddr_in s4; |
67 | struct sockaddr_in6 s6; |
68 | } addr, netmask, broadaddr; |
69 | char name[IF_NAMESIZE + 1]; |
70 | }; |
71 | |
72 | |
73 | void |
74 | __netlink_free_handle (struct netlink_handle *h) |
75 | { |
76 | struct netlink_res *ptr; |
77 | int saved_errno = errno; |
78 | |
79 | ptr = h->nlm_list; |
80 | while (ptr != NULL) |
81 | { |
82 | struct netlink_res *tmpptr; |
83 | |
84 | tmpptr = ptr->next; |
85 | free (ptr); |
86 | ptr = tmpptr; |
87 | } |
88 | |
89 | __set_errno (saved_errno); |
90 | } |
91 | |
92 | |
93 | static int |
94 | __netlink_sendreq (struct netlink_handle *h, int type) |
95 | { |
96 | struct req |
97 | { |
98 | struct nlmsghdr nlh; |
99 | struct rtgenmsg g; |
100 | char pad[0]; |
101 | } req; |
102 | struct sockaddr_nl nladdr; |
103 | |
104 | if (h->seq == 0) |
105 | h->seq = time (NULL); |
106 | |
107 | req.nlh.nlmsg_len = sizeof (req); |
108 | req.nlh.nlmsg_type = type; |
109 | req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; |
110 | req.nlh.nlmsg_pid = 0; |
111 | req.nlh.nlmsg_seq = h->seq; |
112 | req.g.rtgen_family = AF_UNSPEC; |
113 | if (sizeof (req) != offsetof (struct req, pad)) |
114 | memset (req.pad, '\0', sizeof (req) - offsetof (struct req, pad)); |
115 | |
116 | memset (&nladdr, '\0', sizeof (nladdr)); |
117 | nladdr.nl_family = AF_NETLINK; |
118 | |
119 | return TEMP_FAILURE_RETRY (__sendto (h->fd, (void *) &req, sizeof (req), 0, |
120 | (struct sockaddr *) &nladdr, |
121 | sizeof (nladdr))); |
122 | } |
123 | |
124 | |
125 | int |
126 | __netlink_request (struct netlink_handle *h, int type) |
127 | { |
128 | struct netlink_res *nlm_next; |
129 | struct sockaddr_nl nladdr; |
130 | struct nlmsghdr *nlmh; |
131 | ssize_t read_len; |
132 | bool done = false; |
133 | |
134 | #ifdef PAGE_SIZE |
135 | /* Help the compiler optimize out the malloc call if PAGE_SIZE |
136 | is constant and smaller or equal to PTHREAD_STACK_MIN/4. */ |
137 | const size_t buf_size = PAGE_SIZE; |
138 | #else |
139 | const size_t buf_size = __getpagesize (); |
140 | #endif |
141 | bool use_malloc = false; |
142 | char *buf; |
143 | |
144 | if (__libc_use_alloca (buf_size)) |
145 | buf = alloca (buf_size); |
146 | else |
147 | { |
148 | buf = malloc (buf_size); |
149 | if (buf != NULL) |
150 | use_malloc = true; |
151 | else |
152 | goto out_fail; |
153 | } |
154 | |
155 | struct iovec iov = { buf, buf_size }; |
156 | |
157 | if (__netlink_sendreq (h, type) < 0) |
158 | goto out_fail; |
159 | |
160 | while (! done) |
161 | { |
162 | struct msghdr msg = |
163 | { |
164 | .msg_name = (void *) &nladdr, |
165 | .msg_namelen = sizeof (nladdr), |
166 | .msg_iov = &iov, |
167 | .msg_iovlen = 1, |
168 | .msg_control = NULL, |
169 | .msg_controllen = 0, |
170 | .msg_flags = 0 |
171 | }; |
172 | |
173 | read_len = TEMP_FAILURE_RETRY (__recvmsg (h->fd, &msg, 0)); |
174 | __netlink_assert_response (h->fd, read_len); |
175 | if (read_len < 0) |
176 | goto out_fail; |
177 | |
178 | if (nladdr.nl_pid != 0) |
179 | continue; |
180 | |
181 | if (__glibc_unlikely (msg.msg_flags & MSG_TRUNC)) |
182 | goto out_fail; |
183 | |
184 | size_t count = 0; |
185 | size_t remaining_len = read_len; |
186 | for (nlmh = (struct nlmsghdr *) buf; |
187 | NLMSG_OK (nlmh, remaining_len); |
188 | nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, remaining_len)) |
189 | { |
190 | if ((pid_t) nlmh->nlmsg_pid != h->pid |
191 | || nlmh->nlmsg_seq != h->seq) |
192 | continue; |
193 | |
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 | |
212 | /* If there was nothing with the expected nlmsg_pid and nlmsg_seq, |
213 | there is no point to record it. */ |
214 | if (count == 0) |
215 | continue; |
216 | |
217 | nlm_next = (struct netlink_res *) malloc (sizeof (struct netlink_res) |
218 | + read_len); |
219 | if (nlm_next == NULL) |
220 | goto out_fail; |
221 | nlm_next->next = NULL; |
222 | nlm_next->nlh = memcpy (nlm_next + 1, buf, read_len); |
223 | nlm_next->size = read_len; |
224 | nlm_next->seq = h->seq; |
225 | if (h->nlm_list == NULL) |
226 | h->nlm_list = nlm_next; |
227 | else |
228 | h->end_ptr->next = nlm_next; |
229 | h->end_ptr = nlm_next; |
230 | } |
231 | |
232 | if (use_malloc) |
233 | free (buf); |
234 | return 0; |
235 | |
236 | out_fail: |
237 | if (use_malloc) |
238 | free (buf); |
239 | return -1; |
240 | } |
241 | |
242 | |
243 | void |
244 | __netlink_close (struct netlink_handle *h) |
245 | { |
246 | /* Don't modify errno. */ |
247 | INTERNAL_SYSCALL_DECL (err); |
248 | (void) INTERNAL_SYSCALL (close, err, 1, h->fd); |
249 | } |
250 | |
251 | |
252 | /* Open a NETLINK socket. */ |
253 | int |
254 | __netlink_open (struct netlink_handle *h) |
255 | { |
256 | struct sockaddr_nl nladdr; |
257 | |
258 | h->fd = __socket (PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); |
259 | if (h->fd < 0) |
260 | goto out; |
261 | |
262 | memset (&nladdr, '\0', sizeof (nladdr)); |
263 | nladdr.nl_family = AF_NETLINK; |
264 | if (__bind (h->fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) < 0) |
265 | { |
266 | close_and_out: |
267 | __netlink_close (h); |
268 | out: |
269 | return -1; |
270 | } |
271 | /* Determine the ID the kernel assigned for this netlink connection. |
272 | It is not necessarily the PID if there is more than one socket |
273 | open. */ |
274 | socklen_t addr_len = sizeof (nladdr); |
275 | if (__getsockname (h->fd, (struct sockaddr *) &nladdr, &addr_len) < 0) |
276 | goto close_and_out; |
277 | h->pid = nladdr.nl_pid; |
278 | return 0; |
279 | } |
280 | |
281 | |
282 | /* We know the number of RTM_NEWLINK entries, so we reserve the first |
283 | # of entries for this type. All RTM_NEWADDR entries have an index |
284 | pointer to the RTM_NEWLINK entry. To find the entry, create |
285 | a table to map kernel index entries to our index numbers. |
286 | Since we get at first all RTM_NEWLINK entries, it can never happen |
287 | that a RTM_NEWADDR index is not known to this map. */ |
288 | static int |
289 | internal_function |
290 | map_newlink (int index, struct ifaddrs_storage *ifas, int *map, int max) |
291 | { |
292 | int i; |
293 | |
294 | for (i = 0; i < max; i++) |
295 | { |
296 | if (map[i] == -1) |
297 | { |
298 | map[i] = index; |
299 | if (i > 0) |
300 | ifas[i - 1].ifa.ifa_next = &ifas[i].ifa; |
301 | return i; |
302 | } |
303 | else if (map[i] == index) |
304 | return i; |
305 | } |
306 | |
307 | /* This means interfaces changed between the reading of the |
308 | RTM_GETLINK and RTM_GETADDR information. We have to repeat |
309 | everything. */ |
310 | return -1; |
311 | } |
312 | |
313 | |
314 | /* Create a linked list of `struct ifaddrs' structures, one for each |
315 | network interface on the host machine. If successful, store the |
316 | list in *IFAP and return 0. On errors, return -1 and set `errno'. */ |
317 | static int |
318 | getifaddrs_internal (struct ifaddrs **ifap) |
319 | { |
320 | struct netlink_handle nh = { 0, 0, 0, NULL, NULL }; |
321 | struct netlink_res *nlp; |
322 | struct ifaddrs_storage *ifas; |
323 | unsigned int i, newlink, newaddr, newaddr_idx; |
324 | int *map_newlink_data; |
325 | size_t ifa_data_size = 0; /* Size to allocate for all ifa_data. */ |
326 | char *ifa_data_ptr; /* Pointer to the unused part of memory for |
327 | ifa_data. */ |
328 | int result = 0; |
329 | |
330 | *ifap = NULL; |
331 | |
332 | if (__netlink_open (&nh) < 0) |
333 | return -1; |
334 | |
335 | /* Tell the kernel that we wish to get a list of all |
336 | active interfaces, collect all data for every interface. */ |
337 | if (__netlink_request (&nh, RTM_GETLINK) < 0) |
338 | { |
339 | result = -1; |
340 | goto exit_free; |
341 | } |
342 | |
343 | /* Now ask the kernel for all addresses which are assigned |
344 | to an interface and collect all data for every interface. |
345 | Since we store the addresses after the interfaces in the |
346 | list, we will later always find the interface before the |
347 | corresponding addresses. */ |
348 | ++nh.seq; |
349 | if (__netlink_request (&nh, RTM_GETADDR) < 0) |
350 | { |
351 | result = -1; |
352 | goto exit_free; |
353 | } |
354 | |
355 | /* Count all RTM_NEWLINK and RTM_NEWADDR entries to allocate |
356 | enough memory. */ |
357 | newlink = newaddr = 0; |
358 | for (nlp = nh.nlm_list; nlp; nlp = nlp->next) |
359 | { |
360 | struct nlmsghdr *nlh; |
361 | size_t size = nlp->size; |
362 | |
363 | if (nlp->nlh == NULL) |
364 | continue; |
365 | |
366 | /* Walk through all entries we got from the kernel and look, which |
367 | message type they contain. */ |
368 | for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size)) |
369 | { |
370 | /* Check if the message is what we want. */ |
371 | if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq) |
372 | continue; |
373 | |
374 | /* If the dump got interrupted, we can't rely on the results |
375 | so try again. */ |
376 | if (nlh->nlmsg_flags & NLM_F_DUMP_INTR) |
377 | { |
378 | result = -EAGAIN; |
379 | goto exit_free; |
380 | } |
381 | |
382 | if (nlh->nlmsg_type == NLMSG_DONE) |
383 | break; /* ok */ |
384 | |
385 | if (nlh->nlmsg_type == RTM_NEWLINK) |
386 | { |
387 | /* A RTM_NEWLINK message can have IFLA_STATS data. We need to |
388 | know the size before creating the list to allocate enough |
389 | memory. */ |
390 | struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh); |
391 | struct rtattr *rta = IFLA_RTA (ifim); |
392 | size_t rtasize = IFLA_PAYLOAD (nlh); |
393 | |
394 | while (RTA_OK (rta, rtasize)) |
395 | { |
396 | size_t rta_payload = RTA_PAYLOAD (rta); |
397 | |
398 | if (rta->rta_type == IFLA_STATS) |
399 | { |
400 | ifa_data_size += rta_payload; |
401 | break; |
402 | } |
403 | else |
404 | rta = RTA_NEXT (rta, rtasize); |
405 | } |
406 | ++newlink; |
407 | } |
408 | else if (nlh->nlmsg_type == RTM_NEWADDR) |
409 | ++newaddr; |
410 | } |
411 | } |
412 | |
413 | /* Return if no interface is up. */ |
414 | if ((newlink + newaddr) == 0) |
415 | goto exit_free; |
416 | |
417 | /* Allocate memory for all entries we have and initialize next |
418 | pointer. */ |
419 | ifas = (struct ifaddrs_storage *) calloc (1, |
420 | (newlink + newaddr) |
421 | * sizeof (struct ifaddrs_storage) |
422 | + ifa_data_size); |
423 | if (ifas == NULL) |
424 | { |
425 | result = -1; |
426 | goto exit_free; |
427 | } |
428 | |
429 | /* Table for mapping kernel index to entry in our list. */ |
430 | map_newlink_data = alloca (newlink * sizeof (int)); |
431 | memset (map_newlink_data, '\xff', newlink * sizeof (int)); |
432 | |
433 | ifa_data_ptr = (char *) &ifas[newlink + newaddr]; |
434 | newaddr_idx = 0; /* Counter for newaddr index. */ |
435 | |
436 | /* Walk through the list of data we got from the kernel. */ |
437 | for (nlp = nh.nlm_list; nlp; nlp = nlp->next) |
438 | { |
439 | struct nlmsghdr *nlh; |
440 | size_t size = nlp->size; |
441 | |
442 | if (nlp->nlh == NULL) |
443 | continue; |
444 | |
445 | /* Walk through one message and look at the type: If it is our |
446 | message, we need RTM_NEWLINK/RTM_NEWADDR and stop if we reach |
447 | the end or we find the end marker (in this case we ignore the |
448 | following data. */ |
449 | for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size)) |
450 | { |
451 | int ifa_index = 0; |
452 | |
453 | /* Check if the message is the one we want */ |
454 | if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq) |
455 | continue; |
456 | |
457 | if (nlh->nlmsg_type == NLMSG_DONE) |
458 | break; /* ok */ |
459 | |
460 | if (nlh->nlmsg_type == RTM_NEWLINK) |
461 | { |
462 | /* We found a new interface. Now extract everything from the |
463 | interface data we got and need. */ |
464 | struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh); |
465 | struct rtattr *rta = IFLA_RTA (ifim); |
466 | size_t rtasize = IFLA_PAYLOAD (nlh); |
467 | |
468 | /* Interfaces are stored in the first "newlink" entries |
469 | of our list, starting in the order as we got from the |
470 | kernel. */ |
471 | ifa_index = map_newlink (ifim->ifi_index - 1, ifas, |
472 | map_newlink_data, newlink); |
473 | if (__glibc_unlikely (ifa_index == -1)) |
474 | { |
475 | try_again: |
476 | result = -EAGAIN; |
477 | free (ifas); |
478 | goto exit_free; |
479 | } |
480 | ifas[ifa_index].ifa.ifa_flags = ifim->ifi_flags; |
481 | |
482 | while (RTA_OK (rta, rtasize)) |
483 | { |
484 | char *rta_data = RTA_DATA (rta); |
485 | size_t rta_payload = RTA_PAYLOAD (rta); |
486 | |
487 | switch (rta->rta_type) |
488 | { |
489 | case IFLA_ADDRESS: |
490 | if (rta_payload <= sizeof (ifas[ifa_index].addr)) |
491 | { |
492 | ifas[ifa_index].addr.sl.sll_family = AF_PACKET; |
493 | memcpy (ifas[ifa_index].addr.sl.sll_addr, |
494 | (char *) rta_data, rta_payload); |
495 | ifas[ifa_index].addr.sl.sll_halen = rta_payload; |
496 | ifas[ifa_index].addr.sl.sll_ifindex |
497 | = ifim->ifi_index; |
498 | ifas[ifa_index].addr.sl.sll_hatype = ifim->ifi_type; |
499 | |
500 | ifas[ifa_index].ifa.ifa_addr |
501 | = &ifas[ifa_index].addr.sa; |
502 | } |
503 | break; |
504 | |
505 | case IFLA_BROADCAST: |
506 | if (rta_payload <= sizeof (ifas[ifa_index].broadaddr)) |
507 | { |
508 | ifas[ifa_index].broadaddr.sl.sll_family = AF_PACKET; |
509 | memcpy (ifas[ifa_index].broadaddr.sl.sll_addr, |
510 | (char *) rta_data, rta_payload); |
511 | ifas[ifa_index].broadaddr.sl.sll_halen = rta_payload; |
512 | ifas[ifa_index].broadaddr.sl.sll_ifindex |
513 | = ifim->ifi_index; |
514 | ifas[ifa_index].broadaddr.sl.sll_hatype |
515 | = ifim->ifi_type; |
516 | |
517 | ifas[ifa_index].ifa.ifa_broadaddr |
518 | = &ifas[ifa_index].broadaddr.sa; |
519 | } |
520 | break; |
521 | |
522 | case IFLA_IFNAME: /* Name of Interface */ |
523 | if ((rta_payload + 1) <= sizeof (ifas[ifa_index].name)) |
524 | { |
525 | ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name; |
526 | *(char *) __mempcpy (ifas[ifa_index].name, rta_data, |
527 | rta_payload) = '\0'; |
528 | } |
529 | break; |
530 | |
531 | case IFLA_STATS: /* Statistics of Interface */ |
532 | ifas[ifa_index].ifa.ifa_data = ifa_data_ptr; |
533 | ifa_data_ptr += rta_payload; |
534 | memcpy (ifas[ifa_index].ifa.ifa_data, rta_data, |
535 | rta_payload); |
536 | break; |
537 | |
538 | case IFLA_UNSPEC: |
539 | break; |
540 | case IFLA_MTU: |
541 | break; |
542 | case IFLA_LINK: |
543 | break; |
544 | case IFLA_QDISC: |
545 | break; |
546 | default: |
547 | break; |
548 | } |
549 | |
550 | rta = RTA_NEXT (rta, rtasize); |
551 | } |
552 | } |
553 | else if (nlh->nlmsg_type == RTM_NEWADDR) |
554 | { |
555 | struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlh); |
556 | struct rtattr *rta = IFA_RTA (ifam); |
557 | size_t rtasize = IFA_PAYLOAD (nlh); |
558 | |
559 | /* New Addresses are stored in the order we got them from |
560 | the kernel after the interfaces. Theoretically it is possible |
561 | that we have holes in the interface part of the list, |
562 | but we always have already the interface for this address. */ |
563 | ifa_index = newlink + newaddr_idx; |
564 | int idx = map_newlink (ifam->ifa_index - 1, ifas, |
565 | map_newlink_data, newlink); |
566 | if (__glibc_unlikely (idx == -1)) |
567 | goto try_again; |
568 | ifas[ifa_index].ifa.ifa_flags = ifas[idx].ifa.ifa_flags; |
569 | if (ifa_index > 0) |
570 | ifas[ifa_index - 1].ifa.ifa_next = &ifas[ifa_index].ifa; |
571 | ++newaddr_idx; |
572 | |
573 | while (RTA_OK (rta, rtasize)) |
574 | { |
575 | char *rta_data = RTA_DATA (rta); |
576 | size_t rta_payload = RTA_PAYLOAD (rta); |
577 | |
578 | switch (rta->rta_type) |
579 | { |
580 | case IFA_ADDRESS: |
581 | { |
582 | struct sockaddr *sa; |
583 | |
584 | if (ifas[ifa_index].ifa.ifa_addr != NULL) |
585 | { |
586 | /* In a point-to-poing network IFA_ADDRESS |
587 | contains the destination address, local |
588 | address is supplied in IFA_LOCAL attribute. |
589 | destination address and broadcast address |
590 | are stored in an union, so it doesn't matter |
591 | which name we use. */ |
592 | ifas[ifa_index].ifa.ifa_broadaddr |
593 | = &ifas[ifa_index].broadaddr.sa; |
594 | sa = &ifas[ifa_index].broadaddr.sa; |
595 | } |
596 | else |
597 | { |
598 | ifas[ifa_index].ifa.ifa_addr |
599 | = &ifas[ifa_index].addr.sa; |
600 | sa = &ifas[ifa_index].addr.sa; |
601 | } |
602 | |
603 | sa->sa_family = ifam->ifa_family; |
604 | |
605 | switch (ifam->ifa_family) |
606 | { |
607 | case AF_INET: |
608 | /* Size must match that of an address for IPv4. */ |
609 | if (rta_payload == 4) |
610 | memcpy (&((struct sockaddr_in *) sa)->sin_addr, |
611 | rta_data, rta_payload); |
612 | break; |
613 | |
614 | case AF_INET6: |
615 | /* Size must match that of an address for IPv6. */ |
616 | if (rta_payload == 16) |
617 | { |
618 | memcpy (&((struct sockaddr_in6 *) sa)->sin6_addr, |
619 | rta_data, rta_payload); |
620 | if (IN6_IS_ADDR_LINKLOCAL (rta_data) |
621 | || IN6_IS_ADDR_MC_LINKLOCAL (rta_data)) |
622 | ((struct sockaddr_in6 *) sa)->sin6_scope_id |
623 | = ifam->ifa_index; |
624 | } |
625 | break; |
626 | |
627 | default: |
628 | if (rta_payload <= sizeof (ifas[ifa_index].addr)) |
629 | memcpy (sa->sa_data, rta_data, rta_payload); |
630 | break; |
631 | } |
632 | } |
633 | break; |
634 | |
635 | case IFA_LOCAL: |
636 | if (ifas[ifa_index].ifa.ifa_addr != NULL) |
637 | { |
638 | /* If ifa_addr is set and we get IFA_LOCAL, |
639 | assume we have a point-to-point network. |
640 | Move address to correct field. */ |
641 | ifas[ifa_index].broadaddr = ifas[ifa_index].addr; |
642 | ifas[ifa_index].ifa.ifa_broadaddr |
643 | = &ifas[ifa_index].broadaddr.sa; |
644 | memset (&ifas[ifa_index].addr, '\0', |
645 | sizeof (ifas[ifa_index].addr)); |
646 | } |
647 | |
648 | ifas[ifa_index].ifa.ifa_addr = &ifas[ifa_index].addr.sa; |
649 | ifas[ifa_index].ifa.ifa_addr->sa_family |
650 | = ifam->ifa_family; |
651 | |
652 | switch (ifam->ifa_family) |
653 | { |
654 | case AF_INET: |
655 | /* Size must match that of an address for IPv4. */ |
656 | if (rta_payload == 4) |
657 | memcpy (&ifas[ifa_index].addr.s4.sin_addr, |
658 | rta_data, rta_payload); |
659 | break; |
660 | |
661 | case AF_INET6: |
662 | /* Size must match that of an address for IPv6. */ |
663 | if (rta_payload == 16) |
664 | { |
665 | memcpy (&ifas[ifa_index].addr.s6.sin6_addr, |
666 | rta_data, rta_payload); |
667 | if (IN6_IS_ADDR_LINKLOCAL (rta_data) |
668 | || IN6_IS_ADDR_MC_LINKLOCAL (rta_data)) |
669 | ifas[ifa_index].addr.s6.sin6_scope_id = |
670 | ifam->ifa_index; |
671 | } |
672 | break; |
673 | |
674 | default: |
675 | if (rta_payload <= sizeof (ifas[ifa_index].addr)) |
676 | memcpy (ifas[ifa_index].addr.sa.sa_data, |
677 | rta_data, rta_payload); |
678 | break; |
679 | } |
680 | break; |
681 | |
682 | case IFA_BROADCAST: |
683 | /* We get IFA_BROADCAST, so IFA_LOCAL was too much. */ |
684 | if (ifas[ifa_index].ifa.ifa_broadaddr != NULL) |
685 | memset (&ifas[ifa_index].broadaddr, '\0', |
686 | sizeof (ifas[ifa_index].broadaddr)); |
687 | |
688 | ifas[ifa_index].ifa.ifa_broadaddr |
689 | = &ifas[ifa_index].broadaddr.sa; |
690 | ifas[ifa_index].ifa.ifa_broadaddr->sa_family |
691 | = ifam->ifa_family; |
692 | |
693 | switch (ifam->ifa_family) |
694 | { |
695 | case AF_INET: |
696 | /* Size must match that of an address for IPv4. */ |
697 | if (rta_payload == 4) |
698 | memcpy (&ifas[ifa_index].broadaddr.s4.sin_addr, |
699 | rta_data, rta_payload); |
700 | break; |
701 | |
702 | case AF_INET6: |
703 | /* Size must match that of an address for IPv6. */ |
704 | if (rta_payload == 16) |
705 | { |
706 | memcpy (&ifas[ifa_index].broadaddr.s6.sin6_addr, |
707 | rta_data, rta_payload); |
708 | if (IN6_IS_ADDR_LINKLOCAL (rta_data) |
709 | || IN6_IS_ADDR_MC_LINKLOCAL (rta_data)) |
710 | ifas[ifa_index].broadaddr.s6.sin6_scope_id |
711 | = ifam->ifa_index; |
712 | } |
713 | break; |
714 | |
715 | default: |
716 | if (rta_payload <= sizeof (ifas[ifa_index].addr)) |
717 | memcpy (&ifas[ifa_index].broadaddr.sa.sa_data, |
718 | rta_data, rta_payload); |
719 | break; |
720 | } |
721 | break; |
722 | |
723 | case IFA_LABEL: |
724 | if (rta_payload + 1 <= sizeof (ifas[ifa_index].name)) |
725 | { |
726 | ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name; |
727 | *(char *) __mempcpy (ifas[ifa_index].name, rta_data, |
728 | rta_payload) = '\0'; |
729 | } |
730 | else |
731 | abort (); |
732 | break; |
733 | |
734 | case IFA_UNSPEC: |
735 | break; |
736 | case IFA_CACHEINFO: |
737 | break; |
738 | default: |
739 | break; |
740 | } |
741 | |
742 | rta = RTA_NEXT (rta, rtasize); |
743 | } |
744 | |
745 | /* If we didn't get the interface name with the |
746 | address, use the name from the interface entry. */ |
747 | if (ifas[ifa_index].ifa.ifa_name == NULL) |
748 | { |
749 | int idx = map_newlink (ifam->ifa_index - 1, ifas, |
750 | map_newlink_data, newlink); |
751 | if (__glibc_unlikely (idx == -1)) |
752 | goto try_again; |
753 | ifas[ifa_index].ifa.ifa_name = ifas[idx].ifa.ifa_name; |
754 | } |
755 | |
756 | /* Calculate the netmask. */ |
757 | if (ifas[ifa_index].ifa.ifa_addr |
758 | && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_UNSPEC |
759 | && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_PACKET) |
760 | { |
761 | uint32_t max_prefixlen = 0; |
762 | char *cp = NULL; |
763 | |
764 | ifas[ifa_index].ifa.ifa_netmask |
765 | = &ifas[ifa_index].netmask.sa; |
766 | |
767 | switch (ifas[ifa_index].ifa.ifa_addr->sa_family) |
768 | { |
769 | case AF_INET: |
770 | cp = (char *) &ifas[ifa_index].netmask.s4.sin_addr; |
771 | max_prefixlen = 32; |
772 | break; |
773 | |
774 | case AF_INET6: |
775 | cp = (char *) &ifas[ifa_index].netmask.s6.sin6_addr; |
776 | max_prefixlen = 128; |
777 | break; |
778 | } |
779 | |
780 | ifas[ifa_index].ifa.ifa_netmask->sa_family |
781 | = ifas[ifa_index].ifa.ifa_addr->sa_family; |
782 | |
783 | if (cp != NULL) |
784 | { |
785 | unsigned int preflen; |
786 | |
787 | if (ifam->ifa_prefixlen > max_prefixlen) |
788 | preflen = max_prefixlen; |
789 | else |
790 | preflen = ifam->ifa_prefixlen; |
791 | |
792 | for (i = 0; i < preflen / 8; i++) |
793 | *cp++ = 0xff; |
794 | if (preflen % 8) |
795 | *cp = 0xff << (8 - preflen % 8); |
796 | } |
797 | } |
798 | } |
799 | } |
800 | } |
801 | |
802 | assert (ifa_data_ptr <= (char *) &ifas[newlink + newaddr] + ifa_data_size); |
803 | |
804 | if (newaddr_idx > 0) |
805 | { |
806 | for (i = 0; i < newlink; ++i) |
807 | if (map_newlink_data[i] == -1) |
808 | { |
809 | /* We have fewer links then we anticipated. Adjust the |
810 | forward pointer to the first address entry. */ |
811 | ifas[i - 1].ifa.ifa_next = &ifas[newlink].ifa; |
812 | } |
813 | |
814 | if (i == 0 && newlink > 0) |
815 | /* No valid link, but we allocated memory. We have to |
816 | populate the first entry. */ |
817 | memmove (ifas, &ifas[newlink], sizeof (struct ifaddrs_storage)); |
818 | } |
819 | |
820 | *ifap = &ifas[0].ifa; |
821 | |
822 | exit_free: |
823 | __netlink_free_handle (&nh); |
824 | __netlink_close (&nh); |
825 | |
826 | return result; |
827 | } |
828 | |
829 | |
830 | /* Create a linked list of `struct ifaddrs' structures, one for each |
831 | network interface on the host machine. If successful, store the |
832 | list in *IFAP and return 0. On errors, return -1 and set `errno'. */ |
833 | int |
834 | __getifaddrs (struct ifaddrs **ifap) |
835 | { |
836 | int res; |
837 | |
838 | do |
839 | res = getifaddrs_internal (ifap); |
840 | while (res == -EAGAIN); |
841 | |
842 | return res; |
843 | } |
844 | weak_alias (__getifaddrs, getifaddrs) |
845 | libc_hidden_weak (getifaddrs) |
846 | |
847 | |
848 | void |
849 | __freeifaddrs (struct ifaddrs *ifa) |
850 | { |
851 | free (ifa); |
852 | } |
853 | weak_alias (__freeifaddrs, freeifaddrs) |
854 | libc_hidden_weak (freeifaddrs) |
855 | |