2 * DDP: An implementation of the AppleTalk DDP protocol for
5 * Alan Cox <Alan.Cox@linux.org>
7 * With more than a little assistance from
9 * Wesley Craig <netatalk@umich.edu>
12 * Michael Callahan : Made routing work
13 * Wesley Craig : Fix probing to listen to a
15 * Alan Cox : Added send/recvmsg support
16 * Alan Cox : Moved at. to protinfo in
18 * Alan Cox : Added firewall hooks.
19 * Alan Cox : Supports new ARPHRD_LOOPBACK
20 * Christer Weinigel : Routing and /proc fixes.
21 * Bradford Johnson : LocalTalk.
22 * Tom Dyas : Module support.
23 * Alan Cox : Hooks for PPP (based on the
25 * Alan Cox : Posix bits
26 * Alan Cox/Mike Freeman : Possible fix to NBP problems
27 * Bradford Johnson : IP-over-DDP (experimental)
28 * Jay Schulist : Moved IP-over-DDP to its own
29 * driver file. (ipddp.c & ipddp.h)
30 * Jay Schulist : Made work as module with
31 * AppleTalk drivers, cleaned it.
32 * Rob Newberry : Added proxy AARP and AARP
33 * procfs, moved probing to AARP
36 * Michael Zuelsdorff : fix for net.0 packets. don't
37 * allow illegal ether/tokentalk
38 * port assignment. we lose a
39 * valid localtalk port as a
41 * Arnaldo C. de Melo : Cleanup, in preparation for
42 * shared skb support 8)
44 * This program is free software; you can redistribute it and/or
45 * modify it under the terms of the GNU General Public License
46 * as published by the Free Software Foundation; either version
47 * 2 of the License, or (at your option) any later version.
51 #include <linux/config.h>
52 #if defined(CONFIG_ATALK) || defined(CONFIG_ATALK_MODULE)
53 #include <linux/module.h>
54 #include <asm/uaccess.h>
55 #include <asm/system.h>
56 #include <asm/bitops.h>
57 #include <linux/types.h>
58 #include <linux/kernel.h>
59 #include <linux/sched.h>
60 #include <linux/string.h>
62 #include <linux/socket.h>
63 #include <linux/sockios.h>
65 #include <linux/errno.h>
66 #include <linux/interrupt.h>
67 #include <linux/if_ether.h>
68 #include <linux/notifier.h>
69 #include <linux/netdevice.h>
70 #include <linux/inetdevice.h>
71 #include <linux/route.h>
72 #include <linux/inet.h>
73 #include <linux/etherdevice.h>
74 #include <linux/if_arp.h>
75 #include <linux/skbuff.h>
76 #include <linux/spinlock.h>
77 #include <linux/termios.h> /* For TIOCOUTQ/INQ */
78 #include <net/datalink.h>
79 #include <net/p8022.h>
80 #include <net/psnap.h>
83 #include <net/route.h>
84 #include <linux/atalk.h>
85 #include <linux/proc_fs.h>
86 #include <linux/stat.h>
87 #include <linux/init.h>
90 extern void aarp_register_proc_fs(void);
91 extern void aarp_unregister_proc_fs(void);
94 extern void aarp_cleanup_module(void);
96 extern void aarp_probe_network(struct atalk_iface *atif);
97 extern int aarp_proxy_probe_network(struct atalk_iface *atif,
99 extern void aarp_proxy_remove(struct net_device *dev, struct at_addr *sa);
101 #undef APPLETALK_DEBUG
102 #ifdef APPLETALK_DEBUG
103 #define DPRINT(x) print(x)
106 #endif /* APPLETALK_DEBUG */
109 extern inline void atalk_register_sysctl(void);
110 extern inline void atalk_unregister_sysctl(void);
111 #endif /* CONFIG_SYSCTL */
113 struct datalink_proto *ddp_dl, *aarp_dl;
114 static struct proto_ops atalk_dgram_ops;
116 /**************************************************************************\
118 * Handlers for the socket list. *
120 \**************************************************************************/
122 static struct sock *atalk_sockets;
123 static spinlock_t atalk_sockets_lock = SPIN_LOCK_UNLOCKED;
125 extern inline void atalk_insert_socket(struct sock *sk)
127 spin_lock_bh(&atalk_sockets_lock);
128 sk->next = atalk_sockets;
130 atalk_sockets->pprev = &sk->next;
132 sk->pprev = &atalk_sockets;
133 spin_unlock_bh(&atalk_sockets_lock);
136 extern inline void atalk_remove_socket(struct sock *sk)
138 spin_lock_bh(&atalk_sockets_lock);
141 sk->next->pprev = sk->pprev;
142 *sk->pprev = sk->next;
145 spin_unlock_bh(&atalk_sockets_lock);
148 static struct sock *atalk_search_socket(struct sockaddr_at *to,
149 struct atalk_iface *atif)
153 spin_lock_bh(&atalk_sockets_lock);
154 for (s = atalk_sockets; s; s = s->next) {
155 if (to->sat_port != s->protinfo.af_at.src_port)
158 if (to->sat_addr.s_net == ATADDR_ANYNET &&
159 to->sat_addr.s_node == ATADDR_BCAST &&
160 s->protinfo.af_at.src_net == atif->address.s_net)
163 if (to->sat_addr.s_net == s->protinfo.af_at.src_net &&
164 (to->sat_addr.s_node == s->protinfo.af_at.src_node ||
165 to->sat_addr.s_node == ATADDR_BCAST ||
166 to->sat_addr.s_node == ATADDR_ANYNODE))
169 /* XXXX.0 -- we got a request for this router. make sure
170 * that the node is appropriately set. */
171 if (to->sat_addr.s_node == ATADDR_ANYNODE &&
172 to->sat_addr.s_net != ATADDR_ANYNET &&
173 atif->address.s_node == s->protinfo.af_at.src_node) {
174 to->sat_addr.s_node = atif->address.s_node;
178 spin_unlock_bh(&atalk_sockets_lock);
183 * Try to find a socket matching ADDR in the socket list,
184 * if found then return it. If not, insert SK into the
187 * This entire operation must execute atomically.
189 static struct sock *atalk_find_or_insert_socket(struct sock *sk,
190 struct sockaddr_at *sat)
194 spin_lock_bh(&atalk_sockets_lock);
195 for (s = atalk_sockets; s; s = s->next)
196 if (s->protinfo.af_at.src_net == sat->sat_addr.s_net &&
197 s->protinfo.af_at.src_node == sat->sat_addr.s_node &&
198 s->protinfo.af_at.src_port == sat->sat_port)
202 /* Wheee, it's free, assign and insert. */
203 sk->next = atalk_sockets;
205 atalk_sockets->pprev = &sk->next;
207 sk->pprev = &atalk_sockets;
210 spin_unlock_bh(&atalk_sockets_lock);
214 static void atalk_destroy_timer(unsigned long data)
216 struct sock *sk = (struct sock *) data;
218 if (!atomic_read(&sk->wmem_alloc) &&
219 !atomic_read(&sk->rmem_alloc) && sk->dead) {
223 sk->timer.expires = jiffies + SOCK_DESTROY_TIME;
224 add_timer(&sk->timer);
228 extern inline void atalk_destroy_socket(struct sock *sk)
230 atalk_remove_socket(sk);
231 skb_queue_purge(&sk->receive_queue);
233 if (!atomic_read(&sk->wmem_alloc) &&
234 !atomic_read(&sk->rmem_alloc) && sk->dead) {
238 init_timer(&sk->timer);
239 sk->timer.expires = jiffies + SOCK_DESTROY_TIME;
240 sk->timer.function = atalk_destroy_timer;
241 sk->timer.data = (unsigned long) sk;
242 add_timer(&sk->timer);
246 /* Called from proc fs */
247 static int atalk_get_info(char *buffer, char **start, off_t offset, int length)
251 int len = sprintf(buffer, "Type local_addr remote_addr tx_queue "
252 "rx_queue st uid\n");
254 /* Output the AppleTalk data for the /proc filesystem */
256 spin_lock_bh(&atalk_sockets_lock);
257 for (s = atalk_sockets; s; s = s->next) {
258 len += sprintf(buffer + len,"%02X ", s->type);
259 len += sprintf(buffer + len,"%04X:%02X:%02X ",
260 ntohs(s->protinfo.af_at.src_net),
261 s->protinfo.af_at.src_node,
262 s->protinfo.af_at.src_port);
263 len += sprintf(buffer + len,"%04X:%02X:%02X ",
264 ntohs(s->protinfo.af_at.dest_net),
265 s->protinfo.af_at.dest_node,
266 s->protinfo.af_at.dest_port);
267 len += sprintf(buffer + len,"%08X:%08X ",
268 atomic_read(&s->wmem_alloc),
269 atomic_read(&s->rmem_alloc));
270 len += sprintf(buffer + len,"%02X %d\n", s->state,
271 SOCK_INODE(s->socket)->i_uid);
273 /* Are we still dumping unwanted data then discard the record */
277 len = 0; /* Keep dumping into the buffer start */
280 if (pos > offset + length) /* We have dumped enough */
283 spin_unlock_bh(&atalk_sockets_lock);
285 /* The data in question runs from begin to begin+len */
286 *start = buffer + offset - begin; /* Start of wanted data */
287 len -= offset - begin; /* Remove unwanted header data from length */
289 len = length; /* Remove unwanted tail data from length */
294 /**************************************************************************\
296 * Routing tables for the AppleTalk socket layer. *
298 \**************************************************************************/
300 /* Anti-deadlock ordering is router_lock --> iface_lock -DaveM */
301 static struct atalk_route *atalk_router_list;
302 static rwlock_t atalk_router_lock = RW_LOCK_UNLOCKED;
304 static struct atalk_iface *atalk_iface_list;
305 static spinlock_t atalk_iface_lock = SPIN_LOCK_UNLOCKED;
307 /* For probing devices or in a routerless network */
308 static struct atalk_route atrtr_default;
310 /* AppleTalk interface control */
312 * Drop a device. Doesn't drop any of its routes - that is the caller's
313 * problem. Called when we down the interface or delete the address.
315 static void atif_drop_device(struct net_device *dev)
317 struct atalk_iface **iface = &atalk_iface_list;
318 struct atalk_iface *tmp;
320 spin_lock_bh(&atalk_iface_lock);
321 while ((tmp = *iface) != NULL) {
322 if (tmp->dev == dev) {
325 dev->atalk_ptr = NULL;
330 spin_unlock_bh(&atalk_iface_lock);
333 static struct atalk_iface *atif_add_device(struct net_device *dev,
336 struct atalk_iface *iface = kmalloc(sizeof(*iface), GFP_KERNEL);
342 dev->atalk_ptr = iface;
343 iface->address = *sa;
346 spin_lock_bh(&atalk_iface_lock);
347 iface->next = atalk_iface_list;
348 atalk_iface_list = iface;
349 spin_unlock_bh(&atalk_iface_lock);
355 /* Perform phase 2 AARP probing on our tentative address */
356 static int atif_probe_device(struct atalk_iface *atif)
358 int netrange = ntohs(atif->nets.nr_lastnet) -
359 ntohs(atif->nets.nr_firstnet) + 1;
360 int probe_net = ntohs(atif->address.s_net);
361 int probe_node = atif->address.s_node;
364 /* Offset the network we start probing with */
365 if (probe_net == ATADDR_ANYNET) {
366 probe_net = ntohs(atif->nets.nr_firstnet);
368 probe_net += jiffies % netrange;
370 if (probe_node == ATADDR_ANYNODE)
371 probe_node = jiffies & 0xFF;
373 /* Scan the networks */
374 atif->status |= ATIF_PROBE;
375 for (netct = 0; netct <= netrange; netct++) {
376 /* Sweep the available nodes from a given start */
377 atif->address.s_net = htons(probe_net);
378 for (nodect = 0; nodect < 256; nodect++) {
379 atif->address.s_node = ((nodect+probe_node) & 0xFF);
380 if (atif->address.s_node > 0 &&
381 atif->address.s_node < 254) {
382 /* Probe a proposed address */
383 aarp_probe_network(atif);
385 if (!(atif->status & ATIF_PROBE_FAIL)) {
386 atif->status &= ~ATIF_PROBE;
390 atif->status &= ~ATIF_PROBE_FAIL;
393 if (probe_net > ntohs(atif->nets.nr_lastnet))
394 probe_net = ntohs(atif->nets.nr_firstnet);
396 atif->status &= ~ATIF_PROBE;
398 return -EADDRINUSE; /* Network is full... */
402 /* Perform AARP probing for a proxy address */
403 static int atif_proxy_probe_device(struct atalk_iface *atif,
404 struct at_addr* proxy_addr)
406 int netrange = ntohs(atif->nets.nr_lastnet) -
407 ntohs(atif->nets.nr_firstnet) + 1;
408 /* we probe the interface's network */
409 int probe_net = ntohs(atif->address.s_net);
410 int probe_node = ATADDR_ANYNODE; /* we'll take anything */
413 /* Offset the network we start probing with */
414 if (probe_net == ATADDR_ANYNET) {
415 probe_net = ntohs(atif->nets.nr_firstnet);
417 probe_net += jiffies % netrange;
420 if (probe_node == ATADDR_ANYNODE)
421 probe_node = jiffies & 0xFF;
423 /* Scan the networks */
424 for (netct = 0; netct <= netrange; netct++) {
425 /* Sweep the available nodes from a given start */
426 proxy_addr->s_net = htons(probe_net);
427 for (nodect = 0; nodect < 256; nodect++) {
428 proxy_addr->s_node = ((nodect + probe_node) & 0xFF);
429 if (proxy_addr->s_node > 0 &&
430 proxy_addr->s_node < 254) {
431 /* Tell AARP to probe a proposed address */
432 int ret = aarp_proxy_probe_network(atif,
435 if (ret != -EADDRINUSE)
440 if (probe_net > ntohs(atif->nets.nr_lastnet))
441 probe_net = ntohs(atif->nets.nr_firstnet);
444 return -EADDRINUSE; /* Network is full... */
448 struct at_addr *atalk_find_dev_addr(struct net_device *dev)
450 struct atalk_iface *iface = dev->atalk_ptr;
451 return iface ? &iface->address : NULL;
454 static struct at_addr *atalk_find_primary(void)
456 struct atalk_iface *fiface = NULL;
457 struct at_addr *retval;
458 struct atalk_iface *iface;
461 * Return a point-to-point interface only if
462 * there is no non-ptp interface available.
464 spin_lock_bh(&atalk_iface_lock);
465 for (iface = atalk_iface_list; iface; iface = iface->next) {
466 if (!fiface && !(iface->dev->flags & IFF_LOOPBACK))
468 if (!(iface->dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
469 retval = &iface->address;
475 retval = &fiface->address;
476 else if (atalk_iface_list)
477 retval = &atalk_iface_list->address;
480 out: spin_unlock_bh(&atalk_iface_lock);
485 * Find a match for 'any network' - ie any of our interfaces with that
486 * node number will do just nicely.
488 static struct atalk_iface *atalk_find_anynet(int node, struct net_device *dev)
490 struct atalk_iface *iface = dev->atalk_ptr;
492 if (!iface || iface->status & ATIF_PROBE)
495 if (node == ATADDR_BCAST ||
496 iface->address.s_node == node ||
497 node == ATADDR_ANYNODE)
503 /* Find a match for a specific network:node pair */
504 static struct atalk_iface *atalk_find_interface(int net, int node)
506 struct atalk_iface *iface;
508 spin_lock_bh(&atalk_iface_lock);
509 for (iface = atalk_iface_list; iface; iface = iface->next) {
510 if ((node == ATADDR_BCAST ||
511 node == ATADDR_ANYNODE ||
512 iface->address.s_node == node) &&
513 iface->address.s_net == net &&
514 !(iface->status & ATIF_PROBE))
517 /* XXXX.0 -- net.0 returns the iface associated with net */
518 if (node == ATADDR_ANYNODE && net != ATADDR_ANYNET &&
519 ntohs(iface->nets.nr_firstnet) <= ntohs(net) &&
520 ntohs(net) <= ntohs(iface->nets.nr_lastnet))
523 spin_unlock_bh(&atalk_iface_lock);
529 * Find a route for an AppleTalk packet. This ought to get cached in
530 * the socket (later on...). We know about host routes and the fact
531 * that a route must be direct to broadcast.
533 static struct atalk_route *atrtr_find(struct at_addr *target)
536 * we must search through all routes unless we find a
537 * host route, because some host routes might overlap
540 struct atalk_route *net_route = NULL;
541 struct atalk_route *r;
543 read_lock_bh(&atalk_router_lock);
544 for (r = atalk_router_list; r; r = r->next) {
545 if (!(r->flags & RTF_UP))
548 if (r->target.s_net == target->s_net) {
549 if (r->flags & RTF_HOST) {
551 * if this host route is for the target,
554 if (r->target.s_node == target->s_node)
558 * this route will work if there isn't a
559 * direct host route, so cache it
566 * if we found a network route but not a direct host
567 * route, then return it
571 else if (atrtr_default.dev)
573 else /* No route can be found */
575 out: read_unlock_bh(&atalk_router_lock);
581 * Given an AppleTalk network, find the device to use. This can be
584 struct net_device *atrtr_get_dev(struct at_addr *sa)
586 struct atalk_route *atr = atrtr_find(sa);
587 return atr ? atr->dev : NULL;
590 /* Set up a default router */
591 static void atrtr_set_default(struct net_device *dev)
593 atrtr_default.dev = dev;
594 atrtr_default.flags = RTF_UP;
595 atrtr_default.gateway.s_net = htons(0);
596 atrtr_default.gateway.s_node = 0;
600 * Add a router. Basically make sure it looks valid and stuff the
601 * entry in the list. While it uses netranges we always set them to one
602 * entry to work like netatalk.
604 static int atrtr_create(struct rtentry *r, struct net_device *devhint)
606 struct sockaddr_at *ta = (struct sockaddr_at *)&r->rt_dst;
607 struct sockaddr_at *ga = (struct sockaddr_at *)&r->rt_gateway;
608 struct atalk_route *rt;
609 struct atalk_iface *iface, *riface;
613 * Fixme: Raise/Lower a routing change semaphore for these
617 /* Validate the request */
618 if (ta->sat_family != AF_APPLETALK)
621 if (!devhint && ga->sat_family != AF_APPLETALK)
624 /* Now walk the routing table and make our decisions */
625 write_lock_bh(&atalk_router_lock);
626 for (rt = atalk_router_list; rt; rt = rt->next) {
627 if (r->rt_flags != rt->flags)
630 if (ta->sat_addr.s_net == rt->target.s_net) {
631 if (!(rt->flags & RTF_HOST))
633 if (ta->sat_addr.s_node == rt->target.s_node)
641 spin_lock_bh(&atalk_iface_lock);
642 for (iface = atalk_iface_list; iface; iface = iface->next) {
644 ntohs(ga->sat_addr.s_net) >=
645 ntohs(iface->nets.nr_firstnet) &&
646 ntohs(ga->sat_addr.s_net) <=
647 ntohs(iface->nets.nr_lastnet))
650 if (ga->sat_addr.s_net == iface->address.s_net &&
651 ga->sat_addr.s_node == iface->address.s_node)
654 spin_unlock_bh(&atalk_iface_lock);
656 retval = -ENETUNREACH;
660 devhint = riface->dev;
664 rt = kmalloc(sizeof(struct atalk_route), GFP_ATOMIC);
670 rt->next = atalk_router_list;
671 atalk_router_list = rt;
674 /* Fill in the routing entry */
675 rt->target = ta->sat_addr;
677 rt->flags = r->rt_flags;
678 rt->gateway = ga->sat_addr;
681 out: write_unlock_bh(&atalk_router_lock);
685 /* Delete a route. Find it and discard it */
686 static int atrtr_delete(struct at_addr * addr)
688 struct atalk_route **r = &atalk_router_list;
690 struct atalk_route *tmp;
692 write_lock_bh(&atalk_router_lock);
693 while ((tmp = *r) != NULL) {
694 if (tmp->target.s_net == addr->s_net &&
695 (!(tmp->flags&RTF_GATEWAY) ||
696 tmp->target.s_node == addr->s_node)) {
704 out: write_unlock_bh(&atalk_router_lock);
709 * Called when a device is downed. Just throw away any routes
712 void atrtr_device_down(struct net_device *dev)
714 struct atalk_route **r = &atalk_router_list;
715 struct atalk_route *tmp;
717 write_lock_bh(&atalk_router_lock);
718 while ((tmp = *r) != NULL) {
719 if (tmp->dev == dev) {
725 write_unlock_bh(&atalk_router_lock);
727 if (atrtr_default.dev == dev)
728 atrtr_set_default(NULL);
731 /* Actually down the interface */
732 static inline void atalk_dev_down(struct net_device *dev)
734 atrtr_device_down(dev); /* Remove all routes for the device */
735 aarp_device_down(dev); /* Remove AARP entries for the device */
736 atif_drop_device(dev); /* Remove the device */
740 * A device event has occurred. Watch for devices going down and
741 * delete our use of them (iface and route).
743 static int ddp_device_event(struct notifier_block *this, unsigned long event,
746 if (event == NETDEV_DOWN)
747 /* Discard any use of this */
748 atalk_dev_down((struct net_device *) ptr);
753 /* ioctl calls. Shouldn't even need touching */
754 /* Device configuration ioctl calls */
755 static int atif_ioctl(int cmd, void *arg)
757 static char aarp_mcast[6] = {0x09, 0x00, 0x00, 0xFF, 0xFF, 0xFF};
760 struct sockaddr_at *sa;
761 struct net_device *dev;
762 struct atalk_iface *atif;
765 struct rtentry rtdef;
768 if (copy_from_user(&atreq, arg, sizeof(atreq)))
771 dev = __dev_get_by_name(atreq.ifr_name);
775 sa = (struct sockaddr_at*) &atreq.ifr_addr;
776 atif = atalk_find_dev(dev);
780 if (!capable(CAP_NET_ADMIN))
782 if (sa->sat_family != AF_APPLETALK)
784 if (dev->type != ARPHRD_ETHER &&
785 dev->type != ARPHRD_LOOPBACK &&
786 dev->type != ARPHRD_LOCALTLK &&
787 dev->type != ARPHRD_PPP)
788 return -EPROTONOSUPPORT;
790 nr = (struct netrange *) &sa->sat_zero[0];
794 * if this is a point-to-point iface, and we already
795 * have an iface for this AppleTalk address, then we
796 * should not add a route
798 if ((dev->flags & IFF_POINTOPOINT) &&
799 atalk_find_interface(sa->sat_addr.s_net,
800 sa->sat_addr.s_node)) {
801 printk(KERN_DEBUG "AppleTalk: point-to-point "
802 "interface added with "
803 "existing address\n");
808 * Phase 1 is fine on LocalTalk but we don't do
809 * EtherTalk phase 1. Anyone wanting to add it go ahead.
811 if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
812 return -EPROTONOSUPPORT;
813 if (sa->sat_addr.s_node == ATADDR_BCAST ||
814 sa->sat_addr.s_node == 254)
817 /* Already setting address */
818 if (atif->status & ATIF_PROBE)
821 atif->address.s_net = sa->sat_addr.s_net;
822 atif->address.s_node = sa->sat_addr.s_node;
823 atrtr_device_down(dev); /* Flush old routes */
825 atif = atif_add_device(dev, &sa->sat_addr);
832 * Check if the chosen address is used. If so we
833 * error and atalkd will try another.
836 if (!(dev->flags & IFF_LOOPBACK) &&
837 !(dev->flags & IFF_POINTOPOINT) &&
838 atif_probe_device(atif) < 0) {
839 atif_drop_device(dev);
843 /* Hey it worked - add the direct routes */
844 sa = (struct sockaddr_at *) &rtdef.rt_gateway;
845 sa->sat_family = AF_APPLETALK;
846 sa->sat_addr.s_net = atif->address.s_net;
847 sa->sat_addr.s_node = atif->address.s_node;
848 sa = (struct sockaddr_at *) &rtdef.rt_dst;
849 rtdef.rt_flags = RTF_UP;
850 sa->sat_family = AF_APPLETALK;
851 sa->sat_addr.s_node = ATADDR_ANYNODE;
852 if (dev->flags & IFF_LOOPBACK ||
853 dev->flags & IFF_POINTOPOINT)
854 rtdef.rt_flags |= RTF_HOST;
856 /* Routerless initial state */
857 if (nr->nr_firstnet == htons(0) &&
858 nr->nr_lastnet == htons(0xFFFE)) {
859 sa->sat_addr.s_net = atif->address.s_net;
860 atrtr_create(&rtdef, dev);
861 atrtr_set_default(dev);
863 limit = ntohs(nr->nr_lastnet);
864 if (limit - ntohs(nr->nr_firstnet) > 4096) {
865 printk(KERN_WARNING "Too many routes/"
870 for (ct = ntohs(nr->nr_firstnet);
872 sa->sat_addr.s_net = htons(ct);
873 atrtr_create(&rtdef, dev);
876 dev_mc_add(dev, aarp_mcast, 6, 1);
881 return -EADDRNOTAVAIL;
883 sa->sat_family = AF_APPLETALK;
884 sa->sat_addr = atif->address;
889 return -EADDRNOTAVAIL;
891 sa->sat_family = AF_APPLETALK;
892 sa->sat_addr.s_net = atif->address.s_net;
893 sa->sat_addr.s_node = ATADDR_BCAST;
896 case SIOCATALKDIFADDR:
898 if (!capable(CAP_NET_ADMIN))
900 if (sa->sat_family != AF_APPLETALK)
906 if (!capable(CAP_NET_ADMIN))
908 if (sa->sat_family != AF_APPLETALK)
911 return -EADDRNOTAVAIL;
914 * for now, we only support proxy AARP on ELAP;
915 * we should be able to do it for LocalTalk, too.
917 if (dev->type != ARPHRD_ETHER)
918 return -EPROTONOSUPPORT;
921 * atif points to the current interface on this network;
922 * we aren't concerned about its current status (at
923 * least for now), but it has all the settings about
924 * the network we're going to probe. Consequently, it
928 return -EADDRNOTAVAIL;
930 nr = (struct netrange *) &(atif->nets);
932 * Phase 1 is fine on Localtalk but we don't do
933 * Ethertalk phase 1. Anyone wanting to add it go ahead.
935 if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
936 return -EPROTONOSUPPORT;
938 if (sa->sat_addr.s_node == ATADDR_BCAST ||
939 sa->sat_addr.s_node == 254)
943 * Check if the chosen address is used. If so we
944 * error and ATCP will try another.
946 if (atif_proxy_probe_device(atif, &(sa->sat_addr)) < 0)
950 * We now have an address on the local network, and
951 * the AARP code will defend it for us until we take it
952 * down. We don't set up any routes right now, because
953 * ATCP will install them manually via SIOCADDRT.
958 if (!capable(CAP_NET_ADMIN))
960 if (sa->sat_family != AF_APPLETALK)
963 return -EADDRNOTAVAIL;
965 /* give to aarp module to remove proxy entry */
966 aarp_proxy_remove(atif->dev, &(sa->sat_addr));
970 return copy_to_user(arg, &atreq, sizeof(atreq)) ? -EFAULT : 0;
973 /* Routing ioctl() calls */
974 static int atrtr_ioctl(unsigned int cmd, void *arg)
976 struct net_device *dev = NULL;
979 if (copy_from_user(&rt, arg, sizeof(rt)))
984 if (rt.rt_dst.sa_family != AF_APPLETALK)
986 return atrtr_delete(&((struct sockaddr_at *)
987 &rt.rt_dst)->sat_addr);
990 /* FIXME: the name of the device is still in user
991 * space, isn't it? */
993 dev = __dev_get_by_name(rt.rt_dev);
997 return atrtr_create(&rt, dev);
1002 /* Called from proc fs - just make it print the ifaces neatly */
1003 static int atalk_if_get_info(char *buffer, char **start, off_t offset,
1008 struct atalk_iface *iface;
1009 int len = sprintf(buffer, "Interface Address "
1010 "Networks Status\n");
1012 spin_lock_bh(&atalk_iface_lock);
1013 for (iface = atalk_iface_list; iface; iface = iface->next) {
1014 len += sprintf(buffer+len,"%-16s %04X:%02X %04X-%04X %d\n",
1015 iface->dev->name, ntohs(iface->address.s_net),
1016 iface->address.s_node,
1017 ntohs(iface->nets.nr_firstnet),
1018 ntohs(iface->nets.nr_lastnet), iface->status);
1024 if (pos > offset + length)
1027 spin_unlock_bh(&atalk_iface_lock);
1029 *start = buffer + (offset - begin);
1030 len -= (offset - begin);
1036 /* Called from proc fs - just make it print the routes neatly */
1037 static int atalk_rt_get_info(char *buffer, char **start, off_t offset,
1042 int len = sprintf(buffer, "Target Router Flags Dev\n");
1043 struct atalk_route *rt;
1045 if (atrtr_default.dev) {
1046 rt = &atrtr_default;
1047 len += sprintf(buffer + len,"Default %04X:%02X %-4d %s\n",
1048 ntohs(rt->gateway.s_net), rt->gateway.s_node,
1049 rt->flags, rt->dev->name);
1052 read_lock_bh(&atalk_router_lock);
1053 for (rt = atalk_router_list; rt; rt = rt->next) {
1054 len += sprintf(buffer + len,
1055 "%04X:%02X %04X:%02X %-4d %s\n",
1056 ntohs(rt->target.s_net), rt->target.s_node,
1057 ntohs(rt->gateway.s_net), rt->gateway.s_node,
1058 rt->flags, rt->dev->name);
1064 if (pos > offset + length)
1067 read_unlock_bh(&atalk_router_lock);
1069 *start = buffer + (offset - begin);
1070 len -= (offset - begin);
1076 /**************************************************************************\
1078 * Handling for system calls applied via the various interfaces to an *
1079 * AppleTalk socket object. *
1081 \**************************************************************************/
1084 * Checksum: This is 'optional'. It's quite likely also a good
1085 * candidate for assembler hackery 8)
1087 unsigned short atalk_checksum(struct ddpehdr *ddp, int len)
1089 unsigned long sum = 0; /* Assume unsigned long is >16 bits */
1090 unsigned char *data = (unsigned char *) ddp;
1092 len -= 4; /* skip header 4 bytes */
1095 /* This ought to be unwrapped neatly. I'll trust gcc for now */
1099 if (sum & 0x10000) {
1105 /* Use 0xFFFF for 0. 0 itself means none */
1106 return sum ? htons((unsigned short) sum) : 0xFFFF;
1110 * Create a socket. Initialise the socket, blank the addresses
1113 static int atalk_create(struct socket *sock, int protocol)
1115 struct sock *sk = sk_alloc(PF_APPLETALK, GFP_KERNEL, 1);
1120 switch (sock->type) {
1122 * We permit SOCK_DGRAM and RAW is an extension. It is
1123 * trivial to do and gives you the full ELAP frame.
1124 * Should be handy for CAP 8)
1128 sock->ops = &atalk_dgram_ops;
1133 * TODO: if you want to implement ADSP, here's the
1137 sock->ops = &atalk_stream_ops;
1142 return -ESOCKTNOSUPPORT;
1146 sock_init_data(sock, sk);
1147 sk->destruct = NULL;
1148 /* Checksums on by default */
1153 /* Free a socket. No work needed */
1154 static int atalk_release(struct socket *sock)
1156 struct sock *sk = sock->sk;
1162 sk->state_change(sk);
1166 atalk_destroy_socket(sk);
1171 * Pick a source port when one is not given. If we can
1172 * find a suitable free one, we insert the socket into
1173 * the tables using it.
1175 * This whole operation must be atomic.
1177 static int atalk_pick_and_bind_port(struct sock *sk, struct sockaddr_at *sat)
1182 spin_lock_bh(&atalk_sockets_lock);
1184 for (sat->sat_port = ATPORT_RESERVED;
1185 sat->sat_port < ATPORT_LAST;
1187 for (s = atalk_sockets; s; s = s->next) {
1188 if (s->protinfo.af_at.src_net == sat->sat_addr.s_net &&
1189 s->protinfo.af_at.src_node ==
1190 sat->sat_addr.s_node &&
1191 s->protinfo.af_at.src_port == sat->sat_port)
1195 /* Wheee, it's free, assign and insert. */
1196 sk->next = atalk_sockets;
1198 atalk_sockets->pprev = &sk->next;
1200 sk->pprev = &atalk_sockets;
1201 sk->protinfo.af_at.src_port = sat->sat_port;
1210 out: spin_unlock_bh(&atalk_sockets_lock);
1214 static int atalk_autobind(struct sock *sk)
1216 struct sockaddr_at sat;
1218 struct at_addr *ap = atalk_find_primary();
1220 if (!ap || ap->s_net == htons(ATADDR_ANYNET))
1221 return -EADDRNOTAVAIL;
1223 sk->protinfo.af_at.src_net = sat.sat_addr.s_net = ap->s_net;
1224 sk->protinfo.af_at.src_node = sat.sat_addr.s_node = ap->s_node;
1226 n = atalk_pick_and_bind_port(sk, &sat);
1234 /* Set the address 'our end' of the connection */
1235 static int atalk_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1237 struct sockaddr_at *addr = (struct sockaddr_at *)uaddr;
1238 struct sock *sk = sock->sk;
1240 if (!sk->zapped || addr_len != sizeof(struct sockaddr_at))
1243 if (addr->sat_family != AF_APPLETALK)
1244 return -EAFNOSUPPORT;
1246 if (addr->sat_addr.s_net == htons(ATADDR_ANYNET)) {
1247 struct at_addr *ap = atalk_find_primary();
1250 return -EADDRNOTAVAIL;
1252 sk->protinfo.af_at.src_net = addr->sat_addr.s_net = ap->s_net;
1253 sk->protinfo.af_at.src_node = addr->sat_addr.s_node= ap->s_node;
1255 if (!atalk_find_interface(addr->sat_addr.s_net,
1256 addr->sat_addr.s_node))
1257 return -EADDRNOTAVAIL;
1259 sk->protinfo.af_at.src_net = addr->sat_addr.s_net;
1260 sk->protinfo.af_at.src_node = addr->sat_addr.s_node;
1263 if (addr->sat_port == ATADDR_ANYPORT) {
1264 int n = atalk_pick_and_bind_port(sk, addr);
1269 sk->protinfo.af_at.src_port = addr->sat_port;
1271 if (atalk_find_or_insert_socket(sk, addr))
1279 /* Set the address we talk to */
1280 static int atalk_connect(struct socket *sock, struct sockaddr *uaddr,
1281 int addr_len, int flags)
1283 struct sock *sk = sock->sk;
1284 struct sockaddr_at *addr;
1286 sk->state = TCP_CLOSE;
1287 sock->state = SS_UNCONNECTED;
1289 if (addr_len != sizeof(*addr))
1292 addr = (struct sockaddr_at *)uaddr;
1294 if (addr->sat_family != AF_APPLETALK)
1295 return -EAFNOSUPPORT;
1297 if (addr->sat_addr.s_node == ATADDR_BCAST && !sk->broadcast) {
1299 printk(KERN_WARNING "%s is broken and did not set "
1300 "SO_BROADCAST. It will break when 2.2 is "
1309 if (atalk_autobind(sk) < 0)
1312 if (!atrtr_get_dev(&addr->sat_addr))
1313 return -ENETUNREACH;
1315 sk->protinfo.af_at.dest_port = addr->sat_port;
1316 sk->protinfo.af_at.dest_net = addr->sat_addr.s_net;
1317 sk->protinfo.af_at.dest_node = addr->sat_addr.s_node;
1319 sock->state = SS_CONNECTED;
1320 sk->state = TCP_ESTABLISHED;
1326 * Find the name of an AppleTalk socket. Just copy the right
1327 * fields into the sockaddr.
1329 static int atalk_getname(struct socket *sock, struct sockaddr *uaddr,
1330 int *uaddr_len, int peer)
1332 struct sockaddr_at sat;
1333 struct sock *sk = sock->sk;
1336 if (atalk_autobind(sk) < 0)
1339 *uaddr_len = sizeof(struct sockaddr_at);
1342 if (sk->state != TCP_ESTABLISHED)
1345 sat.sat_addr.s_net = sk->protinfo.af_at.dest_net;
1346 sat.sat_addr.s_node = sk->protinfo.af_at.dest_node;
1347 sat.sat_port = sk->protinfo.af_at.dest_port;
1349 sat.sat_addr.s_net = sk->protinfo.af_at.src_net;
1350 sat.sat_addr.s_node = sk->protinfo.af_at.src_node;
1351 sat.sat_port = sk->protinfo.af_at.src_port;
1354 sat.sat_family = AF_APPLETALK;
1355 memcpy(uaddr, &sat, sizeof(sat));
1360 * Receive a packet (in skb) from device dev. This has come from the SNAP
1361 * decoder, and on entry skb->h.raw is the DDP header, skb->len is the DDP
1362 * header, skb->len is the DDP length. The physical headers have been
1363 * extracted. PPP should probably pass frames marked as for this layer.
1364 * [ie ARPHRD_ETHERTALK]
1366 static int atalk_rcv(struct sk_buff *skb, struct net_device *dev,
1367 struct packet_type *pt)
1369 struct ddpehdr *ddp = (void *) skb->h.raw;
1371 struct atalk_iface *atif;
1372 struct sockaddr_at tosat;
1374 struct ddpebits ddphv;
1377 if (skb->len < sizeof(*ddp))
1381 * Fix up the length field [Ok this is horrible but otherwise
1382 * I end up with unions of bit fields and messy bit field order
1383 * compiler/endian dependencies..]
1385 * FIXME: This is a write to a shared object. Granted it
1386 * happens to be safe BUT.. (Its safe as user space will not
1387 * run until we put it back)
1389 *((__u16 *)&ddphv) = ntohs(*((__u16 *)ddp));
1391 /* Trim buffer in case of stray trailing data */
1393 skb_trim(skb, min_t(unsigned int, skb->len, ddphv.deh_len));
1396 * Size check to see if ddp->deh_len was crap
1397 * (Otherwise we'll detonate most spectacularly
1398 * in the middle of recvmsg()).
1400 if (skb->len < sizeof(*ddp))
1404 * Any checksums. Note we don't do htons() on this == is assumed to be
1405 * valid for net byte orders all over the networking code...
1408 atalk_checksum(ddp, ddphv.deh_len) != ddp->deh_sum)
1409 /* Not a valid AppleTalk frame - dustbin time */
1412 /* Check the packet is aimed at us */
1413 if (!ddp->deh_dnet) /* Net 0 is 'this network' */
1414 atif = atalk_find_anynet(ddp->deh_dnode, dev);
1416 atif = atalk_find_interface(ddp->deh_dnet, ddp->deh_dnode);
1418 /* Not ours, so we route the packet via the correct AppleTalk iface */
1420 struct atalk_route *rt;
1424 * Don't route multicast, etc., packets, or packets
1425 * sent to "this network"
1427 if (skb->pkt_type != PACKET_HOST || !ddp->deh_dnet) {
1429 * Can it ever happen that a packet is from a PPP
1430 * iface and needs to be broadcast onto the default
1432 if (dev->type == ARPHRD_PPP)
1433 printk(KERN_DEBUG "AppleTalk: didn't forward "
1434 "broadcast packet received "
1435 "from PPP iface\n");
1439 ta.s_net = ddp->deh_dnet;
1440 ta.s_node = ddp->deh_dnode;
1442 /* Route the packet */
1443 rt = atrtr_find(&ta);
1444 if (!rt || ddphv.deh_hops == DDP_MAXHOPS)
1449 * Route goes through another gateway, so
1450 * set the target to the gateway instead.
1452 if (rt->flags & RTF_GATEWAY) {
1453 ta.s_net = rt->gateway.s_net;
1454 ta.s_node = rt->gateway.s_node;
1457 /* Fix up skb->len field */
1458 skb_trim(skb, min_t(unsigned int, origlen, rt->dev->hard_header_len +
1459 ddp_dl->header_length + ddphv.deh_len));
1461 /* Mend the byte order */
1462 *((__u16 *)ddp) = ntohs(*((__u16 *)&ddphv));
1465 * Send the buffer onwards
1467 * Now we must always be careful. If it's come from
1468 * LocalTalk to EtherTalk it might not fit
1470 * Order matters here: If a packet has to be copied
1471 * to make a new headroom (rare hopefully) then it
1472 * won't need unsharing.
1474 * Note. ddp-> becomes invalid at the realloc.
1476 if (skb_headroom(skb) < 22) {
1477 /* 22 bytes - 12 ether, 2 len, 3 802.2 5 snap */
1478 struct sk_buff *nskb = skb_realloc_headroom(skb, 32);
1484 skb = skb_unshare(skb, GFP_ATOMIC);
1487 * If the buffer didn't vanish into the lack of
1488 * space bitbucket we can send it.
1490 if (skb && aarp_send_ddp(rt->dev, skb, &ta, NULL) == -1)
1495 #if defined(CONFIG_IPDDP) || defined(CONFIG_IPDDP_MODULE)
1496 /* Check if IP-over-DDP */
1497 if (skb->data[12] == 22) {
1498 struct net_device *dev = __dev_get_by_name("ipddp0");
1499 struct net_device_stats *stats;
1501 /* This needs to be able to handle ipddp"N" devices */
1505 skb->protocol = htons(ETH_P_IP);
1508 skb->h.raw = skb->data;
1511 stats->rx_packets++;
1512 stats->rx_bytes += skb->len + 13;
1513 netif_rx(skb); /* Send the SKB up to a higher place. */
1518 * Which socket - atalk_search_socket() looks for a *full match*
1519 * of the <net,node,port> tuple.
1521 tosat.sat_addr.s_net = ddp->deh_dnet;
1522 tosat.sat_addr.s_node = ddp->deh_dnode;
1523 tosat.sat_port = ddp->deh_dport;
1525 sock = atalk_search_socket(&tosat, atif);
1526 if (!sock) /* But not one of our sockets */
1529 /* Queue packet (standard) */
1532 if (sock_queue_rcv_skb(sock, skb) < 0)
1535 freeit: kfree_skb(skb);
1540 * Receive a LocalTalk frame. We make some demands on the caller here.
1541 * Caller must provide enough headroom on the packet to pull the short
1542 * header and append a long one.
1544 static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev,
1545 struct packet_type *pt)
1547 struct ddpehdr *ddp;
1550 /* Expand any short form frames */
1551 if (skb->mac.raw[2] == 1) {
1552 /* Find our address */
1554 ap = atalk_find_dev_addr(dev);
1555 if (!ap || skb->len < sizeof(struct ddpshdr)) {
1561 * The push leaves us with a ddephdr not an shdr, and
1562 * handily the port bytes in the right place preset.
1565 skb_push(skb, sizeof(*ddp) - 4);
1566 ddp = (struct ddpehdr *)skb->data;
1568 /* Now fill in the long header */
1571 * These two first. The mac overlays the new source/dest
1572 * network information so we MUST copy these before
1573 * we write the network numbers !
1576 ddp->deh_dnode = skb->mac.raw[0]; /* From physical header */
1577 ddp->deh_snode = skb->mac.raw[1]; /* From physical header */
1579 ddp->deh_dnet = ap->s_net; /* Network number */
1580 ddp->deh_snet = ap->s_net;
1581 ddp->deh_sum = 0; /* No checksum */
1583 * Not sure about this bit...
1585 ddp->deh_len = skb->len;
1586 ddp->deh_hops = DDP_MAXHOPS; /* Non routable, so force a drop
1587 if we slip up later */
1588 /* Mend the byte order */
1589 *((__u16 *)ddp) = htons(*((__u16 *)ddp));
1591 skb->h.raw = skb->data;
1593 return atalk_rcv(skb, dev, pt);
1596 static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, int len,
1597 struct scm_cookie *scm)
1599 struct sock *sk = sock->sk;
1600 struct sockaddr_at *usat = (struct sockaddr_at *)msg->msg_name;
1601 int flags = msg->msg_flags;
1603 struct sockaddr_at local_satalk, gsat;
1604 struct sk_buff *skb;
1605 struct net_device *dev;
1606 struct ddpehdr *ddp;
1608 struct atalk_route *rt;
1611 if (flags & ~MSG_DONTWAIT)
1614 if (len > DDP_MAXSZ)
1619 if (atalk_autobind(sk) < 0)
1622 if (msg->msg_namelen < sizeof(*usat) ||
1623 usat->sat_family != AF_APPLETALK)
1626 /* netatalk doesn't implement this check */
1627 if (usat->sat_addr.s_node == ATADDR_BCAST && !sk->broadcast) {
1628 printk(KERN_INFO "SO_BROADCAST: Fix your netatalk as "
1629 "it will break before 2.2\n");
1635 if (sk->state != TCP_ESTABLISHED)
1637 usat = &local_satalk;
1638 usat->sat_family = AF_APPLETALK;
1639 usat->sat_port = sk->protinfo.af_at.dest_port;
1640 usat->sat_addr.s_node = sk->protinfo.af_at.dest_node;
1641 usat->sat_addr.s_net = sk->protinfo.af_at.dest_net;
1644 /* Build a packet */
1645 SOCK_DEBUG(sk, "SK %p: Got address.\n", sk);
1648 size = sizeof(struct ddpehdr) + len + ddp_dl->header_length;
1650 if (usat->sat_addr.s_net || usat->sat_addr.s_node == ATADDR_ANYNODE) {
1651 rt = atrtr_find(&usat->sat_addr);
1653 return -ENETUNREACH;
1657 struct at_addr at_hint;
1660 at_hint.s_net = sk->protinfo.af_at.src_net;
1662 rt = atrtr_find(&at_hint);
1664 return -ENETUNREACH;
1669 SOCK_DEBUG(sk, "SK %p: Size needed %d, device %s\n",
1670 sk, size, dev->name);
1672 size += dev->hard_header_len;
1673 skb = sock_alloc_send_skb(sk, size, (flags & MSG_DONTWAIT), &err);
1678 skb_reserve(skb, ddp_dl->header_length);
1679 skb_reserve(skb, dev->hard_header_len);
1682 SOCK_DEBUG(sk, "SK %p: Begin build.\n", sk);
1684 ddp = (struct ddpehdr *)skb_put(skb, sizeof(struct ddpehdr));
1687 ddp->deh_len = len + sizeof(*ddp);
1689 * Fix up the length field [Ok this is horrible but otherwise
1690 * I end up with unions of bit fields and messy bit field order
1691 * compiler/endian dependencies..
1693 *((__u16 *)ddp) = ntohs(*((__u16 *)ddp));
1695 ddp->deh_dnet = usat->sat_addr.s_net;
1696 ddp->deh_snet = sk->protinfo.af_at.src_net;
1697 ddp->deh_dnode = usat->sat_addr.s_node;
1698 ddp->deh_snode = sk->protinfo.af_at.src_node;
1699 ddp->deh_dport = usat->sat_port;
1700 ddp->deh_sport = sk->protinfo.af_at.src_port;
1702 SOCK_DEBUG(sk, "SK %p: Copy user data (%d bytes).\n", sk, len);
1704 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1710 if (sk->no_check == 1)
1713 ddp->deh_sum = atalk_checksum(ddp, len + sizeof(*ddp));
1716 * Loopback broadcast packets to non gateway targets (ie routes
1717 * to group we are in)
1719 if (ddp->deh_dnode == ATADDR_BCAST &&
1720 !(rt->flags & RTF_GATEWAY) && !(dev->flags & IFF_LOOPBACK)) {
1721 struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL);
1725 SOCK_DEBUG(sk, "SK %p: send out(copy).\n", sk);
1726 if (aarp_send_ddp(dev, skb2,
1727 &usat->sat_addr, NULL) == -1)
1729 /* else queued/sent above in the aarp queue */
1733 if (dev->flags & IFF_LOOPBACK || loopback) {
1734 SOCK_DEBUG(sk, "SK %p: Loop back.\n", sk);
1737 ddp_dl->datalink_header(ddp_dl, skb, dev->dev_addr);
1738 skb->mac.raw = skb->data;
1739 skb->h.raw = skb->data + ddp_dl->header_length +
1740 dev->hard_header_len;
1741 skb_pull(skb, dev->hard_header_len);
1742 skb_pull(skb, ddp_dl->header_length);
1743 atalk_rcv(skb, dev, NULL);
1745 SOCK_DEBUG(sk, "SK %p: send out.\n", sk);
1746 if (rt->flags & RTF_GATEWAY) {
1747 gsat.sat_addr = rt->gateway;
1751 if (aarp_send_ddp(dev, skb, &usat->sat_addr, NULL) == -1)
1753 /* else queued/sent above in the aarp queue */
1755 SOCK_DEBUG(sk, "SK %p: Done write (%d).\n", sk, len);
1760 static int atalk_recvmsg(struct socket *sock, struct msghdr *msg, int size,
1761 int flags, struct scm_cookie *scm)
1763 struct sock *sk = sock->sk;
1764 struct sockaddr_at *sat = (struct sockaddr_at *)msg->msg_name;
1765 struct ddpehdr *ddp = NULL;
1768 struct ddpebits ddphv;
1769 struct sk_buff *skb;
1771 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1772 flags & MSG_DONTWAIT, &err);
1776 ddp = (struct ddpehdr *)(skb->h.raw);
1777 *((__u16 *)&ddphv) = ntohs(*((__u16 *)ddp));
1779 if (sk->type == SOCK_RAW) {
1780 copied = ddphv.deh_len;
1781 if (copied > size) {
1783 msg->msg_flags |= MSG_TRUNC;
1786 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1788 copied = ddphv.deh_len - sizeof(*ddp);
1789 if (copied > size) {
1791 msg->msg_flags |= MSG_TRUNC;
1793 err = skb_copy_datagram_iovec(skb, sizeof(*ddp),
1794 msg->msg_iov, copied);
1799 sat->sat_family = AF_APPLETALK;
1800 sat->sat_port = ddp->deh_sport;
1801 sat->sat_addr.s_node = ddp->deh_snode;
1802 sat->sat_addr.s_net = ddp->deh_snet;
1804 msg->msg_namelen = sizeof(*sat);
1807 skb_free_datagram(sk, skb); /* Free the datagram. */
1808 return err ? err : copied;
1813 * AppleTalk ioctl calls.
1815 static int atalk_ioctl(struct socket *sock,unsigned int cmd, unsigned long arg)
1818 struct sock *sk = sock->sk;
1821 /* Protocol layer */
1823 amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1829 /* These two are safe on a single CPU system as only
1830 * user tasks fiddle here */
1831 struct sk_buff *skb = skb_peek(&sk->receive_queue);
1834 amount = skb->len-sizeof(struct ddpehdr);
1840 if (!sk->stamp.tv_sec)
1842 return copy_to_user((void *)arg, &sk->stamp,
1843 sizeof(struct timeval)) ? -EFAULT : 0;
1847 if (!capable(CAP_NET_ADMIN))
1849 return atrtr_ioctl(cmd, (void *)arg);
1853 case SIOCGIFBRDADDR:
1854 case SIOCATALKDIFADDR:
1856 case SIOCSARP: /* proxy AARP */
1857 case SIOCDARP: /* proxy AARP */
1862 ret = atif_ioctl(cmd, (void *)arg);
1867 /* Physical layer ioctl calls */
1880 return dev_ioctl(cmd,(void *) arg);
1882 case SIOCSIFBRDADDR:
1883 case SIOCGIFNETMASK:
1884 case SIOCSIFNETMASK:
1887 case SIOCGIFDSTADDR:
1888 case SIOCSIFDSTADDR:
1894 return put_user(amount, (int *)arg);
1897 static struct net_proto_family atalk_family_ops =
1903 static struct proto_ops SOCKOPS_WRAPPED(atalk_dgram_ops)=
1905 family: PF_APPLETALK,
1907 release: atalk_release,
1909 connect: atalk_connect,
1910 socketpair: sock_no_socketpair,
1911 accept: sock_no_accept,
1912 getname: atalk_getname,
1913 poll: datagram_poll,
1915 listen: sock_no_listen,
1916 shutdown: sock_no_shutdown,
1917 setsockopt: sock_no_setsockopt,
1918 getsockopt: sock_no_getsockopt,
1919 sendmsg: atalk_sendmsg,
1920 recvmsg: atalk_recvmsg,
1922 sendpage: sock_no_sendpage,
1925 #include <linux/smp_lock.h>
1926 SOCKOPS_WRAP(atalk_dgram, PF_APPLETALK);
1928 static struct notifier_block ddp_notifier=
1935 struct packet_type ltalk_packet_type=
1944 struct packet_type ppptalk_packet_type=
1953 static char ddp_snap_id[] = {0x08, 0x00, 0x07, 0x80, 0x9B};
1955 /* Export symbols for use by drivers when AppleTalk is a module */
1956 EXPORT_SYMBOL(aarp_send_ddp);
1957 EXPORT_SYMBOL(atrtr_get_dev);
1958 EXPORT_SYMBOL(atalk_find_dev_addr);
1960 /* Called by proto.c on kernel start up */
1961 static int __init atalk_init(void)
1963 (void) sock_register(&atalk_family_ops);
1964 ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv);
1966 printk(KERN_CRIT "Unable to register DDP with SNAP.\n");
1968 ltalk_packet_type.type = htons(ETH_P_LOCALTALK);
1969 dev_add_pack(<alk_packet_type);
1971 ppptalk_packet_type.type = htons(ETH_P_PPPTALK);
1972 dev_add_pack(&ppptalk_packet_type);
1974 register_netdevice_notifier(&ddp_notifier);
1977 proc_net_create("appletalk", 0, atalk_get_info);
1978 proc_net_create("atalk_route", 0, atalk_rt_get_info);
1979 proc_net_create("atalk_iface", 0, atalk_if_get_info);
1980 #ifdef CONFIG_PROC_FS
1981 aarp_register_proc_fs();
1982 #endif /* CONFIG_PROC_FS */
1983 #ifdef CONFIG_SYSCTL
1984 atalk_register_sysctl();
1985 #endif /* CONFIG_SYSCTL */
1986 printk(KERN_INFO "NET4: AppleTalk 0.18a for Linux NET4.0\n");
1989 module_init(atalk_init);
1993 * Note on MOD_{INC,DEC}_USE_COUNT:
1995 * Use counts are incremented/decremented when
1996 * sockets are created/deleted.
1998 * AppleTalk interfaces are not incremented until atalkd is run
1999 * and are only decremented when they are downed.
2001 * Ergo, before the AppleTalk module can be removed, all AppleTalk
2002 * sockets be closed from user space.
2004 static void __exit atalk_exit(void)
2006 #ifdef CONFIG_SYSCTL
2007 atalk_unregister_sysctl();
2008 #endif /* CONFIG_SYSCTL */
2009 proc_net_remove("appletalk");
2010 proc_net_remove("atalk_route");
2011 proc_net_remove("atalk_iface");
2012 #ifdef CONFIG_PROC_FS
2013 aarp_unregister_proc_fs();
2014 #endif /* CONFIG_PROC_FS */
2015 aarp_cleanup_module(); /* General aarp clean-up. */
2016 unregister_netdevice_notifier(&ddp_notifier);
2017 dev_remove_pack(<alk_packet_type);
2018 dev_remove_pack(&ppptalk_packet_type);
2019 unregister_snap_client(ddp_snap_id);
2020 sock_unregister(PF_APPLETALK);
2022 module_exit(atalk_exit);
2024 #endif /* CONFIG_ATALK || CONFIG_ATALK_MODULE */