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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [net/] [ax25/] [af_ax25.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1629 jcastillo
/*
2
 *      AX.25 release 035
3
 *
4
 *      This code REQUIRES 1.2.1 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
 *      AX.25 006       Alan(GW4PTS)            Nearly died of shock - it's working 8-)
14
 *      AX.25 007       Alan(GW4PTS)            Removed the silliest bugs
15
 *      AX.25 008       Alan(GW4PTS)            Cleaned up, fixed a few state machine problems, added callbacks
16
 *      AX.25 009       Alan(GW4PTS)            Emergency patch kit to fix memory corruption
17
 *      AX.25 010       Alan(GW4PTS)            Added RAW sockets/Digipeat.
18
 *      AX.25 011       Alan(GW4PTS)            RAW socket and datagram fixes (thanks) - Raw sendto now gets PID right
19
 *                                              datagram sendto uses correct target address.
20
 *      AX.25 012       Alan(GW4PTS)            Correct incoming connection handling, send DM to failed connects.
21
 *                                              Use skb->data not skb+1. Support sk->priority correctly.
22
 *                                              Correct receive on SOCK_DGRAM.
23
 *      AX.25 013       Alan(GW4PTS)            Send DM to all unknown frames, missing initialiser fixed
24
 *                                              Leave spare SSID bits set (DAMA etc) - thanks for bug report,
25
 *                                              removed device registration (it's not used or needed). Clean up for
26
 *                                              gcc 2.5.8. PID to AX25_P_
27
 *      AX.25 014       Alan(GW4PTS)            Cleanup and NET3 merge
28
 *      AX.25 015       Alan(GW4PTS)            Internal test version.
29
 *      AX.25 016       Alan(GW4PTS)            Semi Internal version for PI card
30
 *                                              work.
31
 *      AX.25 017       Alan(GW4PTS)            Fixed some small bugs reported by
32
 *                                              G4KLX
33
 *      AX.25 018       Alan(GW4PTS)            Fixed a small error in SOCK_DGRAM
34
 *      AX.25 019       Alan(GW4PTS)            Clean ups for the non INET kernel and device ioctls in AX.25
35
 *      AX.25 020       Jonathan(G4KLX)         /proc support and other changes.
36
 *      AX.25 021       Alan(GW4PTS)            Added AX25_T1, AX25_N2, AX25_T3 as requested.
37
 *      AX.25 022       Jonathan(G4KLX)         More work on the ax25 auto router and /proc improved (again)!
38
 *                      Alan(GW4PTS)            Added TIOCINQ/OUTQ
39
 *      AX.25 023       Alan(GW4PTS)            Fixed shutdown bug
40
 *      AX.25 023       Alan(GW4PTS)            Linus changed timers
41
 *      AX.25 024       Alan(GW4PTS)            Small bug fixes
42
 *      AX.25 025       Alan(GW4PTS)            More fixes, Linux 1.1.51 compatibility stuff, timers again!
43
 *      AX.25 026       Alan(GW4PTS)            Small state fix.
44
 *      AX.25 027       Alan(GW4PTS)            Socket close crash fixes.
45
 *      AX.25 028       Alan(GW4PTS)            Callsign control including settings per uid.
46
 *                                              Small bug fixes.
47
 *                                              Protocol set by sockets only.
48
 *                                              Small changes to allow for start of NET/ROM layer.
49
 *      AX.25 028a      Jonathan(G4KLX)         Changes to state machine.
50
 *      AX.25 028b      Jonathan(G4KLX)         Extracted ax25 control block
51
 *                                              from sock structure.
52
 *      AX.25 029       Alan(GW4PTS)            Combined 028b and some KA9Q code
53
 *                      Jonathan(G4KLX)         and removed all the old Berkeley, added IP mode registration.
54
 *                      Darryl(G7LED)           stuff. Cross-port digipeating. Minor fixes and enhancements.
55
 *                      Alan(GW4PTS)            Missed suser() on axassociate checks
56
 *      AX.25 030       Alan(GW4PTS)            Added variable length headers.
57
 *                      Jonathan(G4KLX)         Added BPQ Ethernet interface.
58
 *                      Steven(GW7RRM)          Added digi-peating control ioctl.
59
 *                                              Added extended AX.25 support.
60
 *                                              Added AX.25 frame segmentation.
61
 *                      Darryl(G7LED)           Changed connect(), recvfrom(), sendto() sockaddr/addrlen to
62
 *                                              fall inline with bind() and new policy.
63
 *                                              Moved digipeating ctl to new ax25_dev structs.
64
 *                                              Fixed ax25_release(), set TCP_CLOSE, wakeup app
65
 *                                              context, THEN make the sock dead.
66
 *                      Alan(GW4PTS)            Cleaned up for single recvmsg methods.
67
 *                      Alan(GW4PTS)            Fixed not clearing error on connect failure.
68
 *      AX.25 031       Jonathan(G4KLX)         Added binding to any device.
69
 *                      Joerg(DL1BKE)           Added DAMA support, fixed (?) digipeating, fixed buffer locking
70
 *                                              for "virtual connect" mode... Result: Probably the
71
 *                                              "Most Buggiest Code You've Ever Seen" (TM)
72
 *                      HaJo(DD8NE)             Implementation of a T5 (idle) timer
73
 *                      Joerg(DL1BKE)           Renamed T5 to IDLE and changed behaviour:
74
 *                                              the timer gets reloaded on every received or transmitted
75
 *                                              I frame for IP or NETROM. The idle timer is not active
76
 *                                              on "vanilla AX.25" connections. Furthermore added PACLEN
77
 *                                              to provide AX.25-layer based fragmentation (like WAMPES)
78
 *      AX.25 032       Joerg(DL1BKE)           Fixed DAMA timeout error.
79
 *                                              ax25_send_frame() limits the number of enqueued
80
 *                                              datagrams per socket.
81
 *      AX.25 033       Jonathan(G4KLX)         Removed auto-router.
82
 *                      Hans(PE1AYX)            Converted to Module.
83
 *                      Joerg(DL1BKE)           Moved BPQ Ethernet to seperate driver.
84
 *                                              Fixed 2.0.x specific IP over AX.25 problem.
85
 *      AX.25 035       Frederic(F1OAT)         Support for pseudo-digipeating.
86
 *                      Jonathan(G4KLX)         Support for packet forwarding.
87
 *                      Jonathan(G4KLX)         Fix wildcard listening parameter setting.
88
 *
89
 *      To do:
90
 *              Restructure the ax25_rcv code to be cleaner/faster and
91
 *              copy only when needed.
92
 *              Consider better arbitrary protocol support.
93
 */
94
 
95
#include <linux/config.h>
96
#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
97
#include <linux/module.h>
98
#include <linux/errno.h>
99
#include <linux/types.h>
100
#include <linux/socket.h>
101
#include <linux/in.h>
102
#include <linux/kernel.h>
103
#include <linux/sched.h>
104
#include <linux/timer.h>
105
#include <linux/string.h>
106
#include <linux/sockios.h>
107
#include <linux/net.h>
108
#include <net/ax25.h>
109
#include <linux/inet.h>
110
#include <linux/netdevice.h>
111
#include <linux/if_arp.h>
112
#include <linux/skbuff.h>
113
#include <net/sock.h>
114
#include <asm/segment.h>
115
#include <asm/system.h>
116
#include <linux/fcntl.h>
117
#include <linux/termios.h>      /* For TIOCINQ/OUTQ */
118
#include <linux/mm.h>
119
#include <linux/interrupt.h>
120
#include <linux/notifier.h>
121
#include <linux/proc_fs.h>
122
#include <linux/stat.h>
123
#include <linux/firewall.h>
124
 
125
#include <net/ip.h>
126
#include <net/arp.h>
127
 
128
/*
129
 *      The null address is defined as a callsign of all spaces with an
130
 *      SSID of zero.
131
 */
132
ax25_address null_ax25_address = {{0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00}};
133
 
134
ax25_cb *volatile ax25_list = NULL;
135
 
136
/*
137
 *      ax25 -> ascii conversion
138
 */
139
char *ax2asc(ax25_address *a)
140
{
141
        static char buf[11];
142
        char c, *s;
143
        int n;
144
 
145
        for (n = 0, s = buf; n < 6; n++) {
146
                c = (a->ax25_call[n] >> 1) & 0x7F;
147
 
148
                if (c != ' ') *s++ = c;
149
        }
150
 
151
        *s++ = '-';
152
 
153
        if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) {
154
                *s++ = '1';
155
                n -= 10;
156
        }
157
 
158
        *s++ = n + '0';
159
        *s++ = '\0';
160
 
161
        if (*buf == '\0' || *buf == '-')
162
           return "*";
163
 
164
        return buf;
165
 
166
}
167
 
168
/*
169
 *      ascii -> ax25 conversion
170
 */
171
ax25_address *asc2ax(char *callsign)
172
{
173
        static ax25_address addr;
174
        char *s;
175
        int n;
176
 
177
        for (s = callsign, n = 0; n < 6; n++) {
178
                if (*s != '\0' && *s != '-')
179
                        addr.ax25_call[n] = *s++;
180
                else
181
                        addr.ax25_call[n] = ' ';
182
                addr.ax25_call[n] <<= 1;
183
                addr.ax25_call[n] &= 0xFE;
184
        }
185
 
186
        if (*s++ == '\0') {
187
                addr.ax25_call[6] = 0x00;
188
                return &addr;
189
        }
190
 
191
        addr.ax25_call[6] = *s++ - '0';
192
 
193
        if (*s != '\0') {
194
                addr.ax25_call[6] *= 10;
195
                addr.ax25_call[6] += *s++ - '0';
196
        }
197
 
198
        addr.ax25_call[6] <<= 1;
199
        addr.ax25_call[6] &= 0x1E;
200
 
201
        return &addr;
202
}
203
 
204
/*
205
 *      Compare two ax.25 addresses
206
 */
207
int ax25cmp(ax25_address *a, ax25_address *b)
208
{
209
        int ct = 0;
210
 
211
        while (ct < 6) {
212
                if ((a->ax25_call[ct] & 0xFE) != (b->ax25_call[ct] & 0xFE))     /* Clean off repeater bits */
213
                        return 1;
214
                ct++;
215
        }
216
 
217
        if ((a->ax25_call[ct] & 0x1E) == (b->ax25_call[ct] & 0x1E))     /* SSID without control bit */
218
                return 0;
219
 
220
        return 2;                       /* Partial match */
221
}
222
 
223
/*
224
 *      Compare two AX.25 digipeater paths.
225
 */
226
static int ax25digicmp(ax25_digi *digi1, ax25_digi *digi2)
227
{
228
        int i;
229
 
230
        if (digi1->ndigi != digi2->ndigi)
231
                return 1;
232
 
233
        if (digi1->lastrepeat != digi2->lastrepeat)
234
                return 1;
235
 
236
        for (i = 0; i < digi1->ndigi; i++)
237
                if (ax25cmp(&digi1->calls[i], &digi2->calls[i]) != 0)
238
                        return 1;
239
 
240
        return 0;
241
}
242
 
243
/*
244
 *      Free an allocated ax25 control block. This is done to centralise
245
 *      the MOD count code.
246
 */
247
static void ax25_free_cb(ax25_cb *ax25)
248
{
249
        if (ax25->digipeat != NULL) {
250
                kfree_s(ax25->digipeat, sizeof(ax25_digi));
251
                ax25->digipeat = NULL;
252
        }
253
 
254
        kfree_s(ax25, sizeof(ax25_cb));
255
 
256
        MOD_DEC_USE_COUNT;
257
}
258
 
259
/*
260
 *      Socket removal during an interrupt is now safe.
261
 */
262
static void ax25_remove_socket(ax25_cb *ax25)
263
{
264
        ax25_cb *s;
265
        unsigned long flags;
266
 
267
        save_flags(flags);
268
        cli();
269
 
270
        if ((s = ax25_list) == ax25) {
271
                ax25_list = s->next;
272
                restore_flags(flags);
273
                return;
274
        }
275
 
276
        while (s != NULL && s->next != NULL) {
277
                if (s->next == ax25) {
278
                        s->next = ax25->next;
279
                        restore_flags(flags);
280
                        return;
281
                }
282
 
283
                s = s->next;
284
        }
285
 
286
        restore_flags(flags);
287
}
288
 
289
/*
290
 *      Kill all bound sockets on a dropped device.
291
 */
292
static void ax25_kill_by_device(struct device *dev)
293
{
294
        ax25_cb *s;
295
 
296
        for (s = ax25_list; s != NULL; s = s->next) {
297
                if (s->device == dev) {
298
                        s->device = NULL;
299
                        ax25_disconnect(s, ENETUNREACH);
300
                }
301
        }
302
}
303
 
304
/*
305
 *      Handle device status changes.
306
 */
