MIPS: Octeon: Add cn63XX to Octeon chip detection macros.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipx / af_ipx.c
blobda3d21c41d9064cb691342dfe23df37460766b84
1 /*
2 * Implements an IPX socket layer.
4 * This code is derived from work by
5 * Ross Biro : Writing the original IP stack
6 * Fred Van Kempen : Tidying up the TCP/IP
8 * Many thanks go to Keith Baker, Institute For Industrial Information
9 * Technology Ltd, Swansea University for allowing me to work on this
10 * in my own time even though it was in some ways related to commercial
11 * work I am currently employed to do there.
13 * All the material in this file is subject to the Gnu license version 2.
14 * Neither Alan Cox nor the Swansea University Computer Society admit
15 * liability nor provide warranty for any of this software. This material
16 * is provided as is and at no charge.
18 * 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.
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.
28 * See net/ipx/ChangeLog.
31 #include <linux/capability.h>
32 #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>
43 #include <linux/slab.h>
44 #include <linux/skbuff.h>
45 #include <linux/smp_lock.h>
46 #include <linux/socket.h>
47 #include <linux/sockios.h>
48 #include <linux/string.h>
49 #include <linux/types.h>
50 #include <linux/termios.h>
52 #include <net/ipx.h>
53 #include <net/p8022.h>
54 #include <net/psnap.h>
55 #include <net/sock.h>
56 #include <net/tcp_states.h>
58 #include <asm/uaccess.h>
60 #ifdef CONFIG_SYSCTL
61 extern void ipx_register_sysctl(void);
62 extern void ipx_unregister_sysctl(void);
63 #else
64 #define ipx_register_sysctl()
65 #define ipx_unregister_sysctl()
66 #endif
68 /* Configuration Variables */
69 static unsigned char ipxcfg_max_hops = 16;
70 static char ipxcfg_auto_select_primary;
71 static char ipxcfg_auto_create_interfaces;
72 int sysctl_ipx_pprop_broadcasting = 1;
74 /* Global Variables */
75 static struct datalink_proto *p8022_datalink;
76 static struct datalink_proto *pEII_datalink;
77 static struct datalink_proto *p8023_datalink;
78 static struct datalink_proto *pSNAP_datalink;
80 static const struct proto_ops ipx_dgram_ops;
82 LIST_HEAD(ipx_interfaces);
83 DEFINE_SPINLOCK(ipx_interfaces_lock);
85 struct ipx_interface *ipx_primary_net;
86 struct ipx_interface *ipx_internal_net;
88 extern int ipxrtr_add_route(__be32 network, struct ipx_interface *intrfc,
89 unsigned char *node);
90 extern void ipxrtr_del_routes(struct ipx_interface *intrfc);
91 extern int ipxrtr_route_packet(struct sock *sk, struct sockaddr_ipx *usipx,
92 struct iovec *iov, size_t len, int noblock);
93 extern int ipxrtr_route_skb(struct sk_buff *skb);
94 extern struct ipx_route *ipxrtr_lookup(__be32 net);
95 extern int ipxrtr_ioctl(unsigned int cmd, void __user *arg);
97 struct ipx_interface *ipx_interfaces_head(void)
99 struct ipx_interface *rc = NULL;
101 if (!list_empty(&ipx_interfaces))
102 rc = list_entry(ipx_interfaces.next,
103 struct ipx_interface, node);
104 return rc;
107 static void ipxcfg_set_auto_select(char val)
109 ipxcfg_auto_select_primary = val;
110 if (val && !ipx_primary_net)
111 ipx_primary_net = ipx_interfaces_head();
114 static int ipxcfg_get_config_data(struct ipx_config_data __user *arg)
116 struct ipx_config_data vals;
118 vals.ipxcfg_auto_create_interfaces = ipxcfg_auto_create_interfaces;
119 vals.ipxcfg_auto_select_primary = ipxcfg_auto_select_primary;
121 return copy_to_user(arg, &vals, sizeof(vals)) ? -EFAULT : 0;
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.
130 static void ipx_remove_socket(struct sock *sk)
132 /* Determine interface with which socket is associated */
133 struct ipx_interface *intrfc = ipx_sk(sk)->intrfc;
135 if (!intrfc)
136 goto out;
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);
143 out:
144 return;
147 static void ipx_destroy_socket(struct sock *sk)
149 ipx_remove_socket(sk);
150 skb_queue_purge(&sk->sk_receive_queue);
151 sk_refcnt_debug_dec(sk);
152 sock_put(sk);
156 * The following code is used to support IPX Interfaces (IPXITF). An
157 * IPX interface is defined by a physical device and a frame type.
160 /* ipxitf_clear_primary_net has to be called with ipx_interfaces_lock held */
162 static void ipxitf_clear_primary_net(void)
164 ipx_primary_net = NULL;
165 if (ipxcfg_auto_select_primary)
166 ipx_primary_net = ipx_interfaces_head();
169 static struct ipx_interface *__ipxitf_find_using_phys(struct net_device *dev,
170 __be16 datalink)
172 struct ipx_interface *i;
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;
178 out:
179 return i;
182 static struct ipx_interface *ipxitf_find_using_phys(struct net_device *dev,
183 __be16 datalink)
185 struct ipx_interface *i;
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;
195 struct ipx_interface *ipxitf_find_using_net(__be32 net)
197 struct ipx_interface *i;
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;
208 i = ipx_primary_net;
209 if (i)
210 hold:
211 ipxitf_hold(i);
212 unlock:
213 spin_unlock_bh(&ipx_interfaces_lock);
214 return i;
217 /* Sockets are bound to a particular IPX interface. */
218 static void ipxitf_insert_socket(struct ipx_interface *intrfc, struct sock *sk)
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);
228 /* caller must hold intrfc->if_sklist_lock */
229 static struct sock *__ipxitf_find_socket(struct ipx_interface *intrfc,
230 __be16 port)
232 struct sock *s;
233 struct hlist_node *node;
235 sk_for_each(s, node, &intrfc->if_sklist)
236 if (ipx_sk(s)->port == port)
237 goto found;
238 s = NULL;
239 found:
240 return s;
243 /* caller must hold a reference to intrfc */
244 static struct sock *ipxitf_find_socket(struct ipx_interface *intrfc,
245 __be16 port)
247 struct sock *s;
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);
255 return s;
258 #ifdef CONFIG_IPX_INTERN
259 static struct sock *ipxitf_find_internal_socket(struct ipx_interface *intrfc,
260 unsigned char *ipx_node,
261 __be16 port)
263 struct sock *s;
264 struct hlist_node *node;
266 ipxitf_hold(intrfc);
267 spin_lock_bh(&intrfc->if_sklist_lock);
269 sk_for_each(s, node, &intrfc->if_sklist) {
270 struct ipx_sock *ipxs = ipx_sk(s);
272 if (ipxs->port == port &&
273 !memcmp(ipx_node, ipxs->node, IPX_NODE_LEN))
274 goto found;
276 s = NULL;
277 found:
278 spin_unlock_bh(&intrfc->if_sklist_lock);
279 ipxitf_put(intrfc);
280 return s;
282 #endif
284 static void __ipxitf_down(struct ipx_interface *intrfc)
286 struct sock *s;
287 struct hlist_node *node, *t;
289 /* Delete all routes associated with this interface */
290 ipxrtr_del_routes(intrfc);
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);
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);
304 INIT_HLIST_HEAD(&intrfc->if_sklist);
305 spin_unlock_bh(&intrfc->if_sklist_lock);
307 /* remove this interface from list */
308 list_del(&intrfc->node);
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;
316 if (intrfc->if_dev)
317 dev_put(intrfc->if_dev);
318 kfree(intrfc);
321 void ipxitf_down(struct ipx_interface *intrfc)
323 spin_lock_bh(&ipx_interfaces_lock);
324 __ipxitf_down(intrfc);
325 spin_unlock_bh(&ipx_interfaces_lock);
328 static __inline__ void __ipxitf_put(struct ipx_interface *intrfc)
330 if (atomic_dec_and_test(&intrfc->refcnt))
331 __ipxitf_down(intrfc);
334 static int ipxitf_device_event(struct notifier_block *notifier,
335 unsigned long event, void *ptr)
337 struct net_device *dev = ptr;
338 struct ipx_interface *i, *tmp;
340 if (!net_eq(dev_net(dev), &init_net))
341 return NOTIFY_DONE;
343 if (event != NETDEV_DOWN && event != NETDEV_UP)
344 goto out;
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);
354 spin_unlock_bh(&ipx_interfaces_lock);
355 out:
356 return NOTIFY_DONE;
360 static __exit void ipxitf_cleanup(void)
362 struct ipx_interface *i, *tmp;
364 spin_lock_bh(&ipx_interfaces_lock);
365 list_for_each_entry_safe(i, tmp, &ipx_interfaces, node)
366 __ipxitf_put(i);
367 spin_unlock_bh(&ipx_interfaces_lock);
370 static void ipxitf_def_skb_handler(struct sock *sock, struct sk_buff *skb)
372 if (sock_queue_rcv_skb(sock, skb) < 0)
373 kfree_skb(skb);
377 * On input skb->sk is NULL. Nobody is charged for the memory.
380 /* caller must hold a reference to intrfc */
382 #ifdef CONFIG_IPX_INTERN
383 static int ipxitf_demux_socket(struct ipx_interface *intrfc,
384 struct sk_buff *skb, int copy)
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;
393 spin_lock_bh(&intrfc->if_sklist_lock);
395 sk_for_each(s, node, &intrfc->if_sklist) {
396 struct ipx_sock *ipxs = ipx_sk(s);
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;
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 */
413 ipxitf_def_skb_handler(s, skb1);
415 /* On an external interface, one socket can listen */
416 if (intrfc != ipx_internal_net)
417 break;
421 /* skb was solely for us, and we did not make a copy, so free it. */
422 if (!copy)
423 kfree_skb(skb);
425 rc = 0;
426 out:
427 spin_unlock_bh(&intrfc->if_sklist_lock);
428 return rc;
430 #else
431 static struct sock *ncp_connection_hack(struct ipx_interface *intrfc,
432 struct ipxhdr *ipx)
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.
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;
442 int connection = 0;
443 u8 *ncphdr = (u8 *)(ipx + 1);
445 if (*ncphdr == 0x22 && *(ncphdr + 1) == 0x22) /* NCP request */
446 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);
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;
461 sk = NULL;
462 found:
463 spin_unlock_bh(&intrfc->if_sklist_lock);
465 return sk;
468 static int ipxitf_demux_socket(struct ipx_interface *intrfc,
469 struct sk_buff *skb, int copy)
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;
476 if (intrfc == ipx_primary_net && ntohs(ipx->ipx_dest.sock) == 0x451)
477 sock1 = ncp_connection_hack(intrfc, ipx);
478 if (!sock1)
479 /* No special socket found, forward the packet the normal way */
480 sock1 = ipxitf_find_socket(intrfc, ipx->ipx_dest.sock);
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).
490 if (ipx_primary_net && intrfc != ipx_primary_net) {
491 const int dsock = ntohs(ipx->ipx_dest.sock);
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);
503 * If there is nothing to do return. The kfree will cancel any charging.
505 rc = 0;
506 if (!sock1 && !sock2) {
507 if (!copy)
508 kfree_skb(skb);
509 goto out;
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.
520 if (copy)
521 skb1 = skb_clone(skb, GFP_ATOMIC);
522 else
523 skb1 = skb;
525 rc = -ENOMEM;
526 if (!skb1)
527 goto out_put;
529 /* Do we need 2 SKBs? */
530 if (sock1 && sock2)
531 skb2 = skb_clone(skb1, GFP_ATOMIC);
532 else
533 skb2 = skb1;
535 if (sock1)
536 ipxitf_def_skb_handler(sock1, skb1);
538 if (!skb2)
539 goto out_put;
541 if (sock2)
542 ipxitf_def_skb_handler(sock2, skb2);
544 rc = 0;
545 out_put:
546 if (sock1)
547 sock_put(sock1);
548 if (sock2)
549 sock_put(sock2);
550 out:
551 return rc;
553 #endif /* CONFIG_IPX_INTERN */
555 static struct sk_buff *ipxitf_adjust_skbuff(struct ipx_interface *intrfc,
556 struct sk_buff *skb)
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;
563 /* Hopefully, most cases */
564 if (in_offset >= out_offset)
565 return skb;
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);
572 skb_reset_network_header(skb2);
573 skb_reset_transport_header(skb2);
574 skb_put(skb2, skb->len);
575 memcpy(ipx_hdr(skb2), ipx_hdr(skb), skb->len);
576 memcpy(skb2->cb, skb->cb, sizeof(skb->cb));
578 kfree_skb(skb);
579 return skb2;
582 /* caller must hold a reference to intrfc and the skb has to be unshared */
583 int ipxitf_send(struct ipx_interface *intrfc, struct sk_buff *skb, char *node)
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;
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;
596 /* see if we need to include the netnum in the route list */
597 if (IPX_SKB_CB(skb)->last_hop.index >= 0) {
598 __be32 *last_hop = (__be32 *)(((u8 *) skb->data) +
599 sizeof(struct ipxhdr) +
600 IPX_SKB_CB(skb)->last_hop.index *
601 sizeof(__be32));
602 *last_hop = IPX_SKB_CB(skb)->last_hop.netnum;
603 IPX_SKB_CB(skb)->last_hop.index = -1;
607 * We need to know how many skbuffs it will take to send out this
608 * packet to avoid unnecessary copies.
611 if (!dl || !dev || dev->flags & IFF_LOOPBACK)
612 send_to_wire = 0; /* No non looped */
615 * See if this should be demuxed to sockets on this interface
617 * We want to ensure the original was eaten or that we only use
618 * up clones.
621 if (ipx->ipx_dest.net == intrfc->if_netnum) {
623 * To our own node, loop and free the original.
624 * The internal net will receive on all node address.
626 if (intrfc == ipx_internal_net ||
627 !memcmp(intrfc->if_node, node, IPX_NODE_LEN)) {
628 /* Don't charge sender */
629 skb_orphan(skb);
631 /* Will charge receiver */
632 return ipxitf_demux_socket(intrfc, skb, 0);
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;
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.
650 if (ipx->ipx_source.net != intrfc->if_netnum) {
652 * Unshare the buffer before modifying the count in
653 * case it's a flood or tcpdump
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;
662 if (!send_to_wire) {
663 kfree_skb(skb);
664 goto out;
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);
674 /* Make any compensation for differing physical/data link size */
675 skb = ipxitf_adjust_skbuff(intrfc, skb);
676 if (!skb)
677 goto out;
679 /* set up data link and physical headers */
680 skb->dev = dev;
681 skb->protocol = htons(ETH_P_IPX);
683 /* Send it out */
684 dl->request(dl, skb, dest_node);
685 out:
686 return 0;
689 static int ipxitf_add_local_route(struct ipx_interface *intrfc)
691 return ipxrtr_add_route(intrfc->if_netnum, intrfc, NULL);
694 static void ipxitf_discover_netnum(struct ipx_interface *intrfc,
695 struct sk_buff *skb);
696 static int ipxitf_pprop(struct ipx_interface *intrfc, struct sk_buff *skb);
698 static int ipxitf_rcv(struct ipx_interface *intrfc, struct sk_buff *skb)
700 struct ipxhdr *ipx = ipx_hdr(skb);
701 int rc = 0;
703 ipxitf_hold(intrfc);
705 /* See if we should update our network number */
706 if (!intrfc->if_netnum) /* net number of intrfc not known yet */
707 ipxitf_discover_netnum(intrfc, skb);
709 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;
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;
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;
734 goto out_free_skb;
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;
744 /* we couldn't pawn it off so unload it */
745 out_free_skb:
746 kfree_skb(skb);
747 out_intrfc:
748 ipxitf_put(intrfc);
749 return rc;
752 static void ipxitf_discover_netnum(struct ipx_interface *intrfc,
753 struct sk_buff *skb)
755 const struct ipx_cb *cb = IPX_SKB_CB(skb);
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",
770 (unsigned long) ntohl(cb->ipx_source_net),
771 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);
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
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.
792 * If it is valid, check if we have pprop broadcasting enabled by the user,
793 * if not, just return zero for local processing.
795 * If it is enabled check the packet and don't broadcast it if we have already
796 * seen this packet.
798 * Broadcast: send it to the interfaces that aren't on the packet visited nets
799 * array, just after the IPX header.
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.
804 static int ipxitf_pprop(struct ipx_interface *intrfc, struct sk_buff *skb)
806 struct ipxhdr *ipx = ipx_hdr(skb);
807 int i, rc = -EINVAL;
808 struct ipx_interface *ifcs;
809 char *c;
810 __be32 *l;
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) +
818 IPX_MAX_PPROP_HOPS * sizeof(u32))
819 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;
830 c = ((u8 *) ipx) + sizeof(struct ipxhdr);
831 l = (__be32 *) c;
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;
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;
850 /* That aren't in the list */
851 if (ifcs == intrfc)
852 continue;
853 l = (__be32 *) c;
854 /* 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);
862 if (s) {
863 IPX_SKB_CB(s)->ipx_dest_net = ifcs->if_netnum;
864 ipxrtr_route_skb(s);
868 spin_unlock_bh(&ipx_interfaces_lock);
869 out:
870 return rc;
873 static void ipxitf_insert(struct ipx_interface *intrfc)
875 spin_lock_bh(&ipx_interfaces_lock);
876 list_add_tail(&intrfc->node, &ipx_interfaces);
877 spin_unlock_bh(&ipx_interfaces_lock);
879 if (ipxcfg_auto_select_primary && !ipx_primary_net)
880 ipx_primary_net = intrfc;
883 static struct ipx_interface *ipxitf_alloc(struct net_device *dev, __be32 netnum,
884 __be16 dlink_type,
885 struct datalink_proto *dlink,
886 unsigned char internal,
887 int ipx_offset)
889 struct ipx_interface *intrfc = kmalloc(sizeof(*intrfc), GFP_ATOMIC);
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);
904 return intrfc;
907 static int ipxitf_create_internal(struct ipx_interface_definition *idef)
909 struct ipx_interface *intrfc;
910 int rc = -EEXIST;
912 /* Only one primary network allowed */
913 if (ipx_primary_net)
914 goto out;
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;
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);
935 rc = ipxitf_add_local_route(intrfc);
936 ipxitf_put(intrfc);
937 out:
938 return rc;
941 static __be16 ipx_map_frame_type(unsigned char type)
943 __be16 rc = 0;
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;
952 return rc;
955 static int ipxitf_create(struct ipx_interface_definition *idef)
957 struct net_device *dev;
958 __be16 dlink_type = 0;
959 struct datalink_proto *datalink = NULL;
960 struct ipx_interface *intrfc;
961 int rc;
963 if (idef->ipx_special == IPX_INTERNAL) {
964 rc = ipxitf_create_internal(idef);
965 goto out;
968 rc = -EEXIST;
969 if (idef->ipx_special == IPX_PRIMARY && ipx_primary_net)
970 goto out;
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;
979 if (intrfc)
980 ipxitf_put(intrfc);
982 dev = dev_get_by_name(&init_net, idef->ipx_device);
983 rc = -ENODEV;
984 if (!dev)
985 goto out;
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;
1001 } else
1002 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;
1020 rc = -ENETDOWN;
1021 if (!(dev->flags & IFF_UP))
1022 goto out_dev;
1024 /* Check addresses are suitable */
1025 rc = -EINVAL;
1026 if (dev->addr_len > IPX_NODE_LEN)
1027 goto out_dev;
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);
1053 /* If the network number is known, add a route */
1054 rc = 0;
1055 if (!intrfc->if_netnum)
1056 goto out_intrfc;
1058 rc = ipxitf_add_local_route(intrfc);
1059 out_intrfc:
1060 ipxitf_put(intrfc);
1061 goto out;
1062 out_dev:
1063 dev_put(dev);
1064 out:
1065 return rc;
1068 static int ipxitf_delete(struct ipx_interface_definition *idef)
1070 struct net_device *dev = NULL;
1071 __be16 dlink_type = 0;
1072 struct ipx_interface *intrfc;
1073 int rc = 0;
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;
1081 rc = -ENOENT;
1082 goto out;
1085 dlink_type = ipx_map_frame_type(idef->ipx_dlink_type);
1086 rc = -EPROTONOSUPPORT;
1087 if (!dlink_type)
1088 goto out;
1090 dev = __dev_get_by_name(&init_net, idef->ipx_device);
1091 rc = -ENODEV;
1092 if (!dev)
1093 goto out;
1095 intrfc = __ipxitf_find_using_phys(dev, dlink_type);
1096 rc = -EINVAL;
1097 if (!intrfc)
1098 goto out;
1099 __ipxitf_put(intrfc);
1101 rc = 0;
1102 out:
1103 spin_unlock_bh(&ipx_interfaces_lock);
1104 return rc;
1107 static struct ipx_interface *ipxitf_auto_create(struct net_device *dev,
1108 __be16 dlink_type)
1110 struct ipx_interface *intrfc = NULL;
1111 struct datalink_proto *datalink;
1113 if (!dev)
1114 goto out;
1116 /* Check addresses are suitable */
1117 if (dev->addr_len > IPX_NODE_LEN)
1118 goto out;
1120 switch (ntohs(dlink_type)) {
1121 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;
1128 intrfc = ipxitf_alloc(dev, 0, dlink_type, datalink, 0,
1129 dev->hard_header_len + datalink->header_length);
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);
1141 out:
1142 return intrfc;
1145 static int ipxitf_ioctl(unsigned int cmd, void __user *arg)
1147 int rc = -EINVAL;
1148 struct ifreq ifr;
1149 int val;
1151 switch (cmd) {
1152 case SIOCSIFADDR: {
1153 struct sockaddr_ipx *sipx;
1154 struct ipx_interface_definition f;
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;
1170 if (sipx->sipx_action == IPX_DLTITF)
1171 rc = ipxitf_delete(&f);
1172 else
1173 rc = ipxitf_create(&f);
1174 break;
1176 case SIOCGIFADDR: {
1177 struct sockaddr_ipx *sipx;
1178 struct ipx_interface *ipxif;
1179 struct net_device *dev;
1181 rc = -EFAULT;
1182 if (copy_from_user(&ifr, arg, sizeof(ifr)))
1183 break;
1184 sipx = (struct sockaddr_ipx *)&ifr.ifr_addr;
1185 dev = __dev_get_by_name(&init_net, ifr.ifr_name);
1186 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;
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;
1206 case SIOCAIPXITFCRT:
1207 rc = -EFAULT;
1208 if (get_user(val, (unsigned char __user *) arg))
1209 break;
1210 rc = 0;
1211 ipxcfg_auto_create_interfaces = val;
1212 break;
1213 case SIOCAIPXPRISLT:
1214 rc = -EFAULT;
1215 if (get_user(val, (unsigned char __user *) arg))
1216 break;
1217 rc = 0;
1218 ipxcfg_set_auto_select(val);
1219 break;
1222 return rc;
1226 * Checksum routine for IPX
1229 /* Note: We assume ipx_tctrl==0 and htons(length)==ipx_pktsize */
1230 /* This functions should *not* mess with packet contents */
1232 __be16 ipx_cksum(struct ipxhdr *packet, int length)
1235 * NOTE: sum is a net byte order quantity, which optimizes the
1236 * loop. This only works on big and little endian machines. (I
1237 * don't know of a machine that isn't.)
1239 /* 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 */
1245 /* Loop through them */
1246 p += 3;
1247 while (i--)
1248 sum += *p++;
1250 /* Add on the last part word if it exists */
1251 if (packet->ipx_pktsize & htons(1))
1252 sum += (__force u16)htons(0xff00) & *p;
1254 /* Do final fixup */
1255 sum = (sum & 0xffff) + (sum >> 16);
1257 /* It's a pity there's no concept of carry in C */
1258 if (sum >= 0x10000)
1259 sum++;
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)
1265 if (sum)
1266 sum = ~sum;
1268 return (__force __be16)sum;
1271 const char *ipx_frame_name(__be16 frame)
1273 char* rc = "None";
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;
1283 return rc;
1286 const char *ipx_device_name(struct ipx_interface *intrfc)
1288 return intrfc->if_internal ? "Internal" :
1289 intrfc->if_dev ? intrfc->if_dev->name : "Unknown";
1292 /* Handling for system calls applied via the various interfaces to an IPX
1293 * socket object. */
1295 static int ipx_setsockopt(struct socket *sock, int level, int optname,
1296 char __user *optval, unsigned int optlen)
1298 struct sock *sk = sock->sk;
1299 int opt;
1300 int rc = -EINVAL;
1302 lock_kernel();
1303 if (optlen != sizeof(int))
1304 goto out;
1306 rc = -EFAULT;
1307 if (get_user(opt, (unsigned int __user *)optval))
1308 goto out;
1310 rc = -ENOPROTOOPT;
1311 if (!(level == SOL_IPX && optname == IPX_TYPE))
1312 goto out;
1314 ipx_sk(sk)->type = opt;
1315 rc = 0;
1316 out:
1317 unlock_kernel();
1318 return rc;
1321 static int ipx_getsockopt(struct socket *sock, int level, int optname,
1322 char __user *optval, int __user *optlen)
1324 struct sock *sk = sock->sk;
1325 int val = 0;
1326 int len;
1327 int rc = -ENOPROTOOPT;
1329 lock_kernel();
1330 if (!(level == SOL_IPX && optname == IPX_TYPE))
1331 goto out;
1333 val = ipx_sk(sk)->type;
1335 rc = -EFAULT;
1336 if (get_user(len, optlen))
1337 goto out;
1339 len = min_t(unsigned int, len, sizeof(int));
1340 rc = -EINVAL;
1341 if(len < 0)
1342 goto out;
1344 rc = -EFAULT;
1345 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
1346 goto out;
1348 rc = 0;
1349 out:
1350 unlock_kernel();
1351 return rc;
1354 static struct proto ipx_proto = {
1355 .name = "IPX",
1356 .owner = THIS_MODULE,
1357 .obj_size = sizeof(struct ipx_sock),
1360 static int ipx_create(struct net *net, struct socket *sock, int protocol,
1361 int kern)
1363 int rc = -ESOCKTNOSUPPORT;
1364 struct sock *sk;
1366 if (!net_eq(net, &init_net))
1367 return -EAFNOSUPPORT;
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.
1375 if (sock->type != SOCK_DGRAM)
1376 goto out;
1378 rc = -ENOMEM;
1379 sk = sk_alloc(net, PF_IPX, GFP_KERNEL, &ipx_proto);
1380 if (!sk)
1381 goto out;
1383 sk_refcnt_debug_inc(sk);
1384 sock_init_data(sock, sk);
1385 sk->sk_no_check = 1; /* Checksum off by default */
1386 sock->ops = &ipx_dgram_ops;
1387 rc = 0;
1388 out:
1389 return rc;
1392 static int ipx_release(struct socket *sock)
1394 struct sock *sk = sock->sk;
1396 if (!sk)
1397 goto out;
1399 lock_kernel();
1400 if (!sock_flag(sk, SOCK_DEAD))
1401 sk->sk_state_change(sk);
1403 sock_set_flag(sk, SOCK_DEAD);
1404 sock->sk = NULL;
1405 sk_refcnt_debug_release(sk);
1406 ipx_destroy_socket(sk);
1407 unlock_kernel();
1408 out:
1409 return 0;
1412 /* caller must hold a reference to intrfc */
1414 static __be16 ipx_first_free_socketnum(struct ipx_interface *intrfc)
1416 unsigned short socketNum = intrfc->if_sknum;
1418 spin_lock_bh(&intrfc->if_sklist_lock);
1420 if (socketNum < IPX_MIN_EPHEMERAL_SOCKET)
1421 socketNum = IPX_MIN_EPHEMERAL_SOCKET;
1423 while (__ipxitf_find_socket(intrfc, htons(socketNum)))
1424 if (socketNum > IPX_MAX_EPHEMERAL_SOCKET)
1425 socketNum = IPX_MIN_EPHEMERAL_SOCKET;
1426 else
1427 socketNum++;
1429 spin_unlock_bh(&intrfc->if_sklist_lock);
1430 intrfc->if_sknum = socketNum;
1432 return htons(socketNum);
1435 static int __ipx_bind(struct socket *sock,
1436 struct sockaddr *uaddr, int addr_len)
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;
1444 if (!sock_flag(sk, SOCK_ZAPPED) || addr_len != sizeof(struct sockaddr_ipx))
1445 goto out;
1447 intrfc = ipxitf_find_using_net(addr->sipx_network);
1448 rc = -EADDRNOTAVAIL;
1449 if (!intrfc)
1450 goto out;
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;
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;
1465 ipxs->port = addr->sipx_port;
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.
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);
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",
1487 ntohs(addr->sipx_port));
1488 goto out_put;
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()
1496 memcpy(ipxs->node, intrfc->if_node, IPX_NODE_LEN);
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",
1502 ntohs(addr->sipx_port));
1503 goto out_put;
1507 #else /* !def CONFIG_IPX_INTERN */
1509 /* Source addresses are easy. It must be our network:node pair for
1510 an interface routed to IPX with the ipx routing ioctl() */
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;
1519 #endif /* CONFIG_IPX_INTERN */
1521 ipxitf_insert_socket(intrfc, sk);
1522 sock_reset_flag(sk, SOCK_ZAPPED);
1524 rc = 0;
1525 out_put:
1526 ipxitf_put(intrfc);
1527 out:
1528 return rc;
1531 static int ipx_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1533 int rc;
1535 lock_kernel();
1536 rc = __ipx_bind(sock, uaddr, addr_len);
1537 unlock_kernel();
1539 return rc;
1542 static int ipx_connect(struct socket *sock, struct sockaddr *uaddr,
1543 int addr_len, int flags)
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;
1551 sk->sk_state = TCP_CLOSE;
1552 sock->state = SS_UNCONNECTED;
1554 lock_kernel();
1555 if (addr_len != sizeof(*addr))
1556 goto out;
1557 addr = (struct sockaddr_ipx *)uaddr;
1559 /* put the autobinding in */
1560 if (!ipxs->port) {
1561 struct sockaddr_ipx uaddr;
1563 uaddr.sipx_port = 0;
1564 uaddr.sipx_network = 0;
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 */
1574 rc = __ipx_bind(sock, (struct sockaddr *)&uaddr,
1575 sizeof(struct sockaddr_ipx));
1576 if (rc)
1577 goto out;
1580 /* We can either connect to primary network or somewhere
1581 * 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;
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;
1592 if (sock->type == SOCK_DGRAM) {
1593 sock->state = SS_CONNECTED;
1594 sk->sk_state = TCP_ESTABLISHED;
1597 if (rt)
1598 ipxrtr_put(rt);
1599 rc = 0;
1600 out:
1601 unlock_kernel();
1602 return rc;
1606 static int ipx_getname(struct socket *sock, struct sockaddr *uaddr,
1607 int *uaddr_len, int peer)
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;
1615 *uaddr_len = sizeof(struct sockaddr_ipx);
1617 lock_kernel();
1618 if (peer) {
1619 rc = -ENOTCONN;
1620 if (sk->sk_state != TCP_ESTABLISHED)
1621 goto out;
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 */
1637 } else {
1638 sipx.sipx_network = 0;
1639 memset(sipx.sipx_node, '\0', IPX_NODE_LEN);
1642 sipx.sipx_port = ipxs->port;
1645 sipx.sipx_family = AF_IPX;
1646 sipx.sipx_type = ipxs->type;
1647 sipx.sipx_zero = 0;
1648 memcpy(uaddr, &sipx, sizeof(sipx));
1650 rc = 0;
1651 out:
1652 unlock_kernel();
1653 return rc;
1656 static unsigned int ipx_datagram_poll(struct file *file, struct socket *sock,
1657 poll_table *wait)
1659 int rc;
1661 lock_kernel();
1662 rc = datagram_poll(file, sock, wait);
1663 unlock_kernel();
1665 return rc;
1668 static int ipx_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
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;
1676 if (!net_eq(dev_net(dev), &init_net))
1677 goto drop;
1679 /* Not ours */
1680 if (skb->pkt_type == PACKET_OTHERHOST)
1681 goto drop;
1683 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
1684 goto out;
1686 if (!pskb_may_pull(skb, sizeof(struct ipxhdr)))
1687 goto drop;
1689 ipx_pktsize = ntohs(ipx_hdr(skb)->ipx_pktsize);
1691 /* Too small or invalid header? */
1692 if (ipx_pktsize < sizeof(struct ipxhdr) ||
1693 !pskb_may_pull(skb, ipx_pktsize))
1694 goto drop;
1696 ipx = ipx_hdr(skb);
1697 if (ipx->ipx_checksum != IPX_NO_CHECKSUM &&
1698 ipx->ipx_checksum != ipx_cksum(ipx, ipx_pktsize))
1699 goto drop;
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;
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 &&
1709 IPX_SKB_CB(skb)->ipx_dest_net) {
1710 intrfc = ipxitf_auto_create(dev, pt->type);
1711 if (intrfc)
1712 ipxitf_hold(intrfc);
1715 if (!intrfc) /* Not one of ours */
1716 /* or invalid packet for auto creation */
1717 goto drop;
1720 rc = ipxitf_rcv(intrfc, skb);
1721 ipxitf_put(intrfc);
1722 goto out;
1723 drop:
1724 kfree_skb(skb);
1725 out:
1726 return rc;
1729 static int ipx_sendmsg(struct kiocb *iocb, struct socket *sock,
1730 struct msghdr *msg, size_t len)
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;
1739 lock_kernel();
1740 /* 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;
1746 /* Max possible packet size limited by 16 bit pktsize in header */
1747 if (len >= 65535 - sizeof(struct ipxhdr))
1748 goto out;
1750 if (usipx) {
1751 if (!ipxs->port) {
1752 struct sockaddr_ipx uaddr;
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
1763 rc = __ipx_bind(sock, (struct sockaddr *)&uaddr,
1764 sizeof(struct sockaddr_ipx));
1765 if (rc)
1766 goto out;
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;
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);
1786 rc = ipxrtr_route_packet(sk, usipx, msg->msg_iov, len,
1787 flags & MSG_DONTWAIT);
1788 if (rc >= 0)
1789 rc = len;
1790 out:
1791 unlock_kernel();
1792 return rc;
1796 static int ipx_recvmsg(struct kiocb *iocb, struct socket *sock,
1797 struct msghdr *msg, size_t size, int flags)
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;
1806 lock_kernel();
1807 /* put the autobinding in */
1808 if (!ipxs->port) {
1809 struct sockaddr_ipx uaddr;
1811 uaddr.sipx_port = 0;
1812 uaddr.sipx_network = 0;
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 */
1821 rc = __ipx_bind(sock, (struct sockaddr *)&uaddr,
1822 sizeof(struct sockaddr_ipx));
1823 if (rc)
1824 goto out;
1827 rc = -ENOTCONN;
1828 if (sock_flag(sk, SOCK_ZAPPED))
1829 goto out;
1831 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1832 flags & MSG_DONTWAIT, &rc);
1833 if (!skb)
1834 goto out;
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;
1843 rc = skb_copy_datagram_iovec(skb, sizeof(struct ipxhdr), msg->msg_iov,
1844 copied);
1845 if (rc)
1846 goto out_free;
1847 if (skb->tstamp.tv64)
1848 sk->sk_stamp = skb->tstamp;
1850 msg->msg_namelen = sizeof(*sipx);
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;
1860 rc = copied;
1862 out_free:
1863 skb_free_datagram(sk, skb);
1864 out:
1865 unlock_kernel();
1866 return rc;
1870 static int ipx_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1872 int rc = 0;
1873 long amount = 0;
1874 struct sock *sk = sock->sk;
1875 void __user *argp = (void __user *)arg;
1877 lock_kernel();
1878 switch (cmd) {
1879 case TIOCOUTQ:
1880 amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1881 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;
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:
1914 * This socket wants to take care of the NCP connection
1915 * handed to us in arg.
1917 rc = -EPERM;
1918 if (!capable(CAP_NET_ADMIN))
1919 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;
1925 if (sk)
1926 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:
1937 rc = -ENOIOCTLCMD;
1938 break;
1940 unlock_kernel();
1942 return rc;
1946 #ifdef CONFIG_COMPAT
1947 static int ipx_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
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.
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;
1965 #endif
1969 * Socket family declarations
1972 static const struct net_proto_family ipx_family_ops = {
1973 .family = PF_IPX,
1974 .create = ipx_create,
1975 .owner = THIS_MODULE,
1978 static const struct proto_ops ipx_dgram_ops = {
1979 .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,
1987 .poll = ipx_datagram_poll,
1988 .ioctl = ipx_ioctl,
1989 #ifdef CONFIG_COMPAT
1990 .compat_ioctl = ipx_compat_ioctl,
1991 #endif
1992 .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,
2002 static struct packet_type ipx_8023_packet_type __read_mostly = {
2003 .type = cpu_to_be16(ETH_P_802_3),
2004 .func = ipx_rcv,
2007 static struct packet_type ipx_dix_packet_type __read_mostly = {
2008 .type = cpu_to_be16(ETH_P_IPX),
2009 .func = ipx_rcv,
2012 static struct notifier_block ipx_dev_notifier = {
2013 .notifier_call = ipxitf_device_event,
2016 extern struct datalink_proto *make_EII_client(void);
2017 extern void destroy_EII_client(struct datalink_proto *);
2019 static const unsigned char ipx_8022_type = 0xE0;
2020 static const unsigned char ipx_snap_id[5] = { 0x0, 0x0, 0x0, 0x81, 0x37 };
2021 static const char ipx_EII_err_msg[] __initconst =
2022 KERN_CRIT "IPX: Unable to register with Ethernet II\n";
2023 static const char ipx_8023_err_msg[] __initconst =
2024 KERN_CRIT "IPX: Unable to register with 802.3\n";
2025 static const char ipx_llc_err_msg[] __initconst =
2026 KERN_CRIT "IPX: Unable to register with 802.2\n";
2027 static const char ipx_snap_err_msg[] __initconst =
2028 KERN_CRIT "IPX: Unable to register with SNAP\n";
2030 static int __init ipx_init(void)
2032 int rc = proto_register(&ipx_proto, 1);
2034 if (rc != 0)
2035 goto out;
2037 sock_register(&ipx_family_ops);
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);
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);
2051 p8022_datalink = register_8022_client(ipx_8022_type, ipx_rcv);
2052 if (!p8022_datalink)
2053 printk(ipx_llc_err_msg);
2055 pSNAP_datalink = register_snap_client(ipx_snap_id, ipx_rcv);
2056 if (!pSNAP_datalink)
2057 printk(ipx_snap_err_msg);
2059 register_netdevice_notifier(&ipx_dev_notifier);
2060 ipx_register_sysctl();
2061 ipx_proc_init();
2062 out:
2063 return rc;
2066 static void __exit ipx_proto_finito(void)
2068 ipx_proc_exit();
2069 ipx_unregister_sysctl();
2071 unregister_netdevice_notifier(&ipx_dev_notifier);
2073 ipxitf_cleanup();
2075 if (pSNAP_datalink) {
2076 unregister_snap_client(pSNAP_datalink);
2077 pSNAP_datalink = NULL;
2080 if (p8022_datalink) {
2081 unregister_8022_client(p8022_datalink);
2082 p8022_datalink = NULL;
2085 dev_remove_pack(&ipx_8023_packet_type);
2086 if (p8023_datalink) {
2087 destroy_8023_client(p8023_datalink);
2088 p8023_datalink = NULL;
2091 dev_remove_pack(&ipx_dix_packet_type);
2092 if (pEII_datalink) {
2093 destroy_EII_client(pEII_datalink);
2094 pEII_datalink = NULL;
2097 proto_unregister(&ipx_proto);
2098 sock_unregister(ipx_family_ops.family);
2101 module_init(ipx_init);
2102 module_exit(ipx_proto_finito);
2103 MODULE_LICENSE("GPL");
2104 MODULE_ALIAS_NETPROTO(PF_IPX);