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