Back to home page

OSCL-LXR

 
 

    


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