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"
21 static char *mntr_flags
[NL80211_MNTR_FLAG_MAX
+ 1] = {
30 static int parse_mntr_flags(int *_argc
, char ***_argv
,
35 enum nl80211_mntr_flags flag
;
39 flags
= nlmsg_alloc();
45 for (flag
= __NL80211_MNTR_FLAG_INVALID
;
46 flag
<= NL80211_MNTR_FLAG_MAX
; flag
++) {
47 if (strcmp(*argv
, mntr_flags
[flag
]) == 0) {
50 * This shouldn't be adding "flag" if that is
51 * zero, but due to a problem in the kernel's
52 * nl80211 code (using NLA_NESTED policy) it
53 * will reject an empty nested attribute but
54 * not one that contains an invalid attribute
56 NLA_PUT_FLAG(flags
, flag
);
68 nla_put_nested(msg
, NL80211_ATTR_MNTR_FLAGS
, flags
);
81 #define IFACE_TYPES "Valid interface types are: managed, ibss, monitor, mesh, wds."
83 /* return 0 if ok, internal error otherwise */
84 static int get_if_type(int *argc
, char ***argv
, enum nl80211_iftype
*type
,
89 if (*argc
< 1 + !!need_type
)
92 if (need_type
&& strcmp((*argv
)[0], "type"))
95 tpstr
= (*argv
)[!!need_type
];
96 *argc
-= 1 + !!need_type
;
97 *argv
+= 1 + !!need_type
;
99 if (strcmp(tpstr
, "adhoc") == 0 ||
100 strcmp(tpstr
, "ibss") == 0) {
101 *type
= NL80211_IFTYPE_ADHOC
;
103 } else if (strcmp(tpstr
, "monitor") == 0) {
104 *type
= NL80211_IFTYPE_MONITOR
;
106 } else if (strcmp(tpstr
, "master") == 0) {
107 *type
= NL80211_IFTYPE_UNSPECIFIED
;
108 fprintf(stderr
, "See http://wireless.kernel.org/RTFM-AP.\n");
110 } else if (strcmp(tpstr
, "ap") == 0) {
111 *type
= NL80211_IFTYPE_UNSPECIFIED
;
112 fprintf(stderr
, "See http://wireless.kernel.org/RTFM-AP.\n");
114 } else if (strcmp(tpstr
, "__ap") == 0) {
115 *type
= NL80211_IFTYPE_AP
;
117 } else if (strcmp(tpstr
, "__ap_vlan") == 0) {
118 *type
= NL80211_IFTYPE_AP_VLAN
;
120 } else if (strcmp(tpstr
, "wds") == 0) {
121 *type
= NL80211_IFTYPE_WDS
;
123 } else if (strcmp(tpstr
, "managed") == 0 ||
124 strcmp(tpstr
, "mgd") == 0 ||
125 strcmp(tpstr
, "station") == 0) {
126 *type
= NL80211_IFTYPE_STATION
;
128 } else if (strcmp(tpstr
, "mp") == 0 ||
129 strcmp(tpstr
, "mesh") == 0) {
130 *type
= NL80211_IFTYPE_MESH_POINT
;
134 fprintf(stderr
, "invalid interface type %s\n", tpstr
);
138 static int handle_interface_add(struct nl80211_state
*state
,
141 int argc
, char **argv
)
144 char *mesh_id
= NULL
;
145 enum nl80211_iftype type
;
155 tpset
= get_if_type(&argc
, &argv
, &type
, true);
160 if (strcmp(argv
[0], "mesh_id") == 0) {
169 } else if (strcmp(argv
[0], "flags") == 0) {
172 if (parse_mntr_flags(&argc
, &argv
, msg
)) {
173 fprintf(stderr
, "flags error\n");
184 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, name
);
185 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
187 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
193 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>] [flags <flag>*]",
194 NL80211_CMD_NEW_INTERFACE
, 0, CIB_PHY
, handle_interface_add
,
195 "Add a new virtual interface with the given configuration.\n"
197 "The flags are only used for monitor interfaces, valid flags are:\n"
199 "The mesh_id is used only for mesh mode.");
200 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>] [flags <flag>*]",
201 NL80211_CMD_NEW_INTERFACE
, 0, CIB_NETDEV
, handle_interface_add
, NULL
);
203 static int handle_interface_del(struct nl80211_state
*state
,
206 int argc
, char **argv
)
210 TOPLEVEL(del
, NULL
, NL80211_CMD_DEL_INTERFACE
, 0, CIB_NETDEV
, handle_interface_del
,
211 "Remove this virtual interface");
212 HIDDEN(interface
, del
, NULL
, NL80211_CMD_DEL_INTERFACE
, 0, CIB_NETDEV
, handle_interface_del
);
214 static int print_iface_handler(struct nl_msg
*msg
, void *arg
)
216 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
217 struct nlattr
*tb_msg
[NL80211_ATTR_MAX
+ 1];
218 unsigned int *wiphy
= arg
;
219 const char *indent
= "";
221 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
222 genlmsg_attrlen(gnlh
, 0), NULL
);
224 if (wiphy
&& tb_msg
[NL80211_ATTR_WIPHY
]) {
225 unsigned int thiswiphy
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY
]);
227 if (*wiphy
!= thiswiphy
)
228 printf("phy#%d\n", thiswiphy
);
232 if (tb_msg
[NL80211_ATTR_IFNAME
])
233 printf("%sInterface %s\n", indent
, nla_get_string(tb_msg
[NL80211_ATTR_IFNAME
]));
234 if (tb_msg
[NL80211_ATTR_IFINDEX
])
235 printf("%s\tifindex %d\n", indent
, nla_get_u32(tb_msg
[NL80211_ATTR_IFINDEX
]));
236 if (tb_msg
[NL80211_ATTR_IFTYPE
])
237 printf("%s\ttype %s\n", indent
, iftype_name(nla_get_u32(tb_msg
[NL80211_ATTR_IFTYPE
])));
242 static int handle_interface_info(struct nl80211_state
*state
,
245 int argc
, char **argv
)
247 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_iface_handler
, NULL
);
250 TOPLEVEL(info
, NULL
, NL80211_CMD_GET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_info
,
251 "Show information for this interface.");
253 static int handle_interface_set(struct nl80211_state
*state
,
256 int argc
, char **argv
)
261 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_MONITOR
);
263 switch (parse_mntr_flags(&argc
, &argv
, msg
)) {
267 fprintf(stderr
, "failed to allocate flags\n");
270 fprintf(stderr
, "unknown flag %s\n", *argv
);
278 COMMAND(set
, monitor
, "<flag>*",
279 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_set
,
280 "Set monitor flags. Valid flags are:\n"
283 static int handle_interface_meshid(struct nl80211_state
*state
,
286 int argc
, char **argv
)
288 char *mesh_id
= NULL
;
295 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
301 COMMAND(set
, meshid
, "<meshid>",
302 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_meshid
, NULL
);
304 static unsigned int dev_dump_wiphy
;
306 static int handle_dev_dump(struct nl80211_state
*state
,
309 int argc
, char **argv
)
312 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_iface_handler
, &dev_dump_wiphy
);
315 TOPLEVEL(dev
, NULL
, NL80211_CMD_GET_INTERFACE
, NLM_F_DUMP
, CIB_NONE
, handle_dev_dump
,
316 "List all network interfaces for wireless hardware.");
318 static int handle_interface_type(struct nl80211_state
*state
,
321 int argc
, char **argv
)
323 enum nl80211_iftype type
;
326 tpset
= get_if_type(&argc
, &argv
, &type
, false);
333 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
339 COMMAND(set
, type
, "<type>",
340 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_type
,
341 "Set interface type/mode.\n"