]> git.ipfire.org Git - thirdparty/iw.git/blame - interface.c
fix netlink handling
[thirdparty/iw.git] / interface.c
CommitLineData
2ef1be68 1#include <net/if.h>
45c7212c 2#include <errno.h>
d5ac8ad3 3#include <string.h>
2ef1be68 4
45c7212c
JB
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>
45c7212c 10
f408e01b 11#include "nl80211.h"
45c7212c
JB
12#include "iw.h"
13
cd293761
JB
14static char *mntr_flags[NL80211_MNTR_FLAG_MAX + 1] = {
15 NULL,
16 "fcsfail",
17 "plcpfail",
18 "control",
19 "otherbss",
20 "cook",
21};
22
45c7212c
JB
23/* return 0 if not found, 1 if ok, -1 on error */
24static int get_if_type(int *argc, char ***argv, enum nl80211_iftype *type)
25{
26 char *tpstr;
27
28 if (*argc < 2)
29 return 0;
30
31 if (strcmp((*argv)[0], "type"))
32 return 0;
33
34 tpstr = (*argv)[1];
35 *argc -= 2;
36 *argv += 2;
37
38 if (strcmp(tpstr, "adhoc") == 0 ||
39 strcmp(tpstr, "ibss") == 0) {
40 *type = NL80211_IFTYPE_ADHOC;
41 return 1;
42 } else if (strcmp(tpstr, "monitor") == 0) {
43 *type = NL80211_IFTYPE_MONITOR;
44 return 1;
4d3a72da 45 } else if (strcmp(tpstr, "__ap") == 0) {
45c7212c
JB
46 *type = NL80211_IFTYPE_AP;
47 return 1;
4d3a72da 48 } else if (strcmp(tpstr, "__ap_vlan") == 0) {
45c7212c
JB
49 *type = NL80211_IFTYPE_AP_VLAN;
50 return 1;
51 } else if (strcmp(tpstr, "wds") == 0) {
52 *type = NL80211_IFTYPE_WDS;
53 return 1;
54 } else if (strcmp(tpstr, "station") == 0) {
55 *type = NL80211_IFTYPE_STATION;
56 return 1;
3d1e8704
LCC
57 } else if (strcmp(tpstr, "mp") == 0 ||
58 strcmp(tpstr, "mesh") == 0) {
59 *type = NL80211_IFTYPE_MESH_POINT;
60 return 1;
45c7212c
JB
61 }
62
63
64 fprintf(stderr, "invalid interface type %s\n", tpstr);
65 return -1;
66}
67
70391ccf 68static int handle_interface_add(struct nl_cb *cb,
bd396f2a
JB
69 struct nl_msg *msg,
70 int argc, char **argv)
45c7212c 71{
2dfd6bfa 72 char *name;
3d1e8704 73 char *mesh_id = NULL;
45c7212c 74 enum nl80211_iftype type;
70391ccf 75 int tpset;
45c7212c 76
bd396f2a 77 if (argc < 1)
5e75fd04 78 return 1;
45c7212c 79
2dfd6bfa 80 name = argv[0];
45c7212c
JB
81 argc--;
82 argv++;
83
2dfd6bfa 84 tpset = get_if_type(&argc, &argv, &type);
2dfd6bfa 85 if (tpset <= 0)
5e75fd04 86 return 1;
45c7212c 87
3d1e8704 88 if (argc) {
5e75fd04
JB
89 if (strcmp(argv[0], "mesh_id") != 0)
90 return 1;
3d1e8704
LCC
91 argc--;
92 argv++;
93
5e75fd04
JB
94 if (!argc)
95 return 1;
3d1e8704
LCC
96 mesh_id = argv[0];
97 argc--;
98 argv++;
99 }
100
5e75fd04
JB
101 if (argc)
102 return 1;
45c7212c 103
45c7212c
JB
104 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name);
105 if (tpset)
106 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
3d1e8704
LCC
107 if (mesh_id)
108 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
45c7212c 109
70391ccf 110 return 0;
5e75fd04 111 nla_put_failure:
70391ccf 112 return -ENOBUFS;
45c7212c 113}
bd396f2a
JB
114COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>]",
115 NL80211_CMD_NEW_INTERFACE, 0, CIB_PHY, handle_interface_add);
1cd3b6c6
JB
116COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>]",
117 NL80211_CMD_NEW_INTERFACE, 0, CIB_NETDEV, handle_interface_add);
45c7212c 118
70391ccf 119static int handle_interface_del(struct nl_cb *cb,
bd396f2a
JB
120 struct nl_msg *msg,
121 int argc, char **argv)
3fcfe40e 122{
70391ccf 123 return 0;
3fcfe40e 124}
bd396f2a 125TOPLEVEL(del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del);
ce5af55c 126HIDDEN(interface, del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del);
3fcfe40e 127
541ef425
JB
128static int print_iface_handler(struct nl_msg *msg, void *arg)
129{
130 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
131 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
132
133 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
134 genlmsg_attrlen(gnlh, 0), NULL);
135
136 if (tb_msg[NL80211_ATTR_IFNAME])
137 printf("Interface %s\n", nla_get_string(tb_msg[NL80211_ATTR_IFNAME]));
138 if (tb_msg[NL80211_ATTR_IFINDEX])
139 printf("\tifindex %d\n", nla_get_u32(tb_msg[NL80211_ATTR_IFINDEX]));
140 if (tb_msg[NL80211_ATTR_IFTYPE])
141 printf("\ttype %s\n", iftype_name(nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE])));
142
143 return NL_SKIP;
144}
145
70391ccf 146static int handle_interface_info(struct nl_cb *cb,
bd396f2a
JB
147 struct nl_msg *msg,
148 int argc, char **argv)
541ef425 149{
541ef425 150 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, NULL);
70391ccf 151 return 0;
541ef425 152}
bd396f2a 153TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info);
cd293761
JB
154
155static int handle_interface_set(struct nl_cb *cb,
156 struct nl_msg *msg,
157 int argc, char **argv)
158{
159 enum nl80211_mntr_flags flag;
160 struct nl_msg *flags;
161 int err;
162
163 if (!argc)
164 return 1;
165
166 flags = nlmsg_alloc();
167 if (!flags) {
168 fprintf(stderr, "failed to allocate flags\n");
169 return 2;
170 }
171
172 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
173
174 while (argc) {
175 int ok = 0;
176 for (flag = __NL80211_MNTR_FLAG_INVALID + 1;
177 flag < NL80211_MNTR_FLAG_MAX; flag++) {
178 if (strcmp(*argv, mntr_flags[flag]) == 0) {
179 ok = 1;
180 NLA_PUT_FLAG(flags, flag);
181 break;
182 }
183 }
184 if (!ok) {
185 fprintf(stderr, "unknown flag %s\n", *argv);
186 err = 2;
187 goto out;
188 }
189 argc--;
190 argv++;
191 }
192
193 nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
194
195 err = 0;
196 goto out;
197 nla_put_failure:
198 err = -ENOBUFS;
199 out:
200 nlmsg_free(flags);
201 return err;
202}
203COMMAND(set, monitor, "<flag> [...]",
204 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_set);
82d028d0
JB
205
206static int handle_interface_meshid(struct nl_cb *cb,
207 struct nl_msg *msg,
208 int argc, char **argv)
209{
210 char *mesh_id = NULL;
211
212 if (argc != 1)
213 return 1;
214
215 mesh_id = argv[0];
216
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}
223COMMAND(set, meshid, "<meshid>",
224 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_meshid);