Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright 2011-2014 Autronica Fire and Security AS
0003  *
0004  * Author(s):
0005  *  2011-2014 Arvid Brodin, arvid.brodin@alten.se
0006  *
0007  * Routines for handling Netlink messages for HSR and PRP.
0008  */
0009 
0010 #include "hsr_netlink.h"
0011 #include <linux/kernel.h>
0012 #include <net/rtnetlink.h>
0013 #include <net/genetlink.h>
0014 #include "hsr_main.h"
0015 #include "hsr_device.h"
0016 #include "hsr_framereg.h"
0017 
0018 static const struct nla_policy hsr_policy[IFLA_HSR_MAX + 1] = {
0019     [IFLA_HSR_SLAVE1]       = { .type = NLA_U32 },
0020     [IFLA_HSR_SLAVE2]       = { .type = NLA_U32 },
0021     [IFLA_HSR_MULTICAST_SPEC]   = { .type = NLA_U8 },
0022     [IFLA_HSR_VERSION]  = { .type = NLA_U8 },
0023     [IFLA_HSR_SUPERVISION_ADDR] = { .len = ETH_ALEN },
0024     [IFLA_HSR_SEQ_NR]       = { .type = NLA_U16 },
0025     [IFLA_HSR_PROTOCOL]     = { .type = NLA_U8 },
0026 };
0027 
0028 /* Here, it seems a netdevice has already been allocated for us, and the
0029  * hsr_dev_setup routine has been executed. Nice!
0030  */
0031 static int hsr_newlink(struct net *src_net, struct net_device *dev,
0032                struct nlattr *tb[], struct nlattr *data[],
0033                struct netlink_ext_ack *extack)
0034 {
0035     enum hsr_version proto_version;
0036     unsigned char multicast_spec;
0037     u8 proto = HSR_PROTOCOL_HSR;
0038     struct net_device *link[2];
0039 
0040     if (!data) {
0041         NL_SET_ERR_MSG_MOD(extack, "No slave devices specified");
0042         return -EINVAL;
0043     }
0044     if (!data[IFLA_HSR_SLAVE1]) {
0045         NL_SET_ERR_MSG_MOD(extack, "Slave1 device not specified");
0046         return -EINVAL;
0047     }
0048     link[0] = __dev_get_by_index(src_net,
0049                      nla_get_u32(data[IFLA_HSR_SLAVE1]));
0050     if (!link[0]) {
0051         NL_SET_ERR_MSG_MOD(extack, "Slave1 does not exist");
0052         return -EINVAL;
0053     }
0054     if (!data[IFLA_HSR_SLAVE2]) {
0055         NL_SET_ERR_MSG_MOD(extack, "Slave2 device not specified");
0056         return -EINVAL;
0057     }
0058     link[1] = __dev_get_by_index(src_net,
0059                      nla_get_u32(data[IFLA_HSR_SLAVE2]));
0060     if (!link[1]) {
0061         NL_SET_ERR_MSG_MOD(extack, "Slave2 does not exist");
0062         return -EINVAL;
0063     }
0064 
0065     if (link[0] == link[1]) {
0066         NL_SET_ERR_MSG_MOD(extack, "Slave1 and Slave2 are same");
0067         return -EINVAL;
0068     }
0069 
0070     if (!data[IFLA_HSR_MULTICAST_SPEC])
0071         multicast_spec = 0;
0072     else
0073         multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
0074 
0075     if (data[IFLA_HSR_PROTOCOL])
0076         proto = nla_get_u8(data[IFLA_HSR_PROTOCOL]);
0077 
0078     if (proto >= HSR_PROTOCOL_MAX) {
0079         NL_SET_ERR_MSG_MOD(extack, "Unsupported protocol");
0080         return -EINVAL;
0081     }
0082 
0083     if (!data[IFLA_HSR_VERSION]) {
0084         proto_version = HSR_V0;
0085     } else {
0086         if (proto == HSR_PROTOCOL_PRP) {
0087             NL_SET_ERR_MSG_MOD(extack, "PRP version unsupported");
0088             return -EINVAL;
0089         }
0090 
0091         proto_version = nla_get_u8(data[IFLA_HSR_VERSION]);
0092         if (proto_version > HSR_V1) {
0093             NL_SET_ERR_MSG_MOD(extack,
0094                        "Only HSR version 0/1 supported");
0095             return -EINVAL;
0096         }
0097     }
0098 
0099     if (proto == HSR_PROTOCOL_PRP)
0100         proto_version = PRP_V1;
0101 
0102     return hsr_dev_finalize(dev, link, multicast_spec, proto_version, extack);
0103 }
0104 
0105 static void hsr_dellink(struct net_device *dev, struct list_head *head)
0106 {
0107     struct hsr_priv *hsr = netdev_priv(dev);
0108     int i;
0109 
0110     del_timer_sync(&hsr->prune_timer);
0111     del_timer_sync(&hsr->announce_timer);
0112 
0113     hsr_debugfs_term(hsr);
0114     hsr_del_ports(hsr);
0115 
0116     hsr_del_self_node(hsr);
0117     for (i = 0; i < hsr->hash_buckets; i++)
0118         hsr_del_nodes(&hsr->node_db[i]);
0119 
0120     unregister_netdevice_queue(dev, head);
0121 }
0122 
0123 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
0124 {
0125     struct hsr_priv *hsr = netdev_priv(dev);
0126     u8 proto = HSR_PROTOCOL_HSR;
0127     struct hsr_port *port;
0128 
0129     port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
0130     if (port) {
0131         if (nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex))
0132             goto nla_put_failure;
0133     }
0134 
0135     port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
0136     if (port) {
0137         if (nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex))
0138             goto nla_put_failure;
0139     }
0140 
0141     if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
0142             hsr->sup_multicast_addr) ||
0143         nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
0144         goto nla_put_failure;
0145     if (hsr->prot_version == PRP_V1)
0146         proto = HSR_PROTOCOL_PRP;
0147     if (nla_put_u8(skb, IFLA_HSR_PROTOCOL, proto))
0148         goto nla_put_failure;
0149 
0150     return 0;
0151 
0152 nla_put_failure:
0153     return -EMSGSIZE;
0154 }
0155 
0156 static struct rtnl_link_ops hsr_link_ops __read_mostly = {
0157     .kind       = "hsr",
0158     .maxtype    = IFLA_HSR_MAX,
0159     .policy     = hsr_policy,
0160     .priv_size  = sizeof(struct hsr_priv),
0161     .setup      = hsr_dev_setup,
0162     .newlink    = hsr_newlink,
0163     .dellink    = hsr_dellink,
0164     .fill_info  = hsr_fill_info,
0165 };
0166 
0167 /* attribute policy */
0168 static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
0169     [HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
0170     [HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
0171     [HSR_A_IFINDEX] = { .type = NLA_U32 },
0172     [HSR_A_IF1_AGE] = { .type = NLA_U32 },
0173     [HSR_A_IF2_AGE] = { .type = NLA_U32 },
0174     [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
0175     [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
0176 };
0177 
0178 static struct genl_family hsr_genl_family;
0179 
0180 static const struct genl_multicast_group hsr_mcgrps[] = {
0181     { .name = "hsr-network", },
0182 };
0183 
0184 /* This is called if for some node with MAC address addr, we only get frames
0185  * over one of the slave interfaces. This would indicate an open network ring
0186  * (i.e. a link has failed somewhere).
0187  */
0188 void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
0189               struct hsr_port *port)
0190 {
0191     struct sk_buff *skb;
0192     void *msg_head;
0193     struct hsr_port *master;
0194     int res;
0195 
0196     skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
0197     if (!skb)
0198         goto fail;
0199 
0200     msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0,
0201                    HSR_C_RING_ERROR);
0202     if (!msg_head)
0203         goto nla_put_failure;
0204 
0205     res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
0206     if (res < 0)
0207         goto nla_put_failure;
0208 
0209     res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
0210     if (res < 0)
0211         goto nla_put_failure;
0212 
0213     genlmsg_end(skb, msg_head);
0214     genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
0215 
0216     return;
0217 
0218 nla_put_failure:
0219     kfree_skb(skb);
0220 
0221 fail:
0222     rcu_read_lock();
0223     master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
0224     netdev_warn(master->dev, "Could not send HSR ring error message\n");
0225     rcu_read_unlock();
0226 }
0227 
0228 /* This is called when we haven't heard from the node with MAC address addr for
0229  * some time (just before the node is removed from the node table/list).
0230  */
0231 void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
0232 {
0233     struct sk_buff *skb;
0234     void *msg_head;
0235     struct hsr_port *master;
0236     int res;
0237 
0238     skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
0239     if (!skb)
0240         goto fail;
0241 
0242     msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
0243     if (!msg_head)
0244         goto nla_put_failure;
0245 
0246     res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
0247     if (res < 0)
0248         goto nla_put_failure;
0249 
0250     genlmsg_end(skb, msg_head);
0251     genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
0252 
0253     return;
0254 
0255 nla_put_failure:
0256     kfree_skb(skb);
0257 
0258 fail:
0259     rcu_read_lock();
0260     master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
0261     netdev_warn(master->dev, "Could not send HSR node down\n");
0262     rcu_read_unlock();
0263 }
0264 
0265 /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
0266  * about the status of a specific node in the network, defined by its MAC
0267  * address.
0268  *
0269  * Input: hsr ifindex, node mac address
0270  * Output: hsr ifindex, node mac address (copied from request),
0271  *     age of latest frame from node over slave 1, slave 2 [ms]
0272  */
0273 static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
0274 {
0275     /* For receiving */
0276     struct nlattr *na;
0277     struct net_device *hsr_dev;
0278 
0279     /* For sending */
0280     struct sk_buff *skb_out;
0281     void *msg_head;
0282     struct hsr_priv *hsr;
0283     struct hsr_port *port;
0284     unsigned char hsr_node_addr_b[ETH_ALEN];
0285     int hsr_node_if1_age;
0286     u16 hsr_node_if1_seq;
0287     int hsr_node_if2_age;
0288     u16 hsr_node_if2_seq;
0289     int addr_b_ifindex;
0290     int res;
0291 
0292     if (!info)
0293         goto invalid;
0294 
0295     na = info->attrs[HSR_A_IFINDEX];
0296     if (!na)
0297         goto invalid;
0298     na = info->attrs[HSR_A_NODE_ADDR];
0299     if (!na)
0300         goto invalid;
0301 
0302     rcu_read_lock();
0303     hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
0304                        nla_get_u32(info->attrs[HSR_A_IFINDEX]));
0305     if (!hsr_dev)
0306         goto rcu_unlock;
0307     if (!is_hsr_master(hsr_dev))
0308         goto rcu_unlock;
0309 
0310     /* Send reply */
0311     skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
0312     if (!skb_out) {
0313         res = -ENOMEM;
0314         goto fail;
0315     }
0316 
0317     msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
0318                    info->snd_seq, &hsr_genl_family, 0,
0319                    HSR_C_SET_NODE_STATUS);
0320     if (!msg_head) {
0321         res = -ENOMEM;
0322         goto nla_put_failure;
0323     }
0324 
0325     res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
0326     if (res < 0)
0327         goto nla_put_failure;
0328 
0329     hsr = netdev_priv(hsr_dev);
0330     res = hsr_get_node_data(hsr,
0331                 (unsigned char *)
0332                 nla_data(info->attrs[HSR_A_NODE_ADDR]),
0333                      hsr_node_addr_b,
0334                      &addr_b_ifindex,
0335                      &hsr_node_if1_age,
0336                      &hsr_node_if1_seq,
0337                      &hsr_node_if2_age,
0338                      &hsr_node_if2_seq);
0339     if (res < 0)
0340         goto nla_put_failure;
0341 
0342     res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
0343               nla_data(info->attrs[HSR_A_NODE_ADDR]));
0344     if (res < 0)
0345         goto nla_put_failure;
0346 
0347     if (addr_b_ifindex > -1) {
0348         res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
0349                   hsr_node_addr_b);
0350         if (res < 0)
0351             goto nla_put_failure;
0352 
0353         res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX,
0354                   addr_b_ifindex);
0355         if (res < 0)
0356             goto nla_put_failure;
0357     }
0358 
0359     res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
0360     if (res < 0)
0361         goto nla_put_failure;
0362     res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
0363     if (res < 0)
0364         goto nla_put_failure;
0365     port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
0366     if (port)
0367         res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
0368                   port->dev->ifindex);
0369     if (res < 0)
0370         goto nla_put_failure;
0371 
0372     res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
0373     if (res < 0)
0374         goto nla_put_failure;
0375     res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
0376     if (res < 0)
0377         goto nla_put_failure;
0378     port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
0379     if (port)
0380         res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
0381                   port->dev->ifindex);
0382     if (res < 0)
0383         goto nla_put_failure;
0384 
0385     rcu_read_unlock();
0386 
0387     genlmsg_end(skb_out, msg_head);
0388     genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
0389 
0390     return 0;
0391 
0392 rcu_unlock:
0393     rcu_read_unlock();
0394 invalid:
0395     netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
0396     return 0;
0397 
0398 nla_put_failure:
0399     kfree_skb(skb_out);
0400     /* Fall through */
0401 
0402 fail:
0403     rcu_read_unlock();
0404     return res;
0405 }
0406 
0407 /* Get a list of MacAddressA of all nodes known to this node (including self).
0408  */
0409 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
0410 {
0411     unsigned char addr[ETH_ALEN];
0412     struct net_device *hsr_dev;
0413     struct sk_buff *skb_out;
0414     struct hsr_priv *hsr;
0415     bool restart = false;
0416     struct nlattr *na;
0417     void *pos = NULL;
0418     void *msg_head;
0419     int res;
0420 
0421     if (!info)
0422         goto invalid;
0423 
0424     na = info->attrs[HSR_A_IFINDEX];
0425     if (!na)
0426         goto invalid;
0427 
0428     rcu_read_lock();
0429     hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
0430                        nla_get_u32(info->attrs[HSR_A_IFINDEX]));
0431     if (!hsr_dev)
0432         goto rcu_unlock;
0433     if (!is_hsr_master(hsr_dev))
0434         goto rcu_unlock;
0435 
0436 restart:
0437     /* Send reply */
0438     skb_out = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
0439     if (!skb_out) {
0440         res = -ENOMEM;
0441         goto fail;
0442     }
0443 
0444     msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
0445                    info->snd_seq, &hsr_genl_family, 0,
0446                    HSR_C_SET_NODE_LIST);
0447     if (!msg_head) {
0448         res = -ENOMEM;
0449         goto nla_put_failure;
0450     }
0451 
0452     if (!restart) {
0453         res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
0454         if (res < 0)
0455             goto nla_put_failure;
0456     }
0457 
0458     hsr = netdev_priv(hsr_dev);
0459 
0460     if (!pos)
0461         pos = hsr_get_next_node(hsr, NULL, addr);
0462     while (pos) {
0463         res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
0464         if (res < 0) {
0465             if (res == -EMSGSIZE) {
0466                 genlmsg_end(skb_out, msg_head);
0467                 genlmsg_unicast(genl_info_net(info), skb_out,
0468                         info->snd_portid);
0469                 restart = true;
0470                 goto restart;
0471             }
0472             goto nla_put_failure;
0473         }
0474         pos = hsr_get_next_node(hsr, pos, addr);
0475     }
0476     rcu_read_unlock();
0477 
0478     genlmsg_end(skb_out, msg_head);
0479     genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
0480 
0481     return 0;
0482 
0483 rcu_unlock:
0484     rcu_read_unlock();
0485 invalid:
0486     netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
0487     return 0;
0488 
0489 nla_put_failure:
0490     nlmsg_free(skb_out);
0491     /* Fall through */
0492 
0493 fail:
0494     rcu_read_unlock();
0495     return res;
0496 }
0497 
0498 static const struct genl_small_ops hsr_ops[] = {
0499     {
0500         .cmd = HSR_C_GET_NODE_STATUS,
0501         .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
0502         .flags = 0,
0503         .doit = hsr_get_node_status,
0504         .dumpit = NULL,
0505     },
0506     {
0507         .cmd = HSR_C_GET_NODE_LIST,
0508         .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
0509         .flags = 0,
0510         .doit = hsr_get_node_list,
0511         .dumpit = NULL,
0512     },
0513 };
0514 
0515 static struct genl_family hsr_genl_family __ro_after_init = {
0516     .hdrsize = 0,
0517     .name = "HSR",
0518     .version = 1,
0519     .maxattr = HSR_A_MAX,
0520     .policy = hsr_genl_policy,
0521     .netnsok = true,
0522     .module = THIS_MODULE,
0523     .small_ops = hsr_ops,
0524     .n_small_ops = ARRAY_SIZE(hsr_ops),
0525     .mcgrps = hsr_mcgrps,
0526     .n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
0527 };
0528 
0529 int __init hsr_netlink_init(void)
0530 {
0531     int rc;
0532 
0533     rc = rtnl_link_register(&hsr_link_ops);
0534     if (rc)
0535         goto fail_rtnl_link_register;
0536 
0537     rc = genl_register_family(&hsr_genl_family);
0538     if (rc)
0539         goto fail_genl_register_family;
0540 
0541     hsr_debugfs_create_root();
0542     return 0;
0543 
0544 fail_genl_register_family:
0545     rtnl_link_unregister(&hsr_link_ops);
0546 fail_rtnl_link_register:
0547 
0548     return rc;
0549 }
0550 
0551 void __exit hsr_netlink_exit(void)
0552 {
0553     genl_unregister_family(&hsr_genl_family);
0554     rtnl_link_unregister(&hsr_link_ops);
0555 }
0556 
0557 MODULE_ALIAS_RTNL_LINK("hsr");