File Index
Files and directories
Sign in
FILE

checksum.h

File preview with syntax highlighting.

linux-6.19.14/arch/x86/um/asm/checksum.h C
  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/* Do not call this directly. Declared for export type visibility. */
 24extern __visible __wsum csum_partial_copy_generic(const void *src, void *dst, int len);
 25
 26/**
 27 * csum_fold - Fold and invert a 32bit checksum.
 28 * sum: 32bit unfolded sum
 29 *
 30 * Fold a 32bit running checksum to 16bit and invert it. This is usually
 31 * the last step before putting a checksum into a packet.
 32 * Make sure not to mix with 64bit checksums.
 33 */
 34static inline __sum16 csum_fold(__wsum sum)
 35{
 36	__asm__(
 37		"  addl %1,%0\n"
 38		"  adcl $0xffff,%0"
 39		: "=r" (sum)
 40		: "r" ((__force u32)sum << 16),
 41		  "0" ((__force u32)sum & 0xffff0000)
 42	);
 43	return (__force __sum16)(~(__force u32)sum >> 16);
 44}
 45
 46/**
 47 * csum_tcpup_nofold - Compute an IPv4 pseudo header checksum.
 48 * @saddr: source address
 49 * @daddr: destination address
 50 * @len: length of packet
 51 * @proto: ip protocol of packet
 52 * @sum: initial sum to be added in (32bit unfolded)
 53 *
 54 * Returns the pseudo header checksum the input data. Result is
 55 * 32bit unfolded.
 56 */
 57static inline __wsum
 58csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
 59		  __u8 proto, __wsum sum)
 60{
 61	asm("  addl %1, %0\n"
 62	    "  adcl %2, %0\n"
 63	    "  adcl %3, %0\n"
 64	    "  adcl $0, %0\n"
 65		: "=r" (sum)
 66	    : "g" (daddr), "g" (saddr), "g" ((len + proto) << 8), "0" (sum));
 67	return sum;
 68}
 69
 70/*
 71 * computes the checksum of the TCP/UDP pseudo-header
 72 * returns a 16-bit checksum, already complemented
 73 */
 74static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
 75					__u32 len, __u8 proto,
 76					__wsum sum)
 77{
 78	return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
 79}
 80
 81/**
 82 * ip_fast_csum - Compute the IPv4 header checksum efficiently.
 83 * iph: ipv4 header
 84 * ihl: length of header / 4
 85 */
 86static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
 87{
 88	unsigned int sum;
 89
 90	asm(	"  movl (%1), %0\n"
 91		"  subl $4, %2\n"
 92		"  jbe 2f\n"
 93		"  addl 4(%1), %0\n"
 94		"  adcl 8(%1), %0\n"
 95		"  adcl 12(%1), %0\n"
 96		"1: adcl 16(%1), %0\n"
 97		"  lea 4(%1), %1\n"
 98		"  decl %2\n"
 99		"  jne	1b\n"
100		"  adcl $0, %0\n"
101		"  movl %0, %2\n"
102		"  shrl $16, %0\n"
103		"  addw %w2, %w0\n"
104		"  adcl $0, %0\n"
105		"  notl %0\n"
106		"2:"
107	/* Since the input registers which are loaded with iph and ipl
108	   are modified, we must also specify them as outputs, or gcc
109	   will assume they contain their original values. */
110	: "=r" (sum), "=r" (iph), "=r" (ihl)
111	: "1" (iph), "2" (ihl)
112	: "memory");
113	return (__force __sum16)sum;
114}
115
116#ifdef CONFIG_X86_32
117# include "checksum_32.h"
118#else
119# include "checksum_64.h"
120#endif
121
122#endif
~/ Read-only file preview.