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 static char *mntr_flags
[NL80211_MNTR_FLAG_MAX
+ 1] = {
23 static int parse_mntr_flags(int *_argc
, char ***_argv
,
28 enum nl80211_mntr_flags flag
;
32 flags
= nlmsg_alloc();
38 for (flag
= __NL80211_MNTR_FLAG_INVALID
;
39 flag
<= NL80211_MNTR_FLAG_MAX
; flag
++) {
40 if (strcmp(*argv
, mntr_flags
[flag
]) == 0) {
43 * This shouldn't be adding "flag" if that is
44 * zero, but due to a problem in the kernel's
45 * nl80211 code (using NLA_NESTED policy) it
46 * will reject an empty nested attribute but
47 * not one that contains an invalid attribute
49 NLA_PUT_FLAG(flags
, flag
);
61 nla_put_nested(msg
, NL80211_ATTR_MNTR_FLAGS
, flags
);
73 /* return 0 if not found, 1 if ok, -1 on error */
74 static int get_if_type(int *argc
, char ***argv
, enum nl80211_iftype
*type
)
81 if (strcmp((*argv
)[0], "type"))
88 if (strcmp(tpstr
, "adhoc") == 0 ||
89 strcmp(tpstr
, "ibss") == 0) {
90 *type
= NL80211_IFTYPE_ADHOC
;
92 } else if (strcmp(tpstr
, "monitor") == 0) {
93 *type
= NL80211_IFTYPE_MONITOR
;
95 } else if (strcmp(tpstr
, "master") == 0) {
96 *type
= NL80211_IFTYPE_UNSPECIFIED
;
97 fprintf(stderr
, "See http://wireless.kernel.org/RTFM-AP.\n");
99 } else if (strcmp(tpstr
, "ap") == 0) {
100 *type
= NL80211_IFTYPE_UNSPECIFIED
;
101 fprintf(stderr
, "See http://wireless.kernel.org/RTFM-AP.\n");
103 } else if (strcmp(tpstr
, "__ap") == 0) {
104 *type
= NL80211_IFTYPE_AP
;
106 } else if (strcmp(tpstr
, "__ap_vlan") == 0) {
107 *type
= NL80211_IFTYPE_AP_VLAN
;
109 } else if (strcmp(tpstr
, "wds") == 0) {
110 *type
= NL80211_IFTYPE_WDS
;
112 } else if (strcmp(tpstr
, "managed") == 0 ||
113 strcmp(tpstr
, "mgd") == 0 ||
114 strcmp(tpstr
, "station") == 0) {
115 *type
= NL80211_IFTYPE_STATION
;
117 } else if (strcmp(tpstr
, "mp") == 0 ||
118 strcmp(tpstr
, "mesh") == 0) {
119 *type
= NL80211_IFTYPE_MESH_POINT
;
124 fprintf(stderr
, "invalid interface type %s\n", tpstr
);
128 static int handle_interface_add(struct nl80211_state
*state
,
131 int argc
, char **argv
)
134 char *mesh_id
= NULL
;
135 enum nl80211_iftype type
;
145 tpset
= get_if_type(&argc
, &argv
, &type
);
150 if (strcmp(argv
[0], "mesh_id") == 0) {
159 } else if (strcmp(argv
[0], "flags") == 0) {
162 if (parse_mntr_flags(&argc
, &argv
, msg
)) {
163 fprintf(stderr
, "flags error\n");
174 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, name
);
176 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
178 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
184 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>] [flags ...]",
185 NL80211_CMD_NEW_INTERFACE
, 0, CIB_PHY
, handle_interface_add
);
186 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>] [flags ...]",
187 NL80211_CMD_NEW_INTERFACE
, 0, CIB_NETDEV
, handle_interface_add
);
189 static int handle_interface_del(struct nl80211_state
*state
,
192 int argc
, char **argv
)
196 TOPLEVEL(del
, NULL
, NL80211_CMD_DEL_INTERFACE
, 0, CIB_NETDEV
, handle_interface_del
);
197 HIDDEN(interface
, del
, NULL
, NL80211_CMD_DEL_INTERFACE
, 0, CIB_NETDEV
, handle_interface_del
);
199 static int print_iface_handler(struct nl_msg
*msg
, void *arg
)
201 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
202 struct nlattr
*tb_msg
[NL80211_ATTR_MAX
+ 1];
204 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
205 genlmsg_attrlen(gnlh
, 0), NULL
);
207 if (tb_msg
[NL80211_ATTR_IFNAME
])
208 printf("Interface %s\n", nla_get_string(tb_msg
[NL80211_ATTR_IFNAME
]));
209 if (tb_msg
[NL80211_ATTR_IFINDEX
])
210 printf("\tifindex %d\n", nla_get_u32(tb_msg
[NL80211_ATTR_IFINDEX
]));
211 if (tb_msg
[NL80211_ATTR_IFTYPE
])
212 printf("\ttype %s\n", iftype_name(nla_get_u32(tb_msg
[NL80211_ATTR_IFTYPE
])));
217 static int handle_interface_info(struct nl80211_state
*state
,
220 int argc
, char **argv
)
222 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_iface_handler
, NULL
);
225 TOPLEVEL(info
, NULL
, NL80211_CMD_GET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_info
);
227 static int handle_interface_set(struct nl80211_state
*state
,
230 int argc
, char **argv
)
235 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_MONITOR
);
237 switch (parse_mntr_flags(&argc
, &argv
, msg
)) {
241 fprintf(stderr
, "failed to allocate flags\n");
244 fprintf(stderr
, "unknown flag %s\n", *argv
);
252 COMMAND(set
, monitor
, "<flag> [...]",
253 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_set
);
255 static int handle_interface_meshid(struct nl80211_state
*state
,
258 int argc
, char **argv
)
260 char *mesh_id
= NULL
;
267 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
273 COMMAND(set
, meshid
, "<meshid>",
274 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_meshid
);