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 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <byteswap.h>
  22. #include <errno.h>
  23. #include <gpxe/if_ether.h>
  24. #include <gpxe/if_arp.h>
  25. #include <gpxe/iobuf.h>
  26. #include <gpxe/netdevice.h>
  27. #include <gpxe/arp.h>
  28. /** @file
  29. *
  30. * Address Resolution Protocol
  31. *
  32. * This file implements the address resolution protocol as defined in
  33. * RFC826. The implementation is media-independent and
  34. * protocol-independent; it is not limited to Ethernet or to IPv4.
  35. *
  36. */
  37. /** An ARP cache entry */
  38. struct arp_entry {
  39. /** Network-layer protocol */
  40. struct net_protocol *net_protocol;
  41. /** Link-layer protocol */
  42. struct ll_protocol *ll_protocol;
  43. /** Network-layer address */
  44. uint8_t net_addr[MAX_NET_ADDR_LEN];
  45. /** Link-layer address */
  46. uint8_t ll_addr[MAX_LL_ADDR_LEN];
  47. };
  48. /** Number of entries in the ARP cache
  49. *
  50. * This is a global cache, covering all network interfaces,
  51. * network-layer protocols and link-layer protocols.
  52. */
  53. #define NUM_ARP_ENTRIES 4
  54. /** The ARP cache */
  55. static struct arp_entry arp_table[NUM_ARP_ENTRIES];
  56. #define arp_table_end &arp_table[NUM_ARP_ENTRIES]
  57. static unsigned int next_new_arp_entry = 0;
  58. struct net_protocol arp_protocol;
  59. /**
  60. * Find entry in the ARP cache
  61. *
  62. * @v ll_protocol Link-layer protocol
  63. * @v net_protocol Network-layer protocol
  64. * @v net_addr Network-layer address
  65. * @ret arp ARP cache entry, or NULL if not found
  66. *
  67. */
  68. static struct arp_entry *
  69. arp_find_entry ( struct ll_protocol *ll_protocol,
  70. struct net_protocol *net_protocol,
  71. const void *net_addr ) {
  72. struct arp_entry *arp;
  73. for ( arp = arp_table ; arp < arp_table_end ; arp++ ) {
  74. if ( ( arp->ll_protocol == ll_protocol ) &&
  75. ( arp->net_protocol == net_protocol ) &&
  76. ( memcmp ( arp->net_addr, net_addr,
  77. net_protocol->net_addr_len ) == 0 ) )
  78. return arp;
  79. }
  80. return NULL;
  81. }
  82. /**
  83. * Look up media-specific link-layer address in the ARP cache
  84. *
  85. * @v netdev Network device
  86. * @v net_protocol Network-layer protocol
  87. * @v dest_net_addr Destination network-layer address
  88. * @v source_net_addr Source network-layer address
  89. * @ret dest_ll_addr Destination link layer address
  90. * @ret rc Return status code
  91. *
  92. * This function will use the ARP cache to look up the link-layer
  93. * address for the link-layer protocol associated with the network
  94. * device and the given network-layer protocol and addresses. If
  95. * found, the destination link-layer address will be filled in in @c
  96. * dest_ll_addr.
  97. *
  98. * If no address is found in the ARP cache, an ARP request will be
  99. * transmitted on the specified network device and -ENOENT will be
  100. * returned.
  101. */
  102. int arp_resolve ( struct net_device *netdev, struct net_protocol *net_protocol,
  103. const void *dest_net_addr, const void *source_net_addr,
  104. void *dest_ll_addr ) {
  105. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  106. const struct arp_entry *arp;
  107. struct io_buffer *iobuf;
  108. struct arphdr *arphdr;
  109. int rc;
  110. /* Look for existing entry in ARP table */
  111. arp = arp_find_entry ( ll_protocol, net_protocol, dest_net_addr );
  112. if ( arp ) {
  113. DBG ( "ARP cache hit: %s %s => %s %s\n",
  114. net_protocol->name, net_protocol->ntoa ( arp->net_addr ),
  115. ll_protocol->name, ll_protocol->ntoa ( arp->ll_addr ) );
  116. memcpy ( dest_ll_addr, arp->ll_addr, ll_protocol->ll_addr_len);
  117. return 0;
  118. }
  119. DBG ( "ARP cache miss: %s %s\n", net_protocol->name,
  120. net_protocol->ntoa ( dest_net_addr ) );
  121. /* Allocate ARP packet */
  122. iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( *arphdr ) +
  123. 2 * ( MAX_LL_ADDR_LEN + MAX_NET_ADDR_LEN ) );
  124. if ( ! iobuf )
  125. return -ENOMEM;
  126. iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
  127. /* Build up ARP request */
  128. arphdr = iob_put ( iobuf, sizeof ( *arphdr ) );
  129. arphdr->ar_hrd = ll_protocol->ll_proto;
  130. arphdr->ar_hln = ll_protocol->ll_addr_len;
  131. arphdr->ar_pro = net_protocol->net_proto;
  132. arphdr->ar_pln = net_protocol->net_addr_len;
  133. arphdr->ar_op = htons ( ARPOP_REQUEST );
  134. memcpy ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
  135. netdev->ll_addr, ll_protocol->ll_addr_len );
  136. memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
  137. source_net_addr, net_protocol->net_addr_len );
  138. memset ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
  139. 0, ll_protocol->ll_addr_len );
  140. memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
  141. dest_net_addr, net_protocol->net_addr_len );
  142. /* Transmit ARP request */
  143. if ( ( rc = net_tx ( iobuf, netdev, &arp_protocol,
  144. netdev->ll_broadcast ) ) != 0 )
  145. return rc;
  146. return -ENOENT;
  147. }
  148. /**
  149. * Identify ARP protocol
  150. *
  151. * @v net_proto Network-layer protocol, in network-endian order
  152. * @ret arp_net_protocol ARP protocol, or NULL
  153. *
  154. */
  155. static struct arp_net_protocol * arp_find_protocol ( uint16_t net_proto ) {
  156. struct arp_net_protocol *arp_net_protocol;
  157. for_each_table_entry ( arp_net_protocol, ARP_NET_PROTOCOLS ) {
  158. if ( arp_net_protocol->net_protocol->net_proto == net_proto ) {
  159. return arp_net_protocol;
  160. }
  161. }
  162. return NULL;
  163. }
  164. /**
  165. * Process incoming ARP packets
  166. *
  167. * @v iobuf I/O buffer
  168. * @v netdev Network device
  169. * @v ll_source Link-layer source address
  170. * @ret rc Return status code
  171. *
  172. * This handles ARP requests and responses as detailed in RFC826. The
  173. * method detailed within the RFC is pretty optimised, handling
  174. * requests and responses with basically a single code path and
  175. * avoiding the need for extraneous ARP requests; read the RFC for
  176. * details.
  177. */
  178. static int arp_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  179. const void *ll_source __unused ) {
  180. struct arphdr *arphdr = iobuf->data;
  181. struct arp_net_protocol *arp_net_protocol;
  182. struct net_protocol *net_protocol;
  183. struct ll_protocol *ll_protocol;
  184. struct arp_entry *arp;
  185. int merge = 0;
  186. /* Identify network-layer and link-layer protocols */
  187. arp_net_protocol = arp_find_protocol ( arphdr->ar_pro );
  188. if ( ! arp_net_protocol )
  189. goto done;
  190. net_protocol = arp_net_protocol->net_protocol;
  191. ll_protocol = netdev->ll_protocol;
  192. /* Sanity checks */
  193. if ( ( arphdr->ar_hrd != ll_protocol->ll_proto ) ||
  194. ( arphdr->ar_hln != ll_protocol->ll_addr_len ) ||
  195. ( arphdr->ar_pln != net_protocol->net_addr_len ) )
  196. goto done;
  197. /* See if we have an entry for this sender, and update it if so */
  198. arp = arp_find_entry ( ll_protocol, net_protocol,
  199. arp_sender_pa ( arphdr ) );
  200. if ( arp ) {
  201. memcpy ( arp->ll_addr, arp_sender_ha ( arphdr ),
  202. arphdr->ar_hln );
  203. merge = 1;
  204. DBG ( "ARP cache update: %s %s => %s %s\n",
  205. net_protocol->name, net_protocol->ntoa ( arp->net_addr ),
  206. ll_protocol->name, ll_protocol->ntoa ( arp->ll_addr ) );
  207. }
  208. /* See if we own the target protocol address */
  209. if ( arp_net_protocol->check ( netdev, arp_target_pa ( arphdr ) ) != 0)
  210. goto done;
  211. /* Create new ARP table entry if necessary */
  212. if ( ! merge ) {
  213. arp = &arp_table[next_new_arp_entry++ % NUM_ARP_ENTRIES];
  214. arp->ll_protocol = ll_protocol;
  215. arp->net_protocol = net_protocol;
  216. memcpy ( arp->ll_addr, arp_sender_ha ( arphdr ),
  217. arphdr->ar_hln );
  218. memcpy ( arp->net_addr, arp_sender_pa ( arphdr ),
  219. arphdr->ar_pln);
  220. DBG ( "ARP cache add: %s %s => %s %s\n",
  221. net_protocol->name, net_protocol->ntoa ( arp->net_addr ),
  222. ll_protocol->name, ll_protocol->ntoa ( arp->ll_addr ) );
  223. }
  224. /* If it's not a request, there's nothing more to do */
  225. if ( arphdr->ar_op != htons ( ARPOP_REQUEST ) )
  226. goto done;
  227. /* Change request to a reply */
  228. DBG ( "ARP reply: %s %s => %s %s\n", net_protocol->name,
  229. net_protocol->ntoa ( arp_target_pa ( arphdr ) ),
  230. ll_protocol->name, ll_protocol->ntoa ( netdev->ll_addr ) );
  231. arphdr->ar_op = htons ( ARPOP_REPLY );
  232. memswap ( arp_sender_ha ( arphdr ), arp_target_ha ( arphdr ),
  233. arphdr->ar_hln + arphdr->ar_pln );
  234. memcpy ( arp_sender_ha ( arphdr ), netdev->ll_addr, arphdr->ar_hln );
  235. /* Send reply */
  236. net_tx ( iob_disown ( iobuf ), netdev, &arp_protocol,
  237. arp_target_ha ( arphdr ) );
  238. done:
  239. free_iob ( iobuf );
  240. return 0;
  241. }
  242. /**
  243. * Transcribe ARP address
  244. *
  245. * @v net_addr ARP address
  246. * @ret string "<ARP>"
  247. *
  248. * This operation is meaningless for the ARP protocol.
  249. */
  250. static const char * arp_ntoa ( const void *net_addr __unused ) {
  251. return "<ARP>";
  252. }
  253. /** ARP protocol */
  254. struct net_protocol arp_protocol __net_protocol = {
  255. .name = "ARP",
  256. .net_proto = htons ( ETH_P_ARP ),
  257. .rx = arp_rx,
  258. .ntoa = arp_ntoa,
  259. };