xref: /linux/net/rose/rose_in.c (revision 8be4d31cb8aaeea27bde4b7ddb26e28a89062ebf)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
5  *
6  * Most of this code is based on the SDL diagrams published in the 7th ARRL
7  * Computer Networking Conference papers. The diagrams have mistakes in them,
8  * but are mostly correct. Before you modify the code could you read the SDL
9  * diagrams as the code is not obvious and probably very easy to break.
10  */
11 #include <linux/errno.h>
12 #include <linux/filter.h>
13 #include <linux/types.h>
14 #include <linux/socket.h>
15 #include <linux/in.h>
16 #include <linux/kernel.h>
17 #include <linux/timer.h>
18 #include <linux/string.h>
19 #include <linux/sockios.h>
20 #include <linux/net.h>
21 #include <net/ax25.h>
22 #include <linux/inet.h>
23 #include <linux/netdevice.h>
24 #include <linux/skbuff.h>
25 #include <net/sock.h>
26 #include <net/tcp_states.h>
27 #include <linux/fcntl.h>
28 #include <linux/mm.h>
29 #include <linux/interrupt.h>
30 #include <net/rose.h>
31 
32 /*
33  * State machine for state 1, Awaiting Call Accepted State.
34  * The handling of the timer(s) is in file rose_timer.c.
35  * Handling of state 0 and connection release is in af_rose.c.
36  */
rose_state1_machine(struct sock * sk,struct sk_buff * skb,int frametype)37 static int rose_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype)
38 {
39 	struct rose_sock *rose = rose_sk(sk);
40 
41 	switch (frametype) {
42 	case ROSE_CALL_ACCEPTED:
43 		rose_stop_timer(sk);
44 		rose_start_idletimer(sk);
45 		rose->condition = 0x00;
46 		rose->vs        = 0;
47 		rose->va        = 0;
48 		rose->vr        = 0;
49 		rose->vl        = 0;
50 		rose->state     = ROSE_STATE_3;
51 		sk->sk_state	= TCP_ESTABLISHED;
52 		if (!sock_flag(sk, SOCK_DEAD))
53 			sk->sk_state_change(sk);
54 		break;
55 
56 	case ROSE_CLEAR_REQUEST:
57 		rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
58 		rose_disconnect(sk, ECONNREFUSED, skb->data[3], skb->data[4]);
59 		rose->neighbour->use--;
60 		break;
61 
62 	default:
63 		break;
64 	}
65 
66 	return 0;
67 }
68 
69 /*
70  * State machine for state 2, Awaiting Clear Confirmation State.
71  * The handling of the timer(s) is in file rose_timer.c
72  * Handling of state 0 and connection release is in af_rose.c.
73  */
rose_state2_machine(struct sock * sk,struct sk_buff * skb,int frametype)74 static int rose_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype)
75 {
76 	struct rose_sock *rose = rose_sk(sk);
77 
78 	switch (frametype) {
79 	case ROSE_CLEAR_REQUEST:
80 		rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
81 		rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
82 		rose->neighbour->use--;
83 		break;
84 
85 	case ROSE_CLEAR_CONFIRMATION:
86 		rose_disconnect(sk, 0, -1, -1);
87 		rose->neighbour->use--;
88 		break;
89 
90 	default:
91 		break;
92 	}
93 
94 	return 0;
95 }
96 
97 /*
98  * State machine for state 3, Connected State.
99  * The handling of the timer(s) is in file rose_timer.c
100  * Handling of state 0 and connection release is in af_rose.c.
101  */
rose_state3_machine(struct sock * sk,struct sk_buff * skb,int frametype,int ns,int nr,int q,int d,int m)102 static int rose_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype, int ns, int nr, int q, int d, int m)
103 {
104 	enum skb_drop_reason dr; /* ignored */
105 	struct rose_sock *rose = rose_sk(sk);
106 	int queued = 0;
107 
108 	switch (frametype) {
109 	case ROSE_RESET_REQUEST:
110 		rose_stop_timer(sk);
111 		rose_start_idletimer(sk);
112 		rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
113 		rose->condition = 0x00;
114 		rose->vs        = 0;
115 		rose->vr        = 0;
116 		rose->va        = 0;
117 		rose->vl        = 0;
118 		rose_requeue_frames(sk);
119 		break;
120 
121 	case ROSE_CLEAR_REQUEST:
122 		rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
123 		rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
124 		rose->neighbour->use--;
125 		break;
126 
127 	case ROSE_RR:
128 	case ROSE_RNR:
129 		if (!rose_validate_nr(sk, nr)) {
130 			rose_write_internal(sk, ROSE_RESET_REQUEST);
131 			rose->condition = 0x00;
132 			rose->vs        = 0;
133 			rose->vr        = 0;
134 			rose->va        = 0;
135 			rose->vl        = 0;
136 			rose->state     = ROSE_STATE_4;
137 			rose_start_t2timer(sk);
138 			rose_stop_idletimer(sk);
139 		} else {
140 			rose_frames_acked(sk, nr);
141 			if (frametype == ROSE_RNR) {
142 				rose->condition |= ROSE_COND_PEER_RX_BUSY;
143 			} else {
144 				rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
145 			}
146 		}
147 		break;
148 
149 	case ROSE_DATA:	/* XXX */
150 		rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
151 		if (!rose_validate_nr(sk, nr)) {
152 			rose_write_internal(sk, ROSE_RESET_REQUEST);
153 			rose->condition = 0x00;
154 			rose->vs        = 0;
155 			rose->vr        = 0;
156 			rose->va        = 0;
157 			rose->vl        = 0;
158 			rose->state     = ROSE_STATE_4;
159 			rose_start_t2timer(sk);
160 			rose_stop_idletimer(sk);
161 			break;
162 		}
163 		rose_frames_acked(sk, nr);
164 		if (ns == rose->vr) {
165 			rose_start_idletimer(sk);
166 			if (!sk_filter_trim_cap(sk, skb, ROSE_MIN_LEN, &dr) &&
167 			    __sock_queue_rcv_skb(sk, skb) == 0) {
168 				rose->vr = (rose->vr + 1) % ROSE_MODULUS;
169 				queued = 1;
170 			} else {
171 				/* Should never happen ! */
172 				rose_write_internal(sk, ROSE_RESET_REQUEST);
173 				rose->condition = 0x00;
174 				rose->vs        = 0;
175 				rose->vr        = 0;
176 				rose->va        = 0;
177 				rose->vl        = 0;
178 				rose->state     = ROSE_STATE_4;
179 				rose_start_t2timer(sk);
180 				rose_stop_idletimer(sk);
181 				break;
182 			}
183 			if (atomic_read(&sk->sk_rmem_alloc) >
184 			    (sk->sk_rcvbuf >> 1))
185 				rose->condition |= ROSE_COND_OWN_RX_BUSY;
186 		}
187 		/*
188 		 * If the window is full, ack the frame, else start the
189 		 * acknowledge hold back timer.
190 		 */
191 		if (((rose->vl + sysctl_rose_window_size) % ROSE_MODULUS) == rose->vr) {
192 			rose->condition &= ~ROSE_COND_ACK_PENDING;
193 			rose_stop_timer(sk);
194 			rose_enquiry_response(sk);
195 		} else {
196 			rose->condition |= ROSE_COND_ACK_PENDING;
197 			rose_start_hbtimer(sk);
198 		}
199 		break;
200 
201 	default:
202 		printk(KERN_WARNING "ROSE: unknown %02X in state 3\n", frametype);
203 		break;
204 	}
205 
206 	return queued;
207 }
208 
209 /*
210  * State machine for state 4, Awaiting Reset Confirmation State.
211  * The handling of the timer(s) is in file rose_timer.c
212  * Handling of state 0 and connection release is in af_rose.c.
213  */
rose_state4_machine(struct sock * sk,struct sk_buff * skb,int frametype)214 static int rose_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype)
215 {
216 	struct rose_sock *rose = rose_sk(sk);
217 
218 	switch (frametype) {
219 	case ROSE_RESET_REQUEST:
220 		rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
221 		fallthrough;
222 	case ROSE_RESET_CONFIRMATION:
223 		rose_stop_timer(sk);
224 		rose_start_idletimer(sk);
225 		rose->condition = 0x00;
226 		rose->va        = 0;
227 		rose->vr        = 0;
228 		rose->vs        = 0;
229 		rose->vl        = 0;
230 		rose->state     = ROSE_STATE_3;
231 		rose_requeue_frames(sk);
232 		break;
233 
234 	case ROSE_CLEAR_REQUEST:
235 		rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
236 		rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
237 		rose->neighbour->use--;
238 		break;
239 
240 	default:
241 		break;
242 	}
243 
244 	return 0;
245 }
246 
247 /*
248  * State machine for state 5, Awaiting Call Acceptance State.
249  * The handling of the timer(s) is in file rose_timer.c
250  * Handling of state 0 and connection release is in af_rose.c.
251  */
rose_state5_machine(struct sock * sk,struct sk_buff * skb,int frametype)252 static int rose_state5_machine(struct sock *sk, struct sk_buff *skb, int frametype)
253 {
254 	if (frametype == ROSE_CLEAR_REQUEST) {
255 		rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
256 		rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
257 		rose_sk(sk)->neighbour->use--;
258 	}
259 
260 	return 0;
261 }
262 
263 /* Higher level upcall for a LAPB frame */
rose_process_rx_frame(struct sock * sk,struct sk_buff * skb)264 int rose_process_rx_frame(struct sock *sk, struct sk_buff *skb)
265 {
266 	struct rose_sock *rose = rose_sk(sk);
267 	int queued = 0, frametype, ns, nr, q, d, m;
268 
269 	if (rose->state == ROSE_STATE_0)
270 		return 0;
271 
272 	frametype = rose_decode(skb, &ns, &nr, &q, &d, &m);
273 
274 	switch (rose->state) {
275 	case ROSE_STATE_1:
276 		queued = rose_state1_machine(sk, skb, frametype);
277 		break;
278 	case ROSE_STATE_2:
279 		queued = rose_state2_machine(sk, skb, frametype);
280 		break;
281 	case ROSE_STATE_3:
282 		queued = rose_state3_machine(sk, skb, frametype, ns, nr, q, d, m);
283 		break;
284 	case ROSE_STATE_4:
285 		queued = rose_state4_machine(sk, skb, frametype);
286 		break;
287 	case ROSE_STATE_5:
288 		queued = rose_state5_machine(sk, skb, frametype);
289 		break;
290 	}
291 
292 	rose_kick(sk);
293 
294 	return queued;
295 }
296