X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flibsystemd%2Fsd-netlink%2Fnetlink-message.c;h=887327c9ae20f90e65f4881556e833f12734be98;hp=e39e4c646c5cded12b41078afb6453a61704a92b;hb=be00b784a382b68bc225cae8b2a6664882362a7e;hpb=724ccdb94309bde65b4e067f7d61f0170e0bbdb2 diff --git a/src/libsystemd/sd-netlink/netlink-message.c b/src/libsystemd/sd-netlink/netlink-message.c index e39e4c646..887327c9a 100644 --- a/src/libsystemd/sd-netlink/netlink-message.c +++ b/src/libsystemd/sd-netlink/netlink-message.c @@ -72,14 +72,11 @@ int message_new(sd_netlink *rtnl, sd_netlink_message **ret, uint16_t type) { if (r < 0) return r; - if (type_get_type(nl_type) != NETLINK_TYPE_NESTED) - return -EINVAL; - r = message_new_empty(rtnl, &m); if (r < 0) return r; - size = NLMSG_SPACE(type_get_size(nl_type)); + size = NLMSG_SPACE(nl_type->size); assert(size >= sizeof(struct nlmsghdr)); m->hdr = malloc0(size); @@ -88,7 +85,8 @@ int message_new(sd_netlink *rtnl, sd_netlink_message **ret, uint16_t type) { m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; - type_get_type_system(nl_type, &m->container_type_system[0]); + if (nl_type->type == NETLINK_TYPE_NESTED) + type_get_type_system(nl_type, &m->container_type_system[0]); m->hdr->nlmsg_len = size; m->hdr->nlmsg_type = type; @@ -217,22 +215,18 @@ static int add_rtattr(sd_netlink_message *m, unsigned short type, const void *da return offset; } -static int message_attribute_has_type(sd_netlink_message *m, size_t *out_size, uint16_t attribute_type, uint16_t data_type) { +static int message_attribute_has_type(sd_netlink_message *m, uint16_t attribute_type, uint16_t data_type) { const NLType *type; int r; - assert(m); - r = type_system_get_type(m->container_type_system[m->n_containers], &type, attribute_type); if (r < 0) return r; - if (type_get_type(type) != data_type) + if (type->type != data_type) return -EINVAL; - if (out_size) - *out_size = type_get_size(type); - return 0; + return type->size; } int sd_netlink_message_append_string(sd_netlink_message *m, unsigned short type, const char *data) { @@ -243,9 +237,11 @@ int sd_netlink_message_append_string(sd_netlink_message *m, unsigned short type, assert_return(!m->sealed, -EPERM); assert_return(data, -EINVAL); - r = message_attribute_has_type(m, &size, type, NETLINK_TYPE_STRING); + r = message_attribute_has_type(m, type, NETLINK_TYPE_STRING); if (r < 0) return r; + else + size = (size_t)r; if (size) { length = strnlen(data, size+1); @@ -267,7 +263,7 @@ int sd_netlink_message_append_u8(sd_netlink_message *m, unsigned short type, uin assert_return(m, -EINVAL); assert_return(!m->sealed, -EPERM); - r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U8); + r = message_attribute_has_type(m, type, NETLINK_TYPE_U8); if (r < 0) return r; @@ -285,7 +281,7 @@ int sd_netlink_message_append_u16(sd_netlink_message *m, unsigned short type, ui assert_return(m, -EINVAL); assert_return(!m->sealed, -EPERM); - r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U16); + r = message_attribute_has_type(m, type, NETLINK_TYPE_U16); if (r < 0) return r; @@ -302,7 +298,7 @@ int sd_netlink_message_append_u32(sd_netlink_message *m, unsigned short type, ui assert_return(m, -EINVAL); assert_return(!m->sealed, -EPERM); - r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U32); + r = message_attribute_has_type(m, type, NETLINK_TYPE_U32); if (r < 0) return r; @@ -320,7 +316,7 @@ int sd_netlink_message_append_in_addr(sd_netlink_message *m, unsigned short type assert_return(!m->sealed, -EPERM); assert_return(data, -EINVAL); - r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR); + r = message_attribute_has_type(m, type, NETLINK_TYPE_IN_ADDR); if (r < 0) return r; @@ -338,7 +334,7 @@ int sd_netlink_message_append_in6_addr(sd_netlink_message *m, unsigned short typ assert_return(!m->sealed, -EPERM); assert_return(data, -EINVAL); - r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR); + r = message_attribute_has_type(m, type, NETLINK_TYPE_IN_ADDR); if (r < 0) return r; @@ -356,7 +352,7 @@ int sd_netlink_message_append_ether_addr(sd_netlink_message *m, unsigned short t assert_return(!m->sealed, -EPERM); assert_return(data, -EINVAL); - r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_ETHER_ADDR); + r = message_attribute_has_type(m, type, NETLINK_TYPE_ETHER_ADDR); if (r < 0) return r; @@ -374,7 +370,7 @@ int sd_netlink_message_append_cache_info(sd_netlink_message *m, unsigned short t assert_return(!m->sealed, -EPERM); assert_return(info, -EINVAL); - r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_CACHE_INFO); + r = message_attribute_has_type(m, type, NETLINK_TYPE_CACHE_INFO); if (r < 0) return r; @@ -393,14 +389,15 @@ int sd_netlink_message_open_container(sd_netlink_message *m, unsigned short type assert_return(!m->sealed, -EPERM); assert_return(m->n_containers < RTNL_CONTAINER_DEPTH, -ERANGE); - r = message_attribute_has_type(m, &size, type, NETLINK_TYPE_NESTED); + r = message_attribute_has_type(m, type, NETLINK_TYPE_NESTED); if (r < 0) { const NLTypeSystemUnion *type_system_union; int family; - r = message_attribute_has_type(m, &size, type, NETLINK_TYPE_UNION); + r = message_attribute_has_type(m, type, NETLINK_TYPE_UNION); if (r < 0) return r; + size = (size_t) r; r = sd_rtnl_message_get_family(m, &family); if (r < 0) @@ -416,6 +413,8 @@ int sd_netlink_message_open_container(sd_netlink_message *m, unsigned short type if (r < 0) return r; } else { + size = (size_t)r; + r = type_system_get_type_system(m->container_type_system[m->n_containers], &m->container_type_system[m->n_containers + 1], type); @@ -501,7 +500,7 @@ int sd_netlink_message_read_string(sd_netlink_message *m, unsigned short type, c assert_return(m, -EINVAL); - r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_STRING); + r = message_attribute_has_type(m, type, NETLINK_TYPE_STRING); if (r < 0) return r; @@ -523,7 +522,7 @@ int sd_netlink_message_read_u8(sd_netlink_message *m, unsigned short type, uint8 assert_return(m, -EINVAL); - r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U8); + r = message_attribute_has_type(m, type, NETLINK_TYPE_U8); if (r < 0) return r; @@ -545,7 +544,7 @@ int sd_netlink_message_read_u16(sd_netlink_message *m, unsigned short type, uint assert_return(m, -EINVAL); - r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U16); + r = message_attribute_has_type(m, type, NETLINK_TYPE_U16); if (r < 0) return r; @@ -567,7 +566,7 @@ int sd_netlink_message_read_u32(sd_netlink_message *m, unsigned short type, uint assert_return(m, -EINVAL); - r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U32); + r = message_attribute_has_type(m, type, NETLINK_TYPE_U32); if (r < 0) return r; @@ -589,7 +588,7 @@ int sd_netlink_message_read_ether_addr(sd_netlink_message *m, unsigned short typ assert_return(m, -EINVAL); - r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_ETHER_ADDR); + r = message_attribute_has_type(m, type, NETLINK_TYPE_ETHER_ADDR); if (r < 0) return r; @@ -611,7 +610,7 @@ int sd_netlink_message_read_cache_info(sd_netlink_message *m, unsigned short typ assert_return(m, -EINVAL); - r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_CACHE_INFO); + r = message_attribute_has_type(m, type, NETLINK_TYPE_CACHE_INFO); if (r < 0) return r; @@ -633,7 +632,7 @@ int sd_netlink_message_read_in_addr(sd_netlink_message *m, unsigned short type, assert_return(m, -EINVAL); - r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR); + r = message_attribute_has_type(m, type, NETLINK_TYPE_IN_ADDR); if (r < 0) return r; @@ -655,7 +654,7 @@ int sd_netlink_message_read_in6_addr(sd_netlink_message *m, unsigned short type, assert_return(m, -EINVAL); - r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR); + r = message_attribute_has_type(m, type, NETLINK_TYPE_IN_ADDR); if (r < 0) return r; @@ -671,11 +670,10 @@ int sd_netlink_message_read_in6_addr(sd_netlink_message *m, unsigned short type, return 0; } -int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short type_id) { +int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short type) { const NLType *nl_type; const NLTypeSystem *type_system; void *container; - uint16_t type; size_t size; int r; @@ -684,24 +682,22 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ r = type_system_get_type(m->container_type_system[m->n_containers], &nl_type, - type_id); + type); if (r < 0) return r; - type = type_get_type(nl_type); - - if (type == NETLINK_TYPE_NESTED) { + if (nl_type->type == NETLINK_TYPE_NESTED) { r = type_system_get_type_system(m->container_type_system[m->n_containers], &type_system, - type_id); + type); if (r < 0) return r; - } else if (type == NETLINK_TYPE_UNION) { + } else if (nl_type->type == NETLINK_TYPE_UNION) { const NLTypeSystemUnion *type_system_union; r = type_system_get_type_system_union(m->container_type_system[m->n_containers], &type_system_union, - type_id); + type); if (r < 0) return r; @@ -744,7 +740,7 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ } else return -EINVAL; - r = rtnl_message_read_internal(m, type_id, &container); + r = rtnl_message_read_internal(m, type, &container); if (r < 0) return r; else @@ -755,7 +751,7 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ r = rtnl_message_parse(m, &m->rta_offset_tb[m->n_containers], &m->rta_tb_size[m->n_containers], - type_system_get_count(type_system), + type_system->max, container, size); if (r < 0) { @@ -813,17 +809,17 @@ int sd_netlink_message_get_errno(sd_netlink_message *m) { int rtnl_message_parse(sd_netlink_message *m, size_t **rta_offset_tb, unsigned short *rta_tb_size, - int count, + int max, struct rtattr *rta, unsigned int rt_len) { unsigned short type; size_t *tb; - tb = new0(size_t, count); + tb = new0(size_t, max + 1); if(!tb) return -ENOMEM; - *rta_tb_size = count; + *rta_tb_size = max + 1; for (; RTA_OK(rta, rt_len); rta = RTA_NEXT(rta, rt_len)) { type = RTA_TYPE(rta); @@ -831,7 +827,7 @@ int rtnl_message_parse(sd_netlink_message *m, /* if the kernel is newer than the headers we used when building, we ignore out-of-range attributes */ - if (type >= count) + if (type > max) continue; if (tb[type]) @@ -846,9 +842,7 @@ int rtnl_message_parse(sd_netlink_message *m, } int sd_netlink_message_rewind(sd_netlink_message *m) { - const NLType *nl_type; - uint16_t type; - size_t size; + const NLType *type; unsigned i; int r; @@ -874,26 +868,24 @@ int sd_netlink_message_rewind(sd_netlink_message *m) { assert(m->hdr); - r = type_system_get_type(NULL, &nl_type, m->hdr->nlmsg_type); + r = type_system_get_type(NULL, &type, m->hdr->nlmsg_type); if (r < 0) return r; - type = type_get_type(nl_type); - size = type_get_size(nl_type); - - if (type == NETLINK_TYPE_NESTED) { + if (type->type == NETLINK_TYPE_NESTED) { const NLTypeSystem *type_system; - type_get_type_system(nl_type, &type_system); + type_get_type_system(type, &type_system); m->container_type_system[0] = type_system; r = rtnl_message_parse(m, &m->rta_offset_tb[m->n_containers], &m->rta_tb_size[m->n_containers], - type_system_get_count(type_system), - (struct rtattr*)((uint8_t*)NLMSG_DATA(m->hdr) + NLMSG_ALIGN(size)), - NLMSG_PAYLOAD(m->hdr, size)); + type_system->max, + (struct rtattr*)((uint8_t*)NLMSG_DATA(m->hdr) + + NLMSG_ALIGN(type->size)), + NLMSG_PAYLOAD(m->hdr, type->size)); if (r < 0) return r; }