1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __ARP_H
8 #define __ARP_H
9 
10 #include <zephyr/sys/slist.h>
11 #include <zephyr/net/ethernet.h>
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 /**
18  * @brief Address resolution (ARP) library
19  * @defgroup arp ARP Library
20  * @ingroup networking
21  * @{
22  */
23 
24 /** @brief Address resolution complete, destination link address injected */
25 #define NET_ARP_COMPLETE     0
26 /** @brief Destination link address unknown, ARP request to be sent instead */
27 #define NET_ARP_PKT_REPLACED 1
28 /** @brief Destination link address unknown, ARP request pending, packet queued */
29 #define NET_ARP_PKT_QUEUED   2
30 
31 #if defined(CONFIG_NET_ARP) && defined(CONFIG_NET_NATIVE)
32 
33 #define NET_ARP_HDR(pkt) ((struct net_arp_hdr *)net_pkt_data(pkt))
34 
35 struct net_arp_hdr {
36 	uint16_t hwtype;			/* HTYPE */
37 	uint16_t protocol;			/* PTYPE */
38 	uint8_t hwlen;				/* HLEN */
39 	uint8_t protolen;			/* PLEN */
40 	uint16_t opcode;
41 	struct net_eth_addr src_hwaddr;		/* SHA */
42 	uint8_t src_ipaddr[NET_IPV4_ADDR_SIZE];	/* SPA */
43 	struct net_eth_addr dst_hwaddr;		/* THA */
44 	uint8_t dst_ipaddr[NET_IPV4_ADDR_SIZE];	/* TPA */
45 } __packed;
46 
47 #define NET_ARP_HTYPE_ETH 1
48 #define NET_ARP_IPV4_PTYPE_SIZE 4
49 
50 #define NET_ARP_REQUEST 1
51 #define NET_ARP_REPLY   2
52 
53 /**
54  * @brief Prepare an ARP request, if required
55  *
56  * @param pkt Packet that wants to be sent
57  * @param request_ip Destination address of the packet
58  * @param current_ip Sending IP (Can be NULL)
59  * @param arp_pkt ARP packet that should be sent in place of @a pkt, if not NULL
60  *
61  * @retval NET_ARP_COMPLETE ARP information populated in @a pkt
62  * @retval NET_ARP_PKT_REPLACED ARP request to be sent existing in @a arp_pkt
63  * @retval NET_ARP_PKT_QUEUED @a pkt was queued for transmission on ARP resolution
64  * @retval <0 on failure
65  */
66 int net_arp_prepare(struct net_pkt *pkt,
67 			struct in_addr *request_ip,
68 			struct in_addr *current_ip,
69 			struct net_pkt **arp_pkt);
70 enum net_verdict net_arp_input(struct net_pkt *pkt,
71 			       struct net_eth_addr *src,
72 			       struct net_eth_addr *dst);
73 
74 int net_arp_clear_pending(struct net_if *iface,
75 				struct in_addr *dst);
76 
77 struct arp_entry {
78 	sys_snode_t node;
79 	uint32_t req_start;
80 	struct net_if *iface;
81 	struct in_addr ip;
82 	struct net_eth_addr eth;
83 	struct k_fifo pending_queue;
84 };
85 
86 typedef void (*net_arp_cb_t)(struct arp_entry *entry,
87 			     void *user_data);
88 int net_arp_foreach(net_arp_cb_t cb, void *user_data);
89 
90 void net_arp_clear_cache(struct net_if *iface);
91 void net_arp_init(void);
92 void net_arp_update(struct net_if *iface, struct in_addr *src,
93 		    struct net_eth_addr *hwaddr, bool gratuitous,
94 		    bool force);
95 
96 
97 #else /* CONFIG_NET_ARP */
98 #define net_arp_prepare(_kt, _u1, _u2, _arp) NET_ARP_COMPLETE
99 #define net_arp_input(...) NET_OK
100 #define net_arp_clear_cache(...)
101 #define net_arp_foreach(...) 0
102 #define net_arp_init(...)
103 #define net_arp_clear_pending(...) 0
104 #define net_arp_update(...)
105 
106 #endif /* CONFIG_NET_ARP */
107 
108 /**
109  * @}
110  */
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
116 #endif /* __ARP_H */
117