307
static int ax25_device_event(struct notifier_block *this,unsigned long event, void *ptr)
308
{
309
        struct device *dev = (struct device *)ptr;
310
 
311
        /* Reject non AX.25 devices */
312
        if (dev->type != ARPHRD_AX25)
313
                return NOTIFY_DONE;
314
 
315
        switch (event) {
316
                case NETDEV_UP:
317
                        ax25_dev_device_up(dev);
318
                        break;
319
                case NETDEV_DOWN:
320
                        ax25_kill_by_device(dev);
321
                        ax25_rt_device_down(dev);
322
                        ax25_dev_device_down(dev);
323
                        break;
324
                default:
325
                        break;
326
        }
327
 
328
        return NOTIFY_DONE;
329
}
330
 
331
/*
332
 *      Add a socket to the bound sockets list.
333
 */
334
static void ax25_insert_socket(ax25_cb *ax25)
335
{
336
        unsigned long flags;
337
 
338
        save_flags(flags);
339
        cli();
340
 
341
        ax25->next = ax25_list;
342
        ax25_list  = ax25;
343
 
344
        restore_flags(flags);
345
}
346
 
347
/*
348
 *      Find a socket that wants to accept the SABM we have just
349
 *      received.
350
 */
351
static struct sock *ax25_find_listener(ax25_address *addr, int digi, struct device *dev, int type)
352
{
353
        unsigned long flags;
354
        ax25_cb *s;
355
 
356
        save_flags(flags);
357
        cli();
358
 
359
        for (s = ax25_list; s != NULL; s = s->next) {
360
                if ((s->iamdigi && !digi) || (!s->iamdigi && digi))
361
                        continue;
362
                if (s->sk != NULL && ax25cmp(&s->source_addr, addr) == 0 && s->sk->type == type && s->sk->state == TCP_LISTEN) {
363
                        /* If device is null we match any device */
364
                        if (s->device == NULL || s->device == dev) {
365
                                restore_flags(flags);
366
                                return s->sk;
367
                        }
368
                }
369
        }
370
 
371
        restore_flags(flags);
372
        return NULL;
373
}
374
 
375
/*
376
 *      Find an AX.25 socket given both ends.
377
 */
378
static struct sock *ax25_find_socket(ax25_address *my_addr, ax25_address *dest_addr, int type)
379
{
380
        ax25_cb *s;
381
        unsigned long flags;
382
 
383
        save_flags(flags);
384
        cli();
385
 
386
        for (s = ax25_list; s != NULL; s = s->next) {
387
                if (s->sk != NULL && ax25cmp(&s->source_addr, my_addr) == 0 && ax25cmp(&s->dest_addr, dest_addr) == 0 && s->sk->type == type) {
388
                        restore_flags(flags);
389
                        return s->sk;
390
                }
391
        }
392
 
393
        restore_flags(flags);
394
 
395
        return NULL;
396
}
397
 
398
/*
399
 *      Find an AX.25 control block given both ends. It will only pick up
400
 *      floating AX.25 control blocks or non Raw socket bound control blocks.
401
 */
402
ax25_cb *ax25_find_cb(ax25_address *src_addr, ax25_address *dest_addr, ax25_digi *digi, struct device *dev)
403
{
404
        ax25_cb *s;
405
        unsigned long flags;
406
 
407
        save_flags(flags);
408
        cli();
409
 
410
        for (s = ax25_list; s != NULL; s = s->next) {
411
                if (s->sk != NULL && s->sk->type != SOCK_SEQPACKET)
412
                        continue;
413
                if (ax25cmp(&s->source_addr, src_addr) == 0 && ax25cmp(&s->dest_addr, dest_addr) == 0 && s->device == dev) {
414
                        if (digi != NULL && digi->ndigi != 0) {
415
                                if (s->digipeat == NULL)
416
                                        continue;
417
                                if (ax25digicmp(s->digipeat, digi) != 0)
418
                                        continue;
419
                        } else {
420
                                if (s->digipeat != NULL && s->digipeat->ndigi != 0)
421
                                        continue;
422
                        }
423
                        restore_flags(flags);
424
                        return s;
425
                }
426
        }
427
 
428
        restore_flags(flags);
429
 
430
        return NULL;
431
}
432
 
433
/*
434
 *      Look for any matching address - RAW sockets can bind to arbitrary names
435
 */
436
static struct sock *ax25_addr_match(ax25_address *addr)
437
{
438
        unsigned long flags;
439
        ax25_cb *s;
440
 
441
        save_flags(flags);
442
        cli();
443
 
444
        for (s = ax25_list; s != NULL; s = s->next) {
445
                if (s->sk != NULL && ax25cmp(&s->source_addr, addr) == 0 && s->sk->type == SOCK_RAW) {
446
                        restore_flags(flags);
447
                        return s->sk;
448
                }
449
        }
450
 
451
        restore_flags(flags);
452
 
453
        return NULL;
454
}
455
 
456
static void ax25_send_to_raw(struct sock *sk, struct sk_buff *skb, int proto)
457
{
458
        struct sk_buff *copy;
459
 
460
        while (sk != NULL) {
461
                if (sk->type == SOCK_RAW && sk->protocol == proto && sk->rmem_alloc <= sk->rcvbuf) {
462
                        if ((copy = skb_clone(skb, GFP_ATOMIC)) == NULL)
463
                                return;
464
 
465
                        if (sock_queue_rcv_skb(sk, copy) != 0)
466
                                kfree_skb(copy, FREE_READ);
467
                }
468
 
469
                sk = sk->next;
470
        }
471
}
472
 
473
/*
474
 *      Deferred destroy.
475
 */
476
void ax25_destroy_socket(ax25_cb *);
477
 
478
/*
479
 *      Handler for deferred kills.
480
 */
481
static void ax25_destroy_timer(unsigned long data)
482
{
483
        ax25_destroy_socket((ax25_cb *)data);
484
}
485
 
486
/*
487
 *      This is called from user mode and the timers. Thus it protects itself against
488
 *      interrupt users but doesn't worry about being called during work.
489
 *      Once it is removed from the queue no interrupt or bottom half will
490
 *      touch it and we are (fairly 8-) ) safe.
491
 */
492
void ax25_destroy_socket(ax25_cb *ax25) /* Not static as it's used by the timer */
493
{
494
        struct sk_buff *skb;
495
        unsigned long flags;
496
 
497
        save_flags(flags);
498
        cli();
499
 
500
        del_timer(&ax25->timer);
501
 
502
        ax25_remove_socket(ax25);
503
        ax25_clear_queues(ax25);        /* Flush the queues */
504
 
505
        if (ax25->sk != NULL) {
506
                while ((skb = skb_dequeue(&ax25->sk->receive_queue)) != NULL) {
507
                        if (skb->sk != ax25->sk) {                      /* A pending connection */
508
                                skb->sk->dead = 1;      /* Queue the unaccepted socket for death */
509
                                ax25_set_timer(skb->sk->protinfo.ax25);
510
                                skb->sk->protinfo.ax25->state = AX25_STATE_0;
511
                        }
512
 
513
                        kfree_skb(skb, FREE_READ);
514
                }
515
        }
516
 
517
        if (ax25->sk != NULL) {
518
                if (ax25->sk->wmem_alloc != 0 || ax25->sk->rmem_alloc != 0) {     /* Defer: outstanding buffers */
519
                        init_timer(&ax25->timer);
520
                        ax25->timer.expires  = jiffies + 10 * HZ;
521
                        ax25->timer.function = ax25_destroy_timer;
522
                        ax25->timer.data     = (unsigned long)ax25;
523
                        add_timer(&ax25->timer);
524
                } else {
525
                        sk_free(ax25->sk);
526
                        ax25_free_cb(ax25);
527
                }
528
        } else {
529
                ax25_free_cb(ax25);
530
        }
531
 
532
        restore_flags(flags);
533
}
534
 
535
/*
536
 *      Callsign/UID mapper. This is in kernel space for security on multi-amateur machines.
537
 */
538
 
539
ax25_uid_assoc *ax25_uid_list;
540
 
541
int ax25_uid_policy = 0;
542
 
543
ax25_address *ax25_findbyuid(uid_t uid)
544
{
545
        ax25_uid_assoc *a;
546
 
547
        for (a = ax25_uid_list; a != NULL; a = a->next) {
548
                if (a->uid == uid)
549
                        return &a->call;
550
        }
551
 
552
        return NULL;
553
}
554
 
555
static int ax25_uid_ioctl(int cmd, struct sockaddr_ax25 *sax)
556
{
557
        ax25_uid_assoc *a;
558
 
559
        switch (cmd) {
560
                case SIOCAX25GETUID:
561
                        for (a = ax25_uid_list; a != NULL; a = a->next) {
562
                                if (ax25cmp(&sax->sax25_call, &a->call) == 0)
563
                                        return a->uid;
564
                        }
565
                        return -ENOENT;
566
 
567
                case SIOCAX25ADDUID:
568
                        if (!suser())
569
                                return -EPERM;
570
                        if (ax25_findbyuid(sax->sax25_uid))
571
                                return -EEXIST;
572
                        if (sax->sax25_uid == 0)
573
                                return -EINVAL;
574
                        a = (ax25_uid_assoc *)kmalloc(sizeof(*a), GFP_KERNEL);
575
                        if (a == NULL)
576
                                return -ENOMEM;
577
                        a->uid  = sax->sax25_uid;
578
                        a->call = sax->sax25_call;
579
                        a->next = ax25_uid_list;
580
                        ax25_uid_list = a;
581
                        return 0;
582
 
583
                case SIOCAX25DELUID: {
584
                        ax25_uid_assoc **l;
585
 
586
                        if (!suser())
587
                                return -EPERM;
588
                        l = &ax25_uid_list;
589
                        while ((*l) != NULL) {
590
                                if (ax25cmp(&((*l)->call), &(sax->sax25_call)) == 0) {
591
                                        a = *l;
592
                                        *l = (*l)->next;
593
                                        kfree_s(a, sizeof(*a));
594
                                        return 0;
595
                                }
596
 
597
                                l = &((*l)->next);
598
                        }
599
                        return -ENOENT;
600
                }
601
 
602
                default:
603
                        return -EINVAL;
604
        }
605
 
606
        return -EINVAL; /*NOTREACHED */
607
}
608
 
609
/*
610
 * dl1bke 960311: set parameters for existing AX.25 connections,
611
 *                includes a KILL command to abort any connection.
612
 *                VERY useful for debugging ;-)
613
 */
614
static int ax25_ctl_ioctl(const unsigned int cmd, void *arg)
615
{
616
        struct ax25_ctl_struct ax25_ctl;
617
        struct device *dev;
618
        ax25_cb *ax25;
619
        unsigned long flags;
620
        int err;
621
 
622
        if ((err = verify_area(VERIFY_READ, arg, sizeof(ax25_ctl))) != 0)
623
                return err;
624
 
625
        memcpy_fromfs(&ax25_ctl, arg, sizeof(ax25_ctl));
626
 
627
        if ((dev = ax25rtr_get_dev(&ax25_ctl.port_addr)) == NULL)
628
                return -ENODEV;
629
 
630
        if ((ax25 = ax25_find_cb(&ax25_ctl.source_addr, &ax25_ctl.dest_addr, NULL, dev)) == NULL)
631
                return -ENOTCONN;
632
 
633
        switch (ax25_ctl.cmd) {
634
                case AX25_KILL:
635
                        ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
636
                        ax25_disconnect(ax25, ENETRESET);
637
                        ax25_set_timer(ax25);
638
                        break;
639
 
640
                case AX25_WINDOW:
641
                        if (ax25->modulus == AX25_MODULUS) {
642
                                if (ax25_ctl.arg < 1 || ax25_ctl.arg > 7)
643
                                        return -EINVAL;
644
                        } else {
645
                                if (ax25_ctl.arg < 1 || ax25_ctl.arg > 63)
646
                                        return -EINVAL;
647
                        }
648
                        ax25->window = ax25_ctl.arg;
649
                        break;
650
 
651
                case AX25_T1:
652
                        if (ax25_ctl.arg < 1)
653
                                return -EINVAL;
654
                        ax25->rtt = (ax25_ctl.arg * AX25_SLOWHZ) / 2;
655
                        ax25->t1  = ax25_ctl.arg * AX25_SLOWHZ;
656
                        save_flags(flags); cli();
657
                        if (ax25->t1timer > ax25->t1)
658
                                ax25->t1timer = ax25->t1;
659
                        restore_flags(flags);
660
                        break;
661
 
662
                case AX25_T2:
663
                        if (ax25_ctl.arg < 1)
664
                                return -EINVAL;
665
                        save_flags(flags); cli();
666
                        ax25->t2 = ax25_ctl.arg * AX25_SLOWHZ;
667
                        if (ax25->t2timer > ax25->t2)
668
                                ax25->t2timer = ax25->t2;
669
                        restore_flags(flags);
670
                        break;
671
 
672
                case AX25_N2:
673
                        if (ax25_ctl.arg < 1 || ax25_ctl.arg > 31)
674
                                return -EINVAL;
675
                        ax25->n2count = 0;
676
                        ax25->n2 = ax25_ctl.arg;
677
                        break;
678
 
679
                case AX25_T3:
680
                        if (ax25_ctl.arg < 0)
681
                                return -EINVAL;
682
                        save_flags(flags); cli();
683
                        ax25->t3 = ax25_ctl.arg * AX25_SLOWHZ;
684
                        if (ax25->t3timer != 0)
685
                                ax25->t3timer = ax25->t3;
686
                        restore_flags(flags);
687
                        break;
688
 
689
                case AX25_IDLE:
690
                        if (ax25_ctl.arg < 0)
691
                                return -EINVAL;
692
                        save_flags(flags); cli();
693
                        ax25->idle = ax25_ctl.arg * AX25_SLOWHZ * 60;
694
                        if (ax25->idletimer != 0)
695
                                ax25->idletimer = ax25->idle;
696
                        restore_flags(flags);
697
                        break;
698
 
699
                case AX25_PACLEN:
700
                        if (ax25_ctl.arg < 16 || ax25_ctl.arg > 65535)
701
                                return -EINVAL;
702
                        ax25->paclen = ax25_ctl.arg;
703
                        break;
704
 
705
                default:
706
                        return -EINVAL;
707
          }
708
 
709
          return 0;
710
}
711
 
