ip.c 14 KB

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