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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <byteswap.h>
  24. #include <errno.h>
  25. #include <assert.h>
  26. #include <ipxe/if_arp.h>
  27. #include <ipxe/if_ether.h>
  28. #include <ipxe/in.h>
  29. #include <ipxe/netdevice.h>
  30. #include <ipxe/iobuf.h>
  31. #include <ipxe/ethernet.h>
  32. /** @file
  33. *
  34. * Ethernet protocol
  35. *
  36. */
  37. /** Ethernet broadcast MAC address */
  38. uint8_t eth_broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  39. /**
  40. * Add Ethernet link-layer header
  41. *
  42. * @v netdev Network device
  43. * @v iobuf I/O buffer
  44. * @v ll_dest Link-layer destination address
  45. * @v ll_source Source link-layer address
  46. * @v net_proto Network-layer protocol, in network-byte order
  47. * @ret rc Return status code
  48. */
  49. int eth_push ( struct net_device *netdev __unused, struct io_buffer *iobuf,
  50. const void *ll_dest, const void *ll_source,
  51. uint16_t net_proto ) {
  52. struct ethhdr *ethhdr = iob_push ( iobuf, sizeof ( *ethhdr ) );
  53. /* Build Ethernet header */
  54. memcpy ( ethhdr->h_dest, ll_dest, ETH_ALEN );
  55. memcpy ( ethhdr->h_source, ll_source, ETH_ALEN );
  56. ethhdr->h_protocol = net_proto;
  57. return 0;
  58. }
  59. /**
  60. * Remove Ethernet link-layer header
  61. *
  62. * @v netdev Network device
  63. * @v iobuf I/O buffer
  64. * @ret ll_dest Link-layer destination address
  65. * @ret ll_source Source link-layer address
  66. * @ret net_proto Network-layer protocol, in network-byte order
  67. * @ret flags Packet flags
  68. * @ret rc Return status code
  69. */
  70. int eth_pull ( struct net_device *netdev __unused, struct io_buffer *iobuf,
  71. const void **ll_dest, const void **ll_source,
  72. uint16_t *net_proto, unsigned int *flags ) {
  73. struct ethhdr *ethhdr = iobuf->data;
  74. /* Sanity check */
  75. if ( iob_len ( iobuf ) < sizeof ( *ethhdr ) ) {
  76. DBG ( "Ethernet packet too short (%zd bytes)\n",
  77. iob_len ( iobuf ) );
  78. return -EINVAL;
  79. }
  80. /* Strip off Ethernet header */
  81. iob_pull ( iobuf, sizeof ( *ethhdr ) );
  82. /* Fill in required fields */
  83. *ll_dest = ethhdr->h_dest;
  84. *ll_source = ethhdr->h_source;
  85. *net_proto = ethhdr->h_protocol;
  86. *flags = ( ( is_multicast_ether_addr ( ethhdr->h_dest ) ?
  87. LL_MULTICAST : 0 ) |
  88. ( is_broadcast_ether_addr ( ethhdr->h_dest ) ?
  89. LL_BROADCAST : 0 ) );
  90. return 0;
  91. }
  92. /**
  93. * Initialise Ethernet address
  94. *
  95. * @v hw_addr Hardware address
  96. * @v ll_addr Link-layer address
  97. */
  98. void eth_init_addr ( const void *hw_addr, void *ll_addr ) {
  99. memcpy ( ll_addr, hw_addr, ETH_ALEN );
  100. }
  101. /**
  102. * Transcribe Ethernet address
  103. *
  104. * @v ll_addr Link-layer address
  105. * @ret string Link-layer address in human-readable format
  106. */
  107. const char * eth_ntoa ( const void *ll_addr ) {
  108. static char buf[18]; /* "00:00:00:00:00:00" */
  109. const uint8_t *eth_addr = ll_addr;
  110. sprintf ( buf, "%02x:%02x:%02x:%02x:%02x:%02x",
  111. eth_addr[0], eth_addr[1], eth_addr[2],
  112. eth_addr[3], eth_addr[4], eth_addr[5] );
  113. return buf;
  114. }
  115. /**
  116. * Hash multicast address
  117. *
  118. * @v af Address family
  119. * @v net_addr Network-layer address
  120. * @v ll_addr Link-layer address to fill in
  121. * @ret rc Return status code
  122. */
  123. int eth_mc_hash ( unsigned int af, const void *net_addr, void *ll_addr ) {
  124. const uint8_t *net_addr_bytes = net_addr;
  125. uint8_t *ll_addr_bytes = ll_addr;
  126. switch ( af ) {
  127. case AF_INET:
  128. ll_addr_bytes[0] = 0x01;
  129. ll_addr_bytes[1] = 0x00;
  130. ll_addr_bytes[2] = 0x5e;
  131. ll_addr_bytes[3] = net_addr_bytes[1] & 0x7f;
  132. ll_addr_bytes[4] = net_addr_bytes[2];
  133. ll_addr_bytes[5] = net_addr_bytes[3];
  134. return 0;
  135. default:
  136. return -ENOTSUP;
  137. }
  138. }
  139. /**
  140. * Generate Ethernet-compatible compressed link-layer address
  141. *
  142. * @v ll_addr Link-layer address
  143. * @v eth_addr Ethernet-compatible address to fill in
  144. */
  145. int eth_eth_addr ( const void *ll_addr, void *eth_addr ) {
  146. memcpy ( eth_addr, ll_addr, ETH_ALEN );
  147. return 0;
  148. }
  149. /** Ethernet protocol */
  150. struct ll_protocol ethernet_protocol __ll_protocol = {
  151. .name = "Ethernet",
  152. .ll_proto = htons ( ARPHRD_ETHER ),
  153. .hw_addr_len = ETH_ALEN,
  154. .ll_addr_len = ETH_ALEN,
  155. .ll_header_len = ETH_HLEN,
  156. .push = eth_push,
  157. .pull = eth_pull,
  158. .init_addr = eth_init_addr,
  159. .ntoa = eth_ntoa,
  160. .mc_hash = eth_mc_hash,
  161. .eth_addr = eth_eth_addr,
  162. };
  163. /**
  164. * Allocate Ethernet device
  165. *
  166. * @v priv_size Size of driver private data
  167. * @ret netdev Network device, or NULL
  168. */
  169. struct net_device * alloc_etherdev ( size_t priv_size ) {
  170. struct net_device *netdev;
  171. netdev = alloc_netdev ( priv_size );
  172. if ( netdev ) {
  173. netdev->ll_protocol = &ethernet_protocol;
  174. netdev->ll_broadcast = eth_broadcast;
  175. netdev->max_pkt_len = ETH_FRAME_LEN;
  176. }
  177. return netdev;
  178. }
  179. /* Drag in Ethernet slow protocols */
  180. REQUIRE_OBJECT ( eth_slow );