• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <netinet/ether.h>
2 
3 #include <netlink/netlink.h>
4 #include <netlink/route/link.h>
5 #include <netlink/route/link/macvtap.h>
6 
7 #include <linux/netlink.h>
8 
main(int argc,char * argv[])9 int main(int argc, char *argv[])
10 {
11 	struct rtnl_link *link;
12 	struct nl_cache *link_cache;
13 	struct nl_sock *sk;
14         struct nl_addr* addr;
15 	int err, master_index;
16 
17 	sk = nl_socket_alloc();
18 	if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
19 		nl_perror(err, "Unable to connect socket");
20 		return err;
21 	}
22 
23 	if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) {
24 		nl_perror(err, "Unable to allocate cache");
25 		return err;
26 	}
27 
28 	if (!(master_index = rtnl_link_name2i(link_cache, "eth0"))) {
29 		fprintf(stderr, "Unable to lookup eth0");
30 		return -1;
31 	}
32 
33 	link = rtnl_link_macvtap_alloc();
34 
35 	rtnl_link_set_link(link, master_index);
36 
37 	addr = nl_addr_build(AF_LLC, ether_aton("00:11:22:33:44:55"), ETH_ALEN);
38 	rtnl_link_set_addr(link, addr);
39 	nl_addr_put(addr);
40 
41 	rtnl_link_macvtap_set_mode(link, rtnl_link_macvtap_str2mode("bridge"));
42 
43 	if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
44 		nl_perror(err, "Unable to add link");
45 		return err;
46 	}
47 
48 	rtnl_link_put(link);
49 	nl_close(sk);
50 
51 	return 0;
52 }
53