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