2 * Implements an IPX socket layer.
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
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.
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
15 * liability nor provide warranty for any of this software. This material
16 * is provided as is and at no charge.
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. Adding a route will
22 * will overwrite any existing route to the same network.
23 * Revision 0.24: Supports new /proc with no 4K limit
24 * Revision 0.25: Add ephemeral sockets, passive local network
25 * identification, support for local net 0 and
26 * multiple datalinks <Greg Page>
27 * Revision 0.26: Device drop kills IPX routes via it. (needed for module)
28 * Revision 0.27: Autobind <Mark Evans>
29 * Revision 0.28: Small fix for multiple local networks <Thomas Winder>
30 * Revision 0.29: Assorted major errors removed <Mark Evans>
31 * Small correction to promisc mode error fix <Alan Cox>
32 * Asynchronous I/O support. Changed to use notifiers
33 * and the newer packet_type stuff. Assorted major
34 * fixes <Alejandro Liu>
35 * Revision 0.30: Moved to net/ipx/... <Alan Cox>
36 * Don't set address length on recvfrom that errors.
37 * Incorrect verify_area.
38 * Revision 0.31: New sk_buffs. This still needs a lot of
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 * Handles WIN95 discovery packets <Volker Lendecke>
47 * Revision 0.36: Internal bump up for 2.1
48 * Revision 0.37: Began adding POSIXisms.
49 * Revision 0.38: Asynchronous socket stuff made current.
50 * Revision 0.39: SPX interfaces
51 * Revision 0.40: Tiny SIOCGSTAMP fix (chris@cybernet.co.nz)
52 * Revision 0.41: 802.2TR removed (p.norton@computer.org)
53 * Fixed connecting to primary net,
54 * Automatic binding on send & receive,
55 * Martijn van Oosterhout <kleptogimp@geocities.com>
57 * Protect the module by a MOD_INC_USE_COUNT/MOD_DEC_USE_COUNT
58 * pair. Also, now usage count is managed this way
59 * -Count one if the auto_interface mode is on
60 * -Count one per configured interface
62 * Jacques Gelinas (jacques@solucorp.qc.ca)
65 * Portions Copyright (c) 1995 Caldera, Inc. <greg@caldera.com>
66 * Neither Greg Page nor Caldera, Inc. admit liability nor provide
67 * warranty for any of this software. This material is provided
68 * "AS-IS" and at no charge.
71 #include <linux/config.h>
72 #if defined (CONFIG_IPX) || defined (CONFIG_IPX_MODULE)
73 #include <linux/module.h>
74 #include <linux/errno.h>
75 #include <linux/types.h>
76 #include <linux/socket.h>
78 #include <linux/kernel.h>
79 #include <linux/sched.h>
80 #include <linux/timer.h>
81 #include <linux/string.h>
82 #include <linux/sockios.h>
83 #include <linux/net.h>
84 #include <linux/netdevice.h>
86 #include <linux/inet.h>
87 #include <linux/route.h>
89 #include <asm/uaccess.h>
90 #include <asm/system.h>
91 #include <linux/fcntl.h>
93 #include <linux/termios.h> /* For TIOCOUTQ/INQ */
94 #include <linux/interrupt.h>
95 #include <net/p8022.h>
96 #include <net/psnap.h>
97 #include <linux/proc_fs.h>
98 #include <linux/stat.h>
99 #include <linux/init.h>
100 #include <linux/if_arp.h>
103 static void ipx_proto_finito(void);
104 #endif /* def MODULE */
106 /* Configuration Variables */
107 static unsigned char ipxcfg_max_hops
= 16;
108 static char ipxcfg_auto_select_primary
= 0;
109 static char ipxcfg_auto_create_interfaces
= 0;
111 /* Global Variables */
112 static struct datalink_proto
*p8022_datalink
= NULL
;
113 static struct datalink_proto
*pEII_datalink
= NULL
;
114 static struct datalink_proto
*p8023_datalink
= NULL
;
115 static struct datalink_proto
*pSNAP_datalink
= NULL
;
117 static struct proto_ops ipx_dgram_ops
;
119 static struct net_proto_family
*spx_family_ops
;
121 static ipx_route
*ipx_routes
= NULL
;
122 static ipx_interface
*ipx_interfaces
= NULL
;
123 static ipx_interface
*ipx_primary_net
= NULL
;
124 static ipx_interface
*ipx_internal_net
= NULL
;
126 static int ipxcfg_set_auto_create(char val
)
128 if(ipxcfg_auto_create_interfaces
!= val
)
135 ipxcfg_auto_create_interfaces
= val
;
141 static int ipxcfg_set_auto_select(char val
)
143 ipxcfg_auto_select_primary
= val
;
144 if(val
&& (ipx_primary_net
== NULL
))
145 ipx_primary_net
= ipx_interfaces
;
150 static int ipxcfg_get_config_data(ipx_config_data
*arg
)
152 ipx_config_data vals
;
154 vals
.ipxcfg_auto_create_interfaces
= ipxcfg_auto_create_interfaces
;
155 vals
.ipxcfg_auto_select_primary
= ipxcfg_auto_select_primary
;
157 return (copy_to_user(arg
, &vals
, sizeof(vals
)) ? -EFAULT
: 0);
160 /**************************************************************************\
162 * Handlers for the socket list. *
164 \**************************************************************************/
167 * Note: Sockets may not be removed _during_ an interrupt or inet_bh
168 * handler using this technique. They can be added although we do not
172 void ipx_remove_socket(struct sock
*sk
)
175 ipx_interface
*intrfc
;
181 /* Determine interface with which socket is associated */
182 intrfc
= sk
->protinfo
.af_ipx
.intrfc
;
185 restore_flags(flags
);
189 s
= intrfc
->if_sklist
;
192 intrfc
->if_sklist
= s
->next
;
193 restore_flags(flags
);
202 restore_flags(flags
);
207 restore_flags(flags
);
211 * This is only called from user mode. Thus it protects itself against
212 * interrupt users but doesn't worry about being called during work.
213 * Once it is removed from the queue no interrupt or bottom half will
214 * touch it and we are (fairly 8-) ) safe.
216 static void ipx_destroy_socket(struct sock
*sk
)
220 ipx_remove_socket(sk
);
221 while((skb
= skb_dequeue(&sk
->receive_queue
)) != NULL
)
228 * The following code is used to support IPX Interfaces (IPXITF). An
229 * IPX interface is defined by a physical device and a frame type.
231 static ipx_route
* ipxrtr_lookup(__u32
);
233 static void ipxitf_clear_primary_net(void)
235 if(ipxcfg_auto_select_primary
&& (ipx_interfaces
!= NULL
))
236 ipx_primary_net
= ipx_interfaces
;
238 ipx_primary_net
= NULL
;
241 static ipx_interface
*ipxitf_find_using_phys(struct net_device
*dev
, unsigned short datalink
)
245 for(i
= ipx_interfaces
;
246 i
&& ((i
->if_dev
!= dev
) || (i
->if_dlink_type
!= datalink
));
253 static ipx_interface
*ipxitf_find_using_net(__u32 net
)
258 return (ipx_primary_net
);
260 for(i
= ipx_interfaces
; i
&& (i
->if_netnum
!= net
); i
= i
->if_next
)
266 /* Sockets are bound to a particular IPX interface. */
267 static void ipxitf_insert_socket(ipx_interface
*intrfc
, struct sock
*sk
)
271 sk
->protinfo
.af_ipx
.intrfc
= intrfc
;
273 if(intrfc
->if_sklist
== NULL
)
274 intrfc
->if_sklist
= sk
;
277 for (s
= intrfc
->if_sklist
; s
->next
!= NULL
; s
= s
->next
)
283 static struct sock
*ipxitf_find_socket(ipx_interface
*intrfc
, unsigned short port
)
287 for(s
= intrfc
->if_sklist
;
288 (s
!= NULL
) && (s
->protinfo
.af_ipx
.port
!= port
);
295 #ifdef CONFIG_IPX_INTERN
297 static struct sock
*ipxitf_find_internal_socket(ipx_interface
*intrfc
,
298 unsigned char *node
, unsigned short port
)
300 struct sock
*s
= intrfc
->if_sklist
;
304 if((s
->protinfo
.af_ipx
.port
== port
)
305 && (memcmp(node
, s
->protinfo
.af_ipx
.node
, IPX_NODE_LEN
) == 0))
316 static void ipxrtr_del_routes(ipx_interface
*);
318 static void ipxitf_down(ipx_interface
*intrfc
)
323 /* Delete all routes associated with this interface */
324 ipxrtr_del_routes(intrfc
);
327 for(s
= intrfc
->if_sklist
; s
!= NULL
; )
331 s
->protinfo
.af_ipx
.intrfc
= NULL
;
332 s
->protinfo
.af_ipx
.port
= 0;
333 s
->zapped
=1; /* Indicates it is no longer bound */
338 intrfc
->if_sklist
= NULL
;
340 /* remove this interface from list */
341 if(intrfc
== ipx_interfaces
)
342 ipx_interfaces
= intrfc
->if_next
;
345 for(i
= ipx_interfaces
;
346 (i
!= NULL
) && (i
->if_next
!= intrfc
);
349 if((i
!= NULL
) && (i
->if_next
== intrfc
))
350 i
->if_next
= intrfc
->if_next
;
353 /* remove this interface from *special* networks */
354 if(intrfc
== ipx_primary_net
)
355 ipxitf_clear_primary_net();
356 if(intrfc
== ipx_internal_net
)
357 ipx_internal_net
= NULL
;
359 kfree_s(intrfc
, sizeof(*intrfc
));
365 static int ipxitf_device_event(struct notifier_block
*notifier
, unsigned long event
, void *ptr
)
367 struct net_device
*dev
= ptr
;
368 ipx_interface
*i
, *tmp
;
370 if(event
!= NETDEV_DOWN
)
373 for(i
= ipx_interfaces
; i
!= NULL
;)
382 return (NOTIFY_DONE
);
385 static int ipxitf_def_skb_handler(struct sock
*sock
, struct sk_buff
*skb
)
389 if((retval
= sock_queue_rcv_skb(sock
, skb
)) < 0)
396 * On input skb->sk is NULL. Nobody is charged for the memory.
399 #ifdef CONFIG_IPX_INTERN
400 static int ipxitf_demux_socket(ipx_interface
*intrfc
, struct sk_buff
*skb
, int copy
)
402 struct ipxhdr
*ipx
= skb
->nh
.ipxh
;
405 int is_broadcast
= (memcmp(ipx
->ipx_dest
.node
, ipx_broadcast_node
,
408 s
= intrfc
->if_sklist
;
412 if((s
->protinfo
.af_ipx
.port
== ipx
->ipx_dest
.sock
)
414 || (memcmp(ipx
->ipx_dest
.node
, s
->protinfo
.af_ipx
.node
,
415 IPX_NODE_LEN
) == 0)))
417 /* We found a socket to which to send */
418 struct sk_buff
*skb1
;
422 skb1
= skb_clone(skb
, GFP_ATOMIC
);
429 copy
= 1; /* skb may only be used once */
431 ipxitf_def_skb_handler(s
, skb1
);
433 /* On an external interface, one socket can listen */
434 if(intrfc
!= ipx_internal_net
)
440 /* skb was solely for us, and we did not make a copy, so free it. */
449 static int ipxitf_demux_socket(ipx_interface
*intrfc
, struct sk_buff
*skb
, int copy
)
451 struct ipxhdr
*ipx
= skb
->nh
.ipxh
;
452 struct sock
*sock1
= NULL
, *sock2
= NULL
;
453 struct sk_buff
*skb1
= NULL
, *skb2
= NULL
;
455 if (intrfc
== ipx_primary_net
&& ntohs(ipx
->ipx_dest
.sock
) == 0x451)
458 * The packet's target is a NCP connection handler. We want to
459 * hand it to the correct socket directly within the kernel,
460 * so that the mars_nwe packet distribution process
461 * does not have to do it. Here we only care about NCP and
463 * You might call this a hack, but believe me, you do not
464 * want a complete NCP layer in the kernel, and this is
469 if (*((char*)(ipx
+1)) == 0x22 && *((char*)(ipx
+1)+1) == 0x22)
472 * The packet is a NCP request
474 connection
= ( ((int) *((char*)(ipx
+1)+5)) << 8 )
475 | (int) *((char*)(ipx
+1)+3);
477 else if (*((char*)(ipx
+1))== 0x77 && *((char*)(ipx
+1)+1) == 0x77)
480 * The packet is a BURST packet
482 connection
= ( ((int) *((char*)(ipx
+1)+9)) << 8 )
483 | (int) *((char*)(ipx
+1)+8);
489 * Now we have to look for a special NCP connection handling
490 * socket. Only these sockets have ipx_ncp_conn != 0, set
493 for (sock1
=intrfc
->if_sklist
;
495 (sock1
->protinfo
.af_ipx
.ipx_ncp_conn
!= connection
);
501 /* No special socket found, forward the packet the
504 sock1
= ipxitf_find_socket(intrfc
, ipx
->ipx_dest
.sock
);
508 * We need to check if there is a primary net and if
509 * this is addressed to one of the *SPECIAL* sockets because
510 * these need to be propagated to the primary net.
511 * The *SPECIAL* socket list contains: 0x452(SAP), 0x453(RIP) and
515 if(ipx_primary_net
&& (intrfc
!= ipx_primary_net
))
517 switch(ntohs(ipx
->ipx_dest
.sock
))
523 * The appropriate thing to do here is to
524 * dup the packet and route to the primary net
525 * interface via ipxitf_send; however, we'll
526 * cheat and just demux it here.
528 sock2
= ipxitf_find_socket(ipx_primary_net
,
537 * If there is nothing to do return. The kfree will cancel any charging.
539 if(sock1
== NULL
&& sock2
== NULL
)
547 * This next segment of code is a little awkward, but it sets it up
548 * so that the appropriate number of copies of the SKB are made and
549 * that skb1 and skb2 point to it (them) so that it (they) can be
550 * demuxed to sock1 and/or sock2. If we are unable to make enough
551 * copies, we do as much as is possible.
555 skb1
= skb_clone(skb
, GFP_ATOMIC
);
562 /* Do we need 2 SKBs? */
564 skb2
= skb_clone(skb1
, GFP_ATOMIC
);
569 (void) ipxitf_def_skb_handler(sock1
, skb1
);
575 (void) ipxitf_def_skb_handler(sock2
, skb2
);
579 #endif /* CONFIG_IPX_INTERN */
581 static struct sk_buff
*ipxitf_adjust_skbuff(ipx_interface
*intrfc
, struct sk_buff
*skb
)
583 struct sk_buff
*skb2
;
584 int in_offset
= skb
->h
.raw
- skb
->head
;
585 int out_offset
= intrfc
->if_ipx_offset
;
588 /* Hopefully, most cases */
589 if(in_offset
>= out_offset
)
593 len
= skb
->len
+ out_offset
;
594 skb2
= alloc_skb(len
, GFP_ATOMIC
);
597 skb_reserve(skb2
, out_offset
);
599 skb2
->h
.raw
= skb_put(skb2
,skb
->len
);
600 memcpy(skb2
->h
.raw
, skb
->h
.raw
, skb
->len
);
607 static int ipxitf_send(ipx_interface
*intrfc
, struct sk_buff
*skb
, char *node
)
609 struct ipxhdr
*ipx
= skb
->nh
.ipxh
;
610 struct net_device
*dev
= intrfc
->if_dev
;
611 struct datalink_proto
*dl
= intrfc
->if_dlink
;
612 char dest_node
[IPX_NODE_LEN
];
613 int send_to_wire
= 1;
617 * We need to know how many skbuffs it will take to send out this
618 * packet to avoid unnecessary copies.
621 if((dl
== NULL
) || (dev
== NULL
) || (dev
->flags
& IFF_LOOPBACK
))
622 send_to_wire
= 0; /* No non looped */
625 * See if this should be demuxed to sockets on this interface
627 * We want to ensure the original was eaten or that we only use
631 if(ipx
->ipx_dest
.net
== intrfc
->if_netnum
)
634 * To our own node, loop and free the original.
635 * The internal net will receive on all node address.
637 if((intrfc
== ipx_internal_net
)
638 || memcmp(intrfc
->if_node
, node
, IPX_NODE_LEN
) == 0)
640 /* Don't charge sender */
643 /* Will charge receiver */
644 return (ipxitf_demux_socket(intrfc
, skb
, 0));
647 /* Broadcast, loop and possibly keep to send on. */
648 if(memcmp(ipx_broadcast_node
, node
, IPX_NODE_LEN
) == 0)
652 ipxitf_demux_socket(intrfc
, skb
, send_to_wire
);
659 * If the originating net is not equal to our net; this is routed
660 * We are still charging the sender. Which is right - the driver
661 * free will handle this fairly.
663 if(ipx
->ipx_source
.net
!= intrfc
->if_netnum
)
666 * Unshare the buffer before modifying the count in
667 * case its a flood or tcpdump
669 skb
= skb_unshare(skb
, GFP_ATOMIC
);
672 if(++(ipx
->ipx_tctrl
) > ipxcfg_max_hops
)
682 /* Determine the appropriate hardware address */
683 addr_len
= dev
->addr_len
;
684 if(memcmp(ipx_broadcast_node
, node
, IPX_NODE_LEN
) == 0)
685 memcpy(dest_node
, dev
->broadcast
, addr_len
);
687 memcpy(dest_node
, &(node
[IPX_NODE_LEN
-addr_len
]), addr_len
);
689 /* Make any compensation for differing physical/data link size */
690 skb
= ipxitf_adjust_skbuff(intrfc
, skb
);
694 /* set up data link and physical headers */
696 skb
->protocol
= htons(ETH_P_IPX
);
697 dl
->datalink_header(dl
, skb
, dest_node
);
705 static int ipxrtr_add_route(__u32
, ipx_interface
*, unsigned char *);
707 static int ipxitf_add_local_route(ipx_interface
*intrfc
)
709 return (ipxrtr_add_route(intrfc
->if_netnum
, intrfc
, NULL
));
712 static const char * ipx_frame_name(unsigned short);
713 static const char * ipx_device_name(ipx_interface
*);
715 static int ipxitf_rcv(ipx_interface
*intrfc
, struct sk_buff
*skb
)
717 struct ipxhdr
*ipx
= skb
->nh
.ipxh
;
720 /* See if we should update our network number */
721 if(!intrfc
->if_netnum
/* net number of intrfc not known yet (== 0) */
722 && (ipx
->ipx_source
.net
== ipx
->ipx_dest
.net
) /* intra packet */
723 && ipx
->ipx_source
.net
) /* source net number of packet != 0 */
725 /* NB: NetWare servers lie about their hop count so we
726 * dropped the test based on it. This is the best way
727 * to determine this is a 0 hop count packet.
729 if((i
=ipxitf_find_using_net(ipx
->ipx_source
.net
)) == NULL
)
731 intrfc
->if_netnum
= ipx
->ipx_source
.net
;
732 (void) ipxitf_add_local_route(intrfc
);
736 printk(KERN_WARNING
"IPX: Network number collision %lx\n %s %s and %s %s\n",
737 (long unsigned int) htonl(ipx
->ipx_source
.net
),
739 ipx_frame_name(i
->if_dlink_type
),
740 ipx_device_name(intrfc
),
741 ipx_frame_name(intrfc
->if_dlink_type
));
745 if(ipx
->ipx_type
== IPX_TYPE_PPROP
746 && ipx
->ipx_tctrl
< 8
747 && skb
->pkt_type
!= PACKET_OTHERHOST
748 /* header + 8 network numbers */
749 && ntohs(ipx
->ipx_pktsize
) >= sizeof(struct ipxhdr
) + 8 * 4)
753 struct sk_buff
*skb2
;
757 c
= (char *) skb
->data
;
758 c
+= sizeof(struct ipxhdr
);
763 /* Dump packet if already seen this net */
764 for( ; i
< ipx
->ipx_tctrl
; i
++)
765 if(*l
++ == intrfc
->if_netnum
)
768 if(i
== ipx
->ipx_tctrl
)
770 /* < 8 hops && input itfc not in list */
771 *l
= intrfc
->if_netnum
; /* insert recvd netnum into list */
773 /* xmit on all other interfaces... */
774 for(ifcs
= ipx_interfaces
; ifcs
!= NULL
; ifcs
= ifcs
->if_next
)
776 /* Except unconfigured interfaces */
777 if(ifcs
->if_netnum
== 0)
780 /* That aren't in the list */
782 for(i
= 0; i
<= ipx
->ipx_tctrl
; i
++)
783 if(ifcs
->if_netnum
== *l
++)
785 if(i
- 1 == ipx
->ipx_tctrl
)
787 ipx
->ipx_dest
.net
= ifcs
->if_netnum
;
788 skb2
=skb_clone(skb
, GFP_ATOMIC
);
789 ipxrtr_route_skb(skb2
);
793 /* Reset network number in packet */
794 ipx
->ipx_dest
.net
= intrfc
->if_netnum
;
798 if(!ipx
->ipx_dest
.net
)
799 ipx
->ipx_dest
.net
= intrfc
->if_netnum
;
800 if(!ipx
->ipx_source
.net
)
801 ipx
->ipx_source
.net
= intrfc
->if_netnum
;
803 if(intrfc
->if_netnum
!= ipx
->ipx_dest
.net
)
805 /* We only route point-to-point packets. */
806 if(skb
->pkt_type
== PACKET_HOST
)
808 skb
=skb_unshare(skb
, GFP_ATOMIC
);
810 return (ipxrtr_route_skb(skb
));
819 /* see if we should keep it */
820 if((memcmp(ipx_broadcast_node
, ipx
->ipx_dest
.node
, IPX_NODE_LEN
) == 0)
821 || (memcmp(intrfc
->if_node
, ipx
->ipx_dest
.node
, IPX_NODE_LEN
) == 0))
823 return (ipxitf_demux_socket(intrfc
, skb
, 0));
826 /* we couldn't pawn it off so unload it */
831 static void ipxitf_insert(ipx_interface
*intrfc
)
835 intrfc
->if_next
= NULL
;
836 if(ipx_interfaces
== NULL
)
837 ipx_interfaces
= intrfc
;
840 for(i
= ipx_interfaces
; i
->if_next
!= NULL
; i
= i
->if_next
)
845 if(ipxcfg_auto_select_primary
&& (ipx_primary_net
== NULL
))
846 ipx_primary_net
= intrfc
;
853 static int ipxitf_create_internal(ipx_interface_definition
*idef
)
855 ipx_interface
*intrfc
;
857 /* Only one primary network allowed */
858 if(ipx_primary_net
!= NULL
)
861 /* Must have a valid network number */
862 if(!idef
->ipx_network
)
863 return (-EADDRNOTAVAIL
);
864 if(ipxitf_find_using_net(idef
->ipx_network
) != NULL
)
865 return (-EADDRINUSE
);
867 intrfc
= (ipx_interface
*)kmalloc(sizeof(ipx_interface
),GFP_ATOMIC
);
870 intrfc
->if_dev
= NULL
;
871 intrfc
->if_netnum
= idef
->ipx_network
;
872 intrfc
->if_dlink_type
= 0;
873 intrfc
->if_dlink
= NULL
;
874 intrfc
->if_sklist
= NULL
;
875 intrfc
->if_internal
= 1;
876 intrfc
->if_ipx_offset
= 0;
877 intrfc
->if_sknum
= IPX_MIN_EPHEMERAL_SOCKET
;
878 memcpy((char *)&(intrfc
->if_node
), idef
->ipx_node
, IPX_NODE_LEN
);
879 ipx_internal_net
= intrfc
;
880 ipx_primary_net
= intrfc
;
881 ipxitf_insert(intrfc
);
883 return (ipxitf_add_local_route(intrfc
));
886 static int ipx_map_frame_type(unsigned char type
)
890 case IPX_FRAME_ETHERII
:
891 return (htons(ETH_P_IPX
));
894 return (htons(ETH_P_802_2
));
897 return (htons(ETH_P_SNAP
));
900 return (htons(ETH_P_802_3
));
906 static int ipxitf_create(ipx_interface_definition
*idef
)
908 struct net_device
*dev
;
909 unsigned short dlink_type
= 0;
910 struct datalink_proto
*datalink
= NULL
;
911 ipx_interface
*intrfc
;
913 if(idef
->ipx_special
== IPX_INTERNAL
)
914 return (ipxitf_create_internal(idef
));
916 if((idef
->ipx_special
== IPX_PRIMARY
) && (ipx_primary_net
!= NULL
))
920 && (ipxitf_find_using_net(idef
->ipx_network
) != NULL
))
921 return (-EADDRINUSE
);
923 dev
= __dev_get_by_name(idef
->ipx_device
);
927 switch(idef
->ipx_dlink_type
)
929 case IPX_FRAME_TR_8022
:
930 printk("IPX frame type 802.2TR is obsolete. Use 802.2 instead.\n");
934 dlink_type
= htons(ETH_P_802_2
);
935 datalink
= p8022_datalink
;
938 case IPX_FRAME_ETHERII
:
939 if (dev
->type
!= ARPHRD_IEEE802
)
941 dlink_type
= htons(ETH_P_IPX
);
942 datalink
= pEII_datalink
;
946 printk("IPX frame type EtherII over token-ring is obsolete. Use SNAP instead.\n");
950 dlink_type
= htons(ETH_P_SNAP
);
951 datalink
= pSNAP_datalink
;
955 dlink_type
= htons(ETH_P_802_3
);
956 datalink
= p8023_datalink
;
964 if(!(dev
->flags
& IFF_UP
))
967 /* Check addresses are suitable */
968 if(dev
->addr_len
> IPX_NODE_LEN
)
972 return (-EPROTONOSUPPORT
);
974 if((intrfc
= ipxitf_find_using_phys(dev
, dlink_type
)) == NULL
)
977 intrfc
= (ipx_interface
*)kmalloc(sizeof(ipx_interface
),GFP_ATOMIC
);
980 intrfc
->if_dev
= dev
;
981 intrfc
->if_netnum
= idef
->ipx_network
;
982 intrfc
->if_dlink_type
= dlink_type
;
983 intrfc
->if_dlink
= datalink
;
984 intrfc
->if_sklist
= NULL
;
985 intrfc
->if_sknum
= IPX_MIN_EPHEMERAL_SOCKET
;
986 /* Setup primary if necessary */
987 if((idef
->ipx_special
== IPX_PRIMARY
))
988 ipx_primary_net
= intrfc
;
989 intrfc
->if_internal
= 0;
990 intrfc
->if_ipx_offset
= dev
->hard_header_len
+ datalink
->header_length
;
991 if(memcmp(idef
->ipx_node
, "\000\000\000\000\000\000", IPX_NODE_LEN
) == 0)
993 memset(intrfc
->if_node
, 0, IPX_NODE_LEN
);
994 memcpy((char *)&(intrfc
->if_node
[IPX_NODE_LEN
-dev
->addr_len
]),
995 dev
->dev_addr
, dev
->addr_len
);
998 memcpy(intrfc
->if_node
, idef
->ipx_node
, IPX_NODE_LEN
);
999 ipxitf_insert(intrfc
);
1002 /* If the network number is known, add a route */
1003 if(!intrfc
->if_netnum
)
1006 return (ipxitf_add_local_route(intrfc
));
1009 static int ipxitf_delete(ipx_interface_definition
*idef
)
1011 struct net_device
*dev
= NULL
;
1012 unsigned short dlink_type
= 0;
1013 ipx_interface
*intrfc
;
1015 if(idef
->ipx_special
== IPX_INTERNAL
)
1017 if(ipx_internal_net
!= NULL
)
1019 ipxitf_down(ipx_internal_net
);
1025 dlink_type
= ipx_map_frame_type(idef
->ipx_dlink_type
);
1027 return (-EPROTONOSUPPORT
);
1029 dev
= __dev_get_by_name(idef
->ipx_device
);
1033 intrfc
= ipxitf_find_using_phys(dev
, dlink_type
);
1036 ipxitf_down(intrfc
);
1043 static ipx_interface
*ipxitf_auto_create(struct net_device
*dev
,
1044 unsigned short dlink_type
)
1046 struct datalink_proto
*datalink
= NULL
;
1047 ipx_interface
*intrfc
;
1049 switch(htons(dlink_type
))
1052 datalink
= pEII_datalink
;
1056 datalink
= p8022_datalink
;
1060 datalink
= pSNAP_datalink
;
1064 datalink
= p8023_datalink
;
1074 /* Check addresses are suitable */
1075 if(dev
->addr_len
>IPX_NODE_LEN
)
1078 intrfc
= (ipx_interface
*)kmalloc(sizeof(ipx_interface
),GFP_ATOMIC
);
1081 intrfc
->if_dev
= dev
;
1082 intrfc
->if_netnum
= 0;
1083 intrfc
->if_dlink_type
= dlink_type
;
1084 intrfc
->if_dlink
= datalink
;
1085 intrfc
->if_sklist
= NULL
;
1086 intrfc
->if_internal
= 0;
1087 intrfc
->if_sknum
= IPX_MIN_EPHEMERAL_SOCKET
;
1088 intrfc
->if_ipx_offset
= dev
->hard_header_len
+
1089 datalink
->header_length
;
1090 memset(intrfc
->if_node
, 0, IPX_NODE_LEN
);
1091 memcpy((char *)&(intrfc
->if_node
[IPX_NODE_LEN
-dev
->addr_len
]),
1092 dev
->dev_addr
, dev
->addr_len
);
1093 ipxitf_insert(intrfc
);
1099 static int ipxitf_ioctl(unsigned int cmd
, void *arg
)
1108 struct sockaddr_ipx
*sipx
;
1109 ipx_interface_definition f
;
1111 if(copy_from_user(&ifr
, arg
, sizeof(ifr
)))
1114 sipx
= (struct sockaddr_ipx
*)&ifr
.ifr_addr
;
1115 if(sipx
->sipx_family
!= AF_IPX
)
1118 f
.ipx_network
= sipx
->sipx_network
;
1119 memcpy(f
.ipx_device
,ifr
.ifr_name
,sizeof(f
.ipx_device
));
1120 memcpy(f
.ipx_node
, sipx
->sipx_node
, IPX_NODE_LEN
);
1121 f
.ipx_dlink_type
= sipx
->sipx_type
;
1122 f
.ipx_special
= sipx
->sipx_special
;
1124 if(sipx
->sipx_action
== IPX_DLTITF
)
1125 return (ipxitf_delete(&f
));
1127 return (ipxitf_create(&f
));
1132 struct sockaddr_ipx
*sipx
;
1133 ipx_interface
*ipxif
;
1134 struct net_device
*dev
;
1136 if(copy_from_user(&ifr
, arg
, sizeof(ifr
)))
1139 sipx
= (struct sockaddr_ipx
*)&ifr
.ifr_addr
;
1140 dev
= __dev_get_by_name(ifr
.ifr_name
);
1144 ipxif
= ipxitf_find_using_phys(dev
, ipx_map_frame_type(sipx
->sipx_type
));
1146 return (-EADDRNOTAVAIL
);
1148 sipx
->sipx_family
= AF_IPX
;
1149 sipx
->sipx_network
= ipxif
->if_netnum
;
1150 memcpy(sipx
->sipx_node
, ipxif
->if_node
, sizeof(sipx
->sipx_node
));
1152 if(!copy_to_user(arg
, &ifr
, sizeof(ifr
)))
1158 case SIOCAIPXITFCRT
:
1160 err
= get_user(val
, (unsigned char *) arg
);
1164 return (ipxcfg_set_auto_create(val
));
1167 case SIOCAIPXPRISLT
:
1169 err
= get_user(val
, (unsigned char *) arg
);
1173 return (ipxcfg_set_auto_select(val
));
1181 /**************************************************************************\
1183 * Routing tables for the IPX socket layer. *
1185 \**************************************************************************/
1187 static ipx_route
*ipxrtr_lookup(__u32 net
)
1191 for(r
= ipx_routes
; (r
!= NULL
) && (r
->ir_net
!= net
); r
= r
->ir_next
)
1197 static int ipxrtr_add_route(__u32 network
, ipx_interface
*intrfc
, unsigned char *node
)
1201 /* Get a route structure; either existing or create */
1202 rt
= ipxrtr_lookup(network
);
1205 rt
= (ipx_route
*)kmalloc(sizeof(ipx_route
),GFP_ATOMIC
);
1208 rt
->ir_next
= ipx_routes
;
1211 else if(intrfc
== ipx_internal_net
)
1214 rt
->ir_net
= network
;
1215 rt
->ir_intrfc
= intrfc
;
1218 memset(rt
->ir_router_node
, '\0', IPX_NODE_LEN
);
1223 memcpy(rt
->ir_router_node
, node
, IPX_NODE_LEN
);
1230 static void ipxrtr_del_routes(ipx_interface
*intrfc
)
1232 ipx_route
**r
, *tmp
;
1234 for(r
= &ipx_routes
; (tmp
= *r
) != NULL
;)
1236 if(tmp
->ir_intrfc
== intrfc
)
1239 kfree_s(tmp
, sizeof(ipx_route
));
1242 r
= &(tmp
->ir_next
);
1246 static int ipxrtr_create(ipx_route_definition
*rd
)
1248 ipx_interface
*intrfc
;
1250 /* Find the appropriate interface */
1251 intrfc
= ipxitf_find_using_net(rd
->ipx_router_network
);
1253 return (-ENETUNREACH
);
1255 return (ipxrtr_add_route(rd
->ipx_network
, intrfc
, rd
->ipx_router_node
));
1258 static int ipxrtr_delete(long net
)
1263 for(r
= &ipx_routes
; (tmp
= *r
) != NULL
;)
1265 if(tmp
->ir_net
== net
)
1267 /* Directly connected; can't lose route */
1268 if(!(tmp
->ir_routed
))
1272 kfree_s(tmp
, sizeof(ipx_route
));
1276 r
= &(tmp
->ir_next
);
1283 * Checksum routine for IPX
1286 /* Note: We assume ipx_tctrl==0 and htons(length)==ipx_pktsize */
1288 static __u16
ipx_set_checksum(struct ipxhdr
*packet
,int length
)
1291 * NOTE: sum is a net byte order quantity, which optimizes the
1292 * loop. This only works on big and little endian machines. (I
1293 * don't know of a machine that isn't.)
1298 /* Pointer to second word - We skip the checksum field */
1299 __u16
*p
= (__u16
*)&packet
->ipx_pktsize
;
1301 /* Number of complete words */
1302 __u32 i
= length
>> 1;
1303 char hops
= packet
->ipx_tctrl
;
1305 /* Hop count excluded from checksum calc */
1306 packet
->ipx_tctrl
= 0;
1308 /* Loop through all complete words except the checksum field */
1312 /* Add on the last part word if it exists */
1313 if(packet
->ipx_pktsize
& htons(1))
1314 sum
+= ntohs(0xff00) & *p
;
1316 packet
->ipx_tctrl
= hops
;
1318 /* Do final fixup */
1319 sum
= (sum
& 0xffff) + (sum
>> 16);
1321 /* It's a pity there's no concept of carry in C */
1329 * Route an outgoing frame from a socket.
1331 static int ipxrtr_route_packet(struct sock
*sk
, struct sockaddr_ipx
*usipx
, struct iovec
*iov
, int len
, int noblock
)
1333 struct sk_buff
*skb
;
1334 ipx_interface
*intrfc
;
1338 ipx_route
*rt
= NULL
;
1341 /* Find the appropriate interface on which to send packet */
1342 if(!usipx
->sipx_network
&& (ipx_primary_net
!= NULL
))
1344 usipx
->sipx_network
= ipx_primary_net
->if_netnum
;
1345 intrfc
= ipx_primary_net
;
1349 rt
= ipxrtr_lookup(usipx
->sipx_network
);
1351 return (-ENETUNREACH
);
1353 intrfc
= rt
->ir_intrfc
;
1356 ipx_offset
= intrfc
->if_ipx_offset
;
1357 size
= sizeof(struct ipxhdr
) + len
;
1360 skb
= sock_alloc_send_skb(sk
, size
, 0, noblock
, &err
);
1364 skb_reserve(skb
,ipx_offset
);
1367 /* Fill in IPX header */
1368 ipx
= (struct ipxhdr
*)skb_put(skb
, sizeof(struct ipxhdr
));
1369 ipx
->ipx_pktsize
= htons(len
+ sizeof(struct ipxhdr
));
1371 ipx
->ipx_type
= usipx
->sipx_type
;
1372 skb
->h
.raw
= (void *)skb
->nh
.ipxh
= ipx
;
1374 ipx
->ipx_source
.net
= sk
->protinfo
.af_ipx
.intrfc
->if_netnum
;
1376 #ifdef CONFIG_IPX_INTERN
1377 memcpy(ipx
->ipx_source
.node
, sk
->protinfo
.af_ipx
.node
, IPX_NODE_LEN
);
1379 if((err
= ntohs(sk
->protinfo
.af_ipx
.port
)) == 0x453 || err
== 0x452)
1381 /* RIP/SAP special handling for mars_nwe */
1382 ipx
->ipx_source
.net
= intrfc
->if_netnum
;
1383 memcpy(ipx
->ipx_source
.node
, intrfc
->if_node
, IPX_NODE_LEN
);
1387 ipx
->ipx_source
.net
= sk
->protinfo
.af_ipx
.intrfc
->if_netnum
;
1388 memcpy(ipx
->ipx_source
.node
, sk
->protinfo
.af_ipx
.intrfc
->if_node
, IPX_NODE_LEN
);
1390 #endif /* CONFIG_IPX_INTERN */
1392 ipx
->ipx_source
.sock
= sk
->protinfo
.af_ipx
.port
;
1393 ipx
->ipx_dest
.net
= usipx
->sipx_network
;
1394 memcpy(ipx
->ipx_dest
.node
,usipx
->sipx_node
,IPX_NODE_LEN
);
1395 ipx
->ipx_dest
.sock
= usipx
->sipx_port
;
1397 err
= memcpy_fromiovec(skb_put(skb
,len
),iov
,len
);
1404 /* Apply checksum. Not allowed on 802.3 links. */
1405 if(sk
->no_check
|| intrfc
->if_dlink_type
== IPX_FRAME_8023
)
1406 ipx
->ipx_checksum
=0xFFFF;
1408 ipx
->ipx_checksum
= ipx_set_checksum(ipx
, len
+ sizeof(struct ipxhdr
));
1410 return (ipxitf_send(intrfc
, skb
, (rt
&& rt
->ir_routed
) ?
1411 rt
->ir_router_node
: ipx
->ipx_dest
.node
));
1414 int ipxrtr_route_skb(struct sk_buff
*skb
)
1416 struct ipxhdr
*ipx
= skb
->nh
.ipxh
;
1420 r
= ipxrtr_lookup(ipx
->ipx_dest
.net
);
1421 if(r
== NULL
) /* no known route */
1428 (void)ipxitf_send(i
, skb
, (r
->ir_routed
) ?
1429 r
->ir_router_node
: ipx
->ipx_dest
.node
);
1435 * We use a normal struct rtentry for route handling
1437 static int ipxrtr_ioctl(unsigned int cmd
, void *arg
)
1439 struct rtentry rt
; /* Use these to behave like 'other' stacks */
1440 struct sockaddr_ipx
*sg
,*st
;
1443 err
= copy_from_user(&rt
,arg
,sizeof(rt
));
1447 sg
= (struct sockaddr_ipx
*)&rt
.rt_gateway
;
1448 st
= (struct sockaddr_ipx
*)&rt
.rt_dst
;
1450 if(!(rt
.rt_flags
& RTF_GATEWAY
))
1451 return (-EINVAL
); /* Direct routes are fixed */
1452 if(sg
->sipx_family
!= AF_IPX
)
1454 if(st
->sipx_family
!= AF_IPX
)
1460 return (ipxrtr_delete(st
->sipx_network
));
1464 struct ipx_route_definition f
;
1465 f
.ipx_network
=st
->sipx_network
;
1466 f
.ipx_router_network
=sg
->sipx_network
;
1467 memcpy(f
.ipx_router_node
, sg
->sipx_node
, IPX_NODE_LEN
);
1468 return (ipxrtr_create(&f
));
1476 static const char *ipx_frame_name(unsigned short frame
)
1478 switch(ntohs(frame
))
1492 case ETH_P_TR_802_2
:
1500 static const char *ipx_device_name(ipx_interface
*intrfc
)
1502 return (intrfc
->if_internal
? "Internal" :
1503 (intrfc
->if_dev
? intrfc
->if_dev
->name
: "Unknown"));
1506 /* Called from proc fs */
1507 static int ipx_interface_get_info(char *buffer
, char **start
, off_t offset
,
1508 int length
, int dummy
)
1511 off_t begin
= 0, pos
= 0;
1514 /* Theory.. Keep printing in the same place until we pass offset */
1516 len
+= sprintf(buffer
,"%-11s%-15s%-9s%-11s%s\n", "Network",
1517 "Node_Address", "Primary", "Device", "Frame_Type");
1518 for(i
= ipx_interfaces
; i
!= NULL
; i
= i
->if_next
)
1520 len
+= sprintf(buffer
+len
, "%08lX ", (long unsigned int)ntohl(i
->if_netnum
));
1521 len
+= sprintf(buffer
+len
,"%02X%02X%02X%02X%02X%02X ",
1522 i
->if_node
[0], i
->if_node
[1], i
->if_node
[2],
1523 i
->if_node
[3], i
->if_node
[4], i
->if_node
[5]);
1524 len
+= sprintf(buffer
+len
, "%-9s", (i
== ipx_primary_net
) ?
1526 len
+= sprintf(buffer
+len
, "%-11s", ipx_device_name(i
));
1527 len
+= sprintf(buffer
+len
, "%s\n",
1528 ipx_frame_name(i
->if_dlink_type
));
1530 /* Are we still dumping unwanted data then discard the record */
1535 len
= 0; /* Keep dumping into the buffer start */
1538 if(pos
> offset
+ length
) /* We have dumped enough */
1542 /* The data in question runs from begin to begin+len */
1543 *start
= buffer
+ (offset
- begin
); /* Start of wanted data */
1544 len
-= (offset
- begin
); /* Remove unwanted header data from length */
1546 len
= length
; /* Remove unwanted tail data from length */
1551 static int ipx_get_info(char *buffer
, char **start
, off_t offset
,
1552 int length
, int dummy
)
1556 off_t begin
= 0, pos
= 0;
1559 /* Theory.. Keep printing in the same place until we pass offset */
1561 #ifdef CONFIG_IPX_INTERN
1562 len
+= sprintf(buffer
,"%-28s%-28s%-10s%-10s%-7s%s\n", "Local_Address",
1564 len
+= sprintf(buffer
,"%-15s%-28s%-10s%-10s%-7s%s\n", "Local_Address",
1565 #endif /* CONFIG_IPX_INTERN */
1566 "Remote_Address", "Tx_Queue", "Rx_Queue",
1569 for(i
= ipx_interfaces
; i
!= NULL
; i
= i
->if_next
)
1571 for(s
= i
->if_sklist
; s
!= NULL
; s
= s
->next
)
1573 #ifdef CONFIG_IPX_INTERN
1574 len
+= sprintf(buffer
+len
,
1575 "%08lX:%02X%02X%02X%02X%02X%02X:%04X ",
1576 (long unsigned int) htonl(s
->protinfo
.af_ipx
.intrfc
->if_netnum
),
1577 s
->protinfo
.af_ipx
.node
[0],
1578 s
->protinfo
.af_ipx
.node
[1],
1579 s
->protinfo
.af_ipx
.node
[2],
1580 s
->protinfo
.af_ipx
.node
[3],
1581 s
->protinfo
.af_ipx
.node
[4],
1582 s
->protinfo
.af_ipx
.node
[5],
1583 htons(s
->protinfo
.af_ipx
.port
));
1585 len
+= sprintf(buffer
+len
,"%08lX:%04X ",
1586 htonl(i
->if_netnum
),
1587 htons(s
->protinfo
.af_ipx
.port
));
1588 #endif /* CONFIG_IPX_INTERN */
1590 if(s
->state
!= TCP_ESTABLISHED
)
1591 len
+= sprintf(buffer
+len
, "%-28s", "Not_Connected");
1594 len
+= sprintf(buffer
+len
,
1595 "%08lX:%02X%02X%02X%02X%02X%02X:%04X ",
1596 (long unsigned int) htonl(s
->protinfo
.af_ipx
.dest_addr
.net
),
1597 s
->protinfo
.af_ipx
.dest_addr
.node
[0],
1598 s
->protinfo
.af_ipx
.dest_addr
.node
[1],
1599 s
->protinfo
.af_ipx
.dest_addr
.node
[2],
1600 s
->protinfo
.af_ipx
.dest_addr
.node
[3],
1601 s
->protinfo
.af_ipx
.dest_addr
.node
[4],
1602 s
->protinfo
.af_ipx
.dest_addr
.node
[5],
1603 htons(s
->protinfo
.af_ipx
.dest_addr
.sock
));
1606 len
+= sprintf(buffer
+len
,"%08X %08X ",
1607 atomic_read(&s
->wmem_alloc
),
1608 atomic_read(&s
->rmem_alloc
));
1609 len
+= sprintf(buffer
+len
,"%02X %03d\n",
1610 s
->state
, SOCK_INODE(s
->socket
)->i_uid
);
1619 if(pos
> offset
+ length
) /* We have dumped enough */
1624 /* The data in question runs from begin to begin+len */
1625 *start
= buffer
+ (offset
-begin
);
1626 len
-= (offset
- begin
);
1633 static int ipx_rt_get_info(char *buffer
, char **start
, off_t offset
,
1634 int length
, int dummy
)
1637 off_t begin
= 0, pos
= 0;
1640 len
+= sprintf(buffer
,"%-11s%-13s%s\n",
1641 "Network", "Router_Net", "Router_Node");
1642 for(rt
= ipx_routes
; rt
!= NULL
; rt
= rt
->ir_next
)
1644 len
+= sprintf(buffer
+len
,"%08lX ", (long unsigned int) ntohl(rt
->ir_net
));
1647 len
+= sprintf(buffer
+len
,"%08lX %02X%02X%02X%02X%02X%02X\n",
1648 (long unsigned int) ntohl(rt
->ir_intrfc
->if_netnum
),
1649 rt
->ir_router_node
[0], rt
->ir_router_node
[1],
1650 rt
->ir_router_node
[2], rt
->ir_router_node
[3],
1651 rt
->ir_router_node
[4], rt
->ir_router_node
[5]);
1655 len
+= sprintf(buffer
+len
, "%-13s%s\n",
1656 "Directly", "Connected");
1666 if(pos
> offset
+ length
)
1670 *start
= buffer
+ (offset
- begin
);
1671 len
-= (offset
- begin
);
1678 /**************************************************************************\
1680 * Handling for system calls applied via the various interfaces to an *
1681 * IPX socket object. *
1683 \**************************************************************************/
1685 static int ipx_setsockopt(struct socket
*sock
, int level
, int optname
, char *optval
, int optlen
)
1692 if(optlen
!= sizeof(int))
1695 err
= get_user(opt
, (unsigned int *)optval
);
1705 sk
->protinfo
.af_ipx
.type
= opt
;
1709 return (-ENOPROTOOPT
);
1714 return (-ENOPROTOOPT
);
1718 static int ipx_getsockopt(struct socket
*sock
, int level
, int optname
,
1719 char *optval
, int *optlen
)
1733 val
= sk
->protinfo
.af_ipx
.type
;
1737 return (-ENOPROTOOPT
);
1742 return (-ENOPROTOOPT
);
1745 if(get_user(len
, optlen
))
1748 len
= min(len
, sizeof(int));
1749 if(put_user(len
, optlen
))
1752 if(copy_to_user(optval
, &val
, len
))
1758 static int ipx_create(struct socket
*sock
, int protocol
)
1765 sk
= sk_alloc(PF_IPX
, GFP_KERNEL
, 1);
1768 sock
->ops
= &ipx_dgram_ops
;
1771 case SOCK_SEQPACKET
:
1773 * From this point on SPX sockets are handled
1774 * by af_spx.c and the methods replaced.
1777 return (spx_family_ops
->create(sock
,protocol
));
1778 /* Fall through if SPX is not loaded */
1779 case SOCK_STREAM
: /* Allow higher levels to piggyback */
1781 return (-ESOCKTNOSUPPORT
);
1784 sock_init_data(sock
, sk
);
1785 sk
->destruct
= NULL
;
1786 sk
->no_check
= 1; /* Checksum off by default */
1793 static int ipx_release(struct socket
*sock
)
1795 struct sock
*sk
= sock
->sk
;
1801 sk
->state_change(sk
);
1805 ipx_destroy_socket(sk
);
1807 if(sock
->type
== SOCK_DGRAM
)
1813 static unsigned short ipx_first_free_socketnum(ipx_interface
*intrfc
)
1815 unsigned short socketNum
= intrfc
->if_sknum
;
1817 if(socketNum
< IPX_MIN_EPHEMERAL_SOCKET
)
1818 socketNum
= IPX_MIN_EPHEMERAL_SOCKET
;
1820 while(ipxitf_find_socket(intrfc
, ntohs(socketNum
)) != NULL
)
1822 if(socketNum
> IPX_MAX_EPHEMERAL_SOCKET
)
1823 socketNum
= IPX_MIN_EPHEMERAL_SOCKET
;
1828 intrfc
->if_sknum
= socketNum
;
1830 return (ntohs(socketNum
));
1833 static int ipx_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
1836 ipx_interface
*intrfc
;
1837 struct sockaddr_ipx
*addr
= (struct sockaddr_ipx
*)uaddr
;
1844 if(addr_len
!= sizeof(struct sockaddr_ipx
))
1847 intrfc
= ipxitf_find_using_net(addr
->sipx_network
);
1849 return (-EADDRNOTAVAIL
);
1851 if(addr
->sipx_port
== 0)
1853 addr
->sipx_port
= ipx_first_free_socketnum(intrfc
);
1854 if(addr
->sipx_port
== 0)
1858 /* protect IPX system stuff like routing/sap */
1859 if(ntohs(addr
->sipx_port
) < IPX_MIN_EPHEMERAL_SOCKET
&& !capable(CAP_NET_ADMIN
))
1862 sk
->protinfo
.af_ipx
.port
= addr
->sipx_port
;
1864 #ifdef CONFIG_IPX_INTERN
1865 if(intrfc
== ipx_internal_net
)
1867 /* The source address is to be set explicitly if the
1868 * socket is to be bound on the internal network. If a
1869 * node number 0 was specified, the default is used.
1872 if(memcmp(addr
->sipx_node
,ipx_broadcast_node
,IPX_NODE_LEN
) == 0)
1874 if(memcmp(addr
->sipx_node
, ipx_this_node
, IPX_NODE_LEN
) == 0)
1876 memcpy(sk
->protinfo
.af_ipx
.node
, intrfc
->if_node
,
1881 memcpy(sk
->protinfo
.af_ipx
.node
, addr
->sipx_node
, IPX_NODE_LEN
);
1884 if(ipxitf_find_internal_socket(intrfc
,
1885 sk
->protinfo
.af_ipx
.node
,
1886 sk
->protinfo
.af_ipx
.port
) != NULL
)
1889 "IPX: bind failed because port %X in use.\n",
1890 ntohs((int)addr
->sipx_port
));
1891 return (-EADDRINUSE
);
1896 /* Source addresses are easy. It must be our
1897 * network:node pair for an interface routed to IPX
1898 * with the ipx routing ioctl()
1901 memcpy(sk
->protinfo
.af_ipx
.node
, intrfc
->if_node
,
1904 if(ipxitf_find_socket(intrfc
, addr
->sipx_port
) != NULL
)
1907 "IPX: bind failed because port %X in use.\n",
1908 ntohs((int)addr
->sipx_port
));
1909 return (-EADDRINUSE
);
1913 #else /* !def CONFIG_IPX_INTERN */
1915 /* Source addresses are easy. It must be our network:node pair for
1916 an interface routed to IPX with the ipx routing ioctl() */
1918 if(ipxitf_find_socket(intrfc
, addr
->sipx_port
) != NULL
)
1920 SOCK_DEBUG(sk
, "IPX: bind failed because port %X in use.\n",
1921 ntohs((int)addr
->sipx_port
));
1922 return (-EADDRINUSE
);
1925 #endif /* CONFIG_IPX_INTERN */
1927 ipxitf_insert_socket(intrfc
, sk
);
1929 SOCK_DEBUG(sk
, "IPX: bound socket 0x%04X.\n", ntohs(addr
->sipx_port
) );
1934 static int ipx_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
1935 int addr_len
, int flags
)
1937 struct sock
*sk
= sock
->sk
;
1938 struct sockaddr_ipx
*addr
;
1940 sk
->state
= TCP_CLOSE
;
1941 sock
->state
= SS_UNCONNECTED
;
1943 if(addr_len
!= sizeof(*addr
))
1945 addr
= (struct sockaddr_ipx
*)uaddr
;
1947 /* put the autobinding in */
1948 if(sk
->protinfo
.af_ipx
.port
== 0)
1950 struct sockaddr_ipx uaddr
;
1953 uaddr
.sipx_port
= 0;
1954 uaddr
.sipx_network
= 0;
1956 #ifdef CONFIG_IPX_INTERN
1957 if(sk
->protinfo
.af_ipx
.intrfc
)
1958 memcpy(uaddr
.sipx_node
, sk
->protinfo
.af_ipx
.intrfc
->if_node
,IPX_NODE_LEN
);
1960 return -ENETDOWN
; /* Someone zonked the iface */
1961 #endif /* CONFIG_IPX_INTERN */
1963 ret
= ipx_bind(sock
, (struct sockaddr
*)&uaddr
,
1964 sizeof(struct sockaddr_ipx
));
1969 /* We can either connect to primary network or somewhere we can route to */
1970 if( !(addr
->sipx_network
== 0 && ipx_primary_net
!= NULL
) && ipxrtr_lookup(addr
->sipx_network
) == NULL
)
1971 return (-ENETUNREACH
);
1973 sk
->protinfo
.af_ipx
.dest_addr
.net
= addr
->sipx_network
;
1974 sk
->protinfo
.af_ipx
.dest_addr
.sock
= addr
->sipx_port
;
1975 memcpy(sk
->protinfo
.af_ipx
.dest_addr
.node
,
1976 addr
->sipx_node
,IPX_NODE_LEN
);
1977 sk
->protinfo
.af_ipx
.type
= addr
->sipx_type
;
1979 if(sock
->type
== SOCK_DGRAM
)
1981 sock
->state
= SS_CONNECTED
;
1982 sk
->state
= TCP_ESTABLISHED
;
1989 static int ipx_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1990 int *uaddr_len
, int peer
)
1993 struct sockaddr_ipx sipx
;
1998 *uaddr_len
= sizeof(struct sockaddr_ipx
);
2002 if(sk
->state
!= TCP_ESTABLISHED
)
2005 addr
= &sk
->protinfo
.af_ipx
.dest_addr
;
2006 sipx
.sipx_network
= addr
->net
;
2007 memcpy(sipx
.sipx_node
,addr
->node
,IPX_NODE_LEN
);
2008 sipx
.sipx_port
= addr
->sock
;
2012 if(sk
->protinfo
.af_ipx
.intrfc
!= NULL
)
2014 sipx
.sipx_network
=sk
->protinfo
.af_ipx
.intrfc
->if_netnum
;
2015 #ifdef CONFIG_IPX_INTERN
2016 memcpy(sipx
.sipx_node
, sk
->protinfo
.af_ipx
.node
, IPX_NODE_LEN
);
2018 memcpy(sipx
.sipx_node
, sk
->protinfo
.af_ipx
.intrfc
->if_node
, IPX_NODE_LEN
);
2019 #endif /* CONFIG_IPX_INTERN */
2024 sipx
.sipx_network
= 0;
2025 memset(sipx
.sipx_node
, '\0', IPX_NODE_LEN
);
2028 sipx
.sipx_port
= sk
->protinfo
.af_ipx
.port
;
2031 sipx
.sipx_family
= AF_IPX
;
2032 sipx
.sipx_type
= sk
->protinfo
.af_ipx
.type
;
2033 memcpy(uaddr
,&sipx
,sizeof(sipx
));
2038 int ipx_rcv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
)
2040 /* NULL here for pt means the packet was looped back */
2041 ipx_interface
*intrfc
;
2047 if(ntohs(ipx
->ipx_pktsize
) < sizeof(struct ipxhdr
))
2050 /* Invalid header */
2051 if(ntohs(ipx
->ipx_pktsize
) > skb
->len
)
2055 if (skb
->pkt_type
== PACKET_OTHERHOST
)
2058 if(ipx
->ipx_checksum
!= IPX_NO_CHECKSUM
)
2060 if(ipx_set_checksum(ipx
, ntohs(ipx
->ipx_pktsize
)) != ipx
->ipx_checksum
)
2064 /* Determine what local ipx endpoint this is */
2065 intrfc
= ipxitf_find_using_phys(dev
, pt
->type
);
2068 if(ipxcfg_auto_create_interfaces
2069 && ntohl(ipx
->ipx_dest
.net
) != 0L)
2071 intrfc
= ipxitf_auto_create(dev
, pt
->type
);
2074 if(intrfc
== NULL
) /* Not one of ours */
2078 return (ipxitf_rcv(intrfc
, skb
));
2085 static int ipx_sendmsg(struct socket
*sock
, struct msghdr
*msg
, int len
,
2086 struct scm_cookie
*scm
)
2088 struct sock
*sk
= sock
->sk
;
2089 struct sockaddr_ipx
*usipx
=(struct sockaddr_ipx
*)msg
->msg_name
;
2090 struct sockaddr_ipx local_sipx
;
2092 int flags
= msg
->msg_flags
;
2094 /* Socket gets bound below anyway */
2096 return (-EIO); */ /* Socket not bound */
2097 if(flags
& ~MSG_DONTWAIT
)
2102 if(sk
->protinfo
.af_ipx
.port
== 0)
2104 struct sockaddr_ipx uaddr
;
2107 uaddr
.sipx_port
= 0;
2108 uaddr
.sipx_network
= 0L;
2109 #ifdef CONFIG_IPX_INTERN
2110 if(sk
->protinfo
.af_ipx
.intrfc
)
2111 memcpy(uaddr
.sipx_node
, sk
->protinfo
.af_ipx
.intrfc
2112 ->if_node
,IPX_NODE_LEN
);
2114 return -ENETDOWN
; /* Someone zonked the iface */
2116 ret
= ipx_bind(sock
, (struct sockaddr
*)&uaddr
,
2117 sizeof(struct sockaddr_ipx
));
2122 if(msg
->msg_namelen
< sizeof(*usipx
))
2124 if(usipx
->sipx_family
!= AF_IPX
)
2129 if(sk
->state
!= TCP_ESTABLISHED
)
2133 usipx
->sipx_family
= AF_IPX
;
2134 usipx
->sipx_type
= sk
->protinfo
.af_ipx
.type
;
2135 usipx
->sipx_port
= sk
->protinfo
.af_ipx
.dest_addr
.sock
;
2136 usipx
->sipx_network
= sk
->protinfo
.af_ipx
.dest_addr
.net
;
2137 memcpy(usipx
->sipx_node
,sk
->protinfo
.af_ipx
.dest_addr
.node
,IPX_NODE_LEN
);
2140 retval
= ipxrtr_route_packet(sk
, usipx
, msg
->msg_iov
, len
, flags
&MSG_DONTWAIT
);
2148 static int ipx_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int size
,
2149 int flags
, struct scm_cookie
*scm
)
2151 struct sock
*sk
= sock
->sk
;
2152 struct sockaddr_ipx
*sipx
= (struct sockaddr_ipx
*)msg
->msg_name
;
2153 struct ipxhdr
*ipx
= NULL
;
2154 struct sk_buff
*skb
;
2157 /* put the autobinding in */
2158 if(sk
->protinfo
.af_ipx
.port
== 0)
2160 struct sockaddr_ipx uaddr
;
2163 uaddr
.sipx_port
= 0;
2164 uaddr
.sipx_network
= 0;
2166 #ifdef CONFIG_IPX_INTERN
2167 if(sk
->protinfo
.af_ipx
.intrfc
)
2168 memcpy(uaddr
.sipx_node
, sk
->protinfo
.af_ipx
.intrfc
->if_node
,IPX_NODE_LEN
);
2170 return -ENETDOWN
; /* Someone zonked the iface */
2171 #endif /* CONFIG_IPX_INTERN */
2173 ret
= ipx_bind(sock
, (struct sockaddr
*)&uaddr
,
2174 sizeof(struct sockaddr_ipx
));
2182 skb
= skb_recv_datagram(sk
,flags
&~MSG_DONTWAIT
,flags
&MSG_DONTWAIT
,&err
);
2187 copied
= ntohs(ipx
->ipx_pktsize
) - sizeof(struct ipxhdr
);
2191 msg
->msg_flags
|= MSG_TRUNC
;
2194 err
= skb_copy_datagram_iovec(skb
, sizeof(struct ipxhdr
), msg
->msg_iov
,
2198 sk
->stamp
= skb
->stamp
;
2200 msg
->msg_namelen
= sizeof(*sipx
);
2204 sipx
->sipx_family
= AF_IPX
;
2205 sipx
->sipx_port
= ipx
->ipx_source
.sock
;
2206 memcpy(sipx
->sipx_node
,ipx
->ipx_source
.node
,IPX_NODE_LEN
);
2207 sipx
->sipx_network
= ipx
->ipx_source
.net
;
2208 sipx
->sipx_type
= ipx
->ipx_type
;
2213 skb_free_datagram(sk
, skb
);
2219 static int ipx_ioctl(struct socket
*sock
,unsigned int cmd
, unsigned long arg
)
2222 struct sock
*sk
= sock
->sk
;
2227 amount
= sk
->sndbuf
- atomic_read(&sk
->wmem_alloc
);
2230 return (put_user(amount
, (int *)arg
));
2234 struct sk_buff
*skb
;
2235 /* These two are safe on a single CPU system as only user tasks fiddle here */
2236 if((skb
= skb_peek(&sk
->receive_queue
)) != NULL
)
2237 amount
= skb
->len
- sizeof(struct ipxhdr
);
2238 return (put_user(amount
, (int *)arg
));
2243 if(!capable(CAP_NET_ADMIN
))
2245 return (ipxrtr_ioctl(cmd
,(void *)arg
));
2248 case SIOCAIPXITFCRT
:
2249 case SIOCAIPXPRISLT
:
2250 if(!capable(CAP_NET_ADMIN
))
2254 return (ipxitf_ioctl(cmd
,(void *)arg
));
2256 case SIOCIPXCFGDATA
:
2257 return (ipxcfg_get_config_data((void *)arg
));
2259 case SIOCIPXNCPCONN
:
2262 * This socket wants to take care of the NCP connection
2263 * handed to us in arg.
2265 if (!capable(CAP_NET_ADMIN
))
2267 return get_user(sk
->protinfo
.af_ipx
.ipx_ncp_conn
, (const unsigned short *)(arg
));
2275 if(sk
->stamp
.tv_sec
== 0)
2278 if(!copy_to_user((void *)arg
, &sk
->stamp
,
2279 sizeof(struct timeval
)))
2286 case SIOCGIFDSTADDR
:
2287 case SIOCSIFDSTADDR
:
2288 case SIOCGIFBRDADDR
:
2289 case SIOCSIFBRDADDR
:
2290 case SIOCGIFNETMASK
:
2291 case SIOCSIFNETMASK
:
2295 return (dev_ioctl(cmd
,(void *) arg
));
2303 * SPX interface support
2306 int ipx_register_spx(struct proto_ops
**p
, struct net_proto_family
*spx
)
2308 if(spx_family_ops
!=NULL
)
2318 int ipx_unregister_spx(void)
2320 spx_family_ops
=NULL
;
2326 * Socket family declarations
2329 static struct net_proto_family ipx_family_ops
= {
2334 static struct proto_ops
SOCKOPS_WRAPPED(ipx_dgram_ops
) = {
2345 sock_no_shutdown
, /* FIXME: We have to really support shutdown. */
2354 #include <linux/smp_lock.h>
2355 SOCKOPS_WRAP(ipx_dgram
, PF_IPX
);
2358 /* Called by protocol.c on kernel start up */
2360 static struct packet_type ipx_8023_packet_type
=
2363 0, /* MUTTER ntohs(ETH_P_802_3),*/
2364 NULL
, /* All devices */
2370 static struct packet_type ipx_dix_packet_type
=
2372 0, /* MUTTER ntohs(ETH_P_IPX),*/
2373 NULL
, /* All devices */
2379 static struct notifier_block ipx_dev_notifier
={
2380 ipxitf_device_event
,
2386 extern struct datalink_proto
*make_EII_client(void);
2387 extern struct datalink_proto
*make_8023_client(void);
2388 extern void destroy_EII_client(struct datalink_proto
*);
2389 extern void destroy_8023_client(struct datalink_proto
*);
2391 #ifdef CONFIG_PROC_FS
2392 struct proc_dir_entry ipx_procinfo
= {
2393 PROC_NET_IPX
, 3, "ipx", S_IFREG
| S_IRUGO
,
2394 1, 0, 0, 0, &proc_net_inode_operations
, ipx_get_info
2397 struct proc_dir_entry ipx_if_procinfo
= {
2398 PROC_NET_IPX_INTERFACE
, 13, "ipx_interface", S_IFREG
| S_IRUGO
,
2399 1, 0, 0, 0, &proc_net_inode_operations
, ipx_interface_get_info
2402 struct proc_dir_entry ipx_rt_procinfo
= {
2403 PROC_NET_IPX_ROUTE
, 9, "ipx_route", S_IFREG
| S_IRUGO
,
2404 1, 0, 0, 0, &proc_net_inode_operations
, ipx_rt_get_info
2408 static unsigned char ipx_8022_type
= 0xE0;
2409 static unsigned char ipx_snap_id
[5] = { 0x0, 0x0, 0x0, 0x81, 0x37 };
2411 void ipx_proto_init(struct net_proto
*pro
)
2413 (void) sock_register(&ipx_family_ops
);
2415 pEII_datalink
= make_EII_client();
2416 ipx_dix_packet_type
.type
= htons(ETH_P_IPX
);
2417 dev_add_pack(&ipx_dix_packet_type
);
2419 p8023_datalink
= make_8023_client();
2420 ipx_8023_packet_type
.type
= htons(ETH_P_802_3
);
2421 dev_add_pack(&ipx_8023_packet_type
);
2423 if((p8022_datalink
= register_8022_client(ipx_8022_type
,ipx_rcv
)) == NULL
)
2424 printk(KERN_CRIT
"IPX: Unable to register with 802.2\n");
2426 if((pSNAP_datalink
= register_snap_client(ipx_snap_id
,ipx_rcv
)) == NULL
)
2427 printk(KERN_CRIT
"IPX: Unable to register with SNAP\n");
2429 register_netdevice_notifier(&ipx_dev_notifier
);
2431 #ifdef CONFIG_PROC_FS
2432 proc_net_register(&ipx_procinfo
);
2433 proc_net_register(&ipx_if_procinfo
);
2434 proc_net_register(&ipx_rt_procinfo
);
2437 printk(KERN_INFO
"NET4: Linux IPX 0.38 for NET4.0\n");
2438 printk(KERN_INFO
"IPX Portions Copyright (c) 1995 Caldera, Inc.\n");
2441 /* Higher layers need this info to prep tx pkts */
2442 int ipx_if_offset(unsigned long ipx_net_number
)
2444 ipx_route
*rt
= NULL
;
2446 rt
= ipxrtr_lookup(ipx_net_number
);
2448 return (rt
? rt
->ir_intrfc
->if_ipx_offset
: -ENETUNREACH
);
2451 /* Export symbols for higher layers */
2452 EXPORT_SYMBOL(ipxrtr_route_skb
);
2453 EXPORT_SYMBOL(ipx_if_offset
);
2454 EXPORT_SYMBOL(ipx_remove_socket
);
2455 EXPORT_SYMBOL(ipx_register_spx
);
2456 EXPORT_SYMBOL(ipx_unregister_spx
);
2459 /* Note on MOD_{INC,DEC}_USE_COUNT:
2461 * Use counts are incremented/decremented when
2462 * sockets are created/deleted.
2464 * Routes are always associated with an interface, and
2465 * allocs/frees will remain properly accounted for by
2466 * their associated interfaces.
2468 * Ergo, before the ipx module can be removed, all IPX
2469 * sockets be closed from user space.
2472 static void ipx_proto_finito(void)
2476 while(ipx_interfaces
)
2478 ifc
= ipx_interfaces
;
2479 ipx_interfaces
= ifc
->if_next
;
2480 ifc
->if_next
= NULL
;
2484 #ifdef CONFIG_PROC_FS
2485 proc_net_unregister(PROC_NET_IPX_ROUTE
);
2486 proc_net_unregister(PROC_NET_IPX_INTERFACE
);
2487 proc_net_unregister(PROC_NET_IPX
);
2490 unregister_netdevice_notifier(&ipx_dev_notifier
);
2492 unregister_snap_client(ipx_snap_id
);
2493 pSNAP_datalink
= NULL
;
2495 unregister_8022_client(ipx_8022_type
);
2496 p8022_datalink
= NULL
;
2498 dev_remove_pack(&ipx_8023_packet_type
);
2499 destroy_8023_client(p8023_datalink
);
2500 p8023_datalink
= NULL
;
2502 dev_remove_pack(&ipx_dix_packet_type
);
2503 destroy_EII_client(pEII_datalink
);
2504 pEII_datalink
= NULL
;
2506 (void) sock_unregister(ipx_family_ops
.family
);
2511 int init_module(void)
2513 ipx_proto_init(NULL
);
2517 void cleanup_module(void)
2524 #endif /* CONFIG_IPX || CONFIG_IPX_MODULE */