blob: 31128cb92a23fc9b3eba377b52705671a3c984c7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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,
David S. Millerf2ccd8f2005-08-09 19:34:12 -070050 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
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
Herbert Xud92a7db2007-08-21 00:06:37 -070058 if (unlikely(!pskb_may_pull(skb, 5)))
59 goto drop;
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 rcu_read_lock();
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -070062 proto = find_snap_client(skb_transport_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (proto) {
64 /* Pass the frame on. */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -070065 skb->transport_header += 5;
Herbert Xucbb042f2006-03-20 22:43:56 -080066 skb_pull_rcsum(skb, 5);
David S. Millerf2ccd8f2005-08-09 19:34:12 -070067 rc = proto->rcvfunc(skb, dev, &snap_packet_type, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 rcu_read_unlock();
Herbert Xud92a7db2007-08-21 00:06:37 -070070
71 if (unlikely(!proto))
72 goto drop;
73
74out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return rc;
Herbert Xud92a7db2007-08-21 00:06:37 -070076
77drop:
78 kfree_skb(skb);
79 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
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 char snap_err_msg[] __initdata =
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
106 if (!snap_sap)
107 printk(snap_err_msg);
108
109 return 0;
110}
111
112module_init(snap_init);
113
114static void __exit snap_exit(void)
115{
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300116 llc_sap_put(snap_sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119module_exit(snap_exit);
120
121
122/*
123 * Register SNAP clients. We don't yet use this for IP.
124 */
125struct datalink_proto *register_snap_client(unsigned char *desc,
126 int (*rcvfunc)(struct sk_buff *,
YOSHIFUJI Hideaki9afa0942007-02-09 23:24:24 +0900127 struct net_device *,
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700128 struct packet_type *,
129 struct net_device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
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) {
140 memcpy(proto->type, desc,5);
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");