]>
Commit | Line | Data |
---|---|---|
2ef1be68 | 1 | #include <net/if.h> |
3d1e8704 | 2 | #include <errno.h> |
d5ac8ad3 | 3 | #include <string.h> |
2ef1be68 | 4 | |
3d1e8704 LCC |
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> | |
3d1e8704 | 10 | |
f408e01b | 11 | #include "nl80211.h" |
3d1e8704 LCC |
12 | #include "iw.h" |
13 | ||
4698bfc2 JB |
14 | SECTION(station); |
15 | ||
3d1e8704 LCC |
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 | ||
3d1e8704 LCC |
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]; | |
9cd3f1c1 | 38 | struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1]; |
3d1e8704 | 39 | char mac_addr[20], state_name[10], dev[20]; |
b638215d | 40 | struct nl80211_sta_flag_update *sta_flags; |
3d1e8704 LCC |
41 | static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = { |
42 | [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 }, | |
43 | [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 }, | |
44 | [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 }, | |
859677cb JB |
45 | [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 }, |
46 | [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 }, | |
9cd3f1c1 | 47 | [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 }, |
ea3ade85 | 48 | [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 }, |
9cd3f1c1 | 49 | [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED }, |
3d1e8704 LCC |
50 | [NL80211_STA_INFO_LLID] = { .type = NLA_U16 }, |
51 | [NL80211_STA_INFO_PLID] = { .type = NLA_U16 }, | |
52 | [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 }, | |
0f5868ee BR |
53 | [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 }, |
54 | [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 }, | |
b638215d HS |
55 | [NL80211_STA_INFO_STA_FLAGS] = |
56 | { .minlen = sizeof(struct nl80211_sta_flag_update) }, | |
3d1e8704 LCC |
57 | }; |
58 | ||
9cd3f1c1 HR |
59 | static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = { |
60 | [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 }, | |
f0148acd | 61 | [NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 }, |
9cd3f1c1 HR |
62 | [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 }, |
63 | [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG }, | |
64 | [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG }, | |
65 | }; | |
66 | ||
3d1e8704 LCC |
67 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), |
68 | genlmsg_attrlen(gnlh, 0), NULL); | |
69 | ||
70 | /* | |
71 | * TODO: validate the interface and mac address! | |
72 | * Otherwise, there's a race condition as soon as | |
73 | * the kernel starts sending station notifications. | |
74 | */ | |
75 | ||
76 | if (!tb[NL80211_ATTR_STA_INFO]) { | |
5fe70c0e | 77 | fprintf(stderr, "sta stats missing!\n"); |
3d1e8704 LCC |
78 | return NL_SKIP; |
79 | } | |
80 | if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, | |
81 | tb[NL80211_ATTR_STA_INFO], | |
82 | stats_policy)) { | |
5fe70c0e | 83 | fprintf(stderr, "failed to parse nested attributes!\n"); |
3d1e8704 LCC |
84 | return NL_SKIP; |
85 | } | |
86 | ||
87 | mac_addr_n2a(mac_addr, nla_data(tb[NL80211_ATTR_MAC])); | |
88 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev); | |
fbb181fa | 89 | printf("Station %s (on %s)", mac_addr, dev); |
3d1e8704 LCC |
90 | |
91 | if (sinfo[NL80211_STA_INFO_INACTIVE_TIME]) | |
f5c9799c | 92 | printf("\n\tinactive time:\t%u ms", |
3d1e8704 LCC |
93 | nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME])); |
94 | if (sinfo[NL80211_STA_INFO_RX_BYTES]) | |
f5c9799c | 95 | printf("\n\trx bytes:\t%u", |
3d1e8704 | 96 | nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES])); |
859677cb | 97 | if (sinfo[NL80211_STA_INFO_RX_PACKETS]) |
f5c9799c | 98 | printf("\n\trx packets:\t%u", |
859677cb | 99 | nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS])); |
3d1e8704 | 100 | if (sinfo[NL80211_STA_INFO_TX_BYTES]) |
f5c9799c | 101 | printf("\n\ttx bytes:\t%u", |
3d1e8704 | 102 | nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES])); |
859677cb | 103 | if (sinfo[NL80211_STA_INFO_TX_PACKETS]) |
f5c9799c | 104 | printf("\n\ttx packets:\t%u", |
859677cb | 105 | nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS])); |
0f5868ee BR |
106 | if (sinfo[NL80211_STA_INFO_TX_RETRIES]) |
107 | printf("\n\ttx retries:\t%u", | |
108 | nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES])); | |
109 | if (sinfo[NL80211_STA_INFO_TX_FAILED]) | |
110 | printf("\n\ttx failed:\t%u", | |
111 | nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED])); | |
9cd3f1c1 HR |
112 | if (sinfo[NL80211_STA_INFO_SIGNAL]) |
113 | printf("\n\tsignal: \t%d dBm", | |
114 | (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL])); | |
ba292ae9 BR |
115 | if (sinfo[NL80211_STA_INFO_SIGNAL_AVG]) |
116 | printf("\n\tsignal avg:\t%d dBm", | |
117 | (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG])); | |
ea3ade85 AN |
118 | if (sinfo[NL80211_STA_INFO_T_OFFSET]) |
119 | printf("\n\tToffset:\t%lld us", | |
120 | (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET])); | |
9cd3f1c1 HR |
121 | |
122 | if (sinfo[NL80211_STA_INFO_TX_BITRATE]) { | |
123 | if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, | |
124 | sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) { | |
5fe70c0e | 125 | fprintf(stderr, "failed to parse nested rate attributes!\n"); |
9cd3f1c1 | 126 | } else { |
f0148acd | 127 | int rate = 0; |
9cd3f1c1 | 128 | printf("\n\ttx bitrate:\t"); |
f0148acd VK |
129 | if (rinfo[NL80211_RATE_INFO_BITRATE32]) |
130 | rate = nla_get_u32(rinfo[NL80211_RATE_INFO_BITRATE32]); | |
131 | else if (rinfo[NL80211_RATE_INFO_BITRATE]) | |
132 | rate = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]); | |
133 | if (rate > 0) | |
9cd3f1c1 | 134 | printf("%d.%d MBit/s", rate / 10, rate % 10); |
9cd3f1c1 HR |
135 | |
136 | if (rinfo[NL80211_RATE_INFO_MCS]) | |
137 | printf(" MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_MCS])); | |
138 | if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH]) | |
139 | printf(" 40Mhz"); | |
140 | if (rinfo[NL80211_RATE_INFO_SHORT_GI]) | |
141 | printf(" short GI"); | |
142 | } | |
143 | } | |
144 | ||
3d1e8704 | 145 | if (sinfo[NL80211_STA_INFO_LLID]) |
fbb181fa | 146 | printf("\n\tmesh llid:\t%d", |
3d1e8704 LCC |
147 | nla_get_u16(sinfo[NL80211_STA_INFO_LLID])); |
148 | if (sinfo[NL80211_STA_INFO_PLID]) | |
fbb181fa | 149 | printf("\n\tmesh plid:\t%d", |
3d1e8704 LCC |
150 | nla_get_u16(sinfo[NL80211_STA_INFO_PLID])); |
151 | if (sinfo[NL80211_STA_INFO_PLINK_STATE]) { | |
dbaabba1 | 152 | switch (nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE])) { |
3d1e8704 LCC |
153 | case LISTEN: |
154 | strcpy(state_name, "LISTEN"); | |
155 | break; | |
156 | case OPN_SNT: | |
157 | strcpy(state_name, "OPN_SNT"); | |
158 | break; | |
159 | case OPN_RCVD: | |
160 | strcpy(state_name, "OPN_RCVD"); | |
161 | break; | |
162 | case CNF_RCVD: | |
163 | strcpy(state_name, "CNF_RCVD"); | |
164 | break; | |
165 | case ESTAB: | |
166 | strcpy(state_name, "ESTAB"); | |
167 | break; | |
168 | case HOLDING: | |
169 | strcpy(state_name, "HOLDING"); | |
170 | break; | |
171 | case BLOCKED: | |
172 | strcpy(state_name, "BLOCKED"); | |
173 | break; | |
174 | default: | |
175 | strcpy(state_name, "UNKNOWN"); | |
176 | break; | |
177 | } | |
fbb181fa | 178 | printf("\n\tmesh plink:\t%s", state_name); |
3d1e8704 LCC |
179 | } |
180 | ||
b638215d HS |
181 | if (sinfo[NL80211_STA_INFO_STA_FLAGS]) { |
182 | sta_flags = (struct nl80211_sta_flag_update *) | |
183 | nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]); | |
184 | ||
185 | if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) { | |
186 | printf("\n\tauthorized:\t"); | |
187 | if (sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED)) | |
188 | printf("yes"); | |
189 | else | |
190 | printf("no"); | |
191 | } | |
192 | ||
193 | if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED)) { | |
194 | printf("\n\tauthenticated:\t"); | |
195 | if (sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED)) | |
196 | printf("yes"); | |
197 | else | |
198 | printf("no"); | |
199 | } | |
200 | ||
201 | if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) { | |
202 | printf("\n\tpreamble:\t"); | |
203 | if (sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) | |
204 | printf("short"); | |
205 | else | |
206 | printf("long"); | |
207 | } | |
208 | ||
209 | if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME)) { | |
210 | printf("\n\tWMM/WME:\t"); | |
211 | if (sta_flags->set & BIT(NL80211_STA_FLAG_WME)) | |
212 | printf("yes"); | |
213 | else | |
214 | printf("no"); | |
215 | } | |
216 | ||
217 | if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP)) { | |
218 | printf("\n\tMFP:\t\t"); | |
219 | if (sta_flags->set & BIT(NL80211_STA_FLAG_MFP)) | |
220 | printf("yes"); | |
221 | else | |
222 | printf("no"); | |
223 | } | |
224 | ||
225 | if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER)) { | |
226 | printf("\n\tTDLS peer:\t\t"); | |
227 | if (sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER)) | |
228 | printf("yes"); | |
229 | else | |
230 | printf("no"); | |
231 | } | |
232 | } | |
233 | ||
3d1e8704 LCC |
234 | printf("\n"); |
235 | return NL_SKIP; | |
236 | } | |
237 | ||
7c37a24d JB |
238 | static int handle_station_get(struct nl80211_state *state, |
239 | struct nl_cb *cb, | |
b1ca19a8 | 240 | struct nl_msg *msg, |
05514f95 JB |
241 | int argc, char **argv, |
242 | enum id_input id) | |
3d1e8704 | 243 | { |
3d1e8704 LCC |
244 | unsigned char mac_addr[ETH_ALEN]; |
245 | ||
b1ca19a8 | 246 | if (argc < 1) |
5e75fd04 | 247 | return 1; |
3d1e8704 LCC |
248 | |
249 | if (mac_addr_a2n(mac_addr, argv[0])) { | |
250 | fprintf(stderr, "invalid mac address\n"); | |
5e75fd04 | 251 | return 2; |
3d1e8704 LCC |
252 | } |
253 | ||
254 | argc--; | |
255 | argv++; | |
256 | ||
b1ca19a8 | 257 | if (argc) |
5e75fd04 | 258 | return 1; |
3d1e8704 LCC |
259 | |
260 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr); | |
3d1e8704 | 261 | |
3d1e8704 | 262 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL); |
3d1e8704 | 263 | |
70391ccf | 264 | return 0; |
3d1e8704 | 265 | nla_put_failure: |
70391ccf | 266 | return -ENOBUFS; |
3d1e8704 | 267 | } |
b1ca19a8 | 268 | COMMAND(station, get, "<MAC address>", |
70cf4544 JB |
269 | NL80211_CMD_GET_STATION, 0, CIB_NETDEV, handle_station_get, |
270 | "Get information for a specific station."); | |
b1ca19a8 | 271 | COMMAND(station, del, "<MAC address>", |
70cf4544 JB |
272 | NL80211_CMD_DEL_STATION, 0, CIB_NETDEV, handle_station_get, |
273 | "Remove the given station entry (use with caution!)"); | |
3d1e8704 | 274 | |
1633ddf7 JB |
275 | static const struct cmd *station_set_plink; |
276 | static const struct cmd *station_set_vlan; | |
277 | ||
278 | static const struct cmd *select_station_cmd(int argc, char **argv) | |
279 | { | |
280 | if (argc < 2) | |
281 | return NULL; | |
282 | if (strcmp(argv[1], "plink_action") == 0) | |
283 | return station_set_plink; | |
284 | if (strcmp(argv[1], "vlan") == 0) | |
285 | return station_set_vlan; | |
286 | return NULL; | |
287 | } | |
288 | ||
ce0fc33a | 289 | static int handle_station_set_plink(struct nl80211_state *state, |
7c37a24d | 290 | struct nl_cb *cb, |
b1ca19a8 | 291 | struct nl_msg *msg, |
05514f95 JB |
292 | int argc, char **argv, |
293 | enum id_input id) | |
3d1e8704 | 294 | { |
3d1e8704 LCC |
295 | unsigned char plink_action; |
296 | unsigned char mac_addr[ETH_ALEN]; | |
297 | ||
b1ca19a8 | 298 | if (argc < 3) |
5e75fd04 | 299 | return 1; |
3d1e8704 LCC |
300 | |
301 | if (mac_addr_a2n(mac_addr, argv[0])) { | |
302 | fprintf(stderr, "invalid mac address\n"); | |
5e75fd04 | 303 | return 2; |
3d1e8704 LCC |
304 | } |
305 | argc--; | |
306 | argv++; | |
307 | ||
b1ca19a8 JB |
308 | if (strcmp("plink_action", argv[0]) != 0) |
309 | return 1; | |
3d1e8704 LCC |
310 | argc--; |
311 | argv++; | |
312 | ||
313 | if (strcmp("open", argv[0]) == 0) | |
314 | plink_action = PLINK_ACTION_OPEN; | |
315 | else if (strcmp("block", argv[0]) == 0) | |
316 | plink_action = PLINK_ACTION_BLOCK; | |
317 | else { | |
318 | fprintf(stderr, "plink action not supported\n"); | |
5e75fd04 | 319 | return 2; |
3d1e8704 LCC |
320 | } |
321 | argc--; | |
322 | argv++; | |
323 | ||
b1ca19a8 | 324 | if (argc) |
5e75fd04 | 325 | return 1; |
3d1e8704 LCC |
326 | |
327 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr); | |
3d1e8704 LCC |
328 | NLA_PUT_U8(msg, NL80211_ATTR_STA_PLINK_ACTION, plink_action); |
329 | ||
70391ccf | 330 | return 0; |
3d1e8704 | 331 | nla_put_failure: |
70391ccf | 332 | return -ENOBUFS; |
3d1e8704 | 333 | } |
1633ddf7 | 334 | COMMAND_ALIAS(station, set, "<MAC address> plink_action <open|block>", |
ce0fc33a | 335 | NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_plink, |
1633ddf7 JB |
336 | "Set mesh peer link action for this station (peer).", |
337 | select_station_cmd, station_set_plink); | |
b1ca19a8 | 338 | |
ce0fc33a | 339 | static int handle_station_set_vlan(struct nl80211_state *state, |
05514f95 JB |
340 | struct nl_cb *cb, |
341 | struct nl_msg *msg, | |
342 | int argc, char **argv, | |
343 | enum id_input id) | |
ce0fc33a FF |
344 | { |
345 | unsigned char mac_addr[ETH_ALEN]; | |
346 | unsigned long sta_vlan = 0; | |
347 | char *err = NULL; | |
348 | ||
349 | if (argc < 3) | |
350 | return 1; | |
351 | ||
352 | if (mac_addr_a2n(mac_addr, argv[0])) { | |
353 | fprintf(stderr, "invalid mac address\n"); | |
354 | return 2; | |
355 | } | |
356 | argc--; | |
357 | argv++; | |
358 | ||
359 | if (strcmp("vlan", argv[0]) != 0) | |
360 | return 1; | |
361 | argc--; | |
362 | argv++; | |
363 | ||
364 | sta_vlan = strtoul(argv[0], &err, 0); | |
365 | if (err && *err) { | |
366 | fprintf(stderr, "invalid vlan id\n"); | |
367 | return 2; | |
368 | } | |
369 | argc--; | |
370 | argv++; | |
371 | ||
372 | if (argc) | |
373 | return 1; | |
374 | ||
375 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr); | |
376 | NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN, sta_vlan); | |
377 | ||
378 | return 0; | |
379 | nla_put_failure: | |
380 | return -ENOBUFS; | |
381 | } | |
1633ddf7 | 382 | COMMAND_ALIAS(station, set, "<MAC address> vlan <ifindex>", |
ce0fc33a | 383 | NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_vlan, |
1633ddf7 JB |
384 | "Set an AP VLAN for this station.", |
385 | select_station_cmd, station_set_vlan); | |
ce0fc33a FF |
386 | |
387 | ||
7c37a24d JB |
388 | static int handle_station_dump(struct nl80211_state *state, |
389 | struct nl_cb *cb, | |
b1ca19a8 | 390 | struct nl_msg *msg, |
05514f95 JB |
391 | int argc, char **argv, |
392 | enum id_input id) | |
3d1e8704 | 393 | { |
3d1e8704 | 394 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL); |
70391ccf | 395 | return 0; |
3d1e8704 | 396 | } |
b1ca19a8 | 397 | COMMAND(station, dump, NULL, |
70cf4544 JB |
398 | NL80211_CMD_GET_STATION, NLM_F_DUMP, CIB_NETDEV, handle_station_dump, |
399 | "List all stations known, e.g. the AP on managed interfaces"); |