* better
[mascara-docs.git] / i386 / linux-2.3.21 / net / ipx / af_ipx.c
blobab24b855f1c16386787ca8d9a464852adb40d6b4
1 /*
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
39 * 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 * 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>
77 #include <linux/in.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>
85 #include <net/ipx.h>
86 #include <linux/inet.h>
87 #include <linux/route.h>
88 #include <net/sock.h>
89 #include <asm/uaccess.h>
90 #include <asm/system.h>
91 #include <linux/fcntl.h>
92 #include <linux/mm.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>
102 #ifdef MODULE
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)
130 if(val)
131 MOD_INC_USE_COUNT;
132 else
133 MOD_DEC_USE_COUNT;
135 ipxcfg_auto_create_interfaces = val;
138 return (0);
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;
147 return (0);
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
169 * use this facility.
172 void ipx_remove_socket(struct sock *sk)
174 struct sock *s;
175 ipx_interface *intrfc;
176 unsigned long flags;
178 save_flags(flags);
179 cli();
181 /* Determine interface with which socket is associated */
182 intrfc = sk->protinfo.af_ipx.intrfc;
183 if(intrfc == NULL)
185 restore_flags(flags);
186 return;
189 s = intrfc->if_sklist;
190 if(s == sk)
192 intrfc->if_sklist = s->next;
193 restore_flags(flags);
194 return;
197 while(s && s->next)
199 if(s->next == sk)
201 s->next = sk->next;
202 restore_flags(flags);
203 return;
205 s = s->next;
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)
218 struct sk_buff *skb;
220 ipx_remove_socket(sk);
221 while((skb = skb_dequeue(&sk->receive_queue)) != NULL)
222 kfree_skb(skb);
224 sk_free(sk);
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;
237 else
238 ipx_primary_net = NULL;
241 static ipx_interface *ipxitf_find_using_phys(struct net_device *dev, unsigned short datalink)
243 ipx_interface *i;
245 for(i = ipx_interfaces;
246 i && ((i->if_dev != dev) || (i->if_dlink_type != datalink));
247 i = i->if_next)
250 return (i);
253 static ipx_interface *ipxitf_find_using_net(__u32 net)
255 ipx_interface *i;
257 if(!net)
258 return (ipx_primary_net);
260 for(i = ipx_interfaces; i && (i->if_netnum != net); i = i->if_next)
263 return (i);
266 /* Sockets are bound to a particular IPX interface. */
267 static void ipxitf_insert_socket(ipx_interface *intrfc, struct sock *sk)
269 struct sock *s;
271 sk->protinfo.af_ipx.intrfc = intrfc;
272 sk->next = NULL;
273 if(intrfc->if_sklist == NULL)
274 intrfc->if_sklist = sk;
275 else
277 for (s = intrfc->if_sklist; s->next != NULL; s = s->next)
279 s->next = sk;
283 static struct sock *ipxitf_find_socket(ipx_interface *intrfc, unsigned short port)
285 struct sock *s;
287 for(s = intrfc->if_sklist;
288 (s != NULL) && (s->protinfo.af_ipx.port != port);
289 s = s->next)
292 return (s);
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;
302 while(s != NULL)
304 if((s->protinfo.af_ipx.port == port)
305 && (memcmp(node, s->protinfo.af_ipx.node, IPX_NODE_LEN) == 0))
307 break;
309 s = s->next;
312 return (s);
314 #endif
316 static void ipxrtr_del_routes(ipx_interface *);
318 static void ipxitf_down(ipx_interface *intrfc)
320 ipx_interface *i;
321 struct sock *s, *t;
323 /* Delete all routes associated with this interface */
324 ipxrtr_del_routes(intrfc);
326 /* error sockets */
327 for(s = intrfc->if_sklist; s != NULL; )
329 s->err = ENOLINK;
330 s->error_report(s);
331 s->protinfo.af_ipx.intrfc = NULL;
332 s->protinfo.af_ipx.port = 0;
333 s->zapped=1; /* Indicates it is no longer bound */
334 t = s;
335 s = s->next;
336 t->next = NULL;
338 intrfc->if_sklist = NULL;
340 /* remove this interface from list */
341 if(intrfc == ipx_interfaces)
342 ipx_interfaces = intrfc->if_next;
343 else
345 for(i = ipx_interfaces;
346 (i != NULL) && (i->if_next != intrfc);
347 i = i->if_next)
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));
360 MOD_DEC_USE_COUNT;
362 return;
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)
371 return NOTIFY_DONE;
373 for(i = ipx_interfaces; i != NULL;)
375 tmp = i->if_next;
376 if(i->if_dev == dev)
377 ipxitf_down(i);
378 i = tmp;
382 return (NOTIFY_DONE);
385 static int ipxitf_def_skb_handler(struct sock *sock, struct sk_buff *skb)
387 int retval;
389 if((retval = sock_queue_rcv_skb(sock, skb)) < 0)
390 kfree_skb(skb);
392 return (retval);
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;
403 struct sock *s;
405 int is_broadcast = (memcmp(ipx->ipx_dest.node, ipx_broadcast_node,
406 IPX_NODE_LEN) == 0);
408 s = intrfc->if_sklist;
410 while(s != NULL)
412 if((s->protinfo.af_ipx.port == ipx->ipx_dest.sock)
413 && (is_broadcast
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;
420 if(copy != 0)
422 skb1 = skb_clone(skb, GFP_ATOMIC);
423 if (skb1 == NULL)
424 return -ENOMEM;
426 else
428 skb1 = skb;
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)
435 break;
437 s = s->next;
440 /* skb was solely for us, and we did not make a copy, so free it. */
441 if(copy == 0)
442 kfree_skb(skb);
444 return (0);
447 #else
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
462 * BURST packets.
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
465 * VERY fast as well.
467 int connection = 0;
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);
486 if (connection)
489 * Now we have to look for a special NCP connection handling
490 * socket. Only these sockets have ipx_ncp_conn != 0, set
491 * by SIOCIPXNCPCONN.
493 for (sock1=intrfc->if_sklist;
494 (sock1 != NULL) &&
495 (sock1->protinfo.af_ipx.ipx_ncp_conn != connection);
496 sock1=sock1->next);
499 if (sock1 == NULL)
501 /* No special socket found, forward the packet the
502 * normal way.
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
512 * 0x456(Diagnostic).
515 if(ipx_primary_net && (intrfc != ipx_primary_net))
517 switch(ntohs(ipx->ipx_dest.sock))
519 case 0x452:
520 case 0x453:
521 case 0x456:
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,
529 ipx->ipx_dest.sock);
530 break;
531 default:
532 break;
537 * If there is nothing to do return. The kfree will cancel any charging.
539 if(sock1 == NULL && sock2 == NULL)
541 if(!copy)
542 kfree_skb(skb);
543 return (0);
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.
554 if(copy)
555 skb1 = skb_clone(skb, GFP_ATOMIC);
556 else
557 skb1 = skb;
559 if(skb1 == NULL)
560 return (-ENOMEM);
562 /* Do we need 2 SKBs? */
563 if(sock1 && sock2)
564 skb2 = skb_clone(skb1, GFP_ATOMIC);
565 else
566 skb2 = skb1;
568 if(sock1)
569 (void) ipxitf_def_skb_handler(sock1, skb1);
571 if(skb2 == NULL)
572 return (-ENOMEM);
574 if(sock2)
575 (void) ipxitf_def_skb_handler(sock2, skb2);
577 return (0);
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;
586 int len;
588 /* Hopefully, most cases */
589 if(in_offset >= out_offset)
590 return (skb);
592 /* Need new SKB */
593 len = skb->len + out_offset;
594 skb2 = alloc_skb(len, GFP_ATOMIC);
595 if(skb2 != NULL)
597 skb_reserve(skb2, out_offset);
598 skb2->nh.raw =
599 skb2->h.raw = skb_put(skb2,skb->len);
600 memcpy(skb2->h.raw, skb->h.raw, skb->len);
602 kfree_skb(skb);
604 return (NULL);
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;
614 int addr_len;
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
628 * up clones.
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 */
641 skb_orphan(skb);
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)
650 if(!send_to_wire)
651 skb_orphan(skb);
652 ipxitf_demux_socket(intrfc, skb, send_to_wire);
653 if(!send_to_wire)
654 return (0);
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);
670 if(!skb)
671 return (0);
672 if(++(ipx->ipx_tctrl) > ipxcfg_max_hops)
673 send_to_wire = 0;
676 if(!send_to_wire)
678 kfree_skb(skb);
679 return (0);
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);
686 else
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);
691 if(skb == NULL)
692 return (0);
694 /* set up data link and physical headers */
695 skb->dev = dev;
696 skb->protocol = htons(ETH_P_IPX);
697 dl->datalink_header(dl, skb, dest_node);
699 /* Send it out */
700 dev_queue_xmit(skb);
702 return (0);
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;
718 ipx_interface *i;
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);
734 else
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),
738 ipx_device_name(i),
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)
751 int i;
752 ipx_interface *ifcs;
753 struct sk_buff *skb2;
754 __u32 *l;
755 char *c;
757 c = (char *) skb->data;
758 c += sizeof(struct ipxhdr);
759 l = (__u32 *) c;
761 i = 0;
763 /* Dump packet if already seen this net */
764 for( ; i < ipx->ipx_tctrl; i++)
765 if(*l++ == intrfc->if_netnum)
766 break;
768 if(i == ipx->ipx_tctrl)
770 /* < 8 hops && input itfc not in list */
771 *l = intrfc->if_netnum; /* insert recvd netnum into list */
772 ipx->ipx_tctrl++;
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)
778 continue;
780 /* That aren't in the list */
781 l = (__u32 *) c;
782 for(i = 0; i <= ipx->ipx_tctrl; i++)
783 if(ifcs->if_netnum == *l++)
784 break;
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);
809 if(skb)
810 return (ipxrtr_route_skb(skb));
811 else
812 return (0);
815 kfree_skb(skb);
816 return (0);
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 */
827 kfree_skb(skb);
828 return (0);
831 static void ipxitf_insert(ipx_interface *intrfc)
833 ipx_interface *i;
835 intrfc->if_next = NULL;
836 if(ipx_interfaces == NULL)
837 ipx_interfaces = intrfc;
838 else
840 for(i = ipx_interfaces; i->if_next != NULL; i = i->if_next)
842 i->if_next = intrfc;
845 if(ipxcfg_auto_select_primary && (ipx_primary_net == NULL))
846 ipx_primary_net = intrfc;
848 MOD_INC_USE_COUNT;
850 return;
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)
859 return (-EEXIST);
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);
868 if(intrfc == NULL)
869 return (-EAGAIN);
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)
888 switch(type)
890 case IPX_FRAME_ETHERII:
891 return (htons(ETH_P_IPX));
893 case IPX_FRAME_8022:
894 return (htons(ETH_P_802_2));
896 case IPX_FRAME_SNAP:
897 return (htons(ETH_P_SNAP));
899 case IPX_FRAME_8023:
900 return (htons(ETH_P_802_3));
903 return (0);
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))
917 return (-EEXIST);
919 if(idef->ipx_network
920 && (ipxitf_find_using_net(idef->ipx_network) != NULL))
921 return (-EADDRINUSE);
923 dev = __dev_get_by_name(idef->ipx_device);
924 if(dev == NULL)
925 return (-ENODEV);
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");
931 /* fall through */
933 case IPX_FRAME_8022:
934 dlink_type = htons(ETH_P_802_2);
935 datalink = p8022_datalink;
936 break;
938 case IPX_FRAME_ETHERII:
939 if (dev->type != ARPHRD_IEEE802)
941 dlink_type = htons(ETH_P_IPX);
942 datalink = pEII_datalink;
943 break;
945 else
946 printk("IPX frame type EtherII over token-ring is obsolete. Use SNAP instead.\n");
947 /* fall through */
949 case IPX_FRAME_SNAP:
950 dlink_type = htons(ETH_P_SNAP);
951 datalink = pSNAP_datalink;
952 break;
954 case IPX_FRAME_8023:
955 dlink_type = htons(ETH_P_802_3);
956 datalink = p8023_datalink;
957 break;
959 case IPX_FRAME_NONE:
960 default:
961 break;
964 if(!(dev->flags & IFF_UP))
965 return (-ENETDOWN);
967 /* Check addresses are suitable */
968 if(dev->addr_len > IPX_NODE_LEN)
969 return (-EINVAL);
971 if(datalink == NULL)
972 return (-EPROTONOSUPPORT);
974 if((intrfc = ipxitf_find_using_phys(dev, dlink_type)) == NULL)
976 /* Ok now create */
977 intrfc = (ipx_interface *)kmalloc(sizeof(ipx_interface),GFP_ATOMIC);
978 if(intrfc == NULL)
979 return (-EAGAIN);
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);
997 else
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)
1004 return (0);
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);
1020 return (0);
1022 return (-ENOENT);
1025 dlink_type = ipx_map_frame_type(idef->ipx_dlink_type);
1026 if(dlink_type == 0)
1027 return (-EPROTONOSUPPORT);
1029 dev = __dev_get_by_name(idef->ipx_device);
1030 if(dev == NULL)
1031 return (-ENODEV);
1033 intrfc = ipxitf_find_using_phys(dev, dlink_type);
1034 if(intrfc != NULL)
1036 ipxitf_down(intrfc);
1037 return (0);
1040 return (-EINVAL);
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))
1051 case ETH_P_IPX:
1052 datalink = pEII_datalink;
1053 break;
1055 case ETH_P_802_2:
1056 datalink = p8022_datalink;
1057 break;
1059 case ETH_P_SNAP:
1060 datalink = pSNAP_datalink;
1061 break;
1063 case ETH_P_802_3:
1064 datalink = p8023_datalink;
1065 break;
1067 default:
1068 return (NULL);
1071 if(dev == NULL)
1072 return (NULL);
1074 /* Check addresses are suitable */
1075 if(dev->addr_len>IPX_NODE_LEN)
1076 return (NULL);
1078 intrfc = (ipx_interface *)kmalloc(sizeof(ipx_interface),GFP_ATOMIC);
1079 if(intrfc != NULL)
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);
1096 return (intrfc);
1099 static int ipxitf_ioctl(unsigned int cmd, void *arg)
1101 struct ifreq ifr;
1102 int err, val;
1104 switch(cmd)
1106 case SIOCSIFADDR:
1108 struct sockaddr_ipx *sipx;
1109 ipx_interface_definition f;
1111 if(copy_from_user(&ifr, arg, sizeof(ifr)))
1112 return (-EFAULT);
1114 sipx = (struct sockaddr_ipx *)&ifr.ifr_addr;
1115 if(sipx->sipx_family != AF_IPX)
1116 return (-EINVAL);
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));
1126 else
1127 return (ipxitf_create(&f));
1130 case SIOCGIFADDR:
1132 struct sockaddr_ipx *sipx;
1133 ipx_interface *ipxif;
1134 struct net_device *dev;
1136 if(copy_from_user(&ifr, arg, sizeof(ifr)))
1137 return (-EFAULT);
1139 sipx = (struct sockaddr_ipx *)&ifr.ifr_addr;
1140 dev = __dev_get_by_name(ifr.ifr_name);
1141 if(!dev)
1142 return (-ENODEV);
1144 ipxif = ipxitf_find_using_phys(dev, ipx_map_frame_type(sipx->sipx_type));
1145 if(ipxif == NULL)
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));
1151 err = -EFAULT;
1152 if(!copy_to_user(arg, &ifr, sizeof(ifr)))
1153 err = 0;
1155 return (err);
1158 case SIOCAIPXITFCRT:
1160 err = get_user(val, (unsigned char *) arg);
1161 if(err)
1162 return (err);
1164 return (ipxcfg_set_auto_create(val));
1167 case SIOCAIPXPRISLT:
1169 err = get_user(val, (unsigned char *) arg);
1170 if(err)
1171 return (err);
1173 return (ipxcfg_set_auto_select(val));
1176 default:
1177 return (-EINVAL);
1181 /**************************************************************************\
1183 * Routing tables for the IPX socket layer. *
1185 \**************************************************************************/
1187 static ipx_route *ipxrtr_lookup(__u32 net)
1189 ipx_route *r;
1191 for(r = ipx_routes; (r != NULL) && (r->ir_net != net); r = r->ir_next)
1194 return (r);
1197 static int ipxrtr_add_route(__u32 network, ipx_interface *intrfc, unsigned char *node)
1199 ipx_route *rt;
1201 /* Get a route structure; either existing or create */
1202 rt = ipxrtr_lookup(network);
1203 if(rt == NULL)
1205 rt = (ipx_route *)kmalloc(sizeof(ipx_route),GFP_ATOMIC);
1206 if(rt == NULL)
1207 return (-EAGAIN);
1208 rt->ir_next = ipx_routes;
1209 ipx_routes = rt;
1211 else if(intrfc == ipx_internal_net)
1212 return (-EEXIST);
1214 rt->ir_net = network;
1215 rt->ir_intrfc = intrfc;
1216 if(node == NULL)
1218 memset(rt->ir_router_node, '\0', IPX_NODE_LEN);
1219 rt->ir_routed = 0;
1221 else
1223 memcpy(rt->ir_router_node, node, IPX_NODE_LEN);
1224 rt->ir_routed = 1;
1227 return (0);
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)
1238 *r = tmp->ir_next;
1239 kfree_s(tmp, sizeof(ipx_route));
1241 else
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);
1252 if(intrfc == NULL)
1253 return (-ENETUNREACH);
1255 return (ipxrtr_add_route(rd->ipx_network, intrfc, rd->ipx_router_node));
1258 static int ipxrtr_delete(long net)
1260 ipx_route **r;
1261 ipx_route *tmp;
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))
1269 return (-EPERM);
1271 *r = tmp->ir_next;
1272 kfree_s(tmp, sizeof(ipx_route));
1273 return (0);
1276 r = &(tmp->ir_next);
1279 return (-ENOENT);
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.)
1296 __u32 sum = 0;
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 */
1309 while(--i)
1310 sum += *p++;
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 */
1322 if(sum >= 0x10000)
1323 sum++;
1325 return (~sum);
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;
1335 struct ipxhdr *ipx;
1336 int size;
1337 int ipx_offset;
1338 ipx_route *rt = NULL;
1339 int err;
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;
1347 else
1349 rt = ipxrtr_lookup(usipx->sipx_network);
1350 if(rt == NULL)
1351 return (-ENETUNREACH);
1353 intrfc = rt->ir_intrfc;
1356 ipx_offset = intrfc->if_ipx_offset;
1357 size = sizeof(struct ipxhdr) + len;
1358 size += ipx_offset;
1360 skb = sock_alloc_send_skb(sk, size, 0, noblock, &err);
1361 if(skb == NULL)
1362 return (err);
1364 skb_reserve(skb,ipx_offset);
1365 skb->sk = sk;
1367 /* Fill in IPX header */
1368 ipx = (struct ipxhdr *)skb_put(skb, sizeof(struct ipxhdr));
1369 ipx->ipx_pktsize= htons(len + sizeof(struct ipxhdr));
1370 ipx->ipx_tctrl = 0;
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);
1378 #else
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);
1385 else
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);
1398 if(err)
1400 kfree_skb(skb);
1401 return (-EFAULT);
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;
1407 else
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;
1417 ipx_interface *i;
1418 ipx_route *r;
1420 r = ipxrtr_lookup(ipx->ipx_dest.net);
1421 if(r == NULL) /* no known route */
1423 kfree_skb(skb);
1424 return (0);
1427 i = r->ir_intrfc;
1428 (void)ipxitf_send(i, skb, (r->ir_routed) ?
1429 r->ir_router_node : ipx->ipx_dest.node);
1431 return (0);
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;
1441 int err;
1443 err = copy_from_user(&rt,arg,sizeof(rt));
1444 if(err)
1445 return (-EFAULT);
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)
1453 return (-EINVAL);
1454 if(st->sipx_family != AF_IPX)
1455 return (-EINVAL);
1457 switch(cmd)
1459 case SIOCDELRT:
1460 return (ipxrtr_delete(st->sipx_network));
1462 case SIOCADDRT:
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));
1471 default:
1472 return (-EINVAL);
1476 static const char *ipx_frame_name(unsigned short frame)
1478 switch(ntohs(frame))
1480 case ETH_P_IPX:
1481 return ("EtherII");
1483 case ETH_P_802_2:
1484 return ("802.2");
1486 case ETH_P_SNAP:
1487 return ("SNAP");
1489 case ETH_P_802_3:
1490 return ("802.3");
1492 case ETH_P_TR_802_2:
1493 return ("802.2TR");
1495 default:
1496 return ("None");
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)
1510 ipx_interface *i;
1511 off_t begin = 0, pos = 0;
1512 int len = 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) ?
1525 "Yes" : "No");
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 */
1531 pos = begin + len;
1533 if(pos < offset)
1535 len = 0; /* Keep dumping into the buffer start */
1536 begin = pos;
1538 if(pos > offset + length) /* We have dumped enough */
1539 break;
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 */
1545 if(len > length)
1546 len = length; /* Remove unwanted tail data from length */
1548 return (len);
1551 static int ipx_get_info(char *buffer, char **start, off_t offset,
1552 int length, int dummy)
1554 struct sock *s;
1555 ipx_interface *i;
1556 off_t begin = 0, pos = 0;
1557 int len = 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",
1563 #else
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",
1567 "State", "Uid");
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));
1584 #else
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");
1592 else
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);
1612 pos = begin + len;
1613 if(pos < offset)
1615 len = 0;
1616 begin = pos;
1619 if(pos > offset + length) /* We have dumped enough */
1620 break;
1624 /* The data in question runs from begin to begin+len */
1625 *start = buffer + (offset-begin);
1626 len -= (offset - begin);
1627 if(len > length)
1628 len = length;
1630 return (len);
1633 static int ipx_rt_get_info(char *buffer, char **start, off_t offset,
1634 int length, int dummy)
1636 ipx_route *rt;
1637 off_t begin = 0, pos = 0;
1638 int len = 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));
1645 if(rt->ir_routed)
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]);
1653 else
1655 len += sprintf(buffer+len, "%-13s%s\n",
1656 "Directly", "Connected");
1659 pos = begin + len;
1660 if(pos < offset)
1662 len = 0;
1663 begin = pos;
1666 if(pos > offset + length)
1667 break;
1670 *start = buffer + (offset - begin);
1671 len -= (offset - begin);
1672 if(len > length)
1673 len = length;
1675 return (len);
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)
1687 struct sock *sk;
1688 int err, opt;
1690 sk = sock->sk;
1692 if(optlen != sizeof(int))
1693 return (-EINVAL);
1695 err = get_user(opt, (unsigned int *)optval);
1696 if(err)
1697 return (err);
1699 switch(level)
1701 case SOL_IPX:
1702 switch(optname)
1704 case IPX_TYPE:
1705 sk->protinfo.af_ipx.type = opt;
1706 return (0);
1708 default:
1709 return (-ENOPROTOOPT);
1711 break;
1713 default:
1714 return (-ENOPROTOOPT);
1718 static int ipx_getsockopt(struct socket *sock, int level, int optname,
1719 char *optval, int *optlen)
1721 struct sock *sk;
1722 int val=0;
1723 int len;
1725 sk = sock->sk;
1727 switch(level)
1729 case SOL_IPX:
1730 switch(optname)
1732 case IPX_TYPE:
1733 val = sk->protinfo.af_ipx.type;
1734 break;
1736 default:
1737 return (-ENOPROTOOPT);
1739 break;
1741 default:
1742 return (-ENOPROTOOPT);
1745 if(get_user(len, optlen))
1746 return (-EFAULT);
1748 len = min(len, sizeof(int));
1749 if(put_user(len, optlen))
1750 return (-EFAULT);
1752 if(copy_to_user(optval, &val, len))
1753 return (-EFAULT);
1755 return (0);
1758 static int ipx_create(struct socket *sock, int protocol)
1760 struct sock *sk;
1762 switch(sock->type)
1764 case SOCK_DGRAM:
1765 sk = sk_alloc(PF_IPX, GFP_KERNEL, 1);
1766 if(sk == NULL)
1767 return (-ENOMEM);
1768 sock->ops = &ipx_dgram_ops;
1769 break;
1771 case SOCK_SEQPACKET:
1773 * From this point on SPX sockets are handled
1774 * by af_spx.c and the methods replaced.
1776 if(spx_family_ops)
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 */
1780 default:
1781 return (-ESOCKTNOSUPPORT);
1784 sock_init_data(sock, sk);
1785 sk->destruct = NULL;
1786 sk->no_check = 1; /* Checksum off by default */
1788 MOD_INC_USE_COUNT;
1790 return (0);
1793 static int ipx_release(struct socket *sock)
1795 struct sock *sk = sock->sk;
1797 if(sk == NULL)
1798 return (0);
1800 if(!sk->dead)
1801 sk->state_change(sk);
1803 sk->dead = 1;
1804 sock->sk = NULL;
1805 ipx_destroy_socket(sk);
1807 if(sock->type == SOCK_DGRAM)
1808 MOD_DEC_USE_COUNT;
1810 return (0);
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;
1824 else
1825 socketNum++;
1828 intrfc->if_sknum = socketNum;
1830 return (ntohs(socketNum));
1833 static int ipx_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1835 struct sock *sk;
1836 ipx_interface *intrfc;
1837 struct sockaddr_ipx *addr = (struct sockaddr_ipx *)uaddr;
1839 sk = sock->sk;
1841 if(sk->zapped == 0)
1842 return (-EINVAL);
1844 if(addr_len != sizeof(struct sockaddr_ipx))
1845 return (-EINVAL);
1847 intrfc = ipxitf_find_using_net(addr->sipx_network);
1848 if(intrfc == NULL)
1849 return (-EADDRNOTAVAIL);
1851 if(addr->sipx_port == 0)
1853 addr->sipx_port = ipx_first_free_socketnum(intrfc);
1854 if(addr->sipx_port == 0)
1855 return (-EINVAL);
1858 /* protect IPX system stuff like routing/sap */
1859 if(ntohs(addr->sipx_port) < IPX_MIN_EPHEMERAL_SOCKET && !capable(CAP_NET_ADMIN))
1860 return (-EACCES);
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)
1873 return (-EINVAL);
1874 if(memcmp(addr->sipx_node, ipx_this_node, IPX_NODE_LEN) == 0)
1876 memcpy(sk->protinfo.af_ipx.node, intrfc->if_node,
1877 IPX_NODE_LEN);
1879 else
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)
1888 SOCK_DEBUG(sk,
1889 "IPX: bind failed because port %X in use.\n",
1890 ntohs((int)addr->sipx_port));
1891 return (-EADDRINUSE);
1894 else
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,
1902 IPX_NODE_LEN);
1904 if(ipxitf_find_socket(intrfc, addr->sipx_port) != NULL)
1906 SOCK_DEBUG(sk,
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);
1928 sk->zapped = 0;
1929 SOCK_DEBUG(sk, "IPX: bound socket 0x%04X.\n", ntohs(addr->sipx_port) );
1931 return (0);
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))
1944 return (-EINVAL);
1945 addr = (struct sockaddr_ipx *)uaddr;
1947 /* put the autobinding in */
1948 if(sk->protinfo.af_ipx.port == 0)
1950 struct sockaddr_ipx uaddr;
1951 int ret;
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);
1959 else
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));
1965 if(ret != 0)
1966 return (ret);
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;
1985 return (0);
1989 static int ipx_getname(struct socket *sock, struct sockaddr *uaddr,
1990 int *uaddr_len, int peer)
1992 ipx_address *addr;
1993 struct sockaddr_ipx sipx;
1994 struct sock *sk;
1996 sk = sock->sk;
1998 *uaddr_len = sizeof(struct sockaddr_ipx);
2000 if(peer)
2002 if(sk->state != TCP_ESTABLISHED)
2003 return (-ENOTCONN);
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;
2010 else
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);
2017 #else
2018 memcpy(sipx.sipx_node, sk->protinfo.af_ipx.intrfc->if_node, IPX_NODE_LEN);
2019 #endif /* CONFIG_IPX_INTERN */
2022 else
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));
2035 return (0);
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;
2042 struct ipxhdr *ipx;
2044 ipx = skb->nh.ipxh;
2046 /* Too small? */
2047 if(ntohs(ipx->ipx_pktsize) < sizeof(struct ipxhdr))
2048 goto drop;
2050 /* Invalid header */
2051 if(ntohs(ipx->ipx_pktsize) > skb->len)
2052 goto drop;
2054 /* Not ours */
2055 if (skb->pkt_type == PACKET_OTHERHOST)
2056 goto drop;
2058 if(ipx->ipx_checksum != IPX_NO_CHECKSUM)
2060 if(ipx_set_checksum(ipx, ntohs(ipx->ipx_pktsize)) != ipx->ipx_checksum)
2061 goto drop;
2064 /* Determine what local ipx endpoint this is */
2065 intrfc = ipxitf_find_using_phys(dev, pt->type);
2066 if(intrfc == NULL)
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 */
2075 goto drop;
2078 return (ipxitf_rcv(intrfc, skb));
2080 drop:
2081 kfree_skb(skb);
2082 return (0);
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;
2091 int retval;
2092 int flags = msg->msg_flags;
2094 /* Socket gets bound below anyway */
2095 /* if(sk->zapped)
2096 return (-EIO); */ /* Socket not bound */
2097 if(flags & ~MSG_DONTWAIT)
2098 return (-EINVAL);
2100 if(usipx)
2102 if(sk->protinfo.af_ipx.port == 0)
2104 struct sockaddr_ipx uaddr;
2105 int ret;
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);
2113 else
2114 return -ENETDOWN; /* Someone zonked the iface */
2115 #endif
2116 ret = ipx_bind(sock, (struct sockaddr *)&uaddr,
2117 sizeof(struct sockaddr_ipx));
2118 if(ret != 0)
2119 return (ret);
2122 if(msg->msg_namelen < sizeof(*usipx))
2123 return (-EINVAL);
2124 if(usipx->sipx_family != AF_IPX)
2125 return (-EINVAL);
2127 else
2129 if(sk->state != TCP_ESTABLISHED)
2130 return (-ENOTCONN);
2132 usipx=&local_sipx;
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);
2141 if(retval < 0)
2142 return (retval);
2144 return (len);
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;
2155 int copied, err;
2157 /* put the autobinding in */
2158 if(sk->protinfo.af_ipx.port == 0)
2160 struct sockaddr_ipx uaddr;
2161 int ret;
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);
2169 else
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));
2175 if(ret != 0)
2176 return (ret);
2179 if(sk->zapped)
2180 return (-ENOTCONN);
2182 skb = skb_recv_datagram(sk,flags&~MSG_DONTWAIT,flags&MSG_DONTWAIT,&err);
2183 if(!skb)
2184 goto out;
2186 ipx = skb->nh.ipxh;
2187 copied = ntohs(ipx->ipx_pktsize) - sizeof(struct ipxhdr);
2188 if(copied > size)
2190 copied=size;
2191 msg->msg_flags |= MSG_TRUNC;
2194 err = skb_copy_datagram_iovec(skb, sizeof(struct ipxhdr), msg->msg_iov,
2195 copied);
2196 if(err)
2197 goto out_free;
2198 sk->stamp = skb->stamp;
2200 msg->msg_namelen = sizeof(*sipx);
2202 if(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;
2210 err = copied;
2212 out_free:
2213 skb_free_datagram(sk, skb);
2214 out:
2215 return (err);
2219 static int ipx_ioctl(struct socket *sock,unsigned int cmd, unsigned long arg)
2221 long amount = 0;
2222 struct sock *sk = sock->sk;
2224 switch(cmd)
2226 case TIOCOUTQ:
2227 amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
2228 if(amount < 0)
2229 amount = 0;
2230 return (put_user(amount, (int *)arg));
2232 case TIOCINQ:
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));
2241 case SIOCADDRT:
2242 case SIOCDELRT:
2243 if(!capable(CAP_NET_ADMIN))
2244 return (-EPERM);
2245 return (ipxrtr_ioctl(cmd,(void *)arg));
2247 case SIOCSIFADDR:
2248 case SIOCAIPXITFCRT:
2249 case SIOCAIPXPRISLT:
2250 if(!capable(CAP_NET_ADMIN))
2251 return (-EPERM);
2253 case SIOCGIFADDR:
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))
2266 return(-EPERM);
2267 return get_user(sk->protinfo.af_ipx.ipx_ncp_conn, (const unsigned short *)(arg));
2270 case SIOCGSTAMP:
2272 int ret = -EINVAL;
2273 if(sk)
2275 if(sk->stamp.tv_sec == 0)
2276 return (-ENOENT);
2277 ret = -EFAULT;
2278 if(!copy_to_user((void *)arg, &sk->stamp,
2279 sizeof(struct timeval)))
2280 ret = 0;
2283 return (ret);
2286 case SIOCGIFDSTADDR:
2287 case SIOCSIFDSTADDR:
2288 case SIOCGIFBRDADDR:
2289 case SIOCSIFBRDADDR:
2290 case SIOCGIFNETMASK:
2291 case SIOCSIFNETMASK:
2292 return (-EINVAL);
2294 default:
2295 return (dev_ioctl(cmd,(void *) arg));
2298 /*NOT REACHED*/
2299 return (0);
2303 * SPX interface support
2306 int ipx_register_spx(struct proto_ops **p, struct net_proto_family *spx)
2308 if(spx_family_ops!=NULL)
2309 return -EBUSY;
2310 cli();
2311 MOD_INC_USE_COUNT;
2312 *p=&ipx_dgram_ops;
2313 spx_family_ops=spx;
2314 sti();
2315 return 0;
2318 int ipx_unregister_spx(void)
2320 spx_family_ops=NULL;
2321 MOD_DEC_USE_COUNT;
2322 return 0;
2326 * Socket family declarations
2329 static struct net_proto_family ipx_family_ops = {
2330 PF_IPX,
2331 ipx_create
2334 static struct proto_ops SOCKOPS_WRAPPED(ipx_dgram_ops) = {
2335 PF_IPX,
2336 ipx_release,
2337 ipx_bind,
2338 ipx_connect,
2339 sock_no_socketpair,
2340 sock_no_accept,
2341 ipx_getname,
2342 datagram_poll,
2343 ipx_ioctl,
2344 sock_no_listen,
2345 sock_no_shutdown, /* FIXME: We have to really support shutdown. */
2346 ipx_setsockopt,
2347 ipx_getsockopt,
2348 sock_no_fcntl,
2349 ipx_sendmsg,
2350 ipx_recvmsg,
2351 sock_no_mmap
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 */
2365 ipx_rcv,
2366 NULL,
2367 NULL,
2370 static struct packet_type ipx_dix_packet_type =
2372 0, /* MUTTER ntohs(ETH_P_IPX),*/
2373 NULL, /* All devices */
2374 ipx_rcv,
2375 NULL,
2376 NULL,
2379 static struct notifier_block ipx_dev_notifier={
2380 ipxitf_device_event,
2381 NULL,
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
2406 #endif
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);
2435 #endif
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);
2458 #ifdef MODULE
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)
2474 ipx_interface *ifc;
2476 while(ipx_interfaces)
2478 ifc = ipx_interfaces;
2479 ipx_interfaces = ifc->if_next;
2480 ifc->if_next = NULL;
2481 ipxitf_down(ifc);
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);
2488 #endif
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);
2508 return;
2511 int init_module(void)
2513 ipx_proto_init(NULL);
2514 return (0);
2517 void cleanup_module(void)
2519 ipx_proto_finito();
2520 return;
2523 #endif /* MODULE */
2524 #endif /* CONFIG_IPX || CONFIG_IPX_MODULE */