prp_netlink.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * prp_netlink.c: Routines for handling Netlink messages for PRP.
  3. * This is based on hsr_netlink.c from Arvid Brodin, arvid.brodin@alten.se
  4. *
  5. * Copyright (C) 2017 Texas Instruments Incorporated
  6. *
  7. * Author(s):
  8. * Murali Karicheri <m-karicheri2@ti.com?
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation version 2.
  13. *
  14. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  15. * kind, whether express or implied; without even the implied warranty
  16. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include "prp_netlink.h"
  20. #include <linux/kernel.h>
  21. #include <net/rtnetlink.h>
  22. #include <net/genetlink.h>
  23. #include "hsr_prp_main.h"
  24. #include "hsr_prp_device.h"
  25. #include "hsr_prp_framereg.h"
  26. static const struct nla_policy prp_policy[IFLA_PRP_MAX + 1] = {
  27. [IFLA_PRP_SLAVE1] = { .type = NLA_U32 },
  28. [IFLA_PRP_SLAVE2] = { .type = NLA_U32 },
  29. [IFLA_PRP_MULTICAST_SPEC] = { .type = NLA_U8 },
  30. [IFLA_PRP_SUPERVISION_ADDR] = { .type = NLA_BINARY,
  31. .len = ETH_ALEN },
  32. [IFLA_PRP_SEQ_NR] = { .type = NLA_U16 },
  33. };
  34. /* Here, it seems a netdevice has already been allocated for us, and the
  35. * hsr_dev_setup routine has been executed. Nice!
  36. */
  37. static int prp_newlink(struct net *src_net, struct net_device *dev,
  38. struct nlattr *tb[], struct nlattr *data[])
  39. {
  40. struct net_device *link[2];
  41. unsigned char multicast_spec;
  42. if (!data) {
  43. netdev_info(dev, "PRP: No slave devices specified\n");
  44. return -EINVAL;
  45. }
  46. if (!data[IFLA_PRP_SLAVE1]) {
  47. netdev_info(dev, "PRP: Slave1 device not specified\n");
  48. return -EINVAL;
  49. }
  50. link[0] = __dev_get_by_index(src_net,
  51. nla_get_u32(data[IFLA_PRP_SLAVE1]));
  52. if (!data[IFLA_PRP_SLAVE2]) {
  53. netdev_info(dev, "PRP: Slave2 device not specified\n");
  54. return -EINVAL;
  55. }
  56. link[1] = __dev_get_by_index(src_net,
  57. nla_get_u32(data[IFLA_PRP_SLAVE2]));
  58. if (!link[0] || !link[1])
  59. return -ENODEV;
  60. if (link[0] == link[1])
  61. return -EINVAL;
  62. if (!data[IFLA_PRP_MULTICAST_SPEC])
  63. multicast_spec = 0;
  64. else
  65. multicast_spec = nla_get_u8(data[IFLA_PRP_MULTICAST_SPEC]);
  66. return hsr_prp_dev_finalize(dev, link, multicast_spec, PRP_V1);
  67. }
  68. static int prp_fill_info(struct sk_buff *skb, const struct net_device *dev)
  69. {
  70. struct hsr_prp_priv *priv;
  71. struct hsr_prp_port *port;
  72. int res;
  73. priv = netdev_priv(dev);
  74. res = 0;
  75. rcu_read_lock();
  76. port = hsr_prp_get_port(priv, HSR_PRP_PT_SLAVE_A);
  77. if (port)
  78. res = nla_put_u32(skb, IFLA_PRP_SLAVE1, port->dev->ifindex);
  79. rcu_read_unlock();
  80. if (res)
  81. goto nla_put_failure;
  82. rcu_read_lock();
  83. port = hsr_prp_get_port(priv, HSR_PRP_PT_SLAVE_B);
  84. if (port)
  85. res = nla_put_u32(skb, IFLA_PRP_SLAVE2, port->dev->ifindex);
  86. rcu_read_unlock();
  87. if (res)
  88. goto nla_put_failure;
  89. if (nla_put(skb, IFLA_PRP_SUPERVISION_ADDR, ETH_ALEN,
  90. priv->sup_multicast_addr) ||
  91. nla_put_u16(skb, IFLA_PRP_SEQ_NR, priv->sequence_nr))
  92. goto nla_put_failure;
  93. return 0;
  94. nla_put_failure:
  95. return -EMSGSIZE;
  96. }
  97. static struct rtnl_link_ops prp_link_ops __read_mostly = {
  98. .kind = "prp",
  99. .maxtype = IFLA_PRP_MAX,
  100. .policy = prp_policy,
  101. .priv_size = sizeof(struct hsr_prp_priv),
  102. .setup = prp_dev_setup,
  103. .newlink = prp_newlink,
  104. .fill_info = prp_fill_info,
  105. };
  106. /* attribute policy */
  107. /* NLA_BINARY missing in libnl; use NLA_UNSPEC in userspace instead. */
  108. static const struct nla_policy prp_genl_policy[PRP_A_MAX + 1] = {
  109. [PRP_A_NODE_ADDR] = { .type = NLA_BINARY, .len = ETH_ALEN },
  110. [PRP_A_NODE_ADDR_B] = { .type = NLA_BINARY, .len = ETH_ALEN },
  111. [PRP_A_IFINDEX] = { .type = NLA_U32 },
  112. [PRP_A_IF1_AGE] = { .type = NLA_U32 },
  113. [PRP_A_IF2_AGE] = { .type = NLA_U32 },
  114. [PRP_A_IF1_SEQ] = { .type = NLA_U16 },
  115. [PRP_A_IF2_SEQ] = { .type = NLA_U16 },
  116. };
  117. static struct genl_family prp_genl_family = {
  118. .id = GENL_ID_GENERATE,
  119. .hdrsize = 0,
  120. .name = "PRP",
  121. .version = 1,
  122. .maxattr = PRP_A_MAX,
  123. };
  124. static const struct genl_multicast_group hsr_mcgrps[] = {
  125. { .name = "prp-network", },
  126. };
  127. /* This is called if for some node with MAC address addr, we only get frames
  128. * over one of the slave interfaces. This would indicate an open network ring
  129. * (i.e. a link has failed somewhere).
  130. */
  131. /* This is called when we haven't heard from the node with MAC address addr for
  132. * some time (just before the node is removed from the node table/list).
  133. */
  134. void prp_nl_nodedown(struct hsr_prp_priv *priv, unsigned char addr[ETH_ALEN])
  135. {
  136. struct sk_buff *skb;
  137. void *msg_head;
  138. struct hsr_prp_port *master;
  139. int res;
  140. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  141. if (!skb)
  142. goto fail;
  143. msg_head = genlmsg_put(skb, 0, 0, &prp_genl_family, 0, PRP_C_NODE_DOWN);
  144. if (!msg_head)
  145. goto nla_put_failure;
  146. res = nla_put(skb, PRP_A_NODE_ADDR, ETH_ALEN, addr);
  147. if (res < 0)
  148. goto nla_put_failure;
  149. genlmsg_end(skb, msg_head);
  150. genlmsg_multicast(&prp_genl_family, skb, 0, 0, GFP_ATOMIC);
  151. return;
  152. nla_put_failure:
  153. kfree_skb(skb);
  154. fail:
  155. rcu_read_lock();
  156. master = hsr_prp_get_port(priv, HSR_PRP_PT_MASTER);
  157. netdev_warn(master->dev, "Could not send PRP node down\n");
  158. rcu_read_unlock();
  159. }
  160. /* PRP_C_GET_NODE_STATUS lets userspace query the internal PRP node table
  161. * about the status of a specific node in the network, defined by its MAC
  162. * address.
  163. *
  164. * Input: hsr ifindex, node mac address
  165. * Output: hsr ifindex, node mac address (copied from request),
  166. * age of latest frame from node over slave 1, slave 2 [ms]
  167. */
  168. static int prp_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
  169. {
  170. /* For receiving */
  171. struct nlattr *na;
  172. struct net_device *hsr_dev;
  173. /* For sending */
  174. struct sk_buff *skb_out;
  175. void *msg_head;
  176. struct hsr_prp_priv *priv;
  177. struct hsr_prp_port *port;
  178. unsigned char hsr_node_addr_b[ETH_ALEN];
  179. int hsr_node_if1_age;
  180. u16 hsr_node_if1_seq;
  181. int hsr_node_if2_age;
  182. u16 hsr_node_if2_seq;
  183. int addr_b_ifindex;
  184. int res;
  185. if (!info)
  186. goto invalid;
  187. na = info->attrs[PRP_A_IFINDEX];
  188. if (!na)
  189. goto invalid;
  190. na = info->attrs[PRP_A_NODE_ADDR];
  191. if (!na)
  192. goto invalid;
  193. hsr_dev = __dev_get_by_index(genl_info_net(info),
  194. nla_get_u32(info->attrs[PRP_A_IFINDEX]));
  195. if (!hsr_dev)
  196. goto invalid;
  197. if (!is_hsr_prp_master(hsr_dev))
  198. goto invalid;
  199. /* Send reply */
  200. skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  201. if (!skb_out) {
  202. res = -ENOMEM;
  203. goto fail;
  204. }
  205. msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
  206. info->snd_seq, &prp_genl_family, 0,
  207. PRP_C_SET_NODE_STATUS);
  208. if (!msg_head) {
  209. res = -ENOMEM;
  210. goto nla_put_failure;
  211. }
  212. res = nla_put_u32(skb_out, PRP_A_IFINDEX, hsr_dev->ifindex);
  213. if (res < 0)
  214. goto nla_put_failure;
  215. priv = netdev_priv(hsr_dev);
  216. res = hsr_prp_get_node_data(
  217. priv,
  218. (unsigned char *)nla_data(info->attrs[PRP_A_NODE_ADDR]),
  219. hsr_node_addr_b,
  220. &addr_b_ifindex,
  221. &hsr_node_if1_age,
  222. &hsr_node_if1_seq,
  223. &hsr_node_if2_age,
  224. &hsr_node_if2_seq);
  225. if (res < 0)
  226. goto nla_put_failure;
  227. res = nla_put(skb_out, PRP_A_NODE_ADDR, ETH_ALEN,
  228. nla_data(info->attrs[PRP_A_NODE_ADDR]));
  229. if (res < 0)
  230. goto nla_put_failure;
  231. if (addr_b_ifindex > -1) {
  232. res = nla_put(skb_out, PRP_A_NODE_ADDR_B, ETH_ALEN,
  233. hsr_node_addr_b);
  234. if (res < 0)
  235. goto nla_put_failure;
  236. res = nla_put_u32(skb_out, PRP_A_ADDR_B_IFINDEX,
  237. addr_b_ifindex);
  238. if (res < 0)
  239. goto nla_put_failure;
  240. }
  241. res = nla_put_u32(skb_out, PRP_A_IF1_AGE, hsr_node_if1_age);
  242. if (res < 0)
  243. goto nla_put_failure;
  244. res = nla_put_u16(skb_out, PRP_A_IF1_SEQ, hsr_node_if1_seq);
  245. if (res < 0)
  246. goto nla_put_failure;
  247. rcu_read_lock();
  248. port = hsr_prp_get_port(priv, HSR_PRP_PT_SLAVE_A);
  249. if (port)
  250. res = nla_put_u32(skb_out, PRP_A_IF1_IFINDEX,
  251. port->dev->ifindex);
  252. rcu_read_unlock();
  253. if (res < 0)
  254. goto nla_put_failure;
  255. res = nla_put_u32(skb_out, PRP_A_IF2_AGE, hsr_node_if2_age);
  256. if (res < 0)
  257. goto nla_put_failure;
  258. res = nla_put_u16(skb_out, PRP_A_IF2_SEQ, hsr_node_if2_seq);
  259. if (res < 0)
  260. goto nla_put_failure;
  261. rcu_read_lock();
  262. port = hsr_prp_get_port(priv, HSR_PRP_PT_SLAVE_B);
  263. if (port)
  264. res = nla_put_u32(skb_out, PRP_A_IF2_IFINDEX,
  265. port->dev->ifindex);
  266. rcu_read_unlock();
  267. if (res < 0)
  268. goto nla_put_failure;
  269. genlmsg_end(skb_out, msg_head);
  270. genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
  271. return 0;
  272. invalid:
  273. netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
  274. return 0;
  275. nla_put_failure:
  276. kfree_skb(skb_out);
  277. /* Fall through */
  278. fail:
  279. return res;
  280. }
  281. /* Get a list of MacAddressA of all nodes known to this node (including self).
  282. */
  283. static int prp_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
  284. {
  285. /* For receiving */
  286. struct nlattr *na;
  287. struct net_device *hsr_dev;
  288. /* For sending */
  289. struct sk_buff *skb_out;
  290. void *msg_head;
  291. struct hsr_prp_priv *priv;
  292. void *pos;
  293. unsigned char addr[ETH_ALEN];
  294. int res;
  295. if (!info)
  296. goto invalid;
  297. na = info->attrs[PRP_A_IFINDEX];
  298. if (!na)
  299. goto invalid;
  300. hsr_dev = __dev_get_by_index(genl_info_net(info),
  301. nla_get_u32(info->attrs[PRP_A_IFINDEX]));
  302. if (!hsr_dev)
  303. goto invalid;
  304. if (!is_hsr_prp_master(hsr_dev))
  305. goto invalid;
  306. /* Send reply */
  307. skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  308. if (!skb_out) {
  309. res = -ENOMEM;
  310. goto fail;
  311. }
  312. msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
  313. info->snd_seq, &prp_genl_family, 0,
  314. PRP_C_SET_NODE_LIST);
  315. if (!msg_head) {
  316. res = -ENOMEM;
  317. goto nla_put_failure;
  318. }
  319. res = nla_put_u32(skb_out, PRP_A_IFINDEX, hsr_dev->ifindex);
  320. if (res < 0)
  321. goto nla_put_failure;
  322. priv = netdev_priv(hsr_dev);
  323. rcu_read_lock();
  324. pos = hsr_prp_get_next_node(priv, NULL, addr);
  325. while (pos) {
  326. if (!hsr_prp_addr_is_self(priv, addr)) {
  327. res = nla_put(skb_out, PRP_A_NODE_ADDR, ETH_ALEN, addr);
  328. if (res < 0) {
  329. rcu_read_unlock();
  330. goto nla_put_failure;
  331. }
  332. }
  333. pos = hsr_prp_get_next_node(priv, pos, addr);
  334. }
  335. rcu_read_unlock();
  336. genlmsg_end(skb_out, msg_head);
  337. genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
  338. return 0;
  339. invalid:
  340. netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
  341. return 0;
  342. nla_put_failure:
  343. kfree_skb(skb_out);
  344. /* Fall through */
  345. fail:
  346. return res;
  347. }
  348. static const struct genl_ops hsr_ops[] = {
  349. {
  350. .cmd = PRP_C_GET_NODE_STATUS,
  351. .flags = 0,
  352. .policy = prp_genl_policy,
  353. .doit = prp_get_node_status,
  354. .dumpit = NULL,
  355. },
  356. {
  357. .cmd = PRP_C_GET_NODE_LIST,
  358. .flags = 0,
  359. .policy = prp_genl_policy,
  360. .doit = prp_get_node_list,
  361. .dumpit = NULL,
  362. },
  363. };
  364. int __init prp_netlink_init(void)
  365. {
  366. int rc;
  367. rc = rtnl_link_register(&prp_link_ops);
  368. if (rc)
  369. goto fail_rtnl_link_register;
  370. rc = genl_register_family_with_ops_groups(&prp_genl_family, hsr_ops,
  371. hsr_mcgrps);
  372. if (rc)
  373. goto fail_genl_register_family;
  374. return 0;
  375. fail_genl_register_family:
  376. rtnl_link_unregister(&prp_link_ops);
  377. fail_rtnl_link_register:
  378. return rc;
  379. }
  380. void __exit prp_netlink_exit(void)
  381. {
  382. genl_unregister_family(&prp_genl_family);
  383. rtnl_link_unregister(&prp_link_ops);
  384. }
  385. MODULE_ALIAS_RTNL_LINK("prp");