hsr_netlink.c 11 KB

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