blob: 590b8396362237e62b82965b2f6e95fb1c167591 [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 */
58#include <net/datalink.h>
59#include <net/psnap.h>
60#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070061#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <net/route.h>
63#include <linux/atalk.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040064#include "../core/kmap_skb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66struct datalink_proto *ddp_dl, *aarp_dl;
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080067static const struct proto_ops atalk_dgram_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/**************************************************************************\
70* *
71* Handlers for the socket list. *
72* *
73\**************************************************************************/
74
75HLIST_HEAD(atalk_sockets);
76DEFINE_RWLOCK(atalk_sockets_lock);
77
78static inline void __atalk_insert_socket(struct sock *sk)
79{
80 sk_add_node(sk, &atalk_sockets);
81}
82
83static inline void atalk_remove_socket(struct sock *sk)
84{
85 write_lock_bh(&atalk_sockets_lock);
86 sk_del_node_init(sk);
87 write_unlock_bh(&atalk_sockets_lock);
88}
89
90static struct sock *atalk_search_socket(struct sockaddr_at *to,
91 struct atalk_iface *atif)
92{
93 struct sock *s;
94 struct hlist_node *node;
95
96 read_lock_bh(&atalk_sockets_lock);
97 sk_for_each(s, node, &atalk_sockets) {
98 struct atalk_sock *at = at_sk(s);
99
100 if (to->sat_port != at->src_port)
101 continue;
102
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900103 if (to->sat_addr.s_net == ATADDR_ANYNET &&
Oliver Dawid64233bf2005-09-27 16:11:29 -0700104 to->sat_addr.s_node == ATADDR_BCAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 goto found;
106
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900107 if (to->sat_addr.s_net == at->src_net &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 (to->sat_addr.s_node == at->src_node ||
109 to->sat_addr.s_node == ATADDR_BCAST ||
110 to->sat_addr.s_node == ATADDR_ANYNODE))
111 goto found;
112
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900113 /* XXXX.0 -- we got a request for this router. make sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * that the node is appropriately set. */
115 if (to->sat_addr.s_node == ATADDR_ANYNODE &&
116 to->sat_addr.s_net != ATADDR_ANYNET &&
117 atif->address.s_node == at->src_node) {
118 to->sat_addr.s_node = atif->address.s_node;
119 goto found;
120 }
121 }
122 s = NULL;
123found:
124 read_unlock_bh(&atalk_sockets_lock);
125 return s;
126}
127
128/**
129 * atalk_find_or_insert_socket - Try to find a socket matching ADDR
130 * @sk - socket to insert in the list if it is not there already
131 * @sat - address to search for
132 *
133 * Try to find a socket matching ADDR in the socket list, if found then return
134 * it. If not, insert SK into the socket list.
135 *
136 * This entire operation must execute atomically.
137 */
138static struct sock *atalk_find_or_insert_socket(struct sock *sk,
139 struct sockaddr_at *sat)
140{
141 struct sock *s;
142 struct hlist_node *node;
143 struct atalk_sock *at;
144
145 write_lock_bh(&atalk_sockets_lock);
146 sk_for_each(s, node, &atalk_sockets) {
147 at = at_sk(s);
148
149 if (at->src_net == sat->sat_addr.s_net &&
150 at->src_node == sat->sat_addr.s_node &&
151 at->src_port == sat->sat_port)
152 goto found;
153 }
154 s = NULL;
155 __atalk_insert_socket(sk); /* Wheee, it's free, assign and insert. */
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,
296 struct atalk_addr* proxy_addr)
297{
298 int netrange = ntohs(atif->nets.nr_lastnet) -
299 ntohs(atif->nets.nr_firstnet) + 1;
300 /* we probe the interface's network */
301 int probe_net = ntohs(atif->address.s_net);
302 int probe_node = ATADDR_ANYNODE; /* we'll take anything */
303 int netct, nodect;
304
305 /* Offset the network we start probing with */
306 if (probe_net == ATADDR_ANYNET) {
307 probe_net = ntohs(atif->nets.nr_firstnet);
308 if (netrange)
309 probe_net += jiffies % netrange;
310 }
311
312 if (probe_node == ATADDR_ANYNODE)
313 probe_node = jiffies & 0xFF;
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 */
584static int atrtr_delete(struct atalk_addr * addr)
585{
586 struct atalk_route **r = &atalk_routes;
587 int retval = 0;
588 struct atalk_route *tmp;
589
590 write_lock_bh(&atalk_routes_lock);
591 while ((tmp = *r) != NULL) {
592 if (tmp->target.s_net == addr->s_net &&
593 (!(tmp->flags&RTF_GATEWAY) ||
594 tmp->target.s_node == addr->s_node)) {
595 *r = tmp->next;
596 dev_put(tmp->dev);
597 kfree(tmp);
598 goto out;
599 }
600 r = &tmp->next;
601 }
602 retval = -ENOENT;
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{
Eric W. Biederman890d52d2007-09-12 11:26:59 +0200647 struct net_device *dev = ptr;
648
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) {
685 case SIOCSIFADDR:
686 if (!capable(CAP_NET_ADMIN))
687 return -EPERM;
688 if (sa->sat_family != AF_APPLETALK)
689 return -EINVAL;
690 if (dev->type != ARPHRD_ETHER &&
691 dev->type != ARPHRD_LOOPBACK &&
692 dev->type != ARPHRD_LOCALTLK &&
693 dev->type != ARPHRD_PPP)
694 return -EPROTONOSUPPORT;
695
696 nr = (struct atalk_netrange *)&sa->sat_zero[0];
697 add_route = 1;
698
699 /*
700 * if this is a point-to-point iface, and we already
701 * have an iface for this AppleTalk address, then we
702 * should not add a route
703 */
704 if ((dev->flags & IFF_POINTOPOINT) &&
705 atalk_find_interface(sa->sat_addr.s_net,
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900706 sa->sat_addr.s_node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /*
714 * Phase 1 is fine on LocalTalk but we don't do
715 * EtherTalk phase 1. Anyone wanting to add it go ahead.
716 */
717 if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
718 return -EPROTONOSUPPORT;
719 if (sa->sat_addr.s_node == ATADDR_BCAST ||
720 sa->sat_addr.s_node == 254)
721 return -EINVAL;
722 if (atif) {
723 /* Already setting address */
724 if (atif->status & ATIF_PROBE)
725 return -EBUSY;
726
727 atif->address.s_net = sa->sat_addr.s_net;
728 atif->address.s_node = sa->sat_addr.s_node;
729 atrtr_device_down(dev); /* Flush old routes */
730 } else {
731 atif = atif_add_device(dev, &sa->sat_addr);
732 if (!atif)
733 return -ENOMEM;
734 }
735 atif->nets = *nr;
736
737 /*
738 * Check if the chosen address is used. If so we
739 * error and atalkd will try another.
740 */
741
742 if (!(dev->flags & IFF_LOOPBACK) &&
743 !(dev->flags & IFF_POINTOPOINT) &&
744 atif_probe_device(atif) < 0) {
745 atif_drop_device(dev);
746 return -EADDRINUSE;
747 }
748
749 /* Hey it worked - add the direct routes */
750 sa = (struct sockaddr_at *)&rtdef.rt_gateway;
751 sa->sat_family = AF_APPLETALK;
752 sa->sat_addr.s_net = atif->address.s_net;
753 sa->sat_addr.s_node = atif->address.s_node;
754 sa = (struct sockaddr_at *)&rtdef.rt_dst;
755 rtdef.rt_flags = RTF_UP;
756 sa->sat_family = AF_APPLETALK;
757 sa->sat_addr.s_node = ATADDR_ANYNODE;
758 if (dev->flags & IFF_LOOPBACK ||
759 dev->flags & IFF_POINTOPOINT)
760 rtdef.rt_flags |= RTF_HOST;
761
762 /* Routerless initial state */
763 if (nr->nr_firstnet == htons(0) &&
764 nr->nr_lastnet == htons(0xFFFE)) {
765 sa->sat_addr.s_net = atif->address.s_net;
766 atrtr_create(&rtdef, dev);
767 atrtr_set_default(dev);
768 } else {
769 limit = ntohs(nr->nr_lastnet);
770 if (limit - ntohs(nr->nr_firstnet) > 4096) {
771 printk(KERN_WARNING "Too many routes/"
772 "iface.\n");
773 return -EINVAL;
774 }
775 if (add_route)
776 for (ct = ntohs(nr->nr_firstnet);
777 ct <= limit; ct++) {
778 sa->sat_addr.s_net = htons(ct);
779 atrtr_create(&rtdef, dev);
780 }
781 }
782 dev_mc_add(dev, aarp_mcast, 6, 1);
783 return 0;
784
785 case SIOCGIFADDR:
786 if (!atif)
787 return -EADDRNOTAVAIL;
788
789 sa->sat_family = AF_APPLETALK;
790 sa->sat_addr = atif->address;
791 break;
792
793 case SIOCGIFBRDADDR:
794 if (!atif)
795 return -EADDRNOTAVAIL;
796
797 sa->sat_family = AF_APPLETALK;
798 sa->sat_addr.s_net = atif->address.s_net;
799 sa->sat_addr.s_node = ATADDR_BCAST;
800 break;
801
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900802 case SIOCATALKDIFADDR:
803 case SIOCDIFADDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!capable(CAP_NET_ADMIN))
805 return -EPERM;
806 if (sa->sat_family != AF_APPLETALK)
807 return -EINVAL;
808 atalk_dev_down(dev);
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900809 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 case SIOCSARP:
812 if (!capable(CAP_NET_ADMIN))
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900813 return -EPERM;
814 if (sa->sat_family != AF_APPLETALK)
815 return -EINVAL;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900816 /*
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
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900823 /*
824 * atif points to the current interface on this network;
825 * we aren't concerned about its current status (at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 * least for now), but it has all the settings about
827 * the network we're going to probe. Consequently, it
828 * must exist.
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900829 */
830 if (!atif)
831 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900833 nr = (struct atalk_netrange *)&(atif->nets);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /*
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900835 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 * 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.
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900857 */
858 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900860 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900868 /* 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) {
885 case SIOCDELRT:
886 if (rt.rt_dst.sa_family != AF_APPLETALK)
887 return -EINVAL;
888 return atrtr_delete(&((struct sockaddr_at *)
889 &rt.rt_dst)->sat_addr);
890
891 case SIOCADDRT: {
892 struct net_device *dev = NULL;
893 if (rt.rt_dev) {
894 char name[IFNAMSIZ];
895 if (copy_from_user(name, rt.rt_dev, IFNAMSIZ-1))
896 return -EFAULT;
897 name[IFNAMSIZ-1] = '\0';
Eric W. Biederman881d9662007-09-17 11:56:21 -0700898 dev = __dev_get_by_name(&init_net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (!dev)
900 return -ENODEV;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return atrtr_create(&rt, dev);
903 }
904 }
905 return -EINVAL;
906}
907
908/**************************************************************************\
909* *
910* Handling for system calls applied via the various interfaces to an *
911* AppleTalk socket object. *
912* *
913\**************************************************************************/
914
915/*
916 * Checksum: This is 'optional'. It's quite likely also a good
917 * candidate for assembler hackery 8)
918 */
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--) {
924 sum += *data;
925 sum <<= 1;
926 if (sum & 0x10000) {
927 sum++;
928 sum &= 0xffff;
929 }
930 data++;
931 }
932 return sum;
933}
934
935/* Checksum skb data -- similar to skb_checksum */
936static unsigned long atalk_sum_skb(const struct sk_buff *skb, int offset,
937 int len, unsigned long sum)
938{
David S. Miller1a028e52007-04-27 15:21:23 -0700939 int start = skb_headlen(skb);
David S. Millerc32ba3f2009-06-09 00:17:44 -0700940 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 int i, copy;
942
943 /* checksum stuff in header space */
David S. Miller1a028e52007-04-27 15:21:23 -0700944 if ( (copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (copy > len)
946 copy = len;
947 sum = atalk_sum_partial(skb->data + offset, copy, sum);
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900948 if ( (len -= copy) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return sum;
950
951 offset += copy;
952 }
953
954 /* checksum stuff in frags */
955 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -0700956 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700958 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -0700959
960 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if ((copy = end - offset) > 0) {
962 u8 *vaddr;
963 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
964
965 if (copy > len)
966 copy = len;
967 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -0700968 sum = atalk_sum_partial(vaddr + frag->page_offset +
969 offset - start, copy, sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 kunmap_skb_frag(vaddr);
971
972 if (!(len -= copy))
973 return sum;
974 offset += copy;
975 }
David S. Miller1a028e52007-04-27 15:21:23 -0700976 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978
David S. Millerc32ba3f2009-06-09 00:17:44 -0700979 skb_walk_frags(skb, frag_iter) {
980 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
David S. Millerc32ba3f2009-06-09 00:17:44 -0700982 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
David S. Millerc32ba3f2009-06-09 00:17:44 -0700984 end = start + frag_iter->len;
985 if ((copy = end - offset) > 0) {
986 if (copy > len)
987 copy = len;
988 sum = atalk_sum_skb(frag_iter, offset - start,
989 copy, sum);
990 if ((len -= copy) == 0)
991 return sum;
992 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
David S. Millerc32ba3f2009-06-09 00:17:44 -0700994 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996
997 BUG_ON(len > 0);
998
999 return sum;
1000}
1001
Al Viro2a50f282006-09-26 21:22:08 -07001002static __be16 atalk_checksum(const struct sk_buff *skb, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 unsigned long sum;
1005
1006 /* skip header 4 bytes */
1007 sum = atalk_sum_skb(skb, 4, len-4, 0);
1008
1009 /* Use 0xFFFF for 0. 0 itself means none */
Al Viro2a50f282006-09-26 21:22:08 -07001010 return sum ? htons((unsigned short)sum) : htons(0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
1013static struct proto ddp_proto = {
1014 .name = "DDP",
1015 .owner = THIS_MODULE,
1016 .obj_size = sizeof(struct atalk_sock),
1017};
1018
1019/*
1020 * Create a socket. Initialise the socket, blank the addresses
1021 * set the state.
1022 */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001023static int atalk_create(struct net *net, struct socket *sock, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 struct sock *sk;
1026 int rc = -ESOCKTNOSUPPORT;
1027
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001028 if (net != &init_net)
1029 return -EAFNOSUPPORT;
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /*
1032 * We permit SOCK_DGRAM and RAW is an extension. It is trivial to do
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001033 * and gives you the full ELAP frame. Should be handy for CAP 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
1035 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
1036 goto out;
1037 rc = -ENOMEM;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001038 sk = sk_alloc(net, PF_APPLETALK, GFP_KERNEL, &ddp_proto);
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) {
1057 sock_orphan(sk);
1058 sock->sk = NULL;
1059 atalk_destroy_socket(sk);
1060 }
1061 return 0;
1062}
1063
1064/**
1065 * atalk_pick_and_bind_port - Pick a source port when one is not given
1066 * @sk - socket to insert into the tables
1067 * @sat - address to search for
1068 *
1069 * Pick a source port when one is not given. If we can find a suitable free
1070 * one, we insert the socket into the tables using it.
1071 *
1072 * This whole operation must be atomic.
1073 */
1074static int atalk_pick_and_bind_port(struct sock *sk, struct sockaddr_at *sat)
1075{
1076 int retval;
1077
1078 write_lock_bh(&atalk_sockets_lock);
1079
1080 for (sat->sat_port = ATPORT_RESERVED;
1081 sat->sat_port < ATPORT_LAST;
1082 sat->sat_port++) {
1083 struct sock *s;
1084 struct hlist_node *node;
1085
1086 sk_for_each(s, node, &atalk_sockets) {
1087 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);
1136
1137 if (!sock_flag(sk, SOCK_ZAPPED) ||
1138 addr_len != sizeof(struct sockaddr_at))
1139 return -EINVAL;
1140
1141 if (addr->sat_family != AF_APPLETALK)
1142 return -EAFNOSUPPORT;
1143
1144 if (addr->sat_addr.s_net == htons(ATADDR_ANYNET)) {
1145 struct atalk_addr *ap = atalk_find_primary();
1146
1147 if (!ap)
1148 return -EADDRNOTAVAIL;
1149
1150 at->src_net = addr->sat_addr.s_net = ap->s_net;
1151 at->src_node = addr->sat_addr.s_node= ap->s_node;
1152 } else {
1153 if (!atalk_find_interface(addr->sat_addr.s_net,
1154 addr->sat_addr.s_node))
1155 return -EADDRNOTAVAIL;
1156
1157 at->src_net = addr->sat_addr.s_net;
1158 at->src_node = addr->sat_addr.s_node;
1159 }
1160
1161 if (addr->sat_port == ATADDR_ANYPORT) {
1162 int n = atalk_pick_and_bind_port(sk, addr);
1163
1164 if (n < 0)
1165 return n;
1166 } else {
1167 at->src_port = addr->sat_port;
1168
1169 if (atalk_find_or_insert_socket(sk, addr))
1170 return -EADDRINUSE;
1171 }
1172
1173 sock_reset_flag(sk, SOCK_ZAPPED);
1174 return 0;
1175}
1176
1177/* Set the address we talk to */
1178static int atalk_connect(struct socket *sock, struct sockaddr *uaddr,
1179 int addr_len, int flags)
1180{
1181 struct sock *sk = sock->sk;
1182 struct atalk_sock *at = at_sk(sk);
1183 struct sockaddr_at *addr;
1184
1185 sk->sk_state = TCP_CLOSE;
1186 sock->state = SS_UNCONNECTED;
1187
1188 if (addr_len != sizeof(*addr))
1189 return -EINVAL;
1190
1191 addr = (struct sockaddr_at *)uaddr;
1192
1193 if (addr->sat_family != AF_APPLETALK)
1194 return -EAFNOSUPPORT;
1195
1196 if (addr->sat_addr.s_node == ATADDR_BCAST &&
1197 !sock_flag(sk, SOCK_BROADCAST)) {
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001198#if 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 printk(KERN_WARNING "%s is broken and did not set "
1200 "SO_BROADCAST. It will break when 2.2 is "
1201 "released.\n",
1202 current->comm);
1203#else
1204 return -EACCES;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001205#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207
1208 if (sock_flag(sk, SOCK_ZAPPED))
1209 if (atalk_autobind(sk) < 0)
1210 return -EBUSY;
1211
1212 if (!atrtr_get_dev(&addr->sat_addr))
1213 return -ENETUNREACH;
1214
1215 at->dest_port = addr->sat_port;
1216 at->dest_net = addr->sat_addr.s_net;
1217 at->dest_node = addr->sat_addr.s_node;
1218
1219 sock->state = SS_CONNECTED;
1220 sk->sk_state = TCP_ESTABLISHED;
1221 return 0;
1222}
1223
1224/*
1225 * Find the name of an AppleTalk socket. Just copy the right
1226 * fields into the sockaddr.
1227 */
1228static int atalk_getname(struct socket *sock, struct sockaddr *uaddr,
1229 int *uaddr_len, int peer)
1230{
1231 struct sockaddr_at sat;
1232 struct sock *sk = sock->sk;
1233 struct atalk_sock *at = at_sk(sk);
1234
1235 if (sock_flag(sk, SOCK_ZAPPED))
1236 if (atalk_autobind(sk) < 0)
1237 return -ENOBUFS;
1238
1239 *uaddr_len = sizeof(struct sockaddr_at);
1240
1241 if (peer) {
1242 if (sk->sk_state != TCP_ESTABLISHED)
1243 return -ENOTCONN;
1244
1245 sat.sat_addr.s_net = at->dest_net;
1246 sat.sat_addr.s_node = at->dest_node;
1247 sat.sat_port = at->dest_port;
1248 } else {
1249 sat.sat_addr.s_net = at->src_net;
1250 sat.sat_addr.s_node = at->src_node;
1251 sat.sat_port = at->src_port;
1252 }
1253
1254 sat.sat_family = AF_APPLETALK;
1255 memcpy(uaddr, &sat, sizeof(sat));
1256 return 0;
1257}
1258
1259#if defined(CONFIG_IPDDP) || defined(CONFIG_IPDDP_MODULE)
1260static __inline__ int is_ip_over_ddp(struct sk_buff *skb)
1261{
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001262 return skb->data[12] == 22;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
1265static int handle_ip_over_ddp(struct sk_buff *skb)
1266{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001267 struct net_device *dev = __dev_get_by_name(&init_net, "ipddp0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 struct net_device_stats *stats;
1269
1270 /* This needs to be able to handle ipddp"N" devices */
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001271 if (!dev)
1272 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001274 skb->protocol = htons(ETH_P_IP);
1275 skb_pull(skb, 13);
1276 skb->dev = dev;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001277 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Wang Chen524ad0a2008-11-12 23:39:10 -08001279 stats = netdev_priv(dev);
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001280 stats->rx_packets++;
1281 stats->rx_bytes += skb->len + 13;
1282 netif_rx(skb); /* Send the SKB up to a higher place. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return 0;
1284}
1285#else
1286/* make it easy for gcc to optimize this test out, i.e. kill the code */
1287#define is_ip_over_ddp(skb) 0
1288#define handle_ip_over_ddp(skb) 0
1289#endif
1290
1291static void atalk_route_packet(struct sk_buff *skb, struct net_device *dev,
Al Viro2a50f282006-09-26 21:22:08 -07001292 struct ddpehdr *ddp, __u16 len_hops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 int origlen)
1294{
1295 struct atalk_route *rt;
1296 struct atalk_addr ta;
1297
1298 /*
1299 * Don't route multicast, etc., packets, or packets sent to "this
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001300 * network"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 */
1302 if (skb->pkt_type != PACKET_HOST || !ddp->deh_dnet) {
1303 /*
1304 * FIXME:
1305 *
1306 * Can it ever happen that a packet is from a PPP iface and
1307 * needs to be broadcast onto the default network?
1308 */
1309 if (dev->type == ARPHRD_PPP)
1310 printk(KERN_DEBUG "AppleTalk: didn't forward broadcast "
1311 "packet received from PPP iface\n");
1312 goto free_it;
1313 }
1314
1315 ta.s_net = ddp->deh_dnet;
1316 ta.s_node = ddp->deh_dnode;
1317
1318 /* Route the packet */
1319 rt = atrtr_find(&ta);
Al Viro2a50f282006-09-26 21:22:08 -07001320 /* increment hops count */
1321 len_hops += 1 << 10;
1322 if (!rt || !(len_hops & (15 << 10)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 goto free_it;
Al Viro2a50f282006-09-26 21:22:08 -07001324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 /* FIXME: use skb->cb to be able to use shared skbs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 /*
1328 * Route goes through another gateway, so set the target to the
1329 * gateway instead.
1330 */
1331
1332 if (rt->flags & RTF_GATEWAY) {
1333 ta.s_net = rt->gateway.s_net;
1334 ta.s_node = rt->gateway.s_node;
1335 }
1336
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001337 /* Fix up skb->len field */
1338 skb_trim(skb, min_t(unsigned int, origlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 (rt->dev->hard_header_len +
Al Viro2a50f282006-09-26 21:22:08 -07001340 ddp_dl->header_length + (len_hops & 1023))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 /* FIXME: use skb->cb to be able to use shared skbs */
Al Viro2a50f282006-09-26 21:22:08 -07001343 ddp->deh_len_hops = htons(len_hops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 /*
1346 * Send the buffer onwards
1347 *
1348 * Now we must always be careful. If it's come from LocalTalk to
1349 * EtherTalk it might not fit
1350 *
1351 * Order matters here: If a packet has to be copied to make a new
1352 * headroom (rare hopefully) then it won't need unsharing.
1353 *
1354 * Note. ddp-> becomes invalid at the realloc.
1355 */
1356 if (skb_headroom(skb) < 22) {
1357 /* 22 bytes - 12 ether, 2 len, 3 802.2 5 snap */
1358 struct sk_buff *nskb = skb_realloc_headroom(skb, 32);
1359 kfree_skb(skb);
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001360 if (!nskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 goto out;
1362 skb = nskb;
1363 } else
1364 skb = skb_unshare(skb, GFP_ATOMIC);
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 /*
1367 * If the buffer didn't vanish into the lack of space bitbucket we can
1368 * send it.
1369 */
1370 if (skb && aarp_send_ddp(rt->dev, skb, &ta, NULL) == -1)
1371 goto free_it;
1372out:
1373 return;
1374free_it:
1375 kfree_skb(skb);
1376}
1377
1378/**
1379 * atalk_rcv - Receive a packet (in skb) from device dev
1380 * @skb - packet received
1381 * @dev - network device where the packet comes from
1382 * @pt - packet type
1383 *
1384 * Receive a packet (in skb) from device dev. This has come from the SNAP
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001385 * decoder, and on entry skb->transport_header is the DDP header, skb->len
1386 * is the DDP header, skb->len is the DDP length. The physical headers
1387 * have been extracted. PPP should probably pass frames marked as for this
1388 * layer. [ie ARPHRD_ETHERTALK]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 */
1390static int atalk_rcv(struct sk_buff *skb, struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001391 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 struct ddpehdr *ddp;
1394 struct sock *sock;
1395 struct atalk_iface *atif;
1396 struct sockaddr_at tosat;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001397 int origlen;
Al Viro2a50f282006-09-26 21:22:08 -07001398 __u16 len_hops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001400 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -07001401 goto freeit;
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 /* Don't mangle buffer if shared */
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001404 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 goto out;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 /* Size check and make sure header is contiguous */
1408 if (!pskb_may_pull(skb, sizeof(*ddp)))
1409 goto freeit;
1410
1411 ddp = ddp_hdr(skb);
1412
Al Viro2a50f282006-09-26 21:22:08 -07001413 len_hops = ntohs(ddp->deh_len_hops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 /* Trim buffer in case of stray trailing data */
1416 origlen = skb->len;
Al Viro2a50f282006-09-26 21:22:08 -07001417 skb_trim(skb, min_t(unsigned int, skb->len, len_hops & 1023));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 /*
1420 * Size check to see if ddp->deh_len was crap
1421 * (Otherwise we'll detonate most spectacularly
Jean Delvare75559c12007-04-04 23:52:46 -07001422 * in the middle of atalk_checksum() or recvmsg()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 */
Jean Delvare75559c12007-04-04 23:52:46 -07001424 if (skb->len < sizeof(*ddp) || skb->len < (len_hops & 1023)) {
1425 pr_debug("AppleTalk: dropping corrupted frame (deh_len=%u, "
1426 "skb->len=%u)\n", len_hops & 1023, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 goto freeit;
Jean Delvare75559c12007-04-04 23:52:46 -07001428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 /*
1431 * Any checksums. Note we don't do htons() on this == is assumed to be
1432 * valid for net byte orders all over the networking code...
1433 */
1434 if (ddp->deh_sum &&
Al Viro2a50f282006-09-26 21:22:08 -07001435 atalk_checksum(skb, len_hops & 1023) != ddp->deh_sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 /* Not a valid AppleTalk frame - dustbin time */
1437 goto freeit;
1438
1439 /* Check the packet is aimed at us */
1440 if (!ddp->deh_dnet) /* Net 0 is 'this network' */
1441 atif = atalk_find_anynet(ddp->deh_dnode, dev);
1442 else
1443 atif = atalk_find_interface(ddp->deh_dnet, ddp->deh_dnode);
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (!atif) {
Oliver Dawid64233bf2005-09-27 16:11:29 -07001446 /* Not ours, so we route the packet via the correct
1447 * AppleTalk iface
1448 */
Al Viro2a50f282006-09-26 21:22:08 -07001449 atalk_route_packet(skb, dev, ddp, len_hops, origlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 goto out;
1451 }
1452
1453 /* if IP over DDP is not selected this code will be optimized out */
1454 if (is_ip_over_ddp(skb))
1455 return handle_ip_over_ddp(skb);
1456 /*
1457 * Which socket - atalk_search_socket() looks for a *full match*
1458 * of the <net, node, port> tuple.
1459 */
1460 tosat.sat_addr.s_net = ddp->deh_dnet;
1461 tosat.sat_addr.s_node = ddp->deh_dnode;
1462 tosat.sat_port = ddp->deh_dport;
1463
1464 sock = atalk_search_socket(&tosat, atif);
1465 if (!sock) /* But not one of our sockets */
1466 goto freeit;
1467
1468 /* Queue packet (standard) */
1469 skb->sk = sock;
1470
1471 if (sock_queue_rcv_skb(sock, skb) < 0)
1472 goto freeit;
1473out:
1474 return 0;
1475freeit:
1476 kfree_skb(skb);
1477 goto out;
1478}
1479
1480/*
1481 * Receive a LocalTalk frame. We make some demands on the caller here.
1482 * Caller must provide enough headroom on the packet to pull the short
1483 * header and append a long one.
1484 */
1485static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001486 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001488 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -07001489 goto freeit;
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 /* Expand any short form frames */
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001492 if (skb_mac_header(skb)[2] == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 struct ddpehdr *ddp;
1494 /* Find our address */
1495 struct atalk_addr *ap = atalk_find_dev_addr(dev);
1496
Al Viro2a50f282006-09-26 21:22:08 -07001497 if (!ap || skb->len < sizeof(__be16) || skb->len > 1023)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 goto freeit;
1499
1500 /* Don't mangle buffer if shared */
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001501 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return 0;
1503
1504 /*
1505 * The push leaves us with a ddephdr not an shdr, and
1506 * handily the port bytes in the right place preset.
1507 */
1508 ddp = (struct ddpehdr *) skb_push(skb, sizeof(*ddp) - 4);
1509
1510 /* Now fill in the long header */
1511
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001512 /*
1513 * These two first. The mac overlays the new source/dest
1514 * network information so we MUST copy these before
1515 * we write the network numbers !
1516 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001518 ddp->deh_dnode = skb_mac_header(skb)[0]; /* From physical header */
1519 ddp->deh_snode = skb_mac_header(skb)[1]; /* From physical header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 ddp->deh_dnet = ap->s_net; /* Network number */
1522 ddp->deh_snet = ap->s_net;
1523 ddp->deh_sum = 0; /* No checksum */
1524 /*
1525 * Not sure about this bit...
1526 */
Al Viro2a50f282006-09-26 21:22:08 -07001527 /* Non routable, so force a drop if we slip up later */
1528 ddp->deh_len_hops = htons(skb->len + (DDP_MAXHOPS << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001530 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001532 return atalk_rcv(skb, dev, pt, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533freeit:
1534 kfree_skb(skb);
1535 return 0;
1536}
1537
1538static int atalk_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
1539 size_t len)
1540{
1541 struct sock *sk = sock->sk;
1542 struct atalk_sock *at = at_sk(sk);
1543 struct sockaddr_at *usat = (struct sockaddr_at *)msg->msg_name;
1544 int flags = msg->msg_flags;
1545 int loopback = 0;
1546 struct sockaddr_at local_satalk, gsat;
1547 struct sk_buff *skb;
1548 struct net_device *dev;
1549 struct ddpehdr *ddp;
1550 int size;
1551 struct atalk_route *rt;
1552 int err;
1553
1554 if (flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
1555 return -EINVAL;
1556
1557 if (len > DDP_MAXSZ)
1558 return -EMSGSIZE;
1559
1560 if (usat) {
1561 if (sock_flag(sk, SOCK_ZAPPED))
1562 if (atalk_autobind(sk) < 0)
1563 return -EBUSY;
1564
1565 if (msg->msg_namelen < sizeof(*usat) ||
1566 usat->sat_family != AF_APPLETALK)
1567 return -EINVAL;
1568
Alan Cox03ba9992009-03-27 00:27:18 -07001569 /* netatalk didn't implement this check */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (usat->sat_addr.s_node == ATADDR_BCAST &&
1571 !sock_flag(sk, SOCK_BROADCAST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574 } else {
1575 if (sk->sk_state != TCP_ESTABLISHED)
1576 return -ENOTCONN;
1577 usat = &local_satalk;
1578 usat->sat_family = AF_APPLETALK;
1579 usat->sat_port = at->dest_port;
1580 usat->sat_addr.s_node = at->dest_node;
1581 usat->sat_addr.s_net = at->dest_net;
1582 }
1583
1584 /* Build a packet */
1585 SOCK_DEBUG(sk, "SK %p: Got address.\n", sk);
1586
1587 /* For headers */
1588 size = sizeof(struct ddpehdr) + len + ddp_dl->header_length;
1589
1590 if (usat->sat_addr.s_net || usat->sat_addr.s_node == ATADDR_ANYNODE) {
1591 rt = atrtr_find(&usat->sat_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 } else {
1593 struct atalk_addr at_hint;
1594
1595 at_hint.s_node = 0;
1596 at_hint.s_net = at->src_net;
1597
1598 rt = atrtr_find(&at_hint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
Oliver Dawid64233bf2005-09-27 16:11:29 -07001600 if (!rt)
1601 return -ENETUNREACH;
1602
1603 dev = rt->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 SOCK_DEBUG(sk, "SK %p: Size needed %d, device %s\n",
1606 sk, size, dev->name);
1607
1608 size += dev->hard_header_len;
1609 skb = sock_alloc_send_skb(sk, size, (flags & MSG_DONTWAIT), &err);
1610 if (!skb)
1611 return err;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 skb->sk = sk;
1614 skb_reserve(skb, ddp_dl->header_length);
1615 skb_reserve(skb, dev->hard_header_len);
1616 skb->dev = dev;
1617
1618 SOCK_DEBUG(sk, "SK %p: Begin build.\n", sk);
1619
1620 ddp = (struct ddpehdr *)skb_put(skb, sizeof(struct ddpehdr));
Al Viro2a50f282006-09-26 21:22:08 -07001621 ddp->deh_len_hops = htons(len + sizeof(*ddp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 ddp->deh_dnet = usat->sat_addr.s_net;
1623 ddp->deh_snet = at->src_net;
1624 ddp->deh_dnode = usat->sat_addr.s_node;
1625 ddp->deh_snode = at->src_node;
1626 ddp->deh_dport = usat->sat_port;
1627 ddp->deh_sport = at->src_port;
1628
1629 SOCK_DEBUG(sk, "SK %p: Copy user data (%Zd bytes).\n", sk, len);
1630
1631 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1632 if (err) {
1633 kfree_skb(skb);
1634 return -EFAULT;
1635 }
1636
1637 if (sk->sk_no_check == 1)
1638 ddp->deh_sum = 0;
1639 else
1640 ddp->deh_sum = atalk_checksum(skb, len + sizeof(*ddp));
1641
1642 /*
1643 * Loopback broadcast packets to non gateway targets (ie routes
1644 * to group we are in)
1645 */
1646 if (ddp->deh_dnode == ATADDR_BCAST &&
1647 !(rt->flags & RTF_GATEWAY) && !(dev->flags & IFF_LOOPBACK)) {
1648 struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL);
1649
1650 if (skb2) {
1651 loopback = 1;
1652 SOCK_DEBUG(sk, "SK %p: send out(copy).\n", sk);
1653 if (aarp_send_ddp(dev, skb2,
1654 &usat->sat_addr, NULL) == -1)
1655 kfree_skb(skb2);
1656 /* else queued/sent above in the aarp queue */
1657 }
1658 }
1659
1660 if (dev->flags & IFF_LOOPBACK || loopback) {
1661 SOCK_DEBUG(sk, "SK %p: Loop back.\n", sk);
1662 /* loop back */
1663 skb_orphan(skb);
Oliver Dawid64233bf2005-09-27 16:11:29 -07001664 if (ddp->deh_dnode == ATADDR_BCAST) {
1665 struct atalk_addr at_lo;
1666
1667 at_lo.s_node = 0;
1668 at_lo.s_net = 0;
1669
1670 rt = atrtr_find(&at_lo);
1671 if (!rt) {
1672 kfree_skb(skb);
1673 return -ENETUNREACH;
1674 }
1675 dev = rt->dev;
1676 skb->dev = dev;
1677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 ddp_dl->request(ddp_dl, skb, dev->dev_addr);
1679 } else {
1680 SOCK_DEBUG(sk, "SK %p: send out.\n", sk);
1681 if (rt->flags & RTF_GATEWAY) {
1682 gsat.sat_addr = rt->gateway;
1683 usat = &gsat;
1684 }
1685
1686 if (aarp_send_ddp(dev, skb, &usat->sat_addr, NULL) == -1)
1687 kfree_skb(skb);
1688 /* else queued/sent above in the aarp queue */
1689 }
1690 SOCK_DEBUG(sk, "SK %p: Done write (%Zd).\n", sk, len);
1691
1692 return len;
1693}
1694
1695static int atalk_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
1696 size_t size, int flags)
1697{
1698 struct sock *sk = sock->sk;
1699 struct sockaddr_at *sat = (struct sockaddr_at *)msg->msg_name;
1700 struct ddpehdr *ddp;
1701 int copied = 0;
Al Viro2a50f282006-09-26 21:22:08 -07001702 int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 struct sk_buff *skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1705 flags & MSG_DONTWAIT, &err);
1706 if (!skb)
1707 return err;
1708
1709 /* FIXME: use skb->cb to be able to use shared skbs */
1710 ddp = ddp_hdr(skb);
Al Viro2a50f282006-09-26 21:22:08 -07001711 copied = ntohs(ddp->deh_len_hops) & 1023;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Al Viro2a50f282006-09-26 21:22:08 -07001713 if (sk->sk_type != SOCK_RAW) {
1714 offset = sizeof(*ddp);
1715 copied -= offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717
Al Viro2a50f282006-09-26 21:22:08 -07001718 if (copied > size) {
1719 copied = size;
1720 msg->msg_flags |= MSG_TRUNC;
1721 }
1722 err = skb_copy_datagram_iovec(skb, offset, msg->msg_iov, copied);
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (!err) {
1725 if (sat) {
1726 sat->sat_family = AF_APPLETALK;
1727 sat->sat_port = ddp->deh_sport;
1728 sat->sat_addr.s_node = ddp->deh_snode;
1729 sat->sat_addr.s_net = ddp->deh_snet;
1730 }
1731 msg->msg_namelen = sizeof(*sat);
1732 }
1733
1734 skb_free_datagram(sk, skb); /* Free the datagram. */
1735 return err ? : copied;
1736}
1737
1738
1739/*
1740 * AppleTalk ioctl calls.
1741 */
1742static int atalk_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1743{
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001744 int rc = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 struct sock *sk = sock->sk;
1746 void __user *argp = (void __user *)arg;
1747
1748 switch (cmd) {
1749 /* Protocol layer */
1750 case TIOCOUTQ: {
Eric Dumazet31e6d362009-06-17 19:05:41 -07001751 long amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 if (amount < 0)
1754 amount = 0;
1755 rc = put_user(amount, (int __user *)argp);
1756 break;
1757 }
1758 case TIOCINQ: {
1759 /*
1760 * These two are safe on a single CPU system as only
1761 * user tasks fiddle here
1762 */
1763 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
1764 long amount = 0;
1765
1766 if (skb)
1767 amount = skb->len - sizeof(struct ddpehdr);
1768 rc = put_user(amount, (int __user *)argp);
1769 break;
1770 }
1771 case SIOCGSTAMP:
1772 rc = sock_get_timestamp(sk, argp);
1773 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001774 case SIOCGSTAMPNS:
1775 rc = sock_get_timestampns(sk, argp);
1776 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 /* Routing */
1778 case SIOCADDRT:
1779 case SIOCDELRT:
1780 rc = -EPERM;
1781 if (capable(CAP_NET_ADMIN))
1782 rc = atrtr_ioctl(cmd, argp);
1783 break;
1784 /* Interface */
1785 case SIOCGIFADDR:
1786 case SIOCSIFADDR:
1787 case SIOCGIFBRDADDR:
1788 case SIOCATALKDIFADDR:
1789 case SIOCDIFADDR:
1790 case SIOCSARP: /* proxy AARP */
1791 case SIOCDARP: /* proxy AARP */
1792 rtnl_lock();
1793 rc = atif_ioctl(cmd, argp);
1794 rtnl_unlock();
1795 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797
1798 return rc;
1799}
1800
Petr Vandrovecf6c90b72006-03-27 23:39:31 -08001801
1802#ifdef CONFIG_COMPAT
1803static int atalk_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1804{
1805 /*
1806 * All Appletalk ioctls except SIOCATALKDIFADDR are standard. And
1807 * SIOCATALKDIFADDR is handled by upper layer as well, so there is
1808 * nothing to do. Eventually SIOCATALKDIFADDR should be moved
1809 * here so there is no generic SIOCPROTOPRIVATE translation in the
1810 * system.
1811 */
1812 return -ENOIOCTLCMD;
1813}
1814#endif
1815
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817static struct net_proto_family atalk_family_ops = {
1818 .family = PF_APPLETALK,
1819 .create = atalk_create,
1820 .owner = THIS_MODULE,
1821};
1822
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001823static const struct proto_ops SOCKOPS_WRAPPED(atalk_dgram_ops) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 .family = PF_APPLETALK,
1825 .owner = THIS_MODULE,
1826 .release = atalk_release,
1827 .bind = atalk_bind,
1828 .connect = atalk_connect,
1829 .socketpair = sock_no_socketpair,
1830 .accept = sock_no_accept,
1831 .getname = atalk_getname,
1832 .poll = datagram_poll,
1833 .ioctl = atalk_ioctl,
Petr Vandrovecf6c90b72006-03-27 23:39:31 -08001834#ifdef CONFIG_COMPAT
1835 .compat_ioctl = atalk_compat_ioctl,
1836#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 .listen = sock_no_listen,
1838 .shutdown = sock_no_shutdown,
1839 .setsockopt = sock_no_setsockopt,
1840 .getsockopt = sock_no_getsockopt,
1841 .sendmsg = atalk_sendmsg,
1842 .recvmsg = atalk_recvmsg,
1843 .mmap = sock_no_mmap,
1844 .sendpage = sock_no_sendpage,
1845};
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847SOCKOPS_WRAP(atalk_dgram, PF_APPLETALK);
1848
1849static struct notifier_block ddp_notifier = {
1850 .notifier_call = ddp_device_event,
1851};
1852
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001853static struct packet_type ltalk_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08001854 .type = cpu_to_be16(ETH_P_LOCALTALK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 .func = ltalk_rcv,
1856};
1857
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001858static struct packet_type ppptalk_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08001859 .type = cpu_to_be16(ETH_P_PPPTALK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 .func = atalk_rcv,
1861};
1862
1863static unsigned char ddp_snap_id[] = { 0x08, 0x00, 0x07, 0x80, 0x9B };
1864
1865/* Export symbols for use by drivers when AppleTalk is a module */
1866EXPORT_SYMBOL(aarp_send_ddp);
1867EXPORT_SYMBOL(atrtr_get_dev);
1868EXPORT_SYMBOL(atalk_find_dev_addr);
1869
Hannes Eder2db09602009-02-25 10:31:59 +00001870static const char atalk_err_snap[] __initconst =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 KERN_CRIT "Unable to register DDP with SNAP.\n";
1872
1873/* Called by proto.c on kernel start up */
1874static int __init atalk_init(void)
1875{
1876 int rc = proto_register(&ddp_proto, 0);
1877
1878 if (rc != 0)
1879 goto out;
1880
1881 (void)sock_register(&atalk_family_ops);
1882 ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv);
1883 if (!ddp_dl)
1884 printk(atalk_err_snap);
1885
1886 dev_add_pack(&ltalk_packet_type);
1887 dev_add_pack(&ppptalk_packet_type);
1888
1889 register_netdevice_notifier(&ddp_notifier);
1890 aarp_proto_init();
1891 atalk_proc_init();
1892 atalk_register_sysctl();
1893out:
1894 return rc;
1895}
1896module_init(atalk_init);
1897
1898/*
1899 * No explicit module reference count manipulation is needed in the
1900 * protocol. Socket layer sets module reference count for us
1901 * and interfaces reference counting is done
1902 * by the network device layer.
1903 *
1904 * Ergo, before the AppleTalk module can be removed, all AppleTalk
1905 * sockets be closed from user space.
1906 */
1907static void __exit atalk_exit(void)
1908{
1909#ifdef CONFIG_SYSCTL
1910 atalk_unregister_sysctl();
1911#endif /* CONFIG_SYSCTL */
1912 atalk_proc_exit();
1913 aarp_cleanup_module(); /* General aarp clean-up. */
1914 unregister_netdevice_notifier(&ddp_notifier);
1915 dev_remove_pack(&ltalk_packet_type);
1916 dev_remove_pack(&ppptalk_packet_type);
1917 unregister_snap_client(ddp_dl);
1918 sock_unregister(PF_APPLETALK);
1919 proto_unregister(&ddp_proto);
1920}
1921module_exit(atalk_exit);
1922
1923MODULE_LICENSE("GPL");
Alan Cox113aa832008-10-13 19:01:08 -07001924MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925MODULE_DESCRIPTION("AppleTalk 0.20\n");
1926MODULE_ALIAS_NETPROTO(PF_APPLETALK);