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)
10 #include <linux/errno.h>
11 #include <linux/types.h>
12 #include <linux/socket.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>
21 #include <linux/inet.h>
22 #include <linux/netdevice.h>
23 #include <linux/skbuff.h>
26 #include <net/ip.h> /* For ip_rcv */
27 #include <asm/uaccess.h>
28 #include <asm/system.h>
29 #include <linux/fcntl.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
);
44 nr
->fraglen
+= skb
->len
;
45 skb_queue_tail(&nr
->frag_queue
, skb
);
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
)
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
);
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
,
79 struct nr_sock
*nr
= nr_sk(sk
);
82 nr_start_idletimer(sk
);
83 nr
->your_index
= skb
->data
[17];
84 nr
->your_id
= skb
->data
[18];
89 nr
->state
= NR_STATE_3
;
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
);
98 case NR_CONNACK
| NR_CHOKE_FLAG
:
99 nr_disconnect(sk
, ECONNREFUSED
);
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
,
117 case NR_CONNACK
| NR_CHOKE_FLAG
:
118 nr_disconnect(sk
, ECONNRESET
);
122 nr_write_internal(sk
, NR_DISCACK
);
125 nr_disconnect(sk
, 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
;
153 nr_write_internal(sk
, NR_CONNACK
);
157 nr_write_internal(sk
, NR_DISCACK
);
158 nr_disconnect(sk
, 0);
161 case NR_CONNACK
| NR_CHOKE_FLAG
:
163 nr_disconnect(sk
, ECONNRESET
);
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
);
174 nrom
->condition
&= ~NR_COND_PEER_RX_BUSY
;
177 if (!nr_validate_nr(sk
, nr
)) {
180 if (frametype
& NR_NAK_FLAG
) {
181 nr_frames_acked(sk
, nr
);
182 nr_send_nak_frame(sk
);
184 if (nrom
->condition
& NR_COND_PEER_RX_BUSY
) {
185 nr_frames_acked(sk
, nr
);
187 nr_check_iframes_acked(sk
, nr
);
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
);
204 nrom
->condition
&= ~NR_COND_PEER_RX_BUSY
;
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
);
212 if (nrom
->condition
& NR_COND_PEER_RX_BUSY
) {
213 nr_frames_acked(sk
, nr
);
215 nr_check_iframes_acked(sk
, nr
);
220 skb_queue_head(&nrom
->reseq_queue
, skb
);
221 if (nrom
->condition
& NR_COND_OWN_RX_BUSY
)
223 skb_queue_head_init(&temp_queue
);
226 while ((skbn
= skb_dequeue(&nrom
->reseq_queue
)) != NULL
) {
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
;
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
);
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
);
251 if (!(nrom
->condition
& NR_COND_ACK_PENDING
)) {
252 nrom
->condition
|= NR_COND_ACK_PENDING
;
253 nr_start_t2timer(sk
);
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
)
273 frametype
= skb
->data
[19];
277 queued
= nr_state1_machine(sk
, skb
, frametype
);
280 queued
= nr_state2_machine(sk
, skb
, frametype
);
283 queued
= nr_state3_machine(sk
, skb
, frametype
);