]> git.ipfire.org Git - thirdparty/iw.git/blob - connect.c
info: macro-ify ext_feat_print()
[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 int freq;
19 int ret;
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) {
41 if (mac_addr_a2n(bssid, argv[0]) == 0) {
42 NLA_PUT(msg, NL80211_ATTR_MAC, 6, bssid);
43 argv++;
44 argc--;
45 }
46 }
47
48 if (!argc)
49 return 0;
50
51 if (strcmp(*argv, "key") != 0 && strcmp(*argv, "keys") != 0)
52 return 1;
53
54 argv++;
55 argc--;
56
57 ret = parse_keys(msg, &argv, &argc);
58 if (ret)
59 return ret;
60
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
75 nla_put_failure:
76 return -ENOSPC;
77 }
78
79 static int disconnect(struct nl80211_state *state,
80 struct nl_msg *msg,
81 int argc, char **argv,
82 enum id_input id)
83 {
84 return 0;
85 }
86 TOPLEVEL(disconnect, NULL,
87 NL80211_CMD_DISCONNECT, 0, CIB_NETDEV, disconnect,
88 "Disconnect from the current network.");
89
90 static int iw_connect(struct nl80211_state *state,
91 struct nl_msg *msg, int argc, char **argv,
92 enum id_input id)
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
114 err = __prepare_listen_events(state);
115 if (err)
116 return err;
117
118 conn_argc = 3 + argc;
119 conn_argv = calloc(conn_argc, sizeof(*conn_argv));
120 if (!conn_argv)
121 return -ENOMEM;
122
123 conn_argv[0] = dev;
124 conn_argv[1] = "connect";
125 conn_argv[2] = "establish";
126 for (i = 0; i < argc; i++)
127 conn_argv[i + 3] = argv[i];
128 err = handle_cmd(state, id, conn_argc, conn_argv);
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 *
139 * This code has a bug:
140 *
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.
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
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.
153 *
154 * Alas, the kernel doesn't do that (yet).
155 */
156
157 __do_listen_events(state,
158 ARRAY_SIZE(cmds), cmds,
159 ARRAY_SIZE(cmds), cmds,
160 &printargs);
161 return 0;
162 }
163 TOPLEVEL(connect, "[-w] <SSID> [<freq in MHz>] [<bssid>] [key 0:abcde d:1:6162636465] [mfp:req/opt/no]",
164 0, 0, CIB_NETDEV, iw_connect,
165 "Join the network with the given SSID (and frequency, BSSID).\n"
166 "With -w, wait for the connect to finish or fail.");
167 HIDDEN(connect, establish, "", NL80211_CMD_CONNECT, 0, CIB_NETDEV, iw_conn);
168
169 static int iw_auth(struct nl80211_state *state,
170 struct nl_msg *msg, int argc, char **argv,
171 enum id_input id)
172 {
173 char *end;
174 unsigned char bssid[6];
175 int freq;
176 bool need_key = false;
177
178 if (argc < 4)
179 return 1;
180
181 /* SSID */
182 NLA_PUT(msg, NL80211_ATTR_SSID, strlen(argv[0]), argv[0]);
183 argv++;
184 argc--;
185
186 /* bssid */
187 if (mac_addr_a2n(bssid, argv[0]) == 0) {
188 NLA_PUT(msg, NL80211_ATTR_MAC, 6, bssid);
189 argv++;
190 argc--;
191 } else {
192 return 1;
193 }
194
195 /* FIXME */
196 if (strcmp(argv[0], "open") == 0) {
197 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
198 NL80211_AUTHTYPE_OPEN_SYSTEM);
199 } else if (strcmp(argv[0], "shared") == 0) {
200 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
201 NL80211_AUTHTYPE_SHARED_KEY);
202 need_key = true;
203 } else {
204 return 1;
205 }
206 argv++;
207 argc--;
208
209 freq = strtoul(argv[0], &end, 10);
210 if (*end == '\0') {
211 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
212 argv++;
213 argc--;
214 } else {
215 return 1;
216 }
217
218 if (!argc && need_key)
219 return 1;
220 if (argc && !need_key)
221 return 1;
222 if (!argc)
223 return 0;
224
225 if (strcmp(*argv, "key") != 0 && strcmp(*argv, "keys") != 0)
226 return 1;
227
228 argv++;
229 argc--;
230
231 return parse_keys(msg, &argv, &argc);
232 nla_put_failure:
233 return -ENOSPC;
234 }
235
236 TOPLEVEL(auth, "<SSID> <bssid> <type:open|shared> <freq in MHz> [key 0:abcde d:1:6162636465]",
237 NL80211_CMD_AUTHENTICATE, 0, CIB_NETDEV, iw_auth,
238 "Authenticate with the given network.\n");