hdlc.c 7.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * Generic HDLC support routines for Linux
 *
4
 * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License
 * as published by the Free Software Foundation.
 *
 * Currently supported:
 *	* raw IP-in-HDLC
 *	* Cisco HDLC
 *	* Frame Relay with ANSI or CCITT LMI (both user and network side)
 *	* PPP
 *	* X.25
 *
 * Use sethdlc utility to set line parameters, protocol and PVCs
 *
 * How does it work:
20
 * - proto->open(), close(), start(), stop() calls are serialized.
L
Linus Torvalds 已提交
21
 *   The order is: open, [ start, stop ... ] close ...
22
 * - proto->start() and stop() are called with spin_lock_irq held.
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/errno.h>
#include <linux/if_arp.h>
#include <linux/init.h>
#include <linux/skbuff.h>
#include <linux/pkt_sched.h>
#include <linux/inetdevice.h>
#include <linux/lapb.h>
#include <linux/rtnetlink.h>
37
#include <linux/notifier.h>
L
Linus Torvalds 已提交
38 39 40
#include <linux/hdlc.h>


41
static const char* version = "HDLC support module revision 1.20";
L
Linus Torvalds 已提交
42 43 44

#undef DEBUG_LINK

45 46
static struct hdlc_proto *first_proto = NULL;

L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

static int hdlc_change_mtu(struct net_device *dev, int new_mtu)
{
	if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
		return -EINVAL;
	dev->mtu = new_mtu;
	return 0;
}



static struct net_device_stats *hdlc_get_stats(struct net_device *dev)
{
	return hdlc_stats(dev);
}



static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev,
D
David S. Miller 已提交
66
		    struct packet_type *p, struct net_device *orig_dev)
L
Linus Torvalds 已提交
67
{
68 69 70
	struct hdlc_device_desc *desc = dev_to_desc(dev);
	if (desc->netif_rx)
		return desc->netif_rx(skb);
L
Linus Torvalds 已提交
71

72
	desc->stats.rx_dropped++; /* Shouldn't happen */
L
Linus Torvalds 已提交
73 74 75 76 77 78
	dev_kfree_skb(skb);
	return NET_RX_DROP;
}



79
static inline void hdlc_proto_start(struct net_device *dev)
L
Linus Torvalds 已提交
80 81
{
	hdlc_device *hdlc = dev_to_hdlc(dev);
82 83
	if (hdlc->proto->start)
		return hdlc->proto->start(dev);
L
Linus Torvalds 已提交
84 85 86 87
}



88
static inline void hdlc_proto_stop(struct net_device *dev)
L
Linus Torvalds 已提交
89 90
{
	hdlc_device *hdlc = dev_to_hdlc(dev);
91 92
	if (hdlc->proto->stop)
		return hdlc->proto->stop(dev);
L
Linus Torvalds 已提交
93 94 95 96
}



97 98
static int hdlc_device_event(struct notifier_block *this, unsigned long event,
			     void *ptr)
L
Linus Torvalds 已提交
99
{
100 101
	struct net_device *dev = ptr;
	hdlc_device *hdlc;
L
Linus Torvalds 已提交
102
	unsigned long flags;
103 104 105 106 107 108 109 110 111
	int on;
 
	if (dev->get_stats != hdlc_get_stats)
		return NOTIFY_DONE; /* not an HDLC device */
 
	if (event != NETDEV_CHANGE)
		return NOTIFY_DONE; /* Only interrested in carrier changes */

	on = netif_carrier_ok(dev);
L
Linus Torvalds 已提交
112 113

#ifdef DEBUG_LINK
114 115
	printk(KERN_DEBUG "%s: hdlc_device_event NETDEV_CHANGE, carrier %i\n",
	       dev->name, on);
L
Linus Torvalds 已提交
116 117
#endif

118
	hdlc = dev_to_hdlc(dev);
L
Linus Torvalds 已提交
119 120 121 122 123 124 125 126 127 128
	spin_lock_irqsave(&hdlc->state_lock, flags);

	if (hdlc->carrier == on)
		goto carrier_exit; /* no change in DCD line level */

	hdlc->carrier = on;

	if (!hdlc->open)
		goto carrier_exit;

K
Krzysztof Halasa 已提交
129 130
	if (hdlc->carrier) {
		printk(KERN_INFO "%s: Carrier detected\n", dev->name);
131
		hdlc_proto_start(dev);
K
Krzysztof Halasa 已提交
132 133
	} else {
		printk(KERN_INFO "%s: Carrier lost\n", dev->name);
134
		hdlc_proto_stop(dev);
K
Krzysztof Halasa 已提交
135
	}
L
Linus Torvalds 已提交
136 137 138

carrier_exit:
	spin_unlock_irqrestore(&hdlc->state_lock, flags);
139
	return NOTIFY_DONE;
L
Linus Torvalds 已提交
140 141 142 143 144 145 146 147 148
}



