Linux Audio

Check our new training course

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