]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
simplify netlink handling
[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 #include <linux/nl80211.h>
22
23 #include "iw.h"
24
25
26 static int nl80211_init(struct nl80211_state *state)
27 {
28 int err;
29
30 state->nl_handle = nl_handle_alloc();
31 if (!state->nl_handle) {
32 fprintf(stderr, "Failed to allocate netlink handle.\n");
33 return -ENOMEM;
34 }
35
36 if (genl_connect(state->nl_handle)) {
37 fprintf(stderr, "Failed to connect to generic netlink.\n");
38 err = -ENOLINK;
39 goto out_handle_destroy;
40 }
41
42 state->nl_cache = genl_ctrl_alloc_cache(state->nl_handle);
43 if (!state->nl_cache) {
44 fprintf(stderr, "Failed to allocate generic netlink cache.\n");
45 err = -ENOMEM;
46 goto out_handle_destroy;
47 }
48
49 state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
50 if (!state->nl80211) {
51 fprintf(stderr, "nl80211 not found.\n");
52 err = -ENOENT;
53 goto out_cache_free;
54 }
55
56 return 0;
57
58 out_cache_free:
59 nl_cache_free(state->nl_cache);
60 out_handle_destroy:
61 nl_handle_destroy(state->nl_handle);
62 return err;
63 }
64
65 static void nl80211_cleanup(struct nl80211_state *state)
66 {
67 genl_family_put(state->nl80211);
68 nl_cache_free(state->nl_cache);
69 nl_handle_destroy(state->nl_handle);
70 }
71
72 static void usage(const char *argv0)
73 {
74 struct cmd *cmd;
75
76 fprintf(stderr, "Usage:\t%s help\n", argv0);
77 for (cmd = &__start___cmd; cmd < &__stop___cmd; cmd++) {
78 switch (cmd->idby) {
79 case CIB_NONE:
80 fprintf(stderr, "\t%s ", argv0);
81 /* fall through */
82 case CIB_PHY:
83 if (cmd->idby == CIB_PHY)
84 fprintf(stderr, "\t%s phy <phyname> ", argv0);
85 /* fall through */
86 case CIB_NETDEV:
87 if (cmd->idby == CIB_NETDEV)
88 fprintf(stderr, "\t%s dev <devname> ", argv0);
89 if (cmd->section)
90 fprintf(stderr, "%s ", cmd->section);
91 fprintf(stderr, "%s", cmd->name);
92 if (cmd->args)
93 fprintf(stderr, " %s", cmd->args);
94 fprintf(stderr, "\n");
95 break;
96 }
97 }
98 }
99
100 static int phy_lookup(char *name)
101 {
102 char buf[200];
103 int fd, pos;
104
105 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
106
107 fd = open(buf, O_RDONLY);
108 pos = read(fd, buf, sizeof(buf) - 1);
109 if (pos < 0)
110 return -1;
111 buf[pos] = '\0';
112 return atoi(buf);
113 }
114
115 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
116 void *arg)
117 {
118 int *ret = arg;
119 *ret = err->error;
120 return NL_STOP;
121 }
122
123 static int wait_handler(struct nl_msg *msg, void *arg)
124 {
125 int *ret = arg;
126 *ret = 0;
127 return NL_STOP;
128 }
129
130 static int handle_cmd(struct nl80211_state *state,
131 enum command_identify_by idby,
132 int argc, char **argv)
133 {
134 struct cmd *cmd;
135 struct nl_cb *cb = NULL;
136 struct nl_msg *msg;
137 int devidx = 0;
138 int err;
139 const char *command, *section;
140
141 if (argc <= 1 && idby != CIB_NONE)
142 return 1;
143
144 switch (idby) {
145 case CIB_PHY:
146 devidx = phy_lookup(*argv);
147 argc--;
148 argv++;
149 break;
150 case CIB_NETDEV:
151 devidx = if_nametoindex(*argv);
152 argc--;
153 argv++;
154 break;
155 default:
156 break;
157 }
158
159 section = command = *argv;
160 argc--;
161 argv++;
162
163 for (cmd = &__start___cmd; cmd < &__stop___cmd; cmd++) {
164 if (cmd->idby != idby)
165 continue;
166 if (cmd->section) {
167 if (strcmp(cmd->section, section))
168 continue;
169 /* this is a bit icky ... */
170 if (command == section) {
171 if (argc <= 0)
172 return 1;
173 command = *argv;
174 argc--;
175 argv++;
176 }
177 } else if (section != command)
178 continue;
179 if (strcmp(cmd->name, command))
180 continue;
181 if (argc && !cmd->args)
182 continue;
183 break;
184 }
185
186 if (cmd == &__stop___cmd)
187 return 1;
188
189 msg = nlmsg_alloc();
190 if (!msg) {
191 fprintf(stderr, "failed to allocate netlink message\n");
192 return 2;
193 }
194
195 cb = nl_cb_alloc(NL_CB_CUSTOM);
196 if (!cb) {
197 fprintf(stderr, "failed to allocate netlink callbacks\n");
198 err = 2;
199 goto out_free_msg;
200 }
201
202 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
203 cmd->nl_msg_flags, cmd->cmd, 0);
204
205 switch (idby) {
206 case CIB_PHY:
207 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
208 break;
209 case CIB_NETDEV:
210 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
211 break;
212 default:
213 break;
214 }
215
216 err = cmd->handler(cb, msg, argc, argv);
217 if (err)
218 goto out;
219
220 err = nl_send_auto_complete(state->nl_handle, msg);
221 if (err < 0)
222 goto out;
223
224 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
225 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, wait_handler, &err);
226
227 err = 1;
228 nl_recvmsgs(state->nl_handle, cb);
229
230 if (err == 1)
231 err = nl_wait_for_ack(state->nl_handle);
232 out:
233 nl_cb_put(cb);
234 out_free_msg:
235 nlmsg_free(msg);
236 return err;
237 nla_put_failure:
238 fprintf(stderr, "building message failed\n");
239 return 2;
240 }
241
242 int main(int argc, char **argv)
243 {
244 struct nl80211_state nlstate;
245 int err;
246 const char *argv0;
247
248 err = nl80211_init(&nlstate);
249 if (err)
250 return 1;
251
252 /* strip off self */
253 argc--;
254 argv0 = *argv++;
255
256 if (argc == 0 || strcmp(*argv, "help") == 0) {
257 usage(argv0);
258 goto out;
259 }
260
261 if (strcmp(*argv, "dev") == 0) {
262 argc--;
263 argv++;
264 err = handle_cmd(&nlstate, CIB_NETDEV, argc, argv);
265 } else if (strcmp(*argv, "phy") == 0) {
266 argc--;
267 argv++;
268 err = handle_cmd(&nlstate, CIB_PHY, argc, argv);
269 } else
270 err = handle_cmd(&nlstate, CIB_NONE, argc, argv);
271
272 if (err == 1)
273 usage(argv0);
274 if (err < 0)
275 fprintf(stderr, "command failed: %s\n", strerror(-err));
276
277 out:
278 nl80211_cleanup(&nlstate);
279
280 return err;
281 }