diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /net/ax25/ax25_subr.c |
Linux-2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'net/ax25/ax25_subr.c')
-rw-r--r-- | net/ax25/ax25_subr.c | 295 |
1 files changed, 295 insertions, 0 deletions
diff --git a/net/ax25/ax25_subr.c b/net/ax25/ax25_subr.c new file mode 100644 index 00000000000..8cf72707af8 --- /dev/null +++ b/net/ax25/ax25_subr.c | |||
@@ -0,0 +1,295 @@ | |||
1 | /* | ||
2 | * This program is free software; you can redistribute it and/or modify | ||
3 | * it under the terms of the GNU General Public License as published by | ||
4 | * the Free Software Foundation; either version 2 of the License, or | ||
5 | * (at your option) any later version. | ||
6 | * | ||
7 | * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk) | ||
8 | * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk) | ||
9 | * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de) | ||
10 | * Copyright (C) Frederic Rible F1OAT (frible@teaser.fr) | ||
11 | */ | ||
12 | #include <linux/errno.h> | ||
13 | #include <linux/types.h> | ||
14 | #include <linux/socket.h> | ||
15 | #include <linux/in.h> | ||
16 | #include <linux/kernel.h> | ||
17 | #include <linux/sched.h> | ||
18 | #include <linux/timer.h> | ||
19 | #include <linux/string.h> | ||
20 | #include <linux/sockios.h> | ||
21 | #include <linux/net.h> | ||
22 | #include <net/ax25.h> | ||
23 | #include <linux/inet.h> | ||
24 | #include <linux/netdevice.h> | ||
25 | #include <linux/skbuff.h> | ||
26 | #include <net/sock.h> | ||
27 | #include <net/tcp.h> | ||
28 | #include <asm/uaccess.h> | ||
29 | #include <asm/system.h> | ||
30 | #include <linux/fcntl.h> | ||
31 | #include <linux/mm.h> | ||
32 | #include <linux/interrupt.h> | ||
33 | |||
34 | /* | ||
35 | * This routine purges all the queues of frames. | ||
36 | */ | ||
37 | void ax25_clear_queues(ax25_cb *ax25) | ||
38 | { | ||
39 | skb_queue_purge(&ax25->write_queue); | ||
40 | skb_queue_purge(&ax25->ack_queue); | ||
41 | skb_queue_purge(&ax25->reseq_queue); | ||
42 | skb_queue_purge(&ax25->frag_queue); | ||
43 | } | ||
44 | |||
45 | /* | ||
46 | * This routine purges the input queue of those frames that have been | ||
47 | * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the | ||
48 | * SDL diagram. | ||
49 | */ | ||
50 | void ax25_frames_acked(ax25_cb *ax25, unsigned short nr) | ||
51 | { | ||
52 | struct sk_buff *skb; | ||
53 | |||
54 | /* | ||
55 | * Remove all the ack-ed frames from the ack queue. | ||
56 | */ | ||
57 | if (ax25->va != nr) { | ||
58 | while (skb_peek(&ax25->ack_queue) != NULL && ax25->va != nr) { | ||
59 | skb = skb_dequeue(&ax25->ack_queue); | ||
60 | kfree_skb(skb); | ||
61 | ax25->va = (ax25->va + 1) % ax25->modulus; | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | |||
66 | void ax25_requeue_frames(ax25_cb *ax25) | ||
67 | { | ||
68 | struct sk_buff *skb, *skb_prev = NULL; | ||
69 | |||
70 | /* | ||
71 | * Requeue all the un-ack-ed frames on the output queue to be picked | ||
72 | * up by ax25_kick called from the timer. This arrangement handles the | ||
73 | * possibility of an empty output queue. | ||
74 | */ | ||
75 | while ((skb = skb_dequeue(&ax25->ack_queue)) != NULL) { | ||
76 | if (skb_prev == NULL) | ||
77 | skb_queue_head(&ax25->write_queue, skb); | ||
78 | else | ||
79 | skb_append(skb_prev, skb); | ||
80 | skb_prev = skb; | ||
81 | } | ||
82 | } | ||
83 | |||
84 | /* | ||
85 | * Validate that the value of nr is between va and vs. Return true or | ||
86 | * false for testing. | ||
87 | */ | ||
88 | int ax25_validate_nr(ax25_cb *ax25, unsigned short nr) | ||
89 | { | ||
90 | unsigned short vc = ax25->va; | ||
91 | |||
92 | while (vc != ax25->vs) { | ||
93 | if (nr == vc) return 1; | ||
94 | vc = (vc + 1) % ax25->modulus; | ||
95 | } | ||
96 | |||
97 | if (nr == ax25->vs) return 1; | ||
98 | |||
99 | return 0; | ||
100 | } | ||
101 | |||
102 | /* | ||
103 | * This routine is the centralised routine for parsing the control | ||
104 | * information for the different frame formats. | ||
105 | */ | ||
106 | int ax25_decode(ax25_cb *ax25, struct sk_buff *skb, int *ns, int *nr, int *pf) | ||
107 | { | ||
108 | unsigned char *frame; | ||
109 | int frametype = AX25_ILLEGAL; | ||
110 | |||
111 | frame = skb->data; | ||
112 | *ns = *nr = *pf = 0; | ||
113 | |||
114 | if (ax25->modulus == AX25_MODULUS) { | ||
115 | if ((frame[0] & AX25_S) == 0) { | ||
116 | frametype = AX25_I; /* I frame - carries NR/NS/PF */ | ||
117 | *ns = (frame[0] >> 1) & 0x07; | ||
118 | *nr = (frame[0] >> 5) & 0x07; | ||
119 | *pf = frame[0] & AX25_PF; | ||
120 | } else if ((frame[0] & AX25_U) == 1) { /* S frame - take out PF/NR */ | ||
121 | frametype = frame[0] & 0x0F; | ||
122 | *nr = (frame[0] >> 5) & 0x07; | ||
123 | *pf = frame[0] & AX25_PF; | ||
124 | } else if ((frame[0] & AX25_U) == 3) { /* U frame - take out PF */ | ||
125 | frametype = frame[0] & ~AX25_PF; | ||
126 | *pf = frame[0] & AX25_PF; | ||
127 | } | ||
128 | skb_pull(skb, 1); | ||
129 | } else { | ||
130 | if ((frame[0] & AX25_S) == 0) { | ||
131 | frametype = AX25_I; /* I frame - carries NR/NS/PF */ | ||
132 | *ns = (frame[0] >> 1) & 0x7F; | ||
133 | *nr = (frame[1] >> 1) & 0x7F; | ||
134 | *pf = frame[1] & AX25_EPF; | ||
135 | skb_pull(skb, 2); | ||
136 | } else if ((frame[0] & AX25_U) == 1) { /* S frame - take out PF/NR */ | ||
137 | frametype = frame[0] & 0x0F; | ||
138 | *nr = (frame[1] >> 1) & 0x7F; | ||
139 | *pf = frame[1] & AX25_EPF; | ||
140 | skb_pull(skb, 2); | ||
141 | } else if ((frame[0] & AX25_U) == 3) { /* U frame - take out PF */ | ||
142 | frametype = frame[0] & ~AX25_PF; | ||
143 | *pf = frame[0] & AX25_PF; | ||
144 | skb_pull(skb, 1); | ||
145 | } | ||
146 | } | ||
147 | |||
148 | return frametype; | ||
149 | } | ||
150 | |||
151 | /* | ||
152 | * This routine is called when the HDLC layer internally generates a | ||
153 | * command or response for the remote machine ( eg. RR, UA etc. ). | ||
154 | * Only supervisory or unnumbered frames are processed. | ||
155 | */ | ||
156 | void ax25_send_control(ax25_cb *ax25, int frametype, int poll_bit, int type) | ||
157 | { | ||
158 | struct sk_buff *skb; | ||
159 | unsigned char *dptr; | ||
160 | |||
161 | if ((skb = alloc_skb(ax25->ax25_dev->dev->hard_header_len + 2, GFP_ATOMIC)) == NULL) | ||
162 | return; | ||
163 | |||
164 | skb_reserve(skb, ax25->ax25_dev->dev->hard_header_len); | ||
165 | |||
166 | skb->nh.raw = skb->data; | ||
167 | |||
168 | /* Assume a response - address structure for DTE */ | ||
169 | if (ax25->modulus == AX25_MODULUS) { | ||
170 | dptr = skb_put(skb, 1); | ||
171 | *dptr = frametype; | ||
172 | *dptr |= (poll_bit) ? AX25_PF : 0; | ||
173 | if ((frametype & AX25_U) == AX25_S) /* S frames carry NR */ | ||
174 | *dptr |= (ax25->vr << 5); | ||
175 | } else { | ||
176 | if ((frametype & AX25_U) == AX25_U) { | ||
177 | dptr = skb_put(skb, 1); | ||
178 | *dptr = frametype; | ||
179 | *dptr |= (poll_bit) ? AX25_PF : 0; | ||
180 | } else { | ||
181 | dptr = skb_put(skb, 2); | ||
182 | dptr[0] = frametype; | ||
183 | dptr[1] = (ax25->vr << 1); | ||
184 | dptr[1] |= (poll_bit) ? AX25_EPF : 0; | ||
185 | } | ||
186 | } | ||
187 | |||
188 | ax25_transmit_buffer(ax25, skb, type); | ||
189 | } | ||
190 | |||
191 | /* | ||
192 | * Send a 'DM' to an unknown connection attempt, or an invalid caller. | ||
193 | * | ||
194 | * Note: src here is the sender, thus it's the target of the DM | ||
195 | */ | ||
196 | void ax25_return_dm(struct net_device *dev, ax25_address *src, ax25_address *dest, ax25_digi *digi) | ||
197 | { | ||
198 | struct sk_buff *skb; | ||
199 | char *dptr; | ||
200 | ax25_digi retdigi; | ||
201 | |||
202 | if (dev == NULL) | ||
203 | return; | ||
204 | |||
205 | if ((skb = alloc_skb(dev->hard_header_len + 1, GFP_ATOMIC)) == NULL) | ||
206 | return; /* Next SABM will get DM'd */ | ||
207 | |||
208 | skb_reserve(skb, dev->hard_header_len); | ||
209 | skb->nh.raw = skb->data; | ||
210 | |||
211 | ax25_digi_invert(digi, &retdigi); | ||
212 | |||
213 | dptr = skb_put(skb, 1); | ||
214 | |||
215 | *dptr = AX25_DM | AX25_PF; | ||
216 | |||
217 | /* | ||
218 | * Do the address ourselves | ||
219 | */ | ||
220 | dptr = skb_push(skb, ax25_addr_size(digi)); | ||
221 | dptr += ax25_addr_build(dptr, dest, src, &retdigi, AX25_RESPONSE, AX25_MODULUS); | ||
222 | |||
223 | skb->dev = dev; | ||
224 | |||
225 | ax25_queue_xmit(skb); | ||
226 | } | ||
227 | |||
228 | /* | ||
229 | * Exponential backoff for AX.25 | ||
230 | */ | ||
231 | void ax25_calculate_t1(ax25_cb *ax25) | ||
232 | { | ||
233 | int n, t = 2; | ||
234 | |||
235 | switch (ax25->backoff) { | ||
236 | case 0: | ||
237 | break; | ||
238 | |||
239 | case 1: | ||
240 | t += 2 * ax25->n2count; | ||
241 | break; | ||
242 | |||
243 | case 2: | ||
244 | for (n = 0; n < ax25->n2count; n++) | ||
245 | t *= 2; | ||
246 | if (t > 8) t = 8; | ||
247 | break; | ||
248 | } | ||
249 | |||
250 | ax25->t1 = t * ax25->rtt; | ||
251 | } | ||
252 | |||
253 | /* | ||
254 | * Calculate the Round Trip Time | ||
255 | */ | ||
256 | void ax25_calculate_rtt(ax25_cb *ax25) | ||
257 | { | ||
258 | if (ax25->backoff == 0) | ||
259 | return; | ||
260 | |||
261 | if (ax25_t1timer_running(ax25) && ax25->n2count == 0) | ||
262 | ax25->rtt = (9 * ax25->rtt + ax25->t1 - ax25_display_timer(&ax25->t1timer)) / 10; | ||
263 | |||
264 | if (ax25->rtt < AX25_T1CLAMPLO) | ||
265 | ax25->rtt = AX25_T1CLAMPLO; | ||
266 | |||
267 | if (ax25->rtt > AX25_T1CLAMPHI) | ||
268 | ax25->rtt = AX25_T1CLAMPHI; | ||
269 | } | ||
270 | |||
271 | void ax25_disconnect(ax25_cb *ax25, int reason) | ||
272 | { | ||
273 | ax25_clear_queues(ax25); | ||
274 | |||
275 | ax25_stop_t1timer(ax25); | ||
276 | ax25_stop_t2timer(ax25); | ||
277 | ax25_stop_t3timer(ax25); | ||
278 | ax25_stop_idletimer(ax25); | ||
279 | |||
280 | ax25->state = AX25_STATE_0; | ||
281 | |||
282 | ax25_link_failed(ax25, reason); | ||
283 | |||
284 | if (ax25->sk != NULL) { | ||
285 | bh_lock_sock(ax25->sk); | ||
286 | ax25->sk->sk_state = TCP_CLOSE; | ||
287 | ax25->sk->sk_err = reason; | ||
288 | ax25->sk->sk_shutdown |= SEND_SHUTDOWN; | ||
289 | if (!sock_flag(ax25->sk, SOCK_DEAD)) { | ||
290 | ax25->sk->sk_state_change(ax25->sk); | ||
291 | sock_set_flag(ax25->sk, SOCK_DEAD); | ||
292 | } | ||
293 | bh_unlock_sock(ax25->sk); | ||
294 | } | ||
295 | } | ||