]> git.ipfire.org Git - thirdparty/iw.git/blob - interface.c
update nl80211.h
[thirdparty/iw.git] / interface.c
1 #include <net/if.h>
2 #include <errno.h>
3 #include <string.h>
4 #include <stdbool.h>
5
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
12 #include "nl80211.h"
13 #include "iw.h"
14
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
21 SECTION(interface);
22
23 static char *mntr_flags[NL80211_MNTR_FLAG_MAX + 1] = {
24 "none",
25 "fcsfail",
26 "plcpfail",
27 "control",
28 "otherbss",
29 "cook",
30 };
31
32 static 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;
48 flag <= NL80211_MNTR_FLAG_MAX; flag++) {
49 if (strcmp(*argv, mntr_flags[flag]) == 0) {
50 ok = 1;
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 */
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
82 /* for help */
83 #define IFACE_TYPES "Valid interface types are: managed, ibss, monitor, mesh, wds."
84
85 /* return 0 if ok, internal error otherwise */
86 static int get_if_type(int *argc, char ***argv, enum nl80211_iftype *type,
87 bool need_type)
88 {
89 char *tpstr;
90
91 if (*argc < 1 + !!need_type)
92 return 1;
93
94 if (need_type && strcmp((*argv)[0], "type"))
95 return 1;
96
97 tpstr = (*argv)[!!need_type];
98 *argc -= 1 + !!need_type;
99 *argv += 1 + !!need_type;
100
101 if (strcmp(tpstr, "adhoc") == 0 ||
102 strcmp(tpstr, "ibss") == 0) {
103 *type = NL80211_IFTYPE_ADHOC;
104 return 0;
105 } else if (strcmp(tpstr, "monitor") == 0) {
106 *type = NL80211_IFTYPE_MONITOR;
107 return 0;
108 } else if (strcmp(tpstr, "master") == 0 ||
109 strcmp(tpstr, "ap") == 0) {
110 *type = NL80211_IFTYPE_UNSPECIFIED;
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");
114 return 2;
115 } else if (strcmp(tpstr, "__ap") == 0) {
116 *type = NL80211_IFTYPE_AP;
117 return 0;
118 } else if (strcmp(tpstr, "__ap_vlan") == 0) {
119 *type = NL80211_IFTYPE_AP_VLAN;
120 return 0;
121 } else if (strcmp(tpstr, "wds") == 0) {
122 *type = NL80211_IFTYPE_WDS;
123 return 0;
124 } else if (strcmp(tpstr, "managed") == 0 ||
125 strcmp(tpstr, "mgd") == 0 ||
126 strcmp(tpstr, "station") == 0) {
127 *type = NL80211_IFTYPE_STATION;
128 return 0;
129 } else if (strcmp(tpstr, "mp") == 0 ||
130 strcmp(tpstr, "mesh") == 0) {
131 *type = NL80211_IFTYPE_MESH_POINT;
132 return 0;
133 } else if (strcmp(tpstr, "__p2pcl") == 0) {
134 *type = NL80211_IFTYPE_P2P_CLIENT;
135 return 0;
136 } else if (strcmp(tpstr, "__p2pdev") == 0) {
137 *type = NL80211_IFTYPE_P2P_DEVICE;
138 return 0;
139 } else if (strcmp(tpstr, "__p2pgo") == 0) {
140 *type = NL80211_IFTYPE_P2P_GO;
141 return 0;
142 }
143
144 fprintf(stderr, "invalid interface type %s\n", tpstr);
145 return 2;
146 }
147
148 static 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
158 nla_put_failure:
159 return 1;
160 }
161
162 static int handle_interface_add(struct nl80211_state *state,
163 struct nl_cb *cb,
164 struct nl_msg *msg,
165 int argc, char **argv,
166 enum id_input id)
167 {
168 char *name;
169 char *mesh_id = NULL;
170 enum nl80211_iftype type;
171 int tpset;
172
173 if (argc < 1)
174 return 1;
175
176 name = argv[0];
177 argc--;
178 argv++;
179
180 tpset = get_if_type(&argc, &argv, &type, true);
181 if (tpset)
182 return tpset;
183
184 if (argc) {
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++;
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++;
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 {
211 return 1;
212 }
213 }
214
215 if (argc)
216 return 1;
217
218 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name);
219 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
220 if (mesh_id)
221 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
222
223 return 0;
224 nla_put_failure:
225 return -ENOBUFS;
226 }
227 COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*]",
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.");
234 COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*]",
235 NL80211_CMD_NEW_INTERFACE, 0, CIB_NETDEV, handle_interface_add, NULL);
236
237 static int handle_interface_del(struct nl80211_state *state,
238 struct nl_cb *cb,
239 struct nl_msg *msg,
240 int argc, char **argv,
241 enum id_input id)
242 {
243 return 0;
244 }
245 TOPLEVEL(del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del,
246 "Remove this virtual interface");
247 HIDDEN(interface, del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del);
248
249 static 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
265 char *channel_width_name(enum nl80211_chan_width width)
266 {
267 switch (width) {
268 case NL80211_CHAN_WIDTH_20_NOHT:
269 return "20 MHz (no HT)";
270 case NL80211_CHAN_WIDTH_20:
271 return "20 MHz";
272 case NL80211_CHAN_WIDTH_40:
273 return "40 MHz";
274 case NL80211_CHAN_WIDTH_80:
275 return "80 MHz";
276 case NL80211_CHAN_WIDTH_80P80:
277 return "80+80 MHz";
278 case NL80211_CHAN_WIDTH_160:
279 return "160 MHz";
280 default:
281 return "unknown";
282 }
283 }
284
285 static int print_iface_handler(struct nl_msg *msg, void *arg)
286 {
287 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
288 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
289 unsigned int *wiphy = arg;
290 const char *indent = "";
291
292 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
293 genlmsg_attrlen(gnlh, 0), NULL);
294
295 if (wiphy && tb_msg[NL80211_ATTR_WIPHY]) {
296 unsigned int thiswiphy = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]);
297 indent = "\t";
298 if (*wiphy != thiswiphy)
299 printf("phy#%d\n", thiswiphy);
300 *wiphy = thiswiphy;
301 }
302
303 if (tb_msg[NL80211_ATTR_IFNAME])
304 printf("%sInterface %s\n", indent, nla_get_string(tb_msg[NL80211_ATTR_IFNAME]));
305 else
306 printf("%sUnnamed/non-netdev interface\n", indent);
307 if (tb_msg[NL80211_ATTR_IFINDEX])
308 printf("%s\tifindex %d\n", indent, nla_get_u32(tb_msg[NL80211_ATTR_IFINDEX]));
309 if (tb_msg[NL80211_ATTR_WDEV])
310 printf("%s\twdev 0x%llx\n", indent,
311 (unsigned long long)nla_get_u64(tb_msg[NL80211_ATTR_WDEV]));
312 if (tb_msg[NL80211_ATTR_MAC]) {
313 char mac_addr[20];
314 mac_addr_n2a(mac_addr, nla_data(tb_msg[NL80211_ATTR_MAC]));
315 printf("%s\taddr %s\n", indent, mac_addr);
316 }
317 if (tb_msg[NL80211_ATTR_SSID]) {
318 printf("%s\tssid ", indent);
319 print_ssid_escaped(nla_len(tb_msg[NL80211_ATTR_SSID]),
320 nla_data(tb_msg[NL80211_ATTR_SSID]));
321 printf("\n");
322 }
323 if (tb_msg[NL80211_ATTR_IFTYPE])
324 printf("%s\ttype %s\n", indent, iftype_name(nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE])));
325 if (!wiphy && tb_msg[NL80211_ATTR_WIPHY])
326 printf("%s\twiphy %d\n", indent, nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]));
327 if (tb_msg[NL80211_ATTR_WIPHY_FREQ]) {
328 uint32_t freq = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY_FREQ]);
329
330 printf("%s\tchannel %d (%d MHz)", indent,
331 ieee80211_frequency_to_channel(freq), freq);
332
333 if (tb_msg[NL80211_ATTR_CHANNEL_WIDTH]) {
334 printf(", width: %s",
335 channel_width_name(nla_get_u32(tb_msg[NL80211_ATTR_CHANNEL_WIDTH])));
336 if (tb_msg[NL80211_ATTR_CENTER_FREQ1])
337 printf(", center1: %d MHz",
338 nla_get_u32(tb_msg[NL80211_ATTR_CENTER_FREQ1]));
339 if (tb_msg[NL80211_ATTR_CENTER_FREQ2])
340 printf(", center2: %d MHz",
341 nla_get_u32(tb_msg[NL80211_ATTR_CENTER_FREQ2]));
342 } else if (tb_msg[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
343 enum nl80211_channel_type channel_type;
344
345 channel_type = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
346 printf(" %s", channel_type_name(channel_type));
347 }
348
349 printf("\n");
350 }
351
352 return NL_SKIP;
353 }
354
355 static int handle_interface_info(struct nl80211_state *state,
356 struct nl_cb *cb,
357 struct nl_msg *msg,
358 int argc, char **argv,
359 enum id_input id)
360 {
361 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, NULL);
362 return 0;
363 }
364 TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info,
365 "Show information for this interface.");
366
367 static int handle_interface_set(struct nl80211_state *state,
368 struct nl_cb *cb,
369 struct nl_msg *msg,
370 int argc, char **argv,
371 enum id_input id)
372 {
373 if (!argc)
374 return 1;
375
376 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
377
378 switch (parse_mntr_flags(&argc, &argv, msg)) {
379 case 0:
380 return 0;
381 case -ENOMEM:
382 fprintf(stderr, "failed to allocate flags\n");
383 return 2;
384 case -EINVAL:
385 fprintf(stderr, "unknown flag %s\n", *argv);
386 return 2;
387 default:
388 return 2;
389 }
390 nla_put_failure:
391 return -ENOBUFS;
392 }
393 COMMAND(set, monitor, "<flag>*",
394 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_set,
395 "Set monitor flags. Valid flags are:\n"
396 VALID_FLAGS);
397
398 static int handle_interface_meshid(struct nl80211_state *state,
399 struct nl_cb *cb,
400 struct nl_msg *msg,
401 int argc, char **argv,
402 enum id_input id)
403 {
404 char *mesh_id = NULL;
405
406 if (argc != 1)
407 return 1;
408
409 mesh_id = argv[0];
410
411 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
412
413 return 0;
414 nla_put_failure:
415 return -ENOBUFS;
416 }
417 COMMAND(set, meshid, "<meshid>",
418 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_meshid, NULL);
419
420 static unsigned int dev_dump_wiphy;
421
422 static int handle_dev_dump(struct nl80211_state *state,
423 struct nl_cb *cb,
424 struct nl_msg *msg,
425 int argc, char **argv,
426 enum id_input id)
427 {
428 dev_dump_wiphy = -1;
429 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, &dev_dump_wiphy);
430 return 0;
431 }
432 TOPLEVEL(dev, NULL, NL80211_CMD_GET_INTERFACE, NLM_F_DUMP, CIB_NONE, handle_dev_dump,
433 "List all network interfaces for wireless hardware.");
434
435 static int handle_interface_type(struct nl80211_state *state,
436 struct nl_cb *cb,
437 struct nl_msg *msg,
438 int argc, char **argv,
439 enum id_input id)
440 {
441 enum nl80211_iftype type;
442 int tpset;
443
444 tpset = get_if_type(&argc, &argv, &type, false);
445 if (tpset)
446 return tpset;
447
448 if (argc)
449 return 1;
450
451 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
452
453 return 0;
454 nla_put_failure:
455 return -ENOBUFS;
456 }
457 COMMAND(set, type, "<type>",
458 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_type,
459 "Set interface type/mode.\n"
460 IFACE_TYPES);
461
462 static int handle_interface_4addr(struct nl80211_state *state,
463 struct nl_cb *cb,
464 struct nl_msg *msg,
465 int argc, char **argv,
466 enum id_input id)
467 {
468 if (argc != 1)
469 return 1;
470 return parse_4addr_flag(argv[0], msg);
471 }
472 COMMAND(set, 4addr, "<on|off>",
473 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_4addr,
474 "Set interface 4addr (WDS) mode.");
475
476 static int handle_interface_noack_map(struct nl80211_state *state,
477 struct nl_cb *cb,
478 struct nl_msg *msg,
479 int argc, char **argv,
480 enum id_input id)
481 {
482 uint16_t noack_map;
483 char *end;
484
485 if (argc != 1)
486 return 1;
487
488 noack_map = strtoul(argv[0], &end, 16);
489 if (*end)
490 return 1;
491
492 NLA_PUT_U16(msg, NL80211_ATTR_NOACK_MAP, noack_map);
493
494 return 0;
495 nla_put_failure:
496 return -ENOBUFS;
497
498 }
499 COMMAND(set, noack_map, "<map>",
500 NL80211_CMD_SET_NOACK_MAP, 0, CIB_NETDEV, handle_interface_noack_map,
501 "Set the NoAck map for the TIDs. (0x0009 = BE, 0x0006 = BK, 0x0030 = VI, 0x00C0 = VO)");
502
503
504 static int handle_interface_wds_peer(struct nl80211_state *state,
505 struct nl_cb *cb,
506 struct nl_msg *msg,
507 int argc, char **argv,
508 enum id_input id)
509 {
510 unsigned char mac_addr[ETH_ALEN];
511
512 if (argc < 1)
513 return 1;
514
515 if (mac_addr_a2n(mac_addr, argv[0])) {
516 fprintf(stderr, "Invalid MAC address\n");
517 return 2;
518 }
519
520 argc--;
521 argv++;
522
523 if (argc)
524 return 1;
525
526 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
527
528 return 0;
529 nla_put_failure:
530 return -ENOBUFS;
531 }
532 COMMAND(set, peer, "<MAC address>",
533 NL80211_CMD_SET_WDS_PEER, 0, CIB_NETDEV, handle_interface_wds_peer,
534 "Set interface WDS peer.");
535
536 static int set_mcast_rate(struct nl80211_state *state,
537 struct nl_cb *cb,
538 struct nl_msg *msg,
539 int argc, char **argv,
540 enum id_input id)
541 {
542 float rate;
543 char *end;
544
545 if (argc != 1) {
546 printf("Invalid parameters!\n");
547 return 2;
548 }
549
550 rate = strtod(argv[0], &end);
551 if (*end != '\0')
552 return 1;
553
554 NLA_PUT_U32(msg, NL80211_ATTR_MCAST_RATE, (int)(rate * 10));
555
556 return 0;
557 nla_put_failure:
558 return -ENOBUFS;
559 }
560
561 COMMAND(set, mcast_rate, "<rate in Mbps>",
562 NL80211_CMD_SET_MCAST_RATE, 0, CIB_NETDEV, set_mcast_rate,
563 "Set the multicast bitrate.");