]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
ship nl80211.h
[thirdparty/iw.git] / iw.c
1 /*
2 * nl80211 userspace tool
3 *
4 * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net>
5 */
6
7 #include <errno.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <net/if.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15
16 #include <netlink/genl/genl.h>
17 #include <netlink/genl/family.h>
18 #include <netlink/genl/ctrl.h>
19 #include <netlink/msg.h>
20 #include <netlink/attr.h>
21
22 #include "nl80211.h"
23 #include "iw.h"
24 #include "version.h"
25
26 int debug = 0;
27
28 static int nl80211_init(struct nl80211_state *state)
29 {
30 int err;
31
32 state->nl_handle = nl_handle_alloc();
33 if (!state->nl_handle) {
34 fprintf(stderr, "Failed to allocate netlink handle.\n");
35 return -ENOMEM;
36 }
37
38 if (genl_connect(state->nl_handle)) {
39 fprintf(stderr, "Failed to connect to generic netlink.\n");
40 err = -ENOLINK;
41 goto out_handle_destroy;
42 }
43
44 state->nl_cache = genl_ctrl_alloc_cache(state->nl_handle);
45 if (!state->nl_cache) {
46 fprintf(stderr, "Failed to allocate generic netlink cache.\n");
47 err = -ENOMEM;
48 goto out_handle_destroy;
49 }
50
51 state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
52 if (!state->nl80211) {
53 fprintf(stderr, "nl80211 not found.\n");
54 err = -ENOENT;
55 goto out_cache_free;
56 }
57
58 return 0;
59
60 out_cache_free:
61 nl_cache_free(state->nl_cache);
62 out_handle_destroy:
63 nl_handle_destroy(state->nl_handle);
64 return err;
65 }
66
67 static void nl80211_cleanup(struct nl80211_state *state)
68 {
69 genl_family_put(state->nl80211);
70 nl_cache_free(state->nl_cache);
71 nl_handle_destroy(state->nl_handle);
72 }
73
74 __COMMAND(NULL, NULL, NULL, 0, 0, 0, CIB_NONE, NULL);
75 __COMMAND(NULL, NULL, NULL, 1, 0, 0, CIB_NONE, NULL);
76
77 static int cmd_size;
78
79 static void usage(const char *argv0)
80 {
81 struct cmd *cmd;
82
83 fprintf(stderr, "Usage:\t%s [options] command\n", argv0);
84 fprintf(stderr, "Options:\n");
85 fprintf(stderr, "\t--debug\t\tenable netlink debugging\n");
86 fprintf(stderr, "\t--version\tshow version\n");
87 fprintf(stderr, "Commands:\n");
88 for (cmd = &__start___cmd; cmd < &__stop___cmd;
89 cmd = (struct cmd *)((char *)cmd + cmd_size)) {
90 if (!cmd->handler || cmd->hidden)
91 continue;
92 switch (cmd->idby) {
93 case CIB_NONE:
94 fprintf(stderr, "\t");
95 /* fall through */
96 case CIB_PHY:
97 if (cmd->idby == CIB_PHY)
98 fprintf(stderr, "\tphy <phyname> ");
99 /* fall through */
100 case CIB_NETDEV:
101 if (cmd->idby == CIB_NETDEV)
102 fprintf(stderr, "\tdev <devname> ");
103 if (cmd->section)
104 fprintf(stderr, "%s ", cmd->section);
105 fprintf(stderr, "%s", cmd->name);
106 if (cmd->args)
107 fprintf(stderr, " %s", cmd->args);
108 fprintf(stderr, "\n");
109 break;
110 }
111 }
112 }
113
114 static void version(void)
115 {
116 printf("iw version " IW_VERSION "\n");
117 }
118
119 static int phy_lookup(char *name)
120 {
121 char buf[200];
122 int fd, pos;
123
124 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
125
126 fd = open(buf, O_RDONLY);
127 pos = read(fd, buf, sizeof(buf) - 1);
128 if (pos < 0)
129 return -1;
130 buf[pos] = '\0';
131 return atoi(buf);
132 }
133
134 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
135 void *arg)
136 {
137 int *ret = arg;
138 *ret = err->error;
139 return NL_STOP;
140 }
141
142 static int finish_handler(struct nl_msg *msg, void *arg)
143 {
144 return NL_SKIP;
145 }
146
147 static int ack_handler(struct nl_msg *msg, void *arg)
148 {
149 int *ret = arg;
150 *ret = 0;
151 return NL_STOP;
152 }
153
154 static int handle_cmd(struct nl80211_state *state,
155 enum command_identify_by idby,
156 int argc, char **argv)
157 {
158 struct cmd *cmd;
159 struct nl_cb *cb = NULL;
160 struct nl_msg *msg;
161 int devidx = 0;
162 int err;
163 const char *command, *section;
164
165 if (argc <= 1 && idby != CIB_NONE)
166 return 1;
167
168 switch (idby) {
169 case CIB_PHY:
170 devidx = phy_lookup(*argv);
171 argc--;
172 argv++;
173 break;
174 case CIB_NETDEV:
175 devidx = if_nametoindex(*argv);
176 argc--;
177 argv++;
178 break;
179 default:
180 break;
181 }
182
183 section = command = *argv;
184 argc--;
185 argv++;
186
187 for (cmd = &__start___cmd; cmd < &__stop___cmd;
188 cmd = (struct cmd *)((char *)cmd + cmd_size)) {
189 if (!cmd->handler)
190 continue;
191 if (cmd->idby != idby)
192 continue;
193 if (cmd->section) {
194 if (strcmp(cmd->section, section))
195 continue;
196 /* this is a bit icky ... */
197 if (command == section) {
198 if (argc <= 0)
199 return 1;
200 command = *argv;
201 argc--;
202 argv++;
203 }
204 } else if (section != command)
205 continue;
206 if (strcmp(cmd->name, command))
207 continue;
208 if (argc && !cmd->args)
209 continue;
210 break;
211 }
212
213 if (cmd >= &__stop___cmd)
214 return 1;
215
216 msg = nlmsg_alloc();
217 if (!msg) {
218 fprintf(stderr, "failed to allocate netlink message\n");
219 return 2;
220 }
221
222 cb = nl_cb_alloc(debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
223 if (!cb) {
224 fprintf(stderr, "failed to allocate netlink callbacks\n");
225 err = 2;
226 goto out_free_msg;
227 }
228
229 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
230 cmd->nl_msg_flags, cmd->cmd, 0);
231
232 switch (idby) {
233 case CIB_PHY:
234 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
235 break;
236 case CIB_NETDEV:
237 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
238 break;
239 default:
240 break;
241 }
242
243 err = cmd->handler(cb, msg, argc, argv);
244 if (err)
245 goto out;
246
247 err = nl_send_auto_complete(state->nl_handle, msg);
248 if (err < 0)
249 goto out;
250
251 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
252 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, NULL);
253 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
254
255 nl_recvmsgs(state->nl_handle, cb);
256 out:
257 nl_cb_put(cb);
258 out_free_msg:
259 nlmsg_free(msg);
260 return err;
261 nla_put_failure:
262 fprintf(stderr, "building message failed\n");
263 return 2;
264 }
265
266 int main(int argc, char **argv)
267 {
268 struct nl80211_state nlstate;
269 int err;
270 const char *argv0;
271
272 /* calculate command size including padding */
273 cmd_size = abs((long)&__cmd_NULL_1_CIB_NONE_0
274 - (long)&__cmd_NULL_0_CIB_NONE_0);
275 /* strip off self */
276 argc--;
277 argv0 = *argv++;
278
279 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
280 debug = 1;
281 argc--;
282 argv++;
283 }
284
285 if (argc > 0 && strcmp(*argv, "--version") == 0) {
286 version();
287 return 0;
288 }
289
290 if (argc == 0 || strcmp(*argv, "help") == 0) {
291 usage(argv0);
292 return 0;
293 }
294
295 err = nl80211_init(&nlstate);
296 if (err)
297 return 1;
298
299 if (strcmp(*argv, "dev") == 0) {
300 argc--;
301 argv++;
302 err = handle_cmd(&nlstate, CIB_NETDEV, argc, argv);
303 } else if (strcmp(*argv, "phy") == 0) {
304 argc--;
305 argv++;
306 err = handle_cmd(&nlstate, CIB_PHY, argc, argv);
307 } else
308 err = handle_cmd(&nlstate, CIB_NONE, argc, argv);
309
310 if (err == 1)
311 usage(argv0);
312 if (err < 0)
313 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
314
315 nl80211_cleanup(&nlstate);
316
317 return err;
318 }