* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / net / ax25 / ax25_out.c
blobfe5125defcd67d245600b450d32732419116aa6a
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 028a Jonathan(G4KLX) New state machine based on SDL diagrams.
20 * AX.25 029 Alan(GW4PTS) Switched to KA9Q constant names.
21 * Jonathan(G4KLX) Only poll when window is full.
22 * AX.25 030 Jonathan(G4KLX) Added fragmentation to ax25_output.
23 * Added support for extended AX.25.
24 * AX.25 031 Joerg(DL1BKE) Added DAMA support
25 * Joerg(DL1BKE) Modified fragmenter to fragment vanilla
26 * AX.25 I-Frames. Added PACLEN parameter.
27 * Joerg(DL1BKE) Fixed a problem with buffer allocation
28 * for fragments.
29 * AX.25 037 Jonathan(G4KLX) New timer architecture.
30 * Joerg(DL1BKE) Fixed DAMA Slave mode: will work
31 * on non-DAMA interfaces like AX25L2V2
32 * again (this behaviour is _required_).
33 * Joerg(DL1BKE) ax25_check_iframes_acked() returns a
34 * value now (for DAMA n2count handling)
37 #include <linux/config.h>
38 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
39 #include <linux/errno.h>
40 #include <linux/types.h>
41 #include <linux/socket.h>
42 #include <linux/in.h>
43 #include <linux/kernel.h>
44 #include <linux/sched.h>
45 #include <linux/timer.h>
46 #include <linux/string.h>
47 #include <linux/sockios.h>
48 #include <linux/net.h>
49 #include <net/ax25.h>
50 #include <linux/inet.h>
51 #include <linux/netdevice.h>
52 #include <linux/skbuff.h>
53 #include <linux/netfilter.h>
54 #include <net/sock.h>
55 #include <asm/uaccess.h>
56 #include <asm/system.h>
57 #include <linux/fcntl.h>
58 #include <linux/mm.h>
59 #include <linux/interrupt.h>
61 ax25_cb *ax25_send_frame(struct sk_buff *skb, int paclen, ax25_address *src, ax25_address *dest, ax25_digi *digi, struct net_device *dev)
63 ax25_dev *ax25_dev;
64 ax25_cb *ax25;
67 * Take the default packet length for the device if zero is
68 * specified.
70 if (paclen == 0) {
71 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
72 return NULL;
74 paclen = ax25_dev->values[AX25_VALUES_PACLEN];
78 * Look for an existing connection.
80 if ((ax25 = ax25_find_cb(src, dest, digi, dev)) != NULL) {
81 ax25_output(ax25, paclen, skb);
82 return ax25; /* It already existed */
85 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
86 return NULL;
88 if ((ax25 = ax25_create_cb()) == NULL)
89 return NULL;
91 ax25_fillin_cb(ax25, ax25_dev);
93 ax25->source_addr = *src;
94 ax25->dest_addr = *dest;
96 if (digi != NULL) {
97 if ((ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
98 ax25_free_cb(ax25);
99 return NULL;
101 *ax25->digipeat = *digi;
104 switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
105 case AX25_PROTO_STD_SIMPLEX:
106 case AX25_PROTO_STD_DUPLEX:
107 ax25_std_establish_data_link(ax25);
108 break;
110 #ifdef CONFIG_AX25_DAMA_SLAVE
111 case AX25_PROTO_DAMA_SLAVE:
112 if (ax25_dev->dama.slave)
113 ax25_ds_establish_data_link(ax25);
114 else
115 ax25_std_establish_data_link(ax25);
116 break;
117 #endif
120 ax25_insert_socket(ax25);
122 ax25->state = AX25_STATE_1;
124 ax25_start_heartbeat(ax25);
126 ax25_output(ax25, paclen, skb);
128 return ax25; /* We had to create it */
132 * All outgoing AX.25 I frames pass via this routine. Therefore this is
133 * where the fragmentation of frames takes place. If fragment is set to
134 * zero then we are not allowed to do fragmentation, even if the frame
135 * is too large.
137 void ax25_output(ax25_cb *ax25, int paclen, struct sk_buff *skb)
139 struct sk_buff *skbn;
140 unsigned char *p;
141 int frontlen, len, fragno, ka9qfrag, first = 1;
142 long flags;
144 if ((skb->len - 1) > paclen) {
145 if (*skb->data == AX25_P_TEXT) {
146 skb_pull(skb, 1); /* skip PID */
147 ka9qfrag = 0;
148 } else {
149 paclen -= 2; /* Allow for fragment control info */
150 ka9qfrag = 1;
153 fragno = skb->len / paclen;
154 if (skb->len % paclen == 0) fragno--;
156 frontlen = skb_headroom(skb); /* Address space + CTRL */
158 while (skb->len > 0) {
159 save_flags(flags);
160 cli();
162 if ((skbn = alloc_skb(paclen + 2 + frontlen, GFP_ATOMIC)) == NULL) {
163 restore_flags(flags);
164 printk(KERN_CRIT "AX.25: ax25_output - out of memory\n");
165 return;
168 if (skb->sk != NULL)
169 skb_set_owner_w(skbn, skb->sk);
171 restore_flags(flags);
173 len = (paclen > skb->len) ? skb->len : paclen;
175 if (ka9qfrag == 1) {
176 skb_reserve(skbn, frontlen + 2);
178 memcpy(skb_put(skbn, len), skb->data, len);
179 p = skb_push(skbn, 2);
181 *p++ = AX25_P_SEGMENT;
183 *p = fragno--;
184 if (first) {
185 *p |= AX25_SEG_FIRST;
186 first = 0;
188 } else {
189 skb_reserve(skbn, frontlen + 1);
190 memcpy(skb_put(skbn, len), skb->data, len);
191 p = skb_push(skbn, 1);
192 *p = AX25_P_TEXT;
195 skb_pull(skb, len);
196 skb_queue_tail(&ax25->write_queue, skbn); /* Throw it on the queue */
199 kfree_skb(skb);
200 } else {
201 skb_queue_tail(&ax25->write_queue, skb); /* Throw it on the queue */
204 switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
205 case AX25_PROTO_STD_SIMPLEX:
206 case AX25_PROTO_STD_DUPLEX:
207 ax25_kick(ax25);
208 break;
210 #ifdef CONFIG_AX25_DAMA_SLAVE
212 * A DAMA slave is _required_ to work as normal AX.25L2V2
213 * if no DAMA master is available.
215 case AX25_PROTO_DAMA_SLAVE:
216 if (!ax25->ax25_dev->dama.slave) ax25_kick(ax25);
217 break;
218 #endif
223 * This procedure is passed a buffer descriptor for an iframe. It builds
224 * the rest of the control part of the frame and then writes it out.
226 static void ax25_send_iframe(ax25_cb *ax25, struct sk_buff *skb, int poll_bit)
228 unsigned char *frame;
230 if (skb == NULL)
231 return;
233 skb->nh.raw = skb->data;
235 if (ax25->modulus == AX25_MODULUS) {
236 frame = skb_push(skb, 1);
238 *frame = AX25_I;
239 *frame |= (poll_bit) ? AX25_PF : 0;
240 *frame |= (ax25->vr << 5);
241 *frame |= (ax25->vs << 1);
242 } else {
243 frame = skb_push(skb, 2);
245 frame[0] = AX25_I;
246 frame[0] |= (ax25->vs << 1);
247 frame[1] = (poll_bit) ? AX25_EPF : 0;
248 frame[1] |= (ax25->vr << 1);
251 ax25_start_idletimer(ax25);
253 ax25_transmit_buffer(ax25, skb, AX25_COMMAND);
256 void ax25_kick(ax25_cb *ax25)
258 struct sk_buff *skb, *skbn;
259 int last = 1;
260 unsigned short start, end, next;
262 if (ax25->state != AX25_STATE_3 && ax25->state != AX25_STATE_4)
263 return;
265 if (ax25->condition & AX25_COND_PEER_RX_BUSY)
266 return;
268 if (skb_peek(&ax25->write_queue) == NULL)
269 return;
271 start = (skb_peek(&ax25->ack_queue) == NULL) ? ax25->va : ax25->vs;
272 end = (ax25->va + ax25->window) % ax25->modulus;
274 if (start == end)
275 return;
277 ax25->vs = start;
280 * Transmit data until either we're out of data to send or
281 * the window is full. Send a poll on the final I frame if
282 * the window is filled.
286 * Dequeue the frame and copy it.
288 skb = skb_dequeue(&ax25->write_queue);
290 do {
291 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
292 skb_queue_head(&ax25->write_queue, skb);
293 break;
296 if (skb->sk != NULL)
297 skb_set_owner_w(skbn, skb->sk);
299 next = (ax25->vs + 1) % ax25->modulus;
300 last = (next == end);
303 * Transmit the frame copy.
304 * bke 960114: do not set the Poll bit on the last frame
305 * in DAMA mode.
307 switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
308 case AX25_PROTO_STD_SIMPLEX:
309 case AX25_PROTO_STD_DUPLEX:
310 ax25_send_iframe(ax25, skbn, (last) ? AX25_POLLON : AX25_POLLOFF);
311 break;
313 #ifdef CONFIG_AX25_DAMA_SLAVE
314 case AX25_PROTO_DAMA_SLAVE:
315 ax25_send_iframe(ax25, skbn, AX25_POLLOFF);
316 break;
317 #endif
320 ax25->vs = next;
323 * Requeue the original data frame.
325 skb_queue_tail(&ax25->ack_queue, skb);
327 } while (!last && (skb = skb_dequeue(&ax25->write_queue)) != NULL);
329 ax25->condition &= ~AX25_COND_ACK_PENDING;
331 if (!ax25_t1timer_running(ax25)) {
332 ax25_stop_t3timer(ax25);
333 ax25_calculate_t1(ax25);
334 ax25_start_t1timer(ax25);
338 void ax25_transmit_buffer(ax25_cb *ax25, struct sk_buff *skb, int type)
340 struct sk_buff *skbn;
341 unsigned char *ptr;
342 int headroom;
344 if (ax25->ax25_dev == NULL) {
345 ax25_disconnect(ax25, ENETUNREACH);
346 return;
349 headroom = ax25_addr_size(ax25->digipeat);
351 if (skb_headroom(skb) < headroom) {
352 if ((skbn = skb_realloc_headroom(skb, headroom)) == NULL) {
353 printk(KERN_CRIT "AX.25: ax25_transmit_buffer - out of memory\n");
354 kfree_skb(skb);
355 return;
358 if (skb->sk != NULL)
359 skb_set_owner_w(skbn, skb->sk);
361 kfree_skb(skb);
362 skb = skbn;
365 ptr = skb_push(skb, headroom);
367 ax25_addr_build(ptr, &ax25->source_addr, &ax25->dest_addr, ax25->digipeat, type, ax25->modulus);
369 skb->dev = ax25->ax25_dev->dev;
371 ax25_queue_xmit(skb);
375 * A small shim to dev_queue_xmit to add the KISS control byte, and do
376 * any packet forwarding in operation.
378 void ax25_queue_xmit(struct sk_buff *skb)
380 unsigned char *ptr;
382 skb->protocol = htons(ETH_P_AX25);
383 skb->dev = ax25_fwd_dev(skb->dev);
385 ptr = skb_push(skb, 1);
386 *ptr = 0x00; /* KISS */
388 dev_queue_xmit(skb);
391 int ax25_check_iframes_acked(ax25_cb *ax25, unsigned short nr)
393 if (ax25->vs == nr) {
394 ax25_frames_acked(ax25, nr);
395 ax25_calculate_rtt(ax25);
396 ax25_stop_t1timer(ax25);
397 ax25_start_t3timer(ax25);
398 return 1;
399 } else {
400 if (ax25->va != nr) {
401 ax25_frames_acked(ax25, nr);
402 ax25_calculate_t1(ax25);
403 ax25_start_t1timer(ax25);
404 return 1;
407 return 0;
410 #endif