blob: 4b2a8a6bf6314091a7b6c729bd30081985eaaee3 [file] [log] [blame]
Johannes Berg2ef1be62008-04-02 17:40:11 +02001#include <net/if.h>
Johannes Berg45c72122007-09-28 23:47:38 +02002#include <errno.h>
Johannes Bergd5ac8ad2008-04-03 15:24:13 +02003#include <string.h>
Johannes Berg2ef1be62008-04-02 17:40:11 +02004
Johannes Berg45c72122007-09-28 23:47:38 +02005#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>
Johannes Berg45c72122007-09-28 23:47:38 +020010
Johannes Bergf408e012008-09-18 19:32:11 +020011#include "nl80211.h"
Johannes Berg45c72122007-09-28 23:47:38 +020012#include "iw.h"
13
Johannes Bergcd293762008-09-16 20:13:59 +020014static char *mntr_flags[NL80211_MNTR_FLAG_MAX + 1] = {
Johannes Bergdd654962008-10-05 14:00:11 +020015 "none",
Johannes Bergcd293762008-09-16 20:13:59 +020016 "fcsfail",
17 "plcpfail",
18 "control",
19 "otherbss",
20 "cook",
21};
22
Johannes Bergdd654962008-10-05 14:00:11 +020023static int parse_mntr_flags(int *_argc, char ***_argv,
24 struct nl_msg *msg)
25{
26 struct nl_msg *flags;
27 int err = -ENOBUFS;
28 enum nl80211_mntr_flags flag;
29 int argc = *_argc;
30 char **argv = *_argv;
31
32 flags = nlmsg_alloc();
33 if (!flags)
34 return -ENOMEM;
35
36 while (argc) {
37 int ok = 0;
38 for (flag = __NL80211_MNTR_FLAG_INVALID;
39 flag < NL80211_MNTR_FLAG_MAX; flag++) {
40 if (strcmp(*argv, mntr_flags[flag]) == 0) {
41 ok = 1;
Johannes Berg4a3bf752008-10-15 11:55:42 +020042 /*
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
48 */
Johannes Bergdd654962008-10-05 14:00:11 +020049 NLA_PUT_FLAG(flags, flag);
50 break;
51 }
52 }
53 if (!ok) {
54 err = -EINVAL;
55 goto out;
56 }
57 argc--;
58 argv++;
59 }
60
61 nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
62 err = 0;
63 nla_put_failure:
64 out:
65 nlmsg_free(flags);
66
67 *_argc = argc;
68 *_argv = argv;
69
70 return err;
71}
72
Johannes Berg45c72122007-09-28 23:47:38 +020073/* return 0 if not found, 1 if ok, -1 on error */
74static int get_if_type(int *argc, char ***argv, enum nl80211_iftype *type)
75{
76 char *tpstr;
77
78 if (*argc < 2)
79 return 0;
80
81 if (strcmp((*argv)[0], "type"))
82 return 0;
83
84 tpstr = (*argv)[1];
85 *argc -= 2;
86 *argv += 2;
87
88 if (strcmp(tpstr, "adhoc") == 0 ||
89 strcmp(tpstr, "ibss") == 0) {
90 *type = NL80211_IFTYPE_ADHOC;
91 return 1;
92 } else if (strcmp(tpstr, "monitor") == 0) {
93 *type = NL80211_IFTYPE_MONITOR;
94 return 1;
Johannes Bergc1d44a62009-01-24 15:35:30 +010095 } else if (strcmp(tpstr, "master") == 0) {
96 *type = NL80211_IFTYPE_UNSPECIFIED;
97 fprintf(stderr, "See http://wireless.kernel.org/RTFM-AP.\n");
98 return 2;
99 } else if (strcmp(tpstr, "ap") == 0) {
100 *type = NL80211_IFTYPE_UNSPECIFIED;
101 fprintf(stderr, "See http://wireless.kernel.org/RTFM-AP.\n");
102 return 2;
Johannes Berg4d3a72d2008-04-14 15:41:31 +0200103 } else if (strcmp(tpstr, "__ap") == 0) {
Johannes Berg45c72122007-09-28 23:47:38 +0200104 *type = NL80211_IFTYPE_AP;
105 return 1;
Johannes Berg4d3a72d2008-04-14 15:41:31 +0200106 } else if (strcmp(tpstr, "__ap_vlan") == 0) {
Johannes Berg45c72122007-09-28 23:47:38 +0200107 *type = NL80211_IFTYPE_AP_VLAN;
108 return 1;
109 } else if (strcmp(tpstr, "wds") == 0) {
110 *type = NL80211_IFTYPE_WDS;
111 return 1;
112 } else if (strcmp(tpstr, "station") == 0) {
113 *type = NL80211_IFTYPE_STATION;
114 return 1;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700115 } else if (strcmp(tpstr, "mp") == 0 ||
116 strcmp(tpstr, "mesh") == 0) {
117 *type = NL80211_IFTYPE_MESH_POINT;
118 return 1;
Johannes Berg45c72122007-09-28 23:47:38 +0200119 }
120
121
122 fprintf(stderr, "invalid interface type %s\n", tpstr);
123 return -1;
124}
125
Johannes Berg70391cc2008-09-16 18:35:06 +0200126static int handle_interface_add(struct nl_cb *cb,
Johannes Bergbd396f22008-09-16 16:56:09 +0200127 struct nl_msg *msg,
128 int argc, char **argv)
Johannes Berg45c72122007-09-28 23:47:38 +0200129{
Andrew Lutomirski2dfd6bf2007-12-20 18:50:07 +0100130 char *name;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700131 char *mesh_id = NULL;
Johannes Berg45c72122007-09-28 23:47:38 +0200132 enum nl80211_iftype type;
Johannes Berg70391cc2008-09-16 18:35:06 +0200133 int tpset;
Johannes Berg45c72122007-09-28 23:47:38 +0200134
Johannes Bergbd396f22008-09-16 16:56:09 +0200135 if (argc < 1)
Johannes Berg5e75fd02008-09-16 18:13:12 +0200136 return 1;
Johannes Berg45c72122007-09-28 23:47:38 +0200137
Andrew Lutomirski2dfd6bf2007-12-20 18:50:07 +0100138 name = argv[0];
Johannes Berg45c72122007-09-28 23:47:38 +0200139 argc--;
140 argv++;
141
Andrew Lutomirski2dfd6bf2007-12-20 18:50:07 +0100142 tpset = get_if_type(&argc, &argv, &type);
Johannes Bergc1d44a62009-01-24 15:35:30 +0100143 if (tpset != 1)
144 return tpset;
Johannes Berg45c72122007-09-28 23:47:38 +0200145
146 if (argc) {
Johannes Bergdd654962008-10-05 14:00:11 +0200147 if (strcmp(argv[0], "mesh_id") == 0) {
148 argc--;
149 argv++;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700150
Johannes Bergdd654962008-10-05 14:00:11 +0200151 if (!argc)
152 return 1;
153 mesh_id = argv[0];
154 argc--;
155 argv++;
156 } else if (strcmp(argv[0], "flags") == 0) {
157 argc--;
158 argv++;
159 if (parse_mntr_flags(&argc, &argv, msg)) {
160 fprintf(stderr, "flags error\n");
161 return 2;
162 }
163 } else {
Johannes Berg5e75fd02008-09-16 18:13:12 +0200164 return 1;
Johannes Bergdd654962008-10-05 14:00:11 +0200165 }
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700166 }
167
Johannes Berg5e75fd02008-09-16 18:13:12 +0200168 if (argc)
169 return 1;
Johannes Berg45c72122007-09-28 23:47:38 +0200170
Johannes Berg45c72122007-09-28 23:47:38 +0200171 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name);
172 if (tpset)
173 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700174 if (mesh_id)
175 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
Johannes Berg45c72122007-09-28 23:47:38 +0200176
Johannes Berg70391cc2008-09-16 18:35:06 +0200177 return 0;
Johannes Berg5e75fd02008-09-16 18:13:12 +0200178 nla_put_failure:
Johannes Berg70391cc2008-09-16 18:35:06 +0200179 return -ENOBUFS;
Johannes Berg45c72122007-09-28 23:47:38 +0200180}
Johannes Bergdd654962008-10-05 14:00:11 +0200181COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [flags ...]",
Johannes Bergbd396f22008-09-16 16:56:09 +0200182 NL80211_CMD_NEW_INTERFACE, 0, CIB_PHY, handle_interface_add);
Johannes Bergdd654962008-10-05 14:00:11 +0200183COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [flags ...]",
Johannes Berg1cd3b6c2008-09-16 17:52:56 +0200184 NL80211_CMD_NEW_INTERFACE, 0, CIB_NETDEV, handle_interface_add);
Johannes Berg45c72122007-09-28 23:47:38 +0200185
Johannes Berg70391cc2008-09-16 18:35:06 +0200186static int handle_interface_del(struct nl_cb *cb,
Johannes Bergbd396f22008-09-16 16:56:09 +0200187 struct nl_msg *msg,
188 int argc, char **argv)
Johannes Berg3fcfe402007-09-28 23:50:25 +0200189{
Johannes Berg70391cc2008-09-16 18:35:06 +0200190 return 0;
Johannes Berg3fcfe402007-09-28 23:50:25 +0200191}
Johannes Bergbd396f22008-09-16 16:56:09 +0200192TOPLEVEL(del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del);
Johannes Bergce5af552008-09-17 13:27:04 +0200193HIDDEN(interface, del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del);
Johannes Berg3fcfe402007-09-28 23:50:25 +0200194
Johannes Berg541ef422008-09-16 14:50:11 +0200195static int print_iface_handler(struct nl_msg *msg, void *arg)
196{
197 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
198 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
199
200 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
201 genlmsg_attrlen(gnlh, 0), NULL);
202
203 if (tb_msg[NL80211_ATTR_IFNAME])
204 printf("Interface %s\n", nla_get_string(tb_msg[NL80211_ATTR_IFNAME]));
205 if (tb_msg[NL80211_ATTR_IFINDEX])
206 printf("\tifindex %d\n", nla_get_u32(tb_msg[NL80211_ATTR_IFINDEX]));
207 if (tb_msg[NL80211_ATTR_IFTYPE])
208 printf("\ttype %s\n", iftype_name(nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE])));
209
210 return NL_SKIP;
211}
212
Johannes Berg70391cc2008-09-16 18:35:06 +0200213static int handle_interface_info(struct nl_cb *cb,
Johannes Bergbd396f22008-09-16 16:56:09 +0200214 struct nl_msg *msg,
215 int argc, char **argv)
Johannes Berg541ef422008-09-16 14:50:11 +0200216{
Johannes Berg541ef422008-09-16 14:50:11 +0200217 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, NULL);
Johannes Berg70391cc2008-09-16 18:35:06 +0200218 return 0;
Johannes Berg541ef422008-09-16 14:50:11 +0200219}
Johannes Bergbd396f22008-09-16 16:56:09 +0200220TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info);
Johannes Bergcd293762008-09-16 20:13:59 +0200221
222static int handle_interface_set(struct nl_cb *cb,
223 struct nl_msg *msg,
224 int argc, char **argv)
225{
Johannes Bergcd293762008-09-16 20:13:59 +0200226 if (!argc)
227 return 1;
228
Johannes Bergcd293762008-09-16 20:13:59 +0200229 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
230
Johannes Bergdd654962008-10-05 14:00:11 +0200231 switch (parse_mntr_flags(&argc, &argv, msg)) {
232 case 0:
233 return 0;
234 case -ENOMEM:
235 fprintf(stderr, "failed to allocate flags\n");
236 return 2;
237 case -EINVAL:
238 fprintf(stderr, "unknown flag %s\n", *argv);
239 return 2;
240 default:
241 return 2;
Johannes Bergcd293762008-09-16 20:13:59 +0200242 }
Johannes Bergcd293762008-09-16 20:13:59 +0200243 nla_put_failure:
Johannes Bergdd654962008-10-05 14:00:11 +0200244 return -ENOBUFS;
Johannes Bergcd293762008-09-16 20:13:59 +0200245}
246COMMAND(set, monitor, "<flag> [...]",
247 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_set);
Johannes Berg82d028d2008-09-16 20:45:34 +0200248
249static int handle_interface_meshid(struct nl_cb *cb,
250 struct nl_msg *msg,
251 int argc, char **argv)
252{
253 char *mesh_id = NULL;
254
255 if (argc != 1)
256 return 1;
257
258 mesh_id = argv[0];
259
260 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
261
262 return 0;
263 nla_put_failure:
264 return -ENOBUFS;
265}
266COMMAND(set, meshid, "<meshid>",
267 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_meshid);