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 },
28 /* Here, it seems a netdevice has already been allocated for us, and the
29 * hsr_dev_setup routine has been executed. Nice!
31 static int hsr_newlink(struct net *src_net, struct net_device *dev,
32 struct nlattr *tb[], struct nlattr *data[],
33 struct netlink_ext_ack *extack)
35 enum hsr_version proto_version;
36 unsigned char multicast_spec;
37 u8 proto = HSR_PROTOCOL_HSR;
38 struct net_device *link[2];
41 NL_SET_ERR_MSG_MOD(extack, "No slave devices specified");
44 if (!data[IFLA_HSR_SLAVE1]) {
45 NL_SET_ERR_MSG_MOD(extack, "Slave1 device not specified");
48 link[0] = __dev_get_by_index(src_net,
49 nla_get_u32(data[IFLA_HSR_SLAVE1]));
51 NL_SET_ERR_MSG_MOD(extack, "Slave1 does not exist");
54 if (!data[IFLA_HSR_SLAVE2]) {
55 NL_SET_ERR_MSG_MOD(extack, "Slave2 device not specified");
58 link[1] = __dev_get_by_index(src_net,
59 nla_get_u32(data[IFLA_HSR_SLAVE2]));
61 NL_SET_ERR_MSG_MOD(extack, "Slave2 does not exist");
65 if (link[0] == link[1]) {
66 NL_SET_ERR_MSG_MOD(extack, "Slave1 and Slave2 are same");
70 if (!data[IFLA_HSR_MULTICAST_SPEC])
73 multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
75 if (data[IFLA_HSR_PROTOCOL])
76 proto = nla_get_u8(data[IFLA_HSR_PROTOCOL]);
78 if (proto >= HSR_PROTOCOL_MAX) {
79 NL_SET_ERR_MSG_MOD(extack, "Unsupported protocol");
83 if (!data[IFLA_HSR_VERSION]) {
84 proto_version = HSR_V0;
86 if (proto == HSR_PROTOCOL_PRP) {
87 NL_SET_ERR_MSG_MOD(extack, "PRP version unsupported");
91 proto_version = nla_get_u8(data[IFLA_HSR_VERSION]);
92 if (proto_version > HSR_V1) {
93 NL_SET_ERR_MSG_MOD(extack,
94 "Only HSR version 0/1 supported");
99 if (proto == HSR_PROTOCOL_PRP)
100 proto_version = PRP_V1;
102 return hsr_dev_finalize(dev, link, multicast_spec, proto_version, extack);
105 static void hsr_dellink(struct net_device *dev, struct list_head *head)
107 struct hsr_priv *hsr = netdev_priv(dev);
109 del_timer_sync(&hsr->prune_timer);
110 del_timer_sync(&hsr->announce_timer);
112 hsr_debugfs_term(hsr);
115 hsr_del_self_node(hsr);
116 hsr_del_nodes(&hsr->node_db);
118 unregister_netdevice_queue(dev, head);
121 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
123 struct hsr_priv *hsr = netdev_priv(dev);
124 u8 proto = HSR_PROTOCOL_HSR;
125 struct hsr_port *port;
127 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
129 if (nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex))
130 goto nla_put_failure;
133 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
135 if (nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex))
136 goto nla_put_failure;
139 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
140 hsr->sup_multicast_addr) ||
141 nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
142 goto nla_put_failure;
143 if (hsr->prot_version == PRP_V1)
144 proto = HSR_PROTOCOL_PRP;
145 if (nla_put_u8(skb, IFLA_HSR_PROTOCOL, proto))
146 goto nla_put_failure;
154 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
156 .maxtype = IFLA_HSR_MAX,
157 .policy = hsr_policy,
158 .priv_size = sizeof(struct hsr_priv),
159 .setup = hsr_dev_setup,
160 .newlink = hsr_newlink,
161 .dellink = hsr_dellink,
162 .fill_info = hsr_fill_info,
165 /* attribute policy */
166 static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
167 [HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
168 [HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
169 [HSR_A_IFINDEX] = { .type = NLA_U32 },
170 [HSR_A_IF1_AGE] = { .type = NLA_U32 },
171 [HSR_A_IF2_AGE] = { .type = NLA_U32 },
172 [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
173 [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
176 static struct genl_family hsr_genl_family;
178 static const struct genl_multicast_group hsr_mcgrps[] = {
179 { .name = "hsr-network", },
182 /* This is called if for some node with MAC address addr, we only get frames
183 * over one of the slave interfaces. This would indicate an open network ring
184 * (i.e. a link has failed somewhere).
186 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
187 struct hsr_port *port)
191 struct hsr_port *master;
194 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
198 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0,
201 goto nla_put_failure;
203 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
205 goto nla_put_failure;
207 res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
209 goto nla_put_failure;
211 genlmsg_end(skb, msg_head);
212 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
221 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
222 netdev_warn(master->dev, "Could not send HSR ring error message\n");
226 /* This is called when we haven't heard from the node with MAC address addr for
227 * some time (just before the node is removed from the node table/list).
229 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
233 struct hsr_port *master;
236 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
240 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
242 goto nla_put_failure;
244 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
246 goto nla_put_failure;
248 genlmsg_end(skb, msg_head);
249 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
258 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
259 netdev_warn(master->dev, "Could not send HSR node down\n");
263 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
264 * about the status of a specific node in the network, defined by its MAC
267 * Input: hsr ifindex, node mac address
268 * Output: hsr ifindex, node mac address (copied from request),
269 * age of latest frame from node over slave 1, slave 2 [ms]
271 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
275 struct net_device *hsr_dev;
278 struct sk_buff *skb_out;
280 struct hsr_priv *hsr;
281 struct hsr_port *port;
282 unsigned char hsr_node_addr_b[ETH_ALEN];
283 int hsr_node_if1_age;
284 u16 hsr_node_if1_seq;
285 int hsr_node_if2_age;
286 u16 hsr_node_if2_seq;
293 na = info->attrs[HSR_A_IFINDEX];
296 na = info->attrs[HSR_A_NODE_ADDR];
301 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
302 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
305 if (!is_hsr_master(hsr_dev))
309 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
315 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
316 info->snd_seq, &hsr_genl_family, 0,
317 HSR_C_SET_NODE_STATUS);
320 goto nla_put_failure;
323 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
325 goto nla_put_failure;
327 hsr = netdev_priv(hsr_dev);
328 res = hsr_get_node_data(hsr,
330 nla_data(info->attrs[HSR_A_NODE_ADDR]),
338 goto nla_put_failure;
340 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
341 nla_data(info->attrs[HSR_A_NODE_ADDR]));
343 goto nla_put_failure;
345 if (addr_b_ifindex > -1) {
346 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
349 goto nla_put_failure;
351 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX,
354 goto nla_put_failure;
357 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
359 goto nla_put_failure;
360 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
362 goto nla_put_failure;
363 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
365 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
368 goto nla_put_failure;
370 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
372 goto nla_put_failure;
373 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
375 goto nla_put_failure;
376 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
378 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
381 goto nla_put_failure;
385 genlmsg_end(skb_out, msg_head);
386 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
393 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
405 /* Get a list of MacAddressA of all nodes known to this node (including self).
407 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
409 unsigned char addr[ETH_ALEN];
410 struct net_device *hsr_dev;
411 struct sk_buff *skb_out;
412 struct hsr_priv *hsr;
413 bool restart = false;
422 na = info->attrs[HSR_A_IFINDEX];
427 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
428 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
431 if (!is_hsr_master(hsr_dev))
436 skb_out = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
442 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
443 info->snd_seq, &hsr_genl_family, 0,
444 HSR_C_SET_NODE_LIST);
447 goto nla_put_failure;
451 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
453 goto nla_put_failure;
456 hsr = netdev_priv(hsr_dev);
459 pos = hsr_get_next_node(hsr, NULL, addr);
461 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
463 if (res == -EMSGSIZE) {
464 genlmsg_end(skb_out, msg_head);
465 genlmsg_unicast(genl_info_net(info), skb_out,
470 goto nla_put_failure;
472 pos = hsr_get_next_node(hsr, pos, addr);
476 genlmsg_end(skb_out, msg_head);
477 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
484 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
496 static const struct genl_small_ops hsr_ops[] = {
498 .cmd = HSR_C_GET_NODE_STATUS,
499 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
501 .doit = hsr_get_node_status,
505 .cmd = HSR_C_GET_NODE_LIST,
506 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
508 .doit = hsr_get_node_list,
513 static struct genl_family hsr_genl_family __ro_after_init = {
517 .maxattr = HSR_A_MAX,
518 .policy = hsr_genl_policy,
520 .module = THIS_MODULE,
521 .small_ops = hsr_ops,
522 .n_small_ops = ARRAY_SIZE(hsr_ops),
523 .mcgrps = hsr_mcgrps,
524 .n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
527 int __init hsr_netlink_init(void)
531 rc = rtnl_link_register(&hsr_link_ops);
533 goto fail_rtnl_link_register;
535 rc = genl_register_family(&hsr_genl_family);
537 goto fail_genl_register_family;
539 hsr_debugfs_create_root();
542 fail_genl_register_family:
543 rtnl_link_unregister(&hsr_link_ops);
544 fail_rtnl_link_register:
549 void __exit hsr_netlink_exit(void)
551 genl_unregister_family(&hsr_genl_family);
552 rtnl_link_unregister(&hsr_link_ops);
555 MODULE_ALIAS_RTNL_LINK("hsr");