• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
can_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)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 
can_tdc_changelink(struct can_priv * priv,const struct nlattr * nla,struct netlink_ext_ack * extack)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 
can_changelink(struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)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 		    !priv->bitrate_const)
181 			return -EOPNOTSUPP;
182 
183 		memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
184 		err = can_get_bittiming(dev, &bt,
185 					priv->bittiming_const,
186 					priv->bitrate_const,
187 					priv->bitrate_const_cnt);
188 		if (err)
189 			return err;
190 
191 		if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) {
192 			netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n",
193 				   priv->bitrate_max);
194 			return -EINVAL;
195 		}
196 
197 		memcpy(&priv->bittiming, &bt, sizeof(bt));
198 
199 		if (priv->do_set_bittiming) {
200 			/* Finally, set the bit-timing registers */
201 			err = priv->do_set_bittiming(dev);
202 			if (err)
203 				return err;
204 		}
205 	}
206 
207 	if (data[IFLA_CAN_CTRLMODE]) {
208 		struct can_ctrlmode *cm;
209 		u32 ctrlstatic;
210 		u32 maskedflags;
211 
212 		/* Do not allow changing controller mode while running */
213 		if (dev->flags & IFF_UP)
214 			return -EBUSY;
215 		cm = nla_data(data[IFLA_CAN_CTRLMODE]);
216 		ctrlstatic = can_get_static_ctrlmode(priv);
217 		maskedflags = cm->flags & cm->mask;
218 
219 		/* check whether provided bits are allowed to be passed */
220 		if (maskedflags & ~(priv->ctrlmode_supported | ctrlstatic))
221 			return -EOPNOTSUPP;
222 
223 		/* do not check for static fd-non-iso if 'fd' is disabled */
224 		if (!(maskedflags & CAN_CTRLMODE_FD))
225 			ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
226 
227 		/* make sure static options are provided by configuration */
228 		if ((maskedflags & ctrlstatic) != ctrlstatic)
229 			return -EOPNOTSUPP;
230 
231 		/* clear bits to be modified and copy the flag values */
232 		priv->ctrlmode &= ~cm->mask;
233 		priv->ctrlmode |= maskedflags;
234 
235 		/* CAN_CTRLMODE_FD can only be set when driver supports FD */
236 		if (priv->ctrlmode & CAN_CTRLMODE_FD) {
237 			dev->mtu = CANFD_MTU;
238 		} else {
239 			dev->mtu = CAN_MTU;
240 			memset(&priv->data_bittiming, 0,
241 			       sizeof(priv->data_bittiming));
242 			priv->ctrlmode &= ~CAN_CTRLMODE_TDC_MASK;
243 			memset(&priv->tdc, 0, sizeof(priv->tdc));
244 		}
245 
246 		tdc_mask = cm->mask & CAN_CTRLMODE_TDC_MASK;
247 		/* CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually
248 		 * exclusive: make sure to turn the other one off
249 		 */
250 		if (tdc_mask)
251 			priv->ctrlmode &= cm->flags | ~CAN_CTRLMODE_TDC_MASK;
252 	}
253 
254 	if (data[IFLA_CAN_RESTART_MS]) {
255 		/* Do not allow changing restart delay while running */
256 		if (dev->flags & IFF_UP)
257 			return -EBUSY;
258 		priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
259 	}
260 
261 	if (data[IFLA_CAN_RESTART]) {
262 		/* Do not allow a restart while not running */
263 		if (!(dev->flags & IFF_UP))
264 			return -EINVAL;
265 		err = can_restart_now(dev);
266 		if (err)
267 			return err;
268 	}
269 
270 	if (data[IFLA_CAN_DATA_BITTIMING]) {
271 		struct can_bittiming dbt;
272 
273 		/* Do not allow changing bittiming while running */
274 		if (dev->flags & IFF_UP)
275 			return -EBUSY;
276 
277 		/* Calculate bittiming parameters based on
278 		 * data_bittiming_const if set, otherwise pass bitrate
279 		 * directly via do_set_bitrate(). Bail out if neither
280 		 * is given.
281 		 */
282 		if (!priv->data_bittiming_const && !priv->do_set_data_bittiming &&
283 		    !priv->data_bitrate_const)
284 			return -EOPNOTSUPP;
285 
286 		memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
287 		       sizeof(dbt));
288 		err = can_get_bittiming(dev, &dbt,
289 					priv->data_bittiming_const,
290 					priv->data_bitrate_const,
291 					priv->data_bitrate_const_cnt);
292 		if (err)
293 			return err;
294 
295 		if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) {
296 			netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n",
297 				   priv->bitrate_max);
298 			return -EINVAL;
299 		}
300 
301 		memset(&priv->tdc, 0, sizeof(priv->tdc));
302 		if (data[IFLA_CAN_TDC]) {
303 			/* TDC parameters are provided: use them */
304 			err = can_tdc_changelink(priv, data[IFLA_CAN_TDC],
305 						 extack);
306 			if (err) {
307 				priv->ctrlmode &= ~CAN_CTRLMODE_TDC_MASK;
308 				return err;
309 			}
310 		} else if (!tdc_mask) {
311 			/* Neither of TDC parameters nor TDC flags are
312 			 * provided: do calculation
313 			 */
314 			can_calc_tdco(&priv->tdc, priv->tdc_const, &dbt,
315 				      &priv->ctrlmode, priv->ctrlmode_supported);
316 		} /* else: both CAN_CTRLMODE_TDC_{AUTO,MANUAL} are explicitly
317 		   * turned off. TDC is disabled: do nothing
318 		   */
319 
320 		memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
321 
322 		if (priv->do_set_data_bittiming) {
323 			/* Finally, set the bit-timing registers */
324 			err = priv->do_set_data_bittiming(dev);
325 			if (err)
326 				return err;
327 		}
328 	}
329 
330 	if (data[IFLA_CAN_TERMINATION]) {
331 		const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
332 		const unsigned int num_term = priv->termination_const_cnt;
333 		unsigned int i;
334 
335 		if (!priv->do_set_termination)
336 			return -EOPNOTSUPP;
337 
338 		/* check whether given value is supported by the interface */
339 		for (i = 0; i < num_term; i++) {
340 			if (termval == priv->termination_const[i])
341 				break;
342 		}
343 		if (i >= num_term)
344 			return -EINVAL;
345 
346 		/* Finally, set the termination value */
347 		err = priv->do_set_termination(dev, termval);
348 		if (err)
349 			return err;
350 
351 		priv->termination = termval;
352 	}
353 
354 	return 0;
355 }
356 
can_tdc_get_size(const struct net_device * dev)357 static size_t can_tdc_get_size(const struct net_device *dev)
358 {
359 	struct can_priv *priv = netdev_priv(dev);
360 	size_t size;
361 
362 	if (!priv->tdc_const)
363 		return 0;
364 
365 	size = nla_total_size(0);			/* nest IFLA_CAN_TDC */
366 	if (priv->ctrlmode_supported & CAN_CTRLMODE_TDC_MANUAL) {
367 		size += nla_total_size(sizeof(u32));	/* IFLA_CAN_TDCV_MIN */
368 		size += nla_total_size(sizeof(u32));	/* IFLA_CAN_TDCV_MAX */
369 	}
370 	size += nla_total_size(sizeof(u32));		/* IFLA_CAN_TDCO_MIN */
371 	size += nla_total_size(sizeof(u32));		/* IFLA_CAN_TDCO_MAX */
372 	if (priv->tdc_const->tdcf_max) {
373 		size += nla_total_size(sizeof(u32));	/* IFLA_CAN_TDCF_MIN */
374 		size += nla_total_size(sizeof(u32));	/* IFLA_CAN_TDCF_MAX */
375 	}
376 
377 	if (can_tdc_is_enabled(priv)) {
378 		if (priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL ||
379 		    priv->do_get_auto_tdcv)
380 			size += nla_total_size(sizeof(u32));	/* IFLA_CAN_TDCV */
381 		size += nla_total_size(sizeof(u32));		/* IFLA_CAN_TDCO */
382 		if (priv->tdc_const->tdcf_max)
383 			size += nla_total_size(sizeof(u32));	/* IFLA_CAN_TDCF */
384 	}
385 
386 	return size;
387 }
388 
can_ctrlmode_ext_get_size(void)389 static size_t can_ctrlmode_ext_get_size(void)
390 {
391 	return nla_total_size(0) +		/* nest IFLA_CAN_CTRLMODE_EXT */
392 		nla_total_size(sizeof(u32));	/* IFLA_CAN_CTRLMODE_SUPPORTED */
393 }
394 
can_get_size(const struct net_device * dev)395 static size_t can_get_size(const struct net_device *dev)
396 {
397 	struct can_priv *priv = netdev_priv(dev);
398 	size_t size = 0;
399 
400 	if (priv->bittiming.bitrate)				/* IFLA_CAN_BITTIMING */
401 		size += nla_total_size(sizeof(struct can_bittiming));
402 	if (priv->bittiming_const)				/* IFLA_CAN_BITTIMING_CONST */
403 		size += nla_total_size(sizeof(struct can_bittiming_const));
404 	size += nla_total_size(sizeof(struct can_clock));	/* IFLA_CAN_CLOCK */
405 	size += nla_total_size(sizeof(u32));			/* IFLA_CAN_STATE */
406 	size += nla_total_size(sizeof(struct can_ctrlmode));	/* IFLA_CAN_CTRLMODE */
407 	size += nla_total_size(sizeof(u32));			/* IFLA_CAN_RESTART_MS */
408 	if (priv->do_get_berr_counter)				/* IFLA_CAN_BERR_COUNTER */
409 		size += nla_total_size(sizeof(struct can_berr_counter));
410 	if (priv->data_bittiming.bitrate)			/* IFLA_CAN_DATA_BITTIMING */
411 		size += nla_total_size(sizeof(struct can_bittiming));
412 	if (priv->data_bittiming_const)				/* IFLA_CAN_DATA_BITTIMING_CONST */
413 		size += nla_total_size(sizeof(struct can_bittiming_const));
414 	if (priv->termination_const) {
415 		size += nla_total_size(sizeof(priv->termination));		/* IFLA_CAN_TERMINATION */
416 		size += nla_total_size(sizeof(*priv->termination_const) *	/* IFLA_CAN_TERMINATION_CONST */
417 				       priv->termination_const_cnt);
418 	}
419 	if (priv->bitrate_const)				/* IFLA_CAN_BITRATE_CONST */
420 		size += nla_total_size(sizeof(*priv->bitrate_const) *
421 				       priv->bitrate_const_cnt);
422 	if (priv->data_bitrate_const)				/* IFLA_CAN_DATA_BITRATE_CONST */
423 		size += nla_total_size(sizeof(*priv->data_bitrate_const) *
424 				       priv->data_bitrate_const_cnt);
425 	size += sizeof(priv->bitrate_max);			/* IFLA_CAN_BITRATE_MAX */
426 	size += can_tdc_get_size(dev);				/* IFLA_CAN_TDC */
427 	size += can_ctrlmode_ext_get_size();			/* IFLA_CAN_CTRLMODE_EXT */
428 
429 	return size;
430 }
431 
can_tdc_fill_info(struct sk_buff * skb,const struct net_device * dev)432 static int can_tdc_fill_info(struct sk_buff *skb, const struct net_device *dev)
433 {
434 	struct nlattr *nest;
435 	struct can_priv *priv = netdev_priv(dev);
436 	struct can_tdc *tdc = &priv->tdc;
437 	const struct can_tdc_const *tdc_const = priv->tdc_const;
438 
439 	if (!tdc_const)
440 		return 0;
441 
442 	nest = nla_nest_start(skb, IFLA_CAN_TDC);
443 	if (!nest)
444 		return -EMSGSIZE;
445 
446 	if (priv->ctrlmode_supported & CAN_CTRLMODE_TDC_MANUAL &&
447 	    (nla_put_u32(skb, IFLA_CAN_TDC_TDCV_MIN, tdc_const->tdcv_min) ||
448 	     nla_put_u32(skb, IFLA_CAN_TDC_TDCV_MAX, tdc_const->tdcv_max)))
449 		goto err_cancel;
450 	if (nla_put_u32(skb, IFLA_CAN_TDC_TDCO_MIN, tdc_const->tdco_min) ||
451 	    nla_put_u32(skb, IFLA_CAN_TDC_TDCO_MAX, tdc_const->tdco_max))
452 		goto err_cancel;
453 	if (tdc_const->tdcf_max &&
454 	    (nla_put_u32(skb, IFLA_CAN_TDC_TDCF_MIN, tdc_const->tdcf_min) ||
455 	     nla_put_u32(skb, IFLA_CAN_TDC_TDCF_MAX, tdc_const->tdcf_max)))
456 		goto err_cancel;
457 
458 	if (can_tdc_is_enabled(priv)) {
459 		u32 tdcv;
460 		int err = -EINVAL;
461 
462 		if (priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL) {
463 			tdcv = tdc->tdcv;
464 			err = 0;
465 		} else if (priv->do_get_auto_tdcv) {
466 			err = priv->do_get_auto_tdcv(dev, &tdcv);
467 		}
468 		if (!err && nla_put_u32(skb, IFLA_CAN_TDC_TDCV, tdcv))
469 			goto err_cancel;
470 		if (nla_put_u32(skb, IFLA_CAN_TDC_TDCO, tdc->tdco))
471 			goto err_cancel;
472 		if (tdc_const->tdcf_max &&
473 		    nla_put_u32(skb, IFLA_CAN_TDC_TDCF, tdc->tdcf))
474 			goto err_cancel;
475 	}
476 
477 	nla_nest_end(skb, nest);
478 	return 0;
479 
480 err_cancel:
481 	nla_nest_cancel(skb, nest);
482 	return -EMSGSIZE;
483 }
484 
can_ctrlmode_ext_fill_info(struct sk_buff * skb,const struct can_priv * priv)485 static int can_ctrlmode_ext_fill_info(struct sk_buff *skb,
486 				      const struct can_priv *priv)
487 {
488 	struct nlattr *nest;
489 
490 	nest = nla_nest_start(skb, IFLA_CAN_CTRLMODE_EXT);
491 	if (!nest)
492 		return -EMSGSIZE;
493 
494 	if (nla_put_u32(skb, IFLA_CAN_CTRLMODE_SUPPORTED,
495 			priv->ctrlmode_supported)) {
496 		nla_nest_cancel(skb, nest);
497 		return -EMSGSIZE;
498 	}
499 
500 	nla_nest_end(skb, nest);
501 	return 0;
502 }
503 
can_fill_info(struct sk_buff * skb,const struct net_device * dev)504 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
505 {
506 	struct can_priv *priv = netdev_priv(dev);
507 	struct can_ctrlmode cm = {.flags = priv->ctrlmode};
508 	struct can_berr_counter bec = { };
509 	enum can_state state = priv->state;
510 
511 	if (priv->do_get_state)
512 		priv->do_get_state(dev, &state);
513 
514 	if ((priv->bittiming.bitrate != CAN_BITRATE_UNSET &&
515 	     priv->bittiming.bitrate != CAN_BITRATE_UNKNOWN &&
516 	     nla_put(skb, IFLA_CAN_BITTIMING,
517 		     sizeof(priv->bittiming), &priv->bittiming)) ||
518 
519 	    (priv->bittiming_const &&
520 	     nla_put(skb, IFLA_CAN_BITTIMING_CONST,
521 		     sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
522 
523 	    nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
524 	    nla_put_u32(skb, IFLA_CAN_STATE, state) ||
525 	    nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
526 	    nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
527 
528 	    (priv->do_get_berr_counter &&
529 	     !priv->do_get_berr_counter(dev, &bec) &&
530 	     nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
531 
532 	    (priv->data_bittiming.bitrate &&
533 	     nla_put(skb, IFLA_CAN_DATA_BITTIMING,
534 		     sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
535 
536 	    (priv->data_bittiming_const &&
537 	     nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
538 		     sizeof(*priv->data_bittiming_const),
539 		     priv->data_bittiming_const)) ||
540 
541 	    (priv->termination_const &&
542 	     (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
543 	      nla_put(skb, IFLA_CAN_TERMINATION_CONST,
544 		      sizeof(*priv->termination_const) *
545 		      priv->termination_const_cnt,
546 		      priv->termination_const))) ||
547 
548 	    (priv->bitrate_const &&
549 	     nla_put(skb, IFLA_CAN_BITRATE_CONST,
550 		     sizeof(*priv->bitrate_const) *
551 		     priv->bitrate_const_cnt,
552 		     priv->bitrate_const)) ||
553 
554 	    (priv->data_bitrate_const &&
555 	     nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
556 		     sizeof(*priv->data_bitrate_const) *
557 		     priv->data_bitrate_const_cnt,
558 		     priv->data_bitrate_const)) ||
559 
560 	    (nla_put(skb, IFLA_CAN_BITRATE_MAX,
561 		     sizeof(priv->bitrate_max),
562 		     &priv->bitrate_max)) ||
563 
564 	    can_tdc_fill_info(skb, dev) ||
565 
566 	    can_ctrlmode_ext_fill_info(skb, priv)
567 	    )
568 
569 		return -EMSGSIZE;
570 
571 	return 0;
572 }
573 
can_get_xstats_size(const struct net_device * dev)574 static size_t can_get_xstats_size(const struct net_device *dev)
575 {
576 	return sizeof(struct can_device_stats);
577 }
578 
can_fill_xstats(struct sk_buff * skb,const struct net_device * dev)579 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
580 {
581 	struct can_priv *priv = netdev_priv(dev);
582 
583 	if (nla_put(skb, IFLA_INFO_XSTATS,
584 		    sizeof(priv->can_stats), &priv->can_stats))
585 		goto nla_put_failure;
586 	return 0;
587 
588 nla_put_failure:
589 	return -EMSGSIZE;
590 }
591 
can_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)592 static int can_newlink(struct net *src_net, struct net_device *dev,
593 		       struct nlattr *tb[], struct nlattr *data[],
594 		       struct netlink_ext_ack *extack)
595 {
596 	return -EOPNOTSUPP;
597 }
598 
can_dellink(struct net_device * dev,struct list_head * head)599 static void can_dellink(struct net_device *dev, struct list_head *head)
600 {
601 }
602 
603 struct rtnl_link_ops can_link_ops __read_mostly = {
604 	.kind		= "can",
605 	.netns_refund	= true,
606 	.maxtype	= IFLA_CAN_MAX,
607 	.policy		= can_policy,
608 	.setup		= can_setup,
609 	.validate	= can_validate,
610 	.newlink	= can_newlink,
611 	.changelink	= can_changelink,
612 	.dellink	= can_dellink,
613 	.get_size	= can_get_size,
614 	.fill_info	= can_fill_info,
615 	.get_xstats_size = can_get_xstats_size,
616 	.fill_xstats	= can_fill_xstats,
617 };
618 
can_netlink_register(void)619 int can_netlink_register(void)
620 {
621 	return rtnl_link_register(&can_link_ops);
622 }
623 
can_netlink_unregister(void)624 void can_netlink_unregister(void)
625 {
626 	rtnl_link_unregister(&can_link_ops);
627 }
628