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