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