blob: da3d21c41d9064cb691342dfe23df37460766b84 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +090014 * Neither Alan Cox nor the Swansea University Computer Society admit
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * liability nor provide warranty for any of this software. This material
16 * is provided as is and at no charge.
17 *
18 * Portions Copyright (c) 2000-2003 Conectiva, Inc. <acme@conectiva.com.br>
19 * Neither Arnaldo Carvalho de Melo nor Conectiva, Inc. admit liability nor
20 * provide warranty for any of this software. This material is provided
21 * "AS-IS" and at no charge.
22 *
23 * Portions Copyright (c) 1995 Caldera, Inc. <greg@caldera.com>
24 * Neither Greg Page nor Caldera, Inc. admit liability nor provide
25 * warranty for any of this software. This material is provided
26 * "AS-IS" and at no charge.
27 *
28 * See net/ipx/ChangeLog.
29 */
30
Randy Dunlap4fc268d2006-01-11 12:17:47 -080031#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/if_arp.h>
34#include <linux/if_ether.h>
35#include <linux/init.h>
36#include <linux/ipx.h>
37#include <linux/kernel.h>
38#include <linux/list.h>
39#include <linux/module.h>
40#include <linux/net.h>
41#include <linux/netdevice.h>
42#include <linux/uio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/skbuff.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040045#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/socket.h>
47#include <linux/sockios.h>
48#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/types.h>
50#include <linux/termios.h>
51
52#include <net/ipx.h>
53#include <net/p8022.h>
54#include <net/psnap.h>
55#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070056#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#include <asm/uaccess.h>
59
60#ifdef CONFIG_SYSCTL
61extern void ipx_register_sysctl(void);
62extern void ipx_unregister_sysctl(void);
63#else
64#define ipx_register_sysctl()
65#define ipx_unregister_sysctl()
66#endif
67
68/* Configuration Variables */
69static unsigned char ipxcfg_max_hops = 16;
70static char ipxcfg_auto_select_primary;
71static char ipxcfg_auto_create_interfaces;
72int sysctl_ipx_pprop_broadcasting = 1;
73
74/* Global Variables */
75static struct datalink_proto *p8022_datalink;
76static struct datalink_proto *pEII_datalink;
77static struct datalink_proto *p8023_datalink;
78static struct datalink_proto *pSNAP_datalink;
79
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080080static const struct proto_ops ipx_dgram_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82LIST_HEAD(ipx_interfaces);
83DEFINE_SPINLOCK(ipx_interfaces_lock);
84
85struct ipx_interface *ipx_primary_net;
86struct ipx_interface *ipx_internal_net;
87
Al Viro4833ed02006-11-03 00:27:06 -080088extern int ipxrtr_add_route(__be32 network, struct ipx_interface *intrfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 unsigned char *node);
90extern void ipxrtr_del_routes(struct ipx_interface *intrfc);
91extern int ipxrtr_route_packet(struct sock *sk, struct sockaddr_ipx *usipx,
David Woodhouse0479ea02007-05-17 18:48:12 +080092 struct iovec *iov, size_t len, int noblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093extern int ipxrtr_route_skb(struct sk_buff *skb);
Al Viro4833ed02006-11-03 00:27:06 -080094extern struct ipx_route *ipxrtr_lookup(__be32 net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095extern int ipxrtr_ioctl(unsigned int cmd, void __user *arg);
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097struct ipx_interface *ipx_interfaces_head(void)
98{
99 struct ipx_interface *rc = NULL;
100
101 if (!list_empty(&ipx_interfaces))
102 rc = list_entry(ipx_interfaces.next,
103 struct ipx_interface, node);
104 return rc;
105}
106
107static void ipxcfg_set_auto_select(char val)
108{
109 ipxcfg_auto_select_primary = val;
110 if (val && !ipx_primary_net)
111 ipx_primary_net = ipx_interfaces_head();
112}
113
114static int ipxcfg_get_config_data(struct ipx_config_data __user *arg)
115{
116 struct ipx_config_data vals;
117
118 vals.ipxcfg_auto_create_interfaces = ipxcfg_auto_create_interfaces;
119 vals.ipxcfg_auto_select_primary = ipxcfg_auto_select_primary;
120
121 return copy_to_user(arg, &vals, sizeof(vals)) ? -EFAULT : 0;
122}
123
124/*
125 * Note: Sockets may not be removed _during_ an interrupt or inet_bh
126 * handler using this technique. They can be added although we do not
127 * use this facility.
128 */
129
130static void ipx_remove_socket(struct sock *sk)
131{
132 /* Determine interface with which socket is associated */
133 struct ipx_interface *intrfc = ipx_sk(sk)->intrfc;
134
135 if (!intrfc)
136 goto out;
137
138 ipxitf_hold(intrfc);
139 spin_lock_bh(&intrfc->if_sklist_lock);
140 sk_del_node_init(sk);
141 spin_unlock_bh(&intrfc->if_sklist_lock);
142 ipxitf_put(intrfc);
143out:
144 return;
145}
146
147static void ipx_destroy_socket(struct sock *sk)
148{
149 ipx_remove_socket(sk);
150 skb_queue_purge(&sk->sk_receive_queue);
Pavel Emelyanovc2b42332007-11-10 21:39:26 -0800151 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 sock_put(sk);
153}
154
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +0900155/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * The following code is used to support IPX Interfaces (IPXITF). An
157 * IPX interface is defined by a physical device and a frame type.
158 */
159
160/* ipxitf_clear_primary_net has to be called with ipx_interfaces_lock held */
161
162static void ipxitf_clear_primary_net(void)
163{
164 ipx_primary_net = NULL;
165 if (ipxcfg_auto_select_primary)
166 ipx_primary_net = ipx_interfaces_head();
167}
168
169static struct ipx_interface *__ipxitf_find_using_phys(struct net_device *dev,
Al Viro4833ed02006-11-03 00:27:06 -0800170 __be16 datalink)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 struct ipx_interface *i;
173
174 list_for_each_entry(i, &ipx_interfaces, node)
175 if (i->if_dev == dev && i->if_dlink_type == datalink)
176 goto out;
177 i = NULL;
178out:
179 return i;
180}
181
182static struct ipx_interface *ipxitf_find_using_phys(struct net_device *dev,
Al Viro4833ed02006-11-03 00:27:06 -0800183 __be16 datalink)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct ipx_interface *i;
186
187 spin_lock_bh(&ipx_interfaces_lock);
188 i = __ipxitf_find_using_phys(dev, datalink);
189 if (i)
190 ipxitf_hold(i);
191 spin_unlock_bh(&ipx_interfaces_lock);
192 return i;
193}
194
Al Viro4833ed02006-11-03 00:27:06 -0800195struct ipx_interface *ipxitf_find_using_net(__be32 net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 struct ipx_interface *i;
198
199 spin_lock_bh(&ipx_interfaces_lock);
200 if (net) {
201 list_for_each_entry(i, &ipx_interfaces, node)
202 if (i->if_netnum == net)
203 goto hold;
204 i = NULL;
205 goto unlock;
206 }
207
208 i = ipx_primary_net;
209 if (i)
210hold:
211 ipxitf_hold(i);
212unlock:
213 spin_unlock_bh(&ipx_interfaces_lock);
214 return i;
215}
216
217/* Sockets are bound to a particular IPX interface. */
218static void ipxitf_insert_socket(struct ipx_interface *intrfc, struct sock *sk)
219{
220 ipxitf_hold(intrfc);
221 spin_lock_bh(&intrfc->if_sklist_lock);
222 ipx_sk(sk)->intrfc = intrfc;
223 sk_add_node(sk, &intrfc->if_sklist);
224 spin_unlock_bh(&intrfc->if_sklist_lock);
225 ipxitf_put(intrfc);
226}
227
228/* caller must hold intrfc->if_sklist_lock */
229static struct sock *__ipxitf_find_socket(struct ipx_interface *intrfc,
Al Viro4833ed02006-11-03 00:27:06 -0800230 __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 struct sock *s;
233 struct hlist_node *node;
234
235 sk_for_each(s, node, &intrfc->if_sklist)
236 if (ipx_sk(s)->port == port)
237 goto found;
238 s = NULL;
239found:
240 return s;
241}
242
243/* caller must hold a reference to intrfc */
244static struct sock *ipxitf_find_socket(struct ipx_interface *intrfc,
Al Viro4833ed02006-11-03 00:27:06 -0800245 __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 struct sock *s;
248
249 spin_lock_bh(&intrfc->if_sklist_lock);
250 s = __ipxitf_find_socket(intrfc, port);
251 if (s)
252 sock_hold(s);
253 spin_unlock_bh(&intrfc->if_sklist_lock);
254
255 return s;
256}
257
258#ifdef CONFIG_IPX_INTERN
259static struct sock *ipxitf_find_internal_socket(struct ipx_interface *intrfc,
260 unsigned char *ipx_node,
Al Viro4833ed02006-11-03 00:27:06 -0800261 __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct sock *s;
264 struct hlist_node *node;
265
266 ipxitf_hold(intrfc);
267 spin_lock_bh(&intrfc->if_sklist_lock);
268
269 sk_for_each(s, node, &intrfc->if_sklist) {
270 struct ipx_sock *ipxs = ipx_sk(s);
271
272 if (ipxs->port == port &&
273 !memcmp(ipx_node, ipxs->node, IPX_NODE_LEN))
274 goto found;
275 }
276 s = NULL;
277found:
278 spin_unlock_bh(&intrfc->if_sklist_lock);
279 ipxitf_put(intrfc);
280 return s;
281}
282#endif
283
284static void __ipxitf_down(struct ipx_interface *intrfc)
285{
286 struct sock *s;
287 struct hlist_node *node, *t;
288
289 /* Delete all routes associated with this interface */
290 ipxrtr_del_routes(intrfc);
291
292 spin_lock_bh(&intrfc->if_sklist_lock);
293 /* error sockets */
294 sk_for_each_safe(s, node, t, &intrfc->if_sklist) {
295 struct ipx_sock *ipxs = ipx_sk(s);
296
297 s->sk_err = ENOLINK;
298 s->sk_error_report(s);
299 ipxs->intrfc = NULL;
300 ipxs->port = 0;
301 sock_set_flag(s, SOCK_ZAPPED); /* Indicates it is no longer bound */
302 sk_del_node_init(s);
303 }
304 INIT_HLIST_HEAD(&intrfc->if_sklist);
305 spin_unlock_bh(&intrfc->if_sklist_lock);
306
307 /* remove this interface from list */
308 list_del(&intrfc->node);
309
310 /* remove this interface from *special* networks */
311 if (intrfc == ipx_primary_net)
312 ipxitf_clear_primary_net();
313 if (intrfc == ipx_internal_net)
314 ipx_internal_net = NULL;
315
316 if (intrfc->if_dev)
317 dev_put(intrfc->if_dev);
318 kfree(intrfc);
319}
320
321void ipxitf_down(struct ipx_interface *intrfc)
322{
323 spin_lock_bh(&ipx_interfaces_lock);
324 __ipxitf_down(intrfc);
325 spin_unlock_bh(&ipx_interfaces_lock);
326}
327
328static __inline__ void __ipxitf_put(struct ipx_interface *intrfc)
329{
330 if (atomic_dec_and_test(&intrfc->refcnt))
331 __ipxitf_down(intrfc);
332}
333
334static int ipxitf_device_event(struct notifier_block *notifier,
335 unsigned long event, void *ptr)
336{
337 struct net_device *dev = ptr;
338 struct ipx_interface *i, *tmp;
339
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700340 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200341 return NOTIFY_DONE;
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (event != NETDEV_DOWN && event != NETDEV_UP)
344 goto out;
345
346 spin_lock_bh(&ipx_interfaces_lock);
347 list_for_each_entry_safe(i, tmp, &ipx_interfaces, node)
348 if (i->if_dev == dev) {
349 if (event == NETDEV_UP)
350 ipxitf_hold(i);
351 else
352 __ipxitf_put(i);
353 }
354 spin_unlock_bh(&ipx_interfaces_lock);
355out:
356 return NOTIFY_DONE;
357}
358
359
360static __exit void ipxitf_cleanup(void)
361{
362 struct ipx_interface *i, *tmp;
363
364 spin_lock_bh(&ipx_interfaces_lock);
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +0900365 list_for_each_entry_safe(i, tmp, &ipx_interfaces, node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 __ipxitf_put(i);
367 spin_unlock_bh(&ipx_interfaces_lock);
368}
369
370static void ipxitf_def_skb_handler(struct sock *sock, struct sk_buff *skb)
371{
372 if (sock_queue_rcv_skb(sock, skb) < 0)
373 kfree_skb(skb);
374}
375
376/*
377 * On input skb->sk is NULL. Nobody is charged for the memory.
378 */
379
380/* caller must hold a reference to intrfc */
381
382#ifdef CONFIG_IPX_INTERN
383static int ipxitf_demux_socket(struct ipx_interface *intrfc,
384 struct sk_buff *skb, int copy)
385{
386 struct ipxhdr *ipx = ipx_hdr(skb);
387 int is_broadcast = !memcmp(ipx->ipx_dest.node, ipx_broadcast_node,
388 IPX_NODE_LEN);
389 struct sock *s;
390 struct hlist_node *node;
391 int rc;
392
393 spin_lock_bh(&intrfc->if_sklist_lock);
394
395 sk_for_each(s, node, &intrfc->if_sklist) {
396 struct ipx_sock *ipxs = ipx_sk(s);
397
398 if (ipxs->port == ipx->ipx_dest.sock &&
399 (is_broadcast || !memcmp(ipx->ipx_dest.node,
400 ipxs->node, IPX_NODE_LEN))) {
401 /* We found a socket to which to send */
402 struct sk_buff *skb1;
403
404 if (copy) {
405 skb1 = skb_clone(skb, GFP_ATOMIC);
406 rc = -ENOMEM;
407 if (!skb1)
408 goto out;
409 } else {
410 skb1 = skb;
411 copy = 1; /* skb may only be used once */
412 }
413 ipxitf_def_skb_handler(s, skb1);
414
415 /* On an external interface, one socket can listen */
416 if (intrfc != ipx_internal_net)
417 break;
418 }
419 }
420
421 /* skb was solely for us, and we did not make a copy, so free it. */
422 if (!copy)
423 kfree_skb(skb);
424
425 rc = 0;
426out:
427 spin_unlock_bh(&intrfc->if_sklist_lock);
428 return rc;
429}
430#else
431static struct sock *ncp_connection_hack(struct ipx_interface *intrfc,
432 struct ipxhdr *ipx)
433{
434 /* The packet's target is a NCP connection handler. We want to hand it
435 * to the correct socket directly within the kernel, so that the
436 * mars_nwe packet distribution process does not have to do it. Here we
437 * only care about NCP and BURST packets.
438 *
439 * You might call this a hack, but believe me, you do not want a
440 * complete NCP layer in the kernel, and this is VERY fast as well. */
441 struct sock *sk = NULL;
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +0900442 int connection = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 u8 *ncphdr = (u8 *)(ipx + 1);
444
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +0900445 if (*ncphdr == 0x22 && *(ncphdr + 1) == 0x22) /* NCP request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 connection = (((int) *(ncphdr + 5)) << 8) | (int) *(ncphdr + 3);
447 else if (*ncphdr == 0x77 && *(ncphdr + 1) == 0x77) /* BURST packet */
448 connection = (((int) *(ncphdr + 9)) << 8) | (int) *(ncphdr + 8);
449
450 if (connection) {
451 struct hlist_node *node;
452 /* Now we have to look for a special NCP connection handling
453 * socket. Only these sockets have ipx_ncp_conn != 0, set by
454 * SIOCIPXNCPCONN. */
455 spin_lock_bh(&intrfc->if_sklist_lock);
456 sk_for_each(sk, node, &intrfc->if_sklist)
457 if (ipx_sk(sk)->ipx_ncp_conn == connection) {
458 sock_hold(sk);
459 goto found;
460 }
461 sk = NULL;
462 found:
463 spin_unlock_bh(&intrfc->if_sklist_lock);
464 }
465 return sk;
466}
467
468static int ipxitf_demux_socket(struct ipx_interface *intrfc,
469 struct sk_buff *skb, int copy)
470{
471 struct ipxhdr *ipx = ipx_hdr(skb);
472 struct sock *sock1 = NULL, *sock2 = NULL;
473 struct sk_buff *skb1 = NULL, *skb2 = NULL;
474 int rc;
475
476 if (intrfc == ipx_primary_net && ntohs(ipx->ipx_dest.sock) == 0x451)
477 sock1 = ncp_connection_hack(intrfc, ipx);
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +0900478 if (!sock1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /* No special socket found, forward the packet the normal way */
480 sock1 = ipxitf_find_socket(intrfc, ipx->ipx_dest.sock);
481
482 /*
483 * We need to check if there is a primary net and if
484 * this is addressed to one of the *SPECIAL* sockets because
485 * these need to be propagated to the primary net.
486 * The *SPECIAL* socket list contains: 0x452(SAP), 0x453(RIP) and
487 * 0x456(Diagnostic).
488 */
489
490 if (ipx_primary_net && intrfc != ipx_primary_net) {
491 const int dsock = ntohs(ipx->ipx_dest.sock);
492
493 if (dsock == 0x452 || dsock == 0x453 || dsock == 0x456)
494 /* The appropriate thing to do here is to dup the
495 * packet and route to the primary net interface via
496 * ipxitf_send; however, we'll cheat and just demux it
497 * here. */
498 sock2 = ipxitf_find_socket(ipx_primary_net,
499 ipx->ipx_dest.sock);
500 }
501
502 /*
503 * If there is nothing to do return. The kfree will cancel any charging.
504 */
505 rc = 0;
506 if (!sock1 && !sock2) {
507 if (!copy)
508 kfree_skb(skb);
509 goto out;
510 }
511
512 /*
513 * This next segment of code is a little awkward, but it sets it up
514 * so that the appropriate number of copies of the SKB are made and
515 * that skb1 and skb2 point to it (them) so that it (they) can be
516 * demuxed to sock1 and/or sock2. If we are unable to make enough
517 * copies, we do as much as is possible.
518 */
519
520 if (copy)
521 skb1 = skb_clone(skb, GFP_ATOMIC);
522 else
523 skb1 = skb;
524
525 rc = -ENOMEM;
526 if (!skb1)
527 goto out_put;
528
529 /* Do we need 2 SKBs? */
530 if (sock1 && sock2)
531 skb2 = skb_clone(skb1, GFP_ATOMIC);
532 else
533 skb2 = skb1;
534
535 if (sock1)
536 ipxitf_def_skb_handler(sock1, skb1);
537
538 if (!skb2)
539 goto out_put;
540
541 if (sock2)
542 ipxitf_def_skb_handler(sock2, skb2);
543
544 rc = 0;
545out_put:
546 if (sock1)
547 sock_put(sock1);
548 if (sock2)
549 sock_put(sock2);
550out:
551 return rc;
552}
553#endif /* CONFIG_IPX_INTERN */
554
555static struct sk_buff *ipxitf_adjust_skbuff(struct ipx_interface *intrfc,
556 struct sk_buff *skb)
557{
558 struct sk_buff *skb2;
559 int in_offset = (unsigned char *)ipx_hdr(skb) - skb->head;
560 int out_offset = intrfc->if_ipx_offset;
561 int len;
562
563 /* Hopefully, most cases */
564 if (in_offset >= out_offset)
565 return skb;
566
567 /* Need new SKB */
568 len = skb->len + out_offset;
569 skb2 = alloc_skb(len, GFP_ATOMIC);
570 if (skb2) {
571 skb_reserve(skb2, out_offset);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300572 skb_reset_network_header(skb2);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300573 skb_reset_transport_header(skb2);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300574 skb_put(skb2, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 memcpy(ipx_hdr(skb2), ipx_hdr(skb), skb->len);
576 memcpy(skb2->cb, skb->cb, sizeof(skb->cb));
577 }
578 kfree_skb(skb);
579 return skb2;
580}
581
582/* caller must hold a reference to intrfc and the skb has to be unshared */
583int ipxitf_send(struct ipx_interface *intrfc, struct sk_buff *skb, char *node)
584{
585 struct ipxhdr *ipx = ipx_hdr(skb);
586 struct net_device *dev = intrfc->if_dev;
587 struct datalink_proto *dl = intrfc->if_dlink;
588 char dest_node[IPX_NODE_LEN];
589 int send_to_wire = 1;
590 int addr_len;
591
592 ipx->ipx_tctrl = IPX_SKB_CB(skb)->ipx_tctrl;
593 ipx->ipx_dest.net = IPX_SKB_CB(skb)->ipx_dest_net;
594 ipx->ipx_source.net = IPX_SKB_CB(skb)->ipx_source_net;
595
596 /* see if we need to include the netnum in the route list */
597 if (IPX_SKB_CB(skb)->last_hop.index >= 0) {
Al Viro4833ed02006-11-03 00:27:06 -0800598 __be32 *last_hop = (__be32 *)(((u8 *) skb->data) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 sizeof(struct ipxhdr) +
600 IPX_SKB_CB(skb)->last_hop.index *
Al Viro4833ed02006-11-03 00:27:06 -0800601 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 *last_hop = IPX_SKB_CB(skb)->last_hop.netnum;
603 IPX_SKB_CB(skb)->last_hop.index = -1;
604 }
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +0900605
606 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * We need to know how many skbuffs it will take to send out this
608 * packet to avoid unnecessary copies.
609 */
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +0900610
611 if (!dl || !dev || dev->flags & IFF_LOOPBACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 send_to_wire = 0; /* No non looped */
613
614 /*
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +0900615 * See if this should be demuxed to sockets on this interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 *
617 * We want to ensure the original was eaten or that we only use
618 * up clones.
619 */
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +0900620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (ipx->ipx_dest.net == intrfc->if_netnum) {
622 /*
623 * To our own node, loop and free the original.
624 * The internal net will receive on all node address.
625 */
626 if (intrfc == ipx_internal_net ||
627 !memcmp(intrfc->if_node, node, IPX_NODE_LEN)) {
628 /* Don't charge sender */
629 skb_orphan(skb);
630
631 /* Will charge receiver */
632 return ipxitf_demux_socket(intrfc, skb, 0);
633 }
634
635 /* Broadcast, loop and possibly keep to send on. */
636 if (!memcmp(ipx_broadcast_node, node, IPX_NODE_LEN)) {
637 if (!send_to_wire)
638 skb_orphan(skb);
639 ipxitf_demux_socket(intrfc, skb, send_to_wire);
640 if (!send_to_wire)
641 goto out;
642 }
643 }
644
645 /*
646 * If the originating net is not equal to our net; this is routed
647 * We are still charging the sender. Which is right - the driver
648 * free will handle this fairly.
649 */
650 if (ipx->ipx_source.net != intrfc->if_netnum) {
651 /*
652 * Unshare the buffer before modifying the count in
653 * case it's a flood or tcpdump
654 */
655 skb = skb_unshare(skb, GFP_ATOMIC);
656 if (!skb)
657 goto out;
658 if (++ipx->ipx_tctrl > ipxcfg_max_hops)
659 send_to_wire = 0;
660 }
661
662 if (!send_to_wire) {
663 kfree_skb(skb);
664 goto out;
665 }
666
667 /* Determine the appropriate hardware address */
668 addr_len = dev->addr_len;
669 if (!memcmp(ipx_broadcast_node, node, IPX_NODE_LEN))
670 memcpy(dest_node, dev->broadcast, addr_len);
671 else
672 memcpy(dest_node, &(node[IPX_NODE_LEN-addr_len]), addr_len);
673
674 /* Make any compensation for differing physical/data link size */
675 skb = ipxitf_adjust_skbuff(intrfc, skb);
676 if (!skb)
677 goto out;
678
679 /* set up data link and physical headers */
680 skb->dev = dev;
681 skb->protocol = htons(ETH_P_IPX);
682
683 /* Send it out */
684 dl->request(dl, skb, dest_node);
685out:
686 return 0;
687}
688
689static int ipxitf_add_local_route(struct ipx_interface *intrfc)
690{
691 return ipxrtr_add_route(intrfc->if_netnum, intrfc, NULL);
692}
693
694static void ipxitf_discover_netnum(struct ipx_interface *intrfc,
695 struct sk_buff *skb);
696static int ipxitf_pprop(struct ipx_interface *intrfc, struct sk_buff *skb);
697
698static int ipxitf_rcv(struct ipx_interface *intrfc, struct sk_buff *skb)
699{
700 struct ipxhdr *ipx = ipx_hdr(skb);
701 int rc = 0;
702
703 ipxitf_hold(intrfc);
704
705 /* See if we should update our network number */
706 if (!intrfc->if_netnum) /* net number of intrfc not known yet */
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +0900707 ipxitf_discover_netnum(intrfc, skb);
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 IPX_SKB_CB(skb)->last_hop.index = -1;
710 if (ipx->ipx_type == IPX_TYPE_PPROP) {
711 rc = ipxitf_pprop(intrfc, skb);
712 if (rc)
713 goto out_free_skb;
714 }
715
716 /* local processing follows */
717 if (!IPX_SKB_CB(skb)->ipx_dest_net)
718 IPX_SKB_CB(skb)->ipx_dest_net = intrfc->if_netnum;
719 if (!IPX_SKB_CB(skb)->ipx_source_net)
720 IPX_SKB_CB(skb)->ipx_source_net = intrfc->if_netnum;
721
722 /* it doesn't make sense to route a pprop packet, there's no meaning
723 * in the ipx_dest_net for such packets */
724 if (ipx->ipx_type != IPX_TYPE_PPROP &&
725 intrfc->if_netnum != IPX_SKB_CB(skb)->ipx_dest_net) {
726 /* We only route point-to-point packets. */
727 if (skb->pkt_type == PACKET_HOST) {
728 skb = skb_unshare(skb, GFP_ATOMIC);
729 if (skb)
730 rc = ipxrtr_route_skb(skb);
731 goto out_intrfc;
732 }
733
734 goto out_free_skb;
735 }
736
737 /* see if we should keep it */
738 if (!memcmp(ipx_broadcast_node, ipx->ipx_dest.node, IPX_NODE_LEN) ||
739 !memcmp(intrfc->if_node, ipx->ipx_dest.node, IPX_NODE_LEN)) {
740 rc = ipxitf_demux_socket(intrfc, skb, 0);
741 goto out_intrfc;
742 }
743
744 /* we couldn't pawn it off so unload it */
745out_free_skb:
746 kfree_skb(skb);
747out_intrfc:
748 ipxitf_put(intrfc);
749 return rc;
750}
751
752static void ipxitf_discover_netnum(struct ipx_interface *intrfc,
753 struct sk_buff *skb)
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +0900754{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 const struct ipx_cb *cb = IPX_SKB_CB(skb);
756
757 /* see if this is an intra packet: source_net == dest_net */
758 if (cb->ipx_source_net == cb->ipx_dest_net && cb->ipx_source_net) {
759 struct ipx_interface *i =
760 ipxitf_find_using_net(cb->ipx_source_net);
761 /* NB: NetWare servers lie about their hop count so we
762 * dropped the test based on it. This is the best way
763 * to determine this is a 0 hop count packet. */
764 if (!i) {
765 intrfc->if_netnum = cb->ipx_source_net;
766 ipxitf_add_local_route(intrfc);
767 } else {
768 printk(KERN_WARNING "IPX: Network number collision "
769 "%lx\n %s %s and %s %s\n",
Al Viro4833ed02006-11-03 00:27:06 -0800770 (unsigned long) ntohl(cb->ipx_source_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 ipx_device_name(i),
772 ipx_frame_name(i->if_dlink_type),
773 ipx_device_name(intrfc),
774 ipx_frame_name(intrfc->if_dlink_type));
775 ipxitf_put(i);
776 }
777 }
778}
779
780/**
781 * ipxitf_pprop - Process packet propagation IPX packet type 0x14, used for
782 * NetBIOS broadcasts
783 * @intrfc: IPX interface receiving this packet
784 * @skb: Received packet
785 *
786 * Checks if packet is valid: if its more than %IPX_MAX_PPROP_HOPS hops or if it
787 * is smaller than a IPX header + the room for %IPX_MAX_PPROP_HOPS hops we drop
788 * it, not even processing it locally, if it has exact %IPX_MAX_PPROP_HOPS we
789 * don't broadcast it, but process it locally. See chapter 5 of Novell's "IPX
790 * RIP and SAP Router Specification", Part Number 107-000029-001.
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +0900791 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 * If it is valid, check if we have pprop broadcasting enabled by the user,
793 * if not, just return zero for local processing.
794 *
795 * If it is enabled check the packet and don't broadcast it if we have already
796 * seen this packet.
797 *
798 * Broadcast: send it to the interfaces that aren't on the packet visited nets
799 * array, just after the IPX header.
800 *
801 * Returns -EINVAL for invalid packets, so that the calling function drops
802 * the packet without local processing. 0 if packet is to be locally processed.
803 */
804static int ipxitf_pprop(struct ipx_interface *intrfc, struct sk_buff *skb)
805{
806 struct ipxhdr *ipx = ipx_hdr(skb);
807 int i, rc = -EINVAL;
808 struct ipx_interface *ifcs;
809 char *c;
Al Viro4833ed02006-11-03 00:27:06 -0800810 __be32 *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /* Illegal packet - too many hops or too short */
813 /* We decide to throw it away: no broadcasting, no local processing.
814 * NetBIOS unaware implementations route them as normal packets -
815 * tctrl <= 15, any data payload... */
816 if (IPX_SKB_CB(skb)->ipx_tctrl > IPX_MAX_PPROP_HOPS ||
817 ntohs(ipx->ipx_pktsize) < sizeof(struct ipxhdr) +
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +0900818 IPX_MAX_PPROP_HOPS * sizeof(u32))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 goto out;
820 /* are we broadcasting this damn thing? */
821 rc = 0;
822 if (!sysctl_ipx_pprop_broadcasting)
823 goto out;
824 /* We do broadcast packet on the IPX_MAX_PPROP_HOPS hop, but we
825 * process it locally. All previous hops broadcasted it, and process it
826 * locally. */
827 if (IPX_SKB_CB(skb)->ipx_tctrl == IPX_MAX_PPROP_HOPS)
828 goto out;
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +0900829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 c = ((u8 *) ipx) + sizeof(struct ipxhdr);
Al Viro4833ed02006-11-03 00:27:06 -0800831 l = (__be32 *) c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /* Don't broadcast packet if already seen this net */
834 for (i = 0; i < IPX_SKB_CB(skb)->ipx_tctrl; i++)
835 if (*l++ == intrfc->if_netnum)
836 goto out;
837
838 /* < IPX_MAX_PPROP_HOPS hops && input interface not in list. Save the
839 * position where we will insert recvd netnum into list, later on,
840 * in ipxitf_send */
841 IPX_SKB_CB(skb)->last_hop.index = i;
842 IPX_SKB_CB(skb)->last_hop.netnum = intrfc->if_netnum;
843 /* xmit on all other interfaces... */
844 spin_lock_bh(&ipx_interfaces_lock);
845 list_for_each_entry(ifcs, &ipx_interfaces, node) {
846 /* Except unconfigured interfaces */
847 if (!ifcs->if_netnum)
848 continue;
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +0900849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /* That aren't in the list */
851 if (ifcs == intrfc)
852 continue;
Al Viro4833ed02006-11-03 00:27:06 -0800853 l = (__be32 *) c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* don't consider the last entry in the packet list,
855 * it is our netnum, and it is not there yet */
856 for (i = 0; i < IPX_SKB_CB(skb)->ipx_tctrl; i++)
857 if (ifcs->if_netnum == *l++)
858 break;
859 if (i == IPX_SKB_CB(skb)->ipx_tctrl) {
860 struct sk_buff *s = skb_copy(skb, GFP_ATOMIC);
861
862 if (s) {
863 IPX_SKB_CB(s)->ipx_dest_net = ifcs->if_netnum;
864 ipxrtr_route_skb(s);
865 }
866 }
867 }
868 spin_unlock_bh(&ipx_interfaces_lock);
869out:
870 return rc;
871}
872
873static void ipxitf_insert(struct ipx_interface *intrfc)
874{
875 spin_lock_bh(&ipx_interfaces_lock);
876 list_add_tail(&intrfc->node, &ipx_interfaces);
877 spin_unlock_bh(&ipx_interfaces_lock);
878
879 if (ipxcfg_auto_select_primary && !ipx_primary_net)
880 ipx_primary_net = intrfc;
881}
882
Al Viro4833ed02006-11-03 00:27:06 -0800883static struct ipx_interface *ipxitf_alloc(struct net_device *dev, __be32 netnum,
884 __be16 dlink_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 struct datalink_proto *dlink,
886 unsigned char internal,
887 int ipx_offset)
888{
889 struct ipx_interface *intrfc = kmalloc(sizeof(*intrfc), GFP_ATOMIC);
890
891 if (intrfc) {
892 intrfc->if_dev = dev;
893 intrfc->if_netnum = netnum;
894 intrfc->if_dlink_type = dlink_type;
895 intrfc->if_dlink = dlink;
896 intrfc->if_internal = internal;
897 intrfc->if_ipx_offset = ipx_offset;
898 intrfc->if_sknum = IPX_MIN_EPHEMERAL_SOCKET;
899 INIT_HLIST_HEAD(&intrfc->if_sklist);
900 atomic_set(&intrfc->refcnt, 1);
901 spin_lock_init(&intrfc->if_sklist_lock);
902 }
903
904 return intrfc;
905}
906
907static int ipxitf_create_internal(struct ipx_interface_definition *idef)
908{
909 struct ipx_interface *intrfc;
910 int rc = -EEXIST;
911
912 /* Only one primary network allowed */
913 if (ipx_primary_net)
914 goto out;
915
916 /* Must have a valid network number */
917 rc = -EADDRNOTAVAIL;
918 if (!idef->ipx_network)
919 goto out;
920 intrfc = ipxitf_find_using_net(idef->ipx_network);
921 rc = -EADDRINUSE;
922 if (intrfc) {
923 ipxitf_put(intrfc);
924 goto out;
925 }
926 intrfc = ipxitf_alloc(NULL, idef->ipx_network, 0, NULL, 1, 0);
927 rc = -EAGAIN;
928 if (!intrfc)
929 goto out;
930 memcpy((char *)&(intrfc->if_node), idef->ipx_node, IPX_NODE_LEN);
931 ipx_internal_net = ipx_primary_net = intrfc;
932 ipxitf_hold(intrfc);
933 ipxitf_insert(intrfc);
934
935 rc = ipxitf_add_local_route(intrfc);
936 ipxitf_put(intrfc);
937out:
938 return rc;
939}
940
Alexey Dobriyan4ac396c2006-05-16 15:17:49 -0700941static __be16 ipx_map_frame_type(unsigned char type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Alexey Dobriyan4ac396c2006-05-16 15:17:49 -0700943 __be16 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 switch (type) {
946 case IPX_FRAME_ETHERII: rc = htons(ETH_P_IPX); break;
947 case IPX_FRAME_8022: rc = htons(ETH_P_802_2); break;
948 case IPX_FRAME_SNAP: rc = htons(ETH_P_SNAP); break;
949 case IPX_FRAME_8023: rc = htons(ETH_P_802_3); break;
950 }
951
952 return rc;
953}
954
955static int ipxitf_create(struct ipx_interface_definition *idef)
956{
957 struct net_device *dev;
Al Viro4833ed02006-11-03 00:27:06 -0800958 __be16 dlink_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 struct datalink_proto *datalink = NULL;
960 struct ipx_interface *intrfc;
961 int rc;
962
963 if (idef->ipx_special == IPX_INTERNAL) {
964 rc = ipxitf_create_internal(idef);
965 goto out;
966 }
967
968 rc = -EEXIST;
969 if (idef->ipx_special == IPX_PRIMARY && ipx_primary_net)
970 goto out;
971
972 intrfc = ipxitf_find_using_net(idef->ipx_network);
973 rc = -EADDRINUSE;
974 if (idef->ipx_network && intrfc) {
975 ipxitf_put(intrfc);
976 goto out;
977 }
978
979 if (intrfc)
980 ipxitf_put(intrfc);
981
Eric W. Biederman881d9662007-09-17 11:56:21 -0700982 dev = dev_get_by_name(&init_net, idef->ipx_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 rc = -ENODEV;
984 if (!dev)
985 goto out;
986
987 switch (idef->ipx_dlink_type) {
988 case IPX_FRAME_TR_8022:
989 printk(KERN_WARNING "IPX frame type 802.2TR is "
990 "obsolete Use 802.2 instead.\n");
991 /* fall through */
992 case IPX_FRAME_8022:
993 dlink_type = htons(ETH_P_802_2);
994 datalink = p8022_datalink;
995 break;
996 case IPX_FRAME_ETHERII:
997 if (dev->type != ARPHRD_IEEE802) {
998 dlink_type = htons(ETH_P_IPX);
999 datalink = pEII_datalink;
1000 break;
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +09001001 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 printk(KERN_WARNING "IPX frame type EtherII over "
1003 "token-ring is obsolete. Use SNAP "
1004 "instead.\n");
1005 /* fall through */
1006 case IPX_FRAME_SNAP:
1007 dlink_type = htons(ETH_P_SNAP);
1008 datalink = pSNAP_datalink;
1009 break;
1010 case IPX_FRAME_8023:
1011 dlink_type = htons(ETH_P_802_3);
1012 datalink = p8023_datalink;
1013 break;
1014 case IPX_FRAME_NONE:
1015 default:
1016 rc = -EPROTONOSUPPORT;
1017 goto out_dev;
1018 }
1019
1020 rc = -ENETDOWN;
1021 if (!(dev->flags & IFF_UP))
1022 goto out_dev;
1023
1024 /* Check addresses are suitable */
1025 rc = -EINVAL;
1026 if (dev->addr_len > IPX_NODE_LEN)
1027 goto out_dev;
1028
1029 intrfc = ipxitf_find_using_phys(dev, dlink_type);
1030 if (!intrfc) {
1031 /* Ok now create */
1032 intrfc = ipxitf_alloc(dev, idef->ipx_network, dlink_type,
1033 datalink, 0, dev->hard_header_len +
1034 datalink->header_length);
1035 rc = -EAGAIN;
1036 if (!intrfc)
1037 goto out_dev;
1038 /* Setup primary if necessary */
1039 if (idef->ipx_special == IPX_PRIMARY)
1040 ipx_primary_net = intrfc;
1041 if (!memcmp(idef->ipx_node, "\000\000\000\000\000\000",
1042 IPX_NODE_LEN)) {
1043 memset(intrfc->if_node, 0, IPX_NODE_LEN);
1044 memcpy(intrfc->if_node + IPX_NODE_LEN - dev->addr_len,
1045 dev->dev_addr, dev->addr_len);
1046 } else
1047 memcpy(intrfc->if_node, idef->ipx_node, IPX_NODE_LEN);
1048 ipxitf_hold(intrfc);
1049 ipxitf_insert(intrfc);
1050 }
1051
1052
1053 /* If the network number is known, add a route */
1054 rc = 0;
1055 if (!intrfc->if_netnum)
1056 goto out_intrfc;
1057
1058 rc = ipxitf_add_local_route(intrfc);
1059out_intrfc:
1060 ipxitf_put(intrfc);
1061 goto out;
1062out_dev:
1063 dev_put(dev);
1064out:
1065 return rc;
1066}
1067
1068static int ipxitf_delete(struct ipx_interface_definition *idef)
1069{
1070 struct net_device *dev = NULL;
Al Viro4833ed02006-11-03 00:27:06 -08001071 __be16 dlink_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 struct ipx_interface *intrfc;
1073 int rc = 0;
1074
1075 spin_lock_bh(&ipx_interfaces_lock);
1076 if (idef->ipx_special == IPX_INTERNAL) {
1077 if (ipx_internal_net) {
1078 __ipxitf_put(ipx_internal_net);
1079 goto out;
1080 }
1081 rc = -ENOENT;
1082 goto out;
1083 }
1084
1085 dlink_type = ipx_map_frame_type(idef->ipx_dlink_type);
1086 rc = -EPROTONOSUPPORT;
1087 if (!dlink_type)
1088 goto out;
1089
Eric W. Biederman881d9662007-09-17 11:56:21 -07001090 dev = __dev_get_by_name(&init_net, idef->ipx_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 rc = -ENODEV;
1092 if (!dev)
1093 goto out;
1094
1095 intrfc = __ipxitf_find_using_phys(dev, dlink_type);
1096 rc = -EINVAL;
1097 if (!intrfc)
1098 goto out;
1099 __ipxitf_put(intrfc);
1100
1101 rc = 0;
1102out:
1103 spin_unlock_bh(&ipx_interfaces_lock);
1104 return rc;
1105}
1106
1107static struct ipx_interface *ipxitf_auto_create(struct net_device *dev,
Al Viro4833ed02006-11-03 00:27:06 -08001108 __be16 dlink_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110 struct ipx_interface *intrfc = NULL;
1111 struct datalink_proto *datalink;
1112
1113 if (!dev)
1114 goto out;
1115
1116 /* Check addresses are suitable */
1117 if (dev->addr_len > IPX_NODE_LEN)
1118 goto out;
1119
Al Viro4833ed02006-11-03 00:27:06 -08001120 switch (ntohs(dlink_type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 case ETH_P_IPX: datalink = pEII_datalink; break;
1122 case ETH_P_802_2: datalink = p8022_datalink; break;
1123 case ETH_P_SNAP: datalink = pSNAP_datalink; break;
1124 case ETH_P_802_3: datalink = p8023_datalink; break;
1125 default: goto out;
1126 }
1127
1128 intrfc = ipxitf_alloc(dev, 0, dlink_type, datalink, 0,
1129 dev->hard_header_len + datalink->header_length);
1130
1131 if (intrfc) {
1132 memset(intrfc->if_node, 0, IPX_NODE_LEN);
1133 memcpy((char *)&(intrfc->if_node[IPX_NODE_LEN-dev->addr_len]),
1134 dev->dev_addr, dev->addr_len);
1135 spin_lock_init(&intrfc->if_sklist_lock);
1136 atomic_set(&intrfc->refcnt, 1);
1137 ipxitf_insert(intrfc);
1138 dev_hold(dev);
1139 }
1140
1141out:
1142 return intrfc;
1143}
1144
1145static int ipxitf_ioctl(unsigned int cmd, void __user *arg)
1146{
1147 int rc = -EINVAL;
1148 struct ifreq ifr;
1149 int val;
1150
1151 switch (cmd) {
1152 case SIOCSIFADDR: {
1153 struct sockaddr_ipx *sipx;
1154 struct ipx_interface_definition f;
1155
1156 rc = -EFAULT;
1157 if (copy_from_user(&ifr, arg, sizeof(ifr)))
1158 break;
1159 sipx = (struct sockaddr_ipx *)&ifr.ifr_addr;
1160 rc = -EINVAL;
1161 if (sipx->sipx_family != AF_IPX)
1162 break;
1163 f.ipx_network = sipx->sipx_network;
1164 memcpy(f.ipx_device, ifr.ifr_name,
1165 sizeof(f.ipx_device));
1166 memcpy(f.ipx_node, sipx->sipx_node, IPX_NODE_LEN);
1167 f.ipx_dlink_type = sipx->sipx_type;
1168 f.ipx_special = sipx->sipx_special;
1169
1170 if (sipx->sipx_action == IPX_DLTITF)
1171 rc = ipxitf_delete(&f);
1172 else
1173 rc = ipxitf_create(&f);
1174 break;
1175 }
1176 case SIOCGIFADDR: {
1177 struct sockaddr_ipx *sipx;
1178 struct ipx_interface *ipxif;
1179 struct net_device *dev;
1180
1181 rc = -EFAULT;
1182 if (copy_from_user(&ifr, arg, sizeof(ifr)))
1183 break;
1184 sipx = (struct sockaddr_ipx *)&ifr.ifr_addr;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001185 dev = __dev_get_by_name(&init_net, ifr.ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 rc = -ENODEV;
1187 if (!dev)
1188 break;
1189 ipxif = ipxitf_find_using_phys(dev,
1190 ipx_map_frame_type(sipx->sipx_type));
1191 rc = -EADDRNOTAVAIL;
1192 if (!ipxif)
1193 break;
1194
1195 sipx->sipx_family = AF_IPX;
1196 sipx->sipx_network = ipxif->if_netnum;
1197 memcpy(sipx->sipx_node, ipxif->if_node,
1198 sizeof(sipx->sipx_node));
1199 rc = -EFAULT;
1200 if (copy_to_user(arg, &ifr, sizeof(ifr)))
1201 break;
1202 ipxitf_put(ipxif);
1203 rc = 0;
1204 break;
1205 }
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +09001206 case SIOCAIPXITFCRT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 rc = -EFAULT;
1208 if (get_user(val, (unsigned char __user *) arg))
1209 break;
1210 rc = 0;
1211 ipxcfg_auto_create_interfaces = val;
1212 break;
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +09001213 case SIOCAIPXPRISLT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 rc = -EFAULT;
1215 if (get_user(val, (unsigned char __user *) arg))
1216 break;
1217 rc = 0;
1218 ipxcfg_set_auto_select(val);
1219 break;
1220 }
1221
1222 return rc;
1223}
1224
1225/*
1226 * Checksum routine for IPX
1227 */
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +09001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229/* Note: We assume ipx_tctrl==0 and htons(length)==ipx_pktsize */
1230/* This functions should *not* mess with packet contents */
1231
Al Viro02e60372006-11-03 00:28:23 -08001232__be16 ipx_cksum(struct ipxhdr *packet, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +09001234 /*
1235 * NOTE: sum is a net byte order quantity, which optimizes the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 * loop. This only works on big and little endian machines. (I
1237 * don't know of a machine that isn't.)
1238 */
Al Viro02e60372006-11-03 00:28:23 -08001239 /* handle the first 3 words separately; checksum should be skipped
1240 * and ipx_tctrl masked out */
1241 __u16 *p = (__u16 *)packet;
1242 __u32 sum = p[1] + (p[2] & (__force u16)htons(0x00ff));
1243 __u32 i = (length >> 1) - 3; /* Number of remaining complete words */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Al Viro02e60372006-11-03 00:28:23 -08001245 /* Loop through them */
1246 p += 3;
1247 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 sum += *p++;
1249
1250 /* Add on the last part word if it exists */
1251 if (packet->ipx_pktsize & htons(1))
Al Viro02e60372006-11-03 00:28:23 -08001252 sum += (__force u16)htons(0xff00) & *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 /* Do final fixup */
1255 sum = (sum & 0xffff) + (sum >> 16);
1256
1257 /* It's a pity there's no concept of carry in C */
1258 if (sum >= 0x10000)
1259 sum++;
1260
Al Viro02e60372006-11-03 00:28:23 -08001261 /*
1262 * Leave 0 alone; we don't want 0xffff here. Note that we can't get
1263 * here with 0x10000, so this check is the same as ((__u16)sum)
1264 */
1265 if (sum)
1266 sum = ~sum;
1267
1268 return (__force __be16)sum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Al Viro4833ed02006-11-03 00:27:06 -08001271const char *ipx_frame_name(__be16 frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 char* rc = "None";
1274
1275 switch (ntohs(frame)) {
1276 case ETH_P_IPX: rc = "EtherII"; break;
1277 case ETH_P_802_2: rc = "802.2"; break;
1278 case ETH_P_SNAP: rc = "SNAP"; break;
1279 case ETH_P_802_3: rc = "802.3"; break;
1280 case ETH_P_TR_802_2: rc = "802.2TR"; break;
1281 }
1282
1283 return rc;
1284}
1285
1286const char *ipx_device_name(struct ipx_interface *intrfc)
1287{
1288 return intrfc->if_internal ? "Internal" :
1289 intrfc->if_dev ? intrfc->if_dev->name : "Unknown";
1290}
1291
1292/* Handling for system calls applied via the various interfaces to an IPX
1293 * socket object. */
1294
1295static int ipx_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001296 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
1298 struct sock *sk = sock->sk;
1299 int opt;
1300 int rc = -EINVAL;
1301
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001302 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (optlen != sizeof(int))
1304 goto out;
1305
1306 rc = -EFAULT;
1307 if (get_user(opt, (unsigned int __user *)optval))
1308 goto out;
1309
1310 rc = -ENOPROTOOPT;
1311 if (!(level == SOL_IPX && optname == IPX_TYPE))
1312 goto out;
1313
1314 ipx_sk(sk)->type = opt;
1315 rc = 0;
1316out:
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001317 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return rc;
1319}
1320
1321static int ipx_getsockopt(struct socket *sock, int level, int optname,
1322 char __user *optval, int __user *optlen)
1323{
1324 struct sock *sk = sock->sk;
1325 int val = 0;
1326 int len;
1327 int rc = -ENOPROTOOPT;
1328
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001329 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (!(level == SOL_IPX && optname == IPX_TYPE))
1331 goto out;
1332
1333 val = ipx_sk(sk)->type;
1334
1335 rc = -EFAULT;
1336 if (get_user(len, optlen))
1337 goto out;
1338
1339 len = min_t(unsigned int, len, sizeof(int));
1340 rc = -EINVAL;
1341 if(len < 0)
1342 goto out;
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +09001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 rc = -EFAULT;
1345 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
1346 goto out;
1347
1348 rc = 0;
1349out:
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001350 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return rc;
1352}
1353
1354static struct proto ipx_proto = {
1355 .name = "IPX",
1356 .owner = THIS_MODULE,
1357 .obj_size = sizeof(struct ipx_sock),
1358};
1359
Eric Paris3f378b62009-11-05 22:18:14 -08001360static int ipx_create(struct net *net, struct socket *sock, int protocol,
1361 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 int rc = -ESOCKTNOSUPPORT;
1364 struct sock *sk;
1365
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001366 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001367 return -EAFNOSUPPORT;
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /*
1370 * SPX support is not anymore in the kernel sources. If you want to
1371 * ressurrect it, completing it and making it understand shared skbs,
1372 * be fully multithreaded, etc, grab the sources in an early 2.5 kernel
1373 * tree.
1374 */
1375 if (sock->type != SOCK_DGRAM)
1376 goto out;
1377
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +09001378 rc = -ENOMEM;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001379 sk = sk_alloc(net, PF_IPX, GFP_KERNEL, &ipx_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (!sk)
1381 goto out;
Pavel Emelyanovc2b42332007-11-10 21:39:26 -08001382
1383 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 sock_init_data(sock, sk);
1385 sk->sk_no_check = 1; /* Checksum off by default */
1386 sock->ops = &ipx_dgram_ops;
1387 rc = 0;
1388out:
1389 return rc;
1390}
1391
1392static int ipx_release(struct socket *sock)
1393{
1394 struct sock *sk = sock->sk;
1395
1396 if (!sk)
1397 goto out;
1398
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001399 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (!sock_flag(sk, SOCK_DEAD))
1401 sk->sk_state_change(sk);
1402
1403 sock_set_flag(sk, SOCK_DEAD);
1404 sock->sk = NULL;
Pavel Emelyanovc2b42332007-11-10 21:39:26 -08001405 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 ipx_destroy_socket(sk);
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001407 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408out:
1409 return 0;
1410}
1411
1412/* caller must hold a reference to intrfc */
1413
Al Viro4833ed02006-11-03 00:27:06 -08001414static __be16 ipx_first_free_socketnum(struct ipx_interface *intrfc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 unsigned short socketNum = intrfc->if_sknum;
1417
1418 spin_lock_bh(&intrfc->if_sklist_lock);
1419
1420 if (socketNum < IPX_MIN_EPHEMERAL_SOCKET)
1421 socketNum = IPX_MIN_EPHEMERAL_SOCKET;
1422
Al Viro4833ed02006-11-03 00:27:06 -08001423 while (__ipxitf_find_socket(intrfc, htons(socketNum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (socketNum > IPX_MAX_EPHEMERAL_SOCKET)
1425 socketNum = IPX_MIN_EPHEMERAL_SOCKET;
1426 else
1427 socketNum++;
1428
1429 spin_unlock_bh(&intrfc->if_sklist_lock);
1430 intrfc->if_sknum = socketNum;
1431
Al Viro4833ed02006-11-03 00:27:06 -08001432 return htons(socketNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001435static int __ipx_bind(struct socket *sock,
1436 struct sockaddr *uaddr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 struct sock *sk = sock->sk;
1439 struct ipx_sock *ipxs = ipx_sk(sk);
1440 struct ipx_interface *intrfc;
1441 struct sockaddr_ipx *addr = (struct sockaddr_ipx *)uaddr;
1442 int rc = -EINVAL;
1443
1444 if (!sock_flag(sk, SOCK_ZAPPED) || addr_len != sizeof(struct sockaddr_ipx))
1445 goto out;
1446
1447 intrfc = ipxitf_find_using_net(addr->sipx_network);
1448 rc = -EADDRNOTAVAIL;
1449 if (!intrfc)
1450 goto out;
1451
1452 if (!addr->sipx_port) {
1453 addr->sipx_port = ipx_first_free_socketnum(intrfc);
1454 rc = -EINVAL;
1455 if (!addr->sipx_port)
1456 goto out_put;
1457 }
1458
1459 /* protect IPX system stuff like routing/sap */
1460 rc = -EACCES;
1461 if (ntohs(addr->sipx_port) < IPX_MIN_EPHEMERAL_SOCKET &&
1462 !capable(CAP_NET_ADMIN))
1463 goto out_put;
1464
1465 ipxs->port = addr->sipx_port;
1466
1467#ifdef CONFIG_IPX_INTERN
1468 if (intrfc == ipx_internal_net) {
1469 /* The source address is to be set explicitly if the
1470 * socket is to be bound on the internal network. If a
1471 * node number 0 was specified, the default is used.
1472 */
1473
1474 rc = -EINVAL;
1475 if (!memcmp(addr->sipx_node, ipx_broadcast_node, IPX_NODE_LEN))
1476 goto out_put;
1477 if (!memcmp(addr->sipx_node, ipx_this_node, IPX_NODE_LEN))
1478 memcpy(ipxs->node, intrfc->if_node, IPX_NODE_LEN);
1479 else
1480 memcpy(ipxs->node, addr->sipx_node, IPX_NODE_LEN);
1481
1482 rc = -EADDRINUSE;
1483 if (ipxitf_find_internal_socket(intrfc, ipxs->node,
1484 ipxs->port)) {
1485 SOCK_DEBUG(sk,
1486 "IPX: bind failed because port %X in use.\n",
Al Viro4833ed02006-11-03 00:27:06 -08001487 ntohs(addr->sipx_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 goto out_put;
1489 }
1490 } else {
1491 /* Source addresses are easy. It must be our
1492 * network:node pair for an interface routed to IPX
1493 * with the ipx routing ioctl()
1494 */
1495
1496 memcpy(ipxs->node, intrfc->if_node, IPX_NODE_LEN);
1497
1498 rc = -EADDRINUSE;
1499 if (ipxitf_find_socket(intrfc, addr->sipx_port)) {
1500 SOCK_DEBUG(sk,
1501 "IPX: bind failed because port %X in use.\n",
Al Viro4833ed02006-11-03 00:27:06 -08001502 ntohs(addr->sipx_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 goto out_put;
1504 }
1505 }
1506
1507#else /* !def CONFIG_IPX_INTERN */
1508
1509 /* Source addresses are easy. It must be our network:node pair for
1510 an interface routed to IPX with the ipx routing ioctl() */
1511
1512 rc = -EADDRINUSE;
1513 if (ipxitf_find_socket(intrfc, addr->sipx_port)) {
1514 SOCK_DEBUG(sk, "IPX: bind failed because port %X in use.\n",
1515 ntohs((int)addr->sipx_port));
1516 goto out_put;
1517 }
1518
1519#endif /* CONFIG_IPX_INTERN */
1520
1521 ipxitf_insert_socket(intrfc, sk);
1522 sock_reset_flag(sk, SOCK_ZAPPED);
1523
1524 rc = 0;
1525out_put:
1526 ipxitf_put(intrfc);
1527out:
1528 return rc;
1529}
1530
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001531static int ipx_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1532{
1533 int rc;
1534
1535 lock_kernel();
1536 rc = __ipx_bind(sock, uaddr, addr_len);
1537 unlock_kernel();
1538
1539 return rc;
1540}
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542static int ipx_connect(struct socket *sock, struct sockaddr *uaddr,
1543 int addr_len, int flags)
1544{
1545 struct sock *sk = sock->sk;
1546 struct ipx_sock *ipxs = ipx_sk(sk);
1547 struct sockaddr_ipx *addr;
1548 int rc = -EINVAL;
1549 struct ipx_route *rt;
1550
1551 sk->sk_state = TCP_CLOSE;
1552 sock->state = SS_UNCONNECTED;
1553
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001554 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 if (addr_len != sizeof(*addr))
1556 goto out;
1557 addr = (struct sockaddr_ipx *)uaddr;
1558
1559 /* put the autobinding in */
1560 if (!ipxs->port) {
1561 struct sockaddr_ipx uaddr;
1562
1563 uaddr.sipx_port = 0;
1564 uaddr.sipx_network = 0;
1565
1566#ifdef CONFIG_IPX_INTERN
1567 rc = -ENETDOWN;
1568 if (!ipxs->intrfc)
1569 goto out; /* Someone zonked the iface */
1570 memcpy(uaddr.sipx_node, ipxs->intrfc->if_node,
1571 IPX_NODE_LEN);
1572#endif /* CONFIG_IPX_INTERN */
1573
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001574 rc = __ipx_bind(sock, (struct sockaddr *)&uaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 sizeof(struct sockaddr_ipx));
1576 if (rc)
1577 goto out;
1578 }
1579
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +09001580 /* We can either connect to primary network or somewhere
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 * we can route to */
1582 rt = ipxrtr_lookup(addr->sipx_network);
1583 rc = -ENETUNREACH;
1584 if (!rt && !(!addr->sipx_network && ipx_primary_net))
1585 goto out;
1586
1587 ipxs->dest_addr.net = addr->sipx_network;
1588 ipxs->dest_addr.sock = addr->sipx_port;
1589 memcpy(ipxs->dest_addr.node, addr->sipx_node, IPX_NODE_LEN);
1590 ipxs->type = addr->sipx_type;
1591
1592 if (sock->type == SOCK_DGRAM) {
1593 sock->state = SS_CONNECTED;
1594 sk->sk_state = TCP_ESTABLISHED;
1595 }
1596
1597 if (rt)
1598 ipxrtr_put(rt);
1599 rc = 0;
1600out:
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001601 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return rc;
1603}
1604
1605
1606static int ipx_getname(struct socket *sock, struct sockaddr *uaddr,
1607 int *uaddr_len, int peer)
1608{
1609 struct ipx_address *addr;
1610 struct sockaddr_ipx sipx;
1611 struct sock *sk = sock->sk;
1612 struct ipx_sock *ipxs = ipx_sk(sk);
1613 int rc;
1614
1615 *uaddr_len = sizeof(struct sockaddr_ipx);
1616
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001617 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (peer) {
1619 rc = -ENOTCONN;
1620 if (sk->sk_state != TCP_ESTABLISHED)
1621 goto out;
1622
1623 addr = &ipxs->dest_addr;
1624 sipx.sipx_network = addr->net;
1625 sipx.sipx_port = addr->sock;
1626 memcpy(sipx.sipx_node, addr->node, IPX_NODE_LEN);
1627 } else {
1628 if (ipxs->intrfc) {
1629 sipx.sipx_network = ipxs->intrfc->if_netnum;
1630#ifdef CONFIG_IPX_INTERN
1631 memcpy(sipx.sipx_node, ipxs->node, IPX_NODE_LEN);
1632#else
1633 memcpy(sipx.sipx_node, ipxs->intrfc->if_node,
1634 IPX_NODE_LEN);
1635#endif /* CONFIG_IPX_INTERN */
1636
1637 } else {
1638 sipx.sipx_network = 0;
1639 memset(sipx.sipx_node, '\0', IPX_NODE_LEN);
1640 }
1641
1642 sipx.sipx_port = ipxs->port;
1643 }
1644
1645 sipx.sipx_family = AF_IPX;
1646 sipx.sipx_type = ipxs->type;
1647 sipx.sipx_zero = 0;
1648 memcpy(uaddr, &sipx, sizeof(sipx));
1649
1650 rc = 0;
1651out:
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001652 unlock_kernel();
1653 return rc;
1654}
1655
1656static unsigned int ipx_datagram_poll(struct file *file, struct socket *sock,
1657 poll_table *wait)
1658{
1659 int rc;
1660
1661 lock_kernel();
1662 rc = datagram_poll(file, sock, wait);
1663 unlock_kernel();
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return rc;
1666}
1667
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001668static int ipx_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
1670 /* NULL here for pt means the packet was looped back */
1671 struct ipx_interface *intrfc;
1672 struct ipxhdr *ipx;
1673 u16 ipx_pktsize;
1674 int rc = 0;
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +09001675
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001676 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -07001677 goto drop;
1678
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +09001679 /* Not ours */
1680 if (skb->pkt_type == PACKET_OTHERHOST)
1681 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
1684 goto out;
1685
Stephen Hemminger7b1ba8d2006-08-08 16:48:51 -07001686 if (!pskb_may_pull(skb, sizeof(struct ipxhdr)))
1687 goto drop;
1688
David S. Millerfff64252006-08-09 17:36:15 -07001689 ipx_pktsize = ntohs(ipx_hdr(skb)->ipx_pktsize);
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +09001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 /* Too small or invalid header? */
Stephen Hemminger8b5cc5e2006-08-07 20:09:20 -07001692 if (ipx_pktsize < sizeof(struct ipxhdr) ||
1693 !pskb_may_pull(skb, ipx_pktsize))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 goto drop;
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +09001695
Stephen Hemminger7b1ba8d2006-08-08 16:48:51 -07001696 ipx = ipx_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (ipx->ipx_checksum != IPX_NO_CHECKSUM &&
1698 ipx->ipx_checksum != ipx_cksum(ipx, ipx_pktsize))
1699 goto drop;
1700
1701 IPX_SKB_CB(skb)->ipx_tctrl = ipx->ipx_tctrl;
1702 IPX_SKB_CB(skb)->ipx_dest_net = ipx->ipx_dest.net;
1703 IPX_SKB_CB(skb)->ipx_source_net = ipx->ipx_source.net;
1704
1705 /* Determine what local ipx endpoint this is */
1706 intrfc = ipxitf_find_using_phys(dev, pt->type);
1707 if (!intrfc) {
1708 if (ipxcfg_auto_create_interfaces &&
Al Viro4833ed02006-11-03 00:27:06 -08001709 IPX_SKB_CB(skb)->ipx_dest_net) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 intrfc = ipxitf_auto_create(dev, pt->type);
1711 if (intrfc)
1712 ipxitf_hold(intrfc);
1713 }
1714
1715 if (!intrfc) /* Not one of ours */
1716 /* or invalid packet for auto creation */
1717 goto drop;
1718 }
1719
1720 rc = ipxitf_rcv(intrfc, skb);
1721 ipxitf_put(intrfc);
1722 goto out;
1723drop:
1724 kfree_skb(skb);
1725out:
1726 return rc;
1727}
1728
1729static int ipx_sendmsg(struct kiocb *iocb, struct socket *sock,
1730 struct msghdr *msg, size_t len)
1731{
1732 struct sock *sk = sock->sk;
1733 struct ipx_sock *ipxs = ipx_sk(sk);
1734 struct sockaddr_ipx *usipx = (struct sockaddr_ipx *)msg->msg_name;
1735 struct sockaddr_ipx local_sipx;
1736 int rc = -EINVAL;
1737 int flags = msg->msg_flags;
1738
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001739 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 /* Socket gets bound below anyway */
1741/* if (sk->sk_zapped)
1742 return -EIO; */ /* Socket not bound */
1743 if (flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
1744 goto out;
1745
1746 /* Max possible packet size limited by 16 bit pktsize in header */
1747 if (len >= 65535 - sizeof(struct ipxhdr))
1748 goto out;
1749
1750 if (usipx) {
1751 if (!ipxs->port) {
1752 struct sockaddr_ipx uaddr;
1753
1754 uaddr.sipx_port = 0;
1755 uaddr.sipx_network = 0;
1756#ifdef CONFIG_IPX_INTERN
1757 rc = -ENETDOWN;
1758 if (!ipxs->intrfc)
1759 goto out; /* Someone zonked the iface */
1760 memcpy(uaddr.sipx_node, ipxs->intrfc->if_node,
1761 IPX_NODE_LEN);
1762#endif
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001763 rc = __ipx_bind(sock, (struct sockaddr *)&uaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 sizeof(struct sockaddr_ipx));
1765 if (rc)
1766 goto out;
1767 }
1768
1769 rc = -EINVAL;
1770 if (msg->msg_namelen < sizeof(*usipx) ||
1771 usipx->sipx_family != AF_IPX)
1772 goto out;
1773 } else {
1774 rc = -ENOTCONN;
1775 if (sk->sk_state != TCP_ESTABLISHED)
1776 goto out;
1777
1778 usipx = &local_sipx;
1779 usipx->sipx_family = AF_IPX;
1780 usipx->sipx_type = ipxs->type;
1781 usipx->sipx_port = ipxs->dest_addr.sock;
1782 usipx->sipx_network = ipxs->dest_addr.net;
1783 memcpy(usipx->sipx_node, ipxs->dest_addr.node, IPX_NODE_LEN);
1784 }
1785
1786 rc = ipxrtr_route_packet(sk, usipx, msg->msg_iov, len,
1787 flags & MSG_DONTWAIT);
1788 if (rc >= 0)
1789 rc = len;
1790out:
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001791 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return rc;
1793}
1794
1795
1796static int ipx_recvmsg(struct kiocb *iocb, struct socket *sock,
1797 struct msghdr *msg, size_t size, int flags)
1798{
1799 struct sock *sk = sock->sk;
1800 struct ipx_sock *ipxs = ipx_sk(sk);
1801 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)msg->msg_name;
1802 struct ipxhdr *ipx = NULL;
1803 struct sk_buff *skb;
1804 int copied, rc;
1805
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001806 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 /* put the autobinding in */
1808 if (!ipxs->port) {
1809 struct sockaddr_ipx uaddr;
1810
1811 uaddr.sipx_port = 0;
1812 uaddr.sipx_network = 0;
1813
1814#ifdef CONFIG_IPX_INTERN
1815 rc = -ENETDOWN;
1816 if (!ipxs->intrfc)
1817 goto out; /* Someone zonked the iface */
1818 memcpy(uaddr.sipx_node, ipxs->intrfc->if_node, IPX_NODE_LEN);
1819#endif /* CONFIG_IPX_INTERN */
1820
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001821 rc = __ipx_bind(sock, (struct sockaddr *)&uaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 sizeof(struct sockaddr_ipx));
1823 if (rc)
1824 goto out;
1825 }
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +09001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 rc = -ENOTCONN;
1828 if (sock_flag(sk, SOCK_ZAPPED))
1829 goto out;
1830
1831 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1832 flags & MSG_DONTWAIT, &rc);
1833 if (!skb)
1834 goto out;
1835
1836 ipx = ipx_hdr(skb);
1837 copied = ntohs(ipx->ipx_pktsize) - sizeof(struct ipxhdr);
1838 if (copied > size) {
1839 copied = size;
1840 msg->msg_flags |= MSG_TRUNC;
1841 }
1842
1843 rc = skb_copy_datagram_iovec(skb, sizeof(struct ipxhdr), msg->msg_iov,
1844 copied);
1845 if (rc)
1846 goto out_free;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07001847 if (skb->tstamp.tv64)
1848 sk->sk_stamp = skb->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 msg->msg_namelen = sizeof(*sipx);
1851
1852 if (sipx) {
1853 sipx->sipx_family = AF_IPX;
1854 sipx->sipx_port = ipx->ipx_source.sock;
1855 memcpy(sipx->sipx_node, ipx->ipx_source.node, IPX_NODE_LEN);
1856 sipx->sipx_network = IPX_SKB_CB(skb)->ipx_source_net;
1857 sipx->sipx_type = ipx->ipx_type;
1858 sipx->sipx_zero = 0;
1859 }
1860 rc = copied;
1861
1862out_free:
1863 skb_free_datagram(sk, skb);
1864out:
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001865 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return rc;
1867}
1868
1869
1870static int ipx_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1871{
1872 int rc = 0;
1873 long amount = 0;
1874 struct sock *sk = sock->sk;
1875 void __user *argp = (void __user *)arg;
1876
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001877 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 switch (cmd) {
1879 case TIOCOUTQ:
Eric Dumazet31e6d362009-06-17 19:05:41 -07001880 amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (amount < 0)
1882 amount = 0;
1883 rc = put_user(amount, (int __user *)argp);
1884 break;
1885 case TIOCINQ: {
1886 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
1887 /* These two are safe on a single CPU system as only
1888 * user tasks fiddle here */
1889 if (skb)
1890 amount = skb->len - sizeof(struct ipxhdr);
1891 rc = put_user(amount, (int __user *)argp);
1892 break;
1893 }
1894 case SIOCADDRT:
1895 case SIOCDELRT:
1896 rc = -EPERM;
1897 if (capable(CAP_NET_ADMIN))
1898 rc = ipxrtr_ioctl(cmd, argp);
1899 break;
1900 case SIOCSIFADDR:
1901 case SIOCAIPXITFCRT:
1902 case SIOCAIPXPRISLT:
1903 rc = -EPERM;
1904 if (!capable(CAP_NET_ADMIN))
1905 break;
1906 case SIOCGIFADDR:
1907 rc = ipxitf_ioctl(cmd, argp);
1908 break;
1909 case SIOCIPXCFGDATA:
1910 rc = ipxcfg_get_config_data(argp);
1911 break;
1912 case SIOCIPXNCPCONN:
1913 /*
1914 * This socket wants to take care of the NCP connection
1915 * handed to us in arg.
1916 */
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +09001917 rc = -EPERM;
1918 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 break;
1920 rc = get_user(ipx_sk(sk)->ipx_ncp_conn,
1921 (const unsigned short __user *)argp);
1922 break;
1923 case SIOCGSTAMP:
1924 rc = -EINVAL;
YOSHIFUJI Hideaki981c0ff2007-02-09 23:24:51 +09001925 if (sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 rc = sock_get_timestamp(sk, argp);
1927 break;
1928 case SIOCGIFDSTADDR:
1929 case SIOCSIFDSTADDR:
1930 case SIOCGIFBRDADDR:
1931 case SIOCSIFBRDADDR:
1932 case SIOCGIFNETMASK:
1933 case SIOCSIFNETMASK:
1934 rc = -EINVAL;
1935 break;
1936 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001937 rc = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 break;
1939 }
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001940 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 return rc;
1943}
1944
Petr Vandrovecf6c90b72006-03-27 23:39:31 -08001945
1946#ifdef CONFIG_COMPAT
1947static int ipx_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1948{
1949 /*
1950 * These 4 commands use same structure on 32bit and 64bit. Rest of IPX
1951 * commands is handled by generic ioctl code. As these commands are
1952 * SIOCPROTOPRIVATE..SIOCPROTOPRIVATE+3, they cannot be handled by generic
1953 * code.
1954 */
1955 switch (cmd) {
1956 case SIOCAIPXITFCRT:
1957 case SIOCAIPXPRISLT:
1958 case SIOCIPXCFGDATA:
1959 case SIOCIPXNCPCONN:
1960 return ipx_ioctl(sock, cmd, arg);
1961 default:
1962 return -ENOIOCTLCMD;
1963 }
1964}
1965#endif
1966
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968/*
1969 * Socket family declarations
1970 */
1971
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001972static const struct net_proto_family ipx_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 .family = PF_IPX,
1974 .create = ipx_create,
1975 .owner = THIS_MODULE,
1976};
1977
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001978static const struct proto_ops ipx_dgram_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 .family = PF_IPX,
1980 .owner = THIS_MODULE,
1981 .release = ipx_release,
1982 .bind = ipx_bind,
1983 .connect = ipx_connect,
1984 .socketpair = sock_no_socketpair,
1985 .accept = sock_no_accept,
1986 .getname = ipx_getname,
Arnd Bergmann83927ba2009-11-05 04:37:27 +00001987 .poll = ipx_datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 .ioctl = ipx_ioctl,
Petr Vandrovecf6c90b72006-03-27 23:39:31 -08001989#ifdef CONFIG_COMPAT
1990 .compat_ioctl = ipx_compat_ioctl,
1991#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 .listen = sock_no_listen,
1993 .shutdown = sock_no_shutdown, /* FIXME: support shutdown */
1994 .setsockopt = ipx_setsockopt,
1995 .getsockopt = ipx_getsockopt,
1996 .sendmsg = ipx_sendmsg,
1997 .recvmsg = ipx_recvmsg,
1998 .mmap = sock_no_mmap,
1999 .sendpage = sock_no_sendpage,
2000};
2001
Stephen Hemminger7546dd92009-03-09 08:18:29 +00002002static struct packet_type ipx_8023_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08002003 .type = cpu_to_be16(ETH_P_802_3),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 .func = ipx_rcv,
2005};
2006
Stephen Hemminger7546dd92009-03-09 08:18:29 +00002007static struct packet_type ipx_dix_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08002008 .type = cpu_to_be16(ETH_P_IPX),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 .func = ipx_rcv,
2010};
2011
2012static struct notifier_block ipx_dev_notifier = {
2013 .notifier_call = ipxitf_device_event,
2014};
2015
2016extern struct datalink_proto *make_EII_client(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017extern void destroy_EII_client(struct datalink_proto *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Stephen Hemmingerfa665cc2009-03-20 05:45:39 +00002019static const unsigned char ipx_8022_type = 0xE0;
2020static const unsigned char ipx_snap_id[5] = { 0x0, 0x0, 0x0, 0x81, 0x37 };
2021static const char ipx_EII_err_msg[] __initconst =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 KERN_CRIT "IPX: Unable to register with Ethernet II\n";
Stephen Hemmingerfa665cc2009-03-20 05:45:39 +00002023static const char ipx_8023_err_msg[] __initconst =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 KERN_CRIT "IPX: Unable to register with 802.3\n";
Stephen Hemmingerfa665cc2009-03-20 05:45:39 +00002025static const char ipx_llc_err_msg[] __initconst =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 KERN_CRIT "IPX: Unable to register with 802.2\n";
Stephen Hemmingerfa665cc2009-03-20 05:45:39 +00002027static const char ipx_snap_err_msg[] __initconst =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 KERN_CRIT "IPX: Unable to register with SNAP\n";
2029
2030static int __init ipx_init(void)
2031{
2032 int rc = proto_register(&ipx_proto, 1);
2033
2034 if (rc != 0)
2035 goto out;
2036
2037 sock_register(&ipx_family_ops);
2038
2039 pEII_datalink = make_EII_client();
2040 if (pEII_datalink)
2041 dev_add_pack(&ipx_dix_packet_type);
2042 else
2043 printk(ipx_EII_err_msg);
2044
2045 p8023_datalink = make_8023_client();
2046 if (p8023_datalink)
2047 dev_add_pack(&ipx_8023_packet_type);
2048 else
2049 printk(ipx_8023_err_msg);
2050
2051 p8022_datalink = register_8022_client(ipx_8022_type, ipx_rcv);
2052 if (!p8022_datalink)
2053 printk(ipx_llc_err_msg);
2054
2055 pSNAP_datalink = register_snap_client(ipx_snap_id, ipx_rcv);
2056 if (!pSNAP_datalink)
2057 printk(ipx_snap_err_msg);
2058
2059 register_netdevice_notifier(&ipx_dev_notifier);
2060 ipx_register_sysctl();
2061 ipx_proc_init();
2062out:
2063 return rc;
2064}
2065
2066static void __exit ipx_proto_finito(void)
2067{
2068 ipx_proc_exit();
2069 ipx_unregister_sysctl();
2070
2071 unregister_netdevice_notifier(&ipx_dev_notifier);
2072
2073 ipxitf_cleanup();
2074
Jiri Bohac1539b982007-02-08 16:02:21 -08002075 if (pSNAP_datalink) {
2076 unregister_snap_client(pSNAP_datalink);
2077 pSNAP_datalink = NULL;
2078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Jiri Bohac1539b982007-02-08 16:02:21 -08002080 if (p8022_datalink) {
2081 unregister_8022_client(p8022_datalink);
2082 p8022_datalink = NULL;
2083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 dev_remove_pack(&ipx_8023_packet_type);
Jiri Bohac1539b982007-02-08 16:02:21 -08002086 if (p8023_datalink) {
2087 destroy_8023_client(p8023_datalink);
2088 p8023_datalink = NULL;
2089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 dev_remove_pack(&ipx_dix_packet_type);
Jiri Bohac1539b982007-02-08 16:02:21 -08002092 if (pEII_datalink) {
2093 destroy_EII_client(pEII_datalink);
2094 pEII_datalink = NULL;
2095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 proto_unregister(&ipx_proto);
2098 sock_unregister(ipx_family_ops.family);
2099}
2100
2101module_init(ipx_init);
2102module_exit(ipx_proto_finito);
2103MODULE_LICENSE("GPL");
2104MODULE_ALIAS_NETPROTO(PF_IPX);