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.
19 FILE_LICENCE ( GPL2_OR_LATER
);
27 #include <gpxe/if_arp.h>
28 #include <gpxe/if_ether.h>
30 #include <gpxe/netdevice.h>
31 #include <gpxe/iobuf.h>
32 #include <gpxe/ethernet.h>
40 /** Ethernet broadcast MAC address */
41 static uint8_t eth_broadcast
[ETH_ALEN
] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
44 * Add Ethernet link-layer header
46 * @v netdev Network device
48 * @v ll_dest Link-layer destination address
49 * @v ll_source Source link-layer address
50 * @v net_proto Network-layer protocol, in network-byte order
51 * @ret rc Return status code
53 static int eth_push ( struct net_device
*netdev __unused
,
54 struct io_buffer
*iobuf
, const void *ll_dest
,
55 const void *ll_source
, uint16_t net_proto
) {
56 struct ethhdr
*ethhdr
= iob_push ( iobuf
, sizeof ( *ethhdr
) );
58 /* Build Ethernet header */
59 memcpy ( ethhdr
->h_dest
, ll_dest
, ETH_ALEN
);
60 memcpy ( ethhdr
->h_source
, ll_source
, ETH_ALEN
);
61 ethhdr
->h_protocol
= net_proto
;
67 * Remove Ethernet link-layer header
69 * @v netdev Network device
71 * @ret ll_dest Link-layer destination address
72 * @ret ll_source Source link-layer address
73 * @ret net_proto Network-layer protocol, in network-byte order
74 * @ret rc Return status code
76 static int eth_pull ( struct net_device
*netdev __unused
,
77 struct io_buffer
*iobuf
, const void **ll_dest
,
78 const void **ll_source
, uint16_t *net_proto
) {
79 struct ethhdr
*ethhdr
= iobuf
->data
;
82 if ( iob_len ( iobuf
) < sizeof ( *ethhdr
) ) {
83 DBG ( "Ethernet packet too short (%zd bytes)\n",
88 /* Strip off Ethernet header */
89 iob_pull ( iobuf
, sizeof ( *ethhdr
) );
91 /* Fill in required fields */
92 *ll_dest
= ethhdr
->h_dest
;
93 *ll_source
= ethhdr
->h_source
;
94 *net_proto
= ethhdr
->h_protocol
;
100 * Initialise Ethernet address
102 * @v hw_addr Hardware address
103 * @v ll_addr Link-layer address
105 void eth_init_addr ( const void *hw_addr
, void *ll_addr
) {
106 memcpy ( ll_addr
, hw_addr
, ETH_ALEN
);
110 * Transcribe Ethernet address
112 * @v ll_addr Link-layer address
113 * @ret string Link-layer address in human-readable format
115 const char * eth_ntoa ( const void *ll_addr
) {
116 static char buf
[18]; /* "00:00:00:00:00:00" */
117 const uint8_t *eth_addr
= ll_addr
;
119 sprintf ( buf
, "%02x:%02x:%02x:%02x:%02x:%02x",
120 eth_addr
[0], eth_addr
[1], eth_addr
[2],
121 eth_addr
[3], eth_addr
[4], eth_addr
[5] );
126 * Hash multicast address
128 * @v af Address family
129 * @v net_addr Network-layer address
130 * @v ll_addr Link-layer address to fill in
131 * @ret rc Return status code
133 int eth_mc_hash ( unsigned int af
, const void *net_addr
, void *ll_addr
) {
134 const uint8_t *net_addr_bytes
= net_addr
;
135 uint8_t *ll_addr_bytes
= ll_addr
;
139 ll_addr_bytes
[0] = 0x01;
140 ll_addr_bytes
[1] = 0x00;
141 ll_addr_bytes
[2] = 0x5e;
142 ll_addr_bytes
[3] = net_addr_bytes
[1] & 0x7f;
143 ll_addr_bytes
[4] = net_addr_bytes
[2];
144 ll_addr_bytes
[5] = net_addr_bytes
[3];
151 /** Ethernet protocol */
152 struct ll_protocol ethernet_protocol __ll_protocol
= {
154 .ll_proto
= htons ( ARPHRD_ETHER
),
155 .hw_addr_len
= ETH_ALEN
,
156 .ll_addr_len
= ETH_ALEN
,
157 .ll_header_len
= ETH_HLEN
,
160 .init_addr
= eth_init_addr
,
162 .mc_hash
= eth_mc_hash
,
166 * Allocate Ethernet device
168 * @v priv_size Size of driver private data
169 * @ret netdev Network device, or NULL
171 struct net_device
* alloc_etherdev ( size_t priv_size
) {
172 struct net_device
*netdev
;
174 netdev
= alloc_netdev ( priv_size
);
176 netdev
->ll_protocol
= ðernet_protocol
;
177 netdev
->ll_broadcast
= eth_broadcast
;
178 netdev
->max_pkt_len
= ETH_FRAME_LEN
;