]> git.ipfire.org Git - thirdparty/iw.git/blame - interface.c
event parsing: remove PARSE_BEACON_CHAN
[thirdparty/iw.git] / interface.c
CommitLineData
2ef1be68 1#include <net/if.h>
45c7212c 2#include <errno.h>
d5ac8ad3 3#include <string.h>
d17fe27e 4#include <stdbool.h>
2ef1be68 5
45c7212c
JB
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>
45c7212c 11
f408e01b 12#include "nl80211.h"
45c7212c
JB
13#include "iw.h"
14
1ae3d621
JB
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"
20
4698bfc2
JB
21SECTION(interface);
22
cd293761 23static char *mntr_flags[NL80211_MNTR_FLAG_MAX + 1] = {
dd65496b 24 "none",
cd293761
JB
25 "fcsfail",
26 "plcpfail",
27 "control",
28 "otherbss",
29 "cook",
30};
31
dd65496b
JB
32static int parse_mntr_flags(int *_argc, char ***_argv,
33 struct nl_msg *msg)
34{
35 struct nl_msg *flags;
36 int err = -ENOBUFS;
37 enum nl80211_mntr_flags flag;
38 int argc = *_argc;
39 char **argv = *_argv;
40
41 flags = nlmsg_alloc();
42 if (!flags)
43 return -ENOMEM;
44
45 while (argc) {
46 int ok = 0;
47 for (flag = __NL80211_MNTR_FLAG_INVALID;
e14cc99e 48 flag <= NL80211_MNTR_FLAG_MAX; flag++) {
dd65496b
JB
49 if (strcmp(*argv, mntr_flags[flag]) == 0) {
50 ok = 1;
4a3bf753
JB
51 /*
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
57 */
dd65496b
JB
58 NLA_PUT_FLAG(flags, flag);
59 break;
60 }
61 }
62 if (!ok) {
63 err = -EINVAL;
64 goto out;
65 }
66 argc--;
67 argv++;
68 }
69
70 nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
71 err = 0;
72 nla_put_failure:
73 out:
74 nlmsg_free(flags);
75
76 *_argc = argc;
77 *_argv = argv;
78
79 return err;
80}
81
1ae3d621
JB
82/* for help */
83#define IFACE_TYPES "Valid interface types are: managed, ibss, monitor, mesh, wds."
84
d17fe27e
JB
85/* return 0 if ok, internal error otherwise */
86static int get_if_type(int *argc, char ***argv, enum nl80211_iftype *type,
87 bool need_type)
45c7212c
JB
88{
89 char *tpstr;
90
d17fe27e
JB
91 if (*argc < 1 + !!need_type)
92 return 1;
45c7212c 93
d17fe27e
JB
94 if (need_type && strcmp((*argv)[0], "type"))
95 return 1;
45c7212c 96
d17fe27e
JB
97 tpstr = (*argv)[!!need_type];
98 *argc -= 1 + !!need_type;
99 *argv += 1 + !!need_type;
45c7212c
JB
100
101 if (strcmp(tpstr, "adhoc") == 0 ||
102 strcmp(tpstr, "ibss") == 0) {
103 *type = NL80211_IFTYPE_ADHOC;
d17fe27e 104 return 0;
45c7212c
JB
105 } else if (strcmp(tpstr, "monitor") == 0) {
106 *type = NL80211_IFTYPE_MONITOR;
d17fe27e 107 return 0;
e08b5488
JB
108 } else if (strcmp(tpstr, "master") == 0 ||
109 strcmp(tpstr, "ap") == 0) {
c1d44a6c 110 *type = NL80211_IFTYPE_UNSPECIFIED;
e08b5488
JB
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");
c1d44a6c 114 return 2;
4d3a72da 115 } else if (strcmp(tpstr, "__ap") == 0) {
45c7212c 116 *type = NL80211_IFTYPE_AP;
d17fe27e 117 return 0;
4d3a72da 118 } else if (strcmp(tpstr, "__ap_vlan") == 0) {
45c7212c 119 *type = NL80211_IFTYPE_AP_VLAN;
d17fe27e 120 return 0;
45c7212c
JB
121 } else if (strcmp(tpstr, "wds") == 0) {
122 *type = NL80211_IFTYPE_WDS;
d17fe27e 123 return 0;
a65df165
JB
124 } else if (strcmp(tpstr, "managed") == 0 ||
125 strcmp(tpstr, "mgd") == 0 ||
126 strcmp(tpstr, "station") == 0) {
45c7212c 127 *type = NL80211_IFTYPE_STATION;
d17fe27e 128 return 0;
3d1e8704 129 } else if (strcmp(tpstr, "mp") == 0 ||
a65df165 130 strcmp(tpstr, "mesh") == 0) {
3d1e8704 131 *type = NL80211_IFTYPE_MESH_POINT;
d17fe27e 132 return 0;
a4464243
JB
133 } else if (strcmp(tpstr, "__p2pcl") == 0) {
134 *type = NL80211_IFTYPE_P2P_CLIENT;
135 return 0;
136 } else if (strcmp(tpstr, "__p2pgo") == 0) {
137 *type = NL80211_IFTYPE_P2P_GO;
138 return 0;
45c7212c
JB
139 }
140
45c7212c 141 fprintf(stderr, "invalid interface type %s\n", tpstr);
d17fe27e 142 return 2;
45c7212c
JB
143}
144
39566cca
FF
145static int parse_4addr_flag(const char *value, struct nl_msg *msg)
146{
147 if (strcmp(value, "on") == 0)
148 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, 1);
149 else if (strcmp(value, "off") == 0)
150 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, 0);
151 else
152 return 1;
153 return 0;
154
155nla_put_failure:
156 return 1;
157}
158
7c37a24d
JB
159static int handle_interface_add(struct nl80211_state *state,
160 struct nl_cb *cb,
bd396f2a
JB
161 struct nl_msg *msg,
162 int argc, char **argv)
45c7212c 163{
2dfd6bfa 164 char *name;
3d1e8704 165 char *mesh_id = NULL;
45c7212c 166 enum nl80211_iftype type;
70391ccf 167 int tpset;
45c7212c 168
bd396f2a 169 if (argc < 1)
5e75fd04 170 return 1;
45c7212c 171
2dfd6bfa 172 name = argv[0];
45c7212c
JB
173 argc--;
174 argv++;
175
d17fe27e
JB
176 tpset = get_if_type(&argc, &argv, &type, true);
177 if (tpset)
c1d44a6c 178 return tpset;
45c7212c 179
3d1e8704 180 if (argc) {
dd65496b
JB
181 if (strcmp(argv[0], "mesh_id") == 0) {
182 argc--;
183 argv++;
184
185 if (!argc)
186 return 1;
187 mesh_id = argv[0];
188 argc--;
189 argv++;
39566cca
FF
190 } else if (strcmp(argv[0], "4addr") == 0) {
191 argc--;
192 argv++;
193 if (parse_4addr_flag(argv[0], msg)) {
194 fprintf(stderr, "4addr error\n");
195 return 2;
196 }
197 argc--;
198 argv++;
dd65496b
JB
199 } else if (strcmp(argv[0], "flags") == 0) {
200 argc--;
201 argv++;
202 if (parse_mntr_flags(&argc, &argv, msg)) {
203 fprintf(stderr, "flags error\n");
204 return 2;
205 }
206 } else {
5e75fd04 207 return 1;
dd65496b 208 }
3d1e8704
LCC
209 }
210
5e75fd04
JB
211 if (argc)
212 return 1;
45c7212c 213
45c7212c 214 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name);
6d28ce25 215 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
3d1e8704
LCC
216 if (mesh_id)
217 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
45c7212c 218
70391ccf 219 return 0;
5e75fd04 220 nla_put_failure:
70391ccf 221 return -ENOBUFS;
45c7212c 222}
39566cca 223COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*]",
1ae3d621
JB
224 NL80211_CMD_NEW_INTERFACE, 0, CIB_PHY, handle_interface_add,
225 "Add a new virtual interface with the given configuration.\n"
226 IFACE_TYPES "\n\n"
227 "The flags are only used for monitor interfaces, valid flags are:\n"
228 VALID_FLAGS "\n\n"
229 "The mesh_id is used only for mesh mode.");
39566cca 230COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*]",
01ae06f9 231 NL80211_CMD_NEW_INTERFACE, 0, CIB_NETDEV, handle_interface_add, NULL);
45c7212c 232
7c37a24d
JB
233static int handle_interface_del(struct nl80211_state *state,
234 struct nl_cb *cb,
bd396f2a
JB
235 struct nl_msg *msg,
236 int argc, char **argv)
3fcfe40e 237{
70391ccf 238 return 0;
3fcfe40e 239}
1ae3d621
JB
240TOPLEVEL(del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del,
241 "Remove this virtual interface");
ce5af55c 242HIDDEN(interface, del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del);
3fcfe40e 243
541ef425
JB
244static int print_iface_handler(struct nl_msg *msg, void *arg)
245{
246 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
247 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
674567b8
JB
248 unsigned int *wiphy = arg;
249 const char *indent = "";
541ef425
JB
250
251 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
252 genlmsg_attrlen(gnlh, 0), NULL);
253
674567b8
JB
254 if (wiphy && tb_msg[NL80211_ATTR_WIPHY]) {
255 unsigned int thiswiphy = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]);
256 indent = "\t";
257 if (*wiphy != thiswiphy)
258 printf("phy#%d\n", thiswiphy);
259 *wiphy = thiswiphy;
260 }
261
541ef425 262 if (tb_msg[NL80211_ATTR_IFNAME])
674567b8 263 printf("%sInterface %s\n", indent, nla_get_string(tb_msg[NL80211_ATTR_IFNAME]));
541ef425 264 if (tb_msg[NL80211_ATTR_IFINDEX])
674567b8 265 printf("%s\tifindex %d\n", indent, nla_get_u32(tb_msg[NL80211_ATTR_IFINDEX]));
541ef425 266 if (tb_msg[NL80211_ATTR_IFTYPE])
674567b8 267 printf("%s\ttype %s\n", indent, iftype_name(nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE])));
541ef425
JB
268
269 return NL_SKIP;
270}
271
7c37a24d
JB
272static int handle_interface_info(struct nl80211_state *state,
273 struct nl_cb *cb,
bd396f2a
JB
274 struct nl_msg *msg,
275 int argc, char **argv)
541ef425 276{
541ef425 277 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, NULL);
70391ccf 278 return 0;
541ef425 279}
1ae3d621
JB
280TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info,
281 "Show information for this interface.");
cd293761 282
7c37a24d
JB
283static int handle_interface_set(struct nl80211_state *state,
284 struct nl_cb *cb,
cd293761
JB
285 struct nl_msg *msg,
286 int argc, char **argv)
287{
cd293761
JB
288 if (!argc)
289 return 1;
290
cd293761
JB
291 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
292
dd65496b
JB
293 switch (parse_mntr_flags(&argc, &argv, msg)) {
294 case 0:
295 return 0;
296 case -ENOMEM:
297 fprintf(stderr, "failed to allocate flags\n");
298 return 2;
299 case -EINVAL:
300 fprintf(stderr, "unknown flag %s\n", *argv);
301 return 2;
302 default:
303 return 2;
cd293761 304 }
cd293761 305 nla_put_failure:
dd65496b 306 return -ENOBUFS;
cd293761 307}
1ae3d621
JB
308COMMAND(set, monitor, "<flag>*",
309 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_set,
310 "Set monitor flags. Valid flags are:\n"
311 VALID_FLAGS);
82d028d0 312
7c37a24d
JB
313static int handle_interface_meshid(struct nl80211_state *state,
314 struct nl_cb *cb,
82d028d0
JB
315 struct nl_msg *msg,
316 int argc, char **argv)
317{
318 char *mesh_id = NULL;
319
320 if (argc != 1)
321 return 1;
322
323 mesh_id = argv[0];
324
325 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
326
327 return 0;
328 nla_put_failure:
329 return -ENOBUFS;
330}
331COMMAND(set, meshid, "<meshid>",
01ae06f9 332 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_meshid, NULL);
674567b8
JB
333
334static unsigned int dev_dump_wiphy;
335
336static int handle_dev_dump(struct nl80211_state *state,
337 struct nl_cb *cb,
338 struct nl_msg *msg,
339 int argc, char **argv)
340{
341 dev_dump_wiphy = -1;
342 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, &dev_dump_wiphy);
343 return 0;
344}
1ae3d621
JB
345TOPLEVEL(dev, NULL, NL80211_CMD_GET_INTERFACE, NLM_F_DUMP, CIB_NONE, handle_dev_dump,
346 "List all network interfaces for wireless hardware.");
d17fe27e
JB
347
348static int handle_interface_type(struct nl80211_state *state,
349 struct nl_cb *cb,
350 struct nl_msg *msg,
351 int argc, char **argv)
352{
353 enum nl80211_iftype type;
354 int tpset;
355
356 tpset = get_if_type(&argc, &argv, &type, false);
357 if (tpset)
358 return tpset;
359
360 if (argc)
361 return 1;
362
363 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
364
365 return 0;
366 nla_put_failure:
367 return -ENOBUFS;
368}
369COMMAND(set, type, "<type>",
1ae3d621
JB
370 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_type,
371 "Set interface type/mode.\n"
372 IFACE_TYPES);