dhcp_utils.c revision faab26d542740f03cbe12e44f6af1f97e8e7c12e
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
28static const char DAEMON_NAME[]        = "dhcpcd";
29static const char DAEMON_PROP_NAME[]   = "init.svc.dhcpcd";
30static const char HOSTNAME_PROP_NAME[] = "net.hostname";
31static const char DHCP_PROP_NAME_PREFIX[]  = "dhcp";
32static const int NAP_TIME = 200;   /* wait for 200ms at a time */
33                                  /* when polling for property values */
34static char errmsg[100];
35
36/*
37 * Wait for a system property to be assigned a specified value.
38 * If desired_value is NULL, then just wait for the property to
39 * be created with any value. maxwait is the maximum amount of
40 * time in seconds to wait before giving up.
41 */
42static int wait_for_property(const char *name, const char *desired_value, int maxwait)
43{
44    char value[PROPERTY_VALUE_MAX] = {'\0'};
45    int maxnaps = (maxwait * 1000) / NAP_TIME;
46
47    if (maxnaps < 1) {
48        maxnaps = 1;
49    }
50
51    while (maxnaps-- > 0) {
52        usleep(NAP_TIME * 1000);
53        if (property_get(name, value, NULL)) {
54            if (desired_value == NULL ||
55                    strcmp(value, desired_value) == 0) {
56                return 0;
57            }
58        }
59    }
60    return -1; /* failure */
61}
62
63static int fill_ip_info(const char *interface,
64                     char *ipaddr,
65                     char *gateway,
66                     uint32_t *prefixLength,
67                     char *dns1,
68                     char *dns2,
69                     char *server,
70                     uint32_t  *lease)
71{
72    char prop_name[PROPERTY_KEY_MAX];
73    char prop_value[PROPERTY_VALUE_MAX];
74
75    snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, interface);
76    property_get(prop_name, ipaddr, NULL);
77
78    snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, interface);
79    property_get(prop_name, gateway, NULL);
80
81    snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, interface);
82    if (property_get(prop_name, prop_value, NULL)) {
83        int p;
84        // this conversion is v4 only, but this dhcp client is v4 only anyway
85        in_addr_t mask = ntohl(inet_addr(prop_value));
86        // Check netmask is a valid IP address.  ntohl gives NONE response (all 1's) for
87        // non 255.255.255.255 inputs.  if we get that value check if it is legit..
88        if (mask == INADDR_NONE && strcmp(prop_value, "255.255.255.255") != 0) {
89            snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
90            return -1;
91        }
92        for (p = 0; p < 32; p++) {
93            if (mask == 0) break;
94            // check for non-contiguous netmask, e.g., 255.254.255.0
95            if ((mask & 0x80000000) == 0) {
96                snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
97                return -1;
98            }
99            mask = mask << 1;
100        }
101        *prefixLength = p;
102    }
103    snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, interface);
104    property_get(prop_name, dns1, NULL);
105
106    snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, interface);
107    property_get(prop_name, dns2, NULL);
108
109    snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, interface);
110    property_get(prop_name, server, NULL);
111
112    snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, interface);
113    if (property_get(prop_name, prop_value, NULL)) {
114        *lease = atol(prop_value);
115    }
116    return 0;
117}
118
119static const char *ipaddr_to_string(in_addr_t addr)
120{
121    struct in_addr in_addr;
122
123    in_addr.s_addr = addr;
124    return inet_ntoa(in_addr);
125}
126
127/*
128 * Start the dhcp client daemon, and wait for it to finish
129 * configuring the interface.
130 */
131int dhcp_do_request(const char *interface,
132                    char *ipaddr,
133                    char *gateway,
134                    uint32_t *prefixLength,
135                    char *dns1,
136                    char *dns2,
137                    char *server,
138                    uint32_t  *lease)
139{
140    char result_prop_name[PROPERTY_KEY_MAX];
141    char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
142    char daemon_cmd[PROPERTY_VALUE_MAX * 2];
143    const char *ctrl_prop = "ctl.start";
144    const char *desired_status = "running";
145
146    snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
147            DHCP_PROP_NAME_PREFIX,
148            interface);
149    /* Erase any previous setting of the dhcp result property */
150    property_set(result_prop_name, "");
151
152    /* Start the daemon and wait until it's ready */
153    if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0'))
154        snprintf(daemon_cmd, sizeof(daemon_cmd), "%s:-h %s %s", DAEMON_NAME,
155                 prop_value, interface);
156    else
157        snprintf(daemon_cmd, sizeof(daemon_cmd), "%s:%s", DAEMON_NAME, interface);
158    memset(prop_value, '\0', PROPERTY_VALUE_MAX);
159    property_set(ctrl_prop, daemon_cmd);
160    if (wait_for_property(DAEMON_PROP_NAME, desired_status, 10) < 0) {
161        snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start");
162        return -1;
163    }
164
165    /* Wait for the daemon to return a result */
166    if (wait_for_property(result_prop_name, NULL, 30) < 0) {
167        snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish");
168        return -1;
169    }
170
171    if (!property_get(result_prop_name, prop_value, NULL)) {
172        /* shouldn't ever happen, given the success of wait_for_property() */
173        snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
174        return -1;
175    }
176    if (strcmp(prop_value, "ok") == 0) {
177        char dns_prop_name[PROPERTY_KEY_MAX];
178        if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns1, dns2, server, lease)
179                == -1) {
180            return -1;
181        }
182
183        /* copy dns data to system properties - TODO - remove this after we have async
184         * notification of renewal's */
185        snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface);
186        property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : "");
187        snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface);
188        property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : "");
189        return 0;
190    } else {
191        snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
192        return -1;
193    }
194}
195
196/**
197 * Stop the DHCP client daemon.
198 */
199int dhcp_stop(const char *interface)
200{
201    char result_prop_name[PROPERTY_KEY_MAX];
202    const char *ctrl_prop = "ctl.stop";
203    const char *desired_status = "stopped";
204
205    snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
206            DHCP_PROP_NAME_PREFIX,
207            interface);
208    /* Stop the daemon and wait until it's reported to be stopped */
209    property_set(ctrl_prop, DAEMON_NAME);
210    if (wait_for_property(DAEMON_PROP_NAME, desired_status, 5) < 0) {
211        return -1;
212    }
213    property_set(result_prop_name, "failed");
214    return 0;
215}
216
217/**
218 * Release the current DHCP client lease.
219 */
220int dhcp_release_lease(const char *interface)
221{
222    const char *ctrl_prop = "ctl.stop";
223    const char *desired_status = "stopped";
224
225    /* Stop the daemon and wait until it's reported to be stopped */
226    property_set(ctrl_prop, DAEMON_NAME);
227    if (wait_for_property(DAEMON_PROP_NAME, desired_status, 5) < 0) {
228        return -1;
229    }
230    return 0;
231}
232
233char *dhcp_get_errmsg() {
234    return errmsg;
235}
236