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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. * Transmit Ethernet packet
  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. * Prepends the Ethernet link-layer header and transmits the packet.
  45. */
  46. static int eth_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  47. struct net_protocol *net_protocol, const void *ll_dest ) {
  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, netdev->ll_addr, ETH_ALEN );
  52. ethhdr->h_protocol = net_protocol->net_proto;
  53. /* Hand off to network device */
  54. return netdev_tx ( netdev, iobuf );
  55. }
  56. /**
  57. * Process received Ethernet packet
  58. *
  59. * @v iobuf I/O buffer
  60. * @v netdev Network device
  61. *
  62. * Strips off the Ethernet link-layer header and passes up to the
  63. * network-layer protocol.
  64. */
  65. static int eth_rx ( struct io_buffer *iobuf, struct net_device *netdev ) {
  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. free_iob ( iobuf );
  72. return -EINVAL;
  73. }
  74. /* Strip off Ethernet header */
  75. iob_pull ( iobuf, sizeof ( *ethhdr ) );
  76. /* Hand off to network-layer protocol */
  77. return net_rx ( iobuf, netdev, ethhdr->h_protocol, ethhdr->h_source );
  78. }
  79. /**
  80. * Transcribe Ethernet address
  81. *
  82. * @v ll_addr Link-layer address
  83. * @ret string Link-layer address in human-readable format
  84. */
  85. const char * eth_ntoa ( const void *ll_addr ) {
  86. static char buf[18]; /* "00:00:00:00:00:00" */
  87. const uint8_t *eth_addr = ll_addr;
  88. sprintf ( buf, "%02x:%02x:%02x:%02x:%02x:%02x",
  89. eth_addr[0], eth_addr[1], eth_addr[2],
  90. eth_addr[3], eth_addr[4], eth_addr[5] );
  91. return buf;
  92. }
  93. /** Ethernet protocol */
  94. struct ll_protocol ethernet_protocol __ll_protocol = {
  95. .name = "Ethernet",
  96. .ll_proto = htons ( ARPHRD_ETHER ),
  97. .ll_addr_len = ETH_ALEN,
  98. .ll_header_len = ETH_HLEN,
  99. .ll_broadcast = eth_broadcast,
  100. .tx = eth_tx,
  101. .rx = eth_rx,
  102. .ntoa = eth_ntoa,
  103. };