hsr_netlink.c 11 KB

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