Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[linux-2.6-block.git] / net / 802 / psnap.c
CommitLineData
1da177e4
LT
1/*
2 * SNAP data link layer. Derived from 802.2
3 *
113aa838 4 * Alan Cox <alan@lxorguk.ukuu.org.uk>,
1da177e4
LT
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 <net/datalink.h>
18#include <net/llc.h>
19#include <net/psnap.h>
20#include <linux/mm.h>
21#include <linux/in.h>
22#include <linux/init.h>
82524746 23#include <linux/rculist.h>
1da177e4
LT
24
25static LIST_HEAD(snap_list);
26static DEFINE_SPINLOCK(snap_lock);
27static struct llc_sap *snap_sap;
28
29/*
30 * Find a snap client by matching the 5 bytes.
31 */
7ca98fa2 32static struct datalink_proto *find_snap_client(const unsigned char *desc)
1da177e4 33{
1da177e4
LT
34 struct datalink_proto *proto = NULL, *p;
35
696adfe8 36 list_for_each_entry_rcu(p, &snap_list, node) {
1da177e4
LT
37 if (!memcmp(p->type, desc, 5)) {
38 proto = p;
39 break;
40 }
41 }
42 return proto;
43}
44
45/*
46 * A SNAP packet has arrived
47 */
48static int snap_rcv(struct sk_buff *skb, struct net_device *dev,
f2ccd8fa 49 struct packet_type *pt, struct net_device *orig_dev)
1da177e4
LT
50{
51 int rc = 1;
52 struct datalink_proto *proto;
53 static struct packet_type snap_packet_type = {
09640e63 54 .type = cpu_to_be16(ETH_P_SNAP),
1da177e4
LT
55 };
56
d92a7db7
HX
57 if (unlikely(!pskb_may_pull(skb, 5)))
58 goto drop;
59
1da177e4 60 rcu_read_lock();
9c70220b 61 proto = find_snap_client(skb_transport_header(skb));
1da177e4
LT
62 if (proto) {
63 /* Pass the frame on. */
b0e380b1 64 skb->transport_header += 5;
cbb042f9 65 skb_pull_rcsum(skb, 5);
f2ccd8fa 66 rc = proto->rcvfunc(skb, dev, &snap_packet_type, orig_dev);
1da177e4 67 }
1da177e4 68 rcu_read_unlock();
d92a7db7
HX
69
70 if (unlikely(!proto))
71 goto drop;
72
73out:
1da177e4 74 return rc;
d92a7db7
HX
75
76drop:
77 kfree_skb(skb);
78 goto out;
1da177e4
LT
79}
80
81/*
82 * Put a SNAP header on a frame and pass to 802.2
83 */
84static int snap_request(struct datalink_proto *dl,
85 struct sk_buff *skb, u8 *dest)
86{
87 memcpy(skb_push(skb, 5), dl->type, 5);
88 llc_build_and_send_ui_pkt(snap_sap, skb, dest, snap_sap->laddr.lsap);
89 return 0;
90}
91
92/*
93 * Set up the SNAP layer
94 */
95EXPORT_SYMBOL(register_snap_client);
96EXPORT_SYMBOL(unregister_snap_client);
97
0117cfab 98static const char snap_err_msg[] __initconst =
1da177e4
LT
99 KERN_CRIT "SNAP - unable to register with 802.2\n";
100
101static int __init snap_init(void)
102{
103 snap_sap = llc_sap_open(0xAA, snap_rcv);
0117cfab 104 if (!snap_sap) {
1da177e4 105 printk(snap_err_msg);
0117cfab
SH
106 return -EBUSY;
107 }
1da177e4
LT
108
109 return 0;
110}
111
112module_init(snap_init);
113
114static void __exit snap_exit(void)
115{
6e2144b7 116 llc_sap_put(snap_sap);
1da177e4
LT
117}
118
119module_exit(snap_exit);
120
121
122/*
123 * Register SNAP clients. We don't yet use this for IP.
124 */
7ca98fa2 125struct datalink_proto *register_snap_client(const unsigned char *desc,
1da177e4 126 int (*rcvfunc)(struct sk_buff *,
9afa0949 127 struct net_device *,
f2ccd8fa
DM
128 struct packet_type *,
129 struct net_device *))
1da177e4
LT
130{
131 struct datalink_proto *proto = NULL;
132
133 spin_lock_bh(&snap_lock);
134
135 if (find_snap_client(desc))
136 goto out;
137
138 proto = kmalloc(sizeof(*proto), GFP_ATOMIC);
139 if (proto) {
7ca98fa2 140 memcpy(proto->type, desc, 5);
1da177e4
LT
141 proto->rcvfunc = rcvfunc;
142 proto->header_length = 5 + 3; /* snap + 802.2 */
143 proto->request = snap_request;
144 list_add_rcu(&proto->node, &snap_list);
145 }
146out:
147 spin_unlock_bh(&snap_lock);
148
149 synchronize_net();
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");