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 <sys/mbuf.h> 36 #include <net/ethernet.h> 37 #include <net/if_vlan_var.h> 38 #include <netinet/in.h> 39 40 #if 0 41 /* 42 * This is for reference. We have a table-driven version 43 * of the little-endian crc32 generator, which is faster 44 * than the double-loop. 45 */ 46 uint32_t 47 ether_crc32_le(const uint8_t *buf, size_t len) 48 { 49 size_t i; 50 uint32_t crc; 51 int bit; 52 uint8_t data; 53 54 crc = 0xffffffff; /* initial value */ 55 56 for (i = 0; i < len; i++) { 57 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) 58 carry = (crc ^ data) & 1; 59 crc >>= 1; 60 if (carry) 61 crc = (crc ^ ETHER_CRC_POLY_LE); 62 } 63 64 return (crc); 65 } 66 #else 67 uint32_t 68 ether_crc32_le(const uint8_t *buf, size_t len) 69 { 70 static const uint32_t crctab[] = { 71 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 72 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 73 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 74 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 75 }; 76 size_t i; 77 uint32_t crc; 78 79 crc = 0xffffffff; /* initial value */ 80 81 for (i = 0; i < len; i++) { 82 crc ^= buf[i]; 83 crc = (crc >> 4) ^ crctab[crc & 0xf]; 84 crc = (crc >> 4) ^ crctab[crc & 0xf]; 85 } 86 87 return (crc); 88 } 89 #endif 90 91 uint32_t 92 ether_crc32_be(const uint8_t *buf, size_t len) 93 { 94 size_t i; 95 uint32_t crc, carry; 96 int bit; 97 uint8_t data; 98 99 crc = 0xffffffff; /* initial value */ 100 101 for (i = 0; i < len; i++) { 102 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) { 103 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01); 104 crc <<= 1; 105 if (carry) 106 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 107 } 108 } 109 110 return (crc); 111 } 112 113 char * 114 ether_sprintf(const u_char *ap) 115 { 116 static char etherbuf[18]; 117 snprintf(etherbuf, sizeof (etherbuf), 118 "%02x:%02x:%02x:%02x:%02x:%02x", 119 (unsigned)ap[0], (unsigned)ap[1], (unsigned)ap[2], 120 (unsigned)ap[3], (unsigned)ap[4], (unsigned)ap[5]); 121 return (etherbuf); 122 } 123 124 struct mbuf * 125 ether_vlanencap(struct mbuf *m, uint16_t tag) 126 { 127 struct ether_vlan_header *evl; 128 129 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT); 130 if (m == NULL) 131 return (NULL); 132 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 133 134 if (m->m_len < sizeof(*evl)) { 135 m = m_pullup(m, sizeof(*evl)); 136 if (m == NULL) 137 return (NULL); 138 } 139 140 /* 141 * Transform the Ethernet header into an Ethernet header 142 * with 802.1Q encapsulation. 143 */ 144 evl = mtod(m, struct ether_vlan_header *); 145 bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN, 146 (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN); 147 evl->evl_encap_proto = htons(ETHERTYPE_VLAN); 148 evl->evl_tag = htons(tag); 149 return (m); 150 } 151