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 029 Alan(GW4PTS) Switched to KA9Q constant names. Removed
21 * AX.25 030 Jonathan(G4KLX) Added support for extended AX.25.
22 * Added fragmentation support.
23 * Darryl(G7LED) Added function ax25_requeue_frames() to split
24 * it up from ax25_frames_acked().
25 * AX.25 031 Joerg(DL1BKE) DAMA needs KISS Fullduplex ON/OFF.
26 * Thus we have ax25_kiss_cmd() now... ;-)
28 * Killed a silly bug in the DAMA code.
29 * Joerg(DL1BKE) Found the real bug in ax25.h, sri.
30 * AX.25 032 Joerg(DL1BKE) Added ax25_queue_length to count the number of
31 * enqueued buffers of a socket..
32 * AX.25 035 Frederic(F1OAT) Support for pseudo-digipeating.
33 * AX.25 037 Jonathan(G4KLX) New timer architecture.
36 #include <linux/config.h>
37 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
38 #include <linux/errno.h>
39 #include <linux/types.h>
40 #include <linux/socket.h>
42 #include <linux/kernel.h>
43 #include <linux/sched.h>
44 #include <linux/timer.h>
45 #include <linux/string.h>
46 #include <linux/sockios.h>
47 #include <linux/net.h>
49 #include <linux/inet.h>
50 #include <linux/netdevice.h>
51 #include <linux/skbuff.h>
53 #include <asm/uaccess.h>
54 #include <asm/system.h>
55 #include <linux/fcntl.h>
57 #include <linux/interrupt.h>
60 * This routine purges all the queues of frames.
62 void ax25_clear_queues(ax25_cb
*ax25
)
66 while ((skb
= skb_dequeue(&ax25
->write_queue
)) != NULL
)
69 while ((skb
= skb_dequeue(&ax25
->ack_queue
)) != NULL
)
72 while ((skb
= skb_dequeue(&ax25
->reseq_queue
)) != NULL
)
75 while ((skb
= skb_dequeue(&ax25
->frag_queue
)) != NULL
)
80 * This routine purges the input queue of those frames that have been
81 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
84 void ax25_frames_acked(ax25_cb
*ax25
, unsigned short nr
)
89 * Remove all the ack-ed frames from the ack queue.
92 while (skb_peek(&ax25
->ack_queue
) != NULL
&& ax25
->va
!= nr
) {
93 skb
= skb_dequeue(&ax25
->ack_queue
);
95 ax25
->va
= (ax25
->va
+ 1) % ax25
->modulus
;
100 void ax25_requeue_frames(ax25_cb
*ax25
)
102 struct sk_buff
*skb
, *skb_prev
= NULL
;
105 * Requeue all the un-ack-ed frames on the output queue to be picked
106 * up by ax25_kick called from the timer. This arrangement handles the
107 * possibility of an empty output queue.
109 while ((skb
= skb_dequeue(&ax25
->ack_queue
)) != NULL
) {
110 if (skb_prev
== NULL
)
111 skb_queue_head(&ax25
->write_queue
, skb
);
113 skb_append(skb_prev
, skb
);
119 * Validate that the value of nr is between va and vs. Return true or
122 int ax25_validate_nr(ax25_cb
*ax25
, unsigned short nr
)
124 unsigned short vc
= ax25
->va
;
126 while (vc
!= ax25
->vs
) {
127 if (nr
== vc
) return 1;
128 vc
= (vc
+ 1) % ax25
->modulus
;
131 if (nr
== ax25
->vs
) return 1;
137 * This routine is the centralised routine for parsing the control
138 * information for the different frame formats.
140 int ax25_decode(ax25_cb
*ax25
, struct sk_buff
*skb
, int *ns
, int *nr
, int *pf
)
142 unsigned char *frame
;
143 int frametype
= AX25_ILLEGAL
;
148 if (ax25
->modulus
== AX25_MODULUS
) {
149 if ((frame
[0] & AX25_S
) == 0) {
150 frametype
= AX25_I
; /* I frame - carries NR/NS/PF */
151 *ns
= (frame
[0] >> 1) & 0x07;
152 *nr
= (frame
[0] >> 5) & 0x07;
153 *pf
= frame
[0] & AX25_PF
;
154 } else if ((frame
[0] & AX25_U
) == 1) { /* S frame - take out PF/NR */
155 frametype
= frame
[0] & 0x0F;
156 *nr
= (frame
[0] >> 5) & 0x07;
157 *pf
= frame
[0] & AX25_PF
;
158 } else if ((frame
[0] & AX25_U
) == 3) { /* U frame - take out PF */
159 frametype
= frame
[0] & ~AX25_PF
;
160 *pf
= frame
[0] & AX25_PF
;
164 if ((frame
[0] & AX25_S
) == 0) {
165 frametype
= AX25_I
; /* I frame - carries NR/NS/PF */
166 *ns
= (frame
[0] >> 1) & 0x7F;
167 *nr
= (frame
[1] >> 1) & 0x7F;
168 *pf
= frame
[1] & AX25_EPF
;
170 } else if ((frame
[0] & AX25_U
) == 1) { /* S frame - take out PF/NR */
171 frametype
= frame
[0] & 0x0F;
172 *nr
= (frame
[1] >> 1) & 0x7F;
173 *pf
= frame
[1] & AX25_EPF
;
175 } else if ((frame
[0] & AX25_U
) == 3) { /* U frame - take out PF */
176 frametype
= frame
[0] & ~AX25_PF
;
177 *pf
= frame
[0] & AX25_PF
;
186 * This routine is called when the HDLC layer internally generates a
187 * command or response for the remote machine ( eg. RR, UA etc. ).
188 * Only supervisory or unnumbered frames are processed.
190 void ax25_send_control(ax25_cb
*ax25
, int frametype
, int poll_bit
, int type
)
195 if ((skb
= alloc_skb(AX25_BPQ_HEADER_LEN
+ ax25_addr_size(ax25
->digipeat
) + 2, GFP_ATOMIC
)) == NULL
)
198 skb_reserve(skb
, AX25_BPQ_HEADER_LEN
+ ax25_addr_size(ax25
->digipeat
));
200 skb
->nh
.raw
= skb
->data
;
202 /* Assume a response - address structure for DTE */
203 if (ax25
->modulus
== AX25_MODULUS
) {
204 dptr
= skb_put(skb
, 1);
206 *dptr
|= (poll_bit
) ? AX25_PF
: 0;
207 if ((frametype
& AX25_U
) == AX25_S
) /* S frames carry NR */
208 *dptr
|= (ax25
->vr
<< 5);
210 if ((frametype
& AX25_U
) == AX25_U
) {
211 dptr
= skb_put(skb
, 1);
213 *dptr
|= (poll_bit
) ? AX25_PF
: 0;
215 dptr
= skb_put(skb
, 2);
217 dptr
[1] = (ax25
->vr
<< 1);
218 dptr
[1] |= (poll_bit
) ? AX25_EPF
: 0;
222 ax25_transmit_buffer(ax25
, skb
, type
);
226 * Send a 'DM' to an unknown connection attempt, or an invalid caller.
228 * Note: src here is the sender, thus it's the target of the DM
230 void ax25_return_dm(struct net_device
*dev
, ax25_address
*src
, ax25_address
*dest
, ax25_digi
*digi
)
239 if ((skb
= alloc_skb(AX25_BPQ_HEADER_LEN
+ ax25_addr_size(digi
) + 1, GFP_ATOMIC
)) == NULL
)
240 return; /* Next SABM will get DM'd */
242 skb_reserve(skb
, AX25_BPQ_HEADER_LEN
+ ax25_addr_size(digi
));
244 ax25_digi_invert(digi
, &retdigi
);
246 dptr
= skb_put(skb
, 1);
248 *dptr
= AX25_DM
| AX25_PF
;
251 * Do the address ourselves
253 dptr
= skb_push(skb
, ax25_addr_size(digi
));
254 dptr
+= ax25_addr_build(dptr
, dest
, src
, &retdigi
, AX25_RESPONSE
, AX25_MODULUS
);
258 ax25_queue_xmit(skb
);
262 * Exponential backoff for AX.25
264 void ax25_calculate_t1(ax25_cb
*ax25
)
268 switch (ax25
->backoff
) {
273 t
+= 2 * ax25
->n2count
;
277 for (n
= 0; n
< ax25
->n2count
; n
++)
283 ax25
->t1
= t
* ax25
->rtt
;
287 * Calculate the Round Trip Time
289 void ax25_calculate_rtt(ax25_cb
*ax25
)
291 if (ax25
->backoff
== 0)
294 if (ax25_t1timer_running(ax25
) && ax25
->n2count
== 0)
295 ax25
->rtt
= (9 * ax25
->rtt
+ ax25
->t1
- ax25_display_timer(&ax25
->t1timer
)) / 10;
297 if (ax25
->rtt
< AX25_T1CLAMPLO
)
298 ax25
->rtt
= AX25_T1CLAMPLO
;
300 if (ax25
->rtt
> AX25_T1CLAMPHI
)
301 ax25
->rtt
= AX25_T1CLAMPHI
;
304 void ax25_disconnect(ax25_cb
*ax25
, int reason
)
306 ax25_clear_queues(ax25
);
308 ax25_stop_t1timer(ax25
);
309 ax25_stop_t2timer(ax25
);
310 ax25_stop_t3timer(ax25
);
311 ax25_stop_idletimer(ax25
);
313 ax25
->state
= AX25_STATE_0
;
315 ax25_link_failed(ax25
, reason
);
317 if (ax25
->sk
!= NULL
) {
318 ax25
->sk
->state
= TCP_CLOSE
;
319 ax25
->sk
->err
= reason
;
320 ax25
->sk
->shutdown
|= SEND_SHUTDOWN
;
322 ax25
->sk
->state_change(ax25
->sk
);