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