stp.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * STP SAP demux
  3. *
  4. * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. */
  10. #include <linux/mutex.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/llc.h>
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <net/llc.h>
  17. #include <net/llc_pdu.h>
  18. #include <net/stp.h>
  19. /* 01:80:c2:00:00:20 - 01:80:c2:00:00:2F */
  20. #define GARP_ADDR_MIN 0x20
  21. #define GARP_ADDR_MAX 0x2F
  22. #define GARP_ADDR_RANGE (GARP_ADDR_MAX - GARP_ADDR_MIN)
  23. static const struct stp_proto __rcu *garp_protos[GARP_ADDR_RANGE + 1] __read_mostly;
  24. static const struct stp_proto __rcu *stp_proto __read_mostly;
  25. static struct llc_sap *sap __read_mostly;
  26. static unsigned int sap_registered;
  27. static DEFINE_MUTEX(stp_proto_mutex);
  28. /* Called under rcu_read_lock from LLC */
  29. static int stp_pdu_rcv(struct sk_buff *skb, struct net_device *dev,
  30. struct packet_type *pt, struct net_device *orig_dev)
  31. {
  32. const struct ethhdr *eh = eth_hdr(skb);
  33. const struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  34. const struct stp_proto *proto;
  35. if (pdu->ssap != LLC_SAP_BSPAN ||
  36. pdu->dsap != LLC_SAP_BSPAN ||
  37. pdu->ctrl_1 != LLC_PDU_TYPE_U)
  38. goto err;
  39. if (eh->h_dest[5] >= GARP_ADDR_MIN && eh->h_dest[5] <= GARP_ADDR_MAX) {
  40. proto = rcu_dereference(garp_protos[eh->h_dest[5] -
  41. GARP_ADDR_MIN]);
  42. if (proto &&
  43. !ether_addr_equal(eh->h_dest, proto->group_address))
  44. goto err;
  45. } else
  46. proto = rcu_dereference(stp_proto);
  47. if (!proto)
  48. goto err;
  49. proto->rcv(proto, skb, dev);
  50. return 0;
  51. err:
  52. kfree_skb(skb);
  53. return 0;
  54. }
  55. int stp_proto_register(const struct stp_proto *proto)
  56. {
  57. int err = 0;
  58. mutex_lock(&stp_proto_mutex);
  59. if (sap_registered++ == 0) {
  60. sap = llc_sap_open(LLC_SAP_BSPAN, stp_pdu_rcv);
  61. if (!sap) {
  62. err = -ENOMEM;
  63. goto out;
  64. }
  65. }
  66. if (is_zero_ether_addr(proto->group_address))
  67. rcu_assign_pointer(stp_proto, proto);
  68. else
  69. rcu_assign_pointer(garp_protos[proto->group_address[5] -
  70. GARP_ADDR_MIN], proto);
  71. out:
  72. mutex_unlock(&stp_proto_mutex);
  73. return err;
  74. }
  75. EXPORT_SYMBOL_GPL(stp_proto_register);
  76. void stp_proto_unregister(const struct stp_proto *proto)
  77. {
  78. mutex_lock(&stp_proto_mutex);
  79. if (is_zero_ether_addr(proto->group_address))
  80. RCU_INIT_POINTER(stp_proto, NULL);
  81. else
  82. RCU_INIT_POINTER(garp_protos[proto->group_address[5] -
  83. GARP_ADDR_MIN], NULL);
  84. synchronize_rcu();
  85. if (--sap_registered == 0)
  86. llc_sap_put(sap);
  87. mutex_unlock(&stp_proto_mutex);
  88. }
  89. EXPORT_SYMBOL_GPL(stp_proto_unregister);
  90. MODULE_LICENSE("GPL");