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_addr *src,
51 			       struct net_eth_addr *dst);
52 
53 int net_arp_clear_pending(struct net_if *iface,
54 				struct in_addr *dst);
55 
56 struct arp_entry {
57 	sys_snode_t node;
58 	uint32_t req_start;
59 	struct net_if *iface;
60 	struct in_addr ip;
61 	struct net_eth_addr eth;
62 	struct k_fifo pending_queue;
63 };
64 
65 typedef void (*net_arp_cb_t)(struct arp_entry *entry,
66 			     void *user_data);
67 int net_arp_foreach(net_arp_cb_t cb, void *user_data);
68 
69 void net_arp_clear_cache(struct net_if *iface);
70 void net_arp_init(void);
71 void net_arp_update(struct net_if *iface, struct in_addr *src,
72 		    struct net_eth_addr *hwaddr, bool gratuitous,
73 		    bool force);
74 
75 /**
76  * @}
77  */
78 
79 #ifdef __cplusplus
80 }
81 #endif
82 
83 #else /* CONFIG_NET_ARP */
84 #define net_arp_prepare(_kt, _u1, _u2) _kt
85 #define net_arp_input(...) NET_OK
86 #define net_arp_clear_cache(...)
87 #define net_arp_foreach(...) 0
88 #define net_arp_init(...)
89 #define net_arp_clear_pending(...) 0
90 #define net_arp_update(...)
91 
92 #endif /* CONFIG_NET_ARP */
93 
94 #endif /* __ARP_H */
95