ethernet.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010,2011 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/misc.h>
  19. #include <grub/mm.h>
  20. #include <grub/env.h>
  21. #include <grub/net/ethernet.h>
  22. #include <grub/net/ip.h>
  23. #include <grub/net/arp.h>
  24. #include <grub/net/netbuff.h>
  25. #include <grub/net.h>
  26. #include <grub/time.h>
  27. #include <grub/net/arp.h>
  28. #define LLCADDRMASK 0x7f
  29. struct etherhdr
  30. {
  31. grub_uint8_t dst[6];
  32. grub_uint8_t src[6];
  33. grub_uint16_t type;
  34. } GRUB_PACKED;
  35. struct llchdr
  36. {
  37. grub_uint8_t dsap;
  38. grub_uint8_t ssap;
  39. grub_uint8_t ctrl;
  40. } GRUB_PACKED;
  41. struct snaphdr
  42. {
  43. grub_uint8_t oui[3];
  44. grub_uint16_t type;
  45. } GRUB_PACKED;
  46. grub_err_t
  47. send_ethernet_packet (struct grub_net_network_level_interface *inf,
  48. struct grub_net_buff *nb,
  49. grub_net_link_level_address_t target_addr,
  50. grub_net_ethertype_t ethertype)
  51. {
  52. struct etherhdr *eth;
  53. grub_err_t err;
  54. grub_uint8_t etherhdr_size;
  55. grub_uint16_t vlantag_id = grub_cpu_to_be16_compile_time (VLANTAG_IDENTIFIER);
  56. etherhdr_size = sizeof (*eth);
  57. COMPILE_TIME_ASSERT (sizeof (*eth) + 4 < GRUB_NET_MAX_LINK_HEADER_SIZE);
  58. /* Increase ethernet header in case of vlantag */
  59. if (inf->vlantag != 0)
  60. etherhdr_size += 4;
  61. err = grub_netbuff_push (nb, etherhdr_size);
  62. if (err)
  63. return err;
  64. eth = (struct etherhdr *) nb->data;
  65. grub_memcpy (eth->dst, target_addr.mac, 6);
  66. grub_memcpy (eth->src, inf->hwaddress.mac, 6);
  67. eth->type = grub_cpu_to_be16 (ethertype);
  68. if (!inf->card->opened)
  69. {
  70. err = GRUB_ERR_NONE;
  71. if (inf->card->driver->open)
  72. err = inf->card->driver->open (inf->card);
  73. if (err)
  74. return err;
  75. inf->card->opened = 1;
  76. }
  77. /* Check and add a vlan-tag if needed. */
  78. if (inf->vlantag != 0)
  79. {
  80. /* Move eth type to the right */
  81. grub_memcpy ((char *) nb->data + etherhdr_size - 2,
  82. (char *) nb->data + etherhdr_size - 6, 2);
  83. /* Add the tag in the middle */
  84. grub_uint16_t vlan = grub_cpu_to_be16 (inf->vlantag);
  85. grub_memcpy ((char *) nb->data + etherhdr_size - 6, &vlantag_id, 2);
  86. grub_memcpy ((char *) nb->data + etherhdr_size - 4, &vlan, 2);
  87. }
  88. return inf->card->driver->send (inf->card, nb);
  89. }
  90. grub_err_t
  91. grub_net_recv_ethernet_packet (struct grub_net_buff *nb,
  92. struct grub_net_card *card)
  93. {
  94. struct etherhdr *eth;
  95. struct llchdr *llch;
  96. struct snaphdr *snaph;
  97. grub_net_ethertype_t type;
  98. grub_net_link_level_address_t hwaddress;
  99. grub_net_link_level_address_t src_hwaddress;
  100. grub_err_t err;
  101. grub_uint8_t etherhdr_size = sizeof (*eth);
  102. grub_uint16_t vlantag = 0;
  103. /* Check if a vlan-tag is present. If so, the ethernet header is 4 bytes */
  104. /* longer than the original one. The vlantag id is extracted and the header */
  105. /* is reseted to the original size. */
  106. if (grub_get_unaligned16 (nb->data + etherhdr_size - 2) == grub_cpu_to_be16_compile_time (VLANTAG_IDENTIFIER))
  107. {
  108. vlantag = grub_be_to_cpu16 (grub_get_unaligned16 (nb->data + etherhdr_size));
  109. etherhdr_size += 4;
  110. /* Move eth type to the original position */
  111. grub_memcpy((char *) nb->data + etherhdr_size - 6,
  112. (char *) nb->data + etherhdr_size - 2, 2);
  113. }
  114. eth = (struct etherhdr *) nb->data;
  115. type = grub_be_to_cpu16 (eth->type);
  116. err = grub_netbuff_pull (nb, etherhdr_size);
  117. if (err)
  118. return err;
  119. if (type <= 1500)
  120. {
  121. llch = (struct llchdr *) nb->data;
  122. type = llch->dsap & LLCADDRMASK;
  123. if (llch->dsap == 0xaa && llch->ssap == 0xaa && llch->ctrl == 0x3)
  124. {
  125. err = grub_netbuff_pull (nb, sizeof (*llch));
  126. if (err)
  127. return err;
  128. snaph = (struct snaphdr *) nb->data;
  129. type = snaph->type;
  130. }
  131. }
  132. hwaddress.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
  133. grub_memcpy (hwaddress.mac, eth->dst, sizeof (hwaddress.mac));
  134. src_hwaddress.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
  135. grub_memcpy (src_hwaddress.mac, eth->src, sizeof (src_hwaddress.mac));
  136. switch (type)
  137. {
  138. /* ARP packet. */
  139. case GRUB_NET_ETHERTYPE_ARP:
  140. grub_net_arp_receive (nb, card, &vlantag);
  141. grub_netbuff_free (nb);
  142. return GRUB_ERR_NONE;
  143. /* IP packet. */
  144. case GRUB_NET_ETHERTYPE_IP:
  145. case GRUB_NET_ETHERTYPE_IP6:
  146. return grub_net_recv_ip_packets (nb, card, &hwaddress, &src_hwaddress,
  147. &vlantag);
  148. }
  149. grub_netbuff_free (nb);
  150. return GRUB_ERR_NONE;
  151. }