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 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. * Add Ethernet link-layer header
  46. *
  47. * @v netdev Network device
  48. * @v iobuf I/O buffer
  49. * @v ll_dest Link-layer destination address
  50. * @v ll_source Source link-layer address
  51. * @v net_proto Network-layer protocol, in network-byte order
  52. * @ret rc Return status code
  53. */
  54. int eth_push ( struct net_device *netdev __unused, struct io_buffer *iobuf,
  55. const void *ll_dest, const void *ll_source,
  56. uint16_t net_proto ) {
  57. 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;
  62. return 0;
  63. }
  64. /**
  65. * Remove Ethernet link-layer header
  66. *
  67. * @v netdev Network device
  68. * @v iobuf I/O buffer
  69. * @ret ll_dest Link-layer destination address
  70. * @ret ll_source Source link-layer address
  71. * @ret net_proto Network-layer protocol, in network-byte order
  72. * @ret flags Packet flags
  73. * @ret rc Return status code
  74. */
  75. int eth_pull ( struct net_device *netdev __unused, struct io_buffer *iobuf,
  76. const void **ll_dest, const void **ll_source,
  77. uint16_t *net_proto, unsigned int *flags ) {
  78. struct ethhdr *ethhdr = iobuf->data;
  79. /* Sanity check */
  80. if ( iob_len ( iobuf ) < sizeof ( *ethhdr ) ) {
  81. DBG ( "Ethernet packet too short (%zd bytes)\n",
  82. iob_len ( iobuf ) );
  83. return -EINVAL;
  84. }
  85. /* Strip off Ethernet header */
  86. iob_pull ( iobuf, sizeof ( *ethhdr ) );
  87. /* Fill in required fields */
  88. *ll_dest = ethhdr->h_dest;
  89. *ll_source = ethhdr->h_source;
  90. *net_proto = ethhdr->h_protocol;
  91. *flags = ( ( is_multicast_ether_addr ( ethhdr->h_dest ) ?
  92. LL_MULTICAST : 0 ) |
  93. ( is_broadcast_ether_addr ( ethhdr->h_dest ) ?
  94. LL_BROADCAST : 0 ) );
  95. return 0;
  96. }
  97. /**
  98. * Initialise Ethernet address
  99. *
  100. * @v hw_addr Hardware address
  101. * @v ll_addr Link-layer address
  102. */
  103. void eth_init_addr ( const void *hw_addr, void *ll_addr ) {
  104. memcpy ( ll_addr, hw_addr, ETH_ALEN );
  105. }
  106. /**
  107. * Generate random Ethernet address
  108. *
  109. * @v hw_addr Generated hardware address
  110. */
  111. void eth_random_addr ( void *hw_addr ) {
  112. uint8_t *addr = hw_addr;
  113. unsigned int i;
  114. for ( i = 0 ; i < ETH_ALEN ; i++ )
  115. addr[i] = random();
  116. addr[0] &= ~0x01; /* Clear multicast bit */
  117. addr[0] |= 0x02; /* Set locally-assigned bit */
  118. }
  119. /**
  120. * Transcribe Ethernet address
  121. *
  122. * @v ll_addr Link-layer address
  123. * @ret string Link-layer address in human-readable format
  124. */
  125. const char * eth_ntoa ( const void *ll_addr ) {
  126. static char buf[18]; /* "00:00:00:00:00:00" */
  127. const uint8_t *eth_addr = ll_addr;
  128. sprintf ( buf, "%02x:%02x:%02x:%02x:%02x:%02x",
  129. eth_addr[0], eth_addr[1], eth_addr[2],
  130. eth_addr[3], eth_addr[4], eth_addr[5] );
  131. return buf;
  132. }
  133. /**
  134. * Hash multicast address
  135. *
  136. * @v af Address family
  137. * @v net_addr Network-layer address
  138. * @v ll_addr Link-layer address to fill in
  139. * @ret rc Return status code
  140. */
  141. int eth_mc_hash ( unsigned int af, const void *net_addr, void *ll_addr ) {
  142. const uint8_t *net_addr_bytes = net_addr;
  143. uint8_t *ll_addr_bytes = ll_addr;
  144. switch ( af ) {
  145. case AF_INET:
  146. ll_addr_bytes[0] = 0x01;
  147. ll_addr_bytes[1] = 0x00;
  148. ll_addr_bytes[2] = 0x5e;
  149. ll_addr_bytes[3] = net_addr_bytes[1] & 0x7f;
  150. ll_addr_bytes[4] = net_addr_bytes[2];
  151. ll_addr_bytes[5] = net_addr_bytes[3];
  152. return 0;
  153. case AF_INET6:
  154. ll_addr_bytes[0] = 0x33;
  155. ll_addr_bytes[1] = 0x33;
  156. memcpy ( &ll_addr_bytes[2], &net_addr_bytes[12], 4 );
  157. return 0;
  158. default:
  159. return -ENOTSUP;
  160. }
  161. }
  162. /**
  163. * Generate Ethernet-compatible compressed link-layer address
  164. *
  165. * @v ll_addr Link-layer address
  166. * @v eth_addr Ethernet-compatible address to fill in
  167. */
  168. int eth_eth_addr ( const void *ll_addr, void *eth_addr ) {
  169. memcpy ( eth_addr, ll_addr, ETH_ALEN );
  170. return 0;
  171. }
  172. /**
  173. * Generate EUI-64 address
  174. *
  175. * @v ll_addr Link-layer address
  176. * @v eui64 EUI-64 address to fill in
  177. * @ret rc Return status code
  178. */
  179. int eth_eui64 ( const void *ll_addr, void *eui64 ) {
  180. memcpy ( ( eui64 + 0 ), ( ll_addr + 0 ), 3 );
  181. memcpy ( ( eui64 + 5 ), ( ll_addr + 3 ), 3 );
  182. *( ( uint16_t * ) ( eui64 + 3 ) ) = htons ( 0xfffe );
  183. return 0;
  184. }
  185. /** Ethernet protocol */
  186. struct ll_protocol ethernet_protocol __ll_protocol = {
  187. .name = "Ethernet",
  188. .ll_proto = htons ( ARPHRD_ETHER ),
  189. .hw_addr_len = ETH_ALEN,
  190. .ll_addr_len = ETH_ALEN,
  191. .ll_header_len = ETH_HLEN,
  192. .push = eth_push,
  193. .pull = eth_pull,
  194. .init_addr = eth_init_addr,
  195. .ntoa = eth_ntoa,
  196. .mc_hash = eth_mc_hash,
  197. .eth_addr = eth_eth_addr,
  198. .eui64 = eth_eui64,
  199. };
  200. /**
  201. * Allocate Ethernet device
  202. *
  203. * @v priv_size Size of driver private data
  204. * @ret netdev Network device, or NULL
  205. */
  206. struct net_device * alloc_etherdev ( size_t priv_size ) {
  207. struct net_device *netdev;
  208. netdev = alloc_netdev ( priv_size );
  209. if ( netdev ) {
  210. netdev->ll_protocol = &ethernet_protocol;
  211. netdev->ll_broadcast = eth_broadcast;
  212. netdev->max_pkt_len = ETH_FRAME_LEN;
  213. }
  214. return netdev;
  215. }
  216. /* Drag in Ethernet slow protocols */
  217. REQUIRE_OBJECT ( eth_slow );