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