Linux Audio

Check our new training course

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