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.
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)
11 #include <linux/errno.h>
12 #include <linux/types.h>
13 #include <linux/socket.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/timer.h>
18 #include <linux/string.h>
19 #include <linux/sockios.h>
20 #include <linux/spinlock.h>
21 #include <linux/net.h>
23 #include <linux/inet.h>
24 #include <linux/netdevice.h>
25 #include <linux/skbuff.h>
26 #include <linux/netfilter.h>
28 #include <asm/uaccess.h>
29 #include <asm/system.h>
30 #include <linux/fcntl.h>
32 #include <linux/interrupt.h>
34 static DEFINE_SPINLOCK(ax25_frag_lock
);
36 ax25_cb
*ax25_send_frame(struct sk_buff
*skb
, int paclen
, ax25_address
*src
, ax25_address
*dest
, ax25_digi
*digi
, struct net_device
*dev
)
42 * Take the default packet length for the device if zero is
46 if ((ax25_dev
= ax25_dev_ax25dev(dev
)) == NULL
)
49 paclen
= ax25_dev
->values
[AX25_VALUES_PACLEN
];
53 * Look for an existing connection.
55 if ((ax25
= ax25_find_cb(src
, dest
, digi
, dev
)) != NULL
) {
56 ax25_output(ax25
, paclen
, skb
);
57 return ax25
; /* It already existed */
60 if ((ax25_dev
= ax25_dev_ax25dev(dev
)) == NULL
)
63 if ((ax25
= ax25_create_cb()) == NULL
)
66 ax25_fillin_cb(ax25
, ax25_dev
);
68 ax25
->source_addr
= *src
;
69 ax25
->dest_addr
= *dest
;
72 ax25
->digipeat
= kmemdup(digi
, sizeof(*digi
), GFP_ATOMIC
);
73 if (ax25
->digipeat
== NULL
) {
79 switch (ax25
->ax25_dev
->values
[AX25_VALUES_PROTOCOL
]) {
80 case AX25_PROTO_STD_SIMPLEX
:
81 case AX25_PROTO_STD_DUPLEX
:
82 ax25_std_establish_data_link(ax25
);
85 #ifdef CONFIG_AX25_DAMA_SLAVE
86 case AX25_PROTO_DAMA_SLAVE
:
87 if (ax25_dev
->dama
.slave
)
88 ax25_ds_establish_data_link(ax25
);
90 ax25_std_establish_data_link(ax25
);
97 ax25
->state
= AX25_STATE_1
;
99 ax25_start_heartbeat(ax25
);
101 ax25_output(ax25
, paclen
, skb
);
103 return ax25
; /* We had to create it */
106 EXPORT_SYMBOL(ax25_send_frame
);
109 * All outgoing AX.25 I frames pass via this routine. Therefore this is
110 * where the fragmentation of frames takes place. If fragment is set to
111 * zero then we are not allowed to do fragmentation, even if the frame
114 void ax25_output(ax25_cb
*ax25
, int paclen
, struct sk_buff
*skb
)
116 struct sk_buff
*skbn
;
118 int frontlen
, len
, fragno
, ka9qfrag
, first
= 1;
120 <<<<<<< HEAD
:net
/ax25
/ax25_out
.c
128 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/ax25
/ax25_out
.c
129 if ((skb
->len
- 1) > paclen
) {
130 if (*skb
->data
== AX25_P_TEXT
) {
131 skb_pull(skb
, 1); /* skip PID */
134 paclen
-= 2; /* Allow for fragment control info */
138 fragno
= skb
->len
/ paclen
;
139 if (skb
->len
% paclen
== 0) fragno
--;
141 frontlen
= skb_headroom(skb
); /* Address space + CTRL */
143 while (skb
->len
> 0) {
144 spin_lock_bh(&ax25_frag_lock
);
145 if ((skbn
= alloc_skb(paclen
+ 2 + frontlen
, GFP_ATOMIC
)) == NULL
) {
146 spin_unlock_bh(&ax25_frag_lock
);
147 printk(KERN_CRIT
"AX.25: ax25_output - out of memory\n");
152 skb_set_owner_w(skbn
, skb
->sk
);
154 spin_unlock_bh(&ax25_frag_lock
);
156 len
= (paclen
> skb
->len
) ? skb
->len
: paclen
;
159 skb_reserve(skbn
, frontlen
+ 2);
160 skb_set_network_header(skbn
,
161 skb_network_offset(skb
));
162 skb_copy_from_linear_data(skb
, skb_put(skbn
, len
), len
);
163 p
= skb_push(skbn
, 2);
165 *p
++ = AX25_P_SEGMENT
;
169 *p
|= AX25_SEG_FIRST
;
173 skb_reserve(skbn
, frontlen
+ 1);
174 skb_set_network_header(skbn
,
175 skb_network_offset(skb
));
176 skb_copy_from_linear_data(skb
, skb_put(skbn
, len
), len
);
177 p
= skb_push(skbn
, 1);
182 skb_queue_tail(&ax25
->write_queue
, skbn
); /* Throw it on the queue */
187 skb_queue_tail(&ax25
->write_queue
, skb
); /* Throw it on the queue */
190 switch (ax25
->ax25_dev
->values
[AX25_VALUES_PROTOCOL
]) {
191 case AX25_PROTO_STD_SIMPLEX
:
192 case AX25_PROTO_STD_DUPLEX
:
196 #ifdef CONFIG_AX25_DAMA_SLAVE
198 * A DAMA slave is _required_ to work as normal AX.25L2V2
199 * if no DAMA master is available.
201 case AX25_PROTO_DAMA_SLAVE
:
202 if (!ax25
->ax25_dev
->dama
.slave
) ax25_kick(ax25
);
209 * This procedure is passed a buffer descriptor for an iframe. It builds
210 * the rest of the control part of the frame and then writes it out.
212 static void ax25_send_iframe(ax25_cb
*ax25
, struct sk_buff
*skb
, int poll_bit
)
214 unsigned char *frame
;
219 skb_reset_network_header(skb
);
221 if (ax25
->modulus
== AX25_MODULUS
) {
222 frame
= skb_push(skb
, 1);
225 *frame
|= (poll_bit
) ? AX25_PF
: 0;
226 *frame
|= (ax25
->vr
<< 5);
227 *frame
|= (ax25
->vs
<< 1);
229 frame
= skb_push(skb
, 2);
232 frame
[0] |= (ax25
->vs
<< 1);
233 frame
[1] = (poll_bit
) ? AX25_EPF
: 0;
234 frame
[1] |= (ax25
->vr
<< 1);
237 ax25_start_idletimer(ax25
);
239 ax25_transmit_buffer(ax25
, skb
, AX25_COMMAND
);
242 void ax25_kick(ax25_cb
*ax25
)
244 struct sk_buff
*skb
, *skbn
;
246 unsigned short start
, end
, next
;
248 if (ax25
->state
!= AX25_STATE_3
&& ax25
->state
!= AX25_STATE_4
)
251 if (ax25
->condition
& AX25_COND_PEER_RX_BUSY
)
254 if (skb_peek(&ax25
->write_queue
) == NULL
)
257 start
= (skb_peek(&ax25
->ack_queue
) == NULL
) ? ax25
->va
: ax25
->vs
;
258 end
= (ax25
->va
+ ax25
->window
) % ax25
->modulus
;
263 <<<<<<< HEAD
:net
/ax25
/ax25_out
.c
267 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/ax25
/ax25_out
.c
269 * Transmit data until either we're out of data to send or
270 * the window is full. Send a poll on the final I frame if
271 * the window is filled.
275 * Dequeue the frame and copy it.
276 <<<<<<< HEAD:net/ax25/ax25_out.c
278 * Check for race with ax25_clear_queues().
279 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/ax25/ax25_out.c
281 skb
= skb_dequeue(&ax25
->write_queue
);
282 <<<<<<< HEAD
:net
/ax25
/ax25_out
.c
288 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/ax25
/ax25_out
.c
291 if ((skbn
= skb_clone(skb
, GFP_ATOMIC
)) == NULL
) {
292 skb_queue_head(&ax25
->write_queue
, skb
);
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
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
);
313 #ifdef CONFIG_AX25_DAMA_SLAVE
314 case AX25_PROTO_DAMA_SLAVE
:
315 ax25_send_iframe(ax25
, skbn
, AX25_POLLOFF
);
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
;
344 if (ax25
->ax25_dev
== NULL
) {
345 ax25_disconnect(ax25
, ENETUNREACH
);
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");
359 skb_set_owner_w(skbn
, skb
->sk
);
365 ptr
= skb_push(skb
, headroom
);
367 ax25_addr_build(ptr
, &ax25
->source_addr
, &ax25
->dest_addr
, ax25
->digipeat
, type
, ax25
->modulus
);
369 ax25_queue_xmit(skb
, ax25
->ax25_dev
->dev
);
373 * A small shim to dev_queue_xmit to add the KISS control byte, and do
374 * any packet forwarding in operation.
376 void ax25_queue_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
380 skb
->protocol
= ax25_type_trans(skb
, ax25_fwd_dev(dev
));
382 ptr
= skb_push(skb
, 1);
383 *ptr
= 0x00; /* KISS */
388 int ax25_check_iframes_acked(ax25_cb
*ax25
, unsigned short nr
)
390 if (ax25
->vs
== nr
) {
391 ax25_frames_acked(ax25
, nr
);
392 ax25_calculate_rtt(ax25
);
393 ax25_stop_t1timer(ax25
);
394 ax25_start_t3timer(ax25
);
397 if (ax25
->va
!= nr
) {
398 ax25_frames_acked(ax25
, nr
);
399 ax25_calculate_t1(ax25
);
400 ax25_start_t1timer(ax25
);