blob: 88ec72a836de5ce0631b952e506f6a9617855366 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Checksum routine for Internet Protocol family headers (C Version).
3 *
4 * Refer to "Computing the Internet Checksum" by R. Braden, D. Borman and
5 * C. Partridge, Computer Communication Review, Vol. 19, No. 2, April 1989,
6 * pp. 86-101, for additional details on computing this checksum.
7 */
8
9#include <zebra.h>
paul34204aa2005-11-03 09:00:23 +000010#include "checksum.h"
paul718e3742002-12-13 20:15:29 +000011
paul34204aa2005-11-03 09:00:23 +000012int /* return checksum in low-order 16 bits */
13in_cksum(void *parg, int nbytes)
paul718e3742002-12-13 20:15:29 +000014{
paul34204aa2005-11-03 09:00:23 +000015 u_short *ptr = parg;
paul718e3742002-12-13 20:15:29 +000016 register long sum; /* assumes long == 32 bits */
17 u_short oddbyte;
18 register u_short answer; /* assumes u_short == 16 bits */
19
20 /*
21 * Our algorithm is simple, using a 32-bit accumulator (sum),
22 * we add sequential 16-bit words to it, and at the end, fold back
23 * all the carry bits from the top 16 bits into the lower 16 bits.
24 */
25
26 sum = 0;
27 while (nbytes > 1) {
28 sum += *ptr++;
29 nbytes -= 2;
30 }
31
32 /* mop up an odd byte, if necessary */
33 if (nbytes == 1) {
34 oddbyte = 0; /* make sure top half is zero */
35 *((u_char *) &oddbyte) = *(u_char *)ptr; /* one byte only */
36 sum += oddbyte;
37 }
38
39 /*
40 * Add back carry outs from top 16 bits to low 16 bits.
41 */
42
43 sum = (sum >> 16) + (sum & 0xffff); /* add high-16 to low-16 */
44 sum += (sum >> 16); /* add carry */
45 answer = ~sum; /* ones-complement, then truncate to 16 bits */
46 return(answer);
47}
Jingjing Duanefda3bb2008-08-13 19:02:03 +010048
49/* Fletcher Checksum -- Refer to RFC1008. */
50#define MODX 4102 /* 5802 should be fine */
51
52/* To be consistent, offset is 0-based index, rather than the 1-based
53 index required in the specification ISO 8473, Annex C.1 */
54u_int16_t
55fletcher_checksum(u_char * buffer, int len, u_int16_t offset)
56{
57 u_int8_t *p;
58 int x;
59 int y;
60 u_int32_t mul;
61 u_int32_t c0;
62 u_int32_t c1;
63 u_int16_t checksum;
64 u_int16_t *csum;
65 int i, init_len, partial_len;
66
67 checksum = 0;
68
69 /*
70 * Zero the csum in the packet.
71 */
72 csum = (u_int16_t *) (buffer + offset);
73 *(csum) = checksum;
74
75 p = buffer;
76 c0 = 0;
77 c1 = 0;
78 init_len = len;
79
80 while (len != 0)
81 {
82 partial_len = MIN(len, MODX);
83
84 for (i = 0; i < partial_len; i++)
85 {
86 c0 = c0 + *(p++);
87 c1 += c0;
88 }
89
90 c0 = c0 % 255;
91 c1 = c1 % 255;
92
93 len -= partial_len;
94 }
95
96 mul = (init_len - offset)*(c0);
97
98 x = mul - c0 - c1;
99 y = c1 - mul - 1;
100
101 if (y > 0)
102 y++;
103 if (x < 0)
104 x--;
105
106 x %= 255;
107 y %= 255;
108
109 if (x == 0)
110 x = 255;
111 if (y == 0)
112 y = 1;
113
114 /*
115 * Now we write this to the packet.
116 * We could skip this step too, since the checksum returned would
117 * be stored into the checksum field by the caller.
118 */
119 buffer[offset] = x;
120 buffer[offset + 1] = y;
121
122 /* Take care of the endian issue */
123 checksum = htons((x << 8) | (y & 0xFF));
124
125 return checksum;
126}