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 #if defined(CONFIG_NET_ARP) && defined(CONFIG_NET_NATIVE)
11 
12 #include <zephyr/sys/slist.h>
13 #include <zephyr/net/ethernet.h>
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 /**
20  * @brief Address resolution (ARP) library
21  * @defgroup arp ARP Library
22  * @ingroup networking
23  * @{
24  */
25 
26 #define NET_ARP_HDR(pkt) ((struct net_arp_hdr *)net_pkt_data(pkt))
27 
28 struct net_arp_hdr {
29 	uint16_t hwtype;			/* HTYPE */
30 	uint16_t protocol;			/* PTYPE */
31 	uint8_t hwlen;				/* HLEN */
32 	uint8_t protolen;			/* PLEN */
33 	uint16_t opcode;
34 	struct net_eth_addr src_hwaddr;		/* SHA */
35 	uint8_t src_ipaddr[NET_IPV4_ADDR_SIZE];	/* SPA */
36 	struct net_eth_addr dst_hwaddr;		/* THA */
37 	uint8_t dst_ipaddr[NET_IPV4_ADDR_SIZE];	/* TPA */
38 } __packed;
39 
40 #define NET_ARP_HTYPE_ETH 1
41 #define NET_ARP_IPV4_PTYPE_SIZE 4
42 
43 #define NET_ARP_REQUEST 1
44 #define NET_ARP_REPLY   2
45 
46 struct net_pkt *net_arp_prepare(struct net_pkt *pkt,
47 				struct in_addr *request_ip,
48 				struct in_addr *current_ip);
49 enum net_verdict net_arp_input(struct net_pkt *pkt,
50 			       struct net_eth_hdr *eth_hdr);
51 
52 int net_arp_clear_pending(struct net_if *iface,
53 				struct in_addr *dst);
54 
55 struct arp_entry {
56 	sys_snode_t node;
57 	uint32_t req_start;
58 	struct net_if *iface;
59 	struct in_addr ip;
60 	struct net_eth_addr eth;
61 	struct k_fifo pending_queue;
62 };
63 
64 typedef void (*net_arp_cb_t)(struct arp_entry *entry,
65 			     void *user_data);
66 int net_arp_foreach(net_arp_cb_t cb, void *user_data);
67 
68 void net_arp_clear_cache(struct net_if *iface);
69 void net_arp_init(void);
70 void net_arp_update(struct net_if *iface, struct in_addr *src,
71 		    struct net_eth_addr *hwaddr, bool gratuitous,
72 		    bool force);
73 
74 /**
75  * @}
76  */
77 
78 #ifdef __cplusplus
79 }
80 #endif
81 
82 #else /* CONFIG_NET_ARP */
83 #define net_arp_prepare(_kt, _u1, _u2) _kt
84 #define net_arp_input(...) NET_OK
85 #define net_arp_clear_cache(...)
86 #define net_arp_foreach(...) 0
87 #define net_arp_init(...)
88 #define net_arp_clear_pending(...) 0
89 #define net_arp_update(...)
90 
91 #endif /* CONFIG_NET_ARP */
92 
93 #endif /* __ARP_H */
94