Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/net/netrom/nr_out.c
49791 views
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
*
4
* Copyright Jonathan Naylor G4KLX ([email protected])
5
* Copyright Darryl Miles G7LED ([email protected])
6
*/
7
#include <linux/errno.h>
8
#include <linux/types.h>
9
#include <linux/socket.h>
10
#include <linux/in.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>
17
#include <net/ax25.h>
18
#include <linux/inet.h>
19
#include <linux/netdevice.h>
20
#include <linux/skbuff.h>
21
#include <net/sock.h>
22
#include <linux/uaccess.h>
23
#include <linux/fcntl.h>
24
#include <linux/mm.h>
25
#include <linux/interrupt.h>
26
#include <net/netrom.h>
27
28
/*
29
* This is where all NET/ROM frames pass, except for IP-over-NET/ROM which
30
* cannot be fragmented in this manner.
31
*/
32
void nr_output(struct sock *sk, struct sk_buff *skb)
33
{
34
struct sk_buff *skbn;
35
unsigned char transport[NR_TRANSPORT_LEN];
36
int err, frontlen, len;
37
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);
42
43
frontlen = skb_headroom(skb);
44
45
while (skb->len > 0) {
46
if ((skbn = sock_alloc_send_skb(sk, frontlen + NR_MAX_PACKET_SIZE, 0, &err)) == NULL) {
47
kfree_skb(skb);
48
return;
49
}
50
51
skb_reserve(skbn, frontlen);
52
53
len = (NR_MAX_PACKET_SIZE > skb->len) ? skb->len : NR_MAX_PACKET_SIZE;
54
55
/* Copy the user data */
56
skb_copy_from_linear_data(skb, skb_put(skbn, len), len);
57
skb_pull(skb, len);
58
59
/* Duplicate the Transport Header */
60
skb_push(skbn, NR_TRANSPORT_LEN);
61
skb_copy_to_linear_data(skbn, transport,
62
NR_TRANSPORT_LEN);
63
if (skb->len > 0)
64
skbn->data[4] |= NR_MORE_FLAG;
65
66
skb_queue_tail(&sk->sk_write_queue, skbn); /* Throw it on the queue */
67
}
68
69
kfree_skb(skb);
70
} else {
71
skb_queue_tail(&sk->sk_write_queue, skb); /* Throw it on the queue */
72
}
73
74
nr_kick(sk);
75
}
76
77
/*
78
* This procedure is passed a buffer descriptor for an iframe. It builds
79
* the rest of the control part of the frame and then writes it out.
80
*/
81
static void nr_send_iframe(struct sock *sk, struct sk_buff *skb)
82
{
83
struct nr_sock *nr = nr_sk(sk);
84
85
if (skb == NULL)
86
return;
87
88
skb->data[2] = nr->vs;
89
skb->data[3] = nr->vr;
90
91
if (nr->condition & NR_COND_OWN_RX_BUSY)
92
skb->data[4] |= NR_CHOKE_FLAG;
93
94
nr_start_idletimer(sk);
95
96
nr_transmit_buffer(sk, skb);
97
}
98
99
void nr_send_nak_frame(struct sock *sk)
100
{
101
struct sk_buff *skb, *skbn;
102
struct nr_sock *nr = nr_sk(sk);
103
104
if ((skb = skb_peek(&nr->ack_queue)) == NULL)
105
return;
106
107
if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL)
108
return;
109
110
skbn->data[2] = nr->va;
111
skbn->data[3] = nr->vr;
112
113
if (nr->condition & NR_COND_OWN_RX_BUSY)
114
skbn->data[4] |= NR_CHOKE_FLAG;
115
116
nr_transmit_buffer(sk, skbn);
117
118
nr->condition &= ~NR_COND_ACK_PENDING;
119
nr->vl = nr->vr;
120
121
nr_stop_t1timer(sk);
122
}
123
124
void nr_kick(struct sock *sk)
125
{
126
struct nr_sock *nr = nr_sk(sk);
127
struct sk_buff *skb, *skbn;
128
unsigned short start, end;
129
130
if (nr->state != NR_STATE_3)
131
return;
132
133
if (nr->condition & NR_COND_PEER_RX_BUSY)
134
return;
135
136
if (!skb_peek(&sk->sk_write_queue))
137
return;
138
139
start = (skb_peek(&nr->ack_queue) == NULL) ? nr->va : nr->vs;
140
end = (nr->va + nr->window) % NR_MODULUS;
141
142
if (start == end)
143
return;
144
145
nr->vs = start;
146
147
/*
148
* Transmit data until either we're out of data to send or
149
* the window is full.
150
*/
151
152
/*
153
* Dequeue the frame and copy it.
154
*/
155
skb = skb_dequeue(&sk->sk_write_queue);
156
157
do {
158
if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
159
skb_queue_head(&sk->sk_write_queue, skb);
160
break;
161
}
162
163
skb_set_owner_w(skbn, sk);
164
165
/*
166
* Transmit the frame copy.
167
*/
168
nr_send_iframe(sk, skbn);
169
170
nr->vs = (nr->vs + 1) % NR_MODULUS;
171
172
/*
173
* Requeue the original data frame.
174
*/
175
skb_queue_tail(&nr->ack_queue, skb);
176
177
} while (nr->vs != end &&
178
(skb = skb_dequeue(&sk->sk_write_queue)) != NULL);
179
180
nr->vl = nr->vr;
181
nr->condition &= ~NR_COND_ACK_PENDING;
182
183
if (!nr_t1timer_running(sk))
184
nr_start_t1timer(sk);
185
}
186
187
void nr_transmit_buffer(struct sock *sk, struct sk_buff *skb)
188
{
189
struct nr_sock *nr = nr_sk(sk);
190
unsigned char *dptr;
191
192
/*
193
* Add the protocol byte and network header.
194
*/
195
dptr = skb_push(skb, NR_NETWORK_LEN);
196
197
memcpy(dptr, &nr->source_addr, AX25_ADDR_LEN);
198
dptr[6] &= ~AX25_CBIT;
199
dptr[6] &= ~AX25_EBIT;
200
dptr[6] |= AX25_SSSID_SPARE;
201
dptr += AX25_ADDR_LEN;
202
203
memcpy(dptr, &nr->dest_addr, AX25_ADDR_LEN);
204
dptr[6] &= ~AX25_CBIT;
205
dptr[6] |= AX25_EBIT;
206
dptr[6] |= AX25_SSSID_SPARE;
207
dptr += AX25_ADDR_LEN;
208
209
*dptr++ = READ_ONCE(sysctl_netrom_network_ttl_initialiser);
210
211
if (!nr_route_frame(skb, NULL)) {
212
kfree_skb(skb);
213
nr_disconnect(sk, ENETUNREACH);
214
}
215
}
216
217
/*
218
* The following routines are taken from page 170 of the 7th ARRL Computer
219
* Networking Conference paper, as is the whole state machine.
220
*/
221
222
void nr_establish_data_link(struct sock *sk)
223
{
224
struct nr_sock *nr = nr_sk(sk);
225
226
nr->condition = 0x00;
227
nr->n2count = 0;
228
229
nr_write_internal(sk, NR_CONNREQ);
230
231
nr_stop_t2timer(sk);
232
nr_stop_t4timer(sk);
233
nr_stop_idletimer(sk);
234
nr_start_t1timer(sk);
235
}
236
237
/*
238
* Never send a NAK when we are CHOKEd.
239
*/
240
void nr_enquiry_response(struct sock *sk)
241
{
242
struct nr_sock *nr = nr_sk(sk);
243
int frametype = NR_INFOACK;
244
245
if (nr->condition & NR_COND_OWN_RX_BUSY) {
246
frametype |= NR_CHOKE_FLAG;
247
} else {
248
if (skb_peek(&nr->reseq_queue) != NULL)
249
frametype |= NR_NAK_FLAG;
250
}
251
252
nr_write_internal(sk, frametype);
253
254
nr->vl = nr->vr;
255
nr->condition &= ~NR_COND_ACK_PENDING;
256
}
257
258
void nr_check_iframes_acked(struct sock *sk, unsigned short nr)
259
{
260
struct nr_sock *nrom = nr_sk(sk);
261
262
if (nrom->vs == nr) {
263
nr_frames_acked(sk, nr);
264
nr_stop_t1timer(sk);
265
nrom->n2count = 0;
266
} else {
267
if (nrom->va != nr) {
268
nr_frames_acked(sk, nr);
269
nr_start_t1timer(sk);
270
}
271
}
272
}
273
274