ip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. typedef struct Ip4hdr Ip4hdr;
  9. typedef struct IP IP;
  10. typedef struct Fragment4 Fragment4;
  11. typedef struct Fragment6 Fragment6;
  12. typedef struct Ipfrag Ipfrag;
  13. enum
  14. {
  15. IP4HDR = 20, /* sizeof(Ip4hdr) */
  16. IP6HDR = 40, /* sizeof(Ip6hdr) */
  17. IP_HLEN4 = 0x05, /* Header length in words */
  18. IP_DF = 0x4000, /* Don't fragment */
  19. IP_MF = 0x2000, /* More fragments */
  20. IP6FHDR = 8, /* sizeof(Fraghdr6) */
  21. IP_MAX = 64*1024, /* Maximum Internet packet size */
  22. };
  23. #define BLKIPVER(xp) (((Ip4hdr*)((xp)->rp))->vihl&0xF0)
  24. struct Ip4hdr
  25. {
  26. uchar vihl; /* Version and header length */
  27. uchar tos; /* Type of service */
  28. uchar length[2]; /* packet length */
  29. uchar id[2]; /* ip->identification */
  30. uchar frag[2]; /* Fragment information */
  31. uchar ttl; /* Time to live */
  32. uchar proto; /* Protocol */
  33. uchar cksum[2]; /* Header checksum */
  34. uchar src[4]; /* IP source */
  35. uchar dst[4]; /* IP destination */
  36. };
  37. /* MIB II counters */
  38. enum
  39. {
  40. Forwarding,
  41. DefaultTTL,
  42. InReceives,
  43. InHdrErrors,
  44. InAddrErrors,
  45. ForwDatagrams,
  46. InUnknownProtos,
  47. InDiscards,
  48. InDelivers,
  49. OutRequests,
  50. OutDiscards,
  51. OutNoRoutes,
  52. ReasmTimeout,
  53. ReasmReqds,
  54. ReasmOKs,
  55. ReasmFails,
  56. FragOKs,
  57. FragFails,
  58. FragCreates,
  59. Nstats,
  60. };
  61. struct Fragment4
  62. {
  63. Block* blist;
  64. Fragment4* next;
  65. ulong src;
  66. ulong dst;
  67. ushort id;
  68. ulong age;
  69. };
  70. struct Fragment6
  71. {
  72. Block* blist;
  73. Fragment6* next;
  74. uchar src[IPaddrlen];
  75. uchar dst[IPaddrlen];
  76. uint id;
  77. ulong age;
  78. };
  79. struct Ipfrag
  80. {
  81. ushort foff;
  82. ushort flen;
  83. };
  84. /* an instance of IP */
  85. struct IP
  86. {
  87. ulong stats[Nstats];
  88. QLock fraglock4;
  89. Fragment4* flisthead4;
  90. Fragment4* fragfree4;
  91. Ref id4;
  92. QLock fraglock6;
  93. Fragment6* flisthead6;
  94. Fragment6* fragfree6;
  95. Ref id6;
  96. int iprouting; /* true if we route like a gateway */
  97. };
  98. static char *statnames[] =
  99. {
  100. [Forwarding] "Forwarding",
  101. [DefaultTTL] "DefaultTTL",
  102. [InReceives] "InReceives",
  103. [InHdrErrors] "InHdrErrors",
  104. [InAddrErrors] "InAddrErrors",
  105. [ForwDatagrams] "ForwDatagrams",
  106. [InUnknownProtos] "InUnknownProtos",
  107. [InDiscards] "InDiscards",
  108. [InDelivers] "InDelivers",
  109. [OutRequests] "OutRequests",
  110. [OutDiscards] "OutDiscards",
  111. [OutNoRoutes] "OutNoRoutes",
  112. [ReasmTimeout] "ReasmTimeout",
  113. [ReasmReqds] "ReasmReqds",
  114. [ReasmOKs] "ReasmOKs",
  115. [ReasmFails] "ReasmFails",
  116. [FragOKs] "FragOKs",
  117. [FragFails] "FragFails",
  118. [FragCreates] "FragCreates",
  119. };
  120. #define BLKIP(xp) ((Ip4hdr*)((xp)->rp))
  121. /*
  122. * This sleazy macro relies on the media header size being
  123. * larger than sizeof(Ipfrag). ipreassemble checks this is true
  124. */
  125. #define BKFG(xp) ((Ipfrag*)((xp)->base))
  126. ushort ipcsum(uchar*);
  127. Block* ip4reassemble(IP*, int, Block*, Ip4hdr*);
  128. void ipfragfree4(IP*, Fragment4*);
  129. Fragment4* ipfragallo4(IP*);
  130. void
  131. ip_init_6(Fs *f)
  132. {
  133. v6params *v6p;
  134. v6p = smalloc(sizeof(v6params));
  135. v6p->rp.mflag = 0; // default not managed
  136. v6p->rp.oflag = 0;
  137. v6p->rp.maxraint = 600000; // millisecs
  138. v6p->rp.minraint = 200000;
  139. v6p->rp.linkmtu = 0; // no mtu sent
  140. v6p->rp.reachtime = 0;
  141. v6p->rp.rxmitra = 0;
  142. v6p->rp.ttl = MAXTTL;
  143. v6p->rp.routerlt = 3*(v6p->rp.maxraint);
  144. v6p->hp.rxmithost = 1000; // v6 RETRANS_TIMER
  145. v6p->cdrouter = -1;
  146. f->v6p = v6p;
  147. }
  148. void
  149. initfrag(IP *ip, int size)
  150. {
  151. Fragment4 *fq4, *eq4;
  152. Fragment6 *fq6, *eq6;
  153. ip->fragfree4 = (Fragment4*)malloc(sizeof(Fragment4) * size);
  154. if(ip->fragfree4 == nil)
  155. panic("initfrag");
  156. eq4 = &ip->fragfree4[size];
  157. for(fq4 = ip->fragfree4; fq4 < eq4; fq4++)
  158. fq4->next = fq4+1;
  159. ip->fragfree4[size-1].next = nil;
  160. ip->fragfree6 = (Fragment6*)malloc(sizeof(Fragment6) * size);
  161. if(ip->fragfree6 == nil)
  162. panic("initfrag");
  163. eq6 = &ip->fragfree6[size];
  164. for(fq6 = ip->fragfree6; fq6 < eq6; fq6++)
  165. fq6->next = fq6+1;
  166. ip->fragfree6[size-1].next = nil;
  167. }
  168. void
  169. ip_init(Fs *f)
  170. {
  171. IP *ip;
  172. ip = smalloc(sizeof(IP));
  173. initfrag(ip, 100);
  174. f->ip = ip;
  175. ip_init_6(f);
  176. }
  177. void
  178. iprouting(Fs *f, int on)
  179. {
  180. f->ip->iprouting = on;
  181. if(f->ip->iprouting==0)
  182. f->ip->stats[Forwarding] = 2;
  183. else
  184. f->ip->stats[Forwarding] = 1;
  185. }
  186. int
  187. ipoput4(Fs *f, Block *bp, int gating, int ttl, int tos, Conv *c)
  188. {
  189. Ipifc *ifc;
  190. uchar *gate;
  191. ulong fragoff;
  192. Block *xp, *nb;
  193. Ip4hdr *eh, *feh;
  194. int lid, len, seglen, chunk, dlen, blklen, offset, medialen;
  195. Route *r, *sr;
  196. IP *ip;
  197. int rv = 0;
  198. ip = f->ip;
  199. /* Fill out the ip header */
  200. eh = (Ip4hdr*)(bp->rp);
  201. ip->stats[OutRequests]++;
  202. /* Number of uchars in data and ip header to write */
  203. len = blocklen(bp);
  204. if(gating){
  205. chunk = nhgets(eh->length);
  206. if(chunk > len){
  207. ip->stats[OutDiscards]++;
  208. netlog(f, Logip, "short gated packet\n");
  209. goto free;
  210. }
  211. if(chunk < len)
  212. len = chunk;
  213. }
  214. if(len >= IP_MAX){
  215. ip->stats[OutDiscards]++;
  216. netlog(f, Logip, "exceeded ip max size %V\n", eh->dst);
  217. goto free;
  218. }
  219. r = v4lookup(f, eh->dst, c);
  220. if(r == nil){
  221. ip->stats[OutNoRoutes]++;
  222. netlog(f, Logip, "no interface %V\n", eh->dst);
  223. rv = -1;
  224. goto free;
  225. }
  226. ifc = r->ifc;
  227. if(r->type & (Rifc|Runi))
  228. gate = eh->dst;
  229. else
  230. if(r->type & (Rbcast|Rmulti)) {
  231. gate = eh->dst;
  232. sr = v4lookup(f, eh->src, nil);
  233. if(sr != nil && (sr->type & Runi))
  234. ifc = sr->ifc;
  235. }
  236. else
  237. gate = r->v4.gate;
  238. if(!gating)
  239. eh->vihl = IP_VER4|IP_HLEN4;
  240. eh->ttl = ttl;
  241. if(!gating)
  242. eh->tos = tos;
  243. if(!canrlock(ifc))
  244. goto free;
  245. if(waserror()){
  246. runlock(ifc);
  247. nexterror();
  248. }
  249. if(ifc->m == nil)
  250. goto raise;
  251. /* If we dont need to fragment just send it */
  252. medialen = ifc->maxtu - ifc->m->hsize;
  253. if(len <= medialen) {
  254. if(!gating)
  255. hnputs(eh->id, incref(&ip->id4));
  256. hnputs(eh->length, len);
  257. if(!gating){
  258. eh->frag[0] = 0;
  259. eh->frag[1] = 0;
  260. }
  261. eh->cksum[0] = 0;
  262. eh->cksum[1] = 0;
  263. hnputs(eh->cksum, ipcsum(&eh->vihl));
  264. ifc->m->bwrite(ifc, bp, V4, gate);
  265. runlock(ifc);
  266. poperror();
  267. return 0;
  268. }
  269. if((eh->frag[0] & (IP_DF>>8)) && !gating) print("%V: DF set\n", eh->dst);
  270. if(eh->frag[0] & (IP_DF>>8)){
  271. ip->stats[FragFails]++;
  272. ip->stats[OutDiscards]++;
  273. icmpcantfrag(f, bp, medialen);
  274. netlog(f, Logip, "%V: eh->frag[0] & (IP_DF>>8)\n", eh->dst);
  275. goto raise;
  276. }
  277. seglen = (medialen - IP4HDR) & ~7;
  278. if(seglen < 8){
  279. ip->stats[FragFails]++;
  280. ip->stats[OutDiscards]++;
  281. netlog(f, Logip, "%V seglen < 8\n", eh->dst);
  282. goto raise;
  283. }
  284. dlen = len - IP4HDR;
  285. xp = bp;
  286. if(gating)
  287. lid = nhgets(eh->id);
  288. else
  289. lid = incref(&ip->id4);
  290. offset = IP4HDR;
  291. while(xp != nil && offset && offset >= BLEN(xp)) {
  292. offset -= BLEN(xp);
  293. xp = xp->next;
  294. }
  295. xp->rp += offset;
  296. if(gating)
  297. fragoff = nhgets(eh->frag)<<3;
  298. else
  299. fragoff = 0;
  300. dlen += fragoff;
  301. for(; fragoff < dlen; fragoff += seglen) {
  302. nb = allocb(IP4HDR+seglen);
  303. feh = (Ip4hdr*)(nb->rp);
  304. memmove(nb->wp, eh, IP4HDR);
  305. nb->wp += IP4HDR;
  306. if((fragoff + seglen) >= dlen) {
  307. seglen = dlen - fragoff;
  308. hnputs(feh->frag, fragoff>>3);
  309. }
  310. else
  311. hnputs(feh->frag, (fragoff>>3)|IP_MF);
  312. hnputs(feh->length, seglen + IP4HDR);
  313. hnputs(feh->id, lid);
  314. /* Copy up the data area */
  315. chunk = seglen;
  316. while(chunk) {
  317. if(!xp) {
  318. ip->stats[OutDiscards]++;
  319. ip->stats[FragFails]++;
  320. freeblist(nb);
  321. netlog(f, Logip, "!xp: chunk %d\n", chunk);
  322. goto raise;
  323. }
  324. blklen = chunk;
  325. if(BLEN(xp) < chunk)
  326. blklen = BLEN(xp);
  327. memmove(nb->wp, xp->rp, blklen);
  328. nb->wp += blklen;
  329. xp->rp += blklen;
  330. chunk -= blklen;
  331. if(xp->rp == xp->wp)
  332. xp = xp->next;
  333. }
  334. feh->cksum[0] = 0;
  335. feh->cksum[1] = 0;
  336. hnputs(feh->cksum, ipcsum(&feh->vihl));
  337. ifc->m->bwrite(ifc, nb, V4, gate);
  338. ip->stats[FragCreates]++;
  339. }
  340. ip->stats[FragOKs]++;
  341. raise:
  342. runlock(ifc);
  343. poperror();
  344. free:
  345. freeblist(bp);
  346. return rv;
  347. }
  348. void
  349. ipiput4(Fs *f, Ipifc *ifc, Block *bp)
  350. {
  351. int hl;
  352. int hop, tos, proto, olen;
  353. Ip4hdr *h;
  354. Proto *p;
  355. ushort frag;
  356. int notforme;
  357. uchar *dp, v6dst[IPaddrlen];
  358. IP *ip;
  359. Route *r;
  360. if(BLKIPVER(bp) != IP_VER4) {
  361. ipiput6(f, ifc, bp);
  362. return;
  363. }
  364. ip = f->ip;
  365. ip->stats[InReceives]++;
  366. /*
  367. * Ensure we have all the header info in the first
  368. * block. Make life easier for other protocols by
  369. * collecting up to the first 64 bytes in the first block.
  370. */
  371. if(BLEN(bp) < 64) {
  372. hl = blocklen(bp);
  373. if(hl < IP4HDR)
  374. hl = IP4HDR;
  375. if(hl > 64)
  376. hl = 64;
  377. bp = pullupblock(bp, hl);
  378. if(bp == nil)
  379. return;
  380. }
  381. h = (Ip4hdr*)(bp->rp);
  382. /* dump anything that whose header doesn't checksum */
  383. if((bp->flag & Bipck) == 0 && ipcsum(&h->vihl)) {
  384. ip->stats[InHdrErrors]++;
  385. netlog(f, Logip, "ip: checksum error %V\n", h->src);
  386. freeblist(bp);
  387. return;
  388. }
  389. v4tov6(v6dst, h->dst);
  390. notforme = ipforme(f, v6dst) == 0;
  391. /* Check header length and version */
  392. if((h->vihl&0x0F) != IP_HLEN4) {
  393. hl = (h->vihl&0xF)<<2;
  394. if(hl < (IP_HLEN4<<2)) {
  395. ip->stats[InHdrErrors]++;
  396. netlog(f, Logip, "ip: %V bad hivl %ux\n", h->src, h->vihl);
  397. freeblist(bp);
  398. return;
  399. }
  400. /* If this is not routed strip off the options */
  401. if(notforme == 0) {
  402. olen = nhgets(h->length);
  403. dp = bp->rp + (hl - (IP_HLEN4<<2));
  404. memmove(dp, h, IP_HLEN4<<2);
  405. bp->rp = dp;
  406. h = (Ip4hdr*)(bp->rp);
  407. h->vihl = (IP_VER4|IP_HLEN4);
  408. hnputs(h->length, olen-hl+(IP_HLEN4<<2));
  409. }
  410. }
  411. /* route */
  412. if(notforme) {
  413. Conv conv;
  414. if(!ip->iprouting){
  415. freeb(bp);
  416. return;
  417. }
  418. /* don't forward to source's network */
  419. conv.r = nil;
  420. r = v4lookup(f, h->dst, &conv);
  421. if(r == nil || r->ifc == ifc){
  422. ip->stats[OutDiscards]++;
  423. freeblist(bp);
  424. return;
  425. }
  426. /* don't forward if packet has timed out */
  427. hop = h->ttl;
  428. if(hop < 1) {
  429. ip->stats[InHdrErrors]++;
  430. icmpttlexceeded(f, ifc->lifc->local, bp);
  431. freeblist(bp);
  432. return;
  433. }
  434. /* reassemble if the interface expects it */
  435. if(r->ifc == nil) panic("nil route rfc");
  436. if(r->ifc->reassemble){
  437. frag = nhgets(h->frag);
  438. if(frag) {
  439. h->tos = 0;
  440. if(frag & IP_MF)
  441. h->tos = 1;
  442. bp = ip4reassemble(ip, frag, bp, h);
  443. if(bp == nil)
  444. return;
  445. h = (Ip4hdr*)(bp->rp);
  446. }
  447. }
  448. ip->stats[ForwDatagrams]++;
  449. tos = h->tos;
  450. hop = h->ttl;
  451. ipoput4(f, bp, 1, hop - 1, tos, &conv);
  452. return;
  453. }
  454. frag = nhgets(h->frag);
  455. if(frag) {
  456. h->tos = 0;
  457. if(frag & IP_MF)
  458. h->tos = 1;
  459. bp = ip4reassemble(ip, frag, bp, h);
  460. if(bp == nil)
  461. return;
  462. h = (Ip4hdr*)(bp->rp);
  463. }
  464. /* don't let any frag info go up the stack */
  465. h->frag[0] = 0;
  466. h->frag[1] = 0;
  467. proto = h->proto;
  468. p = Fsrcvpcol(f, proto);
  469. if(p != nil && p->rcv != nil) {
  470. ip->stats[InDelivers]++;
  471. (*p->rcv)(p, ifc, bp);
  472. return;
  473. }
  474. ip->stats[InDiscards]++;
  475. ip->stats[InUnknownProtos]++;
  476. freeblist(bp);
  477. }
  478. int
  479. ipstats(Fs *f, char *buf, int len)
  480. {
  481. IP *ip;
  482. char *p, *e;
  483. int i;
  484. ip = f->ip;
  485. ip->stats[DefaultTTL] = MAXTTL;
  486. p = buf;
  487. e = p+len;
  488. for(i = 0; i < Nstats; i++)
  489. p = seprint(p, e, "%s: %lud\n", statnames[i], ip->stats[i]);
  490. return p - buf;
  491. }
  492. Block*
  493. ip4reassemble(IP *ip, int offset, Block *bp, Ip4hdr *ih)
  494. {
  495. int fend;
  496. ushort id;
  497. Fragment4 *f, *fnext;
  498. ulong src, dst;
  499. Block *bl, **l, *last, *prev;
  500. int ovlap, len, fragsize, pktposn;
  501. src = nhgetl(ih->src);
  502. dst = nhgetl(ih->dst);
  503. id = nhgets(ih->id);
  504. /*
  505. * block lists are too hard, pullupblock into a single block
  506. */
  507. if(bp->next){
  508. bp = pullupblock(bp, blocklen(bp));
  509. ih = (Ip4hdr*)(bp->rp);
  510. }
  511. qlock(&ip->fraglock4);
  512. /*
  513. * find a reassembly queue for this fragment
  514. */
  515. for(f = ip->flisthead4; f; f = fnext){
  516. fnext = f->next; /* because ipfragfree4 changes the list */
  517. if(f->src == src && f->dst == dst && f->id == id)
  518. break;
  519. if(f->age < NOW){
  520. ip->stats[ReasmTimeout]++;
  521. ipfragfree4(ip, f);
  522. }
  523. }
  524. /*
  525. * if this isn't a fragmented packet, accept it
  526. * and get rid of any fragments that might go
  527. * with it.
  528. */
  529. if(!ih->tos && (offset & ~(IP_MF|IP_DF)) == 0) {
  530. if(f != nil) {
  531. ipfragfree4(ip, f);
  532. ip->stats[ReasmFails]++;
  533. }
  534. qunlock(&ip->fraglock4);
  535. return bp;
  536. }
  537. if(bp->base+sizeof(Ipfrag) >= bp->rp){
  538. bp = padblock(bp, sizeof(Ipfrag));
  539. bp->rp += sizeof(Ipfrag);
  540. }
  541. BKFG(bp)->foff = offset<<3;
  542. BKFG(bp)->flen = nhgets(ih->length)-IP4HDR;
  543. /* First fragment allocates a reassembly queue */
  544. if(f == nil) {
  545. f = ipfragallo4(ip);
  546. f->id = id;
  547. f->src = src;
  548. f->dst = dst;
  549. f->blist = bp;
  550. qunlock(&ip->fraglock4);
  551. ip->stats[ReasmReqds]++;
  552. return nil;
  553. }
  554. /*
  555. * find the new fragment's position in the queue
  556. */
  557. prev = nil;
  558. l = &f->blist;
  559. bl = f->blist;
  560. while(bl != nil && BKFG(bp)->foff > BKFG(bl)->foff) {
  561. prev = bl;
  562. l = &bl->next;
  563. bl = bl->next;
  564. }
  565. /* Check overlap of a previous fragment - trim away as necessary */
  566. if(prev) {
  567. ovlap = BKFG(prev)->foff + BKFG(prev)->flen - BKFG(bp)->foff;
  568. if(ovlap > 0) {
  569. if(ovlap >= BKFG(bp)->flen) {
  570. freeblist(bp);
  571. qunlock(&ip->fraglock4);
  572. return nil;
  573. }
  574. BKFG(prev)->flen -= ovlap;
  575. }
  576. }
  577. /* Link onto assembly queue */
  578. bp->next = *l;
  579. *l = bp;
  580. /* Check to see if succeeding segments overlap */
  581. if(bp->next) {
  582. l = &bp->next;
  583. fend = BKFG(bp)->foff + BKFG(bp)->flen;
  584. /* Take completely covered segments out */
  585. while(*l) {
  586. ovlap = fend - BKFG(*l)->foff;
  587. if(ovlap <= 0)
  588. break;
  589. if(ovlap < BKFG(*l)->flen) {
  590. BKFG(*l)->flen -= ovlap;
  591. BKFG(*l)->foff += ovlap;
  592. /* move up ih hdrs */
  593. memmove((*l)->rp + ovlap, (*l)->rp, IP4HDR);
  594. (*l)->rp += ovlap;
  595. break;
  596. }
  597. last = (*l)->next;
  598. (*l)->next = nil;
  599. freeblist(*l);
  600. *l = last;
  601. }
  602. }
  603. /*
  604. * look for a complete packet. if we get to a fragment
  605. * without IP_MF set, we're done.
  606. */
  607. pktposn = 0;
  608. for(bl = f->blist; bl; bl = bl->next) {
  609. if(BKFG(bl)->foff != pktposn)
  610. break;
  611. if((BLKIP(bl)->frag[0]&(IP_MF>>8)) == 0) {
  612. bl = f->blist;
  613. len = nhgets(BLKIP(bl)->length);
  614. bl->wp = bl->rp + len;
  615. /* Pullup all the fragment headers and
  616. * return a complete packet
  617. */
  618. for(bl = bl->next; bl; bl = bl->next) {
  619. fragsize = BKFG(bl)->flen;
  620. len += fragsize;
  621. bl->rp += IP4HDR;
  622. bl->wp = bl->rp + fragsize;
  623. }
  624. bl = f->blist;
  625. f->blist = nil;
  626. ipfragfree4(ip, f);
  627. ih = BLKIP(bl);
  628. hnputs(ih->length, len);
  629. qunlock(&ip->fraglock4);
  630. ip->stats[ReasmOKs]++;
  631. return bl;
  632. }
  633. pktposn += BKFG(bl)->flen;
  634. }
  635. qunlock(&ip->fraglock4);
  636. return nil;
  637. }
  638. /*
  639. * ipfragfree4 - Free a list of fragments - assume hold fraglock4
  640. */
  641. void
  642. ipfragfree4(IP *ip, Fragment4 *frag)
  643. {
  644. Fragment4 *fl, **l;
  645. if(frag->blist)
  646. freeblist(frag->blist);
  647. frag->src = 0;
  648. frag->id = 0;
  649. frag->blist = nil;
  650. l = &ip->flisthead4;
  651. for(fl = *l; fl; fl = fl->next) {
  652. if(fl == frag) {
  653. *l = frag->next;
  654. break;
  655. }
  656. l = &fl->next;
  657. }
  658. frag->next = ip->fragfree4;
  659. ip->fragfree4 = frag;
  660. }
  661. /*
  662. * ipfragallo4 - allocate a reassembly queue - assume hold fraglock4
  663. */
  664. Fragment4 *
  665. ipfragallo4(IP *ip)
  666. {
  667. Fragment4 *f;
  668. while(ip->fragfree4 == nil) {
  669. /* free last entry on fraglist */
  670. for(f = ip->flisthead4; f->next; f = f->next)
  671. ;
  672. ipfragfree4(ip, f);
  673. }
  674. f = ip->fragfree4;
  675. ip->fragfree4 = f->next;
  676. f->next = ip->flisthead4;
  677. ip->flisthead4 = f;
  678. f->age = NOW + 30000;
  679. return f;
  680. }
  681. ushort
  682. ipcsum(uchar *addr)
  683. {
  684. int len;
  685. ulong sum;
  686. sum = 0;
  687. len = (addr[0]&0xf)<<2;
  688. while(len > 0) {
  689. sum += addr[0]<<8 | addr[1] ;
  690. len -= 2;
  691. addr += 2;
  692. }
  693. sum = (sum & 0xffff) + (sum >> 16);
  694. sum = (sum & 0xffff) + (sum >> 16);
  695. return (sum^0xffff);
  696. }