ip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. void
  187. ipoput4(Fs *f, Block *bp, int gating, int ttl, int tos)
  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. Proto *pr;
  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);
  220. if(r == nil){
  221. ip->stats[OutNoRoutes]++;
  222. netlog(f, Logip, "no interface %V\n", eh->dst);
  223. if(!gating){
  224. freeblist(bp);
  225. print("ipoput4: no route\n");
  226. error("no route");
  227. }
  228. goto free;
  229. }
  230. ifc = r->ifc;
  231. if(r->type & (Rifc|Runi))
  232. gate = eh->dst;
  233. else
  234. if(r->type & (Rbcast|Rmulti)) {
  235. gate = eh->dst;
  236. sr = v4lookup(f, eh->src);
  237. if(sr != nil && (sr->type & Runi))
  238. ifc = sr->ifc;
  239. }
  240. else
  241. gate = r->v4.gate;
  242. if(!gating)
  243. eh->vihl = IP_VER4|IP_HLEN4;
  244. eh->ttl = ttl;
  245. if(!gating)
  246. eh->tos = tos;
  247. if(!canrlock(ifc))
  248. goto free;
  249. if(waserror()){
  250. runlock(ifc);
  251. nexterror();
  252. }
  253. if(ifc->m == nil)
  254. goto raise;
  255. /* If we dont need to fragment just send it */
  256. medialen = ifc->maxtu - ifc->m->hsize;
  257. if(len <= medialen) {
  258. if(!gating)
  259. hnputs(eh->id, incref(&ip->id4));
  260. hnputs(eh->length, len);
  261. if(!gating){
  262. eh->frag[0] = 0;
  263. eh->frag[1] = 0;
  264. }
  265. eh->cksum[0] = 0;
  266. eh->cksum[1] = 0;
  267. hnputs(eh->cksum, ipcsum(&eh->vihl));
  268. ifc->m->bwrite(ifc, bp, V4, gate);
  269. runlock(ifc);
  270. poperror();
  271. return;
  272. }
  273. if((eh->frag[0] & (IP_DF>>8)) && !gating) print("%V: DF set\n", eh->dst);
  274. if(eh->frag[0] & (IP_DF>>8)){
  275. ip->stats[FragFails]++;
  276. ip->stats[OutDiscards]++;
  277. icmpcantfrag(f, bp, medialen);
  278. netlog(f, Logip, "%V: eh->frag[0] & (IP_DF>>8)\n", eh->dst);
  279. goto raise;
  280. }
  281. seglen = (medialen - IP4HDR) & ~7;
  282. if(seglen < 8){
  283. ip->stats[FragFails]++;
  284. ip->stats[OutDiscards]++;
  285. netlog(f, Logip, "%V seglen < 8\n", eh->dst);
  286. goto raise;
  287. }
  288. dlen = len - IP4HDR;
  289. xp = bp;
  290. if(gating)
  291. lid = nhgets(eh->id);
  292. else
  293. lid = incref(&ip->id4);
  294. offset = IP4HDR;
  295. while(xp != nil && offset && offset >= BLEN(xp)) {
  296. offset -= BLEN(xp);
  297. xp = xp->next;
  298. }
  299. xp->rp += offset;
  300. if(gating)
  301. fragoff = nhgets(eh->frag)<<3;
  302. else
  303. fragoff = 0;
  304. dlen += fragoff;
  305. for(; fragoff < dlen; fragoff += seglen) {
  306. nb = allocb(IP4HDR+seglen);
  307. feh = (Ip4hdr*)(nb->rp);
  308. memmove(nb->wp, eh, IP4HDR);
  309. nb->wp += IP4HDR;
  310. if((fragoff + seglen) >= dlen) {
  311. seglen = dlen - fragoff;
  312. hnputs(feh->frag, fragoff>>3);
  313. }
  314. else
  315. hnputs(feh->frag, (fragoff>>3)|IP_MF);
  316. hnputs(feh->length, seglen + IP4HDR);
  317. hnputs(feh->id, lid);
  318. /* Copy up the data area */
  319. chunk = seglen;
  320. while(chunk) {
  321. if(!xp) {
  322. ip->stats[OutDiscards]++;
  323. ip->stats[FragFails]++;
  324. freeblist(nb);
  325. netlog(f, Logip, "!xp: chunk %d\n", chunk);
  326. goto raise;
  327. }
  328. blklen = chunk;
  329. if(BLEN(xp) < chunk)
  330. blklen = BLEN(xp);
  331. memmove(nb->wp, xp->rp, blklen);
  332. nb->wp += blklen;
  333. xp->rp += blklen;
  334. chunk -= blklen;
  335. if(xp->rp == xp->wp)
  336. xp = xp->next;
  337. }
  338. feh->cksum[0] = 0;
  339. feh->cksum[1] = 0;
  340. hnputs(feh->cksum, ipcsum(&feh->vihl));
  341. ifc->m->bwrite(ifc, nb, V4, gate);
  342. ip->stats[FragCreates]++;
  343. }
  344. ip->stats[FragOKs]++;
  345. raise:
  346. runlock(ifc);
  347. poperror();
  348. free:
  349. freeblist(bp);
  350. }
  351. void
  352. ipiput4(Fs *f, Ipifc *ifc, Block *bp)
  353. {
  354. int hl;
  355. int hop, tos, proto, olen;
  356. Ip4hdr *h;
  357. Proto *p;
  358. ushort frag;
  359. int notforme;
  360. uchar *dp, v6dst[IPaddrlen];
  361. IP *ip;
  362. Route *r, *sr;
  363. if(BLKIPVER(bp) != IP_VER4) {
  364. ipiput6(f, ifc, bp);
  365. return;
  366. }
  367. ip = f->ip;
  368. ip->stats[InReceives]++;
  369. /*
  370. * Ensure we have all the header info in the first
  371. * block. Make life easier for other protocols by
  372. * collecting up to the first 64 bytes in the first block.
  373. */
  374. if(BLEN(bp) < 64) {
  375. hl = blocklen(bp);
  376. if(hl < IP4HDR)
  377. hl = IP4HDR;
  378. if(hl > 64)
  379. hl = 64;
  380. bp = pullupblock(bp, hl);
  381. if(bp == nil)
  382. return;
  383. }
  384. h = (Ip4hdr*)(bp->rp);
  385. /* dump anything that whose header doesn't checksum */
  386. if((bp->flag & Bipck) == 0 && ipcsum(&h->vihl)) {
  387. ip->stats[InHdrErrors]++;
  388. netlog(f, Logip, "ip: checksum error %V\n", h->src);
  389. freeblist(bp);
  390. return;
  391. }
  392. v4tov6(v6dst, h->dst);
  393. notforme = ipforme(f, v6dst) == 0;
  394. /* Check header length and version */
  395. if((h->vihl&0x0F) != IP_HLEN4) {
  396. hl = (h->vihl&0xF)<<2;
  397. if(hl < (IP_HLEN4<<2)) {
  398. ip->stats[InHdrErrors]++;
  399. netlog(f, Logip, "ip: %V bad hivl %ux\n", h->src, h->vihl);
  400. freeblist(bp);
  401. return;
  402. }
  403. /* If this is not routed strip off the options */
  404. if(notforme == 0) {
  405. olen = nhgets(h->length);
  406. dp = bp->rp + (hl - (IP_HLEN4<<2));
  407. memmove(dp, h, IP_HLEN4<<2);
  408. bp->rp = dp;
  409. h = (Ip4hdr*)(bp->rp);
  410. h->vihl = (IP_VER4|IP_HLEN4);
  411. hnputs(h->length, olen-hl+(IP_HLEN4<<2));
  412. }
  413. }
  414. /* route */
  415. if(notforme) {
  416. if(!ip->iprouting){
  417. freeb(bp);
  418. return;
  419. }
  420. /* don't forward to source's network */
  421. sr = v4lookup(f, h->src);
  422. r = v4lookup(f, h->dst);
  423. if(r == nil || sr == r){
  424. ip->stats[OutDiscards]++;
  425. freeblist(bp);
  426. return;
  427. }
  428. /* don't forward if packet has timed out */
  429. hop = h->ttl;
  430. if(hop < 1) {
  431. ip->stats[InHdrErrors]++;
  432. icmpttlexceeded(f, ifc->lifc->local, bp);
  433. freeblist(bp);
  434. return;
  435. }
  436. /* reassemble if the interface expects it */
  437. if(r->ifc->reassemble){
  438. frag = nhgets(h->frag);
  439. if(frag) {
  440. h->tos = 0;
  441. if(frag & IP_MF)
  442. h->tos = 1;
  443. bp = ip4reassemble(ip, frag, bp, h);
  444. if(bp == nil)
  445. return;
  446. h = (Ip4hdr*)(bp->rp);
  447. }
  448. }
  449. ip->stats[ForwDatagrams]++;
  450. tos = h->tos;
  451. hop = h->ttl;
  452. ipoput4(f, bp, 1, hop - 1, tos);
  453. return;
  454. }
  455. frag = nhgets(h->frag);
  456. if(frag) {
  457. h->tos = 0;
  458. if(frag & IP_MF)
  459. h->tos = 1;
  460. bp = ip4reassemble(ip, frag, bp, h);
  461. if(bp == nil)
  462. return;
  463. h = (Ip4hdr*)(bp->rp);
  464. }
  465. /* don't let any frag info go up the stack */
  466. h->frag[0] = 0;
  467. h->frag[1] = 0;
  468. proto = h->proto;
  469. p = Fsrcvpcol(f, proto);
  470. if(p != nil && p->rcv != nil) {
  471. ip->stats[InDelivers]++;
  472. (*p->rcv)(p, ifc, bp);
  473. return;
  474. }
  475. ip->stats[InDiscards]++;
  476. ip->stats[InUnknownProtos]++;
  477. freeblist(bp);
  478. }
  479. int
  480. ipstats(Fs *f, char *buf, int len)
  481. {
  482. IP *ip;
  483. char *p, *e;
  484. int i;
  485. ip = f->ip;
  486. ip->stats[DefaultTTL] = MAXTTL;
  487. p = buf;
  488. e = p+len;
  489. for(i = 0; i < Nstats; i++)
  490. p = seprint(p, e, "%s: %lud\n", statnames[i], ip->stats[i]);
  491. return p - buf;
  492. }
  493. Block*
  494. ip4reassemble(IP *ip, int offset, Block *bp, Ip4hdr *ih)
  495. {
  496. int fend;
  497. ushort id;
  498. Fragment4 *f, *fnext;
  499. ulong src, dst;
  500. Block *bl, **l, *last, *prev;
  501. int ovlap, len, fragsize, pktposn;
  502. src = nhgetl(ih->src);
  503. dst = nhgetl(ih->dst);
  504. id = nhgets(ih->id);
  505. /*
  506. * block lists are too hard, pullupblock into a single block
  507. */
  508. if(bp->next){
  509. bp = pullupblock(bp, blocklen(bp));
  510. ih = (Ip4hdr*)(bp->rp);
  511. }
  512. qlock(&ip->fraglock4);
  513. /*
  514. * find a reassembly queue for this fragment
  515. */
  516. for(f = ip->flisthead4; f; f = fnext){
  517. fnext = f->next; /* because ipfragfree4 changes the list */
  518. if(f->src == src && f->dst == dst && f->id == id)
  519. break;
  520. if(f->age < NOW){
  521. ip->stats[ReasmTimeout]++;
  522. ipfragfree4(ip, f);
  523. }
  524. }
  525. /*
  526. * if this isn't a fragmented packet, accept it
  527. * and get rid of any fragments that might go
  528. * with it.
  529. */
  530. if(!ih->tos && (offset & ~(IP_MF|IP_DF)) == 0) {
  531. if(f != nil) {
  532. ipfragfree4(ip, f);
  533. ip->stats[ReasmFails]++;
  534. }
  535. qunlock(&ip->fraglock4);
  536. return bp;
  537. }
  538. if(bp->base+sizeof(Ipfrag) >= bp->rp){
  539. bp = padblock(bp, sizeof(Ipfrag));
  540. bp->rp += sizeof(Ipfrag);
  541. }
  542. BKFG(bp)->foff = offset<<3;
  543. BKFG(bp)->flen = nhgets(ih->length)-IP4HDR;
  544. /* First fragment allocates a reassembly queue */
  545. if(f == nil) {
  546. f = ipfragallo4(ip);
  547. f->id = id;
  548. f->src = src;
  549. f->dst = dst;
  550. f->blist = bp;
  551. qunlock(&ip->fraglock4);
  552. ip->stats[ReasmReqds]++;
  553. return nil;
  554. }
  555. /*
  556. * find the new fragment's position in the queue
  557. */
  558. prev = nil;
  559. l = &f->blist;
  560. bl = f->blist;
  561. while(bl != nil && BKFG(bp)->foff > BKFG(bl)->foff) {
  562. prev = bl;
  563. l = &bl->next;
  564. bl = bl->next;
  565. }
  566. /* Check overlap of a previous fragment - trim away as necessary */
  567. if(prev) {
  568. ovlap = BKFG(prev)->foff + BKFG(prev)->flen - BKFG(bp)->foff;
  569. if(ovlap > 0) {
  570. if(ovlap >= BKFG(bp)->flen) {
  571. freeblist(bp);
  572. qunlock(&ip->fraglock4);
  573. return nil;
  574. }
  575. BKFG(prev)->flen -= ovlap;
  576. }
  577. }
  578. /* Link onto assembly queue */
  579. bp->next = *l;
  580. *l = bp;
  581. /* Check to see if succeeding segments overlap */
  582. if(bp->next) {
  583. l = &bp->next;
  584. fend = BKFG(bp)->foff + BKFG(bp)->flen;
  585. /* Take completely covered segments out */
  586. while(*l) {
  587. ovlap = fend - BKFG(*l)->foff;
  588. if(ovlap <= 0)
  589. break;
  590. if(ovlap < BKFG(*l)->flen) {
  591. BKFG(*l)->flen -= ovlap;
  592. BKFG(*l)->foff += ovlap;
  593. /* move up ih hdrs */
  594. memmove((*l)->rp + ovlap, (*l)->rp, IP4HDR);
  595. (*l)->rp += ovlap;
  596. break;
  597. }
  598. last = (*l)->next;
  599. (*l)->next = nil;
  600. freeblist(*l);
  601. *l = last;
  602. }
  603. }
  604. /*
  605. * look for a complete packet. if we get to a fragment
  606. * without IP_MF set, we're done.
  607. */
  608. pktposn = 0;
  609. for(bl = f->blist; bl; bl = bl->next) {
  610. if(BKFG(bl)->foff != pktposn)
  611. break;
  612. if((BLKIP(bl)->frag[0]&(IP_MF>>8)) == 0) {
  613. bl = f->blist;
  614. len = nhgets(BLKIP(bl)->length);
  615. bl->wp = bl->rp + len;
  616. /* Pullup all the fragment headers and
  617. * return a complete packet
  618. */
  619. for(bl = bl->next; bl; bl = bl->next) {
  620. fragsize = BKFG(bl)->flen;
  621. len += fragsize;
  622. bl->rp += IP4HDR;
  623. bl->wp = bl->rp + fragsize;
  624. }
  625. bl = f->blist;
  626. f->blist = nil;
  627. ipfragfree4(ip, f);
  628. ih = BLKIP(bl);
  629. hnputs(ih->length, len);
  630. qunlock(&ip->fraglock4);
  631. ip->stats[ReasmOKs]++;
  632. return bl;
  633. }
  634. pktposn += BKFG(bl)->flen;
  635. }
  636. qunlock(&ip->fraglock4);
  637. return nil;
  638. }
  639. /*
  640. * ipfragfree4 - Free a list of fragments - assume hold fraglock4
  641. */
  642. void
  643. ipfragfree4(IP *ip, Fragment4 *frag)
  644. {
  645. Fragment4 *fl, **l;
  646. if(frag->blist)
  647. freeblist(frag->blist);
  648. frag->src = 0;
  649. frag->id = 0;
  650. frag->blist = nil;
  651. l = &ip->flisthead4;
  652. for(fl = *l; fl; fl = fl->next) {
  653. if(fl == frag) {
  654. *l = frag->next;
  655. break;
  656. }
  657. l = &fl->next;
  658. }
  659. frag->next = ip->fragfree4;
  660. ip->fragfree4 = frag;
  661. }
  662. /*
  663. * ipfragallo4 - allocate a reassembly queue - assume hold fraglock4
  664. */
  665. Fragment4 *
  666. ipfragallo4(IP *ip)
  667. {
  668. Fragment4 *f;
  669. while(ip->fragfree4 == nil) {
  670. /* free last entry on fraglist */
  671. for(f = ip->flisthead4; f->next; f = f->next)
  672. ;
  673. ipfragfree4(ip, f);
  674. }
  675. f = ip->fragfree4;
  676. ip->fragfree4 = f->next;
  677. f->next = ip->flisthead4;
  678. ip->flisthead4 = f;
  679. f->age = NOW + 30000;
  680. return f;
  681. }
  682. ushort
  683. ipcsum(uchar *addr)
  684. {
  685. int len;
  686. ulong sum;
  687. sum = 0;
  688. len = (addr[0]&0xf)<<2;
  689. while(len > 0) {
  690. sum += addr[0]<<8 | addr[1] ;
  691. len -= 2;
  692. addr += 2;
  693. }
  694. sum = (sum & 0xffff) + (sum >> 16);
  695. sum = (sum & 0xffff) + (sum >> 16);
  696. return (sum^0xffff);
  697. }