712
/*
713
 *      Fill in a created AX.25 created control block with the default
714
 *      values for a particular device.
715
 */
716
static void ax25_fillin_cb(ax25_cb *ax25, struct device *dev)
717
{
718
        ax25->device = dev;
719
 
720
        if (dev != NULL) {
721
                ax25->rtt     = ax25_dev_get_value(dev, AX25_VALUES_T1) / 2;
722
                ax25->t1      = ax25_dev_get_value(dev, AX25_VALUES_T1);
723
                ax25->t2      = ax25_dev_get_value(dev, AX25_VALUES_T2);
724
                ax25->t3      = ax25_dev_get_value(dev, AX25_VALUES_T3);
725
                ax25->n2      = ax25_dev_get_value(dev, AX25_VALUES_N2);
726
                ax25->paclen  = ax25_dev_get_value(dev, AX25_VALUES_PACLEN);
727
                ax25->idle    = ax25_dev_get_value(dev, AX25_VALUES_IDLE);
728
                ax25->backoff = ax25_dev_get_value(dev, AX25_VALUES_BACKOFF);
729
 
730
                if (ax25_dev_get_value(dev, AX25_VALUES_AXDEFMODE)) {
731
                        ax25->modulus = AX25_EMODULUS;
732
                        ax25->window  = ax25_dev_get_value(dev, AX25_VALUES_EWINDOW);
733
                } else {
734
                        ax25->modulus = AX25_MODULUS;
735
                        ax25->window  = ax25_dev_get_value(dev, AX25_VALUES_WINDOW);
736
                }
737
        } else {
738
                ax25->rtt     = AX25_DEF_T1 / 2;
739
                ax25->t1      = AX25_DEF_T1;
740
                ax25->t2      = AX25_DEF_T2;
741
                ax25->t3      = AX25_DEF_T3;
742
                ax25->n2      = AX25_DEF_N2;
743
                ax25->paclen  = AX25_DEF_PACLEN;
744
                ax25->idle    = AX25_DEF_IDLE;
745
                ax25->backoff = AX25_DEF_BACKOFF;
746
 
747
                if (AX25_DEF_AXDEFMODE) {
748
                        ax25->modulus = AX25_EMODULUS;
749
                        ax25->window  = AX25_DEF_EWINDOW;
750
                } else {
751
                        ax25->modulus = AX25_MODULUS;
752
                        ax25->window  = AX25_DEF_WINDOW;
753
                }
754
        }
755
}
756
 
757
/*
758
 * Create an empty AX.25 control block.
759
 */
760
static ax25_cb *ax25_create_cb(void)
761
{
762
        ax25_cb *ax25;
763
 
764
        if ((ax25 = (ax25_cb *)kmalloc(sizeof(*ax25), GFP_ATOMIC)) == NULL)
765
                return NULL;
766
 
767
        MOD_INC_USE_COUNT;
768
 
769
        memset(ax25, 0x00, sizeof(*ax25));
770
 
771
        skb_queue_head_init(&ax25->write_queue);
772
        skb_queue_head_init(&ax25->frag_queue);
773
        skb_queue_head_init(&ax25->ack_queue);
774
        skb_queue_head_init(&ax25->reseq_queue);
775
 
776
        init_timer(&ax25->timer);
777
 
778
        ax25_fillin_cb(ax25, NULL);
779
 
780
        ax25->state = AX25_STATE_0;
781
 
782
        return ax25;
783
}
784
 
785
/*
786
 *      Find out if we are a DAMA slave for this device and count the
787
 *      number of connections.
788
 *
789
 *      dl1bke 951121
790
 */
791
int ax25_dev_is_dama_slave(struct device *dev)
792
{
793
        ax25_cb *ax25;
794
        int count = 0;
795
 
796
        for (ax25 = ax25_list; ax25 != NULL; ax25 = ax25->next) {
797
                if (ax25->device == dev && ax25->dama_slave) {
798
                        count++;
799
                        break;
800
                }
801
        }
802
 
803
        return count;
804
}
805
 
806
ax25_cb *ax25_send_frame(struct sk_buff *skb, int paclen, ax25_address *src, ax25_address *dest,
807
        ax25_digi *digi, struct device *dev)
808
{
809
        ax25_cb *ax25;
810
 
811
        if (paclen == 0)
812
                paclen = ax25_dev_get_value(dev, AX25_VALUES_PACLEN);
813
 
814
        /*
815
         * Look for an existing connection.
816
         */
817
        if ((ax25 = ax25_find_cb(src, dest, digi, dev)) != NULL) {
818
                ax25_output(ax25, paclen, skb);
819
                return ax25;            /* It already existed */
820
        }
821
 
822
        if ((ax25 = ax25_create_cb()) == NULL)
823
                return NULL;
824
 
825
        ax25_fillin_cb(ax25, dev);
826
 
827
        ax25->source_addr = *src;
828
        ax25->dest_addr   = *dest;
829
 
830
        if (digi != NULL) {
831
                if ((ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
832
                        ax25_free_cb(ax25);
833
                        return NULL;
834
                }
835
                *ax25->digipeat = *digi;
836
        }
837
 
838
        if (ax25_dev_is_dama_slave(ax25->device))
839
                dama_establish_data_link(ax25);
840
        else
841
                ax25_establish_data_link(ax25);
842
 
843
        ax25_insert_socket(ax25);
844
 
845
        ax25->state = AX25_STATE_1;
846
 
847
        ax25_set_timer(ax25);
848
 
849
        ax25_output(ax25, paclen, skb);
850
 
851
        return ax25;                    /* We had to create it */
852
}
853
 
854
/*
855
 *      Find the AX.25 device that matches the hardware address supplied.
856
 */
857
struct device *ax25rtr_get_dev(ax25_address *addr)
858
{
859
        struct device *dev;
860
 
861
        for (dev = dev_base; dev != NULL; dev = dev->next)
862
                if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25 &&
863
                    ax25cmp(addr, (ax25_address *)dev->dev_addr) == 0)
864
                        return dev;
865
 
866
        return NULL;
867
}
868
 
869
/*
870
 *      Handling for system calls applied via the various interfaces to an
871
 *      AX25 socket object
872
 */
873
static int ax25_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
874
{
875
        return -EINVAL;
876
}
877
 
878
static int ax25_setsockopt(struct socket *sock, int level, int optname,
879
        char *optval, int optlen)
880
{
881
        struct sock *sk = (struct sock *)sock->data;
882
        int err, opt;
883
 
884
        if (level == SOL_SOCKET)
885
                return sock_setsockopt(sk, level, optname, optval, optlen);
886
 
887
        if (level != SOL_AX25)
888
                return -EOPNOTSUPP;
889
 
890
        if (optval == NULL)
891
                return -EINVAL;
892
 
893
        if ((err = verify_area(VERIFY_READ, optval, sizeof(int))) != 0)
894
                return err;
895
 
896
        opt = get_fs_long((int *)optval);
897
 
898
        switch (optname) {
899
                case AX25_WINDOW:
900
                        if (sk->protinfo.ax25->modulus == AX25_MODULUS) {
901
                                if (opt < 1 || opt > 7)
902
                                        return -EINVAL;
903
                        } else {
904
                                if (opt < 1 || opt > 63)
905
                                        return -EINVAL;
906
                        }
907
                        sk->protinfo.ax25->window = opt;
908
                        return 0;
909
 
910
                case AX25_T1:
911
                        if (opt < 1)
912
                                return -EINVAL;
913
                        sk->protinfo.ax25->rtt = (opt * AX25_SLOWHZ) / 2;
914
                        sk->protinfo.ax25->t1  = opt * AX25_SLOWHZ;
915
                        return 0;
916
 
917
                case AX25_T2:
918
                        if (opt < 1)
919
                                return -EINVAL;
920
                        sk->protinfo.ax25->t2 = opt * AX25_SLOWHZ;
921
                        return 0;
922
 
923
                case AX25_N2:
924
                        if (opt < 1 || opt > 31)
925
                                return -EINVAL;
926
                        sk->protinfo.ax25->n2 = opt;
927
                        return 0;
928
 
929
                case AX25_T3:
930
                        if (opt < 1)
931
                                return -EINVAL;
932
                        sk->protinfo.ax25->t3 = opt * AX25_SLOWHZ;
933
                        return 0;
934
 
935
                case AX25_IDLE:
936
                        if (opt < 0)
937
                                return -EINVAL;
938
                        sk->protinfo.ax25->idle = opt * AX25_SLOWHZ * 60;
939
                        return 0;
940
 
941
                case AX25_BACKOFF:
942
                        if (opt < 0 || opt > 2)
943
                                return -EINVAL;
944
                        sk->protinfo.ax25->backoff = opt;
945
                        return 0;
946
 
947
                case AX25_EXTSEQ:
948
                        sk->protinfo.ax25->modulus = opt ? AX25_EMODULUS : AX25_MODULUS;
949
                        return 0;
950
 
951
                case AX25_PIDINCL:
952
                        sk->protinfo.ax25->pidincl = opt ? 1 : 0;
953
                        return 0;
954
 
955
                case AX25_IAMDIGI:
956
                        sk->protinfo.ax25->iamdigi = opt ? 1 : 0;
957
                        return 0;
958
 
959
                case AX25_PACLEN:
960
                        if (opt < 16 || opt > 65535)
961
                                return -EINVAL;
962
                        sk->protinfo.ax25->paclen = opt;
963
                        return 0;
964
 
965
                default:
966
                        return -ENOPROTOOPT;
967
        }
968
}
969
 
970
static int ax25_getsockopt(struct socket *sock, int level, int optname,
971
        char *optval, int *optlen)
972
{
973
        struct sock *sk = (struct sock *)sock->data;
974
        int val = 0;
975
        int err;
976
 
977
        if (level == SOL_SOCKET)
978
                return sock_getsockopt(sk, level, optname, optval, optlen);
979
 
980
        if (level != SOL_AX25)
981
                return -EOPNOTSUPP;
982
 
983
        switch (optname) {
984
                case AX25_WINDOW:
985
                        val = sk->protinfo.ax25->window;
986
                        break;
987
 
988
                case AX25_T1:
989
                        val = sk->protinfo.ax25->t1 / AX25_SLOWHZ;
990
                        break;
991
 
992
                case AX25_T2:
993
                        val = sk->protinfo.ax25->t2 / AX25_SLOWHZ;
994
                        break;
995
 
996
                case AX25_N2:
997
                        val = sk->protinfo.ax25->n2;
998
                        break;
999
 
1000
                case AX25_T3:
1001
                        val = sk->protinfo.ax25->t3 / AX25_SLOWHZ;
1002
                        break;
1003
 
1004
                case AX25_IDLE:
1005
                        val = sk->protinfo.ax25->idle / (AX25_SLOWHZ * 60);
1006
                        break;
1007
 
1008
                case AX25_BACKOFF:
1009
                        val = sk->protinfo.ax25->backoff;
1010
                        break;
1011
 
1012
                case AX25_EXTSEQ:
1013
                        val = (sk->protinfo.ax25->modulus == AX25_EMODULUS);
1014
                        break;
1015
 
1016
                case AX25_PIDINCL:
1017
                        val = sk->protinfo.ax25->pidincl;
1018
                        break;
1019
 
1020
                case AX25_IAMDIGI:
1021
                        val = sk->protinfo.ax25->iamdigi;
1022
                        break;
1023
 
1024
                case AX25_PACLEN:
1025
                        val = sk->protinfo.ax25->paclen;
1026
                        break;
1027
 
1028
                default:
1029
                        return -ENOPROTOOPT;
1030
        }
1031
 
1032
        if ((err = verify_area(VERIFY_WRITE, optlen, sizeof(int))) != 0)
1033
                return err;
1034
 
1035
        put_user(sizeof(int), optlen);
1036
 
1037
        if ((err = verify_area(VERIFY_WRITE, optval, sizeof(int))) != 0)
1038
                return err;
1039
 
1040
        put_user(val, (int *)optval);
1041
 
1042
        return 0;
1043
}
1044
 
