1 /*- 2 * Copyright (c) 1982, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 * 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 $ 31 */ 32 33 #include <stdio.h> 34 #include <sys/types.h> 35 #include <net/ethernet.h> 36 37 #if 0 38 /* 39 * This is for reference. We have a table-driven version 40 * of the little-endian crc32 generator, which is faster 41 * than the double-loop. 42 */ 43 uint32_t 44 ether_crc32_le(const uint8_t *buf, size_t len) 45 { 46 size_t i; 47 uint32_t crc; 48 int bit; 49 uint8_t data; 50 51 crc = 0xffffffff; /* initial value */ 52 53 for (i = 0; i < len; i++) { 54 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) 55 carry = (crc ^ data) & 1; 56 crc >>= 1; 57 if (carry) 58 crc = (crc ^ ETHER_CRC_POLY_LE); 59 } 60 61 return (crc); 62 } 63 #else 64 uint32_t 65 ether_crc32_le(const uint8_t *buf, size_t len) 66 { 67 static const uint32_t crctab[] = { 68 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 69 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 70 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 71 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 72 }; 73 size_t i; 74 uint32_t crc; 75 76 crc = 0xffffffff; /* initial value */ 77 78 for (i = 0; i < len; i++) { 79 crc ^= buf[i]; 80 crc = (crc >> 4) ^ crctab[crc & 0xf]; 81 crc = (crc >> 4) ^ crctab[crc & 0xf]; 82 } 83 84 return (crc); 85 } 86 #endif 87 88 uint32_t 89 ether_crc32_be(const uint8_t *buf, size_t len) 90 { 91 size_t i; 92 uint32_t crc, carry; 93 int bit; 94 uint8_t data; 95 96 crc = 0xffffffff; /* initial value */ 97 98 for (i = 0; i < len; i++) { 99 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) { 100 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01); 101 crc <<= 1; 102 if (carry) 103 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 104 } 105 } 106 107 return (crc); 108 } 109 110 111 char * 112 ether_sprintf(const u_char *ap) 113 { 114 static char etherbuf[18]; 115 snprintf(etherbuf, sizeof (etherbuf), 116 "%02x:%02x:%02x:%02x:%02x:%02x", 117 (unsigned)ap[0], (unsigned)ap[1], (unsigned)ap[2], 118 (unsigned)ap[3], (unsigned)ap[4], (unsigned)ap[5]); 119 return (etherbuf); 120 } 121