/* Must be called by hardware driver when HDLC device is being opened */
int hdlc_open(struct net_device *dev)
{
	hdlc_device *hdlc = dev_to_hdlc(dev);
#ifdef DEBUG_LINK
149
	printk(KERN_DEBUG "%s: hdlc_open() carrier %i open %i\n", dev->name,
L
Linus Torvalds 已提交
150 151 152
	       hdlc->carrier, hdlc->open);
#endif

153
	if (hdlc->proto == NULL)
L
Linus Torvalds 已提交
154 155
		return -ENOSYS;	/* no protocol attached */

156 157
	if (hdlc->proto->open) {
		int result = hdlc->proto->open(dev);
L
Linus Torvalds 已提交
158 159 160 161 162 163
		if (result)
			return result;
	}

	spin_lock_irq(&hdlc->state_lock);

K
Krzysztof Halasa 已提交
164 165
	if (hdlc->carrier) {
		printk(KERN_INFO "%s: Carrier detected\n", dev->name);
166
		hdlc_proto_start(dev);
K
Krzysztof Halasa 已提交
167 168
	} else
		printk(KERN_INFO "%s: No carrier\n", dev->name);
L
Linus Torvalds 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182

	hdlc->open = 1;

	spin_unlock_irq(&hdlc->state_lock);
	return 0;
}



/* Must be called by hardware driver when HDLC device is being closed */
void hdlc_close(struct net_device *dev)
{
	hdlc_device *hdlc = dev_to_hdlc(dev);
#ifdef DEBUG_LINK
183
	printk(KERN_DEBUG "%s: hdlc_close() carrier %i open %i\n", dev->name,
L
Linus Torvalds 已提交
184 185 186 187 188 189 190
	       hdlc->carrier, hdlc->open);
#endif

	spin_lock_irq(&hdlc->state_lock);

	hdlc->open = 0;
	if (hdlc->carrier)
191
		hdlc_proto_stop(dev);
L
Linus Torvalds 已提交
192 193 194

	spin_unlock_irq(&hdlc->state_lock);

195 196
	if (hdlc->proto->close)
		hdlc->proto->close(dev);
L
Linus Torvalds 已提交
197 198 199 200 201 202
}



int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
203 204
	struct hdlc_proto *proto = first_proto;
	int result;
L
Linus Torvalds 已提交
205 206 207 208

	if (cmd != SIOCWANDEV)
		return -EINVAL;

209 210 211 212
	if (dev_to_hdlc(dev)->proto) {
		result = dev_to_hdlc(dev)->proto->ioctl(dev, ifr);
		if (result != -EINVAL)
			return result;
L
Linus Torvalds 已提交
213 214
	}

215 216 217 218 219 220
	/* Not handled by currently attached protocol (if any) */

	while (proto) {
		if ((result = proto->ioctl(dev, ifr)) != -EINVAL)
			return result;
		proto = proto->next;
L
Linus Torvalds 已提交
221
	}
222
	return -EINVAL;
L
Linus Torvalds 已提交
223 224
}

A
Adrian Bunk 已提交
225
static void hdlc_setup(struct net_device *dev)
L
Linus Torvalds 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
{
	hdlc_device *hdlc = dev_to_hdlc(dev);

	dev->get_stats = hdlc_get_stats;
	dev->change_mtu = hdlc_change_mtu;
	dev->mtu = HDLC_MAX_MTU;

	dev->type = ARPHRD_RAWHDLC;
	dev->hard_header_len = 16;

	dev->flags = IFF_POINTOPOINT | IFF_NOARP;

	hdlc->carrier = 1;
	hdlc->open = 0;
	spin_lock_init(&hdlc->state_lock);
}

