blob: 1ab30e7d3f99e19c5e54fd9747cfce7a5c1f559b [file] [log] [blame]
Arvid Brodin70ebe4a2014-07-04 23:34:38 +02001/* Copyright 2011-2014 Autronica Fire and Security AS
Arvid Brodinf4214362013-10-30 21:10:47 +01002 *
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):
Arvid Brodin70ebe4a2014-07-04 23:34:38 +02009 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
Arvid Brodinf4214362013-10-30 21:10:47 +010010 *
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
22static 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 },
Peter Heiseee1c2792016-04-13 13:52:22 +020026 [IFLA_HSR_VERSION] = { .type = NLA_U8 },
Peter Heisef9375722016-04-19 13:34:28 +020027 [IFLA_HSR_SUPERVISION_ADDR] = { .len = ETH_ALEN },
Arvid Brodin98bf8362013-11-29 23:38:16 +010028 [IFLA_HSR_SEQ_NR] = { .type = NLA_U16 },
Arvid Brodinf4214362013-10-30 21:10:47 +010029};
30
31
32/* Here, it seems a netdevice has already been allocated for us, and the
33 * hsr_dev_setup routine has been executed. Nice!
34 */
35static int hsr_newlink(struct net *src_net, struct net_device *dev,
36 struct nlattr *tb[], struct nlattr *data[])
37{
38 struct net_device *link[2];
Peter Heiseee1c2792016-04-13 13:52:22 +020039 unsigned char multicast_spec, hsr_version;
Arvid Brodinf4214362013-10-30 21:10:47 +010040
Arvid Brodina718dcc2014-07-04 23:42:00 +020041 if (!data) {
42 netdev_info(dev, "HSR: No slave devices specified\n");
43 return -EINVAL;
44 }
Arvid Brodinf4214362013-10-30 21:10:47 +010045 if (!data[IFLA_HSR_SLAVE1]) {
Arvid Brodina718dcc2014-07-04 23:42:00 +020046 netdev_info(dev, "HSR: Slave1 device not specified\n");
Arvid Brodinf4214362013-10-30 21:10:47 +010047 return -EINVAL;
48 }
49 link[0] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE1]));
50 if (!data[IFLA_HSR_SLAVE2]) {
Arvid Brodina718dcc2014-07-04 23:42:00 +020051 netdev_info(dev, "HSR: Slave2 device not specified\n");
Arvid Brodinf4214362013-10-30 21:10:47 +010052 return -EINVAL;
53 }
54 link[1] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE2]));
55
56 if (!link[0] || !link[1])
57 return -ENODEV;
58 if (link[0] == link[1])
59 return -EINVAL;
60
61 if (!data[IFLA_HSR_MULTICAST_SPEC])
62 multicast_spec = 0;
63 else
64 multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
65
Peter Heiseee1c2792016-04-13 13:52:22 +020066 if (!data[IFLA_HSR_VERSION])
67 hsr_version = 0;
68 else
69 hsr_version = nla_get_u8(data[IFLA_HSR_VERSION]);
70
71 return hsr_dev_finalize(dev, link, multicast_spec, hsr_version);
Arvid Brodinf4214362013-10-30 21:10:47 +010072}
73
Arvid Brodin98bf8362013-11-29 23:38:16 +010074static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
75{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020076 struct hsr_priv *hsr;
Arvid Brodinc5a75912014-07-04 23:38:05 +020077 struct hsr_port *port;
Arvid Brodin51f3c602014-07-04 23:37:27 +020078 int res;
Arvid Brodin98bf8362013-11-29 23:38:16 +010079
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020080 hsr = netdev_priv(dev);
Arvid Brodin98bf8362013-11-29 23:38:16 +010081
Arvid Brodin51f3c602014-07-04 23:37:27 +020082 res = 0;
Arvid Brodin98bf8362013-11-29 23:38:16 +010083
Arvid Brodin51f3c602014-07-04 23:37:27 +020084 rcu_read_lock();
Arvid Brodinc5a75912014-07-04 23:38:05 +020085 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
86 if (port)
87 res = nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex);
Arvid Brodin51f3c602014-07-04 23:37:27 +020088 rcu_read_unlock();
89 if (res)
90 goto nla_put_failure;
91
92 rcu_read_lock();
Arvid Brodinc5a75912014-07-04 23:38:05 +020093 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
94 if (port)
95 res = nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex);
Arvid Brodin51f3c602014-07-04 23:37:27 +020096 rcu_read_unlock();
97 if (res)
98 goto nla_put_failure;
Arvid Brodin98bf8362013-11-29 23:38:16 +010099
100 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200101 hsr->sup_multicast_addr) ||
102 nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
Arvid Brodin98bf8362013-11-29 23:38:16 +0100103 goto nla_put_failure;
104
105 return 0;
106
107nla_put_failure:
108 return -EMSGSIZE;
109}
110
Arvid Brodinf4214362013-10-30 21:10:47 +0100111static struct rtnl_link_ops hsr_link_ops __read_mostly = {
112 .kind = "hsr",
113 .maxtype = IFLA_HSR_MAX,
114 .policy = hsr_policy,
115 .priv_size = sizeof(struct hsr_priv),
116 .setup = hsr_dev_setup,
117 .newlink = hsr_newlink,
Arvid Brodin98bf8362013-11-29 23:38:16 +0100118 .fill_info = hsr_fill_info,
Arvid Brodinf4214362013-10-30 21:10:47 +0100119};
120
121
122
123/* attribute policy */
Arvid Brodinf4214362013-10-30 21:10:47 +0100124static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
Peter Heisef9375722016-04-19 13:34:28 +0200125 [HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
126 [HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
Arvid Brodinf4214362013-10-30 21:10:47 +0100127 [HSR_A_IFINDEX] = { .type = NLA_U32 },
128 [HSR_A_IF1_AGE] = { .type = NLA_U32 },
129 [HSR_A_IF2_AGE] = { .type = NLA_U32 },
130 [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
131 [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
132};
133
Johannes Berg489111e2016-10-24 14:40:03 +0200134static struct genl_family hsr_genl_family;
Arvid Brodinf4214362013-10-30 21:10:47 +0100135
Johannes Berg2a94fe42013-11-19 15:19:39 +0100136static const struct genl_multicast_group hsr_mcgrps[] = {
137 { .name = "hsr-network", },
Arvid Brodinf4214362013-10-30 21:10:47 +0100138};
139
140
141
142/* This is called if for some node with MAC address addr, we only get frames
143 * over one of the slave interfaces. This would indicate an open network ring
144 * (i.e. a link has failed somewhere).
145 */
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200146void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
Arvid Brodinc5a75912014-07-04 23:38:05 +0200147 struct hsr_port *port)
Arvid Brodinf4214362013-10-30 21:10:47 +0100148{
149 struct sk_buff *skb;
150 void *msg_head;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200151 struct hsr_port *master;
Arvid Brodinf4214362013-10-30 21:10:47 +0100152 int res;
Arvid Brodinf4214362013-10-30 21:10:47 +0100153
154 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
155 if (!skb)
156 goto fail;
157
158 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_RING_ERROR);
159 if (!msg_head)
160 goto nla_put_failure;
161
162 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
163 if (res < 0)
164 goto nla_put_failure;
165
Arvid Brodinc5a75912014-07-04 23:38:05 +0200166 res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
Arvid Brodinf4214362013-10-30 21:10:47 +0100167 if (res < 0)
168 goto nla_put_failure;
169
170 genlmsg_end(skb, msg_head);
Johannes Berg2a94fe42013-11-19 15:19:39 +0100171 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
Arvid Brodinf4214362013-10-30 21:10:47 +0100172
173 return;
174
175nla_put_failure:
176 kfree_skb(skb);
177
178fail:
Arvid Brodinc5a75912014-07-04 23:38:05 +0200179 rcu_read_lock();
180 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
181 netdev_warn(master->dev, "Could not send HSR ring error message\n");
182 rcu_read_unlock();
Arvid Brodinf4214362013-10-30 21:10:47 +0100183}
184
185/* This is called when we haven't heard from the node with MAC address addr for
186 * some time (just before the node is removed from the node table/list).
187 */
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200188void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
Arvid Brodinf4214362013-10-30 21:10:47 +0100189{
190 struct sk_buff *skb;
191 void *msg_head;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200192 struct hsr_port *master;
Arvid Brodinf4214362013-10-30 21:10:47 +0100193 int res;
194
195 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
196 if (!skb)
197 goto fail;
198
199 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
200 if (!msg_head)
201 goto nla_put_failure;
202
203
204 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
205 if (res < 0)
206 goto nla_put_failure;
207
208 genlmsg_end(skb, msg_head);
Johannes Berg2a94fe42013-11-19 15:19:39 +0100209 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
Arvid Brodinf4214362013-10-30 21:10:47 +0100210
211 return;
212
213nla_put_failure:
214 kfree_skb(skb);
215
216fail:
Arvid Brodinc5a75912014-07-04 23:38:05 +0200217 rcu_read_lock();
218 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
219 netdev_warn(master->dev, "Could not send HSR node down\n");
220 rcu_read_unlock();
Arvid Brodinf4214362013-10-30 21:10:47 +0100221}
222
223
224/* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
225 * about the status of a specific node in the network, defined by its MAC
226 * address.
227 *
228 * Input: hsr ifindex, node mac address
229 * Output: hsr ifindex, node mac address (copied from request),
230 * age of latest frame from node over slave 1, slave 2 [ms]
231 */
232static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
233{
234 /* For receiving */
235 struct nlattr *na;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200236 struct net_device *hsr_dev;
Arvid Brodinf4214362013-10-30 21:10:47 +0100237
238 /* For sending */
239 struct sk_buff *skb_out;
240 void *msg_head;
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200241 struct hsr_priv *hsr;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200242 struct hsr_port *port;
Arvid Brodinf4214362013-10-30 21:10:47 +0100243 unsigned char hsr_node_addr_b[ETH_ALEN];
244 int hsr_node_if1_age;
245 u16 hsr_node_if1_seq;
246 int hsr_node_if2_age;
247 u16 hsr_node_if2_seq;
248 int addr_b_ifindex;
249 int res;
250
251 if (!info)
252 goto invalid;
253
254 na = info->attrs[HSR_A_IFINDEX];
255 if (!na)
256 goto invalid;
257 na = info->attrs[HSR_A_NODE_ADDR];
258 if (!na)
259 goto invalid;
260
261 hsr_dev = __dev_get_by_index(genl_info_net(info),
262 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
263 if (!hsr_dev)
264 goto invalid;
265 if (!is_hsr_master(hsr_dev))
266 goto invalid;
267
268
269 /* Send reply */
270
271 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
272 if (!skb_out) {
273 res = -ENOMEM;
274 goto fail;
275 }
276
277 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
278 info->snd_seq, &hsr_genl_family, 0,
279 HSR_C_SET_NODE_STATUS);
280 if (!msg_head) {
281 res = -ENOMEM;
282 goto nla_put_failure;
283 }
284
285 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
286 if (res < 0)
287 goto nla_put_failure;
288
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200289 hsr = netdev_priv(hsr_dev);
290 res = hsr_get_node_data(hsr,
Arvid Brodinf4214362013-10-30 21:10:47 +0100291 (unsigned char *) nla_data(info->attrs[HSR_A_NODE_ADDR]),
292 hsr_node_addr_b,
293 &addr_b_ifindex,
294 &hsr_node_if1_age,
295 &hsr_node_if1_seq,
296 &hsr_node_if2_age,
297 &hsr_node_if2_seq);
298 if (res < 0)
Geyslan G. Bem84a035f2013-11-14 16:12:54 -0300299 goto nla_put_failure;
Arvid Brodinf4214362013-10-30 21:10:47 +0100300
301 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
302 nla_data(info->attrs[HSR_A_NODE_ADDR]));
303 if (res < 0)
304 goto nla_put_failure;
305
306 if (addr_b_ifindex > -1) {
307 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
308 hsr_node_addr_b);
309 if (res < 0)
310 goto nla_put_failure;
311
312 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX, addr_b_ifindex);
313 if (res < 0)
314 goto nla_put_failure;
315 }
316
317 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
318 if (res < 0)
319 goto nla_put_failure;
320 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
321 if (res < 0)
322 goto nla_put_failure;
Arvid Brodin51f3c602014-07-04 23:37:27 +0200323 rcu_read_lock();
Arvid Brodinc5a75912014-07-04 23:38:05 +0200324 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);
Arvid Brodin51f3c602014-07-04 23:37:27 +0200328 rcu_read_unlock();
Arvid Brodinf4214362013-10-30 21:10:47 +0100329 if (res < 0)
330 goto nla_put_failure;
331
332 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
333 if (res < 0)
334 goto nla_put_failure;
335 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
336 if (res < 0)
337 goto nla_put_failure;
Arvid Brodin51f3c602014-07-04 23:37:27 +0200338 rcu_read_lock();
Arvid Brodinc5a75912014-07-04 23:38:05 +0200339 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
340 if (port)
341 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
342 port->dev->ifindex);
Arvid Brodin51f3c602014-07-04 23:37:27 +0200343 rcu_read_unlock();
344 if (res < 0)
345 goto nla_put_failure;
Arvid Brodinf4214362013-10-30 21:10:47 +0100346
347 genlmsg_end(skb_out, msg_head);
348 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
349
350 return 0;
351
352invalid:
353 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
354 return 0;
355
356nla_put_failure:
357 kfree_skb(skb_out);
358 /* Fall through */
359
360fail:
361 return res;
362}
363
Arvid Brodinf266a682014-07-04 23:41:03 +0200364/* Get a list of MacAddressA of all nodes known to this node (including self).
Arvid Brodinf4214362013-10-30 21:10:47 +0100365 */
366static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
367{
368 /* For receiving */
369 struct nlattr *na;
370 struct net_device *hsr_dev;
371
372 /* For sending */
373 struct sk_buff *skb_out;
374 void *msg_head;
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200375 struct hsr_priv *hsr;
Arvid Brodinf4214362013-10-30 21:10:47 +0100376 void *pos;
377 unsigned char addr[ETH_ALEN];
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 hsr_dev = __dev_get_by_index(genl_info_net(info),
388 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
389 if (!hsr_dev)
390 goto invalid;
391 if (!is_hsr_master(hsr_dev))
392 goto invalid;
393
394
395 /* Send reply */
396
397 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
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 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
412 if (res < 0)
413 goto nla_put_failure;
414
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200415 hsr = netdev_priv(hsr_dev);
Arvid Brodinf4214362013-10-30 21:10:47 +0100416
417 rcu_read_lock();
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200418 pos = hsr_get_next_node(hsr, NULL, addr);
Arvid Brodinf4214362013-10-30 21:10:47 +0100419 while (pos) {
420 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
421 if (res < 0) {
422 rcu_read_unlock();
423 goto nla_put_failure;
424 }
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200425 pos = hsr_get_next_node(hsr, pos, addr);
Arvid Brodinf4214362013-10-30 21:10:47 +0100426 }
427 rcu_read_unlock();
428
429 genlmsg_end(skb_out, msg_head);
430 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
431
432 return 0;
433
434invalid:
435 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
436 return 0;
437
438nla_put_failure:
439 kfree_skb(skb_out);
440 /* Fall through */
441
442fail:
443 return res;
444}
445
446
Johannes Berg4534de82013-11-14 17:14:46 +0100447static const struct genl_ops hsr_ops[] = {
Johannes Berg9504b3e2013-11-14 17:14:40 +0100448 {
449 .cmd = HSR_C_GET_NODE_STATUS,
450 .flags = 0,
451 .policy = hsr_genl_policy,
452 .doit = hsr_get_node_status,
453 .dumpit = NULL,
454 },
455 {
456 .cmd = HSR_C_GET_NODE_LIST,
457 .flags = 0,
458 .policy = hsr_genl_policy,
459 .doit = hsr_get_node_list,
460 .dumpit = NULL,
461 },
Arvid Brodinf4214362013-10-30 21:10:47 +0100462};
463
Johannes Berg56989f62016-10-24 14:40:05 +0200464static struct genl_family hsr_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200465 .hdrsize = 0,
466 .name = "HSR",
467 .version = 1,
468 .maxattr = HSR_A_MAX,
469 .module = THIS_MODULE,
470 .ops = hsr_ops,
471 .n_ops = ARRAY_SIZE(hsr_ops),
472 .mcgrps = hsr_mcgrps,
473 .n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
474};
475
Arvid Brodinf4214362013-10-30 21:10:47 +0100476int __init hsr_netlink_init(void)
477{
478 int rc;
479
480 rc = rtnl_link_register(&hsr_link_ops);
481 if (rc)
482 goto fail_rtnl_link_register;
483
Johannes Berg489111e2016-10-24 14:40:03 +0200484 rc = genl_register_family(&hsr_genl_family);
Arvid Brodinf4214362013-10-30 21:10:47 +0100485 if (rc)
486 goto fail_genl_register_family;
487
Arvid Brodinf4214362013-10-30 21:10:47 +0100488 return 0;
489
Arvid Brodinf4214362013-10-30 21:10:47 +0100490fail_genl_register_family:
491 rtnl_link_unregister(&hsr_link_ops);
492fail_rtnl_link_register:
493
494 return rc;
495}
496
497void __exit hsr_netlink_exit(void)
498{
Arvid Brodinf4214362013-10-30 21:10:47 +0100499 genl_unregister_family(&hsr_genl_family);
Arvid Brodinf4214362013-10-30 21:10:47 +0100500 rtnl_link_unregister(&hsr_link_ops);
501}
502
503MODULE_ALIAS_RTNL_LINK("hsr");