hsr_netlink.c 12 KB

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