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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 *      ROSE release 006
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 af_netrom.c.
14
 *                      Terry (VK2KTJ)  Added support for variable length
15
 *                                      address masks.
16
 *      ROSE 002        Jonathan(G4KLX) Changed hdrincl to qbitincl.
17
 *                  Added random number facilities entry.
18
 *      ROSE 003        Jonathan(G4KLX) Added use count to neighbour.
19
 *      ROSE 004        Jean-Paul(F6FBB) Added LoopBack, M-Bit and
20
 *                                      FPAC facilities.
21
 *  ROSE 005    Jean-Paul(F6FBB) Added rose_clean_neighbour
22
 *  ROSE 006    Jean-Paul(F6FBB) Accept up to 6 digis
23
 *              Fixed a possible loop in facilities
24
 */
25
 
26
#include <linux/config.h>
27
#if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE)
28
#include <linux/module.h>
29
#include <linux/errno.h>
30
#include <linux/types.h>
31
#include <linux/socket.h>
32
#include <linux/in.h>
33
#include <linux/kernel.h>
34
#include <linux/sched.h>
35
#include <linux/timer.h>
36
#include <linux/string.h>
37
#include <linux/sockios.h>
38
#include <linux/net.h>
39
#include <linux/stat.h>
40
#include <net/ax25.h>
41
#include <linux/inet.h>
42
#include <linux/netdevice.h>
43
#include <linux/if_arp.h>
44
#include <linux/skbuff.h>
45
#include <net/sock.h>
46
#include <asm/segment.h>
47
#include <asm/system.h>
48
#include <linux/fcntl.h>
49
#include <linux/termios.h>      /* For TIOCINQ/OUTQ */
50
#include <linux/mm.h>
51
#include <linux/interrupt.h>
52
#include <linux/notifier.h>
53
#include <net/rose.h>
54
#include <linux/proc_fs.h>
55
#include <net/ip.h>
56
#include <net/arp.h>
57
#include <linux/if_arp.h>
58
 
59
int sysctl_rose_restart_request_timeout = ROSE_DEFAULT_T0;
60
int sysctl_rose_call_request_timeout    = ROSE_DEFAULT_T1;
61
int sysctl_rose_reset_request_timeout   = ROSE_DEFAULT_T2;
62
int sysctl_rose_clear_request_timeout   = ROSE_DEFAULT_T3;
63
int sysctl_rose_ack_hold_back_timeout   = ROSE_DEFAULT_HB;
64
int sysctl_rose_routing_control         = ROSE_DEFAULT_ROUTING;
65
int sysctl_rose_link_fail_timeout       = ROSE_DEFAULT_FAIL_TIMEOUT;
66
int sysctl_rose_maximum_vcs             = ROSE_DEFAULT_MAXVC;
67
int sysctl_rose_window_size             = ROSE_DEFAULT_WINDOW_SIZE;
68
 
69
static struct sock *volatile rose_list = NULL;
70
 
71
ax25_address rose_callsign;
72
 
73
/*
74
 *      Convert a ROSE address into text.
75
 */
76
char *rose2asc(rose_address *addr)
77
{
78
        static char buffer[11];
79
 
80
        if (addr->rose_addr[0] == 0x00 && addr->rose_addr[1] == 0x00 &&
81
            addr->rose_addr[2] == 0x00 && addr->rose_addr[3] == 0x00 &&
82
            addr->rose_addr[4] == 0x00) {
83
                strcpy(buffer, "*");
84
        } else {
85
                sprintf(buffer, "%02X%02X%02X%02X%02X", addr->rose_addr[0] & 0xFF,
86
                                                addr->rose_addr[1] & 0xFF,
87
                                                addr->rose_addr[2] & 0xFF,
88
                                                addr->rose_addr[3] & 0xFF,
89
                                                addr->rose_addr[4] & 0xFF);
90
        }
91
 
92
        return buffer;
93
}
94
 
95
/*
96
 *      Compare two ROSE addresses, 0 == equal.
97
 */
98
int rosecmp(rose_address *addr1, rose_address *addr2)
99
{
100
        int i;
101
 
102
        for (i = 0; i < 5; i++)
103
                if (addr1->rose_addr[i] != addr2->rose_addr[i])
104
                        return 1;
105
 
106
        return 0;
107
}
108
 
109
/*
110
 *      Compare two ROSE addresses for only mask digits, 0 == equal.
111
 */
112
int rosecmpm(rose_address *addr1, rose_address *addr2, unsigned short mask)
113
{
114
        int i, j;
115
 
116
        if (mask > 10)
117
                return 1;
118
 
119
        for (i = 0; i < mask; i++) {
120
                j = i / 2;
121
 
122
                if ((i % 2) != 0) {
123
                        if ((addr1->rose_addr[j] & 0x0F) != (addr2->rose_addr[j] & 0x0F))
124
                                return 1;
125
                } else {
126
                        if ((addr1->rose_addr[j] & 0xF0) != (addr2->rose_addr[j] & 0xF0))
127
                                return 1;
128
                }
129
        }
130
 
131
        return 0;
132
}
133
 
134
static void rose_free_sock(struct sock *sk)
135
{
136
        kfree_s(sk->protinfo.rose, sizeof(*sk->protinfo.rose));
137
 
138
        sk_free(sk);
139
 
140
        MOD_DEC_USE_COUNT;
141
}
142
 
143
static struct sock *rose_alloc_sock(void)
144
{
145
        struct sock *sk;
146
        rose_cb *rose;
147
 
148
        if ((sk = sk_alloc(GFP_ATOMIC)) == NULL)
149
                return NULL;
150
 
151
        if ((rose = (rose_cb *)kmalloc(sizeof(*rose), GFP_ATOMIC)) == NULL) {
152
                sk_free(sk);
153
                return NULL;
154
        }
155
 
156
        MOD_INC_USE_COUNT;
157
 
158
        memset(rose, 0x00, sizeof(*rose));
159
 
160
        sk->protinfo.rose = rose;
161
        rose->sk          = sk;
162
 
163
        return sk;
164
}
165
 
166
/*
167
 *      Socket removal during an interrupt is now safe.
168
 */
169
static void rose_remove_socket(struct sock *sk)
170
{
171
        struct sock *s;
172
        unsigned long flags;
173
 
174
        save_flags(flags);
175
        cli();
176
 
177
        if ((s = rose_list) == sk) {
178
                rose_list = s->next;
179
                restore_flags(flags);
180
                return;
181
        }
182
 
183
        while (s != NULL && s->next != NULL) {
184
                if (s->next == sk) {
185
                        s->next = sk->next;
186
                        restore_flags(flags);
187
                        return;
188
                }
189
 
190
                s = s->next;
191
        }
192
 
193
        restore_flags(flags);
194
}
195
 
196
/*
197
 *      Kill all bound sockets on a broken link layer connection to a
198
 *      particular neighbour.
199
 */
200
void rose_kill_by_neigh(struct rose_neigh *neigh)
201
{
202
        struct sock *s;
203
 
204
        for (s = rose_list; s != NULL; s = s->next) {
205
                if (s->protinfo.rose->neighbour == neigh) {
206
                        rose_clear_queues(s); /* F6FBB */
207
                        s->protinfo.rose->cause      = ROSE_OUT_OF_ORDER;
208
                        s->protinfo.rose->diagnostic = 0;
209
                        s->protinfo.rose->state     = ROSE_STATE_0;
210
                        s->protinfo.rose->neighbour->use--;
211
                        s->protinfo.rose->neighbour = NULL;
212
                        s->state                    = TCP_CLOSE;
213
                        s->err                      = ENETUNREACH;
214
                        s->shutdown                |= SEND_SHUTDOWN;
215
                        s->state_change(s);
216
                        s->dead                     = 1;
217
                }
218
        }
219
}
220
 
221
/*
222
 *      Kill all bound sockets on a dropped device.
223
 */
