blob: 0c9ef8bc7655eed3d03ecca9bd30771d5d04ea8c [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/**
29 * llc_sap_alloc - allocates and initializes sap.
30 *
31 * Allocates and initializes sap.
32 */
33static struct llc_sap *llc_sap_alloc(void)
34{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -070035 struct llc_sap *sap = kzalloc(sizeof(*sap), GFP_ATOMIC);
Octavian Purdila52d58ae2009-12-26 11:51:05 +000036 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 if (sap) {
Joonwoo Parka5a04812008-03-28 16:28:36 -070039 /* sap->laddr.mac - leave as a null, it's filled by bind */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 sap->state = LLC_SAP_STATE_ACTIVE;
Octavian Purdilab76f5a82009-12-26 11:51:02 +000041 spin_lock_init(&sap->sk_lock);
Octavian Purdila52d58ae2009-12-26 11:51:05 +000042 for (i = 0; i < LLC_SK_LADDR_HASH_ENTRIES; i++)
43 INIT_HLIST_NULLS_HEAD(&sap->sk_laddr_hash[i], i);
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -030044 atomic_set(&sap->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 }
46 return sap;
47}
48
49/**
50 * llc_add_sap - add sap to station list
51 * @sap: Address of the sap
52 *
53 * Adds a sap to the LLC's station sap list.
54 */
55static void llc_add_sap(struct llc_sap *sap)
56{
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 list_add_tail(&sap->node, &llc_sap_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
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 */
66static 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
Arnaldo Carvalho de Melo2928c192005-09-22 05:14:33 -030073static struct llc_sap *__llc_sap_find(unsigned char sap_value)
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -030074{
75 struct llc_sap* sap;
76
77 list_for_each_entry(sap, &llc_sap_list, node)
78 if (sap->laddr.lsap == sap_value)
79 goto out;
80 sap = NULL;
81out:
82 return sap;
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/**
86 * llc_sap_find - searchs a SAP in station
87 * @sap_value: sap to be found
88 *
89 * 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 -030090 * If the sap is found it will be refcounted and the user will have to do
91 * a llc_sap_put after use.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * Returns the sap or %NULL if not found.
93 */
94struct llc_sap *llc_sap_find(unsigned char sap_value)
95{
96 struct llc_sap* sap;
97
98 read_lock_bh(&llc_sap_list_lock);
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -030099 sap = __llc_sap_find(sap_value);
100 if (sap)
101 llc_sap_hold(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 read_unlock_bh(&llc_sap_list_lock);
103 return sap;
104}
105
106/**
107 * llc_sap_open - open interface to the upper layers.
108 * @lsap: SAP number.
109 * @func: rcv func for datalink protos
110 *
111 * Interface function to upper layer. Each one who wants to get a SAP
112 * (for example NetBEUI) should call this function. Returns the opened
113 * SAP for success, NULL for failure.
114 */
115struct llc_sap *llc_sap_open(unsigned char lsap,
116 int (*func)(struct sk_buff *skb,
117 struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700118 struct packet_type *pt,
119 struct net_device *orig_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300121 struct llc_sap *sap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300123 write_lock_bh(&llc_sap_list_lock);
124 if (__llc_sap_find(lsap)) /* SAP already exists */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 sap = llc_sap_alloc();
127 if (!sap)
128 goto out;
129 sap->laddr.lsap = lsap;
130 sap->rcv_func = func;
131 llc_add_sap(sap);
132out:
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300133 write_unlock_bh(&llc_sap_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return sap;
135}
136
137/**
138 * llc_sap_close - close interface for upper layers.
139 * @sap: SAP to be closed.
140 *
141 * Close interface function to upper layer. Each one who wants to
142 * close an open SAP (for example NetBEUI) should call this function.
143 * Removes this sap from the list of saps in the station and then
144 * frees the memory for this sap.
145 */
146void llc_sap_close(struct llc_sap *sap)
147{
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000148 WARN_ON(sap->sk_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 llc_del_sap(sap);
150 kfree(sap);
151}
152
Stephen Hemminger7546dd92009-03-09 08:18:29 +0000153static struct packet_type llc_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -0800154 .type = cpu_to_be16(ETH_P_802_2),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .func = llc_rcv,
156};
157
Stephen Hemminger7546dd92009-03-09 08:18:29 +0000158static struct packet_type llc_tr_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -0800159 .type = cpu_to_be16(ETH_P_TR_802_2),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 .func = llc_rcv,
161};
162
163static int __init llc_init(void)
164{
Pavel Emelianov7562f872007-05-03 15:13:45 -0700165 struct net_device *dev;
166
Eric W. Biederman881d9662007-09-17 11:56:21 -0700167 dev = first_net_device(&init_net);
Pavel Emelianov7562f872007-05-03 15:13:45 -0700168 if (dev != NULL)
169 dev = next_net_device(dev);
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 dev_add_pack(&llc_packet_type);
172 dev_add_pack(&llc_tr_packet_type);
173 return 0;
174}
175
176static void __exit llc_exit(void)
177{
178 dev_remove_pack(&llc_packet_type);
179 dev_remove_pack(&llc_tr_packet_type);
180}
181
182module_init(llc_init);
183module_exit(llc_exit);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185EXPORT_SYMBOL(llc_sap_list);
186EXPORT_SYMBOL(llc_sap_list_lock);
187EXPORT_SYMBOL(llc_sap_find);
188EXPORT_SYMBOL(llc_sap_open);
189EXPORT_SYMBOL(llc_sap_close);
190
191MODULE_LICENSE("GPL");
192MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003");
193MODULE_DESCRIPTION("LLC IEEE 802.2 core support");