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