]> git.ipfire.org Git - thirdparty/iw.git/blob - station.c
finish docs
[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 nl80211_state *state,
162 struct nl_cb *cb,
163 struct nl_msg *msg,
164 int argc, char **argv)
165 {
166 unsigned char mac_addr[ETH_ALEN];
167
168 if (argc < 1)
169 return 1;
170
171 if (mac_addr_a2n(mac_addr, argv[0])) {
172 fprintf(stderr, "invalid mac address\n");
173 return 2;
174 }
175
176 argc--;
177 argv++;
178
179 if (argc)
180 return 1;
181
182 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
183
184 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
185
186 return 0;
187 nla_put_failure:
188 return -ENOBUFS;
189 }
190 COMMAND(station, get, "<MAC address>",
191 NL80211_CMD_GET_STATION, 0, CIB_NETDEV, handle_station_get,
192 "Get information for a specific station.");
193 COMMAND(station, del, "<MAC address>",
194 NL80211_CMD_DEL_STATION, 0, CIB_NETDEV, handle_station_get,
195 "Remove the given station entry (use with caution!)");
196
197 static int handle_station_set(struct nl80211_state *state,
198 struct nl_cb *cb,
199 struct nl_msg *msg,
200 int argc, char **argv)
201 {
202 unsigned char plink_action;
203 unsigned char mac_addr[ETH_ALEN];
204
205 if (argc < 3)
206 return 1;
207
208 if (mac_addr_a2n(mac_addr, argv[0])) {
209 fprintf(stderr, "invalid mac address\n");
210 return 2;
211 }
212 argc--;
213 argv++;
214
215 if (strcmp("plink_action", argv[0]) != 0)
216 return 1;
217 argc--;
218 argv++;
219
220 if (strcmp("open", argv[0]) == 0)
221 plink_action = PLINK_ACTION_OPEN;
222 else if (strcmp("block", argv[0]) == 0)
223 plink_action = PLINK_ACTION_BLOCK;
224 else {
225 fprintf(stderr, "plink action not supported\n");
226 return 2;
227 }
228 argc--;
229 argv++;
230
231 if (argc)
232 return 1;
233
234 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
235 NLA_PUT_U8(msg, NL80211_ATTR_STA_PLINK_ACTION, plink_action);
236
237 return 0;
238 nla_put_failure:
239 return -ENOBUFS;
240 }
241 COMMAND(station, set, "<MAC address> plink_action <open|block>",
242 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set,
243 "Set mesh peer link action for this station (peer).");
244
245 static int handle_station_dump(struct nl80211_state *state,
246 struct nl_cb *cb,
247 struct nl_msg *msg,
248 int argc, char **argv)
249 {
250 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
251 return 0;
252 }
253 COMMAND(station, dump, NULL,
254 NL80211_CMD_GET_STATION, NLM_F_DUMP, CIB_NETDEV, handle_station_dump,
255 "List all stations known, e.g. the AP on managed interfaces");