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