tripmedium.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "../port/error.h"
  7. #include "ip.h"
  8. #include "trip.h"
  9. static void tripread(void *a);
  10. static void tripbind(Ipifc *ifc, int argc, char **argv);
  11. static void tripunbind(Ipifc *ifc);
  12. static void tripbwrite(Ipifc *ifc, Block *bp, int version, uchar *ip);
  13. static void tripaddmulti(Ipifc *ifc, uchar*, uchar*);
  14. static void tripremmulti(Ipifc *ifc, uchar*, uchar*);
  15. static void tripaddroute(Ipifc *ifc, int, uchar*, uchar*, uchar*, int);
  16. static void tripremroute(Ipifc *ifc, int, uchar*, uchar*);
  17. static void tripares(Fs*, int, uchar*, uchar*, int, int);
  18. Medium tripmedium =
  19. {
  20. .name= "trip",
  21. .mintu= 20,
  22. .maxtu= 64*1024,
  23. .maclen= LCIMACSIZE,
  24. .bind= tripbind,
  25. .unbind= tripunbind,
  26. .bwrite= tripbwrite,
  27. .addmulti= tripaddmulti,
  28. .remmulti= tripremmulti,
  29. .addroute= tripaddroute,
  30. .remroute= tripremroute,
  31. .ares= tripares,
  32. };
  33. typedef struct Tripinfo Tripinfo;
  34. struct Tripinfo
  35. {
  36. Fs* fs; /* my instance of the IP stack */
  37. Ipifc* ifc; /* IP interface */
  38. Card* dev;
  39. Proc* readp; /* reading process */
  40. Chan* mchan; /* Data channel */
  41. };
  42. /*
  43. * called to bind an IP ifc to an ethernet device
  44. * called with ifc qlock'd
  45. */
  46. static void
  47. tripbind(Ipifc *ifc, int argc, char **argv)
  48. {
  49. int fd;
  50. Chan *mchan;
  51. Tripinfo *er;
  52. if(argc < 2)
  53. error(Ebadarg);
  54. fd = kopen(argv[2], ORDWR);
  55. if(fd < 0)
  56. error("trip open failed");
  57. mchan = fdtochan(up->env->fgrp, fd, ORDWR, 0, 1);
  58. kclose(fd);
  59. if(devtab[mchan->type]->dc != 'T') {
  60. cclose(mchan);
  61. error(Enoport);
  62. }
  63. er = smalloc(sizeof(*er));
  64. er->mchan = mchan;
  65. er->ifc = ifc;
  66. er->dev = tripsetifc(mchan, ifc);
  67. er->fs = ifc->conv->p->f;
  68. ifc->arg = er;
  69. kproc("tripread", tripread, ifc);
  70. }
  71. /*
  72. * called with ifc qlock'd
  73. */
  74. static void
  75. tripunbind(Ipifc *ifc)
  76. {
  77. Tripinfo *er = ifc->arg;
  78. /*
  79. if(er->readp)
  80. postnote(er->readp, 1, "unbind", 0);
  81. */
  82. tsleep(&up->sleep, return0, 0, 300);
  83. if(er->mchan != nil)
  84. cclose(er->mchan);
  85. free(er);
  86. }
  87. /*
  88. * called by ipoput with a single block to write
  89. */
  90. static void
  91. tripbwrite(Ipifc *ifc, Block *bp, int version, uchar *ip)
  92. {
  93. Tripinfo *er = ifc->arg;
  94. /*
  95. * Packet is rerouted at linecard
  96. * so the gateway is ignored
  97. */
  98. USED(ip);
  99. USED(version);
  100. if(waserror()) {
  101. print("tripwrite failed\n");
  102. return;
  103. }
  104. devtab[er->mchan->type]->bwrite(er->mchan, bp, 0);
  105. poperror();
  106. ifc->out++;
  107. }
  108. /*
  109. * process to read from the trip interface
  110. */
  111. static void
  112. tripread(void *a)
  113. {
  114. Ipifc *ifc;
  115. Block *bp;
  116. Tripinfo *er;
  117. ifc = a;
  118. er = ifc->arg;
  119. er->readp = up; /* hide identity under a rock for unbind */
  120. for(;;) {
  121. bp = devtab[er->mchan->type]->bread(er->mchan, ifc->maxtu, 0);
  122. ifc->in++;
  123. ipiput4(er->fs, ifc, bp);
  124. }
  125. pexit("hangup", 1);
  126. }
  127. static void
  128. tripaddroute(Ipifc *ifc, int v, uchar *addr, uchar *mask, uchar *gate, int t)
  129. {
  130. int alen;
  131. MTroute mtr;
  132. Tripinfo *tinfo;
  133. tinfo = ifc->arg;
  134. if(!tinfo->dev->routing)
  135. return;
  136. /*
  137. * Multicast addresses are handled on the linecard by
  138. * the multicast port driver, so the route load is dumped.
  139. * loaded by addmulti/remmulti for SBC routes
  140. * joinmulti/leavemulti for inter LC
  141. */
  142. if(ipismulticast(addr))
  143. return;
  144. mtr.type = T_ROUTEADMIN;
  145. if(v & Rv4) {
  146. mtr.op = RTADD4;
  147. alen = IPv4addrlen;
  148. }
  149. else {
  150. mtr.op = RTADD6;
  151. alen = IPaddrlen;
  152. }
  153. mtr.rtype = t;
  154. memmove(mtr.addr, addr, alen);
  155. memmove(mtr.mask, mask, alen);
  156. memmove(mtr.gate, gate, alen);
  157. i2osend(tinfo->dev, &mtr, sizeof(mtr));
  158. }
  159. static void
  160. tripremroute(Ipifc *ifc, int v, uchar *addr, uchar *mask)
  161. {
  162. int alen;
  163. MTroute mtr;
  164. Tripinfo *tinfo;
  165. tinfo = ifc->arg;
  166. if(!tinfo->dev->routing)
  167. return;
  168. if(ipismulticast(addr))
  169. return;
  170. mtr.type = T_ROUTEADMIN;
  171. if(v & Rv4) {
  172. mtr.op = RTDEL4;
  173. alen = IPv4addrlen;
  174. }
  175. else {
  176. mtr.op = RTDEL6;
  177. alen = IPaddrlen;
  178. }
  179. memmove(mtr.addr, addr, alen);
  180. memmove(mtr.mask, mask, alen);
  181. i2osend(tinfo->dev, &mtr, sizeof(mtr));
  182. }
  183. static void
  184. tripxmitroute(Route *r, Routewalk *rw)
  185. {
  186. int nifc;
  187. char t[5];
  188. uchar a[IPaddrlen], m[IPaddrlen], g[IPaddrlen];
  189. convroute(r, a, m, g, t, &nifc);
  190. if(!(r->type & Rv4)) {
  191. tripaddroute(rw->state, 0, a, m, g, r->type);
  192. return;
  193. }
  194. tripaddroute(rw->state, Rv4, a+IPv4off, m+IPv4off, g+IPv4off, r->type);
  195. }
  196. static void
  197. sendifcinfo(Ipifc *dest)
  198. {
  199. Conv **cp, **e;
  200. Iplifc *l;
  201. Ipifc *ifc;
  202. MTifctl mtc;
  203. Tripinfo *tinfo, *oinfo;
  204. Proto *p;
  205. tinfo = dest->arg;
  206. /* Install interfaces */
  207. p = tinfo->fs->ipifc;
  208. e = &p->conv[p->nc];
  209. for(cp = p->conv; cp < e; cp++) {
  210. if(*cp == nil)
  211. continue;
  212. ifc = (Ipifc*)(*cp)->ptcl;
  213. if(dest == ifc)
  214. continue;
  215. mtc.type = T_CTLIFADMIN;
  216. mtc.maxtu = ifc->maxtu;
  217. mtc.mintu = ifc->mintu;
  218. mtc.port = 0;
  219. if(ifc->m == &tripmedium) {
  220. oinfo = ifc->arg;
  221. mtc.port = oinfo->dev->bar[0].bar;
  222. }
  223. for(l = ifc->lifc; l != nil; l = l->next) {
  224. if(isv4(l->local)) {
  225. mtc.op = IFADD4;
  226. memmove(mtc.addr, l->local+IPv4off, IPv4addrlen);
  227. memmove(mtc.mask, l->mask+IPv4off, IPv4addrlen);
  228. }
  229. else {
  230. mtc.op = IFADD6;
  231. memmove(mtc.addr, l->local, sizeof(mtc.addr));
  232. memmove(mtc.mask, l->mask, sizeof(mtc.mask));
  233. }
  234. i2osend(tinfo->dev, &mtc, sizeof(mtc));
  235. }
  236. }
  237. }
  238. void
  239. tripsync(Ipifc *ifc)
  240. {
  241. Routewalk rw;
  242. if(ifc == nil) {
  243. print("tripsync: interface not bound\n");
  244. return;
  245. }
  246. /* Mirror the route table into the lincard */
  247. rw.o = 0;
  248. rw.n = (1<<22);
  249. rw.state = ifc;
  250. rw.walk = tripxmitroute;
  251. ipwalkroutes(ifc->conv->p->f, &rw);
  252. /*
  253. * Tell the linecard about interfaces that already
  254. * exist elsewhere
  255. */
  256. sendifcinfo(ifc);
  257. }
  258. /* Tell a line card the SBC is interested in listening
  259. * to a multicast address
  260. */
  261. static void
  262. tripaddmulti(Ipifc *ifc, uchar *addr, uchar *ifca)
  263. {
  264. MTmultiears mt;
  265. Tripinfo *tinfo;
  266. /* print("tripaddmulti %I %I\n", addr, ifca); /**/
  267. tinfo = ifc->arg;
  268. if(!tinfo->dev->routing)
  269. return;
  270. mt.type = T_MULTIEAR;
  271. mt.op = ADDMULTI;
  272. memmove(mt.addr, addr, sizeof(mt.addr));
  273. memmove(mt.ifca, ifca, sizeof(mt.ifca));
  274. i2osend(tinfo->dev, &mt, sizeof(mt));
  275. }
  276. /* Tell a line card the SBC is no longer interested in listening
  277. * to a multicast address
  278. */
  279. static void
  280. tripremmulti(Ipifc *ifc, uchar *addr, uchar *ifca)
  281. {
  282. MTmultiears mt;
  283. Tripinfo *tinfo;
  284. tinfo = ifc->arg;
  285. if(!tinfo->dev->routing)
  286. return;
  287. mt.type = T_MULTIEAR;
  288. mt.op = REMMULTI;
  289. memmove(mt.addr, addr, sizeof(mt.addr));
  290. memmove(mt.ifca, ifca, sizeof(mt.ifca));
  291. i2osend(tinfo->dev, &mt, sizeof(mt));
  292. }
  293. static void
  294. tripares(Fs *fs, int vers, uchar *ip, uchar *mac, int l, int)
  295. {
  296. Route *r;
  297. Ipifc *ifc;
  298. MTaresenter ta;
  299. Tripinfo *tinfo;
  300. uchar v6ip[IPaddrlen];
  301. if(vers == V4) {
  302. r = v4lookup(fs, ip);
  303. v4tov6(v6ip, ip);
  304. ip = v6ip;
  305. }
  306. else
  307. r = v6lookup(fs, ip);
  308. if(r == nil) {
  309. print("tripares: no route for entry\n");
  310. return;
  311. }
  312. ifc = r->ifc;
  313. tinfo = ifc->arg;
  314. if(!tinfo->dev->routing)
  315. return;
  316. if(vers == V4) {
  317. v4tov6(v6ip, ip);
  318. ip = v6ip;
  319. }
  320. ta.type = T_ARESENTER;
  321. ta.maclen = l;
  322. memmove(ta.addr, ip, IPaddrlen);
  323. memmove(ta.amac, mac, l);
  324. i2osend(tinfo->dev, &ta, sizeof(ta));
  325. }
  326. void
  327. tripmediumlink(void)
  328. {
  329. addipmedium(&tripmedium);
  330. }