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 static char *mntr_flags
[NL80211_MNTR_FLAG_MAX
+ 1] = {
24 static int parse_mntr_flags(int *_argc
, char ***_argv
,
29 enum nl80211_mntr_flags flag
;
33 flags
= nlmsg_alloc();
39 for (flag
= __NL80211_MNTR_FLAG_INVALID
;
40 flag
<= NL80211_MNTR_FLAG_MAX
; flag
++) {
41 if (strcmp(*argv
, mntr_flags
[flag
]) == 0) {
44 * This shouldn't be adding "flag" if that is
45 * zero, but due to a problem in the kernel's
46 * nl80211 code (using NLA_NESTED policy) it
47 * will reject an empty nested attribute but
48 * not one that contains an invalid attribute
50 NLA_PUT_FLAG(flags
, flag
);
62 nla_put_nested(msg
, NL80211_ATTR_MNTR_FLAGS
, flags
);
74 /* return 0 if ok, internal error otherwise */
75 static int get_if_type(int *argc
, char ***argv
, enum nl80211_iftype
*type
,
80 if (*argc
< 1 + !!need_type
)
83 if (need_type
&& strcmp((*argv
)[0], "type"))
86 tpstr
= (*argv
)[!!need_type
];
87 *argc
-= 1 + !!need_type
;
88 *argv
+= 1 + !!need_type
;
90 if (strcmp(tpstr
, "adhoc") == 0 ||
91 strcmp(tpstr
, "ibss") == 0) {
92 *type
= NL80211_IFTYPE_ADHOC
;
94 } else if (strcmp(tpstr
, "monitor") == 0) {
95 *type
= NL80211_IFTYPE_MONITOR
;
97 } else if (strcmp(tpstr
, "master") == 0) {
98 *type
= NL80211_IFTYPE_UNSPECIFIED
;
99 fprintf(stderr
, "See http://wireless.kernel.org/RTFM-AP.\n");
101 } else if (strcmp(tpstr
, "ap") == 0) {
102 *type
= NL80211_IFTYPE_UNSPECIFIED
;
103 fprintf(stderr
, "See http://wireless.kernel.org/RTFM-AP.\n");
105 } else if (strcmp(tpstr
, "__ap") == 0) {
106 *type
= NL80211_IFTYPE_AP
;
108 } else if (strcmp(tpstr
, "__ap_vlan") == 0) {
109 *type
= NL80211_IFTYPE_AP_VLAN
;
111 } else if (strcmp(tpstr
, "wds") == 0) {
112 *type
= NL80211_IFTYPE_WDS
;
114 } else if (strcmp(tpstr
, "managed") == 0 ||
115 strcmp(tpstr
, "mgd") == 0 ||
116 strcmp(tpstr
, "station") == 0) {
117 *type
= NL80211_IFTYPE_STATION
;
119 } else if (strcmp(tpstr
, "mp") == 0 ||
120 strcmp(tpstr
, "mesh") == 0) {
121 *type
= NL80211_IFTYPE_MESH_POINT
;
125 fprintf(stderr
, "invalid interface type %s\n", tpstr
);
129 static int handle_interface_add(struct nl80211_state
*state
,
132 int argc
, char **argv
)
135 char *mesh_id
= NULL
;
136 enum nl80211_iftype type
;
146 tpset
= get_if_type(&argc
, &argv
, &type
, true);
151 if (strcmp(argv
[0], "mesh_id") == 0) {
160 } else if (strcmp(argv
[0], "flags") == 0) {
163 if (parse_mntr_flags(&argc
, &argv
, msg
)) {
164 fprintf(stderr
, "flags error\n");
175 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];
203 unsigned int *wiphy
= arg
;
204 const char *indent
= "";
206 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
207 genlmsg_attrlen(gnlh
, 0), NULL
);
209 if (wiphy
&& tb_msg
[NL80211_ATTR_WIPHY
]) {
210 unsigned int thiswiphy
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY
]);
212 if (*wiphy
!= thiswiphy
)
213 printf("phy#%d\n", thiswiphy
);
217 if (tb_msg
[NL80211_ATTR_IFNAME
])
218 printf("%sInterface %s\n", indent
, nla_get_string(tb_msg
[NL80211_ATTR_IFNAME
]));
219 if (tb_msg
[NL80211_ATTR_IFINDEX
])
220 printf("%s\tifindex %d\n", indent
, nla_get_u32(tb_msg
[NL80211_ATTR_IFINDEX
]));
221 if (tb_msg
[NL80211_ATTR_IFTYPE
])
222 printf("%s\ttype %s\n", indent
, iftype_name(nla_get_u32(tb_msg
[NL80211_ATTR_IFTYPE
])));
227 static int handle_interface_info(struct nl80211_state
*state
,
230 int argc
, char **argv
)
232 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_iface_handler
, NULL
);
235 TOPLEVEL(info
, NULL
, NL80211_CMD_GET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_info
);
237 static int handle_interface_set(struct nl80211_state
*state
,
240 int argc
, char **argv
)
245 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_MONITOR
);
247 switch (parse_mntr_flags(&argc
, &argv
, msg
)) {
251 fprintf(stderr
, "failed to allocate flags\n");
254 fprintf(stderr
, "unknown flag %s\n", *argv
);
262 COMMAND(set
, monitor
, "<flag> [...]",
263 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_set
);
265 static int handle_interface_meshid(struct nl80211_state
*state
,
268 int argc
, char **argv
)
270 char *mesh_id
= NULL
;
277 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
283 COMMAND(set
, meshid
, "<meshid>",
284 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_meshid
);
286 static unsigned int dev_dump_wiphy
;
288 static int handle_dev_dump(struct nl80211_state
*state
,
291 int argc
, char **argv
)
294 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_iface_handler
, &dev_dump_wiphy
);
297 TOPLEVEL(dev
, NULL
, NL80211_CMD_GET_INTERFACE
, NLM_F_DUMP
, CIB_NONE
, handle_dev_dump
);
299 static int handle_interface_type(struct nl80211_state
*state
,
302 int argc
, char **argv
)
304 enum nl80211_iftype type
;
307 tpset
= get_if_type(&argc
, &argv
, &type
, false);
314 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
320 COMMAND(set
, type
, "<type>",
321 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_type
);