Back to index

glibc  2.9
check_pf.c
Go to the documentation of this file.
00001 /* Determine protocol families for which interfaces exist.  Linux version.
00002    Copyright (C) 2003, 2006, 2007, 2008 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <assert.h>
00021 #include <errno.h>
00022 #include <ifaddrs.h>
00023 #include <netdb.h>
00024 #include <stddef.h>
00025 #include <string.h>
00026 #include <time.h>
00027 #include <unistd.h>
00028 #include <sys/socket.h>
00029 
00030 #include <asm/types.h>
00031 #include <linux/netlink.h>
00032 #include <linux/rtnetlink.h>
00033 
00034 #include <not-cancel.h>
00035 #include <kernel-features.h>
00036 
00037 
00038 #ifndef IFA_F_HOMEADDRESS
00039 # define IFA_F_HOMEADDRESS 0
00040 #endif
00041 #ifndef IFA_F_OPTIMISTIC
00042 # define IFA_F_OPTIMISTIC 0
00043 #endif
00044 
00045 
00046 static int
00047 make_request (int fd, pid_t pid, bool *seen_ipv4, bool *seen_ipv6,
00048              struct in6addrinfo **in6ai, size_t *in6ailen)
00049 {
00050   struct req
00051   {
00052     struct nlmsghdr nlh;
00053     struct rtgenmsg g;
00054     /* struct rtgenmsg consists of a single byte.  This means there
00055        are three bytes of padding included in the REQ definition.
00056        We make them explicit here.  */
00057     char pad[3];
00058   } req;
00059   struct sockaddr_nl nladdr;
00060 
00061   req.nlh.nlmsg_len = sizeof (req);
00062   req.nlh.nlmsg_type = RTM_GETADDR;
00063   req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
00064   req.nlh.nlmsg_pid = 0;
00065   req.nlh.nlmsg_seq = time (NULL);
00066   req.g.rtgen_family = AF_UNSPEC;
00067 
00068   assert (sizeof (req) - offsetof (struct req, pad) == 3);
00069   memset (req.pad, '\0', sizeof (req.pad));
00070 
00071   memset (&nladdr, '\0', sizeof (nladdr));
00072   nladdr.nl_family = AF_NETLINK;
00073 
00074 #ifdef PAGE_SIZE
00075   /* Help the compiler optimize out the malloc call if PAGE_SIZE
00076      is constant and smaller or equal to PTHREAD_STACK_MIN/4.  */
00077   const size_t buf_size = PAGE_SIZE;
00078 #else
00079   const size_t buf_size = __getpagesize ();
00080 #endif
00081   bool use_malloc = false;
00082   char *buf;
00083 
00084   if (__libc_use_alloca (buf_size))
00085     buf = alloca (buf_size);
00086   else
00087     {
00088       buf = malloc (buf_size);
00089       if (buf != NULL)
00090        use_malloc = true;
00091       else
00092        goto out_fail;
00093     }
00094 
00095   struct iovec iov = { buf, buf_size };
00096 
00097   if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0,
00098                                 (struct sockaddr *) &nladdr,
00099                                 sizeof (nladdr))) < 0)
00100     goto out_fail;
00101 
00102   *seen_ipv4 = false;
00103   *seen_ipv6 = false;
00104 
00105   bool done = false;
00106   struct in6ailist
00107   {
00108     struct in6addrinfo info;
00109     struct in6ailist *next;
00110   } *in6ailist = NULL;
00111   size_t in6ailistlen = 0;
00112 
00113   do
00114     {
00115       struct msghdr msg =
00116        {
00117          (void *) &nladdr, sizeof (nladdr),
00118          &iov, 1,
00119          NULL, 0,
00120          0
00121        };
00122 
00123       ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0));
00124       if (read_len < 0)
00125        goto out_fail;
00126 
00127       if (msg.msg_flags & MSG_TRUNC)
00128        goto out_fail;
00129 
00130       struct nlmsghdr *nlmh;
00131       for (nlmh = (struct nlmsghdr *) buf;
00132           NLMSG_OK (nlmh, (size_t) read_len);
00133           nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, read_len))
00134        {
00135          if (nladdr.nl_pid != 0 || (pid_t) nlmh->nlmsg_pid != pid
00136              || nlmh->nlmsg_seq != req.nlh.nlmsg_seq)
00137            continue;
00138 
00139          if (nlmh->nlmsg_type == RTM_NEWADDR)
00140            {
00141              struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlmh);
00142              struct rtattr *rta = IFA_RTA (ifam);
00143              size_t len = nlmh->nlmsg_len - NLMSG_LENGTH (sizeof (*ifam));
00144 
00145              if (ifam->ifa_family != AF_INET
00146                 && ifam->ifa_family != AF_INET6)
00147               continue;
00148 
00149              const void *local = NULL;
00150              const void *address = NULL;
00151              while (RTA_OK (rta, len))
00152               {
00153                 switch (rta->rta_type)
00154                   {
00155                   case IFA_LOCAL:
00156                     local = RTA_DATA (rta);
00157                     break;
00158 
00159                   case IFA_ADDRESS:
00160                     address = RTA_DATA (rta);
00161                     goto out;
00162                   }
00163 
00164                 rta = RTA_NEXT (rta, len);
00165               }
00166 
00167              if (local != NULL)
00168               {
00169                 address = local;
00170               out:
00171                 if (ifam->ifa_family == AF_INET)
00172                   {
00173                     if (*(const in_addr_t *) address
00174                        != htonl (INADDR_LOOPBACK))
00175                      *seen_ipv4 = true;
00176                   }
00177                 else
00178                   {
00179                     if (!IN6_IS_ADDR_LOOPBACK (address))
00180                      *seen_ipv6 = true;
00181                   }
00182               }
00183 
00184              struct in6ailist *newp = alloca (sizeof (*newp));
00185              newp->info.flags = (((ifam->ifa_flags
00186                                 & (IFA_F_DEPRECATED
00187                                    | IFA_F_OPTIMISTIC))
00188                                ? in6ai_deprecated : 0)
00189                               | ((ifam->ifa_flags
00190                                   & IFA_F_HOMEADDRESS)
00191                                  ? in6ai_homeaddress : 0));
00192              newp->info.prefixlen = ifam->ifa_prefixlen;
00193              newp->info.index = ifam->ifa_index;
00194              if (ifam->ifa_family == AF_INET)
00195               {
00196                 newp->info.addr[0] = 0;
00197                 newp->info.addr[1] = 0;
00198                 newp->info.addr[2] = htonl (0xffff);
00199                 newp->info.addr[3] = *(const in_addr_t *) address;
00200               }
00201              else
00202               memcpy (newp->info.addr, address, sizeof (newp->info.addr));
00203              newp->next = in6ailist;
00204              in6ailist = newp;
00205              ++in6ailistlen;
00206            }
00207          else if (nlmh->nlmsg_type == NLMSG_DONE)
00208            /* We found the end, leave the loop.  */
00209            done = true;
00210        }
00211     }
00212   while (! done);
00213 
00214   close_not_cancel_no_status (fd);
00215 
00216   if (*seen_ipv6 && in6ailist != NULL)
00217     {
00218       *in6ai = malloc (in6ailistlen * sizeof (**in6ai));
00219       if (*in6ai == NULL)
00220        goto out_fail;
00221 
00222       *in6ailen = in6ailistlen;
00223 
00224       do
00225        {
00226          (*in6ai)[--in6ailistlen] = in6ailist->info;
00227          in6ailist = in6ailist->next;
00228        }
00229       while (in6ailist != NULL);
00230     }
00231 
00232   if (use_malloc)
00233     free (buf);
00234   return 0;
00235 
00236 out_fail:
00237   if (use_malloc)
00238     free (buf);
00239   return -1;
00240 }
00241 
00242 
00243 /* We don't know if we have NETLINK support compiled in in our
00244    Kernel.  */
00245 #if __ASSUME_NETLINK_SUPPORT == 0
00246 /* Define in ifaddrs.h.  */
00247 extern int __no_netlink_support attribute_hidden;
00248 #else
00249 # define __no_netlink_support 0
00250 #endif
00251 
00252 
00253 void
00254 attribute_hidden
00255 __check_pf (bool *seen_ipv4, bool *seen_ipv6,
00256            struct in6addrinfo **in6ai, size_t *in6ailen)
00257 {
00258   *in6ai = NULL;
00259   *in6ailen = 0;
00260 
00261   if (! __no_netlink_support)
00262     {
00263       int fd = __socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
00264 
00265       struct sockaddr_nl nladdr;
00266       memset (&nladdr, '\0', sizeof (nladdr));
00267       nladdr.nl_family = AF_NETLINK;
00268 
00269       socklen_t addr_len = sizeof (nladdr);
00270 
00271       if (fd >= 0
00272          && __bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) == 0
00273          && __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) == 0
00274          && make_request (fd, nladdr.nl_pid, seen_ipv4, seen_ipv6,
00275                         in6ai, in6ailen) == 0)
00276        /* It worked.  */
00277        return;
00278 
00279       if (fd >= 0)
00280        __close (fd);
00281 
00282 #if __ASSUME_NETLINK_SUPPORT == 0
00283       /* Remember that there is no netlink support.  */
00284       __no_netlink_support = 1;
00285 #else
00286       /* We cannot determine what interfaces are available.  Be
00287         pessimistic.  */
00288       *seen_ipv4 = true;
00289       *seen_ipv6 = true;
00290 #endif
00291     }
00292 
00293 #if __ASSUME_NETLINK_SUPPORT == 0
00294   /* No netlink.  Get the interface list via getifaddrs.  */
00295   struct ifaddrs *ifa = NULL;
00296   if (getifaddrs (&ifa) != 0)
00297     {
00298       /* We cannot determine what interfaces are available.  Be
00299         pessimistic.  */
00300       *seen_ipv4 = true;
00301       *seen_ipv6 = true;
00302       return;
00303     }
00304 
00305   struct ifaddrs *runp;
00306   for (runp = ifa; runp != NULL; runp = runp->ifa_next)
00307     if (runp->ifa_addr->sa_family == PF_INET)
00308       *seen_ipv4 = true;
00309     else if (runp->ifa_addr->sa_family == PF_INET6)
00310       *seen_ipv6 = true;
00311 
00312   (void) freeifaddrs (ifa);
00313 #endif
00314 }