hsr_netlink.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright 2011-2014 Autronica Fire and Security AS
  3. *
  4. * Author(s):
  5. * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
  6. *
  7. * Routines for handling Netlink messages for HSR and PRP.
  8. */
  9. #include "hsr_netlink.h"
  10. #include <linux/kernel.h>
  11. #include <net/rtnetlink.h>
  12. #include <net/genetlink.h>
  13. #include "hsr_main.h"
  14. #include "hsr_device.h"
  15. #include "hsr_framereg.h"
  16. static const struct nla_policy hsr_policy[IFLA_HSR_MAX + 1] = {
  17. [IFLA_HSR_SLAVE1] = { .type = NLA_U32 },
  18. [IFLA_HSR_SLAVE2] = { .type = NLA_U32 },
  19. [IFLA_HSR_MULTICAST_SPEC] = { .type = NLA_U8 },
  20. [IFLA_HSR_VERSION] = { .type = NLA_U8 },
  21. [IFLA_HSR_SUPERVISION_ADDR] = { .len = ETH_ALEN },
  22. [IFLA_HSR_SEQ_NR] = { .type = NLA_U16 },
  23. [IFLA_HSR_PROTOCOL] = { .type = NLA_U8 },
  24. [IFLA_HSR_INTERLINK] = { .type = NLA_U32 },
  25. };
  26. /* Here, it seems a netdevice has already been allocated for us, and the
  27. * hsr_dev_setup routine has been executed. Nice!
  28. */
  29. static int hsr_newlink(struct net *src_net, struct net_device *dev,
  30. struct nlattr *tb[], struct nlattr *data[],
  31. struct netlink_ext_ack *extack)
  32. {
  33. enum hsr_version proto_version;
  34. unsigned char multicast_spec;
  35. u8 proto = HSR_PROTOCOL_HSR;
  36. struct net_device *link[2], *interlink = NULL;
  37. if (!data) {
  38. NL_SET_ERR_MSG_MOD(extack, "No slave devices specified");
  39. return -EINVAL;
  40. }
  41. if (!data[IFLA_HSR_SLAVE1]) {
  42. NL_SET_ERR_MSG_MOD(extack, "Slave1 device not specified");
  43. return -EINVAL;
  44. }
  45. link[0] = __dev_get_by_index(src_net,
  46. nla_get_u32(data[IFLA_HSR_SLAVE1]));
  47. if (!link[0]) {
  48. NL_SET_ERR_MSG_MOD(extack, "Slave1 does not exist");
  49. return -EINVAL;
  50. }
  51. if (!data[IFLA_HSR_SLAVE2]) {
  52. NL_SET_ERR_MSG_MOD(extack, "Slave2 device not specified");
  53. return -EINVAL;
  54. }
  55. link[1] = __dev_get_by_index(src_net,
  56. nla_get_u32(data[IFLA_HSR_SLAVE2]));
  57. if (!link[1]) {
  58. NL_SET_ERR_MSG_MOD(extack, "Slave2 does not exist");
  59. return -EINVAL;
  60. }
  61. if (link[0] == link[1]) {
  62. NL_SET_ERR_MSG_MOD(extack, "Slave1 and Slave2 are same");
  63. return -EINVAL;
  64. }
  65. if (data[IFLA_HSR_INTERLINK])
  66. interlink = __dev_get_by_index(src_net,
  67. nla_get_u32(data[IFLA_HSR_INTERLINK]));
  68. if (interlink && interlink == link[0]) {
  69. NL_SET_ERR_MSG_MOD(extack, "Interlink and Slave1 are the same");
  70. return -EINVAL;
  71. }
  72. if (interlink && interlink == link[1]) {
  73. NL_SET_ERR_MSG_MOD(extack, "Interlink and Slave2 are the same");
  74. return -EINVAL;
  75. }
  76. if (!data[IFLA_HSR_MULTICAST_SPEC])
  77. multicast_spec = 0;
  78. else
  79. multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
  80. if (data[IFLA_HSR_PROTOCOL])
  81. proto = nla_get_u8(data[IFLA_HSR_PROTOCOL]);
  82. if (proto >= HSR_PROTOCOL_MAX) {
  83. NL_SET_ERR_MSG_MOD(extack, "Unsupported protocol");
  84. return -EINVAL;
  85. }
  86. if (!data[IFLA_HSR_VERSION]) {
  87. proto_version = HSR_V0;
  88. } else {
  89. if (proto == HSR_PROTOCOL_PRP) {
  90. NL_SET_ERR_MSG_MOD(extack, "PRP version unsupported");
  91. return -EINVAL;
  92. }
  93. proto_version = nla_get_u8(data[IFLA_HSR_VERSION]);
  94. if (proto_version > HSR_V1) {
  95. NL_SET_ERR_MSG_MOD(extack,
  96. "Only HSR version 0/1 supported");
  97. return -EINVAL;
  98. }
  99. }
  100. if (proto == HSR_PROTOCOL_PRP) {
  101. proto_version = PRP_V1;
  102. if (interlink) {
  103. NL_SET_ERR_MSG_MOD(extack,
  104. "Interlink only works with HSR");
  105. return -EINVAL;
  106. }
  107. }
  108. return hsr_dev_finalize(dev, link, interlink, multicast_spec,
  109. proto_version, extack);
  110. }
  111. static void hsr_dellink(struct net_device *dev, struct list_head *head)
  112. {
  113. struct hsr_priv *hsr = netdev_priv(dev);
  114. del_timer_sync(&hsr->prune_timer);
  115. del_timer_sync(&hsr->prune_proxy_timer);
  116. del_timer_sync(&hsr->announce_timer);
  117. timer_delete_sync(&hsr->announce_proxy_timer);
  118. hsr_debugfs_term(hsr);
  119. hsr_del_ports(hsr);
  120. hsr_del_self_node(hsr);
  121. hsr_del_nodes(&hsr->node_db);
  122. hsr_del_nodes(&hsr->proxy_node_db);
  123. unregister_netdevice_queue(dev, head);
  124. }
  125. static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
  126. {
  127. struct hsr_priv *hsr = netdev_priv(dev);
  128. u8 proto = HSR_PROTOCOL_HSR;
  129. struct hsr_port *port;
  130. port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  131. if (port) {
  132. if (nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex))
  133. goto nla_put_failure;
  134. }
  135. port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  136. if (port) {
  137. if (nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex))
  138. goto nla_put_failure;
  139. }
  140. if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
  141. hsr->sup_multicast_addr) ||
  142. nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
  143. goto nla_put_failure;
  144. if (hsr->prot_version == PRP_V1)
  145. proto = HSR_PROTOCOL_PRP;
  146. if (nla_put_u8(skb, IFLA_HSR_PROTOCOL, proto))
  147. goto nla_put_failure;
  148. return 0;
  149. nla_put_failure:
  150. return -EMSGSIZE;
  151. }
  152. static struct rtnl_link_ops hsr_link_ops __read_mostly = {
  153. .kind = "hsr",
  154. .maxtype = IFLA_HSR_MAX,
  155. .policy = hsr_policy,
  156. .priv_size = sizeof(struct hsr_priv),
  157. .setup = hsr_dev_setup,
  158. .newlink = hsr_newlink,
  159. .dellink = hsr_dellink,
  160. .fill_info = hsr_fill_info,
  161. };
  162. /* attribute policy */
  163. static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
  164. [HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
  165. [HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
  166. [HSR_A_IFINDEX] = { .type = NLA_U32 },
  167. [HSR_A_IF1_AGE] = { .type = NLA_U32 },
  168. [HSR_A_IF2_AGE] = { .type = NLA_U32 },
  169. [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
  170. [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
  171. };
  172. static struct genl_family hsr_genl_family;
  173. static const struct genl_multicast_group hsr_mcgrps[] = {
  174. { .name = "hsr-network", },
  175. };
  176. /* This is called if for some node with MAC address addr, we only get frames
  177. * over one of the slave interfaces. This would indicate an open network ring
  178. * (i.e. a link has failed somewhere).
  179. */
  180. void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
  181. struct hsr_port *port)
  182. {
  183. struct sk_buff *skb;
  184. void *msg_head;
  185. struct hsr_port *master;
  186. int res;
  187. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  188. if (!skb)
  189. goto fail;
  190. msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0,
  191. HSR_C_RING_ERROR);
  192. if (!msg_head)
  193. goto nla_put_failure;
  194. res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
  195. if (res < 0)
  196. goto nla_put_failure;
  197. res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
  198. if (res < 0)
  199. goto nla_put_failure;
  200. genlmsg_end(skb, msg_head);
  201. genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
  202. return;
  203. nla_put_failure:
  204. kfree_skb(skb);
  205. fail:
  206. rcu_read_lock();
  207. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  208. netdev_warn(master->dev, "Could not send HSR ring error message\n");
  209. rcu_read_unlock();
  210. }
  211. /* This is called when we haven't heard from the node with MAC address addr for
  212. * some time (just before the node is removed from the node table/list).
  213. */
  214. void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
  215. {
  216. struct sk_buff *skb;
  217. void *msg_head;
  218. struct hsr_port *master;
  219. int res;
  220. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  221. if (!skb)
  222. goto fail;
  223. msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
  224. if (!msg_head)
  225. goto nla_put_failure;
  226. res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
  227. if (res < 0)
  228. goto nla_put_failure;
  229. genlmsg_end(skb, msg_head);
  230. genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
  231. return;
  232. nla_put_failure:
  233. kfree_skb(skb);
  234. fail:
  235. rcu_read_lock();
  236. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  237. netdev_warn(master->dev, "Could not send HSR node down\n");
  238. rcu_read_unlock();
  239. }
  240. /* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
  241. * about the status of a specific node in the network, defined by its MAC
  242. * address.
  243. *
  244. * Input: hsr ifindex, node mac address
  245. * Output: hsr ifindex, node mac address (copied from request),
  246. * age of latest frame from node over slave 1, slave 2 [ms]
  247. */
  248. static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
  249. {
  250. /* For receiving */
  251. struct nlattr *na;
  252. struct net_device *hsr_dev;
  253. /* For sending */
  254. struct sk_buff *skb_out;
  255. void *msg_head;
  256. struct hsr_priv *hsr;
  257. struct hsr_port *port;
  258. unsigned char hsr_node_addr_b[ETH_ALEN];
  259. int hsr_node_if1_age;
  260. u16 hsr_node_if1_seq;
  261. int hsr_node_if2_age;
  262. u16 hsr_node_if2_seq;
  263. int addr_b_ifindex;
  264. int res;
  265. if (!info)
  266. goto invalid;
  267. na = info->attrs[HSR_A_IFINDEX];
  268. if (!na)
  269. goto invalid;
  270. na = info->attrs[HSR_A_NODE_ADDR];
  271. if (!na)
  272. goto invalid;
  273. rcu_read_lock();
  274. hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
  275. nla_get_u32(info->attrs[HSR_A_IFINDEX]));
  276. if (!hsr_dev)
  277. goto rcu_unlock;
  278. if (!is_hsr_master(hsr_dev))
  279. goto rcu_unlock;
  280. /* Send reply */
  281. skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  282. if (!skb_out) {
  283. res = -ENOMEM;
  284. goto fail;
  285. }
  286. msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
  287. info->snd_seq, &hsr_genl_family, 0,
  288. HSR_C_SET_NODE_STATUS);
  289. if (!msg_head) {
  290. res = -ENOMEM;
  291. goto nla_put_failure;
  292. }
  293. res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
  294. if (res < 0)
  295. goto nla_put_failure;
  296. hsr = netdev_priv(hsr_dev);
  297. res = hsr_get_node_data(hsr,
  298. (unsigned char *)
  299. nla_data(info->attrs[HSR_A_NODE_ADDR]),
  300. hsr_node_addr_b,
  301. &addr_b_ifindex,
  302. &hsr_node_if1_age,
  303. &hsr_node_if1_seq,
  304. &hsr_node_if2_age,
  305. &hsr_node_if2_seq);
  306. if (res < 0)
  307. goto nla_put_failure;
  308. res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
  309. nla_data(info->attrs[HSR_A_NODE_ADDR]));
  310. if (res < 0)
  311. goto nla_put_failure;
  312. if (addr_b_ifindex > -1) {
  313. res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
  314. hsr_node_addr_b);
  315. if (res < 0)
  316. goto nla_put_failure;
  317. res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX,
  318. addr_b_ifindex);
  319. if (res < 0)
  320. goto nla_put_failure;
  321. }
  322. res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
  323. if (res < 0)
  324. goto nla_put_failure;
  325. res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
  326. if (res < 0)
  327. goto nla_put_failure;
  328. port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  329. if (port)
  330. res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
  331. port->dev->ifindex);
  332. if (res < 0)
  333. goto nla_put_failure;
  334. res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
  335. if (res < 0)
  336. goto nla_put_failure;
  337. res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
  338. if (res < 0)
  339. goto nla_put_failure;
  340. port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  341. if (port)
  342. res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
  343. port->dev->ifindex);
  344. if (res < 0)
  345. goto nla_put_failure;
  346. rcu_read_unlock();
  347. genlmsg_end(skb_out, msg_head);
  348. genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
  349. return 0;
  350. rcu_unlock:
  351. rcu_read_unlock();
  352. invalid:
  353. netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
  354. return 0;
  355. nla_put_failure:
  356. kfree_skb(skb_out);
  357. /* Fall through */
  358. fail:
  359. rcu_read_unlock();
  360. return res;
  361. }
  362. /* Get a list of MacAddressA of all nodes known to this node (including self).
  363. */
  364. static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
  365. {
  366. unsigned char addr[ETH_ALEN];
  367. struct net_device *hsr_dev;
  368. struct sk_buff *skb_out;
  369. struct hsr_priv *hsr;
  370. bool restart = false;
  371. struct nlattr *na;
  372. void *pos = NULL;
  373. void *msg_head;
  374. int res;
  375. if (!info)
  376. goto invalid;
  377. na = info->attrs[HSR_A_IFINDEX];
  378. if (!na)
  379. goto invalid;
  380. rcu_read_lock();
  381. hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
  382. nla_get_u32(info->attrs[HSR_A_IFINDEX]));
  383. if (!hsr_dev)
  384. goto rcu_unlock;
  385. if (!is_hsr_master(hsr_dev))
  386. goto rcu_unlock;
  387. restart:
  388. /* Send reply */
  389. skb_out = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  390. if (!skb_out) {
  391. res = -ENOMEM;
  392. goto fail;
  393. }
  394. msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
  395. info->snd_seq, &hsr_genl_family, 0,
  396. HSR_C_SET_NODE_LIST);
  397. if (!msg_head) {
  398. res = -ENOMEM;
  399. goto nla_put_failure;
  400. }
  401. if (!restart) {
  402. res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
  403. if (res < 0)
  404. goto nla_put_failure;
  405. }
  406. hsr = netdev_priv(hsr_dev);
  407. if (!pos)
  408. pos = hsr_get_next_node(hsr, NULL, addr);
  409. while (pos) {
  410. res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
  411. if (res < 0) {
  412. if (res == -EMSGSIZE) {
  413. genlmsg_end(skb_out, msg_head);
  414. genlmsg_unicast(genl_info_net(info), skb_out,
  415. info->snd_portid);
  416. restart = true;
  417. goto restart;
  418. }
  419. goto nla_put_failure;
  420. }
  421. pos = hsr_get_next_node(hsr, pos, addr);
  422. }
  423. rcu_read_unlock();
  424. genlmsg_end(skb_out, msg_head);
  425. genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
  426. return 0;
  427. rcu_unlock:
  428. rcu_read_unlock();
  429. invalid:
  430. netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
  431. return 0;
  432. nla_put_failure:
  433. nlmsg_free(skb_out);
  434. /* Fall through */
  435. fail:
  436. rcu_read_unlock();
  437. return res;
  438. }
  439. static const struct genl_small_ops hsr_ops[] = {
  440. {
  441. .cmd = HSR_C_GET_NODE_STATUS,
  442. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  443. .flags = 0,
  444. .doit = hsr_get_node_status,
  445. .dumpit = NULL,
  446. },
  447. {
  448. .cmd = HSR_C_GET_NODE_LIST,
  449. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  450. .flags = 0,
  451. .doit = hsr_get_node_list,
  452. .dumpit = NULL,
  453. },
  454. };
  455. static struct genl_family hsr_genl_family __ro_after_init = {
  456. .hdrsize = 0,
  457. .name = "HSR",
  458. .version = 1,
  459. .maxattr = HSR_A_MAX,
  460. .policy = hsr_genl_policy,
  461. .netnsok = true,
  462. .module = THIS_MODULE,
  463. .small_ops = hsr_ops,
  464. .n_small_ops = ARRAY_SIZE(hsr_ops),
  465. .resv_start_op = HSR_C_SET_NODE_LIST + 1,
  466. .mcgrps = hsr_mcgrps,
  467. .n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
  468. };
  469. int __init hsr_netlink_init(void)
  470. {
  471. int rc;
  472. rc = rtnl_link_register(&hsr_link_ops);
  473. if (rc)
  474. goto fail_rtnl_link_register;
  475. rc = genl_register_family(&hsr_genl_family);
  476. if (rc)
  477. goto fail_genl_register_family;
  478. hsr_debugfs_create_root();
  479. return 0;
  480. fail_genl_register_family:
  481. rtnl_link_unregister(&hsr_link_ops);
  482. fail_rtnl_link_register:
  483. return rc;
  484. }
  485. void __exit hsr_netlink_exit(void)
  486. {
  487. genl_unregister_family(&hsr_genl_family);
  488. rtnl_link_unregister(&hsr_link_ops);
  489. }
  490. MODULE_ALIAS_RTNL_LINK("hsr");