Home | History | Annotate | Download | only in libnetutils
      1 /*
      2  * Copyright 2008, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /* Utilities for managing the dhcpcd DHCP client daemon */
     18 
     19 #include <stdio.h>
     20 #include <stdlib.h>
     21 #include <string.h>
     22 #include <unistd.h>
     23 #include <arpa/inet.h>
     24 #include <netinet/in.h>
     25 
     26 #include <cutils/properties.h>
     27 
     28 static const char DAEMON_NAME[]        = "dhcpcd";
     29 static const char DAEMON_PROP_NAME[]   = "init.svc.dhcpcd";
     30 static const char HOSTNAME_PROP_NAME[] = "net.hostname";
     31 static const char DHCP_PROP_NAME_PREFIX[]  = "dhcp";
     32 static const char DHCP_CONFIG_PATH[]   = "/system/etc/dhcpcd/dhcpcd.conf";
     33 static const int NAP_TIME = 200;   /* wait for 200ms at a time */
     34                                   /* when polling for property values */
     35 static const char DAEMON_NAME_RENEW[]  = "iprenew";
     36 static char errmsg[100] = "\0";
     37 /* interface length for dhcpcd daemon start (dhcpcd_<interface> as defined in init.rc file)
     38  * or for filling up system properties dhcpcd.<interface>.ipaddress, dhcpcd.<interface>.dns1
     39  * and other properties on a successful bind
     40  */
     41 #define MAX_INTERFACE_LENGTH 25
     42 
     43 /*
     44  * P2p interface names increase sequentially p2p-p2p0-1, p2p-p2p0-2.. after
     45  * group formation. This does not work well with system properties which can quickly
     46  * exhaust or for specifiying a dhcp start target in init which requires
     47  * interface to be pre-defined in init.rc file.
     48  *
     49  * This function returns a common string p2p for all p2p interfaces.
     50  */
     51 void get_p2p_interface_replacement(const char *interface, char *p2p_interface) {
     52     /* Use p2p for any interface starting with p2p. */
     53     if (strncmp(interface, "p2p",3) == 0) {
     54         strncpy(p2p_interface, "p2p", MAX_INTERFACE_LENGTH);
     55     } else {
     56         strncpy(p2p_interface, interface, MAX_INTERFACE_LENGTH);
     57     }
     58 }
     59 
     60 /*
     61  * Wait for a system property to be assigned a specified value.
     62  * If desired_value is NULL, then just wait for the property to
     63  * be created with any value. maxwait is the maximum amount of
     64  * time in seconds to wait before giving up.
     65  */
     66 static int wait_for_property(const char *name, const char *desired_value, int maxwait)
     67 {
     68     char value[PROPERTY_VALUE_MAX] = {'\0'};
     69     int maxnaps = (maxwait * 1000) / NAP_TIME;
     70 
     71     if (maxnaps < 1) {
     72         maxnaps = 1;
     73     }
     74 
     75     while (maxnaps-- >= 0) {
     76         if (property_get(name, value, NULL)) {
     77             if (desired_value == NULL ||
     78                     strcmp(value, desired_value) == 0) {
     79                 return 0;
     80             }
     81         }
     82         if (maxnaps >= 0) {
     83             usleep(NAP_TIME * 1000);
     84         }
     85     }
     86     return -1; /* failure */
     87 }
     88 
     89 static int fill_ip_info(const char *interface,
     90                      char *ipaddr,
     91                      char *gateway,
     92                      uint32_t *prefixLength,
     93                      char *dns[],
     94                      char *server,
     95                      uint32_t *lease,
     96                      char *vendorInfo,
     97                      char *domain,
     98                      char *mtu)
     99 {
    100     char prop_name[PROPERTY_KEY_MAX];
    101     char prop_value[PROPERTY_VALUE_MAX];
    102     /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
    103     char p2p_interface[MAX_INTERFACE_LENGTH];
    104     int x;
    105 
    106     get_p2p_interface_replacement(interface, p2p_interface);
    107 
    108     snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, p2p_interface);
    109     property_get(prop_name, ipaddr, NULL);
    110 
    111     snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, p2p_interface);
    112     property_get(prop_name, gateway, NULL);
    113 
    114     snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, p2p_interface);
    115     property_get(prop_name, server, NULL);
    116 
    117     //TODO: Handle IPv6 when we change system property usage
    118     if (gateway[0] == '\0' || strncmp(gateway, "0.0.0.0", 7) == 0) {
    119         //DHCP server is our best bet as gateway
    120         strncpy(gateway, server, PROPERTY_VALUE_MAX);
    121     }
    122 
    123     snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, p2p_interface);
    124     if (property_get(prop_name, prop_value, NULL)) {
    125         int p;
    126         // this conversion is v4 only, but this dhcp client is v4 only anyway
    127         in_addr_t mask = ntohl(inet_addr(prop_value));
    128         // Check netmask is a valid IP address.  ntohl gives NONE response (all 1's) for
    129         // non 255.255.255.255 inputs.  if we get that value check if it is legit..
    130         if (mask == INADDR_NONE && strcmp(prop_value, "255.255.255.255") != 0) {
    131             snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
    132             return -1;
    133         }
    134         for (p = 0; p < 32; p++) {
    135             if (mask == 0) break;
    136             // check for non-contiguous netmask, e.g., 255.254.255.0
    137             if ((mask & 0x80000000) == 0) {
    138                 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
    139                 return -1;
    140             }
    141             mask = mask << 1;
    142         }
    143         *prefixLength = p;
    144     }
    145 
    146     for (x=0; dns[x] != NULL; x++) {
    147         snprintf(prop_name, sizeof(prop_name), "%s.%s.dns%d", DHCP_PROP_NAME_PREFIX, p2p_interface, x+1);
    148         property_get(prop_name, dns[x], NULL);
    149     }
    150 
    151     snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, p2p_interface);
    152     if (property_get(prop_name, prop_value, NULL)) {
    153         *lease = atol(prop_value);
    154     }
    155 
    156     snprintf(prop_name, sizeof(prop_name), "%s.%s.vendorInfo", DHCP_PROP_NAME_PREFIX,
    157             p2p_interface);
    158     property_get(prop_name, vendorInfo, NULL);
    159 
    160     snprintf(prop_name, sizeof(prop_name), "%s.%s.domain", DHCP_PROP_NAME_PREFIX,
    161             p2p_interface);
    162     property_get(prop_name, domain, NULL);
    163 
    164     snprintf(prop_name, sizeof(prop_name), "%s.%s.mtu", DHCP_PROP_NAME_PREFIX,
    165             p2p_interface);
    166     property_get(prop_name, mtu, NULL);
    167 
    168     return 0;
    169 }
    170 
    171 /*
    172  * Get any available DHCP results.
    173  */
    174 int dhcp_get_results(const char *interface,
    175                      char *ipaddr,
    176                      char *gateway,
    177                      uint32_t *prefixLength,
    178                      char *dns[],
    179                      char *server,
    180                      uint32_t *lease,
    181                      char *vendorInfo,
    182                      char *domain,
    183                      char *mtu)
    184 {
    185     char result_prop_name[PROPERTY_KEY_MAX];
    186     char prop_value[PROPERTY_VALUE_MAX];
    187 
    188     /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
    189     char p2p_interface[MAX_INTERFACE_LENGTH];
    190     get_p2p_interface_replacement(interface, p2p_interface);
    191     snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
    192             DHCP_PROP_NAME_PREFIX,
    193             p2p_interface);
    194 
    195     memset(prop_value, '\0', PROPERTY_VALUE_MAX);
    196     if (!property_get(result_prop_name, prop_value, NULL)) {
    197         snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
    198         return -1;
    199     }
    200     if (strcmp(prop_value, "ok") == 0) {
    201         if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
    202                 server, lease, vendorInfo, domain, mtu) == -1) {
    203             return -1;
    204         }
    205         return 0;
    206     } else {
    207         snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
    208         return -1;
    209     }
    210 }
    211 
    212 /*
    213  * Start the dhcp client daemon, and wait for it to finish
    214  * configuring the interface.
    215  *
    216  * The device init.rc file needs a corresponding entry for this work.
    217  *
    218  * Example:
    219  * service dhcpcd_<interface> /system/bin/dhcpcd -ABKL -f dhcpcd.conf
    220  */
    221 int dhcp_start(const char *interface)
    222 {
    223     char result_prop_name[PROPERTY_KEY_MAX];
    224     char daemon_prop_name[PROPERTY_KEY_MAX];
    225     char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
    226     char daemon_cmd[PROPERTY_VALUE_MAX * 2 + sizeof(DHCP_CONFIG_PATH)];
    227     const char *ctrl_prop = "ctl.start";
    228     const char *desired_status = "running";
    229     /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
    230     char p2p_interface[MAX_INTERFACE_LENGTH];
    231 
    232     get_p2p_interface_replacement(interface, p2p_interface);
    233 
    234     snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
    235             DHCP_PROP_NAME_PREFIX,
    236             p2p_interface);
    237 
    238     snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
    239             DAEMON_PROP_NAME,
    240             p2p_interface);
    241 
    242     /* Erase any previous setting of the dhcp result property */
    243     property_set(result_prop_name, "");
    244 
    245     /* Start the daemon and wait until it's ready */
    246     if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0'))
    247         snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-f %s -h %s %s", DAEMON_NAME,
    248                  p2p_interface, DHCP_CONFIG_PATH, prop_value, interface);
    249     else
    250         snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-f %s %s", DAEMON_NAME,
    251                  p2p_interface, DHCP_CONFIG_PATH, interface);
    252     memset(prop_value, '\0', PROPERTY_VALUE_MAX);
    253     property_set(ctrl_prop, daemon_cmd);
    254     if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) {
    255         snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start");
    256         return -1;
    257     }
    258 
    259     /* Wait for the daemon to return a result */
    260     if (wait_for_property(result_prop_name, NULL, 30) < 0) {
    261         snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish");
    262         return -1;
    263     }
    264 
    265     return 0;
    266 }
    267 
    268 /**
    269  * Stop the DHCP client daemon.
    270  */
    271 int dhcp_stop(const char *interface)
    272 {
    273     char result_prop_name[PROPERTY_KEY_MAX];
    274     char daemon_prop_name[PROPERTY_KEY_MAX];
    275     char daemon_cmd[PROPERTY_VALUE_MAX * 2];
    276     const char *ctrl_prop = "ctl.stop";
    277     const char *desired_status = "stopped";
    278 
    279     char p2p_interface[MAX_INTERFACE_LENGTH];
    280 
    281     get_p2p_interface_replacement(interface, p2p_interface);
    282 
    283     snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
    284             DHCP_PROP_NAME_PREFIX,
    285             p2p_interface);
    286 
    287     snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
    288             DAEMON_PROP_NAME,
    289             p2p_interface);
    290 
    291     snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, p2p_interface);
    292 
    293     /* Stop the daemon and wait until it's reported to be stopped */
    294     property_set(ctrl_prop, daemon_cmd);
    295     if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
    296         return -1;
    297     }
    298     property_set(result_prop_name, "failed");
    299     return 0;
    300 }
    301 
    302 /**
    303  * Release the current DHCP client lease.
    304  */
    305 int dhcp_release_lease(const char *interface)
    306 {
    307     char daemon_prop_name[PROPERTY_KEY_MAX];
    308     char daemon_cmd[PROPERTY_VALUE_MAX * 2];
    309     const char *ctrl_prop = "ctl.stop";
    310     const char *desired_status = "stopped";
    311 
    312     char p2p_interface[MAX_INTERFACE_LENGTH];
    313 
    314     get_p2p_interface_replacement(interface, p2p_interface);
    315 
    316     snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
    317             DAEMON_PROP_NAME,
    318             p2p_interface);
    319 
    320     snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, p2p_interface);
    321 
    322     /* Stop the daemon and wait until it's reported to be stopped */
    323     property_set(ctrl_prop, daemon_cmd);
    324     if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
    325         return -1;
    326     }
    327     return 0;
    328 }
    329 
    330 char *dhcp_get_errmsg() {
    331     return errmsg;
    332 }
    333 
    334 /**
    335  * The device init.rc file needs a corresponding entry.
    336  *
    337  * Example:
    338  * service iprenew_<interface> /system/bin/dhcpcd -n
    339  *
    340  */
    341 int dhcp_start_renew(const char *interface)
    342 {
    343     char result_prop_name[PROPERTY_KEY_MAX];
    344     char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
    345     char daemon_cmd[PROPERTY_VALUE_MAX * 2];
    346     const char *ctrl_prop = "ctl.start";
    347 
    348     char p2p_interface[MAX_INTERFACE_LENGTH];
    349 
    350     get_p2p_interface_replacement(interface, p2p_interface);
    351 
    352     snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
    353             DHCP_PROP_NAME_PREFIX,
    354             p2p_interface);
    355 
    356     /* Erase any previous setting of the dhcp result property */
    357     property_set(result_prop_name, "");
    358 
    359     /* Start the renew daemon and wait until it's ready */
    360     snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME_RENEW,
    361             p2p_interface, interface);
    362     memset(prop_value, '\0', PROPERTY_VALUE_MAX);
    363     property_set(ctrl_prop, daemon_cmd);
    364 
    365     /* Wait for the daemon to return a result */
    366     if (wait_for_property(result_prop_name, NULL, 30) < 0) {
    367         snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP Renew to finish");
    368         return -1;
    369     }
    370 
    371     return 0;
    372 }
    373