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