1 /*
2 * AX.25 release 031
3 *
4 * This is ALPHA test software. This code may break your machine, randomly fail to work with new
5 * releases, misbehave and/or generally screw up. It might even work.
6 *
7 * This code REQUIRES 1.2.1 or higher/ NET3.029
8 *
9 * This module:
10 * This module is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * History
16 * AX.25 028a Jonathan(G4KLX) New state machine based on SDL diagrams.
17 * AX.25 028b Jonathan(G4KLX) Extracted AX25 control block from the
18 * sock structure.
19 * AX.25 029 Alan(GW4PTS) Switched to KA9Q constant names.
20 * AX.25 031 Joerg(DL1BKE) Added DAMA support
21 */
22
23 #include <linux/config.h>
24 #ifdef CONFIG_AX25
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 <asm/segment.h>
41 #include <asm/system.h>
42 #include <linux/fcntl.h>
43 #include <linux/mm.h>
44 #include <linux/interrupt.h>
45 #ifdef CONFIG_NETROM
46 #include <net/netrom.h>
47 #endif
48
49 static void ax25_timer(unsigned long);
50
51 /*
52 * Linux set/reset timer routines
53 */
54 void ax25_set_timer(ax25_cb *ax25)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
55 {
56 unsigned long flags;
57
58 save_flags(flags);
59 cli();
60 del_timer(&ax25->timer);
61 restore_flags(flags);
62
63 ax25->timer.next = ax25->timer.prev = NULL;
64 ax25->timer.data = (unsigned long)ax25;
65 ax25->timer.function = &ax25_timer;
66
67 ax25->timer.expires = jiffies + 10;
68 add_timer(&ax25->timer);
69 }
70
71 static void ax25_reset_timer(ax25_cb *ax25)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
72 {
73 unsigned long flags;
74
75 save_flags(flags);
76 cli();
77 del_timer(&ax25->timer);
78 restore_flags(flags);
79
80 ax25->timer.data = (unsigned long)ax25;
81 ax25->timer.function = &ax25_timer;
82 ax25->timer.expires = jiffies + 10;
83 add_timer(&ax25->timer);
84 }
85
86 /*
87 * AX.25 TIMER
88 *
89 * This routine is called every 500ms. Decrement timer by this
90 * amount - if expired then process the event.
91 */
92 static void ax25_timer(unsigned long param)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
93 {
94 ax25_cb *ax25 = (ax25_cb *)param;
95
96 switch (ax25->state) {
97 case AX25_STATE_0:
98 /* Magic here: If we listen() and a new link dies before it
99 is accepted() it isnt 'dead' so doesnt get removed. */
100 if (ax25->sk == NULL || ax25->sk->destroy || (ax25->sk->state == TCP_LISTEN && ax25->sk->dead)) {
101 del_timer(&ax25->timer);
102 ax25_destroy_socket(ax25);
103 return;
104 }
105 break;
106
107 case AX25_STATE_3:
108 case AX25_STATE_4:
109 /*
110 * Check the state of the receive buffer.
111 */
112 if (ax25->sk != NULL) {
113 if (ax25->sk->rmem_alloc < (ax25->sk->rcvbuf / 2) && (ax25->condition & OWN_RX_BUSY_CONDITION)) {
114 ax25->condition &= ~OWN_RX_BUSY_CONDITION;
115 if (!ax25->dama_slave) /* dl1bke */
116 ax25_send_control(ax25, RR, POLLOFF, C_RESPONSE);
117 ax25->condition &= ~ACK_PENDING_CONDITION;
118 break;
119 }
120 }
121 /*
122 * Check for frames to transmit.
123 */
124 if (!ax25->dama_slave)
125 ax25_kick(ax25); /* dl1bke 960114 */
126 break;
127
128 default:
129 break;
130 }
131
132 if (ax25->t2timer > 0 && --ax25->t2timer == 0) {
133 if (ax25->state == AX25_STATE_3 || ax25->state == AX25_STATE_4) {
134 if (ax25->condition & ACK_PENDING_CONDITION) {
135 ax25->condition &= ~ACK_PENDING_CONDITION;
136 if (!ax25->dama_slave) /* dl1bke 960114 */
137 ax25_timeout_response(ax25);
138 }
139 }
140 }
141
142 if (ax25->t3timer > 0 && --ax25->t3timer == 0) {
143 /* dl1bke 960114: T3 expires and we are in DAMA mode: */
144 /* send a DISC and abort the connection */
145 if (ax25->dama_slave) {
146 #ifdef CONFIG_NETROM
147 nr_link_failed(&ax25->dest_addr, ax25->device);
148 #endif
149 ax25_clear_queues(ax25);
150 ax25_send_control(ax25, DISC, POLLON, C_COMMAND);
151
152 ax25->state = AX25_STATE_0;
153 if (ax25->sk != NULL) {
154 if (ax25->sk->debug)
155 printk("T3 Timeout\n");
156 ax25->sk->state = TCP_CLOSE;
157 ax25->sk->err = ETIMEDOUT;
158 if (!ax25->sk->dead)
159 ax25->sk->state_change(ax25->sk);
160 ax25->sk->dead = 1;
161 }
162
163 ax25_reset_timer(ax25);
164 return;
165 }
166
167 if (ax25->state == AX25_STATE_3) {
168 ax25->n2count = 0;
169 ax25_transmit_enquiry(ax25);
170 ax25->state = AX25_STATE_4;
171 }
172 ax25->t3timer = ax25->t3;
173 }
174
175 if (ax25->idletimer > 0 && --ax25->idletimer == 0) {
176 /* dl1bke 960228: close the connection when IDLE expires */
177 /* similar to DAMA T3 timeout but with */
178 /* a "clean" disconnect of the connection */
179
180 ax25_clear_queues(ax25);
181
182 ax25->n2count = 0;
183 if (!ax25->dama_slave)
184 ax25_send_control(ax25, DISC, POLLON, C_COMMAND);
185
186 /* state 1 or 2 should not happen, but... */
187
188 if (ax25->state == AX25_STATE_1 || ax25->state == AX25_STATE_2)
189 ax25->state = AX25_STATE_0;
190 else
191 ax25->state = AX25_STATE_2;
192
193 ax25->t3timer = 0;
194 ax25->t1timer = ax25->t1 = ax25_calculate_t1(ax25);
195
196 if (ax25->sk != NULL)
197 {
198 ax25->sk->state = TCP_CLOSE;
199 ax25->sk->err = 0;
200 if (!ax25->sk->dead)
201 ax25->sk->state_change(ax25->sk);
202 ax25->sk->dead = 1;
203 ax25->sk->destroy = 1;
204 }
205 }
206
207
208 /* dl1bke 960114: DAMA T1 timeouts are handled in ax25_dama_slave_transmit */
209 /* nevertheless we have to re-enqueue the timer struct... */
210
211 if (ax25->t1timer == 0 || --ax25->t1timer > 0) {
212 ax25_reset_timer(ax25);
213 return;
214 }
215
216 if (!ax25_dev_is_dama_slave(ax25->device)) {
217 if (ax25->dama_slave)
218 ax25->dama_slave = 0;
219 ax25_t1_timeout(ax25);
220 }
221 }
222
223
224 /* dl1bke 960114: The DAMA protocol requires to send data and SABM/DISC
225 * within the poll of any connected channel. Remember
226 * that we are not allowed to send anything unless we
227 * get polled by the Master.
228 *
229 * Thus we'll have to do parts of our T1 handling in
230 * ax25_enquiry_response().
231 */
232 void ax25_t1_timeout(ax25_cb * ax25)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
233 {
234 switch (ax25->state) {
235 case AX25_STATE_1:
236 if (ax25->n2count == ax25->n2) {
237 if (ax25->modulus == MODULUS) {
238 #ifdef CONFIG_NETROM
239 nr_link_failed(&ax25->dest_addr, ax25->device);
240 #endif
241 ax25_clear_queues(ax25);
242 ax25->state = AX25_STATE_0;
243 if (ax25->sk != NULL) {
244 ax25->sk->state = TCP_CLOSE;
245 ax25->sk->err = ETIMEDOUT;
246 if (!ax25->sk->dead)
247 ax25->sk->state_change(ax25->sk);
248 ax25->sk->dead = 1;
249 }
250 } else {
251 ax25->modulus = MODULUS;
252 ax25->window = ax25_dev_get_value(ax25->device, AX25_VALUES_WINDOW);
253 ax25->n2count = 0;
254 ax25_send_control(ax25, SABM, ax25_dev_is_dama_slave(ax25->device)? POLLOFF : POLLON, C_COMMAND);
255 }
256 } else {
257 ax25->n2count++;
258 if (ax25->modulus == MODULUS) {
259 ax25_send_control(ax25, SABM, ax25_dev_is_dama_slave(ax25->device)? POLLOFF : POLLON, C_COMMAND);
260 } else {
261 ax25_send_control(ax25, SABME, ax25_dev_is_dama_slave(ax25->device)? POLLOFF : POLLON, C_COMMAND);
262 }
263 }
264 break;
265
266 case AX25_STATE_2:
267 if (ax25->n2count == ax25->n2) {
268 #ifdef CONFIG_NETROM
269 nr_link_failed(&ax25->dest_addr, ax25->device);
270 #endif
271 ax25_clear_queues(ax25);
272 ax25->state = AX25_STATE_0;
273 ax25_send_control(ax25, DISC, POLLON, C_COMMAND); /* dl1bke */
274
275 if (ax25->sk != NULL) {
276 ax25->sk->state = TCP_CLOSE;
277 ax25->sk->err = ETIMEDOUT;
278 if (!ax25->sk->dead)
279 ax25->sk->state_change(ax25->sk);
280 ax25->sk->dead = 1;
281 }
282 } else {
283 ax25->n2count++;
284 if (!ax25_dev_is_dama_slave(ax25->device)) /* dl1bke */
285 ax25_send_control(ax25, DISC, POLLON, C_COMMAND);
286 }
287 break;
288
289 case AX25_STATE_3:
290 ax25->n2count = 1;
291 if (!ax25->dama_slave) /* dl1bke 960114 */
292 ax25_transmit_enquiry(ax25);
293 ax25->state = AX25_STATE_4;
294 break;
295
296 case AX25_STATE_4:
297 if (ax25->n2count == ax25->n2) {
298 #ifdef CONFIG_NETROM
299 nr_link_failed(&ax25->dest_addr, ax25->device);
300 #endif
301 ax25_clear_queues(ax25);
302 ax25_send_control(ax25, DM, POLLON, C_RESPONSE);
303 ax25->state = AX25_STATE_0;
304 if (ax25->sk != NULL) {
305 if (ax25->sk->debug)
306 printk("Link Failure\n");
307 ax25->sk->state = TCP_CLOSE;
308 ax25->sk->err = ETIMEDOUT;
309 if (!ax25->sk->dead)
310 ax25->sk->state_change(ax25->sk);
311 ax25->sk->dead = 1;
312 }
313 } else {
314 ax25->n2count++;
315 if (!ax25->dama_slave) /* dl1bke 960114 */
316 ax25_transmit_enquiry(ax25);
317 }
318 break;
319 }
320
321 ax25->t1timer = ax25->t1 = ax25_calculate_t1(ax25);
322
323 ax25_set_timer(ax25);
324 }
325
326 #endif