RefCount n_ref;
struct nlmsghdr *hdr;
-
- struct rtattr *current_container;
-
- struct rtattr *next_rta;
- size_t remaining_size;
+ size_t container_offset; /* offset from hdr to container start */
+ size_t next_rta_offset; /* offset from hdr to next rta */
bool sealed:1;
};
+#define CURRENT_CONTAINER(m) ((m)->container_offset ? (struct rtattr*)((uint8_t*)(m)->hdr + (m)->container_offset) : NULL)
+#define NEXT_RTA(m) ((struct rtattr*)((uint8_t*)(m)->hdr + (m)->next_rta_offset))
+#define UPDATE_RTA(m, new) (m)->next_rta_offset = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
+
static int message_new(sd_rtnl_message **ret, size_t initial_size) {
sd_rtnl_message *m;
rtm = NLMSG_DATA((*ret)->hdr);
+ UPDATE_RTA(*ret, RTM_RTA(rtm));
+
rtm->rtm_family = rtm_family;
rtm->rtm_scope = RT_SCOPE_UNIVERSE;
rtm->rtm_type = RTN_UNICAST;
ifi->ifi_index = index;
ifi->ifi_change = 0xffffffff;
+ UPDATE_RTA(*ret, IFLA_RTA(ifi));
+
return 0;
}
ifa->ifa_scope = scope;
ifa->ifa_index = index;
+ UPDATE_RTA(*ret, IFA_RTA(ifa));
+
return 0;
}
return 0;
}
-/* If successful the updated message will be correctly aligned, if unsuccessful the old message is
- untouched */
+/* If successful the updated message will be correctly aligned, if
+ unsuccessful the old message is untouched. */
static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data, size_t data_length) {
uint32_t rta_length, message_length;
struct nlmsghdr *new_hdr;
/* get the size of the new rta attribute (with padding at the end) */
rta_length = RTA_LENGTH(data_length);
- /* get the new message size (with padding at the end)
- */
+
+ /* get the new message size (with padding at the end) */
message_length = m->hdr->nlmsg_len + RTA_ALIGN(rta_length);
/* realloc to fit the new attribute */
/* get pointer to the attribute we are about to add */
rta = (struct rtattr *) ((uint8_t *) m->hdr + m->hdr->nlmsg_len);
- /* update message size */
- m->hdr->nlmsg_len = message_length;
- /* we are inside a container, extend it */
- if (m->current_container)
- m->current_container->rta_len = (unsigned char *) m->hdr +
- m->hdr->nlmsg_len -
- (unsigned char *) m->current_container;
+ /* if we are inside a container, extend it */
+ if (CURRENT_CONTAINER(m))
+ CURRENT_CONTAINER(m)->rta_len += message_length - m->hdr->nlmsg_len;
/* fill in the attribute */
rta->rta_type = type;
rta->rta_len = rta_length;
if (!data) {
- /* this is a container, set pointer */
- m->current_container = rta;
+ /* this is the start of a new container */
+ m->container_offset = m->hdr->nlmsg_len;
} else {
/* we don't deal with the case where the user lies about the type
* and gives us too little data (so don't do that)
*/
padding = mempcpy(RTA_DATA(rta), data, data_length);
/* make sure also the padding at the end of the message is initialized */
- memset(padding, '\0', (unsigned char *) m->hdr +
- m->hdr->nlmsg_len -
- (unsigned char *) padding);
+ memzero(padding,
+ (uint8_t *) m->hdr + message_length - (uint8_t *) padding);
}
+ /* update message size */
+ m->hdr->nlmsg_len = message_length;
+
return 0;
}
-int sd_rtnl_message_append(sd_rtnl_message *m, unsigned short type, const void *data) {
+int sd_rtnl_message_append_string(sd_rtnl_message *m, unsigned short type, const char *data) {
uint16_t rtm_type;
- struct ifaddrmsg *ifa;
- struct rtmsg *rtm;
+ int r;
assert_return(m, -EINVAL);
assert_return(data, -EINVAL);
- sd_rtnl_message_get_type(m, &rtm_type);
+ r = sd_rtnl_message_get_type(m, &rtm_type);
+ if (r < 0)
+ return r;
- if (m->current_container) {
- switch (rtm_type) {
- case RTM_NEWLINK:
- case RTM_SETLINK:
- case RTM_GETLINK:
- case RTM_DELLINK:
- switch (m->current_container->rta_type) {
- case IFLA_LINKINFO:
- switch (type) {
- case IFLA_INFO_KIND:
- return add_rtattr(m, type, data, strlen(data) + 1);
- default:
- return -ENOTSUP;
- }
+ /* check that the type is correct */
+ switch (rtm_type) {
+ case RTM_NEWLINK:
+ case RTM_SETLINK:
+ case RTM_GETLINK:
+ case RTM_DELLINK:
+ if (CURRENT_CONTAINER(m)) {
+ if (CURRENT_CONTAINER(m)->rta_type != IFLA_LINKINFO ||
+ type != IFLA_INFO_KIND)
+ return -ENOTSUP;
+ } else {
+ switch (type) {
+ case IFLA_IFNAME:
+ case IFLA_IFALIAS:
+ case IFLA_QDISC:
+ break;
default:
return -ENOTSUP;
}
- default:
+ }
+ break;
+ case RTM_NEWADDR:
+ case RTM_GETADDR:
+ case RTM_DELADDR:
+ if (type != IFA_LABEL)
return -ENOTSUP;
- }
+ break;
+ default:
+ return -ENOTSUP;
}
+ r = add_rtattr(m, type, data, strlen(data) + 1);
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
+int sd_rtnl_message_append_u32(sd_rtnl_message *m, unsigned short type, uint32_t data) {
+ uint16_t rtm_type;
+ int r;
+
+ assert_return(m, -EINVAL);
+
+ r = sd_rtnl_message_get_type(m, &rtm_type);
+ if (r < 0)
+ return r;
+
+ /* check that the type is correct */
switch (rtm_type) {
case RTM_NEWLINK:
case RTM_SETLINK:
- case RTM_DELLINK:
case RTM_GETLINK:
+ case RTM_DELLINK:
switch (type) {
- case IFLA_IFNAME:
- case IFLA_IFALIAS:
- case IFLA_QDISC:
- return add_rtattr(m, type, data, strlen(data) + 1);
case IFLA_MASTER:
case IFLA_MTU:
case IFLA_LINK:
- return add_rtattr(m, type, data, sizeof(uint32_t));
- case IFLA_STATS:
- return add_rtattr(m, type, data, sizeof(struct rtnl_link_stats));
- case IFLA_ADDRESS:
- case IFLA_BROADCAST:
- return add_rtattr(m, type, data, ETH_ALEN);
+ break;
+ default:
+ return -ENOTSUP;
+ }
+ break;
+ case RTM_NEWROUTE:
+ case RTM_GETROUTE:
+ case RTM_DELROUTE:
+ switch (type) {
+ case RTA_TABLE:
+ case RTA_PRIORITY:
+ case RTA_IIF:
+ case RTA_OIF:
+ break;
default:
return -ENOTSUP;
}
+ break;
+ default:
+ return -ENOTSUP;
+ }
+
+ r = add_rtattr(m, type, &data, sizeof(uint32_t));
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
+int sd_rtnl_message_append_in_addr(sd_rtnl_message *m, unsigned short type, const struct in_addr *data) {
+ struct ifaddrmsg *ifa;
+ struct rtmsg *rtm;
+ uint16_t rtm_type;
+ int r;
+
+ assert_return(m, -EINVAL);
+ assert_return(data, -EINVAL);
+
+ r = sd_rtnl_message_get_type(m, &rtm_type);
+ if (r < 0)
+ return r;
+
+ /* check that the type is correct */
+ switch (rtm_type) {
case RTM_NEWADDR:
- case RTM_DELADDR:
case RTM_GETADDR:
+ case RTM_DELADDR:
switch (type) {
- case IFA_LABEL:
- return add_rtattr(m, type, data, strlen(data) + 1);
case IFA_ADDRESS:
case IFA_LOCAL:
case IFA_BROADCAST:
case IFA_ANYCAST:
ifa = NLMSG_DATA(m->hdr);
- switch (ifa->ifa_family) {
- case AF_INET:
- return add_rtattr(m, type, data, sizeof(struct in_addr));
- case AF_INET6:
- return add_rtattr(m, type, data, sizeof(struct in6_addr));
- default:
- return -EINVAL;
- }
+
+ if (ifa->ifa_family != AF_INET)
+ return -EINVAL;
+
+ break;
default:
return -ENOTSUP;
}
+ break;
case RTM_NEWROUTE:
+ case RTM_GETROUTE:
case RTM_DELROUTE:
+ switch (type) {
+ case RTA_DST:
+ case RTA_SRC:
+ case RTA_GATEWAY:
+ rtm = NLMSG_DATA(m->hdr);
+
+ if (rtm->rtm_family != AF_INET)
+ return -EINVAL;
+
+ break;
+ default:
+ return -ENOTSUP;
+ }
+ break;
+ default:
+ return -ENOTSUP;
+ }
+
+ r = add_rtattr(m, type, data, sizeof(struct in_addr));
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
+int sd_rtnl_message_append_in6_addr(sd_rtnl_message *m, unsigned short type, const struct in6_addr *data) {
+ struct ifaddrmsg *ifa;
+ struct rtmsg *rtm;
+ uint16_t rtm_type;
+ int r;
+
+ assert_return(m, -EINVAL);
+ assert_return(data, -EINVAL);
+
+ r = sd_rtnl_message_get_type(m, &rtm_type);
+ if (r < 0)
+ return r;
+
+ /* check that the type is correct */
+ switch (rtm_type) {
+ case RTM_NEWADDR:
+ case RTM_GETADDR:
+ case RTM_DELADDR:
+ switch (type) {
+ case IFA_ADDRESS:
+ case IFA_LOCAL:
+ case IFA_BROADCAST:
+ case IFA_ANYCAST:
+ ifa = NLMSG_DATA(m->hdr);
+
+ if (ifa->ifa_family != AF_INET6)
+ return -EINVAL;
+
+ break;
+ default:
+ return -ENOTSUP;
+ }
+ break;
+ case RTM_NEWROUTE:
case RTM_GETROUTE:
+ case RTM_DELROUTE:
switch (type) {
case RTA_DST:
case RTA_SRC:
case RTA_GATEWAY:
rtm = NLMSG_DATA(m->hdr);
- switch (rtm->rtm_family) {
- case AF_INET:
- return add_rtattr(m, type, data, sizeof(struct in_addr));
- case AF_INET6:
- return add_rtattr(m, type, data, sizeof(struct in6_addr));
- default:
- return -EINVAL;
- }
- case RTA_TABLE:
- case RTA_PRIORITY:
- case RTA_IIF:
- case RTA_OIF:
- return add_rtattr(m, type, data, sizeof(uint32_t));
+
+ if (rtm->rtm_family != AF_INET6)
+ return -EINVAL;
+
+ break;
default:
return -ENOTSUP;
}
default:
return -ENOTSUP;
}
+
+ r = add_rtattr(m, type, data, sizeof(struct in6_addr));
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
+int sd_rtnl_message_append_ether_addr(sd_rtnl_message *m, unsigned short type, const struct ether_addr *data) {
+ uint16_t rtm_type;
+ int r;
+
+ assert_return(m, -EINVAL);
+ assert_return(data, -EINVAL);
+
+ sd_rtnl_message_get_type(m, &rtm_type);
+
+ switch (rtm_type) {
+ case RTM_NEWLINK:
+ case RTM_SETLINK:
+ case RTM_DELLINK:
+ case RTM_GETLINK:
+ switch (type) {
+ case IFLA_ADDRESS:
+ case IFLA_BROADCAST:
+ break;
+ default:
+ return -ENOTSUP;
+ }
+ break;
+ default:
+ return -ENOTSUP;
+ }
+
+ r = add_rtattr(m, type, data, ETH_ALEN);
+ if (r < 0)
+ return r;
+
+ return 0;
}
int sd_rtnl_message_open_container(sd_rtnl_message *m, unsigned short type) {
uint16_t rtm_type;
assert_return(m, -EINVAL);
- assert_return(!m->current_container, -EINVAL);
+ assert_return(!CURRENT_CONTAINER(m), -EINVAL);
sd_rtnl_message_get_type(m, &rtm_type);
int sd_rtnl_message_close_container(sd_rtnl_message *m) {
assert_return(m, -EINVAL);
- assert_return(m->current_container, -EINVAL);
+ assert_return(CURRENT_CONTAINER(m), -EINVAL);
- m->current_container = NULL;
+ m->container_offset = 0;
return 0;
}
-static int message_read(sd_rtnl_message *m, unsigned short *type, void **data) {
+int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data) {
+ size_t remaining_size;
uint16_t rtm_type;
int r;
assert(m);
- assert(m->next_rta);
+ assert(m->next_rta_offset);
assert(type);
assert(data);
- if (!RTA_OK(m->next_rta, m->remaining_size))
+ remaining_size = m->hdr->nlmsg_len - m->next_rta_offset;
+
+ if (!RTA_OK(NEXT_RTA(m), remaining_size))
return 0;
/* make sure we don't try to read a container
if (r < 0)
return r;
- switch (rtm_type) {
- case RTM_NEWLINK:
- case RTM_GETLINK:
- case RTM_SETLINK:
- case RTM_DELLINK:
- if (m->next_rta->rta_type == IFLA_LINKINFO) {
- return -EINVAL;
- }
- }
+ if (message_type_is_link(rtm_type) &&
+ NEXT_RTA(m)->rta_type == IFLA_LINKINFO)
+ return -EINVAL;
- *data = RTA_DATA(m->next_rta);
- *type = m->next_rta->rta_type;
+ *data = RTA_DATA(NEXT_RTA(m));
+ *type = NEXT_RTA(m)->rta_type;
- m->next_rta = RTA_NEXT(m->next_rta, m->remaining_size);
+ UPDATE_RTA(m, RTA_NEXT(NEXT_RTA(m), remaining_size));
return 1;
}
-int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data) {
- uint16_t rtm_type;
- int r;
-
- assert_return(m, -EINVAL);
- assert_return(data, -EINVAL);
-
- r = sd_rtnl_message_get_type(m, &rtm_type);
- if (r < 0)
- return r;
-
- switch (rtm_type) {
- case RTM_NEWLINK:
- case RTM_SETLINK:
- case RTM_DELLINK:
- case RTM_GETLINK:
- if (!m->next_rta) {
- struct ifinfomsg *ifi = NLMSG_DATA(m->hdr);
-
- m->next_rta = IFLA_RTA(ifi);
- m->remaining_size = IFLA_PAYLOAD(m->hdr);
- }
- break;
- case RTM_NEWADDR:
- case RTM_DELADDR:
- case RTM_GETADDR:
- if (!m->next_rta) {
- struct ifaddrmsg *ifa = NLMSG_DATA(m->hdr);
-
- m->next_rta = IFA_RTA(ifa);
- m->remaining_size = IFA_PAYLOAD(m->hdr);
- }
- break;
- case RTM_NEWROUTE:
- case RTM_DELROUTE:
- case RTM_GETROUTE:
- if (!m->next_rta) {
- struct rtmesg *rtm = NLMSG_DATA(m->hdr);
-
- m->next_rta = RTM_RTA(rtm);
- m->remaining_size = RTM_PAYLOAD(m->hdr);
- }
- break;
- default:
- return -ENOTSUP;
- }
-
- return message_read(m, type, data);
-}
-
uint32_t message_get_serial(sd_rtnl_message *m) {
assert(m);
assert(m->hdr);
if (k > 0)
switch (m->hdr->nlmsg_type) {
+ struct ifinfomsg *ifi;
+ struct ifaddrmsg *ifa;
+ struct rtmsg *rtm;
+
/* check that the size matches the message type */
case NLMSG_ERROR:
if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
case RTM_GETLINK:
if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifinfomsg)))
k = -EIO;
+ else {
+ ifi = NLMSG_DATA(m->hdr);
+ UPDATE_RTA(m, IFLA_RTA(ifi));
+ }
break;
case RTM_NEWADDR:
case RTM_DELADDR:
case RTM_GETADDR:
if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifaddrmsg)))
k = -EIO;
+ else {
+ ifa = NLMSG_DATA(m->hdr);
+ UPDATE_RTA(m, IFA_RTA(ifa));
+ }
break;
case RTM_NEWROUTE:
case RTM_DELROUTE:
case RTM_GETROUTE:
if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtmsg)))
k = -EIO;
+ else {
+ rtm = NLMSG_DATA(m->hdr);
+ UPDATE_RTA(m, RTM_RTA(rtm));
+ }
break;
case NLMSG_NOOP:
k = 0;
return k;
}
+
+int sd_rtnl_message_rewind(sd_rtnl_message *m) {
+ struct ifinfomsg *ifi;
+ struct ifaddrmsg *ifa;
+ struct rtmsg *rtm;
+
+ assert_return(m, -EINVAL);
+ assert_return(m->hdr, -EINVAL);
+
+ switch(m->hdr->nlmsg_type) {
+ case RTM_NEWLINK:
+ case RTM_SETLINK:
+ case RTM_GETLINK:
+ case RTM_DELLINK:
+ ifi = NLMSG_DATA(m->hdr);
+ UPDATE_RTA(m, IFLA_RTA(ifi));
+
+ break;
+ case RTM_NEWADDR:
+ case RTM_GETADDR:
+ case RTM_DELADDR:
+ ifa = NLMSG_DATA(m->hdr);
+ UPDATE_RTA(m, IFA_RTA(ifa));
+
+ break;
+ case RTM_NEWROUTE:
+ case RTM_GETROUTE:
+ case RTM_DELROUTE:
+ rtm = NLMSG_DATA(m->hdr);
+ UPDATE_RTA(m, RTM_RTA(rtm));
+
+ break;
+ default:
+ return -ENOTSUP;
+ }
+
+ return 0;
+}