Skip to content

Commit bb208f1

Browse files
hartkoppmarckleinebudde
authored andcommitted
can: fix handling of unmodifiable configuration options
As described in 'can: m_can: tag current CAN FD controllers as non-ISO' (6cfda7f) it is possible to define fixed configuration options by setting the according bit in 'ctrlmode' and clear it in 'ctrlmode_supported'. This leads to the incovenience that the fixed configuration bits can not be passed by netlink even when they have the correct values (e.g. non-ISO, FD). This patch fixes that issue and not only allows fixed set bit values to be set again but now requires(!) to provide these fixed values at configuration time. A valid CAN FD configuration consists of a nominal/arbitration bittiming, a data bittiming and a control mode with CAN_CTRLMODE_FD set - which is now enforced by a new can_validate() function. This fix additionally removed the inconsistency that was prohibiting the support of 'CANFD-only' controller drivers, like the RCar CAN FD. For this reason a new helper can_set_static_ctrlmode() has been introduced to provide a proper interface to handle static enabled CAN controller options. Reported-by: Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com> Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net> Reviewed-by: Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com> Cc: <stable@vger.kernel.org> # >= 3.18 Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
1 parent b6fd3ab commit bb208f1

3 files changed

Lines changed: 73 additions & 7 deletions

File tree

‎drivers/net/can/dev.c‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -696,11 +696,17 @@ int can_change_mtu(struct net_device *dev, int new_mtu)
696696
/* allow change of MTU according to the CANFD ability of the device */
697697
switch (new_mtu) {
698698
case CAN_MTU:
699+
/* 'CANFD-only' controllers can not switch to CAN_MTU */
700+
if (priv->ctrlmode_static & CAN_CTRLMODE_FD)
701+
return -EINVAL;
702+
699703
priv->ctrlmode &= ~CAN_CTRLMODE_FD;
700704
break;
701705

702706
case CANFD_MTU:
703-
if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD))
707+
/* check for potential CANFD ability */
708+
if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
709+
!(priv->ctrlmode_static & CAN_CTRLMODE_FD))
704710
return -EINVAL;
705711

706712
priv->ctrlmode |= CAN_CTRLMODE_FD;
@@ -782,6 +788,35 @@ static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
782788
= { .len = sizeof(struct can_bittiming_const) },
783789
};
784790

791+
static int can_validate(struct nlattr *tb[], struct nlattr *data[])
792+
{
793+
bool is_can_fd = false;
794+
795+
/* Make sure that valid CAN FD configurations always consist of
796+
* - nominal/arbitration bittiming
797+
* - data bittiming
798+
* - control mode with CAN_CTRLMODE_FD set
799+
*/
800+
801+
if (data[IFLA_CAN_CTRLMODE]) {
802+
struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
803+
804+
is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
805+
}
806+
807+
if (is_can_fd) {
808+
if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
809+
return -EOPNOTSUPP;
810+
}
811+
812+
if (data[IFLA_CAN_DATA_BITTIMING]) {
813+
if (!is_can_fd || !data[IFLA_CAN_BITTIMING])
814+
return -EOPNOTSUPP;
815+
}
816+
817+
return 0;
818+
}
819+
785820
static int can_changelink(struct net_device *dev,
786821
struct nlattr *tb[], struct nlattr *data[])
787822
{
@@ -813,19 +848,31 @@ static int can_changelink(struct net_device *dev,
813848

814849
if (data[IFLA_CAN_CTRLMODE]) {
815850
struct can_ctrlmode *cm;
851+
u32 ctrlstatic;
852+
u32 maskedflags;
816853

817854
/* Do not allow changing controller mode while running */
818855
if (dev->flags & IFF_UP)
819856
return -EBUSY;
820857
cm = nla_data(data[IFLA_CAN_CTRLMODE]);
858+
ctrlstatic = priv->ctrlmode_static;
859+
maskedflags = cm->flags & cm->mask;
860+
861+
/* check whether provided bits are allowed to be passed */
862+
if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic))
863+
return -EOPNOTSUPP;
864+
865+
/* do not check for static fd-non-iso if 'fd' is disabled */
866+
if (!(maskedflags & CAN_CTRLMODE_FD))
867+
ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
821868

822-
/* check whether changed bits are allowed to be modified */
823-
if (cm->mask & ~priv->ctrlmode_supported)
869+
/* make sure static options are provided by configuration */
870+
if ((maskedflags & ctrlstatic) != ctrlstatic)
824871
return -EOPNOTSUPP;
825872

826873
/* clear bits to be modified and copy the flag values */
827874
priv->ctrlmode &= ~cm->mask;
828-
priv->ctrlmode |= (cm->flags & cm->mask);
875+
priv->ctrlmode |= maskedflags;
829876

830877
/* CAN_CTRLMODE_FD can only be set when driver supports FD */
831878
if (priv->ctrlmode & CAN_CTRLMODE_FD)
@@ -966,6 +1013,7 @@ static struct rtnl_link_ops can_link_ops __read_mostly = {
9661013
.maxtype = IFLA_CAN_MAX,
9671014
.policy = can_policy,
9681015
.setup = can_setup,
1016+
.validate = can_validate,
9691017
.newlink = can_newlink,
9701018
.changelink = can_changelink,
9711019
.get_size = can_get_size,

‎drivers/net/can/m_can/m_can.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ static struct net_device *alloc_m_can_dev(void)
955955
priv->can.do_get_berr_counter = m_can_get_berr_counter;
956956

957957
/* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.0.1 */
958-
priv->can.ctrlmode = CAN_CTRLMODE_FD_NON_ISO;
958+
can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
959959

960960
/* CAN_CTRLMODE_FD_NON_ISO can not be changed with M_CAN IP v3.0.1 */
961961
priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |

‎include/linux/can/dev.h‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ struct can_priv {
4040
struct can_clock clock;
4141

4242
enum can_state state;
43-
u32 ctrlmode;
44-
u32 ctrlmode_supported;
43+
44+
/* CAN controller features - see include/uapi/linux/can/netlink.h */
45+
u32 ctrlmode; /* current options setting */
46+
u32 ctrlmode_supported; /* options that can be modified by netlink */
47+
u32 ctrlmode_static; /* static enabled options for driver/hardware */
4548

4649
int restart_ms;
4750
struct timer_list restart_timer;
@@ -108,6 +111,21 @@ static inline bool can_is_canfd_skb(const struct sk_buff *skb)
108111
return skb->len == CANFD_MTU;
109112
}
110113

114+
/* helper to define static CAN controller features at device creation time */
115+
static inline void can_set_static_ctrlmode(struct net_device *dev,
116+
u32 static_mode)
117+
{
118+
struct can_priv *priv = netdev_priv(dev);
119+
120+
/* alloc_candev() succeeded => netdev_priv() is valid at this point */
121+
priv->ctrlmode = static_mode;
122+
priv->ctrlmode_static = static_mode;
123+
124+
/* override MTU which was set by default in can_setup()? */
125+
if (static_mode & CAN_CTRLMODE_FD)
126+
dev->mtu = CANFD_MTU;
127+
}
128+
111129
/* get data length from can_dlc with sanitized can_dlc */
112130
u8 can_dlc2len(u8 can_dlc);
113131

0 commit comments

Comments
 (0)