1045
static int ax25_listen(struct socket *sock, int backlog)
1046
{
1047
        struct sock *sk = (struct sock *)sock->data;
1048
 
1049
        if (sk->type == SOCK_SEQPACKET && sk->state != TCP_LISTEN) {
1050
                sk->max_ack_backlog = backlog;
1051
                sk->state           = TCP_LISTEN;
1052
                return 0;
1053
        }
1054
 
1055
        return -EOPNOTSUPP;
1056
}
1057
 
1058
static void def_callback1(struct sock *sk)
1059
{
1060
        if (!sk->dead)
1061
                wake_up_interruptible(sk->sleep);
1062
}
1063
 
1064
static void def_callback2(struct sock *sk, int len)
1065
{
1066
        if (!sk->dead)
1067
                wake_up_interruptible(sk->sleep);
1068
}
1069
 
1070
static int ax25_create(struct socket *sock, int protocol)
1071
{
1072
        struct sock *sk;
1073
        ax25_cb *ax25;
1074
 
1075
        switch (sock->type) {
1076
                case SOCK_DGRAM:
1077
                        if (protocol == 0 || protocol == AF_AX25)
1078
                                protocol = AX25_P_TEXT;
1079
                        break;
1080
                case SOCK_SEQPACKET:
1081
                        switch (protocol) {
1082
                                case 0:
1083
                                case AF_AX25:   /* For CLX */
1084
                                        protocol = AX25_P_TEXT;
1085
                                        break;
1086
                                case AX25_P_SEGMENT:
1087
#ifdef CONFIG_INET
1088
                                case AX25_P_ARP:
1089
                                case AX25_P_IP:
1090
#endif
1091
#ifdef CONFIG_NETROM
1092
                                case AX25_P_NETROM:
1093
#endif
1094
#ifdef CONFIG_ROSE
1095
                                case AX25_P_ROSE:
1096
#endif
1097
                                        return -ESOCKTNOSUPPORT;
1098
#ifdef CONFIG_NETROM_MODULE
1099
                                case AX25_P_NETROM:
1100
                                        if (ax25_protocol_is_registered(AX25_P_NETROM))
1101
                                                return -ESOCKTNOSUPPORT;
1102
#endif
1103
#ifdef CONFIG_ROSE_MODULE
1104
                                case AX25_P_ROSE:
1105
                                        if (ax25_protocol_is_registered(AX25_P_ROSE))
1106
                                                return -ESOCKTNOSUPPORT;
1107
#endif
1108
                                default:
1109
                                        break;
1110
                        }
1111
                        break;
1112
                case SOCK_RAW:
1113
                        break;
1114
                default:
1115
                        return -ESOCKTNOSUPPORT;
1116
        }
1117
 
1118
        if ((sk = sk_alloc(GFP_ATOMIC)) == NULL)
1119
                return -ENOMEM;
1120
 
1121
        if ((ax25 = ax25_create_cb()) == NULL) {
1122
                sk_free(sk);
1123
                return -ENOMEM;
1124
        }
1125
 
1126
        skb_queue_head_init(&sk->receive_queue);
1127
        skb_queue_head_init(&sk->write_queue);
1128
        skb_queue_head_init(&sk->back_log);
1129
 
1130
        sk->socket        = sock;
1131
        sk->type          = sock->type;
1132
        sk->protocol      = protocol;
1133
        sk->next          = NULL;
1134
        sk->allocation    = GFP_KERNEL;
1135
        sk->rcvbuf        = SK_RMEM_MAX;
1136
        sk->sndbuf        = SK_WMEM_MAX;
1137
        sk->state         = TCP_CLOSE;
1138
        sk->priority      = SOPRI_NORMAL;
1139
        sk->mtu           = AX25_MTU;   /* 256 */
1140
        sk->zapped        = 1;
1141
 
1142
        sk->state_change = def_callback1;
1143
        sk->data_ready   = def_callback2;
1144
        sk->write_space  = def_callback1;
1145
        sk->error_report = def_callback1;
1146
 
1147
        if (sock != NULL) {
1148
                sock->data = (void *)sk;
1149
                sk->sleep  = sock->wait;
1150
        }
1151
 
1152
        ax25->sk          = sk;
1153
        sk->protinfo.ax25 = ax25;
1154
 
1155
        return 0;
1156
}
1157
 
1158
static struct sock *ax25_make_new(struct sock *osk, struct device *dev)
1159
{
1160
        struct sock *sk;
1161
        ax25_cb *ax25;
1162
 
1163
        if ((sk = sk_alloc(GFP_ATOMIC)) == NULL)
1164
                return NULL;
1165
 
1166
        if ((ax25 = ax25_create_cb()) == NULL) {
1167
                sk_free(sk);
1168
                return NULL;
1169
        }
1170
 
1171
        ax25_fillin_cb(ax25, dev);
1172
 
1173
        sk->type   = osk->type;
1174
        sk->socket = osk->socket;
1175
 
1176
        switch (osk->type) {
1177
                case SOCK_DGRAM:
1178
                        break;
1179
                case SOCK_SEQPACKET:
1180
                        break;
1181
                default:
1182
                        sk_free(sk);
1183
                        ax25_free_cb(ax25);
1184
                        return NULL;
1185
        }
1186
 
1187
        skb_queue_head_init(&sk->receive_queue);
1188
        skb_queue_head_init(&sk->write_queue);
1189
        skb_queue_head_init(&sk->back_log);
1190
 
1191
        sk->next        = NULL;
1192
        sk->priority    = osk->priority;
1193
        sk->protocol    = osk->protocol;
1194
        sk->rcvbuf      = osk->rcvbuf;
1195
        sk->sndbuf      = osk->sndbuf;
1196
        sk->debug       = osk->debug;
1197
        sk->state       = TCP_ESTABLISHED;
1198
        sk->mtu         = osk->mtu;
1199
        sk->sleep       = osk->sleep;
1200
        sk->zapped      = osk->zapped;
1201
 
1202
        sk->state_change = def_callback1;
1203
        sk->data_ready   = def_callback2;
1204
        sk->write_space  = def_callback1;
1205
        sk->error_report = def_callback1;
1206
 
1207
        ax25->modulus = osk->protinfo.ax25->modulus;
1208
        ax25->backoff = osk->protinfo.ax25->backoff;
1209
        ax25->pidincl = osk->protinfo.ax25->pidincl;
1210
        ax25->iamdigi = osk->protinfo.ax25->iamdigi;
1211
        ax25->rtt     = osk->protinfo.ax25->rtt;
1212
        ax25->t1      = osk->protinfo.ax25->t1;
1213
        ax25->t2      = osk->protinfo.ax25->t2;
1214
        ax25->t3      = osk->protinfo.ax25->t3;
1215
        ax25->n2      = osk->protinfo.ax25->n2;
1216
        ax25->idle    = osk->protinfo.ax25->idle;
1217
        ax25->paclen  = osk->protinfo.ax25->paclen;
1218
        ax25->window  = osk->protinfo.ax25->window;
1219
 
1220
        ax25->source_addr = osk->protinfo.ax25->source_addr;
1221
 
1222
        if (osk->protinfo.ax25->digipeat != NULL) {
1223
                if ((ax25->digipeat = (ax25_digi *)kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
1224
                        sk_free(sk);
1225
                        ax25_free_cb(ax25);
1226
                        return NULL;
1227
                }
1228
 
1229
                *ax25->digipeat = *osk->protinfo.ax25->digipeat;
1230
        }
1231
 
1232
        sk->protinfo.ax25 = ax25;
1233
        ax25->sk          = sk;
1234
 
1235
        return sk;
1236
}
1237
 
1238
static int ax25_dup(struct socket *newsock, struct socket *oldsock)
1239
{
1240
        struct sock *sk = (struct sock *)oldsock->data;
1241
 
1242
        if (sk == NULL || newsock == NULL)
1243
                return -EINVAL;
1244
 
1245
        return ax25_create(newsock, sk->protocol);
1246
}
1247
 
1248
static int ax25_release(struct socket *sock, struct socket *peer)
1249
{
1250
        struct sock *sk = (struct sock *)sock->data;
1251
 
1252
        if (sk == NULL) return 0;
1253
 
1254
        if (sk->type == SOCK_SEQPACKET) {
1255
                switch (sk->protinfo.ax25->state) {
1256
                        case AX25_STATE_0:
1257
                                ax25_disconnect(sk->protinfo.ax25, 0);
1258
                                ax25_destroy_socket(sk->protinfo.ax25);
1259
                                break;
1260
 
1261
                        case AX25_STATE_1:
1262
                                ax25_send_control(sk->protinfo.ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
1263
                                ax25_disconnect(sk->protinfo.ax25, 0);
1264
                                ax25_destroy_socket(sk->protinfo.ax25);
1265
                                break;
1266
 
1267
                        case AX25_STATE_2:
1268
                                if (sk->protinfo.ax25->dama_slave)
1269
                                        ax25_send_control(sk->protinfo.ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
1270
                                else
1271
                                        ax25_send_control(sk->protinfo.ax25, AX25_DM, AX25_POLLON, AX25_RESPONSE);
1272
                                ax25_disconnect(sk->protinfo.ax25, 0);
1273
                                ax25_destroy_socket(sk->protinfo.ax25);
1274
                                break;
1275
 
1276
                        case AX25_STATE_3:
1277
                        case AX25_STATE_4:
1278
                                ax25_clear_queues(sk->protinfo.ax25);
1279
                                sk->protinfo.ax25->n2count = 0;
1280
                                if (!sk->protinfo.ax25->dama_slave) {
1281
                                        ax25_send_control(sk->protinfo.ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
1282
                                        sk->protinfo.ax25->t3timer = 0;
1283
                                } else {
1284
                                        sk->protinfo.ax25->t3timer = sk->protinfo.ax25->t3;     /* DAMA slave timeout */
1285
                                }
1286
                                sk->protinfo.ax25->t1timer = sk->protinfo.ax25->t1 = ax25_calculate_t1(sk->protinfo.ax25);
1287
                                sk->protinfo.ax25->state   = AX25_STATE_2;
1288
                                sk->state                  = TCP_CLOSE;
1289
                                sk->shutdown              |= SEND_SHUTDOWN;
1290
                                sk->state_change(sk);
1291
                                sk->dead                   = 1;
1292
                                sk->destroy                = 1;
1293
                                break;
1294
 
1295
                        default:
1296
                                break;
1297
                }
1298
        } else {
1299
                sk->state       = TCP_CLOSE;
1300
                sk->shutdown   |= SEND_SHUTDOWN;
1301
                sk->state_change(sk);
1302
                sk->dead        = 1;
1303
                ax25_destroy_socket(sk->protinfo.ax25);
1304
        }
1305
 
1306
        sock->data = NULL;
1307
        sk->socket = NULL;      /* Not used, but we should do this. **/
1308
 
1309
        return 0;
1310
}
1311
 
1312
/*
1313
 *      We support a funny extension here so you can (as root) give any callsign
1314
 *      digipeated via a local address as source. This is a hack until we add
1315
 *      BSD 4.4 ADDIFADDR type support. It is however small and trivially backward
1316
 *      compatible 8)
1317
 */
1318
static int ax25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1319
{
1320
        struct sock *sk = (struct sock *)sock->data;
1321
        struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
1322
        struct device *dev;
1323
        ax25_address *call;
1324
 
1325
        if (sk->zapped == 0)
1326
                return -EINVAL;
1327
 
1328
        if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25))
1329
                return -EINVAL;
1330
 
1331
        if (addr->fsa_ax25.sax25_family != AF_AX25)
1332
                return -EINVAL;
1333
 
1334
        call = ax25_findbyuid(current->euid);
1335
        if (call == NULL && ax25_uid_policy && !suser())
1336
                return -EACCES;
1337
 
1338
        if (call == NULL)
1339
                sk->protinfo.ax25->source_addr = addr->fsa_ax25.sax25_call;
1340
        else
1341
                sk->protinfo.ax25->source_addr = *call;
1342
 
1343
        if (sk->debug)
1344
                printk("AX25: source address set to %s\n", ax2asc(&sk->protinfo.ax25->source_addr));
1345
 
1346
        if (addr_len == sizeof(struct full_sockaddr_ax25) && addr->fsa_ax25.sax25_ndigis == 1) {
1347
                if (ax25cmp(&addr->fsa_digipeater[0], &null_ax25_address) == 0) {
1348
                        dev = NULL;
1349
                        if (sk->debug)
1350
                                printk("AX25: bound to any device\n");
1351
                } else {
1352
                        if ((dev = ax25rtr_get_dev(&addr->fsa_digipeater[0])) == NULL) {
1353
                                if (sk->debug)
1354
                                        printk("AX25: bind failed - no device\n");
1355
                                return -EADDRNOTAVAIL;
1356
                        }
1357
                        if (sk->debug)
1358
                                printk("AX25: bound to device %s\n", dev->name);
1359
                }
1360
        } else {
1361
                if ((dev = ax25rtr_get_dev(&addr->fsa_ax25.sax25_call)) == NULL) {
1362
                        if (sk->debug)
1363
                                printk("AX25: bind failed - no device\n");
1364
                        return -EADDRNOTAVAIL;
1365
                }
1366
                if (sk->debug)
1367
                        printk("AX25: bound to device %s\n", dev->name);
1368
        }
1369
 
1370
        ax25_fillin_cb(sk->protinfo.ax25, dev);
1371
        ax25_insert_socket(sk->protinfo.ax25);
1372
 
1373
        sk->zapped = 0;
1374
 
1375
        if (sk->debug)
1376
                printk("AX25: socket is bound\n");
1377
 
1378
        return 0;
1379
}
1380
 
1381
static int ax25_connect(struct socket *sock, struct sockaddr *uaddr,
1382
        int addr_len, int flags)
1383
{
1384
        struct sock *sk = (struct sock *)sock->data;
1385
        struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)uaddr;
1386
        ax25_digi *digi = NULL;
1387
        int ct = 0, err;
1388
 
1389
        if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
1390
                sock->state = SS_CONNECTED;
1391
                return 0;        /* Connect completed during a ERESTARTSYS event */
1392
        }
1393
 
1394
        if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
1395
                sock->state = SS_UNCONNECTED;
1396
                return -ECONNREFUSED;
1397
        }
