summaryrefslogtreecommitdiffstats
path: root/net/x25/x25_link.c
blob: 34d065b833b3c663f9eed8054fc8ae391aa341fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 *	X.25 Packet Layer release 002
 *
 *	This is ALPHA test software. This code may break your machine, randomly fail to work with new 
 *	releases, misbehave and/or generally screw up. It might even work. 
 *
 *	This code REQUIRES 2.1.15 or higher
 *
 *	This module:
 *		This module is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 *	History
 *	X.25 001	Jonathan Naylor	  Started coding.
 *	X.25 002	Jonathan Naylor	  New timer architecture.
 *	mar/20/00	Daniela Squassoni Disabling/enabling of facilities 
 *					  negotiation.
 */

#include <linux/config.h>
#if defined(CONFIG_X25) || defined(CONFIG_X25_MODULE)
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/in.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/string.h>
#include <linux/sockios.h>
#include <linux/net.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <asm/segment.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/fcntl.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <net/x25.h>

static struct x25_neigh *x25_neigh_list = NULL;

static void x25_t20timer_expiry(unsigned long);

/*
 *	Linux set/reset timer routines
 */
static void x25_start_t20timer(struct x25_neigh *neigh)
{
	del_timer(&neigh->t20timer);

	neigh->t20timer.data     = (unsigned long)neigh;
	neigh->t20timer.function = &x25_t20timer_expiry;
	neigh->t20timer.expires  = jiffies + neigh->t20;

	add_timer(&neigh->t20timer);
}

static void x25_t20timer_expiry(unsigned long param)
{
	struct x25_neigh *neigh = (struct x25_neigh *)param;

	x25_transmit_restart_request(neigh);

	x25_start_t20timer(neigh);
}

static void x25_stop_t20timer(struct x25_neigh *neigh)
{
	del_timer(&neigh->t20timer);
}

static int x25_t20timer_pending(struct x25_neigh *neigh)
{
	return timer_pending(&neigh->t20timer);
}

/*
 *	This handles all restart and diagnostic frames.
 */
void x25_link_control(struct sk_buff *skb, struct x25_neigh *neigh, unsigned short frametype)
{
	struct sk_buff *skbn;
	int confirm;

	switch (frametype) {
		case X25_RESTART_REQUEST:
			confirm = !x25_t20timer_pending(neigh);
			x25_stop_t20timer(neigh);
			neigh->state = X25_LINK_STATE_3;
			if (confirm) x25_transmit_restart_confirmation(neigh);
			break;

		case X25_RESTART_CONFIRMATION:
			x25_stop_t20timer(neigh);
			neigh->state = X25_LINK_STATE_3;
			break;

		case X25_DIAGNOSTIC:
			printk(KERN_WARNING "x25: diagnostic #%d - %02X %02X %02X\n", skb->data[3], skb->data[4], skb->data[5], skb->data[6]);
			break;
			
		default:
			printk(KERN_WARNING "x25: received unknown %02X with LCI 000\n", frametype);
			break;
	}

	if (neigh->state == X25_LINK_STATE_3) {
		while ((skbn = skb_dequeue(&neigh->queue)) != NULL)
			x25_send_frame(skbn, neigh);
	}
}

/*
 *	This routine is called when a Restart Request is needed
 */
void x25_transmit_restart_request(struct x25_neigh *neigh)
{
	struct sk_buff *skb;
	unsigned char *dptr;
	int len;

	len = X25_MAX_L2_LEN + X25_STD_MIN_LEN + 2;

	if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
		return;

	skb_reserve(skb, X25_MAX_L2_LEN);

	dptr = skb_put(skb, X25_STD_MIN_LEN + 2);

	*dptr++ = (neigh->extended) ? X25_GFI_EXTSEQ : X25_GFI_STDSEQ;
	*dptr++ = 0x00;
	*dptr++ = X25_RESTART_REQUEST;
	*dptr++ = 0x00;
	*dptr++ = 0;

	skb->sk = NULL;

	x25_send_frame(skb, neigh);
}

/*
 * This routine is called when a Restart Confirmation is needed
 */
