ip.c 14 KB

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