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 NL_SET_ERR_MSG_MOD(extack, "No slave devices specified");
41 if (!data[IFLA_HSR_SLAVE1]) {
42 NL_SET_ERR_MSG_MOD(extack, "Slave1 device not specified");
45 link[0] = __dev_get_by_index(src_net,
46 nla_get_u32(data[IFLA_HSR_SLAVE1]));
48 NL_SET_ERR_MSG_MOD(extack, "Slave1 does not exist");
51 if (!data[IFLA_HSR_SLAVE2]) {
52 NL_SET_ERR_MSG_MOD(extack, "Slave2 device not specified");
55 link[1] = __dev_get_by_index(src_net,
56 nla_get_u32(data[IFLA_HSR_SLAVE2]));
58 NL_SET_ERR_MSG_MOD(extack, "Slave2 does not exist");
62 if (link[0] == link[1]) {
63 NL_SET_ERR_MSG_MOD(extack, "Slave1 and Slave2 are same");
67 if (!data[IFLA_HSR_MULTICAST_SPEC])
70 multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
72 if (!data[IFLA_HSR_VERSION])
75 hsr_version = nla_get_u8(data[IFLA_HSR_VERSION]);
77 return hsr_dev_finalize(dev, link, multicast_spec, hsr_version, extack);
80 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
82 struct hsr_priv *hsr = netdev_priv(dev);
83 struct hsr_port *port;
85 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
87 if (nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex))
91 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
93 if (nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex))
97 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
98 hsr->sup_multicast_addr) ||
99 nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
100 goto nla_put_failure;
108 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
110 .maxtype = IFLA_HSR_MAX,
111 .policy = hsr_policy,
112 .priv_size = sizeof(struct hsr_priv),
113 .setup = hsr_dev_setup,
114 .newlink = hsr_newlink,
115 .fill_info = hsr_fill_info,
118 /* attribute policy */
119 static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
120 [HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
121 [HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
122 [HSR_A_IFINDEX] = { .type = NLA_U32 },
123 [HSR_A_IF1_AGE] = { .type = NLA_U32 },
124 [HSR_A_IF2_AGE] = { .type = NLA_U32 },
125 [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
126 [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
129 static struct genl_family hsr_genl_family;
131 static const struct genl_multicast_group hsr_mcgrps[] = {
132 { .name = "hsr-network", },
135 /* This is called if for some node with MAC address addr, we only get frames
136 * over one of the slave interfaces. This would indicate an open network ring
137 * (i.e. a link has failed somewhere).
139 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
140 struct hsr_port *port)
144 struct hsr_port *master;
147 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
151 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0,
154 goto nla_put_failure;
156 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
158 goto nla_put_failure;
160 res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
162 goto nla_put_failure;
164 genlmsg_end(skb, msg_head);
165 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
174 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
175 netdev_warn(master->dev, "Could not send HSR ring error message\n");
179 /* This is called when we haven't heard from the node with MAC address addr for
180 * some time (just before the node is removed from the node table/list).
182 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
186 struct hsr_port *master;
189 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
193 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
195 goto nla_put_failure;
197 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
199 goto nla_put_failure;
201 genlmsg_end(skb, msg_head);
202 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
211 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
212 netdev_warn(master->dev, "Could not send HSR node down\n");
216 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
217 * about the status of a specific node in the network, defined by its MAC
220 * Input: hsr ifindex, node mac address
221 * Output: hsr ifindex, node mac address (copied from request),
222 * age of latest frame from node over slave 1, slave 2 [ms]
224 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
228 struct net_device *hsr_dev;
231 struct sk_buff *skb_out;
233 struct hsr_priv *hsr;
234 struct hsr_port *port;
235 unsigned char hsr_node_addr_b[ETH_ALEN];
236 int hsr_node_if1_age;
237 u16 hsr_node_if1_seq;
238 int hsr_node_if2_age;
239 u16 hsr_node_if2_seq;
246 na = info->attrs[HSR_A_IFINDEX];
249 na = info->attrs[HSR_A_NODE_ADDR];
254 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
255 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
258 if (!is_hsr_master(hsr_dev))
262 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
268 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
269 info->snd_seq, &hsr_genl_family, 0,
270 HSR_C_SET_NODE_STATUS);
273 goto nla_put_failure;
276 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
278 goto nla_put_failure;
280 hsr = netdev_priv(hsr_dev);
281 res = hsr_get_node_data(hsr,
283 nla_data(info->attrs[HSR_A_NODE_ADDR]),
291 goto nla_put_failure;
293 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
294 nla_data(info->attrs[HSR_A_NODE_ADDR]));
296 goto nla_put_failure;
298 if (addr_b_ifindex > -1) {
299 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
302 goto nla_put_failure;
304 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX,
307 goto nla_put_failure;
310 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
312 goto nla_put_failure;
313 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
315 goto nla_put_failure;
316 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
318 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
321 goto nla_put_failure;
323 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
325 goto nla_put_failure;
326 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
328 goto nla_put_failure;
329 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
331 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
334 goto nla_put_failure;
338 genlmsg_end(skb_out, msg_head);
339 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
346 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
358 /* Get a list of MacAddressA of all nodes known to this node (including self).
360 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
362 unsigned char addr[ETH_ALEN];
363 struct net_device *hsr_dev;
364 struct sk_buff *skb_out;
365 struct hsr_priv *hsr;
366 bool restart = false;
375 na = info->attrs[HSR_A_IFINDEX];
380 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
381 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
384 if (!is_hsr_master(hsr_dev))
389 skb_out = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
395 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
396 info->snd_seq, &hsr_genl_family, 0,
397 HSR_C_SET_NODE_LIST);
400 goto nla_put_failure;
404 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
406 goto nla_put_failure;
409 hsr = netdev_priv(hsr_dev);
412 pos = hsr_get_next_node(hsr, NULL, addr);
414 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
416 if (res == -EMSGSIZE) {
417 genlmsg_end(skb_out, msg_head);
418 genlmsg_unicast(genl_info_net(info), skb_out,
423 goto nla_put_failure;
425 pos = hsr_get_next_node(hsr, pos, addr);
429 genlmsg_end(skb_out, msg_head);
430 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
437 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
449 static const struct genl_ops hsr_ops[] = {
451 .cmd = HSR_C_GET_NODE_STATUS,
452 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
454 .doit = hsr_get_node_status,
458 .cmd = HSR_C_GET_NODE_LIST,
459 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
461 .doit = hsr_get_node_list,
466 static struct genl_family hsr_genl_family __ro_after_init = {
470 .maxattr = HSR_A_MAX,
471 .policy = hsr_genl_policy,
473 .module = THIS_MODULE,
475 .n_ops = ARRAY_SIZE(hsr_ops),
476 .mcgrps = hsr_mcgrps,
477 .n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
480 int __init hsr_netlink_init(void)
484 rc = rtnl_link_register(&hsr_link_ops);
486 goto fail_rtnl_link_register;
488 rc = genl_register_family(&hsr_genl_family);
490 goto fail_genl_register_family;
492 hsr_debugfs_create_root();
495 fail_genl_register_family:
496 rtnl_link_unregister(&hsr_link_ops);
497 fail_rtnl_link_register:
502 void __exit hsr_netlink_exit(void)
504 genl_unregister_family(&hsr_genl_family);
505 rtnl_link_unregister(&hsr_link_ops);
508 MODULE_ALIAS_RTNL_LINK("hsr");