1 /* $NetBSD: ethernet.c,v 1.1.1.2 2014/07/12 11:57:43 spz Exp $ */ 2 /* ethernet.c 3 4 Packet assembly code, originally contributed by Archie Cobbs. */ 5 6 /* 7 * Copyright (c) 2004,2007,2009,2014 by Internet Systems Consortium, Inc. ("ISC") 8 * Copyright (c) 1996-2003 by Internet Software Consortium 9 * 10 * Permission to use, copy, modify, and distribute this software for any 11 * purpose with or without fee is hereby granted, provided that the above 12 * copyright notice and this permission notice appear in all copies. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 20 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 21 * 22 * Internet Systems Consortium, Inc. 23 * 950 Charter Street 24 * Redwood City, CA 94063 25 * <info@isc.org> 26 * https://www.isc.org/ 27 * 28 */ 29 30 #include <sys/cdefs.h> 31 __RCSID("$NetBSD: ethernet.c,v 1.1.1.2 2014/07/12 11:57:43 spz Exp $"); 32 33 #include "dhcpd.h" 34 35 #if defined (PACKET_ASSEMBLY) || defined (PACKET_DECODING) 36 #include "includes/netinet/if_ether.h" 37 #endif /* PACKET_ASSEMBLY || PACKET_DECODING */ 38 39 #if defined (PACKET_ASSEMBLY) 40 /* Assemble an hardware header... */ 41 42 void assemble_ethernet_header (interface, buf, bufix, to) 43 struct interface_info *interface; 44 unsigned char *buf; 45 unsigned *bufix; 46 struct hardware *to; 47 { 48 struct isc_ether_header eh; 49 50 if (to && to -> hlen == 7) /* XXX */ 51 memcpy (eh.ether_dhost, &to -> hbuf [1], 52 sizeof eh.ether_dhost); 53 else 54 memset (eh.ether_dhost, 0xff, sizeof (eh.ether_dhost)); 55 if (interface -> hw_address.hlen - 1 == sizeof (eh.ether_shost)) 56 memcpy (eh.ether_shost, &interface -> hw_address.hbuf [1], 57 sizeof (eh.ether_shost)); 58 else 59 memset (eh.ether_shost, 0x00, sizeof (eh.ether_shost)); 60 61 eh.ether_type = htons (ETHERTYPE_IP); 62 63 memcpy (&buf [*bufix], &eh, ETHER_HEADER_SIZE); 64 *bufix += ETHER_HEADER_SIZE; 65 } 66 #endif /* PACKET_ASSEMBLY */ 67 68 #ifdef PACKET_DECODING 69 /* Decode a hardware header... */ 70 71 ssize_t decode_ethernet_header (interface, buf, bufix, from) 72 struct interface_info *interface; 73 unsigned char *buf; 74 unsigned bufix; 75 struct hardware *from; 76 { 77 struct isc_ether_header eh; 78 79 memcpy (&eh, buf + bufix, ETHER_HEADER_SIZE); 80 81 #ifdef USERLAND_FILTER 82 if (ntohs (eh.ether_type) != ETHERTYPE_IP) 83 return -1; 84 #endif 85 memcpy (&from -> hbuf [1], eh.ether_shost, sizeof (eh.ether_shost)); 86 from -> hbuf [0] = ARPHRD_ETHER; 87 from -> hlen = (sizeof eh.ether_shost) + 1; 88 89 return ETHER_HEADER_SIZE; 90 } 91 #endif /* PACKET_DECODING */ 92