2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
8 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
9 * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
10 * Copyright (C) Hans-Joachim Hetscher DD8NE (dd8ne@bnv-bamberg.de)
12 * Most of this code is based on the SDL diagrams published in the 7th ARRL
13 * Computer Networking Conference papers. The diagrams have mistakes in them,
14 * but are mostly correct. Before you modify the code could you read the SDL
15 * diagrams as the code is not obvious and probably very easy to break.
17 #include <linux/errno.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/timer.h>
24 #include <linux/string.h>
25 #include <linux/sockios.h>
26 #include <linux/net.h>
28 #include <linux/inet.h>
29 #include <linux/netdevice.h>
30 #include <linux/skbuff.h>
32 #include <net/ip.h> /* For ip_rcv */
34 #include <asm/uaccess.h>
35 #include <asm/system.h>
36 #include <linux/fcntl.h>
38 #include <linux/interrupt.h>
41 * State machine for state 1, Awaiting Connection State.
42 * The handling of the timer(s) is in file ax25_std_timer.c.
43 * Handling of state 0 and connection release is in ax25.c.
45 static int ax25_std_state1_machine(ax25_cb
*ax25
, struct sk_buff
*skb
, int frametype
, int pf
, int type
)
49 ax25
->modulus
= AX25_MODULUS
;
50 ax25
->window
= ax25
->ax25_dev
->values
[AX25_VALUES_WINDOW
];
51 ax25_send_control(ax25
, AX25_UA
, pf
, AX25_RESPONSE
);
55 ax25
->modulus
= AX25_EMODULUS
;
56 ax25
->window
= ax25
->ax25_dev
->values
[AX25_VALUES_EWINDOW
];
57 ax25_send_control(ax25
, AX25_UA
, pf
, AX25_RESPONSE
);
61 ax25_send_control(ax25
, AX25_DM
, pf
, AX25_RESPONSE
);
66 ax25_calculate_rtt(ax25
);
67 ax25_stop_t1timer(ax25
);
68 ax25_start_t3timer(ax25
);
69 ax25_start_idletimer(ax25
);
73 ax25
->state
= AX25_STATE_3
;
75 if (ax25
->sk
!= NULL
) {
76 bh_lock_sock(ax25
->sk
);
77 ax25
->sk
->sk_state
= TCP_ESTABLISHED
;
78 /* For WAIT_SABM connections we will produce an accept ready socket here */
79 if (!sock_flag(ax25
->sk
, SOCK_DEAD
))
80 ax25
->sk
->sk_state_change(ax25
->sk
);
81 bh_unlock_sock(ax25
->sk
);
88 if (ax25
->modulus
== AX25_MODULUS
) {
89 ax25_disconnect(ax25
, ECONNREFUSED
);
91 ax25
->modulus
= AX25_MODULUS
;
92 ax25
->window
= ax25
->ax25_dev
->values
[AX25_VALUES_WINDOW
];
105 * State machine for state 2, Awaiting Release State.
106 * The handling of the timer(s) is in file ax25_std_timer.c
107 * Handling of state 0 and connection release is in ax25.c.
109 static int ax25_std_state2_machine(ax25_cb
*ax25
, struct sk_buff
*skb
, int frametype
, int pf
, int type
)
114 ax25_send_control(ax25
, AX25_DM
, pf
, AX25_RESPONSE
);
118 ax25_send_control(ax25
, AX25_UA
, pf
, AX25_RESPONSE
);
119 ax25_disconnect(ax25
, 0);
125 ax25_disconnect(ax25
, 0);
132 if (pf
) ax25_send_control(ax25
, AX25_DM
, AX25_POLLON
, AX25_RESPONSE
);
143 * State machine for state 3, Connected State.
144 * The handling of the timer(s) is in file ax25_std_timer.c
145 * Handling of state 0 and connection release is in ax25.c.
147 static int ax25_std_state3_machine(ax25_cb
*ax25
, struct sk_buff
*skb
, int frametype
, int ns
, int nr
, int pf
, int type
)
154 if (frametype
== AX25_SABM
) {
155 ax25
->modulus
= AX25_MODULUS
;
156 ax25
->window
= ax25
->ax25_dev
->values
[AX25_VALUES_WINDOW
];
158 ax25
->modulus
= AX25_EMODULUS
;
159 ax25
->window
= ax25
->ax25_dev
->values
[AX25_VALUES_EWINDOW
];
161 ax25_send_control(ax25
, AX25_UA
, pf
, AX25_RESPONSE
);
162 ax25_stop_t1timer(ax25
);
163 ax25_stop_t2timer(ax25
);
164 ax25_start_t3timer(ax25
);
165 ax25_start_idletimer(ax25
);
166 ax25
->condition
= 0x00;
170 ax25_requeue_frames(ax25
);
174 ax25_send_control(ax25
, AX25_UA
, pf
, AX25_RESPONSE
);
175 ax25_disconnect(ax25
, 0);
179 ax25_disconnect(ax25
, ECONNRESET
);
184 if (frametype
== AX25_RR
)
185 ax25
->condition
&= ~AX25_COND_PEER_RX_BUSY
;
187 ax25
->condition
|= AX25_COND_PEER_RX_BUSY
;
188 if (type
== AX25_COMMAND
&& pf
)
189 ax25_std_enquiry_response(ax25
);
190 if (ax25_validate_nr(ax25
, nr
)) {
191 ax25_check_iframes_acked(ax25
, nr
);
193 ax25_std_nr_error_recovery(ax25
);
194 ax25
->state
= AX25_STATE_1
;
199 ax25
->condition
&= ~AX25_COND_PEER_RX_BUSY
;
200 if (type
== AX25_COMMAND
&& pf
)
201 ax25_std_enquiry_response(ax25
);
202 if (ax25_validate_nr(ax25
, nr
)) {
203 ax25_frames_acked(ax25
, nr
);
204 ax25_calculate_rtt(ax25
);
205 ax25_stop_t1timer(ax25
);
206 ax25_start_t3timer(ax25
);
207 ax25_requeue_frames(ax25
);
209 ax25_std_nr_error_recovery(ax25
);
210 ax25
->state
= AX25_STATE_1
;
215 if (!ax25_validate_nr(ax25
, nr
)) {
216 ax25_std_nr_error_recovery(ax25
);
217 ax25
->state
= AX25_STATE_1
;
220 if (ax25
->condition
& AX25_COND_PEER_RX_BUSY
) {
221 ax25_frames_acked(ax25
, nr
);
223 ax25_check_iframes_acked(ax25
, nr
);
225 if (ax25
->condition
& AX25_COND_OWN_RX_BUSY
) {
226 if (pf
) ax25_std_enquiry_response(ax25
);
229 if (ns
== ax25
->vr
) {
230 ax25
->vr
= (ax25
->vr
+ 1) % ax25
->modulus
;
231 queued
= ax25_rx_iframe(ax25
, skb
);
232 if (ax25
->condition
& AX25_COND_OWN_RX_BUSY
)
233 ax25
->vr
= ns
; /* ax25->vr - 1 */
234 ax25
->condition
&= ~AX25_COND_REJECT
;
236 ax25_std_enquiry_response(ax25
);
238 if (!(ax25
->condition
& AX25_COND_ACK_PENDING
)) {
239 ax25
->condition
|= AX25_COND_ACK_PENDING
;
240 ax25_start_t2timer(ax25
);
244 if (ax25
->condition
& AX25_COND_REJECT
) {
245 if (pf
) ax25_std_enquiry_response(ax25
);
247 ax25
->condition
|= AX25_COND_REJECT
;
248 ax25_send_control(ax25
, AX25_REJ
, pf
, AX25_RESPONSE
);
249 ax25
->condition
&= ~AX25_COND_ACK_PENDING
;
256 ax25_std_establish_data_link(ax25
);
257 ax25
->state
= AX25_STATE_1
;
268 * State machine for state 4, Timer Recovery State.
269 * The handling of the timer(s) is in file ax25_std_timer.c
270 * Handling of state 0 and connection release is in ax25.c.
272 static int ax25_std_state4_machine(ax25_cb
*ax25
, struct sk_buff
*skb
, int frametype
, int ns
, int nr
, int pf
, int type
)
279 if (frametype
== AX25_SABM
) {
280 ax25
->modulus
= AX25_MODULUS
;
281 ax25
->window
= ax25
->ax25_dev
->values
[AX25_VALUES_WINDOW
];
283 ax25
->modulus
= AX25_EMODULUS
;
284 ax25
->window
= ax25
->ax25_dev
->values
[AX25_VALUES_EWINDOW
];
286 ax25_send_control(ax25
, AX25_UA
, pf
, AX25_RESPONSE
);
287 ax25_stop_t1timer(ax25
);
288 ax25_stop_t2timer(ax25
);
289 ax25_start_t3timer(ax25
);
290 ax25_start_idletimer(ax25
);
291 ax25
->condition
= 0x00;
295 ax25
->state
= AX25_STATE_3
;
297 ax25_requeue_frames(ax25
);
301 ax25_send_control(ax25
, AX25_UA
, pf
, AX25_RESPONSE
);
302 ax25_disconnect(ax25
, 0);
306 ax25_disconnect(ax25
, ECONNRESET
);
311 if (frametype
== AX25_RR
)
312 ax25
->condition
&= ~AX25_COND_PEER_RX_BUSY
;
314 ax25
->condition
|= AX25_COND_PEER_RX_BUSY
;
315 if (type
== AX25_RESPONSE
&& pf
) {
316 ax25_stop_t1timer(ax25
);
318 if (ax25_validate_nr(ax25
, nr
)) {
319 ax25_frames_acked(ax25
, nr
);
320 if (ax25
->vs
== ax25
->va
) {
321 ax25_start_t3timer(ax25
);
322 ax25
->state
= AX25_STATE_3
;
324 ax25_requeue_frames(ax25
);
327 ax25_std_nr_error_recovery(ax25
);
328 ax25
->state
= AX25_STATE_1
;
332 if (type
== AX25_COMMAND
&& pf
)
333 ax25_std_enquiry_response(ax25
);
334 if (ax25_validate_nr(ax25
, nr
)) {
335 ax25_frames_acked(ax25
, nr
);
337 ax25_std_nr_error_recovery(ax25
);
338 ax25
->state
= AX25_STATE_1
;
343 ax25
->condition
&= ~AX25_COND_PEER_RX_BUSY
;
344 if (pf
&& type
== AX25_RESPONSE
) {
345 ax25_stop_t1timer(ax25
);
347 if (ax25_validate_nr(ax25
, nr
)) {
348 ax25_frames_acked(ax25
, nr
);
349 if (ax25
->vs
== ax25
->va
) {
350 ax25_start_t3timer(ax25
);
351 ax25
->state
= AX25_STATE_3
;
353 ax25_requeue_frames(ax25
);
356 ax25_std_nr_error_recovery(ax25
);
357 ax25
->state
= AX25_STATE_1
;
361 if (type
== AX25_COMMAND
&& pf
)
362 ax25_std_enquiry_response(ax25
);
363 if (ax25_validate_nr(ax25
, nr
)) {
364 ax25_frames_acked(ax25
, nr
);
365 ax25_requeue_frames(ax25
);
367 ax25_std_nr_error_recovery(ax25
);
368 ax25
->state
= AX25_STATE_1
;
373 if (!ax25_validate_nr(ax25
, nr
)) {
374 ax25_std_nr_error_recovery(ax25
);
375 ax25
->state
= AX25_STATE_1
;
378 ax25_frames_acked(ax25
, nr
);
379 if (ax25
->condition
& AX25_COND_OWN_RX_BUSY
) {
381 ax25_std_enquiry_response(ax25
);
384 if (ns
== ax25
->vr
) {
385 ax25
->vr
= (ax25
->vr
+ 1) % ax25
->modulus
;
386 queued
= ax25_rx_iframe(ax25
, skb
);
387 if (ax25
->condition
& AX25_COND_OWN_RX_BUSY
)
388 ax25
->vr
= ns
; /* ax25->vr - 1 */
389 ax25
->condition
&= ~AX25_COND_REJECT
;
391 ax25_std_enquiry_response(ax25
);
393 if (!(ax25
->condition
& AX25_COND_ACK_PENDING
)) {
394 ax25
->condition
|= AX25_COND_ACK_PENDING
;
395 ax25_start_t2timer(ax25
);
399 if (ax25
->condition
& AX25_COND_REJECT
) {
400 if (pf
) ax25_std_enquiry_response(ax25
);
402 ax25
->condition
|= AX25_COND_REJECT
;
403 ax25_send_control(ax25
, AX25_REJ
, pf
, AX25_RESPONSE
);
404 ax25
->condition
&= ~AX25_COND_ACK_PENDING
;
411 ax25_std_establish_data_link(ax25
);
412 ax25
->state
= AX25_STATE_1
;
423 * Higher level upcall for a LAPB frame
425 int ax25_std_frame_in(ax25_cb
*ax25
, struct sk_buff
*skb
, int type
)
427 int queued
= 0, frametype
, ns
, nr
, pf
;
429 frametype
= ax25_decode(ax25
, skb
, &ns
, &nr
, &pf
);
431 switch (ax25
->state
) {
433 queued
= ax25_std_state1_machine(ax25
, skb
, frametype
, pf
, type
);
436 queued
= ax25_std_state2_machine(ax25
, skb
, frametype
, pf
, type
);
439 queued
= ax25_std_state3_machine(ax25
, skb
, frametype
, ns
, nr
, pf
, type
);
442 queued
= ax25_std_state4_machine(ax25
, skb
, frametype
, ns
, nr
, pf
, type
);