hsr_prp_netlink.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright 2011-2014 Autronica Fire and Security AS
  3. * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
  4. *
  5. * Author(s):
  6. * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
  7. *
  8. * This file contains device methods for creating, using and destroying
  9. * virtual HSR devices.
  10. */
  11. #include <linux/kernel.h>
  12. #include <net/rtnetlink.h>
  13. #include <net/genetlink.h>
  14. #include "hsr_prp_main.h"
  15. #include "hsr_prp_netlink.h"
  16. #include "hsr_prp_device.h"
  17. #include "hsr_prp_framereg.h"
  18. /* Here, it seems a netdevice has already been allocated for us, and the
  19. * hsr_prp_dev_setup routine has been executed. Nice!
  20. */
  21. int hsr_prp_newlink(int proto, struct net *src_net,
  22. struct net_device *dev, struct nlattr *tb[],
  23. struct nlattr *data[],
  24. struct netlink_ext_ack *extack)
  25. {
  26. struct net_device *link[2];
  27. unsigned char mc_lsb, version = 0;
  28. char *sproto = (proto == PRP) ? "PRP" : "HSR";
  29. unsigned short vid = 0;
  30. unsigned char pcp = 0, dei = 0;
  31. bool sv_vlan_tag_needed = false;
  32. if (!data) {
  33. netdev_info(dev, "%s: No slave devices specified\n", sproto);
  34. return -EINVAL;
  35. }
  36. if (!data[IFLA_HSR_PRP_SLAVE1]) {
  37. netdev_info(dev, "%s: Slave1 device not specified\n", sproto);
  38. return -EINVAL;
  39. }
  40. link[0] = __dev_get_by_index(src_net,
  41. nla_get_u32(data[IFLA_HSR_PRP_SLAVE1]));
  42. if (!data[IFLA_HSR_PRP_SLAVE2]) {
  43. netdev_info(dev, "%s: Slave2 device not specified\n", sproto);
  44. return -EINVAL;
  45. }
  46. link[1] = __dev_get_by_index(src_net,
  47. nla_get_u32(data[IFLA_HSR_PRP_SLAVE2]));
  48. if (!link[0] || !link[1])
  49. return -ENODEV;
  50. if (link[0] == link[1])
  51. return -EINVAL;
  52. if (!data[IFLA_HSR_PRP_SF_MC_ADDR_LSB])
  53. mc_lsb = 0;
  54. else
  55. mc_lsb = nla_get_u8(data[IFLA_HSR_PRP_SF_MC_ADDR_LSB]);
  56. if (proto == PRP) {
  57. version = PRP_V1;
  58. } else {
  59. if (!data[IFLA_HSR_VERSION])
  60. version = 0;
  61. else
  62. version = nla_get_u8(data[IFLA_HSR_VERSION]);
  63. }
  64. if (data[IFLA_HSR_PRP_SV_VID]) {
  65. sv_vlan_tag_needed = true;
  66. vid = nla_get_u16(data[IFLA_HSR_PRP_SV_VID]);
  67. }
  68. if (data[IFLA_HSR_PRP_SV_PCP]) {
  69. sv_vlan_tag_needed = true;
  70. pcp = nla_get_u8(data[IFLA_HSR_PRP_SV_PCP]);
  71. }
  72. if (data[IFLA_HSR_PRP_SV_DEI]) {
  73. sv_vlan_tag_needed = true;
  74. dei = nla_get_u8(data[IFLA_HSR_PRP_SV_DEI]);
  75. }
  76. if (sv_vlan_tag_needed &&
  77. (vid >= (VLAN_N_VID - 1) || dei > 1 || pcp > 7)) {
  78. netdev_info(dev,
  79. "%s: wrong vlan params: vid %d, pcp %d, dei %d\n",
  80. sproto, vid, pcp, dei);
  81. return -EINVAL;
  82. }
  83. return hsr_prp_dev_finalize(dev, link, mc_lsb, version,
  84. sv_vlan_tag_needed, vid, pcp, dei);
  85. }
  86. int hsr_prp_fill_info(struct sk_buff *skb, const struct net_device *dev)
  87. {
  88. struct hsr_prp_priv *priv;
  89. struct hsr_prp_port *port;
  90. int res;
  91. priv = netdev_priv(dev);
  92. res = 0;
  93. rcu_read_lock();
  94. port = hsr_prp_get_port(priv, HSR_PRP_PT_SLAVE_A);
  95. if (port)
  96. res = nla_put_u32(skb, IFLA_HSR_PRP_SLAVE1, port->dev->ifindex);
  97. rcu_read_unlock();
  98. if (res)
  99. goto nla_put_failure;
  100. rcu_read_lock();
  101. port = hsr_prp_get_port(priv, HSR_PRP_PT_SLAVE_B);
  102. if (port)
  103. res = nla_put_u32(skb, IFLA_HSR_PRP_SLAVE2, port->dev->ifindex);
  104. rcu_read_unlock();
  105. if (res)
  106. goto nla_put_failure;
  107. if (nla_put(skb, IFLA_HSR_PRP_SF_MC_ADDR, ETH_ALEN,
  108. priv->sup_multicast_addr) ||
  109. nla_put_u16(skb, IFLA_HSR_PRP_SEQ_NR, priv->sequence_nr))
  110. goto nla_put_failure;
  111. return 0;
  112. nla_put_failure:
  113. return -EMSGSIZE;
  114. }
  115. /* This is called when we haven't heard from the node with MAC address addr for
  116. * some time (just before the node is removed from the node table/list).
  117. */
  118. void hsr_prp_nl_nodedown(struct hsr_prp_priv *priv,
  119. struct genl_family *genl_family,
  120. unsigned char addr[ETH_ALEN])
  121. {
  122. struct sk_buff *skb;
  123. void *msg_head;
  124. struct hsr_prp_port *master;
  125. int res;
  126. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  127. if (!skb)
  128. goto fail;
  129. msg_head = genlmsg_put(skb, 0, 0, genl_family, 0, HSR_PRP_C_NODE_DOWN);
  130. if (!msg_head)
  131. goto nla_put_failure;
  132. res = nla_put(skb, HSR_PRP_A_NODE_ADDR, ETH_ALEN, addr);
  133. if (res < 0)
  134. goto nla_put_failure;
  135. genlmsg_end(skb, msg_head);
  136. genlmsg_multicast(genl_family, skb, 0, 0, GFP_ATOMIC);
  137. return;
  138. nla_put_failure:
  139. kfree_skb(skb);
  140. fail:
  141. rcu_read_lock();
  142. master = hsr_prp_get_port(priv, HSR_PRP_PT_MASTER);
  143. netdev_warn(master->dev, "Could not send PRP node down\n");
  144. rcu_read_unlock();
  145. }
  146. /* PRP_C_GET_NODE_STATUS lets userspace query the internal PRP node table
  147. * about the status of a specific node in the network, defined by its MAC
  148. * address.
  149. *
  150. * Input: hsr ifindex, node mac address
  151. * Output: hsr ifindex, node mac address (copied from request),
  152. * age of latest frame from node over slave 1, slave 2 [ms]
  153. */
  154. int hsr_prp_get_node_status(struct genl_family *genl_family,
  155. struct sk_buff *skb_in,
  156. struct genl_info *info)
  157. {
  158. /* For receiving */
  159. struct nlattr *na;
  160. struct net_device *ndev;
  161. /* For sending */
  162. struct sk_buff *skb_out;
  163. void *msg_head;
  164. struct hsr_prp_priv *priv;
  165. struct hsr_prp_port *port;
  166. unsigned char hsr_node_addr_b[ETH_ALEN];
  167. int hsr_node_if1_age;
  168. u16 hsr_node_if1_seq;
  169. int hsr_node_if2_age;
  170. u16 hsr_node_if2_seq;
  171. int addr_b_ifindex;
  172. int res;
  173. if (!info)
  174. goto invalid;
  175. na = info->attrs[HSR_PRP_A_IFINDEX];
  176. if (!na)
  177. goto invalid;
  178. na = info->attrs[HSR_PRP_A_NODE_ADDR];
  179. if (!na)
  180. goto invalid;
  181. ndev = __dev_get_by_index(genl_info_net(info),
  182. nla_get_u32(info->attrs[HSR_PRP_A_IFINDEX]));
  183. if (!ndev)
  184. goto invalid;
  185. if (!is_hsr_prp_master(ndev))
  186. goto invalid;
  187. /* Send reply */
  188. skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  189. if (!skb_out) {
  190. res = -ENOMEM;
  191. goto fail;
  192. }
  193. msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
  194. info->snd_seq, genl_family, 0,
  195. HSR_PRP_C_SET_NODE_STATUS);
  196. if (!msg_head) {
  197. res = -ENOMEM;
  198. goto nla_put_failure;
  199. }
  200. res = nla_put_u32(skb_out, HSR_PRP_A_IFINDEX, ndev->ifindex);
  201. if (res < 0)
  202. goto nla_put_failure;
  203. priv = netdev_priv(ndev);
  204. res = hsr_prp_get_node_data(priv,
  205. (unsigned char *)
  206. nla_data(info->attrs[HSR_PRP_A_NODE_ADDR]),
  207. hsr_node_addr_b, &addr_b_ifindex,
  208. &hsr_node_if1_age, &hsr_node_if1_seq,
  209. &hsr_node_if2_age, &hsr_node_if2_seq);
  210. if (res < 0)
  211. goto nla_put_failure;
  212. res = nla_put(skb_out, HSR_PRP_A_NODE_ADDR, ETH_ALEN,
  213. nla_data(info->attrs[HSR_PRP_A_NODE_ADDR]));
  214. if (res < 0)
  215. goto nla_put_failure;
  216. if (addr_b_ifindex > -1) {
  217. res = nla_put(skb_out, HSR_PRP_A_NODE_ADDR_B, ETH_ALEN,
  218. hsr_node_addr_b);
  219. if (res < 0)
  220. goto nla_put_failure;
  221. res = nla_put_u32(skb_out, HSR_PRP_A_ADDR_B_IFINDEX,
  222. addr_b_ifindex);
  223. if (res < 0)
  224. goto nla_put_failure;
  225. }
  226. res = nla_put_u32(skb_out, HSR_PRP_A_IF1_AGE, hsr_node_if1_age);
  227. if (res < 0)
  228. goto nla_put_failure;
  229. res = nla_put_u16(skb_out, HSR_PRP_A_IF1_SEQ, hsr_node_if1_seq);
  230. if (res < 0)
  231. goto nla_put_failure;
  232. rcu_read_lock();
  233. port = hsr_prp_get_port(priv, HSR_PRP_PT_SLAVE_A);
  234. if (port)
  235. res = nla_put_u32(skb_out, HSR_PRP_A_IF1_IFINDEX,
  236. port->dev->ifindex);
  237. rcu_read_unlock();
  238. if (res < 0)
  239. goto nla_put_failure;
  240. res = nla_put_u32(skb_out, HSR_PRP_A_IF2_AGE, hsr_node_if2_age);
  241. if (res < 0)
  242. goto nla_put_failure;
  243. res = nla_put_u16(skb_out, HSR_PRP_A_IF2_SEQ, hsr_node_if2_seq);
  244. if (res < 0)
  245. goto nla_put_failure;
  246. rcu_read_lock();
  247. port = hsr_prp_get_port(priv, HSR_PRP_PT_SLAVE_B);
  248. if (port)
  249. res = nla_put_u32(skb_out, HSR_PRP_A_IF2_IFINDEX,
  250. port->dev->ifindex);
  251. rcu_read_unlock();
  252. if (res < 0)
  253. goto nla_put_failure;
  254. genlmsg_end(skb_out, msg_head);
  255. genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
  256. return 0;
  257. invalid:
  258. netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
  259. return 0;
  260. nla_put_failure:
  261. kfree_skb(skb_out);
  262. /* Fall through */
  263. fail:
  264. return res;
  265. }
  266. /* Get a list of mac_address_a of all nodes known to this node (including self).
  267. */
  268. int hsr_prp_get_node_list(struct genl_family *genl_family,
  269. struct sk_buff *skb_in, struct genl_info *info)
  270. {
  271. /* For receiving */
  272. struct nlattr *na;
  273. struct net_device *ndev;
  274. /* For sending */
  275. struct sk_buff *skb_out;
  276. void *msg_head;
  277. struct hsr_prp_priv *priv;
  278. void *pos;
  279. unsigned char addr[ETH_ALEN];
  280. int res;
  281. if (!info)
  282. goto invalid;
  283. na = info->attrs[HSR_PRP_A_IFINDEX];
  284. if (!na)
  285. goto invalid;
  286. ndev = __dev_get_by_index(genl_info_net(info),
  287. nla_get_u32(info->attrs[HSR_PRP_A_IFINDEX]));
  288. if (!ndev)
  289. goto invalid;
  290. if (!is_hsr_prp_master(ndev))
  291. goto invalid;
  292. /* Send reply */
  293. skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  294. if (!skb_out) {
  295. res = -ENOMEM;
  296. goto fail;
  297. }
  298. msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
  299. info->snd_seq, genl_family, 0,
  300. HSR_PRP_C_SET_NODE_LIST);
  301. if (!msg_head) {
  302. res = -ENOMEM;
  303. goto nla_put_failure;
  304. }
  305. res = nla_put_u32(skb_out, HSR_PRP_A_IFINDEX, ndev->ifindex);
  306. if (res < 0)
  307. goto nla_put_failure;
  308. priv = netdev_priv(ndev);
  309. rcu_read_lock();
  310. pos = hsr_prp_get_next_node(priv, NULL, addr);
  311. while (pos) {
  312. if (!hsr_prp_addr_is_self(priv, addr)) {
  313. res = nla_put(skb_out, HSR_PRP_A_NODE_ADDR,
  314. ETH_ALEN, addr);
  315. if (res < 0) {
  316. rcu_read_unlock();
  317. goto nla_put_failure;
  318. }
  319. }
  320. pos = hsr_prp_get_next_node(priv, pos, addr);
  321. }
  322. rcu_read_unlock();
  323. genlmsg_end(skb_out, msg_head);
  324. genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
  325. return 0;
  326. invalid:
  327. netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
  328. return 0;
  329. nla_put_failure:
  330. kfree_skb(skb_out);
  331. /* Fall through */
  332. fail:
  333. return res;
  334. }