hsr_netlink.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #include <linux/kernel.h>
  10. #include <net/genetlink.h>
  11. #include <net/rtnetlink.h>
  12. #include "hsr_netlink.h"
  13. #include "hsr_prp_device.h"
  14. #include "hsr_prp_framereg.h"
  15. #include "hsr_prp_main.h"
  16. #include "hsr_prp_netlink.h"
  17. static const struct nla_policy hsr_policy[IFLA_HSR_PRP_MAX + 1] = {
  18. [IFLA_HSR_PRP_SLAVE1] = { .type = NLA_U32 },
  19. [IFLA_HSR_PRP_SLAVE2] = { .type = NLA_U32 },
  20. [IFLA_HSR_PRP_SF_MC_ADDR_LSB] = { .type = NLA_U8 },
  21. [IFLA_HSR_VERSION] = { .type = NLA_U8 },
  22. [IFLA_HSR_PRP_SF_MC_ADDR] = { .len = ETH_ALEN },
  23. [IFLA_HSR_PRP_SEQ_NR] = { .type = NLA_U16 },
  24. [IFLA_HSR_PRP_SV_VID] = { .type = NLA_U16 },
  25. [IFLA_HSR_PRP_SV_PCP] = { .type = NLA_U8 },
  26. [IFLA_HSR_PRP_SV_DEI] = { .type = NLA_U8 },
  27. };
  28. static int hsr_newlink(struct net *src_net, struct net_device *dev,
  29. struct nlattr *tb[], struct nlattr *data[],
  30. struct netlink_ext_ack *extack)
  31. {
  32. return hsr_prp_newlink(HSR, src_net, dev, tb, data, extack);
  33. }
  34. static struct rtnl_link_ops hsr_link_ops __read_mostly = {
  35. .kind = "hsr",
  36. .maxtype = IFLA_HSR_PRP_MAX,
  37. .policy = hsr_policy,
  38. .priv_size = sizeof(struct hsr_prp_priv),
  39. .setup = hsr_dev_setup,
  40. .newlink = hsr_newlink,
  41. .fill_info = hsr_prp_fill_info,
  42. };
  43. /* attribute policy */
  44. static const struct nla_policy hsr_genl_policy[HSR_PRP_A_MAX + 1] = {
  45. [HSR_PRP_A_NODE_ADDR] = { .len = ETH_ALEN },
  46. [HSR_PRP_A_NODE_ADDR_B] = { .len = ETH_ALEN },
  47. [HSR_PRP_A_IFINDEX] = { .type = NLA_U32 },
  48. [HSR_PRP_A_IF1_AGE] = { .type = NLA_U32 },
  49. [HSR_PRP_A_IF2_AGE] = { .type = NLA_U32 },
  50. [HSR_PRP_A_IF1_SEQ] = { .type = NLA_U16 },
  51. [HSR_PRP_A_IF2_SEQ] = { .type = NLA_U16 },
  52. };
  53. static struct genl_family hsr_genl_family;
  54. static const struct genl_multicast_group hsr_mcgrps[] = {
  55. { .name = "hsr-network", },
  56. };
  57. /* This is called if for some node with MAC address addr, we only get frames
  58. * over one of the slave interfaces. This would indicate an open network ring
  59. * (i.e. a link has failed somewhere).
  60. */
  61. void hsr_prp_nl_ringerror(struct hsr_prp_priv *priv,
  62. unsigned char addr[ETH_ALEN],
  63. struct hsr_prp_port *port)
  64. {
  65. struct sk_buff *skb;
  66. void *msg_head;
  67. struct hsr_prp_port *master;
  68. int res;
  69. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  70. if (!skb)
  71. goto fail;
  72. msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0,
  73. HSR_C_RING_ERROR);
  74. if (!msg_head)
  75. goto nla_put_failure;
  76. res = nla_put(skb, HSR_PRP_A_NODE_ADDR, ETH_ALEN, addr);
  77. if (res < 0)
  78. goto nla_put_failure;
  79. res = nla_put_u32(skb, HSR_PRP_A_IFINDEX, port->dev->ifindex);
  80. if (res < 0)
  81. goto nla_put_failure;
  82. genlmsg_end(skb, msg_head);
  83. genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
  84. return;
  85. nla_put_failure:
  86. kfree_skb(skb);
  87. fail:
  88. rcu_read_lock();
  89. master = hsr_prp_get_port(priv, HSR_PRP_PT_MASTER);
  90. netdev_warn(master->dev, "Could not send HSR ring error message\n");
  91. rcu_read_unlock();
  92. }
  93. /* This is called when we haven't heard from the node with MAC address addr for
  94. * some time (just before the node is removed from the node table/list).
  95. */
  96. void hsr_nl_nodedown(struct hsr_prp_priv *priv, unsigned char addr[ETH_ALEN])
  97. {
  98. hsr_prp_nl_nodedown(priv, &hsr_genl_family, addr);
  99. }
  100. /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
  101. * about the status of a specific node in the network, defined by its MAC
  102. * address.
  103. *
  104. * Input: hsr ifindex, node mac address
  105. * Output: hsr ifindex, node mac address (copied from request),
  106. * age of latest frame from node over slave 1, slave 2 [ms]
  107. */
  108. static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
  109. {
  110. return hsr_prp_get_node_status(&hsr_genl_family, skb_in, info);
  111. }
  112. /* Get a list of MacAddressA of all nodes known to this node (including self).
  113. */
  114. static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
  115. {
  116. return hsr_prp_get_node_list(&hsr_genl_family, skb_in, info);
  117. }
  118. static const struct genl_ops hsr_ops[] = {
  119. {
  120. .cmd = HSR_PRP_C_GET_NODE_STATUS,
  121. .flags = 0,
  122. .policy = hsr_genl_policy,
  123. .doit = hsr_get_node_status,
  124. .dumpit = NULL,
  125. },
  126. {
  127. .cmd = HSR_PRP_C_GET_NODE_LIST,
  128. .flags = 0,
  129. .policy = hsr_genl_policy,
  130. .doit = hsr_get_node_list,
  131. .dumpit = NULL,
  132. },
  133. };
  134. static struct genl_family hsr_genl_family __ro_after_init = {
  135. .hdrsize = 0,
  136. .name = "HSR",
  137. .version = 1,
  138. .maxattr = HSR_PRP_A_MAX,
  139. .module = THIS_MODULE,
  140. .ops = hsr_ops,
  141. .n_ops = ARRAY_SIZE(hsr_ops),
  142. .mcgrps = hsr_mcgrps,
  143. .n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
  144. };
  145. int __init hsr_netlink_init(void)
  146. {
  147. int rc;
  148. rc = rtnl_link_register(&hsr_link_ops);
  149. if (rc)
  150. goto fail_rtnl_link_register;
  151. rc = genl_register_family(&hsr_genl_family);
  152. if (rc)
  153. goto fail_genl_register_family;
  154. return 0;
  155. fail_genl_register_family:
  156. rtnl_link_unregister(&hsr_link_ops);
  157. fail_rtnl_link_register:
  158. return rc;
  159. }
  160. void __exit hsr_netlink_exit(void)
  161. {
  162. genl_unregister_family(&hsr_genl_family);
  163. rtnl_link_unregister(&hsr_link_ops);
  164. }
  165. MODULE_ALIAS_RTNL_LINK("hsr");