]> git.ipfire.org Git - thirdparty/iw.git/blob - station.c
allow sub-command selection
[thirdparty/iw.git] / station.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(station);
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_sta_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 *sinfo[NL80211_STA_INFO_MAX + 1];
38 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
39 char mac_addr[20], state_name[10], dev[20];
40 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
41 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
42 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
43 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
44 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
45 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
46 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
47 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
48 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
49 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
50 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
51 [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
52 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
53 };
54
55 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
56 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
57 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
58 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
59 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
60 };
61
62 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
63 genlmsg_attrlen(gnlh, 0), NULL);
64
65 /*
66 * TODO: validate the interface and mac address!
67 * Otherwise, there's a race condition as soon as
68 * the kernel starts sending station notifications.
69 */
70
71 if (!tb[NL80211_ATTR_STA_INFO]) {
72 fprintf(stderr, "sta stats missing!\n");
73 return NL_SKIP;
74 }
75 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
76 tb[NL80211_ATTR_STA_INFO],
77 stats_policy)) {
78 fprintf(stderr, "failed to parse nested attributes!\n");
79 return NL_SKIP;
80 }
81
82 mac_addr_n2a(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
83 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
84 printf("Station %s (on %s)", mac_addr, dev);
85
86 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
87 printf("\n\tinactive time:\t%u ms",
88 nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]));
89 if (sinfo[NL80211_STA_INFO_RX_BYTES])
90 printf("\n\trx bytes:\t%u",
91 nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]));
92 if (sinfo[NL80211_STA_INFO_RX_PACKETS])
93 printf("\n\trx packets:\t%u",
94 nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]));
95 if (sinfo[NL80211_STA_INFO_TX_BYTES])
96 printf("\n\ttx bytes:\t%u",
97 nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]));
98 if (sinfo[NL80211_STA_INFO_TX_PACKETS])
99 printf("\n\ttx packets:\t%u",
100 nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]));
101 if (sinfo[NL80211_STA_INFO_TX_RETRIES])
102 printf("\n\ttx retries:\t%u",
103 nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]));
104 if (sinfo[NL80211_STA_INFO_TX_FAILED])
105 printf("\n\ttx failed:\t%u",
106 nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]));
107 if (sinfo[NL80211_STA_INFO_SIGNAL])
108 printf("\n\tsignal: \t%d dBm",
109 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]));
110
111 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
112 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
113 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) {
114 fprintf(stderr, "failed to parse nested rate attributes!\n");
115 } else {
116 printf("\n\ttx bitrate:\t");
117 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
118 int rate = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
119 printf("%d.%d MBit/s", rate / 10, rate % 10);
120 }
121
122 if (rinfo[NL80211_RATE_INFO_MCS])
123 printf(" MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]));
124 if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
125 printf(" 40Mhz");
126 if (rinfo[NL80211_RATE_INFO_SHORT_GI])
127 printf(" short GI");
128 }
129 }
130
131 if (sinfo[NL80211_STA_INFO_LLID])
132 printf("\n\tmesh llid:\t%d",
133 nla_get_u16(sinfo[NL80211_STA_INFO_LLID]));
134 if (sinfo[NL80211_STA_INFO_PLID])
135 printf("\n\tmesh plid:\t%d",
136 nla_get_u16(sinfo[NL80211_STA_INFO_PLID]));
137 if (sinfo[NL80211_STA_INFO_PLINK_STATE]) {
138 switch (nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE])) {
139 case LISTEN:
140 strcpy(state_name, "LISTEN");
141 break;
142 case OPN_SNT:
143 strcpy(state_name, "OPN_SNT");
144 break;
145 case OPN_RCVD:
146 strcpy(state_name, "OPN_RCVD");
147 break;
148 case CNF_RCVD:
149 strcpy(state_name, "CNF_RCVD");
150 break;
151 case ESTAB:
152 strcpy(state_name, "ESTAB");
153 break;
154 case HOLDING:
155 strcpy(state_name, "HOLDING");
156 break;
157 case BLOCKED:
158 strcpy(state_name, "BLOCKED");
159 break;
160 default:
161 strcpy(state_name, "UNKNOWN");
162 break;
163 }
164 printf("\n\tmesh plink:\t%s", state_name);
165 }
166
167 printf("\n");
168 return NL_SKIP;
169 }
170
171 static int handle_station_get(struct nl80211_state *state,
172 struct nl_cb *cb,
173 struct nl_msg *msg,
174 int argc, char **argv)
175 {
176 unsigned char mac_addr[ETH_ALEN];
177
178 if (argc < 1)
179 return 1;
180
181 if (mac_addr_a2n(mac_addr, argv[0])) {
182 fprintf(stderr, "invalid mac address\n");
183 return 2;
184 }
185
186 argc--;
187 argv++;
188
189 if (argc)
190 return 1;
191
192 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
193
194 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
195
196 return 0;
197 nla_put_failure:
198 return -ENOBUFS;
199 }
200 COMMAND(station, get, "<MAC address>",
201 NL80211_CMD_GET_STATION, 0, CIB_NETDEV, handle_station_get,
202 "Get information for a specific station.");
203 COMMAND(station, del, "<MAC address>",
204 NL80211_CMD_DEL_STATION, 0, CIB_NETDEV, handle_station_get,
205 "Remove the given station entry (use with caution!)");
206
207 static const struct cmd *station_set_plink;
208 static const struct cmd *station_set_vlan;
209
210 static const struct cmd *select_station_cmd(int argc, char **argv)
211 {
212 if (argc < 2)
213 return NULL;
214 if (strcmp(argv[1], "plink_action") == 0)
215 return station_set_plink;
216 if (strcmp(argv[1], "vlan") == 0)
217 return station_set_vlan;
218 return NULL;
219 }
220
221 static int handle_station_set_plink(struct nl80211_state *state,
222 struct nl_cb *cb,
223 struct nl_msg *msg,
224 int argc, char **argv)
225 {
226 unsigned char plink_action;
227 unsigned char mac_addr[ETH_ALEN];
228
229 if (argc < 3)
230 return 1;
231
232 if (mac_addr_a2n(mac_addr, argv[0])) {
233 fprintf(stderr, "invalid mac address\n");
234 return 2;
235 }
236 argc--;
237 argv++;
238
239 if (strcmp("plink_action", argv[0]) != 0)
240 return 1;
241 argc--;
242 argv++;
243
244 if (strcmp("open", argv[0]) == 0)
245 plink_action = PLINK_ACTION_OPEN;
246 else if (strcmp("block", argv[0]) == 0)
247 plink_action = PLINK_ACTION_BLOCK;
248 else {
249 fprintf(stderr, "plink action not supported\n");
250 return 2;
251 }
252 argc--;
253 argv++;
254
255 if (argc)
256 return 1;
257
258 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
259 NLA_PUT_U8(msg, NL80211_ATTR_STA_PLINK_ACTION, plink_action);
260
261 return 0;
262 nla_put_failure:
263 return -ENOBUFS;
264 }
265 COMMAND_ALIAS(station, set, "<MAC address> plink_action <open|block>",
266 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_plink,
267 "Set mesh peer link action for this station (peer).",
268 select_station_cmd, station_set_plink);
269
270 static int handle_station_set_vlan(struct nl80211_state *state,
271 struct nl_cb *cb,
272 struct nl_msg *msg,
273 int argc, char **argv)
274 {
275 unsigned char mac_addr[ETH_ALEN];
276 unsigned long sta_vlan = 0;
277 char *err = NULL;
278
279 if (argc < 3)
280 return 1;
281
282 if (mac_addr_a2n(mac_addr, argv[0])) {
283 fprintf(stderr, "invalid mac address\n");
284 return 2;
285 }
286 argc--;
287 argv++;
288
289 if (strcmp("vlan", argv[0]) != 0)
290 return 1;
291 argc--;
292 argv++;
293
294 sta_vlan = strtoul(argv[0], &err, 0);
295 if (err && *err) {
296 fprintf(stderr, "invalid vlan id\n");
297 return 2;
298 }
299 argc--;
300 argv++;
301
302 if (argc)
303 return 1;
304
305 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
306 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN, sta_vlan);
307
308 return 0;
309 nla_put_failure:
310 return -ENOBUFS;
311 }
312 COMMAND_ALIAS(station, set, "<MAC address> vlan <ifindex>",
313 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_vlan,
314 "Set an AP VLAN for this station.",
315 select_station_cmd, station_set_vlan);
316
317
318 static int handle_station_dump(struct nl80211_state *state,
319 struct nl_cb *cb,
320 struct nl_msg *msg,
321 int argc, char **argv)
322 {
323 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
324 return 0;
325 }
326 COMMAND(station, dump, NULL,
327 NL80211_CMD_GET_STATION, NLM_F_DUMP, CIB_NETDEV, handle_station_dump,
328 "List all stations known, e.g. the AP on managed interfaces");