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