hsr_netlink.c 11 KB

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