dhcp_utils.c revision da96a66b88520d8414f30d0eab9eaf1630d374f7
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 const char DAEMON_NAME_RENEW[]  = "iprenew";
35static char errmsg[100];
36/* interface suffix on dhcpcd */
37#define MAX_DAEMON_SUFFIX 25
38
39/*
40 * Wait for a system property to be assigned a specified value.
41 * If desired_value is NULL, then just wait for the property to
42 * be created with any value. maxwait is the maximum amount of
43 * time in seconds to wait before giving up.
44 */
45static int wait_for_property(const char *name, const char *desired_value, int maxwait)
46{
47    char value[PROPERTY_VALUE_MAX] = {'\0'};
48    int maxnaps = (maxwait * 1000) / NAP_TIME;
49
50    if (maxnaps < 1) {
51        maxnaps = 1;
52    }
53
54    while (maxnaps-- > 0) {
55        usleep(NAP_TIME * 1000);
56        if (property_get(name, value, NULL)) {
57            if (desired_value == NULL ||
58                    strcmp(value, desired_value) == 0) {
59                return 0;
60            }
61        }
62    }
63    return -1; /* failure */
64}
65
66static int fill_ip_info(const char *interface,
67                     char *ipaddr,
68                     char *gateway,
69                     uint32_t *prefixLength,
70                     char *dns1,
71                     char *dns2,
72                     char *server,
73                     uint32_t  *lease)
74{
75    char prop_name[PROPERTY_KEY_MAX];
76    char prop_value[PROPERTY_VALUE_MAX];
77
78    snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, interface);
79    property_get(prop_name, ipaddr, NULL);
80
81    snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, interface);
82    property_get(prop_name, gateway, NULL);
83
84    snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, interface);
85    property_get(prop_name, server, NULL);
86
87    //TODO: Handle IPv6 when we change system property usage
88    if (strcmp(gateway, "0.0.0.0") == 0) {
89        //DHCP server is our best bet as gateway
90        strncpy(gateway, server, PROPERTY_VALUE_MAX);
91    }
92
93    snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, interface);
94    if (property_get(prop_name, prop_value, NULL)) {
95        int p;
96        // this conversion is v4 only, but this dhcp client is v4 only anyway
97        in_addr_t mask = ntohl(inet_addr(prop_value));
98        // Check netmask is a valid IP address.  ntohl gives NONE response (all 1's) for
99        // non 255.255.255.255 inputs.  if we get that value check if it is legit..
100        if (mask == INADDR_NONE && strcmp(prop_value, "255.255.255.255") != 0) {
101            snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
102            return -1;
103        }
104        for (p = 0; p < 32; p++) {
105            if (mask == 0) break;
106            // check for non-contiguous netmask, e.g., 255.254.255.0
107            if ((mask & 0x80000000) == 0) {
108                snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
109                return -1;
110            }
111            mask = mask << 1;
112        }
113        *prefixLength = p;
114    }
115    snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, interface);
116    property_get(prop_name, dns1, NULL);
117
118    snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, interface);
119    property_get(prop_name, dns2, NULL);
120
121    snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, interface);
122    if (property_get(prop_name, prop_value, NULL)) {
123        *lease = atol(prop_value);
124    }
125    return 0;
126}
127
128static const char *ipaddr_to_string(in_addr_t addr)
129{
130    struct in_addr in_addr;
131
132    in_addr.s_addr = addr;
133    return inet_ntoa(in_addr);
134}
135
136void get_daemon_suffix(const char *interface, char *daemon_suffix) {
137    /* Use p2p suffix for any p2p interface. */
138    if (strncmp(interface, "p2p",3) == 0) {
139        sprintf(daemon_suffix, "p2p");
140    } else {
141        snprintf(daemon_suffix, MAX_DAEMON_SUFFIX, "%s", interface);
142    }
143}
144
145/*
146 * Start the dhcp client daemon, and wait for it to finish
147 * configuring the interface.
148 *
149 * The device init.rc file needs a corresponding entry for this work.
150 *
151 * Example:
152 * service dhcpcd_<interface> /system/bin/dhcpcd -ABKL
153 */
154int dhcp_do_request(const char *interface,
155                    char *ipaddr,
156                    char *gateway,
157                    uint32_t *prefixLength,
158                    char *dns1,
159                    char *dns2,
160                    char *server,
161                    uint32_t  *lease)
162{
163    char result_prop_name[PROPERTY_KEY_MAX];
164    char daemon_prop_name[PROPERTY_KEY_MAX];
165    char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
166    char daemon_cmd[PROPERTY_VALUE_MAX * 2];
167    const char *ctrl_prop = "ctl.start";
168    const char *desired_status = "running";
169    char daemon_suffix[MAX_DAEMON_SUFFIX];
170
171    get_daemon_suffix(interface, daemon_suffix);
172
173    snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
174            DHCP_PROP_NAME_PREFIX,
175            interface);
176
177    snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
178            DAEMON_PROP_NAME,
179            daemon_suffix);
180
181    /* Erase any previous setting of the dhcp result property */
182    property_set(result_prop_name, "");
183
184    /* Start the daemon and wait until it's ready */
185    if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0'))
186        snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-h %s %s", DAEMON_NAME, daemon_suffix,
187                 prop_value, interface);
188    else
189        snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME, daemon_suffix, interface);
190    memset(prop_value, '\0', PROPERTY_VALUE_MAX);
191    property_set(ctrl_prop, daemon_cmd);
192    if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) {
193        snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start");
194        return -1;
195    }
196
197    /* Wait for the daemon to return a result */
198    if (wait_for_property(result_prop_name, NULL, 30) < 0) {
199        snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish");
200        return -1;
201    }
202
203    if (!property_get(result_prop_name, prop_value, NULL)) {
204        /* shouldn't ever happen, given the success of wait_for_property() */
205        snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
206        return -1;
207    }
208    if (strcmp(prop_value, "ok") == 0) {
209        char dns_prop_name[PROPERTY_KEY_MAX];
210        if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns1, dns2, server, lease)
211                == -1) {
212            return -1;
213        }
214
215        /* copy dns data to system properties - TODO - remove this after we have async
216         * notification of renewal's */
217        snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface);
218        property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : "");
219        snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface);
220        property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : "");
221        return 0;
222    } else {
223        snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
224        return -1;
225    }
226}
227
228/**
229 * Stop the DHCP client daemon.
230 */
231int dhcp_stop(const char *interface)
232{
233    char result_prop_name[PROPERTY_KEY_MAX];
234    char daemon_prop_name[PROPERTY_KEY_MAX];
235    char daemon_cmd[PROPERTY_VALUE_MAX * 2];
236    const char *ctrl_prop = "ctl.stop";
237    const char *desired_status = "stopped";
238
239    char daemon_suffix[MAX_DAEMON_SUFFIX];
240
241    get_daemon_suffix(interface, daemon_suffix);
242
243    snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
244            DHCP_PROP_NAME_PREFIX,
245            interface);
246
247    snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
248            DAEMON_PROP_NAME,
249            daemon_suffix);
250
251    snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, daemon_suffix);
252
253    /* Stop the daemon and wait until it's reported to be stopped */
254    property_set(ctrl_prop, daemon_cmd);
255    if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
256        return -1;
257    }
258    property_set(result_prop_name, "failed");
259    return 0;
260}
261
262/**
263 * Release the current DHCP client lease.
264 */
265int dhcp_release_lease(const char *interface)
266{
267    char daemon_prop_name[PROPERTY_KEY_MAX];
268    char daemon_cmd[PROPERTY_VALUE_MAX * 2];
269    const char *ctrl_prop = "ctl.stop";
270    const char *desired_status = "stopped";
271
272    char daemon_suffix[MAX_DAEMON_SUFFIX];
273
274    get_daemon_suffix(interface, daemon_suffix);
275
276    snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
277            DAEMON_PROP_NAME,
278            daemon_suffix);
279
280    snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, daemon_suffix);
281
282    /* Stop the daemon and wait until it's reported to be stopped */
283    property_set(ctrl_prop, daemon_cmd);
284    if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
285        return -1;
286    }
287    return 0;
288}
289
290char *dhcp_get_errmsg() {
291    return errmsg;
292}
293
294/**
295 * The device init.rc file needs a corresponding entry.
296 *
297 * Example:
298 * service iprenew_<interface> /system/bin/dhcpcd -n
299 *
300 */
301int dhcp_do_request_renew(const char *interface,
302                    in_addr_t *ipaddr,
303                    in_addr_t *gateway,
304                    uint32_t *prefixLength,
305                    in_addr_t *dns1,
306                    in_addr_t *dns2,
307                    in_addr_t *server,
308                    uint32_t  *lease)
309{
310    char result_prop_name[PROPERTY_KEY_MAX];
311    char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
312    char daemon_cmd[PROPERTY_VALUE_MAX * 2];
313    const char *ctrl_prop = "ctl.start";
314
315    char daemon_suffix[MAX_DAEMON_SUFFIX];
316
317    get_daemon_suffix(interface, daemon_suffix);
318
319    snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
320            DHCP_PROP_NAME_PREFIX,
321            interface);
322
323    /* Erase any previous setting of the dhcp result property */
324    property_set(result_prop_name, "");
325
326    /* Start the renew daemon and wait until it's ready */
327    snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME_RENEW,
328            daemon_suffix, interface);
329    memset(prop_value, '\0', PROPERTY_VALUE_MAX);
330    property_set(ctrl_prop, daemon_cmd);
331
332    /* Wait for the daemon to return a result */
333    if (wait_for_property(result_prop_name, NULL, 30) < 0) {
334        snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP Renew to finish");
335        return -1;
336    }
337
338    if (!property_get(result_prop_name, prop_value, NULL)) {
339        /* shouldn't ever happen, given the success of wait_for_property() */
340        snprintf(errmsg, sizeof(errmsg), "%s", "DHCP Renew result property was not set");
341        return -1;
342    }
343    if (strcmp(prop_value, "ok") == 0) {
344        fill_ip_info(interface, ipaddr, gateway, prefixLength, dns1, dns2, server, lease);
345        return 0;
346    } else {
347        snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value);
348        return -1;
349    }
350}
351