blob: 7f8ce26a70b0290c265e0278151b5d854fda46ea [file] [log] [blame]
Johannes Berg379f8392008-12-08 12:53:58 +01001#include <stdbool.h>
Johannes Berg0f55e0b2008-09-16 17:40:48 +02002#include <errno.h>
Johannes Berg0f55e0b2008-09-16 17:40:48 +02003#include <net/if.h>
Johannes Bergd0260392010-09-22 11:33:28 +02004#include <strings.h>
Johannes Berg0f55e0b2008-09-16 17:40:48 +02005
6#include <netlink/genl/genl.h>
7#include <netlink/genl/family.h>
8#include <netlink/genl/ctrl.h>
9#include <netlink/msg.h>
10#include <netlink/attr.h>
11
Johannes Bergf408e012008-09-18 19:32:11 +020012#include "nl80211.h"
Johannes Berg0f55e0b2008-09-16 17:40:48 +020013#include "iw.h"
14
Johannes Berg7c37a242009-04-08 13:13:28 +020015static int handle_name(struct nl80211_state *state,
16 struct nl_cb *cb,
Johannes Berg0f55e0b2008-09-16 17:40:48 +020017 struct nl_msg *msg,
Johannes Berg05514f92012-07-19 11:50:50 +020018 int argc, char **argv,
19 enum id_input id)
Johannes Berg0f55e0b2008-09-16 17:40:48 +020020{
Johannes Berg0f55e0b2008-09-16 17:40:48 +020021 if (argc != 1)
Johannes Berg5e75fd02008-09-16 18:13:12 +020022 return 1;
Johannes Berg0f55e0b2008-09-16 17:40:48 +020023
24 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, *argv);
25
Johannes Berg70391cc2008-09-16 18:35:06 +020026 return 0;
Johannes Berg0f55e0b2008-09-16 17:40:48 +020027 nla_put_failure:
Johannes Berg70391cc2008-09-16 18:35:06 +020028 return -ENOBUFS;
Johannes Berg0f55e0b2008-09-16 17:40:48 +020029}
Johannes Bergcea8fa12009-05-05 15:05:10 +020030COMMAND(set, name, "<new name>", NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_name,
31 "Rename this wireless device.");
Johannes Bergb822cda2008-12-08 12:43:42 +010032
Johannes Berg7c60bb72012-11-20 16:52:12 +010033static int handle_freqs(struct nl_msg *msg, int argc, char **argv)
34{
35 static const struct {
36 const char *name;
37 unsigned int val;
38 } bwmap[] = {
39 { .name = "20", .val = NL80211_CHAN_WIDTH_20, },
40 { .name = "40", .val = NL80211_CHAN_WIDTH_40, },
41 { .name = "80", .val = NL80211_CHAN_WIDTH_80, },
42 { .name = "80+80", .val = NL80211_CHAN_WIDTH_80P80, },
43 { .name = "160", .val = NL80211_CHAN_WIDTH_160, },
44 };
45 uint32_t freq;
46 int i, bwval = NL80211_CHAN_WIDTH_20_NOHT;
47 char *end;
48
49 if (argc < 1)
50 return 1;
51
52 for (i = 0; i < ARRAY_SIZE(bwmap); i++) {
53 if (strcasecmp(bwmap[i].name, argv[0]) == 0) {
54 bwval = bwmap[i].val;
55 break;
56 }
57 }
58
59 if (bwval == NL80211_CHAN_WIDTH_20_NOHT)
60 return 1;
61
62 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, bwval);
63
64 if (argc == 1)
65 return 0;
66
67 /* center freq 1 */
68 if (!*argv[1])
69 return 1;
70 freq = strtoul(argv[1], &end, 10);
71 if (*end)
72 return 1;
73 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq);
74
75 if (argc == 2)
76 return 0;
77
78 /* center freq 2 */
79 if (!*argv[2])
80 return 1;
81 freq = strtoul(argv[2], &end, 10);
82 if (*end)
83 return 1;
84 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2, freq);
85
86 return 0;
87 nla_put_failure:
88 return -ENOBUFS;
89}
90
Johannes Berg379f8392008-12-08 12:53:58 +010091static int handle_freqchan(struct nl_msg *msg, bool chan,
92 int argc, char **argv)
Johannes Bergb822cda2008-12-08 12:43:42 +010093{
Johannes Berge86b7e02010-04-01 18:49:21 +020094 char *end;
Johannes Bergb822cda2008-12-08 12:43:42 +010095 static const struct {
96 const char *name;
97 unsigned int val;
98 } htmap[] = {
Johannes Berg68632dc2008-12-12 22:40:45 +010099 { .name = "HT20", .val = NL80211_CHAN_HT20, },
100 { .name = "HT40+", .val = NL80211_CHAN_HT40PLUS, },
101 { .name = "HT40-", .val = NL80211_CHAN_HT40MINUS, },
Johannes Bergb822cda2008-12-08 12:43:42 +0100102 };
Johannes Berg68632dc2008-12-12 22:40:45 +0100103 unsigned int htval = NL80211_CHAN_NO_HT;
Johannes Bergb822cda2008-12-08 12:43:42 +0100104 unsigned int freq;
105 int i;
106
Johannes Berg7c60bb72012-11-20 16:52:12 +0100107 if (!argc || argc > 4)
Johannes Bergb822cda2008-12-08 12:43:42 +0100108 return 1;
109
Johannes Berge86b7e02010-04-01 18:49:21 +0200110 if (!*argv[0])
111 return 1;
112 freq = strtoul(argv[0], &end, 10);
113 if (*end)
114 return 1;
115
Johannes Berg379f8392008-12-08 12:53:58 +0100116 if (chan)
117 freq = ieee80211_channel_to_frequency(freq);
Johannes Bergb822cda2008-12-08 12:43:42 +0100118
119 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Johannes Berg7c60bb72012-11-20 16:52:12 +0100120
121 if (argc > 2) {
122 return handle_freqs(msg, argc - 1, argv + 1);
123 } else if (argc == 2) {
124 for (i = 0; i < ARRAY_SIZE(htmap); i++) {
125 if (strcasecmp(htmap[i].name, argv[1]) == 0) {
126 htval = htmap[i].val;
127 break;
128 }
129 }
130 if (htval == NL80211_CHAN_NO_HT)
131 return handle_freqs(msg, argc - 1, argv + 1);
132 }
133
Johannes Berg68632dc2008-12-12 22:40:45 +0100134 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, htval);
Johannes Bergb822cda2008-12-08 12:43:42 +0100135
136 return 0;
137 nla_put_failure:
138 return -ENOBUFS;
139}
Johannes Berg379f8392008-12-08 12:53:58 +0100140
Johannes Berg7c37a242009-04-08 13:13:28 +0200141static int handle_freq(struct nl80211_state *state,
142 struct nl_cb *cb, struct nl_msg *msg,
Johannes Berg05514f92012-07-19 11:50:50 +0200143 int argc, char **argv,
144 enum id_input id)
Johannes Berg379f8392008-12-08 12:53:58 +0100145{
146 return handle_freqchan(msg, false, argc, argv);
147}
Johannes Bergb822cda2008-12-08 12:43:42 +0100148COMMAND(set, freq, "<freq> [HT20|HT40+|HT40-]",
Johannes Berg00c448b2009-05-05 15:04:19 +0200149 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_freq,
150 "Set frequency/channel the hardware is using, including HT\n"
151 "configuration.");
Johannes Berg7c60bb72012-11-20 16:52:12 +0100152COMMAND(set, freq, "<freq> [HT20|HT40+|HT40-]\n"
153 "<control freq> [20|40|80|80+80|160] [<center freq 1>] [<center freq 2>]",
Johannes Berg01ae06f2009-05-05 14:48:16 +0200154 NL80211_CMD_SET_WIPHY, 0, CIB_NETDEV, handle_freq, NULL);
Johannes Berg379f8392008-12-08 12:53:58 +0100155
Johannes Berg7c37a242009-04-08 13:13:28 +0200156static int handle_chan(struct nl80211_state *state,
157 struct nl_cb *cb, struct nl_msg *msg,
Johannes Berg05514f92012-07-19 11:50:50 +0200158 int argc, char **argv,
159 enum id_input id)
Johannes Berg379f8392008-12-08 12:53:58 +0100160{
161 return handle_freqchan(msg, true, argc, argv);
162}
163COMMAND(set, channel, "<channel> [HT20|HT40+|HT40-]",
Johannes Berg01ae06f2009-05-05 14:48:16 +0200164 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_chan, NULL);
Johannes Berg379f8392008-12-08 12:53:58 +0100165COMMAND(set, channel, "<channel> [HT20|HT40+|HT40-]",
Johannes Berg01ae06f2009-05-05 14:48:16 +0200166 NL80211_CMD_SET_WIPHY, 0, CIB_NETDEV, handle_chan, NULL);
Johannes Berg625aa4a2009-08-11 11:26:42 +0200167
168static int handle_fragmentation(struct nl80211_state *state,
169 struct nl_cb *cb, struct nl_msg *msg,
Johannes Berg05514f92012-07-19 11:50:50 +0200170 int argc, char **argv,
171 enum id_input id)
Johannes Berg625aa4a2009-08-11 11:26:42 +0200172{
173 unsigned int frag;
174
175 if (argc != 1)
176 return 1;
177
178 if (strcmp("off", argv[0]) == 0)
179 frag = -1;
Johannes Berge86b7e02010-04-01 18:49:21 +0200180 else {
181 char *end;
182
183 if (!*argv[0])
184 return 1;
185 frag = strtoul(argv[0], &end, 10);
186 if (*end != '\0')
187 return 1;
188 }
Johannes Berg625aa4a2009-08-11 11:26:42 +0200189
190 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, frag);
191
192 return 0;
193 nla_put_failure:
194 return -ENOBUFS;
195}
196COMMAND(set, frag, "<fragmentation threshold|off>",
197 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_fragmentation,
198 "Set fragmentation threshold.");
199
200static int handle_rts(struct nl80211_state *state,
201 struct nl_cb *cb, struct nl_msg *msg,
Johannes Berg05514f92012-07-19 11:50:50 +0200202 int argc, char **argv,
203 enum id_input id)
Johannes Berg625aa4a2009-08-11 11:26:42 +0200204{
205 unsigned int rts;
206
207 if (argc != 1)
208 return 1;
209
210 if (strcmp("off", argv[0]) == 0)
211 rts = -1;
Johannes Berge86b7e02010-04-01 18:49:21 +0200212 else {
213 char *end;
214
215 if (!*argv[0])
216 return 1;
217 rts = strtoul(argv[0], &end, 10);
218 if (*end != '\0')
219 return 1;
220 }
Johannes Berg625aa4a2009-08-11 11:26:42 +0200221
222 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, rts);
223
224 return 0;
225 nla_put_failure:
226 return -ENOBUFS;
227}
228COMMAND(set, rts, "<rts threshold|off>",
229 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_rts,
230 "Set rts threshold.");
Johannes Berge960e062009-09-24 20:18:47 +0200231
232static int handle_netns(struct nl80211_state *state,
233 struct nl_cb *cb,
234 struct nl_msg *msg,
Johannes Berg05514f92012-07-19 11:50:50 +0200235 int argc, char **argv,
236 enum id_input id)
Johannes Berge960e062009-09-24 20:18:47 +0200237{
238 char *end;
239
240 if (argc != 1)
241 return 1;
242
Johannes Berge86b7e02010-04-01 18:49:21 +0200243 if (!*argv[0])
244 return 1;
245
Johannes Berge960e062009-09-24 20:18:47 +0200246 NLA_PUT_U32(msg, NL80211_ATTR_PID,
Johannes Bergc5514492011-12-07 09:08:40 +0100247 strtoul(argv[0], &end, 10));
Johannes Berge960e062009-09-24 20:18:47 +0200248
249 if (*end != '\0')
250 return 1;
251
252 return 0;
253 nla_put_failure:
254 return -ENOBUFS;
255}
256COMMAND(set, netns, "<pid>",
257 NL80211_CMD_SET_WIPHY_NETNS, 0, CIB_PHY, handle_netns,
258 "Put this wireless device into a different network namespace");
Lukáš Turekb2f92dd2010-02-17 21:13:23 -0500259
260static int handle_coverage(struct nl80211_state *state,
261 struct nl_cb *cb,
262 struct nl_msg *msg,
Johannes Berg05514f92012-07-19 11:50:50 +0200263 int argc, char **argv,
264 enum id_input id)
Lukáš Turekb2f92dd2010-02-17 21:13:23 -0500265{
Johannes Berge86b7e02010-04-01 18:49:21 +0200266 char *end;
Lukáš Turekb2f92dd2010-02-17 21:13:23 -0500267 unsigned int coverage;
268
269 if (argc != 1)
270 return 1;
271
Johannes Berge86b7e02010-04-01 18:49:21 +0200272 if (!*argv[0])
273 return 1;
274 coverage = strtoul(argv[0], &end, 10);
Lukáš Turekb2f92dd2010-02-17 21:13:23 -0500275 if (coverage > 255)
276 return 1;
277
Johannes Berge86b7e02010-04-01 18:49:21 +0200278 if (*end)
279 return 1;
280
Lukáš Turekb2f92dd2010-02-17 21:13:23 -0500281 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS, coverage);
282
283 return 0;
284 nla_put_failure:
285 return -ENOBUFS;
286}
287COMMAND(set, coverage, "<coverage class>",
288 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_coverage,
289 "Set coverage class (1 for every 3 usec of air propagation time).\n"
290 "Valid values: 0 - 255.");
291
292static int handle_distance(struct nl80211_state *state,
293 struct nl_cb *cb,
294 struct nl_msg *msg,
Johannes Berg05514f92012-07-19 11:50:50 +0200295 int argc, char **argv,
296 enum id_input id)
Lukáš Turekb2f92dd2010-02-17 21:13:23 -0500297{
Johannes Berge86b7e02010-04-01 18:49:21 +0200298 char *end;
Lukáš Turekb2f92dd2010-02-17 21:13:23 -0500299 unsigned int distance, coverage;
300
301 if (argc != 1)
302 return 1;
303
Johannes Berge86b7e02010-04-01 18:49:21 +0200304 if (!*argv[0])
305 return 1;
306
307 distance = strtoul(argv[0], &end, 10);
308
309 if (*end)
310 return 1;
Lukáš Turekb2f92dd2010-02-17 21:13:23 -0500311
312 /*
313 * Divide double the distance by the speed of light in m/usec (300) to
314 * get round-trip time in microseconds and then divide the result by
315 * three to get coverage class as specified in IEEE 802.11-2007 table
316 * 7-27. Values are rounded upwards.
317 */
318 coverage = (distance + 449) / 450;
319 if (coverage > 255)
320 return 1;
321
322 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS, coverage);
323
324 return 0;
325 nla_put_failure:
326 return -ENOBUFS;
327}
328COMMAND(set, distance, "<distance>",
329 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_distance,
330 "Set appropriate coverage class for given link distance in meters.\n"
331 "Valid values: 0 - 114750");
Juuso Oikarinena0b1f572010-06-23 12:12:57 +0300332
333static int handle_txpower(struct nl80211_state *state,
334 struct nl_cb *cb,
335 struct nl_msg *msg,
Johannes Berg05514f92012-07-19 11:50:50 +0200336 int argc, char **argv,
337 enum id_input id)
Juuso Oikarinena0b1f572010-06-23 12:12:57 +0300338{
339 enum nl80211_tx_power_setting type;
340 int mbm;
341
342 /* get the required args */
343 if (argc != 1 && argc != 2)
344 return 1;
345
346 if (!strcmp(argv[0], "auto"))
347 type = NL80211_TX_POWER_AUTOMATIC;
348 else if (!strcmp(argv[0], "fixed"))
349 type = NL80211_TX_POWER_FIXED;
350 else if (!strcmp(argv[0], "limit"))
351 type = NL80211_TX_POWER_LIMITED;
352 else {
353 printf("Invalid parameter: %s\n", argv[0]);
354 return 2;
355 }
356
357 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_TX_POWER_SETTING, type);
358
359 if (type != NL80211_TX_POWER_AUTOMATIC) {
Johannes Berg18e05612011-04-28 19:41:04 +0200360 char *endptr;
Juuso Oikarinena0b1f572010-06-23 12:12:57 +0300361 if (argc != 2) {
362 printf("Missing TX power level argument.\n");
363 return 2;
364 }
365
Johannes Berg18e05612011-04-28 19:41:04 +0200366 mbm = strtol(argv[1], &endptr, 10);
Felix Fietkau08ec4c62011-11-07 18:49:51 +0100367 if (*endptr)
Johannes Berg18e05612011-04-28 19:41:04 +0200368 return 2;
Juuso Oikarinena0b1f572010-06-23 12:12:57 +0300369 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL, mbm);
370 } else if (argc != 1)
371 return 1;
372
373 return 0;
374
375 nla_put_failure:
376 return -ENOBUFS;
377}
378COMMAND(set, txpower, "<auto|fixed|limit> [<tx power in mBm>]",
379 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_txpower,
380 "Specify transmit power level and setting type.");
381COMMAND(set, txpower, "<auto|fixed|limit> [<tx power in mBm>]",
382 NL80211_CMD_SET_WIPHY, 0, CIB_NETDEV, handle_txpower,
383 "Specify transmit power level and setting type.");
Bruno Randolfafce7982010-12-22 10:54:39 +0900384
385static int handle_antenna(struct nl80211_state *state,
386 struct nl_cb *cb,
387 struct nl_msg *msg,
Johannes Berg05514f92012-07-19 11:50:50 +0200388 int argc, char **argv,
389 enum id_input id)
Bruno Randolfafce7982010-12-22 10:54:39 +0900390{
391 char *end;
392 uint32_t tx_ant = 0, rx_ant = 0;
393
394 if (argc == 1 && strcmp(argv[0], "all") == 0) {
395 tx_ant = 0xffffffff;
396 rx_ant = 0xffffffff;
397 } else if (argc == 1) {
398 tx_ant = rx_ant = strtoul(argv[0], &end, 0);
399 if (*end)
400 return 1;
401 }
402 else if (argc == 2) {
403 tx_ant = strtoul(argv[0], &end, 0);
404 if (*end)
405 return 1;
406 rx_ant = strtoul(argv[1], &end, 0);
407 if (*end)
408 return 1;
409 } else
410 return 1;
411
412 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
413 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
414
415 return 0;
416
417 nla_put_failure:
418 return -ENOBUFS;
419}
420COMMAND(set, antenna, "<bitmap> | all | <tx bitmap> <rx bitmap>",
421 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_antenna,
422 "Set a bitmap of allowed antennas to use for TX and RX.\n"
423 "The driver may reject antenna configurations it cannot support.");