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