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/errno.h>
19 #include <linux/types.h>
20 #include <linux/socket.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>
29 #include <linux/inet.h>
30 #include <linux/netdevice.h>
31 #include <linux/skbuff.h>
33 #include <asm/segment.h>
34 #include <asm/system.h>
35 #include <linux/fcntl.h>
37 #include <linux/interrupt.h>
41 * This routine purges all of the queues of frames.
43 void rose_clear_queues(struct sock *sk)
45 skb_queue_purge(&sk->write_queue);
46 skb_queue_purge(&sk->protinfo.rose->ack_queue);
50 * This routine purges the input queue of those frames that have been
51 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
54 void rose_frames_acked(struct sock *sk, unsigned short nr)
59 * Remove all the ack-ed frames from the ack queue.
61 if (sk->protinfo.rose->va != nr) {
62 while (skb_peek(&sk->protinfo.rose->ack_queue) != NULL && sk->protinfo.rose->va != nr) {
63 skb = skb_dequeue(&sk->protinfo.rose->ack_queue);
65 sk->protinfo.rose->va = (sk->protinfo.rose->va + 1) % ROSE_MODULUS;
70 void rose_requeue_frames(struct sock *sk)
72 struct sk_buff *skb, *skb_prev = NULL;
75 * Requeue all the un-ack-ed frames on the output queue to be picked
76 * up by rose_kick. This arrangement handles the possibility of an
79 while ((skb = skb_dequeue(&sk->protinfo.rose->ack_queue)) != NULL) {
81 skb_queue_head(&sk->write_queue, skb);
83 skb_append(skb_prev, skb);
89 * Validate that the value of nr is between va and vs. Return true or
92 int rose_validate_nr(struct sock *sk, unsigned short nr)
94 unsigned short vc = sk->protinfo.rose->va;
96 while (vc != sk->protinfo.rose->vs) {
97 if (nr == vc) return 1;
98 vc = (vc + 1) % ROSE_MODULUS;
101 if (nr == sk->protinfo.rose->vs) return 1;
107 * This routine is called when the packet layer internally generates a
110 void rose_write_internal(struct sock *sk, int frametype)
114 unsigned char lci1, lci2;
117 int ax25_header_len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 1;
119 len = ax25_header_len + ROSE_MIN_LEN;
122 case ROSE_CALL_REQUEST:
123 len += 1 + ROSE_ADDR_LEN + ROSE_ADDR_LEN;
124 faclen = rose_create_facilities(buffer, sk->protinfo.rose);
127 case ROSE_CALL_ACCEPTED:
128 case ROSE_RESET_REQUEST:
131 case ROSE_CLEAR_REQUEST:
134 faclen = 3 + 2 + AX25_ADDR_LEN + 3 + ROSE_ADDR_LEN;
136 *dptr++ = faclen-1; /* Facilities length */
138 *dptr++ = FAC_NATIONAL;
139 *dptr++ = FAC_NATIONAL_FAIL_CALL;
140 *dptr++ = AX25_ADDR_LEN;
141 memcpy(dptr, &rose_callsign, AX25_ADDR_LEN);
142 dptr += AX25_ADDR_LEN;
143 *dptr++ = FAC_NATIONAL_FAIL_ADD;
144 *dptr++ = ROSE_ADDR_LEN + 1;
145 *dptr++ = ROSE_ADDR_LEN * 2;
146 memcpy(dptr, &sk->protinfo.rose->source_addr, ROSE_ADDR_LEN);
151 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
155 * Space for AX.25 header and PID.
157 skb_reserve(skb, ax25_header_len);
159 dptr = skb_put(skb, len - ax25_header_len);
161 lci1 = (sk->protinfo.rose->lci >> 8) & 0x0F;
162 lci2 = (sk->protinfo.rose->lci >> 0) & 0xFF;
166 case ROSE_CALL_REQUEST:
167 *dptr++ = ROSE_GFI | lci1;
171 memcpy(dptr, &sk->protinfo.rose->dest_addr, ROSE_ADDR_LEN);
172 dptr += ROSE_ADDR_LEN;
173 memcpy(dptr, &sk->protinfo.rose->source_addr, ROSE_ADDR_LEN);
174 dptr += ROSE_ADDR_LEN;
175 memcpy(dptr, buffer, faclen);
179 case ROSE_CALL_ACCEPTED:
180 *dptr++ = ROSE_GFI | lci1;
183 *dptr++ = 0x00; /* Address length */
184 *dptr++ = 0; /* Facilities length */
187 case ROSE_CLEAR_REQUEST:
188 *dptr++ = ROSE_GFI | lci1;
191 *dptr++ = sk->protinfo.rose->cause;
192 *dptr++ = sk->protinfo.rose->diagnostic;
193 *dptr++ = 0x00; /* Address length */
194 memcpy(dptr, buffer, faclen);
198 case ROSE_RESET_REQUEST:
199 *dptr++ = ROSE_GFI | lci1;
202 *dptr++ = ROSE_DTE_ORIGINATED;
208 *dptr++ = ROSE_GFI | lci1;
211 *dptr++ |= (sk->protinfo.rose->vr << 5) & 0xE0;
214 case ROSE_CLEAR_CONFIRMATION:
215 case ROSE_RESET_CONFIRMATION:
216 *dptr++ = ROSE_GFI | lci1;
222 printk(KERN_ERR "ROSE: rose_write_internal - invalid frametype %02X\n", frametype);
227 rose_transmit_link(skb, sk->protinfo.rose->neighbour);
230 int rose_decode(struct sk_buff *skb, int *ns, int *nr, int *q, int *d, int *m)
232 unsigned char *frame;
236 *ns = *nr = *q = *d = *m = 0;
239 case ROSE_CALL_REQUEST:
240 case ROSE_CALL_ACCEPTED:
241 case ROSE_CLEAR_REQUEST:
242 case ROSE_CLEAR_CONFIRMATION:
243 case ROSE_RESET_REQUEST:
244 case ROSE_RESET_CONFIRMATION:
250 if ((frame[2] & 0x1F) == ROSE_RR ||
251 (frame[2] & 0x1F) == ROSE_RNR) {
252 *nr = (frame[2] >> 5) & 0x07;
253 return frame[2] & 0x1F;
256 if ((frame[2] & 0x01) == ROSE_DATA) {
257 *q = (frame[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
258 *d = (frame[0] & ROSE_D_BIT) == ROSE_D_BIT;
259 *m = (frame[2] & ROSE_M_BIT) == ROSE_M_BIT;
260 *nr = (frame[2] >> 5) & 0x07;
261 *ns = (frame[2] >> 1) & 0x07;
268 static int rose_parse_national(unsigned char *p, struct rose_facilities_struct *facilities, int len)
271 unsigned char l, lg, n = 0;
272 int fac_national_digis_received = 0;
283 if (*p == FAC_NATIONAL_RAND)
284 facilities->rand = ((p[1] << 8) & 0xFF00) + ((p[2] << 0) & 0x00FF);
298 if (*p == FAC_NATIONAL_DEST_DIGI) {
299 if (!fac_national_digis_received) {
300 memcpy(&facilities->source_digis[0], p + 2, AX25_ADDR_LEN);
301 facilities->source_ndigis = 1;
304 else if (*p == FAC_NATIONAL_SRC_DIGI) {
305 if (!fac_national_digis_received) {
306 memcpy(&facilities->dest_digis[0], p + 2, AX25_ADDR_LEN);
307 facilities->dest_ndigis = 1;
310 else if (*p == FAC_NATIONAL_FAIL_CALL) {
311 memcpy(&facilities->fail_call, p + 2, AX25_ADDR_LEN);
313 else if (*p == FAC_NATIONAL_FAIL_ADD) {
314 memcpy(&facilities->fail_addr, p + 3, ROSE_ADDR_LEN);
316 else if (*p == FAC_NATIONAL_DIGIS) {
317 fac_national_digis_received = 1;
318 facilities->source_ndigis = 0;
319 facilities->dest_ndigis = 0;
320 for (pt = p + 2, lg = 0 ; lg < l ; pt += AX25_ADDR_LEN, lg += AX25_ADDR_LEN) {
321 if (pt[6] & AX25_HBIT)
322 memcpy(&facilities->dest_digis[facilities->dest_ndigis++], pt, AX25_ADDR_LEN);
324 memcpy(&facilities->source_digis[facilities->source_ndigis++], pt, AX25_ADDR_LEN);
332 } while (*p != 0x00 && len > 0);
337 static int rose_parse_ccitt(unsigned char *p, struct rose_facilities_struct *facilities, int len)
339 unsigned char l, n = 0;
364 if (*p == FAC_CCITT_DEST_NSAP) {
365 memcpy(&facilities->source_addr, p + 7, ROSE_ADDR_LEN);
366 memcpy(callsign, p + 12, l - 10);
367 callsign[l - 10] = '\0';
368 facilities->source_call = *asc2ax(callsign);
370 if (*p == FAC_CCITT_SRC_NSAP) {
371 memcpy(&facilities->dest_addr, p + 7, ROSE_ADDR_LEN);
372 memcpy(callsign, p + 12, l - 10);
373 callsign[l - 10] = '\0';
374 facilities->dest_call = *asc2ax(callsign);
381 } while (*p != 0x00 && len > 0);
386 int rose_parse_facilities(unsigned char *p, struct rose_facilities_struct *facilities)
388 int facilities_len, len;
390 facilities_len = *p++;
392 if (facilities_len == 0)
395 while (facilities_len > 0) {
401 case FAC_NATIONAL: /* National */
402 len = rose_parse_national(p + 1, facilities, facilities_len - 1);
403 facilities_len -= len + 1;
407 case FAC_CCITT: /* CCITT */
408 len = rose_parse_ccitt(p + 1, facilities, facilities_len - 1);
409 facilities_len -= len + 1;
414 printk(KERN_DEBUG "ROSE: rose_parse_facilities - unknown facilities family %02X\n", *p);
420 else break; /* Error in facilities format */
426 int rose_create_facilities(unsigned char *buffer, rose_cb *rose)
428 unsigned char *p = buffer + 1;
432 /* National Facilities */
433 if (rose->rand != 0 || rose->source_ndigis == 1 || rose->dest_ndigis == 1) {
437 if (rose->rand != 0) {
438 *p++ = FAC_NATIONAL_RAND;
439 *p++ = (rose->rand >> 8) & 0xFF;
440 *p++ = (rose->rand >> 0) & 0xFF;
443 /* Sent before older facilities */
444 if ((rose->source_ndigis > 0) || (rose->dest_ndigis > 0)) {
446 *p++ = FAC_NATIONAL_DIGIS;
447 *p++ = AX25_ADDR_LEN * (rose->source_ndigis + rose->dest_ndigis);
448 for (nb = 0 ; nb < rose->source_ndigis ; nb++) {
449 if (++maxdigi >= ROSE_MAX_DIGIS)
451 memcpy(p, &rose->source_digis[nb], AX25_ADDR_LEN);
455 for (nb = 0 ; nb < rose->dest_ndigis ; nb++) {
456 if (++maxdigi >= ROSE_MAX_DIGIS)
458 memcpy(p, &rose->dest_digis[nb], AX25_ADDR_LEN);
464 /* For compatibility */
465 if (rose->source_ndigis > 0) {
466 *p++ = FAC_NATIONAL_SRC_DIGI;
467 *p++ = AX25_ADDR_LEN;
468 memcpy(p, &rose->source_digis[0], AX25_ADDR_LEN);
472 /* For compatibility */
473 if (rose->dest_ndigis > 0) {
474 *p++ = FAC_NATIONAL_DEST_DIGI;
475 *p++ = AX25_ADDR_LEN;
476 memcpy(p, &rose->dest_digis[0], AX25_ADDR_LEN);
484 *p++ = FAC_CCITT_DEST_NSAP;
486 callsign = ax2asc(&rose->dest_call);
488 *p++ = strlen(callsign) + 10;
489 *p++ = (strlen(callsign) + 9) * 2; /* ??? */
491 *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
492 *p++ = ROSE_ADDR_LEN * 2;
493 memcpy(p, &rose->dest_addr, ROSE_ADDR_LEN);
496 memcpy(p, callsign, strlen(callsign));
497 p += strlen(callsign);
499 *p++ = FAC_CCITT_SRC_NSAP;
501 callsign = ax2asc(&rose->source_call);
503 *p++ = strlen(callsign) + 10;
504 *p++ = (strlen(callsign) + 9) * 2; /* ??? */
506 *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
507 *p++ = ROSE_ADDR_LEN * 2;
508 memcpy(p, &rose->source_addr, ROSE_ADDR_LEN);
511 memcpy(p, callsign, strlen(callsign));
512 p += strlen(callsign);
520 void rose_disconnect(struct sock *sk, int reason, int cause, int diagnostic)
523 rose_stop_idletimer(sk);
525 rose_clear_queues(sk);
527 sk->protinfo.rose->lci = 0;
528 sk->protinfo.rose->state = ROSE_STATE_0;
531 sk->protinfo.rose->cause = cause;
533 if (diagnostic != -1)
534 sk->protinfo.rose->diagnostic = diagnostic;
536 sk->state = TCP_CLOSE;
538 sk->shutdown |= SEND_SHUTDOWN;
541 sk->state_change(sk);