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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <byteswap.h>
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include <gpxe/if_arp.h>
  25. #include <gpxe/if_ether.h>
  26. #include <gpxe/in.h>
  27. #include <gpxe/netdevice.h>
  28. #include <gpxe/iobuf.h>
  29. #include <gpxe/ethernet.h>
  30. /** @file
  31. *
  32. * Ethernet protocol
  33. *
  34. */
  35. /** Ethernet broadcast MAC address */
  36. static uint8_t eth_broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  37. /**
  38. * Add Ethernet link-layer header
  39. *
  40. * @v iobuf I/O buffer
  41. * @v ll_dest Link-layer destination address
  42. * @v ll_source Source link-layer address
  43. * @v net_proto Network-layer protocol, in network-byte order
  44. * @ret rc Return status code
  45. */
  46. static int eth_push ( struct io_buffer *iobuf, const void *ll_dest,
  47. const void *ll_source, uint16_t net_proto ) {
  48. struct ethhdr *ethhdr = iob_push ( iobuf, sizeof ( *ethhdr ) );
  49. /* Build Ethernet header */
  50. memcpy ( ethhdr->h_dest, ll_dest, ETH_ALEN );
  51. memcpy ( ethhdr->h_source, ll_source, ETH_ALEN );
  52. ethhdr->h_protocol = net_proto;
  53. return 0;
  54. }
  55. /**
  56. * Remove Ethernet link-layer header
  57. *
  58. * @v iobuf I/O buffer
  59. * @ret ll_dest Link-layer destination address
  60. * @ret ll_source Source link-layer address
  61. * @ret net_proto Network-layer protocol, in network-byte order
  62. * @ret rc Return status code
  63. */
  64. static int eth_pull ( struct io_buffer *iobuf, const void **ll_dest,
  65. const void **ll_source, uint16_t *net_proto ) {
  66. struct ethhdr *ethhdr = iobuf->data;
  67. /* Sanity check */
  68. if ( iob_len ( iobuf ) < sizeof ( *ethhdr ) ) {
  69. DBG ( "Ethernet packet too short (%zd bytes)\n",
  70. iob_len ( iobuf ) );
  71. return -EINVAL;
  72. }
  73. /* Strip off Ethernet header */
  74. iob_pull ( iobuf, sizeof ( *ethhdr ) );
  75. /* Fill in required fields */
  76. *ll_dest = ethhdr->h_dest;
  77. *ll_source = ethhdr->h_source;
  78. *net_proto = ethhdr->h_protocol;
  79. return 0;
  80. }
  81. /**
  82. * Transcribe Ethernet address
  83. *
  84. * @v ll_addr Link-layer address
  85. * @ret string Link-layer address in human-readable format
  86. */
  87. const char * eth_ntoa ( const void *ll_addr ) {
  88. static char buf[18]; /* "00:00:00:00:00:00" */
  89. const uint8_t *eth_addr = ll_addr;
  90. sprintf ( buf, "%02x:%02x:%02x:%02x:%02x:%02x",
  91. eth_addr[0], eth_addr[1], eth_addr[2],
  92. eth_addr[3], eth_addr[4], eth_addr[5] );
  93. return buf;
  94. }
  95. /**
  96. * Hash multicast address
  97. *
  98. * @v af Address family
  99. * @v net_addr Network-layer address
  100. * @v ll_addr Link-layer address to fill in
  101. * @ret rc Return status code
  102. */
  103. static int eth_mc_hash ( unsigned int af, const void *net_addr,
  104. void *ll_addr ) {
  105. const uint8_t *net_addr_bytes = net_addr;
  106. uint8_t *ll_addr_bytes = ll_addr;
  107. switch ( af ) {
  108. case AF_INET:
  109. ll_addr_bytes[0] = 0x01;
  110. ll_addr_bytes[1] = 0x00;
  111. ll_addr_bytes[2] = 0x5e;
  112. ll_addr_bytes[3] = net_addr_bytes[1] & 0x7f;
  113. ll_addr_bytes[4] = net_addr_bytes[2];
  114. ll_addr_bytes[5] = net_addr_bytes[3];
  115. default:
  116. return -ENOTSUP;
  117. }
  118. }
  119. /** Ethernet protocol */
  120. struct ll_protocol ethernet_protocol __ll_protocol = {
  121. .name = "Ethernet",
  122. .ll_proto = htons ( ARPHRD_ETHER ),
  123. .ll_addr_len = ETH_ALEN,
  124. .ll_header_len = ETH_HLEN,
  125. .ll_broadcast = eth_broadcast,
  126. .push = eth_push,
  127. .pull = eth_pull,
  128. .ntoa = eth_ntoa,
  129. .mc_hash = eth_mc_hash,
  130. };