hsr_netlink.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. .id = GENL_ID_GENERATE,
  114. .hdrsize = 0,
  115. .name = "HSR",
  116. .version = 1,
  117. .maxattr = HSR_A_MAX,
  118. };
  119. static const struct genl_multicast_group hsr_mcgrps[] = {
  120. { .name = "hsr-network", },
  121. };
  122. /* This is called if for some node with MAC address addr, we only get frames
  123. * over one of the slave interfaces. This would indicate an open network ring
  124. * (i.e. a link has failed somewhere).
  125. */
  126. void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
  127. struct hsr_port *port)
  128. {
  129. struct sk_buff *skb;
  130. void *msg_head;
  131. struct hsr_port *master;
  132. int res;
  133. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  134. if (!skb)
  135. goto fail;
  136. msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_RING_ERROR);
  137. if (!msg_head)
  138. goto nla_put_failure;
  139. res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
  140. if (res < 0)
  141. goto nla_put_failure;
  142. res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
  143. if (res < 0)
  144. goto nla_put_failure;
  145. genlmsg_end(skb, msg_head);
  146. genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
  147. return;
  148. nla_put_failure:
  149. kfree_skb(skb);
  150. fail:
  151. rcu_read_lock();
  152. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  153. netdev_warn(master->dev, "Could not send HSR ring error message\n");
  154. rcu_read_unlock();
  155. }
  156. /* This is called when we haven't heard from the node with MAC address addr for
  157. * some time (just before the node is removed from the node table/list).
  158. */
  159. void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
  160. {
  161. struct sk_buff *skb;
  162. void *msg_head;
  163. struct hsr_port *master;
  164. int res;
  165. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  166. if (!skb)
  167. goto fail;
  168. msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
  169. if (!msg_head)
  170. goto nla_put_failure;
  171. res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
  172. if (res < 0)
  173. goto nla_put_failure;
  174. genlmsg_end(skb, msg_head);
  175. genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
  176. return;
  177. nla_put_failure:
  178. kfree_skb(skb);
  179. fail:
  180. rcu_read_lock();
  181. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  182. netdev_warn(master->dev, "Could not send HSR node down\n");
  183. rcu_read_unlock();
  184. }
  185. /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
  186. * about the status of a specific node in the network, defined by its MAC
  187. * address.
  188. *
  189. * Input: hsr ifindex, node mac address
  190. * Output: hsr ifindex, node mac address (copied from request),
  191. * age of latest frame from node over slave 1, slave 2 [ms]
  192. */
  193. static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
  194. {
  195. /* For receiving */
  196. struct nlattr *na;
  197. struct net_device *hsr_dev;
  198. /* For sending */
  199. struct sk_buff *skb_out;
  200. void *msg_head;
  201. struct hsr_priv *hsr;
  202. struct hsr_port *port;
  203. unsigned char hsr_node_addr_b[ETH_ALEN];
  204. int hsr_node_if1_age;
  205. u16 hsr_node_if1_seq;
  206. int hsr_node_if2_age;
  207. u16 hsr_node_if2_seq;
  208. int addr_b_ifindex;
  209. int res;
  210. if (!info)
  211. goto invalid;
  212. na = info->attrs[HSR_A_IFINDEX];
  213. if (!na)
  214. goto invalid;
  215. na = info->attrs[HSR_A_NODE_ADDR];
  216. if (!na)
  217. goto invalid;
  218. hsr_dev = __dev_get_by_index(genl_info_net(info),
  219. nla_get_u32(info->attrs[HSR_A_IFINDEX]));
  220. if (!hsr_dev)
  221. goto invalid;
  222. if (!is_hsr_master(hsr_dev))
  223. goto invalid;
  224. /* Send reply */
  225. skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  226. if (!skb_out) {
  227. res = -ENOMEM;
  228. goto fail;
  229. }
  230. msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
  231. info->snd_seq, &hsr_genl_family, 0,
  232. HSR_C_SET_NODE_STATUS);
  233. if (!msg_head) {
  234. res = -ENOMEM;
  235. goto nla_put_failure;
  236. }
  237. res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
  238. if (res < 0)
  239. goto nla_put_failure;
  240. hsr = netdev_priv(hsr_dev);
  241. res = hsr_get_node_data(hsr,
  242. (unsigned char *) nla_data(info->attrs[HSR_A_NODE_ADDR]),
  243. hsr_node_addr_b,
  244. &addr_b_ifindex,
  245. &hsr_node_if1_age,
  246. &hsr_node_if1_seq,
  247. &hsr_node_if2_age,
  248. &hsr_node_if2_seq);
  249. if (res < 0)
  250. goto nla_put_failure;
  251. res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
  252. nla_data(info->attrs[HSR_A_NODE_ADDR]));
  253. if (res < 0)
  254. goto nla_put_failure;
  255. if (addr_b_ifindex > -1) {
  256. res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
  257. hsr_node_addr_b);
  258. if (res < 0)
  259. goto nla_put_failure;
  260. res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX, addr_b_ifindex);
  261. if (res < 0)
  262. goto nla_put_failure;
  263. }
  264. res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
  265. if (res < 0)
  266. goto nla_put_failure;
  267. res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
  268. if (res < 0)
  269. goto nla_put_failure;
  270. rcu_read_lock();
  271. port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  272. if (port)
  273. res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
  274. port->dev->ifindex);
  275. rcu_read_unlock();
  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. rcu_read_lock();
  285. port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  286. if (port)
  287. res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
  288. port->dev->ifindex);
  289. rcu_read_unlock();
  290. if (res < 0)
  291. goto nla_put_failure;
  292. genlmsg_end(skb_out, msg_head);
  293. genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
  294. return 0;
  295. invalid:
  296. netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
  297. return 0;
  298. nla_put_failure:
  299. kfree_skb(skb_out);
  300. /* Fall through */
  301. fail:
  302. return res;
  303. }
  304. /* Get a list of MacAddressA of all nodes known to this node (including self).
  305. */
  306. static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
  307. {
  308. /* For receiving */
  309. struct nlattr *na;
  310. struct net_device *hsr_dev;
  311. /* For sending */
  312. struct sk_buff *skb_out;
  313. void *msg_head;
  314. struct hsr_priv *hsr;
  315. void *pos;
  316. unsigned char addr[ETH_ALEN];
  317. int res;
  318. if (!info)
  319. goto invalid;
  320. na = info->attrs[HSR_A_IFINDEX];
  321. if (!na)
  322. goto invalid;
  323. hsr_dev = __dev_get_by_index(genl_info_net(info),
  324. nla_get_u32(info->attrs[HSR_A_IFINDEX]));
  325. if (!hsr_dev)
  326. goto invalid;
  327. if (!is_hsr_master(hsr_dev))
  328. goto invalid;
  329. /* Send reply */
  330. skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  331. if (!skb_out) {
  332. res = -ENOMEM;
  333. goto fail;
  334. }
  335. msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
  336. info->snd_seq, &hsr_genl_family, 0,
  337. HSR_C_SET_NODE_LIST);
  338. if (!msg_head) {
  339. res = -ENOMEM;
  340. goto nla_put_failure;
  341. }
  342. res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
  343. if (res < 0)
  344. goto nla_put_failure;
  345. hsr = netdev_priv(hsr_dev);
  346. rcu_read_lock();
  347. pos = hsr_get_next_node(hsr, NULL, addr);
  348. while (pos) {
  349. res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
  350. if (res < 0) {
  351. rcu_read_unlock();
  352. goto nla_put_failure;
  353. }
  354. pos = hsr_get_next_node(hsr, pos, addr);
  355. }
  356. rcu_read_unlock();
  357. genlmsg_end(skb_out, msg_head);
  358. genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
  359. return 0;
  360. invalid:
  361. netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
  362. return 0;
  363. nla_put_failure:
  364. kfree_skb(skb_out);
  365. /* Fall through */
  366. fail:
  367. return res;
  368. }
  369. static const struct genl_ops hsr_ops[] = {
  370. {
  371. .cmd = HSR_C_GET_NODE_STATUS,
  372. .flags = 0,
  373. .policy = hsr_genl_policy,
  374. .doit = hsr_get_node_status,
  375. .dumpit = NULL,
  376. },
  377. {
  378. .cmd = HSR_C_GET_NODE_LIST,
  379. .flags = 0,
  380. .policy = hsr_genl_policy,
  381. .doit = hsr_get_node_list,
  382. .dumpit = NULL,
  383. },
  384. };
  385. int __init hsr_netlink_init(void)
  386. {
  387. int rc;
  388. rc = rtnl_link_register(&hsr_link_ops);
  389. if (rc)
  390. goto fail_rtnl_link_register;
  391. rc = genl_register_family_with_ops_groups(&hsr_genl_family, hsr_ops,
  392. hsr_mcgrps);
  393. if (rc)
  394. goto fail_genl_register_family;
  395. return 0;
  396. fail_genl_register_family:
  397. rtnl_link_unregister(&hsr_link_ops);
  398. fail_rtnl_link_register:
  399. return rc;
  400. }
  401. void __exit hsr_netlink_exit(void)
  402. {
  403. genl_unregister_family(&hsr_genl_family);
  404. rtnl_link_unregister(&hsr_link_ops);
  405. }
  406. MODULE_ALIAS_RTNL_LINK("hsr");