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)"
23 static char *mntr_flags
[NL80211_MNTR_FLAG_MAX
+ 1] = {
33 static int parse_mntr_flags(int *_argc
, char ***_argv
,
38 enum nl80211_mntr_flags flag
;
42 flags
= nlmsg_alloc();
48 for (flag
= __NL80211_MNTR_FLAG_INVALID
;
49 flag
<= NL80211_MNTR_FLAG_MAX
; flag
++) {
50 if (strcmp(*argv
, mntr_flags
[flag
]) == 0) {
53 * This shouldn't be adding "flag" if that is
54 * zero, but due to a problem in the kernel's
55 * nl80211 code (using NLA_NESTED policy) it
56 * will reject an empty nested attribute but
57 * not one that contains an invalid attribute
59 NLA_PUT_FLAG(flags
, flag
);
71 nla_put_nested(msg
, NL80211_ATTR_MNTR_FLAGS
, flags
);
84 #define IFACE_TYPES "Valid interface types are: managed, ibss, monitor, mesh, wds."
86 /* return 0 if ok, internal error otherwise */
87 static int get_if_type(int *argc
, char ***argv
, enum nl80211_iftype
*type
,
92 if (*argc
< 1 + !!need_type
)
95 if (need_type
&& strcmp((*argv
)[0], "type"))
98 tpstr
= (*argv
)[!!need_type
];
99 *argc
-= 1 + !!need_type
;
100 *argv
+= 1 + !!need_type
;
102 if (strcmp(tpstr
, "adhoc") == 0 ||
103 strcmp(tpstr
, "ibss") == 0) {
104 *type
= NL80211_IFTYPE_ADHOC
;
106 } else if (strcmp(tpstr
, "ocb") == 0) {
107 *type
= NL80211_IFTYPE_OCB
;
109 } else if (strcmp(tpstr
, "monitor") == 0) {
110 *type
= NL80211_IFTYPE_MONITOR
;
112 } else if (strcmp(tpstr
, "master") == 0 ||
113 strcmp(tpstr
, "ap") == 0) {
114 *type
= NL80211_IFTYPE_UNSPECIFIED
;
115 fprintf(stderr
, "You need to run a management daemon, e.g. hostapd,\n");
116 fprintf(stderr
, "see http://wireless.kernel.org/en/users/Documentation/hostapd\n");
117 fprintf(stderr
, "for more information on how to do that.\n");
119 } else if (strcmp(tpstr
, "__ap") == 0) {
120 *type
= NL80211_IFTYPE_AP
;
122 } else if (strcmp(tpstr
, "__ap_vlan") == 0) {
123 *type
= NL80211_IFTYPE_AP_VLAN
;
125 } else if (strcmp(tpstr
, "wds") == 0) {
126 *type
= NL80211_IFTYPE_WDS
;
128 } else if (strcmp(tpstr
, "managed") == 0 ||
129 strcmp(tpstr
, "mgd") == 0 ||
130 strcmp(tpstr
, "station") == 0) {
131 *type
= NL80211_IFTYPE_STATION
;
133 } else if (strcmp(tpstr
, "mp") == 0 ||
134 strcmp(tpstr
, "mesh") == 0) {
135 *type
= NL80211_IFTYPE_MESH_POINT
;
137 } else if (strcmp(tpstr
, "__p2pcl") == 0) {
138 *type
= NL80211_IFTYPE_P2P_CLIENT
;
140 } else if (strcmp(tpstr
, "__p2pdev") == 0) {
141 *type
= NL80211_IFTYPE_P2P_DEVICE
;
143 } else if (strcmp(tpstr
, "__p2pgo") == 0) {
144 *type
= NL80211_IFTYPE_P2P_GO
;
148 fprintf(stderr
, "invalid interface type %s\n", tpstr
);
152 static int parse_4addr_flag(const char *value
, struct nl_msg
*msg
)
154 if (strcmp(value
, "on") == 0)
155 NLA_PUT_U8(msg
, NL80211_ATTR_4ADDR
, 1);
156 else if (strcmp(value
, "off") == 0)
157 NLA_PUT_U8(msg
, NL80211_ATTR_4ADDR
, 0);
166 static int handle_interface_add(struct nl80211_state
*state
,
168 int argc
, char **argv
,
172 char *mesh_id
= NULL
;
173 enum nl80211_iftype type
;
175 unsigned char mac_addr
[ETH_ALEN
];
185 tpset
= get_if_type(&argc
, &argv
, &type
, true);
191 if (strcmp(argv
[0], "mesh_id") == 0) {
200 } else if (strcmp(argv
[0], "addr") == 0) {
203 if (mac_addr_a2n(mac_addr
, argv
[0])) {
204 fprintf(stderr
, "Invalid MAC address\n");
211 } else if (strcmp(argv
[0], "4addr") == 0) {
214 if (parse_4addr_flag(argv
[0], msg
)) {
215 fprintf(stderr
, "4addr error\n");
220 } else if (strcmp(argv
[0], "flags") == 0) {
223 if (parse_mntr_flags(&argc
, &argv
, msg
)) {
224 fprintf(stderr
, "flags error\n");
235 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, name
);
236 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
238 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
240 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
246 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*] [addr <mac-addr>]",
247 NL80211_CMD_NEW_INTERFACE
, 0, CIB_PHY
, handle_interface_add
,
248 "Add a new virtual interface with the given configuration.\n"
250 "The flags are only used for monitor interfaces, valid flags are:\n"
252 "The mesh_id is used only for mesh mode.");
253 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*] [addr <mac-addr>]",
254 NL80211_CMD_NEW_INTERFACE
, 0, CIB_NETDEV
, handle_interface_add
, NULL
);
256 static int handle_interface_del(struct nl80211_state
*state
,
258 int argc
, char **argv
,
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
);
267 static char *channel_type_name(enum nl80211_channel_type channel_type
)
269 switch (channel_type
) {
270 case NL80211_CHAN_NO_HT
:
272 case NL80211_CHAN_HT20
:
274 case NL80211_CHAN_HT40MINUS
:
276 case NL80211_CHAN_HT40PLUS
:
283 char *channel_width_name(enum nl80211_chan_width width
)
286 case NL80211_CHAN_WIDTH_20_NOHT
:
287 return "20 MHz (no HT)";
288 case NL80211_CHAN_WIDTH_20
:
290 case NL80211_CHAN_WIDTH_40
:
292 case NL80211_CHAN_WIDTH_80
:
294 case NL80211_CHAN_WIDTH_80P80
:
296 case NL80211_CHAN_WIDTH_160
:
298 case NL80211_CHAN_WIDTH_5
:
300 case NL80211_CHAN_WIDTH_10
:
307 static int print_iface_handler(struct nl_msg
*msg
, void *arg
)
309 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
310 struct nlattr
*tb_msg
[NL80211_ATTR_MAX
+ 1];
311 unsigned int *wiphy
= arg
;
312 const char *indent
= "";
314 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
315 genlmsg_attrlen(gnlh
, 0), NULL
);
317 if (wiphy
&& tb_msg
[NL80211_ATTR_WIPHY
]) {
318 unsigned int thiswiphy
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY
]);
320 if (*wiphy
!= thiswiphy
)
321 printf("phy#%d\n", thiswiphy
);
325 if (tb_msg
[NL80211_ATTR_IFNAME
])
326 printf("%sInterface %s\n", indent
, nla_get_string(tb_msg
[NL80211_ATTR_IFNAME
]));
328 printf("%sUnnamed/non-netdev interface\n", indent
);
329 if (tb_msg
[NL80211_ATTR_IFINDEX
])
330 printf("%s\tifindex %d\n", indent
, nla_get_u32(tb_msg
[NL80211_ATTR_IFINDEX
]));
331 if (tb_msg
[NL80211_ATTR_WDEV
])
332 printf("%s\twdev 0x%llx\n", indent
,
333 (unsigned long long)nla_get_u64(tb_msg
[NL80211_ATTR_WDEV
]));
334 if (tb_msg
[NL80211_ATTR_MAC
]) {
336 mac_addr_n2a(mac_addr
, nla_data(tb_msg
[NL80211_ATTR_MAC
]));
337 printf("%s\taddr %s\n", indent
, mac_addr
);
339 if (tb_msg
[NL80211_ATTR_SSID
]) {
340 printf("%s\tssid ", indent
);
341 print_ssid_escaped(nla_len(tb_msg
[NL80211_ATTR_SSID
]),
342 nla_data(tb_msg
[NL80211_ATTR_SSID
]));
345 if (tb_msg
[NL80211_ATTR_IFTYPE
])
346 printf("%s\ttype %s\n", indent
, iftype_name(nla_get_u32(tb_msg
[NL80211_ATTR_IFTYPE
])));
347 if (!wiphy
&& tb_msg
[NL80211_ATTR_WIPHY
])
348 printf("%s\twiphy %d\n", indent
, nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY
]));
349 if (tb_msg
[NL80211_ATTR_WIPHY_FREQ
]) {
350 uint32_t freq
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY_FREQ
]);
352 printf("%s\tchannel %d (%d MHz)", indent
,
353 ieee80211_frequency_to_channel(freq
), freq
);
355 if (tb_msg
[NL80211_ATTR_CHANNEL_WIDTH
]) {
356 printf(", width: %s",
357 channel_width_name(nla_get_u32(tb_msg
[NL80211_ATTR_CHANNEL_WIDTH
])));
358 if (tb_msg
[NL80211_ATTR_CENTER_FREQ1
])
359 printf(", center1: %d MHz",
360 nla_get_u32(tb_msg
[NL80211_ATTR_CENTER_FREQ1
]));
361 if (tb_msg
[NL80211_ATTR_CENTER_FREQ2
])
362 printf(", center2: %d MHz",
363 nla_get_u32(tb_msg
[NL80211_ATTR_CENTER_FREQ2
]));
364 } else if (tb_msg
[NL80211_ATTR_WIPHY_CHANNEL_TYPE
]) {
365 enum nl80211_channel_type channel_type
;
367 channel_type
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY_CHANNEL_TYPE
]);
368 printf(" %s", channel_type_name(channel_type
));
374 if (tb_msg
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
]) {
375 uint32_t txp
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
]);
377 printf("%s\ttxpower %d.%.2d dBm\n",
378 indent
, txp
/ 100, txp
% 100);
384 static int handle_interface_info(struct nl80211_state
*state
,
386 int argc
, char **argv
,
389 register_handler(print_iface_handler
, NULL
);
392 TOPLEVEL(info
, NULL
, NL80211_CMD_GET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_info
,
393 "Show information for this interface.");
395 static int handle_interface_set(struct nl80211_state
*state
,
397 int argc
, char **argv
,
403 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_MONITOR
);
405 switch (parse_mntr_flags(&argc
, &argv
, msg
)) {
409 fprintf(stderr
, "failed to allocate flags\n");
412 fprintf(stderr
, "unknown flag %s\n", *argv
);
420 COMMAND(set
, monitor
, "<flag>*",
421 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_set
,
422 "Set monitor flags. Valid flags are:\n"
425 static int handle_interface_meshid(struct nl80211_state
*state
,
427 int argc
, char **argv
,
430 char *mesh_id
= NULL
;
437 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
443 COMMAND(set
, meshid
, "<meshid>",
444 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_meshid
, NULL
);
446 static unsigned int dev_dump_wiphy
;
448 static int handle_dev_dump(struct nl80211_state
*state
,
450 int argc
, char **argv
,
454 register_handler(print_iface_handler
, &dev_dump_wiphy
);
457 TOPLEVEL(dev
, NULL
, NL80211_CMD_GET_INTERFACE
, NLM_F_DUMP
, CIB_NONE
, handle_dev_dump
,
458 "List all network interfaces for wireless hardware.");
460 static int handle_interface_type(struct nl80211_state
*state
,
462 int argc
, char **argv
,
465 enum nl80211_iftype type
;
468 tpset
= get_if_type(&argc
, &argv
, &type
, false);
475 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
481 COMMAND(set
, type
, "<type>",
482 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_type
,
483 "Set interface type/mode.\n"
486 static int handle_interface_4addr(struct nl80211_state
*state
,
488 int argc
, char **argv
,
493 return parse_4addr_flag(argv
[0], msg
);
495 COMMAND(set
, 4addr
, "<on|off>",
496 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_4addr
,
497 "Set interface 4addr (WDS) mode.");
499 static int handle_interface_noack_map(struct nl80211_state
*state
,
501 int argc
, char **argv
,
510 noack_map
= strtoul(argv
[0], &end
, 16);
514 NLA_PUT_U16(msg
, NL80211_ATTR_NOACK_MAP
, noack_map
);
521 COMMAND(set
, noack_map
, "<map>",
522 NL80211_CMD_SET_NOACK_MAP
, 0, CIB_NETDEV
, handle_interface_noack_map
,
523 "Set the NoAck map for the TIDs. (0x0009 = BE, 0x0006 = BK, 0x0030 = VI, 0x00C0 = VO)");
526 static int handle_interface_wds_peer(struct nl80211_state
*state
,
528 int argc
, char **argv
,
531 unsigned char mac_addr
[ETH_ALEN
];
536 if (mac_addr_a2n(mac_addr
, argv
[0])) {
537 fprintf(stderr
, "Invalid MAC address\n");
547 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
553 COMMAND(set
, peer
, "<MAC address>",
554 NL80211_CMD_SET_WDS_PEER
, 0, CIB_NETDEV
, handle_interface_wds_peer
,
555 "Set interface WDS peer.");
557 static int set_mcast_rate(struct nl80211_state
*state
,
559 int argc
, char **argv
,
568 rate
= strtod(argv
[0], &end
);
572 NLA_PUT_U32(msg
, NL80211_ATTR_MCAST_RATE
, (int)(rate
* 10));
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.");