2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <gpxe/if_arp.h>
26 #include <gpxe/if_ether.h>
28 #include <gpxe/netdevice.h>
29 #include <gpxe/iobuf.h>
30 #include <gpxe/ethernet.h>
38 /** Ethernet broadcast MAC address */
39 static uint8_t eth_broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
42 * Add Ethernet link-layer header
45 * @v ll_dest Link-layer destination address
46 * @v ll_source Source link-layer address
47 * @v net_proto Network-layer protocol, in network-byte order
48 * @ret rc Return status code
50 static int eth_push ( struct io_buffer *iobuf, const void *ll_dest,
51 const void *ll_source, uint16_t net_proto ) {
52 struct ethhdr *ethhdr = iob_push ( iobuf, sizeof ( *ethhdr ) );
54 /* Build Ethernet header */
55 memcpy ( ethhdr->h_dest, ll_dest, ETH_ALEN );
56 memcpy ( ethhdr->h_source, ll_source, ETH_ALEN );
57 ethhdr->h_protocol = net_proto;
63 * Remove Ethernet link-layer header
66 * @ret ll_dest Link-layer destination address
67 * @ret ll_source Source link-layer address
68 * @ret net_proto Network-layer protocol, in network-byte order
69 * @ret rc Return status code
71 static int eth_pull ( struct io_buffer *iobuf, const void **ll_dest,
72 const void **ll_source, uint16_t *net_proto ) {
73 struct ethhdr *ethhdr = iobuf->data;
76 if ( iob_len ( iobuf ) < sizeof ( *ethhdr ) ) {
77 DBG ( "Ethernet packet too short (%zd bytes)\n",
82 /* Strip off Ethernet header */
83 iob_pull ( iobuf, sizeof ( *ethhdr ) );
85 /* Fill in required fields */
86 *ll_dest = ethhdr->h_dest;
87 *ll_source = ethhdr->h_source;
88 *net_proto = ethhdr->h_protocol;
94 * Transcribe Ethernet address
96 * @v ll_addr Link-layer address
97 * @ret string Link-layer address in human-readable format
99 const char * eth_ntoa ( const void *ll_addr ) {
100 static char buf[18]; /* "00:00:00:00:00:00" */
101 const uint8_t *eth_addr = ll_addr;
103 sprintf ( buf, "%02x:%02x:%02x:%02x:%02x:%02x",
104 eth_addr[0], eth_addr[1], eth_addr[2],
105 eth_addr[3], eth_addr[4], eth_addr[5] );
110 * Hash multicast address
112 * @v af Address family
113 * @v net_addr Network-layer address
114 * @v ll_addr Link-layer address to fill in
115 * @ret rc Return status code
117 static int eth_mc_hash ( unsigned int af, const void *net_addr,
119 const uint8_t *net_addr_bytes = net_addr;
120 uint8_t *ll_addr_bytes = ll_addr;
124 ll_addr_bytes[0] = 0x01;
125 ll_addr_bytes[1] = 0x00;
126 ll_addr_bytes[2] = 0x5e;
127 ll_addr_bytes[3] = net_addr_bytes[1] & 0x7f;
128 ll_addr_bytes[4] = net_addr_bytes[2];
129 ll_addr_bytes[5] = net_addr_bytes[3];
135 /** Ethernet protocol */
136 struct ll_protocol ethernet_protocol __ll_protocol = {
138 .ll_proto = htons ( ARPHRD_ETHER ),
139 .ll_addr_len = ETH_ALEN,
140 .ll_header_len = ETH_HLEN,
141 .ll_broadcast = eth_broadcast,
145 .mc_hash = eth_mc_hash,