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