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

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