1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
5 * Copyright Darryl Miles G7LED (dlm@g7led.demon.co.uk)
7 #include <linux/errno.h>
8 #include <linux/types.h>
9 #include <linux/socket.h>
11 #include <linux/kernel.h>
12 #include <linux/timer.h>
13 #include <linux/string.h>
14 #include <linux/sockios.h>
15 #include <linux/net.h>
16 #include <linux/slab.h>
18 #include <linux/inet.h>
19 #include <linux/netdevice.h>
20 #include <linux/skbuff.h>
22 #include <linux/uaccess.h>
23 #include <linux/fcntl.h>
25 #include <linux/interrupt.h>
26 #include <net/netrom.h>
29 * This is where all NET/ROM frames pass, except for IP-over-NET/ROM which
30 * cannot be fragmented in this manner.
32 void nr_output(struct sock
*sk
, struct sk_buff
*skb
)
35 unsigned char transport
[NR_TRANSPORT_LEN
];
36 int err
, frontlen
, len
;
38 if (skb
->len
- NR_TRANSPORT_LEN
> NR_MAX_PACKET_SIZE
) {
39 /* Save a copy of the Transport Header */
40 skb_copy_from_linear_data(skb
, transport
, NR_TRANSPORT_LEN
);
41 skb_pull(skb
, NR_TRANSPORT_LEN
);
43 frontlen
= skb_headroom(skb
);
45 while (skb
->len
> 0) {
46 if ((skbn
= sock_alloc_send_skb(sk
, frontlen
+ NR_MAX_PACKET_SIZE
, 0, &err
)) == NULL
)
49 skb_reserve(skbn
, frontlen
);
51 len
= (NR_MAX_PACKET_SIZE
> skb
->len
) ? skb
->len
: NR_MAX_PACKET_SIZE
;
53 /* Copy the user data */
54 skb_copy_from_linear_data(skb
, skb_put(skbn
, len
), len
);
57 /* Duplicate the Transport Header */
58 skb_push(skbn
, NR_TRANSPORT_LEN
);
59 skb_copy_to_linear_data(skbn
, transport
,
62 skbn
->data
[4] |= NR_MORE_FLAG
;
64 skb_queue_tail(&sk
->sk_write_queue
, skbn
); /* Throw it on the queue */
69 skb_queue_tail(&sk
->sk_write_queue
, skb
); /* Throw it on the queue */
76 * This procedure is passed a buffer descriptor for an iframe. It builds
77 * the rest of the control part of the frame and then writes it out.
79 static void nr_send_iframe(struct sock
*sk
, struct sk_buff
*skb
)
81 struct nr_sock
*nr
= nr_sk(sk
);
86 skb
->data
[2] = nr
->vs
;
87 skb
->data
[3] = nr
->vr
;
89 if (nr
->condition
& NR_COND_OWN_RX_BUSY
)
90 skb
->data
[4] |= NR_CHOKE_FLAG
;
92 nr_start_idletimer(sk
);
94 nr_transmit_buffer(sk
, skb
);
97 void nr_send_nak_frame(struct sock
*sk
)
99 struct sk_buff
*skb
, *skbn
;
100 struct nr_sock
*nr
= nr_sk(sk
);
102 if ((skb
= skb_peek(&nr
->ack_queue
)) == NULL
)
105 if ((skbn
= skb_clone(skb
, GFP_ATOMIC
)) == NULL
)
108 skbn
->data
[2] = nr
->va
;
109 skbn
->data
[3] = nr
->vr
;
111 if (nr
->condition
& NR_COND_OWN_RX_BUSY
)
112 skbn
->data
[4] |= NR_CHOKE_FLAG
;
114 nr_transmit_buffer(sk
, skbn
);
116 nr
->condition
&= ~NR_COND_ACK_PENDING
;
122 void nr_kick(struct sock
*sk
)
124 struct nr_sock
*nr
= nr_sk(sk
);
125 struct sk_buff
*skb
, *skbn
;
126 unsigned short start
, end
;
128 if (nr
->state
!= NR_STATE_3
)
131 if (nr
->condition
& NR_COND_PEER_RX_BUSY
)
134 if (!skb_peek(&sk
->sk_write_queue
))
137 start
= (skb_peek(&nr
->ack_queue
) == NULL
) ? nr
->va
: nr
->vs
;
138 end
= (nr
->va
+ nr
->window
) % NR_MODULUS
;
146 * Transmit data until either we're out of data to send or
147 * the window is full.
151 * Dequeue the frame and copy it.
153 skb
= skb_dequeue(&sk
->sk_write_queue
);
156 if ((skbn
= skb_clone(skb
, GFP_ATOMIC
)) == NULL
) {
157 skb_queue_head(&sk
->sk_write_queue
, skb
);
161 skb_set_owner_w(skbn
, sk
);
164 * Transmit the frame copy.
166 nr_send_iframe(sk
, skbn
);
168 nr
->vs
= (nr
->vs
+ 1) % NR_MODULUS
;
171 * Requeue the original data frame.
173 skb_queue_tail(&nr
->ack_queue
, skb
);
175 } while (nr
->vs
!= end
&&
176 (skb
= skb_dequeue(&sk
->sk_write_queue
)) != NULL
);
179 nr
->condition
&= ~NR_COND_ACK_PENDING
;
181 if (!nr_t1timer_running(sk
))
182 nr_start_t1timer(sk
);
185 void nr_transmit_buffer(struct sock
*sk
, struct sk_buff
*skb
)
187 struct nr_sock
*nr
= nr_sk(sk
);
191 * Add the protocol byte and network header.
193 dptr
= skb_push(skb
, NR_NETWORK_LEN
);
195 memcpy(dptr
, &nr
->source_addr
, AX25_ADDR_LEN
);
196 dptr
[6] &= ~AX25_CBIT
;
197 dptr
[6] &= ~AX25_EBIT
;
198 dptr
[6] |= AX25_SSSID_SPARE
;
199 dptr
+= AX25_ADDR_LEN
;
201 memcpy(dptr
, &nr
->dest_addr
, AX25_ADDR_LEN
);
202 dptr
[6] &= ~AX25_CBIT
;
203 dptr
[6] |= AX25_EBIT
;
204 dptr
[6] |= AX25_SSSID_SPARE
;
205 dptr
+= AX25_ADDR_LEN
;
207 *dptr
++ = sysctl_netrom_network_ttl_initialiser
;
209 if (!nr_route_frame(skb
, NULL
)) {
211 nr_disconnect(sk
, ENETUNREACH
);
216 * The following routines are taken from page 170 of the 7th ARRL Computer
217 * Networking Conference paper, as is the whole state machine.
220 void nr_establish_data_link(struct sock
*sk
)
222 struct nr_sock
*nr
= nr_sk(sk
);
224 nr
->condition
= 0x00;
227 nr_write_internal(sk
, NR_CONNREQ
);
231 nr_stop_idletimer(sk
);
232 nr_start_t1timer(sk
);
236 * Never send a NAK when we are CHOKEd.
238 void nr_enquiry_response(struct sock
*sk
)
240 struct nr_sock
*nr
= nr_sk(sk
);
241 int frametype
= NR_INFOACK
;
243 if (nr
->condition
& NR_COND_OWN_RX_BUSY
) {
244 frametype
|= NR_CHOKE_FLAG
;
246 if (skb_peek(&nr
->reseq_queue
) != NULL
)
247 frametype
|= NR_NAK_FLAG
;
250 nr_write_internal(sk
, frametype
);
253 nr
->condition
&= ~NR_COND_ACK_PENDING
;
256 void nr_check_iframes_acked(struct sock
*sk
, unsigned short nr
)
258 struct nr_sock
*nrom
= nr_sk(sk
);
260 if (nrom
->vs
== nr
) {
261 nr_frames_acked(sk
, nr
);
265 if (nrom
->va
!= nr
) {
266 nr_frames_acked(sk
, nr
);
267 nr_start_t1timer(sk
);