ip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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->reassemble){
  436. frag = nhgets(h->frag);
  437. if(frag) {
  438. h->tos = 0;
  439. if(frag & IP_MF)
  440. h->tos = 1;
  441. bp = ip4reassemble(ip, frag, bp, h);
  442. if(bp == nil)
  443. return;
  444. h = (Ip4hdr*)(bp->rp);
  445. }
  446. }
  447. ip->stats[ForwDatagrams]++;
  448. tos = h->tos;
  449. hop = h->ttl;
  450. ipoput4(f, bp, 1, hop - 1, tos, &conv);
  451. return;
  452. }
  453. frag = nhgets(h->frag);
  454. if(frag) {
  455. h->tos = 0;
  456. if(frag & IP_MF)
  457. h->tos = 1;
  458. bp = ip4reassemble(ip, frag, bp, h);
  459. if(bp == nil)
  460. return;
  461. h = (Ip4hdr*)(bp->rp);
  462. }
  463. /* don't let any frag info go up the stack */
  464. h->frag[0] = 0;
  465. h->frag[1] = 0;
  466. proto = h->proto;
  467. p = Fsrcvpcol(f, proto);
  468. if(p != nil && p->rcv != nil) {
  469. ip->stats[InDelivers]++;
  470. (*p->rcv)(p, ifc, bp);
  471. return;
  472. }
  473. ip->stats[InDiscards]++;
  474. ip->stats[InUnknownProtos]++;
  475. freeblist(bp);
  476. }
  477. int
  478. ipstats(Fs *f, char *buf, int len)
  479. {
  480. IP *ip;
  481. char *p, *e;
  482. int i;
  483. ip = f->ip;
  484. ip->stats[DefaultTTL] = MAXTTL;
  485. p = buf;
  486. e = p+len;
  487. for(i = 0; i < Nstats; i++)
  488. p = seprint(p, e, "%s: %lud\n", statnames[i], ip->stats[i]);
  489. return p - buf;
  490. }
  491. Block*
  492. ip4reassemble(IP *ip, int offset, Block *bp, Ip4hdr *ih)
  493. {
  494. int fend;
  495. ushort id;
  496. Fragment4 *f, *fnext;
  497. ulong src, dst;
  498. Block *bl, **l, *last, *prev;
  499. int ovlap, len, fragsize, pktposn;
  500. src = nhgetl(ih->src);
  501. dst = nhgetl(ih->dst);
  502. id = nhgets(ih->id);
  503. /*
  504. * block lists are too hard, pullupblock into a single block
  505. */
  506. if(bp->next){
  507. bp = pullupblock(bp, blocklen(bp));
  508. ih = (Ip4hdr*)(bp->rp);
  509. }
  510. qlock(&ip->fraglock4);
  511. /*
  512. * find a reassembly queue for this fragment
  513. */
  514. for(f = ip->flisthead4; f; f = fnext){
  515. fnext = f->next; /* because ipfragfree4 changes the list */
  516. if(f->src == src && f->dst == dst && f->id == id)
  517. break;
  518. if(f->age < NOW){
  519. ip->stats[ReasmTimeout]++;
  520. ipfragfree4(ip, f);
  521. }
  522. }
  523. /*
  524. * if this isn't a fragmented packet, accept it
  525. * and get rid of any fragments that might go
  526. * with it.
  527. */
  528. if(!ih->tos && (offset & ~(IP_MF|IP_DF)) == 0) {
  529. if(f != nil) {
  530. ipfragfree4(ip, f);
  531. ip->stats[ReasmFails]++;
  532. }
  533. qunlock(&ip->fraglock4);
  534. return bp;
  535. }
  536. if(bp->base+sizeof(Ipfrag) >= bp->rp){
  537. bp = padblock(bp, sizeof(Ipfrag));
  538. bp->rp += sizeof(Ipfrag);
  539. }
  540. BKFG(bp)->foff = offset<<3;
  541. BKFG(bp)->flen = nhgets(ih->length)-IP4HDR;
  542. /* First fragment allocates a reassembly queue */
  543. if(f == nil) {
  544. f = ipfragallo4(ip);
  545. f->id = id;
  546. f->src = src;
  547. f->dst = dst;
  548. f->blist = bp;
  549. qunlock(&ip->fraglock4);
  550. ip->stats[ReasmReqds]++;
  551. return nil;
  552. }
  553. /*
  554. * find the new fragment's position in the queue
  555. */
  556. prev = nil;
  557. l = &f->blist;
  558. bl = f->blist;
  559. while(bl != nil && BKFG(bp)->foff > BKFG(bl)->foff) {
  560. prev = bl;
  561. l = &bl->next;
  562. bl = bl->next;
  563. }
  564. /* Check overlap of a previous fragment - trim away as necessary */
  565. if(prev) {
  566. ovlap = BKFG(prev)->foff + BKFG(prev)->flen - BKFG(bp)->foff;
  567. if(ovlap > 0) {
  568. if(ovlap >= BKFG(bp)->flen) {
  569. freeblist(bp);
  570. qunlock(&ip->fraglock4);
  571. return nil;
  572. }
  573. BKFG(prev)->flen -= ovlap;
  574. }
  575. }
  576. /* Link onto assembly queue */
  577. bp->next = *l;
  578. *l = bp;
  579. /* Check to see if succeeding segments overlap */
  580. if(bp->next) {
  581. l = &bp->next;
  582. fend = BKFG(bp)->foff + BKFG(bp)->flen;
  583. /* Take completely covered segments out */
  584. while(*l) {
  585. ovlap = fend - BKFG(*l)->foff;
  586. if(ovlap <= 0)
  587. break;
  588. if(ovlap < BKFG(*l)->flen) {
  589. BKFG(*l)->flen -= ovlap;
  590. BKFG(*l)->foff += ovlap;
  591. /* move up ih hdrs */
  592. memmove((*l)->rp + ovlap, (*l)->rp, IP4HDR);
  593. (*l)->rp += ovlap;
  594. break;
  595. }
  596. last = (*l)->next;
  597. (*l)->next = nil;
  598. freeblist(*l);
  599. *l = last;
  600. }
  601. }
  602. /*
  603. * look for a complete packet. if we get to a fragment
  604. * without IP_MF set, we're done.
  605. */
  606. pktposn = 0;
  607. for(bl = f->blist; bl; bl = bl->next) {
  608. if(BKFG(bl)->foff != pktposn)
  609. break;
  610. if((BLKIP(bl)->frag[0]&(IP_MF>>8)) == 0) {
  611. bl = f->blist;
  612. len = nhgets(BLKIP(bl)->length);
  613. bl->wp = bl->rp + len;
  614. /* Pullup all the fragment headers and
  615. * return a complete packet
  616. */
  617. for(bl = bl->next; bl; bl = bl->next) {
  618. fragsize = BKFG(bl)->flen;
  619. len += fragsize;
  620. bl->rp += IP4HDR;
  621. bl->wp = bl->rp + fragsize;
  622. }
  623. bl = f->blist;
  624. f->blist = nil;
  625. ipfragfree4(ip, f);
  626. ih = BLKIP(bl);
  627. hnputs(ih->length, len);
  628. qunlock(&ip->fraglock4);
  629. ip->stats[ReasmOKs]++;
  630. return bl;
  631. }
  632. pktposn += BKFG(bl)->flen;
  633. }
  634. qunlock(&ip->fraglock4);
  635. return nil;
  636. }
  637. /*
  638. * ipfragfree4 - Free a list of fragments - assume hold fraglock4
  639. */
  640. void
  641. ipfragfree4(IP *ip, Fragment4 *frag)
  642. {
  643. Fragment4 *fl, **l;
  644. if(frag->blist)
  645. freeblist(frag->blist);
  646. frag->src = 0;
  647. frag->id = 0;
  648. frag->blist = nil;
  649. l = &ip->flisthead4;
  650. for(fl = *l; fl; fl = fl->next) {
  651. if(fl == frag) {
  652. *l = frag->next;
  653. break;
  654. }
  655. l = &fl->next;
  656. }
  657. frag->next = ip->fragfree4;
  658. ip->fragfree4 = frag;
  659. }
  660. /*
  661. * ipfragallo4 - allocate a reassembly queue - assume hold fraglock4
  662. */
  663. Fragment4 *
  664. ipfragallo4(IP *ip)
  665. {
  666. Fragment4 *f;
  667. while(ip->fragfree4 == nil) {
  668. /* free last entry on fraglist */
  669. for(f = ip->flisthead4; f->next; f = f->next)
  670. ;
  671. ipfragfree4(ip, f);
  672. }
  673. f = ip->fragfree4;
  674. ip->fragfree4 = f->next;
  675. f->next = ip->flisthead4;
  676. ip->flisthead4 = f;
  677. f->age = NOW + 30000;
  678. return f;
  679. }
  680. ushort
  681. ipcsum(uchar *addr)
  682. {
  683. int len;
  684. ulong sum;
  685. sum = 0;
  686. len = (addr[0]&0xf)<<2;
  687. while(len > 0) {
  688. sum += addr[0]<<8 | addr[1] ;
  689. len -= 2;
  690. addr += 2;
  691. }
  692. sum = (sum & 0xffff) + (sum >> 16);
  693. sum = (sum & 0xffff) + (sum >> 16);
  694. return (sum^0xffff);
  695. }