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