1 /* Copyright 2011-2013 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-2013 Arvid Brodin, arvid.brodin@xdin.com
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
},
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
[])
35 struct net_device
*link
[2];
36 unsigned char multicast_spec
;
38 if (!data
[IFLA_HSR_SLAVE1
]) {
39 netdev_info(dev
, "IFLA_HSR_SLAVE1 missing!\n");
42 link
[0] = __dev_get_by_index(src_net
, nla_get_u32(data
[IFLA_HSR_SLAVE1
]));
43 if (!data
[IFLA_HSR_SLAVE2
]) {
44 netdev_info(dev
, "IFLA_HSR_SLAVE2 missing!\n");
47 link
[1] = __dev_get_by_index(src_net
, nla_get_u32(data
[IFLA_HSR_SLAVE2
]));
49 if (!link
[0] || !link
[1])
51 if (link
[0] == link
[1])
54 if (!data
[IFLA_HSR_MULTICAST_SPEC
])
57 multicast_spec
= nla_get_u8(data
[IFLA_HSR_MULTICAST_SPEC
]);
59 return hsr_dev_finalize(dev
, link
, multicast_spec
);
62 static struct rtnl_link_ops hsr_link_ops __read_mostly
= {
64 .maxtype
= IFLA_HSR_MAX
,
66 .priv_size
= sizeof(struct hsr_priv
),
67 .setup
= hsr_dev_setup
,
68 .newlink
= hsr_newlink
,
73 /* attribute policy */
74 /* NLA_BINARY missing in libnl; use NLA_UNSPEC in userspace instead. */
75 static const struct nla_policy hsr_genl_policy
[HSR_A_MAX
+ 1] = {
76 [HSR_A_NODE_ADDR
] = { .type
= NLA_BINARY
, .len
= ETH_ALEN
},
77 [HSR_A_NODE_ADDR_B
] = { .type
= NLA_BINARY
, .len
= ETH_ALEN
},
78 [HSR_A_IFINDEX
] = { .type
= NLA_U32
},
79 [HSR_A_IF1_AGE
] = { .type
= NLA_U32
},
80 [HSR_A_IF2_AGE
] = { .type
= NLA_U32
},
81 [HSR_A_IF1_SEQ
] = { .type
= NLA_U16
},
82 [HSR_A_IF2_SEQ
] = { .type
= NLA_U16
},
85 static struct genl_family hsr_genl_family
= {
86 .id
= GENL_ID_GENERATE
,
93 static struct genl_multicast_group hsr_network_genl_mcgrp
= {
94 .name
= "hsr-network",
99 /* This is called if for some node with MAC address addr, we only get frames
100 * over one of the slave interfaces. This would indicate an open network ring
101 * (i.e. a link has failed somewhere).
103 void hsr_nl_ringerror(struct hsr_priv
*hsr_priv
, unsigned char addr
[ETH_ALEN
],
104 enum hsr_dev_idx dev_idx
)
111 skb
= genlmsg_new(NLMSG_GOODSIZE
, GFP_ATOMIC
);
115 msg_head
= genlmsg_put(skb
, 0, 0, &hsr_genl_family
, 0, HSR_C_RING_ERROR
);
117 goto nla_put_failure
;
119 res
= nla_put(skb
, HSR_A_NODE_ADDR
, ETH_ALEN
, addr
);
121 goto nla_put_failure
;
123 if (hsr_priv
->slave
[dev_idx
])
124 ifindex
= hsr_priv
->slave
[dev_idx
]->ifindex
;
127 res
= nla_put_u32(skb
, HSR_A_IFINDEX
, ifindex
);
129 goto nla_put_failure
;
131 genlmsg_end(skb
, msg_head
);
132 genlmsg_multicast(skb
, 0, hsr_network_genl_mcgrp
.id
, GFP_ATOMIC
);
140 netdev_warn(hsr_priv
->dev
, "Could not send HSR ring error message\n");
143 /* This is called when we haven't heard from the node with MAC address addr for
144 * some time (just before the node is removed from the node table/list).
146 void hsr_nl_nodedown(struct hsr_priv
*hsr_priv
, unsigned char addr
[ETH_ALEN
])
152 skb
= genlmsg_new(NLMSG_GOODSIZE
, GFP_ATOMIC
);
156 msg_head
= genlmsg_put(skb
, 0, 0, &hsr_genl_family
, 0, HSR_C_NODE_DOWN
);
158 goto nla_put_failure
;
161 res
= nla_put(skb
, HSR_A_NODE_ADDR
, ETH_ALEN
, addr
);
163 goto nla_put_failure
;
165 genlmsg_end(skb
, msg_head
);
166 genlmsg_multicast(skb
, 0, hsr_network_genl_mcgrp
.id
, GFP_ATOMIC
);
174 netdev_warn(hsr_priv
->dev
, "Could not send HSR node down\n");
178 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
179 * about the status of a specific node in the network, defined by its MAC
182 * Input: hsr ifindex, node mac address
183 * Output: hsr ifindex, node mac address (copied from request),
184 * age of latest frame from node over slave 1, slave 2 [ms]
186 static int hsr_get_node_status(struct sk_buff
*skb_in
, struct genl_info
*info
)
190 struct net_device
*hsr_dev
;
193 struct sk_buff
*skb_out
;
195 struct hsr_priv
*hsr_priv
;
196 unsigned char hsr_node_addr_b
[ETH_ALEN
];
197 int hsr_node_if1_age
;
198 u16 hsr_node_if1_seq
;
199 int hsr_node_if2_age
;
200 u16 hsr_node_if2_seq
;
207 na
= info
->attrs
[HSR_A_IFINDEX
];
210 na
= info
->attrs
[HSR_A_NODE_ADDR
];
214 hsr_dev
= __dev_get_by_index(genl_info_net(info
),
215 nla_get_u32(info
->attrs
[HSR_A_IFINDEX
]));
218 if (!is_hsr_master(hsr_dev
))
224 skb_out
= genlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
230 msg_head
= genlmsg_put(skb_out
, NETLINK_CB(skb_in
).portid
,
231 info
->snd_seq
, &hsr_genl_family
, 0,
232 HSR_C_SET_NODE_STATUS
);
235 goto nla_put_failure
;
238 res
= nla_put_u32(skb_out
, HSR_A_IFINDEX
, hsr_dev
->ifindex
);
240 goto nla_put_failure
;
242 hsr_priv
= netdev_priv(hsr_dev
);
243 res
= hsr_get_node_data(hsr_priv
,
244 (unsigned char *) nla_data(info
->attrs
[HSR_A_NODE_ADDR
]),
252 goto nla_put_failure
;
254 res
= nla_put(skb_out
, HSR_A_NODE_ADDR
, ETH_ALEN
,
255 nla_data(info
->attrs
[HSR_A_NODE_ADDR
]));
257 goto nla_put_failure
;
259 if (addr_b_ifindex
> -1) {
260 res
= nla_put(skb_out
, HSR_A_NODE_ADDR_B
, ETH_ALEN
,
263 goto nla_put_failure
;
265 res
= nla_put_u32(skb_out
, HSR_A_ADDR_B_IFINDEX
, addr_b_ifindex
);
267 goto nla_put_failure
;
270 res
= nla_put_u32(skb_out
, HSR_A_IF1_AGE
, hsr_node_if1_age
);
272 goto nla_put_failure
;
273 res
= nla_put_u16(skb_out
, HSR_A_IF1_SEQ
, hsr_node_if1_seq
);
275 goto nla_put_failure
;
276 if (hsr_priv
->slave
[0])
277 res
= nla_put_u32(skb_out
, HSR_A_IF1_IFINDEX
,
278 hsr_priv
->slave
[0]->ifindex
);
280 goto nla_put_failure
;
282 res
= nla_put_u32(skb_out
, HSR_A_IF2_AGE
, hsr_node_if2_age
);
284 goto nla_put_failure
;
285 res
= nla_put_u16(skb_out
, HSR_A_IF2_SEQ
, hsr_node_if2_seq
);
287 goto nla_put_failure
;
288 if (hsr_priv
->slave
[1])
289 res
= nla_put_u32(skb_out
, HSR_A_IF2_IFINDEX
,
290 hsr_priv
->slave
[1]->ifindex
);
292 genlmsg_end(skb_out
, msg_head
);
293 genlmsg_unicast(genl_info_net(info
), skb_out
, info
->snd_portid
);
298 netlink_ack(skb_in
, nlmsg_hdr(skb_in
), -EINVAL
);
309 /* Get a list of MacAddressA of all nodes known to this node (other than self).
311 static int hsr_get_node_list(struct sk_buff
*skb_in
, struct genl_info
*info
)
315 struct net_device
*hsr_dev
;
318 struct sk_buff
*skb_out
;
320 struct hsr_priv
*hsr_priv
;
322 unsigned char addr
[ETH_ALEN
];
328 na
= info
->attrs
[HSR_A_IFINDEX
];
332 hsr_dev
= __dev_get_by_index(genl_info_net(info
),
333 nla_get_u32(info
->attrs
[HSR_A_IFINDEX
]));
336 if (!is_hsr_master(hsr_dev
))
342 skb_out
= genlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
348 msg_head
= genlmsg_put(skb_out
, NETLINK_CB(skb_in
).portid
,
349 info
->snd_seq
, &hsr_genl_family
, 0,
350 HSR_C_SET_NODE_LIST
);
353 goto nla_put_failure
;
356 res
= nla_put_u32(skb_out
, HSR_A_IFINDEX
, hsr_dev
->ifindex
);
358 goto nla_put_failure
;
360 hsr_priv
= netdev_priv(hsr_dev
);
363 pos
= hsr_get_next_node(hsr_priv
, NULL
, addr
);
365 res
= nla_put(skb_out
, HSR_A_NODE_ADDR
, ETH_ALEN
, addr
);
368 goto nla_put_failure
;
370 pos
= hsr_get_next_node(hsr_priv
, pos
, addr
);
374 genlmsg_end(skb_out
, msg_head
);
375 genlmsg_unicast(genl_info_net(info
), skb_out
, info
->snd_portid
);
380 netlink_ack(skb_in
, nlmsg_hdr(skb_in
), -EINVAL
);
392 static const struct genl_ops hsr_ops
[] = {
394 .cmd
= HSR_C_GET_NODE_STATUS
,
396 .policy
= hsr_genl_policy
,
397 .doit
= hsr_get_node_status
,
401 .cmd
= HSR_C_GET_NODE_LIST
,
403 .policy
= hsr_genl_policy
,
404 .doit
= hsr_get_node_list
,
409 int __init
hsr_netlink_init(void)
413 rc
= rtnl_link_register(&hsr_link_ops
);
415 goto fail_rtnl_link_register
;
417 rc
= genl_register_family_with_ops(&hsr_genl_family
, hsr_ops
);
419 goto fail_genl_register_family
;
421 rc
= genl_register_mc_group(&hsr_genl_family
, &hsr_network_genl_mcgrp
);
423 goto fail_genl_register_mc_group
;
427 fail_genl_register_mc_group
:
428 genl_unregister_family(&hsr_genl_family
);
429 fail_genl_register_family
:
430 rtnl_link_unregister(&hsr_link_ops
);
431 fail_rtnl_link_register
:
436 void __exit
hsr_netlink_exit(void)
438 genl_unregister_mc_group(&hsr_genl_family
, &hsr_network_genl_mcgrp
);
439 genl_unregister_family(&hsr_genl_family
);
441 rtnl_link_unregister(&hsr_link_ops
);
444 MODULE_ALIAS_RTNL_LINK("hsr");