xref: /openbmc/linux/drivers/net/can/dev/netlink.c (revision 79e790ff)
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 || !data[IFLA_CAN_BITTIMING])
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 (cm->mask & ~(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 	}
140 
141 	if (data[IFLA_CAN_RESTART_MS]) {
142 		/* Do not allow changing restart delay while running */
143 		if (dev->flags & IFF_UP)
144 			return -EBUSY;
145 		priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
146 	}
147 
148 	if (data[IFLA_CAN_RESTART]) {
149 		/* Do not allow a restart while not running */
150 		if (!(dev->flags & IFF_UP))
151 			return -EINVAL;
152 		err = can_restart_now(dev);
153 		if (err)
154 			return err;
155 	}
156 
157 	if (data[IFLA_CAN_DATA_BITTIMING]) {
158 		struct can_bittiming dbt;
159 
160 		/* Do not allow changing bittiming while running */
161 		if (dev->flags & IFF_UP)
162 			return -EBUSY;
163 
164 		/* Calculate bittiming parameters based on
165 		 * data_bittiming_const if set, otherwise pass bitrate
166 		 * directly via do_set_bitrate(). Bail out if neither
167 		 * is given.
168 		 */
169 		if (!priv->data_bittiming_const && !priv->do_set_data_bittiming)
170 			return -EOPNOTSUPP;
171 
172 		memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
173 		       sizeof(dbt));
174 		err = can_get_bittiming(dev, &dbt,
175 					priv->data_bittiming_const,
176 					priv->data_bitrate_const,
177 					priv->data_bitrate_const_cnt);
178 		if (err)
179 			return err;
180 
181 		if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) {
182 			netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n",
183 				   priv->bitrate_max);
184 			return -EINVAL;
185 		}
186 
187 		memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
188 
189 		can_calc_tdco(dev);
190 
191 		if (priv->do_set_data_bittiming) {
192 			/* Finally, set the bit-timing registers */
193 			err = priv->do_set_data_bittiming(dev);
194 			if (err)
195 				return err;
196 		}
197 	}
198 
199 	if (data[IFLA_CAN_TERMINATION]) {
200 		const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
201 		const unsigned int num_term = priv->termination_const_cnt;
202 		unsigned int i;
203 
204 		if (!priv->do_set_termination)
205 			return -EOPNOTSUPP;
206 
207 		/* check whether given value is supported by the interface */
208 		for (i = 0; i < num_term; i++) {
209 			if (termval == priv->termination_const[i])
210 				break;
211 		}
212 		if (i >= num_term)
213 			return -EINVAL;
214 
215 		/* Finally, set the termination value */
216 		err = priv->do_set_termination(dev, termval);
217 		if (err)
218 			return err;
219 
220 		priv->termination = termval;
221 	}
222 
223 	return 0;
224 }
225 
226 static size_t can_get_size(const struct net_device *dev)
227 {
228 	struct can_priv *priv = netdev_priv(dev);
229 	size_t size = 0;
230 
231 	if (priv->bittiming.bitrate)				/* IFLA_CAN_BITTIMING */
232 		size += nla_total_size(sizeof(struct can_bittiming));
233 	if (priv->bittiming_const)				/* IFLA_CAN_BITTIMING_CONST */
234 		size += nla_total_size(sizeof(struct can_bittiming_const));
235 	size += nla_total_size(sizeof(struct can_clock));	/* IFLA_CAN_CLOCK */
236 	size += nla_total_size(sizeof(u32));			/* IFLA_CAN_STATE */
237 	size += nla_total_size(sizeof(struct can_ctrlmode));	/* IFLA_CAN_CTRLMODE */
238 	size += nla_total_size(sizeof(u32));			/* IFLA_CAN_RESTART_MS */
239 	if (priv->do_get_berr_counter)				/* IFLA_CAN_BERR_COUNTER */
240 		size += nla_total_size(sizeof(struct can_berr_counter));
241 	if (priv->data_bittiming.bitrate)			/* IFLA_CAN_DATA_BITTIMING */
242 		size += nla_total_size(sizeof(struct can_bittiming));
243 	if (priv->data_bittiming_const)				/* IFLA_CAN_DATA_BITTIMING_CONST */
244 		size += nla_total_size(sizeof(struct can_bittiming_const));
245 	if (priv->termination_const) {
246 		size += nla_total_size(sizeof(priv->termination));		/* IFLA_CAN_TERMINATION */
247 		size += nla_total_size(sizeof(*priv->termination_const) *	/* IFLA_CAN_TERMINATION_CONST */
248 				       priv->termination_const_cnt);
249 	}
250 	if (priv->bitrate_const)				/* IFLA_CAN_BITRATE_CONST */
251 		size += nla_total_size(sizeof(*priv->bitrate_const) *
252 				       priv->bitrate_const_cnt);
253 	if (priv->data_bitrate_const)				/* IFLA_CAN_DATA_BITRATE_CONST */
254 		size += nla_total_size(sizeof(*priv->data_bitrate_const) *
255 				       priv->data_bitrate_const_cnt);
256 	size += sizeof(priv->bitrate_max);			/* IFLA_CAN_BITRATE_MAX */
257 
258 	return size;
259 }
260 
261 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
262 {
263 	struct can_priv *priv = netdev_priv(dev);
264 	struct can_ctrlmode cm = {.flags = priv->ctrlmode};
265 	struct can_berr_counter bec = { };
266 	enum can_state state = priv->state;
267 
268 	if (priv->do_get_state)
269 		priv->do_get_state(dev, &state);
270 
271 	if ((priv->bittiming.bitrate &&
272 	     nla_put(skb, IFLA_CAN_BITTIMING,
273 		     sizeof(priv->bittiming), &priv->bittiming)) ||
274 
275 	    (priv->bittiming_const &&
276 	     nla_put(skb, IFLA_CAN_BITTIMING_CONST,
277 		     sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
278 
279 	    nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
280 	    nla_put_u32(skb, IFLA_CAN_STATE, state) ||
281 	    nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
282 	    nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
283 
284 	    (priv->do_get_berr_counter &&
285 	     !priv->do_get_berr_counter(dev, &bec) &&
286 	     nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
287 
288 	    (priv->data_bittiming.bitrate &&
289 	     nla_put(skb, IFLA_CAN_DATA_BITTIMING,
290 		     sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
291 
292 	    (priv->data_bittiming_const &&
293 	     nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
294 		     sizeof(*priv->data_bittiming_const),
295 		     priv->data_bittiming_const)) ||
296 
297 	    (priv->termination_const &&
298 	     (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
299 	      nla_put(skb, IFLA_CAN_TERMINATION_CONST,
300 		      sizeof(*priv->termination_const) *
301 		      priv->termination_const_cnt,
302 		      priv->termination_const))) ||
303 
304 	    (priv->bitrate_const &&
305 	     nla_put(skb, IFLA_CAN_BITRATE_CONST,
306 		     sizeof(*priv->bitrate_const) *
307 		     priv->bitrate_const_cnt,
308 		     priv->bitrate_const)) ||
309 
310 	    (priv->data_bitrate_const &&
311 	     nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
312 		     sizeof(*priv->data_bitrate_const) *
313 		     priv->data_bitrate_const_cnt,
314 		     priv->data_bitrate_const)) ||
315 
316 	    (nla_put(skb, IFLA_CAN_BITRATE_MAX,
317 		     sizeof(priv->bitrate_max),
318 		     &priv->bitrate_max))
319 	    )
320 
321 		return -EMSGSIZE;
322 
323 	return 0;
324 }
325 
326 static size_t can_get_xstats_size(const struct net_device *dev)
327 {
328 	return sizeof(struct can_device_stats);
329 }
330 
331 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
332 {
333 	struct can_priv *priv = netdev_priv(dev);
334 
335 	if (nla_put(skb, IFLA_INFO_XSTATS,
336 		    sizeof(priv->can_stats), &priv->can_stats))
337 		goto nla_put_failure;
338 	return 0;
339 
340 nla_put_failure:
341 	return -EMSGSIZE;
342 }
343 
344 static int can_newlink(struct net *src_net, struct net_device *dev,
345 		       struct nlattr *tb[], struct nlattr *data[],
346 		       struct netlink_ext_ack *extack)
347 {
348 	return -EOPNOTSUPP;
349 }
350 
351 static void can_dellink(struct net_device *dev, struct list_head *head)
352 {
353 }
354 
355 struct rtnl_link_ops can_link_ops __read_mostly = {
356 	.kind		= "can",
357 	.netns_refund	= true,
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