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_SUPERVISION_ADDR] = { .type = NLA_BINARY, .len = ETH_ALEN },
27 [IFLA_HSR_SEQ_NR] = { .type = NLA_U16 },
31 /* Here, it seems a netdevice has already been allocated for us, and the
32 * hsr_dev_setup routine has been executed. Nice!
34 static int hsr_newlink(struct net *src_net, struct net_device *dev,
35 struct nlattr *tb[], struct nlattr *data[])
37 struct net_device *link[2];
38 unsigned char multicast_spec;
40 if (!data[IFLA_HSR_SLAVE1]) {
41 netdev_info(dev, "IFLA_HSR_SLAVE1 missing!\n");
44 link[0] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE1]));
45 if (!data[IFLA_HSR_SLAVE2]) {
46 netdev_info(dev, "IFLA_HSR_SLAVE2 missing!\n");
49 link[1] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE2]));
51 if (!link[0] || !link[1])
53 if (link[0] == link[1])
56 if (!data[IFLA_HSR_MULTICAST_SPEC])
59 multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
61 return hsr_dev_finalize(dev, link, multicast_spec);
64 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
67 struct hsr_port *port;
70 hsr = netdev_priv(dev);
75 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
77 res = nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex);
83 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
85 res = nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex);
90 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
91 hsr->sup_multicast_addr) ||
92 nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
101 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
103 .maxtype = IFLA_HSR_MAX,
104 .policy = hsr_policy,
105 .priv_size = sizeof(struct hsr_priv),
106 .setup = hsr_dev_setup,
107 .newlink = hsr_newlink,
108 .fill_info = hsr_fill_info,
113 /* attribute policy */
114 /* NLA_BINARY missing in libnl; use NLA_UNSPEC in userspace instead. */
115 static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
116 [HSR_A_NODE_ADDR] = { .type = NLA_BINARY, .len = ETH_ALEN },
117 [HSR_A_NODE_ADDR_B] = { .type = NLA_BINARY, .len = ETH_ALEN },
118 [HSR_A_IFINDEX] = { .type = NLA_U32 },
119 [HSR_A_IF1_AGE] = { .type = NLA_U32 },
120 [HSR_A_IF2_AGE] = { .type = NLA_U32 },
121 [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
122 [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
125 static struct genl_family hsr_genl_family = {
126 .id = GENL_ID_GENERATE,
130 .maxattr = HSR_A_MAX,
133 static const struct genl_multicast_group hsr_mcgrps[] = {
134 { .name = "hsr-network", },
139 /* This is called if for some node with MAC address addr, we only get frames
140 * over one of the slave interfaces. This would indicate an open network ring
141 * (i.e. a link has failed somewhere).
143 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
144 struct hsr_port *port)
148 struct hsr_port *master;
151 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
155 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_RING_ERROR);
157 goto nla_put_failure;
159 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
161 goto nla_put_failure;
163 res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
165 goto nla_put_failure;
167 genlmsg_end(skb, msg_head);
168 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
177 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
178 netdev_warn(master->dev, "Could not send HSR ring error message\n");
182 /* This is called when we haven't heard from the node with MAC address addr for
183 * some time (just before the node is removed from the node table/list).
185 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
189 struct hsr_port *master;
192 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
196 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
198 goto nla_put_failure;
201 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
203 goto nla_put_failure;
205 genlmsg_end(skb, msg_head);
206 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
215 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
216 netdev_warn(master->dev, "Could not send HSR node down\n");
221 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
222 * about the status of a specific node in the network, defined by its MAC
225 * Input: hsr ifindex, node mac address
226 * Output: hsr ifindex, node mac address (copied from request),
227 * age of latest frame from node over slave 1, slave 2 [ms]
229 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
233 struct net_device *hsr_dev;
236 struct sk_buff *skb_out;
238 struct hsr_priv *hsr;
239 struct hsr_port *port;
240 unsigned char hsr_node_addr_b[ETH_ALEN];
241 int hsr_node_if1_age;
242 u16 hsr_node_if1_seq;
243 int hsr_node_if2_age;
244 u16 hsr_node_if2_seq;
251 na = info->attrs[HSR_A_IFINDEX];
254 na = info->attrs[HSR_A_NODE_ADDR];
258 hsr_dev = __dev_get_by_index(genl_info_net(info),
259 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
262 if (!is_hsr_master(hsr_dev))
268 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
274 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
275 info->snd_seq, &hsr_genl_family, 0,
276 HSR_C_SET_NODE_STATUS);
279 goto nla_put_failure;
282 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
284 goto nla_put_failure;
286 hsr = netdev_priv(hsr_dev);
287 res = hsr_get_node_data(hsr,
288 (unsigned char *) nla_data(info->attrs[HSR_A_NODE_ADDR]),
296 goto nla_put_failure;
298 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
299 nla_data(info->attrs[HSR_A_NODE_ADDR]));
301 goto nla_put_failure;
303 if (addr_b_ifindex > -1) {
304 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
307 goto nla_put_failure;
309 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX, addr_b_ifindex);
311 goto nla_put_failure;
314 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
316 goto nla_put_failure;
317 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
319 goto nla_put_failure;
321 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
323 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
327 goto nla_put_failure;
329 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
331 goto nla_put_failure;
332 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
334 goto nla_put_failure;
336 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
338 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
342 goto nla_put_failure;
344 genlmsg_end(skb_out, msg_head);
345 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
350 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
361 /* Get a list of MacAddressA of all nodes known to this node (other than self).
363 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
367 struct net_device *hsr_dev;
370 struct sk_buff *skb_out;
372 struct hsr_priv *hsr;
374 unsigned char addr[ETH_ALEN];
380 na = info->attrs[HSR_A_IFINDEX];
384 hsr_dev = __dev_get_by_index(genl_info_net(info),
385 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
388 if (!is_hsr_master(hsr_dev))
394 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
400 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
401 info->snd_seq, &hsr_genl_family, 0,
402 HSR_C_SET_NODE_LIST);
405 goto nla_put_failure;
408 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
410 goto nla_put_failure;
412 hsr = netdev_priv(hsr_dev);
415 pos = hsr_get_next_node(hsr, NULL, addr);
417 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
420 goto nla_put_failure;
422 pos = hsr_get_next_node(hsr, pos, addr);
426 genlmsg_end(skb_out, msg_head);
427 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
432 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
444 static const struct genl_ops hsr_ops[] = {
446 .cmd = HSR_C_GET_NODE_STATUS,
448 .policy = hsr_genl_policy,
449 .doit = hsr_get_node_status,
453 .cmd = HSR_C_GET_NODE_LIST,
455 .policy = hsr_genl_policy,
456 .doit = hsr_get_node_list,
461 int __init hsr_netlink_init(void)
465 rc = rtnl_link_register(&hsr_link_ops);
467 goto fail_rtnl_link_register;
469 rc = genl_register_family_with_ops_groups(&hsr_genl_family, hsr_ops,
472 goto fail_genl_register_family;
476 fail_genl_register_family:
477 rtnl_link_unregister(&hsr_link_ops);
478 fail_rtnl_link_register:
483 void __exit hsr_netlink_exit(void)
485 genl_unregister_family(&hsr_genl_family);
486 rtnl_link_unregister(&hsr_link_ops);
489 MODULE_ALIAS_RTNL_LINK("hsr");