1398
 
1399
        if (sk->state == TCP_ESTABLISHED && sk->type == SOCK_SEQPACKET)
1400
                return -EISCONN;        /* No reconnect on a seqpacket socket */
1401
 
1402
        sk->state   = TCP_CLOSE;
1403
        sock->state = SS_UNCONNECTED;
1404
 
1405
        if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25))
1406
                return -EINVAL;
1407
 
1408
        if (fsa->fsa_ax25.sax25_family != AF_AX25)
1409
                return -EINVAL;
1410
 
1411
        if (sk->protinfo.ax25->digipeat != NULL) {
1412
                kfree_s(sk->protinfo.ax25->digipeat, sizeof(ax25_digi));
1413
                sk->protinfo.ax25->digipeat = NULL;
1414
        }
1415
 
1416
        /*
1417
         *      Handle digi-peaters to be used.
1418
         */
1419
        if (addr_len == sizeof(struct full_sockaddr_ax25) && fsa->fsa_ax25.sax25_ndigis != 0) {
1420
                /* Valid number of digipeaters ? */
1421
                if (fsa->fsa_ax25.sax25_ndigis < 1 || fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS)
1422
                        return -EINVAL;
1423
 
1424
                if ((digi = (ax25_digi *)kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL)
1425
                        return -ENOBUFS;
1426
 
1427
                digi->ndigi      = fsa->fsa_ax25.sax25_ndigis;
1428
                digi->lastrepeat = -1;
1429
 
1430
                while (ct < fsa->fsa_ax25.sax25_ndigis) {
1431
                        if ((fsa->fsa_digipeater[ct].ax25_call[6] & AX25_HBIT) && sk->protinfo.ax25->iamdigi) {
1432
                                digi->repeated[ct] = 1;
1433
                                digi->lastrepeat   = ct;
1434
                        } else {
1435
                                digi->repeated[ct] = 0;
1436
                        }
1437
                        digi->calls[ct] = fsa->fsa_digipeater[ct];
1438
                        ct++;
1439
                }
1440
        }
1441
 
1442
        /*
1443
         *      Must bind first - autobinding in this may or may not work. If
1444
         *      the socket is already bound, check to see if the device has
1445
         *      been filled in, error if it hasn't.
1446
         */
1447
        if (sk->zapped) {
1448
                if ((err = ax25_rt_autobind(sk->protinfo.ax25, &fsa->fsa_ax25.sax25_call)) < 0)
1449
                        return err;
1450
                ax25_fillin_cb(sk->protinfo.ax25, sk->protinfo.ax25->device);
1451
                ax25_insert_socket(sk->protinfo.ax25);
1452
        } else {
1453
                if (sk->protinfo.ax25->device == NULL)
1454
                        return -EHOSTUNREACH;
1455
        }
1456
 
1457
        if (sk->type == SOCK_SEQPACKET && ax25_find_cb(&sk->protinfo.ax25->source_addr, &fsa->fsa_ax25.sax25_call, digi, sk->protinfo.ax25->device) != NULL) {
1458
                if (digi != NULL) kfree_s(digi, sizeof(ax25_digi));
1459
                return -EADDRINUSE;                     /* Already such a connection */
1460
        }
1461
 
1462
        sk->protinfo.ax25->dest_addr = fsa->fsa_ax25.sax25_call;
1463
        sk->protinfo.ax25->digipeat  = digi;
1464
 
1465
        /* First the easy one */
1466
        if (sk->type != SOCK_SEQPACKET) {
1467
                sock->state = SS_CONNECTED;
1468
                sk->state   = TCP_ESTABLISHED;
1469
                return 0;
1470
        }
1471
 
1472
        /* Move to connecting socket, ax.25 lapb WAIT_UA.. */
1473
        sock->state        = SS_CONNECTING;
1474
        sk->state          = TCP_SYN_SENT;
1475
 
1476
        if (ax25_dev_is_dama_slave(sk->protinfo.ax25->device))
1477
                dama_establish_data_link(sk->protinfo.ax25);
1478
        else
1479
                ax25_establish_data_link(sk->protinfo.ax25);
1480
 
1481
        sk->protinfo.ax25->state = AX25_STATE_1;
1482
        ax25_set_timer(sk->protinfo.ax25);              /* Start going SABM SABM until a UA or a give up and DM */
1483
 
1484
        /* Now the loop */
1485
        if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
1486
                return -EINPROGRESS;
1487
 
1488
        cli();  /* To avoid races on the sleep */
1489
 
1490
        /* A DM or timeout will go to closed, a UA will go to ABM */
1491
        while (sk->state == TCP_SYN_SENT) {
1492
                interruptible_sleep_on(sk->sleep);
1493
                if (current->signal & ~current->blocked) {
1494
                        sti();
1495
                        return -ERESTARTSYS;
1496
                }
1497
        }
1498
 
1499
        if (sk->state != TCP_ESTABLISHED) {
1500
                /* Not in ABM, not in WAIT_UA -> failed */
1501
                sti();
1502
                sock->state = SS_UNCONNECTED;
1503
                return sock_error(sk);  /* Always set at this point */
1504
        }
1505
 
1506
        sock->state = SS_CONNECTED;
1507
 
1508
        sti();
1509
 
1510
        return 0;
1511
}
1512
 
1513
static int ax25_socketpair(struct socket *sock1, struct socket *sock2)
1514
{
1515
        return -EOPNOTSUPP;
1516
}
1517
 
1518
static int ax25_accept(struct socket *sock, struct socket *newsock, int flags)
1519
{
1520
        struct sock *sk;
1521
        struct sock *newsk;
1522
        struct sk_buff *skb;
1523
 
1524
        if (newsock->data != NULL) {
1525
                sk = (struct sock *)newsock->data;
1526
                ax25_destroy_socket(sk->protinfo.ax25);
1527
        }
1528
 
1529
        newsock->data = NULL;
1530
 
1531
        if ((sk = (struct sock *)sock->data) == NULL)
1532
                return -EINVAL;
1533
 
1534
        if (sk->type != SOCK_SEQPACKET)
1535
                return -EOPNOTSUPP;
1536
 
1537
        if (sk->state != TCP_LISTEN)
1538
                return -EINVAL;
1539
 
1540
        /*
1541
         *      The write queue this time is holding sockets ready to use
1542
         *      hooked into the SABM we saved
1543
         */
1544
        do {
1545
                cli();
1546
                if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
1547
                        if (flags & O_NONBLOCK) {
1548
                                sti();
1549
                                return -EWOULDBLOCK;
1550
                        }
1551
                        interruptible_sleep_on(sk->sleep);
1552
                        if (current->signal & ~current->blocked) {
1553
                                sti();
1554
                                return -ERESTARTSYS;
1555
                        }
1556
                }
1557
        } while (skb == NULL);
1558
 
1559
        newsk = skb->sk;
1560
        newsk->pair = NULL;
1561
        newsk->socket = newsock;
1562
        newsk->sleep = newsock->wait;
1563
        sti();
1564
 
1565
        /* Now attach up the new socket */
1566
        skb->sk = NULL;
1567
        kfree_skb(skb, FREE_READ);
1568
        sk->ack_backlog--;
1569
        newsock->data = newsk;
1570
 
1571
        return 0;
1572
}
1573
 
1574
static int ax25_getname(struct socket *sock, struct sockaddr *uaddr,
1575
        int *uaddr_len, int peer)
1576
{
1577
        struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
1578
        struct sock *sk = (struct sock *)sock->data;
1579
        unsigned char ndigi, i;
1580
 
1581
        if (peer != 0) {
1582
                if (sk->state != TCP_ESTABLISHED)
1583
                        return -ENOTCONN;
1584
 
1585
                sax->fsa_ax25.sax25_family = AF_AX25;
1586
                sax->fsa_ax25.sax25_call   = sk->protinfo.ax25->dest_addr;
1587
                sax->fsa_ax25.sax25_ndigis = 0;
1588
                *uaddr_len = sizeof(struct full_sockaddr_ax25);
1589
 
1590
                if (sk->protinfo.ax25->digipeat != NULL) {
1591
                        ndigi = sk->protinfo.ax25->digipeat->ndigi;
1592
                        sax->fsa_ax25.sax25_ndigis = ndigi;
1593
                        for (i = 0; i < ndigi; i++)
1594
                                sax->fsa_digipeater[i] = sk->protinfo.ax25->digipeat->calls[i];
1595
                }
1596
        } else {
1597
                sax->fsa_ax25.sax25_family = AF_AX25;
1598
                sax->fsa_ax25.sax25_call   = sk->protinfo.ax25->source_addr;
1599
                sax->fsa_ax25.sax25_ndigis = 1;
1600
                *uaddr_len = sizeof(struct full_sockaddr_ax25);
1601
 
1602
                if (sk->protinfo.ax25->device != NULL)
1603
                        memcpy(&sax->fsa_digipeater[0], sk->protinfo.ax25->device->dev_addr, AX25_ADDR_LEN);
1604
                else
1605
                        sax->fsa_digipeater[0] = null_ax25_address;
1606
        }
1607
 
1608
        return 0;
1609
}
1610
 
