This source file includes following definitions.
- ipxcfg_set_auto_create
- ipxcfg_set_auto_select
- ipxcfg_get_config_data
- ipx_remove_socket
- ipx_destroy_socket
- ipxitf_clear_primary_net
- ipxitf_find_using_phys
- ipxitf_find_using_net
- ipxitf_insert_socket
- ipxitf_find_socket
- ipxitf_down
- ipxitf_device_event
- ipxitf_def_skb_handler
- ipxitf_demux_socket
- ipxitf_adjust_skbuff
- ipxitf_send
- ipxitf_add_local_route
- ipxitf_rcv
- ipxitf_insert
- ipxitf_create_internal
- ipx_map_frame_type
- ipxitf_create
- ipxitf_delete
- ipxitf_auto_create
- ipxitf_ioctl
- ipxrtr_lookup
- ipxrtr_add_route
- ipxrtr_del_routes
- ipxrtr_create
- ipxrtr_delete
- ipxrtr_route_packet
- ipxrtr_route_skb
- ipxrtr_ioctl
- ipx_frame_name
- ipx_device_name
- ipx_interface_get_info
- ipx_get_info
- ipx_rt_get_info
- ipx_fcntl
- ipx_setsockopt
- ipx_getsockopt
- ipx_listen
- def_callback1
- def_callback2
- ipx_create
- ipx_release
- ipx_dup
- ipx_first_free_socketnum
- ipx_bind
- ipx_connect
- ipx_socketpair
- ipx_accept
- ipx_getname
- dump_data
- dump_addr
- dump_hdr
- dump_pkt
- ipx_rcv
- ipx_sendmsg
- ipx_recvmsg
- ipx_shutdown
- ipx_select
- ipx_ioctl
- ipx_proto_init
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 #include <linux/config.h>
50 #include <linux/errno.h>
51 #include <linux/types.h>
52 #include <linux/socket.h>
53 #include <linux/in.h>
54 #include <linux/kernel.h>
55 #include <linux/sched.h>
56 #include <linux/timer.h>
57 #include <linux/string.h>
58 #include <linux/sockios.h>
59 #include <linux/net.h>
60 #include <linux/ipx.h>
61 #include <linux/inet.h>
62 #include <linux/netdevice.h>
63 #include <linux/route.h>
64 #include <linux/skbuff.h>
65 #include <net/sock.h>
66 #include <asm/segment.h>
67 #include <asm/system.h>
68 #include <linux/fcntl.h>
69 #include <linux/mm.h>
70 #include <linux/termios.h>
71 #include <linux/interrupt.h>
72 #include <net/p8022.h>
73 #include <net/psnap.h>
74 #include <linux/proc_fs.h>
75 #include <linux/stat.h>
76 #include <linux/firewall.h>
77
78 #ifdef CONFIG_IPX
79
80 static unsigned char ipxcfg_max_hops = 16;
81 static char ipxcfg_auto_select_primary = 0;
82 static char ipxcfg_auto_create_interfaces = 0;
83
84
85 static struct datalink_proto *p8022_datalink = NULL;
86 static struct datalink_proto *pEII_datalink = NULL;
87 static struct datalink_proto *p8023_datalink = NULL;
88 static struct datalink_proto *pSNAP_datalink = NULL;
89
90 static ipx_interface *ipx_interfaces = NULL;
91 static ipx_route *ipx_routes = NULL;
92 static ipx_interface *ipx_internal_net = NULL;
93 static ipx_interface *ipx_primary_net = NULL;
94
95 static int
96 ipxcfg_set_auto_create(char val)
97 {
98 ipxcfg_auto_create_interfaces = val;
99 return 0;
100 }
101
102 static int
103 ipxcfg_set_auto_select(char val)
104 {
105 ipxcfg_auto_select_primary = val;
106 if (val && (ipx_primary_net == NULL))
107 ipx_primary_net = ipx_interfaces;
108 return 0;
109 }
110
111 static int
112 ipxcfg_get_config_data(ipx_config_data *arg)
113 {
114 ipx_config_data vals;
115
116 vals.ipxcfg_auto_create_interfaces = ipxcfg_auto_create_interfaces;
117 vals.ipxcfg_auto_select_primary = ipxcfg_auto_select_primary;
118 memcpy_tofs(arg, &vals, sizeof(vals));
119 return 0;
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 static void
136 ipx_remove_socket(ipx_socket *sk)
137 {
138 ipx_socket *s;
139 ipx_interface *intrfc;
140 unsigned long flags;
141
142 save_flags(flags);
143 cli();
144
145
146 intrfc = sk->ipx_intrfc;
147 if (intrfc == NULL) {
148 restore_flags(flags);
149 return;
150 }
151
152 s=intrfc->if_sklist;
153 if(s==sk) {
154 intrfc->if_sklist=s->next;
155 restore_flags(flags);
156 return;
157 }
158
159 while(s && s->next) {
160 if(s->next==sk) {
161 s->next=sk->next;
162 restore_flags(flags);
163 return;
164 }
165 s=s->next;
166 }
167 restore_flags(flags);
168 }
169
170
171
172
173
174
175
176
177 static void
178 ipx_destroy_socket(ipx_socket *sk)
179 {
180 struct sk_buff *skb;
181
182 ipx_remove_socket(sk);
183 while((skb=skb_dequeue(&sk->receive_queue))!=NULL) {
184 kfree_skb(skb,FREE_READ);
185 }
186
187 kfree_s(sk,sizeof(*sk));
188 }
189
190
191
192
193
194 static ipx_route * ipxrtr_lookup(unsigned long);
195
196 static void
197 ipxitf_clear_primary_net(void)
198 {
199 if (ipxcfg_auto_select_primary && (ipx_interfaces != NULL))
200 ipx_primary_net = ipx_interfaces;
201 else
202 ipx_primary_net = NULL;
203 }
204
205 static ipx_interface *
206 ipxitf_find_using_phys(struct device *dev, unsigned short datalink)
207 {
208 ipx_interface *i;
209
210 for (i=ipx_interfaces;
211 i && ((i->if_dev!=dev) || (i->if_dlink_type!=datalink));
212 i=i->if_next)
213 ;
214 return i;
215 }
216
217 static ipx_interface *
218 ipxitf_find_using_net(unsigned long net)
219 {
220 ipx_interface *i;
221
222 if (net == 0L)
223 return ipx_primary_net;
224
225 for (i=ipx_interfaces; i && (i->if_netnum!=net); i=i->if_next)
226 ;
227
228 return i;
229 }
230
231
232 static void
233 ipxitf_insert_socket(ipx_interface *intrfc, ipx_socket *sk)
234 {
235 ipx_socket *s;
236
237 sk->ipx_intrfc = intrfc;
238 sk->next = NULL;
239 if (intrfc->if_sklist == NULL) {
240 intrfc->if_sklist = sk;
241 } else {
242 for (s = intrfc->if_sklist; s->next != NULL; s = s->next)
243 ;
244 s->next = sk;
245 }
246 }
247
248 static ipx_socket *
249 ipxitf_find_socket(ipx_interface *intrfc, unsigned short port)
250 {
251 ipx_socket *s;
252
253 for (s=intrfc->if_sklist;
254 (s != NULL) && (s->ipx_port != port);
255 s=s->next)
256 ;
257
258 return s;
259 }
260
261 static void ipxrtr_del_routes(ipx_interface *);
262
263 static void
264 ipxitf_down(ipx_interface *intrfc)
265 {
266 ipx_interface *i;
267 ipx_socket *s, *t;
268
269
270 ipxrtr_del_routes(intrfc);
271
272
273 for (s = intrfc->if_sklist; s != NULL; ) {
274 s->err = ENOLINK;
275 s->error_report(s);
276 s->ipx_intrfc = NULL;
277 s->ipx_port = 0;
278 s->zapped=1;
279 t = s;
280 s = s->next;
281 t->next = NULL;
282 }
283 intrfc->if_sklist = NULL;
284
285
286 if (intrfc == ipx_interfaces) {
287 ipx_interfaces = intrfc->if_next;
288 } else {
289 for (i = ipx_interfaces;
290 (i != NULL) && (i->if_next != intrfc);
291 i = i->if_next)
292 ;
293 if ((i != NULL) && (i->if_next == intrfc))
294 i->if_next = intrfc->if_next;
295 }
296
297
298 if (intrfc == ipx_primary_net)
299 ipxitf_clear_primary_net();
300 if (intrfc == ipx_internal_net)
301 ipx_internal_net = NULL;
302
303 kfree_s(intrfc, sizeof(*intrfc));
304 }
305
306 static int
307 ipxitf_device_event(struct notifier_block *notifier, unsigned long event, void *ptr)
308 {
309 struct device *dev = ptr;
310 ipx_interface *i, *tmp;
311
312 if(event!=NETDEV_DOWN)
313 return NOTIFY_DONE;
314
315 for (i = ipx_interfaces; i != NULL; ) {
316
317 tmp = i->if_next;
318 if (i->if_dev == dev)
319 ipxitf_down(i);
320 i = tmp;
321
322 }
323
324 return NOTIFY_DONE;
325 }
326
327 static int ipxitf_def_skb_handler(struct sock *sock, struct sk_buff *skb)
328 {
329 int retval;
330
331 if((retval = sock_queue_rcv_skb(sock, skb))<0)
332 {
333
334
335
336
337
338
339
340
341 kfree_skb(skb,FREE_WRITE);
342 }
343 return retval;
344 }
345
346
347
348
349
350
351
352 static int ipxitf_demux_socket(ipx_interface *intrfc, struct sk_buff *skb, int copy)
353 {
354 ipx_packet *ipx = (ipx_packet *)(skb->h.raw);
355 ipx_socket *sock1 = NULL, *sock2 = NULL;
356 struct sk_buff *skb1 = NULL, *skb2 = NULL;
357
358 sock1 = ipxitf_find_socket(intrfc, ipx->ipx_dest.sock);
359
360
361
362
363
364
365
366
367
368 if (ipx_primary_net && (intrfc != ipx_primary_net))
369 {
370 switch (ntohs(ipx->ipx_dest.sock))
371 {
372 case 0x452:
373 case 0x453:
374 case 0x456:
375
376
377
378
379
380
381 sock2 = ipxitf_find_socket(ipx_primary_net,
382 ipx->ipx_dest.sock);
383 break;
384 default:
385 break;
386 }
387 }
388
389
390
391
392
393
394 if (sock1 == NULL && sock2 == NULL)
395 {
396 if (!copy)
397 kfree_skb(skb,FREE_WRITE);
398 return 0;
399 }
400
401
402
403
404
405
406
407
408
409
410
411
412
413 if(skb->sk && !copy)
414 {
415 skb->sk->wmem_alloc -= skb->truesize;
416 skb->sk=NULL;
417 }
418
419
420 if (copy)
421 {
422 skb1 = skb_clone(skb, GFP_ATOMIC);
423 if (skb1 != NULL)
424 skb1->arp = skb1->free = 1;
425 }
426 else
427 {
428 skb1 = skb;
429 }
430
431 if (skb1 == NULL)
432 return -ENOMEM;
433
434
435
436
437
438 if (sock1 && sock2)
439 {
440 skb2 = skb_clone(skb1, GFP_ATOMIC);
441 if (skb2 != NULL)
442 skb2->arp = skb2->free = 1;
443 }
444 else
445 skb2 = skb1;
446
447 if (sock1)
448 (void) ipxitf_def_skb_handler(sock1, skb1);
449
450 if (skb2 == NULL)
451 return -ENOMEM;
452
453 if (sock2)
454 (void) ipxitf_def_skb_handler(sock2, skb2);
455
456 return 0;
457 }
458
459 static struct sk_buff *
460 ipxitf_adjust_skbuff(ipx_interface *intrfc, struct sk_buff *skb)
461 {
462 struct sk_buff *skb2;
463 int in_offset = skb->h.raw - skb->head;
464 int out_offset = intrfc->if_ipx_offset;
465 int len;
466
467
468 if (in_offset >= out_offset) {
469 skb->arp = skb->free = 1;
470 return skb;
471 }
472
473
474 len = skb->len + out_offset;
475 skb2 = alloc_skb(len, GFP_ATOMIC);
476 if (skb2 != NULL) {
477 skb_reserve(skb2,out_offset);
478 skb2->h.raw=skb_put(skb2,skb->len);
479 skb2->free=1;
480 skb2->arp=1;
481 memcpy(skb2->h.raw, skb->h.raw, skb->len);
482 }
483 kfree_skb(skb, FREE_WRITE);
484 return skb2;
485 }
486
487 static int ipxitf_send(ipx_interface *intrfc, struct sk_buff *skb, char *node)
488 {
489 ipx_packet *ipx = (ipx_packet *)(skb->h.raw);
490 struct device *dev = intrfc->if_dev;
491 struct datalink_proto *dl = intrfc->if_dlink;
492 char dest_node[IPX_NODE_LEN];
493 int send_to_wire = 1;
494 int addr_len;
495
496
497
498
499
500
501 if ((dl == NULL) || (dev == NULL) || (dev->flags & IFF_LOOPBACK))
502 send_to_wire = 0;
503
504
505
506
507
508
509
510
511 if (ipx->ipx_dest.net == intrfc->if_netnum)
512 {
513
514
515
516 if (memcmp(intrfc->if_node, node, IPX_NODE_LEN) == 0)
517 return ipxitf_demux_socket(intrfc, skb, 0);
518
519
520
521 if (memcmp(ipx_broadcast_node, node, IPX_NODE_LEN) == 0)
522 {
523 ipxitf_demux_socket(intrfc, skb, send_to_wire);
524 if (!send_to_wire)
525 return 0;
526 }
527 }
528
529
530
531
532
533 if (ipx->ipx_source.net != intrfc->if_netnum)
534 {
535 if (++(ipx->ipx_tctrl) > ipxcfg_max_hops)
536 send_to_wire = 0;
537 }
538
539 if (!send_to_wire)
540 {
541
542
543
544
545
546
547
548
549 kfree_skb(skb,FREE_WRITE);
550 return 0;
551 }
552
553
554
555
556
557 addr_len = dev->addr_len;
558 if (memcmp(ipx_broadcast_node, node, IPX_NODE_LEN) == 0)
559 memcpy(dest_node, dev->broadcast, addr_len);
560 else
561 memcpy(dest_node, &(node[IPX_NODE_LEN-addr_len]), addr_len);
562
563
564
565
566
567 skb = ipxitf_adjust_skbuff(intrfc, skb);
568 if (skb == NULL)
569 return 0;
570
571
572 skb->dev = dev;
573 dl->datalink_header(dl, skb, dest_node);
574 #if 0
575
576
577
578
579 dump_pkt("IPX snd:", (ipx_packet *)skb->h.raw);
580 dump_data("ETH hdr:", skb->data, skb->h.raw - skb->data);
581 #endif
582
583
584
585
586
587 dev_queue_xmit(skb, dev, SOPRI_NORMAL);
588 return 0;
589 }
590
591 static int ipxrtr_add_route(unsigned long, ipx_interface *, unsigned char *);
592
593 static int ipxitf_add_local_route(ipx_interface *intrfc)
594 {
595 return ipxrtr_add_route(intrfc->if_netnum, intrfc, NULL);
596 }
597
598 static const char * ipx_frame_name(unsigned short);
599 static const char * ipx_device_name(ipx_interface *);
600 static int ipxrtr_route_skb(struct sk_buff *);
601
602 static int ipxitf_rcv(ipx_interface *intrfc, struct sk_buff *skb)
603 {
604 ipx_packet *ipx = (ipx_packet *) (skb->h.raw);
605 ipx_interface *i;
606
607 #ifdef CONFIG_FIREWALL
608
609
610
611
612 if (call_in_firewall(PF_IPX, skb, ipx)!=FW_ACCEPT)
613 {
614 kfree_skb(skb, FREE_READ);
615 return 0;
616 }
617
618 #endif
619
620
621 if ((intrfc->if_netnum == 0L) &&
622 (ipx->ipx_source.net == ipx->ipx_dest.net) &&
623 (ipx->ipx_source.net != 0L))
624 {
625
626
627
628
629 if ((i=ipxitf_find_using_net(ipx->ipx_source.net))==NULL)
630 {
631 intrfc->if_netnum = ipx->ipx_source.net;
632 (void) ipxitf_add_local_route(intrfc);
633 }
634 else
635 {
636 printk("IPX: Network number collision %lx\n\t%s %s and %s %s\n",
637 htonl(ipx->ipx_source.net),
638 ipx_device_name(i),
639 ipx_frame_name(i->if_dlink_type),
640 ipx_device_name(intrfc),
641 ipx_frame_name(intrfc->if_dlink_type));
642 }
643 }
644
645 if (ipx->ipx_dest.net == 0L)
646 ipx->ipx_dest.net = intrfc->if_netnum;
647 if (ipx->ipx_source.net == 0L)
648 ipx->ipx_source.net = intrfc->if_netnum;
649
650 if (intrfc->if_netnum != ipx->ipx_dest.net)
651 {
652 #ifdef CONFIG_FIREWALL
653
654
655
656 if (call_fw_firewall(PF_IPX, skb, ipx)!=FW_ACCEPT)
657 {
658 kfree_skb(skb, FREE_READ);
659 return 0;
660 }
661 #endif
662
663 if ((skb->pkt_type != PACKET_BROADCAST) &&
664 (skb->pkt_type != PACKET_MULTICAST))
665 return ipxrtr_route_skb(skb);
666
667 kfree_skb(skb,FREE_READ);
668 return 0;
669 }
670
671
672 if ((memcmp(ipx_broadcast_node, ipx->ipx_dest.node, IPX_NODE_LEN) == 0)
673 || (memcmp(intrfc->if_node, ipx->ipx_dest.node, IPX_NODE_LEN) == 0))
674 {
675 return ipxitf_demux_socket(intrfc, skb, 0);
676 }
677
678
679 kfree_skb(skb,FREE_READ);
680 return 0;
681 }
682
683 static void
684 ipxitf_insert(ipx_interface *intrfc)
685 {
686 ipx_interface *i;
687
688 intrfc->if_next = NULL;
689 if (ipx_interfaces == NULL) {
690 ipx_interfaces = intrfc;
691 } else {
692 for (i = ipx_interfaces; i->if_next != NULL; i = i->if_next)
693 ;
694 i->if_next = intrfc;
695 }
696
697 if (ipxcfg_auto_select_primary && (ipx_primary_net == NULL))
698 ipx_primary_net = intrfc;
699 }
700
701 static int
702 ipxitf_create_internal(ipx_interface_definition *idef)
703 {
704 ipx_interface *intrfc;
705
706
707 if (ipx_primary_net != NULL) return -EEXIST;
708
709
710 if (idef->ipx_network == 0L) return -EADDRNOTAVAIL;
711 if (ipxitf_find_using_net(idef->ipx_network) != NULL)
712 return -EADDRINUSE;
713
714 intrfc=(ipx_interface *)kmalloc(sizeof(ipx_interface),GFP_ATOMIC);
715 if (intrfc==NULL)
716 return -EAGAIN;
717 intrfc->if_dev=NULL;
718 intrfc->if_netnum=idef->ipx_network;
719 intrfc->if_dlink_type = 0;
720 intrfc->if_dlink = NULL;
721 intrfc->if_sklist = NULL;
722 intrfc->if_internal = 1;
723 intrfc->if_ipx_offset = 0;
724 intrfc->if_sknum = IPX_MIN_EPHEMERAL_SOCKET;
725 memcpy((char *)&(intrfc->if_node), idef->ipx_node, IPX_NODE_LEN);
726 ipx_internal_net = intrfc;
727 ipx_primary_net = intrfc;
728 ipxitf_insert(intrfc);
729 return ipxitf_add_local_route(intrfc);
730 }
731
732 static int
733 ipx_map_frame_type(unsigned char type)
734 {
735 switch (type) {
736 case IPX_FRAME_ETHERII: return htons(ETH_P_IPX);
737 case IPX_FRAME_8022: return htons(ETH_P_802_2);
738 case IPX_FRAME_SNAP: return htons(ETH_P_SNAP);
739 case IPX_FRAME_8023: return htons(ETH_P_802_3);
740 }
741 return 0;
742 }
743
744 static int
745 ipxitf_create(ipx_interface_definition *idef)
746 {
747 struct device *dev;
748 unsigned short dlink_type = 0;
749 struct datalink_proto *datalink = NULL;
750 ipx_interface *intrfc;
751
752 if (idef->ipx_special == IPX_INTERNAL)
753 return ipxitf_create_internal(idef);
754
755 if ((idef->ipx_special == IPX_PRIMARY) && (ipx_primary_net != NULL))
756 return -EEXIST;
757
758 if ((idef->ipx_network != 0L) &&
759 (ipxitf_find_using_net(idef->ipx_network) != NULL))
760 return -EADDRINUSE;
761
762 switch (idef->ipx_dlink_type) {
763 case IPX_FRAME_ETHERII:
764 dlink_type = htons(ETH_P_IPX);
765 datalink = pEII_datalink;
766 break;
767 case IPX_FRAME_8022:
768 dlink_type = htons(ETH_P_802_2);
769 datalink = p8022_datalink;
770 break;
771 case IPX_FRAME_SNAP:
772 dlink_type = htons(ETH_P_SNAP);
773 datalink = pSNAP_datalink;
774 break;
775 case IPX_FRAME_8023:
776 dlink_type = htons(ETH_P_802_3);
777 datalink = p8023_datalink;
778 break;
779 case IPX_FRAME_NONE:
780 default:
781 break;
782 }
783
784 if (datalink == NULL)
785 return -EPROTONOSUPPORT;
786
787 dev=dev_get(idef->ipx_device);
788 if (dev==NULL)
789 return -ENODEV;
790
791 if (!(dev->flags & IFF_UP))
792 return -ENETDOWN;
793
794
795 if(dev->addr_len>IPX_NODE_LEN)
796 return -EINVAL;
797
798 if ((intrfc = ipxitf_find_using_phys(dev, dlink_type)) == NULL) {
799
800
801 intrfc=(ipx_interface *)kmalloc(sizeof(ipx_interface),GFP_ATOMIC);
802 if (intrfc==NULL)
803 return -EAGAIN;
804 intrfc->if_dev=dev;
805 intrfc->if_netnum=idef->ipx_network;
806 intrfc->if_dlink_type = dlink_type;
807 intrfc->if_dlink = datalink;
808 intrfc->if_sklist = NULL;
809 intrfc->if_sknum = IPX_MIN_EPHEMERAL_SOCKET;
810
811 if ((idef->ipx_special == IPX_PRIMARY))
812 ipx_primary_net = intrfc;
813 intrfc->if_internal = 0;
814 intrfc->if_ipx_offset = dev->hard_header_len + datalink->header_length;
815 memset(intrfc->if_node, 0, IPX_NODE_LEN);
816 memcpy((char *)&(intrfc->if_node[IPX_NODE_LEN-dev->addr_len]), dev->dev_addr, dev->addr_len);
817
818 ipxitf_insert(intrfc);
819 }
820
821
822 if (intrfc->if_netnum == 0L)
823 return 0;
824
825 return ipxitf_add_local_route(intrfc);
826 }
827
828 static int
829 ipxitf_delete(ipx_interface_definition *idef)
830 {
831 struct device *dev = NULL;
832 unsigned short dlink_type = 0;
833 ipx_interface *intrfc;
834
835 if (idef->ipx_special == IPX_INTERNAL) {
836 if (ipx_internal_net != NULL) {
837 ipxitf_down(ipx_internal_net);
838 return 0;
839 }
840 return -ENOENT;
841 }
842
843 dlink_type = ipx_map_frame_type(idef->ipx_dlink_type);
844 if (dlink_type == 0)
845 return -EPROTONOSUPPORT;
846
847 dev=dev_get(idef->ipx_device);
848 if(dev==NULL) return -ENODEV;
849
850 intrfc = ipxitf_find_using_phys(dev, dlink_type);
851 if (intrfc != NULL) {
852 ipxitf_down(intrfc);
853 return 0;
854 }
855 return -EINVAL;
856 }
857
858 static ipx_interface *
859 ipxitf_auto_create(struct device *dev, unsigned short dlink_type)
860 {
861 struct datalink_proto *datalink = NULL;
862 ipx_interface *intrfc;
863
864 switch (htons(dlink_type)) {
865 case ETH_P_IPX: datalink = pEII_datalink; break;
866 case ETH_P_802_2: datalink = p8022_datalink; break;
867 case ETH_P_SNAP: datalink = pSNAP_datalink; break;
868 case ETH_P_802_3: datalink = p8023_datalink; break;
869 default: return NULL;
870 }
871
872 if (dev == NULL)
873 return NULL;
874
875
876 if(dev->addr_len>IPX_NODE_LEN) return NULL;
877
878 intrfc=(ipx_interface *)kmalloc(sizeof(ipx_interface),GFP_ATOMIC);
879 if (intrfc!=NULL) {
880 intrfc->if_dev=dev;
881 intrfc->if_netnum=0L;
882 intrfc->if_dlink_type = dlink_type;
883 intrfc->if_dlink = datalink;
884 intrfc->if_sklist = NULL;
885 intrfc->if_internal = 0;
886 intrfc->if_sknum = IPX_MIN_EPHEMERAL_SOCKET;
887 intrfc->if_ipx_offset = dev->hard_header_len +
888 datalink->header_length;
889 memset(intrfc->if_node, 0, IPX_NODE_LEN);
890 memcpy((char *)&(intrfc->if_node[IPX_NODE_LEN-dev->addr_len]),
891 dev->dev_addr, dev->addr_len);
892 ipxitf_insert(intrfc);
893 }
894
895 return intrfc;
896 }
897
898 static int
899 ipxitf_ioctl(unsigned int cmd, void *arg)
900 {
901 int err;
902 switch(cmd)
903 {
904 case SIOCSIFADDR:
905 {
906 struct ifreq ifr;
907 struct sockaddr_ipx *sipx;
908 ipx_interface_definition f;
909 err=verify_area(VERIFY_READ,arg,sizeof(ifr));
910 if(err)
911 return err;
912 memcpy_fromfs(&ifr,arg,sizeof(ifr));
913 sipx=(struct sockaddr_ipx *)&ifr.ifr_addr;
914 if(sipx->sipx_family!=AF_IPX)
915 return -EINVAL;
916 f.ipx_network=sipx->sipx_network;
917 memcpy(f.ipx_device, ifr.ifr_name, sizeof(f.ipx_device));
918 memcpy(f.ipx_node, sipx->sipx_node, IPX_NODE_LEN);
919 f.ipx_dlink_type=sipx->sipx_type;
920 f.ipx_special=sipx->sipx_special;
921 if(sipx->sipx_action==IPX_DLTITF)
922 return ipxitf_delete(&f);
923 else
924 return ipxitf_create(&f);
925 }
926 case SIOCGIFADDR:
927 {
928 struct ifreq ifr;
929 struct sockaddr_ipx *sipx;
930 ipx_interface *ipxif;
931 struct device *dev;
932 err=verify_area(VERIFY_WRITE,arg,sizeof(ifr));
933 if(err)
934 return err;
935 memcpy_fromfs(&ifr,arg,sizeof(ifr));
936 sipx=(struct sockaddr_ipx *)&ifr.ifr_addr;
937 dev=dev_get(ifr.ifr_name);
938 if(!dev)
939 return -ENODEV;
940 ipxif=ipxitf_find_using_phys(dev, ipx_map_frame_type(sipx->sipx_type));
941 if(ipxif==NULL)
942 return -EADDRNOTAVAIL;
943 sipx->sipx_network=ipxif->if_netnum;
944 memcpy(sipx->sipx_node, ipxif->if_node, sizeof(sipx->sipx_node));
945 memcpy_tofs(arg,&ifr,sizeof(ifr));
946 return 0;
947 }
948 case SIOCAIPXITFCRT:
949 err=verify_area(VERIFY_READ,arg,sizeof(char));
950 if(err)
951 return err;
952 return ipxcfg_set_auto_create(get_fs_byte(arg));
953 case SIOCAIPXPRISLT:
954 err=verify_area(VERIFY_READ,arg,sizeof(char));
955 if(err)
956 return err;
957 return ipxcfg_set_auto_select(get_fs_byte(arg));
958 default:
959 return -EINVAL;
960 }
961 }
962
963
964
965
966
967
968
969 static ipx_route *
970 ipxrtr_lookup(unsigned long net)
971 {
972 ipx_route *r;
973
974 for (r=ipx_routes; (r!=NULL) && (r->ir_net!=net); r=r->ir_next)
975 ;
976
977 return r;
978 }
979
980 static int
981 ipxrtr_add_route(unsigned long network, ipx_interface *intrfc, unsigned char *node)
982 {
983 ipx_route *rt;
984
985
986 rt = ipxrtr_lookup(network);
987 if (rt==NULL) {
988 rt=(ipx_route *)kmalloc(sizeof(ipx_route),GFP_ATOMIC);
989 if(rt==NULL)
990 return -EAGAIN;
991 rt->ir_next=ipx_routes;
992 ipx_routes=rt;
993 }
994
995 rt->ir_net = network;
996 rt->ir_intrfc = intrfc;
997 if (node == NULL) {
998 memset(rt->ir_router_node, '\0', IPX_NODE_LEN);
999 rt->ir_routed = 0;
1000 } else {
1001 memcpy(rt->ir_router_node, node, IPX_NODE_LEN);
1002 rt->ir_routed=1;
1003 }
1004 return 0;
1005 }
1006
1007 static void
1008 ipxrtr_del_routes(ipx_interface *intrfc)
1009 {
1010 ipx_route **r, *tmp;
1011
1012 for (r = &ipx_routes; (tmp = *r) != NULL; ) {
1013 if (tmp->ir_intrfc == intrfc) {
1014 *r = tmp->ir_next;
1015 kfree_s(tmp, sizeof(ipx_route));
1016 } else {
1017 r = &(tmp->ir_next);
1018 }
1019 }
1020 }
1021
1022 static int
1023 ipxrtr_create(ipx_route_definition *rd)
1024 {
1025 ipx_interface *intrfc;
1026
1027
1028 intrfc = ipxitf_find_using_net(rd->ipx_router_network);
1029 if (intrfc == NULL)
1030 return -ENETUNREACH;
1031
1032 return ipxrtr_add_route(rd->ipx_network, intrfc, rd->ipx_router_node);
1033 }
1034
1035
1036 static int
1037 ipxrtr_delete(long net)
1038 {
1039 ipx_route **r;
1040 ipx_route *tmp;
1041
1042 for (r = &ipx_routes; (tmp = *r) != NULL; ) {
1043 if (tmp->ir_net == net) {
1044 if (!(tmp->ir_routed)) {
1045
1046 return -EPERM;
1047 }
1048 *r = tmp->ir_next;
1049 kfree_s(tmp, sizeof(ipx_route));
1050 return 0;
1051 }
1052 r = &(tmp->ir_next);
1053 }
1054
1055 return -ENOENT;
1056 }
1057
1058
1059
1060
1061
1062 static int ipxrtr_route_packet(ipx_socket *sk, struct sockaddr_ipx *usipx, struct iovec *iov, int len)
1063 {
1064 struct sk_buff *skb;
1065 ipx_interface *intrfc;
1066 ipx_packet *ipx;
1067 int size;
1068 int ipx_offset;
1069 ipx_route *rt = NULL;
1070 int err;
1071
1072
1073 if ((usipx->sipx_network == 0L) && (ipx_primary_net != NULL))
1074 {
1075 usipx->sipx_network = ipx_primary_net->if_netnum;
1076 intrfc = ipx_primary_net;
1077 }
1078 else
1079 {
1080 rt = ipxrtr_lookup(usipx->sipx_network);
1081 if (rt==NULL) {
1082 return -ENETUNREACH;
1083 }
1084 intrfc = rt->ir_intrfc;
1085 }
1086
1087 ipx_offset = intrfc->if_ipx_offset;
1088 size=sizeof(ipx_packet)+len;
1089 size += ipx_offset;
1090
1091 skb=sock_alloc_send_skb(sk, size, 0, 0, &err);
1092 if(skb==NULL)
1093 return err;
1094
1095 skb_reserve(skb,ipx_offset);
1096 skb->free=1;
1097 skb->arp=1;
1098
1099
1100 ipx=(ipx_packet *)skb_put(skb,sizeof(ipx_packet));
1101 ipx->ipx_checksum=0xFFFF;
1102 ipx->ipx_pktsize=htons(len+sizeof(ipx_packet));
1103 ipx->ipx_tctrl=0;
1104 ipx->ipx_type=usipx->sipx_type;
1105 skb->h.raw = (unsigned char *)ipx;
1106
1107 ipx->ipx_source.net = sk->ipx_intrfc->if_netnum;
1108 memcpy(ipx->ipx_source.node, sk->ipx_intrfc->if_node, IPX_NODE_LEN);
1109 ipx->ipx_source.sock = sk->ipx_port;
1110 ipx->ipx_dest.net=usipx->sipx_network;
1111 memcpy(ipx->ipx_dest.node,usipx->sipx_node,IPX_NODE_LEN);
1112 ipx->ipx_dest.sock=usipx->sipx_port;
1113
1114 memcpy_fromiovec(skb_put(skb,len),iov,len);
1115
1116 #ifdef CONFIG_FIREWALL
1117 if(call_out_firewall(PF_IPX, skb, ipx)!=FW_ACCEPT)
1118 {
1119 kfree_skb(skb, FREE_WRITE);
1120 return -EPERM;
1121 }
1122 #endif
1123
1124 return ipxitf_send(intrfc, skb, (rt && rt->ir_routed) ?
1125 rt->ir_router_node : ipx->ipx_dest.node);
1126 }
1127
1128 static int
1129 ipxrtr_route_skb(struct sk_buff *skb)
1130 {
1131 ipx_packet *ipx = (ipx_packet *) (skb->h.raw);
1132 ipx_route *r;
1133 ipx_interface *i;
1134
1135 r = ipxrtr_lookup(ipx->ipx_dest.net);
1136 if (r == NULL) {
1137
1138 kfree_skb(skb,FREE_READ);
1139 return 0;
1140 }
1141 i = r->ir_intrfc;
1142 (void)ipxitf_send(i, skb, (r->ir_routed) ?
1143 r->ir_router_node : ipx->ipx_dest.node);
1144 return 0;
1145 }
1146
1147
1148
1149
1150
1151 static int ipxrtr_ioctl(unsigned int cmd, void *arg)
1152 {
1153 int err;
1154 struct rtentry rt;
1155 struct sockaddr_ipx *sg,*st;
1156
1157 err=verify_area(VERIFY_READ,arg,sizeof(rt));
1158 if(err)
1159 return err;
1160
1161 memcpy_fromfs(&rt,arg,sizeof(rt));
1162
1163 sg=(struct sockaddr_ipx *)&rt.rt_gateway;
1164 st=(struct sockaddr_ipx *)&rt.rt_dst;
1165
1166 if(!(rt.rt_flags&RTF_GATEWAY))
1167 return -EINVAL;
1168 if(sg->sipx_family!=AF_IPX)
1169 return -EINVAL;
1170 if(st->sipx_family!=AF_IPX)
1171 return -EINVAL;
1172
1173 switch(cmd)
1174 {
1175 case SIOCDELRT:
1176 return ipxrtr_delete(st->sipx_network);
1177 case SIOCADDRT:
1178 {
1179 struct ipx_route_definition f;
1180 f.ipx_network=st->sipx_network;
1181 f.ipx_router_network=sg->sipx_network;
1182 memcpy(f.ipx_router_node, sg->sipx_node, IPX_NODE_LEN);
1183 return ipxrtr_create(&f);
1184 }
1185 default:
1186 return -EINVAL;
1187 }
1188 }
1189
1190 static const char *
1191 ipx_frame_name(unsigned short frame)
1192 {
1193 switch (ntohs(frame)) {
1194 case ETH_P_IPX: return "EtherII";
1195 case ETH_P_802_2: return "802.2";
1196 case ETH_P_SNAP: return "SNAP";
1197 case ETH_P_802_3: return "802.3";
1198 default: return "None";
1199 }
1200 }
1201
1202 static const char *
1203 ipx_device_name(ipx_interface *intrfc)
1204 {
1205 return (intrfc->if_internal ? "Internal" :
1206 (intrfc->if_dev ? intrfc->if_dev->name : "Unknown"));
1207 }
1208
1209
1210 static int ipx_interface_get_info(char *buffer, char **start, off_t offset,
1211 int length, int dummy)
1212 {
1213 ipx_interface *i;
1214 int len=0;
1215 off_t pos=0;
1216 off_t begin=0;
1217
1218
1219
1220 len += sprintf (buffer,"%-11s%-15s%-9s%-11s%s\n", "Network",
1221 "Node_Address", "Primary", "Device", "Frame_Type");
1222 for (i = ipx_interfaces; i != NULL; i = i->if_next) {
1223 len += sprintf(buffer+len, "%08lX ", ntohl(i->if_netnum));
1224 len += sprintf (buffer+len,"%02X%02X%02X%02X%02X%02X ",
1225 i->if_node[0], i->if_node[1], i->if_node[2],
1226 i->if_node[3], i->if_node[4], i->if_node[5]);
1227 len += sprintf(buffer+len, "%-9s", (i == ipx_primary_net) ?
1228 "Yes" : "No");
1229 len += sprintf (buffer+len, "%-11s", ipx_device_name(i));
1230 len += sprintf (buffer+len, "%s\n",
1231 ipx_frame_name(i->if_dlink_type));
1232
1233
1234 pos=begin+len;
1235
1236 if(pos<offset) {
1237 len=0;
1238 begin=pos;
1239 }
1240 if(pos>offset+length)
1241 break;
1242 }
1243
1244
1245 *start=buffer+(offset-begin);
1246 len-=(offset-begin);
1247 if(len>length)
1248 len=length;
1249
1250 return len;
1251 }
1252
1253 static int ipx_get_info(char *buffer, char **start, off_t offset,
1254 int length, int dummy)
1255 {
1256 ipx_socket *s;
1257 ipx_interface *i;
1258 int len=0;
1259 off_t pos=0;
1260 off_t begin=0;
1261
1262
1263
1264 len += sprintf (buffer,"%-15s%-28s%-10s%-10s%-7s%s\n", "Local_Address",
1265 "Remote_Address", "Tx_Queue", "Rx_Queue",
1266 "State", "Uid");
1267 for (i = ipx_interfaces; i != NULL; i = i->if_next) {
1268 for (s = i->if_sklist; s != NULL; s = s->next) {
1269 len += sprintf (buffer+len,"%08lX:%04X ",
1270 htonl(i->if_netnum),
1271 htons(s->ipx_port));
1272 if (s->state!=TCP_ESTABLISHED) {
1273 len += sprintf(buffer+len, "%-28s", "Not_Connected");
1274 } else {
1275 len += sprintf (buffer+len,
1276 "%08lX:%02X%02X%02X%02X%02X%02X:%04X ",
1277 htonl(s->ipx_dest_addr.net),
1278 s->ipx_dest_addr.node[0], s->ipx_dest_addr.node[1],
1279 s->ipx_dest_addr.node[2], s->ipx_dest_addr.node[3],
1280 s->ipx_dest_addr.node[4], s->ipx_dest_addr.node[5],
1281 htons(s->ipx_dest_addr.sock));
1282 }
1283 len += sprintf (buffer+len,"%08lX %08lX ",
1284 s->wmem_alloc, s->rmem_alloc);
1285 len += sprintf (buffer+len,"%02X %03d\n",
1286 s->state, SOCK_INODE(s->socket)->i_uid);
1287
1288
1289 pos=begin+len;
1290
1291 if(pos<offset)
1292 {
1293 len=0;
1294 begin=pos;
1295 }
1296 if(pos>offset+length)
1297 break;
1298 }
1299 }
1300
1301
1302 *start=buffer+(offset-begin);
1303 len-=(offset-begin);
1304 if(len>length)
1305 len=length;
1306
1307 return len;
1308 }
1309
1310 static int ipx_rt_get_info(char *buffer, char **start, off_t offset,
1311 int length, int dummy)
1312 {
1313 ipx_route *rt;
1314 int len=0;
1315 off_t pos=0;
1316 off_t begin=0;
1317
1318 len += sprintf (buffer,"%-11s%-13s%s\n",
1319 "Network", "Router_Net", "Router_Node");
1320 for (rt = ipx_routes; rt != NULL; rt = rt->ir_next)
1321 {
1322 len += sprintf (buffer+len,"%08lX ", ntohl(rt->ir_net));
1323 if (rt->ir_routed) {
1324 len += sprintf (buffer+len,"%08lX %02X%02X%02X%02X%02X%02X\n",
1325 ntohl(rt->ir_intrfc->if_netnum),
1326 rt->ir_router_node[0], rt->ir_router_node[1],
1327 rt->ir_router_node[2], rt->ir_router_node[3],
1328 rt->ir_router_node[4], rt->ir_router_node[5]);
1329 } else {
1330 len += sprintf (buffer+len, "%-13s%s\n",
1331 "Directly", "Connected");
1332 }
1333 pos=begin+len;
1334 if(pos<offset)
1335 {
1336 len=0;
1337 begin=pos;
1338 }
1339 if(pos>offset+length)
1340 break;
1341 }
1342 *start=buffer+(offset-begin);
1343 len-=(offset-begin);
1344 if(len>length)
1345 len=length;
1346 return len;
1347 }
1348
1349
1350
1351
1352
1353
1354
1355 static int ipx_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
1356 {
1357 switch(cmd)
1358 {
1359 default:
1360 return(-EINVAL);
1361 }
1362 }
1363
1364 static int ipx_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
1365 {
1366 ipx_socket *sk;
1367 int err,opt;
1368
1369 sk=(ipx_socket *)sock->data;
1370
1371 if(optval==NULL)
1372 return(-EINVAL);
1373
1374 err=verify_area(VERIFY_READ,optval,sizeof(int));
1375 if(err)
1376 return err;
1377 opt=get_fs_long((unsigned long *)optval);
1378
1379 switch(level)
1380 {
1381 case SOL_IPX:
1382 switch(optname)
1383 {
1384 case IPX_TYPE:
1385 sk->ipx_type=opt;
1386 return 0;
1387 default:
1388 return -EOPNOTSUPP;
1389 }
1390 break;
1391
1392 case SOL_SOCKET:
1393 return sock_setsockopt(sk,level,optname,optval,optlen);
1394
1395 default:
1396 return -EOPNOTSUPP;
1397 }
1398 }
1399
1400 static int ipx_getsockopt(struct socket *sock, int level, int optname,
1401 char *optval, int *optlen)
1402 {
1403 ipx_socket *sk;
1404 int val=0;
1405 int err;
1406
1407 sk=(ipx_socket *)sock->data;
1408
1409 switch(level)
1410 {
1411
1412 case SOL_IPX:
1413 switch(optname)
1414 {
1415 case IPX_TYPE:
1416 val=sk->ipx_type;
1417 break;
1418 default:
1419 return -ENOPROTOOPT;
1420 }
1421 break;
1422
1423 case SOL_SOCKET:
1424 return sock_getsockopt(sk,level,optname,optval,optlen);
1425
1426 default:
1427 return -EOPNOTSUPP;
1428 }
1429 err=verify_area(VERIFY_WRITE,optlen,sizeof(int));
1430 if(err)
1431 return err;
1432 put_fs_long(sizeof(int),(unsigned long *)optlen);
1433 err=verify_area(VERIFY_WRITE,optval,sizeof(int));
1434 put_fs_long(val,(unsigned long *)optval);
1435 return(0);
1436 }
1437
1438 static int ipx_listen(struct socket *sock, int backlog)
1439 {
1440 return -EOPNOTSUPP;
1441 }
1442
1443 static void def_callback1(struct sock *sk)
1444 {
1445 if(!sk->dead)
1446 wake_up_interruptible(sk->sleep);
1447 }
1448
1449 static void def_callback2(struct sock *sk, int len)
1450 {
1451 if(!sk->dead)
1452 {
1453 wake_up_interruptible(sk->sleep);
1454 sock_wake_async(sk->socket, 1);
1455 }
1456 }
1457
1458 static int
1459 ipx_create(struct socket *sock, int protocol)
1460 {
1461 ipx_socket *sk;
1462 sk=(ipx_socket *)kmalloc(sizeof(*sk),GFP_KERNEL);
1463 if(sk==NULL)
1464 return(-ENOMEM);
1465 switch(sock->type)
1466 {
1467 case SOCK_DGRAM:
1468 break;
1469 default:
1470 kfree_s((void *)sk,sizeof(*sk));
1471 return(-ESOCKTNOSUPPORT);
1472 }
1473 sk->dead=0;
1474 sk->next=NULL;
1475 sk->broadcast=0;
1476 sk->rcvbuf=SK_RMEM_MAX;
1477 sk->sndbuf=SK_WMEM_MAX;
1478 sk->wmem_alloc=0;
1479 sk->rmem_alloc=0;
1480 sk->inuse=0;
1481 sk->shutdown=0;
1482 sk->prot=NULL;
1483 sk->err=0;
1484 skb_queue_head_init(&sk->receive_queue);
1485 skb_queue_head_init(&sk->write_queue);
1486 sk->send_head=NULL;
1487 skb_queue_head_init(&sk->back_log);
1488 sk->state=TCP_CLOSE;
1489 sk->socket=sock;
1490 sk->type=sock->type;
1491 sk->ipx_type=0;
1492 sk->debug=0;
1493 sk->ipx_intrfc = NULL;
1494 memset(&sk->ipx_dest_addr,'\0',sizeof(sk->ipx_dest_addr));
1495 sk->ipx_port = 0;
1496 sk->mtu=IPX_MTU;
1497
1498 if(sock!=NULL)
1499 {
1500 sock->data=(void *)sk;
1501 sk->sleep=sock->wait;
1502 }
1503
1504 sk->state_change=def_callback1;
1505 sk->data_ready=def_callback2;
1506 sk->write_space=def_callback1;
1507 sk->error_report=def_callback1;
1508
1509 sk->zapped=1;
1510 return 0;
1511 }
1512
1513 static int ipx_release(struct socket *sock, struct socket *peer)
1514 {
1515 ipx_socket *sk=(ipx_socket *)sock->data;
1516 if(sk==NULL)
1517 return(0);
1518 if(!sk->dead)
1519 sk->state_change(sk);
1520 sk->dead=1;
1521 sock->data=NULL;
1522 ipx_destroy_socket(sk);
1523 return(0);
1524 }
1525
1526 static int ipx_dup(struct socket *newsock,struct socket *oldsock)
1527 {
1528 return(ipx_create(newsock,SOCK_DGRAM));
1529 }
1530
1531 static unsigned short
1532 ipx_first_free_socketnum(ipx_interface *intrfc)
1533 {
1534 unsigned short socketNum = intrfc->if_sknum;
1535
1536 if (socketNum < IPX_MIN_EPHEMERAL_SOCKET)
1537 socketNum = IPX_MIN_EPHEMERAL_SOCKET;
1538
1539 while (ipxitf_find_socket(intrfc, ntohs(socketNum)) != NULL)
1540 if (socketNum > IPX_MAX_EPHEMERAL_SOCKET)
1541 socketNum = IPX_MIN_EPHEMERAL_SOCKET;
1542 else
1543 socketNum++;
1544
1545 intrfc->if_sknum = socketNum;
1546 return ntohs(socketNum);
1547 }
1548
1549 static int ipx_bind(struct socket *sock, struct sockaddr *uaddr,int addr_len)
1550 {
1551 ipx_socket *sk;
1552 ipx_interface *intrfc;
1553 struct sockaddr_ipx *addr=(struct sockaddr_ipx *)uaddr;
1554
1555 sk=(ipx_socket *)sock->data;
1556
1557 if(sk->zapped==0)
1558 return -EIO;
1559
1560 if(addr_len!=sizeof(struct sockaddr_ipx))
1561 return -EINVAL;
1562
1563 intrfc = ipxitf_find_using_net(addr->sipx_network);
1564 if (intrfc == NULL)
1565 return -EADDRNOTAVAIL;
1566
1567 if (addr->sipx_port == 0) {
1568 addr->sipx_port = ipx_first_free_socketnum(intrfc);
1569 if (addr->sipx_port == 0)
1570 return -EINVAL;
1571 }
1572
1573 if(ntohs(addr->sipx_port)<IPX_MIN_EPHEMERAL_SOCKET && !suser())
1574 return -EPERM;
1575
1576
1577
1578
1579 if(ipxitf_find_socket(intrfc, addr->sipx_port)!=NULL) {
1580 if(sk->debug)
1581 printk("IPX: bind failed because port %X in use.\n",
1582 (int)addr->sipx_port);
1583 return -EADDRINUSE;
1584 }
1585
1586 sk->ipx_port=addr->sipx_port;
1587 ipxitf_insert_socket(intrfc, sk);
1588 sk->zapped=0;
1589 if(sk->debug)
1590 printk("IPX: socket is bound.\n");
1591 return 0;
1592 }
1593
1594 static int ipx_connect(struct socket *sock, struct sockaddr *uaddr,
1595 int addr_len, int flags)
1596 {
1597 ipx_socket *sk=(ipx_socket *)sock->data;
1598 struct sockaddr_ipx *addr;
1599
1600 sk->state = TCP_CLOSE;
1601 sock->state = SS_UNCONNECTED;
1602
1603 if(addr_len!=sizeof(*addr))
1604 return(-EINVAL);
1605 addr=(struct sockaddr_ipx *)uaddr;
1606
1607 if(sk->ipx_port==0)
1608
1609 {
1610 struct sockaddr_ipx uaddr;
1611 int ret;
1612
1613 uaddr.sipx_port = 0;
1614 uaddr.sipx_network = 0L;
1615 ret = ipx_bind (sock, (struct sockaddr *)&uaddr, sizeof(struct sockaddr_ipx));
1616 if (ret != 0) return (ret);
1617 }
1618
1619 if(ipxrtr_lookup(addr->sipx_network)==NULL)
1620 return -ENETUNREACH;
1621 sk->ipx_dest_addr.net=addr->sipx_network;
1622 sk->ipx_dest_addr.sock=addr->sipx_port;
1623 memcpy(sk->ipx_dest_addr.node,addr->sipx_node,IPX_NODE_LEN);
1624 sk->ipx_type=addr->sipx_type;
1625 sock->state = SS_CONNECTED;
1626 sk->state=TCP_ESTABLISHED;
1627 return 0;
1628 }
1629
1630 static int ipx_socketpair(struct socket *sock1, struct socket *sock2)
1631 {
1632 return(-EOPNOTSUPP);
1633 }
1634
1635 static int ipx_accept(struct socket *sock, struct socket *newsock, int flags)
1636 {
1637 if(newsock->data)
1638 kfree_s(newsock->data,sizeof(ipx_socket));
1639 return -EOPNOTSUPP;
1640 }
1641
1642 static int ipx_getname(struct socket *sock, struct sockaddr *uaddr,
1643 int *uaddr_len, int peer)
1644 {
1645 ipx_address *addr;
1646 struct sockaddr_ipx sipx;
1647 ipx_socket *sk;
1648
1649 sk=(ipx_socket *)sock->data;
1650
1651 *uaddr_len = sizeof(struct sockaddr_ipx);
1652
1653 if(peer) {
1654 if(sk->state!=TCP_ESTABLISHED)
1655 return -ENOTCONN;
1656 addr=&sk->ipx_dest_addr;
1657 sipx.sipx_network = addr->net;
1658 memcpy(sipx.sipx_node,addr->node,IPX_NODE_LEN);
1659 sipx.sipx_port = addr->sock;
1660 } else {
1661 if (sk->ipx_intrfc != NULL) {
1662 sipx.sipx_network = sk->ipx_intrfc->if_netnum;
1663 memcpy(sipx.sipx_node, sk->ipx_intrfc->if_node,
1664 IPX_NODE_LEN);
1665 } else {
1666 sipx.sipx_network = 0L;
1667 memset(sipx.sipx_node, '\0', IPX_NODE_LEN);
1668 }
1669 sipx.sipx_port = sk->ipx_port;
1670 }
1671
1672 sipx.sipx_family = AF_IPX;
1673 sipx.sipx_type = sk->ipx_type;
1674 memcpy(uaddr,&sipx,sizeof(sipx));
1675 return 0;
1676 }
1677
1678 #if 0
1679
1680
1681
1682 void dump_data(char *str,unsigned char *d, int len) {
1683 static char h2c[] = "0123456789ABCDEF";
1684 int l,i;
1685 char *p, b[64];
1686 for (l=0;len > 0 && l<16;l++) {
1687 p = b;
1688 for (i=0; i < 8 ; i++, --len) {
1689 if (len > 0) {
1690 *(p++) = h2c[(d[i] >> 4) & 0x0f];
1691 *(p++) = h2c[d[i] & 0x0f];
1692 }
1693 else {
1694 *(p++) = ' ';
1695 *(p++) = ' ';
1696 }
1697 *(p++) = ' ';
1698 }
1699 *(p++) = '-';
1700 *(p++) = ' ';
1701 len += 8;
1702 for (i=0; i < 8 ; i++, --len)
1703 if (len > 0)
1704 *(p++) = ' '<= d[i] && d[i]<'\177' ? d[i] : '.';
1705 else
1706 *(p++) = ' ';
1707 *p = '\000';
1708 d += i;
1709 printk("%s-%04X: %s\n",str,l*8,b);
1710 }
1711 }
1712
1713 void dump_addr(char *str,ipx_address *p) {
1714 printk("%s: %08X:%02X%02X%02X%02X%02X%02X:%04X\n",
1715 str,ntohl(p->net),p->node[0],p->node[1],p->node[2],
1716 p->node[3],p->node[4],p->node[5],ntohs(p->sock));
1717 }
1718
1719 void dump_hdr(char *str,ipx_packet *p) {
1720 printk("%s: CHKSUM=%04X SIZE=%d (%04X) HOPS=%d (%02X) TYPE=%02X\n",
1721 str,p->ipx_checksum,ntohs(p->ipx_pktsize),ntohs(p->ipx_pktsize),
1722 p->ipx_tctrl,p->ipx_tctrl,p->ipx_type);
1723 dump_addr(" IPX-DST",&p->ipx_dest);
1724 dump_addr(" IPX-SRC",&p->ipx_source);
1725 }
1726
1727 void dump_pkt(char *str,ipx_packet *p) {
1728 int len = ntohs(p->ipx_pktsize);
1729 dump_hdr(str,p);
1730 if (len > 30)
1731 dump_data(str,(unsigned char *)p + 30, len - 30);
1732 }
1733 #endif
1734
1735 int ipx_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
1736 {
1737
1738 ipx_interface *intrfc;
1739 ipx_packet *ipx;
1740
1741
1742 ipx=(ipx_packet *)skb->h.raw;
1743
1744 if(ipx->ipx_checksum!=IPX_NO_CHECKSUM) {
1745
1746
1747
1748 kfree_skb(skb,FREE_READ);
1749 return 0;
1750 }
1751
1752
1753 if(htons(ipx->ipx_pktsize)<sizeof(ipx_packet)) {
1754 kfree_skb(skb,FREE_READ);
1755 return 0;
1756 }
1757
1758
1759 intrfc = ipxitf_find_using_phys(dev, pt->type);
1760 if (intrfc == NULL) {
1761 if (ipxcfg_auto_create_interfaces) {
1762 intrfc = ipxitf_auto_create(dev, pt->type);
1763 }
1764
1765 if (intrfc == NULL) {
1766
1767 kfree_skb(skb,FREE_READ);
1768 return 0;
1769 }
1770 }
1771
1772 return ipxitf_rcv(intrfc, skb);
1773 }
1774
1775 static int ipx_sendmsg(struct socket *sock, struct msghdr *msg, int len, int noblock,
1776 int flags)
1777 {
1778 ipx_socket *sk=(ipx_socket *)sock->data;
1779 struct sockaddr_ipx *usipx=(struct sockaddr_ipx *)msg->msg_name;
1780 struct sockaddr_ipx local_sipx;
1781 int retval;
1782
1783 if (sk->zapped) return -EIO;
1784 if(flags) return -EINVAL;
1785
1786 if(usipx)
1787 {
1788 if(sk->ipx_port == 0)
1789 {
1790 struct sockaddr_ipx uaddr;
1791 int ret;
1792
1793 uaddr.sipx_port = 0;
1794 uaddr.sipx_network = 0L;
1795 ret = ipx_bind (sock, (struct sockaddr *)&uaddr, sizeof(struct sockaddr_ipx));
1796 if (ret != 0) return ret;
1797 }
1798
1799 if(msg->msg_namelen <sizeof(*usipx))
1800 return -EINVAL;
1801 if(usipx->sipx_family != AF_IPX)
1802 return -EINVAL;
1803 }
1804 else
1805 {
1806 if(sk->state!=TCP_ESTABLISHED)
1807 return -ENOTCONN;
1808 usipx=&local_sipx;
1809 usipx->sipx_family=AF_IPX;
1810 usipx->sipx_type=sk->ipx_type;
1811 usipx->sipx_port=sk->ipx_dest_addr.sock;
1812 usipx->sipx_network=sk->ipx_dest_addr.net;
1813 memcpy(usipx->sipx_node,sk->ipx_dest_addr.node,IPX_NODE_LEN);
1814 }
1815
1816 retval = ipxrtr_route_packet(sk, usipx, msg->msg_iov, len);
1817 if (retval < 0) return retval;
1818
1819 return len;
1820 }
1821
1822
1823 static int ipx_recvmsg(struct socket *sock, struct msghdr *msg, int size, int noblock,
1824 int flags, int *addr_len)
1825 {
1826 ipx_socket *sk=(ipx_socket *)sock->data;
1827 struct sockaddr_ipx *sipx=(struct sockaddr_ipx *)msg->msg_name;
1828 struct ipx_packet *ipx = NULL;
1829 int copied = 0;
1830 int truesize;
1831 struct sk_buff *skb;
1832 int er;
1833
1834 if(sk->err)
1835 return sock_error(sk);
1836
1837 if (sk->zapped)
1838 return -EIO;
1839
1840
1841 skb=skb_recv_datagram(sk,flags,noblock,&er);
1842 if(skb==NULL)
1843 return er;
1844
1845 if(addr_len)
1846 *addr_len=sizeof(*sipx);
1847
1848 ipx = (ipx_packet *)(skb->h.raw);
1849 truesize=ntohs(ipx->ipx_pktsize) - sizeof(ipx_packet);
1850 copied = (truesize > size) ? size : truesize;
1851 skb_copy_datagram_iovec(skb,sizeof(struct ipx_packet),msg->msg_iov,copied);
1852
1853 if(sipx)
1854 {
1855 sipx->sipx_family=AF_IPX;
1856 sipx->sipx_port=ipx->ipx_source.sock;
1857 memcpy(sipx->sipx_node,ipx->ipx_source.node,IPX_NODE_LEN);
1858 sipx->sipx_network=ipx->ipx_source.net;
1859 sipx->sipx_type = ipx->ipx_type;
1860 }
1861 skb_free_datagram(skb);
1862 return(truesize);
1863 }
1864
1865 static int ipx_shutdown(struct socket *sk,int how)
1866 {
1867 return -EOPNOTSUPP;
1868 }
1869
1870 static int ipx_select(struct socket *sock , int sel_type, select_table *wait)
1871 {
1872 ipx_socket *sk=(ipx_socket *)sock->data;
1873
1874 return datagram_select(sk,sel_type,wait);
1875 }
1876
1877 static int ipx_ioctl(struct socket *sock,unsigned int cmd, unsigned long arg)
1878 {
1879 int err;
1880 long amount=0;
1881 ipx_socket *sk=(ipx_socket *)sock->data;
1882
1883 switch(cmd)
1884 {
1885 case TIOCOUTQ:
1886 err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(unsigned long));
1887 if(err)
1888 return err;
1889 amount=sk->sndbuf-sk->wmem_alloc;
1890 if(amount<0)
1891 amount=0;
1892 put_fs_long(amount,(unsigned long *)arg);
1893 return 0;
1894 case TIOCINQ:
1895 {
1896 struct sk_buff *skb;
1897
1898 if((skb=skb_peek(&sk->receive_queue))!=NULL)
1899 amount=skb->len;
1900 err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(unsigned long));
1901 if(err)
1902 return err;
1903 put_fs_long(amount,(unsigned long *)arg);
1904 return 0;
1905 }
1906 case SIOCADDRT:
1907 case SIOCDELRT:
1908 if(!suser())
1909 return -EPERM;
1910 return(ipxrtr_ioctl(cmd,(void *)arg));
1911 case SIOCSIFADDR:
1912 case SIOCGIFADDR:
1913 case SIOCAIPXITFCRT:
1914 case SIOCAIPXPRISLT:
1915 if(!suser())
1916 return -EPERM;
1917 return(ipxitf_ioctl(cmd,(void *)arg));
1918 case SIOCIPXCFGDATA:
1919 {
1920 err=verify_area(VERIFY_WRITE,(void *)arg,
1921 sizeof(ipx_config_data));
1922 if(err) return err;
1923 return(ipxcfg_get_config_data((void *)arg));
1924 }
1925 case SIOCGSTAMP:
1926 if (sk)
1927 {
1928 if(sk->stamp.tv_sec==0)
1929 return -ENOENT;
1930 err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval));
1931 if(err)
1932 return err;
1933 memcpy_tofs((void *)arg,&sk->stamp,sizeof(struct timeval));
1934 return 0;
1935 }
1936 return -EINVAL;
1937 case SIOCGIFDSTADDR:
1938 case SIOCSIFDSTADDR:
1939 case SIOCGIFBRDADDR:
1940 case SIOCSIFBRDADDR:
1941 case SIOCGIFNETMASK:
1942 case SIOCSIFNETMASK:
1943 return -EINVAL;
1944 default:
1945 return(dev_ioctl(cmd,(void *) arg));
1946 }
1947
1948 return(0);
1949 }
1950
1951 static struct proto_ops ipx_proto_ops = {
1952 AF_IPX,
1953
1954 ipx_create,
1955 ipx_dup,
1956 ipx_release,
1957 ipx_bind,
1958 ipx_connect,
1959 ipx_socketpair,
1960 ipx_accept,
1961 ipx_getname,
1962 ipx_select,
1963 ipx_ioctl,
1964 ipx_listen,
1965 ipx_shutdown,
1966 ipx_setsockopt,
1967 ipx_getsockopt,
1968 ipx_fcntl,
1969 ipx_sendmsg,
1970 ipx_recvmsg
1971 };
1972
1973
1974
1975 static struct packet_type ipx_8023_packet_type =
1976
1977 {
1978 0,
1979 NULL,
1980 ipx_rcv,
1981 NULL,
1982 NULL,
1983 };
1984
1985 static struct packet_type ipx_dix_packet_type =
1986 {
1987 0,
1988 NULL,
1989 ipx_rcv,
1990 NULL,
1991 NULL,
1992 };
1993
1994 static struct notifier_block ipx_dev_notifier={
1995 ipxitf_device_event,
1996 NULL,
1997 0
1998 };
1999
2000
2001 extern struct datalink_proto *make_EII_client(void);
2002 extern struct datalink_proto *make_8023_client(void);
2003
2004 void ipx_proto_init(struct net_proto *pro)
2005 {
2006 unsigned char val = 0xE0;
2007 unsigned char snapval[5] = { 0x0, 0x0, 0x0, 0x81, 0x37 };
2008
2009 (void) sock_register(ipx_proto_ops.family, &ipx_proto_ops);
2010
2011 pEII_datalink = make_EII_client();
2012 ipx_dix_packet_type.type=htons(ETH_P_IPX);
2013 dev_add_pack(&ipx_dix_packet_type);
2014
2015 p8023_datalink = make_8023_client();
2016 ipx_8023_packet_type.type=htons(ETH_P_802_3);
2017 dev_add_pack(&ipx_8023_packet_type);
2018
2019 if ((p8022_datalink = register_8022_client(val, ipx_rcv)) == NULL)
2020 printk("IPX: Unable to register with 802.2\n");
2021
2022 if ((pSNAP_datalink = register_snap_client(snapval, ipx_rcv)) == NULL)
2023 printk("IPX: Unable to register with SNAP\n");
2024
2025 register_netdevice_notifier(&ipx_dev_notifier);
2026
2027 proc_net_register(&(struct proc_dir_entry) {
2028 PROC_NET_IPX, 3, "ipx",
2029 S_IFREG | S_IRUGO, 1, 0, 0,
2030 0, &proc_net_inode_operations,
2031 ipx_get_info
2032 });
2033 proc_net_register(&(struct proc_dir_entry) {
2034 PROC_NET_IPX_INTERFACE, 13, "ipx_interface",
2035 S_IFREG | S_IRUGO, 1, 0, 0,
2036 0, &proc_net_inode_operations,
2037 ipx_interface_get_info
2038 });
2039 proc_net_register(&(struct proc_dir_entry) {
2040 PROC_NET_IPX_ROUTE, 9, "ipx_route",
2041 S_IFREG | S_IRUGO, 1, 0, 0,
2042 0, &proc_net_inode_operations,
2043 ipx_rt_get_info
2044 });
2045
2046 printk("Swansea University Computer Society IPX 0.33 for NET3.032\n");
2047 printk("IPX Portions Copyright (c) 1995 Caldera, Inc.\n");
2048 }
2049 #endif