xref: /linux/net/hsr/hsr_netlink.c (revision c5288cda69ee2d8607f5026bd599a5cebf0ee783)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2011-2014 Autronica Fire and Security AS
3  *
4  * Author(s):
5  *	2011-2014 Arvid Brodin, arvid.brodin@alten.se
6  *
7  * Routines for handling Netlink messages for HSR and PRP.
8  */
9 
10 #include "hsr_netlink.h"
11 #include <linux/kernel.h>
12 #include <net/rtnetlink.h>
13 #include <net/genetlink.h>
14 #include "hsr_main.h"
15 #include "hsr_device.h"
16 #include "hsr_framereg.h"
17 
18 static const struct nla_policy hsr_policy[IFLA_HSR_MAX + 1] = {
19 	[IFLA_HSR_SLAVE1]		= { .type = NLA_U32 },
20 	[IFLA_HSR_SLAVE2]		= { .type = NLA_U32 },
21 	[IFLA_HSR_MULTICAST_SPEC]	= { .type = NLA_U8 },
22 	[IFLA_HSR_VERSION]	= { .type = NLA_U8 },
23 	[IFLA_HSR_SUPERVISION_ADDR]	= { .len = ETH_ALEN },
24 	[IFLA_HSR_SEQ_NR]		= { .type = NLA_U16 },
25 	[IFLA_HSR_PROTOCOL]		= { .type = NLA_U8 },
26 	[IFLA_HSR_INTERLINK]		= { .type = NLA_U32 },
27 };
28 
29 /* Here, it seems a netdevice has already been allocated for us, and the
30  * hsr_dev_setup routine has been executed. Nice!
31  */
32 static int hsr_newlink(struct net *src_net, struct net_device *dev,
33 		       struct nlattr *tb[], struct nlattr *data[],
34 		       struct netlink_ext_ack *extack)
35 {
36 	enum hsr_version proto_version;
37 	unsigned char multicast_spec;
38 	u8 proto = HSR_PROTOCOL_HSR;
39 
40 	struct net_device *link[2], *interlink = NULL;
41 	if (!data) {
42 		NL_SET_ERR_MSG_MOD(extack, "No slave devices specified");
43 		return -EINVAL;
44 	}
45 	if (!data[IFLA_HSR_SLAVE1]) {
46 		NL_SET_ERR_MSG_MOD(extack, "Slave1 device not specified");
47 		return -EINVAL;
48 	}
49 	link[0] = __dev_get_by_index(src_net,
50 				     nla_get_u32(data[IFLA_HSR_SLAVE1]));
51 	if (!link[0]) {
52 		NL_SET_ERR_MSG_MOD(extack, "Slave1 does not exist");
53 		return -EINVAL;
54 	}
55 	if (!data[IFLA_HSR_SLAVE2]) {
56 		NL_SET_ERR_MSG_MOD(extack, "Slave2 device not specified");
57 		return -EINVAL;
58 	}
59 	link[1] = __dev_get_by_index(src_net,
60 				     nla_get_u32(data[IFLA_HSR_SLAVE2]));
61 	if (!link[1]) {
62 		NL_SET_ERR_MSG_MOD(extack, "Slave2 does not exist");
63 		return -EINVAL;
64 	}
65 
66 	if (link[0] == link[1]) {
67 		NL_SET_ERR_MSG_MOD(extack, "Slave1 and Slave2 are same");
68 		return -EINVAL;
69 	}
70 
71 	if (data[IFLA_HSR_INTERLINK])
72 		interlink = __dev_get_by_index(src_net,
73 					       nla_get_u32(data[IFLA_HSR_INTERLINK]));
74 
75 	if (interlink && interlink == link[0]) {
76 		NL_SET_ERR_MSG_MOD(extack, "Interlink and Slave1 are the same");
77 		return -EINVAL;
78 	}
79 
80 	if (interlink && interlink == link[1]) {
81 		NL_SET_ERR_MSG_MOD(extack, "Interlink and Slave2 are the same");
82 		return -EINVAL;
83 	}
84 
85 	if (!data[IFLA_HSR_MULTICAST_SPEC])
86 		multicast_spec = 0;
87 	else
88 		multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
89 
90 	if (data[IFLA_HSR_PROTOCOL])
91 		proto = nla_get_u8(data[IFLA_HSR_PROTOCOL]);
92 
93 	if (proto >= HSR_PROTOCOL_MAX) {
94 		NL_SET_ERR_MSG_MOD(extack, "Unsupported protocol");
95 		return -EINVAL;
96 	}
97 
98 	if (!data[IFLA_HSR_VERSION]) {
99 		proto_version = HSR_V0;
100 	} else {
101 		if (proto == HSR_PROTOCOL_PRP) {
102 			NL_SET_ERR_MSG_MOD(extack, "PRP version unsupported");
103 			return -EINVAL;
104 		}
105 
106 		proto_version = nla_get_u8(data[IFLA_HSR_VERSION]);
107 		if (proto_version > HSR_V1) {
108 			NL_SET_ERR_MSG_MOD(extack,
109 					   "Only HSR version 0/1 supported");
110 			return -EINVAL;
111 		}
112 	}
113 
114 	if (proto == HSR_PROTOCOL_PRP) {
115 		proto_version = PRP_V1;
116 		if (interlink) {
117 			NL_SET_ERR_MSG_MOD(extack,
118 					   "Interlink only works with HSR");
119 			return -EINVAL;
120 		}
121 	}
122 
123 	return hsr_dev_finalize(dev, link, interlink, multicast_spec,
124 				proto_version, extack);
125 }
126 
127 static void hsr_dellink(struct net_device *dev, struct list_head *head)
128 {
129 	struct hsr_priv *hsr = netdev_priv(dev);
130 
131 	del_timer_sync(&hsr->prune_timer);
132 	del_timer_sync(&hsr->prune_proxy_timer);
133 	del_timer_sync(&hsr->announce_timer);
134 
135 	hsr_debugfs_term(hsr);
136 	hsr_del_ports(hsr);
137 
138 	hsr_del_self_node(hsr);
139 	hsr_del_nodes(&hsr->node_db);
140 	hsr_del_nodes(&hsr->proxy_node_db);
141 
142 	unregister_netdevice_queue(dev, head);
143 }
144 
145 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
146 {
147 	struct hsr_priv *hsr = netdev_priv(dev);
148 	u8 proto = HSR_PROTOCOL_HSR;
149 	struct hsr_port *port;
150 
151 	port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
152 	if (port) {
153 		if (nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex))
154 			goto nla_put_failure;
155 	}
156 
157 	port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
158 	if (port) {
159 		if (nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex))
160 			goto nla_put_failure;
161 	}
162 
163 	if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
164 		    hsr->sup_multicast_addr) ||
165 	    nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
166 		goto nla_put_failure;
167 	if (hsr->prot_version == PRP_V1)
168 		proto = HSR_PROTOCOL_PRP;
169 	if (nla_put_u8(skb, IFLA_HSR_PROTOCOL, proto))
170 		goto nla_put_failure;
171 
172 	return 0;
173 
174 nla_put_failure:
175 	return -EMSGSIZE;
176 }
177 
178 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
179 	.kind		= "hsr",
180 	.maxtype	= IFLA_HSR_MAX,
181 	.policy		= hsr_policy,
182 	.priv_size	= sizeof(struct hsr_priv),
183 	.setup		= hsr_dev_setup,
184 	.newlink	= hsr_newlink,
185 	.dellink	= hsr_dellink,
186 	.fill_info	= hsr_fill_info,
187 };
188 
189 /* attribute policy */
190 static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
191 	[HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
192 	[HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
193 	[HSR_A_IFINDEX] = { .type = NLA_U32 },
194 	[HSR_A_IF1_AGE] = { .type = NLA_U32 },
195 	[HSR_A_IF2_AGE] = { .type = NLA_U32 },
196 	[HSR_A_IF1_SEQ] = { .type = NLA_U16 },
197 	[HSR_A_IF2_SEQ] = { .type = NLA_U16 },
198 };
199 
200 static struct genl_family hsr_genl_family;
201 
202 static const struct genl_multicast_group hsr_mcgrps[] = {
203 	{ .name = "hsr-network", },
204 };
205 
206 /* This is called if for some node with MAC address addr, we only get frames
207  * over one of the slave interfaces. This would indicate an open network ring
208  * (i.e. a link has failed somewhere).
209  */
210 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
211 		      struct hsr_port *port)
212 {
213 	struct sk_buff *skb;
214 	void *msg_head;
215 	struct hsr_port *master;
216 	int res;
217 
218 	skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
219 	if (!skb)
220 		goto fail;
221 
222 	msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0,
223 			       HSR_C_RING_ERROR);
224 	if (!msg_head)
225 		goto nla_put_failure;
226 
227 	res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
228 	if (res < 0)
229 		goto nla_put_failure;
230 
231 	res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
232 	if (res < 0)
233 		goto nla_put_failure;
234 
235 	genlmsg_end(skb, msg_head);
236 	genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
237 
238 	return;
239 
240 nla_put_failure:
241 	kfree_skb(skb);
242 
243 fail:
244 	rcu_read_lock();
245 	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
246 	netdev_warn(master->dev, "Could not send HSR ring error message\n");
247 	rcu_read_unlock();
248 }
249 
250 /* This is called when we haven't heard from the node with MAC address addr for
251  * some time (just before the node is removed from the node table/list).
252  */
253 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
254 {
255 	struct sk_buff *skb;
256 	void *msg_head;
257 	struct hsr_port *master;
258 	int res;
259 
260 	skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
261 	if (!skb)
262 		goto fail;
263 
264 	msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
265 	if (!msg_head)
266 		goto nla_put_failure;
267 
268 	res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
269 	if (res < 0)
270 		goto nla_put_failure;
271 
272 	genlmsg_end(skb, msg_head);
273 	genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
274 
275 	return;
276 
277 nla_put_failure:
278 	kfree_skb(skb);
279 
280 fail:
281 	rcu_read_lock();
282 	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
283 	netdev_warn(master->dev, "Could not send HSR node down\n");
284 	rcu_read_unlock();
285 }
286 
287 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
288  * about the status of a specific node in the network, defined by its MAC
289  * address.
290  *
291  * Input: hsr ifindex, node mac address
292  * Output: hsr ifindex, node mac address (copied from request),
293  *	   age of latest frame from node over slave 1, slave 2 [ms]
294  */
295 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
296 {
297 	/* For receiving */
298 	struct nlattr *na;
299 	struct net_device *hsr_dev;
300 
301 	/* For sending */
302 	struct sk_buff *skb_out;
303 	void *msg_head;
304 	struct hsr_priv *hsr;
305 	struct hsr_port *port;
306 	unsigned char hsr_node_addr_b[ETH_ALEN];
307 	int hsr_node_if1_age;
308 	u16 hsr_node_if1_seq;
309 	int hsr_node_if2_age;
310 	u16 hsr_node_if2_seq;
311 	int addr_b_ifindex;
312 	int res;
313 
314 	if (!info)
315 		goto invalid;
316 
317 	na = info->attrs[HSR_A_IFINDEX];
318 	if (!na)
319 		goto invalid;
320 	na = info->attrs[HSR_A_NODE_ADDR];
321 	if (!na)
322 		goto invalid;
323 
324 	rcu_read_lock();
325 	hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
326 				       nla_get_u32(info->attrs[HSR_A_IFINDEX]));
327 	if (!hsr_dev)
328 		goto rcu_unlock;
329 	if (!is_hsr_master(hsr_dev))
330 		goto rcu_unlock;
331 
332 	/* Send reply */
333 	skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
334 	if (!skb_out) {
335 		res = -ENOMEM;
336 		goto fail;
337 	}
338 
339 	msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
340 			       info->snd_seq, &hsr_genl_family, 0,
341 			       HSR_C_SET_NODE_STATUS);
342 	if (!msg_head) {
343 		res = -ENOMEM;
344 		goto nla_put_failure;
345 	}
346 
347 	res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
348 	if (res < 0)
349 		goto nla_put_failure;
350 
351 	hsr = netdev_priv(hsr_dev);
352 	res = hsr_get_node_data(hsr,
353 				(unsigned char *)
354 				nla_data(info->attrs[HSR_A_NODE_ADDR]),
355 					 hsr_node_addr_b,
356 					 &addr_b_ifindex,
357 					 &hsr_node_if1_age,
358 					 &hsr_node_if1_seq,
359 					 &hsr_node_if2_age,
360 					 &hsr_node_if2_seq);
361 	if (res < 0)
362 		goto nla_put_failure;
363 
364 	res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
365 		      nla_data(info->attrs[HSR_A_NODE_ADDR]));
366 	if (res < 0)
367 		goto nla_put_failure;
368 
369 	if (addr_b_ifindex > -1) {
370 		res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
371 			      hsr_node_addr_b);
372 		if (res < 0)
373 			goto nla_put_failure;
374 
375 		res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX,
376 				  addr_b_ifindex);
377 		if (res < 0)
378 			goto nla_put_failure;
379 	}
380 
381 	res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
382 	if (res < 0)
383 		goto nla_put_failure;
384 	res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
385 	if (res < 0)
386 		goto nla_put_failure;
387 	port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
388 	if (port)
389 		res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
390 				  port->dev->ifindex);
391 	if (res < 0)
392 		goto nla_put_failure;
393 
394 	res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
395 	if (res < 0)
396 		goto nla_put_failure;
397 	res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
398 	if (res < 0)
399 		goto nla_put_failure;
400 	port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
401 	if (port)
402 		res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
403 				  port->dev->ifindex);
404 	if (res < 0)
405 		goto nla_put_failure;
406 
407 	rcu_read_unlock();
408 
409 	genlmsg_end(skb_out, msg_head);
410 	genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
411 
412 	return 0;
413 
414 rcu_unlock:
415 	rcu_read_unlock();
416 invalid:
417 	netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
418 	return 0;
419 
420 nla_put_failure:
421 	kfree_skb(skb_out);
422 	/* Fall through */
423 
424 fail:
425 	rcu_read_unlock();
426 	return res;
427 }
428 
429 /* Get a list of MacAddressA of all nodes known to this node (including self).
430  */
431 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
432 {
433 	unsigned char addr[ETH_ALEN];
434 	struct net_device *hsr_dev;
435 	struct sk_buff *skb_out;
436 	struct hsr_priv *hsr;
437 	bool restart = false;
438 	struct nlattr *na;
439 	void *pos = NULL;
440 	void *msg_head;
441 	int res;
442 
443 	if (!info)
444 		goto invalid;
445 
446 	na = info->attrs[HSR_A_IFINDEX];
447 	if (!na)
448 		goto invalid;
449 
450 	rcu_read_lock();
451 	hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
452 				       nla_get_u32(info->attrs[HSR_A_IFINDEX]));
453 	if (!hsr_dev)
454 		goto rcu_unlock;
455 	if (!is_hsr_master(hsr_dev))
456 		goto rcu_unlock;
457 
458 restart:
459 	/* Send reply */
460 	skb_out = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
461 	if (!skb_out) {
462 		res = -ENOMEM;
463 		goto fail;
464 	}
465 
466 	msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
467 			       info->snd_seq, &hsr_genl_family, 0,
468 			       HSR_C_SET_NODE_LIST);
469 	if (!msg_head) {
470 		res = -ENOMEM;
471 		goto nla_put_failure;
472 	}
473 
474 	if (!restart) {
475 		res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
476 		if (res < 0)
477 			goto nla_put_failure;
478 	}
479 
480 	hsr = netdev_priv(hsr_dev);
481 
482 	if (!pos)
483 		pos = hsr_get_next_node(hsr, NULL, addr);
484 	while (pos) {
485 		res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
486 		if (res < 0) {
487 			if (res == -EMSGSIZE) {
488 				genlmsg_end(skb_out, msg_head);
489 				genlmsg_unicast(genl_info_net(info), skb_out,
490 						info->snd_portid);
491 				restart = true;
492 				goto restart;
493 			}
494 			goto nla_put_failure;
495 		}
496 		pos = hsr_get_next_node(hsr, pos, addr);
497 	}
498 	rcu_read_unlock();
499 
500 	genlmsg_end(skb_out, msg_head);
501 	genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
502 
503 	return 0;
504 
505 rcu_unlock:
506 	rcu_read_unlock();
507 invalid:
508 	netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
509 	return 0;
510 
511 nla_put_failure:
512 	nlmsg_free(skb_out);
513 	/* Fall through */
514 
515 fail:
516 	rcu_read_unlock();
517 	return res;
518 }
519 
520 static const struct genl_small_ops hsr_ops[] = {
521 	{
522 		.cmd = HSR_C_GET_NODE_STATUS,
523 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
524 		.flags = 0,
525 		.doit = hsr_get_node_status,
526 		.dumpit = NULL,
527 	},
528 	{
529 		.cmd = HSR_C_GET_NODE_LIST,
530 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
531 		.flags = 0,
532 		.doit = hsr_get_node_list,
533 		.dumpit = NULL,
534 	},
535 };
536 
537 static struct genl_family hsr_genl_family __ro_after_init = {
538 	.hdrsize = 0,
539 	.name = "HSR",
540 	.version = 1,
541 	.maxattr = HSR_A_MAX,
542 	.policy = hsr_genl_policy,
543 	.netnsok = true,
544 	.module = THIS_MODULE,
545 	.small_ops = hsr_ops,
546 	.n_small_ops = ARRAY_SIZE(hsr_ops),
547 	.resv_start_op = HSR_C_SET_NODE_LIST + 1,
548 	.mcgrps = hsr_mcgrps,
549 	.n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
550 };
551 
552 int __init hsr_netlink_init(void)
553 {
554 	int rc;
555 
556 	rc = rtnl_link_register(&hsr_link_ops);
557 	if (rc)
558 		goto fail_rtnl_link_register;
559 
560 	rc = genl_register_family(&hsr_genl_family);
561 	if (rc)
562 		goto fail_genl_register_family;
563 
564 	hsr_debugfs_create_root();
565 	return 0;
566 
567 fail_genl_register_family:
568 	rtnl_link_unregister(&hsr_link_ops);
569 fail_rtnl_link_register:
570 
571 	return rc;
572 }
573 
574 void __exit hsr_netlink_exit(void)
575 {
576 	genl_unregister_family(&hsr_genl_family);
577 	rtnl_link_unregister(&hsr_link_ops);
578 }
579 
580 MODULE_ALIAS_RTNL_LINK("hsr");
581