1 /*
2 * Network checksum routines
3 *
4 * Copyright (C) 1999 Hewlett-Packard Co
5 * Copyright (C) 1999 Stephane Eranian <eranian@hpl.hp.com>
6 *
7 * Most of the code coming from arch/alpha/lib/checksum.c
8 *
9 * This file contains network checksum routines that are better done
10 * in an architecture-specific manner due to speed..
11 */
12
13 #include <linux/string.h>
14
15 #include <asm/byteorder.h>
16
17 static inline unsigned short
from64to16(unsigned long x)18 from64to16(unsigned long x)
19 {
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);
26 /* add up carry.. */
27 x = (x & 0xffff) + (x >> 16);
28 return x;
29 }
30
31 /*
32 * computes the checksum of the TCP/UDP pseudo-header
33 * returns a 16-bit checksum, already complemented.
34 */
csum_tcpudp_magic(unsigned long saddr,unsigned long daddr,unsigned short len,unsigned short proto,unsigned int sum)35 unsigned short int csum_tcpudp_magic(unsigned long saddr,
36 unsigned long daddr,
37 unsigned short len,
38 unsigned short proto,
39 unsigned int sum)
40 {
41 return ~from64to16(saddr + daddr + sum +
42 ((unsigned long) ntohs(len) << 16) +
43 ((unsigned long) proto << 8));
44 }
45
csum_tcpudp_nofold(unsigned long saddr,unsigned long daddr,unsigned short len,unsigned short proto,unsigned int sum)46 unsigned int csum_tcpudp_nofold(unsigned long saddr,
47 unsigned long daddr,
48 unsigned short len,
49 unsigned short proto,
50 unsigned int sum)
51 {
52 unsigned long result;
53
54 result = (saddr + daddr + sum +
55 ((unsigned long) ntohs(len) << 16) +
56 ((unsigned long) proto << 8));
57
58 /* Fold down to 32-bits so we don't loose in the typedef-less network stack. */
59 /* 64 to 33 */
60 result = (result & 0xffffffff) + (result >> 32);
61 /* 33 to 32 */
62 result = (result & 0xffffffff) + (result >> 32);
63 return result;
64 }
65
66 extern unsigned long do_csum (const unsigned char *, long);
67
68 /*
69 * computes the checksum of a memory block at buff, length len,
70 * and adds in "sum" (32-bit)
71 *
72 * returns a 32-bit number suitable for feeding into itself
73 * or csum_tcpudp_magic
74 *
75 * this function must be called with even lengths, except
76 * for the last fragment, which may be odd
77 *
78 * it's best to have buff aligned on a 32-bit boundary
79 */
csum_partial(const unsigned char * buff,int len,unsigned int sum)80 unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
81 {
82 unsigned long result = do_csum(buff, len);
83
84 /* add in old sum, and carry.. */
85 result += sum;
86 /* 32+c bits -> 32 bits */
87 result = (result & 0xffffffff) + (result >> 32);
88 return result;
89 }
90
91
92 /*
93 * this routine is used for miscellaneous IP-like checksums, mainly
94 * in icmp.c
95 */
ip_compute_csum(unsigned char * buff,int len)96 unsigned short ip_compute_csum(unsigned char * buff, int len)
97 {
98 return ~do_csum(buff,len);
99 }
100