blob: 4177a74f65436fb4379ebfd20bb277cc280844a3 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * DDP: An implementation of the AppleTalk DDP protocol for
4 * Ethernet 'ELAP'.
5 *
Alan Cox113aa832008-10-13 19:01:08 -07006 * Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * With more than a little assistance from
9 *
10 * Wesley Craig <netatalk@umich.edu>
11 *
12 * Fixes:
13 * Neil Horman : Added missing device ioctls
14 * Michael Callahan : Made routing work
15 * Wesley Craig : Fix probing to listen to a
16 * passed node id.
17 * Alan Cox : Added send/recvmsg support
18 * Alan Cox : Moved at. to protinfo in
19 * socket.
20 * Alan Cox : Added firewall hooks.
21 * Alan Cox : Supports new ARPHRD_LOOPBACK
22 * Christer Weinigel : Routing and /proc fixes.
23 * Bradford Johnson : LocalTalk.
24 * Tom Dyas : Module support.
25 * Alan Cox : Hooks for PPP (based on the
26 * LocalTalk hook).
27 * Alan Cox : Posix bits
28 * Alan Cox/Mike Freeman : Possible fix to NBP problems
29 * Bradford Johnson : IP-over-DDP (experimental)
30 * Jay Schulist : Moved IP-over-DDP to its own
31 * driver file. (ipddp.c & ipddp.h)
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +090032 * Jay Schulist : Made work as module with
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * AppleTalk drivers, cleaned it.
34 * Rob Newberry : Added proxy AARP and AARP
35 * procfs, moved probing to AARP
36 * module.
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +090037 * Adrian Sun/
38 * Michael Zuelsdorff : fix for net.0 packets. don't
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * allow illegal ether/tokentalk
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +090040 * port assignment. we lose a
41 * valid localtalk port as a
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * result.
43 * Arnaldo C. de Melo : Cleanup, in preparation for
44 * shared skb support 8)
45 * Arnaldo C. de Melo : Move proc stuff to atalk_proc.c,
46 * use seq_file
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
48
Randy Dunlap4fc268d2006-01-11 12:17:47 -080049#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/if_arp.h>
52#include <linux/termios.h> /* For TIOCOUTQ/INQ */
Stephen Rothwell415ce612009-11-08 20:41:03 -080053#include <linux/compat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/datalink.h>
56#include <net/psnap.h>
57#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070058#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <net/route.h>
60#include <linux/atalk.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020061#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63struct datalink_proto *ddp_dl, *aarp_dl;
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080064static const struct proto_ops atalk_dgram_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/**************************************************************************\
67* *
68* Handlers for the socket list. *
69* *
70\**************************************************************************/
71
72HLIST_HEAD(atalk_sockets);
73DEFINE_RWLOCK(atalk_sockets_lock);
74
75static inline void __atalk_insert_socket(struct sock *sk)
76{
77 sk_add_node(sk, &atalk_sockets);
78}
79
80static inline void atalk_remove_socket(struct sock *sk)
81{
82 write_lock_bh(&atalk_sockets_lock);
83 sk_del_node_init(sk);
84 write_unlock_bh(&atalk_sockets_lock);
85}
86
87static struct sock *atalk_search_socket(struct sockaddr_at *to,
88 struct atalk_iface *atif)
89{
90 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 read_lock_bh(&atalk_sockets_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -080093 sk_for_each(s, &atalk_sockets) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct atalk_sock *at = at_sk(s);
95
96 if (to->sat_port != at->src_port)
97 continue;
98
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +090099 if (to->sat_addr.s_net == ATADDR_ANYNET &&
Oliver Dawid64233bf2005-09-27 16:11:29 -0700100 to->sat_addr.s_node == ATADDR_BCAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 goto found;
102
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900103 if (to->sat_addr.s_net == at->src_net &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 (to->sat_addr.s_node == at->src_node ||
105 to->sat_addr.s_node == ATADDR_BCAST ||
106 to->sat_addr.s_node == ATADDR_ANYNODE))
107 goto found;
108
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900109 /* XXXX.0 -- we got a request for this router. make sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * that the node is appropriately set. */
111 if (to->sat_addr.s_node == ATADDR_ANYNODE &&
112 to->sat_addr.s_net != ATADDR_ANYNET &&
113 atif->address.s_node == at->src_node) {
114 to->sat_addr.s_node = atif->address.s_node;
115 goto found;
116 }
117 }
118 s = NULL;
119found:
120 read_unlock_bh(&atalk_sockets_lock);
121 return s;
122}
123
124/**
125 * atalk_find_or_insert_socket - Try to find a socket matching ADDR
Ben Hutchings2c530402012-07-10 10:55:09 +0000126 * @sk: socket to insert in the list if it is not there already
127 * @sat: address to search for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 *
129 * Try to find a socket matching ADDR in the socket list, if found then return
130 * it. If not, insert SK into the socket list.
131 *
132 * This entire operation must execute atomically.
133 */
134static struct sock *atalk_find_or_insert_socket(struct sock *sk,
135 struct sockaddr_at *sat)
136{
137 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 struct atalk_sock *at;
139
140 write_lock_bh(&atalk_sockets_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800141 sk_for_each(s, &atalk_sockets) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 at = at_sk(s);
143
144 if (at->src_net == sat->sat_addr.s_net &&
145 at->src_node == sat->sat_addr.s_node &&
146 at->src_port == sat->sat_port)
147 goto found;
148 }
149 s = NULL;
150 __atalk_insert_socket(sk); /* Wheee, it's free, assign and insert. */
151found:
152 write_unlock_bh(&atalk_sockets_lock);
153 return s;
154}
155
Kees Cooke99e88a2017-10-16 14:43:17 -0700156static void atalk_destroy_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Kees Cooke99e88a2017-10-16 14:43:17 -0700158 struct sock *sk = from_timer(sk, t, sk_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Eric Dumazetc5640392009-06-16 10:12:03 +0000160 if (sk_has_allocations(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 sk->sk_timer.expires = jiffies + SOCK_DESTROY_TIME;
162 add_timer(&sk->sk_timer);
163 } else
164 sock_put(sk);
165}
166
167static inline void atalk_destroy_socket(struct sock *sk)
168{
169 atalk_remove_socket(sk);
170 skb_queue_purge(&sk->sk_receive_queue);
171
Eric Dumazetc5640392009-06-16 10:12:03 +0000172 if (sk_has_allocations(sk)) {
Kees Cooke99e88a2017-10-16 14:43:17 -0700173 timer_setup(&sk->sk_timer, atalk_destroy_timer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 sk->sk_timer.expires = jiffies + SOCK_DESTROY_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 add_timer(&sk->sk_timer);
176 } else
177 sock_put(sk);
178}
179
180/**************************************************************************\
181* *
182* Routing tables for the AppleTalk socket layer. *
183* *
184\**************************************************************************/
185
186/* Anti-deadlock ordering is atalk_routes_lock --> iface_lock -DaveM */
187struct atalk_route *atalk_routes;
188DEFINE_RWLOCK(atalk_routes_lock);
189
190struct atalk_iface *atalk_interfaces;
191DEFINE_RWLOCK(atalk_interfaces_lock);
192
193/* For probing devices or in a routerless network */
194struct atalk_route atrtr_default;
195
196/* AppleTalk interface control */
197/*
198 * Drop a device. Doesn't drop any of its routes - that is the caller's
199 * problem. Called when we down the interface or delete the address.
200 */
201static void atif_drop_device(struct net_device *dev)
202{
203 struct atalk_iface **iface = &atalk_interfaces;
204 struct atalk_iface *tmp;
205
206 write_lock_bh(&atalk_interfaces_lock);
207 while ((tmp = *iface) != NULL) {
208 if (tmp->dev == dev) {
209 *iface = tmp->next;
210 dev_put(dev);
211 kfree(tmp);
212 dev->atalk_ptr = NULL;
213 } else
214 iface = &tmp->next;
215 }
216 write_unlock_bh(&atalk_interfaces_lock);
217}
218
219static struct atalk_iface *atif_add_device(struct net_device *dev,
220 struct atalk_addr *sa)
221{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700222 struct atalk_iface *iface = kzalloc(sizeof(*iface), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 if (!iface)
225 goto out;
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 dev_hold(dev);
228 iface->dev = dev;
229 dev->atalk_ptr = iface;
230 iface->address = *sa;
231 iface->status = 0;
232
233 write_lock_bh(&atalk_interfaces_lock);
234 iface->next = atalk_interfaces;
235 atalk_interfaces = iface;
236 write_unlock_bh(&atalk_interfaces_lock);
237out:
238 return iface;
239}
240
241/* Perform phase 2 AARP probing on our tentative address */
242static int atif_probe_device(struct atalk_iface *atif)
243{
244 int netrange = ntohs(atif->nets.nr_lastnet) -
245 ntohs(atif->nets.nr_firstnet) + 1;
246 int probe_net = ntohs(atif->address.s_net);
247 int probe_node = atif->address.s_node;
248 int netct, nodect;
249
250 /* Offset the network we start probing with */
251 if (probe_net == ATADDR_ANYNET) {
252 probe_net = ntohs(atif->nets.nr_firstnet);
253 if (netrange)
254 probe_net += jiffies % netrange;
255 }
256 if (probe_node == ATADDR_ANYNODE)
257 probe_node = jiffies & 0xFF;
258
259 /* Scan the networks */
260 atif->status |= ATIF_PROBE;
261 for (netct = 0; netct <= netrange; netct++) {
262 /* Sweep the available nodes from a given start */
263 atif->address.s_net = htons(probe_net);
264 for (nodect = 0; nodect < 256; nodect++) {
265 atif->address.s_node = (nodect + probe_node) & 0xFF;
266 if (atif->address.s_node > 0 &&
267 atif->address.s_node < 254) {
268 /* Probe a proposed address */
269 aarp_probe_network(atif);
270
271 if (!(atif->status & ATIF_PROBE_FAIL)) {
272 atif->status &= ~ATIF_PROBE;
273 return 0;
274 }
275 }
276 atif->status &= ~ATIF_PROBE_FAIL;
277 }
278 probe_net++;
279 if (probe_net > ntohs(atif->nets.nr_lastnet))
280 probe_net = ntohs(atif->nets.nr_firstnet);
281 }
282 atif->status &= ~ATIF_PROBE;
283
284 return -EADDRINUSE; /* Network is full... */
285}
286
287
288/* Perform AARP probing for a proxy address */
289static int atif_proxy_probe_device(struct atalk_iface *atif,
wangweidong6f0984a2014-02-14 15:43:44 +0800290 struct atalk_addr *proxy_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 int netrange = ntohs(atif->nets.nr_lastnet) -
293 ntohs(atif->nets.nr_firstnet) + 1;
294 /* we probe the interface's network */
295 int probe_net = ntohs(atif->address.s_net);
296 int probe_node = ATADDR_ANYNODE; /* we'll take anything */
297 int netct, nodect;
298
299 /* Offset the network we start probing with */
300 if (probe_net == ATADDR_ANYNET) {
301 probe_net = ntohs(atif->nets.nr_firstnet);
302 if (netrange)
303 probe_net += jiffies % netrange;
304 }
305
306 if (probe_node == ATADDR_ANYNODE)
307 probe_node = jiffies & 0xFF;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* Scan the networks */
310 for (netct = 0; netct <= netrange; netct++) {
311 /* Sweep the available nodes from a given start */
312 proxy_addr->s_net = htons(probe_net);
313 for (nodect = 0; nodect < 256; nodect++) {
314 proxy_addr->s_node = (nodect + probe_node) & 0xFF;
315 if (proxy_addr->s_node > 0 &&
316 proxy_addr->s_node < 254) {
317 /* Tell AARP to probe a proposed address */
318 int ret = aarp_proxy_probe_network(atif,
319 proxy_addr);
320
321 if (ret != -EADDRINUSE)
322 return ret;
323 }
324 }
325 probe_net++;
326 if (probe_net > ntohs(atif->nets.nr_lastnet))
327 probe_net = ntohs(atif->nets.nr_firstnet);
328 }
329
330 return -EADDRINUSE; /* Network is full... */
331}
332
333
334struct atalk_addr *atalk_find_dev_addr(struct net_device *dev)
335{
336 struct atalk_iface *iface = dev->atalk_ptr;
337 return iface ? &iface->address : NULL;
338}
339
340static struct atalk_addr *atalk_find_primary(void)
341{
342 struct atalk_iface *fiface = NULL;
343 struct atalk_addr *retval;
344 struct atalk_iface *iface;
345
346 /*
347 * Return a point-to-point interface only if
348 * there is no non-ptp interface available.
349 */
350 read_lock_bh(&atalk_interfaces_lock);
351 for (iface = atalk_interfaces; iface; iface = iface->next) {
352 if (!fiface && !(iface->dev->flags & IFF_LOOPBACK))
353 fiface = iface;
354 if (!(iface->dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
355 retval = &iface->address;
356 goto out;
357 }
358 }
359
360 if (fiface)
361 retval = &fiface->address;
362 else if (atalk_interfaces)
363 retval = &atalk_interfaces->address;
364 else
365 retval = NULL;
366out:
367 read_unlock_bh(&atalk_interfaces_lock);
368 return retval;
369}
370
371/*
372 * Find a match for 'any network' - ie any of our interfaces with that
373 * node number will do just nicely.
374 */
375static struct atalk_iface *atalk_find_anynet(int node, struct net_device *dev)
376{
377 struct atalk_iface *iface = dev->atalk_ptr;
378
379 if (!iface || iface->status & ATIF_PROBE)
380 goto out_err;
381
382 if (node != ATADDR_BCAST &&
383 iface->address.s_node != node &&
384 node != ATADDR_ANYNODE)
385 goto out_err;
386out:
387 return iface;
388out_err:
389 iface = NULL;
390 goto out;
391}
392
393/* Find a match for a specific network:node pair */
Alexey Dobriyanf6e276ee2005-06-20 13:32:05 -0700394static struct atalk_iface *atalk_find_interface(__be16 net, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 struct atalk_iface *iface;
397
398 read_lock_bh(&atalk_interfaces_lock);
399 for (iface = atalk_interfaces; iface; iface = iface->next) {
400 if ((node == ATADDR_BCAST ||
401 node == ATADDR_ANYNODE ||
402 iface->address.s_node == node) &&
403 iface->address.s_net == net &&
404 !(iface->status & ATIF_PROBE))
405 break;
406
407 /* XXXX.0 -- net.0 returns the iface associated with net */
408 if (node == ATADDR_ANYNODE && net != ATADDR_ANYNET &&
409 ntohs(iface->nets.nr_firstnet) <= ntohs(net) &&
410 ntohs(net) <= ntohs(iface->nets.nr_lastnet))
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900411 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 read_unlock_bh(&atalk_interfaces_lock);
414 return iface;
415}
416
417
418/*
419 * Find a route for an AppleTalk packet. This ought to get cached in
420 * the socket (later on...). We know about host routes and the fact
421 * that a route must be direct to broadcast.
422 */
423static struct atalk_route *atrtr_find(struct atalk_addr *target)
424{
425 /*
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900426 * we must search through all routes unless we find a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * host route, because some host routes might overlap
428 * network routes
429 */
430 struct atalk_route *net_route = NULL;
431 struct atalk_route *r;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 read_lock_bh(&atalk_routes_lock);
434 for (r = atalk_routes; r; r = r->next) {
435 if (!(r->flags & RTF_UP))
436 continue;
437
438 if (r->target.s_net == target->s_net) {
439 if (r->flags & RTF_HOST) {
440 /*
441 * if this host route is for the target,
442 * the we're done
443 */
444 if (r->target.s_node == target->s_node)
445 goto out;
446 } else
447 /*
448 * this route will work if there isn't a
449 * direct host route, so cache it
450 */
451 net_route = r;
452 }
453 }
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900454
455 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * if we found a network route but not a direct host
457 * route, then return it
458 */
459 if (net_route)
460 r = net_route;
461 else if (atrtr_default.dev)
462 r = &atrtr_default;
463 else /* No route can be found */
464 r = NULL;
465out:
466 read_unlock_bh(&atalk_routes_lock);
467 return r;
468}
469
470
471/*
472 * Given an AppleTalk network, find the device to use. This can be
473 * a simple lookup.
474 */
475struct net_device *atrtr_get_dev(struct atalk_addr *sa)
476{
477 struct atalk_route *atr = atrtr_find(sa);
478 return atr ? atr->dev : NULL;
479}
480
481/* Set up a default router */
482static void atrtr_set_default(struct net_device *dev)
483{
484 atrtr_default.dev = dev;
485 atrtr_default.flags = RTF_UP;
486 atrtr_default.gateway.s_net = htons(0);
487 atrtr_default.gateway.s_node = 0;
488}
489
490/*
491 * Add a router. Basically make sure it looks valid and stuff the
492 * entry in the list. While it uses netranges we always set them to one
493 * entry to work like netatalk.
494 */
495static int atrtr_create(struct rtentry *r, struct net_device *devhint)
496{
497 struct sockaddr_at *ta = (struct sockaddr_at *)&r->rt_dst;
498 struct sockaddr_at *ga = (struct sockaddr_at *)&r->rt_gateway;
499 struct atalk_route *rt;
500 struct atalk_iface *iface, *riface;
501 int retval = -EINVAL;
502
503 /*
504 * Fixme: Raise/Lower a routing change semaphore for these
505 * operations.
506 */
507
508 /* Validate the request */
509 if (ta->sat_family != AF_APPLETALK ||
510 (!devhint && ga->sat_family != AF_APPLETALK))
511 goto out;
512
513 /* Now walk the routing table and make our decisions */
514 write_lock_bh(&atalk_routes_lock);
515 for (rt = atalk_routes; rt; rt = rt->next) {
516 if (r->rt_flags != rt->flags)
517 continue;
518
519 if (ta->sat_addr.s_net == rt->target.s_net) {
520 if (!(rt->flags & RTF_HOST))
521 break;
522 if (ta->sat_addr.s_node == rt->target.s_node)
523 break;
524 }
525 }
526
527 if (!devhint) {
528 riface = NULL;
529
530 read_lock_bh(&atalk_interfaces_lock);
531 for (iface = atalk_interfaces; iface; iface = iface->next) {
532 if (!riface &&
533 ntohs(ga->sat_addr.s_net) >=
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900534 ntohs(iface->nets.nr_firstnet) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 ntohs(ga->sat_addr.s_net) <=
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900536 ntohs(iface->nets.nr_lastnet))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 riface = iface;
538
539 if (ga->sat_addr.s_net == iface->address.s_net &&
540 ga->sat_addr.s_node == iface->address.s_node)
541 riface = iface;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 read_unlock_bh(&atalk_interfaces_lock);
544
545 retval = -ENETUNREACH;
546 if (!riface)
547 goto out_unlock;
548
549 devhint = riface->dev;
550 }
551
552 if (!rt) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700553 rt = kzalloc(sizeof(*rt), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 retval = -ENOBUFS;
556 if (!rt)
557 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 rt->next = atalk_routes;
560 atalk_routes = rt;
561 }
562
563 /* Fill in the routing entry */
564 rt->target = ta->sat_addr;
Herbert Xuc7f905f02005-04-19 22:44:17 -0700565 dev_hold(devhint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 rt->dev = devhint;
567 rt->flags = r->rt_flags;
568 rt->gateway = ga->sat_addr;
569
570 retval = 0;
571out_unlock:
572 write_unlock_bh(&atalk_routes_lock);
573out:
574 return retval;
575}
576
577/* Delete a route. Find it and discard it */
wangweidong6f0984a2014-02-14 15:43:44 +0800578static int atrtr_delete(struct atalk_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct atalk_route **r = &atalk_routes;
581 int retval = 0;
582 struct atalk_route *tmp;
583
584 write_lock_bh(&atalk_routes_lock);
585 while ((tmp = *r) != NULL) {
586 if (tmp->target.s_net == addr->s_net &&
587 (!(tmp->flags&RTF_GATEWAY) ||
588 tmp->target.s_node == addr->s_node)) {
589 *r = tmp->next;
590 dev_put(tmp->dev);
591 kfree(tmp);
592 goto out;
593 }
594 r = &tmp->next;
595 }
596 retval = -ENOENT;
597out:
598 write_unlock_bh(&atalk_routes_lock);
599 return retval;
600}
601
602/*
603 * Called when a device is downed. Just throw away any routes
604 * via it.
605 */
606static void atrtr_device_down(struct net_device *dev)
607{
608 struct atalk_route **r = &atalk_routes;
609 struct atalk_route *tmp;
610
611 write_lock_bh(&atalk_routes_lock);
612 while ((tmp = *r) != NULL) {
613 if (tmp->dev == dev) {
614 *r = tmp->next;
615 dev_put(dev);
616 kfree(tmp);
617 } else
618 r = &tmp->next;
619 }
620 write_unlock_bh(&atalk_routes_lock);
621
622 if (atrtr_default.dev == dev)
623 atrtr_set_default(NULL);
624}
625
626/* Actually down the interface */
627static inline void atalk_dev_down(struct net_device *dev)
628{
629 atrtr_device_down(dev); /* Remove all routes for the device */
630 aarp_device_down(dev); /* Remove AARP entries for the device */
631 atif_drop_device(dev); /* Remove the device */
632}
633
634/*
635 * A device event has occurred. Watch for devices going down and
636 * delete our use of them (iface and route).
637 */
638static int ddp_device_event(struct notifier_block *this, unsigned long event,
639 void *ptr)
640{
Jiri Pirko351638e2013-05-28 01:30:21 +0000641 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biederman890d52d2007-09-12 11:26:59 +0200642
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700643 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200644 return NOTIFY_DONE;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (event == NETDEV_DOWN)
647 /* Discard any use of this */
Eric W. Biederman890d52d2007-09-12 11:26:59 +0200648 atalk_dev_down(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 return NOTIFY_DONE;
651}
652
653/* ioctl calls. Shouldn't even need touching */
654/* Device configuration ioctl calls */
655static int atif_ioctl(int cmd, void __user *arg)
656{
657 static char aarp_mcast[6] = { 0x09, 0x00, 0x00, 0xFF, 0xFF, 0xFF };
658 struct ifreq atreq;
659 struct atalk_netrange *nr;
660 struct sockaddr_at *sa;
661 struct net_device *dev;
662 struct atalk_iface *atif;
663 int ct;
664 int limit;
665 struct rtentry rtdef;
666 int add_route;
667
668 if (copy_from_user(&atreq, arg, sizeof(atreq)))
669 return -EFAULT;
670
Eric W. Biederman881d9662007-09-17 11:56:21 -0700671 dev = __dev_get_by_name(&init_net, atreq.ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (!dev)
673 return -ENODEV;
674
675 sa = (struct sockaddr_at *)&atreq.ifr_addr;
676 atif = atalk_find_dev(dev);
677
678 switch (cmd) {
Joe Perches4a9e4b02011-07-01 09:43:02 +0000679 case SIOCSIFADDR:
680 if (!capable(CAP_NET_ADMIN))
681 return -EPERM;
682 if (sa->sat_family != AF_APPLETALK)
683 return -EINVAL;
684 if (dev->type != ARPHRD_ETHER &&
685 dev->type != ARPHRD_LOOPBACK &&
686 dev->type != ARPHRD_LOCALTLK &&
687 dev->type != ARPHRD_PPP)
688 return -EPROTONOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Joe Perches4a9e4b02011-07-01 09:43:02 +0000690 nr = (struct atalk_netrange *)&sa->sat_zero[0];
691 add_route = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Joe Perches4a9e4b02011-07-01 09:43:02 +0000693 /*
694 * if this is a point-to-point iface, and we already
695 * have an iface for this AppleTalk address, then we
696 * should not add a route
697 */
698 if ((dev->flags & IFF_POINTOPOINT) &&
699 atalk_find_interface(sa->sat_addr.s_net,
700 sa->sat_addr.s_node)) {
701 printk(KERN_DEBUG "AppleTalk: point-to-point "
702 "interface added with "
703 "existing address\n");
704 add_route = 0;
705 }
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900706
Joe Perches4a9e4b02011-07-01 09:43:02 +0000707 /*
708 * Phase 1 is fine on LocalTalk but we don't do
709 * EtherTalk phase 1. Anyone wanting to add it go ahead.
710 */
711 if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
712 return -EPROTONOSUPPORT;
713 if (sa->sat_addr.s_node == ATADDR_BCAST ||
714 sa->sat_addr.s_node == 254)
715 return -EINVAL;
716 if (atif) {
717 /* Already setting address */
718 if (atif->status & ATIF_PROBE)
719 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Joe Perches4a9e4b02011-07-01 09:43:02 +0000721 atif->address.s_net = sa->sat_addr.s_net;
722 atif->address.s_node = sa->sat_addr.s_node;
723 atrtr_device_down(dev); /* Flush old routes */
724 } else {
725 atif = atif_add_device(dev, &sa->sat_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (!atif)
Joe Perches4a9e4b02011-07-01 09:43:02 +0000727 return -ENOMEM;
728 }
729 atif->nets = *nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Joe Perches4a9e4b02011-07-01 09:43:02 +0000731 /*
732 * Check if the chosen address is used. If so we
733 * error and atalkd will try another.
734 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Joe Perches4a9e4b02011-07-01 09:43:02 +0000736 if (!(dev->flags & IFF_LOOPBACK) &&
737 !(dev->flags & IFF_POINTOPOINT) &&
738 atif_probe_device(atif) < 0) {
739 atif_drop_device(dev);
740 return -EADDRINUSE;
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Joe Perches4a9e4b02011-07-01 09:43:02 +0000743 /* Hey it worked - add the direct routes */
744 sa = (struct sockaddr_at *)&rtdef.rt_gateway;
745 sa->sat_family = AF_APPLETALK;
746 sa->sat_addr.s_net = atif->address.s_net;
747 sa->sat_addr.s_node = atif->address.s_node;
748 sa = (struct sockaddr_at *)&rtdef.rt_dst;
749 rtdef.rt_flags = RTF_UP;
750 sa->sat_family = AF_APPLETALK;
751 sa->sat_addr.s_node = ATADDR_ANYNODE;
752 if (dev->flags & IFF_LOOPBACK ||
753 dev->flags & IFF_POINTOPOINT)
754 rtdef.rt_flags |= RTF_HOST;
755
756 /* Routerless initial state */
757 if (nr->nr_firstnet == htons(0) &&
758 nr->nr_lastnet == htons(0xFFFE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 sa->sat_addr.s_net = atif->address.s_net;
Joe Perches4a9e4b02011-07-01 09:43:02 +0000760 atrtr_create(&rtdef, dev);
761 atrtr_set_default(dev);
762 } else {
763 limit = ntohs(nr->nr_lastnet);
764 if (limit - ntohs(nr->nr_firstnet) > 4096) {
765 printk(KERN_WARNING "Too many routes/"
766 "iface.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return -EINVAL;
Joe Perches4a9e4b02011-07-01 09:43:02 +0000768 }
769 if (add_route)
770 for (ct = ntohs(nr->nr_firstnet);
771 ct <= limit; ct++) {
772 sa->sat_addr.s_net = htons(ct);
773 atrtr_create(&rtdef, dev);
774 }
775 }
776 dev_mc_add_global(dev, aarp_mcast);
777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Joe Perches4a9e4b02011-07-01 09:43:02 +0000779 case SIOCGIFADDR:
780 if (!atif)
781 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Joe Perches4a9e4b02011-07-01 09:43:02 +0000783 sa->sat_family = AF_APPLETALK;
784 sa->sat_addr = atif->address;
785 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Joe Perches4a9e4b02011-07-01 09:43:02 +0000787 case SIOCGIFBRDADDR:
788 if (!atif)
789 return -EADDRNOTAVAIL;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900790
Joe Perches4a9e4b02011-07-01 09:43:02 +0000791 sa->sat_family = AF_APPLETALK;
792 sa->sat_addr.s_net = atif->address.s_net;
793 sa->sat_addr.s_node = ATADDR_BCAST;
794 break;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900795
Joe Perches4a9e4b02011-07-01 09:43:02 +0000796 case SIOCATALKDIFADDR:
797 case SIOCDIFADDR:
798 if (!capable(CAP_NET_ADMIN))
799 return -EPERM;
800 if (sa->sat_family != AF_APPLETALK)
801 return -EINVAL;
802 atalk_dev_down(dev);
803 break;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900804
Joe Perches4a9e4b02011-07-01 09:43:02 +0000805 case SIOCSARP:
806 if (!capable(CAP_NET_ADMIN))
807 return -EPERM;
808 if (sa->sat_family != AF_APPLETALK)
809 return -EINVAL;
810 /*
811 * for now, we only support proxy AARP on ELAP;
812 * we should be able to do it for LocalTalk, too.
813 */
814 if (dev->type != ARPHRD_ETHER)
815 return -EPROTONOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Joe Perches4a9e4b02011-07-01 09:43:02 +0000817 /*
818 * atif points to the current interface on this network;
819 * we aren't concerned about its current status (at
820 * least for now), but it has all the settings about
821 * the network we're going to probe. Consequently, it
822 * must exist.
823 */
824 if (!atif)
825 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Joe Perches4a9e4b02011-07-01 09:43:02 +0000827 nr = (struct atalk_netrange *)&(atif->nets);
828 /*
829 * Phase 1 is fine on Localtalk but we don't do
830 * Ethertalk phase 1. Anyone wanting to add it go ahead.
831 */
832 if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
833 return -EPROTONOSUPPORT;
834
835 if (sa->sat_addr.s_node == ATADDR_BCAST ||
836 sa->sat_addr.s_node == 254)
837 return -EINVAL;
838
839 /*
840 * Check if the chosen address is used. If so we
841 * error and ATCP will try another.
842 */
843 if (atif_proxy_probe_device(atif, &(sa->sat_addr)) < 0)
844 return -EADDRINUSE;
845
846 /*
847 * We now have an address on the local network, and
848 * the AARP code will defend it for us until we take it
849 * down. We don't set up any routes right now, because
850 * ATCP will install them manually via SIOCADDRT.
851 */
852 break;
853
854 case SIOCDARP:
855 if (!capable(CAP_NET_ADMIN))
856 return -EPERM;
857 if (sa->sat_family != AF_APPLETALK)
858 return -EINVAL;
859 if (!atif)
860 return -EADDRNOTAVAIL;
861
862 /* give to aarp module to remove proxy entry */
863 aarp_proxy_remove(atif->dev, &(sa->sat_addr));
864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866
867 return copy_to_user(arg, &atreq, sizeof(atreq)) ? -EFAULT : 0;
868}
869
Christoph Hellwiga5004922020-05-18 08:28:07 +0200870static int atrtr_ioctl_addrt(struct rtentry *rt)
871{
872 struct net_device *dev = NULL;
873
874 if (rt->rt_dev) {
875 char name[IFNAMSIZ];
876
877 if (copy_from_user(name, rt->rt_dev, IFNAMSIZ-1))
878 return -EFAULT;
879 name[IFNAMSIZ-1] = '\0';
880
881 dev = __dev_get_by_name(&init_net, name);
882 if (!dev)
883 return -ENODEV;
884 }
885 return atrtr_create(rt, dev);
886}
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888/* Routing ioctl() calls */
889static int atrtr_ioctl(unsigned int cmd, void __user *arg)
890{
891 struct rtentry rt;
892
893 if (copy_from_user(&rt, arg, sizeof(rt)))
894 return -EFAULT;
895
896 switch (cmd) {
Joe Perches4a9e4b02011-07-01 09:43:02 +0000897 case SIOCDELRT:
898 if (rt.rt_dst.sa_family != AF_APPLETALK)
899 return -EINVAL;
900 return atrtr_delete(&((struct sockaddr_at *)
901 &rt.rt_dst)->sat_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Christoph Hellwiga5004922020-05-18 08:28:07 +0200903 case SIOCADDRT:
904 return atrtr_ioctl_addrt(&rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906 return -EINVAL;
907}
908
909/**************************************************************************\
910* *
911* Handling for system calls applied via the various interfaces to an *
912* AppleTalk socket object. *
913* *
914\**************************************************************************/
915
916/*
917 * Checksum: This is 'optional'. It's quite likely also a good
918 * candidate for assembler hackery 8)
919 */
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900920static unsigned long atalk_sum_partial(const unsigned char *data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int len, unsigned long sum)
922{
923 /* This ought to be unwrapped neatly. I'll trust gcc for now */
924 while (len--) {
Joe Perchesf7a3a1d2009-11-04 10:26:13 +0000925 sum += *data++;
926 sum = rol16(sum, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928 return sum;
929}
930
931/* Checksum skb data -- similar to skb_checksum */
932static unsigned long atalk_sum_skb(const struct sk_buff *skb, int offset,
933 int len, unsigned long sum)
934{
David S. Miller1a028e52007-04-27 15:21:23 -0700935 int start = skb_headlen(skb);
David S. Millerc32ba3f2009-06-09 00:17:44 -0700936 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 int i, copy;
938
939 /* checksum stuff in header space */
wangweidongfd1dc262014-02-14 15:43:43 +0800940 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (copy > len)
942 copy = len;
943 sum = atalk_sum_partial(skb->data + offset, copy, sum);
wangweidongfd1dc262014-02-14 15:43:43 +0800944 if ((len -= copy) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return sum;
946
947 offset += copy;
948 }
949
950 /* checksum stuff in frags */
951 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -0700952 int end;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000953 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700954 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -0700955
Eric Dumazet9e903e02011-10-18 21:00:24 +0000956 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if ((copy = end - offset) > 0) {
958 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (copy > len)
961 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +0200962 vaddr = kmap_atomic(skb_frag_page(frag));
Jonathan Lemonb54c9d52019-07-30 07:40:33 -0700963 sum = atalk_sum_partial(vaddr + skb_frag_off(frag) +
964 offset - start, copy, sum);
Eric Dumazet51c56b02012-04-05 11:35:15 +0200965 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 if (!(len -= copy))
968 return sum;
969 offset += copy;
970 }
David S. Miller1a028e52007-04-27 15:21:23 -0700971 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973
David S. Millerc32ba3f2009-06-09 00:17:44 -0700974 skb_walk_frags(skb, frag_iter) {
975 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
David S. Millerc32ba3f2009-06-09 00:17:44 -0700977 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
David S. Millerc32ba3f2009-06-09 00:17:44 -0700979 end = start + frag_iter->len;
980 if ((copy = end - offset) > 0) {
981 if (copy > len)
982 copy = len;
983 sum = atalk_sum_skb(frag_iter, offset - start,
984 copy, sum);
985 if ((len -= copy) == 0)
986 return sum;
987 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
David S. Millerc32ba3f2009-06-09 00:17:44 -0700989 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991
992 BUG_ON(len > 0);
993
994 return sum;
995}
996
Al Viro2a50f282006-09-26 21:22:08 -0700997static __be16 atalk_checksum(const struct sk_buff *skb, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 unsigned long sum;
1000
1001 /* skip header 4 bytes */
1002 sum = atalk_sum_skb(skb, 4, len-4, 0);
1003
1004 /* Use 0xFFFF for 0. 0 itself means none */
Al Viro2a50f282006-09-26 21:22:08 -07001005 return sum ? htons((unsigned short)sum) : htons(0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008static struct proto ddp_proto = {
1009 .name = "DDP",
1010 .owner = THIS_MODULE,
1011 .obj_size = sizeof(struct atalk_sock),
1012};
1013
1014/*
1015 * Create a socket. Initialise the socket, blank the addresses
1016 * set the state.
1017 */
Eric Paris3f378b62009-11-05 22:18:14 -08001018static int atalk_create(struct net *net, struct socket *sock, int protocol,
1019 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 struct sock *sk;
1022 int rc = -ESOCKTNOSUPPORT;
1023
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001024 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001025 return -EAFNOSUPPORT;
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /*
1028 * We permit SOCK_DGRAM and RAW is an extension. It is trivial to do
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001029 * and gives you the full ELAP frame. Should be handy for CAP 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 */
1031 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
1032 goto out;
Ori Nimron6cc03e82019-09-20 09:35:46 +02001033
1034 rc = -EPERM;
1035 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1036 goto out;
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 rc = -ENOMEM;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001039 sk = sk_alloc(net, PF_APPLETALK, GFP_KERNEL, &ddp_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (!sk)
1041 goto out;
1042 rc = 0;
1043 sock->ops = &atalk_dgram_ops;
1044 sock_init_data(sock, sk);
1045
1046 /* Checksums on by default */
1047 sock_set_flag(sk, SOCK_ZAPPED);
1048out:
1049 return rc;
1050}
1051
1052/* Free a socket. No work needed */
1053static int atalk_release(struct socket *sock)
1054{
1055 struct sock *sk = sock->sk;
1056
1057 if (sk) {
David S. Millerc100c8f2011-03-31 18:59:10 -07001058 sock_hold(sk);
1059 lock_sock(sk);
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 sock_orphan(sk);
1062 sock->sk = NULL;
1063 atalk_destroy_socket(sk);
Arnd Bergmannb20e7bb2011-03-21 18:18:00 -07001064
David S. Millerc100c8f2011-03-31 18:59:10 -07001065 release_sock(sk);
1066 sock_put(sk);
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return 0;
1069}
1070
1071/**
1072 * atalk_pick_and_bind_port - Pick a source port when one is not given
Ben Hutchings2c530402012-07-10 10:55:09 +00001073 * @sk: socket to insert into the tables
1074 * @sat: address to search for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 *
1076 * Pick a source port when one is not given. If we can find a suitable free
1077 * one, we insert the socket into the tables using it.
1078 *
1079 * This whole operation must be atomic.
1080 */
1081static int atalk_pick_and_bind_port(struct sock *sk, struct sockaddr_at *sat)
1082{
1083 int retval;
1084
1085 write_lock_bh(&atalk_sockets_lock);
1086
1087 for (sat->sat_port = ATPORT_RESERVED;
1088 sat->sat_port < ATPORT_LAST;
1089 sat->sat_port++) {
1090 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Sasha Levinb67bfe02013-02-27 17:06:00 -08001092 sk_for_each(s, &atalk_sockets) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 struct atalk_sock *at = at_sk(s);
1094
1095 if (at->src_net == sat->sat_addr.s_net &&
1096 at->src_node == sat->sat_addr.s_node &&
1097 at->src_port == sat->sat_port)
1098 goto try_next_port;
1099 }
1100
1101 /* Wheee, it's free, assign and insert. */
1102 __atalk_insert_socket(sk);
1103 at_sk(sk)->src_port = sat->sat_port;
1104 retval = 0;
1105 goto out;
1106
1107try_next_port:;
1108 }
1109
1110 retval = -EBUSY;
1111out:
1112 write_unlock_bh(&atalk_sockets_lock);
1113 return retval;
1114}
1115
1116static int atalk_autobind(struct sock *sk)
1117{
1118 struct atalk_sock *at = at_sk(sk);
1119 struct sockaddr_at sat;
1120 struct atalk_addr *ap = atalk_find_primary();
1121 int n = -EADDRNOTAVAIL;
1122
1123 if (!ap || ap->s_net == htons(ATADDR_ANYNET))
1124 goto out;
1125
1126 at->src_net = sat.sat_addr.s_net = ap->s_net;
1127 at->src_node = sat.sat_addr.s_node = ap->s_node;
1128
1129 n = atalk_pick_and_bind_port(sk, &sat);
1130 if (!n)
1131 sock_reset_flag(sk, SOCK_ZAPPED);
1132out:
1133 return n;
1134}
1135
1136/* Set the address 'our end' of the connection */
1137static int atalk_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1138{
1139 struct sockaddr_at *addr = (struct sockaddr_at *)uaddr;
1140 struct sock *sk = sock->sk;
1141 struct atalk_sock *at = at_sk(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001142 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 if (!sock_flag(sk, SOCK_ZAPPED) ||
1145 addr_len != sizeof(struct sockaddr_at))
1146 return -EINVAL;
1147
1148 if (addr->sat_family != AF_APPLETALK)
1149 return -EAFNOSUPPORT;
1150
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001151 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (addr->sat_addr.s_net == htons(ATADDR_ANYNET)) {
1153 struct atalk_addr *ap = atalk_find_primary();
1154
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001155 err = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (!ap)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001157 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 at->src_net = addr->sat_addr.s_net = ap->s_net;
wangweidongfd1dc262014-02-14 15:43:43 +08001160 at->src_node = addr->sat_addr.s_node = ap->s_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 } else {
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001162 err = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (!atalk_find_interface(addr->sat_addr.s_net,
1164 addr->sat_addr.s_node))
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001165 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 at->src_net = addr->sat_addr.s_net;
1168 at->src_node = addr->sat_addr.s_node;
1169 }
1170
1171 if (addr->sat_port == ATADDR_ANYPORT) {
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001172 err = atalk_pick_and_bind_port(sk, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001174 if (err < 0)
1175 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 } else {
1177 at->src_port = addr->sat_port;
1178
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001179 err = -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (atalk_find_or_insert_socket(sk, addr))
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001181 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
1184 sock_reset_flag(sk, SOCK_ZAPPED);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001185 err = 0;
1186out:
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001187 release_sock(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001188 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191/* Set the address we talk to */
1192static int atalk_connect(struct socket *sock, struct sockaddr *uaddr,
1193 int addr_len, int flags)
1194{
1195 struct sock *sk = sock->sk;
1196 struct atalk_sock *at = at_sk(sk);
1197 struct sockaddr_at *addr;
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001198 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 sk->sk_state = TCP_CLOSE;
1201 sock->state = SS_UNCONNECTED;
1202
1203 if (addr_len != sizeof(*addr))
1204 return -EINVAL;
1205
1206 addr = (struct sockaddr_at *)uaddr;
1207
1208 if (addr->sat_family != AF_APPLETALK)
1209 return -EAFNOSUPPORT;
1210
1211 if (addr->sat_addr.s_node == ATADDR_BCAST &&
1212 !sock_flag(sk, SOCK_BROADCAST)) {
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001213#if 1
Dave Jones278f0152012-06-06 08:45:59 +00001214 pr_warn("atalk_connect: %s is broken and did not set SO_BROADCAST.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 current->comm);
1216#else
1217 return -EACCES;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001221 lock_sock(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001222 err = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (sock_flag(sk, SOCK_ZAPPED))
1224 if (atalk_autobind(sk) < 0)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001225 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001227 err = -ENETUNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (!atrtr_get_dev(&addr->sat_addr))
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001229 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 at->dest_port = addr->sat_port;
1232 at->dest_net = addr->sat_addr.s_net;
1233 at->dest_node = addr->sat_addr.s_node;
1234
1235 sock->state = SS_CONNECTED;
1236 sk->sk_state = TCP_ESTABLISHED;
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001237 err = 0;
1238out:
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001239 release_sock(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001240 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
1243/*
1244 * Find the name of an AppleTalk socket. Just copy the right
1245 * fields into the sockaddr.
1246 */
1247static int atalk_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001248 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
1250 struct sockaddr_at sat;
1251 struct sock *sk = sock->sk;
1252 struct atalk_sock *at = at_sk(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001253 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001255 lock_sock(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001256 err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (sock_flag(sk, SOCK_ZAPPED))
1258 if (atalk_autobind(sk) < 0)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001259 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Dan Carpenterfccc9f12013-04-22 20:22:15 +00001261 memset(&sat, 0, sizeof(sat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 if (peer) {
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001264 err = -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (sk->sk_state != TCP_ESTABLISHED)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001266 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
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 sat.sat_family = AF_APPLETALK;
1278 memcpy(uaddr, &sat, sizeof(sat));
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001279 err = sizeof(struct sockaddr_at);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001280
1281out:
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001282 release_sock(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001283 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Javier Martinez Canillasa73ec312016-09-09 08:43:13 -04001286#if IS_ENABLED(CONFIG_IPDDP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287static __inline__ int is_ip_over_ddp(struct sk_buff *skb)
1288{
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001289 return skb->data[12] == 22;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
1292static int handle_ip_over_ddp(struct sk_buff *skb)
1293{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001294 struct net_device *dev = __dev_get_by_name(&init_net, "ipddp0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 struct net_device_stats *stats;
1296
1297 /* This needs to be able to handle ipddp"N" devices */
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -07001298 if (!dev) {
1299 kfree_skb(skb);
1300 return NET_RX_DROP;
1301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001303 skb->protocol = htons(ETH_P_IP);
1304 skb_pull(skb, 13);
1305 skb->dev = dev;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001306 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Wang Chen524ad0a2008-11-12 23:39:10 -08001308 stats = netdev_priv(dev);
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001309 stats->rx_packets++;
1310 stats->rx_bytes += skb->len + 13;
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -07001311 return netif_rx(skb); /* Send the SKB up to a higher place. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
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
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -07001319static int atalk_route_packet(struct sk_buff *skb, struct net_device *dev,
1320 struct ddpehdr *ddp, __u16 len_hops, int origlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 struct atalk_route *rt;
1323 struct atalk_addr ta;
1324
1325 /*
1326 * Don't route multicast, etc., packets, or packets sent to "this
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001327 * network"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 */
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);
Al Viro2a50f282006-09-26 21:22:08 -07001347 /* increment hops count */
1348 len_hops += 1 << 10;
1349 if (!rt || !(len_hops & (15 << 10)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 goto free_it;
Al Viro2a50f282006-09-26 21:22:08 -07001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 /* FIXME: use skb->cb to be able to use shared skbs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
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
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001364 /* Fix up skb->len field */
1365 skb_trim(skb, min_t(unsigned int, origlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 (rt->dev->hard_header_len +
Al Viro2a50f282006-09-26 21:22:08 -07001367 ddp_dl->header_length + (len_hops & 1023))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /* FIXME: use skb->cb to be able to use shared skbs */
Al Viro2a50f282006-09-26 21:22:08 -07001370 ddp->deh_len_hops = htons(len_hops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 skb = nskb;
1388 } else
1389 skb = skb_unshare(skb, GFP_ATOMIC);
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /*
1392 * If the buffer didn't vanish into the lack of space bitbucket we can
1393 * send it.
1394 */
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -07001395 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;
Mark Smith8be80572009-09-12 20:48:43 +00001400 return NET_RX_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401free_it:
1402 kfree_skb(skb);
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -07001403drop:
1404 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
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
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001414 * 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]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 */
1419static int atalk_rcv(struct sk_buff *skb, struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001420 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
1422 struct ddpehdr *ddp;
1423 struct sock *sock;
1424 struct atalk_iface *atif;
1425 struct sockaddr_at tosat;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001426 int origlen;
Al Viro2a50f282006-09-26 21:22:08 -07001427 __u16 len_hops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001429 if (!net_eq(dev_net(dev), &init_net))
Mark Smith6885ffb2009-08-06 23:21:22 +00001430 goto drop;
Eric W. Biedermane730c152007-09-17 11:53:39 -07001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /* Don't mangle buffer if shared */
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001433 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 goto out;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 /* Size check and make sure header is contiguous */
1437 if (!pskb_may_pull(skb, sizeof(*ddp)))
Mark Smith6885ffb2009-08-06 23:21:22 +00001438 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 ddp = ddp_hdr(skb);
1441
Al Viro2a50f282006-09-26 21:22:08 -07001442 len_hops = ntohs(ddp->deh_len_hops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 /* Trim buffer in case of stray trailing data */
1445 origlen = skb->len;
Al Viro2a50f282006-09-26 21:22:08 -07001446 skb_trim(skb, min_t(unsigned int, skb->len, len_hops & 1023));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 /*
1449 * Size check to see if ddp->deh_len was crap
1450 * (Otherwise we'll detonate most spectacularly
Jean Delvare75559c12007-04-04 23:52:46 -07001451 * in the middle of atalk_checksum() or recvmsg()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 */
Jean Delvare75559c12007-04-04 23:52:46 -07001453 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);
Mark Smith6885ffb2009-08-06 23:21:22 +00001456 goto drop;
Jean Delvare75559c12007-04-04 23:52:46 -07001457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
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 &&
Al Viro2a50f282006-09-26 21:22:08 -07001464 atalk_checksum(skb, len_hops & 1023) != ddp->deh_sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 /* Not a valid AppleTalk frame - dustbin time */
Mark Smith6885ffb2009-08-06 23:21:22 +00001466 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (!atif) {
Oliver Dawid64233bf2005-09-27 16:11:29 -07001475 /* Not ours, so we route the packet via the correct
1476 * AppleTalk iface
1477 */
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -07001478 return atalk_route_packet(skb, dev, ddp, len_hops, origlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
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 */
Mark Smith6885ffb2009-08-06 23:21:22 +00001494 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 /* Queue packet (standard) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (sock_queue_rcv_skb(sock, skb) < 0)
Mark Smith6885ffb2009-08-06 23:21:22 +00001498 goto drop;
1499
1500 return NET_RX_SUCCESS;
1501
1502drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 kfree_skb(skb);
Mark Smith6885ffb2009-08-06 23:21:22 +00001504out:
1505 return NET_RX_DROP;
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
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 */
1514static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001515 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001517 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -07001518 goto freeit;
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 /* Expand any short form frames */
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001521 if (skb_mac_header(skb)[2] == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 struct ddpehdr *ddp;
1523 /* Find our address */
1524 struct atalk_addr *ap = atalk_find_dev_addr(dev);
1525
Al Viro2a50f282006-09-26 21:22:08 -07001526 if (!ap || skb->len < sizeof(__be16) || skb->len > 1023)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 goto freeit;
1528
1529 /* Don't mangle buffer if shared */
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001530 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 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 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001537 ddp = skb_push(skb, sizeof(*ddp) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 /* Now fill in the long header */
1540
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001541 /*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001547 ddp->deh_dnode = skb_mac_header(skb)[0]; /* From physical header */
1548 ddp->deh_snode = skb_mac_header(skb)[1]; /* From physical header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
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 */
Al Viro2a50f282006-09-26 21:22:08 -07001556 /* Non routable, so force a drop if we slip up later */
1557 ddp->deh_len_hops = htons(skb->len + (DDP_MAXHOPS << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001559 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001561 return atalk_rcv(skb, dev, pt, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562freeit:
1563 kfree_skb(skb);
1564 return 0;
1565}
1566
Ying Xue1b784142015-03-02 15:37:48 +08001567static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 struct sock *sk = sock->sk;
1570 struct atalk_sock *at = at_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001571 DECLARE_SOCKADDR(struct sockaddr_at *, usat, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 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;
1579 struct atalk_route *rt;
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
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001588 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (usat) {
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001590 err = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (sock_flag(sk, SOCK_ZAPPED))
1592 if (atalk_autobind(sk) < 0)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001593 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001595 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (msg->msg_namelen < sizeof(*usat) ||
1597 usat->sat_family != AF_APPLETALK)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001598 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001600 err = -EPERM;
Alan Cox03ba9992009-03-27 00:27:18 -07001601 /* netatalk didn't implement this check */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (usat->sat_addr.s_node == ATADDR_BCAST &&
1603 !sock_flag(sk, SOCK_BROADCAST)) {
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001604 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
1606 } else {
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001607 err = -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (sk->sk_state != TCP_ESTABLISHED)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001609 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 } 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
Anton Protopopov48bb2302016-02-17 10:53:59 -05001633 err = -ENETUNREACH;
Oliver Dawid64233bf2005-09-27 16:11:29 -07001634 if (!rt)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001635 goto out;
Oliver Dawid64233bf2005-09-27 16:11:29 -07001636
1637 dev = rt->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 SOCK_DEBUG(sk, "SK %p: Size needed %d, device %s\n",
1640 sk, size, dev->name);
1641
1642 size += dev->hard_header_len;
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001643 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 skb = sock_alloc_send_skb(sk, size, (flags & MSG_DONTWAIT), &err);
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001645 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 if (!skb)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001647 goto out;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 skb_reserve(skb, ddp_dl->header_length);
1650 skb_reserve(skb, dev->hard_header_len);
1651 skb->dev = dev;
1652
1653 SOCK_DEBUG(sk, "SK %p: Begin build.\n", sk);
1654
Johannes Berg4df864c2017-06-16 14:29:21 +02001655 ddp = skb_put(skb, sizeof(struct ddpehdr));
Al Viro2a50f282006-09-26 21:22:08 -07001656 ddp->deh_len_hops = htons(len + sizeof(*ddp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 ddp->deh_dnet = usat->sat_addr.s_net;
1658 ddp->deh_snet = at->src_net;
1659 ddp->deh_dnode = usat->sat_addr.s_node;
1660 ddp->deh_snode = at->src_node;
1661 ddp->deh_dport = usat->sat_port;
1662 ddp->deh_sport = at->src_port;
1663
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001664 SOCK_DEBUG(sk, "SK %p: Copy user data (%zd bytes).\n", sk, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Al Viro6ce8e9c2014-04-06 21:25:44 -04001666 err = memcpy_from_msg(skb_put(skb, len), msg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (err) {
1668 kfree_skb(skb);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001669 err = -EFAULT;
1670 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
1672
Tom Herbert28448b82014-05-23 08:47:19 -07001673 if (sk->sk_no_check_tx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 ddp->deh_sum = 0;
1675 else
1676 ddp->deh_sum = atalk_checksum(skb, len + sizeof(*ddp));
1677
1678 /*
1679 * Loopback broadcast packets to non gateway targets (ie routes
1680 * to group we are in)
1681 */
1682 if (ddp->deh_dnode == ATADDR_BCAST &&
1683 !(rt->flags & RTF_GATEWAY) && !(dev->flags & IFF_LOOPBACK)) {
1684 struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL);
1685
1686 if (skb2) {
1687 loopback = 1;
1688 SOCK_DEBUG(sk, "SK %p: send out(copy).\n", sk);
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -07001689 /*
1690 * If it fails it is queued/sent above in the aarp queue
1691 */
1692 aarp_send_ddp(dev, skb2, &usat->sat_addr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694 }
1695
1696 if (dev->flags & IFF_LOOPBACK || loopback) {
1697 SOCK_DEBUG(sk, "SK %p: Loop back.\n", sk);
1698 /* loop back */
1699 skb_orphan(skb);
Oliver Dawid64233bf2005-09-27 16:11:29 -07001700 if (ddp->deh_dnode == ATADDR_BCAST) {
1701 struct atalk_addr at_lo;
1702
1703 at_lo.s_node = 0;
1704 at_lo.s_net = 0;
1705
1706 rt = atrtr_find(&at_lo);
1707 if (!rt) {
1708 kfree_skb(skb);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001709 err = -ENETUNREACH;
1710 goto out;
Oliver Dawid64233bf2005-09-27 16:11:29 -07001711 }
1712 dev = rt->dev;
1713 skb->dev = dev;
1714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 ddp_dl->request(ddp_dl, skb, dev->dev_addr);
1716 } else {
1717 SOCK_DEBUG(sk, "SK %p: send out.\n", sk);
1718 if (rt->flags & RTF_GATEWAY) {
1719 gsat.sat_addr = rt->gateway;
1720 usat = &gsat;
1721 }
1722
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -07001723 /*
1724 * If it fails it is queued/sent above in the aarp queue
1725 */
1726 aarp_send_ddp(dev, skb, &usat->sat_addr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001728 SOCK_DEBUG(sk, "SK %p: Done write (%zd).\n", sk, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001730out:
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001731 release_sock(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001732 return err ? : len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
Ying Xue1b784142015-03-02 15:37:48 +08001735static int atalk_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1736 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 struct ddpehdr *ddp;
1740 int copied = 0;
Al Viro2a50f282006-09-26 21:22:08 -07001741 int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 int err = 0;
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001743 struct sk_buff *skb;
1744
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001745 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 flags & MSG_DONTWAIT, &err);
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001747 lock_sock(sk);
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (!skb)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001750 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752 /* FIXME: use skb->cb to be able to use shared skbs */
1753 ddp = ddp_hdr(skb);
Al Viro2a50f282006-09-26 21:22:08 -07001754 copied = ntohs(ddp->deh_len_hops) & 1023;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Al Viro2a50f282006-09-26 21:22:08 -07001756 if (sk->sk_type != SOCK_RAW) {
1757 offset = sizeof(*ddp);
1758 copied -= offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
1760
Al Viro2a50f282006-09-26 21:22:08 -07001761 if (copied > size) {
1762 copied = size;
1763 msg->msg_flags |= MSG_TRUNC;
1764 }
David S. Miller51f3d022014-11-05 16:46:40 -05001765 err = skb_copy_datagram_msg(skb, offset, msg, copied);
Al Viro2a50f282006-09-26 21:22:08 -07001766
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01001767 if (!err && msg->msg_name) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001768 DECLARE_SOCKADDR(struct sockaddr_at *, sat, msg->msg_name);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01001769 sat->sat_family = AF_APPLETALK;
1770 sat->sat_port = ddp->deh_sport;
1771 sat->sat_addr.s_node = ddp->deh_snode;
1772 sat->sat_addr.s_net = ddp->deh_snet;
1773 msg->msg_namelen = sizeof(*sat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775
1776 skb_free_datagram(sk, skb); /* Free the datagram. */
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001777
1778out:
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001779 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return err ? : copied;
1781}
1782
1783
1784/*
1785 * AppleTalk ioctl calls.
1786 */
1787static int atalk_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1788{
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001789 int rc = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 struct sock *sk = sock->sk;
1791 void __user *argp = (void __user *)arg;
1792
1793 switch (cmd) {
wangweidong7b306002014-02-14 15:43:46 +08001794 /* Protocol layer */
1795 case TIOCOUTQ: {
1796 long amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
wangweidong7b306002014-02-14 15:43:46 +08001798 if (amount < 0)
1799 amount = 0;
1800 rc = put_user(amount, (int __user *)argp);
1801 break;
1802 }
1803 case TIOCINQ: {
1804 /*
1805 * These two are safe on a single CPU system as only
1806 * user tasks fiddle here
1807 */
1808 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
1809 long amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
wangweidong7b306002014-02-14 15:43:46 +08001811 if (skb)
wangweidong63ae8892014-07-04 15:29:48 +08001812 amount = skb->len - sizeof(struct ddpehdr);
wangweidong7b306002014-02-14 15:43:46 +08001813 rc = put_user(amount, (int __user *)argp);
1814 break;
1815 }
wangweidong7b306002014-02-14 15:43:46 +08001816 /* Routing */
1817 case SIOCADDRT:
1818 case SIOCDELRT:
1819 rc = -EPERM;
1820 if (capable(CAP_NET_ADMIN))
1821 rc = atrtr_ioctl(cmd, argp);
1822 break;
1823 /* Interface */
1824 case SIOCGIFADDR:
1825 case SIOCSIFADDR:
1826 case SIOCGIFBRDADDR:
1827 case SIOCATALKDIFADDR:
1828 case SIOCDIFADDR:
1829 case SIOCSARP: /* proxy AARP */
1830 case SIOCDARP: /* proxy AARP */
1831 rtnl_lock();
1832 rc = atif_ioctl(cmd, argp);
1833 rtnl_unlock();
1834 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 }
1836
1837 return rc;
1838}
1839
Petr Vandrovecf6c90b72006-03-27 23:39:31 -08001840
1841#ifdef CONFIG_COMPAT
1842static int atalk_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1843{
1844 /*
Arnd Bergmann20660222009-11-06 08:09:06 +00001845 * SIOCATALKDIFADDR is a SIOCPROTOPRIVATE ioctl number, so we
1846 * cannot handle it in common code. The data we access if ifreq
1847 * here is compatible, so we can simply call the native
1848 * handler.
Petr Vandrovecf6c90b72006-03-27 23:39:31 -08001849 */
Arnd Bergmann20660222009-11-06 08:09:06 +00001850 if (cmd == SIOCATALKDIFADDR)
1851 return atalk_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
1852
Petr Vandrovecf6c90b72006-03-27 23:39:31 -08001853 return -ENOIOCTLCMD;
1854}
1855#endif
1856
1857
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001858static const struct net_proto_family atalk_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 .family = PF_APPLETALK,
1860 .create = atalk_create,
1861 .owner = THIS_MODULE,
1862};
1863
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001864static const struct proto_ops atalk_dgram_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 .family = PF_APPLETALK,
1866 .owner = THIS_MODULE,
1867 .release = atalk_release,
1868 .bind = atalk_bind,
1869 .connect = atalk_connect,
1870 .socketpair = sock_no_socketpair,
1871 .accept = sock_no_accept,
1872 .getname = atalk_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001873 .poll = datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 .ioctl = atalk_ioctl,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001875 .gettstamp = sock_gettstamp,
Petr Vandrovecf6c90b72006-03-27 23:39:31 -08001876#ifdef CONFIG_COMPAT
1877 .compat_ioctl = atalk_compat_ioctl,
1878#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 .listen = sock_no_listen,
1880 .shutdown = sock_no_shutdown,
1881 .setsockopt = sock_no_setsockopt,
1882 .getsockopt = sock_no_getsockopt,
1883 .sendmsg = atalk_sendmsg,
1884 .recvmsg = atalk_recvmsg,
1885 .mmap = sock_no_mmap,
1886 .sendpage = sock_no_sendpage,
1887};
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889static struct notifier_block ddp_notifier = {
1890 .notifier_call = ddp_device_event,
1891};
1892
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001893static struct packet_type ltalk_packet_type __read_mostly = {
Harvey Harrison09640e632009-02-01 00:45:17 -08001894 .type = cpu_to_be16(ETH_P_LOCALTALK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 .func = ltalk_rcv,
1896};
1897
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001898static struct packet_type ppptalk_packet_type __read_mostly = {
Harvey Harrison09640e632009-02-01 00:45:17 -08001899 .type = cpu_to_be16(ETH_P_PPPTALK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 .func = atalk_rcv,
1901};
1902
1903static unsigned char ddp_snap_id[] = { 0x08, 0x00, 0x07, 0x80, 0x9B };
1904
1905/* Export symbols for use by drivers when AppleTalk is a module */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906EXPORT_SYMBOL(atrtr_get_dev);
1907EXPORT_SYMBOL(atalk_find_dev_addr);
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909/* Called by proto.c on kernel start up */
1910static int __init atalk_init(void)
1911{
YueHaibing6377f782019-03-01 10:57:57 +08001912 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
YueHaibing6377f782019-03-01 10:57:57 +08001914 rc = proto_register(&ddp_proto, 0);
1915 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 goto out;
1917
YueHaibing6377f782019-03-01 10:57:57 +08001918 rc = sock_register(&atalk_family_ops);
1919 if (rc)
1920 goto out_proto;
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv);
YueHaibing98045012019-03-14 13:47:59 +08001923 if (!ddp_dl) {
1924 pr_crit("Unable to register DDP with SNAP.\n");
YueHaibingc93ad132019-04-30 19:34:08 +08001925 rc = -ENOMEM;
YueHaibing98045012019-03-14 13:47:59 +08001926 goto out_sock;
1927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 dev_add_pack(&ltalk_packet_type);
1930 dev_add_pack(&ppptalk_packet_type);
1931
YueHaibing6377f782019-03-01 10:57:57 +08001932 rc = register_netdevice_notifier(&ddp_notifier);
1933 if (rc)
YueHaibing98045012019-03-14 13:47:59 +08001934 goto out_snap;
YueHaibing6377f782019-03-01 10:57:57 +08001935
YueHaibing98045012019-03-14 13:47:59 +08001936 rc = aarp_proto_init();
1937 if (rc)
1938 goto out_dev;
1939
YueHaibing6377f782019-03-01 10:57:57 +08001940 rc = atalk_proc_init();
1941 if (rc)
1942 goto out_aarp;
1943
1944 rc = atalk_register_sysctl();
1945 if (rc)
1946 goto out_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947out:
1948 return rc;
YueHaibing6377f782019-03-01 10:57:57 +08001949out_proc:
1950 atalk_proc_exit();
1951out_aarp:
1952 aarp_cleanup_module();
YueHaibing98045012019-03-14 13:47:59 +08001953out_dev:
YueHaibing6377f782019-03-01 10:57:57 +08001954 unregister_netdevice_notifier(&ddp_notifier);
YueHaibing98045012019-03-14 13:47:59 +08001955out_snap:
YueHaibing6377f782019-03-01 10:57:57 +08001956 dev_remove_pack(&ppptalk_packet_type);
1957 dev_remove_pack(&ltalk_packet_type);
1958 unregister_snap_client(ddp_dl);
YueHaibing98045012019-03-14 13:47:59 +08001959out_sock:
YueHaibing6377f782019-03-01 10:57:57 +08001960 sock_unregister(PF_APPLETALK);
1961out_proto:
1962 proto_unregister(&ddp_proto);
1963 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965module_init(atalk_init);
1966
1967/*
1968 * No explicit module reference count manipulation is needed in the
1969 * protocol. Socket layer sets module reference count for us
1970 * and interfaces reference counting is done
1971 * by the network device layer.
1972 *
1973 * Ergo, before the AppleTalk module can be removed, all AppleTalk
1974 * sockets be closed from user space.
1975 */
1976static void __exit atalk_exit(void)
1977{
1978#ifdef CONFIG_SYSCTL
1979 atalk_unregister_sysctl();
1980#endif /* CONFIG_SYSCTL */
1981 atalk_proc_exit();
1982 aarp_cleanup_module(); /* General aarp clean-up. */
1983 unregister_netdevice_notifier(&ddp_notifier);
1984 dev_remove_pack(&ltalk_packet_type);
1985 dev_remove_pack(&ppptalk_packet_type);
1986 unregister_snap_client(ddp_dl);
1987 sock_unregister(PF_APPLETALK);
1988 proto_unregister(&ddp_proto);
1989}
1990module_exit(atalk_exit);
1991
1992MODULE_LICENSE("GPL");
Alan Cox113aa832008-10-13 19:01:08 -07001993MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994MODULE_DESCRIPTION("AppleTalk 0.20\n");
1995MODULE_ALIAS_NETPROTO(PF_APPLETALK);