]> www.infradead.org Git - users/hch/misc.git/commitdiff
can: netlink: refactor can_validate_bittiming()
authorVincent Mailhol <mailhol@kernel.org>
Tue, 23 Sep 2025 06:58:29 +0000 (15:58 +0900)
committerMarc Kleine-Budde <mkl@pengutronix.de>
Wed, 24 Sep 2025 15:09:44 +0000 (17:09 +0200)
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>
drivers/net/can/dev/netlink.c

index 248f607e3864ffbda6f0b8daf4e2484179cf9cd5..13555253e789ec6ca9c8c30571c990ad6bfde770 100644 (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;
 }