1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SNAP data link layer. Derived from 802.2
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.
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>
16 #include <net/psnap.h>
19 #include <linux/init.h>
20 #include <linux/rculist.h>
22 static LIST_HEAD(snap_list);
23 static DEFINE_SPINLOCK(snap_lock);
24 static struct llc_sap *snap_sap;
27 * Find a snap client by matching the 5 bytes.
29 static struct datalink_proto *find_snap_client(const unsigned char *desc)
31 struct datalink_proto *proto = NULL, *p;
33 list_for_each_entry_rcu(p, &snap_list, node, lockdep_is_held(&snap_lock)) {
34 if (!memcmp(p->type, desc, 5)) {
43 * A SNAP packet has arrived
45 static int snap_rcv(struct sk_buff *skb, struct net_device *dev,
46 struct packet_type *pt, struct net_device *orig_dev)
49 struct datalink_proto *proto;
50 static struct packet_type snap_packet_type = {
51 .type = cpu_to_be16(ETH_P_SNAP),
54 if (unlikely(!pskb_may_pull(skb, 5)))
58 proto = find_snap_client(skb_transport_header(skb));
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);
79 * Put a SNAP header on a frame and pass to 802.2
81 static int snap_request(struct datalink_proto *dl,
82 struct sk_buff *skb, const u8 *dest)
84 memcpy(skb_push(skb, 5), dl->type, 5);
85 llc_build_and_send_ui_pkt(snap_sap, skb, dest, snap_sap->laddr.lsap);
90 * Set up the SNAP layer
92 EXPORT_SYMBOL(register_snap_client);
93 EXPORT_SYMBOL(unregister_snap_client);
95 static const char snap_err_msg[] __initconst =
96 KERN_CRIT "SNAP - unable to register with 802.2\n";
98 static int __init snap_init(void)
100 snap_sap = llc_sap_open(0xAA, snap_rcv);
102 printk(snap_err_msg);
109 module_init(snap_init);
111 static void __exit snap_exit(void)
113 llc_sap_put(snap_sap);
116 module_exit(snap_exit);
120 * Register SNAP clients. We don't yet use this for IP.
122 struct datalink_proto *register_snap_client(const unsigned char *desc,
123 int (*rcvfunc)(struct sk_buff *,
125 struct packet_type *,
126 struct net_device *))
128 struct datalink_proto *proto = NULL;
130 spin_lock_bh(&snap_lock);
132 if (find_snap_client(desc))
135 proto = kmalloc(sizeof(*proto), GFP_ATOMIC);
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);
144 spin_unlock_bh(&snap_lock);
150 * Unregister SNAP clients. Protocols no longer want to play with us ...
152 void unregister_snap_client(struct datalink_proto *proto)
154 spin_lock_bh(&snap_lock);
155 list_del_rcu(&proto->node);
156 spin_unlock_bh(&snap_lock);
163 MODULE_LICENSE("GPL");