1 /* Copyright 2011-2014 Autronica Fire and Security AS
2 *
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)
6 * any later version.
7 *
8 * Author(s):
9 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
10 *
11 * Routines for handling Netlink messages for HSR.
12 */
13
14 #include "hsr_netlink.h"
15 #include <linux/kernel.h>
16 #include <net/rtnetlink.h>
17 #include <net/genetlink.h>
18 #include "hsr_main.h"
19 #include "hsr_device.h"
20 #include "hsr_framereg.h"
21
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 },
28 };
29
30
31 /* Here, it seems a netdevice has already been allocated for us, and the
32 * hsr_dev_setup routine has been executed. Nice!
33 */
hsr_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[])34 static int hsr_newlink(struct net *src_net, struct net_device *dev,
35 struct nlattr *tb[], struct nlattr *data[])
36 {
37 struct net_device *link[2];
38 unsigned char multicast_spec;
39
40 if (!data) {
41 netdev_info(dev, "HSR: No slave devices specified\n");
42 return -EINVAL;
43 }
44 if (!data[IFLA_HSR_SLAVE1]) {
45 netdev_info(dev, "HSR: Slave1 device not specified\n");
46 return -EINVAL;
47 }
48 link[0] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE1]));
49 if (!data[IFLA_HSR_SLAVE2]) {
50 netdev_info(dev, "HSR: Slave2 device not specified\n");
51 return -EINVAL;
52 }
53 link[1] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE2]));
54
55 if (!link[0] || !link[1])
56 return -ENODEV;
57 if (link[0] == link[1])
58 return -EINVAL;
59
60 if (!data[IFLA_HSR_MULTICAST_SPEC])
61 multicast_spec = 0;
62 else
63 multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
64
65 return hsr_dev_finalize(dev, link, multicast_spec);
66 }
67
hsr_fill_info(struct sk_buff * skb,const struct net_device * dev)68 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
69 {
70 struct hsr_priv *hsr;
71 struct hsr_port *port;
72 int res;
73
74 hsr = netdev_priv(dev);
75
76 res = 0;
77
78 rcu_read_lock();
79 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
80 if (port)
81 res = nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex);
82 rcu_read_unlock();
83 if (res)
84 goto nla_put_failure;
85
86 rcu_read_lock();
87 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
88 if (port)
89 res = nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex);
90 rcu_read_unlock();
91 if (res)
92 goto nla_put_failure;
93
94 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
95 hsr->sup_multicast_addr) ||
96 nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
97 goto nla_put_failure;
98
99 return 0;
100
101 nla_put_failure:
102 return -EMSGSIZE;
103 }
104
105 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
106 .kind = "hsr",
107 .maxtype = IFLA_HSR_MAX,
108 .policy = hsr_policy,
109 .priv_size = sizeof(struct hsr_priv),
110 .setup = hsr_dev_setup,
111 .newlink = hsr_newlink,
112 .fill_info = hsr_fill_info,
113 };
114
115
116
117 /* attribute policy */
118 /* NLA_BINARY missing in libnl; use NLA_UNSPEC in userspace instead. */
119 static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
120 [HSR_A_NODE_ADDR] = { .type = NLA_BINARY, .len = ETH_ALEN },
121 [HSR_A_NODE_ADDR_B] = { .type = NLA_BINARY, .len = ETH_ALEN },
122 [HSR_A_IFINDEX] = { .type = NLA_U32 },
123 [HSR_A_IF1_AGE] = { .type = NLA_U32 },
124 [HSR_A_IF2_AGE] = { .type = NLA_U32 },
125 [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
126 [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
127 };
128
129 static struct genl_family hsr_genl_family = {
130 .id = GENL_ID_GENERATE,
131 .hdrsize = 0,
132 .name = "HSR",
133 .version = 1,
134 .maxattr = HSR_A_MAX,
135 .netnsok = true,
136 };
137
138 static const struct genl_multicast_group hsr_mcgrps[] = {
139 { .name = "hsr-network", },
140 };
141
142
143
144 /* This is called if for some node with MAC address addr, we only get frames
145 * over one of the slave interfaces. This would indicate an open network ring
146 * (i.e. a link has failed somewhere).
147 */
hsr_nl_ringerror(struct hsr_priv * hsr,unsigned char addr[ETH_ALEN],struct hsr_port * port)148 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
149 struct hsr_port *port)
150 {
151 struct sk_buff *skb;
152 void *msg_head;
153 struct hsr_port *master;
154 int res;
155
156 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
157 if (!skb)
158 goto fail;
159
160 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_RING_ERROR);
161 if (!msg_head)
162 goto nla_put_failure;
163
164 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
165 if (res < 0)
166 goto nla_put_failure;
167
168 res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
169 if (res < 0)
170 goto nla_put_failure;
171
172 genlmsg_end(skb, msg_head);
173 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
174
175 return;
176
177 nla_put_failure:
178 kfree_skb(skb);
179
180 fail:
181 rcu_read_lock();
182 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
183 netdev_warn(master->dev, "Could not send HSR ring error message\n");
184 rcu_read_unlock();
185 }
186
187 /* This is called when we haven't heard from the node with MAC address addr for
188 * some time (just before the node is removed from the node table/list).
189 */
hsr_nl_nodedown(struct hsr_priv * hsr,unsigned char addr[ETH_ALEN])190 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
191 {
192 struct sk_buff *skb;
193 void *msg_head;
194 struct hsr_port *master;
195 int res;
196
197 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
198 if (!skb)
199 goto fail;
200
201 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
202 if (!msg_head)
203 goto nla_put_failure;
204
205
206 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
207 if (res < 0)
208 goto nla_put_failure;
209
210 genlmsg_end(skb, msg_head);
211 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
212
213 return;
214
215 nla_put_failure:
216 kfree_skb(skb);
217
218 fail:
219 rcu_read_lock();
220 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
221 netdev_warn(master->dev, "Could not send HSR node down\n");
222 rcu_read_unlock();
223 }
224
225
226 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
227 * about the status of a specific node in the network, defined by its MAC
228 * address.
229 *
230 * Input: hsr ifindex, node mac address
231 * Output: hsr ifindex, node mac address (copied from request),
232 * age of latest frame from node over slave 1, slave 2 [ms]
233 */
hsr_get_node_status(struct sk_buff * skb_in,struct genl_info * info)234 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
235 {
236 /* For receiving */
237 struct nlattr *na;
238 struct net_device *hsr_dev;
239
240 /* For sending */
241 struct sk_buff *skb_out;
242 void *msg_head;
243 struct hsr_priv *hsr;
244 struct hsr_port *port;
245 unsigned char hsr_node_addr_b[ETH_ALEN];
246 int hsr_node_if1_age;
247 u16 hsr_node_if1_seq;
248 int hsr_node_if2_age;
249 u16 hsr_node_if2_seq;
250 int addr_b_ifindex;
251 int res;
252
253 if (!info)
254 goto invalid;
255
256 na = info->attrs[HSR_A_IFINDEX];
257 if (!na)
258 goto invalid;
259 na = info->attrs[HSR_A_NODE_ADDR];
260 if (!na)
261 goto invalid;
262
263 rcu_read_lock();
264 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
265 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
266 if (!hsr_dev)
267 goto rcu_unlock;
268 if (!is_hsr_master(hsr_dev))
269 goto rcu_unlock;
270
271 /* Send reply */
272 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
273 if (!skb_out) {
274 res = -ENOMEM;
275 goto fail;
276 }
277
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);
281 if (!msg_head) {
282 res = -ENOMEM;
283 goto nla_put_failure;
284 }
285
286 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
287 if (res < 0)
288 goto nla_put_failure;
289
290 hsr = netdev_priv(hsr_dev);
291 res = hsr_get_node_data(hsr,
292 (unsigned char *) nla_data(info->attrs[HSR_A_NODE_ADDR]),
293 hsr_node_addr_b,
294 &addr_b_ifindex,
295 &hsr_node_if1_age,
296 &hsr_node_if1_seq,
297 &hsr_node_if2_age,
298 &hsr_node_if2_seq);
299 if (res < 0)
300 goto nla_put_failure;
301
302 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
303 nla_data(info->attrs[HSR_A_NODE_ADDR]));
304 if (res < 0)
305 goto nla_put_failure;
306
307 if (addr_b_ifindex > -1) {
308 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
309 hsr_node_addr_b);
310 if (res < 0)
311 goto nla_put_failure;
312
313 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX, addr_b_ifindex);
314 if (res < 0)
315 goto nla_put_failure;
316 }
317
318 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
319 if (res < 0)
320 goto nla_put_failure;
321 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
322 if (res < 0)
323 goto nla_put_failure;
324 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
325 if (port)
326 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
327 port->dev->ifindex);
328 if (res < 0)
329 goto nla_put_failure;
330
331 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
332 if (res < 0)
333 goto nla_put_failure;
334 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
335 if (res < 0)
336 goto nla_put_failure;
337 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
338 if (port)
339 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
340 port->dev->ifindex);
341 if (res < 0)
342 goto nla_put_failure;
343
344 rcu_read_unlock();
345
346 genlmsg_end(skb_out, msg_head);
347 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
348
349 return 0;
350
351 rcu_unlock:
352 rcu_read_unlock();
353 invalid:
354 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
355 return 0;
356
357 nla_put_failure:
358 kfree_skb(skb_out);
359 /* Fall through */
360
361 fail:
362 rcu_read_unlock();
363 return res;
364 }
365
366 /* Get a list of MacAddressA of all nodes known to this node (including self).
367 */
hsr_get_node_list(struct sk_buff * skb_in,struct genl_info * info)368 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
369 {
370 unsigned char addr[ETH_ALEN];
371 struct net_device *hsr_dev;
372 struct sk_buff *skb_out;
373 struct hsr_priv *hsr;
374 bool restart = false;
375 struct nlattr *na;
376 void *pos = NULL;
377 void *msg_head;
378 int res;
379
380 if (!info)
381 goto invalid;
382
383 na = info->attrs[HSR_A_IFINDEX];
384 if (!na)
385 goto invalid;
386
387 rcu_read_lock();
388 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
389 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
390 if (!hsr_dev)
391 goto rcu_unlock;
392 if (!is_hsr_master(hsr_dev))
393 goto rcu_unlock;
394
395 restart:
396 /* Send reply */
397 skb_out = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
398 if (!skb_out) {
399 res = -ENOMEM;
400 goto fail;
401 }
402
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);
406 if (!msg_head) {
407 res = -ENOMEM;
408 goto nla_put_failure;
409 }
410
411 if (!restart) {
412 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
413 if (res < 0)
414 goto nla_put_failure;
415 }
416
417 hsr = netdev_priv(hsr_dev);
418
419 if (!pos)
420 pos = hsr_get_next_node(hsr, NULL, addr);
421 while (pos) {
422 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
423 if (res < 0) {
424 if (res == -EMSGSIZE) {
425 genlmsg_end(skb_out, msg_head);
426 genlmsg_unicast(genl_info_net(info), skb_out,
427 info->snd_portid);
428 restart = true;
429 goto restart;
430 }
431 goto nla_put_failure;
432 }
433 pos = hsr_get_next_node(hsr, pos, addr);
434 }
435 rcu_read_unlock();
436
437 genlmsg_end(skb_out, msg_head);
438 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
439
440 return 0;
441
442 rcu_unlock:
443 rcu_read_unlock();
444 invalid:
445 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
446 return 0;
447
448 nla_put_failure:
449 nlmsg_free(skb_out);
450 /* Fall through */
451
452 fail:
453 rcu_read_unlock();
454 return res;
455 }
456
457
458 static const struct genl_ops hsr_ops[] = {
459 {
460 .cmd = HSR_C_GET_NODE_STATUS,
461 .flags = 0,
462 .policy = hsr_genl_policy,
463 .doit = hsr_get_node_status,
464 .dumpit = NULL,
465 },
466 {
467 .cmd = HSR_C_GET_NODE_LIST,
468 .flags = 0,
469 .policy = hsr_genl_policy,
470 .doit = hsr_get_node_list,
471 .dumpit = NULL,
472 },
473 };
474
hsr_netlink_init(void)475 int __init hsr_netlink_init(void)
476 {
477 int rc;
478
479 rc = rtnl_link_register(&hsr_link_ops);
480 if (rc)
481 goto fail_rtnl_link_register;
482
483 rc = genl_register_family_with_ops_groups(&hsr_genl_family, hsr_ops,
484 hsr_mcgrps);
485 if (rc)
486 goto fail_genl_register_family;
487
488 return 0;
489
490 fail_genl_register_family:
491 rtnl_link_unregister(&hsr_link_ops);
492 fail_rtnl_link_register:
493
494 return rc;
495 }
496
hsr_netlink_exit(void)497 void __exit hsr_netlink_exit(void)
498 {
499 genl_unregister_family(&hsr_genl_family);
500 rtnl_link_unregister(&hsr_link_ops);
501 }
502
503 MODULE_ALIAS_RTNL_LINK("hsr");
504