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
, struct nl_cb
*cb
,
13 struct nl_msg
*msg
, int argc
, char **argv
,
17 unsigned char bssid
[6];
24 NLA_PUT(msg
, NL80211_ATTR_SSID
, strlen(argv
[0]), argv
[0]);
30 freq
= strtoul(argv
[0], &end
, 10);
32 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, freq
);
40 if (mac_addr_a2n(bssid
, argv
[0]) == 0) {
41 NLA_PUT(msg
, NL80211_ATTR_MAC
, 6, bssid
);
50 if (strcmp(*argv
, "key") != 0 && strcmp(*argv
, "keys") != 0)
56 return parse_keys(msg
, argv
, argc
);
61 static int disconnect(struct nl80211_state
*state
,
64 int argc
, char **argv
,
69 TOPLEVEL(disconnect
, NULL
,
70 NL80211_CMD_DISCONNECT
, 0, CIB_NETDEV
, disconnect
,
71 "Disconnect from the current network.");
73 static int iw_connect(struct nl80211_state
*state
, struct nl_cb
*cb
,
74 struct nl_msg
*msg
, int argc
, char **argv
,
77 char **conn_argv
, *dev
= argv
[0];
78 static const __u32 cmds
[] = {
81 struct print_event_args printargs
= { };
86 /* strip "wlan0 connect" */
91 if (argc
&& strcmp(argv
[0], "-w") == 0) {
97 err
= __prepare_listen_events(state
);
101 conn_argc
= 3 + argc
;
102 conn_argv
= calloc(conn_argc
, sizeof(*conn_argv
));
107 conn_argv
[1] = "connect";
108 conn_argv
[2] = "establish";
109 for (i
= 0; i
< argc
; i
++)
110 conn_argv
[i
+ 3] = argv
[i
];
111 err
= handle_cmd(state
, id
, conn_argc
, conn_argv
);
120 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
122 * This code has a bug:
124 * It is possible for a connect result message from another
125 * connect attempt to be processed here first, because we
126 * start listening to the multicast group before starting
127 * our own connect request, which may succeed but we get a
128 * fail message from a previous attempt that raced with us,
131 * The only proper way to fix this would be to listen to events
132 * before sending the command, and for the kernel to send the
133 * connect request or a cookie along with the event, so that you
134 * can match up whether the connect _you_ requested was finished
137 * Alas, the kernel doesn't do that (yet).
140 __do_listen_events(state
, ARRAY_SIZE(cmds
), cmds
, &printargs
);
143 TOPLEVEL(connect
, "[-w] <SSID> [<freq in MHz>] [<bssid>] [key 0:abcde d:1:6162636465]",
144 0, 0, CIB_NETDEV
, iw_connect
,
145 "Join the network with the given SSID (and frequency, BSSID).\n"
146 "With -w, wait for the connect to finish or fail.");
147 HIDDEN(connect
, establish
, "", NL80211_CMD_CONNECT
, 0, CIB_NETDEV
, iw_conn
);
149 static int iw_auth(struct nl80211_state
*state
, struct nl_cb
*cb
,
150 struct nl_msg
*msg
, int argc
, char **argv
,
154 unsigned char bssid
[6];
156 bool need_key
= false;
162 NLA_PUT(msg
, NL80211_ATTR_SSID
, strlen(argv
[0]), argv
[0]);
167 if (mac_addr_a2n(bssid
, argv
[0]) == 0) {
168 NLA_PUT(msg
, NL80211_ATTR_MAC
, 6, bssid
);
176 if (strcmp(argv
[0], "open") == 0) {
177 NLA_PUT_U32(msg
, NL80211_ATTR_AUTH_TYPE
,
178 NL80211_AUTHTYPE_OPEN_SYSTEM
);
179 } else if (strcmp(argv
[0], "shared") == 0) {
180 NLA_PUT_U32(msg
, NL80211_ATTR_AUTH_TYPE
,
181 NL80211_AUTHTYPE_SHARED_KEY
);
189 freq
= strtoul(argv
[0], &end
, 10);
191 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, freq
);
198 if (!argc
&& need_key
)
200 if (argc
&& !need_key
)
205 if (strcmp(*argv
, "key") != 0 && strcmp(*argv
, "keys") != 0)
211 return parse_keys(msg
, argv
, argc
);
216 TOPLEVEL(auth
, "<SSID> <bssid> <type:open|shared> <freq in MHz> [key 0:abcde d:1:6162636465]",
217 NL80211_CMD_AUTHENTICATE
, 0, CIB_NETDEV
, iw_auth
,
218 "Authenticate with the given network.\n");