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

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