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