xref: /aosp_15_r20/external/iw/mpp.c (revision 92022041c981f431db0b590d0c3272306d0ea2a2)
1 #include <net/if.h>
2 #include <errno.h>
3 
4 #include <netlink/genl/genl.h>
5 #include <netlink/genl/family.h>
6 #include <netlink/genl/ctrl.h>
7 #include <netlink/msg.h>
8 #include <netlink/attr.h>
9 
10 #include "nl80211.h"
11 #include "iw.h"
12 
13 SECTION(mpp);
14 
print_mpp_handler(struct nl_msg * msg,void * arg)15 static int print_mpp_handler(struct nl_msg *msg, void *arg)
16 {
17 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
18 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
19 	char dst[20], next_hop[20], dev[20];
20 
21 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
22 		  genlmsg_attrlen(gnlh, 0), NULL);
23 
24 	/*
25 	 * TODO: validate the interface and mac address!
26 	 * Otherwise, there's a race condition as soon as
27 	 * the kernel starts sending mpath notifications.
28 	 */
29 
30 	mac_addr_n2a(dst, nla_data(tb[NL80211_ATTR_MAC]));
31 	mac_addr_n2a(next_hop, nla_data(tb[NL80211_ATTR_MPATH_NEXT_HOP]));
32 	if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
33 	printf("%s %s %s\n", dst, next_hop, dev);
34 
35 	return NL_SKIP;
36 }
37 
handle_mpp_get(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)38 static int handle_mpp_get(struct nl80211_state *state,
39 			  struct nl_msg *msg,
40 			  int argc, char **argv,
41 			  enum id_input id)
42 {
43 	unsigned char dst[ETH_ALEN];
44 
45 	if (argc < 1)
46 		return 1;
47 
48 	if (mac_addr_a2n(dst, argv[0])) {
49 		fprintf(stderr, "invalid mac address\n");
50 		return 2;
51 	}
52 	argc--;
53 	argv++;
54 
55 	if (argc)
56 		return 1;
57 
58 	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
59 
60 	register_handler(print_mpp_handler, NULL);
61 
62 	return 0;
63  nla_put_failure:
64 	return -ENOBUFS;
65 }
66 COMMAND(mpp, get, "<MAC address>",
67 	NL80211_CMD_GET_MPP, 0, CIB_NETDEV, handle_mpp_get,
68 	"Get information on mesh proxy path to the given node.");
69 
handle_mpp_dump(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)70 static int handle_mpp_dump(struct nl80211_state *state,
71 			     struct nl_msg *msg,
72 			     int argc, char **argv,
73 			     enum id_input id)
74 {
75 	printf("DEST ADDR         PROXY NODE        IFACE\n");
76 	register_handler(print_mpp_handler, NULL);
77 	return 0;
78 }
79 COMMAND(mpp, dump, NULL,
80 	NL80211_CMD_GET_MPP, NLM_F_DUMP, CIB_NETDEV, handle_mpp_dump,
81 	"List known mesh proxy paths.");
82