root/opal/mca/if/linux_ipv6/if_linux_ipv6.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. if_linux_ipv6_open

   1 /*
   2  * Copyright (c) 2010      Cisco Systems, Inc.  All rights reserved.
   3  * Copyright (c) 2010      Oracle and/or its affiliates.  All rights reserved.
   4  * Copyright (c) 2018      Amazon.com, Inc. or its affiliates.  All Rights
   5  *                         reserved.
   6  * $COPYRIGHT$
   7  *
   8  * Additional copyrights may follow
   9  *
  10  * $HEADER$
  11  */
  12 
  13 #include "opal_config.h"
  14 
  15 #include <string.h>
  16 #ifdef HAVE_UNISTD_H
  17 #include <unistd.h>
  18 #endif
  19 #include <errno.h>
  20 #ifdef HAVE_SYS_TYPES_H
  21 #include <sys/types.h>
  22 #endif
  23 #ifdef HAVE_SYS_SOCKET_H
  24 #include <sys/socket.h>
  25 #endif
  26 #ifdef HAVE_SYS_SOCKIO_H
  27 #include <sys/sockio.h>
  28 #endif
  29 #ifdef HAVE_SYS_IOCTL_H
  30 #include <sys/ioctl.h>
  31 #endif
  32 #ifdef HAVE_NETINET_IN_H
  33 #include <netinet/in.h>
  34 #endif
  35 #ifdef HAVE_ARPA_INET_H
  36 #include <arpa/inet.h>
  37 #endif
  38 #ifdef HAVE_NET_IF_H
  39 #include <net/if.h>
  40 #endif
  41 #ifdef HAVE_NETDB_H
  42 #include <netdb.h>
  43 #endif
  44 #ifdef HAVE_IFADDRS_H
  45 #include <ifaddrs.h>
  46 #endif
  47 
  48 #include "opal/constants.h"
  49 #include "opal/util/if.h"
  50 #include "opal/util/output.h"
  51 #include "opal/util/string_copy.h"
  52 #include "opal/mca/if/if.h"
  53 #include "opal/mca/if/base/base.h"
  54 
  55 static int if_linux_ipv6_open(void);
  56 
  57 /* Discovers Linux IPv6 interfaces */
  58 opal_if_base_component_t mca_if_linux_ipv6_component = {
  59     /* First, the mca_component_t struct containing meta information
  60        about the component itself */
  61     {
  62         OPAL_IF_BASE_VERSION_2_0_0,
  63 
  64         /* Component name and version */
  65         "linux_ipv6",
  66         OPAL_MAJOR_VERSION,
  67         OPAL_MINOR_VERSION,
  68         OPAL_RELEASE_VERSION,
  69 
  70         /* Component open and close functions */
  71         if_linux_ipv6_open,
  72         NULL
  73     },
  74     {
  75         /* This component is checkpointable */
  76         MCA_BASE_METADATA_PARAM_CHECKPOINT
  77     },
  78 };
  79 
  80 /* configure using getifaddrs(3) */
  81 static int if_linux_ipv6_open(void)
  82 {
  83 #if OPAL_ENABLE_IPV6
  84     FILE *f;
  85     if ((f = fopen("/proc/net/if_inet6", "r"))) {
  86         char ifname[IF_NAMESIZE];
  87         unsigned int idx, pfxlen, scope, dadstat;
  88         struct in6_addr a6;
  89         int iter;
  90         uint32_t flag;
  91         unsigned int addrbyte[16];
  92 
  93         while (fscanf(f, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x %x %x %x %x %20s\n",
  94                       &addrbyte[0], &addrbyte[1], &addrbyte[2], &addrbyte[3],
  95                       &addrbyte[4], &addrbyte[5], &addrbyte[6], &addrbyte[7],
  96                       &addrbyte[8], &addrbyte[9], &addrbyte[10], &addrbyte[11],
  97                       &addrbyte[12], &addrbyte[13], &addrbyte[14], &addrbyte[15],
  98                       &idx, &pfxlen, &scope, &dadstat, ifname) != EOF) {
  99             opal_if_t *intf;
 100 
 101             opal_output_verbose(1, opal_if_base_framework.framework_output,
 102                                 "found interface %2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x scope %x\n",
 103                                 addrbyte[0], addrbyte[1], addrbyte[2], addrbyte[3],
 104                                 addrbyte[4], addrbyte[5], addrbyte[6], addrbyte[7],
 105                                 addrbyte[8], addrbyte[9], addrbyte[10], addrbyte[11],
 106                                 addrbyte[12], addrbyte[13], addrbyte[14], addrbyte[15], scope);
 107 
 108             /* Only interested in global (0x00) scope */
 109             if (scope != 0x00)  {
 110                 opal_output_verbose(1, opal_if_base_framework.framework_output,
 111                                     "skipping interface %2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x scope %x\n",
 112                                     addrbyte[0], addrbyte[1], addrbyte[2], addrbyte[3],
 113                                     addrbyte[4], addrbyte[5], addrbyte[6], addrbyte[7],
 114                                     addrbyte[8], addrbyte[9], addrbyte[10], addrbyte[11],
 115                                     addrbyte[12], addrbyte[13], addrbyte[14], addrbyte[15], scope);
 116                 continue;
 117             }
 118 
 119             intf = OBJ_NEW(opal_if_t);
 120             if (NULL == intf) {
 121                 opal_output(0, "opal_ifinit: unable to allocate %lu bytes\n",
 122                             (unsigned long)sizeof(opal_if_t));
 123                 fclose(f);
 124                 return OPAL_ERR_OUT_OF_RESOURCE;
 125             }
 126             intf->af_family = AF_INET6;
 127 
 128             for (iter = 0; iter < 16; iter++) {
 129                 a6.s6_addr[iter] = addrbyte[iter];
 130             }
 131 
 132             /* now construct the opal_if_t */
 133             opal_string_copy(intf->if_name, ifname, IF_NAMESIZE);
 134             intf->if_index = opal_list_get_size(&opal_if_list)+1;
 135             intf->if_kernel_index = (uint16_t) idx;
 136             ((struct sockaddr_in6*) &intf->if_addr)->sin6_addr = a6;
 137             ((struct sockaddr_in6*) &intf->if_addr)->sin6_family = AF_INET6;
 138             ((struct sockaddr_in6*) &intf->if_addr)->sin6_scope_id = scope;
 139             intf->if_mask = pfxlen;
 140             if (OPAL_SUCCESS == opal_ifindextoflags(opal_ifnametoindex (ifname), &flag)) {
 141                 intf->if_flags = flag;
 142             } else {
 143                 intf->if_flags = IFF_UP;
 144             }
 145 
 146             /* copy new interface information to heap and append
 147                to list */
 148             opal_list_append(&opal_if_list, &(intf->super));
 149             opal_output_verbose(1, opal_if_base_framework.framework_output,
 150                                 "added interface %2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x\n",
 151                                 addrbyte[0], addrbyte[1], addrbyte[2], addrbyte[3],
 152                                 addrbyte[4], addrbyte[5], addrbyte[6], addrbyte[7],
 153                                 addrbyte[8], addrbyte[9], addrbyte[10], addrbyte[11],
 154                                 addrbyte[12], addrbyte[13], addrbyte[14], addrbyte[15]);
 155         } /* of while */
 156         fclose(f);
 157     }
 158 #endif  /* OPAL_ENABLE_IPV6 */
 159 
 160     return OPAL_SUCCESS;
 161 }
 162 
 163 

/* [<][>][^][v][top][bottom][index][help] */