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"
23 static char *mntr_flags
[NL80211_MNTR_FLAG_MAX
+ 1] = {
32 static int parse_mntr_flags(int *_argc
, char ***_argv
,
37 enum nl80211_mntr_flags flag
;
41 flags
= nlmsg_alloc();
47 for (flag
= __NL80211_MNTR_FLAG_INVALID
;
48 flag
<= NL80211_MNTR_FLAG_MAX
; flag
++) {
49 if (strcmp(*argv
, mntr_flags
[flag
]) == 0) {
52 * This shouldn't be adding "flag" if that is
53 * zero, but due to a problem in the kernel's
54 * nl80211 code (using NLA_NESTED policy) it
55 * will reject an empty nested attribute but
56 * not one that contains an invalid attribute
58 NLA_PUT_FLAG(flags
, flag
);
70 nla_put_nested(msg
, NL80211_ATTR_MNTR_FLAGS
, flags
);
83 #define IFACE_TYPES "Valid interface types are: managed, ibss, monitor, mesh, wds."
85 /* return 0 if ok, internal error otherwise */
86 static int get_if_type(int *argc
, char ***argv
, enum nl80211_iftype
*type
,
91 if (*argc
< 1 + !!need_type
)
94 if (need_type
&& strcmp((*argv
)[0], "type"))
97 tpstr
= (*argv
)[!!need_type
];
98 *argc
-= 1 + !!need_type
;
99 *argv
+= 1 + !!need_type
;
101 if (strcmp(tpstr
, "adhoc") == 0 ||
102 strcmp(tpstr
, "ibss") == 0) {
103 *type
= NL80211_IFTYPE_ADHOC
;
105 } else if (strcmp(tpstr
, "monitor") == 0) {
106 *type
= NL80211_IFTYPE_MONITOR
;
108 } else if (strcmp(tpstr
, "master") == 0 ||
109 strcmp(tpstr
, "ap") == 0) {
110 *type
= NL80211_IFTYPE_UNSPECIFIED
;
111 fprintf(stderr
, "You need to run a management daemon, e.g. hostapd,\n");
112 fprintf(stderr
, "see http://wireless.kernel.org/en/users/Documentation/hostapd\n");
113 fprintf(stderr
, "for more information on how to do that.\n");
115 } else if (strcmp(tpstr
, "__ap") == 0) {
116 *type
= NL80211_IFTYPE_AP
;
118 } else if (strcmp(tpstr
, "__ap_vlan") == 0) {
119 *type
= NL80211_IFTYPE_AP_VLAN
;
121 } else if (strcmp(tpstr
, "wds") == 0) {
122 *type
= NL80211_IFTYPE_WDS
;
124 } else if (strcmp(tpstr
, "managed") == 0 ||
125 strcmp(tpstr
, "mgd") == 0 ||
126 strcmp(tpstr
, "station") == 0) {
127 *type
= NL80211_IFTYPE_STATION
;
129 } else if (strcmp(tpstr
, "mp") == 0 ||
130 strcmp(tpstr
, "mesh") == 0) {
131 *type
= NL80211_IFTYPE_MESH_POINT
;
133 } else if (strcmp(tpstr
, "__p2pcl") == 0) {
134 *type
= NL80211_IFTYPE_P2P_CLIENT
;
136 } else if (strcmp(tpstr
, "__p2pdev") == 0) {
137 *type
= NL80211_IFTYPE_P2P_DEVICE
;
139 } else if (strcmp(tpstr
, "__p2pgo") == 0) {
140 *type
= NL80211_IFTYPE_P2P_GO
;
144 fprintf(stderr
, "invalid interface type %s\n", tpstr
);
148 static int parse_4addr_flag(const char *value
, struct nl_msg
*msg
)
150 if (strcmp(value
, "on") == 0)
151 NLA_PUT_U8(msg
, NL80211_ATTR_4ADDR
, 1);
152 else if (strcmp(value
, "off") == 0)
153 NLA_PUT_U8(msg
, NL80211_ATTR_4ADDR
, 0);
162 static int handle_interface_add(struct nl80211_state
*state
,
165 int argc
, char **argv
,
169 char *mesh_id
= NULL
;
170 enum nl80211_iftype type
;
180 tpset
= get_if_type(&argc
, &argv
, &type
, true);
185 if (strcmp(argv
[0], "mesh_id") == 0) {
194 } else if (strcmp(argv
[0], "4addr") == 0) {
197 if (parse_4addr_flag(argv
[0], msg
)) {
198 fprintf(stderr
, "4addr error\n");
203 } else if (strcmp(argv
[0], "flags") == 0) {
206 if (parse_mntr_flags(&argc
, &argv
, msg
)) {
207 fprintf(stderr
, "flags error\n");
218 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, name
);
219 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
221 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
227 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*]",
228 NL80211_CMD_NEW_INTERFACE
, 0, CIB_PHY
, handle_interface_add
,
229 "Add a new virtual interface with the given configuration.\n"
231 "The flags are only used for monitor interfaces, valid flags are:\n"
233 "The mesh_id is used only for mesh mode.");
234 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*]",
235 NL80211_CMD_NEW_INTERFACE
, 0, CIB_NETDEV
, handle_interface_add
, NULL
);
237 static int handle_interface_del(struct nl80211_state
*state
,
240 int argc
, char **argv
,
245 TOPLEVEL(del
, NULL
, NL80211_CMD_DEL_INTERFACE
, 0, CIB_NETDEV
, handle_interface_del
,
246 "Remove this virtual interface");
247 HIDDEN(interface
, del
, NULL
, NL80211_CMD_DEL_INTERFACE
, 0, CIB_NETDEV
, handle_interface_del
);
249 static char *channel_type_name(enum nl80211_channel_type channel_type
)
251 switch (channel_type
) {
252 case NL80211_CHAN_NO_HT
:
254 case NL80211_CHAN_HT20
:
256 case NL80211_CHAN_HT40MINUS
:
258 case NL80211_CHAN_HT40PLUS
:
265 static int print_iface_handler(struct nl_msg
*msg
, void *arg
)
267 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
268 struct nlattr
*tb_msg
[NL80211_ATTR_MAX
+ 1];
269 unsigned int *wiphy
= arg
;
270 const char *indent
= "";
272 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
273 genlmsg_attrlen(gnlh
, 0), NULL
);
275 if (wiphy
&& tb_msg
[NL80211_ATTR_WIPHY
]) {
276 unsigned int thiswiphy
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY
]);
278 if (*wiphy
!= thiswiphy
)
279 printf("phy#%d\n", thiswiphy
);
283 if (tb_msg
[NL80211_ATTR_IFNAME
])
284 printf("%sInterface %s\n", indent
, nla_get_string(tb_msg
[NL80211_ATTR_IFNAME
]));
286 printf("%sUnnamed/non-netdev interface\n", indent
);
287 if (tb_msg
[NL80211_ATTR_IFINDEX
])
288 printf("%s\tifindex %d\n", indent
, nla_get_u32(tb_msg
[NL80211_ATTR_IFINDEX
]));
289 if (tb_msg
[NL80211_ATTR_WDEV
])
290 printf("%s\twdev 0x%llx\n", indent
,
291 (unsigned long long)nla_get_u64(tb_msg
[NL80211_ATTR_WDEV
]));
292 if (tb_msg
[NL80211_ATTR_MAC
]) {
294 mac_addr_n2a(mac_addr
, nla_data(tb_msg
[NL80211_ATTR_MAC
]));
295 printf("%s\taddr %s\n", indent
, mac_addr
);
297 if (tb_msg
[NL80211_ATTR_SSID
]) {
298 printf("%s\tssid ", indent
);
299 print_ssid_escaped(nla_len(tb_msg
[NL80211_ATTR_SSID
]),
300 nla_data(tb_msg
[NL80211_ATTR_SSID
]));
303 if (tb_msg
[NL80211_ATTR_IFTYPE
])
304 printf("%s\ttype %s\n", indent
, iftype_name(nla_get_u32(tb_msg
[NL80211_ATTR_IFTYPE
])));
305 if (!wiphy
&& tb_msg
[NL80211_ATTR_WIPHY
])
306 printf("%s\twiphy %d\n", indent
, nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY
]));
307 if (tb_msg
[NL80211_ATTR_WIPHY_FREQ
]) {
308 uint32_t freq
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY_FREQ
]);
309 enum nl80211_channel_type channel_type
= NL80211_CHAN_NO_HT
;
311 if (tb_msg
[NL80211_ATTR_WIPHY_CHANNEL_TYPE
])
312 channel_type
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY_CHANNEL_TYPE
]);
314 printf("%s\tchannel %d (%d MHz) %s\n", indent
,
315 ieee80211_frequency_to_channel(freq
), freq
,
316 channel_type_name(channel_type
));
322 static int handle_interface_info(struct nl80211_state
*state
,
325 int argc
, char **argv
,
328 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_iface_handler
, NULL
);
331 TOPLEVEL(info
, NULL
, NL80211_CMD_GET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_info
,
332 "Show information for this interface.");
334 static int handle_interface_set(struct nl80211_state
*state
,
337 int argc
, char **argv
,
343 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_MONITOR
);
345 switch (parse_mntr_flags(&argc
, &argv
, msg
)) {
349 fprintf(stderr
, "failed to allocate flags\n");
352 fprintf(stderr
, "unknown flag %s\n", *argv
);
360 COMMAND(set
, monitor
, "<flag>*",
361 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_set
,
362 "Set monitor flags. Valid flags are:\n"
365 static int handle_interface_meshid(struct nl80211_state
*state
,
368 int argc
, char **argv
,
371 char *mesh_id
= NULL
;
378 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
384 COMMAND(set
, meshid
, "<meshid>",
385 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_meshid
, NULL
);
387 static unsigned int dev_dump_wiphy
;
389 static int handle_dev_dump(struct nl80211_state
*state
,
392 int argc
, char **argv
,
396 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_iface_handler
, &dev_dump_wiphy
);
399 TOPLEVEL(dev
, NULL
, NL80211_CMD_GET_INTERFACE
, NLM_F_DUMP
, CIB_NONE
, handle_dev_dump
,
400 "List all network interfaces for wireless hardware.");
402 static int handle_interface_type(struct nl80211_state
*state
,
405 int argc
, char **argv
,
408 enum nl80211_iftype type
;
411 tpset
= get_if_type(&argc
, &argv
, &type
, false);
418 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
424 COMMAND(set
, type
, "<type>",
425 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_type
,
426 "Set interface type/mode.\n"
429 static int handle_interface_4addr(struct nl80211_state
*state
,
432 int argc
, char **argv
,
437 return parse_4addr_flag(argv
[0], msg
);
439 COMMAND(set
, 4addr
, "<on|off>",
440 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_4addr
,
441 "Set interface 4addr (WDS) mode.");
443 static int handle_interface_noack_map(struct nl80211_state
*state
,
446 int argc
, char **argv
,
455 noack_map
= strtoul(argv
[0], &end
, 16);
459 NLA_PUT_U16(msg
, NL80211_ATTR_NOACK_MAP
, noack_map
);
466 COMMAND(set
, noack_map
, "<map>",
467 NL80211_CMD_SET_NOACK_MAP
, 0, CIB_NETDEV
, handle_interface_noack_map
,
468 "Set the NoAck map for the TIDs. (0x0009 = BE, 0x0006 = BK, 0x0030 = VI, 0x00C0 = VO)");
471 static int handle_interface_wds_peer(struct nl80211_state
*state
,
474 int argc
, char **argv
,
477 unsigned char mac_addr
[ETH_ALEN
];
482 if (mac_addr_a2n(mac_addr
, argv
[0])) {
483 fprintf(stderr
, "Invalid MAC address\n");
493 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
499 COMMAND(set
, peer
, "<MAC address>",
500 NL80211_CMD_SET_WDS_PEER
, 0, CIB_NETDEV
, handle_interface_wds_peer
,
501 "Set interface WDS peer.");