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