blob: 93209c009df58f24d62a629050f75e98a0bda888 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * DDP: An implementation of the AppleTalk DDP protocol for
3 * Ethernet 'ELAP'.
4 *
Alan Cox113aa832008-10-13 19:01:08 -07005 * Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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)
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +090031 * Jay Schulist : Made work as module with
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * AppleTalk drivers, cleaned it.
33 * Rob Newberry : Added proxy AARP and AARP
34 * procfs, moved probing to AARP
35 * module.
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +090036 * Adrian Sun/
37 * Michael Zuelsdorff : fix for net.0 packets. don't
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * allow illegal ether/tokentalk
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +090039 * port assignment. we lose a
40 * valid localtalk port as a
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * 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.
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +090051 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
53
Randy Dunlap4fc268d2006-01-11 12:17:47 -080054#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/if_arp.h>
57#include <linux/termios.h> /* For TIOCOUTQ/INQ */
Stephen Rothwell415ce612009-11-08 20:41:03 -080058#include <linux/compat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090059#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <net/datalink.h>
61#include <net/psnap.h>
62#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070063#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <net/route.h>
65#include <linux/atalk.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020066#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68struct datalink_proto *ddp_dl, *aarp_dl;
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080069static const struct proto_ops atalk_dgram_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 read_lock_bh(&atalk_sockets_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -080098 sk_for_each(s, &atalk_sockets) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 struct atalk_sock *at = at_sk(s);
100
101 if (to->sat_port != at->src_port)
102 continue;
103
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900104 if (to->sat_addr.s_net == ATADDR_ANYNET &&
Oliver Dawid64233bf2005-09-27 16:11:29 -0700105 to->sat_addr.s_node == ATADDR_BCAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 goto found;
107
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900108 if (to->sat_addr.s_net == at->src_net &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 (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
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900114 /* XXXX.0 -- we got a request for this router. make sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * 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
Ben Hutchings2c530402012-07-10 10:55:09 +0000131 * @sk: socket to insert in the list if it is not there already
132 * @sat: address to search for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 *
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 struct atalk_sock *at;
144
145 write_lock_bh(&atalk_sockets_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800146 sk_for_each(s, &atalk_sockets) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 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
161static void atalk_destroy_timer(unsigned long data)
162{
163 struct sock *sk = (struct sock *)data;
164
Eric Dumazetc5640392009-06-16 10:12:03 +0000165 if (sk_has_allocations(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 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
Eric Dumazetc5640392009-06-16 10:12:03 +0000177 if (sk_has_allocations(sk)) {
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800178 setup_timer(&sk->sk_timer, atalk_destroy_timer,
179 (unsigned long)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 sk->sk_timer.expires = jiffies + SOCK_DESTROY_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 add_timer(&sk->sk_timer);
182 } else
183 sock_put(sk);
184}
185
186/**************************************************************************\
187* *
188* Routing tables for the AppleTalk socket layer. *
189* *
190\**************************************************************************/
191
192/* Anti-deadlock ordering is atalk_routes_lock --> iface_lock -DaveM */
193struct atalk_route *atalk_routes;
194DEFINE_RWLOCK(atalk_routes_lock);
195
196struct atalk_iface *atalk_interfaces;
197DEFINE_RWLOCK(atalk_interfaces_lock);
198
199/* For probing devices or in a routerless network */
200struct atalk_route atrtr_default;
201
202/* AppleTalk interface control */
203/*
204 * Drop a device. Doesn't drop any of its routes - that is the caller's
205 * problem. Called when we down the interface or delete the address.
206 */
207static void atif_drop_device(struct net_device *dev)
208{
209 struct atalk_iface **iface = &atalk_interfaces;
210 struct atalk_iface *tmp;
211
212 write_lock_bh(&atalk_interfaces_lock);
213 while ((tmp = *iface) != NULL) {
214 if (tmp->dev == dev) {
215 *iface = tmp->next;
216 dev_put(dev);
217 kfree(tmp);
218 dev->atalk_ptr = NULL;
219 } else
220 iface = &tmp->next;
221 }
222 write_unlock_bh(&atalk_interfaces_lock);
223}
224
225static struct atalk_iface *atif_add_device(struct net_device *dev,
226 struct atalk_addr *sa)
227{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700228 struct atalk_iface *iface = kzalloc(sizeof(*iface), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (!iface)
231 goto out;
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 dev_hold(dev);
234 iface->dev = dev;
235 dev->atalk_ptr = iface;
236 iface->address = *sa;
237 iface->status = 0;
238
239 write_lock_bh(&atalk_interfaces_lock);
240 iface->next = atalk_interfaces;
241 atalk_interfaces = iface;
242 write_unlock_bh(&atalk_interfaces_lock);
243out:
244 return iface;
245}
246
247/* Perform phase 2 AARP probing on our tentative address */
248static int atif_probe_device(struct atalk_iface *atif)
249{
250 int netrange = ntohs(atif->nets.nr_lastnet) -
251 ntohs(atif->nets.nr_firstnet) + 1;
252 int probe_net = ntohs(atif->address.s_net);
253 int probe_node = atif->address.s_node;
254 int netct, nodect;
255
256 /* Offset the network we start probing with */
257 if (probe_net == ATADDR_ANYNET) {
258 probe_net = ntohs(atif->nets.nr_firstnet);
259 if (netrange)
260 probe_net += jiffies % netrange;
261 }
262 if (probe_node == ATADDR_ANYNODE)
263 probe_node = jiffies & 0xFF;
264
265 /* Scan the networks */
266 atif->status |= ATIF_PROBE;
267 for (netct = 0; netct <= netrange; netct++) {
268 /* Sweep the available nodes from a given start */
269 atif->address.s_net = htons(probe_net);
270 for (nodect = 0; nodect < 256; nodect++) {
271 atif->address.s_node = (nodect + probe_node) & 0xFF;
272 if (atif->address.s_node > 0 &&
273 atif->address.s_node < 254) {
274 /* Probe a proposed address */
275 aarp_probe_network(atif);
276
277 if (!(atif->status & ATIF_PROBE_FAIL)) {
278 atif->status &= ~ATIF_PROBE;
279 return 0;
280 }
281 }
282 atif->status &= ~ATIF_PROBE_FAIL;
283 }
284 probe_net++;
285 if (probe_net > ntohs(atif->nets.nr_lastnet))
286 probe_net = ntohs(atif->nets.nr_firstnet);
287 }
288 atif->status &= ~ATIF_PROBE;
289
290 return -EADDRINUSE; /* Network is full... */
291}
292
293
294/* Perform AARP probing for a proxy address */
295static int atif_proxy_probe_device(struct atalk_iface *atif,
wangweidong6f0984a2014-02-14 15:43:44 +0800296 struct atalk_addr *proxy_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 int netrange = ntohs(atif->nets.nr_lastnet) -
299 ntohs(atif->nets.nr_firstnet) + 1;
300 /* we probe the interface's network */
301 int probe_net = ntohs(atif->address.s_net);
302 int probe_node = ATADDR_ANYNODE; /* we'll take anything */
303 int netct, nodect;
304
305 /* Offset the network we start probing with */
306 if (probe_net == ATADDR_ANYNET) {
307 probe_net = ntohs(atif->nets.nr_firstnet);
308 if (netrange)
309 probe_net += jiffies % netrange;
310 }
311
312 if (probe_node == ATADDR_ANYNODE)
313 probe_node = jiffies & 0xFF;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /* Scan the networks */
316 for (netct = 0; netct <= netrange; netct++) {
317 /* Sweep the available nodes from a given start */
318 proxy_addr->s_net = htons(probe_net);
319 for (nodect = 0; nodect < 256; nodect++) {
320 proxy_addr->s_node = (nodect + probe_node) & 0xFF;
321 if (proxy_addr->s_node > 0 &&
322 proxy_addr->s_node < 254) {
323 /* Tell AARP to probe a proposed address */
324 int ret = aarp_proxy_probe_network(atif,
325 proxy_addr);
326
327 if (ret != -EADDRINUSE)
328 return ret;
329 }
330 }
331 probe_net++;
332 if (probe_net > ntohs(atif->nets.nr_lastnet))
333 probe_net = ntohs(atif->nets.nr_firstnet);
334 }
335
336 return -EADDRINUSE; /* Network is full... */
337}
338
339
340struct atalk_addr *atalk_find_dev_addr(struct net_device *dev)
341{
342 struct atalk_iface *iface = dev->atalk_ptr;
343 return iface ? &iface->address : NULL;
344}
345
346static struct atalk_addr *atalk_find_primary(void)
347{
348 struct atalk_iface *fiface = NULL;
349 struct atalk_addr *retval;
350 struct atalk_iface *iface;
351
352 /*
353 * Return a point-to-point interface only if
354 * there is no non-ptp interface available.
355 */
356 read_lock_bh(&atalk_interfaces_lock);
357 for (iface = atalk_interfaces; iface; iface = iface->next) {
358 if (!fiface && !(iface->dev->flags & IFF_LOOPBACK))
359 fiface = iface;
360 if (!(iface->dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
361 retval = &iface->address;
362 goto out;
363 }
364 }
365
366 if (fiface)
367 retval = &fiface->address;
368 else if (atalk_interfaces)
369 retval = &atalk_interfaces->address;
370 else
371 retval = NULL;
372out:
373 read_unlock_bh(&atalk_interfaces_lock);
374 return retval;
375}
376
377/*
378 * Find a match for 'any network' - ie any of our interfaces with that
379 * node number will do just nicely.
380 */
381static struct atalk_iface *atalk_find_anynet(int node, struct net_device *dev)
382{
383 struct atalk_iface *iface = dev->atalk_ptr;
384
385 if (!iface || iface->status & ATIF_PROBE)
386 goto out_err;
387
388 if (node != ATADDR_BCAST &&
389 iface->address.s_node != node &&
390 node != ATADDR_ANYNODE)
391 goto out_err;
392out:
393 return iface;
394out_err:
395 iface = NULL;
396 goto out;
397}
398
399/* Find a match for a specific network:node pair */
Alexey Dobriyanf6e276ee2005-06-20 13:32:05 -0700400static struct atalk_iface *atalk_find_interface(__be16 net, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 struct atalk_iface *iface;
403
404 read_lock_bh(&atalk_interfaces_lock);
405 for (iface = atalk_interfaces; iface; iface = iface->next) {
406 if ((node == ATADDR_BCAST ||
407 node == ATADDR_ANYNODE ||
408 iface->address.s_node == node) &&
409 iface->address.s_net == net &&
410 !(iface->status & ATIF_PROBE))
411 break;
412
413 /* XXXX.0 -- net.0 returns the iface associated with net */
414 if (node == ATADDR_ANYNODE && net != ATADDR_ANYNET &&
415 ntohs(iface->nets.nr_firstnet) <= ntohs(net) &&
416 ntohs(net) <= ntohs(iface->nets.nr_lastnet))
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900417 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419 read_unlock_bh(&atalk_interfaces_lock);
420 return iface;
421}
422
423
424/*
425 * Find a route for an AppleTalk packet. This ought to get cached in
426 * the socket (later on...). We know about host routes and the fact
427 * that a route must be direct to broadcast.
428 */
429static struct atalk_route *atrtr_find(struct atalk_addr *target)
430{
431 /*
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900432 * we must search through all routes unless we find a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * host route, because some host routes might overlap
434 * network routes
435 */
436 struct atalk_route *net_route = NULL;
437 struct atalk_route *r;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 read_lock_bh(&atalk_routes_lock);
440 for (r = atalk_routes; r; r = r->next) {
441 if (!(r->flags & RTF_UP))
442 continue;
443
444 if (r->target.s_net == target->s_net) {
445 if (r->flags & RTF_HOST) {
446 /*
447 * if this host route is for the target,
448 * the we're done
449 */
450 if (r->target.s_node == target->s_node)
451 goto out;
452 } else
453 /*
454 * this route will work if there isn't a
455 * direct host route, so cache it
456 */
457 net_route = r;
458 }
459 }
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900460
461 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 * if we found a network route but not a direct host
463 * route, then return it
464 */
465 if (net_route)
466 r = net_route;
467 else if (atrtr_default.dev)
468 r = &atrtr_default;
469 else /* No route can be found */
470 r = NULL;
471out:
472 read_unlock_bh(&atalk_routes_lock);
473 return r;
474}
475
476
477/*
478 * Given an AppleTalk network, find the device to use. This can be
479 * a simple lookup.
480 */
481struct net_device *atrtr_get_dev(struct atalk_addr *sa)
482{
483 struct atalk_route *atr = atrtr_find(sa);
484 return atr ? atr->dev : NULL;
485}
486
487/* Set up a default router */
488static void atrtr_set_default(struct net_device *dev)
489{
490 atrtr_default.dev = dev;
491 atrtr_default.flags = RTF_UP;
492 atrtr_default.gateway.s_net = htons(0);
493 atrtr_default.gateway.s_node = 0;
494}
495
496/*
497 * Add a router. Basically make sure it looks valid and stuff the
498 * entry in the list. While it uses netranges we always set them to one
499 * entry to work like netatalk.
500 */
501static int atrtr_create(struct rtentry *r, struct net_device *devhint)
502{
503 struct sockaddr_at *ta = (struct sockaddr_at *)&r->rt_dst;
504 struct sockaddr_at *ga = (struct sockaddr_at *)&r->rt_gateway;
505 struct atalk_route *rt;
506 struct atalk_iface *iface, *riface;
507 int retval = -EINVAL;
508
509 /*
510 * Fixme: Raise/Lower a routing change semaphore for these
511 * operations.
512 */
513
514 /* Validate the request */
515 if (ta->sat_family != AF_APPLETALK ||
516 (!devhint && ga->sat_family != AF_APPLETALK))
517 goto out;
518
519 /* Now walk the routing table and make our decisions */
520 write_lock_bh(&atalk_routes_lock);
521 for (rt = atalk_routes; rt; rt = rt->next) {
522 if (r->rt_flags != rt->flags)
523 continue;
524
525 if (ta->sat_addr.s_net == rt->target.s_net) {
526 if (!(rt->flags & RTF_HOST))
527 break;
528 if (ta->sat_addr.s_node == rt->target.s_node)
529 break;
530 }
531 }
532
533 if (!devhint) {
534 riface = NULL;
535
536 read_lock_bh(&atalk_interfaces_lock);
537 for (iface = atalk_interfaces; iface; iface = iface->next) {
538 if (!riface &&
539 ntohs(ga->sat_addr.s_net) >=
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900540 ntohs(iface->nets.nr_firstnet) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 ntohs(ga->sat_addr.s_net) <=
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900542 ntohs(iface->nets.nr_lastnet))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 riface = iface;
544
545 if (ga->sat_addr.s_net == iface->address.s_net &&
546 ga->sat_addr.s_node == iface->address.s_node)
547 riface = iface;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 read_unlock_bh(&atalk_interfaces_lock);
550
551 retval = -ENETUNREACH;
552 if (!riface)
553 goto out_unlock;
554
555 devhint = riface->dev;
556 }
557
558 if (!rt) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700559 rt = kzalloc(sizeof(*rt), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 retval = -ENOBUFS;
562 if (!rt)
563 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 rt->next = atalk_routes;
566 atalk_routes = rt;
567 }
568
569 /* Fill in the routing entry */
570 rt->target = ta->sat_addr;
Herbert Xuc7f905f02005-04-19 22:44:17 -0700571 dev_hold(devhint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 rt->dev = devhint;
573 rt->flags = r->rt_flags;
574 rt->gateway = ga->sat_addr;
575
576 retval = 0;
577out_unlock:
578 write_unlock_bh(&atalk_routes_lock);
579out:
580 return retval;
581}
582
583/* Delete a route. Find it and discard it */
wangweidong6f0984a2014-02-14 15:43:44 +0800584static int atrtr_delete(struct atalk_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 struct atalk_route **r = &atalk_routes;
587 int retval = 0;
588 struct atalk_route *tmp;
589
590 write_lock_bh(&atalk_routes_lock);
591 while ((tmp = *r) != NULL) {
592 if (tmp->target.s_net == addr->s_net &&
593 (!(tmp->flags&RTF_GATEWAY) ||
594 tmp->target.s_node == addr->s_node)) {
595 *r = tmp->next;
596 dev_put(tmp->dev);
597 kfree(tmp);
598 goto out;
599 }
600 r = &tmp->next;
601 }
602 retval = -ENOENT;
603out:
604 write_unlock_bh(&atalk_routes_lock);
605 return retval;
606}
607
608/*
609 * Called when a device is downed. Just throw away any routes
610 * via it.
611 */
612static void atrtr_device_down(struct net_device *dev)
613{
614 struct atalk_route **r = &atalk_routes;
615 struct atalk_route *tmp;
616
617 write_lock_bh(&atalk_routes_lock);
618 while ((tmp = *r) != NULL) {
619 if (tmp->dev == dev) {
620 *r = tmp->next;
621 dev_put(dev);
622 kfree(tmp);
623 } else
624 r = &tmp->next;
625 }
626 write_unlock_bh(&atalk_routes_lock);
627
628 if (atrtr_default.dev == dev)
629 atrtr_set_default(NULL);
630}
631
632/* Actually down the interface */
633static inline void atalk_dev_down(struct net_device *dev)
634{
635 atrtr_device_down(dev); /* Remove all routes for the device */
636 aarp_device_down(dev); /* Remove AARP entries for the device */
637 atif_drop_device(dev); /* Remove the device */
638}
639
640/*
641 * A device event has occurred. Watch for devices going down and
642 * delete our use of them (iface and route).
643 */
644static int ddp_device_event(struct notifier_block *this, unsigned long event,
645 void *ptr)
646{
Jiri Pirko351638e2013-05-28 01:30:21 +0000647 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biederman890d52d2007-09-12 11:26:59 +0200648
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700649 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200650 return NOTIFY_DONE;
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (event == NETDEV_DOWN)
653 /* Discard any use of this */
Eric W. Biederman890d52d2007-09-12 11:26:59 +0200654 atalk_dev_down(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 return NOTIFY_DONE;
657}
658
659/* ioctl calls. Shouldn't even need touching */
660/* Device configuration ioctl calls */
661static int atif_ioctl(int cmd, void __user *arg)
662{
663 static char aarp_mcast[6] = { 0x09, 0x00, 0x00, 0xFF, 0xFF, 0xFF };
664 struct ifreq atreq;
665 struct atalk_netrange *nr;
666 struct sockaddr_at *sa;
667 struct net_device *dev;
668 struct atalk_iface *atif;
669 int ct;
670 int limit;
671 struct rtentry rtdef;
672 int add_route;
673
674 if (copy_from_user(&atreq, arg, sizeof(atreq)))
675 return -EFAULT;
676
Eric W. Biederman881d9662007-09-17 11:56:21 -0700677 dev = __dev_get_by_name(&init_net, atreq.ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (!dev)
679 return -ENODEV;
680
681 sa = (struct sockaddr_at *)&atreq.ifr_addr;
682 atif = atalk_find_dev(dev);
683
684 switch (cmd) {
Joe Perches4a9e4b02011-07-01 09:43:02 +0000685 case SIOCSIFADDR:
686 if (!capable(CAP_NET_ADMIN))
687 return -EPERM;
688 if (sa->sat_family != AF_APPLETALK)
689 return -EINVAL;
690 if (dev->type != ARPHRD_ETHER &&
691 dev->type != ARPHRD_LOOPBACK &&
692 dev->type != ARPHRD_LOCALTLK &&
693 dev->type != ARPHRD_PPP)
694 return -EPROTONOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Joe Perches4a9e4b02011-07-01 09:43:02 +0000696 nr = (struct atalk_netrange *)&sa->sat_zero[0];
697 add_route = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Joe Perches4a9e4b02011-07-01 09:43:02 +0000699 /*
700 * if this is a point-to-point iface, and we already
701 * have an iface for this AppleTalk address, then we
702 * should not add a route
703 */
704 if ((dev->flags & IFF_POINTOPOINT) &&
705 atalk_find_interface(sa->sat_addr.s_net,
706 sa->sat_addr.s_node)) {
707 printk(KERN_DEBUG "AppleTalk: point-to-point "
708 "interface added with "
709 "existing address\n");
710 add_route = 0;
711 }
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900712
Joe Perches4a9e4b02011-07-01 09:43:02 +0000713 /*
714 * Phase 1 is fine on LocalTalk but we don't do
715 * EtherTalk phase 1. Anyone wanting to add it go ahead.
716 */
717 if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
718 return -EPROTONOSUPPORT;
719 if (sa->sat_addr.s_node == ATADDR_BCAST ||
720 sa->sat_addr.s_node == 254)
721 return -EINVAL;
722 if (atif) {
723 /* Already setting address */
724 if (atif->status & ATIF_PROBE)
725 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Joe Perches4a9e4b02011-07-01 09:43:02 +0000727 atif->address.s_net = sa->sat_addr.s_net;
728 atif->address.s_node = sa->sat_addr.s_node;
729 atrtr_device_down(dev); /* Flush old routes */
730 } else {
731 atif = atif_add_device(dev, &sa->sat_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (!atif)
Joe Perches4a9e4b02011-07-01 09:43:02 +0000733 return -ENOMEM;
734 }
735 atif->nets = *nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Joe Perches4a9e4b02011-07-01 09:43:02 +0000737 /*
738 * Check if the chosen address is used. If so we
739 * error and atalkd will try another.
740 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Joe Perches4a9e4b02011-07-01 09:43:02 +0000742 if (!(dev->flags & IFF_LOOPBACK) &&
743 !(dev->flags & IFF_POINTOPOINT) &&
744 atif_probe_device(atif) < 0) {
745 atif_drop_device(dev);
746 return -EADDRINUSE;
747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Joe Perches4a9e4b02011-07-01 09:43:02 +0000749 /* Hey it worked - add the direct routes */
750 sa = (struct sockaddr_at *)&rtdef.rt_gateway;
751 sa->sat_family = AF_APPLETALK;
752 sa->sat_addr.s_net = atif->address.s_net;
753 sa->sat_addr.s_node = atif->address.s_node;
754 sa = (struct sockaddr_at *)&rtdef.rt_dst;
755 rtdef.rt_flags = RTF_UP;
756 sa->sat_family = AF_APPLETALK;
757 sa->sat_addr.s_node = ATADDR_ANYNODE;
758 if (dev->flags & IFF_LOOPBACK ||
759 dev->flags & IFF_POINTOPOINT)
760 rtdef.rt_flags |= RTF_HOST;
761
762 /* Routerless initial state */
763 if (nr->nr_firstnet == htons(0) &&
764 nr->nr_lastnet == htons(0xFFFE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 sa->sat_addr.s_net = atif->address.s_net;
Joe Perches4a9e4b02011-07-01 09:43:02 +0000766 atrtr_create(&rtdef, dev);
767 atrtr_set_default(dev);
768 } else {
769 limit = ntohs(nr->nr_lastnet);
770 if (limit - ntohs(nr->nr_firstnet) > 4096) {
771 printk(KERN_WARNING "Too many routes/"
772 "iface.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return -EINVAL;
Joe Perches4a9e4b02011-07-01 09:43:02 +0000774 }
775 if (add_route)
776 for (ct = ntohs(nr->nr_firstnet);
777 ct <= limit; ct++) {
778 sa->sat_addr.s_net = htons(ct);
779 atrtr_create(&rtdef, dev);
780 }
781 }
782 dev_mc_add_global(dev, aarp_mcast);
783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Joe Perches4a9e4b02011-07-01 09:43:02 +0000785 case SIOCGIFADDR:
786 if (!atif)
787 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Joe Perches4a9e4b02011-07-01 09:43:02 +0000789 sa->sat_family = AF_APPLETALK;
790 sa->sat_addr = atif->address;
791 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Joe Perches4a9e4b02011-07-01 09:43:02 +0000793 case SIOCGIFBRDADDR:
794 if (!atif)
795 return -EADDRNOTAVAIL;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900796
Joe Perches4a9e4b02011-07-01 09:43:02 +0000797 sa->sat_family = AF_APPLETALK;
798 sa->sat_addr.s_net = atif->address.s_net;
799 sa->sat_addr.s_node = ATADDR_BCAST;
800 break;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900801
Joe Perches4a9e4b02011-07-01 09:43:02 +0000802 case SIOCATALKDIFADDR:
803 case SIOCDIFADDR:
804 if (!capable(CAP_NET_ADMIN))
805 return -EPERM;
806 if (sa->sat_family != AF_APPLETALK)
807 return -EINVAL;
808 atalk_dev_down(dev);
809 break;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900810
Joe Perches4a9e4b02011-07-01 09:43:02 +0000811 case SIOCSARP:
812 if (!capable(CAP_NET_ADMIN))
813 return -EPERM;
814 if (sa->sat_family != AF_APPLETALK)
815 return -EINVAL;
816 /*
817 * for now, we only support proxy AARP on ELAP;
818 * we should be able to do it for LocalTalk, too.
819 */
820 if (dev->type != ARPHRD_ETHER)
821 return -EPROTONOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Joe Perches4a9e4b02011-07-01 09:43:02 +0000823 /*
824 * atif points to the current interface on this network;
825 * we aren't concerned about its current status (at
826 * least for now), but it has all the settings about
827 * the network we're going to probe. Consequently, it
828 * must exist.
829 */
830 if (!atif)
831 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Joe Perches4a9e4b02011-07-01 09:43:02 +0000833 nr = (struct atalk_netrange *)&(atif->nets);
834 /*
835 * Phase 1 is fine on Localtalk but we don't do
836 * Ethertalk phase 1. Anyone wanting to add it go ahead.
837 */
838 if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
839 return -EPROTONOSUPPORT;
840
841 if (sa->sat_addr.s_node == ATADDR_BCAST ||
842 sa->sat_addr.s_node == 254)
843 return -EINVAL;
844
845 /*
846 * Check if the chosen address is used. If so we
847 * error and ATCP will try another.
848 */
849 if (atif_proxy_probe_device(atif, &(sa->sat_addr)) < 0)
850 return -EADDRINUSE;
851
852 /*
853 * We now have an address on the local network, and
854 * the AARP code will defend it for us until we take it
855 * down. We don't set up any routes right now, because
856 * ATCP will install them manually via SIOCADDRT.
857 */
858 break;
859
860 case SIOCDARP:
861 if (!capable(CAP_NET_ADMIN))
862 return -EPERM;
863 if (sa->sat_family != AF_APPLETALK)
864 return -EINVAL;
865 if (!atif)
866 return -EADDRNOTAVAIL;
867
868 /* give to aarp module to remove proxy entry */
869 aarp_proxy_remove(atif->dev, &(sa->sat_addr));
870 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872
873 return copy_to_user(arg, &atreq, sizeof(atreq)) ? -EFAULT : 0;
874}
875
876/* Routing ioctl() calls */
877static int atrtr_ioctl(unsigned int cmd, void __user *arg)
878{
879 struct rtentry rt;
880
881 if (copy_from_user(&rt, arg, sizeof(rt)))
882 return -EFAULT;
883
884 switch (cmd) {
Joe Perches4a9e4b02011-07-01 09:43:02 +0000885 case SIOCDELRT:
886 if (rt.rt_dst.sa_family != AF_APPLETALK)
887 return -EINVAL;
888 return atrtr_delete(&((struct sockaddr_at *)
889 &rt.rt_dst)->sat_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Joe Perches4a9e4b02011-07-01 09:43:02 +0000891 case SIOCADDRT: {
892 struct net_device *dev = NULL;
893 if (rt.rt_dev) {
894 char name[IFNAMSIZ];
895 if (copy_from_user(name, rt.rt_dev, IFNAMSIZ-1))
896 return -EFAULT;
897 name[IFNAMSIZ-1] = '\0';
898 dev = __dev_get_by_name(&init_net, name);
899 if (!dev)
900 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
Joe Perches4a9e4b02011-07-01 09:43:02 +0000902 return atrtr_create(&rt, dev);
903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905 return -EINVAL;
906}
907
908/**************************************************************************\
909* *
910* Handling for system calls applied via the various interfaces to an *
911* AppleTalk socket object. *
912* *
913\**************************************************************************/
914
915/*
916 * Checksum: This is 'optional'. It's quite likely also a good
917 * candidate for assembler hackery 8)
918 */
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900919static unsigned long atalk_sum_partial(const unsigned char *data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 int len, unsigned long sum)
921{
922 /* This ought to be unwrapped neatly. I'll trust gcc for now */
923 while (len--) {
Joe Perchesf7a3a1d2009-11-04 10:26:13 +0000924 sum += *data++;
925 sum = rol16(sum, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927 return sum;
928}
929
930/* Checksum skb data -- similar to skb_checksum */
931static unsigned long atalk_sum_skb(const struct sk_buff *skb, int offset,
932 int len, unsigned long sum)
933{
David S. Miller1a028e52007-04-27 15:21:23 -0700934 int start = skb_headlen(skb);
David S. Millerc32ba3f2009-06-09 00:17:44 -0700935 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int i, copy;
937
938 /* checksum stuff in header space */
wangweidongfd1dc262014-02-14 15:43:43 +0800939 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (copy > len)
941 copy = len;
942 sum = atalk_sum_partial(skb->data + offset, copy, sum);
wangweidongfd1dc262014-02-14 15:43:43 +0800943 if ((len -= copy) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return sum;
945
946 offset += copy;
947 }
948
949 /* checksum stuff in frags */
950 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -0700951 int end;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000952 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700953 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -0700954
Eric Dumazet9e903e02011-10-18 21:00:24 +0000955 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if ((copy = end - offset) > 0) {
957 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 if (copy > len)
960 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +0200961 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -0700962 sum = atalk_sum_partial(vaddr + frag->page_offset +
963 offset - start, copy, sum);
Eric Dumazet51c56b02012-04-05 11:35:15 +0200964 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (!(len -= copy))
967 return sum;
968 offset += copy;
969 }
David S. Miller1a028e52007-04-27 15:21:23 -0700970 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972
David S. Millerc32ba3f2009-06-09 00:17:44 -0700973 skb_walk_frags(skb, frag_iter) {
974 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
David S. Millerc32ba3f2009-06-09 00:17:44 -0700976 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
David S. Millerc32ba3f2009-06-09 00:17:44 -0700978 end = start + frag_iter->len;
979 if ((copy = end - offset) > 0) {
980 if (copy > len)
981 copy = len;
982 sum = atalk_sum_skb(frag_iter, offset - start,
983 copy, sum);
984 if ((len -= copy) == 0)
985 return sum;
986 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
David S. Millerc32ba3f2009-06-09 00:17:44 -0700988 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990
991 BUG_ON(len > 0);
992
993 return sum;
994}
995
Al Viro2a50f282006-09-26 21:22:08 -0700996static __be16 atalk_checksum(const struct sk_buff *skb, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 unsigned long sum;
999
1000 /* skip header 4 bytes */
1001 sum = atalk_sum_skb(skb, 4, len-4, 0);
1002
1003 /* Use 0xFFFF for 0. 0 itself means none */
Al Viro2a50f282006-09-26 21:22:08 -07001004 return sum ? htons((unsigned short)sum) : htons(0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
1007static struct proto ddp_proto = {
1008 .name = "DDP",
1009 .owner = THIS_MODULE,
1010 .obj_size = sizeof(struct atalk_sock),
1011};
1012
1013/*
1014 * Create a socket. Initialise the socket, blank the addresses
1015 * set the state.
1016 */
Eric Paris3f378b62009-11-05 22:18:14 -08001017static int atalk_create(struct net *net, struct socket *sock, int protocol,
1018 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 struct sock *sk;
1021 int rc = -ESOCKTNOSUPPORT;
1022
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001023 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001024 return -EAFNOSUPPORT;
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /*
1027 * We permit SOCK_DGRAM and RAW is an extension. It is trivial to do
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001028 * and gives you the full ELAP frame. Should be handy for CAP 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 */
1030 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
1031 goto out;
Ori Nimron08d2af92019-09-20 09:35:46 +02001032
1033 rc = -EPERM;
1034 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1035 goto out;
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 rc = -ENOMEM;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001038 sk = sk_alloc(net, PF_APPLETALK, GFP_KERNEL, &ddp_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (!sk)
1040 goto out;
1041 rc = 0;
1042 sock->ops = &atalk_dgram_ops;
1043 sock_init_data(sock, sk);
1044
1045 /* Checksums on by default */
1046 sock_set_flag(sk, SOCK_ZAPPED);
1047out:
1048 return rc;
1049}
1050
1051/* Free a socket. No work needed */
1052static int atalk_release(struct socket *sock)
1053{
1054 struct sock *sk = sock->sk;
1055
1056 if (sk) {
David S. Millerc100c8f2011-03-31 18:59:10 -07001057 sock_hold(sk);
1058 lock_sock(sk);
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 sock_orphan(sk);
1061 sock->sk = NULL;
1062 atalk_destroy_socket(sk);
Arnd Bergmannb20e7bb2011-03-21 18:18:00 -07001063
David S. Millerc100c8f2011-03-31 18:59:10 -07001064 release_sock(sk);
1065 sock_put(sk);
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return 0;
1068}
1069
1070/**
1071 * atalk_pick_and_bind_port - Pick a source port when one is not given
Ben Hutchings2c530402012-07-10 10:55:09 +00001072 * @sk: socket to insert into the tables
1073 * @sat: address to search for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *
1075 * Pick a source port when one is not given. If we can find a suitable free
1076 * one, we insert the socket into the tables using it.
1077 *
1078 * This whole operation must be atomic.
1079 */
1080static int atalk_pick_and_bind_port(struct sock *sk, struct sockaddr_at *sat)
1081{
1082 int retval;
1083
1084 write_lock_bh(&atalk_sockets_lock);
1085
1086 for (sat->sat_port = ATPORT_RESERVED;
1087 sat->sat_port < ATPORT_LAST;
1088 sat->sat_port++) {
1089 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Sasha Levinb67bfe02013-02-27 17:06:00 -08001091 sk_for_each(s, &atalk_sockets) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 struct atalk_sock *at = at_sk(s);
1093
1094 if (at->src_net == sat->sat_addr.s_net &&
1095 at->src_node == sat->sat_addr.s_node &&
1096 at->src_port == sat->sat_port)
1097 goto try_next_port;
1098 }
1099
1100 /* Wheee, it's free, assign and insert. */
1101 __atalk_insert_socket(sk);
1102 at_sk(sk)->src_port = sat->sat_port;
1103 retval = 0;
1104 goto out;
1105
1106try_next_port:;
1107 }
1108
1109 retval = -EBUSY;
1110out:
1111 write_unlock_bh(&atalk_sockets_lock);
1112 return retval;
1113}
1114
1115static int atalk_autobind(struct sock *sk)
1116{
1117 struct atalk_sock *at = at_sk(sk);
1118 struct sockaddr_at sat;
1119 struct atalk_addr *ap = atalk_find_primary();
1120 int n = -EADDRNOTAVAIL;
1121
1122 if (!ap || ap->s_net == htons(ATADDR_ANYNET))
1123 goto out;
1124
1125 at->src_net = sat.sat_addr.s_net = ap->s_net;
1126 at->src_node = sat.sat_addr.s_node = ap->s_node;
1127
1128 n = atalk_pick_and_bind_port(sk, &sat);
1129 if (!n)
1130 sock_reset_flag(sk, SOCK_ZAPPED);
1131out:
1132 return n;
1133}
1134
1135/* Set the address 'our end' of the connection */
1136static int atalk_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1137{
1138 struct sockaddr_at *addr = (struct sockaddr_at *)uaddr;
1139 struct sock *sk = sock->sk;
1140 struct atalk_sock *at = at_sk(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001141 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 if (!sock_flag(sk, SOCK_ZAPPED) ||
1144 addr_len != sizeof(struct sockaddr_at))
1145 return -EINVAL;
1146
1147 if (addr->sat_family != AF_APPLETALK)
1148 return -EAFNOSUPPORT;
1149
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001150 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (addr->sat_addr.s_net == htons(ATADDR_ANYNET)) {
1152 struct atalk_addr *ap = atalk_find_primary();
1153
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001154 err = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (!ap)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001156 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 at->src_net = addr->sat_addr.s_net = ap->s_net;
wangweidongfd1dc262014-02-14 15:43:43 +08001159 at->src_node = addr->sat_addr.s_node = ap->s_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 } else {
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001161 err = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (!atalk_find_interface(addr->sat_addr.s_net,
1163 addr->sat_addr.s_node))
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001164 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 at->src_net = addr->sat_addr.s_net;
1167 at->src_node = addr->sat_addr.s_node;
1168 }
1169
1170 if (addr->sat_port == ATADDR_ANYPORT) {
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001171 err = atalk_pick_and_bind_port(sk, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001173 if (err < 0)
1174 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 } else {
1176 at->src_port = addr->sat_port;
1177
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001178 err = -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (atalk_find_or_insert_socket(sk, addr))
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001180 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182
1183 sock_reset_flag(sk, SOCK_ZAPPED);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001184 err = 0;
1185out:
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001186 release_sock(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001187 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
1190/* Set the address we talk to */
1191static int atalk_connect(struct socket *sock, struct sockaddr *uaddr,
1192 int addr_len, int flags)
1193{
1194 struct sock *sk = sock->sk;
1195 struct atalk_sock *at = at_sk(sk);
1196 struct sockaddr_at *addr;
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001197 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 sk->sk_state = TCP_CLOSE;
1200 sock->state = SS_UNCONNECTED;
1201
1202 if (addr_len != sizeof(*addr))
1203 return -EINVAL;
1204
1205 addr = (struct sockaddr_at *)uaddr;
1206
1207 if (addr->sat_family != AF_APPLETALK)
1208 return -EAFNOSUPPORT;
1209
1210 if (addr->sat_addr.s_node == ATADDR_BCAST &&
1211 !sock_flag(sk, SOCK_BROADCAST)) {
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001212#if 1
Dave Jones278f0152012-06-06 08:45:59 +00001213 pr_warn("atalk_connect: %s is broken and did not set SO_BROADCAST.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 current->comm);
1215#else
1216 return -EACCES;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001220 lock_sock(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001221 err = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (sock_flag(sk, SOCK_ZAPPED))
1223 if (atalk_autobind(sk) < 0)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001224 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001226 err = -ENETUNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (!atrtr_get_dev(&addr->sat_addr))
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001228 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 at->dest_port = addr->sat_port;
1231 at->dest_net = addr->sat_addr.s_net;
1232 at->dest_node = addr->sat_addr.s_node;
1233
1234 sock->state = SS_CONNECTED;
1235 sk->sk_state = TCP_ESTABLISHED;
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001236 err = 0;
1237out:
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001238 release_sock(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001239 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
1242/*
1243 * Find the name of an AppleTalk socket. Just copy the right
1244 * fields into the sockaddr.
1245 */
1246static int atalk_getname(struct socket *sock, struct sockaddr *uaddr,
1247 int *uaddr_len, int peer)
1248{
1249 struct sockaddr_at sat;
1250 struct sock *sk = sock->sk;
1251 struct atalk_sock *at = at_sk(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001252 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001254 lock_sock(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001255 err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (sock_flag(sk, SOCK_ZAPPED))
1257 if (atalk_autobind(sk) < 0)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001258 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 *uaddr_len = sizeof(struct sockaddr_at);
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
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001277 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 sat.sat_family = AF_APPLETALK;
1279 memcpy(uaddr, &sat, sizeof(sat));
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 */
1537 ddp = (struct ddpehdr *) skb_push(skb, sizeof(*ddp) - 4);
1538
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
1655 ddp = (struct ddpehdr *)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
1664 SOCK_DEBUG(sk, "SK %p: Copy user data (%Zd bytes).\n", sk, len);
1665
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 }
1728 SOCK_DEBUG(sk, "SK %p: Done write (%Zd).\n", sk, len);
1729
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 }
1816 case SIOCGSTAMP:
1817 rc = sock_get_timestamp(sk, argp);
1818 break;
1819 case SIOCGSTAMPNS:
1820 rc = sock_get_timestampns(sk, argp);
1821 break;
1822 /* Routing */
1823 case SIOCADDRT:
1824 case SIOCDELRT:
1825 rc = -EPERM;
1826 if (capable(CAP_NET_ADMIN))
1827 rc = atrtr_ioctl(cmd, argp);
1828 break;
1829 /* Interface */
1830 case SIOCGIFADDR:
1831 case SIOCSIFADDR:
1832 case SIOCGIFBRDADDR:
1833 case SIOCATALKDIFADDR:
1834 case SIOCDIFADDR:
1835 case SIOCSARP: /* proxy AARP */
1836 case SIOCDARP: /* proxy AARP */
1837 rtnl_lock();
1838 rc = atif_ioctl(cmd, argp);
1839 rtnl_unlock();
1840 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842
1843 return rc;
1844}
1845
Petr Vandrovecf6c90b72006-03-27 23:39:31 -08001846
1847#ifdef CONFIG_COMPAT
1848static int atalk_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1849{
1850 /*
Arnd Bergmann20660222009-11-06 08:09:06 +00001851 * SIOCATALKDIFADDR is a SIOCPROTOPRIVATE ioctl number, so we
1852 * cannot handle it in common code. The data we access if ifreq
1853 * here is compatible, so we can simply call the native
1854 * handler.
Petr Vandrovecf6c90b72006-03-27 23:39:31 -08001855 */
Arnd Bergmann20660222009-11-06 08:09:06 +00001856 if (cmd == SIOCATALKDIFADDR)
1857 return atalk_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
1858
Petr Vandrovecf6c90b72006-03-27 23:39:31 -08001859 return -ENOIOCTLCMD;
1860}
1861#endif
1862
1863
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001864static const struct net_proto_family atalk_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 .family = PF_APPLETALK,
1866 .create = atalk_create,
1867 .owner = THIS_MODULE,
1868};
1869
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001870static const struct proto_ops atalk_dgram_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 .family = PF_APPLETALK,
1872 .owner = THIS_MODULE,
1873 .release = atalk_release,
1874 .bind = atalk_bind,
1875 .connect = atalk_connect,
1876 .socketpair = sock_no_socketpair,
1877 .accept = sock_no_accept,
1878 .getname = atalk_getname,
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001879 .poll = datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 .ioctl = atalk_ioctl,
Petr Vandrovecf6c90b72006-03-27 23:39:31 -08001881#ifdef CONFIG_COMPAT
1882 .compat_ioctl = atalk_compat_ioctl,
1883#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 .listen = sock_no_listen,
1885 .shutdown = sock_no_shutdown,
1886 .setsockopt = sock_no_setsockopt,
1887 .getsockopt = sock_no_getsockopt,
1888 .sendmsg = atalk_sendmsg,
1889 .recvmsg = atalk_recvmsg,
1890 .mmap = sock_no_mmap,
1891 .sendpage = sock_no_sendpage,
1892};
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894static struct notifier_block ddp_notifier = {
1895 .notifier_call = ddp_device_event,
1896};
1897
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001898static struct packet_type ltalk_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08001899 .type = cpu_to_be16(ETH_P_LOCALTALK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 .func = ltalk_rcv,
1901};
1902
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001903static struct packet_type ppptalk_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08001904 .type = cpu_to_be16(ETH_P_PPPTALK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 .func = atalk_rcv,
1906};
1907
1908static unsigned char ddp_snap_id[] = { 0x08, 0x00, 0x07, 0x80, 0x9B };
1909
1910/* Export symbols for use by drivers when AppleTalk is a module */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911EXPORT_SYMBOL(atrtr_get_dev);
1912EXPORT_SYMBOL(atalk_find_dev_addr);
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914/* Called by proto.c on kernel start up */
1915static int __init atalk_init(void)
1916{
YueHaibing057a0da2019-03-01 10:57:57 +08001917 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
YueHaibing057a0da2019-03-01 10:57:57 +08001919 rc = proto_register(&ddp_proto, 0);
1920 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 goto out;
1922
YueHaibing057a0da2019-03-01 10:57:57 +08001923 rc = sock_register(&atalk_family_ops);
1924 if (rc)
1925 goto out_proto;
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv);
YueHaibing540b3412019-03-14 13:47:59 +08001928 if (!ddp_dl) {
1929 pr_crit("Unable to register DDP with SNAP.\n");
YueHaibinga2f50a52019-04-30 19:34:08 +08001930 rc = -ENOMEM;
YueHaibing540b3412019-03-14 13:47:59 +08001931 goto out_sock;
1932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 dev_add_pack(&ltalk_packet_type);
1935 dev_add_pack(&ppptalk_packet_type);
1936
YueHaibing057a0da2019-03-01 10:57:57 +08001937 rc = register_netdevice_notifier(&ddp_notifier);
1938 if (rc)
YueHaibing540b3412019-03-14 13:47:59 +08001939 goto out_snap;
YueHaibing057a0da2019-03-01 10:57:57 +08001940
YueHaibing540b3412019-03-14 13:47:59 +08001941 rc = aarp_proto_init();
1942 if (rc)
1943 goto out_dev;
1944
YueHaibing057a0da2019-03-01 10:57:57 +08001945 rc = atalk_proc_init();
1946 if (rc)
1947 goto out_aarp;
1948
1949 rc = atalk_register_sysctl();
1950 if (rc)
1951 goto out_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952out:
1953 return rc;
YueHaibing057a0da2019-03-01 10:57:57 +08001954out_proc:
1955 atalk_proc_exit();
1956out_aarp:
1957 aarp_cleanup_module();
YueHaibing540b3412019-03-14 13:47:59 +08001958out_dev:
YueHaibing057a0da2019-03-01 10:57:57 +08001959 unregister_netdevice_notifier(&ddp_notifier);
YueHaibing540b3412019-03-14 13:47:59 +08001960out_snap:
YueHaibing057a0da2019-03-01 10:57:57 +08001961 dev_remove_pack(&ppptalk_packet_type);
1962 dev_remove_pack(&ltalk_packet_type);
1963 unregister_snap_client(ddp_dl);
YueHaibing540b3412019-03-14 13:47:59 +08001964out_sock:
YueHaibing057a0da2019-03-01 10:57:57 +08001965 sock_unregister(PF_APPLETALK);
1966out_proto:
1967 proto_unregister(&ddp_proto);
1968 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970module_init(atalk_init);
1971
1972/*
1973 * No explicit module reference count manipulation is needed in the
1974 * protocol. Socket layer sets module reference count for us
1975 * and interfaces reference counting is done
1976 * by the network device layer.
1977 *
1978 * Ergo, before the AppleTalk module can be removed, all AppleTalk
1979 * sockets be closed from user space.
1980 */
1981static void __exit atalk_exit(void)
1982{
1983#ifdef CONFIG_SYSCTL
1984 atalk_unregister_sysctl();
1985#endif /* CONFIG_SYSCTL */
1986 atalk_proc_exit();
1987 aarp_cleanup_module(); /* General aarp clean-up. */
1988 unregister_netdevice_notifier(&ddp_notifier);
1989 dev_remove_pack(&ltalk_packet_type);
1990 dev_remove_pack(&ppptalk_packet_type);
1991 unregister_snap_client(ddp_dl);
1992 sock_unregister(PF_APPLETALK);
1993 proto_unregister(&ddp_proto);
1994}
1995module_exit(atalk_exit);
1996
1997MODULE_LICENSE("GPL");
Alan Cox113aa832008-10-13 19:01:08 -07001998MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999MODULE_DESCRIPTION("AppleTalk 0.20\n");
2000MODULE_ALIAS_NETPROTO(PF_APPLETALK);