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