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]);
71 return hsr_dev_finalize(dev, link, multicast_spec, hsr_version);
74 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
77 struct hsr_port *port;
80 hsr = netdev_priv(dev);
85 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
87 res = nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex);
93 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
95 res = nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex);
100 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
101 hsr->sup_multicast_addr) ||
102 nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
103 goto nla_put_failure;
111 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
113 .maxtype = IFLA_HSR_MAX,
114 .policy = hsr_policy,
115 .priv_size = sizeof(struct hsr_priv),
116 .setup = hsr_dev_setup,
117 .newlink = hsr_newlink,
118 .fill_info = hsr_fill_info,
123 /* attribute policy */
124 static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
125 [HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
126 [HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
127 [HSR_A_IFINDEX] = { .type = NLA_U32 },
128 [HSR_A_IF1_AGE] = { .type = NLA_U32 },
129 [HSR_A_IF2_AGE] = { .type = NLA_U32 },
130 [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
131 [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
134 static struct genl_family hsr_genl_family;
136 static const struct genl_multicast_group hsr_mcgrps[] = {
137 { .name = "hsr-network", },
142 /* This is called if for some node with MAC address addr, we only get frames
143 * over one of the slave interfaces. This would indicate an open network ring
144 * (i.e. a link has failed somewhere).
146 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
147 struct hsr_port *port)
151 struct hsr_port *master;
154 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
158 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_RING_ERROR);
160 goto nla_put_failure;
162 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
164 goto nla_put_failure;
166 res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
168 goto nla_put_failure;
170 genlmsg_end(skb, msg_head);
171 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
180 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
181 netdev_warn(master->dev, "Could not send HSR ring error message\n");
185 /* This is called when we haven't heard from the node with MAC address addr for
186 * some time (just before the node is removed from the node table/list).
188 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
192 struct hsr_port *master;
195 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
199 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
201 goto nla_put_failure;
204 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
206 goto nla_put_failure;
208 genlmsg_end(skb, msg_head);
209 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
218 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
219 netdev_warn(master->dev, "Could not send HSR node down\n");
224 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
225 * about the status of a specific node in the network, defined by its MAC
228 * Input: hsr ifindex, node mac address
229 * Output: hsr ifindex, node mac address (copied from request),
230 * age of latest frame from node over slave 1, slave 2 [ms]
232 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
236 struct net_device *hsr_dev;
239 struct sk_buff *skb_out;
241 struct hsr_priv *hsr;
242 struct hsr_port *port;
243 unsigned char hsr_node_addr_b[ETH_ALEN];
244 int hsr_node_if1_age;
245 u16 hsr_node_if1_seq;
246 int hsr_node_if2_age;
247 u16 hsr_node_if2_seq;
254 na = info->attrs[HSR_A_IFINDEX];
257 na = info->attrs[HSR_A_NODE_ADDR];
261 hsr_dev = __dev_get_by_index(genl_info_net(info),
262 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
265 if (!is_hsr_master(hsr_dev))
271 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
277 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
278 info->snd_seq, &hsr_genl_family, 0,
279 HSR_C_SET_NODE_STATUS);
282 goto nla_put_failure;
285 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
287 goto nla_put_failure;
289 hsr = netdev_priv(hsr_dev);
290 res = hsr_get_node_data(hsr,
291 (unsigned char *) nla_data(info->attrs[HSR_A_NODE_ADDR]),
299 goto nla_put_failure;
301 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
302 nla_data(info->attrs[HSR_A_NODE_ADDR]));
304 goto nla_put_failure;
306 if (addr_b_ifindex > -1) {
307 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
310 goto nla_put_failure;
312 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX, addr_b_ifindex);
314 goto nla_put_failure;
317 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
319 goto nla_put_failure;
320 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
322 goto nla_put_failure;
324 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
326 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
330 goto nla_put_failure;
332 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
334 goto nla_put_failure;
335 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
337 goto nla_put_failure;
339 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
341 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
345 goto nla_put_failure;
347 genlmsg_end(skb_out, msg_head);
348 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
353 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
364 /* Get a list of MacAddressA of all nodes known to this node (including self).
366 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
370 struct net_device *hsr_dev;
373 struct sk_buff *skb_out;
375 struct hsr_priv *hsr;
377 unsigned char addr[ETH_ALEN];
383 na = info->attrs[HSR_A_IFINDEX];
387 hsr_dev = __dev_get_by_index(genl_info_net(info),
388 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
391 if (!is_hsr_master(hsr_dev))
397 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
403 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
404 info->snd_seq, &hsr_genl_family, 0,
405 HSR_C_SET_NODE_LIST);
408 goto nla_put_failure;
411 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
413 goto nla_put_failure;
415 hsr = netdev_priv(hsr_dev);
418 pos = hsr_get_next_node(hsr, NULL, addr);
420 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
423 goto nla_put_failure;
425 pos = hsr_get_next_node(hsr, pos, addr);
429 genlmsg_end(skb_out, msg_head);
430 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
435 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
447 static const struct genl_ops hsr_ops[] = {
449 .cmd = HSR_C_GET_NODE_STATUS,
451 .policy = hsr_genl_policy,
452 .doit = hsr_get_node_status,
456 .cmd = HSR_C_GET_NODE_LIST,
458 .policy = hsr_genl_policy,
459 .doit = hsr_get_node_list,
464 static struct genl_family hsr_genl_family __ro_after_init = {
468 .maxattr = HSR_A_MAX,
469 .module = THIS_MODULE,
471 .n_ops = ARRAY_SIZE(hsr_ops),
472 .mcgrps = hsr_mcgrps,
473 .n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
476 int __init hsr_netlink_init(void)
480 rc = rtnl_link_register(&hsr_link_ops);
482 goto fail_rtnl_link_register;
484 rc = genl_register_family(&hsr_genl_family);
486 goto fail_genl_register_family;
490 fail_genl_register_family:
491 rtnl_link_unregister(&hsr_link_ops);
492 fail_rtnl_link_register:
497 void __exit hsr_netlink_exit(void)
499 genl_unregister_family(&hsr_genl_family);
500 rtnl_link_unregister(&hsr_link_ops);
503 MODULE_ALIAS_RTNL_LINK("hsr");