xref: /linux/drivers/net/wan/hdlc_x25.c (revision 95298d63c67673c654c08952672d016212b26054)
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 };
27 
28 static int x25_ioctl(struct net_device *dev, struct ifreq *ifr);
29 
30 static struct x25_state *state(hdlc_device *hdlc)
31 {
32 	return hdlc->state;
33 }
34 
35 /* These functions are callbacks called by LAPB layer */
36 
37 static void x25_connect_disconnect(struct net_device *dev, int reason, int code)
38 {
39 	struct sk_buff *skb;
40 	unsigned char *ptr;
41 
42 	if ((skb = dev_alloc_skb(1)) == NULL) {
43 		netdev_err(dev, "out of memory\n");
44 		return;
45 	}
46 
47 	ptr = skb_put(skb, 1);
48 	*ptr = code;
49 
50 	skb->protocol = x25_type_trans(skb, dev);
51 	netif_rx(skb);
52 }
53 
54 
55 
56 static void x25_connected(struct net_device *dev, int reason)
57 {
58 	x25_connect_disconnect(dev, reason, X25_IFACE_CONNECT);
59 }
60 
61 
62 
63 static void x25_disconnected(struct net_device *dev, int reason)
64 {
65 	x25_connect_disconnect(dev, reason, X25_IFACE_DISCONNECT);
66 }
67 
68 
69 
70 static int x25_data_indication(struct net_device *dev, struct sk_buff *skb)
71 {
72 	unsigned char *ptr;
73 
74 	if (skb_cow(skb, 1))
75 		return NET_RX_DROP;
76 
77 	skb_push(skb, 1);
78 	skb_reset_network_header(skb);
79 
80 	ptr  = skb->data;
81 	*ptr = X25_IFACE_DATA;
82 
83 	skb->protocol = x25_type_trans(skb, dev);
84 	return netif_rx(skb);
85 }
86 
87 
88 
89 static void x25_data_transmit(struct net_device *dev, struct sk_buff *skb)
90 {
91 	hdlc_device *hdlc = dev_to_hdlc(dev);
92 
93 	skb_reset_network_header(skb);
94 	skb->protocol = hdlc_type_trans(skb, dev);
95 
96 	if (dev_nit_active(dev))
97 		dev_queue_xmit_nit(skb, dev);
98 
99 	hdlc->xmit(skb, dev); /* Ignore return value :-( */
100 }
101 
102 
103 
104 static netdev_tx_t x25_xmit(struct sk_buff *skb, struct net_device *dev)
105 {
106 	int result;
107 
108 
109 	/* X.25 to LAPB */
110 	switch (skb->data[0]) {
111 	case X25_IFACE_DATA:	/* Data to be transmitted */
112 		skb_pull(skb, 1);
113 		skb_reset_network_header(skb);
114 		if ((result = lapb_data_request(dev, skb)) != LAPB_OK)
115 			dev_kfree_skb(skb);
116 		return NETDEV_TX_OK;
117 
118 	case X25_IFACE_CONNECT:
119 		if ((result = lapb_connect_request(dev))!= LAPB_OK) {
120 			if (result == LAPB_CONNECTED)
121 				/* Send connect confirm. msg to level 3 */
122 				x25_connected(dev, 0);
123 			else
124 				netdev_err(dev, "LAPB connect request failed, error code = %i\n",
125 					   result);
126 		}
127 		break;
128 
129 	case X25_IFACE_DISCONNECT:
130 		if ((result = lapb_disconnect_request(dev)) != LAPB_OK) {
131 			if (result == LAPB_NOTCONNECTED)
132 				/* Send disconnect confirm. msg to level 3 */
133 				x25_disconnected(dev, 0);
134 			else
135 				netdev_err(dev, "LAPB disconnect request failed, error code = %i\n",
136 					   result);
137 		}
138 		break;
139 
140 	default:		/* to be defined */
141 		break;
142 	}
143 
144 	dev_kfree_skb(skb);
145 	return NETDEV_TX_OK;
146 }
147 
148 
149 
150 static int x25_open(struct net_device *dev)
151 {
152 	static const struct lapb_register_struct cb = {
153 		.connect_confirmation = x25_connected,
154 		.connect_indication = x25_connected,
155 		.disconnect_confirmation = x25_disconnected,
156 		.disconnect_indication = x25_disconnected,
157 		.data_indication = x25_data_indication,
158 		.data_transmit = x25_data_transmit,
159 	};
160 	hdlc_device *hdlc = dev_to_hdlc(dev);
161 	struct lapb_parms_struct params;
162 	int result;
163 
164 	result = lapb_register(dev, &cb);
165 	if (result != LAPB_OK)
166 		return result;
167 
168 	result = lapb_getparms(dev, &params);
169 	if (result != LAPB_OK)
170 		return result;
171 
172 	if (state(hdlc)->settings.dce)
173 		params.mode = params.mode | LAPB_DCE;
174 
175 	if (state(hdlc)->settings.modulo == 128)
176 		params.mode = params.mode | LAPB_EXTENDED;
177 
178 	params.window = state(hdlc)->settings.window;
179 	params.t1 = state(hdlc)->settings.t1;
180 	params.t2 = state(hdlc)->settings.t2;
181 	params.n2 = state(hdlc)->settings.n2;
182 
183 	result = lapb_setparms(dev, &params);
184 	if (result != LAPB_OK)
185 		return result;
186 
187 	return 0;
188 }
189 
190 
191 
192 static void x25_close(struct net_device *dev)
193 {
194 	lapb_unregister(dev);
195 }
196 
197 
198 
199 static int x25_rx(struct sk_buff *skb)
200 {
201 	struct net_device *dev = skb->dev;
202 
203 	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
204 		dev->stats.rx_dropped++;
205 		return NET_RX_DROP;
206 	}
207 
208 	if (lapb_data_received(dev, skb) == LAPB_OK)
209 		return NET_RX_SUCCESS;
210 
211 	dev->stats.rx_errors++;
212 	dev_kfree_skb_any(skb);
213 	return NET_RX_DROP;
214 }
215 
216 
217 static struct hdlc_proto proto = {
218 	.open		= x25_open,
219 	.close		= x25_close,
220 	.ioctl		= x25_ioctl,
221 	.netif_rx	= x25_rx,
222 	.xmit		= x25_xmit,
223 	.module		= THIS_MODULE,
224 };
225 
226 
227 static int x25_ioctl(struct net_device *dev, struct ifreq *ifr)
228 {
229 	x25_hdlc_proto __user *x25_s = ifr->ifr_settings.ifs_ifsu.x25;
230 	const size_t size = sizeof(x25_hdlc_proto);
231 	hdlc_device *hdlc = dev_to_hdlc(dev);
232 	x25_hdlc_proto new_settings;
233 	int result;
234 
235 	switch (ifr->ifr_settings.type) {
236 	case IF_GET_PROTO:
237 		if (dev_to_hdlc(dev)->proto != &proto)
238 			return -EINVAL;
239 		ifr->ifr_settings.type = IF_PROTO_X25;
240 		if (ifr->ifr_settings.size < size) {
241 			ifr->ifr_settings.size = size; /* data size wanted */
242 			return -ENOBUFS;
243 		}
244 		if (copy_to_user(x25_s, &state(hdlc)->settings, size))
245 			return -EFAULT;
246 		return 0;
247 
248 	case IF_PROTO_X25:
249 		if (!capable(CAP_NET_ADMIN))
250 			return -EPERM;
251 
252 		if (dev->flags & IFF_UP)
253 			return -EBUSY;
254 
255 		/* backward compatibility */
256 		if (ifr->ifr_settings.size == 0) {
257 			new_settings.dce = 0;
258 			new_settings.modulo = 8;
259 			new_settings.window = 7;
260 			new_settings.t1 = 3;
261 			new_settings.t2 = 1;
262 			new_settings.n2 = 10;
263 		}
264 		else {
265 			if (copy_from_user(&new_settings, x25_s, size))
266 				return -EFAULT;
267 
268 			if ((new_settings.dce != 0 &&
269 			new_settings.dce != 1) ||
270 			(new_settings.modulo != 8 &&
271 			new_settings.modulo != 128) ||
272 			new_settings.window < 1 ||
273 			(new_settings.modulo == 8 &&
274 			new_settings.window > 7) ||
275 			(new_settings.modulo == 128 &&
276 			new_settings.window > 127) ||
277 			new_settings.t1 < 1 ||
278 			new_settings.t1 > 255 ||
279 			new_settings.t2 < 1 ||
280 			new_settings.t2 > 255 ||
281 			new_settings.n2 < 1 ||
282 			new_settings.n2 > 255)
283 				return -EINVAL;
284 		}
285 
286 		result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
287 		if (result)
288 			return result;
289 
290 		if ((result = attach_hdlc_protocol(dev, &proto,
291 						   sizeof(struct x25_state))))
292 			return result;
293 
294 		memcpy(&state(hdlc)->settings, &new_settings, size);
295 		dev->type = ARPHRD_X25;
296 		call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
297 		netif_dormant_off(dev);
298 		return 0;
299 	}
300 
301 	return -EINVAL;
302 }
303 
304 
305 static int __init mod_init(void)
306 {
307 	register_hdlc_protocol(&proto);
308 	return 0;
309 }
310 
311 
312 
313 static void __exit mod_exit(void)
314 {
315 	unregister_hdlc_protocol(&proto);
316 }
317 
318 
319 module_init(mod_init);
320 module_exit(mod_exit);
321 
322 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
323 MODULE_DESCRIPTION("X.25 protocol support for generic HDLC");
324 MODULE_LICENSE("GPL v2");
325