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/sched.h>
15 #include <linux/timer.h>
16 #include <linux/string.h>
17 #include <linux/sockios.h>
18 #include <linux/net.h>
20 #include <linux/inet.h>
21 #include <linux/netdevice.h>
22 #include <linux/skbuff.h>
25 #include <asm/system.h>
26 #include <linux/fcntl.h>
28 #include <linux/interrupt.h>
32 * This routine purges all of the queues of frames.
34 void rose_clear_queues(struct sock *sk)
36 skb_queue_purge(&sk->sk_write_queue);
37 skb_queue_purge(&rose_sk(sk)->ack_queue);
41 * This routine purges the input queue of those frames that have been
42 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
45 void rose_frames_acked(struct sock *sk, unsigned short nr)
48 rose_cb *rose = rose_sk(sk);
51 * Remove all the ack-ed frames from the ack queue.
54 while (skb_peek(&rose->ack_queue) != NULL && rose->va != nr) {
55 skb = skb_dequeue(&rose->ack_queue);
57 rose->va = (rose->va + 1) % ROSE_MODULUS;
62 void rose_requeue_frames(struct sock *sk)
64 struct sk_buff *skb, *skb_prev = NULL;
67 * Requeue all the un-ack-ed frames on the output queue to be picked
68 * up by rose_kick. This arrangement handles the possibility of an
71 while ((skb = skb_dequeue(&rose_sk(sk)->ack_queue)) != NULL) {
73 skb_queue_head(&sk->sk_write_queue, skb);
75 skb_append(skb_prev, skb);
81 * Validate that the value of nr is between va and vs. Return true or
84 int rose_validate_nr(struct sock *sk, unsigned short nr)
86 rose_cb *rose = rose_sk(sk);
87 unsigned short vc = rose->va;
89 while (vc != rose->vs) {
90 if (nr == vc) return 1;
91 vc = (vc + 1) % ROSE_MODULUS;
94 return nr == rose->vs;
98 * This routine is called when the packet layer internally generates a
101 void rose_write_internal(struct sock *sk, int frametype)
103 rose_cb *rose = rose_sk(sk);
106 unsigned char lci1, lci2;
110 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 1;
113 case ROSE_CALL_REQUEST:
114 len += 1 + ROSE_ADDR_LEN + ROSE_ADDR_LEN;
115 faclen = rose_create_facilities(buffer, rose);
118 case ROSE_CALL_ACCEPTED:
119 case ROSE_CLEAR_REQUEST:
120 case ROSE_RESET_REQUEST:
125 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
129 * Space for AX.25 header and PID.
131 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 1);
133 dptr = skb_put(skb, skb_tailroom(skb));
135 lci1 = (rose->lci >> 8) & 0x0F;
136 lci2 = (rose->lci >> 0) & 0xFF;
139 case ROSE_CALL_REQUEST:
140 *dptr++ = ROSE_GFI | lci1;
144 memcpy(dptr, &rose->dest_addr, ROSE_ADDR_LEN);
145 dptr += ROSE_ADDR_LEN;
146 memcpy(dptr, &rose->source_addr, ROSE_ADDR_LEN);
147 dptr += ROSE_ADDR_LEN;
148 memcpy(dptr, buffer, faclen);
152 case ROSE_CALL_ACCEPTED:
153 *dptr++ = ROSE_GFI | lci1;
156 *dptr++ = 0x00; /* Address length */
157 *dptr++ = 0; /* Facilities length */
160 case ROSE_CLEAR_REQUEST:
161 *dptr++ = ROSE_GFI | lci1;
164 *dptr++ = rose->cause;
165 *dptr++ = rose->diagnostic;
168 case ROSE_RESET_REQUEST:
169 *dptr++ = ROSE_GFI | lci1;
172 *dptr++ = ROSE_DTE_ORIGINATED;
178 *dptr++ = ROSE_GFI | lci1;
181 *dptr++ |= (rose->vr << 5) & 0xE0;
184 case ROSE_CLEAR_CONFIRMATION:
185 case ROSE_RESET_CONFIRMATION:
186 *dptr++ = ROSE_GFI | lci1;
192 printk(KERN_ERR "ROSE: rose_write_internal - invalid frametype %02X\n", frametype);
197 rose_transmit_link(skb, rose->neighbour);
200 int rose_decode(struct sk_buff *skb, int *ns, int *nr, int *q, int *d, int *m)
202 unsigned char *frame;
206 *ns = *nr = *q = *d = *m = 0;
209 case ROSE_CALL_REQUEST:
210 case ROSE_CALL_ACCEPTED:
211 case ROSE_CLEAR_REQUEST:
212 case ROSE_CLEAR_CONFIRMATION:
213 case ROSE_RESET_REQUEST:
214 case ROSE_RESET_CONFIRMATION:
220 if ((frame[2] & 0x1F) == ROSE_RR ||
221 (frame[2] & 0x1F) == ROSE_RNR) {
222 *nr = (frame[2] >> 5) & 0x07;
223 return frame[2] & 0x1F;
226 if ((frame[2] & 0x01) == ROSE_DATA) {
227 *q = (frame[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
228 *d = (frame[0] & ROSE_D_BIT) == ROSE_D_BIT;
229 *m = (frame[2] & ROSE_M_BIT) == ROSE_M_BIT;
230 *nr = (frame[2] >> 5) & 0x07;
231 *ns = (frame[2] >> 1) & 0x07;
238 static int rose_parse_national(unsigned char *p, struct rose_facilities_struct *facilities, int len)
241 unsigned char l, lg, n = 0;
242 int fac_national_digis_received = 0;
253 if (*p == FAC_NATIONAL_RAND)
254 facilities->rand = ((p[1] << 8) & 0xFF00) + ((p[2] << 0) & 0x00FF);
268 if (*p == FAC_NATIONAL_DEST_DIGI) {
269 if (!fac_national_digis_received) {
270 memcpy(&facilities->source_digis[0], p + 2, AX25_ADDR_LEN);
271 facilities->source_ndigis = 1;
274 else if (*p == FAC_NATIONAL_SRC_DIGI) {
275 if (!fac_national_digis_received) {
276 memcpy(&facilities->dest_digis[0], p + 2, AX25_ADDR_LEN);
277 facilities->dest_ndigis = 1;
280 else if (*p == FAC_NATIONAL_FAIL_CALL) {
281 memcpy(&facilities->fail_call, p + 2, AX25_ADDR_LEN);
283 else if (*p == FAC_NATIONAL_FAIL_ADD) {
284 memcpy(&facilities->fail_addr, p + 3, ROSE_ADDR_LEN);
286 else if (*p == FAC_NATIONAL_DIGIS) {
287 fac_national_digis_received = 1;
288 facilities->source_ndigis = 0;
289 facilities->dest_ndigis = 0;
290 for (pt = p + 2, lg = 0 ; lg < l ; pt += AX25_ADDR_LEN, lg += AX25_ADDR_LEN) {
291 if (pt[6] & AX25_HBIT)
292 memcpy(&facilities->dest_digis[facilities->dest_ndigis++], pt, AX25_ADDR_LEN);
294 memcpy(&facilities->source_digis[facilities->source_ndigis++], pt, AX25_ADDR_LEN);
302 } while (*p != 0x00 && len > 0);
307 static int rose_parse_ccitt(unsigned char *p, struct rose_facilities_struct *facilities, int len)
309 unsigned char l, n = 0;
334 if (*p == FAC_CCITT_DEST_NSAP) {
335 memcpy(&facilities->source_addr, p + 7, ROSE_ADDR_LEN);
336 memcpy(callsign, p + 12, l - 10);
337 callsign[l - 10] = '\0';
338 facilities->source_call = *asc2ax(callsign);
340 if (*p == FAC_CCITT_SRC_NSAP) {
341 memcpy(&facilities->dest_addr, p + 7, ROSE_ADDR_LEN);
342 memcpy(callsign, p + 12, l - 10);
343 callsign[l - 10] = '\0';
344 facilities->dest_call = *asc2ax(callsign);
351 } while (*p != 0x00 && len > 0);
356 int rose_parse_facilities(unsigned char *p,
357 struct rose_facilities_struct *facilities)
359 int facilities_len, len;
361 facilities_len = *p++;
363 if (facilities_len == 0)
366 while (facilities_len > 0) {
372 case FAC_NATIONAL: /* National */
373 len = rose_parse_national(p + 1, facilities, facilities_len - 1);
374 facilities_len -= len + 1;
378 case FAC_CCITT: /* CCITT */
379 len = rose_parse_ccitt(p + 1, facilities, facilities_len - 1);
380 facilities_len -= len + 1;
385 printk(KERN_DEBUG "ROSE: rose_parse_facilities - unknown facilities family %02X\n", *p);
391 break; /* Error in facilities format */
397 int rose_create_facilities(unsigned char *buffer, rose_cb *rose)
399 unsigned char *p = buffer + 1;
403 /* National Facilities */
404 if (rose->rand != 0 || rose->source_ndigis == 1 || rose->dest_ndigis == 1) {
408 if (rose->rand != 0) {
409 *p++ = FAC_NATIONAL_RAND;
410 *p++ = (rose->rand >> 8) & 0xFF;
411 *p++ = (rose->rand >> 0) & 0xFF;
414 /* Sent before older facilities */
415 if ((rose->source_ndigis > 0) || (rose->dest_ndigis > 0)) {
417 *p++ = FAC_NATIONAL_DIGIS;
418 *p++ = AX25_ADDR_LEN * (rose->source_ndigis + rose->dest_ndigis);
419 for (nb = 0 ; nb < rose->source_ndigis ; nb++) {
420 if (++maxdigi >= ROSE_MAX_DIGIS)
422 memcpy(p, &rose->source_digis[nb], AX25_ADDR_LEN);
426 for (nb = 0 ; nb < rose->dest_ndigis ; nb++) {
427 if (++maxdigi >= ROSE_MAX_DIGIS)
429 memcpy(p, &rose->dest_digis[nb], AX25_ADDR_LEN);
435 /* For compatibility */
436 if (rose->source_ndigis > 0) {
437 *p++ = FAC_NATIONAL_SRC_DIGI;
438 *p++ = AX25_ADDR_LEN;
439 memcpy(p, &rose->source_digis[0], AX25_ADDR_LEN);
443 /* For compatibility */
444 if (rose->dest_ndigis > 0) {
445 *p++ = FAC_NATIONAL_DEST_DIGI;
446 *p++ = AX25_ADDR_LEN;
447 memcpy(p, &rose->dest_digis[0], AX25_ADDR_LEN);
455 *p++ = FAC_CCITT_DEST_NSAP;
457 callsign = ax2asc(&rose->dest_call);
459 *p++ = strlen(callsign) + 10;
460 *p++ = (strlen(callsign) + 9) * 2; /* ??? */
462 *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
463 *p++ = ROSE_ADDR_LEN * 2;
464 memcpy(p, &rose->dest_addr, ROSE_ADDR_LEN);
467 memcpy(p, callsign, strlen(callsign));
468 p += strlen(callsign);
470 *p++ = FAC_CCITT_SRC_NSAP;
472 callsign = ax2asc(&rose->source_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->source_addr, ROSE_ADDR_LEN);
482 memcpy(p, callsign, strlen(callsign));
483 p += strlen(callsign);
491 void rose_disconnect(struct sock *sk, int reason, int cause, int diagnostic)
493 rose_cb *rose = rose_sk(sk);
496 rose_stop_idletimer(sk);
498 rose_clear_queues(sk);
501 rose->state = ROSE_STATE_0;
506 if (diagnostic != -1)
507 rose->diagnostic = diagnostic;
509 sk->sk_state = TCP_CLOSE;
511 sk->sk_shutdown |= SEND_SHUTDOWN;
513 if (!sock_flag(sk, SOCK_DEAD)) {
514 sk->sk_state_change(sk);
515 sock_set_flag(sk, SOCK_DEAD);