Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * SNAP data link layer. Derived from 802.2
4 *
5 * Alan Cox <alan@lxorguk.ukuu.org.uk>,
6 * from the 802.2 layer by Greg Page.
7 * Merged in additions from Greg Page's psnap.c.
8 */
9
10#include <linux/module.h>
11#include <linux/netdevice.h>
12#include <linux/skbuff.h>
13#include <linux/slab.h>
14#include <net/datalink.h>
15#include <net/llc.h>
16#include <net/psnap.h>
17#include <linux/mm.h>
18#include <linux/in.h>
19#include <linux/init.h>
20#include <linux/rculist.h>
21
22static LIST_HEAD(snap_list);
23static DEFINE_SPINLOCK(snap_lock);
24static struct llc_sap *snap_sap;
25
26/*
27 * Find a snap client by matching the 5 bytes.
28 */
29static struct datalink_proto *find_snap_client(const unsigned char *desc)
30{
31 struct datalink_proto *proto = NULL, *p;
32
33 list_for_each_entry_rcu(p, &snap_list, node, lockdep_is_held(&snap_lock)) {
34 if (!memcmp(p->type, desc, 5)) {
35 proto = p;
36 break;
37 }
38 }
39 return proto;
40}
41
42/*
43 * A SNAP packet has arrived
44 */
45static int snap_rcv(struct sk_buff *skb, struct net_device *dev,
46 struct packet_type *pt, struct net_device *orig_dev)
47{
48 int rc = 1;
49 struct datalink_proto *proto;
50 static struct packet_type snap_packet_type = {
51 .type = cpu_to_be16(ETH_P_SNAP),
52 };
53
54 if (unlikely(!pskb_may_pull(skb, 5)))
55 goto drop;
56
57 rcu_read_lock();
58 proto = find_snap_client(skb_transport_header(skb));
59 if (proto) {
60 /* Pass the frame on. */
61 skb->transport_header += 5;
62 skb_pull_rcsum(skb, 5);
63 rc = proto->rcvfunc(skb, dev, &snap_packet_type, orig_dev);
64 }
65 rcu_read_unlock();
66
67 if (unlikely(!proto))
68 goto drop;
69
70out:
71 return rc;
72
73drop:
74 kfree_skb(skb);
75 goto out;
76}
77
78/*
79 * Put a SNAP header on a frame and pass to 802.2
80 */
81static int snap_request(struct datalink_proto *dl,
82 struct sk_buff *skb, const u8 *dest)
83{
84 memcpy(skb_push(skb, 5), dl->type, 5);
85 llc_build_and_send_ui_pkt(snap_sap, skb, dest, snap_sap->laddr.lsap);
86 return 0;
87}
88
89/*
90 * Set up the SNAP layer
91 */
92EXPORT_SYMBOL(register_snap_client);
93EXPORT_SYMBOL(unregister_snap_client);
94
95static const char snap_err_msg[] __initconst =
96 KERN_CRIT "SNAP - unable to register with 802.2\n";
97
98static int __init snap_init(void)
99{
100 snap_sap = llc_sap_open(0xAA, snap_rcv);
101 if (!snap_sap) {
102 printk(snap_err_msg);
103 return -EBUSY;
104 }
105
106 return 0;
107}
108
109module_init(snap_init);
110
111static void __exit snap_exit(void)
112{
113 llc_sap_put(snap_sap);
114}
115
116module_exit(snap_exit);
117
118
119/*
120 * Register SNAP clients. We don't yet use this for IP.
121 */
122struct datalink_proto *register_snap_client(const unsigned char *desc,
123 int (*rcvfunc)(struct sk_buff *,
124 struct net_device *,
125 struct packet_type *,
126 struct net_device *))
127{
128 struct datalink_proto *proto = NULL;
129
130 spin_lock_bh(&snap_lock);
131
132 if (find_snap_client(desc))
133 goto out;
134
135 proto = kmalloc(sizeof(*proto), GFP_ATOMIC);
136 if (proto) {
137 memcpy(proto->type, desc, 5);
138 proto->rcvfunc = rcvfunc;
139 proto->header_length = 5 + 3; /* snap + 802.2 */
140 proto->request = snap_request;
141 list_add_rcu(&proto->node, &snap_list);
142 }
143out:
144 spin_unlock_bh(&snap_lock);
145
146 return proto;
147}
148
149/*
150 * Unregister SNAP clients. Protocols no longer want to play with us ...
151 */
152void unregister_snap_client(struct datalink_proto *proto)
153{
154 spin_lock_bh(&snap_lock);
155 list_del_rcu(&proto->node);
156 spin_unlock_bh(&snap_lock);
157
158 synchronize_net();
159
160 kfree(proto);
161}
162
163MODULE_LICENSE("GPL");
1/*
2 * SNAP data link layer. Derived from 802.2
3 *
4 * Alan Cox <alan@lxorguk.ukuu.org.uk>,
5 * from the 802.2 layer by Greg Page.
6 * Merged in additions from Greg Page's psnap.c.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14#include <linux/module.h>
15#include <linux/netdevice.h>
16#include <linux/skbuff.h>
17#include <linux/slab.h>
18#include <net/datalink.h>
19#include <net/llc.h>
20#include <net/psnap.h>
21#include <linux/mm.h>
22#include <linux/in.h>
23#include <linux/init.h>
24#include <linux/rculist.h>
25
26static LIST_HEAD(snap_list);
27static DEFINE_SPINLOCK(snap_lock);
28static struct llc_sap *snap_sap;
29
30/*
31 * Find a snap client by matching the 5 bytes.
32 */
33static struct datalink_proto *find_snap_client(const unsigned char *desc)
34{
35 struct datalink_proto *proto = NULL, *p;
36
37 list_for_each_entry_rcu(p, &snap_list, node) {
38 if (!memcmp(p->type, desc, 5)) {
39 proto = p;
40 break;
41 }
42 }
43 return proto;
44}
45
46/*
47 * A SNAP packet has arrived
48 */
49static int snap_rcv(struct sk_buff *skb, struct net_device *dev,
50 struct packet_type *pt, struct net_device *orig_dev)
51{
52 int rc = 1;
53 struct datalink_proto *proto;
54 static struct packet_type snap_packet_type = {
55 .type = cpu_to_be16(ETH_P_SNAP),
56 };
57
58 if (unlikely(!pskb_may_pull(skb, 5)))
59 goto drop;
60
61 rcu_read_lock();
62 proto = find_snap_client(skb_transport_header(skb));
63 if (proto) {
64 /* Pass the frame on. */
65 skb->transport_header += 5;
66 skb_pull_rcsum(skb, 5);
67 rc = proto->rcvfunc(skb, dev, &snap_packet_type, orig_dev);
68 }
69 rcu_read_unlock();
70
71 if (unlikely(!proto))
72 goto drop;
73
74out:
75 return rc;
76
77drop:
78 kfree_skb(skb);
79 goto out;
80}
81
82/*
83 * Put a SNAP header on a frame and pass to 802.2
84 */
85static int snap_request(struct datalink_proto *dl,
86 struct sk_buff *skb, u8 *dest)
87{
88 memcpy(skb_push(skb, 5), dl->type, 5);
89 llc_build_and_send_ui_pkt(snap_sap, skb, dest, snap_sap->laddr.lsap);
90 return 0;
91}
92
93/*
94 * Set up the SNAP layer
95 */
96EXPORT_SYMBOL(register_snap_client);
97EXPORT_SYMBOL(unregister_snap_client);
98
99static const char snap_err_msg[] __initconst =
100 KERN_CRIT "SNAP - unable to register with 802.2\n";
101
102static int __init snap_init(void)
103{
104 snap_sap = llc_sap_open(0xAA, snap_rcv);
105 if (!snap_sap) {
106 printk(snap_err_msg);
107 return -EBUSY;
108 }
109
110 return 0;
111}
112
113module_init(snap_init);
114
115static void __exit snap_exit(void)
116{
117 llc_sap_put(snap_sap);
118}
119
120module_exit(snap_exit);
121
122
123/*
124 * Register SNAP clients. We don't yet use this for IP.
125 */
126struct datalink_proto *register_snap_client(const unsigned char *desc,
127 int (*rcvfunc)(struct sk_buff *,
128 struct net_device *,
129 struct packet_type *,
130 struct net_device *))
131{
132 struct datalink_proto *proto = NULL;
133
134 spin_lock_bh(&snap_lock);
135
136 if (find_snap_client(desc))
137 goto out;
138
139 proto = kmalloc(sizeof(*proto), GFP_ATOMIC);
140 if (proto) {
141 memcpy(proto->type, desc, 5);
142 proto->rcvfunc = rcvfunc;
143 proto->header_length = 5 + 3; /* snap + 802.2 */
144 proto->request = snap_request;
145 list_add_rcu(&proto->node, &snap_list);
146 }
147out:
148 spin_unlock_bh(&snap_lock);
149
150 return proto;
151}
152
153/*
154 * Unregister SNAP clients. Protocols no longer want to play with us ...
155 */
156void unregister_snap_client(struct datalink_proto *proto)
157{
158 spin_lock_bh(&snap_lock);
159 list_del_rcu(&proto->node);
160 spin_unlock_bh(&snap_lock);
161
162 synchronize_net();
163
164 kfree(proto);
165}
166
167MODULE_LICENSE("GPL");