/* Create a linked list of `struct ifaddrs' structures, one for each network interface on the host machine. If successful, store the list in *IFAP and return 0. On errors, return -1 and set `errno'. */ int getifaddrs (struct ifaddrs **ifap) { struct netlink_handle nh = { 0, 0, 0, NULL, NULL }; struct netlink_res *nlp; struct ifaddrs_storage *ifas; unsigned int i, newlink, newaddr, newaddr_idx; int *map_newlink_data; size_t ifa_data_size = 0; /* Size to allocate for all ifa_data. */ char *ifa_data_ptr; /* Pointer to the unused part of memory for ifa_data. */ int result = 0; if (ifap) *ifap = NULL; if (! __no_netlink_support && __netlink_open (&nh) < 0) { #if __ASSUME_NETLINK_SUPPORT != 0 return -1; #endif } #if __ASSUME_NETLINK_SUPPORT == 0 if (__no_netlink_support) return fallback_getifaddrs (ifap); #endif /* Tell the kernel that we wish to get a list of all active interfaces, collect all data for every interface. */ if (__netlink_request (&nh, RTM_GETLINK) < 0) { result = -1; goto exit_free; } /* Now ask the kernel for all addresses which are assigned to an interface and collect all data for every interface. Since we store the addresses after the interfaces in the list, we will later always find the interface before the corresponding addresses. */ ++nh.seq; if (__netlink_request (&nh, RTM_GETADDR) < 0) { result = -1; goto exit_free; } /* Count all RTM_NEWLINK and RTM_NEWADDR entries to allocate enough memory. */ newlink = newaddr = 0; for (nlp = nh.nlm_list; nlp; nlp = nlp->next) { struct nlmsghdr *nlh; size_t size = nlp->size; if (nlp->nlh == NULL) continue; /* Walk through all entries we got from the kernel and look, which message type they contain. */ for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size)) { /* Check if the message is what we want. */ if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq) continue; if (nlh->nlmsg_type == NLMSG_DONE) break; /* ok */ if (nlh->nlmsg_type == RTM_NEWLINK) { /* A RTM_NEWLINK message can have IFLA_STATS data. We need to know the size before creating the list to allocate enough memory. */ struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh); struct rtattr *rta = IFLA_RTA (ifim); size_t rtasize = IFLA_PAYLOAD (nlh); while (RTA_OK (rta, rtasize)) { size_t rta_payload = RTA_PAYLOAD (rta); if (rta->rta_type == IFLA_STATS) { ifa_data_size += rta_payload; break; } else rta = RTA_NEXT (rta, rtasize); } ++newlink; } else if (nlh->nlmsg_type == RTM_NEWADDR) ++newaddr; } } /* Return if no interface is up. */ if ((newlink + newaddr) == 0) goto exit_free; /* Allocate memory for all entries we have and initialize next pointer. */ ifas = (struct ifaddrs_storage *) calloc (1, (newlink + newaddr) * sizeof (struct ifaddrs_storage) + ifa_data_size); if (ifas == NULL) { result = -1; goto exit_free; } /* Table for mapping kernel index to entry in our list. */ map_newlink_data = alloca (newlink * sizeof (int)); memset (map_newlink_data, '\xff', newlink * sizeof (int)); ifa_data_ptr = (char *) &ifas[newlink + newaddr]; newaddr_idx = 0; /* Counter for newaddr index. */ /* Walk through the list of data we got from the kernel. */ for (nlp = nh.nlm_list; nlp; nlp = nlp->next) { struct nlmsghdr *nlh; size_t size = nlp->size; if (nlp->nlh == NULL) continue; /* Walk through one message and look at the type: If it is our message, we need RTM_NEWLINK/RTM_NEWADDR and stop if we reach the end or we find the end marker (in this case we ignore the following data. */ for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size)) { int ifa_index = 0; /* Check if the message is the one we want */ if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq) continue; if (nlh->nlmsg_type == NLMSG_DONE) break; /* ok */ if (nlh->nlmsg_type == RTM_NEWLINK) { /* We found a new interface. Now extract everything from the interface data we got and need. */ struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh); struct rtattr *rta = IFLA_RTA (ifim); size_t rtasize = IFLA_PAYLOAD (nlh); /* Interfaces are stored in the first "newlink" entries of our list, starting in the order as we got from the kernel. */ ifa_index = map_newlink (ifim->ifi_index - 1, ifas, map_newlink_data, newlink); ifas[ifa_index].ifa.ifa_flags = ifim->ifi_flags; while (RTA_OK (rta, rtasize)) { char *rta_data = RTA_DATA (rta); size_t rta_payload = RTA_PAYLOAD (rta); switch (rta->rta_type) { case IFLA_ADDRESS: if (rta_payload <= sizeof (ifas[ifa_index].addr)) { ifas[ifa_index].addr.sl.sll_family = AF_PACKET; memcpy (ifas[ifa_index].addr.sl.sll_addr, (char *) rta_data, rta_payload); ifas[ifa_index].addr.sl.sll_halen = rta_payload; ifas[ifa_index].addr.sl.sll_ifindex = ifim->ifi_index; ifas[ifa_index].addr.sl.sll_hatype = ifim->ifi_type; ifas[ifa_index].ifa.ifa_addr = &ifas[ifa_index].addr.sa; } break; case IFLA_BROADCAST: if (rta_payload <= sizeof (ifas[ifa_index].broadaddr)) { ifas[ifa_index].broadaddr.sl.sll_family = AF_PACKET; memcpy (ifas[ifa_index].broadaddr.sl.sll_addr, (char *) rta_data, rta_payload); ifas[ifa_index].broadaddr.sl.sll_halen = rta_payload; ifas[ifa_index].broadaddr.sl.sll_ifindex = ifim->ifi_index; ifas[ifa_index].broadaddr.sl.sll_hatype = ifim->ifi_type; ifas[ifa_index].ifa.ifa_broadaddr = &ifas[ifa_index].broadaddr.sa; } break; case IFLA_IFNAME: /* Name of Interface */ if ((rta_payload + 1) <= sizeof (ifas[ifa_index].name)) { ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name; *(char *) __mempcpy (ifas[ifa_index].name, rta_data, rta_payload) = '\0'; } break; case IFLA_STATS: /* Statistics of Interface */ ifas[ifa_index].ifa.ifa_data = ifa_data_ptr; ifa_data_ptr += rta_payload; memcpy (ifas[ifa_index].ifa.ifa_data, rta_data, rta_payload); break; case IFLA_UNSPEC: break; case IFLA_MTU: break; case IFLA_LINK: break; case IFLA_QDISC: break; default: break; } rta = RTA_NEXT (rta, rtasize); } } else if (nlh->nlmsg_type == RTM_NEWADDR) { struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlh); struct rtattr *rta = IFA_RTA (ifam); size_t rtasize = IFA_PAYLOAD (nlh); /* New Addresses are stored in the order we got them from the kernel after the interfaces. Theoretically it is possible that we have holes in the interface part of the list, but we always have already the interface for this address. */ ifa_index = newlink + newaddr_idx; ifas[ifa_index].ifa.ifa_flags = ifas[map_newlink (ifam->ifa_index - 1, ifas, map_newlink_data, newlink)].ifa.ifa_flags; if (ifa_index > 0) ifas[ifa_index - 1].ifa.ifa_next = &ifas[ifa_index].ifa; ++newaddr_idx; while (RTA_OK (rta, rtasize)) { char *rta_data = RTA_DATA (rta); size_t rta_payload = RTA_PAYLOAD (rta); switch (rta->rta_type) { case IFA_ADDRESS: { struct sockaddr *sa; if (ifas[ifa_index].ifa.ifa_addr != NULL) { /* In a point-to-poing network IFA_ADDRESS contains the destination address, local address is supplied in IFA_LOCAL attribute. destination address and broadcast address are stored in an union, so it doesn't matter which name we use. */ ifas[ifa_index].ifa.ifa_broadaddr = &ifas[ifa_index].broadaddr.sa; sa = &ifas[ifa_index].broadaddr.sa; } else { ifas[ifa_index].ifa.ifa_addr = &ifas[ifa_index].addr.sa; sa = &ifas[ifa_index].addr.sa; } sa->sa_family = ifam->ifa_family; switch (ifam->ifa_family) { case AF_INET: /* Size must match that of an address for IPv4. */ if (rta_payload == 4) memcpy (&((struct sockaddr_in *) sa)->sin_addr, rta_data, rta_payload); break; case AF_INET6: /* Size must match that of an address for IPv6. */ if (rta_payload == 16) { memcpy (&((struct sockaddr_in6 *) sa)->sin6_addr, rta_data, rta_payload); if (IN6_IS_ADDR_LINKLOCAL (rta_data) || IN6_IS_ADDR_MC_LINKLOCAL (rta_data)) ((struct sockaddr_in6 *) sa)->sin6_scope_id = ifam->ifa_index; } break; default: if (rta_payload <= sizeof (ifas[ifa_index].addr)) memcpy (sa->sa_data, rta_data, rta_payload); break; } } break; case IFA_LOCAL: if (ifas[ifa_index].ifa.ifa_addr != NULL) { /* If ifa_addr is set and we get IFA_LOCAL, assume we have a point-to-point network. Move address to correct field. */ ifas[ifa_index].broadaddr = ifas[ifa_index].addr; ifas[ifa_index].ifa.ifa_broadaddr = &ifas[ifa_index].broadaddr.sa; memset (&ifas[ifa_index].addr, '\0', sizeof (ifas[ifa_index].addr)); } ifas[ifa_index].ifa.ifa_addr = &ifas[ifa_index].addr.sa; ifas[ifa_index].ifa.ifa_addr->sa_family = ifam->ifa_family; switch (ifam->ifa_family) { case AF_INET: /* Size must match that of an address for IPv4. */ if (rta_payload == 4) memcpy (&ifas[ifa_index].addr.s4.sin_addr, rta_data, rta_payload); break; case AF_INET6: /* Size must match that of an address for IPv6. */ if (rta_payload == 16) { memcpy (&ifas[ifa_index].addr.s6.sin6_addr, rta_data, rta_payload); if (IN6_IS_ADDR_LINKLOCAL (rta_data) || IN6_IS_ADDR_MC_LINKLOCAL (rta_data)) ifas[ifa_index].addr.s6.sin6_scope_id = ifam->ifa_index; } break; default: if (rta_payload <= sizeof (ifas[ifa_index].addr)) memcpy (ifas[ifa_index].addr.sa.sa_data, rta_data, rta_payload); break; } break; case IFA_BROADCAST: /* We get IFA_BROADCAST, so IFA_LOCAL was too much. */ if (ifas[ifa_index].ifa.ifa_broadaddr != NULL) memset (&ifas[ifa_index].broadaddr, '\0', sizeof (ifas[ifa_index].broadaddr)); ifas[ifa_index].ifa.ifa_broadaddr = &ifas[ifa_index].broadaddr.sa; ifas[ifa_index].ifa.ifa_broadaddr->sa_family = ifam->ifa_family; switch (ifam->ifa_family) { case AF_INET: /* Size must match that of an address for IPv4. */ if (rta_payload == 4) memcpy (&ifas[ifa_index].broadaddr.s4.sin_addr, rta_data, rta_payload); break; case AF_INET6: /* Size must match that of an address for IPv6. */ if (rta_payload == 16) { memcpy (&ifas[ifa_index].broadaddr.s6.sin6_addr, rta_data, rta_payload); if (IN6_IS_ADDR_LINKLOCAL (rta_data) || IN6_IS_ADDR_MC_LINKLOCAL (rta_data)) ifas[ifa_index].broadaddr.s6.sin6_scope_id = ifam->ifa_index; } break; default: if (rta_payload <= sizeof (ifas[ifa_index].addr)) memcpy (&ifas[ifa_index].broadaddr.sa.sa_data, rta_data, rta_payload); break; } break; case IFA_LABEL: if (rta_payload + 1 <= sizeof (ifas[ifa_index].name)) { ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name; *(char *) __mempcpy (ifas[ifa_index].name, rta_data, rta_payload) = '\0'; } else abort (); break; case IFA_UNSPEC: break; case IFA_CACHEINFO: break; default: break; } rta = RTA_NEXT (rta, rtasize); } /* If we didn't get the interface name with the address, use the name from the interface entry. */ if (ifas[ifa_index].ifa.ifa_name == NULL) ifas[ifa_index].ifa.ifa_name = ifas[map_newlink (ifam->ifa_index - 1, ifas, map_newlink_data, newlink)].ifa.ifa_name; /* Calculate the netmask. */ if (ifas[ifa_index].ifa.ifa_addr && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_UNSPEC && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_PACKET) { uint32_t max_prefixlen = 0; char *cp = NULL; ifas[ifa_index].ifa.ifa_netmask = &ifas[ifa_index].netmask.sa; switch (ifas[ifa_index].ifa.ifa_addr->sa_family) { case AF_INET: cp = (char *) &ifas[ifa_index].netmask.s4.sin_addr; max_prefixlen = 32; break; case AF_INET6: cp = (char *) &ifas[ifa_index].netmask.s6.sin6_addr; max_prefixlen = 128; break; } ifas[ifa_index].ifa.ifa_netmask->sa_family = ifas[ifa_index].ifa.ifa_addr->sa_family; if (cp != NULL) { char c; unsigned int preflen; if ((max_prefixlen > 0) && (ifam->ifa_prefixlen > max_prefixlen)) preflen = max_prefixlen; else preflen = ifam->ifa_prefixlen; for (i = 0; i < (preflen / 8); i++) *cp++ = 0xff; c = 0xff; c <<= (8 - (preflen % 8)); *cp = c; } } } } } assert (ifa_data_ptr <= (char *) &ifas[newlink + newaddr] + ifa_data_size); if (newaddr_idx > 0) { for (i = 0; i < newlink; ++i) if (map_newlink_data[i] == -1) { /* We have fewer links then we anticipated. Adjust the forward pointer to the first address entry. */ ifas[i - 1].ifa.ifa_next = &ifas[newlink].ifa; } if (i == 0 && newlink > 0) /* No valid link, but we allocated memory. We have to populate the first entry. */ memmove (ifas, &ifas[newlink], sizeof (struct ifaddrs_storage)); } if (ifap != NULL) *ifap = &ifas[0].ifa; exit_free: __netlink_free_handle (&nh); __netlink_close (&nh); return result; }
static struct if_nameindex * if_nameindex_netlink (void) { struct netlink_handle nh = { 0, 0, 0, NULL, NULL }; struct if_nameindex *idx = NULL; if (__no_netlink_support || __netlink_open (&nh) < 0) return NULL; /* Tell the kernel that we wish to get a list of all active interfaces. Collect all data for every interface. */ if (__netlink_request (&nh, RTM_GETLINK) < 0) goto exit_free; /* Count the interfaces. */ unsigned int nifs = 0; for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next) { struct nlmsghdr *nlh; size_t size = nlp->size; if (nlp->nlh == NULL) continue; /* Walk through all entries we got from the kernel and look, which message type they contain. */ for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size)) { /* Check if the message is what we want. */ if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq) continue; if (nlh->nlmsg_type == NLMSG_DONE) break; /* ok */ if (nlh->nlmsg_type == RTM_NEWLINK) ++nifs; } } idx = malloc ((nifs + 1) * sizeof (struct if_nameindex)); if (idx == NULL) { nomem: __set_errno (ENOBUFS); goto exit_free; } /* Add the interfaces. */ nifs = 0; for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next) { struct nlmsghdr *nlh; size_t size = nlp->size; if (nlp->nlh == NULL) continue; /* Walk through all entries we got from the kernel and look, which message type they contain. */ for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size)) { /* Check if the message is what we want. */ if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq) continue; if (nlh->nlmsg_type == NLMSG_DONE) break; /* ok */ if (nlh->nlmsg_type == RTM_NEWLINK) { struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh); struct rtattr *rta = IFLA_RTA (ifim); size_t rtasize = IFLA_PAYLOAD (nlh); idx[nifs].if_index = ifim->ifi_index; while (RTA_OK (rta, rtasize)) { char *rta_data = RTA_DATA (rta); size_t rta_payload = RTA_PAYLOAD (rta); if (rta->rta_type == IFLA_IFNAME) { idx[nifs].if_name = __strndup (rta_data, rta_payload); if (idx[nifs].if_name == NULL) { idx[nifs].if_index = 0; if_freenameindex (idx); idx = NULL; goto nomem; } break; } rta = RTA_NEXT (rta, rtasize); } ++nifs; } } } idx[nifs].if_index = 0; idx[nifs].if_name = NULL; exit_free: __netlink_free_handle (&nh); __netlink_close (&nh); return idx; }