224
static void rose_kill_by_device(struct device *dev)
225
{
226
        struct sock *s;
227
 
228
        for (s = rose_list; s != NULL; s = s->next) {
229
                if (s->protinfo.rose->device == dev) {
230
                        rose_clear_queues(s); /* F6FBB */
231
                        s->protinfo.rose->cause  = ROSE_OUT_OF_ORDER;
232
                        s->protinfo.rose->diagnostic = 0;
233
                        s->protinfo.rose->state  = ROSE_STATE_0;
234
                        s->protinfo.rose->neighbour->use--;
235
                        s->protinfo.rose->device = NULL;
236
                        s->state                 = TCP_CLOSE;
237
                        s->err                   = ENETUNREACH;
238
                        s->shutdown             |= SEND_SHUTDOWN;
239
                        s->state_change(s);
240
                        s->dead                  = 1;
241
                }
242
        }
243
}
244
 
245
/*
246
 *      Handle device status changes.
247
 */
248
static int rose_device_event(struct notifier_block *this, unsigned long event, void *ptr)
249
{
250
        struct device *dev = (struct device *)ptr;
251
 
252
        if (event != NETDEV_DOWN)
253
                return NOTIFY_DONE;
254
 
255
        switch (dev->type) {
256
                case ARPHRD_ROSE:
257
                        rose_kill_by_device(dev);
258
                        break;
259
                case ARPHRD_AX25:
260
                        rose_link_device_down(dev);
261
                        rose_rt_device_down(dev);
262
                        break;
263
        }
264
 
265
        return NOTIFY_DONE;
266
}
267
 
268
/*
269
 *      Add a socket to the bound sockets list.
270
 */
271
static void rose_insert_socket(struct sock *sk)
272
{
273
        unsigned long flags;
274
 
275
        save_flags(flags);
276
        cli();
277
 
278
        sk->next  = rose_list;
279
        rose_list = sk;
280
 
281
        restore_flags(flags);
282
}
283
 
284
/*
285
 *      Find a socket that wants to accept the Call Request we just
286
 *      received.
287
 */
288
static struct sock *rose_find_listener(rose_address *addr, ax25_address *call)
289
{
290
        unsigned long flags;
291
        struct sock *s;
292
 
293
        save_flags(flags);
294
        cli();
295
 
296
        for (s = rose_list; s != NULL; s = s->next) {
297
                if (rosecmp(&s->protinfo.rose->source_addr, addr) == 0 && ax25cmp(&s->protinfo.rose->source_call, call) == 0 && s->protinfo.rose->source_ndigis == 0 && s->state == TCP_LISTEN) {
298
                        restore_flags(flags);
299
                        return s;
300
                }
301
        }
302
 
303
        for (s = rose_list; s != NULL; s = s->next) {
304
                if (rosecmp(&s->protinfo.rose->source_addr, addr) == 0 && ax25cmp(&s->protinfo.rose->source_call, &null_ax25_address) == 0 && s->state == TCP_LISTEN) {
305
                        restore_flags(flags);
306
                        return s;
307
                }
308
        }
309
 
310
        restore_flags(flags);
311
        return NULL;
312
}
313
 
314
/*
315
 *      Find a connected ROSE socket given my LCI and device.
316
 */
317
struct sock *rose_find_socket(unsigned int lci, struct rose_neigh *neigh)
318
{
319
        struct sock *s;
320
        unsigned long flags;
321
 
322
        save_flags(flags);
323
        cli();
324
 
325
        for (s = rose_list; s != NULL; s = s->next) {
326
                if (s->protinfo.rose->lci == lci && s->protinfo.rose->neighbour == neigh) {
327
                        restore_flags(flags);
328
                        return s;
329
                }
330
        }
331
 
332
        restore_flags(flags);
333
 
334
        return NULL;
335
}
336
 
337
/*
338
 *      Find a unique LCI for a given device.
339
 */
340
unsigned int rose_new_lci(struct rose_neigh *neigh)
341
{
342
        int lci;
343
 
344
        if (neigh->dce_mode) {
345
                for (lci = 1; lci <= sysctl_rose_maximum_vcs; lci++)
346
                        if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
347
                                return lci;
348
        } else {
349
                for (lci = sysctl_rose_maximum_vcs; lci > 0; lci--)
350
                        if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
351
                                return lci;
352
        }
353
 
354
        return 0;
355
}
356
 
357
/*
358
 *      Deferred destroy.
359
 */
360
void rose_destroy_socket(struct sock *);
361
 
362
/*
363
 *      Handler for deferred kills.
364
 */
365
static void rose_destroy_timer(unsigned long data)
366
{
367
        rose_destroy_socket((struct sock *)data);
368
}
369
 
370
/*
371
 *      This is called from user mode and the timers. Thus it protects itself against
372
 *      interrupt users but doesn't worry about being called during work.
373
 *      Once it is removed from the queue no interrupt or bottom half will
374
 *      touch it and we are (fairly 8-) ) safe.
375
 */
376
void rose_destroy_socket(struct sock *sk)       /* Not static as it's used by the timer */
377
{
378
        struct sk_buff *skb;
379
        unsigned long flags;
380
 
381
        save_flags(flags);
382
        cli();
383
 
384
        del_timer(&sk->timer);
385
 
386
        rose_remove_socket(sk);
387
        rose_clear_queues(sk);          /* Flush the queues */
388
 
389
        while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
390
                if (skb->sk != sk) {                    /* A pending connection */
391
                        skb->sk->dead = 1;      /* Queue the unaccepted socket for death */
392
                        rose_set_timer(skb->sk);
393
                        skb->sk->protinfo.rose->state = ROSE_STATE_0;
394
                }
395
 
396
                kfree_skb(skb, FREE_READ);
397
        }
398
 
399
        if (sk->wmem_alloc != 0 || sk->rmem_alloc != 0) { /* Defer: outstanding buffers */
400
                init_timer(&sk->timer);
401
                sk->timer.expires  = jiffies + 10 * HZ;
402
                sk->timer.function = rose_destroy_timer;
403
                sk->timer.data     = (unsigned long)sk;
404
                add_timer(&sk->timer);
405
        } else {
406
                rose_free_sock(sk);
407
        }
408
 
409
        restore_flags(flags);
410
}
411
 
412
/*
413
 *      Handling for system calls applied via the various interfaces to a
414
 *      ROSE socket object.
415
 */
416
 
417
static int rose_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
418
{
419
        return -EINVAL;
420
}
421
 
422
static int rose_setsockopt(struct socket *sock, int level, int optname,
423
        char *optval, int optlen)
424
{
425
        struct sock *sk = (struct sock *)sock->data;
426
        int err, opt;
427
 
428
        if (level == SOL_SOCKET)
429
                return sock_setsockopt(sk, level, optname, optval, optlen);
430
 
431
        if (level != SOL_ROSE)
432
                return -EOPNOTSUPP;
433
 
434
        if (optval == NULL)
435
                return -EINVAL;
436
 
437
        if ((err = verify_area(VERIFY_READ, optval, sizeof(int))) != 0)
438
                return err;
439
 
440
        opt = get_fs_long((unsigned long *)optval);
441
 
442
        switch (optname) {
443
                case ROSE_DEFER:
444
                        sk->protinfo.rose->defer = opt ? 1 : 0;
445
                        return 0;
446
 
447
                case ROSE_T1:
448
                        if (opt < 1)
449
                                return -EINVAL;
450
                        sk->protinfo.rose->t1 = opt * ROSE_SLOWHZ;
451
                        return 0;
452
 
453
                case ROSE_T2:
454
                        if (opt < 1)
455
                                return -EINVAL;
456
                        sk->protinfo.rose->t2 = opt * ROSE_SLOWHZ;
457
                        return 0;
458
 
459
                case ROSE_T3:
460
                        if (opt < 1)
461
                                return -EINVAL;
462
                        sk->protinfo.rose->t3 = opt * ROSE_SLOWHZ;
463
                        return 0;
464
 
465
                case ROSE_HOLDBACK:
466
                        if (opt < 1)
467
                                return -EINVAL;
468
                        sk->protinfo.rose->hb = opt * ROSE_SLOWHZ;
469
                        return 0;
470
 
471
                case ROSE_IDLE:
472
                        if (opt < 0)
473
                                return -EINVAL;
474
                        return 0;
475
 
476
                case ROSE_QBITINCL:
477
                        sk->protinfo.rose->qbitincl = opt ? 1 : 0;
478
                        return 0;
479
 
480
                default:
481
                        return -ENOPROTOOPT;
482
        }
483
}
484
 
