netlink.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
  3. * Copyright (C) 2006 Andrey Volkov, Varma Electronics
  4. * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
  5. * Copyright (C) 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
  6. */
  7. #include <linux/can/dev.h>
  8. #include <net/rtnetlink.h>
  9. static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
  10. [IFLA_CAN_STATE] = { .type = NLA_U32 },
  11. [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
  12. [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
  13. [IFLA_CAN_RESTART] = { .type = NLA_U32 },
  14. [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
  15. [IFLA_CAN_BITTIMING_CONST] = { .len = sizeof(struct can_bittiming_const) },
  16. [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
  17. [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
  18. [IFLA_CAN_DATA_BITTIMING] = { .len = sizeof(struct can_bittiming) },
  19. [IFLA_CAN_DATA_BITTIMING_CONST] = { .len = sizeof(struct can_bittiming_const) },
  20. [IFLA_CAN_TERMINATION] = { .type = NLA_U16 },
  21. [IFLA_CAN_TDC] = { .type = NLA_NESTED },
  22. [IFLA_CAN_CTRLMODE_EXT] = { .type = NLA_NESTED },
  23. };
  24. static const struct nla_policy can_tdc_policy[IFLA_CAN_TDC_MAX + 1] = {
  25. [IFLA_CAN_TDC_TDCV_MIN] = { .type = NLA_U32 },
  26. [IFLA_CAN_TDC_TDCV_MAX] = { .type = NLA_U32 },
  27. [IFLA_CAN_TDC_TDCO_MIN] = { .type = NLA_U32 },
  28. [IFLA_CAN_TDC_TDCO_MAX] = { .type = NLA_U32 },
  29. [IFLA_CAN_TDC_TDCF_MIN] = { .type = NLA_U32 },
  30. [IFLA_CAN_TDC_TDCF_MAX] = { .type = NLA_U32 },
  31. [IFLA_CAN_TDC_TDCV] = { .type = NLA_U32 },
  32. [IFLA_CAN_TDC_TDCO] = { .type = NLA_U32 },
  33. [IFLA_CAN_TDC_TDCF] = { .type = NLA_U32 },
  34. };
  35. static int can_validate_bittiming(const struct can_bittiming *bt,
  36. struct netlink_ext_ack *extack)
  37. {
  38. /* sample point is in one-tenth of a percent */
  39. if (bt->sample_point >= 1000) {
  40. NL_SET_ERR_MSG(extack, "sample point must be between 0 and 100%");
  41. return -EINVAL;
  42. }
  43. return 0;
  44. }
  45. static int can_validate(struct nlattr *tb[], struct nlattr *data[],
  46. struct netlink_ext_ack *extack)
  47. {
  48. bool is_can_fd = false;
  49. int err;
  50. /* Make sure that valid CAN FD configurations always consist of
  51. * - nominal/arbitration bittiming
  52. * - data bittiming
  53. * - control mode with CAN_CTRLMODE_FD set
  54. * - TDC parameters are coherent (details below)
  55. */
  56. if (!data)
  57. return 0;
  58. if (data[IFLA_CAN_CTRLMODE]) {
  59. struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  60. u32 tdc_flags = cm->flags & CAN_CTRLMODE_TDC_MASK;
  61. is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
  62. /* CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually exclusive */
  63. if (tdc_flags == CAN_CTRLMODE_TDC_MASK)
  64. return -EOPNOTSUPP;
  65. /* If one of the CAN_CTRLMODE_TDC_* flag is set then
  66. * TDC must be set and vice-versa
  67. */
  68. if (!!tdc_flags != !!data[IFLA_CAN_TDC])
  69. return -EOPNOTSUPP;
  70. /* If providing TDC parameters, at least TDCO is
  71. * needed. TDCV is needed if and only if
  72. * CAN_CTRLMODE_TDC_MANUAL is set
  73. */
  74. if (data[IFLA_CAN_TDC]) {
  75. struct nlattr *tb_tdc[IFLA_CAN_TDC_MAX + 1];
  76. err = nla_parse_nested(tb_tdc, IFLA_CAN_TDC_MAX,
  77. data[IFLA_CAN_TDC],
  78. can_tdc_policy, extack);
  79. if (err)
  80. return err;
  81. if (tb_tdc[IFLA_CAN_TDC_TDCV]) {
  82. if (tdc_flags & CAN_CTRLMODE_TDC_AUTO)
  83. return -EOPNOTSUPP;
  84. } else {
  85. if (tdc_flags & CAN_CTRLMODE_TDC_MANUAL)
  86. return -EOPNOTSUPP;
  87. }
  88. if (!tb_tdc[IFLA_CAN_TDC_TDCO])
  89. return -EOPNOTSUPP;
  90. }
  91. }
  92. if (data[IFLA_CAN_BITTIMING]) {
  93. struct can_bittiming bt;
  94. memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
  95. err = can_validate_bittiming(&bt, extack);
  96. if (err)
  97. return err;
  98. }
  99. if (is_can_fd) {
  100. if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
  101. return -EOPNOTSUPP;
  102. }
  103. if (data[IFLA_CAN_DATA_BITTIMING] || data[IFLA_CAN_TDC]) {
  104. if (!is_can_fd)
  105. return -EOPNOTSUPP;
  106. }
  107. if (data[IFLA_CAN_DATA_BITTIMING]) {
  108. struct can_bittiming bt;
  109. memcpy(&bt, nla_data(data[IFLA_CAN_DATA_BITTIMING]), sizeof(bt));
  110. err = can_validate_bittiming(&bt, extack);
  111. if (err)
  112. return err;
  113. }
  114. return 0;
  115. }
  116. static int can_tdc_changelink(struct can_priv *priv, const struct nlattr *nla,
  117. struct netlink_ext_ack *extack)
  118. {
  119. struct nlattr *tb_tdc[IFLA_CAN_TDC_MAX + 1];
  120. struct can_tdc tdc = { 0 };
  121. const struct can_tdc_const *tdc_const = priv->tdc_const;
  122. int err;
  123. if (!tdc_const || !can_tdc_is_enabled(priv))
  124. return -EOPNOTSUPP;
  125. err = nla_parse_nested(tb_tdc, IFLA_CAN_TDC_MAX, nla,
  126. can_tdc_policy, extack);
  127. if (err)
  128. return err;
  129. if (tb_tdc[IFLA_CAN_TDC_TDCV]) {
  130. u32 tdcv = nla_get_u32(tb_tdc[IFLA_CAN_TDC_TDCV]);
  131. if (tdcv < tdc_const->tdcv_min || tdcv > tdc_const->tdcv_max)
  132. return -EINVAL;
  133. tdc.tdcv = tdcv;
  134. }
  135. if (tb_tdc[IFLA_CAN_TDC_TDCO]) {
  136. u32 tdco = nla_get_u32(tb_tdc[IFLA_CAN_TDC_TDCO]);
  137. if (tdco < tdc_const->tdco_min || tdco > tdc_const->tdco_max)
  138. return -EINVAL;
  139. tdc.tdco = tdco;
  140. }
  141. if (tb_tdc[IFLA_CAN_TDC_TDCF]) {
  142. u32 tdcf = nla_get_u32(tb_tdc[IFLA_CAN_TDC_TDCF]);
  143. if (tdcf < tdc_const->tdcf_min || tdcf > tdc_const->tdcf_max)
  144. return -EINVAL;
  145. tdc.tdcf = tdcf;
  146. }
  147. priv->tdc = tdc;
  148. return 0;
  149. }
  150. static int can_changelink(struct net_device *dev, struct nlattr *tb[],
  151. struct nlattr *data[],
  152. struct netlink_ext_ack *extack)
  153. {
  154. struct can_priv *priv = netdev_priv(dev);
  155. u32 tdc_mask = 0;
  156. int err;
  157. /* We need synchronization with dev->stop() */
  158. ASSERT_RTNL();
  159. if (data[IFLA_CAN_CTRLMODE]) {
  160. struct can_ctrlmode *cm;
  161. u32 ctrlstatic;
  162. u32 maskedflags;
  163. /* Do not allow changing controller mode while running */
  164. if (dev->flags & IFF_UP)
  165. return -EBUSY;
  166. cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  167. ctrlstatic = can_get_static_ctrlmode(priv);
  168. maskedflags = cm->flags & cm->mask;
  169. /* check whether provided bits are allowed to be passed */
  170. if (maskedflags & ~(priv->ctrlmode_supported | ctrlstatic))
  171. return -EOPNOTSUPP;
  172. /* do not check for static fd-non-iso if 'fd' is disabled */
  173. if (!(maskedflags & CAN_CTRLMODE_FD))
  174. ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
  175. /* make sure static options are provided by configuration */
  176. if ((maskedflags & ctrlstatic) != ctrlstatic)
  177. return -EOPNOTSUPP;
  178. /* clear bits to be modified and copy the flag values */
  179. priv->ctrlmode &= ~cm->mask;
  180. priv->ctrlmode |= maskedflags;
  181. /* CAN_CTRLMODE_FD can only be set when driver supports FD */
  182. if (priv->ctrlmode & CAN_CTRLMODE_FD) {
  183. dev->mtu = CANFD_MTU;
  184. } else {
  185. dev->mtu = CAN_MTU;
  186. memset(&priv->data_bittiming, 0,
  187. sizeof(priv->data_bittiming));
  188. priv->ctrlmode &= ~CAN_CTRLMODE_TDC_MASK;
  189. memset(&priv->tdc, 0, sizeof(priv->tdc));
  190. }
  191. tdc_mask = cm->mask & CAN_CTRLMODE_TDC_MASK;
  192. /* CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually
  193. * exclusive: make sure to turn the other one off
  194. */
  195. if (tdc_mask)
  196. priv->ctrlmode &= cm->flags | ~CAN_CTRLMODE_TDC_MASK;
  197. }
  198. if (data[IFLA_CAN_BITTIMING]) {
  199. struct can_bittiming bt;
  200. /* Do not allow changing bittiming while running */
  201. if (dev->flags & IFF_UP)
  202. return -EBUSY;
  203. /* Calculate bittiming parameters based on
  204. * bittiming_const if set, otherwise pass bitrate
  205. * directly via do_set_bitrate(). Bail out if neither
  206. * is given.
  207. */
  208. if (!priv->bittiming_const && !priv->do_set_bittiming &&
  209. !priv->bitrate_const)
  210. return -EOPNOTSUPP;
  211. memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
  212. err = can_get_bittiming(dev, &bt,
  213. priv->bittiming_const,
  214. priv->bitrate_const,
  215. priv->bitrate_const_cnt,
  216. extack);
  217. if (err)
  218. return err;
  219. if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) {
  220. NL_SET_ERR_MSG_FMT(extack,
  221. "arbitration bitrate %u bps surpasses transceiver capabilities of %u bps",
  222. bt.bitrate, priv->bitrate_max);
  223. return -EINVAL;
  224. }
  225. memcpy(&priv->bittiming, &bt, sizeof(bt));
  226. if (priv->do_set_bittiming) {
  227. /* Finally, set the bit-timing registers */
  228. err = priv->do_set_bittiming(dev);
  229. if (err)
  230. return err;
  231. }
  232. }
  233. if (data[IFLA_CAN_RESTART_MS]) {
  234. unsigned int restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
  235. if (restart_ms != 0 && !priv->do_set_mode) {
  236. NL_SET_ERR_MSG(extack,
  237. "Device doesn't support restart from Bus Off");
  238. return -EOPNOTSUPP;
  239. }
  240. /* Do not allow changing restart delay while running */
  241. if (dev->flags & IFF_UP)
  242. return -EBUSY;
  243. priv->restart_ms = restart_ms;
  244. }
  245. if (data[IFLA_CAN_RESTART]) {
  246. if (!priv->do_set_mode) {
  247. NL_SET_ERR_MSG(extack,
  248. "Device doesn't support restart from Bus Off");
  249. return -EOPNOTSUPP;
  250. }
  251. /* Do not allow a restart while not running */
  252. if (!(dev->flags & IFF_UP))
  253. return -EINVAL;
  254. err = can_restart_now(dev);
  255. if (err)
  256. return err;
  257. }
  258. if (data[IFLA_CAN_DATA_BITTIMING]) {
  259. struct can_bittiming dbt;
  260. /* Do not allow changing bittiming while running */
  261. if (dev->flags & IFF_UP)
  262. return -EBUSY;
  263. /* Calculate bittiming parameters based on
  264. * data_bittiming_const if set, otherwise pass bitrate
  265. * directly via do_set_bitrate(). Bail out if neither
  266. * is given.
  267. */
  268. if (!priv->data_bittiming_const && !priv->do_set_data_bittiming &&
  269. !priv->data_bitrate_const)
  270. return -EOPNOTSUPP;
  271. memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
  272. sizeof(dbt));
  273. err = can_get_bittiming(dev, &dbt,
  274. priv->data_bittiming_const,
  275. priv->data_bitrate_const,
  276. priv->data_bitrate_const_cnt,
  277. extack);
  278. if (err)
  279. return err;
  280. if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) {
  281. NL_SET_ERR_MSG_FMT(extack,
  282. "CANFD data bitrate %u bps surpasses transceiver capabilities of %u bps",
  283. dbt.bitrate, priv->bitrate_max);
  284. return -EINVAL;
  285. }
  286. memset(&priv->tdc, 0, sizeof(priv->tdc));
  287. if (data[IFLA_CAN_TDC]) {
  288. /* TDC parameters are provided: use them */
  289. err = can_tdc_changelink(priv, data[IFLA_CAN_TDC],
  290. extack);
  291. if (err) {
  292. priv->ctrlmode &= ~CAN_CTRLMODE_TDC_MASK;
  293. return err;
  294. }
  295. } else if (!tdc_mask) {
  296. /* Neither of TDC parameters nor TDC flags are
  297. * provided: do calculation
  298. */
  299. can_calc_tdco(&priv->tdc, priv->tdc_const, &dbt,
  300. &priv->ctrlmode, priv->ctrlmode_supported);
  301. } /* else: both CAN_CTRLMODE_TDC_{AUTO,MANUAL} are explicitly
  302. * turned off. TDC is disabled: do nothing
  303. */
  304. memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
  305. if (priv->do_set_data_bittiming) {
  306. /* Finally, set the bit-timing registers */
  307. err = priv->do_set_data_bittiming(dev);
  308. if (err)
  309. return err;
  310. }
  311. }
  312. if (data[IFLA_CAN_TERMINATION]) {
  313. const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
  314. const unsigned int num_term = priv->termination_const_cnt;
  315. unsigned int i;
  316. if (!priv->do_set_termination)
  317. return -EOPNOTSUPP;
  318. /* check whether given value is supported by the interface */
  319. for (i = 0; i < num_term; i++) {
  320. if (termval == priv->termination_const[i])
  321. break;
  322. }
  323. if (i >= num_term)
  324. return -EINVAL;
  325. /* Finally, set the termination value */
  326. err = priv->do_set_termination(dev, termval);
  327. if (err)
  328. return err;
  329. priv->termination = termval;
  330. }
  331. return 0;
  332. }
  333. static size_t can_tdc_get_size(const struct net_device *dev)
  334. {
  335. struct can_priv *priv = netdev_priv(dev);
  336. size_t size;
  337. if (!priv->tdc_const)
  338. return 0;
  339. size = nla_total_size(0); /* nest IFLA_CAN_TDC */
  340. if (priv->ctrlmode_supported & CAN_CTRLMODE_TDC_MANUAL) {
  341. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCV_MIN */
  342. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCV_MAX */
  343. }
  344. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCO_MIN */
  345. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCO_MAX */
  346. if (priv->tdc_const->tdcf_max) {
  347. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCF_MIN */
  348. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCF_MAX */
  349. }
  350. if (can_tdc_is_enabled(priv)) {
  351. if (priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL ||
  352. priv->do_get_auto_tdcv)
  353. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCV */
  354. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCO */
  355. if (priv->tdc_const->tdcf_max)
  356. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCF */
  357. }
  358. return size;
  359. }
  360. static size_t can_ctrlmode_ext_get_size(void)
  361. {
  362. return nla_total_size(0) + /* nest IFLA_CAN_CTRLMODE_EXT */
  363. nla_total_size(sizeof(u32)); /* IFLA_CAN_CTRLMODE_SUPPORTED */
  364. }
  365. static size_t can_get_size(const struct net_device *dev)
  366. {
  367. struct can_priv *priv = netdev_priv(dev);
  368. size_t size = 0;
  369. if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
  370. size += nla_total_size(sizeof(struct can_bittiming));
  371. if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
  372. size += nla_total_size(sizeof(struct can_bittiming_const));
  373. size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
  374. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
  375. size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
  376. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
  377. if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
  378. size += nla_total_size(sizeof(struct can_berr_counter));
  379. if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
  380. size += nla_total_size(sizeof(struct can_bittiming));
  381. if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
  382. size += nla_total_size(sizeof(struct can_bittiming_const));
  383. if (priv->termination_const) {
  384. size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */
  385. size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */
  386. priv->termination_const_cnt);
  387. }
  388. if (priv->bitrate_const) /* IFLA_CAN_BITRATE_CONST */
  389. size += nla_total_size(sizeof(*priv->bitrate_const) *
  390. priv->bitrate_const_cnt);
  391. if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */
  392. size += nla_total_size(sizeof(*priv->data_bitrate_const) *
  393. priv->data_bitrate_const_cnt);
  394. size += sizeof(priv->bitrate_max); /* IFLA_CAN_BITRATE_MAX */
  395. size += can_tdc_get_size(dev); /* IFLA_CAN_TDC */
  396. size += can_ctrlmode_ext_get_size(); /* IFLA_CAN_CTRLMODE_EXT */
  397. return size;
  398. }
  399. static int can_tdc_fill_info(struct sk_buff *skb, const struct net_device *dev)
  400. {
  401. struct nlattr *nest;
  402. struct can_priv *priv = netdev_priv(dev);
  403. struct can_tdc *tdc = &priv->tdc;
  404. const struct can_tdc_const *tdc_const = priv->tdc_const;
  405. if (!tdc_const)
  406. return 0;
  407. nest = nla_nest_start(skb, IFLA_CAN_TDC);
  408. if (!nest)
  409. return -EMSGSIZE;
  410. if (priv->ctrlmode_supported & CAN_CTRLMODE_TDC_MANUAL &&
  411. (nla_put_u32(skb, IFLA_CAN_TDC_TDCV_MIN, tdc_const->tdcv_min) ||
  412. nla_put_u32(skb, IFLA_CAN_TDC_TDCV_MAX, tdc_const->tdcv_max)))
  413. goto err_cancel;
  414. if (nla_put_u32(skb, IFLA_CAN_TDC_TDCO_MIN, tdc_const->tdco_min) ||
  415. nla_put_u32(skb, IFLA_CAN_TDC_TDCO_MAX, tdc_const->tdco_max))
  416. goto err_cancel;
  417. if (tdc_const->tdcf_max &&
  418. (nla_put_u32(skb, IFLA_CAN_TDC_TDCF_MIN, tdc_const->tdcf_min) ||
  419. nla_put_u32(skb, IFLA_CAN_TDC_TDCF_MAX, tdc_const->tdcf_max)))
  420. goto err_cancel;
  421. if (can_tdc_is_enabled(priv)) {
  422. u32 tdcv;
  423. int err = -EINVAL;
  424. if (priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL) {
  425. tdcv = tdc->tdcv;
  426. err = 0;
  427. } else if (priv->do_get_auto_tdcv) {
  428. err = priv->do_get_auto_tdcv(dev, &tdcv);
  429. }
  430. if (!err && nla_put_u32(skb, IFLA_CAN_TDC_TDCV, tdcv))
  431. goto err_cancel;
  432. if (nla_put_u32(skb, IFLA_CAN_TDC_TDCO, tdc->tdco))
  433. goto err_cancel;
  434. if (tdc_const->tdcf_max &&
  435. nla_put_u32(skb, IFLA_CAN_TDC_TDCF, tdc->tdcf))
  436. goto err_cancel;
  437. }
  438. nla_nest_end(skb, nest);
  439. return 0;
  440. err_cancel:
  441. nla_nest_cancel(skb, nest);
  442. return -EMSGSIZE;
  443. }
  444. static int can_ctrlmode_ext_fill_info(struct sk_buff *skb,
  445. const struct can_priv *priv)
  446. {
  447. struct nlattr *nest;
  448. nest = nla_nest_start(skb, IFLA_CAN_CTRLMODE_EXT);
  449. if (!nest)
  450. return -EMSGSIZE;
  451. if (nla_put_u32(skb, IFLA_CAN_CTRLMODE_SUPPORTED,
  452. priv->ctrlmode_supported)) {
  453. nla_nest_cancel(skb, nest);
  454. return -EMSGSIZE;
  455. }
  456. nla_nest_end(skb, nest);
  457. return 0;
  458. }
  459. static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
  460. {
  461. struct can_priv *priv = netdev_priv(dev);
  462. struct can_ctrlmode cm = {.flags = priv->ctrlmode};
  463. struct can_berr_counter bec = { };
  464. enum can_state state = priv->state;
  465. if (priv->do_get_state)
  466. priv->do_get_state(dev, &state);
  467. if ((priv->bittiming.bitrate != CAN_BITRATE_UNSET &&
  468. priv->bittiming.bitrate != CAN_BITRATE_UNKNOWN &&
  469. nla_put(skb, IFLA_CAN_BITTIMING,
  470. sizeof(priv->bittiming), &priv->bittiming)) ||
  471. (priv->bittiming_const &&
  472. nla_put(skb, IFLA_CAN_BITTIMING_CONST,
  473. sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
  474. nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
  475. nla_put_u32(skb, IFLA_CAN_STATE, state) ||
  476. nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
  477. nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
  478. (priv->do_get_berr_counter &&
  479. !priv->do_get_berr_counter(dev, &bec) &&
  480. nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
  481. (priv->data_bittiming.bitrate &&
  482. nla_put(skb, IFLA_CAN_DATA_BITTIMING,
  483. sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
  484. (priv->data_bittiming_const &&
  485. nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
  486. sizeof(*priv->data_bittiming_const),
  487. priv->data_bittiming_const)) ||
  488. (priv->termination_const &&
  489. (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
  490. nla_put(skb, IFLA_CAN_TERMINATION_CONST,
  491. sizeof(*priv->termination_const) *
  492. priv->termination_const_cnt,
  493. priv->termination_const))) ||
  494. (priv->bitrate_const &&
  495. nla_put(skb, IFLA_CAN_BITRATE_CONST,
  496. sizeof(*priv->bitrate_const) *
  497. priv->bitrate_const_cnt,
  498. priv->bitrate_const)) ||
  499. (priv->data_bitrate_const &&
  500. nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
  501. sizeof(*priv->data_bitrate_const) *
  502. priv->data_bitrate_const_cnt,
  503. priv->data_bitrate_const)) ||
  504. (nla_put(skb, IFLA_CAN_BITRATE_MAX,
  505. sizeof(priv->bitrate_max),
  506. &priv->bitrate_max)) ||
  507. can_tdc_fill_info(skb, dev) ||
  508. can_ctrlmode_ext_fill_info(skb, priv)
  509. )
  510. return -EMSGSIZE;
  511. return 0;
  512. }
  513. static size_t can_get_xstats_size(const struct net_device *dev)
  514. {
  515. return sizeof(struct can_device_stats);
  516. }
  517. static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
  518. {
  519. struct can_priv *priv = netdev_priv(dev);
  520. if (nla_put(skb, IFLA_INFO_XSTATS,
  521. sizeof(priv->can_stats), &priv->can_stats))
  522. goto nla_put_failure;
  523. return 0;
  524. nla_put_failure:
  525. return -EMSGSIZE;
  526. }
  527. static int can_newlink(struct net *src_net, struct net_device *dev,
  528. struct nlattr *tb[], struct nlattr *data[],
  529. struct netlink_ext_ack *extack)
  530. {
  531. return -EOPNOTSUPP;
  532. }
  533. static void can_dellink(struct net_device *dev, struct list_head *head)
  534. {
  535. }
  536. struct rtnl_link_ops can_link_ops __read_mostly = {
  537. .kind = "can",
  538. .netns_refund = true,
  539. .maxtype = IFLA_CAN_MAX,
  540. .policy = can_policy,
  541. .setup = can_setup,
  542. .validate = can_validate,
  543. .newlink = can_newlink,
  544. .changelink = can_changelink,
  545. .dellink = can_dellink,
  546. .get_size = can_get_size,
  547. .fill_info = can_fill_info,
  548. .get_xstats_size = can_get_xstats_size,
  549. .fill_xstats = can_fill_xstats,
  550. };
  551. int can_netlink_register(void)
  552. {
  553. return rtnl_link_register(&can_link_ops);
  554. }
  555. void can_netlink_unregister(void)
  556. {
  557. rtnl_link_unregister(&can_link_ops);
  558. }