iPXE
ethernet.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <byteswap.h>
31 #include <errno.h>
32 #include <assert.h>
33 #include <ipxe/if_arp.h>
34 #include <ipxe/if_ether.h>
35 #include <ipxe/in.h>
36 #include <ipxe/netdevice.h>
37 #include <ipxe/iobuf.h>
38 #include <ipxe/ethernet.h>
39 
40 /** @file
41  *
42  * Ethernet protocol
43  *
44  */
45 
46 /** Ethernet broadcast MAC address */
47 uint8_t eth_broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
48 
49 /**
50  * Check if Ethernet packet has an 802.3 LLC header
51  *
52  * @v ethhdr Ethernet header
53  * @ret is_llc Packet has 802.3 LLC header
54  */
55 static inline int eth_is_llc_packet ( struct ethhdr *ethhdr ) {
56  uint8_t len_msb;
57 
58  /* Check if the protocol field contains a value short enough
59  * to be a frame length. The slightly convoluted form of the
60  * comparison is designed to reduce to a single x86
61  * instruction.
62  */
63  len_msb = *( ( uint8_t * ) &ethhdr->h_protocol );
64  return ( len_msb < 0x06 );
65 }
66 
67 /**
68  * Add Ethernet link-layer header
69  *
70  * @v netdev Network device
71  * @v iobuf I/O buffer
72  * @v ll_dest Link-layer destination address
73  * @v ll_source Source link-layer address
74  * @v net_proto Network-layer protocol, in network-byte order
75  * @ret rc Return status code
76  */
77 int eth_push ( struct net_device *netdev __unused, struct io_buffer *iobuf,
78  const void *ll_dest, const void *ll_source,
80  struct ethhdr *ethhdr = iob_push ( iobuf, sizeof ( *ethhdr ) );
81 
82  /* Build Ethernet header */
83  memcpy ( ethhdr->h_dest, ll_dest, ETH_ALEN );
84  memcpy ( ethhdr->h_source, ll_source, ETH_ALEN );
85  ethhdr->h_protocol = net_proto;
86 
87  return 0;
88 }
89 
90 /**
91  * Remove Ethernet link-layer header
92  *
93  * @v netdev Network device
94  * @v iobuf I/O buffer
95  * @ret ll_dest Link-layer destination address
96  * @ret ll_source Source link-layer address
97  * @ret net_proto Network-layer protocol, in network-byte order
98  * @ret flags Packet flags
99  * @ret rc Return status code
100  */
101 int eth_pull ( struct net_device *netdev __unused, struct io_buffer *iobuf,
102  const void **ll_dest, const void **ll_source,
103  uint16_t *net_proto, unsigned int *flags ) {
104  struct ethhdr *ethhdr = iobuf->data;
105  uint16_t *llc_proto;
106 
107  /* Sanity check. While in theory we could receive a one-byte
108  * packet, this will never happen in practice and performing
109  * the combined length check here avoids the need for an
110  * additional comparison if we detect an LLC frame.
111  */
112  if ( iob_len ( iobuf ) < ( sizeof ( *ethhdr ) + sizeof ( *llc_proto ))){
113  DBG ( "Ethernet packet too short (%zd bytes)\n",
114  iob_len ( iobuf ) );
115  return -EINVAL;
116  }
117 
118  /* Strip off Ethernet header */
119  iob_pull ( iobuf, sizeof ( *ethhdr ) );
120 
121  /* Fill in required fields */
122  *ll_dest = ethhdr->h_dest;
123  *ll_source = ethhdr->h_source;
124  *net_proto = ethhdr->h_protocol;
126  LL_MULTICAST : 0 ) |
128  LL_BROADCAST : 0 ) );
129 
130  /* If this is an LLC frame (with a length in place of the
131  * protocol field), then use the next two bytes (which happen
132  * to be the LLC DSAP and SSAP) as the protocol. This allows
133  * for minimal-overhead support for receiving (rare) LLC
134  * frames, without requiring a full LLC protocol layer.
135  */
136  if ( eth_is_llc_packet ( ethhdr ) ) {
137  llc_proto = iobuf->data;
138  *net_proto = *llc_proto;
139  }
140 
141  return 0;
142 }
143 
144 /**
145  * Initialise Ethernet address
146  *
147  * @v hw_addr Hardware address
148  * @v ll_addr Link-layer address
149  */
150 void eth_init_addr ( const void *hw_addr, void *ll_addr ) {
151  memcpy ( ll_addr, hw_addr, ETH_ALEN );
152 }
153 
154 /**
155  * Generate random Ethernet address
156  *
157  * @v hw_addr Generated hardware address
158  */
159 void eth_random_addr ( void *hw_addr ) {
160  uint8_t *addr = hw_addr;
161  unsigned int i;
162 
163  for ( i = 0 ; i < ETH_ALEN ; i++ )
164  addr[i] = random();
165  addr[0] &= ~0x01; /* Clear multicast bit */
166  addr[0] |= 0x02; /* Set locally-assigned bit */
167 }
168 
169 /**
170  * Transcribe Ethernet address
171  *
172  * @v ll_addr Link-layer address
173  * @ret string Link-layer address in human-readable format
174  */
175 const char * eth_ntoa ( const void *ll_addr ) {
176  static char buf[18]; /* "00:00:00:00:00:00" */
177  const uint8_t *eth_addr = ll_addr;
178 
179  sprintf ( buf, "%02x:%02x:%02x:%02x:%02x:%02x",
180  eth_addr[0], eth_addr[1], eth_addr[2],
181  eth_addr[3], eth_addr[4], eth_addr[5] );
182  return buf;
183 }
184 
185 /**
186  * Hash multicast address
187  *
188  * @v af Address family
189  * @v net_addr Network-layer address
190  * @v ll_addr Link-layer address to fill in
191  * @ret rc Return status code
192  */
193 int eth_mc_hash ( unsigned int af, const void *net_addr, void *ll_addr ) {
194  const uint8_t *net_addr_bytes = net_addr;
195  uint8_t *ll_addr_bytes = ll_addr;
196 
197  switch ( af ) {
198  case AF_INET:
199  ll_addr_bytes[0] = 0x01;
200  ll_addr_bytes[1] = 0x00;
201  ll_addr_bytes[2] = 0x5e;
202  ll_addr_bytes[3] = net_addr_bytes[1] & 0x7f;
203  ll_addr_bytes[4] = net_addr_bytes[2];
204  ll_addr_bytes[5] = net_addr_bytes[3];
205  return 0;
206  case AF_INET6:
207  ll_addr_bytes[0] = 0x33;
208  ll_addr_bytes[1] = 0x33;
209  memcpy ( &ll_addr_bytes[2], &net_addr_bytes[12], 4 );
210  return 0;
211  default:
212  return -ENOTSUP;
213  }
214 }
215 
216 /**
217  * Generate Ethernet-compatible compressed link-layer address
218  *
219  * @v ll_addr Link-layer address
220  * @v eth_addr Ethernet-compatible address to fill in
221  */
222 int eth_eth_addr ( const void *ll_addr, void *eth_addr ) {
223  memcpy ( eth_addr, ll_addr, ETH_ALEN );
224  return 0;
225 }
226 
227 /**
228  * Generate EUI-64 address
229  *
230  * @v ll_addr Link-layer address
231  * @v eui64 EUI-64 address to fill in
232  * @ret rc Return status code
233  */
234 int eth_eui64 ( const void *ll_addr, void *eui64 ) {
235 
236  memcpy ( ( eui64 + 0 ), ( ll_addr + 0 ), 3 );
237  memcpy ( ( eui64 + 5 ), ( ll_addr + 3 ), 3 );
238  *( ( uint16_t * ) ( eui64 + 3 ) ) = htons ( 0xfffe );
239  return 0;
240 }
241 
242 /** Ethernet protocol */
243 struct ll_protocol ethernet_protocol __ll_protocol = {
244  .name = "Ethernet",
245  .ll_proto = htons ( ARPHRD_ETHER ),
246  .hw_addr_len = ETH_ALEN,
247  .ll_addr_len = ETH_ALEN,
248  .ll_header_len = ETH_HLEN,
249  .push = eth_push,
250  .pull = eth_pull,
251  .init_addr = eth_init_addr,
252  .ntoa = eth_ntoa,
253  .mc_hash = eth_mc_hash,
254  .eth_addr = eth_eth_addr,
255  .eui64 = eth_eui64,
256 };
257 
258 /**
259  * Allocate Ethernet device
260  *
261  * @v priv_size Size of driver private data
262  * @ret netdev Network device, or NULL
263  */
264 struct net_device * alloc_etherdev ( size_t priv_size ) {
265  struct net_device *netdev;
266 
267  netdev = alloc_netdev ( priv_size );
268  if ( netdev ) {
269  netdev->ll_protocol = &ethernet_protocol;
273  }
274  return netdev;
275 }
276 
277 /* Drag in objects via ethernet_protocol */
278 REQUIRING_SYMBOL ( ethernet_protocol );
279 
280 /* Drag in Ethernet configuration */
281 REQUIRE_OBJECT ( config_ethernet );
#define LL_MULTICAST
Packet is a multicast (including broadcast) packet.
Definition: netdevice.h:105
uint16_t h_protocol
Protocol ID.
Definition: if_ether.h:37
#define iob_pull(iobuf, len)
Definition: iobuf.h:102
#define EINVAL
Invalid argument.
Definition: errno.h:428
unsigned short uint16_t
Definition: stdint.h:11
struct ll_protocol ethernet_protocol __ll_protocol
Ethernet protocol.
Definition: ethernet.c:243
#define AF_INET6
IPv6 Internet addresses.
Definition: socket.h:64
int eth_eth_addr(const void *ll_addr, void *eth_addr)
Generate Ethernet-compatible compressed link-layer address.
Definition: ethernet.c:222
Error codes.
#define iob_push(iobuf, len)
Definition: iobuf.h:84
I/O buffers.
#define sprintf(buf, fmt,...)
Write a formatted string to a buffer.
Definition: stdio.h:36
size_t mtu
Maximum transmission unit length.
Definition: netdevice.h:415
uint8_t eth_broadcast[ETH_ALEN]
Ethernet broadcast MAC address.
Definition: ethernet.c:47
REQUIRING_SYMBOL(ethernet_protocol)
const uint8_t * ll_broadcast
Link-layer broadcast address.
Definition: netdevice.h:389
#define ETH_MAX_MTU
Definition: if_ether.h:14
int eth_push(struct net_device *netdev __unused, struct io_buffer *iobuf, const void *ll_dest, const void *ll_source, uint16_t net_proto)
Add Ethernet link-layer header.
Definition: ethernet.c:77
void eth_init_addr(const void *hw_addr, void *ll_addr)
Initialise Ethernet address.
Definition: ethernet.c:150
A link-layer protocol.
Definition: netdevice.h:114
Address Resolution Protocol constants and types.
int eth_eui64(const void *ll_addr, void *eui64)
Generate EUI-64 address.
Definition: ethernet.c:234
int eth_pull(struct net_device *netdev __unused, struct io_buffer *iobuf, const void **ll_dest, const void **ll_source, uint16_t *net_proto, unsigned int *flags)
Remove Ethernet link-layer header.
Definition: ethernet.c:101
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
struct net_device * alloc_netdev(size_t priv_len)
Allocate network device.
Definition: netdevice.c:721
const char * name
Protocol name.
Definition: netdevice.h:116
uint8_t h_dest[ETH_ALEN]
Destination MAC address.
Definition: if_ether.h:33
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
#define ETH_HLEN
Definition: if_ether.h:9
Assertions.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Ethernet protocol.
#define ETH_FRAME_LEN
Definition: if_ether.h:11
static struct net_device * netdev
Definition: gdbudp.c:52
static int eth_is_llc_packet(struct ethhdr *ethhdr)
Check if Ethernet packet has an 802.3 LLC header.
Definition: ethernet.c:55
int eth_mc_hash(unsigned int af, const void *net_addr, void *ll_addr)
Hash multicast address.
Definition: ethernet.c:193
static int is_multicast_ether_addr(const void *addr)
Check if Ethernet address is a multicast address.
Definition: ethernet.h:37
uint8_t flags
Flags.
Definition: ena.h:18
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:155
const char * eth_ntoa(const void *ll_addr)
Transcribe Ethernet address.
Definition: ethernet.c:175
A network device.
Definition: netdevice.h:352
long int random(void)
Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
Definition: random.c:31
unsigned char uint8_t
Definition: stdint.h:10
uint8_t h_source[ETH_ALEN]
Source MAC address.
Definition: if_ether.h:35
uint16_t net_proto
Network-layer protocol.
Definition: netdevice.h:99
#define ETH_ALEN
Definition: if_ether.h:8
#define LL_BROADCAST
Packet is a broadcast packet.
Definition: netdevice.h:108
Network device management.
void * data
Start of data.
Definition: iobuf.h:48
u32 addr
Definition: sky2.h:8
static int is_broadcast_ether_addr(const void *addr)
Check if Ethernet address is the broadcast address.
Definition: ethernet.h:61
struct net_device * alloc_etherdev(size_t priv_size)
Allocate Ethernet device.
Definition: ethernet.c:264
size_t max_pkt_len
Maximum packet length.
Definition: netdevice.h:409
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define ARPHRD_ETHER
Ethernet 10Mbps.
Definition: if_arp.h:16
An Ethernet link-layer header.
Definition: if_ether.h:31
REQUIRE_OBJECT(config_ethernet)
String functions.
#define htons(value)
Definition: byteswap.h:135
#define AF_INET
IPv4 Internet addresses.
Definition: socket.h:63
void eth_random_addr(void *hw_addr)
Generate random Ethernet address.
Definition: ethernet.c:159
struct ll_protocol * ll_protocol
Link-layer protocol.
Definition: netdevice.h:372
A persistent I/O buffer.
Definition: iobuf.h:33