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