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