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