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
, "ocb") == 0) {
108 *type
= NL80211_IFTYPE_OCB
;
110 } else if (strcmp(tpstr
, "monitor") == 0) {
111 *type
= NL80211_IFTYPE_MONITOR
;
113 } else if (strcmp(tpstr
, "master") == 0 ||
114 strcmp(tpstr
, "ap") == 0) {
115 *type
= NL80211_IFTYPE_UNSPECIFIED
;
116 fprintf(stderr
, "You need to run a management daemon, e.g. hostapd,\n");
117 fprintf(stderr
, "see http://wireless.kernel.org/en/users/Documentation/hostapd\n");
118 fprintf(stderr
, "for more information on how to do that.\n");
120 } else if (strcmp(tpstr
, "__ap") == 0) {
121 *type
= NL80211_IFTYPE_AP
;
123 } else if (strcmp(tpstr
, "__ap_vlan") == 0) {
124 *type
= NL80211_IFTYPE_AP_VLAN
;
126 } else if (strcmp(tpstr
, "wds") == 0) {
127 *type
= NL80211_IFTYPE_WDS
;
129 } else if (strcmp(tpstr
, "managed") == 0 ||
130 strcmp(tpstr
, "mgd") == 0 ||
131 strcmp(tpstr
, "station") == 0) {
132 *type
= NL80211_IFTYPE_STATION
;
134 } else if (strcmp(tpstr
, "mp") == 0 ||
135 strcmp(tpstr
, "mesh") == 0) {
136 *type
= NL80211_IFTYPE_MESH_POINT
;
138 } else if (strcmp(tpstr
, "__p2pcl") == 0) {
139 *type
= NL80211_IFTYPE_P2P_CLIENT
;
141 } else if (strcmp(tpstr
, "__p2pdev") == 0) {
142 *type
= NL80211_IFTYPE_P2P_DEVICE
;
144 } else if (strcmp(tpstr
, "__p2pgo") == 0) {
145 *type
= NL80211_IFTYPE_P2P_GO
;
149 fprintf(stderr
, "invalid interface type %s\n", tpstr
);
153 static int parse_4addr_flag(const char *value
, struct nl_msg
*msg
)
155 if (strcmp(value
, "on") == 0)
156 NLA_PUT_U8(msg
, NL80211_ATTR_4ADDR
, 1);
157 else if (strcmp(value
, "off") == 0)
158 NLA_PUT_U8(msg
, NL80211_ATTR_4ADDR
, 0);
167 static int handle_interface_add(struct nl80211_state
*state
,
169 int argc
, char **argv
,
173 char *mesh_id
= NULL
;
174 enum nl80211_iftype type
;
176 unsigned char mac_addr
[ETH_ALEN
];
186 tpset
= get_if_type(&argc
, &argv
, &type
, true);
192 if (strcmp(argv
[0], "mesh_id") == 0) {
201 } else if (strcmp(argv
[0], "addr") == 0) {
204 if (mac_addr_a2n(mac_addr
, argv
[0])) {
205 fprintf(stderr
, "Invalid MAC address\n");
212 } else if (strcmp(argv
[0], "4addr") == 0) {
215 if (parse_4addr_flag(argv
[0], msg
)) {
216 fprintf(stderr
, "4addr error\n");
221 } else if (strcmp(argv
[0], "flags") == 0) {
224 if (parse_mntr_flags(&argc
, &argv
, msg
)) {
225 fprintf(stderr
, "flags error\n");
236 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, name
);
237 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
239 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
241 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
247 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*] [addr <mac-addr>]",
248 NL80211_CMD_NEW_INTERFACE
, 0, CIB_PHY
, handle_interface_add
,
249 "Add a new virtual interface with the given configuration.\n"
251 "The flags are only used for monitor interfaces, valid flags are:\n"
253 "The mesh_id is used only for mesh mode.");
254 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*] [addr <mac-addr>]",
255 NL80211_CMD_NEW_INTERFACE
, 0, CIB_NETDEV
, handle_interface_add
, NULL
);
257 static int handle_interface_del(struct nl80211_state
*state
,
259 int argc
, char **argv
,
264 TOPLEVEL(del
, NULL
, NL80211_CMD_DEL_INTERFACE
, 0, CIB_NETDEV
, handle_interface_del
,
265 "Remove this virtual interface");
266 HIDDEN(interface
, del
, NULL
, NL80211_CMD_DEL_INTERFACE
, 0, CIB_NETDEV
, handle_interface_del
);
268 static char *channel_type_name(enum nl80211_channel_type channel_type
)
270 switch (channel_type
) {
271 case NL80211_CHAN_NO_HT
:
273 case NL80211_CHAN_HT20
:
275 case NL80211_CHAN_HT40MINUS
:
277 case NL80211_CHAN_HT40PLUS
:
284 char *channel_width_name(enum nl80211_chan_width width
)
287 case NL80211_CHAN_WIDTH_20_NOHT
:
288 return "20 MHz (no HT)";
289 case NL80211_CHAN_WIDTH_20
:
291 case NL80211_CHAN_WIDTH_40
:
293 case NL80211_CHAN_WIDTH_80
:
295 case NL80211_CHAN_WIDTH_80P80
:
297 case NL80211_CHAN_WIDTH_160
:
304 static int print_iface_handler(struct nl_msg
*msg
, void *arg
)
306 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
307 struct nlattr
*tb_msg
[NL80211_ATTR_MAX
+ 1];
308 unsigned int *wiphy
= arg
;
309 const char *indent
= "";
311 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
312 genlmsg_attrlen(gnlh
, 0), NULL
);
314 if (wiphy
&& tb_msg
[NL80211_ATTR_WIPHY
]) {
315 unsigned int thiswiphy
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY
]);
317 if (*wiphy
!= thiswiphy
)
318 printf("phy#%d\n", thiswiphy
);
322 if (tb_msg
[NL80211_ATTR_IFNAME
])
323 printf("%sInterface %s\n", indent
, nla_get_string(tb_msg
[NL80211_ATTR_IFNAME
]));
325 printf("%sUnnamed/non-netdev interface\n", indent
);
326 if (tb_msg
[NL80211_ATTR_IFINDEX
])
327 printf("%s\tifindex %d\n", indent
, nla_get_u32(tb_msg
[NL80211_ATTR_IFINDEX
]));
328 if (tb_msg
[NL80211_ATTR_WDEV
])
329 printf("%s\twdev 0x%llx\n", indent
,
330 (unsigned long long)nla_get_u64(tb_msg
[NL80211_ATTR_WDEV
]));
331 if (tb_msg
[NL80211_ATTR_MAC
]) {
333 mac_addr_n2a(mac_addr
, nla_data(tb_msg
[NL80211_ATTR_MAC
]));
334 printf("%s\taddr %s\n", indent
, mac_addr
);
336 if (tb_msg
[NL80211_ATTR_SSID
]) {
337 printf("%s\tssid ", indent
);
338 print_ssid_escaped(nla_len(tb_msg
[NL80211_ATTR_SSID
]),
339 nla_data(tb_msg
[NL80211_ATTR_SSID
]));
342 if (tb_msg
[NL80211_ATTR_IFTYPE
])
343 printf("%s\ttype %s\n", indent
, iftype_name(nla_get_u32(tb_msg
[NL80211_ATTR_IFTYPE
])));
344 if (!wiphy
&& tb_msg
[NL80211_ATTR_WIPHY
])
345 printf("%s\twiphy %d\n", indent
, nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY
]));
346 if (tb_msg
[NL80211_ATTR_WIPHY_FREQ
]) {
347 uint32_t freq
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY_FREQ
]);
349 printf("%s\tchannel %d (%d MHz)", indent
,
350 ieee80211_frequency_to_channel(freq
), freq
);
352 if (tb_msg
[NL80211_ATTR_CHANNEL_WIDTH
]) {
353 printf(", width: %s",
354 channel_width_name(nla_get_u32(tb_msg
[NL80211_ATTR_CHANNEL_WIDTH
])));
355 if (tb_msg
[NL80211_ATTR_CENTER_FREQ1
])
356 printf(", center1: %d MHz",
357 nla_get_u32(tb_msg
[NL80211_ATTR_CENTER_FREQ1
]));
358 if (tb_msg
[NL80211_ATTR_CENTER_FREQ2
])
359 printf(", center2: %d MHz",
360 nla_get_u32(tb_msg
[NL80211_ATTR_CENTER_FREQ2
]));
361 } else if (tb_msg
[NL80211_ATTR_WIPHY_CHANNEL_TYPE
]) {
362 enum nl80211_channel_type channel_type
;
364 channel_type
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY_CHANNEL_TYPE
]);
365 printf(" %s", channel_type_name(channel_type
));
374 static int handle_interface_info(struct nl80211_state
*state
,
376 int argc
, char **argv
,
379 register_handler(print_iface_handler
, NULL
);
382 TOPLEVEL(info
, NULL
, NL80211_CMD_GET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_info
,
383 "Show information for this interface.");
385 static int handle_interface_set(struct nl80211_state
*state
,
387 int argc
, char **argv
,
393 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_MONITOR
);
395 switch (parse_mntr_flags(&argc
, &argv
, msg
)) {
399 fprintf(stderr
, "failed to allocate flags\n");
402 fprintf(stderr
, "unknown flag %s\n", *argv
);
410 COMMAND(set
, monitor
, "<flag>*",
411 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_set
,
412 "Set monitor flags. Valid flags are:\n"
415 static int handle_interface_meshid(struct nl80211_state
*state
,
417 int argc
, char **argv
,
420 char *mesh_id
= NULL
;
427 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
433 COMMAND(set
, meshid
, "<meshid>",
434 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_meshid
, NULL
);
436 static unsigned int dev_dump_wiphy
;
438 static int handle_dev_dump(struct nl80211_state
*state
,
440 int argc
, char **argv
,
444 register_handler(print_iface_handler
, &dev_dump_wiphy
);
447 TOPLEVEL(dev
, NULL
, NL80211_CMD_GET_INTERFACE
, NLM_F_DUMP
, CIB_NONE
, handle_dev_dump
,
448 "List all network interfaces for wireless hardware.");
450 static int handle_interface_type(struct nl80211_state
*state
,
452 int argc
, char **argv
,
455 enum nl80211_iftype type
;
458 tpset
= get_if_type(&argc
, &argv
, &type
, false);
465 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
471 COMMAND(set
, type
, "<type>",
472 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_type
,
473 "Set interface type/mode.\n"
476 static int handle_interface_4addr(struct nl80211_state
*state
,
478 int argc
, char **argv
,
483 return parse_4addr_flag(argv
[0], msg
);
485 COMMAND(set
, 4addr
, "<on|off>",
486 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_4addr
,
487 "Set interface 4addr (WDS) mode.");
489 static int handle_interface_noack_map(struct nl80211_state
*state
,
491 int argc
, char **argv
,
500 noack_map
= strtoul(argv
[0], &end
, 16);
504 NLA_PUT_U16(msg
, NL80211_ATTR_NOACK_MAP
, noack_map
);
511 COMMAND(set
, noack_map
, "<map>",
512 NL80211_CMD_SET_NOACK_MAP
, 0, CIB_NETDEV
, handle_interface_noack_map
,
513 "Set the NoAck map for the TIDs. (0x0009 = BE, 0x0006 = BK, 0x0030 = VI, 0x00C0 = VO)");
516 static int handle_interface_wds_peer(struct nl80211_state
*state
,
518 int argc
, char **argv
,
521 unsigned char mac_addr
[ETH_ALEN
];
526 if (mac_addr_a2n(mac_addr
, argv
[0])) {
527 fprintf(stderr
, "Invalid MAC address\n");
537 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
543 COMMAND(set
, peer
, "<MAC address>",
544 NL80211_CMD_SET_WDS_PEER
, 0, CIB_NETDEV
, handle_interface_wds_peer
,
545 "Set interface WDS peer.");
547 static int set_mcast_rate(struct nl80211_state
*state
,
549 int argc
, char **argv
,
556 printf("Invalid parameters!\n");
560 rate
= strtod(argv
[0], &end
);
564 NLA_PUT_U32(msg
, NL80211_ATTR_MCAST_RATE
, (int)(rate
* 10));
571 COMMAND(set
, mcast_rate
, "<rate in Mbps>",
572 NL80211_CMD_SET_MCAST_RATE
, 0, CIB_NETDEV
, set_mcast_rate
,
573 "Set the multicast bitrate.");