blob: 786ee2f83d5fea1dbfd6bb2660544d7f88e1eff2 [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;
1032 rc = -ENOMEM;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001033 sk = sk_alloc(net, PF_APPLETALK, GFP_KERNEL, &ddp_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (!sk)
1035 goto out;
1036 rc = 0;
1037 sock->ops = &atalk_dgram_ops;
1038 sock_init_data(sock, sk);
1039
1040 /* Checksums on by default */
1041 sock_set_flag(sk, SOCK_ZAPPED);
1042out:
1043 return rc;
1044}
1045
1046/* Free a socket. No work needed */
1047static int atalk_release(struct socket *sock)
1048{
1049 struct sock *sk = sock->sk;
1050
1051 if (sk) {
David S. Millerc100c8f2011-03-31 18:59:10 -07001052 sock_hold(sk);
1053 lock_sock(sk);
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 sock_orphan(sk);
1056 sock->sk = NULL;
1057 atalk_destroy_socket(sk);
Arnd Bergmannb20e7bb2011-03-21 18:18:00 -07001058
David S. Millerc100c8f2011-03-31 18:59:10 -07001059 release_sock(sk);
1060 sock_put(sk);
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return 0;
1063}
1064
1065/**
1066 * atalk_pick_and_bind_port - Pick a source port when one is not given
Ben Hutchings2c530402012-07-10 10:55:09 +00001067 * @sk: socket to insert into the tables
1068 * @sat: address to search for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 *
1070 * Pick a source port when one is not given. If we can find a suitable free
1071 * one, we insert the socket into the tables using it.
1072 *
1073 * This whole operation must be atomic.
1074 */
1075static int atalk_pick_and_bind_port(struct sock *sk, struct sockaddr_at *sat)
1076{
1077 int retval;
1078
1079 write_lock_bh(&atalk_sockets_lock);
1080
1081 for (sat->sat_port = ATPORT_RESERVED;
1082 sat->sat_port < ATPORT_LAST;
1083 sat->sat_port++) {
1084 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Sasha Levinb67bfe02013-02-27 17:06:00 -08001086 sk_for_each(s, &atalk_sockets) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 struct atalk_sock *at = at_sk(s);
1088
1089 if (at->src_net == sat->sat_addr.s_net &&
1090 at->src_node == sat->sat_addr.s_node &&
1091 at->src_port == sat->sat_port)
1092 goto try_next_port;
1093 }
1094
1095 /* Wheee, it's free, assign and insert. */
1096 __atalk_insert_socket(sk);
1097 at_sk(sk)->src_port = sat->sat_port;
1098 retval = 0;
1099 goto out;
1100
1101try_next_port:;
1102 }
1103
1104 retval = -EBUSY;
1105out:
1106 write_unlock_bh(&atalk_sockets_lock);
1107 return retval;
1108}
1109
1110static int atalk_autobind(struct sock *sk)
1111{
1112 struct atalk_sock *at = at_sk(sk);
1113 struct sockaddr_at sat;
1114 struct atalk_addr *ap = atalk_find_primary();
1115 int n = -EADDRNOTAVAIL;
1116
1117 if (!ap || ap->s_net == htons(ATADDR_ANYNET))
1118 goto out;
1119
1120 at->src_net = sat.sat_addr.s_net = ap->s_net;
1121 at->src_node = sat.sat_addr.s_node = ap->s_node;
1122
1123 n = atalk_pick_and_bind_port(sk, &sat);
1124 if (!n)
1125 sock_reset_flag(sk, SOCK_ZAPPED);
1126out:
1127 return n;
1128}
1129
1130/* Set the address 'our end' of the connection */
1131static int atalk_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1132{
1133 struct sockaddr_at *addr = (struct sockaddr_at *)uaddr;
1134 struct sock *sk = sock->sk;
1135 struct atalk_sock *at = at_sk(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001136 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 if (!sock_flag(sk, SOCK_ZAPPED) ||
1139 addr_len != sizeof(struct sockaddr_at))
1140 return -EINVAL;
1141
1142 if (addr->sat_family != AF_APPLETALK)
1143 return -EAFNOSUPPORT;
1144
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001145 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (addr->sat_addr.s_net == htons(ATADDR_ANYNET)) {
1147 struct atalk_addr *ap = atalk_find_primary();
1148
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001149 err = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (!ap)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001151 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 at->src_net = addr->sat_addr.s_net = ap->s_net;
wangweidongfd1dc262014-02-14 15:43:43 +08001154 at->src_node = addr->sat_addr.s_node = ap->s_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 } else {
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001156 err = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (!atalk_find_interface(addr->sat_addr.s_net,
1158 addr->sat_addr.s_node))
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001159 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 at->src_net = addr->sat_addr.s_net;
1162 at->src_node = addr->sat_addr.s_node;
1163 }
1164
1165 if (addr->sat_port == ATADDR_ANYPORT) {
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001166 err = atalk_pick_and_bind_port(sk, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001168 if (err < 0)
1169 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 } else {
1171 at->src_port = addr->sat_port;
1172
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001173 err = -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (atalk_find_or_insert_socket(sk, addr))
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001175 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177
1178 sock_reset_flag(sk, SOCK_ZAPPED);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001179 err = 0;
1180out:
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001181 release_sock(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001182 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
1185/* Set the address we talk to */
1186static int atalk_connect(struct socket *sock, struct sockaddr *uaddr,
1187 int addr_len, int flags)
1188{
1189 struct sock *sk = sock->sk;
1190 struct atalk_sock *at = at_sk(sk);
1191 struct sockaddr_at *addr;
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001192 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 sk->sk_state = TCP_CLOSE;
1195 sock->state = SS_UNCONNECTED;
1196
1197 if (addr_len != sizeof(*addr))
1198 return -EINVAL;
1199
1200 addr = (struct sockaddr_at *)uaddr;
1201
1202 if (addr->sat_family != AF_APPLETALK)
1203 return -EAFNOSUPPORT;
1204
1205 if (addr->sat_addr.s_node == ATADDR_BCAST &&
1206 !sock_flag(sk, SOCK_BROADCAST)) {
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001207#if 1
Dave Jones278f0152012-06-06 08:45:59 +00001208 pr_warn("atalk_connect: %s is broken and did not set SO_BROADCAST.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 current->comm);
1210#else
1211 return -EACCES;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001212#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001215 lock_sock(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001216 err = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (sock_flag(sk, SOCK_ZAPPED))
1218 if (atalk_autobind(sk) < 0)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001219 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001221 err = -ENETUNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (!atrtr_get_dev(&addr->sat_addr))
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001223 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 at->dest_port = addr->sat_port;
1226 at->dest_net = addr->sat_addr.s_net;
1227 at->dest_node = addr->sat_addr.s_node;
1228
1229 sock->state = SS_CONNECTED;
1230 sk->sk_state = TCP_ESTABLISHED;
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001231 err = 0;
1232out:
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001233 release_sock(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001234 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
1237/*
1238 * Find the name of an AppleTalk socket. Just copy the right
1239 * fields into the sockaddr.
1240 */
1241static int atalk_getname(struct socket *sock, struct sockaddr *uaddr,
1242 int *uaddr_len, int peer)
1243{
1244 struct sockaddr_at sat;
1245 struct sock *sk = sock->sk;
1246 struct atalk_sock *at = at_sk(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001247 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001249 lock_sock(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001250 err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (sock_flag(sk, SOCK_ZAPPED))
1252 if (atalk_autobind(sk) < 0)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001253 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 *uaddr_len = sizeof(struct sockaddr_at);
Dan Carpenterfccc9f12013-04-22 20:22:15 +00001256 memset(&sat, 0, sizeof(sat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 if (peer) {
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001259 err = -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (sk->sk_state != TCP_ESTABLISHED)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001261 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 sat.sat_addr.s_net = at->dest_net;
1264 sat.sat_addr.s_node = at->dest_node;
1265 sat.sat_port = at->dest_port;
1266 } else {
1267 sat.sat_addr.s_net = at->src_net;
1268 sat.sat_addr.s_node = at->src_node;
1269 sat.sat_port = at->src_port;
1270 }
1271
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001272 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 sat.sat_family = AF_APPLETALK;
1274 memcpy(uaddr, &sat, sizeof(sat));
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001275
1276out:
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001277 release_sock(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001278 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
1281#if defined(CONFIG_IPDDP) || defined(CONFIG_IPDDP_MODULE)
1282static __inline__ int is_ip_over_ddp(struct sk_buff *skb)
1283{
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001284 return skb->data[12] == 22;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
1287static int handle_ip_over_ddp(struct sk_buff *skb)
1288{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001289 struct net_device *dev = __dev_get_by_name(&init_net, "ipddp0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 struct net_device_stats *stats;
1291
1292 /* This needs to be able to handle ipddp"N" devices */
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -07001293 if (!dev) {
1294 kfree_skb(skb);
1295 return NET_RX_DROP;
1296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001298 skb->protocol = htons(ETH_P_IP);
1299 skb_pull(skb, 13);
1300 skb->dev = dev;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001301 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Wang Chen524ad0a2008-11-12 23:39:10 -08001303 stats = netdev_priv(dev);
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001304 stats->rx_packets++;
1305 stats->rx_bytes += skb->len + 13;
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -07001306 return netif_rx(skb); /* Send the SKB up to a higher place. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308#else
1309/* make it easy for gcc to optimize this test out, i.e. kill the code */
1310#define is_ip_over_ddp(skb) 0
1311#define handle_ip_over_ddp(skb) 0
1312#endif
1313
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -07001314static int atalk_route_packet(struct sk_buff *skb, struct net_device *dev,
1315 struct ddpehdr *ddp, __u16 len_hops, int origlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
1317 struct atalk_route *rt;
1318 struct atalk_addr ta;
1319
1320 /*
1321 * Don't route multicast, etc., packets, or packets sent to "this
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001322 * network"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 */
1324 if (skb->pkt_type != PACKET_HOST || !ddp->deh_dnet) {
1325 /*
1326 * FIXME:
1327 *
1328 * Can it ever happen that a packet is from a PPP iface and
1329 * needs to be broadcast onto the default network?
1330 */
1331 if (dev->type == ARPHRD_PPP)
1332 printk(KERN_DEBUG "AppleTalk: didn't forward broadcast "
1333 "packet received from PPP iface\n");
1334 goto free_it;
1335 }
1336
1337 ta.s_net = ddp->deh_dnet;
1338 ta.s_node = ddp->deh_dnode;
1339
1340 /* Route the packet */
1341 rt = atrtr_find(&ta);
Al Viro2a50f282006-09-26 21:22:08 -07001342 /* increment hops count */
1343 len_hops += 1 << 10;
1344 if (!rt || !(len_hops & (15 << 10)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 goto free_it;
Al Viro2a50f282006-09-26 21:22:08 -07001346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 /* FIXME: use skb->cb to be able to use shared skbs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 /*
1350 * Route goes through another gateway, so set the target to the
1351 * gateway instead.
1352 */
1353
1354 if (rt->flags & RTF_GATEWAY) {
1355 ta.s_net = rt->gateway.s_net;
1356 ta.s_node = rt->gateway.s_node;
1357 }
1358
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001359 /* Fix up skb->len field */
1360 skb_trim(skb, min_t(unsigned int, origlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 (rt->dev->hard_header_len +
Al Viro2a50f282006-09-26 21:22:08 -07001362 ddp_dl->header_length + (len_hops & 1023))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 /* FIXME: use skb->cb to be able to use shared skbs */
Al Viro2a50f282006-09-26 21:22:08 -07001365 ddp->deh_len_hops = htons(len_hops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 /*
1368 * Send the buffer onwards
1369 *
1370 * Now we must always be careful. If it's come from LocalTalk to
1371 * EtherTalk it might not fit
1372 *
1373 * Order matters here: If a packet has to be copied to make a new
1374 * headroom (rare hopefully) then it won't need unsharing.
1375 *
1376 * Note. ddp-> becomes invalid at the realloc.
1377 */
1378 if (skb_headroom(skb) < 22) {
1379 /* 22 bytes - 12 ether, 2 len, 3 802.2 5 snap */
1380 struct sk_buff *nskb = skb_realloc_headroom(skb, 32);
1381 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 skb = nskb;
1383 } else
1384 skb = skb_unshare(skb, GFP_ATOMIC);
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /*
1387 * If the buffer didn't vanish into the lack of space bitbucket we can
1388 * send it.
1389 */
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -07001390 if (skb == NULL)
1391 goto drop;
1392
1393 if (aarp_send_ddp(rt->dev, skb, &ta, NULL) == NET_XMIT_DROP)
1394 return NET_RX_DROP;
Mark Smith8be80572009-09-12 20:48:43 +00001395 return NET_RX_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396free_it:
1397 kfree_skb(skb);
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -07001398drop:
1399 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
1402/**
1403 * atalk_rcv - Receive a packet (in skb) from device dev
1404 * @skb - packet received
1405 * @dev - network device where the packet comes from
1406 * @pt - packet type
1407 *
1408 * Receive a packet (in skb) from device dev. This has come from the SNAP
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001409 * decoder, and on entry skb->transport_header is the DDP header, skb->len
1410 * is the DDP header, skb->len is the DDP length. The physical headers
1411 * have been extracted. PPP should probably pass frames marked as for this
1412 * layer. [ie ARPHRD_ETHERTALK]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 */
1414static int atalk_rcv(struct sk_buff *skb, struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001415 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 struct ddpehdr *ddp;
1418 struct sock *sock;
1419 struct atalk_iface *atif;
1420 struct sockaddr_at tosat;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001421 int origlen;
Al Viro2a50f282006-09-26 21:22:08 -07001422 __u16 len_hops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001424 if (!net_eq(dev_net(dev), &init_net))
Mark Smith6885ffb2009-08-06 23:21:22 +00001425 goto drop;
Eric W. Biedermane730c152007-09-17 11:53:39 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 /* Don't mangle buffer if shared */
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001428 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 goto out;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 /* Size check and make sure header is contiguous */
1432 if (!pskb_may_pull(skb, sizeof(*ddp)))
Mark Smith6885ffb2009-08-06 23:21:22 +00001433 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 ddp = ddp_hdr(skb);
1436
Al Viro2a50f282006-09-26 21:22:08 -07001437 len_hops = ntohs(ddp->deh_len_hops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 /* Trim buffer in case of stray trailing data */
1440 origlen = skb->len;
Al Viro2a50f282006-09-26 21:22:08 -07001441 skb_trim(skb, min_t(unsigned int, skb->len, len_hops & 1023));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 /*
1444 * Size check to see if ddp->deh_len was crap
1445 * (Otherwise we'll detonate most spectacularly
Jean Delvare75559c12007-04-04 23:52:46 -07001446 * in the middle of atalk_checksum() or recvmsg()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
Jean Delvare75559c12007-04-04 23:52:46 -07001448 if (skb->len < sizeof(*ddp) || skb->len < (len_hops & 1023)) {
1449 pr_debug("AppleTalk: dropping corrupted frame (deh_len=%u, "
1450 "skb->len=%u)\n", len_hops & 1023, skb->len);
Mark Smith6885ffb2009-08-06 23:21:22 +00001451 goto drop;
Jean Delvare75559c12007-04-04 23:52:46 -07001452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 /*
1455 * Any checksums. Note we don't do htons() on this == is assumed to be
1456 * valid for net byte orders all over the networking code...
1457 */
1458 if (ddp->deh_sum &&
Al Viro2a50f282006-09-26 21:22:08 -07001459 atalk_checksum(skb, len_hops & 1023) != ddp->deh_sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /* Not a valid AppleTalk frame - dustbin time */
Mark Smith6885ffb2009-08-06 23:21:22 +00001461 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 /* Check the packet is aimed at us */
1464 if (!ddp->deh_dnet) /* Net 0 is 'this network' */
1465 atif = atalk_find_anynet(ddp->deh_dnode, dev);
1466 else
1467 atif = atalk_find_interface(ddp->deh_dnet, ddp->deh_dnode);
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (!atif) {
Oliver Dawid64233bf2005-09-27 16:11:29 -07001470 /* Not ours, so we route the packet via the correct
1471 * AppleTalk iface
1472 */
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -07001473 return atalk_route_packet(skb, dev, ddp, len_hops, origlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475
1476 /* if IP over DDP is not selected this code will be optimized out */
1477 if (is_ip_over_ddp(skb))
1478 return handle_ip_over_ddp(skb);
1479 /*
1480 * Which socket - atalk_search_socket() looks for a *full match*
1481 * of the <net, node, port> tuple.
1482 */
1483 tosat.sat_addr.s_net = ddp->deh_dnet;
1484 tosat.sat_addr.s_node = ddp->deh_dnode;
1485 tosat.sat_port = ddp->deh_dport;
1486
1487 sock = atalk_search_socket(&tosat, atif);
1488 if (!sock) /* But not one of our sockets */
Mark Smith6885ffb2009-08-06 23:21:22 +00001489 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 /* Queue packet (standard) */
1492 skb->sk = sock;
1493
1494 if (sock_queue_rcv_skb(sock, skb) < 0)
Mark Smith6885ffb2009-08-06 23:21:22 +00001495 goto drop;
1496
1497 return NET_RX_SUCCESS;
1498
1499drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 kfree_skb(skb);
Mark Smith6885ffb2009-08-06 23:21:22 +00001501out:
1502 return NET_RX_DROP;
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
1506/*
1507 * Receive a LocalTalk frame. We make some demands on the caller here.
1508 * Caller must provide enough headroom on the packet to pull the short
1509 * header and append a long one.
1510 */
1511static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001512 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001514 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -07001515 goto freeit;
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 /* Expand any short form frames */
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001518 if (skb_mac_header(skb)[2] == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct ddpehdr *ddp;
1520 /* Find our address */
1521 struct atalk_addr *ap = atalk_find_dev_addr(dev);
1522
Al Viro2a50f282006-09-26 21:22:08 -07001523 if (!ap || skb->len < sizeof(__be16) || skb->len > 1023)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 goto freeit;
1525
1526 /* Don't mangle buffer if shared */
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001527 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return 0;
1529
1530 /*
1531 * The push leaves us with a ddephdr not an shdr, and
1532 * handily the port bytes in the right place preset.
1533 */
1534 ddp = (struct ddpehdr *) skb_push(skb, sizeof(*ddp) - 4);
1535
1536 /* Now fill in the long header */
1537
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001538 /*
1539 * These two first. The mac overlays the new source/dest
1540 * network information so we MUST copy these before
1541 * we write the network numbers !
1542 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001544 ddp->deh_dnode = skb_mac_header(skb)[0]; /* From physical header */
1545 ddp->deh_snode = skb_mac_header(skb)[1]; /* From physical header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 ddp->deh_dnet = ap->s_net; /* Network number */
1548 ddp->deh_snet = ap->s_net;
1549 ddp->deh_sum = 0; /* No checksum */
1550 /*
1551 * Not sure about this bit...
1552 */
Al Viro2a50f282006-09-26 21:22:08 -07001553 /* Non routable, so force a drop if we slip up later */
1554 ddp->deh_len_hops = htons(skb->len + (DDP_MAXHOPS << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001556 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001558 return atalk_rcv(skb, dev, pt, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559freeit:
1560 kfree_skb(skb);
1561 return 0;
1562}
1563
1564static int atalk_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
1565 size_t len)
1566{
1567 struct sock *sk = sock->sk;
1568 struct atalk_sock *at = at_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001569 DECLARE_SOCKADDR(struct sockaddr_at *, usat, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 int flags = msg->msg_flags;
1571 int loopback = 0;
1572 struct sockaddr_at local_satalk, gsat;
1573 struct sk_buff *skb;
1574 struct net_device *dev;
1575 struct ddpehdr *ddp;
1576 int size;
1577 struct atalk_route *rt;
1578 int err;
1579
1580 if (flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
1581 return -EINVAL;
1582
1583 if (len > DDP_MAXSZ)
1584 return -EMSGSIZE;
1585
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001586 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (usat) {
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001588 err = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (sock_flag(sk, SOCK_ZAPPED))
1590 if (atalk_autobind(sk) < 0)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001591 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001593 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (msg->msg_namelen < sizeof(*usat) ||
1595 usat->sat_family != AF_APPLETALK)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001596 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001598 err = -EPERM;
Alan Cox03ba9992009-03-27 00:27:18 -07001599 /* netatalk didn't implement this check */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (usat->sat_addr.s_node == ATADDR_BCAST &&
1601 !sock_flag(sk, SOCK_BROADCAST)) {
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001602 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
1604 } else {
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001605 err = -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (sk->sk_state != TCP_ESTABLISHED)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001607 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 usat = &local_satalk;
1609 usat->sat_family = AF_APPLETALK;
1610 usat->sat_port = at->dest_port;
1611 usat->sat_addr.s_node = at->dest_node;
1612 usat->sat_addr.s_net = at->dest_net;
1613 }
1614
1615 /* Build a packet */
1616 SOCK_DEBUG(sk, "SK %p: Got address.\n", sk);
1617
1618 /* For headers */
1619 size = sizeof(struct ddpehdr) + len + ddp_dl->header_length;
1620
1621 if (usat->sat_addr.s_net || usat->sat_addr.s_node == ATADDR_ANYNODE) {
1622 rt = atrtr_find(&usat->sat_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 } else {
1624 struct atalk_addr at_hint;
1625
1626 at_hint.s_node = 0;
1627 at_hint.s_net = at->src_net;
1628
1629 rt = atrtr_find(&at_hint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001631 err = ENETUNREACH;
Oliver Dawid64233bf2005-09-27 16:11:29 -07001632 if (!rt)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001633 goto out;
Oliver Dawid64233bf2005-09-27 16:11:29 -07001634
1635 dev = rt->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 SOCK_DEBUG(sk, "SK %p: Size needed %d, device %s\n",
1638 sk, size, dev->name);
1639
1640 size += dev->hard_header_len;
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001641 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 skb = sock_alloc_send_skb(sk, size, (flags & MSG_DONTWAIT), &err);
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001643 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (!skb)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001645 goto out;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 skb->sk = sk;
1648 skb_reserve(skb, ddp_dl->header_length);
1649 skb_reserve(skb, dev->hard_header_len);
1650 skb->dev = dev;
1651
1652 SOCK_DEBUG(sk, "SK %p: Begin build.\n", sk);
1653
1654 ddp = (struct ddpehdr *)skb_put(skb, sizeof(struct ddpehdr));
Al Viro2a50f282006-09-26 21:22:08 -07001655 ddp->deh_len_hops = htons(len + sizeof(*ddp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 ddp->deh_dnet = usat->sat_addr.s_net;
1657 ddp->deh_snet = at->src_net;
1658 ddp->deh_dnode = usat->sat_addr.s_node;
1659 ddp->deh_snode = at->src_node;
1660 ddp->deh_dport = usat->sat_port;
1661 ddp->deh_sport = at->src_port;
1662
1663 SOCK_DEBUG(sk, "SK %p: Copy user data (%Zd bytes).\n", sk, len);
1664
1665 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1666 if (err) {
1667 kfree_skb(skb);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001668 err = -EFAULT;
1669 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671
1672 if (sk->sk_no_check == 1)
1673 ddp->deh_sum = 0;
1674 else
1675 ddp->deh_sum = atalk_checksum(skb, len + sizeof(*ddp));
1676
1677 /*
1678 * Loopback broadcast packets to non gateway targets (ie routes
1679 * to group we are in)
1680 */
1681 if (ddp->deh_dnode == ATADDR_BCAST &&
1682 !(rt->flags & RTF_GATEWAY) && !(dev->flags & IFF_LOOPBACK)) {
1683 struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL);
1684
1685 if (skb2) {
1686 loopback = 1;
1687 SOCK_DEBUG(sk, "SK %p: send out(copy).\n", sk);
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -07001688 /*
1689 * If it fails it is queued/sent above in the aarp queue
1690 */
1691 aarp_send_ddp(dev, skb2, &usat->sat_addr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
1693 }
1694
1695 if (dev->flags & IFF_LOOPBACK || loopback) {
1696 SOCK_DEBUG(sk, "SK %p: Loop back.\n", sk);
1697 /* loop back */
1698 skb_orphan(skb);
Oliver Dawid64233bf2005-09-27 16:11:29 -07001699 if (ddp->deh_dnode == ATADDR_BCAST) {
1700 struct atalk_addr at_lo;
1701
1702 at_lo.s_node = 0;
1703 at_lo.s_net = 0;
1704
1705 rt = atrtr_find(&at_lo);
1706 if (!rt) {
1707 kfree_skb(skb);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001708 err = -ENETUNREACH;
1709 goto out;
Oliver Dawid64233bf2005-09-27 16:11:29 -07001710 }
1711 dev = rt->dev;
1712 skb->dev = dev;
1713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 ddp_dl->request(ddp_dl, skb, dev->dev_addr);
1715 } else {
1716 SOCK_DEBUG(sk, "SK %p: send out.\n", sk);
1717 if (rt->flags & RTF_GATEWAY) {
1718 gsat.sat_addr = rt->gateway;
1719 usat = &gsat;
1720 }
1721
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -07001722 /*
1723 * If it fails it is queued/sent above in the aarp queue
1724 */
1725 aarp_send_ddp(dev, skb, &usat->sat_addr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
1727 SOCK_DEBUG(sk, "SK %p: Done write (%Zd).\n", sk, len);
1728
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001729out:
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001730 release_sock(sk);
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001731 return err ? : len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
1734static int atalk_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
1735 size_t size, int flags)
1736{
1737 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 struct ddpehdr *ddp;
1739 int copied = 0;
Al Viro2a50f282006-09-26 21:22:08 -07001740 int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 int err = 0;
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001742 struct sk_buff *skb;
1743
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001744 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 flags & MSG_DONTWAIT, &err);
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001746 lock_sock(sk);
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (!skb)
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001749 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 /* FIXME: use skb->cb to be able to use shared skbs */
1752 ddp = ddp_hdr(skb);
Al Viro2a50f282006-09-26 21:22:08 -07001753 copied = ntohs(ddp->deh_len_hops) & 1023;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Al Viro2a50f282006-09-26 21:22:08 -07001755 if (sk->sk_type != SOCK_RAW) {
1756 offset = sizeof(*ddp);
1757 copied -= offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
1759
Al Viro2a50f282006-09-26 21:22:08 -07001760 if (copied > size) {
1761 copied = size;
1762 msg->msg_flags |= MSG_TRUNC;
1763 }
1764 err = skb_copy_datagram_iovec(skb, offset, msg->msg_iov, copied);
1765
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01001766 if (!err && msg->msg_name) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001767 DECLARE_SOCKADDR(struct sockaddr_at *, sat, msg->msg_name);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01001768 sat->sat_family = AF_APPLETALK;
1769 sat->sat_port = ddp->deh_sport;
1770 sat->sat_addr.s_node = ddp->deh_snode;
1771 sat->sat_addr.s_net = ddp->deh_snet;
1772 msg->msg_namelen = sizeof(*sat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774
1775 skb_free_datagram(sk, skb); /* Free the datagram. */
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001776
1777out:
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001778 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return err ? : copied;
1780}
1781
1782
1783/*
1784 * AppleTalk ioctl calls.
1785 */
1786static int atalk_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1787{
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001788 int rc = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 struct sock *sk = sock->sk;
1790 void __user *argp = (void __user *)arg;
1791
1792 switch (cmd) {
wangweidong7b306002014-02-14 15:43:46 +08001793 /* Protocol layer */
1794 case TIOCOUTQ: {
1795 long amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
wangweidong7b306002014-02-14 15:43:46 +08001797 if (amount < 0)
1798 amount = 0;
1799 rc = put_user(amount, (int __user *)argp);
1800 break;
1801 }
1802 case TIOCINQ: {
1803 /*
1804 * These two are safe on a single CPU system as only
1805 * user tasks fiddle here
1806 */
1807 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
1808 long amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
wangweidong7b306002014-02-14 15:43:46 +08001810 if (skb)
1811 amount = skb->len - sizeof(struct ddpehdr);
1812 rc = put_user(amount, (int __user *)argp);
1813 break;
1814 }
1815 case SIOCGSTAMP:
1816 rc = sock_get_timestamp(sk, argp);
1817 break;
1818 case SIOCGSTAMPNS:
1819 rc = sock_get_timestampns(sk, argp);
1820 break;
1821 /* Routing */
1822 case SIOCADDRT:
1823 case SIOCDELRT:
1824 rc = -EPERM;
1825 if (capable(CAP_NET_ADMIN))
1826 rc = atrtr_ioctl(cmd, argp);
1827 break;
1828 /* Interface */
1829 case SIOCGIFADDR:
1830 case SIOCSIFADDR:
1831 case SIOCGIFBRDADDR:
1832 case SIOCATALKDIFADDR:
1833 case SIOCDIFADDR:
1834 case SIOCSARP: /* proxy AARP */
1835 case SIOCDARP: /* proxy AARP */
1836 rtnl_lock();
1837 rc = atif_ioctl(cmd, argp);
1838 rtnl_unlock();
1839 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
1841
1842 return rc;
1843}
1844
Petr Vandrovecf6c90b72006-03-27 23:39:31 -08001845
1846#ifdef CONFIG_COMPAT
1847static int atalk_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1848{
1849 /*
Arnd Bergmann20660222009-11-06 08:09:06 +00001850 * SIOCATALKDIFADDR is a SIOCPROTOPRIVATE ioctl number, so we
1851 * cannot handle it in common code. The data we access if ifreq
1852 * here is compatible, so we can simply call the native
1853 * handler.
Petr Vandrovecf6c90b72006-03-27 23:39:31 -08001854 */
Arnd Bergmann20660222009-11-06 08:09:06 +00001855 if (cmd == SIOCATALKDIFADDR)
1856 return atalk_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
1857
Petr Vandrovecf6c90b72006-03-27 23:39:31 -08001858 return -ENOIOCTLCMD;
1859}
1860#endif
1861
1862
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001863static const struct net_proto_family atalk_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 .family = PF_APPLETALK,
1865 .create = atalk_create,
1866 .owner = THIS_MODULE,
1867};
1868
Arnd Bergmannecced8b2009-11-05 04:37:26 +00001869static const struct proto_ops atalk_dgram_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 .family = PF_APPLETALK,
1871 .owner = THIS_MODULE,
1872 .release = atalk_release,
1873 .bind = atalk_bind,
1874 .connect = atalk_connect,
1875 .socketpair = sock_no_socketpair,
1876 .accept = sock_no_accept,
1877 .getname = atalk_getname,
Arnd Bergmann60d9f462011-01-23 00:21:11 +01001878 .poll = datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 .ioctl = atalk_ioctl,
Petr Vandrovecf6c90b72006-03-27 23:39:31 -08001880#ifdef CONFIG_COMPAT
1881 .compat_ioctl = atalk_compat_ioctl,
1882#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 .listen = sock_no_listen,
1884 .shutdown = sock_no_shutdown,
1885 .setsockopt = sock_no_setsockopt,
1886 .getsockopt = sock_no_getsockopt,
1887 .sendmsg = atalk_sendmsg,
1888 .recvmsg = atalk_recvmsg,
1889 .mmap = sock_no_mmap,
1890 .sendpage = sock_no_sendpage,
1891};
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893static struct notifier_block ddp_notifier = {
1894 .notifier_call = ddp_device_event,
1895};
1896
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001897static struct packet_type ltalk_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08001898 .type = cpu_to_be16(ETH_P_LOCALTALK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 .func = ltalk_rcv,
1900};
1901
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001902static struct packet_type ppptalk_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08001903 .type = cpu_to_be16(ETH_P_PPPTALK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 .func = atalk_rcv,
1905};
1906
1907static unsigned char ddp_snap_id[] = { 0x08, 0x00, 0x07, 0x80, 0x9B };
1908
1909/* Export symbols for use by drivers when AppleTalk is a module */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910EXPORT_SYMBOL(atrtr_get_dev);
1911EXPORT_SYMBOL(atalk_find_dev_addr);
1912
Hannes Eder2db09602009-02-25 10:31:59 +00001913static const char atalk_err_snap[] __initconst =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 KERN_CRIT "Unable to register DDP with SNAP.\n";
1915
1916/* Called by proto.c on kernel start up */
1917static int __init atalk_init(void)
1918{
1919 int rc = proto_register(&ddp_proto, 0);
1920
1921 if (rc != 0)
1922 goto out;
1923
1924 (void)sock_register(&atalk_family_ops);
1925 ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv);
1926 if (!ddp_dl)
1927 printk(atalk_err_snap);
1928
1929 dev_add_pack(&ltalk_packet_type);
1930 dev_add_pack(&ppptalk_packet_type);
1931
1932 register_netdevice_notifier(&ddp_notifier);
1933 aarp_proto_init();
1934 atalk_proc_init();
1935 atalk_register_sysctl();
1936out:
1937 return rc;
1938}
1939module_init(atalk_init);
1940
1941/*
1942 * No explicit module reference count manipulation is needed in the
1943 * protocol. Socket layer sets module reference count for us
1944 * and interfaces reference counting is done
1945 * by the network device layer.
1946 *
1947 * Ergo, before the AppleTalk module can be removed, all AppleTalk
1948 * sockets be closed from user space.
1949 */
1950static void __exit atalk_exit(void)
1951{
1952#ifdef CONFIG_SYSCTL
1953 atalk_unregister_sysctl();
1954#endif /* CONFIG_SYSCTL */
1955 atalk_proc_exit();
1956 aarp_cleanup_module(); /* General aarp clean-up. */
1957 unregister_netdevice_notifier(&ddp_notifier);
1958 dev_remove_pack(&ltalk_packet_type);
1959 dev_remove_pack(&ppptalk_packet_type);
1960 unregister_snap_client(ddp_dl);
1961 sock_unregister(PF_APPLETALK);
1962 proto_unregister(&ddp_proto);
1963}
1964module_exit(atalk_exit);
1965
1966MODULE_LICENSE("GPL");
Alan Cox113aa832008-10-13 19:01:08 -07001967MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968MODULE_DESCRIPTION("AppleTalk 0.20\n");
1969MODULE_ALIAS_NETPROTO(PF_APPLETALK);