void x25_transmit_restart_confirmation(struct x25_neigh *neigh)
{
	struct sk_buff *skb;
	unsigned char *dptr;
	int len;

	len = X25_MAX_L2_LEN + X25_STD_MIN_LEN;

	if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
		return;

	skb_reserve(skb, X25_MAX_L2_LEN);

	dptr = skb_put(skb, X25_STD_MIN_LEN);

	*dptr++ = (neigh->extended) ? X25_GFI_EXTSEQ : X25_GFI_STDSEQ;
	*dptr++ = 0x00;
	*dptr++ = X25_RESTART_CONFIRMATION;

	skb->sk = NULL;

	x25_send_frame(skb, neigh);
}

/*
 * This routine is called when a Diagnostic is required.
 */
void x25_transmit_diagnostic(struct x25_neigh *neigh, unsigned char diag)
{
	struct sk_buff *skb;
	unsigned char *dptr;
	int len;

	len = X25_MAX_L2_LEN + X25_STD_MIN_LEN + 1;

	if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
		return;

	skb_reserve(skb, X25_MAX_L2_LEN);

	dptr = skb_put(skb, X25_STD_MIN_LEN + 1);

	*dptr++ = (neigh->extended) ? X25_GFI_EXTSEQ : X25_GFI_STDSEQ;
	*dptr++ = 0x00;
	*dptr++ = X25_DIAGNOSTIC;
	*dptr++ = diag;

	skb->sk = NULL;

	x25_send_frame(skb, neigh);
}

/*
 *	This routine is called when a Clear Request is needed outside of the context
 *	of a connected socket.
 */
void x25_transmit_clear_request(struct x25_neigh *neigh, unsigned int lci, unsigned char cause)
{
	struct sk_buff *skb;
	unsigned char *dptr;
	int len;

	len = X25_MAX_L2_LEN + X25_STD_MIN_LEN + 2;

	if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
		return;

	skb_reserve(skb, X25_MAX_L2_LEN);

	dptr = skb_put(skb, X25_STD_MIN_LEN + 2);

	*dptr++ = ((lci >> 8) & 0x0F) | (neigh->extended) ? X25_GFI_EXTSEQ : X25_GFI_STDSEQ;
	*dptr++ = ((lci >> 0) & 0xFF);
	*dptr++ = X25_CLEAR_REQUEST;
	*dptr++ = cause;
	*dptr++ = 0x00;

	skb->sk = NULL;

	x25_send_frame(skb, neigh);
}

void x25_transmit_link(struct sk_buff *skb, struct x25_neigh *neigh)
{
	switch (neigh->state) {
		case X25_LINK_STATE_0:
			skb_queue_tail(&neigh->queue, skb);
			neigh->state = X25_LINK_STATE_1;
			x25_establish_link(neigh);
			break;
		case X25_LINK_STATE_1:
		case X25_LINK_STATE_2:
			skb_queue_tail(&neigh->queue, skb);
			break;
		case X25_LINK_STATE_3:
			x25_send_frame(skb, neigh);
			break;
	}
}

/*
 *	Called when the link layer has become established.
 */
void x25_link_established(struct x25_neigh *neigh)
{
	switch (neigh->state) {
		case X25_LINK_STATE_0:
			neigh->state = X25_LINK_STATE_2;
			break;
		case X25_LINK_STATE_1:
			x25_transmit_restart_request(neigh);
			neigh->state = X25_LINK_STATE_2;
			x25_start_t20timer(neigh);
			break;
	}
}

/*
 *	Called when the link layer has terminated, or an establishment
 *	request has failed.
 */

void x25_link_terminated(struct x25_neigh *neigh)
{
	neigh->state = X25_LINK_STATE_0;
	/* Out of order: clear existing virtual calls (X.25 03/93 4.6.3) */
	x25_kill_by_neigh(neigh);
}

/*
 *	Add a new device.
 */
void x25_link_device_up(struct net_device *dev)
{
	struct x25_neigh *x25_neigh;
	unsigned long flags;

	if ((x25_neigh = kmalloc(sizeof(*x25_neigh), GFP_ATOMIC)) == NULL)
		return;

	skb_queue_head_init(&x25_neigh->queue);

	init_timer(&x25_neigh->t20timer);

	x25_neigh->dev      = dev;
	x25_neigh->state    = X25_LINK_STATE_0;
	x25_neigh->extended = 0;
	x25_neigh->global_facil_mask = (X25_MASK_REVERSE | X25_MASK_THROUGHPUT | X25_MASK_PACKET_SIZE | X25_MASK_WINDOW_SIZE); /* enables negotiation */
	x25_neigh->t20      = sysctl_x25_restart_request_timeout;

	save_flags(flags); cli();
	x25_neigh->next = x25_neigh_list;
	x25_neigh_list  = x25_neigh;
	restore_flags(flags);
}

