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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Generic HDLC support routines for Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Cisco HDLC support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2000 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/hdlc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/inetdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/pkt_sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #undef DEBUG_HARD_HEADER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define CISCO_MULTICAST		0x8F	/* Cisco multicast address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define CISCO_UNICAST		0x0F	/* Cisco unicast address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define CISCO_KEEPALIVE		0x8035	/* Cisco keepalive protocol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define CISCO_SYS_INFO		0x2000	/* Cisco interface/system info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define CISCO_ADDR_REQ		0	/* Cisco address request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define CISCO_ADDR_REPLY	1	/* Cisco address reply */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define CISCO_KEEPALIVE_REQ	2	/* Cisco keepalive request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) struct hdlc_header {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	u8 address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	u8 control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	__be16 protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) }__packed;
^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) struct cisco_packet {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	__be32 type;		/* code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	__be32 par1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	__be32 par2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	__be16 rel;		/* reliability */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	__be32 time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }__packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define	CISCO_PACKET_LEN	18
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define	CISCO_BIG_PACKET_LEN	20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) struct cisco_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	cisco_proto settings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct timer_list timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	unsigned long last_poll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	int up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	u32 txseq; /* TX sequence number, 0 = none */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	u32 rxseq; /* RX sequence number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static inline struct cisco_state* state(hdlc_device *hdlc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	return (struct cisco_state *)hdlc->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			     u16 type, const void *daddr, const void *saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			     unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct hdlc_header *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) #ifdef DEBUG_HARD_HEADER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	netdev_dbg(dev, "%s called\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	skb_push(skb, sizeof(struct hdlc_header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	data = (struct hdlc_header*)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (type == CISCO_KEEPALIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		data->address = CISCO_MULTICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		data->address = CISCO_UNICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	data->control = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	data->protocol = htons(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	return sizeof(struct hdlc_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^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 void cisco_keepalive_send(struct net_device *dev, u32 type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 				 __be32 par1, __be32 par2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct cisco_packet *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	skb = dev_alloc_skb(sizeof(struct hdlc_header) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			    sizeof(struct cisco_packet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		netdev_warn(dev, "Memory squeeze on %s()\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	skb_reserve(skb, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	cisco_hard_header(skb, dev, CISCO_KEEPALIVE, NULL, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	data = (struct cisco_packet*)(skb->data + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	data->type = htonl(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	data->par1 = par1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	data->par2 = par2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	data->rel = cpu_to_be16(0xFFFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	/* we will need do_div here if 1000 % HZ != 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	data->time = htonl((jiffies - INITIAL_JIFFIES) * (1000 / HZ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	skb_put(skb, sizeof(struct cisco_packet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	skb->priority = TC_PRIO_CONTROL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	skb->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	skb->protocol = htons(ETH_P_HDLC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	dev_queue_xmit(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static __be16 cisco_type_trans(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct hdlc_header *data = (struct hdlc_header*)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (skb->len < sizeof(struct hdlc_header))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return cpu_to_be16(ETH_P_HDLC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (data->address != CISCO_MULTICAST &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	    data->address != CISCO_UNICAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		return cpu_to_be16(ETH_P_HDLC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	switch (data->protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	case cpu_to_be16(ETH_P_IP):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	case cpu_to_be16(ETH_P_IPX):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	case cpu_to_be16(ETH_P_IPV6):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		skb_pull(skb, sizeof(struct hdlc_header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		return data->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		return cpu_to_be16(ETH_P_HDLC);
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static int cisco_rx(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	struct net_device *dev = skb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	hdlc_device *hdlc = dev_to_hdlc(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	struct cisco_state *st = state(hdlc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	struct hdlc_header *data = (struct hdlc_header*)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	struct cisco_packet *cisco_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	struct in_device *in_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	__be32 addr, mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	u32 ack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (skb->len < sizeof(struct hdlc_header))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		goto rx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (data->address != CISCO_MULTICAST &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	    data->address != CISCO_UNICAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		goto rx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	switch (ntohs(data->protocol)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	case CISCO_SYS_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		/* Packet is not needed, drop it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		dev_kfree_skb_any(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	case CISCO_KEEPALIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		if ((skb->len != sizeof(struct hdlc_header) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		     CISCO_PACKET_LEN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		    (skb->len != sizeof(struct hdlc_header) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		     CISCO_BIG_PACKET_LEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			netdev_info(dev, "Invalid length of Cisco control packet (%d bytes)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 				    skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			goto rx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		cisco_data = (struct cisco_packet*)(skb->data + sizeof
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 						    (struct hdlc_header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		switch (ntohl (cisco_data->type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		case CISCO_ADDR_REQ: /* Stolen from syncppp.c :-) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			in_dev = __in_dev_get_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			mask = ~cpu_to_be32(0); /* is the mask correct? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			if (in_dev != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 				const struct in_ifaddr *ifa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 				in_dev_for_each_ifa_rcu(ifa, in_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 					if (strcmp(dev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 						   ifa->ifa_label) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 						addr = ifa->ifa_local;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 						mask = ifa->ifa_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 						break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 				cisco_keepalive_send(dev, CISCO_ADDR_REPLY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 						     addr, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			dev_kfree_skb_any(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		case CISCO_ADDR_REPLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			netdev_info(dev, "Unexpected Cisco IP address reply\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			goto rx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		case CISCO_KEEPALIVE_REQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			spin_lock(&st->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			st->rxseq = ntohl(cisco_data->par1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			ack = ntohl(cisco_data->par2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			if (ack && (ack == st->txseq ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 				    /* our current REQ may be in transit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 				    ack == st->txseq - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 				st->last_poll = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 				if (!st->up) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 					u32 sec, min, hrs, days;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 					sec = ntohl(cisco_data->time) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 					min = sec / 60; sec -= min * 60;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 					hrs = min / 60; min -= hrs * 60;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 					days = hrs / 24; hrs -= days * 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 					netdev_info(dev, "Link up (peer uptime %ud%uh%um%us)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 						    days, hrs, min, sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 					netif_dormant_off(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 					st->up = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			spin_unlock(&st->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			dev_kfree_skb_any(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		} /* switch (keepalive type) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	} /* switch (protocol) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	netdev_info(dev, "Unsupported protocol %x\n", ntohs(data->protocol));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	dev_kfree_skb_any(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) rx_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	dev->stats.rx_errors++; /* Mark error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	dev_kfree_skb_any(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static void cisco_timer(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	struct cisco_state *st = from_timer(st, t, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	struct net_device *dev = st->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	spin_lock(&st->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (st->up &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	    time_after(jiffies, st->last_poll + st->settings.timeout * HZ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		st->up = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		netdev_info(dev, "Link down\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		netif_dormant_on(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ, htonl(++st->txseq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			     htonl(st->rxseq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	spin_unlock(&st->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	st->timer.expires = jiffies + st->settings.interval * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	add_timer(&st->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static void cisco_start(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	hdlc_device *hdlc = dev_to_hdlc(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	struct cisco_state *st = state(hdlc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	spin_lock_irqsave(&st->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	st->up = st->txseq = st->rxseq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	spin_unlock_irqrestore(&st->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	st->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	timer_setup(&st->timer, cisco_timer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	st->timer.expires = jiffies + HZ; /* First poll after 1 s */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	add_timer(&st->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^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) static void cisco_stop(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	hdlc_device *hdlc = dev_to_hdlc(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	struct cisco_state *st = state(hdlc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	del_timer_sync(&st->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	spin_lock_irqsave(&st->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	netif_dormant_on(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	st->up = st->txseq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	spin_unlock_irqrestore(&st->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static struct hdlc_proto proto = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	.start		= cisco_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	.stop		= cisco_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	.type_trans	= cisco_type_trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	.ioctl		= cisco_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	.netif_rx	= cisco_rx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	.module		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static const struct header_ops cisco_header_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	.create = cisco_hard_header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	cisco_proto __user *cisco_s = ifr->ifr_settings.ifs_ifsu.cisco;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	const size_t size = sizeof(cisco_proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	cisco_proto new_settings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	hdlc_device *hdlc = dev_to_hdlc(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	switch (ifr->ifr_settings.type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	case IF_GET_PROTO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		if (dev_to_hdlc(dev)->proto != &proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		ifr->ifr_settings.type = IF_PROTO_CISCO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		if (ifr->ifr_settings.size < size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			ifr->ifr_settings.size = size; /* data size wanted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		if (copy_to_user(cisco_s, &state(hdlc)->settings, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	case IF_PROTO_CISCO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		if (!capable(CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		if (dev->flags & IFF_UP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		if (copy_from_user(&new_settings, cisco_s, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		if (new_settings.interval < 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		    new_settings.timeout < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		result = hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		result = attach_hdlc_protocol(dev, &proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 					      sizeof(struct cisco_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		memcpy(&state(hdlc)->settings, &new_settings, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		spin_lock_init(&state(hdlc)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		dev->header_ops = &cisco_header_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		dev->hard_header_len = sizeof(struct hdlc_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		dev->type = ARPHRD_CISCO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		netif_dormant_on(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) static int __init mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	register_hdlc_protocol(&proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^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 __exit mod_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	unregister_hdlc_protocol(&proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) module_init(mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) module_exit(mod_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) MODULE_DESCRIPTION("Cisco HDLC protocol support for generic HDLC");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) MODULE_LICENSE("GPL v2");