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 nr_cb *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,
80 nr_cb *nr = nr_sk(sk);
83 nr_start_idletimer(sk);
84 nr->your_index = skb->data[17];
85 nr->your_id = skb->data[18];
90 nr->state = NR_STATE_3;
92 nr->window = skb->data[20];
93 sk->sk_state = TCP_ESTABLISHED;
94 if (!sock_flag(sk, SOCK_DEAD))
95 sk->sk_state_change(sk);
99 case NR_CONNACK | NR_CHOKE_FLAG:
100 nr_disconnect(sk, ECONNREFUSED);
112 * State machine for state 2, Awaiting Release State.
113 * The handling of the timer(s) is in file nr_timer.c
114 * Handling of state 0 and connection release is in netrom.c.
116 static int nr_state2_machine(struct sock *sk, struct sk_buff *skb,
121 case NR_CONNACK | NR_CHOKE_FLAG:
122 nr_disconnect(sk, ECONNRESET);
126 nr_write_internal(sk, NR_DISCACK);
129 nr_disconnect(sk, 0);
141 * State machine for state 3, Connected State.
142 * The handling of the timer(s) is in file nr_timer.c
143 * Handling of state 0 and connection release is in netrom.c.
145 static int nr_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype)
147 nr_cb *nrom = nr_sk(sk);
148 struct sk_buff_head temp_queue;
149 struct sk_buff *skbn;
150 unsigned short save_vr;
151 unsigned short nr, ns;
160 nr_write_internal(sk, NR_CONNACK);
164 nr_write_internal(sk, NR_DISCACK);
165 nr_disconnect(sk, 0);
168 case NR_CONNACK | NR_CHOKE_FLAG:
170 nr_disconnect(sk, ECONNRESET);
174 case NR_INFOACK | NR_CHOKE_FLAG:
175 case NR_INFOACK | NR_NAK_FLAG:
176 case NR_INFOACK | NR_NAK_FLAG | NR_CHOKE_FLAG:
177 if (frametype & NR_CHOKE_FLAG) {
178 nrom->condition |= NR_COND_PEER_RX_BUSY;
179 nr_start_t4timer(sk);
181 nrom->condition &= ~NR_COND_PEER_RX_BUSY;
184 if (!nr_validate_nr(sk, nr)) {
187 if (frametype & NR_NAK_FLAG) {
188 nr_frames_acked(sk, nr);
189 nr_send_nak_frame(sk);
191 if (nrom->condition & NR_COND_PEER_RX_BUSY) {
192 nr_frames_acked(sk, nr);
194 nr_check_iframes_acked(sk, nr);
200 case NR_INFO | NR_NAK_FLAG:
201 case NR_INFO | NR_CHOKE_FLAG:
202 case NR_INFO | NR_MORE_FLAG:
203 case NR_INFO | NR_NAK_FLAG | NR_CHOKE_FLAG:
204 case NR_INFO | NR_CHOKE_FLAG | NR_MORE_FLAG:
205 case NR_INFO | NR_NAK_FLAG | NR_MORE_FLAG:
206 case NR_INFO | NR_NAK_FLAG | NR_CHOKE_FLAG | NR_MORE_FLAG:
207 if (frametype & NR_CHOKE_FLAG) {
208 nrom->condition |= NR_COND_PEER_RX_BUSY;
209 nr_start_t4timer(sk);
211 nrom->condition &= ~NR_COND_PEER_RX_BUSY;
214 if (nr_validate_nr(sk, nr)) {
215 if (frametype & NR_NAK_FLAG) {
216 nr_frames_acked(sk, nr);
217 nr_send_nak_frame(sk);
219 if (nrom->condition & NR_COND_PEER_RX_BUSY) {
220 nr_frames_acked(sk, nr);
222 nr_check_iframes_acked(sk, nr);
227 skb_queue_head(&nrom->reseq_queue, skb);
228 if (nrom->condition & NR_COND_OWN_RX_BUSY)
230 skb_queue_head_init(&temp_queue);
233 while ((skbn = skb_dequeue(&nrom->reseq_queue)) != NULL) {
235 if (ns == nrom->vr) {
236 if (nr_queue_rx_frame(sk, skbn, frametype & NR_MORE_FLAG) == 0) {
237 nrom->vr = (nrom->vr + 1) % NR_MODULUS;
239 nrom->condition |= NR_COND_OWN_RX_BUSY;
240 skb_queue_tail(&temp_queue, skbn);
242 } else if (nr_in_rx_window(sk, ns)) {
243 skb_queue_tail(&temp_queue, skbn);
248 while ((skbn = skb_dequeue(&temp_queue)) != NULL) {
249 skb_queue_tail(&nrom->reseq_queue, skbn);
251 } while (save_vr != nrom->vr);
253 * Window is full, ack it immediately.
255 if (((nrom->vl + nrom->window) % NR_MODULUS) == nrom->vr) {
256 nr_enquiry_response(sk);
258 if (!(nrom->condition & NR_COND_ACK_PENDING)) {
259 nrom->condition |= NR_COND_ACK_PENDING;
260 nr_start_t2timer(sk);
273 /* Higher level upcall for a LAPB frame */
274 int nr_process_rx_frame(struct sock *sk, struct sk_buff *skb)
276 nr_cb *nr = nr_sk(sk);
277 int queued = 0, frametype;
279 if (nr->state == NR_STATE_0)
282 frametype = skb->data[19];
286 queued = nr_state1_machine(sk, skb, frametype);
289 queued = nr_state2_machine(sk, skb, frametype);
292 queued = nr_state3_machine(sk, skb, frametype);