devbridge.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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. /*
  10. * IPv4 Ethernet bridge
  11. */
  12. #include "u.h"
  13. #include "../port/lib.h"
  14. #include "mem.h"
  15. #include "dat.h"
  16. #include "fns.h"
  17. #include "../ip/ip.h"
  18. #include "../port/netif.h"
  19. #include "../port/error.h"
  20. typedef struct Bridge Bridge;
  21. typedef struct Port Port;
  22. typedef struct Centry Centry;
  23. typedef struct Iphdr Iphdr;
  24. typedef struct Tcphdr Tcphdr;
  25. enum {
  26. Qtopdir = 1, /* top level directory */
  27. Qbridgedir, /* bridge* directory */
  28. Qbctl,
  29. Qstats,
  30. Qcache,
  31. Qlog,
  32. Qportdir, /* directory for a protocol */
  33. Qpctl,
  34. Qlocal,
  35. Qstatus,
  36. MaxQ,
  37. Maxbridge = 4,
  38. Maxport = 128, // power of 2
  39. CacheHash = 257, // prime
  40. CacheLook = 5, // how many cache entries to examine
  41. CacheSize = (CacheHash + CacheLook - 1),
  42. CacheTimeout = 5 * 60, // timeout for cache entry in seconds
  43. MaxMTU = IP_MAX, // allow for jumbo frames and large UDP
  44. TcpMssMax = 1300, // max desirable Tcp MSS value
  45. TunnelMtu = 1400,
  46. };
  47. static Dirtab bridgedirtab[] = {
  48. {"ctl", {Qbctl}, 0, 0666},
  49. {"stats", {Qstats}, 0, 0444},
  50. {"cache", {Qcache}, 0, 0444},
  51. {"log", {Qlog}, 0, 0666},
  52. };
  53. static Dirtab portdirtab[] = {
  54. {"ctl", {Qpctl}, 0, 0666},
  55. {"local", {Qlocal}, 0, 0444},
  56. {"status", {Qstatus}, 0, 0444},
  57. };
  58. enum {
  59. Logcache = (1 << 0),
  60. Logmcast = (1 << 1),
  61. };
  62. // types of interfaces
  63. enum {
  64. Tether,
  65. Ttun,
  66. };
  67. static Logflag logflags[] =
  68. {
  69. {
  70. "cache",
  71. Logcache,
  72. },
  73. {
  74. "multicast",
  75. Logmcast,
  76. },
  77. {
  78. nil,
  79. 0,
  80. },
  81. };
  82. static Dirtab *dirtab[MaxQ];
  83. #define TYPE(x) (((u32)(x).path) & 0xff)
  84. #define PORT(x) ((((u32)(x).path) >> 8) & (Maxport - 1))
  85. #define QID(x, y) (((x) << 8) | (y))
  86. struct Centry {
  87. u8 d[Eaddrlen];
  88. int port;
  89. long expire; // entry expires this many seconds after bootime
  90. long src;
  91. long dst;
  92. };
  93. struct Bridge {
  94. QLock QLock;
  95. int nport;
  96. Port *port[Maxport];
  97. Centry cache[CacheSize];
  98. unsigned long hit;
  99. unsigned long miss;
  100. unsigned long copy;
  101. long delay0; // constant microsecond delay per packet
  102. long delayn; // microsecond delay per byte
  103. int tcpmss; // modify tcpmss value
  104. Log Log;
  105. };
  106. struct Port {
  107. Ref Ref;
  108. int id;
  109. Bridge *bridge;
  110. int closed;
  111. Chan *data[2]; // channel to data
  112. Proc *readp; // read proc
  113. // the following uniquely identifies the port
  114. int type;
  115. char name[KNAMELEN];
  116. // owner hash - avoids bind/unbind races
  117. unsigned long ownhash;
  118. // various stats
  119. int in; // number of packets read
  120. int inmulti; // multicast or broadcast
  121. int inunknown; // unknown address
  122. int out; // number of packets read
  123. int outmulti; // multicast or broadcast
  124. int outunknown; // unknown address
  125. int outfrag; // fragmented the packet
  126. int nentry; // number of cache entries for this port
  127. };
  128. enum {
  129. IP_TCPPROTO = 6,
  130. EOLOPT = 0,
  131. NOOPOPT = 1,
  132. MSSOPT = 2,
  133. MSS_LENGTH = 4, /* Mean segment size */
  134. SYN = 0x02, /* Pkt. is synchronise */
  135. IPHDR = 20, /* sizeof(Iphdr) */
  136. };
  137. struct Iphdr {
  138. u8 vihl; /* Version and header length */
  139. u8 tos; /* Type of service */
  140. u8 length[2]; /* packet length */
  141. u8 id[2]; /* ip->identification */
  142. u8 frag[2]; /* Fragment information */
  143. u8 ttl; /* Time to live */
  144. u8 proto; /* Protocol */
  145. u8 cksum[2]; /* Header checksum */
  146. u8 src[4]; /* IP source */
  147. u8 dst[4]; /* IP destination */
  148. };
  149. struct Tcphdr {
  150. u8 sport[2];
  151. u8 dport[2];
  152. u8 seq[4];
  153. u8 ack[4];
  154. u8 flag[2];
  155. u8 win[2];
  156. u8 cksum[2];
  157. u8 urg[2];
  158. };
  159. static Bridge bridgetab[Maxbridge];
  160. static int m2p[] = {
  161. [OREAD] = 4,
  162. [OWRITE] = 2,
  163. [ORDWR] = 6};
  164. static int bridgegen(Chan *c, char *, Dirtab *, int, int s, Dir *dp);
  165. static void portbind(Bridge *b, int argc, char *argv[]);
  166. static void portunbind(Bridge *b, int argc, char *argv[]);
  167. static void etherread(void *a);
  168. static char *cachedump(Bridge *b);
  169. static void portfree(Port *port);
  170. static void cacheflushport(Bridge *b, int port);
  171. static void etherwrite(Port *port, Block *bp);
  172. static void
  173. bridgeinit(void)
  174. {
  175. int i;
  176. Dirtab *dt;
  177. // setup dirtab with non directory entries
  178. for(i = 0; i < nelem(bridgedirtab); i++){
  179. dt = bridgedirtab + i;
  180. dirtab[TYPE(dt->qid)] = dt;
  181. }
  182. for(i = 0; i < nelem(portdirtab); i++){
  183. dt = portdirtab + i;
  184. dirtab[TYPE(dt->qid)] = dt;
  185. }
  186. }
  187. static Chan *
  188. bridgeattach(char *spec)
  189. {
  190. Chan *c;
  191. int dev;
  192. dev = atoi(spec);
  193. print("at %s %d\n", spec, dev);
  194. if(dev < 0 || dev >= Maxbridge)
  195. error("bad specification");
  196. c = devattach('B', spec);
  197. mkqid(&c->qid, QID(0, Qtopdir), 0, QTDIR);
  198. c->devno = dev;
  199. return c;
  200. }
  201. static Walkqid *
  202. bridgewalk(Chan *c, Chan *nc, char **name, int nname)
  203. {
  204. return devwalk(c, nc, name, nname, (Dirtab *)0, 0, bridgegen);
  205. }
  206. static int
  207. bridgestat(Chan *c, u8 *db, int n)
  208. {
  209. return devstat(c, db, n, (Dirtab *)0, 0L, bridgegen);
  210. }
  211. static Chan *
  212. bridgeopen(Chan *c, int omode)
  213. {
  214. int perm;
  215. Bridge *b;
  216. omode &= 3;
  217. perm = m2p[omode];
  218. USED(perm);
  219. b = bridgetab + c->devno;
  220. USED(b);
  221. switch(TYPE(c->qid)){
  222. default:
  223. break;
  224. case Qlog:
  225. logopen(&b->Log);
  226. break;
  227. case Qcache:
  228. c->aux = cachedump(b);
  229. break;
  230. }
  231. c->mode = openmode(omode);
  232. c->flag |= COPEN;
  233. c->offset = 0;
  234. return c;
  235. }
  236. static void
  237. bridgeclose(Chan *c)
  238. {
  239. Bridge *b = bridgetab + c->devno;
  240. switch(TYPE(c->qid)){
  241. case Qcache:
  242. if(c->flag & COPEN)
  243. free(c->aux);
  244. break;
  245. case Qlog:
  246. if(c->flag & COPEN)
  247. logclose(&b->Log);
  248. break;
  249. }
  250. }
  251. static i32
  252. bridgeread(Chan *c, void *a, i32 n, i64 off)
  253. {
  254. Proc *up = externup();
  255. char buf[256];
  256. Bridge *b = bridgetab + c->devno;
  257. Port *port;
  258. int i, ingood, outgood;
  259. USED(off);
  260. switch(TYPE(c->qid)){
  261. default:
  262. error(Egreg);
  263. case Qtopdir:
  264. case Qbridgedir:
  265. case Qportdir:
  266. return devdirread(c, a, n, 0, 0, bridgegen);
  267. case Qlog:
  268. return logread(&b->Log, a, off, n);
  269. case Qlocal:
  270. return 0; /* TO DO */
  271. case Qstatus:
  272. qlock(&b->QLock);
  273. if(waserror()){
  274. qunlock(&b->QLock);
  275. nexterror();
  276. }
  277. port = b->port[PORT(c->qid)];
  278. if(port == 0)
  279. strcpy(buf, "unbound\n");
  280. else {
  281. i = 0;
  282. switch(port->type){
  283. default:
  284. panic("bridgeread: unknown port type: %d",
  285. port->type);
  286. case Tether:
  287. i += snprint(buf + i, sizeof(buf) - i, "ether %s: ", port->name);
  288. break;
  289. case Ttun:
  290. i += snprint(buf + i, sizeof(buf) - i, "tunnel %s: ", port->name);
  291. break;
  292. }
  293. ingood = port->in - port->inmulti - port->inunknown;
  294. outgood = port->out - port->outmulti - port->outunknown;
  295. snprint(buf + i, sizeof(buf) - i,
  296. "in=%d(%d:%d:%d) out=%d(%d:%d:%d:%d)\n",
  297. port->in, ingood, port->inmulti, port->inunknown,
  298. port->out, outgood, port->outmulti,
  299. port->outunknown, port->outfrag);
  300. }
  301. poperror();
  302. qunlock(&b->QLock);
  303. return readstr(off, a, n, buf);
  304. case Qbctl:
  305. snprint(buf, sizeof(buf), "%s tcpmss\ndelay %ld %ld\n",
  306. b->tcpmss ? "set" : "clear", b->delay0, b->delayn);
  307. n = readstr(off, a, n, buf);
  308. return n;
  309. case Qcache:
  310. n = readstr(off, a, n, c->aux);
  311. return n;
  312. case Qstats:
  313. snprint(buf, sizeof(buf), "hit=%lu miss=%lu copy=%lu\n",
  314. b->hit, b->miss, b->copy);
  315. n = readstr(off, a, n, buf);
  316. return n;
  317. }
  318. }
  319. static void
  320. bridgeoption(Bridge *b, char *option, int value)
  321. {
  322. if(strcmp(option, "tcpmss") == 0)
  323. b->tcpmss = value;
  324. else
  325. error("unknown bridge option");
  326. }
  327. static i32
  328. bridgewrite(Chan *c, void *a, i32 n, i64 off)
  329. {
  330. Proc *up = externup();
  331. Bridge *b = bridgetab + c->devno;
  332. Cmdbuf *cb;
  333. char *arg0, *p;
  334. USED(off);
  335. switch(TYPE(c->qid)){
  336. default:
  337. error(Eperm);
  338. case Qbctl:
  339. cb = parsecmd(a, n);
  340. qlock(&b->QLock);
  341. if(waserror()){
  342. qunlock(&b->QLock);
  343. free(cb);
  344. nexterror();
  345. }
  346. if(cb->nf == 0)
  347. error("short write");
  348. arg0 = cb->f[0];
  349. if(strcmp(arg0, "bind") == 0){
  350. portbind(b, cb->nf - 1, cb->f + 1);
  351. } else if(strcmp(arg0, "unbind") == 0){
  352. portunbind(b, cb->nf - 1, cb->f + 1);
  353. } else if(strcmp(arg0, "cacheflush") == 0){
  354. log(&b->Log, Logcache, "cache flush\n");
  355. memset(b->cache, 0, CacheSize * sizeof(Centry));
  356. } else if(strcmp(arg0, "set") == 0){
  357. if(cb->nf != 2)
  358. error("usage: set option");
  359. bridgeoption(b, cb->f[1], 1);
  360. } else if(strcmp(arg0, "clear") == 0){
  361. if(cb->nf != 2)
  362. error("usage: clear option");
  363. bridgeoption(b, cb->f[1], 0);
  364. } else if(strcmp(arg0, "delay") == 0){
  365. if(cb->nf != 3)
  366. error("usage: delay delay0 delayn");
  367. b->delay0 = strtol(cb->f[1], nil, 10);
  368. b->delayn = strtol(cb->f[2], nil, 10);
  369. } else
  370. error("unknown control request");
  371. poperror();
  372. qunlock(&b->QLock);
  373. free(cb);
  374. return n;
  375. case Qlog:
  376. cb = parsecmd(a, n);
  377. p = logctl(&b->Log, cb->nf, cb->f, logflags);
  378. free(cb);
  379. if(p != nil)
  380. error(p);
  381. return n;
  382. }
  383. }
  384. static int
  385. bridgegen(Chan *c, char *_, Dirtab *__, int ___, int s, Dir *dp)
  386. {
  387. Proc *up = externup();
  388. Bridge *b = bridgetab + c->devno;
  389. int type = TYPE(c->qid);
  390. Dirtab *dt;
  391. Qid qid;
  392. if(s == DEVDOTDOT){
  393. switch(TYPE(c->qid)){
  394. case Qtopdir:
  395. case Qbridgedir:
  396. snprint(up->genbuf, sizeof(up->genbuf), "#B%ld", c->devno);
  397. mkqid(&qid, Qtopdir, 0, QTDIR);
  398. devdir(c, qid, up->genbuf, 0, eve, 0555, dp);
  399. break;
  400. case Qportdir:
  401. snprint(up->genbuf, sizeof(up->genbuf), "bridge%ld", c->devno);
  402. mkqid(&qid, Qbridgedir, 0, QTDIR);
  403. devdir(c, qid, up->genbuf, 0, eve, 0555, dp);
  404. break;
  405. default:
  406. panic("bridgewalk %llux", c->qid.path);
  407. }
  408. return 1;
  409. }
  410. switch(type){
  411. default:
  412. /* non-directory entries end up here */
  413. if(c->qid.type & QTDIR)
  414. panic("bridgegen: unexpected directory");
  415. if(s != 0)
  416. return -1;
  417. dt = dirtab[TYPE(c->qid)];
  418. if(dt == nil)
  419. panic("bridgegen: unknown type: %lu", TYPE(c->qid));
  420. devdir(c, c->qid, dt->name, dt->length, eve, dt->perm, dp);
  421. return 1;
  422. case Qtopdir:
  423. if(s != 0)
  424. return -1;
  425. snprint(up->genbuf, sizeof(up->genbuf), "bridge%ld", c->devno);
  426. mkqid(&qid, QID(0, Qbridgedir), 0, QTDIR);
  427. devdir(c, qid, up->genbuf, 0, eve, 0555, dp);
  428. return 1;
  429. case Qbridgedir:
  430. if(s < nelem(bridgedirtab)){
  431. dt = bridgedirtab + s;
  432. devdir(c, dt->qid, dt->name, dt->length, eve, dt->perm, dp);
  433. return 1;
  434. }
  435. s -= nelem(bridgedirtab);
  436. if(s >= b->nport)
  437. return -1;
  438. mkqid(&qid, QID(s, Qportdir), 0, QTDIR);
  439. snprint(up->genbuf, sizeof(up->genbuf), "%d", s);
  440. devdir(c, qid, up->genbuf, 0, eve, 0555, dp);
  441. return 1;
  442. case Qportdir:
  443. if(s >= nelem(portdirtab))
  444. return -1;
  445. dt = portdirtab + s;
  446. mkqid(&qid, QID(PORT(c->qid), TYPE(dt->qid)), 0, QTFILE);
  447. devdir(c, qid, dt->name, dt->length, eve, dt->perm, dp);
  448. return 1;
  449. }
  450. }
  451. // parse mac address; also in netif.c
  452. int
  453. parseaddr(u8 *to, char *from, int alen)
  454. {
  455. char nip[4];
  456. char *p;
  457. int i;
  458. p = from;
  459. for(i = 0; i < alen; i++){
  460. if(*p == 0)
  461. return -1;
  462. nip[0] = *p++;
  463. if(*p == 0)
  464. return -1;
  465. nip[1] = *p++;
  466. nip[2] = 0;
  467. to[i] = strtoul(nip, 0, 16);
  468. if(*p == ':')
  469. p++;
  470. }
  471. return 0;
  472. }
  473. // assumes b is locked
  474. static void
  475. portbind(Bridge *b, int argc, char *argv[])
  476. {
  477. Proc *up = externup();
  478. Port *port;
  479. Chan *ctl;
  480. int type = 0, i, n;
  481. unsigned long ownhash;
  482. char *dev, *dev2 = nil;
  483. char buf[100], name[KNAMELEN], path[8 * KNAMELEN];
  484. static char usage[] = "usage: bind ether|tunnel name ownhash dev [dev2]";
  485. extern Dev *devtab[];
  486. memset(name, 0, KNAMELEN);
  487. if(argc < 4)
  488. error(usage);
  489. if(strcmp(argv[0], "ether") == 0){
  490. if(argc != 4)
  491. error(usage);
  492. type = Tether;
  493. strncpy(name, argv[1], KNAMELEN);
  494. name[KNAMELEN - 1] = 0;
  495. // parseaddr(addr, argv[1], Eaddrlen);
  496. } else if(strcmp(argv[0], "tunnel") == 0){
  497. if(argc != 5)
  498. error(usage);
  499. type = Ttun;
  500. strncpy(name, argv[1], KNAMELEN);
  501. name[KNAMELEN - 1] = 0;
  502. // parseip(addr, argv[1]);
  503. dev2 = argv[4];
  504. } else
  505. error(usage);
  506. ownhash = atoi(argv[2]);
  507. dev = argv[3];
  508. for(i = 0; i < b->nport; i++){
  509. port = b->port[i];
  510. if(port != nil && port->type == type &&
  511. memcmp(port->name, name, KNAMELEN) == 0)
  512. error("port in use");
  513. }
  514. for(i = 0; i < Maxport; i++)
  515. if(b->port[i] == nil)
  516. break;
  517. if(i == Maxport)
  518. error("no more ports");
  519. port = smalloc(sizeof(Port));
  520. port->Ref.ref = 1;
  521. port->id = i;
  522. port->ownhash = ownhash;
  523. if(waserror()){
  524. portfree(port);
  525. nexterror();
  526. }
  527. port->type = type;
  528. memmove(port->name, name, KNAMELEN);
  529. switch(port->type){
  530. default:
  531. panic("portbind: unknown port type: %d", type);
  532. case Tether:
  533. snprint(path, sizeof(path), "%s/clone", dev);
  534. ctl = namec(path, Aopen, ORDWR, 0);
  535. if(waserror()){
  536. cclose(ctl);
  537. nexterror();
  538. }
  539. // check addr?
  540. // get directory name
  541. n = devtab[ctl->qid.type]->read(ctl, buf, sizeof(buf) - 1, 0);
  542. buf[n] = 0;
  543. snprint(path, sizeof(path), "%s/%lu/data", dev, strtoul(buf, 0, 0));
  544. // setup connection to be promiscuous
  545. snprint(buf, sizeof(buf), "connect -1");
  546. devtab[ctl->qid.type]->write(ctl, buf, strlen(buf), 0);
  547. snprint(buf, sizeof(buf), "promiscuous");
  548. devtab[ctl->qid.type]->write(ctl, buf, strlen(buf), 0);
  549. snprint(buf, sizeof(buf), "bridge");
  550. devtab[ctl->qid.type]->write(ctl, buf, strlen(buf), 0);
  551. // open data port
  552. port->data[0] = namec(path, Aopen, ORDWR, 0);
  553. // dup it
  554. incref(&port->data[0]->r);
  555. port->data[1] = port->data[0];
  556. poperror();
  557. cclose(ctl);
  558. break;
  559. case Ttun:
  560. port->data[0] = namec(dev, Aopen, OREAD, 0);
  561. port->data[1] = namec(dev2, Aopen, OWRITE, 0);
  562. break;
  563. }
  564. poperror();
  565. /* committed to binding port */
  566. b->port[port->id] = port;
  567. port->bridge = b;
  568. if(b->nport <= port->id)
  569. b->nport = port->id + 1;
  570. // assumes kproc always succeeds
  571. incref(&port->Ref);
  572. snprint(buf, sizeof(buf), "bridge:%s", dev);
  573. kproc(buf, etherread, port);
  574. }
  575. // assumes b is locked
  576. static void
  577. portunbind(Bridge *b, int argc, char *argv[])
  578. {
  579. int type = 0, i;
  580. char name[KNAMELEN];
  581. unsigned long ownhash;
  582. Port *port = nil;
  583. static char usage[] = "usage: unbind ether|tunnel addr [ownhash]";
  584. memset(name, 0, KNAMELEN);
  585. if(argc < 2 || argc > 3)
  586. error(usage);
  587. if(strcmp(argv[0], "ether") == 0){
  588. type = Tether;
  589. strncpy(name, argv[1], KNAMELEN);
  590. name[KNAMELEN - 1] = 0;
  591. // parseaddr(addr, argv[1], Eaddrlen);
  592. } else if(strcmp(argv[0], "tunnel") == 0){
  593. type = Ttun;
  594. strncpy(name, argv[1], KNAMELEN);
  595. name[KNAMELEN - 1] = 0;
  596. // parseip(addr, argv[1]);
  597. } else
  598. error(usage);
  599. if(argc == 3)
  600. ownhash = atoi(argv[2]);
  601. else
  602. ownhash = 0;
  603. for(i = 0; i < b->nport; i++){
  604. port = b->port[i];
  605. if(port != nil && port->type == type &&
  606. memcmp(port->name, name, KNAMELEN) == 0)
  607. break;
  608. }
  609. if(i == b->nport)
  610. error("port not found");
  611. if(ownhash != 0 && port->ownhash != 0 && ownhash != port->ownhash)
  612. error("bad owner hash");
  613. port->closed = 1;
  614. b->port[i] = nil; // port is now unbound
  615. cacheflushport(b, i);
  616. // try and stop reader
  617. if(port->readp)
  618. postnote(port->readp, 1, "unbind", 0);
  619. portfree(port);
  620. }
  621. // assumes b is locked
  622. static Centry *
  623. cachelookup(Bridge *b, u8 d[Eaddrlen])
  624. {
  625. int i;
  626. u32 h;
  627. Centry *p;
  628. long sec;
  629. // dont cache multicast or broadcast
  630. if(d[0] & 1)
  631. return 0;
  632. h = 0;
  633. for(i = 0; i < Eaddrlen; i++){
  634. h *= 7;
  635. h += d[i];
  636. }
  637. h %= CacheHash;
  638. p = b->cache + h;
  639. sec = TK2SEC(machp()->ticks);
  640. for(i = 0; i < CacheLook; i++, p++){
  641. if(memcmp(d, p->d, Eaddrlen) == 0){
  642. p->dst++;
  643. if(sec >= p->expire){
  644. log(&b->Log, Logcache, "expired cache entry: %E %d\n",
  645. d, p->port);
  646. return nil;
  647. }
  648. p->expire = sec + CacheTimeout;
  649. return p;
  650. }
  651. }
  652. log(&b->Log, Logcache, "cache miss: %E\n", d);
  653. return nil;
  654. }
  655. // assumes b is locked
  656. static void
  657. cacheupdate(Bridge *b, u8 d[Eaddrlen], int port)
  658. {
  659. int i;
  660. u32 h;
  661. Centry *p, *pp;
  662. long sec;
  663. // dont cache multicast or broadcast
  664. if(d[0] & 1){
  665. log(&b->Log, Logcache, "bad source address: %E\n", d);
  666. return;
  667. }
  668. h = 0;
  669. for(i = 0; i < Eaddrlen; i++){
  670. h *= 7;
  671. h += d[i];
  672. }
  673. h %= CacheHash;
  674. p = b->cache + h;
  675. pp = p;
  676. sec = p->expire;
  677. // look for oldest entry
  678. for(i = 0; i < CacheLook; i++, p++){
  679. if(memcmp(p->d, d, Eaddrlen) == 0){
  680. p->expire = TK2SEC(machp()->ticks) + CacheTimeout;
  681. if(p->port != port){
  682. log(&b->Log, Logcache, "NIC changed port %d->%d: %E\n",
  683. p->port, port, d);
  684. p->port = port;
  685. }
  686. p->src++;
  687. return;
  688. }
  689. if(p->expire < sec){
  690. sec = p->expire;
  691. pp = p;
  692. }
  693. }
  694. if(pp->expire != 0)
  695. log(&b->Log, Logcache, "bumping from cache: %E %d\n", pp->d, pp->port);
  696. pp->expire = TK2SEC(machp()->ticks) + CacheTimeout;
  697. memmove(pp->d, d, Eaddrlen);
  698. pp->port = port;
  699. pp->src = 1;
  700. pp->dst = 0;
  701. log(&b->Log, Logcache, "adding to cache: %E %d\n", pp->d, pp->port);
  702. }
  703. // assumes b is locked
  704. static void
  705. cacheflushport(Bridge *b, int port)
  706. {
  707. Centry *ce;
  708. int i;
  709. ce = b->cache;
  710. for(i = 0; i < CacheSize; i++, ce++){
  711. if(ce->port != port)
  712. continue;
  713. memset(ce, 0, sizeof(Centry));
  714. }
  715. }
  716. static char *
  717. cachedump(Bridge *b)
  718. {
  719. Proc *up = externup();
  720. int i, n;
  721. long sec, off;
  722. char *buf, *p, *ep;
  723. Centry *ce;
  724. char c;
  725. qlock(&b->QLock);
  726. if(waserror()){
  727. qunlock(&b->QLock);
  728. nexterror();
  729. }
  730. sec = TK2SEC(machp()->ticks);
  731. n = 0;
  732. for(i = 0; i < CacheSize; i++)
  733. if(b->cache[i].expire != 0)
  734. n++;
  735. n *= 51; // change if print format is changed
  736. n += 10; // some slop at the end
  737. buf = malloc(n);
  738. if(buf == nil)
  739. error(Enomem);
  740. p = buf;
  741. ep = buf + n;
  742. ce = b->cache;
  743. off = seconds() - sec;
  744. for(i = 0; i < CacheSize; i++, ce++){
  745. if(ce->expire == 0)
  746. continue;
  747. c = (sec < ce->expire) ? 'v' : 'e';
  748. p += snprint(p, ep - p, "%E %2d %10ld %10ld %10ld %c\n", ce->d,
  749. ce->port, ce->src, ce->dst, ce->expire + off, c);
  750. }
  751. *p = 0;
  752. poperror();
  753. qunlock(&b->QLock);
  754. return buf;
  755. }
  756. // assumes b is locked, no error return
  757. static void
  758. ethermultiwrite(Bridge *b, Block *bp, Port *port)
  759. {
  760. Port *oport;
  761. Etherpkt *ep;
  762. int i, mcast;
  763. ep = (Etherpkt *)bp->rp;
  764. mcast = ep->d[0] & 1; /* multicast bit of ethernet address */
  765. oport = nil;
  766. for(i = 0; i < b->nport; i++){
  767. if(i == port->id || b->port[i] == nil)
  768. continue;
  769. /*
  770. * we need to forward multicast packets for ipv6,
  771. * so always do it.
  772. */
  773. if(mcast)
  774. b->port[i]->outmulti++;
  775. else
  776. b->port[i]->outunknown++;
  777. // delay one so that the last write does not copy
  778. if(oport != nil){
  779. b->copy++;
  780. etherwrite(oport, copyblock(bp, blocklen(bp)));
  781. }
  782. oport = b->port[i];
  783. }
  784. // last write free block
  785. if(oport)
  786. etherwrite(oport, bp);
  787. else
  788. freeb(bp);
  789. }
  790. static void
  791. tcpmsshack(Etherpkt *epkt, int n)
  792. {
  793. int hl, optlen;
  794. Iphdr *iphdr;
  795. Tcphdr *tcphdr;
  796. unsigned long mss, cksum;
  797. u8 *optr;
  798. /* ignore non-ipv4 packets */
  799. if(nhgets(epkt->type) != ETIP4)
  800. return;
  801. iphdr = (Iphdr *)(epkt->data);
  802. n -= ETHERHDRSIZE;
  803. if(n < IPHDR)
  804. return;
  805. /* ignore bad packets */
  806. if(iphdr->vihl != (IP_VER4 | IP_HLEN4)){
  807. hl = (iphdr->vihl & 0xF) << 2;
  808. if((iphdr->vihl & 0xF0) != IP_VER4 || hl < (IP_HLEN4 << 2))
  809. return;
  810. } else
  811. hl = IP_HLEN4 << 2;
  812. /* ignore non-tcp packets */
  813. if(iphdr->proto != IP_TCPPROTO)
  814. return;
  815. n -= hl;
  816. if(n < sizeof(Tcphdr))
  817. return;
  818. tcphdr = (Tcphdr *)((u8 *)(iphdr) + hl);
  819. // MSS can only appear in SYN packet
  820. if(!(tcphdr->flag[1] & SYN))
  821. return;
  822. hl = (tcphdr->flag[0] & 0xf0) >> 2;
  823. if(n < hl)
  824. return;
  825. // check for MSS option
  826. optr = (u8 *)tcphdr + sizeof(Tcphdr);
  827. n = hl - sizeof(Tcphdr);
  828. for(;;){
  829. if(n <= 0 || *optr == EOLOPT)
  830. return;
  831. if(*optr == NOOPOPT){
  832. n--;
  833. optr++;
  834. continue;
  835. }
  836. optlen = optr[1];
  837. if(optlen < 2 || optlen > n)
  838. return;
  839. if(*optr == MSSOPT && optlen == MSS_LENGTH)
  840. break;
  841. n -= optlen;
  842. optr += optlen;
  843. }
  844. mss = nhgets(optr + 2);
  845. if(mss <= TcpMssMax)
  846. return;
  847. // fit checksum
  848. cksum = nhgets(tcphdr->cksum);
  849. if((optr - (u8 *)tcphdr) & 1){
  850. print("tcpmsshack: odd alignment!\n");
  851. // odd alignments are a pain
  852. cksum += nhgets(optr + 1);
  853. cksum -= (optr[1] << 8) | (TcpMssMax >> 8);
  854. cksum += (cksum >> 16);
  855. cksum &= 0xffff;
  856. cksum += nhgets(optr + 3);
  857. cksum -= ((TcpMssMax & 0xff) << 8) | optr[4];
  858. cksum += (cksum >> 16);
  859. } else {
  860. cksum += mss;
  861. cksum -= TcpMssMax;
  862. cksum += (cksum >> 16);
  863. }
  864. hnputs(tcphdr->cksum, cksum);
  865. hnputs(optr + 2, TcpMssMax);
  866. }
  867. /*
  868. * process to read from the ethernet
  869. */
  870. static void
  871. etherread(void *a)
  872. {
  873. Proc *up = externup();
  874. Port *port = a;
  875. Bridge *b = port->bridge;
  876. Block *bp;
  877. Etherpkt *ep;
  878. Centry *ce;
  879. long md, n;
  880. extern Dev *devtab[];
  881. qlock(&b->QLock);
  882. port->readp = up; /* hide identity under a rock for unbind */
  883. while(!port->closed){
  884. // release lock to read - error means it is time to quit
  885. qunlock(&b->QLock);
  886. if(waserror()){
  887. print("etherread read error: %s\n", up->errstr);
  888. qlock(&b->QLock);
  889. break;
  890. }
  891. bp = devtab[port->data[0]->qid.type]->bread(port->data[0], MaxMTU, 0);
  892. poperror();
  893. qlock(&b->QLock);
  894. if(bp == nil)
  895. break;
  896. n = blocklen(bp);
  897. if(port->closed || n < ETHERMINTU){
  898. freeb(bp);
  899. continue;
  900. }
  901. if(waserror()){
  902. // print("etherread bridge error\n");
  903. freeb(bp);
  904. continue;
  905. }
  906. port->in++;
  907. ep = (Etherpkt *)bp->rp;
  908. cacheupdate(b, ep->s, port->id);
  909. if(b->tcpmss)
  910. tcpmsshack(ep, n);
  911. /*
  912. * delay packets to simulate a slow link
  913. */
  914. if(b->delay0 != 0 || b->delayn != 0){
  915. md = b->delay0 + b->delayn * n;
  916. if(md > 0)
  917. microdelay(md);
  918. }
  919. poperror(); /* must now dispose of bp */
  920. if(ep->d[0] & 1){
  921. log(&b->Log, Logmcast, "multicast: port=%d src=%E dst=%E type=%#.4ux\n",
  922. port->id, ep->s, ep->d, ep->type[0] << 8 | ep->type[1]);
  923. port->inmulti++;
  924. ethermultiwrite(b, bp, port);
  925. } else {
  926. ce = cachelookup(b, ep->d);
  927. if(ce == nil){
  928. b->miss++;
  929. port->inunknown++;
  930. ethermultiwrite(b, bp, port);
  931. } else if(ce->port != port->id){
  932. b->hit++;
  933. etherwrite(b->port[ce->port], bp);
  934. } else
  935. freeb(bp);
  936. }
  937. }
  938. // print("etherread: trying to exit\n");
  939. port->readp = nil;
  940. portfree(port);
  941. qunlock(&b->QLock);
  942. pexit("hangup", 1);
  943. }
  944. static int
  945. fragment(Etherpkt *epkt, int n)
  946. {
  947. Iphdr *iphdr;
  948. if(n <= TunnelMtu)
  949. return 0;
  950. /* ignore non-ipv4 packets */
  951. if(nhgets(epkt->type) != ETIP4)
  952. return 0;
  953. iphdr = (Iphdr *)(epkt->data);
  954. n -= ETHERHDRSIZE;
  955. /*
  956. * ignore: IP runt packets, bad packets (I don't handle IP
  957. * options for the moment), packets with don't-fragment set,
  958. * and short blocks.
  959. */
  960. if(n < IPHDR || iphdr->vihl != (IP_VER4 | IP_HLEN4) ||
  961. iphdr->frag[0] & (IP_DF >> 8) || nhgets(iphdr->length) > n)
  962. return 0;
  963. return 1;
  964. }
  965. static void
  966. etherwrite(Port *port, Block *bp)
  967. {
  968. Proc *up = externup();
  969. Iphdr *eh, *feh;
  970. Etherpkt *epkt;
  971. int n, lid, len, seglen, chunk, dlen, blklen, offset, mf;
  972. Block *xp, *nb;
  973. u16 fragoff, frag;
  974. extern Dev *devtab[];
  975. port->out++;
  976. epkt = (Etherpkt *)bp->rp;
  977. n = blocklen(bp);
  978. if(port->type != Ttun || !fragment(epkt, n)){
  979. if(!waserror()){
  980. devtab[port->data[1]->qid.type]->bwrite(port->data[1], bp, 0);
  981. poperror();
  982. }
  983. return;
  984. }
  985. port->outfrag++;
  986. if(waserror()){
  987. freeblist(bp);
  988. return;
  989. }
  990. seglen = (TunnelMtu - ETHERHDRSIZE - IPHDR) & ~7;
  991. eh = (Iphdr *)(epkt->data);
  992. len = nhgets(eh->length);
  993. frag = nhgets(eh->frag);
  994. mf = frag & IP_MF;
  995. frag <<= 3;
  996. dlen = len - IPHDR;
  997. xp = bp;
  998. lid = nhgets(eh->id);
  999. offset = ETHERHDRSIZE + IPHDR;
  1000. while(xp != nil && offset && offset >= BLEN(xp)){
  1001. offset -= BLEN(xp);
  1002. xp = xp->next;
  1003. }
  1004. xp->rp += offset;
  1005. if(0)
  1006. print("seglen=%d, dlen=%d, mf=%x, frag=%d\n",
  1007. seglen, dlen, mf, frag);
  1008. for(fragoff = 0; fragoff < dlen; fragoff += seglen){
  1009. nb = allocb(ETHERHDRSIZE + IPHDR + seglen);
  1010. feh = (Iphdr *)(nb->wp + ETHERHDRSIZE);
  1011. memmove(nb->wp, epkt, ETHERHDRSIZE + IPHDR);
  1012. nb->wp += ETHERHDRSIZE + IPHDR;
  1013. if((fragoff + seglen) >= dlen){
  1014. seglen = dlen - fragoff;
  1015. hnputs(feh->frag, ((frag + fragoff) >> 3) | mf);
  1016. } else
  1017. hnputs(feh->frag, ((frag + fragoff) >> 3) | IP_MF);
  1018. hnputs(feh->length, seglen + IPHDR);
  1019. hnputs(feh->id, lid);
  1020. /* Copy up the data area */
  1021. chunk = seglen;
  1022. while(chunk){
  1023. blklen = chunk;
  1024. if(BLEN(xp) < chunk)
  1025. blklen = BLEN(xp);
  1026. memmove(nb->wp, xp->rp, blklen);
  1027. nb->wp += blklen;
  1028. xp->rp += blklen;
  1029. chunk -= blklen;
  1030. if(xp->rp == xp->wp)
  1031. xp = xp->next;
  1032. }
  1033. feh->cksum[0] = 0;
  1034. feh->cksum[1] = 0;
  1035. hnputs(feh->cksum, ipcsum(&feh->vihl));
  1036. /* don't generate small packets */
  1037. if(BLEN(nb) < ETHERMINTU)
  1038. nb->wp = nb->rp + ETHERMINTU;
  1039. devtab[port->data[1]->qid.type]->bwrite(port->data[1], nb, 0);
  1040. }
  1041. poperror();
  1042. freeblist(bp);
  1043. }
  1044. // hold b lock
  1045. static void
  1046. portfree(Port *port)
  1047. {
  1048. if(decref(&port->Ref) != 0)
  1049. return;
  1050. if(port->data[0])
  1051. cclose(port->data[0]);
  1052. if(port->data[1])
  1053. cclose(port->data[1]);
  1054. memset(port, 0, sizeof(Port));
  1055. free(port);
  1056. }
  1057. Dev bridgedevtab = {
  1058. 'B',
  1059. "bridge",
  1060. devreset,
  1061. bridgeinit,
  1062. devshutdown,
  1063. bridgeattach,
  1064. bridgewalk,
  1065. bridgestat,
  1066. bridgeopen,
  1067. devcreate,
  1068. bridgeclose,
  1069. bridgeread,
  1070. devbread,
  1071. bridgewrite,
  1072. devbwrite,
  1073. devremove,
  1074. devwstat,
  1075. };