Back to home page

LXR

 
 

    


0001 /*
0002  *
0003  * INET     An implementation of the TCP/IP protocol suite for the LINUX
0004  *      operating system.  INET is implemented using the  BSD Socket
0005  *      interface as the means of communication with the user level.
0006  *
0007  *      IP/TCP/UDP checksumming routines
0008  *
0009  * Authors: Jorge Cwik, <jorge@laser.satlink.net>
0010  *      Arnt Gulbrandsen, <agulbra@nvg.unit.no>
0011  *      Tom May, <ftom@netcom.com>
0012  *      Andreas Schwab, <schwab@issan.informatik.uni-dortmund.de>
0013  *      Lots of code moved from tcp.c and ip.c; see those files
0014  *      for more names.
0015  *
0016  * 03/02/96 Jes Sorensen, Andreas Schwab, Roman Hodek:
0017  *      Fixed some nasty bugs, causing some horrible crashes.
0018  *      A: At some points, the sum (%0) was used as
0019  *      length-counter instead of the length counter
0020  *      (%1). Thanks to Roman Hodek for pointing this out.
0021  *      B: GCC seems to mess up if one uses too many
0022  *      data-registers to hold input values and one tries to
0023  *      specify d0 and d1 as scratch registers. Letting gcc
0024  *      choose these registers itself solves the problem.
0025  *
0026  *      This program is free software; you can redistribute it and/or
0027  *      modify it under the terms of the GNU General Public License
0028  *      as published by the Free Software Foundation; either version
0029  *      2 of the License, or (at your option) any later version.
0030  */
0031 
0032 /* Revised by Kenneth Albanowski for m68knommu. Basic problem: unaligned access
0033  kills, so most of the assembly has to go. */
0034 
0035 #include <linux/export.h>
0036 #include <net/checksum.h>
0037 
0038 #include <asm/byteorder.h>
0039 
0040 #ifndef do_csum
0041 static inline unsigned short from32to16(unsigned int x)
0042 {
0043     /* add up 16-bit and 16-bit for 16+c bit */
0044     x = (x & 0xffff) + (x >> 16);
0045     /* add up carry.. */
0046     x = (x & 0xffff) + (x >> 16);
0047     return x;
0048 }
0049 
0050 static unsigned int do_csum(const unsigned char *buff, int len)
0051 {
0052     int odd;
0053     unsigned int result = 0;
0054 
0055     if (len <= 0)
0056         goto out;
0057     odd = 1 & (unsigned long) buff;
0058     if (odd) {
0059 #ifdef __LITTLE_ENDIAN
0060         result += (*buff << 8);
0061 #else
0062         result = *buff;
0063 #endif
0064         len--;
0065         buff++;
0066     }
0067     if (len >= 2) {
0068         if (2 & (unsigned long) buff) {
0069             result += *(unsigned short *) buff;
0070             len -= 2;
0071             buff += 2;
0072         }
0073         if (len >= 4) {
0074             const unsigned char *end = buff + ((unsigned)len & ~3);
0075             unsigned int carry = 0;
0076             do {
0077                 unsigned int w = *(unsigned int *) buff;
0078                 buff += 4;
0079                 result += carry;
0080                 result += w;
0081                 carry = (w > result);
0082             } while (buff < end);
0083             result += carry;
0084             result = (result & 0xffff) + (result >> 16);
0085         }
0086         if (len & 2) {
0087             result += *(unsigned short *) buff;
0088             buff += 2;
0089         }
0090     }
0091     if (len & 1)
0092 #ifdef __LITTLE_ENDIAN
0093         result += *buff;
0094 #else
0095         result += (*buff << 8);
0096 #endif
0097     result = from32to16(result);
0098     if (odd)
0099         result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
0100 out:
0101     return result;
0102 }
0103 #endif
0104 
0105 #ifndef ip_fast_csum
0106 /*
0107  *  This is a version of ip_compute_csum() optimized for IP headers,
0108  *  which always checksum on 4 octet boundaries.
0109  */
0110 __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
0111 {
0112     return (__force __sum16)~do_csum(iph, ihl*4);
0113 }
0114 EXPORT_SYMBOL(ip_fast_csum);
0115 #endif
0116 
0117 /*
0118  * computes the checksum of a memory block at buff, length len,
0119  * and adds in "sum" (32-bit)
0120  *
0121  * returns a 32-bit number suitable for feeding into itself
0122  * or csum_tcpudp_magic
0123  *
0124  * this function must be called with even lengths, except
0125  * for the last fragment, which may be odd
0126  *
0127  * it's best to have buff aligned on a 32-bit boundary
0128  */
0129 __wsum csum_partial(const void *buff, int len, __wsum wsum)
0130 {
0131     unsigned int sum = (__force unsigned int)wsum;
0132     unsigned int result = do_csum(buff, len);
0133 
0134     /* add in old sum, and carry.. */
0135     result += sum;
0136     if (sum > result)
0137         result += 1;
0138     return (__force __wsum)result;
0139 }
0140 EXPORT_SYMBOL(csum_partial);
0141 
0142 /*
0143  * this routine is used for miscellaneous IP-like checksums, mainly
0144  * in icmp.c
0145  */
0146 __sum16 ip_compute_csum(const void *buff, int len)
0147 {
0148     return (__force __sum16)~do_csum(buff, len);
0149 }
0150 EXPORT_SYMBOL(ip_compute_csum);
0151 
0152 /*
0153  * copy from fs while checksumming, otherwise like csum_partial
0154  */
0155 __wsum
0156 csum_partial_copy_from_user(const void __user *src, void *dst, int len,
0157                         __wsum sum, int *csum_err)
0158 {
0159     int missing;
0160 
0161     missing = __copy_from_user(dst, src, len);
0162     if (missing) {
0163         memset(dst + len - missing, 0, missing);
0164         *csum_err = -EFAULT;
0165     } else
0166         *csum_err = 0;
0167 
0168     return csum_partial(dst, len, sum);
0169 }
0170 EXPORT_SYMBOL(csum_partial_copy_from_user);
0171 
0172 /*
0173  * copy from ds while checksumming, otherwise like csum_partial
0174  */
0175 __wsum
0176 csum_partial_copy(const void *src, void *dst, int len, __wsum sum)
0177 {
0178     memcpy(dst, src, len);
0179     return csum_partial(dst, len, sum);
0180 }
0181 EXPORT_SYMBOL(csum_partial_copy);
0182 
0183 #ifndef csum_tcpudp_nofold
0184 static inline u32 from64to32(u64 x)
0185 {
0186     /* add up 32-bit and 32-bit for 32+c bit */
0187     x = (x & 0xffffffff) + (x >> 32);
0188     /* add up carry.. */
0189     x = (x & 0xffffffff) + (x >> 32);
0190     return (u32)x;
0191 }
0192 
0193 __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
0194               __u32 len, __u8 proto, __wsum sum)
0195 {
0196     unsigned long long s = (__force u32)sum;
0197 
0198     s += (__force u32)saddr;
0199     s += (__force u32)daddr;
0200 #ifdef __BIG_ENDIAN
0201     s += proto + len;
0202 #else
0203     s += (proto + len) << 8;
0204 #endif
0205     return (__force __wsum)from64to32(s);
0206 }
0207 EXPORT_SYMBOL(csum_tcpudp_nofold);
0208 #endif