1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54#include <linux/capability.h>
55#include <linux/module.h>
56#include <linux/if_arp.h>
57#include <linux/termios.h>
58#include <linux/compat.h>
59#include <linux/slab.h>
60#include <net/datalink.h>
61#include <net/psnap.h>
62#include <net/sock.h>
63#include <net/tcp_states.h>
64#include <net/route.h>
65#include <linux/atalk.h>
66#include <linux/highmem.h>
67
68struct datalink_proto *ddp_dl, *aarp_dl;
69static const struct proto_ops atalk_dgram_ops;
70
71
72
73
74
75
76
77HLIST_HEAD(atalk_sockets);
78DEFINE_RWLOCK(atalk_sockets_lock);
79
80static inline void __atalk_insert_socket(struct sock *sk)
81{
82 sk_add_node(sk, &atalk_sockets);
83}
84
85static inline void atalk_remove_socket(struct sock *sk)
86{
87 write_lock_bh(&atalk_sockets_lock);
88 sk_del_node_init(sk);
89 write_unlock_bh(&atalk_sockets_lock);
90}
91
92static struct sock *atalk_search_socket(struct sockaddr_at *to,
93 struct atalk_iface *atif)
94{
95 struct sock *s;
96
97 read_lock_bh(&atalk_sockets_lock);
98 sk_for_each(s, &atalk_sockets) {
99 struct atalk_sock *at = at_sk(s);
100
101 if (to->sat_port != at->src_port)
102 continue;
103
104 if (to->sat_addr.s_net == ATADDR_ANYNET &&
105 to->sat_addr.s_node == ATADDR_BCAST)
106 goto found;
107
108 if (to->sat_addr.s_net == at->src_net &&
109 (to->sat_addr.s_node == at->src_node ||
110 to->sat_addr.s_node == ATADDR_BCAST ||
111 to->sat_addr.s_node == ATADDR_ANYNODE))
112 goto found;
113
114
115
116 if (to->sat_addr.s_node == ATADDR_ANYNODE &&
117 to->sat_addr.s_net != ATADDR_ANYNET &&
118 atif->address.s_node == at->src_node) {
119 to->sat_addr.s_node = atif->address.s_node;
120 goto found;
121 }
122 }
123 s = NULL;
124found:
125 read_unlock_bh(&atalk_sockets_lock);
126 return s;
127}
128
129
130
131
132
133
134
135
136
137
138
139static struct sock *atalk_find_or_insert_socket(struct sock *sk,
140 struct sockaddr_at *sat)
141{
142 struct sock *s;
143 struct atalk_sock *at;
144
145 write_lock_bh(&atalk_sockets_lock);
146 sk_for_each(s, &atalk_sockets) {
147 at = at_sk(s);
148
149 if (at->src_net == sat->sat_addr.s_net &&
150 at->src_node == sat->sat_addr.s_node &&
151 at->src_port == sat->sat_port)
152 goto found;
153 }
154 s = NULL;
155 __atalk_insert_socket(sk);
156found:
157 write_unlock_bh(&atalk_sockets_lock);
158 return s;
159}
160
161static void atalk_destroy_timer(unsigned long data)
162{
163 struct sock *sk = (struct sock *)data;
164
165 if (sk_has_allocations(sk)) {
166 sk->sk_timer.expires = jiffies + SOCK_DESTROY_TIME;
167 add_timer(&sk->sk_timer);
168 } else
169 sock_put(sk);
170}
171
172static inline void atalk_destroy_socket(struct sock *sk)
173{
174 atalk_remove_socket(sk);
175 skb_queue_purge(&sk->sk_receive_queue);
176
177 if (sk_has_allocations(sk)) {
178 setup_timer(&sk->sk_timer, atalk_destroy_timer,
179 (unsigned long)sk);
180 sk->sk_timer.expires = jiffies + SOCK_DESTROY_TIME;
181 add_timer(&sk->sk_timer);
182 } else
183 sock_put(sk);
184}
185
186
187
188
189
190
191
192
193struct atalk_route *atalk_routes;
194DEFINE_RWLOCK(atalk_routes_lock);
195
196struct atalk_iface *atalk_interfaces;
197DEFINE_RWLOCK(atalk_interfaces_lock);
198
199
200struct atalk_route atrtr_default;
201
202
203
204
205
206
207static void atif_drop_device(struct net_device *dev)
208{
209 struct atalk_iface **iface = &atalk_interfaces;
210 struct atalk_iface *tmp;
211
212 write_lock_bh(&atalk_interfaces_lock);
213 while ((tmp = *iface) != NULL) {
214 if (tmp->dev == dev) {
215 *iface = tmp->next;
216 dev_put(dev);
217 kfree(tmp);
218 dev->atalk_ptr = NULL;
219 } else
220 iface = &tmp->next;
221 }
222 write_unlock_bh(&atalk_interfaces_lock);
223}
224
225static struct atalk_iface *atif_add_device(struct net_device *dev,
226 struct atalk_addr *sa)
227{
228 struct atalk_iface *iface = kzalloc(sizeof(*iface), GFP_KERNEL);
229
230 if (!iface)
231 goto out;
232
233 dev_hold(dev);
234 iface->dev = dev;
235 dev->atalk_ptr = iface;
236 iface->address = *sa;
237 iface->status = 0;
238
239 write_lock_bh(&atalk_interfaces_lock);
240 iface->next = atalk_interfaces;
241 atalk_interfaces = iface;
242 write_unlock_bh(&atalk_interfaces_lock);
243out:
244 return iface;
245}
246
247
248static int atif_probe_device(struct atalk_iface *atif)
249{
250 int netrange = ntohs(atif->nets.nr_lastnet) -
251 ntohs(atif->nets.nr_firstnet) + 1;
252 int probe_net = ntohs(atif->address.s_net);
253 int probe_node = atif->address.s_node;
254 int netct, nodect;
255
256
257 if (probe_net == ATADDR_ANYNET) {
258 probe_net = ntohs(atif->nets.nr_firstnet);
259 if (netrange)
260 probe_net += jiffies % netrange;
261 }
262 if (probe_node == ATADDR_ANYNODE)
263 probe_node = jiffies & 0xFF;
264
265
266 atif->status |= ATIF_PROBE;
267 for (netct = 0; netct <= netrange; netct++) {
268
269 atif->address.s_net = htons(probe_net);
270 for (nodect = 0; nodect < 256; nodect++) {
271 atif->address.s_node = (nodect + probe_node) & 0xFF;
272 if (atif->address.s_node > 0 &&
273 atif->address.s_node < 254) {
274
275 aarp_probe_network(atif);
276
277 if (!(atif->status & ATIF_PROBE_FAIL)) {
278 atif->status &= ~ATIF_PROBE;
279 return 0;
280 }
281 }
282 atif->status &= ~ATIF_PROBE_FAIL;
283 }
284 probe_net++;
285 if (probe_net > ntohs(atif->nets.nr_lastnet))
286 probe_net = ntohs(atif->nets.nr_firstnet);
287 }
288 atif->status &= ~ATIF_PROBE;
289
290 return -EADDRINUSE;
291}
292
293
294
295static int atif_proxy_probe_device(struct atalk_iface *atif,
296 struct atalk_addr *proxy_addr)
297{
298 int netrange = ntohs(atif->nets.nr_lastnet) -
299 ntohs(atif->nets.nr_firstnet) + 1;
300
301 int probe_net = ntohs(atif->address.s_net);
302 int probe_node = ATADDR_ANYNODE;
303 int netct, nodect;
304
305
306 if (probe_net == ATADDR_ANYNET) {
307 probe_net = ntohs(atif->nets.nr_firstnet);
308 if (netrange)
309 probe_net += jiffies % netrange;
310 }
311
312 if (probe_node == ATADDR_ANYNODE)
313 probe_node = jiffies & 0xFF;
314
315
316 for (netct = 0; netct <= netrange; netct++) {
317
318 proxy_addr->s_net = htons(probe_net);
319 for (nodect = 0; nodect < 256; nodect++) {
320 proxy_addr->s_node = (nodect + probe_node) & 0xFF;
321 if (proxy_addr->s_node > 0 &&
322 proxy_addr->s_node < 254) {
323
324 int ret = aarp_proxy_probe_network(atif,
325 proxy_addr);
326
327 if (ret != -EADDRINUSE)
328 return ret;
329 }
330 }
331 probe_net++;
332 if (probe_net > ntohs(atif->nets.nr_lastnet))
333 probe_net = ntohs(atif->nets.nr_firstnet);
334 }
335
336 return -EADDRINUSE;
337}
338
339
340struct atalk_addr *atalk_find_dev_addr(struct net_device *dev)
341{
342 struct atalk_iface *iface = dev->atalk_ptr;
343 return iface ? &iface->address : NULL;
344}
345
346static struct atalk_addr *atalk_find_primary(void)
347{
348 struct atalk_iface *fiface = NULL;
349 struct atalk_addr *retval;
350 struct atalk_iface *iface;
351
352
353
354
355
356 read_lock_bh(&atalk_interfaces_lock);
357 for (iface = atalk_interfaces; iface; iface = iface->next) {
358 if (!fiface && !(iface->dev->flags & IFF_LOOPBACK))
359 fiface = iface;
360 if (!(iface->dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
361 retval = &iface->address;
362 goto out;
363 }
364 }
365
366 if (fiface)
367 retval = &fiface->address;
368 else if (atalk_interfaces)
369 retval = &atalk_interfaces->address;
370 else
371 retval = NULL;
372out:
373 read_unlock_bh(&atalk_interfaces_lock);
374 return retval;
375}
376
377
378
379
380
381static struct atalk_iface *atalk_find_anynet(int node, struct net_device *dev)
382{
383 struct atalk_iface *iface = dev->atalk_ptr;
384
385 if (!iface || iface->status & ATIF_PROBE)
386 goto out_err;
387
388 if (node != ATADDR_BCAST &&
389 iface->address.s_node != node &&
390 node != ATADDR_ANYNODE)
391 goto out_err;
392out:
393 return iface;
394out_err:
395 iface = NULL;
396 goto out;
397}
398
399
400static struct atalk_iface *atalk_find_interface(__be16 net, int node)
401{
402 struct atalk_iface *iface;
403
404 read_lock_bh(&atalk_interfaces_lock);
405 for (iface = atalk_interfaces; iface; iface = iface->next) {
406 if ((node == ATADDR_BCAST ||
407 node == ATADDR_ANYNODE ||
408 iface->address.s_node == node) &&
409 iface->address.s_net == net &&
410 !(iface->status & ATIF_PROBE))
411 break;
412
413
414 if (node == ATADDR_ANYNODE && net != ATADDR_ANYNET &&
415 ntohs(iface->nets.nr_firstnet) <= ntohs(net) &&
416 ntohs(net) <= ntohs(iface->nets.nr_lastnet))
417 break;
418 }
419 read_unlock_bh(&atalk_interfaces_lock);
420 return iface;
421}
422
423
424
425
426
427
428
429static struct atalk_route *atrtr_find(struct atalk_addr *target)
430{
431
432
433
434
435
436 struct atalk_route *net_route = NULL;
437 struct atalk_route *r;
438
439 read_lock_bh(&atalk_routes_lock);
440 for (r = atalk_routes; r; r = r->next) {
441 if (!(r->flags & RTF_UP))
442 continue;
443
444 if (r->target.s_net == target->s_net) {
445 if (r->flags & RTF_HOST) {
446
447
448
449
450 if (r->target.s_node == target->s_node)
451 goto out;
452 } else
453
454
455
456
457 net_route = r;
458 }
459 }
460
461
462
463
464
465 if (net_route)
466 r = net_route;
467 else if (atrtr_default.dev)
468 r = &atrtr_default;
469 else
470 r = NULL;
471out:
472 read_unlock_bh(&atalk_routes_lock);
473 return r;
474}
475
476
477
478
479
480
481struct net_device *atrtr_get_dev(struct atalk_addr *sa)
482{
483 struct atalk_route *atr = atrtr_find(sa);
484 return atr ? atr->dev : NULL;
485}
486
487
488static void atrtr_set_default(struct net_device *dev)
489{
490 atrtr_default.dev = dev;
491 atrtr_default.flags = RTF_UP;
492 atrtr_default.gateway.s_net = htons(0);
493 atrtr_default.gateway.s_node = 0;
494}
495
496
497
498
499
500
501static int atrtr_create(struct rtentry *r, struct net_device *devhint)
502{
503 struct sockaddr_at *ta = (struct sockaddr_at *)&r->rt_dst;
504 struct sockaddr_at *ga = (struct sockaddr_at *)&r->rt_gateway;
505 struct atalk_route *rt;
506 struct atalk_iface *iface, *riface;
507 int retval = -EINVAL;
508
509
510
511
512
513
514
515 if (ta->sat_family != AF_APPLETALK ||
516 (!devhint && ga->sat_family != AF_APPLETALK))
517 goto out;
518
519
520 write_lock_bh(&atalk_routes_lock);
521 for (rt = atalk_routes; rt; rt = rt->next) {
522 if (r->rt_flags != rt->flags)
523 continue;
524
525 if (ta->sat_addr.s_net == rt->target.s_net) {
526 if (!(rt->flags & RTF_HOST))
527 break;
528 if (ta->sat_addr.s_node == rt->target.s_node)
529 break;
530 }
531 }
532
533 if (!devhint) {
534 riface = NULL;
535
536 read_lock_bh(&atalk_interfaces_lock);
537 for (iface = atalk_interfaces; iface; iface = iface->next) {
538 if (!riface &&
539 ntohs(ga->sat_addr.s_net) >=
540 ntohs(iface->nets.nr_firstnet) &&
541 ntohs(ga->sat_addr.s_net) <=
542 ntohs(iface->nets.nr_lastnet))
543 riface = iface;
544
545 if (ga->sat_addr.s_net == iface->address.s_net &&
546 ga->sat_addr.s_node == iface->address.s_node)
547 riface = iface;
548 }
549 read_unlock_bh(&atalk_interfaces_lock);
550
551 retval = -ENETUNREACH;
552 if (!riface)
553 goto out_unlock;
554
555 devhint = riface->dev;
556 }
557
558 if (!rt) {
559 rt = kzalloc(sizeof(*rt), GFP_ATOMIC);
560
561 retval = -ENOBUFS;
562 if (!rt)
563 goto out_unlock;
564
565 rt->next = atalk_routes;
566 atalk_routes = rt;
567 }
568
569
570 rt->target = ta->sat_addr;
571 dev_hold(devhint);
572 rt->dev = devhint;
573 rt->flags = r->rt_flags;
574 rt->gateway = ga->sat_addr;
575
576 retval = 0;
577out_unlock:
578 write_unlock_bh(&atalk_routes_lock);
579out:
580 return retval;
581}
582
583
584static int atrtr_delete(struct atalk_addr *addr)
585{
586 struct atalk_route **r = &atalk_routes;
587 int retval = 0;
588 struct atalk_route *tmp;
589
590 write_lock_bh(&atalk_routes_lock);
591 while ((tmp = *r) != NULL) {
592 if (tmp->target.s_net == addr->s_net &&
593 (!(tmp->flags&RTF_GATEWAY) ||
594 tmp->target.s_node == addr->s_node)) {
595 *r = tmp->next;
596 dev_put(tmp->dev);
597 kfree(tmp);
598 goto out;
599 }
600 r = &tmp->next;
601 }
602 retval = -ENOENT;
603out:
604 write_unlock_bh(&atalk_routes_lock);
605 return retval;
606}
607
608
609
610
611
612static void atrtr_device_down(struct net_device *dev)
613{
614 struct atalk_route **r = &atalk_routes;
615 struct atalk_route *tmp;
616
617 write_lock_bh(&atalk_routes_lock);
618 while ((tmp = *r) != NULL) {
619 if (tmp->dev == dev) {
620 *r = tmp->next;
621 dev_put(dev);
622 kfree(tmp);
623 } else
624 r = &tmp->next;
625 }
626 write_unlock_bh(&atalk_routes_lock);
627
628 if (atrtr_default.dev == dev)
629 atrtr_set_default(NULL);
630}
631
632
633static inline void atalk_dev_down(struct net_device *dev)
634{
635 atrtr_device_down(dev);
636 aarp_device_down(dev);
637 atif_drop_device(dev);
638}
639
640
641
642
643
644static int ddp_device_event(struct notifier_block *this, unsigned long event,
645 void *ptr)
646{
647 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
648
649 if (!net_eq(dev_net(dev), &init_net))
650 return NOTIFY_DONE;
651
652 if (event == NETDEV_DOWN)
653
654 atalk_dev_down(dev);
655
656 return NOTIFY_DONE;
657}
658
659
660
661static int atif_ioctl(int cmd, void __user *arg)
662{
663 static char aarp_mcast[6] = { 0x09, 0x00, 0x00, 0xFF, 0xFF, 0xFF };
664 struct ifreq atreq;
665 struct atalk_netrange *nr;
666 struct sockaddr_at *sa;
667 struct net_device *dev;
668 struct atalk_iface *atif;
669 int ct;
670 int limit;
671 struct rtentry rtdef;
672 int add_route;
673
674 if (copy_from_user(&atreq, arg, sizeof(atreq)))
675 return -EFAULT;
676
677 dev = __dev_get_by_name(&init_net, atreq.ifr_name);
678 if (!dev)
679 return -ENODEV;
680
681 sa = (struct sockaddr_at *)&atreq.ifr_addr;
682 atif = atalk_find_dev(dev);
683
684 switch (cmd) {
685 case SIOCSIFADDR:
686 if (!capable(CAP_NET_ADMIN))
687 return -EPERM;
688 if (sa->sat_family != AF_APPLETALK)
689 return -EINVAL;
690 if (dev->type != ARPHRD_ETHER &&
691 dev->type != ARPHRD_LOOPBACK &&
692 dev->type != ARPHRD_LOCALTLK &&
693 dev->type != ARPHRD_PPP)
694 return -EPROTONOSUPPORT;
695
696 nr = (struct atalk_netrange *)&sa->sat_zero[0];
697 add_route = 1;
698
699
700
701
702
703
704 if ((dev->flags & IFF_POINTOPOINT) &&
705 atalk_find_interface(sa->sat_addr.s_net,
706 sa->sat_addr.s_node)) {
707 printk(KERN_DEBUG "AppleTalk: point-to-point "
708 "interface added with "
709 "existing address\n");
710 add_route = 0;
711 }
712
713
714
715
716
717 if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
718 return -EPROTONOSUPPORT;
719 if (sa->sat_addr.s_node == ATADDR_BCAST ||
720 sa->sat_addr.s_node == 254)
721 return -EINVAL;
722 if (atif) {
723
724 if (atif->status & ATIF_PROBE)
725 return -EBUSY;
726
727 atif->address.s_net = sa->sat_addr.s_net;
728 atif->address.s_node = sa->sat_addr.s_node;
729 atrtr_device_down(dev);
730 } else {
731 atif = atif_add_device(dev, &sa->sat_addr);
732 if (!atif)
733 return -ENOMEM;
734 }
735 atif->nets = *nr;
736
737
738
739
740
741
742 if (!(dev->flags & IFF_LOOPBACK) &&
743 !(dev->flags & IFF_POINTOPOINT) &&
744 atif_probe_device(atif) < 0) {
745 atif_drop_device(dev);
746 return -EADDRINUSE;
747 }
748
749
750 sa = (struct sockaddr_at *)&rtdef.rt_gateway;
751 sa->sat_family = AF_APPLETALK;
752 sa->sat_addr.s_net = atif->address.s_net;
753 sa->sat_addr.s_node = atif->address.s_node;
754 sa = (struct sockaddr_at *)&rtdef.rt_dst;
755 rtdef.rt_flags = RTF_UP;
756 sa->sat_family = AF_APPLETALK;
757 sa->sat_addr.s_node = ATADDR_ANYNODE;
758 if (dev->flags & IFF_LOOPBACK ||
759 dev->flags & IFF_POINTOPOINT)
760 rtdef.rt_flags |= RTF_HOST;
761
762
763 if (nr->nr_firstnet == htons(0) &&
764 nr->nr_lastnet == htons(0xFFFE)) {
765 sa->sat_addr.s_net = atif->address.s_net;
766 atrtr_create(&rtdef, dev);
767 atrtr_set_default(dev);
768 } else {
769 limit = ntohs(nr->nr_lastnet);
770 if (limit - ntohs(nr->nr_firstnet) > 4096) {
771 printk(KERN_WARNING "Too many routes/"
772 "iface.\n");
773 return -EINVAL;
774 }
775 if (add_route)
776 for (ct = ntohs(nr->nr_firstnet);
777 ct <= limit; ct++) {
778 sa->sat_addr.s_net = htons(ct);
779 atrtr_create(&rtdef, dev);
780 }
781 }
782 dev_mc_add_global(dev, aarp_mcast);
783 return 0;
784
785 case SIOCGIFADDR:
786 if (!atif)
787 return -EADDRNOTAVAIL;
788
789 sa->sat_family = AF_APPLETALK;
790 sa->sat_addr = atif->address;
791 break;
792
793 case SIOCGIFBRDADDR:
794 if (!atif)
795 return -EADDRNOTAVAIL;
796
797 sa->sat_family = AF_APPLETALK;
798 sa->sat_addr.s_net = atif->address.s_net;
799 sa->sat_addr.s_node = ATADDR_BCAST;
800 break;
801
802 case SIOCATALKDIFADDR:
803 case SIOCDIFADDR:
804 if (!capable(CAP_NET_ADMIN))
805 return -EPERM;
806 if (sa->sat_family != AF_APPLETALK)
807 return -EINVAL;
808 atalk_dev_down(dev);
809 break;
810
811 case SIOCSARP:
812 if (!capable(CAP_NET_ADMIN))
813 return -EPERM;
814 if (sa->sat_family != AF_APPLETALK)
815 return -EINVAL;
816
817
818
819
820 if (dev->type != ARPHRD_ETHER)
821 return -EPROTONOSUPPORT;
822
823
824
825
826
827
828
829
830 if (!atif)
831 return -EADDRNOTAVAIL;
832
833 nr = (struct atalk_netrange *)&(atif->nets);
834
835
836
837
838 if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
839 return -EPROTONOSUPPORT;
840
841 if (sa->sat_addr.s_node == ATADDR_BCAST ||
842 sa->sat_addr.s_node == 254)
843 return -EINVAL;
844
845
846
847
848
849 if (atif_proxy_probe_device(atif, &(sa->sat_addr)) < 0)
850 return -EADDRINUSE;
851
852
853
854
855
856
857
858 break;
859
860 case SIOCDARP:
861 if (!capable(CAP_NET_ADMIN))
862 return -EPERM;
863 if (sa->sat_family != AF_APPLETALK)
864 return -EINVAL;
865 if (!atif)
866 return -EADDRNOTAVAIL;
867
868
869 aarp_proxy_remove(atif->dev, &(sa->sat_addr));
870 return 0;
871 }
872
873 return copy_to_user(arg, &atreq, sizeof(atreq)) ? -EFAULT : 0;
874}
875
876
877static int atrtr_ioctl(unsigned int cmd, void __user *arg)
878{
879 struct rtentry rt;
880
881 if (copy_from_user(&rt, arg, sizeof(rt)))
882 return -EFAULT;
883
884 switch (cmd) {
885 case SIOCDELRT:
886 if (rt.rt_dst.sa_family != AF_APPLETALK)
887 return -EINVAL;
888 return atrtr_delete(&((struct sockaddr_at *)
889 &rt.rt_dst)->sat_addr);
890
891 case SIOCADDRT: {
892 struct net_device *dev = NULL;
893 if (rt.rt_dev) {
894 char name[IFNAMSIZ];
895 if (copy_from_user(name, rt.rt_dev, IFNAMSIZ-1))
896 return -EFAULT;
897 name[IFNAMSIZ-1] = '\0';
898 dev = __dev_get_by_name(&init_net, name);
899 if (!dev)
900 return -ENODEV;
901 }
902 return atrtr_create(&rt, dev);
903 }
904 }
905 return -EINVAL;
906}
907
908
909
910
911
912
913
914
915
916
917
918
919static unsigned long atalk_sum_partial(const unsigned char *data,
920 int len, unsigned long sum)
921{
922
923 while (len--) {
924 sum += *data++;
925 sum = rol16(sum, 1);
926 }
927 return sum;
928}
929
930
931static unsigned long atalk_sum_skb(const struct sk_buff *skb, int offset,
932 int len, unsigned long sum)
933{
934 int start = skb_headlen(skb);
935 struct sk_buff *frag_iter;
936 int i, copy;
937
938
939 if ((copy = start - offset) > 0) {
940 if (copy > len)
941 copy = len;
942 sum = atalk_sum_partial(skb->data + offset, copy, sum);
943 if ((len -= copy) == 0)
944 return sum;
945
946 offset += copy;
947 }
948
949
950 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
951 int end;
952 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
953 WARN_ON(start > offset + len);
954
955 end = start + skb_frag_size(frag);
956 if ((copy = end - offset) > 0) {
957 u8 *vaddr;
958
959 if (copy > len)
960 copy = len;
961 vaddr = kmap_atomic(skb_frag_page(frag));
962 sum = atalk_sum_partial(vaddr + frag->page_offset +
963 offset - start, copy, sum);
964 kunmap_atomic(vaddr);
965
966 if (!(len -= copy))
967 return sum;
968 offset += copy;
969 }
970 start = end;
971 }
972
973 skb_walk_frags(skb, frag_iter) {
974 int end;
975
976 WARN_ON(start > offset + len);
977
978 end = start + frag_iter->len;
979 if ((copy = end - offset) > 0) {
980 if (copy > len)
981 copy = len;
982 sum = atalk_sum_skb(frag_iter, offset - start,
983 copy, sum);
984 if ((len -= copy) == 0)
985 return sum;
986 offset += copy;
987 }
988 start = end;
989 }
990
991 BUG_ON(len > 0);
992
993 return sum;
994}
995
996static __be16 atalk_checksum(const struct sk_buff *skb, int len)
997{
998 unsigned long sum;
999
1000
1001 sum = atalk_sum_skb(skb, 4, len-4, 0);
1002
1003
1004 return sum ? htons((unsigned short)sum) : htons(0xFFFF);
1005}
1006
1007static struct proto ddp_proto = {
1008 .name = "DDP",
1009 .owner = THIS_MODULE,
1010 .obj_size = sizeof(struct atalk_sock),
1011};
1012
1013
1014
1015
1016
1017static int atalk_create(struct net *net, struct socket *sock, int protocol,
1018 int kern)
1019{
1020 struct sock *sk;
1021 int rc = -ESOCKTNOSUPPORT;
1022
1023 if (!net_eq(net, &init_net))
1024 return -EAFNOSUPPORT;
1025
1026
1027
1028
1029
1030 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
1031 goto out;
1032 rc = -ENOMEM;
1033 sk = sk_alloc(net, PF_APPLETALK, GFP_KERNEL, &ddp_proto);
1034 if (!sk)
1035 goto out;
1036 rc = 0;
1037 sock->ops = &atalk_dgram_ops;
1038 sock_init_data(sock, sk);
1039
1040
1041 sock_set_flag(sk, SOCK_ZAPPED);
1042out:
1043 return rc;
1044}
1045
1046
1047static int atalk_release(struct socket *sock)
1048{
1049 struct sock *sk = sock->sk;
1050
1051 if (sk) {
1052 sock_hold(sk);
1053 lock_sock(sk);
1054
1055 sock_orphan(sk);
1056 sock->sk = NULL;
1057 atalk_destroy_socket(sk);
1058
1059 release_sock(sk);
1060 sock_put(sk);
1061 }
1062 return 0;
1063}
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075static int atalk_pick_and_bind_port(struct sock *sk, struct sockaddr_at *sat)
1076{
1077 int retval;
1078
1079 write_lock_bh(&atalk_sockets_lock);
1080
1081 for (sat->sat_port = ATPORT_RESERVED;
1082 sat->sat_port < ATPORT_LAST;
1083 sat->sat_port++) {
1084 struct sock *s;
1085
1086 sk_for_each(s, &atalk_sockets) {
1087 struct atalk_sock *at = at_sk(s);
1088
1089 if (at->src_net == sat->sat_addr.s_net &&
1090 at->src_node == sat->sat_addr.s_node &&
1091 at->src_port == sat->sat_port)
1092 goto try_next_port;
1093 }
1094
1095
1096 __atalk_insert_socket(sk);
1097 at_sk(sk)->src_port = sat->sat_port;
1098 retval = 0;
1099 goto out;
1100
1101try_next_port:;
1102 }
1103
1104 retval = -EBUSY;
1105out:
1106 write_unlock_bh(&atalk_sockets_lock);
1107 return retval;
1108}
1109
1110static int atalk_autobind(struct sock *sk)
1111{
1112 struct atalk_sock *at = at_sk(sk);
1113 struct sockaddr_at sat;
1114 struct atalk_addr *ap = atalk_find_primary();
1115 int n = -EADDRNOTAVAIL;
1116
1117 if (!ap || ap->s_net == htons(ATADDR_ANYNET))
1118 goto out;
1119
1120 at->src_net = sat.sat_addr.s_net = ap->s_net;
1121 at->src_node = sat.sat_addr.s_node = ap->s_node;
1122
1123 n = atalk_pick_and_bind_port(sk, &sat);
1124 if (!n)
1125 sock_reset_flag(sk, SOCK_ZAPPED);
1126out:
1127 return n;
1128}
1129
1130
1131static int atalk_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1132{
1133 struct sockaddr_at *addr = (struct sockaddr_at *)uaddr;
1134 struct sock *sk = sock->sk;
1135 struct atalk_sock *at = at_sk(sk);
1136 int err;
1137
1138 if (!sock_flag(sk, SOCK_ZAPPED) ||
1139 addr_len != sizeof(struct sockaddr_at))
1140 return -EINVAL;
1141
1142 if (addr->sat_family != AF_APPLETALK)
1143 return -EAFNOSUPPORT;
1144
1145 lock_sock(sk);
1146 if (addr->sat_addr.s_net == htons(ATADDR_ANYNET)) {
1147 struct atalk_addr *ap = atalk_find_primary();
1148
1149 err = -EADDRNOTAVAIL;
1150 if (!ap)
1151 goto out;
1152
1153 at->src_net = addr->sat_addr.s_net = ap->s_net;
1154 at->src_node = addr->sat_addr.s_node = ap->s_node;
1155 } else {
1156 err = -EADDRNOTAVAIL;
1157 if (!atalk_find_interface(addr->sat_addr.s_net,
1158 addr->sat_addr.s_node))
1159 goto out;
1160
1161 at->src_net = addr->sat_addr.s_net;
1162 at->src_node = addr->sat_addr.s_node;
1163 }
1164
1165 if (addr->sat_port == ATADDR_ANYPORT) {
1166 err = atalk_pick_and_bind_port(sk, addr);
1167
1168 if (err < 0)
1169 goto out;
1170 } else {
1171 at->src_port = addr->sat_port;
1172
1173 err = -EADDRINUSE;
1174 if (atalk_find_or_insert_socket(sk, addr))
1175 goto out;
1176 }
1177
1178 sock_reset_flag(sk, SOCK_ZAPPED);
1179 err = 0;
1180out:
1181 release_sock(sk);
1182 return err;
1183}
1184
1185
1186static int atalk_connect(struct socket *sock, struct sockaddr *uaddr,
1187 int addr_len, int flags)
1188{
1189 struct sock *sk = sock->sk;
1190 struct atalk_sock *at = at_sk(sk);
1191 struct sockaddr_at *addr;
1192 int err;
1193
1194 sk->sk_state = TCP_CLOSE;
1195 sock->state = SS_UNCONNECTED;
1196
1197 if (addr_len != sizeof(*addr))
1198 return -EINVAL;
1199
1200 addr = (struct sockaddr_at *)uaddr;
1201
1202 if (addr->sat_family != AF_APPLETALK)
1203 return -EAFNOSUPPORT;
1204
1205 if (addr->sat_addr.s_node == ATADDR_BCAST &&
1206 !sock_flag(sk, SOCK_BROADCAST)) {
1207#if 1
1208 pr_warn("atalk_connect: %s is broken and did not set SO_BROADCAST.\n",
1209 current->comm);
1210#else
1211 return -EACCES;
1212#endif
1213 }
1214
1215 lock_sock(sk);
1216 err = -EBUSY;
1217 if (sock_flag(sk, SOCK_ZAPPED))
1218 if (atalk_autobind(sk) < 0)
1219 goto out;
1220
1221 err = -ENETUNREACH;
1222 if (!atrtr_get_dev(&addr->sat_addr))
1223 goto out;
1224
1225 at->dest_port = addr->sat_port;
1226 at->dest_net = addr->sat_addr.s_net;
1227 at->dest_node = addr->sat_addr.s_node;
1228
1229 sock->state = SS_CONNECTED;
1230 sk->sk_state = TCP_ESTABLISHED;
1231 err = 0;
1232out:
1233 release_sock(sk);
1234 return err;
1235}
1236
1237
1238
1239
1240
1241static int atalk_getname(struct socket *sock, struct sockaddr *uaddr,
1242 int *uaddr_len, int peer)
1243{
1244 struct sockaddr_at sat;
1245 struct sock *sk = sock->sk;
1246 struct atalk_sock *at = at_sk(sk);
1247 int err;
1248
1249 lock_sock(sk);
1250 err = -ENOBUFS;
1251 if (sock_flag(sk, SOCK_ZAPPED))
1252 if (atalk_autobind(sk) < 0)
1253 goto out;
1254
1255 *uaddr_len = sizeof(struct sockaddr_at);
1256 memset(&sat, 0, sizeof(sat));
1257
1258 if (peer) {
1259 err = -ENOTCONN;
1260 if (sk->sk_state != TCP_ESTABLISHED)
1261 goto out;
1262
1263 sat.sat_addr.s_net = at->dest_net;
1264 sat.sat_addr.s_node = at->dest_node;
1265 sat.sat_port = at->dest_port;
1266 } else {
1267 sat.sat_addr.s_net = at->src_net;
1268 sat.sat_addr.s_node = at->src_node;
1269 sat.sat_port = at->src_port;
1270 }
1271
1272 err = 0;
1273 sat.sat_family = AF_APPLETALK;
1274 memcpy(uaddr, &sat, sizeof(sat));
1275
1276out:
1277 release_sock(sk);
1278 return err;
1279}
1280
1281#if defined(CONFIG_IPDDP) || defined(CONFIG_IPDDP_MODULE)
1282static __inline__ int is_ip_over_ddp(struct sk_buff *skb)
1283{
1284 return skb->data[12] == 22;
1285}
1286
1287static int handle_ip_over_ddp(struct sk_buff *skb)
1288{
1289 struct net_device *dev = __dev_get_by_name(&init_net, "ipddp0");
1290 struct net_device_stats *stats;
1291
1292
1293 if (!dev) {
1294 kfree_skb(skb);
1295 return NET_RX_DROP;
1296 }
1297
1298 skb->protocol = htons(ETH_P_IP);
1299 skb_pull(skb, 13);
1300 skb->dev = dev;
1301 skb_reset_transport_header(skb);
1302
1303 stats = netdev_priv(dev);
1304 stats->rx_packets++;
1305 stats->rx_bytes += skb->len + 13;
1306 return netif_rx(skb);
1307}
1308#else
1309
1310#define is_ip_over_ddp(skb) 0
1311#define handle_ip_over_ddp(skb) 0
1312#endif
1313
1314static int atalk_route_packet(struct sk_buff *skb, struct net_device *dev,
1315 struct ddpehdr *ddp, __u16 len_hops, int origlen)
1316{
1317 struct atalk_route *rt;
1318 struct atalk_addr ta;
1319
1320
1321
1322
1323
1324 if (skb->pkt_type != PACKET_HOST || !ddp->deh_dnet) {
1325
1326
1327
1328
1329
1330
1331 if (dev->type == ARPHRD_PPP)
1332 printk(KERN_DEBUG "AppleTalk: didn't forward broadcast "
1333 "packet received from PPP iface\n");
1334 goto free_it;
1335 }
1336
1337 ta.s_net = ddp->deh_dnet;
1338 ta.s_node = ddp->deh_dnode;
1339
1340
1341 rt = atrtr_find(&ta);
1342
1343 len_hops += 1 << 10;
1344 if (!rt || !(len_hops & (15 << 10)))
1345 goto free_it;
1346
1347
1348
1349
1350
1351
1352
1353
1354 if (rt->flags & RTF_GATEWAY) {
1355 ta.s_net = rt->gateway.s_net;
1356 ta.s_node = rt->gateway.s_node;
1357 }
1358
1359
1360 skb_trim(skb, min_t(unsigned int, origlen,
1361 (rt->dev->hard_header_len +
1362 ddp_dl->header_length + (len_hops & 1023))));
1363
1364
1365 ddp->deh_len_hops = htons(len_hops);
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378 if (skb_headroom(skb) < 22) {
1379
1380 struct sk_buff *nskb = skb_realloc_headroom(skb, 32);
1381 kfree_skb(skb);
1382 skb = nskb;
1383 } else
1384 skb = skb_unshare(skb, GFP_ATOMIC);
1385
1386
1387
1388
1389
1390 if (skb == NULL)
1391 goto drop;
1392
1393 if (aarp_send_ddp(rt->dev, skb, &ta, NULL) == NET_XMIT_DROP)
1394 return NET_RX_DROP;
1395 return NET_RX_SUCCESS;
1396free_it:
1397 kfree_skb(skb);
1398drop:
1399 return NET_RX_DROP;
1400}
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414static int atalk_rcv(struct sk_buff *skb, struct net_device *dev,
1415 struct packet_type *pt, struct net_device *orig_dev)
1416{
1417 struct ddpehdr *ddp;
1418 struct sock *sock;
1419 struct atalk_iface *atif;
1420 struct sockaddr_at tosat;
1421 int origlen;
1422 __u16 len_hops;
1423
1424 if (!net_eq(dev_net(dev), &init_net))
1425 goto drop;
1426
1427
1428 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
1429 goto out;
1430
1431
1432 if (!pskb_may_pull(skb, sizeof(*ddp)))
1433 goto drop;
1434
1435 ddp = ddp_hdr(skb);
1436
1437 len_hops = ntohs(ddp->deh_len_hops);
1438
1439
1440 origlen = skb->len;
1441 skb_trim(skb, min_t(unsigned int, skb->len, len_hops & 1023));
1442
1443
1444
1445
1446
1447
1448 if (skb->len < sizeof(*ddp) || skb->len < (len_hops & 1023)) {
1449 pr_debug("AppleTalk: dropping corrupted frame (deh_len=%u, "
1450 "skb->len=%u)\n", len_hops & 1023, skb->len);
1451 goto drop;
1452 }
1453
1454
1455
1456
1457
1458 if (ddp->deh_sum &&
1459 atalk_checksum(skb, len_hops & 1023) != ddp->deh_sum)
1460
1461 goto drop;
1462
1463
1464 if (!ddp->deh_dnet)
1465 atif = atalk_find_anynet(ddp->deh_dnode, dev);
1466 else
1467 atif = atalk_find_interface(ddp->deh_dnet, ddp->deh_dnode);
1468
1469 if (!atif) {
1470
1471
1472
1473 return atalk_route_packet(skb, dev, ddp, len_hops, origlen);
1474 }
1475
1476
1477 if (is_ip_over_ddp(skb))
1478 return handle_ip_over_ddp(skb);
1479
1480
1481
1482
1483 tosat.sat_addr.s_net = ddp->deh_dnet;
1484 tosat.sat_addr.s_node = ddp->deh_dnode;
1485 tosat.sat_port = ddp->deh_dport;
1486
1487 sock = atalk_search_socket(&tosat, atif);
1488 if (!sock)
1489 goto drop;
1490
1491
1492 if (sock_queue_rcv_skb(sock, skb) < 0)
1493 goto drop;
1494
1495 return NET_RX_SUCCESS;
1496
1497drop:
1498 kfree_skb(skb);
1499out:
1500 return NET_RX_DROP;
1501
1502}
1503
1504
1505
1506
1507
1508
1509static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev,
1510 struct packet_type *pt, struct net_device *orig_dev)
1511{
1512 if (!net_eq(dev_net(dev), &init_net))
1513 goto freeit;
1514
1515
1516 if (skb_mac_header(skb)[2] == 1) {
1517 struct ddpehdr *ddp;
1518
1519 struct atalk_addr *ap = atalk_find_dev_addr(dev);
1520
1521 if (!ap || skb->len < sizeof(__be16) || skb->len > 1023)
1522 goto freeit;
1523
1524
1525 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
1526 return 0;
1527
1528
1529
1530
1531
1532 ddp = (struct ddpehdr *) skb_push(skb, sizeof(*ddp) - 4);
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542 ddp->deh_dnode = skb_mac_header(skb)[0];
1543 ddp->deh_snode = skb_mac_header(skb)[1];
1544
1545 ddp->deh_dnet = ap->s_net;
1546 ddp->deh_snet = ap->s_net;
1547 ddp->deh_sum = 0;
1548
1549
1550
1551
1552 ddp->deh_len_hops = htons(skb->len + (DDP_MAXHOPS << 10));
1553 }
1554 skb_reset_transport_header(skb);
1555
1556 return atalk_rcv(skb, dev, pt, orig_dev);
1557freeit:
1558 kfree_skb(skb);
1559 return 0;
1560}
1561
1562static int atalk_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
1563 size_t len)
1564{
1565 struct sock *sk = sock->sk;
1566 struct atalk_sock *at = at_sk(sk);
1567 DECLARE_SOCKADDR(struct sockaddr_at *, usat, msg->msg_name);
1568 int flags = msg->msg_flags;
1569 int loopback = 0;
1570 struct sockaddr_at local_satalk, gsat;
1571 struct sk_buff *skb;
1572 struct net_device *dev;
1573 struct ddpehdr *ddp;
1574 int size;
1575 struct atalk_route *rt;
1576 int err;
1577
1578 if (flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
1579 return -EINVAL;
1580
1581 if (len > DDP_MAXSZ)
1582 return -EMSGSIZE;
1583
1584 lock_sock(sk);
1585 if (usat) {
1586 err = -EBUSY;
1587 if (sock_flag(sk, SOCK_ZAPPED))
1588 if (atalk_autobind(sk) < 0)
1589 goto out;
1590
1591 err = -EINVAL;
1592 if (msg->msg_namelen < sizeof(*usat) ||
1593 usat->sat_family != AF_APPLETALK)
1594 goto out;
1595
1596 err = -EPERM;
1597
1598 if (usat->sat_addr.s_node == ATADDR_BCAST &&
1599 !sock_flag(sk, SOCK_BROADCAST)) {
1600 goto out;
1601 }
1602 } else {
1603 err = -ENOTCONN;
1604 if (sk->sk_state != TCP_ESTABLISHED)
1605 goto out;
1606 usat = &local_satalk;
1607 usat->sat_family = AF_APPLETALK;
1608 usat->sat_port = at->dest_port;
1609 usat->sat_addr.s_node = at->dest_node;
1610 usat->sat_addr.s_net = at->dest_net;
1611 }
1612
1613
1614 SOCK_DEBUG(sk, "SK %p: Got address.\n", sk);
1615
1616
1617 size = sizeof(struct ddpehdr) + len + ddp_dl->header_length;
1618
1619 if (usat->sat_addr.s_net || usat->sat_addr.s_node == ATADDR_ANYNODE) {
1620 rt = atrtr_find(&usat->sat_addr);
1621 } else {
1622 struct atalk_addr at_hint;
1623
1624 at_hint.s_node = 0;
1625 at_hint.s_net = at->src_net;
1626
1627 rt = atrtr_find(&at_hint);
1628 }
1629 err = ENETUNREACH;
1630 if (!rt)
1631 goto out;
1632
1633 dev = rt->dev;
1634
1635 SOCK_DEBUG(sk, "SK %p: Size needed %d, device %s\n",
1636 sk, size, dev->name);
1637
1638 size += dev->hard_header_len;
1639 release_sock(sk);
1640 skb = sock_alloc_send_skb(sk, size, (flags & MSG_DONTWAIT), &err);
1641 lock_sock(sk);
1642 if (!skb)
1643 goto out;
1644
1645 skb_reserve(skb, ddp_dl->header_length);
1646 skb_reserve(skb, dev->hard_header_len);
1647 skb->dev = dev;
1648
1649 SOCK_DEBUG(sk, "SK %p: Begin build.\n", sk);
1650
1651 ddp = (struct ddpehdr *)skb_put(skb, sizeof(struct ddpehdr));
1652 ddp->deh_len_hops = htons(len + sizeof(*ddp));
1653 ddp->deh_dnet = usat->sat_addr.s_net;
1654 ddp->deh_snet = at->src_net;
1655 ddp->deh_dnode = usat->sat_addr.s_node;
1656 ddp->deh_snode = at->src_node;
1657 ddp->deh_dport = usat->sat_port;
1658 ddp->deh_sport = at->src_port;
1659
1660 SOCK_DEBUG(sk, "SK %p: Copy user data (%Zd bytes).\n", sk, len);
1661
1662 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1663 if (err) {
1664 kfree_skb(skb);
1665 err = -EFAULT;
1666 goto out;
1667 }
1668
1669 if (sk->sk_no_check_tx)
1670 ddp->deh_sum = 0;
1671 else
1672 ddp->deh_sum = atalk_checksum(skb, len + sizeof(*ddp));
1673
1674
1675
1676
1677
1678 if (ddp->deh_dnode == ATADDR_BCAST &&
1679 !(rt->flags & RTF_GATEWAY) && !(dev->flags & IFF_LOOPBACK)) {
1680 struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL);
1681
1682 if (skb2) {
1683 loopback = 1;
1684 SOCK_DEBUG(sk, "SK %p: send out(copy).\n", sk);
1685
1686
1687
1688 aarp_send_ddp(dev, skb2, &usat->sat_addr, NULL);
1689 }
1690 }
1691
1692 if (dev->flags & IFF_LOOPBACK || loopback) {
1693 SOCK_DEBUG(sk, "SK %p: Loop back.\n", sk);
1694
1695 skb_orphan(skb);
1696 if (ddp->deh_dnode == ATADDR_BCAST) {
1697 struct atalk_addr at_lo;
1698
1699 at_lo.s_node = 0;
1700 at_lo.s_net = 0;
1701
1702 rt = atrtr_find(&at_lo);
1703 if (!rt) {
1704 kfree_skb(skb);
1705 err = -ENETUNREACH;
1706 goto out;
1707 }
1708 dev = rt->dev;
1709 skb->dev = dev;
1710 }
1711 ddp_dl->request(ddp_dl, skb, dev->dev_addr);
1712 } else {
1713 SOCK_DEBUG(sk, "SK %p: send out.\n", sk);
1714 if (rt->flags & RTF_GATEWAY) {
1715 gsat.sat_addr = rt->gateway;
1716 usat = &gsat;
1717 }
1718
1719
1720
1721
1722 aarp_send_ddp(dev, skb, &usat->sat_addr, NULL);
1723 }
1724 SOCK_DEBUG(sk, "SK %p: Done write (%Zd).\n", sk, len);
1725
1726out:
1727 release_sock(sk);
1728 return err ? : len;
1729}
1730
1731static int atalk_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
1732 size_t size, int flags)
1733{
1734 struct sock *sk = sock->sk;
1735 struct ddpehdr *ddp;
1736 int copied = 0;
1737 int offset = 0;
1738 int err = 0;
1739 struct sk_buff *skb;
1740
1741 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1742 flags & MSG_DONTWAIT, &err);
1743 lock_sock(sk);
1744
1745 if (!skb)
1746 goto out;
1747
1748
1749 ddp = ddp_hdr(skb);
1750 copied = ntohs(ddp->deh_len_hops) & 1023;
1751
1752 if (sk->sk_type != SOCK_RAW) {
1753 offset = sizeof(*ddp);
1754 copied -= offset;
1755 }
1756
1757 if (copied > size) {
1758 copied = size;
1759 msg->msg_flags |= MSG_TRUNC;
1760 }
1761 err = skb_copy_datagram_iovec(skb, offset, msg->msg_iov, copied);
1762
1763 if (!err && msg->msg_name) {
1764 DECLARE_SOCKADDR(struct sockaddr_at *, sat, msg->msg_name);
1765 sat->sat_family = AF_APPLETALK;
1766 sat->sat_port = ddp->deh_sport;
1767 sat->sat_addr.s_node = ddp->deh_snode;
1768 sat->sat_addr.s_net = ddp->deh_snet;
1769 msg->msg_namelen = sizeof(*sat);
1770 }
1771
1772 skb_free_datagram(sk, skb);
1773
1774out:
1775 release_sock(sk);
1776 return err ? : copied;
1777}
1778
1779
1780
1781
1782
1783static int atalk_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1784{
1785 int rc = -ENOIOCTLCMD;
1786 struct sock *sk = sock->sk;
1787 void __user *argp = (void __user *)arg;
1788
1789 switch (cmd) {
1790
1791 case TIOCOUTQ: {
1792 long amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1793
1794 if (amount < 0)
1795 amount = 0;
1796 rc = put_user(amount, (int __user *)argp);
1797 break;
1798 }
1799 case TIOCINQ: {
1800
1801
1802
1803
1804 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
1805 long amount = 0;
1806
1807 if (skb)
1808 amount = skb->len - sizeof(struct ddpehdr);
1809 rc = put_user(amount, (int __user *)argp);
1810 break;
1811 }
1812 case SIOCGSTAMP:
1813 rc = sock_get_timestamp(sk, argp);
1814 break;
1815 case SIOCGSTAMPNS:
1816 rc = sock_get_timestampns(sk, argp);
1817 break;
1818
1819 case SIOCADDRT:
1820 case SIOCDELRT:
1821 rc = -EPERM;
1822 if (capable(CAP_NET_ADMIN))
1823 rc = atrtr_ioctl(cmd, argp);
1824 break;
1825
1826 case SIOCGIFADDR:
1827 case SIOCSIFADDR:
1828 case SIOCGIFBRDADDR:
1829 case SIOCATALKDIFADDR:
1830 case SIOCDIFADDR:
1831 case SIOCSARP:
1832 case SIOCDARP:
1833 rtnl_lock();
1834 rc = atif_ioctl(cmd, argp);
1835 rtnl_unlock();
1836 break;
1837 }
1838
1839 return rc;
1840}
1841
1842
1843#ifdef CONFIG_COMPAT
1844static int atalk_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1845{
1846
1847
1848
1849
1850
1851
1852 if (cmd == SIOCATALKDIFADDR)
1853 return atalk_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
1854
1855 return -ENOIOCTLCMD;
1856}
1857#endif
1858
1859
1860static const struct net_proto_family atalk_family_ops = {
1861 .family = PF_APPLETALK,
1862 .create = atalk_create,
1863 .owner = THIS_MODULE,
1864};
1865
1866static const struct proto_ops atalk_dgram_ops = {
1867 .family = PF_APPLETALK,
1868 .owner = THIS_MODULE,
1869 .release = atalk_release,
1870 .bind = atalk_bind,
1871 .connect = atalk_connect,
1872 .socketpair = sock_no_socketpair,
1873 .accept = sock_no_accept,
1874 .getname = atalk_getname,
1875 .poll = datagram_poll,
1876 .ioctl = atalk_ioctl,
1877#ifdef CONFIG_COMPAT
1878 .compat_ioctl = atalk_compat_ioctl,
1879#endif
1880 .listen = sock_no_listen,
1881 .shutdown = sock_no_shutdown,
1882 .setsockopt = sock_no_setsockopt,
1883 .getsockopt = sock_no_getsockopt,
1884 .sendmsg = atalk_sendmsg,
1885 .recvmsg = atalk_recvmsg,
1886 .mmap = sock_no_mmap,
1887 .sendpage = sock_no_sendpage,
1888};
1889
1890static struct notifier_block ddp_notifier = {
1891 .notifier_call = ddp_device_event,
1892};
1893
1894static struct packet_type ltalk_packet_type __read_mostly = {
1895 .type = cpu_to_be16(ETH_P_LOCALTALK),
1896 .func = ltalk_rcv,
1897};
1898
1899static struct packet_type ppptalk_packet_type __read_mostly = {
1900 .type = cpu_to_be16(ETH_P_PPPTALK),
1901 .func = atalk_rcv,
1902};
1903
1904static unsigned char ddp_snap_id[] = { 0x08, 0x00, 0x07, 0x80, 0x9B };
1905
1906
1907EXPORT_SYMBOL(atrtr_get_dev);
1908EXPORT_SYMBOL(atalk_find_dev_addr);
1909
1910static const char atalk_err_snap[] __initconst =
1911 KERN_CRIT "Unable to register DDP with SNAP.\n";
1912
1913
1914static int __init atalk_init(void)
1915{
1916 int rc = proto_register(&ddp_proto, 0);
1917
1918 if (rc != 0)
1919 goto out;
1920
1921 (void)sock_register(&atalk_family_ops);
1922 ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv);
1923 if (!ddp_dl)
1924 printk(atalk_err_snap);
1925
1926 dev_add_pack(<alk_packet_type);
1927 dev_add_pack(&ppptalk_packet_type);
1928
1929 register_netdevice_notifier(&ddp_notifier);
1930 aarp_proto_init();
1931 atalk_proc_init();
1932 atalk_register_sysctl();
1933out:
1934 return rc;
1935}
1936module_init(atalk_init);
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947static void __exit atalk_exit(void)
1948{
1949#ifdef CONFIG_SYSCTL
1950 atalk_unregister_sysctl();
1951#endif
1952 atalk_proc_exit();
1953 aarp_cleanup_module();
1954 unregister_netdevice_notifier(&ddp_notifier);
1955 dev_remove_pack(<alk_packet_type);
1956 dev_remove_pack(&ppptalk_packet_type);
1957 unregister_snap_client(ddp_dl);
1958 sock_unregister(PF_APPLETALK);
1959 proto_unregister(&ddp_proto);
1960}
1961module_exit(atalk_exit);
1962
1963MODULE_LICENSE("GPL");
1964MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
1965MODULE_DESCRIPTION("AppleTalk 0.20\n");
1966MODULE_ALIAS_NETPROTO(PF_APPLETALK);
1967