1 /*
2  *	Implements an IPX socket layer.
3  *
4  *	This code is derived from work by
5  *		Ross Biro	: 	Writing the original IP stack
6  *		Fred Van Kempen :	Tidying up the TCP/IP
7  *
8  *	Many thanks go to Keith Baker, Institute For Industrial Information
9  *	Technology Ltd, Swansea University for allowing me to work on this
10  *	in my own time even though it was in some ways related to commercial
11  *	work I am currently employed to do there.
12  *
13  *	All the material in this file is subject to the Gnu license version 2.
14  *	Neither Alan Cox nor the Swansea University Computer Society admit
15  *	liability nor provide warranty for any of this software. This material
16  *	is provided as is and at no charge.
17  *
18  *	Revision 0.21:	Uses the new generic socket option code.
19  *	Revision 0.22:	Gcc clean ups and drop out device registration. Use the
20  *			new multi-protocol edition of hard_header
21  *	Revision 0.23:  IPX /proc by Mark Evans. 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>
56  *	Revision 042:   Multithreading - use spinlocks and refcounting to
57  *			protect some structures: ipx_interface sock list, list
58  *			of ipx interfaces, etc.
59  *			Bugfixes - do refcounting on net_devices, check function
60  *			results, etc. Thanks to davem and freitag for
61  *			suggestions and guidance.
62  *			Arnaldo Carvalho de Melo <acme@conectiva.com.br>,
63  *			November, 2000
64  *	Revision 043:	Shared SKBs, don't mangle packets, some cleanups
65  *			Arnaldo Carvalho de Melo <acme@conectiva.com.br>,
66  *			December, 2000
67  *	Revision 044:	Call ipxitf_hold on NETDEV_UP (acme)
68  *	Revision 045:	fix PPROP routing bug (acme)
69  *	Revision 046:	Further fixes to PPROP, ipxitf_create_internal was
70  *			doing an unneeded MOD_INC_USE_COUNT, implement
71  *			sysctl for ipx_pprop_broacasting, fix the ipx sysctl
72  *			handling, making it dynamic, some cleanups, thanks to
73  *			Petr Vandrovec for review and good suggestions. (acme)
74  *	Revision 047:	Cleanups, CodingStyle changes, move the ncp connection
75  *			hack out of line (acme)
76  *
77  *	Protect the module by a MOD_INC_USE_COUNT/MOD_DEC_USE_COUNT
78  *	pair. Also, now usage count is managed this way
79  *	-Count one if the auto_interface mode is on
80  *      -Count one per configured interface
81  *
82  *	Jacques Gelinas (jacques@solucorp.qc.ca)
83  *
84  *
85  * 	Portions Copyright (c) 1995 Caldera, Inc. <greg@caldera.com>
86  *	Neither Greg Page nor Caldera, Inc. admit liability nor provide
87  *	warranty for any of this software. This material is provided
88  *	"AS-IS" and at no charge.
89  */
90 
91 #include <linux/config.h>
92 #include <linux/module.h>
93 #include <linux/errno.h>
94 #include <linux/types.h>
95 #include <linux/socket.h>
96 #include <linux/in.h>
97 #include <linux/kernel.h>
98 #include <linux/sched.h>
99 #include <linux/timer.h>
100 #include <linux/string.h>
101 #include <linux/sockios.h>
102 #include <linux/net.h>
103 #include <linux/netdevice.h>
104 #include <net/ipx.h>
105 #include <linux/inet.h>
106 #include <linux/route.h>
107 #include <net/sock.h>
108 #include <asm/uaccess.h>
109 #include <asm/system.h>
110 #include <linux/fcntl.h>
111 #include <linux/mm.h>
112 #include <linux/termios.h>	/* For TIOCOUTQ/INQ */
113 #include <linux/interrupt.h>
114 #include <net/p8022.h>
115 #include <net/psnap.h>
116 #include <linux/proc_fs.h>
117 #include <linux/stat.h>
118 #include <linux/init.h>
119 #include <linux/if_arp.h>
120 
121 #ifdef CONFIG_SYSCTL
122 extern void ipx_register_sysctl(void);
123 extern void ipx_unregister_sysctl(void);
124 #else
125 #define ipx_register_sysctl()
126 #define ipx_unregister_sysctl()
127 #endif
128 
129 /* Configuration Variables */
130 static unsigned char ipxcfg_max_hops = 16;
131 static char ipxcfg_auto_select_primary;
132 static char ipxcfg_auto_create_interfaces;
133 int sysctl_ipx_pprop_broadcasting = 1;
134 
135 /* Global Variables */
136 static struct datalink_proto *p8022_datalink;
137 static struct datalink_proto *pEII_datalink;
138 static struct datalink_proto *p8023_datalink;
139 static struct datalink_proto *pSNAP_datalink;
140 
141 static struct proto_ops ipx_dgram_ops;
142 
143 static struct net_proto_family *spx_family_ops;
144 
145 static ipx_route *ipx_routes;
146 static rwlock_t ipx_routes_lock = RW_LOCK_UNLOCKED;
147 
148 static ipx_interface *ipx_interfaces;
149 static spinlock_t ipx_interfaces_lock = SPIN_LOCK_UNLOCKED;
150 
151 static ipx_interface *ipx_primary_net;
152 static ipx_interface *ipx_internal_net;
153 
154 #define IPX_SKB_CB(__skb) ((struct ipx_cb *)&((__skb)->cb[0]))
155 
156 #undef IPX_REFCNT_DEBUG
157 #ifdef IPX_REFCNT_DEBUG
158 atomic_t ipx_sock_nr;
159 #endif
160 
ipxcfg_set_auto_create(char val)161 static void ipxcfg_set_auto_create(char val)
162 {
163 	if (ipxcfg_auto_create_interfaces != val) {
164 		if (val)
165 			MOD_INC_USE_COUNT;
166 		else
167 			MOD_DEC_USE_COUNT;
168 
169 		ipxcfg_auto_create_interfaces = val;
170 	}
171 }
172 
ipxcfg_set_auto_select(char val)173 static void ipxcfg_set_auto_select(char val)
174 {
175 	ipxcfg_auto_select_primary = val;
176 	if (val && !ipx_primary_net)
177 		ipx_primary_net = ipx_interfaces;
178 }
179 
ipxcfg_get_config_data(ipx_config_data * arg)180 static int ipxcfg_get_config_data(ipx_config_data *arg)
181 {
182 	ipx_config_data	vals;
183 
184 	vals.ipxcfg_auto_create_interfaces = ipxcfg_auto_create_interfaces;
185 	vals.ipxcfg_auto_select_primary	   = ipxcfg_auto_select_primary;
186 
187 	return copy_to_user(arg, &vals, sizeof(vals)) ? -EFAULT : 0;
188 }
189 
190 /* Handlers for the socket list. */
191 
ipxitf_hold(ipx_interface * intrfc)192 static inline void ipxitf_hold(ipx_interface *intrfc)
193 {
194 	atomic_inc(&intrfc->refcnt);
195 }
196 
197 static void ipxitf_down(ipx_interface *intrfc);
198 
ipxitf_put(ipx_interface * intrfc)199 static inline void ipxitf_put(ipx_interface *intrfc)
200 {
201 	if (atomic_dec_and_test(&intrfc->refcnt))
202 		ipxitf_down(intrfc);
203 }
204 
205 static void __ipxitf_down(ipx_interface *intrfc);
206 
__ipxitf_put(ipx_interface * intrfc)207 static inline void __ipxitf_put(ipx_interface *intrfc)
208 {
209 	if (atomic_dec_and_test(&intrfc->refcnt))
210 		__ipxitf_down(intrfc);
211 }
212 /*
213  * Note: Sockets may not be removed _during_ an interrupt or inet_bh
214  * handler using this technique. They can be added although we do not
215  * use this facility.
216  */
217 
ipx_remove_socket(struct sock * sk)218 void ipx_remove_socket(struct sock *sk)
219 {
220 	struct sock *s;
221 	ipx_interface *intrfc;
222 
223 	/* Determine interface with which socket is associated */
224 	intrfc = sk->protinfo.af_ipx.intrfc;
225 	if (!intrfc)
226 		goto out;
227 
228 	ipxitf_hold(intrfc);
229 	spin_lock_bh(&intrfc->if_sklist_lock);
230 	s = intrfc->if_sklist;
231 	if (s == sk) {
232 		intrfc->if_sklist = s->next;
233 		goto out_unlock;
234 	}
235 
236 	while (s && s->next) {
237 		if (s->next == sk) {
238 			s->next = sk->next;
239 			goto out_unlock;
240 		}
241 		s = s->next;
242 	}
243 out_unlock:
244 	spin_unlock_bh(&intrfc->if_sklist_lock);
245 	sock_put(sk);
246 	ipxitf_put(intrfc);
247 out:	return;
248 }
249 
ipx_destroy_socket(struct sock * sk)250 static void ipx_destroy_socket(struct sock *sk)
251 {
252 	ipx_remove_socket(sk);
253 	skb_queue_purge(&sk->receive_queue);
254 #ifdef IPX_REFCNT_DEBUG
255         atomic_dec(&ipx_sock_nr);
256         printk(KERN_DEBUG "IPX socket %p released, %d are still alive\n", sk,
257 			atomic_read(&ipx_sock_nr));
258 	if (atomic_read(&sk->refcnt) != 1)
259 		printk(KERN_DEBUG "Destruction sock ipx %p delayed, cnt=%d\n",
260 				sk, atomic_read(&sk->refcnt));
261 #endif
262 	sock_put(sk);
263 }
264 
265 /*
266  * The following code is used to support IPX Interfaces (IPXITF).  An
267  * IPX interface is defined by a physical device and a frame type.
268  */
269 static ipx_route * ipxrtr_lookup(__u32);
270 
271 /* ipxitf_clear_primary_net has to be called with ipx_interfaces_lock held */
272 
ipxitf_clear_primary_net(void)273 static void ipxitf_clear_primary_net(void)
274 {
275 	ipx_primary_net = ipxcfg_auto_select_primary ? ipx_interfaces : NULL;
276 }
277 
__ipxitf_find_using_phys(struct net_device * dev,unsigned short datalink)278 static ipx_interface *__ipxitf_find_using_phys(struct net_device *dev,
279 						unsigned short datalink)
280 {
281 	ipx_interface *i = ipx_interfaces;
282 
283 	while (i && (i->if_dev != dev || i->if_dlink_type != datalink))
284 		i = i->if_next;
285 
286 	return i;
287 }
288 
ipxitf_find_using_phys(struct net_device * dev,unsigned short datalink)289 static ipx_interface *ipxitf_find_using_phys(struct net_device *dev,
290 						unsigned short datalink)
291 {
292 	ipx_interface *i;
293 
294 	spin_lock_bh(&ipx_interfaces_lock);
295 	i = __ipxitf_find_using_phys(dev, datalink);
296 	if (i)
297 		ipxitf_hold(i);
298 	spin_unlock_bh(&ipx_interfaces_lock);
299 	return i;
300 }
301 
ipxitf_find_using_net(__u32 net)302 static ipx_interface *ipxitf_find_using_net(__u32 net)
303 {
304 	ipx_interface *i;
305 
306 	spin_lock_bh(&ipx_interfaces_lock);
307 	if (net)
308 		for (i = ipx_interfaces; i && i->if_netnum != net;
309 		     i = i->if_next)
310 		;
311 	else
312 		i = ipx_primary_net;
313 	if (i)
314 		ipxitf_hold(i);
315 	spin_unlock_bh(&ipx_interfaces_lock);
316 
317 	return i;
318 }
319 
320 /* Sockets are bound to a particular IPX interface. */
ipxitf_insert_socket(ipx_interface * intrfc,struct sock * sk)321 static void ipxitf_insert_socket(ipx_interface *intrfc, struct sock *sk)
322 {
323 	ipxitf_hold(intrfc);
324 	sock_hold(sk);
325 	spin_lock_bh(&intrfc->if_sklist_lock);
326 	sk->protinfo.af_ipx.intrfc = intrfc;
327 	sk->next = NULL;
328 	if (!intrfc->if_sklist)
329 		intrfc->if_sklist = sk;
330 	else {
331 		struct sock *s = intrfc->if_sklist;
332 		while (s->next)
333 			s = s->next;
334 		s->next = sk;
335 	}
336 	spin_unlock_bh(&intrfc->if_sklist_lock);
337 	ipxitf_put(intrfc);
338 }
339 
340 /* caller must hold intrfc->if_sklist_lock */
__ipxitf_find_socket(ipx_interface * intrfc,unsigned short port)341 static struct sock *__ipxitf_find_socket(ipx_interface *intrfc,
342 					 unsigned short port)
343 {
344 	struct sock *s = intrfc->if_sklist;
345 
346 	while (s && s->protinfo.af_ipx.port != port)
347 	     s = s->next;
348 
349 	return s;
350 }
351 
352 /* caller must hold a reference to intrfc */
ipxitf_find_socket(ipx_interface * intrfc,unsigned short port)353 static struct sock *ipxitf_find_socket(ipx_interface *intrfc,
354 					unsigned short port)
355 {
356 	struct sock *s;
357 
358 	spin_lock_bh(&intrfc->if_sklist_lock);
359 	s = __ipxitf_find_socket(intrfc, port);
360 	if (s)
361 		sock_hold(s);
362 	spin_unlock_bh(&intrfc->if_sklist_lock);
363 
364 	return s;
365 }
366 
367 #ifdef CONFIG_IPX_INTERN
ipxitf_find_internal_socket(ipx_interface * intrfc,unsigned char * node,unsigned short port)368 static struct sock *ipxitf_find_internal_socket(ipx_interface *intrfc,
369 			    unsigned char *node, unsigned short port)
370 {
371 	struct sock *s;
372 
373 	ipxitf_hold(intrfc);
374 	spin_lock_bh(&intrfc->if_sklist_lock);
375 	s = intrfc->if_sklist;
376 
377 	while (s) {
378 		if (s->protinfo.af_ipx.port == port &&
379 		    !memcmp(node, s->protinfo.af_ipx.node, IPX_NODE_LEN))
380 			break;
381 		s = s->next;
382 	}
383 	spin_unlock_bh(&intrfc->if_sklist_lock);
384 	ipxitf_put(intrfc);
385 
386 	return s;
387 }
388 #endif
389 
390 static void ipxrtr_del_routes(ipx_interface *);
391 
__ipxitf_down(ipx_interface * intrfc)392 static void __ipxitf_down(ipx_interface *intrfc)
393 {
394 	struct sock *s, *t;
395 
396 	/* Delete all routes associated with this interface */
397 	ipxrtr_del_routes(intrfc);
398 
399 	spin_lock_bh(&intrfc->if_sklist_lock);
400 	/* error sockets */
401 	for (s = intrfc->if_sklist; s;) {
402 		s->err = ENOLINK;
403 		s->error_report(s);
404 		s->protinfo.af_ipx.intrfc = NULL;
405 		s->protinfo.af_ipx.port	  = 0;
406 		s->zapped = 1;	/* Indicates it is no longer bound */
407 		t = s;
408 		s = s->next;
409 		t->next = NULL;
410 	}
411 	intrfc->if_sklist = NULL;
412 	spin_unlock_bh(&intrfc->if_sklist_lock);
413 
414 	/* remove this interface from list */
415 	if (intrfc == ipx_interfaces)
416 		ipx_interfaces = intrfc->if_next;
417 	else {
418 		ipx_interface *i = ipx_interfaces;
419 		while (i && i->if_next != intrfc)
420 		     i = i->if_next;
421 		if (i && i->if_next == intrfc)
422 			i->if_next = intrfc->if_next;
423 	}
424 
425 	/* remove this interface from *special* networks */
426 	if (intrfc == ipx_primary_net)
427 		ipxitf_clear_primary_net();
428 	if (intrfc == ipx_internal_net)
429 		ipx_internal_net = NULL;
430 
431 	if (intrfc->if_dev)
432 		dev_put(intrfc->if_dev);
433 	kfree(intrfc);
434 	MOD_DEC_USE_COUNT;
435 }
436 
ipxitf_down(ipx_interface * intrfc)437 static void ipxitf_down(ipx_interface *intrfc)
438 {
439 	spin_lock_bh(&ipx_interfaces_lock);
440 	__ipxitf_down(intrfc);
441 	spin_unlock_bh(&ipx_interfaces_lock);
442 }
443 
ipxitf_device_event(struct notifier_block * notifier,unsigned long event,void * ptr)444 static int ipxitf_device_event(struct notifier_block *notifier,
445 				unsigned long event, void *ptr)
446 {
447 	struct net_device *dev = ptr;
448 	ipx_interface *i, *tmp;
449 
450 	if (event != NETDEV_DOWN && event != NETDEV_UP)
451 		goto out;
452 
453 	spin_lock_bh(&ipx_interfaces_lock);
454 	for (i = ipx_interfaces; i;) {
455 		tmp = i->if_next;
456 		if (i->if_dev == dev) {
457 			if (event == NETDEV_UP)
458 				ipxitf_hold(i);
459 			else
460 				__ipxitf_put(i);
461 		}
462 		i = tmp;
463 	}
464 	spin_unlock_bh(&ipx_interfaces_lock);
465 out:	return NOTIFY_DONE;
466 }
467 
ipxitf_def_skb_handler(struct sock * sock,struct sk_buff * skb)468 static void ipxitf_def_skb_handler(struct sock *sock, struct sk_buff *skb)
469 {
470 	if (sock_queue_rcv_skb(sock, skb) < 0)
471 		kfree_skb(skb);
472 }
473 
474 /*
475  * On input skb->sk is NULL. Nobody is charged for the memory.
476  */
477 
478 /* caller must hold a reference to intrfc */
479 
480 #ifdef CONFIG_IPX_INTERN
ipxitf_demux_socket(ipx_interface * intrfc,struct sk_buff * skb,int copy)481 static int ipxitf_demux_socket(ipx_interface *intrfc, struct sk_buff *skb,
482 				int copy)
483 {
484 	struct ipxhdr *ipx = skb->nh.ipxh;
485 	int is_broadcast = !memcmp(ipx->ipx_dest.node, ipx_broadcast_node,
486 				   IPX_NODE_LEN);
487 	struct sock *s;
488 	int ret;
489 
490 	spin_lock_bh(&intrfc->if_sklist_lock);
491 	s = intrfc->if_sklist;
492 
493 	while (s) {
494 		if (s->protinfo.af_ipx.port == ipx->ipx_dest.sock &&
495 		    (is_broadcast || !memcmp(ipx->ipx_dest.node,
496 					     s->protinfo.af_ipx.node,
497 					     IPX_NODE_LEN))) {
498 			/* We found a socket to which to send */
499 			struct sk_buff *skb1;
500 
501 			if (copy) {
502 				skb1 = skb_clone(skb, GFP_ATOMIC);
503 				ret = -ENOMEM;
504 				if (!skb1)
505 					goto out;
506 			} else {
507 				skb1 = skb;
508 				copy = 1; /* skb may only be used once */
509 			}
510 			ipxitf_def_skb_handler(s, skb1);
511 
512 			/* On an external interface, one socket can listen */
513 			if (intrfc != ipx_internal_net)
514 				break;
515 		}
516 		s = s->next;
517 	}
518 
519 	/* skb was solely for us, and we did not make a copy, so free it. */
520 	if (!copy)
521 		kfree_skb(skb);
522 
523 	ret = 0;
524 out:	spin_unlock_bh(&intrfc->if_sklist_lock);
525 	return ret;
526 }
527 #else
ncp_connection_hack(ipx_interface * intrfc,struct ipxhdr * ipx)528 static struct sock *ncp_connection_hack(ipx_interface *intrfc,
529 					struct ipxhdr *ipx)
530 {
531 	/* The packet's target is a NCP connection handler. We want to hand it
532 	 * to the correct socket directly within the kernel, so that the
533 	 * mars_nwe packet distribution process does not have to do it. Here we
534 	 * only care about NCP and BURST packets.
535 	 *
536 	 * You might call this a hack, but believe me, you do not want a
537 	 * complete NCP layer in the kernel, and this is VERY fast as well. */
538 	struct sock *sk = NULL;
539  	int connection = 0;
540 	u8 *ncphdr = (u8 *)(ipx + 1);
541 
542  	if (*ncphdr == 0x22 && *(ncphdr + 1) == 0x22) /* NCP request */
543 		connection = (((int) *(ncphdr + 5)) << 8) | (int) *(ncphdr + 3);
544 	else if (*ncphdr == 0x77 && *(ncphdr + 1) == 0x77) /* BURST packet */
545 		connection = (((int) *(ncphdr + 9)) << 8) | (int) *(ncphdr + 8);
546 
547 	if (connection) {
548 		/* Now we have to look for a special NCP connection handling
549 		 * socket. Only these sockets have ipx_ncp_conn != 0, set by
550 		 * SIOCIPXNCPCONN. */
551 		spin_lock_bh(&intrfc->if_sklist_lock);
552 		for (sk = intrfc->if_sklist;
553 		     sk && sk->protinfo.af_ipx.ipx_ncp_conn != connection;
554 		     sk = sk->next);
555 		if (sk)
556 			sock_hold(sk);
557 		spin_unlock_bh(&intrfc->if_sklist_lock);
558 	}
559 	return sk;
560 }
561 
ipxitf_demux_socket(ipx_interface * intrfc,struct sk_buff * skb,int copy)562 static int ipxitf_demux_socket(ipx_interface *intrfc, struct sk_buff *skb,
563 				int copy)
564 {
565 	struct ipxhdr *ipx = skb->nh.ipxh;
566 	struct sock *sock1 = NULL, *sock2 = NULL;
567 	struct sk_buff *skb1 = NULL, *skb2 = NULL;
568 	int ret;
569 
570 	if (intrfc == ipx_primary_net && ntohs(ipx->ipx_dest.sock) == 0x451)
571 		sock1 = ncp_connection_hack(intrfc, ipx);
572         if (!sock1)
573 		/* No special socket found, forward the packet the normal way */
574 		sock1 = ipxitf_find_socket(intrfc, ipx->ipx_dest.sock);
575 
576 	/*
577 	 * We need to check if there is a primary net and if
578 	 * this is addressed to one of the *SPECIAL* sockets because
579 	 * these need to be propagated to the primary net.
580 	 * The *SPECIAL* socket list contains: 0x452(SAP), 0x453(RIP) and
581 	 * 0x456(Diagnostic).
582 	 */
583 
584 	if (ipx_primary_net && intrfc != ipx_primary_net) {
585 		const int dsock = ntohs(ipx->ipx_dest.sock);
586 
587 		if (dsock == 0x452 || dsock == 0x453 || dsock == 0x456)
588 			/* The appropriate thing to do here is to dup the
589 			 * packet and route to the primary net interface via
590 			 * ipxitf_send; however, we'll cheat and just demux it
591 			 * here. */
592 			sock2 = ipxitf_find_socket(ipx_primary_net,
593 							ipx->ipx_dest.sock);
594 	}
595 
596 	/*
597 	 * If there is nothing to do return. The kfree will cancel any charging.
598 	 */
599 	if (!sock1 && !sock2) {
600 		if (!copy)
601 			kfree_skb(skb);
602 		return 0;
603 	}
604 
605 	/*
606 	 * This next segment of code is a little awkward, but it sets it up
607 	 * so that the appropriate number of copies of the SKB are made and
608 	 * that skb1 and skb2 point to it (them) so that it (they) can be
609 	 * demuxed to sock1 and/or sock2.  If we are unable to make enough
610 	 * copies, we do as much as is possible.
611 	 */
612 
613 	if (copy)
614 		skb1 = skb_clone(skb, GFP_ATOMIC);
615 	else
616 		skb1 = skb;
617 
618 	ret = -ENOMEM;
619 	if (!skb1)
620 		goto out;
621 
622 	/* Do we need 2 SKBs? */
623 	if (sock1 && sock2)
624 		skb2 = skb_clone(skb1, GFP_ATOMIC);
625 	else
626 		skb2 = skb1;
627 
628 	if (sock1)
629 		ipxitf_def_skb_handler(sock1, skb1);
630 
631 	ret = -ENOMEM;
632 	if (!skb2)
633 		goto out;
634 
635 	if (sock2)
636 		ipxitf_def_skb_handler(sock2, skb2);
637 
638 	ret = 0;
639 out:	if (sock1)
640 		sock_put(sock1);
641 	if (sock2)
642 		sock_put(sock2);
643 	return ret;
644 }
645 #endif	/* CONFIG_IPX_INTERN */
646 
ipxitf_adjust_skbuff(ipx_interface * intrfc,struct sk_buff * skb)647 static struct sk_buff *ipxitf_adjust_skbuff(ipx_interface *intrfc,
648 					    struct sk_buff *skb)
649 {
650 	struct sk_buff *skb2;
651 	int in_offset = skb->h.raw - skb->head;
652 	int out_offset = intrfc->if_ipx_offset;
653 	int len;
654 
655 	/* Hopefully, most cases */
656 	if (in_offset >= out_offset)
657 		return skb;
658 
659 	/* Need new SKB */
660 	len  = skb->len + out_offset;
661 	skb2 = alloc_skb(len, GFP_ATOMIC);
662 	if (skb2) {
663 		skb_reserve(skb2, out_offset);
664 		skb2->nh.raw = skb2->h.raw = skb_put(skb2, skb->len);
665 		memcpy(skb2->h.raw, skb->h.raw, skb->len);
666 		memcpy(skb2->cb, skb->cb, sizeof(skb->cb));
667 	}
668 	kfree_skb(skb);
669 	return skb2;
670 }
671 
672 /* caller must hold a reference to intrfc and the skb has to be unshared */
673 
ipxitf_send(ipx_interface * intrfc,struct sk_buff * skb,char * node)674 static int ipxitf_send(ipx_interface *intrfc, struct sk_buff *skb, char *node)
675 {
676 	struct ipxhdr *ipx = skb->nh.ipxh;
677 	struct net_device *dev = intrfc->if_dev;
678 	struct datalink_proto *dl = intrfc->if_dlink;
679 	char dest_node[IPX_NODE_LEN];
680 	int send_to_wire = 1;
681 	int addr_len;
682 
683 	ipx->ipx_tctrl = IPX_SKB_CB(skb)->ipx_tctrl;
684 	ipx->ipx_dest.net = IPX_SKB_CB(skb)->ipx_dest_net;
685 	ipx->ipx_source.net = IPX_SKB_CB(skb)->ipx_source_net;
686 
687 	/* see if we need to include the netnum in the route list */
688 	if (IPX_SKB_CB(skb)->last_hop.index >= 0) {
689 		u32 *last_hop = (u32 *)(((u8 *) skb->data) +
690 				sizeof(struct ipxhdr) +
691 				IPX_SKB_CB(skb)->last_hop.index *
692 				sizeof(u32));
693 		*last_hop = IPX_SKB_CB(skb)->last_hop.netnum;
694 		IPX_SKB_CB(skb)->last_hop.index = -1;
695 	}
696 
697 	/*
698 	 * We need to know how many skbuffs it will take to send out this
699 	 * packet to avoid unnecessary copies.
700 	 */
701 
702 	if (!dl || !dev || dev->flags & IFF_LOOPBACK)
703 		send_to_wire = 0;	/* No non looped */
704 
705 	/*
706 	 * See if this should be demuxed to sockets on this interface
707 	 *
708 	 * We want to ensure the original was eaten or that we only use
709 	 * up clones.
710 	 */
711 
712 	if (ipx->ipx_dest.net == intrfc->if_netnum) {
713 		/*
714 		 * To our own node, loop and free the original.
715 		 * The internal net will receive on all node address.
716 		 */
717 		if (intrfc == ipx_internal_net ||
718 		    !memcmp(intrfc->if_node, node, IPX_NODE_LEN)) {
719 			/* Don't charge sender */
720 			skb_orphan(skb);
721 
722 			/* Will charge receiver */
723 			return ipxitf_demux_socket(intrfc, skb, 0);
724 		}
725 
726 		/* Broadcast, loop and possibly keep to send on. */
727 		if (!memcmp(ipx_broadcast_node, node, IPX_NODE_LEN)) {
728 			if (!send_to_wire)
729 				skb_orphan(skb);
730 			ipxitf_demux_socket(intrfc, skb, send_to_wire);
731 			if (!send_to_wire)
732 				goto out;
733 		}
734 	}
735 
736 	/*
737 	 * If the originating net is not equal to our net; this is routed
738 	 * We are still charging the sender. Which is right - the driver
739 	 * free will handle this fairly.
740 	 */
741 	if (ipx->ipx_source.net != intrfc->if_netnum) {
742 		/*
743 		 * Unshare the buffer before modifying the count in
744 		 * case its a flood or tcpdump
745 		 */
746 		skb = skb_unshare(skb, GFP_ATOMIC);
747 		if (!skb)
748 			goto out;
749 		if (++ipx->ipx_tctrl > ipxcfg_max_hops)
750 			send_to_wire = 0;
751 	}
752 
753 	if (!send_to_wire) {
754 		kfree_skb(skb);
755 		goto out;
756 	}
757 
758 	/* Determine the appropriate hardware address */
759 	addr_len = dev->addr_len;
760 	if (!memcmp(ipx_broadcast_node, node, IPX_NODE_LEN))
761 		memcpy(dest_node, dev->broadcast, addr_len);
762 	else
763 		memcpy(dest_node, &(node[IPX_NODE_LEN-addr_len]), addr_len);
764 
765 	/* Make any compensation for differing physical/data link size */
766 	skb = ipxitf_adjust_skbuff(intrfc, skb);
767 	if (!skb)
768 		goto out;
769 
770 	/* set up data link and physical headers */
771 	skb->dev	= dev;
772 	skb->protocol	= __constant_htons(ETH_P_IPX);
773 	dl->datalink_header(dl, skb, dest_node);
774 
775 	/* Send it out */
776 	dev_queue_xmit(skb);
777 out:	return 0;
778 }
779 
780 static int ipxrtr_add_route(__u32, ipx_interface *, unsigned char *);
781 
ipxitf_add_local_route(ipx_interface * intrfc)782 static int ipxitf_add_local_route(ipx_interface *intrfc)
783 {
784 	return ipxrtr_add_route(intrfc->if_netnum, intrfc, NULL);
785 }
786 
787 static const char * ipx_frame_name(unsigned short);
788 static const char * ipx_device_name(ipx_interface *);
789 static void ipxitf_discover_netnum(ipx_interface *intrfc, struct sk_buff *skb);
790 static int ipxitf_pprop(ipx_interface *intrfc, struct sk_buff *skb);
791 
ipxitf_rcv(ipx_interface * intrfc,struct sk_buff * skb)792 static int ipxitf_rcv(ipx_interface *intrfc, struct sk_buff *skb)
793 {
794 	struct ipxhdr	*ipx = skb->nh.ipxh;
795 	int ret = 0;
796 
797 	ipxitf_hold(intrfc);
798 
799 	/* See if we should update our network number */
800 	if (!intrfc->if_netnum) /* net number of intrfc not known yet */
801  		ipxitf_discover_netnum(intrfc, skb);
802 
803 	IPX_SKB_CB(skb)->last_hop.index = -1;
804 	if (ipx->ipx_type == IPX_TYPE_PPROP) {
805 		ret = ipxitf_pprop(intrfc, skb);
806 		if (ret)
807 			goto out_free_skb;
808 	}
809 
810 	/* local processing follows */
811 	if (!IPX_SKB_CB(skb)->ipx_dest_net)
812 		IPX_SKB_CB(skb)->ipx_dest_net = intrfc->if_netnum;
813 	if (!IPX_SKB_CB(skb)->ipx_source_net)
814 		IPX_SKB_CB(skb)->ipx_source_net = intrfc->if_netnum;
815 
816 	/* it doesn't make sense to route a pprop packet, there's no meaning
817 	 * in the ipx_dest_net for such packets */
818 	if (ipx->ipx_type != IPX_TYPE_PPROP &&
819 	    intrfc->if_netnum != IPX_SKB_CB(skb)->ipx_dest_net) {
820 		/* We only route point-to-point packets. */
821 		if (skb->pkt_type == PACKET_HOST) {
822 			skb = skb_unshare(skb, GFP_ATOMIC);
823 			if (skb)
824 				ret = ipxrtr_route_skb(skb);
825 			goto out_intrfc;
826 		}
827 
828 		goto out_free_skb;
829 	}
830 
831 	/* see if we should keep it */
832 	if (!memcmp(ipx_broadcast_node, ipx->ipx_dest.node, IPX_NODE_LEN) ||
833 	    !memcmp(intrfc->if_node, ipx->ipx_dest.node, IPX_NODE_LEN)) {
834 		ret = ipxitf_demux_socket(intrfc, skb, 0);
835 		goto out_intrfc;
836 	}
837 
838 	/* we couldn't pawn it off so unload it */
839 out_free_skb:
840 	kfree_skb(skb);
841 out_intrfc:
842 	ipxitf_put(intrfc);
843 	return ret;
844 }
845 
ipxitf_discover_netnum(ipx_interface * intrfc,struct sk_buff * skb)846 static void ipxitf_discover_netnum(ipx_interface *intrfc, struct sk_buff *skb)
847 {
848 	const struct ipx_cb *cb = IPX_SKB_CB(skb);
849 
850 	/* see if this is an intra packet: source_net == dest_net */
851 	if (cb->ipx_source_net == cb->ipx_dest_net && cb->ipx_source_net) {
852 		ipx_interface *i = ipxitf_find_using_net(cb->ipx_source_net);
853 		/* NB: NetWare servers lie about their hop count so we
854 		 * dropped the test based on it. This is the best way
855 		 * to determine this is a 0 hop count packet. */
856 		if (!i) {
857 			intrfc->if_netnum = cb->ipx_source_net;
858 			ipxitf_add_local_route(intrfc);
859 		} else {
860 			printk(KERN_WARNING "IPX: Network number collision "
861 				"%lx\n        %s %s and %s %s\n",
862 				(unsigned long) htonl(cb->ipx_source_net),
863 				ipx_device_name(i),
864 				ipx_frame_name(i->if_dlink_type),
865 				ipx_device_name(intrfc),
866 				ipx_frame_name(intrfc->if_dlink_type));
867 			ipxitf_put(i);
868 		}
869 	}
870 }
871 
872 /**
873  * ipxitf_pprop - Process packet propagation IPX packet type 0x14, used for
874  * 		  NetBIOS broadcasts
875  * @intrfc: IPX interface receiving this packet
876  * @skb: Received packet
877  *
878  * Checks if packet is valid: if its more than %IPX_MAX_PPROP_HOPS hops or if it
879  * is smaller than a IPX header + the room for %IPX_MAX_PPROP_HOPS hops we drop
880  * it, not even processing it locally, if it has exact %IPX_MAX_PPROP_HOPS we
881  * don't broadcast it, but process it locally. See chapter 5 of Novell's "IPX
882  * RIP and SAP Router Specification", Part Number 107-000029-001.
883  *
884  * If it is valid, check if we have pprop broadcasting enabled by the user,
885  * if not, just return zero for local processing.
886  *
887  * If it is enabled check the packet and don't broadcast it if we have already
888  * seen this packet.
889  *
890  * Broadcast: send it to the interfaces that aren't on the packet visited nets
891  * array, just after the IPX header.
892  *
893  * Returns -EINVAL for invalid packets, so that the calling function drops
894  * the packet without local processing. 0 if packet is to be locally processed.
895  */
ipxitf_pprop(ipx_interface * intrfc,struct sk_buff * skb)896 static int ipxitf_pprop(ipx_interface *intrfc, struct sk_buff *skb)
897 {
898 	struct ipxhdr *ipx = skb->nh.ipxh;
899 	int i, ret = -EINVAL;
900 	ipx_interface *ifcs;
901 	char *c;
902 	u32 *l;
903 
904 	/* Illegal packet - too many hops or too short */
905 	/* We decide to throw it away: no broadcasting, no local processing.
906 	 * NetBIOS unaware implementations route them as normal packets -
907 	 * tctrl <= 15, any data payload... */
908 	if (IPX_SKB_CB(skb)->ipx_tctrl > IPX_MAX_PPROP_HOPS ||
909 	    ntohs(ipx->ipx_pktsize) < sizeof(struct ipxhdr) +
910 	    				IPX_MAX_PPROP_HOPS * sizeof(u32))
911 		goto out;
912 	/* are we broadcasting this damn thing? */
913 	ret = 0;
914 	if (!sysctl_ipx_pprop_broadcasting)
915 		goto out;
916 	/* We do broadcast packet on the IPX_MAX_PPROP_HOPS hop, but we
917 	 * process it locally. All previous hops broadcasted it, and process it
918 	 * locally. */
919 	if (IPX_SKB_CB(skb)->ipx_tctrl == IPX_MAX_PPROP_HOPS)
920 		goto out;
921 
922 	c = ((u8 *) ipx) + sizeof(struct ipxhdr);
923 	l = (u32 *) c;
924 
925 	/* Don't broadcast packet if already seen this net */
926 	for (i = 0; i < IPX_SKB_CB(skb)->ipx_tctrl; i++)
927 		if (*l++ == intrfc->if_netnum)
928 			goto out;
929 
930 	/* < IPX_MAX_PPROP_HOPS hops && input interface not in list. Save the
931 	 * position where we will insert recvd netnum into list, later on,
932 	 * in ipxitf_send */
933 	IPX_SKB_CB(skb)->last_hop.index = i;
934 	IPX_SKB_CB(skb)->last_hop.netnum = intrfc->if_netnum;
935 	/* xmit on all other interfaces... */
936 	spin_lock_bh(&ipx_interfaces_lock);
937 	for (ifcs = ipx_interfaces; ifcs; ifcs = ifcs->if_next) {
938 		/* Except unconfigured interfaces */
939 		if (!ifcs->if_netnum)
940 			continue;
941 
942 		/* That aren't in the list */
943 		if (ifcs == intrfc)
944 			continue;
945 		l = (__u32 *) c;
946 		/* don't consider the last entry in the packet list,
947 		 * it is our netnum, and it is not there yet */
948 		for (i = 0; i < IPX_SKB_CB(skb)->ipx_tctrl; i++)
949 			if (ifcs->if_netnum == *l++)
950 				break;
951 		if (i == IPX_SKB_CB(skb)->ipx_tctrl) {
952 			struct sk_buff *s = skb_copy(skb, GFP_ATOMIC);
953 
954 			if (s) {
955 				IPX_SKB_CB(s)->ipx_dest_net = ifcs->if_netnum;
956 				ipxrtr_route_skb(s);
957 			}
958 		}
959 	}
960 	spin_unlock_bh(&ipx_interfaces_lock);
961 out:	return ret;
962 }
963 
ipxitf_insert(ipx_interface * intrfc)964 static void ipxitf_insert(ipx_interface *intrfc)
965 {
966 	intrfc->if_next = NULL;
967 	spin_lock_bh(&ipx_interfaces_lock);
968 	if (!ipx_interfaces)
969 		ipx_interfaces = intrfc;
970 	else {
971 		ipx_interface *i = ipx_interfaces;
972 		while (i->if_next)
973 			i = i->if_next;
974 		i->if_next = intrfc;
975 	}
976 	spin_unlock_bh(&ipx_interfaces_lock);
977 
978 	if (ipxcfg_auto_select_primary && !ipx_primary_net)
979 		ipx_primary_net = intrfc;
980 }
981 
ipxitf_alloc(struct net_device * dev,__u32 netnum,unsigned short dlink_type,struct datalink_proto * dlink,unsigned char internal,int ipx_offset)982 static ipx_interface *ipxitf_alloc(struct net_device *dev, __u32 netnum,
983 				   unsigned short dlink_type,
984 				   struct datalink_proto *dlink,
985 				   unsigned char internal, int ipx_offset)
986 {
987 	ipx_interface *intrfc = kmalloc(sizeof(*intrfc), GFP_ATOMIC);
988 
989 	if (intrfc) {
990 		intrfc->if_dev		= dev;
991 		intrfc->if_netnum	= netnum;
992 		intrfc->if_dlink_type 	= dlink_type;
993 		intrfc->if_dlink 	= dlink;
994 		intrfc->if_internal 	= internal;
995 		intrfc->if_ipx_offset 	= ipx_offset;
996 		intrfc->if_sknum 	= IPX_MIN_EPHEMERAL_SOCKET;
997 		intrfc->if_sklist 	= NULL;
998 		atomic_set(&intrfc->refcnt, 1);
999 		spin_lock_init(&intrfc->if_sklist_lock);
1000 		MOD_INC_USE_COUNT;
1001 	}
1002 
1003 	return intrfc;
1004 }
1005 
ipxitf_create_internal(ipx_interface_definition * idef)1006 static int ipxitf_create_internal(ipx_interface_definition *idef)
1007 {
1008 	ipx_interface *intrfc;
1009 	int ret = -EEXIST;
1010 
1011 	/* Only one primary network allowed */
1012 	if (ipx_primary_net)
1013 		goto out;
1014 
1015 	/* Must have a valid network number */
1016 	ret = -EADDRNOTAVAIL;
1017 	if (!idef->ipx_network)
1018 		goto out;
1019 	intrfc = ipxitf_find_using_net(idef->ipx_network);
1020 	ret = -EADDRINUSE;
1021 	if (intrfc) {
1022 		ipxitf_put(intrfc);
1023 		goto out;
1024 	}
1025 	intrfc = ipxitf_alloc(NULL, idef->ipx_network, 0, NULL, 1, 0);
1026 	ret = -EAGAIN;
1027 	if (!intrfc)
1028 		goto out;
1029 	memcpy((char *)&(intrfc->if_node), idef->ipx_node, IPX_NODE_LEN);
1030 	ipx_internal_net = ipx_primary_net = intrfc;
1031 	ipxitf_hold(intrfc);
1032 	ipxitf_insert(intrfc);
1033 
1034 	ret = ipxitf_add_local_route(intrfc);
1035 	ipxitf_put(intrfc);
1036 out:	return ret;
1037 }
1038 
ipx_map_frame_type(unsigned char type)1039 static int ipx_map_frame_type(unsigned char type)
1040 {
1041 	int ret = 0;
1042 
1043 	switch (type) {
1044 		case IPX_FRAME_ETHERII:
1045 			ret = __constant_htons(ETH_P_IPX);
1046 			break;
1047 		case IPX_FRAME_8022:
1048 			ret = __constant_htons(ETH_P_802_2);
1049 			break;
1050 		case IPX_FRAME_SNAP:
1051 			ret = __constant_htons(ETH_P_SNAP);
1052 			break;
1053 		case IPX_FRAME_8023:
1054 			ret = __constant_htons(ETH_P_802_3);
1055 			break;
1056 	}
1057 
1058 	return ret;
1059 }
1060 
ipxitf_create(ipx_interface_definition * idef)1061 static int ipxitf_create(ipx_interface_definition *idef)
1062 {
1063 	struct net_device *dev;
1064 	unsigned short dlink_type = 0;
1065 	struct datalink_proto *datalink = NULL;
1066 	ipx_interface *intrfc;
1067 	int err;
1068 
1069 	if (idef->ipx_special == IPX_INTERNAL) {
1070 		err = ipxitf_create_internal(idef);
1071 		goto out;
1072 	}
1073 
1074 	err = -EEXIST;
1075 	if (idef->ipx_special == IPX_PRIMARY && ipx_primary_net)
1076 		goto out;
1077 
1078 	intrfc = ipxitf_find_using_net(idef->ipx_network);
1079 	err = -EADDRINUSE;
1080 	if (idef->ipx_network && intrfc) {
1081 		ipxitf_put(intrfc);
1082 		goto out;
1083 	}
1084 
1085 	if (intrfc)
1086 		ipxitf_put(intrfc);
1087 
1088 	dev = dev_get_by_name(idef->ipx_device);
1089 	err = -ENODEV;
1090 	if (!dev)
1091 		goto out;
1092 
1093 	switch (idef->ipx_dlink_type) {
1094 		case IPX_FRAME_TR_8022:
1095 			printk(KERN_WARNING "IPX frame type 802.2TR is "
1096 				"obsolete Use 802.2 instead.\n");
1097 			/* fall through */
1098 		case IPX_FRAME_8022:
1099 			dlink_type 	= __constant_htons(ETH_P_802_2);
1100 			datalink 	= p8022_datalink;
1101 			break;
1102 		case IPX_FRAME_ETHERII:
1103 			if (dev->type != ARPHRD_IEEE802) {
1104 				dlink_type 	= __constant_htons(ETH_P_IPX);
1105 				datalink 	= pEII_datalink;
1106 				break;
1107 			} else
1108 				printk(KERN_WARNING "IPX frame type EtherII "
1109 					"over token-ring is obsolete. Use SNAP "
1110 					"instead.\n");
1111 			/* fall through */
1112 		case IPX_FRAME_SNAP:
1113 			dlink_type 	= __constant_htons(ETH_P_SNAP);
1114 			datalink 	= pSNAP_datalink;
1115 			break;
1116 		case IPX_FRAME_8023:
1117 			dlink_type 	= __constant_htons(ETH_P_802_3);
1118 			datalink 	= p8023_datalink;
1119 			break;
1120 		case IPX_FRAME_NONE:
1121 		default:
1122 			err = -EPROTONOSUPPORT;
1123 			goto out_dev;
1124 	}
1125 
1126 	err = -ENETDOWN;
1127 	if (!(dev->flags & IFF_UP))
1128 		goto out_dev;
1129 
1130 	/* Check addresses are suitable */
1131 	err = -EINVAL;
1132 	if (dev->addr_len > IPX_NODE_LEN)
1133 		goto out_dev;
1134 
1135 	intrfc = ipxitf_find_using_phys(dev, dlink_type);
1136 	if (!intrfc) {
1137 		/* Ok now create */
1138 		intrfc = ipxitf_alloc(dev, idef->ipx_network, dlink_type,
1139 				      datalink, 0, dev->hard_header_len +
1140 					datalink->header_length);
1141 		err = -EAGAIN;
1142 		if (!intrfc)
1143 			goto out_dev;
1144 		/* Setup primary if necessary */
1145 		if (idef->ipx_special == IPX_PRIMARY)
1146 			ipx_primary_net = intrfc;
1147 		if (!memcmp(idef->ipx_node, "\000\000\000\000\000\000",
1148 			    IPX_NODE_LEN)) {
1149 			memset(intrfc->if_node, 0, IPX_NODE_LEN);
1150 			memcpy(intrfc->if_node + IPX_NODE_LEN - dev->addr_len,
1151 				dev->dev_addr, dev->addr_len);
1152 		} else
1153 			memcpy(intrfc->if_node, idef->ipx_node, IPX_NODE_LEN);
1154 		ipxitf_hold(intrfc);
1155 		ipxitf_insert(intrfc);
1156 	}
1157 
1158 
1159 	/* If the network number is known, add a route */
1160 	err = 0;
1161 	if (!intrfc->if_netnum)
1162 		goto out_intrfc;
1163 
1164 	err = ipxitf_add_local_route(intrfc);
1165 out_intrfc:
1166 	ipxitf_put(intrfc);
1167 	goto out;
1168 out_dev:
1169 	dev_put(dev);
1170 out:	return err;
1171 }
1172 
ipxitf_delete(ipx_interface_definition * idef)1173 static int ipxitf_delete(ipx_interface_definition *idef)
1174 {
1175 	struct net_device *dev = NULL;
1176 	unsigned short dlink_type = 0;
1177 	ipx_interface *intrfc;
1178 	int ret = 0;
1179 
1180 	spin_lock_bh(&ipx_interfaces_lock);
1181 	if (idef->ipx_special == IPX_INTERNAL) {
1182 		if (ipx_internal_net) {
1183 			__ipxitf_put(ipx_internal_net);
1184 			goto out;
1185 		}
1186 		ret = -ENOENT;
1187 		goto out;
1188 	}
1189 
1190 	dlink_type = ipx_map_frame_type(idef->ipx_dlink_type);
1191 	ret = -EPROTONOSUPPORT;
1192 	if (!dlink_type)
1193 		goto out;
1194 
1195 	dev = __dev_get_by_name(idef->ipx_device);
1196 	ret = -ENODEV;
1197 	if (!dev)
1198 		goto out;
1199 
1200 	intrfc = __ipxitf_find_using_phys(dev, dlink_type);
1201 	ret = -EINVAL;
1202 	if (!intrfc)
1203 		goto out;
1204 	__ipxitf_put(intrfc);
1205 
1206 	ret = 0;
1207 out:	spin_unlock_bh(&ipx_interfaces_lock);
1208 	return ret;
1209 }
1210 
ipxitf_auto_create(struct net_device * dev,unsigned short dlink_type)1211 static ipx_interface *ipxitf_auto_create(struct net_device *dev,
1212 					 unsigned short dlink_type)
1213 {
1214 	ipx_interface *intrfc = NULL;
1215 	struct datalink_proto *datalink;
1216 
1217 	if (!dev)
1218 		goto out;
1219 
1220 	/* Check addresses are suitable */
1221 	if (dev->addr_len > IPX_NODE_LEN)
1222 		goto out;
1223 
1224 	switch (htons(dlink_type)) {
1225 		case ETH_P_IPX:
1226 			datalink = pEII_datalink;
1227 			break;
1228 
1229 		case ETH_P_802_2:
1230 			datalink = p8022_datalink;
1231 			break;
1232 
1233 		case ETH_P_SNAP:
1234 			datalink = pSNAP_datalink;
1235 			break;
1236 
1237 		case ETH_P_802_3:
1238 			datalink = p8023_datalink;
1239 			break;
1240 
1241 		default:
1242 			goto out;
1243 	}
1244 
1245 	intrfc = ipxitf_alloc(dev, 0, dlink_type, datalink, 0,
1246 				dev->hard_header_len + datalink->header_length);
1247 
1248 	if (intrfc) {
1249 		memset(intrfc->if_node, 0, IPX_NODE_LEN);
1250 		memcpy((char *)&(intrfc->if_node[IPX_NODE_LEN-dev->addr_len]),
1251 			dev->dev_addr, dev->addr_len);
1252 		spin_lock_init(&intrfc->if_sklist_lock);
1253 		atomic_set(&intrfc->refcnt, 1);
1254 		ipxitf_insert(intrfc);
1255 		dev_hold(dev);
1256 	}
1257 
1258 out:	return intrfc;
1259 }
1260 
ipxitf_ioctl(unsigned int cmd,void * arg)1261 static int ipxitf_ioctl(unsigned int cmd, void *arg)
1262 {
1263 	struct ifreq ifr;
1264 	int val;
1265 
1266 	switch (cmd) {
1267 		case SIOCSIFADDR: {
1268 			struct sockaddr_ipx *sipx;
1269 			ipx_interface_definition f;
1270 
1271 			if (copy_from_user(&ifr, arg, sizeof(ifr)))
1272 				return -EFAULT;
1273 
1274 			sipx = (struct sockaddr_ipx *)&ifr.ifr_addr;
1275 			if (sipx->sipx_family != AF_IPX)
1276 				return -EINVAL;
1277 
1278 			f.ipx_network = sipx->sipx_network;
1279 			memcpy(f.ipx_device, ifr.ifr_name,
1280 				sizeof(f.ipx_device));
1281 			memcpy(f.ipx_node, sipx->sipx_node, IPX_NODE_LEN);
1282 			f.ipx_dlink_type = sipx->sipx_type;
1283 			f.ipx_special = sipx->sipx_special;
1284 
1285 			if (sipx->sipx_action == IPX_DLTITF)
1286 				return ipxitf_delete(&f);
1287 			else
1288 				return ipxitf_create(&f);
1289 		}
1290 
1291 		case SIOCGIFADDR: {
1292 			int err = 0;
1293 			struct sockaddr_ipx *sipx;
1294 			ipx_interface *ipxif;
1295 			struct net_device *dev;
1296 
1297 			if (copy_from_user(&ifr, arg, sizeof(ifr)))
1298 				return -EFAULT;
1299 
1300 			sipx = (struct sockaddr_ipx *)&ifr.ifr_addr;
1301 			dev = __dev_get_by_name(ifr.ifr_name);
1302 			if (!dev)
1303 				return -ENODEV;
1304 
1305 			ipxif = ipxitf_find_using_phys(dev, ipx_map_frame_type(sipx->sipx_type));
1306 			if (!ipxif)
1307 				return -EADDRNOTAVAIL;
1308 
1309 			sipx->sipx_family	= AF_IPX;
1310 			sipx->sipx_network	= ipxif->if_netnum;
1311 			memcpy(sipx->sipx_node, ipxif->if_node,
1312 				sizeof(sipx->sipx_node));
1313 			if (copy_to_user(arg, &ifr, sizeof(ifr)))
1314 				err = -EFAULT;
1315 
1316 			ipxitf_put(ipxif);
1317 			return err;
1318 		}
1319 
1320 		case SIOCAIPXITFCRT:
1321 			if (get_user(val, (unsigned char *) arg))
1322 				return -EFAULT;
1323 			ipxcfg_set_auto_create(val);
1324 			break;
1325 
1326 		case SIOCAIPXPRISLT:
1327 			if (get_user(val, (unsigned char *) arg))
1328 				return -EFAULT;
1329 			ipxcfg_set_auto_select(val);
1330 			break;
1331 
1332 		default:
1333 			return -EINVAL;
1334 	}
1335 
1336 	return 0;
1337 }
1338 
1339 /* Routing tables for the IPX socket layer. */
1340 
ipxrtr_hold(ipx_route * rt)1341 static inline void ipxrtr_hold(ipx_route *rt)
1342 {
1343 	atomic_inc(&rt->refcnt);
1344 }
1345 
ipxrtr_put(ipx_route * rt)1346 static inline void ipxrtr_put(ipx_route *rt)
1347 {
1348 	if (atomic_dec_and_test(&rt->refcnt))
1349 		kfree(rt);
1350 }
1351 
ipxrtr_lookup(__u32 net)1352 static ipx_route *ipxrtr_lookup(__u32 net)
1353 {
1354 	ipx_route *r;
1355 
1356 	read_lock_bh(&ipx_routes_lock);
1357 	for (r = ipx_routes; r && r->ir_net != net; r = r->ir_next)
1358 		;
1359 	if (r)
1360 		ipxrtr_hold(r);
1361 	read_unlock_bh(&ipx_routes_lock);
1362 
1363 	return r;
1364 }
1365 
1366 /* caller must hold a reference to intrfc */
1367 
ipxrtr_add_route(__u32 network,ipx_interface * intrfc,unsigned char * node)1368 static int ipxrtr_add_route(__u32 network, ipx_interface *intrfc,
1369 				unsigned char *node)
1370 {
1371 	ipx_route *rt;
1372 	int ret;
1373 
1374 	/* Get a route structure; either existing or create */
1375 	rt = ipxrtr_lookup(network);
1376 	if (!rt) {
1377 		rt = kmalloc(sizeof(ipx_route), GFP_ATOMIC);
1378 		ret = -EAGAIN;
1379 		if (!rt)
1380 			goto out;
1381 
1382 		atomic_set(&rt->refcnt, 1);
1383 		ipxrtr_hold(rt);
1384 		write_lock_bh(&ipx_routes_lock);
1385 		rt->ir_next	= ipx_routes;
1386 		ipx_routes	= rt;
1387 		write_unlock_bh(&ipx_routes_lock);
1388 	} else {
1389 		ret = -EEXIST;
1390 		if (intrfc == ipx_internal_net)
1391 			goto out_put;
1392 	}
1393 
1394 	rt->ir_net 	= network;
1395 	rt->ir_intrfc 	= intrfc;
1396 	if (!node) {
1397 		memset(rt->ir_router_node, '\0', IPX_NODE_LEN);
1398 		rt->ir_routed = 0;
1399 	} else {
1400 		memcpy(rt->ir_router_node, node, IPX_NODE_LEN);
1401 		rt->ir_routed = 1;
1402 	}
1403 
1404 	ret = 0;
1405 out_put:
1406 	ipxrtr_put(rt);
1407 out:	return ret;
1408 }
1409 
ipxrtr_del_routes(ipx_interface * intrfc)1410 static void ipxrtr_del_routes(ipx_interface *intrfc)
1411 {
1412 	ipx_route **r, *tmp;
1413 
1414 	write_lock_bh(&ipx_routes_lock);
1415 	for (r = &ipx_routes; (tmp = *r) != NULL;) {
1416 		if (tmp->ir_intrfc == intrfc) {
1417 			*r = tmp->ir_next;
1418 			ipxrtr_put(tmp);
1419 		} else
1420 			r = &(tmp->ir_next);
1421 	}
1422 	write_unlock_bh(&ipx_routes_lock);
1423 }
1424 
ipxrtr_create(ipx_route_definition * rd)1425 static int ipxrtr_create(ipx_route_definition *rd)
1426 {
1427 	ipx_interface *intrfc;
1428 	int ret = -ENETUNREACH;
1429 
1430 	/* Find the appropriate interface */
1431 	intrfc = ipxitf_find_using_net(rd->ipx_router_network);
1432 	if (!intrfc)
1433 		goto out;
1434 	ret = ipxrtr_add_route(rd->ipx_network, intrfc, rd->ipx_router_node);
1435 	ipxitf_put(intrfc);
1436 out:	return ret;
1437 }
1438 
ipxrtr_delete(long net)1439 static int ipxrtr_delete(long net)
1440 {
1441 	ipx_route **r;
1442 	ipx_route *tmp;
1443 	int err;
1444 
1445 	write_lock_bh(&ipx_routes_lock);
1446 	for (r = &ipx_routes; (tmp = *r) != NULL;) {
1447 		if (tmp->ir_net == net) {
1448 			/* Directly connected; can't lose route */
1449 			err = -EPERM;
1450 			if (!tmp->ir_routed)
1451 				goto out;
1452 
1453 			*r = tmp->ir_next;
1454 			ipxrtr_put(tmp);
1455 			err = 0;
1456 			goto out;
1457 		}
1458 
1459 		r = &(tmp->ir_next);
1460 	}
1461 	err = -ENOENT;
1462 out:	write_unlock_bh(&ipx_routes_lock);
1463 	return err;
1464 }
1465 
1466 /*
1467  *	Checksum routine for IPX
1468  */
1469 
1470 /* Note: We assume ipx_tctrl==0 and htons(length)==ipx_pktsize */
1471 /* This functions should *not* mess with packet contents */
1472 
ipx_cksum(struct ipxhdr * packet,int length)1473 static __u16 ipx_cksum(struct ipxhdr *packet, int length)
1474 {
1475 	/*
1476 	 *	NOTE: sum is a net byte order quantity, which optimizes the
1477 	 *	loop. This only works on big and little endian machines. (I
1478 	 *	don't know of a machine that isn't.)
1479 	 */
1480 	/* start at ipx_dest - We skip the checksum field and start with
1481 	 * ipx_type before the loop, not considering ipx_tctrl in the calc */
1482 	__u16 *p = (__u16 *)&packet->ipx_dest;
1483 	__u32 i = (length >> 1) - 1; /* Number of complete words */
1484 	__u32 sum = packet->ipx_type << sizeof(packet->ipx_tctrl);
1485 
1486 	/* Loop through all complete words except the checksum field,
1487 	 * ipx_type (accounted above) and ipx_tctrl (not used in the cksum) */
1488 	while (--i)
1489 		sum += *p++;
1490 
1491 	/* Add on the last part word if it exists */
1492 	if (packet->ipx_pktsize & __constant_htons(1))
1493 		sum += ntohs(0xff00) & *p;
1494 
1495 	/* Do final fixup */
1496 	sum = (sum & 0xffff) + (sum >> 16);
1497 
1498 	/* It's a pity there's no concept of carry in C */
1499 	if (sum >= 0x10000)
1500 		sum++;
1501 
1502 	return ~sum;
1503 }
1504 
1505 /*
1506  * Route an outgoing frame from a socket.
1507  */
ipxrtr_route_packet(struct sock * sk,struct sockaddr_ipx * usipx,struct iovec * iov,int len,int noblock)1508 static int ipxrtr_route_packet(struct sock *sk, struct sockaddr_ipx *usipx,
1509 				struct iovec *iov, int len, int noblock)
1510 {
1511 	struct sk_buff *skb;
1512 	ipx_interface *intrfc;
1513 	struct ipxhdr *ipx;
1514 	int size;
1515 	int ipx_offset;
1516 	ipx_route *rt = NULL;
1517 	int err;
1518 
1519 	/* Find the appropriate interface on which to send packet */
1520 	if (!usipx->sipx_network && ipx_primary_net) {
1521 		usipx->sipx_network = ipx_primary_net->if_netnum;
1522 		intrfc = ipx_primary_net;
1523 	} else {
1524 		rt = ipxrtr_lookup(usipx->sipx_network);
1525 		err = -ENETUNREACH;
1526 		if (!rt)
1527 			goto out;
1528 		intrfc = rt->ir_intrfc;
1529 	}
1530 
1531 	ipxitf_hold(intrfc);
1532 	ipx_offset = intrfc->if_ipx_offset;
1533 	size = sizeof(struct ipxhdr) + len + ipx_offset;
1534 
1535 	skb = sock_alloc_send_skb(sk, size, noblock, &err);
1536 	if (!skb)
1537 		goto out_put;
1538 
1539 	skb_reserve(skb, ipx_offset);
1540 	skb->sk = sk;
1541 
1542 	/* Fill in IPX header */
1543 	ipx = (struct ipxhdr *)skb_put(skb, sizeof(struct ipxhdr));
1544 	ipx->ipx_pktsize = htons(len + sizeof(struct ipxhdr));
1545 	IPX_SKB_CB(skb)->ipx_tctrl = 0;
1546 	ipx->ipx_type 	 = usipx->sipx_type;
1547 	skb->nh.ipxh	 = ipx;
1548 	skb->h.raw 	 = (void *)skb->nh.ipxh;
1549 
1550 	IPX_SKB_CB(skb)->last_hop.index = -1;
1551 #ifdef CONFIG_IPX_INTERN
1552 	IPX_SKB_CB(skb)->ipx_source_net = sk->protinfo.af_ipx.intrfc->if_netnum;
1553 	memcpy(ipx->ipx_source.node, sk->protinfo.af_ipx.node, IPX_NODE_LEN);
1554 #else
1555 	err = ntohs(sk->protinfo.af_ipx.port);
1556 	if (err == 0x453 || err == 0x452) {
1557 		/* RIP/SAP special handling for mars_nwe */
1558 		IPX_SKB_CB(skb)->ipx_source_net = intrfc->if_netnum;
1559 		memcpy(ipx->ipx_source.node, intrfc->if_node, IPX_NODE_LEN);
1560 	} else {
1561 		IPX_SKB_CB(skb)->ipx_source_net =
1562 					sk->protinfo.af_ipx.intrfc->if_netnum;
1563 		memcpy(ipx->ipx_source.node, sk->protinfo.af_ipx.intrfc->if_node, IPX_NODE_LEN);
1564 	}
1565 #endif	/* CONFIG_IPX_INTERN */
1566 	ipx->ipx_source.sock		= sk->protinfo.af_ipx.port;
1567 	IPX_SKB_CB(skb)->ipx_dest_net	= usipx->sipx_network;
1568 	memcpy(ipx->ipx_dest.node, usipx->sipx_node, IPX_NODE_LEN);
1569 	ipx->ipx_dest.sock		= usipx->sipx_port;
1570 
1571 	err = memcpy_fromiovec(skb_put(skb, len), iov, len);
1572 	if (err) {
1573 		kfree_skb(skb);
1574 		goto out_put;
1575 	}
1576 
1577 	/* Apply checksum. Not allowed on 802.3 links. */
1578 	if (sk->no_check || intrfc->if_dlink_type == IPX_FRAME_8023)
1579 		ipx->ipx_checksum = 0xFFFF;
1580 	else
1581 		ipx->ipx_checksum = ipx_cksum(ipx, len + sizeof(struct ipxhdr));
1582 
1583 	err = ipxitf_send(intrfc, skb, (rt && rt->ir_routed) ?
1584 				rt->ir_router_node : ipx->ipx_dest.node);
1585 out_put:
1586 	ipxitf_put(intrfc);
1587 	if (rt)
1588 		ipxrtr_put(rt);
1589 out:	return err;
1590 }
1591 
1592 /* the skb has to be unshared, we'll end up calling ipxitf_send, that'll
1593  * modify the packet */
ipxrtr_route_skb(struct sk_buff * skb)1594 int ipxrtr_route_skb(struct sk_buff *skb)
1595 {
1596 	struct ipxhdr *ipx = skb->nh.ipxh;
1597 	ipx_route *r = ipxrtr_lookup(IPX_SKB_CB(skb)->ipx_dest_net);
1598 
1599 	if (!r) {	/* no known route */
1600 		kfree_skb(skb);
1601 		return 0;
1602 	}
1603 
1604 	ipxitf_hold(r->ir_intrfc);
1605 	ipxitf_send(r->ir_intrfc, skb, r->ir_routed ?
1606 			r->ir_router_node : ipx->ipx_dest.node);
1607 	ipxitf_put(r->ir_intrfc);
1608 	ipxrtr_put(r);
1609 
1610 	return 0;
1611 }
1612 
1613 /*
1614  * We use a normal struct rtentry for route handling
1615  */
ipxrtr_ioctl(unsigned int cmd,void * arg)1616 static int ipxrtr_ioctl(unsigned int cmd, void *arg)
1617 {
1618 	struct rtentry rt;	/* Use these to behave like 'other' stacks */
1619 	struct sockaddr_ipx *sg, *st;
1620 	int ret = -EFAULT;
1621 
1622 	if (copy_from_user(&rt, arg, sizeof(rt)))
1623 		goto out;
1624 
1625 	sg = (struct sockaddr_ipx *)&rt.rt_gateway;
1626 	st = (struct sockaddr_ipx *)&rt.rt_dst;
1627 
1628 	ret = -EINVAL;
1629 	if (!(rt.rt_flags & RTF_GATEWAY) || /* Direct routes are fixed */
1630 	    sg->sipx_family != AF_IPX ||
1631 	    st->sipx_family != AF_IPX)
1632 		goto out;
1633 
1634 	switch (cmd) {
1635 		case SIOCDELRT:
1636 			ret = ipxrtr_delete(st->sipx_network);
1637 			break;
1638 		case SIOCADDRT: {
1639 			struct ipx_route_definition f;
1640 			f.ipx_network		= st->sipx_network;
1641 			f.ipx_router_network	= sg->sipx_network;
1642 			memcpy(f.ipx_router_node, sg->sipx_node, IPX_NODE_LEN);
1643 			ret = ipxrtr_create(&f);
1644 			break;
1645 		}
1646 	}
1647 
1648 out:	return ret;
1649 }
1650 
ipx_frame_name(unsigned short frame)1651 static const char *ipx_frame_name(unsigned short frame)
1652 {
1653 	char* ret = "None";
1654 
1655 	switch (ntohs(frame)) {
1656 		case ETH_P_IPX:		ret = "EtherII";	break;
1657 		case ETH_P_802_2:	ret = "802.2";		break;
1658 		case ETH_P_SNAP:	ret = "SNAP";		break;
1659 		case ETH_P_802_3:	ret = "802.3";		break;
1660 		case ETH_P_TR_802_2:	ret = "802.2TR";	break;
1661 	}
1662 
1663 	return ret;
1664 }
1665 
ipx_device_name(ipx_interface * intrfc)1666 static const char *ipx_device_name(ipx_interface *intrfc)
1667 {
1668 	return intrfc->if_internal ? "Internal" :
1669 		intrfc->if_dev ? intrfc->if_dev->name : "Unknown";
1670 }
1671 
1672 /* Called from proc fs */
ipx_interface_get_info(char * buffer,char ** start,off_t offset,int length)1673 static int ipx_interface_get_info(char *buffer, char **start, off_t offset,
1674 				  int length)
1675 {
1676 	ipx_interface *i;
1677 	off_t begin = 0, pos = 0;
1678 	int len = 0;
1679 
1680 	/* Theory.. Keep printing in the same place until we pass offset */
1681 
1682 	len += sprintf(buffer, "%-11s%-15s%-9s%-11s%s", "Network",
1683 		"Node_Address", "Primary", "Device", "Frame_Type");
1684 #ifdef IPX_REFCNT_DEBUG
1685 	len += sprintf(buffer + len, "  refcnt");
1686 #endif
1687 	strcat(buffer + len++, "\n");
1688 	spin_lock_bh(&ipx_interfaces_lock);
1689 	for (i = ipx_interfaces; i; i = i->if_next) {
1690 		len += sprintf(buffer + len, "%08lX   ",
1691 				(long unsigned int) ntohl(i->if_netnum));
1692 		len += sprintf(buffer + len, "%02X%02X%02X%02X%02X%02X   ",
1693 				i->if_node[0], i->if_node[1], i->if_node[2],
1694 				i->if_node[3], i->if_node[4], i->if_node[5]);
1695 		len += sprintf(buffer + len, "%-9s", i == ipx_primary_net ?
1696 			"Yes" : "No");
1697 		len += sprintf(buffer + len, "%-11s", ipx_device_name(i));
1698 		len += sprintf(buffer + len, "%-9s",
1699 			ipx_frame_name(i->if_dlink_type));
1700 #ifdef IPX_REFCNT_DEBUG
1701 		len += sprintf(buffer + len, "%6d", atomic_read(&i->refcnt));
1702 #endif
1703 		strcat(buffer + len++, "\n");
1704 		/* Are we still dumping unwanted data then discard the record */
1705 		pos = begin + len;
1706 
1707 		if (pos < offset) {
1708 			len   = 0;	/* Keep dumping into the buffer start */
1709 			begin = pos;
1710 		}
1711 		if (pos > offset + length)	/* We have dumped enough */
1712 			break;
1713 	}
1714 	spin_unlock_bh(&ipx_interfaces_lock);
1715 
1716 	/* The data in question runs from begin to begin+len */
1717 	*start = buffer + (offset - begin);	/* Start of wanted data */
1718 	len -= (offset - begin); /* Remove unwanted header data from length */
1719 	if (len > length)
1720 		len = length;	/* Remove unwanted tail data from length */
1721 
1722 	return len;
1723 }
1724 
ipx_get_info(char * buffer,char ** start,off_t offset,int length)1725 static int ipx_get_info(char *buffer, char **start, off_t offset, int length)
1726 {
1727 	struct sock *s;
1728 	ipx_interface *i;
1729 	off_t begin = 0, pos = 0;
1730 	int len = 0;
1731 
1732 	/* Theory.. Keep printing in the same place until we pass offset */
1733 
1734 #ifdef CONFIG_IPX_INTERN
1735 	len += sprintf(buffer, "%-28s%-28s%-10s%-10s%-7s%s\n", "Local_Address",
1736 #else
1737 	len += sprintf(buffer, "%-15s%-28s%-10s%-10s%-7s%s\n", "Local_Address",
1738 #endif	/* CONFIG_IPX_INTERN */
1739 			"Remote_Address", "Tx_Queue", "Rx_Queue",
1740 			"State", "Uid");
1741 
1742 	spin_lock_bh(&ipx_interfaces_lock);
1743 	for (i = ipx_interfaces; i; i = i->if_next) {
1744 		ipxitf_hold(i);
1745 		spin_lock_bh(&i->if_sklist_lock);
1746 		for (s = i->if_sklist; s; s = s->next) {
1747 #ifdef CONFIG_IPX_INTERN
1748 			len += sprintf(buffer + len,
1749 				       "%08lX:%02X%02X%02X%02X%02X%02X:%04X  ",
1750                                        (unsigned long) htonl(s->protinfo.af_ipx.intrfc->if_netnum),
1751 				       s->protinfo.af_ipx.node[0],
1752 				       s->protinfo.af_ipx.node[1],
1753 				       s->protinfo.af_ipx.node[2],
1754 				       s->protinfo.af_ipx.node[3],
1755 				       s->protinfo.af_ipx.node[4],
1756 				       s->protinfo.af_ipx.node[5],
1757 				       htons(s->protinfo.af_ipx.port));
1758 #else
1759 			len += sprintf(buffer + len, "%08lX:%04X  ",
1760 				       (unsigned long) htonl(i->if_netnum),
1761 				       htons(s->protinfo.af_ipx.port));
1762 #endif	/* CONFIG_IPX_INTERN */
1763 			if (s->state != TCP_ESTABLISHED)
1764 				len += sprintf(buffer + len, "%-28s",
1765 						"Not_Connected");
1766 			else {
1767 				len += sprintf(buffer + len,
1768 					"%08lX:%02X%02X%02X%02X%02X%02X:%04X  ",
1769 					(unsigned long) htonl(s->protinfo.af_ipx.dest_addr.net),
1770 					s->protinfo.af_ipx.dest_addr.node[0],
1771 					s->protinfo.af_ipx.dest_addr.node[1],
1772 					s->protinfo.af_ipx.dest_addr.node[2],
1773 					s->protinfo.af_ipx.dest_addr.node[3],
1774 					s->protinfo.af_ipx.dest_addr.node[4],
1775 					s->protinfo.af_ipx.dest_addr.node[5],
1776 					htons(s->protinfo.af_ipx.dest_addr.sock));
1777 			}
1778 
1779 			len += sprintf(buffer + len, "%08X  %08X  ",
1780 				atomic_read(&s->wmem_alloc),
1781 				atomic_read(&s->rmem_alloc));
1782 			len += sprintf(buffer + len, "%02X     %03d\n",
1783 				s->state, SOCK_INODE(s->socket)->i_uid);
1784 
1785 			pos = begin + len;
1786 			if (pos < offset) {
1787 				len   = 0;
1788 				begin = pos;
1789 			}
1790 
1791 			if (pos > offset + length)  /* We have dumped enough */
1792 				break;
1793 		}
1794 		spin_unlock_bh(&i->if_sklist_lock);
1795 		ipxitf_put(i);
1796 	}
1797 	spin_unlock_bh(&ipx_interfaces_lock);
1798 
1799 	/* The data in question runs from begin to begin+len */
1800 	*start = buffer + offset - begin;
1801 	len -= (offset - begin);
1802 	if (len > length)
1803 		len = length;
1804 
1805 	return len;
1806 }
1807 
ipx_rt_get_info(char * buffer,char ** start,off_t offset,int length)1808 static int ipx_rt_get_info(char *buffer, char **start, off_t offset, int length)
1809 {
1810 	ipx_route *rt;
1811 	off_t begin = 0, pos = 0;
1812 	int len = 0;
1813 
1814 	len += sprintf(buffer, "%-11s%-13s%s\n",
1815 			"Network", "Router_Net", "Router_Node");
1816 	read_lock_bh(&ipx_routes_lock);
1817 	for (rt = ipx_routes; rt; rt = rt->ir_next) {
1818 		len += sprintf(buffer + len, "%08lX   ",
1819 				(long unsigned int) ntohl(rt->ir_net));
1820 		if (rt->ir_routed) {
1821 			len += sprintf(buffer + len,
1822 					"%08lX     %02X%02X%02X%02X%02X%02X\n",
1823 			(long unsigned int) ntohl(rt->ir_intrfc->if_netnum),
1824 				rt->ir_router_node[0], rt->ir_router_node[1],
1825 				rt->ir_router_node[2], rt->ir_router_node[3],
1826 				rt->ir_router_node[4], rt->ir_router_node[5]);
1827 		} else {
1828 			len += sprintf(buffer + len, "%-13s%s\n",
1829 					"Directly", "Connected");
1830 		}
1831 
1832 		pos = begin + len;
1833 		if (pos < offset) {
1834 			len = 0;
1835 			begin = pos;
1836 		}
1837 
1838 		if (pos > offset + length)
1839 			break;
1840 	}
1841 	read_unlock_bh(&ipx_routes_lock);
1842 
1843 	*start = buffer + (offset - begin);
1844 	len -= (offset - begin);
1845 	if (len > length)
1846 		len = length;
1847 
1848 	return len;
1849 }
1850 
1851 /* Handling for system calls applied via the various interfaces to an IPX
1852  * socket object. */
1853 
ipx_setsockopt(struct socket * sock,int level,int optname,char * optval,int optlen)1854 static int ipx_setsockopt(struct socket *sock, int level, int optname,
1855 			  char *optval, int optlen)
1856 {
1857 	struct sock *sk = sock->sk;
1858 	int opt;
1859 	int ret = -EINVAL;
1860 
1861 	if (optlen != sizeof(int))
1862 		goto out;
1863 
1864 	ret = -EFAULT;
1865 	if (get_user(opt, (unsigned int *)optval))
1866 		goto out;
1867 
1868 	ret = -ENOPROTOOPT;
1869 	if (!(level == SOL_IPX && optname == IPX_TYPE))
1870 		goto out;
1871 
1872 	sk->protinfo.af_ipx.type = opt;
1873 	ret = 0;
1874 out:	return ret;
1875 }
1876 
ipx_getsockopt(struct socket * sock,int level,int optname,char * optval,int * optlen)1877 static int ipx_getsockopt(struct socket *sock, int level, int optname,
1878 	char *optval, int *optlen)
1879 {
1880 	struct sock *sk = sock->sk;
1881 	int val = 0;
1882 	int len;
1883 	int ret = -ENOPROTOOPT;
1884 
1885 	if (!(level == SOL_IPX && optname == IPX_TYPE))
1886 		goto out;
1887 
1888 	val = sk->protinfo.af_ipx.type;
1889 
1890 	ret = -EFAULT;
1891 	if (get_user(len, optlen))
1892 		goto out;
1893 
1894 	len = min_t(unsigned int, len, sizeof(int));
1895 	ret = -EINVAL;
1896 	if(len < 0)
1897 		goto out;
1898 
1899 	ret = -EFAULT;
1900 	if (put_user(len, optlen) || copy_to_user(optval, &val, len))
1901 		goto out;
1902 
1903 	ret = 0;
1904 out:	return ret;
1905 }
1906 
ipx_create(struct socket * sock,int protocol)1907 static int ipx_create(struct socket *sock, int protocol)
1908 {
1909 	int ret = -ESOCKTNOSUPPORT;
1910 	struct sock *sk;
1911 
1912 	switch (sock->type) {
1913 		case SOCK_DGRAM:
1914 			sk = sk_alloc(PF_IPX, GFP_KERNEL, 1);
1915                 	ret = -ENOMEM;
1916 			if (!sk)
1917 				goto out;
1918                         sock->ops = &ipx_dgram_ops;
1919                         break;
1920 
1921 		case SOCK_SEQPACKET:
1922 			/*
1923 			 * From this point on SPX sockets are handled
1924 			 * by af_spx.c and the methods replaced.
1925 			 */
1926 			if (spx_family_ops) {
1927 				ret = spx_family_ops->create(sock, protocol);
1928 				goto out;
1929 			}
1930 			/* Fall through if SPX is not loaded */
1931 		case SOCK_STREAM:       /* Allow higher levels to piggyback */
1932 		default:
1933 			goto out;
1934 	}
1935 #ifdef IPX_REFCNT_DEBUG
1936         atomic_inc(&ipx_sock_nr);
1937         printk(KERN_DEBUG "IPX socket %p created, now we have %d alive\n", sk,
1938 			atomic_read(&ipx_sock_nr));
1939 #endif
1940 	sock_init_data(sock, sk);
1941 	sk->destruct	= NULL;
1942 	sk->no_check 	= 1;		/* Checksum off by default */
1943 
1944 	MOD_INC_USE_COUNT;
1945 	ret = 0;
1946 out:	return ret;
1947 }
1948 
ipx_release(struct socket * sock)1949 static int ipx_release(struct socket *sock)
1950 {
1951 	struct sock *sk = sock->sk;
1952 
1953 	if (!sk)
1954 		goto out;
1955 
1956 	if (!sk->dead)
1957 		sk->state_change(sk);
1958 
1959 	sk->dead = 1;
1960 	sock->sk = NULL;
1961 	ipx_destroy_socket(sk);
1962 
1963 	if (sock->type == SOCK_DGRAM)
1964 		MOD_DEC_USE_COUNT;
1965 
1966 out:	return 0;
1967 }
1968 
1969 /* caller must hold a reference to intrfc */
1970 
ipx_first_free_socketnum(ipx_interface * intrfc)1971 static unsigned short ipx_first_free_socketnum(ipx_interface *intrfc)
1972 {
1973 	unsigned short socketNum = intrfc->if_sknum;
1974 
1975 	spin_lock_bh(&intrfc->if_sklist_lock);
1976 
1977 	if (socketNum < IPX_MIN_EPHEMERAL_SOCKET)
1978 		socketNum = IPX_MIN_EPHEMERAL_SOCKET;
1979 
1980 	while (__ipxitf_find_socket(intrfc, ntohs(socketNum)))
1981 		if (socketNum > IPX_MAX_EPHEMERAL_SOCKET)
1982 			socketNum = IPX_MIN_EPHEMERAL_SOCKET;
1983 		else
1984 			socketNum++;
1985 
1986 	spin_unlock_bh(&intrfc->if_sklist_lock);
1987 	intrfc->if_sknum = socketNum;
1988 
1989 	return ntohs(socketNum);
1990 }
1991 
ipx_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)1992 static int ipx_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1993 {
1994 	struct sock *sk = sock->sk;
1995 	ipx_interface *intrfc;
1996 	struct sockaddr_ipx *addr = (struct sockaddr_ipx *)uaddr;
1997 	int ret = -EINVAL;
1998 
1999 	if (!sk->zapped || addr_len != sizeof(struct sockaddr_ipx))
2000 		goto out;
2001 
2002 	intrfc = ipxitf_find_using_net(addr->sipx_network);
2003 	ret = -EADDRNOTAVAIL;
2004 	if (!intrfc)
2005 		goto out;
2006 
2007 	if (!addr->sipx_port) {
2008 		addr->sipx_port = ipx_first_free_socketnum(intrfc);
2009 		ret = -EINVAL;
2010 		if (!addr->sipx_port)
2011 			goto out_put;
2012 	}
2013 
2014 	/* protect IPX system stuff like routing/sap */
2015 	ret = -EACCES;
2016 	if (ntohs(addr->sipx_port) < IPX_MIN_EPHEMERAL_SOCKET &&
2017 	    !capable(CAP_NET_ADMIN))
2018 		goto out_put;
2019 
2020 	sk->protinfo.af_ipx.port = addr->sipx_port;
2021 
2022 #ifdef CONFIG_IPX_INTERN
2023 	if (intrfc == ipx_internal_net) {
2024 		/* The source address is to be set explicitly if the
2025 		 * socket is to be bound on the internal network. If a
2026 		 * node number 0 was specified, the default is used.
2027 		 */
2028 
2029 		ret = -EINVAL;
2030 		if (!memcmp(addr->sipx_node, ipx_broadcast_node, IPX_NODE_LEN))
2031 			goto out_put;
2032 		if (!memcmp(addr->sipx_node, ipx_this_node, IPX_NODE_LEN))
2033 			memcpy(sk->protinfo.af_ipx.node, intrfc->if_node,
2034 			       IPX_NODE_LEN);
2035 		else
2036 			memcpy(sk->protinfo.af_ipx.node, addr->sipx_node,
2037 				IPX_NODE_LEN);
2038 
2039 		ret = -EADDRINUSE;
2040 		if (ipxitf_find_internal_socket(intrfc,
2041 						sk->protinfo.af_ipx.node,
2042 						sk->protinfo.af_ipx.port)) {
2043 			SOCK_DEBUG(sk,
2044 				"IPX: bind failed because port %X in use.\n",
2045 				ntohs((int)addr->sipx_port));
2046 			goto out_put;
2047 		}
2048 	} else {
2049 		/* Source addresses are easy. It must be our
2050 		 * network:node pair for an interface routed to IPX
2051 		 * with the ipx routing ioctl()
2052 		 */
2053 
2054 		memcpy(sk->protinfo.af_ipx.node, intrfc->if_node,
2055 			IPX_NODE_LEN);
2056 
2057 		ret = -EADDRINUSE;
2058 		if (ipxitf_find_socket(intrfc, addr->sipx_port)) {
2059 			SOCK_DEBUG(sk,
2060 				"IPX: bind failed because port %X in use.\n",
2061 				ntohs((int)addr->sipx_port));
2062 			goto out_put;
2063 		}
2064 	}
2065 
2066 #else	/* !def CONFIG_IPX_INTERN */
2067 
2068 	/* Source addresses are easy. It must be our network:node pair for
2069 	   an interface routed to IPX with the ipx routing ioctl() */
2070 
2071 	ret = -EADDRINUSE;
2072 	if (ipxitf_find_socket(intrfc, addr->sipx_port)) {
2073 		SOCK_DEBUG(sk, "IPX: bind failed because port %X in use.\n",
2074 				ntohs((int)addr->sipx_port));
2075 		goto out_put;
2076 	}
2077 
2078 #endif	/* CONFIG_IPX_INTERN */
2079 
2080 	ipxitf_insert_socket(intrfc, sk);
2081 	sk->zapped = 0;
2082 	SOCK_DEBUG(sk, "IPX: bound socket 0x%04X.\n", ntohs(addr->sipx_port) );
2083 
2084 	ret = 0;
2085 out_put:
2086 	ipxitf_put(intrfc);
2087 out:	return ret;
2088 }
2089 
ipx_connect(struct socket * sock,struct sockaddr * uaddr,int addr_len,int flags)2090 static int ipx_connect(struct socket *sock, struct sockaddr *uaddr,
2091 	int addr_len, int flags)
2092 {
2093 	struct sock *sk = sock->sk;
2094 	struct sockaddr_ipx *addr;
2095 	int ret = -EINVAL;
2096 	ipx_route *rt;
2097 
2098 	sk->state	= TCP_CLOSE;
2099 	sock->state 	= SS_UNCONNECTED;
2100 
2101 	if (addr_len != sizeof(*addr))
2102 		goto out;
2103 	addr = (struct sockaddr_ipx *)uaddr;
2104 
2105 	/* put the autobinding in */
2106 	if (!sk->protinfo.af_ipx.port) {
2107 		struct sockaddr_ipx uaddr;
2108 
2109 		uaddr.sipx_port		= 0;
2110 		uaddr.sipx_network 	= 0;
2111 
2112 #ifdef CONFIG_IPX_INTERN
2113 		ret = -ENETDOWN;
2114 		if (!sk->protinfo.af_ipx.intrfc)
2115 			goto out; /* Someone zonked the iface */
2116 		memcpy(uaddr.sipx_node, sk->protinfo.af_ipx.intrfc->if_node,
2117 			IPX_NODE_LEN);
2118 #endif	/* CONFIG_IPX_INTERN */
2119 
2120 		ret = ipx_bind(sock, (struct sockaddr *)&uaddr,
2121 				sizeof(struct sockaddr_ipx));
2122 		if (ret)
2123 			goto out;
2124 	}
2125 
2126         /* We can either connect to primary network or somewhere
2127 	 * we can route to */
2128 	rt = ipxrtr_lookup(addr->sipx_network);
2129 	ret = -ENETUNREACH;
2130 	if (!rt && !(!addr->sipx_network && ipx_primary_net))
2131 		goto out;
2132 
2133 	sk->protinfo.af_ipx.dest_addr.net  = addr->sipx_network;
2134 	sk->protinfo.af_ipx.dest_addr.sock = addr->sipx_port;
2135 	memcpy(sk->protinfo.af_ipx.dest_addr.node,
2136 		addr->sipx_node, IPX_NODE_LEN);
2137 	sk->protinfo.af_ipx.type = addr->sipx_type;
2138 
2139 	if (sock->type == SOCK_DGRAM) {
2140 		sock->state 	= SS_CONNECTED;
2141 		sk->state 	= TCP_ESTABLISHED;
2142 	}
2143 
2144 	if (rt)
2145 		ipxrtr_put(rt);
2146 	ret = 0;
2147 out:	return ret;
2148 }
2149 
2150 
ipx_getname(struct socket * sock,struct sockaddr * uaddr,int * uaddr_len,int peer)2151 static int ipx_getname(struct socket *sock, struct sockaddr *uaddr,
2152 			int *uaddr_len, int peer)
2153 {
2154 	ipx_address *addr;
2155 	struct sockaddr_ipx sipx;
2156 	struct sock *sk = sock->sk;
2157 	int ret;
2158 
2159 	*uaddr_len = sizeof(struct sockaddr_ipx);
2160 
2161 	if (peer) {
2162 		ret = -ENOTCONN;
2163 		if (sk->state != TCP_ESTABLISHED)
2164 			goto out;
2165 
2166 		addr = &sk->protinfo.af_ipx.dest_addr;
2167 		sipx.sipx_network	= addr->net;
2168 		sipx.sipx_port		= addr->sock;
2169 		memcpy(sipx.sipx_node, addr->node, IPX_NODE_LEN);
2170 	} else {
2171 		if (sk->protinfo.af_ipx.intrfc) {
2172 			sipx.sipx_network =
2173 				sk->protinfo.af_ipx.intrfc->if_netnum;
2174 #ifdef CONFIG_IPX_INTERN
2175 			memcpy(sipx.sipx_node, sk->protinfo.af_ipx.node,
2176 				IPX_NODE_LEN);
2177 #else
2178 			memcpy(sipx.sipx_node,
2179 				sk->protinfo.af_ipx.intrfc->if_node,
2180 				IPX_NODE_LEN);
2181 #endif	/* CONFIG_IPX_INTERN */
2182 
2183 		} else {
2184 			sipx.sipx_network = 0;
2185 			memset(sipx.sipx_node, '\0', IPX_NODE_LEN);
2186 		}
2187 
2188 		sipx.sipx_port = sk->protinfo.af_ipx.port;
2189 	}
2190 
2191 	sipx.sipx_family = AF_IPX;
2192 	sipx.sipx_type	 = sk->protinfo.af_ipx.type;
2193 	memcpy(uaddr, &sipx, sizeof(sipx));
2194 
2195 	ret = 0;
2196 out:	return ret;
2197 }
2198 
ipx_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt)2199 int ipx_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
2200 {
2201 	/* NULL here for pt means the packet was looped back */
2202 	ipx_interface *intrfc;
2203 	struct ipxhdr *ipx;
2204 	u16 ipx_pktsize;
2205 	int ret = 0;
2206 
2207 	/* Not ours */
2208         if (skb->pkt_type == PACKET_OTHERHOST)
2209         	goto drop;
2210 
2211 	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
2212 		goto out;
2213 
2214 	ipx		= skb->nh.ipxh;
2215 	ipx_pktsize	= ntohs(ipx->ipx_pktsize);
2216 
2217 	/* Too small or invalid header? */
2218 	if (ipx_pktsize < sizeof(struct ipxhdr) || ipx_pktsize > skb->len)
2219 		goto drop;
2220 
2221 	if (ipx->ipx_checksum != IPX_NO_CHECKSUM &&
2222 	   ipx->ipx_checksum != ipx_cksum(ipx, ipx_pktsize))
2223 		goto drop;
2224 
2225 	IPX_SKB_CB(skb)->ipx_tctrl	= ipx->ipx_tctrl;
2226 	IPX_SKB_CB(skb)->ipx_dest_net	= ipx->ipx_dest.net;
2227 	IPX_SKB_CB(skb)->ipx_source_net = ipx->ipx_source.net;
2228 
2229 	/* Determine what local ipx endpoint this is */
2230 	intrfc = ipxitf_find_using_phys(dev, pt->type);
2231 	if (!intrfc) {
2232 		if (ipxcfg_auto_create_interfaces &&
2233 		   ntohl(IPX_SKB_CB(skb)->ipx_dest_net)) {
2234 			intrfc = ipxitf_auto_create(dev, pt->type);
2235 			if (intrfc)
2236 				ipxitf_hold(intrfc);
2237 		}
2238 
2239 		if (!intrfc)	/* Not one of ours */
2240 				/* or invalid packet for auto creation */
2241 			goto drop;
2242 	}
2243 
2244 	ret = ipxitf_rcv(intrfc, skb);
2245 	ipxitf_put(intrfc);
2246 	goto out;
2247 drop:	kfree_skb(skb);
2248 out:	return ret;
2249 }
2250 
ipx_sendmsg(struct socket * sock,struct msghdr * msg,int len,struct scm_cookie * scm)2251 static int ipx_sendmsg(struct socket *sock, struct msghdr *msg, int len,
2252 	struct scm_cookie *scm)
2253 {
2254 	struct sock *sk = sock->sk;
2255 	struct sockaddr_ipx *usipx = (struct sockaddr_ipx *)msg->msg_name;
2256 	struct sockaddr_ipx local_sipx;
2257 	int ret = -EINVAL;
2258 	int flags = msg->msg_flags;
2259 
2260 	/* Socket gets bound below anyway */
2261 /*	if (sk->zapped)
2262 		return -EIO; */	/* Socket not bound */
2263 	if (flags & ~MSG_DONTWAIT)
2264 		goto out;
2265 
2266 	if (usipx) {
2267 		if (!sk->protinfo.af_ipx.port) {
2268 			struct sockaddr_ipx uaddr;
2269 
2270 			uaddr.sipx_port		= 0;
2271 			uaddr.sipx_network	= 0;
2272 #ifdef CONFIG_IPX_INTERN
2273 			ret = -ENETDOWN;
2274 			if (!sk->protinfo.af_ipx.intrfc)
2275 				goto out; /* Someone zonked the iface */
2276 			memcpy(uaddr.sipx_node,
2277 				sk->protinfo.af_ipx.intrfc->if_node,
2278 				IPX_NODE_LEN);
2279 #endif
2280 			ret = ipx_bind(sock, (struct sockaddr *)&uaddr,
2281 					sizeof(struct sockaddr_ipx));
2282 			if (ret)
2283 				goto out;
2284 		}
2285 
2286 		ret = -EINVAL;
2287 		if (msg->msg_namelen < sizeof(*usipx) ||
2288 		    usipx->sipx_family != AF_IPX)
2289 			goto out;
2290 	} else {
2291 		ret = -ENOTCONN;
2292 		if (sk->state != TCP_ESTABLISHED)
2293 			goto out;
2294 
2295 		usipx = &local_sipx;
2296 		usipx->sipx_family 	= AF_IPX;
2297 		usipx->sipx_type 	= sk->protinfo.af_ipx.type;
2298 		usipx->sipx_port 	= sk->protinfo.af_ipx.dest_addr.sock;
2299 		usipx->sipx_network 	= sk->protinfo.af_ipx.dest_addr.net;
2300 		memcpy(usipx->sipx_node, sk->protinfo.af_ipx.dest_addr.node,
2301 				IPX_NODE_LEN);
2302 	}
2303 
2304 	ret = ipxrtr_route_packet(sk, usipx, msg->msg_iov, len,
2305 				     flags & MSG_DONTWAIT);
2306 	if (ret >= 0)
2307 		ret = len;
2308 out:	return ret;
2309 }
2310 
2311 
ipx_recvmsg(struct socket * sock,struct msghdr * msg,int size,int flags,struct scm_cookie * scm)2312 static int ipx_recvmsg(struct socket *sock, struct msghdr *msg, int size,
2313 		int flags, struct scm_cookie *scm)
2314 {
2315 	struct sock *sk = sock->sk;
2316 	struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)msg->msg_name;
2317 	struct ipxhdr *ipx = NULL;
2318 	struct sk_buff *skb;
2319 	int copied, err;
2320 
2321 	/* put the autobinding in */
2322 	if (!sk->protinfo.af_ipx.port) {
2323 		struct sockaddr_ipx uaddr;
2324 
2325 		uaddr.sipx_port		= 0;
2326 		uaddr.sipx_network 	= 0;
2327 
2328 #ifdef CONFIG_IPX_INTERN
2329 		err = -ENETDOWN;
2330 		if (!sk->protinfo.af_ipx.intrfc)
2331 			goto out; /* Someone zonked the iface */
2332 		memcpy(uaddr.sipx_node,
2333 			sk->protinfo.af_ipx.intrfc->if_node, IPX_NODE_LEN);
2334 #endif	/* CONFIG_IPX_INTERN */
2335 
2336 		err = ipx_bind(sock, (struct sockaddr *)&uaddr,
2337 				sizeof(struct sockaddr_ipx));
2338 		if (err)
2339 			goto out;
2340 	}
2341 
2342 	err = -ENOTCONN;
2343 	if (sk->zapped)
2344 		goto out;
2345 
2346 	skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
2347 					flags & MSG_DONTWAIT, &err);
2348 	if (!skb)
2349 		goto out;
2350 
2351 	ipx 	= skb->nh.ipxh;
2352 	copied 	= ntohs(ipx->ipx_pktsize) - sizeof(struct ipxhdr);
2353 	if (copied > size) {
2354 		copied = size;
2355 		msg->msg_flags |= MSG_TRUNC;
2356 	}
2357 
2358 	err = skb_copy_datagram_iovec(skb, sizeof(struct ipxhdr), msg->msg_iov,
2359 					copied);
2360 	if (err)
2361 		goto out_free;
2362 	sk->stamp = skb->stamp;
2363 
2364 	msg->msg_namelen = sizeof(*sipx);
2365 
2366 	if (sipx) {
2367 		sipx->sipx_family	= AF_IPX;
2368 		sipx->sipx_port		= ipx->ipx_source.sock;
2369 		memcpy(sipx->sipx_node, ipx->ipx_source.node, IPX_NODE_LEN);
2370 		sipx->sipx_network	= IPX_SKB_CB(skb)->ipx_source_net;
2371 		sipx->sipx_type 	= ipx->ipx_type;
2372 	}
2373 	err = copied;
2374 
2375 out_free:
2376 	skb_free_datagram(sk, skb);
2377 out:	return err;
2378 }
2379 
2380 
ipx_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)2381 static int ipx_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
2382 {
2383 	long amount = 0;
2384 	struct sock *sk = sock->sk;
2385 
2386 	switch (cmd) {
2387 		case TIOCOUTQ:
2388 			amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
2389 			if (amount < 0)
2390 				amount = 0;
2391 			return put_user(amount, (int *)arg);
2392 
2393 		case TIOCINQ: {
2394 			struct sk_buff *skb = skb_peek(&sk->receive_queue);
2395 			/* These two are safe on a single CPU system as only
2396 			 * user tasks fiddle here */
2397 			if (skb)
2398 				amount = skb->len - sizeof(struct ipxhdr);
2399 			return put_user(amount, (int *)arg);
2400 		}
2401 
2402 		case SIOCADDRT:
2403 		case SIOCDELRT:
2404 			if (!capable(CAP_NET_ADMIN))
2405 				return -EPERM;
2406 			return ipxrtr_ioctl(cmd, (void *)arg);
2407 
2408 		case SIOCSIFADDR:
2409 		case SIOCAIPXITFCRT:
2410 		case SIOCAIPXPRISLT:
2411 			if (!capable(CAP_NET_ADMIN))
2412 				return -EPERM;
2413 
2414 		case SIOCGIFADDR:
2415 			return ipxitf_ioctl(cmd, (void *)arg);
2416 
2417 		case SIOCIPXCFGDATA:
2418 			return ipxcfg_get_config_data((void *)arg);
2419 
2420 		case SIOCIPXNCPCONN:
2421 			/*
2422 			 * This socket wants to take care of the NCP connection
2423 			 * handed to us in arg.
2424 			 */
2425                 	if (!capable(CAP_NET_ADMIN))
2426                 		return -EPERM;
2427 			return get_user(sk->protinfo.af_ipx.ipx_ncp_conn,
2428 					(const unsigned short *)(arg));
2429 
2430 		case SIOCGSTAMP: {
2431 			int ret = -EINVAL;
2432 			if (sk) {
2433 				if (!sk->stamp.tv_sec)
2434 					return -ENOENT;
2435 				ret = -EFAULT;
2436 				if (!copy_to_user((void *)arg, &sk->stamp,
2437 						sizeof(struct timeval)))
2438 					ret = 0;
2439 			}
2440 
2441 			return ret;
2442 		}
2443 
2444 		case SIOCGIFDSTADDR:
2445 		case SIOCSIFDSTADDR:
2446 		case SIOCGIFBRDADDR:
2447 		case SIOCSIFBRDADDR:
2448 		case SIOCGIFNETMASK:
2449 		case SIOCSIFNETMASK:
2450 			return -EINVAL;
2451 
2452 		default:
2453 			return dev_ioctl(cmd,(void *) arg);
2454 	}
2455 
2456 	/*NOT REACHED*/
2457 	return 0;
2458 }
2459 
2460 /*
2461  *      SPX interface support
2462  */
2463 
ipx_register_spx(struct proto_ops ** p,struct net_proto_family * spx)2464 int ipx_register_spx(struct proto_ops **p, struct net_proto_family *spx)
2465 {
2466         if (spx_family_ops)
2467                 return -EBUSY;
2468         cli();
2469         MOD_INC_USE_COUNT;
2470         *p = &ipx_dgram_ops;
2471         spx_family_ops = spx;
2472         sti();
2473         return 0;
2474 }
2475 
ipx_unregister_spx(void)2476 int ipx_unregister_spx(void)
2477 {
2478         spx_family_ops = NULL;
2479         MOD_DEC_USE_COUNT;
2480         return 0;
2481 }
2482 
2483 /*
2484  * Socket family declarations
2485  */
2486 
2487 static struct net_proto_family ipx_family_ops = {
2488 	family:		PF_IPX,
2489 	create:		ipx_create,
2490 };
2491 
2492 static struct proto_ops SOCKOPS_WRAPPED(ipx_dgram_ops) = {
2493 	family:		PF_IPX,
2494 
2495 	release:	ipx_release,
2496 	bind:		ipx_bind,
2497 	connect:	ipx_connect,
2498 	socketpair:	sock_no_socketpair,
2499 	accept:		sock_no_accept,
2500 	getname:	ipx_getname,
2501 	poll:		datagram_poll,
2502 	ioctl:		ipx_ioctl,
2503 	listen:		sock_no_listen,
2504 	shutdown:	sock_no_shutdown, /* FIXME: have to support shutdown */
2505 	setsockopt:	ipx_setsockopt,
2506 	getsockopt:	ipx_getsockopt,
2507 	sendmsg:	ipx_sendmsg,
2508 	recvmsg:	ipx_recvmsg,
2509 	mmap:		sock_no_mmap,
2510 	sendpage:	sock_no_sendpage,
2511 };
2512 
2513 #include <linux/smp_lock.h>
2514 SOCKOPS_WRAP(ipx_dgram, PF_IPX);
2515 
2516 static struct packet_type ipx_8023_packet_type = {
2517 	type:		__constant_htons(ETH_P_802_3),
2518 	func:		ipx_rcv,
2519 	data:		(void *) 1,	/* yap, I understand shared skbs :-) */
2520 };
2521 
2522 static struct packet_type ipx_dix_packet_type = {
2523 	type:		__constant_htons(ETH_P_IPX),
2524 	func:		ipx_rcv,
2525 	data:		(void *) 1,	/* yap, I understand shared skbs :-) */
2526 };
2527 
2528 static struct notifier_block ipx_dev_notifier = {
2529 	notifier_call:	ipxitf_device_event,
2530 };
2531 
2532 
2533 extern struct datalink_proto *make_EII_client(void);
2534 extern struct datalink_proto *make_8023_client(void);
2535 extern void destroy_EII_client(struct datalink_proto *);
2536 extern void destroy_8023_client(struct datalink_proto *);
2537 
2538 static unsigned char ipx_8022_type = 0xE0;
2539 static unsigned char ipx_snap_id[5] = { 0x0, 0x0, 0x0, 0x81, 0x37 };
2540 static char banner[] __initdata =
2541 	KERN_INFO "NET4: Linux IPX 0.47 for NET4.0\n"
2542 	KERN_INFO "IPX Portions Copyright (c) 1995 Caldera, Inc.\n" \
2543 	KERN_INFO "IPX Portions Copyright (c) 2000, 2001 Conectiva, Inc.\n";
2544 
ipx_init(void)2545 static int __init ipx_init(void)
2546 {
2547 	sock_register(&ipx_family_ops);
2548 
2549 	pEII_datalink = make_EII_client();
2550 	dev_add_pack(&ipx_dix_packet_type);
2551 
2552 	p8023_datalink = make_8023_client();
2553 	dev_add_pack(&ipx_8023_packet_type);
2554 
2555 	p8022_datalink = register_8022_client(ipx_8022_type, ipx_rcv);
2556 	if (!p8022_datalink)
2557 		printk(KERN_CRIT "IPX: Unable to register with 802.2\n");
2558 
2559 	pSNAP_datalink = register_snap_client(ipx_snap_id, ipx_rcv);
2560 	if (!pSNAP_datalink)
2561 		printk(KERN_CRIT "IPX: Unable to register with SNAP\n");
2562 
2563 	register_netdevice_notifier(&ipx_dev_notifier);
2564 	ipx_register_sysctl();
2565 #ifdef CONFIG_PROC_FS
2566 	proc_net_create("ipx", 0, ipx_get_info);
2567 	proc_net_create("ipx_interface", 0, ipx_interface_get_info);
2568 	proc_net_create("ipx_route", 0, ipx_rt_get_info);
2569 #endif
2570 	printk(banner);
2571 	return 0;
2572 }
2573 
2574 module_init(ipx_init);
2575 
2576 /* Higher layers need this info to prep tx pkts */
ipx_if_offset(unsigned long ipx_net_number)2577 int ipx_if_offset(unsigned long ipx_net_number)
2578 {
2579 	ipx_route *rt = ipxrtr_lookup(ipx_net_number);
2580 	int ret = -ENETUNREACH;
2581 
2582 	if (!rt)
2583 		goto out;
2584 	ret = rt->ir_intrfc->if_ipx_offset;
2585 	ipxrtr_put(rt);
2586 out:	return ret;
2587 }
2588 
2589 /* Export symbols for higher layers */
2590 EXPORT_SYMBOL(ipxrtr_route_skb);
2591 EXPORT_SYMBOL(ipx_if_offset);
2592 EXPORT_SYMBOL(ipx_remove_socket);
2593 EXPORT_SYMBOL(ipx_register_spx);
2594 EXPORT_SYMBOL(ipx_unregister_spx);
2595 
2596 /* Note on MOD_{INC,DEC}_USE_COUNT:
2597  *
2598  * Use counts are incremented/decremented when
2599  * sockets are created/deleted.
2600  *
2601  * Routes are always associated with an interface, and
2602  * allocs/frees will remain properly accounted for by
2603  * their associated interfaces.
2604  *
2605  * Ergo, before the ipx module can be removed, all IPX
2606  * sockets be closed from user space.
2607  */
2608 
ipx_proto_finito(void)2609 static void __exit ipx_proto_finito(void)
2610 {
2611 	/* no need to worry about having anything on the ipx_interfaces
2612 	 * list, when a interface is created we increment the module
2613 	 * usage count, so the module will only be unloaded when there
2614 	 * are no more interfaces */
2615 
2616 	proc_net_remove("ipx_route");
2617 	proc_net_remove("ipx_interface");
2618 	proc_net_remove("ipx");
2619 	ipx_unregister_sysctl();
2620 
2621 	unregister_netdevice_notifier(&ipx_dev_notifier);
2622 
2623 	unregister_snap_client(ipx_snap_id);
2624 	pSNAP_datalink = NULL;
2625 
2626 	unregister_8022_client(ipx_8022_type);
2627 	p8022_datalink = NULL;
2628 
2629 	dev_remove_pack(&ipx_8023_packet_type);
2630 	destroy_8023_client(p8023_datalink);
2631 	p8023_datalink = NULL;
2632 
2633 	dev_remove_pack(&ipx_dix_packet_type);
2634 	destroy_EII_client(pEII_datalink);
2635 	pEII_datalink = NULL;
2636 
2637 	sock_unregister(ipx_family_ops.family);
2638 }
2639 
2640 module_exit(ipx_proto_finito);
2641 MODULE_LICENSE("GPL");
2642