llc_core.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * llc_core.c - Minimum needed routines for sap handling and module init/exit
  3. *
  4. * Copyright (c) 1997 by Procom Technology, Inc.
  5. * 2001-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  6. *
  7. * This program can be redistributed or modified under the terms of the
  8. * GNU General Public License as published by the Free Software Foundation.
  9. * This program is distributed without any warranty or implied warranty
  10. * of merchantability or fitness for a particular purpose.
  11. *
  12. * See the GNU General Public License for more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/slab.h>
  19. #include <linux/string.h>
  20. #include <linux/init.h>
  21. #include <net/net_namespace.h>
  22. #include <net/llc.h>
  23. LIST_HEAD(llc_sap_list);
  24. static DEFINE_SPINLOCK(llc_sap_list_lock);
  25. /**
  26. * llc_sap_alloc - allocates and initializes sap.
  27. *
  28. * Allocates and initializes sap.
  29. */
  30. static struct llc_sap *llc_sap_alloc(void)
  31. {
  32. struct llc_sap *sap = kzalloc(sizeof(*sap), GFP_ATOMIC);
  33. int i;
  34. if (sap) {
  35. /* sap->laddr.mac - leave as a null, it's filled by bind */
  36. sap->state = LLC_SAP_STATE_ACTIVE;
  37. spin_lock_init(&sap->sk_lock);
  38. for (i = 0; i < LLC_SK_LADDR_HASH_ENTRIES; i++)
  39. INIT_HLIST_NULLS_HEAD(&sap->sk_laddr_hash[i], i);
  40. refcount_set(&sap->refcnt, 1);
  41. }
  42. return sap;
  43. }
  44. static struct llc_sap *__llc_sap_find(unsigned char sap_value)
  45. {
  46. struct llc_sap *sap;
  47. list_for_each_entry(sap, &llc_sap_list, node)
  48. if (sap->laddr.lsap == sap_value)
  49. goto out;
  50. sap = NULL;
  51. out:
  52. return sap;
  53. }
  54. /**
  55. * llc_sap_find - searchs a SAP in station
  56. * @sap_value: sap to be found
  57. *
  58. * Searchs for a sap in the sap list of the LLC's station upon the sap ID.
  59. * If the sap is found it will be refcounted and the user will have to do
  60. * a llc_sap_put after use.
  61. * Returns the sap or %NULL if not found.
  62. */
  63. struct llc_sap *llc_sap_find(unsigned char sap_value)
  64. {
  65. struct llc_sap *sap;
  66. rcu_read_lock_bh();
  67. sap = __llc_sap_find(sap_value);
  68. if (!sap || !llc_sap_hold_safe(sap))
  69. sap = NULL;
  70. rcu_read_unlock_bh();
  71. return sap;
  72. }
  73. /**
  74. * llc_sap_open - open interface to the upper layers.
  75. * @lsap: SAP number.
  76. * @func: rcv func for datalink protos
  77. *
  78. * Interface function to upper layer. Each one who wants to get a SAP
  79. * (for example NetBEUI) should call this function. Returns the opened
  80. * SAP for success, NULL for failure.
  81. */
  82. struct llc_sap *llc_sap_open(unsigned char lsap,
  83. int (*func)(struct sk_buff *skb,
  84. struct net_device *dev,
  85. struct packet_type *pt,
  86. struct net_device *orig_dev))
  87. {
  88. struct llc_sap *sap = NULL;
  89. spin_lock_bh(&llc_sap_list_lock);
  90. if (__llc_sap_find(lsap)) /* SAP already exists */
  91. goto out;
  92. sap = llc_sap_alloc();
  93. if (!sap)
  94. goto out;
  95. sap->laddr.lsap = lsap;
  96. sap->rcv_func = func;
  97. list_add_tail_rcu(&sap->node, &llc_sap_list);
  98. out:
  99. spin_unlock_bh(&llc_sap_list_lock);
  100. return sap;
  101. }
  102. /**
  103. * llc_sap_close - close interface for upper layers.
  104. * @sap: SAP to be closed.
  105. *
  106. * Close interface function to upper layer. Each one who wants to
  107. * close an open SAP (for example NetBEUI) should call this function.
  108. * Removes this sap from the list of saps in the station and then
  109. * frees the memory for this sap.
  110. */
  111. void llc_sap_close(struct llc_sap *sap)
  112. {
  113. WARN_ON(sap->sk_count);
  114. spin_lock_bh(&llc_sap_list_lock);
  115. list_del_rcu(&sap->node);
  116. spin_unlock_bh(&llc_sap_list_lock);
  117. kfree_rcu(sap, rcu);
  118. }
  119. static struct packet_type llc_packet_type __read_mostly = {
  120. .type = cpu_to_be16(ETH_P_802_2),
  121. .func = llc_rcv,
  122. };
  123. static struct packet_type llc_tr_packet_type __read_mostly = {
  124. .type = cpu_to_be16(ETH_P_TR_802_2),
  125. .func = llc_rcv,
  126. };
  127. static int __init llc_init(void)
  128. {
  129. dev_add_pack(&llc_packet_type);
  130. dev_add_pack(&llc_tr_packet_type);
  131. return 0;
  132. }
  133. static void __exit llc_exit(void)
  134. {
  135. dev_remove_pack(&llc_packet_type);
  136. dev_remove_pack(&llc_tr_packet_type);
  137. }
  138. module_init(llc_init);
  139. module_exit(llc_exit);
  140. EXPORT_SYMBOL(llc_sap_list);
  141. EXPORT_SYMBOL(llc_sap_find);
  142. EXPORT_SYMBOL(llc_sap_open);
  143. EXPORT_SYMBOL(llc_sap_close);
  144. MODULE_LICENSE("GPL");
  145. MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003");
  146. MODULE_DESCRIPTION("LLC IEEE 802.2 core support");