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.

arp.c 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <byteswap.h>
  28. #include <errno.h>
  29. #include <ipxe/if_ether.h>
  30. #include <ipxe/if_arp.h>
  31. #include <ipxe/iobuf.h>
  32. #include <ipxe/netdevice.h>
  33. #include <ipxe/neighbour.h>
  34. #include <ipxe/arp.h>
  35. /** @file
  36. *
  37. * Address Resolution Protocol
  38. *
  39. * This file implements the address resolution protocol as defined in
  40. * RFC826. The implementation is media-independent and
  41. * protocol-independent; it is not limited to Ethernet or to IPv4.
  42. *
  43. */
  44. struct net_protocol arp_protocol __net_protocol;
  45. /**
  46. * Transmit ARP request
  47. *
  48. * @v netdev Network device
  49. * @v net_protocol Network-layer protocol
  50. * @v net_dest Destination network-layer address
  51. * @v net_source Source network-layer address
  52. * @ret rc Return status code
  53. */
  54. int arp_tx_request ( struct net_device *netdev,
  55. struct net_protocol *net_protocol,
  56. const void *net_dest, const void *net_source ) {
  57. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  58. struct io_buffer *iobuf;
  59. struct arphdr *arphdr;
  60. int rc;
  61. /* Allocate ARP packet */
  62. iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( *arphdr ) +
  63. ( 2 * ( MAX_LL_ADDR_LEN + MAX_NET_ADDR_LEN ) ) );
  64. if ( ! iobuf )
  65. return -ENOMEM;
  66. iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
  67. /* Build up ARP request */
  68. arphdr = iob_put ( iobuf, sizeof ( *arphdr ) );
  69. arphdr->ar_hrd = ll_protocol->ll_proto;
  70. arphdr->ar_hln = ll_protocol->ll_addr_len;
  71. arphdr->ar_pro = net_protocol->net_proto;
  72. arphdr->ar_pln = net_protocol->net_addr_len;
  73. arphdr->ar_op = htons ( ARPOP_REQUEST );
  74. memcpy ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
  75. netdev->ll_addr, ll_protocol->ll_addr_len );
  76. memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
  77. net_source, net_protocol->net_addr_len );
  78. memset ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
  79. 0, ll_protocol->ll_addr_len );
  80. memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
  81. net_dest, net_protocol->net_addr_len );
  82. /* Transmit ARP request */
  83. if ( ( rc = net_tx ( iobuf, netdev, &arp_protocol,
  84. netdev->ll_broadcast, netdev->ll_addr ) ) != 0 ) {
  85. DBGC ( netdev, "ARP %s %s %s could not transmit request: %s\n",
  86. netdev->name, net_protocol->name,
  87. net_protocol->ntoa ( net_dest ), strerror ( rc ) );
  88. return rc;
  89. }
  90. return 0;
  91. }
  92. /** ARP neighbour discovery protocol */
  93. struct neighbour_discovery arp_discovery = {
  94. .name = "ARP",
  95. .tx_request = arp_tx_request,
  96. };
  97. /**
  98. * Identify ARP protocol
  99. *
  100. * @v net_proto Network-layer protocol, in network-endian order
  101. * @ret arp_net_protocol ARP protocol, or NULL
  102. *
  103. */
  104. static struct arp_net_protocol * arp_find_protocol ( uint16_t net_proto ) {
  105. struct arp_net_protocol *arp_net_protocol;
  106. for_each_table_entry ( arp_net_protocol, ARP_NET_PROTOCOLS ) {
  107. if ( arp_net_protocol->net_protocol->net_proto == net_proto )
  108. return arp_net_protocol;
  109. }
  110. return NULL;
  111. }
  112. /**
  113. * Process incoming ARP packets
  114. *
  115. * @v iobuf I/O buffer
  116. * @v netdev Network device
  117. * @v ll_source Link-layer source address
  118. * @v flags Packet flags
  119. * @ret rc Return status code
  120. */
  121. static int arp_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  122. const void *ll_dest __unused,
  123. const void *ll_source __unused,
  124. unsigned int flags __unused ) {
  125. struct arphdr *arphdr = iobuf->data;
  126. struct arp_net_protocol *arp_net_protocol;
  127. struct net_protocol *net_protocol;
  128. struct ll_protocol *ll_protocol;
  129. size_t len = iob_len ( iobuf );
  130. int rc;
  131. /* Sanity check */
  132. if ( ( len < sizeof ( *arphdr ) ) || ( len < arp_len ( arphdr ) ) ) {
  133. rc = -EINVAL;
  134. goto done;
  135. }
  136. /* Identify network-layer and link-layer protocols */
  137. arp_net_protocol = arp_find_protocol ( arphdr->ar_pro );
  138. if ( ! arp_net_protocol ) {
  139. rc = -EPROTONOSUPPORT;
  140. goto done;
  141. }
  142. net_protocol = arp_net_protocol->net_protocol;
  143. ll_protocol = netdev->ll_protocol;
  144. /* Sanity checks */
  145. if ( ( arphdr->ar_hrd != ll_protocol->ll_proto ) ||
  146. ( arphdr->ar_hln != ll_protocol->ll_addr_len ) ||
  147. ( arphdr->ar_pln != net_protocol->net_addr_len ) ) {
  148. rc = -EINVAL;
  149. goto done;
  150. }
  151. /* Update neighbour cache entry for this sender, if any */
  152. neighbour_update ( netdev, net_protocol, arp_sender_pa ( arphdr ),
  153. arp_sender_ha ( arphdr ) );
  154. /* If it's not a request, there's nothing more to do */
  155. if ( arphdr->ar_op != htons ( ARPOP_REQUEST ) ) {
  156. rc = 0;
  157. goto done;
  158. }
  159. /* See if we own the target protocol address */
  160. if ( arp_net_protocol->check ( netdev, arp_target_pa ( arphdr ) ) != 0){
  161. rc = 0;
  162. goto done;
  163. }
  164. /* Change request to a reply */
  165. DBGC2 ( netdev, "ARP %s %s %s reply => %s %s\n",
  166. netdev->name, net_protocol->name,
  167. net_protocol->ntoa ( arp_target_pa ( arphdr ) ),
  168. ll_protocol->name, ll_protocol->ntoa ( netdev->ll_addr ) );
  169. arphdr->ar_op = htons ( ARPOP_REPLY );
  170. memswap ( arp_sender_ha ( arphdr ), arp_target_ha ( arphdr ),
  171. arphdr->ar_hln + arphdr->ar_pln );
  172. memcpy ( arp_sender_ha ( arphdr ), netdev->ll_addr, arphdr->ar_hln );
  173. /* Send reply */
  174. if ( ( rc = net_tx ( iob_disown ( iobuf ), netdev, &arp_protocol,
  175. arp_target_ha ( arphdr ),
  176. netdev->ll_addr ) ) != 0 ) {
  177. DBGC ( netdev, "ARP %s %s %s could not transmit reply: %s\n",
  178. netdev->name, net_protocol->name,
  179. net_protocol->ntoa ( arp_target_pa ( arphdr ) ),
  180. strerror ( rc ) );
  181. goto done;
  182. }
  183. /* Success */
  184. rc = 0;
  185. done:
  186. free_iob ( iobuf );
  187. return rc;
  188. }
  189. /**
  190. * Transcribe ARP address
  191. *
  192. * @v net_addr ARP address
  193. * @ret string "<ARP>"
  194. *
  195. * This operation is meaningless for the ARP protocol.
  196. */
  197. static const char * arp_ntoa ( const void *net_addr __unused ) {
  198. return "<ARP>";
  199. }
  200. /** ARP network protocol */
  201. struct net_protocol arp_protocol __net_protocol = {
  202. .name = "ARP",
  203. .net_proto = htons ( ETH_P_ARP ),
  204. .rx = arp_rx,
  205. .ntoa = arp_ntoa,
  206. };