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