Changeset View
Changeset View
Standalone View
Standalone View
sys/netlink/netlink_snl.h
| Show First 20 Lines • Show All 718 Lines • ▼ Show 20 Lines | snl_attr_get_nested(struct snl_state *ss, struct nlattr *nla, const void *arg, void *target) | ||||
| /* Assumes target points to the beginning of the structure */ | /* Assumes target points to the beginning of the structure */ | ||||
| return (snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), p, target)); | return (snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), p, target)); | ||||
| } | } | ||||
| struct snl_parray { | struct snl_parray { | ||||
| uint32_t count; | uint32_t count; | ||||
| void **items; | void **items; | ||||
| uint32_t _capacity; | |||||
| }; | }; | ||||
| static inline bool | static inline bool | ||||
| snl_attr_get_parray_sz(struct snl_state *ss, struct nlattr *container_nla, | snl_parray_append(struct snl_state *ss, struct snl_parray *array, void *item, | ||||
| uint32_t start_size, const void *arg, void *target) | uint32_t start_size) | ||||
| { | { | ||||
| const struct snl_hdr_parser *p = (const struct snl_hdr_parser *)arg; | void **new_items; | ||||
| struct snl_parray *array = (struct snl_parray *)target; | size_t alloc_size; | ||||
| struct nlattr *nla; | uint32_t new_size; | ||||
| uint32_t count = 0, size = start_size; | |||||
| if (p->out_size == 0) | if (start_size == 0) | ||||
| return (false); | return (false); | ||||
| if (array->_capacity == 0) { | |||||
| array->items = (void **)snl_allocz(ss, size * sizeof(void *)); | new_size = start_size; | ||||
| if (__builtin_mul_overflow((size_t)new_size, | |||||
| sizeof(*new_items), &alloc_size)) | |||||
| return (false); | |||||
| array->items = (void **)snl_allocz(ss, alloc_size); | |||||
| if (array->items == NULL) | if (array->items == NULL) | ||||
| return (false); | return (false); | ||||
| array->_capacity = new_size; | |||||
| } else if (array->count == array->_capacity) { | |||||
| if (array->_capacity > UINT32_MAX / 2) | |||||
| return (false); | |||||
| new_size = array->_capacity * 2; | |||||
| if (__builtin_mul_overflow((size_t)new_size, | |||||
| sizeof(*new_items), &alloc_size)) | |||||
| return (false); | |||||
| new_items = (void **)snl_allocz(ss, alloc_size); | |||||
| if (new_items == NULL) | |||||
| return (false); | |||||
| memcpy(new_items, array->items, | |||||
| array->count * sizeof(*new_items)); | |||||
| array->items = new_items; | |||||
| array->_capacity = new_size; | |||||
| } | |||||
| array->items[array->count++] = item; | |||||
| return (true); | |||||
| } | |||||
| static inline bool | |||||
| snl_attr_get_parray_sz(struct snl_state *ss, struct nlattr *container_nla, | |||||
| uint32_t start_size, const void *arg, struct snl_parray *array) | |||||
| { | |||||
| const struct snl_hdr_parser *p = (const struct snl_hdr_parser *)arg; | |||||
| struct nlattr *nla; | |||||
| if (p->out_size == 0 || start_size == 0) | |||||
| return (false); | |||||
| /* A container attribute replaces, rather than extends, its output. */ | |||||
| array->count = 0; | |||||
| array->items = NULL; | |||||
| array->_capacity = 0; | |||||
| /* | /* | ||||
| * If the provided parser is an attribute parser, assume that each | * If the provided parser is an attribute parser, assume that each | ||||
| * nla in the container nla is the container nla itself and parse | * nla in the container nla is the container nla itself and parse | ||||
| * the contents of this nla. | * the contents of this nla. | ||||
| * Otherwise, run the parser on raw data, assuming the header of this | * Otherwise, run the parser on raw data, assuming the header of this | ||||
| * data has u16 field with total size in the beginning. | * data has u16 field with total size in the beginning. | ||||
| */ | */ | ||||
| Show All 9 Lines | if (item == NULL) | ||||
| return (false); | return (false); | ||||
| void *data = (char *)(void *)nla + data_off; | void *data = (char *)(void *)nla + data_off; | ||||
| int data_len = nla->nla_len - data_off; | int data_len = nla->nla_len - data_off; | ||||
| if (!(snl_parse_header(ss, data, data_len, p, item))) | if (!(snl_parse_header(ss, data, data_len, p, item))) | ||||
| return (false); | return (false); | ||||
| if (count == size) { | if (!snl_parray_append(ss, array, item, start_size)) | ||||
| uint32_t new_size = size * 2; | return (false); | ||||
| void **new_array = (void **)snl_allocz(ss, new_size *sizeof(void *)); | |||||
| memcpy(new_array, array->items, size * sizeof(void *)); | |||||
| array->items = new_array; | |||||
| size = new_size; | |||||
| } | } | ||||
| array->items[count++] = item; | |||||
| } | |||||
| array->count = count; | |||||
| return (true); | return (true); | ||||
| } | } | ||||
| /* | /* | ||||
| * Parses and stores the unknown-size array. | * Parses and stores the unknown-size array. | ||||
| * Assumes each array item is a container and the NLAs in the container are parsable | * Assumes each array item is a container and the NLAs in the container are | ||||
| * by the parser provided in @arg. | * parsable by the parser provided in @arg. | ||||
| * Assumes @target is struct snl_parray | * @target must point to an actual struct snl_parray. Do not substitute a | ||||
| * layout-compatible structure: the parser array includes private growth state. | |||||
| */ | */ | ||||
| static inline bool | static inline bool | ||||
| snl_attr_get_parray(struct snl_state *ss, struct nlattr *nla, const void *arg, void *target) | snl_attr_get_parray(struct snl_state *ss, struct nlattr *nla, const void *arg, void *target) | ||||
| { | { | ||||
| return (snl_attr_get_parray_sz(ss, nla, 8, arg, target)); | return (snl_attr_get_parray_sz(ss, nla, 8, arg, | ||||
| (struct snl_parray *)target)); | |||||
| } | } | ||||
| /* | |||||
| * Append one repeated nested attribute to an array. Unlike | |||||
| * snl_attr_get_parray(), the attribute itself is one array member rather | |||||
| * than a container holding an entire array. Use this for modern Netlink | |||||
| * multi-attributes. @target must point to an actual struct snl_parray. | |||||
| */ | |||||
| static inline bool | |||||
| snl_attr_get_multi(struct snl_state *ss, struct nlattr *nla, const void *arg, | |||||
| void *target) | |||||
| { | |||||
| const struct snl_hdr_parser *p = arg; | |||||
| struct snl_parray *array = target; | |||||
| void *item; | |||||
| if (p->out_size == 0) | |||||
| return (false); | |||||
| item = snl_allocz(ss, p->out_size); | |||||
| if (item == NULL) | |||||
| return (false); | |||||
| if (!snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), p, item)) | |||||
| return (false); | |||||
| return (snl_parray_append(ss, array, item, 8)); | |||||
| } | |||||
| static inline bool | static inline bool | ||||
| snl_attr_get_nla(struct snl_state *ss __unused, struct nlattr *nla, | snl_attr_get_nla(struct snl_state *ss __unused, struct nlattr *nla, | ||||
| const void *arg __unused, void *target) | const void *arg __unused, void *target) | ||||
| { | { | ||||
| *((struct nlattr **)target) = nla; | *((struct nlattr **)target) = nla; | ||||
| return (true); | return (true); | ||||
| } | } | ||||
| Show All 38 Lines | snl_attr_dup_struct(struct snl_state *ss, struct nlattr *nla, | ||||
| } | } | ||||
| return (false); | return (false); | ||||
| } | } | ||||
| struct snl_attr_bit { | struct snl_attr_bit { | ||||
| uint32_t bit_index; | uint32_t bit_index; | ||||
| char *bit_name; | char *bit_name; | ||||
| int bit_value; | int bit_value; | ||||
| }; | |||||
| struct snl_attr_bits { | |||||
| uint32_t num_bits; | |||||
| struct snl_attr_bit **bits; | |||||
| }; | }; | ||||
| #define _OUT(_field) offsetof(struct snl_attr_bit, _field) | #define _OUT(_field) offsetof(struct snl_attr_bit, _field) | ||||
| static const struct snl_attr_parser _nla_p_bit[] = { | static const struct snl_attr_parser _nla_p_bit[] = { | ||||
| { .type = NLA_BITSET_BIT_INDEX, .off = _OUT(bit_index), .cb = snl_attr_get_uint32 }, | { .type = NLA_BITSET_BIT_INDEX, .off = _OUT(bit_index), .cb = snl_attr_get_uint32 }, | ||||
| { .type = NLA_BITSET_BIT_NAME, .off = _OUT(bit_name), .cb = snl_attr_dup_string }, | { .type = NLA_BITSET_BIT_NAME, .off = _OUT(bit_name), .cb = snl_attr_dup_string }, | ||||
| { .type = NLA_BITSET_BIT_VALUE, .off = _OUT(bit_value), .cb = snl_attr_get_flag }, | { .type = NLA_BITSET_BIT_VALUE, .off = _OUT(bit_value), .cb = snl_attr_get_flag }, | ||||
| }; | }; | ||||
| #undef _OUT | #undef _OUT | ||||
| SNL_DECLARE_ATTR_PARSER_EXT(_nla_bit_parser, sizeof(struct snl_attr_bit), _nla_p_bit, NULL); | SNL_DECLARE_ATTR_PARSER_EXT(_nla_bit_parser, sizeof(struct snl_attr_bit), _nla_p_bit, NULL); | ||||
| struct snl_attr_bitset { | struct snl_attr_bitset { | ||||
| uint32_t nla_bitset_size; | uint32_t nla_bitset_size; | ||||
| uint32_t *nla_bitset_mask; | uint32_t *nla_bitset_mask; | ||||
| uint32_t *nla_bitset_value; | uint32_t *nla_bitset_value; | ||||
| struct snl_attr_bits bits; | struct snl_parray bits; | ||||
| }; | }; | ||||
| #define _OUT(_field) offsetof(struct snl_attr_bitset, _field) | #define _OUT(_field) offsetof(struct snl_attr_bitset, _field) | ||||
| static const struct snl_attr_parser _nla_p_bitset[] = { | static const struct snl_attr_parser _nla_p_bitset[] = { | ||||
| { .type = NLA_BITSET_SIZE, .off = _OUT(nla_bitset_size), .cb = snl_attr_get_uint32 }, | { .type = NLA_BITSET_SIZE, .off = _OUT(nla_bitset_size), .cb = snl_attr_get_uint32 }, | ||||
| { .type = NLA_BITSET_BITS, .off = _OUT(bits), .cb = snl_attr_get_parray, .arg = &_nla_bit_parser }, | { .type = NLA_BITSET_BITS, .off = _OUT(bits), .cb = snl_attr_get_parray, .arg = &_nla_bit_parser }, | ||||
| { .type = NLA_BITSET_VALUE, .off = _OUT(nla_bitset_mask), .cb = snl_attr_dup_nla }, | { .type = NLA_BITSET_VALUE, .off = _OUT(nla_bitset_mask), .cb = snl_attr_dup_nla }, | ||||
| { .type = NLA_BITSET_MASK, .off = _OUT(nla_bitset_value), .cb = snl_attr_dup_nla }, | { .type = NLA_BITSET_MASK, .off = _OUT(nla_bitset_value), .cb = snl_attr_dup_nla }, | ||||
| ▲ Show 20 Lines • Show All 473 Lines • Show Last 20 Lines | |||||