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