blob: 8b7e5d2e2ac94ced976d54587ba074142a13776a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic HDLC support routines for Linux
3 * X.25 support
4 *
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02005 * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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.
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/hdlc.h>
Krzysztof HaƂasa4dfce402008-06-30 19:06:40 +020014#include <linux/if_arp.h>
15#include <linux/inetdevice.h>
16#include <linux/init.h>
17#include <linux/kernel.h>
18#include <linux/lapb.h>
19#include <linux/module.h>
20#include <linux/pkt_sched.h>
21#include <linux/poll.h>
22#include <linux/rtnetlink.h>
23#include <linux/skbuff.h>
24#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <net/x25device.h>
26
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020027static int x25_ioctl(struct net_device *dev, struct ifreq *ifr);
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* These functions are callbacks called by LAPB layer */
30
31static void x25_connect_disconnect(struct net_device *dev, int reason, int code)
32{
33 struct sk_buff *skb;
34 unsigned char *ptr;
35
36 if ((skb = dev_alloc_skb(1)) == NULL) {
37 printk(KERN_ERR "%s: out of memory\n", dev->name);
38 return;
39 }
40
41 ptr = skb_put(skb, 1);
42 *ptr = code;
43
44 skb->protocol = x25_type_trans(skb, dev);
45 netif_rx(skb);
46}
47
48
49
50static void x25_connected(struct net_device *dev, int reason)
51{
52 x25_connect_disconnect(dev, reason, 1);
53}
54
55
56
57static void x25_disconnected(struct net_device *dev, int reason)
58{
59 x25_connect_disconnect(dev, reason, 2);
60}
61
62
63
64static int x25_data_indication(struct net_device *dev, struct sk_buff *skb)
65{
66 unsigned char *ptr;
67
68 skb_push(skb, 1);
69
70 if (skb_cow(skb, 1))
71 return NET_RX_DROP;
72
73 ptr = skb->data;
74 *ptr = 0;
75
76 skb->protocol = x25_type_trans(skb, dev);
77 return netif_rx(skb);
78}
79
80
81
82static void x25_data_transmit(struct net_device *dev, struct sk_buff *skb)
83{
84 hdlc_device *hdlc = dev_to_hdlc(dev);
85 hdlc->xmit(skb, dev); /* Ignore return value :-( */
86}
87
88
89
90static int x25_xmit(struct sk_buff *skb, struct net_device *dev)
91{
92 int result;
93
94
95 /* X.25 to LAPB */
96 switch (skb->data[0]) {
97 case 0: /* Data to be transmitted */
98 skb_pull(skb, 1);
99 if ((result = lapb_data_request(dev, skb)) != LAPB_OK)
100 dev_kfree_skb(skb);
101 return 0;
102
103 case 1:
104 if ((result = lapb_connect_request(dev))!= LAPB_OK) {
105 if (result == LAPB_CONNECTED)
106 /* Send connect confirm. msg to level 3 */
107 x25_connected(dev, 0);
108 else
109 printk(KERN_ERR "%s: LAPB connect request "
110 "failed, error code = %i\n",
111 dev->name, result);
112 }
113 break;
114
115 case 2:
116 if ((result = lapb_disconnect_request(dev)) != LAPB_OK) {
117 if (result == LAPB_NOTCONNECTED)
118 /* Send disconnect confirm. msg to level 3 */
119 x25_disconnected(dev, 0);
120 else
121 printk(KERN_ERR "%s: LAPB disconnect request "
122 "failed, error code = %i\n",
123 dev->name, result);
124 }
125 break;
126
127 default: /* to be defined */
128 break;
129 }
130
131 dev_kfree_skb(skb);
132 return 0;
133}
134
135
136
137static int x25_open(struct net_device *dev)
138{
139 struct lapb_register_struct cb;
140 int result;
141
142 cb.connect_confirmation = x25_connected;
143 cb.connect_indication = x25_connected;
144 cb.disconnect_confirmation = x25_disconnected;
145 cb.disconnect_indication = x25_disconnected;
146 cb.data_indication = x25_data_indication;
147 cb.data_transmit = x25_data_transmit;
148
149 result = lapb_register(dev, &cb);
150 if (result != LAPB_OK)
151 return result;
152 return 0;
153}
154
155
156
157static void x25_close(struct net_device *dev)
158{
159 lapb_unregister(dev);
160}
161
162
163
164static int x25_rx(struct sk_buff *skb)
165{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200167 skb->dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return NET_RX_DROP;
169 }
170
171 if (lapb_data_received(skb->dev, skb) == LAPB_OK)
172 return NET_RX_SUCCESS;
173
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200174 skb->dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dev_kfree_skb_any(skb);
176 return NET_RX_DROP;
177}
178
179
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200180static struct hdlc_proto proto = {
181 .open = x25_open,
182 .close = x25_close,
183 .ioctl = x25_ioctl,
Krzysztof Halasa40d25142008-02-01 22:37:12 +0100184 .netif_rx = x25_rx,
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200185 .module = THIS_MODULE,
186};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200188
189static int x25_ioctl(struct net_device *dev, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 hdlc_device *hdlc = dev_to_hdlc(dev);
192 int result;
193
194 switch (ifr->ifr_settings.type) {
195 case IF_GET_PROTO:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200196 if (dev_to_hdlc(dev)->proto != &proto)
197 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 ifr->ifr_settings.type = IF_PROTO_X25;
199 return 0; /* return protocol only, no settable parameters */
200
201 case IF_PROTO_X25:
202 if(!capable(CAP_NET_ADMIN))
203 return -EPERM;
204
205 if(dev->flags & IFF_UP)
206 return -EBUSY;
207
208 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
209 if (result)
210 return result;
211
Krzysztof Halasa40d25142008-02-01 22:37:12 +0100212 if ((result = attach_hdlc_protocol(dev, &proto, 0)))
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200213 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 dev->hard_start_xmit = x25_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 dev->type = ARPHRD_X25;
Krzysztof Halasa4bc83b42006-07-21 14:41:01 -0700216 netif_dormant_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
218 }
219
220 return -EINVAL;
221}
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200222
223
224static int __init mod_init(void)
225{
226 register_hdlc_protocol(&proto);
227 return 0;
228}
229
230
231
232static void __exit mod_exit(void)
233{
234 unregister_hdlc_protocol(&proto);
235}
236
237
238module_init(mod_init);
239module_exit(mod_exit);
240
241MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
242MODULE_DESCRIPTION("X.25 protocol support for generic HDLC");
243MODULE_LICENSE("GPL v2");