1 /*
2 * NET/ROM release 007
3 *
4 * This code REQUIRES 2.1.15 or higher/ NET3.038
5 *
6 * This module:
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.
11 *
12 * History
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.
16 */
17
18 #include <linux/errno.h>
19 #include <linux/types.h>
20 #include <linux/socket.h>
21 #include <linux/in.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>
28 #include <net/ax25.h>
29 #include <linux/inet.h>
30 #include <linux/netdevice.h>
31 #include <linux/skbuff.h>
32 #include <net/sock.h>
33 #include <asm/uaccess.h>
34 #include <asm/system.h>
35 #include <linux/fcntl.h>
36 #include <linux/mm.h>
37 #include <linux/interrupt.h>
38 #include <net/netrom.h>
39
40 /*
41 * This routine purges all of the queues of frames.
42 */
43 void nr_clear_queues(struct sock *sk)
44 {
45 struct sk_buff *skb;
46
47 while ((skb = skb_dequeue(&sk->write_queue)) != NULL)
48 kfree_skb(skb);
49
50 while ((skb = skb_dequeue(&sk->protinfo.nr->ack_queue)) != NULL)
51 kfree_skb(skb);
52
53 while ((skb = skb_dequeue(&sk->protinfo.nr->reseq_queue)) != NULL)
54 kfree_skb(skb);
55
56 while ((skb = skb_dequeue(&sk->protinfo.nr->frag_queue)) != NULL)
57 kfree_skb(skb);
58 }
59
60 /*
61 * This routine purges the input queue of those frames that have been
62 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
63 * SDL diagram.
64 */
65 void nr_frames_acked(struct sock *sk, unsigned short nr)
66 {
67 struct sk_buff *skb;
68
69 /*
70 * Remove all the ack-ed frames from the ack queue.
71 */
72 if (sk->protinfo.nr->va != nr) {
73 while (skb_peek(&sk->protinfo.nr->ack_queue) != NULL && sk->protinfo.nr->va != nr) {
74 skb = skb_dequeue(&sk->protinfo.nr->ack_queue);
75 kfree_skb(skb);
76 sk->protinfo.nr->va = (sk->protinfo.nr->va + 1) % NR_MODULUS;
77 }
78 }
79 }
80
81 /*
82 * Requeue all the un-ack-ed frames on the output queue to be picked
83 * up by nr_kick called from the timer. This arrangement handles the
84 * possibility of an empty output queue.
85 */
86 void nr_requeue_frames(struct sock *sk)
87 {
88 struct sk_buff *skb, *skb_prev = NULL;
89
90 while ((skb = skb_dequeue(&sk->protinfo.nr->ack_queue)) != NULL) {
91 if (skb_prev == NULL)
92 skb_queue_head(&sk->write_queue, skb);
93 else
94 skb_append(skb_prev, skb);
95 skb_prev = skb;
96 }
97 }
98
99 /*
100 * Validate that the value of nr is between va and vs. Return true or
101 * false for testing.
102 */
103 int nr_validate_nr(struct sock *sk, unsigned short nr)
104 {
105 unsigned short vc = sk->protinfo.nr->va;
106
107 while (vc != sk->protinfo.nr->vs) {
108 if (nr == vc) return 1;
109 vc = (vc + 1) % NR_MODULUS;
110 }
111
112 if (nr == sk->protinfo.nr->vs) return 1;
113
114 return 0;
115 }
116
117 /*
118 * Check that ns is within the receive window.
119 */
120 int nr_in_rx_window(struct sock *sk, unsigned short ns)
121 {
122 unsigned short vc = sk->protinfo.nr->vr;
123 unsigned short vt = (sk->protinfo.nr->vl + sk->protinfo.nr->window) % NR_MODULUS;
124
125 while (vc != vt) {
126 if (ns == vc) return 1;
127 vc = (vc + 1) % NR_MODULUS;
128 }
129
130 return 0;
131 }
132
133 /*
134 * This routine is called when the HDLC layer internally generates a
135 * control frame.
136 */
137 void nr_write_internal(struct sock *sk, int frametype)
138 {
139 struct sk_buff *skb;
140 unsigned char *dptr;
141 int len, timeout;
142
143 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
144
145 switch (frametype & 0x0F) {
146 case NR_CONNREQ:
147 len += 17;
148 break;
149 case NR_CONNACK:
150 len += (sk->protinfo.nr->bpqext) ? 2 : 1;
151 break;
152 case NR_DISCREQ:
153 case NR_DISCACK:
154 case NR_INFOACK:
155 break;
156 default:
157 printk(KERN_ERR "NET/ROM: nr_write_internal - invalid frame type %d\n", frametype);
158 return;
159 }
160
161 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
162 return;
163
164 /*
165 * Space for AX.25 and NET/ROM network header
166 */
167 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + NR_NETWORK_LEN);
168
169 dptr = skb_put(skb, skb_tailroom(skb));
170
171 switch (frametype & 0x0F) {
172
173 case NR_CONNREQ:
174 timeout = sk->protinfo.nr->t1 / HZ;
175 *dptr++ = sk->protinfo.nr->my_index;
176 *dptr++ = sk->protinfo.nr->my_id;
177 *dptr++ = 0;
178 *dptr++ = 0;
179 *dptr++ = frametype;
180 *dptr++ = sk->protinfo.nr->window;
181 memcpy(dptr, &sk->protinfo.nr->user_addr, AX25_ADDR_LEN);
182 dptr[6] &= ~AX25_CBIT;
183 dptr[6] &= ~AX25_EBIT;
184 dptr[6] |= AX25_SSSID_SPARE;
185 dptr += AX25_ADDR_LEN;
186 memcpy(dptr, &sk->protinfo.nr->source_addr, AX25_ADDR_LEN);
187 dptr[6] &= ~AX25_CBIT;
188 dptr[6] &= ~AX25_EBIT;
189 dptr[6] |= AX25_SSSID_SPARE;
190 dptr += AX25_ADDR_LEN;
191 *dptr++ = timeout % 256;
192 *dptr++ = timeout / 256;
193 break;
194
195 case NR_CONNACK:
196 *dptr++ = sk->protinfo.nr->your_index;
197 *dptr++ = sk->protinfo.nr->your_id;
198 *dptr++ = sk->protinfo.nr->my_index;
199 *dptr++ = sk->protinfo.nr->my_id;
200 *dptr++ = frametype;
201 *dptr++ = sk->protinfo.nr->window;
202 if (sk->protinfo.nr->bpqext) *dptr++ = sysctl_netrom_network_ttl_initialiser;
203 break;
204
205 case NR_DISCREQ:
206 case NR_DISCACK:
207 *dptr++ = sk->protinfo.nr->your_index;
208 *dptr++ = sk->protinfo.nr->your_id;
209 *dptr++ = 0;
210 *dptr++ = 0;
211 *dptr++ = frametype;
212 break;
213
214 case NR_INFOACK:
215 *dptr++ = sk->protinfo.nr->your_index;
216 *dptr++ = sk->protinfo.nr->your_id;
217 *dptr++ = 0;
218 *dptr++ = sk->protinfo.nr->vr;
219 *dptr++ = frametype;
220 break;
221 }
222
223 nr_transmit_buffer(sk, skb);
224 }
225
226 /*
227 * This routine is called when a Connect Acknowledge with the Choke Flag
228 * set is needed to refuse a connection.
229 */
230 void nr_transmit_refusal(struct sk_buff *skb, int mine)
231 {
232 struct sk_buff *skbn;
233 unsigned char *dptr;
234 int len;
235
236 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + NR_NETWORK_LEN + NR_TRANSPORT_LEN + 1;
237
238 if ((skbn = alloc_skb(len, GFP_ATOMIC)) == NULL)
239 return;
240
241 skb_reserve(skbn, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
242
243 dptr = skb_put(skbn, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
244
245 memcpy(dptr, skb->data + 7, AX25_ADDR_LEN);
246 dptr[6] &= ~AX25_CBIT;
247 dptr[6] &= ~AX25_EBIT;
248 dptr[6] |= AX25_SSSID_SPARE;
249 dptr += AX25_ADDR_LEN;
250
251 memcpy(dptr, skb->data + 0, AX25_ADDR_LEN);
252 dptr[6] &= ~AX25_CBIT;
253 dptr[6] |= AX25_EBIT;
254 dptr[6] |= AX25_SSSID_SPARE;
255 dptr += AX25_ADDR_LEN;
256
257 *dptr++ = sysctl_netrom_network_ttl_initialiser;
258
259 if (mine) {
260 *dptr++ = 0;
261 *dptr++ = 0;
262 *dptr++ = skb->data[15];
263 *dptr++ = skb->data[16];
264 } else {
265 *dptr++ = skb->data[15];
266 *dptr++ = skb->data[16];
267 *dptr++ = 0;
268 *dptr++ = 0;
269 }
270
271 *dptr++ = NR_CONNACK | NR_CHOKE_FLAG;
272 *dptr++ = 0;
273
274 if (!nr_route_frame(skbn, NULL))
275 kfree_skb(skbn);
276 }
277
278 void nr_disconnect(struct sock *sk, int reason)
279 {
280 nr_stop_t1timer(sk);
281 nr_stop_t2timer(sk);
282 nr_stop_t4timer(sk);
283 nr_stop_idletimer(sk);
284
285 nr_clear_queues(sk);
286
287 sk->protinfo.nr->state = NR_STATE_0;
288
289 sk->state = TCP_CLOSE;
290 sk->err = reason;
291 sk->shutdown |= SEND_SHUTDOWN;
292
293 if (!sk->dead)
294 sk->state_change(sk);
295
296 sk->dead = 1;
297 }
298
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.