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