ip.c 14 KB

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