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]);
69 return hsr_dev_finalize(dev, link, multicast_spec, hsr_version);
72 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
75 struct hsr_port *port;
78 hsr = netdev_priv(dev);
83 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
85 res = nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex);
91 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
93 res = nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex);
98 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
99 hsr->sup_multicast_addr) ||
100 nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
101 goto nla_put_failure;
109 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
111 .maxtype = IFLA_HSR_MAX,
112 .policy = hsr_policy,
113 .priv_size = sizeof(struct hsr_priv),
114 .setup = hsr_dev_setup,
115 .newlink = hsr_newlink,
116 .fill_info = hsr_fill_info,
119 /* attribute policy */
120 static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
121 [HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
122 [HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
123 [HSR_A_IFINDEX] = { .type = NLA_U32 },
124 [HSR_A_IF1_AGE] = { .type = NLA_U32 },
125 [HSR_A_IF2_AGE] = { .type = NLA_U32 },
126 [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
127 [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
130 static struct genl_family hsr_genl_family;
132 static const struct genl_multicast_group hsr_mcgrps[] = {
133 { .name = "hsr-network", },
136 /* This is called if for some node with MAC address addr, we only get frames
137 * over one of the slave interfaces. This would indicate an open network ring
138 * (i.e. a link has failed somewhere).
140 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
141 struct hsr_port *port)
145 struct hsr_port *master;
148 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
152 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0,
155 goto nla_put_failure;
157 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
159 goto nla_put_failure;
161 res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
163 goto nla_put_failure;
165 genlmsg_end(skb, msg_head);
166 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
175 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
176 netdev_warn(master->dev, "Could not send HSR ring error message\n");
180 /* This is called when we haven't heard from the node with MAC address addr for
181 * some time (just before the node is removed from the node table/list).
183 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
187 struct hsr_port *master;
190 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
194 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
196 goto nla_put_failure;
198 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
200 goto nla_put_failure;
202 genlmsg_end(skb, msg_head);
203 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
212 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
213 netdev_warn(master->dev, "Could not send HSR node down\n");
217 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
218 * about the status of a specific node in the network, defined by its MAC
221 * Input: hsr ifindex, node mac address
222 * Output: hsr ifindex, node mac address (copied from request),
223 * age of latest frame from node over slave 1, slave 2 [ms]
225 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
229 struct net_device *hsr_dev;
232 struct sk_buff *skb_out;
234 struct hsr_priv *hsr;
235 struct hsr_port *port;
236 unsigned char hsr_node_addr_b[ETH_ALEN];
237 int hsr_node_if1_age;
238 u16 hsr_node_if1_seq;
239 int hsr_node_if2_age;
240 u16 hsr_node_if2_seq;
247 na = info->attrs[HSR_A_IFINDEX];
250 na = info->attrs[HSR_A_NODE_ADDR];
255 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
256 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
259 if (!is_hsr_master(hsr_dev))
263 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
269 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
270 info->snd_seq, &hsr_genl_family, 0,
271 HSR_C_SET_NODE_STATUS);
274 goto nla_put_failure;
277 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
279 goto nla_put_failure;
281 hsr = netdev_priv(hsr_dev);
282 res = hsr_get_node_data(hsr,
284 nla_data(info->attrs[HSR_A_NODE_ADDR]),
292 goto nla_put_failure;
294 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
295 nla_data(info->attrs[HSR_A_NODE_ADDR]));
297 goto nla_put_failure;
299 if (addr_b_ifindex > -1) {
300 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
303 goto nla_put_failure;
305 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX,
308 goto nla_put_failure;
311 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
313 goto nla_put_failure;
314 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
316 goto nla_put_failure;
317 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
319 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
322 goto nla_put_failure;
324 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
326 goto nla_put_failure;
327 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
329 goto nla_put_failure;
330 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
332 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
335 goto nla_put_failure;
339 genlmsg_end(skb_out, msg_head);
340 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
347 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
359 /* Get a list of MacAddressA of all nodes known to this node (including self).
361 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
363 unsigned char addr[ETH_ALEN];
364 struct net_device *hsr_dev;
365 struct sk_buff *skb_out;
366 struct hsr_priv *hsr;
367 bool restart = false;
376 na = info->attrs[HSR_A_IFINDEX];
381 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
382 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
385 if (!is_hsr_master(hsr_dev))
390 skb_out = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
396 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
397 info->snd_seq, &hsr_genl_family, 0,
398 HSR_C_SET_NODE_LIST);
401 goto nla_put_failure;
405 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
407 goto nla_put_failure;
410 hsr = netdev_priv(hsr_dev);
413 pos = hsr_get_next_node(hsr, NULL, addr);
415 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
417 if (res == -EMSGSIZE) {
418 genlmsg_end(skb_out, msg_head);
419 genlmsg_unicast(genl_info_net(info), skb_out,
424 goto nla_put_failure;
426 pos = hsr_get_next_node(hsr, pos, addr);
430 genlmsg_end(skb_out, msg_head);
431 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
438 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
450 static const struct genl_ops hsr_ops[] = {
452 .cmd = HSR_C_GET_NODE_STATUS,
453 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
455 .doit = hsr_get_node_status,
459 .cmd = HSR_C_GET_NODE_LIST,
460 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
462 .doit = hsr_get_node_list,
467 static struct genl_family hsr_genl_family __ro_after_init = {
471 .maxattr = HSR_A_MAX,
472 .policy = hsr_genl_policy,
474 .module = THIS_MODULE,
476 .n_ops = ARRAY_SIZE(hsr_ops),
477 .mcgrps = hsr_mcgrps,
478 .n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
481 int __init hsr_netlink_init(void)
485 rc = rtnl_link_register(&hsr_link_ops);
487 goto fail_rtnl_link_register;
489 rc = genl_register_family(&hsr_genl_family);
491 goto fail_genl_register_family;
493 hsr_debugfs_create_root();
496 fail_genl_register_family:
497 rtnl_link_unregister(&hsr_link_ops);
498 fail_rtnl_link_register:
503 void __exit hsr_netlink_exit(void)
505 genl_unregister_family(&hsr_genl_family);
506 rtnl_link_unregister(&hsr_link_ops);
509 MODULE_ALIAS_RTNL_LINK("hsr");