static void x25_remove_neigh(struct x25_neigh *x25_neigh)
{
	struct x25_neigh *s;
	unsigned long flags;
	struct sk_buff *skb;

	while ((skb = skb_dequeue(&x25_neigh->queue)) != NULL)
		kfree_skb(skb);

	x25_stop_t20timer(x25_neigh);

	save_flags(flags); cli();

	if ((s = x25_neigh_list) == x25_neigh) {
		x25_neigh_list = x25_neigh->next;
		restore_flags(flags);
		kfree(x25_neigh);
		return;
	}

	while (s != NULL && s->next != NULL) {
		if (s->next == x25_neigh) {
			s->next = x25_neigh->next;
			restore_flags(flags);
			kfree(x25_neigh);
			return;
		}

		s = s->next;
	}

	restore_flags(flags);
}

/*
 *	A device has been removed, remove its links.
 */
void x25_link_device_down(struct net_device *dev)
{
	struct x25_neigh *neigh, *x25_neigh = x25_neigh_list;

	while (x25_neigh != NULL) {
		neigh     = x25_neigh;
		x25_neigh = x25_neigh->next;

		if (neigh->dev == dev)
			x25_remove_neigh(neigh);
	}
}

/*
 *	Given a device, return the neighbour address.
 */
struct x25_neigh *x25_get_neigh(struct net_device *dev)
{
	struct x25_neigh *x25_neigh;

	for (x25_neigh = x25_neigh_list; x25_neigh != NULL; x25_neigh = x25_neigh->next)
		if (x25_neigh->dev == dev)
			return x25_neigh;

	return NULL;
}

/*
 *	Handle the ioctls that control the subscription functions.
 */
int x25_subscr_ioctl(unsigned int cmd, void *arg)
{
	struct x25_subscrip_struct x25_subscr;
	struct x25_neigh *x25_neigh;
	struct net_device *dev;

	switch (cmd) {

		case SIOCX25GSUBSCRIP:
			if (copy_from_user(&x25_subscr, arg, sizeof(struct x25_subscrip_struct)))
				return -EFAULT;
			if ((dev = x25_dev_get(x25_subscr.device)) == NULL)
				return -EINVAL;
			if ((x25_neigh = x25_get_neigh(dev)) == NULL) {
				dev_put(dev);
				return -EINVAL;
			}
			dev_put(dev);
			x25_subscr.extended = x25_neigh->extended;
			x25_subscr.global_facil_mask = x25_neigh->global_facil_mask;
			if (copy_to_user(arg, &x25_subscr, sizeof(struct x25_subscrip_struct)))
				return -EFAULT;
			break;

		case SIOCX25SSUBSCRIP:
			if (copy_from_user(&x25_subscr, arg, sizeof(struct x25_subscrip_struct)))
				return -EFAULT;
			if ((dev = x25_dev_get(x25_subscr.device)) == NULL)
				return -EINVAL;
			if ((x25_neigh = x25_get_neigh(dev)) == NULL) {
				dev_put(dev);
				return -EINVAL;
			}
			dev_put(dev);
			if (x25_subscr.extended != 0 && x25_subscr.extended != 1)
				return -EINVAL;
			x25_neigh->extended = x25_subscr.extended;
			x25_neigh->global_facil_mask = x25_subscr.global_facil_mask;
			break;

		default:
			return -EINVAL;
	}

	return 0;
}

#ifdef MODULE

/*
 *	Release all memory associated with X.25 neighbour structures.
 */
void x25_link_free(void)
{
	struct x25_neigh *neigh, *x25_neigh = x25_neigh_list;

	while (x25_neigh != NULL) {
		neigh     = x25_neigh;
		x25_neigh = x25_neigh->next;

		x25_remove_neigh(neigh);
	}
}

#endif

#endif