]>
Commit | Line | Data |
---|---|---|
1 | #include <errno.h> | |
2 | ||
3 | #include <netlink/genl/genl.h> | |
4 | #include <netlink/genl/family.h> | |
5 | #include <netlink/genl/ctrl.h> | |
6 | #include <netlink/msg.h> | |
7 | #include <netlink/attr.h> | |
8 | ||
9 | #include "nl80211.h" | |
10 | #include "iw.h" | |
11 | ||
12 | static int iw_conn(struct nl80211_state *state, | |
13 | struct nl_msg *msg, int argc, char **argv, | |
14 | enum id_input id) | |
15 | { | |
16 | char *end; | |
17 | unsigned char bssid[6]; | |
18 | bool need_key = false; | |
19 | int freq; | |
20 | int ret; | |
21 | ||
22 | if (argc < 1) | |
23 | return 1; | |
24 | ||
25 | /* SSID */ | |
26 | NLA_PUT(msg, NL80211_ATTR_SSID, strlen(argv[0]), argv[0]); | |
27 | argv++; | |
28 | argc--; | |
29 | ||
30 | /* freq */ | |
31 | if (argc) { | |
32 | freq = strtoul(argv[0], &end, 10); | |
33 | if (*end == '\0') { | |
34 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq); | |
35 | argv++; | |
36 | argc--; | |
37 | } | |
38 | } | |
39 | ||
40 | /* bssid */ | |
41 | if (argc) { | |
42 | if (mac_addr_a2n(bssid, argv[0]) == 0) { | |
43 | NLA_PUT(msg, NL80211_ATTR_MAC, 6, bssid); | |
44 | argv++; | |
45 | argc--; | |
46 | } | |
47 | } | |
48 | ||
49 | if (!argc) | |
50 | return 0; | |
51 | ||
52 | if (strcmp(*argv, "auth") == 0) { | |
53 | argv++; | |
54 | argc--; | |
55 | ||
56 | if (!argc) | |
57 | return 1; | |
58 | ||
59 | if (strcmp(argv[0], "open") == 0) { | |
60 | NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, | |
61 | NL80211_AUTHTYPE_OPEN_SYSTEM); | |
62 | } else if (strcmp(argv[0], "shared") == 0) { | |
63 | NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, | |
64 | NL80211_AUTHTYPE_SHARED_KEY); | |
65 | need_key = true; | |
66 | } else { | |
67 | return 1; | |
68 | } | |
69 | ||
70 | argv++; | |
71 | argc--; | |
72 | } | |
73 | ||
74 | if (need_key && !argc) | |
75 | return 1; | |
76 | ||
77 | if (argc && strcmp(*argv, "key") != 0 && strcmp(*argv, "keys") != 0) | |
78 | return 1; | |
79 | ||
80 | if (!argc) | |
81 | return 0; | |
82 | ||
83 | argv++; | |
84 | argc--; | |
85 | ||
86 | ret = parse_keys(msg, &argv, &argc); | |
87 | if (ret) | |
88 | return ret; | |
89 | ||
90 | if (!argc) | |
91 | return 0; | |
92 | ||
93 | if (!strcmp(*argv, "mfp:req")) | |
94 | NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED); | |
95 | else if (!strcmp(*argv, "mfp:opt")) | |
96 | NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_OPTIONAL); | |
97 | else if (!strcmp(*argv, "mfp:no")) | |
98 | NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_NO); | |
99 | else | |
100 | return -EINVAL; | |
101 | ||
102 | return 0; | |
103 | ||
104 | nla_put_failure: | |
105 | return -ENOSPC; | |
106 | } | |
107 | ||
108 | static int disconnect(struct nl80211_state *state, | |
109 | struct nl_msg *msg, | |
110 | int argc, char **argv, | |
111 | enum id_input id) | |
112 | { | |
113 | return 0; | |
114 | } | |
115 | TOPLEVEL(disconnect, NULL, | |
116 | NL80211_CMD_DISCONNECT, 0, CIB_NETDEV, disconnect, | |
117 | "Disconnect from the current network."); | |
118 | ||
119 | static int iw_connect(struct nl80211_state *state, | |
120 | struct nl_msg *msg, int argc, char **argv, | |
121 | enum id_input id) | |
122 | { | |
123 | char **conn_argv, *dev = argv[0]; | |
124 | static const __u32 cmds[] = { | |
125 | NL80211_CMD_CONNECT, | |
126 | }; | |
127 | struct print_event_args printargs = { }; | |
128 | int conn_argc, err; | |
129 | bool wait = false; | |
130 | int i; | |
131 | ||
132 | /* strip "wlan0 connect" */ | |
133 | argc -= 2; | |
134 | argv += 2; | |
135 | ||
136 | /* check -w */ | |
137 | if (argc && strcmp(argv[0], "-w") == 0) { | |
138 | wait = true; | |
139 | argc--; | |
140 | argv++; | |
141 | } | |
142 | ||
143 | err = __prepare_listen_events(state); | |
144 | if (err) | |
145 | return err; | |
146 | ||
147 | conn_argc = 3 + argc; | |
148 | conn_argv = calloc(conn_argc, sizeof(*conn_argv)); | |
149 | if (!conn_argv) | |
150 | return -ENOMEM; | |
151 | ||
152 | conn_argv[0] = dev; | |
153 | conn_argv[1] = "connect"; | |
154 | conn_argv[2] = "establish"; | |
155 | for (i = 0; i < argc; i++) | |
156 | conn_argv[i + 3] = argv[i]; | |
157 | err = handle_cmd(state, id, conn_argc, conn_argv); | |
158 | free(conn_argv); | |
159 | if (err) | |
160 | return err; | |
161 | ||
162 | if (!wait) | |
163 | return 0; | |
164 | ||
165 | /* | |
166 | * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION | |
167 | * | |
168 | * This code has a bug: | |
169 | * | |
170 | * It is possible for a connect result message from another | |
171 | * connect attempt to be processed here first, because we | |
172 | * start listening to the multicast group before starting | |
173 | * our own connect request, which may succeed but we get a | |
174 | * fail message from a previous attempt that raced with us, | |
175 | * or similar. | |
176 | * | |
177 | * The only proper way to fix this would be to listen to events | |
178 | * before sending the command, and for the kernel to send the | |
179 | * connect request or a cookie along with the event, so that you | |
180 | * can match up whether the connect _you_ requested was finished | |
181 | * or aborted. | |
182 | * | |
183 | * Alas, the kernel doesn't do that (yet). | |
184 | */ | |
185 | ||
186 | __do_listen_events(state, | |
187 | ARRAY_SIZE(cmds), cmds, | |
188 | ARRAY_SIZE(cmds), cmds, | |
189 | &printargs); | |
190 | return 0; | |
191 | } | |
192 | TOPLEVEL(connect, "[-w] <SSID> [<freq in MHz>] [<bssid>] [auth open|shared] [key 0:abcde d:1:6162636465] [mfp:req/opt/no]", | |
193 | 0, 0, CIB_NETDEV, iw_connect, | |
194 | "Join the network with the given SSID (and frequency, BSSID).\n" | |
195 | "With -w, wait for the connect to finish or fail."); | |
196 | HIDDEN(connect, establish, "", NL80211_CMD_CONNECT, 0, CIB_NETDEV, iw_conn); | |
197 | ||
198 | static int iw_auth(struct nl80211_state *state, | |
199 | struct nl_msg *msg, int argc, char **argv, | |
200 | enum id_input id) | |
201 | { | |
202 | char *end; | |
203 | unsigned char bssid[6]; | |
204 | int freq; | |
205 | bool need_key = false; | |
206 | ||
207 | if (argc < 4) | |
208 | return 1; | |
209 | ||
210 | /* SSID */ | |
211 | NLA_PUT(msg, NL80211_ATTR_SSID, strlen(argv[0]), argv[0]); | |
212 | argv++; | |
213 | argc--; | |
214 | ||
215 | /* bssid */ | |
216 | if (mac_addr_a2n(bssid, argv[0]) == 0) { | |
217 | NLA_PUT(msg, NL80211_ATTR_MAC, 6, bssid); | |
218 | argv++; | |
219 | argc--; | |
220 | } else { | |
221 | return 1; | |
222 | } | |
223 | ||
224 | /* FIXME */ | |
225 | if (strcmp(argv[0], "open") == 0) { | |
226 | NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, | |
227 | NL80211_AUTHTYPE_OPEN_SYSTEM); | |
228 | } else if (strcmp(argv[0], "shared") == 0) { | |
229 | NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, | |
230 | NL80211_AUTHTYPE_SHARED_KEY); | |
231 | need_key = true; | |
232 | } else { | |
233 | return 1; | |
234 | } | |
235 | argv++; | |
236 | argc--; | |
237 | ||
238 | freq = strtoul(argv[0], &end, 10); | |
239 | if (*end == '\0') { | |
240 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq); | |
241 | argv++; | |
242 | argc--; | |
243 | } else { | |
244 | return 1; | |
245 | } | |
246 | ||
247 | if (!argc && need_key) | |
248 | return 1; | |
249 | if (argc && !need_key) | |
250 | return 1; | |
251 | if (!argc) | |
252 | return 0; | |
253 | ||
254 | if (strcmp(*argv, "key") != 0 && strcmp(*argv, "keys") != 0) | |
255 | return 1; | |
256 | ||
257 | argv++; | |
258 | argc--; | |
259 | ||
260 | return parse_keys(msg, &argv, &argc); | |
261 | nla_put_failure: | |
262 | return -ENOSPC; | |
263 | } | |
264 | ||
265 | TOPLEVEL(auth, "<SSID> <bssid> <type:open|shared> <freq in MHz> [key 0:abcde d:1:6162636465]", | |
266 | NL80211_CMD_AUTHENTICATE, 0, CIB_NETDEV, iw_auth, | |
267 | "Authenticate with the given network.\n"); |