blob: d12413cff5bd77085d0571703263f97d1bf3b077 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
24LIST_HEAD(llc_sap_list);
25DEFINE_RWLOCK(llc_sap_list_lock);
26
27unsigned char llc_station_mac_sa[ETH_ALEN];
28
29/**
30 * llc_sap_alloc - allocates and initializes sap.
31 *
32 * Allocates and initializes sap.
33 */
34static struct llc_sap *llc_sap_alloc(void)
35{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -070036 struct llc_sap *sap = kzalloc(sizeof(*sap), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 if (sap) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 sap->state = LLC_SAP_STATE_ACTIVE;
40 memcpy(sap->laddr.mac, llc_station_mac_sa, ETH_ALEN);
41 rwlock_init(&sap->sk_list.lock);
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -030042 atomic_set(&sap->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 }
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 */
53static void llc_add_sap(struct llc_sap *sap)
54{
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 list_add_tail(&sap->node, &llc_sap_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
58/**
59 * llc_del_sap - del sap from station list
60 * @sap: Address of the sap
61 *
62 * Removes a sap to the LLC's station sap list.
63 */
64static void llc_del_sap(struct llc_sap *sap)
65{
66 write_lock_bh(&llc_sap_list_lock);
67 list_del(&sap->node);
68 write_unlock_bh(&llc_sap_list_lock);
69}
70
Arnaldo Carvalho de Melo2928c192005-09-22 05:14:33 -030071static struct llc_sap *__llc_sap_find(unsigned char sap_value)
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -030072{
73 struct llc_sap* sap;
74
75 list_for_each_entry(sap, &llc_sap_list, node)
76 if (sap->laddr.lsap == sap_value)
77 goto out;
78 sap = NULL;
79out:
80 return sap;
81}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/**
84 * llc_sap_find - searchs a SAP in station
85 * @sap_value: sap to be found
86 *
87 * Searchs for a sap in the sap list of the LLC's station upon the sap ID.
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -030088 * If the sap is found it will be refcounted and the user will have to do
89 * a llc_sap_put after use.
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * Returns the sap or %NULL if not found.
91 */
92struct llc_sap *llc_sap_find(unsigned char sap_value)
93{
94 struct llc_sap* sap;
95
96 read_lock_bh(&llc_sap_list_lock);
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -030097 sap = __llc_sap_find(sap_value);
98 if (sap)
99 llc_sap_hold(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 read_unlock_bh(&llc_sap_list_lock);
101 return sap;
102}
103
104/**
105 * llc_sap_open - open interface to the upper layers.
106 * @lsap: SAP number.
107 * @func: rcv func for datalink protos
108 *
109 * Interface function to upper layer. Each one who wants to get a SAP
110 * (for example NetBEUI) should call this function. Returns the opened
111 * SAP for success, NULL for failure.
112 */
113struct llc_sap *llc_sap_open(unsigned char lsap,
114 int (*func)(struct sk_buff *skb,
115 struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700116 struct packet_type *pt,
117 struct net_device *orig_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300119 struct llc_sap *sap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300121 write_lock_bh(&llc_sap_list_lock);
122 if (__llc_sap_find(lsap)) /* SAP already exists */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 sap = llc_sap_alloc();
125 if (!sap)
126 goto out;
127 sap->laddr.lsap = lsap;
128 sap->rcv_func = func;
129 llc_add_sap(sap);
130out:
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300131 write_unlock_bh(&llc_sap_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return sap;
133}
134
135/**
136 * llc_sap_close - close interface for upper layers.
137 * @sap: SAP to be closed.
138 *
139 * Close interface function to upper layer. Each one who wants to
140 * close an open SAP (for example NetBEUI) should call this function.
141 * Removes this sap from the list of saps in the station and then
142 * frees the memory for this sap.
143 */
144void llc_sap_close(struct llc_sap *sap)
145{
146 WARN_ON(!hlist_empty(&sap->sk_list.list));
147 llc_del_sap(sap);
148 kfree(sap);
149}
150
151static struct packet_type llc_packet_type = {
152 .type = __constant_htons(ETH_P_802_2),
153 .func = llc_rcv,
154};
155
156static struct packet_type llc_tr_packet_type = {
157 .type = __constant_htons(ETH_P_TR_802_2),
158 .func = llc_rcv,
159};
160
161static int __init llc_init(void)
162{
163 if (dev_base->next)
164 memcpy(llc_station_mac_sa, dev_base->next->dev_addr, ETH_ALEN);
165 else
166 memset(llc_station_mac_sa, 0, ETH_ALEN);
167 dev_add_pack(&llc_packet_type);
168 dev_add_pack(&llc_tr_packet_type);
169 return 0;
170}
171
172static void __exit llc_exit(void)
173{
174 dev_remove_pack(&llc_packet_type);
175 dev_remove_pack(&llc_tr_packet_type);
176}
177
178module_init(llc_init);
179module_exit(llc_exit);
180
181EXPORT_SYMBOL(llc_station_mac_sa);
182EXPORT_SYMBOL(llc_sap_list);
183EXPORT_SYMBOL(llc_sap_list_lock);
184EXPORT_SYMBOL(llc_sap_find);
185EXPORT_SYMBOL(llc_sap_open);
186EXPORT_SYMBOL(llc_sap_close);
187
188MODULE_LICENSE("GPL");
189MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003");
190MODULE_DESCRIPTION("LLC IEEE 802.2 core support");