ipconfig.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  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. fprint(cfd, "add 0 0 %I", gaddr);
  520. close(cfd);
  521. }
  522. // create a client id
  523. void
  524. mkclientid(void)
  525. {
  526. if(strcmp(conf.type, "ether") == 0)
  527. if(myetheraddr(conf.hwa, conf.dev) == 0){
  528. conf.hwalen = 6;
  529. conf.hwatype = 1;
  530. conf.cid[0] = conf.hwatype;
  531. memmove(&conf.cid[1], conf.hwa, conf.hwalen);
  532. conf.cidlen = conf.hwalen+1;
  533. } else {
  534. conf.hwatype = -1;
  535. snprint((char*)conf.cid, sizeof(conf.cid), "plan9_%ld.%d", lrand(), getpid());
  536. conf.cidlen = strlen((char*)conf.cid);
  537. }
  538. }
  539. // bind ip into the namespace
  540. void
  541. lookforip(char *net)
  542. {
  543. char proto[64];
  544. snprint(proto, sizeof(proto), "%s/ipifc", net);
  545. if(access(proto, 0) == 0)
  546. return;
  547. sysfatal("no ip stack bound onto %s", net);
  548. }
  549. // send some ctls to a device
  550. void
  551. controldevice(void)
  552. {
  553. char ctlfile[256];
  554. int fd;
  555. Ctl *cp;
  556. if(firstctl == nil)
  557. return;
  558. if(strcmp(conf.type, "ether") == 0)
  559. snprint(ctlfile, sizeof(ctlfile), "%s/clone", conf.dev);
  560. else
  561. return;
  562. fd = open(ctlfile, ORDWR);
  563. if(fd < 0)
  564. sysfatal("can't open %s", ctlfile);
  565. for(cp = firstctl; cp != nil; cp = cp->next){
  566. if(write(fd, cp->ctl, strlen(cp->ctl)) < 0)
  567. sysfatal("ctl message %s: %r", cp->ctl);
  568. seek(fd, 0, 0);
  569. }
  570. }
  571. // bind an ip stack to a device, leave the control channel open
  572. void
  573. binddevice(void)
  574. {
  575. char buf[256];
  576. if(myifc < 0){
  577. // get a new ip interface
  578. snprint(buf, sizeof(buf), "%s/ipifc/clone", conf.mpoint);
  579. conf.cfd = open(buf, ORDWR);
  580. if(conf.cfd < 0)
  581. sysfatal("opening %s/ipifc/clone: %r", conf.mpoint);
  582. // specify the medium as an ethernet, and bind the interface to it
  583. if(fprint(conf.cfd, "bind %s %s", conf.type, conf.dev) < 0)
  584. sysfatal("binding device: %r");
  585. } else {
  586. // open the old interface
  587. snprint(buf, sizeof(buf), "%s/ipifc/%d/ctl", conf.mpoint, myifc);
  588. conf.cfd = open(buf, ORDWR);
  589. if(conf.cfd < 0)
  590. sysfatal("opening %s/ipifc/%d/ctl: %r", conf.mpoint, myifc);
  591. }
  592. }
  593. // add a logical interface to the ip stack
  594. int
  595. ipconfig(void)
  596. {
  597. char buf[256];
  598. int n;
  599. if(!validip(conf.laddr))
  600. return -1;
  601. n = sprint(buf, "add");
  602. n += snprint(buf+n, sizeof(buf)-n, " %I", conf.laddr);
  603. if(!validip(conf.mask))
  604. ipmove(conf.mask, defmask(conf.laddr));
  605. n += snprint(buf+n, sizeof(buf)-n, " %I", conf.mask);
  606. if(validip(conf.raddr)){
  607. n += snprint(buf+n, sizeof(buf)-n, " %I", conf.raddr);
  608. if(conf.mtu != 0)
  609. n += snprint(buf+n, sizeof(buf)-n, " %d", conf.mtu);
  610. }
  611. if(write(conf.cfd, buf, n) < 0){
  612. fprint(2, "ipconfig: write(%s): %r\n", buf);
  613. return -1;
  614. }
  615. if(beprimary==1 && validip(conf.gaddr))
  616. adddefroute(conf.mpoint, conf.gaddr);
  617. return 0;
  618. }
  619. // remove a logical interface to the ip stack
  620. void
  621. ipunconfig(void)
  622. {
  623. char buf[256];
  624. int n;
  625. if(!validip(conf.laddr))
  626. return;
  627. DEBUG("couldn't renew IP lease, releasing %I\n", conf.laddr);
  628. n = sprint(buf, "remove");
  629. n += snprint(buf+n, sizeof(buf)-n, " %I", conf.laddr);
  630. if(!validip(conf.mask))
  631. ipmove(conf.mask, defmask(conf.laddr));
  632. n += snprint(buf+n, sizeof(buf)-n, " %I", conf.mask);
  633. write(conf.cfd, buf, n);
  634. ipmove(conf.laddr, IPnoaddr);
  635. ipmove(conf.raddr, IPnoaddr);
  636. ipmove(conf.mask, IPnoaddr);
  637. // forget configuration info
  638. if(beprimary==1)
  639. writendb("", 0, 0);
  640. }
  641. void
  642. ding(void*, char *msg)
  643. {
  644. if(strstr(msg, "alarm"))
  645. noted(NCONT);
  646. noted(NDFLT);
  647. }
  648. void
  649. dhcpquery(int needconfig, int startstate)
  650. {
  651. if(needconfig)
  652. fprint(conf.cfd, "add %I %I", IPnoaddr, IPnoaddr);
  653. conf.fd = openlisten();
  654. if(conf.fd < 0){
  655. conf.state = Sinit;
  656. return;
  657. }
  658. notify(ding);
  659. // try dhcp for 10 seconds
  660. conf.xid = lrand();
  661. conf.starttime = time(0);
  662. conf.state = startstate;
  663. switch(startstate){
  664. case Sselecting:
  665. conf.offered = 0;
  666. dhcpsend(Discover);
  667. break;
  668. case Srenewing:
  669. dhcpsend(Request);
  670. break;
  671. default:
  672. sysfatal("internal error 0");
  673. }
  674. conf.resend = 0;
  675. conf.timeout = time(0) + 4;
  676. while(conf.state != Sbound){
  677. dhcprecv();
  678. if(dhcptimer() < 0)
  679. break;
  680. if(time(0) - conf.starttime > 10)
  681. break;
  682. }
  683. close(conf.fd);
  684. if(needconfig)
  685. fprint(conf.cfd, "remove %I %I", IPnoaddr, IPnoaddr);
  686. }
  687. #define HOUR (60*60)
  688. void
  689. dhcpwatch(int needconfig)
  690. {
  691. int secs, s;
  692. ulong t;
  693. if(nodhcpwatch)
  694. return;
  695. switch(rfork(RFPROC|RFFDG|RFNOWAIT|RFNOTEG)){
  696. default:
  697. return;
  698. case 0:
  699. break;
  700. }
  701. // keep trying to renew the lease
  702. for(;;){
  703. if(conf.lease == 0)
  704. secs = 5;
  705. else
  706. secs = conf.lease>>1;
  707. // avoid overflows
  708. for(s = secs; s > 0; s -= t){
  709. if(s > HOUR)
  710. t = HOUR;
  711. else
  712. t = s;
  713. sleep(t*1000);
  714. }
  715. if(conf.lease > 0){
  716. // during boot, the starttime can be bogus so avoid
  717. // spurious ipinconfig's
  718. t = time(0) - conf.starttime;
  719. if(t > (3*secs)/2)
  720. t = secs;
  721. if(t >= conf.lease){
  722. conf.lease = 0;
  723. if(!noconfig){
  724. ipunconfig();
  725. needconfig = 1;
  726. }
  727. } else
  728. conf.lease -= t;
  729. }
  730. dhcpquery(needconfig, needconfig ? Sselecting : Srenewing);
  731. if(needconfig && conf.state == Sbound){
  732. if(ipconfig() < 0)
  733. sysfatal("can't start ip: %r");
  734. needconfig = 0;
  735. // leave everything we've learned somewhere other procs can find it
  736. if(beprimary==1){
  737. putndb();
  738. tweakservers();
  739. }
  740. }
  741. }
  742. }
  743. int
  744. dhcptimer(void)
  745. {
  746. ulong now;
  747. now = time(0);
  748. if(now < conf.timeout)
  749. return 0;
  750. switch(conf.state) {
  751. default:
  752. sysfatal("dhcptimer: unknown state %d", conf.state);
  753. case Sinit:
  754. break;
  755. case Sselecting:
  756. dhcpsend(Discover);
  757. conf.timeout = now + 4;
  758. conf.resend++;
  759. if(conf.resend>5)
  760. goto err;
  761. break;
  762. case Srequesting:
  763. dhcpsend(Request);
  764. conf.timeout = now + 4;
  765. conf.resend++;
  766. if(conf.resend>5)
  767. goto err;
  768. break;
  769. case Srenewing:
  770. dhcpsend(Request);
  771. conf.timeout = now + 1;
  772. conf.resend++;
  773. if(conf.resend>3) {
  774. conf.state = Srebinding;
  775. conf.resend = 0;
  776. }
  777. break;
  778. case Srebinding:
  779. dhcpsend(Request);
  780. conf.timeout = now + 4;
  781. conf.resend++;
  782. if(conf.resend>5)
  783. goto err;
  784. break;
  785. case Sbound:
  786. break;
  787. }
  788. return 0;
  789. err:
  790. conf.state = Sinit;
  791. return -1;
  792. }
  793. void
  794. dhcpsend(int type)
  795. {
  796. Bootp bp;
  797. uchar *p;
  798. int n;
  799. uchar vendor[64];
  800. OUdphdr *up = (OUdphdr*)bp.udphdr;
  801. memset(&bp, 0, sizeof(bp));
  802. hnputs(up->rport, 67);
  803. bp.op = Bootrequest;
  804. hnputl(bp.xid, conf.xid);
  805. hnputs(bp.secs, time(0)-conf.starttime);
  806. hnputs(bp.flags, 0);
  807. memmove(bp.optmagic, optmagic, 4);
  808. if(conf.hwatype >= 0 && conf.hwalen < sizeof(bp.chaddr)){
  809. memmove(bp.chaddr, conf.hwa, conf.hwalen);
  810. bp.hlen = conf.hwalen;
  811. bp.htype = conf.hwatype;
  812. }
  813. p = bp.optdata;
  814. p = optaddbyte(p, ODtype, type);
  815. p = optadd(p, ODclientid, conf.cid, conf.cidlen);
  816. switch(type) {
  817. default:
  818. sysfatal("dhcpsend: unknown message type: %d", type);
  819. case Discover:
  820. ipmove(up->raddr, IPv4bcast); // broadcast
  821. if(*conf.hostname && sendhostname)
  822. p = optaddstr(p, OBhostname, conf.hostname);
  823. if(plan9){
  824. n = snprint((char*)vendor, sizeof(vendor), "plan9_%s", conf.cputype);
  825. p = optaddvec(p, ODvendorclass, vendor, n);
  826. }
  827. p = optaddvec(p, ODparams, requested, nrequested);
  828. if(validip(conf.laddr))
  829. p = optaddaddr(p, ODipaddr, conf.laddr);
  830. break;
  831. case Request:
  832. switch(conf.state){
  833. case Srenewing:
  834. ipmove(up->raddr, conf.server);
  835. v6tov4(bp.ciaddr, conf.laddr);
  836. break;
  837. case Srebinding:
  838. ipmove(up->raddr, IPv4bcast); // broadcast
  839. v6tov4(bp.ciaddr, conf.laddr);
  840. break;
  841. case Srequesting:
  842. ipmove(up->raddr, IPv4bcast); // broadcast
  843. p = optaddaddr(p, ODipaddr, conf.laddr);
  844. p = optaddaddr(p, ODserverid, conf.server);
  845. break;
  846. }
  847. p = optaddulong(p, ODlease, conf.offered);
  848. if(plan9){
  849. n = snprint((char*)vendor, sizeof(vendor), "plan9_%s", conf.cputype);
  850. p = optaddvec(p, ODvendorclass, vendor, n);
  851. }
  852. p = optaddvec(p, ODparams, requested, nrequested);
  853. if(*conf.hostname && sendhostname)
  854. p = optaddstr(p, OBhostname, conf.hostname);
  855. break;
  856. case Release:
  857. ipmove(up->raddr, conf.server);
  858. v6tov4(bp.ciaddr, conf.laddr);
  859. p = optaddaddr(p, ODipaddr, conf.laddr);
  860. p = optaddaddr(p, ODserverid, conf.server);
  861. break;
  862. }
  863. *p++ = OBend;
  864. n = p - (uchar*)&bp;
  865. USED(n);
  866. /*
  867. * We use a maximum size DHCP packet to survive the
  868. * All_Aboard NAT package from Internet Share. It
  869. * always replies to DHCP requests with a packet of the
  870. * same size, so if the request is too short the reply
  871. * is truncated.
  872. */
  873. if(write(conf.fd, &bp, sizeof(bp)) != sizeof(bp))
  874. fprint(2, "dhcpsend: write failed: %r\n");
  875. }
  876. void
  877. dhcprecv(void)
  878. {
  879. uchar buf[8000];
  880. Bootp *bp;
  881. int i, n, type;
  882. ulong lease;
  883. char err[ERRMAX];
  884. uchar vopts[256];
  885. alarm(1000);
  886. n = read(conf.fd, buf, sizeof(buf));
  887. alarm(0);
  888. if(n < 0){
  889. errstr(err, sizeof err);
  890. if(strstr(err, "interrupt") == nil)
  891. fprint(2, "ipconfig: bad read: %s\n", err);
  892. else
  893. DEBUG("read timed out\n");
  894. return;
  895. }
  896. bp = parsebootp(buf, n);
  897. if(bp == 0) {
  898. DEBUG("parsebootp failed: dropping packet\n");
  899. return;
  900. }
  901. type = optgetbyte(bp->optdata, ODtype);
  902. switch(type) {
  903. default:
  904. fprint(2, "%s: unknown type: %d\n", argv0, type);
  905. break;
  906. case Offer:
  907. DEBUG("got offer from %V ", bp->siaddr);
  908. if(conf.state != Sselecting){
  909. DEBUG("\n");
  910. break;
  911. }
  912. lease = optgetulong(bp->optdata, ODlease);
  913. if(lease == 0){
  914. /*
  915. * The All_Aboard NAT package from Internet Share doesn't
  916. * give a lease time, so we have to assume one.
  917. */
  918. fprint(2, "%s: Offer with %lud lease, using %d\n", argv0, lease, MinLease);
  919. lease = MinLease;
  920. }
  921. DEBUG("lease=%lud ", lease);
  922. if(!optgetaddr(bp->optdata, ODserverid, conf.server)) {
  923. fprint(2, "%s: Offer from server with invalid serverid\n", argv0);
  924. break;
  925. }
  926. v4tov6(conf.laddr, bp->yiaddr);
  927. memmove(conf.sname, bp->sname, sizeof(conf.sname));
  928. conf.sname[sizeof(conf.sname)-1] = 0;
  929. DEBUG("server=%I sname=%s\n", conf.server, conf.sname);
  930. conf.offered = lease;
  931. conf.state = Srequesting;
  932. dhcpsend(Request);
  933. conf.resend = 0;
  934. conf.timeout = time(0) + 4;
  935. break;
  936. case Ack:
  937. DEBUG("got ack from %V ", bp->siaddr);
  938. if(conf.state != Srequesting)
  939. if(conf.state != Srenewing)
  940. if(conf.state != Srebinding)
  941. break;
  942. // ignore a bad lease
  943. lease = optgetulong(bp->optdata, ODlease);
  944. if(lease == 0){
  945. /*
  946. * The All_Aboard NAT package from Internet Share doesn't
  947. * give a lease time, so we have to assume one.
  948. */
  949. fprint(2, "%s: Ack with %lud lease, using %d\n", argv0, lease, MinLease);
  950. lease = MinLease;
  951. }
  952. DEBUG("lease=%lud ", lease);
  953. // address and mask
  954. if(!validip(conf.laddr) || !Oflag)
  955. v4tov6(conf.laddr, bp->yiaddr);
  956. if(!validip(conf.mask) || !Oflag){
  957. if(!optgetaddr(bp->optdata, OBmask, conf.mask))
  958. ipmove(conf.mask, IPnoaddr);
  959. }
  960. DEBUG("ipaddr=%I ipmask=%M ", conf.laddr, conf.mask);
  961. // get a router address either from the router option
  962. // or from the router that forwarded the dhcp packet
  963. if(!validip(conf.gaddr) || !Oflag){
  964. if(optgetaddr(bp->optdata, OBrouter, conf.gaddr)){
  965. DEBUG("ipgw=%I ", conf.gaddr);
  966. } else {
  967. if(memcmp(bp->giaddr, IPnoaddr+IPv4off, IPv4addrlen) != 0){
  968. v4tov6(conf.gaddr, bp->giaddr);
  969. DEBUG("giaddr=%I ", conf.gaddr);
  970. }
  971. }
  972. }
  973. else
  974. DEBUG("ipgw=%I ", conf.gaddr);
  975. // get dns servers
  976. memset(conf.dns, 0, sizeof(conf.dns));
  977. n = optgetaddrs(bp->optdata, OBdnserver, conf.dns,
  978. sizeof(conf.dns)/IPaddrlen);
  979. for(i = 0; i < n; i++)
  980. DEBUG("dns=%I ", conf.dns+i*IPaddrlen);
  981. // get ntp servers
  982. memset(conf.ntp, 0, sizeof(conf.ntp));
  983. n = optgetaddrs(bp->optdata, OBntpserver, conf.ntp,
  984. sizeof(conf.ntp)/IPaddrlen);
  985. for(i = 0; i < n; i++)
  986. DEBUG("ntp=%I ", conf.ntp+i*IPaddrlen);
  987. // get names
  988. optgetstr(bp->optdata, OBhostname, conf.hostname, sizeof(conf.hostname));
  989. optgetstr(bp->optdata, OBdomainname, conf.domainname, sizeof(conf.domainname));
  990. // get anything else we asked for
  991. getoptions(bp->optdata);
  992. // get plan9 specific options
  993. n = optgetvec(bp->optdata, OBvendorinfo, vopts, sizeof(vopts)-1);
  994. if(n > 0 && parseoptions(vopts, n) == 0){
  995. if(validip(conf.fs) && Oflag)
  996. n = 1;
  997. else
  998. n = optgetaddrs(vopts, OP9fs, conf.fs, 2);
  999. for(i = 0; i < n; i++)
  1000. DEBUG("fs=%I ", conf.fs+i*IPaddrlen);
  1001. if(validip(conf.auth) && Oflag)
  1002. n = 1;
  1003. else
  1004. n = optgetaddrs(vopts, OP9auth, conf.auth, 2);
  1005. for(i = 0; i < n; i++)
  1006. DEBUG("auth=%I ", conf.auth+i*IPaddrlen);
  1007. }
  1008. conf.lease = lease;
  1009. conf.state = Sbound;
  1010. DEBUG("server=%I sname=%s\n", conf.server, conf.sname);
  1011. break;
  1012. case Nak:
  1013. conf.state = Sinit;
  1014. fprint(2, "%s: recved dhcpnak on %s\n", argv0, conf.mpoint);
  1015. break;
  1016. }
  1017. }
  1018. int
  1019. openlisten()
  1020. {
  1021. int fd, cfd;
  1022. char data[128];
  1023. char devdir[40];
  1024. int n;
  1025. if(validip(conf.laddr)
  1026. && (conf.state == Srenewing || conf.state == Srebinding))
  1027. sprint(data, "%s/udp!%I!68", conf.mpoint, conf.laddr);
  1028. else
  1029. sprint(data, "%s/udp!*!68", conf.mpoint);
  1030. for(n=0;;n++) {
  1031. cfd = announce(data, devdir);
  1032. if(cfd >= 0)
  1033. break;
  1034. if(!noconfig)
  1035. sysfatal("can't announce for dhcp: %r");
  1036. // might be another client - wait and try again
  1037. fprint(2, "%s: can't announce: %r\n", argv0);
  1038. sleep((nrand(10)+1)*1000);
  1039. if(n > 10)
  1040. return -1;
  1041. }
  1042. if(fprint(cfd, "headers") < 0)
  1043. sysfatal("can't set header mode: %r");
  1044. fprint(cfd, "oldheaders");
  1045. sprint(data, "%s/data", devdir);
  1046. fd = open(data, ORDWR);
  1047. if(fd < 0)
  1048. sysfatal("open udp data: %r");
  1049. close(cfd);
  1050. return fd;
  1051. }
  1052. uchar*
  1053. optadd(uchar *p, int op, void *d, int n)
  1054. {
  1055. p[0] = op;
  1056. p[1] = n;
  1057. memmove(p+2, d, n);
  1058. return p+n+2;
  1059. }
  1060. uchar*
  1061. optaddbyte(uchar *p, int op, int b)
  1062. {
  1063. p[0] = op;
  1064. p[1] = 1;
  1065. p[2] = b;
  1066. return p+3;
  1067. }
  1068. uchar*
  1069. optaddulong(uchar *p, int op, ulong x)
  1070. {
  1071. p[0] = op;
  1072. p[1] = 4;
  1073. hnputl(p+2, x);
  1074. return p+6;
  1075. }
  1076. uchar *
  1077. optaddaddr(uchar *p, int op, uchar *ip)
  1078. {
  1079. p[0] = op;
  1080. p[1] = 4;
  1081. v6tov4(p+2, ip);
  1082. return p+6;
  1083. }
  1084. uchar *
  1085. optaddvec(uchar *p, int op, uchar *v, int n)
  1086. {
  1087. p[0] = op;
  1088. p[1] = n;
  1089. memmove(p+2, v, n);
  1090. return p+2+n;
  1091. }
  1092. uchar *
  1093. optaddstr(uchar *p, int op, char *v)
  1094. {
  1095. int n;
  1096. n = strlen(v)+1; // microsoft leaves on the null, so we do too
  1097. p[0] = op;
  1098. p[1] = n;
  1099. memmove(p+2, v, n);
  1100. return p+2+n;
  1101. }
  1102. uchar*
  1103. optget(uchar *p, int op, int *np)
  1104. {
  1105. int len, code;
  1106. for(;;) {
  1107. code = *p++;
  1108. if(code == OBpad)
  1109. continue;
  1110. if(code == OBend)
  1111. return 0;
  1112. len = *p++;
  1113. if(code != op) {
  1114. p += len;
  1115. continue;
  1116. }
  1117. if(np != nil){
  1118. if(*np > len)
  1119. return 0;
  1120. *np = len;
  1121. }
  1122. return p;
  1123. }
  1124. }
  1125. int
  1126. optgetbyte(uchar *p, int op)
  1127. {
  1128. int len;
  1129. len = 1;
  1130. p = optget(p, op, &len);
  1131. if(p == 0)
  1132. return 0;
  1133. return *p;
  1134. }
  1135. ulong
  1136. optgetulong(uchar *p, int op)
  1137. {
  1138. int len;
  1139. len = 4;
  1140. p = optget(p, op, &len);
  1141. if(p == 0)
  1142. return 0;
  1143. return nhgetl(p);
  1144. }
  1145. int
  1146. optgetaddr(uchar *p, int op, uchar *ip)
  1147. {
  1148. int len;
  1149. len = 4;
  1150. p = optget(p, op, &len);
  1151. if(p == 0)
  1152. return 0;
  1153. v4tov6(ip, p);
  1154. return 1;
  1155. }
  1156. int
  1157. optgetaddrs(uchar *p, int op, uchar *ip, int n)
  1158. {
  1159. int len, i;
  1160. len = 4;
  1161. p = optget(p, op, &len);
  1162. if(p == 0)
  1163. return 0;
  1164. len /= IPv4addrlen;
  1165. if(len > n)
  1166. len = n;
  1167. for(i = 0; i < len; i++)
  1168. v4tov6(&ip[i*IPaddrlen], &p[i*IPv4addrlen]);
  1169. return i;
  1170. }
  1171. int
  1172. optgetvec(uchar *p, int op, uchar *v, int n)
  1173. {
  1174. int len;
  1175. len = 1;
  1176. p = optget(p, op, &len);
  1177. if(p == 0)
  1178. return 0;
  1179. if(len > n)
  1180. len = n;
  1181. memmove(v, p, len);
  1182. return len;
  1183. }
  1184. int
  1185. optgetstr(uchar *p, int op, char *s, int n)
  1186. {
  1187. int len;
  1188. len = 1;
  1189. p = optget(p, op, &len);
  1190. if(p == 0)
  1191. return 0;
  1192. if(len >= n)
  1193. len = n-1;
  1194. memmove(s, p, len);
  1195. s[len] = 0;
  1196. return len;
  1197. }
  1198. // sanity check options area
  1199. // - options don't overflow packet
  1200. // - options end with an OBend
  1201. int
  1202. parseoptions(uchar *p, int n)
  1203. {
  1204. int code, len;
  1205. int nin = n;
  1206. while(n>0) {
  1207. code = *p++;
  1208. n--;
  1209. if(code == OBpad)
  1210. continue;
  1211. if(code == OBend)
  1212. return 0;
  1213. if(n == 0) {
  1214. fprint(2, "%s: parse: bad option: 0x%ux: truncated: opt length = %d\n",
  1215. argv0, code, nin);
  1216. return -1;
  1217. }
  1218. len = *p++;
  1219. n--;
  1220. if(len > n) {
  1221. fprint(2, "%s: parse: bad option: 0x%ux: %d > %d: opt length = %d\n",
  1222. argv0, code, len, n, nin);
  1223. return -1;
  1224. }
  1225. p += len;
  1226. n -= len;
  1227. }
  1228. // make sure packet ends with an OBend all the optget code
  1229. *p = OBend;
  1230. return 0;
  1231. }
  1232. // sanity check received packet:
  1233. // - magic is dhcp magic
  1234. // - options don't overflow packet
  1235. Bootp *
  1236. parsebootp(uchar *p, int n)
  1237. {
  1238. Bootp *bp;
  1239. bp = (Bootp*)p;
  1240. if(n < bp->optmagic - p) {
  1241. fprint(2, "%s: parse: short bootp packet\n", argv0);
  1242. return nil;
  1243. }
  1244. if(conf.xid != nhgetl(bp->xid))
  1245. return nil;
  1246. if(bp->op != Bootreply) {
  1247. fprint(2, "%s: parse: bad op\n", argv0);
  1248. return nil;
  1249. }
  1250. n -= bp->optmagic - p;
  1251. p = bp->optmagic;
  1252. if(n < 4) {
  1253. fprint(2, "%s: parse: not option data\n", argv0);
  1254. return nil;
  1255. }
  1256. if(memcmp(optmagic, p, 4) != 0) {
  1257. fprint(2, "%s: parse: bad opt magic %ux %ux %ux %ux\n", argv0,
  1258. p[0], p[1], p[2], p[3]);
  1259. return nil;
  1260. }
  1261. p += 4;
  1262. n -= 4;
  1263. if(parseoptions(p, n) < 0)
  1264. return nil;
  1265. return bp;
  1266. }
  1267. // write out an ndb entry
  1268. void
  1269. writendb(char *s, int n, int append)
  1270. {
  1271. char file[64];
  1272. int fd;
  1273. snprint(file, sizeof file, "%s/ndb", conf.mpoint);
  1274. if(append){
  1275. fd = open(file, OWRITE);
  1276. seek(fd, 0, 2);
  1277. } else
  1278. fd = open(file, OWRITE|OTRUNC);
  1279. write(fd, s, n);
  1280. close(fd);
  1281. }
  1282. // put server addresses into the ndb entry
  1283. char*
  1284. putaddrs(char *p, char *e, char *attr, uchar *a, int len)
  1285. {
  1286. int i;
  1287. for(i = 0; i < len; i += IPaddrlen, a += IPaddrlen){
  1288. if(!validip(a))
  1289. break;
  1290. p = seprint(p, e, "%s=%I\n", attr, a);
  1291. }
  1292. return p;
  1293. }
  1294. // make an ndb entry and put it into /net/ndb for the servers to see
  1295. void
  1296. putndb(void)
  1297. {
  1298. char buf[1024];
  1299. char *p, *e, *np;
  1300. int append;
  1301. e = buf + sizeof(buf);
  1302. p = buf;
  1303. if(getndb() == 0){
  1304. append = 1;
  1305. } else {
  1306. append = 0;
  1307. p = seprint(p, e, "ip=%I ipmask=%M ipgw=%I\n",
  1308. conf.laddr, conf.mask, conf.gaddr);
  1309. }
  1310. if(np = strchr(conf.hostname, '.')){
  1311. if(*conf.domainname == 0)
  1312. strcpy(conf.domainname, np+1);
  1313. *np = 0;
  1314. }
  1315. if(*conf.hostname)
  1316. p = seprint(p, e, "\tsys=%s\n", conf.hostname);
  1317. if(*conf.domainname)
  1318. p = seprint(p, e, "\tdom=%s.%s\n", conf.hostname, conf.domainname);
  1319. if(validip(conf.fs))
  1320. p = putaddrs(p, e, "\tfs", conf.fs, sizeof(conf.fs));
  1321. if(validip(conf.auth))
  1322. p = putaddrs(p, e, "\tauth", conf.auth, sizeof(conf.auth));
  1323. if(validip(conf.dns))
  1324. p = putaddrs(p, e, "\tdns", conf.dns, sizeof(conf.dns));
  1325. if(validip(conf.ntp))
  1326. p = putaddrs(p, e, "\tntp", conf.ntp, sizeof(conf.ntp));
  1327. if(ndboptions)
  1328. p = seprint(p, e, "%s\n", ndboptions);
  1329. if(p > buf)
  1330. writendb(buf, p-buf, append);
  1331. }
  1332. // get an ndb entry someone else wrote
  1333. int
  1334. getndb(void)
  1335. {
  1336. char buf[1024];
  1337. int fd;
  1338. int n;
  1339. char *p;
  1340. snprint(buf, sizeof buf, "%s/ndb", conf.mpoint);
  1341. fd = open(buf, OREAD);
  1342. n = read(fd, buf, sizeof(buf)-1);
  1343. close(fd);
  1344. if(n <= 0)
  1345. return -1;
  1346. buf[n] = 0;
  1347. p = strstr(buf, "ip=");
  1348. if(p == nil)
  1349. return -1;
  1350. parseip(conf.laddr, p+3);
  1351. return 0;
  1352. }
  1353. // tell a server to refresh
  1354. void
  1355. tweakserver(char *server)
  1356. {
  1357. char file[64];
  1358. int fd;
  1359. snprint(file, sizeof(file), "%s/%s", conf.mpoint, server);
  1360. fd = open(file, ORDWR);
  1361. if(fd < 0)
  1362. return;
  1363. fprint(fd, "refresh");
  1364. close(fd);
  1365. }
  1366. // tell all servers to refresh their information
  1367. void
  1368. tweakservers(void)
  1369. {
  1370. tweakserver("dns");
  1371. tweakserver("cs");
  1372. }
  1373. // return number of networks
  1374. int
  1375. nipifcs(char *net)
  1376. {
  1377. Ipifc *nifc;
  1378. Iplifc *lifc;
  1379. int n;
  1380. n = 0;
  1381. ifc = readipifc(net, ifc, -1);
  1382. for(nifc = ifc; nifc != nil; nifc = nifc->next){
  1383. /*
  1384. * ignore loopback devices when trying to
  1385. * figure out if we're the primary interface.
  1386. */
  1387. if(strcmp(nifc->dev, "/dev/null") != 0)
  1388. for(lifc = nifc->lifc; lifc != nil; lifc = lifc->next)
  1389. if(validip(lifc->ip)){
  1390. n++;
  1391. break;
  1392. }
  1393. if(strcmp(nifc->dev, conf.dev) == 0)
  1394. myifc = nifc->index;
  1395. }
  1396. return n;
  1397. }
  1398. // return true if this is a valid v4 address
  1399. int
  1400. validip(uchar *addr)
  1401. {
  1402. return ipcmp(addr, IPnoaddr) != 0 && ipcmp(addr, v4prefix) != 0;
  1403. }
  1404. char *verbs[] = {
  1405. [Vadd] "add",
  1406. [Vremove] "remove",
  1407. [Vunbind] "unbind",
  1408. [Vether] "ether",
  1409. [Vloopback] "loopback",
  1410. };
  1411. // look for an action
  1412. int
  1413. parseverb(char *name)
  1414. {
  1415. int i;
  1416. for(i = 0; i < nelem(verbs); i++){
  1417. if(verbs[i] == 0)
  1418. continue;
  1419. if(strcmp(name, verbs[i]) == 0)
  1420. return i;
  1421. }
  1422. return -1;
  1423. }
  1424. // get everything out of ndb
  1425. void
  1426. ndbconfig(void)
  1427. {
  1428. Ndb *db;
  1429. Ndbtuple *t, *nt;
  1430. char etheraddr[32];
  1431. char *attrs[10];
  1432. int nattr;
  1433. int ndns = 0;
  1434. int nfs = 0;
  1435. int nauth = 0;
  1436. db = ndbopen(0);
  1437. if(db == nil)
  1438. sysfatal("can't open ndb: %r");
  1439. if(strcmp(conf.type, "ether") != 0 || myetheraddr(conf.hwa, conf.dev) != 0)
  1440. sysfatal("can't read hardware address");
  1441. sprint(etheraddr, "%E", conf.hwa);
  1442. nattr = 0;
  1443. attrs[nattr++] = "ip";
  1444. attrs[nattr++] = "ipmask";
  1445. attrs[nattr++] = "ipgw";
  1446. attrs[nattr++] = "@dns";
  1447. attrs[nattr++] = "@ntp";
  1448. attrs[nattr++] = "@fs";
  1449. attrs[nattr++] = "@auth";
  1450. attrs[nattr] = nil;
  1451. t = ndbipinfo(db, "ether", etheraddr, attrs, nattr);
  1452. for(nt = t; nt != nil; nt = nt->entry){
  1453. if(strcmp(nt->attr, "ip") == 0){
  1454. parseip(conf.laddr, nt->val);
  1455. } else if(strcmp(nt->attr, "ipmask") == 0){
  1456. parseipmask(conf.mask, nt->val);
  1457. } else if(strcmp(nt->attr, "ipgw") == 0){
  1458. parseip(conf.gaddr, nt->val);
  1459. } else if(ndns < 2 && strcmp(nt->attr, "dns") == 0){
  1460. parseip(conf.dns+IPaddrlen*ndns, nt->val);
  1461. } else if(strcmp(nt->attr, "ntp") == 0){
  1462. parseip(conf.ntp, nt->val);
  1463. } else if(nfs < 2 && strcmp(nt->attr, "fs") == 0){
  1464. parseip(conf.fs+IPaddrlen*nfs, nt->val);
  1465. } else if(nauth < 2 && strcmp(nt->attr, "auth") == 0){
  1466. parseip(conf.auth+IPaddrlen*nauth, nt->val);
  1467. }
  1468. }
  1469. ndbfree(t);
  1470. if(!validip(conf.laddr))
  1471. sysfatal("address not found in ndb");
  1472. }
  1473. int
  1474. addoption(char *opt)
  1475. {
  1476. int i;
  1477. Option *o;
  1478. if(opt == nil)
  1479. return -1;
  1480. for(o = option; o < &option[nelem(option)]; o++){
  1481. if(o->name == nil)
  1482. continue;
  1483. if(strcmp(opt, o->name) == 0){
  1484. i = o-option;
  1485. if(!memchr(requested, i, nrequested))
  1486. if(nrequested < nelem(requested))
  1487. requested[nrequested++] = i;
  1488. return 0;
  1489. }
  1490. }
  1491. return -1;
  1492. }
  1493. char*
  1494. optgetx(uchar *p, uchar opt)
  1495. {
  1496. Option *o;
  1497. int i, n;
  1498. ulong x;
  1499. char *s, *ns;
  1500. uchar ip[IPaddrlen];
  1501. uchar ips[16*IPaddrlen];
  1502. char str[256];
  1503. uchar vec[256];
  1504. o = &option[opt];
  1505. if(o->name == nil)
  1506. return nil;
  1507. s = nil;
  1508. switch(o->type){
  1509. case Taddr:
  1510. if(optgetaddr(p, opt, ip))
  1511. s = smprint("%s=%I", o->name, ip);
  1512. break;
  1513. case Taddrs:
  1514. n = optgetaddrs(p, opt, ips, 16);
  1515. if(n > 0)
  1516. s = smprint("%s=%I", o->name, ips);
  1517. for(i = 1; i < n; i++){
  1518. ns = smprint("%s %s=%I", s, o->name, &ips[i*IPaddrlen]);
  1519. free(s);
  1520. s = ns;
  1521. }
  1522. break;
  1523. case Tulong:
  1524. x = optgetulong(p, opt);
  1525. if(x != 0)
  1526. s = smprint("%s=%lud", o->name, x);
  1527. break;
  1528. case Tbyte:
  1529. x = optgetbyte(p, opt);
  1530. if(x != 0)
  1531. s = smprint("%s=%lud", o->name, x);
  1532. break;
  1533. case Tstr:
  1534. if(optgetstr(p, opt, str, sizeof(str)))
  1535. s = smprint("%s=%s", o->name, str);
  1536. break;
  1537. case Tvec:
  1538. n = optgetvec(p, opt, vec, sizeof(vec));
  1539. if(n > 0)
  1540. s = smprint("%s=%.*H", o->name, n, vec);
  1541. break;
  1542. }
  1543. return s;
  1544. }
  1545. void
  1546. getoptions(uchar *p)
  1547. {
  1548. int i;
  1549. char *s, *t;
  1550. for(i = nelem(defrequested); i < nrequested; i++){
  1551. s = optgetx(p, requested[i]);
  1552. if(s != nil)
  1553. DEBUG("%s ", s);
  1554. if(ndboptions == nil)
  1555. ndboptions = smprint("\t%s", s);
  1556. else{
  1557. t = ndboptions;
  1558. ndboptions = smprint("\t%s%s", s, ndboptions);
  1559. free(t);
  1560. }
  1561. free(s);
  1562. }
  1563. }