2 * Copyright 2008, The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "DHCP"
22 #include <netinet/in.h>
27 #include <sys/select.h>
28 #include <sys/socket.h>
30 #include <sys/types.h>
34 #include <cutils/properties.h>
37 #include <netutils/ifc.h>
43 static int verbose = 1;
44 static char errmsg[2048];
46 typedef unsigned long long msecs_t;
51 msecs_t get_msecs(void)
55 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
58 return (((msecs_t) ts.tv_sec) * ((msecs_t) 1000)) +
59 (((msecs_t) ts.tv_nsec) / ((msecs_t) 1000000));
63 void printerr(char *fmt, ...)
68 vsnprintf(errmsg, sizeof(errmsg), fmt, ap);
74 const char *dhcp_lasterror()
79 int fatal(const char *reason)
81 printerr("%s: %s\n", reason, strerror(errno));
86 const char *ipaddr(in_addr_t addr)
88 struct in_addr in_addr;
90 in_addr.s_addr = addr;
91 return inet_ntoa(in_addr);
94 extern int ipv4NetmaskToPrefixLength(in_addr_t mask);
96 typedef struct dhcp_info dhcp_info;
103 uint32_t prefixLength;
112 dhcp_info last_good_info;
114 void get_dhcp_info(uint32_t *ipaddr, uint32_t *gateway, uint32_t *prefixLength,
115 uint32_t *dns1, uint32_t *dns2, uint32_t *server,
118 *ipaddr = last_good_info.ipaddr;
119 *gateway = last_good_info.gateway;
120 *prefixLength = last_good_info.prefixLength;
121 *dns1 = last_good_info.dns1;
122 *dns2 = last_good_info.dns2;
123 *server = last_good_info.serveraddr;
124 *lease = last_good_info.lease;
127 static int dhcp_configure(const char *ifname, dhcp_info *info)
129 last_good_info = *info;
130 return ifc_configure(ifname, info->ipaddr, info->prefixLength, info->gateway,
131 info->dns1, info->dns2);
134 static const char *dhcp_type_to_name(uint32_t type)
137 case DHCPDISCOVER: return "discover";
138 case DHCPOFFER: return "offer";
139 case DHCPREQUEST: return "request";
140 case DHCPDECLINE: return "decline";
141 case DHCPACK: return "ack";
142 case DHCPNAK: return "nak";
143 case DHCPRELEASE: return "release";
144 case DHCPINFORM: return "inform";
145 default: return "???";
149 void dump_dhcp_info(dhcp_info *info)
151 char addr[20], gway[20];
152 ALOGD("--- dhcp %s (%d) ---",
153 dhcp_type_to_name(info->type), info->type);
154 strcpy(addr, ipaddr(info->ipaddr));
155 strcpy(gway, ipaddr(info->gateway));
156 ALOGD("ip %s gw %s prefixLength %d", addr, gway, info->prefixLength);
157 if (info->dns1) ALOGD("dns1: %s", ipaddr(info->dns1));
158 if (info->dns2) ALOGD("dns2: %s", ipaddr(info->dns2));
159 ALOGD("server %s, lease %d seconds",
160 ipaddr(info->serveraddr), info->lease);
164 int decode_dhcp_msg(dhcp_msg *msg, int len, dhcp_info *info)
170 memset(info, 0, sizeof(dhcp_info));
171 if (len < (DHCP_MSG_FIXED_SIZE + 4)) return -1;
173 len -= (DHCP_MSG_FIXED_SIZE + 4);
175 if (msg->options[0] != OPT_COOKIE1) return -1;
176 if (msg->options[1] != OPT_COOKIE2) return -1;
177 if (msg->options[2] != OPT_COOKIE3) return -1;
178 if (msg->options[3] != OPT_COOKIE4) return -1;
180 x = msg->options + 4;
184 if (opt == OPT_PAD) {
188 if (opt == OPT_END) {
197 case OPT_SUBNET_MASK:
201 info->prefixLength = ipv4NetmaskToPrefixLength(mask);
205 if (optlen >= 4) memcpy(&info->gateway, x, 4);
208 if (optlen >= 4) memcpy(&info->dns1, x + 0, 4);
209 if (optlen >= 8) memcpy(&info->dns2, x + 4, 4);
213 memcpy(&info->lease, x, 4);
214 info->lease = ntohl(info->lease);
218 if (optlen >= 4) memcpy(&info->serveraddr, x, 4);
220 case OPT_MESSAGE_TYPE:
230 info->ipaddr = msg->yiaddr;
237 static void hex2str(char *buf, size_t buf_size, const unsigned char *array, int len)
241 char *buf_end = buf + buf_size;
242 for (i = 0; i < len; i++) {
243 cp += snprintf(cp, buf_end - cp, " %02x ", array[i]);
247 void dump_dhcp_msg(dhcp_msg *msg, int len)
255 ALOGD("===== DHCP message:");
256 if (len < DHCP_MSG_FIXED_SIZE) {
257 ALOGD("Invalid length %d, should be %d", len, DHCP_MSG_FIXED_SIZE);
261 len -= DHCP_MSG_FIXED_SIZE;
263 if (msg->op == OP_BOOTREQUEST)
264 name = "BOOTREQUEST";
265 else if (msg->op == OP_BOOTREPLY)
269 ALOGD("op = %s (%d), htype = %d, hlen = %d, hops = %d",
270 name, msg->op, msg->htype, msg->hlen, msg->hops);
271 ALOGD("xid = 0x%08x secs = %d, flags = 0x%04x optlen = %d",
272 ntohl(msg->xid), ntohs(msg->secs), ntohs(msg->flags), len);
273 ALOGD("ciaddr = %s", ipaddr(msg->ciaddr));
274 ALOGD("yiaddr = %s", ipaddr(msg->yiaddr));
275 ALOGD("siaddr = %s", ipaddr(msg->siaddr));
276 ALOGD("giaddr = %s", ipaddr(msg->giaddr));
278 c = msg->hlen > 16 ? 16 : msg->hlen;
279 hex2str(buf, sizeof(buf), msg->chaddr, c);
280 ALOGD("chaddr = {%s}", buf);
282 for (n = 0; n < 64; n++) {
283 unsigned char x = msg->sname[n];
284 if ((x < ' ') || (x > 127)) {
291 for (n = 0; n < 128; n++) {
292 unsigned char x = msg->file[n];
293 if ((x < ' ') || (x > 127)) {
300 ALOGD("sname = '%s'", msg->sname);
301 ALOGD("file = '%s'", msg->file);
305 x = msg->options + 4;
318 if (optsz > len) break;
319 if (x[0] == OPT_DOMAIN_NAME || x[0] == OPT_MESSAGE) {
320 if ((unsigned int)optsz < sizeof(buf) - 1) {
325 memcpy(buf, &x[2], n);
328 hex2str(buf, sizeof(buf), &x[2], optsz);
330 if (x[0] == OPT_MESSAGE_TYPE)
331 name = dhcp_type_to_name(x[2]);
334 ALOGD("op %d len %d {%s} %s", x[0], optsz, buf, name == NULL ? "" : name);
342 static int send_message(int sock, int if_index, dhcp_msg *msg, int size)
345 dump_dhcp_msg(msg, size);
347 return send_packet(sock, if_index, msg, size, INADDR_ANY, INADDR_BROADCAST,
348 PORT_BOOTP_CLIENT, PORT_BOOTP_SERVER);
351 static int is_valid_reply(dhcp_msg *msg, dhcp_msg *reply, int sz)
353 if (sz < DHCP_MSG_FIXED_SIZE) {
354 if (verbose) ALOGD("Wrong size %d != %d\n", sz, DHCP_MSG_FIXED_SIZE);
357 if (reply->op != OP_BOOTREPLY) {
358 if (verbose) ALOGD("Wrong Op %d != %d\n", reply->op, OP_BOOTREPLY);
361 if (reply->xid != msg->xid) {
362 if (verbose) ALOGD("Wrong Xid 0x%x != 0x%x\n", ntohl(reply->xid),
366 if (reply->htype != msg->htype) {
367 if (verbose) ALOGD("Wrong Htype %d != %d\n", reply->htype, msg->htype);
370 if (reply->hlen != msg->hlen) {
371 if (verbose) ALOGD("Wrong Hlen %d != %d\n", reply->hlen, msg->hlen);
374 if (memcmp(msg->chaddr, reply->chaddr, msg->hlen)) {
375 if (verbose) ALOGD("Wrong chaddr %x != %x\n", *(reply->chaddr),*(msg->chaddr));
381 #define STATE_SELECTING 1
382 #define STATE_REQUESTING 2
384 #define TIMEOUT_INITIAL 4000
385 #define TIMEOUT_MAX 32000
387 int dhcp_init_ifc(const char *ifname)
389 dhcp_msg discover_msg;
390 dhcp_msg request_msg;
397 unsigned char hwaddr[6];
400 unsigned int timeout;
403 xid = (uint32_t) get_msecs();
405 if (ifc_get_hwaddr(ifname, hwaddr)) {
406 return fatal("cannot obtain interface address");
408 if (ifc_get_ifindex(ifname, &if_index)) {
409 return fatal("cannot obtain interface index");
412 s = open_raw_socket(ifname, hwaddr, if_index);
414 timeout = TIMEOUT_INITIAL;
415 state = STATE_SELECTING;
423 r = poll(&pfd, 1, timeout);
427 printerr("TIMEOUT\n");
429 if (timeout >= TIMEOUT_MAX) {
430 printerr("timed out\n");
431 if ( info.type == DHCPOFFER ) {
432 printerr("no acknowledgement from DHCP server\nconfiguring %s with offered parameters\n", ifname);
433 return dhcp_configure(ifname, &info);
439 timeout = timeout * 2;
445 case STATE_SELECTING:
447 size = init_dhcp_discover_msg(msg, hwaddr, xid);
449 case STATE_REQUESTING:
451 size = init_dhcp_request_msg(msg, hwaddr, xid, info.ipaddr, info.serveraddr);
457 r = send_message(s, if_index, msg, size);
459 printerr("error sending dhcp msg: %s\n", strerror(errno));
466 if ((errno == EAGAIN) || (errno == EINTR)) {
469 return fatal("poll failed");
473 r = receive_packet(s, &reply);
476 ALOGD("receive_packet failed (%d): %s", r, strerror(errno));
477 if (errno == ENETDOWN || errno == ENXIO) {
485 dump_dhcp_msg(&reply, r);
487 decode_dhcp_msg(&reply, r, &info);
489 if (state == STATE_SELECTING) {
490 valid_reply = is_valid_reply(&discover_msg, &reply, r);
492 valid_reply = is_valid_reply(&request_msg, &reply, r);
495 printerr("invalid reply\n");
499 if (verbose) dump_dhcp_info(&info);
502 case STATE_SELECTING:
503 if (info.type == DHCPOFFER) {
504 state = STATE_REQUESTING;
505 timeout = TIMEOUT_INITIAL;
510 case STATE_REQUESTING:
511 if (info.type == DHCPACK) {
512 printerr("configuring %s\n", ifname);
514 return dhcp_configure(ifname, &info);
515 } else if (info.type == DHCPNAK) {
516 printerr("configuration request denied\n");
520 printerr("ignoring %s message in state %d\n",
521 dhcp_type_to_name(info.type), state);
530 int do_dhcp(char *iname)
532 if (ifc_set_addr(iname, 0)) {
533 printerr("failed to set ip addr for %s to 0.0.0.0: %s\n", iname, strerror(errno));
538 printerr("failed to bring up interface %s: %s\n", iname, strerror(errno));
542 return dhcp_init_ifc(iname);