hsr_netlink.c 11 KB

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