xfrmi: Use helpers for nested Netlink attributes

This commit is contained in:
Tobias Brunner 2019-03-08 15:22:58 +01:00
parent 2feba5aa22
commit 28b702ad8b
1 changed files with 6 additions and 15 deletions

View File

@ -33,8 +33,6 @@ enum {
#define IFLA_XFRM_MAX (__IFLA_XFRM_MAX - 1) #define IFLA_XFRM_MAX (__IFLA_XFRM_MAX - 1)
#endif #endif
#define NLMSG_TAIL(nlh) ((void*)(((char*)nlh) + NLMSG_ALIGN(nlh->nlmsg_len)))
/** /**
* Create an XFRM interface with the given ID and underlying interface * Create an XFRM interface with the given ID and underlying interface
*/ */
@ -66,25 +64,20 @@ static int add_xfrm_interface(char *name, uint32_t xfrm_id, uint32_t ifindex)
netlink_add_attribute(hdr, IFLA_IFNAME, chunk_from_str(name), netlink_add_attribute(hdr, IFLA_IFNAME, chunk_from_str(name),
sizeof(request)); sizeof(request));
/* the following attributes are nested under this one */ linkinfo = netlink_nested_start(hdr, sizeof(request), IFLA_LINKINFO);
linkinfo = netlink_reserve(hdr, sizeof(request), IFLA_LINKINFO, 0);
linkinfo = (void*)linkinfo - RTA_LENGTH(0);
netlink_add_attribute(hdr, IFLA_INFO_KIND, chunk_from_str("xfrm"), netlink_add_attribute(hdr, IFLA_INFO_KIND, chunk_from_str("xfrm"),
sizeof(request)); sizeof(request));
/* the following attributes are nested under this one */ info_data = netlink_nested_start(hdr, sizeof(request), IFLA_INFO_DATA);
info_data = netlink_reserve(hdr, sizeof(request), IFLA_INFO_DATA, 0);
info_data = (void*)info_data - RTA_LENGTH(0);
netlink_add_attribute(hdr, IFLA_XFRM_IF_ID, chunk_from_thing(xfrm_id), netlink_add_attribute(hdr, IFLA_XFRM_IF_ID, chunk_from_thing(xfrm_id),
sizeof(request)); sizeof(request));
netlink_add_attribute(hdr, IFLA_XFRM_LINK, chunk_from_thing(ifindex), netlink_add_attribute(hdr, IFLA_XFRM_LINK, chunk_from_thing(ifindex),
sizeof(request)); sizeof(request));
info_data->rta_len = NLMSG_TAIL(hdr) - (void*)info_data; netlink_nested_end(hdr, info_data);
netlink_nested_end(hdr, linkinfo);
linkinfo->rta_len = NLMSG_TAIL(hdr) - (void*)linkinfo;
switch (socket->send_ack(socket, hdr)) switch (socket->send_ack(socket, hdr))
{ {
@ -184,14 +177,12 @@ static int list_xfrm_interfaces()
msg = NLMSG_DATA(hdr); msg = NLMSG_DATA(hdr);
msg->ifi_family = AF_UNSPEC; msg->ifi_family = AF_UNSPEC;
/* the following attributes are nested under this one */ linkinfo = netlink_nested_start(hdr, sizeof(request), IFLA_LINKINFO);
linkinfo = netlink_reserve(hdr, sizeof(request), IFLA_LINKINFO, 0);
linkinfo = (void*)linkinfo - RTA_LENGTH(0);
netlink_add_attribute(hdr, IFLA_INFO_KIND, chunk_from_str("xfrm"), netlink_add_attribute(hdr, IFLA_INFO_KIND, chunk_from_str("xfrm"),
sizeof(request)); sizeof(request));
linkinfo->rta_len = NLMSG_TAIL(hdr) - (void*)linkinfo; netlink_nested_end(hdr, linkinfo);
if (socket->send(socket, hdr, &out, &len) != SUCCESS) if (socket->send(socket, hdr, &out, &len) != SUCCESS)
{ {