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   } req;
00055   struct sockaddr_nl nladdr;
00056 
00057   /* struct rtgenmsg consists of a single byte but the ARM ABI rounds
00058      it up to a word.  Clear the padding explicitly here.  */
00059   assert (sizeof (req.g) == 4);
00060   memset (&req.g, '\0', sizeof (req.g));
00061 
00062   req.nlh.nlmsg_len = sizeof (req);
00063   req.nlh.nlmsg_type = RTM_GETADDR;
00064   req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
00065   req.nlh.nlmsg_pid = 0;
00066   req.nlh.nlmsg_seq = time (NULL);
00067   req.g.rtgen_family = AF_UNSPEC;
00068 
00069   memset (&nladdr, '\0', sizeof (nladdr));
00070   nladdr.nl_family = AF_NETLINK;
00071 
00072 #ifdef PAGE_SIZE
00073   /* Help the compiler optimize out the malloc call if PAGE_SIZE
00074      is constant and smaller or equal to PTHREAD_STACK_MIN/4.  */
00075   const size_t buf_size = PAGE_SIZE;
00076 #else
00077   const size_t buf_size = __getpagesize ();
00078 #endif
00079   bool use_malloc = false;
00080   char *buf;
00081 
00082   if (__libc_use_alloca (buf_size))
00083     buf = alloca (buf_size);
00084   else
00085     {
00086       buf = malloc (buf_size);
00087       if (buf != NULL)
00088        use_malloc = true;
00089       else
00090        goto out_fail;
00091     }
00092 
00093   struct iovec iov = { buf, buf_size };
00094 
00095   if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0,
00096                                 (struct sockaddr *) &nladdr,
00097                                 sizeof (nladdr))) < 0)
00098     goto out_fail;
00099 
00100   *seen_ipv4 = false;
00101   *seen_ipv6 = false;
00102 
00103   bool done = false;
00104   struct in6ailist
00105   {
00106     struct in6addrinfo info;
00107     struct in6ailist *next;
00108   } *in6ailist = NULL;
00109   size_t in6ailistlen = 0;
00110 
00111   do
00112     {
00113       struct msghdr msg =
00114        {
00115          (void *) &nladdr, sizeof (nladdr),
00116          &iov, 1,
00117          NULL, 0,
00118          0
00119        };
00120 
00121       ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0));
00122       if (read_len < 0)
00123        goto out_fail;
00124 
00125       if (msg.msg_flags & MSG_TRUNC)
00126        goto out_fail;
00127 
00128       struct nlmsghdr *nlmh;
00129       for (nlmh = (struct nlmsghdr *) buf;
00130           NLMSG_OK (nlmh, (size_t) read_len);
00131           nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, read_len))
00132        {
00133          if (nladdr.nl_pid != 0 || (pid_t) nlmh->nlmsg_pid != pid
00134              || nlmh->nlmsg_seq != req.nlh.nlmsg_seq)
00135            continue;
00136 
00137          if (nlmh->nlmsg_type == RTM_NEWADDR)
00138            {
00139              struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlmh);
00140              struct rtattr *rta = IFA_RTA (ifam);
00141              size_t len = nlmh->nlmsg_len - NLMSG_LENGTH (sizeof (*ifam));
00142 
00143              if (ifam->ifa_family != AF_INET
00144                 && ifam->ifa_family != AF_INET6)
00145               continue;
00146 
00147              const void *local = NULL;
00148              const void *address = NULL;
00149              while (RTA_OK (rta, len))
00150               {
00151                 switch (rta->rta_type)
00152                   {
00153                   case IFA_LOCAL:
00154                     local = RTA_DATA (rta);
00155                     break;
00156 
00157                   case IFA_ADDRESS:
00158                     address = RTA_DATA (rta);
00159                     goto out;
00160                   }
00161 
00162                 rta = RTA_NEXT (rta, len);
00163               }
00164 
00165              if (local != NULL)
00166               {
00167                 address = local;
00168               out:
00169                 if (ifam->ifa_family == AF_INET)
00170                   {
00171                     if (*(const in_addr_t *) address
00172                        != htonl (INADDR_LOOPBACK))
00173                      *seen_ipv4 = true;
00174                   }
00175                 else
00176                   {
00177                     if (!IN6_IS_ADDR_LOOPBACK (address))
00178                      *seen_ipv6 = true;
00179                   }
00180               }
00181 
00182              struct in6ailist *newp = alloca (sizeof (*newp));
00183              newp->info.flags = (((ifam->ifa_flags
00184                                 & (IFA_F_DEPRECATED
00185                                    | IFA_F_OPTIMISTIC))
00186                                ? in6ai_deprecated : 0)
00187                               | ((ifam->ifa_flags
00188                                   & IFA_F_HOMEADDRESS)
00189                                  ? in6ai_homeaddress : 0));
00190              newp->info.prefixlen = ifam->ifa_prefixlen;
00191              newp->info.index = ifam->ifa_index;
00192              if (ifam->ifa_family == AF_INET)
00193               {
00194                 newp->info.addr[0] = 0;
00195                 newp->info.addr[1] = 0;
00196                 newp->info.addr[2] = htonl (0xffff);
00197                 newp->info.addr[3] = *(const in_addr_t *) address;
00198               }
00199              else
00200               memcpy (newp->info.addr, address, sizeof (newp->info.addr));
00201              newp->next = in6ailist;
00202              in6ailist = newp;
00203              ++in6ailistlen;
00204            }
00205          else if (nlmh->nlmsg_type == NLMSG_DONE)
00206            /* We found the end, leave the loop.  */
00207            done = true;
00208        }
00209     }
00210   while (! done);
00211 
00212   close_not_cancel_no_status (fd);
00213 
00214   if (*seen_ipv6 && in6ailist != NULL)
00215     {
00216       *in6ai = malloc (in6ailistlen * sizeof (**in6ai));
00217       if (*in6ai == NULL)
00218        goto out_fail;
00219 
00220       *in6ailen = in6ailistlen;
00221 
00222       do
00223        {
00224          (*in6ai)[--in6ailistlen] = in6ailist->info;
00225          in6ailist = in6ailist->next;
00226        }
00227       while (in6ailist != NULL);
00228     }
00229 
00230   if (use_malloc)
00231     free (buf);
00232   return 0;
00233 
00234 out_fail:
00235   if (use_malloc)
00236     free (buf);
00237   return -1;
00238 }
00239 
00240 
00241 /* We don't know if we have NETLINK support compiled in in our
00242    Kernel.  */
00243 #if __ASSUME_NETLINK_SUPPORT == 0
00244 /* Define in ifaddrs.h.  */
00245 extern int __no_netlink_support attribute_hidden;
00246 #else
00247 # define __no_netlink_support 0
00248 #endif
00249 
00250 
00251 void
00252 attribute_hidden
00253 __check_pf (bool *seen_ipv4, bool *seen_ipv6,
00254            struct in6addrinfo **in6ai, size_t *in6ailen)
00255 {
00256   *in6ai = NULL;
00257   *in6ailen = 0;
00258 
00259   if (! __no_netlink_support)
00260     {
00261       int fd = __socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
00262 
00263       struct sockaddr_nl nladdr;
00264       memset (&nladdr, '\0', sizeof (nladdr));
00265       nladdr.nl_family = AF_NETLINK;
00266 
00267       socklen_t addr_len = sizeof (nladdr);
00268 
00269       if (fd >= 0
00270          && __bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) == 0
00271          && __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) == 0
00272          && make_request (fd, nladdr.nl_pid, seen_ipv4, seen_ipv6,
00273                         in6ai, in6ailen) == 0)
00274        /* It worked.  */
00275        return;
00276 
00277       if (fd >= 0)
00278        __close (fd);
00279 
00280 #if __ASSUME_NETLINK_SUPPORT == 0
00281       /* Remember that there is no netlink support.  */
00282       __no_netlink_support = 1;
00283 #else
00284       /* We cannot determine what interfaces are available.  Be
00285         pessimistic.  */
00286       *seen_ipv4 = true;
00287       *seen_ipv6 = true;
00288 #endif
00289     }
00290 
00291 #if __ASSUME_NETLINK_SUPPORT == 0
00292   /* No netlink.  Get the interface list via getifaddrs.  */
00293   struct ifaddrs *ifa = NULL;
00294   if (getifaddrs (&ifa) != 0)
00295     {
00296       /* We cannot determine what interfaces are available.  Be
00297         pessimistic.  */
00298       *seen_ipv4 = true;
00299       *seen_ipv6 = true;
00300       return;
00301     }
00302 
00303   struct ifaddrs *runp;
00304   for (runp = ifa; runp != NULL; runp = runp->ifa_next)
00305     if (runp->ifa_addr->sa_family == PF_INET)
00306       *seen_ipv4 = true;
00307     else if (runp->ifa_addr->sa_family == PF_INET6)
00308       *seen_ipv6 = true;
00309 
00310   (void) freeifaddrs (ifa);
00311 #endif
00312 }