hsr_netlink.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* Copyright 2011-2013 Autronica Fire and Security AS
  2. *
  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):
  9. * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com
  10. *
  11. * Routines for handling Netlink messages for HSR.
  12. */
  13. #include "hsr_netlink.h"
  14. #include <linux/kernel.h>
  15. #include <net/rtnetlink.h>
  16. #include <net/genetlink.h>
  17. #include "hsr_main.h"
  18. #include "hsr_device.h"
  19. #include "hsr_framereg.h"
  20. static const struct nla_policy hsr_policy[IFLA_HSR_MAX + 1] = {
  21. [IFLA_HSR_SLAVE1] = { .type = NLA_U32 },
  22. [IFLA_HSR_SLAVE2] = { .type = NLA_U32 },
  23. [IFLA_HSR_MULTICAST_SPEC] = { .type = NLA_U8 },
  24. };
  25. /* Here, it seems a netdevice has already been allocated for us, and the
  26. * hsr_dev_setup routine has been executed. Nice!
  27. */
  28. static int hsr_newlink(struct net *src_net, struct net_device *dev,
  29. struct nlattr *tb[], struct nlattr *data[])
  30. {
  31. struct net_device *link[2];
  32. unsigned char multicast_spec;
  33. if (!data[IFLA_HSR_SLAVE1]) {
  34. netdev_info(dev, "IFLA_HSR_SLAVE1 missing!\n");
  35. return -EINVAL;
  36. }
  37. link[0] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE1]));
  38. if (!data[IFLA_HSR_SLAVE2]) {
  39. netdev_info(dev, "IFLA_HSR_SLAVE2 missing!\n");
  40. return -EINVAL;
  41. }
  42. link[1] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE2]));
  43. if (!link[0] || !link[1])
  44. return -ENODEV;
  45. if (link[0] == link[1])
  46. return -EINVAL;
  47. if (!data[IFLA_HSR_MULTICAST_SPEC])
  48. multicast_spec = 0;
  49. else
  50. multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
  51. return hsr_dev_finalize(dev, link, multicast_spec);
  52. }
  53. static struct rtnl_link_ops hsr_link_ops __read_mostly = {
  54. .kind = "hsr",
  55. .maxtype = IFLA_HSR_MAX,
  56. .policy = hsr_policy,
  57. .priv_size = sizeof(struct hsr_priv),
  58. .setup = hsr_dev_setup,
  59. .newlink = hsr_newlink,
  60. };
  61. /* attribute policy */
  62. /* NLA_BINARY missing in libnl; use NLA_UNSPEC in userspace instead. */
  63. static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
  64. [HSR_A_NODE_ADDR] = { .type = NLA_BINARY, .len = ETH_ALEN },
  65. [HSR_A_NODE_ADDR_B] = { .type = NLA_BINARY, .len = ETH_ALEN },
  66. [HSR_A_IFINDEX] = { .type = NLA_U32 },
  67. [HSR_A_IF1_AGE] = { .type = NLA_U32 },
  68. [HSR_A_IF2_AGE] = { .type = NLA_U32 },
  69. [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
  70. [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
  71. };
  72. static struct genl_family hsr_genl_family = {
  73. .id = GENL_ID_GENERATE,
  74. .hdrsize = 0,
  75. .name = "HSR",
  76. .version = 1,
  77. .maxattr = HSR_A_MAX,
  78. };
  79. static const struct genl_multicast_group hsr_mcgrps[] = {
  80. { .name = "hsr-network", },
  81. };
  82. /* This is called if for some node with MAC address addr, we only get frames
  83. * over one of the slave interfaces. This would indicate an open network ring
  84. * (i.e. a link has failed somewhere).
  85. */
  86. void hsr_nl_ringerror(struct hsr_priv *hsr_priv, unsigned char addr[ETH_ALEN],
  87. enum hsr_dev_idx dev_idx)
  88. {
  89. struct sk_buff *skb;
  90. void *msg_head;
  91. int res;
  92. int ifindex;
  93. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  94. if (!skb)
  95. goto fail;
  96. msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_RING_ERROR);
  97. if (!msg_head)
  98. goto nla_put_failure;
  99. res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
  100. if (res < 0)
  101. goto nla_put_failure;
  102. if (hsr_priv->slave[dev_idx])
  103. ifindex = hsr_priv->slave[dev_idx]->ifindex;
  104. else
  105. ifindex = -1;
  106. res = nla_put_u32(skb, HSR_A_IFINDEX, ifindex);
  107. if (res < 0)
  108. goto nla_put_failure;
  109. genlmsg_end(skb, msg_head);
  110. genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
  111. return;
  112. nla_put_failure:
  113. kfree_skb(skb);
  114. fail:
  115. netdev_warn(hsr_priv->dev, "Could not send HSR ring error message\n");
  116. }
  117. /* This is called when we haven't heard from the node with MAC address addr for
  118. * some time (just before the node is removed from the node table/list).
  119. */
  120. void hsr_nl_nodedown(struct hsr_priv *hsr_priv, unsigned char addr[ETH_ALEN])
  121. {
  122. struct sk_buff *skb;
  123. void *msg_head;
  124. int res;
  125. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  126. if (!skb)
  127. goto fail;
  128. msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
  129. if (!msg_head)
  130. goto nla_put_failure;
  131. res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
  132. if (res < 0)
  133. goto nla_put_failure;
  134. genlmsg_end(skb, msg_head);
  135. genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
  136. return;
  137. nla_put_failure:
  138. kfree_skb(skb);
  139. fail:
  140. netdev_warn(hsr_priv->dev, "Could not send HSR node down\n");
  141. }
  142. /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
  143. * about the status of a specific node in the network, defined by its MAC
  144. * address.
  145. *
  146. * Input: hsr ifindex, node mac address
  147. * Output: hsr ifindex, node mac address (copied from request),
  148. * age of latest frame from node over slave 1, slave 2 [ms]
  149. */
  150. static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
  151. {
  152. /* For receiving */
  153. struct nlattr *na;
  154. struct net_device *hsr_dev;
  155. /* For sending */
  156. struct sk_buff *skb_out;
  157. void *msg_head;
  158. struct hsr_priv *hsr_priv;
  159. unsigned char hsr_node_addr_b[ETH_ALEN];
  160. int hsr_node_if1_age;
  161. u16 hsr_node_if1_seq;
  162. int hsr_node_if2_age;
  163. u16 hsr_node_if2_seq;
  164. int addr_b_ifindex;
  165. int res;
  166. if (!info)
  167. goto invalid;
  168. na = info->attrs[HSR_A_IFINDEX];
  169. if (!na)
  170. goto invalid;
  171. na = info->attrs[HSR_A_NODE_ADDR];
  172. if (!na)
  173. goto invalid;
  174. hsr_dev = __dev_get_by_index(genl_info_net(info),
  175. nla_get_u32(info->attrs[HSR_A_IFINDEX]));
  176. if (!hsr_dev)
  177. goto invalid;
  178. if (!is_hsr_master(hsr_dev))
  179. goto invalid;
  180. /* Send reply */
  181. skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  182. if (!skb_out) {
  183. res = -ENOMEM;
  184. goto fail;
  185. }
  186. msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
  187. info->snd_seq, &hsr_genl_family, 0,
  188. HSR_C_SET_NODE_STATUS);
  189. if (!msg_head) {
  190. res = -ENOMEM;
  191. goto nla_put_failure;
  192. }
  193. res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
  194. if (res < 0)
  195. goto nla_put_failure;
  196. hsr_priv = netdev_priv(hsr_dev);
  197. res = hsr_get_node_data(hsr_priv,
  198. (unsigned char *) nla_data(info->attrs[HSR_A_NODE_ADDR]),
  199. hsr_node_addr_b,
  200. &addr_b_ifindex,
  201. &hsr_node_if1_age,
  202. &hsr_node_if1_seq,
  203. &hsr_node_if2_age,
  204. &hsr_node_if2_seq);
  205. if (res < 0)
  206. goto nla_put_failure;
  207. res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
  208. nla_data(info->attrs[HSR_A_NODE_ADDR]));
  209. if (res < 0)
  210. goto nla_put_failure;
  211. if (addr_b_ifindex > -1) {
  212. res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
  213. hsr_node_addr_b);
  214. if (res < 0)
  215. goto nla_put_failure;
  216. res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX, addr_b_ifindex);
  217. if (res < 0)
  218. goto nla_put_failure;
  219. }
  220. res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
  221. if (res < 0)
  222. goto nla_put_failure;
  223. res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
  224. if (res < 0)
  225. goto nla_put_failure;
  226. if (hsr_priv->slave[0])
  227. res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
  228. hsr_priv->slave[0]->ifindex);
  229. if (res < 0)
  230. goto nla_put_failure;
  231. res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
  232. if (res < 0)
  233. goto nla_put_failure;
  234. res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
  235. if (res < 0)
  236. goto nla_put_failure;
  237. if (hsr_priv->slave[1])
  238. res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
  239. hsr_priv->slave[1]->ifindex);
  240. genlmsg_end(skb_out, msg_head);
  241. genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
  242. return 0;
  243. invalid:
  244. netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
  245. return 0;
  246. nla_put_failure:
  247. kfree_skb(skb_out);
  248. /* Fall through */
  249. fail:
  250. return res;
  251. }
  252. /* Get a list of MacAddressA of all nodes known to this node (other than self).
  253. */
  254. static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
  255. {
  256. /* For receiving */
  257. struct nlattr *na;
  258. struct net_device *hsr_dev;
  259. /* For sending */
  260. struct sk_buff *skb_out;
  261. void *msg_head;
  262. struct hsr_priv *hsr_priv;
  263. void *pos;
  264. unsigned char addr[ETH_ALEN];
  265. int res;
  266. if (!info)
  267. goto invalid;
  268. na = info->attrs[HSR_A_IFINDEX];
  269. if (!na)
  270. goto invalid;
  271. hsr_dev = __dev_get_by_index(genl_info_net(info),
  272. nla_get_u32(info->attrs[HSR_A_IFINDEX]));
  273. if (!hsr_dev)
  274. goto invalid;
  275. if (!is_hsr_master(hsr_dev))
  276. goto invalid;
  277. /* Send reply */
  278. skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  279. if (!skb_out) {
  280. res = -ENOMEM;
  281. goto fail;
  282. }
  283. msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
  284. info->snd_seq, &hsr_genl_family, 0,
  285. HSR_C_SET_NODE_LIST);
  286. if (!msg_head) {
  287. res = -ENOMEM;
  288. goto nla_put_failure;
  289. }
  290. res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
  291. if (res < 0)
  292. goto nla_put_failure;
  293. hsr_priv = netdev_priv(hsr_dev);
  294. rcu_read_lock();
  295. pos = hsr_get_next_node(hsr_priv, NULL, addr);
  296. while (pos) {
  297. res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
  298. if (res < 0) {
  299. rcu_read_unlock();
  300. goto nla_put_failure;
  301. }
  302. pos = hsr_get_next_node(hsr_priv, pos, addr);
  303. }
  304. rcu_read_unlock();
  305. genlmsg_end(skb_out, msg_head);
  306. genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
  307. return 0;
  308. invalid:
  309. netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
  310. return 0;
  311. nla_put_failure:
  312. kfree_skb(skb_out);
  313. /* Fall through */
  314. fail:
  315. return res;
  316. }
  317. static const struct genl_ops hsr_ops[] = {
  318. {
  319. .cmd = HSR_C_GET_NODE_STATUS,
  320. .flags = 0,
  321. .policy = hsr_genl_policy,
  322. .doit = hsr_get_node_status,
  323. .dumpit = NULL,
  324. },
  325. {
  326. .cmd = HSR_C_GET_NODE_LIST,
  327. .flags = 0,
  328. .policy = hsr_genl_policy,
  329. .doit = hsr_get_node_list,
  330. .dumpit = NULL,
  331. },
  332. };
  333. int __init hsr_netlink_init(void)
  334. {
  335. int rc;
  336. rc = rtnl_link_register(&hsr_link_ops);
  337. if (rc)
  338. goto fail_rtnl_link_register;
  339. rc = genl_register_family_with_ops_groups(&hsr_genl_family, hsr_ops,
  340. hsr_mcgrps);
  341. if (rc)
  342. goto fail_genl_register_family;
  343. return 0;
  344. fail_genl_register_family:
  345. rtnl_link_unregister(&hsr_link_ops);
  346. fail_rtnl_link_register:
  347. return rc;
  348. }
  349. void __exit hsr_netlink_exit(void)
  350. {
  351. genl_unregister_family(&hsr_genl_family);
  352. rtnl_link_unregister(&hsr_link_ops);
  353. }
  354. MODULE_ALIAS_RTNL_LINK("hsr");