You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ethernet.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <byteswap.h>
  29. #include <errno.h>
  30. #include <assert.h>
  31. #include <ipxe/if_arp.h>
  32. #include <ipxe/if_ether.h>
  33. #include <ipxe/in.h>
  34. #include <ipxe/netdevice.h>
  35. #include <ipxe/iobuf.h>
  36. #include <ipxe/ethernet.h>
  37. /** @file
  38. *
  39. * Ethernet protocol
  40. *
  41. */
  42. /** Ethernet broadcast MAC address */
  43. uint8_t eth_broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  44. /**
  45. * Check if Ethernet packet has an 802.3 LLC header
  46. *
  47. * @v ethhdr Ethernet header
  48. * @ret is_llc Packet has 802.3 LLC header
  49. */
  50. static inline int eth_is_llc_packet ( struct ethhdr *ethhdr ) {
  51. uint8_t len_msb;
  52. /* Check if the protocol field contains a value short enough
  53. * to be a frame length. The slightly convoluted form of the
  54. * comparison is designed to reduce to a single x86
  55. * instruction.
  56. */
  57. len_msb = *( ( uint8_t * ) &ethhdr->h_protocol );
  58. return ( len_msb < 0x06 );
  59. }
  60. /**
  61. * Add Ethernet link-layer header
  62. *
  63. * @v netdev Network device
  64. * @v iobuf I/O buffer
  65. * @v ll_dest Link-layer destination address
  66. * @v ll_source Source link-layer address
  67. * @v net_proto Network-layer protocol, in network-byte order
  68. * @ret rc Return status code
  69. */
  70. int eth_push ( struct net_device *netdev __unused, struct io_buffer *iobuf,
  71. const void *ll_dest, const void *ll_source,
  72. uint16_t net_proto ) {
  73. struct ethhdr *ethhdr = iob_push ( iobuf, sizeof ( *ethhdr ) );
  74. /* Build Ethernet header */
  75. memcpy ( ethhdr->h_dest, ll_dest, ETH_ALEN );
  76. memcpy ( ethhdr->h_source, ll_source, ETH_ALEN );
  77. ethhdr->h_protocol = net_proto;
  78. return 0;
  79. }
  80. /**
  81. * Remove Ethernet link-layer header
  82. *
  83. * @v netdev Network device
  84. * @v iobuf I/O buffer
  85. * @ret ll_dest Link-layer destination address
  86. * @ret ll_source Source link-layer address
  87. * @ret net_proto Network-layer protocol, in network-byte order
  88. * @ret flags Packet flags
  89. * @ret rc Return status code
  90. */
  91. int eth_pull ( struct net_device *netdev __unused, struct io_buffer *iobuf,
  92. const void **ll_dest, const void **ll_source,
  93. uint16_t *net_proto, unsigned int *flags ) {
  94. struct ethhdr *ethhdr = iobuf->data;
  95. uint16_t *llc_proto;
  96. /* Sanity check. While in theory we could receive a one-byte
  97. * packet, this will never happen in practice and performing
  98. * the combined length check here avoids the need for an
  99. * additional comparison if we detect an LLC frame.
  100. */
  101. if ( iob_len ( iobuf ) < ( sizeof ( *ethhdr ) + sizeof ( *llc_proto ))){
  102. DBG ( "Ethernet packet too short (%zd bytes)\n",
  103. iob_len ( iobuf ) );
  104. return -EINVAL;
  105. }
  106. /* Strip off Ethernet header */
  107. iob_pull ( iobuf, sizeof ( *ethhdr ) );
  108. /* Fill in required fields */
  109. *ll_dest = ethhdr->h_dest;
  110. *ll_source = ethhdr->h_source;
  111. *net_proto = ethhdr->h_protocol;
  112. *flags = ( ( is_multicast_ether_addr ( ethhdr->h_dest ) ?
  113. LL_MULTICAST : 0 ) |
  114. ( is_broadcast_ether_addr ( ethhdr->h_dest ) ?
  115. LL_BROADCAST : 0 ) );
  116. /* If this is an LLC frame (with a length in place of the
  117. * protocol field), then use the next two bytes (which happen
  118. * to be the LLC DSAP and SSAP) as the protocol. This allows
  119. * for minimal-overhead support for receiving (rare) LLC
  120. * frames, without requiring a full LLC protocol layer.
  121. */
  122. if ( eth_is_llc_packet ( ethhdr ) ) {
  123. llc_proto = ( &ethhdr->h_protocol + 1 );
  124. *net_proto = *llc_proto;
  125. }
  126. return 0;
  127. }
  128. /**
  129. * Initialise Ethernet address
  130. *
  131. * @v hw_addr Hardware address
  132. * @v ll_addr Link-layer address
  133. */
  134. void eth_init_addr ( const void *hw_addr, void *ll_addr ) {
  135. memcpy ( ll_addr, hw_addr, ETH_ALEN );
  136. }
  137. /**
  138. * Generate random Ethernet address
  139. *
  140. * @v hw_addr Generated hardware address
  141. */
  142. void eth_random_addr ( void *hw_addr ) {
  143. uint8_t *addr = hw_addr;
  144. unsigned int i;
  145. for ( i = 0 ; i < ETH_ALEN ; i++ )
  146. addr[i] = random();
  147. addr[0] &= ~0x01; /* Clear multicast bit */
  148. addr[0] |= 0x02; /* Set locally-assigned bit */
  149. }
  150. /**
  151. * Transcribe Ethernet address
  152. *
  153. * @v ll_addr Link-layer address
  154. * @ret string Link-layer address in human-readable format
  155. */
  156. const char * eth_ntoa ( const void *ll_addr ) {
  157. static char buf[18]; /* "00:00:00:00:00:00" */
  158. const uint8_t *eth_addr = ll_addr;
  159. sprintf ( buf, "%02x:%02x:%02x:%02x:%02x:%02x",
  160. eth_addr[0], eth_addr[1], eth_addr[2],
  161. eth_addr[3], eth_addr[4], eth_addr[5] );
  162. return buf;
  163. }
  164. /**
  165. * Hash multicast address
  166. *
  167. * @v af Address family
  168. * @v net_addr Network-layer address
  169. * @v ll_addr Link-layer address to fill in
  170. * @ret rc Return status code
  171. */
  172. int eth_mc_hash ( unsigned int af, const void *net_addr, void *ll_addr ) {
  173. const uint8_t *net_addr_bytes = net_addr;
  174. uint8_t *ll_addr_bytes = ll_addr;
  175. switch ( af ) {
  176. case AF_INET:
  177. ll_addr_bytes[0] = 0x01;
  178. ll_addr_bytes[1] = 0x00;
  179. ll_addr_bytes[2] = 0x5e;
  180. ll_addr_bytes[3] = net_addr_bytes[1] & 0x7f;
  181. ll_addr_bytes[4] = net_addr_bytes[2];
  182. ll_addr_bytes[5] = net_addr_bytes[3];
  183. return 0;
  184. case AF_INET6:
  185. ll_addr_bytes[0] = 0x33;
  186. ll_addr_bytes[1] = 0x33;
  187. memcpy ( &ll_addr_bytes[2], &net_addr_bytes[12], 4 );
  188. return 0;
  189. default:
  190. return -ENOTSUP;
  191. }
  192. }
  193. /**
  194. * Generate Ethernet-compatible compressed link-layer address
  195. *
  196. * @v ll_addr Link-layer address
  197. * @v eth_addr Ethernet-compatible address to fill in
  198. */
  199. int eth_eth_addr ( const void *ll_addr, void *eth_addr ) {
  200. memcpy ( eth_addr, ll_addr, ETH_ALEN );
  201. return 0;
  202. }
  203. /**
  204. * Generate EUI-64 address
  205. *
  206. * @v ll_addr Link-layer address
  207. * @v eui64 EUI-64 address to fill in
  208. * @ret rc Return status code
  209. */
  210. int eth_eui64 ( const void *ll_addr, void *eui64 ) {
  211. memcpy ( ( eui64 + 0 ), ( ll_addr + 0 ), 3 );
  212. memcpy ( ( eui64 + 5 ), ( ll_addr + 3 ), 3 );
  213. *( ( uint16_t * ) ( eui64 + 3 ) ) = htons ( 0xfffe );
  214. return 0;
  215. }
  216. /** Ethernet protocol */
  217. struct ll_protocol ethernet_protocol __ll_protocol = {
  218. .name = "Ethernet",
  219. .ll_proto = htons ( ARPHRD_ETHER ),
  220. .hw_addr_len = ETH_ALEN,
  221. .ll_addr_len = ETH_ALEN,
  222. .ll_header_len = ETH_HLEN,
  223. .push = eth_push,
  224. .pull = eth_pull,
  225. .init_addr = eth_init_addr,
  226. .ntoa = eth_ntoa,
  227. .mc_hash = eth_mc_hash,
  228. .eth_addr = eth_eth_addr,
  229. .eui64 = eth_eui64,
  230. };
  231. /**
  232. * Allocate Ethernet device
  233. *
  234. * @v priv_size Size of driver private data
  235. * @ret netdev Network device, or NULL
  236. */
  237. struct net_device * alloc_etherdev ( size_t priv_size ) {
  238. struct net_device *netdev;
  239. netdev = alloc_netdev ( priv_size );
  240. if ( netdev ) {
  241. netdev->ll_protocol = &ethernet_protocol;
  242. netdev->ll_broadcast = eth_broadcast;
  243. netdev->max_pkt_len = ETH_FRAME_LEN;
  244. }
  245. return netdev;
  246. }
  247. /* Drag in objects via ethernet_protocol */
  248. REQUIRING_SYMBOL ( ethernet_protocol );
  249. /* Drag in Ethernet configuration */
  250. REQUIRE_OBJECT ( config_ethernet );