OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uClinux-2.0.x/] [net/] [rose/] [rose_subr.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 *      ROSE release 003
3
 *
4
 *      This code REQUIRES 2.1.0 or higher/ NET3.029
5
 *
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.
11
 *
12
 *      History
13
 *      ROSE 001        Jonathan(G4KLX) Cloned from nr_subr.c
14
 */
15
 
16
#include <linux/config.h>
17
#if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE)
18
#include <linux/errno.h>
19
#include <linux/types.h>
20
#include <linux/socket.h>
21
#include <linux/in.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>
28
#include <net/ax25.h>
29
#include <linux/inet.h>
30
#include <linux/netdevice.h>
31
#include <linux/skbuff.h>
32
#include <net/sock.h>
33
#include <asm/segment.h>
34
#include <asm/system.h>
35
#include <linux/fcntl.h>
36
#include <linux/mm.h>
37
#include <linux/interrupt.h>
38
#include <net/rose.h>
39
 
40
/*
41
 *      This routine purges all of the queues of frames.
42
 */
43
void rose_clear_queues(struct sock *sk)
44
{
45
        struct sk_buff *skb;
46
 
47
        while ((skb = skb_dequeue(&sk->write_queue)) != NULL)
48
                kfree_skb(skb, FREE_WRITE);
49
 
50
        while ((skb = skb_dequeue(&sk->protinfo.rose->ack_queue)) != NULL)
51
                kfree_skb(skb, FREE_WRITE);
52
 
53
#ifdef M_BIT
54
        while ((skb = skb_dequeue(&sk->protinfo.rose->frag_queue)) != NULL)
55
                kfree_skb(skb, FREE_READ);
56
#endif
57
}
58
 
59
/*
60
 * This routine purges the input queue of those frames that have been
61
 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
62
 * SDL diagram.
63
 */
64
void rose_frames_acked(struct sock *sk, unsigned short nr)
65
{
66
        struct sk_buff *skb;
67
 
68
        /*
69
         * Remove all the ack-ed frames from the ack queue.
70
         */
71
        if (sk->protinfo.rose->va != nr) {
72
 
73
                while (skb_peek(&sk->protinfo.rose->ack_queue) != NULL && sk->protinfo.rose->va != nr) {
74
                        skb = skb_dequeue(&sk->protinfo.rose->ack_queue);
75
                        kfree_skb(skb, FREE_WRITE);
76
                        sk->protinfo.rose->va = (sk->protinfo.rose->va + 1) % ROSE_MODULUS;
77
                }
78
        }
79
}
80
 
81
/*
82
 *      Validate that the value of nr is between va and vs. Return true or
83
 *      false for testing.
84
 */
85
int rose_validate_nr(struct sock *sk, unsigned short nr)
86
{
87
        unsigned short vc = sk->protinfo.rose->va;
88
 
89
        while (vc != sk->protinfo.rose->vs) {
90
                if (nr == vc) return 1;
91
                vc = (vc + 1) % ROSE_MODULUS;
92
        }
93
 
94
        if (nr == sk->protinfo.rose->vs) return 1;
95
 
96
        return 0;
97
}
98
 
99
/*
100
 *  This routine is called when the packet layer internally generates a
101
 *  control frame.
102
 */
103
void rose_write_internal(struct sock *sk, int frametype)
104
{
105
        struct sk_buff *skb;
106
        unsigned char  *dptr;
107
        unsigned char  lci1, lci2;
108
        char buffer[250];
109
        int len, faclen = 0;
110
 
111
        len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 1;
112
 
113
        switch (frametype) {
114
                case ROSE_CALL_REQUEST:
115
                        len   += 1 + ROSE_ADDR_LEN + ROSE_ADDR_LEN;
116
                        /* facilities */
117
                        faclen = rose_create_facilities(buffer, sk->protinfo.rose);
118
                        len   += faclen;
119
                        break;
120
                case ROSE_CALL_ACCEPTED:
121
                case ROSE_RESET_REQUEST:
122
                        len   += 2;
123
                        break;
124
                case ROSE_CLEAR_REQUEST:
125
                        len   += 3;
126
                        /* facilities */
127
                        faclen = 3 + 2 + AX25_ADDR_LEN + 3 + ROSE_ADDR_LEN;
128
                        dptr = buffer;
129
                        *dptr++ = faclen-1;     /* Facilities length */
130
                        *dptr++ = 0;
131
                        *dptr++ = FAC_NATIONAL;
132
                        *dptr++ = FAC_NATIONAL_FAIL_CALL;
133
                        *dptr++ = AX25_ADDR_LEN;
134
                        memcpy(dptr, &rose_callsign, AX25_ADDR_LEN);
135
                        dptr += AX25_ADDR_LEN;
136
                        *dptr++ = FAC_NATIONAL_FAIL_ADD;
137
                        *dptr++ = ROSE_ADDR_LEN + 1;
138
                        *dptr++ = ROSE_ADDR_LEN * 2;
139
                        memcpy(dptr, &sk->protinfo.rose->source_addr, ROSE_ADDR_LEN);
140
                        len   += faclen;
141
                        break;
142
        }
143
 
144
        if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
145
                return;
146
 
147
        skb->free = 1;
148
 
149
        /*
150
         *      Space for AX.25 header and PID.
151
         */
152
        skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 1);
153
 
154
        dptr = skb_put(skb, skb_tailroom(skb));
155
 
156
        lci1 = (sk->protinfo.rose->lci >> 8) & 0x0F;
157
        lci2 = (sk->protinfo.rose->lci >> 0) & 0xFF;
158
 
159
        switch (frametype) {
160
 
161
                case ROSE_CALL_REQUEST:
162
                        *dptr++ = ROSE_GFI | lci1;
163
                        *dptr++ = lci2;
164
                        *dptr++ = frametype;
165
                        *dptr++ = 0xAA;
166
                        memcpy(dptr, &sk->protinfo.rose->dest_addr,  ROSE_ADDR_LEN);
167
                        dptr   += ROSE_ADDR_LEN;
168
                        memcpy(dptr, &sk->protinfo.rose->source_addr, ROSE_ADDR_LEN);
169
                        dptr   += ROSE_ADDR_LEN;
170
                        memcpy(dptr, buffer, faclen);
171
                        dptr   += faclen;
172
                        break;
173
 
174
                case ROSE_CALL_ACCEPTED:
175
                        *dptr++ = ROSE_GFI | lci1;
176
                        *dptr++ = lci2;
177
                        *dptr++ = frametype;
178
                        *dptr++ = 0x00;         /* Address length */
179
                        *dptr++ = 0;             /* Facilities length */
180
                        break;
181
 
182
                case ROSE_CLEAR_REQUEST:
183
                        *dptr++ = ROSE_GFI | lci1;
184
                        *dptr++ = lci2;
185
                        *dptr++ = frametype;
186
                        *dptr++ = sk->protinfo.rose->cause;
187
                        *dptr++ = sk->protinfo.rose->diagnostic;
188
                        *dptr++ = 0x00;         /* Address length */
189
                        memcpy(dptr, buffer, faclen);
190
                        dptr   += faclen;
191
                        break;
192
 
193
                case ROSE_RESET_REQUEST:
194
                        *dptr++ = ROSE_GFI | lci1;
195
                        *dptr++ = lci2;
196
                        *dptr++ = frametype;
197
                        *dptr++ = ROSE_DTE_ORIGINATED;
198
                        *dptr++ = 0x00;         /* Address length */
199
                        break;
200
 
201
                case ROSE_RR:
202
                case ROSE_RNR:
203
                        *dptr++ = ROSE_GFI | lci1;
204
                        *dptr++ = lci2;
205
                        *dptr   = frametype;
206
                        *dptr++ |= (sk->protinfo.rose->vr << 5) & 0xE0;
207
                        break;
208
 
209
                case ROSE_CLEAR_CONFIRMATION:
210
                case ROSE_RESET_CONFIRMATION:
211
                        *dptr++ = ROSE_GFI | lci1;
212
                        *dptr++ = lci2;
213
                        *dptr++  = frametype;
214
                        break;
215
 
216
                default:
217
                        printk(KERN_ERR "rose_write_internal: invalid frametype %02X\n", frametype);
218
                        kfree_skb(skb, FREE_WRITE);
219
                        return;
220
        }
221
 
222
        rose_transmit_link(skb, sk->protinfo.rose->neighbour);
223
}
224
 
