]> git.ipfire.org Git - thirdparty/iw.git/blame - connect.c
update nl80211.h
[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
7fabd346
JB
12static int iw_conn(struct nl80211_state *state, struct nl_cb *cb,
13 struct nl_msg *msg, int argc, char **argv)
99dde7aa
JB
14{
15 char *end;
51e9bd80 16 unsigned char bssid[6];
1e03690e 17 int freq;
99dde7aa
JB
18
19 if (argc < 1)
20 return 1;
21
22 /* SSID */
23 NLA_PUT(msg, NL80211_ATTR_SSID, strlen(argv[0]), argv[0]);
24 argv++;
25 argc--;
26
27 /* freq */
28 if (argc) {
29 freq = strtoul(argv[0], &end, 10);
30 if (*end == '\0') {
31 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
32 argv++;
33 argc--;
34 }
35 }
36
37 /* bssid */
38 if (argc) {
51e9bd80
JB
39 if (mac_addr_a2n(bssid, argv[0]) == 0) {
40 NLA_PUT(msg, NL80211_ATTR_MAC, 6, bssid);
41 argv++;
42 argc--;
43 }
99dde7aa
JB
44 }
45
1e03690e
JB
46 if (!argc)
47 return 0;
99dde7aa 48
1e03690e
JB
49 if (strcmp(*argv, "key") != 0 && strcmp(*argv, "keys") != 0)
50 return 1;
ded1f078 51
1e03690e
JB
52 argv++;
53 argc--;
51e9bd80 54
1e03690e 55 return parse_keys(msg, argv, argc);
99dde7aa
JB
56 nla_put_failure:
57 return -ENOSPC;
58}
59
60static int disconnect(struct nl80211_state *state,
61 struct nl_cb *cb,
62 struct nl_msg *msg,
63 int argc, char **argv)
64{
65 return 0;
66}
67TOPLEVEL(disconnect, NULL,
68 NL80211_CMD_DISCONNECT, 0, CIB_NETDEV, disconnect,
69 "Disconnect from the current network.");
7fabd346
JB
70
71static int iw_connect(struct nl80211_state *state, struct nl_cb *cb,
72 struct nl_msg *msg, int argc, char **argv)
73{
74 char **conn_argv, *dev = argv[0];
75 static const __u32 cmds[] = {
76 NL80211_CMD_CONNECT,
77 };
78 struct print_event_args printargs = { };
79 int conn_argc, err;
80 bool wait = false;
81 int i;
82
83 /* strip "wlan0 connect" */
84 argc -= 2;
85 argv += 2;
86
87 /* check -w */
88 if (argc && strcmp(argv[0], "-w") == 0) {
89 wait = true;
90 argc--;
91 argv++;
92 }
93
94 conn_argc = 3 + argc;
95 conn_argv = calloc(conn_argc, sizeof(*conn_argv));
96 if (!conn_argv)
97 return -ENOMEM;
ee374e4d
JB
98
99 err = __prepare_listen_events(state);
100 if (err)
101 return err;
102
7fabd346 103 conn_argv[0] = dev;
4698bfc2 104 conn_argv[1] = "connect";
7fabd346
JB
105 conn_argv[2] = "establish";
106 for (i = 0; i < argc; i++)
107 conn_argv[i + 3] = argv[i];
108 err = handle_cmd(state, II_NETDEV, conn_argc, conn_argv);
109 free(conn_argv);
110 if (err)
111 return err;
112
113 if (!wait)
114 return 0;
115
116 /*
117 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
118 *
ee374e4d 119 * This code has a bug:
7fabd346 120 *
ee374e4d
JB
121 * It is possible for a connect result message from another
122 * connect attempt to be processed here first, because we
123 * start listening to the multicast group before starting
124 * our own connect request, which may succeed but we get a
125 * fail message from a previous attempt that raced with us,
126 * or similar.
7fabd346
JB
127 *
128 * The only proper way to fix this would be to listen to events
129 * before sending the command, and for the kernel to send the
ee374e4d
JB
130 * connect request or a cookie along with the event, so that you
131 * can match up whether the connect _you_ requested was finished
132 * or aborted.
7fabd346
JB
133 *
134 * Alas, the kernel doesn't do that (yet).
135 */
136
ee374e4d 137 __do_listen_events(state, ARRAY_SIZE(cmds), cmds, &printargs);
7fabd346
JB
138 return 0;
139}
140TOPLEVEL(connect, "[-w] <SSID> [<freq in MHz>] [<bssid>] [key 0:abcde d:1:6162636465]",
141 0, 0, CIB_NETDEV, iw_connect,
142 "Join the network with the given SSID (and frequency, BSSID).\n"
143 "With -w, wait for the connect to finish or fail.");
4698bfc2 144HIDDEN(connect, establish, "", NL80211_CMD_CONNECT, 0, CIB_NETDEV, iw_conn);