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. ipx_set_checksum
  34. ipxrtr_route_packet
  35. ipxrtr_route_skb
  36. ipxrtr_ioctl
  37. ipx_frame_name
  38. ipx_device_name
  39. ipx_interface_get_info
  40. ipx_get_info
  41. ipx_rt_get_info
  42. ipx_fcntl
  43. ipx_setsockopt
  44. ipx_getsockopt
  45. ipx_listen
  46. def_callback1
  47. def_callback2
  48. ipx_create
  49. ipx_release
  50. ipx_dup
  51. ipx_first_free_socketnum
  52. ipx_bind
  53. ipx_connect
  54. ipx_socketpair
  55. ipx_accept
  56. ipx_getname
  57. dump_data
  58. dump_addr
  59. dump_hdr
  60. dump_pkt
  61. ipx_rcv
  62. ipx_sendmsg
  63. ipx_recvmsg
  64. ipx_shutdown
  65. ipx_select
  66. ipx_ioctl
  67. ipx_proto_init
  68. ipx_proto_finito
  69. init_module
  70. cleanup_module

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

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