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 (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
9 #include <linux/errno.h>
10 #include <linux/types.h>
11 #include <linux/socket.h>
13 #include <linux/kernel.h>
14 #include <linux/timer.h>
15 #include <linux/string.h>
16 #include <linux/sockios.h>
17 #include <linux/net.h>
18 #include <linux/slab.h>
20 #include <linux/inet.h>
21 #include <linux/netdevice.h>
22 #include <linux/skbuff.h>
24 #include <net/tcp_states.h>
25 #include <asm/system.h>
26 #include <linux/fcntl.h>
28 #include <linux/interrupt.h>
31 static int rose_create_facilities(unsigned char *buffer
, struct rose_sock
*rose
);
34 * This routine purges all of the queues of frames.
36 void rose_clear_queues(struct sock
*sk
)
38 skb_queue_purge(&sk
->sk_write_queue
);
39 skb_queue_purge(&rose_sk(sk
)->ack_queue
);
43 * This routine purges the input queue of those frames that have been
44 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
47 void rose_frames_acked(struct sock
*sk
, unsigned short nr
)
50 struct rose_sock
*rose
= rose_sk(sk
);
53 * Remove all the ack-ed frames from the ack queue.
56 while (skb_peek(&rose
->ack_queue
) != NULL
&& rose
->va
!= nr
) {
57 skb
= skb_dequeue(&rose
->ack_queue
);
59 rose
->va
= (rose
->va
+ 1) % ROSE_MODULUS
;
64 void rose_requeue_frames(struct sock
*sk
)
66 struct sk_buff
*skb
, *skb_prev
= NULL
;
69 * Requeue all the un-ack-ed frames on the output queue to be picked
70 * up by rose_kick. This arrangement handles the possibility of an
73 while ((skb
= skb_dequeue(&rose_sk(sk
)->ack_queue
)) != NULL
) {
75 skb_queue_head(&sk
->sk_write_queue
, skb
);
77 skb_append(skb_prev
, skb
, &sk
->sk_write_queue
);
83 * Validate that the value of nr is between va and vs. Return true or
86 int rose_validate_nr(struct sock
*sk
, unsigned short nr
)
88 struct rose_sock
*rose
= rose_sk(sk
);
89 unsigned short vc
= rose
->va
;
91 while (vc
!= rose
->vs
) {
92 if (nr
== vc
) return 1;
93 vc
= (vc
+ 1) % ROSE_MODULUS
;
96 return nr
== rose
->vs
;
100 * This routine is called when the packet layer internally generates a
103 void rose_write_internal(struct sock
*sk
, int frametype
)
105 struct rose_sock
*rose
= rose_sk(sk
);
108 unsigned char lci1
, lci2
;
112 len
= AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
+ ROSE_MIN_LEN
+ 1;
115 case ROSE_CALL_REQUEST
:
116 len
+= 1 + ROSE_ADDR_LEN
+ ROSE_ADDR_LEN
;
117 faclen
= rose_create_facilities(buffer
, rose
);
120 case ROSE_CALL_ACCEPTED
:
121 case ROSE_CLEAR_REQUEST
:
122 case ROSE_RESET_REQUEST
:
127 if ((skb
= alloc_skb(len
, GFP_ATOMIC
)) == NULL
)
131 * Space for AX.25 header and PID.
133 skb_reserve(skb
, AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
+ 1);
135 dptr
= skb_put(skb
, skb_tailroom(skb
));
137 lci1
= (rose
->lci
>> 8) & 0x0F;
138 lci2
= (rose
->lci
>> 0) & 0xFF;
141 case ROSE_CALL_REQUEST
:
142 *dptr
++ = ROSE_GFI
| lci1
;
145 *dptr
++ = ROSE_CALL_REQ_ADDR_LEN_VAL
;
146 memcpy(dptr
, &rose
->dest_addr
, ROSE_ADDR_LEN
);
147 dptr
+= ROSE_ADDR_LEN
;
148 memcpy(dptr
, &rose
->source_addr
, ROSE_ADDR_LEN
);
149 dptr
+= ROSE_ADDR_LEN
;
150 memcpy(dptr
, buffer
, faclen
);
154 case ROSE_CALL_ACCEPTED
:
155 *dptr
++ = ROSE_GFI
| lci1
;
158 *dptr
++ = 0x00; /* Address length */
159 *dptr
++ = 0; /* Facilities length */
162 case ROSE_CLEAR_REQUEST
:
163 *dptr
++ = ROSE_GFI
| lci1
;
166 *dptr
++ = rose
->cause
;
167 *dptr
++ = rose
->diagnostic
;
170 case ROSE_RESET_REQUEST
:
171 *dptr
++ = ROSE_GFI
| lci1
;
174 *dptr
++ = ROSE_DTE_ORIGINATED
;
180 *dptr
++ = ROSE_GFI
| lci1
;
183 *dptr
++ |= (rose
->vr
<< 5) & 0xE0;
186 case ROSE_CLEAR_CONFIRMATION
:
187 case ROSE_RESET_CONFIRMATION
:
188 *dptr
++ = ROSE_GFI
| lci1
;
194 printk(KERN_ERR
"ROSE: rose_write_internal - invalid frametype %02X\n", frametype
);
199 rose_transmit_link(skb
, rose
->neighbour
);
202 int rose_decode(struct sk_buff
*skb
, int *ns
, int *nr
, int *q
, int *d
, int *m
)
204 unsigned char *frame
;
208 *ns
= *nr
= *q
= *d
= *m
= 0;
211 case ROSE_CALL_REQUEST
:
212 case ROSE_CALL_ACCEPTED
:
213 case ROSE_CLEAR_REQUEST
:
214 case ROSE_CLEAR_CONFIRMATION
:
215 case ROSE_RESET_REQUEST
:
216 case ROSE_RESET_CONFIRMATION
:
222 if ((frame
[2] & 0x1F) == ROSE_RR
||
223 (frame
[2] & 0x1F) == ROSE_RNR
) {
224 *nr
= (frame
[2] >> 5) & 0x07;
225 return frame
[2] & 0x1F;
228 if ((frame
[2] & 0x01) == ROSE_DATA
) {
229 *q
= (frame
[0] & ROSE_Q_BIT
) == ROSE_Q_BIT
;
230 *d
= (frame
[0] & ROSE_D_BIT
) == ROSE_D_BIT
;
231 *m
= (frame
[2] & ROSE_M_BIT
) == ROSE_M_BIT
;
232 *nr
= (frame
[2] >> 5) & 0x07;
233 *ns
= (frame
[2] >> 1) & 0x07;
240 static int rose_parse_national(unsigned char *p
, struct rose_facilities_struct
*facilities
, int len
)
243 unsigned char l
, lg
, n
= 0;
244 int fac_national_digis_received
= 0;
259 if (*p
== FAC_NATIONAL_RAND
)
260 facilities
->rand
= ((p
[1] << 8) & 0xFF00) + ((p
[2] << 0) & 0x00FF);
280 if (*p
== FAC_NATIONAL_DEST_DIGI
) {
281 if (!fac_national_digis_received
) {
282 if (l
< AX25_ADDR_LEN
)
284 memcpy(&facilities
->source_digis
[0], p
+ 2, AX25_ADDR_LEN
);
285 facilities
->source_ndigis
= 1;
288 else if (*p
== FAC_NATIONAL_SRC_DIGI
) {
289 if (!fac_national_digis_received
) {
290 if (l
< AX25_ADDR_LEN
)
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 if (l
< AX25_ADDR_LEN
)
299 memcpy(&facilities
->fail_call
, p
+ 2, AX25_ADDR_LEN
);
301 else if (*p
== FAC_NATIONAL_FAIL_ADD
) {
302 if (l
< 1 + ROSE_ADDR_LEN
)
304 memcpy(&facilities
->fail_addr
, p
+ 3, ROSE_ADDR_LEN
);
306 else if (*p
== FAC_NATIONAL_DIGIS
) {
307 if (l
% AX25_ADDR_LEN
)
309 fac_national_digis_received
= 1;
310 facilities
->source_ndigis
= 0;
311 facilities
->dest_ndigis
= 0;
312 for (pt
= p
+ 2, lg
= 0 ; lg
< l
; pt
+= AX25_ADDR_LEN
, lg
+= AX25_ADDR_LEN
) {
313 if (pt
[6] & AX25_HBIT
) {
314 if (facilities
->dest_ndigis
>= ROSE_MAX_DIGIS
)
316 memcpy(&facilities
->dest_digis
[facilities
->dest_ndigis
++], pt
, AX25_ADDR_LEN
);
318 if (facilities
->source_ndigis
>= ROSE_MAX_DIGIS
)
320 memcpy(&facilities
->source_digis
[facilities
->source_ndigis
++], pt
, AX25_ADDR_LEN
);
329 } while (*p
!= 0x00 && len
> 0);
334 static int rose_parse_ccitt(unsigned char *p
, struct rose_facilities_struct
*facilities
, int len
)
336 unsigned char l
, n
= 0;
370 /* Prevent overflows*/
371 if (l
< 10 || l
> 20)
374 if (*p
== FAC_CCITT_DEST_NSAP
) {
375 memcpy(&facilities
->source_addr
, p
+ 7, ROSE_ADDR_LEN
);
376 memcpy(callsign
, p
+ 12, l
- 10);
377 callsign
[l
- 10] = '\0';
378 asc2ax(&facilities
->source_call
, callsign
);
380 if (*p
== FAC_CCITT_SRC_NSAP
) {
381 memcpy(&facilities
->dest_addr
, p
+ 7, ROSE_ADDR_LEN
);
382 memcpy(callsign
, p
+ 12, l
- 10);
383 callsign
[l
- 10] = '\0';
384 asc2ax(&facilities
->dest_call
, callsign
);
391 } while (*p
!= 0x00 && len
> 0);
396 int rose_parse_facilities(unsigned char *p
, unsigned packet_len
,
397 struct rose_facilities_struct
*facilities
)
399 int facilities_len
, len
;
401 facilities_len
= *p
++;
403 if (facilities_len
== 0 || (unsigned)facilities_len
> packet_len
)
406 while (facilities_len
>= 3 && *p
== 0x00) {
411 case FAC_NATIONAL
: /* National */
412 len
= rose_parse_national(p
+ 1, facilities
, facilities_len
- 1);
415 case FAC_CCITT
: /* CCITT */
416 len
= rose_parse_ccitt(p
+ 1, facilities
, facilities_len
- 1);
420 printk(KERN_DEBUG
"ROSE: rose_parse_facilities - unknown facilities family %02X\n", *p
);
427 if (WARN_ON(len
>= facilities_len
))
429 facilities_len
-= len
+ 1;
433 return facilities_len
== 0;
436 static int rose_create_facilities(unsigned char *buffer
, struct rose_sock
*rose
)
438 unsigned char *p
= buffer
+ 1;
443 /* National Facilities */
444 if (rose
->rand
!= 0 || rose
->source_ndigis
== 1 || rose
->dest_ndigis
== 1) {
448 if (rose
->rand
!= 0) {
449 *p
++ = FAC_NATIONAL_RAND
;
450 *p
++ = (rose
->rand
>> 8) & 0xFF;
451 *p
++ = (rose
->rand
>> 0) & 0xFF;
454 /* Sent before older facilities */
455 if ((rose
->source_ndigis
> 0) || (rose
->dest_ndigis
> 0)) {
457 *p
++ = FAC_NATIONAL_DIGIS
;
458 *p
++ = AX25_ADDR_LEN
* (rose
->source_ndigis
+ rose
->dest_ndigis
);
459 for (nb
= 0 ; nb
< rose
->source_ndigis
; nb
++) {
460 if (++maxdigi
>= ROSE_MAX_DIGIS
)
462 memcpy(p
, &rose
->source_digis
[nb
], AX25_ADDR_LEN
);
466 for (nb
= 0 ; nb
< rose
->dest_ndigis
; nb
++) {
467 if (++maxdigi
>= ROSE_MAX_DIGIS
)
469 memcpy(p
, &rose
->dest_digis
[nb
], AX25_ADDR_LEN
);
475 /* For compatibility */
476 if (rose
->source_ndigis
> 0) {
477 *p
++ = FAC_NATIONAL_SRC_DIGI
;
478 *p
++ = AX25_ADDR_LEN
;
479 memcpy(p
, &rose
->source_digis
[0], AX25_ADDR_LEN
);
483 /* For compatibility */
484 if (rose
->dest_ndigis
> 0) {
485 *p
++ = FAC_NATIONAL_DEST_DIGI
;
486 *p
++ = AX25_ADDR_LEN
;
487 memcpy(p
, &rose
->dest_digis
[0], AX25_ADDR_LEN
);
495 *p
++ = FAC_CCITT_DEST_NSAP
;
497 callsign
= ax2asc(buf
, &rose
->dest_call
);
499 *p
++ = strlen(callsign
) + 10;
500 *p
++ = (strlen(callsign
) + 9) * 2; /* ??? */
502 *p
++ = 0x47; *p
++ = 0x00; *p
++ = 0x11;
503 *p
++ = ROSE_ADDR_LEN
* 2;
504 memcpy(p
, &rose
->dest_addr
, ROSE_ADDR_LEN
);
507 memcpy(p
, callsign
, strlen(callsign
));
508 p
+= strlen(callsign
);
510 *p
++ = FAC_CCITT_SRC_NSAP
;
512 callsign
= ax2asc(buf
, &rose
->source_call
);
514 *p
++ = strlen(callsign
) + 10;
515 *p
++ = (strlen(callsign
) + 9) * 2; /* ??? */
517 *p
++ = 0x47; *p
++ = 0x00; *p
++ = 0x11;
518 *p
++ = ROSE_ADDR_LEN
* 2;
519 memcpy(p
, &rose
->source_addr
, ROSE_ADDR_LEN
);
522 memcpy(p
, callsign
, strlen(callsign
));
523 p
+= strlen(callsign
);
531 void rose_disconnect(struct sock
*sk
, int reason
, int cause
, int diagnostic
)
533 struct rose_sock
*rose
= rose_sk(sk
);
536 rose_stop_idletimer(sk
);
538 rose_clear_queues(sk
);
541 rose
->state
= ROSE_STATE_0
;
546 if (diagnostic
!= -1)
547 rose
->diagnostic
= diagnostic
;
549 sk
->sk_state
= TCP_CLOSE
;
551 sk
->sk_shutdown
|= SEND_SHUTDOWN
;
553 if (!sock_flag(sk
, SOCK_DEAD
)) {
554 sk
->sk_state_change(sk
);
555 sock_set_flag(sk
, SOCK_DEAD
);