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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [net/] [ipx/] [af_ipx.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1629 jcastillo
/*
2
 *      Implements an IPX socket layer (badly - but I'm working on it).
3
 *
4
 *      This code is derived from work by
5
 *              Ross Biro       :       Writing the original IP stack
6
 *              Fred Van Kempen :       Tidying up the TCP/IP
7
 *
8
 *      Many thanks go to Keith Baker, Institute For Industrial Information
9
 *      Technology Ltd, Swansea University for allowing me to work on this
10
 *      in my own time even though it was in some ways related to commercial
11
 *      work I am currently employed to do there.
12
 *
13
 *      All the material in this file is subject to the Gnu license version 2.
14
 *      Neither Alan Cox nor the Swansea University Computer Society admit liability
15
 *      nor provide warranty for any of this software. This material is provided
16
 *      as is and at no charge.
17
 *
18
 *      Revision 0.21:  Uses the new generic socket option code.
19
 *      Revision 0.22:  Gcc clean ups and drop out device registration. Use the
20
 *                      new multi-protocol edition of hard_header
21
 *      Revision 0.23:  IPX /proc by Mark Evans.
22
 *                      Adding a route will overwrite any existing route to the same
23
 *                      network.
24
 *      Revision 0.24:  Supports new /proc with no 4K limit
25
 *      Revision 0.25:  Add ephemeral sockets, passive local network
26
 *                      identification, support for local net 0 and
27
 *                      multiple datalinks <Greg Page>
28
 *      Revision 0.26:  Device drop kills IPX routes via it. (needed for modules)
29
 *      Revision 0.27:  Autobind <Mark Evans>
30
 *      Revision 0.28:  Small fix for multiple local networks <Thomas Winder>
31
 *      Revision 0.29:  Assorted major errors removed <Mark Evans>
32
 *                      Small correction to promisc mode error fix <Alan Cox>
33
 *                      Asynchronous I/O support.
34
 *                      Changed to use notifiers and the newer packet_type stuff.
35
 *                      Assorted major fixes <Alejandro Liu>
36
 *      Revision 0.30:  Moved to net/ipx/...    <Alan Cox>
37
 *                      Don't set address length on recvfrom that errors.
38
 *                      Incorrect verify_area.
39
 *      Revision 0.31:  New sk_buffs. This still needs a lot of testing. <Alan Cox>
40
 *      Revision 0.32:  Using sock_alloc_send_skb, firewall hooks. <Alan Cox>
41
 *                      Supports sendmsg/recvmsg
42
 *      Revision 0.33:  Internal network support, routing changes, uses a
43
 *                      protocol private area for ipx data.
44
 *      Revision 0.34:  Module support. <Jim Freeman>
45
 *      Revision 0.35:  Checksum support. <Neil Turton>, hooked in by <Alan Cox>
46
 *                      Handles WIN95 discovery packets <Volker Lendecke>
47
 *
48
 *      Protect the module by a MOD_INC_USE_COUNT/MOD_DEC_USE_COUNT
49
 *      pair. Also, now usage count is managed this way
50
 *      -Count one if the auto_interface mode is on
51
 *      -Count one per configured interface
52
 *
53
 *      Jacques Gelinas (jacques@solucorp.qc.ca)
54
 *
55
 *
56
 *      Portions Copyright (c) 1995 Caldera, Inc. <greg@caldera.com>
57
 *      Neither Greg Page nor Caldera, Inc. admit liability nor provide
58
 *      warranty for any of this software. This material is provided
59
 *      "AS-IS" and at no charge.
60
 */
61
 
62
#include <linux/module.h>
63
 
64
#include <linux/config.h>
65
#include <linux/errno.h>
66
#include <linux/types.h>
67
#include <linux/socket.h>
68
#include <linux/in.h>
69
#include <linux/kernel.h>
70
#include <linux/sched.h>
71
#include <linux/timer.h>
72
#include <linux/string.h>
73
#include <linux/sockios.h>
74
#include <linux/net.h>
75
#include <linux/netdevice.h>
76
#include <net/ipx.h>
77
#include <linux/inet.h>
78
#include <linux/route.h>
79
#include <net/sock.h>
80
#include <asm/segment.h>
81
#include <asm/system.h>
82
#include <linux/fcntl.h>
83
#include <linux/mm.h>
84
#include <linux/termios.h>      /* For TIOCOUTQ/INQ */
85
#include <linux/interrupt.h>
86
#include <net/p8022.h>
87
#include <net/p8022tr.h>
88
#include <net/psnap.h>
89
#include <linux/proc_fs.h>
90
#include <linux/stat.h>
91
#include <linux/firewall.h>
92
 
93
#ifdef MODULE
94
static void ipx_proto_finito(void);
95
#endif /* def MODULE */
96
 
97
/* Configuration Variables */
98
static unsigned char    ipxcfg_max_hops = 16;
99
static char             ipxcfg_auto_select_primary = 0;
100
static char             ipxcfg_auto_create_interfaces = 0;
101
 
102
/* Global Variables */
103
static struct datalink_proto    *p8022_datalink = NULL;
104
static struct datalink_proto    *p8022tr_datalink = NULL;
105
static struct datalink_proto    *pEII_datalink = NULL;
106
static struct datalink_proto    *p8023_datalink = NULL;
107
static struct datalink_proto    *pSNAP_datalink = NULL;
108
 
109
static ipx_route        *ipx_routes = NULL;
110
static ipx_interface    *ipx_interfaces = NULL;
111
static ipx_interface    *ipx_primary_net = NULL;
112
static ipx_interface    *ipx_internal_net = NULL;
113
 
114
static int
115
ipxcfg_set_auto_create(char val)
116
{
117
        if (ipxcfg_auto_create_interfaces != val){
118
                if (val){
119
                        MOD_INC_USE_COUNT;
120
                }else{
121
                        MOD_DEC_USE_COUNT;
122
                }
123
                ipxcfg_auto_create_interfaces = val;
124
        }
125
        return 0;
126
}
127
 
128
static int
129
ipxcfg_set_auto_select(char val)
130
{
131
        ipxcfg_auto_select_primary = val;
132
        if (val && (ipx_primary_net == NULL))
133
                ipx_primary_net = ipx_interfaces;
134
        return 0;
135
}
136
 
137
static int
138
ipxcfg_get_config_data(ipx_config_data *arg)
139
{
140
        ipx_config_data vals;
141
 
142
        vals.ipxcfg_auto_create_interfaces = ipxcfg_auto_create_interfaces;
143
        vals.ipxcfg_auto_select_primary = ipxcfg_auto_select_primary;
144
        memcpy_tofs(arg, &vals, sizeof(vals));
145
        return 0;
146
}
147
 
148
 
149
/***********************************************************************************************************************\
150
*                                                                                                                       *
151
*                                               Handlers for the socket list.                                           *
152
*                                                                                                                       *
153
\***********************************************************************************************************************/
154
 
155
/*
156
 *      Note: Sockets may not be removed _during_ an interrupt or inet_bh
157
 *      handler using this technique. They can be added although we do not
158
 *      use this facility.
159
 */
160
 
161
static void
162
ipx_remove_socket(ipx_socket *sk)
163
{
164
        ipx_socket      *s;
165
        ipx_interface   *intrfc;
166
        unsigned long   flags;
167
 
168
        save_flags(flags);
169
        cli();
170
 
171
        /* Determine interface with which socket is associated */
172
        intrfc = sk->protinfo.af_ipx.intrfc;
173
        if (intrfc == NULL) {
174
                restore_flags(flags);
175
                return;
176
        }
177
 
178
        s=intrfc->if_sklist;
179
        if(s==sk) {
180
                intrfc->if_sklist=s->next;
181
                restore_flags(flags);
182
                return;
183
        }
184
 
185
        while(s && s->next) {
186
                if(s->next==sk) {
187
                        s->next=sk->next;
188
                        restore_flags(flags);
189
                        return;
190
                }
191
                s=s->next;
192
        }
193
        restore_flags(flags);
194
}
195
 
196
/*
197
 *      This is only called from user mode. Thus it protects itself against
198
 *      interrupt users but doesn't worry about being called during work.
199
 *      Once it is removed from the queue no interrupt or bottom half will
200
 *      touch it and we are (fairly 8-) ) safe.
201
 */
202
 
203
static void
204
ipx_destroy_socket(ipx_socket *sk)
205
{
206
        struct sk_buff  *skb;
207
 
208
        ipx_remove_socket(sk);
209
        while((skb=skb_dequeue(&sk->receive_queue))!=NULL) {
210
                kfree_skb(skb,FREE_READ);
211
        }
212
 
213
        sk_free(sk);
214
        MOD_DEC_USE_COUNT;
215
}
216
 
217
/* The following code is used to support IPX Interfaces (IPXITF).  An
218
 * IPX interface is defined by a physical device and a frame type.
219
 */
220
 
221
static ipx_route * ipxrtr_lookup(unsigned long);
222
 
223
static void
224
ipxitf_clear_primary_net(void)
225
{
226
        if (ipxcfg_auto_select_primary && (ipx_interfaces != NULL))
227
                ipx_primary_net = ipx_interfaces;
228
        else
229
                ipx_primary_net = NULL;
230
}
231
 
232
static ipx_interface *
233
ipxitf_find_using_phys(struct device *dev, unsigned short datalink)
234
{
235
        ipx_interface   *i;
236
 
237
        for (i=ipx_interfaces;
238
                i && ((i->if_dev!=dev) || (i->if_dlink_type!=datalink));
239
                i=i->if_next)
240
                ;
241
        return i;
242
}
243
 
244
static ipx_interface *
245
ipxitf_find_using_net(unsigned long net)
246
{
247
        ipx_interface   *i;
248
 
249
        if (net == 0L)
250
                return ipx_primary_net;
251
 
252
        for (i=ipx_interfaces; i && (i->if_netnum!=net); i=i->if_next)
253
                ;
254
 
255
        return i;
256
}
257
 
258
/* Sockets are bound to a particular IPX interface. */
259
static void
260
ipxitf_insert_socket(ipx_interface *intrfc, ipx_socket *sk)
261
{
262
        ipx_socket      *s;
263
 
264
        sk->protinfo.af_ipx.intrfc = intrfc;
265
        sk->next = NULL;
266
        if (intrfc->if_sklist == NULL) {
267
                intrfc->if_sklist = sk;
268
        } else {
269
                for (s = intrfc->if_sklist; s->next != NULL; s = s->next)
270
                        ;
271
                s->next = sk;
272
        }
273
}
274
 
275
static ipx_socket *
276
ipxitf_find_socket(ipx_interface *intrfc, unsigned short port)
277
{
278
        ipx_socket      *s;
279
 
280
        for (s=intrfc->if_sklist;
281
                (s != NULL) && (s->protinfo.af_ipx.port != port);
282
                s=s->next)
283
                ;
284
 
285
        return s;
286
}
287
 
288
#ifdef CONFIG_IPX_INTERN
289
 
290
static ipx_socket *
291
ipxitf_find_internal_socket(ipx_interface *intrfc,
292
                            unsigned char *node, unsigned short port)
293
{
294
        ipx_socket *s = intrfc->if_sklist;
295
 
296
        while (s != NULL)
297
        {
298
                if (   (s->protinfo.af_ipx.port == port)
299
                    && (memcmp(node, s->protinfo.af_ipx.node, IPX_NODE_LEN) == 0))
300
                {
301
                        break;
302
                }
303
                s = s->next;
304
        }
305
        return s;
306
}
307
#endif
308
 
309
static void ipxrtr_del_routes(ipx_interface *);
310
 
311
static void
312
ipxitf_down(ipx_interface *intrfc)
313
{
314
        ipx_interface   *i;
315
        ipx_socket      *s, *t;
316
 
317
        /* Delete all routes associated with this interface */
318
        ipxrtr_del_routes(intrfc);
319
 
320
        /* error sockets */
321
        for (s = intrfc->if_sklist; s != NULL; ) {
322
                s->err = ENOLINK;
323
                s->error_report(s);
324
                s->protinfo.af_ipx.intrfc = NULL;
325
                s->protinfo.af_ipx.port = 0;
326
                s->zapped=1;    /* Indicates it is no longer bound */
327
                t = s;
328
                s = s->next;
329
                t->next = NULL;
330
        }
331
        intrfc->if_sklist = NULL;
332
 
333
        /* remove this interface from list */
334
        if (intrfc == ipx_interfaces) {
335
                ipx_interfaces = intrfc->if_next;
336
        } else {
337
                for (i = ipx_interfaces;
338
                        (i != NULL) && (i->if_next != intrfc);
339
                        i = i->if_next)
340
                        ;
341
                if ((i != NULL) && (i->if_next == intrfc))
342
                        i->if_next = intrfc->if_next;
343
        }
344
 
345
        /* remove this interface from *special* networks */
346
        if (intrfc == ipx_primary_net)
347
                ipxitf_clear_primary_net();
348
        if (intrfc == ipx_internal_net)
349
                ipx_internal_net = NULL;
350
 
351
        kfree_s(intrfc, sizeof(*intrfc));
352
        /* sockets still dangling
353
         * - must be closed from user space
354
         */
355
        MOD_DEC_USE_COUNT;
356
        return;
357
}
358
 
359
static int
360
ipxitf_device_event(struct notifier_block *notifier, unsigned long event, void *ptr)
361
{
362
        struct device *dev = ptr;
363
        ipx_interface *i, *tmp;
364
 
365
        if(event!=NETDEV_DOWN)
366
                return NOTIFY_DONE;
367
 
368
        for (i = ipx_interfaces; i != NULL; ) {
369
 
370
                tmp = i->if_next;
371
                if (i->if_dev == dev)
372
                        ipxitf_down(i);
373
                i = tmp;
374
 
375
        }
376
 
377
        return NOTIFY_DONE;
378
}
379
 
380
static int ipxitf_def_skb_handler(struct sock *sock, struct sk_buff *skb)
381
{
382
        int     retval;
383
 
384
        if((retval = sock_queue_rcv_skb(sock, skb))<0)
385
        {
386
                /*
387
                 * skb->sk is NULL here, so FREE_WRITE does not hurt
388
                 * the sending socket.
389
                 */
390
                kfree_skb(skb,FREE_WRITE);
391
        }
392
        return retval;
393
}
394
 
395
/*
396
 * On input skb->sk is NULL. Nobody is charged for the memory.
397
 */
398
 
399
#ifdef CONFIG_IPX_INTERN
400
static int
401
ipxitf_demux_socket(ipx_interface *intrfc, struct sk_buff *skb, int copy)
402
{
403
        ipx_packet      *ipx = (ipx_packet *)(skb->h.raw);
404
        ipx_socket      *s;
405
 
406
        int is_broadcast = (memcmp(ipx->ipx_dest.node, ipx_broadcast_node,
407
                                   IPX_NODE_LEN) == 0);
408
 
409
        s = intrfc->if_sklist;
410
 
411
        while (s != NULL)
412
        {
413
                if (   (s->protinfo.af_ipx.port == ipx->ipx_dest.sock)
414
                    && (   is_broadcast
415
                        || (memcmp(ipx->ipx_dest.node, s->protinfo.af_ipx.node,
416
                                   IPX_NODE_LEN) == 0)))
417
                {
418
                        /* We found a socket to which to send */
419
                        struct sk_buff *skb1;
420
 
421
                        if (copy != 0)
422
                        {
423
                                skb1 = skb_clone(skb, GFP_ATOMIC);
424
                                if (skb1 != NULL)
425
                                {
426
                                        skb1->arp = skb1->free = 1;
427
                                }
428
                                else
429
                                {
430
                                        return -ENOMEM;
431
                                }
432
                        }
433
                        else
434
                        {
435
                                skb1 = skb;
436
                                copy = 1; /* skb may only be used once */
437
                        }
438
                        ipxitf_def_skb_handler(s, skb1);
439
 
440
                        if (intrfc != ipx_internal_net)
441
                        {
442
                                /* on an external interface, at most
443
                                 * one socket can listen.
444
                                 */
445
                                break;
446
                        }
447
                }
448
                s = s->next;
449
        }
450
 
451
        if (copy == 0)
452
        {
453
                /* skb was solely for us, and we did not make a copy,
454
                 * so free it. FREE_WRITE does not hurt, because
455
                 * skb->sk is NULL here.
456
                 */
457
                kfree_skb(skb, FREE_WRITE);
458
        }
459
        return 0;
460
}
461
 
462
#else
463
 
464
static int
465
ipxitf_demux_socket(ipx_interface *intrfc, struct sk_buff *skb, int copy)
466
{
467
        ipx_packet      *ipx = (ipx_packet *)(skb->h.raw);
468
        ipx_socket      *sock1 = NULL, *sock2 = NULL;
469
        struct sk_buff  *skb1 = NULL, *skb2 = NULL;
470
 
471
        if (intrfc == ipx_primary_net
472
          && ntohs(ipx->ipx_dest.sock) == 0x451)
473
        {
474
          /*
475
           * The packet's target is a NCP connection handler. We want to
476
           * hand it to the correct socket directly within the kernel,
477
           * so that the mars_nwe packet distribution process
478
           * does not have to do it. Here we only care about NCP and
479
           * BURST packets.
480
           * You might call this a hack, but believe me, you do not
481
           * want a complete NCP layer in the kernel, and this is
482
           * VERY fast as well.
483
           */
484
          int connection = 0;
485
 
486
          if (    *((char*)(ipx+1))   == 0x22
487
              &&  *((char*)(ipx+1)+1) == 0x22)
488
          {
489
                /*
490
                 * The packet is a NCP request
491
                 */
492
                 connection = ( ((int) *((char*)(ipx+1)+5)) << 8 )
493
                               | (int) *((char*)(ipx+1)+3);
494
          }
495
          else if (    *((char*)(ipx+1))   == 0x77
496
                   &&  *((char*)(ipx+1)+1) == 0x77)
497
          {
498
                /*
499
                 * The packet is a BURST packet
500
                 */
501
                 connection = ( ((int) *((char*)(ipx+1)+9)) << 8 )
502
                               | (int) *((char*)(ipx+1)+8);
503
          }
504
 
505
          if (connection)
506
          {
507
            /*
508
             * Now we have to look for a special NCP connection handling
509
             * socket. Only these sockets have ipx_ncp_conn != 0, set
510
             * by SIOCIPXNCPCONN.
511
             */
512
            for (sock1=intrfc->if_sklist;
513
                (sock1 != NULL) &&
514
                (sock1->protinfo.af_ipx.ipx_ncp_conn != connection);
515
                sock1=sock1->next);;
516
          }
517
        }
518
        if (sock1 == NULL)
519
        {
520
                /* No special socket found, forward the packet the
521
                 * normal way.
522
                 */
523
                sock1 = ipxitf_find_socket(intrfc, ipx->ipx_dest.sock);
524
        }
525
 
526
        /*
527
         *      We need to check if there is a primary net and if
528
         *      this is addressed to one of the *SPECIAL* sockets because
529
         *      these need to be propagated to the primary net.
530
         *      The *SPECIAL* socket list contains: 0x452(SAP), 0x453(RIP) and
531
         *      0x456(Diagnostic).
532
         */
533
 
534
        if (ipx_primary_net && (intrfc != ipx_primary_net))
535
        {
536
                switch (ntohs(ipx->ipx_dest.sock))
537
                {
538
                        case 0x452:
539
                        case 0x453:
540
                        case 0x456:
541
                                /*
542
                                 *      The appropriate thing to do here is to
543
                                 *      dup the packet and route to the primary net
544
                                 *      interface via ipxitf_send; however, we'll cheat
545
                                 *      and just demux it here.
546
                                 */
547
                                sock2 = ipxitf_find_socket(ipx_primary_net,
548
                                        ipx->ipx_dest.sock);
549
                                break;
550
                        default:
551
                                break;
552
                }
553
        }
554
 
555
        /*
556
         *      if there is nothing to do, return. The kfree will
557
         *      cancel any charging.
558
         */
559
 
560
        if (sock1 == NULL && sock2 == NULL)
561
        {
562
                if (!copy)
563
                        kfree_skb(skb,FREE_WRITE);
564
                return 0;
565
        }
566
 
567
        /*
568
         * This next segment of code is a little awkward, but it sets it up
569
         * so that the appropriate number of copies of the SKB are made and
570
         * that skb1 and skb2 point to it (them) so that it (they) can be
571
         * demuxed to sock1 and/or sock2.  If we are unable to make enough
572
         * copies, we do as much as is possible.
573
         */
574
 
575
        if (copy)
576
        {
577
                skb1 = skb_clone(skb, GFP_ATOMIC);
578
                if (skb1 != NULL)
579
                        skb1->arp = skb1->free = 1;
580
        }
581
        else
582
        {
583
                skb1 = skb;
584
        }
585
 
586
        if (skb1 == NULL)
587
                return -ENOMEM;
588
 
589
        /*
590
         *      Do we need 2 SKBs?
591
         */
592
 
593
        if (sock1 && sock2)
594
        {
595
                skb2 = skb_clone(skb1, GFP_ATOMIC);
596
                if (skb2 != NULL)
597
                        skb2->arp = skb2->free = 1;
598
        }
599
        else
600
                skb2 = skb1;
601
 
602
        if (sock1)
603
                (void) ipxitf_def_skb_handler(sock1, skb1);
604
 
605
        if (skb2 == NULL)
606
                return -ENOMEM;
607
 
608
        if (sock2)
609
                (void) ipxitf_def_skb_handler(sock2, skb2);
610
 
611
        return 0;
612
}
613
#endif
614
 
615
static struct sk_buff *
616
ipxitf_adjust_skbuff(ipx_interface *intrfc, struct sk_buff *skb)
617
{
618
        struct sk_buff  *skb2;
619
        int     in_offset = skb->h.raw - skb->head;
620
        int     out_offset = intrfc->if_ipx_offset;
621
        int     len;
622
 
623
        /* Hopefully, most cases */
624
        if (in_offset >= out_offset) {
625
                skb->arp = skb->free = 1;
626
                return skb;
627
        }
628
 
629
        /* Need new SKB */
630
        len = skb->len + out_offset;
631
        skb2 = alloc_skb(len, GFP_ATOMIC);
632
        if (skb2 != NULL) {
633
                skb_reserve(skb2,out_offset);
634
                skb2->h.raw=skb_put(skb2,skb->len);
635
                skb2->free=1;
636
                skb2->arp=1;
637
                memcpy(skb2->h.raw, skb->h.raw, skb->len);
638
        }
639
        kfree_skb(skb, FREE_WRITE);
640
        return skb2;
641
}
642
 
643
static int ipxitf_send(ipx_interface *intrfc, struct sk_buff *skb, char *node)
644
{
645
        ipx_packet      *ipx = (ipx_packet *)(skb->h.raw);
646
        struct device   *dev = intrfc->if_dev;
647
        struct datalink_proto   *dl = intrfc->if_dlink;
648
        char            dest_node[IPX_NODE_LEN];
649
        int             send_to_wire = 1;
650
        int             addr_len;
651
 
652
        /*
653
         *      We need to know how many skbuffs it will take to send out this
654
         *      packet to avoid unnecessary copies.
655
         */
656
 
657
        if ((dl == NULL) || (dev == NULL) || (dev->flags & IFF_LOOPBACK))
658
                send_to_wire = 0;        /* No non looped */
659
 
660
        /*
661
         *      See if this should be demuxed to sockets on this interface
662
         *
663
         *      We want to ensure the original was eaten or that we only use
664
         *      up clones.
665
         */
666
 
667
        if (ipx->ipx_dest.net == intrfc->if_netnum)
668
        {
669
                /*
670
                 *      To our own node, loop and free the original.
671
                 */
672
                if (memcmp(intrfc->if_node, node, IPX_NODE_LEN) == 0)
673
                {
674
                        /*
675
                         *      Don't charge sender
676
                         */
677
                        if(skb->sk)
678
                        {
679
                                atomic_sub(skb->truesize, &skb->sk->wmem_alloc);
680
                                skb->sk=NULL;
681
                        }
682
                        /*
683
                         *      Will charge receiver
684
                         */
685
                        return ipxitf_demux_socket(intrfc, skb, 0);
686
                }
687
                /*
688
                 *      Broadcast, loop and possibly keep to send on.
689
                 */
690
                if (memcmp(ipx_broadcast_node, node, IPX_NODE_LEN) == 0)
691
                {
692
                        if (!send_to_wire && skb->sk)
693
                        {
694
                                atomic_sub(skb->truesize, &skb->sk->wmem_alloc);
695
                                skb->sk=NULL;
696
                        }
697
                        ipxitf_demux_socket(intrfc, skb, send_to_wire);
698
                        if (!send_to_wire)
699
                                return 0;
700
                }
701
        }
702
 
703
        /*
704
         *      If the originating net is not equal to our net; this is routed
705
         *      We are still charging the sender. Which is right - the driver
706
         *      free will handle this fairly.
707
         */
708
 
709
        if (ipx->ipx_source.net != intrfc->if_netnum)
710
        {
711
                if (++(ipx->ipx_tctrl) > ipxcfg_max_hops)
712
                        send_to_wire = 0;
713
        }
714
 
715
        if (!send_to_wire)
716
        {
717
                /*
718
                 *      We do a FREE_WRITE here because this indicates how
719
                 *      to treat the socket with which the packet is
720
                 *      associated.  If this packet is associated with a
721
                 *      socket at all, it must be the originator of the
722
                 *      packet.   Routed packets will have no socket associated
723
                 *      with them.
724
                 */
725
                kfree_skb(skb,FREE_WRITE);
726
                return 0;
727
        }
728
 
729
        /*
730
         *      Determine the appropriate hardware address
731
         */
732
 
733
        addr_len = dev->addr_len;
734
        if (memcmp(ipx_broadcast_node, node, IPX_NODE_LEN) == 0)
735
                memcpy(dest_node, dev->broadcast, addr_len);
736
        else
737
                memcpy(dest_node, &(node[IPX_NODE_LEN-addr_len]), addr_len);
738
 
739
        /*
740
         *      Make any compensation for differing physical/data link size
741
         */
742
 
743
        skb = ipxitf_adjust_skbuff(intrfc, skb);
744
        if (skb == NULL)
745
                return 0;
746
 
747
        /* set up data link and physical headers */
748
        skb->dev = dev;
749
        skb->protocol = htons(ETH_P_IPX);
750
        dl->datalink_header(dl, skb, dest_node);
751
#if 0
752
        /*
753
         *      Now log the packet just before transmission
754
         */
755
 
756
        dump_pkt("IPX snd:", (ipx_packet *)skb->h.raw);
757
        dump_data("ETH hdr:", skb->data, skb->h.raw - skb->data);
758
#endif
759
 
760
        /*
761
         *      Send it out
762
         */
763
 
764
        dev_queue_xmit(skb, dev, SOPRI_NORMAL);
765
        return 0;
766
}
767
 
768
static int ipxrtr_add_route(unsigned long, ipx_interface *, unsigned char *);
769
 
770
static int ipxitf_add_local_route(ipx_interface *intrfc)
771
{
772
        return ipxrtr_add_route(intrfc->if_netnum, intrfc, NULL);
773
}
774
 
775
static const char * ipx_frame_name(unsigned short);
776
static const char * ipx_device_name(ipx_interface *);
777
static int ipxrtr_route_skb(struct sk_buff *);
778
 
779
static int ipxitf_rcv(ipx_interface *intrfc, struct sk_buff *skb)
780
{
781
        ipx_packet      *ipx = (ipx_packet *) (skb->h.raw);
782
        ipx_interface   *i;
783
 
784
#ifdef CONFIG_FIREWALL  
785
        /*
786
         *      We firewall first, ask questions later.
787
         */
788
 
789
        if (call_in_firewall(PF_IPX, skb->dev, ipx, NULL)!=FW_ACCEPT)
790
        {
791
                kfree_skb(skb, FREE_READ);
792
                return 0;
793
        }
794
 
795
#endif  
796
 
797
        /* See if we should update our network number */
798
        if ((intrfc->if_netnum == 0L) &&
799
                (ipx->ipx_source.net == ipx->ipx_dest.net) &&
800
                (ipx->ipx_source.net != 0L))
801
        {
802
                /* NB: NetWare servers lie about their hop count so we
803
                 * dropped the test based on it.  This is the best way
804
                 * to determine this is a 0 hop count packet.
805
                 */
806
                if ((i=ipxitf_find_using_net(ipx->ipx_source.net))==NULL)
807
                {
808
                        intrfc->if_netnum = ipx->ipx_source.net;
809
                        (void) ipxitf_add_local_route(intrfc);
810
                }
811
                else
812
                {
813
                        printk(KERN_WARNING "IPX: Network number collision %lx\n        %s %s and %s %s\n",
814
                                htonl(ipx->ipx_source.net),
815
                                ipx_device_name(i),
816
                                ipx_frame_name(i->if_dlink_type),
817
                                ipx_device_name(intrfc),
818
                                ipx_frame_name(intrfc->if_dlink_type));
819
                }
820
        }
821
 
822
        if (ipx->ipx_dest.net == 0L)
823
                ipx->ipx_dest.net = intrfc->if_netnum;
824
        if (ipx->ipx_source.net == 0L)
825
                ipx->ipx_source.net = intrfc->if_netnum;
826
 
827
        if (intrfc->if_netnum != ipx->ipx_dest.net)
828
        {
829
#ifdef CONFIG_FIREWALL  
830
                /*
831
                 *      See if we are allowed to firewall forward
832
                 */
833
                if (call_fw_firewall(PF_IPX, skb->dev, ipx, NULL)!=FW_ACCEPT)
834
                {
835
                        kfree_skb(skb, FREE_READ);
836
                        return 0;
837
                }
838
#endif          
839
                /* We only route point-to-point packets. */
840
                if (skb->pkt_type == PACKET_HOST)
841
                        return ipxrtr_route_skb(skb);
842
 
843
                kfree_skb(skb,FREE_READ);
844
                return 0;
845
        }
846
 
847
        /* see if we should keep it */
848
        if ((memcmp(ipx_broadcast_node, ipx->ipx_dest.node, IPX_NODE_LEN) == 0)
849
                || (memcmp(intrfc->if_node, ipx->ipx_dest.node, IPX_NODE_LEN) == 0))
850
        {
851
                return ipxitf_demux_socket(intrfc, skb, 0);
852
        }
853
 
854
        /* we couldn't pawn it off so unload it */
855
        kfree_skb(skb,FREE_READ);
856
        return 0;
857
}
858
 
859
static void
860
ipxitf_insert(ipx_interface *intrfc)
861
{
862
        ipx_interface   *i;
863
 
864
        intrfc->if_next = NULL;
865
        if (ipx_interfaces == NULL) {
866
                ipx_interfaces = intrfc;
867
        } else {
868
                for (i = ipx_interfaces; i->if_next != NULL; i = i->if_next)
869
                        ;
870
                i->if_next = intrfc;
871
        }
872
 
873
        if (ipxcfg_auto_select_primary && (ipx_primary_net == NULL))
874
                ipx_primary_net = intrfc;
875
        MOD_INC_USE_COUNT;
876
        return;
877
}
878
 
879
static int
880
ipxitf_create_internal(ipx_interface_definition *idef)
881
{
882
        ipx_interface   *intrfc;
883
 
884
        /* Only one primary network allowed */
885
        if (ipx_primary_net != NULL) return -EEXIST;
886
 
887
        /* Must have a valid network number */
888
        if (idef->ipx_network == 0L) return -EADDRNOTAVAIL;
889
        if (ipxitf_find_using_net(idef->ipx_network) != NULL)
890
                return -EADDRINUSE;
891
 
892
        intrfc=(ipx_interface *)kmalloc(sizeof(ipx_interface),GFP_ATOMIC);
893
        if (intrfc==NULL)
894
                return -EAGAIN;
895
        intrfc->if_dev=NULL;
896
        intrfc->if_netnum=idef->ipx_network;
897
        intrfc->if_dlink_type = 0;
898
        intrfc->if_dlink = NULL;
899
        intrfc->if_sklist = NULL;
900
        intrfc->if_internal = 1;
901
        intrfc->if_ipx_offset = 0;
902
        intrfc->if_sknum = IPX_MIN_EPHEMERAL_SOCKET;
903
        memcpy((char *)&(intrfc->if_node), idef->ipx_node, IPX_NODE_LEN);
904
        ipx_internal_net = intrfc;
905
        ipx_primary_net = intrfc;
906
        ipxitf_insert(intrfc);
907
        return ipxitf_add_local_route(intrfc);
908
}
909
 
910
static int
911
ipx_map_frame_type(unsigned char type)
912
{
913
        switch (type) {
914
        case IPX_FRAME_ETHERII: return htons(ETH_P_IPX);
915
        case IPX_FRAME_8022: return htons(ETH_P_802_2);
916
        case IPX_FRAME_TR_8022: return htons(ETH_P_TR_802_2);
917
        case IPX_FRAME_SNAP: return htons(ETH_P_SNAP);
918
        case IPX_FRAME_8023: return htons(ETH_P_802_3);
919
        }
920
        return 0;
921
}
922
 
923
static int
924
ipxitf_create(ipx_interface_definition *idef)
925
{
926
        struct device   *dev;
927
        unsigned short  dlink_type = 0;
928
        struct datalink_proto   *datalink = NULL;
929
        ipx_interface   *intrfc;
930
 
931
        if (idef->ipx_special == IPX_INTERNAL)
932
                return ipxitf_create_internal(idef);
933
 
934
        if ((idef->ipx_special == IPX_PRIMARY) && (ipx_primary_net != NULL))
935
                return -EEXIST;
936
 
937
        if ((idef->ipx_network != 0L) &&
938
                (ipxitf_find_using_net(idef->ipx_network) != NULL))
939
                return -EADDRINUSE;
940
 
941
        switch (idef->ipx_dlink_type) {
942
        case IPX_FRAME_ETHERII:
943
                dlink_type = htons(ETH_P_IPX);
944
                datalink = pEII_datalink;
945
                break;
946
        case IPX_FRAME_TR_8022:
947
                dlink_type = htons(ETH_P_TR_802_2);
948
                datalink = p8022tr_datalink;
949
                break;
950
        case IPX_FRAME_8022:
951
                dlink_type = htons(ETH_P_802_2);
952
                datalink = p8022_datalink;
953
                break;
954
        case IPX_FRAME_SNAP:
955
                dlink_type = htons(ETH_P_SNAP);
956
                datalink = pSNAP_datalink;
957
                break;
958
        case IPX_FRAME_8023:
959
                dlink_type = htons(ETH_P_802_3);
960
                datalink = p8023_datalink;
961
                break;
962
        case IPX_FRAME_NONE:
963
        default:
964
                break;
965
        }
966
 
967
        if (datalink == NULL)
968
                return -EPROTONOSUPPORT;
969
 
970
        dev=dev_get(idef->ipx_device);
971
        if (dev==NULL)
972
                return -ENODEV;
973
 
974
        if (!(dev->flags & IFF_UP))
975
                return -ENETDOWN;
976
 
977
        /* Check addresses are suitable */
978
        if(dev->addr_len>IPX_NODE_LEN)
979
                return -EINVAL;
980
 
981
        if ((intrfc = ipxitf_find_using_phys(dev, dlink_type)) == NULL)
982
        {
983
                /* Ok now create */
984
                intrfc=(ipx_interface *)kmalloc(sizeof(ipx_interface),GFP_ATOMIC);
985
                if (intrfc==NULL)
986
                        return -EAGAIN;
987
                intrfc->if_dev=dev;
988
                intrfc->if_netnum=idef->ipx_network;
989
                intrfc->if_dlink_type = dlink_type;
990
                intrfc->if_dlink = datalink;
991
                intrfc->if_sklist = NULL;
992
                intrfc->if_sknum = IPX_MIN_EPHEMERAL_SOCKET;
993
                /* Setup primary if necessary */
994
                if ((idef->ipx_special == IPX_PRIMARY))
995
                        ipx_primary_net = intrfc;
996
                intrfc->if_internal = 0;
997
                intrfc->if_ipx_offset = dev->hard_header_len + datalink->header_length;
998
                if(memcmp(idef->ipx_node, "\000\000\000\000\000\000", IPX_NODE_LEN)==0)
999
                {
1000
                        memset(intrfc->if_node, 0, IPX_NODE_LEN);
1001
                        memcpy((char *)&(intrfc->if_node[IPX_NODE_LEN-dev->addr_len]),
1002
                                dev->dev_addr, dev->addr_len);
1003
                }
1004
                else
1005
                        memcpy(intrfc->if_node, idef->ipx_node, IPX_NODE_LEN);
1006
                ipxitf_insert(intrfc);
1007
        }
1008
 
1009
        /* If the network number is known, add a route */
1010
        if (intrfc->if_netnum == 0L)
1011
                return 0;
1012
 
1013
        return ipxitf_add_local_route(intrfc);
1014
}
1015
 
1016
static int
1017
ipxitf_delete(ipx_interface_definition *idef)
1018
{
1019
        struct device   *dev = NULL;
1020
        unsigned short  dlink_type = 0;
1021
        ipx_interface   *intrfc;
1022
 
1023
        if (idef->ipx_special == IPX_INTERNAL) {
1024
                if (ipx_internal_net != NULL) {
1025
                        ipxitf_down(ipx_internal_net);
1026
                        return 0;
1027
                }
1028
                return -ENOENT;
1029
        }
1030
 
1031
        dlink_type = ipx_map_frame_type(idef->ipx_dlink_type);
1032
        if (dlink_type == 0)
1033
                return -EPROTONOSUPPORT;
1034
 
1035
        dev=dev_get(idef->ipx_device);
1036
        if(dev==NULL) return -ENODEV;
1037
 
1038
        intrfc = ipxitf_find_using_phys(dev, dlink_type);
1039
        if (intrfc != NULL) {
1040
                ipxitf_down(intrfc);
1041
                return 0;
1042
        }
1043
        return -EINVAL;
1044
}
1045
 
1046
static ipx_interface *
1047
ipxitf_auto_create(struct device *dev, unsigned short dlink_type)
1048
{
1049
        struct datalink_proto *datalink = NULL;
1050
        ipx_interface   *intrfc;
1051
 
1052
        switch (htons(dlink_type)) {
1053
        case ETH_P_IPX: datalink = pEII_datalink; break;
1054
        case ETH_P_802_2: datalink = p8022_datalink; break;
1055
        case ETH_P_TR_802_2: datalink = p8022tr_datalink; break;
1056
        case ETH_P_SNAP: datalink = pSNAP_datalink; break;
1057
        case ETH_P_802_3: datalink = p8023_datalink; break;
1058
        default: return NULL;
1059
        }
1060
 
1061
        if (dev == NULL)
1062
                return NULL;
1063
 
1064
        /* Check addresses are suitable */
1065
        if(dev->addr_len>IPX_NODE_LEN) return NULL;
1066
 
1067
        intrfc=(ipx_interface *)kmalloc(sizeof(ipx_interface),GFP_ATOMIC);
1068
        if (intrfc!=NULL) {
1069
                intrfc->if_dev=dev;
1070
                intrfc->if_netnum=0L;
1071
                intrfc->if_dlink_type = dlink_type;
1072
                intrfc->if_dlink = datalink;
1073
                intrfc->if_sklist = NULL;
1074
                intrfc->if_internal = 0;
1075
                intrfc->if_sknum = IPX_MIN_EPHEMERAL_SOCKET;
1076
                intrfc->if_ipx_offset = dev->hard_header_len +
1077
                        datalink->header_length;
1078
                memset(intrfc->if_node, 0, IPX_NODE_LEN);
1079
                memcpy((char *)&(intrfc->if_node[IPX_NODE_LEN-dev->addr_len]),
1080
                        dev->dev_addr, dev->addr_len);
1081
                ipxitf_insert(intrfc);
1082
        }
1083
 
1084
        return intrfc;
1085
}
1086
 
1087
static int
1088
ipxitf_ioctl_real(unsigned int cmd, void *arg)
1089
{
1090
        int err;
1091
        switch(cmd)
1092
        {
1093
                case SIOCSIFADDR:
1094
                {
1095
                        struct ifreq ifr;
1096
                        struct sockaddr_ipx *sipx;
1097
                        ipx_interface_definition f;
1098
                        err=verify_area(VERIFY_READ,arg,sizeof(ifr));
1099
                        if(err)
1100
                                return err;
1101
                        memcpy_fromfs(&ifr,arg,sizeof(ifr));
1102
                        sipx=(struct sockaddr_ipx *)&ifr.ifr_addr;
1103
                        if(sipx->sipx_family!=AF_IPX)
1104
                                return -EINVAL;
1105
                        f.ipx_network=sipx->sipx_network;
1106
                        memcpy(f.ipx_device, ifr.ifr_name, sizeof(f.ipx_device));
1107
                        memcpy(f.ipx_node, sipx->sipx_node, IPX_NODE_LEN);
1108
                        f.ipx_dlink_type=sipx->sipx_type;
1109
                        f.ipx_special=sipx->sipx_special;
1110
                        if(sipx->sipx_action==IPX_DLTITF)
1111
                                return ipxitf_delete(&f);
1112
                        else
1113
                                return ipxitf_create(&f);
1114
                }
1115
                case SIOCGIFADDR:
1116
                {
1117
                        struct ifreq ifr;
1118
                        struct sockaddr_ipx *sipx;
1119
                        ipx_interface *ipxif;
1120
                        struct device *dev;
1121
                        err=verify_area(VERIFY_WRITE,arg,sizeof(ifr));
1122
                        if(err)
1123
                                return err;
1124
                        memcpy_fromfs(&ifr,arg,sizeof(ifr));
1125
                        sipx=(struct sockaddr_ipx *)&ifr.ifr_addr;
1126
                        dev=dev_get(ifr.ifr_name);
1127
                        if(!dev)
1128
                                return -ENODEV;
1129
                        ipxif=ipxitf_find_using_phys(dev, ipx_map_frame_type(sipx->sipx_type));
1130
                        if(ipxif==NULL)
1131
                                return -EADDRNOTAVAIL;
1132
                        sipx->sipx_family=AF_IPX;
1133
                        sipx->sipx_network=ipxif->if_netnum;
1134
                        memcpy(sipx->sipx_node, ipxif->if_node, sizeof(sipx->sipx_node));
1135
                        memcpy_tofs(arg,&ifr,sizeof(ifr));
1136
                        return 0;
1137
                }
1138
                case SIOCAIPXITFCRT:
1139
                        err=verify_area(VERIFY_READ,arg,sizeof(char));
1140
                        if(err)
1141
                                return err;
1142
                        return ipxcfg_set_auto_create(get_fs_byte(arg));
1143
                case SIOCAIPXPRISLT:
1144
                        err=verify_area(VERIFY_READ,arg,sizeof(char));
1145
                        if(err)
1146
                                return err;
1147
                        return ipxcfg_set_auto_select(get_fs_byte(arg));
1148
                default:
1149
                        return -EINVAL;
1150
        }
1151
}
1152
 
1153
static int
1154
ipxitf_ioctl(unsigned int cmd, void *arg)
1155
{
1156
        int ret;
1157
        MOD_INC_USE_COUNT;
1158
        ret = ipxitf_ioctl_real (cmd,arg);
1159
        MOD_DEC_USE_COUNT;
1160
        return ret;
1161
}
1162
/*******************************************************************************************************************\
1163
*                                                                                                                   *
1164
*                                       Routing tables for the IPX socket layer                                     *
1165
*                                                                                                                   *
1166
\*******************************************************************************************************************/
1167
 
1168
static ipx_route *
1169
ipxrtr_lookup(unsigned long net)
1170
{
1171
        ipx_route *r;
1172
 
1173
        for (r=ipx_routes; (r!=NULL) && (r->ir_net!=net); r=r->ir_next)
1174
                ;
1175
 
1176
        return r;
1177
}
1178
 
1179
static int
1180
ipxrtr_add_route(unsigned long network, ipx_interface *intrfc, unsigned char *node)
1181
{
1182
        ipx_route       *rt;
1183
 
1184
        /* Get a route structure; either existing or create */
1185
        rt = ipxrtr_lookup(network);
1186
        if (rt==NULL) {
1187
                rt=(ipx_route *)kmalloc(sizeof(ipx_route),GFP_ATOMIC);
1188
                if(rt==NULL)
1189
                        return -EAGAIN;
1190
                rt->ir_next=ipx_routes;
1191
                ipx_routes=rt;
1192
        }
1193
        else if (intrfc == ipx_internal_net)
1194
                return(-EEXIST);
1195
 
1196
        rt->ir_net = network;
1197
        rt->ir_intrfc = intrfc;
1198
        if (node == NULL) {
1199
                memset(rt->ir_router_node, '\0', IPX_NODE_LEN);
1200
                rt->ir_routed = 0;
1201
        } else {
1202
                memcpy(rt->ir_router_node, node, IPX_NODE_LEN);
1203
                rt->ir_routed=1;
1204
        }
1205
        return 0;
1206
}
1207
 
1208
static void
1209
ipxrtr_del_routes(ipx_interface *intrfc)
1210
{
1211
        ipx_route       **r, *tmp;
1212
 
1213
        for (r = &ipx_routes; (tmp = *r) != NULL; ) {
1214
                if (tmp->ir_intrfc == intrfc) {
1215
                        *r = tmp->ir_next;
1216
                        kfree_s(tmp, sizeof(ipx_route));
1217
                } else {
1218
                        r = &(tmp->ir_next);
1219
                }
1220
        }
1221
}
1222
 
1223
static int
1224
ipxrtr_create(ipx_route_definition *rd)
1225
{
1226
        ipx_interface *intrfc;
1227
 
1228
        /* Find the appropriate interface */
1229
        intrfc = ipxitf_find_using_net(rd->ipx_router_network);
1230
        if (intrfc == NULL)
1231
                return -ENETUNREACH;
1232
 
1233
        return ipxrtr_add_route(rd->ipx_network, intrfc, rd->ipx_router_node);
1234
}
1235
 
1236
 
1237
static int
1238
ipxrtr_delete(long net)
1239
{
1240
        ipx_route       **r;
1241
        ipx_route       *tmp;
1242
 
1243
        for (r = &ipx_routes; (tmp = *r) != NULL; ) {
1244
                if (tmp->ir_net == net) {
1245
                        if (!(tmp->ir_routed)) {
1246
                                /* Directly connected; can't lose route */
1247
                                return -EPERM;
1248
                        }
1249
                        *r = tmp->ir_next;
1250
                        kfree_s(tmp, sizeof(ipx_route));
1251
                        return 0;
1252
                }
1253
                r = &(tmp->ir_next);
1254
        }
1255
 
1256
        return -ENOENT;
1257
}
1258
 
1259
/*
1260
 *      Checksum routine for IPX
1261
 */
1262
 
1263
/* Note: We assume ipx_tctrl==0 and htons(length)==ipx_pktsize */
1264
 
1265
static __u16 ipx_set_checksum(ipx_packet *packet,int length)
1266
{
1267
        /*
1268
         *      NOTE: sum is a net byte order quantity, which optimizes the
1269
         *      loop. This only works on big and little endian machines. (I
1270
         *      don't know of a machine that isn't.)
1271
         */
1272
 
1273
        __u32 sum=0;
1274
 
1275
        /*
1276
         *      Pointer to second word - We skip the checksum field
1277
         */
1278
 
1279
        __u16 *p=(__u16 *)&packet->ipx_pktsize;
1280
 
1281
        /*
1282
         *      Number of complete words
1283
         */
1284
 
1285
        __u32 i=length>>1;
1286
        char    hops = packet->ipx_tctrl;
1287
 
1288
        packet->ipx_tctrl = 0;     /* hop count excluded from checksum calc */
1289
 
1290
        /*
1291
         *      Loop through all complete words except the checksum field
1292
         */
1293
 
1294
        while(--i)
1295
                sum+=*p++;
1296
 
1297
        /*
1298
         *      Add on the last part word if it exists
1299
         */
1300
 
1301
        if(packet->ipx_pktsize&htons(1))
1302
                sum+=ntohs(0xff00)&*p;
1303
 
1304
        packet->ipx_tctrl = hops;
1305
        /*
1306
         *      Do final fixup
1307
         */
1308
 
1309
        sum=(sum&0xffff)+(sum>>16);
1310
 
1311
        /*
1312
         *      It's a pity there's no concept of carry in C
1313
         */
1314
 
1315
        if(sum>=0x10000)
1316
                sum++;
1317
 
1318
        return ~sum;
1319
};
1320
 
1321
 
1322
/*
1323
 *      Route an outgoing frame from a socket.
1324
 */
1325
 
1326
static int ipxrtr_route_packet(ipx_socket *sk, struct sockaddr_ipx *usipx, struct iovec *iov, int len, int noblock)
1327
{
1328
        struct sk_buff *skb;
1329
        ipx_interface *intrfc;
1330
        ipx_packet *ipx;
1331
        int size;
1332
        int ipx_offset;
1333
        ipx_route *rt = NULL;
1334
        int err;
1335
 
1336
        /* Find the appropriate interface on which to send packet */
1337
        if ((usipx->sipx_network == 0L) && (ipx_primary_net != NULL))
1338
        {
1339
                usipx->sipx_network = ipx_primary_net->if_netnum;
1340
                intrfc = ipx_primary_net;
1341
        }
1342
        else
1343
        {
1344
                rt = ipxrtr_lookup(usipx->sipx_network);
1345
                if (rt==NULL) {
1346
                        return -ENETUNREACH;
1347
                }
1348
                intrfc = rt->ir_intrfc;
1349
        }
1350
 
1351
        ipx_offset = intrfc->if_ipx_offset;
1352
        size=sizeof(ipx_packet)+len;
1353
        size += ipx_offset;
1354
 
1355
        skb=sock_alloc_send_skb(sk, size, 0, noblock, &err);
1356
        if(skb==NULL)
1357
                return err;
1358
 
1359
        skb_reserve(skb,ipx_offset);
1360
        skb->free=1;
1361
        skb->arp=1;
1362
        skb->sk=sk;
1363
 
1364
        /* Fill in IPX header */
1365
        ipx=(ipx_packet *)skb_put(skb,sizeof(ipx_packet));
1366
        ipx->ipx_pktsize=htons(len+sizeof(ipx_packet));
1367
        ipx->ipx_tctrl=0;
1368
        ipx->ipx_type=usipx->sipx_type;
1369
        skb->h.raw = (unsigned char *)ipx;
1370
 
1371
        ipx->ipx_source.net = sk->protinfo.af_ipx.intrfc->if_netnum;
1372
#ifdef CONFIG_IPX_INTERN
1373
        memcpy(ipx->ipx_source.node, sk->protinfo.af_ipx.node, IPX_NODE_LEN);
1374
#else
1375
        if ((err = ntohs(sk->protinfo.af_ipx.port)) == 0x453 || err == 0x452)
1376
        {
1377
                /* RIP/SAP special handling for mars_nwe */
1378
                ipx->ipx_source.net = intrfc->if_netnum;
1379
                memcpy(ipx->ipx_source.node, intrfc->if_node, IPX_NODE_LEN);
1380
        }
1381
        else
1382
        {
1383
                ipx->ipx_source.net = sk->protinfo.af_ipx.intrfc->if_netnum;
1384
                memcpy(ipx->ipx_source.node, sk->protinfo.af_ipx.intrfc->if_node, IPX_NODE_LEN);
1385
        }
1386
#endif
1387
        ipx->ipx_source.sock = sk->protinfo.af_ipx.port;
1388
        ipx->ipx_dest.net=usipx->sipx_network;
1389
        memcpy(ipx->ipx_dest.node,usipx->sipx_node,IPX_NODE_LEN);
1390
        ipx->ipx_dest.sock=usipx->sipx_port;
1391
 
1392
        memcpy_fromiovec(skb_put(skb,len),iov,len);
1393
 
1394
        /*
1395
         *      Apply checksum. Not allowed on 802.3 links.
1396
         */
1397
 
1398
        if(sk->no_check || intrfc->if_dlink_type==IPX_FRAME_8023)
1399
                ipx->ipx_checksum=0xFFFF;
1400
        else
1401
                ipx->ipx_checksum=ipx_set_checksum(ipx, len+sizeof(ipx_packet));
1402
 
1403
#ifdef CONFIG_FIREWALL  
1404
        if(call_out_firewall(PF_IPX, skb->dev, ipx, NULL)!=FW_ACCEPT)
1405
        {
1406
                kfree_skb(skb, FREE_WRITE);
1407
                return -EPERM;
1408
        }
1409
#endif
1410
 
1411
        return ipxitf_send(intrfc, skb, (rt && rt->ir_routed) ?
1412
                                rt->ir_router_node : ipx->ipx_dest.node);
1413
}
1414
 
1415
static int
1416
ipxrtr_route_skb(struct sk_buff *skb)
1417
{
1418
        ipx_packet      *ipx = (ipx_packet *) (skb->h.raw);
1419
        ipx_route       *r;
1420
        ipx_interface   *i;
1421
 
1422
        r = ipxrtr_lookup(ipx->ipx_dest.net);
1423
        if (r == NULL) {
1424
                /* no known route */
1425
                kfree_skb(skb,FREE_READ);
1426
                return 0;
1427
        }
1428
        i = r->ir_intrfc;
1429
        (void)ipxitf_send(i, skb, (r->ir_routed) ?
1430
                        r->ir_router_node : ipx->ipx_dest.node);
1431
        return 0;
1432
}
1433
 
1434
/*
1435
 *      We use a normal struct rtentry for route handling
1436
 */
1437
 
1438
static int ipxrtr_ioctl(unsigned int cmd, void *arg)
1439
{
1440
        int err;
1441
        struct rtentry rt;      /* Use these to behave like 'other' stacks */
1442
        struct sockaddr_ipx *sg,*st;
1443
 
1444
        err=verify_area(VERIFY_READ,arg,sizeof(rt));
1445
        if(err)
1446
                return err;
1447
 
1448
        memcpy_fromfs(&rt,arg,sizeof(rt));
1449
 
1450
        sg=(struct sockaddr_ipx *)&rt.rt_gateway;
1451
        st=(struct sockaddr_ipx *)&rt.rt_dst;
1452
 
1453
        if(!(rt.rt_flags&RTF_GATEWAY))
1454
                return -EINVAL;         /* Direct routes are fixed */
1455
        if(sg->sipx_family!=AF_IPX)
1456
                return -EINVAL;
1457
        if(st->sipx_family!=AF_IPX)
1458
                return -EINVAL;
1459
 
1460
        switch(cmd)
1461
        {
1462
                case SIOCDELRT:
1463
                        return ipxrtr_delete(st->sipx_network);
1464
                case SIOCADDRT:
1465
                {
1466
                        struct ipx_route_definition f;
1467
                        f.ipx_network=st->sipx_network;
1468
                        f.ipx_router_network=sg->sipx_network;
1469
                        memcpy(f.ipx_router_node, sg->sipx_node, IPX_NODE_LEN);
1470
                        return ipxrtr_create(&f);
1471
                }
1472
                default:
1473
                        return -EINVAL;
1474
        }
1475
}
1476
 
1477
static const char *
1478
ipx_frame_name(unsigned short frame)
1479
{
1480
        switch (ntohs(frame)) {
1481
        case ETH_P_IPX: return "EtherII";
1482
        case ETH_P_802_2: return "802.2";
1483
        case ETH_P_SNAP: return "SNAP";
1484
        case ETH_P_802_3: return "802.3";
1485
        case ETH_P_TR_802_2: return "802.2TR";
1486
        default: return "None";
1487
        }
1488
}
1489
 
1490
static const char *
1491
ipx_device_name(ipx_interface *intrfc)
1492
{
1493
        return (intrfc->if_internal ? "Internal" :
1494
                (intrfc->if_dev ? intrfc->if_dev->name : "Unknown"));
1495
}
1496
 
1497
/* Called from proc fs */
1498
static int ipx_interface_get_info(char *buffer, char **start, off_t offset,
1499
                                  int length, int dummy)
1500
{
1501
        ipx_interface *i;
1502
        int len=0;
1503
        off_t pos=0;
1504
        off_t begin=0;
1505
 
1506
        /* Theory.. Keep printing in the same place until we pass offset */
1507
 
1508
        len += sprintf (buffer,"%-11s%-15s%-9s%-11s%s\n", "Network",
1509
                "Node_Address", "Primary", "Device", "Frame_Type");
1510
        for (i = ipx_interfaces; i != NULL; i = i->if_next) {
1511
                len += sprintf(buffer+len, "%08lX   ", ntohl(i->if_netnum));
1512
                len += sprintf (buffer+len,"%02X%02X%02X%02X%02X%02X   ",
1513
                                i->if_node[0], i->if_node[1], i->if_node[2],
1514
                                i->if_node[3], i->if_node[4], i->if_node[5]);
1515
                len += sprintf(buffer+len, "%-9s", (i == ipx_primary_net) ?
1516
                        "Yes" : "No");
1517
                len += sprintf (buffer+len, "%-11s", ipx_device_name(i));
1518
                len += sprintf (buffer+len, "%s\n",
1519
                        ipx_frame_name(i->if_dlink_type));
1520
 
1521
                /* Are we still dumping unwanted data then discard the record */
1522
                pos=begin+len;
1523
 
1524
                if(pos<offset) {
1525
                        len=0;                   /* Keep dumping into the buffer start */
1526
                        begin=pos;
1527
                }
1528
                if(pos>offset+length)           /* We have dumped enough */
1529
                        break;
1530
        }
1531
 
1532
        /* The data in question runs from begin to begin+len */
1533
        *start=buffer+(offset-begin);   /* Start of wanted data */
1534
        len-=(offset-begin);            /* Remove unwanted header data from length */
1535
        if(len>length)
1536
                len=length;             /* Remove unwanted tail data from length */
1537
 
1538
        return len;
1539
}
1540
 
1541
static int ipx_get_info(char *buffer, char **start, off_t offset,
1542
                        int length, int dummy)
1543
{
1544
        ipx_socket *s;
1545
        ipx_interface *i;
1546
        int len=0;
1547
        off_t pos=0;
1548
        off_t begin=0;
1549
 
1550
        /* Theory.. Keep printing in the same place until we pass offset */
1551
 
1552
#ifdef CONFIG_IPX_INTERN        
1553
        len += sprintf (buffer,"%-28s%-28s%-10s%-10s%-7s%s\n", "Local_Address",
1554
#else
1555
        len += sprintf (buffer,"%-15s%-28s%-10s%-10s%-7s%s\n", "Local_Address",
1556
#endif
1557
                        "Remote_Address", "Tx_Queue", "Rx_Queue",
1558
                        "State", "Uid");
1559
        for (i = ipx_interfaces; i != NULL; i = i->if_next) {
1560
                for (s = i->if_sklist; s != NULL; s = s->next) {
1561
#ifdef CONFIG_IPX_INTERN
1562
                        len += sprintf(buffer+len,
1563
                                       "%08lX:%02X%02X%02X%02X%02X%02X:%04X  ",
1564
                                 htonl(s->protinfo.af_ipx.intrfc->if_netnum),
1565
                                       s->protinfo.af_ipx.node[0],
1566
                                       s->protinfo.af_ipx.node[1],
1567
                                       s->protinfo.af_ipx.node[2],
1568
                                       s->protinfo.af_ipx.node[3],
1569
                                       s->protinfo.af_ipx.node[4],
1570
                                       s->protinfo.af_ipx.node[5],
1571
                                       htons(s->protinfo.af_ipx.port));
1572
#else
1573
                        len += sprintf(buffer+len,"%08lX:%04X  ",
1574
                                       htonl(i->if_netnum),
1575
                                       htons(s->protinfo.af_ipx.port));
1576
#endif
1577
                        if (s->state!=TCP_ESTABLISHED) {
1578
                                len += sprintf(buffer+len, "%-28s", "Not_Connected");
1579
                        } else {
1580
                                len += sprintf (buffer+len,
1581
                                        "%08lX:%02X%02X%02X%02X%02X%02X:%04X  ",
1582
                                        htonl(s->protinfo.af_ipx.dest_addr.net),
1583
                                        s->protinfo.af_ipx.dest_addr.node[0],
1584
                                        s->protinfo.af_ipx.dest_addr.node[1],
1585
                                        s->protinfo.af_ipx.dest_addr.node[2],
1586
                                        s->protinfo.af_ipx.dest_addr.node[3],
1587
                                        s->protinfo.af_ipx.dest_addr.node[4],
1588
                                        s->protinfo.af_ipx.dest_addr.node[5],
1589
                                        htons(s->protinfo.af_ipx.dest_addr.sock));
1590
                        }
1591
                        len += sprintf (buffer+len,"%08X  %08X  ",
1592
                                s->wmem_alloc, s->rmem_alloc);
1593
                        len += sprintf (buffer+len,"%02X     %03d\n",
1594
                                s->state, SOCK_INODE(s->socket)->i_uid);
1595
 
1596
                        /* Are we still dumping unwanted data then discard the record */
1597
                        pos=begin+len;
1598
 
1599
                        if(pos<offset)
1600
                        {
1601
                                len=0;                   /* Keep dumping into the buffer start */
1602
                                begin=pos;
1603
                        }
1604
                        if(pos>offset+length)           /* We have dumped enough */
1605
                                break;
1606
                }
1607
        }
1608
 
1609
        /* The data in question runs from begin to begin+len */
1610
        *start=buffer+(offset-begin);   /* Start of wanted data */
1611
        len-=(offset-begin);            /* Remove unwanted header data from length */
1612
        if(len>length)
1613
                len=length;             /* Remove unwanted tail data from length */
1614
 
1615
        return len;
1616
}
1617
 
1618
static int ipx_rt_get_info(char *buffer, char **start, off_t offset,
1619
                           int length, int dummy)
1620
{
1621
        ipx_route *rt;
1622
        int len=0;
1623
        off_t pos=0;
1624
        off_t begin=0;
1625
 
1626
        len += sprintf (buffer,"%-11s%-13s%s\n",
1627
                        "Network", "Router_Net", "Router_Node");
1628
        for (rt = ipx_routes; rt != NULL; rt = rt->ir_next)
1629
        {
1630
                len += sprintf (buffer+len,"%08lX   ", ntohl(rt->ir_net));
1631
                if (rt->ir_routed) {
1632
                        len += sprintf (buffer+len,"%08lX     %02X%02X%02X%02X%02X%02X\n",
1633
                                ntohl(rt->ir_intrfc->if_netnum),
1634
                                rt->ir_router_node[0], rt->ir_router_node[1],
1635
                                rt->ir_router_node[2], rt->ir_router_node[3],
1636
                                rt->ir_router_node[4], rt->ir_router_node[5]);
1637
                } else {
1638
                        len += sprintf (buffer+len, "%-13s%s\n",
1639
                                        "Directly", "Connected");
1640
                }
1641
                pos=begin+len;
1642
                if(pos<offset)
1643
                {
1644
                        len=0;
1645
                        begin=pos;
1646
                }
1647
                if(pos>offset+length)
1648
                        break;
1649
        }
1650
        *start=buffer+(offset-begin);
1651
        len-=(offset-begin);
1652
        if(len>length)
1653
                len=length;
1654
        return len;
1655
}
1656
 
1657
/*******************************************************************************************************************\
1658
*                                                                                                                   *
1659
*             Handling for system calls applied via the various interfaces to an IPX socket object                  *
1660
*                                                                                                                   *
1661
\*******************************************************************************************************************/
1662
 
1663
static int ipx_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
1664
{
1665
        switch(cmd)
1666
        {
1667
                default:
1668
                        return(-EINVAL);
1669
        }
1670
}
1671
 
1672
static int ipx_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
1673
{
1674
        ipx_socket *sk;
1675
        int err,opt;
1676
 
1677
        sk=(ipx_socket *)sock->data;
1678
 
1679
        if(optval==NULL)
1680
                return(-EINVAL);
1681
 
1682
        err=verify_area(VERIFY_READ,optval,sizeof(int));
1683
        if(err)
1684
                return err;
1685
        opt=get_fs_long((unsigned long *)optval);
1686
 
1687
        switch(level)
1688
        {
1689
                case SOL_IPX:
1690
                        switch(optname)
1691
                        {
1692
                                case IPX_TYPE:
1693
                                        sk->protinfo.af_ipx.type=opt;
1694
                                        return 0;
1695
                                default:
1696
                                        return -EOPNOTSUPP;
1697
                        }
1698
                        break;
1699
 
1700
                case SOL_SOCKET:
1701
                        return sock_setsockopt(sk,level,optname,optval,optlen);
1702
 
1703
                default:
1704
                        return -EOPNOTSUPP;
1705
        }
1706
}
1707
 
1708
static int ipx_getsockopt(struct socket *sock, int level, int optname,
1709
        char *optval, int *optlen)
1710
{
1711
        ipx_socket *sk;
1712
        int val=0;
1713
        int err;
1714
 
1715
        sk=(ipx_socket *)sock->data;
1716
 
1717
        switch(level)
1718
        {
1719
 
1720
                case SOL_IPX:
1721
                        switch(optname)
1722
                        {
1723
                                case IPX_TYPE:
1724
                                        val=sk->protinfo.af_ipx.type;
1725
                                        break;
1726
                                default:
1727
                                        return -ENOPROTOOPT;
1728
                        }
1729
                        break;
1730
 
1731
                case SOL_SOCKET:
1732
                        return sock_getsockopt(sk,level,optname,optval,optlen);
1733
 
1734
                default:
1735
                        return -EOPNOTSUPP;
1736
        }
1737
        err=verify_area(VERIFY_WRITE,optlen,sizeof(int));
1738
        if(err)
1739
                return err;
1740
        put_fs_long(sizeof(int),(unsigned long *)optlen);
1741
        err=verify_area(VERIFY_WRITE,optval,sizeof(int));
1742
        if (err) return err;
1743
        put_fs_long(val,(unsigned long *)optval);
1744
        return(0);
1745
}
1746
 
1747
static int ipx_listen(struct socket *sock, int backlog)
1748
{
1749
        return -EOPNOTSUPP;
1750
}
1751
 
1752
static void def_callback1(struct sock *sk)
1753
{
1754
        if(!sk->dead)
1755
                wake_up_interruptible(sk->sleep);
1756
}
1757
 
1758
static void def_callback2(struct sock *sk, int len)
1759
{
1760
        if(!sk->dead)
1761
        {
1762
                wake_up_interruptible(sk->sleep);
1763
                sock_wake_async(sk->socket, 1);
1764
        }
1765
}
1766
 
1767
static int ipx_create(struct socket *sock, int protocol)
1768
{
1769
        ipx_socket *sk;
1770
        sk=(ipx_socket *)sk_alloc(GFP_KERNEL);
1771
        if(sk==NULL)
1772
                return(-ENOMEM);
1773
        switch(sock->type)
1774
        {
1775
                case SOCK_DGRAM:
1776
                        break;
1777
                default:
1778
                        kfree_s((void *)sk,sizeof(*sk));
1779
                        return(-ESOCKTNOSUPPORT);
1780
        }
1781
        sk->rcvbuf=SK_RMEM_MAX;
1782
        sk->sndbuf=SK_WMEM_MAX;
1783
        sk->allocation=GFP_KERNEL;
1784
        sk->prot=NULL;  /* So we use default free mechanisms */
1785
        skb_queue_head_init(&sk->receive_queue);
1786
        skb_queue_head_init(&sk->write_queue);
1787
        sk->send_head=NULL;
1788
        skb_queue_head_init(&sk->back_log);
1789
        sk->state=TCP_CLOSE;
1790
        sk->socket=sock;
1791
        sk->type=sock->type;
1792
        sk->mtu=IPX_MTU;
1793
        sk->no_check = 1;               /* Checksum off by default */
1794
        if(sock!=NULL)
1795
        {
1796
                sock->data=(void *)sk;
1797
                sk->sleep=sock->wait;
1798
        }
1799
 
1800
        sk->state_change=def_callback1;
1801
        sk->data_ready=def_callback2;
1802
        sk->write_space=def_callback1;
1803
        sk->error_report=def_callback1;
1804
 
1805
        sk->zapped=1;
1806
        MOD_INC_USE_COUNT;
1807
        return 0;
1808
}
1809
 
1810
static int ipx_release(struct socket *sock, struct socket *peer)
1811
{
1812
        ipx_socket *sk=(ipx_socket *)sock->data;
1813
        if(sk==NULL)
1814
                return(0);
1815
        if(!sk->dead)
1816
                sk->state_change(sk);
1817
        sk->dead=1;
1818
        sock->data=NULL;
1819
        ipx_destroy_socket(sk);
1820
        return(0);
1821
}
1822
 
1823
static int ipx_dup(struct socket *newsock,struct socket *oldsock)
1824
{
1825
        return(ipx_create(newsock,SOCK_DGRAM));
1826
}
1827
 
1828
static unsigned short
1829
ipx_first_free_socketnum(ipx_interface *intrfc)
1830
{
1831
        unsigned short  socketNum = intrfc->if_sknum;
1832
 
1833
        if (socketNum < IPX_MIN_EPHEMERAL_SOCKET)
1834
                socketNum = IPX_MIN_EPHEMERAL_SOCKET;
1835
 
1836
        while (ipxitf_find_socket(intrfc, ntohs(socketNum)) != NULL)
1837
                if (socketNum > IPX_MAX_EPHEMERAL_SOCKET)
1838
                        socketNum = IPX_MIN_EPHEMERAL_SOCKET;
1839
                else
1840
                        socketNum++;
1841
 
1842
        intrfc->if_sknum = socketNum;
1843
        return  ntohs(socketNum);
1844
}
1845
 
1846
static int ipx_bind(struct socket *sock, struct sockaddr *uaddr,int addr_len)
1847
{
1848
        ipx_socket *sk;
1849
        ipx_interface *intrfc;
1850
        struct sockaddr_ipx *addr=(struct sockaddr_ipx *)uaddr;
1851
 
1852
        sk=(ipx_socket *)sock->data;
1853
 
1854
        if(sk->zapped==0)
1855
                return -EIO;
1856
 
1857
        if(addr_len!=sizeof(struct sockaddr_ipx))
1858
                return -EINVAL;
1859
 
1860
        intrfc = ipxitf_find_using_net(addr->sipx_network);
1861
        if (intrfc == NULL)
1862
                return -EADDRNOTAVAIL;
1863
 
1864
        if (addr->sipx_port == 0) {
1865
                addr->sipx_port = ipx_first_free_socketnum(intrfc);
1866
                if (addr->sipx_port == 0)
1867
                        return -EINVAL;
1868
        }
1869
 
1870
        if(ntohs(addr->sipx_port)<IPX_MIN_EPHEMERAL_SOCKET && !suser())
1871
                return -EPERM;  /* protect IPX system stuff like routing/sap */
1872
 
1873
        sk->protinfo.af_ipx.port=addr->sipx_port;
1874
 
1875
#ifdef CONFIG_IPX_INTERN
1876
        if (intrfc == ipx_internal_net)
1877
        {
1878
                /* The source address is to be set explicitly if the
1879
                 * socket is to be bound on the internal network. If a
1880
                 * node number 0 was specified, the default is used.
1881
                 */
1882
 
1883
                if (memcmp(addr->sipx_node, ipx_broadcast_node,
1884
                           IPX_NODE_LEN) == 0)
1885
                {
1886
                        return -EINVAL;
1887
                }
1888
                if (memcmp(addr->sipx_node, ipx_this_node, IPX_NODE_LEN) == 0)
1889
                {
1890
                        memcpy(sk->protinfo.af_ipx.node, intrfc->if_node,
1891
                               IPX_NODE_LEN);
1892
                }
1893
                else
1894
                {
1895
                        memcpy(sk->protinfo.af_ipx.node, addr->sipx_node, IPX_NODE_LEN);
1896
                }
1897
                if (ipxitf_find_internal_socket(intrfc,
1898
                        sk->protinfo.af_ipx.node,
1899
                        sk->protinfo.af_ipx.port) != NULL)
1900
                {
1901
                        if(sk->debug)
1902
                                printk("IPX: bind failed because port %X in"
1903
                                       " use.\n", (int)addr->sipx_port);
1904
                        return -EADDRINUSE;
1905
                }
1906
        }
1907
        else
1908
        {
1909
                /* Source addresses are easy. It must be our
1910
                 * network:node pair for an interface routed to IPX
1911
                 * with the ipx routing ioctl()
1912
                 */
1913
 
1914
                memcpy(sk->protinfo.af_ipx.node, intrfc->if_node,
1915
                        IPX_NODE_LEN);
1916
 
1917
                if(ipxitf_find_socket(intrfc, addr->sipx_port)!=NULL) {
1918
                        if(sk->debug)
1919
                                printk("IPX: bind failed because port %X in"
1920
                                       " use.\n", (int)addr->sipx_port);
1921
                        return -EADDRINUSE;
1922
                }
1923
        }
1924
 
1925
#else
1926
 
1927
        /* Source addresses are easy. It must be our network:node pair for
1928
           an interface routed to IPX with the ipx routing ioctl() */
1929
 
1930
        if(ipxitf_find_socket(intrfc, addr->sipx_port)!=NULL) {
1931
                if(sk->debug)
1932
                        printk("IPX: bind failed because port %X in use.\n",
1933
                                (int)addr->sipx_port);
1934
                return -EADDRINUSE;
1935
        }
1936
 
1937
#endif
1938
 
1939
        ipxitf_insert_socket(intrfc, sk);
1940
        sk->zapped=0;
1941
        if(sk->debug)
1942
                printk("IPX: socket is bound.\n");
1943
        return 0;
1944
}
1945
 
1946
static int ipx_connect(struct socket *sock, struct sockaddr *uaddr,
1947
        int addr_len, int flags)
1948
{
1949
        ipx_socket *sk=(ipx_socket *)sock->data;
1950
        struct sockaddr_ipx *addr;
1951
 
1952
        sk->state = TCP_CLOSE;
1953
        sock->state = SS_UNCONNECTED;
1954
 
1955
        if(addr_len!=sizeof(*addr))
1956
                return(-EINVAL);
1957
        addr=(struct sockaddr_ipx *)uaddr;
1958
 
1959
        if(sk->protinfo.af_ipx.port==0)
1960
        /* put the autobinding in */
1961
        {
1962
                struct sockaddr_ipx uaddr;
1963
                int ret;
1964
 
1965
                uaddr.sipx_port = 0;
1966
                uaddr.sipx_network = 0L;
1967
#ifdef CONFIG_IPX_INTERN
1968
                memcpy(uaddr.sipx_node, sk->protinfo.af_ipx.intrfc->if_node,
1969
                       IPX_NODE_LEN);
1970
#endif
1971
                ret = ipx_bind (sock, (struct sockaddr *)&uaddr,
1972
                                sizeof(struct sockaddr_ipx));
1973
                if (ret != 0) return (ret);
1974
        }
1975
 
1976
        if(ipxrtr_lookup(addr->sipx_network)==NULL)
1977
                return -ENETUNREACH;
1978
        sk->protinfo.af_ipx.dest_addr.net=addr->sipx_network;
1979
        sk->protinfo.af_ipx.dest_addr.sock=addr->sipx_port;
1980
        memcpy(sk->protinfo.af_ipx.dest_addr.node,
1981
                addr->sipx_node,IPX_NODE_LEN);
1982
        sk->protinfo.af_ipx.type=addr->sipx_type;
1983
        sock->state = SS_CONNECTED;
1984
        sk->state=TCP_ESTABLISHED;
1985
        return 0;
1986
}
1987
 
1988
static int ipx_socketpair(struct socket *sock1, struct socket *sock2)
1989
{
1990
        return(-EOPNOTSUPP);
1991
}
1992
 
1993
static int ipx_accept(struct socket *sock, struct socket *newsock, int flags)
1994
{
1995
        if(newsock->data) {
1996
                kfree_s(newsock->data,sizeof(ipx_socket));
1997
                MOD_DEC_USE_COUNT;
1998
        }
1999
        return -EOPNOTSUPP;
2000
}
2001
 
2002
static int ipx_getname(struct socket *sock, struct sockaddr *uaddr,
2003
        int *uaddr_len, int peer)
2004
{
2005
        ipx_address *addr;
2006
        struct sockaddr_ipx sipx;
2007
        ipx_socket *sk;
2008
 
2009
        sk=(ipx_socket *)sock->data;
2010
 
2011
        *uaddr_len = sizeof(struct sockaddr_ipx);
2012
 
2013
        if(peer) {
2014
                if(sk->state!=TCP_ESTABLISHED)
2015
                        return -ENOTCONN;
2016
                addr=&sk->protinfo.af_ipx.dest_addr;
2017
                sipx.sipx_network = addr->net;
2018
                memcpy(sipx.sipx_node,addr->node,IPX_NODE_LEN);
2019
                sipx.sipx_port = addr->sock;
2020
        } else {
2021
                if (sk->protinfo.af_ipx.intrfc != NULL) {
2022
                        sipx.sipx_network = sk->protinfo.af_ipx.intrfc->if_netnum;
2023
#ifdef CONFIG_IPX_INTERN
2024
                        memcpy(sipx.sipx_node, sk->protinfo.af_ipx.node, IPX_NODE_LEN);
2025
#else
2026
                        memcpy(sipx.sipx_node,
2027
                                sk->protinfo.af_ipx.intrfc->if_node, IPX_NODE_LEN);
2028
#endif
2029
 
2030
                } else {
2031
                        sipx.sipx_network = 0L;
2032
                        memset(sipx.sipx_node, '\0', IPX_NODE_LEN);
2033
                }
2034
                sipx.sipx_port = sk->protinfo.af_ipx.port;
2035
        }
2036
 
2037
        sipx.sipx_family = AF_IPX;
2038
        sipx.sipx_type = sk->protinfo.af_ipx.type;
2039
        memcpy(uaddr,&sipx,sizeof(sipx));
2040
        return 0;
2041
}
2042
 
2043
#if 0
2044
/*
2045
 * User to dump IPX packets (debugging)
2046
 */
2047
void dump_data(char *str,unsigned char *d, int len) {
2048
  static char h2c[] = "0123456789ABCDEF";
2049
  int l,i;
2050
  char *p, b[64];
2051
  for (l=0;len > 0 && l<16;l++) {
2052
    p = b;
2053
    for (i=0; i < 8 ; i++, --len) {
2054
          if (len > 0) {
2055
              *(p++) = h2c[(d[i] >> 4) & 0x0f];
2056
              *(p++) = h2c[d[i] & 0x0f];
2057
          }
2058
          else {
2059
              *(p++) = ' ';
2060
              *(p++) = ' ';
2061
          }
2062
      *(p++) = ' ';
2063
    }
2064
    *(p++) = '-';
2065
    *(p++) = ' ';
2066
        len += 8;
2067
    for (i=0; i < 8 ; i++, --len)
2068
                if (len > 0)
2069
                        *(p++) = ' '<= d[i] && d[i]<'\177' ? d[i] : '.';
2070
                else
2071
                        *(p++) = ' ';
2072
    *p = '\000';
2073
    d += i;
2074
    printk("%s-%04X: %s\n",str,l*8,b);
2075
  }
2076
}
2077
 
2078
void dump_addr(char *str,ipx_address *p) {
2079
  printk("%s: %08X:%02X%02X%02X%02X%02X%02X:%04X\n",
2080
   str,ntohl(p->net),p->node[0],p->node[1],p->node[2],
2081
   p->node[3],p->node[4],p->node[5],ntohs(p->sock));
2082
}
2083
 
2084
void dump_hdr(char *str,ipx_packet *p) {
2085
  printk("%s: CHKSUM=%04X SIZE=%d (%04X) HOPS=%d (%02X) TYPE=%02X\n",
2086
   str,p->ipx_checksum,ntohs(p->ipx_pktsize),ntohs(p->ipx_pktsize),
2087
   p->ipx_tctrl,p->ipx_tctrl,p->ipx_type);
2088
  dump_addr("  IPX-DST",&p->ipx_dest);
2089
  dump_addr("  IPX-SRC",&p->ipx_source);
2090
}
2091
 
2092
void dump_pkt(char *str,ipx_packet *p) {
2093
  int len = ntohs(p->ipx_pktsize);
2094
  dump_hdr(str,p);
2095
  if (len > 30)
2096
          dump_data(str,(unsigned char *)p + 30, len - 30);
2097
}
2098
#endif
2099
 
2100
int ipx_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
2101
{
2102
        /* NULL here for pt means the packet was looped back */
2103
        ipx_interface   *intrfc;
2104
        ipx_packet *ipx;
2105
 
2106
 
2107
        ipx=(ipx_packet *)skb->h.raw;
2108
 
2109
        /* Too small */
2110
 
2111
        if(ntohs(ipx->ipx_pktsize)<sizeof(ipx_packet)) {
2112
                kfree_skb(skb,FREE_READ);
2113
                return 0;
2114
        }
2115
 
2116
        if(ipx->ipx_checksum!=IPX_NO_CHECKSUM)
2117
        {
2118
                if(ipx_set_checksum(ipx, ntohs(ipx->ipx_pktsize))!=ipx->ipx_checksum)
2119
                {
2120
                        kfree_skb(skb,FREE_READ);
2121
                        return 0;
2122
                }
2123
        }
2124
 
2125
        /* Determine what local ipx endpoint this is */
2126
        intrfc = ipxitf_find_using_phys(dev, pt->type);
2127
        if (intrfc == NULL)
2128
        {
2129
                if (ipxcfg_auto_create_interfaces &&
2130
                    ntohl(ipx->ipx_dest.net)!=0L)
2131
                {
2132
                        intrfc = ipxitf_auto_create(dev, pt->type);
2133
                }
2134
 
2135
                if (intrfc == NULL) {
2136
                        /* Not one of ours */
2137
                        kfree_skb(skb,FREE_READ);
2138
                        return 0;
2139
                }
2140
        }
2141
 
2142
        return ipxitf_rcv(intrfc, skb);
2143
}
2144
 
2145
static int ipx_sendmsg(struct socket *sock, struct msghdr *msg, int len, int noblock,
2146
        int flags)
2147
{
2148
        ipx_socket *sk=(ipx_socket *)sock->data;
2149
        struct sockaddr_ipx *usipx=(struct sockaddr_ipx *)msg->msg_name;
2150
        struct sockaddr_ipx local_sipx;
2151
        int retval;
2152
 
2153
        if (sk->zapped)
2154
                return -EIO; /* Socket not bound */
2155
        if(flags)
2156
                return -EINVAL;
2157
 
2158
        if(usipx)
2159
        {
2160
                if(sk->protinfo.af_ipx.port == 0)
2161
                {
2162
                        struct sockaddr_ipx uaddr;
2163
                        int ret;
2164
 
2165
                        uaddr.sipx_port = 0;
2166
                        uaddr.sipx_network = 0L;
2167
#ifdef CONFIG_IPX_INTERN
2168
                        memcpy(uaddr.sipx_node, sk->protinfo.af_ipx.intrfc
2169
                                ->if_node, IPX_NODE_LEN);
2170
#endif
2171
                        ret = ipx_bind (sock, (struct sockaddr *)&uaddr,
2172
                                        sizeof(struct sockaddr_ipx));
2173
                        if (ret != 0) return ret;
2174
                }
2175
 
2176
                if(msg->msg_namelen <sizeof(*usipx))
2177
                        return -EINVAL;
2178
                if(usipx->sipx_family != AF_IPX)
2179
                        return -EINVAL;
2180
        }
2181
        else
2182
        {
2183
                if(sk->state!=TCP_ESTABLISHED)
2184
                        return -ENOTCONN;
2185
                usipx=&local_sipx;
2186
                usipx->sipx_family=AF_IPX;
2187
                usipx->sipx_type=sk->protinfo.af_ipx.type;
2188
                usipx->sipx_port=sk->protinfo.af_ipx.dest_addr.sock;
2189
                usipx->sipx_network=sk->protinfo.af_ipx.dest_addr.net;
2190
                memcpy(usipx->sipx_node,sk->protinfo.af_ipx.dest_addr.node,IPX_NODE_LEN);
2191
        }
2192
 
2193
        retval = ipxrtr_route_packet(sk, usipx, msg->msg_iov, len, noblock);
2194
        if (retval < 0)
2195
                return retval;
2196
 
2197
        return len;
2198
}
2199
 
2200
 
2201
static int ipx_recvmsg(struct socket *sock, struct msghdr *msg, int size, int noblock,
2202
                 int flags, int *addr_len)
2203
{
2204
        ipx_socket *sk=(ipx_socket *)sock->data;
2205
        struct sockaddr_ipx *sipx=(struct sockaddr_ipx *)msg->msg_name;
2206
        struct ipx_packet *ipx = NULL;
2207
        int copied = 0;
2208
        int truesize;
2209
        struct sk_buff *skb;
2210
        int er;
2211
 
2212
        if(sk->err)
2213
                return sock_error(sk);
2214
 
2215
        if (sk->zapped)
2216
                return -EIO;
2217
 
2218
 
2219
        skb=skb_recv_datagram(sk,flags,noblock,&er);
2220
        if(skb==NULL)
2221
                return er;
2222
 
2223
        if(addr_len)
2224
                *addr_len=sizeof(*sipx);
2225
 
2226
        ipx = (ipx_packet *)(skb->h.raw);
2227
        truesize=ntohs(ipx->ipx_pktsize) - sizeof(ipx_packet);
2228
        copied = (truesize > size) ? size : truesize;
2229
        skb_copy_datagram_iovec(skb,sizeof(struct ipx_packet),msg->msg_iov,copied);
2230
 
2231
        if(sipx)
2232
        {
2233
                sipx->sipx_family=AF_IPX;
2234
                sipx->sipx_port=ipx->ipx_source.sock;
2235
                memcpy(sipx->sipx_node,ipx->ipx_source.node,IPX_NODE_LEN);
2236
                sipx->sipx_network=ipx->ipx_source.net;
2237
                sipx->sipx_type = ipx->ipx_type;
2238
        }
2239
        skb_free_datagram(sk, skb);
2240
        return(truesize);
2241
}
2242
 
2243
static int ipx_shutdown(struct socket *sk,int how)
2244
{
2245
        return -EOPNOTSUPP;
2246
}
2247
 
2248
static int ipx_select(struct socket *sock , int sel_type, select_table *wait)
2249
{
2250
        ipx_socket *sk=(ipx_socket *)sock->data;
2251
 
2252
        return datagram_select(sk,sel_type,wait);
2253
}
2254
 
2255
static int ipx_ioctl(struct socket *sock,unsigned int cmd, unsigned long arg)
2256
{
2257
        int err;
2258
        long amount=0;
2259
        ipx_socket *sk=(ipx_socket *)sock->data;
2260
 
2261
        switch(cmd)
2262
        {
2263
                case TIOCOUTQ:
2264
                        err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(unsigned long));
2265
                        if(err)
2266
                                return err;
2267
                        amount=sk->sndbuf-sk->wmem_alloc;
2268
                        if(amount<0)
2269
                                amount=0;
2270
                        put_fs_long(amount,(unsigned long *)arg);
2271
                        return 0;
2272
                case TIOCINQ:
2273
                {
2274
                        struct sk_buff *skb;
2275
                        /* These two are safe on a single CPU system as only user tasks fiddle here */
2276
                        if((skb=skb_peek(&sk->receive_queue))!=NULL)
2277
                                amount=skb->len-sizeof(struct ipx_packet);
2278
                        err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(unsigned long));
2279
                        if(err)
2280
                                return err;
2281
                        put_fs_long(amount,(unsigned long *)arg);
2282
                        return 0;
2283
                }
2284
                case SIOCADDRT:
2285
                case SIOCDELRT:
2286
                        if(!suser())
2287
                                return -EPERM;
2288
                        return(ipxrtr_ioctl(cmd,(void *)arg));
2289
                case SIOCSIFADDR:
2290
                case SIOCAIPXITFCRT:
2291
                case SIOCAIPXPRISLT:
2292
                        if(!suser())
2293
                                return -EPERM;
2294
                case SIOCGIFADDR:
2295
                        return(ipxitf_ioctl(cmd,(void *)arg));
2296
                case SIOCIPXCFGDATA:
2297
                {
2298
                        err=verify_area(VERIFY_WRITE,(void *)arg,
2299
                                sizeof(ipx_config_data));
2300
                        if(err) return err;
2301
                        return(ipxcfg_get_config_data((void *)arg));
2302
                }
2303
 
2304
                case SIOCIPXNCPCONN:
2305
                {
2306
                  /*
2307
                   * This socket wants to take care of the NCP connection
2308
                   * handed to us in arg.
2309
                   */
2310
                  if (!suser()) return(-EPERM);
2311
                  err = verify_area(VERIFY_READ, (void *)arg,
2312
                                sizeof(unsigned short));
2313
                  if (err) return err;
2314
                  sk->protinfo.af_ipx.ipx_ncp_conn = get_fs_word(arg);
2315
                  return 0;
2316
                }
2317
 
2318
                case SIOCGSTAMP:
2319
                        if (sk)
2320
                        {
2321
                                if(sk->stamp.tv_sec==0)
2322
                                        return -ENOENT;
2323
                                err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval));
2324
                                if(err)
2325
                                        return err;
2326
                                memcpy_tofs((void *)arg,&sk->stamp,sizeof(struct timeval));
2327
                                return 0;
2328
                        }
