dhcp.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 int *lease, unsigned char server[4])
  133. {
  134. const unsigned char *p;
  135. unsigned char s_ip[4], d_ip[4];
  136. unsigned int sport, dport, plen, i, o;
  137. if (!net_udp_parse(frame, len, s_ip, d_ip, &sport, &dport, &p, &plen))
  138. return 0;
  139. if (dport != DHCP_CLIENT_PORT)
  140. return 0;
  141. if (plen < DHCP_OPT_OFF || p[0] != DHCP_OP_REPLY)
  142. return 0;
  143. if (p[DHCP_MAGIC_OFF] != 0x63 || p[DHCP_MAGIC_OFF + 1] != 0x82 ||
  144. p[DHCP_MAGIC_OFF + 2] != 0x53 || p[DHCP_MAGIC_OFF + 3] != 0x63)
  145. return 0;
  146. if (rd32be(p + 4) != xid)
  147. return 0;
  148. for (i = 0; i < 6; i++) {
  149. if (p[28 + i] != my_mac[i])
  150. return 0;
  151. }
  152. *type = 0;
  153. for (i = 0; i < 4; i++) {
  154. yiaddr[i] = p[16 + i];
  155. mask[i] = 0;
  156. gw[i] = 0;
  157. server[i] = 0;
  158. }
  159. *lease = 0;
  160. /* Option walk. */
  161. o = DHCP_OPT_OFF;
  162. while (o + 1 < plen) {
  163. unsigned char code = p[o];
  164. unsigned int olen;
  165. if (code == 0) { /* pad */
  166. o++;
  167. continue;
  168. }
  169. if (code == 255) /* end */
  170. break;
  171. if (o + 1 >= plen)
  172. break;
  173. olen = p[o + 1];
  174. if (o + 2 + olen > plen)
  175. break;
  176. switch (code) {
  177. case 53:
  178. if (olen >= 1)
  179. *type = p[o + 2];
  180. break;
  181. case 1:
  182. if (olen >= 4)
  183. for (i = 0; i < 4; i++)
  184. mask[i] = p[o + 2 + i];
  185. break;
  186. case 3:
  187. if (olen >= 4)
  188. for (i = 0; i < 4; i++)
  189. gw[i] = p[o + 2 + i];
  190. break;
  191. case 51:
  192. if (olen >= 4)
  193. *lease = rd32be(p + o + 2);
  194. break;
  195. case 54:
  196. if (olen >= 4)
  197. for (i = 0; i < 4; i++)
  198. server[i] = p[o + 2 + i];
  199. break;
  200. default:
  201. break;
  202. }
  203. o += 2 + olen;
  204. }
  205. (void)sport;
  206. return *type != 0;
  207. }
  208. /* Wait for a reply of the wanted type until `deadline`. */
  209. static int dhcp_wait(const struct netdev *nd, unsigned int xid,
  210. const unsigned char *my_mac, unsigned int deadline,
  211. unsigned char want, unsigned char yiaddr[4],
  212. unsigned char mask[4], unsigned char gw[4],
  213. unsigned int *lease, unsigned char server[4])
  214. {
  215. while ((int)(get_ticks_ms() - deadline) < 0) {
  216. unsigned char type;
  217. unsigned char ryi[4], rmask[4], rgw[4], rserver[4];
  218. unsigned int rlease;
  219. int len = nd->rx(rxbuf, sizeof(rxbuf));
  220. if (len < 0)
  221. continue;
  222. if (!dhcp_parse(rxbuf, (unsigned int)len, xid, my_mac, &type,
  223. ryi, rmask, rgw, &rlease, rserver))
  224. continue;
  225. if (type == DHCP_NAK)
  226. return -2;
  227. if (type != want)
  228. continue;
  229. {
  230. unsigned int i;
  231. for (i = 0; i < 4; i++) {
  232. yiaddr[i] = ryi[i];
  233. mask[i] = rmask[i];
  234. gw[i] = rgw[i];
  235. server[i] = rserver[i];
  236. }
  237. }
  238. *lease = rlease;
  239. return 0;
  240. }
  241. return -1;
  242. }
  243. int dhcp_run(unsigned int timeout_ms)
  244. {
  245. static const unsigned char bcast_mac[6] = {
  246. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  247. };
  248. static const unsigned char bcast_ip[4] = { 255, 255, 255, 255 };
  249. static const unsigned char zero_ip[4] = { 0, 0, 0, 0 };
  250. const struct netdev *nd = netdev_active();
  251. unsigned char my_mac[6], want_ip[4], current[4];
  252. unsigned char yiaddr[4], mask[4], gw[4], server[4];
  253. unsigned int xid, start, deadline, tries;
  254. unsigned int lease = 0;
  255. if (!nd || !nd->get_mac)
  256. return -2;
  257. nd->get_mac(my_mac);
  258. net_get_ip(current);
  259. /* Try to get the address we would use anyway (static default or the
  260. * previous lease): most routers honour option 50. */
  261. for (tries = 0; tries < 4; tries++)
  262. want_ip[tries] = current[tries];
  263. xid = (get_ticks_ms() << 8) ^ ((unsigned int)my_mac[3] << 16) ^
  264. ((unsigned int)my_mac[4] << 8) ^ my_mac[5];
  265. start = get_ticks_ms();
  266. deadline = start + timeout_ms;
  267. netdev_acquire();
  268. io_putstr("dhcp: DISCOVER\n");
  269. /* DISCOVER -> OFFER */
  270. for (tries = 0; tries < 3; tries++) {
  271. unsigned int wait_until = get_ticks_ms() + 600;
  272. if ((int)(wait_until - deadline) > 0)
  273. wait_until = deadline;
  274. dhcp_build(DHCP_DISCOVER, my_mac, xid,
  275. get_ticks_ms() - start, want_ip, 0);
  276. if (net_udp_send_from(zero_ip, bcast_mac, bcast_ip,
  277. DHCP_CLIENT_PORT, DHCP_SERVER_PORT,
  278. msg, DHCP_MSG_LEN) != 0) {
  279. netdev_release();
  280. return -2;
  281. }
  282. if (dhcp_wait(nd, xid, my_mac, wait_until, DHCP_OFFER,
  283. yiaddr, mask, gw, &lease, server) == 0)
  284. break;
  285. if ((int)(get_ticks_ms() - deadline) >= 0)
  286. break;
  287. }
  288. if (tries == 3) {
  289. netdev_release();
  290. io_putstr("dhcp: no server, keeping ");
  291. put_ip(current);
  292. io_putchar('\n');
  293. return -1;
  294. }
  295. io_putstr("dhcp: offer ");
  296. put_ip(yiaddr);
  297. io_putstr(" from ");
  298. put_ip(server);
  299. io_putchar('\n');
  300. /* REQUEST -> ACK */
  301. for (tries = 0; tries < 3; tries++) {
  302. unsigned int wait_until = get_ticks_ms() + 600;
  303. int r;
  304. if ((int)(wait_until - deadline) > 0)
  305. wait_until = deadline;
  306. dhcp_build(DHCP_REQUEST, my_mac, xid,
  307. get_ticks_ms() - start, yiaddr, server);
  308. if (net_udp_send_from(zero_ip, bcast_mac, bcast_ip,
  309. DHCP_CLIENT_PORT, DHCP_SERVER_PORT,
  310. msg, DHCP_MSG_LEN) != 0) {
  311. netdev_release();
  312. return -2;
  313. }
  314. r = dhcp_wait(nd, xid, my_mac, wait_until, DHCP_ACK,
  315. yiaddr, mask, gw, &lease, server);
  316. if (r == 0)
  317. break;
  318. if (r == -2) { /* NAK: start over */
  319. netdev_release();
  320. io_putstr("dhcp: NAK, retrying\n");
  321. return dhcp_run(timeout_ms);
  322. }
  323. if ((int)(get_ticks_ms() - deadline) >= 0)
  324. break;
  325. }
  326. if (tries == 3) {
  327. netdev_release();
  328. io_putstr("dhcp: no ACK, keeping ");
  329. put_ip(current);
  330. io_putchar('\n');
  331. return -1;
  332. }
  333. net_configure(yiaddr, mask, gw);
  334. have_info = 1;
  335. {
  336. unsigned int i;
  337. for (i = 0; i < 4; i++)
  338. last_server[i] = server[i];
  339. }
  340. last_lease = lease;
  341. io_putstr("dhcp: ack ");
  342. put_ip(yiaddr);
  343. io_putstr("/");
  344. put_ip(mask);
  345. io_putstr(" gw ");
  346. put_ip(gw);
  347. io_putstr(" lease ");
  348. put_dec(lease);
  349. io_putstr("s\n");
  350. netdev_release();
  351. return 0;
  352. }
  353. int dhcp_last_info(unsigned char server[4], unsigned int *lease_s)
  354. {
  355. unsigned int i;
  356. if (!have_info)
  357. return 0;
  358. for (i = 0; i < 4; i++)
  359. server[i] = last_server[i];
  360. *lease_s = last_lease;
  361. return 1;
  362. }