hsr_netlink.c 14 KB

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