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)  *	G8BPQ compatible "AX.25 via ethernet" driver release 004
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *	This code REQUIRES 2.0.0 or higher/ NET3.029
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *	This is a "pseudo" network driver to allow AX.25 over Ethernet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *	using G8BPQ encapsulation. It has been extracted from the protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *	implementation because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *		- things got unreadable within the protocol stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *		- to cure the protocol stack from "feature-ism"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *		- a protocol implementation shouldn't need to know on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *		  which hardware it is running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *		- user-level programs like the AX.25 utilities shouldn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *		  need to know about the hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *		- IP over ethernet encapsulated AX.25 was impossible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *		- rxecho.c did not work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *		- to have room for extensions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *		- it just deserves to "live" as an own driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *	This driver can use any ethernet destination address, and can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *	limited to accept frames from one dedicated ethernet card only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *	Note that the driver sets up the BPQ devices automagically on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *	startup or (if started before the "insmod" of an ethernet device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *	on "ifconfig up". It hopefully will remove the BPQ on "rmmod"ing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *	the ethernet device (in fact: as soon as another ethernet or bpq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *	device gets "ifconfig"ured).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  *	I have heard that several people are thinking of experiments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  *	with highspeed packet radio using existing ethernet cards.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  *	Well, this driver is prepared for this purpose, just add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  *	your tx key control and a txdelay / tailtime algorithm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  *	probably some buffering, and /voila/...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  *	History
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  *	BPQ   001	Joerg(DL1BKE)		Extracted BPQ code from AX.25
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  *						protocol stack and added my own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  *						yet existing patches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  *	BPQ   002	Joerg(DL1BKE)		Scan network device list on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  *						startup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  *	BPQ   003	Joerg(DL1BKE)		Ethernet destination address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  *						and accepted source address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  *						can be configured by an ioctl()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  *						call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  *						Fixed to match Linux networking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  *						changes - 2.1.15.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *	BPQ   004	Joerg(DL1BKE)		Fixed to not lock up on ifconfig.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #include <net/ax25.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #include <linux/inet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #include <net/arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) #include <linux/bpqether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static const char banner[] __initconst = KERN_INFO \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	"AX.25: bpqether driver version 004\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static int bpq_rcv(struct sk_buff *, struct net_device *, struct packet_type *, struct net_device *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static int bpq_device_event(struct notifier_block *, unsigned long, void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static struct packet_type bpq_packet_type __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	.type	= cpu_to_be16(ETH_P_BPQ),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	.func	= bpq_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) static struct notifier_block bpq_dev_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	.notifier_call = bpq_device_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct bpqdev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct list_head bpq_list;	/* list of bpq devices chain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	struct net_device *ethdev;	/* link to ethernet device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	struct net_device *axdev;	/* bpq device (bpq#) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	char   dest_addr[6];		/* ether destination address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	char   acpt_addr[6];		/* accept ether frames from this address only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static LIST_HEAD(bpq_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  * bpqether network devices are paired with ethernet devices below them, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  * form a special "super class" of normal ethernet devices; split their locks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  * off into a separate class since they always nest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static struct lock_class_key bpq_netdev_xmit_lock_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static struct lock_class_key bpq_netdev_addr_lock_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static void bpq_set_lockdep_class_one(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 				      struct netdev_queue *txq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 				      void *_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	lockdep_set_class(&txq->_xmit_lock, &bpq_netdev_xmit_lock_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static void bpq_set_lockdep_class(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	lockdep_set_class(&dev->addr_list_lock, &bpq_netdev_addr_lock_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	netdev_for_each_tx_queue(dev, bpq_set_lockdep_class_one, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* ------------------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)  *	Get the ethernet device for a BPQ device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static inline struct net_device *bpq_get_ether_dev(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	struct bpqdev *bpq = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return bpq ? bpq->ethdev : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  *	Get the BPQ device for the ethernet device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static inline struct net_device *bpq_get_ax25_dev(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	struct bpqdev *bpq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	list_for_each_entry_rcu(bpq, &bpq_devices, bpq_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				lockdep_rtnl_is_held()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		if (bpq->ethdev == dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			return bpq->axdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static inline int dev_is_ethdev(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /* ------------------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  *	Receive an AX.25 frame via an ethernet interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static int bpq_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	char * ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	struct ethhdr *eth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	struct bpqdev *bpq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (!net_eq(dev_net(dev), &init_net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (!pskb_may_pull(skb, sizeof(struct ethhdr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	dev = bpq_get_ax25_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (dev == NULL || !netif_running(dev)) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		goto drop_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	 * if we want to accept frames from just one ethernet device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	 * we check the source address of the sender.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	bpq = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	eth = eth_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (!(bpq->acpt_addr[0] & 0x01) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	    !ether_addr_equal(eth->h_source, bpq->acpt_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		goto drop_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	if (skb_cow(skb, sizeof(struct ethhdr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		goto drop_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	len = skb->data[0] + skb->data[1] * 256 - 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	skb_pull(skb, 2);	/* Remove the length bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	skb_trim(skb, len);	/* Set the length of the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	dev->stats.rx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	dev->stats.rx_bytes += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	ptr = skb_push(skb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	*ptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	skb->protocol = ax25_type_trans(skb, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	netif_rx(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) drop_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  * 	Send an AX.25 frame via an ethernet interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static netdev_tx_t bpq_xmit(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	unsigned char *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	struct bpqdev *bpq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	struct net_device *orig_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (skb->protocol == htons(ETH_P_IP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		return ax25_ip_xmit(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	 * Just to be *really* sure not to send anything if the interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	 * is down, the ethernet device may have gone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	if (!netif_running(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	skb_pull(skb, 1);			/* Drop KISS byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	size = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	 * We're about to mess with the skb which may still shared with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	 * generic networking code so unshare and ensure it's got enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	 * space for the BPQ headers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (skb_cow(skb, AX25_BPQ_HEADER_LEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		if (net_ratelimit())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			pr_err("bpqether: out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	ptr = skb_push(skb, 2);			/* Make space for length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	*ptr++ = (size + 5) % 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	*ptr++ = (size + 5) / 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	bpq = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	orig_dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if ((dev = bpq_get_ether_dev(dev)) == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		orig_dev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	skb->protocol = ax25_type_trans(skb, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	dev_hard_header(skb, dev, ETH_P_BPQ, bpq->dest_addr, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	dev->stats.tx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	dev->stats.tx_bytes+=skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)   
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	dev_queue_xmit(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	netif_wake_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)  *	Set AX.25 callsign
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static int bpq_set_mac_address(struct net_device *dev, void *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)     struct sockaddr *sa = (struct sockaddr *)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)     memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)     return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /*	Ioctl commands
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)  *		SIOCSBPQETHOPT		reserved for enhancements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)  *		SIOCSBPQETHADDR		set the destination and accepted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)  *					source ethernet address (broadcast
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)  *					or multicast: accept all)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) static int bpq_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	struct bpq_ethaddr __user *ethaddr = ifr->ifr_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	struct bpqdev *bpq = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	struct bpq_req req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (!capable(CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		case SIOCSBPQETHOPT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			if (copy_from_user(&req, ifr->ifr_data, sizeof(struct bpq_req)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 				return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			switch (req.cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 				case SIOCGBPQETHPARAM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 				case SIOCSBPQETHPARAM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 				default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 					return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		case SIOCSBPQETHADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			if (copy_from_user(bpq->dest_addr, ethaddr->destination, ETH_ALEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 				return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			if (copy_from_user(bpq->acpt_addr, ethaddr->accept, ETH_ALEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 				return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)  * open/close a device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static int bpq_open(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	netif_start_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static int bpq_close(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	netif_stop_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) /* ------------------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)  *	Proc filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) static void *bpq_seq_start(struct seq_file *seq, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	__acquires(RCU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	int i = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	struct bpqdev *bpqdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	if (*pos == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		return SEQ_START_TOKEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	list_for_each_entry_rcu(bpqdev, &bpq_devices, bpq_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		if (i == *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			return bpqdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static void *bpq_seq_next(struct seq_file *seq, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	struct list_head *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	struct bpqdev *bpqdev = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	++*pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	if (v == SEQ_START_TOKEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		p = rcu_dereference(list_next_rcu(&bpq_devices));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		p = rcu_dereference(list_next_rcu(&bpqdev->bpq_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	return (p == &bpq_devices) ? NULL 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		: list_entry(p, struct bpqdev, bpq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static void bpq_seq_stop(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	__releases(RCU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static int bpq_seq_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	if (v == SEQ_START_TOKEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		seq_puts(seq, 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			 "dev   ether      destination        accept from\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		const struct bpqdev *bpqdev = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		seq_printf(seq, "%-5s %-10s %pM  ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 			bpqdev->axdev->name, bpqdev->ethdev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 			bpqdev->dest_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		if (is_multicast_ether_addr(bpqdev->acpt_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 			seq_printf(seq, "*\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 			seq_printf(seq, "%pM\n", bpqdev->acpt_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static const struct seq_operations bpq_seqops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	.start = bpq_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	.next = bpq_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	.stop = bpq_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	.show = bpq_seq_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) /* ------------------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static const struct net_device_ops bpq_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	.ndo_open	     = bpq_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	.ndo_stop	     = bpq_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	.ndo_start_xmit	     = bpq_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	.ndo_set_mac_address = bpq_set_mac_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	.ndo_do_ioctl	     = bpq_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) static void bpq_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	dev->netdev_ops	     = &bpq_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	dev->needs_free_netdev = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	memcpy(dev->dev_addr,  &ax25_defaddr, AX25_ADDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	dev->flags      = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	dev->features	= NETIF_F_LLTX;	/* Allow recursion */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) #if IS_ENABLED(CONFIG_AX25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	dev->header_ops      = &ax25_header_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	dev->type            = ARPHRD_AX25;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	dev->mtu             = AX25_DEF_PACLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	dev->addr_len        = AX25_ADDR_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)  *	Setup a new device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) static int bpq_new_device(struct net_device *edev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	struct net_device *ndev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	struct bpqdev *bpq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	ndev = alloc_netdev(sizeof(struct bpqdev), "bpq%d", NET_NAME_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 			    bpq_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	if (!ndev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	bpq = netdev_priv(ndev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	dev_hold(edev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	bpq->ethdev = edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	bpq->axdev = ndev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	eth_broadcast_addr(bpq->dest_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	eth_broadcast_addr(bpq->acpt_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	err = register_netdevice(ndev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	bpq_set_lockdep_class(ndev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	/* List protected by RTNL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	list_add_rcu(&bpq->bpq_list, &bpq_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)  error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	dev_put(edev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	free_netdev(ndev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) static void bpq_free_device(struct net_device *ndev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	struct bpqdev *bpq = netdev_priv(ndev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	dev_put(bpq->ethdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	list_del_rcu(&bpq->bpq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	unregister_netdevice(ndev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)  *	Handle device status changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) static int bpq_device_event(struct notifier_block *this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 			    unsigned long event, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	if (!net_eq(dev_net(dev), &init_net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	if (!dev_is_ethdev(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	switch (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	case NETDEV_UP:		/* new ethernet device -> new BPQ interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		if (bpq_get_ax25_dev(dev) == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 			bpq_new_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	case NETDEV_DOWN:	/* ethernet device closed -> close BPQ interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		if ((dev = bpq_get_ax25_dev(dev)) != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 			dev_close(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	case NETDEV_UNREGISTER:	/* ethernet device removed -> free BPQ interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		if ((dev = bpq_get_ax25_dev(dev)) != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 			bpq_free_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) /* ------------------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)  * Initialize driver. To be called from af_ax25 if not compiled as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)  * module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) static int __init bpq_init_driver(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	if (!proc_create_seq("bpqether", 0444, init_net.proc_net, &bpq_seqops)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 			"bpq: cannot create /proc/net/bpqether entry.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) #endif  /* CONFIG_PROC_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	dev_add_pack(&bpq_packet_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	register_netdevice_notifier(&bpq_dev_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	printk(banner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) static void __exit bpq_cleanup_driver(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	struct bpqdev *bpq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	dev_remove_pack(&bpq_packet_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	unregister_netdevice_notifier(&bpq_dev_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	remove_proc_entry("bpqether", init_net.proc_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	while (!list_empty(&bpq_devices)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 		bpq = list_entry(bpq_devices.next, struct bpqdev, bpq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		bpq_free_device(bpq->axdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) MODULE_AUTHOR("Joerg Reuter DL1BKE <jreuter@yaina.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) MODULE_DESCRIPTION("Transmit and receive AX.25 packets over Ethernet");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) module_init(bpq_init_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) module_exit(bpq_cleanup_driver);