Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ethernet.c 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. case AF_INET6:
  136. ll_addr_bytes[0] = 0x33;
  137. ll_addr_bytes[1] = 0x33;
  138. memcpy ( &ll_addr_bytes[2], &net_addr_bytes[12], 4 );
  139. return 0;
  140. default:
  141. return -ENOTSUP;
  142. }
  143. }
  144. /**
  145. * Generate Ethernet-compatible compressed link-layer address
  146. *
  147. * @v ll_addr Link-layer address
  148. * @v eth_addr Ethernet-compatible address to fill in
  149. */
  150. int eth_eth_addr ( const void *ll_addr, void *eth_addr ) {
  151. memcpy ( eth_addr, ll_addr, ETH_ALEN );
  152. return 0;
  153. }
  154. /**
  155. * Generate EUI-64 address
  156. *
  157. * @v ll_addr Link-layer address
  158. * @v eui64 EUI-64 address to fill in
  159. * @ret rc Return status code
  160. */
  161. int eth_eui64 ( const void *ll_addr, void *eui64 ) {
  162. memcpy ( ( eui64 + 0 ), ( ll_addr + 0 ), 3 );
  163. memcpy ( ( eui64 + 5 ), ( ll_addr + 3 ), 3 );
  164. *( ( uint16_t * ) ( eui64 + 3 ) ) = htons ( 0xfffe );
  165. return 0;
  166. }
  167. /** Ethernet protocol */
  168. struct ll_protocol ethernet_protocol __ll_protocol = {
  169. .name = "Ethernet",
  170. .ll_proto = htons ( ARPHRD_ETHER ),
  171. .hw_addr_len = ETH_ALEN,
  172. .ll_addr_len = ETH_ALEN,
  173. .ll_header_len = ETH_HLEN,
  174. .push = eth_push,
  175. .pull = eth_pull,
  176. .init_addr = eth_init_addr,
  177. .ntoa = eth_ntoa,
  178. .mc_hash = eth_mc_hash,
  179. .eth_addr = eth_eth_addr,
  180. .eui64 = eth_eui64,
  181. };
  182. /**
  183. * Allocate Ethernet device
  184. *
  185. * @v priv_size Size of driver private data
  186. * @ret netdev Network device, or NULL
  187. */
  188. struct net_device * alloc_etherdev ( size_t priv_size ) {
  189. struct net_device *netdev;
  190. netdev = alloc_netdev ( priv_size );
  191. if ( netdev ) {
  192. netdev->ll_protocol = &ethernet_protocol;
  193. netdev->ll_broadcast = eth_broadcast;
  194. netdev->max_pkt_len = ETH_FRAME_LEN;
  195. }
  196. return netdev;
  197. }
  198. /* Drag in Ethernet slow protocols */
  199. REQUIRE_OBJECT ( eth_slow );