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_family=AF_IPX;
1036                         sipx->sipx_network=ipxif->if_netnum;
1037                         memcpy(sipx->sipx_node, ipxif->if_node, sizeof(sipx->sipx_node));
1038                         memcpy_tofs(arg,&ifr,sizeof(ifr));
1039                         return 0;
1040                 }
1041                 case SIOCAIPXITFCRT:
1042                         err=verify_area(VERIFY_READ,arg,sizeof(char));
1043                         if(err)
1044                                 return err;
1045                         return ipxcfg_set_auto_create(get_fs_byte(arg));
1046                 case SIOCAIPXPRISLT:
1047                         err=verify_area(VERIFY_READ,arg,sizeof(char));
1048                         if(err)
1049                                 return err;
1050                         return ipxcfg_set_auto_select(get_fs_byte(arg));
1051                 default:
1052                         return -EINVAL;
1053         }
1054 }
1055 
1056 /*******************************************************************************************************************\
1057 *                                                                                                                   *
1058 *                                       Routing tables for the IPX socket layer                                     *
1059 *                                                                                                                   *
1060 \*******************************************************************************************************************/
1061 
1062 static ipx_route *
1063 ipxrtr_lookup(unsigned long net)
     /* [previous][next][first][last][top][bottom][index][help] */
1064 {
1065         ipx_route *r;
1066 
1067         for (r=ipx_routes; (r!=NULL) && (r->ir_net!=net); r=r->ir_next)
1068                 ;
1069 
1070         return r;
1071 }
1072 
1073 static int
1074 ipxrtr_add_route(unsigned long network, ipx_interface *intrfc, unsigned char *node)
     /* [previous][next][first][last][top][bottom][index][help] */
1075 {
1076         ipx_route       *rt;
1077 
1078         /* Get a route structure; either existing or create */
1079         rt = ipxrtr_lookup(network);
1080         if (rt==NULL) {
1081                 rt=(ipx_route *)kmalloc(sizeof(ipx_route),GFP_ATOMIC);
1082                 if(rt==NULL)
1083                         return -EAGAIN;
1084                 rt->ir_next=ipx_routes;
1085                 ipx_routes=rt;
1086         }
1087         else if (intrfc == ipx_internal_net)
1088                 return(-EINVAL);
1089 
1090         rt->ir_net = network;
1091         rt->ir_intrfc = intrfc;
1092         if (node == NULL) {
1093                 memset(rt->ir_router_node, '\0', IPX_NODE_LEN);
1094                 rt->ir_routed = 0;
1095         } else {
1096                 memcpy(rt->ir_router_node, node, IPX_NODE_LEN);
1097                 rt->ir_routed=1;
1098         }
1099         return 0;
1100 }
1101 
1102 static void
1103 ipxrtr_del_routes(ipx_interface *intrfc)
     /* [previous][next][first][last][top][bottom][index][help] */
1104 {
1105         ipx_route       **r, *tmp;
1106 
1107         for (r = &ipx_routes; (tmp = *r) != NULL; ) {
1108                 if (tmp->ir_intrfc == intrfc) {
1109                         *r = tmp->ir_next;
1110                         kfree_s(tmp, sizeof(ipx_route));
1111                 } else {
1112                         r = &(tmp->ir_next);
1113                 }
1114         }
1115 }
1116 
1117 static int 
1118 ipxrtr_create(ipx_route_definition *rd)
     /* [previous][next][first][last][top][bottom][index][help] */
1119 {
1120         ipx_interface *intrfc;
1121 
1122         /* Find the appropriate interface */
1123         intrfc = ipxitf_find_using_net(rd->ipx_router_network);
1124         if (intrfc == NULL)
1125                 return -ENETUNREACH;
1126 
1127         return ipxrtr_add_route(rd->ipx_network, intrfc, rd->ipx_router_node);
1128 }
1129 
1130 
1131 static int 
1132 ipxrtr_delete(long net)
     /* [previous][next][first][last][top][bottom][index][help] */
