xref: /openbmc/linux/drivers/net/can/dev/netlink.c (revision 97e6ea6d)
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  */
6 
7 #include <linux/can/dev.h>
8 #include <net/rtnetlink.h>
9 
10 static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
11 	[IFLA_CAN_STATE] = { .type = NLA_U32 },
12 	[IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
13 	[IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
14 	[IFLA_CAN_RESTART] = { .type = NLA_U32 },
15 	[IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
16 	[IFLA_CAN_BITTIMING_CONST] = { .len = sizeof(struct can_bittiming_const) },
17 	[IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
18 	[IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
19 	[IFLA_CAN_DATA_BITTIMING] = { .len = sizeof(struct can_bittiming) },
20 	[IFLA_CAN_DATA_BITTIMING_CONST]	= { .len = sizeof(struct can_bittiming_const) },
21 	[IFLA_CAN_TERMINATION] = { .type = NLA_U16 },
22 };
23 
24 static int can_validate(struct nlattr *tb[], struct nlattr *data[],
25 			struct netlink_ext_ack *extack)
26 {
27 	bool is_can_fd = false;
28 
29 	/* Make sure that valid CAN FD configurations always consist of
30 	 * - nominal/arbitration bittiming
31 	 * - data bittiming
32 	 * - control mode with CAN_CTRLMODE_FD set
33 	 */
34 
35 	if (!data)
36 		return 0;
37 
38 	if (data[IFLA_CAN_CTRLMODE]) {
39 		struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
40 
41 		is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
42 	}
43 
44 	if (is_can_fd) {
45 		if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
46 			return -EOPNOTSUPP;
47 	}
48 
49 	if (data[IFLA_CAN_DATA_BITTIMING]) {
50 		if (!is_can_fd)
51 			return -EOPNOTSUPP;
52 	}
53 
54 	return 0;
55 }
56 
57 static int can_changelink(struct net_device *dev, struct nlattr *tb[],
58 			  struct nlattr *data[],
59 			  struct netlink_ext_ack *extack)
60 {
61 	struct can_priv *priv = netdev_priv(dev);
62 	int err;
63 
64 	/* We need synchronization with dev->stop() */
65 	ASSERT_RTNL();
66 
67 	if (data[IFLA_CAN_BITTIMING]) {
68 		struct can_bittiming bt;
69 
70 		/* Do not allow changing bittiming while running */
71 		if (dev->flags & IFF_UP)
72 			return -EBUSY;
73 
74 		/* Calculate bittiming parameters based on
75 		 * bittiming_const if set, otherwise pass bitrate
76 		 * directly via do_set_bitrate(). Bail out if neither
77 		 * is given.
78 		 */
79 		if (!priv->bittiming_const && !priv->do_set_bittiming)
80 			return -EOPNOTSUPP;
81 
82 		memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
83 		err = can_get_bittiming(dev, &bt,
84 					priv->bittiming_const,
85 					priv->bitrate_const,
86 					priv->bitrate_const_cnt);
87 		if (err)
88 			return err;
89 
90 		if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) {
91 			netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n",
92 				   priv->bitrate_max);
93 			return -EINVAL;
94 		}
95 
96 		memcpy(&priv->bittiming, &bt, sizeof(bt));
97 
98 		if (priv->do_set_bittiming) {
99 			/* Finally, set the bit-timing registers */
100 			err = priv->do_set_bittiming(dev);
101 			if (err)
102 				return err;
103 		}
104 	}
105 
106 	if (data[IFLA_CAN_CTRLMODE]) {
107 		struct can_ctrlmode *cm;
108 		u32 ctrlstatic;
109 		u32 maskedflags;
110 
111 		/* Do not allow changing controller mode while running */
112 		if (dev->flags & IFF_UP)
113 			return -EBUSY;
114 		cm = nla_data(data[IFLA_CAN_CTRLMODE]);
115 		ctrlstatic = priv->ctrlmode_static;
116 		maskedflags = cm->flags & cm->mask;
117 
118 		/* check whether provided bits are allowed to be passed */
119 		if (maskedflags & ~(priv->ctrlmode_supported | ctrlstatic))
120 			return -EOPNOTSUPP;
121 
122 		/* do not check for static fd-non-iso if 'fd' is disabled */
123 		if (!(maskedflags & CAN_CTRLMODE_FD))
124 			ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
125 
126 		/* make sure static options are provided by configuration */
127 		if ((maskedflags & ctrlstatic) != ctrlstatic)
128 			return -EOPNOTSUPP;
129 
130 		/* clear bits to be modified and copy the flag values */
131 		priv->ctrlmode &= ~cm->mask;
132 		priv->ctrlmode |= maskedflags;
133 
134 		/* CAN_CTRLMODE_FD can only be set when driver supports FD */
135 		if (priv->ctrlmode & CAN_CTRLMODE_FD) {
136 			dev->mtu = CANFD_MTU;
137 		} else {
138 			dev->mtu = CAN_MTU;
139 			memset(&priv->data_bittiming, 0,
140 			       sizeof(priv->data_bittiming));
141 		}
142 	}
143 
144 	if (data[IFLA_CAN_RESTART_MS]) {
145 		/* Do not allow changing restart delay while running */
146 		if (dev->flags & IFF_UP)
147 			return -EBUSY;
148 		priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
149 	}
150 
151 	if (data[IFLA_CAN_RESTART]) {
152 		/* Do not allow a restart while not running */
153 		if (!(dev->flags & IFF_UP))
154 			return -EINVAL;
155 		err = can_restart_now(dev);
156 		if (err)
157 			return err;
158 	}
159 
160 	if (data[IFLA_CAN_DATA_BITTIMING]) {
161 		struct can_bittiming dbt;
162 
163 		/* Do not allow changing bittiming while running */
164 		if (dev->flags & IFF_UP)
165 			return -EBUSY;
166 
167 		/* Calculate bittiming parameters based on
168 		 * data_bittiming_const if set, otherwise pass bitrate
169 		 * directly via do_set_bitrate(). Bail out if neither
170 		 * is given.
171 		 */
172 		if (!priv->data_bittiming_const && !priv->do_set_data_bittiming)
173 			return -EOPNOTSUPP;
174 
175 		memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
176 		       sizeof(dbt));
177 		err = can_get_bittiming(dev, &dbt,
178 					priv->data_bittiming_const,
179 					priv->data_bitrate_const,
180 					priv->data_bitrate_const_cnt);
181 		if (err)
182 			return err;
183 
184 		if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) {
185 			netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n",
186 				   priv->bitrate_max);
187 			return -EINVAL;
188 		}
189 
190 		memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
191 
192 		can_calc_tdco(dev);
193 
194 		if (priv->do_set_data_bittiming) {
195 			/* Finally, set the bit-timing registers */
196 			err = priv->do_set_data_bittiming(dev);
197 			if (err)
198 				return err;
199 		}
200 	}
201 
202 	if (data[IFLA_CAN_TERMINATION]) {
203 		const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
204 		const unsigned int num_term = priv->termination_const_cnt;
205 		unsigned int i;
206 
207 		if (!priv->do_set_termination)
208 			return -EOPNOTSUPP;
209 
210 		/* check whether given value is supported by the interface */
211 		for (i = 0; i < num_term; i++) {
212 			if (termval == priv->termination_const[i])
213 				break;
214 		}
215 		if (i >= num_term)
216 			return -EINVAL;
217 
218 		/* Finally, set the termination value */
219 		err = priv->do_set_termination(dev, termval);
220 		if (err)
221 			return err;
222 
223 		priv->termination = termval;
224 	}
225 
226 	return 0;
227 }
228 
229 static size_t can_get_size(const struct net_device *dev)
230 {
231 	struct can_priv *priv = netdev_priv(dev);
232 	size_t size = 0;
233 
234 	if (priv->bittiming.bitrate)				/* IFLA_CAN_BITTIMING */
235 		size += nla_total_size(sizeof(struct can_bittiming));
236 	if (priv->bittiming_const)				/* IFLA_CAN_BITTIMING_CONST */
237 		size += nla_total_size(sizeof(struct can_bittiming_const));
238 	size += nla_total_size(sizeof(struct can_clock));	/* IFLA_CAN_CLOCK */
239 	size += nla_total_size(sizeof(u32));			/* IFLA_CAN_STATE */
240 	size += nla_total_size(sizeof(struct can_ctrlmode));	/* IFLA_CAN_CTRLMODE */
241 	size += nla_total_size(sizeof(u32));			/* IFLA_CAN_RESTART_MS */
242 	if (priv->do_get_berr_counter)				/* IFLA_CAN_BERR_COUNTER */
243 		size += nla_total_size(sizeof(struct can_berr_counter));
244 	if (priv->data_bittiming.bitrate)			/* IFLA_CAN_DATA_BITTIMING */
245 		size += nla_total_size(sizeof(struct can_bittiming));
246 	if (priv->data_bittiming_const)				/* IFLA_CAN_DATA_BITTIMING_CONST */
247 		size += nla_total_size(sizeof(struct can_bittiming_const));
248 	if (priv->termination_const) {
249 		size += nla_total_size(sizeof(priv->termination));		/* IFLA_CAN_TERMINATION */
250 		size += nla_total_size(sizeof(*priv->termination_const) *	/* IFLA_CAN_TERMINATION_CONST */
251 				       priv->termination_const_cnt);
252 	}
253 	if (priv->bitrate_const)				/* IFLA_CAN_BITRATE_CONST */
254 		size += nla_total_size(sizeof(*priv->bitrate_const) *
255 				       priv->bitrate_const_cnt);
256 	if (priv->data_bitrate_const)				/* IFLA_CAN_DATA_BITRATE_CONST */
257 		size += nla_total_size(sizeof(*priv->data_bitrate_const) *
258 				       priv->data_bitrate_const_cnt);
259 	size += sizeof(priv->bitrate_max);			/* IFLA_CAN_BITRATE_MAX */
260 
261 	return size;
262 }
263 
264 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
265 {
266 	struct can_priv *priv = netdev_priv(dev);
267 	struct can_ctrlmode cm = {.flags = priv->ctrlmode};
268 	struct can_berr_counter bec = { };
269 	enum can_state state = priv->state;
270 
271 	if (priv->do_get_state)
272 		priv->do_get_state(dev, &state);
273 
274 	if ((priv->bittiming.bitrate &&
275 	     nla_put(skb, IFLA_CAN_BITTIMING,
276 		     sizeof(priv->bittiming), &priv->bittiming)) ||
277 
278 	    (priv->bittiming_const &&
279 	     nla_put(skb, IFLA_CAN_BITTIMING_CONST,
280 		     sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
281 
282 	    nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
283 	    nla_put_u32(skb, IFLA_CAN_STATE, state) ||
284 	    nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
285 	    nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
286 
287 	    (priv->do_get_berr_counter &&
288 	     !priv->do_get_berr_counter(dev, &bec) &&
289 	     nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
290 
291 	    (priv->data_bittiming.bitrate &&
292 	     nla_put(skb, IFLA_CAN_DATA_BITTIMING,
293 		     sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
294 
295 	    (priv->data_bittiming_const &&
296 	     nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
297 		     sizeof(*priv->data_bittiming_const),
298 		     priv->data_bittiming_const)) ||
299 
300 	    (priv->termination_const &&
301 	     (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
302 	      nla_put(skb, IFLA_CAN_TERMINATION_CONST,
303 		      sizeof(*priv->termination_const) *
304 		      priv->termination_const_cnt,
305 		      priv->termination_const))) ||
306 
307 	    (priv->bitrate_const &&
308 	     nla_put(skb, IFLA_CAN_BITRATE_CONST,
309 		     sizeof(*priv->bitrate_const) *
310 		     priv->bitrate_const_cnt,
311 		     priv->bitrate_const)) ||
312 
313 	    (priv->data_bitrate_const &&
314 	     nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
315 		     sizeof(*priv->data_bitrate_const) *
316 		     priv->data_bitrate_const_cnt,
317 		     priv->data_bitrate_const)) ||
318 
319 	    (nla_put(skb, IFLA_CAN_BITRATE_MAX,
320 		     sizeof(priv->bitrate_max),
321 		     &priv->bitrate_max))
322 	    )
323 
324 		return -EMSGSIZE;
325 
326 	return 0;
327 }
328 
329 static size_t can_get_xstats_size(const struct net_device *dev)
330 {
331 	return sizeof(struct can_device_stats);
332 }
333 
334 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
335 {
336 	struct can_priv *priv = netdev_priv(dev);
337 
338 	if (nla_put(skb, IFLA_INFO_XSTATS,
339 		    sizeof(priv->can_stats), &priv->can_stats))
340 		goto nla_put_failure;
341 	return 0;
342 
343 nla_put_failure:
344 	return -EMSGSIZE;
345 }
346 
347 static int can_newlink(struct net *src_net, struct net_device *dev,
348 		       struct nlattr *tb[], struct nlattr *data[],
349 		       struct netlink_ext_ack *extack)
350 {
351 	return -EOPNOTSUPP;
352 }
353 
354 static void can_dellink(struct net_device *dev, struct list_head *head)
355 {
356 }
357 
358 struct rtnl_link_ops can_link_ops __read_mostly = {
359 	.kind		= "can",
360 	.netns_refund	= true,
361 	.maxtype	= IFLA_CAN_MAX,
362 	.policy		= can_policy,
363 	.setup		= can_setup,
364 	.validate	= can_validate,
365 	.newlink	= can_newlink,
366 	.changelink	= can_changelink,
367 	.dellink	= can_dellink,
368 	.get_size	= can_get_size,
369 	.fill_info	= can_fill_info,
370 	.get_xstats_size = can_get_xstats_size,
371 	.fill_xstats	= can_fill_xstats,
372 };
373 
374 int can_netlink_register(void)
375 {
376 	return rtnl_link_register(&can_link_ops);
377 }
378 
379 void can_netlink_unregister(void)
380 {
381 	rtnl_link_unregister(&can_link_ops);
382 }
383