root/net/ipx/af_ipx.c

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

DEFINITIONS

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

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

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