llc_core.c 4.4 KB

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