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 multicast_spec = nla_get_u8_default(data[IFLA_HSR_MULTICAST_SPEC], 0);
87 if (data[IFLA_HSR_PROTOCOL])
88 proto = nla_get_u8(data[IFLA_HSR_PROTOCOL]);
90 if (proto >= HSR_PROTOCOL_MAX) {
91 NL_SET_ERR_MSG_MOD(extack, "Unsupported protocol");
95 if (!data[IFLA_HSR_VERSION]) {
96 proto_version = HSR_V0;
98 if (proto == HSR_PROTOCOL_PRP) {
99 NL_SET_ERR_MSG_MOD(extack, "PRP version unsupported");
103 proto_version = nla_get_u8(data[IFLA_HSR_VERSION]);
104 if (proto_version > HSR_V1) {
105 NL_SET_ERR_MSG_MOD(extack,
106 "Only HSR version 0/1 supported");
111 if (proto == HSR_PROTOCOL_PRP) {
112 proto_version = PRP_V1;
114 NL_SET_ERR_MSG_MOD(extack,
115 "Interlink only works with HSR");
120 return hsr_dev_finalize(dev, link, interlink, multicast_spec,
121 proto_version, extack);
124 static void hsr_dellink(struct net_device *dev, struct list_head *head)
126 struct hsr_priv *hsr = netdev_priv(dev);
128 timer_delete_sync(&hsr->prune_timer);
129 timer_delete_sync(&hsr->prune_proxy_timer);
130 timer_delete_sync(&hsr->announce_timer);
131 timer_delete_sync(&hsr->announce_proxy_timer);
133 hsr_debugfs_term(hsr);
136 hsr_del_self_node(hsr);
137 hsr_del_nodes(&hsr->node_db);
138 hsr_del_nodes(&hsr->proxy_node_db);
140 unregister_netdevice_queue(dev, head);
143 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
145 struct hsr_priv *hsr = netdev_priv(dev);
146 u8 proto = HSR_PROTOCOL_HSR;
147 struct hsr_port *port;
149 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
151 if (nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex))
152 goto nla_put_failure;
155 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
157 if (nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex))
158 goto nla_put_failure;
161 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
162 hsr->sup_multicast_addr) ||
163 nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
164 goto nla_put_failure;
165 if (hsr->prot_version == PRP_V1)
166 proto = HSR_PROTOCOL_PRP;
167 if (nla_put_u8(skb, IFLA_HSR_PROTOCOL, proto))
168 goto nla_put_failure;
176 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
178 .maxtype = IFLA_HSR_MAX,
179 .policy = hsr_policy,
180 .priv_size = sizeof(struct hsr_priv),
181 .setup = hsr_dev_setup,
182 .newlink = hsr_newlink,
183 .dellink = hsr_dellink,
184 .fill_info = hsr_fill_info,
187 /* attribute policy */
188 static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
189 [HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
190 [HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
191 [HSR_A_IFINDEX] = { .type = NLA_U32 },
192 [HSR_A_IF1_AGE] = { .type = NLA_U32 },
193 [HSR_A_IF2_AGE] = { .type = NLA_U32 },
194 [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
195 [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
198 static struct genl_family hsr_genl_family;
200 static const struct genl_multicast_group hsr_mcgrps[] = {
201 { .name = "hsr-network", },
204 /* This is called if for some node with MAC address addr, we only get frames
205 * over one of the slave interfaces. This would indicate an open network ring
206 * (i.e. a link has failed somewhere).
208 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
209 struct hsr_port *port)
213 struct hsr_port *master;
216 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
220 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0,
223 goto nla_put_failure;
225 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
227 goto nla_put_failure;
229 res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
231 goto nla_put_failure;
233 genlmsg_end(skb, msg_head);
234 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
243 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
244 netdev_warn(master->dev, "Could not send HSR ring error message\n");
248 /* This is called when we haven't heard from the node with MAC address addr for
249 * some time (just before the node is removed from the node table/list).
251 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
255 struct hsr_port *master;
258 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
262 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
264 goto nla_put_failure;
266 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
268 goto nla_put_failure;
270 genlmsg_end(skb, msg_head);
271 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
280 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
281 netdev_warn(master->dev, "Could not send HSR node down\n");
285 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
286 * about the status of a specific node in the network, defined by its MAC
289 * Input: hsr ifindex, node mac address
290 * Output: hsr ifindex, node mac address (copied from request),
291 * age of latest frame from node over slave 1, slave 2 [ms]
293 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
297 struct net_device *hsr_dev;
300 struct sk_buff *skb_out;
302 struct hsr_priv *hsr;
303 struct hsr_port *port;
304 unsigned char hsr_node_addr_b[ETH_ALEN];
305 int hsr_node_if1_age;
306 u16 hsr_node_if1_seq;
307 int hsr_node_if2_age;
308 u16 hsr_node_if2_seq;
315 na = info->attrs[HSR_A_IFINDEX];
318 na = info->attrs[HSR_A_NODE_ADDR];
323 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
324 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
327 if (!is_hsr_master(hsr_dev))
331 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
337 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
338 info->snd_seq, &hsr_genl_family, 0,
339 HSR_C_SET_NODE_STATUS);
342 goto nla_put_failure;
345 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
347 goto nla_put_failure;
349 hsr = netdev_priv(hsr_dev);
350 res = hsr_get_node_data(hsr,
352 nla_data(info->attrs[HSR_A_NODE_ADDR]),
360 goto nla_put_failure;
362 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
363 nla_data(info->attrs[HSR_A_NODE_ADDR]));
365 goto nla_put_failure;
367 if (addr_b_ifindex > -1) {
368 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
371 goto nla_put_failure;
373 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX,
376 goto nla_put_failure;
379 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
381 goto nla_put_failure;
382 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
384 goto nla_put_failure;
385 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
387 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
390 goto nla_put_failure;
392 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
394 goto nla_put_failure;
395 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
397 goto nla_put_failure;
398 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
400 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
403 goto nla_put_failure;
407 genlmsg_end(skb_out, msg_head);
408 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
415 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
427 /* Get a list of MacAddressA of all nodes known to this node (including self).
429 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
431 unsigned char addr[ETH_ALEN];
432 struct net_device *hsr_dev;
433 struct sk_buff *skb_out;
434 struct hsr_priv *hsr;
435 bool restart = false;
444 na = info->attrs[HSR_A_IFINDEX];
449 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
450 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
453 if (!is_hsr_master(hsr_dev))
458 skb_out = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
464 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
465 info->snd_seq, &hsr_genl_family, 0,
466 HSR_C_SET_NODE_LIST);
469 goto nla_put_failure;
473 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
475 goto nla_put_failure;
478 hsr = netdev_priv(hsr_dev);
481 pos = hsr_get_next_node(hsr, NULL, addr);
483 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
485 if (res == -EMSGSIZE) {
486 genlmsg_end(skb_out, msg_head);
487 genlmsg_unicast(genl_info_net(info), skb_out,
492 goto nla_put_failure;
494 pos = hsr_get_next_node(hsr, pos, addr);
498 genlmsg_end(skb_out, msg_head);
499 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
506 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
518 static const struct genl_small_ops hsr_ops[] = {
520 .cmd = HSR_C_GET_NODE_STATUS,
521 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
523 .doit = hsr_get_node_status,
527 .cmd = HSR_C_GET_NODE_LIST,
528 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
530 .doit = hsr_get_node_list,
535 static struct genl_family hsr_genl_family __ro_after_init = {
539 .maxattr = HSR_A_MAX,
540 .policy = hsr_genl_policy,
542 .module = THIS_MODULE,
543 .small_ops = hsr_ops,
544 .n_small_ops = ARRAY_SIZE(hsr_ops),
545 .resv_start_op = HSR_C_SET_NODE_LIST + 1,
546 .mcgrps = hsr_mcgrps,
547 .n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
550 int __init hsr_netlink_init(void)
554 rc = rtnl_link_register(&hsr_link_ops);
556 goto fail_rtnl_link_register;
558 rc = genl_register_family(&hsr_genl_family);
560 goto fail_genl_register_family;
562 hsr_debugfs_create_root();
565 fail_genl_register_family:
566 rtnl_link_unregister(&hsr_link_ops);
567 fail_rtnl_link_register:
572 void __exit hsr_netlink_exit(void)
574 genl_unregister_family(&hsr_genl_family);
575 rtnl_link_unregister(&hsr_link_ops);
578 MODULE_ALIAS_RTNL_LINK("hsr");