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>
12 static int iw_conn(struct nl80211_state
*state
,
13 struct nl_msg
*msg
, int argc
, char **argv
,
17 unsigned char bssid
[6];
18 bool need_key
= false;
26 NLA_PUT(msg
, NL80211_ATTR_SSID
, strlen(argv
[0]), argv
[0]);
32 freq
= strtoul(argv
[0], &end
, 10);
34 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, freq
);
42 if (mac_addr_a2n(bssid
, argv
[0]) == 0) {
43 NLA_PUT(msg
, NL80211_ATTR_MAC
, 6, bssid
);
52 if (strcmp(*argv
, "auth") == 0) {
59 if (strcmp(argv
[0], "open") == 0) {
60 NLA_PUT_U32(msg
, NL80211_ATTR_AUTH_TYPE
,
61 NL80211_AUTHTYPE_OPEN_SYSTEM
);
62 } else if (strcmp(argv
[0], "shared") == 0) {
63 NLA_PUT_U32(msg
, NL80211_ATTR_AUTH_TYPE
,
64 NL80211_AUTHTYPE_SHARED_KEY
);
74 if (need_key
&& !argc
)
77 if (argc
&& strcmp(*argv
, "key") != 0 && strcmp(*argv
, "keys") != 0)
83 ret
= parse_keys(msg
, &argv
, &argc
);
90 if (!strcmp(*argv
, "mfp:req"))
91 NLA_PUT_U32(msg
, NL80211_ATTR_USE_MFP
, NL80211_MFP_REQUIRED
);
92 else if (!strcmp(*argv
, "mfp:opt"))
93 NLA_PUT_U32(msg
, NL80211_ATTR_USE_MFP
, NL80211_MFP_OPTIONAL
);
94 else if (!strcmp(*argv
, "mfp:no"))
95 NLA_PUT_U32(msg
, NL80211_ATTR_USE_MFP
, NL80211_MFP_NO
);
105 static int disconnect(struct nl80211_state
*state
,
107 int argc
, char **argv
,
112 TOPLEVEL(disconnect
, NULL
,
113 NL80211_CMD_DISCONNECT
, 0, CIB_NETDEV
, disconnect
,
114 "Disconnect from the current network.");
116 static int iw_connect(struct nl80211_state
*state
,
117 struct nl_msg
*msg
, int argc
, char **argv
,
120 char **conn_argv
, *dev
= argv
[0];
121 static const __u32 cmds
[] = {
124 struct print_event_args printargs
= { };
129 /* strip "wlan0 connect" */
134 if (argc
&& strcmp(argv
[0], "-w") == 0) {
140 err
= __prepare_listen_events(state
);
144 conn_argc
= 3 + argc
;
145 conn_argv
= calloc(conn_argc
, sizeof(*conn_argv
));
150 conn_argv
[1] = "connect";
151 conn_argv
[2] = "establish";
152 for (i
= 0; i
< argc
; i
++)
153 conn_argv
[i
+ 3] = argv
[i
];
154 err
= handle_cmd(state
, id
, conn_argc
, conn_argv
);
163 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
165 * This code has a bug:
167 * It is possible for a connect result message from another
168 * connect attempt to be processed here first, because we
169 * start listening to the multicast group before starting
170 * our own connect request, which may succeed but we get a
171 * fail message from a previous attempt that raced with us,
174 * The only proper way to fix this would be to listen to events
175 * before sending the command, and for the kernel to send the
176 * connect request or a cookie along with the event, so that you
177 * can match up whether the connect _you_ requested was finished
180 * Alas, the kernel doesn't do that (yet).
183 __do_listen_events(state
,
184 ARRAY_SIZE(cmds
), cmds
,
185 ARRAY_SIZE(cmds
), cmds
,
189 TOPLEVEL(connect
, "[-w] <SSID> [<freq in MHz>] [<bssid>] [auth open|shared] [key 0:abcde d:1:6162636465] [mfp:req/opt/no]",
190 0, 0, CIB_NETDEV
, iw_connect
,
191 "Join the network with the given SSID (and frequency, BSSID).\n"
192 "With -w, wait for the connect to finish or fail.");
193 HIDDEN(connect
, establish
, "", NL80211_CMD_CONNECT
, 0, CIB_NETDEV
, iw_conn
);
195 static int iw_auth(struct nl80211_state
*state
,
196 struct nl_msg
*msg
, int argc
, char **argv
,
200 unsigned char bssid
[6];
202 bool need_key
= false;
208 NLA_PUT(msg
, NL80211_ATTR_SSID
, strlen(argv
[0]), argv
[0]);
213 if (mac_addr_a2n(bssid
, argv
[0]) == 0) {
214 NLA_PUT(msg
, NL80211_ATTR_MAC
, 6, bssid
);
222 if (strcmp(argv
[0], "open") == 0) {
223 NLA_PUT_U32(msg
, NL80211_ATTR_AUTH_TYPE
,
224 NL80211_AUTHTYPE_OPEN_SYSTEM
);
225 } else if (strcmp(argv
[0], "shared") == 0) {
226 NLA_PUT_U32(msg
, NL80211_ATTR_AUTH_TYPE
,
227 NL80211_AUTHTYPE_SHARED_KEY
);
235 freq
= strtoul(argv
[0], &end
, 10);
237 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, freq
);
244 if (!argc
&& need_key
)
246 if (argc
&& !need_key
)
251 if (strcmp(*argv
, "key") != 0 && strcmp(*argv
, "keys") != 0)
257 return parse_keys(msg
, &argv
, &argc
);
262 TOPLEVEL(auth
, "<SSID> <bssid> <type:open|shared> <freq in MHz> [key 0:abcde d:1:6162636465]",
263 NL80211_CMD_AUTHENTICATE
, 0, CIB_NETDEV
, iw_auth
,
264 "Authenticate with the given network.\n");