ipv6.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 "ipv6.h"
  9. enum
  10. {
  11. IP6FHDR = 8, /* sizeof(Fraghdr6) */
  12. };
  13. #define IPV6CLASS(hdr) (((hdr)->vcf[0]&0x0F)<<2 | ((hdr)->vcf[1]&0xF0)>>2)
  14. #define BLKIPVER(xp) (((Ip6hdr*)((xp)->rp))->vcf[0] & 0xF0)
  15. /*
  16. * This sleazy macro is stolen shamelessly from ip.c, see comment there.
  17. */
  18. #define BKFG(xp) ((Ipfrag*)((xp)->base))
  19. Block* ip6reassemble(IP*, int, Block*, Ip6hdr*);
  20. Fragment6* ipfragallo6(IP*);
  21. void ipfragfree6(IP*, Fragment6*);
  22. Block* procopts(Block *bp);
  23. static Block* procxtns(IP *ip, Block *bp, int doreasm);
  24. int unfraglen(Block *bp, uchar *nexthdr, int setfh);
  25. int
  26. ipoput6(Fs *f, Block *bp, int gating, int ttl, int tos, Conv *c)
  27. {
  28. int medialen, len, chunk, uflen, flen, seglen, lid, offset, fragoff;
  29. int morefrags, blklen, rv = 0, tentative;
  30. uchar *gate, nexthdr;
  31. Block *xp, *nb;
  32. Fraghdr6 fraghdr;
  33. IP *ip;
  34. Ip6hdr *eh;
  35. Ipifc *ifc;
  36. Route *r, *sr;
  37. ip = f->ip;
  38. /* Fill out the ip header */
  39. eh = (Ip6hdr*)(bp->rp);
  40. ip->stats[OutRequests]++;
  41. /* Number of uchars in data and ip header to write */
  42. len = blocklen(bp);
  43. tentative = iptentative(f, eh->src);
  44. if(tentative){
  45. netlog(f, Logip, "reject tx of packet with tentative src address %I\n",
  46. eh->src);
  47. goto free;
  48. }
  49. if(gating){
  50. chunk = nhgets(eh->ploadlen);
  51. if(chunk > len){
  52. ip->stats[OutDiscards]++;
  53. netlog(f, Logip, "short gated packet\n");
  54. goto free;
  55. }
  56. if(chunk + IP6HDR < len)
  57. len = chunk + IP6HDR;
  58. }
  59. if(len >= IP_MAX){
  60. ip->stats[OutDiscards]++;
  61. netlog(f, Logip, "exceeded ip max size %I\n", eh->dst);
  62. goto free;
  63. }
  64. r = v6lookup(f, eh->dst, c);
  65. if(r == nil){
  66. // print("no route for %I, src %I free\n", eh->dst, eh->src);
  67. ip->stats[OutNoRoutes]++;
  68. netlog(f, Logip, "no interface %I\n", eh->dst);
  69. rv = -1;
  70. goto free;
  71. }
  72. ifc = r->ifc;
  73. if(r->type & (Rifc|Runi))
  74. gate = eh->dst;
  75. else if(r->type & (Rbcast|Rmulti)) {
  76. gate = eh->dst;
  77. sr = v6lookup(f, eh->src, nil);
  78. if(sr && (sr->type & Runi))
  79. ifc = sr->ifc;
  80. }
  81. else
  82. gate = r->v6.gate;
  83. if(!gating)
  84. eh->vcf[0] = IP_VER6;
  85. eh->ttl = ttl;
  86. if(!gating) {
  87. eh->vcf[0] |= tos >> 4;
  88. eh->vcf[1] = tos << 4;
  89. }
  90. if(!canrlock(ifc))
  91. goto free;
  92. if(waserror()){
  93. runlock(ifc);
  94. nexterror();
  95. }
  96. if(ifc->m == nil)
  97. goto raise;
  98. /* If we dont need to fragment just send it */
  99. medialen = ifc->maxtu - ifc->m->hsize;
  100. if(len <= medialen) {
  101. hnputs(eh->ploadlen, len - IP6HDR);
  102. ifc->m->bwrite(ifc, bp, V6, gate);
  103. runlock(ifc);
  104. poperror();
  105. return 0;
  106. }
  107. if(gating && ifc->reassemble <= 0) {
  108. /*
  109. * v6 intermediate nodes are not supposed to fragment pkts;
  110. * we fragment if ifc->reassemble is turned on; an exception
  111. * needed for nat.
  112. */
  113. ip->stats[OutDiscards]++;
  114. icmppkttoobig6(f, ifc, bp);
  115. netlog(f, Logip, "%I: gated pkts not fragmented\n", eh->dst);
  116. goto raise;
  117. }
  118. /* start v6 fragmentation */
  119. uflen = unfraglen(bp, &nexthdr, 1);
  120. if(uflen > medialen) {
  121. ip->stats[FragFails]++;
  122. ip->stats[OutDiscards]++;
  123. netlog(f, Logip, "%I: unfragmentable part too big\n", eh->dst);
  124. goto raise;
  125. }
  126. flen = len - uflen;
  127. seglen = (medialen - (uflen + IP6FHDR)) & ~7;
  128. if(seglen < 8) {
  129. ip->stats[FragFails]++;
  130. ip->stats[OutDiscards]++;
  131. netlog(f, Logip, "%I: seglen < 8\n", eh->dst);
  132. goto raise;
  133. }
  134. lid = incref(&ip->id6);
  135. fraghdr.nexthdr = nexthdr;
  136. fraghdr.res = 0;
  137. hnputl(fraghdr.id, lid);
  138. xp = bp;
  139. offset = uflen;
  140. while (xp && offset && offset >= BLEN(xp)) {
  141. offset -= BLEN(xp);
  142. xp = xp->next;
  143. }
  144. xp->rp += offset;
  145. fragoff = 0;
  146. morefrags = 1;
  147. for(; fragoff < flen; fragoff += seglen) {
  148. nb = allocb(uflen + IP6FHDR + seglen);
  149. if(fragoff + seglen >= flen) {
  150. seglen = flen - fragoff;
  151. morefrags = 0;
  152. }
  153. hnputs(eh->ploadlen, seglen+IP6FHDR);
  154. memmove(nb->wp, eh, uflen);
  155. nb->wp += uflen;
  156. hnputs(fraghdr.offsetRM, fragoff); /* last 3 bits must be 0 */
  157. fraghdr.offsetRM[1] |= morefrags;
  158. memmove(nb->wp, &fraghdr, IP6FHDR);
  159. nb->wp += IP6FHDR;
  160. /* Copy data */
  161. chunk = seglen;
  162. while (chunk) {
  163. if(!xp) {
  164. ip->stats[OutDiscards]++;
  165. ip->stats[FragFails]++;
  166. freeblist(nb);
  167. netlog(f, Logip, "!xp: chunk in v6%d\n", chunk);
  168. goto raise;
  169. }
  170. blklen = chunk;
  171. if(BLEN(xp) < chunk)
  172. blklen = BLEN(xp);
  173. memmove(nb->wp, xp->rp, blklen);
  174. nb->wp += blklen;
  175. xp->rp += blklen;
  176. chunk -= blklen;
  177. if(xp->rp == xp->wp)
  178. xp = xp->next;
  179. }
  180. ifc->m->bwrite(ifc, nb, V6, gate);
  181. ip->stats[FragCreates]++;
  182. }
  183. ip->stats[FragOKs]++;
  184. raise:
  185. runlock(ifc);
  186. poperror();
  187. free:
  188. freeblist(bp);
  189. return rv;
  190. }
  191. void
  192. ipiput6(Fs *f, Ipifc *ifc, Block *bp)
  193. {
  194. int hl, hop, tos, notforme, tentative;
  195. uchar proto;
  196. uchar v6dst[IPaddrlen];
  197. IP *ip;
  198. Ip6hdr *h;
  199. Proto *p;
  200. Route *r, *sr;
  201. ip = f->ip;
  202. ip->stats[InReceives]++;
  203. /*
  204. * Ensure we have all the header info in the first
  205. * block. Make life easier for other protocols by
  206. * collecting up to the first 64 bytes in the first block.
  207. */
  208. if(BLEN(bp) < 64) {
  209. hl = blocklen(bp);
  210. if(hl < IP6HDR)
  211. hl = IP6HDR;
  212. if(hl > 64)
  213. hl = 64;
  214. bp = pullupblock(bp, hl);
  215. if(bp == nil)
  216. return;
  217. }
  218. h = (Ip6hdr *)bp->rp;
  219. memmove(&v6dst[0], &h->dst[0], IPaddrlen);
  220. notforme = ipforme(f, v6dst) == 0;
  221. tentative = iptentative(f, v6dst);
  222. if(tentative && h->proto != ICMPv6) {
  223. print("tentative addr, drop\n");
  224. freeblist(bp);
  225. return;
  226. }
  227. /* Check header version */
  228. if(BLKIPVER(bp) != IP_VER6) {
  229. ip->stats[InHdrErrors]++;
  230. netlog(f, Logip, "ip: bad version %ux\n", (h->vcf[0]&0xF0)>>2);
  231. freeblist(bp);
  232. return;
  233. }
  234. /* route */
  235. if(notforme) {
  236. if(!ip->iprouting){
  237. freeb(bp);
  238. return;
  239. }
  240. /* don't forward to link-local destinations */
  241. if(islinklocal(h->dst) ||
  242. (isv6mcast(h->dst) && (h->dst[1]&0xF) <= Link_local_scop)){
  243. ip->stats[OutDiscards]++;
  244. freeblist(bp);
  245. return;
  246. }
  247. /* don't forward to source's network */
  248. sr = v6lookup(f, h->src, nil);
  249. r = v6lookup(f, h->dst, nil);
  250. if(r == nil || sr == r){
  251. ip->stats[OutDiscards]++;
  252. freeblist(bp);
  253. return;
  254. }
  255. /* don't forward if packet has timed out */
  256. hop = h->ttl;
  257. if(hop < 1) {
  258. ip->stats[InHdrErrors]++;
  259. icmpttlexceeded6(f, ifc, bp);
  260. freeblist(bp);
  261. return;
  262. }
  263. /* process headers & reassemble if the interface expects it */
  264. bp = procxtns(ip, bp, r->ifc->reassemble);
  265. if(bp == nil)
  266. return;
  267. ip->stats[ForwDatagrams]++;
  268. h = (Ip6hdr *)bp->rp;
  269. tos = IPV6CLASS(h);
  270. hop = h->ttl;
  271. ipoput6(f, bp, 1, hop-1, tos, nil);
  272. return;
  273. }
  274. /* reassemble & process headers if needed */
  275. bp = procxtns(ip, bp, 1);
  276. if(bp == nil)
  277. return;
  278. h = (Ip6hdr *) (bp->rp);
  279. proto = h->proto;
  280. p = Fsrcvpcol(f, proto);
  281. if(p && p->rcv) {
  282. ip->stats[InDelivers]++;
  283. (*p->rcv)(p, ifc, bp);
  284. return;
  285. }
  286. ip->stats[InDiscards]++;
  287. ip->stats[InUnknownProtos]++;
  288. freeblist(bp);
  289. }
  290. /*
  291. * ipfragfree6 - copied from ipfragfree4 - assume hold fraglock6
  292. */
  293. void
  294. ipfragfree6(IP *ip, Fragment6 *frag)
  295. {
  296. Fragment6 *fl, **l;
  297. if(frag->blist)
  298. freeblist(frag->blist);
  299. memset(frag->src, 0, IPaddrlen);
  300. frag->id = 0;
  301. frag->blist = nil;
  302. l = &ip->flisthead6;
  303. for(fl = *l; fl; fl = fl->next) {
  304. if(fl == frag) {
  305. *l = frag->next;
  306. break;
  307. }
  308. l = &fl->next;
  309. }
  310. frag->next = ip->fragfree6;
  311. ip->fragfree6 = frag;
  312. }
  313. /*
  314. * ipfragallo6 - copied from ipfragalloc4
  315. */
  316. Fragment6*
  317. ipfragallo6(IP *ip)
  318. {
  319. Fragment6 *f;
  320. while(ip->fragfree6 == nil) {
  321. /* free last entry on fraglist */
  322. for(f = ip->flisthead6; f->next; f = f->next)
  323. ;
  324. ipfragfree6(ip, f);
  325. }
  326. f = ip->fragfree6;
  327. ip->fragfree6 = f->next;
  328. f->next = ip->flisthead6;
  329. ip->flisthead6 = f;
  330. f->age = NOW + 30000;
  331. return f;
  332. }
  333. static Block*
  334. procxtns(IP *ip, Block *bp, int doreasm)
  335. {
  336. int offset;
  337. uchar proto;
  338. Ip6hdr *h;
  339. h = (Ip6hdr *)bp->rp;
  340. offset = unfraglen(bp, &proto, 0);
  341. if(proto == FH && doreasm != 0) {
  342. bp = ip6reassemble(ip, offset, bp, h);
  343. if(bp == nil)
  344. return nil;
  345. offset = unfraglen(bp, &proto, 0);
  346. }
  347. if(proto == DOH || offset > IP6HDR)
  348. bp = procopts(bp);
  349. return bp;
  350. }
  351. /*
  352. * returns length of "Unfragmentable part", i.e., sum of lengths of ipv6 hdr,
  353. * hop-by-hop & routing headers if present; *nexthdr is set to nexthdr value
  354. * of the last header in the "Unfragmentable part"; if setfh != 0, nexthdr
  355. * field of the last header in the "Unfragmentable part" is set to FH.
  356. */
  357. int
  358. unfraglen(Block *bp, uchar *nexthdr, int setfh)
  359. {
  360. uchar *p, *q;
  361. int ufl, hs;
  362. p = bp->rp;
  363. q = p+6; /* proto, = p+sizeof(Ip6hdr.vcf)+sizeof(Ip6hdr.ploadlen) */
  364. *nexthdr = *q;
  365. ufl = IP6HDR;
  366. p += ufl;
  367. while (*nexthdr == HBH || *nexthdr == RH) {
  368. *nexthdr = *p;
  369. hs = ((int)*(p+1) + 1) * 8;
  370. ufl += hs;
  371. q = p;
  372. p += hs;
  373. }
  374. if(*nexthdr == FH)
  375. *q = *p;
  376. if(setfh)
  377. *q = FH;
  378. return ufl;
  379. }
  380. Block*
  381. procopts(Block *bp)
  382. {
  383. return bp;
  384. }
  385. Block*
  386. ip6reassemble(IP* ip, int uflen, Block* bp, Ip6hdr* ih)
  387. {
  388. int fend, offset, ovlap, len, fragsize, pktposn;
  389. uint id;
  390. uchar src[IPaddrlen], dst[IPaddrlen];
  391. Block *bl, **l, *last, *prev;
  392. Fraghdr6 *fraghdr;
  393. Fragment6 *f, *fnext;
  394. fraghdr = (Fraghdr6 *)(bp->rp + uflen);
  395. memmove(src, ih->src, IPaddrlen);
  396. memmove(dst, ih->dst, IPaddrlen);
  397. id = nhgetl(fraghdr->id);
  398. offset = nhgets(fraghdr->offsetRM) & ~7;
  399. /*
  400. * block lists are too hard, pullupblock into a single block
  401. */
  402. if(bp->next){
  403. bp = pullupblock(bp, blocklen(bp));
  404. ih = (Ip6hdr *)bp->rp;
  405. }
  406. qlock(&ip->fraglock6);
  407. /*
  408. * find a reassembly queue for this fragment
  409. */
  410. for(f = ip->flisthead6; f; f = fnext){
  411. fnext = f->next;
  412. if(ipcmp(f->src, src)==0 && ipcmp(f->dst, dst)==0 && f->id == id)
  413. break;
  414. if(f->age < NOW){
  415. ip->stats[ReasmTimeout]++;
  416. ipfragfree6(ip, f);
  417. }
  418. }
  419. /*
  420. * if this isn't a fragmented packet, accept it
  421. * and get rid of any fragments that might go
  422. * with it.
  423. */
  424. if(nhgets(fraghdr->offsetRM) == 0) { /* 1st frag is also last */
  425. if(f) {
  426. ipfragfree6(ip, f);
  427. ip->stats[ReasmFails]++;
  428. }
  429. qunlock(&ip->fraglock6);
  430. return bp;
  431. }
  432. if(bp->base+IPFRAGSZ >= bp->rp){
  433. bp = padblock(bp, IPFRAGSZ);
  434. bp->rp += IPFRAGSZ;
  435. }
  436. BKFG(bp)->foff = offset;
  437. BKFG(bp)->flen = nhgets(ih->ploadlen) + IP6HDR - uflen - IP6FHDR;
  438. /* First fragment allocates a reassembly queue */
  439. if(f == nil) {
  440. f = ipfragallo6(ip);
  441. f->id = id;
  442. memmove(f->src, src, IPaddrlen);
  443. memmove(f->dst, dst, IPaddrlen);
  444. f->blist = bp;
  445. qunlock(&ip->fraglock6);
  446. ip->stats[ReasmReqds]++;
  447. return nil;
  448. }
  449. /*
  450. * find the new fragment's position in the queue
  451. */
  452. prev = nil;
  453. l = &f->blist;
  454. bl = f->blist;
  455. while(bl != nil && BKFG(bp)->foff > BKFG(bl)->foff) {
  456. prev = bl;
  457. l = &bl->next;
  458. bl = bl->next;
  459. }
  460. /* Check overlap of a previous fragment - trim away as necessary */
  461. if(prev) {
  462. ovlap = BKFG(prev)->foff + BKFG(prev)->flen - BKFG(bp)->foff;
  463. if(ovlap > 0) {
  464. if(ovlap >= BKFG(bp)->flen) {
  465. freeblist(bp);
  466. qunlock(&ip->fraglock6);
  467. return nil;
  468. }
  469. BKFG(prev)->flen -= ovlap;
  470. }
  471. }
  472. /* Link onto assembly queue */
  473. bp->next = *l;
  474. *l = bp;
  475. /* Check to see if succeeding segments overlap */
  476. if(bp->next) {
  477. l = &bp->next;
  478. fend = BKFG(bp)->foff + BKFG(bp)->flen;
  479. /* Take completely covered segments out */
  480. while(*l) {
  481. ovlap = fend - BKFG(*l)->foff;
  482. if(ovlap <= 0)
  483. break;
  484. if(ovlap < BKFG(*l)->flen) {
  485. BKFG(*l)->flen -= ovlap;
  486. BKFG(*l)->foff += ovlap;
  487. /* move up ih hdrs */
  488. memmove((*l)->rp + ovlap, (*l)->rp, uflen);
  489. (*l)->rp += ovlap;
  490. break;
  491. }
  492. last = (*l)->next;
  493. (*l)->next = nil;
  494. freeblist(*l);
  495. *l = last;
  496. }
  497. }
  498. /*
  499. * look for a complete packet. if we get to a fragment
  500. * with the trailing bit of fraghdr->offsetRM[1] set, we're done.
  501. */
  502. pktposn = 0;
  503. for(bl = f->blist; bl && BKFG(bl)->foff == pktposn; bl = bl->next) {
  504. fraghdr = (Fraghdr6 *)(bl->rp + uflen);
  505. if((fraghdr->offsetRM[1] & 1) == 0) {
  506. bl = f->blist;
  507. /* get rid of frag header in first fragment */
  508. memmove(bl->rp + IP6FHDR, bl->rp, uflen);
  509. bl->rp += IP6FHDR;
  510. len = nhgets(((Ip6hdr*)bl->rp)->ploadlen) - IP6FHDR;
  511. bl->wp = bl->rp + len + IP6HDR;
  512. /*
  513. * Pullup all the fragment headers and
  514. * return a complete packet
  515. */
  516. for(bl = bl->next; bl; bl = bl->next) {
  517. fragsize = BKFG(bl)->flen;
  518. len += fragsize;
  519. bl->rp += uflen + IP6FHDR;
  520. bl->wp = bl->rp + fragsize;
  521. }
  522. bl = f->blist;
  523. f->blist = nil;
  524. ipfragfree6(ip, f);
  525. ih = (Ip6hdr*)bl->rp;
  526. hnputs(ih->ploadlen, len);
  527. qunlock(&ip->fraglock6);
  528. ip->stats[ReasmOKs]++;
  529. return bl;
  530. }
  531. pktposn += BKFG(bl)->flen;
  532. }
  533. qunlock(&ip->fraglock6);
  534. return nil;
  535. }