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

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