ISC DHCP  4.4.1
A reference DHCPv4 and DHCPv6 implementation
ethernet.c
Go to the documentation of this file.
1 /* ethernet.c
2 
3  Packet assembly code, originally contributed by Archie Cobbs. */
4 
5 /*
6  * Copyright (c) 2004-2017 by Internet Systems Consortium, Inc. ("ISC")
7  * Copyright (c) 1996-2003 by Internet Software Consortium
8  *
9  * This Source Code Form is subject to the terms of the Mozilla Public
10  * License, v. 2.0. If a copy of the MPL was not distributed with this
11  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  *
21  * Internet Systems Consortium, Inc.
22  * 950 Charter Street
23  * Redwood City, CA 94063
24  * <info@isc.org>
25  * https://www.isc.org/
26  *
27  */
28 
29 #include "dhcpd.h"
30 
31 #if defined (PACKET_ASSEMBLY) || defined (PACKET_DECODING)
33 #endif /* PACKET_ASSEMBLY || PACKET_DECODING */
34 
35 #if defined (PACKET_ASSEMBLY)
36 /* Assemble an hardware header... */
37 
38 void assemble_ethernet_header (interface, buf, bufix, to)
39  struct interface_info *interface;
40  unsigned char *buf;
41  unsigned *bufix;
42  struct hardware *to;
43 {
44  struct isc_ether_header eh;
45 
46  if (to && to -> hlen == 7) /* XXX */
47  memcpy (eh.ether_dhost, &to -> hbuf [1],
48  sizeof eh.ether_dhost);
49  else
50  memset (eh.ether_dhost, 0xff, sizeof (eh.ether_dhost));
51  if (interface -> hw_address.hlen - 1 == sizeof (eh.ether_shost))
52  memcpy (eh.ether_shost, &interface -> hw_address.hbuf [1],
53  sizeof (eh.ether_shost));
54  else
55  memset (eh.ether_shost, 0x00, sizeof (eh.ether_shost));
56 
57  eh.ether_type = htons (ETHERTYPE_IP);
58 
59  memcpy (&buf [*bufix], &eh, ETHER_HEADER_SIZE);
60  *bufix += ETHER_HEADER_SIZE;
61 }
62 #endif /* PACKET_ASSEMBLY */
63 
64 #ifdef PACKET_DECODING
65 /* Decode a hardware header... */
66 
67 ssize_t decode_ethernet_header (interface, buf, bufix, from)
68  struct interface_info *interface;
69  unsigned char *buf;
70  unsigned bufix;
71  struct hardware *from;
72 {
73  struct isc_ether_header eh;
74 
75  memcpy (&eh, buf + bufix, ETHER_HEADER_SIZE);
76 
77 #ifdef USERLAND_FILTER
78  if (ntohs (eh.ether_type) != ETHERTYPE_IP)
79  return -1;
80 #endif
81  memcpy (&from -> hbuf [1], eh.ether_shost, sizeof (eh.ether_shost));
82  from -> hbuf [0] = ARPHRD_ETHER;
83  from -> hlen = (sizeof eh.ether_shost) + 1;
84 
85  return ETHER_HEADER_SIZE;
86 }
87 #endif /* PACKET_DECODING */
#define ETHER_HEADER_SIZE
Definition: if_ether.h:60
void assemble_ethernet_header(struct interface_info *, unsigned char *, unsigned *, struct hardware *)
#define ETHERTYPE_IP
Definition: if_ether.h:57
ssize_t decode_ethernet_header(struct interface_info *, unsigned char *, unsigned, struct hardware *)