1611
static int ax25_rcv(struct sk_buff *skb, struct device *dev, ax25_address *dev_addr, struct packet_type *ptype)
1612
{
1613
        struct sock *make;
1614
        struct sock *sk;
1615
        int type = 0;
1616
        ax25_digi dp, reverse_dp;
1617
        struct sk_buff *skbn;
1618
        ax25_cb *ax25;
1619
        ax25_address src, dest;
1620
        ax25_address *next_digi = NULL;
1621
        struct sock *raw;
1622
        int mine = 0;
1623
        int dama;
1624
 
1625
        /*
1626
         *      Process the AX.25/LAPB frame.
1627
         */
1628
 
1629
        skb->h.raw = skb->data;
1630
 
1631
#ifdef CONFIG_FIREWALL
1632
        if (call_in_firewall(PF_AX25, skb->dev, skb->h.raw, NULL) != FW_ACCEPT) {
1633
                kfree_skb(skb, FREE_READ);
1634
                return 0;
1635
        }
1636
#endif
1637
 
1638
        /*
1639
         *      Parse the address header.
1640
         */
1641
 
1642
        if (ax25_parse_addr(skb->data, skb->len, &src, &dest, &dp, &type, &dama) == NULL) {
1643
                kfree_skb(skb, FREE_READ);
1644
                return 0;
1645
        }
1646
 
1647
        /*
1648
         *      Ours perhaps ?
1649
         */
1650
        if (dp.lastrepeat + 1 < dp.ndigi)               /* Not yet digipeated completely */
1651
                next_digi = &dp.calls[dp.lastrepeat + 1];
1652
 
1653
        /*
1654
         *      Pull of the AX.25 headers leaving the CTRL/PID bytes
1655
         */
1656
        skb_pull(skb, size_ax25_addr(&dp));
1657
 
1658
        /* For our port addresses ? */
1659
        if (ax25cmp(&dest, dev_addr) == 0 && dp.lastrepeat + 1 == dp.ndigi)
1660
                mine = 1;
1661
 
1662
        /* Also match on any registered callsign from L3/4 */
1663
        if (!mine && ax25_listen_mine(&dest, dev) && dp.lastrepeat + 1 == dp.ndigi)
1664
                mine = 1;
1665
 
1666
        /* UI frame - bypass LAPB processing */
1667
        if ((*skb->data & ~0x10) == AX25_UI && dp.lastrepeat + 1 == dp.ndigi) {
1668
                skb->h.raw = skb->data + 2;             /* skip control and pid */
1669
 
1670
                if ((raw = ax25_addr_match(&dest)) != NULL)
1671
                        ax25_send_to_raw(raw, skb, skb->data[1]);
1672
 
1673
                if (!mine && ax25cmp(&dest, (ax25_address *)dev->broadcast) != 0) {
1674
                        kfree_skb(skb, FREE_READ);
1675
                        return 0;
1676
                }
1677
 
1678
                /* Now we are pointing at the pid byte */
1679
                switch (skb->data[1]) {
1680
#ifdef CONFIG_INET
1681
                        case AX25_P_IP:
1682
                                if ((skbn = skb_copy(skb, GFP_ATOMIC)) != NULL) {
1683
                                        kfree_skb(skb, FREE_READ);
1684
                                        skb = skbn;
1685
                                }
1686
                                skb_pull(skb,2);                /* drop PID/CTRL */
1687
                                ip_rcv(skb, dev, ptype);        /* Note ptype here is the wrong one, fix me later */
1688
                                break;
1689
 
1690
                        case AX25_P_ARP:
1691
                                skb_pull(skb,2);
1692
                                arp_rcv(skb, dev, ptype);       /* Note ptype here is wrong... */
1693
                                break;
1694
#endif
1695
                        case AX25_P_TEXT:
1696
                                /* Now find a suitable dgram socket */
1697
                                if ((sk = ax25_find_socket(&dest, &src, SOCK_DGRAM)) != NULL) {
1698
                                        if (sk->rmem_alloc >= sk->rcvbuf) {
1699
                                                kfree_skb(skb, FREE_READ);
1700
                                        } else {
1701
                                                /*
1702
                                                 *      Remove the control and PID.
1703
                                                 */
1704
                                                skb_pull(skb, 2);
1705
                                                if (sock_queue_rcv_skb(sk, skb) != 0)
1706
                                                        kfree_skb(skb, FREE_READ);
1707
                                        }
1708
                                } else {
1709
                                        kfree_skb(skb, FREE_READ);
1710
                                }
1711
                                break;
1712
 
1713
                        default:
1714
                                kfree_skb(skb, FREE_READ);      /* Will scan SOCK_AX25 RAW sockets */
1715
                                break;
1716
                }
1717
 
1718
                return 0;
1719
        }
1720
 
1721
        /*
1722
         *      Is connected mode supported on this device ?
1723
         *      If not, should we DM the incoming frame (except DMs) or
1724
         *      silently ignore them. For now we stay quiet.
1725
         */
1726
        if (ax25_dev_get_value(dev, AX25_VALUES_CONMODE) == 0) {
1727
                kfree_skb(skb, FREE_READ);
1728
                return 0;
1729
        }
1730
 
1731
        /* LAPB */
1732
 
1733
        /* AX.25 state 1-4 */
1734
 
1735
        ax25_digi_invert(&dp, &reverse_dp);
1736
 
1737
        if ((ax25 = ax25_find_cb(&dest, &src, &reverse_dp, dev)) != NULL) {
1738
                /*
1739
                 *      Process the frame. If it is queued up internally it returns one otherwise we
1740
                 *      free it immediately. This routine itself wakes the user context layers so we
1741
                 *      do no further work
1742
                 */
1743
                if (ax25_process_rx_frame(ax25, skb, type, dama) == 0)
1744
                        kfree_skb(skb, FREE_READ);
1745
 
1746
                return 0;
1747
        }
1748
 
1749
        /* AX.25 state 0 (disconnected) */
1750
 
1751
        /* a) received not a SABM(E) */
1752
 
1753
        if ((*skb->data & ~AX25_PF) != AX25_SABM && (*skb->data & ~AX25_PF) != AX25_SABME) {
1754
                /*
1755
                 *      Never reply to a DM. Also ignore any connects for
1756
                 *      addresses that are not our interfaces and not a socket.
1757
                 */
1758
                if ((*skb->data & ~AX25_PF) != AX25_DM && mine)
1759
                        ax25_return_dm(dev, &src, &dest, &dp);
1760
 
1761
                kfree_skb(skb, FREE_READ);
1762
                return 0;
1763
        }
1764
 
1765
        /* b) received SABM(E) */
1766
 
1767
        if (dp.lastrepeat + 1 == dp.ndigi)
1768
                sk = ax25_find_listener(&dest, 0, dev, SOCK_SEQPACKET);
1769
        else
1770
                sk = ax25_find_listener(next_digi, 1, dev, SOCK_SEQPACKET);
1771
 
1772
        if (sk != NULL) {
1773
                if (sk->ack_backlog == sk->max_ack_backlog || (make = ax25_make_new(sk, dev)) == NULL) {
1774
                        if (mine)
1775
                                ax25_return_dm(dev, &src, &dest, &dp);
1776
 
1777
                        kfree_skb(skb, FREE_READ);
1778
                        return 0;
1779
                }
1780
 
1781
                ax25 = make->protinfo.ax25;
1782
 
1783
                skb_queue_head(&sk->receive_queue, skb);
1784
 
1785
                skb->sk     = make;
1786
                make->state = TCP_ESTABLISHED;
1787
                make->pair  = sk;
1788
 
1789
                sk->ack_backlog++;
1790
        } else {
1791
                if (!mine) {
1792
                        kfree_skb(skb, FREE_READ);
1793
                        return 0;
1794
                }
1795
 
1796
                if ((ax25 = ax25_create_cb()) == NULL) {
1797
                        ax25_return_dm(dev, &src, &dest, &dp);
1798
                        kfree_skb(skb, FREE_READ);
1799
                        return 0;
1800
                }
1801
 
1802
                ax25_fillin_cb(ax25, dev);
1803
        }
1804
 
1805
        ax25->source_addr = dest;
1806
        ax25->dest_addr   = src;
1807
 
1808
        /*
1809
         *      Sort out any digipeated paths.
1810
         */
1811
        if (dp.ndigi != 0 && ax25->digipeat == NULL && (ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
1812
                kfree_skb(skb, FREE_READ);
1813
                ax25_destroy_socket(ax25);
1814
                return 0;
1815
        }
1816
 
1817
        if (dp.ndigi == 0) {
1818
                if (ax25->digipeat != NULL) {
1819
                        kfree_s(ax25->digipeat, sizeof(ax25_digi));
1820
                        ax25->digipeat = NULL;
1821
                }
1822
        } else {
1823
                /* Reverse the source SABM's path */
1824
                *ax25->digipeat = reverse_dp;
1825
        }
1826
 
1827
        if ((*skb->data & ~AX25_PF) == AX25_SABME) {
1828
                ax25->modulus = AX25_EMODULUS;
1829
                ax25->window  = ax25_dev_get_value(dev, AX25_VALUES_EWINDOW);
1830
        } else {
1831
                ax25->modulus = AX25_MODULUS;
1832
                ax25->window  = ax25_dev_get_value(dev, AX25_VALUES_WINDOW);
1833
        }
1834
 
1835
        ax25->device = dev;
1836
 
1837
        ax25_send_control(ax25, AX25_UA, AX25_POLLON, AX25_RESPONSE);
1838
 
1839
        if (dama) ax25_dama_on(ax25);   /* bke 951121 */
1840
 
1841
        ax25->dama_slave = dama;
1842
        ax25->t3timer    = ax25->t3;
1843
        ax25->idletimer  = ax25->idle;
1844
 
1845
        ax25->state   = AX25_STATE_3;
1846
 
1847
        ax25_insert_socket(ax25);
1848
 
1849
        ax25_set_timer(ax25);
1850
 
1851
        if (sk != NULL) {
1852
                if (!sk->dead)
1853
                        sk->data_ready(sk, skb->len);
1854
        } else {
1855
                kfree_skb(skb, FREE_READ);
1856
        }
1857
 
1858
        return 0;
1859
}
1860
 
1861
/*
1862
 *      Receive an AX.25 frame via a SLIP interface.
1863
 */
1864
static int kiss_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *ptype)
1865
{
1866
        skb->sk = NULL;         /* Initially we don't know who it's for */
1867
 
1868
        if ((*skb->data & 0x0F) != 0) {
1869
                kfree_skb(skb, FREE_READ);      /* Not a KISS data frame */
1870
                return 0;
1871
        }
1872
 
1873
        skb_pull(skb, AX25_KISS_HEADER_LEN);    /* Remove the KISS byte */
1874
 
1875
        return ax25_rcv(skb, dev, (ax25_address *)dev->dev_addr, ptype);
1876
}
1877
 
1878
 
1879
static int ax25_sendmsg(struct socket *sock, struct msghdr *msg, int len, int noblock, int flags)
1880
{
1881
        struct sock *sk = (struct sock *)sock->data;
1882
        struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name;
1883
        int err;
1884
        struct sockaddr_ax25 sax;
1885
        struct sk_buff *skb;
1886
        unsigned char *asmptr;
1887
        int size;
1888
        ax25_digi *dp;
1889
        ax25_digi dtmp;
1890
        int lv;
1891
        int addr_len = msg->msg_namelen;
1892
 
1893
        if (sk->err)
1894
                return sock_error(sk);
1895
 
1896
        if (flags || msg->msg_control)
1897
                return -EINVAL;
1898
 
1899
        if (sk->zapped)
1900
                return -EADDRNOTAVAIL;
1901
 
1902
        if (sk->shutdown & SEND_SHUTDOWN) {
1903
                send_sig(SIGPIPE, current, 0);
1904
                return -EPIPE;
1905
        }
1906
 
1907
        if (sk->protinfo.ax25->device == NULL)
1908
                return -ENETUNREACH;
1909
 
1910
        if (usax != NULL) {
1911
                if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25))
1912
                        return -EINVAL;
1913
                if (usax->sax25_family != AF_AX25)
1914
                        return -EINVAL;
1915
                if (addr_len == sizeof(struct full_sockaddr_ax25) && usax->sax25_ndigis != 0) {
1916
                        int ct           = 0;
1917
                        struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)usax;
1918
 
1919
                        /* Valid number of digipeaters ? */
1920
                        if (usax->sax25_ndigis < 1 || usax->sax25_ndigis > AX25_MAX_DIGIS)
1921
                                return -EINVAL;
1922
 
1923
                        dtmp.ndigi      = usax->sax25_ndigis;
1924
 
1925
                        while (ct < usax->sax25_ndigis) {
1926
                                dtmp.repeated[ct] = 0;
1927
                                dtmp.calls[ct]    = fsa->fsa_digipeater[ct];
1928
                                ct++;
1929
                        }
1930
 
1931
                        dtmp.lastrepeat = 0;
1932
                }
1933
 
1934
                sax = *usax;
1935
                if (sk->type == SOCK_SEQPACKET && ax25cmp(&sk->protinfo.ax25->dest_addr, &sax.sax25_call) != 0)
1936
                        return -EISCONN;
1937
                if (usax->sax25_ndigis == 0)
1938
                        dp = NULL;
1939
                else
1940
                        dp = &dtmp;
1941
        } else {
1942
                if (sk->state != TCP_ESTABLISHED)
1943
                        return -ENOTCONN;
1944
                sax.sax25_family = AF_AX25;
1945
                sax.sax25_call   = sk->protinfo.ax25->dest_addr;
1946
                dp = sk->protinfo.ax25->digipeat;
1947
        }
1948
 
1949
        if (sk->debug)
1950
                printk("AX.25: sendto: Addresses built.\n");
1951
 
1952
        /* Build a packet */
1953
        if (sk->debug)
1954
                printk("AX.25: sendto: building packet.\n");
1955
 
1956
        /* Assume the worst case */
1957
        size = len + 3 + size_ax25_addr(dp) + AX25_BPQ_HEADER_LEN;
1958
 
1959
        if ((skb = sock_alloc_send_skb(sk, size, 0, 0, &err)) == NULL)
1960
                return err;
1961
 
1962
        skb->sk   = sk;
1963
        skb->free = 1;
1964
 
1965
        skb_reserve(skb, size - len);
1966
 
1967
        if (sk->debug)
