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];
25 NLA_PUT(msg
, NL80211_ATTR_SSID
, strlen(argv
[0]), argv
[0]);
31 freq
= strtoul(argv
[0], &end
, 10);
33 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, freq
);
41 if (mac_addr_a2n(bssid
, argv
[0]) == 0) {
42 NLA_PUT(msg
, NL80211_ATTR_MAC
, 6, bssid
);
51 if (strcmp(*argv
, "key") != 0 && strcmp(*argv
, "keys") != 0)
57 ret
= parse_keys(msg
, &argv
, &argc
);
64 if (!strcmp(*argv
, "mfp:req"))
65 NLA_PUT_U32(msg
, NL80211_ATTR_USE_MFP
, NL80211_MFP_REQUIRED
);
66 else if (!strcmp(*argv
, "mfp:opt"))
67 NLA_PUT_U32(msg
, NL80211_ATTR_USE_MFP
, NL80211_MFP_OPTIONAL
);
68 else if (!strcmp(*argv
, "mfp:no"))
69 NLA_PUT_U32(msg
, NL80211_ATTR_USE_MFP
, NL80211_MFP_NO
);
79 static int disconnect(struct nl80211_state
*state
,
81 int argc
, char **argv
,
86 TOPLEVEL(disconnect
, NULL
,
87 NL80211_CMD_DISCONNECT
, 0, CIB_NETDEV
, disconnect
,
88 "Disconnect from the current network.");
90 static int iw_connect(struct nl80211_state
*state
,
91 struct nl_msg
*msg
, int argc
, char **argv
,
94 char **conn_argv
, *dev
= argv
[0];
95 static const __u32 cmds
[] = {
98 struct print_event_args printargs
= { };
103 /* strip "wlan0 connect" */
108 if (argc
&& strcmp(argv
[0], "-w") == 0) {
114 err
= __prepare_listen_events(state
);
118 conn_argc
= 3 + argc
;
119 conn_argv
= calloc(conn_argc
, sizeof(*conn_argv
));
124 conn_argv
[1] = "connect";
125 conn_argv
[2] = "establish";
126 for (i
= 0; i
< argc
; i
++)
127 conn_argv
[i
+ 3] = argv
[i
];
128 err
= handle_cmd(state
, id
, conn_argc
, conn_argv
);
137 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
139 * This code has a bug:
141 * It is possible for a connect result message from another
142 * connect attempt to be processed here first, because we
143 * start listening to the multicast group before starting
144 * our own connect request, which may succeed but we get a
145 * fail message from a previous attempt that raced with us,
148 * The only proper way to fix this would be to listen to events
149 * before sending the command, and for the kernel to send the
150 * connect request or a cookie along with the event, so that you
151 * can match up whether the connect _you_ requested was finished
154 * Alas, the kernel doesn't do that (yet).
157 __do_listen_events(state
,
158 ARRAY_SIZE(cmds
), cmds
,
159 ARRAY_SIZE(cmds
), cmds
,
163 TOPLEVEL(connect
, "[-w] <SSID> [<freq in MHz>] [<bssid>] [key 0:abcde d:1:6162636465] [mfp:req/opt/no]",
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.");
167 HIDDEN(connect
, establish
, "", NL80211_CMD_CONNECT
, 0, CIB_NETDEV
, iw_conn
);
169 static int iw_auth(struct nl80211_state
*state
,
170 struct nl_msg
*msg
, int argc
, char **argv
,
174 unsigned char bssid
[6];
176 bool need_key
= false;
182 NLA_PUT(msg
, NL80211_ATTR_SSID
, strlen(argv
[0]), argv
[0]);
187 if (mac_addr_a2n(bssid
, argv
[0]) == 0) {
188 NLA_PUT(msg
, NL80211_ATTR_MAC
, 6, bssid
);
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
);
209 freq
= strtoul(argv
[0], &end
, 10);
211 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, freq
);
218 if (!argc
&& need_key
)
220 if (argc
&& !need_key
)
225 if (strcmp(*argv
, "key") != 0 && strcmp(*argv
, "keys") != 0)
231 return parse_keys(msg
, &argv
, &argc
);
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");