2 * Generic HDLC support routines for Linux
5 * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License
9 * as published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/poll.h>
16 #include <linux/errno.h>
17 #include <linux/if_arp.h>
18 #include <linux/init.h>
19 #include <linux/skbuff.h>
20 #include <linux/pkt_sched.h>
21 #include <linux/inetdevice.h>
22 #include <linux/lapb.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/hdlc.h>
26 #include <net/x25device.h>
28 static int x25_ioctl(struct net_device *dev, struct ifreq *ifr);
30 /* These functions are callbacks called by LAPB layer */
32 static void x25_connect_disconnect(struct net_device *dev, int reason, int code)
37 if ((skb = dev_alloc_skb(1)) == NULL) {
38 printk(KERN_ERR "%s: out of memory\n", dev->name);
42 ptr = skb_put(skb, 1);
45 skb->protocol = x25_type_trans(skb, dev);
51 static void x25_connected(struct net_device *dev, int reason)
53 x25_connect_disconnect(dev, reason, 1);
58 static void x25_disconnected(struct net_device *dev, int reason)
60 x25_connect_disconnect(dev, reason, 2);
65 static int x25_data_indication(struct net_device *dev, struct sk_buff *skb)
77 skb->protocol = x25_type_trans(skb, dev);
83 static void x25_data_transmit(struct net_device *dev, struct sk_buff *skb)
85 hdlc_device *hdlc = dev_to_hdlc(dev);
86 hdlc->xmit(skb, dev); /* Ignore return value :-( */
91 static int x25_xmit(struct sk_buff *skb, struct net_device *dev)
97 switch (skb->data[0]) {
98 case 0: /* Data to be transmitted */
100 if ((result = lapb_data_request(dev, skb)) != LAPB_OK)
105 if ((result = lapb_connect_request(dev))!= LAPB_OK) {
106 if (result == LAPB_CONNECTED)
107 /* Send connect confirm. msg to level 3 */
108 x25_connected(dev, 0);
110 printk(KERN_ERR "%s: LAPB connect request "
111 "failed, error code = %i\n",
117 if ((result = lapb_disconnect_request(dev)) != LAPB_OK) {
118 if (result == LAPB_NOTCONNECTED)
119 /* Send disconnect confirm. msg to level 3 */
120 x25_disconnected(dev, 0);
122 printk(KERN_ERR "%s: LAPB disconnect request "
123 "failed, error code = %i\n",
128 default: /* to be defined */
138 static int x25_open(struct net_device *dev)
140 struct lapb_register_struct cb;
143 cb.connect_confirmation = x25_connected;
144 cb.connect_indication = x25_connected;
145 cb.disconnect_confirmation = x25_disconnected;
146 cb.disconnect_indication = x25_disconnected;
147 cb.data_indication = x25_data_indication;
148 cb.data_transmit = x25_data_transmit;
150 result = lapb_register(dev, &cb);
151 if (result != LAPB_OK)
158 static void x25_close(struct net_device *dev)
160 lapb_unregister(dev);
165 static int x25_rx(struct sk_buff *skb)
167 struct hdlc_device_desc *desc = dev_to_desc(skb->dev);
169 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
170 desc->stats.rx_dropped++;
174 if (lapb_data_received(skb->dev, skb) == LAPB_OK)
175 return NET_RX_SUCCESS;
177 desc->stats.rx_errors++;
178 dev_kfree_skb_any(skb);
183 static struct hdlc_proto proto = {
187 .module = THIS_MODULE,
191 static int x25_ioctl(struct net_device *dev, struct ifreq *ifr)
193 hdlc_device *hdlc = dev_to_hdlc(dev);
196 switch (ifr->ifr_settings.type) {
198 if (dev_to_hdlc(dev)->proto != &proto)
200 ifr->ifr_settings.type = IF_PROTO_X25;
201 return 0; /* return protocol only, no settable parameters */
204 if(!capable(CAP_NET_ADMIN))
207 if(dev->flags & IFF_UP)
210 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
214 if ((result = attach_hdlc_protocol(dev, &proto,
217 dev->hard_start_xmit = x25_xmit;
218 dev->hard_header = NULL;
219 dev->type = ARPHRD_X25;
221 netif_dormant_off(dev);
229 static int __init mod_init(void)
231 register_hdlc_protocol(&proto);
237 static void __exit mod_exit(void)
239 unregister_hdlc_protocol(&proto);
243 module_init(mod_init);
244 module_exit(mod_exit);
246 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
247 MODULE_DESCRIPTION("X.25 protocol support for generic HDLC");
248 MODULE_LICENSE("GPL v2");