Merge branch 'master' of git+ssh://galak@master.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / net / 802 / psnap.c
CommitLineData
1da177e4
LT
1/*
2 * SNAP data link layer. Derived from 802.2
3 *
4 * Alan Cox <Alan.Cox@linux.org>,
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>
23
24static LIST_HEAD(snap_list);
25static DEFINE_SPINLOCK(snap_lock);
26static struct llc_sap *snap_sap;
27
28/*
29 * Find a snap client by matching the 5 bytes.
30 */
31static struct datalink_proto *find_snap_client(unsigned char *desc)
32{
33 struct list_head *entry;
34 struct datalink_proto *proto = NULL, *p;
35
36 list_for_each_rcu(entry, &snap_list) {
37 p = list_entry(entry, struct datalink_proto, 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,
f2ccd8fa 50 struct packet_type *pt, struct net_device *orig_dev)
1da177e4
LT
51{
52 int rc = 1;
53 struct datalink_proto *proto;
54 static struct packet_type snap_packet_type = {
55 .type = __constant_htons(ETH_P_SNAP),
56 };
57
58 rcu_read_lock();
59 proto = find_snap_client(skb->h.raw);
60 if (proto) {
61 /* Pass the frame on. */
42c5e15f 62 u8 *hdr = skb->data;
1da177e4
LT
63 skb->h.raw += 5;
64 skb_pull(skb, 5);
42c5e15f 65 skb_postpull_rcsum(skb, hdr, 5);
f2ccd8fa 66 rc = proto->rcvfunc(skb, dev, &snap_packet_type, orig_dev);
1da177e4
LT
67 } else {
68 skb->sk = NULL;
69 kfree_skb(skb);
70 rc = 1;
71 }
72
73 rcu_read_unlock();
74 return rc;
75}
76
77/*
78 * Put a SNAP header on a frame and pass to 802.2
79 */
80static int snap_request(struct datalink_proto *dl,
81 struct sk_buff *skb, u8 *dest)
82{
83 memcpy(skb_push(skb, 5), dl->type, 5);
84 llc_build_and_send_ui_pkt(snap_sap, skb, dest, snap_sap->laddr.lsap);
85 return 0;
86}
87
88/*
89 * Set up the SNAP layer
90 */
91EXPORT_SYMBOL(register_snap_client);
92EXPORT_SYMBOL(unregister_snap_client);
93
94static char snap_err_msg[] __initdata =
95 KERN_CRIT "SNAP - unable to register with 802.2\n";
96
97static int __init snap_init(void)
98{
99 snap_sap = llc_sap_open(0xAA, snap_rcv);
100
101 if (!snap_sap)
102 printk(snap_err_msg);
103
104 return 0;
105}
106
107module_init(snap_init);
108
109static void __exit snap_exit(void)
110{
6e2144b7 111 llc_sap_put(snap_sap);
1da177e4
LT
112}
113
114module_exit(snap_exit);
115
116
117/*
118 * Register SNAP clients. We don't yet use this for IP.
119 */
120struct datalink_proto *register_snap_client(unsigned char *desc,
121 int (*rcvfunc)(struct sk_buff *,
122 struct net_device *,
f2ccd8fa
DM
123 struct packet_type *,
124 struct net_device *))
1da177e4
LT
125{
126 struct datalink_proto *proto = NULL;
127
128 spin_lock_bh(&snap_lock);
129
130 if (find_snap_client(desc))
131 goto out;
132
133 proto = kmalloc(sizeof(*proto), GFP_ATOMIC);
134 if (proto) {
135 memcpy(proto->type, desc,5);
136 proto->rcvfunc = rcvfunc;
137 proto->header_length = 5 + 3; /* snap + 802.2 */
138 proto->request = snap_request;
139 list_add_rcu(&proto->node, &snap_list);
140 }
141out:
142 spin_unlock_bh(&snap_lock);
143
144 synchronize_net();
145 return proto;
146}
147
148/*
149 * Unregister SNAP clients. Protocols no longer want to play with us ...
150 */
151void unregister_snap_client(struct datalink_proto *proto)
152{
153 spin_lock_bh(&snap_lock);
154 list_del_rcu(&proto->node);
155 spin_unlock_bh(&snap_lock);
156
157 synchronize_net();
158
159 kfree(proto);
160}
161
162MODULE_LICENSE("GPL");