0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/if_ether.h>
0015 #include <linux/etherdevice.h>
0016 #include <linux/slab.h>
0017 #include <linux/rculist.h>
0018 #include <linux/jhash.h>
0019 #include "hsr_main.h"
0020 #include "hsr_framereg.h"
0021 #include "hsr_netlink.h"
0022
0023 #ifdef CONFIG_LOCKDEP
0024 int lockdep_hsr_is_held(spinlock_t *lock)
0025 {
0026 return lockdep_is_held(lock);
0027 }
0028 #endif
0029
0030 u32 hsr_mac_hash(struct hsr_priv *hsr, const unsigned char *addr)
0031 {
0032 u32 hash = jhash(addr, ETH_ALEN, hsr->hash_seed);
0033
0034 return reciprocal_scale(hash, hsr->hash_buckets);
0035 }
0036
0037 struct hsr_node *hsr_node_get_first(struct hlist_head *head, spinlock_t *lock)
0038 {
0039 struct hlist_node *first;
0040
0041 first = rcu_dereference_bh_check(hlist_first_rcu(head),
0042 lockdep_hsr_is_held(lock));
0043 if (first)
0044 return hlist_entry(first, struct hsr_node, mac_list);
0045
0046 return NULL;
0047 }
0048
0049
0050
0051
0052 static bool seq_nr_after(u16 a, u16 b)
0053 {
0054
0055
0056
0057 if ((int)b - a == 32768)
0058 return false;
0059
0060 return (((s16)(b - a)) < 0);
0061 }
0062
0063 #define seq_nr_before(a, b) seq_nr_after((b), (a))
0064 #define seq_nr_before_or_eq(a, b) (!seq_nr_after((a), (b)))
0065
0066 bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr)
0067 {
0068 struct hsr_node *node;
0069
0070 node = hsr_node_get_first(&hsr->self_node_db, &hsr->list_lock);
0071 if (!node) {
0072 WARN_ONCE(1, "HSR: No self node\n");
0073 return false;
0074 }
0075
0076 if (ether_addr_equal(addr, node->macaddress_A))
0077 return true;
0078 if (ether_addr_equal(addr, node->macaddress_B))
0079 return true;
0080
0081 return false;
0082 }
0083
0084
0085
0086 static struct hsr_node *find_node_by_addr_A(struct hlist_head *node_db,
0087 const unsigned char addr[ETH_ALEN])
0088 {
0089 struct hsr_node *node;
0090
0091 hlist_for_each_entry_rcu(node, node_db, mac_list) {
0092 if (ether_addr_equal(node->macaddress_A, addr))
0093 return node;
0094 }
0095
0096 return NULL;
0097 }
0098
0099
0100
0101
0102 int hsr_create_self_node(struct hsr_priv *hsr,
0103 const unsigned char addr_a[ETH_ALEN],
0104 const unsigned char addr_b[ETH_ALEN])
0105 {
0106 struct hlist_head *self_node_db = &hsr->self_node_db;
0107 struct hsr_node *node, *oldnode;
0108
0109 node = kmalloc(sizeof(*node), GFP_KERNEL);
0110 if (!node)
0111 return -ENOMEM;
0112
0113 ether_addr_copy(node->macaddress_A, addr_a);
0114 ether_addr_copy(node->macaddress_B, addr_b);
0115
0116 spin_lock_bh(&hsr->list_lock);
0117 oldnode = hsr_node_get_first(self_node_db, &hsr->list_lock);
0118 if (oldnode) {
0119 hlist_replace_rcu(&oldnode->mac_list, &node->mac_list);
0120 spin_unlock_bh(&hsr->list_lock);
0121 kfree_rcu(oldnode, rcu_head);
0122 } else {
0123 hlist_add_tail_rcu(&node->mac_list, self_node_db);
0124 spin_unlock_bh(&hsr->list_lock);
0125 }
0126
0127 return 0;
0128 }
0129
0130 void hsr_del_self_node(struct hsr_priv *hsr)
0131 {
0132 struct hlist_head *self_node_db = &hsr->self_node_db;
0133 struct hsr_node *node;
0134
0135 spin_lock_bh(&hsr->list_lock);
0136 node = hsr_node_get_first(self_node_db, &hsr->list_lock);
0137 if (node) {
0138 hlist_del_rcu(&node->mac_list);
0139 kfree_rcu(node, rcu_head);
0140 }
0141 spin_unlock_bh(&hsr->list_lock);
0142 }
0143
0144 void hsr_del_nodes(struct hlist_head *node_db)
0145 {
0146 struct hsr_node *node;
0147 struct hlist_node *tmp;
0148
0149 hlist_for_each_entry_safe(node, tmp, node_db, mac_list)
0150 kfree_rcu(node, rcu_head);
0151 }
0152
0153 void prp_handle_san_frame(bool san, enum hsr_port_type port,
0154 struct hsr_node *node)
0155 {
0156
0157 if (port == HSR_PT_SLAVE_A) {
0158 node->san_a = true;
0159 return;
0160 }
0161
0162 if (port == HSR_PT_SLAVE_B)
0163 node->san_b = true;
0164 }
0165
0166
0167
0168
0169
0170 static struct hsr_node *hsr_add_node(struct hsr_priv *hsr,
0171 struct hlist_head *node_db,
0172 unsigned char addr[],
0173 u16 seq_out, bool san,
0174 enum hsr_port_type rx_port)
0175 {
0176 struct hsr_node *new_node, *node;
0177 unsigned long now;
0178 int i;
0179
0180 new_node = kzalloc(sizeof(*new_node), GFP_ATOMIC);
0181 if (!new_node)
0182 return NULL;
0183
0184 ether_addr_copy(new_node->macaddress_A, addr);
0185
0186
0187
0188
0189 now = jiffies;
0190 for (i = 0; i < HSR_PT_PORTS; i++) {
0191 new_node->time_in[i] = now;
0192 new_node->time_out[i] = now;
0193 }
0194 for (i = 0; i < HSR_PT_PORTS; i++)
0195 new_node->seq_out[i] = seq_out;
0196
0197 if (san && hsr->proto_ops->handle_san_frame)
0198 hsr->proto_ops->handle_san_frame(san, rx_port, new_node);
0199
0200 spin_lock_bh(&hsr->list_lock);
0201 hlist_for_each_entry_rcu(node, node_db, mac_list,
0202 lockdep_hsr_is_held(&hsr->list_lock)) {
0203 if (ether_addr_equal(node->macaddress_A, addr))
0204 goto out;
0205 if (ether_addr_equal(node->macaddress_B, addr))
0206 goto out;
0207 }
0208 hlist_add_tail_rcu(&new_node->mac_list, node_db);
0209 spin_unlock_bh(&hsr->list_lock);
0210 return new_node;
0211 out:
0212 spin_unlock_bh(&hsr->list_lock);
0213 kfree(new_node);
0214 return node;
0215 }
0216
0217 void prp_update_san_info(struct hsr_node *node, bool is_sup)
0218 {
0219 if (!is_sup)
0220 return;
0221
0222 node->san_a = false;
0223 node->san_b = false;
0224 }
0225
0226
0227
0228 struct hsr_node *hsr_get_node(struct hsr_port *port, struct hlist_head *node_db,
0229 struct sk_buff *skb, bool is_sup,
0230 enum hsr_port_type rx_port)
0231 {
0232 struct hsr_priv *hsr = port->hsr;
0233 struct hsr_node *node;
0234 struct ethhdr *ethhdr;
0235 struct prp_rct *rct;
0236 bool san = false;
0237 u16 seq_out;
0238
0239 if (!skb_mac_header_was_set(skb))
0240 return NULL;
0241
0242 ethhdr = (struct ethhdr *)skb_mac_header(skb);
0243
0244 hlist_for_each_entry_rcu(node, node_db, mac_list) {
0245 if (ether_addr_equal(node->macaddress_A, ethhdr->h_source)) {
0246 if (hsr->proto_ops->update_san_info)
0247 hsr->proto_ops->update_san_info(node, is_sup);
0248 return node;
0249 }
0250 if (ether_addr_equal(node->macaddress_B, ethhdr->h_source)) {
0251 if (hsr->proto_ops->update_san_info)
0252 hsr->proto_ops->update_san_info(node, is_sup);
0253 return node;
0254 }
0255 }
0256
0257
0258
0259
0260 if (ethhdr->h_proto == htons(ETH_P_PRP) ||
0261 ethhdr->h_proto == htons(ETH_P_HSR)) {
0262
0263
0264
0265 seq_out = hsr_get_skb_sequence_nr(skb) - 1;
0266 } else {
0267 rct = skb_get_PRP_rct(skb);
0268 if (rct && prp_check_lsdu_size(skb, rct, is_sup)) {
0269 seq_out = prp_get_skb_sequence_nr(rct);
0270 } else {
0271 if (rx_port != HSR_PT_MASTER)
0272 san = true;
0273 seq_out = HSR_SEQNR_START;
0274 }
0275 }
0276
0277 return hsr_add_node(hsr, node_db, ethhdr->h_source, seq_out,
0278 san, rx_port);
0279 }
0280
0281
0282
0283
0284
0285 void hsr_handle_sup_frame(struct hsr_frame_info *frame)
0286 {
0287 struct hsr_node *node_curr = frame->node_src;
0288 struct hsr_port *port_rcv = frame->port_rcv;
0289 struct hsr_priv *hsr = port_rcv->hsr;
0290 struct hsr_sup_payload *hsr_sp;
0291 struct hsr_sup_tlv *hsr_sup_tlv;
0292 struct hsr_node *node_real;
0293 struct sk_buff *skb = NULL;
0294 struct hlist_head *node_db;
0295 struct ethhdr *ethhdr;
0296 int i;
0297 unsigned int pull_size = 0;
0298 unsigned int total_pull_size = 0;
0299 u32 hash;
0300
0301
0302
0303
0304
0305 if (frame->skb_hsr)
0306 skb = frame->skb_hsr;
0307 else if (frame->skb_prp)
0308 skb = frame->skb_prp;
0309 else if (frame->skb_std)
0310 skb = frame->skb_std;
0311 if (!skb)
0312 return;
0313
0314
0315 pull_size = sizeof(struct ethhdr);
0316 skb_pull(skb, pull_size);
0317 total_pull_size += pull_size;
0318
0319 ethhdr = (struct ethhdr *)skb_mac_header(skb);
0320
0321
0322 if (ethhdr->h_proto == htons(ETH_P_HSR)) {
0323 pull_size = sizeof(struct ethhdr);
0324 skb_pull(skb, pull_size);
0325 total_pull_size += pull_size;
0326 }
0327
0328
0329 pull_size = sizeof(struct hsr_tag);
0330 skb_pull(skb, pull_size);
0331 total_pull_size += pull_size;
0332
0333
0334 hsr_sp = (struct hsr_sup_payload *)skb->data;
0335
0336
0337 node_db = port_rcv->hsr->node_db;
0338 hash = hsr_mac_hash(hsr, hsr_sp->macaddress_A);
0339 node_real = find_node_by_addr_A(&node_db[hash], hsr_sp->macaddress_A);
0340 if (!node_real)
0341
0342 node_real = hsr_add_node(hsr, &node_db[hash],
0343 hsr_sp->macaddress_A,
0344 HSR_SEQNR_START - 1, true,
0345 port_rcv->type);
0346 if (!node_real)
0347 goto done;
0348 if (node_real == node_curr)
0349
0350 goto done;
0351
0352
0353 pull_size = sizeof(struct hsr_sup_payload);
0354 skb_pull(skb, pull_size);
0355 total_pull_size += pull_size;
0356
0357
0358 hsr_sup_tlv = (struct hsr_sup_tlv *)skb->data;
0359
0360 if (hsr_sup_tlv->HSR_TLV_type == PRP_TLV_REDBOX_MAC) {
0361
0362
0363
0364
0365 if (hsr_sup_tlv->HSR_TLV_length != 6)
0366 goto done;
0367
0368
0369 pull_size = sizeof(struct hsr_sup_tlv);
0370 skb_pull(skb, pull_size);
0371 total_pull_size += pull_size;
0372
0373
0374 hsr_sp = (struct hsr_sup_payload *)skb->data;
0375
0376
0377 if (!ether_addr_equal(node_real->macaddress_A,
0378 hsr_sp->macaddress_A)) {
0379
0380 goto done;
0381 }
0382 }
0383
0384 ether_addr_copy(node_real->macaddress_B, ethhdr->h_source);
0385 for (i = 0; i < HSR_PT_PORTS; i++) {
0386 if (!node_curr->time_in_stale[i] &&
0387 time_after(node_curr->time_in[i], node_real->time_in[i])) {
0388 node_real->time_in[i] = node_curr->time_in[i];
0389 node_real->time_in_stale[i] =
0390 node_curr->time_in_stale[i];
0391 }
0392 if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i]))
0393 node_real->seq_out[i] = node_curr->seq_out[i];
0394 }
0395 node_real->addr_B_port = port_rcv->type;
0396
0397 spin_lock_bh(&hsr->list_lock);
0398 hlist_del_rcu(&node_curr->mac_list);
0399 spin_unlock_bh(&hsr->list_lock);
0400 kfree_rcu(node_curr, rcu_head);
0401
0402 done:
0403
0404 skb_push(skb, total_pull_size);
0405 }
0406
0407
0408
0409
0410
0411
0412
0413 void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
0414 {
0415 if (!skb_mac_header_was_set(skb)) {
0416 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
0417 return;
0418 }
0419
0420 memcpy(ð_hdr(skb)->h_source, node->macaddress_A, ETH_ALEN);
0421 }
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432 void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
0433 struct hsr_port *port)
0434 {
0435 struct hsr_node *node_dst;
0436 u32 hash;
0437
0438 if (!skb_mac_header_was_set(skb)) {
0439 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
0440 return;
0441 }
0442
0443 if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
0444 return;
0445
0446 hash = hsr_mac_hash(port->hsr, eth_hdr(skb)->h_dest);
0447 node_dst = find_node_by_addr_A(&port->hsr->node_db[hash],
0448 eth_hdr(skb)->h_dest);
0449 if (!node_dst) {
0450 if (net_ratelimit())
0451 netdev_err(skb->dev, "%s: Unknown node\n", __func__);
0452 return;
0453 }
0454 if (port->type != node_dst->addr_B_port)
0455 return;
0456
0457 if (is_valid_ether_addr(node_dst->macaddress_B))
0458 ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->macaddress_B);
0459 }
0460
0461 void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
0462 u16 sequence_nr)
0463 {
0464
0465
0466
0467
0468 if (!(port->dev->features & NETIF_F_HW_HSR_TAG_RM) &&
0469 seq_nr_before(sequence_nr, node->seq_out[port->type]))
0470 return;
0471
0472 node->time_in[port->type] = jiffies;
0473 node->time_in_stale[port->type] = false;
0474 }
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484 int hsr_register_frame_out(struct hsr_port *port, struct hsr_node *node,
0485 u16 sequence_nr)
0486 {
0487 if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]) &&
0488 time_is_after_jiffies(node->time_out[port->type] +
0489 msecs_to_jiffies(HSR_ENTRY_FORGET_TIME)))
0490 return 1;
0491
0492 node->time_out[port->type] = jiffies;
0493 node->seq_out[port->type] = sequence_nr;
0494 return 0;
0495 }
0496
0497 static struct hsr_port *get_late_port(struct hsr_priv *hsr,
0498 struct hsr_node *node)
0499 {
0500 if (node->time_in_stale[HSR_PT_SLAVE_A])
0501 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
0502 if (node->time_in_stale[HSR_PT_SLAVE_B])
0503 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
0504
0505 if (time_after(node->time_in[HSR_PT_SLAVE_B],
0506 node->time_in[HSR_PT_SLAVE_A] +
0507 msecs_to_jiffies(MAX_SLAVE_DIFF)))
0508 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
0509 if (time_after(node->time_in[HSR_PT_SLAVE_A],
0510 node->time_in[HSR_PT_SLAVE_B] +
0511 msecs_to_jiffies(MAX_SLAVE_DIFF)))
0512 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
0513
0514 return NULL;
0515 }
0516
0517
0518
0519
0520 void hsr_prune_nodes(struct timer_list *t)
0521 {
0522 struct hsr_priv *hsr = from_timer(hsr, t, prune_timer);
0523 struct hlist_node *tmp;
0524 struct hsr_node *node;
0525 struct hsr_port *port;
0526 unsigned long timestamp;
0527 unsigned long time_a, time_b;
0528 int i;
0529
0530 spin_lock_bh(&hsr->list_lock);
0531
0532 for (i = 0; i < hsr->hash_buckets; i++) {
0533 hlist_for_each_entry_safe(node, tmp, &hsr->node_db[i],
0534 mac_list) {
0535
0536
0537
0538
0539
0540
0541 if (hsr_addr_is_self(hsr, node->macaddress_A))
0542 continue;
0543
0544
0545 time_a = node->time_in[HSR_PT_SLAVE_A];
0546 time_b = node->time_in[HSR_PT_SLAVE_B];
0547
0548
0549
0550
0551 if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET / 2))
0552 node->time_in_stale[HSR_PT_SLAVE_A] = true;
0553 if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET / 2))
0554 node->time_in_stale[HSR_PT_SLAVE_B] = true;
0555
0556
0557
0558
0559
0560 timestamp = time_a;
0561 if (node->time_in_stale[HSR_PT_SLAVE_A] ||
0562 (!node->time_in_stale[HSR_PT_SLAVE_B] &&
0563 time_after(time_b, time_a)))
0564 timestamp = time_b;
0565
0566
0567
0568
0569 if (time_is_after_jiffies(timestamp +
0570 msecs_to_jiffies(1.5 * MAX_SLAVE_DIFF))) {
0571 rcu_read_lock();
0572 port = get_late_port(hsr, node);
0573 if (port)
0574 hsr_nl_ringerror(hsr,
0575 node->macaddress_A,
0576 port);
0577 rcu_read_unlock();
0578 }
0579
0580
0581 if (time_is_before_jiffies(timestamp +
0582 msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
0583 hsr_nl_nodedown(hsr, node->macaddress_A);
0584 hlist_del_rcu(&node->mac_list);
0585
0586
0587
0588 kfree_rcu(node, rcu_head);
0589 }
0590 }
0591 }
0592 spin_unlock_bh(&hsr->list_lock);
0593
0594
0595 mod_timer(&hsr->prune_timer,
0596 jiffies + msecs_to_jiffies(PRUNE_PERIOD));
0597 }
0598
0599 void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
0600 unsigned char addr[ETH_ALEN])
0601 {
0602 struct hsr_node *node;
0603 u32 hash;
0604
0605 hash = hsr_mac_hash(hsr, addr);
0606
0607 if (!_pos) {
0608 node = hsr_node_get_first(&hsr->node_db[hash],
0609 &hsr->list_lock);
0610 if (node)
0611 ether_addr_copy(addr, node->macaddress_A);
0612 return node;
0613 }
0614
0615 node = _pos;
0616 hlist_for_each_entry_continue_rcu(node, mac_list) {
0617 ether_addr_copy(addr, node->macaddress_A);
0618 return node;
0619 }
0620
0621 return NULL;
0622 }
0623
0624 int hsr_get_node_data(struct hsr_priv *hsr,
0625 const unsigned char *addr,
0626 unsigned char addr_b[ETH_ALEN],
0627 unsigned int *addr_b_ifindex,
0628 int *if1_age,
0629 u16 *if1_seq,
0630 int *if2_age,
0631 u16 *if2_seq)
0632 {
0633 struct hsr_node *node;
0634 struct hsr_port *port;
0635 unsigned long tdiff;
0636 u32 hash;
0637
0638 hash = hsr_mac_hash(hsr, addr);
0639
0640 node = find_node_by_addr_A(&hsr->node_db[hash], addr);
0641 if (!node)
0642 return -ENOENT;
0643
0644 ether_addr_copy(addr_b, node->macaddress_B);
0645
0646 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
0647 if (node->time_in_stale[HSR_PT_SLAVE_A])
0648 *if1_age = INT_MAX;
0649 #if HZ <= MSEC_PER_SEC
0650 else if (tdiff > msecs_to_jiffies(INT_MAX))
0651 *if1_age = INT_MAX;
0652 #endif
0653 else
0654 *if1_age = jiffies_to_msecs(tdiff);
0655
0656 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
0657 if (node->time_in_stale[HSR_PT_SLAVE_B])
0658 *if2_age = INT_MAX;
0659 #if HZ <= MSEC_PER_SEC
0660 else if (tdiff > msecs_to_jiffies(INT_MAX))
0661 *if2_age = INT_MAX;
0662 #endif
0663 else
0664 *if2_age = jiffies_to_msecs(tdiff);
0665
0666
0667 *if1_seq = node->seq_out[HSR_PT_SLAVE_B];
0668 *if2_seq = node->seq_out[HSR_PT_SLAVE_A];
0669
0670 if (node->addr_B_port != HSR_PT_NONE) {
0671 port = hsr_port_get_hsr(hsr, node->addr_B_port);
0672 *addr_b_ifindex = port->dev->ifindex;
0673 } else {
0674 *addr_b_ifindex = -1;
0675 }
0676
0677 return 0;
0678 }