1133 {
1134         ipx_route       **r;
1135         ipx_route       *tmp;
1136 
1137         for (r = &ipx_routes; (tmp = *r) != NULL; ) {
1138                 if (tmp->ir_net == net) {
1139                         if (!(tmp->ir_routed)) {
1140                                 /* Directly connected; can't lose route */
1141                                 return -EPERM;
1142                         }
1143                         *r = tmp->ir_next;
1144                         kfree_s(tmp, sizeof(ipx_route));
1145                         return 0;
1146                 } 
1147                 r = &(tmp->ir_next);
1148         }
1149 
1150         return -ENOENT;
1151 }
1152 
1153 /*
1154  *      Route an outgoing frame from a socket.
1155  */
1156  
1157 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] */
1158 {
1159         struct sk_buff *skb;
1160         ipx_interface *intrfc;
1161         ipx_packet *ipx;
1162         int size;
1163         int ipx_offset;
1164         ipx_route *rt = NULL;
1165         int err;
1166         
1167         /* Find the appropriate interface on which to send packet */
1168         if ((usipx->sipx_network == 0L) && (ipx_primary_net != NULL)) 
1169         {
1170                 usipx->sipx_network = ipx_primary_net->if_netnum;
1171                 intrfc = ipx_primary_net;
1172         } 
1173         else 
1174         {
1175                 rt = ipxrtr_lookup(usipx->sipx_network);
1176                 if (rt==NULL) {
1177                         return -ENETUNREACH;
1178                 }
1179                 intrfc = rt->ir_intrfc;
1180         }
1181         
1182         ipx_offset = intrfc->if_ipx_offset;
1183         size=sizeof(ipx_packet)+len;
1184         size += ipx_offset;
1185 
1186         skb=sock_alloc_send_skb(sk, size, 0, 0, &err);
1187         if(skb==NULL)
1188                 return err;
1189 
1190         skb_reserve(skb,ipx_offset);
1191         skb->free=1;
1192         skb->arp=1;
1193         skb->sk=sk;
1194 
1195         /* Fill in IPX header */
1196         ipx=(ipx_packet *)skb_put(skb,sizeof(ipx_packet));
1197         ipx->ipx_checksum=0xFFFF;
1198         ipx->ipx_pktsize=htons(len+sizeof(ipx_packet));
1199         ipx->ipx_tctrl=0;
1200         ipx->ipx_type=usipx->sipx_type;
1201         skb->h.raw = (unsigned char *)ipx;
1202 
1203         ipx->ipx_source.net = sk->ipx_intrfc->if_netnum;
1204 #ifdef CONFIG_IPX_INTERN
1205         memcpy(ipx->ipx_source.node, sk->ipx_node, IPX_NODE_LEN);
1206 #else
1207         if ((err = ntohs(sk->ipx_port)) == 0x453 || err == 0x452)  
1208         {
1209                 /* RIP/SAP special handling for mars_nwe */
1210                 ipx->ipx_source.net = intrfc->if_netnum;
1211                 memcpy(ipx->ipx_source.node, intrfc->if_node, IPX_NODE_LEN);
1212         }
1213         else
1214         {
1215                 ipx->ipx_source.net = sk->ipx_intrfc->if_netnum;
1216                 memcpy(ipx->ipx_source.node, sk->ipx_intrfc->if_node, IPX_NODE_LEN);
1217         }
1218 #endif
1219         ipx->ipx_source.sock = sk->ipx_port;
1220         ipx->ipx_dest.net=usipx->sipx_network;
1221         memcpy(ipx->ipx_dest.node,usipx->sipx_node,IPX_NODE_LEN);
1222         ipx->ipx_dest.sock=usipx->sipx_port;
1223 
1224         memcpy_fromiovec(skb_put(skb,len),iov,len);
1225 
1226 #ifdef CONFIG_FIREWALL  
1227         if(call_out_firewall(PF_IPX, skb, ipx)!=FW_ACCEPT)
1228         {
1229                 kfree_skb(skb, FREE_WRITE);
1230                 return -EPERM;
1231         }
1232 #endif
1233         
1234         return ipxitf_send(intrfc, skb, (rt && rt->ir_routed) ? 
1235                                 rt->ir_router_node : ipx->ipx_dest.node);
1236 }
1237         
1238 static int
1239 ipxrtr_route_skb(struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
1240 {
1241         ipx_packet      *ipx = (ipx_packet *) (skb->h.raw);
1242         ipx_route       *r;
1243         ipx_interface   *i;
1244 
1245         r = ipxrtr_lookup(ipx->ipx_dest.net);
1246         if (r == NULL) {
1247                 /* no known route */
1248                 kfree_skb(skb,FREE_READ);
1249                 return 0;
1250         }
1251         i = r->ir_intrfc;
1252         (void)ipxitf_send(i, skb, (r->ir_routed) ? 
1253                         r->ir_router_node : ipx->ipx_dest.node);
1254         return 0;
1255 }
1256 
1257 /*
1258  *      We use a normal struct rtentry for route handling
1259  */
1260  
1261 static int ipxrtr_ioctl(unsigned int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1262 {
1263         int err;
1264         struct rtentry rt;      /* Use these to behave like 'other' stacks */
1265         struct sockaddr_ipx *sg,*st;
1266 
1267         err=verify_area(VERIFY_READ,arg,sizeof(rt));
1268         if(err)
1269                 return err;
1270                 
1271         memcpy_fromfs(&rt,arg,sizeof(rt));
1272         
1273         sg=(struct sockaddr_ipx *)&rt.rt_gateway;
1274         st=(struct sockaddr_ipx *)&rt.rt_dst;
1275         
1276         if(!(rt.rt_flags&RTF_GATEWAY))
1277                 return -EINVAL;         /* Direct routes are fixed */
1278         if(sg->sipx_family!=AF_IPX)
1279                 return -EINVAL;
1280         if(st->sipx_family!=AF_IPX)
1281                 return -EINVAL;
1282                 
1283         switch(cmd)
1284         {
1285                 case SIOCDELRT:
1286                         return ipxrtr_delete(st->sipx_network);
1287                 case SIOCADDRT:
1288                 {
1289                         struct ipx_route_definition f;
1290                         f.ipx_network=st->sipx_network;
1291                         f.ipx_router_network=sg->sipx_network;
1292                         memcpy(f.ipx_router_node, sg->sipx_node, IPX_NODE_LEN);
1293                         return ipxrtr_create(&f);
1294                 }
1295                 default:
1296                         return -EINVAL;
1297         }
1298 }
1299 
1300 static const char *
1301 ipx_frame_name(unsigned short frame)
     /* [previous][next][first][last][top][bottom][index][help] */
1302 {
1303         switch (ntohs(frame)) {
1304         case ETH_P_IPX: return "EtherII";
1305         case ETH_P_802_2: return "802.2";
1306         case ETH_P_SNAP: return "SNAP";
1307         case ETH_P_802_3: return "802.3";
1308         default: return "None";
1309         }
1310 }
1311 
1312 static const char *
1313 ipx_device_name(ipx_interface *intrfc)
     /* [previous][next][first][last][top][bottom][index][help] */
1314 {
1315         return (intrfc->if_internal ? "Internal" :
1316                 (intrfc->if_dev ? intrfc->if_dev->name : "Unknown"));
1317 }
1318 
1319 /* Called from proc fs */
1320 static int ipx_interface_get_info(char *buffer, char **start, off_t offset,
     /* [previous][next][first][last][top][bottom][index][help] */
1321                                   int length, int dummy)
1322 {
1323         ipx_interface *i;
1324         int len=0;
1325         off_t pos=0;
1326         off_t begin=0;
1327 
1328         /* Theory.. Keep printing in the same place until we pass offset */
1329 
1330         len += sprintf (buffer,"%-11s%-15s%-9s%-11s%s\n", "Network", 
1331                 "Node_Address", "Primary", "Device", "Frame_Type");
1332         for (i = ipx_interfaces; i != NULL; i = i->if_next) {
1333                 len += sprintf(buffer+len, "%08lX   ", ntohl(i->if_netnum));
1334                 len += sprintf (buffer+len,"%02X%02X%02X%02X%02X%02X   ", 
1335                                 i->if_node[0], i->if_node[1], i->if_node[2],
1336                                 i->if_node[3], i->if_node[4], i->if_node[5]);
1337                 len += sprintf(buffer+len, "%-9s", (i == ipx_primary_net) ?
1338                         "Yes" : "No");
1339                 len += sprintf (buffer+len, "%-11s", ipx_device_name(i));
1340                 len += sprintf (buffer+len, "%s\n", 
1341                         ipx_frame_name(i->if_dlink_type));
1342 
1343                 /* Are we still dumping unwanted data then discard the record */
1344                 pos=begin+len;
1345                 
1346                 if(pos<offset) {
1347                         len=0;                  /* Keep dumping into the buffer start */
1348                         begin=pos;
1349                 }
1350                 if(pos>offset+length)           /* We have dumped enough */
1351                         break;
1352         }
1353         
1354         /* The data in question runs from begin to begin+len */
1355         *start=buffer+(offset-begin);   /* Start of wanted data */
1356         len-=(offset-begin);            /* Remove unwanted header data from length */
1357         if(len>length)
1358                 len=length;             /* Remove unwanted tail data from length */
1359         
1360         return len;
1361 }
1362 
1363 static int ipx_get_info(char *buffer, char **start, off_t offset,
     /* [previous][next][first][last][top][bottom][index][help] */
1364                         int length, int dummy)
1365 {
1366         ipx_socket *s;
1367         ipx_interface *i;
1368         int len=0;
1369         off_t pos=0;
1370         off_t begin=0;
1371 
1372         /* Theory.. Keep printing in the same place until we pass offset */
1373 
1374 #ifdef CONFIG_IPX_INTERN        
1375         len += sprintf (buffer,"%-28s%-28s%-10s%-10s%-7s%s\n", "Local_Address", 
1376 #else
1377         len += sprintf (buffer,"%-15s%-28s%-10s%-10s%-7s%s\n", "Local_Address", 
1378 #endif
1379                         "Remote_Address", "Tx_Queue", "Rx_Queue", 
1380                         "State", "Uid");
1381         for (i = ipx_interfaces; i != NULL; i = i->if_next) {
1382                 for (s = i->if_sklist; s != NULL; s = s->next) {
1383 #ifdef CONFIG_IPX_INTERN
1384                         len += sprintf(buffer+len,
1385                                        "%08lX:%02X%02X%02X%02X%02X%02X:%04X  ", 
1386                                        htonl(s->ipx_intrfc->if_netnum),
1387                                        s->ipx_node[0], s->ipx_node[1], 
1388                                        s->ipx_node[2], s->ipx_node[3], 
1389                                        s->ipx_node[4], s->ipx_node[5],
1390                                        htons(s->ipx_port));
1391 #else
1392                         len += sprintf(buffer+len,"%08lX:%04X  ", 
1393                                        htonl(i->if_netnum),
1394                                        htons(s->ipx_port));
1395 #endif
1396                         if (s->state!=TCP_ESTABLISHED) {
1397                                 len += sprintf(buffer+len, "%-28s", "Not_Connected");
1398                         } else {
1399                                 len += sprintf (buffer+len,
1400                                         "%08lX:%02X%02X%02X%02X%02X%02X:%04X  ", 
1401                                         htonl(s->ipx_dest_addr.net),
1402                                         s->ipx_dest_addr.node[0], s->ipx_dest_addr.node[1], 
1403                                         s->ipx_dest_addr.node[2], s->ipx_dest_addr.node[3], 
1404                                         s->ipx_dest_addr.node[4], s->ipx_dest_addr.node[5],
1405                                         htons(s->ipx_dest_addr.sock));
1406                         }
1407                         len += sprintf (buffer+len,"%08lX  %08lX  ", 
1408                                 s->wmem_alloc, s->rmem_alloc);
1409                         len += sprintf (buffer+len,"%02X     %03d\n", 
1410                                 s->state, SOCK_INODE(s->socket)->i_uid);
1411                 
1412                         /* Are we still dumping unwanted data then discard the record */
1413                         pos=begin+len;
1414                 
1415                         if(pos<offset)
1416                         {
1417                                 len=0;                  /* Keep dumping into the buffer start */
1418                                 begin=pos;
1419                         }
1420                         if(pos>offset+length)           /* We have dumped enough */
1421                                 break;
1422                 }
1423         }
1424         
1425         /* The data in question runs from begin to begin+len */
1426         *start=buffer+(offset-begin);   /* Start of wanted data */
1427         len-=(offset-begin);            /* Remove unwanted header data from length */
1428         if(len>length)
1429                 len=length;             /* Remove unwanted tail data from length */
1430         
1431         return len;
1432 }
1433 
1434 static int ipx_rt_get_info(char *buffer, char **start, off_t offset,
     /* [previous][next][first][last][top][bottom][index][help] */
1435                            int length, int dummy)
1436 {
1437         ipx_route *rt;
1438         int len=0;
1439         off_t pos=0;
1440         off_t begin=0;
1441 
1442         len += sprintf (buffer,"%-11s%-13s%s\n", 
1443                         "Network", "Router_Net", "Router_Node");
1444         for (rt = ipx_routes; rt != NULL; rt = rt->ir_next)
1445         {
1446                 len += sprintf (buffer+len,"%08lX   ", ntohl(rt->ir_net));
1447                 if (rt->ir_routed) {
1448                         len += sprintf (buffer+len,"%08lX     %02X%02X%02X%02X%02X%02X\n", 
1449                                 ntohl(rt->ir_intrfc->if_netnum), 
1450                                 rt->ir_router_node[0], rt->ir_router_node[1], 
1451                                 rt->ir_router_node[2], rt->ir_router_node[3], 
1452                                 rt->ir_router_node[4], rt->ir_router_node[5]);
1453                 } else {
1454                         len += sprintf (buffer+len, "%-13s%s\n",
1455                                         "Directly", "Connected");
1456                 }
1457                 pos=begin+len;
1458                 if(pos<offset)
1459                 {
1460                         len=0;
1461                         begin=pos;
1462                 }
1463                 if(pos>offset+length)
1464                         break;
1465         }
1466         *start=buffer+(offset-begin);
1467         len-=(offset-begin);
1468         if(len>length)
1469                 len=length;
1470         return len;
1471 }
1472 
1473 /*******************************************************************************************************************\
1474 *                                                                                                                   *
1475 *             Handling for system calls applied via the various interfaces to an IPX socket object                  *
1476 *                                                                                                                   *
1477 \*******************************************************************************************************************/
1478  
1479 static int ipx_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1480 {
1481         switch(cmd)
1482         {
1483                 default:
1484                         return(-EINVAL);
1485         }
1486 }
1487 
1488 static int ipx_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
     /* [previous][next][first][last][top][bottom][index][help] */
1489 {
1490         ipx_socket *sk;
1491         int err,opt;
1492         
1493         sk=(ipx_socket *)sock->data;
1494         
1495         if(optval==NULL)
1496                 return(-EINVAL);
1497 
1498         err=verify_area(VERIFY_READ,optval,sizeof(int));
1499         if(err)
1500                 return err;
1501         opt=get_fs_long((unsigned long *)optval);
1502         
1503         switch(level)
1504         {
1505                 case SOL_IPX:
1506                         switch(optname)
1507                         {
1508                                 case IPX_TYPE:
1509                                         sk->ipx_type=opt;
1510                                         return 0;
1511                                 default:
1512                                         return -EOPNOTSUPP;
1513                         }
1514                         break;
1515                         
1516                 case SOL_SOCKET:
1517                         return sock_setsockopt(sk,level,optname,optval,optlen);
1518 
1519                 default:
1520                         return -EOPNOTSUPP;
1521         }
1522 }
1523 
1524 static int ipx_getsockopt(struct socket *sock, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
1525         char *optval, int *optlen)
1526 {
1527         ipx_socket *sk;
1528         int val=0;
1529         int err;
1530         
1531         sk=(ipx_socket *)sock->data;
1532 
1533         switch(level)
1534         {
1535 
1536                 case SOL_IPX:
1537                         switch(optname)
1538                         {
1539                                 case IPX_TYPE:
1540                                         val=sk->ipx_type;
1541                                         break;
1542                                 default:
1543                                         return -ENOPROTOOPT;
1544                         }
1545                         break;
1546                         
1547                 case SOL_SOCKET:
1548                         return sock_getsockopt(sk,level,optname,optval,optlen);
1549                         
1550                 default:
1551                         return -EOPNOTSUPP;
1552         }
1553         err=verify_area(VERIFY_WRITE,optlen,sizeof(int));
1554         if(err)
1555                 return err;
1556         put_fs_long(sizeof(int),(unsigned long *)optlen);
1557         err=verify_area(VERIFY_WRITE,optval,sizeof(int));
1558         if (err) return err;
1559         put_fs_long(val,(unsigned long *)optval);
1560         return(0);
1561 }
1562 
1563 static int ipx_listen(struct socket *sock, int backlog)
     /* [previous][next][first][last][top][bottom][index][help] */
1564 {
1565         return -EOPNOTSUPP;
1566 }
1567 
1568 static void def_callback1(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1569 {
1570         if(!sk->dead)
1571                 wake_up_interruptible(sk->sleep);
1572 }
1573 
1574 static void def_callback2(struct sock *sk, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1575 {
1576         if(!sk->dead)
1577         {
1578                 wake_up_interruptible(sk->sleep);
1579                 sock_wake_async(sk->socket, 1);
1580         }
1581 }
1582 
1583 static int 
1584 ipx_create(struct socket *sock, int protocol)
     /* [previous][next][first][last][top][bottom][index][help] */
1585 {
1586         ipx_socket *sk;
1587         sk=(ipx_socket *)kmalloc(sizeof(*sk),GFP_KERNEL);
1588         if(sk==NULL)
1589                 return(-ENOMEM);
1590         switch(sock->type)
1591         {
1592                 case SOCK_DGRAM:
1593                         break;
1594                 default:
1595                         kfree_s((void *)sk,sizeof(*sk));
1596                         return(-ESOCKTNOSUPPORT);
1597         }
1598         sk->dead=0;
1599         sk->next=NULL;
1600         sk->broadcast=0;
1601         sk->rcvbuf=SK_RMEM_MAX;
1602         sk->sndbuf=SK_WMEM_MAX;
1603         sk->wmem_alloc=0;
1604         sk->rmem_alloc=0;
1605         sk->users=0;
1606         sk->shutdown=0;
1607         sk->prot=NULL;  /* So we use default free mechanisms */
1608         sk->err=0;
1609         skb_queue_head_init(&sk->receive_queue);
1610         skb_queue_head_init(&sk->write_queue);
1611         sk->send_head=NULL;
1612         skb_queue_head_init(&sk->back_log);
1613         sk->state=TCP_CLOSE;
1614         sk->socket=sock;
1615         sk->type=sock->type;
1616         sk->ipx_type=0;         /* General user level IPX */
1617         sk->debug=0;
1618         sk->ipx_intrfc = NULL;
1619         memset(&sk->ipx_dest_addr,'\0',sizeof(sk->ipx_dest_addr));
1620         sk->ipx_port = 0;
1621         sk->mtu=IPX_MTU;
1622         
1623         if(sock!=NULL)
1624         {
1625                 sock->data=(void *)sk;
1626                 sk->sleep=sock->wait;
1627         }
1628         
1629         sk->state_change=def_callback1;
1630         sk->data_ready=def_callback2;
1631         sk->write_space=def_callback1;
1632         sk->error_report=def_callback1;
1633 
1634         sk->zapped=1;
1635         return 0;
1636 }
1637 
1638 static int ipx_release(struct socket *sock, struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
1639 {
1640         ipx_socket *sk=(ipx_socket *)sock->data;
1641         if(sk==NULL)
1642                 return(0);
1643         if(!sk->dead)
1644                 sk->state_change(sk);
1645         sk->dead=1;
1646         sock->data=NULL;
1647         ipx_destroy_socket(sk);
1648         return(0);
1649 }
1650 
1651 static int ipx_dup(struct socket *newsock,struct socket *oldsock)
     /* [previous][next][first][last][top][bottom][index][help] */
1652 {
1653         return(ipx_create(newsock,SOCK_DGRAM));
1654 }
1655 
1656 static unsigned short 
1657 ipx_first_free_socketnum(ipx_interface *intrfc)
     /* [previous][next][first][last][top][bottom][index][help] */
1658 {
1659         unsigned short  socketNum = intrfc->if_sknum;
1660 
1661         if (socketNum < IPX_MIN_EPHEMERAL_SOCKET)
1662                 socketNum = IPX_MIN_EPHEMERAL_SOCKET;
1663 
1664         while (ipxitf_find_socket(intrfc, ntohs(socketNum)) != NULL)
1665                 if (socketNum > IPX_MAX_EPHEMERAL_SOCKET)
1666                         socketNum = IPX_MIN_EPHEMERAL_SOCKET;
1667                 else
1668                         socketNum++;
1669 
1670         intrfc->if_sknum = socketNum;
1671         return  ntohs(socketNum);
1672 }
1673         
1674 static int ipx_bind(struct socket *sock, struct sockaddr *uaddr,int addr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
1675 {
1676         ipx_socket *sk;
1677         ipx_interface *intrfc;
1678         struct sockaddr_ipx *addr=(struct sockaddr_ipx *)uaddr;
1679         
1680         sk=(ipx_socket *)sock->data;
1681         
1682         if(sk->zapped==0)
1683                 return -EIO;
1684                 
1685         if(addr_len!=sizeof(struct sockaddr_ipx))
1686                 return -EINVAL;
1687         
1688         intrfc = ipxitf_find_using_net(addr->sipx_network);
1689         if (intrfc == NULL)
1690                 return -EADDRNOTAVAIL;
1691 
1692         if (addr->sipx_port == 0) {
1693                 addr->sipx_port = ipx_first_free_socketnum(intrfc);
1694                 if (addr->sipx_port == 0)
1695                         return -EINVAL;
1696         }
1697 
1698         if(ntohs(addr->sipx_port)<IPX_MIN_EPHEMERAL_SOCKET && !suser())
1699                 return -EPERM;  /* protect IPX system stuff like routing/sap */
1700 
1701         sk->ipx_port=addr->sipx_port;
1702 
1703 #ifdef CONFIG_IPX_INTERN
1704         if (intrfc == ipx_internal_net)
1705         {
1706                 /* The source address is to be set explicitly if the
1707                  * socket is to be bound on the internal network. If a
1708                  * node number 0 was specified, the default is used.
1709                  */
1710 
1711                 if (memcmp(addr->sipx_node, ipx_broadcast_node,
1712                            IPX_NODE_LEN) == 0)
1713                 {
1714                         return -EINVAL;
1715                 }
1716                 if (memcmp(addr->sipx_node, ipx_this_node, IPX_NODE_LEN) == 0)
1717                 {
1718                         memcpy(sk->ipx_node, intrfc->if_node,
1719                                IPX_NODE_LEN);
1720                 }
1721                 else
1722                 {
1723                         memcpy(sk->ipx_node, addr->sipx_node, IPX_NODE_LEN);
1724                 }
1725                 if (ipxitf_find_internal_socket(intrfc, sk->ipx_node,
1726                                                 sk->ipx_port) != NULL)
1727                 {
1728                         if(sk->debug)
1729                                 printk("IPX: bind failed because port %X in"
1730                                        " use.\n", (int)addr->sipx_port);
1731                         return -EADDRINUSE;
1732                 }
1733         }
1734         else
1735         {
1736                 /* Source addresses are easy. It must be our
1737                  * network:node pair for an interface routed to IPX
1738                  * with the ipx routing ioctl()
1739                  */
1740 
1741                 memcpy(sk->ipx_node, intrfc->if_node, IPX_NODE_LEN);
1742                 
1743                 if(ipxitf_find_socket(intrfc, addr->sipx_port)!=NULL) {
1744                         if(sk->debug)
1745                                 printk("IPX: bind failed because port %X in"
1746                                        " use.\n", (int)addr->sipx_port);
1747                         return -EADDRINUSE;        
1748                 }
1749         }
1750 
1751 #else
1752 
1753         /* Source addresses are easy. It must be our network:node pair for
1754            an interface routed to IPX with the ipx routing ioctl() */
1755 
1756         if(ipxitf_find_socket(intrfc, addr->sipx_port)!=NULL) {
1757                 if(sk->debug)
1758                         printk("IPX: bind failed because port %X in use.\n",
1759                                 (int)addr->sipx_port);
1760                 return -EADDRINUSE;        
1761         }
1762 
1763 #endif
1764 
1765         ipxitf_insert_socket(intrfc, sk);
1766         sk->zapped=0;
1767         if(sk->debug)
1768                 printk("IPX: socket is bound.\n");
1769         return 0;
1770 }
1771 
1772 static int ipx_connect(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1773         int addr_len, int flags)
1774 {
1775         ipx_socket *sk=(ipx_socket *)sock->data;
1776         struct sockaddr_ipx *addr;
1777         
1778         sk->state = TCP_CLOSE;  
1779         sock->state = SS_UNCONNECTED;
1780 
1781         if(addr_len!=sizeof(*addr))
1782                 return(-EINVAL);
1783         addr=(struct sockaddr_ipx *)uaddr;
1784         
1785         if(sk->ipx_port==0)
1786         /* put the autobinding in */
1787         {
1788                 struct sockaddr_ipx uaddr;
1789                 int ret;
1790         
1791                 uaddr.sipx_port = 0;
1792                 uaddr.sipx_network = 0L;
1793 #ifdef CONFIG_IPX_INTERN
1794                 memcpy(uaddr.sipx_node, sk->ipx_intrfc->if_node,
1795                        IPX_NODE_LEN);
1796 #endif
1797                 ret = ipx_bind (sock, (struct sockaddr *)&uaddr,
1798                                 sizeof(struct sockaddr_ipx));
1799                 if (ret != 0) return (ret);
1800         }
1801         
1802         if(ipxrtr_lookup(addr->sipx_network)==NULL)
1803                 return -ENETUNREACH;
1804         sk->ipx_dest_addr.net=addr->sipx_network;
1805         sk->ipx_dest_addr.sock=addr->sipx_port;
1806         memcpy(sk->ipx_dest_addr.node,addr->sipx_node,IPX_NODE_LEN);
1807         sk->ipx_type=addr->sipx_type;
1808         sock->state = SS_CONNECTED;
1809         sk->state=TCP_ESTABLISHED;
1810         return 0;
1811 }
1812 
1813 static int ipx_socketpair(struct socket *sock1, struct socket *sock2)
     /* [previous][next][first][last][top][bottom][index][help] */
1814 {
1815         return(-EOPNOTSUPP);
1816 }
1817 
1818 static int ipx_accept(struct socket *sock, struct socket *newsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1819 {
1820         if(newsock->data)
1821                 kfree_s(newsock->data,sizeof(ipx_socket));
1822         return -EOPNOTSUPP;
1823 }
1824 
1825 static int ipx_getname(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1826         int *uaddr_len, int peer)
1827 {
1828         ipx_address *addr;
1829         struct sockaddr_ipx sipx;
1830         ipx_socket *sk;
1831         
1832         sk=(ipx_socket *)sock->data;
1833         
1834         *uaddr_len = sizeof(struct sockaddr_ipx);
1835                 
1836         if(peer) {
1837                 if(sk->state!=TCP_ESTABLISHED)
1838                         return -ENOTCONN;
1839                 addr=&sk->ipx_dest_addr;
1840                 sipx.sipx_network = addr->net;
1841                 memcpy(sipx.sipx_node,addr->node,IPX_NODE_LEN);
1842                 sipx.sipx_port = addr->sock;
1843         } else {
1844                 if (sk->ipx_intrfc != NULL) {
1845                         sipx.sipx_network = sk->ipx_intrfc->if_netnum;
1846 #ifdef CONFIG_IPX_INTERN
1847                         memcpy(sipx.sipx_node, sk->ipx_node, IPX_NODE_LEN);
1848 #else
1849                         memcpy(sipx.sipx_node, sk->ipx_intrfc->if_node,
1850                                IPX_NODE_LEN);
1851 #endif
1852 
1853                 } else {
1854                         sipx.sipx_network = 0L;
1855                         memset(sipx.sipx_node, '\0', IPX_NODE_LEN);
1856                 }
1857                 sipx.sipx_port = sk->ipx_port;
1858         }
1859                 
1860         sipx.sipx_family = AF_IPX;
1861         sipx.sipx_type = sk->ipx_type;
1862         memcpy(uaddr,&sipx,sizeof(sipx));
1863         return 0;
1864 }
1865 
1866 #if 0
1867 /*
1868  * User to dump IPX packets (debugging)
1869  */
1870 void dump_data(char *str,unsigned char *d, int len) {
     /* [previous][next][first][last][top][bottom][index][help] */
1871   static char h2c[] = "0123456789ABCDEF";
1872   int l,i;
1873   char *p, b[64];
1874   for (l=0;len > 0 && l<16;l++) {
1875     p = b;
1876     for (i=0; i < 8 ; i++, --len) {
1877           if (len > 0) {
1878               *(p++) = h2c[(d[i] >> 4) & 0x0f];
1879               *(p++) = h2c[d[i] & 0x0f];
1880           }
1881           else {
1882               *(p++) = ' ';
1883               *(p++) = ' ';
1884           }
1885       *(p++) = ' ';
1886     }
1887     *(p++) = '-';
1888     *(p++) = ' ';
1889         len += 8;
1890     for (i=0; i < 8 ; i++, --len)
1891                 if (len > 0)
1892                         *(p++) = ' '<= d[i] && d[i]<'\177' ? d[i] : '.';
1893                 else
1894                         *(p++) = ' ';
1895     *p = '\000';
1896     d += i;
1897     printk("%s-%04X: %s\n",str,l*8,b);
1898   }
1899 }
1900 
1901 void dump_addr(char *str,ipx_address *p) {
     /* [previous][next][first][last][top][bottom][index][help] */
1902   printk("%s: %08X:%02X%02X%02X%02X%02X%02X:%04X\n",
1903    str,ntohl(p->net),p->node[0],p->node[1],p->node[2],
1904    p->node[3],p->node[4],p->node[5],ntohs(p->sock));
1905 }
1906 
1907 void dump_hdr(char *str,ipx_packet *p) {
     /* [previous][next][first][last][top][bottom][index][help] */
1908   printk("%s: CHKSUM=%04X SIZE=%d (%04X) HOPS=%d (%02X) TYPE=%02X\n",
1909    str,p->ipx_checksum,ntohs(p->ipx_pktsize),ntohs(p->ipx_pktsize),
1910    p->ipx_tctrl,p->ipx_tctrl,p->ipx_type);
1911   dump_addr("  IPX-DST",&p->ipx_dest);
1912   dump_addr("  IPX-SRC",&p->ipx_source);
1913 }
1914 
1915 void dump_pkt(char *str,ipx_packet *p) {
     /* [previous][next][first][last][top][bottom][index][help] */
1916   int len = ntohs(p->ipx_pktsize);
1917   dump_hdr(str,p);
1918   if (len > 30)
1919           dump_data(str,(unsigned char *)p + 30, len - 30);
1920 }
1921 #endif
1922 
1923 int ipx_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
1924 {
1925         /* NULL here for pt means the packet was looped back */
1926         ipx_interface   *intrfc;
1927         ipx_packet *ipx;
1928         
1929         
1930         ipx=(ipx_packet *)skb->h.raw;
1931         
1932         if(ipx->ipx_checksum!=IPX_NO_CHECKSUM) {
1933                 /* We don't do checksum options. We can't really. Novell don't seem to have documented them.
1934                    If you need them try the XNS checksum since IPX is basically XNS in disguise. It might be
1935                    the same... */
1936                 kfree_skb(skb,FREE_READ);
1937                 return 0;
1938         }
1939         
1940         /* Too small */
1941         if(htons(ipx->ipx_pktsize)<sizeof(ipx_packet)) {
1942                 kfree_skb(skb,FREE_READ);
1943                 return 0;
1944         }
1945         
1946         /* Determine what local ipx endpoint this is */
1947         intrfc = ipxitf_find_using_phys(dev, pt->type);
1948         if (intrfc == NULL) {
1949                 if (ipxcfg_auto_create_interfaces) {
1950                         intrfc = ipxitf_auto_create(dev, pt->type);
1951                 }
1952 
1953                 if (intrfc == NULL) {
1954                         /* Not one of ours */
1955                         kfree_skb(skb,FREE_READ);
1956                         return 0;
1957                 }
1958         }
1959 
1960         return ipxitf_rcv(intrfc, skb);
1961 }
1962 
1963 static int ipx_sendmsg(struct socket *sock, struct msghdr *msg, int len, int noblock,
     /* [previous][next][first][last][top][bottom][index][help] */
1964         int flags)
1965 {
1966         ipx_socket *sk=(ipx_socket *)sock->data;
1967         struct sockaddr_ipx *usipx=(struct sockaddr_ipx *)msg->msg_name;
1968         struct sockaddr_ipx local_sipx;
1969         int retval;
1970 
1971         if (sk->zapped) return -EIO; /* Socket not bound */
1972         if(flags) return -EINVAL;
1973                 
1974         if(usipx) 
1975         {
1976                 if(sk->ipx_port == 0) 
1977                 {
1978                         struct sockaddr_ipx uaddr;
1979                         int ret;
1980 
1981                         uaddr.sipx_port = 0;
1982                         uaddr.sipx_network = 0L; 
1983 #ifdef CONFIG_IPX_INTERN
1984                         memcpy(uaddr.sipx_node, sk->ipx_intrfc->if_node,
1985                                IPX_NODE_LEN);
1986 #endif
1987                         ret = ipx_bind (sock, (struct sockaddr *)&uaddr,
1988                                         sizeof(struct sockaddr_ipx));
1989                         if (ret != 0) return ret;
1990                 }
1991 
1992                 if(msg->msg_namelen <sizeof(*usipx))
1993                         return -EINVAL;
1994                 if(usipx->sipx_family != AF_IPX)
1995                         return -EINVAL;
1996         }
1997         else 
1998         {
1999                 if(sk->state!=TCP_ESTABLISHED)
2000                         return -ENOTCONN;
2001                 usipx=&local_sipx;
2002                 usipx->sipx_family=AF_IPX;
2003                 usipx->sipx_type=sk->ipx_type;
2004                 usipx->sipx_port=sk->ipx_dest_addr.sock;
2005                 usipx->sipx_network=sk->ipx_dest_addr.net;
2006                 memcpy(usipx->sipx_node,sk->ipx_dest_addr.node,IPX_NODE_LEN);
2007         }
2008         
2009         retval = ipxrtr_route_packet(sk, usipx, msg->msg_iov, len);
2010         if (retval < 0) return retval;
2011 
2012         return len;
2013 }
2014 
2015 
2016 static int ipx_recvmsg(struct socket *sock, struct msghdr *msg, int size, int noblock,
     /* [previous][next][first][last][top][bottom][index][help] */
2017                  int flags, int *addr_len)
2018 {
2019         ipx_socket *sk=(ipx_socket *)sock->data;
2020         struct sockaddr_ipx *sipx=(struct sockaddr_ipx *)msg->msg_name;
2021         struct ipx_packet *ipx = NULL;
2022         int copied = 0;
2023         int truesize;
2024         struct sk_buff *skb;
2025         int er;
2026         
2027         if(sk->err)
2028                 return sock_error(sk);
2029         
2030         if (sk->zapped)
2031                 return -EIO;
2032 
2033 
2034         skb=skb_recv_datagram(sk,flags,noblock,&er);
2035         if(skb==NULL)
2036                 return er;
2037         
2038         if(addr_len)
2039                 *addr_len=sizeof(*sipx);
2040 
2041         ipx = (ipx_packet *)(skb->h.raw);
2042         truesize=ntohs(ipx->ipx_pktsize) - sizeof(ipx_packet);
2043         copied = (truesize > size) ? size : truesize;
2044         skb_copy_datagram_iovec(skb,sizeof(struct ipx_packet),msg->msg_iov,copied);
2045         
2046         if(sipx)
2047         {
2048                 sipx->sipx_family=AF_IPX;
2049                 sipx->sipx_port=ipx->ipx_source.sock;
2050                 memcpy(sipx->sipx_node,ipx->ipx_source.node,IPX_NODE_LEN);
2051                 sipx->sipx_network=ipx->ipx_source.net;
2052                 sipx->sipx_type = ipx->ipx_type;
2053         }
2054         skb_free_datagram(sk, skb);
2055         return(truesize);
2056 }               
2057 
2058 static int ipx_shutdown(struct socket *sk,int how)
     /* [previous][next][first][last][top][bottom][index][help] */
2059 {
2060         return -EOPNOTSUPP;
2061 }
2062 
2063 static int ipx_select(struct socket *sock , int sel_type, select_table *wait)
     /* [previous][next][first][last][top][bottom][index][help] */
2064 {
2065         ipx_socket *sk=(ipx_socket *)sock->data;
2066         
2067         return datagram_select(sk,sel_type,wait);
2068 }
2069 
2070 static int ipx_ioctl(struct socket *sock,unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
2071 {
2072         int err;
2073         long amount=0;
2074         ipx_socket *sk=(ipx_socket *)sock->data;
2075         
2076         switch(cmd)
2077         {
2078                 case TIOCOUTQ:
2079                         err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(unsigned long));
2080                         if(err)
2081                                 return err;
2082                         amount=sk->sndbuf-sk->wmem_alloc;
2083                         if(amount<0)
2084                                 amount=0;
2085                         put_fs_long(amount,(unsigned long *)arg);
2086                         return 0;
2087                 case TIOCINQ:
2088                 {
2089                         struct sk_buff *skb;
2090                         /* These two are safe on a single CPU system as only user tasks fiddle here */
2091                         if((skb=skb_peek(&sk->receive_queue))!=NULL)
2092                                 amount=skb->len-sizeof(struct ipx_packet);
2093                         err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(unsigned long));
2094                         if(err)
2095                                 return err;
2096                         put_fs_long(amount,(unsigned long *)arg);
2097                         return 0;
2098                 }
2099                 case SIOCADDRT:
2100                 case SIOCDELRT:
2101                         if(!suser())
2102                                 return -EPERM;
2103                         return(ipxrtr_ioctl(cmd,(void *)arg));
2104                 case SIOCSIFADDR:
2105                 case SIOCGIFADDR:
2106                 case SIOCAIPXITFCRT:
2107                 case SIOCAIPXPRISLT:
2108                         if(!suser())
2109                                 return -EPERM;
2110                         return(ipxitf_ioctl(cmd,(void *)arg));
2111                 case SIOCIPXCFGDATA: 
2112                 {
2113                         err=verify_area(VERIFY_WRITE,(void *)arg,
2114                                 sizeof(ipx_config_data));
2115                         if(err) return err;
2116                         return(ipxcfg_get_config_data((void *)arg));
2117                 }
2118                 case SIOCGSTAMP:
2119                         if (sk)
2120                         {
2121                                 if(sk->stamp.tv_sec==0)
2122                                         return -ENOENT;
2123                                 err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval));
2124                                 if(err)
2125                                         return err;
2126                                 memcpy_tofs((void *)arg,&sk->stamp,sizeof(struct timeval));
2127                                 return 0;
2128                         }
2129                         return -EINVAL;
2130                 case SIOCGIFDSTADDR:
2131                 case SIOCSIFDSTADDR:
2132                 case SIOCGIFBRDADDR:
2133                 case SIOCSIFBRDADDR:
2134                 case SIOCGIFNETMASK:
2135                 case SIOCSIFNETMASK:
2136                         return -EINVAL;
2137                 default:
2138                         return(dev_ioctl(cmd,(void *) arg));
2139         }
2140         /*NOTREACHED*/
2141         return(0);
2142 }
2143 
2144 static struct proto_ops ipx_proto_ops = {
2145         AF_IPX,
2146         
2147         ipx_create,
2148         ipx_dup,
2149         ipx_release,
2150         ipx_bind,
2151         ipx_connect,
2152         ipx_socketpair,
2153         ipx_accept,
2154         ipx_getname,
2155         ipx_select,
2156         ipx_ioctl,
2157         ipx_listen,
2158         ipx_shutdown,
2159         ipx_setsockopt,
2160         ipx_getsockopt,
2161         ipx_fcntl,
2162         ipx_sendmsg,
2163         ipx_recvmsg
2164 };
2165 
2166 /* Called by protocol.c on kernel start up */
2167 
2168 static struct packet_type ipx_8023_packet_type = 
2169 
2170 {
2171         0,      /* MUTTER ntohs(ETH_P_8023),*/
2172         NULL,           /* All devices */
2173         ipx_rcv,
2174         NULL,
2175         NULL,
2176 };
2177  
2178 static struct packet_type ipx_dix_packet_type = 
2179 {
2180         0,      /* MUTTER ntohs(ETH_P_IPX),*/
2181         NULL,           /* All devices */
2182         ipx_rcv,
2183         NULL,
2184         NULL,
2185 };
2186  
2187 static struct notifier_block ipx_dev_notifier={
2188         ipxitf_device_event,
2189         NULL,
2190         0
2191 };
2192 
2193 
2194 extern struct datalink_proto    *make_EII_client(void);
2195 extern struct datalink_proto    *make_8023_client(void);
2196 
2197 void ipx_proto_init(struct net_proto *pro)
     /* [previous][next][first][last][top][bottom][index][help] */
2198 {
2199         unsigned char   val = 0xE0;
2200         unsigned char   snapval[5] =  { 0x0, 0x0, 0x0, 0x81, 0x37 };
2201 
2202         (void) sock_register(ipx_proto_ops.family, &ipx_proto_ops);
2203 
2204         pEII_datalink = make_EII_client();
2205         ipx_dix_packet_type.type=htons(ETH_P_IPX);
2206         dev_add_pack(&ipx_dix_packet_type);
2207 
2208         p8023_datalink = make_8023_client();
2209         ipx_8023_packet_type.type=htons(ETH_P_802_3);
2210         dev_add_pack(&ipx_8023_packet_type);
2211         
2212         if ((p8022_datalink = register_8022_client(val, ipx_rcv)) == NULL)
2213                 printk("IPX: Unable to register with 802.2\n");
2214 
2215         if ((pSNAP_datalink = register_snap_client(snapval, ipx_rcv)) == NULL)
2216                 printk("IPX: Unable to register with SNAP\n");
2217         
2218         register_netdevice_notifier(&ipx_dev_notifier);
2219 
2220         proc_net_register(&(struct proc_dir_entry) {
2221                 PROC_NET_IPX, 3, "ipx",
2222                 S_IFREG | S_IRUGO, 1, 0, 0,
2223                 0, &proc_net_inode_operations,
2224                 ipx_get_info
2225         });
2226         proc_net_register(&(struct proc_dir_entry) {
2227                 PROC_NET_IPX_INTERFACE, 13, "ipx_interface",
2228                 S_IFREG | S_IRUGO, 1, 0, 0,
2229                 0, &proc_net_inode_operations,
2230                 ipx_interface_get_info
2231         });
2232         proc_net_register(&(struct proc_dir_entry) {
2233                 PROC_NET_IPX_ROUTE, 9, "ipx_route",
2234                 S_IFREG | S_IRUGO, 1, 0, 0,
2235                 0, &proc_net_inode_operations,
2236                 ipx_rt_get_info
2237         });
2238                 
2239         printk("Swansea University Computer Society IPX 0.33 for NET3.032\n");
2240         printk("IPX Portions Copyright (c) 1995 Caldera, Inc.\n");
2241 }
2242 #endif

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