]>
Commit | Line | Data |
---|---|---|
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 | 12 | static 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 | |
86dbcee9 EG |
57 | ret = parse_keys(msg, argv, argc); |
58 | if (ret) | |
59 | return ret; | |
60 | ||
61 | argc -= 4; | |
62 | argv += 4; | |
63 | ||
64 | if (!argc) | |
65 | return 0; | |
66 | ||
67 | if (!strcmp(*argv, "mfp:req")) | |
68 | NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED); | |
69 | else if (!strcmp(*argv, "mfp:opt")) | |
70 | NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_OPTIONAL); | |
71 | else if (!strcmp(*argv, "mfp:no")) | |
72 | NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_NO); | |
73 | else | |
74 | return -EINVAL; | |
75 | ||
76 | return 0; | |
77 | ||
99dde7aa JB |
78 | nla_put_failure: |
79 | return -ENOSPC; | |
80 | } | |
81 | ||
82 | static int disconnect(struct nl80211_state *state, | |
99dde7aa | 83 | struct nl_msg *msg, |
05514f95 JB |
84 | int argc, char **argv, |
85 | enum id_input id) | |
99dde7aa JB |
86 | { |
87 | return 0; | |
88 | } | |
89 | TOPLEVEL(disconnect, NULL, | |
90 | NL80211_CMD_DISCONNECT, 0, CIB_NETDEV, disconnect, | |
91 | "Disconnect from the current network."); | |
7fabd346 | 92 | |
34b23014 | 93 | static int iw_connect(struct nl80211_state *state, |
05514f95 JB |
94 | struct nl_msg *msg, int argc, char **argv, |
95 | enum id_input id) | |
7fabd346 JB |
96 | { |
97 | char **conn_argv, *dev = argv[0]; | |
98 | static const __u32 cmds[] = { | |
99 | NL80211_CMD_CONNECT, | |
100 | }; | |
101 | struct print_event_args printargs = { }; | |
102 | int conn_argc, err; | |
103 | bool wait = false; | |
104 | int i; | |
105 | ||
106 | /* strip "wlan0 connect" */ | |
107 | argc -= 2; | |
108 | argv += 2; | |
109 | ||
110 | /* check -w */ | |
111 | if (argc && strcmp(argv[0], "-w") == 0) { | |
112 | wait = true; | |
113 | argc--; | |
114 | argv++; | |
115 | } | |
116 | ||
d8d294c4 JL |
117 | err = __prepare_listen_events(state); |
118 | if (err) | |
119 | return err; | |
120 | ||
7fabd346 JB |
121 | conn_argc = 3 + argc; |
122 | conn_argv = calloc(conn_argc, sizeof(*conn_argv)); | |
123 | if (!conn_argv) | |
124 | return -ENOMEM; | |
ee374e4d | 125 | |
7fabd346 | 126 | conn_argv[0] = dev; |
4698bfc2 | 127 | conn_argv[1] = "connect"; |
7fabd346 JB |
128 | conn_argv[2] = "establish"; |
129 | for (i = 0; i < argc; i++) | |
130 | conn_argv[i + 3] = argv[i]; | |
75f4204c | 131 | err = handle_cmd(state, id, conn_argc, conn_argv); |
7fabd346 JB |
132 | free(conn_argv); |
133 | if (err) | |
134 | return err; | |
135 | ||
136 | if (!wait) | |
137 | return 0; | |
138 | ||
139 | /* | |
140 | * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION | |
141 | * | |
ee374e4d | 142 | * This code has a bug: |
7fabd346 | 143 | * |
ee374e4d JB |
144 | * It is possible for a connect result message from another |
145 | * connect attempt to be processed here first, because we | |
146 | * start listening to the multicast group before starting | |
147 | * our own connect request, which may succeed but we get a | |
148 | * fail message from a previous attempt that raced with us, | |
149 | * or similar. | |
7fabd346 JB |
150 | * |
151 | * The only proper way to fix this would be to listen to events | |
152 | * before sending the command, and for the kernel to send the | |
ee374e4d JB |
153 | * connect request or a cookie along with the event, so that you |
154 | * can match up whether the connect _you_ requested was finished | |
155 | * or aborted. | |
7fabd346 JB |
156 | * |
157 | * Alas, the kernel doesn't do that (yet). | |
158 | */ | |
159 | ||
ee374e4d | 160 | __do_listen_events(state, ARRAY_SIZE(cmds), cmds, &printargs); |
7fabd346 JB |
161 | return 0; |
162 | } | |
86dbcee9 | 163 | TOPLEVEL(connect, "[-w] <SSID> [<freq in MHz>] [<bssid>] [key 0:abcde d:1:6162636465] [mfp:req/opt/no]", |
7fabd346 JB |
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."); | |
4698bfc2 | 167 | HIDDEN(connect, establish, "", NL80211_CMD_CONNECT, 0, CIB_NETDEV, iw_conn); |
5727fe03 | 168 | |
34b23014 | 169 | static int iw_auth(struct nl80211_state *state, |
5727fe03 JB |
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"); |