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 * 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>
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>
31 #include <linux/inet.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
35 #include <asm/segment.h>
36 #include <asm/system.h>
37 #include <linux/fcntl.h>
39 #include <linux/interrupt.h>
43 * This routine purges all of the queues of frames.
45 void rose_clear_queues(struct sock
*sk
)
49 while ((skb
= skb_dequeue(&sk
->write_queue
)) != NULL
)
52 while ((skb
= skb_dequeue(&sk
->protinfo
.rose
->ack_queue
)) != NULL
)
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
61 void rose_frames_acked(struct sock
*sk
, unsigned short nr
)
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
);
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
86 while ((skb
= skb_dequeue(&sk
->protinfo
.rose
->ack_queue
)) != NULL
) {
88 skb_queue_head(&sk
->write_queue
, skb
);
90 skb_append(skb_prev
, skb
);
96 * Validate that the value of nr is between va and vs. Return true or
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;
114 * This routine is called when the packet layer internally generates a
117 void rose_write_internal(struct sock
*sk
, int frametype
)
121 unsigned char lci1
, lci2
;
125 len
= AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
+ ROSE_MIN_LEN
+ 1;
128 case ROSE_CALL_REQUEST
:
129 len
+= 1 + ROSE_ADDR_LEN
+ ROSE_ADDR_LEN
;
130 faclen
= rose_create_facilities(buffer
, sk
->protinfo
.rose
);
133 case ROSE_CALL_ACCEPTED
:
134 case ROSE_CLEAR_REQUEST
:
135 case ROSE_RESET_REQUEST
:
140 if ((skb
= alloc_skb(len
, GFP_ATOMIC
)) == NULL
)
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;
155 case ROSE_CALL_REQUEST
:
156 *dptr
++ = ROSE_GFI
| lci1
;
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
);
168 case ROSE_CALL_ACCEPTED
:
169 *dptr
++ = ROSE_GFI
| lci1
;
172 *dptr
++ = 0x00; /* Address length */
173 *dptr
++ = 0; /* Facilities length */
176 case ROSE_CLEAR_REQUEST
:
177 *dptr
++ = ROSE_GFI
| lci1
;
180 *dptr
++ = sk
->protinfo
.rose
->cause
;
181 *dptr
++ = sk
->protinfo
.rose
->diagnostic
;
184 case ROSE_RESET_REQUEST
:
185 *dptr
++ = ROSE_GFI
| lci1
;
188 *dptr
++ = ROSE_DTE_ORIGINATED
;
194 *dptr
++ = ROSE_GFI
| lci1
;
197 *dptr
++ |= (sk
->protinfo
.rose
->vr
<< 5) & 0xE0;
200 case ROSE_CLEAR_CONFIRMATION
:
201 case ROSE_RESET_CONFIRMATION
:
202 *dptr
++ = ROSE_GFI
| lci1
;
208 printk(KERN_ERR
"ROSE: rose_write_internal - invalid frametype %02X\n", frametype
);
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
;
222 *ns
= *nr
= *q
= *d
= *m
= 0;
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
:
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;
254 static int rose_parse_national(unsigned char *p
, struct rose_facilities_struct
*facilities
, int len
)
257 unsigned char l
, lg
, n
= 0;
258 int fac_national_digis_received
= 0;
269 if (*p
== FAC_NATIONAL_RAND
)
270 facilities
->rand
= ((p
[1] << 8) & 0xFF00) + ((p
[2] << 0) & 0x00FF);
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
);
310 memcpy(&facilities
->source_digis
[facilities
->source_ndigis
++], pt
, AX25_ADDR_LEN
);
318 } while (*p
!= 0x00 && len
> 0);
323 static int rose_parse_ccitt(unsigned char *p
, struct rose_facilities_struct
*facilities
, int len
)
325 unsigned char l
, n
= 0;
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
);
367 } while (*p
!= 0x00 && len
> 0);
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)
381 while (facilities_len
> 0) {
387 case FAC_NATIONAL
: /* National */
388 len
= rose_parse_national(p
+ 1, facilities
, facilities_len
- 1);
389 facilities_len
-= len
+ 1;
393 case FAC_CCITT
: /* CCITT */
394 len
= rose_parse_ccitt(p
+ 1, facilities
, facilities_len
- 1);
395 facilities_len
-= len
+ 1;
400 printk(KERN_DEBUG
"ROSE: rose_parse_facilities - unknown facilities family %02X\n", *p
);
406 else break; /* Error in facilities format */
412 int rose_create_facilities(unsigned char *buffer
, rose_cb
*rose
)
414 unsigned char *p
= buffer
+ 1;
418 /* National Facilities */
419 if (rose
->rand
!= 0 || rose
->source_ndigis
== 1 || rose
->dest_ndigis
== 1) {
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)) {
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
)
437 memcpy(p
, &rose
->source_digis
[nb
], AX25_ADDR_LEN
);
441 for (nb
= 0 ; nb
< rose
->dest_ndigis
; nb
++) {
442 if (++maxdigi
>= ROSE_MAX_DIGIS
)
444 memcpy(p
, &rose
->dest_digis
[nb
], 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
);
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
);
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
);
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
);
497 memcpy(p
, callsign
, strlen(callsign
));
498 p
+= strlen(callsign
);
506 void rose_disconnect(struct sock
*sk
, int reason
, int cause
, int diagnostic
)
509 rose_stop_idletimer(sk
);
511 rose_clear_queues(sk
);
513 sk
->protinfo
.rose
->lci
= 0;
514 sk
->protinfo
.rose
->state
= ROSE_STATE_0
;
517 sk
->protinfo
.rose
->cause
= cause
;
519 if (diagnostic
!= -1)
520 sk
->protinfo
.rose
->diagnostic
= diagnostic
;
522 sk
->state
= TCP_CLOSE
;
524 sk
->shutdown
|= SEND_SHUTDOWN
;
527 sk
->state_change(sk
);