dhcpclient.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <ip.h>
  12. #include "dhcp.h"
  13. void bootpdump(uint8_t *p, int n);
  14. void dhcpinit(void);
  15. void dhcprecv(void);
  16. void dhcpsend(int);
  17. void myfatal(char *fmt, ...);
  18. int openlisten(char*);
  19. uint8_t *optaddaddr(uint8_t*, int, uint8_t*);
  20. uint8_t *optaddbyte(uint8_t*, int, int);
  21. uint8_t *optadd(uint8_t*, int, void*, int);
  22. uint8_t *optaddulong(uint8_t*, int, uint32_t);
  23. uint8_t *optget(Bootp*, int, int);
  24. int optgetaddr(Bootp*, int, uint8_t*);
  25. int optgetbyte(Bootp*, int);
  26. uint32_t optgetulong(Bootp*, int);
  27. Bootp *parse(uint8_t*, int);
  28. void stdinthread(void*);
  29. uint32_t thread(void(*f)(void*), void *a);
  30. void timerthread(void*);
  31. void usage(void);
  32. struct {
  33. QLock lk;
  34. int state;
  35. int fd;
  36. uint32_t xid;
  37. uint32_t starttime;
  38. char cid[100];
  39. char sname[64];
  40. uint8_t server[IPaddrlen]; /* server IP address */
  41. uint8_t client[IPaddrlen]; /* client IP address */
  42. uint8_t mask[IPaddrlen]; /* client mask */
  43. uint32_t lease; /* lease time */
  44. uint32_t resend; /* number of resends for current state */
  45. uint32_t timeout; /* time to timeout - seconds */
  46. } dhcp;
  47. char net[64];
  48. char optmagic[4] = { 0x63, 0x82, 0x53, 0x63 };
  49. void
  50. main(int argc, char *argv[])
  51. {
  52. char *p;
  53. setnetmtpt(net, sizeof(net), nil);
  54. ARGBEGIN{
  55. case 'x':
  56. p = ARGF();
  57. if(p == nil)
  58. usage();
  59. setnetmtpt(net, sizeof(net), p);
  60. }ARGEND;
  61. fmtinstall('E', eipfmt);
  62. fmtinstall('I', eipfmt);
  63. fmtinstall('V', eipfmt);
  64. dhcpinit();
  65. rfork(RFNOTEG|RFREND);
  66. thread(timerthread, 0);
  67. thread(stdinthread, 0);
  68. qlock(&dhcp.lk);
  69. dhcp.starttime = time(0);
  70. dhcp.fd = openlisten(net);
  71. dhcpsend(Discover);
  72. dhcp.state = Sselecting;
  73. dhcp.resend = 0;
  74. dhcp.timeout = 4;
  75. while(dhcp.state != Sbound)
  76. dhcprecv();
  77. /* allows other clients on this machine */
  78. close(dhcp.fd);
  79. dhcp.fd = -1;
  80. print("ip=%I\n", dhcp.client);
  81. print("mask=%I\n", dhcp.mask);
  82. print("end\n");
  83. /* keep lease alive */
  84. for(;;) {
  85. //fprint(2, "got lease for %d\n", dhcp.lease);
  86. qunlock(&dhcp.lk);
  87. sleep(dhcp.lease*500); /* wait half of lease time */
  88. qlock(&dhcp.lk);
  89. //fprint(2, "try renue\n", dhcp.lease);
  90. dhcp.starttime = time(0);
  91. dhcp.fd = openlisten(net);
  92. dhcp.xid = time(0)*getpid();
  93. dhcpsend(Request);
  94. dhcp.state = Srenewing;
  95. dhcp.resend = 0;
  96. dhcp.timeout = 1;
  97. while(dhcp.state != Sbound)
  98. dhcprecv();
  99. /* allows other clients on this machine */
  100. close(dhcp.fd);
  101. dhcp.fd = -1;
  102. }
  103. }
  104. void
  105. usage(void)
  106. {
  107. fprint(2, "usage: %s [-x netextension]\n", argv0);
  108. exits("usage");
  109. }
  110. void
  111. timerthread(void* v)
  112. {
  113. for(;;) {
  114. sleep(1000);
  115. qlock(&dhcp.lk);
  116. if(--dhcp.timeout > 0) {
  117. qunlock(&dhcp.lk);
  118. continue;
  119. }
  120. switch(dhcp.state) {
  121. default:
  122. myfatal("timerthread: unknown state %d", dhcp.state);
  123. case Sinit:
  124. break;
  125. case Sselecting:
  126. dhcpsend(Discover);
  127. dhcp.timeout = 4;
  128. dhcp.resend++;
  129. if(dhcp.resend>5)
  130. myfatal("dhcp: giving up: selecting");
  131. break;
  132. case Srequesting:
  133. dhcpsend(Request);
  134. dhcp.timeout = 4;
  135. dhcp.resend++;
  136. if(dhcp.resend>5)
  137. myfatal("dhcp: giving up: requesting");
  138. break;
  139. case Srenewing:
  140. dhcpsend(Request);
  141. dhcp.timeout = 1;
  142. dhcp.resend++;
  143. if(dhcp.resend>3) {
  144. dhcp.state = Srebinding;
  145. dhcp.resend = 0;
  146. }
  147. break;
  148. case Srebinding:
  149. dhcpsend(Request);
  150. dhcp.timeout = 4;
  151. dhcp.resend++;
  152. if(dhcp.resend>5)
  153. myfatal("dhcp: giving up: rebinding");
  154. break;
  155. case Sbound:
  156. break;
  157. }
  158. qunlock(&dhcp.lk);
  159. }
  160. }
  161. void
  162. stdinthread(void* v)
  163. {
  164. uint8_t buf[100];
  165. int n;
  166. for(;;) {
  167. n = read(0, buf, sizeof(buf));
  168. if(n <= 0)
  169. break;
  170. }
  171. /* shutdown cleanly */
  172. qlock(&dhcp.lk);
  173. if(dhcp.client) {
  174. if(dhcp.fd < 0)
  175. dhcp.fd = openlisten(net);
  176. dhcpsend(Release);
  177. }
  178. qunlock(&dhcp.lk);
  179. postnote(PNGROUP, getpid(), "die");
  180. exits(0);
  181. }
  182. void
  183. dhcpinit(void)
  184. {
  185. int fd;
  186. dhcp.state = Sinit;
  187. dhcp.timeout = 4;
  188. fd = open("/dev/random", 0);
  189. if(fd >= 0) {
  190. read(fd, &dhcp.xid, sizeof(dhcp.xid));
  191. close(fd);
  192. } else
  193. dhcp.xid = time(0)*getpid();
  194. srand(dhcp.xid);
  195. sprint(dhcp.cid, "%s.%d", getenv("sysname"), getpid());
  196. }
  197. void
  198. dhcpsend(int type)
  199. {
  200. int n;
  201. uint8_t *p;
  202. Bootp bp;
  203. Udphdr *up;
  204. memset(&bp, 0, sizeof bp);
  205. up = (Udphdr*)bp.udphdr;
  206. hnputs(up->rport, 67);
  207. bp.op = Bootrequest;
  208. hnputl(bp.xid, dhcp.xid);
  209. hnputs(bp.secs, time(0) - dhcp.starttime);
  210. hnputs(bp.flags, Fbroadcast); /* reply must be broadcast */
  211. memmove(bp.optmagic, optmagic, 4);
  212. p = bp.optdata;
  213. p = optaddbyte(p, ODtype, type);
  214. p = optadd(p, ODclientid, dhcp.cid, strlen(dhcp.cid));
  215. switch(type) {
  216. default:
  217. myfatal("dhcpsend: unknown message type: %d", type);
  218. case Discover:
  219. ipmove(up->raddr, IPv4bcast); /* broadcast */
  220. break;
  221. case Request:
  222. if(dhcp.state == Sbound || dhcp.state == Srenewing)
  223. ipmove(up->raddr, dhcp.server);
  224. else
  225. ipmove(up->raddr, IPv4bcast); /* broadcast */
  226. p = optaddulong(p, ODlease, dhcp.lease);
  227. if(dhcp.state == Sselecting || dhcp.state == Srequesting) {
  228. p = optaddaddr(p, ODipaddr, dhcp.client); /* mistake?? */
  229. p = optaddaddr(p, ODserverid, dhcp.server);
  230. } else
  231. v6tov4(bp.ciaddr, dhcp.client);
  232. break;
  233. case Release:
  234. ipmove(up->raddr, dhcp.server);
  235. v6tov4(bp.ciaddr, dhcp.client);
  236. p = optaddaddr(p, ODipaddr, dhcp.client);
  237. p = optaddaddr(p, ODserverid, dhcp.server);
  238. break;
  239. }
  240. *p++ = OBend;
  241. n = p - (uint8_t*)&bp;
  242. if(write(dhcp.fd, &bp, n) != n)
  243. myfatal("dhcpsend: write failed: %r");
  244. }
  245. void
  246. dhcprecv(void)
  247. {
  248. uint8_t buf[2000];
  249. Bootp *bp;
  250. int n, type;
  251. uint32_t lease;
  252. uint8_t mask[IPaddrlen];
  253. qunlock(&dhcp.lk);
  254. n = read(dhcp.fd, buf, sizeof(buf));
  255. qlock(&dhcp.lk);
  256. if(n <= 0)
  257. myfatal("dhcprecv: bad read: %r");
  258. bp = parse(buf, n);
  259. if(bp == 0)
  260. return;
  261. if(1) {
  262. fprint(2, "recved\n");
  263. bootpdump(buf, n);
  264. }
  265. type = optgetbyte(bp, ODtype);
  266. switch(type) {
  267. default:
  268. fprint(2, "dhcprecv: unknown type: %d\n", type);
  269. break;
  270. case Offer:
  271. if(dhcp.state != Sselecting)
  272. break;
  273. lease = optgetulong(bp, ODlease);
  274. if(lease == 0)
  275. myfatal("bad lease");
  276. if(!optgetaddr(bp, OBmask, mask))
  277. memset(mask, 0xff, sizeof(mask));
  278. v4tov6(dhcp.client, bp->yiaddr);
  279. if(!optgetaddr(bp, ODserverid, dhcp.server)) {
  280. fprint(2, "dhcprecv: Offer from server with invalid serverid\n");
  281. break;
  282. }
  283. dhcp.lease = lease;
  284. ipmove(dhcp.mask, mask);
  285. memmove(dhcp.sname, bp->sname, sizeof(dhcp.sname));
  286. dhcp.sname[sizeof(dhcp.sname)-1] = 0;
  287. dhcpsend(Request);
  288. dhcp.state = Srequesting;
  289. dhcp.resend = 0;
  290. dhcp.timeout = 4;
  291. break;
  292. case Ack:
  293. if(dhcp.state != Srequesting)
  294. if(dhcp.state != Srenewing)
  295. if(dhcp.state != Srebinding)
  296. break;
  297. lease = optgetulong(bp, ODlease);
  298. if(lease == 0)
  299. myfatal("bad lease");
  300. if(!optgetaddr(bp, OBmask, mask))
  301. memset(mask, 0xff, sizeof(mask));
  302. v4tov6(dhcp.client, bp->yiaddr);
  303. dhcp.lease = lease;
  304. ipmove(dhcp.mask, mask);
  305. dhcp.state = Sbound;
  306. break;
  307. case Nak:
  308. myfatal("recved nak");
  309. break;
  310. }
  311. }
  312. int
  313. openlisten(char *net)
  314. {
  315. int n, fd, cfd;
  316. char data[128], devdir[40];
  317. // sprint(data, "%s/udp!*!bootpc", net);
  318. sprint(data, "%s/udp!*!68", net);
  319. for(n = 0; ; n++) {
  320. cfd = announce(data, devdir);
  321. if(cfd >= 0)
  322. break;
  323. /* might be another client - wait and try again */
  324. fprint(2, "dhcpclient: can't announce %s: %r", data);
  325. sleep(1000);
  326. if(n > 10)
  327. myfatal("can't announce: giving up: %r");
  328. }
  329. if(fprint(cfd, "headers") < 0)
  330. myfatal("can't set header mode: %r");
  331. sprint(data, "%s/data", devdir);
  332. fd = open(data, ORDWR);
  333. if(fd < 0)
  334. myfatal("open %s: %r", data);
  335. close(cfd);
  336. return fd;
  337. }
  338. uint8_t*
  339. optadd(uint8_t *p, int op, void *d, int n)
  340. {
  341. p[0] = op;
  342. p[1] = n;
  343. memmove(p+2, d, n);
  344. return p+n+2;
  345. }
  346. uint8_t*
  347. optaddbyte(uint8_t *p, int op, int b)
  348. {
  349. p[0] = op;
  350. p[1] = 1;
  351. p[2] = b;
  352. return p+3;
  353. }
  354. uint8_t*
  355. optaddulong(uint8_t *p, int op, uint32_t x)
  356. {
  357. p[0] = op;
  358. p[1] = 4;
  359. hnputl(p+2, x);
  360. return p+6;
  361. }
  362. uint8_t *
  363. optaddaddr(uint8_t *p, int op, uint8_t *ip)
  364. {
  365. p[0] = op;
  366. p[1] = 4;
  367. v6tov4(p+2, ip);
  368. return p+6;
  369. }
  370. uint8_t*
  371. optget(Bootp *bp, int op, int n)
  372. {
  373. int len, code;
  374. uint8_t *p;
  375. p = bp->optdata;
  376. for(;;) {
  377. code = *p++;
  378. if(code == OBpad)
  379. continue;
  380. if(code == OBend)
  381. return 0;
  382. len = *p++;
  383. if(code != op) {
  384. p += len;
  385. continue;
  386. }
  387. if(n && n != len)
  388. return 0;
  389. return p;
  390. }
  391. }
  392. int
  393. optgetbyte(Bootp *bp, int op)
  394. {
  395. uint8_t *p;
  396. p = optget(bp, op, 1);
  397. if(p == 0)
  398. return 0;
  399. return *p;
  400. }
  401. uint32_t
  402. optgetulong(Bootp *bp, int op)
  403. {
  404. uint8_t *p;
  405. p = optget(bp, op, 4);
  406. if(p == 0)
  407. return 0;
  408. return nhgetl(p);
  409. }
  410. int
  411. optgetaddr(Bootp *bp, int op, uint8_t *ip)
  412. {
  413. uint8_t *p;
  414. p = optget(bp, op, 4);
  415. if(p == 0)
  416. return 0;
  417. v4tov6(ip, p);
  418. return 1;
  419. }
  420. /* make sure packet looks ok */
  421. Bootp *
  422. parse(uint8_t *p, int n)
  423. {
  424. int len, code;
  425. Bootp *bp;
  426. bp = (Bootp*)p;
  427. if(n < bp->optmagic - p) {
  428. fprint(2, "dhcpclient: parse: short bootp packet");
  429. return 0;
  430. }
  431. if(dhcp.xid != nhgetl(bp->xid)) {
  432. fprint(2, "dhcpclient: parse: bad xid: got %ux expected %lux\n",
  433. nhgetl(bp->xid), dhcp.xid);
  434. return 0;
  435. }
  436. if(bp->op != Bootreply) {
  437. fprint(2, "dhcpclient: parse: bad op\n");
  438. return 0;
  439. }
  440. n -= bp->optmagic - p;
  441. p = bp->optmagic;
  442. if(n < 4) {
  443. fprint(2, "dhcpclient: parse: not option data");
  444. return 0;
  445. }
  446. if(memcmp(optmagic, p, 4) != 0) {
  447. fprint(2, "dhcpclient: parse: bad opt magic %ux %ux %ux %ux\n",
  448. p[0], p[1], p[2], p[3]);
  449. return 0;
  450. }
  451. p += 4;
  452. n -= 4;
  453. while(n>0) {
  454. code = *p++;
  455. n--;
  456. if(code == OBpad)
  457. continue;
  458. if(code == OBend)
  459. return bp;
  460. if(n == 0) {
  461. fprint(2, "dhcpclient: parse: bad option: %d", code);
  462. return 0;
  463. }
  464. len = *p++;
  465. n--;
  466. if(len > n) {
  467. fprint(2, "dhcpclient: parse: bad option: %d", code);
  468. return 0;
  469. }
  470. p += len;
  471. n -= len;
  472. }
  473. /* fix up nonstandard packets */
  474. /* assume there is space */
  475. *p = OBend;
  476. return bp;
  477. }
  478. void
  479. bootpdump(uint8_t *p, int n)
  480. {
  481. int len, i, code;
  482. Bootp *bp;
  483. Udphdr *up;
  484. bp = (Bootp*)p;
  485. up = (Udphdr*)bp->udphdr;
  486. if(n < bp->optmagic - p) {
  487. fprint(2, "dhcpclient: short bootp packet");
  488. return;
  489. }
  490. fprint(2, "laddr=%I lport=%d raddr=%I rport=%d\n", up->laddr,
  491. nhgets(up->lport), up->raddr, nhgets(up->rport));
  492. fprint(2, "op=%d htype=%d hlen=%d hops=%d\n", bp->op, bp->htype,
  493. bp->hlen, bp->hops);
  494. fprint(2, "xid=%ux secs=%d flags=%ux\n", nhgetl(bp->xid),
  495. nhgets(bp->secs), nhgets(bp->flags));
  496. fprint(2, "ciaddr=%V yiaddr=%V siaddr=%V giaddr=%V\n",
  497. bp->ciaddr, bp->yiaddr, bp->siaddr, bp->giaddr);
  498. fprint(2, "chaddr=");
  499. for(i=0; i<16; i++)
  500. fprint(2, "%ux ", bp->chaddr[i]);
  501. fprint(2, "\n");
  502. fprint(2, "sname=%s\n", bp->sname);
  503. fprint(2, "file = %s\n", bp->file);
  504. n -= bp->optmagic - p;
  505. p = bp->optmagic;
  506. if(n < 4)
  507. return;
  508. if(memcmp(optmagic, p, 4) != 0)
  509. fprint(2, "dhcpclient: bad opt magic %ux %ux %ux %ux\n",
  510. p[0], p[1], p[2], p[3]);
  511. p += 4;
  512. n -= 4;
  513. while(n>0) {
  514. code = *p++;
  515. n--;
  516. if(code == OBpad)
  517. continue;
  518. if(code == OBend)
  519. break;
  520. if(n == 0) {
  521. fprint(2, " bad option: %d", code);
  522. return;
  523. }
  524. len = *p++;
  525. n--;
  526. if(len > n) {
  527. fprint(2, " bad option: %d", code);
  528. return;
  529. }
  530. switch(code) {
  531. default:
  532. fprint(2, "unknown option %d\n", code);
  533. for(i = 0; i<len; i++)
  534. fprint(2, "%ux ", p[i]);
  535. case ODtype:
  536. fprint(2, "DHCP type %d\n", p[0]);
  537. break;
  538. case ODclientid:
  539. fprint(2, "client id=");
  540. for(i = 0; i<len; i++)
  541. fprint(2, "%ux ", p[i]);
  542. fprint(2, "\n");
  543. break;
  544. case ODlease:
  545. fprint(2, "lease=%d\n", nhgetl(p));
  546. break;
  547. case ODserverid:
  548. fprint(2, "server id=%V\n", p);
  549. break;
  550. case OBmask:
  551. fprint(2, "mask=%V\n", p);
  552. break;
  553. case OBrouter:
  554. fprint(2, "router=%V\n", p);
  555. break;
  556. }
  557. p += len;
  558. n -= len;
  559. }
  560. }
  561. uint32_t
  562. thread(void(*f)(void*), void *a)
  563. {
  564. int pid;
  565. pid = rfork(RFNOWAIT|RFMEM|RFPROC);
  566. if(pid < 0)
  567. myfatal("rfork failed: %r");
  568. if(pid != 0)
  569. return pid;
  570. (*f)(a);
  571. return 0; /* never reaches here */
  572. }
  573. void
  574. myfatal(char *fmt, ...)
  575. {
  576. char buf[1024];
  577. va_list arg;
  578. va_start(arg, fmt);
  579. vseprint(buf, buf+sizeof(buf), fmt, arg);
  580. va_end(arg);
  581. fprint(2, "%s: %s\n", argv0, buf);
  582. postnote(PNGROUP, getpid(), "die");
  583. exits(buf);
  584. }