main.c 39 KB

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