Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * SNAP data link layer. Derived from 802.2 |
| 3 | * |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 4 | * Alan Cox <alan@lxorguk.ukuu.org.uk>, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame^] | 17 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <net/datalink.h> |
| 19 | #include <net/llc.h> |
| 20 | #include <net/psnap.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/in.h> |
| 23 | #include <linux/init.h> |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 24 | #include <linux/rculist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | static LIST_HEAD(snap_list); |
| 27 | static DEFINE_SPINLOCK(snap_lock); |
| 28 | static struct llc_sap *snap_sap; |
| 29 | |
| 30 | /* |
| 31 | * Find a snap client by matching the 5 bytes. |
| 32 | */ |
Stephen Hemminger | 7ca98fa | 2009-03-20 05:43:14 +0000 | [diff] [blame] | 33 | static struct datalink_proto *find_snap_client(const unsigned char *desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | struct datalink_proto *proto = NULL, *p; |
| 36 | |
Paul E. McKenney | 696adfe | 2008-07-25 01:45:34 -0700 | [diff] [blame] | 37 | list_for_each_entry_rcu(p, &snap_list, node) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
| 49 | static int snap_rcv(struct sk_buff *skb, struct net_device *dev, |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 50 | struct packet_type *pt, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
| 52 | int rc = 1; |
| 53 | struct datalink_proto *proto; |
| 54 | static struct packet_type snap_packet_type = { |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 55 | .type = cpu_to_be16(ETH_P_SNAP), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Herbert Xu | d92a7db | 2007-08-21 00:06:37 -0700 | [diff] [blame] | 58 | if (unlikely(!pskb_may_pull(skb, 5))) |
| 59 | goto drop; |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | rcu_read_lock(); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 62 | proto = find_snap_client(skb_transport_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | if (proto) { |
| 64 | /* Pass the frame on. */ |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 65 | skb->transport_header += 5; |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 66 | skb_pull_rcsum(skb, 5); |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 67 | rc = proto->rcvfunc(skb, dev, &snap_packet_type, orig_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | rcu_read_unlock(); |
Herbert Xu | d92a7db | 2007-08-21 00:06:37 -0700 | [diff] [blame] | 70 | |
| 71 | if (unlikely(!proto)) |
| 72 | goto drop; |
| 73 | |
| 74 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | return rc; |
Herbert Xu | d92a7db | 2007-08-21 00:06:37 -0700 | [diff] [blame] | 76 | |
| 77 | drop: |
| 78 | kfree_skb(skb); |
| 79 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Put a SNAP header on a frame and pass to 802.2 |
| 84 | */ |
| 85 | static 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 | */ |
| 96 | EXPORT_SYMBOL(register_snap_client); |
| 97 | EXPORT_SYMBOL(unregister_snap_client); |
| 98 | |
Stephen Hemminger | 0117cfa | 2009-02-22 00:03:19 -0800 | [diff] [blame] | 99 | static const char snap_err_msg[] __initconst = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | KERN_CRIT "SNAP - unable to register with 802.2\n"; |
| 101 | |
| 102 | static int __init snap_init(void) |
| 103 | { |
| 104 | snap_sap = llc_sap_open(0xAA, snap_rcv); |
Stephen Hemminger | 0117cfa | 2009-02-22 00:03:19 -0800 | [diff] [blame] | 105 | if (!snap_sap) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | printk(snap_err_msg); |
Stephen Hemminger | 0117cfa | 2009-02-22 00:03:19 -0800 | [diff] [blame] | 107 | return -EBUSY; |
| 108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | module_init(snap_init); |
| 114 | |
| 115 | static void __exit snap_exit(void) |
| 116 | { |
Arnaldo Carvalho de Melo | 6e2144b | 2005-09-22 04:43:05 -0300 | [diff] [blame] | 117 | llc_sap_put(snap_sap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | module_exit(snap_exit); |
| 121 | |
| 122 | |
| 123 | /* |
| 124 | * Register SNAP clients. We don't yet use this for IP. |
| 125 | */ |
Stephen Hemminger | 7ca98fa | 2009-03-20 05:43:14 +0000 | [diff] [blame] | 126 | struct datalink_proto *register_snap_client(const unsigned char *desc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | int (*rcvfunc)(struct sk_buff *, |
YOSHIFUJI Hideaki | 9afa094 | 2007-02-09 23:24:24 +0900 | [diff] [blame] | 128 | struct net_device *, |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 129 | struct packet_type *, |
| 130 | struct net_device *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
| 132 | struct datalink_proto *proto = NULL; |
| 133 | |
| 134 | spin_lock_bh(&snap_lock); |
| 135 | |
| 136 | if (find_snap_client(desc)) |
| 137 | goto out; |
| 138 | |
| 139 | proto = kmalloc(sizeof(*proto), GFP_ATOMIC); |
| 140 | if (proto) { |
Stephen Hemminger | 7ca98fa | 2009-03-20 05:43:14 +0000 | [diff] [blame] | 141 | memcpy(proto->type, desc, 5); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | proto->rcvfunc = rcvfunc; |
| 143 | proto->header_length = 5 + 3; /* snap + 802.2 */ |
| 144 | proto->request = snap_request; |
| 145 | list_add_rcu(&proto->node, &snap_list); |
| 146 | } |
| 147 | out: |
| 148 | spin_unlock_bh(&snap_lock); |
| 149 | |
| 150 | synchronize_net(); |
| 151 | return proto; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Unregister SNAP clients. Protocols no longer want to play with us ... |
| 156 | */ |
| 157 | void unregister_snap_client(struct datalink_proto *proto) |
| 158 | { |
| 159 | spin_lock_bh(&snap_lock); |
| 160 | list_del_rcu(&proto->node); |
| 161 | spin_unlock_bh(&snap_lock); |
| 162 | |
| 163 | synchronize_net(); |
| 164 | |
| 165 | kfree(proto); |
| 166 | } |
| 167 | |
| 168 | MODULE_LICENSE("GPL"); |