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);
110 del_timer_sync(&hsr->prune_timer);
111 del_timer_sync(&hsr->announce_timer);
113 hsr_debugfs_term(hsr);
116 hsr_del_self_node(hsr);
117 for (i = 0; i < hsr->hash_buckets; i++)
118 hsr_del_nodes(&hsr->node_db[i]);
120 unregister_netdevice_queue(dev, head);
123 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
125 struct hsr_priv *hsr = netdev_priv(dev);
126 u8 proto = HSR_PROTOCOL_HSR;
127 struct hsr_port *port;
129 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
131 if (nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex))
132 goto nla_put_failure;
135 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
137 if (nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex))
138 goto nla_put_failure;
141 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
142 hsr->sup_multicast_addr) ||
143 nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
144 goto nla_put_failure;
145 if (hsr->prot_version == PRP_V1)
146 proto = HSR_PROTOCOL_PRP;
147 if (nla_put_u8(skb, IFLA_HSR_PROTOCOL, proto))
148 goto nla_put_failure;
156 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
158 .maxtype = IFLA_HSR_MAX,
159 .policy = hsr_policy,
160 .priv_size = sizeof(struct hsr_priv),
161 .setup = hsr_dev_setup,
162 .newlink = hsr_newlink,
163 .dellink = hsr_dellink,
164 .fill_info = hsr_fill_info,
167 /* attribute policy */
168 static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
169 [HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
170 [HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
171 [HSR_A_IFINDEX] = { .type = NLA_U32 },
172 [HSR_A_IF1_AGE] = { .type = NLA_U32 },
173 [HSR_A_IF2_AGE] = { .type = NLA_U32 },
174 [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
175 [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
178 static struct genl_family hsr_genl_family;
180 static const struct genl_multicast_group hsr_mcgrps[] = {
181 { .name = "hsr-network", },
184 /* This is called if for some node with MAC address addr, we only get frames
185 * over one of the slave interfaces. This would indicate an open network ring
186 * (i.e. a link has failed somewhere).
188 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
189 struct hsr_port *port)
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,
203 goto nla_put_failure;
205 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
207 goto nla_put_failure;
209 res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
211 goto nla_put_failure;
213 genlmsg_end(skb, msg_head);
214 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
223 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
224 netdev_warn(master->dev, "Could not send HSR ring error message\n");
228 /* This is called when we haven't heard from the node with MAC address addr for
229 * some time (just before the node is removed from the node table/list).
231 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
235 struct hsr_port *master;
238 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
242 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
244 goto nla_put_failure;
246 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
248 goto nla_put_failure;
250 genlmsg_end(skb, msg_head);
251 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
260 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
261 netdev_warn(master->dev, "Could not send HSR node down\n");
265 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
266 * about the status of a specific node in the network, defined by its MAC
269 * Input: hsr ifindex, node mac address
270 * Output: hsr ifindex, node mac address (copied from request),
271 * age of latest frame from node over slave 1, slave 2 [ms]
273 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
277 struct net_device *hsr_dev;
280 struct sk_buff *skb_out;
282 struct hsr_priv *hsr;
283 struct hsr_port *port;
284 unsigned char hsr_node_addr_b[ETH_ALEN];
285 int hsr_node_if1_age;
286 u16 hsr_node_if1_seq;
287 int hsr_node_if2_age;
288 u16 hsr_node_if2_seq;
295 na = info->attrs[HSR_A_IFINDEX];
298 na = info->attrs[HSR_A_NODE_ADDR];
303 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
304 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
307 if (!is_hsr_master(hsr_dev))
311 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
317 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
318 info->snd_seq, &hsr_genl_family, 0,
319 HSR_C_SET_NODE_STATUS);
322 goto nla_put_failure;
325 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
327 goto nla_put_failure;
329 hsr = netdev_priv(hsr_dev);
330 res = hsr_get_node_data(hsr,
332 nla_data(info->attrs[HSR_A_NODE_ADDR]),
340 goto nla_put_failure;
342 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
343 nla_data(info->attrs[HSR_A_NODE_ADDR]));
345 goto nla_put_failure;
347 if (addr_b_ifindex > -1) {
348 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
351 goto nla_put_failure;
353 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX,
356 goto nla_put_failure;
359 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
361 goto nla_put_failure;
362 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
364 goto nla_put_failure;
365 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
367 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
370 goto nla_put_failure;
372 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
374 goto nla_put_failure;
375 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
377 goto nla_put_failure;
378 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
380 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
383 goto nla_put_failure;
387 genlmsg_end(skb_out, msg_head);
388 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
395 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
407 /* Get a list of MacAddressA of all nodes known to this node (including self).
409 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
411 unsigned char addr[ETH_ALEN];
412 struct net_device *hsr_dev;
413 struct sk_buff *skb_out;
414 struct hsr_priv *hsr;
415 bool restart = false;
424 na = info->attrs[HSR_A_IFINDEX];
429 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
430 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
433 if (!is_hsr_master(hsr_dev))
438 skb_out = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
444 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
445 info->snd_seq, &hsr_genl_family, 0,
446 HSR_C_SET_NODE_LIST);
449 goto nla_put_failure;
453 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
455 goto nla_put_failure;
458 hsr = netdev_priv(hsr_dev);
461 pos = hsr_get_next_node(hsr, NULL, addr);
463 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
465 if (res == -EMSGSIZE) {
466 genlmsg_end(skb_out, msg_head);
467 genlmsg_unicast(genl_info_net(info), skb_out,
472 goto nla_put_failure;
474 pos = hsr_get_next_node(hsr, pos, addr);
478 genlmsg_end(skb_out, msg_head);
479 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
486 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
498 static const struct genl_small_ops hsr_ops[] = {
500 .cmd = HSR_C_GET_NODE_STATUS,
501 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
503 .doit = hsr_get_node_status,
507 .cmd = HSR_C_GET_NODE_LIST,
508 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
510 .doit = hsr_get_node_list,
515 static struct genl_family hsr_genl_family __ro_after_init = {
519 .maxattr = HSR_A_MAX,
520 .policy = hsr_genl_policy,
522 .module = THIS_MODULE,
523 .small_ops = hsr_ops,
524 .n_small_ops = ARRAY_SIZE(hsr_ops),
525 .resv_start_op = HSR_C_SET_NODE_LIST + 1,
526 .mcgrps = hsr_mcgrps,
527 .n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
530 int __init hsr_netlink_init(void)
534 rc = rtnl_link_register(&hsr_link_ops);
536 goto fail_rtnl_link_register;
538 rc = genl_register_family(&hsr_genl_family);
540 goto fail_genl_register_family;
542 hsr_debugfs_create_root();
545 fail_genl_register_family:
546 rtnl_link_unregister(&hsr_link_ops);
547 fail_rtnl_link_register:
552 void __exit hsr_netlink_exit(void)
554 genl_unregister_family(&hsr_genl_family);
555 rtnl_link_unregister(&hsr_link_ops);
558 MODULE_ALIAS_RTNL_LINK("hsr");