devip.c 25 KB

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