2329
                        return -EINVAL;
2330
                case SIOCGIFDSTADDR:
2331
                case SIOCSIFDSTADDR:
2332
                case SIOCGIFBRDADDR:
2333
                case SIOCSIFBRDADDR:
2334
                case SIOCGIFNETMASK:
2335
                case SIOCSIFNETMASK:
2336
                        return -EINVAL;
2337
                default:
2338
                        return(dev_ioctl(cmd,(void *) arg));
2339
        }
2340
        /*NOTREACHED*/
2341
        return(0);
2342
}
2343
 
2344
static struct proto_ops ipx_proto_ops = {
2345
        AF_IPX,
2346
 
2347
        ipx_create,
2348
        ipx_dup,
2349
        ipx_release,
2350
        ipx_bind,
2351
        ipx_connect,
2352
        ipx_socketpair,
2353
        ipx_accept,
2354
        ipx_getname,
2355
        ipx_select,
2356
        ipx_ioctl,
2357
        ipx_listen,
2358
        ipx_shutdown,
2359
        ipx_setsockopt,
2360
        ipx_getsockopt,
2361
        ipx_fcntl,
2362
        ipx_sendmsg,
2363
        ipx_recvmsg
2364
};
2365
 
2366
/* Called by protocol.c on kernel start up */
2367
 
