Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _ASM_X86_CHECKSUM_32_H
0003 #define _ASM_X86_CHECKSUM_32_H
0004 
0005 #include <linux/in6.h>
0006 #include <linux/uaccess.h>
0007 
0008 /*
0009  * computes the checksum of a memory block at buff, length len,
0010  * and adds in "sum" (32-bit)
0011  *
0012  * returns a 32-bit number suitable for feeding into itself
0013  * or csum_tcpudp_magic
0014  *
0015  * this function must be called with even lengths, except
0016  * for the last fragment, which may be odd
0017  *
0018  * it's best to have buff aligned on a 32-bit boundary
0019  */
0020 asmlinkage __wsum csum_partial(const void *buff, int len, __wsum sum);
0021 
0022 /*
0023  * the same as csum_partial, but copies from src while it
0024  * checksums, and handles user-space pointer exceptions correctly, when needed.
0025  *
0026  * here even more important to align src and dst on a 32-bit (or even
0027  * better 64-bit) boundary
0028  */
0029 
0030 asmlinkage __wsum csum_partial_copy_generic(const void *src, void *dst, int len);
0031 
0032 /*
0033  *  Note: when you get a NULL pointer exception here this means someone
0034  *  passed in an incorrect kernel address to one of these functions.
0035  *
0036  *  If you use these functions directly please don't forget the
0037  *  access_ok().
0038  */
0039 static inline __wsum csum_partial_copy_nocheck(const void *src, void *dst, int len)
0040 {
0041     return csum_partial_copy_generic(src, dst, len);
0042 }
0043 
0044 static inline __wsum csum_and_copy_from_user(const void __user *src,
0045                          void *dst, int len)
0046 {
0047     __wsum ret;
0048 
0049     might_sleep();
0050     if (!user_access_begin(src, len))
0051         return 0;
0052     ret = csum_partial_copy_generic((__force void *)src, dst, len);
0053     user_access_end();
0054 
0055     return ret;
0056 }
0057 
0058 /*
0059  *  This is a version of ip_compute_csum() optimized for IP headers,
0060  *  which always checksum on 4 octet boundaries.
0061  *
0062  *  By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
0063  *  Arnt Gulbrandsen.
0064  */
0065 static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
0066 {
0067     unsigned int sum;
0068 
0069     asm volatile("movl (%1), %0 ;\n"
0070              "subl $4, %2   ;\n"
0071              "jbe 2f        ;\n"
0072              "addl 4(%1), %0    ;\n"
0073              "adcl 8(%1), %0    ;\n"
0074              "adcl 12(%1), %0;\n"
0075              "1:    adcl 16(%1), %0 ;\n"
0076              "lea 4(%1), %1 ;\n"
0077              "decl %2   ;\n"
0078              "jne 1b        ;\n"
0079              "adcl $0, %0   ;\n"
0080              "movl %0, %2   ;\n"
0081              "shrl $16, %0  ;\n"
0082              "addw %w2, %w0 ;\n"
0083              "adcl $0, %0   ;\n"
0084              "notl %0   ;\n"
0085              "2:        ;\n"
0086     /* Since the input registers which are loaded with iph and ihl
0087        are modified, we must also specify them as outputs, or gcc
0088        will assume they contain their original values. */
0089              : "=r" (sum), "=r" (iph), "=r" (ihl)
0090              : "1" (iph), "2" (ihl)
0091              : "memory");
0092     return (__force __sum16)sum;
0093 }
0094 
0095 /*
0096  *  Fold a partial checksum
0097  */
0098 
0099 static inline __sum16 csum_fold(__wsum sum)
0100 {
0101     asm("addl %1, %0        ;\n"
0102         "adcl $0xffff, %0   ;\n"
0103         : "=r" (sum)
0104         : "r" ((__force u32)sum << 16),
0105           "0" ((__force u32)sum & 0xffff0000));
0106     return (__force __sum16)(~(__force u32)sum >> 16);
0107 }
0108 
0109 static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
0110                     __u32 len, __u8 proto,
0111                     __wsum sum)
0112 {
0113     asm("addl %1, %0    ;\n"
0114         "adcl %2, %0    ;\n"
0115         "adcl %3, %0    ;\n"
0116         "adcl $0, %0    ;\n"
0117         : "=r" (sum)
0118         : "g" (daddr), "g"(saddr),
0119           "g" ((len + proto) << 8), "0" (sum));
0120     return sum;
0121 }
0122 
0123 /*
0124  * computes the checksum of the TCP/UDP pseudo-header
0125  * returns a 16-bit checksum, already complemented
0126  */
0127 static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
0128                     __u32 len, __u8 proto,
0129                     __wsum sum)
0130 {
0131     return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
0132 }
0133 
0134 /*
0135  * this routine is used for miscellaneous IP-like checksums, mainly
0136  * in icmp.c
0137  */
0138 
0139 static inline __sum16 ip_compute_csum(const void *buff, int len)
0140 {
0141     return csum_fold(csum_partial(buff, len, 0));
0142 }
0143 
0144 #define _HAVE_ARCH_IPV6_CSUM
0145 static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
0146                       const struct in6_addr *daddr,
0147                       __u32 len, __u8 proto, __wsum sum)
0148 {
0149     asm("addl 0(%1), %0 ;\n"
0150         "adcl 4(%1), %0 ;\n"
0151         "adcl 8(%1), %0 ;\n"
0152         "adcl 12(%1), %0    ;\n"
0153         "adcl 0(%2), %0 ;\n"
0154         "adcl 4(%2), %0 ;\n"
0155         "adcl 8(%2), %0 ;\n"
0156         "adcl 12(%2), %0    ;\n"
0157         "adcl %3, %0    ;\n"
0158         "adcl %4, %0    ;\n"
0159         "adcl $0, %0    ;\n"
0160         : "=&r" (sum)
0161         : "r" (saddr), "r" (daddr),
0162           "r" (htonl(len)), "r" (htonl(proto)), "0" (sum)
0163         : "memory");
0164 
0165     return csum_fold(sum);
0166 }
0167 
0168 /*
0169  *  Copy and checksum to user
0170  */
0171 static inline __wsum csum_and_copy_to_user(const void *src,
0172                        void __user *dst,
0173                        int len)
0174 {
0175     __wsum ret;
0176 
0177     might_sleep();
0178     if (!user_access_begin(dst, len))
0179         return 0;
0180 
0181     ret = csum_partial_copy_generic(src, (__force void *)dst, len);
0182     user_access_end();
0183     return ret;
0184 }
0185 
0186 #endif /* _ASM_X86_CHECKSUM_32_H */