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.

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