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