stp.c 2.6 KB

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