]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipx/af_ipx.c
net: Move prototype declaration to header file include/net/datalink.h from net/ipx...
[mirror_ubuntu-bionic-kernel.git] / net / ipx / af_ipx.c
CommitLineData
1da177e4
LT
1/*
2 * Implements an IPX socket layer.
3 *
4 * This code is derived from work by
5 * Ross Biro : Writing the original IP stack
6 * Fred Van Kempen : Tidying up the TCP/IP
7 *
8 * Many thanks go to Keith Baker, Institute For Industrial Information
9 * Technology Ltd, Swansea University for allowing me to work on this
10 * in my own time even though it was in some ways related to commercial
11 * work I am currently employed to do there.
12 *
13 * All the material in this file is subject to the Gnu license version 2.
981c0ff6 14 * Neither Alan Cox nor the Swansea University Computer Society admit
1da177e4
LT
15 * 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
4fc268d2 31#include <linux/capability.h>
1da177e4
LT
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>
5a0e3ad6 43#include <linux/slab.h>
1da177e4
LT
44#include <linux/skbuff.h>
45#include <linux/socket.h>
46#include <linux/sockios.h>
47#include <linux/string.h>
1da177e4
LT
48#include <linux/types.h>
49#include <linux/termios.h>
50
51#include <net/ipx.h>
52#include <net/p8022.h>
53#include <net/psnap.h>
54#include <net/sock.h>
7780d8ae 55#include <net/datalink.h>
c752f073 56#include <net/tcp_states.h>
1da177e4
LT
57
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
90ddc4f0 80static const struct proto_ops ipx_dgram_ops;
1da177e4
LT
81
82LIST_HEAD(ipx_interfaces);
83DEFINE_SPINLOCK(ipx_interfaces_lock);
84
85struct ipx_interface *ipx_primary_net;
86struct ipx_interface *ipx_internal_net;
87
1da177e4
LT
88struct ipx_interface *ipx_interfaces_head(void)
89{
90 struct ipx_interface *rc = NULL;
91
92 if (!list_empty(&ipx_interfaces))
93 rc = list_entry(ipx_interfaces.next,
94 struct ipx_interface, node);
95 return rc;
96}
97
98static void ipxcfg_set_auto_select(char val)
99{
100 ipxcfg_auto_select_primary = val;
101 if (val && !ipx_primary_net)
102 ipx_primary_net = ipx_interfaces_head();
103}
104
105static int ipxcfg_get_config_data(struct ipx_config_data __user *arg)
106{
107 struct ipx_config_data vals;
108
109 vals.ipxcfg_auto_create_interfaces = ipxcfg_auto_create_interfaces;
110 vals.ipxcfg_auto_select_primary = ipxcfg_auto_select_primary;
111
112 return copy_to_user(arg, &vals, sizeof(vals)) ? -EFAULT : 0;
113}
114
115/*
116 * Note: Sockets may not be removed _during_ an interrupt or inet_bh
117 * handler using this technique. They can be added although we do not
118 * use this facility.
119 */
120
121static void ipx_remove_socket(struct sock *sk)
122{
123 /* Determine interface with which socket is associated */
124 struct ipx_interface *intrfc = ipx_sk(sk)->intrfc;
125
126 if (!intrfc)
127 goto out;
128
129 ipxitf_hold(intrfc);
130 spin_lock_bh(&intrfc->if_sklist_lock);
131 sk_del_node_init(sk);
132 spin_unlock_bh(&intrfc->if_sklist_lock);
133 ipxitf_put(intrfc);
134out:
135 return;
136}
137
138static void ipx_destroy_socket(struct sock *sk)
139{
140 ipx_remove_socket(sk);
141 skb_queue_purge(&sk->sk_receive_queue);
c2b42336 142 sk_refcnt_debug_dec(sk);
1da177e4
LT
143}
144
981c0ff6 145/*
1da177e4
LT
146 * The following code is used to support IPX Interfaces (IPXITF). An
147 * IPX interface is defined by a physical device and a frame type.
148 */
149
150/* ipxitf_clear_primary_net has to be called with ipx_interfaces_lock held */
151
152static void ipxitf_clear_primary_net(void)
153{
154 ipx_primary_net = NULL;
155 if (ipxcfg_auto_select_primary)
156 ipx_primary_net = ipx_interfaces_head();
157}
158
159static struct ipx_interface *__ipxitf_find_using_phys(struct net_device *dev,
4833ed09 160 __be16 datalink)
1da177e4
LT
161{
162 struct ipx_interface *i;
163
164 list_for_each_entry(i, &ipx_interfaces, node)
165 if (i->if_dev == dev && i->if_dlink_type == datalink)
166 goto out;
167 i = NULL;
168out:
169 return i;
170}
171
172static struct ipx_interface *ipxitf_find_using_phys(struct net_device *dev,
4833ed09 173 __be16 datalink)
1da177e4
LT
174{
175 struct ipx_interface *i;
176
177 spin_lock_bh(&ipx_interfaces_lock);
178 i = __ipxitf_find_using_phys(dev, datalink);
179 if (i)
180 ipxitf_hold(i);
181 spin_unlock_bh(&ipx_interfaces_lock);
182 return i;
183}
184
4833ed09 185struct ipx_interface *ipxitf_find_using_net(__be32 net)
1da177e4
LT
186{
187 struct ipx_interface *i;
188
189 spin_lock_bh(&ipx_interfaces_lock);
190 if (net) {
191 list_for_each_entry(i, &ipx_interfaces, node)
192 if (i->if_netnum == net)
193 goto hold;
194 i = NULL;
195 goto unlock;
196 }
197
198 i = ipx_primary_net;
199 if (i)
200hold:
201 ipxitf_hold(i);
202unlock:
203 spin_unlock_bh(&ipx_interfaces_lock);
204 return i;
205}
206
207/* Sockets are bound to a particular IPX interface. */
208static void ipxitf_insert_socket(struct ipx_interface *intrfc, struct sock *sk)
209{
210 ipxitf_hold(intrfc);
211 spin_lock_bh(&intrfc->if_sklist_lock);
212 ipx_sk(sk)->intrfc = intrfc;
213 sk_add_node(sk, &intrfc->if_sklist);
214 spin_unlock_bh(&intrfc->if_sklist_lock);
215 ipxitf_put(intrfc);
216}
217
218/* caller must hold intrfc->if_sklist_lock */
219static struct sock *__ipxitf_find_socket(struct ipx_interface *intrfc,
4833ed09 220 __be16 port)
1da177e4
LT
221{
222 struct sock *s;
1da177e4 223
b67bfe0d 224 sk_for_each(s, &intrfc->if_sklist)
1da177e4
LT
225 if (ipx_sk(s)->port == port)
226 goto found;
227 s = NULL;
228found:
229 return s;
230}
231
232/* caller must hold a reference to intrfc */
233static struct sock *ipxitf_find_socket(struct ipx_interface *intrfc,
4833ed09 234 __be16 port)
1da177e4
LT
235{
236 struct sock *s;
237
238 spin_lock_bh(&intrfc->if_sklist_lock);
239 s = __ipxitf_find_socket(intrfc, port);
240 if (s)
241 sock_hold(s);
242 spin_unlock_bh(&intrfc->if_sklist_lock);
243
244 return s;
245}
246
247#ifdef CONFIG_IPX_INTERN
248static struct sock *ipxitf_find_internal_socket(struct ipx_interface *intrfc,
249 unsigned char *ipx_node,
4833ed09 250 __be16 port)
1da177e4
LT
251{
252 struct sock *s;
1da177e4
LT
253
254 ipxitf_hold(intrfc);
255 spin_lock_bh(&intrfc->if_sklist_lock);
256
b67bfe0d 257 sk_for_each(s, &intrfc->if_sklist) {
1da177e4
LT
258 struct ipx_sock *ipxs = ipx_sk(s);
259
260 if (ipxs->port == port &&
261 !memcmp(ipx_node, ipxs->node, IPX_NODE_LEN))
262 goto found;
263 }
264 s = NULL;
265found:
266 spin_unlock_bh(&intrfc->if_sklist_lock);
267 ipxitf_put(intrfc);
268 return s;
269}
270#endif
271
272static void __ipxitf_down(struct ipx_interface *intrfc)
273{
274 struct sock *s;
b67bfe0d 275 struct hlist_node *t;
1da177e4
LT
276
277 /* Delete all routes associated with this interface */
278 ipxrtr_del_routes(intrfc);
279
280 spin_lock_bh(&intrfc->if_sklist_lock);
281 /* error sockets */
b67bfe0d 282 sk_for_each_safe(s, t, &intrfc->if_sklist) {
1da177e4
LT
283 struct ipx_sock *ipxs = ipx_sk(s);
284
285 s->sk_err = ENOLINK;
286 s->sk_error_report(s);
287 ipxs->intrfc = NULL;
288 ipxs->port = 0;
289 sock_set_flag(s, SOCK_ZAPPED); /* Indicates it is no longer bound */
290 sk_del_node_init(s);
291 }
292 INIT_HLIST_HEAD(&intrfc->if_sklist);
293 spin_unlock_bh(&intrfc->if_sklist_lock);
294
295 /* remove this interface from list */
296 list_del(&intrfc->node);
297
298 /* remove this interface from *special* networks */
299 if (intrfc == ipx_primary_net)
300 ipxitf_clear_primary_net();
301 if (intrfc == ipx_internal_net)
302 ipx_internal_net = NULL;
303
304 if (intrfc->if_dev)
305 dev_put(intrfc->if_dev);
306 kfree(intrfc);
307}
308
309void ipxitf_down(struct ipx_interface *intrfc)
310{
311 spin_lock_bh(&ipx_interfaces_lock);
312 __ipxitf_down(intrfc);
313 spin_unlock_bh(&ipx_interfaces_lock);
314}
315
316static __inline__ void __ipxitf_put(struct ipx_interface *intrfc)
317{
318 if (atomic_dec_and_test(&intrfc->refcnt))
319 __ipxitf_down(intrfc);
320}
321
322static int ipxitf_device_event(struct notifier_block *notifier,
323 unsigned long event, void *ptr)
324{
351638e7 325 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1da177e4
LT
326 struct ipx_interface *i, *tmp;
327
721499e8 328 if (!net_eq(dev_net(dev), &init_net))
e9dc8653
EB
329 return NOTIFY_DONE;
330
1da177e4
LT
331 if (event != NETDEV_DOWN && event != NETDEV_UP)
332 goto out;
333
334 spin_lock_bh(&ipx_interfaces_lock);
335 list_for_each_entry_safe(i, tmp, &ipx_interfaces, node)
336 if (i->if_dev == dev) {
337 if (event == NETDEV_UP)
338 ipxitf_hold(i);
339 else
340 __ipxitf_put(i);
341 }
342 spin_unlock_bh(&ipx_interfaces_lock);
343out:
344 return NOTIFY_DONE;
345}
346
347
348static __exit void ipxitf_cleanup(void)
349{
350 struct ipx_interface *i, *tmp;
351
352 spin_lock_bh(&ipx_interfaces_lock);
981c0ff6 353 list_for_each_entry_safe(i, tmp, &ipx_interfaces, node)
1da177e4
LT
354 __ipxitf_put(i);
355 spin_unlock_bh(&ipx_interfaces_lock);
356}
357
358static void ipxitf_def_skb_handler(struct sock *sock, struct sk_buff *skb)
359{
360 if (sock_queue_rcv_skb(sock, skb) < 0)
361 kfree_skb(skb);
362}
363
364/*
365 * On input skb->sk is NULL. Nobody is charged for the memory.
366 */
367
368/* caller must hold a reference to intrfc */
369
370#ifdef CONFIG_IPX_INTERN
371static int ipxitf_demux_socket(struct ipx_interface *intrfc,
372 struct sk_buff *skb, int copy)
373{
374 struct ipxhdr *ipx = ipx_hdr(skb);
375 int is_broadcast = !memcmp(ipx->ipx_dest.node, ipx_broadcast_node,
376 IPX_NODE_LEN);
377 struct sock *s;
1da177e4
LT
378 int rc;
379
380 spin_lock_bh(&intrfc->if_sklist_lock);
381
b67bfe0d 382 sk_for_each(s, &intrfc->if_sklist) {
1da177e4
LT
383 struct ipx_sock *ipxs = ipx_sk(s);
384
385 if (ipxs->port == ipx->ipx_dest.sock &&
386 (is_broadcast || !memcmp(ipx->ipx_dest.node,
387 ipxs->node, IPX_NODE_LEN))) {
388 /* We found a socket to which to send */
389 struct sk_buff *skb1;
390
391 if (copy) {
392 skb1 = skb_clone(skb, GFP_ATOMIC);
393 rc = -ENOMEM;
394 if (!skb1)
395 goto out;
396 } else {
397 skb1 = skb;
398 copy = 1; /* skb may only be used once */
399 }
400 ipxitf_def_skb_handler(s, skb1);
401
402 /* On an external interface, one socket can listen */
403 if (intrfc != ipx_internal_net)
404 break;
405 }
406 }
407
408 /* skb was solely for us, and we did not make a copy, so free it. */
409 if (!copy)
410 kfree_skb(skb);
411
412 rc = 0;
413out:
414 spin_unlock_bh(&intrfc->if_sklist_lock);
415 return rc;
416}
417#else
418static struct sock *ncp_connection_hack(struct ipx_interface *intrfc,
419 struct ipxhdr *ipx)
420{
421 /* The packet's target is a NCP connection handler. We want to hand it
422 * to the correct socket directly within the kernel, so that the
423 * mars_nwe packet distribution process does not have to do it. Here we
424 * only care about NCP and BURST packets.
425 *
426 * You might call this a hack, but believe me, you do not want a
427 * complete NCP layer in the kernel, and this is VERY fast as well. */
428 struct sock *sk = NULL;
981c0ff6 429 int connection = 0;
1da177e4
LT
430 u8 *ncphdr = (u8 *)(ipx + 1);
431
981c0ff6 432 if (*ncphdr == 0x22 && *(ncphdr + 1) == 0x22) /* NCP request */
1da177e4
LT
433 connection = (((int) *(ncphdr + 5)) << 8) | (int) *(ncphdr + 3);
434 else if (*ncphdr == 0x77 && *(ncphdr + 1) == 0x77) /* BURST packet */
435 connection = (((int) *(ncphdr + 9)) << 8) | (int) *(ncphdr + 8);
436
437 if (connection) {
1da177e4
LT
438 /* Now we have to look for a special NCP connection handling
439 * socket. Only these sockets have ipx_ncp_conn != 0, set by
440 * SIOCIPXNCPCONN. */
441 spin_lock_bh(&intrfc->if_sklist_lock);
b67bfe0d 442 sk_for_each(sk, &intrfc->if_sklist)
1da177e4
LT
443 if (ipx_sk(sk)->ipx_ncp_conn == connection) {
444 sock_hold(sk);
445 goto found;
446 }
447 sk = NULL;
448 found:
449 spin_unlock_bh(&intrfc->if_sklist_lock);
450 }
451 return sk;
452}
453
454static int ipxitf_demux_socket(struct ipx_interface *intrfc,
455 struct sk_buff *skb, int copy)
456{
457 struct ipxhdr *ipx = ipx_hdr(skb);
458 struct sock *sock1 = NULL, *sock2 = NULL;
459 struct sk_buff *skb1 = NULL, *skb2 = NULL;
460 int rc;
461
462 if (intrfc == ipx_primary_net && ntohs(ipx->ipx_dest.sock) == 0x451)
463 sock1 = ncp_connection_hack(intrfc, ipx);
981c0ff6 464 if (!sock1)
1da177e4
LT
465 /* No special socket found, forward the packet the normal way */
466 sock1 = ipxitf_find_socket(intrfc, ipx->ipx_dest.sock);
467
468 /*
469 * We need to check if there is a primary net and if
470 * this is addressed to one of the *SPECIAL* sockets because
471 * these need to be propagated to the primary net.
472 * The *SPECIAL* socket list contains: 0x452(SAP), 0x453(RIP) and
473 * 0x456(Diagnostic).
474 */
475
476 if (ipx_primary_net && intrfc != ipx_primary_net) {
477 const int dsock = ntohs(ipx->ipx_dest.sock);
478
479 if (dsock == 0x452 || dsock == 0x453 || dsock == 0x456)
480 /* The appropriate thing to do here is to dup the
481 * packet and route to the primary net interface via
482 * ipxitf_send; however, we'll cheat and just demux it
483 * here. */
484 sock2 = ipxitf_find_socket(ipx_primary_net,
485 ipx->ipx_dest.sock);
486 }
487
488 /*
489 * If there is nothing to do return. The kfree will cancel any charging.
490 */
491 rc = 0;
492 if (!sock1 && !sock2) {
493 if (!copy)
494 kfree_skb(skb);
495 goto out;
496 }
497
498 /*
499 * This next segment of code is a little awkward, but it sets it up
500 * so that the appropriate number of copies of the SKB are made and
501 * that skb1 and skb2 point to it (them) so that it (they) can be
502 * demuxed to sock1 and/or sock2. If we are unable to make enough
503 * copies, we do as much as is possible.
504 */
505
506 if (copy)
507 skb1 = skb_clone(skb, GFP_ATOMIC);
508 else
509 skb1 = skb;
510
511 rc = -ENOMEM;
512 if (!skb1)
513 goto out_put;
514
515 /* Do we need 2 SKBs? */
516 if (sock1 && sock2)
517 skb2 = skb_clone(skb1, GFP_ATOMIC);
518 else
519 skb2 = skb1;
520
521 if (sock1)
522 ipxitf_def_skb_handler(sock1, skb1);
523
524 if (!skb2)
525 goto out_put;
526
527 if (sock2)
528 ipxitf_def_skb_handler(sock2, skb2);
529
530 rc = 0;
531out_put:
532 if (sock1)
533 sock_put(sock1);
534 if (sock2)
535 sock_put(sock2);
536out:
537 return rc;
538}
539#endif /* CONFIG_IPX_INTERN */
540
541static struct sk_buff *ipxitf_adjust_skbuff(struct ipx_interface *intrfc,
542 struct sk_buff *skb)
543{
544 struct sk_buff *skb2;
545 int in_offset = (unsigned char *)ipx_hdr(skb) - skb->head;
546 int out_offset = intrfc->if_ipx_offset;
547 int len;
548
549 /* Hopefully, most cases */
550 if (in_offset >= out_offset)
551 return skb;
552
553 /* Need new SKB */
554 len = skb->len + out_offset;
555 skb2 = alloc_skb(len, GFP_ATOMIC);
556 if (skb2) {
557 skb_reserve(skb2, out_offset);
7e28ecc2 558 skb_reset_network_header(skb2);
badff6d0 559 skb_reset_transport_header(skb2);
7e28ecc2 560 skb_put(skb2, skb->len);
1da177e4
LT
561 memcpy(ipx_hdr(skb2), ipx_hdr(skb), skb->len);
562 memcpy(skb2->cb, skb->cb, sizeof(skb->cb));
563 }
564 kfree_skb(skb);
565 return skb2;
566}
567
568/* caller must hold a reference to intrfc and the skb has to be unshared */
569int ipxitf_send(struct ipx_interface *intrfc, struct sk_buff *skb, char *node)
570{
571 struct ipxhdr *ipx = ipx_hdr(skb);
572 struct net_device *dev = intrfc->if_dev;
573 struct datalink_proto *dl = intrfc->if_dlink;
574 char dest_node[IPX_NODE_LEN];
575 int send_to_wire = 1;
576 int addr_len;
577
578 ipx->ipx_tctrl = IPX_SKB_CB(skb)->ipx_tctrl;
579 ipx->ipx_dest.net = IPX_SKB_CB(skb)->ipx_dest_net;
580 ipx->ipx_source.net = IPX_SKB_CB(skb)->ipx_source_net;
581
582 /* see if we need to include the netnum in the route list */
583 if (IPX_SKB_CB(skb)->last_hop.index >= 0) {
4833ed09 584 __be32 *last_hop = (__be32 *)(((u8 *) skb->data) +
1da177e4
LT
585 sizeof(struct ipxhdr) +
586 IPX_SKB_CB(skb)->last_hop.index *
4833ed09 587 sizeof(__be32));
1da177e4
LT
588 *last_hop = IPX_SKB_CB(skb)->last_hop.netnum;
589 IPX_SKB_CB(skb)->last_hop.index = -1;
590 }
981c0ff6
YH
591
592 /*
1da177e4
LT
593 * We need to know how many skbuffs it will take to send out this
594 * packet to avoid unnecessary copies.
595 */
981c0ff6
YH
596
597 if (!dl || !dev || dev->flags & IFF_LOOPBACK)
1da177e4
LT
598 send_to_wire = 0; /* No non looped */
599
600 /*
981c0ff6 601 * See if this should be demuxed to sockets on this interface
1da177e4
LT
602 *
603 * We want to ensure the original was eaten or that we only use
604 * up clones.
605 */
981c0ff6 606
1da177e4
LT
607 if (ipx->ipx_dest.net == intrfc->if_netnum) {
608 /*
609 * To our own node, loop and free the original.
610 * The internal net will receive on all node address.
611 */
612 if (intrfc == ipx_internal_net ||
613 !memcmp(intrfc->if_node, node, IPX_NODE_LEN)) {
614 /* Don't charge sender */
615 skb_orphan(skb);
616
617 /* Will charge receiver */
618 return ipxitf_demux_socket(intrfc, skb, 0);
619 }
620
621 /* Broadcast, loop and possibly keep to send on. */
622 if (!memcmp(ipx_broadcast_node, node, IPX_NODE_LEN)) {
623 if (!send_to_wire)
624 skb_orphan(skb);
625 ipxitf_demux_socket(intrfc, skb, send_to_wire);
626 if (!send_to_wire)
627 goto out;
628 }
629 }
630
631 /*
632 * If the originating net is not equal to our net; this is routed
633 * We are still charging the sender. Which is right - the driver
634 * free will handle this fairly.
635 */
636 if (ipx->ipx_source.net != intrfc->if_netnum) {
637 /*
638 * Unshare the buffer before modifying the count in
639 * case it's a flood or tcpdump
640 */
641 skb = skb_unshare(skb, GFP_ATOMIC);
642 if (!skb)
643 goto out;
644 if (++ipx->ipx_tctrl > ipxcfg_max_hops)
645 send_to_wire = 0;
646 }
647
648 if (!send_to_wire) {
649 kfree_skb(skb);
650 goto out;
651 }
652
653 /* Determine the appropriate hardware address */
654 addr_len = dev->addr_len;
655 if (!memcmp(ipx_broadcast_node, node, IPX_NODE_LEN))
656 memcpy(dest_node, dev->broadcast, addr_len);
657 else
658 memcpy(dest_node, &(node[IPX_NODE_LEN-addr_len]), addr_len);
659
660 /* Make any compensation for differing physical/data link size */
661 skb = ipxitf_adjust_skbuff(intrfc, skb);
662 if (!skb)
663 goto out;
664
665 /* set up data link and physical headers */
666 skb->dev = dev;
667 skb->protocol = htons(ETH_P_IPX);
668
669 /* Send it out */
670 dl->request(dl, skb, dest_node);
671out:
672 return 0;
673}
674
675static int ipxitf_add_local_route(struct ipx_interface *intrfc)
676{
677 return ipxrtr_add_route(intrfc->if_netnum, intrfc, NULL);
678}
679
680static void ipxitf_discover_netnum(struct ipx_interface *intrfc,
681 struct sk_buff *skb);
682static int ipxitf_pprop(struct ipx_interface *intrfc, struct sk_buff *skb);
683
684static int ipxitf_rcv(struct ipx_interface *intrfc, struct sk_buff *skb)
685{
686 struct ipxhdr *ipx = ipx_hdr(skb);
687 int rc = 0;
688
689 ipxitf_hold(intrfc);
690
691 /* See if we should update our network number */
692 if (!intrfc->if_netnum) /* net number of intrfc not known yet */
981c0ff6
YH
693 ipxitf_discover_netnum(intrfc, skb);
694
1da177e4
LT
695 IPX_SKB_CB(skb)->last_hop.index = -1;
696 if (ipx->ipx_type == IPX_TYPE_PPROP) {
697 rc = ipxitf_pprop(intrfc, skb);
698 if (rc)
699 goto out_free_skb;
700 }
701
702 /* local processing follows */
703 if (!IPX_SKB_CB(skb)->ipx_dest_net)
704 IPX_SKB_CB(skb)->ipx_dest_net = intrfc->if_netnum;
705 if (!IPX_SKB_CB(skb)->ipx_source_net)
706 IPX_SKB_CB(skb)->ipx_source_net = intrfc->if_netnum;
707
708 /* it doesn't make sense to route a pprop packet, there's no meaning
709 * in the ipx_dest_net for such packets */
710 if (ipx->ipx_type != IPX_TYPE_PPROP &&
711 intrfc->if_netnum != IPX_SKB_CB(skb)->ipx_dest_net) {
712 /* We only route point-to-point packets. */
713 if (skb->pkt_type == PACKET_HOST) {
714 skb = skb_unshare(skb, GFP_ATOMIC);
715 if (skb)
716 rc = ipxrtr_route_skb(skb);
717 goto out_intrfc;
718 }
719
720 goto out_free_skb;
721 }
722
723 /* see if we should keep it */
724 if (!memcmp(ipx_broadcast_node, ipx->ipx_dest.node, IPX_NODE_LEN) ||
725 !memcmp(intrfc->if_node, ipx->ipx_dest.node, IPX_NODE_LEN)) {
726 rc = ipxitf_demux_socket(intrfc, skb, 0);
727 goto out_intrfc;
728 }
729
730 /* we couldn't pawn it off so unload it */
731out_free_skb:
732 kfree_skb(skb);
733out_intrfc:
734 ipxitf_put(intrfc);
735 return rc;
736}
737
738static void ipxitf_discover_netnum(struct ipx_interface *intrfc,
739 struct sk_buff *skb)
981c0ff6 740{
1da177e4
LT
741 const struct ipx_cb *cb = IPX_SKB_CB(skb);
742
743 /* see if this is an intra packet: source_net == dest_net */
744 if (cb->ipx_source_net == cb->ipx_dest_net && cb->ipx_source_net) {
745 struct ipx_interface *i =
746 ipxitf_find_using_net(cb->ipx_source_net);
747 /* NB: NetWare servers lie about their hop count so we
748 * dropped the test based on it. This is the best way
749 * to determine this is a 0 hop count packet. */
750 if (!i) {
751 intrfc->if_netnum = cb->ipx_source_net;
752 ipxitf_add_local_route(intrfc);
753 } else {
754 printk(KERN_WARNING "IPX: Network number collision "
755 "%lx\n %s %s and %s %s\n",
4833ed09 756 (unsigned long) ntohl(cb->ipx_source_net),
1da177e4
LT
757 ipx_device_name(i),
758 ipx_frame_name(i->if_dlink_type),
759 ipx_device_name(intrfc),
760 ipx_frame_name(intrfc->if_dlink_type));
761 ipxitf_put(i);
762 }
763 }
764}
765
766/**
767 * ipxitf_pprop - Process packet propagation IPX packet type 0x14, used for
768 * NetBIOS broadcasts
769 * @intrfc: IPX interface receiving this packet
770 * @skb: Received packet
771 *
772 * Checks if packet is valid: if its more than %IPX_MAX_PPROP_HOPS hops or if it
773 * is smaller than a IPX header + the room for %IPX_MAX_PPROP_HOPS hops we drop
774 * it, not even processing it locally, if it has exact %IPX_MAX_PPROP_HOPS we
775 * don't broadcast it, but process it locally. See chapter 5 of Novell's "IPX
776 * RIP and SAP Router Specification", Part Number 107-000029-001.
981c0ff6 777 *
1da177e4
LT
778 * If it is valid, check if we have pprop broadcasting enabled by the user,
779 * if not, just return zero for local processing.
780 *
781 * If it is enabled check the packet and don't broadcast it if we have already
782 * seen this packet.
783 *
784 * Broadcast: send it to the interfaces that aren't on the packet visited nets
785 * array, just after the IPX header.
786 *
787 * Returns -EINVAL for invalid packets, so that the calling function drops
788 * the packet without local processing. 0 if packet is to be locally processed.
789 */
790static int ipxitf_pprop(struct ipx_interface *intrfc, struct sk_buff *skb)
791{
792 struct ipxhdr *ipx = ipx_hdr(skb);
793 int i, rc = -EINVAL;
794 struct ipx_interface *ifcs;
795 char *c;
4833ed09 796 __be32 *l;
1da177e4
LT
797
798 /* Illegal packet - too many hops or too short */
799 /* We decide to throw it away: no broadcasting, no local processing.
800 * NetBIOS unaware implementations route them as normal packets -
801 * tctrl <= 15, any data payload... */
802 if (IPX_SKB_CB(skb)->ipx_tctrl > IPX_MAX_PPROP_HOPS ||
803 ntohs(ipx->ipx_pktsize) < sizeof(struct ipxhdr) +
981c0ff6 804 IPX_MAX_PPROP_HOPS * sizeof(u32))
1da177e4
LT
805 goto out;
806 /* are we broadcasting this damn thing? */
807 rc = 0;
808 if (!sysctl_ipx_pprop_broadcasting)
809 goto out;
810 /* We do broadcast packet on the IPX_MAX_PPROP_HOPS hop, but we
811 * process it locally. All previous hops broadcasted it, and process it
812 * locally. */
813 if (IPX_SKB_CB(skb)->ipx_tctrl == IPX_MAX_PPROP_HOPS)
814 goto out;
981c0ff6 815
1da177e4 816 c = ((u8 *) ipx) + sizeof(struct ipxhdr);
4833ed09 817 l = (__be32 *) c;
1da177e4
LT
818
819 /* Don't broadcast packet if already seen this net */
820 for (i = 0; i < IPX_SKB_CB(skb)->ipx_tctrl; i++)
821 if (*l++ == intrfc->if_netnum)
822 goto out;
823
824 /* < IPX_MAX_PPROP_HOPS hops && input interface not in list. Save the
825 * position where we will insert recvd netnum into list, later on,
826 * in ipxitf_send */
827 IPX_SKB_CB(skb)->last_hop.index = i;
828 IPX_SKB_CB(skb)->last_hop.netnum = intrfc->if_netnum;
829 /* xmit on all other interfaces... */
830 spin_lock_bh(&ipx_interfaces_lock);
831 list_for_each_entry(ifcs, &ipx_interfaces, node) {
832 /* Except unconfigured interfaces */
833 if (!ifcs->if_netnum)
834 continue;
981c0ff6 835
1da177e4
LT
836 /* That aren't in the list */
837 if (ifcs == intrfc)
838 continue;
4833ed09 839 l = (__be32 *) c;
1da177e4
LT
840 /* don't consider the last entry in the packet list,
841 * it is our netnum, and it is not there yet */
842 for (i = 0; i < IPX_SKB_CB(skb)->ipx_tctrl; i++)
843 if (ifcs->if_netnum == *l++)
844 break;
845 if (i == IPX_SKB_CB(skb)->ipx_tctrl) {
846 struct sk_buff *s = skb_copy(skb, GFP_ATOMIC);
847
848 if (s) {
849 IPX_SKB_CB(s)->ipx_dest_net = ifcs->if_netnum;
850 ipxrtr_route_skb(s);
851 }
852 }
853 }
854 spin_unlock_bh(&ipx_interfaces_lock);
855out:
856 return rc;
857}
858
859static void ipxitf_insert(struct ipx_interface *intrfc)
860{
861 spin_lock_bh(&ipx_interfaces_lock);
862 list_add_tail(&intrfc->node, &ipx_interfaces);
863 spin_unlock_bh(&ipx_interfaces_lock);
864
865 if (ipxcfg_auto_select_primary && !ipx_primary_net)
866 ipx_primary_net = intrfc;
867}
868
4833ed09
AV
869static struct ipx_interface *ipxitf_alloc(struct net_device *dev, __be32 netnum,
870 __be16 dlink_type,
1da177e4
LT
871 struct datalink_proto *dlink,
872 unsigned char internal,
873 int ipx_offset)
874{
875 struct ipx_interface *intrfc = kmalloc(sizeof(*intrfc), GFP_ATOMIC);
876
877 if (intrfc) {
878 intrfc->if_dev = dev;
879 intrfc->if_netnum = netnum;
880 intrfc->if_dlink_type = dlink_type;
881 intrfc->if_dlink = dlink;
882 intrfc->if_internal = internal;
883 intrfc->if_ipx_offset = ipx_offset;
884 intrfc->if_sknum = IPX_MIN_EPHEMERAL_SOCKET;
885 INIT_HLIST_HEAD(&intrfc->if_sklist);
886 atomic_set(&intrfc->refcnt, 1);
887 spin_lock_init(&intrfc->if_sklist_lock);
888 }
889
890 return intrfc;
891}
892
893static int ipxitf_create_internal(struct ipx_interface_definition *idef)
894{
895 struct ipx_interface *intrfc;
896 int rc = -EEXIST;
897
898 /* Only one primary network allowed */
899 if (ipx_primary_net)
900 goto out;
901
902 /* Must have a valid network number */
903 rc = -EADDRNOTAVAIL;
904 if (!idef->ipx_network)
905 goto out;
906 intrfc = ipxitf_find_using_net(idef->ipx_network);
907 rc = -EADDRINUSE;
908 if (intrfc) {
909 ipxitf_put(intrfc);
910 goto out;
911 }
912 intrfc = ipxitf_alloc(NULL, idef->ipx_network, 0, NULL, 1, 0);
913 rc = -EAGAIN;
914 if (!intrfc)
915 goto out;
916 memcpy((char *)&(intrfc->if_node), idef->ipx_node, IPX_NODE_LEN);
917 ipx_internal_net = ipx_primary_net = intrfc;
918 ipxitf_hold(intrfc);
919 ipxitf_insert(intrfc);
920
921 rc = ipxitf_add_local_route(intrfc);
922 ipxitf_put(intrfc);
923out:
924 return rc;
925}
926
4ac396c0 927static __be16 ipx_map_frame_type(unsigned char type)
1da177e4 928{
4ac396c0 929 __be16 rc = 0;
1da177e4
LT
930
931 switch (type) {
932 case IPX_FRAME_ETHERII: rc = htons(ETH_P_IPX); break;
933 case IPX_FRAME_8022: rc = htons(ETH_P_802_2); break;
934 case IPX_FRAME_SNAP: rc = htons(ETH_P_SNAP); break;
935 case IPX_FRAME_8023: rc = htons(ETH_P_802_3); break;
936 }
937
938 return rc;
939}
940
941static int ipxitf_create(struct ipx_interface_definition *idef)
942{
943 struct net_device *dev;
4833ed09 944 __be16 dlink_type = 0;
1da177e4
LT
945 struct datalink_proto *datalink = NULL;
946 struct ipx_interface *intrfc;
947 int rc;
948
949 if (idef->ipx_special == IPX_INTERNAL) {
950 rc = ipxitf_create_internal(idef);
951 goto out;
952 }
953
954 rc = -EEXIST;
955 if (idef->ipx_special == IPX_PRIMARY && ipx_primary_net)
956 goto out;
957
958 intrfc = ipxitf_find_using_net(idef->ipx_network);
959 rc = -EADDRINUSE;
960 if (idef->ipx_network && intrfc) {
961 ipxitf_put(intrfc);
962 goto out;
963 }
964
965 if (intrfc)
966 ipxitf_put(intrfc);
967
881d966b 968 dev = dev_get_by_name(&init_net, idef->ipx_device);
1da177e4
LT
969 rc = -ENODEV;
970 if (!dev)
971 goto out;
972
973 switch (idef->ipx_dlink_type) {
1da177e4
LT
974 case IPX_FRAME_8022:
975 dlink_type = htons(ETH_P_802_2);
976 datalink = p8022_datalink;
977 break;
978 case IPX_FRAME_ETHERII:
979 if (dev->type != ARPHRD_IEEE802) {
980 dlink_type = htons(ETH_P_IPX);
981 datalink = pEII_datalink;
982 break;
211ed865 983 }
1da177e4
LT
984 /* fall through */
985 case IPX_FRAME_SNAP:
986 dlink_type = htons(ETH_P_SNAP);
987 datalink = pSNAP_datalink;
988 break;
989 case IPX_FRAME_8023:
990 dlink_type = htons(ETH_P_802_3);
991 datalink = p8023_datalink;
992 break;
993 case IPX_FRAME_NONE:
994 default:
995 rc = -EPROTONOSUPPORT;
996 goto out_dev;
997 }
998
999 rc = -ENETDOWN;
1000 if (!(dev->flags & IFF_UP))
1001 goto out_dev;
1002
1003 /* Check addresses are suitable */
1004 rc = -EINVAL;
1005 if (dev->addr_len > IPX_NODE_LEN)
1006 goto out_dev;
1007
1008 intrfc = ipxitf_find_using_phys(dev, dlink_type);
1009 if (!intrfc) {
1010 /* Ok now create */
1011 intrfc = ipxitf_alloc(dev, idef->ipx_network, dlink_type,
1012 datalink, 0, dev->hard_header_len +
1013 datalink->header_length);
1014 rc = -EAGAIN;
1015 if (!intrfc)
1016 goto out_dev;
1017 /* Setup primary if necessary */
1018 if (idef->ipx_special == IPX_PRIMARY)
1019 ipx_primary_net = intrfc;
1020 if (!memcmp(idef->ipx_node, "\000\000\000\000\000\000",
1021 IPX_NODE_LEN)) {
1022 memset(intrfc->if_node, 0, IPX_NODE_LEN);
1023 memcpy(intrfc->if_node + IPX_NODE_LEN - dev->addr_len,
1024 dev->dev_addr, dev->addr_len);
1025 } else
1026 memcpy(intrfc->if_node, idef->ipx_node, IPX_NODE_LEN);
1027 ipxitf_hold(intrfc);
1028 ipxitf_insert(intrfc);
1029 }
1030
1031
1032 /* If the network number is known, add a route */
1033 rc = 0;
1034 if (!intrfc->if_netnum)
1035 goto out_intrfc;
1036
1037 rc = ipxitf_add_local_route(intrfc);
1038out_intrfc:
1039 ipxitf_put(intrfc);
1040 goto out;
1041out_dev:
1042 dev_put(dev);
1043out:
1044 return rc;
1045}
1046
1047static int ipxitf_delete(struct ipx_interface_definition *idef)
1048{
1049 struct net_device *dev = NULL;
4833ed09 1050 __be16 dlink_type = 0;
1da177e4
LT
1051 struct ipx_interface *intrfc;
1052 int rc = 0;
1053
1054 spin_lock_bh(&ipx_interfaces_lock);
1055 if (idef->ipx_special == IPX_INTERNAL) {
1056 if (ipx_internal_net) {
1057 __ipxitf_put(ipx_internal_net);
1058 goto out;
1059 }
1060 rc = -ENOENT;
1061 goto out;
1062 }
1063
1064 dlink_type = ipx_map_frame_type(idef->ipx_dlink_type);
1065 rc = -EPROTONOSUPPORT;
1066 if (!dlink_type)
1067 goto out;
1068
881d966b 1069 dev = __dev_get_by_name(&init_net, idef->ipx_device);
1da177e4
LT
1070 rc = -ENODEV;
1071 if (!dev)
1072 goto out;
1073
1074 intrfc = __ipxitf_find_using_phys(dev, dlink_type);
1075 rc = -EINVAL;
1076 if (!intrfc)
1077 goto out;
1078 __ipxitf_put(intrfc);
1079
1080 rc = 0;
1081out:
1082 spin_unlock_bh(&ipx_interfaces_lock);
1083 return rc;
1084}
1085
1086static struct ipx_interface *ipxitf_auto_create(struct net_device *dev,
4833ed09 1087 __be16 dlink_type)
1da177e4
LT
1088{
1089 struct ipx_interface *intrfc = NULL;
1090 struct datalink_proto *datalink;
1091
1092 if (!dev)
1093 goto out;
1094
1095 /* Check addresses are suitable */
1096 if (dev->addr_len > IPX_NODE_LEN)
1097 goto out;
1098
4833ed09 1099 switch (ntohs(dlink_type)) {
1da177e4
LT
1100 case ETH_P_IPX: datalink = pEII_datalink; break;
1101 case ETH_P_802_2: datalink = p8022_datalink; break;
1102 case ETH_P_SNAP: datalink = pSNAP_datalink; break;
1103 case ETH_P_802_3: datalink = p8023_datalink; break;
1104 default: goto out;
1105 }
1106
1107 intrfc = ipxitf_alloc(dev, 0, dlink_type, datalink, 0,
1108 dev->hard_header_len + datalink->header_length);
1109
1110 if (intrfc) {
1111 memset(intrfc->if_node, 0, IPX_NODE_LEN);
1112 memcpy((char *)&(intrfc->if_node[IPX_NODE_LEN-dev->addr_len]),
1113 dev->dev_addr, dev->addr_len);
1114 spin_lock_init(&intrfc->if_sklist_lock);
1115 atomic_set(&intrfc->refcnt, 1);
1116 ipxitf_insert(intrfc);
1117 dev_hold(dev);
1118 }
1119
1120out:
1121 return intrfc;
1122}
1123
1124static int ipxitf_ioctl(unsigned int cmd, void __user *arg)
1125{
1126 int rc = -EINVAL;
1127 struct ifreq ifr;
1128 int val;
1129
1130 switch (cmd) {
1131 case SIOCSIFADDR: {
1132 struct sockaddr_ipx *sipx;
1133 struct ipx_interface_definition f;
1134
1135 rc = -EFAULT;
1136 if (copy_from_user(&ifr, arg, sizeof(ifr)))
1137 break;
1138 sipx = (struct sockaddr_ipx *)&ifr.ifr_addr;
1139 rc = -EINVAL;
1140 if (sipx->sipx_family != AF_IPX)
1141 break;
1142 f.ipx_network = sipx->sipx_network;
1143 memcpy(f.ipx_device, ifr.ifr_name,
1144 sizeof(f.ipx_device));
1145 memcpy(f.ipx_node, sipx->sipx_node, IPX_NODE_LEN);
1146 f.ipx_dlink_type = sipx->sipx_type;
1147 f.ipx_special = sipx->sipx_special;
1148
1149 if (sipx->sipx_action == IPX_DLTITF)
1150 rc = ipxitf_delete(&f);
1151 else
1152 rc = ipxitf_create(&f);
1153 break;
1154 }
1155 case SIOCGIFADDR: {
1156 struct sockaddr_ipx *sipx;
1157 struct ipx_interface *ipxif;
1158 struct net_device *dev;
1159
1160 rc = -EFAULT;
1161 if (copy_from_user(&ifr, arg, sizeof(ifr)))
1162 break;
1163 sipx = (struct sockaddr_ipx *)&ifr.ifr_addr;
881d966b 1164 dev = __dev_get_by_name(&init_net, ifr.ifr_name);
1da177e4
LT
1165 rc = -ENODEV;
1166 if (!dev)
1167 break;
1168 ipxif = ipxitf_find_using_phys(dev,
1169 ipx_map_frame_type(sipx->sipx_type));
1170 rc = -EADDRNOTAVAIL;
1171 if (!ipxif)
1172 break;
1173
1174 sipx->sipx_family = AF_IPX;
1175 sipx->sipx_network = ipxif->if_netnum;
1176 memcpy(sipx->sipx_node, ipxif->if_node,
1177 sizeof(sipx->sipx_node));
1178 rc = -EFAULT;
1179 if (copy_to_user(arg, &ifr, sizeof(ifr)))
1180 break;
1181 ipxitf_put(ipxif);
1182 rc = 0;
1183 break;
1184 }
981c0ff6 1185 case SIOCAIPXITFCRT:
1da177e4
LT
1186 rc = -EFAULT;
1187 if (get_user(val, (unsigned char __user *) arg))
1188 break;
1189 rc = 0;
1190 ipxcfg_auto_create_interfaces = val;
1191 break;
981c0ff6 1192 case SIOCAIPXPRISLT:
1da177e4
LT
1193 rc = -EFAULT;
1194 if (get_user(val, (unsigned char __user *) arg))
1195 break;
1196 rc = 0;
1197 ipxcfg_set_auto_select(val);
1198 break;
1199 }
1200
1201 return rc;
1202}
1203
1204/*
1205 * Checksum routine for IPX
1206 */
981c0ff6 1207
1da177e4
LT
1208/* Note: We assume ipx_tctrl==0 and htons(length)==ipx_pktsize */
1209/* This functions should *not* mess with packet contents */
1210
02e60370 1211__be16 ipx_cksum(struct ipxhdr *packet, int length)
1da177e4 1212{
981c0ff6
YH
1213 /*
1214 * NOTE: sum is a net byte order quantity, which optimizes the
1da177e4
LT
1215 * loop. This only works on big and little endian machines. (I
1216 * don't know of a machine that isn't.)
1217 */
02e60370
AV
1218 /* handle the first 3 words separately; checksum should be skipped
1219 * and ipx_tctrl masked out */
1220 __u16 *p = (__u16 *)packet;
1221 __u32 sum = p[1] + (p[2] & (__force u16)htons(0x00ff));
1222 __u32 i = (length >> 1) - 3; /* Number of remaining complete words */
1223
1224 /* Loop through them */
1225 p += 3;
1226 while (i--)
1da177e4
LT
1227 sum += *p++;
1228
1229 /* Add on the last part word if it exists */
1230 if (packet->ipx_pktsize & htons(1))
02e60370 1231 sum += (__force u16)htons(0xff00) & *p;
1da177e4
LT
1232
1233 /* Do final fixup */
1234 sum = (sum & 0xffff) + (sum >> 16);
1235
1236 /* It's a pity there's no concept of carry in C */
1237 if (sum >= 0x10000)
1238 sum++;
1239
02e60370
AV
1240 /*
1241 * Leave 0 alone; we don't want 0xffff here. Note that we can't get
1242 * here with 0x10000, so this check is the same as ((__u16)sum)
1243 */
1244 if (sum)
1245 sum = ~sum;
1246
1247 return (__force __be16)sum;
1da177e4
LT
1248}
1249
4833ed09 1250const char *ipx_frame_name(__be16 frame)
1da177e4
LT
1251{
1252 char* rc = "None";
1253
1254 switch (ntohs(frame)) {
1255 case ETH_P_IPX: rc = "EtherII"; break;
1256 case ETH_P_802_2: rc = "802.2"; break;
1257 case ETH_P_SNAP: rc = "SNAP"; break;
1258 case ETH_P_802_3: rc = "802.3"; break;
1da177e4
LT
1259 }
1260
1261 return rc;
1262}
1263
1264const char *ipx_device_name(struct ipx_interface *intrfc)
1265{
1266 return intrfc->if_internal ? "Internal" :
1267 intrfc->if_dev ? intrfc->if_dev->name : "Unknown";
1268}
1269
1270/* Handling for system calls applied via the various interfaces to an IPX
1271 * socket object. */
1272
1273static int ipx_setsockopt(struct socket *sock, int level, int optname,
b7058842 1274 char __user *optval, unsigned int optlen)
1da177e4
LT
1275{
1276 struct sock *sk = sock->sk;
1277 int opt;
1278 int rc = -EINVAL;
1279
b0d0d915 1280 lock_sock(sk);
1da177e4
LT
1281 if (optlen != sizeof(int))
1282 goto out;
1283
1284 rc = -EFAULT;
1285 if (get_user(opt, (unsigned int __user *)optval))
1286 goto out;
1287
1288 rc = -ENOPROTOOPT;
1289 if (!(level == SOL_IPX && optname == IPX_TYPE))
1290 goto out;
1291
1292 ipx_sk(sk)->type = opt;
1293 rc = 0;
1294out:
b0d0d915 1295 release_sock(sk);
1da177e4
LT
1296 return rc;
1297}
1298
1299static int ipx_getsockopt(struct socket *sock, int level, int optname,
1300 char __user *optval, int __user *optlen)
1301{
1302 struct sock *sk = sock->sk;
1303 int val = 0;
1304 int len;
1305 int rc = -ENOPROTOOPT;
1306
b0d0d915 1307 lock_sock(sk);
1da177e4
LT
1308 if (!(level == SOL_IPX && optname == IPX_TYPE))
1309 goto out;
1310
1311 val = ipx_sk(sk)->type;
1312
1313 rc = -EFAULT;
1314 if (get_user(len, optlen))
1315 goto out;
1316
1317 len = min_t(unsigned int, len, sizeof(int));
1318 rc = -EINVAL;
1319 if(len < 0)
1320 goto out;
981c0ff6 1321
1da177e4
LT
1322 rc = -EFAULT;
1323 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
1324 goto out;
1325
1326 rc = 0;
1327out:
b0d0d915 1328 release_sock(sk);
1da177e4
LT
1329 return rc;
1330}
1331
1332static struct proto ipx_proto = {
1333 .name = "IPX",
1334 .owner = THIS_MODULE,
1335 .obj_size = sizeof(struct ipx_sock),
1336};
1337
3f378b68
EP
1338static int ipx_create(struct net *net, struct socket *sock, int protocol,
1339 int kern)
1da177e4
LT
1340{
1341 int rc = -ESOCKTNOSUPPORT;
1342 struct sock *sk;
1343
09ad9bc7 1344 if (!net_eq(net, &init_net))
1b8d7ae4
EB
1345 return -EAFNOSUPPORT;
1346
1da177e4
LT
1347 /*
1348 * SPX support is not anymore in the kernel sources. If you want to
1349 * ressurrect it, completing it and making it understand shared skbs,
1350 * be fully multithreaded, etc, grab the sources in an early 2.5 kernel
1351 * tree.
1352 */
1353 if (sock->type != SOCK_DGRAM)
1354 goto out;
1355
981c0ff6 1356 rc = -ENOMEM;
6257ff21 1357 sk = sk_alloc(net, PF_IPX, GFP_KERNEL, &ipx_proto);
1da177e4
LT
1358 if (!sk)
1359 goto out;
c2b42336
PE
1360
1361 sk_refcnt_debug_inc(sk);
1da177e4
LT
1362 sock_init_data(sock, sk);
1363 sk->sk_no_check = 1; /* Checksum off by default */
1364 sock->ops = &ipx_dgram_ops;
1365 rc = 0;
1366out:
1367 return rc;
1368}
1369
1370static int ipx_release(struct socket *sock)
1371{
1372 struct sock *sk = sock->sk;
1373
1374 if (!sk)
1375 goto out;
1376
b0d0d915 1377 lock_sock(sk);
1da177e4
LT
1378 if (!sock_flag(sk, SOCK_DEAD))
1379 sk->sk_state_change(sk);
1380
1381 sock_set_flag(sk, SOCK_DEAD);
1382 sock->sk = NULL;
c2b42336 1383 sk_refcnt_debug_release(sk);
1da177e4 1384 ipx_destroy_socket(sk);
b0d0d915 1385 release_sock(sk);
674f2115 1386 sock_put(sk);
1da177e4
LT
1387out:
1388 return 0;
1389}
1390
1391/* caller must hold a reference to intrfc */
1392
4833ed09 1393static __be16 ipx_first_free_socketnum(struct ipx_interface *intrfc)
1da177e4
LT
1394{
1395 unsigned short socketNum = intrfc->if_sknum;
1396
1397 spin_lock_bh(&intrfc->if_sklist_lock);
1398
1399 if (socketNum < IPX_MIN_EPHEMERAL_SOCKET)
1400 socketNum = IPX_MIN_EPHEMERAL_SOCKET;
1401
4833ed09 1402 while (__ipxitf_find_socket(intrfc, htons(socketNum)))
1da177e4
LT
1403 if (socketNum > IPX_MAX_EPHEMERAL_SOCKET)
1404 socketNum = IPX_MIN_EPHEMERAL_SOCKET;
1405 else
1406 socketNum++;
1407
1408 spin_unlock_bh(&intrfc->if_sklist_lock);
1409 intrfc->if_sknum = socketNum;
1410
4833ed09 1411 return htons(socketNum);
1da177e4
LT
1412}
1413
83927ba0
AB
1414static int __ipx_bind(struct socket *sock,
1415 struct sockaddr *uaddr, int addr_len)
1da177e4
LT
1416{
1417 struct sock *sk = sock->sk;
1418 struct ipx_sock *ipxs = ipx_sk(sk);
1419 struct ipx_interface *intrfc;
1420 struct sockaddr_ipx *addr = (struct sockaddr_ipx *)uaddr;
1421 int rc = -EINVAL;
1422
1423 if (!sock_flag(sk, SOCK_ZAPPED) || addr_len != sizeof(struct sockaddr_ipx))
1424 goto out;
1425
1426 intrfc = ipxitf_find_using_net(addr->sipx_network);
1427 rc = -EADDRNOTAVAIL;
1428 if (!intrfc)
1429 goto out;
1430
1431 if (!addr->sipx_port) {
1432 addr->sipx_port = ipx_first_free_socketnum(intrfc);
1433 rc = -EINVAL;
1434 if (!addr->sipx_port)
1435 goto out_put;
1436 }
1437
1438 /* protect IPX system stuff like routing/sap */
1439 rc = -EACCES;
1440 if (ntohs(addr->sipx_port) < IPX_MIN_EPHEMERAL_SOCKET &&
1441 !capable(CAP_NET_ADMIN))
1442 goto out_put;
1443
1444 ipxs->port = addr->sipx_port;
1445
1446#ifdef CONFIG_IPX_INTERN
1447 if (intrfc == ipx_internal_net) {
1448 /* The source address is to be set explicitly if the
1449 * socket is to be bound on the internal network. If a
1450 * node number 0 was specified, the default is used.
1451 */
1452
1453 rc = -EINVAL;
1454 if (!memcmp(addr->sipx_node, ipx_broadcast_node, IPX_NODE_LEN))
1455 goto out_put;
1456 if (!memcmp(addr->sipx_node, ipx_this_node, IPX_NODE_LEN))
1457 memcpy(ipxs->node, intrfc->if_node, IPX_NODE_LEN);
1458 else
1459 memcpy(ipxs->node, addr->sipx_node, IPX_NODE_LEN);
1460
1461 rc = -EADDRINUSE;
1462 if (ipxitf_find_internal_socket(intrfc, ipxs->node,
1463 ipxs->port)) {
1464 SOCK_DEBUG(sk,
1465 "IPX: bind failed because port %X in use.\n",
4833ed09 1466 ntohs(addr->sipx_port));
1da177e4
LT
1467 goto out_put;
1468 }
1469 } else {
1470 /* Source addresses are easy. It must be our
1471 * network:node pair for an interface routed to IPX
1472 * with the ipx routing ioctl()
1473 */
1474
1475 memcpy(ipxs->node, intrfc->if_node, IPX_NODE_LEN);
1476
1477 rc = -EADDRINUSE;
1478 if (ipxitf_find_socket(intrfc, addr->sipx_port)) {
1479 SOCK_DEBUG(sk,
1480 "IPX: bind failed because port %X in use.\n",
4833ed09 1481 ntohs(addr->sipx_port));
1da177e4
LT
1482 goto out_put;
1483 }
1484 }
1485
1486#else /* !def CONFIG_IPX_INTERN */
1487
1488 /* Source addresses are easy. It must be our network:node pair for
1489 an interface routed to IPX with the ipx routing ioctl() */
1490
1491 rc = -EADDRINUSE;
1492 if (ipxitf_find_socket(intrfc, addr->sipx_port)) {
1493 SOCK_DEBUG(sk, "IPX: bind failed because port %X in use.\n",
1494 ntohs((int)addr->sipx_port));
1495 goto out_put;
1496 }
1497
1498#endif /* CONFIG_IPX_INTERN */
1499
1500 ipxitf_insert_socket(intrfc, sk);
1501 sock_reset_flag(sk, SOCK_ZAPPED);
1502
1503 rc = 0;
1504out_put:
1505 ipxitf_put(intrfc);
1506out:
1507 return rc;
1508}
1509
83927ba0
AB
1510static int ipx_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1511{
b0d0d915 1512 struct sock *sk = sock->sk;
83927ba0
AB
1513 int rc;
1514
b0d0d915 1515 lock_sock(sk);
83927ba0 1516 rc = __ipx_bind(sock, uaddr, addr_len);
b0d0d915 1517 release_sock(sk);
83927ba0
AB
1518
1519 return rc;
1520}
1521
1da177e4
LT
1522static int ipx_connect(struct socket *sock, struct sockaddr *uaddr,
1523 int addr_len, int flags)
1524{
1525 struct sock *sk = sock->sk;
1526 struct ipx_sock *ipxs = ipx_sk(sk);
1527 struct sockaddr_ipx *addr;
1528 int rc = -EINVAL;
1529 struct ipx_route *rt;
1530
1531 sk->sk_state = TCP_CLOSE;
1532 sock->state = SS_UNCONNECTED;
1533
b0d0d915 1534 lock_sock(sk);
1da177e4
LT
1535 if (addr_len != sizeof(*addr))
1536 goto out;
1537 addr = (struct sockaddr_ipx *)uaddr;
1538
1539 /* put the autobinding in */
1540 if (!ipxs->port) {
1541 struct sockaddr_ipx uaddr;
1542
1543 uaddr.sipx_port = 0;
1544 uaddr.sipx_network = 0;
1545
1546#ifdef CONFIG_IPX_INTERN
1547 rc = -ENETDOWN;
1548 if (!ipxs->intrfc)
1549 goto out; /* Someone zonked the iface */
1550 memcpy(uaddr.sipx_node, ipxs->intrfc->if_node,
1551 IPX_NODE_LEN);
1552#endif /* CONFIG_IPX_INTERN */
1553
83927ba0 1554 rc = __ipx_bind(sock, (struct sockaddr *)&uaddr,
1da177e4
LT
1555 sizeof(struct sockaddr_ipx));
1556 if (rc)
1557 goto out;
1558 }
1559
981c0ff6 1560 /* We can either connect to primary network or somewhere
1da177e4
LT
1561 * we can route to */
1562 rt = ipxrtr_lookup(addr->sipx_network);
1563 rc = -ENETUNREACH;
1564 if (!rt && !(!addr->sipx_network && ipx_primary_net))
1565 goto out;
1566
1567 ipxs->dest_addr.net = addr->sipx_network;
1568 ipxs->dest_addr.sock = addr->sipx_port;
1569 memcpy(ipxs->dest_addr.node, addr->sipx_node, IPX_NODE_LEN);
1570 ipxs->type = addr->sipx_type;
1571
1572 if (sock->type == SOCK_DGRAM) {
1573 sock->state = SS_CONNECTED;
1574 sk->sk_state = TCP_ESTABLISHED;
1575 }
1576
1577 if (rt)
1578 ipxrtr_put(rt);
1579 rc = 0;
1580out:
b0d0d915 1581 release_sock(sk);
1da177e4
LT
1582 return rc;
1583}
1584
1585
1586static int ipx_getname(struct socket *sock, struct sockaddr *uaddr,
1587 int *uaddr_len, int peer)
1588{
1589 struct ipx_address *addr;
1590 struct sockaddr_ipx sipx;
1591 struct sock *sk = sock->sk;
1592 struct ipx_sock *ipxs = ipx_sk(sk);
1593 int rc;
1594
1595 *uaddr_len = sizeof(struct sockaddr_ipx);
1596
b0d0d915 1597 lock_sock(sk);
1da177e4
LT
1598 if (peer) {
1599 rc = -ENOTCONN;
1600 if (sk->sk_state != TCP_ESTABLISHED)
1601 goto out;
1602
1603 addr = &ipxs->dest_addr;
1604 sipx.sipx_network = addr->net;
1605 sipx.sipx_port = addr->sock;
1606 memcpy(sipx.sipx_node, addr->node, IPX_NODE_LEN);
1607 } else {
1608 if (ipxs->intrfc) {
1609 sipx.sipx_network = ipxs->intrfc->if_netnum;
1610#ifdef CONFIG_IPX_INTERN
1611 memcpy(sipx.sipx_node, ipxs->node, IPX_NODE_LEN);
1612#else
1613 memcpy(sipx.sipx_node, ipxs->intrfc->if_node,
1614 IPX_NODE_LEN);
1615#endif /* CONFIG_IPX_INTERN */
1616
1617 } else {
1618 sipx.sipx_network = 0;
1619 memset(sipx.sipx_node, '\0', IPX_NODE_LEN);
1620 }
1621
1622 sipx.sipx_port = ipxs->port;
1623 }
1624
1625 sipx.sipx_family = AF_IPX;
1626 sipx.sipx_type = ipxs->type;
1627 sipx.sipx_zero = 0;
1628 memcpy(uaddr, &sipx, sizeof(sipx));
1629
1630 rc = 0;
1631out:
b0d0d915 1632 release_sock(sk);
1da177e4
LT
1633 return rc;
1634}
1635
f2ccd8fa 1636static int ipx_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
1da177e4
LT
1637{
1638 /* NULL here for pt means the packet was looped back */
1639 struct ipx_interface *intrfc;
1640 struct ipxhdr *ipx;
1641 u16 ipx_pktsize;
1642 int rc = 0;
981c0ff6 1643
721499e8 1644 if (!net_eq(dev_net(dev), &init_net))
e730c155
EB
1645 goto drop;
1646
981c0ff6
YH
1647 /* Not ours */
1648 if (skb->pkt_type == PACKET_OTHERHOST)
1649 goto drop;
1da177e4
LT
1650
1651 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
1652 goto out;
1653
7b1ba8de
SH
1654 if (!pskb_may_pull(skb, sizeof(struct ipxhdr)))
1655 goto drop;
1656
fff64257 1657 ipx_pktsize = ntohs(ipx_hdr(skb)->ipx_pktsize);
981c0ff6 1658
1da177e4 1659 /* Too small or invalid header? */
8b5cc5ef
SH
1660 if (ipx_pktsize < sizeof(struct ipxhdr) ||
1661 !pskb_may_pull(skb, ipx_pktsize))
1da177e4 1662 goto drop;
981c0ff6 1663
7b1ba8de 1664 ipx = ipx_hdr(skb);
1da177e4
LT
1665 if (ipx->ipx_checksum != IPX_NO_CHECKSUM &&
1666 ipx->ipx_checksum != ipx_cksum(ipx, ipx_pktsize))
1667 goto drop;
1668
1669 IPX_SKB_CB(skb)->ipx_tctrl = ipx->ipx_tctrl;
1670 IPX_SKB_CB(skb)->ipx_dest_net = ipx->ipx_dest.net;
1671 IPX_SKB_CB(skb)->ipx_source_net = ipx->ipx_source.net;
1672
1673 /* Determine what local ipx endpoint this is */
1674 intrfc = ipxitf_find_using_phys(dev, pt->type);
1675 if (!intrfc) {
1676 if (ipxcfg_auto_create_interfaces &&
4833ed09 1677 IPX_SKB_CB(skb)->ipx_dest_net) {
1da177e4
LT
1678 intrfc = ipxitf_auto_create(dev, pt->type);
1679 if (intrfc)
1680 ipxitf_hold(intrfc);
1681 }
1682
1683 if (!intrfc) /* Not one of ours */
1684 /* or invalid packet for auto creation */
1685 goto drop;
1686 }
1687
1688 rc = ipxitf_rcv(intrfc, skb);
1689 ipxitf_put(intrfc);
1690 goto out;
1691drop:
1692 kfree_skb(skb);
1693out:
1694 return rc;
1695}
1696
1697static int ipx_sendmsg(struct kiocb *iocb, struct socket *sock,
1698 struct msghdr *msg, size_t len)
1699{
1700 struct sock *sk = sock->sk;
1701 struct ipx_sock *ipxs = ipx_sk(sk);
342dfc30 1702 DECLARE_SOCKADDR(struct sockaddr_ipx *, usipx, msg->msg_name);
1da177e4
LT
1703 struct sockaddr_ipx local_sipx;
1704 int rc = -EINVAL;
1705 int flags = msg->msg_flags;
1706
b0d0d915 1707 lock_sock(sk);
1da177e4
LT
1708 /* Socket gets bound below anyway */
1709/* if (sk->sk_zapped)
1710 return -EIO; */ /* Socket not bound */
1711 if (flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
1712 goto out;
1713
1714 /* Max possible packet size limited by 16 bit pktsize in header */
1715 if (len >= 65535 - sizeof(struct ipxhdr))
1716 goto out;
1717
1718 if (usipx) {
1719 if (!ipxs->port) {
1720 struct sockaddr_ipx uaddr;
1721
1722 uaddr.sipx_port = 0;
1723 uaddr.sipx_network = 0;
1724#ifdef CONFIG_IPX_INTERN
1725 rc = -ENETDOWN;
1726 if (!ipxs->intrfc)
1727 goto out; /* Someone zonked the iface */
1728 memcpy(uaddr.sipx_node, ipxs->intrfc->if_node,
1729 IPX_NODE_LEN);
1730#endif
83927ba0 1731 rc = __ipx_bind(sock, (struct sockaddr *)&uaddr,
1da177e4
LT
1732 sizeof(struct sockaddr_ipx));
1733 if (rc)
1734 goto out;
1735 }
1736
1737 rc = -EINVAL;
1738 if (msg->msg_namelen < sizeof(*usipx) ||
1739 usipx->sipx_family != AF_IPX)
1740 goto out;
1741 } else {
1742 rc = -ENOTCONN;
1743 if (sk->sk_state != TCP_ESTABLISHED)
1744 goto out;
1745
1746 usipx = &local_sipx;
1747 usipx->sipx_family = AF_IPX;
1748 usipx->sipx_type = ipxs->type;
1749 usipx->sipx_port = ipxs->dest_addr.sock;
1750 usipx->sipx_network = ipxs->dest_addr.net;
1751 memcpy(usipx->sipx_node, ipxs->dest_addr.node, IPX_NODE_LEN);
1752 }
1753
1754 rc = ipxrtr_route_packet(sk, usipx, msg->msg_iov, len,
1755 flags & MSG_DONTWAIT);
1756 if (rc >= 0)
1757 rc = len;
1758out:
b0d0d915 1759 release_sock(sk);
1da177e4
LT
1760 return rc;
1761}
1762
1763
1764static int ipx_recvmsg(struct kiocb *iocb, struct socket *sock,
1765 struct msghdr *msg, size_t size, int flags)
1766{
1767 struct sock *sk = sock->sk;
1768 struct ipx_sock *ipxs = ipx_sk(sk);
342dfc30 1769 DECLARE_SOCKADDR(struct sockaddr_ipx *, sipx, msg->msg_name);
1da177e4
LT
1770 struct ipxhdr *ipx = NULL;
1771 struct sk_buff *skb;
1772 int copied, rc;
1773
b0d0d915 1774 lock_sock(sk);
1da177e4
LT
1775 /* put the autobinding in */
1776 if (!ipxs->port) {
1777 struct sockaddr_ipx uaddr;
1778
1779 uaddr.sipx_port = 0;
1780 uaddr.sipx_network = 0;
1781
1782#ifdef CONFIG_IPX_INTERN
1783 rc = -ENETDOWN;
1784 if (!ipxs->intrfc)
1785 goto out; /* Someone zonked the iface */
1786 memcpy(uaddr.sipx_node, ipxs->intrfc->if_node, IPX_NODE_LEN);
1787#endif /* CONFIG_IPX_INTERN */
1788
83927ba0 1789 rc = __ipx_bind(sock, (struct sockaddr *)&uaddr,
1da177e4
LT
1790 sizeof(struct sockaddr_ipx));
1791 if (rc)
1792 goto out;
1793 }
981c0ff6 1794
1da177e4
LT
1795 rc = -ENOTCONN;
1796 if (sock_flag(sk, SOCK_ZAPPED))
1797 goto out;
1798
1799 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1800 flags & MSG_DONTWAIT, &rc);
1801 if (!skb)
1802 goto out;
1803
1804 ipx = ipx_hdr(skb);
1805 copied = ntohs(ipx->ipx_pktsize) - sizeof(struct ipxhdr);
1806 if (copied > size) {
1807 copied = size;
1808 msg->msg_flags |= MSG_TRUNC;
1809 }
1810
1811 rc = skb_copy_datagram_iovec(skb, sizeof(struct ipxhdr), msg->msg_iov,
1812 copied);
1813 if (rc)
1814 goto out_free;
b7aa0bf7
ED
1815 if (skb->tstamp.tv64)
1816 sk->sk_stamp = skb->tstamp;
1da177e4 1817
1da177e4
LT
1818 if (sipx) {
1819 sipx->sipx_family = AF_IPX;
1820 sipx->sipx_port = ipx->ipx_source.sock;
1821 memcpy(sipx->sipx_node, ipx->ipx_source.node, IPX_NODE_LEN);
1822 sipx->sipx_network = IPX_SKB_CB(skb)->ipx_source_net;
1823 sipx->sipx_type = ipx->ipx_type;
1824 sipx->sipx_zero = 0;
f3d33426 1825 msg->msg_namelen = sizeof(*sipx);
1da177e4
LT
1826 }
1827 rc = copied;
1828
1829out_free:
1830 skb_free_datagram(sk, skb);
1831out:
b0d0d915 1832 release_sock(sk);
1da177e4
LT
1833 return rc;
1834}
1835
1836
1837static int ipx_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1838{
1839 int rc = 0;
1840 long amount = 0;
1841 struct sock *sk = sock->sk;
1842 void __user *argp = (void __user *)arg;
1843
b0d0d915 1844 lock_sock(sk);
1da177e4
LT
1845 switch (cmd) {
1846 case TIOCOUTQ:
31e6d363 1847 amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1da177e4
LT
1848 if (amount < 0)
1849 amount = 0;
1850 rc = put_user(amount, (int __user *)argp);
1851 break;
1852 case TIOCINQ: {
1853 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
1854 /* These two are safe on a single CPU system as only
1855 * user tasks fiddle here */
1856 if (skb)
1857 amount = skb->len - sizeof(struct ipxhdr);
1858 rc = put_user(amount, (int __user *)argp);
1859 break;
1860 }
1861 case SIOCADDRT:
1862 case SIOCDELRT:
1863 rc = -EPERM;
1864 if (capable(CAP_NET_ADMIN))
1865 rc = ipxrtr_ioctl(cmd, argp);
1866 break;
1867 case SIOCSIFADDR:
1868 case SIOCAIPXITFCRT:
1869 case SIOCAIPXPRISLT:
1870 rc = -EPERM;
1871 if (!capable(CAP_NET_ADMIN))
1872 break;
1873 case SIOCGIFADDR:
1874 rc = ipxitf_ioctl(cmd, argp);
1875 break;
1876 case SIOCIPXCFGDATA:
1877 rc = ipxcfg_get_config_data(argp);
1878 break;
1879 case SIOCIPXNCPCONN:
1880 /*
1881 * This socket wants to take care of the NCP connection
1882 * handed to us in arg.
1883 */
981c0ff6
YH
1884 rc = -EPERM;
1885 if (!capable(CAP_NET_ADMIN))
1da177e4
LT
1886 break;
1887 rc = get_user(ipx_sk(sk)->ipx_ncp_conn,
1888 (const unsigned short __user *)argp);
1889 break;
1890 case SIOCGSTAMP:
32e9072b 1891 rc = sock_get_timestamp(sk, argp);
1da177e4
LT
1892 break;
1893 case SIOCGIFDSTADDR:
1894 case SIOCSIFDSTADDR:
1895 case SIOCGIFBRDADDR:
1896 case SIOCSIFBRDADDR:
1897 case SIOCGIFNETMASK:
1898 case SIOCSIFNETMASK:
1899 rc = -EINVAL;
1900 break;
1901 default:
b5e5fa5e 1902 rc = -ENOIOCTLCMD;
1da177e4
LT
1903 break;
1904 }
b0d0d915 1905 release_sock(sk);
1da177e4
LT
1906
1907 return rc;
1908}
1909
f6c90b71
PV
1910
1911#ifdef CONFIG_COMPAT
1912static int ipx_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1913{
1914 /*
1915 * These 4 commands use same structure on 32bit and 64bit. Rest of IPX
1916 * commands is handled by generic ioctl code. As these commands are
1917 * SIOCPROTOPRIVATE..SIOCPROTOPRIVATE+3, they cannot be handled by generic
1918 * code.
1919 */
1920 switch (cmd) {
1921 case SIOCAIPXITFCRT:
1922 case SIOCAIPXPRISLT:
1923 case SIOCIPXCFGDATA:
1924 case SIOCIPXNCPCONN:
1925 return ipx_ioctl(sock, cmd, arg);
1926 default:
1927 return -ENOIOCTLCMD;
1928 }
1929}
1930#endif
1931
1932
1da177e4
LT
1933/*
1934 * Socket family declarations
1935 */
1936
ec1b4cf7 1937static const struct net_proto_family ipx_family_ops = {
1da177e4
LT
1938 .family = PF_IPX,
1939 .create = ipx_create,
1940 .owner = THIS_MODULE,
1941};
1942
83927ba0 1943static const struct proto_ops ipx_dgram_ops = {
1da177e4
LT
1944 .family = PF_IPX,
1945 .owner = THIS_MODULE,
1946 .release = ipx_release,
1947 .bind = ipx_bind,
1948 .connect = ipx_connect,
1949 .socketpair = sock_no_socketpair,
1950 .accept = sock_no_accept,
1951 .getname = ipx_getname,
b0d0d915 1952 .poll = datagram_poll,
1da177e4 1953 .ioctl = ipx_ioctl,
f6c90b71
PV
1954#ifdef CONFIG_COMPAT
1955 .compat_ioctl = ipx_compat_ioctl,
1956#endif
1da177e4
LT
1957 .listen = sock_no_listen,
1958 .shutdown = sock_no_shutdown, /* FIXME: support shutdown */
1959 .setsockopt = ipx_setsockopt,
1960 .getsockopt = ipx_getsockopt,
1961 .sendmsg = ipx_sendmsg,
1962 .recvmsg = ipx_recvmsg,
1963 .mmap = sock_no_mmap,
1964 .sendpage = sock_no_sendpage,
1965};
1966
7546dd97 1967static struct packet_type ipx_8023_packet_type __read_mostly = {
09640e63 1968 .type = cpu_to_be16(ETH_P_802_3),
1da177e4
LT
1969 .func = ipx_rcv,
1970};
1971
7546dd97 1972static struct packet_type ipx_dix_packet_type __read_mostly = {
09640e63 1973 .type = cpu_to_be16(ETH_P_IPX),
1da177e4
LT
1974 .func = ipx_rcv,
1975};
1976
1977static struct notifier_block ipx_dev_notifier = {
1978 .notifier_call = ipxitf_device_event,
1979};
1980
fa665ccf
SH
1981static const unsigned char ipx_8022_type = 0xE0;
1982static const unsigned char ipx_snap_id[5] = { 0x0, 0x0, 0x0, 0x81, 0x37 };
1983static const char ipx_EII_err_msg[] __initconst =
1da177e4 1984 KERN_CRIT "IPX: Unable to register with Ethernet II\n";
fa665ccf 1985static const char ipx_8023_err_msg[] __initconst =
1da177e4 1986 KERN_CRIT "IPX: Unable to register with 802.3\n";
fa665ccf 1987static const char ipx_llc_err_msg[] __initconst =
1da177e4 1988 KERN_CRIT "IPX: Unable to register with 802.2\n";
fa665ccf 1989static const char ipx_snap_err_msg[] __initconst =
1da177e4
LT
1990 KERN_CRIT "IPX: Unable to register with SNAP\n";
1991
1992static int __init ipx_init(void)
1993{
1994 int rc = proto_register(&ipx_proto, 1);
1995
1996 if (rc != 0)
1997 goto out;
1998
1999 sock_register(&ipx_family_ops);
2000
2001 pEII_datalink = make_EII_client();
2002 if (pEII_datalink)
2003 dev_add_pack(&ipx_dix_packet_type);
2004 else
2005 printk(ipx_EII_err_msg);
2006
2007 p8023_datalink = make_8023_client();
2008 if (p8023_datalink)
2009 dev_add_pack(&ipx_8023_packet_type);
2010 else
2011 printk(ipx_8023_err_msg);
2012
2013 p8022_datalink = register_8022_client(ipx_8022_type, ipx_rcv);
2014 if (!p8022_datalink)
2015 printk(ipx_llc_err_msg);
2016
2017 pSNAP_datalink = register_snap_client(ipx_snap_id, ipx_rcv);
2018 if (!pSNAP_datalink)
2019 printk(ipx_snap_err_msg);
2020
2021 register_netdevice_notifier(&ipx_dev_notifier);
2022 ipx_register_sysctl();
2023 ipx_proc_init();
2024out:
2025 return rc;
2026}
2027
2028static void __exit ipx_proto_finito(void)
2029{
2030 ipx_proc_exit();
2031 ipx_unregister_sysctl();
2032
2033 unregister_netdevice_notifier(&ipx_dev_notifier);
2034
2035 ipxitf_cleanup();
2036
1539b98b
JB
2037 if (pSNAP_datalink) {
2038 unregister_snap_client(pSNAP_datalink);
2039 pSNAP_datalink = NULL;
2040 }
1da177e4 2041
1539b98b
JB
2042 if (p8022_datalink) {
2043 unregister_8022_client(p8022_datalink);
2044 p8022_datalink = NULL;
2045 }
1da177e4
LT
2046
2047 dev_remove_pack(&ipx_8023_packet_type);
1539b98b
JB
2048 if (p8023_datalink) {
2049 destroy_8023_client(p8023_datalink);
2050 p8023_datalink = NULL;
2051 }
1da177e4
LT
2052
2053 dev_remove_pack(&ipx_dix_packet_type);
1539b98b
JB
2054 if (pEII_datalink) {
2055 destroy_EII_client(pEII_datalink);
2056 pEII_datalink = NULL;
2057 }
1da177e4
LT
2058
2059 proto_unregister(&ipx_proto);
2060 sock_unregister(ipx_family_ops.family);
2061}
2062
2063module_init(ipx_init);
2064module_exit(ipx_proto_finito);
2065MODULE_LICENSE("GPL");
2066MODULE_ALIAS_NETPROTO(PF_IPX);