2 * Network checksum routines
4 * Copyright (C) 1999 Hewlett-Packard Co
5 * Copyright (C) 1999 Stephane Eranian <eranian@hpl.hp.com>
7 * Most of the code coming from arch/alpha/lib/checksum.c
9 * This file contains network checksum routines that are better done
10 * in an architecture-specific manner due to speed..
13 #include <linux/string.h>
15 #include <asm/byteorder.h>
17 static inline unsigned short
18 from64to16(unsigned long x)
20 /* add up 32-bit words for 33 bits */
21 x = (x & 0xffffffff) + (x >> 32);
22 /* add up 16-bit and 17-bit words for 17+c bits */
23 x = (x & 0xffff) + (x >> 16);
24 /* add up 16-bit and 2-bit for 16+c bit */
25 x = (x & 0xffff) + (x >> 16);
27 x = (x & 0xffff) + (x >> 16);
32 * computes the checksum of the TCP/UDP pseudo-header
33 * returns a 16-bit checksum, already complemented.
35 unsigned short int csum_tcpudp_magic(unsigned long saddr,
41 return ~from64to16(saddr + daddr + sum +
42 ((unsigned long) ntohs(len) << 16) +
43 ((unsigned long) proto << 8));
46 unsigned int csum_tcpudp_nofold(unsigned long saddr,
54 result = (saddr + daddr + sum +
55 ((unsigned long) ntohs(len) << 16) +
56 ((unsigned long) proto << 8));
58 /* Fold down to 32-bits so we don't loose in the typedef-less network stack. */
60 result = (result & 0xffffffff) + (result >> 32);
62 result = (result & 0xffffffff) + (result >> 32);
66 extern unsigned long do_csum (const unsigned char *, long);
69 * computes the checksum of a memory block at buff, length len,
70 * and adds in "sum" (32-bit)
72 * returns a 32-bit number suitable for feeding into itself
73 * or csum_tcpudp_magic
75 * this function must be called with even lengths, except
76 * for the last fragment, which may be odd
78 * it's best to have buff aligned on a 32-bit boundary
80 unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
82 unsigned long result = do_csum(buff, len);
84 /* add in old sum, and carry.. */
86 /* 32+c bits -> 32 bits */
87 result = (result & 0xffffffff) + (result >> 32);
93 * this routine is used for miscellaneous IP-like checksums, mainly
96 unsigned short ip_compute_csum(unsigned char * buff, int len)
98 return ~do_csum(buff,len);