projects/linux·blob master · refs

153 lines · 3.6 KiB

1// SPDX-License-Identifier: GPL-2.0
2/*
3 * llc_core.c - Minimum needed routines for sap handling and module init/exit
4 *
5 * Copyright (c) 1997 by Procom Technology, Inc.
6 * 2001-2003 by Arnaldo Carvalho de Melo <[email protected]>
7 */
8
9#include <linux/module.h>
10#include <linux/interrupt.h>
11#include <linux/if_ether.h>
12#include <linux/netdevice.h>
13#include <linux/slab.h>
14#include <linux/string.h>
15#include <linux/init.h>
16#include <net/net_namespace.h>
17#include <net/llc.h>
18
19LIST_HEAD(llc_sap_list);
20static DEFINE_SPINLOCK(llc_sap_list_lock);
21
22/**
23 * llc_sap_alloc - allocates and initializes sap.
24 *
25 * Allocates and initializes sap.
26 */
27static struct llc_sap *llc_sap_alloc(void)
28{
29 struct llc_sap *sap = kzalloc_obj(*sap, GFP_ATOMIC);
30 int i;
31
32 if (sap) {
33 /* sap->laddr.mac - leave as a null, it's filled by bind */
34 sap->state = LLC_SAP_STATE_ACTIVE;
35 spin_lock_init(&sap->sk_lock);
36 for (i = 0; i < LLC_SK_LADDR_HASH_ENTRIES; i++)
37 INIT_HLIST_NULLS_HEAD(&sap->sk_laddr_hash[i], i);
38 refcount_set(&sap->refcnt, 1);
39 }
40 return sap;
41}
42
43static struct llc_sap *__llc_sap_find(unsigned char sap_value)
44{
45 struct llc_sap *sap;
46
47 list_for_each_entry(sap, &llc_sap_list, node)
48 if (sap->laddr.lsap == sap_value)
49 goto out;
50 sap = NULL;
51out:
52 return sap;
53}
54
55/**
56 * llc_sap_find - searches a SAP in station
57 * @sap_value: sap to be found
58 *
59 * Searches for a sap in the sap list of the LLC's station upon the sap ID.
60 * If the sap is found it will be refcounted and the user will have to do
61 * a llc_sap_put after use.
62 * Returns the sap or %NULL if not found.
63 */
64struct llc_sap *llc_sap_find(unsigned char sap_value)
65{
66 struct llc_sap *sap;
67
68 rcu_read_lock_bh();
69 sap = __llc_sap_find(sap_value);
70 if (!sap || !llc_sap_hold_safe(sap))
71 sap = NULL;
72 rcu_read_unlock_bh();
73 return sap;
74}
75
76/**
77 * llc_sap_open - open interface to the upper layers.
78 * @lsap: SAP number.
79 * @func: rcv func for datalink protos
80 *
81 * Interface function to upper layer. Each one who wants to get a SAP
82 * (for example NetBEUI) should call this function. Returns the opened
83 * SAP for success, NULL for failure.
84 */
85struct llc_sap *llc_sap_open(unsigned char lsap,
86 int (*func)(struct sk_buff *skb,
87 struct net_device *dev,
88 struct packet_type *pt,
89 struct net_device *orig_dev))
90{
91 struct llc_sap *sap = NULL;
92
93 spin_lock_bh(&llc_sap_list_lock);
94 if (__llc_sap_find(lsap)) /* SAP already exists */
95 goto out;
96 sap = llc_sap_alloc();
97 if (!sap)
98 goto out;
99 sap->laddr.lsap = lsap;
100 sap->rcv_func = func;
101 list_add_tail_rcu(&sap->node, &llc_sap_list);
102out:
103 spin_unlock_bh(&llc_sap_list_lock);
104 return sap;
105}
106
107/**
108 * llc_sap_close - close interface for upper layers.
109 * @sap: SAP to be closed.
110 *
111 * Close interface function to upper layer. Each one who wants to
112 * close an open SAP (for example NetBEUI) should call this function.
113 * Removes this sap from the list of saps in the station and then
114 * frees the memory for this sap.
115 */
116void llc_sap_close(struct llc_sap *sap)
117{
118 WARN_ON(sap->sk_count);
119
120 spin_lock_bh(&llc_sap_list_lock);
121 list_del_rcu(&sap->node);
122 spin_unlock_bh(&llc_sap_list_lock);
123
124 kfree_rcu(sap, rcu);
125}
126
127static struct packet_type llc_packet_type __read_mostly = {
128 .type = cpu_to_be16(ETH_P_802_2),
129 .func = llc_rcv,
130};
131
132static int __init llc_init(void)
133{
134 dev_add_pack(&llc_packet_type);
135 return 0;
136}
137
138static void __exit llc_exit(void)
139{
140 dev_remove_pack(&llc_packet_type);
141}
142
143module_init(llc_init);
144module_exit(llc_exit);
145
146EXPORT_SYMBOL(llc_sap_list);
147EXPORT_SYMBOL(llc_sap_find);
148EXPORT_SYMBOL(llc_sap_open);
149EXPORT_SYMBOL(llc_sap_close);
150
151MODULE_LICENSE("GPL");
152MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003");
153MODULE_DESCRIPTION("LLC IEEE 802.2 core support");