]> git.ipfire.org Git - thirdparty/iw.git/blame - connect.c
split out multicast group adding
[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;
98 conn_argv[0] = dev;
4698bfc2 99 conn_argv[1] = "connect";
7fabd346
JB
100 conn_argv[2] = "establish";
101 for (i = 0; i < argc; i++)
102 conn_argv[i + 3] = argv[i];
103 err = handle_cmd(state, II_NETDEV, conn_argc, conn_argv);
104 free(conn_argv);
105 if (err)
106 return err;
107
108 if (!wait)
109 return 0;
110
111 /*
112 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
113 *
114 * This code has a bug, which requires creating a separate
115 * nl80211 socket to fix:
116 * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or
117 * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel
118 * before (!) we listen to it, because we only start listening
119 * after we send our scan request.
120 *
121 * Doing it the other way around has a race condition as well,
122 * if you first open the events socket you may get a notification
123 * for a previous scan.
124 *
125 * The only proper way to fix this would be to listen to events
126 * before sending the command, and for the kernel to send the
127 * connect request along with the event, so that you can match
128 * up whether the connect _you_ requested was finished or aborted.
129 *
130 * Alas, the kernel doesn't do that (yet).
131 */
132
133 __listen_events(state, ARRAY_SIZE(cmds), cmds, &printargs);
134 return 0;
135}
136TOPLEVEL(connect, "[-w] <SSID> [<freq in MHz>] [<bssid>] [key 0:abcde d:1:6162636465]",
137 0, 0, CIB_NETDEV, iw_connect,
138 "Join the network with the given SSID (and frequency, BSSID).\n"
139 "With -w, wait for the connect to finish or fail.");
4698bfc2 140HIDDEN(connect, establish, "", NL80211_CMD_CONNECT, 0, CIB_NETDEV, iw_conn);