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