ip.c 15 KB

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