dhcp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. /* Number of UDP port-68 datagrams seen in the current dhcp_run(), regardless
  29. * of whether dhcp_parse() liked them. Printed on failure so netmon can tell
  30. * "no reply at all" from "reply arrived but was rejected". */
  31. static unsigned int dhcp_rx68;
  32. /* Rejection breakdown for the same window. */
  33. static unsigned int dhcp_frames, dhcp_udp, dhcp_other_udp, dhcp_not_udp;
  34. static unsigned int dhcp_p68, dhcp_short, dhcp_bad_magic, dhcp_bad_xid;
  35. static unsigned int dhcp_bad_mac;
  36. static unsigned int dhcp_first_sport, dhcp_first_dport;
  37. static unsigned int dhcp_first_xid;
  38. static unsigned char dhcp_first_src[4], dhcp_first_dst[4];
  39. static unsigned char dhcp_first_chaddr[6];
  40. static int dhcp_have_udp, dhcp_have_p68;
  41. static void put_ip(const unsigned char *ip)
  42. {
  43. unsigned int i;
  44. for (i = 0; i < 4; i++) {
  45. unsigned int v = ip[i];
  46. if (i)
  47. io_putchar('.');
  48. if (v >= 100) {
  49. io_putchar((char)('0' + v / 100));
  50. v %= 100;
  51. io_putchar((char)('0' + v / 10));
  52. v %= 10;
  53. } else if (v >= 10) {
  54. io_putchar((char)('0' + v / 10));
  55. v %= 10;
  56. }
  57. io_putchar((char)('0' + v));
  58. }
  59. }
  60. static void put_dec(unsigned int v)
  61. {
  62. char b[12];
  63. int i = 0;
  64. if (v == 0) {
  65. io_putchar('0');
  66. return;
  67. }
  68. while (v) {
  69. b[i++] = (char)('0' + (v % 10));
  70. v /= 10;
  71. }
  72. while (i)
  73. io_putchar(b[--i]);
  74. }
  75. static void put_hex2(unsigned int v)
  76. {
  77. static const char hex[] = "0123456789ABCDEF";
  78. io_putchar(hex[(v >> 4) & 0xF]);
  79. io_putchar(hex[v & 0xF]);
  80. }
  81. static unsigned int rd32be(const unsigned char *p)
  82. {
  83. return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) |
  84. ((unsigned int)p[2] << 8) | p[3];
  85. }
  86. static void wr32be(unsigned char *p, unsigned int v)
  87. {
  88. p[0] = (unsigned char)(v >> 24);
  89. p[1] = (unsigned char)(v >> 16);
  90. p[2] = (unsigned char)(v >> 8);
  91. p[3] = (unsigned char)v;
  92. }
  93. /* Build a DHCP message of the given type. `requested` (may be NULL) goes
  94. * into option 50 so the router tends to hand back the address we ask for. */
  95. static void dhcp_build(unsigned char type, const unsigned char *my_mac,
  96. unsigned int xid, unsigned int secs,
  97. const unsigned char *requested,
  98. const unsigned char *server_id)
  99. {
  100. unsigned int i, o;
  101. for (i = 0; i < DHCP_MSG_LEN; i++)
  102. msg[i] = 0;
  103. msg[0] = DHCP_OP_REQUEST;
  104. msg[1] = 1; /* htype: Ethernet */
  105. msg[2] = 6; /* hlen */
  106. msg[3] = 0; /* hops */
  107. wr32be(msg + 4, xid);
  108. msg[8] = (unsigned char)(secs >> 8);
  109. msg[9] = (unsigned char)secs;
  110. msg[10] = 0x80; /* flags: broadcast replies */
  111. msg[11] = 0;
  112. /* ciaddr/yiaddr/siaddr/giaddr stay zero */
  113. for (i = 0; i < 6; i++)
  114. msg[28 + i] = my_mac[i]; /* chaddr */
  115. msg[DHCP_MAGIC_OFF + 0] = 0x63;
  116. msg[DHCP_MAGIC_OFF + 1] = 0x82;
  117. msg[DHCP_MAGIC_OFF + 2] = 0x53;
  118. msg[DHCP_MAGIC_OFF + 3] = 0x63;
  119. o = DHCP_OPT_OFF;
  120. msg[o++] = 53; msg[o++] = 1; msg[o++] = type; /* message type */
  121. msg[o++] = 61; msg[o++] = 7; msg[o++] = 1; /* client id */
  122. for (i = 0; i < 6; i++)
  123. msg[o++] = my_mac[i];
  124. {
  125. static const char hostname[] = "nebos";
  126. unsigned int n = sizeof(hostname) - 1;
  127. msg[o++] = 12; /* host name */
  128. msg[o++] = (unsigned char)n;
  129. for (i = 0; i < n; i++)
  130. msg[o++] = (unsigned char)hostname[i];
  131. }
  132. msg[o++] = 55; msg[o++] = 4; /* param request */
  133. msg[o++] = 1; msg[o++] = 3; msg[o++] = 6; msg[o++] = 51;
  134. if (requested) {
  135. msg[o++] = 50; msg[o++] = 4;
  136. for (i = 0; i < 4; i++)
  137. msg[o++] = requested[i];
  138. }
  139. if (server_id) {
  140. msg[o++] = 54; msg[o++] = 4;
  141. for (i = 0; i < 4; i++)
  142. msg[o++] = server_id[i];
  143. }
  144. msg[o++] = 255; /* end */
  145. }
  146. /* Parse a reply; returns 1 for "ours" and fills the option values. */
  147. static int dhcp_parse(const unsigned char *frame, unsigned int len,
  148. unsigned int xid, const unsigned char *my_mac,
  149. unsigned char *type, unsigned char yiaddr[4],
  150. unsigned char mask[4], unsigned char gw[4],
  151. unsigned char dns[4],
  152. unsigned int *lease, unsigned char server[4])
  153. {
  154. const unsigned char *p;
  155. unsigned char s_ip[4], d_ip[4];
  156. unsigned int sport, dport, plen, i, o;
  157. dhcp_frames++;
  158. if (!net_udp_parse_any(frame, len, s_ip, d_ip, &sport, &dport, &p, &plen)) {
  159. dhcp_not_udp++;
  160. return 0;
  161. }
  162. dhcp_udp++;
  163. if (!dhcp_have_udp) {
  164. dhcp_have_udp = 1;
  165. dhcp_first_sport = sport;
  166. dhcp_first_dport = dport;
  167. for (i = 0; i < 4; i++) {
  168. dhcp_first_src[i] = s_ip[i];
  169. dhcp_first_dst[i] = d_ip[i];
  170. }
  171. }
  172. if (dport != DHCP_CLIENT_PORT) {
  173. dhcp_other_udp++;
  174. return 0;
  175. }
  176. dhcp_rx68++;
  177. dhcp_p68++;
  178. if (!dhcp_have_p68) {
  179. dhcp_have_p68 = 1;
  180. dhcp_first_xid = 0;
  181. for (i = 0; i < 6; i++)
  182. dhcp_first_chaddr[i] = 0;
  183. if (plen >= 34) {
  184. dhcp_first_xid = rd32be(p + 4);
  185. for (i = 0; i < 6; i++)
  186. dhcp_first_chaddr[i] = p[28 + i];
  187. }
  188. }
  189. if (plen < DHCP_OPT_OFF || p[0] != DHCP_OP_REPLY) {
  190. dhcp_short++;
  191. return 0;
  192. }
  193. if (p[DHCP_MAGIC_OFF] != 0x63 || p[DHCP_MAGIC_OFF + 1] != 0x82 ||
  194. p[DHCP_MAGIC_OFF + 2] != 0x53 || p[DHCP_MAGIC_OFF + 3] != 0x63) {
  195. dhcp_bad_magic++;
  196. return 0;
  197. }
  198. if (rd32be(p + 4) != xid) {
  199. dhcp_bad_xid++;
  200. return 0;
  201. }
  202. for (i = 0; i < 6; i++) {
  203. if (p[28 + i] != my_mac[i]) {
  204. dhcp_bad_mac++;
  205. return 0;
  206. }
  207. }
  208. *type = 0;
  209. for (i = 0; i < 4; i++) {
  210. yiaddr[i] = p[16 + i];
  211. mask[i] = 0;
  212. gw[i] = 0;
  213. dns[i] = 0;
  214. server[i] = 0;
  215. }
  216. *lease = 0;
  217. /* Option walk. */
  218. o = DHCP_OPT_OFF;
  219. while (o + 1 < plen) {
  220. unsigned char code = p[o];
  221. unsigned int olen;
  222. if (code == 0) { /* pad */
  223. o++;
  224. continue;
  225. }
  226. if (code == 255) /* end */
  227. break;
  228. if (o + 1 >= plen)
  229. break;
  230. olen = p[o + 1];
  231. if (o + 2 + olen > plen)
  232. break;
  233. switch (code) {
  234. case 53:
  235. if (olen >= 1)
  236. *type = p[o + 2];
  237. break;
  238. case 1:
  239. if (olen >= 4)
  240. for (i = 0; i < 4; i++)
  241. mask[i] = p[o + 2 + i];
  242. break;
  243. case 3:
  244. if (olen >= 4)
  245. for (i = 0; i < 4; i++)
  246. gw[i] = p[o + 2 + i];
  247. break;
  248. case 6: /* DNS servers (first one wins) */
  249. if (olen >= 4)
  250. for (i = 0; i < 4; i++)
  251. dns[i] = p[o + 2 + i];
  252. break;
  253. case 51:
  254. if (olen >= 4)
  255. *lease = rd32be(p + o + 2);
  256. break;
  257. case 54:
  258. if (olen >= 4)
  259. for (i = 0; i < 4; i++)
  260. server[i] = p[o + 2 + i];
  261. break;
  262. default:
  263. break;
  264. }
  265. o += 2 + olen;
  266. }
  267. (void)sport;
  268. return *type != 0;
  269. }
  270. /* Wait for a reply of the wanted type until `deadline`. */
  271. static int dhcp_wait(const struct netdev *nd, unsigned int xid,
  272. const unsigned char *my_mac, unsigned int deadline,
  273. unsigned char want, unsigned char yiaddr[4],
  274. unsigned char mask[4], unsigned char gw[4],
  275. unsigned char dns[4],
  276. unsigned int *lease, unsigned char server[4])
  277. {
  278. while ((int)(get_ticks_ms() - deadline) < 0) {
  279. unsigned char type;
  280. unsigned char ryi[4], rmask[4], rgw[4], rdns[4], rserver[4];
  281. unsigned int rlease;
  282. int len = nd->rx(rxbuf, sizeof(rxbuf));
  283. if (len < 0)
  284. continue;
  285. if (!dhcp_parse(rxbuf, (unsigned int)len, xid, my_mac, &type,
  286. ryi, rmask, rgw, rdns, &rlease, rserver))
  287. continue;
  288. if (type == DHCP_NAK)
  289. return -2;
  290. if (type != want)
  291. continue;
  292. {
  293. unsigned int i;
  294. for (i = 0; i < 4; i++) {
  295. yiaddr[i] = ryi[i];
  296. mask[i] = rmask[i];
  297. gw[i] = rgw[i];
  298. dns[i] = rdns[i];
  299. server[i] = rserver[i];
  300. }
  301. }
  302. *lease = rlease;
  303. return 0;
  304. }
  305. return -1;
  306. }
  307. static void dhcp_dump_stats(unsigned int xid)
  308. {
  309. unsigned int i;
  310. io_putstr("dhcp: seen ");
  311. put_dec(dhcp_frames);
  312. io_putstr(" frames, udp ");
  313. put_dec(dhcp_udp);
  314. io_putstr(" (p68 ");
  315. put_dec(dhcp_p68);
  316. io_putstr(", other port ");
  317. put_dec(dhcp_other_udp);
  318. io_putstr("), notudp ");
  319. put_dec(dhcp_not_udp);
  320. io_putstr(", short ");
  321. put_dec(dhcp_short);
  322. io_putstr(", magic ");
  323. put_dec(dhcp_bad_magic);
  324. io_putstr(", xid ");
  325. put_dec(dhcp_bad_xid);
  326. io_putstr(", mac ");
  327. put_dec(dhcp_bad_mac);
  328. io_putchar('\n');
  329. if (dhcp_have_udp) {
  330. io_putstr("dhcp: first udp ");
  331. put_dec(dhcp_first_sport);
  332. io_putstr("->");
  333. put_dec(dhcp_first_dport);
  334. io_putstr(" from ");
  335. put_ip(dhcp_first_src);
  336. io_putstr(" to ");
  337. put_ip(dhcp_first_dst);
  338. io_putchar('\n');
  339. }
  340. if (dhcp_have_p68) {
  341. io_putstr("dhcp: first p68 xid 0x");
  342. io_puthex(dhcp_first_xid);
  343. io_putstr(" want 0x");
  344. io_puthex(xid);
  345. io_putstr(" chaddr ");
  346. for (i = 0; i < 6; i++) {
  347. if (i)
  348. io_putchar(':');
  349. put_hex2(dhcp_first_chaddr[i]);
  350. }
  351. io_putchar('\n');
  352. }
  353. }
  354. int dhcp_run(unsigned int timeout_ms)
  355. {
  356. static const unsigned char bcast_mac[6] = {
  357. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  358. };
  359. static const unsigned char bcast_ip[4] = { 255, 255, 255, 255 };
  360. static const unsigned char zero_ip[4] = { 0, 0, 0, 0 };
  361. const struct netdev *nd = netdev_active();
  362. unsigned char my_mac[6], current[4];
  363. unsigned char yiaddr[4], mask[4], gw[4], dns[4], server[4];
  364. unsigned int xid, start, deadline, tries;
  365. unsigned int lease = 0;
  366. if (!nd || !nd->get_mac)
  367. return -2;
  368. nd->get_mac(my_mac);
  369. net_get_ip(current);
  370. xid = (get_ticks_ms() << 8) ^ ((unsigned int)my_mac[3] << 16) ^
  371. ((unsigned int)my_mac[4] << 8) ^ my_mac[5];
  372. start = get_ticks_ms();
  373. deadline = start + timeout_ms;
  374. dhcp_rx68 = 0;
  375. dhcp_frames = dhcp_udp = dhcp_other_udp = dhcp_not_udp = 0;
  376. dhcp_p68 = dhcp_short = dhcp_bad_magic = dhcp_bad_xid = 0;
  377. dhcp_bad_mac = 0;
  378. dhcp_have_udp = dhcp_have_p68 = 0;
  379. /* Do not share the receiver with a raw net command or a stuck
  380. * netkexec transfer: both would consume each other's replies. */
  381. if (netdev_in_use()) {
  382. io_putstr("dhcp: NIC is busy (see ps, kill the other command)\n");
  383. return -4;
  384. }
  385. netdev_acquire();
  386. io_putstr("dhcp: DISCOVER\n");
  387. /* DISCOVER -> OFFER */
  388. for (tries = 0; tries < 3; tries++) {
  389. unsigned int wait_until = get_ticks_ms() + 600;
  390. if ((int)(wait_until - deadline) > 0)
  391. wait_until = deadline;
  392. dhcp_build(DHCP_DISCOVER, my_mac, xid,
  393. get_ticks_ms() - start, 0, 0);
  394. if (net_udp_send_from(zero_ip, bcast_mac, bcast_ip,
  395. DHCP_CLIENT_PORT, DHCP_SERVER_PORT,
  396. msg, DHCP_MSG_LEN) != 0) {
  397. netdev_release();
  398. return -3; /* TX failed (NIC present) */
  399. }
  400. if (dhcp_wait(nd, xid, my_mac, wait_until, DHCP_OFFER,
  401. yiaddr, mask, gw, dns, &lease, server) == 0)
  402. break;
  403. if ((int)(get_ticks_ms() - deadline) >= 0)
  404. break;
  405. }
  406. if (tries == 3) {
  407. netdev_release();
  408. io_putstr("dhcp: no server (rx ");
  409. put_dec(dhcp_rx68);
  410. io_putstr("), keeping ");
  411. put_ip(current);
  412. io_putchar('\n');
  413. dhcp_dump_stats(xid);
  414. return -1;
  415. }
  416. io_putstr("dhcp: offer ");
  417. put_ip(yiaddr);
  418. io_putstr(" from ");
  419. put_ip(server);
  420. io_putchar('\n');
  421. /* REQUEST -> ACK */
  422. for (tries = 0; tries < 3; tries++) {
  423. unsigned int wait_until = get_ticks_ms() + 600;
  424. int r;
  425. if ((int)(wait_until - deadline) > 0)
  426. wait_until = deadline;
  427. dhcp_build(DHCP_REQUEST, my_mac, xid,
  428. get_ticks_ms() - start, yiaddr, server);
  429. if (net_udp_send_from(zero_ip, bcast_mac, bcast_ip,
  430. DHCP_CLIENT_PORT, DHCP_SERVER_PORT,
  431. msg, DHCP_MSG_LEN) != 0) {
  432. netdev_release();
  433. return -3; /* TX failed (NIC present) */
  434. }
  435. r = dhcp_wait(nd, xid, my_mac, wait_until, DHCP_ACK,
  436. yiaddr, mask, gw, dns, &lease, server);
  437. if (r == 0)
  438. break;
  439. if (r == -2) { /* NAK: start over */
  440. netdev_release();
  441. io_putstr("dhcp: NAK, retrying\n");
  442. return dhcp_run(timeout_ms);
  443. }
  444. if ((int)(get_ticks_ms() - deadline) >= 0)
  445. break;
  446. }
  447. if (tries == 3) {
  448. netdev_release();
  449. io_putstr("dhcp: no ACK (rx ");
  450. put_dec(dhcp_rx68);
  451. io_putstr("), keeping ");
  452. put_ip(current);
  453. io_putchar('\n');
  454. dhcp_dump_stats(xid);
  455. return -1;
  456. }
  457. net_configure(yiaddr, mask, gw);
  458. if (dns[0] || dns[1] || dns[2] || dns[3])
  459. net_set_dns(dns);
  460. have_info = 1;
  461. {
  462. unsigned int i;
  463. for (i = 0; i < 4; i++)
  464. last_server[i] = server[i];
  465. }
  466. last_lease = lease;
  467. io_putstr("dhcp: ack ");
  468. put_ip(yiaddr);
  469. io_putstr("/");
  470. put_ip(mask);
  471. io_putstr(" gw ");
  472. put_ip(gw);
  473. if (dns[0] || dns[1] || dns[2] || dns[3]) {
  474. io_putstr(" dns ");
  475. put_ip(dns);
  476. }
  477. io_putstr(" lease ");
  478. put_dec(lease);
  479. io_putstr("s\n");
  480. netdev_release();
  481. return 0;
  482. }
  483. int dhcp_last_info(unsigned char server[4], unsigned int *lease_s)
  484. {
  485. unsigned int i;
  486. if (!have_info)
  487. return 0;
  488. for (i = 0; i < 4; i++)
  489. server[i] = last_server[i];
  490. *lease_s = last_lease;
  491. return 1;
  492. }