4 * This code REQUIRES 2.1.15 or higher/ NET3.038
7 * This module is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 * NET/ROM 001 Jonathan(G4KLX) Cloned from ax25_subr.c
14 * NET/ROM 003 Jonathan(G4KLX) Added G8BPQ NET/ROM extensions.
15 * NET/ROM 007 Jonathan(G4KLX) New timer architecture.
18 #include <linux/errno.h>
19 #include <linux/types.h>
20 #include <linux/socket.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/timer.h>
25 #include <linux/string.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
29 #include <linux/inet.h>
30 #include <linux/netdevice.h>
31 #include <linux/skbuff.h>
33 #include <asm/uaccess.h>
34 #include <asm/system.h>
35 #include <linux/fcntl.h>
37 #include <linux/interrupt.h>
38 #include <net/netrom.h>
41 * This routine purges all of the queues of frames.
43 void nr_clear_queues(struct sock *sk)
45 skb_queue_purge(&sk->write_queue);
46 skb_queue_purge(&sk->protinfo.nr->ack_queue);
47 skb_queue_purge(&sk->protinfo.nr->reseq_queue);
48 skb_queue_purge(&sk->protinfo.nr->frag_queue);
52 * This routine purges the input queue of those frames that have been
53 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
56 void nr_frames_acked(struct sock *sk, unsigned short nr)
61 * Remove all the ack-ed frames from the ack queue.
63 if (sk->protinfo.nr->va != nr) {
64 while (skb_peek(&sk->protinfo.nr->ack_queue) != NULL && sk->protinfo.nr->va != nr) {
65 skb = skb_dequeue(&sk->protinfo.nr->ack_queue);
67 sk->protinfo.nr->va = (sk->protinfo.nr->va + 1) % NR_MODULUS;
73 * Requeue all the un-ack-ed frames on the output queue to be picked
74 * up by nr_kick called from the timer. This arrangement handles the
75 * possibility of an empty output queue.
77 void nr_requeue_frames(struct sock *sk)
79 struct sk_buff *skb, *skb_prev = NULL;
81 while ((skb = skb_dequeue(&sk->protinfo.nr->ack_queue)) != NULL) {
83 skb_queue_head(&sk->write_queue, skb);
85 skb_append(skb_prev, skb);
91 * Validate that the value of nr is between va and vs. Return true or
94 int nr_validate_nr(struct sock *sk, unsigned short nr)
96 unsigned short vc = sk->protinfo.nr->va;
98 while (vc != sk->protinfo.nr->vs) {
99 if (nr == vc) return 1;
100 vc = (vc + 1) % NR_MODULUS;
103 if (nr == sk->protinfo.nr->vs) return 1;
109 * Check that ns is within the receive window.
111 int nr_in_rx_window(struct sock *sk, unsigned short ns)
113 unsigned short vc = sk->protinfo.nr->vr;
114 unsigned short vt = (sk->protinfo.nr->vl + sk->protinfo.nr->window) % NR_MODULUS;
117 if (ns == vc) return 1;
118 vc = (vc + 1) % NR_MODULUS;
125 * This routine is called when the HDLC layer internally generates a
128 void nr_write_internal(struct sock *sk, int frametype)
134 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
136 switch (frametype & 0x0F) {
141 len += (sk->protinfo.nr->bpqext) ? 2 : 1;
148 printk(KERN_ERR "NET/ROM: nr_write_internal - invalid frame type %d\n", frametype);
152 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
156 * Space for AX.25 and NET/ROM network header
158 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + NR_NETWORK_LEN);
160 dptr = skb_put(skb, skb_tailroom(skb));
162 switch (frametype & 0x0F) {
165 timeout = sk->protinfo.nr->t1 / HZ;
166 *dptr++ = sk->protinfo.nr->my_index;
167 *dptr++ = sk->protinfo.nr->my_id;
171 *dptr++ = sk->protinfo.nr->window;
172 memcpy(dptr, &sk->protinfo.nr->user_addr, AX25_ADDR_LEN);
173 dptr[6] &= ~AX25_CBIT;
174 dptr[6] &= ~AX25_EBIT;
175 dptr[6] |= AX25_SSSID_SPARE;
176 dptr += AX25_ADDR_LEN;
177 memcpy(dptr, &sk->protinfo.nr->source_addr, AX25_ADDR_LEN);
178 dptr[6] &= ~AX25_CBIT;
179 dptr[6] &= ~AX25_EBIT;
180 dptr[6] |= AX25_SSSID_SPARE;
181 dptr += AX25_ADDR_LEN;
182 *dptr++ = timeout % 256;
183 *dptr++ = timeout / 256;
187 *dptr++ = sk->protinfo.nr->your_index;
188 *dptr++ = sk->protinfo.nr->your_id;
189 *dptr++ = sk->protinfo.nr->my_index;
190 *dptr++ = sk->protinfo.nr->my_id;
192 *dptr++ = sk->protinfo.nr->window;
193 if (sk->protinfo.nr->bpqext) *dptr++ = sysctl_netrom_network_ttl_initialiser;
198 *dptr++ = sk->protinfo.nr->your_index;
199 *dptr++ = sk->protinfo.nr->your_id;
206 *dptr++ = sk->protinfo.nr->your_index;
207 *dptr++ = sk->protinfo.nr->your_id;
209 *dptr++ = sk->protinfo.nr->vr;
214 nr_transmit_buffer(sk, skb);
218 * This routine is called when a Connect Acknowledge with the Choke Flag
219 * set is needed to refuse a connection.
221 void nr_transmit_refusal(struct sk_buff *skb, int mine)
223 struct sk_buff *skbn;
227 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + NR_NETWORK_LEN + NR_TRANSPORT_LEN + 1;
229 if ((skbn = alloc_skb(len, GFP_ATOMIC)) == NULL)
232 skb_reserve(skbn, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
234 dptr = skb_put(skbn, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
236 memcpy(dptr, skb->data + 7, AX25_ADDR_LEN);
237 dptr[6] &= ~AX25_CBIT;
238 dptr[6] &= ~AX25_EBIT;
239 dptr[6] |= AX25_SSSID_SPARE;
240 dptr += AX25_ADDR_LEN;
242 memcpy(dptr, skb->data + 0, AX25_ADDR_LEN);
243 dptr[6] &= ~AX25_CBIT;
244 dptr[6] |= AX25_EBIT;
245 dptr[6] |= AX25_SSSID_SPARE;
246 dptr += AX25_ADDR_LEN;
248 *dptr++ = sysctl_netrom_network_ttl_initialiser;
253 *dptr++ = skb->data[15];
254 *dptr++ = skb->data[16];
256 *dptr++ = skb->data[15];
257 *dptr++ = skb->data[16];
262 *dptr++ = NR_CONNACK | NR_CHOKE_FLAG;
265 if (!nr_route_frame(skbn, NULL))
269 void nr_disconnect(struct sock *sk, int reason)
274 nr_stop_idletimer(sk);
278 sk->protinfo.nr->state = NR_STATE_0;
280 sk->state = TCP_CLOSE;
282 sk->shutdown |= SEND_SHUTDOWN;
285 sk->state_change(sk);