ip.c 14 KB

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