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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/netdevice.h>
  27. #include <gpxe/iobuf.h>
  28. #include <gpxe/ethernet.h>
  29. /** @file
  30. *
  31. * Ethernet protocol
  32. *
  33. */
  34. /** Ethernet broadcast MAC address */
  35. static uint8_t eth_broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  36. /**
  37. * Add Ethernet link-layer header
  38. *
  39. * @v iobuf I/O buffer
  40. * @v netdev Network device
  41. * @v net_protocol Network-layer protocol
  42. * @v ll_dest Link-layer destination address
  43. */
  44. static int eth_push ( struct io_buffer *iobuf, struct net_device *netdev,
  45. struct net_protocol *net_protocol,
  46. const void *ll_dest ) {
  47. struct ethhdr *ethhdr = iob_push ( iobuf, sizeof ( *ethhdr ) );
  48. /* Build Ethernet header */
  49. memcpy ( ethhdr->h_dest, ll_dest, ETH_ALEN );
  50. memcpy ( ethhdr->h_source, netdev->ll_addr, ETH_ALEN );
  51. ethhdr->h_protocol = net_protocol->net_proto;
  52. return 0;
  53. }
  54. /**
  55. * Remove Ethernet link-layer header
  56. *
  57. * @v iobuf I/O buffer
  58. * @v netdev Network device
  59. * @v net_proto Network-layer protocol, in network-byte order
  60. * @v ll_source Source link-layer address
  61. * @ret rc Return status code
  62. */
  63. static int eth_pull ( struct io_buffer *iobuf,
  64. struct net_device *netdev __unused,
  65. uint16_t *net_proto, const void **ll_source ) {
  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. *net_proto = ethhdr->h_protocol;
  77. *ll_source = ethhdr->h_source;
  78. return 0;
  79. }
  80. /**
  81. * Transcribe Ethernet address
  82. *
  83. * @v ll_addr Link-layer address
  84. * @ret string Link-layer address in human-readable format
  85. */
  86. const char * eth_ntoa ( const void *ll_addr ) {
  87. static char buf[18]; /* "00:00:00:00:00:00" */
  88. const uint8_t *eth_addr = ll_addr;
  89. sprintf ( buf, "%02x:%02x:%02x:%02x:%02x:%02x",
  90. eth_addr[0], eth_addr[1], eth_addr[2],
  91. eth_addr[3], eth_addr[4], eth_addr[5] );
  92. return buf;
  93. }
  94. /** Ethernet protocol */
  95. struct ll_protocol ethernet_protocol __ll_protocol = {
  96. .name = "Ethernet",
  97. .ll_proto = htons ( ARPHRD_ETHER ),
  98. .ll_addr_len = ETH_ALEN,
  99. .ll_header_len = ETH_HLEN,
  100. .ll_broadcast = eth_broadcast,
  101. .push = eth_push,
  102. .pull = eth_pull,
  103. .ntoa = eth_ntoa,
  104. };