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>
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)"
24 static char *mntr_flags
[NL80211_MNTR_FLAG_MAX
+ 1] = {
34 static int parse_mntr_flags(int *_argc
, char ***_argv
,
39 enum nl80211_mntr_flags flag
;
43 flags
= nlmsg_alloc();
49 for (flag
= __NL80211_MNTR_FLAG_INVALID
;
50 flag
<= NL80211_MNTR_FLAG_MAX
; flag
++) {
51 if (strcmp(*argv
, mntr_flags
[flag
]) == 0) {
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
60 NLA_PUT_FLAG(flags
, flag
);
72 nla_put_nested(msg
, NL80211_ATTR_MNTR_FLAGS
, flags
);
85 #define IFACE_TYPES "Valid interface types are: managed, ibss, monitor, mesh, wds."
87 /* return 0 if ok, internal error otherwise */
88 static int get_if_type(int *argc
, char ***argv
, enum nl80211_iftype
*type
,
93 if (*argc
< 1 + !!need_type
)
96 if (need_type
&& strcmp((*argv
)[0], "type"))
99 tpstr
= (*argv
)[!!need_type
];
100 *argc
-= 1 + !!need_type
;
101 *argv
+= 1 + !!need_type
;
103 if (strcmp(tpstr
, "adhoc") == 0 ||
104 strcmp(tpstr
, "ibss") == 0) {
105 *type
= NL80211_IFTYPE_ADHOC
;
107 } else if (strcmp(tpstr
, "monitor") == 0) {
108 *type
= NL80211_IFTYPE_MONITOR
;
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");
117 } else if (strcmp(tpstr
, "__ap") == 0) {
118 *type
= NL80211_IFTYPE_AP
;
120 } else if (strcmp(tpstr
, "__ap_vlan") == 0) {
121 *type
= NL80211_IFTYPE_AP_VLAN
;
123 } else if (strcmp(tpstr
, "wds") == 0) {
124 *type
= NL80211_IFTYPE_WDS
;
126 } else if (strcmp(tpstr
, "managed") == 0 ||
127 strcmp(tpstr
, "mgd") == 0 ||
128 strcmp(tpstr
, "station") == 0) {
129 *type
= NL80211_IFTYPE_STATION
;
131 } else if (strcmp(tpstr
, "mp") == 0 ||
132 strcmp(tpstr
, "mesh") == 0) {
133 *type
= NL80211_IFTYPE_MESH_POINT
;
135 } else if (strcmp(tpstr
, "__p2pcl") == 0) {
136 *type
= NL80211_IFTYPE_P2P_CLIENT
;
138 } else if (strcmp(tpstr
, "__p2pdev") == 0) {
139 *type
= NL80211_IFTYPE_P2P_DEVICE
;
141 } else if (strcmp(tpstr
, "__p2pgo") == 0) {
142 *type
= NL80211_IFTYPE_P2P_GO
;
146 fprintf(stderr
, "invalid interface type %s\n", tpstr
);
150 static int parse_4addr_flag(const char *value
, struct nl_msg
*msg
)
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);
164 static int handle_interface_add(struct nl80211_state
*state
,
167 int argc
, char **argv
,
171 char *mesh_id
= NULL
;
172 enum nl80211_iftype type
;
182 tpset
= get_if_type(&argc
, &argv
, &type
, true);
187 if (strcmp(argv
[0], "mesh_id") == 0) {
196 } else if (strcmp(argv
[0], "4addr") == 0) {
199 if (parse_4addr_flag(argv
[0], msg
)) {
200 fprintf(stderr
, "4addr error\n");
205 } else if (strcmp(argv
[0], "flags") == 0) {
208 if (parse_mntr_flags(&argc
, &argv
, msg
)) {
209 fprintf(stderr
, "flags error\n");
220 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, name
);
221 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
223 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
229 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*]",
230 NL80211_CMD_NEW_INTERFACE
, 0, CIB_PHY
, handle_interface_add
,
231 "Add a new virtual interface with the given configuration.\n"
233 "The flags are only used for monitor interfaces, valid flags are:\n"
235 "The mesh_id is used only for mesh mode.");
236 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*]",
237 NL80211_CMD_NEW_INTERFACE
, 0, CIB_NETDEV
, handle_interface_add
, NULL
);
239 static int handle_interface_del(struct nl80211_state
*state
,
242 int argc
, char **argv
,
247 TOPLEVEL(del
, NULL
, NL80211_CMD_DEL_INTERFACE
, 0, CIB_NETDEV
, handle_interface_del
,
248 "Remove this virtual interface");
249 HIDDEN(interface
, del
, NULL
, NL80211_CMD_DEL_INTERFACE
, 0, CIB_NETDEV
, handle_interface_del
);
251 static char *channel_type_name(enum nl80211_channel_type channel_type
)
253 switch (channel_type
) {
254 case NL80211_CHAN_NO_HT
:
256 case NL80211_CHAN_HT20
:
258 case NL80211_CHAN_HT40MINUS
:
260 case NL80211_CHAN_HT40PLUS
:
267 char *channel_width_name(enum nl80211_chan_width width
)
270 case NL80211_CHAN_WIDTH_20_NOHT
:
271 return "20 MHz (no HT)";
272 case NL80211_CHAN_WIDTH_20
:
274 case NL80211_CHAN_WIDTH_40
:
276 case NL80211_CHAN_WIDTH_80
:
278 case NL80211_CHAN_WIDTH_80P80
:
280 case NL80211_CHAN_WIDTH_160
:
287 static int print_iface_handler(struct nl_msg
*msg
, void *arg
)
289 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
290 struct nlattr
*tb_msg
[NL80211_ATTR_MAX
+ 1];
291 unsigned int *wiphy
= arg
;
292 const char *indent
= "";
294 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
295 genlmsg_attrlen(gnlh
, 0), NULL
);
297 if (wiphy
&& tb_msg
[NL80211_ATTR_WIPHY
]) {
298 unsigned int thiswiphy
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY
]);
300 if (*wiphy
!= thiswiphy
)
301 printf("phy#%d\n", thiswiphy
);
305 if (tb_msg
[NL80211_ATTR_IFNAME
])
306 printf("%sInterface %s\n", indent
, nla_get_string(tb_msg
[NL80211_ATTR_IFNAME
]));
308 printf("%sUnnamed/non-netdev interface\n", indent
);
309 if (tb_msg
[NL80211_ATTR_IFINDEX
])
310 printf("%s\tifindex %d\n", indent
, nla_get_u32(tb_msg
[NL80211_ATTR_IFINDEX
]));
311 if (tb_msg
[NL80211_ATTR_WDEV
])
312 printf("%s\twdev 0x%llx\n", indent
,
313 (unsigned long long)nla_get_u64(tb_msg
[NL80211_ATTR_WDEV
]));
314 if (tb_msg
[NL80211_ATTR_MAC
]) {
316 mac_addr_n2a(mac_addr
, nla_data(tb_msg
[NL80211_ATTR_MAC
]));
317 printf("%s\taddr %s\n", indent
, mac_addr
);
319 if (tb_msg
[NL80211_ATTR_SSID
]) {
320 printf("%s\tssid ", indent
);
321 print_ssid_escaped(nla_len(tb_msg
[NL80211_ATTR_SSID
]),
322 nla_data(tb_msg
[NL80211_ATTR_SSID
]));
325 if (tb_msg
[NL80211_ATTR_IFTYPE
])
326 printf("%s\ttype %s\n", indent
, iftype_name(nla_get_u32(tb_msg
[NL80211_ATTR_IFTYPE
])));
327 if (!wiphy
&& tb_msg
[NL80211_ATTR_WIPHY
])
328 printf("%s\twiphy %d\n", indent
, nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY
]));
329 if (tb_msg
[NL80211_ATTR_WIPHY_FREQ
]) {
330 uint32_t freq
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY_FREQ
]);
332 printf("%s\tchannel %d (%d MHz)", indent
,
333 ieee80211_frequency_to_channel(freq
), freq
);
335 if (tb_msg
[NL80211_ATTR_CHANNEL_WIDTH
]) {
336 printf(", width: %s",
337 channel_width_name(nla_get_u32(tb_msg
[NL80211_ATTR_CHANNEL_WIDTH
])));
338 if (tb_msg
[NL80211_ATTR_CENTER_FREQ1
])
339 printf(", center1: %d MHz",
340 nla_get_u32(tb_msg
[NL80211_ATTR_CENTER_FREQ1
]));
341 if (tb_msg
[NL80211_ATTR_CENTER_FREQ2
])
342 printf(", center2: %d MHz",
343 nla_get_u32(tb_msg
[NL80211_ATTR_CENTER_FREQ2
]));
344 } else if (tb_msg
[NL80211_ATTR_WIPHY_CHANNEL_TYPE
]) {
345 enum nl80211_channel_type channel_type
;
347 channel_type
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY_CHANNEL_TYPE
]);
348 printf(" %s", channel_type_name(channel_type
));
357 static int handle_interface_info(struct nl80211_state
*state
,
360 int argc
, char **argv
,
363 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_iface_handler
, NULL
);
366 TOPLEVEL(info
, NULL
, NL80211_CMD_GET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_info
,
367 "Show information for this interface.");
369 static int handle_interface_set(struct nl80211_state
*state
,
372 int argc
, char **argv
,
378 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_MONITOR
);
380 switch (parse_mntr_flags(&argc
, &argv
, msg
)) {
384 fprintf(stderr
, "failed to allocate flags\n");
387 fprintf(stderr
, "unknown flag %s\n", *argv
);
395 COMMAND(set
, monitor
, "<flag>*",
396 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_set
,
397 "Set monitor flags. Valid flags are:\n"
400 static int handle_interface_meshid(struct nl80211_state
*state
,
403 int argc
, char **argv
,
406 char *mesh_id
= NULL
;
413 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
419 COMMAND(set
, meshid
, "<meshid>",
420 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_meshid
, NULL
);
422 static unsigned int dev_dump_wiphy
;
424 static int handle_dev_dump(struct nl80211_state
*state
,
427 int argc
, char **argv
,
431 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_iface_handler
, &dev_dump_wiphy
);
434 TOPLEVEL(dev
, NULL
, NL80211_CMD_GET_INTERFACE
, NLM_F_DUMP
, CIB_NONE
, handle_dev_dump
,
435 "List all network interfaces for wireless hardware.");
437 static int handle_interface_type(struct nl80211_state
*state
,
440 int argc
, char **argv
,
443 enum nl80211_iftype type
;
446 tpset
= get_if_type(&argc
, &argv
, &type
, false);
453 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
459 COMMAND(set
, type
, "<type>",
460 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_type
,
461 "Set interface type/mode.\n"
464 static int handle_interface_4addr(struct nl80211_state
*state
,
467 int argc
, char **argv
,
472 return parse_4addr_flag(argv
[0], msg
);
474 COMMAND(set
, 4addr
, "<on|off>",
475 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_4addr
,
476 "Set interface 4addr (WDS) mode.");
478 static int handle_interface_noack_map(struct nl80211_state
*state
,
481 int argc
, char **argv
,
490 noack_map
= strtoul(argv
[0], &end
, 16);
494 NLA_PUT_U16(msg
, NL80211_ATTR_NOACK_MAP
, noack_map
);
501 COMMAND(set
, noack_map
, "<map>",
502 NL80211_CMD_SET_NOACK_MAP
, 0, CIB_NETDEV
, handle_interface_noack_map
,
503 "Set the NoAck map for the TIDs. (0x0009 = BE, 0x0006 = BK, 0x0030 = VI, 0x00C0 = VO)");
506 static int handle_interface_wds_peer(struct nl80211_state
*state
,
509 int argc
, char **argv
,
512 unsigned char mac_addr
[ETH_ALEN
];
517 if (mac_addr_a2n(mac_addr
, argv
[0])) {
518 fprintf(stderr
, "Invalid MAC address\n");
528 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
534 COMMAND(set
, peer
, "<MAC address>",
535 NL80211_CMD_SET_WDS_PEER
, 0, CIB_NETDEV
, handle_interface_wds_peer
,
536 "Set interface WDS peer.");
538 static int set_mcast_rate(struct nl80211_state
*state
,
541 int argc
, char **argv
,
548 printf("Invalid parameters!\n");
552 rate
= strtod(argv
[0], &end
);
556 NLA_PUT_U32(msg
, NL80211_ATTR_MCAST_RATE
, (int)(rate
* 10));
563 COMMAND(set
, mcast_rate
, "<rate in Mbps>",
564 NL80211_CMD_SET_MCAST_RATE
, 0, CIB_NETDEV
, set_mcast_rate
,
565 "Set the multicast bitrate.");