2368
static struct packet_type ipx_8023_packet_type =
2369
 
2370
{
2371
        0,       /* MUTTER ntohs(ETH_P_8023),*/
2372
        NULL,           /* All devices */
2373
        ipx_rcv,
2374
        NULL,
2375
        NULL,
2376
};
2377
 
2378
static struct packet_type ipx_dix_packet_type =
2379
{
2380
        0,       /* MUTTER ntohs(ETH_P_IPX),*/
2381
        NULL,           /* All devices */
2382
        ipx_rcv,
2383
        NULL,
2384
        NULL,
2385
};
2386
 
2387
static struct notifier_block ipx_dev_notifier={
2388
        ipxitf_device_event,
2389
        NULL,
2390
 
2391
};
2392
 
2393
 
2394
extern struct datalink_proto    *make_EII_client(void);
2395
extern struct datalink_proto    *make_8023_client(void);
2396
extern void     destroy_EII_client(struct datalink_proto *);
2397
extern void     destroy_8023_client(struct datalink_proto *);
2398
 
2399
#ifdef CONFIG_PROC_FS
2400
struct proc_dir_entry ipx_procinfo = {
2401
        PROC_NET_IPX, 3, "ipx", S_IFREG | S_IRUGO,
2402
        1, 0, 0, 0, &proc_net_inode_operations, ipx_get_info
2403
};
2404
 
