1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
5 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
6 * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
8 #include <linux/errno.h>
9 #include <linux/types.h>
10 #include <linux/socket.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/timer.h>
15 #include <linux/string.h>
16 #include <linux/sockios.h>
17 #include <linux/spinlock.h>
18 #include <linux/net.h>
19 #include <linux/slab.h>
21 #include <linux/inet.h>
22 #include <linux/netdevice.h>
23 #include <linux/skbuff.h>
25 #include <linux/uaccess.h>
26 #include <linux/fcntl.h>
28 #include <linux/interrupt.h>
30 static DEFINE_SPINLOCK(ax25_frag_lock
);
32 ax25_cb
*ax25_send_frame(struct sk_buff
*skb
, int paclen
, const ax25_address
*src
, ax25_address
*dest
, ax25_digi
*digi
, struct net_device
*dev
)
38 * Take the default packet length for the device if zero is
43 ax25_dev
= ax25_dev_ax25dev(dev
);
48 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 */
61 ax25_dev
= ax25_dev_ax25dev(dev
);
67 if ((ax25
= ax25_create_cb()) == NULL
) {
71 ax25_fillin_cb(ax25
, ax25_dev
);
74 ax25
->source_addr
= *src
;
75 ax25
->dest_addr
= *dest
;
78 ax25
->digipeat
= kmemdup(digi
, sizeof(*digi
), GFP_ATOMIC
);
79 if (ax25
->digipeat
== NULL
) {
85 switch (ax25
->ax25_dev
->values
[AX25_VALUES_PROTOCOL
]) {
86 case AX25_PROTO_STD_SIMPLEX
:
87 case AX25_PROTO_STD_DUPLEX
:
88 ax25_std_establish_data_link(ax25
);
91 #ifdef CONFIG_AX25_DAMA_SLAVE
92 case AX25_PROTO_DAMA_SLAVE
:
93 if (ax25_dev
->dama
.slave
)
94 ax25_ds_establish_data_link(ax25
);
96 ax25_std_establish_data_link(ax25
);
102 * There is one ref for the state machine; a caller needs
103 * one more to put it back, just like with the existing one.
109 ax25
->state
= AX25_STATE_1
;
111 ax25_start_heartbeat(ax25
);
113 ax25_output(ax25
, paclen
, skb
);
115 return ax25
; /* We had to create it */
118 EXPORT_SYMBOL(ax25_send_frame
);
121 * All outgoing AX.25 I frames pass via this routine. Therefore this is
122 * where the fragmentation of frames takes place. If fragment is set to
123 * zero then we are not allowed to do fragmentation, even if the frame
126 void ax25_output(ax25_cb
*ax25
, int paclen
, struct sk_buff
*skb
)
128 struct sk_buff
*skbn
;
130 int frontlen
, len
, fragno
, ka9qfrag
, first
= 1;
138 if ((skb
->len
- 1) > paclen
) {
139 if (*skb
->data
== AX25_P_TEXT
) {
140 skb_pull(skb
, 1); /* skip PID */
143 paclen
-= 2; /* Allow for fragment control info */
147 fragno
= skb
->len
/ paclen
;
148 if (skb
->len
% paclen
== 0) fragno
--;
150 frontlen
= skb_headroom(skb
); /* Address space + CTRL */
152 while (skb
->len
> 0) {
153 spin_lock_bh(&ax25_frag_lock
);
154 if ((skbn
= alloc_skb(paclen
+ 2 + frontlen
, GFP_ATOMIC
)) == NULL
) {
155 spin_unlock_bh(&ax25_frag_lock
);
156 printk(KERN_CRIT
"AX.25: ax25_output - out of memory\n");
161 skb_set_owner_w(skbn
, skb
->sk
);
163 spin_unlock_bh(&ax25_frag_lock
);
165 len
= (paclen
> skb
->len
) ? skb
->len
: paclen
;
168 skb_reserve(skbn
, frontlen
+ 2);
169 skb_set_network_header(skbn
,
170 skb_network_offset(skb
));
171 skb_copy_from_linear_data(skb
, skb_put(skbn
, len
), len
);
172 p
= skb_push(skbn
, 2);
174 *p
++ = AX25_P_SEGMENT
;
178 *p
|= AX25_SEG_FIRST
;
182 skb_reserve(skbn
, frontlen
+ 1);
183 skb_set_network_header(skbn
,
184 skb_network_offset(skb
));
185 skb_copy_from_linear_data(skb
, skb_put(skbn
, len
), len
);
186 p
= skb_push(skbn
, 1);
191 skb_queue_tail(&ax25
->write_queue
, skbn
); /* Throw it on the queue */
196 skb_queue_tail(&ax25
->write_queue
, skb
); /* Throw it on the queue */
199 switch (ax25
->ax25_dev
->values
[AX25_VALUES_PROTOCOL
]) {
200 case AX25_PROTO_STD_SIMPLEX
:
201 case AX25_PROTO_STD_DUPLEX
:
205 #ifdef CONFIG_AX25_DAMA_SLAVE
207 * A DAMA slave is _required_ to work as normal AX.25L2V2
208 * if no DAMA master is available.
210 case AX25_PROTO_DAMA_SLAVE
:
211 if (!ax25
->ax25_dev
->dama
.slave
) ax25_kick(ax25
);
218 * This procedure is passed a buffer descriptor for an iframe. It builds
219 * the rest of the control part of the frame and then writes it out.
221 static void ax25_send_iframe(ax25_cb
*ax25
, struct sk_buff
*skb
, int poll_bit
)
223 unsigned char *frame
;
228 skb_reset_network_header(skb
);
230 if (ax25
->modulus
== AX25_MODULUS
) {
231 frame
= skb_push(skb
, 1);
234 *frame
|= (poll_bit
) ? AX25_PF
: 0;
235 *frame
|= (ax25
->vr
<< 5);
236 *frame
|= (ax25
->vs
<< 1);
238 frame
= skb_push(skb
, 2);
241 frame
[0] |= (ax25
->vs
<< 1);
242 frame
[1] = (poll_bit
) ? AX25_EPF
: 0;
243 frame
[1] |= (ax25
->vr
<< 1);
246 ax25_start_idletimer(ax25
);
248 ax25_transmit_buffer(ax25
, skb
, AX25_COMMAND
);
251 void ax25_kick(ax25_cb
*ax25
)
253 struct sk_buff
*skb
, *skbn
;
255 unsigned short start
, end
, next
;
257 if (ax25
->state
!= AX25_STATE_3
&& ax25
->state
!= AX25_STATE_4
)
260 if (ax25
->condition
& AX25_COND_PEER_RX_BUSY
)
263 if (skb_peek(&ax25
->write_queue
) == NULL
)
266 start
= (skb_peek(&ax25
->ack_queue
) == NULL
) ? ax25
->va
: ax25
->vs
;
267 end
= (ax25
->va
+ ax25
->window
) % ax25
->modulus
;
273 * Transmit data until either we're out of data to send or
274 * the window is full. Send a poll on the final I frame if
275 * the window is filled.
279 * Dequeue the frame and copy it.
280 * Check for race with ax25_clear_queues().
282 skb
= skb_dequeue(&ax25
->write_queue
);
289 if ((skbn
= skb_clone(skb
, GFP_ATOMIC
)) == NULL
) {
290 skb_queue_head(&ax25
->write_queue
, skb
);
295 skb_set_owner_w(skbn
, skb
->sk
);
297 next
= (ax25
->vs
+ 1) % ax25
->modulus
;
298 last
= (next
== end
);
301 * Transmit the frame copy.
302 * bke 960114: do not set the Poll bit on the last frame
305 switch (ax25
->ax25_dev
->values
[AX25_VALUES_PROTOCOL
]) {
306 case AX25_PROTO_STD_SIMPLEX
:
307 case AX25_PROTO_STD_DUPLEX
:
308 ax25_send_iframe(ax25
, skbn
, (last
) ? AX25_POLLON
: AX25_POLLOFF
);
311 #ifdef CONFIG_AX25_DAMA_SLAVE
312 case AX25_PROTO_DAMA_SLAVE
:
313 ax25_send_iframe(ax25
, skbn
, AX25_POLLOFF
);
321 * Requeue the original data frame.
323 skb_queue_tail(&ax25
->ack_queue
, skb
);
325 } while (!last
&& (skb
= skb_dequeue(&ax25
->write_queue
)) != NULL
);
327 ax25
->condition
&= ~AX25_COND_ACK_PENDING
;
329 if (!ax25_t1timer_running(ax25
)) {
330 ax25_stop_t3timer(ax25
);
331 ax25_calculate_t1(ax25
);
332 ax25_start_t1timer(ax25
);
336 void ax25_transmit_buffer(ax25_cb
*ax25
, struct sk_buff
*skb
, int type
)
341 if (ax25
->ax25_dev
== NULL
) {
342 ax25_disconnect(ax25
, ENETUNREACH
);
346 headroom
= ax25_addr_size(ax25
->digipeat
);
348 if (unlikely(skb_headroom(skb
) < headroom
)) {
349 skb
= skb_expand_head(skb
, headroom
);
351 printk(KERN_CRIT
"AX.25: ax25_transmit_buffer - out of memory\n");
356 ptr
= skb_push(skb
, headroom
);
358 ax25_addr_build(ptr
, &ax25
->source_addr
, &ax25
->dest_addr
, ax25
->digipeat
, type
, ax25
->modulus
);
360 ax25_queue_xmit(skb
, ax25
->ax25_dev
->dev
);
364 * A small shim to dev_queue_xmit to add the KISS control byte, and do
365 * any packet forwarding in operation.
367 void ax25_queue_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
372 skb
->protocol
= ax25_type_trans(skb
, ax25_fwd_dev(dev
));
375 ptr
= skb_push(skb
, 1);
376 *ptr
= 0x00; /* KISS */
381 int ax25_check_iframes_acked(ax25_cb
*ax25
, unsigned short nr
)
383 if (ax25
->vs
== nr
) {
384 ax25_frames_acked(ax25
, nr
);
385 ax25_calculate_rtt(ax25
);
386 ax25_stop_t1timer(ax25
);
387 ax25_start_t3timer(ax25
);
390 if (ax25
->va
!= nr
) {
391 ax25_frames_acked(ax25
, nr
);
392 ax25_calculate_t1(ax25
);
393 ax25_start_t1timer(ax25
);