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