]> git.ipfire.org Git - thirdparty/iw.git/blob - measurements.c
info: macro-ify ext_feat_print()
[thirdparty/iw.git] / measurements.c
1 #include <errno.h>
2
3 #include "nl80211.h"
4 #include "iw.h"
5 #include <unistd.h>
6
7 SECTION(measurement);
8
9 static int put_preamble(struct nl_msg *msg, char *s)
10 {
11 static const struct {
12 const char *name;
13 unsigned int val;
14 } preamble_map[] = {
15 { .name = "legacy", .val = NL80211_PREAMBLE_LEGACY, },
16 { .name = "ht", .val = NL80211_PREAMBLE_HT, },
17 { .name = "vht", .val = NL80211_PREAMBLE_VHT, },
18 { .name = "dmg", .val = NL80211_PREAMBLE_DMG, },
19 };
20 unsigned int i;
21
22 for (i = 0; i < ARRAY_SIZE(preamble_map); i++) {
23 if (strcasecmp(preamble_map[i].name, s) == 0) {
24 NLA_PUT_U32(msg, NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE,
25 preamble_map[i].val);
26 return 0;
27 }
28 }
29
30 nla_put_failure:
31 return -1;
32 }
33
34 static int parse_ftm_target(struct nl_msg *msg, char *str, int peer_index)
35 {
36 unsigned char addr[ETH_ALEN];
37 int res, consumed;
38 char *bw = NULL, *pos, *tmp, *save_ptr, *delims = " \t\n";
39 struct nlattr *peer, *req, *reqdata, *ftm, *chan;
40 bool report_ap_tsf = false, preamble = false;
41 unsigned int freq = 0, cf1 = 0, cf2 = 0;
42
43 res = sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx%n",
44 &addr[0], &addr[1], &addr[2], &addr[3], &addr[4], &addr[5],
45 &consumed);
46
47 if (res != ETH_ALEN) {
48 printf("Invalid MAC address\n");
49 return HANDLER_RET_USAGE;
50 }
51
52 peer = nla_nest_start(msg, peer_index);
53
54 NLA_PUT(msg, NL80211_PMSR_PEER_ATTR_ADDR, ETH_ALEN, addr);
55
56 req = nla_nest_start(msg, NL80211_PMSR_PEER_ATTR_REQ);
57 if (!req)
58 goto nla_put_failure;
59 reqdata = nla_nest_start(msg, NL80211_PMSR_REQ_ATTR_DATA);
60 if (!reqdata)
61 goto nla_put_failure;
62 ftm = nla_nest_start(msg, NL80211_PMSR_TYPE_FTM);
63 if (!ftm)
64 goto nla_put_failure;
65
66 str += consumed;
67 pos = strtok_r(str, delims, &save_ptr);
68
69 while (pos) {
70 if (strncmp(pos, "cf=", 3) == 0) {
71 freq = strtol(pos + 3, &tmp, 0);
72 if (*tmp) {
73 printf("Invalid cf value!\n");
74 return HANDLER_RET_USAGE;
75 }
76 } else if (strncmp(pos, "bw=", 3) == 0) {
77 bw = pos + 3;
78 } else if (strncmp(pos, "cf1=", 4) == 0) {
79 cf1 = strtol(pos + 4, &tmp, 0);
80 if (*tmp) {
81 printf("Invalid cf1 value!\n");
82 return HANDLER_RET_USAGE;
83 }
84 } else if (strncmp(pos, "cf2=", 4) == 0) {
85 cf2 = strtol(pos + 4, &tmp, 0);
86 if (*tmp) {
87 printf("Invalid cf2 value!\n");
88 return HANDLER_RET_USAGE;
89 }
90 } else if (strncmp(pos, "bursts_exp=", 11) == 0) {
91 NLA_PUT_U8(msg,
92 NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP,
93 strtol(pos + 11, &tmp, 0));
94 if (*tmp) {
95 printf("Invalid bursts_exp value!\n");
96 return HANDLER_RET_USAGE;
97 }
98 } else if (strncmp(pos, "burst_period=", 13) == 0) {
99 NLA_PUT_U16(msg, NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD,
100 strtol(pos + 13, &tmp, 0));
101 if (*tmp) {
102 printf("Invalid burst_period value!\n");
103 return HANDLER_RET_USAGE;
104 }
105 } else if (strncmp(pos, "retries=", 8) == 0) {
106 NLA_PUT_U8(msg,
107 NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES,
108 strtol(pos + 8, &tmp, 0));
109 if (*tmp) {
110 printf("Invalid retries value!\n");
111 return HANDLER_RET_USAGE;
112 }
113 } else if (strncmp(pos, "burst_duration=", 15) == 0) {
114 NLA_PUT_U8(msg,
115 NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION,
116 strtol(pos + 15, &tmp, 0));
117 if (*tmp) {
118 printf("Invalid burst_duration value!\n");
119 return HANDLER_RET_USAGE;
120 }
121 } else if (strncmp(pos, "ftms_per_burst=", 15) == 0) {
122 NLA_PUT_U8(msg,
123 NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST,
124 strtol(pos + 15, &tmp, 0));
125 if (*tmp) {
126 printf("Invalid ftms_per_burst value!\n");
127 return HANDLER_RET_USAGE;
128 }
129 } else if (strcmp(pos, "asap") == 0) {
130 NLA_PUT_FLAG(msg, NL80211_PMSR_FTM_REQ_ATTR_ASAP);
131 } else if (strcmp(pos, "ap-tsf") == 0) {
132 report_ap_tsf = true;
133 } else if (strcmp(pos, "civic") == 0) {
134 NLA_PUT_FLAG(msg, NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC);
135 } else if (strcmp(pos, "lci") == 0) {
136 NLA_PUT_FLAG(msg, NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI);
137 } else if (strncmp(pos, "preamble=", 9) == 0) {
138 if (put_preamble(msg, pos + 9)) {
139 printf("Invalid preamble %s\n", pos + 9);
140 return HANDLER_RET_USAGE;
141 }
142 preamble = true;
143 } else {
144 printf("Unknown parameter %s\n", pos);
145 return HANDLER_RET_USAGE;
146 }
147
148 pos = strtok_r(NULL, delims, &save_ptr);
149 }
150
151 if (!preamble) {
152 int preamble = -1;
153
154 switch (str_to_bw(bw)) {
155 case NL80211_CHAN_WIDTH_20_NOHT:
156 case NL80211_CHAN_WIDTH_5:
157 case NL80211_CHAN_WIDTH_10:
158 preamble = NL80211_PREAMBLE_LEGACY;
159 break;
160 case NL80211_CHAN_WIDTH_20:
161 case NL80211_CHAN_WIDTH_40:
162 preamble = NL80211_PREAMBLE_HT;
163 break;
164 case NL80211_CHAN_WIDTH_80:
165 case NL80211_CHAN_WIDTH_80P80:
166 case NL80211_CHAN_WIDTH_160:
167 preamble = NL80211_PREAMBLE_VHT;
168 break;
169 }
170
171 NLA_PUT_U32(msg, NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE, preamble);
172 }
173
174 nla_nest_end(msg, ftm);
175 if (report_ap_tsf)
176 NLA_PUT_FLAG(msg, NL80211_PMSR_REQ_ATTR_GET_AP_TSF);
177 nla_nest_end(msg, reqdata);
178 nla_nest_end(msg, req);
179
180 /* set the channel */
181 chan = nla_nest_start(msg, NL80211_PMSR_PEER_ATTR_CHAN);
182 if (!chan)
183 goto nla_put_failure;
184 if (freq)
185 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
186 if (cf1)
187 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, cf1);
188 if (cf2)
189 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2, cf2);
190 if (bw)
191 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
192 str_to_bw(bw));
193 nla_nest_end(msg, chan);
194
195 nla_nest_end(msg, peer);
196 return 0;
197 nla_put_failure:
198 return -ENOBUFS;
199 }
200
201 static int parse_ftm_config(struct nl_msg *msg, const char *file)
202 {
203 FILE *input;
204 char line[256];
205 int line_num;
206
207 input = fopen(file, "r");
208 if (!input) {
209 int err = errno;
210
211 printf("Failed to open file: %s\n", strerror(err));
212 return -err;
213 }
214
215 for (line_num = 1; fgets(line, sizeof(line), input); line_num++) {
216 if (line[0] == '#')
217 continue;
218
219 if (parse_ftm_target(msg, line, line_num)) {
220 printf("Invalid FTM configuration at line %d!\n",
221 line_num);
222 return HANDLER_RET_USAGE;
223 }
224 }
225
226 return 0;
227 }
228
229 static int handle_ftm_req(struct nl80211_state *state, struct nl_msg *msg,
230 int argc, char **argv, enum id_input id)
231 {
232 int err, i;
233 static char **req_argv;
234 static const __u32 wait[] = {
235 NL80211_CMD_PEER_MEASUREMENT_COMPLETE,
236 };
237 static const __u32 print[] = {
238 NL80211_CMD_PEER_MEASUREMENT_RESULT,
239 NL80211_CMD_PEER_MEASUREMENT_COMPLETE,
240 };
241 struct print_event_args printargs = { };
242
243 req_argv = calloc(argc + 1, sizeof(req_argv[0]));
244 req_argv[0] = argv[0];
245 req_argv[1] = "measurement";
246 req_argv[2] = "ftm_request_send";
247 for (i = 3; i < argc; i++)
248 req_argv[i] = argv[i];
249
250 err = handle_cmd(state, id, argc, req_argv);
251
252 free(req_argv);
253
254 if (err)
255 return err;
256
257 __do_listen_events(state,
258 ARRAY_SIZE(wait), wait,
259 ARRAY_SIZE(print), print,
260 &printargs);
261 return 0;
262 }
263
264 static int handle_ftm_req_send(struct nl80211_state *state, struct nl_msg *msg,
265 int argc, char **argv, enum id_input id)
266 {
267 struct nlattr *pmsr, *peers;
268 const char *file;
269 int err;
270
271 file = argv[0];
272 argc--;
273 argv++;
274 while (argc) {
275 if (strncmp(argv[0], "randomise", 9) == 0 ||
276 strncmp(argv[0], "randomize", 9) == 0) {
277 err = parse_random_mac_addr(msg, argv[0] + 9);
278 if (err)
279 return err;
280 } else if (strncmp(argv[0], "timeout=", 8) == 0) {
281 char *end;
282
283 NLA_PUT_U32(msg, NL80211_ATTR_TIMEOUT,
284 strtoul(argv[0] + 8, &end, 0));
285 if (*end)
286 return HANDLER_RET_USAGE;
287 } else {
288 return HANDLER_RET_USAGE;
289 }
290
291 argc--;
292 argv++;
293 }
294
295 pmsr = nla_nest_start(msg, NL80211_ATTR_PEER_MEASUREMENTS);
296 if (!pmsr)
297 goto nla_put_failure;
298 peers = nla_nest_start(msg, NL80211_PMSR_ATTR_PEERS);
299 if (!peers)
300 goto nla_put_failure;
301
302 err = parse_ftm_config(msg, file);
303 if (err)
304 return err;
305
306 nla_nest_end(msg, peers);
307 nla_nest_end(msg, pmsr);
308
309 return 0;
310
311 nla_put_failure:
312 return -ENOBUFS;
313 }
314 COMMAND(measurement, ftm_request, "<config-file> [timeout=<seconds>] [randomise[=<addr>/<mask>]]", 0, 0,
315 CIB_NETDEV, handle_ftm_req,
316 "Send an FTM request to the targets supplied in the config file.\n"
317 "Each line in the file represents a target, with the following format:\n"
318 "<addr> bw=<[20|40|80|80+80|160]> cf=<center_freq> [cf1=<center_freq1>] [cf2=<center_freq2>] [ftms_per_burst=<samples per burst>] [ap-tsf] [asap] [bursts_exp=<num of bursts exponent>] [burst_period=<burst period>] [retries=<num of retries>] [burst_duration=<burst duration>] [preamble=<legacy,ht,vht,dmg>] [lci] [civic]");
319 HIDDEN(measurement, ftm_request_send, "", NL80211_CMD_PEER_MEASUREMENT_START,
320 0, CIB_NETDEV, handle_ftm_req_send);