blob: ad7cf7957611c60ef77d804415620852e0a9a3b1 [file] [log] [blame]
#include <errno.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/family.h>
#include <netlink/genl/ctrl.h>
#include <linux/rtnetlink.h>
#include <netpacket/packet.h>
#include <linux/filter.h>
#include <linux/errqueue.h>
#include <linux/pkt_sched.h>
#include <netlink/object-api.h>
#include <netlink/netlink.h>
#include <netlink/socket.h>
#include <netlink/attr.h>
#include <netlink/handlers.h>
#include <netlink/msg.h>
#include <dirent.h>
#include <net/if.h>
#include "sync.h"
#define LOG_TAG "WifiHAL"
#include <utils/Log.h>
#include "wifi_hal.h"
#include "common.h"
#include "cpp_bindings.h"
#define WIFI_HAL_CMD_SOCK_PORT 644
#define WIFI_HAL_EVENT_SOCK_PORT 645
#define FEATURE_SET 0
#define FEATURE_SET_MATRIX 1
#define ATTR_NODFS_VALUE 3
static void internal_event_handler(wifi_handle handle, int events);
static int internal_no_seq_check(nl_msg *msg, void *arg);
static int internal_valid_message_handler(nl_msg *msg, void *arg);
static int wifi_get_multicast_id(wifi_handle handle, const char *name, const char *group);
static int wifi_add_membership(wifi_handle handle, const char *group);
static wifi_error wifi_init_interfaces(wifi_handle handle);
typedef enum wifi_attr {
ANDR_WIFI_ATTRIBUTE_NUM_FEATURE_SET,
ANDR_WIFI_ATTRIBUTE_FEATURE_SET,
ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI
} wifi_attr_t;
/* Initialize/Cleanup */
void wifi_socket_set_local_port(struct nl_sock *sock, uint32_t port)
{
uint32_t pid = getpid() & 0x3FFFFF;
nl_socket_set_local_port(sock, pid + (port << 22));
}
static nl_sock * wifi_create_nl_socket(int port)
{
ALOGI("Creating socket");
struct nl_sock *sock = nl_socket_alloc();
if (sock == NULL) {
ALOGE("Could not create handle");
return NULL;
}
wifi_socket_set_local_port(sock, port);
struct sockaddr *addr = NULL;
// ALOGI("sizeof(sockaddr) = %d, sizeof(sockaddr_nl) = %d", sizeof(*addr), sizeof(*addr_nl));
ALOGI("Connecting socket");
if (nl_connect(sock, NETLINK_GENERIC)) {
ALOGE("Could not connect handle");
nl_socket_free(sock);
return NULL;
}
ALOGI("Making socket nonblocking");
/*
if (nl_socket_set_nonblocking(sock)) {
ALOGE("Could make socket non-blocking");
nl_socket_free(sock);
return NULL;
}
*/
return sock;
}
wifi_error wifi_initialize(wifi_handle *handle)
{
srand(getpid());
ALOGI("Initializing wifi");
hal_info *info = (hal_info *)malloc(sizeof(hal_info));
if (info == NULL) {
ALOGE("Could not allocate hal_info");
return WIFI_ERROR_UNKNOWN;
}
memset(info, 0, sizeof(*info));
ALOGI("Creating socket");
struct nl_sock *cmd_sock = wifi_create_nl_socket(WIFI_HAL_CMD_SOCK_PORT);
if (cmd_sock == NULL) {
ALOGE("Could not create handle");
return WIFI_ERROR_UNKNOWN;
}
struct nl_sock *event_sock = wifi_create_nl_socket(WIFI_HAL_EVENT_SOCK_PORT);
if (event_sock == NULL) {
ALOGE("Could not create handle");
nl_socket_free(cmd_sock);
return WIFI_ERROR_UNKNOWN;
}
struct nl_cb *cb = nl_socket_get_cb(event_sock);
if (cb == NULL) {
ALOGE("Could not create handle");
return WIFI_ERROR_UNKNOWN;
}
// ALOGI("cb->refcnt = %d", cb->cb_refcnt);
nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, internal_no_seq_check, info);
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, internal_valid_message_handler, info);
nl_cb_put(cb);
info->cmd_sock = cmd_sock;
info->event_sock = event_sock;
info->clean_up = false;
info->in_event_loop = false;
info->event_cb = (cb_info *)malloc(sizeof(cb_info) * DEFAULT_EVENT_CB_SIZE);
info->alloc_event_cb = DEFAULT_EVENT_CB_SIZE;
info->num_event_cb = 0;
info->cmd = (cmd_info *)malloc(sizeof(cmd_info) * DEFAULT_CMD_SIZE);
info->alloc_cmd = DEFAULT_CMD_SIZE;
info->num_cmd = 0;
info->nl80211_family_id = genl_ctrl_resolve(cmd_sock, "nl80211");
if (info->nl80211_family_id < 0) {
ALOGE("Could not resolve nl80211 familty id");
nl_socket_free(cmd_sock);
nl_socket_free(event_sock);
free(info);
return WIFI_ERROR_UNKNOWN;
}
pthread_mutex_init(&info->cb_lock, NULL);
*handle = (wifi_handle) info;
ALOGD("wifi_initialize, handle = %p\n", handle);
ALOGD("wifi_initialize, *handle = %p\n", *handle);
ALOGD("wifi_initialize, info = %p\n", info);
ALOGD("wifi_initialize, *info = %pn", *info);
wifi_add_membership(*handle, "scan");
wifi_add_membership(*handle, "mlme");
wifi_add_membership(*handle, "regulatory");
wifi_add_membership(*handle, "vendor");
wifi_init_interfaces(*handle);
ALOGD("Found %d interfaces", info->num_interfaces);
ALOGI("Initialized Wifi HAL Successfully; vendor cmd = %d", NL80211_CMD_VENDOR);
return WIFI_SUCCESS;
}
static int wifi_add_membership(wifi_handle handle, const char *group)
{
hal_info *info = getHalInfo(handle);
int id = wifi_get_multicast_id(handle, "nl80211", group);
if (id < 0) {
ALOGE("Could not find group %s", group);
return id;
}
int ret = nl_socket_add_membership(info->event_sock, id);
if (ret < 0) {
ALOGE("Could not add membership to group %s", group);
}
ALOGI("Successfully added membership for group %s", group);
return ret;
}
static void internal_cleaned_up_handler(wifi_handle handle)
{
hal_info *info = getHalInfo(handle);
wifi_cleaned_up_handler cleaned_up_handler = info->cleaned_up_handler;
if (info->cmd_sock != 0) {
nl_socket_free(info->cmd_sock);
nl_socket_free(info->event_sock);
info->cmd_sock = NULL;
info->event_sock = NULL;
}
(*cleaned_up_handler)(handle);
pthread_mutex_destroy(&info->cb_lock);
free(info);
ALOGI("Internal cleanup completed");
}
void wifi_cleanup(wifi_handle handle, wifi_cleaned_up_handler handler)
{
hal_info *info = getHalInfo(handle);
info->cleaned_up_handler = handler;
info->clean_up = true;
ALOGI("Wifi cleanup completed");
}
static int internal_pollin_handler(wifi_handle handle)
{
hal_info *info = getHalInfo(handle);
ALOGI("even_loop info = %p", info);
struct nl_cb *cb = nl_socket_get_cb(info->event_sock);
int res = nl_recvmsgs(info->event_sock, cb);
ALOGD("nl_recvmsgs returned %d", res);
nl_cb_put(cb);
return res;
}
/* Run event handler */
void wifi_event_loop(wifi_handle handle)
{
hal_info *info = getHalInfo(handle);
ALOGI("even_loop info = %p", info);
ALOGI("even_loop info = %p", handle);
if (info->in_event_loop) {
return;
} else {
info->in_event_loop = true;
}
pollfd pfd;
memset(&pfd, 0, sizeof(pfd));
pfd.fd = nl_socket_get_fd(info->event_sock);
pfd.events = POLLIN;
/* TODO: Add support for timeouts */
do {
int timeout = -1; /* Infinite timeout */
pfd.revents = 0;
ALOGI("Polling socket");
int result = poll(&pfd, 1, -1);
if (result < 0) {
ALOGE("Error polling socket");
} else if (pfd.revents & POLLERR) {
ALOGE("POLL Error; error no = %d", errno);
char buf[2048];
int result2 = read(pfd.fd, buf, sizeof(buf));
ALOGE("Read after POLL returned %d, error no = %d", result2, errno);
} else if (pfd.revents & POLLHUP) {
ALOGE("Remote side hung up");
break;
} else if (pfd.revents & POLLIN) {
ALOGI("Found some events!!!");
internal_pollin_handler(handle);
} else {
ALOGE("Unknown event - %0x", pfd.revents);
}
} while (!info->clean_up);
ALOGI("Cleaning up");
internal_cleaned_up_handler(handle);
}
///////////////////////////////////////////////////////////////////////////////////////
static int internal_no_seq_check(struct nl_msg *msg, void *arg)
{
return NL_OK;
}
static int internal_valid_message_handler(nl_msg *msg, void *arg)
{
wifi_handle handle = (wifi_handle)arg;
hal_info *info = getHalInfo(handle);
ALOGI("even_loop info = %p", handle);
ALOGD("internal_valid_message_handler, info = %p", info);
WifiEvent event(msg);
int res = event.parse();
if (res < 0) {
ALOGE("Failed to parse event: %d", res);
return NL_SKIP;
}
int cmd = event.get_cmd();
uint32_t vendor_id = 0;
int subcmd = 0;
if (cmd == NL80211_CMD_VENDOR) {
vendor_id = event.get_u32(NL80211_ATTR_VENDOR_ID);
subcmd = event.get_u32(NL80211_ATTR_VENDOR_SUBCMD);
ALOGI("event received %s, vendor_id = 0x%0x, subcmd = 0x%0x",
event.get_cmdString(), vendor_id, subcmd);
} else {
ALOGI("event received %s", event.get_cmdString());
}
//ALOGI("event received %s, vendor_id = 0x%0x", event.get_cmdString(), vendor_id);
//event.log();
bool dispatched = false;
pthread_mutex_lock(&info->cb_lock);
ALOGI("Number of events %d", info->num_event_cb);
for (int i = 0; i < info->num_event_cb; i++) {
if (cmd == info->event_cb[i].nl_cmd) {
if (cmd == NL80211_CMD_VENDOR
&& ((vendor_id != info->event_cb[i].vendor_id)
|| (subcmd != info->event_cb[i].vendor_subcmd)))
{
/* event for a different vendor, ignore it */
continue;
}
cb_info *cbi = &(info->event_cb[i]);
nl_recvmsg_msg_cb_t cb_func = cbi->cb_func;
void *cb_arg = cbi->cb_arg;
WifiCommand *cmd = (WifiCommand *)cbi->cb_arg;
if (cmd != NULL) {
cmd->addRef();
}
pthread_mutex_unlock(&info->cb_lock);
(*cb_func)(msg, cb_arg);
if (cmd != NULL) {
cmd->releaseRef();
}
return NL_OK;
}
}
pthread_mutex_unlock(&info->cb_lock);
return NL_OK;
}
///////////////////////////////////////////////////////////////////////////////////////
class GetMulticastIdCommand : public WifiCommand
{
private:
const char *mName;
const char *mGroup;
int mId;
public:
GetMulticastIdCommand(wifi_handle handle, const char *name, const char *group)
: WifiCommand(handle, 0)
{
mName = name;
mGroup = group;
mId = -1;
}
int getId() {
return mId;
}
virtual int create() {
int nlctrlFamily = genl_ctrl_resolve(mInfo->cmd_sock, "nlctrl");
ALOGI("ctrl family = %d", nlctrlFamily);
int ret = mMsg.create(nlctrlFamily, CTRL_CMD_GETFAMILY, 0, 0);
if (ret < 0) {
return ret;
}
ret = mMsg.put_string(CTRL_ATTR_FAMILY_NAME, mName);
return ret;
}
virtual int handleResponse(WifiEvent& reply) {
ALOGE("handling reponse in %s", __func__);
struct nlattr **tb = reply.attributes();
struct genlmsghdr *gnlh = reply.header();
struct nlattr *mcgrp = NULL;
int i;
if (!tb[CTRL_ATTR_MCAST_GROUPS]) {
ALOGE("No multicast groups found");
return NL_SKIP;
} else {
ALOGE("Multicast groups attr size = %d", nla_len(tb[CTRL_ATTR_MCAST_GROUPS]));
}
for_each_attr(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
ALOGE("Processing group");
struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, (nlattr *)nla_data(mcgrp),
nla_len(mcgrp), NULL);
if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] || !tb2[CTRL_ATTR_MCAST_GRP_ID]) {
continue;
}
char *grpName = (char *)nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]);
int grpNameLen = nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME]);
ALOGE("Found group name %s", grpName);
if (strncmp(grpName, mGroup, grpNameLen) != 0)
continue;
mId = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
break;
}
return NL_SKIP;
}
};
class SetPnoMacAddrOuiCommand : public WifiCommand {
private:
byte *mOui;
feature_set *fset;
feature_set *feature_matrix;
int *fm_size;
int set_size_max;
public:
SetPnoMacAddrOuiCommand(wifi_interface_handle handle, oui scan_oui)
: WifiCommand(handle, 0)
{
mOui = scan_oui;
}
int createRequest(WifiRequest& request, int subcmd, byte *scan_oui) {
int result = request.create(GOOGLE_OUI, subcmd);
if (result < 0) {
return result;
}
nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
result = request.put(ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI, scan_oui, DOT11_OUI_LEN);
if (result < 0) {
return result;
}
request.attr_end(data);
return WIFI_SUCCESS;
}
int start() {
ALOGD("Sending mac address OUI");
WifiRequest request(familyId(), ifaceId());
int result = createRequest(request, SLSI_NL80211_VENDOR_SUBCMD_SET_GSCAN_OUI, mOui);
if (result != WIFI_SUCCESS) {
ALOGE("failed to create request; result = %d", result);
return result;
}
result = requestResponse(request);
if (result != WIFI_SUCCESS) {
ALOGE("failed to set scanning mac OUI; result = %d", result);
}
return result;
}
protected:
virtual int handleResponse(WifiEvent& reply) {
ALOGD("Request complete!");
/* Nothing to do on response! */
return NL_SKIP;
}
};
class SetNodfsCommand : public WifiCommand {
private:
u32 mNoDfs;
public:
SetNodfsCommand(wifi_interface_handle handle, u32 nodfs)
: WifiCommand(handle, 0) {
mNoDfs = nodfs;
}
virtual int create() {
int ret;
ret = mMsg.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_NODFS);
if (ret < 0) {
ALOGE("Can't create message to send to driver - %d", ret);
return ret;
}
nlattr *data = mMsg.attr_start(NL80211_ATTR_VENDOR_DATA);
ret = mMsg.put_u32(ATTR_NODFS_VALUE, mNoDfs);
if (ret < 0) {
return ret;
}
mMsg.attr_end(data);
return WIFI_SUCCESS;
}
};
static int wifi_get_multicast_id(wifi_handle handle, const char *name, const char *group)
{
GetMulticastIdCommand cmd(handle, name, group);
int res = cmd.requestResponse();
if (res < 0)
return res;
else
return cmd.getId();
}
/////////////////////////////////////////////////////////////////////////
static bool is_wifi_interface(const char *name)
{
if (strncmp(name, "wlan", 4) != 0 && strncmp(name, "p2p", 3) != 0) {
/* not a wifi interface; ignore it */
return false;
} else {
return true;
}
}
static int get_interface(const char *name, interface_info *info)
{
strcpy(info->name, name);
info->id = if_nametoindex(name);
ALOGI("found an interface : %s, id = %d", name, info->id);
return WIFI_SUCCESS;
}
wifi_error wifi_init_interfaces(wifi_handle handle)
{
hal_info *info = (hal_info *)handle;
ALOGD("wifi_init_interfaces, info = %p", info);
struct dirent *de;
DIR *d = opendir("/sys/class/net");
if (d == 0)
return WIFI_ERROR_UNKNOWN;
int n = 0;
while ((de = readdir(d))) {
if (de->d_name[0] == '.')
continue;
if (is_wifi_interface(de->d_name) ) {
n++;
}
}
closedir(d);
d = opendir("/sys/class/net");
if (d == 0)
return WIFI_ERROR_UNKNOWN;
info->interfaces = (interface_info **)malloc(sizeof(interface_info *) * n);
int i = 0;
while ((de = readdir(d))) {
if (de->d_name[0] == '.')
continue;
if (is_wifi_interface(de->d_name)) {
interface_info *ifinfo = (interface_info *)malloc(sizeof(interface_info));
if (get_interface(de->d_name, ifinfo) != WIFI_SUCCESS) {
free(ifinfo);
continue;
}
ifinfo->handle = handle;
info->interfaces[i] = ifinfo;
i++;
}
}
closedir(d);
info->num_interfaces = n;
return WIFI_SUCCESS;
}
wifi_error wifi_get_ifaces(wifi_handle handle, int *num, wifi_interface_handle **interfaces)
{
hal_info *info = (hal_info *)handle;
*interfaces = (wifi_interface_handle *)info->interfaces;
*num = info->num_interfaces;
return WIFI_SUCCESS;
}
wifi_error wifi_get_iface_name(wifi_interface_handle handle, char *name, size_t size)
{
interface_info *info = (interface_info *)handle;
strcpy(name, info->name);
return WIFI_SUCCESS;
}
wifi_error wifi_get_supported_feature_set(wifi_interface_handle handle, feature_set *set)
{
return WIFI_ERROR_NOT_SUPPORTED;
}
wifi_error wifi_get_concurrency_matrix(wifi_interface_handle handle, int set_size_max,
feature_set set[], int *set_size)
{
return WIFI_ERROR_NOT_SUPPORTED;
}
wifi_error wifi_set_scanning_mac_oui(wifi_interface_handle handle, oui scan_oui)
{
SetPnoMacAddrOuiCommand command(handle, scan_oui);
return (wifi_error)command.start();
}
wifi_error wifi_set_nodfs_flag(wifi_interface_handle handle, u32 nodfs)
{
SetNodfsCommand command(handle, nodfs);
return (wifi_error) command.requestResponse();
}
/////////////////////////////////////////////////////////////////////////////