18 #include <sys/types.h> 23 #include <linux/netlink.h> 24 #include <linux/rtnetlink.h> 37 void *msg_data,
int msg_len)
45 nh = (
struct nlmsghdr *) p;
46 nh->nlmsg_flags = flags | NLM_F_ACK;
47 nh->nlmsg_type = type;
53 void *rta_data,
int rta_data_len)
59 rta = (
struct rtattr *) p;
60 rta->rta_type = rta_type;
61 rta->rta_len = RTA_LENGTH (rta_data_len);
62 clib_memcpy (RTA_DATA (rta), rta_data, rta_data_len);
69 struct sockaddr_nl ra = { 0 };
71 struct nlmsghdr *nh = (
struct nlmsghdr *) m->
data;
75 if ((sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)
78 ra.nl_family = AF_NETLINK;
81 if ((bind (sock, (
struct sockaddr *) &ra,
sizeof (ra))) == -1)
90 if ((len = recv (sock, buf,
sizeof (buf), 0)) == -1)
93 for (nh = (
struct nlmsghdr *) buf; NLMSG_OK (nh, len);
94 nh = NLMSG_NEXT (nh, len))
96 if (nh->nlmsg_type == NLMSG_DONE)
99 if (nh->nlmsg_type == NLMSG_ERROR)
101 struct nlmsgerr *e = (
struct nlmsgerr *) NLMSG_DATA (nh);
118 struct ifinfomsg ifmsg = { 0 };
120 ifmsg.ifi_index = ifindex;
122 &ifmsg,
sizeof (
struct ifinfomsg));
125 strlen (new_ifname) + 1);
134 struct ifinfomsg ifmsg = { 0 };
136 ifmsg.ifi_index = ifindex;
138 &ifmsg,
sizeof (
struct ifinfomsg));
143 strlen (new_ifname) + 1);
152 struct ifinfomsg ifmsg = { 0 };
155 ifmsg.ifi_index = ifindex;
162 &ifmsg,
sizeof (
struct ifinfomsg));
171 struct ifinfomsg ifmsg = { 0 };
173 ifmsg.ifi_index = ifindex;
176 &ifmsg,
sizeof (
struct ifinfomsg));
185 struct ifinfomsg ifmsg = { 0 };
187 ifmsg.ifi_flags = IFF_UP;
188 ifmsg.ifi_change = IFF_UP;
189 ifmsg.ifi_index = ifindex;
192 &ifmsg,
sizeof (
struct ifinfomsg));
200 struct ifinfomsg ifmsg = { 0 };
202 ifmsg.ifi_index = ifindex;
205 &ifmsg,
sizeof (
struct ifinfomsg));
214 struct ifaddrmsg ifa = { 0 };
216 ifa.ifa_family = AF_INET;
217 ifa.ifa_prefixlen = pfx_len;
218 ifa.ifa_index = ifindex;
221 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL,
222 &ifa,
sizeof (
struct ifaddrmsg));
233 struct ifaddrmsg ifa = { 0 };
235 ifa.ifa_family = AF_INET6;
236 ifa.ifa_prefixlen = pfx_len;
237 ifa.ifa_index = ifindex;
240 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL,
241 &ifa,
sizeof (
struct ifaddrmsg));
252 struct rtmsg rtm = { 0 };
255 rtm.rtm_family = AF_INET;
256 rtm.rtm_table = RT_TABLE_MAIN;
257 rtm.rtm_type = RTN_UNICAST;
258 rtm.rtm_dst_len = dst_len;
261 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL,
262 &rtm,
sizeof (
struct rtmsg));
273 struct rtmsg rtm = { 0 };
276 rtm.rtm_family = AF_INET6;
277 rtm.rtm_table = RT_TABLE_MAIN;
278 rtm.rtm_type = RTN_UNICAST;
279 rtm.rtm_dst_len = dst_len;
282 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL,
283 &rtm,
sizeof (
struct rtmsg));
clib_error_t * vnet_netlink_set_link_addr(int ifindex, u8 *mac)
clib_error_t * vnet_netlink_set_link_state(int ifindex, int up)
static void vnet_netlink_msg_add_rtattr(vnet_netlink_msg_t *m, u16 rta_type, void *rta_data, int rta_data_len)
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
clib_error_t * vnet_netlink_set_link_name(int ifindex, char *new_ifname)
static void vnet_netlink_msg_init(vnet_netlink_msg_t *m, u16 type, u16 flags, void *msg_data, int msg_len)
#define clib_memcpy(d, s, n)
clib_error_t * vnet_netlink_set_link_netns(int ifindex, int netns_fd, char *new_ifname)
#define clib_error_return(e, args...)
#define clib_error_return_unix(e, args...)
static clib_error_t * vnet_netlink_msg_send(vnet_netlink_msg_t *m)
clib_error_t * vnet_netlink_add_ip6_route(void *dst, u8 dst_len, void *gw)
#define vec_free(V)
Free vector's memory (no header).
clib_error_t * vnet_netlink_set_link_mtu(int ifindex, int mtu)
clib_error_t * vnet_netlink_add_ip4_route(void *dst, u8 dst_len, void *gw)
unsigned int if_nametoindex(const char *ifname)
clib_error_t * vnet_netlink_add_ip6_addr(int ifindex, void *addr, int pfx_len)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
clib_error_t * vnet_netlink_add_ip4_addr(int ifindex, void *addr, int pfx_len)
clib_error_t * vnet_netlink_set_link_master(int ifindex, char *master_ifname)