2405
struct proc_dir_entry ipx_if_procinfo = {
2406
        PROC_NET_IPX_INTERFACE, 13, "ipx_interface", S_IFREG | S_IRUGO,
2407
        1, 0, 0, 0, &proc_net_inode_operations, ipx_interface_get_info
2408
};
2409
 
2410
struct proc_dir_entry ipx_rt_procinfo = {
2411
        PROC_NET_IPX_ROUTE, 9, "ipx_route", S_IFREG | S_IRUGO,
2412
        1, 0, 0, 0, &proc_net_inode_operations, ipx_rt_get_info
2413
};
2414
#endif
2415
 
2416
static unsigned char    ipx_8022_type = 0xE0;
2417
static unsigned char    ipx_snap_id[5] =  { 0x0, 0x0, 0x0, 0x81, 0x37 };
2418
 
2419
void
2420
ipx_proto_init(struct net_proto *pro)
2421
{
2422
        (void) sock_register(ipx_proto_ops.family, &ipx_proto_ops);
2423
 
2424
        pEII_datalink = make_EII_client();
2425
        ipx_dix_packet_type.type=htons(ETH_P_IPX);
2426
        dev_add_pack(&ipx_dix_packet_type);
2427
 
2428
        p8023_datalink = make_8023_client();
2429
        ipx_8023_packet_type.type=htons(ETH_P_802_3);
2430
        dev_add_pack(&ipx_8023_packet_type);
2431
 
2432
        if ((p8022_datalink = register_8022_client(ipx_8022_type, ipx_rcv)) == NULL)
2433
                printk(KERN_CRIT "IPX: Unable to register with 802.2\n");
2434
 
2435
        if ((p8022tr_datalink = register_8022tr_client(ipx_8022_type, ipx_rcv)) == NULL)
2436
                printk(KERN_CRIT "IPX: Unable to register with 802.2TR\n");
2437
 
2438
        if ((pSNAP_datalink = register_snap_client(ipx_snap_id, ipx_rcv)) == NULL)
2439
                printk(KERN_CRIT "IPX: Unable to register with SNAP\n");
2440
 
2441
        register_netdevice_notifier(&ipx_dev_notifier);
2442
#ifdef CONFIG_PROC_FS
2443
        proc_net_register(&ipx_procinfo);
2444
        proc_net_register(&ipx_if_procinfo);
2445
        proc_net_register(&ipx_rt_procinfo);
2446
#endif  
2447
 
2448
        printk(KERN_INFO "Swansea University Computer Society IPX 0.34 for NET3.035\n");
2449
        printk(KERN_INFO "IPX Portions Copyright (c) 1995 Caldera, Inc.\n");
2450
}
2451
 
