]> git.ipfire.org Git - thirdparty/iw.git/blame - station.c
update nl80211.h
[thirdparty/iw.git] / station.c
CommitLineData
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
14enum plink_state {
15 LISTEN,
16 OPN_SNT,
17 OPN_RCVD,
18 CNF_RCVD,
19 ESTAB,
20 HOLDING,
21 BLOCKED
22};
23
24enum plink_actions {
25 PLINK_ACTION_UNDEFINED,
26 PLINK_ACTION_OPEN,
27 PLINK_ACTION_BLOCK,
28};
29
30
3d1e8704
LCC
31static int print_sta_handler(struct nl_msg *msg, void *arg)
32{
33 struct nlattr *tb[NL80211_ATTR_MAX + 1];
34 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
35 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
9cd3f1c1 36 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
3d1e8704
LCC
37 char mac_addr[20], state_name[10], dev[20];
38 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
39 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
40 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
41 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
9cd3f1c1
HR
42 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
43 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
3d1e8704
LCC
44 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
45 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
46 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
47 };
48
9cd3f1c1
HR
49 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
50 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
51 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
52 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
53 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
54 };
55
3d1e8704
LCC
56 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
57 genlmsg_attrlen(gnlh, 0), NULL);
58
59 /*
60 * TODO: validate the interface and mac address!
61 * Otherwise, there's a race condition as soon as
62 * the kernel starts sending station notifications.
63 */
64
65 if (!tb[NL80211_ATTR_STA_INFO]) {
66 fprintf(stderr, "sta stats missing!");
67 return NL_SKIP;
68 }
69 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
70 tb[NL80211_ATTR_STA_INFO],
71 stats_policy)) {
72 fprintf(stderr, "failed to parse nested attributes!");
73 return NL_SKIP;
74 }
75
76 mac_addr_n2a(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
77 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
fbb181fa 78 printf("Station %s (on %s)", mac_addr, dev);
3d1e8704
LCC
79
80 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
fbb181fa 81 printf("\n\tinactive time:\t%d ms",
3d1e8704
LCC
82 nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]));
83 if (sinfo[NL80211_STA_INFO_RX_BYTES])
fbb181fa 84 printf("\n\trx bytes:\t%d",
3d1e8704
LCC
85 nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]));
86 if (sinfo[NL80211_STA_INFO_TX_BYTES])
fbb181fa 87 printf("\n\ttx bytes:\t%d",
3d1e8704 88 nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]));
9cd3f1c1
HR
89 if (sinfo[NL80211_STA_INFO_SIGNAL])
90 printf("\n\tsignal: \t%d dBm",
91 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]));
92
93 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
94 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
95 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) {
96 fprintf(stderr, "failed to parse nested rate attributes!");
97 } else {
98 printf("\n\ttx bitrate:\t");
99 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
100 int rate = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
101 printf("%d.%d MBit/s", rate / 10, rate % 10);
102 }
103
104 if (rinfo[NL80211_RATE_INFO_MCS])
105 printf(" MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]));
106 if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
107 printf(" 40Mhz");
108 if (rinfo[NL80211_RATE_INFO_SHORT_GI])
109 printf(" short GI");
110 }
111 }
112
3d1e8704 113 if (sinfo[NL80211_STA_INFO_LLID])
fbb181fa 114 printf("\n\tmesh llid:\t%d",
3d1e8704
LCC
115 nla_get_u16(sinfo[NL80211_STA_INFO_LLID]));
116 if (sinfo[NL80211_STA_INFO_PLID])
fbb181fa 117 printf("\n\tmesh plid:\t%d",
3d1e8704
LCC
118 nla_get_u16(sinfo[NL80211_STA_INFO_PLID]));
119 if (sinfo[NL80211_STA_INFO_PLINK_STATE]) {
120 switch (nla_get_u16(sinfo[NL80211_STA_INFO_PLINK_STATE])) {
121 case LISTEN:
122 strcpy(state_name, "LISTEN");
123 break;
124 case OPN_SNT:
125 strcpy(state_name, "OPN_SNT");
126 break;
127 case OPN_RCVD:
128 strcpy(state_name, "OPN_RCVD");
129 break;
130 case CNF_RCVD:
131 strcpy(state_name, "CNF_RCVD");
132 break;
133 case ESTAB:
134 strcpy(state_name, "ESTAB");
135 break;
136 case HOLDING:
137 strcpy(state_name, "HOLDING");
138 break;
139 case BLOCKED:
140 strcpy(state_name, "BLOCKED");
141 break;
142 default:
143 strcpy(state_name, "UNKNOWN");
144 break;
145 }
fbb181fa 146 printf("\n\tmesh plink:\t%s", state_name);
3d1e8704
LCC
147 }
148
149 printf("\n");
150 return NL_SKIP;
151}
152
70391ccf 153static int handle_station_get(struct nl_cb *cb,
b1ca19a8
JB
154 struct nl_msg *msg,
155 int argc, char **argv)
3d1e8704 156{
3d1e8704
LCC
157 unsigned char mac_addr[ETH_ALEN];
158
b1ca19a8 159 if (argc < 1)
5e75fd04 160 return 1;
3d1e8704
LCC
161
162 if (mac_addr_a2n(mac_addr, argv[0])) {
163 fprintf(stderr, "invalid mac address\n");
5e75fd04 164 return 2;
3d1e8704
LCC
165 }
166
167 argc--;
168 argv++;
169
b1ca19a8 170 if (argc)
5e75fd04 171 return 1;
3d1e8704
LCC
172
173 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
3d1e8704 174
3d1e8704 175 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
3d1e8704 176
70391ccf 177 return 0;
3d1e8704 178 nla_put_failure:
70391ccf 179 return -ENOBUFS;
3d1e8704 180}
b1ca19a8
JB
181COMMAND(station, get, "<MAC address>",
182 NL80211_CMD_GET_STATION, 0, CIB_NETDEV, handle_station_get);
183COMMAND(station, del, "<MAC address>",
184 NL80211_CMD_DEL_STATION, 0, CIB_NETDEV, handle_station_get);
3d1e8704 185
70391ccf 186static int handle_station_set(struct nl_cb *cb,
b1ca19a8
JB
187 struct nl_msg *msg,
188 int argc, char **argv)
3d1e8704 189{
3d1e8704
LCC
190 unsigned char plink_action;
191 unsigned char mac_addr[ETH_ALEN];
192
b1ca19a8 193 if (argc < 3)
5e75fd04 194 return 1;
3d1e8704
LCC
195
196 if (mac_addr_a2n(mac_addr, argv[0])) {
197 fprintf(stderr, "invalid mac address\n");
5e75fd04 198 return 2;
3d1e8704
LCC
199 }
200 argc--;
201 argv++;
202
b1ca19a8
JB
203 if (strcmp("plink_action", argv[0]) != 0)
204 return 1;
3d1e8704
LCC
205 argc--;
206 argv++;
207
208 if (strcmp("open", argv[0]) == 0)
209 plink_action = PLINK_ACTION_OPEN;
210 else if (strcmp("block", argv[0]) == 0)
211 plink_action = PLINK_ACTION_BLOCK;
212 else {
213 fprintf(stderr, "plink action not supported\n");
5e75fd04 214 return 2;
3d1e8704
LCC
215 }
216 argc--;
217 argv++;
218
b1ca19a8 219 if (argc)
5e75fd04 220 return 1;
3d1e8704
LCC
221
222 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
3d1e8704
LCC
223 NLA_PUT_U8(msg, NL80211_ATTR_STA_PLINK_ACTION, plink_action);
224
70391ccf 225 return 0;
3d1e8704 226 nla_put_failure:
70391ccf 227 return -ENOBUFS;
3d1e8704 228}
b1ca19a8
JB
229COMMAND(station, set, "<MAC address> plink_action <open|block>",
230 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set);
231
70391ccf 232static int handle_station_dump(struct nl_cb *cb,
b1ca19a8
JB
233 struct nl_msg *msg,
234 int argc, char **argv)
3d1e8704 235{
3d1e8704 236 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
70391ccf 237 return 0;
3d1e8704 238}
b1ca19a8 239COMMAND(station, dump, NULL,
f903d035 240 NL80211_CMD_GET_STATION, NLM_F_DUMP, CIB_NETDEV, handle_station_dump);