ipconfig.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ip.h>
  4. #include "dhcp.h"
  5. int noconfig;
  6. int debug;
  7. int dodhcp;
  8. int nip;
  9. int myifc = -1;
  10. int plan9 = 1;
  11. int beprimary;
  12. int nodhcpwatch;
  13. Ipifc *ifc;
  14. // possible verbs
  15. enum
  16. {
  17. Vadd,
  18. Vremove,
  19. Vunbind,
  20. Vether,
  21. };
  22. struct {
  23. // locally generated
  24. char *type;
  25. char *dev;
  26. char mpoint[32];
  27. int cfd; // ifc control channel
  28. int dfd; // ifc data channel (for ppp)
  29. char *cputype;
  30. uchar hwa[32]; // hardware address
  31. int hwatype;
  32. int hwalen;
  33. uchar cid[32];
  34. int cidlen;
  35. char *baud;
  36. // learned info
  37. uchar gaddr[IPaddrlen];
  38. uchar laddr[IPaddrlen];
  39. uchar mask[IPaddrlen];
  40. uchar raddr[IPaddrlen];
  41. uchar dns[2*IPaddrlen];
  42. uchar fs[2*IPaddrlen];
  43. uchar auth[2*IPaddrlen];
  44. uchar ntp[IPaddrlen];
  45. int mtu;
  46. // dhcp specific
  47. int state;
  48. int fd;
  49. ulong xid;
  50. ulong starttime;
  51. char sname[64];
  52. char hostname[32];
  53. char domainname[64];
  54. uchar server[IPaddrlen]; /* server IP address */
  55. ulong offered; /* offered lease time */
  56. ulong lease; /* lease time */
  57. ulong resend; /* number of resends for current state */
  58. ulong timeout; /* time to timeout - seconds */
  59. } conf;
  60. void adddefroute(char*, uchar*);
  61. void binddevice(void);
  62. void controldevice(void);
  63. void bootprequest(void);
  64. void dhcprecv(void);
  65. void dhcpsend(int);
  66. int dhcptimer(void);
  67. void dhcpquery(int, int);
  68. void dhcpwatch(int);
  69. int getndb(void);
  70. int ipconfig(void);
  71. void lookforip(char*);
  72. uchar *optadd(uchar*, int, void*, int);
  73. uchar *optaddbyte(uchar*, int, int);
  74. uchar *optaddulong(uchar*, int, ulong);
  75. uchar *optaddaddr(uchar*, int, uchar*);
  76. uchar *optaddstr(uchar*, int, char*);
  77. uchar *optaddvec(uchar*, int, uchar*, int);
  78. uchar *optget(uchar*, int, int*);
  79. int optgetbyte(uchar*, int);
  80. ulong optgetulong(uchar*, int);
  81. int optgetaddr(uchar*, int, uchar*);
  82. int optgetaddrs(uchar*, int, uchar*, int);
  83. int optgetstr(uchar*, int, char*, int);
  84. int optgetvec(uchar*, int, uchar*, int);
  85. void mkclientid(void);
  86. int nipifcs(char*);
  87. int openlisten(void);
  88. int parseoptions(uchar *p, int n);
  89. Bootp *parsebootp(uchar*, int);
  90. void putndb(void);
  91. void tweakservers(void);
  92. void writendb(char*, int, int);
  93. void usage(void);
  94. int validip(uchar*);
  95. int parseverb(char*);
  96. void doadd(int);
  97. void doremove(void);
  98. void dounbind(void);
  99. char optmagic[4] = { 0x63, 0x82, 0x53, 0x63 };
  100. #define DEBUG if(debug)print
  101. typedef struct Ctl Ctl;
  102. struct Ctl
  103. {
  104. Ctl *next;
  105. char *ctl;
  106. };
  107. Ctl *firstctl, **ctll;
  108. void
  109. usage(void)
  110. {
  111. fprint(2, "usage: %s [-ndDrGX] [-x netmtpt] [-m mtu] [-b baud] [-g gateway] [-h hostname] [-c control-string]* type device [verb] [localaddr [mask [remoteaddr [fsaddr [authaddr]]]]]\n", argv0);
  112. exits("usage");
  113. }
  114. void
  115. main(int argc, char **argv)
  116. {
  117. char *p;
  118. int retry, verb, action;
  119. Ctl *cp;
  120. srand(truerand());
  121. fmtinstall('E', eipfmt);
  122. fmtinstall('I', eipfmt);
  123. fmtinstall('M', eipfmt);
  124. fmtinstall('V', eipfmt);
  125. setnetmtpt(conf.mpoint, sizeof(conf.mpoint), nil);
  126. conf.cputype = getenv("cputype");
  127. if(conf.cputype == nil)
  128. conf.cputype = "386";
  129. retry = 0;
  130. ctll = &firstctl;
  131. ARGBEGIN {
  132. case 'D':
  133. debug = 1;
  134. break;
  135. case 'G':
  136. plan9 = 0;
  137. break;
  138. case 'P':
  139. beprimary = 1;
  140. break;
  141. case 'b':
  142. p = ARGF();
  143. if(p == nil)
  144. usage();
  145. conf.baud = p;
  146. break;
  147. case 'c':
  148. p = ARGF();
  149. if(p == nil)
  150. usage();
  151. cp = malloc(sizeof(*cp));
  152. if(cp == nil)
  153. sysfatal("%r");
  154. *ctll = cp;
  155. ctll = &cp->next;
  156. cp->next = nil;
  157. cp->ctl = p;
  158. break;
  159. case 'd':
  160. dodhcp = 1;
  161. break;
  162. case 'g':
  163. p = ARGF();
  164. if(p == nil)
  165. usage();
  166. parseip(conf.gaddr, p);
  167. break;
  168. case 'h':
  169. p = ARGF();
  170. if(p == nil)
  171. usage();
  172. snprint(conf.hostname, sizeof(conf.hostname), "%s", p);
  173. break;
  174. case 'n':
  175. noconfig = 1;
  176. break;
  177. case 'm':
  178. p = ARGF();
  179. if(p == nil)
  180. usage();
  181. conf.mtu = atoi(p);
  182. break;
  183. case 'r':
  184. retry = 1;
  185. break;
  186. case 'x':
  187. p = ARGF();
  188. if(p == nil)
  189. usage();
  190. setnetmtpt(conf.mpoint, sizeof(conf.mpoint), p);
  191. break;
  192. case 'X':
  193. nodhcpwatch = 1;
  194. break;
  195. } ARGEND;
  196. // default
  197. conf.type = "ether";
  198. conf.dev = "/net/ether0";
  199. action = Vadd;
  200. // get verb, default is "add"
  201. while(argc > 0){
  202. verb = parseverb(argv[0]);
  203. switch(verb){
  204. case Vadd:
  205. case Vremove:
  206. case Vunbind:
  207. action = verb;
  208. break;
  209. case Vether:
  210. conf.type = argv[0];
  211. if(argc > 1){
  212. conf.dev = argv[1];
  213. argc--, argv++;
  214. }
  215. break;
  216. }
  217. if(verb < 0)
  218. break;
  219. argc--, argv++;
  220. }
  221. // get addresses
  222. switch(argc){
  223. case 5:
  224. parseip(conf.auth, argv[4]);
  225. /* fall through */
  226. case 4:
  227. parseip(conf.fs, argv[3]);
  228. /* fall through */
  229. case 3:
  230. parseip(conf.raddr, argv[2]);
  231. /* fall through */
  232. case 2:
  233. parseipmask(conf.mask, argv[1]);
  234. /* fall through */
  235. case 1:
  236. parseip(conf.laddr, argv[0]);
  237. break;
  238. }
  239. switch(action){
  240. case Vadd:
  241. doadd(retry);
  242. break;
  243. case Vremove:
  244. doremove();
  245. break;
  246. case Vunbind:
  247. dounbind();
  248. break;
  249. }
  250. exits(0);
  251. }
  252. void
  253. doadd(int retry)
  254. {
  255. int tries;
  256. // get number of preexisting interfaces
  257. nip = nipifcs(conf.mpoint);
  258. if(nip == 0)
  259. beprimary = 1;
  260. // get ipifc into name space and condition device for ip
  261. if(!noconfig){
  262. lookforip(conf.mpoint);
  263. controldevice();
  264. binddevice();
  265. }
  266. if(!validip(conf.laddr))
  267. dodhcp = 1;
  268. // run dhcp if we need something
  269. if(dodhcp){
  270. mkclientid();
  271. for(tries = 0; tries < 6; tries++){
  272. dhcpquery(!noconfig, Sselecting);
  273. if(conf.state == Sbound)
  274. break;
  275. sleep(1000);
  276. }
  277. }
  278. if(!validip(conf.laddr)){
  279. if(retry && dodhcp && !noconfig){
  280. fprint(2, "%s: couldn't determine ip address, retrying\n", argv0);
  281. dhcpwatch(1);
  282. return;
  283. } else {
  284. fprint(2, "%s: no success with DHCP\n", argv0);
  285. exits("failed");
  286. }
  287. }
  288. if(!noconfig){
  289. if(ipconfig() < 0)
  290. sysfatal("can't start ip");
  291. if(dodhcp && conf.lease != Lforever)
  292. dhcpwatch(0);
  293. }
  294. // leave everything we've learned somewhere other procs can find it
  295. if(beprimary){
  296. putndb();
  297. tweakservers();
  298. }
  299. }
  300. void
  301. doremove(void)
  302. {
  303. char file[128];
  304. int cfd;
  305. Ipifc *nifc;
  306. Iplifc *lifc;
  307. if(!validip(conf.laddr)){
  308. fprint(2, "%s: remove requires an address\n", argv0);
  309. exits("usage");
  310. }
  311. ifc = readipifc(conf.mpoint, ifc, -1);
  312. for(nifc = ifc; nifc != nil; nifc = nifc->next){
  313. if(strcmp(nifc->dev, conf.dev) != 0)
  314. continue;
  315. for(lifc = nifc->lifc; lifc != nil; lifc = lifc->next){
  316. if(ipcmp(conf.laddr, lifc->ip) != 0)
  317. continue;
  318. if(validip(conf.mask) && ipcmp(conf.mask, lifc->mask) != 0)
  319. continue;
  320. if(validip(conf.raddr) && ipcmp(conf.raddr, lifc->net) != 0)
  321. continue;
  322. snprint(file, sizeof(file), "%s/ipifc/%d/ctl", conf.mpoint, nifc->index);
  323. cfd = open(file, ORDWR);
  324. if(cfd < 0){
  325. fprint(2, "%s: can't open %s: %r\n", argv0, conf.mpoint);
  326. continue;
  327. }
  328. if(fprint(cfd, "remove %I %M", lifc->ip, lifc->mask) < 0)
  329. fprint(2, "%s: can't remove %I %I from %s: %r\n", argv0,
  330. lifc->ip, lifc->mask, file);
  331. }
  332. }
  333. }
  334. void
  335. dounbind(void)
  336. {
  337. Ipifc *nifc;
  338. char file[128];
  339. int cfd;
  340. ifc = readipifc(conf.mpoint, ifc, -1);
  341. for(nifc = ifc; nifc != nil; nifc = nifc->next){
  342. if(strcmp(nifc->dev, conf.dev) == 0){
  343. snprint(file, sizeof(file), "%s/ipifc/%d/ctl", conf.mpoint, nifc->index);
  344. cfd = open(file, ORDWR);
  345. if(cfd < 0){
  346. fprint(2, "%s: can't open %s: %r\n", argv0, conf.mpoint);
  347. break;
  348. }
  349. if(fprint(cfd, "unbind") < 0)
  350. fprint(2, "%s: can't unbind from %s: %r\n", argv0, file);
  351. break;
  352. }
  353. }
  354. }
  355. // set the default route
  356. void
  357. adddefroute(char *mpoint, uchar *gaddr)
  358. {
  359. char buf[256];
  360. int cfd;
  361. sprint(buf, "%s/iproute", mpoint);
  362. cfd = open(buf, ORDWR);
  363. if(cfd < 0)
  364. return;
  365. fprint(cfd, "add 0 0 %I", gaddr);
  366. close(cfd);
  367. }
  368. // create a client id
  369. void
  370. mkclientid(void)
  371. {
  372. if(strcmp(conf.type, "ether") == 0)
  373. if(myetheraddr(conf.hwa, conf.dev) == 0){
  374. conf.hwalen = 6;
  375. conf.hwatype = 1;
  376. conf.cid[0] = conf.hwatype;
  377. memmove(&conf.cid[1], conf.hwa, conf.hwalen);
  378. conf.cidlen = conf.hwalen+1;
  379. } else {
  380. conf.hwatype = -1;
  381. snprint((char*)conf.cid, sizeof(conf.cid), "plan9_%ld.%d", lrand(), getpid());
  382. conf.cidlen = strlen((char*)conf.cid);
  383. }
  384. }
  385. // bind ip into the namespace
  386. void
  387. lookforip(char *net)
  388. {
  389. char proto[64];
  390. snprint(proto, sizeof(proto), "%s/ipifc", net);
  391. if(access(proto, 0) == 0)
  392. return;
  393. sysfatal("no ip stack bound onto %s", net);
  394. }
  395. // send some ctls to a device
  396. void
  397. controldevice(void)
  398. {
  399. char ctlfile[256];
  400. int fd;
  401. Ctl *cp;
  402. if(firstctl == nil)
  403. return;
  404. if(strcmp(conf.type, "ether") == 0)
  405. snprint(ctlfile, sizeof(ctlfile), "%s/clone", conf.dev);
  406. else
  407. return;
  408. fd = open(ctlfile, ORDWR);
  409. if(fd < 0)
  410. sysfatal("can't open %s", ctlfile);
  411. for(cp = firstctl; cp != nil; cp = cp->next){
  412. if(write(fd, cp->ctl, strlen(cp->ctl)) < 0)
  413. sysfatal("ctl message %s: %r", cp->ctl);
  414. seek(fd, 0, 0);
  415. }
  416. }
  417. // bind an ip stack to a device, leave the control channel open
  418. void
  419. binddevice(void)
  420. {
  421. char buf[256];
  422. if(myifc < 0){
  423. // get a new ip interface
  424. snprint(buf, sizeof(buf), "%s/ipifc/clone", conf.mpoint);
  425. conf.cfd = open(buf, ORDWR);
  426. if(conf.cfd < 0)
  427. sysfatal("opening %s/ipifc/clone: %r", conf.mpoint);
  428. // specify the medium as an ethernet, and bind the interface to it
  429. if(fprint(conf.cfd, "bind %s %s", conf.type, conf.dev) < 0)
  430. sysfatal("binding device: %r");
  431. } else {
  432. // open the old interface
  433. snprint(buf, sizeof(buf), "%s/ipifc/%d/ctl", conf.mpoint, myifc);
  434. conf.cfd = open(buf, ORDWR);
  435. if(conf.cfd < 0)
  436. sysfatal("opening %s/ipifc/%d/ctl: %r", conf.mpoint, myifc);
  437. }
  438. }
  439. // add a logical interface to the ip stack
  440. int
  441. ipconfig(void)
  442. {
  443. char buf[256];
  444. int n;
  445. if(!validip(conf.laddr))
  446. return -1;
  447. n = sprint(buf, "add");
  448. n += snprint(buf+n, sizeof(buf)-n, " %I", conf.laddr);
  449. if(!validip(conf.mask))
  450. ipmove(conf.mask, defmask(conf.laddr));
  451. n += snprint(buf+n, sizeof(buf)-n, " %I", conf.mask);
  452. if(validip(conf.raddr)){
  453. n += snprint(buf+n, sizeof(buf)-n, " %I", conf.raddr);
  454. if(conf.mtu != 0)
  455. n += snprint(buf+n, sizeof(buf)-n, " %d", conf.mtu);
  456. }
  457. if(write(conf.cfd, buf, n) < 0){
  458. fprint(2, "ipconfig: write(%s): %r\n", buf);
  459. return -1;
  460. }
  461. if(beprimary && validip(conf.gaddr))
  462. adddefroute(conf.mpoint, conf.gaddr);
  463. return 0;
  464. }
  465. // remove a logical interface to the ip stack
  466. void
  467. ipunconfig(void)
  468. {
  469. char buf[256];
  470. int n;
  471. if(!validip(conf.laddr))
  472. return;
  473. DEBUG("couldn't renew IP lease, releasing %I\n", conf.laddr);
  474. n = sprint(buf, "remove");
  475. n += snprint(buf+n, sizeof(buf)-n, " %I", conf.laddr);
  476. if(!validip(conf.mask))
  477. ipmove(conf.mask, defmask(conf.laddr));
  478. n += snprint(buf+n, sizeof(buf)-n, " %I", conf.mask);
  479. write(conf.cfd, buf, n);
  480. ipmove(conf.laddr, IPnoaddr);
  481. ipmove(conf.raddr, IPnoaddr);
  482. ipmove(conf.mask, IPnoaddr);
  483. // forget configuration info
  484. if(beprimary)
  485. writendb("", 0, 0);
  486. }
  487. void
  488. ding(void*, char *msg)
  489. {
  490. if(strstr(msg, "alarm"))
  491. noted(NCONT);
  492. noted(NDFLT);
  493. }
  494. void
  495. dhcpquery(int needconfig, int startstate)
  496. {
  497. if(needconfig)
  498. fprint(conf.cfd, "add %I %I", IPnoaddr, IPnoaddr);
  499. conf.fd = openlisten();
  500. if(conf.fd < 0){
  501. conf.state = Sinit;
  502. return;
  503. }
  504. notify(ding);
  505. // try dhcp for 10 seconds
  506. conf.xid = lrand();
  507. conf.starttime = time(0);
  508. conf.state = startstate;
  509. switch(startstate){
  510. case Sselecting:
  511. conf.offered = 0;
  512. dhcpsend(Discover);
  513. break;
  514. case Srenewing:
  515. dhcpsend(Request);
  516. break;
  517. default:
  518. sysfatal("internal error 0");
  519. }
  520. conf.resend = 0;
  521. conf.timeout = time(0) + 4;
  522. while(conf.state != Sbound){
  523. dhcprecv();
  524. if(dhcptimer() < 0)
  525. break;
  526. if(time(0) - conf.starttime > 10)
  527. break;
  528. }
  529. close(conf.fd);
  530. if(needconfig)
  531. fprint(conf.cfd, "remove %I %I", IPnoaddr, IPnoaddr);
  532. }
  533. #define HOUR (60*60)
  534. void
  535. dhcpwatch(int needconfig)
  536. {
  537. int secs, s;
  538. ulong t;
  539. if(nodhcpwatch)
  540. return;
  541. switch(rfork(RFPROC|RFFDG|RFNOWAIT|RFNOTEG)){
  542. default:
  543. return;
  544. case 0:
  545. break;
  546. }
  547. // keep trying to renew the lease
  548. for(;;){
  549. if(conf.lease == 0)
  550. secs = 5;
  551. else
  552. secs = conf.lease>>1;
  553. // avoid overflows
  554. for(s = secs; s > 0; s -= t){
  555. if(s > HOUR)
  556. t = HOUR;
  557. else
  558. t = s;
  559. sleep(t*1000);
  560. }
  561. if(conf.lease > 0){
  562. // during boot, the starttime can be bogus so avoid
  563. // spurious ipinconfig's
  564. t = time(0) - conf.starttime;
  565. if(t > (3*secs)/2)
  566. t = secs;
  567. if(t >= conf.lease){
  568. conf.lease = 0;
  569. if(!noconfig){
  570. ipunconfig();
  571. needconfig = 1;
  572. }
  573. } else
  574. conf.lease -= t;
  575. }
  576. dhcpquery(needconfig, needconfig ? Sselecting : Srenewing);
  577. if(needconfig && conf.state == Sbound){
  578. if(ipconfig() < 0)
  579. sysfatal("can't start ip: %r");
  580. needconfig = 0;
  581. // leave everything we've learned somewhere other procs can find it
  582. if(beprimary){
  583. putndb();
  584. tweakservers();
  585. }
  586. }
  587. }
  588. }
  589. int
  590. dhcptimer(void)
  591. {
  592. ulong now;
  593. now = time(0);
  594. if(now < conf.timeout)
  595. return 0;
  596. switch(conf.state) {
  597. default:
  598. sysfatal("dhcptimer: unknown state %d", conf.state);
  599. case Sinit:
  600. break;
  601. case Sselecting:
  602. dhcpsend(Discover);
  603. conf.timeout = now + 4;
  604. conf.resend++;
  605. if(conf.resend>5)
  606. goto err;
  607. break;
  608. case Srequesting:
  609. dhcpsend(Request);
  610. conf.timeout = now + 4;
  611. conf.resend++;
  612. if(conf.resend>5)
  613. goto err;
  614. break;
  615. case Srenewing:
  616. dhcpsend(Request);
  617. conf.timeout = now + 1;
  618. conf.resend++;
  619. if(conf.resend>3) {
  620. conf.state = Srebinding;
  621. conf.resend = 0;
  622. }
  623. break;
  624. case Srebinding:
  625. dhcpsend(Request);
  626. conf.timeout = now + 4;
  627. conf.resend++;
  628. if(conf.resend>5)
  629. goto err;
  630. break;
  631. case Sbound:
  632. break;
  633. }
  634. return 0;
  635. err:
  636. conf.state = Sinit;
  637. return -1;
  638. }
  639. uchar requested[] = {
  640. OBmask, OBrouter, OBdnserver, OBhostname, OBdomainname, OBntpserver,
  641. };
  642. void
  643. dhcpsend(int type)
  644. {
  645. Bootp bp;
  646. uchar *p;
  647. int n;
  648. uchar vendor[64];
  649. OUdphdr *up = (OUdphdr*)bp.udphdr;
  650. memset(&bp, 0, sizeof(bp));
  651. hnputs(up->rport, 67);
  652. bp.op = Bootrequest;
  653. hnputl(bp.xid, conf.xid);
  654. hnputs(bp.secs, time(0)-conf.starttime);
  655. hnputs(bp.flags, 0);
  656. memmove(bp.optmagic, optmagic, 4);
  657. if(conf.hwatype >= 0 && conf.hwalen < sizeof(bp.chaddr)){
  658. memmove(bp.chaddr, conf.hwa, conf.hwalen);
  659. bp.hlen = conf.hwalen;
  660. bp.htype = conf.hwatype;
  661. }
  662. p = bp.optdata;
  663. p = optaddbyte(p, ODtype, type);
  664. p = optadd(p, ODclientid, conf.cid, conf.cidlen);
  665. switch(type) {
  666. default:
  667. sysfatal("dhcpsend: unknown message type: %d", type);
  668. case Discover:
  669. ipmove(up->raddr, IPv4bcast); // broadcast
  670. if(*conf.hostname)
  671. p = optaddstr(p, OBhostname, conf.hostname);
  672. if(plan9){
  673. n = snprint((char*)vendor, sizeof(vendor), "plan9_%s", conf.cputype);
  674. p = optaddvec(p, ODvendorclass, vendor, n);
  675. }
  676. p = optaddvec(p, ODparams, requested, sizeof(requested));
  677. if(validip(conf.laddr))
  678. p = optaddaddr(p, ODipaddr, conf.laddr);
  679. break;
  680. case Request:
  681. switch(conf.state){
  682. case Srenewing:
  683. ipmove(up->raddr, conf.server);
  684. v6tov4(bp.ciaddr, conf.laddr);
  685. break;
  686. case Srebinding:
  687. ipmove(up->raddr, IPv4bcast); // broadcast
  688. v6tov4(bp.ciaddr, conf.laddr);
  689. break;
  690. case Srequesting:
  691. ipmove(up->raddr, IPv4bcast); // broadcast
  692. p = optaddaddr(p, ODipaddr, conf.laddr);
  693. p = optaddaddr(p, ODserverid, conf.server);
  694. break;
  695. }
  696. p = optaddulong(p, ODlease, conf.offered);
  697. if(plan9){
  698. n = snprint((char*)vendor, sizeof(vendor), "plan9_%s", conf.cputype);
  699. p = optaddvec(p, ODvendorclass, vendor, n);
  700. }
  701. p = optaddvec(p, ODparams, requested, sizeof(requested));
  702. if(*conf.hostname)
  703. p = optaddstr(p, OBhostname, conf.hostname);
  704. break;
  705. case Release:
  706. ipmove(up->raddr, conf.server);
  707. v6tov4(bp.ciaddr, conf.laddr);
  708. p = optaddaddr(p, ODipaddr, conf.laddr);
  709. p = optaddaddr(p, ODserverid, conf.server);
  710. break;
  711. }
  712. *p++ = OBend;
  713. n = p - (uchar*)&bp;
  714. USED(n);
  715. /*
  716. * We use a maximum size DHCP packet to survive the
  717. * All_Aboard NAT package from Internet Share. It
  718. * always replies to DHCP requests with a packet of the
  719. * same size, so if the request is too short the reply
  720. * is truncated.
  721. */
  722. if(write(conf.fd, &bp, sizeof(bp)) != sizeof(bp))
  723. fprint(2, "dhcpsend: write failed: %r\n");
  724. }
  725. void
  726. dhcprecv(void)
  727. {
  728. uchar buf[8000];
  729. Bootp *bp;
  730. int i, n, type;
  731. ulong lease;
  732. char err[ERRMAX];
  733. uchar vopts[256];
  734. alarm(1000);
  735. n = read(conf.fd, buf, sizeof(buf));
  736. alarm(0);
  737. if(n < 0){
  738. errstr(err, sizeof err);
  739. if(strstr(err, "interrupt") == nil)
  740. fprint(2, "ipconfig: bad read: %s\n", err);
  741. else
  742. DEBUG("read timed out\n");
  743. return;
  744. }
  745. bp = parsebootp(buf, n);
  746. if(bp == 0) {
  747. DEBUG("parsebootp failed: dropping packet\n");
  748. return;
  749. }
  750. type = optgetbyte(bp->optdata, ODtype);
  751. switch(type) {
  752. default:
  753. fprint(2, "%s: unknown type: %d\n", argv0, type);
  754. break;
  755. case Offer:
  756. DEBUG("got offer from %V ", bp->siaddr);
  757. if(conf.state != Sselecting){
  758. DEBUG("\n");
  759. break;
  760. }
  761. lease = optgetulong(bp->optdata, ODlease);
  762. if(lease == 0){
  763. /*
  764. * The All_Aboard NAT package from Internet Share doesn't
  765. * give a lease time, so we have to assume one.
  766. */
  767. fprint(2, "%s: Offer with %lud lease, using %d\n", argv0, lease, MinLease);
  768. lease = MinLease;
  769. }
  770. DEBUG("lease %lud ", lease);
  771. if(!optgetaddr(bp->optdata, ODserverid, conf.server)) {
  772. fprint(2, "%s: Offer from server with invalid serverid\n", argv0);
  773. break;
  774. }
  775. v4tov6(conf.laddr, bp->yiaddr);
  776. memmove(conf.sname, bp->sname, sizeof(conf.sname));
  777. conf.sname[sizeof(conf.sname)-1] = 0;
  778. DEBUG("server %I %s\n", conf.server, conf.sname);
  779. conf.offered = lease;
  780. conf.state = Srequesting;
  781. dhcpsend(Request);
  782. conf.resend = 0;
  783. conf.timeout = time(0) + 4;
  784. break;
  785. case Ack:
  786. DEBUG("got ack from %V ", bp->siaddr);
  787. if(conf.state != Srequesting)
  788. if(conf.state != Srenewing)
  789. if(conf.state != Srebinding)
  790. break;
  791. // ignore a bad lease
  792. lease = optgetulong(bp->optdata, ODlease);
  793. if(lease == 0){
  794. /*
  795. * The All_Aboard NAT package from Internet Share doesn't
  796. * give a lease time, so we have to assume one.
  797. */
  798. fprint(2, "%s: Ack with %lud lease, using %d\n", argv0, lease, MinLease);
  799. lease = MinLease;
  800. }
  801. DEBUG("lease %lud ", lease);
  802. // address and mask
  803. v4tov6(conf.laddr, bp->yiaddr);
  804. if(!optgetaddr(bp->optdata, OBmask, conf.mask))
  805. ipmove(conf.mask, IPnoaddr);
  806. DEBUG("addr %I mask %M ", conf.laddr, conf.mask);
  807. // get a router address either from the router option
  808. // or from the router that forwarded the dhcp packet
  809. if(optgetaddr(bp->optdata, OBrouter, conf.gaddr)){
  810. DEBUG("router %I ", conf.gaddr);
  811. } else {
  812. if(memcmp(bp->giaddr, IPnoaddr+IPv4off, IPv4addrlen) != 0){
  813. v4tov6(conf.gaddr, bp->giaddr);
  814. DEBUG("giaddr %I ", conf.gaddr);
  815. }
  816. }
  817. // get dns servers
  818. memset(conf.dns, 0, sizeof(conf.dns));
  819. n = optgetaddrs(bp->optdata, OBdnserver, conf.dns,
  820. sizeof(conf.dns)/IPaddrlen);
  821. for(i = 0; i < n; i++)
  822. DEBUG("dns %I ", conf.dns+i*IPaddrlen);
  823. // get ntp servers
  824. memset(conf.ntp, 0, sizeof(conf.ntp));
  825. n = optgetaddrs(bp->optdata, OBntpserver, conf.ntp,
  826. sizeof(conf.ntp)/IPaddrlen);
  827. for(i = 0; i < n; i++)
  828. DEBUG("ntp %I ", conf.ntp+i*IPaddrlen);
  829. // get names
  830. optgetstr(bp->optdata, OBhostname, conf.hostname, sizeof(conf.hostname));
  831. optgetstr(bp->optdata, OBdomainname, conf.domainname, sizeof(conf.domainname));
  832. // get plan9 specific options
  833. n = optgetvec(bp->optdata, OBvendorinfo, vopts, sizeof(vopts)-1);
  834. if(n > 0){
  835. if(parseoptions(vopts, n) == 0){
  836. n = optgetaddrs(vopts, OP9fs, conf.fs, 2);
  837. for(i = 0; i < n; i++)
  838. DEBUG("fs %I ", conf.fs+i*IPaddrlen);
  839. n = optgetaddrs(vopts, OP9auth, conf.auth, 2);
  840. for(i = 0; i < n; i++)
  841. DEBUG("auth %I ", conf.auth+i*IPaddrlen);
  842. }
  843. }
  844. conf.lease = lease;
  845. conf.state = Sbound;
  846. DEBUG("server %I %s\n", conf.server, conf.sname);
  847. break;
  848. case Nak:
  849. conf.state = Sinit;
  850. fprint(2, "%s: recved dhcpnak on %s\n", argv0, conf.mpoint);
  851. break;
  852. }
  853. }
  854. int
  855. openlisten()
  856. {
  857. int fd, cfd;
  858. char data[128];
  859. char devdir[40];
  860. int n;
  861. if(validip(conf.laddr)
  862. && (conf.state == Srenewing || conf.state == Srebinding))
  863. sprint(data, "%s/udp!%I!68", conf.mpoint, conf.laddr);
  864. else
  865. sprint(data, "%s/udp!*!68", conf.mpoint);
  866. for(n=0;;n++) {
  867. cfd = announce(data, devdir);
  868. if(cfd >= 0)
  869. break;
  870. if(!noconfig)
  871. sysfatal("can't announce for dhcp: %r");
  872. // might be another client - wait and try again
  873. fprint(2, "%s: can't announce: %r\n", argv0);
  874. sleep((nrand(10)+1)*1000);
  875. if(n > 10)
  876. return -1;
  877. }
  878. if(fprint(cfd, "headers") < 0)
  879. sysfatal("can't set header mode: %r");
  880. fprint(cfd, "oldheaders");
  881. sprint(data, "%s/data", devdir);
  882. fd = open(data, ORDWR);
  883. if(fd < 0)
  884. sysfatal("open udp data: %r");
  885. close(cfd);
  886. return fd;
  887. }
  888. uchar*
  889. optadd(uchar *p, int op, void *d, int n)
  890. {
  891. p[0] = op;
  892. p[1] = n;
  893. memmove(p+2, d, n);
  894. return p+n+2;
  895. }
  896. uchar*
  897. optaddbyte(uchar *p, int op, int b)
  898. {
  899. p[0] = op;
  900. p[1] = 1;
  901. p[2] = b;
  902. return p+3;
  903. }
  904. uchar*
  905. optaddulong(uchar *p, int op, ulong x)
  906. {
  907. p[0] = op;
  908. p[1] = 4;
  909. hnputl(p+2, x);
  910. return p+6;
  911. }
  912. uchar *
  913. optaddaddr(uchar *p, int op, uchar *ip)
  914. {
  915. p[0] = op;
  916. p[1] = 4;
  917. v6tov4(p+2, ip);
  918. return p+6;
  919. }
  920. uchar *
  921. optaddvec(uchar *p, int op, uchar *v, int n)
  922. {
  923. p[0] = op;
  924. p[1] = n;
  925. memmove(p+2, v, n);
  926. return p+2+n;
  927. }
  928. uchar *
  929. optaddstr(uchar *p, int op, char *v)
  930. {
  931. int n;
  932. n = strlen(v)+1; // microsoft leaves on the null, so we do too
  933. p[0] = op;
  934. p[1] = n;
  935. memmove(p+2, v, n);
  936. return p+2+n;
  937. }
  938. uchar*
  939. optget(uchar *p, int op, int *np)
  940. {
  941. int len, code;
  942. for(;;) {
  943. code = *p++;
  944. if(code == OBpad)
  945. continue;
  946. if(code == OBend)
  947. return 0;
  948. len = *p++;
  949. if(code != op) {
  950. p += len;
  951. continue;
  952. }
  953. if(np != nil){
  954. if(*np > len)
  955. return 0;
  956. *np = len;
  957. }
  958. return p;
  959. }
  960. }
  961. int
  962. optgetbyte(uchar *p, int op)
  963. {
  964. int len;
  965. len = 1;
  966. p = optget(p, op, &len);
  967. if(p == 0)
  968. return 0;
  969. return *p;
  970. }
  971. ulong
  972. optgetulong(uchar *p, int op)
  973. {
  974. int len;
  975. len = 4;
  976. p = optget(p, op, &len);
  977. if(p == 0)
  978. return 0;
  979. return nhgetl(p);
  980. }
  981. int
  982. optgetaddr(uchar *p, int op, uchar *ip)
  983. {
  984. int len;
  985. len = 4;
  986. p = optget(p, op, &len);
  987. if(p == 0)
  988. return 0;
  989. v4tov6(ip, p);
  990. return 1;
  991. }
  992. int
  993. optgetaddrs(uchar *p, int op, uchar *ip, int n)
  994. {
  995. int len, i;
  996. len = 4;
  997. p = optget(p, op, &len);
  998. if(p == 0)
  999. return 0;
  1000. len /= IPv4addrlen;
  1001. if(len > n)
  1002. len = n;
  1003. for(i = 0; i < len; i++)
  1004. v4tov6(&ip[i*IPaddrlen], &p[i*IPv4addrlen]);
  1005. return i;
  1006. }
  1007. int
  1008. optgetvec(uchar *p, int op, uchar *v, int n)
  1009. {
  1010. int len;
  1011. len = 1;
  1012. p = optget(p, op, &len);
  1013. if(p == 0)
  1014. return 0;
  1015. if(len > n)
  1016. len = n;
  1017. memmove(v, p, len);
  1018. return len;
  1019. }
  1020. int
  1021. optgetstr(uchar *p, int op, char *s, int n)
  1022. {
  1023. int len;
  1024. len = 1;
  1025. p = optget(p, op, &len);
  1026. if(p == 0)
  1027. return 0;
  1028. if(len >= n)
  1029. len = n-1;
  1030. memmove(s, p, len);
  1031. s[len] = 0;
  1032. return len;
  1033. }
  1034. // sanity check options area
  1035. // - options don't overflow packet
  1036. // - options end with an OBend
  1037. int
  1038. parseoptions(uchar *p, int n)
  1039. {
  1040. int code, len;
  1041. int nin = n;
  1042. while(n>0) {
  1043. code = *p++;
  1044. n--;
  1045. if(code == OBpad)
  1046. continue;
  1047. if(code == OBend)
  1048. return 0;
  1049. if(n == 0) {
  1050. fprint(2, "%s: parse: bad option: 0x%ux: truncated: opt length = %d\n",
  1051. argv0, code, nin);
  1052. return -1;
  1053. }
  1054. len = *p++;
  1055. n--;
  1056. if(len > n) {
  1057. fprint(2, "%s: parse: bad option: 0x%ux: %d > %d: opt length = %d\n",
  1058. argv0, code, len, n, nin);
  1059. return -1;
  1060. }
  1061. p += len;
  1062. n -= len;
  1063. }
  1064. // make sure packet ends with an OBend all the optget code
  1065. *p = OBend;
  1066. return 0;
  1067. }
  1068. // sanity check received packet:
  1069. // - magic is dhcp magic
  1070. // - options don't overflow packet
  1071. Bootp *
  1072. parsebootp(uchar *p, int n)
  1073. {
  1074. Bootp *bp;
  1075. bp = (Bootp*)p;
  1076. if(n < bp->optmagic - p) {
  1077. fprint(2, "%s: parse: short bootp packet\n", argv0);
  1078. return nil;
  1079. }
  1080. if(conf.xid != nhgetl(bp->xid))
  1081. return nil;
  1082. if(bp->op != Bootreply) {
  1083. fprint(2, "%s: parse: bad op\n", argv0);
  1084. return nil;
  1085. }
  1086. n -= bp->optmagic - p;
  1087. p = bp->optmagic;
  1088. if(n < 4) {
  1089. fprint(2, "%s: parse: not option data\n", argv0);
  1090. return nil;
  1091. }
  1092. if(memcmp(optmagic, p, 4) != 0) {
  1093. fprint(2, "%s: parse: bad opt magic %ux %ux %ux %ux\n", argv0,
  1094. p[0], p[1], p[2], p[3]);
  1095. return nil;
  1096. }
  1097. p += 4;
  1098. n -= 4;
  1099. if(parseoptions(p, n) < 0)
  1100. return nil;
  1101. return bp;
  1102. }
  1103. // write out an ndb entry
  1104. void
  1105. writendb(char *s, int n, int append)
  1106. {
  1107. char file[64];
  1108. int fd;
  1109. snprint(file, sizeof file, "%s/ndb", conf.mpoint);
  1110. if(append){
  1111. fd = open(file, OWRITE);
  1112. seek(fd, 0, 2);
  1113. } else
  1114. fd = open(file, OWRITE|OTRUNC);
  1115. write(fd, s, n);
  1116. close(fd);
  1117. }
  1118. // put server addresses into the ndb entry
  1119. char*
  1120. putaddrs(char *p, char *e, char *attr, uchar *a, int len)
  1121. {
  1122. int i;
  1123. for(i = 0; i < len; i += IPaddrlen, a += IPaddrlen){
  1124. if(!validip(a))
  1125. break;
  1126. p = seprint(p, e, "%s=%I\n", attr, a);
  1127. }
  1128. return p;
  1129. }
  1130. // make an ndb entry and put it into /net/ndb for the servers to see
  1131. void
  1132. putndb(void)
  1133. {
  1134. char buf[1024];
  1135. char *p, *e, *np;
  1136. int append;
  1137. e = buf + sizeof(buf);
  1138. p = buf;
  1139. if(getndb() == 0){
  1140. append = 1;
  1141. } else {
  1142. append = 0;
  1143. p = seprint(p, e, "ip=%I ipmask=%M ipgw=%I\n",
  1144. conf.laddr, conf.mask, conf.gaddr);
  1145. }
  1146. if(np = strchr(conf.hostname, '.')){
  1147. if(*conf.domainname == 0)
  1148. strcpy(conf.domainname, np+1);
  1149. *np = 0;
  1150. }
  1151. if(*conf.hostname)
  1152. p = seprint(p, e, "\tsys=%s\n", conf.hostname);
  1153. if(*conf.domainname)
  1154. p = seprint(p, e, "\tdom=%s.%s\n", conf.hostname, conf.domainname);
  1155. if(validip(conf.fs))
  1156. p = putaddrs(p, e, "\tfs", conf.fs, sizeof(conf.fs));
  1157. if(validip(conf.auth))
  1158. p = putaddrs(p, e, "\tauth", conf.auth, sizeof(conf.auth));
  1159. if(validip(conf.dns))
  1160. p = putaddrs(p, e, "\tdns", conf.dns, sizeof(conf.dns));
  1161. if(validip(conf.ntp))
  1162. p = putaddrs(p, e, "\tntp", conf.ntp, sizeof(conf.ntp));
  1163. if(p > buf)
  1164. writendb(buf, p-buf, append);
  1165. }
  1166. // get an ndb entry someone else wrote
  1167. int
  1168. getndb(void)
  1169. {
  1170. char buf[1024];
  1171. int fd;
  1172. int n;
  1173. char *p;
  1174. snprint(buf, sizeof buf, "%s/ndb", conf.mpoint);
  1175. fd = open(buf, OREAD);
  1176. n = read(fd, buf, sizeof(buf)-1);
  1177. close(fd);
  1178. if(n <= 0)
  1179. return -1;
  1180. buf[n] = 0;
  1181. p = strstr(buf, "ip=");
  1182. if(p == nil)
  1183. return -1;
  1184. parseip(conf.laddr, p+3);
  1185. return 0;
  1186. }
  1187. // tell a server to refresh
  1188. void
  1189. tweakserver(char *server)
  1190. {
  1191. char file[64];
  1192. int fd;
  1193. snprint(file, sizeof(file), "%s/%s", conf.mpoint, server);
  1194. fd = open(file, ORDWR);
  1195. if(fd < 0)
  1196. return;
  1197. fprint(fd, "refresh");
  1198. close(fd);
  1199. }
  1200. // tell all servers to refresh their information
  1201. void
  1202. tweakservers(void)
  1203. {
  1204. tweakserver("dns");
  1205. tweakserver("cs");
  1206. }
  1207. // return number of networks
  1208. int
  1209. nipifcs(char *net)
  1210. {
  1211. Ipifc *nifc;
  1212. Iplifc *lifc;
  1213. int n;
  1214. n = 0;
  1215. ifc = readipifc(net, ifc, -1);
  1216. for(nifc = ifc; nifc != nil; nifc = nifc->next){
  1217. /*
  1218. * ignore loopback devices when trying to
  1219. * figure out if we're the primary interface.
  1220. */
  1221. if(strcmp(nifc->dev, "/dev/null") != 0)
  1222. for(lifc = nifc->lifc; lifc != nil; lifc = lifc->next)
  1223. if(validip(lifc->ip)){
  1224. n++;
  1225. break;
  1226. }
  1227. if(strcmp(nifc->dev, conf.dev) == 0)
  1228. myifc = nifc->index;
  1229. }
  1230. return n;
  1231. }
  1232. // return true if this is a valid v4 address
  1233. int
  1234. validip(uchar *addr)
  1235. {
  1236. return ipcmp(addr, IPnoaddr) != 0 && ipcmp(addr, v4prefix) != 0;
  1237. }
  1238. char *verbs[] = {
  1239. [Vadd] "add",
  1240. [Vremove] "remove",
  1241. [Vunbind] "unbind",
  1242. [Vether] "ether",
  1243. };
  1244. // look for an action
  1245. int
  1246. parseverb(char *name)
  1247. {
  1248. int i;
  1249. for(i = 0; i < nelem(verbs); i++){
  1250. if(verbs[i] == 0)
  1251. continue;
  1252. if(strcmp(name, verbs[i]) == 0)
  1253. return i;
  1254. }
  1255. return -1;
  1256. }