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