]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/appletalk/ddp.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / net / appletalk / ddp.c
CommitLineData
1da177e4
LT
1/*
2 * DDP: An implementation of the AppleTalk DDP protocol for
3 * Ethernet 'ELAP'.
4 *
113aa838 5 * Alan Cox <alan@lxorguk.ukuu.org.uk>
1da177e4
LT
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)
ed4477b9 31 * Jay Schulist : Made work as module with
1da177e4
LT
32 * AppleTalk drivers, cleaned it.
33 * Rob Newberry : Added proxy AARP and AARP
34 * procfs, moved probing to AARP
35 * module.
ed4477b9
YH
36 * Adrian Sun/
37 * Michael Zuelsdorff : fix for net.0 packets. don't
1da177e4 38 * allow illegal ether/tokentalk
ed4477b9
YH
39 * port assignment. we lose a
40 * valid localtalk port as a
1da177e4
LT
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.
ed4477b9 51 *
1da177e4
LT
52 */
53
4fc268d2 54#include <linux/capability.h>
1da177e4 55#include <linux/module.h>
1da177e4
LT
56#include <linux/if_arp.h>
57#include <linux/termios.h> /* For TIOCOUTQ/INQ */
415ce61a 58#include <linux/compat.h>
5a0e3ad6 59#include <linux/slab.h>
1da177e4
LT
60#include <net/datalink.h>
61#include <net/psnap.h>
62#include <net/sock.h>
c752f073 63#include <net/tcp_states.h>
1da177e4
LT
64#include <net/route.h>
65#include <linux/atalk.h>
51c56b00 66#include <linux/highmem.h>
1da177e4
LT
67
68struct datalink_proto *ddp_dl, *aarp_dl;
90ddc4f0 69static const struct proto_ops atalk_dgram_ops;
1da177e4
LT
70
71/**************************************************************************\
72* *
73* Handlers for the socket list. *
74* *
75\**************************************************************************/
76
77HLIST_HEAD(atalk_sockets);
78DEFINE_RWLOCK(atalk_sockets_lock);
79
80static inline void __atalk_insert_socket(struct sock *sk)
81{
82 sk_add_node(sk, &atalk_sockets);
83}
84
85static inline void atalk_remove_socket(struct sock *sk)
86{
87 write_lock_bh(&atalk_sockets_lock);
88 sk_del_node_init(sk);
89 write_unlock_bh(&atalk_sockets_lock);
90}
91
92static struct sock *atalk_search_socket(struct sockaddr_at *to,
93 struct atalk_iface *atif)
94{
95 struct sock *s;
1da177e4
LT
96
97 read_lock_bh(&atalk_sockets_lock);
b67bfe0d 98 sk_for_each(s, &atalk_sockets) {
1da177e4
LT
99 struct atalk_sock *at = at_sk(s);
100
101 if (to->sat_port != at->src_port)
102 continue;
103
ed4477b9 104 if (to->sat_addr.s_net == ATADDR_ANYNET &&
64233bff 105 to->sat_addr.s_node == ATADDR_BCAST)
1da177e4
LT
106 goto found;
107
ed4477b9 108 if (to->sat_addr.s_net == at->src_net &&
1da177e4
LT
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
ed4477b9 114 /* XXXX.0 -- we got a request for this router. make sure
1da177e4
LT
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;
124found:
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
2c53040f
BH
131 * @sk: socket to insert in the list if it is not there already
132 * @sat: address to search for
1da177e4
LT
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 */
139static struct sock *atalk_find_or_insert_socket(struct sock *sk,
140 struct sockaddr_at *sat)
141{
142 struct sock *s;
1da177e4
LT
143 struct atalk_sock *at;
144
145 write_lock_bh(&atalk_sockets_lock);
b67bfe0d 146 sk_for_each(s, &atalk_sockets) {
1da177e4
LT
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. */
156found:
157 write_unlock_bh(&atalk_sockets_lock);
158 return s;
159}
160
e99e88a9 161static void atalk_destroy_timer(struct timer_list *t)
1da177e4 162{
e99e88a9 163 struct sock *sk = from_timer(sk, t, sk_timer);
1da177e4 164
c564039f 165 if (sk_has_allocations(sk)) {
1da177e4
LT
166 sk->sk_timer.expires = jiffies + SOCK_DESTROY_TIME;
167 add_timer(&sk->sk_timer);
168 } else
169 sock_put(sk);
170}
171
172static inline void atalk_destroy_socket(struct sock *sk)
173{
174 atalk_remove_socket(sk);
175 skb_queue_purge(&sk->sk_receive_queue);
176
c564039f 177 if (sk_has_allocations(sk)) {
e99e88a9 178 timer_setup(&sk->sk_timer, atalk_destroy_timer, 0);
1da177e4 179 sk->sk_timer.expires = jiffies + SOCK_DESTROY_TIME;
1da177e4
LT
180 add_timer(&sk->sk_timer);
181 } else
182 sock_put(sk);
183}
184
185/**************************************************************************\
186* *
187* Routing tables for the AppleTalk socket layer. *
188* *
189\**************************************************************************/
190
191/* Anti-deadlock ordering is atalk_routes_lock --> iface_lock -DaveM */
192struct atalk_route *atalk_routes;
193DEFINE_RWLOCK(atalk_routes_lock);
194
195struct atalk_iface *atalk_interfaces;
196DEFINE_RWLOCK(atalk_interfaces_lock);
197
198/* For probing devices or in a routerless network */
199struct atalk_route atrtr_default;
200
201/* AppleTalk interface control */
202/*
203 * Drop a device. Doesn't drop any of its routes - that is the caller's
204 * problem. Called when we down the interface or delete the address.
205 */
206static void atif_drop_device(struct net_device *dev)
207{
208 struct atalk_iface **iface = &atalk_interfaces;
209 struct atalk_iface *tmp;
210
211 write_lock_bh(&atalk_interfaces_lock);
212 while ((tmp = *iface) != NULL) {
213 if (tmp->dev == dev) {
214 *iface = tmp->next;
215 dev_put(dev);
216 kfree(tmp);
217 dev->atalk_ptr = NULL;
218 } else
219 iface = &tmp->next;
220 }
221 write_unlock_bh(&atalk_interfaces_lock);
222}
223
224static struct atalk_iface *atif_add_device(struct net_device *dev,
225 struct atalk_addr *sa)
226{
0da974f4 227 struct atalk_iface *iface = kzalloc(sizeof(*iface), GFP_KERNEL);
1da177e4
LT
228
229 if (!iface)
230 goto out;
231
1da177e4
LT
232 dev_hold(dev);
233 iface->dev = dev;
234 dev->atalk_ptr = iface;
235 iface->address = *sa;
236 iface->status = 0;
237
238 write_lock_bh(&atalk_interfaces_lock);
239 iface->next = atalk_interfaces;
240 atalk_interfaces = iface;
241 write_unlock_bh(&atalk_interfaces_lock);
242out:
243 return iface;
244}
245
246/* Perform phase 2 AARP probing on our tentative address */
247static int atif_probe_device(struct atalk_iface *atif)
248{
249 int netrange = ntohs(atif->nets.nr_lastnet) -
250 ntohs(atif->nets.nr_firstnet) + 1;
251 int probe_net = ntohs(atif->address.s_net);
252 int probe_node = atif->address.s_node;
253 int netct, nodect;
254
255 /* Offset the network we start probing with */
256 if (probe_net == ATADDR_ANYNET) {
257 probe_net = ntohs(atif->nets.nr_firstnet);
258 if (netrange)
259 probe_net += jiffies % netrange;
260 }
261 if (probe_node == ATADDR_ANYNODE)
262 probe_node = jiffies & 0xFF;
263
264 /* Scan the networks */
265 atif->status |= ATIF_PROBE;
266 for (netct = 0; netct <= netrange; netct++) {
267 /* Sweep the available nodes from a given start */
268 atif->address.s_net = htons(probe_net);
269 for (nodect = 0; nodect < 256; nodect++) {
270 atif->address.s_node = (nodect + probe_node) & 0xFF;
271 if (atif->address.s_node > 0 &&
272 atif->address.s_node < 254) {
273 /* Probe a proposed address */
274 aarp_probe_network(atif);
275
276 if (!(atif->status & ATIF_PROBE_FAIL)) {
277 atif->status &= ~ATIF_PROBE;
278 return 0;
279 }
280 }
281 atif->status &= ~ATIF_PROBE_FAIL;
282 }
283 probe_net++;
284 if (probe_net > ntohs(atif->nets.nr_lastnet))
285 probe_net = ntohs(atif->nets.nr_firstnet);
286 }
287 atif->status &= ~ATIF_PROBE;
288
289 return -EADDRINUSE; /* Network is full... */
290}
291
292
293/* Perform AARP probing for a proxy address */
294static int atif_proxy_probe_device(struct atalk_iface *atif,
6f0984a0 295 struct atalk_addr *proxy_addr)
1da177e4
LT
296{
297 int netrange = ntohs(atif->nets.nr_lastnet) -
298 ntohs(atif->nets.nr_firstnet) + 1;
299 /* we probe the interface's network */
300 int probe_net = ntohs(atif->address.s_net);
301 int probe_node = ATADDR_ANYNODE; /* we'll take anything */
302 int netct, nodect;
303
304 /* Offset the network we start probing with */
305 if (probe_net == ATADDR_ANYNET) {
306 probe_net = ntohs(atif->nets.nr_firstnet);
307 if (netrange)
308 probe_net += jiffies % netrange;
309 }
310
311 if (probe_node == ATADDR_ANYNODE)
312 probe_node = jiffies & 0xFF;
ed4477b9 313
1da177e4
LT
314 /* Scan the networks */
315 for (netct = 0; netct <= netrange; netct++) {
316 /* Sweep the available nodes from a given start */
317 proxy_addr->s_net = htons(probe_net);
318 for (nodect = 0; nodect < 256; nodect++) {
319 proxy_addr->s_node = (nodect + probe_node) & 0xFF;
320 if (proxy_addr->s_node > 0 &&
321 proxy_addr->s_node < 254) {
322 /* Tell AARP to probe a proposed address */
323 int ret = aarp_proxy_probe_network(atif,
324 proxy_addr);
325
326 if (ret != -EADDRINUSE)
327 return ret;
328 }
329 }
330 probe_net++;
331 if (probe_net > ntohs(atif->nets.nr_lastnet))
332 probe_net = ntohs(atif->nets.nr_firstnet);
333 }
334
335 return -EADDRINUSE; /* Network is full... */
336}
337
338
339struct atalk_addr *atalk_find_dev_addr(struct net_device *dev)
340{
341 struct atalk_iface *iface = dev->atalk_ptr;
342 return iface ? &iface->address : NULL;
343}
344
345static struct atalk_addr *atalk_find_primary(void)
346{
347 struct atalk_iface *fiface = NULL;
348 struct atalk_addr *retval;
349 struct atalk_iface *iface;
350
351 /*
352 * Return a point-to-point interface only if
353 * there is no non-ptp interface available.
354 */
355 read_lock_bh(&atalk_interfaces_lock);
356 for (iface = atalk_interfaces; iface; iface = iface->next) {
357 if (!fiface && !(iface->dev->flags & IFF_LOOPBACK))
358 fiface = iface;
359 if (!(iface->dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
360 retval = &iface->address;
361 goto out;
362 }
363 }
364
365 if (fiface)
366 retval = &fiface->address;
367 else if (atalk_interfaces)
368 retval = &atalk_interfaces->address;
369 else
370 retval = NULL;
371out:
372 read_unlock_bh(&atalk_interfaces_lock);
373 return retval;
374}
375
376/*
377 * Find a match for 'any network' - ie any of our interfaces with that
378 * node number will do just nicely.
379 */
380static struct atalk_iface *atalk_find_anynet(int node, struct net_device *dev)
381{
382 struct atalk_iface *iface = dev->atalk_ptr;
383
384 if (!iface || iface->status & ATIF_PROBE)
385 goto out_err;
386
387 if (node != ATADDR_BCAST &&
388 iface->address.s_node != node &&
389 node != ATADDR_ANYNODE)
390 goto out_err;
391out:
392 return iface;
393out_err:
394 iface = NULL;
395 goto out;
396}
397
398/* Find a match for a specific network:node pair */
f6e276ee 399static struct atalk_iface *atalk_find_interface(__be16 net, int node)
1da177e4
LT
400{
401 struct atalk_iface *iface;
402
403 read_lock_bh(&atalk_interfaces_lock);
404 for (iface = atalk_interfaces; iface; iface = iface->next) {
405 if ((node == ATADDR_BCAST ||
406 node == ATADDR_ANYNODE ||
407 iface->address.s_node == node) &&
408 iface->address.s_net == net &&
409 !(iface->status & ATIF_PROBE))
410 break;
411
412 /* XXXX.0 -- net.0 returns the iface associated with net */
413 if (node == ATADDR_ANYNODE && net != ATADDR_ANYNET &&
414 ntohs(iface->nets.nr_firstnet) <= ntohs(net) &&
415 ntohs(net) <= ntohs(iface->nets.nr_lastnet))
ed4477b9 416 break;
1da177e4
LT
417 }
418 read_unlock_bh(&atalk_interfaces_lock);
419 return iface;
420}
421
422
423/*
424 * Find a route for an AppleTalk packet. This ought to get cached in
425 * the socket (later on...). We know about host routes and the fact
426 * that a route must be direct to broadcast.
427 */
428static struct atalk_route *atrtr_find(struct atalk_addr *target)
429{
430 /*
ed4477b9 431 * we must search through all routes unless we find a
1da177e4
LT
432 * host route, because some host routes might overlap
433 * network routes
434 */
435 struct atalk_route *net_route = NULL;
436 struct atalk_route *r;
ed4477b9 437
1da177e4
LT
438 read_lock_bh(&atalk_routes_lock);
439 for (r = atalk_routes; r; r = r->next) {
440 if (!(r->flags & RTF_UP))
441 continue;
442
443 if (r->target.s_net == target->s_net) {
444 if (r->flags & RTF_HOST) {
445 /*
446 * if this host route is for the target,
447 * the we're done
448 */
449 if (r->target.s_node == target->s_node)
450 goto out;
451 } else
452 /*
453 * this route will work if there isn't a
454 * direct host route, so cache it
455 */
456 net_route = r;
457 }
458 }
ed4477b9
YH
459
460 /*
1da177e4
LT
461 * if we found a network route but not a direct host
462 * route, then return it
463 */
464 if (net_route)
465 r = net_route;
466 else if (atrtr_default.dev)
467 r = &atrtr_default;
468 else /* No route can be found */
469 r = NULL;
470out:
471 read_unlock_bh(&atalk_routes_lock);
472 return r;
473}
474
475
476/*
477 * Given an AppleTalk network, find the device to use. This can be
478 * a simple lookup.
479 */
480struct net_device *atrtr_get_dev(struct atalk_addr *sa)
481{
482 struct atalk_route *atr = atrtr_find(sa);
483 return atr ? atr->dev : NULL;
484}
485
486/* Set up a default router */
487static void atrtr_set_default(struct net_device *dev)
488{
489 atrtr_default.dev = dev;
490 atrtr_default.flags = RTF_UP;
491 atrtr_default.gateway.s_net = htons(0);
492 atrtr_default.gateway.s_node = 0;
493}
494
495/*
496 * Add a router. Basically make sure it looks valid and stuff the
497 * entry in the list. While it uses netranges we always set them to one
498 * entry to work like netatalk.
499 */
500static int atrtr_create(struct rtentry *r, struct net_device *devhint)
501{
502 struct sockaddr_at *ta = (struct sockaddr_at *)&r->rt_dst;
503 struct sockaddr_at *ga = (struct sockaddr_at *)&r->rt_gateway;
504 struct atalk_route *rt;
505 struct atalk_iface *iface, *riface;
506 int retval = -EINVAL;
507
508 /*
509 * Fixme: Raise/Lower a routing change semaphore for these
510 * operations.
511 */
512
513 /* Validate the request */
514 if (ta->sat_family != AF_APPLETALK ||
515 (!devhint && ga->sat_family != AF_APPLETALK))
516 goto out;
517
518 /* Now walk the routing table and make our decisions */
519 write_lock_bh(&atalk_routes_lock);
520 for (rt = atalk_routes; rt; rt = rt->next) {
521 if (r->rt_flags != rt->flags)
522 continue;
523
524 if (ta->sat_addr.s_net == rt->target.s_net) {
525 if (!(rt->flags & RTF_HOST))
526 break;
527 if (ta->sat_addr.s_node == rt->target.s_node)
528 break;
529 }
530 }
531
532 if (!devhint) {
533 riface = NULL;
534
535 read_lock_bh(&atalk_interfaces_lock);
536 for (iface = atalk_interfaces; iface; iface = iface->next) {
537 if (!riface &&
538 ntohs(ga->sat_addr.s_net) >=
ed4477b9 539 ntohs(iface->nets.nr_firstnet) &&
1da177e4 540 ntohs(ga->sat_addr.s_net) <=
ed4477b9 541 ntohs(iface->nets.nr_lastnet))
1da177e4
LT
542 riface = iface;
543
544 if (ga->sat_addr.s_net == iface->address.s_net &&
545 ga->sat_addr.s_node == iface->address.s_node)
546 riface = iface;
ed4477b9 547 }
1da177e4
LT
548 read_unlock_bh(&atalk_interfaces_lock);
549
550 retval = -ENETUNREACH;
551 if (!riface)
552 goto out_unlock;
553
554 devhint = riface->dev;
555 }
556
557 if (!rt) {
0da974f4 558 rt = kzalloc(sizeof(*rt), GFP_ATOMIC);
1da177e4
LT
559
560 retval = -ENOBUFS;
561 if (!rt)
562 goto out_unlock;
1da177e4
LT
563
564 rt->next = atalk_routes;
565 atalk_routes = rt;
566 }
567
568 /* Fill in the routing entry */
569 rt->target = ta->sat_addr;
c7f905f0 570 dev_hold(devhint);
1da177e4
LT
571 rt->dev = devhint;
572 rt->flags = r->rt_flags;
573 rt->gateway = ga->sat_addr;
574
575 retval = 0;
576out_unlock:
577 write_unlock_bh(&atalk_routes_lock);
578out:
579 return retval;
580}
581
582/* Delete a route. Find it and discard it */
6f0984a0 583static int atrtr_delete(struct atalk_addr *addr)
1da177e4
LT
584{
585 struct atalk_route **r = &atalk_routes;
586 int retval = 0;
587 struct atalk_route *tmp;
588
589 write_lock_bh(&atalk_routes_lock);
590 while ((tmp = *r) != NULL) {
591 if (tmp->target.s_net == addr->s_net &&
592 (!(tmp->flags&RTF_GATEWAY) ||
593 tmp->target.s_node == addr->s_node)) {
594 *r = tmp->next;
595 dev_put(tmp->dev);
596 kfree(tmp);
597 goto out;
598 }
599 r = &tmp->next;
600 }
601 retval = -ENOENT;
602out:
603 write_unlock_bh(&atalk_routes_lock);
604 return retval;
605}
606
607/*
608 * Called when a device is downed. Just throw away any routes
609 * via it.
610 */
611static void atrtr_device_down(struct net_device *dev)
612{
613 struct atalk_route **r = &atalk_routes;
614 struct atalk_route *tmp;
615
616 write_lock_bh(&atalk_routes_lock);
617 while ((tmp = *r) != NULL) {
618 if (tmp->dev == dev) {
619 *r = tmp->next;
620 dev_put(dev);
621 kfree(tmp);
622 } else
623 r = &tmp->next;
624 }
625 write_unlock_bh(&atalk_routes_lock);
626
627 if (atrtr_default.dev == dev)
628 atrtr_set_default(NULL);
629}
630
631/* Actually down the interface */
632static inline void atalk_dev_down(struct net_device *dev)
633{
634 atrtr_device_down(dev); /* Remove all routes for the device */
635 aarp_device_down(dev); /* Remove AARP entries for the device */
636 atif_drop_device(dev); /* Remove the device */
637}
638
639/*
640 * A device event has occurred. Watch for devices going down and
641 * delete our use of them (iface and route).
642 */
643static int ddp_device_event(struct notifier_block *this, unsigned long event,
644 void *ptr)
645{
351638e7 646 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
890d52d3 647
721499e8 648 if (!net_eq(dev_net(dev), &init_net))
e9dc8653
EB
649 return NOTIFY_DONE;
650
1da177e4
LT
651 if (event == NETDEV_DOWN)
652 /* Discard any use of this */
890d52d3 653 atalk_dev_down(dev);
1da177e4
LT
654
655 return NOTIFY_DONE;
656}
657
658/* ioctl calls. Shouldn't even need touching */
659/* Device configuration ioctl calls */
660static int atif_ioctl(int cmd, void __user *arg)
661{
662 static char aarp_mcast[6] = { 0x09, 0x00, 0x00, 0xFF, 0xFF, 0xFF };
663 struct ifreq atreq;
664 struct atalk_netrange *nr;
665 struct sockaddr_at *sa;
666 struct net_device *dev;
667 struct atalk_iface *atif;
668 int ct;
669 int limit;
670 struct rtentry rtdef;
671 int add_route;
672
673 if (copy_from_user(&atreq, arg, sizeof(atreq)))
674 return -EFAULT;
675
881d966b 676 dev = __dev_get_by_name(&init_net, atreq.ifr_name);
1da177e4
LT
677 if (!dev)
678 return -ENODEV;
679
680 sa = (struct sockaddr_at *)&atreq.ifr_addr;
681 atif = atalk_find_dev(dev);
682
683 switch (cmd) {
4a9e4b09
JP
684 case SIOCSIFADDR:
685 if (!capable(CAP_NET_ADMIN))
686 return -EPERM;
687 if (sa->sat_family != AF_APPLETALK)
688 return -EINVAL;
689 if (dev->type != ARPHRD_ETHER &&
690 dev->type != ARPHRD_LOOPBACK &&
691 dev->type != ARPHRD_LOCALTLK &&
692 dev->type != ARPHRD_PPP)
693 return -EPROTONOSUPPORT;
694
695 nr = (struct atalk_netrange *)&sa->sat_zero[0];
696 add_route = 1;
697
698 /*
699 * if this is a point-to-point iface, and we already
700 * have an iface for this AppleTalk address, then we
701 * should not add a route
702 */
703 if ((dev->flags & IFF_POINTOPOINT) &&
704 atalk_find_interface(sa->sat_addr.s_net,
705 sa->sat_addr.s_node)) {
706 printk(KERN_DEBUG "AppleTalk: point-to-point "
707 "interface added with "
708 "existing address\n");
709 add_route = 0;
710 }
1da177e4 711
4a9e4b09
JP
712 /*
713 * Phase 1 is fine on LocalTalk but we don't do
714 * EtherTalk phase 1. Anyone wanting to add it go ahead.
715 */
716 if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
717 return -EPROTONOSUPPORT;
718 if (sa->sat_addr.s_node == ATADDR_BCAST ||
719 sa->sat_addr.s_node == 254)
720 return -EINVAL;
721 if (atif) {
722 /* Already setting address */
723 if (atif->status & ATIF_PROBE)
724 return -EBUSY;
725
726 atif->address.s_net = sa->sat_addr.s_net;
727 atif->address.s_node = sa->sat_addr.s_node;
728 atrtr_device_down(dev); /* Flush old routes */
729 } else {
730 atif = atif_add_device(dev, &sa->sat_addr);
1da177e4 731 if (!atif)
4a9e4b09
JP
732 return -ENOMEM;
733 }
734 atif->nets = *nr;
735
736 /*
737 * Check if the chosen address is used. If so we
738 * error and atalkd will try another.
739 */
740
741 if (!(dev->flags & IFF_LOOPBACK) &&
742 !(dev->flags & IFF_POINTOPOINT) &&
743 atif_probe_device(atif) < 0) {
744 atif_drop_device(dev);
745 return -EADDRINUSE;
746 }
1da177e4 747
4a9e4b09
JP
748 /* Hey it worked - add the direct routes */
749 sa = (struct sockaddr_at *)&rtdef.rt_gateway;
750 sa->sat_family = AF_APPLETALK;
751 sa->sat_addr.s_net = atif->address.s_net;
752 sa->sat_addr.s_node = atif->address.s_node;
753 sa = (struct sockaddr_at *)&rtdef.rt_dst;
754 rtdef.rt_flags = RTF_UP;
755 sa->sat_family = AF_APPLETALK;
756 sa->sat_addr.s_node = ATADDR_ANYNODE;
757 if (dev->flags & IFF_LOOPBACK ||
758 dev->flags & IFF_POINTOPOINT)
759 rtdef.rt_flags |= RTF_HOST;
760
761 /* Routerless initial state */
762 if (nr->nr_firstnet == htons(0) &&
763 nr->nr_lastnet == htons(0xFFFE)) {
1da177e4 764 sa->sat_addr.s_net = atif->address.s_net;
4a9e4b09
JP
765 atrtr_create(&rtdef, dev);
766 atrtr_set_default(dev);
767 } else {
768 limit = ntohs(nr->nr_lastnet);
769 if (limit - ntohs(nr->nr_firstnet) > 4096) {
770 printk(KERN_WARNING "Too many routes/"
771 "iface.\n");
1da177e4 772 return -EINVAL;
4a9e4b09
JP
773 }
774 if (add_route)
775 for (ct = ntohs(nr->nr_firstnet);
776 ct <= limit; ct++) {
777 sa->sat_addr.s_net = htons(ct);
778 atrtr_create(&rtdef, dev);
779 }
780 }
781 dev_mc_add_global(dev, aarp_mcast);
782 return 0;
783
784 case SIOCGIFADDR:
785 if (!atif)
786 return -EADDRNOTAVAIL;
787
788 sa->sat_family = AF_APPLETALK;
789 sa->sat_addr = atif->address;
790 break;
791
792 case SIOCGIFBRDADDR:
793 if (!atif)
794 return -EADDRNOTAVAIL;
795
796 sa->sat_family = AF_APPLETALK;
797 sa->sat_addr.s_net = atif->address.s_net;
798 sa->sat_addr.s_node = ATADDR_BCAST;
799 break;
800
801 case SIOCATALKDIFADDR:
802 case SIOCDIFADDR:
803 if (!capable(CAP_NET_ADMIN))
804 return -EPERM;
805 if (sa->sat_family != AF_APPLETALK)
806 return -EINVAL;
807 atalk_dev_down(dev);
808 break;
809
810 case SIOCSARP:
811 if (!capable(CAP_NET_ADMIN))
812 return -EPERM;
813 if (sa->sat_family != AF_APPLETALK)
814 return -EINVAL;
815 /*
816 * for now, we only support proxy AARP on ELAP;
817 * we should be able to do it for LocalTalk, too.
818 */
819 if (dev->type != ARPHRD_ETHER)
820 return -EPROTONOSUPPORT;
821
822 /*
823 * atif points to the current interface on this network;
824 * we aren't concerned about its current status (at
825 * least for now), but it has all the settings about
826 * the network we're going to probe. Consequently, it
827 * must exist.
828 */
829 if (!atif)
830 return -EADDRNOTAVAIL;
831
832 nr = (struct atalk_netrange *)&(atif->nets);
833 /*
834 * Phase 1 is fine on Localtalk but we don't do
835 * Ethertalk phase 1. Anyone wanting to add it go ahead.
836 */
837 if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
838 return -EPROTONOSUPPORT;
839
840 if (sa->sat_addr.s_node == ATADDR_BCAST ||
841 sa->sat_addr.s_node == 254)
842 return -EINVAL;
843
844 /*
845 * Check if the chosen address is used. If so we
846 * error and ATCP will try another.
847 */
848 if (atif_proxy_probe_device(atif, &(sa->sat_addr)) < 0)
849 return -EADDRINUSE;
850
851 /*
852 * We now have an address on the local network, and
853 * the AARP code will defend it for us until we take it
854 * down. We don't set up any routes right now, because
855 * ATCP will install them manually via SIOCADDRT.
856 */
857 break;
858
859 case SIOCDARP:
860 if (!capable(CAP_NET_ADMIN))
861 return -EPERM;
862 if (sa->sat_family != AF_APPLETALK)
863 return -EINVAL;
864 if (!atif)
865 return -EADDRNOTAVAIL;
866
867 /* give to aarp module to remove proxy entry */
868 aarp_proxy_remove(atif->dev, &(sa->sat_addr));
869 return 0;
1da177e4
LT
870 }
871
872 return copy_to_user(arg, &atreq, sizeof(atreq)) ? -EFAULT : 0;
873}
874
875/* Routing ioctl() calls */
876static int atrtr_ioctl(unsigned int cmd, void __user *arg)
877{
878 struct rtentry rt;
879
880 if (copy_from_user(&rt, arg, sizeof(rt)))
881 return -EFAULT;
882
883 switch (cmd) {
4a9e4b09
JP
884 case SIOCDELRT:
885 if (rt.rt_dst.sa_family != AF_APPLETALK)
886 return -EINVAL;
887 return atrtr_delete(&((struct sockaddr_at *)
888 &rt.rt_dst)->sat_addr);
889
890 case SIOCADDRT: {
891 struct net_device *dev = NULL;
892 if (rt.rt_dev) {
893 char name[IFNAMSIZ];
894 if (copy_from_user(name, rt.rt_dev, IFNAMSIZ-1))
895 return -EFAULT;
896 name[IFNAMSIZ-1] = '\0';
897 dev = __dev_get_by_name(&init_net, name);
898 if (!dev)
899 return -ENODEV;
1da177e4 900 }
4a9e4b09
JP
901 return atrtr_create(&rt, dev);
902 }
1da177e4
LT
903 }
904 return -EINVAL;
905}
906
907/**************************************************************************\
908* *
909* Handling for system calls applied via the various interfaces to an *
910* AppleTalk socket object. *
911* *
912\**************************************************************************/
913
914/*
915 * Checksum: This is 'optional'. It's quite likely also a good
916 * candidate for assembler hackery 8)
917 */
ed4477b9 918static unsigned long atalk_sum_partial(const unsigned char *data,
1da177e4
LT
919 int len, unsigned long sum)
920{
921 /* This ought to be unwrapped neatly. I'll trust gcc for now */
922 while (len--) {
f7a3a1d8
JP
923 sum += *data++;
924 sum = rol16(sum, 1);
1da177e4
LT
925 }
926 return sum;
927}
928
929/* Checksum skb data -- similar to skb_checksum */
930static unsigned long atalk_sum_skb(const struct sk_buff *skb, int offset,
931 int len, unsigned long sum)
932{
1a028e50 933 int start = skb_headlen(skb);
c32ba3f9 934 struct sk_buff *frag_iter;
1da177e4
LT
935 int i, copy;
936
937 /* checksum stuff in header space */
fd1dc261 938 if ((copy = start - offset) > 0) {
1da177e4
LT
939 if (copy > len)
940 copy = len;
941 sum = atalk_sum_partial(skb->data + offset, copy, sum);
fd1dc261 942 if ((len -= copy) == 0)
1da177e4
LT
943 return sum;
944
945 offset += copy;
946 }
947
948 /* checksum stuff in frags */
949 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1a028e50 950 int end;
9e903e08 951 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
547b792c 952 WARN_ON(start > offset + len);
1a028e50 953
9e903e08 954 end = start + skb_frag_size(frag);
1da177e4
LT
955 if ((copy = end - offset) > 0) {
956 u8 *vaddr;
1da177e4
LT
957
958 if (copy > len)
959 copy = len;
51c56b00 960 vaddr = kmap_atomic(skb_frag_page(frag));
1a028e50
DM
961 sum = atalk_sum_partial(vaddr + frag->page_offset +
962 offset - start, copy, sum);
51c56b00 963 kunmap_atomic(vaddr);
1da177e4
LT
964
965 if (!(len -= copy))
966 return sum;
967 offset += copy;
968 }
1a028e50 969 start = end;
1da177e4
LT
970 }
971
c32ba3f9
DM
972 skb_walk_frags(skb, frag_iter) {
973 int end;
1a028e50 974
c32ba3f9 975 WARN_ON(start > offset + len);
1da177e4 976
c32ba3f9
DM
977 end = start + frag_iter->len;
978 if ((copy = end - offset) > 0) {
979 if (copy > len)
980 copy = len;
981 sum = atalk_sum_skb(frag_iter, offset - start,
982 copy, sum);
983 if ((len -= copy) == 0)
984 return sum;
985 offset += copy;
1da177e4 986 }
c32ba3f9 987 start = end;
1da177e4
LT
988 }
989
990 BUG_ON(len > 0);
991
992 return sum;
993}
994
2a50f28c 995static __be16 atalk_checksum(const struct sk_buff *skb, int len)
1da177e4
LT
996{
997 unsigned long sum;
998
999 /* skip header 4 bytes */
1000 sum = atalk_sum_skb(skb, 4, len-4, 0);
1001
1002 /* Use 0xFFFF for 0. 0 itself means none */
2a50f28c 1003 return sum ? htons((unsigned short)sum) : htons(0xFFFF);
1da177e4
LT
1004}
1005
1006static struct proto ddp_proto = {
1007 .name = "DDP",
1008 .owner = THIS_MODULE,
1009 .obj_size = sizeof(struct atalk_sock),
1010};
1011
1012/*
1013 * Create a socket. Initialise the socket, blank the addresses
1014 * set the state.
1015 */
3f378b68
EP
1016static int atalk_create(struct net *net, struct socket *sock, int protocol,
1017 int kern)
1da177e4
LT
1018{
1019 struct sock *sk;
1020 int rc = -ESOCKTNOSUPPORT;
1021
09ad9bc7 1022 if (!net_eq(net, &init_net))
1b8d7ae4
EB
1023 return -EAFNOSUPPORT;
1024
1da177e4
LT
1025 /*
1026 * We permit SOCK_DGRAM and RAW is an extension. It is trivial to do
ed4477b9 1027 * and gives you the full ELAP frame. Should be handy for CAP 8)
1da177e4
LT
1028 */
1029 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
1030 goto out;
f7c9a930
ON
1031
1032 rc = -EPERM;
1033 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1034 goto out;
1035
1da177e4 1036 rc = -ENOMEM;
11aa9c28 1037 sk = sk_alloc(net, PF_APPLETALK, GFP_KERNEL, &ddp_proto, kern);
1da177e4
LT
1038 if (!sk)
1039 goto out;
1040 rc = 0;
1041 sock->ops = &atalk_dgram_ops;
1042 sock_init_data(sock, sk);
1043
1044 /* Checksums on by default */
1045 sock_set_flag(sk, SOCK_ZAPPED);
1046out:
1047 return rc;
1048}
1049
1050/* Free a socket. No work needed */
1051static int atalk_release(struct socket *sock)
1052{
1053 struct sock *sk = sock->sk;
1054
1055 if (sk) {
c100c8f4
DM
1056 sock_hold(sk);
1057 lock_sock(sk);
1058
1da177e4
LT
1059 sock_orphan(sk);
1060 sock->sk = NULL;
1061 atalk_destroy_socket(sk);
b20e7bbf 1062
c100c8f4
DM
1063 release_sock(sk);
1064 sock_put(sk);
1065 }
1da177e4
LT
1066 return 0;
1067}
1068
1069/**
1070 * atalk_pick_and_bind_port - Pick a source port when one is not given
2c53040f
BH
1071 * @sk: socket to insert into the tables
1072 * @sat: address to search for
1da177e4
LT
1073 *
1074 * Pick a source port when one is not given. If we can find a suitable free
1075 * one, we insert the socket into the tables using it.
1076 *
1077 * This whole operation must be atomic.
1078 */
1079static int atalk_pick_and_bind_port(struct sock *sk, struct sockaddr_at *sat)
1080{
1081 int retval;
1082
1083 write_lock_bh(&atalk_sockets_lock);
1084
1085 for (sat->sat_port = ATPORT_RESERVED;
1086 sat->sat_port < ATPORT_LAST;
1087 sat->sat_port++) {
1088 struct sock *s;
1da177e4 1089
b67bfe0d 1090 sk_for_each(s, &atalk_sockets) {
1da177e4
LT
1091 struct atalk_sock *at = at_sk(s);
1092
1093 if (at->src_net == sat->sat_addr.s_net &&
1094 at->src_node == sat->sat_addr.s_node &&
1095 at->src_port == sat->sat_port)
1096 goto try_next_port;
1097 }
1098
1099 /* Wheee, it's free, assign and insert. */
1100 __atalk_insert_socket(sk);
1101 at_sk(sk)->src_port = sat->sat_port;
1102 retval = 0;
1103 goto out;
1104
1105try_next_port:;
1106 }
1107
1108 retval = -EBUSY;
1109out:
1110 write_unlock_bh(&atalk_sockets_lock);
1111 return retval;
1112}
1113
1114static int atalk_autobind(struct sock *sk)
1115{
1116 struct atalk_sock *at = at_sk(sk);
1117 struct sockaddr_at sat;
1118 struct atalk_addr *ap = atalk_find_primary();
1119 int n = -EADDRNOTAVAIL;
1120
1121 if (!ap || ap->s_net == htons(ATADDR_ANYNET))
1122 goto out;
1123
1124 at->src_net = sat.sat_addr.s_net = ap->s_net;
1125 at->src_node = sat.sat_addr.s_node = ap->s_node;
1126
1127 n = atalk_pick_and_bind_port(sk, &sat);
1128 if (!n)
1129 sock_reset_flag(sk, SOCK_ZAPPED);
1130out:
1131 return n;
1132}
1133
1134/* Set the address 'our end' of the connection */
1135static int atalk_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1136{
1137 struct sockaddr_at *addr = (struct sockaddr_at *)uaddr;
1138 struct sock *sk = sock->sk;
1139 struct atalk_sock *at = at_sk(sk);
ecced8ba 1140 int err;
1da177e4
LT
1141
1142 if (!sock_flag(sk, SOCK_ZAPPED) ||
1143 addr_len != sizeof(struct sockaddr_at))
1144 return -EINVAL;
1145
1146 if (addr->sat_family != AF_APPLETALK)
1147 return -EAFNOSUPPORT;
1148
60d9f461 1149 lock_sock(sk);
1da177e4
LT
1150 if (addr->sat_addr.s_net == htons(ATADDR_ANYNET)) {
1151 struct atalk_addr *ap = atalk_find_primary();
1152
ecced8ba 1153 err = -EADDRNOTAVAIL;
1da177e4 1154 if (!ap)
ecced8ba 1155 goto out;
1da177e4
LT
1156
1157 at->src_net = addr->sat_addr.s_net = ap->s_net;
fd1dc261 1158 at->src_node = addr->sat_addr.s_node = ap->s_node;
1da177e4 1159 } else {
ecced8ba 1160 err = -EADDRNOTAVAIL;
1da177e4
LT
1161 if (!atalk_find_interface(addr->sat_addr.s_net,
1162 addr->sat_addr.s_node))
ecced8ba 1163 goto out;
1da177e4
LT
1164
1165 at->src_net = addr->sat_addr.s_net;
1166 at->src_node = addr->sat_addr.s_node;
1167 }
1168
1169 if (addr->sat_port == ATADDR_ANYPORT) {
ecced8ba 1170 err = atalk_pick_and_bind_port(sk, addr);
1da177e4 1171
ecced8ba
AB
1172 if (err < 0)
1173 goto out;
1da177e4
LT
1174 } else {
1175 at->src_port = addr->sat_port;
1176
ecced8ba 1177 err = -EADDRINUSE;
1da177e4 1178 if (atalk_find_or_insert_socket(sk, addr))
ecced8ba 1179 goto out;
1da177e4
LT
1180 }
1181
1182 sock_reset_flag(sk, SOCK_ZAPPED);
ecced8ba
AB
1183 err = 0;
1184out:
60d9f461 1185 release_sock(sk);
ecced8ba 1186 return err;
1da177e4
LT
1187}
1188
1189/* Set the address we talk to */
1190static int atalk_connect(struct socket *sock, struct sockaddr *uaddr,
1191 int addr_len, int flags)
1192{
1193 struct sock *sk = sock->sk;
1194 struct atalk_sock *at = at_sk(sk);
1195 struct sockaddr_at *addr;
ecced8ba 1196 int err;
1da177e4
LT
1197
1198 sk->sk_state = TCP_CLOSE;
1199 sock->state = SS_UNCONNECTED;
1200
1201 if (addr_len != sizeof(*addr))
1202 return -EINVAL;
1203
1204 addr = (struct sockaddr_at *)uaddr;
1205
1206 if (addr->sat_family != AF_APPLETALK)
1207 return -EAFNOSUPPORT;
1208
1209 if (addr->sat_addr.s_node == ATADDR_BCAST &&
1210 !sock_flag(sk, SOCK_BROADCAST)) {
ed4477b9 1211#if 1
278f015e 1212 pr_warn("atalk_connect: %s is broken and did not set SO_BROADCAST.\n",
1da177e4
LT
1213 current->comm);
1214#else
1215 return -EACCES;
ed4477b9 1216#endif
1da177e4
LT
1217 }
1218
60d9f461 1219 lock_sock(sk);
ecced8ba 1220 err = -EBUSY;
1da177e4
LT
1221 if (sock_flag(sk, SOCK_ZAPPED))
1222 if (atalk_autobind(sk) < 0)
ecced8ba 1223 goto out;
1da177e4 1224
ecced8ba 1225 err = -ENETUNREACH;
1da177e4 1226 if (!atrtr_get_dev(&addr->sat_addr))
ecced8ba 1227 goto out;
1da177e4
LT
1228
1229 at->dest_port = addr->sat_port;
1230 at->dest_net = addr->sat_addr.s_net;
1231 at->dest_node = addr->sat_addr.s_node;
1232
1233 sock->state = SS_CONNECTED;
1234 sk->sk_state = TCP_ESTABLISHED;
ecced8ba
AB
1235 err = 0;
1236out:
60d9f461 1237 release_sock(sk);
ecced8ba 1238 return err;
1da177e4
LT
1239}
1240
1241/*
1242 * Find the name of an AppleTalk socket. Just copy the right
1243 * fields into the sockaddr.
1244 */
1245static int atalk_getname(struct socket *sock, struct sockaddr *uaddr,
1246 int *uaddr_len, int peer)
1247{
1248 struct sockaddr_at sat;
1249 struct sock *sk = sock->sk;
1250 struct atalk_sock *at = at_sk(sk);
ecced8ba 1251 int err;
1da177e4 1252
60d9f461 1253 lock_sock(sk);
ecced8ba 1254 err = -ENOBUFS;
1da177e4
LT
1255 if (sock_flag(sk, SOCK_ZAPPED))
1256 if (atalk_autobind(sk) < 0)
ecced8ba 1257 goto out;
1da177e4
LT
1258
1259 *uaddr_len = sizeof(struct sockaddr_at);
fccc9f1f 1260 memset(&sat, 0, sizeof(sat));
1da177e4
LT
1261
1262 if (peer) {
ecced8ba 1263 err = -ENOTCONN;
1da177e4 1264 if (sk->sk_state != TCP_ESTABLISHED)
ecced8ba 1265 goto out;
1da177e4
LT
1266
1267 sat.sat_addr.s_net = at->dest_net;
1268 sat.sat_addr.s_node = at->dest_node;
1269 sat.sat_port = at->dest_port;
1270 } else {
1271 sat.sat_addr.s_net = at->src_net;
1272 sat.sat_addr.s_node = at->src_node;
1273 sat.sat_port = at->src_port;
1274 }
1275
ecced8ba 1276 err = 0;
1da177e4
LT
1277 sat.sat_family = AF_APPLETALK;
1278 memcpy(uaddr, &sat, sizeof(sat));
ecced8ba
AB
1279
1280out:
60d9f461 1281 release_sock(sk);
ecced8ba 1282 return err;
1da177e4
LT
1283}
1284
a73ec314 1285#if IS_ENABLED(CONFIG_IPDDP)
1da177e4
LT
1286static __inline__ int is_ip_over_ddp(struct sk_buff *skb)
1287{
ed4477b9 1288 return skb->data[12] == 22;
1da177e4
LT
1289}
1290
1291static int handle_ip_over_ddp(struct sk_buff *skb)
1292{
881d966b 1293 struct net_device *dev = __dev_get_by_name(&init_net, "ipddp0");
1da177e4
LT
1294 struct net_device_stats *stats;
1295
1296 /* This needs to be able to handle ipddp"N" devices */
ffcfb8db
ACM
1297 if (!dev) {
1298 kfree_skb(skb);
1299 return NET_RX_DROP;
1300 }
1da177e4 1301
ed4477b9
YH
1302 skb->protocol = htons(ETH_P_IP);
1303 skb_pull(skb, 13);
1304 skb->dev = dev;
badff6d0 1305 skb_reset_transport_header(skb);
1da177e4 1306
524ad0a7 1307 stats = netdev_priv(dev);
ed4477b9
YH
1308 stats->rx_packets++;
1309 stats->rx_bytes += skb->len + 13;
ffcfb8db 1310 return netif_rx(skb); /* Send the SKB up to a higher place. */
1da177e4
LT
1311}
1312#else
1313/* make it easy for gcc to optimize this test out, i.e. kill the code */
1314#define is_ip_over_ddp(skb) 0
1315#define handle_ip_over_ddp(skb) 0
1316#endif
1317
ffcfb8db
ACM
1318static int atalk_route_packet(struct sk_buff *skb, struct net_device *dev,
1319 struct ddpehdr *ddp, __u16 len_hops, int origlen)
1da177e4
LT
1320{
1321 struct atalk_route *rt;
1322 struct atalk_addr ta;
1323
1324 /*
1325 * Don't route multicast, etc., packets, or packets sent to "this
ed4477b9 1326 * network"
1da177e4
LT
1327 */
1328 if (skb->pkt_type != PACKET_HOST || !ddp->deh_dnet) {
1329 /*
1330 * FIXME:
1331 *
1332 * Can it ever happen that a packet is from a PPP iface and
1333 * needs to be broadcast onto the default network?
1334 */
1335 if (dev->type == ARPHRD_PPP)
1336 printk(KERN_DEBUG "AppleTalk: didn't forward broadcast "
1337 "packet received from PPP iface\n");
1338 goto free_it;
1339 }
1340
1341 ta.s_net = ddp->deh_dnet;
1342 ta.s_node = ddp->deh_dnode;
1343
1344 /* Route the packet */
1345 rt = atrtr_find(&ta);
2a50f28c
AV
1346 /* increment hops count */
1347 len_hops += 1 << 10;
1348 if (!rt || !(len_hops & (15 << 10)))
1da177e4 1349 goto free_it;
2a50f28c 1350
1da177e4 1351 /* FIXME: use skb->cb to be able to use shared skbs */
1da177e4
LT
1352
1353 /*
1354 * Route goes through another gateway, so set the target to the
1355 * gateway instead.
1356 */
1357
1358 if (rt->flags & RTF_GATEWAY) {
1359 ta.s_net = rt->gateway.s_net;
1360 ta.s_node = rt->gateway.s_node;
1361 }
1362
ed4477b9
YH
1363 /* Fix up skb->len field */
1364 skb_trim(skb, min_t(unsigned int, origlen,
1da177e4 1365 (rt->dev->hard_header_len +
2a50f28c 1366 ddp_dl->header_length + (len_hops & 1023))));
1da177e4 1367
1da177e4 1368 /* FIXME: use skb->cb to be able to use shared skbs */
2a50f28c 1369 ddp->deh_len_hops = htons(len_hops);
1da177e4
LT
1370
1371 /*
1372 * Send the buffer onwards
1373 *
1374 * Now we must always be careful. If it's come from LocalTalk to
1375 * EtherTalk it might not fit
1376 *
1377 * Order matters here: If a packet has to be copied to make a new
1378 * headroom (rare hopefully) then it won't need unsharing.
1379 *
1380 * Note. ddp-> becomes invalid at the realloc.
1381 */
1382 if (skb_headroom(skb) < 22) {
1383 /* 22 bytes - 12 ether, 2 len, 3 802.2 5 snap */
1384 struct sk_buff *nskb = skb_realloc_headroom(skb, 32);
1385 kfree_skb(skb);
1da177e4
LT
1386 skb = nskb;
1387 } else
1388 skb = skb_unshare(skb, GFP_ATOMIC);
ed4477b9 1389
1da177e4
LT
1390 /*
1391 * If the buffer didn't vanish into the lack of space bitbucket we can
1392 * send it.
1393 */
ffcfb8db
ACM
1394 if (skb == NULL)
1395 goto drop;
1396
1397 if (aarp_send_ddp(rt->dev, skb, &ta, NULL) == NET_XMIT_DROP)
1398 return NET_RX_DROP;
8be8057e 1399 return NET_RX_SUCCESS;
1da177e4
LT
1400free_it:
1401 kfree_skb(skb);
ffcfb8db
ACM
1402drop:
1403 return NET_RX_DROP;
1da177e4
LT
1404}
1405
1406/**
1407 * atalk_rcv - Receive a packet (in skb) from device dev
1408 * @skb - packet received
1409 * @dev - network device where the packet comes from
1410 * @pt - packet type
1411 *
1412 * Receive a packet (in skb) from device dev. This has come from the SNAP
b0e380b1
ACM
1413 * decoder, and on entry skb->transport_header is the DDP header, skb->len
1414 * is the DDP header, skb->len is the DDP length. The physical headers
1415 * have been extracted. PPP should probably pass frames marked as for this
1416 * layer. [ie ARPHRD_ETHERTALK]
1da177e4
LT
1417 */
1418static int atalk_rcv(struct sk_buff *skb, struct net_device *dev,
f2ccd8fa 1419 struct packet_type *pt, struct net_device *orig_dev)
1da177e4
LT
1420{
1421 struct ddpehdr *ddp;
1422 struct sock *sock;
1423 struct atalk_iface *atif;
1424 struct sockaddr_at tosat;
ed4477b9 1425 int origlen;
2a50f28c 1426 __u16 len_hops;
1da177e4 1427
721499e8 1428 if (!net_eq(dev_net(dev), &init_net))
6885ffb3 1429 goto drop;
e730c155 1430
1da177e4 1431 /* Don't mangle buffer if shared */
ed4477b9 1432 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
1da177e4 1433 goto out;
ed4477b9 1434
1da177e4
LT
1435 /* Size check and make sure header is contiguous */
1436 if (!pskb_may_pull(skb, sizeof(*ddp)))
6885ffb3 1437 goto drop;
1da177e4
LT
1438
1439 ddp = ddp_hdr(skb);
1440
2a50f28c 1441 len_hops = ntohs(ddp->deh_len_hops);
1da177e4
LT
1442
1443 /* Trim buffer in case of stray trailing data */
1444 origlen = skb->len;
2a50f28c 1445 skb_trim(skb, min_t(unsigned int, skb->len, len_hops & 1023));
1da177e4
LT
1446
1447 /*
1448 * Size check to see if ddp->deh_len was crap
1449 * (Otherwise we'll detonate most spectacularly
75559c16 1450 * in the middle of atalk_checksum() or recvmsg()).
1da177e4 1451 */
75559c16
JD
1452 if (skb->len < sizeof(*ddp) || skb->len < (len_hops & 1023)) {
1453 pr_debug("AppleTalk: dropping corrupted frame (deh_len=%u, "
1454 "skb->len=%u)\n", len_hops & 1023, skb->len);
6885ffb3 1455 goto drop;
75559c16 1456 }
1da177e4
LT
1457
1458 /*
1459 * Any checksums. Note we don't do htons() on this == is assumed to be
1460 * valid for net byte orders all over the networking code...
1461 */
1462 if (ddp->deh_sum &&
2a50f28c 1463 atalk_checksum(skb, len_hops & 1023) != ddp->deh_sum)
1da177e4 1464 /* Not a valid AppleTalk frame - dustbin time */
6885ffb3 1465 goto drop;
1da177e4
LT
1466
1467 /* Check the packet is aimed at us */
1468 if (!ddp->deh_dnet) /* Net 0 is 'this network' */
1469 atif = atalk_find_anynet(ddp->deh_dnode, dev);
1470 else
1471 atif = atalk_find_interface(ddp->deh_dnet, ddp->deh_dnode);
1472
1da177e4 1473 if (!atif) {
64233bff
OD
1474 /* Not ours, so we route the packet via the correct
1475 * AppleTalk iface
1476 */
ffcfb8db 1477 return atalk_route_packet(skb, dev, ddp, len_hops, origlen);
1da177e4
LT
1478 }
1479
1480 /* if IP over DDP is not selected this code will be optimized out */
1481 if (is_ip_over_ddp(skb))
1482 return handle_ip_over_ddp(skb);
1483 /*
1484 * Which socket - atalk_search_socket() looks for a *full match*
1485 * of the <net, node, port> tuple.
1486 */
1487 tosat.sat_addr.s_net = ddp->deh_dnet;
1488 tosat.sat_addr.s_node = ddp->deh_dnode;
1489 tosat.sat_port = ddp->deh_dport;
1490
1491 sock = atalk_search_socket(&tosat, atif);
1492 if (!sock) /* But not one of our sockets */
6885ffb3 1493 goto drop;
1da177e4
LT
1494
1495 /* Queue packet (standard) */
1da177e4 1496 if (sock_queue_rcv_skb(sock, skb) < 0)
6885ffb3
MS
1497 goto drop;
1498
1499 return NET_RX_SUCCESS;
1500
1501drop:
1da177e4 1502 kfree_skb(skb);
6885ffb3
MS
1503out:
1504 return NET_RX_DROP;
1505
1da177e4
LT
1506}
1507
1508/*
1509 * Receive a LocalTalk frame. We make some demands on the caller here.
1510 * Caller must provide enough headroom on the packet to pull the short
1511 * header and append a long one.
1512 */
1513static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev,
f2ccd8fa 1514 struct packet_type *pt, struct net_device *orig_dev)
1da177e4 1515{
721499e8 1516 if (!net_eq(dev_net(dev), &init_net))
e730c155
EB
1517 goto freeit;
1518
1da177e4 1519 /* Expand any short form frames */
98e399f8 1520 if (skb_mac_header(skb)[2] == 1) {
1da177e4
LT
1521 struct ddpehdr *ddp;
1522 /* Find our address */
1523 struct atalk_addr *ap = atalk_find_dev_addr(dev);
1524
2a50f28c 1525 if (!ap || skb->len < sizeof(__be16) || skb->len > 1023)
1da177e4
LT
1526 goto freeit;
1527
1528 /* Don't mangle buffer if shared */
ed4477b9 1529 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
1da177e4
LT
1530 return 0;
1531
1532 /*
1533 * The push leaves us with a ddephdr not an shdr, and
1534 * handily the port bytes in the right place preset.
1535 */
d58ff351 1536 ddp = skb_push(skb, sizeof(*ddp) - 4);
1da177e4
LT
1537
1538 /* Now fill in the long header */
1539
ed4477b9
YH
1540 /*
1541 * These two first. The mac overlays the new source/dest
1542 * network information so we MUST copy these before
1543 * we write the network numbers !
1544 */
1da177e4 1545
98e399f8
ACM
1546 ddp->deh_dnode = skb_mac_header(skb)[0]; /* From physical header */
1547 ddp->deh_snode = skb_mac_header(skb)[1]; /* From physical header */
1da177e4
LT
1548
1549 ddp->deh_dnet = ap->s_net; /* Network number */
1550 ddp->deh_snet = ap->s_net;
1551 ddp->deh_sum = 0; /* No checksum */
1552 /*
1553 * Not sure about this bit...
1554 */
2a50f28c
AV
1555 /* Non routable, so force a drop if we slip up later */
1556 ddp->deh_len_hops = htons(skb->len + (DDP_MAXHOPS << 10));
1da177e4 1557 }
badff6d0 1558 skb_reset_transport_header(skb);
1da177e4 1559
f2ccd8fa 1560 return atalk_rcv(skb, dev, pt, orig_dev);
1da177e4
LT
1561freeit:
1562 kfree_skb(skb);
1563 return 0;
1564}
1565
1b784140 1566static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1da177e4
LT
1567{
1568 struct sock *sk = sock->sk;
1569 struct atalk_sock *at = at_sk(sk);
342dfc30 1570 DECLARE_SOCKADDR(struct sockaddr_at *, usat, msg->msg_name);
1da177e4
LT
1571 int flags = msg->msg_flags;
1572 int loopback = 0;
1573 struct sockaddr_at local_satalk, gsat;
1574 struct sk_buff *skb;
1575 struct net_device *dev;
1576 struct ddpehdr *ddp;
1577 int size;
1578 struct atalk_route *rt;
1579 int err;
1580
1581 if (flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
1582 return -EINVAL;
1583
1584 if (len > DDP_MAXSZ)
1585 return -EMSGSIZE;
1586
60d9f461 1587 lock_sock(sk);
1da177e4 1588 if (usat) {
ecced8ba 1589 err = -EBUSY;
1da177e4
LT
1590 if (sock_flag(sk, SOCK_ZAPPED))
1591 if (atalk_autobind(sk) < 0)
ecced8ba 1592 goto out;
1da177e4 1593
ecced8ba 1594 err = -EINVAL;
1da177e4
LT
1595 if (msg->msg_namelen < sizeof(*usat) ||
1596 usat->sat_family != AF_APPLETALK)
ecced8ba 1597 goto out;
1da177e4 1598
ecced8ba 1599 err = -EPERM;
03ba9991 1600 /* netatalk didn't implement this check */
1da177e4
LT
1601 if (usat->sat_addr.s_node == ATADDR_BCAST &&
1602 !sock_flag(sk, SOCK_BROADCAST)) {
ecced8ba 1603 goto out;
1da177e4
LT
1604 }
1605 } else {
ecced8ba 1606 err = -ENOTCONN;
1da177e4 1607 if (sk->sk_state != TCP_ESTABLISHED)
ecced8ba 1608 goto out;
1da177e4
LT
1609 usat = &local_satalk;
1610 usat->sat_family = AF_APPLETALK;
1611 usat->sat_port = at->dest_port;
1612 usat->sat_addr.s_node = at->dest_node;
1613 usat->sat_addr.s_net = at->dest_net;
1614 }
1615
1616 /* Build a packet */
1617 SOCK_DEBUG(sk, "SK %p: Got address.\n", sk);
1618
1619 /* For headers */
1620 size = sizeof(struct ddpehdr) + len + ddp_dl->header_length;
1621
1622 if (usat->sat_addr.s_net || usat->sat_addr.s_node == ATADDR_ANYNODE) {
1623 rt = atrtr_find(&usat->sat_addr);
1da177e4
LT
1624 } else {
1625 struct atalk_addr at_hint;
1626
1627 at_hint.s_node = 0;
1628 at_hint.s_net = at->src_net;
1629
1630 rt = atrtr_find(&at_hint);
1da177e4 1631 }
48bb230e 1632 err = -ENETUNREACH;
64233bff 1633 if (!rt)
ecced8ba 1634 goto out;
64233bff
OD
1635
1636 dev = rt->dev;
1da177e4
LT
1637
1638 SOCK_DEBUG(sk, "SK %p: Size needed %d, device %s\n",
1639 sk, size, dev->name);
1640
1641 size += dev->hard_header_len;
60d9f461 1642 release_sock(sk);
1da177e4 1643 skb = sock_alloc_send_skb(sk, size, (flags & MSG_DONTWAIT), &err);
60d9f461 1644 lock_sock(sk);
1da177e4 1645 if (!skb)
ecced8ba 1646 goto out;
ed4477b9 1647
1da177e4
LT
1648 skb_reserve(skb, ddp_dl->header_length);
1649 skb_reserve(skb, dev->hard_header_len);
1650 skb->dev = dev;
1651
1652 SOCK_DEBUG(sk, "SK %p: Begin build.\n", sk);
1653
4df864c1 1654 ddp = skb_put(skb, sizeof(struct ddpehdr));
2a50f28c 1655 ddp->deh_len_hops = htons(len + sizeof(*ddp));
1da177e4
LT
1656 ddp->deh_dnet = usat->sat_addr.s_net;
1657 ddp->deh_snet = at->src_net;
1658 ddp->deh_dnode = usat->sat_addr.s_node;
1659 ddp->deh_snode = at->src_node;
1660 ddp->deh_dport = usat->sat_port;
1661 ddp->deh_sport = at->src_port;
1662
5b5e0928 1663 SOCK_DEBUG(sk, "SK %p: Copy user data (%zd bytes).\n", sk, len);
1da177e4 1664
6ce8e9ce 1665 err = memcpy_from_msg(skb_put(skb, len), msg, len);
1da177e4
LT
1666 if (err) {
1667 kfree_skb(skb);
ecced8ba
AB
1668 err = -EFAULT;
1669 goto out;
1da177e4
LT
1670 }
1671
28448b80 1672 if (sk->sk_no_check_tx)
1da177e4
LT
1673 ddp->deh_sum = 0;
1674 else
1675 ddp->deh_sum = atalk_checksum(skb, len + sizeof(*ddp));
1676
1677 /*
1678 * Loopback broadcast packets to non gateway targets (ie routes
1679 * to group we are in)
1680 */
1681 if (ddp->deh_dnode == ATADDR_BCAST &&
1682 !(rt->flags & RTF_GATEWAY) && !(dev->flags & IFF_LOOPBACK)) {
1683 struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL);
1684
1685 if (skb2) {
1686 loopback = 1;
1687 SOCK_DEBUG(sk, "SK %p: send out(copy).\n", sk);
ffcfb8db
ACM
1688 /*
1689 * If it fails it is queued/sent above in the aarp queue
1690 */
1691 aarp_send_ddp(dev, skb2, &usat->sat_addr, NULL);
1da177e4
LT
1692 }
1693 }
1694
1695 if (dev->flags & IFF_LOOPBACK || loopback) {
1696 SOCK_DEBUG(sk, "SK %p: Loop back.\n", sk);
1697 /* loop back */
1698 skb_orphan(skb);
64233bff
OD
1699 if (ddp->deh_dnode == ATADDR_BCAST) {
1700 struct atalk_addr at_lo;
1701
1702 at_lo.s_node = 0;
1703 at_lo.s_net = 0;
1704
1705 rt = atrtr_find(&at_lo);
1706 if (!rt) {
1707 kfree_skb(skb);
ecced8ba
AB
1708 err = -ENETUNREACH;
1709 goto out;
64233bff
OD
1710 }
1711 dev = rt->dev;
1712 skb->dev = dev;
1713 }
1da177e4
LT
1714 ddp_dl->request(ddp_dl, skb, dev->dev_addr);
1715 } else {
1716 SOCK_DEBUG(sk, "SK %p: send out.\n", sk);
1717 if (rt->flags & RTF_GATEWAY) {
1718 gsat.sat_addr = rt->gateway;
1719 usat = &gsat;
1720 }
1721
ffcfb8db
ACM
1722 /*
1723 * If it fails it is queued/sent above in the aarp queue
1724 */
1725 aarp_send_ddp(dev, skb, &usat->sat_addr, NULL);
1da177e4 1726 }
5b5e0928 1727 SOCK_DEBUG(sk, "SK %p: Done write (%zd).\n", sk, len);
1da177e4 1728
ecced8ba 1729out:
60d9f461 1730 release_sock(sk);
ecced8ba 1731 return err ? : len;
1da177e4
LT
1732}
1733
1b784140
YX
1734static int atalk_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1735 int flags)
1da177e4
LT
1736{
1737 struct sock *sk = sock->sk;
1da177e4
LT
1738 struct ddpehdr *ddp;
1739 int copied = 0;
2a50f28c 1740 int offset = 0;
1da177e4 1741 int err = 0;
ecced8ba
AB
1742 struct sk_buff *skb;
1743
ecced8ba 1744 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1da177e4 1745 flags & MSG_DONTWAIT, &err);
60d9f461
AB
1746 lock_sock(sk);
1747
1da177e4 1748 if (!skb)
ecced8ba 1749 goto out;
1da177e4
LT
1750
1751 /* FIXME: use skb->cb to be able to use shared skbs */
1752 ddp = ddp_hdr(skb);
2a50f28c 1753 copied = ntohs(ddp->deh_len_hops) & 1023;
1da177e4 1754
2a50f28c
AV
1755 if (sk->sk_type != SOCK_RAW) {
1756 offset = sizeof(*ddp);
1757 copied -= offset;
1758 }
1da177e4 1759
2a50f28c
AV
1760 if (copied > size) {
1761 copied = size;
1762 msg->msg_flags |= MSG_TRUNC;
1da177e4 1763 }
51f3d02b 1764 err = skb_copy_datagram_msg(skb, offset, msg, copied);
1da177e4 1765
f3d33426 1766 if (!err && msg->msg_name) {
342dfc30 1767 DECLARE_SOCKADDR(struct sockaddr_at *, sat, msg->msg_name);
f3d33426
HFS
1768 sat->sat_family = AF_APPLETALK;
1769 sat->sat_port = ddp->deh_sport;
1770 sat->sat_addr.s_node = ddp->deh_snode;
1771 sat->sat_addr.s_net = ddp->deh_snet;
1772 msg->msg_namelen = sizeof(*sat);
1da177e4
LT
1773 }
1774
1775 skb_free_datagram(sk, skb); /* Free the datagram. */
ecced8ba
AB
1776
1777out:
60d9f461 1778 release_sock(sk);
1da177e4
LT
1779 return err ? : copied;
1780}
1781
1782
1783/*
1784 * AppleTalk ioctl calls.
1785 */
1786static int atalk_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1787{
b5e5fa5e 1788 int rc = -ENOIOCTLCMD;
1da177e4
LT
1789 struct sock *sk = sock->sk;
1790 void __user *argp = (void __user *)arg;
1791
1792 switch (cmd) {
7b30600c 1793 /* Protocol layer */
1794 case TIOCOUTQ: {
1795 long amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1da177e4 1796
7b30600c 1797 if (amount < 0)
1798 amount = 0;
1799 rc = put_user(amount, (int __user *)argp);
1800 break;
1801 }
1802 case TIOCINQ: {
1803 /*
1804 * These two are safe on a single CPU system as only
1805 * user tasks fiddle here
1806 */
1807 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
1808 long amount = 0;
1da177e4 1809
7b30600c 1810 if (skb)
63ae8894 1811 amount = skb->len - sizeof(struct ddpehdr);
7b30600c 1812 rc = put_user(amount, (int __user *)argp);
1813 break;
1814 }
1815 case SIOCGSTAMP:
1816 rc = sock_get_timestamp(sk, argp);
1817 break;
1818 case SIOCGSTAMPNS:
1819 rc = sock_get_timestampns(sk, argp);
1820 break;
1821 /* Routing */
1822 case SIOCADDRT:
1823 case SIOCDELRT:
1824 rc = -EPERM;
1825 if (capable(CAP_NET_ADMIN))
1826 rc = atrtr_ioctl(cmd, argp);
1827 break;
1828 /* Interface */
1829 case SIOCGIFADDR:
1830 case SIOCSIFADDR:
1831 case SIOCGIFBRDADDR:
1832 case SIOCATALKDIFADDR:
1833 case SIOCDIFADDR:
1834 case SIOCSARP: /* proxy AARP */
1835 case SIOCDARP: /* proxy AARP */
1836 rtnl_lock();
1837 rc = atif_ioctl(cmd, argp);
1838 rtnl_unlock();
1839 break;
1da177e4
LT
1840 }
1841
1842 return rc;
1843}
1844
f6c90b71
PV
1845
1846#ifdef CONFIG_COMPAT
1847static int atalk_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1848{
1849 /*
20660221
AB
1850 * SIOCATALKDIFADDR is a SIOCPROTOPRIVATE ioctl number, so we
1851 * cannot handle it in common code. The data we access if ifreq
1852 * here is compatible, so we can simply call the native
1853 * handler.
f6c90b71 1854 */
20660221
AB
1855 if (cmd == SIOCATALKDIFADDR)
1856 return atalk_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
1857
f6c90b71
PV
1858 return -ENOIOCTLCMD;
1859}
1860#endif
1861
1862
ec1b4cf7 1863static const struct net_proto_family atalk_family_ops = {
1da177e4
LT
1864 .family = PF_APPLETALK,
1865 .create = atalk_create,
1866 .owner = THIS_MODULE,
1867};
1868
ecced8ba 1869static const struct proto_ops atalk_dgram_ops = {
1da177e4
LT
1870 .family = PF_APPLETALK,
1871 .owner = THIS_MODULE,
1872 .release = atalk_release,
1873 .bind = atalk_bind,
1874 .connect = atalk_connect,
1875 .socketpair = sock_no_socketpair,
1876 .accept = sock_no_accept,
1877 .getname = atalk_getname,
60d9f461 1878 .poll = datagram_poll,
1da177e4 1879 .ioctl = atalk_ioctl,
f6c90b71
PV
1880#ifdef CONFIG_COMPAT
1881 .compat_ioctl = atalk_compat_ioctl,
1882#endif
1da177e4
LT
1883 .listen = sock_no_listen,
1884 .shutdown = sock_no_shutdown,
1885 .setsockopt = sock_no_setsockopt,
1886 .getsockopt = sock_no_getsockopt,
1887 .sendmsg = atalk_sendmsg,
1888 .recvmsg = atalk_recvmsg,
1889 .mmap = sock_no_mmap,
1890 .sendpage = sock_no_sendpage,
1891};
1892
1da177e4
LT
1893static struct notifier_block ddp_notifier = {
1894 .notifier_call = ddp_device_event,
1895};
1896
7546dd97 1897static struct packet_type ltalk_packet_type __read_mostly = {
09640e63 1898 .type = cpu_to_be16(ETH_P_LOCALTALK),
1da177e4
LT
1899 .func = ltalk_rcv,
1900};
1901
7546dd97 1902static struct packet_type ppptalk_packet_type __read_mostly = {
09640e63 1903 .type = cpu_to_be16(ETH_P_PPPTALK),
1da177e4
LT
1904 .func = atalk_rcv,
1905};
1906
1907static unsigned char ddp_snap_id[] = { 0x08, 0x00, 0x07, 0x80, 0x9B };
1908
1909/* Export symbols for use by drivers when AppleTalk is a module */
1da177e4
LT
1910EXPORT_SYMBOL(atrtr_get_dev);
1911EXPORT_SYMBOL(atalk_find_dev_addr);
1912
1da177e4
LT
1913/* Called by proto.c on kernel start up */
1914static int __init atalk_init(void)
1915{
0979e088 1916 int rc;
1da177e4 1917
0979e088
Y
1918 rc = proto_register(&ddp_proto, 0);
1919 if (rc)
1da177e4
LT
1920 goto out;
1921
0979e088
Y
1922 rc = sock_register(&atalk_family_ops);
1923 if (rc)
1924 goto out_proto;
1925
1da177e4 1926 ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv);
ecf1bef9
Y
1927 if (!ddp_dl) {
1928 pr_crit("Unable to register DDP with SNAP.\n");
4eb4eef7 1929 rc = -ENOMEM;
ecf1bef9
Y
1930 goto out_sock;
1931 }
1da177e4
LT
1932
1933 dev_add_pack(&ltalk_packet_type);
1934 dev_add_pack(&ppptalk_packet_type);
1935
0979e088
Y
1936 rc = register_netdevice_notifier(&ddp_notifier);
1937 if (rc)
ecf1bef9
Y
1938 goto out_snap;
1939
1940 rc = aarp_proto_init();
1941 if (rc)
1942 goto out_dev;
0979e088 1943
0979e088
Y
1944 rc = atalk_proc_init();
1945 if (rc)
1946 goto out_aarp;
1947
1948 rc = atalk_register_sysctl();
1949 if (rc)
1950 goto out_proc;
1da177e4
LT
1951out:
1952 return rc;
0979e088
Y
1953out_proc:
1954 atalk_proc_exit();
1955out_aarp:
1956 aarp_cleanup_module();
ecf1bef9 1957out_dev:
0979e088 1958 unregister_netdevice_notifier(&ddp_notifier);
ecf1bef9 1959out_snap:
0979e088
Y
1960 dev_remove_pack(&ppptalk_packet_type);
1961 dev_remove_pack(&ltalk_packet_type);
1962 unregister_snap_client(ddp_dl);
ecf1bef9 1963out_sock:
0979e088
Y
1964 sock_unregister(PF_APPLETALK);
1965out_proto:
1966 proto_unregister(&ddp_proto);
1967 goto out;
1da177e4
LT
1968}
1969module_init(atalk_init);
1970
1971/*
1972 * No explicit module reference count manipulation is needed in the
1973 * protocol. Socket layer sets module reference count for us
1974 * and interfaces reference counting is done
1975 * by the network device layer.
1976 *
1977 * Ergo, before the AppleTalk module can be removed, all AppleTalk
1978 * sockets be closed from user space.
1979 */
1980static void __exit atalk_exit(void)
1981{
1982#ifdef CONFIG_SYSCTL
1983 atalk_unregister_sysctl();
1984#endif /* CONFIG_SYSCTL */
1985 atalk_proc_exit();
1986 aarp_cleanup_module(); /* General aarp clean-up. */
1987 unregister_netdevice_notifier(&ddp_notifier);
1988 dev_remove_pack(&ltalk_packet_type);
1989 dev_remove_pack(&ppptalk_packet_type);
1990 unregister_snap_client(ddp_dl);
1991 sock_unregister(PF_APPLETALK);
1992 proto_unregister(&ddp_proto);
1993}
1994module_exit(atalk_exit);
1995
1996MODULE_LICENSE("GPL");
113aa838 1997MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
1da177e4
LT
1998MODULE_DESCRIPTION("AppleTalk 0.20\n");
1999MODULE_ALIAS_NETPROTO(PF_APPLETALK);