libifconfig: fix carp key configuration

There were two issues with the carp key configuration in the new netlink
code.

The first is that userspace failed to actually pass the CARP_NL_KEY
attribute to the kernel, so a key was never set.

The second issue is that snl_attr_get_string() returns a pointer to the
string inside the netlink message. It does not copy the string to the
target buffer. That's somewhat inconvenient to work with in libifconfig
where we have a static buffer for the key.
Introduce snl_attr_copy_string() which can copy a string to a target
buffer and uses the 'arg' parameter to pass the buffer size, so it
doesn't accidentally exceed the available space.

Reviewed by:	melifaro
Sponsored by:	Rubicon Communications, LLC ("Netgate")
Differential Revision:	https://reviews.freebsd.org/D39874
This commit is contained in:
Kristof Provost 2023-04-28 18:18:55 +02:00
parent a50ef47c0a
commit 6a23843a4a
2 changed files with 21 additions and 2 deletions

View File

@ -55,7 +55,7 @@ static struct snl_attr_parser ap_carp_get[] = {
{ .type = CARP_NL_STATE, .off = _OUT(carpr_state), .cb = snl_attr_get_uint32 },
{ .type = CARP_NL_ADVBASE, .off = _OUT(carpr_advbase), .cb = snl_attr_get_int32 },
{ .type = CARP_NL_ADVSKEW, .off = _OUT(carpr_advskew), .cb = snl_attr_get_int32 },
{ .type = CARP_NL_KEY, .off = _OUT(carpr_key), .cb = snl_attr_get_string },
{ .type = CARP_NL_KEY, .off = _OUT(carpr_key), .cb = snl_attr_copy_string, .arg_u32 = CARP_KEY_LEN },
{ .type = CARP_NL_ADDR, .off = _OUT(carpr_addr), .cb = snl_attr_get_in_addr },
{ .type = CARP_NL_ADDR6, .off = _OUT(carpr_addr6), .cb = snl_attr_get_in6_addr },
};
@ -176,6 +176,7 @@ ifconfig_carp_set_info(ifconfig_handle_t *h, const char *name,
&carpr->carpr_addr);
snl_add_msg_attr(&nw, CARP_NL_ADDR6, sizeof(carpr->carpr_addr6),
&carpr->carpr_addr6);
snl_add_msg_attr_string(&nw, CARP_NL_KEY, carpr->carpr_key);
hdr = snl_finalize_msg(&nw);
if (hdr == NULL) {

View File

@ -138,7 +138,12 @@ struct snl_attr_parser {
uint16_t type; /* Attribute type */
uint16_t off; /* field offset in the target structure */
snl_parse_attr_f *cb; /* parser function to call */
const void *arg; /* Optional argument parser */
/* Optional parser argument */
union {
const void *arg;
const uint32_t arg_u32;
};
};
struct snl_hdr_parser {
@ -588,6 +593,19 @@ snl_attr_get_stringn(struct snl_state *ss, struct nlattr *nla,
return (true);
}
static inline bool
snl_attr_copy_string(struct snl_state *ss, struct nlattr *nla,
const void *arg, void *target)
{
char *tmp;
if (snl_attr_get_string(ss, nla, NULL, &tmp)) {
strlcpy(target, tmp, (size_t)arg);
return (true);
}
return (false);
}
static inline bool
snl_attr_get_nested(struct snl_state *ss, struct nlattr *nla, const void *arg, void *target)
{