コード例 #1
0
int main(int argc, char *argv[])
{
	struct rtnl_link *link;
	struct nl_sock *sk;
	int err;

	sk = nl_socket_alloc();
	if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
		nl_perror(err, "Unable to connect socket");
		return err;
	}

	link = rtnl_link_alloc();
	rtnl_link_set_name(link, "my_bond");

	if ((err = rtnl_link_set_type(link, "bond")) < 0) {
		nl_perror(err, "Unable to set link info type");
		return err;
	}

	if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
		nl_perror(err, "Unable to add link");
		return err;
	}

	rtnl_link_put(link);
	nl_close(sk);

	return 0;
}
コード例 #2
0
ファイル: bonding.c プロジェクト: cjenkin2/libnl-3.2.3
/**
 * Create a new kernel bonding device
 * @arg sock		netlink socket
 * @arg name		name of bonding device or NULL
 * @arg opts		bonding options (currently unused)
 *
 * Creates a new bonding device in the kernel. If no name is
 * provided, the kernel will automatically pick a name of the
 * form "type%d" (e.g. bond0, vlan1, etc.)
 *
 * The \a opts argument is currently unused. In the future, it
 * may be used to carry additional bonding options to be set
 * when creating the bonding device.
 *
 * @note When letting the kernel assign a name, it will become
 *       difficult to retrieve the interface afterwards because
 *       you have to guess the name the kernel has chosen. It is
 *       therefore not recommended to not provide a device name.
 *
 * @see rtnl_link_bond_enslave()
 * @see rtnl_link_bond_release()
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_bond_add(struct nl_sock *sock, const char *name,
		       struct rtnl_link *opts)
{
	struct rtnl_link *link;
	int err;

	if (!(link = rtnl_link_alloc()))
		return -NLE_NOMEM;

	if (!name) {
		if (opts)
			name = rtnl_link_get_name(opts);

		if (!name)
			return -NLE_MISSING_ATTR;
	}

	if ((err = rtnl_link_set_type(link, "bond")) < 0)
		goto errout;
	

		rtnl_link_set_name(link, name);

	err = rtnl_link_add(sock, link, NLM_F_CREATE);
errout:
	rtnl_link_put(link);

	return err;
}
コード例 #3
0
ファイル: test-create-vrf.c プロジェクト: Domikk/libnl
int main(int argc, char *argv[])
{
	struct nl_cache *link_cache;
	struct rtnl_link *link, *link2;
	struct nl_sock *sk;
	uint32_t tb_id;
	int err;

	sk = nl_socket_alloc();
	if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
		nl_perror(err, "Unable to connect socket");
		return err;
	}

	if (!(link = rtnl_link_vrf_alloc())) {
		fprintf(stderr, "Unable to allocate link");
		return -1;
	}

	rtnl_link_set_name(link, "vrf-red");

	if ((err = rtnl_link_vrf_set_tableid(link, 10)) < 0) {
		nl_perror(err, "Unable to set VRF table id");
		return err;
	}

	if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
		nl_perror(err, "Unable to add link");
		return err;
	}

	if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) {
		nl_perror(err, "Unable to allocate cache");
		return err;
	}

	if (!(link2 = rtnl_link_get_by_name(link_cache, "vrf-red"))) {
		fprintf(stderr, "Unable to lookup vrf-red");
		return -1;
	}

	if ((err = rtnl_link_vrf_get_tableid(link2, &tb_id)) < 0) {
		nl_perror(err, "Unable to get VRF table id");
		return err;
	}

	if (tb_id != 10) {
		fprintf(stderr, "Mismatch with VRF table id\n");
	}

	rtnl_link_put(link);
	nl_close(sk);

	return 0;
}
コード例 #4
0
ファイル: test-create-sit.c プロジェクト: Distrotech/libnl
int main(int argc, char *argv[])
{
	struct nl_cache *link_cache;
	struct rtnl_link *link;
	struct in_addr addr;
	struct nl_sock *sk;
	int err, if_index;

	sk = nl_socket_alloc();
	if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
		nl_perror(err, "Unable to connect socket");
		return err;
	}

	err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache);
	if ( err < 0) {
		nl_perror(err, "Unable to allocate cache");
		return err;
	}

	if_index = rtnl_link_name2i(link_cache, "eno16777736");
	if (!if_index) {
		fprintf(stderr, "Unable to lookup eno16777736");
		return -1;
	}

	link = rtnl_link_sit_alloc();
	if(!link) {
		nl_perror(err, "Unable to allocate link");
		return -1;

	}
	rtnl_link_set_name(link, "sit-tun");
	rtnl_link_sit_set_link(link, if_index);

	inet_pton(AF_INET, "192.168.254.12", &addr.s_addr);
	rtnl_link_sit_set_local(link, addr.s_addr);

	inet_pton(AF_INET, "192.168.254.13", &addr.s_addr);
	rtnl_link_sit_set_remote(link, addr.s_addr);

	rtnl_link_sit_set_ttl(link, 64);
	err = rtnl_link_add(sk, link, NLM_F_CREATE);
	if (err < 0) {
		nl_perror(err, "Unable to add link");
		return err;
	}

	rtnl_link_put(link);
	nl_close(sk);

	return 0;
}
コード例 #5
0
ファイル: test-create-ip6tnl.c プロジェクト: Distrotech/libnl
int main(int argc, char *argv[])
{
	struct nl_cache *link_cache;
	struct rtnl_link *link;
        struct in6_addr addr;
	struct nl_sock *sk;
	int err, if_index;

	sk = nl_socket_alloc();
	if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
		nl_perror(err, "Unable to connect socket");
		return err;
	}

	err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache);
	if ( err < 0) {
		nl_perror(err, "Unable to allocate cache");
		return err;
	}

	if_index = rtnl_link_name2i(link_cache, "ens33");
	if (!if_index) {
		fprintf(stderr, "Unable to lookup ens33");
		return -1;
	}

	link = rtnl_link_ip6_tnl_alloc();
	if(!link) {
		nl_perror(err, "Unable to allocate link");
		return -1;

	}
	rtnl_link_set_name(link, "ip6tnl-tun");
	rtnl_link_ip6_tnl_set_link(link, if_index);

	inet_pton(AF_INET6, "2607:f0d0:1002:51::4", &addr);
	rtnl_link_ip6_tnl_set_local(link, &addr);

	inet_pton(AF_INET6, "2607:f0d0:1002:52::5", &addr);
	rtnl_link_ip6_tnl_set_remote(link, &addr);

	err = rtnl_link_add(sk, link, NLM_F_CREATE);
	if (err < 0) {
		nl_perror(err, "Unable to add link");
		return err;
	}

	rtnl_link_put(link);
	nl_close(sk);

	return 0;
}
コード例 #6
0
ファイル: bridge.c プロジェクト: Drakey83/steamlink-sdk
/** 
 * Create a new kernel bridge device
 * @arg sk              netlink socket
 * @arg name            name of the bridge device or NULL
 *
 * Creates a new bridge device in the kernel. If no name is
 * provided, the kernel will automatically pick a name of the
 * form "type%d" (e.g. bridge0, vlan1, etc.)
 *
 * @return 0 on success or a negative error code
*/
int rtnl_link_bridge_add(struct nl_sock *sk, const char *name)
{
	int err;
	struct rtnl_link *link;

	if (!(link = rtnl_link_bridge_alloc()))
		return -NLE_NOMEM;

	if(name)
		rtnl_link_set_name(link, name);

	err = rtnl_link_add(sk, link, NLM_F_CREATE);
	rtnl_link_put(link);

	return err;
}
コード例 #7
0
ファイル: netlink.cpp プロジェクト: direvius/porto
TError TNlLink::AddVeth(const std::string &name,
                        const std::string &hw,
                        int mtu, int nsFd) {
    struct rtnl_link *veth, *peer;
    int ret;

    peer = rtnl_link_veth_alloc();
    if (!peer)
        return TError(EError::Unknown, "Unable to allocate veth");

    rtnl_link_set_name(peer, rtnl_link_get_name(Link));

    veth = rtnl_link_veth_get_peer(peer);
    rtnl_link_set_name(veth, name.c_str());

    if (nsFd >= 0)
        rtnl_link_set_ns_fd(veth, nsFd);

    if (mtu > 0) {
        rtnl_link_set_mtu(peer, mtu);
        rtnl_link_set_mtu(veth, mtu);
    }

    if (!hw.empty()) {
        TNlAddr addr;
        TError error = addr.Parse(AF_LLC, hw.c_str());
        if (error)
            return error;
        rtnl_link_set_addr(veth, addr.Addr);
    }

    rtnl_link_set_flags(peer, IFF_UP);

    Dump("add", veth);
    rtnl_link_put(veth);

    Dump("add", peer);
    ret = rtnl_link_add(GetSock(), peer, NLM_F_CREATE | NLM_F_EXCL);
    if (ret < 0) {
        rtnl_link_put(peer);
        return Error(ret, "Cannot add veth");
    }

    rtnl_link_put(peer);

    return Load();
}
コード例 #8
0
ファイル: runner.c プロジェクト: mato/docker-unikernel-runner
/*
 * Create a bridge interface. Returns 0 if successful, libnl error if not.
 */