1968
                printk("AX.25: Appending user data\n");
1969
 
1970
        /* User data follows immediately after the AX.25 data */
1971
        memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1972
 
1973
        /* Add the PID if one is not supplied by the user in the skb */
1974
        if (!sk->protinfo.ax25->pidincl) {
1975
                asmptr  = skb_push(skb, 1);
1976
                *asmptr = sk->protocol;
1977
        }
1978
 
1979
        if (sk->debug)
1980
                printk("AX.25: Transmitting buffer\n");
1981
 
1982
        if (sk->type == SOCK_SEQPACKET) {
1983
                /* Connected mode sockets go via the LAPB machine */
1984
                if (sk->state != TCP_ESTABLISHED) {
1985
                        kfree_skb(skb, FREE_WRITE);
1986
                        return -ENOTCONN;
1987
                }
1988
 
1989
                ax25_output(sk->protinfo.ax25, sk->protinfo.ax25->paclen, skb); /* Shove it onto the queue and kick */
1990
 
1991
                return len;
1992
        } else {
1993
                asmptr = skb_push(skb, 1 + size_ax25_addr(dp));
1994
 
1995
                if (sk->debug) {
1996
                        printk("Building AX.25 Header (dp=%p).\n", dp);
1997
                        if (dp != 0)
1998
                                printk("Num digipeaters=%d\n", dp->ndigi);
1999
                }
2000
 
2001
                /* Build an AX.25 header */
2002
                asmptr += (lv = build_ax25_addr(asmptr, &sk->protinfo.ax25->source_addr, &sax.sax25_call, dp, AX25_COMMAND, AX25_MODULUS));
2003
 
2004
                if (sk->debug)
2005
                        printk("Built header (%d bytes)\n",lv);
2006
 
2007
                skb->h.raw = asmptr;
2008
 
2009
                if (sk->debug)
2010
                        printk("base=%p pos=%p\n", skb->data, asmptr);
2011
 
2012
                *asmptr = AX25_UI;
2013
 
2014
                /* Datagram frames go straight out of the door as UI */
2015
                ax25_queue_xmit(skb, sk->protinfo.ax25->device, SOPRI_NORMAL);
2016
 
2017
                return len;
2018
        }
2019
}
2020
 
2021
static int ax25_recvmsg(struct socket *sock, struct msghdr *msg, int size, int noblock, int flags, int *addr_len)
2022
{
2023
        struct sock *sk = (struct sock *)sock->data;
2024
        struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name;
2025
        int copied;
2026
        struct sk_buff *skb;
2027
        int er;
2028
 
2029
        if (sk->err)
2030
                return sock_error(sk);
2031
 
2032
        if (addr_len != NULL)
2033
                *addr_len = sizeof(*sax);
2034
 
2035
        /*
2036
         *      This works for seqpacket too. The receiver has ordered the
2037
         *      queue for us! We do one quick check first though
2038
         */
2039
        if (sk->type == SOCK_SEQPACKET && sk->state != TCP_ESTABLISHED)
2040
                return -ENOTCONN;
2041
 
2042
        /* Now we can treat all alike */
2043
        if ((skb = skb_recv_datagram(sk, flags, noblock, &er)) == NULL)
2044
                return er;
2045
 
2046
        if (!sk->protinfo.ax25->pidincl)
2047
                skb_pull(skb, 1);               /* Remove PID */
2048
 
2049
        skb->h.raw = skb->data;
2050
 
2051
        copied = (size < skb->len) ? size : skb->len;
2052
        skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2053
 
2054
        if (sax != NULL) {
2055
                ax25_digi digi;
2056
                ax25_address dest;
2057
                int dama;
2058
 
2059
                if (addr_len == NULL)
2060
                        return -EINVAL;
2061
                if (*addr_len != sizeof(struct sockaddr_ax25) && *addr_len != sizeof(struct full_sockaddr_ax25))
2062
                        return -EINVAL;
2063
 
2064
                ax25_parse_addr(skb->data, skb->len, NULL, &dest, &digi, NULL, &dama);
2065
 
2066
                sax->sax25_family = AF_AX25;
2067
                /* We set this correctly, even though we may not let the
2068
                   application know the digi calls further down (because it
2069
                   did NOT ask to know them).  This could get political... **/
2070
                sax->sax25_ndigis = digi.ndigi;
2071
                sax->sax25_call   = dest;
2072
 
2073
                *addr_len = sizeof(struct sockaddr_ax25);
2074
 
2075
                if (*addr_len == sizeof(struct full_sockaddr_ax25) && sax->sax25_ndigis != 0) {
2076
                        int ct           = 0;
2077
                        struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)sax;
2078
 
2079
                        while (ct < digi.ndigi) {
2080
                                fsa->fsa_digipeater[ct] = digi.calls[ct];
2081
                                ct++;
2082
                        }
2083
 
2084
                        *addr_len = sizeof(struct full_sockaddr_ax25);
2085
                }
2086
        }
2087
 
2088
        skb_free_datagram(sk, skb);
2089
 
2090
        return copied;
2091
}
2092
 
2093
static int ax25_shutdown(struct socket *sk, int how)
2094
{
2095
        /* FIXME - generate DM and RNR states */
2096
        return -EOPNOTSUPP;
2097
}
2098
 
2099
static int ax25_select(struct socket *sock , int sel_type, select_table *wait)
2100
{
2101
        struct sock *sk = (struct sock *)sock->data;
2102
 
2103
        return datagram_select(sk, sel_type, wait);
2104
}
2105
 
2106
static int ax25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
2107
{
2108
        struct sock *sk = (struct sock *)sock->data;
2109
        int err;
2110
 
2111
        switch (cmd) {
2112
                case TIOCOUTQ: {
2113
                        long amount;
2114
                        if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(int))) != 0)
2115
                                return err;
2116
                        amount = sk->sndbuf - sk->wmem_alloc;
2117
                        if (amount < 0)
2118
                                amount = 0;
2119
                        put_fs_long(amount, (int *)arg);
2120
                        return 0;
2121
                }
2122
 
2123
                case TIOCINQ: {
2124
                        struct sk_buff *skb;
2125
                        long amount = 0L;
2126
                        /* These two are safe on a single CPU system as only user tasks fiddle here */
2127
                        if ((skb = skb_peek(&sk->receive_queue)) != NULL)
2128
                                amount = skb->len;
2129
                        if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(int))) != 0)
2130
                                return err;
2131
                        put_fs_long(amount, (int *)arg);
2132
                        return 0;
2133
                }
2134
 
2135
                case SIOCGSTAMP:
2136
                        if (sk != NULL) {
2137
                                if (sk->stamp.tv_sec == 0)
2138
                                        return -ENOENT;
2139
                                if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct timeval))) != 0)
2140
                                        return err;
2141
                                memcpy_tofs((void *)arg, &sk->stamp, sizeof(struct timeval));
2142
                                return 0;
2143
                        }
2144
                        return -EINVAL;
2145
 
2146
                case SIOCAX25ADDUID:    /* Add a uid to the uid/call map table */
2147
                case SIOCAX25DELUID:    /* Delete a uid from the uid/call map table */
2148
                case SIOCAX25GETUID: {
2149
                        struct sockaddr_ax25 sax25;
2150
                        if ((err = verify_area(VERIFY_READ, (void *)arg, sizeof(struct sockaddr_ax25))) != 0)
2151
                                return err;
2152
                        memcpy_fromfs(&sax25, (void *)arg, sizeof(sax25));
2153
                        return ax25_uid_ioctl(cmd, &sax25);
2154
                }
2155
 
2156
                case SIOCAX25NOUID: {   /* Set the default policy (default/bar) */
2157
                        long amount;
2158
                        if ((err = verify_area(VERIFY_READ, (void *)arg, sizeof(unsigned long))) != 0)
2159
                                return err;
2160
                        if (!suser())
2161
                                return -EPERM;
2162
                        amount = get_fs_long((void *)arg);
2163
                        if (amount > AX25_NOUID_BLOCK)
2164
                                return -EINVAL;
2165
                        ax25_uid_policy = amount;
2166
                        return 0;
2167
                }
2168
 
2169
                case SIOCADDRT:
2170
                case SIOCDELRT:
2171
                case SIOCAX25OPTRT:
2172
                        if (!suser())
2173
                                return -EPERM;
2174
                        return ax25_rt_ioctl(cmd, (void *)arg);
2175
 
2176
                case SIOCAX25CTLCON:
2177
                        if (!suser())
2178
                                return -EPERM;
2179
                        return ax25_ctl_ioctl(cmd, (void *)arg);
2180
 
2181
                case SIOCAX25GETINFO: {
2182
                        struct ax25_info_struct ax25_info;
2183
                        if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(ax25_info))) != 0)
2184
                                return err;
2185
                        ax25_info.t1        = sk->protinfo.ax25->t1   / AX25_SLOWHZ;
2186
                        ax25_info.t2        = sk->protinfo.ax25->t2   / AX25_SLOWHZ;
2187
                        ax25_info.t3        = sk->protinfo.ax25->t3   / AX25_SLOWHZ;
2188
                        ax25_info.idle      = sk->protinfo.ax25->idle / (60 * AX25_SLOWHZ);
2189
                        ax25_info.n2        = sk->protinfo.ax25->n2;
2190
                        ax25_info.t1timer   = sk->protinfo.ax25->t1timer   / AX25_SLOWHZ;
2191
                        ax25_info.t2timer   = sk->protinfo.ax25->t2timer   / AX25_SLOWHZ;
2192
                        ax25_info.t3timer   = sk->protinfo.ax25->t3timer   / AX25_SLOWHZ;
2193
                        ax25_info.idletimer = sk->protinfo.ax25->idletimer / (60 * AX25_SLOWHZ);
2194
                        ax25_info.n2count   = sk->protinfo.ax25->n2count;
2195
                        ax25_info.state     = sk->protinfo.ax25->state;
2196
                        ax25_info.rcv_q     = sk->rmem_alloc;
2197
                        ax25_info.snd_q     = sk->wmem_alloc;
2198
                        memcpy_tofs((void *)arg, &ax25_info, sizeof(ax25_info));
2199
                        return 0;
2200
                }
2201
 
2202
                case SIOCAX25ADDFWD:
2203
                case SIOCAX25DELFWD: {
2204
                        struct ax25_fwd_struct ax25_fwd;
2205
                        if (!suser())
2206
                                return -EPERM;
2207
                        if ((err = verify_area(VERIFY_READ, (void *)arg, sizeof(ax25_fwd))) != 0)
2208
                                return err;
2209
                        memcpy_fromfs(&ax25_fwd, (void *)arg, sizeof(ax25_fwd));
2210
                        return ax25_fwd_ioctl(cmd, &ax25_fwd);
2211
                }
2212
 
2213
                case SIOCGIFADDR:
2214
                case SIOCSIFADDR:
2215
                case SIOCGIFDSTADDR:
2216
                case SIOCSIFDSTADDR:
2217
                case SIOCGIFBRDADDR:
2218
                case SIOCSIFBRDADDR:
2219
                case SIOCGIFNETMASK:
2220
                case SIOCSIFNETMASK:
2221
                case SIOCGIFMETRIC:
2222
                case SIOCSIFMETRIC:
2223
                        return -EINVAL;
2224
 
2225
                default:
2226
                        return dev_ioctl(cmd, (void *)arg);
2227
        }
2228
 
2229
        /*NOTREACHED*/
2230
        return 0;
2231
}
2232
 
2233
 
