1 /* Copyright 2011-2014 Autronica Fire and Security AS
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 2 of the License, or (at your option)
9 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
11 * Routines for handling Netlink messages for HSR.
14 #include "hsr_netlink.h"
15 #include <linux/kernel.h>
16 #include <net/rtnetlink.h>
17 #include <net/genetlink.h>
19 #include "hsr_device.h"
20 #include "hsr_framereg.h"
22 static const struct nla_policy hsr_policy[IFLA_HSR_MAX + 1] = {
23 [IFLA_HSR_SLAVE1] = { .type = NLA_U32 },
24 [IFLA_HSR_SLAVE2] = { .type = NLA_U32 },
25 [IFLA_HSR_MULTICAST_SPEC] = { .type = NLA_U8 },
26 [IFLA_HSR_VERSION] = { .type = NLA_U8 },
27 [IFLA_HSR_SUPERVISION_ADDR] = { .len = ETH_ALEN },
28 [IFLA_HSR_SEQ_NR] = { .type = NLA_U16 },
32 /* Here, it seems a netdevice has already been allocated for us, and the
33 * hsr_dev_setup routine has been executed. Nice!
35 static int hsr_newlink(struct net *src_net, struct net_device *dev,
36 struct nlattr *tb[], struct nlattr *data[])
38 struct net_device *link[2];
39 unsigned char multicast_spec, hsr_version;
42 netdev_info(dev, "HSR: No slave devices specified\n");
45 if (!data[IFLA_HSR_SLAVE1]) {
46 netdev_info(dev, "HSR: Slave1 device not specified\n");
49 link[0] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE1]));
50 if (!data[IFLA_HSR_SLAVE2]) {
51 netdev_info(dev, "HSR: Slave2 device not specified\n");
54 link[1] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE2]));
56 if (!link[0] || !link[1])
58 if (link[0] == link[1])
61 if (!data[IFLA_HSR_MULTICAST_SPEC])
64 multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
66 if (!data[IFLA_HSR_VERSION]) {
69 hsr_version = nla_get_u8(data[IFLA_HSR_VERSION]);
70 if (hsr_version > 1) {
71 netdev_info(dev, "Only versions 0..1 are supported");
76 return hsr_dev_finalize(dev, link, multicast_spec, hsr_version);
79 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
82 struct hsr_port *port;
85 hsr = netdev_priv(dev);
90 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
92 res = nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex);
98 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
100 res = nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex);
103 goto nla_put_failure;
105 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
106 hsr->sup_multicast_addr) ||
107 nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
108 goto nla_put_failure;
116 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
118 .maxtype = IFLA_HSR_MAX,
119 .policy = hsr_policy,
120 .priv_size = sizeof(struct hsr_priv),
121 .setup = hsr_dev_setup,
122 .newlink = hsr_newlink,
123 .fill_info = hsr_fill_info,
128 /* attribute policy */
129 static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
130 [HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
131 [HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
132 [HSR_A_IFINDEX] = { .type = NLA_U32 },
133 [HSR_A_IF1_AGE] = { .type = NLA_U32 },
134 [HSR_A_IF2_AGE] = { .type = NLA_U32 },
135 [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
136 [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
139 static struct genl_family hsr_genl_family = {
140 .id = GENL_ID_GENERATE,
144 .maxattr = HSR_A_MAX,
148 static const struct genl_multicast_group hsr_mcgrps[] = {
149 { .name = "hsr-network", },
154 /* This is called if for some node with MAC address addr, we only get frames
155 * over one of the slave interfaces. This would indicate an open network ring
156 * (i.e. a link has failed somewhere).
158 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
159 struct hsr_port *port)
163 struct hsr_port *master;
166 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
170 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_RING_ERROR);
172 goto nla_put_failure;
174 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
176 goto nla_put_failure;
178 res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
180 goto nla_put_failure;
182 genlmsg_end(skb, msg_head);
183 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
192 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
193 netdev_warn(master->dev, "Could not send HSR ring error message\n");
197 /* This is called when we haven't heard from the node with MAC address addr for
198 * some time (just before the node is removed from the node table/list).
200 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
204 struct hsr_port *master;
207 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
211 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
213 goto nla_put_failure;
216 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
218 goto nla_put_failure;
220 genlmsg_end(skb, msg_head);
221 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
230 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
231 netdev_warn(master->dev, "Could not send HSR node down\n");
236 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
237 * about the status of a specific node in the network, defined by its MAC
240 * Input: hsr ifindex, node mac address
241 * Output: hsr ifindex, node mac address (copied from request),
242 * age of latest frame from node over slave 1, slave 2 [ms]
244 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
248 struct net_device *hsr_dev;
251 struct sk_buff *skb_out;
253 struct hsr_priv *hsr;
254 struct hsr_port *port;
255 unsigned char hsr_node_addr_b[ETH_ALEN];
256 int hsr_node_if1_age;
257 u16 hsr_node_if1_seq;
258 int hsr_node_if2_age;
259 u16 hsr_node_if2_seq;
266 na = info->attrs[HSR_A_IFINDEX];
269 na = info->attrs[HSR_A_NODE_ADDR];
274 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
275 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
278 if (!is_hsr_master(hsr_dev))
282 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
288 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
289 info->snd_seq, &hsr_genl_family, 0,
290 HSR_C_SET_NODE_STATUS);
293 goto nla_put_failure;
296 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
298 goto nla_put_failure;
300 hsr = netdev_priv(hsr_dev);
301 res = hsr_get_node_data(hsr,
302 (unsigned char *) nla_data(info->attrs[HSR_A_NODE_ADDR]),
310 goto nla_put_failure;
312 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
313 nla_data(info->attrs[HSR_A_NODE_ADDR]));
315 goto nla_put_failure;
317 if (addr_b_ifindex > -1) {
318 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
321 goto nla_put_failure;
323 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX, addr_b_ifindex);
325 goto nla_put_failure;
328 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
330 goto nla_put_failure;
331 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
333 goto nla_put_failure;
334 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
336 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
339 goto nla_put_failure;
341 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
343 goto nla_put_failure;
344 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
346 goto nla_put_failure;
347 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
349 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
352 goto nla_put_failure;
356 genlmsg_end(skb_out, msg_head);
357 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
364 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
376 /* Get a list of MacAddressA of all nodes known to this node (including self).
378 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
380 unsigned char addr[ETH_ALEN];
381 struct net_device *hsr_dev;
382 struct sk_buff *skb_out;
383 struct hsr_priv *hsr;
384 bool restart = false;
393 na = info->attrs[HSR_A_IFINDEX];
398 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
399 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
402 if (!is_hsr_master(hsr_dev))
407 skb_out = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
413 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
414 info->snd_seq, &hsr_genl_family, 0,
415 HSR_C_SET_NODE_LIST);
418 goto nla_put_failure;
422 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
424 goto nla_put_failure;
427 hsr = netdev_priv(hsr_dev);
430 pos = hsr_get_next_node(hsr, NULL, addr);
432 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
434 if (res == -EMSGSIZE) {
435 genlmsg_end(skb_out, msg_head);
436 genlmsg_unicast(genl_info_net(info), skb_out,
441 goto nla_put_failure;
443 pos = hsr_get_next_node(hsr, pos, addr);
447 genlmsg_end(skb_out, msg_head);
448 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
455 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
468 static const struct genl_ops hsr_ops[] = {
470 .cmd = HSR_C_GET_NODE_STATUS,
472 .policy = hsr_genl_policy,
473 .doit = hsr_get_node_status,
477 .cmd = HSR_C_GET_NODE_LIST,
479 .policy = hsr_genl_policy,
480 .doit = hsr_get_node_list,
485 int __init hsr_netlink_init(void)
489 rc = rtnl_link_register(&hsr_link_ops);
491 goto fail_rtnl_link_register;
493 rc = genl_register_family_with_ops_groups(&hsr_genl_family, hsr_ops,
496 goto fail_genl_register_family;
500 fail_genl_register_family:
501 rtnl_link_unregister(&hsr_link_ops);
502 fail_rtnl_link_register:
507 void __exit hsr_netlink_exit(void)
509 genl_unregister_family(&hsr_genl_family);
510 rtnl_link_unregister(&hsr_link_ops);
513 MODULE_ALIAS_RTNL_LINK("hsr");