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]);
76 if (hsr_version > 1) {
77 NL_SET_ERR_MSG_MOD(extack,
78 "Only versions 0..1 are supported");
83 return hsr_dev_finalize(dev, link, multicast_spec, hsr_version, extack);
86 static void hsr_dellink(struct net_device *dev, struct list_head *head)
88 struct hsr_priv *hsr = netdev_priv(dev);
90 del_timer_sync(&hsr->prune_timer);
91 del_timer_sync(&hsr->announce_timer);
93 hsr_debugfs_term(hsr);
96 hsr_del_self_node(hsr);
97 hsr_del_nodes(&hsr->node_db);
99 unregister_netdevice_queue(dev, head);
102 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
104 struct hsr_priv *hsr = netdev_priv(dev);
105 struct hsr_port *port;
107 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
109 if (nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex))
110 goto nla_put_failure;
113 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
115 if (nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex))
116 goto nla_put_failure;
119 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
120 hsr->sup_multicast_addr) ||
121 nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
122 goto nla_put_failure;
130 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
132 .maxtype = IFLA_HSR_MAX,
133 .policy = hsr_policy,
134 .priv_size = sizeof(struct hsr_priv),
135 .setup = hsr_dev_setup,
136 .newlink = hsr_newlink,
137 .dellink = hsr_dellink,
138 .fill_info = hsr_fill_info,
141 /* attribute policy */
142 static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
143 [HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
144 [HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
145 [HSR_A_IFINDEX] = { .type = NLA_U32 },
146 [HSR_A_IF1_AGE] = { .type = NLA_U32 },
147 [HSR_A_IF2_AGE] = { .type = NLA_U32 },
148 [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
149 [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
152 static struct genl_family hsr_genl_family;
154 static const struct genl_multicast_group hsr_mcgrps[] = {
155 { .name = "hsr-network", },
158 /* This is called if for some node with MAC address addr, we only get frames
159 * over one of the slave interfaces. This would indicate an open network ring
160 * (i.e. a link has failed somewhere).
162 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
163 struct hsr_port *port)
167 struct hsr_port *master;
170 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
174 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0,
177 goto nla_put_failure;
179 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
181 goto nla_put_failure;
183 res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
185 goto nla_put_failure;
187 genlmsg_end(skb, msg_head);
188 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
197 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
198 netdev_warn(master->dev, "Could not send HSR ring error message\n");
202 /* This is called when we haven't heard from the node with MAC address addr for
203 * some time (just before the node is removed from the node table/list).
205 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
209 struct hsr_port *master;
212 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
216 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
218 goto nla_put_failure;
220 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
222 goto nla_put_failure;
224 genlmsg_end(skb, msg_head);
225 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
234 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
235 netdev_warn(master->dev, "Could not send HSR node down\n");
239 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
240 * about the status of a specific node in the network, defined by its MAC
243 * Input: hsr ifindex, node mac address
244 * Output: hsr ifindex, node mac address (copied from request),
245 * age of latest frame from node over slave 1, slave 2 [ms]
247 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
251 struct net_device *hsr_dev;
254 struct sk_buff *skb_out;
256 struct hsr_priv *hsr;
257 struct hsr_port *port;
258 unsigned char hsr_node_addr_b[ETH_ALEN];
259 int hsr_node_if1_age;
260 u16 hsr_node_if1_seq;
261 int hsr_node_if2_age;
262 u16 hsr_node_if2_seq;
269 na = info->attrs[HSR_A_IFINDEX];
272 na = info->attrs[HSR_A_NODE_ADDR];
277 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
278 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
281 if (!is_hsr_master(hsr_dev))
285 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
291 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
292 info->snd_seq, &hsr_genl_family, 0,
293 HSR_C_SET_NODE_STATUS);
296 goto nla_put_failure;
299 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
301 goto nla_put_failure;
303 hsr = netdev_priv(hsr_dev);
304 res = hsr_get_node_data(hsr,
306 nla_data(info->attrs[HSR_A_NODE_ADDR]),
314 goto nla_put_failure;
316 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
317 nla_data(info->attrs[HSR_A_NODE_ADDR]));
319 goto nla_put_failure;
321 if (addr_b_ifindex > -1) {
322 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
325 goto nla_put_failure;
327 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX,
330 goto nla_put_failure;
333 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
335 goto nla_put_failure;
336 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
338 goto nla_put_failure;
339 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
341 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
344 goto nla_put_failure;
346 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
348 goto nla_put_failure;
349 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
351 goto nla_put_failure;
352 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
354 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
357 goto nla_put_failure;
361 genlmsg_end(skb_out, msg_head);
362 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
369 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
381 /* Get a list of MacAddressA of all nodes known to this node (including self).
383 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
385 unsigned char addr[ETH_ALEN];
386 struct net_device *hsr_dev;
387 struct sk_buff *skb_out;
388 struct hsr_priv *hsr;
389 bool restart = false;
398 na = info->attrs[HSR_A_IFINDEX];
403 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
404 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
407 if (!is_hsr_master(hsr_dev))
412 skb_out = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
418 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
419 info->snd_seq, &hsr_genl_family, 0,
420 HSR_C_SET_NODE_LIST);
423 goto nla_put_failure;
427 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
429 goto nla_put_failure;
432 hsr = netdev_priv(hsr_dev);
435 pos = hsr_get_next_node(hsr, NULL, addr);
437 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
439 if (res == -EMSGSIZE) {
440 genlmsg_end(skb_out, msg_head);
441 genlmsg_unicast(genl_info_net(info), skb_out,
446 goto nla_put_failure;
448 pos = hsr_get_next_node(hsr, pos, addr);
452 genlmsg_end(skb_out, msg_head);
453 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
460 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
472 static const struct genl_ops hsr_ops[] = {
474 .cmd = HSR_C_GET_NODE_STATUS,
475 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
477 .doit = hsr_get_node_status,
481 .cmd = HSR_C_GET_NODE_LIST,
482 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
484 .doit = hsr_get_node_list,
489 static struct genl_family hsr_genl_family __ro_after_init = {
493 .maxattr = HSR_A_MAX,
494 .policy = hsr_genl_policy,
496 .module = THIS_MODULE,
498 .n_ops = ARRAY_SIZE(hsr_ops),
499 .mcgrps = hsr_mcgrps,
500 .n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
503 int __init hsr_netlink_init(void)
507 rc = rtnl_link_register(&hsr_link_ops);
509 goto fail_rtnl_link_register;
511 rc = genl_register_family(&hsr_genl_family);
513 goto fail_genl_register_family;
515 hsr_debugfs_create_root();
518 fail_genl_register_family:
519 rtnl_link_unregister(&hsr_link_ops);
520 fail_rtnl_link_register:
525 void __exit hsr_netlink_exit(void)
527 genl_unregister_family(&hsr_genl_family);
528 rtnl_link_unregister(&hsr_link_ops);
531 MODULE_ALIAS_RTNL_LINK("hsr");