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