1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Generic HDLC support routines for Linux
4 * X.25 support
5 *
6 * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
7 */
8
9 #include <linux/errno.h>
10 #include <linux/gfp.h>
11 #include <linux/hdlc.h>
12 #include <linux/if_arp.h>
13 #include <linux/inetdevice.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/lapb.h>
17 #include <linux/module.h>
18 #include <linux/pkt_sched.h>
19 #include <linux/poll.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/skbuff.h>
22 #include <net/x25device.h>
23
24 struct x25_state {
25 x25_hdlc_proto settings;
26 bool up;
27 spinlock_t up_lock; /* Protects "up" */
28 };
29
30 static int x25_ioctl(struct net_device *dev, struct ifreq *ifr);
31
state(hdlc_device * hdlc)32 static struct x25_state *state(hdlc_device *hdlc)
33 {
34 return hdlc->state;
35 }
36
37 /* These functions are callbacks called by LAPB layer */
38
x25_connect_disconnect(struct net_device * dev,int reason,int code)39 static void x25_connect_disconnect(struct net_device *dev, int reason, int code)
40 {
41 struct sk_buff *skb;
42 unsigned char *ptr;
43
44 if ((skb = dev_alloc_skb(1)) == NULL) {
45 netdev_err(dev, "out of memory\n");
46 return;
47 }
48
49 ptr = skb_put(skb, 1);
50 *ptr = code;
51
52 skb->protocol = x25_type_trans(skb, dev);
53 netif_rx(skb);
54 }
55
56
57
x25_connected(struct net_device * dev,int reason)58 static void x25_connected(struct net_device *dev, int reason)
59 {
60 x25_connect_disconnect(dev, reason, X25_IFACE_CONNECT);
61 }
62
63
64
x25_disconnected(struct net_device * dev,int reason)65 static void x25_disconnected(struct net_device *dev, int reason)
66 {
67 x25_connect_disconnect(dev, reason, X25_IFACE_DISCONNECT);
68 }
69
70
71
x25_data_indication(struct net_device * dev,struct sk_buff * skb)72 static int x25_data_indication(struct net_device *dev, struct sk_buff *skb)
73 {
74 unsigned char *ptr;
75
76 if (skb_cow(skb, 1)) {
77 kfree_skb(skb);
78 return NET_RX_DROP;
79 }
80
81 skb_push(skb, 1);
82 skb_reset_network_header(skb);
83
84 ptr = skb->data;
85 *ptr = X25_IFACE_DATA;
86
87 skb->protocol = x25_type_trans(skb, dev);
88 return netif_rx(skb);
89 }
90
91
92
x25_data_transmit(struct net_device * dev,struct sk_buff * skb)93 static void x25_data_transmit(struct net_device *dev, struct sk_buff *skb)
94 {
95 hdlc_device *hdlc = dev_to_hdlc(dev);
96
97 skb_reset_network_header(skb);
98 skb->protocol = hdlc_type_trans(skb, dev);
99
100 if (dev_nit_active(dev))
101 dev_queue_xmit_nit(skb, dev);
102
103 hdlc->xmit(skb, dev); /* Ignore return value :-( */
104 }
105
106
107
x25_xmit(struct sk_buff * skb,struct net_device * dev)108 static netdev_tx_t x25_xmit(struct sk_buff *skb, struct net_device *dev)
109 {
110 hdlc_device *hdlc = dev_to_hdlc(dev);
111 struct x25_state *x25st = state(hdlc);
112 int result;
113
114 /* There should be a pseudo header of 1 byte added by upper layers.
115 * Check to make sure it is there before reading it.
116 */
117 if (skb->len < 1) {
118 kfree_skb(skb);
119 return NETDEV_TX_OK;
120 }
121
122 spin_lock_bh(&x25st->up_lock);
123 if (!x25st->up) {
124 spin_unlock_bh(&x25st->up_lock);
125 kfree_skb(skb);
126 return NETDEV_TX_OK;
127 }
128
129 switch (skb->data[0]) {
130 case X25_IFACE_DATA: /* Data to be transmitted */
131 skb_pull(skb, 1);
132 skb_reset_network_header(skb);
133 if ((result = lapb_data_request(dev, skb)) != LAPB_OK)
134 dev_kfree_skb(skb);
135 spin_unlock_bh(&x25st->up_lock);
136 return NETDEV_TX_OK;
137
138 case X25_IFACE_CONNECT:
139 if ((result = lapb_connect_request(dev))!= LAPB_OK) {
140 if (result == LAPB_CONNECTED)
141 /* Send connect confirm. msg to level 3 */
142 x25_connected(dev, 0);
143 else
144 netdev_err(dev, "LAPB connect request failed, error code = %i\n",
145 result);
146 }
147 break;
148
149 case X25_IFACE_DISCONNECT:
150 if ((result = lapb_disconnect_request(dev)) != LAPB_OK) {
151 if (result == LAPB_NOTCONNECTED)
152 /* Send disconnect confirm. msg to level 3 */
153 x25_disconnected(dev, 0);
154 else
155 netdev_err(dev, "LAPB disconnect request failed, error code = %i\n",
156 result);
157 }
158 break;
159
160 default: /* to be defined */
161 break;
162 }
163
164 spin_unlock_bh(&x25st->up_lock);
165 dev_kfree_skb(skb);
166 return NETDEV_TX_OK;
167 }
168
169
170
x25_open(struct net_device * dev)171 static int x25_open(struct net_device *dev)
172 {
173 static const struct lapb_register_struct cb = {
174 .connect_confirmation = x25_connected,
175 .connect_indication = x25_connected,
176 .disconnect_confirmation = x25_disconnected,
177 .disconnect_indication = x25_disconnected,
178 .data_indication = x25_data_indication,
179 .data_transmit = x25_data_transmit,
180 };
181 hdlc_device *hdlc = dev_to_hdlc(dev);
182 struct x25_state *x25st = state(hdlc);
183 struct lapb_parms_struct params;
184 int result;
185
186 result = lapb_register(dev, &cb);
187 if (result != LAPB_OK)
188 return -ENOMEM;
189
190 result = lapb_getparms(dev, ¶ms);
191 if (result != LAPB_OK)
192 return -EINVAL;
193
194 if (state(hdlc)->settings.dce)
195 params.mode = params.mode | LAPB_DCE;
196
197 if (state(hdlc)->settings.modulo == 128)
198 params.mode = params.mode | LAPB_EXTENDED;
199
200 params.window = state(hdlc)->settings.window;
201 params.t1 = state(hdlc)->settings.t1;
202 params.t2 = state(hdlc)->settings.t2;
203 params.n2 = state(hdlc)->settings.n2;
204
205 result = lapb_setparms(dev, ¶ms);
206 if (result != LAPB_OK)
207 return -EINVAL;
208
209 spin_lock_bh(&x25st->up_lock);
210 x25st->up = true;
211 spin_unlock_bh(&x25st->up_lock);
212
213 return 0;
214 }
215
216
217
x25_close(struct net_device * dev)218 static void x25_close(struct net_device *dev)
219 {
220 hdlc_device *hdlc = dev_to_hdlc(dev);
221 struct x25_state *x25st = state(hdlc);
222
223 spin_lock_bh(&x25st->up_lock);
224 x25st->up = false;
225 spin_unlock_bh(&x25st->up_lock);
226
227 lapb_unregister(dev);
228 }
229
230
231
x25_rx(struct sk_buff * skb)232 static int x25_rx(struct sk_buff *skb)
233 {
234 struct net_device *dev = skb->dev;
235 hdlc_device *hdlc = dev_to_hdlc(dev);
236 struct x25_state *x25st = state(hdlc);
237
238 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
239 dev->stats.rx_dropped++;
240 return NET_RX_DROP;
241 }
242
243 spin_lock_bh(&x25st->up_lock);
244 if (!x25st->up) {
245 spin_unlock_bh(&x25st->up_lock);
246 kfree_skb(skb);
247 dev->stats.rx_dropped++;
248 return NET_RX_DROP;
249 }
250
251 if (lapb_data_received(dev, skb) == LAPB_OK) {
252 spin_unlock_bh(&x25st->up_lock);
253 return NET_RX_SUCCESS;
254 }
255
256 spin_unlock_bh(&x25st->up_lock);
257 dev->stats.rx_errors++;
258 dev_kfree_skb_any(skb);
259 return NET_RX_DROP;
260 }
261
262
263 static struct hdlc_proto proto = {
264 .open = x25_open,
265 .close = x25_close,
266 .ioctl = x25_ioctl,
267 .netif_rx = x25_rx,
268 .xmit = x25_xmit,
269 .module = THIS_MODULE,
270 };
271
272
x25_ioctl(struct net_device * dev,struct ifreq * ifr)273 static int x25_ioctl(struct net_device *dev, struct ifreq *ifr)
274 {
275 x25_hdlc_proto __user *x25_s = ifr->ifr_settings.ifs_ifsu.x25;
276 const size_t size = sizeof(x25_hdlc_proto);
277 hdlc_device *hdlc = dev_to_hdlc(dev);
278 x25_hdlc_proto new_settings;
279 int result;
280
281 switch (ifr->ifr_settings.type) {
282 case IF_GET_PROTO:
283 if (dev_to_hdlc(dev)->proto != &proto)
284 return -EINVAL;
285 ifr->ifr_settings.type = IF_PROTO_X25;
286 if (ifr->ifr_settings.size < size) {
287 ifr->ifr_settings.size = size; /* data size wanted */
288 return -ENOBUFS;
289 }
290 if (copy_to_user(x25_s, &state(hdlc)->settings, size))
291 return -EFAULT;
292 return 0;
293
294 case IF_PROTO_X25:
295 if (!capable(CAP_NET_ADMIN))
296 return -EPERM;
297
298 if (dev->flags & IFF_UP)
299 return -EBUSY;
300
301 /* backward compatibility */
302 if (ifr->ifr_settings.size == 0) {
303 new_settings.dce = 0;
304 new_settings.modulo = 8;
305 new_settings.window = 7;
306 new_settings.t1 = 3;
307 new_settings.t2 = 1;
308 new_settings.n2 = 10;
309 }
310 else {
311 if (copy_from_user(&new_settings, x25_s, size))
312 return -EFAULT;
313
314 if ((new_settings.dce != 0 &&
315 new_settings.dce != 1) ||
316 (new_settings.modulo != 8 &&
317 new_settings.modulo != 128) ||
318 new_settings.window < 1 ||
319 (new_settings.modulo == 8 &&
320 new_settings.window > 7) ||
321 (new_settings.modulo == 128 &&
322 new_settings.window > 127) ||
323 new_settings.t1 < 1 ||
324 new_settings.t1 > 255 ||
325 new_settings.t2 < 1 ||
326 new_settings.t2 > 255 ||
327 new_settings.n2 < 1 ||
328 new_settings.n2 > 255)
329 return -EINVAL;
330 }
331
332 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
333 if (result)
334 return result;
335
336 if ((result = attach_hdlc_protocol(dev, &proto,
337 sizeof(struct x25_state))))
338 return result;
339
340 memcpy(&state(hdlc)->settings, &new_settings, size);
341 state(hdlc)->up = false;
342 spin_lock_init(&state(hdlc)->up_lock);
343
344 /* There's no header_ops so hard_header_len should be 0. */
345 dev->hard_header_len = 0;
346 /* When transmitting data:
347 * first we'll remove a pseudo header of 1 byte,
348 * then we'll prepend an LAPB header of at most 3 bytes.
349 */
350 dev->needed_headroom = 3 - 1;
351
352 dev->type = ARPHRD_X25;
353 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
354 netif_dormant_off(dev);
355 return 0;
356 }
357
358 return -EINVAL;
359 }
360
361
mod_init(void)362 static int __init mod_init(void)
363 {
364 register_hdlc_protocol(&proto);
365 return 0;
366 }
367
368
369
mod_exit(void)370 static void __exit mod_exit(void)
371 {
372 unregister_hdlc_protocol(&proto);
373 }
374
375
376 module_init(mod_init);
377 module_exit(mod_exit);
378
379 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
380 MODULE_DESCRIPTION("X.25 protocol support for generic HDLC");
381 MODULE_LICENSE("GPL v2");
382