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