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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 <stdlib.h>
  21. #include <string.h>
  22. #include <byteswap.h>
  23. #include <errno.h>
  24. #include <ipxe/if_ether.h>
  25. #include <ipxe/if_arp.h>
  26. #include <ipxe/iobuf.h>
  27. #include <ipxe/netdevice.h>
  28. #include <ipxe/list.h>
  29. #include <ipxe/retry.h>
  30. #include <ipxe/timer.h>
  31. #include <ipxe/malloc.h>
  32. #include <ipxe/arp.h>
  33. /** @file
  34. *
  35. * Address Resolution Protocol
  36. *
  37. * This file implements the address resolution protocol as defined in
  38. * RFC826. The implementation is media-independent and
  39. * protocol-independent; it is not limited to Ethernet or to IPv4.
  40. *
  41. */
  42. /** ARP minimum timeout */
  43. #define ARP_MIN_TIMEOUT ( TICKS_PER_SEC / 8 )
  44. /** ARP maximum timeout */
  45. #define ARP_MAX_TIMEOUT ( TICKS_PER_SEC * 3 )
  46. /** An ARP cache entry */
  47. struct arp_entry {
  48. /** List of ARP cache entries */
  49. struct list_head list;
  50. /** Network device */
  51. struct net_device *netdev;
  52. /** Network-layer protocol */
  53. struct net_protocol *net_protocol;
  54. /** Network-layer destination address */
  55. uint8_t net_dest[MAX_NET_ADDR_LEN];
  56. /** Network-layer source address */
  57. uint8_t net_source[MAX_NET_ADDR_LEN];
  58. /** Link-layer destination address */
  59. uint8_t ll_dest[MAX_LL_ADDR_LEN];
  60. /** Retransmission timer */
  61. struct retry_timer timer;
  62. /** Pending I/O buffers */
  63. struct list_head tx_queue;
  64. };
  65. /** The ARP cache */
  66. static LIST_HEAD ( arp_entries );
  67. struct net_protocol arp_protocol __net_protocol;
  68. static void arp_expired ( struct retry_timer *timer, int over );
  69. /**
  70. * Create ARP cache entry
  71. *
  72. * @v netdev Network device
  73. * @v net_protocol Network-layer protocol
  74. * @v net_dest Destination network-layer address
  75. * @v net_source Source network-layer address
  76. * @ret arp ARP cache entry, or NULL if allocation failed
  77. */
  78. static struct arp_entry * arp_create ( struct net_device *netdev,
  79. struct net_protocol *net_protocol,
  80. const void *net_dest,
  81. const void *net_source ) {
  82. struct arp_entry *arp;
  83. /* Allocate entry and add to cache */
  84. arp = zalloc ( sizeof ( *arp ) );
  85. if ( ! arp )
  86. return NULL;
  87. /* Initialise entry and add to cache */
  88. arp->netdev = netdev_get ( netdev );
  89. arp->net_protocol = net_protocol;
  90. memcpy ( arp->net_dest, net_dest,
  91. net_protocol->net_addr_len );
  92. memcpy ( arp->net_source, net_source,
  93. net_protocol->net_addr_len );
  94. timer_init ( &arp->timer, arp_expired, NULL );
  95. arp->timer.min_timeout = ARP_MIN_TIMEOUT;
  96. arp->timer.max_timeout = ARP_MAX_TIMEOUT;
  97. INIT_LIST_HEAD ( &arp->tx_queue );
  98. list_add ( &arp->list, &arp_entries );
  99. /* Start timer running to trigger initial transmission */
  100. start_timer_nodelay ( &arp->timer );
  101. DBGC ( arp, "ARP %p %s %s %s created\n", arp, netdev->name,
  102. net_protocol->name, net_protocol->ntoa ( net_dest ) );
  103. return arp;
  104. }
  105. /**
  106. * Find entry in the ARP cache
  107. *
  108. * @v netdev Network device
  109. * @v net_protocol Network-layer protocol
  110. * @v net_dest Destination network-layer address
  111. * @ret arp ARP cache entry, or NULL if not found
  112. */
  113. static struct arp_entry * arp_find ( struct net_device *netdev,
  114. struct net_protocol *net_protocol,
  115. const void *net_dest ) {
  116. struct arp_entry *arp;
  117. list_for_each_entry ( arp, &arp_entries, list ) {
  118. if ( ( arp->netdev == netdev ) &&
  119. ( arp->net_protocol == net_protocol ) &&
  120. ( memcmp ( arp->net_dest, net_dest,
  121. net_protocol->net_addr_len ) == 0 ) ) {
  122. /* Move to start of cache */
  123. list_del ( &arp->list );
  124. list_add ( &arp->list, &arp_entries );
  125. return arp;
  126. }
  127. }
  128. return NULL;
  129. }
  130. /**
  131. * Destroy ARP cache entry
  132. *
  133. * @v arp ARP cache entry
  134. * @v rc Reason for destruction
  135. */
  136. static void arp_destroy ( struct arp_entry *arp, int rc ) {
  137. struct net_device *netdev = arp->netdev;
  138. struct net_protocol *net_protocol = arp->net_protocol;
  139. struct io_buffer *iobuf;
  140. struct io_buffer *tmp;
  141. /* Stop timer */
  142. stop_timer ( &arp->timer );
  143. /* Discard any outstanding I/O buffers */
  144. list_for_each_entry_safe ( iobuf, tmp, &arp->tx_queue, list ) {
  145. DBGC2 ( arp, "ARP %p %s %s %s discarding deferred packet: "
  146. "%s\n", arp, netdev->name, net_protocol->name,
  147. net_protocol->ntoa ( arp->net_dest ), strerror ( rc ) );
  148. list_del ( &iobuf->list );
  149. netdev_tx_err ( arp->netdev, iobuf, rc );
  150. }
  151. DBGC ( arp, "ARP %p %s %s %s destroyed: %s\n", arp, netdev->name,
  152. net_protocol->name, net_protocol->ntoa ( arp->net_dest ),
  153. strerror ( rc ) );
  154. /* Drop reference to network device, remove from cache and free */
  155. netdev_put ( arp->netdev );
  156. list_del ( &arp->list );
  157. free ( arp );
  158. }
  159. /**
  160. * Test if ARP cache entry has a valid link-layer address
  161. *
  162. * @v arp ARP cache entry
  163. * @ret resolved ARP cache entry is resolved
  164. */
  165. static inline int arp_resolved ( struct arp_entry *arp ) {
  166. return ( ! timer_running ( &arp->timer ) );
  167. }
  168. /**
  169. * Transmit packet, determining link-layer address via ARP
  170. *
  171. * @v iobuf I/O buffer
  172. * @v netdev Network device
  173. * @v net_protocol Network-layer protocol
  174. * @v net_dest Destination network-layer address
  175. * @v net_source Source network-layer address
  176. * @v ll_source Source link-layer address
  177. * @ret rc Return status code
  178. */
  179. int arp_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  180. struct net_protocol *net_protocol, const void *net_dest,
  181. const void *net_source, const void *ll_source ) {
  182. struct arp_entry *arp;
  183. /* Find or create ARP cache entry */
  184. arp = arp_find ( netdev, net_protocol, net_dest );
  185. if ( ! arp ) {
  186. arp = arp_create ( netdev, net_protocol, net_dest,
  187. net_source );
  188. if ( ! arp )
  189. return -ENOMEM;
  190. }
  191. /* If a link-layer address is available then transmit
  192. * immediately, otherwise queue for later transmission.
  193. */
  194. if ( arp_resolved ( arp ) ) {
  195. return net_tx ( iobuf, netdev, net_protocol, arp->ll_dest,
  196. ll_source );
  197. } else {
  198. DBGC2 ( arp, "ARP %p %s %s %s deferring packet\n",
  199. arp, netdev->name, net_protocol->name,
  200. net_protocol->ntoa ( net_dest ) );
  201. list_add_tail ( &iobuf->list, &arp->tx_queue );
  202. return -EAGAIN;
  203. }
  204. }
  205. /**
  206. * Update ARP cache entry
  207. *
  208. * @v arp ARP cache entry
  209. * @v ll_dest Destination link-layer address
  210. */
  211. static void arp_update ( struct arp_entry *arp, const void *ll_dest ) {
  212. struct net_device *netdev = arp->netdev;
  213. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  214. struct net_protocol *net_protocol = arp->net_protocol;
  215. struct io_buffer *iobuf;
  216. struct io_buffer *tmp;
  217. int rc;
  218. DBGC ( arp, "ARP %p %s %s %s updated => %s\n", arp, netdev->name,
  219. net_protocol->name, net_protocol->ntoa ( arp->net_dest ),
  220. ll_protocol->ntoa ( ll_dest ) );
  221. /* Fill in link-layer address */
  222. memcpy ( arp->ll_dest, ll_dest, ll_protocol->ll_addr_len );
  223. /* Stop retransmission timer */
  224. stop_timer ( &arp->timer );
  225. /* Transmit any packets in queue */
  226. list_for_each_entry_safe ( iobuf, tmp, &arp->tx_queue, list ) {
  227. DBGC2 ( arp, "ARP %p %s %s %s transmitting deferred packet\n",
  228. arp, netdev->name, net_protocol->name,
  229. net_protocol->ntoa ( arp->net_dest ) );
  230. list_del ( &iobuf->list );
  231. if ( ( rc = net_tx ( iobuf, netdev, net_protocol, ll_dest,
  232. netdev->ll_addr ) ) != 0 ) {
  233. DBGC ( arp, "ARP %p could not transmit deferred "
  234. "packet: %s\n", arp, strerror ( rc ) );
  235. /* Ignore error and continue */
  236. }
  237. }
  238. }
  239. /**
  240. * Handle ARP timer expiry
  241. *
  242. * @v timer Retry timer
  243. * @v fail Failure indicator
  244. */
  245. static void arp_expired ( struct retry_timer *timer, int fail ) {
  246. struct arp_entry *arp = container_of ( timer, struct arp_entry, timer );
  247. struct net_device *netdev = arp->netdev;
  248. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  249. struct net_protocol *net_protocol = arp->net_protocol;
  250. struct io_buffer *iobuf;
  251. struct arphdr *arphdr;
  252. int rc;
  253. /* If we have failed, destroy the cache entry */
  254. if ( fail ) {
  255. arp_destroy ( arp, -ETIMEDOUT );
  256. return;
  257. }
  258. /* Restart the timer */
  259. start_timer ( &arp->timer );
  260. /* Allocate ARP packet */
  261. iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( *arphdr ) +
  262. ( 2 * ( MAX_LL_ADDR_LEN + MAX_NET_ADDR_LEN ) ) );
  263. if ( ! iobuf ) {
  264. /* Leave timer running and try again later */
  265. return;
  266. }
  267. iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
  268. /* Build up ARP request */
  269. arphdr = iob_put ( iobuf, sizeof ( *arphdr ) );
  270. arphdr->ar_hrd = ll_protocol->ll_proto;
  271. arphdr->ar_hln = ll_protocol->ll_addr_len;
  272. arphdr->ar_pro = net_protocol->net_proto;
  273. arphdr->ar_pln = net_protocol->net_addr_len;
  274. arphdr->ar_op = htons ( ARPOP_REQUEST );
  275. memcpy ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
  276. netdev->ll_addr, ll_protocol->ll_addr_len );
  277. memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
  278. arp->net_source, net_protocol->net_addr_len );
  279. memset ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
  280. 0, ll_protocol->ll_addr_len );
  281. memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
  282. arp->net_dest, net_protocol->net_addr_len );
  283. /* Transmit ARP request */
  284. if ( ( rc = net_tx ( iobuf, netdev, &arp_protocol,
  285. netdev->ll_broadcast, netdev->ll_addr ) ) != 0 ) {
  286. DBGC ( arp, "ARP %p could not transmit request: %s\n",
  287. arp, strerror ( rc ) );
  288. return;
  289. }
  290. }
  291. /**
  292. * Identify ARP protocol
  293. *
  294. * @v net_proto Network-layer protocol, in network-endian order
  295. * @ret arp_net_protocol ARP protocol, or NULL
  296. *
  297. */
  298. static struct arp_net_protocol * arp_find_protocol ( uint16_t net_proto ) {
  299. struct arp_net_protocol *arp_net_protocol;
  300. for_each_table_entry ( arp_net_protocol, ARP_NET_PROTOCOLS ) {
  301. if ( arp_net_protocol->net_protocol->net_proto == net_proto ) {
  302. return arp_net_protocol;
  303. }
  304. }
  305. return NULL;
  306. }
  307. /**
  308. * Process incoming ARP packets
  309. *
  310. * @v iobuf I/O buffer
  311. * @v netdev Network device
  312. * @v ll_source Link-layer source address
  313. * @v flags Packet flags
  314. * @ret rc Return status code
  315. */
  316. static int arp_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  317. const void *ll_dest __unused,
  318. const void *ll_source __unused,
  319. unsigned int flags __unused ) {
  320. struct arphdr *arphdr = iobuf->data;
  321. struct arp_net_protocol *arp_net_protocol;
  322. struct net_protocol *net_protocol;
  323. struct ll_protocol *ll_protocol;
  324. struct arp_entry *arp;
  325. int rc;
  326. /* Identify network-layer and link-layer protocols */
  327. arp_net_protocol = arp_find_protocol ( arphdr->ar_pro );
  328. if ( ! arp_net_protocol ) {
  329. rc = -EPROTONOSUPPORT;
  330. goto done;
  331. }
  332. net_protocol = arp_net_protocol->net_protocol;
  333. ll_protocol = netdev->ll_protocol;
  334. /* Sanity checks */
  335. if ( ( arphdr->ar_hrd != ll_protocol->ll_proto ) ||
  336. ( arphdr->ar_hln != ll_protocol->ll_addr_len ) ||
  337. ( arphdr->ar_pln != net_protocol->net_addr_len ) ) {
  338. rc = -EINVAL;
  339. goto done;
  340. }
  341. /* See if we have an entry for this sender, and update it if so */
  342. arp = arp_find ( netdev, net_protocol, arp_sender_pa ( arphdr ) );
  343. if ( arp ) {
  344. arp_update ( arp, arp_sender_ha ( arphdr ) );
  345. }
  346. /* If it's not a request, there's nothing more to do */
  347. if ( arphdr->ar_op != htons ( ARPOP_REQUEST ) ) {
  348. rc = 0;
  349. goto done;
  350. }
  351. /* See if we own the target protocol address */
  352. if ( arp_net_protocol->check ( netdev, arp_target_pa ( arphdr ) ) != 0){
  353. rc = 0;
  354. goto done;
  355. }
  356. /* Change request to a reply */
  357. DBGC ( netdev, "ARP reply %s %s %s => %s %s\n",
  358. netdev->name, net_protocol->name,
  359. net_protocol->ntoa ( arp_target_pa ( arphdr ) ),
  360. ll_protocol->name, ll_protocol->ntoa ( netdev->ll_addr ) );
  361. arphdr->ar_op = htons ( ARPOP_REPLY );
  362. memswap ( arp_sender_ha ( arphdr ), arp_target_ha ( arphdr ),
  363. arphdr->ar_hln + arphdr->ar_pln );
  364. memcpy ( arp_sender_ha ( arphdr ), netdev->ll_addr, arphdr->ar_hln );
  365. /* Send reply */
  366. if ( ( rc = net_tx ( iob_disown ( iobuf ), netdev, &arp_protocol,
  367. arp_target_ha ( arphdr ),
  368. netdev->ll_addr ) ) != 0 ) {
  369. DBGC ( netdev, "ARP could not transmit reply via %s: %s\n",
  370. netdev->name, strerror ( rc ) );
  371. goto done;
  372. }
  373. /* Success */
  374. rc = 0;
  375. done:
  376. free_iob ( iobuf );
  377. return rc;
  378. }
  379. /**
  380. * Transcribe ARP address
  381. *
  382. * @v net_addr ARP address
  383. * @ret string "<ARP>"
  384. *
  385. * This operation is meaningless for the ARP protocol.
  386. */
  387. static const char * arp_ntoa ( const void *net_addr __unused ) {
  388. return "<ARP>";
  389. }
  390. /** ARP protocol */
  391. struct net_protocol arp_protocol __net_protocol = {
  392. .name = "ARP",
  393. .net_proto = htons ( ETH_P_ARP ),
  394. .rx = arp_rx,
  395. .ntoa = arp_ntoa,
  396. };
  397. /**
  398. * Update ARP cache on network device creation
  399. *
  400. * @v netdev Network device
  401. */
  402. static int arp_probe ( struct net_device *netdev __unused ) {
  403. /* Nothing to do */
  404. return 0;
  405. }
  406. /**
  407. * Update ARP cache on network device state change or removal
  408. *
  409. * @v netdev Network device
  410. */
  411. static void arp_flush ( struct net_device *netdev ) {
  412. struct arp_entry *arp;
  413. struct arp_entry *tmp;
  414. /* Remove all ARP cache entries when a network device is closed */
  415. if ( ! netdev_is_open ( netdev ) ) {
  416. list_for_each_entry_safe ( arp, tmp, &arp_entries, list )
  417. arp_destroy ( arp, -ENODEV );
  418. }
  419. }
  420. /** ARP driver (for net device notifications) */
  421. struct net_driver arp_net_driver __net_driver = {
  422. .name = "ARP",
  423. .probe = arp_probe,
  424. .notify = arp_flush,
  425. .remove = arp_flush,
  426. };
  427. /**
  428. * Discard some cached ARP entries
  429. *
  430. * @ret discarded Number of cached items discarded
  431. */
  432. static unsigned int arp_discard ( void ) {
  433. struct arp_entry *arp;
  434. /* Drop oldest cache entry, if any */
  435. list_for_each_entry_reverse ( arp, &arp_entries, list ) {
  436. arp_destroy ( arp, -ENOBUFS );
  437. return 1;
  438. }
  439. return 0;
  440. }
  441. /** ARP cache discarder */
  442. struct cache_discarder arp_cache_discarder __cache_discarder = {
  443. .discard = arp_discard,
  444. };