arp_table.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /****************************************************************************
  2. * net/arp/arp_table.c
  3. * Implementation of the ARP Address Resolution Protocol.
  4. *
  5. * Copyright (C) 2007-2009, 2011, 2014, 2018 Gregory Nutt. All rights
  6. * reserved.
  7. * Author: Gregory Nutt <gnutt@nuttx.org>
  8. *
  9. * Based originally on uIP which also has a BSD style license:
  10. *
  11. * Author: Adam Dunkels <adam@dunkels.com>
  12. * Copyright (c) 2001-2003, Adam Dunkels.
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. *
  19. * 1. Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. * 3. The name of the author may not be used to endorse or promote
  25. * products derived from this software without specific prior
  26. * written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  30. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  32. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  36. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  37. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. ****************************************************************************/
  41. /****************************************************************************
  42. * Included Files
  43. ****************************************************************************/
  44. #include <nuttx/config.h>
  45. #ifdef CONFIG_NET
  46. #include <sys/ioctl.h>
  47. #include <stdint.h>
  48. #include <string.h>
  49. #include <debug.h>
  50. #include <netinet/in.h>
  51. #include <net/ethernet.h>
  52. #include <nuttx/clock.h>
  53. #include <nuttx/net/netconfig.h>
  54. #include <nuttx/net/net.h>
  55. #include <nuttx/net/netdev.h>
  56. #include <nuttx/net/arp.h>
  57. #include <nuttx/net/ip.h>
  58. #include <arp/arp.h>
  59. #include <netdev/netdev.h>
  60. #ifdef CONFIG_NET_ARP
  61. /****************************************************************************
  62. * Pre-processor Definitions
  63. ****************************************************************************/
  64. #define ARP_MAXAGE_TICK SEC2TICK(10 * CONFIG_NET_ARP_MAXAGE)
  65. /****************************************************************************
  66. * Private Types
  67. ****************************************************************************/
  68. struct arp_table_info_s
  69. {
  70. in_addr_t ai_ipaddr; /* IP address for lookup */
  71. FAR struct ether_addr *ai_ethaddr; /* Location to return the MAC address */
  72. };
  73. /****************************************************************************
  74. * Private Data
  75. ****************************************************************************/
  76. /* The table of known address mappings */
  77. static struct arp_entry_s g_arptable[CONFIG_NET_ARPTAB_SIZE];
  78. /****************************************************************************
  79. * Private Functions
  80. ****************************************************************************/
  81. /****************************************************************************
  82. * Name: arp_match
  83. *
  84. * Description:
  85. * This is a callback that checks if the Ethernet network device has the
  86. * indicated IPv4 address assigned to it.
  87. *
  88. ****************************************************************************/
  89. static int arp_match(FAR struct net_driver_s *dev, FAR void *arg)
  90. {
  91. FAR struct arp_table_info_s *info = arg;
  92. /* Make sure that this is an Ethernet device (or an IEEE 802.11 device
  93. * which is also Ethernet)
  94. */
  95. if (dev->d_lltype != NET_LL_ETHERNET &&
  96. dev->d_lltype != NET_LL_IEEE80211)
  97. {
  98. return 0;
  99. }
  100. /* Check if the network device has been assigned the IP address of the
  101. * lookup.
  102. */
  103. if (!net_ipv4addr_cmp(dev->d_ipaddr, info->ai_ipaddr))
  104. {
  105. return 0;
  106. }
  107. /* Yes.. Return the matching Ethernet MAC address if the caller of
  108. * arp_find() provided a non-NULL location.
  109. */
  110. if (info->ai_ethaddr != NULL)
  111. {
  112. memcpy(info->ai_ethaddr, &dev->d_mac.ether, ETHER_ADDR_LEN);
  113. }
  114. /* Return success in any event */
  115. return 1;
  116. }
  117. /****************************************************************************
  118. * Name: arp_return_old_entry
  119. *
  120. * Description:
  121. * Compare and return the old ARP table entry.
  122. *
  123. ****************************************************************************/
  124. static FAR struct arp_entry_s *
  125. arp_return_old_entry(FAR struct arp_entry_s *e1, FAR struct arp_entry_s *e2)
  126. {
  127. if (e1->at_ipaddr == 0)
  128. {
  129. return e1;
  130. }
  131. else if (e2->at_ipaddr == 0)
  132. {
  133. return e2;
  134. }
  135. else if ((int)(e1->at_time - e2->at_time) <= 0)
  136. {
  137. return e1;
  138. }
  139. else
  140. {
  141. return e2;
  142. }
  143. }
  144. /****************************************************************************
  145. * Public Functions
  146. ****************************************************************************/
  147. /****************************************************************************
  148. * Name: arp_update
  149. *
  150. * Description:
  151. * Add the IP/HW address mapping to the ARP table -OR- change the IP
  152. * address of an existing association.
  153. *
  154. * Input Parameters:
  155. * ipaddr - The IP address as an inaddr_t
  156. * ethaddr - Refers to a HW address uint8_t[IFHWADDRLEN]
  157. *
  158. * Returned Value:
  159. * Zero (OK) if the ARP table entry was successfully modified. A negated
  160. * errno value is returned on any error.
  161. *
  162. * Assumptions
  163. * The network is locked to assure exclusive access to the ARP table
  164. *
  165. ****************************************************************************/
  166. int arp_update(in_addr_t ipaddr, FAR uint8_t *ethaddr)
  167. {
  168. FAR struct arp_entry_s *tabptr = &g_arptable[0];
  169. int i;
  170. /* Walk through the ARP mapping table and try to find an entry to
  171. * update. If none is found, the IP -> MAC address mapping is
  172. * inserted in the ARP table.
  173. */
  174. for (i = 0; i < CONFIG_NET_ARPTAB_SIZE; ++i)
  175. {
  176. /* Check if the source IP address of the incoming packet matches
  177. * the IP address in this ARP table entry.
  178. */
  179. if (g_arptable[i].at_ipaddr != 0 &&
  180. net_ipv4addr_cmp(ipaddr, g_arptable[i].at_ipaddr))
  181. {
  182. /* An old entry found, break. */
  183. tabptr = &g_arptable[i];
  184. break;
  185. }
  186. else
  187. {
  188. /* Record the oldest entry. */
  189. tabptr = arp_return_old_entry(tabptr, &g_arptable[i]);
  190. }
  191. }
  192. /* Now, tabptr is the ARP table entry which we will fill with the new
  193. * information.
  194. */
  195. tabptr->at_ipaddr = ipaddr;
  196. memcpy(tabptr->at_ethaddr.ether_addr_octet, ethaddr, ETHER_ADDR_LEN);
  197. tabptr->at_time = clock_systimer();
  198. return OK;
  199. }
  200. /****************************************************************************
  201. * Name: arp_hdr_update
  202. *
  203. * Description:
  204. * Add the IP/HW address mapping to the ARP table -OR- change the IP
  205. * address of an existing association.
  206. *
  207. * Input Parameters:
  208. * pipaddr - Refers to an IP address uint16_t[2] in network order
  209. * ethaddr - Refers to a HW address uint8_t[IFHWADDRLEN]
  210. *
  211. * Returned Value:
  212. * Zero (OK) if the ARP table entry was successfully modified. A negated
  213. * errno value is returned on any error.
  214. *
  215. * Assumptions
  216. * The network is locked to assure exclusive access to the ARP table
  217. *
  218. ****************************************************************************/
  219. void arp_hdr_update(FAR uint16_t *pipaddr, FAR uint8_t *ethaddr)
  220. {
  221. in_addr_t ipaddr = net_ip4addr_conv32(pipaddr);
  222. /* Update the ARP table */
  223. (void)arp_update(ipaddr, ethaddr);
  224. }
  225. /****************************************************************************
  226. * Name: arp_lookup
  227. *
  228. * Description:
  229. * Find the ARP entry corresponding to this IP address in the ARP table.
  230. *
  231. * Input Parameters:
  232. * ipaddr - Refers to an IP address in network order
  233. *
  234. * Assumptions:
  235. * The network is locked to assure exclusive access to the ARP table.
  236. * The return value will become unstable when the network is unlocked.
  237. *
  238. ****************************************************************************/
  239. FAR struct arp_entry_s *arp_lookup(in_addr_t ipaddr)
  240. {
  241. FAR struct arp_entry_s *tabptr;
  242. int i;
  243. /* Check if the IPv4 address is already in the ARP table. */
  244. for (i = 0; i < CONFIG_NET_ARPTAB_SIZE; ++i)
  245. {
  246. tabptr = &g_arptable[i];
  247. if (net_ipv4addr_cmp(ipaddr, tabptr->at_ipaddr) &&
  248. clock_systimer() - tabptr->at_time <= ARP_MAXAGE_TICK)
  249. {
  250. return tabptr;
  251. }
  252. }
  253. /* Not found */
  254. return NULL;
  255. }
  256. /****************************************************************************
  257. * Name: arp_find
  258. *
  259. * Description:
  260. * Find the ARP entry corresponding to this IP address which may or may
  261. * not be in the ARP table (it may, instead, be a local network device).
  262. *
  263. * Input Parameters:
  264. * ipaddr - Refers to an IP address in network order
  265. * ethaddr - Location to return the corresponding Ethernet MAN address.
  266. * This address may be NULL. In that case, this function may be
  267. * used simply to determine if the Ethernet MAC address is
  268. * available.
  269. *
  270. * Assumptions
  271. * The network is locked to assure exclusive access to the ARP table.
  272. *
  273. ****************************************************************************/
  274. int arp_find(in_addr_t ipaddr, FAR struct ether_addr *ethaddr)
  275. {
  276. FAR struct arp_entry_s *tabptr;
  277. struct arp_table_info_s info;
  278. /* Check if the IPv4 address is already in the ARP table. */
  279. tabptr = arp_lookup(ipaddr);
  280. if (tabptr != NULL)
  281. {
  282. /* Yes.. return the Ethernet MAC address if the caller has provided a
  283. * non-NULL address in 'ethaddr'.
  284. */
  285. if (ethaddr != NULL)
  286. {
  287. memcpy(ethaddr, &tabptr->at_ethaddr, ETHER_ADDR_LEN);
  288. }
  289. /* Return success in any case meaning that a valid Ethernet MAC
  290. * address mapping is available for the IP address.
  291. */
  292. return OK;
  293. }
  294. /* No.. check if the IPv4 address is the address assigned to a local
  295. * Ethernet network device. If so, return a mapping of that IP address
  296. * to the Ethernet MAC address assigned to the network device.
  297. */
  298. info.ai_ipaddr = ipaddr;
  299. info.ai_ethaddr = ethaddr;
  300. if (netdev_foreach(arp_match, &info) != 0)
  301. {
  302. return OK;
  303. }
  304. /* Not found */
  305. return -ENOENT;
  306. }
  307. /****************************************************************************
  308. * Name: arp_delete
  309. *
  310. * Description:
  311. * Remove an IP association from the ARP table
  312. *
  313. * Input Parameters:
  314. * ipaddr - Refers to an IP address in network order
  315. *
  316. * Assumptions
  317. * The network is locked to assure exclusive access to the ARP table.
  318. *
  319. ****************************************************************************/
  320. void arp_delete(in_addr_t ipaddr)
  321. {
  322. FAR struct arp_entry_s *tabptr;
  323. /* Check if the IPv4 address is in the ARP table. */
  324. tabptr = arp_lookup(ipaddr);
  325. if (tabptr != NULL)
  326. {
  327. /* Yes.. Set the IP address to zero to "delete" it */
  328. tabptr->at_ipaddr = 0;
  329. }
  330. }
  331. /****************************************************************************
  332. * Name: arp_snapshot
  333. *
  334. * Description:
  335. * Take a snapshot of the current state of the ARP table.
  336. *
  337. * Input Parameters:
  338. * snapshot - Location to return the ARP table copy
  339. * nentries - The size of the user provided 'dest' in entries, each of
  340. * size sizeof(struct arp_entry_s)
  341. *
  342. * Returned Value:
  343. * On success, the number of entries actually copied is returned. Unused
  344. * entries are not returned.
  345. *
  346. * Assumptions
  347. * The network is locked to assure exclusive access to the ARP table
  348. *
  349. ****************************************************************************/
  350. #ifdef CONFIG_NETLINK_ROUTE
  351. unsigned int arp_snapshot(FAR struct arp_entry_s *snapshot,
  352. unsigned int nentries)
  353. {
  354. FAR struct arp_entry_s *tabptr;
  355. clock_t now;
  356. unsigned int ncopied;
  357. int i;
  358. /* Copy all non-empty, non-expired entries in the ARP table. */
  359. for (i = 0, now = clock_systimer(), ncopied = 0;
  360. nentries > ncopied && i < CONFIG_NET_ARPTAB_SIZE;
  361. i++)
  362. {
  363. tabptr = &g_arptable[i];
  364. if (tabptr->at_ipaddr != 0 &&
  365. now - tabptr->at_time <= ARP_MAXAGE_TICK)
  366. {
  367. memcpy(&snapshot[ncopied], tabptr, sizeof(struct arp_entry_s));
  368. ncopied++;
  369. }
  370. }
  371. /* Return the number of entries copied into the user buffer */
  372. return ncopied;
  373. }
  374. #endif
  375. #endif /* CONFIG_NET_ARP */
  376. #endif /* CONFIG_NET */