Linux Audio

Check our new training course

Loading...
v6.13.7
  1/* SPDX-License-Identifier: GPL-2.0 */
  2#ifndef __UM_CHECKSUM_H
  3#define __UM_CHECKSUM_H
  4
  5#include <linux/string.h>
  6#include <linux/in6.h>
  7#include <linux/uaccess.h>
  8
  9/*
 10 * computes the checksum of a memory block at buff, length len,
 11 * and adds in "sum" (32-bit)
 12 *
 13 * returns a 32-bit number suitable for feeding into itself
 14 * or csum_tcpudp_magic
 15 *
 16 * this function must be called with even lengths, except
 17 * for the last fragment, which may be odd
 18 *
 19 * it's best to have buff aligned on a 32-bit boundary
 20 */
 21extern __wsum csum_partial(const void *buff, int len, __wsum sum);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 22
 23/**
 24 * csum_fold - Fold and invert a 32bit checksum.
 25 * sum: 32bit unfolded sum
 26 *
 27 * Fold a 32bit running checksum to 16bit and invert it. This is usually
 28 * the last step before putting a checksum into a packet.
 29 * Make sure not to mix with 64bit checksums.
 30 */
 31static inline __sum16 csum_fold(__wsum sum)
 32{
 33	__asm__(
 34		"  addl %1,%0\n"
 35		"  adcl $0xffff,%0"
 36		: "=r" (sum)
 37		: "r" ((__force u32)sum << 16),
 38		  "0" ((__force u32)sum & 0xffff0000)
 39	);
 40	return (__force __sum16)(~(__force u32)sum >> 16);
 41}
 42
 43/**
 44 * csum_tcpup_nofold - Compute an IPv4 pseudo header checksum.
 45 * @saddr: source address
 46 * @daddr: destination address
 47 * @len: length of packet
 48 * @proto: ip protocol of packet
 49 * @sum: initial sum to be added in (32bit unfolded)
 50 *
 51 * Returns the pseudo header checksum the input data. Result is
 52 * 32bit unfolded.
 53 */
 54static inline __wsum
 55csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
 56		  __u8 proto, __wsum sum)
 57{
 58	asm("  addl %1, %0\n"
 59	    "  adcl %2, %0\n"
 60	    "  adcl %3, %0\n"
 61	    "  adcl $0, %0\n"
 62		: "=r" (sum)
 63	    : "g" (daddr), "g" (saddr), "g" ((len + proto) << 8), "0" (sum));
 64	return sum;
 65}
 66
 67/*
 68 * computes the checksum of the TCP/UDP pseudo-header
 69 * returns a 16-bit checksum, already complemented
 70 */
 71static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
 72					__u32 len, __u8 proto,
 73					__wsum sum)
 74{
 75	return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
 76}
 77
 78/**
 79 * ip_fast_csum - Compute the IPv4 header checksum efficiently.
 80 * iph: ipv4 header
 81 * ihl: length of header / 4
 82 */
 83static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
 84{
 85	unsigned int sum;
 86
 87	asm(	"  movl (%1), %0\n"
 88		"  subl $4, %2\n"
 89		"  jbe 2f\n"
 90		"  addl 4(%1), %0\n"
 91		"  adcl 8(%1), %0\n"
 92		"  adcl 12(%1), %0\n"
 93		"1: adcl 16(%1), %0\n"
 94		"  lea 4(%1), %1\n"
 95		"  decl %2\n"
 96		"  jne	1b\n"
 97		"  adcl $0, %0\n"
 98		"  movl %0, %2\n"
 99		"  shrl $16, %0\n"
100		"  addw %w2, %w0\n"
101		"  adcl $0, %0\n"
102		"  notl %0\n"
103		"2:"
104	/* Since the input registers which are loaded with iph and ipl
105	   are modified, we must also specify them as outputs, or gcc
106	   will assume they contain their original values. */
107	: "=r" (sum), "=r" (iph), "=r" (ihl)
108	: "1" (iph), "2" (ihl)
109	: "memory");
110	return (__force __sum16)sum;
111}
112
113#ifdef CONFIG_X86_32
114# include "checksum_32.h"
115#else
116# include "checksum_64.h"
117#endif
118
119#endif
v4.6
 
  1#ifndef __UM_CHECKSUM_H
  2#define __UM_CHECKSUM_H
  3
  4#include <linux/string.h>
  5#include <linux/in6.h>
  6#include <linux/uaccess.h>
  7
  8/*
  9 * computes the checksum of a memory block at buff, length len,
 10 * and adds in "sum" (32-bit)
 11 *
 12 * returns a 32-bit number suitable for feeding into itself
 13 * or csum_tcpudp_magic
 14 *
 15 * this function must be called with even lengths, except
 16 * for the last fragment, which may be odd
 17 *
 18 * it's best to have buff aligned on a 32-bit boundary
 19 */
 20extern __wsum csum_partial(const void *buff, int len, __wsum sum);
 21
 22/*
 23 *	Note: when you get a NULL pointer exception here this means someone
 24 *	passed in an incorrect kernel address to one of these functions.
 25 *
 26 *	If you use these functions directly please don't forget the
 27 *	access_ok().
 28 */
 29
 30static __inline__
 31__wsum csum_partial_copy_nocheck(const void *src, void *dst,
 32				       int len, __wsum sum)
 33{
 34	memcpy(dst, src, len);
 35	return csum_partial(dst, len, sum);
 36}
 37
 38/*
 39 * the same as csum_partial, but copies from src while it
 40 * checksums, and handles user-space pointer exceptions correctly, when needed.
 41 *
 42 * here even more important to align src and dst on a 32-bit (or even
 43 * better 64-bit) boundary
 44 */
 45
 46static __inline__
 47__wsum csum_partial_copy_from_user(const void __user *src, void *dst,
 48					 int len, __wsum sum, int *err_ptr)
 49{
 50	if (copy_from_user(dst, src, len)) {
 51		*err_ptr = -EFAULT;
 52		return (__force __wsum)-1;
 53	}
 54
 55	return csum_partial(dst, len, sum);
 56}
 57
 58/**
 59 * csum_fold - Fold and invert a 32bit checksum.
 60 * sum: 32bit unfolded sum
 61 *
 62 * Fold a 32bit running checksum to 16bit and invert it. This is usually
 63 * the last step before putting a checksum into a packet.
 64 * Make sure not to mix with 64bit checksums.
 65 */
 66static inline __sum16 csum_fold(__wsum sum)
 67{
 68	__asm__(
 69		"  addl %1,%0\n"
 70		"  adcl $0xffff,%0"
 71		: "=r" (sum)
 72		: "r" ((__force u32)sum << 16),
 73		  "0" ((__force u32)sum & 0xffff0000)
 74	);
 75	return (__force __sum16)(~(__force u32)sum >> 16);
 76}
 77
 78/**
 79 * csum_tcpup_nofold - Compute an IPv4 pseudo header checksum.
 80 * @saddr: source address
 81 * @daddr: destination address
 82 * @len: length of packet
 83 * @proto: ip protocol of packet
 84 * @sum: initial sum to be added in (32bit unfolded)
 85 *
 86 * Returns the pseudo header checksum the input data. Result is
 87 * 32bit unfolded.
 88 */
 89static inline __wsum
 90csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
 91		  __u8 proto, __wsum sum)
 92{
 93	asm("  addl %1, %0\n"
 94	    "  adcl %2, %0\n"
 95	    "  adcl %3, %0\n"
 96	    "  adcl $0, %0\n"
 97		: "=r" (sum)
 98	    : "g" (daddr), "g" (saddr), "g" ((len + proto) << 8), "0" (sum));
 99	return sum;
100}
101
102/*
103 * computes the checksum of the TCP/UDP pseudo-header
104 * returns a 16-bit checksum, already complemented
105 */
106static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
107					__u32 len, __u8 proto,
108					__wsum sum)
109{
110	return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
111}
112
113/**
114 * ip_fast_csum - Compute the IPv4 header checksum efficiently.
115 * iph: ipv4 header
116 * ihl: length of header / 4
117 */
118static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
119{
120	unsigned int sum;
121
122	asm(	"  movl (%1), %0\n"
123		"  subl $4, %2\n"
124		"  jbe 2f\n"
125		"  addl 4(%1), %0\n"
126		"  adcl 8(%1), %0\n"
127		"  adcl 12(%1), %0\n"
128		"1: adcl 16(%1), %0\n"
129		"  lea 4(%1), %1\n"
130		"  decl %2\n"
131		"  jne	1b\n"
132		"  adcl $0, %0\n"
133		"  movl %0, %2\n"
134		"  shrl $16, %0\n"
135		"  addw %w2, %w0\n"
136		"  adcl $0, %0\n"
137		"  notl %0\n"
138		"2:"
139	/* Since the input registers which are loaded with iph and ipl
140	   are modified, we must also specify them as outputs, or gcc
141	   will assume they contain their original values. */
142	: "=r" (sum), "=r" (iph), "=r" (ihl)
143	: "1" (iph), "2" (ihl)
144	: "memory");
145	return (__force __sum16)sum;
146}
147
148#ifdef CONFIG_X86_32
149# include "checksum_32.h"
150#else
151# include "checksum_64.h"
152#endif
153
154#endif