Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

arp.c 9.1KB

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