Import 2.3.10pre5
[davej-history.git] / net / ax25 / ax25_subr.c
blobcb3e1ba4b9526741e07bde27221e615c11af420c
1 /*
2 * AX.25 release 037
4 * This code REQUIRES 2.1.15 or higher/ NET3.038
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.
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;
18 * History
19 * AX.25 029 Alan(GW4PTS) Switched to KA9Q constant names. Removed
20 * old BSD code.
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... ;-)
27 * Dave Brown(N2RJT)
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>
41 #include <linux/in.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>
48 #include <net/ax25.h>
49 #include <linux/inet.h>
50 #include <linux/netdevice.h>
51 #include <linux/skbuff.h>
52 #include <net/sock.h>
53 #include <asm/uaccess.h>
54 #include <asm/system.h>
55 #include <linux/fcntl.h>
56 #include <linux/mm.h>
57 #include <linux/interrupt.h>
60 * This routine purges all the queues of frames.
62 void ax25_clear_queues(ax25_cb *ax25)
64 struct sk_buff *skb;
66 while ((skb = skb_dequeue(&ax25->write_queue)) != NULL)
67 kfree_skb(skb);
69 while ((skb = skb_dequeue(&ax25->ack_queue)) != NULL)
70 kfree_skb(skb);
72 while ((skb = skb_dequeue(&ax25->reseq_queue)) != NULL)
73 kfree_skb(skb);
75 while ((skb = skb_dequeue(&ax25->frag_queue)) != NULL)
76 kfree_skb(skb);
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
82 * SDL diagram.
84 void ax25_frames_acked(ax25_cb *ax25, unsigned short nr)
86 struct sk_buff *skb;
89 * Remove all the ack-ed frames from the ack queue.
91 if (ax25->va != nr) {
92 while (skb_peek(&ax25->ack_queue) != NULL && ax25->va != nr) {
93 skb = skb_dequeue(&ax25->ack_queue);
94 kfree_skb(skb);
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);
112 else
113 skb_append(skb_prev, skb);
114 skb_prev = skb;
119 * Validate that the value of nr is between va and vs. Return true or
120 * false for testing.
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;
133 return 0;
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;
145 frame = skb->data;
146 *ns = *nr = *pf = 0;
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;
162 skb_pull(skb, 1);
163 } else {
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;
169 skb_pull(skb, 2);
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;
174 skb_pull(skb, 2);
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;
178 skb_pull(skb, 1);
182 return frametype;
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)
192 struct sk_buff *skb;
193 unsigned char *dptr;
195 if ((skb = alloc_skb(AX25_BPQ_HEADER_LEN + ax25_addr_size(ax25->digipeat) + 2, GFP_ATOMIC)) == NULL)
196 return;
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);
205 *dptr = frametype;
206 *dptr |= (poll_bit) ? AX25_PF : 0;
207 if ((frametype & AX25_U) == AX25_S) /* S frames carry NR */
208 *dptr |= (ax25->vr << 5);
209 } else {
210 if ((frametype & AX25_U) == AX25_U) {
211 dptr = skb_put(skb, 1);
212 *dptr = frametype;
213 *dptr |= (poll_bit) ? AX25_PF : 0;
214 } else {
215 dptr = skb_put(skb, 2);
216 dptr[0] = frametype;
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 device *dev, ax25_address *src, ax25_address *dest, ax25_digi *digi)
232 struct sk_buff *skb;
233 char *dptr;
234 ax25_digi retdigi;
236 if (dev == NULL)
237 return;
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);
256 skb->dev = dev;
258 ax25_queue_xmit(skb);
262 * Exponential backoff for AX.25
264 void ax25_calculate_t1(ax25_cb *ax25)
266 int n, t = 2;
268 switch (ax25->backoff) {
269 case 0:
270 break;
272 case 1:
273 t += 2 * ax25->n2count;
274 break;
276 case 2:
277 for (n = 0; n < ax25->n2count; n++)
278 t *= 2;
279 if (t > 8) t = 8;
280 break;
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)
292 return;
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;
321 if (!ax25->sk->dead)
322 ax25->sk->state_change(ax25->sk);
323 ax25->sk->dead = 1;
327 #endif