/* Minimal DHCP client: see dhcp.h. */ #include "io.h" #include "net.h" #include "netdev.h" #include "timer.h" #include "dhcp.h" #define DHCP_SERVER_PORT 67 #define DHCP_CLIENT_PORT 68 #define DHCP_OP_REQUEST 1 #define DHCP_OP_REPLY 2 #define DHCP_DISCOVER 1 #define DHCP_OFFER 2 #define DHCP_REQUEST 3 #define DHCP_ACK 5 #define DHCP_NAK 6 /* BOOTP header (236 bytes) + cookie + options, padded to a 300-byte DHCP * message (RFC 2131 minimum). */ #define DHCP_MSG_LEN 300 #define DHCP_HDR_LEN 236 #define DHCP_MAGIC_OFF 236 #define DHCP_OPT_OFF 240 static unsigned char msg[DHCP_MSG_LEN]; static unsigned char rxbuf[1520]; /* Last successful lease (for `netinfo`). */ static int have_info; static unsigned char last_server[4]; static unsigned int last_lease; /* Number of UDP port-68 datagrams seen in the current dhcp_run(), regardless * of whether dhcp_parse() liked them. Printed on failure so netmon can tell * "no reply at all" from "reply arrived but was rejected". */ static unsigned int dhcp_rx68; /* Rejection breakdown for the same window. */ static unsigned int dhcp_frames, dhcp_udp, dhcp_other_udp, dhcp_not_udp; static unsigned int dhcp_p68, dhcp_short, dhcp_bad_magic, dhcp_bad_xid; static unsigned int dhcp_bad_mac; /* What the non-UDP frames seen during the wait actually are. A tagged * OFFER, a fragmented frame and a missing OFFER all look the same from the * dhcp_rx68 counter otherwise. */ static unsigned int dhcp_arp, dhcp_ip4, dhcp_ip6, dhcp_vlan, dhcp_other; static unsigned int dhcp_maxlen; static unsigned int dhcp_max_et; static unsigned char dhcp_max_hdr[16]; static unsigned int dhcp_first_sport, dhcp_first_dport; static unsigned int dhcp_first_xid; static unsigned char dhcp_first_src[4], dhcp_first_dst[4]; static unsigned char dhcp_first_chaddr[6]; static int dhcp_have_udp, dhcp_have_p68; static void put_ip(const unsigned char *ip) { unsigned int i; for (i = 0; i < 4; i++) { unsigned int v = ip[i]; if (i) io_putchar('.'); if (v >= 100) { io_putchar((char)('0' + v / 100)); v %= 100; io_putchar((char)('0' + v / 10)); v %= 10; } else if (v >= 10) { io_putchar((char)('0' + v / 10)); v %= 10; } io_putchar((char)('0' + v)); } } static void put_dec(unsigned int v) { char b[12]; int i = 0; if (v == 0) { io_putchar('0'); return; } while (v) { b[i++] = (char)('0' + (v % 10)); v /= 10; } while (i) io_putchar(b[--i]); } static void put_hex2(unsigned int v) { static const char hex[] = "0123456789ABCDEF"; io_putchar(hex[(v >> 4) & 0xF]); io_putchar(hex[v & 0xF]); } static unsigned int rd32be(const unsigned char *p) { return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) | ((unsigned int)p[2] << 8) | p[3]; } static void wr32be(unsigned char *p, unsigned int v) { p[0] = (unsigned char)(v >> 24); p[1] = (unsigned char)(v >> 16); p[2] = (unsigned char)(v >> 8); p[3] = (unsigned char)v; } /* Build a DHCP message of the given type. `requested` (may be NULL) goes * into option 50 so the router tends to hand back the address we ask for. */ static void dhcp_build(unsigned char type, const unsigned char *my_mac, unsigned int xid, unsigned int secs, const unsigned char *requested, const unsigned char *server_id) { unsigned int i, o; for (i = 0; i < DHCP_MSG_LEN; i++) msg[i] = 0; msg[0] = DHCP_OP_REQUEST; msg[1] = 1; /* htype: Ethernet */ msg[2] = 6; /* hlen */ msg[3] = 0; /* hops */ wr32be(msg + 4, xid); msg[8] = (unsigned char)(secs >> 8); msg[9] = (unsigned char)secs; msg[10] = 0x80; /* flags: broadcast replies */ msg[11] = 0; /* ciaddr/yiaddr/siaddr/giaddr stay zero */ for (i = 0; i < 6; i++) msg[28 + i] = my_mac[i]; /* chaddr */ msg[DHCP_MAGIC_OFF + 0] = 0x63; msg[DHCP_MAGIC_OFF + 1] = 0x82; msg[DHCP_MAGIC_OFF + 2] = 0x53; msg[DHCP_MAGIC_OFF + 3] = 0x63; o = DHCP_OPT_OFF; msg[o++] = 53; msg[o++] = 1; msg[o++] = type; /* message type */ msg[o++] = 61; msg[o++] = 7; msg[o++] = 1; /* client id */ for (i = 0; i < 6; i++) msg[o++] = my_mac[i]; { static const char hostname[] = "nebos"; unsigned int n = sizeof(hostname) - 1; msg[o++] = 12; /* host name */ msg[o++] = (unsigned char)n; for (i = 0; i < n; i++) msg[o++] = (unsigned char)hostname[i]; } msg[o++] = 55; msg[o++] = 4; /* param request */ msg[o++] = 1; msg[o++] = 3; msg[o++] = 6; msg[o++] = 51; if (requested) { msg[o++] = 50; msg[o++] = 4; for (i = 0; i < 4; i++) msg[o++] = requested[i]; } if (server_id) { msg[o++] = 54; msg[o++] = 4; for (i = 0; i < 4; i++) msg[o++] = server_id[i]; } msg[o++] = 255; /* end */ } /* Parse a reply; returns 1 for "ours" and fills the option values. */ static int dhcp_parse(const unsigned char *frame, unsigned int len, unsigned int xid, const unsigned char *my_mac, unsigned char *type, unsigned char yiaddr[4], unsigned char mask[4], unsigned char gw[4], unsigned char dns[4], unsigned int *lease, unsigned char server[4]) { const unsigned char *p; unsigned char s_ip[4], d_ip[4]; unsigned int sport, dport, plen, i, o; dhcp_frames++; { unsigned int et = ((unsigned int)frame[12] << 8) | frame[13]; if (len > dhcp_maxlen) { unsigned int i; dhcp_maxlen = len; dhcp_max_et = et; for (i = 0; i < sizeof(dhcp_max_hdr) && i < len; i++) dhcp_max_hdr[i] = frame[i]; } if (et == 0x0806) dhcp_arp++; else if (et == 0x0800) dhcp_ip4++; else if (et == 0x86DD) dhcp_ip6++; else if (et == 0x8100) dhcp_vlan++; else dhcp_other++; } if (!net_udp_parse_any(frame, len, s_ip, d_ip, &sport, &dport, &p, &plen)) { dhcp_not_udp++; return 0; } dhcp_udp++; if (!dhcp_have_udp) { dhcp_have_udp = 1; dhcp_first_sport = sport; dhcp_first_dport = dport; for (i = 0; i < 4; i++) { dhcp_first_src[i] = s_ip[i]; dhcp_first_dst[i] = d_ip[i]; } } if (dport != DHCP_CLIENT_PORT) { dhcp_other_udp++; return 0; } dhcp_rx68++; dhcp_p68++; if (!dhcp_have_p68) { dhcp_have_p68 = 1; dhcp_first_xid = 0; for (i = 0; i < 6; i++) dhcp_first_chaddr[i] = 0; if (plen >= 34) { dhcp_first_xid = rd32be(p + 4); for (i = 0; i < 6; i++) dhcp_first_chaddr[i] = p[28 + i]; } } if (plen < DHCP_OPT_OFF || p[0] != DHCP_OP_REPLY) { dhcp_short++; return 0; } if (p[DHCP_MAGIC_OFF] != 0x63 || p[DHCP_MAGIC_OFF + 1] != 0x82 || p[DHCP_MAGIC_OFF + 2] != 0x53 || p[DHCP_MAGIC_OFF + 3] != 0x63) { dhcp_bad_magic++; return 0; } if (rd32be(p + 4) != xid) { dhcp_bad_xid++; return 0; } for (i = 0; i < 6; i++) { if (p[28 + i] != my_mac[i]) { dhcp_bad_mac++; return 0; } } *type = 0; for (i = 0; i < 4; i++) { yiaddr[i] = p[16 + i]; mask[i] = 0; gw[i] = 0; dns[i] = 0; server[i] = 0; } *lease = 0; /* Option walk. */ o = DHCP_OPT_OFF; while (o + 1 < plen) { unsigned char code = p[o]; unsigned int olen; if (code == 0) { /* pad */ o++; continue; } if (code == 255) /* end */ break; if (o + 1 >= plen) break; olen = p[o + 1]; if (o + 2 + olen > plen) break; switch (code) { case 53: if (olen >= 1) *type = p[o + 2]; break; case 1: if (olen >= 4) for (i = 0; i < 4; i++) mask[i] = p[o + 2 + i]; break; case 3: if (olen >= 4) for (i = 0; i < 4; i++) gw[i] = p[o + 2 + i]; break; case 6: /* DNS servers (first one wins) */ if (olen >= 4) for (i = 0; i < 4; i++) dns[i] = p[o + 2 + i]; break; case 51: if (olen >= 4) *lease = rd32be(p + o + 2); break; case 54: if (olen >= 4) for (i = 0; i < 4; i++) server[i] = p[o + 2 + i]; break; default: break; } o += 2 + olen; } (void)sport; return *type != 0; } /* Wait for a reply of the wanted type until `deadline`. */ static int dhcp_wait(const struct netdev *nd, unsigned int xid, const unsigned char *my_mac, unsigned int deadline, unsigned char want, unsigned char yiaddr[4], unsigned char mask[4], unsigned char gw[4], unsigned char dns[4], unsigned int *lease, unsigned char server[4]) { while ((int)(get_ticks_ms() - deadline) < 0) { unsigned char type; unsigned char ryi[4], rmask[4], rgw[4], rdns[4], rserver[4]; unsigned int rlease; int len = nd->rx(rxbuf, sizeof(rxbuf)); if (len < 0) continue; if (!dhcp_parse(rxbuf, (unsigned int)len, xid, my_mac, &type, ryi, rmask, rgw, rdns, &rlease, rserver)) continue; if (type == DHCP_NAK) return -2; if (type != want) continue; { unsigned int i; for (i = 0; i < 4; i++) { yiaddr[i] = ryi[i]; mask[i] = rmask[i]; gw[i] = rgw[i]; dns[i] = rdns[i]; server[i] = rserver[i]; } } *lease = rlease; return 0; } return -1; } static void dhcp_dump_stats(unsigned int xid) { unsigned int i; io_putstr("dhcp: seen "); put_dec(dhcp_frames); io_putstr(" frames, udp "); put_dec(dhcp_udp); io_putstr(" (p68 "); put_dec(dhcp_p68); io_putstr(", other port "); put_dec(dhcp_other_udp); io_putstr("), notudp "); put_dec(dhcp_not_udp); io_putstr(", short "); put_dec(dhcp_short); io_putstr(", magic "); put_dec(dhcp_bad_magic); io_putstr(", xid "); put_dec(dhcp_bad_xid); io_putstr(", mac "); put_dec(dhcp_bad_mac); io_putchar('\n'); io_putstr("dhcp: types arp "); put_dec(dhcp_arp); io_putstr(" ip4 "); put_dec(dhcp_ip4); io_putstr(" ip6 "); put_dec(dhcp_ip6); io_putstr(" vlan "); put_dec(dhcp_vlan); io_putstr(" other "); put_dec(dhcp_other); io_putstr(" maxlen "); put_dec(dhcp_maxlen); io_putstr(" maxet=0x"); put_hex2((dhcp_max_et >> 8) & 0xFF); put_hex2(dhcp_max_et & 0xFF); io_putstr(" hdr="); for (i = 0; i < sizeof(dhcp_max_hdr); i++) put_hex2(dhcp_max_hdr[i]); io_putchar('\n'); if (dhcp_have_udp) { io_putstr("dhcp: first udp "); put_dec(dhcp_first_sport); io_putstr("->"); put_dec(dhcp_first_dport); io_putstr(" from "); put_ip(dhcp_first_src); io_putstr(" to "); put_ip(dhcp_first_dst); io_putchar('\n'); } if (dhcp_have_p68) { io_putstr("dhcp: first p68 xid 0x"); io_puthex(dhcp_first_xid); io_putstr(" want 0x"); io_puthex(xid); io_putstr(" chaddr "); for (i = 0; i < 6; i++) { if (i) io_putchar(':'); put_hex2(dhcp_first_chaddr[i]); } io_putchar('\n'); } } int dhcp_run(unsigned int timeout_ms) { static const unsigned char bcast_mac[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; static const unsigned char bcast_ip[4] = { 255, 255, 255, 255 }; static const unsigned char zero_ip[4] = { 0, 0, 0, 0 }; const struct netdev *nd = netdev_active(); unsigned char my_mac[6], current[4]; unsigned char yiaddr[4], mask[4], gw[4], dns[4], server[4]; unsigned int xid, start, deadline, tries; unsigned int lease = 0; if (!nd || !nd->get_mac) return -2; nd->get_mac(my_mac); net_get_ip(current); xid = (get_ticks_ms() << 8) ^ ((unsigned int)my_mac[3] << 16) ^ ((unsigned int)my_mac[4] << 8) ^ my_mac[5]; start = get_ticks_ms(); deadline = start + timeout_ms; dhcp_rx68 = 0; dhcp_frames = dhcp_udp = dhcp_other_udp = dhcp_not_udp = 0; dhcp_p68 = dhcp_short = dhcp_bad_magic = dhcp_bad_xid = 0; dhcp_bad_mac = 0; dhcp_arp = dhcp_ip4 = dhcp_ip6 = dhcp_vlan = dhcp_other = 0; dhcp_maxlen = 0; dhcp_max_et = 0; { unsigned int i; for (i = 0; i < sizeof(dhcp_max_hdr); i++) dhcp_max_hdr[i] = 0; } dhcp_have_udp = dhcp_have_p68 = 0; /* Do not share the receiver with a raw net command or a stuck * netkexec transfer: both would consume each other's replies. */ if (netdev_in_use()) { io_putstr("dhcp: NIC is busy (see ps, kill the other command)\n"); return -4; } netdev_acquire(); io_putstr("dhcp: DISCOVER\n"); /* DISCOVER -> OFFER */ for (tries = 0; tries < 3; tries++) { unsigned int wait_until = get_ticks_ms() + 600; if ((int)(wait_until - deadline) > 0) wait_until = deadline; dhcp_build(DHCP_DISCOVER, my_mac, xid, get_ticks_ms() - start, 0, 0); if (net_udp_send_from(zero_ip, bcast_mac, bcast_ip, DHCP_CLIENT_PORT, DHCP_SERVER_PORT, msg, DHCP_MSG_LEN) != 0) { netdev_release(); return -3; /* TX failed (NIC present) */ } if (dhcp_wait(nd, xid, my_mac, wait_until, DHCP_OFFER, yiaddr, mask, gw, dns, &lease, server) == 0) break; if ((int)(get_ticks_ms() - deadline) >= 0) break; } if (tries == 3) { netdev_release(); io_putstr("dhcp: no server (rx "); put_dec(dhcp_rx68); io_putstr("), keeping "); put_ip(current); io_putchar('\n'); dhcp_dump_stats(xid); return -1; } io_putstr("dhcp: offer "); put_ip(yiaddr); io_putstr(" from "); put_ip(server); io_putchar('\n'); /* REQUEST -> ACK */ for (tries = 0; tries < 3; tries++) { unsigned int wait_until = get_ticks_ms() + 600; int r; if ((int)(wait_until - deadline) > 0) wait_until = deadline; dhcp_build(DHCP_REQUEST, my_mac, xid, get_ticks_ms() - start, yiaddr, server); if (net_udp_send_from(zero_ip, bcast_mac, bcast_ip, DHCP_CLIENT_PORT, DHCP_SERVER_PORT, msg, DHCP_MSG_LEN) != 0) { netdev_release(); return -3; /* TX failed (NIC present) */ } r = dhcp_wait(nd, xid, my_mac, wait_until, DHCP_ACK, yiaddr, mask, gw, dns, &lease, server); if (r == 0) break; if (r == -2) { /* NAK: start over */ netdev_release(); io_putstr("dhcp: NAK, retrying\n"); return dhcp_run(timeout_ms); } if ((int)(get_ticks_ms() - deadline) >= 0) break; } if (tries == 3) { netdev_release(); io_putstr("dhcp: no ACK (rx "); put_dec(dhcp_rx68); io_putstr("), keeping "); put_ip(current); io_putchar('\n'); dhcp_dump_stats(xid); return -1; } net_configure(yiaddr, mask, gw); if (dns[0] || dns[1] || dns[2] || dns[3]) net_set_dns(dns); have_info = 1; { unsigned int i; for (i = 0; i < 4; i++) last_server[i] = server[i]; } last_lease = lease; io_putstr("dhcp: ack "); put_ip(yiaddr); io_putstr("/"); put_ip(mask); io_putstr(" gw "); put_ip(gw); if (dns[0] || dns[1] || dns[2] || dns[3]) { io_putstr(" dns "); put_ip(dns); } io_putstr(" lease "); put_dec(lease); io_putstr("s\n"); netdev_release(); dhcp_dump_stats(xid); return 0; } int dhcp_last_info(unsigned char server[4], unsigned int *lease_s) { unsigned int i; if (!have_info) return 0; for (i = 0; i < 4; i++) server[i] = last_server[i]; *lease_s = last_lease; return 1; }