main.c 39 KB

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