]> git.ipfire.org Git - thirdparty/iw.git/blob - mpath.c
add P2P Device handling primitives
[thirdparty/iw.git] / mpath.c
1 #include <net/if.h>
2 #include <errno.h>
3 #include <string.h>
4
5 #include <netlink/genl/genl.h>
6 #include <netlink/genl/family.h>
7 #include <netlink/genl/ctrl.h>
8 #include <netlink/msg.h>
9 #include <netlink/attr.h>
10
11 #include "nl80211.h"
12 #include "iw.h"
13
14 SECTION(mpath);
15
16 enum plink_state {
17 LISTEN,
18 OPN_SNT,
19 OPN_RCVD,
20 CNF_RCVD,
21 ESTAB,
22 HOLDING,
23 BLOCKED
24 };
25
26 enum plink_actions {
27 PLINK_ACTION_UNDEFINED,
28 PLINK_ACTION_OPEN,
29 PLINK_ACTION_BLOCK,
30 };
31
32
33 static int print_mpath_handler(struct nl_msg *msg, void *arg)
34 {
35 struct nlattr *tb[NL80211_ATTR_MAX + 1];
36 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
37 struct nlattr *pinfo[NL80211_MPATH_INFO_MAX + 1];
38 char dst[20], next_hop[20], dev[20];
39 static struct nla_policy mpath_policy[NL80211_MPATH_INFO_MAX + 1] = {
40 [NL80211_MPATH_INFO_FRAME_QLEN] = { .type = NLA_U32 },
41 [NL80211_MPATH_INFO_SN] = { .type = NLA_U32 },
42 [NL80211_MPATH_INFO_METRIC] = { .type = NLA_U32 },
43 [NL80211_MPATH_INFO_EXPTIME] = { .type = NLA_U32 },
44 [NL80211_MPATH_INFO_DISCOVERY_TIMEOUT] = { .type = NLA_U32 },
45 [NL80211_MPATH_INFO_DISCOVERY_RETRIES] = { .type = NLA_U8 },
46 [NL80211_MPATH_INFO_FLAGS] = { .type = NLA_U8 },
47 };
48
49 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
50 genlmsg_attrlen(gnlh, 0), NULL);
51
52 /*
53 * TODO: validate the interface and mac address!
54 * Otherwise, there's a race condition as soon as
55 * the kernel starts sending mpath notifications.
56 */
57
58 if (!tb[NL80211_ATTR_MPATH_INFO]) {
59 fprintf(stderr, "mpath info missing!\n");
60 return NL_SKIP;
61 }
62 if (nla_parse_nested(pinfo, NL80211_MPATH_INFO_MAX,
63 tb[NL80211_ATTR_MPATH_INFO],
64 mpath_policy)) {
65 fprintf(stderr, "failed to parse nested attributes!\n");
66 return NL_SKIP;
67 }
68
69 mac_addr_n2a(dst, nla_data(tb[NL80211_ATTR_MAC]));
70 mac_addr_n2a(next_hop, nla_data(tb[NL80211_ATTR_MPATH_NEXT_HOP]));
71 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
72 printf("%s %s %s", dst, next_hop, dev);
73 if (pinfo[NL80211_MPATH_INFO_SN])
74 printf("\t%u",
75 nla_get_u32(pinfo[NL80211_MPATH_INFO_SN]));
76 if (pinfo[NL80211_MPATH_INFO_METRIC])
77 printf("\t%u",
78 nla_get_u32(pinfo[NL80211_MPATH_INFO_METRIC]));
79 if (pinfo[NL80211_MPATH_INFO_FRAME_QLEN])
80 printf("\t%u",
81 nla_get_u32(pinfo[NL80211_MPATH_INFO_FRAME_QLEN]));
82 if (pinfo[NL80211_MPATH_INFO_EXPTIME])
83 printf("\t%u",
84 nla_get_u32(pinfo[NL80211_MPATH_INFO_EXPTIME]));
85 if (pinfo[NL80211_MPATH_INFO_DISCOVERY_TIMEOUT])
86 printf("\t%u",
87 nla_get_u32(pinfo[NL80211_MPATH_INFO_DISCOVERY_TIMEOUT]));
88 if (pinfo[NL80211_MPATH_INFO_DISCOVERY_RETRIES])
89 printf("\t%u",
90 nla_get_u8(pinfo[NL80211_MPATH_INFO_DISCOVERY_RETRIES]));
91 if (pinfo[NL80211_MPATH_INFO_FLAGS])
92 printf("\t0x%x",
93 nla_get_u8(pinfo[NL80211_MPATH_INFO_FLAGS]));
94
95 printf("\n");
96 return NL_SKIP;
97 }
98
99 static int handle_mpath_get(struct nl80211_state *state,
100 struct nl_cb *cb,
101 struct nl_msg *msg,
102 int argc, char **argv,
103 enum id_input id)
104 {
105 unsigned char dst[ETH_ALEN];
106
107 if (argc < 1)
108 return 1;
109
110 if (mac_addr_a2n(dst, argv[0])) {
111 fprintf(stderr, "invalid mac address\n");
112 return 2;
113 }
114 argc--;
115 argv++;
116
117 if (argc)
118 return 1;
119
120 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
121
122 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_mpath_handler, NULL);
123
124 return 0;
125 nla_put_failure:
126 return -ENOBUFS;
127 }
128 COMMAND(mpath, get, "<MAC address>",
129 NL80211_CMD_GET_MPATH, 0, CIB_NETDEV, handle_mpath_get,
130 "Get information on mesh path to the given node.");
131 COMMAND(mpath, del, "<MAC address>",
132 NL80211_CMD_DEL_MPATH, 0, CIB_NETDEV, handle_mpath_get,
133 "Remove the mesh path to the given node.");
134
135 static int handle_mpath_set(struct nl80211_state *state,
136 struct nl_cb *cb,
137 struct nl_msg *msg,
138 int argc, char **argv,
139 enum id_input id)
140 {
141 unsigned char dst[ETH_ALEN];
142 unsigned char next_hop[ETH_ALEN];
143
144 if (argc < 3)
145 return 1;
146
147 if (mac_addr_a2n(dst, argv[0])) {
148 fprintf(stderr, "invalid destination mac address\n");
149 return 2;
150 }
151 argc--;
152 argv++;
153
154 if (strcmp("next_hop", argv[0]) != 0)
155 return 1;
156 argc--;
157 argv++;
158
159 if (mac_addr_a2n(next_hop, argv[0])) {
160 fprintf(stderr, "invalid next hop mac address\n");
161 return 2;
162 }
163 argc--;
164 argv++;
165
166 if (argc)
167 return 1;
168
169 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
170 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
171
172 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_mpath_handler, NULL);
173 return 0;
174 nla_put_failure:
175 return -ENOBUFS;
176 }
177 COMMAND(mpath, new, "<destination MAC address> next_hop <next hop MAC address>",
178 NL80211_CMD_NEW_MPATH, 0, CIB_NETDEV, handle_mpath_set,
179 "Create a new mesh path (instead of relying on automatic discovery).");
180 COMMAND(mpath, set, "<destination MAC address> next_hop <next hop MAC address>",
181 NL80211_CMD_SET_MPATH, 0, CIB_NETDEV, handle_mpath_set,
182 "Set an existing mesh path's next hop.");
183
184 static int handle_mpath_dump(struct nl80211_state *state,
185 struct nl_cb *cb,
186 struct nl_msg *msg,
187 int argc, char **argv,
188 enum id_input id)
189 {
190 printf("DEST ADDR NEXT HOP IFACE\tSN\tMETRIC\tQLEN\t"
191 "EXPTIME\t\tDTIM\tDRET\tFLAGS\n");
192 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_mpath_handler, NULL);
193 return 0;
194 }
195 COMMAND(mpath, dump, NULL,
196 NL80211_CMD_GET_MPATH, NLM_F_DUMP, CIB_NETDEV, handle_mpath_dump,
197 "List known mesh paths.");