hsr_netlink.c 11 KB

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