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
},
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
)
66 struct hsr_priv
*hsr_priv
;
68 hsr_priv
= netdev_priv(dev
);
70 if (hsr_priv
->slave
[0])
71 if (nla_put_u32(skb
, IFLA_HSR_SLAVE1
, hsr_priv
->slave
[0]->ifindex
))
74 if (hsr_priv
->slave
[1])
75 if (nla_put_u32(skb
, IFLA_HSR_SLAVE2
, hsr_priv
->slave
[1]->ifindex
))
78 if (nla_put(skb
, IFLA_HSR_SUPERVISION_ADDR
, ETH_ALEN
,
79 hsr_priv
->sup_multicast_addr
) ||
80 nla_put_u16(skb
, IFLA_HSR_SEQ_NR
, hsr_priv
->sequence_nr
))
89 static struct rtnl_link_ops hsr_link_ops __read_mostly
= {
91 .maxtype
= IFLA_HSR_MAX
,
93 .priv_size
= sizeof(struct hsr_priv
),
94 .setup
= hsr_dev_setup
,
95 .newlink
= hsr_newlink
,
96 .fill_info
= hsr_fill_info
,
101 /* attribute policy */
102 /* NLA_BINARY missing in libnl; use NLA_UNSPEC in userspace instead. */
103 static const struct nla_policy hsr_genl_policy
[HSR_A_MAX
+ 1] = {
104 [HSR_A_NODE_ADDR
] = { .type
= NLA_BINARY
, .len
= ETH_ALEN
},
105 [HSR_A_NODE_ADDR_B
] = { .type
= NLA_BINARY
, .len
= ETH_ALEN
},
106 [HSR_A_IFINDEX
] = { .type
= NLA_U32
},
107 [HSR_A_IF1_AGE
] = { .type
= NLA_U32
},
108 [HSR_A_IF2_AGE
] = { .type
= NLA_U32
},
109 [HSR_A_IF1_SEQ
] = { .type
= NLA_U16
},
110 [HSR_A_IF2_SEQ
] = { .type
= NLA_U16
},
113 static struct genl_family hsr_genl_family
= {
114 .id
= GENL_ID_GENERATE
,
118 .maxattr
= HSR_A_MAX
,
121 static const struct genl_multicast_group hsr_mcgrps
[] = {
122 { .name
= "hsr-network", },
127 /* This is called if for some node with MAC address addr, we only get frames
128 * over one of the slave interfaces. This would indicate an open network ring
129 * (i.e. a link has failed somewhere).
131 void hsr_nl_ringerror(struct hsr_priv
*hsr_priv
, unsigned char addr
[ETH_ALEN
],
132 enum hsr_dev_idx dev_idx
)
139 skb
= genlmsg_new(NLMSG_GOODSIZE
, GFP_ATOMIC
);
143 msg_head
= genlmsg_put(skb
, 0, 0, &hsr_genl_family
, 0, HSR_C_RING_ERROR
);
145 goto nla_put_failure
;
147 res
= nla_put(skb
, HSR_A_NODE_ADDR
, ETH_ALEN
, addr
);
149 goto nla_put_failure
;
151 if (hsr_priv
->slave
[dev_idx
])
152 ifindex
= hsr_priv
->slave
[dev_idx
]->ifindex
;
155 res
= nla_put_u32(skb
, HSR_A_IFINDEX
, ifindex
);
157 goto nla_put_failure
;
159 genlmsg_end(skb
, msg_head
);
160 genlmsg_multicast(&hsr_genl_family
, skb
, 0, 0, GFP_ATOMIC
);
168 netdev_warn(hsr_priv
->dev
, "Could not send HSR ring error message\n");
171 /* This is called when we haven't heard from the node with MAC address addr for
172 * some time (just before the node is removed from the node table/list).
174 void hsr_nl_nodedown(struct hsr_priv
*hsr_priv
, unsigned char addr
[ETH_ALEN
])
180 skb
= genlmsg_new(NLMSG_GOODSIZE
, GFP_ATOMIC
);
184 msg_head
= genlmsg_put(skb
, 0, 0, &hsr_genl_family
, 0, HSR_C_NODE_DOWN
);
186 goto nla_put_failure
;
189 res
= nla_put(skb
, HSR_A_NODE_ADDR
, ETH_ALEN
, addr
);
191 goto nla_put_failure
;
193 genlmsg_end(skb
, msg_head
);
194 genlmsg_multicast(&hsr_genl_family
, skb
, 0, 0, GFP_ATOMIC
);
202 netdev_warn(hsr_priv
->dev
, "Could not send HSR node down\n");
206 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
207 * about the status of a specific node in the network, defined by its MAC
210 * Input: hsr ifindex, node mac address
211 * Output: hsr ifindex, node mac address (copied from request),
212 * age of latest frame from node over slave 1, slave 2 [ms]
214 static int hsr_get_node_status(struct sk_buff
*skb_in
, struct genl_info
*info
)
218 struct net_device
*hsr_dev
;
221 struct sk_buff
*skb_out
;
223 struct hsr_priv
*hsr_priv
;
224 unsigned char hsr_node_addr_b
[ETH_ALEN
];
225 int hsr_node_if1_age
;
226 u16 hsr_node_if1_seq
;
227 int hsr_node_if2_age
;
228 u16 hsr_node_if2_seq
;
235 na
= info
->attrs
[HSR_A_IFINDEX
];
238 na
= info
->attrs
[HSR_A_NODE_ADDR
];
242 hsr_dev
= __dev_get_by_index(genl_info_net(info
),
243 nla_get_u32(info
->attrs
[HSR_A_IFINDEX
]));
246 if (!is_hsr_master(hsr_dev
))
252 skb_out
= genlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
258 msg_head
= genlmsg_put(skb_out
, NETLINK_CB(skb_in
).portid
,
259 info
->snd_seq
, &hsr_genl_family
, 0,
260 HSR_C_SET_NODE_STATUS
);
263 goto nla_put_failure
;
266 res
= nla_put_u32(skb_out
, HSR_A_IFINDEX
, hsr_dev
->ifindex
);
268 goto nla_put_failure
;
270 hsr_priv
= netdev_priv(hsr_dev
);
271 res
= hsr_get_node_data(hsr_priv
,
272 (unsigned char *) nla_data(info
->attrs
[HSR_A_NODE_ADDR
]),
280 goto nla_put_failure
;
282 res
= nla_put(skb_out
, HSR_A_NODE_ADDR
, ETH_ALEN
,
283 nla_data(info
->attrs
[HSR_A_NODE_ADDR
]));
285 goto nla_put_failure
;
287 if (addr_b_ifindex
> -1) {
288 res
= nla_put(skb_out
, HSR_A_NODE_ADDR_B
, ETH_ALEN
,
291 goto nla_put_failure
;
293 res
= nla_put_u32(skb_out
, HSR_A_ADDR_B_IFINDEX
, addr_b_ifindex
);
295 goto nla_put_failure
;
298 res
= nla_put_u32(skb_out
, HSR_A_IF1_AGE
, hsr_node_if1_age
);
300 goto nla_put_failure
;
301 res
= nla_put_u16(skb_out
, HSR_A_IF1_SEQ
, hsr_node_if1_seq
);
303 goto nla_put_failure
;
304 if (hsr_priv
->slave
[0])
305 res
= nla_put_u32(skb_out
, HSR_A_IF1_IFINDEX
,
306 hsr_priv
->slave
[0]->ifindex
);
308 goto nla_put_failure
;
310 res
= nla_put_u32(skb_out
, HSR_A_IF2_AGE
, hsr_node_if2_age
);
312 goto nla_put_failure
;
313 res
= nla_put_u16(skb_out
, HSR_A_IF2_SEQ
, hsr_node_if2_seq
);
315 goto nla_put_failure
;
316 if (hsr_priv
->slave
[1])
317 res
= nla_put_u32(skb_out
, HSR_A_IF2_IFINDEX
,
318 hsr_priv
->slave
[1]->ifindex
);
320 genlmsg_end(skb_out
, msg_head
);
321 genlmsg_unicast(genl_info_net(info
), skb_out
, info
->snd_portid
);
326 netlink_ack(skb_in
, nlmsg_hdr(skb_in
), -EINVAL
);
337 /* Get a list of MacAddressA of all nodes known to this node (other than self).
339 static int hsr_get_node_list(struct sk_buff
*skb_in
, struct genl_info
*info
)
343 struct net_device
*hsr_dev
;
346 struct sk_buff
*skb_out
;
348 struct hsr_priv
*hsr_priv
;
350 unsigned char addr
[ETH_ALEN
];
356 na
= info
->attrs
[HSR_A_IFINDEX
];
360 hsr_dev
= __dev_get_by_index(genl_info_net(info
),
361 nla_get_u32(info
->attrs
[HSR_A_IFINDEX
]));
364 if (!is_hsr_master(hsr_dev
))
370 skb_out
= genlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
376 msg_head
= genlmsg_put(skb_out
, NETLINK_CB(skb_in
).portid
,
377 info
->snd_seq
, &hsr_genl_family
, 0,
378 HSR_C_SET_NODE_LIST
);
381 goto nla_put_failure
;
384 res
= nla_put_u32(skb_out
, HSR_A_IFINDEX
, hsr_dev
->ifindex
);
386 goto nla_put_failure
;
388 hsr_priv
= netdev_priv(hsr_dev
);
391 pos
= hsr_get_next_node(hsr_priv
, NULL
, addr
);
393 res
= nla_put(skb_out
, HSR_A_NODE_ADDR
, ETH_ALEN
, addr
);
396 goto nla_put_failure
;
398 pos
= hsr_get_next_node(hsr_priv
, pos
, addr
);
402 genlmsg_end(skb_out
, msg_head
);
403 genlmsg_unicast(genl_info_net(info
), skb_out
, info
->snd_portid
);
408 netlink_ack(skb_in
, nlmsg_hdr(skb_in
), -EINVAL
);
420 static const struct genl_ops hsr_ops
[] = {
422 .cmd
= HSR_C_GET_NODE_STATUS
,
424 .policy
= hsr_genl_policy
,
425 .doit
= hsr_get_node_status
,
429 .cmd
= HSR_C_GET_NODE_LIST
,
431 .policy
= hsr_genl_policy
,
432 .doit
= hsr_get_node_list
,
437 int __init
hsr_netlink_init(void)
441 rc
= rtnl_link_register(&hsr_link_ops
);
443 goto fail_rtnl_link_register
;
445 rc
= genl_register_family_with_ops_groups(&hsr_genl_family
, hsr_ops
,
448 goto fail_genl_register_family
;
452 fail_genl_register_family
:
453 rtnl_link_unregister(&hsr_link_ops
);
454 fail_rtnl_link_register
:
459 void __exit
hsr_netlink_exit(void)
461 genl_unregister_family(&hsr_genl_family
);
462 rtnl_link_unregister(&hsr_link_ops
);
465 MODULE_ALIAS_RTNL_LINK("hsr");