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