arp.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /****************************************************************************
  2. * net/arp/arp.h
  3. *
  4. * Copyright (C) 2014-2016, 2018-2019 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. #ifndef __NET_ARP_ARP_H
  36. #define __NET_ARP_ARP_H
  37. /* The Address Resolution Protocol ARP is used for mapping between IP
  38. * addresses and link level addresses such as the Ethernet MAC
  39. * addresses. ARP uses broadcast queries to ask for the link level
  40. * address of a known IP address and the host which is configured with
  41. * the IP address for which the query was meant, will respond with its
  42. * link level address.
  43. *
  44. * Note: This ARP implementation only supports Ethernet.
  45. */
  46. /****************************************************************************
  47. * Included Files
  48. ****************************************************************************/
  49. #include <nuttx/config.h>
  50. #include <stdint.h>
  51. #include <queue.h>
  52. #include <errno.h>
  53. #include <netinet/in.h>
  54. #include <nuttx/net/netdev.h>
  55. #include <nuttx/semaphore.h>
  56. /****************************************************************************
  57. * Pre-processor Definitions
  58. ****************************************************************************/
  59. /* Configuration ************************************************************/
  60. #ifndef CONFIG_DEBUG_FEATURES
  61. # undef CONFIG_NET_ARP_DUMP
  62. #endif
  63. #ifndef CONFIG_ARP_SEND_MAXTRIES
  64. # define CONFIG_ARP_SEND_MAXTRIES 5
  65. #endif
  66. #ifndef CONFIG_ARP_SEND_DELAYMSEC
  67. # define CONFIG_ARP_SEND_DELAYMSEC 20
  68. #endif
  69. /* ARP Definitions **********************************************************/
  70. #define ARP_REQUEST 1
  71. #define ARP_REPLY 2
  72. #define ARP_HWTYPE_ETH 1
  73. #define RASIZE 4 /* Size of ROUTER ALERT */
  74. /* Allocate a new ARP data callback */
  75. #define arp_callback_alloc(dev) devif_callback_alloc(dev, &(dev)->d_conncb)
  76. #define arp_callback_free(dev,cb) devif_dev_callback_free(dev, cb)
  77. /****************************************************************************
  78. * Public Types
  79. ****************************************************************************/
  80. /* ARP header -- Size 28 bytes */
  81. struct arp_hdr_s
  82. {
  83. uint16_t ah_hwtype; /* 16-bit Hardware type (Ethernet=0x001) */
  84. uint16_t ah_protocol; /* 16-bit Protocol type (IP=0x0800) */
  85. uint8_t ah_hwlen; /* 8-bit Hardware address size (6) */
  86. uint8_t ah_protolen; /* 8-bit Procotol address size (4) */
  87. uint16_t ah_opcode; /* 16-bit Operation */
  88. uint8_t ah_shwaddr[6]; /* 48-bit Sender hardware address */
  89. uint16_t ah_sipaddr[2]; /* 32-bit Sender IP address */
  90. uint8_t ah_dhwaddr[6]; /* 48-bit Target hardware address */
  91. uint16_t ah_dipaddr[2]; /* 32-bit Target IP address */
  92. };
  93. /* IP header -- Size 20 or 24 bytes */
  94. struct arp_iphdr_s
  95. {
  96. uint8_t eh_vhl; /* 8-bit Version (4) and header length (5 or 6) */
  97. uint8_t eh_tos; /* 8-bit Type of service (e.g., 6=TCP) */
  98. uint8_t eh_len[2]; /* 16-bit Total length */
  99. uint8_t eh_ipid[2]; /* 16-bit Identification */
  100. uint8_t eh_ipoffset[2]; /* 16-bit IP flags + fragment offset */
  101. uint8_t eh_ttl; /* 8-bit Time to Live */
  102. uint8_t eh_proto; /* 8-bit Protocol */
  103. uint16_t eh_ipchksum; /* 16-bit Header checksum */
  104. uint16_t eh_srcipaddr[2]; /* 32-bit Source IP address */
  105. uint16_t eh_destipaddr[2]; /* 32-bit Destination IP address */
  106. uint16_t eh_ipoption[2]; /* (optional) */
  107. };
  108. #ifdef CONFIG_NET_ARP_SEND
  109. /* This structure holds the state of the send operation until it can be
  110. * operated upon from the network driver poll.
  111. */
  112. struct arp_send_s
  113. {
  114. FAR struct devif_callback_s *snd_cb; /* Reference to callback instance */
  115. sem_t snd_sem; /* Used to wake up the waiting thread */
  116. uint8_t snd_retries; /* Retry count */
  117. volatile bool snd_sent; /* True: if request sent */
  118. uint8_t snd_ifname[IFNAMSIZ]; /* Interface name */
  119. int16_t snd_result; /* The result of the send operation */
  120. in_addr_t snd_ipaddr; /* The IP address to be queried */
  121. };
  122. #endif
  123. #ifdef CONFIG_NET_ARP_SEND
  124. /* Used to notify a thread waiting for a particular ARP response */
  125. struct arp_notify_s
  126. {
  127. FAR struct arp_notify_s *nt_flink; /* Supports singly linked list */
  128. in_addr_t nt_ipaddr; /* Waited for IP address in the mapping */
  129. sem_t nt_sem; /* Will wake up the waiter */
  130. int nt_result; /* The result of the wait */
  131. };
  132. #endif
  133. /****************************************************************************
  134. * Public Data
  135. ****************************************************************************/
  136. /****************************************************************************
  137. * Public Function Prototypes
  138. ****************************************************************************/
  139. #ifdef CONFIG_NET_ARP
  140. /****************************************************************************
  141. * Name: arp_format
  142. *
  143. * Description:
  144. * Format an ARP packet.
  145. *
  146. * Input Parameters:
  147. * dev - Device structure
  148. * ipaddr - Target IP address (32-bit)
  149. *
  150. * Returned Value:
  151. * None
  152. *
  153. ****************************************************************************/
  154. struct net_driver_s; /* Forward reference */
  155. void arp_format(FAR struct net_driver_s *dev, in_addr_t ipaddr);
  156. /****************************************************************************
  157. * Name: arp_send
  158. *
  159. * Description:
  160. * The arp_send() call may be to send an ARP request to resolve an IPv4
  161. * address. This function first checks if the IPv4 address is already in
  162. * the ARP table. If so, then it returns success immediately.
  163. *
  164. * If the requested IPv4 address in not in the ARP table, then this function
  165. * will send an ARP request, delay, then check if the IP address is now in
  166. * the ARP table. It will repeat this sequence until either (1) the IP
  167. * address mapping is now in the ARP table, or (2) a configurable number
  168. * of timeouts occur without receiving the ARP replay.
  169. *
  170. * Input Parameters:
  171. * ipaddr The IP address to be queried.
  172. *
  173. * Returned Value:
  174. * Zero (OK) is returned on success and the IP address mapping can now be
  175. * found in the ARP table. On error a negated errno value is returned:
  176. *
  177. * -ETIMEDOUT: The number or retry counts has been exceed.
  178. * -EHOSTUNREACH: Could not find a route to the host
  179. *
  180. * Assumptions:
  181. * This function is called from the normal tasking context.
  182. *
  183. ****************************************************************************/
  184. #ifdef CONFIG_NET_ARP_SEND
  185. int arp_send(in_addr_t ipaddr);
  186. #else
  187. # define arp_send(i) (0)
  188. #endif
  189. /****************************************************************************
  190. * Name: arp_poll
  191. *
  192. * Description:
  193. * Poll all pending transfer for ARP requests to send.
  194. *
  195. * Assumptions:
  196. * This function is called from the MAC device driver indirectly through
  197. * devif_poll() and devif_timer(). The network must be locked.
  198. *
  199. ****************************************************************************/
  200. #ifdef CONFIG_NET_ARP_SEND
  201. int arp_poll(FAR struct net_driver_s *dev, devif_poll_callback_t callback);
  202. #else
  203. # define arp_poll(d,c) (0)
  204. #endif
  205. /****************************************************************************
  206. * Name: arp_wait_setup
  207. *
  208. * Description:
  209. * Called BEFORE an ARP request is sent. This function sets up the ARP
  210. * response timeout before the ARP request is sent so that there is
  211. * no race condition when arp_wait() is called.
  212. *
  213. * Assumptions:
  214. * This function is called from ARP send and executes in the normal
  215. * tasking environment.
  216. *
  217. ****************************************************************************/
  218. #ifdef CONFIG_NET_ARP_SEND
  219. void arp_wait_setup(in_addr_t ipaddr, FAR struct arp_notify_s *notify);
  220. #else
  221. # define arp_wait_setup(i,n)
  222. #endif
  223. /****************************************************************************
  224. * Name: arp_wait_cancel
  225. *
  226. * Description:
  227. * Cancel any wait set after arp_wait_setup is called but before arm_wait()
  228. * is called (arp_wait() will automatically cancel the wait).
  229. *
  230. * Assumptions:
  231. * This function may execute in the interrupt context when called from
  232. * arp_wait().
  233. *
  234. ****************************************************************************/
  235. #ifdef CONFIG_NET_ARP_SEND
  236. int arp_wait_cancel(FAR struct arp_notify_s *notify);
  237. #else
  238. # define arp_wait_cancel(n) (0)
  239. #endif
  240. /****************************************************************************
  241. * Name: arp_wait
  242. *
  243. * Description:
  244. * Called each time that a ARP request is sent. This function will sleep
  245. * until either: (1) the matching ARP response is received, or (2) a
  246. * timeout occurs.
  247. *
  248. * Assumptions:
  249. * This function is called from ARP send and must execute with the network
  250. * un-locked.
  251. *
  252. ****************************************************************************/
  253. #ifdef CONFIG_NET_ARP_SEND
  254. int arp_wait(FAR struct arp_notify_s *notify, unsigned int timeout);
  255. #else
  256. # define arp_wait(n,t) (0)
  257. #endif
  258. /****************************************************************************
  259. * Name: arp_notify
  260. *
  261. * Description:
  262. * Called each time that a ARP response is received in order to wake-up
  263. * any threads that may be waiting for this particular ARP response.
  264. *
  265. * Assumptions:
  266. * This function is called from the MAC device driver indirectly through
  267. * arp_arpin() and will execute with the network locked.
  268. *
  269. ****************************************************************************/
  270. #ifdef CONFIG_NET_ARP_SEND
  271. void arp_notify(in_addr_t ipaddr);
  272. #else
  273. # define arp_notify(i)
  274. #endif
  275. /****************************************************************************
  276. * Name: arp_lookup
  277. *
  278. * Description:
  279. * Find the ARP entry corresponding to this IP address in the ARP table.
  280. *
  281. * Input Parameters:
  282. * ipaddr - Refers to an IP address in network order
  283. *
  284. * Assumptions:
  285. * The network is locked to assure exclusive access to the ARP table.
  286. * The return value will become unstable when the network is unlocked.
  287. *
  288. ****************************************************************************/
  289. FAR struct arp_entry_s *arp_lookup(in_addr_t ipaddr);
  290. /****************************************************************************
  291. * Name: arp_find
  292. *
  293. * Description:
  294. * Find the ARP entry corresponding to this IP address which may or may
  295. * not be in the ARP table (it may, instead, be a local network device).
  296. *
  297. * Input Parameters:
  298. * ipaddr - Refers to an IP address in network order
  299. * ethaddr - Location to return the corresponding Ethernet MAN address.
  300. * This address may be NULL. In that case, this function may be
  301. * used simply to determine if the Ethernet MAC address is
  302. * available.
  303. *
  304. * Assumptions
  305. * The network is locked to assure exclusive access to the ARP table.
  306. *
  307. ****************************************************************************/
  308. struct ether_addr; /* Forward reference */
  309. int arp_find(in_addr_t ipaddr, FAR struct ether_addr *ethaddr);
  310. /****************************************************************************
  311. * Name: arp_delete
  312. *
  313. * Description:
  314. * Remove an IP association from the ARP table
  315. *
  316. * Input Parameters:
  317. * ipaddr - Refers to an IP address in network order
  318. *
  319. * Assumptions
  320. * The network is locked to assure exclusive access to the ARP table.
  321. *
  322. ****************************************************************************/
  323. void arp_delete(in_addr_t ipaddr);
  324. /****************************************************************************
  325. * Name: arp_update
  326. *
  327. * Description:
  328. * Add the IP/HW address mapping to the ARP table -OR- change the IP
  329. * address of an existing association.
  330. *
  331. * Input Parameters:
  332. * ipaddr - The IP address as an inaddr_t
  333. * ethaddr - Refers to a HW address uint8_t[IFHWADDRLEN]
  334. *
  335. * Returned Value:
  336. * Zero (OK) if the ARP table entry was successfully modified. A negated
  337. * errno value is returned on any error.
  338. *
  339. * Assumptions
  340. * The network is locked to assure exclusive access to the ARP table
  341. *
  342. ****************************************************************************/
  343. int arp_update(in_addr_t ipaddr, FAR uint8_t *ethaddr);
  344. /****************************************************************************
  345. * Name: arp_hdr_update
  346. *
  347. * Description:
  348. * Add the IP/HW address mapping to the ARP table -OR- change the IP
  349. * address of an existing association.
  350. *
  351. * Input Parameters:
  352. * pipaddr - Refers to an IP address uint16_t[2] in network order
  353. * ethaddr - Refers to a HW address uint8_t[IFHWADDRLEN]
  354. *
  355. * Returned Value:
  356. * Zero (OK) if the ARP table entry was successfully modified. A negated
  357. * errno value is returned on any error.
  358. *
  359. * Assumptions
  360. * The network is locked to assure exclusive access to the ARP table
  361. *
  362. ****************************************************************************/
  363. void arp_hdr_update(FAR uint16_t *pipaddr, FAR uint8_t *ethaddr);
  364. /****************************************************************************
  365. * Name: arp_snapshot
  366. *
  367. * Description:
  368. * Take a snapshot of the current state of the ARP table.
  369. *
  370. * Input Parameters:
  371. * snapshot - Location to return the ARP table copy
  372. * nentries - The size of the user provided 'dest' in entries, each of
  373. * size sizeof(struct arp_entry_s)
  374. *
  375. * Returned Value:
  376. * On success, the number of entries actually copied is returned. Unused
  377. * entries are not returned.
  378. *
  379. * Assumptions
  380. * The network is locked to assure exclusive access to the ARP table
  381. *
  382. ****************************************************************************/
  383. #ifdef CONFIG_NETLINK_ROUTE
  384. unsigned int arp_snapshot(FAR struct arp_entry_s *snapshot,
  385. unsigned int nentries);
  386. #else
  387. # define arp_snapshot(s,n) (0)
  388. #endif
  389. /****************************************************************************
  390. * Name: arp_dump
  391. *
  392. * Description:
  393. * Dump the contents of an ARP packet to the SYSLOG device
  394. *
  395. * Input Parameters:
  396. * arp - A reference to the ARP header to be dumped.
  397. *
  398. * Returned Value:
  399. * None
  400. *
  401. ****************************************************************************/
  402. #ifdef CONFIG_NET_ARP_DUMP
  403. void arp_dump(FAR struct arp_hdr_s *arp);
  404. #else
  405. # define arp_dump(arp)
  406. #endif
  407. #else /* CONFIG_NET_ARP */
  408. /* If ARP is disabled, stub out all ARP interfaces */
  409. # define arp_format(d,i);
  410. # define arp_send(i) (0)
  411. # define arp_poll(d,c) (0)
  412. # define arp_wait_setup(i,n)
  413. # define arp_wait_cancel(n) (0)
  414. # define arp_wait(n,t) (0)
  415. # define arp_notify(i)
  416. # define arp_find(i,e) (-ENOSYS)
  417. # define arp_delete(i)
  418. # define arp_update(i,m);
  419. # define arp_hdr_update(i,m);
  420. # define arp_snapshot(s,n) (0)
  421. # define arp_dump(arp)
  422. #endif /* CONFIG_NET_ARP */
  423. #endif /* __NET_ARP_ARP_H */