blob: 43940b7155591d2996ea3e2664f90bce82831353 [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 */
JR Riversd8a4e422012-09-13 17:17:36 +000054/* calling with offset == FLETCHER_CHECKSUM_VALIDATE will validate the checksum
55 without modifying the buffer; a valid checksum returns 0 */
Jingjing Duanefda3bb2008-08-13 19:02:03 +010056u_int16_t
Paul Jakma5d4b8cf2008-11-16 18:34:19 +000057fletcher_checksum(u_char * buffer, const size_t len, const uint16_t offset)
Jingjing Duanefda3bb2008-08-13 19:02:03 +010058{
59 u_int8_t *p;
Paul Jakma5d4b8cf2008-11-16 18:34:19 +000060 int x, y, c0, c1;
Jingjing Duanefda3bb2008-08-13 19:02:03 +010061 u_int16_t checksum;
62 u_int16_t *csum;
Paul Jakma5d4b8cf2008-11-16 18:34:19 +000063 size_t partial_len, i, left = len;
64
Jingjing Duanefda3bb2008-08-13 19:02:03 +010065 checksum = 0;
66
Paul Jakma5d4b8cf2008-11-16 18:34:19 +000067
JR Riversd8a4e422012-09-13 17:17:36 +000068 if (offset != FLETCHER_CHECKSUM_VALIDATE)
69 /* Zero the csum in the packet. */
70 {
71 assert (offset < (len - 1)); /* account for two bytes of checksum */
72 csum = (u_int16_t *) (buffer + offset);
73 *(csum) = 0;
74 }
Jingjing Duanefda3bb2008-08-13 19:02:03 +010075
76 p = buffer;
77 c0 = 0;
78 c1 = 0;
Jingjing Duanefda3bb2008-08-13 19:02:03 +010079
Paul Jakma5d4b8cf2008-11-16 18:34:19 +000080 while (left != 0)
Jingjing Duanefda3bb2008-08-13 19:02:03 +010081 {
Paul Jakma5d4b8cf2008-11-16 18:34:19 +000082 partial_len = MIN(left, MODX);
Jingjing Duanefda3bb2008-08-13 19:02:03 +010083
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
Paul Jakma5d4b8cf2008-11-16 18:34:19 +000093 left -= partial_len;
Jingjing Duanefda3bb2008-08-13 19:02:03 +010094 }
JR Riversd8a4e422012-09-13 17:17:36 +000095
Paul Jakma5d4b8cf2008-11-16 18:34:19 +000096 /* The cast is important, to ensure the mod is taken as a signed value. */
Joakim Tjernlund6e907dd2008-11-17 11:22:25 +010097 x = (int)((len - offset - 1) * c0 - c1) % 255;
Jingjing Duanefda3bb2008-08-13 19:02:03 +010098
Paul Jakma5d4b8cf2008-11-16 18:34:19 +000099 if (x <= 0)
100 x += 255;
101 y = 510 - c0 - x;
102 if (y > 255)
103 y -= 255;
Jingjing Duanefda3bb2008-08-13 19:02:03 +0100104
JR Riversd8a4e422012-09-13 17:17:36 +0000105 if (offset == FLETCHER_CHECKSUM_VALIDATE)
106 {
107 checksum = (c1 << 8) + c0;
108 }
109 else
110 {
111 /*
112 * Now we write this to the packet.
113 * We could skip this step too, since the checksum returned would
114 * be stored into the checksum field by the caller.
115 */
116 buffer[offset] = x;
117 buffer[offset + 1] = y;
118
119 /* Take care of the endian issue */
120 checksum = htons((x << 8) | (y & 0xFF));
121 }
Jingjing Duanefda3bb2008-08-13 19:02:03 +0100122
123 return checksum;
124}