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