4 * This code REQUIRES 2.1.15 or higher/ NET3.038
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.
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
19 * AX.25 028a Jonathan(G4KLX) New state machine based on SDL diagrams.
20 * AX.25 028b Jonathan(G4KLX) Extracted AX25 control block from
22 * AX.25 029 Alan(GW4PTS) Switched to KA9Q constant names.
23 * Jonathan(G4KLX) Added IP mode registration.
24 * AX.25 030 Jonathan(G4KLX) Added AX.25 fragment reception.
25 * Upgraded state machine for SABME.
26 * Added arbitrary protocol id support.
27 * AX.25 031 Joerg(DL1BKE) Added DAMA support
28 * HaJo(DD8NE) Added Idle Disc Timer T5
29 * Joerg(DL1BKE) Renamed it to "IDLE" with a slightly
30 * different behaviour. Fixed defrag
32 * AX.25 032 Darryl(G7LED) AX.25 segmentation fixed.
33 * AX.25 033 Jonathan(G4KLX) Remove auto-router.
34 * Modularisation changes.
35 * AX.25 035 Hans(PE1AYX) Fixed interface to IP layer.
36 * AX.25 036 Jonathan(G4KLX) Cloned from ax25_in.c.
37 * AX.25 037 Jonathan(G4KLX) New timer architecture.
40 #include <linux/errno.h>
41 #include <linux/types.h>
42 #include <linux/socket.h>
44 #include <linux/kernel.h>
45 #include <linux/sched.h>
46 #include <linux/timer.h>
47 #include <linux/string.h>
48 #include <linux/sockios.h>
49 #include <linux/net.h>
51 #include <linux/inet.h>
52 #include <linux/netdevice.h>
53 #include <linux/skbuff.h>
55 #include <net/ip.h> /* For ip_rcv */
56 #include <asm/uaccess.h>
57 #include <asm/system.h>
58 #include <linux/fcntl.h>
60 #include <linux/interrupt.h>
63 * State machine for state 1, Awaiting Connection State.
64 * The handling of the timer(s) is in file ax25_std_timer.c.
65 * Handling of state 0 and connection release is in ax25.c.
67 static int ax25_std_state1_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int pf, int type)
71 ax25->modulus = AX25_MODULUS;
72 ax25->window = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
73 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
77 ax25->modulus = AX25_EMODULUS;
78 ax25->window = ax25->ax25_dev->values[AX25_VALUES_EWINDOW];
79 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
83 ax25_send_control(ax25, AX25_DM, pf, AX25_RESPONSE);
88 ax25_calculate_rtt(ax25);
89 ax25_stop_t1timer(ax25);
90 ax25_start_t3timer(ax25);
91 ax25_start_idletimer(ax25);
95 ax25->state = AX25_STATE_3;
97 if (ax25->sk != NULL) {
98 ax25->sk->state = TCP_ESTABLISHED;
99 /* For WAIT_SABM connections we will produce an accept ready socket here */
101 ax25->sk->state_change(ax25->sk);
108 if (ax25->modulus == AX25_MODULUS) {
109 ax25_disconnect(ax25, ECONNREFUSED);
111 ax25->modulus = AX25_MODULUS;
112 ax25->window = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
125 * State machine for state 2, Awaiting Release State.
126 * The handling of the timer(s) is in file ax25_std_timer.c
127 * Handling of state 0 and connection release is in ax25.c.
129 static int ax25_std_state2_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int pf, int type)
134 ax25_send_control(ax25, AX25_DM, pf, AX25_RESPONSE);
138 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
139 ax25_disconnect(ax25, 0);
144 if (pf) ax25_disconnect(ax25, 0);
151 if (pf) ax25_send_control(ax25, AX25_DM, AX25_POLLON, AX25_RESPONSE);
162 * State machine for state 3, Connected State.
163 * The handling of the timer(s) is in file ax25_std_timer.c
164 * Handling of state 0 and connection release is in ax25.c.
166 static int ax25_std_state3_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int ns, int nr, int pf, int type)
173 if (frametype == AX25_SABM) {
174 ax25->modulus = AX25_MODULUS;
175 ax25->window = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
177 ax25->modulus = AX25_EMODULUS;
178 ax25->window = ax25->ax25_dev->values[AX25_VALUES_EWINDOW];
180 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
181 ax25_stop_t1timer(ax25);
182 ax25_stop_t2timer(ax25);
183 ax25_start_t3timer(ax25);
184 ax25_start_idletimer(ax25);
185 ax25->condition = 0x00;
189 ax25_requeue_frames(ax25);
193 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
194 ax25_disconnect(ax25, 0);
198 ax25_disconnect(ax25, ECONNRESET);
203 if (frametype == AX25_RR)
204 ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
206 ax25->condition |= AX25_COND_PEER_RX_BUSY;
207 if (type == AX25_COMMAND && pf)
208 ax25_std_enquiry_response(ax25);
209 if (ax25_validate_nr(ax25, nr)) {
210 ax25_check_iframes_acked(ax25, nr);
212 ax25_std_nr_error_recovery(ax25);
213 ax25->state = AX25_STATE_1;
218 ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
219 if (type == AX25_COMMAND && pf)
220 ax25_std_enquiry_response(ax25);
221 if (ax25_validate_nr(ax25, nr)) {
222 ax25_frames_acked(ax25, nr);
223 ax25_calculate_rtt(ax25);
224 ax25_stop_t1timer(ax25);
225 ax25_start_t3timer(ax25);
226 ax25_requeue_frames(ax25);
228 ax25_std_nr_error_recovery(ax25);
229 ax25->state = AX25_STATE_1;
234 if (!ax25_validate_nr(ax25, nr)) {
235 ax25_std_nr_error_recovery(ax25);
236 ax25->state = AX25_STATE_1;
239 if (ax25->condition & AX25_COND_PEER_RX_BUSY) {
240 ax25_frames_acked(ax25, nr);
242 ax25_check_iframes_acked(ax25, nr);
244 if (ax25->condition & AX25_COND_OWN_RX_BUSY) {
245 if (pf) ax25_std_enquiry_response(ax25);
248 if (ns == ax25->vr) {
249 ax25->vr = (ax25->vr + 1) % ax25->modulus;
250 queued = ax25_rx_iframe(ax25, skb);
251 if (ax25->condition & AX25_COND_OWN_RX_BUSY)
252 ax25->vr = ns; /* ax25->vr - 1 */
253 ax25->condition &= ~AX25_COND_REJECT;
255 ax25_std_enquiry_response(ax25);
257 if (!(ax25->condition & AX25_COND_ACK_PENDING)) {
258 ax25->condition |= AX25_COND_ACK_PENDING;
259 ax25_start_t2timer(ax25);
263 if (ax25->condition & AX25_COND_REJECT) {
264 if (pf) ax25_std_enquiry_response(ax25);
266 ax25->condition |= AX25_COND_REJECT;
267 ax25_send_control(ax25, AX25_REJ, pf, AX25_RESPONSE);
268 ax25->condition &= ~AX25_COND_ACK_PENDING;
275 ax25_std_establish_data_link(ax25);
276 ax25->state = AX25_STATE_1;
287 * State machine for state 4, Timer Recovery State.
288 * The handling of the timer(s) is in file ax25_std_timer.c
289 * Handling of state 0 and connection release is in ax25.c.
291 static int ax25_std_state4_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int ns, int nr, int pf, int type)
298 if (frametype == AX25_SABM) {
299 ax25->modulus = AX25_MODULUS;
300 ax25->window = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
302 ax25->modulus = AX25_EMODULUS;
303 ax25->window = ax25->ax25_dev->values[AX25_VALUES_EWINDOW];
305 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
306 ax25_stop_t1timer(ax25);
307 ax25_stop_t2timer(ax25);
308 ax25_start_t3timer(ax25);
309 ax25_start_idletimer(ax25);
310 ax25->condition = 0x00;
314 ax25->state = AX25_STATE_3;
316 ax25_requeue_frames(ax25);
320 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
321 ax25_disconnect(ax25, 0);
325 ax25_disconnect(ax25, ECONNRESET);
330 if (frametype == AX25_RR)
331 ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
333 ax25->condition |= AX25_COND_PEER_RX_BUSY;
334 if (type == AX25_RESPONSE && pf) {
335 ax25_stop_t1timer(ax25);
337 if (ax25_validate_nr(ax25, nr)) {
338 ax25_frames_acked(ax25, nr);
339 if (ax25->vs == ax25->va) {
340 ax25_start_t3timer(ax25);
341 ax25->state = AX25_STATE_3;
343 ax25_requeue_frames(ax25);
346 ax25_std_nr_error_recovery(ax25);
347 ax25->state = AX25_STATE_1;
351 if (type == AX25_COMMAND && pf)
352 ax25_std_enquiry_response(ax25);
353 if (ax25_validate_nr(ax25, nr)) {
354 ax25_frames_acked(ax25, nr);
356 ax25_std_nr_error_recovery(ax25);
357 ax25->state = AX25_STATE_1;
362 ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
363 if (pf && type == AX25_RESPONSE) {
364 ax25_stop_t1timer(ax25);
366 if (ax25_validate_nr(ax25, nr)) {
367 ax25_frames_acked(ax25, nr);
368 if (ax25->vs == ax25->va) {
369 ax25_start_t3timer(ax25);
370 ax25->state = AX25_STATE_3;
372 ax25_requeue_frames(ax25);
375 ax25_std_nr_error_recovery(ax25);
376 ax25->state = AX25_STATE_1;
380 if (type == AX25_COMMAND && pf)
381 ax25_std_enquiry_response(ax25);
382 if (ax25_validate_nr(ax25, nr)) {
383 ax25_frames_acked(ax25, nr);
384 ax25_requeue_frames(ax25);
386 ax25_std_nr_error_recovery(ax25);
387 ax25->state = AX25_STATE_1;
392 if (!ax25_validate_nr(ax25, nr)) {
393 ax25_std_nr_error_recovery(ax25);
394 ax25->state = AX25_STATE_1;
397 ax25_frames_acked(ax25, nr);
398 if (ax25->condition & AX25_COND_OWN_RX_BUSY) {
399 if (pf) ax25_std_enquiry_response(ax25);
402 if (ns == ax25->vr) {
403 ax25->vr = (ax25->vr + 1) % ax25->modulus;
404 queued = ax25_rx_iframe(ax25, skb);
405 if (ax25->condition & AX25_COND_OWN_RX_BUSY)
406 ax25->vr = ns; /* ax25->vr - 1 */
407 ax25->condition &= ~AX25_COND_REJECT;
409 ax25_std_enquiry_response(ax25);
411 if (!(ax25->condition & AX25_COND_ACK_PENDING)) {
412 ax25->condition |= AX25_COND_ACK_PENDING;
413 ax25_start_t2timer(ax25);
417 if (ax25->condition & AX25_COND_REJECT) {
418 if (pf) ax25_std_enquiry_response(ax25);
420 ax25->condition |= AX25_COND_REJECT;
421 ax25_send_control(ax25, AX25_REJ, pf, AX25_RESPONSE);
422 ax25->condition &= ~AX25_COND_ACK_PENDING;
429 ax25_std_establish_data_link(ax25);
430 ax25->state = AX25_STATE_1;
441 * Higher level upcall for a LAPB frame
443 int ax25_std_frame_in(ax25_cb *ax25, struct sk_buff *skb, int type)
445 int queued = 0, frametype, ns, nr, pf;
447 frametype = ax25_decode(ax25, skb, &ns, &nr, &pf);
449 switch (ax25->state) {
451 queued = ax25_std_state1_machine(ax25, skb, frametype, pf, type);
454 queued = ax25_std_state2_machine(ax25, skb, frametype, pf, type);
457 queued = ax25_std_state3_machine(ax25, skb, frametype, ns, nr, pf, type);
460 queued = ax25_std_state4_machine(ax25, skb, frametype, ns, nr, pf, type);