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