blob: cc07236ea734ba018bd5fea2960bdd800446277c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic HDLC support routines for Linux
3 *
Krzysztof Halasa40d25142008-02-01 22:37:12 +01004 * Copyright (C) 1999 - 2008 Krzysztof Halasa <khc@pm.waw.pl>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of version 2 of the GNU General Public License
8 * as published by the Free Software Foundation.
9 *
10 * Currently supported:
11 * * raw IP-in-HDLC
12 * * Cisco HDLC
13 * * Frame Relay with ANSI or CCITT LMI (both user and network side)
14 * * PPP
15 * * X.25
16 *
17 * Use sethdlc utility to set line parameters, protocol and PVCs
18 *
19 * How does it work:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020020 * - proto->open(), close(), start(), stop() calls are serialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * The order is: open, [ start, stop ... ] close ...
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020022 * - proto->start() and stop() are called with spin_lock_irq held.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/hdlc.h>
Krzysztof Hałasa4dfce402008-06-30 19:06:40 +020027#include <linux/if_arp.h>
28#include <linux/inetdevice.h>
29#include <linux/init.h>
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/notifier.h>
33#include <linux/pkt_sched.h>
34#include <linux/poll.h>
35#include <linux/rtnetlink.h>
36#include <linux/skbuff.h>
37#include <linux/slab.h>
Eric W. Biedermane730c152007-09-17 11:53:39 -070038#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40
Krzysztof Halasa40d25142008-02-01 22:37:12 +010041static const char* version = "HDLC support module revision 1.22";
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#undef DEBUG_LINK
44
Krzysztof Halasafa701bd2008-05-19 19:00:51 +020045static struct hdlc_proto *first_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Krzysztof Hałasa991990a2009-01-08 22:52:11 +010047int hdlc_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
50 return -EINVAL;
51 dev->mtu = new_mtu;
52 return 0;
53}
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -070056 struct packet_type *p, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Krzysztof Halasa40d25142008-02-01 22:37:12 +010058 struct hdlc_device *hdlc = dev_to_hdlc(dev);
Eric W. Biedermane730c152007-09-17 11:53:39 -070059
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +090060 if (dev_net(dev) != &init_net) {
Eric W. Biedermane730c152007-09-17 11:53:39 -070061 kfree_skb(skb);
62 return 0;
63 }
64
Krzysztof Halasa40d25142008-02-01 22:37:12 +010065 BUG_ON(!hdlc->proto->netif_rx);
66 return hdlc->proto->netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Stephen Hemmingerd71a6742009-08-31 19:50:47 +000069netdev_tx_t hdlc_start_xmit(struct sk_buff *skb, struct net_device *dev)
Krzysztof Hałasa991990a2009-01-08 22:52:11 +010070{
71 hdlc_device *hdlc = dev_to_hdlc(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Krzysztof Hałasa991990a2009-01-08 22:52:11 +010073 if (hdlc->proto->xmit)
74 return hdlc->proto->xmit(skb, dev);
75
76 return hdlc->xmit(skb, dev); /* call hardware driver directly */
77}
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Krzysztof Halasac2ce9202006-07-12 13:46:12 -070079static inline void hdlc_proto_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020082 if (hdlc->proto->start)
Krzysztof Halasa40d25142008-02-01 22:37:12 +010083 hdlc->proto->start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86
87
Krzysztof Halasac2ce9202006-07-12 13:46:12 -070088static inline void hdlc_proto_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020091 if (hdlc->proto->stop)
Krzysztof Halasa40d25142008-02-01 22:37:12 +010092 hdlc->proto->stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95
96
Krzysztof Halasac2ce9202006-07-12 13:46:12 -070097static int hdlc_device_event(struct notifier_block *this, unsigned long event,
98 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700100 struct net_device *dev = ptr;
101 hdlc_device *hdlc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 unsigned long flags;
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700103 int on;
Krzysztof Hałasadff3fde2009-01-08 19:55:57 +0100104
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900105 if (dev_net(dev) != &init_net)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200106 return NOTIFY_DONE;
107
Krzysztof Hałasa7cdc15f2009-01-08 19:46:54 +0100108 if (!(dev->priv_flags & IFF_WAN_HDLC))
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700109 return NOTIFY_DONE; /* not an HDLC device */
Krzysztof Hałasa4dfce402008-06-30 19:06:40 +0200110
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700111 if (event != NETDEV_CHANGE)
112 return NOTIFY_DONE; /* Only interrested in carrier changes */
113
114 on = netif_carrier_ok(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116#ifdef DEBUG_LINK
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700117 printk(KERN_DEBUG "%s: hdlc_device_event NETDEV_CHANGE, carrier %i\n",
118 dev->name, on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#endif
120
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700121 hdlc = dev_to_hdlc(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 spin_lock_irqsave(&hdlc->state_lock, flags);
123
124 if (hdlc->carrier == on)
125 goto carrier_exit; /* no change in DCD line level */
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 hdlc->carrier = on;
128
129 if (!hdlc->open)
130 goto carrier_exit;
131
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200132 if (hdlc->carrier) {
133 printk(KERN_INFO "%s: Carrier detected\n", dev->name);
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700134 hdlc_proto_start(dev);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200135 } else {
136 printk(KERN_INFO "%s: Carrier lost\n", dev->name);
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700137 hdlc_proto_stop(dev);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140carrier_exit:
141 spin_unlock_irqrestore(&hdlc->state_lock, flags);
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700142 return NOTIFY_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145
146
147/* Must be called by hardware driver when HDLC device is being opened */
148int hdlc_open(struct net_device *dev)
149{
150 hdlc_device *hdlc = dev_to_hdlc(dev);
151#ifdef DEBUG_LINK
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200152 printk(KERN_DEBUG "%s: hdlc_open() carrier %i open %i\n", dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 hdlc->carrier, hdlc->open);
154#endif
155
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200156 if (hdlc->proto == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return -ENOSYS; /* no protocol attached */
158
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200159 if (hdlc->proto->open) {
160 int result = hdlc->proto->open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (result)
162 return result;
163 }
164
165 spin_lock_irq(&hdlc->state_lock);
166
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200167 if (hdlc->carrier) {
168 printk(KERN_INFO "%s: Carrier detected\n", dev->name);
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700169 hdlc_proto_start(dev);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200170 } else
171 printk(KERN_INFO "%s: No carrier\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 hdlc->open = 1;
174
175 spin_unlock_irq(&hdlc->state_lock);
176 return 0;
177}
178
179
180
181/* Must be called by hardware driver when HDLC device is being closed */
182void hdlc_close(struct net_device *dev)
183{
184 hdlc_device *hdlc = dev_to_hdlc(dev);
185#ifdef DEBUG_LINK
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200186 printk(KERN_DEBUG "%s: hdlc_close() carrier %i open %i\n", dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 hdlc->carrier, hdlc->open);
188#endif
189
190 spin_lock_irq(&hdlc->state_lock);
191
192 hdlc->open = 0;
193 if (hdlc->carrier)
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700194 hdlc_proto_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 spin_unlock_irq(&hdlc->state_lock);
197
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200198 if (hdlc->proto->close)
199 hdlc->proto->close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
205{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200206 struct hdlc_proto *proto = first_proto;
207 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 if (cmd != SIOCWANDEV)
210 return -EINVAL;
211
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200212 if (dev_to_hdlc(dev)->proto) {
213 result = dev_to_hdlc(dev)->proto->ioctl(dev, ifr);
214 if (result != -EINVAL)
215 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200218 /* Not handled by currently attached protocol (if any) */
219
220 while (proto) {
221 if ((result = proto->ioctl(dev, ifr)) != -EINVAL)
222 return result;
223 proto = proto->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200225 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700228static const struct header_ops hdlc_null_ops;
229
Krzysztof Halasab5284e52007-03-02 15:52:22 -0800230static void hdlc_setup_dev(struct net_device *dev)
231{
232 /* Re-init all variables changed by HDLC protocol drivers,
233 * including ether_setup() called from hdlc_raw_eth.c.
234 */
Krzysztof Halasab5284e52007-03-02 15:52:22 -0800235 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
Krzysztof Hałasa7cdc15f2009-01-08 19:46:54 +0100236 dev->priv_flags = IFF_WAN_HDLC;
Krzysztof Halasab5284e52007-03-02 15:52:22 -0800237 dev->mtu = HDLC_MAX_MTU;
238 dev->type = ARPHRD_RAWHDLC;
239 dev->hard_header_len = 16;
240 dev->addr_len = 0;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700241 dev->header_ops = &hdlc_null_ops;
Krzysztof Halasab5284e52007-03-02 15:52:22 -0800242}
243
Adrian Bunk0bf94fa2007-01-11 14:48:59 +0100244static void hdlc_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 hdlc_device *hdlc = dev_to_hdlc(dev);
247
Krzysztof Halasab5284e52007-03-02 15:52:22 -0800248 hdlc_setup_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 hdlc->carrier = 1;
250 hdlc->open = 0;
251 spin_lock_init(&hdlc->state_lock);
252}
253
254struct net_device *alloc_hdlcdev(void *priv)
255{
256 struct net_device *dev;
Krzysztof Halasa40d25142008-02-01 22:37:12 +0100257 dev = alloc_netdev(sizeof(struct hdlc_device), "hdlc%d", hdlc_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (dev)
259 dev_to_hdlc(dev)->priv = priv;
260 return dev;
261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263void unregister_hdlc_device(struct net_device *dev)
264{
265 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 unregister_netdevice(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200267 detach_hdlc_protocol(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 rtnl_unlock();
269}
270
271
272
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200273int attach_hdlc_protocol(struct net_device *dev, struct hdlc_proto *proto,
Krzysztof Halasa40d25142008-02-01 22:37:12 +0100274 size_t size)
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200275{
276 detach_hdlc_protocol(dev);
277
278 if (!try_module_get(proto->module))
279 return -ENOSYS;
280
281 if (size)
282 if ((dev_to_hdlc(dev)->state = kmalloc(size,
283 GFP_KERNEL)) == NULL) {
284 printk(KERN_WARNING "Memory squeeze on"
285 " hdlc_proto_attach()\n");
286 module_put(proto->module);
287 return -ENOBUFS;
288 }
289 dev_to_hdlc(dev)->proto = proto;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200290 return 0;
291}
292
293
294void detach_hdlc_protocol(struct net_device *dev)
295{
296 hdlc_device *hdlc = dev_to_hdlc(dev);
297
298 if (hdlc->proto) {
299 if (hdlc->proto->detach)
300 hdlc->proto->detach(dev);
301 module_put(hdlc->proto->module);
302 hdlc->proto = NULL;
303 }
304 kfree(hdlc->state);
305 hdlc->state = NULL;
Krzysztof Halasab5284e52007-03-02 15:52:22 -0800306 hdlc_setup_dev(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200307}
308
309
310void register_hdlc_protocol(struct hdlc_proto *proto)
311{
Krzysztof Halasafa701bd2008-05-19 19:00:51 +0200312 rtnl_lock();
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200313 proto->next = first_proto;
314 first_proto = proto;
Krzysztof Halasafa701bd2008-05-19 19:00:51 +0200315 rtnl_unlock();
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200316}
317
318
319void unregister_hdlc_protocol(struct hdlc_proto *proto)
320{
Krzysztof Halasafa701bd2008-05-19 19:00:51 +0200321 struct hdlc_proto **p;
322
323 rtnl_lock();
324 p = &first_proto;
325 while (*p != proto) {
326 BUG_ON(!*p);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200327 p = &((*p)->next);
328 }
Krzysztof Halasafa701bd2008-05-19 19:00:51 +0200329 *p = proto->next;
330 rtnl_unlock();
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200331}
332
333
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
336MODULE_DESCRIPTION("HDLC support module");
337MODULE_LICENSE("GPL v2");
338
Krzysztof Hałasa991990a2009-01-08 22:52:11 +0100339EXPORT_SYMBOL(hdlc_change_mtu);
340EXPORT_SYMBOL(hdlc_start_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341EXPORT_SYMBOL(hdlc_open);
342EXPORT_SYMBOL(hdlc_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343EXPORT_SYMBOL(hdlc_ioctl);
344EXPORT_SYMBOL(alloc_hdlcdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345EXPORT_SYMBOL(unregister_hdlc_device);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200346EXPORT_SYMBOL(register_hdlc_protocol);
347EXPORT_SYMBOL(unregister_hdlc_protocol);
348EXPORT_SYMBOL(attach_hdlc_protocol);
349EXPORT_SYMBOL(detach_hdlc_protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Stephen Hemminger7546dd92009-03-09 08:18:29 +0000351static struct packet_type hdlc_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -0800352 .type = cpu_to_be16(ETH_P_HDLC),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 .func = hdlc_rcv,
354};
355
356
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700357static struct notifier_block hdlc_notifier = {
Krzysztof Hałasa4dfce402008-06-30 19:06:40 +0200358 .notifier_call = hdlc_device_event,
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700359};
360
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static int __init hdlc_module_init(void)
363{
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700364 int result;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 printk(KERN_INFO "%s\n", version);
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700367 if ((result = register_netdevice_notifier(&hdlc_notifier)) != 0)
Krzysztof Hałasa4dfce402008-06-30 19:06:40 +0200368 return result;
369 dev_add_pack(&hdlc_packet_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return 0;
371}
372
373
374
375static void __exit hdlc_module_exit(void)
376{
377 dev_remove_pack(&hdlc_packet_type);
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700378 unregister_netdevice_notifier(&hdlc_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
381
382module_init(hdlc_module_init);
383module_exit(hdlc_module_exit);