struct net_device *alloc_hdlcdev(void *priv)
{
	struct net_device *dev;
246 247
	dev = alloc_netdev(sizeof(struct hdlc_device_desc) +
			   sizeof(hdlc_device), "hdlc%d", hdlc_setup);
L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255 256
	if (dev)
		dev_to_hdlc(dev)->priv = priv;
	return dev;
}

void unregister_hdlc_device(struct net_device *dev)
{
	rtnl_lock();
	unregister_netdevice(dev);
257
	detach_hdlc_protocol(dev);
L
Linus Torvalds 已提交
258 259 260 261 262
	rtnl_unlock();
}



263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
int attach_hdlc_protocol(struct net_device *dev, struct hdlc_proto *proto,
			 int (*rx)(struct sk_buff *skb), size_t size)
{
	detach_hdlc_protocol(dev);

	if (!try_module_get(proto->module))
		return -ENOSYS;

	if (size)
		if ((dev_to_hdlc(dev)->state = kmalloc(size,
						       GFP_KERNEL)) == NULL) {
			printk(KERN_WARNING "Memory squeeze on"
			       " hdlc_proto_attach()\n");
			module_put(proto->module);
			return -ENOBUFS;
		}
	dev_to_hdlc(dev)->proto = proto;
	dev_to_desc(dev)->netif_rx = rx;
	return 0;
}


void detach_hdlc_protocol(struct net_device *dev)
{
	hdlc_device *hdlc = dev_to_hdlc(dev);

	if (hdlc->proto) {
		if (hdlc->proto->detach)
			hdlc->proto->detach(dev);
		module_put(hdlc->proto->module);
		hdlc->proto = NULL;
	}
	kfree(hdlc->state);
	hdlc->state = NULL;
}


void register_hdlc_protocol(struct hdlc_proto *proto)
{
	proto->next = first_proto;
	first_proto = proto;
}


void unregister_hdlc_protocol(struct hdlc_proto *proto)
{
	struct hdlc_proto **p = &first_proto;
	while (*p) {
		if (*p == proto) {
			*p = proto->next;
			return;
		}
		p = &((*p)->next);
	}
}



L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328 329
MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
MODULE_DESCRIPTION("HDLC support module");
MODULE_LICENSE("GPL v2");

EXPORT_SYMBOL(hdlc_open);
EXPORT_SYMBOL(hdlc_close);
EXPORT_SYMBOL(hdlc_ioctl);
EXPORT_SYMBOL(alloc_hdlcdev);
EXPORT_SYMBOL(unregister_hdlc_device);
330 331 332 333
EXPORT_SYMBOL(register_hdlc_protocol);
EXPORT_SYMBOL(unregister_hdlc_protocol);
EXPORT_SYMBOL(attach_hdlc_protocol);
EXPORT_SYMBOL(detach_hdlc_protocol);
L
Linus Torvalds 已提交
334 335 336 337 338 339 340

static struct packet_type hdlc_packet_type = {
	.type = __constant_htons(ETH_P_HDLC),
	.func = hdlc_rcv,
};


341 342 343 344 345
static struct notifier_block hdlc_notifier = {
        .notifier_call = hdlc_device_event,
};


L
Linus Torvalds 已提交
346 347
static int __init hdlc_module_init(void)
{
348 349
	int result;

L
Linus Torvalds 已提交
350
	printk(KERN_INFO "%s\n", version);
351 352
	if ((result = register_netdevice_notifier(&hdlc_notifier)) != 0)
                return result;
L
Linus Torvalds 已提交
353 354 355 356 357 358 359 360 361
        dev_add_pack(&hdlc_packet_type);
	return 0;
}



static void __exit hdlc_module_exit(void)
{
	dev_remove_pack(&hdlc_packet_type);
362
	unregister_netdevice_notifier(&hdlc_notifier);
L
Linus Torvalds 已提交
363 364 365 366 367
}


module_init(hdlc_module_init);
module_exit(hdlc_module_exit);