main.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  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 < 6; 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 ipinconfig'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. alarm(1000);
  1042. n = read(conf.fd, buf, sizeof buf);
  1043. alarm(0);
  1044. if(n < 0){
  1045. errstr(err, sizeof err);
  1046. if(strstr(err, "interrupt") == nil)
  1047. warning("dhcprecv: bad read: %s", err);
  1048. else
  1049. DEBUG("dhcprecv: read timed out");
  1050. return;
  1051. }
  1052. bp = parsebootp(buf, n);
  1053. if(bp == 0) {
  1054. DEBUG("parsebootp failed: dropping packet");
  1055. return;
  1056. }
  1057. type = optgetbyte(bp->optdata, ODtype);
  1058. switch(type) {
  1059. default:
  1060. warning("dhcprecv: unknown type: %d", type);
  1061. break;
  1062. case Offer:
  1063. DEBUG("got offer from %V ", bp->siaddr);
  1064. if(conf.state != Sselecting){
  1065. // DEBUG("");
  1066. break;
  1067. }
  1068. lease = optgetulong(bp->optdata, ODlease);
  1069. if(lease == 0){
  1070. /*
  1071. * The All_Aboard NAT package from Internet Share
  1072. * doesn't give a lease time, so we have to assume one.
  1073. */
  1074. warning("Offer with %lud lease, using %d", lease, MinLease);
  1075. lease = MinLease;
  1076. }
  1077. DEBUG("lease=%lud ", lease);
  1078. if(!optgetaddr(bp->optdata, ODserverid, conf.server)) {
  1079. warning("Offer from server with invalid serverid");
  1080. break;
  1081. }
  1082. v4tov6(conf.laddr, bp->yiaddr);
  1083. memmove(conf.sname, bp->sname, sizeof conf.sname);
  1084. conf.sname[sizeof conf.sname-1] = 0;
  1085. DEBUG("server=%I sname=%s", conf.server, conf.sname);
  1086. conf.offered = lease;
  1087. conf.state = Srequesting;
  1088. dhcpsend(Request);
  1089. conf.resend = 0;
  1090. conf.timeout = time(0) + 4;
  1091. break;
  1092. case Ack:
  1093. DEBUG("got ack from %V ", bp->siaddr);
  1094. if (conf.state != Srequesting && conf.state != Srenewing &&
  1095. conf.state != Srebinding)
  1096. break;
  1097. /* ignore a bad lease */
  1098. lease = optgetulong(bp->optdata, ODlease);
  1099. if(lease == 0){
  1100. /*
  1101. * The All_Aboard NAT package from Internet Share
  1102. * doesn't give a lease time, so we have to assume one.
  1103. */
  1104. warning("Ack with %lud lease, using %d", lease, MinLease);
  1105. lease = MinLease;
  1106. }
  1107. DEBUG("lease=%lud ", lease);
  1108. /* address and mask */
  1109. if(!validip(conf.laddr) || !Oflag)
  1110. v4tov6(conf.laddr, bp->yiaddr);
  1111. if(!validip(conf.mask) || !Oflag){
  1112. if(!optgetaddr(bp->optdata, OBmask, conf.mask))
  1113. ipmove(conf.mask, IPnoaddr);
  1114. }
  1115. DEBUG("ipaddr=%I ipmask=%M ", conf.laddr, conf.mask);
  1116. /*
  1117. * get a router address either from the router option
  1118. * or from the router that forwarded the dhcp packet
  1119. */
  1120. if(validip(conf.gaddr) && Oflag) {
  1121. DEBUG("ipgw=%I ", conf.gaddr);
  1122. } else if(optgetaddr(bp->optdata, OBrouter, conf.gaddr)){
  1123. DEBUG("ipgw=%I ", conf.gaddr);
  1124. } else if(memcmp(bp->giaddr, IPnoaddr+IPv4off, IPv4addrlen)!=0){
  1125. v4tov6(conf.gaddr, bp->giaddr);
  1126. DEBUG("giaddr=%I ", conf.gaddr);
  1127. }
  1128. /* get dns servers */
  1129. memset(conf.dns, 0, sizeof conf.dns);
  1130. n = optgetaddrs(bp->optdata, OBdnserver, conf.dns,
  1131. sizeof conf.dns/IPaddrlen);
  1132. for(i = 0; i < n; i++)
  1133. DEBUG("dns=%I ", conf.dns + i*IPaddrlen);
  1134. /* get ntp servers */
  1135. memset(conf.ntp, 0, sizeof conf.ntp);
  1136. n = optgetaddrs(bp->optdata, OBntpserver, conf.ntp,
  1137. sizeof conf.ntp/IPaddrlen);
  1138. for(i = 0; i < n; i++)
  1139. DEBUG("ntp=%I ", conf.ntp + i*IPaddrlen);
  1140. /* get names */
  1141. optgetstr(bp->optdata, OBhostname,
  1142. conf.hostname, sizeof conf.hostname);
  1143. optgetstr(bp->optdata, OBdomainname,
  1144. conf.domainname, sizeof conf.domainname);
  1145. /* get anything else we asked for */
  1146. getoptions(bp->optdata);
  1147. /* get plan9-specific options */
  1148. n = optgetvec(bp->optdata, OBvendorinfo, vopts, sizeof vopts-1);
  1149. if(n > 0 && parseoptions(vopts, n) == 0){
  1150. if(validip(conf.fs) && Oflag)
  1151. n = 1;
  1152. else {
  1153. n = optgetp9addrs(vopts, OP9fs, conf.fs, 2);
  1154. if (n == 0)
  1155. n = optgetaddrs(vopts, OP9fsv4,
  1156. conf.fs, 2);
  1157. }
  1158. for(i = 0; i < n; i++)
  1159. DEBUG("fs=%I ", conf.fs + i*IPaddrlen);
  1160. if(validip(conf.auth) && Oflag)
  1161. n = 1;
  1162. else {
  1163. n = optgetp9addrs(vopts, OP9auth, conf.auth, 2);
  1164. if (n == 0)
  1165. n = optgetaddrs(vopts, OP9authv4,
  1166. conf.auth, 2);
  1167. }
  1168. for(i = 0; i < n; i++)
  1169. DEBUG("auth=%I ", conf.auth + i*IPaddrlen);
  1170. n = optgetp9addrs(vopts, OP9ipaddr, taddr, 1);
  1171. if (n > 0)
  1172. memmove(conf.laddr, taddr, IPaddrlen);
  1173. n = optgetp9addrs(vopts, OP9ipmask, taddr, 1);
  1174. if (n > 0)
  1175. memmove(conf.mask, taddr, IPaddrlen);
  1176. n = optgetp9addrs(vopts, OP9ipgw, taddr, 1);
  1177. if (n > 0)
  1178. memmove(conf.gaddr, taddr, IPaddrlen);
  1179. DEBUG("new ipaddr=%I new ipmask=%M new ipgw=%I",
  1180. conf.laddr, conf.mask, conf.gaddr);
  1181. }
  1182. conf.lease = lease;
  1183. conf.state = Sbound;
  1184. DEBUG("server=%I sname=%s", conf.server, conf.sname);
  1185. break;
  1186. case Nak:
  1187. conf.state = Sinit;
  1188. warning("recved dhcpnak on %s", conf.mpoint);
  1189. break;
  1190. }
  1191. }
  1192. /* return pseudo-random integer in range low...(hi-1) */
  1193. ulong
  1194. randint(ulong low, ulong hi)
  1195. {
  1196. if (hi < low)
  1197. return low;
  1198. return low + nrand(hi - low);
  1199. }
  1200. long
  1201. jitter(void) /* compute small pseudo-random delay in ms */
  1202. {
  1203. return randint(0, 10*1000);
  1204. }
  1205. int
  1206. openlisten(void)
  1207. {
  1208. int n, fd, cfd;
  1209. char data[128], devdir[40];
  1210. if (validip(conf.laddr) &&
  1211. (conf.state == Srenewing || conf.state == Srebinding))
  1212. sprint(data, "%s/udp!%I!68", conf.mpoint, conf.laddr);
  1213. else
  1214. sprint(data, "%s/udp!*!68", conf.mpoint);
  1215. for (n = 0; (cfd = announce(data, devdir)) < 0; n++) {
  1216. if(!noconfig)
  1217. sysfatal("can't announce for dhcp: %r");
  1218. /* might be another client - wait and try again */
  1219. warning("can't announce %s: %r", data);
  1220. sleep(jitter());
  1221. if(n > 10)
  1222. return -1;
  1223. }
  1224. if(fprint(cfd, "headers") < 0)
  1225. sysfatal("can't set header mode: %r");
  1226. sprint(data, "%s/data", devdir);
  1227. fd = open(data, ORDWR);
  1228. if(fd < 0)
  1229. sysfatal("open %s: %r", data);
  1230. close(cfd);
  1231. return fd;
  1232. }
  1233. uchar*
  1234. optadd(uchar *p, int op, void *d, int n)
  1235. {
  1236. p[0] = op;
  1237. p[1] = n;
  1238. memmove(p+2, d, n);
  1239. return p+n+2;
  1240. }
  1241. uchar*
  1242. optaddbyte(uchar *p, int op, int b)
  1243. {
  1244. p[0] = op;
  1245. p[1] = 1;
  1246. p[2] = b;
  1247. return p+3;
  1248. }
  1249. uchar*
  1250. optaddulong(uchar *p, int op, ulong x)
  1251. {
  1252. p[0] = op;
  1253. p[1] = 4;
  1254. hnputl(p+2, x);
  1255. return p+6;
  1256. }
  1257. uchar *
  1258. optaddaddr(uchar *p, int op, uchar *ip)
  1259. {
  1260. p[0] = op;
  1261. p[1] = 4;
  1262. v6tov4(p+2, ip);
  1263. return p+6;
  1264. }
  1265. /* add dhcp option op with value v of length n to dhcp option array p */
  1266. uchar *
  1267. optaddvec(uchar *p, int op, uchar *v, int n)
  1268. {
  1269. p[0] = op;
  1270. p[1] = n;
  1271. memmove(p+2, v, n);
  1272. return p+2+n;
  1273. }
  1274. uchar *
  1275. optaddstr(uchar *p, int op, char *v)
  1276. {
  1277. int n;
  1278. n = strlen(v)+1; /* microsoft leaves on the NUL, so we do too */
  1279. p[0] = op;
  1280. p[1] = n;
  1281. memmove(p+2, v, n);
  1282. return p+2+n;
  1283. }
  1284. /*
  1285. * parse p, looking for option `op'. if non-nil, np points to minimum length.
  1286. * return nil if option is too small, else ptr to opt, and
  1287. * store actual length via np if non-nil.
  1288. */
  1289. uchar*
  1290. optget(uchar *p, int op, int *np)
  1291. {
  1292. int len, code;
  1293. while ((code = *p++) != OBend) {
  1294. if(code == OBpad)
  1295. continue;
  1296. len = *p++;
  1297. if(code != op) {
  1298. p += len;
  1299. continue;
  1300. }
  1301. if(np != nil){
  1302. if(*np > len) {
  1303. return 0;
  1304. }
  1305. *np = len;
  1306. }
  1307. return p;
  1308. }
  1309. return 0;
  1310. }
  1311. int
  1312. optgetbyte(uchar *p, int op)
  1313. {
  1314. int len;
  1315. len = 1;
  1316. p = optget(p, op, &len);
  1317. if(p == nil)
  1318. return 0;
  1319. return *p;
  1320. }
  1321. ulong
  1322. optgetulong(uchar *p, int op)
  1323. {
  1324. int len;
  1325. len = 4;
  1326. p = optget(p, op, &len);
  1327. if(p == nil)
  1328. return 0;
  1329. return nhgetl(p);
  1330. }
  1331. int
  1332. optgetaddr(uchar *p, int op, uchar *ip)
  1333. {
  1334. int len;
  1335. len = 4;
  1336. p = optget(p, op, &len);
  1337. if(p == nil)
  1338. return 0;
  1339. v4tov6(ip, p);
  1340. return 1;
  1341. }
  1342. /* expect at most n addresses; ip[] only has room for that many */
  1343. int
  1344. optgetaddrs(uchar *p, int op, uchar *ip, int n)
  1345. {
  1346. int len, i;
  1347. len = 4;
  1348. p = optget(p, op, &len);
  1349. if(p == nil)
  1350. return 0;
  1351. len /= IPv4addrlen;
  1352. if(len > n)
  1353. len = n;
  1354. for(i = 0; i < len; i++)
  1355. v4tov6(&ip[i*IPaddrlen], &p[i*IPv4addrlen]);
  1356. return i;
  1357. }
  1358. /* expect at most n addresses; ip[] only has room for that many */
  1359. int
  1360. optgetp9addrs(uchar *ap, int op, uchar *ip, int n)
  1361. {
  1362. int len, i, slen, addrs;
  1363. char *p;
  1364. len = 1; /* minimum bytes needed */
  1365. p = (char *)optget(ap, op, &len);
  1366. if(p == nil)
  1367. return 0;
  1368. addrs = *p++; /* first byte is address count */
  1369. for (i = 0; i < n && i < addrs && len > 0; i++) {
  1370. slen = strlen(p) + 1;
  1371. if (parseip(&ip[i*IPaddrlen], p) == -1)
  1372. fprint(2, "%s: bad address %s\n", argv0, p);
  1373. DEBUG("got plan 9 option %d addr %I (%s)",
  1374. op, &ip[i*IPaddrlen], p);
  1375. p += slen;
  1376. len -= slen;
  1377. }
  1378. return addrs;
  1379. }
  1380. int
  1381. optgetvec(uchar *p, int op, uchar *v, int n)
  1382. {
  1383. int len;
  1384. len = 1;
  1385. p = optget(p, op, &len);
  1386. if(p == nil)
  1387. return 0;
  1388. if(len > n)
  1389. len = n;
  1390. memmove(v, p, len);
  1391. return len;
  1392. }
  1393. int
  1394. optgetstr(uchar *p, int op, char *s, int n)
  1395. {
  1396. int len;
  1397. len = 1;
  1398. p = optget(p, op, &len);
  1399. if(p == nil)
  1400. return 0;
  1401. if(len >= n)
  1402. len = n-1;
  1403. memmove(s, p, len);
  1404. s[len] = 0;
  1405. return len;
  1406. }
  1407. /*
  1408. * sanity check options area
  1409. * - options don't overflow packet
  1410. * - options end with an OBend
  1411. */
  1412. int
  1413. parseoptions(uchar *p, int n)
  1414. {
  1415. int code, len, nin = n;
  1416. while (n > 0) {
  1417. code = *p++;
  1418. n--;
  1419. if(code == OBend)
  1420. return 0;
  1421. if(code == OBpad)
  1422. continue;
  1423. if(n == 0) {
  1424. warning("parseoptions: bad option: 0x%ux: truncated: "
  1425. "opt length = %d", code, nin);
  1426. return -1;
  1427. }
  1428. len = *p++;
  1429. n--;
  1430. DEBUG("parseoptions: %s(%d) len %d, bytes left %d",
  1431. option[code].name, code, len, n);
  1432. if(len > n) {
  1433. warning("parseoptions: bad option: 0x%ux: %d > %d: "
  1434. "opt length = %d", code, len, n, nin);
  1435. return -1;
  1436. }
  1437. p += len;
  1438. n -= len;
  1439. }
  1440. /* make sure packet ends with an OBend after all the optget code */
  1441. *p = OBend;
  1442. return 0;
  1443. }
  1444. /*
  1445. * sanity check received packet:
  1446. * - magic is dhcp magic
  1447. * - options don't overflow packet
  1448. */
  1449. Bootp *
  1450. parsebootp(uchar *p, int n)
  1451. {
  1452. Bootp *bp;
  1453. bp = (Bootp*)p;
  1454. if(n < bp->optmagic - p) {
  1455. warning("parsebootp: short bootp packet");
  1456. return nil;
  1457. }
  1458. if(conf.xid != nhgetl(bp->xid))
  1459. return nil;
  1460. if(bp->op != Bootreply) {
  1461. warning("parsebootp: bad op %d", bp->op);
  1462. return nil;
  1463. }
  1464. n -= bp->optmagic - p;
  1465. p = bp->optmagic;
  1466. if(n < 4) {
  1467. warning("parsebootp: no option data");
  1468. return nil;
  1469. }
  1470. if(memcmp(optmagic, p, 4) != 0) {
  1471. warning("parsebootp: bad opt magic %ux %ux %ux %ux",
  1472. p[0], p[1], p[2], p[3]);
  1473. return nil;
  1474. }
  1475. p += 4;
  1476. n -= 4;
  1477. DEBUG("parsebootp: new packet");
  1478. if(parseoptions(p, n) < 0)
  1479. return nil;
  1480. return bp;
  1481. }
  1482. /* write out an ndb entry */
  1483. void
  1484. writendb(char *s, int n, int append)
  1485. {
  1486. char file[64];
  1487. int fd;
  1488. snprint(file, sizeof file, "%s/ndb", conf.mpoint);
  1489. if(append){
  1490. fd = open(file, OWRITE);
  1491. seek(fd, 0, 2);
  1492. } else
  1493. fd = open(file, OWRITE|OTRUNC);
  1494. write(fd, s, n);
  1495. close(fd);
  1496. }
  1497. /* put server addresses into the ndb entry */
  1498. char*
  1499. putaddrs(char *p, char *e, char *attr, uchar *a, int len)
  1500. {
  1501. int i;
  1502. for(i = 0; i < len && validip(a); i += IPaddrlen, a += IPaddrlen)
  1503. p = seprint(p, e, "%s=%I\n", attr, a);
  1504. return p;
  1505. }
  1506. /* make an ndb entry and put it into /net/ndb for the servers to see */
  1507. void
  1508. putndb(void)
  1509. {
  1510. int append;
  1511. char buf[1024];
  1512. char *p, *e, *np;
  1513. p = buf;
  1514. e = buf + sizeof buf;
  1515. if(getndb() == 0)
  1516. append = 1;
  1517. else {
  1518. append = 0;
  1519. p = seprint(p, e, "ip=%I ipmask=%M ipgw=%I\n",
  1520. conf.laddr, conf.mask, conf.gaddr);
  1521. }
  1522. if(np = strchr(conf.hostname, '.')){
  1523. if(*conf.domainname == 0)
  1524. strcpy(conf.domainname, np+1);
  1525. *np = 0;
  1526. }
  1527. if(*conf.hostname)
  1528. p = seprint(p, e, "\tsys=%s\n", conf.hostname);
  1529. if(*conf.domainname)
  1530. p = seprint(p, e, "\tdom=%s.%s\n",
  1531. conf.hostname, conf.domainname);
  1532. if(validip(conf.fs))
  1533. p = putaddrs(p, e, "\tfs", conf.fs, sizeof conf.fs);
  1534. if(validip(conf.auth))
  1535. p = putaddrs(p, e, "\tauth", conf.auth, sizeof conf.auth);
  1536. if(validip(conf.dns))
  1537. p = putaddrs(p, e, "\tdns", conf.dns, sizeof conf.dns);
  1538. if(validip(conf.ntp))
  1539. p = putaddrs(p, e, "\tntp", conf.ntp, sizeof conf.ntp);
  1540. if(ndboptions)
  1541. p = seprint(p, e, "%s\n", ndboptions);
  1542. if(p > buf)
  1543. writendb(buf, p-buf, append);
  1544. }
  1545. /* get an ndb entry someone else wrote */
  1546. int
  1547. getndb(void)
  1548. {
  1549. char buf[1024];
  1550. int fd, n;
  1551. char *p;
  1552. snprint(buf, sizeof buf, "%s/ndb", conf.mpoint);
  1553. fd = open(buf, OREAD);
  1554. n = read(fd, buf, sizeof buf-1);
  1555. close(fd);
  1556. if(n <= 0)
  1557. return -1;
  1558. buf[n] = 0;
  1559. p = strstr(buf, "ip=");
  1560. if(p == nil)
  1561. return -1;
  1562. if (parseip(conf.laddr, p+3) == -1)
  1563. fprint(2, "%s: bad address %s\n", argv0, p+3);
  1564. return 0;
  1565. }
  1566. /* tell a server to refresh */
  1567. void
  1568. tweakserver(char *server)
  1569. {
  1570. int fd;
  1571. char file[64];
  1572. snprint(file, sizeof file, "%s/%s", conf.mpoint, server);
  1573. fd = open(file, ORDWR);
  1574. if(fd < 0)
  1575. return;
  1576. fprint(fd, "refresh");
  1577. close(fd);
  1578. }
  1579. /* tell all servers to refresh their information */
  1580. void
  1581. tweakservers(void)
  1582. {
  1583. tweakserver("dns");
  1584. tweakserver("cs");
  1585. }
  1586. /* return number of networks */
  1587. int
  1588. nipifcs(char *net)
  1589. {
  1590. int n;
  1591. Ipifc *nifc;
  1592. Iplifc *lifc;
  1593. n = 0;
  1594. ifc = readipifc(net, ifc, -1);
  1595. for(nifc = ifc; nifc != nil; nifc = nifc->next){
  1596. /*
  1597. * ignore loopback devices when trying to
  1598. * figure out if we're the primary interface.
  1599. */
  1600. if(strcmp(nifc->dev, "/dev/null") != 0)
  1601. for(lifc = nifc->lifc; lifc != nil; lifc = lifc->next)
  1602. if(validip(lifc->ip)){
  1603. n++;
  1604. break;
  1605. }
  1606. if(strcmp(nifc->dev, conf.dev) == 0)
  1607. myifc = nifc->index;
  1608. }
  1609. return n;
  1610. }
  1611. /* return true if this is a valid v4 address */
  1612. int
  1613. validip(uchar *addr)
  1614. {
  1615. return ipcmp(addr, IPnoaddr) != 0 && ipcmp(addr, v4prefix) != 0;
  1616. }
  1617. /* look for an action */
  1618. int
  1619. parseverb(char *name)
  1620. {
  1621. int i;
  1622. for(i = 0; i < nelem(verbs); i++)
  1623. if(verbs[i] != nil && strcmp(name, verbs[i]) == 0)
  1624. return i;
  1625. return -1;
  1626. }
  1627. /* get everything out of ndb */
  1628. void
  1629. ndbconfig(void)
  1630. {
  1631. int nattr, nauth = 0, ndns = 0, nfs = 0, ok;
  1632. char etheraddr[32];
  1633. char *attrs[10];
  1634. Ndb *db;
  1635. Ndbtuple *t, *nt;
  1636. db = ndbopen(0);
  1637. if(db == nil)
  1638. sysfatal("can't open ndb: %r");
  1639. if (strcmp(conf.type, "ether") != 0 && strcmp(conf.type, "gbe") != 0 ||
  1640. myetheraddr(conf.hwa, conf.dev) != 0)
  1641. sysfatal("can't read hardware address");
  1642. sprint(etheraddr, "%E", conf.hwa);
  1643. nattr = 0;
  1644. attrs[nattr++] = "ip";
  1645. attrs[nattr++] = "ipmask";
  1646. attrs[nattr++] = "ipgw";
  1647. /* the @ triggers resolution to an IP address; see ndb(2) */
  1648. attrs[nattr++] = "@dns";
  1649. attrs[nattr++] = "@ntp";
  1650. attrs[nattr++] = "@fs";
  1651. attrs[nattr++] = "@auth";
  1652. attrs[nattr] = nil;
  1653. t = ndbipinfo(db, "ether", etheraddr, attrs, nattr);
  1654. for(nt = t; nt != nil; nt = nt->entry) {
  1655. ok = 1;
  1656. if(strcmp(nt->attr, "ip") == 0)
  1657. ok = parseip(conf.laddr, nt->val);
  1658. else if(strcmp(nt->attr, "ipmask") == 0)
  1659. parseipmask(conf.mask, nt->val); /* could be -1 */
  1660. else if(strcmp(nt->attr, "ipgw") == 0)
  1661. ok = parseip(conf.gaddr, nt->val);
  1662. else if(ndns < 2 && strcmp(nt->attr, "dns") == 0)
  1663. ok = parseip(conf.dns+IPaddrlen*ndns, nt->val);
  1664. else if(strcmp(nt->attr, "ntp") == 0)
  1665. ok = parseip(conf.ntp, nt->val);
  1666. else if(nfs < 2 && strcmp(nt->attr, "fs") == 0)
  1667. ok = parseip(conf.fs+IPaddrlen*nfs, nt->val);
  1668. else if(nauth < 2 && strcmp(nt->attr, "auth") == 0)
  1669. ok = parseip(conf.auth+IPaddrlen*nauth, nt->val);
  1670. if (!ok)
  1671. fprint(2, "%s: bad %s address in ndb: %s\n", argv0,
  1672. nt->attr, nt->val);
  1673. }
  1674. ndbfree(t);
  1675. if(!validip(conf.laddr))
  1676. sysfatal("address not found in ndb");
  1677. }
  1678. int
  1679. addoption(char *opt)
  1680. {
  1681. int i;
  1682. Option *o;
  1683. if(opt == nil)
  1684. return -1;
  1685. for(o = option; o < &option[nelem(option)]; o++)
  1686. if(o->name && strcmp(opt, o->name) == 0){
  1687. i = o - option;
  1688. if(memchr(requested, i, nrequested) == 0 &&
  1689. nrequested < nelem(requested))
  1690. requested[nrequested++] = i;
  1691. return 0;
  1692. }
  1693. return -1;
  1694. }
  1695. char*
  1696. optgetx(uchar *p, uchar opt)
  1697. {
  1698. int i, n;
  1699. ulong x;
  1700. char *s, *ns;
  1701. char str[256];
  1702. uchar ip[IPaddrlen], ips[16*IPaddrlen], vec[256];
  1703. Option *o;
  1704. o = &option[opt];
  1705. if(o->name == nil)
  1706. return nil;
  1707. s = nil;
  1708. switch(o->type){
  1709. case Taddr:
  1710. if(optgetaddr(p, opt, ip))
  1711. s = smprint("%s=%I", o->name, ip);
  1712. break;
  1713. case Taddrs:
  1714. n = optgetaddrs(p, opt, ips, 16);
  1715. if(n > 0)
  1716. s = smprint("%s=%I", o->name, ips);
  1717. for(i = 1; i < n; i++){
  1718. ns = smprint("%s %s=%I", s, o->name, &ips[i*IPaddrlen]);
  1719. free(s);
  1720. s = ns;
  1721. }
  1722. break;
  1723. case Tulong:
  1724. x = optgetulong(p, opt);
  1725. if(x != 0)
  1726. s = smprint("%s=%lud", o->name, x);
  1727. break;
  1728. case Tbyte:
  1729. x = optgetbyte(p, opt);
  1730. if(x != 0)
  1731. s = smprint("%s=%lud", o->name, x);
  1732. break;
  1733. case Tstr:
  1734. if(optgetstr(p, opt, str, sizeof str))
  1735. s = smprint("%s=%s", o->name, str);
  1736. break;
  1737. case Tvec:
  1738. n = optgetvec(p, opt, vec, sizeof vec);
  1739. if(n > 0)
  1740. /* what's %H? it's not installed */
  1741. s = smprint("%s=%.*H", o->name, n, vec);
  1742. break;
  1743. }
  1744. return s;
  1745. }
  1746. void
  1747. getoptions(uchar *p)
  1748. {
  1749. int i;
  1750. char *s, *t;
  1751. for(i = nelem(defrequested); i < nrequested; i++){
  1752. s = optgetx(p, requested[i]);
  1753. if(s != nil)
  1754. DEBUG("%s ", s);
  1755. if(ndboptions == nil)
  1756. ndboptions = smprint("\t%s", s);
  1757. else{
  1758. t = ndboptions;
  1759. ndboptions = smprint("\t%s%s", s, ndboptions);
  1760. free(t);
  1761. }
  1762. free(s);
  1763. }
  1764. }