1 #include <netlink/netlink.h> 2 #include <netlink/route/link.h> 3 #include <netlink/route/link/macvlan.h> 4 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 struct nl_addr* addr; 11 int err, master_index; 12 13 sk = nl_socket_alloc(); 14 if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) { 15 nl_perror(err, "Unable to connect socket"); 16 return err; 17 } 18 19 if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) { 20 nl_perror(err, "Unable to allocate cache"); 21 return err; 22 } 23 24 if (!(master_index = rtnl_link_name2i(link_cache, "eth0"))) { 25 fprintf(stderr, "Unable to lookup eth0"); 26 return -1; 27 } 28 29 link = rtnl_link_macvlan_alloc(); 30 31 rtnl_link_set_link(link, master_index); 32 33 addr = nl_addr_build(AF_LLC, ether_aton("00:11:22:33:44:55"), ETH_ALEN); 34 rtnl_link_set_addr(link, addr); 35 nl_addr_put(addr); 36 37 rtnl_link_macvlan_set_mode(link, rtnl_link_macvlan_str2mode("bridge")); 38 39 if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) { 40 nl_perror(err, "Unable to add link"); 41 return err; 42 } 43 44 rtnl_link_put(link); 45 nl_close(sk); 46 47 return 0; 48 } 49