Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 *
4 * Copyright Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
5 * Copyright Darryl Miles G7LED (dlm@g7led.demon.co.uk)
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 <net/tcp_states.h>
23#include <linux/uaccess.h>
24#include <linux/fcntl.h>
25#include <linux/mm.h>
26#include <linux/interrupt.h>
27#include <net/netrom.h>
28
29static int nr_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more)
30{
31 struct sk_buff *skbo, *skbn = skb;
32 struct nr_sock *nr = nr_sk(sk);
33
34 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
35
36 nr_start_idletimer(sk);
37
38 if (more) {
39 nr->fraglen += skb->len;
40 skb_queue_tail(&nr->frag_queue, skb);
41 return 0;
42 }
43
44 if (!more && nr->fraglen > 0) { /* End of fragment */
45 nr->fraglen += skb->len;
46 skb_queue_tail(&nr->frag_queue, skb);
47
48 if ((skbn = alloc_skb(nr->fraglen, GFP_ATOMIC)) == NULL)
49 return 1;
50
51 skb_reset_transport_header(skbn);
52
53 while ((skbo = skb_dequeue(&nr->frag_queue)) != NULL) {
54 skb_copy_from_linear_data(skbo,
55 skb_put(skbn, skbo->len),
56 skbo->len);
57 kfree_skb(skbo);
58 }
59
60 nr->fraglen = 0;
61 }
62
63 return sock_queue_rcv_skb(sk, skbn);
64}
65
66/*
67 * State machine for state 1, Awaiting Connection State.
68 * The handling of the timer(s) is in file nr_timer.c.
69 * Handling of state 0 and connection release is in netrom.c.
70 */
71static int nr_state1_machine(struct sock *sk, struct sk_buff *skb,
72 int frametype)
73{
74 switch (frametype) {
75 case NR_CONNACK: {
76 struct nr_sock *nr = nr_sk(sk);
77
78 nr_stop_t1timer(sk);
79 nr_start_idletimer(sk);
80 nr->your_index = skb->data[17];
81 nr->your_id = skb->data[18];
82 nr->vs = 0;
83 nr->va = 0;
84 nr->vr = 0;
85 nr->vl = 0;
86 nr->state = NR_STATE_3;
87 nr->n2count = 0;
88 nr->window = skb->data[20];
89 sk->sk_state = TCP_ESTABLISHED;
90 if (!sock_flag(sk, SOCK_DEAD))
91 sk->sk_state_change(sk);
92 break;
93 }
94
95 case NR_CONNACK | NR_CHOKE_FLAG:
96 nr_disconnect(sk, ECONNREFUSED);
97 break;
98
99 case NR_RESET:
100 if (READ_ONCE(sysctl_netrom_reset_circuit))
101 nr_disconnect(sk, ECONNRESET);
102 break;
103
104 default:
105 break;
106 }
107 return 0;
108}
109
110/*
111 * State machine for state 2, Awaiting Release State.
112 * The handling of the timer(s) is in file nr_timer.c
113 * Handling of state 0 and connection release is in netrom.c.
114 */
115static int nr_state2_machine(struct sock *sk, struct sk_buff *skb,
116 int frametype)
117{
118 switch (frametype) {
119 case NR_CONNACK | NR_CHOKE_FLAG:
120 nr_disconnect(sk, ECONNRESET);
121 break;
122
123 case NR_DISCREQ:
124 nr_write_internal(sk, NR_DISCACK);
125 fallthrough;
126 case NR_DISCACK:
127 nr_disconnect(sk, 0);
128 break;
129
130 case NR_RESET:
131 if (READ_ONCE(sysctl_netrom_reset_circuit))
132 nr_disconnect(sk, ECONNRESET);
133 break;
134
135 default:
136 break;
137 }
138 return 0;
139}
140
141/*
142 * State machine for state 3, Connected State.
143 * The handling of the timer(s) is in file nr_timer.c
144 * Handling of state 0 and connection release is in netrom.c.
145 */
146static int nr_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype)
147{
148 struct nr_sock *nrom = nr_sk(sk);
149 struct sk_buff_head temp_queue;
150 struct sk_buff *skbn;
151 unsigned short save_vr;
152 unsigned short nr, ns;
153 int queued = 0;
154
155 nr = skb->data[18];
156
157 switch (frametype) {
158 case NR_CONNREQ:
159 nr_write_internal(sk, NR_CONNACK);
160 break;
161
162 case NR_DISCREQ:
163 nr_write_internal(sk, NR_DISCACK);
164 nr_disconnect(sk, 0);
165 break;
166
167 case NR_CONNACK | NR_CHOKE_FLAG:
168 case NR_DISCACK:
169 nr_disconnect(sk, ECONNRESET);
170 break;
171
172 case NR_INFOACK:
173 case NR_INFOACK | NR_CHOKE_FLAG:
174 case NR_INFOACK | NR_NAK_FLAG:
175 case NR_INFOACK | NR_NAK_FLAG | NR_CHOKE_FLAG:
176 if (frametype & NR_CHOKE_FLAG) {
177 nrom->condition |= NR_COND_PEER_RX_BUSY;
178 nr_start_t4timer(sk);
179 } else {
180 nrom->condition &= ~NR_COND_PEER_RX_BUSY;
181 nr_stop_t4timer(sk);
182 }
183 if (!nr_validate_nr(sk, nr)) {
184 break;
185 }
186 if (frametype & NR_NAK_FLAG) {
187 nr_frames_acked(sk, nr);
188 nr_send_nak_frame(sk);
189 } else {
190 if (nrom->condition & NR_COND_PEER_RX_BUSY) {
191 nr_frames_acked(sk, nr);
192 } else {
193 nr_check_iframes_acked(sk, nr);
194 }
195 }
196 break;
197
198 case NR_INFO:
199 case NR_INFO | NR_NAK_FLAG:
200 case NR_INFO | NR_CHOKE_FLAG:
201 case NR_INFO | NR_MORE_FLAG:
202 case NR_INFO | NR_NAK_FLAG | NR_CHOKE_FLAG:
203 case NR_INFO | NR_CHOKE_FLAG | NR_MORE_FLAG:
204 case NR_INFO | NR_NAK_FLAG | NR_MORE_FLAG:
205 case NR_INFO | NR_NAK_FLAG | NR_CHOKE_FLAG | NR_MORE_FLAG:
206 if (frametype & NR_CHOKE_FLAG) {
207 nrom->condition |= NR_COND_PEER_RX_BUSY;
208 nr_start_t4timer(sk);
209 } else {
210 nrom->condition &= ~NR_COND_PEER_RX_BUSY;
211 nr_stop_t4timer(sk);
212 }
213 if (nr_validate_nr(sk, nr)) {
214 if (frametype & NR_NAK_FLAG) {
215 nr_frames_acked(sk, nr);
216 nr_send_nak_frame(sk);
217 } else {
218 if (nrom->condition & NR_COND_PEER_RX_BUSY) {
219 nr_frames_acked(sk, nr);
220 } else {
221 nr_check_iframes_acked(sk, nr);
222 }
223 }
224 }
225 queued = 1;
226 skb_queue_head(&nrom->reseq_queue, skb);
227 if (nrom->condition & NR_COND_OWN_RX_BUSY)
228 break;
229 skb_queue_head_init(&temp_queue);
230 do {
231 save_vr = nrom->vr;
232 while ((skbn = skb_dequeue(&nrom->reseq_queue)) != NULL) {
233 ns = skbn->data[17];
234 if (ns == nrom->vr) {
235 if (nr_queue_rx_frame(sk, skbn, frametype & NR_MORE_FLAG) == 0) {
236 nrom->vr = (nrom->vr + 1) % NR_MODULUS;
237 } else {
238 nrom->condition |= NR_COND_OWN_RX_BUSY;
239 skb_queue_tail(&temp_queue, skbn);
240 }
241 } else if (nr_in_rx_window(sk, ns)) {
242 skb_queue_tail(&temp_queue, skbn);
243 } else {
244 kfree_skb(skbn);
245 }
246 }
247 while ((skbn = skb_dequeue(&temp_queue)) != NULL) {
248 skb_queue_tail(&nrom->reseq_queue, skbn);
249 }
250 } while (save_vr != nrom->vr);
251 /*
252 * Window is full, ack it immediately.
253 */
254 if (((nrom->vl + nrom->window) % NR_MODULUS) == nrom->vr) {
255 nr_enquiry_response(sk);
256 } else {
257 if (!(nrom->condition & NR_COND_ACK_PENDING)) {
258 nrom->condition |= NR_COND_ACK_PENDING;
259 nr_start_t2timer(sk);
260 }
261 }
262 break;
263
264 case NR_RESET:
265 if (READ_ONCE(sysctl_netrom_reset_circuit))
266 nr_disconnect(sk, ECONNRESET);
267 break;
268
269 default:
270 break;
271 }
272 return queued;
273}
274
275/* Higher level upcall for a LAPB frame - called with sk locked */
276int nr_process_rx_frame(struct sock *sk, struct sk_buff *skb)
277{
278 struct nr_sock *nr = nr_sk(sk);
279 int queued = 0, frametype;
280
281 if (nr->state == NR_STATE_0)
282 return 0;
283
284 frametype = skb->data[19];
285
286 switch (nr->state) {
287 case NR_STATE_1:
288 queued = nr_state1_machine(sk, skb, frametype);
289 break;
290 case NR_STATE_2:
291 queued = nr_state2_machine(sk, skb, frametype);
292 break;
293 case NR_STATE_3:
294 queued = nr_state3_machine(sk, skb, frametype);
295 break;
296 }
297
298 nr_kick(sk);
299
300 return queued;
301}
1/*
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 Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 * Copyright Darryl Miles G7LED (dlm@g7led.demon.co.uk)
9 */
10#include <linux/errno.h>
11#include <linux/types.h>
12#include <linux/socket.h>
13#include <linux/in.h>
14#include <linux/kernel.h>
15#include <linux/timer.h>
16#include <linux/string.h>
17#include <linux/sockios.h>
18#include <linux/net.h>
19#include <linux/slab.h>
20#include <net/ax25.h>
21#include <linux/inet.h>
22#include <linux/netdevice.h>
23#include <linux/skbuff.h>
24#include <net/sock.h>
25#include <net/tcp_states.h>
26#include <linux/uaccess.h>
27#include <linux/fcntl.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
30#include <net/netrom.h>
31
32static int nr_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more)
33{
34 struct sk_buff *skbo, *skbn = skb;
35 struct nr_sock *nr = nr_sk(sk);
36
37 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
38
39 nr_start_idletimer(sk);
40
41 if (more) {
42 nr->fraglen += skb->len;
43 skb_queue_tail(&nr->frag_queue, skb);
44 return 0;
45 }
46
47 if (!more && nr->fraglen > 0) { /* End of fragment */
48 nr->fraglen += skb->len;
49 skb_queue_tail(&nr->frag_queue, skb);
50
51 if ((skbn = alloc_skb(nr->fraglen, GFP_ATOMIC)) == NULL)
52 return 1;
53
54 skb_reset_transport_header(skbn);
55
56 while ((skbo = skb_dequeue(&nr->frag_queue)) != NULL) {
57 skb_copy_from_linear_data(skbo,
58 skb_put(skbn, skbo->len),
59 skbo->len);
60 kfree_skb(skbo);
61 }
62
63 nr->fraglen = 0;
64 }
65
66 return sock_queue_rcv_skb(sk, skbn);
67}
68
69/*
70 * State machine for state 1, Awaiting Connection State.
71 * The handling of the timer(s) is in file nr_timer.c.
72 * Handling of state 0 and connection release is in netrom.c.
73 */
74static int nr_state1_machine(struct sock *sk, struct sk_buff *skb,
75 int frametype)
76{
77 switch (frametype) {
78 case NR_CONNACK: {
79 struct nr_sock *nr = nr_sk(sk);
80
81 nr_stop_t1timer(sk);
82 nr_start_idletimer(sk);
83 nr->your_index = skb->data[17];
84 nr->your_id = skb->data[18];
85 nr->vs = 0;
86 nr->va = 0;
87 nr->vr = 0;
88 nr->vl = 0;
89 nr->state = NR_STATE_3;
90 nr->n2count = 0;
91 nr->window = skb->data[20];
92 sk->sk_state = TCP_ESTABLISHED;
93 if (!sock_flag(sk, SOCK_DEAD))
94 sk->sk_state_change(sk);
95 break;
96 }
97
98 case NR_CONNACK | NR_CHOKE_FLAG:
99 nr_disconnect(sk, ECONNREFUSED);
100 break;
101
102 case NR_RESET:
103 if (sysctl_netrom_reset_circuit)
104 nr_disconnect(sk, ECONNRESET);
105 break;
106
107 default:
108 break;
109 }
110 return 0;
111}
112
113/*
114 * State machine for state 2, Awaiting Release State.
115 * The handling of the timer(s) is in file nr_timer.c
116 * Handling of state 0 and connection release is in netrom.c.
117 */
118static int nr_state2_machine(struct sock *sk, struct sk_buff *skb,
119 int frametype)
120{
121 switch (frametype) {
122 case NR_CONNACK | NR_CHOKE_FLAG:
123 nr_disconnect(sk, ECONNRESET);
124 break;
125
126 case NR_DISCREQ:
127 nr_write_internal(sk, NR_DISCACK);
128 /* fall through */
129 case NR_DISCACK:
130 nr_disconnect(sk, 0);
131 break;
132
133 case NR_RESET:
134 if (sysctl_netrom_reset_circuit)
135 nr_disconnect(sk, ECONNRESET);
136 break;
137
138 default:
139 break;
140 }
141 return 0;
142}
143
144/*
145 * State machine for state 3, Connected State.
146 * The handling of the timer(s) is in file nr_timer.c
147 * Handling of state 0 and connection release is in netrom.c.
148 */
149static int nr_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype)
150{
151 struct nr_sock *nrom = nr_sk(sk);
152 struct sk_buff_head temp_queue;
153 struct sk_buff *skbn;
154 unsigned short save_vr;
155 unsigned short nr, ns;
156 int queued = 0;
157
158 nr = skb->data[18];
159 ns = skb->data[17];
160
161 switch (frametype) {
162 case NR_CONNREQ:
163 nr_write_internal(sk, NR_CONNACK);
164 break;
165
166 case NR_DISCREQ:
167 nr_write_internal(sk, NR_DISCACK);
168 nr_disconnect(sk, 0);
169 break;
170
171 case NR_CONNACK | NR_CHOKE_FLAG:
172 case NR_DISCACK:
173 nr_disconnect(sk, ECONNRESET);
174 break;
175
176 case NR_INFOACK:
177 case NR_INFOACK | NR_CHOKE_FLAG:
178 case NR_INFOACK | NR_NAK_FLAG:
179 case NR_INFOACK | NR_NAK_FLAG | NR_CHOKE_FLAG:
180 if (frametype & NR_CHOKE_FLAG) {
181 nrom->condition |= NR_COND_PEER_RX_BUSY;
182 nr_start_t4timer(sk);
183 } else {
184 nrom->condition &= ~NR_COND_PEER_RX_BUSY;
185 nr_stop_t4timer(sk);
186 }
187 if (!nr_validate_nr(sk, nr)) {
188 break;
189 }
190 if (frametype & NR_NAK_FLAG) {
191 nr_frames_acked(sk, nr);
192 nr_send_nak_frame(sk);
193 } else {
194 if (nrom->condition & NR_COND_PEER_RX_BUSY) {
195 nr_frames_acked(sk, nr);
196 } else {
197 nr_check_iframes_acked(sk, nr);
198 }
199 }
200 break;
201
202 case NR_INFO:
203 case NR_INFO | NR_NAK_FLAG:
204 case NR_INFO | NR_CHOKE_FLAG:
205 case NR_INFO | NR_MORE_FLAG:
206 case NR_INFO | NR_NAK_FLAG | NR_CHOKE_FLAG:
207 case NR_INFO | NR_CHOKE_FLAG | NR_MORE_FLAG:
208 case NR_INFO | NR_NAK_FLAG | NR_MORE_FLAG:
209 case NR_INFO | NR_NAK_FLAG | NR_CHOKE_FLAG | NR_MORE_FLAG:
210 if (frametype & NR_CHOKE_FLAG) {
211 nrom->condition |= NR_COND_PEER_RX_BUSY;
212 nr_start_t4timer(sk);
213 } else {
214 nrom->condition &= ~NR_COND_PEER_RX_BUSY;
215 nr_stop_t4timer(sk);
216 }
217 if (nr_validate_nr(sk, nr)) {
218 if (frametype & NR_NAK_FLAG) {
219 nr_frames_acked(sk, nr);
220 nr_send_nak_frame(sk);
221 } else {
222 if (nrom->condition & NR_COND_PEER_RX_BUSY) {
223 nr_frames_acked(sk, nr);
224 } else {
225 nr_check_iframes_acked(sk, nr);
226 }
227 }
228 }
229 queued = 1;
230 skb_queue_head(&nrom->reseq_queue, skb);
231 if (nrom->condition & NR_COND_OWN_RX_BUSY)
232 break;
233 skb_queue_head_init(&temp_queue);
234 do {
235 save_vr = nrom->vr;
236 while ((skbn = skb_dequeue(&nrom->reseq_queue)) != NULL) {
237 ns = skbn->data[17];
238 if (ns == nrom->vr) {
239 if (nr_queue_rx_frame(sk, skbn, frametype & NR_MORE_FLAG) == 0) {
240 nrom->vr = (nrom->vr + 1) % NR_MODULUS;
241 } else {
242 nrom->condition |= NR_COND_OWN_RX_BUSY;
243 skb_queue_tail(&temp_queue, skbn);
244 }
245 } else if (nr_in_rx_window(sk, ns)) {
246 skb_queue_tail(&temp_queue, skbn);
247 } else {
248 kfree_skb(skbn);
249 }
250 }
251 while ((skbn = skb_dequeue(&temp_queue)) != NULL) {
252 skb_queue_tail(&nrom->reseq_queue, skbn);
253 }
254 } while (save_vr != nrom->vr);
255 /*
256 * Window is full, ack it immediately.
257 */
258 if (((nrom->vl + nrom->window) % NR_MODULUS) == nrom->vr) {
259 nr_enquiry_response(sk);
260 } else {
261 if (!(nrom->condition & NR_COND_ACK_PENDING)) {
262 nrom->condition |= NR_COND_ACK_PENDING;
263 nr_start_t2timer(sk);
264 }
265 }
266 break;
267
268 case NR_RESET:
269 if (sysctl_netrom_reset_circuit)
270 nr_disconnect(sk, ECONNRESET);
271 break;
272
273 default:
274 break;
275 }
276 return queued;
277}
278
279/* Higher level upcall for a LAPB frame - called with sk locked */
280int nr_process_rx_frame(struct sock *sk, struct sk_buff *skb)
281{
282 struct nr_sock *nr = nr_sk(sk);
283 int queued = 0, frametype;
284
285 if (nr->state == NR_STATE_0)
286 return 0;
287
288 frametype = skb->data[19];
289
290 switch (nr->state) {
291 case NR_STATE_1:
292 queued = nr_state1_machine(sk, skb, frametype);
293 break;
294 case NR_STATE_2:
295 queued = nr_state2_machine(sk, skb, frametype);
296 break;
297 case NR_STATE_3:
298 queued = nr_state3_machine(sk, skb, frametype);
299 break;
300 }
301
302 nr_kick(sk);
303
304 return queued;
305}