5 #include <netlink/genl/genl.h>
6 #include <netlink/genl/family.h>
7 #include <netlink/genl/ctrl.h>
8 #include <netlink/msg.h>
9 #include <netlink/attr.h>
14 #define VALID_FLAGS "none: no special flags\n"\
15 "fcsfail: show frames with FCS errors\n"\
16 "control: show control frames\n"\
17 "otherbss: show frames from other BSSes\n"\
18 "cook: use cooked mode\n"\
19 "active: use active mode (ACK incoming unicast packets)\n"\
20 "mumimo-groupid <GROUP_ID>: use MUMIMO according to a group id\n"\
21 "mumimo-follow-mac <MAC_ADDRESS>: use MUMIMO according to a MAC address"
25 static char *mntr_flags
[NL80211_MNTR_FLAG_MAX
+ 1] = {
35 static int parse_mumimo_options(int *_argc
, char ***_argv
, struct nl_msg
*msg
)
37 uint8_t mumimo_group
[VHT_MUMIMO_GROUP_LEN
];
38 unsigned char mac_addr
[ETH_ALEN
];
44 if (strcmp(*argv
, "mumimo-groupid") == 0) {
47 if (!argc
|| strlen(*argv
) != VHT_MUMIMO_GROUP_LEN
*2) {
48 fprintf(stderr
, "Invalid groupID: %s\n", *argv
);
52 for (i
= 0; i
< VHT_MUMIMO_GROUP_LEN
; i
++) {
53 if (sscanf((*argv
) + i
*2, "%2x", &val
) != 1) {
54 fprintf(stderr
, "Failed reading groupID\n");
57 mumimo_group
[i
] = val
;
61 NL80211_ATTR_MU_MIMO_GROUP_DATA
,
66 } else if (strcmp(*argv
, "mumimo-follow-mac") == 0) {
69 if (!argc
|| mac_addr_a2n(mac_addr
, *argv
)) {
70 fprintf(stderr
, "Invalid MAC address\n");
73 NLA_PUT(msg
, NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR
,
84 static int parse_mntr_flags(int *_argc
, char ***_argv
,
89 enum nl80211_mntr_flags flag
;
93 flags
= nlmsg_alloc();
100 /* parse MU-MIMO options */
101 err
= parse_mumimo_options(&argc
, &argv
, msg
);
107 /* parse monitor flags */
108 for (flag
= __NL80211_MNTR_FLAG_INVALID
;
109 flag
<= NL80211_MNTR_FLAG_MAX
; flag
++) {
110 if (strcmp(*argv
, mntr_flags
[flag
]) == 0) {
113 * This shouldn't be adding "flag" if that is
114 * zero, but due to a problem in the kernel's
115 * nl80211 code (using NLA_NESTED policy) it
116 * will reject an empty nested attribute but
117 * not one that contains an invalid attribute
119 NLA_PUT_FLAG(flags
, flag
);
131 nla_put_nested(msg
, NL80211_ATTR_MNTR_FLAGS
, flags
);
144 #define IFACE_TYPES "Valid interface types are: managed, ibss, monitor, mesh, wds."
146 /* return 0 if ok, internal error otherwise */
147 static int get_if_type(int *argc
, char ***argv
, enum nl80211_iftype
*type
,
152 if (*argc
< 1 + !!need_type
)
155 if (need_type
&& strcmp((*argv
)[0], "type"))
158 tpstr
= (*argv
)[!!need_type
];
159 *argc
-= 1 + !!need_type
;
160 *argv
+= 1 + !!need_type
;
162 if (strcmp(tpstr
, "adhoc") == 0 ||
163 strcmp(tpstr
, "ibss") == 0) {
164 *type
= NL80211_IFTYPE_ADHOC
;
166 } else if (strcmp(tpstr
, "ocb") == 0) {
167 *type
= NL80211_IFTYPE_OCB
;
169 } else if (strcmp(tpstr
, "monitor") == 0) {
170 *type
= NL80211_IFTYPE_MONITOR
;
172 } else if (strcmp(tpstr
, "master") == 0 ||
173 strcmp(tpstr
, "ap") == 0) {
174 *type
= NL80211_IFTYPE_UNSPECIFIED
;
175 fprintf(stderr
, "You need to run a management daemon, e.g. hostapd,\n");
176 fprintf(stderr
, "see http://wireless.kernel.org/en/users/Documentation/hostapd\n");
177 fprintf(stderr
, "for more information on how to do that.\n");
179 } else if (strcmp(tpstr
, "__ap") == 0) {
180 *type
= NL80211_IFTYPE_AP
;
182 } else if (strcmp(tpstr
, "__ap_vlan") == 0) {
183 *type
= NL80211_IFTYPE_AP_VLAN
;
185 } else if (strcmp(tpstr
, "wds") == 0) {
186 *type
= NL80211_IFTYPE_WDS
;
188 } else if (strcmp(tpstr
, "managed") == 0 ||
189 strcmp(tpstr
, "mgd") == 0 ||
190 strcmp(tpstr
, "station") == 0) {
191 *type
= NL80211_IFTYPE_STATION
;
193 } else if (strcmp(tpstr
, "mp") == 0 ||
194 strcmp(tpstr
, "mesh") == 0) {
195 *type
= NL80211_IFTYPE_MESH_POINT
;
197 } else if (strcmp(tpstr
, "__p2pcl") == 0) {
198 *type
= NL80211_IFTYPE_P2P_CLIENT
;
200 } else if (strcmp(tpstr
, "__p2pdev") == 0) {
201 *type
= NL80211_IFTYPE_P2P_DEVICE
;
203 } else if (strcmp(tpstr
, "__p2pgo") == 0) {
204 *type
= NL80211_IFTYPE_P2P_GO
;
206 } else if (strcmp(tpstr
, "__nan") == 0) {
207 *type
= NL80211_IFTYPE_NAN
;
211 fprintf(stderr
, "invalid interface type %s\n", tpstr
);
215 static int parse_4addr_flag(const char *value
, struct nl_msg
*msg
)
217 if (strcmp(value
, "on") == 0)
218 NLA_PUT_U8(msg
, NL80211_ATTR_4ADDR
, 1);
219 else if (strcmp(value
, "off") == 0)
220 NLA_PUT_U8(msg
, NL80211_ATTR_4ADDR
, 0);
229 static int handle_interface_add(struct nl80211_state
*state
,
231 int argc
, char **argv
,
235 char *mesh_id
= NULL
;
236 enum nl80211_iftype type
;
238 unsigned char mac_addr
[ETH_ALEN
];
248 tpset
= get_if_type(&argc
, &argv
, &type
, true);
254 if (strcmp(argv
[0], "mesh_id") == 0) {
263 } else if (strcmp(argv
[0], "addr") == 0) {
266 if (mac_addr_a2n(mac_addr
, argv
[0])) {
267 fprintf(stderr
, "Invalid MAC address\n");
274 } else if (strcmp(argv
[0], "4addr") == 0) {
277 if (parse_4addr_flag(argv
[0], msg
)) {
278 fprintf(stderr
, "4addr error\n");
283 } else if (strcmp(argv
[0], "flags") == 0) {
286 if (parse_mntr_flags(&argc
, &argv
, msg
)) {
287 fprintf(stderr
, "flags error\n");
298 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, name
);
299 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
301 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
303 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
309 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*] [addr <mac-addr>]",
310 NL80211_CMD_NEW_INTERFACE
, 0, CIB_PHY
, handle_interface_add
,
311 "Add a new virtual interface with the given configuration.\n"
313 "The flags are only used for monitor interfaces, valid flags are:\n"
315 "The mesh_id is used only for mesh mode.");
316 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*] [addr <mac-addr>]",
317 NL80211_CMD_NEW_INTERFACE
, 0, CIB_NETDEV
, handle_interface_add
, NULL
);
319 static int handle_interface_del(struct nl80211_state
*state
,
321 int argc
, char **argv
,
326 TOPLEVEL(del
, NULL
, NL80211_CMD_DEL_INTERFACE
, 0, CIB_NETDEV
, handle_interface_del
,
327 "Remove this virtual interface");
328 HIDDEN(interface
, del
, NULL
, NL80211_CMD_DEL_INTERFACE
, 0, CIB_NETDEV
, handle_interface_del
);
330 static char *channel_type_name(enum nl80211_channel_type channel_type
)
332 switch (channel_type
) {
333 case NL80211_CHAN_NO_HT
:
335 case NL80211_CHAN_HT20
:
337 case NL80211_CHAN_HT40MINUS
:
339 case NL80211_CHAN_HT40PLUS
:
346 char *channel_width_name(enum nl80211_chan_width width
)
349 case NL80211_CHAN_WIDTH_20_NOHT
:
350 return "20 MHz (no HT)";
351 case NL80211_CHAN_WIDTH_20
:
353 case NL80211_CHAN_WIDTH_40
:
355 case NL80211_CHAN_WIDTH_80
:
357 case NL80211_CHAN_WIDTH_80P80
:
359 case NL80211_CHAN_WIDTH_160
:
361 case NL80211_CHAN_WIDTH_5
:
363 case NL80211_CHAN_WIDTH_10
:
370 static int print_iface_handler(struct nl_msg
*msg
, void *arg
)
372 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
373 struct nlattr
*tb_msg
[NL80211_ATTR_MAX
+ 1];
374 unsigned int *wiphy
= arg
;
375 const char *indent
= "";
377 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
378 genlmsg_attrlen(gnlh
, 0), NULL
);
380 if (wiphy
&& tb_msg
[NL80211_ATTR_WIPHY
]) {
381 unsigned int thiswiphy
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY
]);
383 if (*wiphy
!= thiswiphy
)
384 printf("phy#%d\n", thiswiphy
);
388 if (tb_msg
[NL80211_ATTR_IFNAME
])
389 printf("%sInterface %s\n", indent
, nla_get_string(tb_msg
[NL80211_ATTR_IFNAME
]));
391 printf("%sUnnamed/non-netdev interface\n", indent
);
392 if (tb_msg
[NL80211_ATTR_IFINDEX
])
393 printf("%s\tifindex %d\n", indent
, nla_get_u32(tb_msg
[NL80211_ATTR_IFINDEX
]));
394 if (tb_msg
[NL80211_ATTR_WDEV
])
395 printf("%s\twdev 0x%llx\n", indent
,
396 (unsigned long long)nla_get_u64(tb_msg
[NL80211_ATTR_WDEV
]));
397 if (tb_msg
[NL80211_ATTR_MAC
]) {
399 mac_addr_n2a(mac_addr
, nla_data(tb_msg
[NL80211_ATTR_MAC
]));
400 printf("%s\taddr %s\n", indent
, mac_addr
);
402 if (tb_msg
[NL80211_ATTR_SSID
]) {
403 printf("%s\tssid ", indent
);
404 print_ssid_escaped(nla_len(tb_msg
[NL80211_ATTR_SSID
]),
405 nla_data(tb_msg
[NL80211_ATTR_SSID
]));
408 if (tb_msg
[NL80211_ATTR_IFTYPE
])
409 printf("%s\ttype %s\n", indent
, iftype_name(nla_get_u32(tb_msg
[NL80211_ATTR_IFTYPE
])));
410 if (!wiphy
&& tb_msg
[NL80211_ATTR_WIPHY
])
411 printf("%s\twiphy %d\n", indent
, nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY
]));
412 if (tb_msg
[NL80211_ATTR_WIPHY_FREQ
]) {
413 uint32_t freq
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY_FREQ
]);
415 printf("%s\tchannel %d (%d MHz)", indent
,
416 ieee80211_frequency_to_channel(freq
), freq
);
418 if (tb_msg
[NL80211_ATTR_CHANNEL_WIDTH
]) {
419 printf(", width: %s",
420 channel_width_name(nla_get_u32(tb_msg
[NL80211_ATTR_CHANNEL_WIDTH
])));
421 if (tb_msg
[NL80211_ATTR_CENTER_FREQ1
])
422 printf(", center1: %d MHz",
423 nla_get_u32(tb_msg
[NL80211_ATTR_CENTER_FREQ1
]));
424 if (tb_msg
[NL80211_ATTR_CENTER_FREQ2
])
425 printf(", center2: %d MHz",
426 nla_get_u32(tb_msg
[NL80211_ATTR_CENTER_FREQ2
]));
427 } else if (tb_msg
[NL80211_ATTR_WIPHY_CHANNEL_TYPE
]) {
428 enum nl80211_channel_type channel_type
;
430 channel_type
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY_CHANNEL_TYPE
]);
431 printf(" %s", channel_type_name(channel_type
));
437 if (tb_msg
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
]) {
438 uint32_t txp
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
]);
440 printf("%s\ttxpower %d.%.2d dBm\n",
441 indent
, txp
/ 100, txp
% 100);
444 if (tb_msg
[NL80211_ATTR_TXQ_STATS
]) {
446 parse_txq_stats(buf
, sizeof(buf
), tb_msg
[NL80211_ATTR_TXQ_STATS
], 1, -1, indent
);
447 printf("%s\tmulticast TXQ:%s\n", indent
, buf
);
450 if (tb_msg
[NL80211_ATTR_4ADDR
]) {
451 uint8_t use_4addr
= nla_get_u8(tb_msg
[NL80211_ATTR_4ADDR
]);
453 printf("%s\t4addr: on\n", indent
);
459 static int handle_interface_info(struct nl80211_state
*state
,
461 int argc
, char **argv
,
464 register_handler(print_iface_handler
, NULL
);
467 TOPLEVEL(info
, NULL
, NL80211_CMD_GET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_info
,
468 "Show information for this interface.");
470 static int handle_interface_set(struct nl80211_state
*state
,
472 int argc
, char **argv
,
478 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_MONITOR
);
480 switch (parse_mntr_flags(&argc
, &argv
, msg
)) {
486 fprintf(stderr
, "failed to allocate flags\n");
489 fprintf(stderr
, "unknown flag %s\n", *argv
);
497 COMMAND(set
, monitor
, "<flag>*",
498 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_set
,
499 "Set monitor flags. Valid flags are:\n"
502 static int handle_interface_meshid(struct nl80211_state
*state
,
504 int argc
, char **argv
,
507 char *mesh_id
= NULL
;
514 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
520 COMMAND(set
, meshid
, "<meshid>",
521 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_meshid
, NULL
);
523 static unsigned int dev_dump_wiphy
;
525 static int handle_dev_dump(struct nl80211_state
*state
,
527 int argc
, char **argv
,
531 register_handler(print_iface_handler
, &dev_dump_wiphy
);
534 TOPLEVEL(dev
, NULL
, NL80211_CMD_GET_INTERFACE
, NLM_F_DUMP
, CIB_NONE
, handle_dev_dump
,
535 "List all network interfaces for wireless hardware.");
537 static int handle_interface_type(struct nl80211_state
*state
,
539 int argc
, char **argv
,
542 enum nl80211_iftype type
;
545 tpset
= get_if_type(&argc
, &argv
, &type
, false);
552 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
558 COMMAND(set
, type
, "<type>",
559 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_type
,
560 "Set interface type/mode.\n"
563 static int handle_interface_4addr(struct nl80211_state
*state
,
565 int argc
, char **argv
,
570 return parse_4addr_flag(argv
[0], msg
);
572 COMMAND(set
, 4addr
, "<on|off>",
573 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_4addr
,
574 "Set interface 4addr (WDS) mode.");
576 static int handle_interface_noack_map(struct nl80211_state
*state
,
578 int argc
, char **argv
,
587 noack_map
= strtoul(argv
[0], &end
, 16);
591 NLA_PUT_U16(msg
, NL80211_ATTR_NOACK_MAP
, noack_map
);
598 COMMAND(set
, noack_map
, "<map>",
599 NL80211_CMD_SET_NOACK_MAP
, 0, CIB_NETDEV
, handle_interface_noack_map
,
600 "Set the NoAck map for the TIDs. (0x0009 = BE, 0x0006 = BK, 0x0030 = VI, 0x00C0 = VO)");
603 static int handle_interface_wds_peer(struct nl80211_state
*state
,
605 int argc
, char **argv
,
608 unsigned char mac_addr
[ETH_ALEN
];
613 if (mac_addr_a2n(mac_addr
, argv
[0])) {
614 fprintf(stderr
, "Invalid MAC address\n");
624 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
630 COMMAND(set
, peer
, "<MAC address>",
631 NL80211_CMD_SET_WDS_PEER
, 0, CIB_NETDEV
, handle_interface_wds_peer
,
632 "Set interface WDS peer.");
634 static int set_mcast_rate(struct nl80211_state
*state
,
636 int argc
, char **argv
,
645 rate
= strtod(argv
[0], &end
);
649 NLA_PUT_U32(msg
, NL80211_ATTR_MCAST_RATE
, (int)(rate
* 10));
656 COMMAND(set
, mcast_rate
, "<rate in Mbps>",
657 NL80211_CMD_SET_MCAST_RATE
, 0, CIB_NETDEV
, set_mcast_rate
,
658 "Set the multicast bitrate.");
661 static int handle_chanfreq(struct nl80211_state
*state
, struct nl_msg
*msg
,
662 bool chan
, int argc
, char **argv
,
665 struct chandef chandef
;
670 res
= parse_freqchan(&chandef
, chan
, argc
, argv
, &parsed
);
678 unsigned int beacons
= 10;
680 if (strcmp(argv
[0], "beacons") == 0) {
684 beacons
= strtol(argv
[1], &end
, 10);
691 NLA_PUT_U32(msg
, NL80211_ATTR_CH_SWITCH_COUNT
, beacons
);
692 } else if (strcmp(argv
[0], "block-tx") == 0) {
696 NLA_PUT_FLAG(msg
, NL80211_ATTR_CH_SWITCH_BLOCK_TX
);
702 return put_chandef(msg
, &chandef
);
708 static int handle_freq(struct nl80211_state
*state
, struct nl_msg
*msg
,
709 int argc
, char **argv
,
712 return handle_chanfreq(state
, msg
, false, argc
, argv
, id
);
715 static int handle_chan(struct nl80211_state
*state
, struct nl_msg
*msg
,
716 int argc
, char **argv
,
719 return handle_chanfreq(state
, msg
, true, argc
, argv
, id
);
723 COMMAND(switch, freq
,
724 "<freq> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz] [beacons <count>] [block-tx]\n"
725 "<control freq> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]] [beacons <count>] [block-tx]",
726 NL80211_CMD_CHANNEL_SWITCH
, 0, CIB_NETDEV
, handle_freq
,
727 "Switch the operating channel by sending a channel switch announcement (CSA).");
728 COMMAND(switch, channel
, "<channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz] [beacons <count>] [block-tx]",
729 NL80211_CMD_CHANNEL_SWITCH
, 0, CIB_NETDEV
, handle_chan
, NULL
);