• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <netlink/netlink.h>
2 #include <netlink/route/link.h>
3 #include <netlink/route/link/vlan.h>
4 
main(int argc,char * argv[])5 int main(int argc, char *argv[])
6 {
7 	struct rtnl_link *link;
8 	struct nl_cache *link_cache;
9 	struct nl_sock *sk;
10 	int err, master_index;
11 
12 	sk = nl_socket_alloc();
13 	if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
14 		nl_perror(err, "Unable to connect socket");
15 		return err;
16 	}
17 
18 	if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) {
19 		nl_perror(err, "Unable to allocate cache");
20 		return err;
21 	}
22 
23 	if (!(master_index = rtnl_link_name2i(link_cache, "eth0"))) {
24 		fprintf(stderr, "Unable to lookup eth0");
25 		return -1;
26 	}
27 
28 	link = rtnl_link_vlan_alloc();
29 
30 	rtnl_link_set_link(link, master_index);
31 
32 	rtnl_link_vlan_set_id(link, 10);
33 
34 	if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
35 		nl_perror(err, "Unable to add link");
36 		return err;
37 	}
38 
39 	rtnl_link_put(link);
40 	nl_close(sk);
41 
42 	return 0;
43 }
44