485
static int rose_getsockopt(struct socket *sock, int level, int optname,
486
        char *optval, int *optlen)
487
{
488
        struct sock *sk = (struct sock *)sock->data;
489
        int val = 0;
490
        int err;
491
 
492
        if (level == SOL_SOCKET)
493
                return sock_getsockopt(sk, level, optname, optval, optlen);
494
 
495
        if (level != SOL_ROSE)
496
                return -EOPNOTSUPP;
497
 
498
        switch (optname) {
499
                case ROSE_DEFER:
500
                        val = sk->protinfo.rose->defer;
501
                        break;
502
 
503
                case ROSE_T1:
504
                        val = sk->protinfo.rose->t1 / ROSE_SLOWHZ;
505
                        break;
506
 
507
                case ROSE_T2:
508
                        val = sk->protinfo.rose->t2 / ROSE_SLOWHZ;
509
                        break;
510
 
511
                case ROSE_T3:
512
                        val = sk->protinfo.rose->t3 / ROSE_SLOWHZ;
513
                        break;
514
 
515
                case ROSE_HOLDBACK:
516
                        val = sk->protinfo.rose->hb / ROSE_SLOWHZ;
517
                        break;
518
 
519
                case ROSE_IDLE:
520
                        val = 0;
521
                        break;
522
 
523
                case ROSE_QBITINCL:
524
                        val = sk->protinfo.rose->qbitincl;
525
                        break;
526
 
527
                default:
528
                        return -ENOPROTOOPT;
529
        }
530
 
531
        if ((err = verify_area(VERIFY_WRITE, optlen, sizeof(int))) != 0)
532
                return err;
533
 
534
        put_fs_long(sizeof(int), (unsigned long *)optlen);
535
 
536
        if ((err = verify_area(VERIFY_WRITE, optval, sizeof(int))) != 0)
537
                return err;
538
 
539
        put_fs_long(val, (unsigned long *)optval);
540
 
541
        return 0;
542
}
543
 
544
static int rose_listen(struct socket *sock, int backlog)
545
{
546
        struct sock *sk = (struct sock *)sock->data;
547
 
548
        if (sk->state != TCP_LISTEN) {
549
                sk->protinfo.rose->dest_ndigis = 0;
550
                memset(&sk->protinfo.rose->dest_addr, '\0', ROSE_ADDR_LEN);
551
                memset(&sk->protinfo.rose->dest_call, '\0', AX25_ADDR_LEN);
552
                memset(sk->protinfo.rose->dest_digis, '\0', AX25_ADDR_LEN*ROSE_MAX_DIGIS);
553
                sk->max_ack_backlog = backlog;
554
                sk->state           = TCP_LISTEN;
555
                return 0;
556
        }
557
 
558
        return -EOPNOTSUPP;
559
}
560
 
561
static void def_callback1(struct sock *sk)
562
{
563
        if (!sk->dead)
564
                wake_up_interruptible(sk->sleep);
565
}
566
 
567
static void def_callback2(struct sock *sk, int len)
568
{
569
        if (!sk->dead)
570
                wake_up_interruptible(sk->sleep);
571
}
572
 
573
static int rose_create(struct socket *sock, int protocol)
574
{
575
        struct sock *sk;
576
        rose_cb *rose;
577
 
578
        if (sock->type != SOCK_SEQPACKET || protocol != 0)
579
                return -ESOCKTNOSUPPORT;
580
 
581
        if ((sk = rose_alloc_sock()) == NULL)
582
                return -ENOMEM;
583
 
584
        rose = sk->protinfo.rose;
585
 
586
        skb_queue_head_init(&sk->receive_queue);
587
        skb_queue_head_init(&sk->write_queue);
588
        skb_queue_head_init(&sk->back_log);
589
        skb_queue_head_init(&rose->ack_queue);
590
#ifdef M_BIT
591
        skb_queue_head_init(&rose->frag_queue);
592
        rose->fraglen    = 0;
593
#endif
594
        init_timer(&sk->timer);
595
 
596
        sk->socket        = sock;
597
        sk->type          = sock->type;
598
        sk->protocol      = protocol;
599
        sk->allocation    = GFP_KERNEL;
600
        sk->rcvbuf        = SK_RMEM_MAX;
601
        sk->sndbuf        = SK_WMEM_MAX;
602
        sk->state         = TCP_CLOSE;
603
        sk->priority      = SOPRI_NORMAL;
604
        sk->mtu           = ROSE_MTU;   /* 128 */
605
        sk->zapped        = 1;
606
 
607
        sk->state_change = def_callback1;
608
        sk->data_ready   = def_callback2;
609
        sk->write_space  = def_callback1;
610
        sk->error_report = def_callback1;
611
 
612
        if (sock != NULL) {
613
                sock->data = (void *)sk;
614
                sk->sleep  = sock->wait;
615
        }
616
 
617
        rose->t1    = sysctl_rose_call_request_timeout;
618
        rose->t2    = sysctl_rose_reset_request_timeout;
619
        rose->t3    = sysctl_rose_clear_request_timeout;
620
        rose->hb    = sysctl_rose_ack_hold_back_timeout;
621
 
622
        rose->state = ROSE_STATE_0;
623
 
624
        return 0;
625
}
626
 
627
static struct sock *rose_make_new(struct sock *osk)
628
{
629
        struct sock *sk;
630
        rose_cb *rose;
631
 
632
        if (osk->type != SOCK_SEQPACKET)
633
                return NULL;
634
 
635
        if ((sk = rose_alloc_sock()) == NULL)
636
                return NULL;
637
 
638
        rose = sk->protinfo.rose;
639
 
640
        skb_queue_head_init(&sk->receive_queue);
641
        skb_queue_head_init(&sk->write_queue);
642
        skb_queue_head_init(&sk->back_log);
643
        skb_queue_head_init(&rose->ack_queue);
644
#ifdef M_BIT
645
        skb_queue_head_init(&rose->frag_queue);
646
        rose->fraglen  = 0;
647
#endif
648
 
649
        init_timer(&sk->timer);
650
 
651
        sk->type        = osk->type;
652
        sk->socket      = osk->socket;
653
        sk->priority    = osk->priority;
654
        sk->protocol    = osk->protocol;
655
        sk->rcvbuf      = osk->rcvbuf;
656
        sk->sndbuf      = osk->sndbuf;
657
        sk->debug       = osk->debug;
658
        sk->state       = TCP_ESTABLISHED;
659
        sk->mtu         = osk->mtu;
660
        sk->sleep       = osk->sleep;
661
        sk->zapped      = osk->zapped;
662
 
663
        sk->state_change = def_callback1;
664
        sk->data_ready   = def_callback2;
665
        sk->write_space  = def_callback1;
666
        sk->error_report = def_callback1;
667
 
668
        rose->t1      = osk->protinfo.rose->t1;
669
        rose->t2      = osk->protinfo.rose->t2;
670
        rose->t3      = osk->protinfo.rose->t3;
671
        rose->hb      = osk->protinfo.rose->hb;
672
 
673
        rose->defer    = osk->protinfo.rose->defer;
674
        rose->device   = osk->protinfo.rose->device;
675
        rose->qbitincl = osk->protinfo.rose->qbitincl;
676
 
677
        return sk;
678
}
679
 
680
static int rose_dup(struct socket *newsock, struct socket *oldsock)
681
{
682
        struct sock *sk = (struct sock *)oldsock->data;
683
 
684
        if (sk == NULL || newsock == NULL)
685
                return -EINVAL;
686
 
687
        return rose_create(newsock, sk->protocol);
688
}
689
 
