can: netlink: refactor can_validate_bittiming()

Whenever can_validate_bittiming() is called, it is always preceded by
some boilerplate code which was copy pasted all over the place. Move
that repeated code directly inside can_validate_bittiming().

Finally, the mempcy() is not needed: the nla attributes are four bytes
aligned which is just enough for struct can_bittiming. Add a
static_assert() to document that the alignment is correct and just use
the pointer returned by nla_data() as-is.

Signed-off-by: Vincent Mailhol <mailhol@kernel.org>
Link: https://patch.msgid.link/20250923-canxl-netlink-prep-v4-4-e720d28f66fe@kernel.org
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
This commit is contained in:
Vincent Mailhol 2025-09-23 15:58:29 +09:00 committed by Marc Kleine-Budde
parent 94040a8f48
commit f5ae5a7541

View File

@ -36,13 +36,21 @@ static const struct nla_policy can_tdc_policy[IFLA_CAN_TDC_MAX + 1] = {
[IFLA_CAN_TDC_TDCF] = { .type = NLA_U32 },
};
static int can_validate_bittiming(const struct can_bittiming *bt,
struct netlink_ext_ack *extack)
static int can_validate_bittiming(struct nlattr *data[],
struct netlink_ext_ack *extack,
int ifla_can_bittiming)
{
struct can_bittiming *bt;
if (!data[ifla_can_bittiming])
return 0;
static_assert(__alignof__(*bt) <= NLA_ALIGNTO);
bt = nla_data(data[ifla_can_bittiming]);
/* sample point is in one-tenth of a percent */
if (bt->sample_point >= 1000) {
NL_SET_ERR_MSG(extack, "sample point must be between 0 and 100%");
return -EINVAL;
}
@ -105,14 +113,9 @@ static int can_validate(struct nlattr *tb[], struct nlattr *data[],
}
}
if (data[IFLA_CAN_BITTIMING]) {
struct can_bittiming bt;
memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
err = can_validate_bittiming(&bt, extack);
if (err)
return err;
}
err = can_validate_bittiming(data, extack, IFLA_CAN_BITTIMING);
if (err)
return err;
if (is_can_fd) {
if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
@ -124,14 +127,9 @@ static int can_validate(struct nlattr *tb[], struct nlattr *data[],
return -EOPNOTSUPP;
}
if (data[IFLA_CAN_DATA_BITTIMING]) {
struct can_bittiming bt;
memcpy(&bt, nla_data(data[IFLA_CAN_DATA_BITTIMING]), sizeof(bt));
err = can_validate_bittiming(&bt, extack);
if (err)
return err;
}
err = can_validate_bittiming(data, extack, IFLA_CAN_DATA_BITTIMING);
if (err)
return err;
return 0;
}