blob: 5fc048dcd39ab29089b4140785f75315d4348f6f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
11#include <linux/config.h>
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/spinlock.h>
22#include <linux/net.h>
23#include <net/ax25.h>
24#include <linux/inet.h>
25#include <linux/netdevice.h>
26#include <linux/skbuff.h>
27#include <linux/netfilter.h>
28#include <net/sock.h>
29#include <asm/uaccess.h>
30#include <asm/system.h>
31#include <linux/fcntl.h>
32#include <linux/mm.h>
33#include <linux/interrupt.h>
34
35static DEFINE_SPINLOCK(ax25_frag_lock);
36
37ax25_cb *ax25_send_frame(struct sk_buff *skb, int paclen, ax25_address *src, ax25_address *dest, ax25_digi *digi, struct net_device *dev)
38{
39 ax25_dev *ax25_dev;
40 ax25_cb *ax25;
41
42 /*
43 * Take the default packet length for the device if zero is
44 * specified.
45 */
46 if (paclen == 0) {
47 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
48 return NULL;
49
50 paclen = ax25_dev->values[AX25_VALUES_PACLEN];
51 }
52
53 /*
54 * Look for an existing connection.
55 */
56 if ((ax25 = ax25_find_cb(src, dest, digi, dev)) != NULL) {
57 ax25_output(ax25, paclen, skb);
58 return ax25; /* It already existed */
59 }
60
61 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
62 return NULL;
63
64 if ((ax25 = ax25_create_cb()) == NULL)
65 return NULL;
66
67 ax25_fillin_cb(ax25, ax25_dev);
68
69 ax25->source_addr = *src;
70 ax25->dest_addr = *dest;
71
72 if (digi != NULL) {
73 if ((ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
74 ax25_cb_put(ax25);
75 return NULL;
76 }
77 memcpy(ax25->digipeat, digi, sizeof(ax25_digi));
78 }
79
80 switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
81 case AX25_PROTO_STD_SIMPLEX:
82 case AX25_PROTO_STD_DUPLEX:
83 ax25_std_establish_data_link(ax25);
84 break;
85
86#ifdef CONFIG_AX25_DAMA_SLAVE
87 case AX25_PROTO_DAMA_SLAVE:
88 if (ax25_dev->dama.slave)
89 ax25_ds_establish_data_link(ax25);
90 else
91 ax25_std_establish_data_link(ax25);
92 break;
93#endif
94 }
95
96 ax25_cb_add(ax25);
97
98 ax25->state = AX25_STATE_1;
99
100 ax25_start_heartbeat(ax25);
101
102 ax25_output(ax25, paclen, skb);
103
104 return ax25; /* We had to create it */
105}
106
107/*
108 * All outgoing AX.25 I frames pass via this routine. Therefore this is
109 * where the fragmentation of frames takes place. If fragment is set to
110 * zero then we are not allowed to do fragmentation, even if the frame
111 * is too large.
112 */
113void ax25_output(ax25_cb *ax25, int paclen, struct sk_buff *skb)
114{
115 struct sk_buff *skbn;
116 unsigned char *p;
117 int frontlen, len, fragno, ka9qfrag, first = 1;
118
119 if ((skb->len - 1) > paclen) {
120 if (*skb->data == AX25_P_TEXT) {
121 skb_pull(skb, 1); /* skip PID */
122 ka9qfrag = 0;
123 } else {
124 paclen -= 2; /* Allow for fragment control info */
125 ka9qfrag = 1;
126 }
127
128 fragno = skb->len / paclen;
129 if (skb->len % paclen == 0) fragno--;
130
131 frontlen = skb_headroom(skb); /* Address space + CTRL */
132
133 while (skb->len > 0) {
134 spin_lock_bh(&ax25_frag_lock);
135 if ((skbn = alloc_skb(paclen + 2 + frontlen, GFP_ATOMIC)) == NULL) {
136 spin_unlock_bh(&ax25_frag_lock);
137 printk(KERN_CRIT "AX.25: ax25_output - out of memory\n");
138 return;
139 }
140
141 if (skb->sk != NULL)
142 skb_set_owner_w(skbn, skb->sk);
143
144 spin_unlock_bh(&ax25_frag_lock);
145
146 len = (paclen > skb->len) ? skb->len : paclen;
147
148 if (ka9qfrag == 1) {
149 skb_reserve(skbn, frontlen + 2);
150 skbn->nh.raw = skbn->data + (skb->nh.raw - skb->data);
151 memcpy(skb_put(skbn, len), skb->data, len);
152 p = skb_push(skbn, 2);
153
154 *p++ = AX25_P_SEGMENT;
155
156 *p = fragno--;
157 if (first) {
158 *p |= AX25_SEG_FIRST;
159 first = 0;
160 }
161 } else {
162 skb_reserve(skbn, frontlen + 1);
163 skbn->nh.raw = skbn->data + (skb->nh.raw - skb->data);
164 memcpy(skb_put(skbn, len), skb->data, len);
165 p = skb_push(skbn, 1);
166 *p = AX25_P_TEXT;
167 }
168
169 skb_pull(skb, len);
170 skb_queue_tail(&ax25->write_queue, skbn); /* Throw it on the queue */
171 }
172
173 kfree_skb(skb);
174 } else {
175 skb_queue_tail(&ax25->write_queue, skb); /* Throw it on the queue */
176 }
177
178 switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
179 case AX25_PROTO_STD_SIMPLEX:
180 case AX25_PROTO_STD_DUPLEX:
181 ax25_kick(ax25);
182 break;
183
184#ifdef CONFIG_AX25_DAMA_SLAVE
185 /*
186 * A DAMA slave is _required_ to work as normal AX.25L2V2
187 * if no DAMA master is available.
188 */
189 case AX25_PROTO_DAMA_SLAVE:
190 if (!ax25->ax25_dev->dama.slave) ax25_kick(ax25);
191 break;
192#endif
193 }
194}
195
196/*
197 * This procedure is passed a buffer descriptor for an iframe. It builds
198 * the rest of the control part of the frame and then writes it out.
199 */
200static void ax25_send_iframe(ax25_cb *ax25, struct sk_buff *skb, int poll_bit)
201{
202 unsigned char *frame;
203
204 if (skb == NULL)
205 return;
206
207 skb->nh.raw = skb->data;
208
209 if (ax25->modulus == AX25_MODULUS) {
210 frame = skb_push(skb, 1);
211
212 *frame = AX25_I;
213 *frame |= (poll_bit) ? AX25_PF : 0;
214 *frame |= (ax25->vr << 5);
215 *frame |= (ax25->vs << 1);
216 } else {
217 frame = skb_push(skb, 2);
218
219 frame[0] = AX25_I;
220 frame[0] |= (ax25->vs << 1);
221 frame[1] = (poll_bit) ? AX25_EPF : 0;
222 frame[1] |= (ax25->vr << 1);
223 }
224
225 ax25_start_idletimer(ax25);
226
227 ax25_transmit_buffer(ax25, skb, AX25_COMMAND);
228}
229
230void ax25_kick(ax25_cb *ax25)
231{
232 struct sk_buff *skb, *skbn;
233 int last = 1;
234 unsigned short start, end, next;
235
236 if (ax25->state != AX25_STATE_3 && ax25->state != AX25_STATE_4)
237 return;
238
239 if (ax25->condition & AX25_COND_PEER_RX_BUSY)
240 return;
241
242 if (skb_peek(&ax25->write_queue) == NULL)
243 return;
244
245 start = (skb_peek(&ax25->ack_queue) == NULL) ? ax25->va : ax25->vs;
246 end = (ax25->va + ax25->window) % ax25->modulus;
247
248 if (start == end)
249 return;
250
251 ax25->vs = start;
252
253 /*
254 * Transmit data until either we're out of data to send or
255 * the window is full. Send a poll on the final I frame if
256 * the window is filled.
257 */
258
259 /*
260 * Dequeue the frame and copy it.
261 */
262 skb = skb_dequeue(&ax25->write_queue);
263
264 do {
265 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
266 skb_queue_head(&ax25->write_queue, skb);
267 break;
268 }
269
270 if (skb->sk != NULL)
271 skb_set_owner_w(skbn, skb->sk);
272
273 next = (ax25->vs + 1) % ax25->modulus;
274 last = (next == end);
275
276 /*
277 * Transmit the frame copy.
278 * bke 960114: do not set the Poll bit on the last frame
279 * in DAMA mode.
280 */
281 switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
282 case AX25_PROTO_STD_SIMPLEX:
283 case AX25_PROTO_STD_DUPLEX:
284 ax25_send_iframe(ax25, skbn, (last) ? AX25_POLLON : AX25_POLLOFF);
285 break;
286
287#ifdef CONFIG_AX25_DAMA_SLAVE
288 case AX25_PROTO_DAMA_SLAVE:
289 ax25_send_iframe(ax25, skbn, AX25_POLLOFF);
290 break;
291#endif
292 }
293
294 ax25->vs = next;
295
296 /*
297 * Requeue the original data frame.
298 */
299 skb_queue_tail(&ax25->ack_queue, skb);
300
301 } while (!last && (skb = skb_dequeue(&ax25->write_queue)) != NULL);
302
303 ax25->condition &= ~AX25_COND_ACK_PENDING;
304
305 if (!ax25_t1timer_running(ax25)) {
306 ax25_stop_t3timer(ax25);
307 ax25_calculate_t1(ax25);
308 ax25_start_t1timer(ax25);
309 }
310}
311
312void ax25_transmit_buffer(ax25_cb *ax25, struct sk_buff *skb, int type)
313{
314 struct sk_buff *skbn;
315 unsigned char *ptr;
316 int headroom;
317
318 if (ax25->ax25_dev == NULL) {
319 ax25_disconnect(ax25, ENETUNREACH);
320 return;
321 }
322
323 headroom = ax25_addr_size(ax25->digipeat);
324
325 if (skb_headroom(skb) < headroom) {
326 if ((skbn = skb_realloc_headroom(skb, headroom)) == NULL) {
327 printk(KERN_CRIT "AX.25: ax25_transmit_buffer - out of memory\n");
328 kfree_skb(skb);
329 return;
330 }
331
332 if (skb->sk != NULL)
333 skb_set_owner_w(skbn, skb->sk);
334
335 kfree_skb(skb);
336 skb = skbn;
337 }
338
339 ptr = skb_push(skb, headroom);
340
341 ax25_addr_build(ptr, &ax25->source_addr, &ax25->dest_addr, ax25->digipeat, type, ax25->modulus);
342
Arnaldo Carvalho de Melo29c4be52005-04-21 16:46:56 -0700343 ax25_queue_xmit(skb, ax25->ax25_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
346/*
347 * A small shim to dev_queue_xmit to add the KISS control byte, and do
348 * any packet forwarding in operation.
349 */
Arnaldo Carvalho de Melo29c4be52005-04-21 16:46:56 -0700350void ax25_queue_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 unsigned char *ptr;
353
Arnaldo Carvalho de Melo56cb5152005-04-24 18:53:06 -0700354 skb->protocol = ax25_type_trans(skb, ax25_fwd_dev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 ptr = skb_push(skb, 1);
357 *ptr = 0x00; /* KISS */
358
359 dev_queue_xmit(skb);
360}
361
362int ax25_check_iframes_acked(ax25_cb *ax25, unsigned short nr)
363{
364 if (ax25->vs == nr) {
365 ax25_frames_acked(ax25, nr);
366 ax25_calculate_rtt(ax25);
367 ax25_stop_t1timer(ax25);
368 ax25_start_t3timer(ax25);
369 return 1;
370 } else {
371 if (ax25->va != nr) {
372 ax25_frames_acked(ax25, nr);
373 ax25_calculate_t1(ax25);
374 ax25_start_t1timer(ax25);
375 return 1;
376 }
377 }
378 return 0;
379}
380