blob: 248b5903bb1348e07f594e5f54119963de82f365 [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>
Eric W. Biederman881d9662007-09-17 11:56:21 -070022#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <net/llc.h>
24
25LIST_HEAD(llc_sap_list);
26DEFINE_RWLOCK(llc_sap_list_lock);
27
28unsigned char llc_station_mac_sa[ETH_ALEN];
29
30/**
31 * llc_sap_alloc - allocates and initializes sap.
32 *
33 * Allocates and initializes sap.
34 */
35static struct llc_sap *llc_sap_alloc(void)
36{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -070037 struct llc_sap *sap = kzalloc(sizeof(*sap), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 if (sap) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 sap->state = LLC_SAP_STATE_ACTIVE;
41 memcpy(sap->laddr.mac, llc_station_mac_sa, ETH_ALEN);
42 rwlock_init(&sap->sk_list.lock);
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -030043 atomic_set(&sap->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 }
45 return sap;
46}
47
48/**
49 * llc_add_sap - add sap to station list
50 * @sap: Address of the sap
51 *
52 * Adds a sap to the LLC's station sap list.
53 */
54static void llc_add_sap(struct llc_sap *sap)
55{
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 list_add_tail(&sap->node, &llc_sap_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59/**
60 * llc_del_sap - del sap from station list
61 * @sap: Address of the sap
62 *
63 * Removes a sap to the LLC's station sap list.
64 */
65static void llc_del_sap(struct llc_sap *sap)
66{
67 write_lock_bh(&llc_sap_list_lock);
68 list_del(&sap->node);
69 write_unlock_bh(&llc_sap_list_lock);
70}
71
Arnaldo Carvalho de Melo2928c192005-09-22 05:14:33 -030072static struct llc_sap *__llc_sap_find(unsigned char sap_value)
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -030073{
74 struct llc_sap* sap;
75
76 list_for_each_entry(sap, &llc_sap_list, node)
77 if (sap->laddr.lsap == sap_value)
78 goto out;
79 sap = NULL;
80out:
81 return sap;
82}
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/**
85 * llc_sap_find - searchs a SAP in station
86 * @sap_value: sap to be found
87 *
88 * 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 -030089 * If the sap is found it will be refcounted and the user will have to do
90 * a llc_sap_put after use.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * Returns the sap or %NULL if not found.
92 */
93struct llc_sap *llc_sap_find(unsigned char sap_value)
94{
95 struct llc_sap* sap;
96
97 read_lock_bh(&llc_sap_list_lock);
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -030098 sap = __llc_sap_find(sap_value);
99 if (sap)
100 llc_sap_hold(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 read_unlock_bh(&llc_sap_list_lock);
102 return sap;
103}
104
105/**
106 * llc_sap_open - open interface to the upper layers.
107 * @lsap: SAP number.
108 * @func: rcv func for datalink protos
109 *
110 * Interface function to upper layer. Each one who wants to get a SAP
111 * (for example NetBEUI) should call this function. Returns the opened
112 * SAP for success, NULL for failure.
113 */
114struct llc_sap *llc_sap_open(unsigned char lsap,
115 int (*func)(struct sk_buff *skb,
116 struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700117 struct packet_type *pt,
118 struct net_device *orig_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300120 struct llc_sap *sap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300122 write_lock_bh(&llc_sap_list_lock);
123 if (__llc_sap_find(lsap)) /* SAP already exists */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 sap = llc_sap_alloc();
126 if (!sap)
127 goto out;
128 sap->laddr.lsap = lsap;
129 sap->rcv_func = func;
130 llc_add_sap(sap);
131out:
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300132 write_unlock_bh(&llc_sap_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return sap;
134}
135
136/**
137 * llc_sap_close - close interface for upper layers.
138 * @sap: SAP to be closed.
139 *
140 * Close interface function to upper layer. Each one who wants to
141 * close an open SAP (for example NetBEUI) should call this function.
142 * Removes this sap from the list of saps in the station and then
143 * frees the memory for this sap.
144 */
145void llc_sap_close(struct llc_sap *sap)
146{
147 WARN_ON(!hlist_empty(&sap->sk_list.list));
148 llc_del_sap(sap);
149 kfree(sap);
150}
151
152static struct packet_type llc_packet_type = {
153 .type = __constant_htons(ETH_P_802_2),
154 .func = llc_rcv,
155};
156
157static struct packet_type llc_tr_packet_type = {
158 .type = __constant_htons(ETH_P_TR_802_2),
159 .func = llc_rcv,
160};
161
162static int __init llc_init(void)
163{
Pavel Emelianov7562f872007-05-03 15:13:45 -0700164 struct net_device *dev;
165
Eric W. Biederman881d9662007-09-17 11:56:21 -0700166 dev = first_net_device(&init_net);
Pavel Emelianov7562f872007-05-03 15:13:45 -0700167 if (dev != NULL)
168 dev = next_net_device(dev);
169
170 if (dev != NULL)
171 memcpy(llc_station_mac_sa, dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 else
173 memset(llc_station_mac_sa, 0, ETH_ALEN);
174 dev_add_pack(&llc_packet_type);
175 dev_add_pack(&llc_tr_packet_type);
176 return 0;
177}
178
179static void __exit llc_exit(void)
180{
181 dev_remove_pack(&llc_packet_type);
182 dev_remove_pack(&llc_tr_packet_type);
183}
184
185module_init(llc_init);
186module_exit(llc_exit);
187
188EXPORT_SYMBOL(llc_station_mac_sa);
189EXPORT_SYMBOL(llc_sap_list);
190EXPORT_SYMBOL(llc_sap_list_lock);
191EXPORT_SYMBOL(llc_sap_find);
192EXPORT_SYMBOL(llc_sap_open);
193EXPORT_SYMBOL(llc_sap_close);
194
195MODULE_LICENSE("GPL");
196MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003");
197MODULE_DESCRIPTION("LLC IEEE 802.2 core support");