690
static int rose_release(struct socket *sock, struct socket *peer)
691
{
692
        struct sock *sk = (struct sock *)sock->data;
693
 
694
        if (sk == NULL) return 0;
695
 
696
        switch (sk->protinfo.rose->state) {
697
 
698
                case ROSE_STATE_0:
699
                        sk->state     = TCP_CLOSE;
700
                        sk->shutdown |= SEND_SHUTDOWN;
701
                        sk->state_change(sk);
702
                        sk->dead      = 1;
703
                        rose_destroy_socket(sk);
704
                        break;
705
 
706
                case ROSE_STATE_2:
707
                        sk->protinfo.rose->neighbour->use--;
708
                        sk->protinfo.rose->state = ROSE_STATE_0;
709
                        sk->state                = TCP_CLOSE;
710
                        sk->shutdown            |= SEND_SHUTDOWN;
711
                        sk->state_change(sk);
712
                        sk->dead                 = 1;
713
                        rose_destroy_socket(sk);
714
                        break;
715
 
716
                case ROSE_STATE_1:
717
                case ROSE_STATE_3:
718
                case ROSE_STATE_4:
719
                case ROSE_STATE_5:
720
                        rose_clear_queues(sk);
721
                        rose_write_internal(sk, ROSE_CLEAR_REQUEST);
722
                        sk->protinfo.rose->timer = sk->protinfo.rose->t3;
723
                        sk->protinfo.rose->state = ROSE_STATE_2;
724
                        sk->state                = TCP_CLOSE;
725
                        sk->shutdown            |= SEND_SHUTDOWN;
726
                        sk->state_change(sk);
727
                        sk->dead                 = 1;
728
                        sk->destroy              = 1;
729
                        break;
730
 
731
                default:
732
                        break;
733
        }
734
 
735
        sock->data = NULL;
736
        sk->socket = NULL;      /* Not used, but we should do this. **/
737
 
738
        return 0;
739
}
740
 
741
static int rose_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
742
{
743
        struct sock *sk = (struct sock *)sock->data;
744
        struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
745
        struct device *dev;
746
        ax25_address *user, *source;
747
        int n;
748
 
749
        if (sk->zapped == 0)
750
                return -EINVAL;
751
 
752
        if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
753
                return -EINVAL;
754
 
755
        if (addr->srose_family != AF_ROSE)
756
                return -EINVAL;
757
 
758
        if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
759
                return -EINVAL;
760
 
761
        if (addr->srose_ndigis > ROSE_MAX_DIGIS)
762
                return -EINVAL;
763
 
764
        if ((dev = rose_dev_get(&addr->srose_addr)) == NULL) {
765
                if (sk->debug)
766
                        printk("ROSE: bind failed: invalid address\n");
767
                return -EADDRNOTAVAIL;
768
        }
769
 
770
        source = &addr->srose_call;
771
 
772
        if ((user = ax25_findbyuid(current->euid)) == NULL) {
773
                if (ax25_uid_policy && !suser())
774
                        return -EACCES;
775
                user = source;
776
        }
777
 
778
        sk->protinfo.rose->source_addr   = addr->srose_addr;
779
        sk->protinfo.rose->source_call   = *user;
780
        sk->protinfo.rose->device        = dev;
781
        sk->protinfo.rose->source_ndigis = addr->srose_ndigis;
782
 
783
        if (addr_len == sizeof(struct full_sockaddr_rose)) {
784
                struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
785
                for (n = 0 ; n < addr->srose_ndigis ; n++)
786
                        sk->protinfo.rose->source_digis[n] = full_addr->srose_digis[n];
787
        } else {
788
                if (sk->protinfo.rose->source_ndigis == 1) {
789
                        sk->protinfo.rose->source_digis[0] = addr->srose_digi;
790
                }
791
        }
792
 
793
        rose_insert_socket(sk);
794
 
795
        sk->zapped = 0;
796
 
797
        if (sk->debug)
798
                printk("ROSE: socket is bound\n");
799
 
800
        return 0;
801
}
802
 
803
static int rose_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
804
{
805
        struct sock *sk = (struct sock *)sock->data;
806
        struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
807
        unsigned char cause, diagnostic;
808
        ax25_address *user;
809
        struct device *dev;
810
        int n;
811
 
812
        if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
813
                sock->state = SS_CONNECTED;
814
                return 0;        /* Connect completed during a ERESTARTSYS event */
815
        }
816
 
817
        if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
818
                sock->state = SS_UNCONNECTED;
819
                return -ECONNREFUSED;
820
        }
821
 
822
        if (sk->state == TCP_ESTABLISHED)
823
                return -EISCONN;        /* No reconnect on a seqpacket socket */
824
 
825
        sk->state   = TCP_CLOSE;
826
        sock->state = SS_UNCONNECTED;
827
 
828
        if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
829
                return -EINVAL;
830
 
831
        if (addr->srose_family != AF_ROSE)
832
                return -EINVAL;
833
 
834
        if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
835
                return -EINVAL;
836
 
837
        if (addr->srose_ndigis > ROSE_MAX_DIGIS)
838
                return -EINVAL;
839
 
840
        /* Source + Destination digis should not exceed ROSE_MAX_DIGIS */
841
        if ((sk->protinfo.rose->source_ndigis + addr->srose_ndigis) > ROSE_MAX_DIGIS)
842
                return -EINVAL;
843
 
844
        if ((sk->protinfo.rose->neighbour = rose_get_neigh(&addr->srose_addr, &cause, &diagnostic)) == NULL) {
845
                sk->protinfo.rose->cause = cause;
846
                sk->protinfo.rose->diagnostic = diagnostic;
847
                return -ENETUNREACH;
848
        }
849
 
850
        if ((sk->protinfo.rose->lci = rose_new_lci(sk->protinfo.rose->neighbour)) == 0)
851
                return -ENETUNREACH;
852
 
853
        if (sk->zapped) {       /* Must bind first - autobinding in this may or may not work */
854
                sk->zapped = 0;
855
 
856
                if ((dev = rose_dev_first()) == NULL)
857
                        return -ENETUNREACH;
858
 
859
                if ((user = ax25_findbyuid(current->euid)) == NULL)
860
                        return -EINVAL;
861
 
862
                memcpy(&sk->protinfo.rose->source_addr, dev->dev_addr, ROSE_ADDR_LEN);
863
                sk->protinfo.rose->source_call = *user;
864
                sk->protinfo.rose->device      = dev;
865
 
866
                rose_insert_socket(sk);         /* Finish the bind */
867
        }
868
 
869
        sk->protinfo.rose->dest_addr   = addr->srose_addr;
870
        sk->protinfo.rose->dest_call   = addr->srose_call;
871
        sk->protinfo.rose->rand        = ((int)sk->protinfo.rose & 0xFFFF) + sk->protinfo.rose->lci;
872
        sk->protinfo.rose->dest_ndigis = addr->srose_ndigis;
873
 
874
        if (addr_len == sizeof(struct full_sockaddr_rose)) {
875
                struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
876
                for (n = 0 ; n < addr->srose_ndigis ; n++)
877
                        sk->protinfo.rose->dest_digis[n] = full_addr->srose_digis[n];
878
        } else {
879
                if (sk->protinfo.rose->dest_ndigis == 1) {
880
                        sk->protinfo.rose->dest_digis[0] = addr->srose_digi;
881
                }
882
        }
883
 
884
        /* Move to connecting socket, start sending Connect Requests */
885
        sock->state   = SS_CONNECTING;
886
        sk->state     = TCP_SYN_SENT;
887
 
888
        sk->protinfo.rose->state = ROSE_STATE_1;
889
 
890
        sk->protinfo.rose->neighbour->use++;
891
 
892
        sk->protinfo.rose->timer = sk->protinfo.rose->t1;
893
        rose_write_internal(sk, ROSE_CALL_REQUEST);
894
 
895
        rose_set_timer(sk);
896
 
897
        /* Now the loop */
898
        if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
899
                return -EINPROGRESS;
900
 
901
        cli();  /* To avoid races on the sleep */
902
 
903
        /*
904
         * A Connect Ack with Choke or timeout or failed routing will go to closed.
905
         */
906
        while (sk->state == TCP_SYN_SENT) {
907
                interruptible_sleep_on(sk->sleep);
908
                if (current->signal & ~current->blocked) {
909
                        sti();
910
                        return -ERESTARTSYS;
911
                }
912
        }
913
 
914
        if (sk->state != TCP_ESTABLISHED) {
915
                sti();
916
                sock->state = SS_UNCONNECTED;
917
                return sock_error(sk);  /* Always set at this point */
918
        }
919
 
920
        sock->state = SS_CONNECTED;
921
 
