* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / net / appletalk / ddp.c
blob4ab75098d6e1eef1ece93c3e0c11b583fe1181bf
1 /*
2 * DDP: An implementation of the AppleTalk DDP protocol for
3 * Ethernet 'ELAP'.
5 * Alan Cox <Alan.Cox@linux.org>
7 * With more than a little assistance from
9 * Wesley Craig <netatalk@umich.edu>
11 * Fixes:
12 * Michael Callahan : Made routing work
13 * Wesley Craig : Fix probing to listen to a
14 * passed node id.
15 * Alan Cox : Added send/recvmsg support
16 * Alan Cox : Moved at. to protinfo in
17 * socket.
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
24 * LocalTalk hook).
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 proc fs,
33 * moved probing to AARP module.
34 * Adrian Sun/
35 * Michael Zuelsdorff : fix for net.0 packets. don't
36 * allow illegal ether/tokentalk
37 * port assignment. we lose a
38 * valid localtalk port as a
39 * result.
42 * This program is free software; you can redistribute it and/or
43 * modify it under the terms of the GNU General Public License
44 * as published by the Free Software Foundation; either version
45 * 2 of the License, or (at your option) any later version.
49 #include <linux/config.h>
50 #if defined(CONFIG_ATALK) || defined(CONFIG_ATALK_MODULE)
51 #include <linux/module.h>
52 #include <asm/uaccess.h>
53 #include <asm/system.h>
54 #include <asm/bitops.h>
55 #include <linux/types.h>
56 #include <linux/kernel.h>
57 #include <linux/sched.h>
58 #include <linux/string.h>
59 #include <linux/mm.h>
60 #include <linux/socket.h>
61 #include <linux/sockios.h>
62 #include <linux/in.h>
63 #include <linux/errno.h>
64 #include <linux/interrupt.h>
65 #include <linux/if_ether.h>
66 #include <linux/notifier.h>
67 #include <linux/netdevice.h>
68 #include <linux/inetdevice.h>
69 #include <linux/route.h>
70 #include <linux/inet.h>
71 #include <linux/etherdevice.h>
72 #include <linux/if_arp.h>
73 #include <linux/skbuff.h>
74 #include <linux/termios.h> /* For TIOCOUTQ/INQ */
75 #include <net/datalink.h>
76 #include <net/p8022.h>
77 #include <net/psnap.h>
78 #include <net/sock.h>
79 #include <linux/ip.h>
80 #include <net/route.h>
81 #include <linux/atalk.h>
82 #include <linux/proc_fs.h>
83 #include <linux/stat.h>
84 #include <linux/init.h>
87 #ifdef CONFIG_PROC_FS
88 extern void aarp_register_proc_fs(void);
89 extern void aarp_unregister_proc_fs(void);
90 #endif
92 extern void aarp_probe_network(struct atalk_iface *atif);
93 extern int aarp_proxy_probe_network(struct atalk_iface *atif, struct at_addr *sa);
94 extern void aarp_proxy_remove(struct net_device *dev, struct at_addr *sa);
97 #undef APPLETALK_DEBUG
99 #ifdef APPLETALK_DEBUG
100 #define DPRINT(x) print(x)
101 #else
102 #define DPRINT(x)
103 #endif /* APPLETALK_DEBUG */
105 #ifdef CONFIG_SYSCTL
106 extern inline void atalk_register_sysctl(void);
107 extern inline void atalk_unregister_sysctl(void);
108 #endif /* CONFIG_SYSCTL */
110 struct datalink_proto *ddp_dl, *aarp_dl;
111 static struct proto_ops atalk_dgram_ops;
113 #define min(a,b) (((a)<(b))?(a):(b))
115 /**************************************************************************\
117 * Handlers for the socket list. *
119 \**************************************************************************/
121 static struct sock *atalk_socket_list = NULL;
124 * Note: Sockets may not be removed _during_ an interrupt or inet_bh
125 * handler using this technique. They can be added although we do not
126 * use this facility.
129 extern inline void atalk_remove_socket(struct sock *sk)
131 sklist_remove_socket(&atalk_socket_list,sk);
134 extern inline void atalk_insert_socket(struct sock *sk)
136 sklist_insert_socket(&atalk_socket_list,sk);
139 static struct sock *atalk_search_socket(struct sockaddr_at *to, struct atalk_iface *atif)
141 struct sock *s;
143 for(s = atalk_socket_list; s != NULL; s = s->next)
145 if(to->sat_port != s->protinfo.af_at.src_port)
147 continue;
150 if(to->sat_addr.s_net == ATADDR_ANYNET
151 && to->sat_addr.s_node == ATADDR_BCAST
152 && s->protinfo.af_at.src_net == atif->address.s_net)
154 break;
157 if(to->sat_addr.s_net == s->protinfo.af_at.src_net
158 && (to->sat_addr.s_node == s->protinfo.af_at.src_node
159 || to->sat_addr.s_node == ATADDR_BCAST
160 || to->sat_addr.s_node == ATADDR_ANYNODE))
162 break;
165 /* XXXX.0 -- we got a request for this router. make sure
166 * that the node is appropriately set. */
167 if (to->sat_addr.s_node == ATADDR_ANYNODE &&
168 to->sat_addr.s_net != ATADDR_ANYNET &&
169 atif->address.s_node == s->protinfo.af_at.src_node) {
170 to->sat_addr.s_node = atif->address.s_node;
171 break;
175 return (s);
179 * Find a socket in the list.
181 static struct sock *atalk_find_socket(struct sockaddr_at *sat)
183 struct sock *s;
185 for(s = atalk_socket_list; s != NULL; s = s->next)
187 if(s->protinfo.af_at.src_net != sat->sat_addr.s_net)
189 continue;
192 if(s->protinfo.af_at.src_node != sat->sat_addr.s_node)
194 continue;
197 if(s->protinfo.af_at.src_port != sat->sat_port)
199 continue;
202 break;
205 return (s);
208 extern inline void atalk_destroy_socket(struct sock *sk)
210 sklist_destroy_socket(&atalk_socket_list, sk);
211 MOD_DEC_USE_COUNT;
215 * Called from proc fs
217 int atalk_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
219 struct sock *s;
220 int len=0;
221 off_t pos=0;
222 off_t begin=0;
225 * Output the AppleTalk data for the /proc filesystem.
228 len += sprintf(buffer,"Type local_addr remote_addr tx_queue rx_queue st uid\n");
229 for(s = atalk_socket_list; s != NULL; s = s->next)
231 len += sprintf(buffer+len,"%02X ", s->type);
232 len += sprintf(buffer+len,"%04X:%02X:%02X ",
233 ntohs(s->protinfo.af_at.src_net),
234 s->protinfo.af_at.src_node,
235 s->protinfo.af_at.src_port);
236 len += sprintf(buffer+len,"%04X:%02X:%02X ",
237 ntohs(s->protinfo.af_at.dest_net),
238 s->protinfo.af_at.dest_node,
239 s->protinfo.af_at.dest_port);
240 len += sprintf(buffer+len,"%08X:%08X ",
241 atomic_read(&s->wmem_alloc),
242 atomic_read(&s->rmem_alloc));
243 len += sprintf(buffer+len,"%02X %d\n", s->state,
244 SOCK_INODE(s->socket)->i_uid);
246 /* Are we still dumping unwanted data then discard the record */
247 pos = begin + len;
249 if(pos < offset)
251 len = 0; /* Keep dumping into the buffer start */
252 begin = pos;
254 if(pos > offset + length) /* We have dumped enough */
255 break;
258 /* The data in question runs from begin to begin+len */
259 *start = buffer + (offset - begin); /* Start of wanted data */
260 len -= (offset - begin); /* Remove unwanted header data from length */
261 if(len > length)
262 len = length; /* Remove unwanted tail data from length */
264 return (len);
267 /**************************************************************************\
269 * Routing tables for the AppleTalk socket layer. *
271 \**************************************************************************/
273 static struct atalk_route *atalk_router_list = NULL;
274 static struct atalk_iface *atalk_iface_list = NULL;
275 static struct atalk_route atrtr_default; /* For probing devices or in a routerless network */
278 * AppleTalk interface control
282 * Drop a device. Doesn't drop any of its routes - that is the caller's
283 * problem. Called when we down the interface or delete the address.
285 static void atif_drop_device(struct net_device *dev)
287 struct atalk_iface **iface = &atalk_iface_list;
288 struct atalk_iface *tmp;
290 while((tmp = *iface) != NULL)
292 if(tmp->dev == dev)
294 *iface = tmp->next;
295 kfree_s(tmp, sizeof(struct atalk_iface));
296 dev->atalk_ptr=NULL;
297 MOD_DEC_USE_COUNT;
299 else
300 iface = &tmp->next;
305 static struct atalk_iface *atif_add_device(struct net_device *dev, struct at_addr *sa)
307 struct atalk_iface *iface = (struct atalk_iface *)
308 kmalloc(sizeof(*iface), GFP_KERNEL);
309 unsigned long flags;
311 if(iface==NULL)
312 return (NULL);
314 iface->dev=dev;
315 dev->atalk_ptr=iface;
316 iface->address= *sa;
317 iface->status=0;
318 save_flags(flags);
319 cli();
320 iface->next=atalk_iface_list;
321 atalk_iface_list=iface;
322 restore_flags(flags);
324 MOD_INC_USE_COUNT;
326 return (iface);
331 * Perform phase 2 AARP probing on our tentative address.
333 static int atif_probe_device(struct atalk_iface *atif)
335 int netrange=ntohs(atif->nets.nr_lastnet)-ntohs(atif->nets.nr_firstnet)+1;
336 int probe_net=ntohs(atif->address.s_net);
337 int probe_node=atif->address.s_node;
338 int netct, nodect;
341 * Offset the network we start probing with.
344 if(probe_net == ATADDR_ANYNET)
346 if(!netrange)
347 probe_net = ntohs(atif->nets.nr_firstnet);
348 else
349 probe_net = ntohs(atif->nets.nr_firstnet) + (jiffies%netrange);
352 if(probe_node == ATADDR_ANYNODE)
353 probe_node = jiffies&0xFF;
356 * Scan the networks.
358 atif->status |= ATIF_PROBE;
359 for(netct = 0; netct <= netrange; netct++)
362 * Sweep the available nodes from a given start.
365 atif->address.s_net = htons(probe_net);
366 for(nodect = 0; nodect < 256; nodect++)
368 atif->address.s_node = ((nodect+probe_node) & 0xFF);
369 if(atif->address.s_node > 0 && atif->address.s_node<254)
372 * Probe a proposed address.
374 aarp_probe_network(atif);
376 if(!(atif->status & ATIF_PROBE_FAIL)) {
377 atif->status &= ~ATIF_PROBE;
378 return (0);
381 atif->status &= ~ATIF_PROBE_FAIL;
383 probe_net++;
384 if(probe_net > ntohs(atif->nets.nr_lastnet))
385 probe_net = ntohs(atif->nets.nr_firstnet);
387 atif->status &= ~ATIF_PROBE;
388 return (-EADDRINUSE); /* Network is full... */
393 * Perform AARP probing for a proxy address
395 static int atif_proxy_probe_device(struct atalk_iface *atif, struct at_addr* proxy_addr)
397 int netrange=ntohs(atif->nets.nr_lastnet)-ntohs(atif->nets.nr_firstnet)+1;
398 int probe_net=ntohs(atif->address.s_net); // we probe the interface's network
399 int probe_node=ATADDR_ANYNODE; // we'll take anything
400 int netct, nodect;
403 * Offset the network we start probing with.
406 if(probe_net == ATADDR_ANYNET)
408 if(!netrange)
409 probe_net = ntohs(atif->nets.nr_firstnet);
410 else
411 probe_net = ntohs(atif->nets.nr_firstnet) + (jiffies%netrange);
414 if(probe_node == ATADDR_ANYNODE)
415 probe_node = jiffies&0xFF;
418 * Scan the networks.
421 for(netct = 0; netct <= netrange; netct++)
424 * Sweep the available nodes from a given start.
427 proxy_addr->s_net = htons(probe_net);
428 for(nodect = 0; nodect < 256; nodect++)
430 proxy_addr->s_node = ((nodect+probe_node) & 0xFF);
431 if((proxy_addr->s_node>0) && (proxy_addr->s_node<254))
434 * Tell AARP to probe a proposed address.
436 int probe_result = aarp_proxy_probe_network(atif, proxy_addr);
438 if (probe_result == 0)
439 return 0;
441 if (probe_result != -EADDRINUSE)
442 return probe_result;
445 probe_net++;
446 if(probe_net > ntohs(atif->nets.nr_lastnet))
447 probe_net = ntohs(atif->nets.nr_firstnet);
450 return (-EADDRINUSE); /* Network is full... */
454 struct at_addr *atalk_find_dev_addr(struct net_device *dev)
456 struct atalk_iface *iface=dev->atalk_ptr;
458 if(iface)
459 return (&iface->address);
461 return (NULL);
464 static struct at_addr *atalk_find_primary(void)
466 struct atalk_iface *iface;
467 struct atalk_iface *fiface = NULL;
470 * Return a point-to-point interface only if
471 * there is no non-ptp interface available.
473 for(iface=atalk_iface_list; iface != NULL; iface=iface->next)
475 if(!fiface && !(iface->dev->flags & IFF_LOOPBACK))
476 fiface=iface;
477 if(!(iface->dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT)))
478 return (&iface->address);
481 if(fiface)
482 return (&fiface->address);
483 if(atalk_iface_list != NULL)
484 return (&atalk_iface_list->address);
485 else
486 return (NULL);
490 * Find a match for 'any network' - ie any of our interfaces with that
491 * node number will do just nicely.
493 static struct atalk_iface *atalk_find_anynet(int node, struct net_device *dev)
495 struct atalk_iface *iface=dev->atalk_ptr;
497 if(iface==NULL || (iface->status & ATIF_PROBE))
498 return (NULL);
499 if(node == ATADDR_BCAST || iface->address.s_node == node
500 || node == ATADDR_ANYNODE)
501 return (iface);
503 return (NULL);
507 * Find a match for a specific network:node pair
509 static struct atalk_iface *atalk_find_interface(int net, int node)
511 struct atalk_iface *iface;
513 for(iface=atalk_iface_list; iface != NULL; iface=iface->next)
515 if((node==ATADDR_BCAST || node==ATADDR_ANYNODE
516 || iface->address.s_node==node)
517 && iface->address.s_net==net
518 && !(iface->status & ATIF_PROBE))
519 return (iface);
521 /* XXXX.0 -- net.0 returns the iface associated with net */
522 if ((node==ATADDR_ANYNODE) && (net != ATADDR_ANYNET) &&
523 (ntohs(iface->nets.nr_firstnet) <= ntohs(net)) &&
524 (ntohs(net) <= ntohs(iface->nets.nr_lastnet)))
525 return (iface);
528 return (NULL);
533 * Find a route for an AppleTalk packet. This ought to get cached in
534 * the socket (later on...). We know about host routes and the fact
535 * that a route must be direct to broadcast.
537 static struct atalk_route *atrtr_find(struct at_addr *target)
540 * we must search through all routes unless we find a
541 * host route, because some host routes might overlap
542 * network routes
544 struct atalk_route *r;
545 struct atalk_route *net_route = NULL;
547 for(r=atalk_router_list; r != NULL; r=r->next)
549 if(!(r->flags & RTF_UP))
550 continue;
551 if(r->target.s_net == target->s_net)
553 if (r->flags & RTF_HOST)
556 * if this host route is for the target,
557 * the we're done
559 if (r->target.s_node == target->s_node)
560 return (r);
562 else
565 * this route will work if there isn't a
566 * direct host route, so cache it
568 net_route = r;
574 * if we found a network route but not a direct host
575 * route, then return it
577 if (net_route != NULL)
578 return (net_route);
580 if(atrtr_default.dev)
581 return (&atrtr_default);
583 return (NULL);
588 * Given an AppleTalk network, find the device to use. This can be
589 * a simple lookup.
591 struct net_device *atrtr_get_dev(struct at_addr *sa)
593 struct atalk_route *atr=atrtr_find(sa);
595 if(atr == NULL)
596 return (NULL);
597 else
598 return (atr->dev);
602 * Set up a default router.
604 static void atrtr_set_default(struct net_device *dev)
606 atrtr_default.dev = dev;
607 atrtr_default.flags = RTF_UP;
608 atrtr_default.gateway.s_net = htons(0);
609 atrtr_default.gateway.s_node = 0;
613 * Add a router. Basically make sure it looks valid and stuff the
614 * entry in the list. While it uses netranges we always set them to one
615 * entry to work like netatalk.
617 static int atrtr_create(struct rtentry *r, struct net_device *devhint)
619 struct sockaddr_at *ta=(struct sockaddr_at *)&r->rt_dst;
620 struct sockaddr_at *ga=(struct sockaddr_at *)&r->rt_gateway;
621 struct atalk_route *rt;
622 struct atalk_iface *iface, *riface;
623 unsigned long flags;
625 save_flags(flags);
628 * Fixme: Raise/Lower a routing change semaphore for these
629 * operations.
633 * Validate the request
635 if(ta->sat_family != AF_APPLETALK)
636 return (-EINVAL);
637 if(devhint == NULL && ga->sat_family != AF_APPLETALK)
638 return (-EINVAL);
641 * Now walk the routing table and make our decisions.
643 for(rt=atalk_router_list; rt!=NULL; rt=rt->next)
645 if(r->rt_flags != rt->flags)
646 continue;
648 if(ta->sat_addr.s_net == rt->target.s_net)
650 if(!(rt->flags & RTF_HOST))
651 break;
652 if(ta->sat_addr.s_node == rt->target.s_node)
653 break;
657 if(devhint == NULL)
659 for(riface = NULL, iface = atalk_iface_list; iface; iface = iface->next)
661 if(riface == NULL && ntohs(ga->sat_addr.s_net) >= ntohs(iface->nets.nr_firstnet) &&
662 ntohs(ga->sat_addr.s_net) <= ntohs(iface->nets.nr_lastnet))
664 riface = iface;
666 if(ga->sat_addr.s_net == iface->address.s_net
667 && ga->sat_addr.s_node == iface->address.s_node)
668 riface = iface;
671 if(riface == NULL)
672 return (-ENETUNREACH);
673 devhint = riface->dev;
676 if(rt == NULL)
678 rt = (struct atalk_route *)kmalloc(sizeof(struct atalk_route), GFP_KERNEL);
679 if(rt == NULL)
680 return (-ENOBUFS);
681 cli();
682 rt->next = atalk_router_list;
683 atalk_router_list = rt;
687 * Fill in the routing entry.
689 rt->target = ta->sat_addr;
690 rt->dev = devhint;
691 rt->flags = r->rt_flags;
692 rt->gateway = ga->sat_addr;
694 restore_flags(flags);
696 return (0);
700 * Delete a route. Find it and discard it.
702 static int atrtr_delete( struct at_addr *addr )
704 struct atalk_route **r = &atalk_router_list;
705 struct atalk_route *tmp;
707 while((tmp = *r) != NULL)
709 if(tmp->target.s_net == addr->s_net
710 && (!(tmp->flags&RTF_GATEWAY)
711 || tmp->target.s_node == addr->s_node))
713 *r = tmp->next;
714 kfree_s(tmp, sizeof(struct atalk_route));
715 return (0);
717 r = &tmp->next;
720 return (-ENOENT);
724 * Called when a device is downed. Just throw away any routes
725 * via it.
727 void atrtr_device_down(struct net_device *dev)
729 struct atalk_route **r = &atalk_router_list;
730 struct atalk_route *tmp;
732 while((tmp = *r) != NULL)
734 if(tmp->dev == dev)
736 *r = tmp->next;
737 kfree_s(tmp, sizeof(struct atalk_route));
739 else
740 r = &tmp->next;
743 if(atrtr_default.dev == dev)
744 atrtr_set_default(NULL);
748 * Actually down the interface.
750 static inline void atalk_dev_down(struct net_device *dev)
752 atrtr_device_down(dev); /* Remove all routes for the device */
753 aarp_device_down(dev); /* Remove AARP entries for the device */
754 atif_drop_device(dev); /* Remove the device */
758 * A device event has occurred. Watch for devices going down and
759 * delete our use of them (iface and route).
761 static int ddp_device_event(struct notifier_block *this, unsigned long event, void *ptr)
763 if(event == NETDEV_DOWN)
765 /* Discard any use of this */
766 atalk_dev_down((struct net_device *) ptr);
769 return (NOTIFY_DONE);
773 * ioctl calls. Shouldn't even need touching.
777 * Device configuration ioctl calls.
779 int atif_ioctl(int cmd, void *arg)
781 struct ifreq atreq;
782 static char aarp_mcast[6] = {0x09, 0x00, 0x00, 0xFF, 0xFF, 0xFF};
783 struct netrange *nr;
784 struct sockaddr_at *sa;
785 struct net_device *dev;
786 struct atalk_iface *atif;
787 int ct;
788 int limit;
789 struct rtentry rtdef;
790 int add_route;
792 if(copy_from_user(&atreq,arg,sizeof(atreq)))
793 return (-EFAULT);
795 if((dev = __dev_get_by_name(atreq.ifr_name)) == NULL)
796 return (-ENODEV);
798 sa=(struct sockaddr_at*)&atreq.ifr_addr;
799 atif=atalk_find_dev(dev);
801 switch(cmd)
803 case SIOCSIFADDR:
804 if(!capable(CAP_NET_ADMIN))
805 return (-EPERM);
806 if(sa->sat_family != AF_APPLETALK)
807 return (-EINVAL);
808 if(dev->type != ARPHRD_ETHER
809 && dev->type != ARPHRD_LOOPBACK
810 && dev->type != ARPHRD_LOCALTLK
811 && dev->type != ARPHRD_PPP)
812 return (-EPROTONOSUPPORT);
814 nr=(struct netrange *)&sa->sat_zero[0];
816 add_route = 1;
819 * if this is a point-to-point iface, and we already have an
820 * iface for this AppleTalk address, then we should not add a route
822 if (dev->flags & IFF_POINTOPOINT && atalk_find_interface(sa->sat_addr.s_net, sa->sat_addr.s_node))
824 printk(KERN_DEBUG "AppleTalk: point-to-point interface added with existing address\n");
825 add_route = 0;
829 * Phase 1 is fine on LocalTalk but we don't do
830 * EtherTalk phase 1. Anyone wanting to add it go ahead.
832 if(dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
833 return (-EPROTONOSUPPORT);
834 if(sa->sat_addr.s_node == ATADDR_BCAST
835 || sa->sat_addr.s_node == 254)
836 return (-EINVAL);
837 if(atif)
840 * Already setting address.
842 if(atif->status&ATIF_PROBE)
843 return (-EBUSY);
845 atif->address.s_net = sa->sat_addr.s_net;
846 atif->address.s_node = sa->sat_addr.s_node;
847 atrtr_device_down(dev); /* Flush old routes */
849 else
851 atif=atif_add_device(dev, &sa->sat_addr);
852 if (atif == NULL)
853 return (-ENOMEM);
855 atif->nets= *nr;
858 * Check if the chosen address is used. If so we
859 * error and atalkd will try another.
862 if(!(dev->flags & IFF_LOOPBACK) && !(dev->flags & IFF_POINTOPOINT) && atif_probe_device(atif) < 0)
864 atif_drop_device(dev);
865 return (-EADDRINUSE);
869 * Hey it worked - add the direct routes.
872 sa = (struct sockaddr_at *)&rtdef.rt_gateway;
873 sa->sat_family = AF_APPLETALK;
874 sa->sat_addr.s_net = atif->address.s_net;
875 sa->sat_addr.s_node = atif->address.s_node;
876 sa = (struct sockaddr_at *)&rtdef.rt_dst;
877 rtdef.rt_flags = RTF_UP;
878 sa->sat_family = AF_APPLETALK;
879 sa->sat_addr.s_node = ATADDR_ANYNODE;
880 if((dev->flags & IFF_LOOPBACK) || (dev->flags & IFF_POINTOPOINT))
881 rtdef.rt_flags |= RTF_HOST;
884 * Routerless initial state.
886 if(nr->nr_firstnet == htons(0)
887 && nr->nr_lastnet == htons(0xFFFE))
889 sa->sat_addr.s_net = atif->address.s_net;
890 atrtr_create(&rtdef, dev);
891 atrtr_set_default(dev);
893 else
895 limit = ntohs(nr->nr_lastnet);
896 if(limit - ntohs(nr->nr_firstnet) > 4096)
898 printk(KERN_WARNING "Too many routes/iface.\n");
899 return (-EINVAL);
901 if (add_route)
902 for(ct=ntohs(nr->nr_firstnet);ct<=limit;ct++)
904 sa->sat_addr.s_net = htons(ct);
905 atrtr_create(&rtdef, dev);
908 dev_mc_add(dev, aarp_mcast, 6, 1);
909 return (0);
911 case SIOCGIFADDR:
912 if(atif == NULL)
913 return (-EADDRNOTAVAIL);
914 ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_family=AF_APPLETALK;
915 ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_addr=atif->address;
916 break;
918 case SIOCGIFBRDADDR:
919 if(atif == NULL)
920 return (-EADDRNOTAVAIL);
921 ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_family=AF_APPLETALK;
922 ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_addr.s_net=atif->address.s_net;
923 ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_addr.s_node=ATADDR_BCAST;
924 break;
926 case SIOCATALKDIFADDR:
927 case SIOCDIFADDR:
928 if(!capable(CAP_NET_ADMIN))
929 return (-EPERM);
930 if(sa->sat_family != AF_APPLETALK)
931 return (-EINVAL);
932 atalk_dev_down(dev);
933 break;
935 case SIOCSARP:
936 if(!capable(CAP_NET_ADMIN))
937 return (-EPERM);
938 if(sa->sat_family != AF_APPLETALK)
939 return (-EINVAL);
940 if (atif == NULL)
941 return (-EADDRNOTAVAIL);
944 * for now, we only support proxy AARP on ELAP;
945 * we should be able to do it for LocalTalk, too.
947 if(dev->type != ARPHRD_ETHER)
948 return (-EPROTONOSUPPORT);
951 * atif points to the current interface on this network;
952 * we aren't concerned about its current status (at least for now),
953 * but it has all the settings about the network we're going
954 * to probe. consequently, it must exist.
956 if (!atif)
957 return (-EADDRNOTAVAIL);
959 nr=(struct netrange *)&(atif->nets);
961 * Phase 1 is fine on Localtalk but we don't do
962 * Ethertalk phase 1. Anyone wanting to add it go ahead.
964 if(dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
965 return (-EPROTONOSUPPORT);
967 if(sa->sat_addr.s_node == ATADDR_BCAST
968 || sa->sat_addr.s_node == 254)
969 return (-EINVAL);
972 * Check if the chosen address is used. If so we
973 * error and ATCP will try another.
975 if (atif_proxy_probe_device(atif, &(sa->sat_addr)) < 0)
976 return (-EADDRINUSE);
979 * We now have an address on the local network, and the AARP
980 * code will defend it for us until we take it down.
981 * We don't set up any routes right now, because ATCP will
982 * install them manually via SIOCADDRT.
984 break;
986 case SIOCDARP:
987 if(!capable(CAP_NET_ADMIN))
988 return (-EPERM);
989 if(sa->sat_family != AF_APPLETALK)
990 return (-EINVAL);
991 if (atif == NULL)
992 return (-EADDRNOTAVAIL);
995 * give to aarp module to remove proxy entry
997 aarp_proxy_remove(atif->dev, &(sa->sat_addr));
999 return (0);
1002 if(copy_to_user(arg, &atreq, sizeof(atreq)))
1003 return (-EFAULT);
1005 return (0);
1009 * Routing ioctl() calls
1011 static int atrtr_ioctl(unsigned int cmd, void *arg)
1013 struct rtentry rt;
1014 struct net_device *dev = NULL;
1016 if(copy_from_user(&rt, arg, sizeof(rt)))
1017 return (-EFAULT);
1019 switch(cmd)
1021 case SIOCDELRT:
1022 if(rt.rt_dst.sa_family != AF_APPLETALK)
1023 return (-EINVAL);
1024 return (atrtr_delete(&((struct sockaddr_at *)&rt.rt_dst)->sat_addr));
1026 case SIOCADDRT:
1027 /* FIX ME: the name of the device is still in user space, isn't it? */
1028 if (rt.rt_dev != NULL)
1029 if ((dev = __dev_get_by_name(rt.rt_dev)) == NULL)
1030 return -(ENODEV);
1032 return (atrtr_create(&rt, dev));
1034 default:
1035 return (-EINVAL);
1039 /* Called from proc fs - just make it print the ifaces neatly */
1041 int atalk_if_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
1043 struct atalk_iface *iface;
1044 int len=0;
1045 off_t pos=0;
1046 off_t begin=0;
1048 len += sprintf(buffer,"Interface Address Networks Status\n");
1049 for(iface = atalk_iface_list; iface != NULL; iface = iface->next)
1051 len += sprintf(buffer+len,"%-16s %04X:%02X %04X-%04X %d\n",
1052 iface->dev->name, ntohs(iface->address.s_net),
1053 iface->address.s_node, ntohs(iface->nets.nr_firstnet),
1054 ntohs(iface->nets.nr_lastnet), iface->status);
1055 pos = begin + len;
1056 if(pos < offset)
1058 len = 0;
1059 begin = pos;
1061 if(pos > offset + length)
1062 break;
1064 *start = buffer + (offset - begin);
1065 len -= (offset - begin);
1066 if(len > length)
1067 len = length;
1069 return (len);
1072 /* Called from proc fs - just make it print the routes neatly */
1074 int atalk_rt_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
1076 struct atalk_route *rt;
1077 int len=0;
1078 off_t pos=0;
1079 off_t begin=0;
1081 len += sprintf(buffer,"Target Router Flags Dev\n");
1082 if(atrtr_default.dev)
1084 rt = &atrtr_default;
1085 len += sprintf(buffer+len,"Default %04X:%02X %-4d %s\n",
1086 ntohs(rt->gateway.s_net), rt->gateway.s_node, rt->flags,
1087 rt->dev->name);
1090 for(rt = atalk_router_list; rt != NULL; rt = rt->next)
1092 len += sprintf(buffer+len,"%04X:%02X %04X:%02X %-4d %s\n",
1093 ntohs(rt->target.s_net),rt->target.s_node,
1094 ntohs(rt->gateway.s_net), rt->gateway.s_node, rt->flags,
1095 rt->dev->name);
1096 pos = begin + len;
1097 if(pos < offset)
1099 len = 0;
1100 begin = pos;
1102 if(pos > offset + length)
1103 break;
1106 *start = buffer + (offset - begin);
1107 len -= (offset - begin);
1108 if(len > length)
1109 len = length;
1111 return (len);
1114 /**************************************************************************\
1116 * Handling for system calls applied via the various interfaces to an *
1117 * AppleTalk socket object. *
1119 \**************************************************************************/
1122 * Checksum: This is 'optional'. It's quite likely also a good
1123 * candidate for assembler hackery 8)
1125 unsigned short atalk_checksum(struct ddpehdr *ddp, int len)
1127 unsigned long sum=0; /* Assume unsigned long is >16 bits */
1128 unsigned char *data=(unsigned char *)ddp;
1130 len -= 4; /* skip header 4 bytes */
1131 data += 4;
1133 /* This ought to be unwrapped neatly. I'll trust gcc for now */
1134 while(len--)
1136 sum += *data;
1137 sum <<= 1;
1138 if(sum & 0x10000)
1140 sum++;
1141 sum &= 0xFFFF;
1143 data++;
1146 if(sum)
1147 return htons((unsigned short)sum);
1149 return 0xFFFF; /* Use 0xFFFF for 0. 0 itself means none */
1153 * Create a socket. Initialise the socket, blank the addresses
1154 * set the state.
1156 static int atalk_create(struct socket *sock, int protocol)
1158 struct sock *sk;
1160 sk = sk_alloc(PF_APPLETALK, GFP_KERNEL, 1);
1161 if(sk == NULL)
1162 return (-ENOMEM);
1164 switch(sock->type)
1167 * We permit SOCK_DGRAM and RAW is an extension. It is
1168 * trivial to do and gives you the full ELAP frame.
1169 * Should be handy for CAP 8)
1171 case SOCK_RAW:
1172 case SOCK_DGRAM:
1173 sock->ops = &atalk_dgram_ops;
1174 break;
1176 case SOCK_STREAM:
1178 * TO DO: if you want to implement ADSP, here's the place to start
1181 sock->ops = &atalk_stream_ops;
1182 break;
1184 default:
1185 sk_free((void *)sk);
1186 return (-ESOCKTNOSUPPORT);
1189 MOD_INC_USE_COUNT;
1191 sock_init_data(sock,sk);
1193 sk->destruct = NULL;
1194 /* Checksums on by default */
1195 sk->zapped = 1;
1197 return (0);
1201 * Free a socket. No work needed
1203 static int atalk_release(struct socket *sock)
1205 struct sock *sk=sock->sk;
1207 if(sk == NULL)
1208 return (0);
1210 if(!sk->dead)
1211 sk->state_change(sk);
1213 sk->dead = 1;
1214 sock->sk = NULL;
1215 atalk_destroy_socket(sk);
1217 return (0);
1221 * Pick a source address if one is not given. Just return
1222 * an error if not supportable.
1224 static int atalk_pick_port(struct sockaddr_at *sat)
1226 for(sat->sat_port = ATPORT_RESERVED; sat->sat_port < ATPORT_LAST; sat->sat_port++)
1228 if(atalk_find_socket(sat) == NULL)
1229 return sat->sat_port;
1232 return (-EBUSY);
1235 static int atalk_autobind(struct sock *sk)
1237 struct at_addr *ap = atalk_find_primary();
1238 struct sockaddr_at sat;
1239 int n;
1241 if(ap == NULL || ap->s_net == htons(ATADDR_ANYNET))
1242 return (-EADDRNOTAVAIL);
1244 sk->protinfo.af_at.src_net = sat.sat_addr.s_net = ap->s_net;
1245 sk->protinfo.af_at.src_node = sat.sat_addr.s_node = ap->s_node;
1247 if((n = atalk_pick_port(&sat)) < 0)
1248 return (n);
1250 sk->protinfo.af_at.src_port = n;
1251 atalk_insert_socket(sk);
1252 sk->zapped = 0;
1254 return (0);
1258 * Set the address 'our end' of the connection.
1260 static int atalk_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1262 struct sock *sk;
1263 struct sockaddr_at *addr=(struct sockaddr_at *)uaddr;
1265 sk = sock->sk;
1267 if(sk->zapped == 0)
1268 return (-EINVAL);
1270 if(addr_len != sizeof(struct sockaddr_at))
1271 return (-EINVAL);
1273 if(addr->sat_family != AF_APPLETALK)
1274 return (-EAFNOSUPPORT);
1276 if(addr->sat_addr.s_net == htons(ATADDR_ANYNET))
1278 struct at_addr *ap=atalk_find_primary();
1280 if(ap == NULL)
1281 return (-EADDRNOTAVAIL);
1283 sk->protinfo.af_at.src_net = addr->sat_addr.s_net = ap->s_net;
1284 sk->protinfo.af_at.src_node = addr->sat_addr.s_node= ap->s_node;
1286 else
1288 if(atalk_find_interface(addr->sat_addr.s_net, addr->sat_addr.s_node) == NULL)
1289 return (-EADDRNOTAVAIL);
1291 sk->protinfo.af_at.src_net = addr->sat_addr.s_net;
1292 sk->protinfo.af_at.src_node = addr->sat_addr.s_node;
1295 if(addr->sat_port == ATADDR_ANYPORT)
1297 int n = atalk_pick_port(addr);
1298 if(n < 0)
1299 return (n);
1301 sk->protinfo.af_at.src_port = addr->sat_port = n;
1303 else
1304 sk->protinfo.af_at.src_port = addr->sat_port;
1306 if(atalk_find_socket(addr) != NULL)
1307 return (-EADDRINUSE);
1309 atalk_insert_socket(sk);
1310 sk->zapped = 0;
1312 return (0);
1316 * Set the address we talk to.
1318 static int atalk_connect(struct socket *sock, struct sockaddr *uaddr,
1319 int addr_len, int flags)
1321 struct sock *sk=sock->sk;
1322 struct sockaddr_at *addr;
1324 sk->state = TCP_CLOSE;
1325 sock->state = SS_UNCONNECTED;
1327 if(addr_len != sizeof(*addr))
1328 return (-EINVAL);
1329 addr = (struct sockaddr_at *)uaddr;
1331 if(addr->sat_family != AF_APPLETALK)
1332 return (-EAFNOSUPPORT);
1334 if(addr->sat_addr.s_node == ATADDR_BCAST && !sk->broadcast)
1336 #if 1
1337 printk(KERN_WARNING "%s is broken and did not set SO_BROADCAST. It will break when 2.2 is released.\n",
1338 current->comm);
1339 #else
1340 return (-EACCES);
1341 #endif
1344 if(sk->zapped)
1346 if(atalk_autobind(sk) < 0)
1347 return (-EBUSY);
1350 if(atrtr_get_dev(&addr->sat_addr) == NULL)
1351 return (-ENETUNREACH);
1353 sk->protinfo.af_at.dest_port = addr->sat_port;
1354 sk->protinfo.af_at.dest_net = addr->sat_addr.s_net;
1355 sk->protinfo.af_at.dest_node = addr->sat_addr.s_node;
1357 sock->state = SS_CONNECTED;
1358 sk->state = TCP_ESTABLISHED;
1360 return (0);
1365 * Find the name of an AppleTalk socket. Just copy the right
1366 * fields into the sockaddr.
1368 static int atalk_getname(struct socket *sock, struct sockaddr *uaddr,
1369 int *uaddr_len, int peer)
1371 struct sockaddr_at sat;
1372 struct sock *sk;
1374 sk = sock->sk;
1375 if(sk->zapped)
1377 if(atalk_autobind(sk) < 0)
1378 return (-ENOBUFS);
1381 *uaddr_len = sizeof(struct sockaddr_at);
1383 if(peer)
1385 if(sk->state != TCP_ESTABLISHED)
1386 return (-ENOTCONN);
1387 sat.sat_addr.s_net = sk->protinfo.af_at.dest_net;
1388 sat.sat_addr.s_node = sk->protinfo.af_at.dest_node;
1389 sat.sat_port = sk->protinfo.af_at.dest_port;
1391 else
1393 sat.sat_addr.s_net = sk->protinfo.af_at.src_net;
1394 sat.sat_addr.s_node = sk->protinfo.af_at.src_node;
1395 sat.sat_port = sk->protinfo.af_at.src_port;
1398 sat.sat_family = AF_APPLETALK;
1399 memcpy(uaddr, &sat, sizeof(sat));
1401 return (0);
1405 * Receive a packet (in skb) from device dev. This has come from the SNAP
1406 * decoder, and on entry skb->h.raw is the DDP header, skb->len is the DDP
1407 * header, skb->len is the DDP length. The physical headers have been
1408 * extracted. PPP should probably pass frames marked as for this layer.
1409 * [ie ARPHRD_ETHERTALK]
1411 static int atalk_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
1413 struct sock *sock;
1414 struct ddpehdr *ddp=(void *)skb->h.raw;
1415 struct atalk_iface *atif;
1416 struct sockaddr_at tosat;
1417 int origlen;
1418 struct ddpebits ddphv;
1420 /* Size check */
1421 if(skb->len < sizeof(*ddp))
1423 kfree_skb(skb);
1424 return (0);
1428 * Fix up the length field [Ok this is horrible but otherwise
1429 * I end up with unions of bit fields and messy bit field order
1430 * compiler/endian dependencies..]
1432 * FIXME: This is a write to a shared object. Granted it
1433 * happens to be safe BUT.. (Its safe as user space will not
1434 * run until we put it back)
1437 *((__u16 *)&ddphv) = ntohs(*((__u16 *)ddp));
1440 * Trim buffer in case of stray trailing data
1443 origlen = skb->len;
1445 skb_trim(skb, min(skb->len, ddphv.deh_len));
1448 * Size check to see if ddp->deh_len was crap
1449 * (Otherwise we'll detonate most spectacularly
1450 * in the middle of recvmsg()).
1452 if(skb->len < sizeof(*ddp))
1454 kfree_skb(skb);
1455 return (0);
1459 * Any checksums. Note we don't do htons() on this == is assumed to be
1460 * valid for net byte orders all over the networking code...
1462 if(ddp->deh_sum && atalk_checksum(ddp, ddphv.deh_len) != ddp->deh_sum)
1464 /* Not a valid AppleTalk frame - dustbin time */
1465 kfree_skb(skb);
1466 return (0);
1469 /* Check the packet is aimed at us */
1471 if(ddp->deh_dnet == 0) /* Net 0 is 'this network' */
1472 atif = atalk_find_anynet(ddp->deh_dnode, dev);
1473 else
1474 atif = atalk_find_interface(ddp->deh_dnet, ddp->deh_dnode);
1477 * Not ours, so we route the packet via the correct AppleTalk interface.
1479 if(atif == NULL)
1481 struct atalk_route *rt;
1482 struct at_addr ta;
1485 * Don't route multicast, etc., packets, or packets
1486 * sent to "this network"
1488 if (skb->pkt_type != PACKET_HOST || ddp->deh_dnet == 0)
1491 * FIX ME:
1492 * Can it ever happen that a packet is from a PPP iface and needs to be broadcast onto the default network?
1494 if (dev->type == ARPHRD_PPP)
1495 printk(KERN_DEBUG "AppleTalk: didn't forward broadcast packet received from PPP iface\n");
1497 kfree_skb(skb);
1498 return (0);
1501 ta.s_net = ddp->deh_dnet;
1502 ta.s_node = ddp->deh_dnode;
1504 /* Route the packet */
1505 rt = atrtr_find(&ta);
1506 if(rt == NULL || ddphv.deh_hops == DDP_MAXHOPS)
1508 kfree_skb(skb);
1509 return (0);
1511 ddphv.deh_hops++;
1514 * Route goes through another gateway, so
1515 * set the target to the gateway instead.
1517 if(rt->flags & RTF_GATEWAY)
1519 ta.s_net = rt->gateway.s_net;
1520 ta.s_node = rt->gateway.s_node;
1523 /* Fix up skb->len field */
1524 skb_trim(skb, min(origlen, rt->dev->hard_header_len +
1525 ddp_dl->header_length + ddphv.deh_len));
1527 /* Mend the byte order */
1528 *((__u16 *)ddp) = ntohs(*((__u16 *)&ddphv));
1531 * Send the buffer onwards
1533 * Now we must always be careful. If it's come from
1534 * LocalTalk to EtherTalk it might not fit
1536 * Order matters here: If a packet has to be copied
1537 * to make a new headroom (rare hopefully) then it
1538 * won't need unsharing.
1540 * Note. ddp-> becomes invalid at the realloc.
1542 if(skb_headroom(skb) < 22)
1543 /* 22 bytes - 12 ether, 2 len, 3 802.2 5 snap */
1544 skb = skb_realloc_headroom(skb, 32);
1545 else
1546 skb = skb_unshare(skb, GFP_ATOMIC);
1549 * If the buffer didn't vanish into the lack of
1550 * space bitbucket we can send it.
1552 if(skb)
1554 if(aarp_send_ddp(rt->dev, skb, &ta, NULL) == -1)
1555 kfree_skb(skb);
1558 return (0);
1561 #if defined(CONFIG_IPDDP) || defined(CONFIG_IPDDP_MODULE)
1563 * Check if IP-over-DDP
1565 if(skb->data[12] == 22)
1567 struct net_device *dev;
1569 /* This needs to be able to handle ipddp"N" devices */
1570 if((dev = __dev_get_by_name("ipddp0")) == NULL)
1571 return (-ENODEV);
1573 skb->protocol = htons(ETH_P_IP);
1574 skb_pull(skb, 13);
1575 skb->dev = dev;
1576 skb->h.raw = skb->data;
1578 ((struct net_device_stats *)dev->priv)->rx_packets++;
1579 ((struct net_device_stats *)dev->priv)->rx_bytes += skb->len+13;
1580 netif_rx(skb); /* Send the SKB up to a higher place. */
1582 return (0);
1584 #endif
1587 * Which socket - atalk_search_socket() looks for a *full match*
1588 * of the <net,node,port> tuple.
1590 tosat.sat_addr.s_net = ddp->deh_dnet;
1591 tosat.sat_addr.s_node = ddp->deh_dnode;
1592 tosat.sat_port = ddp->deh_dport;
1594 sock = atalk_search_socket(&tosat, atif);
1596 if(sock == NULL) /* But not one of our sockets */
1598 kfree_skb(skb);
1599 return (0);
1603 * Queue packet (standard)
1606 skb->sk = sock;
1608 if(sock_queue_rcv_skb(sock, skb) < 0)
1609 kfree_skb(skb);
1611 return (0);
1615 * Receive a LocalTalk frame. We make some demands on the caller here.
1616 * Caller must provide enough headroom on the packet to pull the short
1617 * header and append a long one.
1619 static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
1621 struct ddpehdr *ddp;
1622 struct at_addr *ap;
1625 * Expand any short form frames.
1627 if(skb->mac.raw[2] == 1)
1630 * Find our address.
1633 ap = atalk_find_dev_addr(dev);
1634 if(ap == NULL || skb->len < sizeof(struct ddpshdr))
1636 kfree_skb(skb);
1637 return (0);
1641 * The push leaves us with a ddephdr not an shdr, and
1642 * handily the port bytes in the right place preset.
1645 skb_push(skb, sizeof(*ddp) - 4);
1646 ddp = (struct ddpehdr *)skb->data;
1649 * Now fill in the long header.
1653 * These two first. The mac overlays the new source/dest
1654 * network information so we MUST copy these before
1655 * we write the network numbers !
1658 ddp->deh_dnode = skb->mac.raw[0]; /* From physical header */
1659 ddp->deh_snode = skb->mac.raw[1]; /* From physical header */
1661 ddp->deh_dnet = ap->s_net; /* Network number */
1662 ddp->deh_snet = ap->s_net;
1663 ddp->deh_sum = 0; /* No checksum */
1665 * Not sure about this bit...
1667 ddp->deh_len = skb->len;
1668 ddp->deh_hops = DDP_MAXHOPS; /* Non routable, so force a drop
1669 if we slip up later */
1671 /* Mend the byte order */
1672 *((__u16 *)ddp) = htons(*((__u16 *)ddp));
1674 skb->h.raw = skb->data;
1676 return (atalk_rcv(skb, dev, pt));
1679 static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, int len,
1680 struct scm_cookie *scm)
1682 struct sock *sk=sock->sk;
1683 struct sockaddr_at *usat=(struct sockaddr_at *)msg->msg_name;
1684 struct sockaddr_at local_satalk, gsat;
1685 struct sk_buff *skb;
1686 struct net_device *dev;
1687 struct ddpehdr *ddp;
1688 int size;
1689 struct atalk_route *rt;
1690 int loopback=0;
1691 int err;
1692 int flags = msg->msg_flags;
1694 if(flags & ~MSG_DONTWAIT)
1695 return (-EINVAL);
1697 if(len > DDP_MAXSZ)
1698 return (-EMSGSIZE);
1700 if(usat)
1702 if(sk->zapped)
1704 if(atalk_autobind(sk) < 0)
1705 return (-EBUSY);
1708 if(msg->msg_namelen < sizeof(*usat))
1709 return (-EINVAL);
1710 if(usat->sat_family != AF_APPLETALK)
1711 return (-EINVAL);
1713 /* netatalk doesn't implement this check */
1714 if(usat->sat_addr.s_node == ATADDR_BCAST && !sk->broadcast)
1716 printk(KERN_INFO "SO_BROADCAST: Fix your netatalk as it will break before 2.2\n");
1717 #if 0
1718 return (-EPERM);
1719 #endif
1722 else
1724 if(sk->state != TCP_ESTABLISHED)
1725 return (-ENOTCONN);
1726 usat =& local_satalk;
1727 usat->sat_family = AF_APPLETALK;
1728 usat->sat_port = sk->protinfo.af_at.dest_port;
1729 usat->sat_addr.s_node = sk->protinfo.af_at.dest_node;
1730 usat->sat_addr.s_net = sk->protinfo.af_at.dest_net;
1733 /* Build a packet */
1735 SOCK_DEBUG(sk, "SK %p: Got address.\n",sk);
1737 /* For headers */
1738 size = sizeof(struct ddpehdr) + len + ddp_dl->header_length;
1740 if(usat->sat_addr.s_net != 0 || usat->sat_addr.s_node == ATADDR_ANYNODE)
1742 rt = atrtr_find(&usat->sat_addr);
1743 if(rt == NULL)
1744 return (-ENETUNREACH);
1745 dev = rt->dev;
1747 else
1749 struct at_addr at_hint;
1750 at_hint.s_node = 0;
1751 at_hint.s_net = sk->protinfo.af_at.src_net;
1752 rt = atrtr_find(&at_hint);
1753 if(rt == NULL)
1754 return (-ENETUNREACH);
1755 dev = rt->dev;
1758 SOCK_DEBUG(sk, "SK %p: Size needed %d, device %s\n", sk, size, dev->name);
1760 size += dev->hard_header_len;
1762 skb = sock_alloc_send_skb(sk, size, 0, flags&MSG_DONTWAIT, &err);
1763 if(skb == NULL)
1764 return (err);
1766 skb->sk = sk;
1767 skb_reserve(skb, ddp_dl->header_length);
1768 skb_reserve(skb, dev->hard_header_len);
1770 skb->dev = dev;
1772 SOCK_DEBUG(sk, "SK %p: Begin build.\n", sk);
1774 ddp = (struct ddpehdr *)skb_put(skb,sizeof(struct ddpehdr));
1775 ddp->deh_pad = 0;
1776 ddp->deh_hops = 0;
1777 ddp->deh_len = len + sizeof(*ddp);
1779 * Fix up the length field [Ok this is horrible but otherwise
1780 * I end up with unions of bit fields and messy bit field order
1781 * compiler/endian dependencies..
1783 *((__u16 *)ddp) = ntohs(*((__u16 *)ddp));
1785 ddp->deh_dnet = usat->sat_addr.s_net;
1786 ddp->deh_snet = sk->protinfo.af_at.src_net;
1787 ddp->deh_dnode = usat->sat_addr.s_node;
1788 ddp->deh_snode = sk->protinfo.af_at.src_node;
1789 ddp->deh_dport = usat->sat_port;
1790 ddp->deh_sport = sk->protinfo.af_at.src_port;
1792 SOCK_DEBUG(sk, "SK %p: Copy user data (%d bytes).\n", sk, len);
1794 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
1795 if(err)
1797 kfree_skb(skb);
1798 return (-EFAULT);
1801 if(sk->no_check == 1)
1802 ddp->deh_sum = 0;
1803 else
1804 ddp->deh_sum = atalk_checksum(ddp, len + sizeof(*ddp));
1807 * Loopback broadcast packets to non gateway targets (ie routes
1808 * to group we are in)
1810 if(ddp->deh_dnode == ATADDR_BCAST)
1812 if((!(rt->flags&RTF_GATEWAY)) && (!(dev->flags&IFF_LOOPBACK)))
1814 struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL);
1815 if(skb2)
1817 loopback = 1;
1818 SOCK_DEBUG(sk, "SK %p: send out(copy).\n", sk);
1819 if(aarp_send_ddp(dev, skb2, &usat->sat_addr, NULL) == -1)
1820 kfree_skb(skb2);
1821 /* else queued/sent above in the aarp queue */
1826 if((dev->flags & IFF_LOOPBACK) || loopback)
1828 SOCK_DEBUG(sk, "SK %p: Loop back.\n", sk);
1829 /* loop back */
1830 skb_orphan(skb);
1831 ddp_dl->datalink_header(ddp_dl, skb, dev->dev_addr);
1832 skb->mac.raw = skb->data;
1833 skb->h.raw = skb->data + ddp_dl->header_length + dev->hard_header_len;
1834 skb_pull(skb,dev->hard_header_len);
1835 skb_pull(skb,ddp_dl->header_length);
1836 atalk_rcv(skb, dev, NULL);
1838 else
1840 SOCK_DEBUG(sk, "SK %p: send out.\n", sk);
1841 if (rt->flags & RTF_GATEWAY)
1843 gsat.sat_addr = rt->gateway;
1844 usat = &gsat;
1847 if(aarp_send_ddp(dev, skb, &usat->sat_addr, NULL) == -1)
1848 kfree_skb(skb);
1849 /* else queued/sent above in the aarp queue */
1851 SOCK_DEBUG(sk, "SK %p: Done write (%d).\n", sk, len);
1853 return (len);
1856 static int atalk_recvmsg(struct socket *sock, struct msghdr *msg, int size,
1857 int flags, struct scm_cookie *scm)
1859 struct sock *sk=sock->sk;
1860 struct sockaddr_at *sat=(struct sockaddr_at *)msg->msg_name;
1861 struct ddpehdr *ddp = NULL;
1862 struct ddpebits ddphv;
1863 int copied = 0;
1864 struct sk_buff *skb;
1865 int err = 0;
1868 skb = skb_recv_datagram(sk,flags&~MSG_DONTWAIT,flags&MSG_DONTWAIT,&err);
1869 if(skb == NULL)
1870 return (err);
1872 ddp = (struct ddpehdr *)(skb->h.raw);
1873 *((__u16 *)&ddphv) = ntohs(*((__u16 *)ddp));
1875 if(sk->type == SOCK_RAW)
1877 copied = ddphv.deh_len;
1878 if(copied > size)
1880 copied = size;
1881 msg->msg_flags |= MSG_TRUNC;
1884 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1886 else
1888 copied = ddphv.deh_len - sizeof(*ddp);
1889 if(copied > size)
1891 copied = size;
1892 msg->msg_flags |= MSG_TRUNC;
1894 err = skb_copy_datagram_iovec(skb,sizeof(*ddp),msg->msg_iov,copied);
1897 if(!err)
1899 if(sat)
1901 sat->sat_family = AF_APPLETALK;
1902 sat->sat_port = ddp->deh_sport;
1903 sat->sat_addr.s_node = ddp->deh_snode;
1904 sat->sat_addr.s_net = ddp->deh_snet;
1906 msg->msg_namelen = sizeof(*sat);
1909 skb_free_datagram(sk, skb); /* Free the datagram. */
1911 return (err ? err : (copied));
1916 * AppleTalk ioctl calls.
1918 static int atalk_ioctl(struct socket *sock,unsigned int cmd, unsigned long arg)
1920 long amount=0;
1921 struct sock *sk=sock->sk;
1923 switch(cmd)
1926 * Protocol layer
1928 case TIOCOUTQ:
1929 amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1930 if(amount < 0)
1931 amount = 0;
1932 break;
1934 case TIOCINQ:
1936 struct sk_buff *skb;
1937 /* These two are safe on a single CPU system as only user tasks fiddle here */
1938 if((skb = skb_peek(&sk->receive_queue)) != NULL)
1939 amount = skb->len-sizeof(struct ddpehdr);
1940 break;
1943 case SIOCGSTAMP:
1944 if(sk)
1946 if(sk->stamp.tv_sec == 0)
1947 return -ENOENT;
1948 return (copy_to_user((void *)arg,&sk->stamp,sizeof(struct timeval)) ? -EFAULT : 0);
1950 return (-EINVAL);
1953 * Routing
1955 case SIOCADDRT:
1956 case SIOCDELRT:
1957 if(!capable(CAP_NET_ADMIN))
1958 return -EPERM;
1959 return (atrtr_ioctl(cmd,(void *)arg));
1962 * Interface
1964 case SIOCGIFADDR:
1965 case SIOCSIFADDR:
1966 case SIOCGIFBRDADDR:
1967 case SIOCATALKDIFADDR:
1968 case SIOCDIFADDR:
1969 case SIOCSARP: /* proxy AARP */
1970 case SIOCDARP: /* proxy AARP */
1971 return (atif_ioctl(cmd,(void *)arg));
1974 * Physical layer ioctl calls
1976 case SIOCSIFLINK:
1977 case SIOCGIFHWADDR:
1978 case SIOCSIFHWADDR:
1979 case SIOCGIFFLAGS:
1980 case SIOCSIFFLAGS:
1981 case SIOCGIFMTU:
1982 case SIOCGIFCONF:
1983 case SIOCADDMULTI:
1984 case SIOCDELMULTI:
1985 case SIOCGIFCOUNT:
1986 case SIOCGIFINDEX:
1987 case SIOCGIFNAME:
1988 return ((dev_ioctl(cmd,(void *) arg)));
1990 case SIOCSIFMETRIC:
1991 case SIOCSIFBRDADDR:
1992 case SIOCGIFNETMASK:
1993 case SIOCSIFNETMASK:
1994 case SIOCGIFMEM:
1995 case SIOCSIFMEM:
1996 case SIOCGIFDSTADDR:
1997 case SIOCSIFDSTADDR:
1998 return (-EINVAL);
2000 default:
2001 return (-EINVAL);
2004 return (put_user(amount, (int *)arg));
2007 static struct net_proto_family atalk_family_ops=
2009 PF_APPLETALK,
2010 atalk_create
2013 static struct proto_ops SOCKOPS_WRAPPED(atalk_dgram_ops)=
2015 PF_APPLETALK,
2017 atalk_release,
2018 atalk_bind,
2019 atalk_connect,
2020 sock_no_socketpair,
2021 sock_no_accept,
2022 atalk_getname,
2023 datagram_poll,
2024 atalk_ioctl,
2025 sock_no_listen,
2026 sock_no_shutdown,
2027 sock_no_setsockopt,
2028 sock_no_getsockopt,
2029 sock_no_fcntl,
2030 atalk_sendmsg,
2031 atalk_recvmsg,
2032 sock_no_mmap
2035 #include <linux/smp_lock.h>
2036 SOCKOPS_WRAP(atalk_dgram, PF_APPLETALK);
2038 static struct notifier_block ddp_notifier=
2040 ddp_device_event,
2041 NULL,
2045 struct packet_type ltalk_packet_type=
2048 NULL,
2049 ltalk_rcv,
2050 NULL,
2051 NULL
2054 struct packet_type ppptalk_packet_type=
2057 NULL,
2058 atalk_rcv,
2059 NULL,
2060 NULL
2063 static char ddp_snap_id[] = {0x08, 0x00, 0x07, 0x80, 0x9B};
2066 * Export symbols for use by drivers when AppleTalk is a module.
2068 EXPORT_SYMBOL(aarp_send_ddp);
2069 EXPORT_SYMBOL(atrtr_get_dev);
2070 EXPORT_SYMBOL(atalk_find_dev_addr);
2072 #ifdef CONFIG_PROC_FS
2073 static struct proc_dir_entry proc_appletalk=
2075 PROC_NET_ATALK, 9, "appletalk",
2076 S_IFREG | S_IRUGO, 1, 0, 0,
2077 0, &proc_net_inode_operations,
2078 atalk_get_info
2081 static struct proc_dir_entry proc_atalk_route=
2083 PROC_NET_AT_ROUTE, 11,"atalk_route",
2084 S_IFREG | S_IRUGO, 1, 0, 0,
2085 0, &proc_net_inode_operations,
2086 atalk_rt_get_info
2089 static struct proc_dir_entry proc_atalk_iface=
2091 PROC_NET_ATIF, 11,"atalk_iface",
2092 S_IFREG | S_IRUGO, 1, 0, 0,
2093 0, &proc_net_inode_operations,
2094 atalk_if_get_info
2096 #endif /* CONFIG_PROC_FS */
2098 /* Called by proto.c on kernel start up */
2100 void __init atalk_proto_init(struct net_proto *pro)
2102 (void) sock_register(&atalk_family_ops);
2103 if((ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv)) == NULL)
2104 printk(KERN_CRIT "Unable to register DDP with SNAP.\n");
2106 ltalk_packet_type.type = htons(ETH_P_LOCALTALK);
2107 dev_add_pack(&ltalk_packet_type);
2109 ppptalk_packet_type.type = htons(ETH_P_PPPTALK);
2110 dev_add_pack(&ppptalk_packet_type);
2112 register_netdevice_notifier(&ddp_notifier);
2113 aarp_proto_init();
2115 #ifdef CONFIG_PROC_FS
2116 proc_net_register(&proc_appletalk);
2117 proc_net_register(&proc_atalk_route);
2118 proc_net_register(&proc_atalk_iface);
2120 aarp_register_proc_fs();
2121 #endif /* CONFIG_PROC_FS */
2123 #ifdef CONFIG_SYSCTL
2124 atalk_register_sysctl();
2125 #endif /* CONFIG_SYSCTL */
2127 printk(KERN_INFO "NET4: AppleTalk 0.18 for Linux NET4.0\n");
2130 #ifdef MODULE
2132 int init_module(void)
2134 atalk_proto_init(NULL);
2135 return (0);
2139 * Note on MOD_{INC,DEC}_USE_COUNT:
2141 * Use counts are incremented/decremented when
2142 * sockets are created/deleted.
2144 * AppleTalk interfaces are not incremented untill atalkd is run
2145 * and are only decremented when they are downed.
2147 * Ergo, before the AppleTalk module can be removed, all AppleTalk
2148 * sockets be closed from user space.
2151 void cleanup_module(void)
2153 #ifdef CONFIG_SYSCTL
2154 atalk_unregister_sysctl();
2155 #endif /* CONFIG_SYSCTL */
2157 #ifdef CONFIG_PROC_FS
2158 proc_net_unregister(PROC_NET_ATALK);
2159 proc_net_unregister(PROC_NET_AT_ROUTE);
2160 proc_net_unregister(PROC_NET_ATIF);
2162 aarp_unregister_proc_fs();
2163 #endif /* CONFIG_PROC_FS */
2165 aarp_cleanup_module(); /* General aarp clean-up. */
2167 unregister_netdevice_notifier(&ddp_notifier);
2168 dev_remove_pack(&ltalk_packet_type);
2169 dev_remove_pack(&ppptalk_packet_type);
2170 unregister_snap_client(ddp_snap_id);
2171 sock_unregister(PF_APPLETALK);
2173 return;
2176 #endif /* MODULE */
2177 #endif /* CONFIG_ATALK || CONFIG_ATALK_MODULE */