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);
127 struct ifinfomsg ifmsg = { 0 };
130 ifmsg.ifi_index = ifindex;
132 &ifmsg,
sizeof (
struct ifinfomsg));
135 strlen (new_ifname) + 1);
147 struct ifinfomsg ifmsg = { 0 };
150 ifmsg.ifi_index = ifindex;
152 &ifmsg,
sizeof (
struct ifinfomsg));
157 strlen (new_ifname) + 1);
169 struct ifinfomsg ifmsg = { 0 };
173 ifmsg.ifi_index = ifindex;
180 &ifmsg,
sizeof (
struct ifinfomsg));
192 struct ifinfomsg ifmsg = { 0 };
195 ifmsg.ifi_index = ifindex;
198 &ifmsg,
sizeof (
struct ifinfomsg));
210 struct ifinfomsg ifmsg = { 0 };
213 ifmsg.ifi_flags = ((up) ? IFF_UP : 0);
214 ifmsg.ifi_change = IFF_UP;
215 ifmsg.ifi_index = ifindex;
218 &ifmsg,
sizeof (
struct ifinfomsg));
229 struct ifinfomsg ifmsg = { 0 };
236 ifmsg.ifi_index = ifindex;
239 sizeof (
struct ifinfomsg));
255 struct nlmsghdr *
nh = (
struct nlmsghdr *) replies[0].
data;
256 if (nh->nlmsg_type != RTM_NEWLINK)
259 0,
"netlink reply has wrong type: %d != RTM_NEWLINK", nh->nlmsg_type);
263 offset = NLMSG_HDRLEN + NLMSG_ALIGN (
sizeof (
struct ifinfomsg));
264 attr = (
struct nlattr *) ((
u8 *) nh +
offset);
265 len = nh->nlmsg_len -
offset;
269 if ((attr->nla_type & NLA_TYPE_MASK) == IFLA_MTU)
271 msg_mtu = *(
u32 *) ((
u8 *) attr + NLA_HDRLEN);
272 if (attr->nla_type & NLA_F_NET_BYTEORDER)
273 *mtu = clib_net_to_host_u32 (msg_mtu);
279 offset = NLA_ALIGN (attr->nla_len);
281 attr = (
struct nlattr *) ((
u8 *) attr +
offset);
283 while (len >
sizeof (
struct nlattr));
302 struct ifinfomsg ifmsg = { 0 };
305 ifmsg.ifi_index = ifindex;
308 &ifmsg,
sizeof (
struct ifinfomsg));
320 struct ifaddrmsg ifa = { 0 };
323 ifa.ifa_family = AF_INET;
324 ifa.ifa_prefixlen = pfx_len;
325 ifa.ifa_index = ifindex;
328 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
329 &ifa,
sizeof (
struct ifaddrmsg));
343 struct ifaddrmsg ifa = { 0 };
346 ifa.ifa_family = AF_INET6;
347 ifa.ifa_prefixlen = pfx_len;
348 ifa.ifa_index = ifindex;
351 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
352 &ifa,
sizeof (
struct ifaddrmsg));
366 struct rtmsg rtm = { 0 };
370 rtm.rtm_family = AF_INET;
371 rtm.rtm_table = RT_TABLE_MAIN;
372 rtm.rtm_type = RTN_UNICAST;
373 rtm.rtm_dst_len = dst_len;
376 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
377 &rtm,
sizeof (
struct rtmsg));
391 struct rtmsg rtm = { 0 };
395 rtm.rtm_family = AF_INET6;
396 rtm.rtm_table = RT_TABLE_MAIN;
397 rtm.rtm_type = RTN_UNICAST;
398 rtm.rtm_dst_len = dst_len;
401 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
402 &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)
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_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
#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...)
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).
#define clib_warning(format, args...)
clib_error_t * vnet_netlink_get_link_mtu(int ifindex, u32 *mtu)
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)
static clib_error_t * vnet_netlink_msg_send(vnet_netlink_msg_t *m, vnet_netlink_msg_t **replies)
unsigned int if_nametoindex(const char *ifname)
clib_error_t * vnet_netlink_add_ip6_addr(int ifindex, void *addr, int pfx_len)
template key/value backing page structure
#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)
struct clib_bihash_value offset
template key/value backing page structure
#define vec_foreach(var, vec)
Vector iterator.
__clib_export u8 * format_clib_error(u8 *s, va_list *va)
clib_error_t * vnet_netlink_set_link_master(int ifindex, char *master_ifname)