]> git.proxmox.com Git - mirror_frr.git/blob - isisd/iso_checksum.c
Merge pull request #12248 from pguibert6WIND/bgpasdot
[mirror_frr.git] / isisd / iso_checksum.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * IS-IS Rout(e)ing protocol - iso_checksum.c
4 * ISO checksum related routines
5 *
6 * Copyright (C) 2001,2002 Sampo Saaristo
7 * Tampere University of Technology
8 * Institute of Communications Engineering
9 */
10
11 #include <zebra.h>
12 #include "iso_checksum.h"
13 #include "checksum.h"
14
15 /*
16 * Calculations of the OSI checksum.
17 * ISO/IEC 8473 defines the sum as
18 *
19 * L
20 * sum a (mod 255) = 0
21 * 1 i
22 *
23 * L
24 * sum (L-i+1)a (mod 255) = 0
25 * 1 i
26 *
27 */
28
29 /*
30 * Verifies that the checksum is correct.
31 * Return 0 on correct and 1 on invalid checksum.
32 * Based on Annex C.4 of ISO/IEC 8473
33 */
34
35 int iso_csum_verify(uint8_t *buffer, int len, uint16_t csum, int offset)
36 {
37 uint16_t checksum;
38 uint32_t c0;
39 uint32_t c1;
40
41 c0 = csum & 0xff00;
42 c1 = csum & 0x00ff;
43
44 /*
45 * If both are zero return correct
46 */
47 if (c0 == 0 && c1 == 0)
48 return 0;
49
50 /*
51 * If either, but not both are zero return incorrect
52 */
53 if (c0 == 0 || c1 == 0)
54 return 1;
55
56 checksum = fletcher_checksum(buffer, len, offset);
57 if (checksum == htons(csum))
58 return 0;
59 return 1;
60 }