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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <string.h>
  20. #include <byteswap.h>
  21. #include <assert.h>
  22. #include <gpxe/if_arp.h>
  23. #include <gpxe/if_ether.h>
  24. #include <gpxe/netdevice.h>
  25. #include <gpxe/pkbuff.h>
  26. #include <gpxe/arp.h>
  27. #include <gpxe/ethernet.h>
  28. /** @file
  29. *
  30. * Ethernet protocol
  31. *
  32. */
  33. /** Ethernet broadcast MAC address */
  34. static uint8_t eth_broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  35. /**
  36. * Perform Ethernet routing
  37. *
  38. * @v nethdr Generic network-layer header
  39. * @ret llhdr Generic link-layer header
  40. * @ret rc Return status code
  41. *
  42. * Constructs the generic link-layer header based on the generic
  43. * network-layer header, i.e. maps network-layer addresses (e.g. IPv4
  44. * addresses) to MAC addresses.
  45. *
  46. * If the destination MAC address cannot be determined, an ARP request
  47. * is sent for the requested network-layer address and -ENOENT is
  48. * returned.
  49. */
  50. static int eth_route ( const struct net_header *nethdr,
  51. struct ll_header *llhdr ) {
  52. int rc;
  53. /* Work out the destination MAC address */
  54. if ( nethdr->dest_flags & NETADDR_FL_RAW ) {
  55. memcpy ( llhdr->dest_ll_addr, nethdr->dest_net_addr, ETH_ALEN);
  56. } else if ( nethdr->dest_flags & NETADDR_FL_BROADCAST ) {
  57. memcpy ( llhdr->dest_ll_addr, eth_broadcast, ETH_ALEN );
  58. } else if ( nethdr->dest_flags & NETADDR_FL_MULTICAST ) {
  59. /* IP multicast is a special case; there exists a
  60. * direct mapping from IP address to MAC address
  61. */
  62. assert ( nethdr->net_protocol->net_proto == htons(ETH_P_IP) );
  63. llhdr->dest_ll_addr[0] = 0x01;
  64. llhdr->dest_ll_addr[1] = 0x00;
  65. llhdr->dest_ll_addr[2] = 0x5e;
  66. llhdr->dest_ll_addr[3] = nethdr->dest_net_addr[1] & 0x7f;
  67. llhdr->dest_ll_addr[4] = nethdr->dest_net_addr[2];
  68. llhdr->dest_ll_addr[5] = nethdr->dest_net_addr[3];
  69. } else {
  70. /* Otherwise, look up the address using ARP */
  71. if ( ( rc = arp_resolve ( nethdr, llhdr ) ) != 0 )
  72. return rc;
  73. }
  74. return 0;
  75. }
  76. /**
  77. * Fill in Ethernet link-layer header
  78. *
  79. * @v pkb Packet buffer
  80. * @v llhdr Generic link-layer header
  81. *
  82. * Fills in the Ethernet link-layer header in the packet buffer based
  83. * on information in the generic link-layer header.
  84. */
  85. static void eth_fill_llh ( const struct ll_header *llhdr,
  86. struct pk_buff *pkb ) {
  87. struct ethhdr *ethhdr = pkb->data;
  88. memcpy ( ethhdr->h_dest, llhdr->dest_ll_addr, ETH_ALEN );
  89. memcpy ( ethhdr->h_source, llhdr->source_ll_addr, ETH_ALEN );
  90. ethhdr->h_protocol = llhdr->net_proto;
  91. }
  92. /**
  93. * Parse Ethernet link-layer header
  94. *
  95. * @v pkb Packet buffer
  96. * @v llhdr Generic link-layer header
  97. *
  98. * Fills in the generic link-layer header based on information in the
  99. * Ethernet link-layer header in the packet buffer.
  100. */
  101. static void eth_parse_llh ( const struct pk_buff *pkb,
  102. struct ll_header *llhdr ) {
  103. struct ethhdr *ethhdr = pkb->data;
  104. memcpy ( llhdr->dest_ll_addr, ethhdr->h_dest, ETH_ALEN );
  105. memcpy ( llhdr->source_ll_addr, ethhdr->h_source, ETH_ALEN );
  106. llhdr->net_proto = ethhdr->h_protocol;
  107. if ( memcmp ( ethhdr->h_dest, eth_broadcast, ETH_ALEN ) == 0 ) {
  108. llhdr->dest_flags = NETADDR_FL_BROADCAST;
  109. } else if ( ethhdr->h_dest[0] & 0x01 ) {
  110. llhdr->dest_flags = NETADDR_FL_MULTICAST;
  111. } else {
  112. llhdr->dest_flags = 0;
  113. }
  114. }
  115. /** Ethernet protocol */
  116. struct ll_protocol ethernet_protocol = {
  117. .ll_proto = htons ( ARPHRD_ETHER ),
  118. .ll_addr_len = ETH_ALEN,
  119. .ll_header_len = ETH_HLEN,
  120. .route = eth_route,
  121. .fill_llh = eth_fill_llh,
  122. .parse_llh = eth_parse_llh,
  123. };
  124. LL_PROTOCOL ( ethernet_protocol );