922
        sti();
923
 
924
        return 0;
925
}
926
 
927
static int rose_socketpair(struct socket *sock1, struct socket *sock2)
928
{
929
        return -EOPNOTSUPP;
930
}
931
 
932
static int rose_accept(struct socket *sock, struct socket *newsock, int flags)
933
{
934
        struct sock *sk;
935
        struct sock *newsk;
936
        struct sk_buff *skb;
937
 
938
        if (newsock->data != NULL)
939
                rose_destroy_socket((struct sock *)newsock->data);
940
 
941
        newsock->data = NULL;
942
 
943
        if ((sk = (struct sock *)sock->data) == NULL)
944
                return -EINVAL;
945
 
946
        if (sk->type != SOCK_SEQPACKET)
947
                return -EOPNOTSUPP;
948
 
949
        if (sk->state != TCP_LISTEN)
950
                return -EINVAL;
951
 
952
        /*
953
         *      The write queue this time is holding sockets ready to use
954
         *      hooked into the SABM we saved
955
         */
956
        do {
957
                cli();
958
                if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
959
                        if (flags & O_NONBLOCK) {
960
                                sti();
961
                                return -EWOULDBLOCK;
962
                        }
963
                        interruptible_sleep_on(sk->sleep);
964
                        if (current->signal & ~current->blocked) {
965
                                sti();
966
                                return -ERESTARTSYS;
967
                        }
968
                }
969
        } while (skb == NULL);
970
 
971
        newsk = skb->sk;
972
        newsk->pair = NULL;
973
        sti();
974
 
975
        /* Now attach up the new socket */
976
        skb->sk = NULL;
977
        kfree_skb(skb, FREE_READ);
978
        sk->ack_backlog--;
979
        newsock->data = newsk;
980
 
981
        return 0;
982
}
983
 
984
static int rose_getname(struct socket *sock, struct sockaddr *uaddr,
985
        int *uaddr_len, int peer)
986
{
987
        struct sockaddr_rose *srose = (struct sockaddr_rose *)uaddr;
988
        struct sock *sk = (struct sock *)sock->data;
989
        int n;
990
 
991
        if (peer != 0) {
992
                if (sk->state != TCP_ESTABLISHED)
993
                        return -ENOTCONN;
994
                srose->srose_family = AF_ROSE;
995
                srose->srose_addr   = sk->protinfo.rose->dest_addr;
996
                srose->srose_call   = sk->protinfo.rose->dest_call;
997
                srose->srose_ndigis = sk->protinfo.rose->dest_ndigis;
998
                if (*uaddr_len >= sizeof(struct full_sockaddr_rose)) {
999
                        struct full_sockaddr_rose *full_srose = (struct full_sockaddr_rose *)uaddr;
1000
                        for (n = 0 ; n < sk->protinfo.rose->dest_ndigis ; n++)
1001
                                full_srose->srose_digis[n] = sk->protinfo.rose->dest_digis[n];
1002
                        *uaddr_len = sizeof(struct full_sockaddr_rose);
1003
                } else {
1004
                        if (sk->protinfo.rose->dest_ndigis >= 1) {
1005
                                srose->srose_ndigis = 1;
1006
                                srose->srose_digi = sk->protinfo.rose->dest_digis[0];
1007
                        }
1008
                        *uaddr_len = sizeof(struct sockaddr_rose);
1009
                }
1010
        } else {
1011
                srose->srose_family = AF_ROSE;
1012
                srose->srose_addr   = sk->protinfo.rose->source_addr;
1013
                srose->srose_call   = sk->protinfo.rose->source_call;
1014
                srose->srose_ndigis = sk->protinfo.rose->source_ndigis;
1015
                if (*uaddr_len >= sizeof(struct full_sockaddr_rose)) {
1016
                        struct full_sockaddr_rose *full_srose = (struct full_sockaddr_rose *)uaddr;
1017
                        for (n = 0 ; n < sk->protinfo.rose->source_ndigis ; n++)
1018
                                full_srose->srose_digis[n] = sk->protinfo.rose->source_digis[n];
1019
                        *uaddr_len = sizeof(struct full_sockaddr_rose);
1020
                } else {
1021
                        if (sk->protinfo.rose->source_ndigis >= 1) {
1022
                                srose->srose_ndigis = 1;
1023
                                srose->srose_digi = sk->protinfo.rose->source_digis[sk->protinfo.rose->source_ndigis-1];
1024
                        }
1025
                        *uaddr_len = sizeof(struct sockaddr_rose);
1026
                }
1027
        }
1028
 
1029
        return 0;
1030
}
1031
 
1032
int rose_rx_call_request(struct sk_buff *skb, struct device *dev, struct rose_neigh *neigh, unsigned int lci)
1033
{
1034
        struct sock *sk;
1035
        struct sock *make;
1036
        struct rose_facilities_struct facilities;
1037
        int n, len;
1038
 
1039
        skb->sk = NULL;         /* Initially we don't know who it's for */
1040
 
1041
        /*
1042
         *      skb->data points to the rose frame start
1043
         */
1044
 
1045
        len  = (((skb->data[3] >> 4) & 0x0F) + 1) / 2;
1046
        len += (((skb->data[3] >> 0) & 0x0F) + 1) / 2;
1047
 
1048
        memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
1049
 
1050
        if (!rose_parse_facilities(skb->data + len + 4, &facilities)) {
1051
                rose_transmit_clear_request(neigh, lci, ROSE_INVALID_FACILITY, 76);
1052
                return 0;
1053
        }
1054
 
1055
        sk = rose_find_listener(&facilities.source_addr, &facilities.source_call);
1056
 
1057
        /*
1058
         * We can't accept the Call Request.
1059
         */
1060
        if (sk == NULL || sk->ack_backlog == sk->max_ack_backlog || (make = rose_make_new(sk)) == NULL) {
1061
                rose_transmit_clear_request(neigh, lci, ROSE_NETWORK_CONGESTION, 120);
1062
                return 0;
1063
        }
1064
 
1065
        skb->sk     = make;
1066
        make->state = TCP_ESTABLISHED;
1067
 
1068
        make->protinfo.rose->lci           = lci;
1069
        make->protinfo.rose->dest_addr     = facilities.dest_addr;
1070
        make->protinfo.rose->dest_call     = facilities.dest_call;
1071
        make->protinfo.rose->dest_ndigis   = facilities.dest_ndigis;
1072
        for (n = 0 ; n < facilities.dest_ndigis ; n++)
1073
                make->protinfo.rose->dest_digis[n] = facilities.dest_digis[n];
1074
        make->protinfo.rose->source_addr   = facilities.source_addr;
1075
        make->protinfo.rose->source_call   = facilities.source_call;
1076
        make->protinfo.rose->source_ndigis = facilities.source_ndigis;
1077
        for (n = 0 ; n < facilities.source_ndigis ; n++)
1078
                make->protinfo.rose->source_digis[n]= facilities.source_digis[n];
1079
        make->protinfo.rose->neighbour     = neigh;
1080
        make->protinfo.rose->device        = dev;
1081
        make->protinfo.rose->facilities    = facilities;
1082
 
1083
        make->protinfo.rose->neighbour->use++;
1084
 
1085
        if (sk->protinfo.rose->defer) {
1086
                make->protinfo.rose->state = ROSE_STATE_5;
1087
        } else {
1088
                rose_write_internal(make, ROSE_CALL_ACCEPTED);
1089
                make->protinfo.rose->state = ROSE_STATE_3;
1090
        }
1091
 
1092
        make->protinfo.rose->condition = 0x00;
1093
        make->protinfo.rose->vs        = 0;
1094
        make->protinfo.rose->va        = 0;
1095
        make->protinfo.rose->vr        = 0;
1096
        make->protinfo.rose->vl        = 0;
1097
        sk->ack_backlog++;
1098
        make->pair = sk;
1099
 
1100
        rose_insert_socket(make);
1101
 
1102
        skb_queue_head(&sk->receive_queue, skb);
1103
 
1104
        rose_set_timer(make);
1105
 
1106
        if (!sk->dead)
1107
                sk->data_ready(sk, skb->len);
1108
 
1109
        return 1;
1110
}
1111
 
