dhcp.c 14 KB

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