Beispiel #1
0
static int nl80211_init(void)
{
	int err, fd;

	if (!nls)
	{
		nls = malloc(sizeof(struct nl80211_state));
		if (!nls) {
			err = -ENOMEM;
			goto err;
		}

		memset(nls, 0, sizeof(*nls));

		nls->nl_sock = nl_socket_alloc();
		if (!nls->nl_sock) {
			err = -ENOMEM;
			goto err;
		}

		if (genl_connect(nls->nl_sock)) {
			err = -ENOLINK;
			goto err;
		}

		fd = nl_socket_get_fd(nls->nl_sock);
		if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0) {
			err = -EINVAL;
			goto err;
		}

		if (genl_ctrl_alloc_cache(nls->nl_sock, &nls->nl_cache)) {
			err = -ENOMEM;
			goto err;
		}

		nls->nl80211 = genl_ctrl_search_by_name(nls->nl_cache, "nl80211");
		if (!nls->nl80211) {
			err = -ENOENT;
			goto err;
		}

		nls->nlctrl = genl_ctrl_search_by_name(nls->nl_cache, "nlctrl");
		if (!nls->nlctrl) {
			err = -ENOENT;
			goto err;
		}
	}

	return 0;


err:
	nl80211_close();
	return err;
}
Beispiel #2
0
static bool nl80211_init()
{
	int err;

	sock = nl_socket_alloc();
	if (!sock) {
		fprintf(stderr, "failed to allocate netlink socket\n");
		goto out;
	}

	err = genl_connect(sock);
	if (err) {
		nl_perror(err, "failed to make generic netlink connection");
		goto out;
	}

	err = genl_ctrl_alloc_cache(sock, &cache);
	if (err) {
		nl_perror(err, "failed to allocate netlink controller cache");
		goto out;
	}

	family = genl_ctrl_search_by_name(cache, NL80211_GENL_NAME);
	if (!family) {
		fprintf(stderr, "failed to find nl80211\n");
		goto out;
	}

	return true;
out:
	genl_family_put(family);
	nl_cache_free(cache);
	nl_socket_free(sock);
	return false;
}
Beispiel #3
0
int unl_genl_init(struct unl *unl, const char *family)
{
	memset(unl, 0, sizeof(*unl));

	if (unl_init(unl))
		goto error_out;

	unl->hdrlen = NLMSG_ALIGN(sizeof(struct genlmsghdr));
	unl->family_name = strdup(family);
	if (!unl->family_name)
		goto error;

	if (genl_connect(unl->sock))
		goto error;

	if (genl_ctrl_alloc_cache(unl->sock, &unl->cache))
		goto error;

	unl->family = genl_ctrl_search_by_name(unl->cache, family);
	if (!unl->family)
		goto error;

	return 0;

error:
	unl_free(unl);
error_out:
	return -1;
}
Beispiel #4
0
int nl80211_connect(const char *interface, void **handle, void **cache,
					 void **family, char *errstr) {
#ifndef HAVE_LINUX_NETLINK
	snprintf(errstr, LORCON_STATUS_MAX, "LORCON was not compiled with netlink/nl80211 "
			 "support, check the output of ./configure for why");
	return -1;
#else
	struct nl_sock *nl_handle;
	struct nl_cache *nl_cache;
	struct genl_family *nl80211;

	if ((nl_handle = nl_socket_alloc()) == NULL) {
		snprintf(errstr, LORCON_STATUS_MAX, "%s failed to allocate nlhandle",
				 __FUNCTION__);
		return -1;
	}

	if (genl_connect(nl_handle)) {
		snprintf(errstr, LORCON_STATUS_MAX, "%s failed to connect to generic netlink",
				 __FUNCTION__);
		nl_socket_free(nl_handle);
		return -1;
	}

	if (genl_ctrl_alloc_cache(nl_handle, &nl_cache) != 0) {
		snprintf(errstr, LORCON_STATUS_MAX, "%s failed to allocate "
				 "generic netlink cache", __FUNCTION__);
		nl_socket_free(nl_handle);
		return -1;
	}

	if ((nl80211 = genl_ctrl_search_by_name(nl_cache, "nl80211")) == NULL) {
		snprintf(errstr, LORCON_STATUS_MAX, "%s failed to find "
				 "nl80211 controls, kernel may be too old", __FUNCTION__);
		nl_socket_free(nl_handle);
		return -1;
	}

	(*handle) = (void *) nl_handle;
	(*cache) = (void *) nl_cache;
	(*family) = (void *) nl80211;

	return 1;
#endif
}
Beispiel #5
0
static void nl80211_init(struct nl80211_state *state)
{
	int ret;

	state->nl_sock = nl80211_nl_socket_xalloc();

	ret = genl_connect(state->nl_sock);
	if (ret)
		panic("Cannot connect generic netlink!\n");

	ret = genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache);
	if (ret < 0)
		panic("Failed to allocate generic netlink cache: %s!",
		      nl_geterror(-ret));

	state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
	if (!state->nl80211)
		panic("nl80211 not found in netlink cache!\n");
}
static int netlink_init(void)
{
    info("Starting experimental netlink support");

    handle = nl_handle_alloc();
    if (!handle) {
        error("Failed to allocate netlink handle");
        return -ENOMEM;
    }

    if (genl_connect(handle) < 0) {
        error("Failed to connect to generic netlink");
        nl_handle_destroy(handle);
        return -ENOLINK;
    }

    cache = genl_ctrl_alloc_cache(handle);
    if (!cache) {
        error("Failed to allocate generic netlink cache");
        return -ENOMEM;
        nl_handle_destroy(handle);
    }

    family = genl_ctrl_search_by_name(cache, "bluetooth");
    if (!family) {
        error("Failed to find Bluetooth netlink family");
        nl_cache_free(cache);
        nl_handle_destroy(handle);
        return -ENOENT;
    }

    if (create_channel(nl_socket_get_fd(handle)) < 0)  {
        error("Failed to create netlink IO channel");
        genl_family_put(family);
        nl_cache_free(cache);
        nl_handle_destroy(handle);
        return -ENOMEM;
    }

    return 0;
}
Beispiel #7
0
static int init_nl()
{
    int err;

    nl_soc = nl_socket_alloc();
    if (!nl_soc) {
        ALOGE("Failed to allocate netlink socket.");
        return -ENOMEM;
    }

    if (genl_connect(nl_soc)) {
        ALOGE("Failed to connect to generic netlink.");
        err = -ENOLINK;
        goto out_handle_destroy;
    }

    genl_ctrl_alloc_cache(nl_soc, &nl_cache);
    if (!nl_cache) {
        ALOGE("Failed to allocate generic netlink cache.");
        err = -ENOMEM;
        goto out_handle_destroy;
    }

    nl80211 = genl_ctrl_search_by_name(nl_cache, "nl80211");
    if (!nl80211) {
        ALOGE("nl80211 not found.");
        err = -ENOENT;
        goto out_cache_free;
    }

    return 0;

out_cache_free:
    nl_cache_free(nl_cache);
out_handle_destroy:
    nl_socket_free(nl_soc);
    return err;
}
Beispiel #8
0
static int nl80211_init(struct nl80211_state *state)
{
	int err;

	state->nl_sock = nl_socket_alloc();
	if (!state->nl_sock) {
		fprintf(stderr, "Failed to allocate netlink socket.\n");
		return -ENOMEM;
	}

	if (genl_connect(state->nl_sock)) {
		fprintf(stderr, "Failed to connect to generic netlink.\n");
		err = -ENOLINK;
		goto out_handle_destroy;
	}

	if (genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache)) {
		fprintf(stderr, "Failed to allocate generic netlink cache.\n");
		err = -ENOMEM;
		goto out_handle_destroy;
	}

	state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
	if (!state->nl80211) {
		fprintf(stderr, "nl80211 not found.\n");
		err = -ENOENT;
		goto out_cache_free;
	}

	return 0;

 out_cache_free:
	nl_cache_free(state->nl_cache);
 out_handle_destroy:
	nl_socket_free(state->nl_sock);
	return err;
}
Beispiel #9
0
bool CNL80211::open() {
	if (m_connected)
		return true;
	m_nlCallback = nl_cb_alloc(NL_CB_DEFAULT);
	m_nlSocket = nl_socket_alloc();
	if(!m_nlSocket) {
		emit message("Could not create netlink socket");
		return false;
	}
	if (!genl_connect(m_nlSocket)) {
		emit message("Could not connect to generic netlink interface");
		close();
		return false;
	}
	if (genl_ctrl_alloc_cache(m_nlSocket, &m_nlCache)) {
		emit message("Could not allocate generic netlink cache.");
		close();
		return false;
	}

	m_nlFamily = genl_ctrl_search_by_name(m_nlCache, "nl80211");
	if (!m_nlFamily) {
		emit message("Could not find nl80211");
		close();
		return false;
	}
	
	m_nlFd = nl_socket_get_fd(m_nlSocket);
	m_nlSn = new QSocketNotifier(m_nlFd,QSocketNotifier::Read,this);
	connect(m_nlSn,SIGNAL(activated(int)), this, SLOT(readNlMessage(void)));
	
	//Start Signal Quality polling
	m_sqTimerId = startTimer(m_sqPollrate);
	
	m_connected = true;
	return true;
}
Beispiel #10
0
static int
wprobe_init(void)
{
    int ret;

    if (n_devs++ > 0)
        return 0;

    handle = nl_socket_alloc();
    if (!handle) {
        DPRINTF("Failed to create handle\n");
        goto err;
    }

    if (genl_connect(handle)) {
        DPRINTF("Failed to connect to generic netlink\n");
        goto err;
    }

    ret = genl_ctrl_alloc_cache(handle, &cache);
    if (ret < 0) {
        DPRINTF("Failed to allocate netlink cache\n");
        goto err;
    }

    family = genl_ctrl_search_by_name(cache, "wprobe");
    if (!family) {
        DPRINTF("wprobe API not present\n");
        goto err;
    }
    return 0;

err:
    wprobe_free();
    return -EINVAL;
}