stp.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <net/llc.h>
  15. #include <net/llc_pdu.h>
  16. #include <net/stp.h>
  17. /* 01:80:c2:00:00:20 - 01:80:c2:00:00:2F */
  18. #define GARP_ADDR_MIN 0x20
  19. #define GARP_ADDR_MAX 0x2F
  20. #define GARP_ADDR_RANGE (GARP_ADDR_MAX - GARP_ADDR_MIN)
  21. static const struct stp_proto *garp_protos[GARP_ADDR_RANGE + 1] __read_mostly;
  22. static const struct stp_proto *stp_proto __read_mostly;
  23. static struct llc_sap *sap __read_mostly;
  24. static unsigned int sap_registered;
  25. static DEFINE_MUTEX(stp_proto_mutex);
  26. /* Called under rcu_read_lock from LLC */
  27. static int stp_pdu_rcv(struct sk_buff *skb, struct net_device *dev,
  28. struct packet_type *pt, struct net_device *orig_dev)
  29. {
  30. const struct ethhdr *eh = eth_hdr(skb);
  31. const struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  32. const struct stp_proto *proto;
  33. if (pdu->ssap != LLC_SAP_BSPAN ||
  34. pdu->dsap != LLC_SAP_BSPAN ||
  35. pdu->ctrl_1 != LLC_PDU_TYPE_U)
  36. goto err;
  37. if (eh->h_dest[5] >= GARP_ADDR_MIN && eh->h_dest[5] <= GARP_ADDR_MAX) {
  38. proto = rcu_dereference(garp_protos[eh->h_dest[5] -
  39. GARP_ADDR_MIN]);
  40. if (proto &&
  41. compare_ether_addr(eh->h_dest, proto->group_address))
  42. goto err;
  43. } else
  44. proto = rcu_dereference(stp_proto);
  45. if (!proto)
  46. goto err;
  47. proto->rcv(proto, skb, dev);
  48. return 0;
  49. err:
  50. kfree_skb(skb);
  51. return 0;
  52. }
  53. int stp_proto_register(const struct stp_proto *proto)
  54. {
  55. int err = 0;
  56. mutex_lock(&stp_proto_mutex);
  57. if (sap_registered++ == 0) {
  58. sap = llc_sap_open(LLC_SAP_BSPAN, stp_pdu_rcv);
  59. if (!sap) {
  60. err = -ENOMEM;
  61. goto out;
  62. }
  63. }
  64. if (is_zero_ether_addr(proto->group_address))
  65. rcu_assign_pointer(stp_proto, proto);
  66. else
  67. rcu_assign_pointer(garp_protos[proto->group_address[5] -
  68. GARP_ADDR_MIN], proto);
  69. out:
  70. mutex_unlock(&stp_proto_mutex);
  71. return err;
  72. }
  73. EXPORT_SYMBOL_GPL(stp_proto_register);
  74. void stp_proto_unregister(const struct stp_proto *proto)
  75. {
  76. mutex_lock(&stp_proto_mutex);
  77. if (is_zero_ether_addr(proto->group_address))
  78. rcu_assign_pointer(stp_proto, NULL);
  79. else
  80. rcu_assign_pointer(garp_protos[proto->group_address[5] -
  81. GARP_ADDR_MIN], NULL);
  82. synchronize_rcu();
  83. if (--sap_registered == 0)
  84. llc_sap_put(sap);
  85. mutex_unlock(&stp_proto_mutex);
  86. }
  87. EXPORT_SYMBOL_GPL(stp_proto_unregister);
  88. MODULE_LICENSE("GPL");