]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_checksum.c
Revert "*: reindent pt. 2"
[mirror_frr.git] / tests / lib / test_checksum.c
1 /*
2 * Copyright (C) 2008 Sun Microsystems, Inc.
3 *
4 * This file is part of Quagga.
5 *
6 * Quagga is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * Quagga is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22 #include <stdlib.h>
23 #include <time.h>
24
25 #include "checksum.h"
26
27 struct thread_master *master;
28
29 struct acc_vals {
30 int c0;
31 int c1;
32 };
33
34 struct csum_vals {
35 struct acc_vals a;
36 int x;
37 int y;
38 };
39
40 static struct csum_vals ospfd_vals, isisd_vals;
41
42 typedef size_t testsz_t;
43 typedef uint16_t testoff_t;
44
45 /* Fletcher Checksum -- Refer to RFC1008. */
46 #define MODX 4102U
47
48 /* The final reduction phase.
49 * This one should be the original ospfd version
50 */
51 static u_int16_t reduce_ospfd(struct csum_vals *vals, testsz_t len,
52 testoff_t off)
53 {
54 #define x vals->x
55 #define y vals->y
56 #define c0 vals->a.c0
57 #define c1 vals->a.c1
58
59 x = ((len - off - 1) * c0 - c1) % 255;
60
61 if (x <= 0)
62 x += 255;
63 y = 510 - c0 - x;
64 if (y > 255)
65 y -= 255;
66
67 /* take care endian issue. */
68 return htons((x << 8) + y);
69 #undef x
70 #undef y
71 #undef c0
72 #undef c1
73 }
74
75 /* slightly different concatenation */
76 static u_int16_t reduce_ospfd1(struct csum_vals *vals, testsz_t len,
77 testoff_t off)
78 {
79 #define x vals->x
80 #define y vals->y
81 #define c0 vals->a.c0
82 #define c1 vals->a.c1
83
84 x = ((len - off - 1) * c0 - c1) % 255;
85 if (x <= 0)
86 x += 255;
87 y = 510 - c0 - x;
88 if (y > 255)
89 y -= 255;
90
91 /* take care endian issue. */
92 return htons((x << 8) | (y & 0xff));
93 #undef x
94 #undef y
95 #undef c0
96 #undef c1
97 }
98
99 /* original isisd version */
100 static u_int16_t reduce_isisd(struct csum_vals *vals, testsz_t len,
101 testoff_t off)
102 {
103 #define x vals->x
104 #define y vals->y
105 #define c0 vals->a.c0
106 #define c1 vals->a.c1
107 u_int32_t mul;
108
109 mul = (len - off) * (c0);
110 x = mul - c0 - c1;
111 y = c1 - mul - 1;
112
113 if (y > 0)
114 y++;
115 if (x < 0)
116 x--;
117
118 x %= 255;
119 y %= 255;
120
121 if (x == 0)
122 x = 255;
123 if (y == 0)
124 y = 1;
125
126 return htons((x << 8) | (y & 0xff));
127
128 #undef x
129 #undef y
130 #undef c0
131 #undef c1
132 }
133
134 /* Is the -1 in y wrong perhaps? */
135 static u_int16_t reduce_isisd_yfix(struct csum_vals *vals, testsz_t len,
136 testoff_t off)
137 {
138 #define x vals->x
139 #define y vals->y
140 #define c0 vals->a.c0
141 #define c1 vals->a.c1
142 u_int32_t mul;
143
144 mul = (len - off) * (c0);
145 x = mul - c0 - c1;
146 y = c1 - mul;
147
148 if (y > 0)
149 y++;
150 if (x < 0)
151 x--;
152
153 x %= 255;
154 y %= 255;
155
156 if (x == 0)
157 x = 255;
158 if (y == 0)
159 y = 1;
160
161 return htons((x << 8) | (y & 0xff));
162
163 #undef x
164 #undef y
165 #undef c0
166 #undef c1
167 }
168
169 /* Move the mods yp */
170 static u_int16_t reduce_isisd_mod(struct csum_vals *vals, testsz_t len,
171 testoff_t off)
172 {
173 #define x vals->x
174 #define y vals->y
175 #define c0 vals->a.c0
176 #define c1 vals->a.c1
177 u_int32_t mul;
178
179 mul = (len - off) * (c0);
180 x = mul - c1 - c0;
181 y = c1 - mul - 1;
182
183 x %= 255;
184 y %= 255;
185
186 if (y > 0)
187 y++;
188 if (x < 0)
189 x--;
190
191 if (x == 0)
192 x = 255;
193 if (y == 0)
194 y = 1;
195
196 return htons((x << 8) | (y & 0xff));
197
198 #undef x
199 #undef y
200 #undef c0
201 #undef c1
202 }
203
204 /* Move the mods up + fix y */
205 static u_int16_t reduce_isisd_mody(struct csum_vals *vals, testsz_t len,
206 testoff_t off)
207 {
208 #define x vals->x
209 #define y vals->y
210 #define c0 vals->a.c0
211 #define c1 vals->a.c1
212 u_int32_t mul;
213
214 mul = (len - off) * (c0);
215 x = mul - c0 - c1;
216 y = c1 - mul;
217
218 x %= 255;
219 y %= 255;
220
221 if (y > 0)
222 y++;
223 if (x < 0)
224 x--;
225
226 if (x == 0)
227 x = 255;
228 if (y == 0)
229 y = 1;
230
231 return htons((x << 8) | (y & 0xff));
232
233 #undef x
234 #undef y
235 #undef c0
236 #undef c1
237 }
238
239 struct reductions_t {
240 const char *name;
241 u_int16_t (*f)(struct csum_vals *, testsz_t, testoff_t);
242 } reducts[] = {
243 {.name = "ospfd", .f = reduce_ospfd},
244 {.name = "ospfd-1", .f = reduce_ospfd1},
245 {.name = "isisd", .f = reduce_isisd},
246 {.name = "isisd-yfix", .f = reduce_isisd_yfix},
247 {.name = "isisd-mod", .f = reduce_isisd_mod},
248 {.name = "isisd-mody", .f = reduce_isisd_mody},
249 {NULL, NULL},
250 };
251
252 /* The original ospfd checksum */
253 static u_int16_t ospfd_checksum(u_char *buffer, testsz_t len, testoff_t off)
254 {
255 u_char *sp, *ep, *p, *q;
256 int c0 = 0, c1 = 0;
257 int x, y;
258 u_int16_t checksum, *csum;
259
260 csum = (u_int16_t *)(buffer + off);
261 *(csum) = 0;
262
263 sp = buffer;
264
265 for (ep = sp + len; sp < ep; sp = q) {
266 q = sp + MODX;
267 if (q > ep)
268 q = ep;
269 for (p = sp; p < q; p++) {
270 c0 += *p;
271 c1 += c0;
272 }
273 c0 %= 255;
274 c1 %= 255;
275 }
276
277 ospfd_vals.a.c0 = c0;
278 ospfd_vals.a.c1 = c1;
279
280 // printf ("%s: len %u, off %u, c0 %d, c1 %d\n",
281 // __func__, len, off, c0, c1);
282
283 x = ((int)(len - off - 1) * (int)c0 - (int)c1) % 255;
284
285 if (x <= 0)
286 x += 255;
287 y = 510 - c0 - x;
288 if (y > 255)
289 y -= 255;
290
291 ospfd_vals.x = x;
292 ospfd_vals.y = y;
293
294 buffer[off] = x;
295 buffer[off + 1] = y;
296
297 /* take care endian issue. */
298 checksum = htons((x << 8) | (y & 0xff));
299
300 return (checksum);
301 }
302
303 /* the original, broken isisd checksum */
304 static u_int16_t iso_csum_create(u_char *buffer, testsz_t len, testoff_t off)
305 {
306
307 u_int8_t *p;
308 int x;
309 int y;
310 u_int32_t mul;
311 u_int32_t c0;
312 u_int32_t c1;
313 u_int16_t checksum, *csum;
314 int i, init_len, partial_len;
315
316 checksum = 0;
317
318 csum = (u_int16_t *)(buffer + off);
319 *(csum) = checksum;
320
321 p = buffer;
322 c0 = 0;
323 c1 = 0;
324 init_len = len;
325
326 while (len != 0) {
327 partial_len = MIN(len, MODX);
328
329 for (i = 0; i < partial_len; i++) {
330 c0 = c0 + *(p++);
331 c1 += c0;
332 }
333
334 c0 = c0 % 255;
335 c1 = c1 % 255;
336
337 len -= partial_len;
338 }
339
340 isisd_vals.a.c0 = c0;
341 isisd_vals.a.c1 = c1;
342
343 mul = (init_len - off) * c0;
344
345 x = mul - c1 - c0;
346 y = c1 - mul - 1;
347
348 if (y > 0)
349 y++;
350 if (x < 0)
351 x--;
352
353 x %= 255;
354 y %= 255;
355
356 if (x == 0)
357 x = 255;
358 if (y == 0)
359 y = 1;
360
361 isisd_vals.x = x;
362 isisd_vals.y = y;
363
364 checksum = htons((x << 8) | (y & 0xFF));
365
366 *(csum) = checksum;
367
368 /* return the checksum for user usage */
369 return checksum;
370 }
371
372 static int verify(u_char *buffer, testsz_t len)
373 {
374 u_int8_t *p;
375 u_int32_t c0;
376 u_int32_t c1;
377 int i, partial_len;
378
379 p = buffer;
380
381 c0 = 0;
382 c1 = 0;
383
384 while (len) {
385 partial_len = MIN(len, 5803U);
386
387 for (i = 0; i < partial_len; i++) {
388 c0 = c0 + *(p++);
389 c1 += c0;
390 }
391 c0 = c0 % 255;
392 c1 = c1 % 255;
393
394 len -= partial_len;
395 }
396
397 if (c0 == 0 && c1 == 0)
398 return 0;
399
400 return 1;
401 }
402
403 static int /* return checksum in low-order 16 bits */
404 in_cksum_optimized(void *parg, int nbytes)
405 {
406 u_short *ptr = parg;
407 register long sum; /* assumes long == 32 bits */
408 register u_short answer; /* assumes u_short == 16 bits */
409 register int count;
410 /*
411 * Our algorithm is simple, using a 32-bit accumulator (sum),
412 * we add sequential 16-bit words to it, and at the end, fold back
413 * all the carry bits from the top 16 bits into the lower 16 bits.
414 */
415
416 sum = 0;
417 count = nbytes >> 1; /* div by 2 */
418 for (ptr--; count; --count)
419 sum += *++ptr;
420
421 if (nbytes & 1) /* Odd */
422 sum += *(u_char *)(++ptr); /* one byte only */
423
424 /*
425 * Add back carry outs from top 16 bits to low 16 bits.
426 */
427
428 sum = (sum >> 16) + (sum & 0xffff); /* add high-16 to low-16 */
429 sum += (sum >> 16); /* add carry */
430 answer = ~sum; /* ones-complement, then truncate to 16 bits */
431 return (answer);
432 }
433
434
435 static int /* return checksum in low-order 16 bits */
436 in_cksum_rfc(void *parg, int count)
437 /* from RFC 1071 */
438 {
439 u_short *addr = parg;
440 /* Compute Internet Checksum for "count" bytes
441 * beginning at location "addr".
442 */
443 register long sum = 0;
444
445 while (count > 1) {
446 /* This is the inner loop */
447 sum += *addr++;
448 count -= 2;
449 }
450 /* Add left-over byte, if any */
451 if (count > 0) {
452 sum += *(u_char *)addr;
453 }
454
455 /* Fold 32-bit sum to 16 bits */
456 while (sum >> 16)
457 sum = (sum & 0xffff) + (sum >> 16);
458 return ~sum;
459 }
460
461
462 int main(int argc, char **argv)
463 {
464 /* 60017 65629 702179 */
465 #define MAXDATALEN 60017
466 #define BUFSIZE MAXDATALEN + sizeof(u_int16_t)
467 u_char buffer[BUFSIZE];
468 int exercise = 0;
469 #define EXERCISESTEP 257
470 srandom(time(NULL));
471
472 while (1) {
473 u_int16_t ospfd, isisd, lib, in_csum, in_csum_res, in_csum_rfc;
474 int i, j;
475
476 exercise += EXERCISESTEP;
477 exercise %= MAXDATALEN;
478
479 for (i = 0; i < exercise; i += sizeof(long int)) {
480 long int rand = random();
481
482 for (j = sizeof(long int); j > 0; j--)
483 buffer[i + (sizeof(long int) - j)] =
484 (rand >> (j * 8)) & 0xff;
485 }
486
487 in_csum = in_cksum(buffer, exercise);
488 in_csum_res = in_cksum_optimized(buffer, exercise);
489 in_csum_rfc = in_cksum_rfc(buffer, exercise);
490 if (in_csum_res != in_csum || in_csum != in_csum_rfc)
491 printf("verify: in_chksum failed in_csum:%x, in_csum_res:%x,"
492 "in_csum_rfc %x, len:%d\n",
493 in_csum, in_csum_res, in_csum_rfc, exercise);
494
495 ospfd = ospfd_checksum(buffer, exercise + sizeof(u_int16_t),
496 exercise);
497 if (verify(buffer, exercise + sizeof(u_int16_t)))
498 printf("verify: ospfd failed\n");
499 isisd = iso_csum_create(buffer, exercise + sizeof(u_int16_t),
500 exercise);
501 if (verify(buffer, exercise + sizeof(u_int16_t)))
502 printf("verify: isisd failed\n");
503 lib = fletcher_checksum(buffer, exercise + sizeof(u_int16_t),
504 exercise);
505 if (verify(buffer, exercise + sizeof(u_int16_t)))
506 printf("verify: lib failed\n");
507
508 if (ospfd != lib) {
509 printf("Mismatch in values at size %u\n"
510 "ospfd: 0x%04x\tc0: %d\tc1: %d\tx: %d\ty: %d\n"
511 "isisd: 0x%04x\tc0: %d\tc1: %d\tx: %d\ty: %d\n"
512 "lib: 0x%04x\n",
513 exercise, ospfd, ospfd_vals.a.c0,
514 ospfd_vals.a.c1, ospfd_vals.x, ospfd_vals.y,
515 isisd, isisd_vals.a.c0, isisd_vals.a.c1,
516 isisd_vals.x, isisd_vals.y, lib);
517
518 /* Investigate reduction phase discrepencies */
519 if (ospfd_vals.a.c0 == isisd_vals.a.c0
520 && ospfd_vals.a.c1 == isisd_vals.a.c1) {
521 printf("\n");
522 for (i = 0; reducts[i].name != NULL; i++) {
523 ospfd = reducts[i].f(
524 &ospfd_vals,
525 exercise + sizeof(u_int16_t),
526 exercise);
527 printf("%20s: x: %02x, y %02x, checksum 0x%04x\n",
528 reducts[i].name,
529 ospfd_vals.x & 0xff,
530 ospfd_vals.y & 0xff, ospfd);
531 }
532 }
533
534 printf("\n u_char testdata [] = {\n ");
535 for (i = 0; i < exercise; i++) {
536 printf("0x%02x,%s", buffer[i],
537 (i + 1) % 8 ? " " : "\n ");
538 }
539 printf("\n}\n");
540 exit(1);
541 }
542 }
543 }