]> git.ipfire.org Git - thirdparty/iw.git/blame - interface.c
print wdev id in event info
[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;
136 } else if (strcmp(tpstr, "__p2pgo") == 0) {
137 *type = NL80211_IFTYPE_P2P_GO;
138 return 0;
45c7212c
JB
139 }
140
45c7212c 141 fprintf(stderr, "invalid interface type %s\n", tpstr);
d17fe27e 142 return 2;
45c7212c
JB
143}
144
39566cca
FF
145static int parse_4addr_flag(const char *value, struct nl_msg *msg)
146{
147 if (strcmp(value, "on") == 0)
148 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, 1);
149 else if (strcmp(value, "off") == 0)
150 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, 0);
151 else
152 return 1;
153 return 0;
154
155nla_put_failure:
156 return 1;
157}
158
7c37a24d
JB
159static int handle_interface_add(struct nl80211_state *state,
160 struct nl_cb *cb,
bd396f2a 161 struct nl_msg *msg,
05514f95
JB
162 int argc, char **argv,
163 enum id_input id)
45c7212c 164{
2dfd6bfa 165 char *name;
3d1e8704 166 char *mesh_id = NULL;
45c7212c 167 enum nl80211_iftype type;
70391ccf 168 int tpset;
45c7212c 169
bd396f2a 170 if (argc < 1)
5e75fd04 171 return 1;
45c7212c 172
2dfd6bfa 173 name = argv[0];
45c7212c
JB
174 argc--;
175 argv++;
176
d17fe27e
JB
177 tpset = get_if_type(&argc, &argv, &type, true);
178 if (tpset)
c1d44a6c 179 return tpset;
45c7212c 180
3d1e8704 181 if (argc) {
dd65496b
JB
182 if (strcmp(argv[0], "mesh_id") == 0) {
183 argc--;
184 argv++;
185
186 if (!argc)
187 return 1;
188 mesh_id = argv[0];
189 argc--;
190 argv++;
39566cca
FF
191 } else if (strcmp(argv[0], "4addr") == 0) {
192 argc--;
193 argv++;
194 if (parse_4addr_flag(argv[0], msg)) {
195 fprintf(stderr, "4addr error\n");
196 return 2;
197 }
198 argc--;
199 argv++;
dd65496b
JB
200 } else if (strcmp(argv[0], "flags") == 0) {
201 argc--;
202 argv++;
203 if (parse_mntr_flags(&argc, &argv, msg)) {
204 fprintf(stderr, "flags error\n");
205 return 2;
206 }
207 } else {
5e75fd04 208 return 1;
dd65496b 209 }
3d1e8704
LCC
210 }
211
5e75fd04
JB
212 if (argc)
213 return 1;
45c7212c 214
45c7212c 215 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name);
6d28ce25 216 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
3d1e8704
LCC
217 if (mesh_id)
218 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
45c7212c 219
70391ccf 220 return 0;
5e75fd04 221 nla_put_failure:
70391ccf 222 return -ENOBUFS;
45c7212c 223}
39566cca 224COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*]",
1ae3d621
JB
225 NL80211_CMD_NEW_INTERFACE, 0, CIB_PHY, handle_interface_add,
226 "Add a new virtual interface with the given configuration.\n"
227 IFACE_TYPES "\n\n"
228 "The flags are only used for monitor interfaces, valid flags are:\n"
229 VALID_FLAGS "\n\n"
230 "The mesh_id is used only for mesh mode.");
39566cca 231COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*]",
01ae06f9 232 NL80211_CMD_NEW_INTERFACE, 0, CIB_NETDEV, handle_interface_add, NULL);
45c7212c 233
7c37a24d
JB
234static int handle_interface_del(struct nl80211_state *state,
235 struct nl_cb *cb,
bd396f2a 236 struct nl_msg *msg,
05514f95
JB
237 int argc, char **argv,
238 enum id_input id)
3fcfe40e 239{
70391ccf 240 return 0;
3fcfe40e 241}
1ae3d621
JB
242TOPLEVEL(del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del,
243 "Remove this virtual interface");
ce5af55c 244HIDDEN(interface, del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del);
3fcfe40e 245
3adcc1f4
PF
246static char *channel_type_name(enum nl80211_channel_type channel_type)
247{
248 switch (channel_type) {
249 case NL80211_CHAN_NO_HT:
250 return "NO HT";
251 case NL80211_CHAN_HT20:
252 return "HT20";
253 case NL80211_CHAN_HT40MINUS:
254 return "HT40-";
255 case NL80211_CHAN_HT40PLUS:
256 return "HT40+";
257 default:
258 return "unknown";
259 }
260}
261
541ef425
JB
262static int print_iface_handler(struct nl_msg *msg, void *arg)
263{
264 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
265 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
674567b8
JB
266 unsigned int *wiphy = arg;
267 const char *indent = "";
541ef425
JB
268
269 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
270 genlmsg_attrlen(gnlh, 0), NULL);
271
674567b8
JB
272 if (wiphy && tb_msg[NL80211_ATTR_WIPHY]) {
273 unsigned int thiswiphy = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]);
274 indent = "\t";
275 if (*wiphy != thiswiphy)
276 printf("phy#%d\n", thiswiphy);
277 *wiphy = thiswiphy;
278 }
279
541ef425 280 if (tb_msg[NL80211_ATTR_IFNAME])
674567b8 281 printf("%sInterface %s\n", indent, nla_get_string(tb_msg[NL80211_ATTR_IFNAME]));
3508c5c9
JB
282 else
283 printf("%sUnnamed/non-netdev interface\n", indent);
541ef425 284 if (tb_msg[NL80211_ATTR_IFINDEX])
674567b8 285 printf("%s\tifindex %d\n", indent, nla_get_u32(tb_msg[NL80211_ATTR_IFINDEX]));
3508c5c9
JB
286 if (tb_msg[NL80211_ATTR_WDEV])
287 printf("%s\twdev 0x%llx\n", indent,
288 (unsigned long long)nla_get_u64(tb_msg[NL80211_ATTR_WDEV]));
289 if (tb_msg[NL80211_ATTR_MAC]) {
290 char mac_addr[20];
291 mac_addr_n2a(mac_addr, nla_data(tb_msg[NL80211_ATTR_MAC]));
292 printf("%s\taddr %s\n", indent, mac_addr);
293 }
541ef425 294 if (tb_msg[NL80211_ATTR_IFTYPE])
674567b8 295 printf("%s\ttype %s\n", indent, iftype_name(nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE])));
47b76de4 296 if (!wiphy && tb_msg[NL80211_ATTR_WIPHY])
5b050af4 297 printf("%s\twiphy %d\n", indent, nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]));
3adcc1f4
PF
298 if (tb_msg[NL80211_ATTR_WIPHY_FREQ]) {
299 uint32_t freq = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY_FREQ]);
300 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
301
302 if (tb_msg[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
303 channel_type = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
304
305 printf("%s\tchannel %d (%d MHz) %s\n", indent,
306 ieee80211_frequency_to_channel(freq), freq,
307 channel_type_name(channel_type));
308 }
541ef425
JB
309
310 return NL_SKIP;
311}
312
7c37a24d
JB
313static int handle_interface_info(struct nl80211_state *state,
314 struct nl_cb *cb,
bd396f2a 315 struct nl_msg *msg,
05514f95
JB
316 int argc, char **argv,
317 enum id_input id)
541ef425 318{
541ef425 319 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, NULL);
70391ccf 320 return 0;
541ef425 321}
1ae3d621
JB
322TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info,
323 "Show information for this interface.");
cd293761 324
7c37a24d
JB
325static int handle_interface_set(struct nl80211_state *state,
326 struct nl_cb *cb,
cd293761 327 struct nl_msg *msg,
05514f95
JB
328 int argc, char **argv,
329 enum id_input id)
cd293761 330{
cd293761
JB
331 if (!argc)
332 return 1;
333
cd293761
JB
334 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
335
dd65496b
JB
336 switch (parse_mntr_flags(&argc, &argv, msg)) {
337 case 0:
338 return 0;
339 case -ENOMEM:
340 fprintf(stderr, "failed to allocate flags\n");
341 return 2;
342 case -EINVAL:
343 fprintf(stderr, "unknown flag %s\n", *argv);
344 return 2;
345 default:
346 return 2;
cd293761 347 }
cd293761 348 nla_put_failure:
dd65496b 349 return -ENOBUFS;
cd293761 350}
1ae3d621
JB
351COMMAND(set, monitor, "<flag>*",
352 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_set,
353 "Set monitor flags. Valid flags are:\n"
354 VALID_FLAGS);
82d028d0 355
7c37a24d
JB
356static int handle_interface_meshid(struct nl80211_state *state,
357 struct nl_cb *cb,
82d028d0 358 struct nl_msg *msg,
05514f95
JB
359 int argc, char **argv,
360 enum id_input id)
82d028d0
JB
361{
362 char *mesh_id = NULL;
363
364 if (argc != 1)
365 return 1;
366
367 mesh_id = argv[0];
368
369 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
370
371 return 0;
372 nla_put_failure:
373 return -ENOBUFS;
374}
375COMMAND(set, meshid, "<meshid>",
01ae06f9 376 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_meshid, NULL);
674567b8
JB
377
378static unsigned int dev_dump_wiphy;
379
380static int handle_dev_dump(struct nl80211_state *state,
381 struct nl_cb *cb,
382 struct nl_msg *msg,
05514f95
JB
383 int argc, char **argv,
384 enum id_input id)
674567b8
JB
385{
386 dev_dump_wiphy = -1;
387 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, &dev_dump_wiphy);
388 return 0;
389}
1ae3d621
JB
390TOPLEVEL(dev, NULL, NL80211_CMD_GET_INTERFACE, NLM_F_DUMP, CIB_NONE, handle_dev_dump,
391 "List all network interfaces for wireless hardware.");
d17fe27e
JB
392
393static int handle_interface_type(struct nl80211_state *state,
394 struct nl_cb *cb,
395 struct nl_msg *msg,
05514f95
JB
396 int argc, char **argv,
397 enum id_input id)
d17fe27e
JB
398{
399 enum nl80211_iftype type;
400 int tpset;
401
402 tpset = get_if_type(&argc, &argv, &type, false);
403 if (tpset)
404 return tpset;
405
406 if (argc)
407 return 1;
408
409 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
410
411 return 0;
412 nla_put_failure:
413 return -ENOBUFS;
414}
415COMMAND(set, type, "<type>",
1ae3d621
JB
416 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_type,
417 "Set interface type/mode.\n"
418 IFACE_TYPES);
20e4ff8a
JF
419
420static int handle_interface_4addr(struct nl80211_state *state,
05514f95
JB
421 struct nl_cb *cb,
422 struct nl_msg *msg,
423 int argc, char **argv,
424 enum id_input id)
20e4ff8a
JF
425{
426 if (argc != 1)
427 return 1;
428 return parse_4addr_flag(argv[0], msg);
429}
430COMMAND(set, 4addr, "<on|off>",
431 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_4addr,
5548dfd7 432 "Set interface 4addr (WDS) mode.");
88c06599 433
83a7bd0d
SW
434static int handle_interface_noack_map(struct nl80211_state *state,
435 struct nl_cb *cb,
436 struct nl_msg *msg,
05514f95
JB
437 int argc, char **argv,
438 enum id_input id)
83a7bd0d
SW
439{
440 uint16_t noack_map;
441 char *end;
442
443 if (argc != 1)
444 return 1;
445
446 noack_map = strtoul(argv[0], &end, 16);
447 if (*end)
448 return 1;
449
450 NLA_PUT_U16(msg, NL80211_ATTR_NOACK_MAP, noack_map);
451
452 return 0;
453 nla_put_failure:
454 return -ENOBUFS;
455
456}
457COMMAND(set, noack_map, "<map>",
458 NL80211_CMD_SET_NOACK_MAP, 0, CIB_NETDEV, handle_interface_noack_map,
459 "Set the NoAck map for the TIDs. (0x0009 = BE, 0x0006 = BK, 0x0030 = VI, 0x00C0 = VO)");
460
461
88c06599
BJ
462static int handle_interface_wds_peer(struct nl80211_state *state,
463 struct nl_cb *cb,
464 struct nl_msg *msg,
05514f95
JB
465 int argc, char **argv,
466 enum id_input id)
88c06599
BJ
467{
468 unsigned char mac_addr[ETH_ALEN];
469
470 if (argc < 1)
471 return 1;
472
473 if (mac_addr_a2n(mac_addr, argv[0])) {
5548dfd7 474 fprintf(stderr, "Invalid MAC address\n");
88c06599
BJ
475 return 2;
476 }
477
478 argc--;
479 argv++;
480
481 if (argc)
482 return 1;
483
484 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
485
486 return 0;
487 nla_put_failure:
488 return -ENOBUFS;
489}
490COMMAND(set, peer, "<MAC address>",
491 NL80211_CMD_SET_WDS_PEER, 0, CIB_NETDEV, handle_interface_wds_peer,
5548dfd7 492 "Set interface WDS peer.");