1 /*
2 * DDP: An implementation of the AppleTalk DDP protocol for
3 * Ethernet 'ELAP'.
4 *
5 * Alan Cox <alan@lxorguk.ukuu.org.uk>
6 *
7 * With more than a little assistance from
8 *
9 * Wesley Craig <netatalk@umich.edu>
10 *
11 * Fixes:
12 * Neil Horman : Added missing device ioctls
13 * Michael Callahan : Made routing work
14 * Wesley Craig : Fix probing to listen to a
15 * passed node id.
16 * Alan Cox : Added send/recvmsg support
17 * Alan Cox : Moved at. to protinfo in
18 * socket.
19 * Alan Cox : Added firewall hooks.
20 * Alan Cox : Supports new ARPHRD_LOOPBACK
21 * Christer Weinigel : Routing and /proc fixes.
22 * Bradford Johnson : LocalTalk.
23 * Tom Dyas : Module support.
24 * Alan Cox : Hooks for PPP (based on the
25 * LocalTalk hook).
26 * Alan Cox : Posix bits
27 * Alan Cox/Mike Freeman : Possible fix to NBP problems
28 * Bradford Johnson : IP-over-DDP (experimental)
29 * Jay Schulist : Moved IP-over-DDP to its own
30 * driver file. (ipddp.c & ipddp.h)
31 * Jay Schulist : Made work as module with
32 * AppleTalk drivers, cleaned it.
33 * Rob Newberry : Added proxy AARP and AARP
34 * procfs, moved probing to AARP
35 * module.
36 * Adrian Sun/
37 * Michael Zuelsdorff : fix for net.0 packets. don't
38 * allow illegal ether/tokentalk
39 * port assignment. we lose a
40 * valid localtalk port as a
41 * result.
42 * Arnaldo C. de Melo : Cleanup, in preparation for
43 * shared skb support 8)
44 * Arnaldo C. de Melo : Move proc stuff to atalk_proc.c,
45 * use seq_file
46 *
47 * This program is free software; you can redistribute it and/or
48 * modify it under the terms of the GNU General Public License
49 * as published by the Free Software Foundation; either version
50 * 2 of the License, or (at your option) any later version.
51 *
52 */
53
54 #include <linux/capability.h>
55 #include <linux/module.h>
56 #include <linux/if_arp.h>
57 #include <linux/termios.h> /* For TIOCOUTQ/INQ */
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
68 struct datalink_proto *ddp_dl, *aarp_dl;
69 static const struct proto_ops atalk_dgram_ops;
70
71 /**************************************************************************\
72 * *
73 * Handlers for the socket list. *
74 * *
75 \**************************************************************************/
76
77 HLIST_HEAD(atalk_sockets);
78 DEFINE_RWLOCK(atalk_sockets_lock);
79
__atalk_insert_socket(struct sock * sk)80 static inline void __atalk_insert_socket(struct sock *sk)
81 {
82 sk_add_node(sk, &atalk_sockets);
83 }
84
atalk_remove_socket(struct sock * sk)85 static 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
atalk_search_socket(struct sockaddr_at * to,struct atalk_iface * atif)92 static 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 /* XXXX.0 -- we got a request for this router. make sure
115 * that the node is appropriately set. */
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;
124 found:
125 read_unlock_bh(&atalk_sockets_lock);
126 return s;
127 }
128
129 /**
130 * atalk_find_or_insert_socket - Try to find a socket matching ADDR
131 * @sk: socket to insert in the list if it is not there already
132 * @sat: address to search for
133 *
134 * Try to find a socket matching ADDR in the socket list, if found then return
135 * it. If not, insert SK into the socket list.
136 *
137 * This entire operation must execute atomically.
138 */
atalk_find_or_insert_socket(struct sock * sk,struct sockaddr_at * sat)139 static 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); /* Wheee, it's free, assign and insert. */
156 found:
157 write_unlock_bh(&atalk_sockets_lock);
158 return s;
159 }
160
atalk_destroy_timer(unsigned long data)161 static 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
atalk_destroy_socket(struct sock * sk)172 static 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 * Routing tables for the AppleTalk socket layer. *
189 * *
190 \**************************************************************************/
191
192 /* Anti-deadlock ordering is atalk_routes_lock --> iface_lock -DaveM */
193 struct atalk_route *atalk_routes;
194 DEFINE_RWLOCK(atalk_routes_lock);
195
196 struct atalk_iface *atalk_interfaces;
197 DEFINE_RWLOCK(atalk_interfaces_lock);
198
199 /* For probing devices or in a routerless network */
200 struct atalk_route atrtr_default;
201
202 /* AppleTalk interface control */
203 /*
204 * Drop a device. Doesn't drop any of its routes - that is the caller's
205 * problem. Called when we down the interface or delete the address.
206 */
atif_drop_device(struct net_device * dev)207 static 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
atif_add_device(struct net_device * dev,struct atalk_addr * sa)225 static 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);
243 out:
244 return iface;
245 }
246
247 /* Perform phase 2 AARP probing on our tentative address */
atif_probe_device(struct atalk_iface * atif)248 static 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 /* Offset the network we start probing with */
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 /* Scan the networks */
266 atif->status |= ATIF_PROBE;
267 for (netct = 0; netct <= netrange; netct++) {
268 /* Sweep the available nodes from a given start */
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 /* Probe a proposed address */
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; /* Network is full... */
291 }
292
293
294 /* Perform AARP probing for a proxy address */
atif_proxy_probe_device(struct atalk_iface * atif,struct atalk_addr * proxy_addr)295 static 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 /* we probe the interface's network */
301 int probe_net = ntohs(atif->address.s_net);
302 int probe_node = ATADDR_ANYNODE; /* we'll take anything */
303 int netct, nodect;
304
305 /* Offset the network we start probing with */
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 /* Scan the networks */
316 for (netct = 0; netct <= netrange; netct++) {
317 /* Sweep the available nodes from a given start */
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 /* Tell AARP to probe a proposed address */
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; /* Network is full... */
337 }
338
339
atalk_find_dev_addr(struct net_device * dev)340 struct 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
atalk_find_primary(void)346 static 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 * Return a point-to-point interface only if
354 * there is no non-ptp interface available.
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;
372 out:
373 read_unlock_bh(&atalk_interfaces_lock);
374 return retval;
375 }
376
377 /*
378 * Find a match for 'any network' - ie any of our interfaces with that
379 * node number will do just nicely.
380 */
atalk_find_anynet(int node,struct net_device * dev)381 static 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;
392 out:
393 return iface;
394 out_err:
395 iface = NULL;
396 goto out;
397 }
398
399 /* Find a match for a specific network:node pair */
atalk_find_interface(__be16 net,int node)400 static 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 /* XXXX.0 -- net.0 returns the iface associated with net */
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 * Find a route for an AppleTalk packet. This ought to get cached in
426 * the socket (later on...). We know about host routes and the fact
427 * that a route must be direct to broadcast.
428 */
atrtr_find(struct atalk_addr * target)429 static struct atalk_route *atrtr_find(struct atalk_addr *target)
430 {
431 /*
432 * we must search through all routes unless we find a
433 * host route, because some host routes might overlap
434 * network routes
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 * if this host route is for the target,
448 * the we're done
449 */
450 if (r->target.s_node == target->s_node)
451 goto out;
452 } else
453 /*
454 * this route will work if there isn't a
455 * direct host route, so cache it
456 */
457 net_route = r;
458 }
459 }
460
461 /*
462 * if we found a network route but not a direct host
463 * route, then return it
464 */
465 if (net_route)
466 r = net_route;
467 else if (atrtr_default.dev)
468 r = &atrtr_default;
469 else /* No route can be found */
470 r = NULL;
471 out:
472 read_unlock_bh(&atalk_routes_lock);
473 return r;
474 }
475
476
477 /*
478 * Given an AppleTalk network, find the device to use. This can be
479 * a simple lookup.
480 */
atrtr_get_dev(struct atalk_addr * sa)481 struct 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 /* Set up a default router */
atrtr_set_default(struct net_device * dev)488 static 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 * Add a router. Basically make sure it looks valid and stuff the
498 * entry in the list. While it uses netranges we always set them to one
499 * entry to work like netatalk.
500 */
atrtr_create(struct rtentry * r,struct net_device * devhint)501 static 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 * Fixme: Raise/Lower a routing change semaphore for these
511 * operations.
512 */
513
514 /* Validate the request */
515 if (ta->sat_family != AF_APPLETALK ||
516 (!devhint && ga->sat_family != AF_APPLETALK))
517 goto out;
518
519 /* Now walk the routing table and make our decisions */
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 /* Fill in the routing entry */
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;
577 out_unlock:
578 write_unlock_bh(&atalk_routes_lock);
579 out:
580 return retval;
581 }
582
583 /* Delete a route. Find it and discard it */
atrtr_delete(struct atalk_addr * addr)584 static 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;
603 out:
604 write_unlock_bh(&atalk_routes_lock);
605 return retval;
606 }
607
608 /*
609 * Called when a device is downed. Just throw away any routes
610 * via it.
611 */
atrtr_device_down(struct net_device * dev)612 static 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 /* Actually down the interface */
atalk_dev_down(struct net_device * dev)633 static inline void atalk_dev_down(struct net_device *dev)
634 {
635 atrtr_device_down(dev); /* Remove all routes for the device */
636 aarp_device_down(dev); /* Remove AARP entries for the device */
637 atif_drop_device(dev); /* Remove the device */
638 }
639
640 /*
641 * A device event has occurred. Watch for devices going down and
642 * delete our use of them (iface and route).
643 */
ddp_device_event(struct notifier_block * this,unsigned long event,void * ptr)644 static 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 /* Discard any use of this */
654 atalk_dev_down(dev);
655
656 return NOTIFY_DONE;
657 }
658
659 /* ioctl calls. Shouldn't even need touching */
660 /* Device configuration ioctl calls */
atif_ioctl(int cmd,void __user * arg)661 static 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 * if this is a point-to-point iface, and we already
701 * have an iface for this AppleTalk address, then we
702 * should not add a route
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 * Phase 1 is fine on LocalTalk but we don't do
715 * EtherTalk phase 1. Anyone wanting to add it go ahead.
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 /* Already setting address */
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); /* Flush old routes */
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 * Check if the chosen address is used. If so we
739 * error and atalkd will try another.
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 /* Hey it worked - add the direct routes */
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 /* Routerless initial state */
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 * for now, we only support proxy AARP on ELAP;
818 * we should be able to do it for LocalTalk, too.
819 */
820 if (dev->type != ARPHRD_ETHER)
821 return -EPROTONOSUPPORT;
822
823 /*
824 * atif points to the current interface on this network;
825 * we aren't concerned about its current status (at
826 * least for now), but it has all the settings about
827 * the network we're going to probe. Consequently, it
828 * must exist.
829 */
830 if (!atif)
831 return -EADDRNOTAVAIL;
832
833 nr = (struct atalk_netrange *)&(atif->nets);
834 /*
835 * Phase 1 is fine on Localtalk but we don't do
836 * Ethertalk phase 1. Anyone wanting to add it go ahead.
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 * Check if the chosen address is used. If so we
847 * error and ATCP will try another.
848 */
849 if (atif_proxy_probe_device(atif, &(sa->sat_addr)) < 0)
850 return -EADDRINUSE;
851
852 /*
853 * We now have an address on the local network, and
854 * the AARP code will defend it for us until we take it
855 * down. We don't set up any routes right now, because
856 * ATCP will install them manually via SIOCADDRT.
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 /* give to aarp module to remove proxy entry */
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 /* Routing ioctl() calls */
atrtr_ioctl(unsigned int cmd,void __user * arg)877 static 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 * Handling for system calls applied via the various interfaces to an *
911 * AppleTalk socket object. *
912 * *
913 \**************************************************************************/
914
915 /*
916 * Checksum: This is 'optional'. It's quite likely also a good
917 * candidate for assembler hackery 8)
918 */
atalk_sum_partial(const unsigned char * data,int len,unsigned long sum)919 static unsigned long atalk_sum_partial(const unsigned char *data,
920 int len, unsigned long sum)
921 {
922 /* This ought to be unwrapped neatly. I'll trust gcc for now */
923 while (len--) {
924 sum += *data++;
925 sum = rol16(sum, 1);
926 }
927 return sum;
928 }
929
930 /* Checksum skb data -- similar to skb_checksum */
atalk_sum_skb(const struct sk_buff * skb,int offset,int len,unsigned long sum)931 static 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 /* checksum stuff in header space */
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 /* checksum stuff in frags */
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
atalk_checksum(const struct sk_buff * skb,int len)996 static __be16 atalk_checksum(const struct sk_buff *skb, int len)
997 {
998 unsigned long sum;
999
1000 /* skip header 4 bytes */
1001 sum = atalk_sum_skb(skb, 4, len-4, 0);
1002
1003 /* Use 0xFFFF for 0. 0 itself means none */
1004 return sum ? htons((unsigned short)sum) : htons(0xFFFF);
1005 }
1006
1007 static struct proto ddp_proto = {
1008 .name = "DDP",
1009 .owner = THIS_MODULE,
1010 .obj_size = sizeof(struct atalk_sock),
1011 };
1012
1013 /*
1014 * Create a socket. Initialise the socket, blank the addresses
1015 * set the state.
1016 */
atalk_create(struct net * net,struct socket * sock,int protocol,int kern)1017 static 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 * We permit SOCK_DGRAM and RAW is an extension. It is trivial to do
1028 * and gives you the full ELAP frame. Should be handy for CAP 8)
1029 */
1030 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
1031 goto out;
1032
1033 rc = -EPERM;
1034 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1035 goto out;
1036
1037 rc = -ENOMEM;
1038 sk = sk_alloc(net, PF_APPLETALK, GFP_KERNEL, &ddp_proto, kern);
1039 if (!sk)
1040 goto out;
1041 rc = 0;
1042 sock->ops = &atalk_dgram_ops;
1043 sock_init_data(sock, sk);
1044
1045 /* Checksums on by default */
1046 sock_set_flag(sk, SOCK_ZAPPED);
1047 out:
1048 return rc;
1049 }
1050
1051 /* Free a socket. No work needed */
atalk_release(struct socket * sock)1052 static int atalk_release(struct socket *sock)
1053 {
1054 struct sock *sk = sock->sk;
1055
1056 if (sk) {
1057 sock_hold(sk);
1058 lock_sock(sk);
1059
1060 sock_orphan(sk);
1061 sock->sk = NULL;
1062 atalk_destroy_socket(sk);
1063
1064 release_sock(sk);
1065 sock_put(sk);
1066 }
1067 return 0;
1068 }
1069
1070 /**
1071 * atalk_pick_and_bind_port - Pick a source port when one is not given
1072 * @sk: socket to insert into the tables
1073 * @sat: address to search for
1074 *
1075 * Pick a source port when one is not given. If we can find a suitable free
1076 * one, we insert the socket into the tables using it.
1077 *
1078 * This whole operation must be atomic.
1079 */
atalk_pick_and_bind_port(struct sock * sk,struct sockaddr_at * sat)1080 static int atalk_pick_and_bind_port(struct sock *sk, struct sockaddr_at *sat)
1081 {
1082 int retval;
1083
1084 write_lock_bh(&atalk_sockets_lock);
1085
1086 for (sat->sat_port = ATPORT_RESERVED;
1087 sat->sat_port < ATPORT_LAST;
1088 sat->sat_port++) {
1089 struct sock *s;
1090
1091 sk_for_each(s, &atalk_sockets) {
1092 struct atalk_sock *at = at_sk(s);
1093
1094 if (at->src_net == sat->sat_addr.s_net &&
1095 at->src_node == sat->sat_addr.s_node &&
1096 at->src_port == sat->sat_port)
1097 goto try_next_port;
1098 }
1099
1100 /* Wheee, it's free, assign and insert. */
1101 __atalk_insert_socket(sk);
1102 at_sk(sk)->src_port = sat->sat_port;
1103 retval = 0;
1104 goto out;
1105
1106 try_next_port:;
1107 }
1108
1109 retval = -EBUSY;
1110 out:
1111 write_unlock_bh(&atalk_sockets_lock);
1112 return retval;
1113 }
1114
atalk_autobind(struct sock * sk)1115 static int atalk_autobind(struct sock *sk)
1116 {
1117 struct atalk_sock *at = at_sk(sk);
1118 struct sockaddr_at sat;
1119 struct atalk_addr *ap = atalk_find_primary();
1120 int n = -EADDRNOTAVAIL;
1121
1122 if (!ap || ap->s_net == htons(ATADDR_ANYNET))
1123 goto out;
1124
1125 at->src_net = sat.sat_addr.s_net = ap->s_net;
1126 at->src_node = sat.sat_addr.s_node = ap->s_node;
1127
1128 n = atalk_pick_and_bind_port(sk, &sat);
1129 if (!n)
1130 sock_reset_flag(sk, SOCK_ZAPPED);
1131 out:
1132 return n;
1133 }
1134
1135 /* Set the address 'our end' of the connection */
atalk_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)1136 static int atalk_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1137 {
1138 struct sockaddr_at *addr = (struct sockaddr_at *)uaddr;
1139 struct sock *sk = sock->sk;
1140 struct atalk_sock *at = at_sk(sk);
1141 int err;
1142
1143 if (!sock_flag(sk, SOCK_ZAPPED) ||
1144 addr_len != sizeof(struct sockaddr_at))
1145 return -EINVAL;
1146
1147 if (addr->sat_family != AF_APPLETALK)
1148 return -EAFNOSUPPORT;
1149
1150 lock_sock(sk);
1151 if (addr->sat_addr.s_net == htons(ATADDR_ANYNET)) {
1152 struct atalk_addr *ap = atalk_find_primary();
1153
1154 err = -EADDRNOTAVAIL;
1155 if (!ap)
1156 goto out;
1157
1158 at->src_net = addr->sat_addr.s_net = ap->s_net;
1159 at->src_node = addr->sat_addr.s_node = ap->s_node;
1160 } else {
1161 err = -EADDRNOTAVAIL;
1162 if (!atalk_find_interface(addr->sat_addr.s_net,
1163 addr->sat_addr.s_node))
1164 goto out;
1165
1166 at->src_net = addr->sat_addr.s_net;
1167 at->src_node = addr->sat_addr.s_node;
1168 }
1169
1170 if (addr->sat_port == ATADDR_ANYPORT) {
1171 err = atalk_pick_and_bind_port(sk, addr);
1172
1173 if (err < 0)
1174 goto out;
1175 } else {
1176 at->src_port = addr->sat_port;
1177
1178 err = -EADDRINUSE;
1179 if (atalk_find_or_insert_socket(sk, addr))
1180 goto out;
1181 }
1182
1183 sock_reset_flag(sk, SOCK_ZAPPED);
1184 err = 0;
1185 out:
1186 release_sock(sk);
1187 return err;
1188 }
1189
1190 /* Set the address we talk to */
atalk_connect(struct socket * sock,struct sockaddr * uaddr,int addr_len,int flags)1191 static int atalk_connect(struct socket *sock, struct sockaddr *uaddr,
1192 int addr_len, int flags)
1193 {
1194 struct sock *sk = sock->sk;
1195 struct atalk_sock *at = at_sk(sk);
1196 struct sockaddr_at *addr;
1197 int err;
1198
1199 sk->sk_state = TCP_CLOSE;
1200 sock->state = SS_UNCONNECTED;
1201
1202 if (addr_len != sizeof(*addr))
1203 return -EINVAL;
1204
1205 addr = (struct sockaddr_at *)uaddr;
1206
1207 if (addr->sat_family != AF_APPLETALK)
1208 return -EAFNOSUPPORT;
1209
1210 if (addr->sat_addr.s_node == ATADDR_BCAST &&
1211 !sock_flag(sk, SOCK_BROADCAST)) {
1212 #if 1
1213 pr_warn("atalk_connect: %s is broken and did not set SO_BROADCAST.\n",
1214 current->comm);
1215 #else
1216 return -EACCES;
1217 #endif
1218 }
1219
1220 lock_sock(sk);
1221 err = -EBUSY;
1222 if (sock_flag(sk, SOCK_ZAPPED))
1223 if (atalk_autobind(sk) < 0)
1224 goto out;
1225
1226 err = -ENETUNREACH;
1227 if (!atrtr_get_dev(&addr->sat_addr))
1228 goto out;
1229
1230 at->dest_port = addr->sat_port;
1231 at->dest_net = addr->sat_addr.s_net;
1232 at->dest_node = addr->sat_addr.s_node;
1233
1234 sock->state = SS_CONNECTED;
1235 sk->sk_state = TCP_ESTABLISHED;
1236 err = 0;
1237 out:
1238 release_sock(sk);
1239 return err;
1240 }
1241
1242 /*
1243 * Find the name of an AppleTalk socket. Just copy the right
1244 * fields into the sockaddr.
1245 */
atalk_getname(struct socket * sock,struct sockaddr * uaddr,int * uaddr_len,int peer)1246 static int atalk_getname(struct socket *sock, struct sockaddr *uaddr,
1247 int *uaddr_len, int peer)
1248 {
1249 struct sockaddr_at sat;
1250 struct sock *sk = sock->sk;
1251 struct atalk_sock *at = at_sk(sk);
1252 int err;
1253
1254 lock_sock(sk);
1255 err = -ENOBUFS;
1256 if (sock_flag(sk, SOCK_ZAPPED))
1257 if (atalk_autobind(sk) < 0)
1258 goto out;
1259
1260 *uaddr_len = sizeof(struct sockaddr_at);
1261 memset(&sat, 0, sizeof(sat));
1262
1263 if (peer) {
1264 err = -ENOTCONN;
1265 if (sk->sk_state != TCP_ESTABLISHED)
1266 goto out;
1267
1268 sat.sat_addr.s_net = at->dest_net;
1269 sat.sat_addr.s_node = at->dest_node;
1270 sat.sat_port = at->dest_port;
1271 } else {
1272 sat.sat_addr.s_net = at->src_net;
1273 sat.sat_addr.s_node = at->src_node;
1274 sat.sat_port = at->src_port;
1275 }
1276
1277 err = 0;
1278 sat.sat_family = AF_APPLETALK;
1279 memcpy(uaddr, &sat, sizeof(sat));
1280
1281 out:
1282 release_sock(sk);
1283 return err;
1284 }
1285
1286 #if defined(CONFIG_IPDDP) || defined(CONFIG_IPDDP_MODULE)
is_ip_over_ddp(struct sk_buff * skb)1287 static __inline__ int is_ip_over_ddp(struct sk_buff *skb)
1288 {
1289 return skb->data[12] == 22;
1290 }
1291
handle_ip_over_ddp(struct sk_buff * skb)1292 static int handle_ip_over_ddp(struct sk_buff *skb)
1293 {
1294 struct net_device *dev = __dev_get_by_name(&init_net, "ipddp0");
1295 struct net_device_stats *stats;
1296
1297 /* This needs to be able to handle ipddp"N" devices */
1298 if (!dev) {
1299 kfree_skb(skb);
1300 return NET_RX_DROP;
1301 }
1302
1303 skb->protocol = htons(ETH_P_IP);
1304 skb_pull(skb, 13);
1305 skb->dev = dev;
1306 skb_reset_transport_header(skb);
1307
1308 stats = netdev_priv(dev);
1309 stats->rx_packets++;
1310 stats->rx_bytes += skb->len + 13;
1311 return netif_rx(skb); /* Send the SKB up to a higher place. */
1312 }
1313 #else
1314 /* make it easy for gcc to optimize this test out, i.e. kill the code */
1315 #define is_ip_over_ddp(skb) 0
1316 #define handle_ip_over_ddp(skb) 0
1317 #endif
1318
atalk_route_packet(struct sk_buff * skb,struct net_device * dev,struct ddpehdr * ddp,__u16 len_hops,int origlen)1319 static int atalk_route_packet(struct sk_buff *skb, struct net_device *dev,
1320 struct ddpehdr *ddp, __u16 len_hops, int origlen)
1321 {
1322 struct atalk_route *rt;
1323 struct atalk_addr ta;
1324
1325 /*
1326 * Don't route multicast, etc., packets, or packets sent to "this
1327 * network"
1328 */
1329 if (skb->pkt_type != PACKET_HOST || !ddp->deh_dnet) {
1330 /*
1331 * FIXME:
1332 *
1333 * Can it ever happen that a packet is from a PPP iface and
1334 * needs to be broadcast onto the default network?
1335 */
1336 if (dev->type == ARPHRD_PPP)
1337 printk(KERN_DEBUG "AppleTalk: didn't forward broadcast "
1338 "packet received from PPP iface\n");
1339 goto free_it;
1340 }
1341
1342 ta.s_net = ddp->deh_dnet;
1343 ta.s_node = ddp->deh_dnode;
1344
1345 /* Route the packet */
1346 rt = atrtr_find(&ta);
1347 /* increment hops count */
1348 len_hops += 1 << 10;
1349 if (!rt || !(len_hops & (15 << 10)))
1350 goto free_it;
1351
1352 /* FIXME: use skb->cb to be able to use shared skbs */
1353
1354 /*
1355 * Route goes through another gateway, so set the target to the
1356 * gateway instead.
1357 */
1358
1359 if (rt->flags & RTF_GATEWAY) {
1360 ta.s_net = rt->gateway.s_net;
1361 ta.s_node = rt->gateway.s_node;
1362 }
1363
1364 /* Fix up skb->len field */
1365 skb_trim(skb, min_t(unsigned int, origlen,
1366 (rt->dev->hard_header_len +
1367 ddp_dl->header_length + (len_hops & 1023))));
1368
1369 /* FIXME: use skb->cb to be able to use shared skbs */
1370 ddp->deh_len_hops = htons(len_hops);
1371
1372 /*
1373 * Send the buffer onwards
1374 *
1375 * Now we must always be careful. If it's come from LocalTalk to
1376 * EtherTalk it might not fit
1377 *
1378 * Order matters here: If a packet has to be copied to make a new
1379 * headroom (rare hopefully) then it won't need unsharing.
1380 *
1381 * Note. ddp-> becomes invalid at the realloc.
1382 */
1383 if (skb_headroom(skb) < 22) {
1384 /* 22 bytes - 12 ether, 2 len, 3 802.2 5 snap */
1385 struct sk_buff *nskb = skb_realloc_headroom(skb, 32);
1386 kfree_skb(skb);
1387 skb = nskb;
1388 } else
1389 skb = skb_unshare(skb, GFP_ATOMIC);
1390
1391 /*
1392 * If the buffer didn't vanish into the lack of space bitbucket we can
1393 * send it.
1394 */
1395 if (skb == NULL)
1396 goto drop;
1397
1398 if (aarp_send_ddp(rt->dev, skb, &ta, NULL) == NET_XMIT_DROP)
1399 return NET_RX_DROP;
1400 return NET_RX_SUCCESS;
1401 free_it:
1402 kfree_skb(skb);
1403 drop:
1404 return NET_RX_DROP;
1405 }
1406
1407 /**
1408 * atalk_rcv - Receive a packet (in skb) from device dev
1409 * @skb - packet received
1410 * @dev - network device where the packet comes from
1411 * @pt - packet type
1412 *
1413 * Receive a packet (in skb) from device dev. This has come from the SNAP
1414 * decoder, and on entry skb->transport_header is the DDP header, skb->len
1415 * is the DDP header, skb->len is the DDP length. The physical headers
1416 * have been extracted. PPP should probably pass frames marked as for this
1417 * layer. [ie ARPHRD_ETHERTALK]
1418 */
atalk_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt,struct net_device * orig_dev)1419 static int atalk_rcv(struct sk_buff *skb, struct net_device *dev,
1420 struct packet_type *pt, struct net_device *orig_dev)
1421 {
1422 struct ddpehdr *ddp;
1423 struct sock *sock;
1424 struct atalk_iface *atif;
1425 struct sockaddr_at tosat;
1426 int origlen;
1427 __u16 len_hops;
1428
1429 if (!net_eq(dev_net(dev), &init_net))
1430 goto drop;
1431
1432 /* Don't mangle buffer if shared */
1433 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
1434 goto out;
1435
1436 /* Size check and make sure header is contiguous */
1437 if (!pskb_may_pull(skb, sizeof(*ddp)))
1438 goto drop;
1439
1440 ddp = ddp_hdr(skb);
1441
1442 len_hops = ntohs(ddp->deh_len_hops);
1443
1444 /* Trim buffer in case of stray trailing data */
1445 origlen = skb->len;
1446 skb_trim(skb, min_t(unsigned int, skb->len, len_hops & 1023));
1447
1448 /*
1449 * Size check to see if ddp->deh_len was crap
1450 * (Otherwise we'll detonate most spectacularly
1451 * in the middle of atalk_checksum() or recvmsg()).
1452 */
1453 if (skb->len < sizeof(*ddp) || skb->len < (len_hops & 1023)) {
1454 pr_debug("AppleTalk: dropping corrupted frame (deh_len=%u, "
1455 "skb->len=%u)\n", len_hops & 1023, skb->len);
1456 goto drop;
1457 }
1458
1459 /*
1460 * Any checksums. Note we don't do htons() on this == is assumed to be
1461 * valid for net byte orders all over the networking code...
1462 */
1463 if (ddp->deh_sum &&
1464 atalk_checksum(skb, len_hops & 1023) != ddp->deh_sum)
1465 /* Not a valid AppleTalk frame - dustbin time */
1466 goto drop;
1467
1468 /* Check the packet is aimed at us */
1469 if (!ddp->deh_dnet) /* Net 0 is 'this network' */
1470 atif = atalk_find_anynet(ddp->deh_dnode, dev);
1471 else
1472 atif = atalk_find_interface(ddp->deh_dnet, ddp->deh_dnode);
1473
1474 if (!atif) {
1475 /* Not ours, so we route the packet via the correct
1476 * AppleTalk iface
1477 */
1478 return atalk_route_packet(skb, dev, ddp, len_hops, origlen);
1479 }
1480
1481 /* if IP over DDP is not selected this code will be optimized out */
1482 if (is_ip_over_ddp(skb))
1483 return handle_ip_over_ddp(skb);
1484 /*
1485 * Which socket - atalk_search_socket() looks for a *full match*
1486 * of the <net, node, port> tuple.
1487 */
1488 tosat.sat_addr.s_net = ddp->deh_dnet;
1489 tosat.sat_addr.s_node = ddp->deh_dnode;
1490 tosat.sat_port = ddp->deh_dport;
1491
1492 sock = atalk_search_socket(&tosat, atif);
1493 if (!sock) /* But not one of our sockets */
1494 goto drop;
1495
1496 /* Queue packet (standard) */
1497 if (sock_queue_rcv_skb(sock, skb) < 0)
1498 goto drop;
1499
1500 return NET_RX_SUCCESS;
1501
1502 drop:
1503 kfree_skb(skb);
1504 out:
1505 return NET_RX_DROP;
1506
1507 }
1508
1509 /*
1510 * Receive a LocalTalk frame. We make some demands on the caller here.
1511 * Caller must provide enough headroom on the packet to pull the short
1512 * header and append a long one.
1513 */
ltalk_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt,struct net_device * orig_dev)1514 static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev,
1515 struct packet_type *pt, struct net_device *orig_dev)
1516 {
1517 if (!net_eq(dev_net(dev), &init_net))
1518 goto freeit;
1519
1520 /* Expand any short form frames */
1521 if (skb_mac_header(skb)[2] == 1) {
1522 struct ddpehdr *ddp;
1523 /* Find our address */
1524 struct atalk_addr *ap = atalk_find_dev_addr(dev);
1525
1526 if (!ap || skb->len < sizeof(__be16) || skb->len > 1023)
1527 goto freeit;
1528
1529 /* Don't mangle buffer if shared */
1530 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
1531 return 0;
1532
1533 /*
1534 * The push leaves us with a ddephdr not an shdr, and
1535 * handily the port bytes in the right place preset.
1536 */
1537 ddp = (struct ddpehdr *) skb_push(skb, sizeof(*ddp) - 4);
1538
1539 /* Now fill in the long header */
1540
1541 /*
1542 * These two first. The mac overlays the new source/dest
1543 * network information so we MUST copy these before
1544 * we write the network numbers !
1545 */
1546
1547 ddp->deh_dnode = skb_mac_header(skb)[0]; /* From physical header */
1548 ddp->deh_snode = skb_mac_header(skb)[1]; /* From physical header */
1549
1550 ddp->deh_dnet = ap->s_net; /* Network number */
1551 ddp->deh_snet = ap->s_net;
1552 ddp->deh_sum = 0; /* No checksum */
1553 /*
1554 * Not sure about this bit...
1555 */
1556 /* Non routable, so force a drop if we slip up later */
1557 ddp->deh_len_hops = htons(skb->len + (DDP_MAXHOPS << 10));
1558 }
1559 skb_reset_transport_header(skb);
1560
1561 return atalk_rcv(skb, dev, pt, orig_dev);
1562 freeit:
1563 kfree_skb(skb);
1564 return 0;
1565 }
1566
atalk_sendmsg(struct socket * sock,struct msghdr * msg,size_t len)1567 static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1568 {
1569 struct sock *sk = sock->sk;
1570 struct atalk_sock *at = at_sk(sk);
1571 DECLARE_SOCKADDR(struct sockaddr_at *, usat, msg->msg_name);
1572 int flags = msg->msg_flags;
1573 int loopback = 0;
1574 struct sockaddr_at local_satalk, gsat;
1575 struct sk_buff *skb;
1576 struct net_device *dev;
1577 struct ddpehdr *ddp;
1578 int size, hard_header_len;
1579 struct atalk_route *rt, *rt_lo = NULL;
1580 int err;
1581
1582 if (flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
1583 return -EINVAL;
1584
1585 if (len > DDP_MAXSZ)
1586 return -EMSGSIZE;
1587
1588 lock_sock(sk);
1589 if (usat) {
1590 err = -EBUSY;
1591 if (sock_flag(sk, SOCK_ZAPPED))
1592 if (atalk_autobind(sk) < 0)
1593 goto out;
1594
1595 err = -EINVAL;
1596 if (msg->msg_namelen < sizeof(*usat) ||
1597 usat->sat_family != AF_APPLETALK)
1598 goto out;
1599
1600 err = -EPERM;
1601 /* netatalk didn't implement this check */
1602 if (usat->sat_addr.s_node == ATADDR_BCAST &&
1603 !sock_flag(sk, SOCK_BROADCAST)) {
1604 goto out;
1605 }
1606 } else {
1607 err = -ENOTCONN;
1608 if (sk->sk_state != TCP_ESTABLISHED)
1609 goto out;
1610 usat = &local_satalk;
1611 usat->sat_family = AF_APPLETALK;
1612 usat->sat_port = at->dest_port;
1613 usat->sat_addr.s_node = at->dest_node;
1614 usat->sat_addr.s_net = at->dest_net;
1615 }
1616
1617 /* Build a packet */
1618 SOCK_DEBUG(sk, "SK %p: Got address.\n", sk);
1619
1620 /* For headers */
1621 size = sizeof(struct ddpehdr) + len + ddp_dl->header_length;
1622
1623 if (usat->sat_addr.s_net || usat->sat_addr.s_node == ATADDR_ANYNODE) {
1624 rt = atrtr_find(&usat->sat_addr);
1625 } else {
1626 struct atalk_addr at_hint;
1627
1628 at_hint.s_node = 0;
1629 at_hint.s_net = at->src_net;
1630
1631 rt = atrtr_find(&at_hint);
1632 }
1633 err = ENETUNREACH;
1634 if (!rt)
1635 goto out;
1636
1637 dev = rt->dev;
1638
1639 SOCK_DEBUG(sk, "SK %p: Size needed %d, device %s\n",
1640 sk, size, dev->name);
1641
1642 hard_header_len = dev->hard_header_len;
1643 /* Leave room for loopback hardware header if necessary */
1644 if (usat->sat_addr.s_node == ATADDR_BCAST &&
1645 (dev->flags & IFF_LOOPBACK || !(rt->flags & RTF_GATEWAY))) {
1646 struct atalk_addr at_lo;
1647
1648 at_lo.s_node = 0;
1649 at_lo.s_net = 0;
1650
1651 rt_lo = atrtr_find(&at_lo);
1652
1653 if (rt_lo && rt_lo->dev->hard_header_len > hard_header_len)
1654 hard_header_len = rt_lo->dev->hard_header_len;
1655 }
1656
1657 size += hard_header_len;
1658 release_sock(sk);
1659 skb = sock_alloc_send_skb(sk, size, (flags & MSG_DONTWAIT), &err);
1660 lock_sock(sk);
1661 if (!skb)
1662 goto out;
1663
1664 skb_reserve(skb, ddp_dl->header_length);
1665 skb_reserve(skb, hard_header_len);
1666 skb->dev = dev;
1667
1668 SOCK_DEBUG(sk, "SK %p: Begin build.\n", sk);
1669
1670 ddp = (struct ddpehdr *)skb_put(skb, sizeof(struct ddpehdr));
1671 ddp->deh_len_hops = htons(len + sizeof(*ddp));
1672 ddp->deh_dnet = usat->sat_addr.s_net;
1673 ddp->deh_snet = at->src_net;
1674 ddp->deh_dnode = usat->sat_addr.s_node;
1675 ddp->deh_snode = at->src_node;
1676 ddp->deh_dport = usat->sat_port;
1677 ddp->deh_sport = at->src_port;
1678
1679 SOCK_DEBUG(sk, "SK %p: Copy user data (%Zd bytes).\n", sk, len);
1680
1681 err = memcpy_from_msg(skb_put(skb, len), msg, len);
1682 if (err) {
1683 kfree_skb(skb);
1684 err = -EFAULT;
1685 goto out;
1686 }
1687
1688 if (sk->sk_no_check_tx)
1689 ddp->deh_sum = 0;
1690 else
1691 ddp->deh_sum = atalk_checksum(skb, len + sizeof(*ddp));
1692
1693 /*
1694 * Loopback broadcast packets to non gateway targets (ie routes
1695 * to group we are in)
1696 */
1697 if (ddp->deh_dnode == ATADDR_BCAST &&
1698 !(rt->flags & RTF_GATEWAY) && !(dev->flags & IFF_LOOPBACK)) {
1699 struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL);
1700
1701 if (skb2) {
1702 loopback = 1;
1703 SOCK_DEBUG(sk, "SK %p: send out(copy).\n", sk);
1704 /*
1705 * If it fails it is queued/sent above in the aarp queue
1706 */
1707 aarp_send_ddp(dev, skb2, &usat->sat_addr, NULL);
1708 }
1709 }
1710
1711 if (dev->flags & IFF_LOOPBACK || loopback) {
1712 SOCK_DEBUG(sk, "SK %p: Loop back.\n", sk);
1713 /* loop back */
1714 skb_orphan(skb);
1715 if (ddp->deh_dnode == ATADDR_BCAST) {
1716 if (!rt_lo) {
1717 kfree_skb(skb);
1718 err = -ENETUNREACH;
1719 goto out;
1720 }
1721 dev = rt_lo->dev;
1722 skb->dev = dev;
1723 }
1724 ddp_dl->request(ddp_dl, skb, dev->dev_addr);
1725 } else {
1726 SOCK_DEBUG(sk, "SK %p: send out.\n", sk);
1727 if (rt->flags & RTF_GATEWAY) {
1728 gsat.sat_addr = rt->gateway;
1729 usat = &gsat;
1730 }
1731
1732 /*
1733 * If it fails it is queued/sent above in the aarp queue
1734 */
1735 aarp_send_ddp(dev, skb, &usat->sat_addr, NULL);
1736 }
1737 SOCK_DEBUG(sk, "SK %p: Done write (%Zd).\n", sk, len);
1738
1739 out:
1740 release_sock(sk);
1741 return err ? : len;
1742 }
1743
atalk_recvmsg(struct socket * sock,struct msghdr * msg,size_t size,int flags)1744 static int atalk_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1745 int flags)
1746 {
1747 struct sock *sk = sock->sk;
1748 struct ddpehdr *ddp;
1749 int copied = 0;
1750 int offset = 0;
1751 int err = 0;
1752 struct sk_buff *skb;
1753
1754 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1755 flags & MSG_DONTWAIT, &err);
1756 lock_sock(sk);
1757
1758 if (!skb)
1759 goto out;
1760
1761 /* FIXME: use skb->cb to be able to use shared skbs */
1762 ddp = ddp_hdr(skb);
1763 copied = ntohs(ddp->deh_len_hops) & 1023;
1764
1765 if (sk->sk_type != SOCK_RAW) {
1766 offset = sizeof(*ddp);
1767 copied -= offset;
1768 }
1769
1770 if (copied > size) {
1771 copied = size;
1772 msg->msg_flags |= MSG_TRUNC;
1773 }
1774 err = skb_copy_datagram_msg(skb, offset, msg, copied);
1775
1776 if (!err && msg->msg_name) {
1777 DECLARE_SOCKADDR(struct sockaddr_at *, sat, msg->msg_name);
1778 sat->sat_family = AF_APPLETALK;
1779 sat->sat_port = ddp->deh_sport;
1780 sat->sat_addr.s_node = ddp->deh_snode;
1781 sat->sat_addr.s_net = ddp->deh_snet;
1782 msg->msg_namelen = sizeof(*sat);
1783 }
1784
1785 skb_free_datagram(sk, skb); /* Free the datagram. */
1786
1787 out:
1788 release_sock(sk);
1789 return err ? : copied;
1790 }
1791
1792
1793 /*
1794 * AppleTalk ioctl calls.
1795 */
atalk_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)1796 static int atalk_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1797 {
1798 int rc = -ENOIOCTLCMD;
1799 struct sock *sk = sock->sk;
1800 void __user *argp = (void __user *)arg;
1801
1802 switch (cmd) {
1803 /* Protocol layer */
1804 case TIOCOUTQ: {
1805 long amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1806
1807 if (amount < 0)
1808 amount = 0;
1809 rc = put_user(amount, (int __user *)argp);
1810 break;
1811 }
1812 case TIOCINQ: {
1813 /*
1814 * These two are safe on a single CPU system as only
1815 * user tasks fiddle here
1816 */
1817 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
1818 long amount = 0;
1819
1820 if (skb)
1821 amount = skb->len - sizeof(struct ddpehdr);
1822 rc = put_user(amount, (int __user *)argp);
1823 break;
1824 }
1825 case SIOCGSTAMP:
1826 rc = sock_get_timestamp(sk, argp);
1827 break;
1828 case SIOCGSTAMPNS:
1829 rc = sock_get_timestampns(sk, argp);
1830 break;
1831 /* Routing */
1832 case SIOCADDRT:
1833 case SIOCDELRT:
1834 rc = -EPERM;
1835 if (capable(CAP_NET_ADMIN))
1836 rc = atrtr_ioctl(cmd, argp);
1837 break;
1838 /* Interface */
1839 case SIOCGIFADDR:
1840 case SIOCSIFADDR:
1841 case SIOCGIFBRDADDR:
1842 case SIOCATALKDIFADDR:
1843 case SIOCDIFADDR:
1844 case SIOCSARP: /* proxy AARP */
1845 case SIOCDARP: /* proxy AARP */
1846 rtnl_lock();
1847 rc = atif_ioctl(cmd, argp);
1848 rtnl_unlock();
1849 break;
1850 }
1851
1852 return rc;
1853 }
1854
1855
1856 #ifdef CONFIG_COMPAT
atalk_compat_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)1857 static int atalk_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1858 {
1859 /*
1860 * SIOCATALKDIFADDR is a SIOCPROTOPRIVATE ioctl number, so we
1861 * cannot handle it in common code. The data we access if ifreq
1862 * here is compatible, so we can simply call the native
1863 * handler.
1864 */
1865 if (cmd == SIOCATALKDIFADDR)
1866 return atalk_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
1867
1868 return -ENOIOCTLCMD;
1869 }
1870 #endif
1871
1872
1873 static const struct net_proto_family atalk_family_ops = {
1874 .family = PF_APPLETALK,
1875 .create = atalk_create,
1876 .owner = THIS_MODULE,
1877 };
1878
1879 static const struct proto_ops atalk_dgram_ops = {
1880 .family = PF_APPLETALK,
1881 .owner = THIS_MODULE,
1882 .release = atalk_release,
1883 .bind = atalk_bind,
1884 .connect = atalk_connect,
1885 .socketpair = sock_no_socketpair,
1886 .accept = sock_no_accept,
1887 .getname = atalk_getname,
1888 .poll = datagram_poll,
1889 .ioctl = atalk_ioctl,
1890 #ifdef CONFIG_COMPAT
1891 .compat_ioctl = atalk_compat_ioctl,
1892 #endif
1893 .listen = sock_no_listen,
1894 .shutdown = sock_no_shutdown,
1895 .setsockopt = sock_no_setsockopt,
1896 .getsockopt = sock_no_getsockopt,
1897 .sendmsg = atalk_sendmsg,
1898 .recvmsg = atalk_recvmsg,
1899 .mmap = sock_no_mmap,
1900 .sendpage = sock_no_sendpage,
1901 };
1902
1903 static struct notifier_block ddp_notifier = {
1904 .notifier_call = ddp_device_event,
1905 };
1906
1907 static struct packet_type ltalk_packet_type __read_mostly = {
1908 .type = cpu_to_be16(ETH_P_LOCALTALK),
1909 .func = ltalk_rcv,
1910 };
1911
1912 static struct packet_type ppptalk_packet_type __read_mostly = {
1913 .type = cpu_to_be16(ETH_P_PPPTALK),
1914 .func = atalk_rcv,
1915 };
1916
1917 static unsigned char ddp_snap_id[] = { 0x08, 0x00, 0x07, 0x80, 0x9B };
1918
1919 /* Export symbols for use by drivers when AppleTalk is a module */
1920 EXPORT_SYMBOL(atrtr_get_dev);
1921 EXPORT_SYMBOL(atalk_find_dev_addr);
1922
1923 /* Called by proto.c on kernel start up */
atalk_init(void)1924 static int __init atalk_init(void)
1925 {
1926 int rc;
1927
1928 rc = proto_register(&ddp_proto, 0);
1929 if (rc)
1930 goto out;
1931
1932 rc = sock_register(&atalk_family_ops);
1933 if (rc)
1934 goto out_proto;
1935
1936 ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv);
1937 if (!ddp_dl) {
1938 pr_crit("Unable to register DDP with SNAP.\n");
1939 rc = -ENOMEM;
1940 goto out_sock;
1941 }
1942
1943 dev_add_pack(<alk_packet_type);
1944 dev_add_pack(&ppptalk_packet_type);
1945
1946 rc = register_netdevice_notifier(&ddp_notifier);
1947 if (rc)
1948 goto out_snap;
1949
1950 rc = aarp_proto_init();
1951 if (rc)
1952 goto out_dev;
1953
1954 rc = atalk_proc_init();
1955 if (rc)
1956 goto out_aarp;
1957
1958 rc = atalk_register_sysctl();
1959 if (rc)
1960 goto out_proc;
1961 out:
1962 return rc;
1963 out_proc:
1964 atalk_proc_exit();
1965 out_aarp:
1966 aarp_cleanup_module();
1967 out_dev:
1968 unregister_netdevice_notifier(&ddp_notifier);
1969 out_snap:
1970 dev_remove_pack(&ppptalk_packet_type);
1971 dev_remove_pack(<alk_packet_type);
1972 unregister_snap_client(ddp_dl);
1973 out_sock:
1974 sock_unregister(PF_APPLETALK);
1975 out_proto:
1976 proto_unregister(&ddp_proto);
1977 goto out;
1978 }
1979 module_init(atalk_init);
1980
1981 /*
1982 * No explicit module reference count manipulation is needed in the
1983 * protocol. Socket layer sets module reference count for us
1984 * and interfaces reference counting is done
1985 * by the network device layer.
1986 *
1987 * Ergo, before the AppleTalk module can be removed, all AppleTalk
1988 * sockets be closed from user space.
1989 */
atalk_exit(void)1990 static void __exit atalk_exit(void)
1991 {
1992 #ifdef CONFIG_SYSCTL
1993 atalk_unregister_sysctl();
1994 #endif /* CONFIG_SYSCTL */
1995 atalk_proc_exit();
1996 aarp_cleanup_module(); /* General aarp clean-up. */
1997 unregister_netdevice_notifier(&ddp_notifier);
1998 dev_remove_pack(<alk_packet_type);
1999 dev_remove_pack(&ppptalk_packet_type);
2000 unregister_snap_client(ddp_dl);
2001 sock_unregister(PF_APPLETALK);
2002 proto_unregister(&ddp_proto);
2003 }
2004 module_exit(atalk_exit);
2005
2006 MODULE_LICENSE("GPL");
2007 MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
2008 MODULE_DESCRIPTION("AppleTalk 0.20\n");
2009 MODULE_ALIAS_NETPROTO(PF_APPLETALK);
2010