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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. * Transcribe Ethernet address
  88. *
  89. * @v ll_addr Link-layer address
  90. * @ret string Link-layer address in human-readable format
  91. */
  92. const char * eth_ntoa ( const void *ll_addr ) {
  93. static char buf[18]; /* "00:00:00:00:00:00" */
  94. const uint8_t *eth_addr = ll_addr;
  95. sprintf ( buf, "%02x:%02x:%02x:%02x:%02x:%02x",
  96. eth_addr[0], eth_addr[1], eth_addr[2],
  97. eth_addr[3], eth_addr[4], eth_addr[5] );
  98. return buf;
  99. }
  100. /**
  101. * Hash multicast address
  102. *
  103. * @v af Address family
  104. * @v net_addr Network-layer address
  105. * @v ll_addr Link-layer address to fill in
  106. * @ret rc Return status code
  107. */
  108. static int eth_mc_hash ( unsigned int af, const void *net_addr,
  109. void *ll_addr ) {
  110. const uint8_t *net_addr_bytes = net_addr;
  111. uint8_t *ll_addr_bytes = ll_addr;
  112. switch ( af ) {
  113. case AF_INET:
  114. ll_addr_bytes[0] = 0x01;
  115. ll_addr_bytes[1] = 0x00;
  116. ll_addr_bytes[2] = 0x5e;
  117. ll_addr_bytes[3] = net_addr_bytes[1] & 0x7f;
  118. ll_addr_bytes[4] = net_addr_bytes[2];
  119. ll_addr_bytes[5] = net_addr_bytes[3];
  120. return 0;
  121. default:
  122. return -ENOTSUP;
  123. }
  124. }
  125. /** Ethernet protocol */
  126. struct ll_protocol ethernet_protocol __ll_protocol = {
  127. .name = "Ethernet",
  128. .ll_proto = htons ( ARPHRD_ETHER ),
  129. .ll_addr_len = ETH_ALEN,
  130. .ll_header_len = ETH_HLEN,
  131. .ll_broadcast = eth_broadcast,
  132. .push = eth_push,
  133. .pull = eth_pull,
  134. .ntoa = eth_ntoa,
  135. .mc_hash = eth_mc_hash,
  136. };