]> git.ipfire.org Git - thirdparty/iw.git/blame - connect.c
update nl80211.h
[thirdparty/iw.git] / connect.c
CommitLineData
99dde7aa
JB
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
34b23014 12static int iw_conn(struct nl80211_state *state,
05514f95
JB
13 struct nl_msg *msg, int argc, char **argv,
14 enum id_input id)
99dde7aa
JB
15{
16 char *end;
51e9bd80 17 unsigned char bssid[6];
a0f53c1d 18 bool need_key = false;
1e03690e 19 int freq;
86dbcee9 20 int ret;
99dde7aa
JB
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) {
51e9bd80
JB
42 if (mac_addr_a2n(bssid, argv[0]) == 0) {
43 NLA_PUT(msg, NL80211_ATTR_MAC, 6, bssid);
44 argv++;
45 argc--;
46 }
99dde7aa
JB
47 }
48
1e03690e
JB
49 if (!argc)
50 return 0;
99dde7aa 51
a0f53c1d
JZ
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)
1e03690e 78 return 1;
ded1f078 79
a1c9376a
CT
80 if (!argc)
81 return 0;
82
1e03690e
JB
83 argv++;
84 argc--;
51e9bd80 85
0e39f109 86 ret = parse_keys(msg, &argv, &argc);
86dbcee9
EG
87 if (ret)
88 return ret;
89
86dbcee9
EG
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
99dde7aa
JB
104 nla_put_failure:
105 return -ENOSPC;
106}
107
108static int disconnect(struct nl80211_state *state,
99dde7aa 109 struct nl_msg *msg,
05514f95
JB
110 int argc, char **argv,
111 enum id_input id)
99dde7aa
JB
112{
113 return 0;
114}
115TOPLEVEL(disconnect, NULL,
116 NL80211_CMD_DISCONNECT, 0, CIB_NETDEV, disconnect,
117 "Disconnect from the current network.");
7fabd346 118
34b23014 119static int iw_connect(struct nl80211_state *state,
05514f95
JB
120 struct nl_msg *msg, int argc, char **argv,
121 enum id_input id)
7fabd346
JB
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
d8d294c4
JL
143 err = __prepare_listen_events(state);
144 if (err)
145 return err;
146
7fabd346
JB
147 conn_argc = 3 + argc;
148 conn_argv = calloc(conn_argc, sizeof(*conn_argv));
149 if (!conn_argv)
150 return -ENOMEM;
ee374e4d 151
7fabd346 152 conn_argv[0] = dev;
4698bfc2 153 conn_argv[1] = "connect";
7fabd346
JB
154 conn_argv[2] = "establish";
155 for (i = 0; i < argc; i++)
156 conn_argv[i + 3] = argv[i];
75f4204c 157 err = handle_cmd(state, id, conn_argc, conn_argv);
7fabd346
JB
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 *
ee374e4d 168 * This code has a bug:
7fabd346 169 *
ee374e4d
JB
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.
7fabd346
JB
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
ee374e4d
JB
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.
7fabd346
JB
182 *
183 * Alas, the kernel doesn't do that (yet).
184 */
185
2e192cea
JB
186 __do_listen_events(state,
187 ARRAY_SIZE(cmds), cmds,
188 ARRAY_SIZE(cmds), cmds,
189 &printargs);
7fabd346
JB
190 return 0;
191}
a0f53c1d 192TOPLEVEL(connect, "[-w] <SSID> [<freq in MHz>] [<bssid>] [auth open|shared] [key 0:abcde d:1:6162636465] [mfp:req/opt/no]",
7fabd346
JB
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.");
4698bfc2 196HIDDEN(connect, establish, "", NL80211_CMD_CONNECT, 0, CIB_NETDEV, iw_conn);
5727fe03 197
34b23014 198static int iw_auth(struct nl80211_state *state,
5727fe03
JB
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
0e39f109 260 return parse_keys(msg, &argv, &argc);
5727fe03
JB
261 nla_put_failure:
262 return -ENOSPC;
263}
264
265TOPLEVEL(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");