Blame view

net/x25/x25_in.c 9.77 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
  /*
   *	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
f8e1d2018   YOSHIFUJI Hideaki   [NET] X25: Fix wh...
6
   *	screw up. It might even work.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
9
10
11
12
13
14
15
16
17
18
19
   *
   *	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	  Centralised disconnection code.
   *					  New timer architecture.
f8e1d2018   YOSHIFUJI Hideaki   [NET] X25: Fix wh...
20
   *	2000-03-20	Daniela Squassoni Disabling/enabling of facilities
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
23
24
   *					  negotiation.
   *	2000-11-10	Henner Eisen	  Check and reset for out-of-sequence
   *					  i-frames.
   */
5a0e3ad6a   Tejun Heo   include cleanup: ...
25
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
29
30
  #include <linux/errno.h>
  #include <linux/kernel.h>
  #include <linux/string.h>
  #include <linux/skbuff.h>
  #include <net/sock.h>
c752f0739   Arnaldo Carvalho de Melo   [TCP]: Move the t...
31
  #include <net/tcp_states.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  #include <net/x25.h>
  
  static int x25_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more)
  {
  	struct sk_buff *skbo, *skbn = skb;
  	struct x25_sock *x25 = x25_sk(sk);
  
  	if (more) {
  		x25->fraglen += skb->len;
  		skb_queue_tail(&x25->fragment_queue, skb);
  		skb_set_owner_r(skb, sk);
  		return 0;
  	}
  
  	if (!more && x25->fraglen > 0) {	/* End of fragment */
  		int len = x25->fraglen + skb->len;
  
  		if ((skbn = alloc_skb(len, GFP_ATOMIC)) == NULL){
  			kfree_skb(skb);
  			return 1;
  		}
  
  		skb_queue_tail(&x25->fragment_queue, skb);
badff6d01   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
55
  		skb_reset_transport_header(skbn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
  
  		skbo = skb_dequeue(&x25->fragment_queue);
1a4e2d093   Arnaldo Carvalho de Melo   [SK_BUFF]: Some m...
58
59
  		skb_copy_from_linear_data(skbo, skb_put(skbn, skbo->len),
  					  skbo->len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
61
62
63
64
65
  		kfree_skb(skbo);
  
  		while ((skbo =
  			skb_dequeue(&x25->fragment_queue)) != NULL) {
  			skb_pull(skbo, (x25->neighbour->extended) ?
  					X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1a4e2d093   Arnaldo Carvalho de Melo   [SK_BUFF]: Some m...
66
67
68
  			skb_copy_from_linear_data(skbo,
  						  skb_put(skbn, skbo->len),
  						  skbo->len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
70
  			kfree_skb(skbo);
  		}
f8e1d2018   YOSHIFUJI Hideaki   [NET] X25: Fix wh...
71
  		x25->fraglen = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  	}
  
  	skb_set_owner_r(skbn, sk);
  	skb_queue_tail(&sk->sk_receive_queue, skbn);
  	if (!sock_flag(sk, SOCK_DEAD))
  		sk->sk_data_ready(sk, skbn->len);
  
  	return 0;
  }
  
  /*
   * State machine for state 1, Awaiting Call Accepted State.
   * The handling of the timer(s) is in file x25_timer.c.
   * Handling of state 0 and connection release is in af_x25.c.
   */
  static int x25_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype)
  {
  	struct x25_address source_addr, dest_addr;
f5eb917b8   John Hughes   x25: Patch to fix...
90
  	int len;
95c304300   andrew hendry   x25: possible skb...
91
  	struct x25_sock *x25 = x25_sk(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
93
  
  	switch (frametype) {
fddc5f3e9   Joe Perches   x25: Reduce switc...
94
95
96
97
98
99
100
101
102
103
104
105
106
  	case X25_CALL_ACCEPTED: {
  
  		x25_stop_timer(sk);
  		x25->condition = 0x00;
  		x25->vs        = 0;
  		x25->va        = 0;
  		x25->vr        = 0;
  		x25->vl        = 0;
  		x25->state     = X25_STATE_3;
  		sk->sk_state   = TCP_ESTABLISHED;
  		/*
  		 *	Parse the data in the frame.
  		 */
cb101ed2c   Matthew Daley   x25: Handle under...
107
108
  		if (!pskb_may_pull(skb, X25_STD_MIN_LEN))
  			goto out_clear;
fddc5f3e9   Joe Perches   x25: Reduce switc...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  		skb_pull(skb, X25_STD_MIN_LEN);
  
  		len = x25_parse_address_block(skb, &source_addr,
  					      &dest_addr);
  		if (len > 0)
  			skb_pull(skb, len);
  		else if (len < 0)
  			goto out_clear;
  
  		len = x25_parse_facilities(skb, &x25->facilities,
  					   &x25->dte_facilities,
  					   &x25->vc_facil_mask);
  		if (len > 0)
  			skb_pull(skb, len);
  		else if (len < 0)
  			goto out_clear;
  		/*
  		 *	Copy any Call User Data.
  		 */
  		if (skb->len > 0) {
c7fd0d48b   Matthew Daley   x25: Validate inc...
129
130
  			if (skb->len > X25_MAX_CUD_LEN)
  				goto out_clear;
cb101ed2c   Matthew Daley   x25: Handle under...
131
132
  			skb_copy_bits(skb, 0, x25->calluserdata.cuddata,
  				skb->len);
fddc5f3e9   Joe Perches   x25: Reduce switc...
133
  			x25->calluserdata.cudlength = skb->len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
  		}
fddc5f3e9   Joe Perches   x25: Reduce switc...
135
136
137
138
139
  		if (!sock_flag(sk, SOCK_DEAD))
  			sk->sk_state_change(sk);
  		break;
  	}
  	case X25_CLEAR_REQUEST:
cb101ed2c   Matthew Daley   x25: Handle under...
140
141
  		if (!pskb_may_pull(skb, X25_STD_MIN_LEN + 2))
  			goto out_clear;
fddc5f3e9   Joe Perches   x25: Reduce switc...
142
143
144
  		x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
  		x25_disconnect(sk, ECONNREFUSED, skb->data[3], skb->data[4]);
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145

fddc5f3e9   Joe Perches   x25: Reduce switc...
146
147
  	default:
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
150
  	}
  
  	return 0;
95c304300   andrew hendry   x25: possible skb...
151
152
153
154
155
156
  
  out_clear:
  	x25_write_internal(sk, X25_CLEAR_REQUEST);
  	x25->state = X25_STATE_2;
  	x25_start_t23timer(sk);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
158
159
160
161
162
163
164
165
166
167
168
  }
  
  /*
   * State machine for state 2, Awaiting Clear Confirmation State.
   * The handling of the timer(s) is in file x25_timer.c
   * Handling of state 0 and connection release is in af_x25.c.
   */
  static int x25_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype)
  {
  	switch (frametype) {
  
  		case X25_CLEAR_REQUEST:
cb101ed2c   Matthew Daley   x25: Handle under...
169
170
  			if (!pskb_may_pull(skb, X25_STD_MIN_LEN + 2))
  				goto out_clear;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
171
172
173
174
175
176
177
178
179
180
181
182
183
  			x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
  			x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
  			break;
  
  		case X25_CLEAR_CONFIRMATION:
  			x25_disconnect(sk, 0, 0, 0);
  			break;
  
  		default:
  			break;
  	}
  
  	return 0;
cb101ed2c   Matthew Daley   x25: Handle under...
184
185
186
187
188
  
  out_clear:
  	x25_write_internal(sk, X25_CLEAR_REQUEST);
  	x25_start_t23timer(sk);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
189
190
191
192
193
194
195
196
197
198
199
200
  }
  
  /*
   * State machine for state 3, Connected State.
   * The handling of the timer(s) is in file x25_timer.c
   * Handling of state 0 and connection release is in af_x25.c.
   */
  static int x25_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype, int ns, int nr, int q, int d, int m)
  {
  	int queued = 0;
  	int modulus;
  	struct x25_sock *x25 = x25_sk(sk);
f8e1d2018   YOSHIFUJI Hideaki   [NET] X25: Fix wh...
201

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  	modulus = (x25->neighbour->extended) ? X25_EMODULUS : X25_SMODULUS;
  
  	switch (frametype) {
  
  		case X25_RESET_REQUEST:
  			x25_write_internal(sk, X25_RESET_CONFIRMATION);
  			x25_stop_timer(sk);
  			x25->condition = 0x00;
  			x25->vs        = 0;
  			x25->vr        = 0;
  			x25->va        = 0;
  			x25->vl        = 0;
  			x25_requeue_frames(sk);
  			break;
  
  		case X25_CLEAR_REQUEST:
cb101ed2c   Matthew Daley   x25: Handle under...
218
219
  			if (!pskb_may_pull(skb, X25_STD_MIN_LEN + 2))
  				goto out_clear;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  			x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
  			x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
  			break;
  
  		case X25_RR:
  		case X25_RNR:
  			if (!x25_validate_nr(sk, nr)) {
  				x25_clear_queues(sk);
  				x25_write_internal(sk, X25_RESET_REQUEST);
  				x25_start_t22timer(sk);
  				x25->condition = 0x00;
  				x25->vs        = 0;
  				x25->vr        = 0;
  				x25->va        = 0;
  				x25->vl        = 0;
  				x25->state     = X25_STATE_4;
  			} else {
  				x25_frames_acked(sk, nr);
  				if (frametype == X25_RNR) {
  					x25->condition |= X25_COND_PEER_RX_BUSY;
  				} else {
  					x25->condition &= ~X25_COND_PEER_RX_BUSY;
  				}
  			}
  			break;
  
  		case X25_DATA:	/* XXX */
  			x25->condition &= ~X25_COND_PEER_RX_BUSY;
  			if ((ns != x25->vr) || !x25_validate_nr(sk, nr)) {
  				x25_clear_queues(sk);
  				x25_write_internal(sk, X25_RESET_REQUEST);
  				x25_start_t22timer(sk);
  				x25->condition = 0x00;
  				x25->vs        = 0;
  				x25->vr        = 0;
  				x25->va        = 0;
  				x25->vl        = 0;
  				x25->state     = X25_STATE_4;
  				break;
  			}
  			x25_frames_acked(sk, nr);
  			if (ns == x25->vr) {
  				if (x25_queue_rx_frame(sk, skb, m) == 0) {
  					x25->vr = (x25->vr + 1) % modulus;
  					queued = 1;
  				} else {
  					/* Should never happen */
  					x25_clear_queues(sk);
  					x25_write_internal(sk, X25_RESET_REQUEST);
  					x25_start_t22timer(sk);
  					x25->condition = 0x00;
  					x25->vs        = 0;
  					x25->vr        = 0;
  					x25->va        = 0;
  					x25->vl        = 0;
  					x25->state     = X25_STATE_4;
  					break;
  				}
  				if (atomic_read(&sk->sk_rmem_alloc) >
6bf1574ee   Eric Dumazet   [X25]: Avoid divi...
279
  				    (sk->sk_rcvbuf >> 1))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
  					x25->condition |= X25_COND_OWN_RX_BUSY;
  			}
  			/*
  			 *	If the window is full Ack it immediately, else
  			 *	start the holdback timer.
  			 */
  			if (((x25->vl + x25->facilities.winsize_in) % modulus) == x25->vr) {
  				x25->condition &= ~X25_COND_ACK_PENDING;
  				x25_stop_timer(sk);
  				x25_enquiry_response(sk);
  			} else {
  				x25->condition |= X25_COND_ACK_PENDING;
  				x25_start_t2timer(sk);
  			}
  			break;
  
  		case X25_INTERRUPT_CONFIRMATION:
b7792e34c   andrew hendry   X25: Move interru...
297
  			clear_bit(X25_INTERRUPT_FLAG, &x25->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
  			break;
  
  		case X25_INTERRUPT:
  			if (sock_flag(sk, SOCK_URGINLINE))
  				queued = !sock_queue_rcv_skb(sk, skb);
  			else {
  				skb_set_owner_r(skb, sk);
  				skb_queue_tail(&x25->interrupt_in_queue, skb);
  				queued = 1;
  			}
  			sk_send_sigurg(sk);
  			x25_write_internal(sk, X25_INTERRUPT_CONFIRMATION);
  			break;
  
  		default:
  			printk(KERN_WARNING "x25: unknown %02X in state 3
  ", frametype);
  			break;
  	}
  
  	return queued;
cb101ed2c   Matthew Daley   x25: Handle under...
319
320
321
322
323
324
  
  out_clear:
  	x25_write_internal(sk, X25_CLEAR_REQUEST);
  	x25->state = X25_STATE_2;
  	x25_start_t23timer(sk);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
325
326
327
328
329
330
331
332
333
  }
  
  /*
   * State machine for state 4, Awaiting Reset Confirmation State.
   * The handling of the timer(s) is in file x25_timer.c
   * Handling of state 0 and connection release is in af_x25.c.
   */
  static int x25_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype)
  {
cb101ed2c   Matthew Daley   x25: Handle under...
334
  	struct x25_sock *x25 = x25_sk(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335
336
337
338
339
  	switch (frametype) {
  
  		case X25_RESET_REQUEST:
  			x25_write_internal(sk, X25_RESET_CONFIRMATION);
  		case X25_RESET_CONFIRMATION: {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
340
341
342
343
344
345
346
347
348
349
350
  			x25_stop_timer(sk);
  			x25->condition = 0x00;
  			x25->va        = 0;
  			x25->vr        = 0;
  			x25->vs        = 0;
  			x25->vl        = 0;
  			x25->state     = X25_STATE_3;
  			x25_requeue_frames(sk);
  			break;
  		}
  		case X25_CLEAR_REQUEST:
cb101ed2c   Matthew Daley   x25: Handle under...
351
352
  			if (!pskb_may_pull(skb, X25_STD_MIN_LEN + 2))
  				goto out_clear;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353
354
355
356
357
358
359
360
361
  			x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
  			x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
  			break;
  
  		default:
  			break;
  	}
  
  	return 0;
cb101ed2c   Matthew Daley   x25: Handle under...
362
363
364
365
366
367
  
  out_clear:
  	x25_write_internal(sk, X25_CLEAR_REQUEST);
  	x25->state = X25_STATE_2;
  	x25_start_t23timer(sk);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
368
369
370
371
372
373
374
375
376
377
378
379
380
381
  }
  
  /* Higher level upcall for a LAPB frame */
  int x25_process_rx_frame(struct sock *sk, struct sk_buff *skb)
  {
  	struct x25_sock *x25 = x25_sk(sk);
  	int queued = 0, frametype, ns, nr, q, d, m;
  
  	if (x25->state == X25_STATE_0)
  		return 0;
  
  	frametype = x25_decode(sk, skb, &ns, &nr, &q, &d, &m);
  
  	switch (x25->state) {
fddc5f3e9   Joe Perches   x25: Reduce switc...
382
383
384
385
386
387
388
389
390
391
392
393
  	case X25_STATE_1:
  		queued = x25_state1_machine(sk, skb, frametype);
  		break;
  	case X25_STATE_2:
  		queued = x25_state2_machine(sk, skb, frametype);
  		break;
  	case X25_STATE_3:
  		queued = x25_state3_machine(sk, skb, frametype, ns, nr, q, d, m);
  		break;
  	case X25_STATE_4:
  		queued = x25_state4_machine(sk, skb, frametype);
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
  	}
  
  	x25_kick(sk);
  
  	return queued;
  }
  
  int x25_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  {
  	int queued = x25_process_rx_frame(sk, skb);
  
  	if (!queued)
  		kfree_skb(skb);
  
  	return 0;
  }