]> git.ipfire.org Git - thirdparty/iw.git/blame - interface.c
iw: add set_mcast_rate command support
[thirdparty/iw.git] / interface.c
CommitLineData
2ef1be68 1#include <net/if.h>
45c7212c 2#include <errno.h>
d5ac8ad3 3#include <string.h>
d17fe27e 4#include <stdbool.h>
2ef1be68 5
45c7212c
JB
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>
45c7212c 11
f408e01b 12#include "nl80211.h"
45c7212c
JB
13#include "iw.h"
14
1ae3d621
JB
15#define VALID_FLAGS "none: no special flags\n"\
16 "fcsfail: show frames with FCS errors\n"\
17 "control: show control frames\n"\
18 "otherbss: show frames from other BSSes\n"\
19 "cook: use cooked mode"
20
4698bfc2
JB
21SECTION(interface);
22
cd293761 23static char *mntr_flags[NL80211_MNTR_FLAG_MAX + 1] = {
dd65496b 24 "none",
cd293761
JB
25 "fcsfail",
26 "plcpfail",
27 "control",
28 "otherbss",
29 "cook",
30};
31
dd65496b
JB
32static int parse_mntr_flags(int *_argc, char ***_argv,
33 struct nl_msg *msg)
34{
35 struct nl_msg *flags;
36 int err = -ENOBUFS;
37 enum nl80211_mntr_flags flag;
38 int argc = *_argc;
39 char **argv = *_argv;
40
41 flags = nlmsg_alloc();
42 if (!flags)
43 return -ENOMEM;
44
45 while (argc) {
46 int ok = 0;
47 for (flag = __NL80211_MNTR_FLAG_INVALID;
e14cc99e 48 flag <= NL80211_MNTR_FLAG_MAX; flag++) {
dd65496b
JB
49 if (strcmp(*argv, mntr_flags[flag]) == 0) {
50 ok = 1;
4a3bf753
JB
51 /*
52 * This shouldn't be adding "flag" if that is
53 * zero, but due to a problem in the kernel's
54 * nl80211 code (using NLA_NESTED policy) it
55 * will reject an empty nested attribute but
56 * not one that contains an invalid attribute
57 */
dd65496b
JB
58 NLA_PUT_FLAG(flags, flag);
59 break;
60 }
61 }
62 if (!ok) {
63 err = -EINVAL;
64 goto out;
65 }
66 argc--;
67 argv++;
68 }
69
70 nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
71 err = 0;
72 nla_put_failure:
73 out:
74 nlmsg_free(flags);
75
76 *_argc = argc;
77 *_argv = argv;
78
79 return err;
80}
81
1ae3d621
JB
82/* for help */
83#define IFACE_TYPES "Valid interface types are: managed, ibss, monitor, mesh, wds."
84
d17fe27e
JB
85/* return 0 if ok, internal error otherwise */
86static int get_if_type(int *argc, char ***argv, enum nl80211_iftype *type,
87 bool need_type)
45c7212c
JB
88{
89 char *tpstr;
90
d17fe27e
JB
91 if (*argc < 1 + !!need_type)
92 return 1;
45c7212c 93
d17fe27e
JB
94 if (need_type && strcmp((*argv)[0], "type"))
95 return 1;
45c7212c 96
d17fe27e
JB
97 tpstr = (*argv)[!!need_type];
98 *argc -= 1 + !!need_type;
99 *argv += 1 + !!need_type;
45c7212c
JB
100
101 if (strcmp(tpstr, "adhoc") == 0 ||
102 strcmp(tpstr, "ibss") == 0) {
103 *type = NL80211_IFTYPE_ADHOC;
d17fe27e 104 return 0;
45c7212c
JB
105 } else if (strcmp(tpstr, "monitor") == 0) {
106 *type = NL80211_IFTYPE_MONITOR;
d17fe27e 107 return 0;
e08b5488
JB
108 } else if (strcmp(tpstr, "master") == 0 ||
109 strcmp(tpstr, "ap") == 0) {
c1d44a6c 110 *type = NL80211_IFTYPE_UNSPECIFIED;
e08b5488
JB
111 fprintf(stderr, "You need to run a management daemon, e.g. hostapd,\n");
112 fprintf(stderr, "see http://wireless.kernel.org/en/users/Documentation/hostapd\n");
113 fprintf(stderr, "for more information on how to do that.\n");
c1d44a6c 114 return 2;
4d3a72da 115 } else if (strcmp(tpstr, "__ap") == 0) {
45c7212c 116 *type = NL80211_IFTYPE_AP;
d17fe27e 117 return 0;
4d3a72da 118 } else if (strcmp(tpstr, "__ap_vlan") == 0) {
45c7212c 119 *type = NL80211_IFTYPE_AP_VLAN;
d17fe27e 120 return 0;
45c7212c
JB
121 } else if (strcmp(tpstr, "wds") == 0) {
122 *type = NL80211_IFTYPE_WDS;
d17fe27e 123 return 0;
a65df165
JB
124 } else if (strcmp(tpstr, "managed") == 0 ||
125 strcmp(tpstr, "mgd") == 0 ||
126 strcmp(tpstr, "station") == 0) {
45c7212c 127 *type = NL80211_IFTYPE_STATION;
d17fe27e 128 return 0;
3d1e8704 129 } else if (strcmp(tpstr, "mp") == 0 ||
a65df165 130 strcmp(tpstr, "mesh") == 0) {
3d1e8704 131 *type = NL80211_IFTYPE_MESH_POINT;
d17fe27e 132 return 0;
a4464243
JB
133 } else if (strcmp(tpstr, "__p2pcl") == 0) {
134 *type = NL80211_IFTYPE_P2P_CLIENT;
135 return 0;
8d5d7ba7
JB
136 } else if (strcmp(tpstr, "__p2pdev") == 0) {
137 *type = NL80211_IFTYPE_P2P_DEVICE;
138 return 0;
a4464243
JB
139 } else if (strcmp(tpstr, "__p2pgo") == 0) {
140 *type = NL80211_IFTYPE_P2P_GO;
141 return 0;
45c7212c
JB
142 }
143
45c7212c 144 fprintf(stderr, "invalid interface type %s\n", tpstr);
d17fe27e 145 return 2;
45c7212c
JB
146}
147
39566cca
FF
148static int parse_4addr_flag(const char *value, struct nl_msg *msg)
149{
150 if (strcmp(value, "on") == 0)
151 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, 1);
152 else if (strcmp(value, "off") == 0)
153 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, 0);
154 else
155 return 1;
156 return 0;
157
158nla_put_failure:
159 return 1;
160}
161
7c37a24d
JB
162static int handle_interface_add(struct nl80211_state *state,
163 struct nl_cb *cb,
bd396f2a 164 struct nl_msg *msg,
05514f95
JB
165 int argc, char **argv,
166 enum id_input id)
45c7212c 167{
2dfd6bfa 168 char *name;
3d1e8704 169 char *mesh_id = NULL;
45c7212c 170 enum nl80211_iftype type;
70391ccf 171 int tpset;
45c7212c 172
bd396f2a 173 if (argc < 1)
5e75fd04 174 return 1;
45c7212c 175
2dfd6bfa 176 name = argv[0];
45c7212c
JB
177 argc--;
178 argv++;
179
d17fe27e
JB
180 tpset = get_if_type(&argc, &argv, &type, true);
181 if (tpset)
c1d44a6c 182 return tpset;
45c7212c 183
3d1e8704 184 if (argc) {
dd65496b
JB
185 if (strcmp(argv[0], "mesh_id") == 0) {
186 argc--;
187 argv++;
188
189 if (!argc)
190 return 1;
191 mesh_id = argv[0];
192 argc--;
193 argv++;
39566cca
FF
194 } else if (strcmp(argv[0], "4addr") == 0) {
195 argc--;
196 argv++;
197 if (parse_4addr_flag(argv[0], msg)) {
198 fprintf(stderr, "4addr error\n");
199 return 2;
200 }
201 argc--;
202 argv++;
dd65496b
JB
203 } else if (strcmp(argv[0], "flags") == 0) {
204 argc--;
205 argv++;
206 if (parse_mntr_flags(&argc, &argv, msg)) {
207 fprintf(stderr, "flags error\n");
208 return 2;
209 }
210 } else {
5e75fd04 211 return 1;
dd65496b 212 }
3d1e8704
LCC
213 }
214
5e75fd04
JB
215 if (argc)
216 return 1;
45c7212c 217
45c7212c 218 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name);
6d28ce25 219 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
3d1e8704
LCC
220 if (mesh_id)
221 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
45c7212c 222
70391ccf 223 return 0;
5e75fd04 224 nla_put_failure:
70391ccf 225 return -ENOBUFS;
45c7212c 226}
39566cca 227COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*]",
1ae3d621
JB
228 NL80211_CMD_NEW_INTERFACE, 0, CIB_PHY, handle_interface_add,
229 "Add a new virtual interface with the given configuration.\n"
230 IFACE_TYPES "\n\n"
231 "The flags are only used for monitor interfaces, valid flags are:\n"
232 VALID_FLAGS "\n\n"
233 "The mesh_id is used only for mesh mode.");
39566cca 234COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*]",
01ae06f9 235 NL80211_CMD_NEW_INTERFACE, 0, CIB_NETDEV, handle_interface_add, NULL);
45c7212c 236
7c37a24d
JB
237static int handle_interface_del(struct nl80211_state *state,
238 struct nl_cb *cb,
bd396f2a 239 struct nl_msg *msg,
05514f95
JB
240 int argc, char **argv,
241 enum id_input id)
3fcfe40e 242{
70391ccf 243 return 0;
3fcfe40e 244}
1ae3d621
JB
245TOPLEVEL(del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del,
246 "Remove this virtual interface");
ce5af55c 247HIDDEN(interface, del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del);
3fcfe40e 248
3adcc1f4
PF
249static char *channel_type_name(enum nl80211_channel_type channel_type)
250{
251 switch (channel_type) {
252 case NL80211_CHAN_NO_HT:
253 return "NO HT";
254 case NL80211_CHAN_HT20:
255 return "HT20";
256 case NL80211_CHAN_HT40MINUS:
257 return "HT40-";
258 case NL80211_CHAN_HT40PLUS:
259 return "HT40+";
260 default:
261 return "unknown";
262 }
263}
264
541ef425
JB
265static int print_iface_handler(struct nl_msg *msg, void *arg)
266{
267 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
268 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
674567b8
JB
269 unsigned int *wiphy = arg;
270 const char *indent = "";
541ef425
JB
271
272 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
273 genlmsg_attrlen(gnlh, 0), NULL);
274
674567b8
JB
275 if (wiphy && tb_msg[NL80211_ATTR_WIPHY]) {
276 unsigned int thiswiphy = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]);
277 indent = "\t";
278 if (*wiphy != thiswiphy)
279 printf("phy#%d\n", thiswiphy);
280 *wiphy = thiswiphy;
281 }
282
541ef425 283 if (tb_msg[NL80211_ATTR_IFNAME])
674567b8 284 printf("%sInterface %s\n", indent, nla_get_string(tb_msg[NL80211_ATTR_IFNAME]));
3508c5c9
JB
285 else
286 printf("%sUnnamed/non-netdev interface\n", indent);
541ef425 287 if (tb_msg[NL80211_ATTR_IFINDEX])
674567b8 288 printf("%s\tifindex %d\n", indent, nla_get_u32(tb_msg[NL80211_ATTR_IFINDEX]));
3508c5c9
JB
289 if (tb_msg[NL80211_ATTR_WDEV])
290 printf("%s\twdev 0x%llx\n", indent,
291 (unsigned long long)nla_get_u64(tb_msg[NL80211_ATTR_WDEV]));
292 if (tb_msg[NL80211_ATTR_MAC]) {
293 char mac_addr[20];
294 mac_addr_n2a(mac_addr, nla_data(tb_msg[NL80211_ATTR_MAC]));
295 printf("%s\taddr %s\n", indent, mac_addr);
296 }
01009c3d
AQ
297 if (tb_msg[NL80211_ATTR_SSID]) {
298 printf("%s\tssid ", indent);
299 print_ssid_escaped(nla_len(tb_msg[NL80211_ATTR_SSID]),
300 nla_data(tb_msg[NL80211_ATTR_SSID]));
301 printf("\n");
302 }
541ef425 303 if (tb_msg[NL80211_ATTR_IFTYPE])
674567b8 304 printf("%s\ttype %s\n", indent, iftype_name(nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE])));
47b76de4 305 if (!wiphy && tb_msg[NL80211_ATTR_WIPHY])
5b050af4 306 printf("%s\twiphy %d\n", indent, nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]));
3adcc1f4
PF
307 if (tb_msg[NL80211_ATTR_WIPHY_FREQ]) {
308 uint32_t freq = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY_FREQ]);
309 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
310
311 if (tb_msg[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
312 channel_type = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
313
314 printf("%s\tchannel %d (%d MHz) %s\n", indent,
315 ieee80211_frequency_to_channel(freq), freq,
316 channel_type_name(channel_type));
317 }
541ef425
JB
318
319 return NL_SKIP;
320}
321
7c37a24d
JB
322static int handle_interface_info(struct nl80211_state *state,
323 struct nl_cb *cb,
bd396f2a 324 struct nl_msg *msg,
05514f95
JB
325 int argc, char **argv,
326 enum id_input id)
541ef425 327{
541ef425 328 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, NULL);
70391ccf 329 return 0;
541ef425 330}
1ae3d621
JB
331TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info,
332 "Show information for this interface.");
cd293761 333
7c37a24d
JB
334static int handle_interface_set(struct nl80211_state *state,
335 struct nl_cb *cb,
cd293761 336 struct nl_msg *msg,
05514f95
JB
337 int argc, char **argv,
338 enum id_input id)
cd293761 339{
cd293761
JB
340 if (!argc)
341 return 1;
342
cd293761
JB
343 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
344
dd65496b
JB
345 switch (parse_mntr_flags(&argc, &argv, msg)) {
346 case 0:
347 return 0;
348 case -ENOMEM:
349 fprintf(stderr, "failed to allocate flags\n");
350 return 2;
351 case -EINVAL:
352 fprintf(stderr, "unknown flag %s\n", *argv);
353 return 2;
354 default:
355 return 2;
cd293761 356 }
cd293761 357 nla_put_failure:
dd65496b 358 return -ENOBUFS;
cd293761 359}
1ae3d621
JB
360COMMAND(set, monitor, "<flag>*",
361 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_set,
362 "Set monitor flags. Valid flags are:\n"
363 VALID_FLAGS);
82d028d0 364
7c37a24d
JB
365static int handle_interface_meshid(struct nl80211_state *state,
366 struct nl_cb *cb,
82d028d0 367 struct nl_msg *msg,
05514f95
JB
368 int argc, char **argv,
369 enum id_input id)
82d028d0
JB
370{
371 char *mesh_id = NULL;
372
373 if (argc != 1)
374 return 1;
375
376 mesh_id = argv[0];
377
378 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
379
380 return 0;
381 nla_put_failure:
382 return -ENOBUFS;
383}
384COMMAND(set, meshid, "<meshid>",
01ae06f9 385 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_meshid, NULL);
674567b8
JB
386
387static unsigned int dev_dump_wiphy;
388
389static int handle_dev_dump(struct nl80211_state *state,
390 struct nl_cb *cb,
391 struct nl_msg *msg,
05514f95
JB
392 int argc, char **argv,
393 enum id_input id)
674567b8
JB
394{
395 dev_dump_wiphy = -1;
396 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, &dev_dump_wiphy);
397 return 0;
398}
1ae3d621
JB
399TOPLEVEL(dev, NULL, NL80211_CMD_GET_INTERFACE, NLM_F_DUMP, CIB_NONE, handle_dev_dump,
400 "List all network interfaces for wireless hardware.");
d17fe27e
JB
401
402static int handle_interface_type(struct nl80211_state *state,
403 struct nl_cb *cb,
404 struct nl_msg *msg,
05514f95
JB
405 int argc, char **argv,
406 enum id_input id)
d17fe27e
JB
407{
408 enum nl80211_iftype type;
409 int tpset;
410
411 tpset = get_if_type(&argc, &argv, &type, false);
412 if (tpset)
413 return tpset;
414
415 if (argc)
416 return 1;
417
418 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
419
420 return 0;
421 nla_put_failure:
422 return -ENOBUFS;
423}
424COMMAND(set, type, "<type>",
1ae3d621
JB
425 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_type,
426 "Set interface type/mode.\n"
427 IFACE_TYPES);
20e4ff8a
JF
428
429static int handle_interface_4addr(struct nl80211_state *state,
05514f95
JB
430 struct nl_cb *cb,
431 struct nl_msg *msg,
432 int argc, char **argv,
433 enum id_input id)
20e4ff8a
JF
434{
435 if (argc != 1)
436 return 1;
437 return parse_4addr_flag(argv[0], msg);
438}
439COMMAND(set, 4addr, "<on|off>",
440 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_4addr,
5548dfd7 441 "Set interface 4addr (WDS) mode.");
88c06599 442
83a7bd0d
SW
443static int handle_interface_noack_map(struct nl80211_state *state,
444 struct nl_cb *cb,
445 struct nl_msg *msg,
05514f95
JB
446 int argc, char **argv,
447 enum id_input id)
83a7bd0d
SW
448{
449 uint16_t noack_map;
450 char *end;
451
452 if (argc != 1)
453 return 1;
454
455 noack_map = strtoul(argv[0], &end, 16);
456 if (*end)
457 return 1;
458
459 NLA_PUT_U16(msg, NL80211_ATTR_NOACK_MAP, noack_map);
460
461 return 0;
462 nla_put_failure:
463 return -ENOBUFS;
464
465}
466COMMAND(set, noack_map, "<map>",
467 NL80211_CMD_SET_NOACK_MAP, 0, CIB_NETDEV, handle_interface_noack_map,
468 "Set the NoAck map for the TIDs. (0x0009 = BE, 0x0006 = BK, 0x0030 = VI, 0x00C0 = VO)");
469
470
88c06599
BJ
471static int handle_interface_wds_peer(struct nl80211_state *state,
472 struct nl_cb *cb,
473 struct nl_msg *msg,
05514f95
JB
474 int argc, char **argv,
475 enum id_input id)
88c06599
BJ
476{
477 unsigned char mac_addr[ETH_ALEN];
478
479 if (argc < 1)
480 return 1;
481
482 if (mac_addr_a2n(mac_addr, argv[0])) {
5548dfd7 483 fprintf(stderr, "Invalid MAC address\n");
88c06599
BJ
484 return 2;
485 }
486
487 argc--;
488 argv++;
489
490 if (argc)
491 return 1;
492
493 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
494
495 return 0;
496 nla_put_failure:
497 return -ENOBUFS;
498}
499COMMAND(set, peer, "<MAC address>",
500 NL80211_CMD_SET_WDS_PEER, 0, CIB_NETDEV, handle_interface_wds_peer,
5548dfd7 501 "Set interface WDS peer.");
ffaac0f7
AQ
502
503static int set_mcast_rate(struct nl80211_state *state,
504 struct nl_cb *cb,
505 struct nl_msg *msg,
506 int argc, char **argv,
507 enum id_input id)
508{
509 float rate;
510 char *end;
511
512 if (argc != 1) {
513 printf("Invalid parameters!\n");
514 return 2;
515 }
516
517 rate = strtod(argv[0], &end);
518 if (*end != '\0')
519 return 1;
520
521 NLA_PUT_U32(msg, NL80211_ATTR_MCAST_RATE, (int)(rate * 10));
522
523 return 0;
524nla_put_failure:
525 return -ENOBUFS;
526}
527
528COMMAND(set, mcast_rate, "<rate in Mbps>",
529 NL80211_CMD_SET_MCAST_RATE, 0, CIB_NETDEV, set_mcast_rate,
530 "Set the multicast bitrate.");