blob: b7da55140fbd854b54568cb84add8c1420fd7e8a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic HDLC support routines for Linux
3 *
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +02004 * Copyright (C) 1999 - 2005 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:
20 * - proto.open(), close(), start(), stop() calls are serialized.
21 * The order is: open, [ start, stop ... ] close ...
22 * - proto.start() and stop() are called with spin_lock_irq held.
23 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/slab.h>
28#include <linux/poll.h>
29#include <linux/errno.h>
30#include <linux/if_arp.h>
31#include <linux/init.h>
32#include <linux/skbuff.h>
33#include <linux/pkt_sched.h>
34#include <linux/inetdevice.h>
35#include <linux/lapb.h>
36#include <linux/rtnetlink.h>
37#include <linux/hdlc.h>
38
39
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020040static const char* version = "HDLC support module revision 1.18";
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#undef DEBUG_LINK
43
44
45static int hdlc_change_mtu(struct net_device *dev, int new_mtu)
46{
47 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
48 return -EINVAL;
49 dev->mtu = new_mtu;
50 return 0;
51}
52
53
54
55static struct net_device_stats *hdlc_get_stats(struct net_device *dev)
56{
57 return hdlc_stats(dev);
58}
59
60
61
62static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -070063 struct packet_type *p, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 hdlc_device *hdlc = dev_to_hdlc(dev);
66 if (hdlc->proto.netif_rx)
67 return hdlc->proto.netif_rx(skb);
68
69 hdlc->stats.rx_dropped++; /* Shouldn't happen */
70 dev_kfree_skb(skb);
71 return NET_RX_DROP;
72}
73
74
75
76static void __hdlc_set_carrier_on(struct net_device *dev)
77{
78 hdlc_device *hdlc = dev_to_hdlc(dev);
79 if (hdlc->proto.start)
80 return hdlc->proto.start(dev);
Krzysztof Halasa1f7bad72005-11-11 01:10:30 +010081#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#ifdef DEBUG_LINK
83 if (netif_carrier_ok(dev))
84 printk(KERN_ERR "hdlc_set_carrier_on(): already on\n");
85#endif
86 netif_carrier_on(dev);
Krzysztof Halasa1f7bad72005-11-11 01:10:30 +010087#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90
91
92static void __hdlc_set_carrier_off(struct net_device *dev)
93{
94 hdlc_device *hdlc = dev_to_hdlc(dev);
95 if (hdlc->proto.stop)
96 return hdlc->proto.stop(dev);
97
Krzysztof Halasa1f7bad72005-11-11 01:10:30 +010098#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#ifdef DEBUG_LINK
100 if (!netif_carrier_ok(dev))
101 printk(KERN_ERR "hdlc_set_carrier_off(): already off\n");
102#endif
103 netif_carrier_off(dev);
Krzysztof Halasa1f7bad72005-11-11 01:10:30 +0100104#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107
108
109void hdlc_set_carrier(int on, struct net_device *dev)
110{
111 hdlc_device *hdlc = dev_to_hdlc(dev);
112 unsigned long flags;
113 on = on ? 1 : 0;
114
115#ifdef DEBUG_LINK
116 printk(KERN_DEBUG "hdlc_set_carrier %i\n", on);
117#endif
118
119 spin_lock_irqsave(&hdlc->state_lock, flags);
120
121 if (hdlc->carrier == on)
122 goto carrier_exit; /* no change in DCD line level */
123
124#ifdef DEBUG_LINK
125 printk(KERN_INFO "%s: carrier %s\n", dev->name, on ? "ON" : "off");
126#endif
127 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 __hdlc_set_carrier_on(dev);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200135 } else {
136 printk(KERN_INFO "%s: Carrier lost\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 __hdlc_set_carrier_off(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);
142}
143
144
145
146/* Must be called by hardware driver when HDLC device is being opened */
147int hdlc_open(struct net_device *dev)
148{
149 hdlc_device *hdlc = dev_to_hdlc(dev);
150#ifdef DEBUG_LINK
151 printk(KERN_DEBUG "hdlc_open() carrier %i open %i\n",
152 hdlc->carrier, hdlc->open);
153#endif
154
155 if (hdlc->proto.id == -1)
156 return -ENOSYS; /* no protocol attached */
157
158 if (hdlc->proto.open) {
159 int result = hdlc->proto.open(dev);
160 if (result)
161 return result;
162 }
163
164 spin_lock_irq(&hdlc->state_lock);
165
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200166 if (hdlc->carrier) {
167 printk(KERN_INFO "%s: Carrier detected\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 __hdlc_set_carrier_on(dev);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200169 } else
170 printk(KERN_INFO "%s: No carrier\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 hdlc->open = 1;
173
174 spin_unlock_irq(&hdlc->state_lock);
175 return 0;
176}
177
178
179
180/* Must be called by hardware driver when HDLC device is being closed */
181void hdlc_close(struct net_device *dev)
182{
183 hdlc_device *hdlc = dev_to_hdlc(dev);
184#ifdef DEBUG_LINK
185 printk(KERN_DEBUG "hdlc_close() carrier %i open %i\n",
186 hdlc->carrier, hdlc->open);
187#endif
188
189 spin_lock_irq(&hdlc->state_lock);
190
191 hdlc->open = 0;
192 if (hdlc->carrier)
193 __hdlc_set_carrier_off(dev);
194
195 spin_unlock_irq(&hdlc->state_lock);
196
197 if (hdlc->proto.close)
198 hdlc->proto.close(dev);
199}
200
201
202
203#ifndef CONFIG_HDLC_RAW
204#define hdlc_raw_ioctl(dev, ifr) -ENOSYS
205#endif
206
207#ifndef CONFIG_HDLC_RAW_ETH
208#define hdlc_raw_eth_ioctl(dev, ifr) -ENOSYS
209#endif
210
211#ifndef CONFIG_HDLC_PPP
212#define hdlc_ppp_ioctl(dev, ifr) -ENOSYS
213#endif
214
215#ifndef CONFIG_HDLC_CISCO
216#define hdlc_cisco_ioctl(dev, ifr) -ENOSYS
217#endif
218
219#ifndef CONFIG_HDLC_FR
220#define hdlc_fr_ioctl(dev, ifr) -ENOSYS
221#endif
222
223#ifndef CONFIG_HDLC_X25
224#define hdlc_x25_ioctl(dev, ifr) -ENOSYS
225#endif
226
227
228int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
229{
230 hdlc_device *hdlc = dev_to_hdlc(dev);
231 unsigned int proto;
232
233 if (cmd != SIOCWANDEV)
234 return -EINVAL;
235
236 switch(ifr->ifr_settings.type) {
237 case IF_PROTO_HDLC:
238 case IF_PROTO_HDLC_ETH:
239 case IF_PROTO_PPP:
240 case IF_PROTO_CISCO:
241 case IF_PROTO_FR:
242 case IF_PROTO_X25:
243 proto = ifr->ifr_settings.type;
244 break;
245
246 default:
247 proto = hdlc->proto.id;
248 }
249
250 switch(proto) {
251 case IF_PROTO_HDLC: return hdlc_raw_ioctl(dev, ifr);
252 case IF_PROTO_HDLC_ETH: return hdlc_raw_eth_ioctl(dev, ifr);
253 case IF_PROTO_PPP: return hdlc_ppp_ioctl(dev, ifr);
254 case IF_PROTO_CISCO: return hdlc_cisco_ioctl(dev, ifr);
255 case IF_PROTO_FR: return hdlc_fr_ioctl(dev, ifr);
256 case IF_PROTO_X25: return hdlc_x25_ioctl(dev, ifr);
257 default: return -EINVAL;
258 }
259}
260
Krzysztof Halasa4a31e342006-06-22 22:20:19 +0200261void hdlc_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 hdlc_device *hdlc = dev_to_hdlc(dev);
264
265 dev->get_stats = hdlc_get_stats;
266 dev->change_mtu = hdlc_change_mtu;
267 dev->mtu = HDLC_MAX_MTU;
268
269 dev->type = ARPHRD_RAWHDLC;
270 dev->hard_header_len = 16;
271
272 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
273
274 hdlc->proto.id = -1;
275 hdlc->proto.detach = NULL;
276 hdlc->carrier = 1;
277 hdlc->open = 0;
278 spin_lock_init(&hdlc->state_lock);
279}
280
281struct net_device *alloc_hdlcdev(void *priv)
282{
283 struct net_device *dev;
284 dev = alloc_netdev(sizeof(hdlc_device), "hdlc%d", hdlc_setup);
285 if (dev)
286 dev_to_hdlc(dev)->priv = priv;
287 return dev;
288}
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290void unregister_hdlc_device(struct net_device *dev)
291{
292 rtnl_lock();
293 hdlc_proto_detach(dev_to_hdlc(dev));
294 unregister_netdevice(dev);
295 rtnl_unlock();
296}
297
298
299
300MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
301MODULE_DESCRIPTION("HDLC support module");
302MODULE_LICENSE("GPL v2");
303
304EXPORT_SYMBOL(hdlc_open);
305EXPORT_SYMBOL(hdlc_close);
306EXPORT_SYMBOL(hdlc_set_carrier);
307EXPORT_SYMBOL(hdlc_ioctl);
Krzysztof Halasa4a31e342006-06-22 22:20:19 +0200308EXPORT_SYMBOL(hdlc_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309EXPORT_SYMBOL(alloc_hdlcdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310EXPORT_SYMBOL(unregister_hdlc_device);
311
312static struct packet_type hdlc_packet_type = {
313 .type = __constant_htons(ETH_P_HDLC),
314 .func = hdlc_rcv,
315};
316
317
318static int __init hdlc_module_init(void)
319{
320 printk(KERN_INFO "%s\n", version);
321 dev_add_pack(&hdlc_packet_type);
322 return 0;
323}
324
325
326
327static void __exit hdlc_module_exit(void)
328{
329 dev_remove_pack(&hdlc_packet_type);
330}
331
332
333module_init(hdlc_module_init);
334module_exit(hdlc_module_exit);