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