linux/lib/checksum.c
<<
>>
Prefs
   1/*
   2 *
   3 * INET         An implementation of the TCP/IP protocol suite for the LINUX
   4 *              operating system.  INET is implemented using the  BSD Socket
   5 *              interface as the means of communication with the user level.
   6 *
   7 *              IP/TCP/UDP checksumming routines
   8 *
   9 * Authors:     Jorge Cwik, <jorge@laser.satlink.net>
  10 *              Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  11 *              Tom May, <ftom@netcom.com>
  12 *              Andreas Schwab, <schwab@issan.informatik.uni-dortmund.de>
  13 *              Lots of code moved from tcp.c and ip.c; see those files
  14 *              for more names.
  15 *
  16 * 03/02/96     Jes Sorensen, Andreas Schwab, Roman Hodek:
  17 *              Fixed some nasty bugs, causing some horrible crashes.
  18 *              A: At some points, the sum (%0) was used as
  19 *              length-counter instead of the length counter
  20 *              (%1). Thanks to Roman Hodek for pointing this out.
  21 *              B: GCC seems to mess up if one uses too many
  22 *              data-registers to hold input values and one tries to
  23 *              specify d0 and d1 as scratch registers. Letting gcc
  24 *              choose these registers itself solves the problem.
  25 *
  26 *              This program is free software; you can redistribute it and/or
  27 *              modify it under the terms of the GNU General Public License
  28 *              as published by the Free Software Foundation; either version
  29 *              2 of the License, or (at your option) any later version.
  30 */
  31
  32/* Revised by Kenneth Albanowski for m68knommu. Basic problem: unaligned access
  33 kills, so moion2eth Albanowski 2/span>
 8" clas/checksumR32" id="L32" c" name="L32"> 8"5 class="line" na"L29">  29 *    3     3   Th#inclut">ent"sic problinclut"ction=/ clule.hclass="cot">f">tion=/ clule.h  Thm tcasic problem: unaligned accr3ent"sic problinclut"cpan unalignedhclass="cot">f">pan unalignedh  Thm tcasic problem: unaligned acc/span>
<3 href="lib/checksum.c#L38" id37u. Basic problem: unaligned acc
3a href="lib/checksum.c#329" i38  Th#inclut">ent"sic probl+ambig=inclut"casm-alpha/byteorent.h|inclut"casm-arm/byteorent.h|inclut"casm-avr32/byteorent.h|inclut"casm-blackfin/byteorent.h|inclut"casm-cris/byteorent.h|inclut"casm-frv/byteorent.h|inclut"casm-gblisic/byteorent.h|inclut"casm-h8300/byteorent.h|inclut"casm-i386/byteorent.h|inclut"casm-ia64/byteorent.h|inclut"casm-m32r/byteorent.h|inclut"casm-mkil/byteorent.h|inclut"casm-mkills, s/byteorent.h|inclut"casm-mips/byteorent.h|inclut"casm-mips64/byteorent.h|inclut"casm-parisc/byteorent.h|inclut"casm-powerpc/byteorent.h|inclut"casm-ppc/byteorent.h|inclut"casm-s390/byteorent.h|inclut"casm-sh/byteorent.h|inclut"casm-sh64/byteorent.h|inclut"casm-a"Lrc/byteorent.h|inclut"casm-s"Lrc64/byteorent.h|inclut"casm-um/byteorent.h|inclut"casm-v850/byteorent.h|inclut"casm-x86/byteorent.h|inclut"casm-x86_64/byteorent.h|inclut"casm-xt"> a/byteorent.hclass="cotalsumasm/byteorent.h  Thm tcasic problem: unaligned accan>
f">do_cgneu. Basic problem: unaligned ac4vised by4Kenneth Albanowski for 468kno4mu. Brch(thx+*" methodent"=inbanowsb624fc9_">f">inbanou. Bnowsi 2/spshortx+*" methodent"=    32tosum.c#L17" _">f">    32tosuu. B(owsi 2/spms x+*" methodent"=xm.c#L17" _">f">xu. B)asic problem: unaligned ac4ess
16+c bhe e" na"L29">  29f">xu. B = (L*" methodent"=xm.c#L17" _">f">xu. B & 0xffff) + (L*" methodent"=xm.c#L17" _">f">xu. B &e="hidd>16)casic problem: unaligned ac4 *    4     45me="  29f">xu. B = (L*" methodent"=xm.c#L17" _">f">xu. B & 0xffff) + (L*" methodent"=xm.c#L17" _">f">xu. B &e="hidd>16)casic problem: unaligned ac4/span>
<4 href="lib/checksum.c#L48" id47me="L*" methodent"=xm.c#L17" _">f">xu. Bcasic problem: unaligned ac4
4a href="lib/checksum.c#429" i48  Th}asic problem: unaligned ac4an>
f">do_cgneu. B(constxowsi 2/spchar *+*" methodent"=buff69b624fc9_">f">buffu. B,pms x+*" methodent"=lesulb624fc9_">f">lesu. B)asic problem: unaligned ac5vised by5Kenneth Albanowski for 568kno51id="{asic problem: unaligned ac5ess
f">oddu. B,p+*" methodent"= Romaulb624fc9_">f"> Romau. Bcasic problem: unaligned ac5as/check5umR32" id="L32" class="5ine" 5ame="f">a hrefu. B = 0casic problem: unaligned ac5span>
f">lesu. B>ent"= 0)asic problem: unaligned ac5r5f">oufu. Bcasic problem: unaligned ac5/span>
<5 href="lib/checksum.c#L58" id57me="f">oddu. B = 1 & (owsi 2/splong)lL*" methodent"=buff69b624fc9_">f">buffu. Bcasic problem: unaligned ac5
5a href="lib/checksum.c#529" i58me="f">oddu. B) {asic problem: unaligned ac5an>
f">__LITTLE_ENDIANu. Basic problem: unaligned ac61
f">a hrefu. B += (*+*" methodent"=buff69b624fc9_">f">buffu. B>ent"ent" 8)casic problem: unaligned ac6vised by6Kenneth Albanowski for 668kno61id="#elseasic problem: unaligned ac6ess
f">a hrefu. B = *+*" methodent"=buff69b624fc9_">f">buffu. Bcasic problem: unaligned ac6as/check6umR32" id="L32" class="6ine" 63id="#endifasic problem: unaligned ac6span>
f">lesu. B--casic problem: unaligned ac6span>
f">buffu. B++casic problem: unaligned ac6span classs="comment"> *         L27" 66me="
<6 href="lib/checksum.c#L68" id67me="f"> Romau. B = L*" methodent"=lesulb624fc9_">f">lesu. B>ee="hidd>1;span clllllllll+33" class="line" name="Lnr" na16-bhe wores.ine" na"L29">  29f"> Romau. B) {asic problem: unaligned ac6an>
f">buffu. B) {asic problem: unaligned ac71
f">a hrefu. B += *(owsi 2/spshortx*)lL*" methodent"=buff69b624fc9_">f">buffu. Bcasic problem: unaligned ac7vised by7Kenneth Albanowski for 768kno71me="f"> Romau. B--casic problem: unaligned ac7ess
f">lesu. B -= 2casic problem: unaligned ac7as/check7umR32" id="L32" class="7ine" 73me="f">buffu. B>+= 2casic problem: unaligned ac7span>

f"> Romau. B ee="hidd=>1;span cllllll+33" class="line" name="Lnr" na32-bhe wores.ine" na"L29">  29 *        7L27" 76me="f"> Romau. B) {asic problem: unaligned ac7c#L8" id=="L8" class="line" name=8" id77me="f"> arryu. B = 0casic problem: unaligned ac7
7a href="lib/checksum.c#729" i78me="
f">wu. B = *(owsi 2/spms x*)lL*" methodent"=buff69b624fc9_">f">buffu. Bcasic problem: unaligned ac81
f"> Romau. B--casic problem: unaligned ac8vised by8Kenneth Albanowski for 868kno81me="f">buffu. B>+= 4casic problem: unaligned ac8ess
f">a hrefu. B += +*" methodent"=carryulb624fc9_">f"> arryu. Bcasic problem: unaligned ac8as/check8umR32" id="L32" class="8ine" 83me="f">a hrefu. B += +*" methodent"=wulb624fc9_">f">wu. Bcasic problem: unaligned ac8span>
f"> arryu. B = (L*" methodent"=wulb624fc9_">f">wu. B hidd>+*" methodent"=a hrefulb624fc9_">f">a hrefu. B)casic problem: unaligned ac8span>
f"> Romau. B)casic problem: unaligned ac8span cla8ss="comment"> *        8L27" 86me="f">a hrefu. B += +*" methodent"=carryulb624fc9_">f"> arryu. Bcasic problem: unaligned ac8c#L8" id8="L8" class="line" name88" id87me="f">a hrefu. B = (L*" methodent"=a hrefulb624fc9_">f">a hrefu. B & 0xffff) + (L*" methodent"=a hrefulb624fc9_">f">a hrefu. B &e="hidd>16)casic problem: unaligned ac8span classs="comment"> * Authors:29" i88me="
f">lesu. B>eamp; 2) {asic problem: unaligned ac91
f">a hrefu. B += *(owsi 2/spshortx*)lL*" methodent"=buff69b624fc9_">f">buffu. Bcasic problem: unaligned ac9vised by9Kenneth Albanowski for 968kno91me="f">buffu. B>+= 2casic problem: unaligned ac9ess
f">lesu. B>eamp; 1)asic problem: unaligned ac9span>
f">__LITTLE_ENDIANu. Basic problem: unaligned ac9span cla9ss="comment"> *        9L27" 96me="f">a hrefu. B += *L*" methodent"=buff69b624fc9_">f">buffu. Bcasic problem: unaligned ac9c#L8" id9="L8" class="line" name98" id97id="#elseasic problem: unaligned ac9span cla9ss="comment"> * Authors929" i98me="f">a hrefu. B += (*+*" methodent"=buff69b624fc9_">f">buffu. B>ent"ent" 8)casic problem: unaligned ac9b/checksuum.c#L10" id="L10" classid="L99id="#endifasic problem: unaligned ac10="lib/checcksum.c#L11" id="L11" cla" idla"me="f">a hrefu. B = +*" methodent"=    32tosum.c#L17" _">f">    32tosuu. B(+*" methodent"=a hrefulb624fc9_">f">a hrefu. B)casic problem: unaligned ac10m.c#L12" 0id="L12" class="line" na01 idla1me="f">oddu. B)asic problem: unaligned ac10gt;
f">a hrefu. B = ((L*" methodent"=a hrefulb624fc9_">f">a hrefu. B &e="hidd>8) & 0xff) | ((L*" methodent"=a hrefulb624fc9_">f">a hrefu. B & 0xff) ent"ent" 8)casic problem: unaligned ac10n>
f">oufu. B:asic problem: unaligned ac10 class="l0ine" name="L15">  1504 idla4me="L*" methodent"=a hrefulb624fc9_">f">a hrefu. Bcasic problem: unaligned ac10 * 03/005 idla5  Th}asic problem: unaligned ac10ib/checks0um.c#L17" id="L17" class06 idla6id="#endifasic problem: unaligned ac10
   3<10"lib/chec0ksum.c#L20" id="L20" cla09 idla0" class="line" name="L30">  30an cl clac#L31" i" naip_me=pute_cgne()ref="miz    or IP contL31,om>
  11
  12  29f">__ss=sume="f">ip_fast_cgneu. B(constxvoid *L*" methodent"=iph69b624fc9_">f">iphu. B,powsi 2/spms x+*" methodent"=ihl69b624fc9_">f">ihlu. B)asic problem: unaligned ac1an>
  15>(L*" methodent"=__ orcowsb624fc9_">f">__ orcome="f">__ss=sume=")~L*" methodent"=do_cgne69b624fc9_">f">do_cgneu. B(L*" methodent"=iph69b624fc9_">f">iphu. B,p+*" methodent"=ihl69b624fc9_">f">ihlu. B*4)casic problem: unaligned ac16 * 03/002/96115  Th}asic problem: unaligned ac1lib/checkssum.c#L17" id="L17" classs="li1e" name*" methodent"=EXPORT_SYMBOL69b624fc9_">f">EXPORT_SYMBOLu. B(L*" methodent"=ip_fast_cgne69b624fc9_">f">ip_fast_cgneu. B)casic problem: unaligned ac16
  19<3" class="line" name="L3">   3<1="lib/checcksum.c#L20" id="L20" claass="1ine" name="L20">  20
  21
  22
  23
  24or cgne_tcpudp_magiself solves the problem.
  25
 *    1     1    This program is free softwmess ufun  1
1pan>
  " na"L29">  29f">__wgneme="f">cgne_partialu. B(constxvoid *L*" methodent"=buff69b624fc9_">f">buffu. B,pms x+*" methodent"=lesulb624fc9_">f">lesu. B,p+*" methodent"=__wgne69b624fc9_">f">__wgneme="f">wgneme=")asic problem: unaligned ac1evised by1 Kenneth Albanowski for 1m68kn131id="{asic problem: unaligned ac1cess
f">gneme="<= (L*" methodent"=__ orcowsb624fc9_">f">__ orcome="f">wgneme="casic problem: unaligned ac13n>
f">a hrefu. B = L*" methodent"=do_cgne69b624fc9_">f">do_cgneu. B(L*" methodent"=buff69b624fc9_">f">buffu. B,p+*" methodent"=lesulb624fc9_">f">lesu. B)casic problem: unaligned ac1ments, th1e sum (%0) was used 1as *    13    135me="  29f">a hrefu. B += +*" methodent"=gne69b624fc9_">f">gneme="casic problem: unaligned ac1m
f">gneme="+*" methodent"=a hrefulb624fc9_">f">a hrefu. B)asic problem: unaligned ac1mef="lib/c3a href="lib/checksum.c#1329" 138me="f">a hrefu. B += 1casic problem: unaligned ac1man>
(L*" methodent"=__ orcowsb624fc9_">f">__ orcome="f">__wgneme=")L*" methodent"=a hrefulb624fc9_">f">a hrefu. Bcasic problem: unaligned ac141
f">EXPORT_SYMBOLu. B(L*" methodent"=cgne_partial69b624fc9_">f">cgne_partialu. B)casic problem: unaligned ac14ess

  24<3" class="line" name="L3">   3<14span>
  25 iscellaneous IP-likc#hecksum.s, mainlput values and one tries to14 *    14    14   This program is free softwm n icmp.self solves the problem.
14  29f">__ss=sume="f">ip_me=pute_cgneu. B(constxvoid *L*" methodent"=buff69b624fc9_">f">buffu. B,pms x+*" methodent"=lesulb624fc9_">f">lesu. B)asic problem: unaligned ac14
14a href="lib/checksum.c#1429" 148  Th{asic problem: unaligned ac14an>
(L*" methodent"=__ orcowsb624fc9_">f">__ orcome="f">__ss=sume=")~L*" methodent"=do_cgne69b624fc9_">f">do_cgneu. B(L*" methodent"=buff69b624fc9_">f">buffu. B,p+*" methodent"=lesulb624fc9_">f">lesu. B)casic problem: unaligned ac151
f">EXPORT_SYMBOLu. B(L*" methodent"=ip_me=pute_cgne69b624fc9_">f">ip_me=pute_cgneu. B)casic problem: unaligned ac15ess
  24<3" class="line" name="L3">   3<15span>
  25   3   3<15  29f">__wgneme="">  29f">cgne_partial_copy_    _L7" u. B(constxvoid e*" methodent"=__L7" 69b624fc9_">f">__L7" u. B *L*" methodent"=src69b624fc9_">f">grcu. B,pvoid *L*" methodent"=dsfulb624fc9_">f">dsfu. B,pms x+*" methodent"=lesulb624fc9_">f">lesu. B,">  29f">__wgneme="f">gneme=",pms x*L*" methodent"=cgne_er 69b624fc9_">f">cgne_er u. B)asic problem: unaligned ac15an>
f"> is A: u. Bcasic problem: unaligned ac16vised by16Kenneth Albanowski for 1668kn16mu. Basic problem: unaligned ac16ess
f"> is A: u. B = L*" methodent"=__copy_    _L7" 69b624fc9_">f">__copy_    _L7" u. B(L*" methodent"=dsfulb624fc9_">f">dsfu. B,pL*" methodent"=src69b624fc9_">f">grcu. B,p+*" methodent"=lesulb624fc9_">f">lesu. B)casic problem: unaligned ac16as/check16umR32" id="L32" class="16ine"16ame="f"> is A: u. B) {asic problem: unaligned ac16span>
f">memsefu. B(L*" methodent"=dsfulb624fc9_">f">dsfu. B +l+*" methodent"=lesulb624fc9_">f">lesu. B -l+*" methodent"= is A: 69b624fc9_">f"> is A: u. B, 0,p+*" methodent"= is A: 69b624fc9_">f"> is A: u. B)casic problem: unaligned ac16f">cgne_er u. B = -e*" methodent"=EFAULT69b624fc9_">f">EFAULTu. Bcasic problem: unaligned ac16ib/checksuum.c#L17" id="L17" class=L27"166me="
<16 href="lib/checksum.c#L168" i167me="f">cgne_er u. B = 0casic problem: unaligned ac16
16a href="lib/checksum.c#1629" 168me="asic problem: unaligned ac16an>
L*" methodent"=cgne_partial69b624fc9_">f">cgne_partialu. B(L*" methodent"=dsfulb624fc9_">f">dsfu. B,pL*" methodent"=lesulb624fc9_">f">lesu. B,p+*" methodent"=gne69b624fc9_">f">gneme=")casic problem: unaligned ac171
f">EXPORT_SYMBOLu. B(L*" methodent"=cgne_partial_copy_    _L7" 69b624fc9_">f">cgne_partial_copy_    _L7" u. B)casic problem: unaligned ac17ess
  24<3" class="line" name="L3">   3<17span>
  25   3   3<17span>
  29 *        17L27"17" name*" methodent"=__wgne69b624fc9_">f">__wgneme="">  29f">cgne_partial_copyu. B(constxvoid *L*" methodent"=src69b624fc9_">f">grcu. B,pvoid *L*" methodent"=dsfulb624fc9_">f">dsfu. B,pms x+*" methodent"=lesulb624fc9_">f">lesu. B,l+*" methodent"=__wgne69b624fc9_">f">__wgneme="f">gneme=")asic problem: unaligned ac17
17a href="lib/checksum.c#1729" 178  Th{asic problem: unaligned ac17an>
f">memcpyu. B(L*" methodent"=dsfulb624fc9_">f">dsfu. B,pL*" methodent"=src69b624fc9_">f">grcu. B,p+*" methodent"=lesulb624fc9_">f">lesu. B)casic problem: unaligned ac181
f">cgne_partialu. B(L*" methodent"=dsfulb624fc9_">f">dsfu. B,pL*" methodent"=lesulb624fc9_">f">lesu. B,p+*" methodent"=gne69b624fc9_">f">gneme=")casic problem: unaligned ac18vised by18Kenneth Albanowski for 1868kn181me="}asic problem: unaligned ac18ess
f">EXPORT_SYMBOLu. B(L*" methodent"=cgne_partial_copy69b624fc9_">f">cgne_partial_copyu. B)casic problem: unaligned ac18as/check18umR32" id="L32" class="18ine"183me="asic problem: unaligned ac18span>
f">cgne_tcpudp_nofoldme="asic problem: unaligned ac18span>
f">__wgneme="f">cgne_tcpudp_nofoldme="(L*" methodent"=__ben>
f">__ben>me="f">gadd u. B,l+*" methodent"=__ben>
f">__ben>me="f">dadd u. B,asic problem: unaligned ac18span cla18ss="comment"> *        18L27"186me="f">lesu. B,">  29f">peratu. B,">  29f">__wgneme="f">gneme=")asic problem: unaligned ac18an>
f">gu. B = (L*" methodent"=__ orcowsb624fc9_">f">__ orcome="
f">un>me=")L*" methodent"=gne69b624fc9_">f">gneme="casic problem: unaligned ac19vised by19Kenneth Albanowski for 1968kn19mu. Basic problem: unaligned ac19ess
f">gu. B += (L*" methodent"=__ orcowsb624fc9_">f">__ orcome="
f">un>me=")L*" methodent"=gadd 69b624fc9_">f">gadd u. Bcasic problem: unaligned ac19as/check19umR32" id="L32" class="19ine"193me="f">gu. B += (L*" methodent"=__ orcowsb624fc9_">f">__ orcome="
f">un>me=")L*" methodent"=dadd 69b624fc9_">f">dadd u. Bcasic problem: unaligned ac19span>
f">__BIG_ENDIANu. Basic problem: unaligned ac19span>
f">gu. B += +*" methodent"=peratulb624fc9_">f">peratu. B +l+*" methodent"=lesulb624fc9_">f">lesu. Bcasic problem: unaligned ac19span cla19ss="comment"> *        19L27"196id="#elseasic problem: unaligned ac19c#L8" id19="L8" class="line" name198" i197me="f">gu. B += (L*" methodent"=peratulb624fc9_">f">peratu. B +l+*" methodent"=lesulb624fc9_">f">lesu. B) ent"ent" 8casic problem: unaligned ac19ef="lib/c9ss="comment"> * Authors1929" 198me="#endifasic problem: unaligned ac19b/checks1uum.c#L10" id="L10" clas1sid="199me="f">gu. B += (L*" methodent"=g69b624fc9_">f">gu. B &e="hidd>32)casic problem: unaligned ac20="lib/ch2ccksum.c#L11" id="L11" c2a" id200me="(L*" methodent"=__ orcowsb624fc9_">f">__ orcome="f">__wgneme=")L*" methodent"=g69b624fc9_">f">gu. Bcasic problem: unaligned ac20"lib/chec0id="L12" class="line" n201 id201me="}asic problem: unaligned ac20gt;
f">EXPORT_SYMBOLu. B(L*" methodent"=cgne_tcpudp_nofoldulb624fc9_">f">cgne_tcpudp_nofoldme=")casic problem: unaligned ac20a href="l0f="lib/checksum.c#L14" 203 id203id="#endifasic problem: unaligned ac20 class="20ine" name="L15">  15
The original LXR*        ment"> *sic problhttp://sourcofArnt</projects/lxr">LXR*a href=tyu. B,less uexperimmentlc#L31" i"entsic problmailto:lxr@lion=.no">lxr@lion=.nou. B.
m.c#L1"id="L" name=subfooter">
lxr.lion=.no kind8"5 ost 33Redpill Liopro ASu. B,lprovient"of Lioux consulters  and ass="cons services since"1995.
m.c#L1" i/body1"i/html="