gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / net / hsr / hsr_netlink.c
blob5465a395da040f2a9e7e0ed57e9509d8c36470ec
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2011-2014 Autronica Fire and Security AS
4 * Author(s):
5 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
7 * Routines for handling Netlink messages for HSR.
8 */
10 #include "hsr_netlink.h"
11 #include <linux/kernel.h>
12 #include <net/rtnetlink.h>
13 #include <net/genetlink.h>
14 #include "hsr_main.h"
15 #include "hsr_device.h"
16 #include "hsr_framereg.h"
18 static const struct nla_policy hsr_policy[IFLA_HSR_MAX + 1] = {
19 [IFLA_HSR_SLAVE1] = { .type = NLA_U32 },
20 [IFLA_HSR_SLAVE2] = { .type = NLA_U32 },
21 [IFLA_HSR_MULTICAST_SPEC] = { .type = NLA_U8 },
22 [IFLA_HSR_VERSION] = { .type = NLA_U8 },
23 [IFLA_HSR_SUPERVISION_ADDR] = { .len = ETH_ALEN },
24 [IFLA_HSR_SEQ_NR] = { .type = NLA_U16 },
27 /* Here, it seems a netdevice has already been allocated for us, and the
28 * hsr_dev_setup routine has been executed. Nice!
30 static int hsr_newlink(struct net *src_net, struct net_device *dev,
31 struct nlattr *tb[], struct nlattr *data[],
32 struct netlink_ext_ack *extack)
34 struct net_device *link[2];
35 unsigned char multicast_spec, hsr_version;
37 if (!data) {
38 NL_SET_ERR_MSG_MOD(extack, "No slave devices specified");
39 return -EINVAL;
41 if (!data[IFLA_HSR_SLAVE1]) {
42 NL_SET_ERR_MSG_MOD(extack, "Slave1 device not specified");
43 return -EINVAL;
45 link[0] = __dev_get_by_index(src_net,
46 nla_get_u32(data[IFLA_HSR_SLAVE1]));
47 if (!link[0]) {
48 NL_SET_ERR_MSG_MOD(extack, "Slave1 does not exist");
49 return -EINVAL;
51 if (!data[IFLA_HSR_SLAVE2]) {
52 NL_SET_ERR_MSG_MOD(extack, "Slave2 device not specified");
53 return -EINVAL;
55 link[1] = __dev_get_by_index(src_net,
56 nla_get_u32(data[IFLA_HSR_SLAVE2]));
57 if (!link[1]) {
58 NL_SET_ERR_MSG_MOD(extack, "Slave2 does not exist");
59 return -EINVAL;
62 if (link[0] == link[1]) {
63 NL_SET_ERR_MSG_MOD(extack, "Slave1 and Slave2 are same");
64 return -EINVAL;
67 if (!data[IFLA_HSR_MULTICAST_SPEC])
68 multicast_spec = 0;
69 else
70 multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
72 if (!data[IFLA_HSR_VERSION])
73 hsr_version = 0;
74 else
75 hsr_version = nla_get_u8(data[IFLA_HSR_VERSION]);
77 return hsr_dev_finalize(dev, link, multicast_spec, hsr_version, extack);
80 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
82 struct hsr_priv *hsr = netdev_priv(dev);
83 struct hsr_port *port;
85 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
86 if (port) {
87 if (nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex))
88 goto nla_put_failure;
91 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
92 if (port) {
93 if (nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex))
94 goto nla_put_failure;
97 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
98 hsr->sup_multicast_addr) ||
99 nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
100 goto nla_put_failure;
102 return 0;
104 nla_put_failure:
105 return -EMSGSIZE;
108 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
109 .kind = "hsr",
110 .maxtype = IFLA_HSR_MAX,
111 .policy = hsr_policy,
112 .priv_size = sizeof(struct hsr_priv),
113 .setup = hsr_dev_setup,
114 .newlink = hsr_newlink,
115 .fill_info = hsr_fill_info,
118 /* attribute policy */
119 static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
120 [HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
121 [HSR_A_NODE_ADDR_B] = { .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 },
129 static struct genl_family hsr_genl_family;
131 static const struct genl_multicast_group hsr_mcgrps[] = {
132 { .name = "hsr-network", },
135 /* This is called if for some node with MAC address addr, we only get frames
136 * over one of the slave interfaces. This would indicate an open network ring
137 * (i.e. a link has failed somewhere).
139 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
140 struct hsr_port *port)
142 struct sk_buff *skb;
143 void *msg_head;
144 struct hsr_port *master;
145 int res;
147 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
148 if (!skb)
149 goto fail;
151 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0,
152 HSR_C_RING_ERROR);
153 if (!msg_head)
154 goto nla_put_failure;
156 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
157 if (res < 0)
158 goto nla_put_failure;
160 res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
161 if (res < 0)
162 goto nla_put_failure;
164 genlmsg_end(skb, msg_head);
165 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
167 return;
169 nla_put_failure:
170 kfree_skb(skb);
172 fail:
173 rcu_read_lock();
174 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
175 netdev_warn(master->dev, "Could not send HSR ring error message\n");
176 rcu_read_unlock();
179 /* This is called when we haven't heard from the node with MAC address addr for
180 * some time (just before the node is removed from the node table/list).
182 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
184 struct sk_buff *skb;
185 void *msg_head;
186 struct hsr_port *master;
187 int res;
189 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
190 if (!skb)
191 goto fail;
193 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
194 if (!msg_head)
195 goto nla_put_failure;
197 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
198 if (res < 0)
199 goto nla_put_failure;
201 genlmsg_end(skb, msg_head);
202 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
204 return;
206 nla_put_failure:
207 kfree_skb(skb);
209 fail:
210 rcu_read_lock();
211 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
212 netdev_warn(master->dev, "Could not send HSR node down\n");
213 rcu_read_unlock();
216 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
217 * about the status of a specific node in the network, defined by its MAC
218 * address.
220 * Input: hsr ifindex, node mac address
221 * Output: hsr ifindex, node mac address (copied from request),
222 * age of latest frame from node over slave 1, slave 2 [ms]
224 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
226 /* For receiving */
227 struct nlattr *na;
228 struct net_device *hsr_dev;
230 /* For sending */
231 struct sk_buff *skb_out;
232 void *msg_head;
233 struct hsr_priv *hsr;
234 struct hsr_port *port;
235 unsigned char hsr_node_addr_b[ETH_ALEN];
236 int hsr_node_if1_age;
237 u16 hsr_node_if1_seq;
238 int hsr_node_if2_age;
239 u16 hsr_node_if2_seq;
240 int addr_b_ifindex;
241 int res;
243 if (!info)
244 goto invalid;
246 na = info->attrs[HSR_A_IFINDEX];
247 if (!na)
248 goto invalid;
249 na = info->attrs[HSR_A_NODE_ADDR];
250 if (!na)
251 goto invalid;
253 rcu_read_lock();
254 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
255 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
256 if (!hsr_dev)
257 goto rcu_unlock;
258 if (!is_hsr_master(hsr_dev))
259 goto rcu_unlock;
261 /* Send reply */
262 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
263 if (!skb_out) {
264 res = -ENOMEM;
265 goto fail;
268 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
269 info->snd_seq, &hsr_genl_family, 0,
270 HSR_C_SET_NODE_STATUS);
271 if (!msg_head) {
272 res = -ENOMEM;
273 goto nla_put_failure;
276 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
277 if (res < 0)
278 goto nla_put_failure;
280 hsr = netdev_priv(hsr_dev);
281 res = hsr_get_node_data(hsr,
282 (unsigned char *)
283 nla_data(info->attrs[HSR_A_NODE_ADDR]),
284 hsr_node_addr_b,
285 &addr_b_ifindex,
286 &hsr_node_if1_age,
287 &hsr_node_if1_seq,
288 &hsr_node_if2_age,
289 &hsr_node_if2_seq);
290 if (res < 0)
291 goto nla_put_failure;
293 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
294 nla_data(info->attrs[HSR_A_NODE_ADDR]));
295 if (res < 0)
296 goto nla_put_failure;
298 if (addr_b_ifindex > -1) {
299 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
300 hsr_node_addr_b);
301 if (res < 0)
302 goto nla_put_failure;
304 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX,
305 addr_b_ifindex);
306 if (res < 0)
307 goto nla_put_failure;
310 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
311 if (res < 0)
312 goto nla_put_failure;
313 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
314 if (res < 0)
315 goto nla_put_failure;
316 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
317 if (port)
318 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
319 port->dev->ifindex);
320 if (res < 0)
321 goto nla_put_failure;
323 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
324 if (res < 0)
325 goto nla_put_failure;
326 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
327 if (res < 0)
328 goto nla_put_failure;
329 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
330 if (port)
331 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
332 port->dev->ifindex);
333 if (res < 0)
334 goto nla_put_failure;
336 rcu_read_unlock();
338 genlmsg_end(skb_out, msg_head);
339 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
341 return 0;
343 rcu_unlock:
344 rcu_read_unlock();
345 invalid:
346 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
347 return 0;
349 nla_put_failure:
350 kfree_skb(skb_out);
351 /* Fall through */
353 fail:
354 rcu_read_unlock();
355 return res;
358 /* Get a list of MacAddressA of all nodes known to this node (including self).
360 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
362 unsigned char addr[ETH_ALEN];
363 struct net_device *hsr_dev;
364 struct sk_buff *skb_out;
365 struct hsr_priv *hsr;
366 bool restart = false;
367 struct nlattr *na;
368 void *pos = NULL;
369 void *msg_head;
370 int res;
372 if (!info)
373 goto invalid;
375 na = info->attrs[HSR_A_IFINDEX];
376 if (!na)
377 goto invalid;
379 rcu_read_lock();
380 hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
381 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
382 if (!hsr_dev)
383 goto rcu_unlock;
384 if (!is_hsr_master(hsr_dev))
385 goto rcu_unlock;
387 restart:
388 /* Send reply */
389 skb_out = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
390 if (!skb_out) {
391 res = -ENOMEM;
392 goto fail;
395 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
396 info->snd_seq, &hsr_genl_family, 0,
397 HSR_C_SET_NODE_LIST);
398 if (!msg_head) {
399 res = -ENOMEM;
400 goto nla_put_failure;
403 if (!restart) {
404 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
405 if (res < 0)
406 goto nla_put_failure;
409 hsr = netdev_priv(hsr_dev);
411 if (!pos)
412 pos = hsr_get_next_node(hsr, NULL, addr);
413 while (pos) {
414 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
415 if (res < 0) {
416 if (res == -EMSGSIZE) {
417 genlmsg_end(skb_out, msg_head);
418 genlmsg_unicast(genl_info_net(info), skb_out,
419 info->snd_portid);
420 restart = true;
421 goto restart;
423 goto nla_put_failure;
425 pos = hsr_get_next_node(hsr, pos, addr);
427 rcu_read_unlock();
429 genlmsg_end(skb_out, msg_head);
430 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
432 return 0;
434 rcu_unlock:
435 rcu_read_unlock();
436 invalid:
437 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
438 return 0;
440 nla_put_failure:
441 nlmsg_free(skb_out);
442 /* Fall through */
444 fail:
445 rcu_read_unlock();
446 return res;
449 static const struct genl_ops hsr_ops[] = {
451 .cmd = HSR_C_GET_NODE_STATUS,
452 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
453 .flags = 0,
454 .doit = hsr_get_node_status,
455 .dumpit = NULL,
458 .cmd = HSR_C_GET_NODE_LIST,
459 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
460 .flags = 0,
461 .doit = hsr_get_node_list,
462 .dumpit = NULL,
466 static struct genl_family hsr_genl_family __ro_after_init = {
467 .hdrsize = 0,
468 .name = "HSR",
469 .version = 1,
470 .maxattr = HSR_A_MAX,
471 .policy = hsr_genl_policy,
472 .netnsok = true,
473 .module = THIS_MODULE,
474 .ops = hsr_ops,
475 .n_ops = ARRAY_SIZE(hsr_ops),
476 .mcgrps = hsr_mcgrps,
477 .n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
480 int __init hsr_netlink_init(void)
482 int rc;
484 rc = rtnl_link_register(&hsr_link_ops);
485 if (rc)
486 goto fail_rtnl_link_register;
488 rc = genl_register_family(&hsr_genl_family);
489 if (rc)
490 goto fail_genl_register_family;
492 hsr_debugfs_create_root();
493 return 0;
495 fail_genl_register_family:
496 rtnl_link_unregister(&hsr_link_ops);
497 fail_rtnl_link_register:
499 return rc;
502 void __exit hsr_netlink_exit(void)
504 genl_unregister_family(&hsr_genl_family);
505 rtnl_link_unregister(&hsr_link_ops);
508 MODULE_ALIAS_RTNL_LINK("hsr");