]> git.ipfire.org Git - thirdparty/iw.git/blob - interface.c
iw: add support for NL80211_CMD_DEL_STATION event
[thirdparty/iw.git] / interface.c
1 #include <net/if.h>
2 #include <errno.h>
3 #include <string.h>
4 #include <stdbool.h>
5
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>
11
12 #include "nl80211.h"
13 #include "iw.h"
14
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
21 SECTION(interface);
22
23 static char *mntr_flags[NL80211_MNTR_FLAG_MAX + 1] = {
24 "none",
25 "fcsfail",
26 "plcpfail",
27 "control",
28 "otherbss",
29 "cook",
30 };
31
32 static 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;
48 flag <= NL80211_MNTR_FLAG_MAX; flag++) {
49 if (strcmp(*argv, mntr_flags[flag]) == 0) {
50 ok = 1;
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 */
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
82 /* for help */
83 #define IFACE_TYPES "Valid interface types are: managed, ibss, monitor, mesh, wds."
84
85 /* return 0 if ok, internal error otherwise */
86 static int get_if_type(int *argc, char ***argv, enum nl80211_iftype *type,
87 bool need_type)
88 {
89 char *tpstr;
90
91 if (*argc < 1 + !!need_type)
92 return 1;
93
94 if (need_type && strcmp((*argv)[0], "type"))
95 return 1;
96
97 tpstr = (*argv)[!!need_type];
98 *argc -= 1 + !!need_type;
99 *argv += 1 + !!need_type;
100
101 if (strcmp(tpstr, "adhoc") == 0 ||
102 strcmp(tpstr, "ibss") == 0) {
103 *type = NL80211_IFTYPE_ADHOC;
104 return 0;
105 } else if (strcmp(tpstr, "monitor") == 0) {
106 *type = NL80211_IFTYPE_MONITOR;
107 return 0;
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");
114 return 2;
115 } else if (strcmp(tpstr, "__ap") == 0) {
116 *type = NL80211_IFTYPE_AP;
117 return 0;
118 } else if (strcmp(tpstr, "__ap_vlan") == 0) {
119 *type = NL80211_IFTYPE_AP_VLAN;
120 return 0;
121 } else if (strcmp(tpstr, "wds") == 0) {
122 *type = NL80211_IFTYPE_WDS;
123 return 0;
124 } else if (strcmp(tpstr, "managed") == 0 ||
125 strcmp(tpstr, "mgd") == 0 ||
126 strcmp(tpstr, "station") == 0) {
127 *type = NL80211_IFTYPE_STATION;
128 return 0;
129 } else if (strcmp(tpstr, "mp") == 0 ||
130 strcmp(tpstr, "mesh") == 0) {
131 *type = NL80211_IFTYPE_MESH_POINT;
132 return 0;
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;
139 }
140
141 fprintf(stderr, "invalid interface type %s\n", tpstr);
142 return 2;
143 }
144
145 static 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
155 nla_put_failure:
156 return 1;
157 }
158
159 static int handle_interface_add(struct nl80211_state *state,
160 struct nl_cb *cb,
161 struct nl_msg *msg,
162 int argc, char **argv)
163 {
164 char *name;
165 char *mesh_id = NULL;
166 enum nl80211_iftype type;
167 int tpset;
168
169 if (argc < 1)
170 return 1;
171
172 name = argv[0];
173 argc--;
174 argv++;
175
176 tpset = get_if_type(&argc, &argv, &type, true);
177 if (tpset)
178 return tpset;
179
180 if (argc) {
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++;
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++;
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 {
207 return 1;
208 }
209 }
210
211 if (argc)
212 return 1;
213
214 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name);
215 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
216 if (mesh_id)
217 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
218
219 return 0;
220 nla_put_failure:
221 return -ENOBUFS;
222 }
223 COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*]",
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.");
230 COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*]",
231 NL80211_CMD_NEW_INTERFACE, 0, CIB_NETDEV, handle_interface_add, NULL);
232
233 static int handle_interface_del(struct nl80211_state *state,
234 struct nl_cb *cb,
235 struct nl_msg *msg,
236 int argc, char **argv)
237 {
238 return 0;
239 }
240 TOPLEVEL(del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del,
241 "Remove this virtual interface");
242 HIDDEN(interface, del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del);
243
244 static 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];
248 unsigned int *wiphy = arg;
249 const char *indent = "";
250
251 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
252 genlmsg_attrlen(gnlh, 0), NULL);
253
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
262 if (tb_msg[NL80211_ATTR_IFNAME])
263 printf("%sInterface %s\n", indent, nla_get_string(tb_msg[NL80211_ATTR_IFNAME]));
264 if (tb_msg[NL80211_ATTR_IFINDEX])
265 printf("%s\tifindex %d\n", indent, nla_get_u32(tb_msg[NL80211_ATTR_IFINDEX]));
266 if (tb_msg[NL80211_ATTR_IFTYPE])
267 printf("%s\ttype %s\n", indent, iftype_name(nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE])));
268 if (!wiphy && tb_msg[NL80211_ATTR_WIPHY])
269 printf("%s\twiphy %d\n", indent, nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE]));
270
271 return NL_SKIP;
272 }
273
274 static int handle_interface_info(struct nl80211_state *state,
275 struct nl_cb *cb,
276 struct nl_msg *msg,
277 int argc, char **argv)
278 {
279 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, NULL);
280 return 0;
281 }
282 TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info,
283 "Show information for this interface.");
284
285 static int handle_interface_set(struct nl80211_state *state,
286 struct nl_cb *cb,
287 struct nl_msg *msg,
288 int argc, char **argv)
289 {
290 if (!argc)
291 return 1;
292
293 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
294
295 switch (parse_mntr_flags(&argc, &argv, msg)) {
296 case 0:
297 return 0;
298 case -ENOMEM:
299 fprintf(stderr, "failed to allocate flags\n");
300 return 2;
301 case -EINVAL:
302 fprintf(stderr, "unknown flag %s\n", *argv);
303 return 2;
304 default:
305 return 2;
306 }
307 nla_put_failure:
308 return -ENOBUFS;
309 }
310 COMMAND(set, monitor, "<flag>*",
311 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_set,
312 "Set monitor flags. Valid flags are:\n"
313 VALID_FLAGS);
314
315 static int handle_interface_meshid(struct nl80211_state *state,
316 struct nl_cb *cb,
317 struct nl_msg *msg,
318 int argc, char **argv)
319 {
320 char *mesh_id = NULL;
321
322 if (argc != 1)
323 return 1;
324
325 mesh_id = argv[0];
326
327 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
328
329 return 0;
330 nla_put_failure:
331 return -ENOBUFS;
332 }
333 COMMAND(set, meshid, "<meshid>",
334 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_meshid, NULL);
335
336 static unsigned int dev_dump_wiphy;
337
338 static int handle_dev_dump(struct nl80211_state *state,
339 struct nl_cb *cb,
340 struct nl_msg *msg,
341 int argc, char **argv)
342 {
343 dev_dump_wiphy = -1;
344 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, &dev_dump_wiphy);
345 return 0;
346 }
347 TOPLEVEL(dev, NULL, NL80211_CMD_GET_INTERFACE, NLM_F_DUMP, CIB_NONE, handle_dev_dump,
348 "List all network interfaces for wireless hardware.");
349
350 static int handle_interface_type(struct nl80211_state *state,
351 struct nl_cb *cb,
352 struct nl_msg *msg,
353 int argc, char **argv)
354 {
355 enum nl80211_iftype type;
356 int tpset;
357
358 tpset = get_if_type(&argc, &argv, &type, false);
359 if (tpset)
360 return tpset;
361
362 if (argc)
363 return 1;
364
365 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
366
367 return 0;
368 nla_put_failure:
369 return -ENOBUFS;
370 }
371 COMMAND(set, type, "<type>",
372 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_type,
373 "Set interface type/mode.\n"
374 IFACE_TYPES);
375
376 static int handle_interface_4addr(struct nl80211_state *state,
377 struct nl_cb *cb,
378 struct nl_msg *msg,
379 int argc, char **argv)
380 {
381 if (argc != 1)
382 return 1;
383 return parse_4addr_flag(argv[0], msg);
384 }
385 COMMAND(set, 4addr, "<on|off>",
386 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_4addr,
387 "Set interface 4addr (WDS) mode.");
388
389 static int handle_interface_noack_map(struct nl80211_state *state,
390 struct nl_cb *cb,
391 struct nl_msg *msg,
392 int argc, char **argv)
393 {
394 uint16_t noack_map;
395 char *end;
396
397 if (argc != 1)
398 return 1;
399
400 noack_map = strtoul(argv[0], &end, 16);
401 if (*end)
402 return 1;
403
404 NLA_PUT_U16(msg, NL80211_ATTR_NOACK_MAP, noack_map);
405
406 return 0;
407 nla_put_failure:
408 return -ENOBUFS;
409
410 }
411 COMMAND(set, noack_map, "<map>",
412 NL80211_CMD_SET_NOACK_MAP, 0, CIB_NETDEV, handle_interface_noack_map,
413 "Set the NoAck map for the TIDs. (0x0009 = BE, 0x0006 = BK, 0x0030 = VI, 0x00C0 = VO)");
414
415
416 static int handle_interface_wds_peer(struct nl80211_state *state,
417 struct nl_cb *cb,
418 struct nl_msg *msg,
419 int argc, char **argv)
420 {
421 unsigned char mac_addr[ETH_ALEN];
422
423 if (argc < 1)
424 return 1;
425
426 if (mac_addr_a2n(mac_addr, argv[0])) {
427 fprintf(stderr, "Invalid MAC address\n");
428 return 2;
429 }
430
431 argc--;
432 argv++;
433
434 if (argc)
435 return 1;
436
437 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
438
439 return 0;
440 nla_put_failure:
441 return -ENOBUFS;
442 }
443 COMMAND(set, peer, "<MAC address>",
444 NL80211_CMD_SET_WDS_PEER, 0, CIB_NETDEV, handle_interface_wds_peer,
445 "Set interface WDS peer.");