1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2011-2014 Autronica Fire and Security AS
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 },
26 [IFLA_HSR_INTERLINK] = { .type = NLA_U32 },
29 /* Here, it seems a netdevice has already been allocated for us, and the
30 * hsr_dev_setup routine has been executed. Nice!
32 static int hsr_newlink(struct net *src_net, struct net_device *dev,
33 struct nlattr *tb[], struct nlattr *data[],
34 struct netlink_ext_ack *extack)
36 enum hsr_version proto_version;
37 unsigned char multicast_spec;
38 u8 proto = HSR_PROTOCOL_HSR;
40 struct net_device *link[2], *interlink = NULL;
42 NL_SET_ERR_MSG_MOD(extack, "No slave devices specified");
45 if (!data[IFLA_HSR_SLAVE1]) {
46 NL_SET_ERR_MSG_MOD(extack, "Slave1 device not specified");
49 link[0] = __dev_get_by_index(src_net,
50 nla_get_u32(data[IFLA_HSR_SLAVE1]));
52 NL_SET_ERR_MSG_MOD(extack, "Slave1 does not exist");
55 if (!data[IFLA_HSR_SLAVE2]) {
56 NL_SET_ERR_MSG_MOD(extack, "Slave2 device not specified");
59 link[1] = __dev_get_by_index(src_net,
60 nla_get_u32(data[IFLA_HSR_SLAVE2]));
62 NL_SET_ERR_MSG_MOD(extack, "Slave2 does not exist");
66 if (link[0] == link[1]) {
67 NL_SET_ERR_MSG_MOD(extack, "Slave1 and Slave2 are same");
71 if (data[IFLA_HSR_INTERLINK])
72 interlink = __dev_get_by_index(src_net,
73 nla_get_u32(data[IFLA_HSR_INTERLINK]));
75 if (interlink && interlink == link[0]) {
76 NL_SET_ERR_MSG_MOD(extack, "Interlink and Slave1 are the same");
80 if (interlink && interlink == link[1]) {
81 NL_SET_ERR_MSG_MOD(extack, "Interlink and Slave2 are the same");
85 if (!data[IFLA_HSR_MULTICAST_SPEC])
88 multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
90 if (data[IFLA_HSR_PROTOCOL])
91 proto = nla_get_u8(data[IFLA_HSR_PROTOCOL]);
93 if (proto >= HSR_PROTOCOL_MAX) {
94 NL_SET_ERR_MSG_MOD(extack, "Unsupported protocol");
98 if (!data[IFLA_HSR_VERSION]) {
99 proto_version = HSR_V0;
101 if (proto == HSR_PROTOCOL_PRP) {
102 NL_SET_ERR_MSG_MOD(extack, "PRP version unsupported");
106 proto_version = nla_get_u8(data[IFLA_HSR_VERSION]);
107 if (proto_version > HSR_V1) {
108 NL_SET_ERR_MSG_MOD(extack,
109 "Only HSR version 0/1 supported");
114 if (proto == HSR_PROTOCOL_PRP) {
115 proto_version = PRP_V1;
117 NL_SET_ERR_MSG_MOD(extack,
118 "Interlink only works with HSR");
123 return hsr_dev_finalize(dev, link, interlink, multicast_spec,
124 proto_version, extack);
127 static void hsr_dellink(struct net_device *dev, struct list_head *head)
129 struct hsr_priv *hsr = netdev_priv(dev);
131 del_timer_sync(&hsr->prune_timer);
132 del_timer_sync(&hsr->prune_proxy_timer);
133 del_timer_sync(&hsr->announce_timer);
135 hsr_debugfs_term(hsr);
138 hsr_del_self_node(hsr);
139 hsr_del_nodes(&hsr->node_db);
140 hsr_del_nodes(&hsr->proxy_node_db);
142 unregister_netdevice_queue(dev, head);
145 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
147 struct hsr_priv *hsr = netdev_priv(dev);
148 u8 proto = HSR_PROTOCOL_HSR;
149 struct hsr_port *port;
151 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
153 if (nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex))
154 goto nla_put_failure;
157 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
159 if (nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex))
160 goto nla_put_failure;
163 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
164 hsr->sup_multicast_addr) ||
165 nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
166 goto nla_put_failure;
167 if (hsr->prot_version == PRP_V1)
168 proto = HSR_PROTOCOL_PRP;
169 if (nla_put_u8(skb, IFLA_HSR_PROTOCOL, proto))
170 goto nla_put_failure;
178 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
180 .maxtype = IFLA_HSR_MAX,
181 .policy = hsr_policy,
182 .priv_size = sizeof(struct hsr_priv),
183 .setup = hsr_dev_setup,
184 .newlink = hsr_newlink,
185 .dellink = hsr_dellink,
186 .fill_info = hsr_fill_info,
189 /* attribute policy */
190 static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
191 [HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
192 [HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
193 [HSR_A_IFINDEX] = { .type = NLA_U32 },
194 [HSR_A_IF1_AGE] = { .type = NLA_U32 },
195 [HSR_A_IF2_AGE] = { .type = NLA_U32 },
196 [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
197 [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
200 static struct genl_family hsr_genl_family;
202 static const struct genl_multicast_group hsr_mcgrps[] = {
203 { .name = "hsr-network", },
206 /* This is called if for some node with MAC address addr, we only get frames
207 * over one of the slave interfaces. This would indicate an open network ring
208 * (i.e. a link has failed somewhere).
210 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
211 struct hsr_port *port)
215 struct hsr_port *master;
218 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
222 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0,
225 goto nla_put_failure;
227 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
229 goto nla_put_failure;
231 res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
233 goto nla_put_failure;
235 genlmsg_end(skb, msg_head);
236 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
245 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
246 netdev_warn(master->dev, "Could not send HSR ring error message\n");
250 /* This is called when we haven't heard from the node with MAC address addr for
251 * some time (just before the node is removed from the node table/list).
253 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
257 struct hsr_port *master;
260 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
264 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
266 goto nla_put_failure;
268 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
270 goto nla_put_failure;
272 genlmsg_end(skb, msg_head);
273 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
282 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
283 netdev_warn(master->dev, "Could not send HSR node down\n");
287 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
288 * about the status of a specific node in the network, defined by its MAC
291 * Input: hsr ifindex, node mac address
292 * Output: hsr ifindex, node mac address (copied from request),
293 * age of latest frame from node over slave 1, slave 2 [ms]
295 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
299 struct net_device *hsr_dev;
302 struct sk_buff *skb_out;
304 struct hsr_priv *hsr;
305 struct hsr_port *port;
306 unsigned char hsr_node_addr_b[ETH_ALEN];
307 int hsr_node_if1_age;
308 u16 hsr_node_if1_seq;
309 int hsr_node_if2_age;
310 u16 hsr_node_if2_seq;
317 na = info->attrs[HSR_A_IFINDEX];
320 na = info->attrs[HSR_A_NODE_ADDR];
325 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
326 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
329 if (!is_hsr_master(hsr_dev))
333 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
339 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
340 info->snd_seq, &hsr_genl_family, 0,
341 HSR_C_SET_NODE_STATUS);
344 goto nla_put_failure;
347 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
349 goto nla_put_failure;
351 hsr = netdev_priv(hsr_dev);
352 res = hsr_get_node_data(hsr,
354 nla_data(info->attrs[HSR_A_NODE_ADDR]),
362 goto nla_put_failure;
364 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
365 nla_data(info->attrs[HSR_A_NODE_ADDR]));
367 goto nla_put_failure;
369 if (addr_b_ifindex > -1) {
370 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
373 goto nla_put_failure;
375 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX,
378 goto nla_put_failure;
381 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
383 goto nla_put_failure;
384 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
386 goto nla_put_failure;
387 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
389 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
392 goto nla_put_failure;
394 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
396 goto nla_put_failure;
397 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
399 goto nla_put_failure;
400 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
402 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
405 goto nla_put_failure;
409 genlmsg_end(skb_out, msg_head);
410 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
417 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
429 /* Get a list of MacAddressA of all nodes known to this node (including self).
431 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
433 unsigned char addr[ETH_ALEN];
434 struct net_device *hsr_dev;
435 struct sk_buff *skb_out;
436 struct hsr_priv *hsr;
437 bool restart = false;
446 na = info->attrs[HSR_A_IFINDEX];
451 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
452 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
455 if (!is_hsr_master(hsr_dev))
460 skb_out = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
466 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
467 info->snd_seq, &hsr_genl_family, 0,
468 HSR_C_SET_NODE_LIST);
471 goto nla_put_failure;
475 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
477 goto nla_put_failure;
480 hsr = netdev_priv(hsr_dev);
483 pos = hsr_get_next_node(hsr, NULL, addr);
485 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
487 if (res == -EMSGSIZE) {
488 genlmsg_end(skb_out, msg_head);
489 genlmsg_unicast(genl_info_net(info), skb_out,
494 goto nla_put_failure;
496 pos = hsr_get_next_node(hsr, pos, addr);
500 genlmsg_end(skb_out, msg_head);
501 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
508 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
520 static const struct genl_small_ops hsr_ops[] = {
522 .cmd = HSR_C_GET_NODE_STATUS,
523 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
525 .doit = hsr_get_node_status,
529 .cmd = HSR_C_GET_NODE_LIST,
530 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
532 .doit = hsr_get_node_list,
537 static struct genl_family hsr_genl_family __ro_after_init = {
541 .maxattr = HSR_A_MAX,
542 .policy = hsr_genl_policy,
544 .module = THIS_MODULE,
545 .small_ops = hsr_ops,
546 .n_small_ops = ARRAY_SIZE(hsr_ops),
547 .resv_start_op = HSR_C_SET_NODE_LIST + 1,
548 .mcgrps = hsr_mcgrps,
549 .n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
552 int __init hsr_netlink_init(void)
556 rc = rtnl_link_register(&hsr_link_ops);
558 goto fail_rtnl_link_register;
560 rc = genl_register_family(&hsr_genl_family);
562 goto fail_genl_register_family;
564 hsr_debugfs_create_root();
567 fail_genl_register_family:
568 rtnl_link_unregister(&hsr_link_ops);
569 fail_rtnl_link_register:
574 void __exit hsr_netlink_exit(void)
576 genl_unregister_family(&hsr_genl_family);
577 rtnl_link_unregister(&hsr_link_ops);
580 MODULE_ALIAS_RTNL_LINK("hsr");