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 };
121 ifmsg.ifi_index = ifindex;
123 &ifmsg,
sizeof (
struct ifinfomsg));
126 strlen (new_ifname) + 1);
138 struct ifinfomsg ifmsg = { 0 };
141 ifmsg.ifi_index = ifindex;
143 &ifmsg,
sizeof (
struct ifinfomsg));
148 strlen (new_ifname) + 1);
160 struct ifinfomsg ifmsg = { 0 };
164 ifmsg.ifi_index = ifindex;
171 &ifmsg,
sizeof (
struct ifinfomsg));
183 struct ifinfomsg ifmsg = { 0 };
186 ifmsg.ifi_index = ifindex;
189 &ifmsg,
sizeof (
struct ifinfomsg));
201 struct ifinfomsg ifmsg = { 0 };
204 ifmsg.ifi_flags = ((up) ? IFF_UP : 0);
205 ifmsg.ifi_change = IFF_UP;
206 ifmsg.ifi_index = ifindex;
209 &ifmsg,
sizeof (
struct ifinfomsg));
220 struct ifinfomsg ifmsg = { 0 };
223 ifmsg.ifi_index = ifindex;
226 &ifmsg,
sizeof (
struct ifinfomsg));
238 struct ifaddrmsg ifa = { 0 };
241 ifa.ifa_family = AF_INET;
242 ifa.ifa_prefixlen = pfx_len;
243 ifa.ifa_index = ifindex;
246 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
247 &ifa,
sizeof (
struct ifaddrmsg));
261 struct ifaddrmsg ifa = { 0 };
264 ifa.ifa_family = AF_INET6;
265 ifa.ifa_prefixlen = pfx_len;
266 ifa.ifa_index = ifindex;
269 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
270 &ifa,
sizeof (
struct ifaddrmsg));
284 struct rtmsg rtm = { 0 };
288 rtm.rtm_family = AF_INET;
289 rtm.rtm_table = RT_TABLE_MAIN;
290 rtm.rtm_type = RTN_UNICAST;
291 rtm.rtm_dst_len = dst_len;
294 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
295 &rtm,
sizeof (
struct rtmsg));
309 struct rtmsg rtm = { 0 };
313 rtm.rtm_family = AF_INET6;
314 rtm.rtm_table = RT_TABLE_MAIN;
315 rtm.rtm_type = RTN_UNICAST;
316 rtm.rtm_dst_len = dst_len;
319 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
320 &rtm,
sizeof (
struct rtmsg));
clib_error_t * vnet_netlink_set_link_addr(int ifindex, u8 *mac)
u8 * format_clib_error(u8 *s, va_list *va)
clib_error_t * vnet_netlink_set_link_state(int ifindex, int up)
vl_api_wireguard_peer_flags_t flags
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...)
vl_api_fib_path_type_t type
#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)
sll srl srl sll sra u16x4 i
#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)