1112
static int rose_sendmsg(struct socket *sock, struct msghdr *msg, int len, int noblock, int flags)
1113
{
1114
        struct sock *sk = (struct sock *)sock->data;
1115
        struct sockaddr_rose *usrose = (struct sockaddr_rose *)msg->msg_name;
1116
        int err;
1117
        struct full_sockaddr_rose srose;
1118
        struct sk_buff *skb;
1119
        unsigned char *asmptr;
1120
        int n, size, qbit = 0;
1121
 
1122
        if (sk->err)
1123
                return sock_error(sk);
1124
 
1125
        if (flags)
1126
                return -EINVAL;
1127
 
1128
        if (sk->zapped)
1129
                return -EADDRNOTAVAIL;
1130
 
1131
        if (sk->shutdown & SEND_SHUTDOWN) {
1132
                send_sig(SIGPIPE, current, 0);
1133
                return -EPIPE;
1134
        }
1135
 
1136
        if (sk->protinfo.rose->neighbour == NULL || sk->protinfo.rose->device == NULL)
1137
                return -ENETUNREACH;
1138
 
1139
        if (usrose != NULL) {
1140
                if (msg->msg_namelen != sizeof(struct sockaddr_rose) && msg->msg_namelen != sizeof(struct full_sockaddr_rose))
1141
                        return -EINVAL;
1142
                memset(&srose, 0, sizeof(struct full_sockaddr_rose));
1143
                memcpy(&srose, usrose, msg->msg_namelen);
1144
                if (rosecmp(&sk->protinfo.rose->dest_addr, &srose.srose_addr) != 0 ||
1145
                    ax25cmp(&sk->protinfo.rose->dest_call, &srose.srose_call) != 0)
1146
                        return -EISCONN;
1147
                if (srose.srose_ndigis != sk->protinfo.rose->dest_ndigis)
1148
                        return -EISCONN;
1149
                if (srose.srose_ndigis == sk->protinfo.rose->dest_ndigis) {
1150
                        for (n = 0 ; n < srose.srose_ndigis ; n++)
1151
                                if (ax25cmp(&sk->protinfo.rose->dest_digis[n], &srose.srose_digis[n]) != 0)
1152
                                        return -EISCONN;
1153
                }
1154
                if (srose.srose_family != AF_ROSE)
1155
                        return -EINVAL;
1156
        } else {
1157
                if (sk->state != TCP_ESTABLISHED)
1158
                        return -ENOTCONN;
1159
 
1160
                srose.srose_family = AF_ROSE;
1161
                srose.srose_addr   = sk->protinfo.rose->dest_addr;
1162
                srose.srose_call   = sk->protinfo.rose->dest_call;
1163
                srose.srose_ndigis = sk->protinfo.rose->dest_ndigis;
1164
                for (n = 0 ; n < sk->protinfo.rose->dest_ndigis ; n++)
1165
                        srose.srose_digis[n] = sk->protinfo.rose->dest_digis[n];
1166
        }
1167
 
1168
        if (sk->debug)
1169
                printk("ROSE: sendto: Addresses built.\n");
1170
 
1171
        /* Build a packet */
1172
        if (sk->debug)
1173
                printk("ROSE: sendto: building packet.\n");
1174
 
1175
        size = len + AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN;
1176
 
1177
        if ((skb = sock_alloc_send_skb(sk, size, 0, 0, &err)) == NULL)
1178
                return err;
1179
 
1180
        skb->sk   = sk;
1181
        skb->free = 1;
1182
 
1183
        skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN);
1184
 
1185
        /*
1186
         *      Put the data on the end
1187
         */
1188
        asmptr = skb->h.raw = skb_put(skb, len);
1189
 
1190
        if (sk->debug)
1191
                printk("ROSE: Appending user data\n");
1192
 
1193
        /* User data follows immediately after the ROSE transport header */
1194
        memcpy_fromiovec(asmptr, msg->msg_iov, len);
1195
 
1196
        /*
1197
         *      If the Q BIT Include socket option is in force, the first
1198
         *      byte of the user data is the logical value of the Q Bit.
1199
         */
1200
        if (sk->protinfo.rose->qbitincl) {
1201
                qbit = skb->data[0];
1202
                skb_pull(skb, 1);
1203
        }
1204
 
1205
        /*
1206
         *      Push down the ROSE header
1207
         */
1208
        asmptr = skb_push(skb, ROSE_MIN_LEN);
1209
 
1210
        if (sk->debug)
1211
                printk("Building ROSE Header.\n");
1212
 
1213
        /* Build a ROSE Network header */
1214
        asmptr[0] = ((sk->protinfo.rose->lci >> 8) & 0x0F) | ROSE_GFI;
1215
        asmptr[1] = (sk->protinfo.rose->lci >> 0) & 0xFF;
1216
        asmptr[2] = ROSE_DATA;
1217
 
1218
        if (qbit)
1219
                asmptr[0] |= ROSE_Q_BIT;
1220
 
1221
        if (sk->debug)
1222
                printk("Built header.\n");
1223
 
1224
        if (sk->debug)
1225
                printk("ROSE: Transmitting buffer\n");
1226
 
1227
        if (sk->state != TCP_ESTABLISHED) {
1228
                kfree_skb(skb, FREE_WRITE);
1229
                return -ENOTCONN;
1230
        }
1231
 
1232
#ifdef M_BIT
1233
#define ROSE_PACLEN (256-ROSE_MIN_LEN)
1234
        if (skb->len - ROSE_MIN_LEN > ROSE_PACLEN) {
1235
                unsigned char header[ROSE_MIN_LEN];
1236
                struct sk_buff *skbn;
1237
                int frontlen;
1238
                int lg;
1239
 
1240
                /* Save a copy of the Header */
1241
                memcpy(header, skb->data, ROSE_MIN_LEN);
1242
                skb_pull(skb, ROSE_MIN_LEN);
1243
 
1244
                frontlen = skb_headroom(skb);
1245
 
1246
                while (skb->len > 0) {
1247
                        if ((skbn = sock_alloc_send_skb(sk, frontlen + ROSE_PACLEN, 0, 0, &err)) == NULL)
1248
                                return err;
1249
 
1250
                        skbn->sk   = sk;
1251
                        skbn->free = 1;
1252
                        skbn->arp  = 1;
1253
 
1254
                        skb_reserve(skbn, frontlen);
1255
 
1256
                        lg = (ROSE_PACLEN > skb->len) ? skb->len : ROSE_PACLEN;
1257
 
1258
                        /* Copy the user data */
1259
                        memcpy(skb_put(skbn, lg), skb->data, lg);
1260
                        skb_pull(skb, lg);
1261
 
1262
                        /* Duplicate the Header */
1263
                        skb_push(skbn, ROSE_MIN_LEN);
1264
                        memcpy(skbn->data, header, ROSE_MIN_LEN);
1265
 
1266
                        if (skb->len > 0)
1267
                                skbn->data[2] |= M_BIT;
1268
 
1269
                        skb_queue_tail(&sk->write_queue, skbn); /* Throw it on the queue */
1270
                }
1271
 
1272
                skb->free = 1;
1273
                kfree_skb(skb, FREE_WRITE);
1274
        } else {
1275
                skb_queue_tail(&sk->write_queue, skb);          /* Throw it on the queue */
1276
        }
1277
#else
1278
        skb_queue_tail(&sk->write_queue, skb);  /* Shove it onto the queue */
1279
#endif
1280
        rose_kick(sk);
1281
 
1282
        return len;
1283
}
1284
 
1285
 
1286
static int rose_recvmsg(struct socket *sock, struct msghdr *msg, int size, int noblock,
1287
                   int flags, int *addr_len)
