2 * Copyright (c) 1982, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
30 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.193.2.12 2006/08/28 02:54:14 thompsa Exp $
33 #include <sys/types.h>
34 #include <net/ethernet.h>
38 * This is for reference. We have a table-driven version
39 * of the little-endian crc32 generator, which is faster
40 * than the double-loop.
43 ether_crc32_le(const uint8_t *buf
, size_t len
)
50 crc
= 0xffffffff; /* initial value */
52 for (i
= 0; i
< len
; i
++) {
53 for (data
= *buf
++, bit
= 0; bit
< 8; bit
++, data
>>= 1)
54 carry
= (crc
^ data
) & 1;
57 crc
= (crc
^ ETHER_CRC_POLY_LE
);
64 ether_crc32_le(const uint8_t *buf
, size_t len
)
66 static const uint32_t crctab
[] = {
67 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
68 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
69 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
70 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
75 crc
= 0xffffffff; /* initial value */
77 for (i
= 0; i
< len
; i
++) {
79 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
80 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
88 ether_crc32_be(const uint8_t *buf
, size_t len
)
95 crc
= 0xffffffff; /* initial value */
97 for (i
= 0; i
< len
; i
++) {
98 for (data
= *buf
++, bit
= 0; bit
< 8; bit
++, data
>>= 1) {
99 carry
= ((crc
& 0x80000000) ? 1 : 0) ^ (data
& 0x01);
102 crc
= (crc
^ ETHER_CRC_POLY_BE
) | carry
;
110 ether_sprintf(const u_char
*ap
)
112 static char etherbuf
[18];
113 snprintf(etherbuf
, sizeof (etherbuf
),
114 "%02x:%02x:%02x:%02x:%02x:%02x",
115 (unsigned)ap
[0], (unsigned)ap
[1], (unsigned)ap
[2],
116 (unsigned)ap
[3], (unsigned)ap
[4], (unsigned)ap
[5]);