1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * DDP: An implementation of the AppleTalk DDP protocol for
6 * Alan Cox <alan@lxorguk.ukuu.org.uk>
8 * With more than a little assistance from
10 * Wesley Craig <netatalk@umich.edu>
13 * Neil Horman : Added missing device ioctls
14 * Michael Callahan : Made routing work
15 * Wesley Craig : Fix probing to listen to a
17 * Alan Cox : Added send/recvmsg support
18 * Alan Cox : Moved at. to protinfo in
20 * Alan Cox : Added firewall hooks.
21 * Alan Cox : Supports new ARPHRD_LOOPBACK
22 * Christer Weinigel : Routing and /proc fixes.
23 * Bradford Johnson : LocalTalk.
24 * Tom Dyas : Module support.
25 * Alan Cox : Hooks for PPP (based on the
27 * Alan Cox : Posix bits
28 * Alan Cox/Mike Freeman : Possible fix to NBP problems
29 * Bradford Johnson : IP-over-DDP (experimental)
30 * Jay Schulist : Moved IP-over-DDP to its own
31 * driver file. (ipddp.c & ipddp.h)
32 * Jay Schulist : Made work as module with
33 * AppleTalk drivers, cleaned it.
34 * Rob Newberry : Added proxy AARP and AARP
35 * procfs, moved probing to AARP
38 * Michael Zuelsdorff : fix for net.0 packets. don't
39 * allow illegal ether/tokentalk
40 * port assignment. we lose a
41 * valid localtalk port as a
43 * Arnaldo C. de Melo : Cleanup, in preparation for
44 * shared skb support 8)
45 * Arnaldo C. de Melo : Move proc stuff to atalk_proc.c,
49 #include <linux/capability.h>
50 #include <linux/module.h>
51 #include <linux/if_arp.h>
52 #include <linux/termios.h> /* For TIOCOUTQ/INQ */
53 #include <linux/compat.h>
54 #include <linux/slab.h>
55 #include <net/datalink.h>
56 #include <net/psnap.h>
58 #include <net/tcp_states.h>
59 #include <net/route.h>
60 #include <net/compat.h>
61 #include <linux/atalk.h>
62 #include <linux/highmem.h>
64 struct datalink_proto
*ddp_dl
, *aarp_dl
;
65 static const struct proto_ops atalk_dgram_ops
;
67 /**************************************************************************\
69 * Handlers for the socket list. *
71 \**************************************************************************/
73 HLIST_HEAD(atalk_sockets
);
74 DEFINE_RWLOCK(atalk_sockets_lock
);
76 static inline void __atalk_insert_socket(struct sock
*sk
)
78 sk_add_node(sk
, &atalk_sockets
);
81 static inline void atalk_remove_socket(struct sock
*sk
)
83 write_lock_bh(&atalk_sockets_lock
);
85 write_unlock_bh(&atalk_sockets_lock
);
88 static struct sock
*atalk_search_socket(struct sockaddr_at
*to
,
89 struct atalk_iface
*atif
)
91 struct sock
*def_socket
= NULL
;
94 read_lock_bh(&atalk_sockets_lock
);
95 sk_for_each(s
, &atalk_sockets
) {
96 struct atalk_sock
*at
= at_sk(s
);
98 if (to
->sat_port
!= at
->src_port
)
101 if (to
->sat_addr
.s_net
== ATADDR_ANYNET
&&
102 to
->sat_addr
.s_node
== ATADDR_BCAST
) {
103 if (atif
->address
.s_node
== at
->src_node
&&
104 atif
->address
.s_net
== at
->src_net
) {
105 /* This socket's address matches the address of the interface
106 * that received the packet -- use it
111 /* Continue searching for a socket matching the interface address,
112 * but use this socket by default if no other one is found
117 if (to
->sat_addr
.s_net
== at
->src_net
&&
118 (to
->sat_addr
.s_node
== at
->src_node
||
119 to
->sat_addr
.s_node
== ATADDR_BCAST
||
120 to
->sat_addr
.s_node
== ATADDR_ANYNODE
))
123 /* XXXX.0 -- we got a request for this router. make sure
124 * that the node is appropriately set. */
125 if (to
->sat_addr
.s_node
== ATADDR_ANYNODE
&&
126 to
->sat_addr
.s_net
!= ATADDR_ANYNET
&&
127 atif
->address
.s_node
== at
->src_node
) {
128 to
->sat_addr
.s_node
= atif
->address
.s_node
;
134 read_unlock_bh(&atalk_sockets_lock
);
139 * atalk_find_or_insert_socket - Try to find a socket matching ADDR
140 * @sk: socket to insert in the list if it is not there already
141 * @sat: address to search for
143 * Try to find a socket matching ADDR in the socket list, if found then return
144 * it. If not, insert SK into the socket list.
146 * This entire operation must execute atomically.
148 static struct sock
*atalk_find_or_insert_socket(struct sock
*sk
,
149 struct sockaddr_at
*sat
)
152 struct atalk_sock
*at
;
154 write_lock_bh(&atalk_sockets_lock
);
155 sk_for_each(s
, &atalk_sockets
) {
158 if (at
->src_net
== sat
->sat_addr
.s_net
&&
159 at
->src_node
== sat
->sat_addr
.s_node
&&
160 at
->src_port
== sat
->sat_port
)
164 __atalk_insert_socket(sk
); /* Wheee, it's free, assign and insert. */
166 write_unlock_bh(&atalk_sockets_lock
);
170 static void atalk_destroy_timer(struct timer_list
*t
)
172 struct sock
*sk
= from_timer(sk
, t
, sk_timer
);
174 if (sk_has_allocations(sk
)) {
175 sk
->sk_timer
.expires
= jiffies
+ SOCK_DESTROY_TIME
;
176 add_timer(&sk
->sk_timer
);
181 static inline void atalk_destroy_socket(struct sock
*sk
)
183 atalk_remove_socket(sk
);
184 skb_queue_purge(&sk
->sk_receive_queue
);
186 if (sk_has_allocations(sk
)) {
187 timer_setup(&sk
->sk_timer
, atalk_destroy_timer
, 0);
188 sk
->sk_timer
.expires
= jiffies
+ SOCK_DESTROY_TIME
;
189 add_timer(&sk
->sk_timer
);
194 /**************************************************************************\
196 * Routing tables for the AppleTalk socket layer. *
198 \**************************************************************************/
200 /* Anti-deadlock ordering is atalk_routes_lock --> iface_lock -DaveM */
201 struct atalk_route
*atalk_routes
;
202 DEFINE_RWLOCK(atalk_routes_lock
);
204 struct atalk_iface
*atalk_interfaces
;
205 DEFINE_RWLOCK(atalk_interfaces_lock
);
207 /* For probing devices or in a routerless network */
208 struct atalk_route atrtr_default
;
210 /* AppleTalk interface control */
212 * Drop a device. Doesn't drop any of its routes - that is the caller's
213 * problem. Called when we down the interface or delete the address.
215 static void atif_drop_device(struct net_device
*dev
)
217 struct atalk_iface
**iface
= &atalk_interfaces
;
218 struct atalk_iface
*tmp
;
220 write_lock_bh(&atalk_interfaces_lock
);
221 while ((tmp
= *iface
) != NULL
) {
222 if (tmp
->dev
== dev
) {
226 dev
->atalk_ptr
= NULL
;
230 write_unlock_bh(&atalk_interfaces_lock
);
233 static struct atalk_iface
*atif_add_device(struct net_device
*dev
,
234 struct atalk_addr
*sa
)
236 struct atalk_iface
*iface
= kzalloc(sizeof(*iface
), GFP_KERNEL
);
243 dev
->atalk_ptr
= iface
;
244 iface
->address
= *sa
;
247 write_lock_bh(&atalk_interfaces_lock
);
248 iface
->next
= atalk_interfaces
;
249 atalk_interfaces
= iface
;
250 write_unlock_bh(&atalk_interfaces_lock
);
255 /* Perform phase 2 AARP probing on our tentative address */
256 static int atif_probe_device(struct atalk_iface
*atif
)
258 int netrange
= ntohs(atif
->nets
.nr_lastnet
) -
259 ntohs(atif
->nets
.nr_firstnet
) + 1;
260 int probe_net
= ntohs(atif
->address
.s_net
);
261 int probe_node
= atif
->address
.s_node
;
264 /* Offset the network we start probing with */
265 if (probe_net
== ATADDR_ANYNET
) {
266 probe_net
= ntohs(atif
->nets
.nr_firstnet
);
268 probe_net
+= jiffies
% netrange
;
270 if (probe_node
== ATADDR_ANYNODE
)
271 probe_node
= jiffies
& 0xFF;
273 /* Scan the networks */
274 atif
->status
|= ATIF_PROBE
;
275 for (netct
= 0; netct
<= netrange
; netct
++) {
276 /* Sweep the available nodes from a given start */
277 atif
->address
.s_net
= htons(probe_net
);
278 for (nodect
= 0; nodect
< 256; nodect
++) {
279 atif
->address
.s_node
= (nodect
+ probe_node
) & 0xFF;
280 if (atif
->address
.s_node
> 0 &&
281 atif
->address
.s_node
< 254) {
282 /* Probe a proposed address */
283 aarp_probe_network(atif
);
285 if (!(atif
->status
& ATIF_PROBE_FAIL
)) {
286 atif
->status
&= ~ATIF_PROBE
;
290 atif
->status
&= ~ATIF_PROBE_FAIL
;
293 if (probe_net
> ntohs(atif
->nets
.nr_lastnet
))
294 probe_net
= ntohs(atif
->nets
.nr_firstnet
);
296 atif
->status
&= ~ATIF_PROBE
;
298 return -EADDRINUSE
; /* Network is full... */
302 /* Perform AARP probing for a proxy address */
303 static int atif_proxy_probe_device(struct atalk_iface
*atif
,
304 struct atalk_addr
*proxy_addr
)
306 int netrange
= ntohs(atif
->nets
.nr_lastnet
) -
307 ntohs(atif
->nets
.nr_firstnet
) + 1;
308 /* we probe the interface's network */
309 int probe_net
= ntohs(atif
->address
.s_net
);
310 int probe_node
= ATADDR_ANYNODE
; /* we'll take anything */
313 /* Offset the network we start probing with */
314 if (probe_net
== ATADDR_ANYNET
) {
315 probe_net
= ntohs(atif
->nets
.nr_firstnet
);
317 probe_net
+= jiffies
% netrange
;
320 if (probe_node
== ATADDR_ANYNODE
)
321 probe_node
= jiffies
& 0xFF;
323 /* Scan the networks */
324 for (netct
= 0; netct
<= netrange
; netct
++) {
325 /* Sweep the available nodes from a given start */
326 proxy_addr
->s_net
= htons(probe_net
);
327 for (nodect
= 0; nodect
< 256; nodect
++) {
328 proxy_addr
->s_node
= (nodect
+ probe_node
) & 0xFF;
329 if (proxy_addr
->s_node
> 0 &&
330 proxy_addr
->s_node
< 254) {
331 /* Tell AARP to probe a proposed address */
332 int ret
= aarp_proxy_probe_network(atif
,
335 if (ret
!= -EADDRINUSE
)
340 if (probe_net
> ntohs(atif
->nets
.nr_lastnet
))
341 probe_net
= ntohs(atif
->nets
.nr_firstnet
);
344 return -EADDRINUSE
; /* Network is full... */
348 struct atalk_addr
*atalk_find_dev_addr(struct net_device
*dev
)
350 struct atalk_iface
*iface
= dev
->atalk_ptr
;
351 return iface
? &iface
->address
: NULL
;
354 static struct atalk_addr
*atalk_find_primary(void)
356 struct atalk_iface
*fiface
= NULL
;
357 struct atalk_addr
*retval
;
358 struct atalk_iface
*iface
;
361 * Return a point-to-point interface only if
362 * there is no non-ptp interface available.
364 read_lock_bh(&atalk_interfaces_lock
);
365 for (iface
= atalk_interfaces
; iface
; iface
= iface
->next
) {
366 if (!fiface
&& !(iface
->dev
->flags
& IFF_LOOPBACK
))
368 if (!(iface
->dev
->flags
& (IFF_LOOPBACK
| IFF_POINTOPOINT
))) {
369 retval
= &iface
->address
;
375 retval
= &fiface
->address
;
376 else if (atalk_interfaces
)
377 retval
= &atalk_interfaces
->address
;
381 read_unlock_bh(&atalk_interfaces_lock
);
386 * Find a match for 'any network' - ie any of our interfaces with that
387 * node number will do just nicely.
389 static struct atalk_iface
*atalk_find_anynet(int node
, struct net_device
*dev
)
391 struct atalk_iface
*iface
= dev
->atalk_ptr
;
393 if (!iface
|| iface
->status
& ATIF_PROBE
)
396 if (node
!= ATADDR_BCAST
&&
397 iface
->address
.s_node
!= node
&&
398 node
!= ATADDR_ANYNODE
)
407 /* Find a match for a specific network:node pair */
408 static struct atalk_iface
*atalk_find_interface(__be16 net
, int node
)
410 struct atalk_iface
*iface
;
412 read_lock_bh(&atalk_interfaces_lock
);
413 for (iface
= atalk_interfaces
; iface
; iface
= iface
->next
) {
414 if ((node
== ATADDR_BCAST
||
415 node
== ATADDR_ANYNODE
||
416 iface
->address
.s_node
== node
) &&
417 iface
->address
.s_net
== net
&&
418 !(iface
->status
& ATIF_PROBE
))
421 /* XXXX.0 -- net.0 returns the iface associated with net */
422 if (node
== ATADDR_ANYNODE
&& net
!= ATADDR_ANYNET
&&
423 ntohs(iface
->nets
.nr_firstnet
) <= ntohs(net
) &&
424 ntohs(net
) <= ntohs(iface
->nets
.nr_lastnet
))
427 read_unlock_bh(&atalk_interfaces_lock
);
433 * Find a route for an AppleTalk packet. This ought to get cached in
434 * the socket (later on...). We know about host routes and the fact
435 * that a route must be direct to broadcast.
437 static struct atalk_route
*atrtr_find(struct atalk_addr
*target
)
440 * we must search through all routes unless we find a
441 * host route, because some host routes might overlap
444 struct atalk_route
*net_route
= NULL
;
445 struct atalk_route
*r
;
447 read_lock_bh(&atalk_routes_lock
);
448 for (r
= atalk_routes
; r
; r
= r
->next
) {
449 if (!(r
->flags
& RTF_UP
))
452 if (r
->target
.s_net
== target
->s_net
) {
453 if (r
->flags
& RTF_HOST
) {
455 * if this host route is for the target,
458 if (r
->target
.s_node
== target
->s_node
)
462 * this route will work if there isn't a
463 * direct host route, so cache it
470 * if we found a network route but not a direct host
471 * route, then return it
475 else if (atrtr_default
.dev
)
477 else /* No route can be found */
480 read_unlock_bh(&atalk_routes_lock
);
486 * Given an AppleTalk network, find the device to use. This can be
489 struct net_device
*atrtr_get_dev(struct atalk_addr
*sa
)
491 struct atalk_route
*atr
= atrtr_find(sa
);
492 return atr
? atr
->dev
: NULL
;
495 /* Set up a default router */
496 static void atrtr_set_default(struct net_device
*dev
)
498 atrtr_default
.dev
= dev
;
499 atrtr_default
.flags
= RTF_UP
;
500 atrtr_default
.gateway
.s_net
= htons(0);
501 atrtr_default
.gateway
.s_node
= 0;
505 * Add a router. Basically make sure it looks valid and stuff the
506 * entry in the list. While it uses netranges we always set them to one
507 * entry to work like netatalk.
509 static int atrtr_create(struct rtentry
*r
, struct net_device
*devhint
)
511 struct sockaddr_at
*ta
= (struct sockaddr_at
*)&r
->rt_dst
;
512 struct sockaddr_at
*ga
= (struct sockaddr_at
*)&r
->rt_gateway
;
513 struct atalk_route
*rt
;
514 struct atalk_iface
*iface
, *riface
;
515 int retval
= -EINVAL
;
518 * Fixme: Raise/Lower a routing change semaphore for these
522 /* Validate the request */
523 if (ta
->sat_family
!= AF_APPLETALK
||
524 (!devhint
&& ga
->sat_family
!= AF_APPLETALK
))
527 /* Now walk the routing table and make our decisions */
528 write_lock_bh(&atalk_routes_lock
);
529 for (rt
= atalk_routes
; rt
; rt
= rt
->next
) {
530 if (r
->rt_flags
!= rt
->flags
)
533 if (ta
->sat_addr
.s_net
== rt
->target
.s_net
) {
534 if (!(rt
->flags
& RTF_HOST
))
536 if (ta
->sat_addr
.s_node
== rt
->target
.s_node
)
544 read_lock_bh(&atalk_interfaces_lock
);
545 for (iface
= atalk_interfaces
; iface
; iface
= iface
->next
) {
547 ntohs(ga
->sat_addr
.s_net
) >=
548 ntohs(iface
->nets
.nr_firstnet
) &&
549 ntohs(ga
->sat_addr
.s_net
) <=
550 ntohs(iface
->nets
.nr_lastnet
))
553 if (ga
->sat_addr
.s_net
== iface
->address
.s_net
&&
554 ga
->sat_addr
.s_node
== iface
->address
.s_node
)
557 read_unlock_bh(&atalk_interfaces_lock
);
559 retval
= -ENETUNREACH
;
563 devhint
= riface
->dev
;
567 rt
= kzalloc(sizeof(*rt
), GFP_ATOMIC
);
573 rt
->next
= atalk_routes
;
577 /* Fill in the routing entry */
578 rt
->target
= ta
->sat_addr
;
581 rt
->flags
= r
->rt_flags
;
582 rt
->gateway
= ga
->sat_addr
;
586 write_unlock_bh(&atalk_routes_lock
);
591 /* Delete a route. Find it and discard it */
592 static int atrtr_delete(struct atalk_addr
*addr
)
594 struct atalk_route
**r
= &atalk_routes
;
596 struct atalk_route
*tmp
;
598 write_lock_bh(&atalk_routes_lock
);
599 while ((tmp
= *r
) != NULL
) {
600 if (tmp
->target
.s_net
== addr
->s_net
&&
601 (!(tmp
->flags
&RTF_GATEWAY
) ||
602 tmp
->target
.s_node
== addr
->s_node
)) {
612 write_unlock_bh(&atalk_routes_lock
);
617 * Called when a device is downed. Just throw away any routes
620 static void atrtr_device_down(struct net_device
*dev
)
622 struct atalk_route
**r
= &atalk_routes
;
623 struct atalk_route
*tmp
;
625 write_lock_bh(&atalk_routes_lock
);
626 while ((tmp
= *r
) != NULL
) {
627 if (tmp
->dev
== dev
) {
634 write_unlock_bh(&atalk_routes_lock
);
636 if (atrtr_default
.dev
== dev
)
637 atrtr_set_default(NULL
);
640 /* Actually down the interface */
641 static inline void atalk_dev_down(struct net_device
*dev
)
643 atrtr_device_down(dev
); /* Remove all routes for the device */
644 aarp_device_down(dev
); /* Remove AARP entries for the device */
645 atif_drop_device(dev
); /* Remove the device */
649 * A device event has occurred. Watch for devices going down and
650 * delete our use of them (iface and route).
652 static int ddp_device_event(struct notifier_block
*this, unsigned long event
,
655 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
657 if (!net_eq(dev_net(dev
), &init_net
))
660 if (event
== NETDEV_DOWN
)
661 /* Discard any use of this */
667 /* ioctl calls. Shouldn't even need touching */
668 /* Device configuration ioctl calls */
669 static int atif_ioctl(int cmd
, void __user
*arg
)
671 static char aarp_mcast
[6] = { 0x09, 0x00, 0x00, 0xFF, 0xFF, 0xFF };
673 struct atalk_netrange
*nr
;
674 struct sockaddr_at
*sa
;
675 struct net_device
*dev
;
676 struct atalk_iface
*atif
;
679 struct rtentry rtdef
;
682 if (get_user_ifreq(&atreq
, NULL
, arg
))
685 dev
= __dev_get_by_name(&init_net
, atreq
.ifr_name
);
689 sa
= (struct sockaddr_at
*)&atreq
.ifr_addr
;
690 atif
= atalk_find_dev(dev
);
694 if (!capable(CAP_NET_ADMIN
))
696 if (sa
->sat_family
!= AF_APPLETALK
)
698 if (dev
->type
!= ARPHRD_ETHER
&&
699 dev
->type
!= ARPHRD_LOOPBACK
&&
700 dev
->type
!= ARPHRD_LOCALTLK
&&
701 dev
->type
!= ARPHRD_PPP
)
702 return -EPROTONOSUPPORT
;
704 nr
= (struct atalk_netrange
*)&sa
->sat_zero
[0];
708 * if this is a point-to-point iface, and we already
709 * have an iface for this AppleTalk address, then we
710 * should not add a route
712 if ((dev
->flags
& IFF_POINTOPOINT
) &&
713 atalk_find_interface(sa
->sat_addr
.s_net
,
714 sa
->sat_addr
.s_node
)) {
715 printk(KERN_DEBUG
"AppleTalk: point-to-point "
716 "interface added with "
717 "existing address\n");
722 * Phase 1 is fine on LocalTalk but we don't do
723 * EtherTalk phase 1. Anyone wanting to add it, go ahead.
725 if (dev
->type
== ARPHRD_ETHER
&& nr
->nr_phase
!= 2)
726 return -EPROTONOSUPPORT
;
727 if (sa
->sat_addr
.s_node
== ATADDR_BCAST
||
728 sa
->sat_addr
.s_node
== 254)
731 /* Already setting address */
732 if (atif
->status
& ATIF_PROBE
)
735 atif
->address
.s_net
= sa
->sat_addr
.s_net
;
736 atif
->address
.s_node
= sa
->sat_addr
.s_node
;
737 atrtr_device_down(dev
); /* Flush old routes */
739 atif
= atif_add_device(dev
, &sa
->sat_addr
);
746 * Check if the chosen address is used. If so we
747 * error and atalkd will try another.
750 if (!(dev
->flags
& IFF_LOOPBACK
) &&
751 !(dev
->flags
& IFF_POINTOPOINT
) &&
752 atif_probe_device(atif
) < 0) {
753 atif_drop_device(dev
);
757 /* Hey it worked - add the direct routes */
758 sa
= (struct sockaddr_at
*)&rtdef
.rt_gateway
;
759 sa
->sat_family
= AF_APPLETALK
;
760 sa
->sat_addr
.s_net
= atif
->address
.s_net
;
761 sa
->sat_addr
.s_node
= atif
->address
.s_node
;
762 sa
= (struct sockaddr_at
*)&rtdef
.rt_dst
;
763 rtdef
.rt_flags
= RTF_UP
;
764 sa
->sat_family
= AF_APPLETALK
;
765 sa
->sat_addr
.s_node
= ATADDR_ANYNODE
;
766 if (dev
->flags
& IFF_LOOPBACK
||
767 dev
->flags
& IFF_POINTOPOINT
)
768 rtdef
.rt_flags
|= RTF_HOST
;
770 /* Routerless initial state */
771 if (nr
->nr_firstnet
== htons(0) &&
772 nr
->nr_lastnet
== htons(0xFFFE)) {
773 sa
->sat_addr
.s_net
= atif
->address
.s_net
;
774 atrtr_create(&rtdef
, dev
);
775 atrtr_set_default(dev
);
777 limit
= ntohs(nr
->nr_lastnet
);
778 if (limit
- ntohs(nr
->nr_firstnet
) > 4096) {
779 printk(KERN_WARNING
"Too many routes/"
784 for (ct
= ntohs(nr
->nr_firstnet
);
786 sa
->sat_addr
.s_net
= htons(ct
);
787 atrtr_create(&rtdef
, dev
);
790 dev_mc_add_global(dev
, aarp_mcast
);
795 return -EADDRNOTAVAIL
;
797 sa
->sat_family
= AF_APPLETALK
;
798 sa
->sat_addr
= atif
->address
;
803 return -EADDRNOTAVAIL
;
805 sa
->sat_family
= AF_APPLETALK
;
806 sa
->sat_addr
.s_net
= atif
->address
.s_net
;
807 sa
->sat_addr
.s_node
= ATADDR_BCAST
;
810 case SIOCATALKDIFADDR
:
812 if (!capable(CAP_NET_ADMIN
))
814 if (sa
->sat_family
!= AF_APPLETALK
)
820 if (!capable(CAP_NET_ADMIN
))
822 if (sa
->sat_family
!= AF_APPLETALK
)
825 * for now, we only support proxy AARP on ELAP;
826 * we should be able to do it for LocalTalk, too.
828 if (dev
->type
!= ARPHRD_ETHER
)
829 return -EPROTONOSUPPORT
;
832 * atif points to the current interface on this network;
833 * we aren't concerned about its current status (at
834 * least for now), but it has all the settings about
835 * the network we're going to probe. Consequently, it
839 return -EADDRNOTAVAIL
;
841 nr
= (struct atalk_netrange
*)&(atif
->nets
);
843 * Phase 1 is fine on Localtalk but we don't do
844 * Ethertalk phase 1. Anyone wanting to add it, go ahead.
846 if (dev
->type
== ARPHRD_ETHER
&& nr
->nr_phase
!= 2)
847 return -EPROTONOSUPPORT
;
849 if (sa
->sat_addr
.s_node
== ATADDR_BCAST
||
850 sa
->sat_addr
.s_node
== 254)
854 * Check if the chosen address is used. If so we
855 * error and ATCP will try another.
857 if (atif_proxy_probe_device(atif
, &(sa
->sat_addr
)) < 0)
861 * We now have an address on the local network, and
862 * the AARP code will defend it for us until we take it
863 * down. We don't set up any routes right now, because
864 * ATCP will install them manually via SIOCADDRT.
869 if (!capable(CAP_NET_ADMIN
))
871 if (sa
->sat_family
!= AF_APPLETALK
)
874 return -EADDRNOTAVAIL
;
876 /* give to aarp module to remove proxy entry */
877 aarp_proxy_remove(atif
->dev
, &(sa
->sat_addr
));
881 return put_user_ifreq(&atreq
, arg
);
884 static int atrtr_ioctl_addrt(struct rtentry
*rt
)
886 struct net_device
*dev
= NULL
;
891 if (copy_from_user(name
, rt
->rt_dev
, IFNAMSIZ
-1))
893 name
[IFNAMSIZ
-1] = '\0';
895 dev
= __dev_get_by_name(&init_net
, name
);
899 return atrtr_create(rt
, dev
);
902 /* Routing ioctl() calls */
903 static int atrtr_ioctl(unsigned int cmd
, void __user
*arg
)
907 if (copy_from_user(&rt
, arg
, sizeof(rt
)))
912 if (rt
.rt_dst
.sa_family
!= AF_APPLETALK
)
914 return atrtr_delete(&((struct sockaddr_at
*)
915 &rt
.rt_dst
)->sat_addr
);
918 return atrtr_ioctl_addrt(&rt
);
923 /**************************************************************************\
925 * Handling for system calls applied via the various interfaces to an *
926 * AppleTalk socket object. *
928 \**************************************************************************/
931 * Checksum: This is 'optional'. It's quite likely also a good
932 * candidate for assembler hackery 8)
934 static unsigned long atalk_sum_partial(const unsigned char *data
,
935 int len
, unsigned long sum
)
937 /* This ought to be unwrapped neatly. I'll trust gcc for now */
945 /* Checksum skb data -- similar to skb_checksum */
946 static unsigned long atalk_sum_skb(const struct sk_buff
*skb
, int offset
,
947 int len
, unsigned long sum
)
949 int start
= skb_headlen(skb
);
950 struct sk_buff
*frag_iter
;
953 /* checksum stuff in header space */
954 if ((copy
= start
- offset
) > 0) {
957 sum
= atalk_sum_partial(skb
->data
+ offset
, copy
, sum
);
958 if ((len
-= copy
) == 0)
964 /* checksum stuff in frags */
965 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
967 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
968 WARN_ON(start
> offset
+ len
);
970 end
= start
+ skb_frag_size(frag
);
971 if ((copy
= end
- offset
) > 0) {
976 vaddr
= kmap_atomic(skb_frag_page(frag
));
977 sum
= atalk_sum_partial(vaddr
+ skb_frag_off(frag
) +
978 offset
- start
, copy
, sum
);
979 kunmap_atomic(vaddr
);
988 skb_walk_frags(skb
, frag_iter
) {
991 WARN_ON(start
> offset
+ len
);
993 end
= start
+ frag_iter
->len
;
994 if ((copy
= end
- offset
) > 0) {
997 sum
= atalk_sum_skb(frag_iter
, offset
- start
,
999 if ((len
-= copy
) == 0)
1011 static __be16
atalk_checksum(const struct sk_buff
*skb
, int len
)
1015 /* skip header 4 bytes */
1016 sum
= atalk_sum_skb(skb
, 4, len
-4, 0);
1018 /* Use 0xFFFF for 0. 0 itself means none */
1019 return sum
? htons((unsigned short)sum
) : htons(0xFFFF);
1022 static struct proto ddp_proto
= {
1024 .owner
= THIS_MODULE
,
1025 .obj_size
= sizeof(struct atalk_sock
),
1029 * Create a socket. Initialise the socket, blank the addresses
1032 static int atalk_create(struct net
*net
, struct socket
*sock
, int protocol
,
1036 int rc
= -ESOCKTNOSUPPORT
;
1038 if (!net_eq(net
, &init_net
))
1039 return -EAFNOSUPPORT
;
1042 * We permit SOCK_DGRAM and RAW is an extension. It is trivial to do
1043 * and gives you the full ELAP frame. Should be handy for CAP 8)
1045 if (sock
->type
!= SOCK_RAW
&& sock
->type
!= SOCK_DGRAM
)
1049 if (sock
->type
== SOCK_RAW
&& !kern
&& !capable(CAP_NET_RAW
))
1053 sk
= sk_alloc(net
, PF_APPLETALK
, GFP_KERNEL
, &ddp_proto
, kern
);
1057 sock
->ops
= &atalk_dgram_ops
;
1058 sock_init_data(sock
, sk
);
1060 /* Checksums on by default */
1061 sock_set_flag(sk
, SOCK_ZAPPED
);
1066 /* Free a socket. No work needed */
1067 static int atalk_release(struct socket
*sock
)
1069 struct sock
*sk
= sock
->sk
;
1077 atalk_destroy_socket(sk
);
1086 * atalk_pick_and_bind_port - Pick a source port when one is not given
1087 * @sk: socket to insert into the tables
1088 * @sat: address to search for
1090 * Pick a source port when one is not given. If we can find a suitable free
1091 * one, we insert the socket into the tables using it.
1093 * This whole operation must be atomic.
1095 static int atalk_pick_and_bind_port(struct sock
*sk
, struct sockaddr_at
*sat
)
1099 write_lock_bh(&atalk_sockets_lock
);
1101 for (sat
->sat_port
= ATPORT_RESERVED
;
1102 sat
->sat_port
< ATPORT_LAST
;
1106 sk_for_each(s
, &atalk_sockets
) {
1107 struct atalk_sock
*at
= at_sk(s
);
1109 if (at
->src_net
== sat
->sat_addr
.s_net
&&
1110 at
->src_node
== sat
->sat_addr
.s_node
&&
1111 at
->src_port
== sat
->sat_port
)
1115 /* Wheee, it's free, assign and insert. */
1116 __atalk_insert_socket(sk
);
1117 at_sk(sk
)->src_port
= sat
->sat_port
;
1126 write_unlock_bh(&atalk_sockets_lock
);
1130 static int atalk_autobind(struct sock
*sk
)
1132 struct atalk_sock
*at
= at_sk(sk
);
1133 struct sockaddr_at sat
;
1134 struct atalk_addr
*ap
= atalk_find_primary();
1135 int n
= -EADDRNOTAVAIL
;
1137 if (!ap
|| ap
->s_net
== htons(ATADDR_ANYNET
))
1140 at
->src_net
= sat
.sat_addr
.s_net
= ap
->s_net
;
1141 at
->src_node
= sat
.sat_addr
.s_node
= ap
->s_node
;
1143 n
= atalk_pick_and_bind_port(sk
, &sat
);
1145 sock_reset_flag(sk
, SOCK_ZAPPED
);
1150 /* Set the address 'our end' of the connection */
1151 static int atalk_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
1153 struct sockaddr_at
*addr
= (struct sockaddr_at
*)uaddr
;
1154 struct sock
*sk
= sock
->sk
;
1155 struct atalk_sock
*at
= at_sk(sk
);
1158 if (!sock_flag(sk
, SOCK_ZAPPED
) ||
1159 addr_len
!= sizeof(struct sockaddr_at
))
1162 if (addr
->sat_family
!= AF_APPLETALK
)
1163 return -EAFNOSUPPORT
;
1166 if (addr
->sat_addr
.s_net
== htons(ATADDR_ANYNET
)) {
1167 struct atalk_addr
*ap
= atalk_find_primary();
1169 err
= -EADDRNOTAVAIL
;
1173 at
->src_net
= addr
->sat_addr
.s_net
= ap
->s_net
;
1174 at
->src_node
= addr
->sat_addr
.s_node
= ap
->s_node
;
1176 err
= -EADDRNOTAVAIL
;
1177 if (!atalk_find_interface(addr
->sat_addr
.s_net
,
1178 addr
->sat_addr
.s_node
))
1181 at
->src_net
= addr
->sat_addr
.s_net
;
1182 at
->src_node
= addr
->sat_addr
.s_node
;
1185 if (addr
->sat_port
== ATADDR_ANYPORT
) {
1186 err
= atalk_pick_and_bind_port(sk
, addr
);
1191 at
->src_port
= addr
->sat_port
;
1194 if (atalk_find_or_insert_socket(sk
, addr
))
1198 sock_reset_flag(sk
, SOCK_ZAPPED
);
1205 /* Set the address we talk to */
1206 static int atalk_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
1207 int addr_len
, int flags
)
1209 struct sock
*sk
= sock
->sk
;
1210 struct atalk_sock
*at
= at_sk(sk
);
1211 struct sockaddr_at
*addr
;
1214 sk
->sk_state
= TCP_CLOSE
;
1215 sock
->state
= SS_UNCONNECTED
;
1217 if (addr_len
!= sizeof(*addr
))
1220 addr
= (struct sockaddr_at
*)uaddr
;
1222 if (addr
->sat_family
!= AF_APPLETALK
)
1223 return -EAFNOSUPPORT
;
1225 if (addr
->sat_addr
.s_node
== ATADDR_BCAST
&&
1226 !sock_flag(sk
, SOCK_BROADCAST
)) {
1228 pr_warn("atalk_connect: %s is broken and did not set SO_BROADCAST.\n",
1237 if (sock_flag(sk
, SOCK_ZAPPED
))
1238 if (atalk_autobind(sk
) < 0)
1242 if (!atrtr_get_dev(&addr
->sat_addr
))
1245 at
->dest_port
= addr
->sat_port
;
1246 at
->dest_net
= addr
->sat_addr
.s_net
;
1247 at
->dest_node
= addr
->sat_addr
.s_node
;
1249 sock
->state
= SS_CONNECTED
;
1250 sk
->sk_state
= TCP_ESTABLISHED
;
1258 * Find the name of an AppleTalk socket. Just copy the right
1259 * fields into the sockaddr.
1261 static int atalk_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1264 struct sockaddr_at sat
;
1265 struct sock
*sk
= sock
->sk
;
1266 struct atalk_sock
*at
= at_sk(sk
);
1271 if (sock_flag(sk
, SOCK_ZAPPED
))
1272 if (atalk_autobind(sk
) < 0)
1275 memset(&sat
, 0, sizeof(sat
));
1279 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1282 sat
.sat_addr
.s_net
= at
->dest_net
;
1283 sat
.sat_addr
.s_node
= at
->dest_node
;
1284 sat
.sat_port
= at
->dest_port
;
1286 sat
.sat_addr
.s_net
= at
->src_net
;
1287 sat
.sat_addr
.s_node
= at
->src_node
;
1288 sat
.sat_port
= at
->src_port
;
1291 sat
.sat_family
= AF_APPLETALK
;
1292 memcpy(uaddr
, &sat
, sizeof(sat
));
1293 err
= sizeof(struct sockaddr_at
);
1300 static int atalk_route_packet(struct sk_buff
*skb
, struct net_device
*dev
,
1301 struct ddpehdr
*ddp
, __u16 len_hops
, int origlen
)
1303 struct atalk_route
*rt
;
1304 struct atalk_addr ta
;
1307 * Don't route multicast, etc., packets, or packets sent to "this
1310 if (skb
->pkt_type
!= PACKET_HOST
|| !ddp
->deh_dnet
) {
1314 * Can it ever happen that a packet is from a PPP iface and
1315 * needs to be broadcast onto the default network?
1317 if (dev
->type
== ARPHRD_PPP
)
1318 printk(KERN_DEBUG
"AppleTalk: didn't forward broadcast "
1319 "packet received from PPP iface\n");
1323 ta
.s_net
= ddp
->deh_dnet
;
1324 ta
.s_node
= ddp
->deh_dnode
;
1326 /* Route the packet */
1327 rt
= atrtr_find(&ta
);
1328 /* increment hops count */
1329 len_hops
+= 1 << 10;
1330 if (!rt
|| !(len_hops
& (15 << 10)))
1333 /* FIXME: use skb->cb to be able to use shared skbs */
1336 * Route goes through another gateway, so set the target to the
1340 if (rt
->flags
& RTF_GATEWAY
) {
1341 ta
.s_net
= rt
->gateway
.s_net
;
1342 ta
.s_node
= rt
->gateway
.s_node
;
1345 /* Fix up skb->len field */
1346 skb_trim(skb
, min_t(unsigned int, origlen
,
1347 (rt
->dev
->hard_header_len
+
1348 ddp_dl
->header_length
+ (len_hops
& 1023))));
1350 /* FIXME: use skb->cb to be able to use shared skbs */
1351 ddp
->deh_len_hops
= htons(len_hops
);
1354 * Send the buffer onwards
1356 * Now we must always be careful. If it's come from LocalTalk to
1357 * EtherTalk it might not fit
1359 * Order matters here: If a packet has to be copied to make a new
1360 * headroom (rare hopefully) then it won't need unsharing.
1362 * Note. ddp-> becomes invalid at the realloc.
1364 if (skb_headroom(skb
) < 22) {
1365 /* 22 bytes - 12 ether, 2 len, 3 802.2 5 snap */
1366 struct sk_buff
*nskb
= skb_realloc_headroom(skb
, 32);
1370 skb
= skb_unshare(skb
, GFP_ATOMIC
);
1373 * If the buffer didn't vanish into the lack of space bitbucket we can
1379 if (aarp_send_ddp(rt
->dev
, skb
, &ta
, NULL
) == NET_XMIT_DROP
)
1381 return NET_RX_SUCCESS
;
1389 * atalk_rcv - Receive a packet (in skb) from device dev
1390 * @skb: packet received
1391 * @dev: network device where the packet comes from
1393 * @orig_dev: the original receive net device
1395 * Receive a packet (in skb) from device dev. This has come from the SNAP
1396 * decoder, and on entry skb->transport_header is the DDP header, skb->len
1397 * is the DDP header, skb->len is the DDP length. The physical headers
1398 * have been extracted. PPP should probably pass frames marked as for this
1399 * layer. [ie ARPHRD_ETHERTALK]
1401 static int atalk_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
1402 struct packet_type
*pt
, struct net_device
*orig_dev
)
1404 struct ddpehdr
*ddp
;
1406 struct atalk_iface
*atif
;
1407 struct sockaddr_at tosat
;
1411 if (!net_eq(dev_net(dev
), &init_net
))
1414 /* Don't mangle buffer if shared */
1415 if (!(skb
= skb_share_check(skb
, GFP_ATOMIC
)))
1418 /* Size check and make sure header is contiguous */
1419 if (!pskb_may_pull(skb
, sizeof(*ddp
)))
1424 len_hops
= ntohs(ddp
->deh_len_hops
);
1426 /* Trim buffer in case of stray trailing data */
1428 skb_trim(skb
, min_t(unsigned int, skb
->len
, len_hops
& 1023));
1431 * Size check to see if ddp->deh_len was crap
1432 * (Otherwise we'll detonate most spectacularly
1433 * in the middle of atalk_checksum() or recvmsg()).
1435 if (skb
->len
< sizeof(*ddp
) || skb
->len
< (len_hops
& 1023)) {
1436 pr_debug("AppleTalk: dropping corrupted frame (deh_len=%u, "
1437 "skb->len=%u)\n", len_hops
& 1023, skb
->len
);
1442 * Any checksums. Note we don't do htons() on this == is assumed to be
1443 * valid for net byte orders all over the networking code...
1446 atalk_checksum(skb
, len_hops
& 1023) != ddp
->deh_sum
)
1447 /* Not a valid AppleTalk frame - dustbin time */
1450 /* Check the packet is aimed at us */
1451 if (!ddp
->deh_dnet
) /* Net 0 is 'this network' */
1452 atif
= atalk_find_anynet(ddp
->deh_dnode
, dev
);
1454 atif
= atalk_find_interface(ddp
->deh_dnet
, ddp
->deh_dnode
);
1457 /* Not ours, so we route the packet via the correct
1460 return atalk_route_packet(skb
, dev
, ddp
, len_hops
, origlen
);
1464 * Which socket - atalk_search_socket() looks for a *full match*
1465 * of the <net, node, port> tuple.
1467 tosat
.sat_addr
.s_net
= ddp
->deh_dnet
;
1468 tosat
.sat_addr
.s_node
= ddp
->deh_dnode
;
1469 tosat
.sat_port
= ddp
->deh_dport
;
1471 sock
= atalk_search_socket(&tosat
, atif
);
1472 if (!sock
) /* But not one of our sockets */
1475 /* Queue packet (standard) */
1476 if (sock_queue_rcv_skb(sock
, skb
) < 0)
1479 return NET_RX_SUCCESS
;
1489 * Receive a LocalTalk frame. We make some demands on the caller here.
1490 * Caller must provide enough headroom on the packet to pull the short
1491 * header and append a long one.
1493 static int ltalk_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
1494 struct packet_type
*pt
, struct net_device
*orig_dev
)
1496 if (!net_eq(dev_net(dev
), &init_net
))
1499 /* Expand any short form frames */
1500 if (skb_mac_header(skb
)[2] == 1) {
1501 struct ddpehdr
*ddp
;
1502 /* Find our address */
1503 struct atalk_addr
*ap
= atalk_find_dev_addr(dev
);
1505 if (!ap
|| skb
->len
< sizeof(__be16
) || skb
->len
> 1023)
1508 /* Don't mangle buffer if shared */
1509 if (!(skb
= skb_share_check(skb
, GFP_ATOMIC
)))
1513 * The push leaves us with a ddephdr not an shdr, and
1514 * handily the port bytes in the right place preset.
1516 ddp
= skb_push(skb
, sizeof(*ddp
) - 4);
1518 /* Now fill in the long header */
1521 * These two first. The mac overlays the new source/dest
1522 * network information so we MUST copy these before
1523 * we write the network numbers !
1526 ddp
->deh_dnode
= skb_mac_header(skb
)[0]; /* From physical header */
1527 ddp
->deh_snode
= skb_mac_header(skb
)[1]; /* From physical header */
1529 ddp
->deh_dnet
= ap
->s_net
; /* Network number */
1530 ddp
->deh_snet
= ap
->s_net
;
1531 ddp
->deh_sum
= 0; /* No checksum */
1533 * Not sure about this bit...
1535 /* Non routable, so force a drop if we slip up later */
1536 ddp
->deh_len_hops
= htons(skb
->len
+ (DDP_MAXHOPS
<< 10));
1538 skb_reset_transport_header(skb
);
1540 return atalk_rcv(skb
, dev
, pt
, orig_dev
);
1546 static int atalk_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
1548 struct sock
*sk
= sock
->sk
;
1549 struct atalk_sock
*at
= at_sk(sk
);
1550 DECLARE_SOCKADDR(struct sockaddr_at
*, usat
, msg
->msg_name
);
1551 int flags
= msg
->msg_flags
;
1553 struct sockaddr_at local_satalk
, gsat
;
1554 struct sk_buff
*skb
;
1555 struct net_device
*dev
;
1556 struct ddpehdr
*ddp
;
1557 int size
, hard_header_len
;
1558 struct atalk_route
*rt
, *rt_lo
= NULL
;
1561 if (flags
& ~(MSG_DONTWAIT
|MSG_CMSG_COMPAT
))
1564 if (len
> DDP_MAXSZ
)
1570 if (sock_flag(sk
, SOCK_ZAPPED
))
1571 if (atalk_autobind(sk
) < 0)
1575 if (msg
->msg_namelen
< sizeof(*usat
) ||
1576 usat
->sat_family
!= AF_APPLETALK
)
1580 /* netatalk didn't implement this check */
1581 if (usat
->sat_addr
.s_node
== ATADDR_BCAST
&&
1582 !sock_flag(sk
, SOCK_BROADCAST
)) {
1587 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1589 usat
= &local_satalk
;
1590 usat
->sat_family
= AF_APPLETALK
;
1591 usat
->sat_port
= at
->dest_port
;
1592 usat
->sat_addr
.s_node
= at
->dest_node
;
1593 usat
->sat_addr
.s_net
= at
->dest_net
;
1596 /* Build a packet */
1597 net_dbg_ratelimited("SK %p: Got address.\n", sk
);
1600 size
= sizeof(struct ddpehdr
) + len
+ ddp_dl
->header_length
;
1602 if (usat
->sat_addr
.s_net
|| usat
->sat_addr
.s_node
== ATADDR_ANYNODE
) {
1603 rt
= atrtr_find(&usat
->sat_addr
);
1605 struct atalk_addr at_hint
;
1608 at_hint
.s_net
= at
->src_net
;
1610 rt
= atrtr_find(&at_hint
);
1618 net_dbg_ratelimited("SK %p: Size needed %d, device %s\n",
1619 sk
, size
, dev
->name
);
1621 hard_header_len
= dev
->hard_header_len
;
1622 /* Leave room for loopback hardware header if necessary */
1623 if (usat
->sat_addr
.s_node
== ATADDR_BCAST
&&
1624 (dev
->flags
& IFF_LOOPBACK
|| !(rt
->flags
& RTF_GATEWAY
))) {
1625 struct atalk_addr at_lo
;
1630 rt_lo
= atrtr_find(&at_lo
);
1632 if (rt_lo
&& rt_lo
->dev
->hard_header_len
> hard_header_len
)
1633 hard_header_len
= rt_lo
->dev
->hard_header_len
;
1636 size
+= hard_header_len
;
1638 skb
= sock_alloc_send_skb(sk
, size
, (flags
& MSG_DONTWAIT
), &err
);
1643 skb_reserve(skb
, ddp_dl
->header_length
);
1644 skb_reserve(skb
, hard_header_len
);
1647 net_dbg_ratelimited("SK %p: Begin build.\n", sk
);
1649 ddp
= skb_put(skb
, sizeof(struct ddpehdr
));
1650 ddp
->deh_len_hops
= htons(len
+ sizeof(*ddp
));
1651 ddp
->deh_dnet
= usat
->sat_addr
.s_net
;
1652 ddp
->deh_snet
= at
->src_net
;
1653 ddp
->deh_dnode
= usat
->sat_addr
.s_node
;
1654 ddp
->deh_snode
= at
->src_node
;
1655 ddp
->deh_dport
= usat
->sat_port
;
1656 ddp
->deh_sport
= at
->src_port
;
1658 net_dbg_ratelimited("SK %p: Copy user data (%zd bytes).\n", sk
, len
);
1660 err
= memcpy_from_msg(skb_put(skb
, len
), msg
, len
);
1667 if (sk
->sk_no_check_tx
)
1670 ddp
->deh_sum
= atalk_checksum(skb
, len
+ sizeof(*ddp
));
1673 * Loopback broadcast packets to non gateway targets (ie routes
1674 * to group we are in)
1676 if (ddp
->deh_dnode
== ATADDR_BCAST
&&
1677 !(rt
->flags
& RTF_GATEWAY
) && !(dev
->flags
& IFF_LOOPBACK
)) {
1678 struct sk_buff
*skb2
= skb_copy(skb
, GFP_KERNEL
);
1682 net_dbg_ratelimited("SK %p: send out(copy).\n", sk
);
1684 * If it fails it is queued/sent above in the aarp queue
1686 aarp_send_ddp(dev
, skb2
, &usat
->sat_addr
, NULL
);
1690 if (dev
->flags
& IFF_LOOPBACK
|| loopback
) {
1691 net_dbg_ratelimited("SK %p: Loop back.\n", sk
);
1694 if (ddp
->deh_dnode
== ATADDR_BCAST
) {
1703 ddp_dl
->request(ddp_dl
, skb
, dev
->dev_addr
);
1705 net_dbg_ratelimited("SK %p: send out.\n", sk
);
1706 if (rt
->flags
& RTF_GATEWAY
) {
1707 gsat
.sat_addr
= rt
->gateway
;
1712 * If it fails it is queued/sent above in the aarp queue
1714 aarp_send_ddp(dev
, skb
, &usat
->sat_addr
, NULL
);
1716 net_dbg_ratelimited("SK %p: Done write (%zd).\n", sk
, len
);
1723 static int atalk_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t size
,
1726 struct sock
*sk
= sock
->sk
;
1727 struct ddpehdr
*ddp
;
1731 struct sk_buff
*skb
;
1733 skb
= skb_recv_datagram(sk
, flags
, &err
);
1739 /* FIXME: use skb->cb to be able to use shared skbs */
1741 copied
= ntohs(ddp
->deh_len_hops
) & 1023;
1743 if (sk
->sk_type
!= SOCK_RAW
) {
1744 offset
= sizeof(*ddp
);
1748 if (copied
> size
) {
1750 msg
->msg_flags
|= MSG_TRUNC
;
1752 err
= skb_copy_datagram_msg(skb
, offset
, msg
, copied
);
1754 if (!err
&& msg
->msg_name
) {
1755 DECLARE_SOCKADDR(struct sockaddr_at
*, sat
, msg
->msg_name
);
1756 sat
->sat_family
= AF_APPLETALK
;
1757 sat
->sat_port
= ddp
->deh_sport
;
1758 sat
->sat_addr
.s_node
= ddp
->deh_snode
;
1759 sat
->sat_addr
.s_net
= ddp
->deh_snet
;
1760 msg
->msg_namelen
= sizeof(*sat
);
1763 skb_free_datagram(sk
, skb
); /* Free the datagram. */
1767 return err
? : copied
;
1772 * AppleTalk ioctl calls.
1774 static int atalk_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1776 int rc
= -ENOIOCTLCMD
;
1777 struct sock
*sk
= sock
->sk
;
1778 void __user
*argp
= (void __user
*)arg
;
1781 /* Protocol layer */
1783 long amount
= sk
->sk_sndbuf
- sk_wmem_alloc_get(sk
);
1787 rc
= put_user(amount
, (int __user
*)argp
);
1791 struct sk_buff
*skb
;
1794 spin_lock_irq(&sk
->sk_receive_queue
.lock
);
1795 skb
= skb_peek(&sk
->sk_receive_queue
);
1797 amount
= skb
->len
- sizeof(struct ddpehdr
);
1798 spin_unlock_irq(&sk
->sk_receive_queue
.lock
);
1799 rc
= put_user(amount
, (int __user
*)argp
);
1806 if (capable(CAP_NET_ADMIN
))
1807 rc
= atrtr_ioctl(cmd
, argp
);
1812 case SIOCGIFBRDADDR
:
1813 case SIOCATALKDIFADDR
:
1815 case SIOCSARP
: /* proxy AARP */
1816 case SIOCDARP
: /* proxy AARP */
1818 rc
= atif_ioctl(cmd
, argp
);
1827 #ifdef CONFIG_COMPAT
1828 static int atalk_compat_routing_ioctl(struct sock
*sk
, unsigned int cmd
,
1829 struct compat_rtentry __user
*ur
)
1831 compat_uptr_t rtdev
;
1834 if (copy_from_user(&rt
.rt_dst
, &ur
->rt_dst
,
1835 3 * sizeof(struct sockaddr
)) ||
1836 get_user(rt
.rt_flags
, &ur
->rt_flags
) ||
1837 get_user(rt
.rt_metric
, &ur
->rt_metric
) ||
1838 get_user(rt
.rt_mtu
, &ur
->rt_mtu
) ||
1839 get_user(rt
.rt_window
, &ur
->rt_window
) ||
1840 get_user(rt
.rt_irtt
, &ur
->rt_irtt
) ||
1841 get_user(rtdev
, &ur
->rt_dev
))
1846 if (rt
.rt_dst
.sa_family
!= AF_APPLETALK
)
1848 return atrtr_delete(&((struct sockaddr_at
*)
1849 &rt
.rt_dst
)->sat_addr
);
1852 rt
.rt_dev
= compat_ptr(rtdev
);
1853 return atrtr_ioctl_addrt(&rt
);
1858 static int atalk_compat_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1860 void __user
*argp
= compat_ptr(arg
);
1861 struct sock
*sk
= sock
->sk
;
1866 return atalk_compat_routing_ioctl(sk
, cmd
, argp
);
1868 * SIOCATALKDIFADDR is a SIOCPROTOPRIVATE ioctl number, so we
1869 * cannot handle it in common code. The data we access if ifreq
1870 * here is compatible, so we can simply call the native
1873 case SIOCATALKDIFADDR
:
1874 return atalk_ioctl(sock
, cmd
, (unsigned long)argp
);
1876 return -ENOIOCTLCMD
;
1879 #endif /* CONFIG_COMPAT */
1882 static const struct net_proto_family atalk_family_ops
= {
1883 .family
= PF_APPLETALK
,
1884 .create
= atalk_create
,
1885 .owner
= THIS_MODULE
,
1888 static const struct proto_ops atalk_dgram_ops
= {
1889 .family
= PF_APPLETALK
,
1890 .owner
= THIS_MODULE
,
1891 .release
= atalk_release
,
1893 .connect
= atalk_connect
,
1894 .socketpair
= sock_no_socketpair
,
1895 .accept
= sock_no_accept
,
1896 .getname
= atalk_getname
,
1897 .poll
= datagram_poll
,
1898 .ioctl
= atalk_ioctl
,
1899 .gettstamp
= sock_gettstamp
,
1900 #ifdef CONFIG_COMPAT
1901 .compat_ioctl
= atalk_compat_ioctl
,
1903 .listen
= sock_no_listen
,
1904 .shutdown
= sock_no_shutdown
,
1905 .sendmsg
= atalk_sendmsg
,
1906 .recvmsg
= atalk_recvmsg
,
1907 .mmap
= sock_no_mmap
,
1910 static struct notifier_block ddp_notifier
= {
1911 .notifier_call
= ddp_device_event
,
1914 static struct packet_type ltalk_packet_type __read_mostly
= {
1915 .type
= cpu_to_be16(ETH_P_LOCALTALK
),
1919 static struct packet_type ppptalk_packet_type __read_mostly
= {
1920 .type
= cpu_to_be16(ETH_P_PPPTALK
),
1924 static unsigned char ddp_snap_id
[] = { 0x08, 0x00, 0x07, 0x80, 0x9B };
1926 /* Export symbols for use by drivers when AppleTalk is a module */
1927 EXPORT_SYMBOL(atrtr_get_dev
);
1928 EXPORT_SYMBOL(atalk_find_dev_addr
);
1930 /* Called by proto.c on kernel start up */
1931 static int __init
atalk_init(void)
1935 rc
= proto_register(&ddp_proto
, 0);
1939 rc
= sock_register(&atalk_family_ops
);
1943 ddp_dl
= register_snap_client(ddp_snap_id
, atalk_rcv
);
1945 pr_crit("Unable to register DDP with SNAP.\n");
1950 dev_add_pack(<alk_packet_type
);
1951 dev_add_pack(&ppptalk_packet_type
);
1953 rc
= register_netdevice_notifier(&ddp_notifier
);
1957 rc
= aarp_proto_init();
1961 rc
= atalk_proc_init();
1965 rc
= atalk_register_sysctl();
1973 aarp_cleanup_module();
1975 unregister_netdevice_notifier(&ddp_notifier
);
1977 dev_remove_pack(&ppptalk_packet_type
);
1978 dev_remove_pack(<alk_packet_type
);
1979 unregister_snap_client(ddp_dl
);
1981 sock_unregister(PF_APPLETALK
);
1983 proto_unregister(&ddp_proto
);
1986 module_init(atalk_init
);
1989 * No explicit module reference count manipulation is needed in the
1990 * protocol. Socket layer sets module reference count for us
1991 * and interfaces reference counting is done
1992 * by the network device layer.
1994 * Ergo, before the AppleTalk module can be removed, all AppleTalk
1995 * sockets should be closed from user space.
1997 static void __exit
atalk_exit(void)
1999 #ifdef CONFIG_SYSCTL
2000 atalk_unregister_sysctl();
2001 #endif /* CONFIG_SYSCTL */
2003 aarp_cleanup_module(); /* General aarp clean-up. */
2004 unregister_netdevice_notifier(&ddp_notifier
);
2005 dev_remove_pack(<alk_packet_type
);
2006 dev_remove_pack(&ppptalk_packet_type
);
2007 unregister_snap_client(ddp_dl
);
2008 sock_unregister(PF_APPLETALK
);
2009 proto_unregister(&ddp_proto
);
2011 module_exit(atalk_exit
);
2013 MODULE_LICENSE("GPL");
2014 MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
2015 MODULE_DESCRIPTION("AppleTalk 0.20\n");
2016 MODULE_ALIAS_NETPROTO(PF_APPLETALK
);