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