blob: 57f9538b8fb5efead8c45e792610df97bcf57500 [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
25#include <linux/config.h>
26#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/slab.h>
29#include <linux/poll.h>
30#include <linux/errno.h>
31#include <linux/if_arp.h>
32#include <linux/init.h>
33#include <linux/skbuff.h>
34#include <linux/pkt_sched.h>
35#include <linux/inetdevice.h>
36#include <linux/lapb.h>
37#include <linux/rtnetlink.h>
38#include <linux/hdlc.h>
39
40
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020041static const char* version = "HDLC support module revision 1.18";
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#undef DEBUG_LINK
44
45
46static int hdlc_change_mtu(struct net_device *dev, int new_mtu)
47{
48 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
49 return -EINVAL;
50 dev->mtu = new_mtu;
51 return 0;
52}
53
54
55
56static struct net_device_stats *hdlc_get_stats(struct net_device *dev)
57{
58 return hdlc_stats(dev);
59}
60
61
62
63static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -070064 struct packet_type *p, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 hdlc_device *hdlc = dev_to_hdlc(dev);
67 if (hdlc->proto.netif_rx)
68 return hdlc->proto.netif_rx(skb);
69
70 hdlc->stats.rx_dropped++; /* Shouldn't happen */
71 dev_kfree_skb(skb);
72 return NET_RX_DROP;
73}
74
75
76
77static void __hdlc_set_carrier_on(struct net_device *dev)
78{
79 hdlc_device *hdlc = dev_to_hdlc(dev);
80 if (hdlc->proto.start)
81 return hdlc->proto.start(dev);
Krzysztof Halasa1f7bad72005-11-11 01:10:30 +010082#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#ifdef DEBUG_LINK
84 if (netif_carrier_ok(dev))
85 printk(KERN_ERR "hdlc_set_carrier_on(): already on\n");
86#endif
87 netif_carrier_on(dev);
Krzysztof Halasa1f7bad72005-11-11 01:10:30 +010088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91
92
93static void __hdlc_set_carrier_off(struct net_device *dev)
94{
95 hdlc_device *hdlc = dev_to_hdlc(dev);
96 if (hdlc->proto.stop)
97 return hdlc->proto.stop(dev);
98
Krzysztof Halasa1f7bad72005-11-11 01:10:30 +010099#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#ifdef DEBUG_LINK
101 if (!netif_carrier_ok(dev))
102 printk(KERN_ERR "hdlc_set_carrier_off(): already off\n");
103#endif
104 netif_carrier_off(dev);
Krzysztof Halasa1f7bad72005-11-11 01:10:30 +0100105#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108
109
110void hdlc_set_carrier(int on, struct net_device *dev)
111{
112 hdlc_device *hdlc = dev_to_hdlc(dev);
113 unsigned long flags;
114 on = on ? 1 : 0;
115
116#ifdef DEBUG_LINK
117 printk(KERN_DEBUG "hdlc_set_carrier %i\n", on);
118#endif
119
120 spin_lock_irqsave(&hdlc->state_lock, flags);
121
122 if (hdlc->carrier == on)
123 goto carrier_exit; /* no change in DCD line level */
124
125#ifdef DEBUG_LINK
126 printk(KERN_INFO "%s: carrier %s\n", dev->name, on ? "ON" : "off");
127#endif
128 hdlc->carrier = on;
129
130 if (!hdlc->open)
131 goto carrier_exit;
132
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200133 if (hdlc->carrier) {
134 printk(KERN_INFO "%s: Carrier detected\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 __hdlc_set_carrier_on(dev);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200136 } else {
137 printk(KERN_INFO "%s: Carrier lost\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 __hdlc_set_carrier_off(dev);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141carrier_exit:
142 spin_unlock_irqrestore(&hdlc->state_lock, flags);
143}
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
152 printk(KERN_DEBUG "hdlc_open() carrier %i open %i\n",
153 hdlc->carrier, hdlc->open);
154#endif
155
156 if (hdlc->proto.id == -1)
157 return -ENOSYS; /* no protocol attached */
158
159 if (hdlc->proto.open) {
160 int result = hdlc->proto.open(dev);
161 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 __hdlc_set_carrier_on(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
186 printk(KERN_DEBUG "hdlc_close() carrier %i open %i\n",
187 hdlc->carrier, hdlc->open);
188#endif
189
190 spin_lock_irq(&hdlc->state_lock);
191
192 hdlc->open = 0;
193 if (hdlc->carrier)
194 __hdlc_set_carrier_off(dev);
195
196 spin_unlock_irq(&hdlc->state_lock);
197
198 if (hdlc->proto.close)
199 hdlc->proto.close(dev);
200}
201
202
203
204#ifndef CONFIG_HDLC_RAW
205#define hdlc_raw_ioctl(dev, ifr) -ENOSYS
206#endif
207
208#ifndef CONFIG_HDLC_RAW_ETH
209#define hdlc_raw_eth_ioctl(dev, ifr) -ENOSYS
210#endif
211
212#ifndef CONFIG_HDLC_PPP
213#define hdlc_ppp_ioctl(dev, ifr) -ENOSYS
214#endif
215
216#ifndef CONFIG_HDLC_CISCO
217#define hdlc_cisco_ioctl(dev, ifr) -ENOSYS
218#endif
219
220#ifndef CONFIG_HDLC_FR
221#define hdlc_fr_ioctl(dev, ifr) -ENOSYS
222#endif
223
224#ifndef CONFIG_HDLC_X25
225#define hdlc_x25_ioctl(dev, ifr) -ENOSYS
226#endif
227
228
229int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
230{
231 hdlc_device *hdlc = dev_to_hdlc(dev);
232 unsigned int proto;
233
234 if (cmd != SIOCWANDEV)
235 return -EINVAL;
236
237 switch(ifr->ifr_settings.type) {
238 case IF_PROTO_HDLC:
239 case IF_PROTO_HDLC_ETH:
240 case IF_PROTO_PPP:
241 case IF_PROTO_CISCO:
242 case IF_PROTO_FR:
243 case IF_PROTO_X25:
244 proto = ifr->ifr_settings.type;
245 break;
246
247 default:
248 proto = hdlc->proto.id;
249 }
250
251 switch(proto) {
252 case IF_PROTO_HDLC: return hdlc_raw_ioctl(dev, ifr);
253 case IF_PROTO_HDLC_ETH: return hdlc_raw_eth_ioctl(dev, ifr);
254 case IF_PROTO_PPP: return hdlc_ppp_ioctl(dev, ifr);
255 case IF_PROTO_CISCO: return hdlc_cisco_ioctl(dev, ifr);
256 case IF_PROTO_FR: return hdlc_fr_ioctl(dev, ifr);
257 case IF_PROTO_X25: return hdlc_x25_ioctl(dev, ifr);
258 default: return -EINVAL;
259 }
260}
261
Krzysztof Halasa4a31e342006-06-22 22:20:19 +0200262void hdlc_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 hdlc_device *hdlc = dev_to_hdlc(dev);
265
266 dev->get_stats = hdlc_get_stats;
267 dev->change_mtu = hdlc_change_mtu;
268 dev->mtu = HDLC_MAX_MTU;
269
270 dev->type = ARPHRD_RAWHDLC;
271 dev->hard_header_len = 16;
272
273 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
274
275 hdlc->proto.id = -1;
276 hdlc->proto.detach = NULL;
277 hdlc->carrier = 1;
278 hdlc->open = 0;
279 spin_lock_init(&hdlc->state_lock);
280}
281
282struct net_device *alloc_hdlcdev(void *priv)
283{
284 struct net_device *dev;
285 dev = alloc_netdev(sizeof(hdlc_device), "hdlc%d", hdlc_setup);
286 if (dev)
287 dev_to_hdlc(dev)->priv = priv;
288 return dev;
289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291void unregister_hdlc_device(struct net_device *dev)
292{
293 rtnl_lock();
294 hdlc_proto_detach(dev_to_hdlc(dev));
295 unregister_netdevice(dev);
296 rtnl_unlock();
297}
298
299
300
301MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
302MODULE_DESCRIPTION("HDLC support module");
303MODULE_LICENSE("GPL v2");
304
305EXPORT_SYMBOL(hdlc_open);
306EXPORT_SYMBOL(hdlc_close);
307EXPORT_SYMBOL(hdlc_set_carrier);
308EXPORT_SYMBOL(hdlc_ioctl);
Krzysztof Halasa4a31e342006-06-22 22:20:19 +0200309EXPORT_SYMBOL(hdlc_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310EXPORT_SYMBOL(alloc_hdlcdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311EXPORT_SYMBOL(unregister_hdlc_device);
312
313static struct packet_type hdlc_packet_type = {
314 .type = __constant_htons(ETH_P_HDLC),
315 .func = hdlc_rcv,
316};
317
318
319static int __init hdlc_module_init(void)
320{
321 printk(KERN_INFO "%s\n", version);
322 dev_add_pack(&hdlc_packet_type);
323 return 0;
324}
325
326
327
328static void __exit hdlc_module_exit(void)
329{
330 dev_remove_pack(&hdlc_packet_type);
331}
332
333
334module_init(hdlc_module_init);
335module_exit(hdlc_module_exit);