xref: /openbmc/linux/drivers/net/wan/hdlc_cisco.c (revision 1a545ebe380bf4c1433e3c136e35a77764fda5ad)
125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Generic HDLC support routines for Linux
41da177e4SLinus Torvalds  * Cisco HDLC support
51da177e4SLinus Torvalds  *
6eb2a2fd9SKrzysztof Halasa  * Copyright (C) 2000 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/errno.h>
101da177e4SLinus Torvalds #include <linux/hdlc.h>
114dfce407SKrzysztof Hałasa #include <linux/if_arp.h>
124dfce407SKrzysztof Hałasa #include <linux/inetdevice.h>
134dfce407SKrzysztof Hałasa #include <linux/init.h>
144dfce407SKrzysztof Hałasa #include <linux/kernel.h>
154dfce407SKrzysztof Hałasa #include <linux/module.h>
164dfce407SKrzysztof Hałasa #include <linux/pkt_sched.h>
174dfce407SKrzysztof Hałasa #include <linux/poll.h>
184dfce407SKrzysztof Hałasa #include <linux/rtnetlink.h>
194dfce407SKrzysztof Hałasa #include <linux/skbuff.h>
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #undef DEBUG_HARD_HEADER
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #define CISCO_MULTICAST		0x8F	/* Cisco multicast address */
241da177e4SLinus Torvalds #define CISCO_UNICAST		0x0F	/* Cisco unicast address */
251da177e4SLinus Torvalds #define CISCO_KEEPALIVE		0x8035	/* Cisco keepalive protocol */
261da177e4SLinus Torvalds #define CISCO_SYS_INFO		0x2000	/* Cisco interface/system info */
271da177e4SLinus Torvalds #define CISCO_ADDR_REQ		0	/* Cisco address request */
281da177e4SLinus Torvalds #define CISCO_ADDR_REPLY	1	/* Cisco address reply */
291da177e4SLinus Torvalds #define CISCO_KEEPALIVE_REQ	2	/* Cisco keepalive request */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds 
32eb2a2fd9SKrzysztof Halasa struct hdlc_header {
33eb2a2fd9SKrzysztof Halasa 	u8 address;
34eb2a2fd9SKrzysztof Halasa 	u8 control;
35abf17ffdSKrzysztof Halasa 	__be16 protocol;
36ba2d3587SEric Dumazet }__packed;
37eb2a2fd9SKrzysztof Halasa 
38eb2a2fd9SKrzysztof Halasa 
39eb2a2fd9SKrzysztof Halasa struct cisco_packet {
40abf17ffdSKrzysztof Halasa 	__be32 type;		/* code */
41abf17ffdSKrzysztof Halasa 	__be32 par1;
42abf17ffdSKrzysztof Halasa 	__be32 par2;
43abf17ffdSKrzysztof Halasa 	__be16 rel;		/* reliability */
44abf17ffdSKrzysztof Halasa 	__be32 time;
45ba2d3587SEric Dumazet }__packed;
46eb2a2fd9SKrzysztof Halasa #define	CISCO_PACKET_LEN	18
47eb2a2fd9SKrzysztof Halasa #define	CISCO_BIG_PACKET_LEN	20
48eb2a2fd9SKrzysztof Halasa 
49eb2a2fd9SKrzysztof Halasa 
50eb2a2fd9SKrzysztof Halasa struct cisco_state {
51eb2a2fd9SKrzysztof Halasa 	cisco_proto settings;
52eb2a2fd9SKrzysztof Halasa 
53eb2a2fd9SKrzysztof Halasa 	struct timer_list timer;
54d26c089eSKees Cook 	struct net_device *dev;
55aff26e2fSKrzysztof Halasa 	spinlock_t lock;
56eb2a2fd9SKrzysztof Halasa 	unsigned long last_poll;
57eb2a2fd9SKrzysztof Halasa 	int up;
589652041dSKrzysztof Halasa 	u32 txseq; /* TX sequence number, 0 = none */
59eb2a2fd9SKrzysztof Halasa 	u32 rxseq; /* RX sequence number */
60eb2a2fd9SKrzysztof Halasa };
61eb2a2fd9SKrzysztof Halasa 
62eb2a2fd9SKrzysztof Halasa 
63eb2a2fd9SKrzysztof Halasa static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr);
64eb2a2fd9SKrzysztof Halasa 
65eb2a2fd9SKrzysztof Halasa 
66eb2a2fd9SKrzysztof Halasa static inline struct cisco_state* state(hdlc_device *hdlc)
67eb2a2fd9SKrzysztof Halasa {
684dfce407SKrzysztof Hałasa 	return (struct cisco_state *)hdlc->state;
69eb2a2fd9SKrzysztof Halasa }
70eb2a2fd9SKrzysztof Halasa 
71eb2a2fd9SKrzysztof Halasa 
721da177e4SLinus Torvalds static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev,
733b04dddeSStephen Hemminger 			     u16 type, const void *daddr, const void *saddr,
741da177e4SLinus Torvalds 			     unsigned int len)
751da177e4SLinus Torvalds {
76eb2a2fd9SKrzysztof Halasa 	struct hdlc_header *data;
771da177e4SLinus Torvalds #ifdef DEBUG_HARD_HEADER
78c68d7248SChen Zhou 	netdev_dbg(dev, "%s called\n", __func__);
791da177e4SLinus Torvalds #endif
801da177e4SLinus Torvalds 
81eb2a2fd9SKrzysztof Halasa 	skb_push(skb, sizeof(struct hdlc_header));
82eb2a2fd9SKrzysztof Halasa 	data = (struct hdlc_header*)skb->data;
831da177e4SLinus Torvalds 	if (type == CISCO_KEEPALIVE)
841da177e4SLinus Torvalds 		data->address = CISCO_MULTICAST;
851da177e4SLinus Torvalds 	else
861da177e4SLinus Torvalds 		data->address = CISCO_UNICAST;
871da177e4SLinus Torvalds 	data->control = 0;
881da177e4SLinus Torvalds 	data->protocol = htons(type);
891da177e4SLinus Torvalds 
90eb2a2fd9SKrzysztof Halasa 	return sizeof(struct hdlc_header);
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds static void cisco_keepalive_send(struct net_device *dev, u32 type,
96abf17ffdSKrzysztof Halasa 				 __be32 par1, __be32 par2)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds 	struct sk_buff *skb;
99eb2a2fd9SKrzysztof Halasa 	struct cisco_packet *data;
1001da177e4SLinus Torvalds 
101eb2a2fd9SKrzysztof Halasa 	skb = dev_alloc_skb(sizeof(struct hdlc_header) +
102eb2a2fd9SKrzysztof Halasa 			    sizeof(struct cisco_packet));
1031da177e4SLinus Torvalds 	if (!skb) {
104c68d7248SChen Zhou 		netdev_warn(dev, "Memory squeeze on %s()\n", __func__);
1051da177e4SLinus Torvalds 		return;
1061da177e4SLinus Torvalds 	}
1071da177e4SLinus Torvalds 	skb_reserve(skb, 4);
1081da177e4SLinus Torvalds 	cisco_hard_header(skb, dev, CISCO_KEEPALIVE, NULL, NULL, 0);
109eb2a2fd9SKrzysztof Halasa 	data = (struct cisco_packet*)(skb->data + 4);
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	data->type = htonl(type);
112abf17ffdSKrzysztof Halasa 	data->par1 = par1;
113abf17ffdSKrzysztof Halasa 	data->par2 = par2;
11409640e63SHarvey Harrison 	data->rel = cpu_to_be16(0xFFFF);
1151da177e4SLinus Torvalds 	/* we will need do_div here if 1000 % HZ != 0 */
1161da177e4SLinus Torvalds 	data->time = htonl((jiffies - INITIAL_JIFFIES) * (1000 / HZ));
1171da177e4SLinus Torvalds 
118eb2a2fd9SKrzysztof Halasa 	skb_put(skb, sizeof(struct cisco_packet));
1191da177e4SLinus Torvalds 	skb->priority = TC_PRIO_CONTROL;
1201da177e4SLinus Torvalds 	skb->dev = dev;
121c1d2bbe1SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	dev_queue_xmit(skb);
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 
128ab611487SAlexey Dobriyan static __be16 cisco_type_trans(struct sk_buff *skb, struct net_device *dev)
1291da177e4SLinus Torvalds {
130eb2a2fd9SKrzysztof Halasa 	struct hdlc_header *data = (struct hdlc_header*)skb->data;
1311da177e4SLinus Torvalds 
132eb2a2fd9SKrzysztof Halasa 	if (skb->len < sizeof(struct hdlc_header))
13309640e63SHarvey Harrison 		return cpu_to_be16(ETH_P_HDLC);
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	if (data->address != CISCO_MULTICAST &&
1361da177e4SLinus Torvalds 	    data->address != CISCO_UNICAST)
13709640e63SHarvey Harrison 		return cpu_to_be16(ETH_P_HDLC);
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	switch (data->protocol) {
14009640e63SHarvey Harrison 	case cpu_to_be16(ETH_P_IP):
14109640e63SHarvey Harrison 	case cpu_to_be16(ETH_P_IPX):
14209640e63SHarvey Harrison 	case cpu_to_be16(ETH_P_IPV6):
143eb2a2fd9SKrzysztof Halasa 		skb_pull(skb, sizeof(struct hdlc_header));
1441da177e4SLinus Torvalds 		return data->protocol;
1451da177e4SLinus Torvalds 	default:
14609640e63SHarvey Harrison 		return cpu_to_be16(ETH_P_HDLC);
1471da177e4SLinus Torvalds 	}
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds static int cisco_rx(struct sk_buff *skb)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds 	struct net_device *dev = skb->dev;
1541da177e4SLinus Torvalds 	hdlc_device *hdlc = dev_to_hdlc(dev);
155aff26e2fSKrzysztof Halasa 	struct cisco_state *st = state(hdlc);
156eb2a2fd9SKrzysztof Halasa 	struct hdlc_header *data = (struct hdlc_header*)skb->data;
157eb2a2fd9SKrzysztof Halasa 	struct cisco_packet *cisco_data;
1581da177e4SLinus Torvalds 	struct in_device *in_dev;
159a144ea4bSAl Viro 	__be32 addr, mask;
1609652041dSKrzysztof Halasa 	u32 ack;
1611da177e4SLinus Torvalds 
162eb2a2fd9SKrzysztof Halasa 	if (skb->len < sizeof(struct hdlc_header))
1631da177e4SLinus Torvalds 		goto rx_error;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	if (data->address != CISCO_MULTICAST &&
1661da177e4SLinus Torvalds 	    data->address != CISCO_UNICAST)
1671da177e4SLinus Torvalds 		goto rx_error;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	switch (ntohs(data->protocol)) {
1701da177e4SLinus Torvalds 	case CISCO_SYS_INFO:
1711da177e4SLinus Torvalds 		/* Packet is not needed, drop it. */
1721da177e4SLinus Torvalds 		dev_kfree_skb_any(skb);
1731da177e4SLinus Torvalds 		return NET_RX_SUCCESS;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	case CISCO_KEEPALIVE:
176eb2a2fd9SKrzysztof Halasa 		if ((skb->len != sizeof(struct hdlc_header) +
177eb2a2fd9SKrzysztof Halasa 		     CISCO_PACKET_LEN) &&
178eb2a2fd9SKrzysztof Halasa 		    (skb->len != sizeof(struct hdlc_header) +
179eb2a2fd9SKrzysztof Halasa 		     CISCO_BIG_PACKET_LEN)) {
18012a3bfefSJoe Perches 			netdev_info(dev, "Invalid length of Cisco control packet (%d bytes)\n",
18112a3bfefSJoe Perches 				    skb->len);
1821da177e4SLinus Torvalds 			goto rx_error;
1831da177e4SLinus Torvalds 		}
1841da177e4SLinus Torvalds 
185eb2a2fd9SKrzysztof Halasa 		cisco_data = (struct cisco_packet*)(skb->data + sizeof
186eb2a2fd9SKrzysztof Halasa 						    (struct hdlc_header));
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 		switch (ntohl (cisco_data->type)) {
1891da177e4SLinus Torvalds 		case CISCO_ADDR_REQ: /* Stolen from syncppp.c :-) */
19095ae6b22SEric Dumazet 			rcu_read_lock();
19195ae6b22SEric Dumazet 			in_dev = __in_dev_get_rcu(dev);
1921da177e4SLinus Torvalds 			addr = 0;
19309640e63SHarvey Harrison 			mask = ~cpu_to_be32(0); /* is the mask correct? */
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 			if (in_dev != NULL) {
196cb8f1478SFlorian Westphal 				const struct in_ifaddr *ifa;
1971da177e4SLinus Torvalds 
198cb8f1478SFlorian Westphal 				in_dev_for_each_ifa_rcu(ifa, in_dev) {
1991da177e4SLinus Torvalds 					if (strcmp(dev->name,
200cb8f1478SFlorian Westphal 						   ifa->ifa_label) == 0) {
201cb8f1478SFlorian Westphal 						addr = ifa->ifa_local;
202cb8f1478SFlorian Westphal 						mask = ifa->ifa_mask;
2031da177e4SLinus Torvalds 						break;
2041da177e4SLinus Torvalds 					}
2051da177e4SLinus Torvalds 				}
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 				cisco_keepalive_send(dev, CISCO_ADDR_REPLY,
2081da177e4SLinus Torvalds 						     addr, mask);
2091da177e4SLinus Torvalds 			}
21095ae6b22SEric Dumazet 			rcu_read_unlock();
2111da177e4SLinus Torvalds 			dev_kfree_skb_any(skb);
2121da177e4SLinus Torvalds 			return NET_RX_SUCCESS;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 		case CISCO_ADDR_REPLY:
21512a3bfefSJoe Perches 			netdev_info(dev, "Unexpected Cisco IP address reply\n");
2161da177e4SLinus Torvalds 			goto rx_error;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 		case CISCO_KEEPALIVE_REQ:
219aff26e2fSKrzysztof Halasa 			spin_lock(&st->lock);
220aff26e2fSKrzysztof Halasa 			st->rxseq = ntohl(cisco_data->par1);
2219652041dSKrzysztof Halasa 			ack = ntohl(cisco_data->par2);
2229652041dSKrzysztof Halasa 			if (ack && (ack == st->txseq ||
2239652041dSKrzysztof Halasa 				    /* our current REQ may be in transit */
2249652041dSKrzysztof Halasa 				    ack == st->txseq - 1)) {
225aff26e2fSKrzysztof Halasa 				st->last_poll = jiffies;
226aff26e2fSKrzysztof Halasa 				if (!st->up) {
2271da177e4SLinus Torvalds 					u32 sec, min, hrs, days;
2281da177e4SLinus Torvalds 					sec = ntohl(cisco_data->time) / 1000;
2291da177e4SLinus Torvalds 					min = sec / 60; sec -= min * 60;
2301da177e4SLinus Torvalds 					hrs = min / 60; min -= hrs * 60;
2311da177e4SLinus Torvalds 					days = hrs / 24; hrs -= days * 24;
23212a3bfefSJoe Perches 					netdev_info(dev, "Link up (peer uptime %ud%uh%um%us)\n",
23312a3bfefSJoe Perches 						    days, hrs, min, sec);
234c2ce9204SKrzysztof Halasa 					netif_dormant_off(dev);
235aff26e2fSKrzysztof Halasa 					st->up = 1;
2361da177e4SLinus Torvalds 				}
2371da177e4SLinus Torvalds 			}
238aff26e2fSKrzysztof Halasa 			spin_unlock(&st->lock);
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 			dev_kfree_skb_any(skb);
2411da177e4SLinus Torvalds 			return NET_RX_SUCCESS;
2421da177e4SLinus Torvalds 		} /* switch (keepalive type) */
2431da177e4SLinus Torvalds 	} /* switch (protocol) */
2441da177e4SLinus Torvalds 
24512a3bfefSJoe Perches 	netdev_info(dev, "Unsupported protocol %x\n", ntohs(data->protocol));
2461da177e4SLinus Torvalds 	dev_kfree_skb_any(skb);
2471da177e4SLinus Torvalds 	return NET_RX_DROP;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds rx_error:
250198191c4SKrzysztof Halasa 	dev->stats.rx_errors++; /* Mark error */
2511da177e4SLinus Torvalds 	dev_kfree_skb_any(skb);
2521da177e4SLinus Torvalds 	return NET_RX_DROP;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 
257d26c089eSKees Cook static void cisco_timer(struct timer_list *t)
2581da177e4SLinus Torvalds {
259d26c089eSKees Cook 	struct cisco_state *st = from_timer(st, t, timer);
260d26c089eSKees Cook 	struct net_device *dev = st->dev;
2611da177e4SLinus Torvalds 
262aff26e2fSKrzysztof Halasa 	spin_lock(&st->lock);
263aff26e2fSKrzysztof Halasa 	if (st->up &&
264aff26e2fSKrzysztof Halasa 	    time_after(jiffies, st->last_poll + st->settings.timeout * HZ)) {
265aff26e2fSKrzysztof Halasa 		st->up = 0;
26612a3bfefSJoe Perches 		netdev_info(dev, "Link down\n");
267c2ce9204SKrzysztof Halasa 		netif_dormant_on(dev);
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 
270aff26e2fSKrzysztof Halasa 	cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ, htonl(++st->txseq),
271aff26e2fSKrzysztof Halasa 			     htonl(st->rxseq));
272aff26e2fSKrzysztof Halasa 	spin_unlock(&st->lock);
273aff26e2fSKrzysztof Halasa 
274aff26e2fSKrzysztof Halasa 	st->timer.expires = jiffies + st->settings.interval * HZ;
275aff26e2fSKrzysztof Halasa 	add_timer(&st->timer);
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds static void cisco_start(struct net_device *dev)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	hdlc_device *hdlc = dev_to_hdlc(dev);
283aff26e2fSKrzysztof Halasa 	struct cisco_state *st = state(hdlc);
284aff26e2fSKrzysztof Halasa 	unsigned long flags;
2851da177e4SLinus Torvalds 
286aff26e2fSKrzysztof Halasa 	spin_lock_irqsave(&st->lock, flags);
2879652041dSKrzysztof Halasa 	st->up = st->txseq = st->rxseq = 0;
288aff26e2fSKrzysztof Halasa 	spin_unlock_irqrestore(&st->lock, flags);
289aff26e2fSKrzysztof Halasa 
290d26c089eSKees Cook 	st->dev = dev;
291d26c089eSKees Cook 	timer_setup(&st->timer, cisco_timer, 0);
292aff26e2fSKrzysztof Halasa 	st->timer.expires = jiffies + HZ; /* First poll after 1 s */
293aff26e2fSKrzysztof Halasa 	add_timer(&st->timer);
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds static void cisco_stop(struct net_device *dev)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	hdlc_device *hdlc = dev_to_hdlc(dev);
301aff26e2fSKrzysztof Halasa 	struct cisco_state *st = state(hdlc);
302aff26e2fSKrzysztof Halasa 	unsigned long flags;
303aff26e2fSKrzysztof Halasa 
304aff26e2fSKrzysztof Halasa 	del_timer_sync(&st->timer);
305aff26e2fSKrzysztof Halasa 
306aff26e2fSKrzysztof Halasa 	spin_lock_irqsave(&st->lock, flags);
307c2ce9204SKrzysztof Halasa 	netif_dormant_on(dev);
3089652041dSKrzysztof Halasa 	st->up = st->txseq = 0;
309aff26e2fSKrzysztof Halasa 	spin_unlock_irqrestore(&st->lock, flags);
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 
313eb2a2fd9SKrzysztof Halasa static struct hdlc_proto proto = {
314eb2a2fd9SKrzysztof Halasa 	.start		= cisco_start,
315eb2a2fd9SKrzysztof Halasa 	.stop		= cisco_stop,
316eb2a2fd9SKrzysztof Halasa 	.type_trans	= cisco_type_trans,
317eb2a2fd9SKrzysztof Halasa 	.ioctl		= cisco_ioctl,
31840d25142SKrzysztof Halasa 	.netif_rx	= cisco_rx,
319eb2a2fd9SKrzysztof Halasa 	.module		= THIS_MODULE,
320eb2a2fd9SKrzysztof Halasa };
321eb2a2fd9SKrzysztof Halasa 
3223b04dddeSStephen Hemminger static const struct header_ops cisco_header_ops = {
3233b04dddeSStephen Hemminger 	.create = cisco_hard_header,
3243b04dddeSStephen Hemminger };
325eb2a2fd9SKrzysztof Halasa 
326eb2a2fd9SKrzysztof Halasa static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	cisco_proto __user *cisco_s = ifr->ifr_settings.ifs_ifsu.cisco;
3291da177e4SLinus Torvalds 	const size_t size = sizeof(cisco_proto);
3301da177e4SLinus Torvalds 	cisco_proto new_settings;
3311da177e4SLinus Torvalds 	hdlc_device *hdlc = dev_to_hdlc(dev);
3321da177e4SLinus Torvalds 	int result;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	switch (ifr->ifr_settings.type) {
3351da177e4SLinus Torvalds 	case IF_GET_PROTO:
336eb2a2fd9SKrzysztof Halasa 		if (dev_to_hdlc(dev)->proto != &proto)
337eb2a2fd9SKrzysztof Halasa 			return -EINVAL;
3381da177e4SLinus Torvalds 		ifr->ifr_settings.type = IF_PROTO_CISCO;
3391da177e4SLinus Torvalds 		if (ifr->ifr_settings.size < size) {
3401da177e4SLinus Torvalds 			ifr->ifr_settings.size = size; /* data size wanted */
3411da177e4SLinus Torvalds 			return -ENOBUFS;
3421da177e4SLinus Torvalds 		}
343eb2a2fd9SKrzysztof Halasa 		if (copy_to_user(cisco_s, &state(hdlc)->settings, size))
3441da177e4SLinus Torvalds 			return -EFAULT;
3451da177e4SLinus Torvalds 		return 0;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	case IF_PROTO_CISCO:
3481da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
3491da177e4SLinus Torvalds 			return -EPERM;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 		if (dev->flags & IFF_UP)
3521da177e4SLinus Torvalds 			return -EBUSY;
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 		if (copy_from_user(&new_settings, cisco_s, size))
3551da177e4SLinus Torvalds 			return -EFAULT;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 		if (new_settings.interval < 1 ||
3581da177e4SLinus Torvalds 		    new_settings.timeout < 2)
3591da177e4SLinus Torvalds 			return -EINVAL;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 		result = hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
3621da177e4SLinus Torvalds 		if (result)
3631da177e4SLinus Torvalds 			return result;
3641da177e4SLinus Torvalds 
36540d25142SKrzysztof Halasa 		result = attach_hdlc_protocol(dev, &proto,
366eb2a2fd9SKrzysztof Halasa 					      sizeof(struct cisco_state));
367eb2a2fd9SKrzysztof Halasa 		if (result)
368eb2a2fd9SKrzysztof Halasa 			return result;
3691da177e4SLinus Torvalds 
370eb2a2fd9SKrzysztof Halasa 		memcpy(&state(hdlc)->settings, &new_settings, size);
371aff26e2fSKrzysztof Halasa 		spin_lock_init(&state(hdlc)->lock);
3723b04dddeSStephen Hemminger 		dev->header_ops = &cisco_header_ops;
373*1a545ebeSXie He 		dev->hard_header_len = sizeof(struct hdlc_header);
3741da177e4SLinus Torvalds 		dev->type = ARPHRD_CISCO;
3752f8364a2SAndrew Lunn 		call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
376c2ce9204SKrzysztof Halasa 		netif_dormant_on(dev);
3771da177e4SLinus Torvalds 		return 0;
3781da177e4SLinus Torvalds 	}
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	return -EINVAL;
3811da177e4SLinus Torvalds }
382eb2a2fd9SKrzysztof Halasa 
383eb2a2fd9SKrzysztof Halasa 
384eb2a2fd9SKrzysztof Halasa static int __init mod_init(void)
385eb2a2fd9SKrzysztof Halasa {
386eb2a2fd9SKrzysztof Halasa 	register_hdlc_protocol(&proto);
387eb2a2fd9SKrzysztof Halasa 	return 0;
388eb2a2fd9SKrzysztof Halasa }
389eb2a2fd9SKrzysztof Halasa 
390eb2a2fd9SKrzysztof Halasa 
391eb2a2fd9SKrzysztof Halasa 
392eb2a2fd9SKrzysztof Halasa static void __exit mod_exit(void)
393eb2a2fd9SKrzysztof Halasa {
394eb2a2fd9SKrzysztof Halasa 	unregister_hdlc_protocol(&proto);
395eb2a2fd9SKrzysztof Halasa }
396eb2a2fd9SKrzysztof Halasa 
397eb2a2fd9SKrzysztof Halasa 
398eb2a2fd9SKrzysztof Halasa module_init(mod_init);
399eb2a2fd9SKrzysztof Halasa module_exit(mod_exit);
400eb2a2fd9SKrzysztof Halasa 
401eb2a2fd9SKrzysztof Halasa MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
402eb2a2fd9SKrzysztof Halasa MODULE_DESCRIPTION("Cisco HDLC protocol support for generic HDLC");
403eb2a2fd9SKrzysztof Halasa MODULE_LICENSE("GPL v2");
404