ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / ax25 / ax25_ds_in.c
1 /*
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.
6  *
7  * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8  * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
9  */
10 #include <linux/errno.h>
11 #include <linux/types.h>
12 #include <linux/socket.h>
13 #include <linux/in.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/timer.h>
17 #include <linux/string.h>
18 #include <linux/sockios.h>
19 #include <linux/net.h>
20 #include <net/ax25.h>
21 #include <linux/inet.h>
22 #include <linux/netdevice.h>
23 #include <linux/skbuff.h>
24 #include <net/sock.h>
25 #include <net/ip.h>                     /* For ip_rcv */
26 #include <net/tcp.h>
27 #include <asm/uaccess.h>
28 #include <asm/system.h>
29 #include <linux/fcntl.h>
30 #include <linux/mm.h>
31 #include <linux/interrupt.h>
32
33 /*
34  *      State machine for state 1, Awaiting Connection State.
35  *      The handling of the timer(s) is in file ax25_ds_timer.c.
36  *      Handling of state 0 and connection release is in ax25.c.
37  */
38 static int ax25_ds_state1_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int pf, int type)
39 {
40         switch (frametype) {
41         case AX25_SABM:
42                 ax25->modulus = AX25_MODULUS;
43                 ax25->window  = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
44                 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
45                 break;
46
47         case AX25_SABME:
48                 ax25->modulus = AX25_EMODULUS;
49                 ax25->window  =  ax25->ax25_dev->values[AX25_VALUES_EWINDOW];
50                 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
51                 break;
52
53         case AX25_DISC:
54                 ax25_send_control(ax25, AX25_DM, pf, AX25_RESPONSE);
55                 break;
56
57         case AX25_UA:
58                 ax25_calculate_rtt(ax25);
59                 ax25_stop_t1timer(ax25);
60                 ax25_start_t3timer(ax25);
61                 ax25_start_idletimer(ax25);
62                 ax25->vs      = 0;
63                 ax25->va      = 0;
64                 ax25->vr      = 0;
65                 ax25->state   = AX25_STATE_3;
66                 ax25->n2count = 0;
67                 if (ax25->sk != NULL) {
68                         bh_lock_sock(ax25->sk);
69                         ax25->sk->sk_state = TCP_ESTABLISHED;
70                         /*
71                          * For WAIT_SABM connections we will produce an accept
72                          * ready socket here
73                          */
74                         if (!sock_flag(ax25->sk, SOCK_DEAD))
75                                 ax25->sk->sk_state_change(ax25->sk);
76                         bh_unlock_sock(ax25->sk);
77                 }
78                 ax25_dama_on(ax25);
79
80                 /* according to DK4EG´s spec we are required to
81                  * send a RR RESPONSE FINAL NR=0.
82                  */
83
84                 ax25_std_enquiry_response(ax25);
85                 break;
86
87         case AX25_DM:
88                 if (pf)
89                         ax25_disconnect(ax25, ECONNREFUSED);
90                 break;
91
92         default:
93                 if (pf)
94                         ax25_send_control(ax25, AX25_SABM, AX25_POLLON, AX25_COMMAND);
95                 break;
96         }
97
98         return 0;
99 }
100
101 /*
102  *      State machine for state 2, Awaiting Release State.
103  *      The handling of the timer(s) is in file ax25_ds_timer.c
104  *      Handling of state 0 and connection release is in ax25.c.
105  */
106 static int ax25_ds_state2_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int pf, int type)
107 {
108         switch (frametype) {
109         case AX25_SABM:
110         case AX25_SABME:
111                 ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
112                 ax25_dama_off(ax25);
113                 break;
114
115         case AX25_DISC:
116                 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
117                 ax25_dama_off(ax25);
118                 ax25_disconnect(ax25, 0);
119                 break;
120
121         case AX25_DM:
122         case AX25_UA:
123                 if (pf) {
124                         ax25_dama_off(ax25);
125                         ax25_disconnect(ax25, 0);
126                 }
127                 break;
128
129         case AX25_I:
130         case AX25_REJ:
131         case AX25_RNR:
132         case AX25_RR:
133                 if (pf) {
134                         ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
135                         ax25_dama_off(ax25);
136                 }
137                 break;
138
139         default:
140                 break;
141         }
142
143         return 0;
144 }
145
146 /*
147  *      State machine for state 3, Connected State.
148  *      The handling of the timer(s) is in file ax25_timer.c
149  *      Handling of state 0 and connection release is in ax25.c.
150  */
151 static int ax25_ds_state3_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int ns, int nr, int pf, int type)
152 {
153         int queued = 0;
154
155         switch (frametype) {
156         case AX25_SABM:
157         case AX25_SABME:
158                 if (frametype == AX25_SABM) {
159                         ax25->modulus   = AX25_MODULUS;
160                         ax25->window    = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
161                 } else {
162                         ax25->modulus   = AX25_EMODULUS;
163                         ax25->window    = ax25->ax25_dev->values[AX25_VALUES_EWINDOW];
164                 }
165                 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
166                 ax25_stop_t1timer(ax25);
167                 ax25_start_t3timer(ax25);
168                 ax25_start_idletimer(ax25);
169                 ax25->condition = 0x00;
170                 ax25->vs        = 0;
171                 ax25->va        = 0;
172                 ax25->vr        = 0;
173                 ax25_requeue_frames(ax25);
174                 ax25_dama_on(ax25);
175                 break;
176
177         case AX25_DISC:
178                 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
179                 ax25_dama_off(ax25);
180                 ax25_disconnect(ax25, 0);
181                 break;
182
183         case AX25_DM:
184                 ax25_dama_off(ax25);
185                 ax25_disconnect(ax25, ECONNRESET);
186                 break;
187
188         case AX25_RR:
189         case AX25_RNR:
190                 if (frametype == AX25_RR)
191                         ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
192                 else
193                         ax25->condition |= AX25_COND_PEER_RX_BUSY;
194
195                 if (ax25_validate_nr(ax25, nr)) {
196                         if (ax25_check_iframes_acked(ax25, nr))
197                                 ax25->n2count=0;
198                         if (type == AX25_COMMAND && pf)
199                                 ax25_ds_enquiry_response(ax25);
200                 } else {
201                         ax25_ds_nr_error_recovery(ax25);
202                         ax25->state = AX25_STATE_1;
203                 }
204                 break;
205
206         case AX25_REJ:
207                 ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
208
209                 if (ax25_validate_nr(ax25, nr)) {
210                         if (ax25->va != nr)
211                                 ax25->n2count=0;
212
213                         ax25_frames_acked(ax25, nr);
214                         ax25_calculate_rtt(ax25);
215                         ax25_stop_t1timer(ax25);
216                         ax25_start_t3timer(ax25);
217                         ax25_requeue_frames(ax25);
218
219                         if (type == AX25_COMMAND && pf)
220                                 ax25_ds_enquiry_response(ax25);
221                 } else {
222                         ax25_ds_nr_error_recovery(ax25);
223                         ax25->state = AX25_STATE_1;
224                 }
225                 break;
226
227         case AX25_I:
228                 if (!ax25_validate_nr(ax25, nr)) {
229                         ax25_ds_nr_error_recovery(ax25);
230                         ax25->state = AX25_STATE_1;
231                         break;
232                 }
233                 if (ax25->condition & AX25_COND_PEER_RX_BUSY) {
234                         ax25_frames_acked(ax25, nr);
235                         ax25->n2count = 0;
236                 } else {
237                         if (ax25_check_iframes_acked(ax25, nr))
238                                 ax25->n2count = 0;
239                 }
240                 if (ax25->condition & AX25_COND_OWN_RX_BUSY) {
241                         if (pf) ax25_ds_enquiry_response(ax25);
242                         break;
243                 }
244                 if (ns == ax25->vr) {
245                         ax25->vr = (ax25->vr + 1) % ax25->modulus;
246                         queued = ax25_rx_iframe(ax25, skb);
247                         if (ax25->condition & AX25_COND_OWN_RX_BUSY)
248                                 ax25->vr = ns;  /* ax25->vr - 1 */
249                         ax25->condition &= ~AX25_COND_REJECT;
250                         if (pf) {
251                                 ax25_ds_enquiry_response(ax25);
252                         } else {
253                                 if (!(ax25->condition & AX25_COND_ACK_PENDING)) {
254                                         ax25->condition |= AX25_COND_ACK_PENDING;
255                                         ax25_start_t2timer(ax25);
256                                 }
257                         }
258                 } else {
259                         if (ax25->condition & AX25_COND_REJECT) {
260                                 if (pf) ax25_ds_enquiry_response(ax25);
261                         } else {
262                                 ax25->condition |= AX25_COND_REJECT;
263                                 ax25_ds_enquiry_response(ax25);
264                                 ax25->condition &= ~AX25_COND_ACK_PENDING;
265                         }
266                 }
267                 break;
268
269         case AX25_FRMR:
270         case AX25_ILLEGAL:
271                 ax25_ds_establish_data_link(ax25);
272                 ax25->state = AX25_STATE_1;
273                 break;
274
275         default:
276                 break;
277         }
278
279         return queued;
280 }
281
282 /*
283  *      Higher level upcall for a LAPB frame
284  */
285 int ax25_ds_frame_in(ax25_cb *ax25, struct sk_buff *skb, int type)
286 {
287         int queued = 0, frametype, ns, nr, pf;
288
289         frametype = ax25_decode(ax25, skb, &ns, &nr, &pf);
290
291         switch (ax25->state) {
292         case AX25_STATE_1:
293                 queued = ax25_ds_state1_machine(ax25, skb, frametype, pf, type);
294                 break;
295         case AX25_STATE_2:
296                 queued = ax25_ds_state2_machine(ax25, skb, frametype, pf, type);
297                 break;
298         case AX25_STATE_3:
299                 queued = ax25_ds_state3_machine(ax25, skb, frametype, ns, nr, pf, type);
300                 break;
301         }
302
303         return queued;
304 }
305