OSDN Git Service

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