Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ethernet.c 6.1KB

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