xref: /aosp_15_r20/external/libnl/src/lib/neigh.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2008-2009 Thomas Graf <[email protected]>
4  */
5 
6 /**
7  * @ingroup cli
8  * @defgroup cli_neigh Neighbour
9  *
10  * @{
11  */
12 
13 #include "nl-default.h"
14 
15 #include <netlink/cli/utils.h>
16 #include <netlink/cli/neigh.h>
17 
nl_cli_neigh_alloc(void)18 struct rtnl_neigh *nl_cli_neigh_alloc(void)
19 {
20 	struct rtnl_neigh *neigh;
21 
22 	neigh = rtnl_neigh_alloc();
23 	if (!neigh)
24 		nl_cli_fatal(ENOMEM, "Unable to allocate neighbour object");
25 
26 	return neigh;
27 }
28 
nl_cli_neigh_parse_dst(struct rtnl_neigh * neigh,char * arg)29 void nl_cli_neigh_parse_dst(struct rtnl_neigh *neigh, char *arg)
30 {
31 	struct nl_addr *a;
32 	int err;
33 
34 	a = nl_cli_addr_parse(arg, rtnl_neigh_get_family(neigh));
35 	if ((err = rtnl_neigh_set_dst(neigh, a)) < 0)
36 		nl_cli_fatal(err, "Unable to set local address: %s",
37 			nl_geterror(err));
38 
39 	nl_addr_put(a);
40 }
41 
nl_cli_neigh_parse_lladdr(struct rtnl_neigh * neigh,char * arg)42 void nl_cli_neigh_parse_lladdr(struct rtnl_neigh *neigh, char *arg)
43 {
44 	struct nl_addr *a;
45 
46 	a = nl_cli_addr_parse(arg, AF_UNSPEC);
47 	rtnl_neigh_set_lladdr(neigh, a);
48 	nl_addr_put(a);
49 }
50 
nl_cli_neigh_parse_dev(struct rtnl_neigh * neigh,struct nl_cache * link_cache,char * arg)51 void nl_cli_neigh_parse_dev(struct rtnl_neigh *neigh,
52 			    struct nl_cache *link_cache, char *arg)
53 {
54 	int ival;
55 
56 	if (!(ival = rtnl_link_name2i(link_cache, arg)))
57 		nl_cli_fatal(ENOENT, "Link \"%s\" does not exist", arg);
58 
59 	rtnl_neigh_set_ifindex(neigh, ival);
60 }
61 
nl_cli_neigh_parse_family(struct rtnl_neigh * neigh,char * arg)62 void nl_cli_neigh_parse_family(struct rtnl_neigh *neigh, char *arg)
63 {
64 	int family;
65 
66 	if ((family = nl_str2af(arg)) == AF_UNSPEC)
67 		nl_cli_fatal(EINVAL,
68 			     "Unable to translate address family \"%s\"", arg);
69 
70 	rtnl_neigh_set_family(neigh, family);
71 }
72 
nl_cli_neigh_parse_state(struct rtnl_neigh * neigh,char * arg)73 void nl_cli_neigh_parse_state(struct rtnl_neigh *neigh, char *arg)
74 {
75 	int state;
76 
77 	if ((state = rtnl_neigh_str2state(arg)) < 0)
78 		nl_cli_fatal(state, "Unable to translate state \"%s\": %s",
79 			arg, state);
80 
81 	rtnl_neigh_set_state(neigh, state);
82 }
83 
84 /** @} */
85