Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ethernet.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 <gpxe/if_arp.h>
  26. #include <gpxe/if_ether.h>
  27. #include <gpxe/in.h>
  28. #include <gpxe/netdevice.h>
  29. #include <gpxe/iobuf.h>
  30. #include <gpxe/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. static int eth_push ( struct net_device *netdev __unused,
  49. struct io_buffer *iobuf, const void *ll_dest,
  50. const void *ll_source, 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 rc Return status code
  67. */
  68. static int eth_pull ( struct net_device *netdev __unused,
  69. struct io_buffer *iobuf, const void **ll_dest,
  70. const void **ll_source, uint16_t *net_proto ) {
  71. struct ethhdr *ethhdr = iobuf->data;
  72. /* Sanity check */
  73. if ( iob_len ( iobuf ) < sizeof ( *ethhdr ) ) {
  74. DBG ( "Ethernet packet too short (%zd bytes)\n",
  75. iob_len ( iobuf ) );
  76. return -EINVAL;
  77. }
  78. /* Strip off Ethernet header */
  79. iob_pull ( iobuf, sizeof ( *ethhdr ) );
  80. /* Fill in required fields */
  81. *ll_dest = ethhdr->h_dest;
  82. *ll_source = ethhdr->h_source;
  83. *net_proto = ethhdr->h_protocol;
  84. return 0;
  85. }
  86. /**
  87. * Initialise Ethernet address
  88. *
  89. * @v hw_addr Hardware address
  90. * @v ll_addr Link-layer address
  91. */
  92. void eth_init_addr ( const void *hw_addr, void *ll_addr ) {
  93. memcpy ( ll_addr, hw_addr, ETH_ALEN );
  94. }
  95. /**
  96. * Transcribe Ethernet address
  97. *
  98. * @v ll_addr Link-layer address
  99. * @ret string Link-layer address in human-readable format
  100. */
  101. const char * eth_ntoa ( const void *ll_addr ) {
  102. static char buf[18]; /* "00:00:00:00:00:00" */
  103. const uint8_t *eth_addr = ll_addr;
  104. sprintf ( buf, "%02x:%02x:%02x:%02x:%02x:%02x",
  105. eth_addr[0], eth_addr[1], eth_addr[2],
  106. eth_addr[3], eth_addr[4], eth_addr[5] );
  107. return buf;
  108. }
  109. /**
  110. * Hash multicast address
  111. *
  112. * @v af Address family
  113. * @v net_addr Network-layer address
  114. * @v ll_addr Link-layer address to fill in
  115. * @ret rc Return status code
  116. */
  117. int eth_mc_hash ( unsigned int af, const void *net_addr, void *ll_addr ) {
  118. const uint8_t *net_addr_bytes = net_addr;
  119. uint8_t *ll_addr_bytes = ll_addr;
  120. switch ( af ) {
  121. case AF_INET:
  122. ll_addr_bytes[0] = 0x01;
  123. ll_addr_bytes[1] = 0x00;
  124. ll_addr_bytes[2] = 0x5e;
  125. ll_addr_bytes[3] = net_addr_bytes[1] & 0x7f;
  126. ll_addr_bytes[4] = net_addr_bytes[2];
  127. ll_addr_bytes[5] = net_addr_bytes[3];
  128. return 0;
  129. default:
  130. return -ENOTSUP;
  131. }
  132. }
  133. /** Ethernet protocol */
  134. struct ll_protocol ethernet_protocol __ll_protocol = {
  135. .name = "Ethernet",
  136. .ll_proto = htons ( ARPHRD_ETHER ),
  137. .hw_addr_len = ETH_ALEN,
  138. .ll_addr_len = ETH_ALEN,
  139. .ll_header_len = ETH_HLEN,
  140. .push = eth_push,
  141. .pull = eth_pull,
  142. .init_addr = eth_init_addr,
  143. .ntoa = eth_ntoa,
  144. .mc_hash = eth_mc_hash,
  145. };
  146. /**
  147. * Allocate Ethernet device
  148. *
  149. * @v priv_size Size of driver private data
  150. * @ret netdev Network device, or NULL
  151. */
  152. struct net_device * alloc_etherdev ( size_t priv_size ) {
  153. struct net_device *netdev;
  154. netdev = alloc_netdev ( priv_size );
  155. if ( netdev ) {
  156. netdev->ll_protocol = &ethernet_protocol;
  157. netdev->ll_broadcast = eth_broadcast;
  158. netdev->max_pkt_len = ETH_FRAME_LEN;
  159. }
  160. return netdev;
  161. }