dhcp.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /* Minimal DHCP client: see dhcp.h. */
  2. #include "io.h"
  3. #include "net.h"
  4. #include "netdev.h"
  5. #include "timer.h"
  6. #include "dhcp.h"
  7. #define DHCP_SERVER_PORT 67
  8. #define DHCP_CLIENT_PORT 68
  9. #define DHCP_OP_REQUEST 1
  10. #define DHCP_OP_REPLY 2
  11. #define DHCP_DISCOVER 1
  12. #define DHCP_OFFER 2
  13. #define DHCP_REQUEST 3
  14. #define DHCP_ACK 5
  15. #define DHCP_NAK 6
  16. /* BOOTP header (236 bytes) + cookie + options, padded to a 300-byte DHCP
  17. * message (RFC 2131 minimum). */
  18. #define DHCP_MSG_LEN 300
  19. #define DHCP_HDR_LEN 236
  20. #define DHCP_MAGIC_OFF 236
  21. #define DHCP_OPT_OFF 240
  22. static unsigned char msg[DHCP_MSG_LEN];
  23. static unsigned char rxbuf[1520];
  24. /* Last successful lease (for `netinfo`). */
  25. static int have_info;
  26. static unsigned char last_server[4];
  27. static unsigned int last_lease;
  28. static void put_ip(const unsigned char *ip)
  29. {
  30. unsigned int i;
  31. for (i = 0; i < 4; i++) {
  32. unsigned int v = ip[i];
  33. if (i)
  34. io_putchar('.');
  35. if (v >= 100) {
  36. io_putchar((char)('0' + v / 100));
  37. v %= 100;
  38. io_putchar((char)('0' + v / 10));
  39. v %= 10;
  40. } else if (v >= 10) {
  41. io_putchar((char)('0' + v / 10));
  42. v %= 10;
  43. }
  44. io_putchar((char)('0' + v));
  45. }
  46. }
  47. static void put_dec(unsigned int v)
  48. {
  49. char b[12];
  50. int i = 0;
  51. if (v == 0) {
  52. io_putchar('0');
  53. return;
  54. }
  55. while (v) {
  56. b[i++] = (char)('0' + (v % 10));
  57. v /= 10;
  58. }
  59. while (i)
  60. io_putchar(b[--i]);
  61. }
  62. static unsigned int rd32be(const unsigned char *p)
  63. {
  64. return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) |
  65. ((unsigned int)p[2] << 8) | p[3];
  66. }
  67. static void wr32be(unsigned char *p, unsigned int v)
  68. {
  69. p[0] = (unsigned char)(v >> 24);
  70. p[1] = (unsigned char)(v >> 16);
  71. p[2] = (unsigned char)(v >> 8);
  72. p[3] = (unsigned char)v;
  73. }
  74. /* Build a DHCP message of the given type. `requested` (may be NULL) goes
  75. * into option 50 so the router tends to hand back the address we ask for. */
  76. static void dhcp_build(unsigned char type, const unsigned char *my_mac,
  77. unsigned int xid, unsigned int secs,
  78. const unsigned char *requested,
  79. const unsigned char *server_id)
  80. {
  81. unsigned int i, o;
  82. for (i = 0; i < DHCP_MSG_LEN; i++)
  83. msg[i] = 0;
  84. msg[0] = DHCP_OP_REQUEST;
  85. msg[1] = 1; /* htype: Ethernet */
  86. msg[2] = 6; /* hlen */
  87. msg[3] = 0; /* hops */
  88. wr32be(msg + 4, xid);
  89. msg[8] = (unsigned char)(secs >> 8);
  90. msg[9] = (unsigned char)secs;
  91. msg[10] = 0x80; /* flags: broadcast replies */
  92. msg[11] = 0;
  93. /* ciaddr/yiaddr/siaddr/giaddr stay zero */
  94. for (i = 0; i < 6; i++)
  95. msg[28 + i] = my_mac[i]; /* chaddr */
  96. msg[DHCP_MAGIC_OFF + 0] = 0x63;
  97. msg[DHCP_MAGIC_OFF + 1] = 0x82;
  98. msg[DHCP_MAGIC_OFF + 2] = 0x53;
  99. msg[DHCP_MAGIC_OFF + 3] = 0x63;
  100. o = DHCP_OPT_OFF;
  101. msg[o++] = 53; msg[o++] = 1; msg[o++] = type; /* message type */
  102. msg[o++] = 61; msg[o++] = 7; msg[o++] = 1; /* client id */
  103. for (i = 0; i < 6; i++)
  104. msg[o++] = my_mac[i];
  105. {
  106. static const char hostname[] = "nebos";
  107. unsigned int n = sizeof(hostname) - 1;
  108. msg[o++] = 12; /* host name */
  109. msg[o++] = (unsigned char)n;
  110. for (i = 0; i < n; i++)
  111. msg[o++] = (unsigned char)hostname[i];
  112. }
  113. msg[o++] = 55; msg[o++] = 4; /* param request */
  114. msg[o++] = 1; msg[o++] = 3; msg[o++] = 6; msg[o++] = 51;
  115. if (requested) {
  116. msg[o++] = 50; msg[o++] = 4;
  117. for (i = 0; i < 4; i++)
  118. msg[o++] = requested[i];
  119. }
  120. if (server_id) {
  121. msg[o++] = 54; msg[o++] = 4;
  122. for (i = 0; i < 4; i++)
  123. msg[o++] = server_id[i];
  124. }
  125. msg[o++] = 255; /* end */
  126. }
  127. /* Parse a reply; returns 1 for "ours" and fills the option values. */
  128. static int dhcp_parse(const unsigned char *frame, unsigned int len,
  129. unsigned int xid, const unsigned char *my_mac,
  130. unsigned char *type, unsigned char yiaddr[4],
  131. unsigned char mask[4], unsigned char gw[4],
  132. unsigned char dns[4],
  133. unsigned int *lease, unsigned char server[4])
  134. {
  135. const unsigned char *p;
  136. unsigned char s_ip[4], d_ip[4];
  137. unsigned int sport, dport, plen, i, o;
  138. if (!net_udp_parse(frame, len, s_ip, d_ip, &sport, &dport, &p, &plen))
  139. return 0;
  140. if (dport != DHCP_CLIENT_PORT)
  141. return 0;
  142. if (plen < DHCP_OPT_OFF || p[0] != DHCP_OP_REPLY)
  143. return 0;
  144. if (p[DHCP_MAGIC_OFF] != 0x63 || p[DHCP_MAGIC_OFF + 1] != 0x82 ||
  145. p[DHCP_MAGIC_OFF + 2] != 0x53 || p[DHCP_MAGIC_OFF + 3] != 0x63)
  146. return 0;
  147. if (rd32be(p + 4) != xid)
  148. return 0;
  149. for (i = 0; i < 6; i++) {
  150. if (p[28 + i] != my_mac[i])
  151. return 0;
  152. }
  153. *type = 0;
  154. for (i = 0; i < 4; i++) {
  155. yiaddr[i] = p[16 + i];
  156. mask[i] = 0;
  157. gw[i] = 0;
  158. dns[i] = 0;
  159. server[i] = 0;
  160. }
  161. *lease = 0;
  162. /* Option walk. */
  163. o = DHCP_OPT_OFF;
  164. while (o + 1 < plen) {
  165. unsigned char code = p[o];
  166. unsigned int olen;
  167. if (code == 0) { /* pad */
  168. o++;
  169. continue;
  170. }
  171. if (code == 255) /* end */
  172. break;
  173. if (o + 1 >= plen)
  174. break;
  175. olen = p[o + 1];
  176. if (o + 2 + olen > plen)
  177. break;
  178. switch (code) {
  179. case 53:
  180. if (olen >= 1)
  181. *type = p[o + 2];
  182. break;
  183. case 1:
  184. if (olen >= 4)
  185. for (i = 0; i < 4; i++)
  186. mask[i] = p[o + 2 + i];
  187. break;
  188. case 3:
  189. if (olen >= 4)
  190. for (i = 0; i < 4; i++)
  191. gw[i] = p[o + 2 + i];
  192. break;
  193. case 6: /* DNS servers (first one wins) */
  194. if (olen >= 4)
  195. for (i = 0; i < 4; i++)
  196. dns[i] = p[o + 2 + i];
  197. break;
  198. case 51:
  199. if (olen >= 4)
  200. *lease = rd32be(p + o + 2);
  201. break;
  202. case 54:
  203. if (olen >= 4)
  204. for (i = 0; i < 4; i++)
  205. server[i] = p[o + 2 + i];
  206. break;
  207. default:
  208. break;
  209. }
  210. o += 2 + olen;
  211. }
  212. (void)sport;
  213. return *type != 0;
  214. }
  215. /* Wait for a reply of the wanted type until `deadline`. */
  216. static int dhcp_wait(const struct netdev *nd, unsigned int xid,
  217. const unsigned char *my_mac, unsigned int deadline,
  218. unsigned char want, unsigned char yiaddr[4],
  219. unsigned char mask[4], unsigned char gw[4],
  220. unsigned char dns[4],
  221. unsigned int *lease, unsigned char server[4])
  222. {
  223. while ((int)(get_ticks_ms() - deadline) < 0) {
  224. unsigned char type;
  225. unsigned char ryi[4], rmask[4], rgw[4], rdns[4], rserver[4];
  226. unsigned int rlease;
  227. int len = nd->rx(rxbuf, sizeof(rxbuf));
  228. if (len < 0)
  229. continue;
  230. if (!dhcp_parse(rxbuf, (unsigned int)len, xid, my_mac, &type,
  231. ryi, rmask, rgw, rdns, &rlease, rserver))
  232. continue;
  233. if (type == DHCP_NAK)
  234. return -2;
  235. if (type != want)
  236. continue;
  237. {
  238. unsigned int i;
  239. for (i = 0; i < 4; i++) {
  240. yiaddr[i] = ryi[i];
  241. mask[i] = rmask[i];
  242. gw[i] = rgw[i];
  243. dns[i] = rdns[i];
  244. server[i] = rserver[i];
  245. }
  246. }
  247. *lease = rlease;
  248. return 0;
  249. }
  250. return -1;
  251. }
  252. int dhcp_run(unsigned int timeout_ms)
  253. {
  254. static const unsigned char bcast_mac[6] = {
  255. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  256. };
  257. static const unsigned char bcast_ip[4] = { 255, 255, 255, 255 };
  258. static const unsigned char zero_ip[4] = { 0, 0, 0, 0 };
  259. const struct netdev *nd = netdev_active();
  260. unsigned char my_mac[6], want_ip[4], current[4];
  261. unsigned char yiaddr[4], mask[4], gw[4], dns[4], server[4];
  262. unsigned int xid, start, deadline, tries;
  263. unsigned int lease = 0;
  264. if (!nd || !nd->get_mac)
  265. return -2;
  266. nd->get_mac(my_mac);
  267. net_get_ip(current);
  268. /* Try to get the address we would use anyway (static default or the
  269. * previous lease): most routers honour option 50. */
  270. for (tries = 0; tries < 4; tries++)
  271. want_ip[tries] = current[tries];
  272. xid = (get_ticks_ms() << 8) ^ ((unsigned int)my_mac[3] << 16) ^
  273. ((unsigned int)my_mac[4] << 8) ^ my_mac[5];
  274. start = get_ticks_ms();
  275. deadline = start + timeout_ms;
  276. netdev_acquire();
  277. io_putstr("dhcp: DISCOVER\n");
  278. /* DISCOVER -> OFFER */
  279. for (tries = 0; tries < 3; tries++) {
  280. unsigned int wait_until = get_ticks_ms() + 600;
  281. if ((int)(wait_until - deadline) > 0)
  282. wait_until = deadline;
  283. dhcp_build(DHCP_DISCOVER, my_mac, xid,
  284. get_ticks_ms() - start, want_ip, 0);
  285. if (net_udp_send_from(zero_ip, bcast_mac, bcast_ip,
  286. DHCP_CLIENT_PORT, DHCP_SERVER_PORT,
  287. msg, DHCP_MSG_LEN) != 0) {
  288. netdev_release();
  289. return -2;
  290. }
  291. if (dhcp_wait(nd, xid, my_mac, wait_until, DHCP_OFFER,
  292. yiaddr, mask, gw, dns, &lease, server) == 0)
  293. break;
  294. if ((int)(get_ticks_ms() - deadline) >= 0)
  295. break;
  296. }
  297. if (tries == 3) {
  298. netdev_release();
  299. io_putstr("dhcp: no server, keeping ");
  300. put_ip(current);
  301. io_putchar('\n');
  302. return -1;
  303. }
  304. io_putstr("dhcp: offer ");
  305. put_ip(yiaddr);
  306. io_putstr(" from ");
  307. put_ip(server);
  308. io_putchar('\n');
  309. /* REQUEST -> ACK */
  310. for (tries = 0; tries < 3; tries++) {
  311. unsigned int wait_until = get_ticks_ms() + 600;
  312. int r;
  313. if ((int)(wait_until - deadline) > 0)
  314. wait_until = deadline;
  315. dhcp_build(DHCP_REQUEST, my_mac, xid,
  316. get_ticks_ms() - start, yiaddr, server);
  317. if (net_udp_send_from(zero_ip, bcast_mac, bcast_ip,
  318. DHCP_CLIENT_PORT, DHCP_SERVER_PORT,
  319. msg, DHCP_MSG_LEN) != 0) {
  320. netdev_release();
  321. return -2;
  322. }
  323. r = dhcp_wait(nd, xid, my_mac, wait_until, DHCP_ACK,
  324. yiaddr, mask, gw, dns, &lease, server);
  325. if (r == 0)
  326. break;
  327. if (r == -2) { /* NAK: start over */
  328. netdev_release();
  329. io_putstr("dhcp: NAK, retrying\n");
  330. return dhcp_run(timeout_ms);
  331. }
  332. if ((int)(get_ticks_ms() - deadline) >= 0)
  333. break;
  334. }
  335. if (tries == 3) {
  336. netdev_release();
  337. io_putstr("dhcp: no ACK, keeping ");
  338. put_ip(current);
  339. io_putchar('\n');
  340. return -1;
  341. }
  342. net_configure(yiaddr, mask, gw);
  343. if (dns[0] || dns[1] || dns[2] || dns[3])
  344. net_set_dns(dns);
  345. have_info = 1;
  346. {
  347. unsigned int i;
  348. for (i = 0; i < 4; i++)
  349. last_server[i] = server[i];
  350. }
  351. last_lease = lease;
  352. io_putstr("dhcp: ack ");
  353. put_ip(yiaddr);
  354. io_putstr("/");
  355. put_ip(mask);
  356. io_putstr(" gw ");
  357. put_ip(gw);
  358. if (dns[0] || dns[1] || dns[2] || dns[3]) {
  359. io_putstr(" dns ");
  360. put_ip(dns);
  361. }
  362. io_putstr(" lease ");
  363. put_dec(lease);
  364. io_putstr("s\n");
  365. netdev_release();
  366. return 0;
  367. }
  368. int dhcp_last_info(unsigned char server[4], unsigned int *lease_s)
  369. {
  370. unsigned int i;
  371. if (!have_info)
  372. return 0;
  373. for (i = 0; i < 4; i++)
  374. server[i] = last_server[i];
  375. *lease_s = last_lease;
  376. return 1;
  377. }