ipconfig.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ip.h>
  4. #include <bio.h>
  5. #include <ndb.h>
  6. #include "dhcp.h"
  7. int noconfig;
  8. int debug;
  9. int dodhcp;
  10. int nip;
  11. int myifc = -1;
  12. int plan9 = 1;
  13. int beprimary = -1;
  14. int nodhcpwatch;
  15. int sendhostname;
  16. int dondbconfig = 0;
  17. int Oflag;
  18. char *ndboptions;
  19. Ipifc *ifc;
  20. // possible verbs
  21. enum
  22. {
  23. Vadd,
  24. Vremove,
  25. Vunbind,
  26. Vether,
  27. Vloopback,
  28. };
  29. struct {
  30. // locally generated
  31. char *type;
  32. char *dev;
  33. char mpoint[32];
  34. int cfd; // ifc control channel
  35. char *cputype;
  36. uchar hwa[32]; // hardware address
  37. int hwatype;
  38. int hwalen;
  39. uchar cid[32];
  40. int cidlen;
  41. char *baud;
  42. // learned info
  43. uchar gaddr[IPaddrlen];
  44. uchar laddr[IPaddrlen];
  45. uchar mask[IPaddrlen];
  46. uchar raddr[IPaddrlen];
  47. uchar dns[2*IPaddrlen];
  48. uchar fs[2*IPaddrlen];
  49. uchar auth[2*IPaddrlen];
  50. uchar ntp[IPaddrlen];
  51. int mtu;
  52. // dhcp specific
  53. int state;
  54. int fd;
  55. ulong xid;
  56. ulong starttime;
  57. char sname[64];
  58. char hostname[32];
  59. char domainname[64];
  60. uchar server[IPaddrlen]; /* server IP address */
  61. ulong offered; /* offered lease time */
  62. ulong lease; /* lease time */
  63. ulong resend; /* number of resends for current state */
  64. ulong timeout; /* time to timeout - seconds */
  65. } conf;
  66. enum
  67. {
  68. Taddr,
  69. Taddrs,
  70. Tstr,
  71. Tbyte,
  72. Tulong,
  73. Tvec,
  74. };
  75. typedef struct Option Option;
  76. struct Option
  77. {
  78. char *name;
  79. int type;
  80. };
  81. // I was too lazy to look up the types for each of these
  82. // options. If someone feels like it, please mail me a
  83. // corrected array -- presotto
  84. Option option[256] =
  85. {
  86. [OBmask] { "ipmask", Taddr },
  87. [OBtimeoff] { "timeoff", Tulong },
  88. [OBrouter] { "ipgw", Taddrs },
  89. [OBtimeserver] { "time", Taddrs },
  90. [OBnameserver] { "name", Taddrs },
  91. [OBdnserver] { "dns", Taddrs },
  92. [OBlogserver] { "log", Taddrs },
  93. [OBcookieserver] { "cookie", Taddrs },
  94. [OBlprserver] { "lpr", Taddrs },
  95. [OBimpressserver] { "impress", Taddrs },
  96. [OBrlserver] { "rl", Taddrs },
  97. [OBhostname] { "sys", Tstr },
  98. [OBbflen] { "bflen", Tulong },
  99. [OBdumpfile] { "dumpfile", Tstr },
  100. [OBdomainname] { "dom", Tstr },
  101. [OBswapserver] { "swap", Taddrs },
  102. [OBrootpath] { "rootpath", Tstr },
  103. [OBextpath] { "extpath", Tstr },
  104. [OBipforward] { "ipforward", Taddrs },
  105. [OBnonlocal] { "nonlocal", Taddrs },
  106. [OBpolicyfilter] { "policyfilter", Taddrs },
  107. [OBmaxdatagram] { "maxdatagram", Tulong },
  108. [OBttl] { "ttl", Tulong },
  109. [OBpathtimeout] { "pathtimeout", Taddrs },
  110. [OBpathplateau] { "pathplateau", Taddrs },
  111. [OBmtu] { "mtu", Tulong },
  112. [OBsubnetslocal] { "subnetslocal", Taddrs },
  113. [OBbaddr] { "baddr", Taddrs },
  114. [OBdiscovermask] { "discovermask", Taddrs },
  115. [OBsupplymask] { "supplymask", Taddrs },
  116. [OBdiscoverrouter] { "discoverrouter", Taddrs },
  117. [OBrsserver] { "rs", Taddrs },
  118. [OBstaticroutes] { "staticroutes", Taddrs },
  119. [OBtrailerencap] { "trailerencap", Taddrs },
  120. [OBarptimeout] { "arptimeout", Tulong },
  121. [OBetherencap] { "etherencap", Taddrs },
  122. [OBtcpttl] { "tcpttl", Tulong },
  123. [OBtcpka] { "tcpka", Tulong },
  124. [OBtcpkag] { "tcpkag", Tulong },
  125. [OBnisdomain] { "nisdomain", Tstr },
  126. [OBniserver] { "ni", Taddrs },
  127. [OBntpserver] { "ntp", Taddrs },
  128. [OBnetbiosns] { "netbiosns", Taddrs },
  129. [OBnetbiosdds] { "netbiosdds", Taddrs },
  130. [OBnetbiostype] { "netbiostype", Taddrs },
  131. [OBnetbiosscope] { "netbiosscope", Taddrs },
  132. [OBxfontserver] { "xfont", Taddrs },
  133. [OBxdispmanager] { "xdispmanager", Taddrs },
  134. [OBnisplusdomain] { "nisplusdomain", Tstr },
  135. [OBnisplusserver] { "nisplus", Taddrs },
  136. [OBhomeagent] { "homeagent", Taddrs },
  137. [OBsmtpserver] { "smtp", Taddrs },
  138. [OBpop3server] { "pop3", Taddrs },
  139. [OBnntpserver] { "nntp", Taddrs },
  140. [OBwwwserver] { "www", Taddrs },
  141. [OBfingerserver] { "finger", Taddrs },
  142. [OBircserver] { "irc", Taddrs },
  143. [OBstserver] { "st", Taddrs },
  144. [OBstdaserver] { "stdar", Taddrs },
  145. [ODipaddr] { "ipaddr", Taddr },
  146. [ODlease] { "lease", Tulong },
  147. [ODoverload] { "overload", Taddr },
  148. [ODtype] { "type", Tbyte },
  149. [ODserverid] { "serverid", Taddr },
  150. [ODparams] { "params", Tvec },
  151. [ODmessage] { "message", Tstr },
  152. [ODmaxmsg] { "maxmsg", Tulong },
  153. [ODrenewaltime] { "renewaltime", Tulong },
  154. [ODrebindingtime] { "rebindingtime", Tulong },
  155. [ODvendorclass] { "vendorclass", Tvec },
  156. [ODclientid] { "clientid", Tvec },
  157. [ODtftpserver] { "tftp", Taddr },
  158. [ODbootfile] { "bootfile", Tstr },
  159. };
  160. uchar defrequested[] = {
  161. OBmask, OBrouter, OBdnserver, OBhostname, OBdomainname, OBntpserver,
  162. };
  163. uchar requested[256];
  164. int nrequested;
  165. void adddefroute(char*, uchar*);
  166. int addoption(char*);
  167. void binddevice(void);
  168. void bootprequest(void);
  169. void controldevice(void);
  170. void dhcpquery(int, int);
  171. void dhcprecv(void);
  172. void dhcpsend(int);
  173. int dhcptimer(void);
  174. void dhcpwatch(int);
  175. void doadd(int);
  176. void doremove(void);
  177. void dounbind(void);
  178. int getndb(void);
  179. void getoptions(uchar*);
  180. int ipconfig(void);
  181. void lookforip(char*);
  182. void mkclientid(void);
  183. void ndbconfig(void);
  184. int nipifcs(char*);
  185. int openlisten(void);
  186. uchar* optadd(uchar*, int, void*, int);
  187. uchar* optaddaddr(uchar*, int, uchar*);
  188. uchar* optaddbyte(uchar*, int, int);
  189. uchar* optaddstr(uchar*, int, char*);
  190. uchar* optaddulong(uchar*, int, ulong);
  191. uchar* optaddvec(uchar*, int, uchar*, int);
  192. uchar* optget(uchar*, int, int*);
  193. int optgetaddr(uchar*, int, uchar*);
  194. int optgetaddrs(uchar*, int, uchar*, int);
  195. int optgetbyte(uchar*, int);
  196. int optgetstr(uchar*, int, char*, int);
  197. ulong optgetulong(uchar*, int);
  198. int optgetvec(uchar*, int, uchar*, int);
  199. char* optgetx(uchar*, uchar);
  200. Bootp* parsebootp(uchar*, int);
  201. int parseoptions(uchar *p, int n);
  202. int parseverb(char*);
  203. void putndb(void);
  204. void tweakservers(void);
  205. void usage(void);
  206. int validip(uchar*);
  207. void writendb(char*, int, int);
  208. char optmagic[4] = { 0x63, 0x82, 0x53, 0x63 };
  209. #define DEBUG if(debug)print
  210. typedef struct Ctl Ctl;
  211. struct Ctl
  212. {
  213. Ctl *next;
  214. char *ctl;
  215. };
  216. Ctl *firstctl, **ctll;
  217. void
  218. usage(void)
  219. {
  220. 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);
  221. exits("usage");
  222. }
  223. void
  224. main(int argc, char **argv)
  225. {
  226. char *p;
  227. int retry, verb, action;
  228. Ctl *cp;
  229. srand(truerand());
  230. fmtinstall('E', eipfmt);
  231. fmtinstall('I', eipfmt);
  232. fmtinstall('M', eipfmt);
  233. fmtinstall('V', eipfmt);
  234. setnetmtpt(conf.mpoint, sizeof(conf.mpoint), nil);
  235. conf.cputype = getenv("cputype");
  236. if(conf.cputype == nil)
  237. conf.cputype = "386";
  238. retry = 0;
  239. ctll = &firstctl;
  240. // init set of requested parameters with the default
  241. nrequested = sizeof(defrequested);
  242. memcpy(requested, defrequested, nrequested);
  243. ARGBEGIN {
  244. case 'D':
  245. debug = 1;
  246. break;
  247. case 'G':
  248. plan9 = 0;
  249. break;
  250. case 'N':
  251. dondbconfig = 1;
  252. break;
  253. case 'O':
  254. Oflag = 1;
  255. break;
  256. case 'p':
  257. beprimary = 1;
  258. break;
  259. case 'P':
  260. beprimary = 0;
  261. case 'b':
  262. p = ARGF();
  263. if(p == nil)
  264. usage();
  265. conf.baud = p;
  266. break;
  267. case 'c':
  268. p = ARGF();
  269. if(p == nil)
  270. usage();
  271. cp = malloc(sizeof(*cp));
  272. if(cp == nil)
  273. sysfatal("%r");
  274. *ctll = cp;
  275. ctll = &cp->next;
  276. cp->next = nil;
  277. cp->ctl = p;
  278. break;
  279. case 'd':
  280. dodhcp = 1;
  281. break;
  282. case 'g':
  283. p = ARGF();
  284. if(p == nil)
  285. usage();
  286. parseip(conf.gaddr, p);
  287. break;
  288. case 'h':
  289. p = ARGF();
  290. if(p == nil)
  291. usage();
  292. snprint(conf.hostname, sizeof(conf.hostname), "%s", p);
  293. sendhostname = 1;
  294. break;
  295. case 'n':
  296. noconfig = 1;
  297. break;
  298. case 'm':
  299. p = ARGF();
  300. if(p == nil)
  301. usage();
  302. conf.mtu = atoi(p);
  303. break;
  304. case 'r':
  305. retry = 1;
  306. break;
  307. case 'x':
  308. p = ARGF();
  309. if(p == nil)
  310. usage();
  311. setnetmtpt(conf.mpoint, sizeof(conf.mpoint), p);
  312. break;
  313. case 'X':
  314. nodhcpwatch = 1;
  315. break;
  316. case 'o':
  317. if(addoption(ARGF()) < 0)
  318. usage();
  319. break;
  320. } ARGEND;
  321. // default to any host name we already have
  322. if(*conf.hostname == 0){
  323. p = getenv("sysname");
  324. if(p == nil || *p == 0)
  325. p = sysname();
  326. if(p != nil)
  327. strncpy(conf.hostname, p, sizeof(conf.hostname)-1);
  328. }
  329. // default
  330. conf.type = "ether";
  331. conf.dev = "/net/ether0";
  332. action = Vadd;
  333. // get verb, default is "add"
  334. while(argc > 0){
  335. verb = parseverb(argv[0]);
  336. switch(verb){
  337. case Vadd:
  338. case Vremove:
  339. case Vunbind:
  340. action = verb;
  341. break;
  342. case Vether:
  343. case Vloopback:
  344. conf.type = argv[0];
  345. if(argc > 1){
  346. conf.dev = argv[1];
  347. argc--, argv++;
  348. }
  349. break;
  350. }
  351. if(verb < 0)
  352. break;
  353. argc--, argv++;
  354. }
  355. // get addresses
  356. switch(argc){
  357. case 5:
  358. parseip(conf.auth, argv[4]);
  359. /* fall through */
  360. case 4:
  361. parseip(conf.fs, argv[3]);
  362. /* fall through */
  363. case 3:
  364. parseip(conf.raddr, argv[2]);
  365. /* fall through */
  366. case 2:
  367. if(strcmp(argv[1], "0") != 0)
  368. parseipmask(conf.mask, argv[1]);
  369. /* fall through */
  370. case 1:
  371. parseip(conf.laddr, argv[0]);
  372. break;
  373. }
  374. switch(action){
  375. case Vadd:
  376. doadd(retry);
  377. break;
  378. case Vremove:
  379. doremove();
  380. break;
  381. case Vunbind:
  382. dounbind();
  383. break;
  384. }
  385. exits(0);
  386. }
  387. int
  388. havendb(char *net)
  389. {
  390. Dir *d;
  391. char buf[128];
  392. snprint(buf, sizeof buf, "%s/ndb", net);
  393. if((d = dirstat("/net/ndb")) == nil)
  394. return 0;
  395. if(d->length == 0){
  396. free(d);
  397. return 0;
  398. }
  399. free(d);
  400. return 1;
  401. }
  402. void
  403. doadd(int retry)
  404. {
  405. int tries;
  406. // get number of preexisting interfaces
  407. nip = nipifcs(conf.mpoint);
  408. if(beprimary == -1 && (nip == 0 || !havendb(conf.mpoint)))
  409. beprimary = 1;
  410. // get ipifc into name space and condition device for ip
  411. if(!noconfig){
  412. lookforip(conf.mpoint);
  413. controldevice();
  414. binddevice();
  415. }
  416. if(!validip(conf.laddr)){
  417. if(dondbconfig)
  418. ndbconfig();
  419. else
  420. dodhcp = 1;
  421. }
  422. // run dhcp if we need something
  423. if(dodhcp){
  424. mkclientid();
  425. for(tries = 0; tries < 6; tries++){
  426. dhcpquery(!noconfig, Sselecting);
  427. if(conf.state == Sbound)
  428. break;
  429. sleep(1000);
  430. }
  431. }
  432. if(!validip(conf.laddr)){
  433. if(retry && dodhcp && !noconfig){
  434. fprint(2, "%s: couldn't determine ip address, retrying\n", argv0);
  435. dhcpwatch(1);
  436. return;
  437. } else {
  438. fprint(2, "%s: no success with DHCP\n", argv0);
  439. exits("failed");
  440. }
  441. }
  442. if(!noconfig){
  443. if(ipconfig() < 0)
  444. sysfatal("can't start ip");
  445. if(dodhcp && conf.lease != Lforever)
  446. dhcpwatch(0);
  447. }
  448. // leave everything we've learned somewhere other procs can find it
  449. if(beprimary == 1){
  450. putndb();
  451. tweakservers();
  452. }
  453. }
  454. void
  455. doremove(void)
  456. {
  457. char file[128];
  458. int cfd;
  459. Ipifc *nifc;
  460. Iplifc *lifc;
  461. if(!validip(conf.laddr)){
  462. fprint(2, "%s: remove requires an address\n", argv0);
  463. exits("usage");
  464. }
  465. ifc = readipifc(conf.mpoint, ifc, -1);
  466. for(nifc = ifc; nifc != nil; nifc = nifc->next){
  467. if(strcmp(nifc->dev, conf.dev) != 0)
  468. continue;
  469. for(lifc = nifc->lifc; lifc != nil; lifc = lifc->next){
  470. if(ipcmp(conf.laddr, lifc->ip) != 0)
  471. continue;
  472. if(validip(conf.mask) && ipcmp(conf.mask, lifc->mask) != 0)
  473. continue;
  474. if(validip(conf.raddr) && ipcmp(conf.raddr, lifc->net) != 0)
  475. continue;
  476. snprint(file, sizeof(file), "%s/ipifc/%d/ctl", conf.mpoint, nifc->index);
  477. cfd = open(file, ORDWR);
  478. if(cfd < 0){
  479. fprint(2, "%s: can't open %s: %r\n", argv0, conf.mpoint);
  480. continue;
  481. }
  482. if(fprint(cfd, "remove %I %M", lifc->ip, lifc->mask) < 0)
  483. fprint(2, "%s: can't remove %I %I from %s: %r\n", argv0,
  484. lifc->ip, lifc->mask, file);
  485. }
  486. }
  487. }
  488. void
  489. dounbind(void)
  490. {
  491. Ipifc *nifc;
  492. char file[128];
  493. int cfd;
  494. ifc = readipifc(conf.mpoint, ifc, -1);
  495. for(nifc = ifc; nifc != nil; nifc = nifc->next){
  496. if(strcmp(nifc->dev, conf.dev) == 0){
  497. snprint(file, sizeof(file), "%s/ipifc/%d/ctl", conf.mpoint, nifc->index);
  498. cfd = open(file, ORDWR);
  499. if(cfd < 0){
  500. fprint(2, "%s: can't open %s: %r\n", argv0, conf.mpoint);
  501. break;
  502. }
  503. if(fprint(cfd, "unbind") < 0)
  504. fprint(2, "%s: can't unbind from %s: %r\n", argv0, file);
  505. break;
  506. }
  507. }
  508. }
  509. // set the default route
  510. void
  511. adddefroute(char *mpoint, uchar *gaddr)
  512. {
  513. char buf[256];
  514. int cfd;
  515. sprint(buf, "%s/iproute", mpoint);
  516. cfd = open(buf, ORDWR);
  517. if(cfd < 0)
  518. return;
  519. if(isv4(gaddr))
  520. fprint(cfd, "add 0 0 %I", gaddr);
  521. else
  522. fprint(cfd, "add :: :: %I", gaddr);
  523. close(cfd);
  524. }
  525. // create a client id
  526. void
  527. mkclientid(void)
  528. {
  529. if(strcmp(conf.type, "ether") == 0)
  530. if(myetheraddr(conf.hwa, conf.dev) == 0){
  531. conf.hwalen = 6;
  532. conf.hwatype = 1;
  533. conf.cid[0] = conf.hwatype;
  534. memmove(&conf.cid[1], conf.hwa, conf.hwalen);
  535. conf.cidlen = conf.hwalen+1;
  536. } else {
  537. conf.hwatype = -1;
  538. snprint((char*)conf.cid, sizeof(conf.cid), "plan9_%ld.%d", lrand(), getpid());
  539. conf.cidlen = strlen((char*)conf.cid);
  540. }
  541. }
  542. // bind ip into the namespace
  543. void
  544. lookforip(char *net)
  545. {
  546. char proto[64];
  547. snprint(proto, sizeof(proto), "%s/ipifc", net);
  548. if(access(proto, 0) == 0)
  549. return;
  550. sysfatal("no ip stack bound onto %s", net);
  551. }
  552. // send some ctls to a device
  553. void
  554. controldevice(void)
  555. {
  556. char ctlfile[256];
  557. int fd;
  558. Ctl *cp;
  559. if(firstctl == nil)
  560. return;
  561. if(strcmp(conf.type, "ether") == 0)
  562. snprint(ctlfile, sizeof(ctlfile), "%s/clone", conf.dev);
  563. else
  564. return;
  565. fd = open(ctlfile, ORDWR);
  566. if(fd < 0)
  567. sysfatal("can't open %s", ctlfile);
  568. for(cp = firstctl; cp != nil; cp = cp->next){
  569. if(write(fd, cp->ctl, strlen(cp->ctl)) < 0)
  570. sysfatal("ctl message %s: %r", cp->ctl);
  571. seek(fd, 0, 0);
  572. }
  573. }
  574. // bind an ip stack to a device, leave the control channel open
  575. void
  576. binddevice(void)
  577. {
  578. char buf[256];
  579. if(myifc < 0){
  580. // get a new ip interface
  581. snprint(buf, sizeof(buf), "%s/ipifc/clone", conf.mpoint);
  582. conf.cfd = open(buf, ORDWR);
  583. if(conf.cfd < 0)
  584. sysfatal("opening %s/ipifc/clone: %r", conf.mpoint);
  585. // specify the medium as an ethernet, and bind the interface to it
  586. if(fprint(conf.cfd, "bind %s %s", conf.type, conf.dev) < 0)
  587. sysfatal("binding device: %r");
  588. } else {
  589. // open the old interface
  590. snprint(buf, sizeof(buf), "%s/ipifc/%d/ctl", conf.mpoint, myifc);
  591. conf.cfd = open(buf, ORDWR);
  592. if(conf.cfd < 0)
  593. sysfatal("opening %s/ipifc/%d/ctl: %r", conf.mpoint, myifc);
  594. }
  595. }
  596. // add a logical interface to the ip stack
  597. int
  598. ipconfig(void)
  599. {
  600. char buf[256];
  601. int n;
  602. if(!validip(conf.laddr))
  603. return -1;
  604. n = sprint(buf, "add");
  605. n += snprint(buf+n, sizeof(buf)-n, " %I", conf.laddr);
  606. if(!validip(conf.mask))
  607. ipmove(conf.mask, defmask(conf.laddr));
  608. n += snprint(buf+n, sizeof(buf)-n, " %I", conf.mask);
  609. if(validip(conf.raddr)){
  610. n += snprint(buf+n, sizeof(buf)-n, " %I", conf.raddr);
  611. if(conf.mtu != 0)
  612. n += snprint(buf+n, sizeof(buf)-n, " %d", conf.mtu);
  613. }
  614. if(write(conf.cfd, buf, n) < 0){
  615. fprint(2, "ipconfig: write(%s): %r\n", buf);
  616. return -1;
  617. }
  618. if(beprimary==1 && validip(conf.gaddr))
  619. adddefroute(conf.mpoint, conf.gaddr);
  620. return 0;
  621. }
  622. // remove a logical interface to the ip stack
  623. void
  624. ipunconfig(void)
  625. {
  626. char buf[256];
  627. int n;
  628. if(!validip(conf.laddr))
  629. return;
  630. DEBUG("couldn't renew IP lease, releasing %I\n", conf.laddr);
  631. n = sprint(buf, "remove");
  632. n += snprint(buf+n, sizeof(buf)-n, " %I", conf.laddr);
  633. if(!validip(conf.mask))
  634. ipmove(conf.mask, defmask(conf.laddr));
  635. n += snprint(buf+n, sizeof(buf)-n, " %I", conf.mask);
  636. write(conf.cfd, buf, n);
  637. ipmove(conf.laddr, IPnoaddr);
  638. ipmove(conf.raddr, IPnoaddr);
  639. ipmove(conf.mask, IPnoaddr);
  640. // forget configuration info
  641. if(beprimary==1)
  642. writendb("", 0, 0);
  643. }
  644. void
  645. ding(void*, char *msg)
  646. {
  647. if(strstr(msg, "alarm"))
  648. noted(NCONT);
  649. noted(NDFLT);
  650. }
  651. void
  652. dhcpquery(int needconfig, int startstate)
  653. {
  654. if(needconfig)
  655. fprint(conf.cfd, "add %I %I", IPnoaddr, IPnoaddr);
  656. conf.fd = openlisten();
  657. if(conf.fd < 0){
  658. conf.state = Sinit;
  659. return;
  660. }
  661. notify(ding);
  662. // try dhcp for 10 seconds
  663. conf.xid = lrand();
  664. conf.starttime = time(0);
  665. conf.state = startstate;
  666. switch(startstate){
  667. case Sselecting:
  668. conf.offered = 0;
  669. dhcpsend(Discover);
  670. break;
  671. case Srenewing:
  672. dhcpsend(Request);
  673. break;
  674. default:
  675. sysfatal("internal error 0");
  676. }
  677. conf.resend = 0;
  678. conf.timeout = time(0) + 4;
  679. while(conf.state != Sbound){
  680. dhcprecv();
  681. if(dhcptimer() < 0)
  682. break;
  683. if(time(0) - conf.starttime > 10)
  684. break;
  685. }
  686. close(conf.fd);
  687. if(needconfig)
  688. fprint(conf.cfd, "remove %I %I", IPnoaddr, IPnoaddr);
  689. }
  690. #define HOUR (60*60)
  691. void
  692. dhcpwatch(int needconfig)
  693. {
  694. int secs, s;
  695. ulong t;
  696. if(nodhcpwatch)
  697. return;
  698. switch(rfork(RFPROC|RFFDG|RFNOWAIT|RFNOTEG)){
  699. default:
  700. return;
  701. case 0:
  702. break;
  703. }
  704. // keep trying to renew the lease
  705. for(;;){
  706. if(conf.lease == 0)
  707. secs = 5;
  708. else
  709. secs = conf.lease>>1;
  710. // avoid overflows
  711. for(s = secs; s > 0; s -= t){
  712. if(s > HOUR)
  713. t = HOUR;
  714. else
  715. t = s;
  716. sleep(t*1000);
  717. }
  718. if(conf.lease > 0){
  719. // during boot, the starttime can be bogus so avoid
  720. // spurious ipinconfig's
  721. t = time(0) - conf.starttime;
  722. if(t > (3*secs)/2)
  723. t = secs;
  724. if(t >= conf.lease){
  725. conf.lease = 0;
  726. if(!noconfig){
  727. ipunconfig();
  728. needconfig = 1;
  729. }
  730. } else
  731. conf.lease -= t;
  732. }
  733. dhcpquery(needconfig, needconfig ? Sselecting : Srenewing);
  734. if(needconfig && conf.state == Sbound){
  735. if(ipconfig() < 0)
  736. sysfatal("can't start ip: %r");
  737. needconfig = 0;
  738. // leave everything we've learned somewhere other procs can find it
  739. if(beprimary==1){
  740. putndb();
  741. tweakservers();
  742. }
  743. }
  744. }
  745. }
  746. int
  747. dhcptimer(void)
  748. {
  749. ulong now;
  750. now = time(0);
  751. if(now < conf.timeout)
  752. return 0;
  753. switch(conf.state) {
  754. default:
  755. sysfatal("dhcptimer: unknown state %d", conf.state);
  756. case Sinit:
  757. break;
  758. case Sselecting:
  759. dhcpsend(Discover);
  760. conf.timeout = now + 4;
  761. conf.resend++;
  762. if(conf.resend>5)
  763. goto err;
  764. break;
  765. case Srequesting:
  766. dhcpsend(Request);
  767. conf.timeout = now + 4;
  768. conf.resend++;
  769. if(conf.resend>5)
  770. goto err;
  771. break;
  772. case Srenewing:
  773. dhcpsend(Request);
  774. conf.timeout = now + 1;
  775. conf.resend++;
  776. if(conf.resend>3) {
  777. conf.state = Srebinding;
  778. conf.resend = 0;
  779. }
  780. break;
  781. case Srebinding:
  782. dhcpsend(Request);
  783. conf.timeout = now + 4;
  784. conf.resend++;
  785. if(conf.resend>5)
  786. goto err;
  787. break;
  788. case Sbound:
  789. break;
  790. }
  791. return 0;
  792. err:
  793. conf.state = Sinit;
  794. return -1;
  795. }
  796. void
  797. dhcpsend(int type)
  798. {
  799. Bootp bp;
  800. uchar *p;
  801. int n;
  802. uchar vendor[64];
  803. OUdphdr *up = (OUdphdr*)bp.udphdr;
  804. memset(&bp, 0, sizeof(bp));
  805. hnputs(up->rport, 67);
  806. bp.op = Bootrequest;
  807. hnputl(bp.xid, conf.xid);
  808. hnputs(bp.secs, time(0)-conf.starttime);
  809. hnputs(bp.flags, 0);
  810. memmove(bp.optmagic, optmagic, 4);
  811. if(conf.hwatype >= 0 && conf.hwalen < sizeof(bp.chaddr)){
  812. memmove(bp.chaddr, conf.hwa, conf.hwalen);
  813. bp.hlen = conf.hwalen;
  814. bp.htype = conf.hwatype;
  815. }
  816. p = bp.optdata;
  817. p = optaddbyte(p, ODtype, type);
  818. p = optadd(p, ODclientid, conf.cid, conf.cidlen);
  819. switch(type) {
  820. default:
  821. sysfatal("dhcpsend: unknown message type: %d", type);
  822. case Discover:
  823. ipmove(up->raddr, IPv4bcast); // broadcast
  824. if(*conf.hostname && sendhostname)
  825. p = optaddstr(p, OBhostname, conf.hostname);
  826. if(plan9){
  827. n = snprint((char*)vendor, sizeof(vendor), "plan9_%s", conf.cputype);
  828. p = optaddvec(p, ODvendorclass, vendor, n);
  829. }
  830. p = optaddvec(p, ODparams, requested, nrequested);
  831. if(validip(conf.laddr))
  832. p = optaddaddr(p, ODipaddr, conf.laddr);
  833. break;
  834. case Request:
  835. switch(conf.state){
  836. case Srenewing:
  837. ipmove(up->raddr, conf.server);
  838. v6tov4(bp.ciaddr, conf.laddr);
  839. break;
  840. case Srebinding:
  841. ipmove(up->raddr, IPv4bcast); // broadcast
  842. v6tov4(bp.ciaddr, conf.laddr);
  843. break;
  844. case Srequesting:
  845. ipmove(up->raddr, IPv4bcast); // broadcast
  846. p = optaddaddr(p, ODipaddr, conf.laddr);
  847. p = optaddaddr(p, ODserverid, conf.server);
  848. break;
  849. }
  850. p = optaddulong(p, ODlease, conf.offered);
  851. if(plan9){
  852. n = snprint((char*)vendor, sizeof(vendor), "plan9_%s", conf.cputype);
  853. p = optaddvec(p, ODvendorclass, vendor, n);
  854. }
  855. p = optaddvec(p, ODparams, requested, nrequested);
  856. if(*conf.hostname && sendhostname)
  857. p = optaddstr(p, OBhostname, conf.hostname);
  858. break;
  859. case Release:
  860. ipmove(up->raddr, conf.server);
  861. v6tov4(bp.ciaddr, conf.laddr);
  862. p = optaddaddr(p, ODipaddr, conf.laddr);
  863. p = optaddaddr(p, ODserverid, conf.server);
  864. break;
  865. }
  866. *p++ = OBend;
  867. n = p - (uchar*)&bp;
  868. USED(n);
  869. /*
  870. * We use a maximum size DHCP packet to survive the
  871. * All_Aboard NAT package from Internet Share. It
  872. * always replies to DHCP requests with a packet of the
  873. * same size, so if the request is too short the reply
  874. * is truncated.
  875. */
  876. if(write(conf.fd, &bp, sizeof(bp)) != sizeof(bp))
  877. fprint(2, "dhcpsend: write failed: %r\n");
  878. }
  879. void
  880. dhcprecv(void)
  881. {
  882. uchar buf[8000];
  883. Bootp *bp;
  884. int i, n, type;
  885. ulong lease;
  886. char err[ERRMAX];
  887. uchar vopts[256];
  888. alarm(1000);
  889. n = read(conf.fd, buf, sizeof(buf));
  890. alarm(0);
  891. if(n < 0){
  892. errstr(err, sizeof err);
  893. if(strstr(err, "interrupt") == nil)
  894. fprint(2, "ipconfig: bad read: %s\n", err);
  895. else
  896. DEBUG("read timed out\n");
  897. return;
  898. }
  899. bp = parsebootp(buf, n);
  900. if(bp == 0) {
  901. DEBUG("parsebootp failed: dropping packet\n");
  902. return;
  903. }
  904. type = optgetbyte(bp->optdata, ODtype);
  905. switch(type) {
  906. default:
  907. fprint(2, "%s: unknown type: %d\n", argv0, type);
  908. break;
  909. case Offer:
  910. DEBUG("got offer from %V ", bp->siaddr);
  911. if(conf.state != Sselecting){
  912. DEBUG("\n");
  913. break;
  914. }
  915. lease = optgetulong(bp->optdata, ODlease);
  916. if(lease == 0){
  917. /*
  918. * The All_Aboard NAT package from Internet Share doesn't
  919. * give a lease time, so we have to assume one.
  920. */
  921. fprint(2, "%s: Offer with %lud lease, using %d\n", argv0, lease, MinLease);
  922. lease = MinLease;
  923. }
  924. DEBUG("lease=%lud ", lease);
  925. if(!optgetaddr(bp->optdata, ODserverid, conf.server)) {
  926. fprint(2, "%s: Offer from server with invalid serverid\n", argv0);
  927. break;
  928. }
  929. v4tov6(conf.laddr, bp->yiaddr);
  930. memmove(conf.sname, bp->sname, sizeof(conf.sname));
  931. conf.sname[sizeof(conf.sname)-1] = 0;
  932. DEBUG("server=%I sname=%s\n", conf.server, conf.sname);
  933. conf.offered = lease;
  934. conf.state = Srequesting;
  935. dhcpsend(Request);
  936. conf.resend = 0;
  937. conf.timeout = time(0) + 4;
  938. break;
  939. case Ack:
  940. DEBUG("got ack from %V ", bp->siaddr);
  941. if(conf.state != Srequesting)
  942. if(conf.state != Srenewing)
  943. if(conf.state != Srebinding)
  944. break;
  945. // ignore a bad lease
  946. lease = optgetulong(bp->optdata, ODlease);
  947. if(lease == 0){
  948. /*
  949. * The All_Aboard NAT package from Internet Share doesn't
  950. * give a lease time, so we have to assume one.
  951. */
  952. fprint(2, "%s: Ack with %lud lease, using %d\n", argv0, lease, MinLease);
  953. lease = MinLease;
  954. }
  955. DEBUG("lease=%lud ", lease);
  956. // address and mask
  957. if(!validip(conf.laddr) || !Oflag)
  958. v4tov6(conf.laddr, bp->yiaddr);
  959. if(!validip(conf.mask) || !Oflag){
  960. if(!optgetaddr(bp->optdata, OBmask, conf.mask))
  961. ipmove(conf.mask, IPnoaddr);
  962. }
  963. DEBUG("ipaddr=%I ipmask=%M ", conf.laddr, conf.mask);
  964. // get a router address either from the router option
  965. // or from the router that forwarded the dhcp packet
  966. if(!validip(conf.gaddr) || !Oflag){
  967. if(optgetaddr(bp->optdata, OBrouter, conf.gaddr)){
  968. DEBUG("ipgw=%I ", conf.gaddr);
  969. } else {
  970. if(memcmp(bp->giaddr, IPnoaddr+IPv4off, IPv4addrlen) != 0){
  971. v4tov6(conf.gaddr, bp->giaddr);
  972. DEBUG("giaddr=%I ", conf.gaddr);
  973. }
  974. }
  975. }
  976. else
  977. DEBUG("ipgw=%I ", conf.gaddr);
  978. // get dns servers
  979. memset(conf.dns, 0, sizeof(conf.dns));
  980. n = optgetaddrs(bp->optdata, OBdnserver, conf.dns,
  981. sizeof(conf.dns)/IPaddrlen);
  982. for(i = 0; i < n; i++)
  983. DEBUG("dns=%I ", conf.dns+i*IPaddrlen);
  984. // get ntp servers
  985. memset(conf.ntp, 0, sizeof(conf.ntp));
  986. n = optgetaddrs(bp->optdata, OBntpserver, conf.ntp,
  987. sizeof(conf.ntp)/IPaddrlen);
  988. for(i = 0; i < n; i++)
  989. DEBUG("ntp=%I ", conf.ntp+i*IPaddrlen);
  990. // get names
  991. optgetstr(bp->optdata, OBhostname, conf.hostname, sizeof(conf.hostname));
  992. optgetstr(bp->optdata, OBdomainname, conf.domainname, sizeof(conf.domainname));
  993. // get anything else we asked for
  994. getoptions(bp->optdata);
  995. // get plan9 specific options
  996. n = optgetvec(bp->optdata, OBvendorinfo, vopts, sizeof(vopts)-1);
  997. if(n > 0 && parseoptions(vopts, n) == 0){
  998. if(validip(conf.fs) && Oflag)
  999. n = 1;
  1000. else
  1001. n = optgetaddrs(vopts, OP9fs, conf.fs, 2);
  1002. for(i = 0; i < n; i++)
  1003. DEBUG("fs=%I ", conf.fs+i*IPaddrlen);
  1004. if(validip(conf.auth) && Oflag)
  1005. n = 1;
  1006. else
  1007. n = optgetaddrs(vopts, OP9auth, conf.auth, 2);
  1008. for(i = 0; i < n; i++)
  1009. DEBUG("auth=%I ", conf.auth+i*IPaddrlen);
  1010. }
  1011. conf.lease = lease;
  1012. conf.state = Sbound;
  1013. DEBUG("server=%I sname=%s\n", conf.server, conf.sname);
  1014. break;
  1015. case Nak:
  1016. conf.state = Sinit;
  1017. fprint(2, "%s: recved dhcpnak on %s\n", argv0, conf.mpoint);
  1018. break;
  1019. }
  1020. }
  1021. int
  1022. openlisten()
  1023. {
  1024. int fd, cfd;
  1025. char data[128];
  1026. char devdir[40];
  1027. int n;
  1028. if(validip(conf.laddr)
  1029. && (conf.state == Srenewing || conf.state == Srebinding))
  1030. sprint(data, "%s/udp!%I!68", conf.mpoint, conf.laddr);
  1031. else
  1032. sprint(data, "%s/udp!*!68", conf.mpoint);
  1033. for(n=0;;n++) {
  1034. cfd = announce(data, devdir);
  1035. if(cfd >= 0)
  1036. break;
  1037. if(!noconfig)
  1038. sysfatal("can't announce for dhcp: %r");
  1039. // might be another client - wait and try again
  1040. fprint(2, "%s: can't announce: %r\n", argv0);
  1041. sleep((nrand(10)+1)*1000);
  1042. if(n > 10)
  1043. return -1;
  1044. }
  1045. if(fprint(cfd, "headers") < 0)
  1046. sysfatal("can't set header mode: %r");
  1047. fprint(cfd, "oldheaders");
  1048. sprint(data, "%s/data", devdir);
  1049. fd = open(data, ORDWR);
  1050. if(fd < 0)
  1051. sysfatal("open udp data: %r");
  1052. close(cfd);
  1053. return fd;
  1054. }
  1055. uchar*
  1056. optadd(uchar *p, int op, void *d, int n)
  1057. {
  1058. p[0] = op;
  1059. p[1] = n;
  1060. memmove(p+2, d, n);
  1061. return p+n+2;
  1062. }
  1063. uchar*
  1064. optaddbyte(uchar *p, int op, int b)
  1065. {
  1066. p[0] = op;
  1067. p[1] = 1;
  1068. p[2] = b;
  1069. return p+3;
  1070. }
  1071. uchar*
  1072. optaddulong(uchar *p, int op, ulong x)
  1073. {
  1074. p[0] = op;
  1075. p[1] = 4;
  1076. hnputl(p+2, x);
  1077. return p+6;
  1078. }
  1079. uchar *
  1080. optaddaddr(uchar *p, int op, uchar *ip)
  1081. {
  1082. p[0] = op;
  1083. p[1] = 4;
  1084. v6tov4(p+2, ip);
  1085. return p+6;
  1086. }
  1087. uchar *
  1088. optaddvec(uchar *p, int op, uchar *v, int n)
  1089. {
  1090. p[0] = op;
  1091. p[1] = n;
  1092. memmove(p+2, v, n);
  1093. return p+2+n;
  1094. }
  1095. uchar *
  1096. optaddstr(uchar *p, int op, char *v)
  1097. {
  1098. int n;
  1099. n = strlen(v)+1; // microsoft leaves on the null, so we do too
  1100. p[0] = op;
  1101. p[1] = n;
  1102. memmove(p+2, v, n);
  1103. return p+2+n;
  1104. }
  1105. uchar*
  1106. optget(uchar *p, int op, int *np)
  1107. {
  1108. int len, code;
  1109. for(;;) {
  1110. code = *p++;
  1111. if(code == OBpad)
  1112. continue;
  1113. if(code == OBend)
  1114. return 0;
  1115. len = *p++;
  1116. if(code != op) {
  1117. p += len;
  1118. continue;
  1119. }
  1120. if(np != nil){
  1121. if(*np > len)
  1122. return 0;
  1123. *np = len;
  1124. }
  1125. return p;
  1126. }
  1127. }
  1128. int
  1129. optgetbyte(uchar *p, int op)
  1130. {
  1131. int len;
  1132. len = 1;
  1133. p = optget(p, op, &len);
  1134. if(p == 0)
  1135. return 0;
  1136. return *p;
  1137. }
  1138. ulong
  1139. optgetulong(uchar *p, int op)
  1140. {
  1141. int len;
  1142. len = 4;
  1143. p = optget(p, op, &len);
  1144. if(p == 0)
  1145. return 0;
  1146. return nhgetl(p);
  1147. }
  1148. int
  1149. optgetaddr(uchar *p, int op, uchar *ip)
  1150. {
  1151. int len;
  1152. len = 4;
  1153. p = optget(p, op, &len);
  1154. if(p == 0)
  1155. return 0;
  1156. v4tov6(ip, p);
  1157. return 1;
  1158. }
  1159. int
  1160. optgetaddrs(uchar *p, int op, uchar *ip, int n)
  1161. {
  1162. int len, i;
  1163. len = 4;
  1164. p = optget(p, op, &len);
  1165. if(p == 0)
  1166. return 0;
  1167. len /= IPv4addrlen;
  1168. if(len > n)
  1169. len = n;
  1170. for(i = 0; i < len; i++)
  1171. v4tov6(&ip[i*IPaddrlen], &p[i*IPv4addrlen]);
  1172. return i;
  1173. }
  1174. int
  1175. optgetvec(uchar *p, int op, uchar *v, int n)
  1176. {
  1177. int len;
  1178. len = 1;
  1179. p = optget(p, op, &len);
  1180. if(p == 0)
  1181. return 0;
  1182. if(len > n)
  1183. len = n;
  1184. memmove(v, p, len);
  1185. return len;
  1186. }
  1187. int
  1188. optgetstr(uchar *p, int op, char *s, int n)
  1189. {
  1190. int len;
  1191. len = 1;
  1192. p = optget(p, op, &len);
  1193. if(p == 0)
  1194. return 0;
  1195. if(len >= n)
  1196. len = n-1;
  1197. memmove(s, p, len);
  1198. s[len] = 0;
  1199. return len;
  1200. }
  1201. // sanity check options area
  1202. // - options don't overflow packet
  1203. // - options end with an OBend
  1204. int
  1205. parseoptions(uchar *p, int n)
  1206. {
  1207. int code, len;
  1208. int nin = n;
  1209. while(n>0) {
  1210. code = *p++;
  1211. n--;
  1212. if(code == OBpad)
  1213. continue;
  1214. if(code == OBend)
  1215. return 0;
  1216. if(n == 0) {
  1217. fprint(2, "%s: parse: bad option: 0x%ux: truncated: opt length = %d\n",
  1218. argv0, code, nin);
  1219. return -1;
  1220. }
  1221. len = *p++;
  1222. n--;
  1223. if(len > n) {
  1224. fprint(2, "%s: parse: bad option: 0x%ux: %d > %d: opt length = %d\n",
  1225. argv0, code, len, n, nin);
  1226. return -1;
  1227. }
  1228. p += len;
  1229. n -= len;
  1230. }
  1231. // make sure packet ends with an OBend all the optget code
  1232. *p = OBend;
  1233. return 0;
  1234. }
  1235. // sanity check received packet:
  1236. // - magic is dhcp magic
  1237. // - options don't overflow packet
  1238. Bootp *
  1239. parsebootp(uchar *p, int n)
  1240. {
  1241. Bootp *bp;
  1242. bp = (Bootp*)p;
  1243. if(n < bp->optmagic - p) {
  1244. fprint(2, "%s: parse: short bootp packet\n", argv0);
  1245. return nil;
  1246. }
  1247. if(conf.xid != nhgetl(bp->xid))
  1248. return nil;
  1249. if(bp->op != Bootreply) {
  1250. fprint(2, "%s: parse: bad op\n", argv0);
  1251. return nil;
  1252. }
  1253. n -= bp->optmagic - p;
  1254. p = bp->optmagic;
  1255. if(n < 4) {
  1256. fprint(2, "%s: parse: not option data\n", argv0);
  1257. return nil;
  1258. }
  1259. if(memcmp(optmagic, p, 4) != 0) {
  1260. fprint(2, "%s: parse: bad opt magic %ux %ux %ux %ux\n", argv0,
  1261. p[0], p[1], p[2], p[3]);
  1262. return nil;
  1263. }
  1264. p += 4;
  1265. n -= 4;
  1266. if(parseoptions(p, n) < 0)
  1267. return nil;
  1268. return bp;
  1269. }
  1270. // write out an ndb entry
  1271. void
  1272. writendb(char *s, int n, int append)
  1273. {
  1274. char file[64];
  1275. int fd;
  1276. snprint(file, sizeof file, "%s/ndb", conf.mpoint);
  1277. if(append){
  1278. fd = open(file, OWRITE);
  1279. seek(fd, 0, 2);
  1280. } else
  1281. fd = open(file, OWRITE|OTRUNC);
  1282. write(fd, s, n);
  1283. close(fd);
  1284. }
  1285. // put server addresses into the ndb entry
  1286. char*
  1287. putaddrs(char *p, char *e, char *attr, uchar *a, int len)
  1288. {
  1289. int i;
  1290. for(i = 0; i < len; i += IPaddrlen, a += IPaddrlen){
  1291. if(!validip(a))
  1292. break;
  1293. p = seprint(p, e, "%s=%I\n", attr, a);
  1294. }
  1295. return p;
  1296. }
  1297. // make an ndb entry and put it into /net/ndb for the servers to see
  1298. void
  1299. putndb(void)
  1300. {
  1301. char buf[1024];
  1302. char *p, *e, *np;
  1303. int append;
  1304. e = buf + sizeof(buf);
  1305. p = buf;
  1306. if(getndb() == 0){
  1307. append = 1;
  1308. } else {
  1309. append = 0;
  1310. p = seprint(p, e, "ip=%I ipmask=%M ipgw=%I\n",
  1311. conf.laddr, conf.mask, conf.gaddr);
  1312. }
  1313. if(np = strchr(conf.hostname, '.')){
  1314. if(*conf.domainname == 0)
  1315. strcpy(conf.domainname, np+1);
  1316. *np = 0;
  1317. }
  1318. if(*conf.hostname)
  1319. p = seprint(p, e, "\tsys=%s\n", conf.hostname);
  1320. if(*conf.domainname)
  1321. p = seprint(p, e, "\tdom=%s.%s\n", conf.hostname, conf.domainname);
  1322. if(validip(conf.fs))
  1323. p = putaddrs(p, e, "\tfs", conf.fs, sizeof(conf.fs));
  1324. if(validip(conf.auth))
  1325. p = putaddrs(p, e, "\tauth", conf.auth, sizeof(conf.auth));
  1326. if(validip(conf.dns))
  1327. p = putaddrs(p, e, "\tdns", conf.dns, sizeof(conf.dns));
  1328. if(validip(conf.ntp))
  1329. p = putaddrs(p, e, "\tntp", conf.ntp, sizeof(conf.ntp));
  1330. if(ndboptions)
  1331. p = seprint(p, e, "%s\n", ndboptions);
  1332. if(p > buf)
  1333. writendb(buf, p-buf, append);
  1334. }
  1335. // get an ndb entry someone else wrote
  1336. int
  1337. getndb(void)
  1338. {
  1339. char buf[1024];
  1340. int fd;
  1341. int n;
  1342. char *p;
  1343. snprint(buf, sizeof buf, "%s/ndb", conf.mpoint);
  1344. fd = open(buf, OREAD);
  1345. n = read(fd, buf, sizeof(buf)-1);
  1346. close(fd);
  1347. if(n <= 0)
  1348. return -1;
  1349. buf[n] = 0;
  1350. p = strstr(buf, "ip=");
  1351. if(p == nil)
  1352. return -1;
  1353. parseip(conf.laddr, p+3);
  1354. return 0;
  1355. }
  1356. // tell a server to refresh
  1357. void
  1358. tweakserver(char *server)
  1359. {
  1360. char file[64];
  1361. int fd;
  1362. snprint(file, sizeof(file), "%s/%s", conf.mpoint, server);
  1363. fd = open(file, ORDWR);
  1364. if(fd < 0)
  1365. return;
  1366. fprint(fd, "refresh");
  1367. close(fd);
  1368. }
  1369. // tell all servers to refresh their information
  1370. void
  1371. tweakservers(void)
  1372. {
  1373. tweakserver("dns");
  1374. tweakserver("cs");
  1375. }
  1376. // return number of networks
  1377. int
  1378. nipifcs(char *net)
  1379. {
  1380. Ipifc *nifc;
  1381. Iplifc *lifc;
  1382. int n;
  1383. n = 0;
  1384. ifc = readipifc(net, ifc, -1);
  1385. for(nifc = ifc; nifc != nil; nifc = nifc->next){
  1386. /*
  1387. * ignore loopback devices when trying to
  1388. * figure out if we're the primary interface.
  1389. */
  1390. if(strcmp(nifc->dev, "/dev/null") != 0)
  1391. for(lifc = nifc->lifc; lifc != nil; lifc = lifc->next)
  1392. if(validip(lifc->ip)){
  1393. n++;
  1394. break;
  1395. }
  1396. if(strcmp(nifc->dev, conf.dev) == 0)
  1397. myifc = nifc->index;
  1398. }
  1399. return n;
  1400. }
  1401. // return true if this is a valid v4 address
  1402. int
  1403. validip(uchar *addr)
  1404. {
  1405. return ipcmp(addr, IPnoaddr) != 0 && ipcmp(addr, v4prefix) != 0;
  1406. }
  1407. char *verbs[] = {
  1408. [Vadd] "add",
  1409. [Vremove] "remove",
  1410. [Vunbind] "unbind",
  1411. [Vether] "ether",
  1412. [Vloopback] "loopback",
  1413. };
  1414. // look for an action
  1415. int
  1416. parseverb(char *name)
  1417. {
  1418. int i;
  1419. for(i = 0; i < nelem(verbs); i++){
  1420. if(verbs[i] == 0)
  1421. continue;
  1422. if(strcmp(name, verbs[i]) == 0)
  1423. return i;
  1424. }
  1425. return -1;
  1426. }
  1427. // get everything out of ndb
  1428. void
  1429. ndbconfig(void)
  1430. {
  1431. Ndb *db;
  1432. Ndbtuple *t, *nt;
  1433. char etheraddr[32];
  1434. char *attrs[10];
  1435. int nattr;
  1436. int ndns = 0;
  1437. int nfs = 0;
  1438. int nauth = 0;
  1439. db = ndbopen(0);
  1440. if(db == nil)
  1441. sysfatal("can't open ndb: %r");
  1442. if(strcmp(conf.type, "ether") != 0 || myetheraddr(conf.hwa, conf.dev) != 0)
  1443. sysfatal("can't read hardware address");
  1444. sprint(etheraddr, "%E", conf.hwa);
  1445. nattr = 0;
  1446. attrs[nattr++] = "ip";
  1447. attrs[nattr++] = "ipmask";
  1448. attrs[nattr++] = "ipgw";
  1449. attrs[nattr++] = "@dns";
  1450. attrs[nattr++] = "@ntp";
  1451. attrs[nattr++] = "@fs";
  1452. attrs[nattr++] = "@auth";
  1453. attrs[nattr] = nil;
  1454. t = ndbipinfo(db, "ether", etheraddr, attrs, nattr);
  1455. for(nt = t; nt != nil; nt = nt->entry){
  1456. if(strcmp(nt->attr, "ip") == 0){
  1457. parseip(conf.laddr, nt->val);
  1458. } else if(strcmp(nt->attr, "ipmask") == 0){
  1459. parseipmask(conf.mask, nt->val);
  1460. } else if(strcmp(nt->attr, "ipgw") == 0){
  1461. parseip(conf.gaddr, nt->val);
  1462. } else if(ndns < 2 && strcmp(nt->attr, "dns") == 0){
  1463. parseip(conf.dns+IPaddrlen*ndns, nt->val);
  1464. } else if(strcmp(nt->attr, "ntp") == 0){
  1465. parseip(conf.ntp, nt->val);
  1466. } else if(nfs < 2 && strcmp(nt->attr, "fs") == 0){
  1467. parseip(conf.fs+IPaddrlen*nfs, nt->val);
  1468. } else if(nauth < 2 && strcmp(nt->attr, "auth") == 0){
  1469. parseip(conf.auth+IPaddrlen*nauth, nt->val);
  1470. }
  1471. }
  1472. ndbfree(t);
  1473. if(!validip(conf.laddr))
  1474. sysfatal("address not found in ndb");
  1475. }
  1476. int
  1477. addoption(char *opt)
  1478. {
  1479. int i;
  1480. Option *o;
  1481. if(opt == nil)
  1482. return -1;
  1483. for(o = option; o < &option[nelem(option)]; o++){
  1484. if(o->name == nil)
  1485. continue;
  1486. if(strcmp(opt, o->name) == 0){
  1487. i = o-option;
  1488. if(!memchr(requested, i, nrequested))
  1489. if(nrequested < nelem(requested))
  1490. requested[nrequested++] = i;
  1491. return 0;
  1492. }
  1493. }
  1494. return -1;
  1495. }
  1496. char*
  1497. optgetx(uchar *p, uchar opt)
  1498. {
  1499. Option *o;
  1500. int i, n;
  1501. ulong x;
  1502. char *s, *ns;
  1503. uchar ip[IPaddrlen];
  1504. uchar ips[16*IPaddrlen];
  1505. char str[256];
  1506. uchar vec[256];
  1507. o = &option[opt];
  1508. if(o->name == nil)
  1509. return nil;
  1510. s = nil;
  1511. switch(o->type){
  1512. case Taddr:
  1513. if(optgetaddr(p, opt, ip))
  1514. s = smprint("%s=%I", o->name, ip);
  1515. break;
  1516. case Taddrs:
  1517. n = optgetaddrs(p, opt, ips, 16);
  1518. if(n > 0)
  1519. s = smprint("%s=%I", o->name, ips);
  1520. for(i = 1; i < n; i++){
  1521. ns = smprint("%s %s=%I", s, o->name, &ips[i*IPaddrlen]);
  1522. free(s);
  1523. s = ns;
  1524. }
  1525. break;
  1526. case Tulong:
  1527. x = optgetulong(p, opt);
  1528. if(x != 0)
  1529. s = smprint("%s=%lud", o->name, x);
  1530. break;
  1531. case Tbyte:
  1532. x = optgetbyte(p, opt);
  1533. if(x != 0)
  1534. s = smprint("%s=%lud", o->name, x);
  1535. break;
  1536. case Tstr:
  1537. if(optgetstr(p, opt, str, sizeof(str)))
  1538. s = smprint("%s=%s", o->name, str);
  1539. break;
  1540. case Tvec:
  1541. n = optgetvec(p, opt, vec, sizeof(vec));
  1542. if(n > 0)
  1543. s = smprint("%s=%.*H", o->name, n, vec);
  1544. break;
  1545. }
  1546. return s;
  1547. }
  1548. void
  1549. getoptions(uchar *p)
  1550. {
  1551. int i;
  1552. char *s, *t;
  1553. for(i = nelem(defrequested); i < nrequested; i++){
  1554. s = optgetx(p, requested[i]);
  1555. if(s != nil)
  1556. DEBUG("%s ", s);
  1557. if(ndboptions == nil)
  1558. ndboptions = smprint("\t%s", s);
  1559. else{
  1560. t = ndboptions;
  1561. ndboptions = smprint("\t%s%s", s, ndboptions);
  1562. free(t);
  1563. }
  1564. free(s);
  1565. }
  1566. }