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