static int create_bridge_link(struct nl_sock *sk, const char *name)
{
    struct rtnl_link *l_bridge;
    int err;

    l_bridge = rtnl_link_bridge_alloc();
    assert(l_bridge);
    rtnl_link_set_name(l_bridge, name);
    
    err = rtnl_link_add(sk, l_bridge, NLM_F_CREATE);
    if (err < 0) {
        rtnl_link_put(l_bridge);
        return err;
    }
    
    rtnl_link_put(l_bridge);
    return 0;
}
コード例 #9
0
ファイル: test-create-bridge.c プロジェクト: Distrotech/libnl
int create_bridge(struct nl_sock *sk, struct nl_cache *link_cache, const char *name) {
	struct rtnl_link *link;
	int err;

	link = rtnl_link_alloc();
	if ((err = rtnl_link_set_type(link, "bridge")) < 0) {
		rtnl_link_put(link);
		return err;
	}
	rtnl_link_set_name(link, name);

	if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
		return err;
	}
	rtnl_link_put(link);

	return 0;
}
コード例 #10
0
int main(int argc, char *argv[])
{
	struct rtnl_link *link;
	struct nl_cache *link_cache;
	struct nl_sock *sk;
        struct nl_addr* addr;
	int err, master_index;

	sk = nl_socket_alloc();
	if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
		nl_perror(err, "Unable to connect socket");
		return err;
	}

	if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) {
		nl_perror(err, "Unable to allocate cache");
		return err;
	}

	if (!(master_index = rtnl_link_name2i(link_cache, "eth0"))) {
		fprintf(stderr, "Unable to lookup eth0");
		return -1;
	}

	link = rtnl_link_macvtap_alloc();

	rtnl_link_set_link(link, master_index);

	addr = nl_addr_build(AF_LLC, ether_aton("00:11:22:33:44:55"), ETH_ALEN);
	rtnl_link_set_addr(link, addr);
	nl_addr_put(addr);

	rtnl_link_macvtap_set_mode(link, rtnl_link_macvtap_str2mode("bridge"));

	if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
		nl_perror(err, "Unable to add link");
		return err;
	}

	rtnl_link_put(link);
	nl_close(sk);

	return 0;
}
コード例 #11
0
ファイル: test-create-ipvlan.c プロジェクト: Domikk/libnl
int main(int argc, char *argv[])
{
	struct rtnl_link *link;
	struct nl_cache *link_cache;
	struct nl_sock *sk;
	int err, master_index;

	sk = nl_socket_alloc();
	if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
		nl_perror(err, "Unable to connect socket");
		return err;
	}

	if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) {
		nl_perror(err, "Unable to allocate cache");
		return err;
	}

	if (!(master_index = rtnl_link_name2i(link_cache, "eth0"))) {
		fprintf(stderr, "Unable to lookup eth0");
		return -1;
	}

	if (!(link = rtnl_link_ipvlan_alloc())) {
		fprintf(stderr, "Unable to allocate link");
		return -1;
	}

	rtnl_link_set_link(link, master_index);
	rtnl_link_ipvlan_set_mode(link, rtnl_link_ipvlan_str2mode("l2"));

	if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
		nl_perror(err, "Unable to add link");
		return err;
	}

	rtnl_link_put(link);
	nl_close(sk);

	return 0;
}
コード例 #12
0
ファイル: veth.c プロジェクト: HongweiBi/libnl
/**
 * Create a new kernel veth device
 * @arg sock		netlink socket
 * @arg name		name of the veth device or NULL
 * @arg peer_name	name of its peer or NULL
 * @arg pid		pid of the process in the new netns
 *
 * Creates a new veth device pair in the kernel and move the peer
 * to the network namespace where the process is. If no name is
 * provided, the kernel will automatically pick a name of the
 * form "veth%d" (e.g. veth0, veth1, etc.)
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_veth_add(struct nl_sock *sock, const char *name,
                       const char *peer_name, pid_t pid)
{
	struct rtnl_link *link, *peer;
	int err = -NLE_NOMEM;

	if (!(link = rtnl_link_veth_alloc()))
		return -NLE_NOMEM;
	peer = rtnl_link_veth_get_peer(link);

	if (name && peer_name) {
		rtnl_link_set_name(link, name);
		rtnl_link_set_name(peer, peer_name);
	}

	rtnl_link_set_ns_pid(peer, pid);
	err = rtnl_link_add(sock, link, NLM_F_CREATE);

	rtnl_link_put(peer);
	rtnl_link_put(link);

        return err;
}
コード例 #13
0
ファイル: vlan_util.c プロジェクト: imw/hapd
/*
 * Add a vlan interface with name 'vlan_if_name', VLAN ID 'vid' and
 * tagged interface 'if_name'.
 *
 * returns -1 on error
 * returns 1 if the interface already exists
 * returns 0 otherwise
*/
int vlan_add(const char *if_name, int vid, const char *vlan_if_name)
{
	int ret = -1;
	struct nl_sock *handle = NULL;
	struct nl_cache *cache = NULL;
	struct rtnl_link *rlink = NULL;
	int if_idx = 0;

	wpa_printf(MSG_DEBUG, "VLAN: vlan_add(if_name=%s, vid=%d, "
		   "vlan_if_name=%s)", if_name, vid, vlan_if_name);

	if ((os_strlen(if_name) + 1) > IFNAMSIZ) {
		wpa_printf(MSG_ERROR, "VLAN: Interface name too long: '%s'",
			   if_name);
		return -1;
	}

	if ((os_strlen(vlan_if_name) + 1) > IFNAMSIZ) {
		wpa_printf(MSG_ERROR, "VLAN: Interface name too long: '%s'",
			   vlan_if_name);
		return -1;
	}

	handle = nl_socket_alloc();
	if (!handle) {
		wpa_printf(MSG_ERROR, "VLAN: failed to open netlink socket");
		goto vlan_add_error;
	}

	if (nl_connect(handle, NETLINK_ROUTE) < 0) {
		wpa_printf(MSG_ERROR, "VLAN: failed to connect to netlink");
		goto vlan_add_error;
	}

	if (rtnl_link_alloc_cache(handle, AF_UNSPEC, &cache) < 0) {
		cache = NULL;
		wpa_printf(MSG_ERROR, "VLAN: failed to alloc cache");
		goto vlan_add_error;
	}

	if (!(if_idx = rtnl_link_name2i(cache, if_name))) {
		/* link does not exist */
		wpa_printf(MSG_ERROR, "VLAN: interface %s does not exist",
			   if_name);
		goto vlan_add_error;
	}

	if ((rlink = rtnl_link_get_by_name(cache, vlan_if_name))) {
		/* link does exist */
		rtnl_link_put(rlink);
		rlink = NULL;
		wpa_printf(MSG_ERROR, "VLAN: interface %s already exists",
			   vlan_if_name);
		ret = 1;
		goto vlan_add_error;
	}

	rlink = rtnl_link_alloc();
	if (!rlink) {
		wpa_printf(MSG_ERROR, "VLAN: failed to allocate new link");
		goto vlan_add_error;
	}

	if (rtnl_link_set_type(rlink, "vlan") < 0) {
		wpa_printf(MSG_ERROR, "VLAN: failed to set link type");
		goto vlan_add_error;
	}

	rtnl_link_set_link(rlink, if_idx);
	rtnl_link_set_name(rlink, vlan_if_name);

	if (rtnl_link_vlan_set_id(rlink, vid) < 0) {
		wpa_printf(MSG_ERROR, "VLAN: failed to set link vlan id");
		goto vlan_add_error;
	}

	if (rtnl_link_add(handle, rlink, NLM_F_CREATE) < 0) {
		wpa_printf(MSG_ERROR, "VLAN: failed to create link %s for "
			   "vlan %d on %s (%d)",
			   vlan_if_name, vid, if_name, if_idx);
		goto vlan_add_error;
	}

	ret = 0;

vlan_add_error:
	if (rlink)
		rtnl_link_put(rlink);
	if (cache)
		nl_cache_free(cache);
	if (handle)
		nl_socket_free(handle);
	return ret;
}