2234
static int ax25_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
2235
{
2236
        ax25_cb *ax25;
2237
        struct device *dev;
2238
        const char *devname;
2239
        char callbuf[15];
2240
        int len = 0;
2241
        off_t pos = 0;
2242
        off_t begin = 0;
2243
 
2244
        cli();
2245
 
2246
        len += sprintf(buffer, "dest_addr src_addr   dev  st  vs  vr  va    t1     t2     t3      idle   n2  rtt wnd paclen Snd-Q Rcv-Q Inode\n");
2247
 
2248
        for (ax25 = ax25_list; ax25 != NULL; ax25 = ax25->next) {
2249
                if ((dev = ax25->device) == NULL)
2250
                        devname = "???";
2251
                else
2252
                        devname = dev->name;
2253
 
2254
                len += sprintf(buffer + len, "%-9s ",
2255
                        ax2asc(&ax25->dest_addr));
2256
 
2257
                sprintf(callbuf, "%s%c", ax2asc(&ax25->source_addr),
2258
                                         (ax25->iamdigi) ? '*' : ' ');
2259
 
2260
                len += sprintf(buffer + len, "%-10s %-4s %2d %3d %3d %3d %3d/%03d %2d/%02d %3d/%03d %3d/%03d %2d/%02d %3d %3d  %5d",
2261
                        callbuf, devname,
2262
                        ax25->state,
2263
                        ax25->vs, ax25->vr, ax25->va,
2264
                        ax25->t1timer / AX25_SLOWHZ,
2265
                        ax25->t1      / AX25_SLOWHZ,
2266
                        ax25->t2timer / AX25_SLOWHZ,
2267
                        ax25->t2      / AX25_SLOWHZ,
2268
                        ax25->t3timer / AX25_SLOWHZ,
2269
                        ax25->t3      / AX25_SLOWHZ,
2270
                        ax25->idletimer / (AX25_SLOWHZ * 60),
2271
                        ax25->idle      / (AX25_SLOWHZ * 60),
2272
                        ax25->n2count, ax25->n2,
2273
                        ax25->rtt     / AX25_SLOWHZ,
2274
                        ax25->window,
2275
                        ax25->paclen);
2276
 
2277
                if (ax25->sk != NULL) {
2278
                        struct sock *s = ax25->sk;
2279
 
2280
                        len += sprintf(buffer + len, " %5d %5d %ld\n",
2281
                                s->wmem_alloc,
2282
                                s->rmem_alloc,
2283
                                s->socket && SOCK_INODE(s->socket) ?
2284
                                SOCK_INODE(s->socket)->i_ino : 0);
2285
                } else {
2286
                        len += sprintf(buffer + len, "\n");
2287
                }
2288
 
2289
                pos = begin + len;
2290
 
2291
                if (pos < offset) {
2292
                        len   = 0;
2293
                        begin = pos;
2294
                }
2295
 
2296
                if (pos > offset + length)
2297
                        break;
2298
        }
2299
 
2300
        sti();
2301
 
2302
        *start = buffer + (offset - begin);
2303
        len   -= (offset - begin);
2304
 
2305
        if (len > length) len = length;
2306
 
2307
        return(len);
2308
}
2309
 
2310
static struct proto_ops ax25_proto_ops = {
2311
        AF_AX25,
2312
 
2313
        ax25_create,
2314
        ax25_dup,
2315
        ax25_release,
2316
        ax25_bind,
2317
        ax25_connect,
2318
        ax25_socketpair,
2319
        ax25_accept,
2320
        ax25_getname,
2321
        ax25_select,
2322
        ax25_ioctl,
2323
        ax25_listen,
2324
        ax25_shutdown,
2325
        ax25_setsockopt,
2326
        ax25_getsockopt,
2327
        ax25_fcntl,
2328
        ax25_sendmsg,
2329
        ax25_recvmsg
2330
};
2331
 
2332
/*
2333
 *      Called by socket.c on kernel start up
2334
 */
2335
static struct packet_type ax25_packet_type =
2336
{
2337
        0,       /* MUTTER ntohs(ETH_P_AX25),*/
2338
        0,               /* copy */
2339
        kiss_rcv,
2340
        NULL,
2341
        NULL,
2342
};
2343
 
2344
static struct notifier_block ax25_dev_notifier = {
2345
        ax25_device_event,
2346
 
2347
};
2348
 
2349
#ifdef CONFIG_PROC_FS
2350
static struct proc_dir_entry proc_ax25_route = {
2351
        PROC_NET_AX25_ROUTE, 10, "ax25_route",
2352
        S_IFREG | S_IRUGO, 1, 0, 0,
2353
        0, &proc_net_inode_operations,
2354
        ax25_rt_get_info
2355
};
2356
static struct proc_dir_entry proc_ax25 = {
2357
        PROC_NET_AX25, 4, "ax25",
2358
        S_IFREG | S_IRUGO, 1, 0, 0,
2359
        0, &proc_net_inode_operations,
2360
        ax25_get_info
2361
};
2362
static struct proc_dir_entry proc_ax25_calls = {
2363
        PROC_NET_AX25_CALLS, 10, "ax25_calls",
2364
        S_IFREG | S_IRUGO, 1, 0, 0,
2365
        0, &proc_net_inode_operations,
2366
        ax25_cs_get_info
2367
};
2368
#endif
2369
 
2370
static struct symbol_table ax25_syms = {
2371
#include <linux/symtab_begin.h>
2372
        X(ax25_encapsulate),
2373
        X(ax25_rebuild_header),
2374
        X(ax25_findbyuid),
2375
        X(ax25_find_cb),
2376
        X(ax25_linkfail_register),
2377
        X(ax25_linkfail_release),
2378
        X(ax25_listen_register),
2379
        X(ax25_listen_release),
2380
        X(ax25_protocol_register),
2381
        X(ax25_protocol_release),
2382
        X(ax25_send_frame),
2383
        X(ax25_uid_policy),
2384
        X(ax25cmp),
2385
        X(ax2asc),
2386
        X(asc2ax),
2387
        X(null_ax25_address),
2388
#include <linux/symtab_end.h>
2389
};
2390
 
2391
void ax25_proto_init(struct net_proto *pro)
2392
{
2393
        sock_register(ax25_proto_ops.family, &ax25_proto_ops);
2394
        ax25_packet_type.type = htons(ETH_P_AX25);
2395
        dev_add_pack(&ax25_packet_type);
2396
        register_netdevice_notifier(&ax25_dev_notifier);
2397
        ax25_register_sysctl();
2398
        register_symtab(&ax25_syms);
2399
 
2400
#ifdef CONFIG_PROC_FS
2401
        proc_net_register(&proc_ax25_route);
2402
        proc_net_register(&proc_ax25);
2403
        proc_net_register(&proc_ax25_calls);
2404
#endif
2405
 
2406
        printk(KERN_INFO "G4KLX/GW4PTS AX.25 for Linux. Version 0.35 for Linux NET3.035 (Linux 2.0)\n");
2407
}
2408
 
2409
/*
2410
 *      A small shim to dev_queue_xmit to add the KISS control byte, and do
2411
 *      any packet forwarding in operation.
2412
 */
2413
void ax25_queue_xmit(struct sk_buff *skb, struct device *dev, int pri)
2414
{
2415
        unsigned char *ptr;
2416
 
2417
#ifdef CONFIG_FIREWALL
2418
        if (call_out_firewall(PF_AX25, skb->dev, skb->data, NULL) != FW_ACCEPT) {
2419
                dev_kfree_skb(skb, FREE_WRITE);
2420
                return;
2421
        }
2422
#endif
2423
 
2424
        skb->protocol = htons(ETH_P_AX25);
2425
        skb->dev      = ax25_fwd_dev(dev);
2426
        skb->arp      = 1;
2427
 
2428
        ptr  = skb_push(skb, 1);
2429
        *ptr = 0x00;                    /* KISS */
2430
 
2431
        dev_queue_xmit(skb, skb->dev, pri);
2432
}
2433
 
2434
/*
2435
 *      IP over AX.25 encapsulation.
2436
 */
2437
 
2438
/*
2439
 *      Shove an AX.25 UI header on an IP packet and handle ARP
2440
 */
2441
 
2442
#ifdef CONFIG_INET
2443
 
2444
int ax25_encapsulate(struct sk_buff *skb, struct device *dev, unsigned short type, void *daddr,
2445
                void *saddr, unsigned len)
2446
{
2447
        /* header is an AX.25 UI frame from us to them */
2448
        unsigned char *buff = skb_push(skb, AX25_HEADER_LEN);
2449
 
2450
        *buff++ = 0;     /* KISS DATA */
2451
 
2452
        if (daddr != NULL)
2453
                memcpy(buff, daddr, dev->addr_len);     /* Address specified */
2454
 
2455
        buff[6] &= ~AX25_CBIT;
2456
        buff[6] &= ~AX25_EBIT;
2457
        buff[6] |= AX25_SSSID_SPARE;
2458
        buff += AX25_ADDR_LEN;
2459
 
2460
        if (saddr != NULL)
2461
                memcpy(buff, saddr, dev->addr_len);
2462
        else
2463
                memcpy(buff, dev->dev_addr, dev->addr_len);
2464
 
2465
        buff[6] &= ~AX25_CBIT;
2466
        buff[6] |= AX25_EBIT;
2467
        buff[6] |= AX25_SSSID_SPARE;
2468
        buff   += AX25_ADDR_LEN;
2469
 
2470
        *buff++ = AX25_UI;      /* UI */
2471
 
2472
        /* Append a suitable AX.25 PID */
2473
        switch (type) {
2474
                case ETH_P_IP:
2475
                        *buff++ = AX25_P_IP;
2476
                        break;
2477
                case ETH_P_ARP:
2478
                        *buff++ = AX25_P_ARP;
2479
                        break;
2480
                default:
2481
                        printk(KERN_ERR "AX.25 wrong protocol type 0x%x2.2\n", type);
2482
                        *buff++ = 0;
2483
                        break;
2484
        }
2485
 
2486
        if (daddr != NULL)
2487
                return AX25_HEADER_LEN;
2488
 
2489
        return -AX25_HEADER_LEN;        /* Unfinished header */
2490
}
2491
 
2492
int ax25_rebuild_header(void *buf, struct device *dev, unsigned long dest, struct sk_buff *skb)
2493
{
2494
        struct sk_buff *ourskb;
2495
        unsigned char *bp = (unsigned char *)buf;
2496
        ax25_address *src, *dst;
2497
        ax25_digi *digi;
2498
        int mode;
2499
 
2500
        dst = (ax25_address *)(bp + 1);
2501
        src = (ax25_address *)(bp + 8);
2502
 
2503
        if (arp_find(bp + 1, dest, dev, dev->pa_addr, skb))
2504
                return 1;
2505
 
2506
        digi = ax25_rt_find_path(dst, dev);
2507
 
2508
        if (bp[16] == AX25_P_IP) {
2509
                mode = ax25_rt_mode_get(dst, dev);
2510
 
2511
                if (mode == 'V' || (mode == ' ' && ax25_dev_get_value(dev, AX25_VALUES_IPDEFMODE))) {
2512
                        /*
2513
                         *      We clone the buffer and release the original thereby
2514
                         *      keeping it straight
2515
                         *
2516
                         *      Note: we report 1 back so the caller will
2517
                         *      not feed the frame direct to the physical device
2518
                         *      We don't want that to happen. (It won't be upset
2519
                         *      as we have pulled the frame from the queue by
2520
                         *      freeing it).
2521
                         */
2522
                        if ((ourskb = skb_copy(skb, GFP_ATOMIC)) == NULL) {
2523
                                dev_kfree_skb(skb, FREE_WRITE);
2524
                                return 1;
2525
                        }
2526
 
2527
                        ourskb->sk = skb->sk;
2528
 
2529
                        if (ourskb->sk != NULL)
2530
                                atomic_add(ourskb->truesize, &ourskb->sk->wmem_alloc);
2531
 
2532
                        dev_kfree_skb(skb, FREE_WRITE);
2533
 
2534
                        skb_pull(ourskb, AX25_HEADER_LEN - 1);  /* Keep PID */
2535
 
2536
                        ax25_send_frame(ourskb, ax25_dev_get_value(dev, AX25_VALUES_PACLEN), src, dst, digi, dev);
2537
 
2538
                        return 1;
2539
                }
2540
        }
2541
 
2542
        bp[7]  &= ~AX25_CBIT;
2543
        bp[7]  &= ~AX25_EBIT;
2544
        bp[7]  |= AX25_SSSID_SPARE;
2545
 
2546
        bp[14] &= ~AX25_CBIT;
2547
        bp[14] |= AX25_EBIT;
2548
        bp[14] |= AX25_SSSID_SPARE;
2549
 
2550
        skb_pull(skb, AX25_KISS_HEADER_LEN);
2551
 
2552
        if (digi != NULL)
2553
                ax25_rt_build_path(skb, src, dst, digi);
2554
 
2555
        ax25_queue_xmit(skb, dev, SOPRI_NORMAL);
2556
 
2557
        return 1;
2558
}
2559
 
2560
#endif
2561
 
2562
#ifdef MODULE
2563
int init_module(void)
2564
{
2565
        ax25_proto_init(NULL);
2566
 
2567
        return 0;
2568
}
2569
 
2570
void cleanup_module(void)
2571
{
2572
#ifdef CONFIG_PROC_FS
2573
        proc_net_unregister(PROC_NET_AX25_ROUTE);
2574
        proc_net_unregister(PROC_NET_AX25);
2575
        proc_net_unregister(PROC_NET_AX25_CALLS);
2576
        proc_net_unregister(PROC_NET_AX25_ROUTE);
2577
#endif
2578
        ax25_rt_free();
2579
 
2580
        ax25_unregister_sysctl();
2581
 
2582
        unregister_netdevice_notifier(&ax25_dev_notifier);
2583
 
2584
        ax25_packet_type.type = htons(ETH_P_AX25);
2585
        dev_remove_pack(&ax25_packet_type);
2586
 
2587
        sock_unregister(AF_AX25);
2588
}
2589
#endif
2590
 
2591
#endif

powered by: WebSVN 2.1.0

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