Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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