1*f407d929Schristos /* $NetBSD: ethernet.c,v 1.3 2022/04/03 01:10:58 christos Exp $ */
26fb29d29Schristos
36fb29d29Schristos /* ethernet.c
46fb29d29Schristos
56fb29d29Schristos Packet assembly code, originally contributed by Archie Cobbs. */
66fb29d29Schristos
76fb29d29Schristos /*
8*f407d929Schristos * Copyright (C) 2004-2022 Internet Systems Consortium, Inc. ("ISC")
96fb29d29Schristos * Copyright (c) 1996-2003 by Internet Software Consortium
106fb29d29Schristos *
116fb29d29Schristos * This Source Code Form is subject to the terms of the Mozilla Public
126fb29d29Schristos * License, v. 2.0. If a copy of the MPL was not distributed with this
136fb29d29Schristos * file, You can obtain one at http://mozilla.org/MPL/2.0/.
146fb29d29Schristos *
156fb29d29Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
166fb29d29Schristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
176fb29d29Schristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
186fb29d29Schristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
196fb29d29Schristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
206fb29d29Schristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
216fb29d29Schristos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
226fb29d29Schristos *
236fb29d29Schristos * Internet Systems Consortium, Inc.
24*f407d929Schristos * PO Box 360
25*f407d929Schristos * Newmarket, NH 03857 USA
266fb29d29Schristos * <info@isc.org>
276fb29d29Schristos * https://www.isc.org/
286fb29d29Schristos *
296fb29d29Schristos */
306fb29d29Schristos
316fb29d29Schristos #include <sys/cdefs.h>
32*f407d929Schristos __RCSID("$NetBSD: ethernet.c,v 1.3 2022/04/03 01:10:58 christos Exp $");
336fb29d29Schristos
346fb29d29Schristos #include "dhcpd.h"
356fb29d29Schristos
366fb29d29Schristos #if defined (PACKET_ASSEMBLY) || defined (PACKET_DECODING)
376fb29d29Schristos #include "includes/netinet/if_ether.h"
386fb29d29Schristos #endif /* PACKET_ASSEMBLY || PACKET_DECODING */
396fb29d29Schristos
406fb29d29Schristos #if defined (PACKET_ASSEMBLY)
416fb29d29Schristos /* Assemble an hardware header... */
426fb29d29Schristos
assemble_ethernet_header(interface,buf,bufix,to)436fb29d29Schristos void assemble_ethernet_header (interface, buf, bufix, to)
446fb29d29Schristos struct interface_info *interface;
456fb29d29Schristos unsigned char *buf;
466fb29d29Schristos unsigned *bufix;
476fb29d29Schristos struct hardware *to;
486fb29d29Schristos {
496fb29d29Schristos struct isc_ether_header eh;
506fb29d29Schristos
516fb29d29Schristos if (to && to -> hlen == 7) /* XXX */
526fb29d29Schristos memcpy (eh.ether_dhost, &to -> hbuf [1],
536fb29d29Schristos sizeof eh.ether_dhost);
546fb29d29Schristos else
556fb29d29Schristos memset (eh.ether_dhost, 0xff, sizeof (eh.ether_dhost));
566fb29d29Schristos if (interface -> hw_address.hlen - 1 == sizeof (eh.ether_shost))
576fb29d29Schristos memcpy (eh.ether_shost, &interface -> hw_address.hbuf [1],
586fb29d29Schristos sizeof (eh.ether_shost));
596fb29d29Schristos else
606fb29d29Schristos memset (eh.ether_shost, 0x00, sizeof (eh.ether_shost));
616fb29d29Schristos
626fb29d29Schristos eh.ether_type = htons (ETHERTYPE_IP);
636fb29d29Schristos
646fb29d29Schristos memcpy (&buf [*bufix], &eh, ETHER_HEADER_SIZE);
656fb29d29Schristos *bufix += ETHER_HEADER_SIZE;
666fb29d29Schristos }
676fb29d29Schristos #endif /* PACKET_ASSEMBLY */
686fb29d29Schristos
696fb29d29Schristos #ifdef PACKET_DECODING
706fb29d29Schristos /* Decode a hardware header... */
716fb29d29Schristos
decode_ethernet_header(interface,buf,bufix,from)726fb29d29Schristos ssize_t decode_ethernet_header (interface, buf, bufix, from)
736fb29d29Schristos struct interface_info *interface;
746fb29d29Schristos unsigned char *buf;
756fb29d29Schristos unsigned bufix;
766fb29d29Schristos struct hardware *from;
776fb29d29Schristos {
786fb29d29Schristos struct isc_ether_header eh;
796fb29d29Schristos
806fb29d29Schristos memcpy (&eh, buf + bufix, ETHER_HEADER_SIZE);
816fb29d29Schristos
826fb29d29Schristos #ifdef USERLAND_FILTER
836fb29d29Schristos if (ntohs (eh.ether_type) != ETHERTYPE_IP)
846fb29d29Schristos return -1;
856fb29d29Schristos #endif
866fb29d29Schristos memcpy (&from -> hbuf [1], eh.ether_shost, sizeof (eh.ether_shost));
876fb29d29Schristos from -> hbuf [0] = ARPHRD_ETHER;
886fb29d29Schristos from -> hlen = (sizeof eh.ether_shost) + 1;
896fb29d29Schristos
906fb29d29Schristos return ETHER_HEADER_SIZE;
916fb29d29Schristos }
926fb29d29Schristos #endif /* PACKET_DECODING */
93