1288
{
1289
        struct sock *sk = (struct sock *)sock->data;
1290
        struct sockaddr_rose *srose = (struct sockaddr_rose *)msg->msg_name;
1291
        int copied, qbit;
1292
        unsigned char *asmptr;
1293
        struct sk_buff *skb;
1294
        int n, er;
1295
 
1296
        if (sk->err)
1297
                return sock_error(sk);
1298
 
1299
        if (addr_len != NULL)
1300
                *addr_len = sizeof(*srose);
1301
 
1302
        /*
1303
         * This works for seqpacket too. The receiver has ordered the queue for
1304
         * us! We do one quick check first though
1305
         */
1306
        if (sk->state != TCP_ESTABLISHED)
1307
                return -ENOTCONN;
1308
 
1309
        /* Now we can treat all alike */
1310
        if ((skb = skb_recv_datagram(sk, flags, noblock, &er)) == NULL)
1311
                return er;
1312
 
1313
        qbit = (skb->data[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
1314
 
1315
        skb_pull(skb, ROSE_MIN_LEN);
1316
 
1317
        if (sk->protinfo.rose->qbitincl) {
1318
                asmptr  = skb_push(skb, 1);
1319
                *asmptr = qbit;
1320
        }
1321
 
1322
        skb->h.raw = skb->data;
1323
 
1324
        copied = (size < skb->len) ? size : skb->len;
1325
        skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1326
 
1327
        if (srose != NULL) {
1328
                srose->srose_family = AF_ROSE;
1329
                srose->srose_addr   = sk->protinfo.rose->dest_addr;
1330
                srose->srose_call   = sk->protinfo.rose->dest_call;
1331
                srose->srose_ndigis = sk->protinfo.rose->dest_ndigis;
1332
                if (*addr_len >= sizeof(struct full_sockaddr_rose)) {
1333
                        struct full_sockaddr_rose *full_srose = (struct full_sockaddr_rose *)msg->msg_name;
1334
                        for (n = 0 ; n < sk->protinfo.rose->dest_ndigis ; n++)
1335
                                full_srose->srose_digis[n] = sk->protinfo.rose->dest_digis[n];
1336
                        *addr_len = sizeof(struct full_sockaddr_rose);
1337
                } else {
1338
                        if (sk->protinfo.rose->dest_ndigis >= 1) {
1339
                                srose->srose_ndigis = 1;
1340
                                srose->srose_digi = sk->protinfo.rose->dest_digis[0];
1341
                        }
1342
                        *addr_len = sizeof(struct sockaddr_rose);
1343
                }
1344
        }
1345
 
1346
        skb_free_datagram(sk, skb);
1347
 
1348
        return copied;
1349
}
1350
 
1351
static int rose_shutdown(struct socket *sk, int how)
1352
{
1353
        return -EOPNOTSUPP;
1354
}
1355
 
1356
static int rose_select(struct socket *sock , int sel_type, select_table *wait)
1357
{
1358
        struct sock *sk = (struct sock *)sock->data;
1359
 
1360
        return datagram_select(sk, sel_type, wait);
1361
}
1362
 
1363
static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1364
{
1365
        struct sock *sk = (struct sock *)sock->data;
1366
        int err;
1367
 
1368
        switch (cmd) {
1369
                case TIOCOUTQ: {
1370
                        long amount;
1371
                        if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(unsigned int))) != 0)
1372
                                return err;
1373
                        amount = sk->sndbuf - sk->wmem_alloc;
1374
                        if (amount < 0)
1375
                                amount = 0;
1376
                        put_fs_long(amount, (unsigned int *)arg);
1377
                        return 0;
1378
                }
1379
 
1380
                case TIOCINQ: {
1381
                        struct sk_buff *skb;
1382
                        long amount = 0L;
1383
                        /* These two are safe on a single CPU system as only user tasks fiddle here */
1384
                        if ((skb = skb_peek(&sk->receive_queue)) != NULL)
1385
                                amount = skb->len;
1386
                        if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(unsigned int))) != 0)
1387
                                return err;
1388
                        put_fs_long(amount, (unsigned int *)arg);
1389
                        return 0;
1390
                }
1391
 
1392
                case SIOCGSTAMP:
1393
                        if (sk != NULL) {
1394
                                if (sk->stamp.tv_sec == 0)
1395
                                        return -ENOENT;
1396
                                if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct timeval))) != 0)
1397
                                        return err;
1398
                                memcpy_tofs((void *)arg, &sk->stamp, sizeof(struct timeval));
1399
                                return 0;
1400
                        }
1401
                        return -EINVAL;
1402
 
1403
                case SIOCGIFADDR:
1404
                case SIOCSIFADDR:
1405
                case SIOCGIFDSTADDR:
1406
                case SIOCSIFDSTADDR:
1407
                case SIOCGIFBRDADDR:
1408
                case SIOCSIFBRDADDR:
1409
                case SIOCGIFNETMASK:
1410
                case SIOCSIFNETMASK:
1411
                case SIOCGIFMETRIC:
1412
                case SIOCSIFMETRIC:
1413
                        return -EINVAL;
1414
 
1415
                case SIOCADDRT:
1416
                case SIOCDELRT:
1417
                case SIOCRSCLRRT:
1418
                        if (!suser()) return -EPERM;
1419
                        return rose_rt_ioctl(cmd, (void *)arg);
1420
 
1421
                case SIOCRSGCAUSE: {
1422
                        struct rose_cause_struct rose_cause;
1423
                        if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct rose_cause_struct))) != 0)
1424
                                return err;
1425
                        rose_cause.cause      = sk->protinfo.rose->cause;
1426
                        rose_cause.diagnostic = sk->protinfo.rose->diagnostic;
1427
                        memcpy_tofs((void *)arg, &rose_cause, sizeof(struct rose_cause_struct));
1428
                        return 0;
1429
                }
1430
 
1431
                case SIOCRSSCAUSE: {
1432
                        struct rose_cause_struct rose_cause;
1433
                        if ((err = verify_area(VERIFY_READ, (void *)arg, sizeof(struct rose_cause_struct))) != 0)
1434
                                return err;
1435
                        memcpy_fromfs(&rose_cause, (void *)arg, sizeof(struct rose_cause_struct));
1436
                        sk->protinfo.rose->cause      = rose_cause.cause;
1437
                        sk->protinfo.rose->diagnostic = rose_cause.diagnostic;
1438
                        return 0;
1439
                }
1440
 
1441
                case SIOCRSSL2CALL:
1442
                        if (!suser()) return -EPERM;
1443
                        if ((err = verify_area(VERIFY_READ, (void *)arg, sizeof(ax25_address))) != 0)
1444
                                return err;
1445
                        if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1446
                                ax25_listen_release(&rose_callsign, NULL);
1447
                        memcpy_fromfs(&rose_callsign, (void *)arg, sizeof(ax25_address));
1448
                        if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1449
                                ax25_listen_register(&rose_callsign, NULL);
1450
                        return 0;
1451
 
1452
                case SIOCRSGL2CALL:
1453
                        if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(ax25_address))) != 0)
1454
                                return err;
1455
                        memcpy_tofs((void *)arg, &rose_callsign, sizeof(ax25_address));
1456
                        return 0;
1457
 
1458
                case SIOCRSACCEPT:
1459
                        if (sk->protinfo.rose->state == ROSE_STATE_5) {
1460
                                rose_write_internal(sk, ROSE_CALL_ACCEPTED);
1461
                                sk->protinfo.rose->condition = 0x00;
1462
                                sk->protinfo.rose->vs        = 0;
1463
                                sk->protinfo.rose->va        = 0;
1464
                                sk->protinfo.rose->vr        = 0;
1465
                                sk->protinfo.rose->vl        = 0;
1466
                                sk->protinfo.rose->state     = ROSE_STATE_3;
1467
                        }
1468
                        return 0;
1469
 
1470
                case SIOCRSGFACILITIES: {
1471
                        if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct rose_facilities_struct))) != 0)
1472
                                return err;
1473
                        memcpy_tofs((void *)arg, &sk->protinfo.rose->facilities, sizeof(struct rose_facilities_struct));
1474
                        return 0;
1475
                }
1476
 
1477
                default:
1478
                        return dev_ioctl(cmd, (void *)arg);
1479
        }
1480
 
1481
        /*NOTREACHED*/
1482
        return 0;
1483
}
1484
 
