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