arp.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/net/arp.h>
  19. #include <grub/net/netbuff.h>
  20. #include <grub/mm.h>
  21. #include <grub/net.h>
  22. #include <grub/net/ethernet.h>
  23. #include <grub/net/ip.h>
  24. #include <grub/time.h>
  25. /* ARP header operation codes */
  26. enum
  27. {
  28. ARP_REQUEST = 1,
  29. ARP_REPLY = 2
  30. };
  31. enum
  32. {
  33. /* IANA ARP constant to define hardware type as ethernet. */
  34. GRUB_NET_ARPHRD_ETHERNET = 1
  35. };
  36. struct arppkt {
  37. grub_uint16_t hrd;
  38. grub_uint16_t pro;
  39. grub_uint8_t hln;
  40. grub_uint8_t pln;
  41. grub_uint16_t op;
  42. grub_uint8_t sender_mac[6];
  43. grub_uint32_t sender_ip;
  44. grub_uint8_t recv_mac[6];
  45. grub_uint32_t recv_ip;
  46. } GRUB_PACKED;
  47. static int have_pending;
  48. static grub_uint32_t pending_req;
  49. grub_err_t
  50. grub_net_arp_send_request (struct grub_net_network_level_interface *inf,
  51. const grub_net_network_level_address_t *proto_addr)
  52. {
  53. struct grub_net_buff nb;
  54. struct arppkt *arp_packet;
  55. grub_net_link_level_address_t target_mac_addr;
  56. grub_err_t err;
  57. int i;
  58. grub_uint8_t *nbd;
  59. grub_uint8_t arp_data[128];
  60. if (proto_addr->type != GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4)
  61. return grub_error (GRUB_ERR_BUG, "unsupported address family");
  62. /* Build a request packet. */
  63. nb.head = arp_data;
  64. nb.end = arp_data + sizeof (arp_data);
  65. grub_netbuff_clear (&nb);
  66. grub_netbuff_reserve (&nb, 128);
  67. err = grub_netbuff_push (&nb, sizeof (*arp_packet));
  68. if (err)
  69. return err;
  70. arp_packet = (struct arppkt *) nb.data;
  71. arp_packet->hrd = grub_cpu_to_be16_compile_time (GRUB_NET_ARPHRD_ETHERNET);
  72. arp_packet->hln = 6;
  73. arp_packet->pro = grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP);
  74. arp_packet->pln = 4;
  75. arp_packet->op = grub_cpu_to_be16_compile_time (ARP_REQUEST);
  76. /* Sender hardware address. */
  77. grub_memcpy (arp_packet->sender_mac, &inf->hwaddress.mac, 6);
  78. arp_packet->sender_ip = inf->address.ipv4;
  79. grub_memset (arp_packet->recv_mac, 0, 6);
  80. arp_packet->recv_ip = proto_addr->ipv4;
  81. /* Target protocol address */
  82. grub_memset (&target_mac_addr.mac, 0xff, 6);
  83. nbd = nb.data;
  84. send_ethernet_packet (inf, &nb, target_mac_addr, GRUB_NET_ETHERTYPE_ARP);
  85. for (i = 0; i < GRUB_NET_TRIES; i++)
  86. {
  87. if (grub_net_link_layer_resolve_check (inf, proto_addr))
  88. return GRUB_ERR_NONE;
  89. pending_req = proto_addr->ipv4;
  90. have_pending = 0;
  91. grub_net_poll_cards (GRUB_NET_INTERVAL + (i * GRUB_NET_INTERVAL_ADDITION),
  92. &have_pending);
  93. if (grub_net_link_layer_resolve_check (inf, proto_addr))
  94. return GRUB_ERR_NONE;
  95. nb.data = nbd;
  96. send_ethernet_packet (inf, &nb, target_mac_addr, GRUB_NET_ETHERTYPE_ARP);
  97. }
  98. return GRUB_ERR_NONE;
  99. }
  100. grub_err_t
  101. grub_net_arp_receive (struct grub_net_buff *nb, struct grub_net_card *card,
  102. grub_uint16_t *vlantag)
  103. {
  104. struct arppkt *arp_packet = (struct arppkt *) nb->data;
  105. grub_net_network_level_address_t sender_addr, target_addr;
  106. grub_net_link_level_address_t sender_mac_addr;
  107. struct grub_net_network_level_interface *inf;
  108. if (arp_packet->pro != grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP)
  109. || arp_packet->pln != 4 || arp_packet->hln != 6
  110. || nb->tail - nb->data < (int) sizeof (*arp_packet))
  111. return GRUB_ERR_NONE;
  112. sender_addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
  113. target_addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
  114. sender_addr.ipv4 = arp_packet->sender_ip;
  115. target_addr.ipv4 = arp_packet->recv_ip;
  116. sender_addr.option = 0;
  117. target_addr.option = 0;
  118. if (arp_packet->sender_ip == pending_req)
  119. have_pending = 1;
  120. sender_mac_addr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
  121. grub_memcpy (sender_mac_addr.mac, arp_packet->sender_mac,
  122. sizeof (sender_mac_addr.mac));
  123. grub_net_link_layer_add_address (card, &sender_addr, &sender_mac_addr, 1);
  124. FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
  125. {
  126. /* Verify vlantag id */
  127. if (inf->card == card && inf->vlantag != *vlantag)
  128. {
  129. grub_dprintf ("net", "invalid vlantag! %x != %x\n",
  130. inf->vlantag, *vlantag);
  131. break;
  132. }
  133. /* Am I the protocol address target? */
  134. if (grub_net_addr_cmp (&inf->address, &target_addr) == 0
  135. && arp_packet->op == grub_cpu_to_be16_compile_time (ARP_REQUEST))
  136. {
  137. grub_net_link_level_address_t target;
  138. struct grub_net_buff nb_reply;
  139. struct arppkt *arp_reply;
  140. grub_uint8_t arp_data[128];
  141. grub_err_t err;
  142. nb_reply.head = arp_data;
  143. nb_reply.end = arp_data + sizeof (arp_data);
  144. grub_netbuff_clear (&nb_reply);
  145. grub_netbuff_reserve (&nb_reply, 128);
  146. err = grub_netbuff_push (&nb_reply, sizeof (*arp_packet));
  147. if (err)
  148. return err;
  149. arp_reply = (struct arppkt *) nb_reply.data;
  150. arp_reply->hrd = grub_cpu_to_be16_compile_time (GRUB_NET_ARPHRD_ETHERNET);
  151. arp_reply->pro = grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP);
  152. arp_reply->pln = 4;
  153. arp_reply->hln = 6;
  154. arp_reply->op = grub_cpu_to_be16_compile_time (ARP_REPLY);
  155. arp_reply->sender_ip = arp_packet->recv_ip;
  156. arp_reply->recv_ip = arp_packet->sender_ip;
  157. arp_reply->hln = 6;
  158. target.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
  159. grub_memcpy (target.mac, arp_packet->sender_mac, 6);
  160. grub_memcpy (arp_reply->sender_mac, inf->hwaddress.mac, 6);
  161. grub_memcpy (arp_reply->recv_mac, arp_packet->sender_mac, 6);
  162. /* Change operation to REPLY and send packet */
  163. send_ethernet_packet (inf, &nb_reply, target, GRUB_NET_ETHERTYPE_ARP);
  164. }
  165. }
  166. return GRUB_ERR_NONE;
  167. }