I've developed in C language, some function for managing wifi programmatically, on my embedded device. The code below shows the function that I use to associate and connect my device to an open AP (with no auth encryption).
static int ap_conn() {
struct nl_msg *msg = nlmsg_alloc();
int if_index = if_nametoindex("wlan0"); // Use this wireless interface for scanning.
// Open socket to kernel.
struct nl_sock *socket = nl_socket_alloc(); // Allocate new netlink socket in memory.
genl_connect(socket); // Create file descriptor and bind socket.
int driver_id = genl_ctrl_resolve(socket, "nl80211"); // Find the
nl80211 driver ID.
genlmsg_put(msg, 0, 0, driver_id, 0, (NLM_F_REQUEST | NLM_F_ACK), NL80211_CMD_CONNECT, 0);
nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_index); // Add message attribute, which interface to use.
nla_put(msg, NL80211_ATTR_SSID, strlen("Validator_Test"), "Validator_Test");
int ret = nl_send_auto_complete(socket, msg); // Send the message.
printf("NL80211_CMD_CONNECT sent %d bytes to the kernel.\n", ret);
ret = nl_recvmsgs_default(socket); // Retrieve the kernel's answer. callback_dump() prints SSIDs to stdout.
nlmsg_free(msg);
if (ret < 0) {
printf("ERROR: nl_recvmsgs_default() returned %d (%s).\n", ret, nl_geterror(-ret));
return ret;
}
nla_put_failure:
return -ENOSPC;
Now, I'm trying to do the same thing, but with an AP with WPA2 authentication. Unfortunately, there is little documentation for libnl. I've tried to add some command to the msg struct, that appears to be reasonable, like the following:
nla_put_32(msg, NL80211_ATTR_WPA_VERSIONS, NL80211_WPA_VERSION_2);
nla_put(msg, NL80211_ATTR_PMK, strlen("pass"), "pass");
But it doesn't work! The connection manager "iw" act in a similar way, but the code is not so easy to understand. Some of you can help me develop this? thank you all!!