Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
0003  * Copyright (C) 2006 Andrey Volkov, Varma Electronics
0004  * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
0005  * Copyright (C) 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
0006  */
0007 
0008 #include <linux/can/dev.h>
0009 #include <net/rtnetlink.h>
0010 
0011 static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
0012     [IFLA_CAN_STATE] = { .type = NLA_U32 },
0013     [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
0014     [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
0015     [IFLA_CAN_RESTART] = { .type = NLA_U32 },
0016     [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
0017     [IFLA_CAN_BITTIMING_CONST] = { .len = sizeof(struct can_bittiming_const) },
0018     [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
0019     [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
0020     [IFLA_CAN_DATA_BITTIMING] = { .len = sizeof(struct can_bittiming) },
0021     [IFLA_CAN_DATA_BITTIMING_CONST] = { .len = sizeof(struct can_bittiming_const) },
0022     [IFLA_CAN_TERMINATION] = { .type = NLA_U16 },
0023     [IFLA_CAN_TDC] = { .type = NLA_NESTED },
0024     [IFLA_CAN_CTRLMODE_EXT] = { .type = NLA_NESTED },
0025 };
0026 
0027 static const struct nla_policy can_tdc_policy[IFLA_CAN_TDC_MAX + 1] = {
0028     [IFLA_CAN_TDC_TDCV_MIN] = { .type = NLA_U32 },
0029     [IFLA_CAN_TDC_TDCV_MAX] = { .type = NLA_U32 },
0030     [IFLA_CAN_TDC_TDCO_MIN] = { .type = NLA_U32 },
0031     [IFLA_CAN_TDC_TDCO_MAX] = { .type = NLA_U32 },
0032     [IFLA_CAN_TDC_TDCF_MIN] = { .type = NLA_U32 },
0033     [IFLA_CAN_TDC_TDCF_MAX] = { .type = NLA_U32 },
0034     [IFLA_CAN_TDC_TDCV] = { .type = NLA_U32 },
0035     [IFLA_CAN_TDC_TDCO] = { .type = NLA_U32 },
0036     [IFLA_CAN_TDC_TDCF] = { .type = NLA_U32 },
0037 };
0038 
0039 static int can_validate(struct nlattr *tb[], struct nlattr *data[],
0040             struct netlink_ext_ack *extack)
0041 {
0042     bool is_can_fd = false;
0043 
0044     /* Make sure that valid CAN FD configurations always consist of
0045      * - nominal/arbitration bittiming
0046      * - data bittiming
0047      * - control mode with CAN_CTRLMODE_FD set
0048      * - TDC parameters are coherent (details below)
0049      */
0050 
0051     if (!data)
0052         return 0;
0053 
0054     if (data[IFLA_CAN_CTRLMODE]) {
0055         struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
0056         u32 tdc_flags = cm->flags & CAN_CTRLMODE_TDC_MASK;
0057 
0058         is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
0059 
0060         /* CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually exclusive */
0061         if (tdc_flags == CAN_CTRLMODE_TDC_MASK)
0062             return -EOPNOTSUPP;
0063         /* If one of the CAN_CTRLMODE_TDC_* flag is set then
0064          * TDC must be set and vice-versa
0065          */
0066         if (!!tdc_flags != !!data[IFLA_CAN_TDC])
0067             return -EOPNOTSUPP;
0068         /* If providing TDC parameters, at least TDCO is
0069          * needed. TDCV is needed if and only if
0070          * CAN_CTRLMODE_TDC_MANUAL is set
0071          */
0072         if (data[IFLA_CAN_TDC]) {
0073             struct nlattr *tb_tdc[IFLA_CAN_TDC_MAX + 1];
0074             int err;
0075 
0076             err = nla_parse_nested(tb_tdc, IFLA_CAN_TDC_MAX,
0077                            data[IFLA_CAN_TDC],
0078                            can_tdc_policy, extack);
0079             if (err)
0080                 return err;
0081 
0082             if (tb_tdc[IFLA_CAN_TDC_TDCV]) {
0083                 if (tdc_flags & CAN_CTRLMODE_TDC_AUTO)
0084                     return -EOPNOTSUPP;
0085             } else {
0086                 if (tdc_flags & CAN_CTRLMODE_TDC_MANUAL)
0087                     return -EOPNOTSUPP;
0088             }
0089 
0090             if (!tb_tdc[IFLA_CAN_TDC_TDCO])
0091                 return -EOPNOTSUPP;
0092         }
0093     }
0094 
0095     if (is_can_fd) {
0096         if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
0097             return -EOPNOTSUPP;
0098     }
0099 
0100     if (data[IFLA_CAN_DATA_BITTIMING] || data[IFLA_CAN_TDC]) {
0101         if (!is_can_fd)
0102             return -EOPNOTSUPP;
0103     }
0104 
0105     return 0;
0106 }
0107 
0108 static int can_tdc_changelink(struct can_priv *priv, const struct nlattr *nla,
0109                   struct netlink_ext_ack *extack)
0110 {
0111     struct nlattr *tb_tdc[IFLA_CAN_TDC_MAX + 1];
0112     struct can_tdc tdc = { 0 };
0113     const struct can_tdc_const *tdc_const = priv->tdc_const;
0114     int err;
0115 
0116     if (!tdc_const || !can_tdc_is_enabled(priv))
0117         return -EOPNOTSUPP;
0118 
0119     err = nla_parse_nested(tb_tdc, IFLA_CAN_TDC_MAX, nla,
0120                    can_tdc_policy, extack);
0121     if (err)
0122         return err;
0123 
0124     if (tb_tdc[IFLA_CAN_TDC_TDCV]) {
0125         u32 tdcv = nla_get_u32(tb_tdc[IFLA_CAN_TDC_TDCV]);
0126 
0127         if (tdcv < tdc_const->tdcv_min || tdcv > tdc_const->tdcv_max)
0128             return -EINVAL;
0129 
0130         tdc.tdcv = tdcv;
0131     }
0132 
0133     if (tb_tdc[IFLA_CAN_TDC_TDCO]) {
0134         u32 tdco = nla_get_u32(tb_tdc[IFLA_CAN_TDC_TDCO]);
0135 
0136         if (tdco < tdc_const->tdco_min || tdco > tdc_const->tdco_max)
0137             return -EINVAL;
0138 
0139         tdc.tdco = tdco;
0140     }
0141 
0142     if (tb_tdc[IFLA_CAN_TDC_TDCF]) {
0143         u32 tdcf = nla_get_u32(tb_tdc[IFLA_CAN_TDC_TDCF]);
0144 
0145         if (tdcf < tdc_const->tdcf_min || tdcf > tdc_const->tdcf_max)
0146             return -EINVAL;
0147 
0148         tdc.tdcf = tdcf;
0149     }
0150 
0151     priv->tdc = tdc;
0152 
0153     return 0;
0154 }
0155 
0156 static int can_changelink(struct net_device *dev, struct nlattr *tb[],
0157               struct nlattr *data[],
0158               struct netlink_ext_ack *extack)
0159 {
0160     struct can_priv *priv = netdev_priv(dev);
0161     u32 tdc_mask = 0;
0162     int err;
0163 
0164     /* We need synchronization with dev->stop() */
0165     ASSERT_RTNL();
0166 
0167     if (data[IFLA_CAN_BITTIMING]) {
0168         struct can_bittiming bt;
0169 
0170         /* Do not allow changing bittiming while running */
0171         if (dev->flags & IFF_UP)
0172             return -EBUSY;
0173 
0174         /* Calculate bittiming parameters based on
0175          * bittiming_const if set, otherwise pass bitrate
0176          * directly via do_set_bitrate(). Bail out if neither
0177          * is given.
0178          */
0179         if (!priv->bittiming_const && !priv->do_set_bittiming &&
0180             !priv->bitrate_const)
0181             return -EOPNOTSUPP;
0182 
0183         memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
0184         err = can_get_bittiming(dev, &bt,
0185                     priv->bittiming_const,
0186                     priv->bitrate_const,
0187                     priv->bitrate_const_cnt);
0188         if (err)
0189             return err;
0190 
0191         if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) {
0192             netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n",
0193                    priv->bitrate_max);
0194             return -EINVAL;
0195         }
0196 
0197         memcpy(&priv->bittiming, &bt, sizeof(bt));
0198 
0199         if (priv->do_set_bittiming) {
0200             /* Finally, set the bit-timing registers */
0201             err = priv->do_set_bittiming(dev);
0202             if (err)
0203                 return err;
0204         }
0205     }
0206 
0207     if (data[IFLA_CAN_CTRLMODE]) {
0208         struct can_ctrlmode *cm;
0209         u32 ctrlstatic;
0210         u32 maskedflags;
0211 
0212         /* Do not allow changing controller mode while running */
0213         if (dev->flags & IFF_UP)
0214             return -EBUSY;
0215         cm = nla_data(data[IFLA_CAN_CTRLMODE]);
0216         ctrlstatic = can_get_static_ctrlmode(priv);
0217         maskedflags = cm->flags & cm->mask;
0218 
0219         /* check whether provided bits are allowed to be passed */
0220         if (maskedflags & ~(priv->ctrlmode_supported | ctrlstatic))
0221             return -EOPNOTSUPP;
0222 
0223         /* do not check for static fd-non-iso if 'fd' is disabled */
0224         if (!(maskedflags & CAN_CTRLMODE_FD))
0225             ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
0226 
0227         /* make sure static options are provided by configuration */
0228         if ((maskedflags & ctrlstatic) != ctrlstatic)
0229             return -EOPNOTSUPP;
0230 
0231         /* clear bits to be modified and copy the flag values */
0232         priv->ctrlmode &= ~cm->mask;
0233         priv->ctrlmode |= maskedflags;
0234 
0235         /* CAN_CTRLMODE_FD can only be set when driver supports FD */
0236         if (priv->ctrlmode & CAN_CTRLMODE_FD) {
0237             dev->mtu = CANFD_MTU;
0238         } else {
0239             dev->mtu = CAN_MTU;
0240             memset(&priv->data_bittiming, 0,
0241                    sizeof(priv->data_bittiming));
0242             priv->ctrlmode &= ~CAN_CTRLMODE_TDC_MASK;
0243             memset(&priv->tdc, 0, sizeof(priv->tdc));
0244         }
0245 
0246         tdc_mask = cm->mask & CAN_CTRLMODE_TDC_MASK;
0247         /* CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually
0248          * exclusive: make sure to turn the other one off
0249          */
0250         if (tdc_mask)
0251             priv->ctrlmode &= cm->flags | ~CAN_CTRLMODE_TDC_MASK;
0252     }
0253 
0254     if (data[IFLA_CAN_RESTART_MS]) {
0255         /* Do not allow changing restart delay while running */
0256         if (dev->flags & IFF_UP)
0257             return -EBUSY;
0258         priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
0259     }
0260 
0261     if (data[IFLA_CAN_RESTART]) {
0262         /* Do not allow a restart while not running */
0263         if (!(dev->flags & IFF_UP))
0264             return -EINVAL;
0265         err = can_restart_now(dev);
0266         if (err)
0267             return err;
0268     }
0269 
0270     if (data[IFLA_CAN_DATA_BITTIMING]) {
0271         struct can_bittiming dbt;
0272 
0273         /* Do not allow changing bittiming while running */
0274         if (dev->flags & IFF_UP)
0275             return -EBUSY;
0276 
0277         /* Calculate bittiming parameters based on
0278          * data_bittiming_const if set, otherwise pass bitrate
0279          * directly via do_set_bitrate(). Bail out if neither
0280          * is given.
0281          */
0282         if (!priv->data_bittiming_const && !priv->do_set_data_bittiming &&
0283             !priv->data_bitrate_const)
0284             return -EOPNOTSUPP;
0285 
0286         memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
0287                sizeof(dbt));
0288         err = can_get_bittiming(dev, &dbt,
0289                     priv->data_bittiming_const,
0290                     priv->data_bitrate_const,
0291                     priv->data_bitrate_const_cnt);
0292         if (err)
0293             return err;
0294 
0295         if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) {
0296             netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n",
0297                    priv->bitrate_max);
0298             return -EINVAL;
0299         }
0300 
0301         memset(&priv->tdc, 0, sizeof(priv->tdc));
0302         if (data[IFLA_CAN_TDC]) {
0303             /* TDC parameters are provided: use them */
0304             err = can_tdc_changelink(priv, data[IFLA_CAN_TDC],
0305                          extack);
0306             if (err) {
0307                 priv->ctrlmode &= ~CAN_CTRLMODE_TDC_MASK;
0308                 return err;
0309             }
0310         } else if (!tdc_mask) {
0311             /* Neither of TDC parameters nor TDC flags are
0312              * provided: do calculation
0313              */
0314             can_calc_tdco(&priv->tdc, priv->tdc_const, &priv->data_bittiming,
0315                       &priv->ctrlmode, priv->ctrlmode_supported);
0316         } /* else: both CAN_CTRLMODE_TDC_{AUTO,MANUAL} are explicitly
0317            * turned off. TDC is disabled: do nothing
0318            */
0319 
0320         memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
0321 
0322         if (priv->do_set_data_bittiming) {
0323             /* Finally, set the bit-timing registers */
0324             err = priv->do_set_data_bittiming(dev);
0325             if (err)
0326                 return err;
0327         }
0328     }
0329 
0330     if (data[IFLA_CAN_TERMINATION]) {
0331         const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
0332         const unsigned int num_term = priv->termination_const_cnt;
0333         unsigned int i;
0334 
0335         if (!priv->do_set_termination)
0336             return -EOPNOTSUPP;
0337 
0338         /* check whether given value is supported by the interface */
0339         for (i = 0; i < num_term; i++) {
0340             if (termval == priv->termination_const[i])
0341                 break;
0342         }
0343         if (i >= num_term)
0344             return -EINVAL;
0345 
0346         /* Finally, set the termination value */
0347         err = priv->do_set_termination(dev, termval);
0348         if (err)
0349             return err;
0350 
0351         priv->termination = termval;
0352     }
0353 
0354     return 0;
0355 }
0356 
0357 static size_t can_tdc_get_size(const struct net_device *dev)
0358 {
0359     struct can_priv *priv = netdev_priv(dev);
0360     size_t size;
0361 
0362     if (!priv->tdc_const)
0363         return 0;
0364 
0365     size = nla_total_size(0);           /* nest IFLA_CAN_TDC */
0366     if (priv->ctrlmode_supported & CAN_CTRLMODE_TDC_MANUAL) {
0367         size += nla_total_size(sizeof(u32));    /* IFLA_CAN_TDCV_MIN */
0368         size += nla_total_size(sizeof(u32));    /* IFLA_CAN_TDCV_MAX */
0369     }
0370     size += nla_total_size(sizeof(u32));        /* IFLA_CAN_TDCO_MIN */
0371     size += nla_total_size(sizeof(u32));        /* IFLA_CAN_TDCO_MAX */
0372     if (priv->tdc_const->tdcf_max) {
0373         size += nla_total_size(sizeof(u32));    /* IFLA_CAN_TDCF_MIN */
0374         size += nla_total_size(sizeof(u32));    /* IFLA_CAN_TDCF_MAX */
0375     }
0376 
0377     if (can_tdc_is_enabled(priv)) {
0378         if (priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL ||
0379             priv->do_get_auto_tdcv)
0380             size += nla_total_size(sizeof(u32));    /* IFLA_CAN_TDCV */
0381         size += nla_total_size(sizeof(u32));        /* IFLA_CAN_TDCO */
0382         if (priv->tdc_const->tdcf_max)
0383             size += nla_total_size(sizeof(u32));    /* IFLA_CAN_TDCF */
0384     }
0385 
0386     return size;
0387 }
0388 
0389 static size_t can_ctrlmode_ext_get_size(void)
0390 {
0391     return nla_total_size(0) +      /* nest IFLA_CAN_CTRLMODE_EXT */
0392         nla_total_size(sizeof(u32));    /* IFLA_CAN_CTRLMODE_SUPPORTED */
0393 }
0394 
0395 static size_t can_get_size(const struct net_device *dev)
0396 {
0397     struct can_priv *priv = netdev_priv(dev);
0398     size_t size = 0;
0399 
0400     if (priv->bittiming.bitrate)                /* IFLA_CAN_BITTIMING */
0401         size += nla_total_size(sizeof(struct can_bittiming));
0402     if (priv->bittiming_const)              /* IFLA_CAN_BITTIMING_CONST */
0403         size += nla_total_size(sizeof(struct can_bittiming_const));
0404     size += nla_total_size(sizeof(struct can_clock));   /* IFLA_CAN_CLOCK */
0405     size += nla_total_size(sizeof(u32));            /* IFLA_CAN_STATE */
0406     size += nla_total_size(sizeof(struct can_ctrlmode));    /* IFLA_CAN_CTRLMODE */
0407     size += nla_total_size(sizeof(u32));            /* IFLA_CAN_RESTART_MS */
0408     if (priv->do_get_berr_counter)              /* IFLA_CAN_BERR_COUNTER */
0409         size += nla_total_size(sizeof(struct can_berr_counter));
0410     if (priv->data_bittiming.bitrate)           /* IFLA_CAN_DATA_BITTIMING */
0411         size += nla_total_size(sizeof(struct can_bittiming));
0412     if (priv->data_bittiming_const)             /* IFLA_CAN_DATA_BITTIMING_CONST */
0413         size += nla_total_size(sizeof(struct can_bittiming_const));
0414     if (priv->termination_const) {
0415         size += nla_total_size(sizeof(priv->termination));      /* IFLA_CAN_TERMINATION */
0416         size += nla_total_size(sizeof(*priv->termination_const) *   /* IFLA_CAN_TERMINATION_CONST */
0417                        priv->termination_const_cnt);
0418     }
0419     if (priv->bitrate_const)                /* IFLA_CAN_BITRATE_CONST */
0420         size += nla_total_size(sizeof(*priv->bitrate_const) *
0421                        priv->bitrate_const_cnt);
0422     if (priv->data_bitrate_const)               /* IFLA_CAN_DATA_BITRATE_CONST */
0423         size += nla_total_size(sizeof(*priv->data_bitrate_const) *
0424                        priv->data_bitrate_const_cnt);
0425     size += sizeof(priv->bitrate_max);          /* IFLA_CAN_BITRATE_MAX */
0426     size += can_tdc_get_size(dev);              /* IFLA_CAN_TDC */
0427     size += can_ctrlmode_ext_get_size();            /* IFLA_CAN_CTRLMODE_EXT */
0428 
0429     return size;
0430 }
0431 
0432 static int can_tdc_fill_info(struct sk_buff *skb, const struct net_device *dev)
0433 {
0434     struct nlattr *nest;
0435     struct can_priv *priv = netdev_priv(dev);
0436     struct can_tdc *tdc = &priv->tdc;
0437     const struct can_tdc_const *tdc_const = priv->tdc_const;
0438 
0439     if (!tdc_const)
0440         return 0;
0441 
0442     nest = nla_nest_start(skb, IFLA_CAN_TDC);
0443     if (!nest)
0444         return -EMSGSIZE;
0445 
0446     if (priv->ctrlmode_supported & CAN_CTRLMODE_TDC_MANUAL &&
0447         (nla_put_u32(skb, IFLA_CAN_TDC_TDCV_MIN, tdc_const->tdcv_min) ||
0448          nla_put_u32(skb, IFLA_CAN_TDC_TDCV_MAX, tdc_const->tdcv_max)))
0449         goto err_cancel;
0450     if (nla_put_u32(skb, IFLA_CAN_TDC_TDCO_MIN, tdc_const->tdco_min) ||
0451         nla_put_u32(skb, IFLA_CAN_TDC_TDCO_MAX, tdc_const->tdco_max))
0452         goto err_cancel;
0453     if (tdc_const->tdcf_max &&
0454         (nla_put_u32(skb, IFLA_CAN_TDC_TDCF_MIN, tdc_const->tdcf_min) ||
0455          nla_put_u32(skb, IFLA_CAN_TDC_TDCF_MAX, tdc_const->tdcf_max)))
0456         goto err_cancel;
0457 
0458     if (can_tdc_is_enabled(priv)) {
0459         u32 tdcv;
0460         int err = -EINVAL;
0461 
0462         if (priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL) {
0463             tdcv = tdc->tdcv;
0464             err = 0;
0465         } else if (priv->do_get_auto_tdcv) {
0466             err = priv->do_get_auto_tdcv(dev, &tdcv);
0467         }
0468         if (!err && nla_put_u32(skb, IFLA_CAN_TDC_TDCV, tdcv))
0469             goto err_cancel;
0470         if (nla_put_u32(skb, IFLA_CAN_TDC_TDCO, tdc->tdco))
0471             goto err_cancel;
0472         if (tdc_const->tdcf_max &&
0473             nla_put_u32(skb, IFLA_CAN_TDC_TDCF, tdc->tdcf))
0474             goto err_cancel;
0475     }
0476 
0477     nla_nest_end(skb, nest);
0478     return 0;
0479 
0480 err_cancel:
0481     nla_nest_cancel(skb, nest);
0482     return -EMSGSIZE;
0483 }
0484 
0485 static int can_ctrlmode_ext_fill_info(struct sk_buff *skb,
0486                       const struct can_priv *priv)
0487 {
0488     struct nlattr *nest;
0489 
0490     nest = nla_nest_start(skb, IFLA_CAN_CTRLMODE_EXT);
0491     if (!nest)
0492         return -EMSGSIZE;
0493 
0494     if (nla_put_u32(skb, IFLA_CAN_CTRLMODE_SUPPORTED,
0495             priv->ctrlmode_supported)) {
0496         nla_nest_cancel(skb, nest);
0497         return -EMSGSIZE;
0498     }
0499 
0500     nla_nest_end(skb, nest);
0501     return 0;
0502 }
0503 
0504 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
0505 {
0506     struct can_priv *priv = netdev_priv(dev);
0507     struct can_ctrlmode cm = {.flags = priv->ctrlmode};
0508     struct can_berr_counter bec = { };
0509     enum can_state state = priv->state;
0510 
0511     if (priv->do_get_state)
0512         priv->do_get_state(dev, &state);
0513 
0514     if ((priv->bittiming.bitrate != CAN_BITRATE_UNSET &&
0515          priv->bittiming.bitrate != CAN_BITRATE_UNKNOWN &&
0516          nla_put(skb, IFLA_CAN_BITTIMING,
0517              sizeof(priv->bittiming), &priv->bittiming)) ||
0518 
0519         (priv->bittiming_const &&
0520          nla_put(skb, IFLA_CAN_BITTIMING_CONST,
0521              sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
0522 
0523         nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
0524         nla_put_u32(skb, IFLA_CAN_STATE, state) ||
0525         nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
0526         nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
0527 
0528         (priv->do_get_berr_counter &&
0529          !priv->do_get_berr_counter(dev, &bec) &&
0530          nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
0531 
0532         (priv->data_bittiming.bitrate &&
0533          nla_put(skb, IFLA_CAN_DATA_BITTIMING,
0534              sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
0535 
0536         (priv->data_bittiming_const &&
0537          nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
0538              sizeof(*priv->data_bittiming_const),
0539              priv->data_bittiming_const)) ||
0540 
0541         (priv->termination_const &&
0542          (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
0543           nla_put(skb, IFLA_CAN_TERMINATION_CONST,
0544               sizeof(*priv->termination_const) *
0545               priv->termination_const_cnt,
0546               priv->termination_const))) ||
0547 
0548         (priv->bitrate_const &&
0549          nla_put(skb, IFLA_CAN_BITRATE_CONST,
0550              sizeof(*priv->bitrate_const) *
0551              priv->bitrate_const_cnt,
0552              priv->bitrate_const)) ||
0553 
0554         (priv->data_bitrate_const &&
0555          nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
0556              sizeof(*priv->data_bitrate_const) *
0557              priv->data_bitrate_const_cnt,
0558              priv->data_bitrate_const)) ||
0559 
0560         (nla_put(skb, IFLA_CAN_BITRATE_MAX,
0561              sizeof(priv->bitrate_max),
0562              &priv->bitrate_max)) ||
0563 
0564         can_tdc_fill_info(skb, dev) ||
0565 
0566         can_ctrlmode_ext_fill_info(skb, priv)
0567         )
0568 
0569         return -EMSGSIZE;
0570 
0571     return 0;
0572 }
0573 
0574 static size_t can_get_xstats_size(const struct net_device *dev)
0575 {
0576     return sizeof(struct can_device_stats);
0577 }
0578 
0579 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
0580 {
0581     struct can_priv *priv = netdev_priv(dev);
0582 
0583     if (nla_put(skb, IFLA_INFO_XSTATS,
0584             sizeof(priv->can_stats), &priv->can_stats))
0585         goto nla_put_failure;
0586     return 0;
0587 
0588 nla_put_failure:
0589     return -EMSGSIZE;
0590 }
0591 
0592 static int can_newlink(struct net *src_net, struct net_device *dev,
0593                struct nlattr *tb[], struct nlattr *data[],
0594                struct netlink_ext_ack *extack)
0595 {
0596     return -EOPNOTSUPP;
0597 }
0598 
0599 static void can_dellink(struct net_device *dev, struct list_head *head)
0600 {
0601 }
0602 
0603 struct rtnl_link_ops can_link_ops __read_mostly = {
0604     .kind       = "can",
0605     .netns_refund   = true,
0606     .maxtype    = IFLA_CAN_MAX,
0607     .policy     = can_policy,
0608     .setup      = can_setup,
0609     .validate   = can_validate,
0610     .newlink    = can_newlink,
0611     .changelink = can_changelink,
0612     .dellink    = can_dellink,
0613     .get_size   = can_get_size,
0614     .fill_info  = can_fill_info,
0615     .get_xstats_size = can_get_xstats_size,
0616     .fill_xstats    = can_fill_xstats,
0617 };
0618 
0619 int can_netlink_register(void)
0620 {
0621     return rtnl_link_register(&can_link_ops);
0622 }
0623 
0624 void can_netlink_unregister(void)
0625 {
0626     rtnl_link_unregister(&can_link_ops);
0627 }