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