devip.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. #include "../ip/ip.h"
  16. enum
  17. {
  18. Qtopdir= 1, /* top level directory */
  19. Qtopbase,
  20. Qarp= Qtopbase,
  21. Qbootp,
  22. Qndb,
  23. Qiproute,
  24. Qipselftab,
  25. Qlog,
  26. Qprotodir, /* directory for a protocol */
  27. Qprotobase,
  28. Qclone= Qprotobase,
  29. Qstats,
  30. Qconvdir, /* directory for a conversation */
  31. Qconvbase,
  32. Qctl= Qconvbase,
  33. Qdata,
  34. Qerr,
  35. Qlisten,
  36. Qlocal,
  37. Qremote,
  38. Qstatus,
  39. Qsnoop,
  40. Logtype= 5,
  41. Masktype= (1<<Logtype)-1,
  42. Logconv= 12,
  43. Maskconv= (1<<Logconv)-1,
  44. Shiftconv= Logtype,
  45. Logproto= 8,
  46. Maskproto= (1<<Logproto)-1,
  47. Shiftproto= Logtype + Logconv,
  48. Nfs= 128,
  49. };
  50. #define TYPE(x) ( ((uint32_t)(x).path) & Masktype )
  51. #define CONV(x) ( (((uint32_t)(x).path) >> Shiftconv) & Maskconv )
  52. #define PROTO(x) ( (((uint32_t)(x).path) >> Shiftproto) & Maskproto )
  53. #define QID(p, c, y) ( ((p)<<(Shiftproto)) | ((c)<<Shiftconv) | (y) )
  54. static char network[] = "network";
  55. QLock fslock;
  56. Fs *ipfs[Nfs]; /* attached fs's */
  57. Queue *qlog;
  58. extern void nullmediumlink(void);
  59. extern void pktmediumlink(void);
  60. int32_t ndbwrite(Fs *f, char *a, uint32_t off, int n);
  61. static int
  62. ip3gen(Chan *c, int i, Dir *dp)
  63. {
  64. Qid q;
  65. Conv *cv;
  66. char *p;
  67. cv = ipfs[c->devno]->p[PROTO(c->qid)]->conv[CONV(c->qid)];
  68. if(cv->owner == nil)
  69. kstrdup(&cv->owner, eve);
  70. mkqid(&q, QID(PROTO(c->qid), CONV(c->qid), i), 0, QTFILE);
  71. switch(i) {
  72. default:
  73. return -1;
  74. case Qctl:
  75. devdir(c, q, "ctl", 0, cv->owner, cv->perm, dp);
  76. return 1;
  77. case Qdata:
  78. devdir(c, q, "data", qlen(cv->rq), cv->owner, cv->perm, dp);
  79. return 1;
  80. case Qerr:
  81. devdir(c, q, "err", qlen(cv->eq), cv->owner, cv->perm, dp);
  82. return 1;
  83. case Qlisten:
  84. devdir(c, q, "listen", 0, cv->owner, cv->perm, dp);
  85. return 1;
  86. case Qlocal:
  87. p = "local";
  88. break;
  89. case Qremote:
  90. p = "remote";
  91. break;
  92. case Qsnoop:
  93. if(strcmp(cv->p->name, "ipifc") != 0)
  94. return -1;
  95. devdir(c, q, "snoop", qlen(cv->sq), cv->owner, 0400, dp);
  96. return 1;
  97. case Qstatus:
  98. p = "status";
  99. break;
  100. }
  101. devdir(c, q, p, 0, cv->owner, 0444, dp);
  102. return 1;
  103. }
  104. static int
  105. ip2gen(Chan *c, int i, Dir *dp)
  106. {
  107. Qid q;
  108. switch(i) {
  109. case Qclone:
  110. mkqid(&q, QID(PROTO(c->qid), 0, Qclone), 0, QTFILE);
  111. devdir(c, q, "clone", 0, network, 0666, dp);
  112. return 1;
  113. case Qstats:
  114. mkqid(&q, QID(PROTO(c->qid), 0, Qstats), 0, QTFILE);
  115. devdir(c, q, "stats", 0, network, 0444, dp);
  116. return 1;
  117. }
  118. return -1;
  119. }
  120. static int
  121. ip1gen(Chan *c, int i, Dir *dp)
  122. {
  123. Qid q;
  124. char *p;
  125. int prot;
  126. int len = 0;
  127. Fs *f;
  128. extern uint32_t kerndate;
  129. f = ipfs[c->devno];
  130. prot = 0666;
  131. mkqid(&q, QID(0, 0, i), 0, QTFILE);
  132. switch(i) {
  133. default:
  134. return -1;
  135. case Qarp:
  136. p = "arp";
  137. prot = 0664;
  138. break;
  139. case Qbootp:
  140. p = "bootp";
  141. break;
  142. case Qndb:
  143. p = "ndb";
  144. len = strlen(f->ndb);
  145. q.vers = f->ndbvers;
  146. break;
  147. case Qiproute:
  148. p = "iproute";
  149. prot = 0664;
  150. break;
  151. case Qipselftab:
  152. p = "ipselftab";
  153. prot = 0444;
  154. break;
  155. case Qlog:
  156. p = "log";
  157. break;
  158. }
  159. devdir(c, q, p, len, network, prot, dp);
  160. if(i == Qndb && f->ndbmtime > kerndate)
  161. dp->mtime = f->ndbmtime;
  162. return 1;
  163. }
  164. static int
  165. ipgen(Chan *c, char* j, Dirtab* dir, int mm, int s, Dir *dp)
  166. {
  167. Proc *up = externup();
  168. Qid q;
  169. Conv *cv;
  170. Fs *f;
  171. f = ipfs[c->devno];
  172. switch(TYPE(c->qid)) {
  173. case Qtopdir:
  174. if(s == DEVDOTDOT){
  175. mkqid(&q, QID(0, 0, Qtopdir), 0, QTDIR);
  176. snprint(up->genbuf, sizeof up->genbuf, "#I%lu", c->dev);
  177. devdir(c, q, up->genbuf, 0, network, 0555, dp);
  178. return 1;
  179. }
  180. if(s < f->np) {
  181. if(f->p[s]->connect == nil)
  182. return 0; /* protocol with no user interface */
  183. mkqid(&q, QID(s, 0, Qprotodir), 0, QTDIR);
  184. devdir(c, q, f->p[s]->name, 0, network, 0555, dp);
  185. return 1;
  186. }
  187. s -= f->np;
  188. return ip1gen(c, s+Qtopbase, dp);
  189. case Qarp:
  190. case Qbootp:
  191. case Qndb:
  192. case Qlog:
  193. case Qiproute:
  194. case Qipselftab:
  195. return ip1gen(c, TYPE(c->qid), dp);
  196. case Qprotodir:
  197. if(s == DEVDOTDOT){
  198. mkqid(&q, QID(0, 0, Qtopdir), 0, QTDIR);
  199. snprint(up->genbuf, sizeof up->genbuf, "#I%lu", c->dev);
  200. devdir(c, q, up->genbuf, 0, network, 0555, dp);
  201. return 1;
  202. }
  203. if(s < f->p[PROTO(c->qid)]->ac) {
  204. cv = f->p[PROTO(c->qid)]->conv[s];
  205. snprint(up->genbuf, sizeof up->genbuf, "%d", s);
  206. mkqid(&q, QID(PROTO(c->qid), s, Qconvdir), 0, QTDIR);
  207. devdir(c, q, up->genbuf, 0, cv->owner, 0555, dp);
  208. return 1;
  209. }
  210. s -= f->p[PROTO(c->qid)]->ac;
  211. return ip2gen(c, s+Qprotobase, dp);
  212. case Qclone:
  213. case Qstats:
  214. return ip2gen(c, TYPE(c->qid), dp);
  215. case Qconvdir:
  216. if(s == DEVDOTDOT){
  217. s = PROTO(c->qid);
  218. mkqid(&q, QID(s, 0, Qprotodir), 0, QTDIR);
  219. devdir(c, q, f->p[s]->name, 0, network, 0555, dp);
  220. return 1;
  221. }
  222. return ip3gen(c, s+Qconvbase, dp);
  223. case Qctl:
  224. case Qdata:
  225. case Qerr:
  226. case Qlisten:
  227. case Qlocal:
  228. case Qremote:
  229. case Qstatus:
  230. case Qsnoop:
  231. return ip3gen(c, TYPE(c->qid), dp);
  232. }
  233. return -1;
  234. }
  235. static void
  236. ipreset(void)
  237. {
  238. nullmediumlink();
  239. pktmediumlink();
  240. fmtinstall('i', eipfmt);
  241. fmtinstall('I', eipfmt);
  242. fmtinstall('E', eipfmt);
  243. fmtinstall('V', eipfmt);
  244. fmtinstall('M', eipfmt);
  245. }
  246. static Fs*
  247. ipgetfs(int dev)
  248. {
  249. extern void (*ipprotoinit[])(Fs*);
  250. Fs *f;
  251. int i;
  252. if(dev >= Nfs)
  253. return nil;
  254. qlock(&fslock);
  255. if(ipfs[dev] == nil){
  256. f = smalloc(sizeof(Fs));
  257. ip_init(f);
  258. arpinit(f);
  259. netloginit(f);
  260. for(i = 0; ipprotoinit[i]; i++)
  261. ipprotoinit[i](f);
  262. f->dev = dev;
  263. ipfs[dev] = f;
  264. }
  265. qunlock(&fslock);
  266. return ipfs[dev];
  267. }
  268. IPaux*
  269. newipaux(char *owner, char *tag)
  270. {
  271. IPaux *a;
  272. int n;
  273. a = smalloc(sizeof(*a));
  274. kstrdup(&a->owner, owner);
  275. memset(a->tag, ' ', sizeof(a->tag));
  276. n = strlen(tag);
  277. if(n > sizeof(a->tag))
  278. n = sizeof(a->tag);
  279. memmove(a->tag, tag, n);
  280. return a;
  281. }
  282. #define ATTACHER(c) (((IPaux*)((c)->aux))->owner)
  283. static Chan*
  284. ipattach(char* spec)
  285. {
  286. Chan *c;
  287. int devno;
  288. devno = atoi(spec);
  289. if(devno >= Nfs)
  290. error("bad specification");
  291. ipgetfs(devno);
  292. c = devattach('I', spec);
  293. mkqid(&c->qid, QID(0, 0, Qtopdir), 0, QTDIR);
  294. c->devno = devno;
  295. c->aux = newipaux(commonuser(), "none");
  296. return c;
  297. }
  298. static Walkqid*
  299. ipwalk(Chan* c, Chan *nc, char **name, int nname)
  300. {
  301. IPaux *a = c->aux;
  302. Walkqid* w;
  303. w = devwalk(c, nc, name, nname, nil, 0, ipgen);
  304. if(w != nil && w->clone != nil)
  305. w->clone->aux = newipaux(a->owner, a->tag);
  306. return w;
  307. }
  308. static int
  309. ipstat(Chan* c, uint8_t* db, int n)
  310. {
  311. return devstat(c, db, n, nil, 0, ipgen);
  312. }
  313. static int
  314. incoming(void* arg)
  315. {
  316. Conv *conv;
  317. conv = arg;
  318. return conv->incall != nil;
  319. }
  320. static int m2p[] = {
  321. [OREAD] = 4,
  322. [OWRITE] = 2,
  323. [ORDWR] = 6
  324. };
  325. static Chan*
  326. ipopen(Chan* c, int omode)
  327. {
  328. Proc *up = externup();
  329. Conv *cv, *nc;
  330. Proto *p;
  331. int perm;
  332. Fs *f;
  333. perm = m2p[omode&3];
  334. f = ipfs[c->devno];
  335. switch(TYPE(c->qid)) {
  336. default:
  337. break;
  338. case Qndb:
  339. if(omode & (OWRITE|OTRUNC) && !iseve())
  340. error(Eperm);
  341. if((omode & (OWRITE|OTRUNC)) == (OWRITE|OTRUNC))
  342. f->ndb[0] = 0;
  343. break;
  344. case Qlog:
  345. netlogopen(f);
  346. break;
  347. case Qiproute:
  348. case Qarp:
  349. if(omode != OREAD && !iseve())
  350. error(Eperm);
  351. break;
  352. case Qtopdir:
  353. case Qprotodir:
  354. case Qconvdir:
  355. case Qstatus:
  356. case Qremote:
  357. case Qlocal:
  358. case Qstats:
  359. case Qbootp:
  360. case Qipselftab:
  361. if(omode != OREAD)
  362. error(Eperm);
  363. break;
  364. case Qsnoop:
  365. if(omode != OREAD)
  366. error(Eperm);
  367. p = f->p[PROTO(c->qid)];
  368. cv = p->conv[CONV(c->qid)];
  369. if(strcmp(ATTACHER(c), cv->owner) != 0 && !iseve())
  370. error(Eperm);
  371. incref(&cv->snoopers);
  372. break;
  373. case Qclone:
  374. p = f->p[PROTO(c->qid)];
  375. qlock(&p->ql);
  376. if(waserror()){
  377. qunlock(&p->ql);
  378. nexterror();
  379. }
  380. cv = Fsprotoclone(p, ATTACHER(c));
  381. qunlock(&p->ql);
  382. poperror();
  383. if(cv == nil) {
  384. error(Enodev);
  385. break;
  386. }
  387. mkqid(&c->qid, QID(p->x, cv->x, Qctl), 0, QTFILE);
  388. break;
  389. case Qdata:
  390. case Qctl:
  391. case Qerr:
  392. p = f->p[PROTO(c->qid)];
  393. qlock(&p->ql);
  394. cv = p->conv[CONV(c->qid)];
  395. qlock(&cv->ql);
  396. if(waserror()) {
  397. qunlock(&cv->ql);
  398. qunlock(&p->ql);
  399. nexterror();
  400. }
  401. if((perm & (cv->perm>>6)) != perm) {
  402. if(strcmp(ATTACHER(c), cv->owner) != 0)
  403. error(Eperm);
  404. if((perm & cv->perm) != perm)
  405. error(Eperm);
  406. }
  407. cv->inuse++;
  408. if(cv->inuse == 1){
  409. kstrdup(&cv->owner, ATTACHER(c));
  410. cv->perm = 0660;
  411. }
  412. qunlock(&cv->ql);
  413. qunlock(&p->ql);
  414. poperror();
  415. break;
  416. case Qlisten:
  417. cv = f->p[PROTO(c->qid)]->conv[CONV(c->qid)];
  418. if((perm & (cv->perm>>6)) != perm) {
  419. if(strcmp(ATTACHER(c), cv->owner) != 0)
  420. error(Eperm);
  421. if((perm & cv->perm) != perm)
  422. error(Eperm);
  423. }
  424. if(cv->state != Announced)
  425. error("not announced");
  426. if(waserror()){
  427. closeconv(cv);
  428. nexterror();
  429. }
  430. qlock(&cv->ql);
  431. cv->inuse++;
  432. qunlock(&cv->ql);
  433. nc = nil;
  434. while(nc == nil) {
  435. /* give up if we got a hangup */
  436. if(qisclosed(cv->rq))
  437. error("listen hungup");
  438. qlock(&cv->listenq);
  439. if(waserror()) {
  440. qunlock(&cv->listenq);
  441. nexterror();
  442. }
  443. /* wait for a connect */
  444. sleep(&cv->listenr, incoming, cv);
  445. qlock(&cv->ql);
  446. nc = cv->incall;
  447. if(nc != nil){
  448. cv->incall = nc->next;
  449. mkqid(&c->qid, QID(PROTO(c->qid), nc->x, Qctl), 0, QTFILE);
  450. kstrdup(&cv->owner, ATTACHER(c));
  451. }
  452. qunlock(&cv->ql);
  453. qunlock(&cv->listenq);
  454. poperror();
  455. }
  456. closeconv(cv);
  457. poperror();
  458. break;
  459. }
  460. c->mode = openmode(omode);
  461. c->flag |= COPEN;
  462. c->offset = 0;
  463. return c;
  464. }
  465. static void
  466. ipcreate(Chan* c, char* n, int i, int m)
  467. {
  468. error(Eperm);
  469. }
  470. static void
  471. ipremove(Chan *c)
  472. {
  473. error(Eperm);
  474. }
  475. static int
  476. ipwstat(Chan *c, uint8_t *dp, int n)
  477. {
  478. Dir d;
  479. Conv *cv;
  480. Fs *f;
  481. Proto *p;
  482. f = ipfs[c->devno];
  483. switch(TYPE(c->qid)) {
  484. default:
  485. error(Eperm);
  486. break;
  487. case Qctl:
  488. case Qdata:
  489. break;
  490. }
  491. n = convM2D(dp, n, &d, nil);
  492. if(n > 0){
  493. p = f->p[PROTO(c->qid)];
  494. cv = p->conv[CONV(c->qid)];
  495. if(!iseve() && strcmp(ATTACHER(c), cv->owner) != 0)
  496. error(Eperm);
  497. if(d.uid[0])
  498. kstrdup(&cv->owner, d.uid);
  499. cv->perm = d.mode & 0777;
  500. }
  501. return n;
  502. }
  503. void
  504. closeconv(Conv *cv)
  505. {
  506. Conv *nc;
  507. Ipmulti *mp;
  508. qlock(&cv->ql);
  509. if(--cv->inuse > 0) {
  510. qunlock(&cv->ql);
  511. return;
  512. }
  513. /* close all incoming calls since no listen will ever happen */
  514. for(nc = cv->incall; nc; nc = cv->incall){
  515. cv->incall = nc->next;
  516. closeconv(nc);
  517. }
  518. cv->incall = nil;
  519. kstrdup(&cv->owner, network);
  520. cv->perm = 0660;
  521. while((mp = cv->multi) != nil)
  522. ipifcremmulti(cv, mp->ma, mp->ia);
  523. cv->r = nil;
  524. cv->rgen = 0;
  525. cv->p->close(cv);
  526. cv->state = Idle;
  527. qunlock(&cv->ql);
  528. }
  529. static void
  530. ipclose(Chan* c)
  531. {
  532. Fs *f;
  533. f = ipfs[c->devno];
  534. switch(TYPE(c->qid)) {
  535. default:
  536. break;
  537. case Qlog:
  538. if(c->flag & COPEN)
  539. netlogclose(f);
  540. break;
  541. case Qdata:
  542. case Qctl:
  543. case Qerr:
  544. if(c->flag & COPEN)
  545. closeconv(f->p[PROTO(c->qid)]->conv[CONV(c->qid)]);
  546. break;
  547. case Qsnoop:
  548. if(c->flag & COPEN)
  549. decref(&f->p[PROTO(c->qid)]->conv[CONV(c->qid)]->snoopers);
  550. break;
  551. }
  552. free(((IPaux*)c->aux)->owner);
  553. free(c->aux);
  554. }
  555. enum
  556. {
  557. Statelen= 32*1024,
  558. };
  559. static int32_t
  560. ipread(Chan *ch, void *a, int32_t n, int64_t off)
  561. {
  562. Conv *c;
  563. Proto *x;
  564. char *buf, *p;
  565. int32_t rv;
  566. Fs *f;
  567. uint32_t offset = off;
  568. f = ipfs[ch->devno];
  569. p = a;
  570. switch(TYPE(ch->qid)) {
  571. default:
  572. error(Eperm);
  573. case Qtopdir:
  574. case Qprotodir:
  575. case Qconvdir:
  576. return devdirread(ch, a, n, 0, 0, ipgen);
  577. case Qarp:
  578. return arpread(f->arp, a, offset, n);
  579. case Qbootp:
  580. return bootpread(a, offset, n);
  581. case Qndb:
  582. return readstr(offset, a, n, f->ndb);
  583. case Qiproute:
  584. return routeread(f, a, offset, n);
  585. case Qipselftab:
  586. return ipselftabread(f, a, offset, n);
  587. case Qlog:
  588. return netlogread(f, a, offset, n);
  589. case Qctl:
  590. buf = smalloc(16);
  591. snprint(buf, 16, "%lu", CONV(ch->qid));
  592. rv = readstr(offset, p, n, buf);
  593. free(buf);
  594. return rv;
  595. case Qremote:
  596. buf = smalloc(Statelen);
  597. x = f->p[PROTO(ch->qid)];
  598. c = x->conv[CONV(ch->qid)];
  599. if(x->remote == nil) {
  600. snprint(buf, Statelen, "%I!%d\n", c->raddr, c->rport);
  601. } else {
  602. (*x->remote)(c, buf, Statelen-2);
  603. }
  604. rv = readstr(offset, p, n, buf);
  605. free(buf);
  606. return rv;
  607. case Qlocal:
  608. buf = smalloc(Statelen);
  609. x = f->p[PROTO(ch->qid)];
  610. c = x->conv[CONV(ch->qid)];
  611. if(x->local == nil) {
  612. snprint(buf, Statelen, "%I!%d\n", c->laddr, c->lport);
  613. } else {
  614. (*x->local)(c, buf, Statelen-2);
  615. }
  616. rv = readstr(offset, p, n, buf);
  617. free(buf);
  618. return rv;
  619. case Qstatus:
  620. buf = smalloc(Statelen);
  621. x = f->p[PROTO(ch->qid)];
  622. c = x->conv[CONV(ch->qid)];
  623. (*x->state)(c, buf, Statelen-2);
  624. rv = readstr(offset, p, n, buf);
  625. free(buf);
  626. return rv;
  627. case Qdata:
  628. c = f->p[PROTO(ch->qid)]->conv[CONV(ch->qid)];
  629. return qread(c->rq, a, n);
  630. case Qerr:
  631. c = f->p[PROTO(ch->qid)]->conv[CONV(ch->qid)];
  632. return qread(c->eq, a, n);
  633. case Qsnoop:
  634. c = f->p[PROTO(ch->qid)]->conv[CONV(ch->qid)];
  635. return qread(c->sq, a, n);
  636. case Qstats:
  637. x = f->p[PROTO(ch->qid)];
  638. if(x->stats == nil)
  639. error("stats not implemented");
  640. buf = smalloc(Statelen);
  641. (*x->stats)(x, buf, Statelen);
  642. rv = readstr(offset, p, n, buf);
  643. free(buf);
  644. return rv;
  645. }
  646. }
  647. static Block*
  648. ipbread(Chan* ch, int32_t n, int64_t offset)
  649. {
  650. Conv *c;
  651. Proto *x;
  652. Fs *f;
  653. switch(TYPE(ch->qid)){
  654. case Qdata:
  655. f = ipfs[ch->devno];
  656. x = f->p[PROTO(ch->qid)];
  657. c = x->conv[CONV(ch->qid)];
  658. return qbread(c->rq, n);
  659. default:
  660. return devbread(ch, n, offset);
  661. }
  662. }
  663. /*
  664. * set local address to be that of the ifc closest to remote address
  665. */
  666. static void
  667. setladdr(Conv* c)
  668. {
  669. findlocalip(c->p->f, c->laddr, c->raddr);
  670. }
  671. /*
  672. * set a local port making sure the quad of raddr,rport,laddr,lport is unique
  673. */
  674. char*
  675. setluniqueport(Conv* c, int lport)
  676. {
  677. Proto *p;
  678. Conv *xp;
  679. int x;
  680. p = c->p;
  681. qlock(&p->ql);
  682. for(x = 0; x < p->nc; x++){
  683. xp = p->conv[x];
  684. if(xp == nil)
  685. break;
  686. if(xp == c)
  687. continue;
  688. if((xp->state == Connected || xp->state == Announced)
  689. && xp->lport == lport
  690. && xp->rport == c->rport
  691. && ipcmp(xp->raddr, c->raddr) == 0
  692. && ipcmp(xp->laddr, c->laddr) == 0){
  693. qunlock(&p->ql);
  694. return "address in use";
  695. }
  696. }
  697. c->lport = lport;
  698. qunlock(&p->ql);
  699. return nil;
  700. }
  701. /*
  702. * is lport in use by anyone?
  703. */
  704. static int
  705. lportinuse(Proto *p, uint16_t lport)
  706. {
  707. int x;
  708. for(x = 0; x < p->nc && p->conv[x]; x++)
  709. if(p->conv[x]->lport == lport)
  710. return 1;
  711. return 0;
  712. }
  713. /*
  714. * pick a local port and set it
  715. */
  716. char *
  717. setlport(Conv* c)
  718. {
  719. Proto *p;
  720. int i, port;
  721. p = c->p;
  722. qlock(&p->ql);
  723. if(c->restricted){
  724. /* Restricted ports cycle between 600 and 1024. */
  725. for(i=0; i<1024-600; i++){
  726. if(p->nextrport >= 1024 || p->nextrport < 600)
  727. p->nextrport = 600;
  728. port = p->nextrport++;
  729. if(!lportinuse(p, port))
  730. goto chosen;
  731. }
  732. }else{
  733. /*
  734. * Unrestricted ports are chosen randomly
  735. * between 2^15 and 2^16. There are at most
  736. * 4*Nchan = 4096 ports in use at any given time,
  737. * so even in the worst case, a random probe has a
  738. * 1 - 4096/2^15 = 87% chance of success.
  739. * If 64 successive probes fail, there is a bug somewhere
  740. * (or a once in 10^58 event has happened, but that's
  741. * less likely than a venti collision).
  742. */
  743. for(i=0; i<64; i++){
  744. port = (1<<15) + nrand(1<<15);
  745. if(!lportinuse(p, port))
  746. goto chosen;
  747. }
  748. }
  749. qunlock(&p->ql);
  750. /*
  751. * debugging: let's see if we ever get this.
  752. * if we do (and we're a cpu server), we might as well restart
  753. * since we're now unable to service new connections.
  754. */
  755. panic("setlport: out of ports");
  756. return "no ports available";
  757. chosen:
  758. c->lport = port;
  759. qunlock(&p->ql);
  760. return nil;
  761. }
  762. /*
  763. * set a local address and port from a string of the form
  764. * [address!]port[!r]
  765. */
  766. char*
  767. setladdrport(Conv* c, char* str, int announcing)
  768. {
  769. char *p;
  770. char *rv;
  771. uint16_t lport;
  772. uint8_t addr[IPaddrlen];
  773. /*
  774. * ignore restricted part if it exists. it's
  775. * meaningless on local ports.
  776. */
  777. p = strchr(str, '!');
  778. if(p != nil){
  779. *p++ = 0;
  780. if(strcmp(p, "r") == 0)
  781. p = nil;
  782. }
  783. c->lport = 0;
  784. if(p == nil){
  785. if(announcing)
  786. ipmove(c->laddr, IPnoaddr);
  787. else
  788. setladdr(c);
  789. p = str;
  790. } else {
  791. if(strcmp(str, "*") == 0)
  792. ipmove(c->laddr, IPnoaddr);
  793. else {
  794. if(parseip(addr, str) == -1)
  795. return Ebadip;
  796. if(ipforme(c->p->f, addr))
  797. ipmove(c->laddr, addr);
  798. else
  799. return "not a local IP address";
  800. }
  801. }
  802. /* one process can get all connections */
  803. if(announcing && strcmp(p, "*") == 0){
  804. if(!iseve())
  805. error(Eperm);
  806. return setluniqueport(c, 0);
  807. }
  808. lport = atoi(p);
  809. if(lport <= 0)
  810. rv = setlport(c);
  811. else
  812. rv = setluniqueport(c, lport);
  813. return rv;
  814. }
  815. static char*
  816. setraddrport(Conv* c, char* str)
  817. {
  818. char *p;
  819. p = strchr(str, '!');
  820. if(p == nil)
  821. return "malformed address";
  822. *p++ = 0;
  823. if (parseip(c->raddr, str) == -1)
  824. return Ebadip;
  825. c->rport = atoi(p);
  826. p = strchr(p, '!');
  827. if(p){
  828. if(strstr(p, "!r") != nil)
  829. c->restricted = 1;
  830. }
  831. return nil;
  832. }
  833. /*
  834. * called by protocol connect routine to set addresses
  835. */
  836. char*
  837. Fsstdconnect(Conv *c, char *argv[], int argc)
  838. {
  839. char *p;
  840. switch(argc) {
  841. default:
  842. return "bad args to connect";
  843. case 2:
  844. p = setraddrport(c, argv[1]);
  845. if(p != nil)
  846. return p;
  847. setladdr(c);
  848. p = setlport(c);
  849. if (p != nil)
  850. return p;
  851. break;
  852. case 3:
  853. p = setraddrport(c, argv[1]);
  854. if(p != nil)
  855. return p;
  856. p = setladdrport(c, argv[2], 0);
  857. if(p != nil)
  858. return p;
  859. }
  860. if( (memcmp(c->raddr, v4prefix, IPv4off) == 0 &&
  861. memcmp(c->laddr, v4prefix, IPv4off) == 0)
  862. || ipcmp(c->raddr, IPnoaddr) == 0)
  863. c->ipversion = V4;
  864. else
  865. c->ipversion = V6;
  866. return nil;
  867. }
  868. /*
  869. * initiate connection and sleep till its set up
  870. */
  871. static int
  872. connected(void* a)
  873. {
  874. return ((Conv*)a)->state == Connected;
  875. }
  876. static void
  877. connectctlmsg(Proto *x, Conv *c, Cmdbuf *cb)
  878. {
  879. Proc *up = externup();
  880. char *p;
  881. if(c->state != 0)
  882. error(Econinuse);
  883. c->state = Connecting;
  884. c->cerr[0] = '\0';
  885. if(x->connect == nil)
  886. error("connect not supported");
  887. p = x->connect(c, cb->f, cb->nf);
  888. if(p != nil)
  889. error(p);
  890. qunlock(&c->ql);
  891. if(waserror()){
  892. qlock(&c->ql);
  893. nexterror();
  894. }
  895. sleep(&c->cr, connected, c);
  896. qlock(&c->ql);
  897. poperror();
  898. if(c->cerr[0] != '\0')
  899. error(c->cerr);
  900. }
  901. /*
  902. * called by protocol announce routine to set addresses
  903. */
  904. char*
  905. Fsstdannounce(Conv* c, char* argv[], int argc)
  906. {
  907. memset(c->raddr, 0, sizeof(c->raddr));
  908. c->rport = 0;
  909. switch(argc){
  910. default:
  911. break;
  912. case 2:
  913. return setladdrport(c, argv[1], 1);
  914. }
  915. return "bad args to announce";
  916. }
  917. /*
  918. * initiate announcement and sleep till its set up
  919. */
  920. static int
  921. announced(void* a)
  922. {
  923. return ((Conv*)a)->state == Announced;
  924. }
  925. static void
  926. announcectlmsg(Proto *x, Conv *c, Cmdbuf *cb)
  927. {
  928. Proc *up = externup();
  929. char *p;
  930. if(c->state != 0)
  931. error(Econinuse);
  932. c->state = Announcing;
  933. c->cerr[0] = '\0';
  934. if(x->announce == nil)
  935. error("announce not supported");
  936. p = x->announce(c, cb->f, cb->nf);
  937. if(p != nil)
  938. error(p);
  939. qunlock(&c->ql);
  940. if(waserror()){
  941. qlock(&c->ql);
  942. nexterror();
  943. }
  944. sleep(&c->cr, announced, c);
  945. qlock(&c->ql);
  946. poperror();
  947. if(c->cerr[0] != '\0')
  948. error(c->cerr);
  949. }
  950. /*
  951. * called by protocol bind routine to set addresses
  952. */
  953. char*
  954. Fsstdbind(Conv* c, char* argv[], int argc)
  955. {
  956. switch(argc){
  957. default:
  958. break;
  959. case 2:
  960. return setladdrport(c, argv[1], 0);
  961. }
  962. return "bad args to bind";
  963. }
  964. static void
  965. bindctlmsg(Proto *x, Conv *c, Cmdbuf *cb)
  966. {
  967. char *p;
  968. if(x->bind == nil)
  969. p = Fsstdbind(c, cb->f, cb->nf);
  970. else
  971. p = x->bind(c, cb->f, cb->nf);
  972. if(p != nil)
  973. error(p);
  974. }
  975. static void
  976. tosctlmsg(Conv *c, Cmdbuf *cb)
  977. {
  978. if(cb->nf < 2)
  979. c->tos = 0;
  980. else
  981. c->tos = atoi(cb->f[1]);
  982. }
  983. static void
  984. ttlctlmsg(Conv *c, Cmdbuf *cb)
  985. {
  986. if(cb->nf < 2)
  987. c->ttl = MAXTTL;
  988. else
  989. c->ttl = atoi(cb->f[1]);
  990. }
  991. static int32_t
  992. ipwrite(Chan* ch, void *v, int32_t n, int64_t off)
  993. {
  994. Proc *up = externup();
  995. Conv *c;
  996. Proto *x;
  997. char *p;
  998. Cmdbuf *cb;
  999. uint8_t ia[IPaddrlen], ma[IPaddrlen];
  1000. Fs *f;
  1001. char *a;
  1002. uint32_t offset = off;
  1003. a = v;
  1004. f = ipfs[ch->devno];
  1005. switch(TYPE(ch->qid)){
  1006. default:
  1007. error(Eperm);
  1008. case Qdata:
  1009. x = f->p[PROTO(ch->qid)];
  1010. c = x->conv[CONV(ch->qid)];
  1011. if(c->wq == nil)
  1012. error(Eperm);
  1013. qwrite(c->wq, a, n);
  1014. break;
  1015. case Qarp:
  1016. return arpwrite(f, a, n);
  1017. case Qiproute:
  1018. return routewrite(f, ch, a, n);
  1019. case Qlog:
  1020. netlogctl(f, a, n);
  1021. return n;
  1022. case Qndb:
  1023. return ndbwrite(f, a, offset, n);
  1024. break;
  1025. case Qctl:
  1026. x = f->p[PROTO(ch->qid)];
  1027. c = x->conv[CONV(ch->qid)];
  1028. cb = parsecmd(a, n);
  1029. qlock(&c->ql);
  1030. if(waserror()) {
  1031. qunlock(&c->ql);
  1032. free(cb);
  1033. nexterror();
  1034. }
  1035. if(cb->nf < 1)
  1036. error("short control request");
  1037. if(strcmp(cb->f[0], "connect") == 0)
  1038. connectctlmsg(x, c, cb);
  1039. else if(strcmp(cb->f[0], "announce") == 0)
  1040. announcectlmsg(x, c, cb);
  1041. else if(strcmp(cb->f[0], "bind") == 0)
  1042. bindctlmsg(x, c, cb);
  1043. else if(strcmp(cb->f[0], "ttl") == 0)
  1044. ttlctlmsg(c, cb);
  1045. else if(strcmp(cb->f[0], "tos") == 0)
  1046. tosctlmsg(c, cb);
  1047. else if(strcmp(cb->f[0], "ignoreadvice") == 0)
  1048. c->ignoreadvice = 1;
  1049. else if(strcmp(cb->f[0], "addmulti") == 0){
  1050. if(cb->nf < 2)
  1051. error("addmulti needs interface address");
  1052. if(cb->nf == 2){
  1053. if(!ipismulticast(c->raddr))
  1054. error("addmulti for a non multicast address");
  1055. if (parseip(ia, cb->f[1]) == -1)
  1056. error(Ebadip);
  1057. ipifcaddmulti(c, c->raddr, ia);
  1058. } else {
  1059. if (parseip(ia, cb->f[1]) == -1 ||
  1060. parseip(ma, cb->f[2]) == -1)
  1061. error(Ebadip);
  1062. if(!ipismulticast(ma))
  1063. error("addmulti for a non multicast address");
  1064. ipifcaddmulti(c, ma, ia);
  1065. }
  1066. } else if(strcmp(cb->f[0], "remmulti") == 0){
  1067. if(cb->nf < 2)
  1068. error("remmulti needs interface address");
  1069. if(!ipismulticast(c->raddr))
  1070. error("remmulti for a non multicast address");
  1071. if (parseip(ia, cb->f[1]) == -1)
  1072. error(Ebadip);
  1073. ipifcremmulti(c, c->raddr, ia);
  1074. } else if(strcmp(cb->f[0], "maxfragsize") == 0){
  1075. if(cb->nf < 2)
  1076. error("maxfragsize needs size");
  1077. c->maxfragsize = (int)strtol(cb->f[1], nil, 0);
  1078. } else if(x->ctl != nil) {
  1079. p = x->ctl(c, cb->f, cb->nf);
  1080. if(p != nil)
  1081. error(p);
  1082. } else
  1083. error("unknown control request");
  1084. qunlock(&c->ql);
  1085. free(cb);
  1086. poperror();
  1087. }
  1088. return n;
  1089. }
  1090. static int32_t
  1091. ipbwrite(Chan* ch, Block* bp, int64_t offset)
  1092. {
  1093. Conv *c;
  1094. Proto *x;
  1095. Fs *f;
  1096. int n;
  1097. switch(TYPE(ch->qid)){
  1098. case Qdata:
  1099. f = ipfs[ch->devno];
  1100. x = f->p[PROTO(ch->qid)];
  1101. c = x->conv[CONV(ch->qid)];
  1102. if(c->wq == nil)
  1103. error(Eperm);
  1104. if(bp->next)
  1105. bp = concatblock(bp);
  1106. n = BLEN(bp);
  1107. qbwrite(c->wq, bp);
  1108. return n;
  1109. default:
  1110. return devbwrite(ch, bp, offset);
  1111. }
  1112. }
  1113. Dev ipdevtab = {
  1114. .dc = 'I',
  1115. .name = "ip",
  1116. .reset = ipreset,
  1117. .init = devinit,
  1118. .shutdown = devshutdown,
  1119. .attach = ipattach,
  1120. .walk = ipwalk,
  1121. .stat = ipstat,
  1122. .open = ipopen,
  1123. .create = ipcreate,
  1124. .close = ipclose,
  1125. .read = ipread,
  1126. .bread = ipbread,
  1127. .write = ipwrite,
  1128. .bwrite = ipbwrite,
  1129. .remove = ipremove,
  1130. .wstat = ipwstat,
  1131. };
  1132. int
  1133. Fsproto(Fs *f, Proto *p)
  1134. {
  1135. if(f->np >= Maxproto)
  1136. return -1;
  1137. p->f = f;
  1138. if(p->ipproto > 0){
  1139. if(f->t2p[p->ipproto] != nil)
  1140. return -1;
  1141. f->t2p[p->ipproto] = p;
  1142. }
  1143. p->qid.type = QTDIR;
  1144. p->qid.path = QID(f->np, 0, Qprotodir);
  1145. p->conv = malloc(sizeof(Conv*)*(p->nc+1));
  1146. if(p->conv == nil)
  1147. panic("Fsproto");
  1148. p->x = f->np;
  1149. p->nextrport = 600;
  1150. f->p[f->np++] = p;
  1151. return 0;
  1152. }
  1153. /*
  1154. * return true if this protocol is
  1155. * built in
  1156. */
  1157. int
  1158. Fsbuiltinproto(Fs* f, uint8_t proto)
  1159. {
  1160. return f->t2p[proto] != nil;
  1161. }
  1162. /*
  1163. * called with protocol locked
  1164. */
  1165. Conv*
  1166. Fsprotoclone(Proto *p, char *user)
  1167. {
  1168. Conv *c, **pp, **ep;
  1169. retry:
  1170. c = nil;
  1171. ep = &p->conv[p->nc];
  1172. for(pp = p->conv; pp < ep; pp++) {
  1173. c = *pp;
  1174. if(c == nil){
  1175. c = malloc(sizeof(Conv));
  1176. if(c == nil)
  1177. error(Enomem);
  1178. qlock(&c->ql);
  1179. c->p = p;
  1180. c->x = pp - p->conv;
  1181. if(p->ptclsize != 0){
  1182. c->ptcl = malloc(p->ptclsize);
  1183. if(c->ptcl == nil) {
  1184. free(c);
  1185. error(Enomem);
  1186. }
  1187. }
  1188. *pp = c;
  1189. p->ac++;
  1190. c->eq = qopen(1024, Qmsg, 0, 0);
  1191. (*p->create)(c);
  1192. break;
  1193. }
  1194. if(canqlock(&c->ql)){
  1195. /*
  1196. * make sure both processes and protocol
  1197. * are done with this Conv
  1198. */
  1199. if(c->inuse == 0 && (p->inuse == nil || (*p->inuse)(c) == 0))
  1200. break;
  1201. qunlock(&c->ql);
  1202. }
  1203. }
  1204. if(pp >= ep) {
  1205. if(p->gc)
  1206. print("Fsprotoclone: garbage collecting Convs\n");
  1207. if(p->gc != nil && (*p->gc)(p))
  1208. goto retry;
  1209. /* debugging: do we ever get here? */
  1210. if (cpuserver)
  1211. panic("Fsprotoclone: all conversations in use");
  1212. return nil;
  1213. }
  1214. c->inuse = 1;
  1215. kstrdup(&c->owner, user);
  1216. c->perm = 0660;
  1217. c->state = Idle;
  1218. ipmove(c->laddr, IPnoaddr);
  1219. ipmove(c->raddr, IPnoaddr);
  1220. c->r = nil;
  1221. c->rgen = 0;
  1222. c->lport = 0;
  1223. c->rport = 0;
  1224. c->restricted = 0;
  1225. c->maxfragsize = 0;
  1226. c->ttl = MAXTTL;
  1227. qreopen(c->rq);
  1228. qreopen(c->wq);
  1229. qreopen(c->eq);
  1230. qunlock(&c->ql);
  1231. return c;
  1232. }
  1233. int
  1234. Fsconnected(Conv* c, char* msg)
  1235. {
  1236. if(msg != nil && *msg != '\0')
  1237. strncpy(c->cerr, msg, ERRMAX-1);
  1238. switch(c->state){
  1239. case Announcing:
  1240. c->state = Announced;
  1241. break;
  1242. case Connecting:
  1243. c->state = Connected;
  1244. break;
  1245. }
  1246. wakeup(&c->cr);
  1247. return 0;
  1248. }
  1249. Proto*
  1250. Fsrcvpcol(Fs* f, uint8_t proto)
  1251. {
  1252. if(f->ipmux)
  1253. return f->ipmux;
  1254. else
  1255. return f->t2p[proto];
  1256. }
  1257. Proto*
  1258. Fsrcvpcolx(Fs *f, uint8_t proto)
  1259. {
  1260. return f->t2p[proto];
  1261. }
  1262. /*
  1263. * called with protocol locked
  1264. */
  1265. Conv*
  1266. Fsnewcall(Conv *c, uint8_t *raddr, uint16_t rport, uint8_t *laddr, uint16_t lport, uint8_t version)
  1267. {
  1268. Conv *nc;
  1269. Conv **l;
  1270. int i;
  1271. qlock(&c->ql);
  1272. i = 0;
  1273. for(l = &c->incall; *l; l = &(*l)->next)
  1274. i++;
  1275. if(i >= Maxincall) {
  1276. static int beenhere;
  1277. qunlock(&c->ql);
  1278. if (!beenhere) {
  1279. beenhere = 1;
  1280. print("Fsnewcall: incall queue full (%d) on port %d\n",
  1281. i, c->lport);
  1282. }
  1283. return nil;
  1284. }
  1285. /* find a free conversation */
  1286. nc = Fsprotoclone(c->p, network);
  1287. if(nc == nil) {
  1288. qunlock(&c->ql);
  1289. return nil;
  1290. }
  1291. ipmove(nc->raddr, raddr);
  1292. nc->rport = rport;
  1293. ipmove(nc->laddr, laddr);
  1294. nc->lport = lport;
  1295. nc->next = nil;
  1296. *l = nc;
  1297. nc->state = Connected;
  1298. nc->ipversion = version;
  1299. qunlock(&c->ql);
  1300. wakeup(&c->listenr);
  1301. return nc;
  1302. }
  1303. int32_t
  1304. ndbwrite(Fs *f, char *a, uint32_t off, int n)
  1305. {
  1306. if(off > strlen(f->ndb))
  1307. error(Eio);
  1308. if(off+n >= sizeof(f->ndb))
  1309. error(Eio);
  1310. memmove(f->ndb+off, a, n);
  1311. f->ndb[off+n] = 0;
  1312. f->ndbvers++;
  1313. f->ndbmtime = seconds();
  1314. return n;
  1315. }
  1316. uint32_t
  1317. scalednconv(void)
  1318. {
  1319. if(cpuserver && conf.npage*PGSZ >= 128*MB)
  1320. return Nchans*4;
  1321. return Nchans;
  1322. }