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.3KB

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