llc_core.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/llc.h>
  22. LIST_HEAD(llc_sap_list);
  23. DEFINE_RWLOCK(llc_sap_list_lock);
  24. unsigned char llc_station_mac_sa[ETH_ALEN];
  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 = kmalloc(sizeof(*sap), GFP_ATOMIC);
  33. if (sap) {
  34. memset(sap, 0, sizeof(*sap));
  35. sap->state = LLC_SAP_STATE_ACTIVE;
  36. memcpy(sap->laddr.mac, llc_station_mac_sa, ETH_ALEN);
  37. rwlock_init(&sap->sk_list.lock);
  38. atomic_set(&sap->refcnt, 1);
  39. }
  40. return sap;
  41. }
  42. /**
  43. * llc_add_sap - add sap to station list
  44. * @sap: Address of the sap
  45. *
  46. * Adds a sap to the LLC's station sap list.
  47. */
  48. static void llc_add_sap(struct llc_sap *sap)
  49. {
  50. list_add_tail(&sap->node, &llc_sap_list);
  51. }
  52. /**
  53. * llc_del_sap - del sap from station list
  54. * @sap: Address of the sap
  55. *
  56. * Removes a sap to the LLC's station sap list.
  57. */
  58. static void llc_del_sap(struct llc_sap *sap)
  59. {
  60. write_lock_bh(&llc_sap_list_lock);
  61. list_del(&sap->node);
  62. write_unlock_bh(&llc_sap_list_lock);
  63. }
  64. static struct llc_sap *__llc_sap_find(unsigned char sap_value)
  65. {
  66. struct llc_sap* sap;
  67. list_for_each_entry(sap, &llc_sap_list, node)
  68. if (sap->laddr.lsap == sap_value)
  69. goto out;
  70. sap = NULL;
  71. out:
  72. return sap;
  73. }
  74. /**
  75. * llc_sap_find - searchs a SAP in station
  76. * @sap_value: sap to be found
  77. *
  78. * Searchs for a sap in the sap list of the LLC's station upon the sap ID.
  79. * If the sap is found it will be refcounted and the user will have to do
  80. * a llc_sap_put after use.
  81. * Returns the sap or %NULL if not found.
  82. */
  83. struct llc_sap *llc_sap_find(unsigned char sap_value)
  84. {
  85. struct llc_sap* sap;
  86. read_lock_bh(&llc_sap_list_lock);
  87. sap = __llc_sap_find(sap_value);
  88. if (sap)
  89. llc_sap_hold(sap);
  90. read_unlock_bh(&llc_sap_list_lock);
  91. return sap;
  92. }
  93. /**
  94. * llc_sap_open - open interface to the upper layers.
  95. * @lsap: SAP number.
  96. * @func: rcv func for datalink protos
  97. *
  98. * Interface function to upper layer. Each one who wants to get a SAP
  99. * (for example NetBEUI) should call this function. Returns the opened
  100. * SAP for success, NULL for failure.
  101. */
  102. struct llc_sap *llc_sap_open(unsigned char lsap,
  103. int (*func)(struct sk_buff *skb,
  104. struct net_device *dev,
  105. struct packet_type *pt,
  106. struct net_device *orig_dev))
  107. {
  108. struct llc_sap *sap = NULL;
  109. write_lock_bh(&llc_sap_list_lock);
  110. if (__llc_sap_find(lsap)) /* SAP already exists */
  111. goto out;
  112. sap = llc_sap_alloc();
  113. if (!sap)
  114. goto out;
  115. sap->laddr.lsap = lsap;
  116. sap->rcv_func = func;
  117. llc_sap_hold(sap);
  118. llc_add_sap(sap);
  119. out:
  120. write_unlock_bh(&llc_sap_list_lock);
  121. return sap;
  122. }
  123. /**
  124. * llc_sap_close - close interface for upper layers.
  125. * @sap: SAP to be closed.
  126. *
  127. * Close interface function to upper layer. Each one who wants to
  128. * close an open SAP (for example NetBEUI) should call this function.
  129. * Removes this sap from the list of saps in the station and then
  130. * frees the memory for this sap.
  131. */
  132. void llc_sap_close(struct llc_sap *sap)
  133. {
  134. WARN_ON(!hlist_empty(&sap->sk_list.list));
  135. llc_del_sap(sap);
  136. kfree(sap);
  137. }
  138. static struct packet_type llc_packet_type = {
  139. .type = __constant_htons(ETH_P_802_2),
  140. .func = llc_rcv,
  141. };
  142. static struct packet_type llc_tr_packet_type = {
  143. .type = __constant_htons(ETH_P_TR_802_2),
  144. .func = llc_rcv,
  145. };
  146. static int __init llc_init(void)
  147. {
  148. if (dev_base->next)
  149. memcpy(llc_station_mac_sa, dev_base->next->dev_addr, ETH_ALEN);
  150. else
  151. memset(llc_station_mac_sa, 0, ETH_ALEN);
  152. dev_add_pack(&llc_packet_type);
  153. dev_add_pack(&llc_tr_packet_type);
  154. return 0;
  155. }
  156. static void __exit llc_exit(void)
  157. {
  158. dev_remove_pack(&llc_packet_type);
  159. dev_remove_pack(&llc_tr_packet_type);
  160. }
  161. module_init(llc_init);
  162. module_exit(llc_exit);
  163. EXPORT_SYMBOL(llc_station_mac_sa);
  164. EXPORT_SYMBOL(llc_sap_list);
  165. EXPORT_SYMBOL(llc_sap_list_lock);
  166. EXPORT_SYMBOL(llc_sap_find);
  167. EXPORT_SYMBOL(llc_sap_open);
  168. EXPORT_SYMBOL(llc_sap_close);
  169. MODULE_LICENSE("GPL");
  170. MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003");
  171. MODULE_DESCRIPTION("LLC IEEE 802.2 core support");