blob: b3cfe5a14fcaee0cfb56401286574d1a2e94a74b [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>
Franck Bui-Huu82524742008-05-12 21:21:05 +020023#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
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 */
32static struct datalink_proto *find_snap_client(unsigned char *desc)
33{
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 struct datalink_proto *proto = NULL, *p;
35
Paul E. McKenney696adfe2008-07-25 01:45:34 -070036 list_for_each_entry_rcu(p, &snap_list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 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,
David S. Millerf2ccd8f2005-08-09 19:34:12 -070049 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 int rc = 1;
52 struct datalink_proto *proto;
53 static struct packet_type snap_packet_type = {
54 .type = __constant_htons(ETH_P_SNAP),
55 };
56
Herbert Xud92a7db2007-08-21 00:06:37 -070057 if (unlikely(!pskb_may_pull(skb, 5)))
58 goto drop;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 rcu_read_lock();
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -070061 proto = find_snap_client(skb_transport_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (proto) {
63 /* Pass the frame on. */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -070064 skb->transport_header += 5;
Herbert Xucbb042f92006-03-20 22:43:56 -080065 skb_pull_rcsum(skb, 5);
David S. Millerf2ccd8f2005-08-09 19:34:12 -070066 rc = proto->rcvfunc(skb, dev, &snap_packet_type, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 rcu_read_unlock();
Herbert Xud92a7db2007-08-21 00:06:37 -070069
70 if (unlikely(!proto))
71 goto drop;
72
73out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return rc;
Herbert Xud92a7db2007-08-21 00:06:37 -070075
76drop:
77 kfree_skb(skb);
78 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
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
98static char snap_err_msg[] __initdata =
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);
104
105 if (!snap_sap)
106 printk(snap_err_msg);
107
108 return 0;
109}
110
111module_init(snap_init);
112
113static void __exit snap_exit(void)
114{
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300115 llc_sap_put(snap_sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118module_exit(snap_exit);
119
120
121/*
122 * Register SNAP clients. We don't yet use this for IP.
123 */
124struct datalink_proto *register_snap_client(unsigned char *desc,
125 int (*rcvfunc)(struct sk_buff *,
YOSHIFUJI Hideaki9afa0942007-02-09 23:24:24 +0900126 struct net_device *,
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700127 struct packet_type *,
128 struct net_device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct datalink_proto *proto = NULL;
131
132 spin_lock_bh(&snap_lock);
133
134 if (find_snap_client(desc))
135 goto out;
136
137 proto = kmalloc(sizeof(*proto), GFP_ATOMIC);
138 if (proto) {
139 memcpy(proto->type, desc,5);
140 proto->rcvfunc = rcvfunc;
141 proto->header_length = 5 + 3; /* snap + 802.2 */
142 proto->request = snap_request;
143 list_add_rcu(&proto->node, &snap_list);
144 }
145out:
146 spin_unlock_bh(&snap_lock);
147
148 synchronize_net();
149 return proto;
150}
151
152/*
153 * Unregister SNAP clients. Protocols no longer want to play with us ...
154 */
155void unregister_snap_client(struct datalink_proto *proto)
156{
157 spin_lock_bh(&snap_lock);
158 list_del_rcu(&proto->node);
159 spin_unlock_bh(&snap_lock);
160
161 synchronize_net();
162
163 kfree(proto);
164}
165
166MODULE_LICENSE("GPL");