Import 2.3.10pre5
[davej-history.git] / net / rose / rose_subr.c
blobdc172ac3be4e5ed1ec8365e337747176a7c7a5f1
1 /*
2 * ROSE release 003
4 * This code REQUIRES 2.1.15 or higher/ NET3.038
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.
12 * History
13 * ROSE 001 Jonathan(G4KLX) Cloned from nr_subr.c
14 * ROSE 002 Jonathan(G4KLX) Centralised disconnect processing.
15 * ROSE 003 Jonathan(G4KLX) Added use count to neighbours.
18 #include <linux/config.h>
19 #if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE)
20 #include <linux/errno.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/in.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
26 #include <linux/timer.h>
27 #include <linux/string.h>
28 #include <linux/sockios.h>
29 #include <linux/net.h>
30 #include <net/ax25.h>
31 #include <linux/inet.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
34 #include <net/sock.h>
35 #include <asm/segment.h>
36 #include <asm/system.h>
37 #include <linux/fcntl.h>
38 #include <linux/mm.h>
39 #include <linux/interrupt.h>
40 #include <net/rose.h>
43 * This routine purges all of the queues of frames.
45 void rose_clear_queues(struct sock *sk)
47 struct sk_buff *skb;
49 while ((skb = skb_dequeue(&sk->write_queue)) != NULL)
50 kfree_skb(skb);
52 while ((skb = skb_dequeue(&sk->protinfo.rose->ack_queue)) != NULL)
53 kfree_skb(skb);
57 * This routine purges the input queue of those frames that have been
58 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
59 * SDL diagram.
61 void rose_frames_acked(struct sock *sk, unsigned short nr)
63 struct sk_buff *skb;
66 * Remove all the ack-ed frames from the ack queue.
68 if (sk->protinfo.rose->va != nr) {
69 while (skb_peek(&sk->protinfo.rose->ack_queue) != NULL && sk->protinfo.rose->va != nr) {
70 skb = skb_dequeue(&sk->protinfo.rose->ack_queue);
71 kfree_skb(skb);
72 sk->protinfo.rose->va = (sk->protinfo.rose->va + 1) % ROSE_MODULUS;
77 void rose_requeue_frames(struct sock *sk)
79 struct sk_buff *skb, *skb_prev = NULL;
82 * Requeue all the un-ack-ed frames on the output queue to be picked
83 * up by rose_kick. This arrangement handles the possibility of an
84 * empty output queue.
86 while ((skb = skb_dequeue(&sk->protinfo.rose->ack_queue)) != NULL) {
87 if (skb_prev == NULL)
88 skb_queue_head(&sk->write_queue, skb);
89 else
90 skb_append(skb_prev, skb);
91 skb_prev = skb;
96 * Validate that the value of nr is between va and vs. Return true or
97 * false for testing.
99 int rose_validate_nr(struct sock *sk, unsigned short nr)
101 unsigned short vc = sk->protinfo.rose->va;
103 while (vc != sk->protinfo.rose->vs) {
104 if (nr == vc) return 1;
105 vc = (vc + 1) % ROSE_MODULUS;
108 if (nr == sk->protinfo.rose->vs) return 1;
110 return 0;
114 * This routine is called when the packet layer internally generates a
115 * control frame.
117 void rose_write_internal(struct sock *sk, int frametype)
119 struct sk_buff *skb;
120 unsigned char *dptr;
121 unsigned char lci1, lci2;
122 char buffer[100];
123 int len, faclen = 0;
125 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 1;
127 switch (frametype) {
128 case ROSE_CALL_REQUEST:
129 len += 1 + ROSE_ADDR_LEN + ROSE_ADDR_LEN;
130 faclen = rose_create_facilities(buffer, sk->protinfo.rose);
131 len += faclen;
132 break;
133 case ROSE_CALL_ACCEPTED:
134 case ROSE_CLEAR_REQUEST:
135 case ROSE_RESET_REQUEST:
136 len += 2;
137 break;
140 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
141 return;
144 * Space for AX.25 header and PID.
146 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 1);
148 dptr = skb_put(skb, skb_tailroom(skb));
150 lci1 = (sk->protinfo.rose->lci >> 8) & 0x0F;
151 lci2 = (sk->protinfo.rose->lci >> 0) & 0xFF;
153 switch (frametype) {
155 case ROSE_CALL_REQUEST:
156 *dptr++ = ROSE_GFI | lci1;
157 *dptr++ = lci2;
158 *dptr++ = frametype;
159 *dptr++ = 0xAA;
160 memcpy(dptr, &sk->protinfo.rose->dest_addr, ROSE_ADDR_LEN);
161 dptr += ROSE_ADDR_LEN;
162 memcpy(dptr, &sk->protinfo.rose->source_addr, ROSE_ADDR_LEN);
163 dptr += ROSE_ADDR_LEN;
164 memcpy(dptr, buffer, faclen);
165 dptr += faclen;
166 break;
168 case ROSE_CALL_ACCEPTED:
169 *dptr++ = ROSE_GFI | lci1;
170 *dptr++ = lci2;
171 *dptr++ = frametype;
172 *dptr++ = 0x00; /* Address length */
173 *dptr++ = 0; /* Facilities length */
174 break;
176 case ROSE_CLEAR_REQUEST:
177 *dptr++ = ROSE_GFI | lci1;
178 *dptr++ = lci2;
179 *dptr++ = frametype;
180 *dptr++ = sk->protinfo.rose->cause;
181 *dptr++ = sk->protinfo.rose->diagnostic;
182 break;
184 case ROSE_RESET_REQUEST:
185 *dptr++ = ROSE_GFI | lci1;
186 *dptr++ = lci2;
187 *dptr++ = frametype;
188 *dptr++ = ROSE_DTE_ORIGINATED;
189 *dptr++ = 0;
190 break;
192 case ROSE_RR:
193 case ROSE_RNR:
194 *dptr++ = ROSE_GFI | lci1;
195 *dptr++ = lci2;
196 *dptr = frametype;
197 *dptr++ |= (sk->protinfo.rose->vr << 5) & 0xE0;
198 break;
200 case ROSE_CLEAR_CONFIRMATION:
201 case ROSE_RESET_CONFIRMATION:
202 *dptr++ = ROSE_GFI | lci1;
203 *dptr++ = lci2;
204 *dptr++ = frametype;
205 break;
207 default:
208 printk(KERN_ERR "ROSE: rose_write_internal - invalid frametype %02X\n", frametype);
209 kfree_skb(skb);
210 return;
213 rose_transmit_link(skb, sk->protinfo.rose->neighbour);
216 int rose_decode(struct sk_buff *skb, int *ns, int *nr, int *q, int *d, int *m)
218 unsigned char *frame;
220 frame = skb->data;
222 *ns = *nr = *q = *d = *m = 0;
224 switch (frame[2]) {
225 case ROSE_CALL_REQUEST:
226 case ROSE_CALL_ACCEPTED:
227 case ROSE_CLEAR_REQUEST:
228 case ROSE_CLEAR_CONFIRMATION:
229 case ROSE_RESET_REQUEST:
230 case ROSE_RESET_CONFIRMATION:
231 return frame[2];
232 default:
233 break;
236 if ((frame[2] & 0x1F) == ROSE_RR ||
237 (frame[2] & 0x1F) == ROSE_RNR) {
238 *nr = (frame[2] >> 5) & 0x07;
239 return frame[2] & 0x1F;
242 if ((frame[2] & 0x01) == ROSE_DATA) {
243 *q = (frame[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
244 *d = (frame[0] & ROSE_D_BIT) == ROSE_D_BIT;
245 *m = (frame[2] & ROSE_M_BIT) == ROSE_M_BIT;
246 *nr = (frame[2] >> 5) & 0x07;
247 *ns = (frame[2] >> 1) & 0x07;
248 return ROSE_DATA;
251 return ROSE_ILLEGAL;
254 static int rose_parse_national(unsigned char *p, struct rose_facilities_struct *facilities, int len)
256 unsigned char *pt;
257 unsigned char l, lg, n = 0;
258 int fac_national_digis_received = 0;
260 do {
261 switch (*p & 0xC0) {
262 case 0x00:
263 p += 2;
264 n += 2;
265 len -= 2;
266 break;
268 case 0x40:
269 if (*p == FAC_NATIONAL_RAND)
270 facilities->rand = ((p[1] << 8) & 0xFF00) + ((p[2] << 0) & 0x00FF);
271 p += 3;
272 n += 3;
273 len -= 3;
274 break;
276 case 0x80:
277 p += 4;
278 n += 4;
279 len -= 4;
280 break;
282 case 0xC0:
283 l = p[1];
284 if (*p == FAC_NATIONAL_DEST_DIGI) {
285 if (!fac_national_digis_received) {
286 memcpy(&facilities->source_digis[0], p + 2, AX25_ADDR_LEN);
287 facilities->source_ndigis = 1;
290 else if (*p == FAC_NATIONAL_SRC_DIGI) {
291 if (!fac_national_digis_received) {
292 memcpy(&facilities->dest_digis[0], p + 2, AX25_ADDR_LEN);
293 facilities->dest_ndigis = 1;
296 else if (*p == FAC_NATIONAL_FAIL_CALL) {
297 memcpy(&facilities->fail_call, p + 2, AX25_ADDR_LEN);
299 else if (*p == FAC_NATIONAL_FAIL_ADD) {
300 memcpy(&facilities->fail_addr, p + 3, ROSE_ADDR_LEN);
302 else if (*p == FAC_NATIONAL_DIGIS) {
303 fac_national_digis_received = 1;
304 facilities->source_ndigis = 0;
305 facilities->dest_ndigis = 0;
306 for (pt = p + 2, lg = 0 ; lg < l ; pt += AX25_ADDR_LEN, lg += AX25_ADDR_LEN) {
307 if (pt[6] & AX25_HBIT)
308 memcpy(&facilities->dest_digis[facilities->dest_ndigis++], pt, AX25_ADDR_LEN);
309 else
310 memcpy(&facilities->source_digis[facilities->source_ndigis++], pt, AX25_ADDR_LEN);
313 p += l + 2;
314 n += l + 2;
315 len -= l + 2;
316 break;
318 } while (*p != 0x00 && len > 0);
320 return n;
323 static int rose_parse_ccitt(unsigned char *p, struct rose_facilities_struct *facilities, int len)
325 unsigned char l, n = 0;
326 char callsign[11];
328 do {
329 switch (*p & 0xC0) {
330 case 0x00:
331 p += 2;
332 n += 2;
333 len -= 2;
334 break;
336 case 0x40:
337 p += 3;
338 n += 3;
339 len -= 3;
340 break;
342 case 0x80:
343 p += 4;
344 n += 4;
345 len -= 4;
346 break;
348 case 0xC0:
349 l = p[1];
350 if (*p == FAC_CCITT_DEST_NSAP) {
351 memcpy(&facilities->source_addr, p + 7, ROSE_ADDR_LEN);
352 memcpy(callsign, p + 12, l - 10);
353 callsign[l - 10] = '\0';
354 facilities->source_call = *asc2ax(callsign);
356 if (*p == FAC_CCITT_SRC_NSAP) {
357 memcpy(&facilities->dest_addr, p + 7, ROSE_ADDR_LEN);
358 memcpy(callsign, p + 12, l - 10);
359 callsign[l - 10] = '\0';
360 facilities->dest_call = *asc2ax(callsign);
362 p += l + 2;
363 n += l + 2;
364 len -= l + 2;
365 break;
367 } while (*p != 0x00 && len > 0);
369 return n;
372 int rose_parse_facilities(unsigned char *p, struct rose_facilities_struct *facilities)
374 int facilities_len, len;
376 facilities_len = *p++;
378 if (facilities_len == 0)
379 return 0;
381 while (facilities_len > 0) {
382 if (*p == 0x00) {
383 facilities_len--;
384 p++;
386 switch (*p) {
387 case FAC_NATIONAL: /* National */
388 len = rose_parse_national(p + 1, facilities, facilities_len - 1);
389 facilities_len -= len + 1;
390 p += len + 1;
391 break;
393 case FAC_CCITT: /* CCITT */
394 len = rose_parse_ccitt(p + 1, facilities, facilities_len - 1);
395 facilities_len -= len + 1;
396 p += len + 1;
397 break;
399 default:
400 printk(KERN_DEBUG "ROSE: rose_parse_facilities - unknown facilities family %02X\n", *p);
401 facilities_len--;
402 p++;
403 break;
406 else break; /* Error in facilities format */
409 return 1;
412 int rose_create_facilities(unsigned char *buffer, rose_cb *rose)
414 unsigned char *p = buffer + 1;
415 char *callsign;
416 int len, nb;
418 /* National Facilities */
419 if (rose->rand != 0 || rose->source_ndigis == 1 || rose->dest_ndigis == 1) {
420 *p++ = 0x00;
421 *p++ = FAC_NATIONAL;
423 if (rose->rand != 0) {
424 *p++ = FAC_NATIONAL_RAND;
425 *p++ = (rose->rand >> 8) & 0xFF;
426 *p++ = (rose->rand >> 0) & 0xFF;
429 /* Sent before older facilities */
430 if ((rose->source_ndigis > 0) || (rose->dest_ndigis > 0)) {
431 int maxdigi = 0;
432 *p++ = FAC_NATIONAL_DIGIS;
433 *p++ = AX25_ADDR_LEN * (rose->source_ndigis + rose->dest_ndigis);
434 for (nb = 0 ; nb < rose->source_ndigis ; nb++) {
435 if (++maxdigi >= ROSE_MAX_DIGIS)
436 break;
437 memcpy(p, &rose->source_digis[nb], AX25_ADDR_LEN);
438 p[6] |= AX25_HBIT;
439 p += AX25_ADDR_LEN;
441 for (nb = 0 ; nb < rose->dest_ndigis ; nb++) {
442 if (++maxdigi >= ROSE_MAX_DIGIS)
443 break;
444 memcpy(p, &rose->dest_digis[nb], AX25_ADDR_LEN);
445 p[6] &= ~AX25_HBIT;
446 p += AX25_ADDR_LEN;
450 /* For compatibility */
451 if (rose->source_ndigis > 0) {
452 *p++ = FAC_NATIONAL_SRC_DIGI;
453 *p++ = AX25_ADDR_LEN;
454 memcpy(p, &rose->source_digis[0], AX25_ADDR_LEN);
455 p += AX25_ADDR_LEN;
458 /* For compatibility */
459 if (rose->dest_ndigis > 0) {
460 *p++ = FAC_NATIONAL_DEST_DIGI;
461 *p++ = AX25_ADDR_LEN;
462 memcpy(p, &rose->dest_digis[0], AX25_ADDR_LEN);
463 p += AX25_ADDR_LEN;
467 *p++ = 0x00;
468 *p++ = FAC_CCITT;
470 *p++ = FAC_CCITT_DEST_NSAP;
472 callsign = ax2asc(&rose->dest_call);
474 *p++ = strlen(callsign) + 10;
475 *p++ = (strlen(callsign) + 9) * 2; /* ??? */
477 *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
478 *p++ = ROSE_ADDR_LEN * 2;
479 memcpy(p, &rose->dest_addr, ROSE_ADDR_LEN);
480 p += ROSE_ADDR_LEN;
482 memcpy(p, callsign, strlen(callsign));
483 p += strlen(callsign);
485 *p++ = FAC_CCITT_SRC_NSAP;
487 callsign = ax2asc(&rose->source_call);
489 *p++ = strlen(callsign) + 10;
490 *p++ = (strlen(callsign) + 9) * 2; /* ??? */
492 *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
493 *p++ = ROSE_ADDR_LEN * 2;
494 memcpy(p, &rose->source_addr, ROSE_ADDR_LEN);
495 p += ROSE_ADDR_LEN;
497 memcpy(p, callsign, strlen(callsign));
498 p += strlen(callsign);
500 len = p - buffer;
501 buffer[0] = len - 1;
503 return len;
506 void rose_disconnect(struct sock *sk, int reason, int cause, int diagnostic)
508 rose_stop_timer(sk);
509 rose_stop_idletimer(sk);
511 rose_clear_queues(sk);
513 sk->protinfo.rose->lci = 0;
514 sk->protinfo.rose->state = ROSE_STATE_0;
516 if (cause != -1)
517 sk->protinfo.rose->cause = cause;
519 if (diagnostic != -1)
520 sk->protinfo.rose->diagnostic = diagnostic;
522 sk->state = TCP_CLOSE;
523 sk->err = reason;
524 sk->shutdown |= SEND_SHUTDOWN;
526 if (!sk->dead)
527 sk->state_change(sk);
529 sk->dead = 1;
532 #endif