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