1 /*
2  * Copyright (c) 2016 Intel Corporation
3  * Copyright (c) 2023 Nordic Semiconductor ASA
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/logging/log.h>
9 LOG_MODULE_DECLARE(net_shell);
10 
11 #include "net_shell_private.h"
12 
13 #if defined(CONFIG_NET_ARP)
14 #include "ethernet/arp.h"
15 #endif
16 
17 #if defined(CONFIG_NET_ARP) && defined(CONFIG_NET_NATIVE)
arp_cb(struct arp_entry * entry,void * user_data)18 static void arp_cb(struct arp_entry *entry, void *user_data)
19 {
20 	struct net_shell_user_data *data = user_data;
21 	const struct shell *sh = data->sh;
22 	int *count = data->user_data;
23 
24 	if (*count == 0) {
25 		PR("     Interface  Link              Address\n");
26 	}
27 
28 	PR("[%2d] %d          %s %s\n", *count,
29 	   net_if_get_by_iface(entry->iface),
30 	   net_sprint_ll_addr(entry->eth.addr, sizeof(struct net_eth_addr)),
31 	   net_sprint_ipv4_addr(&entry->ip));
32 
33 	(*count)++;
34 }
35 #endif /* CONFIG_NET_ARP */
36 
37 #if !defined(CONFIG_NET_ARP)
print_arp_error(const struct shell * sh)38 static void print_arp_error(const struct shell *sh)
39 {
40 	PR_INFO("Set %s to enable %s support.\n",
41 		"CONFIG_NET_NATIVE, CONFIG_NET_ARP, CONFIG_NET_IPV4 and"
42 		" CONFIG_NET_L2_ETHERNET", "ARP");
43 }
44 #endif
45 
cmd_net_arp(const struct shell * sh,size_t argc,char * argv[])46 static int cmd_net_arp(const struct shell *sh, size_t argc, char *argv[])
47 {
48 #if defined(CONFIG_NET_ARP)
49 	struct net_shell_user_data user_data;
50 	int arg = 1;
51 #endif
52 
53 	ARG_UNUSED(argc);
54 
55 #if defined(CONFIG_NET_ARP)
56 	if (!argv[arg]) {
57 		/* ARP cache content */
58 		int count = 0;
59 
60 		user_data.sh = sh;
61 		user_data.user_data = &count;
62 
63 		if (net_arp_foreach(arp_cb, &user_data) == 0) {
64 			PR("ARP cache is empty.\n");
65 		}
66 	}
67 #else
68 	print_arp_error(sh);
69 #endif
70 
71 	return 0;
72 }
73 
cmd_net_arp_flush(const struct shell * sh,size_t argc,char * argv[])74 static int cmd_net_arp_flush(const struct shell *sh, size_t argc, char *argv[])
75 {
76 	ARG_UNUSED(argc);
77 	ARG_UNUSED(argv);
78 
79 #if defined(CONFIG_NET_ARP)
80 	PR("Flushing ARP cache.\n");
81 	net_arp_clear_cache(NULL);
82 #else
83 	print_arp_error(sh);
84 #endif
85 
86 	return 0;
87 }
88 
89 SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_arp,
90 	SHELL_CMD(flush, NULL, "Remove all entries from ARP cache.",
91 		  cmd_net_arp_flush),
92 	SHELL_SUBCMD_SET_END
93 );
94 
95 SHELL_SUBCMD_ADD((net), arp, &net_cmd_arp,
96 		 "Print information about IPv4 ARP cache.",
97 		 cmd_net_arp, 1, 0);
98