Linux-2.6.12-rc2
[linux-2.6/next.git] / net / netrom / nr_in.c
blob9c44b379412630dd7b269142b5fd557fca59139a
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.
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.h>
26 #include <net/ip.h> /* For ip_rcv */
27 #include <asm/uaccess.h>
28 #include <asm/system.h>
29 #include <linux/fcntl.h>
30 #include <linux/mm.h>
31 #include <linux/interrupt.h>
32 #include <net/netrom.h>
34 static int nr_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more)
36 struct sk_buff *skbo, *skbn = skb;
37 struct nr_sock *nr = nr_sk(sk);
39 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
41 nr_start_idletimer(sk);
43 if (more) {
44 nr->fraglen += skb->len;
45 skb_queue_tail(&nr->frag_queue, skb);
46 return 0;
49 if (!more && nr->fraglen > 0) { /* End of fragment */
50 nr->fraglen += skb->len;
51 skb_queue_tail(&nr->frag_queue, skb);
53 if ((skbn = alloc_skb(nr->fraglen, GFP_ATOMIC)) == NULL)
54 return 1;
56 skbn->h.raw = skbn->data;
58 while ((skbo = skb_dequeue(&nr->frag_queue)) != NULL) {
59 memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len);
60 kfree_skb(skbo);
63 nr->fraglen = 0;
66 return sock_queue_rcv_skb(sk, skbn);
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.
74 static int nr_state1_machine(struct sock *sk, struct sk_buff *skb,
75 int frametype)
77 switch (frametype) {
78 case NR_CONNACK: {
79 struct nr_sock *nr = nr_sk(sk);
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;
98 case NR_CONNACK | NR_CHOKE_FLAG:
99 nr_disconnect(sk, ECONNREFUSED);
100 break;
102 default:
103 break;
105 return 0;
109 * State machine for state 2, Awaiting Release State.
110 * The handling of the timer(s) is in file nr_timer.c
111 * Handling of state 0 and connection release is in netrom.c.
113 static int nr_state2_machine(struct sock *sk, struct sk_buff *skb,
114 int frametype)
116 switch (frametype) {
117 case NR_CONNACK | NR_CHOKE_FLAG:
118 nr_disconnect(sk, ECONNRESET);
119 break;
121 case NR_DISCREQ:
122 nr_write_internal(sk, NR_DISCACK);
124 case NR_DISCACK:
125 nr_disconnect(sk, 0);
126 break;
128 default:
129 break;
131 return 0;
135 * State machine for state 3, Connected State.
136 * The handling of the timer(s) is in file nr_timer.c
137 * Handling of state 0 and connection release is in netrom.c.
139 static int nr_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype)
141 struct nr_sock *nrom = nr_sk(sk);
142 struct sk_buff_head temp_queue;
143 struct sk_buff *skbn;
144 unsigned short save_vr;
145 unsigned short nr, ns;
146 int queued = 0;
148 nr = skb->data[18];
149 ns = skb->data[17];
151 switch (frametype) {
152 case NR_CONNREQ:
153 nr_write_internal(sk, NR_CONNACK);
154 break;
156 case NR_DISCREQ:
157 nr_write_internal(sk, NR_DISCACK);
158 nr_disconnect(sk, 0);
159 break;
161 case NR_CONNACK | NR_CHOKE_FLAG:
162 case NR_DISCACK:
163 nr_disconnect(sk, ECONNRESET);
164 break;
166 case NR_INFOACK:
167 case NR_INFOACK | NR_CHOKE_FLAG:
168 case NR_INFOACK | NR_NAK_FLAG:
169 case NR_INFOACK | NR_NAK_FLAG | NR_CHOKE_FLAG:
170 if (frametype & NR_CHOKE_FLAG) {
171 nrom->condition |= NR_COND_PEER_RX_BUSY;
172 nr_start_t4timer(sk);
173 } else {
174 nrom->condition &= ~NR_COND_PEER_RX_BUSY;
175 nr_stop_t4timer(sk);
177 if (!nr_validate_nr(sk, nr)) {
178 break;
180 if (frametype & NR_NAK_FLAG) {
181 nr_frames_acked(sk, nr);
182 nr_send_nak_frame(sk);
183 } else {
184 if (nrom->condition & NR_COND_PEER_RX_BUSY) {
185 nr_frames_acked(sk, nr);
186 } else {
187 nr_check_iframes_acked(sk, nr);
190 break;
192 case NR_INFO:
193 case NR_INFO | NR_NAK_FLAG:
194 case NR_INFO | NR_CHOKE_FLAG:
195 case NR_INFO | NR_MORE_FLAG:
196 case NR_INFO | NR_NAK_FLAG | NR_CHOKE_FLAG:
197 case NR_INFO | NR_CHOKE_FLAG | NR_MORE_FLAG:
198 case NR_INFO | NR_NAK_FLAG | NR_MORE_FLAG:
199 case NR_INFO | NR_NAK_FLAG | NR_CHOKE_FLAG | NR_MORE_FLAG:
200 if (frametype & NR_CHOKE_FLAG) {
201 nrom->condition |= NR_COND_PEER_RX_BUSY;
202 nr_start_t4timer(sk);
203 } else {
204 nrom->condition &= ~NR_COND_PEER_RX_BUSY;
205 nr_stop_t4timer(sk);
207 if (nr_validate_nr(sk, nr)) {
208 if (frametype & NR_NAK_FLAG) {
209 nr_frames_acked(sk, nr);
210 nr_send_nak_frame(sk);
211 } else {
212 if (nrom->condition & NR_COND_PEER_RX_BUSY) {
213 nr_frames_acked(sk, nr);
214 } else {
215 nr_check_iframes_acked(sk, nr);
219 queued = 1;
220 skb_queue_head(&nrom->reseq_queue, skb);
221 if (nrom->condition & NR_COND_OWN_RX_BUSY)
222 break;
223 skb_queue_head_init(&temp_queue);
224 do {
225 save_vr = nrom->vr;
226 while ((skbn = skb_dequeue(&nrom->reseq_queue)) != NULL) {
227 ns = skbn->data[17];
228 if (ns == nrom->vr) {
229 if (nr_queue_rx_frame(sk, skbn, frametype & NR_MORE_FLAG) == 0) {
230 nrom->vr = (nrom->vr + 1) % NR_MODULUS;
231 } else {
232 nrom->condition |= NR_COND_OWN_RX_BUSY;
233 skb_queue_tail(&temp_queue, skbn);
235 } else if (nr_in_rx_window(sk, ns)) {
236 skb_queue_tail(&temp_queue, skbn);
237 } else {
238 kfree_skb(skbn);
241 while ((skbn = skb_dequeue(&temp_queue)) != NULL) {
242 skb_queue_tail(&nrom->reseq_queue, skbn);
244 } while (save_vr != nrom->vr);
246 * Window is full, ack it immediately.
248 if (((nrom->vl + nrom->window) % NR_MODULUS) == nrom->vr) {
249 nr_enquiry_response(sk);
250 } else {
251 if (!(nrom->condition & NR_COND_ACK_PENDING)) {
252 nrom->condition |= NR_COND_ACK_PENDING;
253 nr_start_t2timer(sk);
256 break;
258 default:
259 break;
261 return queued;
264 /* Higher level upcall for a LAPB frame - called with sk locked */
265 int nr_process_rx_frame(struct sock *sk, struct sk_buff *skb)
267 struct nr_sock *nr = nr_sk(sk);
268 int queued = 0, frametype;
270 if (nr->state == NR_STATE_0)
271 return 0;
273 frametype = skb->data[19];
275 switch (nr->state) {
276 case NR_STATE_1:
277 queued = nr_state1_machine(sk, skb, frametype);
278 break;
279 case NR_STATE_2:
280 queued = nr_state2_machine(sk, skb, frametype);
281 break;
282 case NR_STATE_3:
283 queued = nr_state3_machine(sk, skb, frametype);
284 break;
287 nr_kick(sk);
289 return queued;