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 * This file contains device methods for creating, using and destroying 7 * virtual HSR or PRP devices. 8 */ 9 10 #include <linux/netdevice.h> 11 #include <linux/skbuff.h> 12 #include <linux/etherdevice.h> 13 #include <linux/rtnetlink.h> 14 #include <linux/pkt_sched.h> 15 #include "hsr_device.h" 16 #include "hsr_slave.h" 17 #include "hsr_framereg.h" 18 #include "hsr_main.h" 19 #include "hsr_forward.h" 20 21 static bool is_admin_up(struct net_device *dev) 22 { 23 return dev && (dev->flags & IFF_UP); 24 } 25 26 static bool is_slave_up(struct net_device *dev) 27 { 28 return dev && is_admin_up(dev) && netif_oper_up(dev); 29 } 30 31 static void hsr_set_operstate(struct hsr_port *master, bool has_carrier) 32 { 33 struct net_device *dev = master->dev; 34 35 if (!is_admin_up(dev)) { 36 netdev_set_operstate(dev, IF_OPER_DOWN); 37 return; 38 } 39 40 if (has_carrier) 41 netdev_set_operstate(dev, IF_OPER_UP); 42 else 43 netdev_set_operstate(dev, IF_OPER_LOWERLAYERDOWN); 44 } 45 46 static bool hsr_check_carrier(struct hsr_port *master) 47 { 48 struct hsr_port *port; 49 50 ASSERT_RTNL(); 51 52 hsr_for_each_port(master->hsr, port) { 53 if (port->type != HSR_PT_MASTER && is_slave_up(port->dev)) { 54 netif_carrier_on(master->dev); 55 return true; 56 } 57 } 58 59 netif_carrier_off(master->dev); 60 61 return false; 62 } 63 64 static void hsr_check_announce(struct net_device *hsr_dev, 65 unsigned char old_operstate) 66 { 67 struct hsr_priv *hsr; 68 69 hsr = netdev_priv(hsr_dev); 70 71 if (READ_ONCE(hsr_dev->operstate) == IF_OPER_UP && old_operstate != IF_OPER_UP) { 72 /* Went up */ 73 hsr->announce_count = 0; 74 mod_timer(&hsr->announce_timer, 75 jiffies + msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL)); 76 } 77 78 if (READ_ONCE(hsr_dev->operstate) != IF_OPER_UP && old_operstate == IF_OPER_UP) 79 /* Went down */ 80 del_timer(&hsr->announce_timer); 81 } 82 83 void hsr_check_carrier_and_operstate(struct hsr_priv *hsr) 84 { 85 struct hsr_port *master; 86 unsigned char old_operstate; 87 bool has_carrier; 88 89 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 90 /* netif_stacked_transfer_operstate() cannot be used here since 91 * it doesn't set IF_OPER_LOWERLAYERDOWN (?) 92 */ 93 old_operstate = READ_ONCE(master->dev->operstate); 94 has_carrier = hsr_check_carrier(master); 95 hsr_set_operstate(master, has_carrier); 96 hsr_check_announce(master->dev, old_operstate); 97 } 98 99 int hsr_get_max_mtu(struct hsr_priv *hsr) 100 { 101 unsigned int mtu_max; 102 struct hsr_port *port; 103 104 mtu_max = ETH_DATA_LEN; 105 hsr_for_each_port(hsr, port) 106 if (port->type != HSR_PT_MASTER) 107 mtu_max = min(port->dev->mtu, mtu_max); 108 109 if (mtu_max < HSR_HLEN) 110 return 0; 111 return mtu_max - HSR_HLEN; 112 } 113 114 static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu) 115 { 116 struct hsr_priv *hsr; 117 118 hsr = netdev_priv(dev); 119 120 if (new_mtu > hsr_get_max_mtu(hsr)) { 121 netdev_info(dev, "A HSR master's MTU cannot be greater than the smallest MTU of its slaves minus the HSR Tag length (%d octets).\n", 122 HSR_HLEN); 123 return -EINVAL; 124 } 125 126 dev->mtu = new_mtu; 127 128 return 0; 129 } 130 131 static int hsr_dev_open(struct net_device *dev) 132 { 133 struct hsr_priv *hsr; 134 struct hsr_port *port; 135 char designation; 136 137 hsr = netdev_priv(dev); 138 designation = '\0'; 139 140 hsr_for_each_port(hsr, port) { 141 if (port->type == HSR_PT_MASTER) 142 continue; 143 switch (port->type) { 144 case HSR_PT_SLAVE_A: 145 designation = 'A'; 146 break; 147 case HSR_PT_SLAVE_B: 148 designation = 'B'; 149 break; 150 default: 151 designation = '?'; 152 } 153 if (!is_slave_up(port->dev)) 154 netdev_warn(dev, "Slave %c (%s) is not up; please bring it up to get a fully working HSR network\n", 155 designation, port->dev->name); 156 } 157 158 if (designation == '\0') 159 netdev_warn(dev, "No slave devices configured\n"); 160 161 return 0; 162 } 163 164 static int hsr_dev_close(struct net_device *dev) 165 { 166 struct hsr_port *port; 167 struct hsr_priv *hsr; 168 169 hsr = netdev_priv(dev); 170 hsr_for_each_port(hsr, port) { 171 if (port->type == HSR_PT_MASTER) 172 continue; 173 switch (port->type) { 174 case HSR_PT_SLAVE_A: 175 case HSR_PT_SLAVE_B: 176 dev_uc_unsync(port->dev, dev); 177 dev_mc_unsync(port->dev, dev); 178 break; 179 default: 180 break; 181 } 182 } 183 184 return 0; 185 } 186 187 static netdev_features_t hsr_features_recompute(struct hsr_priv *hsr, 188 netdev_features_t features) 189 { 190 netdev_features_t mask; 191 struct hsr_port *port; 192 193 mask = features; 194 195 /* Mask out all features that, if supported by one device, should be 196 * enabled for all devices (see NETIF_F_ONE_FOR_ALL). 197 * 198 * Anything that's off in mask will not be enabled - so only things 199 * that were in features originally, and also is in NETIF_F_ONE_FOR_ALL, 200 * may become enabled. 201 */ 202 features &= ~NETIF_F_ONE_FOR_ALL; 203 hsr_for_each_port(hsr, port) 204 features = netdev_increment_features(features, 205 port->dev->features, 206 mask); 207 208 return features; 209 } 210 211 static netdev_features_t hsr_fix_features(struct net_device *dev, 212 netdev_features_t features) 213 { 214 struct hsr_priv *hsr = netdev_priv(dev); 215 216 return hsr_features_recompute(hsr, features); 217 } 218 219 static netdev_tx_t hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev) 220 { 221 struct hsr_priv *hsr = netdev_priv(dev); 222 struct hsr_port *master; 223 224 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 225 if (master) { 226 skb->dev = master->dev; 227 skb_reset_mac_header(skb); 228 skb_reset_mac_len(skb); 229 spin_lock_bh(&hsr->seqnr_lock); 230 hsr_forward_skb(skb, master); 231 spin_unlock_bh(&hsr->seqnr_lock); 232 } else { 233 dev_core_stats_tx_dropped_inc(dev); 234 dev_kfree_skb_any(skb); 235 } 236 return NETDEV_TX_OK; 237 } 238 239 static const struct header_ops hsr_header_ops = { 240 .create = eth_header, 241 .parse = eth_header_parse, 242 }; 243 244 static struct sk_buff *hsr_init_skb(struct hsr_port *master) 245 { 246 struct hsr_priv *hsr = master->hsr; 247 struct sk_buff *skb; 248 int hlen, tlen; 249 250 hlen = LL_RESERVED_SPACE(master->dev); 251 tlen = master->dev->needed_tailroom; 252 /* skb size is same for PRP/HSR frames, only difference 253 * being, for PRP it is a trailer and for HSR it is a 254 * header 255 */ 256 skb = dev_alloc_skb(sizeof(struct hsr_sup_tag) + 257 sizeof(struct hsr_sup_payload) + hlen + tlen); 258 259 if (!skb) 260 return skb; 261 262 skb_reserve(skb, hlen); 263 skb->dev = master->dev; 264 skb->priority = TC_PRIO_CONTROL; 265 266 if (dev_hard_header(skb, skb->dev, ETH_P_PRP, 267 hsr->sup_multicast_addr, 268 skb->dev->dev_addr, skb->len) <= 0) 269 goto out; 270 271 skb_reset_mac_header(skb); 272 skb_reset_mac_len(skb); 273 skb_reset_network_header(skb); 274 skb_reset_transport_header(skb); 275 276 return skb; 277 out: 278 kfree_skb(skb); 279 280 return NULL; 281 } 282 283 static void send_hsr_supervision_frame(struct hsr_port *master, 284 unsigned long *interval) 285 { 286 struct hsr_priv *hsr = master->hsr; 287 __u8 type = HSR_TLV_LIFE_CHECK; 288 struct hsr_sup_payload *hsr_sp; 289 struct hsr_sup_tag *hsr_stag; 290 struct sk_buff *skb; 291 292 *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL); 293 if (hsr->announce_count < 3 && hsr->prot_version == 0) { 294 type = HSR_TLV_ANNOUNCE; 295 *interval = msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL); 296 hsr->announce_count++; 297 } 298 299 skb = hsr_init_skb(master); 300 if (!skb) { 301 netdev_warn_once(master->dev, "HSR: Could not send supervision frame\n"); 302 return; 303 } 304 305 hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag)); 306 set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf)); 307 set_hsr_stag_HSR_ver(hsr_stag, hsr->prot_version); 308 309 /* From HSRv1 on we have separate supervision sequence numbers. */ 310 spin_lock_bh(&hsr->seqnr_lock); 311 if (hsr->prot_version > 0) { 312 hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr); 313 hsr->sup_sequence_nr++; 314 } else { 315 hsr_stag->sequence_nr = htons(hsr->sequence_nr); 316 hsr->sequence_nr++; 317 } 318 319 hsr_stag->tlv.HSR_TLV_type = type; 320 /* TODO: Why 12 in HSRv0? */ 321 hsr_stag->tlv.HSR_TLV_length = hsr->prot_version ? 322 sizeof(struct hsr_sup_payload) : 12; 323 324 /* Payload: MacAddressA */ 325 hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload)); 326 ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr); 327 328 if (skb_put_padto(skb, ETH_ZLEN)) { 329 spin_unlock_bh(&hsr->seqnr_lock); 330 return; 331 } 332 333 hsr_forward_skb(skb, master); 334 spin_unlock_bh(&hsr->seqnr_lock); 335 return; 336 } 337 338 static void send_prp_supervision_frame(struct hsr_port *master, 339 unsigned long *interval) 340 { 341 struct hsr_priv *hsr = master->hsr; 342 struct hsr_sup_payload *hsr_sp; 343 struct hsr_sup_tag *hsr_stag; 344 struct sk_buff *skb; 345 346 skb = hsr_init_skb(master); 347 if (!skb) { 348 netdev_warn_once(master->dev, "PRP: Could not send supervision frame\n"); 349 return; 350 } 351 352 *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL); 353 hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag)); 354 set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf)); 355 set_hsr_stag_HSR_ver(hsr_stag, (hsr->prot_version ? 1 : 0)); 356 357 /* From HSRv1 on we have separate supervision sequence numbers. */ 358 spin_lock_bh(&hsr->seqnr_lock); 359 hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr); 360 hsr->sup_sequence_nr++; 361 hsr_stag->tlv.HSR_TLV_type = PRP_TLV_LIFE_CHECK_DD; 362 hsr_stag->tlv.HSR_TLV_length = sizeof(struct hsr_sup_payload); 363 364 /* Payload: MacAddressA */ 365 hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload)); 366 ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr); 367 368 if (skb_put_padto(skb, ETH_ZLEN)) { 369 spin_unlock_bh(&hsr->seqnr_lock); 370 return; 371 } 372 373 hsr_forward_skb(skb, master); 374 spin_unlock_bh(&hsr->seqnr_lock); 375 } 376 377 /* Announce (supervision frame) timer function 378 */ 379 static void hsr_announce(struct timer_list *t) 380 { 381 struct hsr_priv *hsr; 382 struct hsr_port *master; 383 unsigned long interval; 384 385 hsr = from_timer(hsr, t, announce_timer); 386 387 rcu_read_lock(); 388 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 389 hsr->proto_ops->send_sv_frame(master, &interval); 390 391 if (is_admin_up(master->dev)) 392 mod_timer(&hsr->announce_timer, jiffies + interval); 393 394 rcu_read_unlock(); 395 } 396 397 void hsr_del_ports(struct hsr_priv *hsr) 398 { 399 struct hsr_port *port; 400 401 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A); 402 if (port) 403 hsr_del_port(port); 404 405 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B); 406 if (port) 407 hsr_del_port(port); 408 409 port = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 410 if (port) 411 hsr_del_port(port); 412 } 413 414 static void hsr_set_rx_mode(struct net_device *dev) 415 { 416 struct hsr_port *port; 417 struct hsr_priv *hsr; 418 419 hsr = netdev_priv(dev); 420 421 hsr_for_each_port(hsr, port) { 422 if (port->type == HSR_PT_MASTER) 423 continue; 424 switch (port->type) { 425 case HSR_PT_SLAVE_A: 426 case HSR_PT_SLAVE_B: 427 dev_mc_sync_multiple(port->dev, dev); 428 dev_uc_sync_multiple(port->dev, dev); 429 break; 430 default: 431 break; 432 } 433 } 434 } 435 436 static void hsr_change_rx_flags(struct net_device *dev, int change) 437 { 438 struct hsr_port *port; 439 struct hsr_priv *hsr; 440 441 hsr = netdev_priv(dev); 442 443 hsr_for_each_port(hsr, port) { 444 if (port->type == HSR_PT_MASTER) 445 continue; 446 switch (port->type) { 447 case HSR_PT_SLAVE_A: 448 case HSR_PT_SLAVE_B: 449 if (change & IFF_ALLMULTI) 450 dev_set_allmulti(port->dev, 451 dev->flags & 452 IFF_ALLMULTI ? 1 : -1); 453 break; 454 default: 455 break; 456 } 457 } 458 } 459 460 static const struct net_device_ops hsr_device_ops = { 461 .ndo_change_mtu = hsr_dev_change_mtu, 462 .ndo_open = hsr_dev_open, 463 .ndo_stop = hsr_dev_close, 464 .ndo_start_xmit = hsr_dev_xmit, 465 .ndo_change_rx_flags = hsr_change_rx_flags, 466 .ndo_fix_features = hsr_fix_features, 467 .ndo_set_rx_mode = hsr_set_rx_mode, 468 }; 469 470 static const struct device_type hsr_type = { 471 .name = "hsr", 472 }; 473 474 static struct hsr_proto_ops hsr_ops = { 475 .send_sv_frame = send_hsr_supervision_frame, 476 .create_tagged_frame = hsr_create_tagged_frame, 477 .get_untagged_frame = hsr_get_untagged_frame, 478 .drop_frame = hsr_drop_frame, 479 .fill_frame_info = hsr_fill_frame_info, 480 .invalid_dan_ingress_frame = hsr_invalid_dan_ingress_frame, 481 }; 482 483 static struct hsr_proto_ops prp_ops = { 484 .send_sv_frame = send_prp_supervision_frame, 485 .create_tagged_frame = prp_create_tagged_frame, 486 .get_untagged_frame = prp_get_untagged_frame, 487 .drop_frame = prp_drop_frame, 488 .fill_frame_info = prp_fill_frame_info, 489 .handle_san_frame = prp_handle_san_frame, 490 .update_san_info = prp_update_san_info, 491 }; 492 493 void hsr_dev_setup(struct net_device *dev) 494 { 495 eth_hw_addr_random(dev); 496 497 ether_setup(dev); 498 dev->min_mtu = 0; 499 dev->header_ops = &hsr_header_ops; 500 dev->netdev_ops = &hsr_device_ops; 501 SET_NETDEV_DEVTYPE(dev, &hsr_type); 502 dev->priv_flags |= IFF_NO_QUEUE | IFF_DISABLE_NETPOLL; 503 504 dev->needs_free_netdev = true; 505 506 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA | 507 NETIF_F_GSO_MASK | NETIF_F_HW_CSUM | 508 NETIF_F_HW_VLAN_CTAG_TX; 509 510 dev->features = dev->hw_features; 511 512 /* Prevent recursive tx locking */ 513 dev->features |= NETIF_F_LLTX; 514 /* VLAN on top of HSR needs testing and probably some work on 515 * hsr_header_create() etc. 516 */ 517 dev->features |= NETIF_F_VLAN_CHALLENGED; 518 /* Not sure about this. Taken from bridge code. netdev_features.h says 519 * it means "Does not change network namespaces". 520 */ 521 dev->features |= NETIF_F_NETNS_LOCAL; 522 } 523 524 /* Return true if dev is a HSR master; return false otherwise. 525 */ 526 bool is_hsr_master(struct net_device *dev) 527 { 528 return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit); 529 } 530 EXPORT_SYMBOL(is_hsr_master); 531 532 /* Default multicast address for HSR Supervision frames */ 533 static const unsigned char def_multicast_addr[ETH_ALEN] __aligned(2) = { 534 0x01, 0x15, 0x4e, 0x00, 0x01, 0x00 535 }; 536 537 int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2], 538 unsigned char multicast_spec, u8 protocol_version, 539 struct netlink_ext_ack *extack) 540 { 541 bool unregister = false; 542 struct hsr_priv *hsr; 543 int res; 544 545 hsr = netdev_priv(hsr_dev); 546 INIT_LIST_HEAD(&hsr->ports); 547 INIT_LIST_HEAD(&hsr->node_db); 548 spin_lock_init(&hsr->list_lock); 549 550 eth_hw_addr_set(hsr_dev, slave[0]->dev_addr); 551 552 /* initialize protocol specific functions */ 553 if (protocol_version == PRP_V1) { 554 /* For PRP, lan_id has most significant 3 bits holding 555 * the net_id of PRP_LAN_ID 556 */ 557 hsr->net_id = PRP_LAN_ID << 1; 558 hsr->proto_ops = &prp_ops; 559 } else { 560 hsr->proto_ops = &hsr_ops; 561 } 562 563 /* Make sure we recognize frames from ourselves in hsr_rcv() */ 564 res = hsr_create_self_node(hsr, hsr_dev->dev_addr, 565 slave[1]->dev_addr); 566 if (res < 0) 567 return res; 568 569 spin_lock_init(&hsr->seqnr_lock); 570 /* Overflow soon to find bugs easier: */ 571 hsr->sequence_nr = HSR_SEQNR_START; 572 hsr->sup_sequence_nr = HSR_SUP_SEQNR_START; 573 574 timer_setup(&hsr->announce_timer, hsr_announce, 0); 575 timer_setup(&hsr->prune_timer, hsr_prune_nodes, 0); 576 577 ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr); 578 hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec; 579 580 hsr->prot_version = protocol_version; 581 582 /* Make sure the 1st call to netif_carrier_on() gets through */ 583 netif_carrier_off(hsr_dev); 584 585 res = hsr_add_port(hsr, hsr_dev, HSR_PT_MASTER, extack); 586 if (res) 587 goto err_add_master; 588 589 /* HSR forwarding offload supported in lower device? */ 590 if ((slave[0]->features & NETIF_F_HW_HSR_FWD) && 591 (slave[1]->features & NETIF_F_HW_HSR_FWD)) 592 hsr->fwd_offloaded = true; 593 594 res = register_netdevice(hsr_dev); 595 if (res) 596 goto err_unregister; 597 598 unregister = true; 599 600 res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A, extack); 601 if (res) 602 goto err_unregister; 603 604 res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B, extack); 605 if (res) 606 goto err_unregister; 607 608 hsr_debugfs_init(hsr, hsr_dev); 609 mod_timer(&hsr->prune_timer, jiffies + msecs_to_jiffies(PRUNE_PERIOD)); 610 611 return 0; 612 613 err_unregister: 614 hsr_del_ports(hsr); 615 err_add_master: 616 hsr_del_self_node(hsr); 617 618 if (unregister) 619 unregister_netdevice(hsr_dev); 620 return res; 621 } 622