root/net/ipx/af_ipx.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ipxcfg_set_auto_create
  2. ipxcfg_set_auto_select
  3. ipxcfg_get_config_data
  4. ipx_remove_socket
  5. ipx_destroy_socket
  6. ipxitf_clear_primary_net
  7. ipxitf_find_using_phys
  8. ipxitf_find_using_net
  9. ipxitf_insert_socket
  10. ipxitf_find_socket
  11. ipxitf_down
  12. ipxitf_device_event
  13. ipxitf_def_skb_handler
  14. ipxitf_demux_socket
  15. ipxitf_adjust_skbuff
  16. ipxitf_send
  17. ipxitf_add_local_route
  18. ipxitf_rcv
  19. ipxitf_insert
  20. ipxitf_create_internal
  21. ipx_map_frame_type
  22. ipxitf_create
  23. ipxitf_delete
  24. ipxitf_auto_create
  25. ipxitf_ioctl
  26. ipxrtr_lookup
  27. ipxrtr_add_route
  28. ipxrtr_del_routes
  29. ipxrtr_create
  30. ipxrtr_delete
  31. ipxrtr_route_packet
  32. ipxrtr_route_skb
  33. ipxrtr_ioctl
  34. ipx_frame_name
  35. ipx_device_name
  36. ipx_interface_get_info
  37. ipx_get_info
  38. ipx_rt_get_info
  39. ipx_fcntl
  40. ipx_setsockopt
  41. ipx_getsockopt
  42. ipx_listen
  43. def_callback1
  44. def_callback2
  45. ipx_create
  46. ipx_release
  47. ipx_dup
  48. ipx_first_free_socketnum
  49. ipx_bind
  50. ipx_connect
  51. ipx_socketpair
  52. ipx_accept
  53. ipx_getname
  54. dump_data
  55. dump_addr
  56. dump_hdr
  57. dump_pkt
  58. ipx_rcv
  59. ipx_sendmsg
  60. ipx_recvmsg
  61. ipx_shutdown
  62. ipx_select
  63. ipx_ioctl
  64. ipx_proto_init

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

/* [previous][next][first][last][top][bottom][index][help] */