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