2 * Network checksum routines
4 * Copyright (C) 1999, 2003 Hewlett-Packard Co
5 * 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/module.h>
14 #include <linux/string.h>
16 #include <asm/byteorder.h>
18 static inline unsigned short
19 from64to16 (unsigned long x
)
21 /* add up 32-bit words for 33 bits */
22 x
= (x
& 0xffffffff) + (x
>> 32);
23 /* add up 16-bit and 17-bit words for 17+c bits */
24 x
= (x
& 0xffff) + (x
>> 16);
25 /* add up 16-bit and 2-bit for 16+c bit */
26 x
= (x
& 0xffff) + (x
>> 16);
28 x
= (x
& 0xffff) + (x
>> 16);
33 * computes the checksum of the TCP/UDP pseudo-header
34 * returns a 16-bit checksum, already complemented.
37 csum_tcpudp_magic (unsigned long saddr
, unsigned long daddr
, unsigned short len
,
38 unsigned short proto
, unsigned int sum
)
40 return ~from64to16(saddr
+ daddr
+ sum
+ ((unsigned long) ntohs(len
) << 16) +
41 ((unsigned long) proto
<< 8));
44 EXPORT_SYMBOL(csum_tcpudp_magic
);
47 csum_tcpudp_nofold (unsigned long saddr
, unsigned long daddr
, unsigned short len
,
48 unsigned short proto
, unsigned int sum
)
52 result
= (saddr
+ daddr
+ sum
+
53 ((unsigned long) ntohs(len
) << 16) +
54 ((unsigned long) proto
<< 8));
56 /* Fold down to 32-bits so we don't lose in the typedef-less network stack. */
58 result
= (result
& 0xffffffff) + (result
>> 32);
60 result
= (result
& 0xffffffff) + (result
>> 32);
64 extern unsigned long do_csum (const unsigned char *, long);
67 * computes the checksum of a memory block at buff, length len,
68 * and adds in "sum" (32-bit)
70 * returns a 32-bit number suitable for feeding into itself
71 * or csum_tcpudp_magic
73 * this function must be called with even lengths, except
74 * for the last fragment, which may be odd
76 * it's best to have buff aligned on a 32-bit boundary
79 csum_partial (const unsigned char * buff
, int len
, unsigned int sum
)
81 unsigned long result
= do_csum(buff
, len
);
83 /* add in old sum, and carry.. */
85 /* 32+c bits -> 32 bits */
86 result
= (result
& 0xffffffff) + (result
>> 32);
90 EXPORT_SYMBOL(csum_partial
);
93 * this routine is used for miscellaneous IP-like checksums, mainly
97 ip_compute_csum (unsigned char * buff
, int len
)
99 return ~do_csum(buff
,len
);
102 EXPORT_SYMBOL(ip_compute_csum
);