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) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
10 #include <linux/errno.h>
11 #include <linux/types.h>
12 #include <linux/socket.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/string.h>
17 #include <linux/sockios.h>
18 #include <linux/net.h>
20 #include <linux/inet.h>
21 #include <linux/netdevice.h>
22 #include <linux/skbuff.h>
24 #include <net/tcp_states.h>
25 #include <asm/uaccess.h>
26 #include <linux/fcntl.h>
28 #include <linux/interrupt.h>
31 * State machine for state 1, Awaiting Connection State.
32 * The handling of the timer(s) is in file ax25_ds_timer.c.
33 * Handling of state 0 and connection release is in ax25.c.
35 static int ax25_ds_state1_machine(ax25_cb
*ax25
, struct sk_buff
*skb
, int frametype
, int pf
, int type
)
39 ax25
->modulus
= AX25_MODULUS
;
40 ax25
->window
= ax25
->ax25_dev
->values
[AX25_VALUES_WINDOW
];
41 ax25_send_control(ax25
, AX25_UA
, pf
, AX25_RESPONSE
);
45 ax25
->modulus
= AX25_EMODULUS
;
46 ax25
->window
= ax25
->ax25_dev
->values
[AX25_VALUES_EWINDOW
];
47 ax25_send_control(ax25
, AX25_UA
, pf
, AX25_RESPONSE
);
51 ax25_send_control(ax25
, AX25_DM
, pf
, AX25_RESPONSE
);
55 ax25_calculate_rtt(ax25
);
56 ax25_stop_t1timer(ax25
);
57 ax25_start_t3timer(ax25
);
58 ax25_start_idletimer(ax25
);
62 ax25
->state
= AX25_STATE_3
;
64 if (ax25
->sk
!= NULL
) {
65 bh_lock_sock(ax25
->sk
);
66 ax25
->sk
->sk_state
= TCP_ESTABLISHED
;
68 * For WAIT_SABM connections we will produce an accept
71 if (!sock_flag(ax25
->sk
, SOCK_DEAD
))
72 ax25
->sk
->sk_state_change(ax25
->sk
);
73 bh_unlock_sock(ax25
->sk
);
77 /* according to DK4EG's spec we are required to
78 * send a RR RESPONSE FINAL NR=0.
81 ax25_std_enquiry_response(ax25
);
86 ax25_disconnect(ax25
, ECONNREFUSED
);
91 ax25_send_control(ax25
, AX25_SABM
, AX25_POLLON
, AX25_COMMAND
);
99 * State machine for state 2, Awaiting Release State.
100 * The handling of the timer(s) is in file ax25_ds_timer.c
101 * Handling of state 0 and connection release is in ax25.c.
103 static int ax25_ds_state2_machine(ax25_cb
*ax25
, struct sk_buff
*skb
, int frametype
, int pf
, int type
)
108 ax25_send_control(ax25
, AX25_DISC
, AX25_POLLON
, AX25_COMMAND
);
113 ax25_send_control(ax25
, AX25_UA
, pf
, AX25_RESPONSE
);
115 ax25_disconnect(ax25
, 0);
122 ax25_disconnect(ax25
, 0);
131 ax25_send_control(ax25
, AX25_DISC
, AX25_POLLON
, AX25_COMMAND
);
144 * State machine for state 3, Connected State.
145 * The handling of the timer(s) is in file ax25_timer.c
146 * Handling of state 0 and connection release is in ax25.c.
148 static int ax25_ds_state3_machine(ax25_cb
*ax25
, struct sk_buff
*skb
, int frametype
, int ns
, int nr
, int pf
, int type
)
155 if (frametype
== AX25_SABM
) {
156 ax25
->modulus
= AX25_MODULUS
;
157 ax25
->window
= ax25
->ax25_dev
->values
[AX25_VALUES_WINDOW
];
159 ax25
->modulus
= AX25_EMODULUS
;
160 ax25
->window
= ax25
->ax25_dev
->values
[AX25_VALUES_EWINDOW
];
162 ax25_send_control(ax25
, AX25_UA
, pf
, AX25_RESPONSE
);
163 ax25_stop_t1timer(ax25
);
164 ax25_start_t3timer(ax25
);
165 ax25_start_idletimer(ax25
);
166 ax25
->condition
= 0x00;
170 ax25_requeue_frames(ax25
);
175 ax25_send_control(ax25
, AX25_UA
, pf
, AX25_RESPONSE
);
177 ax25_disconnect(ax25
, 0);
182 ax25_disconnect(ax25
, ECONNRESET
);
187 if (frametype
== AX25_RR
)
188 ax25
->condition
&= ~AX25_COND_PEER_RX_BUSY
;
190 ax25
->condition
|= AX25_COND_PEER_RX_BUSY
;
192 if (ax25_validate_nr(ax25
, nr
)) {
193 if (ax25_check_iframes_acked(ax25
, nr
))
195 if (type
== AX25_COMMAND
&& pf
)
196 ax25_ds_enquiry_response(ax25
);
198 ax25_ds_nr_error_recovery(ax25
);
199 ax25
->state
= AX25_STATE_1
;
204 ax25
->condition
&= ~AX25_COND_PEER_RX_BUSY
;
206 if (ax25_validate_nr(ax25
, nr
)) {
210 ax25_frames_acked(ax25
, nr
);
211 ax25_calculate_rtt(ax25
);
212 ax25_stop_t1timer(ax25
);
213 ax25_start_t3timer(ax25
);
214 ax25_requeue_frames(ax25
);
216 if (type
== AX25_COMMAND
&& pf
)
217 ax25_ds_enquiry_response(ax25
);
219 ax25_ds_nr_error_recovery(ax25
);
220 ax25
->state
= AX25_STATE_1
;
225 if (!ax25_validate_nr(ax25
, nr
)) {
226 ax25_ds_nr_error_recovery(ax25
);
227 ax25
->state
= AX25_STATE_1
;
230 if (ax25
->condition
& AX25_COND_PEER_RX_BUSY
) {
231 ax25_frames_acked(ax25
, nr
);
234 if (ax25_check_iframes_acked(ax25
, nr
))
237 if (ax25
->condition
& AX25_COND_OWN_RX_BUSY
) {
238 if (pf
) ax25_ds_enquiry_response(ax25
);
241 if (ns
== ax25
->vr
) {
242 ax25
->vr
= (ax25
->vr
+ 1) % ax25
->modulus
;
243 queued
= ax25_rx_iframe(ax25
, skb
);
244 if (ax25
->condition
& AX25_COND_OWN_RX_BUSY
)
245 ax25
->vr
= ns
; /* ax25->vr - 1 */
246 ax25
->condition
&= ~AX25_COND_REJECT
;
248 ax25_ds_enquiry_response(ax25
);
250 if (!(ax25
->condition
& AX25_COND_ACK_PENDING
)) {
251 ax25
->condition
|= AX25_COND_ACK_PENDING
;
252 ax25_start_t2timer(ax25
);
256 if (ax25
->condition
& AX25_COND_REJECT
) {
257 if (pf
) ax25_ds_enquiry_response(ax25
);
259 ax25
->condition
|= AX25_COND_REJECT
;
260 ax25_ds_enquiry_response(ax25
);
261 ax25
->condition
&= ~AX25_COND_ACK_PENDING
;
268 ax25_ds_establish_data_link(ax25
);
269 ax25
->state
= AX25_STATE_1
;
280 * Higher level upcall for a LAPB frame
282 int ax25_ds_frame_in(ax25_cb
*ax25
, struct sk_buff
*skb
, int type
)
284 int queued
= 0, frametype
, ns
, nr
, pf
;
286 frametype
= ax25_decode(ax25
, skb
, &ns
, &nr
, &pf
);
288 switch (ax25
->state
) {
290 queued
= ax25_ds_state1_machine(ax25
, skb
, frametype
, pf
, type
);
293 queued
= ax25_ds_state2_machine(ax25
, skb
, frametype
, pf
, type
);
296 queued
= ax25_ds_state3_machine(ax25
, skb
, frametype
, ns
, nr
, pf
, type
);