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)
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[],
37 struct netlink_ext_ack *extack)
39 struct net_device *link[2];
40 unsigned char multicast_spec, hsr_version;
43 netdev_info(dev, "HSR: No slave devices specified\n");
46 if (!data[IFLA_HSR_SLAVE1]) {
47 netdev_info(dev, "HSR: Slave1 device not specified\n");
50 link[0] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE1]));
51 if (!data[IFLA_HSR_SLAVE2]) {
52 netdev_info(dev, "HSR: Slave2 device not specified\n");
55 link[1] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE2]));
57 if (!link[0] || !link[1])
59 if (link[0] == link[1])
62 if (!data[IFLA_HSR_MULTICAST_SPEC])
65 multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
67 if (!data[IFLA_HSR_VERSION])
70 hsr_version = nla_get_u8(data[IFLA_HSR_VERSION]);
72 return hsr_dev_finalize(dev, link, multicast_spec, hsr_version);
75 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
78 struct hsr_port *port;
81 hsr = netdev_priv(dev);
86 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
88 res = nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex);
94 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
96 res = nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex);
101 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
102 hsr->sup_multicast_addr) ||
103 nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
104 goto nla_put_failure;
112 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
114 .maxtype = IFLA_HSR_MAX,
115 .policy = hsr_policy,
116 .priv_size = sizeof(struct hsr_priv),
117 .setup = hsr_dev_setup,
118 .newlink = hsr_newlink,
119 .fill_info = hsr_fill_info,
124 /* attribute policy */
125 static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
126 [HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
127 [HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
128 [HSR_A_IFINDEX] = { .type = NLA_U32 },
129 [HSR_A_IF1_AGE] = { .type = NLA_U32 },
130 [HSR_A_IF2_AGE] = { .type = NLA_U32 },
131 [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
132 [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
135 static struct genl_family hsr_genl_family;
137 static const struct genl_multicast_group hsr_mcgrps[] = {
138 { .name = "hsr-network", },
143 /* This is called if for some node with MAC address addr, we only get frames
144 * over one of the slave interfaces. This would indicate an open network ring
145 * (i.e. a link has failed somewhere).
147 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
148 struct hsr_port *port)
152 struct hsr_port *master;
155 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
159 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_RING_ERROR);
161 goto nla_put_failure;
163 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
165 goto nla_put_failure;
167 res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
169 goto nla_put_failure;
171 genlmsg_end(skb, msg_head);
172 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
181 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
182 netdev_warn(master->dev, "Could not send HSR ring error message\n");
186 /* This is called when we haven't heard from the node with MAC address addr for
187 * some time (just before the node is removed from the node table/list).
189 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
193 struct hsr_port *master;
196 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
200 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
202 goto nla_put_failure;
205 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
207 goto nla_put_failure;
209 genlmsg_end(skb, msg_head);
210 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
219 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
220 netdev_warn(master->dev, "Could not send HSR node down\n");
225 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
226 * about the status of a specific node in the network, defined by its MAC
229 * Input: hsr ifindex, node mac address
230 * Output: hsr ifindex, node mac address (copied from request),
231 * age of latest frame from node over slave 1, slave 2 [ms]
233 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
237 struct net_device *hsr_dev;
240 struct sk_buff *skb_out;
242 struct hsr_priv *hsr;
243 struct hsr_port *port;
244 unsigned char hsr_node_addr_b[ETH_ALEN];
245 int hsr_node_if1_age;
246 u16 hsr_node_if1_seq;
247 int hsr_node_if2_age;
248 u16 hsr_node_if2_seq;
255 na = info->attrs[HSR_A_IFINDEX];
258 na = info->attrs[HSR_A_NODE_ADDR];
262 hsr_dev = __dev_get_by_index(genl_info_net(info),
263 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
266 if (!is_hsr_master(hsr_dev))
272 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
278 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
279 info->snd_seq, &hsr_genl_family, 0,
280 HSR_C_SET_NODE_STATUS);
283 goto nla_put_failure;
286 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
288 goto nla_put_failure;
290 hsr = netdev_priv(hsr_dev);
291 res = hsr_get_node_data(hsr,
292 (unsigned char *) nla_data(info->attrs[HSR_A_NODE_ADDR]),
300 goto nla_put_failure;
302 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
303 nla_data(info->attrs[HSR_A_NODE_ADDR]));
305 goto nla_put_failure;
307 if (addr_b_ifindex > -1) {
308 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
311 goto nla_put_failure;
313 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX, addr_b_ifindex);
315 goto nla_put_failure;
318 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
320 goto nla_put_failure;
321 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
323 goto nla_put_failure;
325 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
327 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
331 goto nla_put_failure;
333 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
335 goto nla_put_failure;
336 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
338 goto nla_put_failure;
340 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
342 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
346 goto nla_put_failure;
348 genlmsg_end(skb_out, msg_head);
349 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
354 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
365 /* Get a list of MacAddressA of all nodes known to this node (including self).
367 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
371 struct net_device *hsr_dev;
374 struct sk_buff *skb_out;
376 struct hsr_priv *hsr;
378 unsigned char addr[ETH_ALEN];
384 na = info->attrs[HSR_A_IFINDEX];
388 hsr_dev = __dev_get_by_index(genl_info_net(info),
389 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
392 if (!is_hsr_master(hsr_dev))
398 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
404 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
405 info->snd_seq, &hsr_genl_family, 0,
406 HSR_C_SET_NODE_LIST);
409 goto nla_put_failure;
412 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
414 goto nla_put_failure;
416 hsr = netdev_priv(hsr_dev);
419 pos = hsr_get_next_node(hsr, NULL, addr);
421 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
424 goto nla_put_failure;
426 pos = hsr_get_next_node(hsr, pos, addr);
430 genlmsg_end(skb_out, msg_head);
431 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
436 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
448 static const struct genl_ops hsr_ops[] = {
450 .cmd = HSR_C_GET_NODE_STATUS,
452 .policy = hsr_genl_policy,
453 .doit = hsr_get_node_status,
457 .cmd = HSR_C_GET_NODE_LIST,
459 .policy = hsr_genl_policy,
460 .doit = hsr_get_node_list,
465 static struct genl_family hsr_genl_family __ro_after_init = {
469 .maxattr = HSR_A_MAX,
470 .module = THIS_MODULE,
472 .n_ops = ARRAY_SIZE(hsr_ops),
473 .mcgrps = hsr_mcgrps,
474 .n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
477 int __init hsr_netlink_init(void)
481 rc = rtnl_link_register(&hsr_link_ops);
483 goto fail_rtnl_link_register;
485 rc = genl_register_family(&hsr_genl_family);
487 goto fail_genl_register_family;
491 fail_genl_register_family:
492 rtnl_link_unregister(&hsr_link_ops);
493 fail_rtnl_link_register:
498 void __exit hsr_netlink_exit(void)
500 genl_unregister_family(&hsr_genl_family);
501 rtnl_link_unregister(&hsr_link_ops);
504 MODULE_ALIAS_RTNL_LINK("hsr");