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.
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 },
27 /* Here, it seems a netdevice has already been allocated for us, and the
28 * hsr_dev_setup routine has been executed. Nice!
30 static int hsr_newlink(struct net *src_net, struct net_device *dev,
31 struct nlattr *tb[], struct nlattr *data[],
32 struct netlink_ext_ack *extack)
34 struct net_device *link[2];
35 unsigned char multicast_spec, hsr_version;
38 netdev_info(dev, "HSR: No slave devices specified\n");
41 if (!data[IFLA_HSR_SLAVE1]) {
42 netdev_info(dev, "HSR: Slave1 device not specified\n");
45 link[0] = __dev_get_by_index(src_net,
46 nla_get_u32(data[IFLA_HSR_SLAVE1]));
47 if (!data[IFLA_HSR_SLAVE2]) {
48 netdev_info(dev, "HSR: Slave2 device not specified\n");
51 link[1] = __dev_get_by_index(src_net,
52 nla_get_u32(data[IFLA_HSR_SLAVE2]));
54 if (!link[0] || !link[1])
56 if (link[0] == link[1])
59 if (!data[IFLA_HSR_MULTICAST_SPEC])
62 multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
64 if (!data[IFLA_HSR_VERSION]) {
67 hsr_version = nla_get_u8(data[IFLA_HSR_VERSION]);
68 if (hsr_version > 1) {
69 NL_SET_ERR_MSG_MOD(extack,
70 "Only versions 0..1 are supported");
75 return hsr_dev_finalize(dev, link, multicast_spec, hsr_version);
78 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
81 struct hsr_port *port;
84 hsr = netdev_priv(dev);
89 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
91 res = nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex);
97 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
99 res = nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex);
102 goto nla_put_failure;
104 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
105 hsr->sup_multicast_addr) ||
106 nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
107 goto nla_put_failure;
115 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
117 .maxtype = IFLA_HSR_MAX,
118 .policy = hsr_policy,
119 .priv_size = sizeof(struct hsr_priv),
120 .setup = hsr_dev_setup,
121 .newlink = hsr_newlink,
122 .fill_info = hsr_fill_info,
125 /* attribute policy */
126 static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
127 [HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
128 [HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
129 [HSR_A_IFINDEX] = { .type = NLA_U32 },
130 [HSR_A_IF1_AGE] = { .type = NLA_U32 },
131 [HSR_A_IF2_AGE] = { .type = NLA_U32 },
132 [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
133 [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
136 static struct genl_family hsr_genl_family;
138 static const struct genl_multicast_group hsr_mcgrps[] = {
139 { .name = "hsr-network", },
142 /* This is called if for some node with MAC address addr, we only get frames
143 * over one of the slave interfaces. This would indicate an open network ring
144 * (i.e. a link has failed somewhere).
146 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
147 struct hsr_port *port)
151 struct hsr_port *master;
154 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
158 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0,
161 goto nla_put_failure;
163 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
165 goto nla_put_failure;
167 res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
169 goto nla_put_failure;
171 genlmsg_end(skb, msg_head);
172 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
181 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
182 netdev_warn(master->dev, "Could not send HSR ring error message\n");
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).
189 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
193 struct hsr_port *master;
196 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
200 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
202 goto nla_put_failure;
204 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
206 goto nla_put_failure;
208 genlmsg_end(skb, msg_head);
209 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
218 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
219 netdev_warn(master->dev, "Could not send HSR node down\n");
223 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
224 * about the status of a specific node in the network, defined by its MAC
227 * Input: hsr ifindex, node mac address
228 * Output: hsr ifindex, node mac address (copied from request),
229 * age of latest frame from node over slave 1, slave 2 [ms]
231 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
235 struct net_device *hsr_dev;
238 struct sk_buff *skb_out;
240 struct hsr_priv *hsr;
241 struct hsr_port *port;
242 unsigned char hsr_node_addr_b[ETH_ALEN];
243 int hsr_node_if1_age;
244 u16 hsr_node_if1_seq;
245 int hsr_node_if2_age;
246 u16 hsr_node_if2_seq;
253 na = info->attrs[HSR_A_IFINDEX];
256 na = info->attrs[HSR_A_NODE_ADDR];
261 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
262 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
265 if (!is_hsr_master(hsr_dev))
269 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
275 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
276 info->snd_seq, &hsr_genl_family, 0,
277 HSR_C_SET_NODE_STATUS);
280 goto nla_put_failure;
283 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
285 goto nla_put_failure;
287 hsr = netdev_priv(hsr_dev);
288 res = hsr_get_node_data(hsr,
290 nla_data(info->attrs[HSR_A_NODE_ADDR]),
298 goto nla_put_failure;
300 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
301 nla_data(info->attrs[HSR_A_NODE_ADDR]));
303 goto nla_put_failure;
305 if (addr_b_ifindex > -1) {
306 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
309 goto nla_put_failure;
311 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX,
314 goto nla_put_failure;
317 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
319 goto nla_put_failure;
320 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
322 goto nla_put_failure;
323 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
325 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
328 goto nla_put_failure;
330 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
332 goto nla_put_failure;
333 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
335 goto nla_put_failure;
336 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
338 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
341 goto nla_put_failure;
345 genlmsg_end(skb_out, msg_head);
346 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
353 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
365 /* Get a list of MacAddressA of all nodes known to this node (including self).
367 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
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;
382 na = info->attrs[HSR_A_IFINDEX];
387 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
388 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
391 if (!is_hsr_master(hsr_dev))
396 skb_out = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
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);
407 goto nla_put_failure;
411 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
413 goto nla_put_failure;
416 hsr = netdev_priv(hsr_dev);
419 pos = hsr_get_next_node(hsr, NULL, addr);
421 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
423 if (res == -EMSGSIZE) {
424 genlmsg_end(skb_out, msg_head);
425 genlmsg_unicast(genl_info_net(info), skb_out,
430 goto nla_put_failure;
432 pos = hsr_get_next_node(hsr, pos, addr);
436 genlmsg_end(skb_out, msg_head);
437 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
444 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
456 static const struct genl_ops hsr_ops[] = {
458 .cmd = HSR_C_GET_NODE_STATUS,
459 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
461 .doit = hsr_get_node_status,
465 .cmd = HSR_C_GET_NODE_LIST,
466 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
468 .doit = hsr_get_node_list,
473 static struct genl_family hsr_genl_family __ro_after_init = {
477 .maxattr = HSR_A_MAX,
478 .policy = hsr_genl_policy,
480 .module = THIS_MODULE,
482 .n_ops = ARRAY_SIZE(hsr_ops),
483 .mcgrps = hsr_mcgrps,
484 .n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
487 int __init hsr_netlink_init(void)
491 rc = rtnl_link_register(&hsr_link_ops);
493 goto fail_rtnl_link_register;
495 rc = genl_register_family(&hsr_genl_family);
497 goto fail_genl_register_family;
499 hsr_debugfs_create_root();
502 fail_genl_register_family:
503 rtnl_link_unregister(&hsr_link_ops);
504 fail_rtnl_link_register:
509 void __exit hsr_netlink_exit(void)
511 genl_unregister_family(&hsr_genl_family);
512 rtnl_link_unregister(&hsr_link_ops);
515 MODULE_ALIAS_RTNL_LINK("hsr");