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.2KB

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