2452
#ifdef MODULE
2453
/* Note on MOD_{INC,DEC}_USE_COUNT:
2454
 *
2455
 * Use counts are incremented/decremented when
2456
 * sockets are created/deleted.
2457
 *
2458
 * Routes are always associated with an interface, and
2459
 * allocs/frees will remain properly accounted for by
2460
 * their associated interfaces.
2461
 *
2462
 * Ergo, before the ipx module can be removed, all IPX
2463
 * sockets be closed from user space.
2464
 */
2465
 
2466
static void
2467
ipx_proto_finito(void)
2468
{       ipx_interface   *ifc;
2469
 
2470
        while (ipx_interfaces) {
2471
                ifc = ipx_interfaces;
2472
                ipx_interfaces = ifc->if_next;
2473
                ifc->if_next = NULL;
2474
                ipxitf_down(ifc);
2475
        }
2476
 
2477
#ifdef CONFIG_PROC_FS
2478
        proc_net_unregister(PROC_NET_IPX_ROUTE);
2479
        proc_net_unregister(PROC_NET_IPX_INTERFACE);
2480
        proc_net_unregister(PROC_NET_IPX);
2481
#endif  
2482
 
2483
        unregister_netdevice_notifier(&ipx_dev_notifier);
2484
 
2485
        unregister_snap_client(ipx_snap_id);
2486
        pSNAP_datalink = NULL;
2487
 
2488
        unregister_8022tr_client(ipx_8022_type);
2489
        p8022tr_datalink = NULL;
2490
 
2491
        unregister_8022_client(ipx_8022_type);
2492
        p8022_datalink = NULL;
2493
 
2494
        dev_remove_pack(&ipx_8023_packet_type);
2495
        destroy_8023_client(p8023_datalink);
2496
        p8023_datalink = NULL;
2497
 
2498
        dev_remove_pack(&ipx_dix_packet_type);
2499
        destroy_EII_client(pEII_datalink);
2500
        pEII_datalink = NULL;
2501
 
2502
        (void) sock_unregister(ipx_proto_ops.family);
2503
 
2504
        return;
2505
}
2506
 
2507
int init_module(void)
2508
{
2509
        ipx_proto_init(NULL);
2510
        register_symtab(0);
2511
        return 0;
2512
}
2513
 
2514
void cleanup_module(void)
2515
{
2516
        ipx_proto_finito();
2517
        return;
2518
}
2519
#endif /* def MODULE */

powered by: WebSVN 2.1.0

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