mirror of
https://github.com/torvalds/linux.git
synced 2026-06-04 04:23:35 +02:00
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:
parent
94040a8f48
commit
f5ae5a7541
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user