1485
static int rose_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
1486
{
1487
        struct sock *s;
1488
        struct device *dev;
1489
        const char *devname, *callsign;
1490
        int len = 0;
1491
        off_t pos = 0;
1492
        off_t begin = 0;
1493
 
1494
        cli();
1495
 
1496
        len += sprintf(buffer, "dest_addr  dest_call src_addr   src_call  dev   lci neigh st vs vr va   t  t1  t2  t3  hb    idle Snd-Q Rcv-Q Inode\n");
1497
 
1498
        for (s = rose_list; s != NULL; s = s->next) {
1499
                if ((dev = s->protinfo.rose->device) == NULL)
1500
                        devname = "???";
1501
                else
1502
                        devname = dev->name;
1503
 
1504
                len += sprintf(buffer + len, "%-10s %-9s ",
1505
                        rose2asc(&s->protinfo.rose->dest_addr),
1506
                        ax2asc(&s->protinfo.rose->dest_call));
1507
 
1508
                if (ax25cmp(&s->protinfo.rose->source_call, &null_ax25_address) == 0)
1509
                        callsign = "??????-?";
1510
                else
1511
                        callsign = ax2asc(&s->protinfo.rose->source_call);
1512
 
1513
                len += sprintf(buffer + len, "%-10s %-9s %-5s %3.3X %05d  %d  %d  %d  %d %3d %3d %3d %3d %3d %3d/%03d %5d %5d %ld\n",
1514
                        rose2asc(&s->protinfo.rose->source_addr), callsign,
1515
                        devname,  s->protinfo.rose->lci & 0x0FFF,
1516
                        (s->protinfo.rose->neighbour) ? s->protinfo.rose->neighbour->number : 0,
1517
                        s->protinfo.rose->state,
1518
                        s->protinfo.rose->vs, s->protinfo.rose->vr, s->protinfo.rose->va,
1519
                        s->protinfo.rose->timer / ROSE_SLOWHZ,
1520
                        s->protinfo.rose->t1    / ROSE_SLOWHZ,
1521
                        s->protinfo.rose->t2    / ROSE_SLOWHZ,
1522
                        s->protinfo.rose->t3    / ROSE_SLOWHZ,
1523
                        s->protinfo.rose->hb    / ROSE_SLOWHZ,
1524
                        0, 0,
1525
                        s->wmem_alloc, s->rmem_alloc,
1526
                        s->socket && SOCK_INODE(s->socket) ? SOCK_INODE(s->socket)->i_ino : 0);
1527
 
1528
                pos = begin + len;
1529
 
1530
                if (pos < offset) {
1531
                        len   = 0;
1532
                        begin = pos;
1533
                }
1534
 
1535
                if (pos > offset + length)
1536
                        break;
1537
        }
1538
 
1539
        sti();
1540
 
1541
        *start = buffer + (offset - begin);
1542
        len   -= (offset - begin);
1543
 
1544
        if (len > length) len = length;
1545
 
1546
        return(len);
1547
}
1548
 
1549
static struct proto_ops rose_proto_ops = {
1550
        AF_ROSE,
1551
 
1552
        rose_create,
1553
        rose_dup,
1554
        rose_release,
1555
        rose_bind,
1556
        rose_connect,
1557
        rose_socketpair,
1558
        rose_accept,
1559
        rose_getname,
1560
        rose_select,
1561
        rose_ioctl,
1562
        rose_listen,
1563
        rose_shutdown,
1564
        rose_setsockopt,
1565
        rose_getsockopt,
1566
        rose_fcntl,
1567
        rose_sendmsg,
1568
        rose_recvmsg
1569
};
1570
 
1571
static struct notifier_block rose_dev_notifier = {
1572
        rose_device_event,
1573
 
1574
};
1575
 
1576
#ifdef CONFIG_PROC_FS
1577
static struct proc_dir_entry proc_net_rose = {
1578
        PROC_NET_RS, 4, "rose",
1579
        S_IFREG | S_IRUGO, 1, 0, 0,
1580
        0, &proc_net_inode_operations,
1581
        rose_get_info
1582
};
1583
static struct proc_dir_entry proc_net_rose_neigh = {
1584
        PROC_NET_RS_NEIGH, 10, "rose_neigh",
1585
        S_IFREG | S_IRUGO, 1, 0, 0,
1586
        0, &proc_net_inode_operations,
1587
        rose_neigh_get_info
1588
};
1589
static struct proc_dir_entry proc_net_rose_nodes = {
1590
        PROC_NET_RS_NODES, 10, "rose_nodes",
1591
        S_IFREG | S_IRUGO, 1, 0, 0,
1592
        0, &proc_net_inode_operations,
1593
        rose_nodes_get_info
1594
};
1595
static struct proc_dir_entry proc_net_rose_routes = {
1596
        PROC_NET_RS_ROUTES, 11, "rose_routes",
1597
        S_IFREG | S_IRUGO, 1, 0, 0,
1598
        0, &proc_net_inode_operations,
1599
        rose_routes_get_info
1600
};
1601
#endif  
1602
 
1603
static struct device dev_rose[] = {
1604
        {"rose0", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, rose_init},
1605
        {"rose1", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, rose_init},
1606
        {"rose2", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, rose_init},
1607
        {"rose3", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, rose_init},
1608
        {"rose4", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, rose_init},
1609
        {"rose5", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, rose_init},
1610
        {"rose6", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, rose_init},
1611
        {"rose7", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, rose_init},
1612
        {"rose8", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, rose_init},
1613
        {"rose9", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, rose_init}
1614
};
1615
 
1616
void rose_proto_init(struct net_proto *pro)
1617
{
1618
        int i;
1619
 
1620
        rose_callsign = null_ax25_address;
1621
 
1622
        sock_register(rose_proto_ops.family, &rose_proto_ops);
1623
        register_netdevice_notifier(&rose_dev_notifier);
1624
        printk(KERN_INFO "F6FBB/G4KLX ROSE for Linux. Version 0.63 for AX25.035 Linux 2.0\n");
1625
 
1626
        if (!ax25_protocol_register(AX25_P_ROSE, rose_route_frame))
1627
                printk(KERN_ERR "ROSE: unable to register protocol with AX.25\n");
1628
        if (!ax25_linkfail_register(rose_link_failed))
1629
                printk(KERN_ERR "ROSE: unable to register linkfail handler with AX.25\n");
1630
 
1631
        for (i = 0; i < 10; i++)
1632
                register_netdev(&dev_rose[i]);
1633
 
1634
        rose_register_sysctl();
1635
 
1636
        rose_loopback_init();
1637
 
1638
#ifdef CONFIG_PROC_FS
1639
        proc_net_register(&proc_net_rose);
1640
        proc_net_register(&proc_net_rose_neigh);
1641
        proc_net_register(&proc_net_rose_nodes);
1642
        proc_net_register(&proc_net_rose_routes);
1643
#endif
1644
}
1645
 
1646
#ifdef MODULE
1647
 
1648
int init_module(void)
1649
{
1650
        rose_proto_init(NULL);
1651
 
1652
        register_symtab(NULL);
1653
 
1654
        return 0;
1655
}
1656
 
1657
void cleanup_module(void)
1658
{
1659
        int i;
1660
 
1661
#ifdef CONFIG_PROC_FS
1662
        proc_net_unregister(PROC_NET_RS);
1663
        proc_net_unregister(PROC_NET_RS_NEIGH);
1664
        proc_net_unregister(PROC_NET_RS_NODES);
1665
        proc_net_unregister(PROC_NET_RS_ROUTES);
1666
#endif
1667
        rose_loopback_clear();
1668
 
1669
        rose_rt_free();
1670
 
1671
        ax25_protocol_release(AX25_P_ROSE);
1672
        ax25_linkfail_release(rose_link_failed);
1673
 
1674
        if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1675
                ax25_listen_release(&rose_callsign, NULL);
1676
 
1677
        rose_unregister_sysctl();
1678
 
1679
        unregister_netdevice_notifier(&rose_dev_notifier);
1680
 
1681
        sock_unregister(AF_ROSE);
1682
 
1683
        for (i = 0; i < 10; i++) {
1684
                if (dev_rose[i].priv != NULL) {
1685
                        kfree(dev_rose[i].priv);
1686
                        dev_rose[i].priv = NULL;
1687
                        unregister_netdev(&dev_rose[i]);
1688
                }
1689
        }
1690
}
1691
 
1692
#endif
1693
 
1694
#endif

powered by: WebSVN 2.1.0

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