1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2011-2014 Autronica Fire and Security AS
5 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
7 * Routines for handling Netlink messages for HSR and PRP.
10 #include "hsr_netlink.h"
11 #include <linux/kernel.h>
12 #include <net/rtnetlink.h>
13 #include <net/genetlink.h>
15 #include "hsr_device.h"
16 #include "hsr_framereg.h"
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 },
29 /* Here, it seems a netdevice has already been allocated for us, and the
30 * hsr_dev_setup routine has been executed. Nice!
32 static int hsr_newlink(struct net_device *dev,
33 struct rtnl_newlink_params *params,
34 struct netlink_ext_ack *extack)
36 struct net *link_net = rtnl_newlink_link_net(params);
37 struct nlattr **data = params->data;
38 enum hsr_version proto_version;
39 unsigned char multicast_spec;
40 u8 proto = HSR_PROTOCOL_HSR;
42 struct net_device *link[2], *interlink = NULL;
44 NL_SET_ERR_MSG_MOD(extack, "No slave devices specified");
47 if (!data[IFLA_HSR_SLAVE1]) {
48 NL_SET_ERR_MSG_MOD(extack, "Slave1 device not specified");
51 link[0] = __dev_get_by_index(link_net,
52 nla_get_u32(data[IFLA_HSR_SLAVE1]));
54 NL_SET_ERR_MSG_MOD(extack, "Slave1 does not exist");
57 if (!data[IFLA_HSR_SLAVE2]) {
58 NL_SET_ERR_MSG_MOD(extack, "Slave2 device not specified");
61 link[1] = __dev_get_by_index(link_net,
62 nla_get_u32(data[IFLA_HSR_SLAVE2]));
64 NL_SET_ERR_MSG_MOD(extack, "Slave2 does not exist");
68 if (link[0] == link[1]) {
69 NL_SET_ERR_MSG_MOD(extack, "Slave1 and Slave2 are same");
73 if (data[IFLA_HSR_INTERLINK])
74 interlink = __dev_get_by_index(link_net,
75 nla_get_u32(data[IFLA_HSR_INTERLINK]));
77 if (interlink && interlink == link[0]) {
78 NL_SET_ERR_MSG_MOD(extack, "Interlink and Slave1 are the same");
82 if (interlink && interlink == link[1]) {
83 NL_SET_ERR_MSG_MOD(extack, "Interlink and Slave2 are the same");
87 multicast_spec = nla_get_u8_default(data[IFLA_HSR_MULTICAST_SPEC], 0);
89 if (data[IFLA_HSR_PROTOCOL])
90 proto = nla_get_u8(data[IFLA_HSR_PROTOCOL]);
92 if (proto >= HSR_PROTOCOL_MAX) {
93 NL_SET_ERR_MSG_MOD(extack, "Unsupported protocol");
97 if (!data[IFLA_HSR_VERSION]) {
98 proto_version = HSR_V0;
100 if (proto == HSR_PROTOCOL_PRP) {
101 NL_SET_ERR_MSG_MOD(extack, "PRP version unsupported");
105 proto_version = nla_get_u8(data[IFLA_HSR_VERSION]);
106 if (proto_version > HSR_V1) {
107 NL_SET_ERR_MSG_MOD(extack,
108 "Only HSR version 0/1 supported");
113 if (proto == HSR_PROTOCOL_PRP) {
114 proto_version = PRP_V1;
116 NL_SET_ERR_MSG_MOD(extack,
117 "Interlink only works with HSR");
122 return hsr_dev_finalize(dev, link, interlink, multicast_spec,
123 proto_version, extack);
126 static void hsr_dellink(struct net_device *dev, struct list_head *head)
128 struct hsr_priv *hsr = netdev_priv(dev);
130 timer_delete_sync(&hsr->prune_timer);
131 timer_delete_sync(&hsr->prune_proxy_timer);
132 timer_delete_sync(&hsr->announce_timer);
133 timer_delete_sync(&hsr->announce_proxy_timer);
135 hsr_debugfs_term(hsr);
138 hsr_del_self_node(hsr);
139 hsr_del_nodes(&hsr->node_db);
140 hsr_del_nodes(&hsr->proxy_node_db);
142 unregister_netdevice_queue(dev, head);
145 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
147 struct hsr_priv *hsr = netdev_priv(dev);
148 u8 proto = HSR_PROTOCOL_HSR;
149 struct hsr_port *port;
151 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
153 if (nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex))
154 goto nla_put_failure;
157 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
159 if (nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex))
160 goto nla_put_failure;
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;
178 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
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,
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 },
200 static struct genl_family hsr_genl_family;
202 static const struct genl_multicast_group hsr_mcgrps[] = {
203 { .name = "hsr-network", },
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).
210 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
211 struct hsr_port *port)
215 struct hsr_port *master;
218 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
222 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0,
225 goto nla_put_failure;
227 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
229 goto nla_put_failure;
231 res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
233 goto nla_put_failure;
235 genlmsg_end(skb, msg_head);
236 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
245 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
246 netdev_warn(master->dev, "Could not send HSR ring error message\n");
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).
253 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
257 struct hsr_port *master;
260 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
264 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
266 goto nla_put_failure;
268 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
270 goto nla_put_failure;
272 genlmsg_end(skb, msg_head);
273 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
282 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
283 netdev_warn(master->dev, "Could not send HSR node down\n");
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
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]
295 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
299 struct net_device *hsr_dev;
302 struct sk_buff *skb_out;
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;
317 na = info->attrs[HSR_A_IFINDEX];
320 na = info->attrs[HSR_A_NODE_ADDR];
325 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
326 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
329 if (!is_hsr_master(hsr_dev))
333 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
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);
344 goto nla_put_failure;
347 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
349 goto nla_put_failure;
351 hsr = netdev_priv(hsr_dev);
352 res = hsr_get_node_data(hsr,
354 nla_data(info->attrs[HSR_A_NODE_ADDR]),
362 goto nla_put_failure;
364 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
365 nla_data(info->attrs[HSR_A_NODE_ADDR]));
367 goto nla_put_failure;
369 if (addr_b_ifindex > -1) {
370 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
373 goto nla_put_failure;
375 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX,
378 goto nla_put_failure;
381 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
383 goto nla_put_failure;
384 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
386 goto nla_put_failure;
387 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
389 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
392 goto nla_put_failure;
394 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
396 goto nla_put_failure;
397 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
399 goto nla_put_failure;
400 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
402 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
405 goto nla_put_failure;
409 genlmsg_end(skb_out, msg_head);
410 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
417 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
429 /* Get a list of MacAddressA of all nodes known to this node (including self).
431 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
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;
446 na = info->attrs[HSR_A_IFINDEX];
451 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
452 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
455 if (!is_hsr_master(hsr_dev))
460 skb_out = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
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);
471 goto nla_put_failure;
475 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
477 goto nla_put_failure;
480 hsr = netdev_priv(hsr_dev);
483 pos = hsr_get_next_node(hsr, NULL, addr);
485 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
487 if (res == -EMSGSIZE) {
488 genlmsg_end(skb_out, msg_head);
489 genlmsg_unicast(genl_info_net(info), skb_out,
494 goto nla_put_failure;
496 pos = hsr_get_next_node(hsr, pos, addr);
500 genlmsg_end(skb_out, msg_head);
501 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
508 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
520 static const struct genl_small_ops hsr_ops[] = {
522 .cmd = HSR_C_GET_NODE_STATUS,
523 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
525 .doit = hsr_get_node_status,
529 .cmd = HSR_C_GET_NODE_LIST,
530 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
532 .doit = hsr_get_node_list,
537 static struct genl_family hsr_genl_family __ro_after_init = {
541 .maxattr = HSR_A_MAX,
542 .policy = hsr_genl_policy,
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),
552 int __init hsr_netlink_init(void)
556 rc = rtnl_link_register(&hsr_link_ops);
558 goto fail_rtnl_link_register;
560 rc = genl_register_family(&hsr_genl_family);
562 goto fail_genl_register_family;
564 hsr_debugfs_create_root();
567 fail_genl_register_family:
568 rtnl_link_unregister(&hsr_link_ops);
569 fail_rtnl_link_register:
574 void __exit hsr_netlink_exit(void)
576 genl_unregister_family(&hsr_genl_family);
577 rtnl_link_unregister(&hsr_link_ops);
580 MODULE_ALIAS_RTNL_LINK("hsr");