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