1 /* $NetBSD: ethernet.c,v 1.1.1.2 2014/07/12 11:57:43 spz Exp $ */
4 Packet assembly code, originally contributed by Archie Cobbs. */
7 * Copyright (c) 2004,2007,2009,2014 by Internet Systems Consortium, Inc. ("ISC")
8 * Copyright (c) 1996-2003 by Internet Software Consortium
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.
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.
22 * Internet Systems Consortium, Inc.
24 * Redwood City, CA 94063
26 * https://www.isc.org/
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: ethernet.c,v 1.1.1.2 2014/07/12 11:57:43 spz Exp $");
35 #if defined (PACKET_ASSEMBLY) || defined (PACKET_DECODING)
36 #include "includes/netinet/if_ether.h"
37 #endif /* PACKET_ASSEMBLY || PACKET_DECODING */
39 #if defined (PACKET_ASSEMBLY)
40 /* Assemble an hardware header... */
42 void assemble_ethernet_header (interface
, buf
, bufix
, to
)
43 struct interface_info
*interface
;
48 struct isc_ether_header eh
;
50 if (to
&& to
-> hlen
== 7) /* XXX */
51 memcpy (eh
.ether_dhost
, &to
-> hbuf
[1],
52 sizeof eh
.ether_dhost
);
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
));
59 memset (eh
.ether_shost
, 0x00, sizeof (eh
.ether_shost
));
61 eh
.ether_type
= htons (ETHERTYPE_IP
);
63 memcpy (&buf
[*bufix
], &eh
, ETHER_HEADER_SIZE
);
64 *bufix
+= ETHER_HEADER_SIZE
;
66 #endif /* PACKET_ASSEMBLY */
68 #ifdef PACKET_DECODING
69 /* Decode a hardware header... */
71 ssize_t
decode_ethernet_header (interface
, buf
, bufix
, from
)
72 struct interface_info
*interface
;
75 struct hardware
*from
;
77 struct isc_ether_header eh
;
79 memcpy (&eh
, buf
+ bufix
, ETHER_HEADER_SIZE
);
81 #ifdef USERLAND_FILTER
82 if (ntohs (eh
.ether_type
) != ETHERTYPE_IP
)
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;
89 return ETHER_HEADER_SIZE
;
91 #endif /* PACKET_DECODING */