225
int rose_decode(struct sk_buff *skb, int *ns, int *nr, int *q, int *d, int *m)
226
{
227
        unsigned char *frame;
228
 
229
        frame = skb->data;
230
 
231
        *ns = *nr = *q = *d = *m = 0;
232
 
233
        switch (frame[2]) {
234
                case ROSE_CALL_REQUEST:
235
                case ROSE_CALL_ACCEPTED:
236
                case ROSE_CLEAR_REQUEST:
237
                case ROSE_CLEAR_CONFIRMATION:
238
                case ROSE_RESET_REQUEST:
239
                case ROSE_RESET_CONFIRMATION:
240
                        return frame[2];
241
                default:
242
                        break;
243
        }
244
 
245
        if ((frame[2] & 0x1F) == ROSE_RR  ||
246
            (frame[2] & 0x1F) == ROSE_RNR) {
247
                *nr = (frame[2] >> 5) & 0x07;
248
                return frame[2] & 0x1F;
249
        }
250
 
251
        if ((frame[2] & 0x01) == ROSE_DATA) {
252
                *q  = (frame[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
253
                *d  = (frame[0] & ROSE_D_BIT) == ROSE_D_BIT;
254
                *m  = (frame[2] & ROSE_M_BIT) == ROSE_M_BIT;
255
                *nr = (frame[2] >> 5) & 0x07;
256
                *ns = (frame[2] >> 1) & 0x07;
257
                return ROSE_DATA;
258
        }
259
 
260
        return ROSE_ILLEGAL;
261
}
262
 
263
static int rose_parse_national(unsigned char *p, struct rose_facilities_struct *facilities, int len)
264
{
265
        unsigned char *pt;
266
        unsigned char l, lg, n = 0;
267
        int fac_national_digis_received = 0;
268
 
269
        do {
270
                switch (*p & 0xC0) {
271
                        case 0x00:
272
                                p   += 2;
273
                                n   += 2;
274
                                len -= 2;
275
                                break;
276
 
277
                        case 0x40:
278
                                if (*p == FAC_NATIONAL_RAND)
279
                                        facilities->rand = ((p[1] << 8) & 0xFF00) + ((p[2] << 0) & 0x00FF);
280
                                p   += 3;
281
                                n   += 3;
282
                                len -= 3;
283
                                break;
284
 
285
                        case 0x80:
286
                                p   += 4;
287
                                n   += 4;
288
                                len -= 4;
289
                                break;
290
 
291
                        case 0xC0:
292
                                l = p[1];
293
                                if (*p == FAC_NATIONAL_DEST_DIGI) {
294
                                        if (!fac_national_digis_received) {
295
                                                memcpy(&facilities->source_digis[0], p + 2, AX25_ADDR_LEN);
296
                                                facilities->source_ndigis = 1;
297
                                        }
298
                                }
299
                                else if (*p == FAC_NATIONAL_SRC_DIGI) {
300
                                        if (!fac_national_digis_received) {
301
                                                memcpy(&facilities->dest_digis[0], p + 2, AX25_ADDR_LEN);
302
                                                facilities->dest_ndigis = 1;
303
                                        }
304
                                }
305
                                else if (*p == FAC_NATIONAL_FAIL_CALL) {
306
                                        memcpy(&facilities->fail_call, p + 2, AX25_ADDR_LEN);
307
                                }
308
                                else if (*p == FAC_NATIONAL_FAIL_ADD) {
309
                                        memcpy(&facilities->fail_addr, p + 3, ROSE_ADDR_LEN);
310
                                }
311
                                else if (*p == FAC_NATIONAL_DIGIS) {
312
                                        fac_national_digis_received = 1;
313
                                        facilities->source_ndigis = 0;
314
                                        facilities->dest_ndigis   = 0;
315
                                        for (pt = p + 2, lg = 0 ; lg < l ; pt += AX25_ADDR_LEN, lg += AX25_ADDR_LEN) {
316
                                                if (pt[6] & AX25_HBIT)
317
                                                        memcpy(&facilities->dest_digis[facilities->dest_ndigis++], pt, AX25_ADDR_LEN);
318
                                                else
319
                                                        memcpy(&facilities->source_digis[facilities->source_ndigis++], pt, AX25_ADDR_LEN);
320
                                        }
321
                                }
322
                                p   += l + 2;
323
                                n   += l + 2;
324
                                len -= l + 2;
325
                                break;
326
                }
327
        } while (*p != 0x00 && len > 0);
328
 
329
        return n;
330
}
331
 
332
static int rose_parse_ccitt(unsigned char *p, struct rose_facilities_struct *facilities, int len)
333
{
334
        unsigned char l, n = 0;
335
        char callsign[11];
336
 
337
        do {
338
                switch (*p & 0xC0) {
339
                        case 0x00:
340
                                p   += 2;
341
                                n   += 2;
342
                                len -= 2;
343
                                break;
344
 
345
                        case 0x40:
346
                                p   += 3;
347
                                n   += 3;
348
                                len -= 3;
349
                                break;
350
 
351
                        case 0x80:
352
                                p   += 4;
353
                                n   += 4;
354
                                len -= 4;
355
                                break;
356
 
357
                        case 0xC0:
358
                                l = p[1];
359
                                if (*p == FAC_CCITT_DEST_NSAP) {
360
                                        memcpy(&facilities->source_addr, p + 7, ROSE_ADDR_LEN);
361
                                        memcpy(callsign, p + 12,   l - 10);
362
                                        callsign[l - 10] = '\0';
363
                                        facilities->source_call = *asc2ax(callsign);
364
                                }
365
                                else if (*p == FAC_CCITT_SRC_NSAP) {
366
                                        memcpy(&facilities->dest_addr, p + 7, ROSE_ADDR_LEN);
367
                                        memcpy(callsign, p + 12, l - 10);
368
                                        callsign[l - 10] = '\0';
369
                                        facilities->dest_call = *asc2ax(callsign);
370
                                }
371
                                p   += l + 2;
372
                                n   += l + 2;
373
                                len -= l + 2;
374
                                break;
375
                }
376
        } while (*p != 0x00 && len > 0);
377
 
378
        return n;
379
}
380
 
381
int rose_parse_facilities(unsigned char *p, struct rose_facilities_struct *facilities)
382
{
383
        int facilities_len, len;
384
 
385
        facilities_len = *p++;
386
 
387
        if (facilities_len == 0)
388
                return 0;
389
 
390
        while (facilities_len > 0) {
391
                if (*p == 0x00) {
392
                        facilities_len--;
393
                        p++;
394
 
395
                        switch (*p) {
396
                                case FAC_NATIONAL:              /* National 0x00 */
397
                                        len = rose_parse_national(p + 1, facilities, facilities_len - 1);
398
                                        facilities_len -= len + 1;
399
                                        p += len + 1;
400
                                        break;
401
 
402
                                case FAC_CCITT:         /* CCITT 0x0F */
403
                                        len = rose_parse_ccitt(p + 1, facilities, facilities_len - 1);
404
                                        facilities_len -= len + 1;
405
                                        p += len + 1;
406
                                        break;
407
 
408
                                default:
409
                                        facilities_len--;
410
                                        p++;
411
                                        break;
412
                        }
413
                }
414
                else break;     /* Error in facilities format */
415
        }
416
 
417
        return 1;
418
}
419
 
420
int rose_create_facilities(unsigned char *buffer, rose_cb *rose)
421
{
422
        unsigned char *p = buffer + 1;
423
        char *callsign;
424
        int len;
425
        int nb;
426
 
427
        /* National Facilities */
428
        if (rose->rand != 0 || rose->source_ndigis == 1 || rose->dest_ndigis == 1) {
429
                *p++ = 0x00;
430
                *p++ = FAC_NATIONAL;
431
 
432
                if (rose->rand != 0) {
433
                        *p++ = FAC_NATIONAL_RAND;
434
                        *p++ = (rose->rand >> 8) & 0xFF;
435
                        *p++ = (rose->rand >> 0) & 0xFF;
436
                }
437
 
438
                /* Sent before older facilities */
439
                if ((rose->source_ndigis > 0) || (rose->dest_ndigis > 0)) {
440
                        int maxdigi = 0;
441
                        *p++ = FAC_NATIONAL_DIGIS;
442
                        *p++ = AX25_ADDR_LEN * (rose->source_ndigis + rose->dest_ndigis);
443
                        for (nb = 0 ; nb < rose->source_ndigis ; nb++) {
444
                                if (++maxdigi >= ROSE_MAX_DIGIS)
445
                                        break;
446
                                memcpy(p, &rose->source_digis[nb], AX25_ADDR_LEN);
447
                                p[6] |= AX25_HBIT;
448
                                p += AX25_ADDR_LEN;
449
                        }
450
                        for (nb = 0 ; nb < rose->dest_ndigis ; nb++) {
451
                                if (++maxdigi >= ROSE_MAX_DIGIS)
452
                                        break;
453
                                memcpy(p, &rose->dest_digis[nb], AX25_ADDR_LEN);
454
                                p[6] &= ~AX25_HBIT;
455
                                p += AX25_ADDR_LEN;
456
                        }
457
                }
458
 
459
                /* For compatibility */
460
                if (rose->source_ndigis > 0) {
461
                        *p++ = FAC_NATIONAL_SRC_DIGI;
462
                        *p++ = AX25_ADDR_LEN;
463
                        memcpy(p, &rose->source_digis[0], AX25_ADDR_LEN);
464
                        p   += AX25_ADDR_LEN;
465
                }
466
 
467
                /* For compatibility */
468
                if (rose->dest_ndigis > 0) {
469
                        *p++ = FAC_NATIONAL_DEST_DIGI;
470
                        *p++ = AX25_ADDR_LEN;
471
                        memcpy(p, &rose->dest_digis[0], AX25_ADDR_LEN);
472
                        p   += AX25_ADDR_LEN;
473
                }
474
 
475
        }
476
 
477
        *p++ = 0x00;
478
        *p++ = FAC_CCITT;
479
 
480
        *p++ = FAC_CCITT_DEST_NSAP;
481
 
482
        callsign = ax2asc(&rose->dest_call);
483
 
484
        *p++ = strlen(callsign) + 10;
485
        *p++ = (strlen(callsign) + 9) * 2;              /* ??? */
486
 
487
        *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
488
        *p++ = ROSE_ADDR_LEN * 2;
489
        memcpy(p, &rose->dest_addr, ROSE_ADDR_LEN);
490
        p   += ROSE_ADDR_LEN;
491
 
492
        memcpy(p, callsign, strlen(callsign));
493
        p   += strlen(callsign);
494
 
495
        *p++ = FAC_CCITT_SRC_NSAP;
496
 
497
        callsign = ax2asc(&rose->source_call);
498
 
499
        *p++ = strlen(callsign) + 10;
500
        *p++ = (strlen(callsign) + 9) * 2;              /* ??? */
501
 
502
        *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
503
        *p++ = ROSE_ADDR_LEN * 2;
504
        memcpy(p, &rose->source_addr, ROSE_ADDR_LEN);
505
        p   += ROSE_ADDR_LEN;
506
 
507
        memcpy(p, callsign, strlen(callsign));
508
        p   += strlen(callsign);
509
 
510
        len       = p - buffer;
511
        buffer[0] = len - 1;
512
 
513
        return len;
514
}
515
 
516
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.