Import 2.2.5pre2
[davej-history.git] / net / appletalk / ddp.c
blob4c8c6e390cbface848b7a318fb2e6f172ff4fe6f
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/firewall.h>
85 #include <linux/init.h>
88 #ifdef CONFIG_PROC_FS
89 extern void aarp_register_proc_fs(void);
90 extern void aarp_unregister_proc_fs(void);
91 #endif
93 extern void aarp_probe_network(struct atalk_iface *atif);
94 extern int aarp_proxy_probe_network(struct atalk_iface *atif, struct at_addr *sa);
95 extern void aarp_proxy_remove(struct device *dev, struct at_addr *sa);
98 #undef APPLETALK_DEBUG
100 #ifdef APPLETALK_DEBUG
101 #define DPRINT(x) print(x)
102 #else
103 #define DPRINT(x)
104 #endif /* APPLETALK_DEBUG */
106 #ifdef CONFIG_SYSCTL
107 extern inline void atalk_register_sysctl(void);
108 extern inline void atalk_unregister_sysctl(void);
109 #endif /* CONFIG_SYSCTL */
111 struct datalink_proto *ddp_dl, *aarp_dl;
112 static struct proto_ops atalk_dgram_ops;
114 #define min(a,b) (((a)<(b))?(a):(b))
116 /**************************************************************************\
118 * Handlers for the socket list. *
120 \**************************************************************************/
122 static struct sock *atalk_socket_list = NULL;
125 * Note: Sockets may not be removed _during_ an interrupt or inet_bh
126 * handler using this technique. They can be added although we do not
127 * use this facility.
130 extern inline void atalk_remove_socket(struct sock *sk)
132 sklist_remove_socket(&atalk_socket_list,sk);
135 extern inline void atalk_insert_socket(struct sock *sk)
137 sklist_insert_socket(&atalk_socket_list,sk);
140 static struct sock *atalk_search_socket(struct sockaddr_at *to, struct atalk_iface *atif)
142 struct sock *s;
144 for(s = atalk_socket_list; s != NULL; s = s->next)
146 if(to->sat_port != s->protinfo.af_at.src_port)
148 continue;
151 if(to->sat_addr.s_net == ATADDR_ANYNET
152 && to->sat_addr.s_node == ATADDR_BCAST
153 && s->protinfo.af_at.src_net == atif->address.s_net)
155 break;
158 if(to->sat_addr.s_net == s->protinfo.af_at.src_net
159 && (to->sat_addr.s_node == s->protinfo.af_at.src_node
160 || to->sat_addr.s_node == ATADDR_BCAST
161 || to->sat_addr.s_node == ATADDR_ANYNODE))
163 break;
166 /* XXXX.0 -- we got a request for this router. make sure
167 * that the node is appropriately set. */
168 if (to->sat_addr.s_node == ATADDR_ANYNODE &&
169 to->sat_addr.s_net != ATADDR_ANYNET &&
170 atif->address.s_node == s->protinfo.af_at.src_node) {
171 to->sat_addr.s_node = atif->address.s_node;
172 break;
176 return (s);
180 * Find a socket in the list.
182 static struct sock *atalk_find_socket(struct sockaddr_at *sat)
184 struct sock *s;
186 for(s = atalk_socket_list; s != NULL; s = s->next)
188 if(s->protinfo.af_at.src_net != sat->sat_addr.s_net)
190 continue;
193 if(s->protinfo.af_at.src_node != sat->sat_addr.s_node)
195 continue;
198 if(s->protinfo.af_at.src_port != sat->sat_port)
200 continue;
203 break;
206 return (s);
209 extern inline void atalk_destroy_socket(struct sock *sk)
211 sklist_destroy_socket(&atalk_socket_list, sk);
212 MOD_DEC_USE_COUNT;
216 * Called from proc fs
218 int atalk_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
220 struct sock *s;
221 int len=0;
222 off_t pos=0;
223 off_t begin=0;
226 * Output the AppleTalk data for the /proc filesystem.
229 len += sprintf(buffer,"Type local_addr remote_addr tx_queue rx_queue st uid\n");
230 for(s = atalk_socket_list; s != NULL; s = s->next)
232 len += sprintf(buffer+len,"%02X ", s->type);
233 len += sprintf(buffer+len,"%04X:%02X:%02X ",
234 ntohs(s->protinfo.af_at.src_net),
235 s->protinfo.af_at.src_node,
236 s->protinfo.af_at.src_port);
237 len += sprintf(buffer+len,"%04X:%02X:%02X ",
238 ntohs(s->protinfo.af_at.dest_net),
239 s->protinfo.af_at.dest_node,
240 s->protinfo.af_at.dest_port);
241 len += sprintf(buffer+len,"%08X:%08X ",
242 atomic_read(&s->wmem_alloc),
243 atomic_read(&s->rmem_alloc));
244 len += sprintf(buffer+len,"%02X %d\n", s->state,
245 SOCK_INODE(s->socket)->i_uid);
247 /* Are we still dumping unwanted data then discard the record */
248 pos = begin + len;
250 if(pos < offset)
252 len = 0; /* Keep dumping into the buffer start */
253 begin = pos;
255 if(pos > offset + length) /* We have dumped enough */
256 break;
259 /* The data in question runs from begin to begin+len */
260 *start = buffer + (offset - begin); /* Start of wanted data */
261 len -= (offset - begin); /* Remove unwanted header data from length */
262 if(len > length)
263 len = length; /* Remove unwanted tail data from length */
265 return (len);
268 /**************************************************************************\
270 * Routing tables for the AppleTalk socket layer. *
272 \**************************************************************************/
274 static struct atalk_route *atalk_router_list = NULL;
275 static struct atalk_iface *atalk_iface_list = NULL;
276 static struct atalk_route atrtr_default; /* For probing devices or in a routerless network */
279 * AppleTalk interface control
283 * Drop a device. Doesn't drop any of its routes - that is the caller's
284 * problem. Called when we down the interface or delete the address.
286 static void atif_drop_device(struct device *dev)
288 struct atalk_iface **iface = &atalk_iface_list;
289 struct atalk_iface *tmp;
291 while((tmp = *iface) != NULL)
293 if(tmp->dev == dev)
295 *iface = tmp->next;
296 kfree_s(tmp, sizeof(struct atalk_iface));
297 dev->atalk_ptr=NULL;
298 MOD_DEC_USE_COUNT;
300 else
301 iface = &tmp->next;
306 static struct atalk_iface *atif_add_device(struct device *dev, struct at_addr *sa)
308 struct atalk_iface *iface = (struct atalk_iface *)
309 kmalloc(sizeof(*iface), GFP_KERNEL);
310 unsigned long flags;
312 if(iface==NULL)
313 return (NULL);
315 iface->dev=dev;
316 dev->atalk_ptr=iface;
317 iface->address= *sa;
318 iface->status=0;
319 save_flags(flags);
320 cli();
321 iface->next=atalk_iface_list;
322 atalk_iface_list=iface;
323 restore_flags(flags);
325 MOD_INC_USE_COUNT;
327 return (iface);
332 * Perform phase 2 AARP probing on our tentative address.
334 static int atif_probe_device(struct atalk_iface *atif)
336 int netrange=ntohs(atif->nets.nr_lastnet)-ntohs(atif->nets.nr_firstnet)+1;
337 int probe_net=ntohs(atif->address.s_net);
338 int probe_node=atif->address.s_node;
339 int netct, nodect;
342 * Offset the network we start probing with.
345 if(probe_net == ATADDR_ANYNET)
347 if(!netrange)
348 probe_net = ntohs(atif->nets.nr_firstnet);
349 else
350 probe_net = ntohs(atif->nets.nr_firstnet) + (jiffies%netrange);
353 if(probe_node == ATADDR_ANYNODE)
354 probe_node = jiffies&0xFF;
357 * Scan the networks.
359 atif->status |= ATIF_PROBE;
360 for(netct = 0; netct <= netrange; netct++)
363 * Sweep the available nodes from a given start.
366 atif->address.s_net = htons(probe_net);
367 for(nodect = 0; nodect < 256; nodect++)
369 atif->address.s_node = ((nodect+probe_node) & 0xFF);
370 if(atif->address.s_node > 0 && atif->address.s_node<254)
373 * Probe a proposed address.
375 aarp_probe_network(atif);
377 if(!(atif->status & ATIF_PROBE_FAIL)) {
378 atif->status &= ~ATIF_PROBE;
379 return (0);
382 atif->status &= ~ATIF_PROBE_FAIL;
384 probe_net++;
385 if(probe_net > ntohs(atif->nets.nr_lastnet))
386 probe_net = ntohs(atif->nets.nr_firstnet);
388 atif->status &= ~ATIF_PROBE;
389 return (-EADDRINUSE); /* Network is full... */
394 * Perform AARP probing for a proxy address
396 static int atif_proxy_probe_device(struct atalk_iface *atif, struct at_addr* proxy_addr)
398 int netrange=ntohs(atif->nets.nr_lastnet)-ntohs(atif->nets.nr_firstnet)+1;
399 int probe_net=ntohs(atif->address.s_net); // we probe the interface's network
400 int probe_node=ATADDR_ANYNODE; // we'll take anything
401 int netct, nodect;
404 * Offset the network we start probing with.
407 if(probe_net == ATADDR_ANYNET)
409 if(!netrange)
410 probe_net = ntohs(atif->nets.nr_firstnet);
411 else
412 probe_net = ntohs(atif->nets.nr_firstnet) + (jiffies%netrange);
415 if(probe_node == ATADDR_ANYNODE)
416 probe_node = jiffies&0xFF;
419 * Scan the networks.
422 for(netct = 0; netct <= netrange; netct++)
425 * Sweep the available nodes from a given start.
428 proxy_addr->s_net = htons(probe_net);
429 for(nodect = 0; nodect < 256; nodect++)
431 proxy_addr->s_node = ((nodect+probe_node) & 0xFF);
432 if((proxy_addr->s_node>0) && (proxy_addr->s_node<254))
435 * Tell AARP to probe a proposed address.
437 int probe_result = aarp_proxy_probe_network(atif, proxy_addr);
439 if (probe_result == 0)
440 return 0;
442 if (probe_result != -EADDRINUSE)
443 return probe_result;
446 probe_net++;
447 if(probe_net > ntohs(atif->nets.nr_lastnet))
448 probe_net = ntohs(atif->nets.nr_firstnet);
451 return (-EADDRINUSE); /* Network is full... */
455 struct at_addr *atalk_find_dev_addr(struct device *dev)
457 struct atalk_iface *iface=dev->atalk_ptr;
459 if(iface)
460 return (&iface->address);
462 return (NULL);
465 static struct at_addr *atalk_find_primary(void)
467 struct atalk_iface *iface;
468 struct atalk_iface *fiface = NULL;
471 * Return a point-to-point interface only if
472 * there is no non-ptp interface available.
474 for(iface=atalk_iface_list; iface != NULL; iface=iface->next)
476 if(!fiface && !(iface->dev->flags & IFF_LOOPBACK))
477 fiface=iface;
478 if(!(iface->dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT)))
479 return (&iface->address);
482 if(fiface)
483 return (&fiface->address);
484 if(atalk_iface_list != NULL)
485 return (&atalk_iface_list->address);
486 else
487 return (NULL);
491 * Find a match for 'any network' - ie any of our interfaces with that
492 * node number will do just nicely.
494 static struct atalk_iface *atalk_find_anynet(int node, struct device *dev)
496 struct atalk_iface *iface=dev->atalk_ptr;
498 if(iface==NULL || (iface->status & ATIF_PROBE))
499 return (NULL);
500 if(node == ATADDR_BCAST || iface->address.s_node == node
501 || node == ATADDR_ANYNODE)
502 return (iface);
504 return (NULL);
508 * Find a match for a specific network:node pair
510 static struct atalk_iface *atalk_find_interface(int net, int node)
512 struct atalk_iface *iface;
514 for(iface=atalk_iface_list; iface != NULL; iface=iface->next)
516 if((node==ATADDR_BCAST || node==ATADDR_ANYNODE
517 || iface->address.s_node==node)
518 && iface->address.s_net==net
519 && !(iface->status & ATIF_PROBE))
520 return (iface);
522 /* XXXX.0 -- net.0 returns the iface associated with net */
523 if ((node==ATADDR_ANYNODE) && (net != ATADDR_ANYNET) &&
524 (ntohs(iface->nets.nr_firstnet) <= ntohs(net)) &&
525 (ntohs(net) <= ntohs(iface->nets.nr_lastnet)))
526 return (iface);
529 return (NULL);
534 * Find a route for an AppleTalk packet. This ought to get cached in
535 * the socket (later on...). We know about host routes and the fact
536 * that a route must be direct to broadcast.
538 static struct atalk_route *atrtr_find(struct at_addr *target)
541 * we must search through all routes unless we find a
542 * host route, because some host routes might overlap
543 * network routes
545 struct atalk_route *r;
546 struct atalk_route *net_route = NULL;
548 for(r=atalk_router_list; r != NULL; r=r->next)
550 if(!(r->flags & RTF_UP))
551 continue;
552 if(r->target.s_net == target->s_net)
554 if (r->flags & RTF_HOST)
557 * if this host route is for the target,
558 * the we're done
560 if (r->target.s_node == target->s_node)
561 return (r);
563 else
566 * this route will work if there isn't a
567 * direct host route, so cache it
569 net_route = r;
575 * if we found a network route but not a direct host
576 * route, then return it
578 if (net_route != NULL)
579 return (net_route);
581 if(atrtr_default.dev)
582 return (&atrtr_default);
584 return (NULL);
589 * Given an AppleTalk network, find the device to use. This can be
590 * a simple lookup.
592 struct device *atrtr_get_dev(struct at_addr *sa)
594 struct atalk_route *atr=atrtr_find(sa);
596 if(atr == NULL)
597 return (NULL);
598 else
599 return (atr->dev);
603 * Set up a default router.
605 static void atrtr_set_default(struct device *dev)
607 atrtr_default.dev = dev;
608 atrtr_default.flags = RTF_UP;
609 atrtr_default.gateway.s_net = htons(0);
610 atrtr_default.gateway.s_node = 0;
614 * Add a router. Basically make sure it looks valid and stuff the
615 * entry in the list. While it uses netranges we always set them to one
616 * entry to work like netatalk.
618 static int atrtr_create(struct rtentry *r, struct device *devhint)
620 struct sockaddr_at *ta=(struct sockaddr_at *)&r->rt_dst;
621 struct sockaddr_at *ga=(struct sockaddr_at *)&r->rt_gateway;
622 struct atalk_route *rt;
623 struct atalk_iface *iface, *riface;
624 unsigned long flags;
626 save_flags(flags);
629 * Fixme: Raise/Lower a routing change semaphore for these
630 * operations.
634 * Validate the request
636 if(ta->sat_family != AF_APPLETALK)
637 return (-EINVAL);
638 if(devhint == NULL && ga->sat_family != AF_APPLETALK)
639 return (-EINVAL);
642 * Now walk the routing table and make our decisions.
644 for(rt=atalk_router_list; rt!=NULL; rt=rt->next)
646 if(r->rt_flags != rt->flags)
647 continue;
649 if(ta->sat_addr.s_net == rt->target.s_net)
651 if(!(rt->flags & RTF_HOST))
652 break;
653 if(ta->sat_addr.s_node == rt->target.s_node)
654 break;
658 if(devhint == NULL)
660 for(riface = NULL, iface = atalk_iface_list; iface; iface = iface->next)
662 if(riface == NULL && ntohs(ga->sat_addr.s_net) >= ntohs(iface->nets.nr_firstnet) &&
663 ntohs(ga->sat_addr.s_net) <= ntohs(iface->nets.nr_lastnet))
665 riface = iface;
667 if(ga->sat_addr.s_net == iface->address.s_net
668 && ga->sat_addr.s_node == iface->address.s_node)
669 riface = iface;
672 if(riface == NULL)
673 return (-ENETUNREACH);
674 devhint = riface->dev;
677 if(rt == NULL)
679 rt = (struct atalk_route *)kmalloc(sizeof(struct atalk_route), GFP_KERNEL);
680 if(rt == NULL)
681 return (-ENOBUFS);
682 cli();
683 rt->next = atalk_router_list;
684 atalk_router_list = rt;
688 * Fill in the routing entry.
690 rt->target = ta->sat_addr;
691 rt->dev = devhint;
692 rt->flags = r->rt_flags;
693 rt->gateway = ga->sat_addr;
695 restore_flags(flags);
697 return (0);
701 * Delete a route. Find it and discard it.
703 static int atrtr_delete( struct at_addr *addr )
705 struct atalk_route **r = &atalk_router_list;
706 struct atalk_route *tmp;
708 while((tmp = *r) != NULL)
710 if(tmp->target.s_net == addr->s_net
711 && (!(tmp->flags&RTF_GATEWAY)
712 || tmp->target.s_node == addr->s_node))
714 *r = tmp->next;
715 kfree_s(tmp, sizeof(struct atalk_route));
716 return (0);
718 r = &tmp->next;
721 return (-ENOENT);
725 * Called when a device is downed. Just throw away any routes
726 * via it.
728 void atrtr_device_down(struct device *dev)
730 struct atalk_route **r = &atalk_router_list;
731 struct atalk_route *tmp;
733 while((tmp = *r) != NULL)
735 if(tmp->dev == dev)
737 *r = tmp->next;
738 kfree_s(tmp, sizeof(struct atalk_route));
740 else
741 r = &tmp->next;
744 if(atrtr_default.dev == dev)
745 atrtr_set_default(NULL);
749 * Actually down the interface.
751 static inline void atalk_dev_down(struct device *dev)
753 atrtr_device_down(dev); /* Remove all routes for the device */
754 aarp_device_down(dev); /* Remove AARP entries for the device */
755 atif_drop_device(dev); /* Remove the device */
759 * A device event has occurred. Watch for devices going down and
760 * delete our use of them (iface and route).
762 static int ddp_device_event(struct notifier_block *this, unsigned long event, void *ptr)
764 if(event == NETDEV_DOWN)
766 /* Discard any use of this */
767 atalk_dev_down((struct device *) ptr);
770 return (NOTIFY_DONE);
774 * ioctl calls. Shouldn't even need touching.
778 * Device configuration ioctl calls.
780 int atif_ioctl(int cmd, void *arg)
782 struct ifreq atreq;
783 static char aarp_mcast[6] = {0x09, 0x00, 0x00, 0xFF, 0xFF, 0xFF};
784 struct netrange *nr;
785 struct sockaddr_at *sa;
786 struct device *dev;
787 struct atalk_iface *atif;
788 int ct;
789 int limit;
790 struct rtentry rtdef;
791 int add_route;
793 if(copy_from_user(&atreq,arg,sizeof(atreq)))
794 return (-EFAULT);
796 if((dev = dev_get(atreq.ifr_name)) == NULL)
797 return (-ENODEV);
799 sa=(struct sockaddr_at*)&atreq.ifr_addr;
800 atif=atalk_find_dev(dev);
802 switch(cmd)
804 case SIOCSIFADDR:
805 if(!capable(CAP_NET_ADMIN))
806 return (-EPERM);
807 if(sa->sat_family != AF_APPLETALK)
808 return (-EINVAL);
809 if(dev->type != ARPHRD_ETHER
810 && dev->type != ARPHRD_LOOPBACK
811 && dev->type != ARPHRD_LOCALTLK
812 && dev->type != ARPHRD_PPP)
813 return (-EPROTONOSUPPORT);
815 nr=(struct netrange *)&sa->sat_zero[0];
817 add_route = 1;
820 * if this is a point-to-point iface, and we already have an
821 * iface for this AppleTalk address, then we should not add a route
823 if (dev->flags & IFF_POINTOPOINT && atalk_find_interface(sa->sat_addr.s_net, sa->sat_addr.s_node))
825 printk(KERN_DEBUG "AppleTalk: point-to-point interface added with existing address\n");
826 add_route = 0;
830 * Phase 1 is fine on LocalTalk but we don't do
831 * EtherTalk phase 1. Anyone wanting to add it go ahead.
833 if(dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
834 return (-EPROTONOSUPPORT);
835 if(sa->sat_addr.s_node == ATADDR_BCAST
836 || sa->sat_addr.s_node == 254)
837 return (-EINVAL);
838 if(atif)
841 * Already setting address.
843 if(atif->status&ATIF_PROBE)
844 return (-EBUSY);
846 atif->address.s_net = sa->sat_addr.s_net;
847 atif->address.s_node = sa->sat_addr.s_node;
848 atrtr_device_down(dev); /* Flush old routes */
850 else
852 atif=atif_add_device(dev, &sa->sat_addr);
853 if (atif == NULL)
854 return (-ENOMEM);
856 atif->nets= *nr;
859 * Check if the chosen address is used. If so we
860 * error and atalkd will try another.
863 if(!(dev->flags & IFF_LOOPBACK) && !(dev->flags & IFF_POINTOPOINT) && atif_probe_device(atif) < 0)
865 atif_drop_device(dev);
866 return (-EADDRINUSE);
870 * Hey it worked - add the direct routes.
873 sa = (struct sockaddr_at *)&rtdef.rt_gateway;
874 sa->sat_family = AF_APPLETALK;
875 sa->sat_addr.s_net = atif->address.s_net;
876 sa->sat_addr.s_node = atif->address.s_node;
877 sa = (struct sockaddr_at *)&rtdef.rt_dst;
878 rtdef.rt_flags = RTF_UP;
879 sa->sat_family = AF_APPLETALK;
880 sa->sat_addr.s_node = ATADDR_ANYNODE;
881 if((dev->flags & IFF_LOOPBACK) || (dev->flags & IFF_POINTOPOINT))
882 rtdef.rt_flags |= RTF_HOST;
885 * Routerless initial state.
887 if(nr->nr_firstnet == htons(0)
888 && nr->nr_lastnet == htons(0xFFFE))
890 sa->sat_addr.s_net = atif->address.s_net;
891 atrtr_create(&rtdef, dev);
892 atrtr_set_default(dev);
894 else
896 limit = ntohs(nr->nr_lastnet);
897 if(limit - ntohs(nr->nr_firstnet) > 256)
899 printk(KERN_WARNING "Too many routes/iface.\n");
900 return (-EINVAL);
902 if (add_route)
903 for(ct=ntohs(nr->nr_firstnet);ct<=limit;ct++)
905 sa->sat_addr.s_net = htons(ct);
906 atrtr_create(&rtdef, dev);
909 dev_mc_add(dev, aarp_mcast, 6, 1);
910 return (0);
912 case SIOCGIFADDR:
913 if(atif == NULL)
914 return (-EADDRNOTAVAIL);
915 ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_family=AF_APPLETALK;
916 ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_addr=atif->address;
917 break;
919 case SIOCGIFBRDADDR:
920 if(atif == NULL)
921 return (-EADDRNOTAVAIL);
922 ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_family=AF_APPLETALK;
923 ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_addr.s_net=atif->address.s_net;
924 ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_addr.s_node=ATADDR_BCAST;
925 break;
927 case SIOCATALKDIFADDR:
928 case SIOCDIFADDR:
929 if(!capable(CAP_NET_ADMIN))
930 return (-EPERM);
931 if(sa->sat_family != AF_APPLETALK)
932 return (-EINVAL);
933 atalk_dev_down(dev);
934 break;
936 case SIOCSARP:
937 if(!capable(CAP_NET_ADMIN))
938 return (-EPERM);
939 if(sa->sat_family != AF_APPLETALK)
940 return (-EINVAL);
943 * for now, we only support proxy AARP on ELAP;
944 * we should be able to do it for LocalTalk, too.
946 if(dev->type != ARPHRD_ETHER)
947 return (-EPROTONOSUPPORT);
950 * atif points to the current interface on this network;
951 * we aren't concerned about its current status (at least for now),
952 * but it has all the settings about the network we're going
953 * to probe. consequently, it must exist.
955 if (!atif)
956 return (-EADDRNOTAVAIL);
958 nr=(struct netrange *)&(atif->nets);
960 * Phase 1 is fine on Localtalk but we don't do
961 * Ethertalk phase 1. Anyone wanting to add it go ahead.
963 if(dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
964 return (-EPROTONOSUPPORT);
966 if(sa->sat_addr.s_node == ATADDR_BCAST
967 || sa->sat_addr.s_node == 254)
968 return (-EINVAL);
971 * Check if the chosen address is used. If so we
972 * error and ATCP will try another.
974 if (atif_proxy_probe_device(atif, &(sa->sat_addr)) < 0)
975 return (-EADDRINUSE);
978 * We now have an address on the local network, and the AARP
979 * code will defend it for us until we take it down.
980 * We don't set up any routes right now, because ATCP will
981 * install them manually via SIOCADDRT.
983 break;
985 case SIOCDARP:
986 if(!capable(CAP_NET_ADMIN))
987 return (-EPERM);
988 if(sa->sat_family != AF_APPLETALK)
989 return (-EINVAL);
992 * give to aarp module to remove proxy entry
994 aarp_proxy_remove(atif->dev, &(sa->sat_addr));
996 return (0);
999 if(copy_to_user(arg, &atreq, sizeof(atreq)))
1000 return (-EFAULT);
1002 return (0);
1006 * Routing ioctl() calls
1008 static int atrtr_ioctl(unsigned int cmd, void *arg)
1010 struct rtentry rt;
1011 struct device *dev = NULL;
1013 if(copy_from_user(&rt, arg, sizeof(rt)))
1014 return (-EFAULT);
1016 switch(cmd)
1018 case SIOCDELRT:
1019 if(rt.rt_dst.sa_family != AF_APPLETALK)
1020 return (-EINVAL);
1021 return (atrtr_delete(&((struct sockaddr_at *)&rt.rt_dst)->sat_addr));
1023 case SIOCADDRT:
1024 /* FIX ME: the name of the device is still in user space, isn't it? */
1025 if (rt.rt_dev != NULL)
1026 if ((dev = dev_get(rt.rt_dev)) == NULL)
1027 return -(ENODEV);
1029 return (atrtr_create(&rt, dev));
1031 default:
1032 return (-EINVAL);
1036 /* Called from proc fs - just make it print the ifaces neatly */
1038 int atalk_if_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
1040 struct atalk_iface *iface;
1041 int len=0;
1042 off_t pos=0;
1043 off_t begin=0;
1045 len += sprintf(buffer,"Interface Address Networks Status\n");
1046 for(iface = atalk_iface_list; iface != NULL; iface = iface->next)
1048 len += sprintf(buffer+len,"%-16s %04X:%02X %04X-%04X %d\n",
1049 iface->dev->name, ntohs(iface->address.s_net),
1050 iface->address.s_node, ntohs(iface->nets.nr_firstnet),
1051 ntohs(iface->nets.nr_lastnet), iface->status);
1052 pos = begin + len;
1053 if(pos < offset)
1055 len = 0;
1056 begin = pos;
1058 if(pos > offset + length)
1059 break;
1061 *start = buffer + (offset - begin);
1062 len -= (offset - begin);
1063 if(len > length)
1064 len = length;
1066 return (len);
1069 /* Called from proc fs - just make it print the routes neatly */
1071 int atalk_rt_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
1073 struct atalk_route *rt;
1074 int len=0;
1075 off_t pos=0;
1076 off_t begin=0;
1078 len += sprintf(buffer,"Target Router Flags Dev\n");
1079 if(atrtr_default.dev)
1081 rt = &atrtr_default;
1082 len += sprintf(buffer+len,"Default %04X:%02X %-4d %s\n",
1083 ntohs(rt->gateway.s_net), rt->gateway.s_node, rt->flags,
1084 rt->dev->name);
1087 for(rt = atalk_router_list; rt != NULL; rt = rt->next)
1089 len += sprintf(buffer+len,"%04X:%02X %04X:%02X %-4d %s\n",
1090 ntohs(rt->target.s_net),rt->target.s_node,
1091 ntohs(rt->gateway.s_net), rt->gateway.s_node, rt->flags,
1092 rt->dev->name);
1093 pos = begin + len;
1094 if(pos < offset)
1096 len = 0;
1097 begin = pos;
1099 if(pos > offset + length)
1100 break;
1103 *start = buffer + (offset - begin);
1104 len -= (offset - begin);
1105 if(len > length)
1106 len = length;
1108 return (len);
1111 /**************************************************************************\
1113 * Handling for system calls applied via the various interfaces to an *
1114 * AppleTalk socket object. *
1116 \**************************************************************************/
1119 * Checksum: This is 'optional'. It's quite likely also a good
1120 * candidate for assembler hackery 8)
1122 unsigned short atalk_checksum(struct ddpehdr *ddp, int len)
1124 unsigned long sum=0; /* Assume unsigned long is >16 bits */
1125 unsigned char *data=(unsigned char *)ddp;
1127 len -= 4; /* skip header 4 bytes */
1128 data += 4;
1130 /* This ought to be unwrapped neatly. I'll trust gcc for now */
1131 while(len--)
1133 sum += *data;
1134 sum <<= 1;
1135 if(sum & 0x10000)
1137 sum++;
1138 sum &= 0xFFFF;
1140 data++;
1143 if(sum)
1144 return htons((unsigned short)sum);
1146 return 0xFFFF; /* Use 0xFFFF for 0. 0 itself means none */
1150 * Create a socket. Initialise the socket, blank the addresses
1151 * set the state.
1153 static int atalk_create(struct socket *sock, int protocol)
1155 struct sock *sk;
1157 sk = sk_alloc(PF_APPLETALK, GFP_KERNEL, 1);
1158 if(sk == NULL)
1159 return (-ENOMEM);
1161 switch(sock->type)
1164 * We permit SOCK_DGRAM and RAW is an extension. It is
1165 * trivial to do and gives you the full ELAP frame.
1166 * Should be handy for CAP 8)
1168 case SOCK_RAW:
1169 case SOCK_DGRAM:
1170 sock->ops = &atalk_dgram_ops;
1171 break;
1173 case SOCK_STREAM:
1175 * TO DO: if you want to implement ADSP, here's the place to start
1178 sock->ops = &atalk_stream_ops;
1179 break;
1181 default:
1182 sk_free((void *)sk);
1183 return (-ESOCKTNOSUPPORT);
1186 MOD_INC_USE_COUNT;
1188 sock_init_data(sock,sk);
1190 sk->destruct = NULL;
1191 /* Checksums on by default */
1192 sk->zapped = 1;
1194 return (0);
1198 * Free a socket. No work needed
1200 static int atalk_release(struct socket *sock, struct socket *peer)
1202 struct sock *sk=sock->sk;
1204 if(sk == NULL)
1205 return (0);
1207 if(!sk->dead)
1208 sk->state_change(sk);
1210 sk->dead = 1;
1211 sock->sk = NULL;
1212 atalk_destroy_socket(sk);
1214 return (0);
1218 * Pick a source address if one is not given. Just return
1219 * an error if not supportable.
1221 static int atalk_pick_port(struct sockaddr_at *sat)
1223 for(sat->sat_port = ATPORT_RESERVED; sat->sat_port < ATPORT_LAST; sat->sat_port++)
1225 if(atalk_find_socket(sat) == NULL)
1226 return sat->sat_port;
1229 return (-EBUSY);
1232 static int atalk_autobind(struct sock *sk)
1234 struct at_addr *ap = atalk_find_primary();
1235 struct sockaddr_at sat;
1236 int n;
1238 if(ap == NULL || ap->s_net == htons(ATADDR_ANYNET))
1239 return (-EADDRNOTAVAIL);
1241 sk->protinfo.af_at.src_net = sat.sat_addr.s_net = ap->s_net;
1242 sk->protinfo.af_at.src_node = sat.sat_addr.s_node = ap->s_node;
1244 if((n = atalk_pick_port(&sat)) < 0)
1245 return (n);
1247 sk->protinfo.af_at.src_port = n;
1248 atalk_insert_socket(sk);
1249 sk->zapped = 0;
1251 return (0);
1255 * Set the address 'our end' of the connection.
1257 static int atalk_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1259 struct sock *sk;
1260 struct sockaddr_at *addr=(struct sockaddr_at *)uaddr;
1262 sk = sock->sk;
1264 if(sk->zapped == 0)
1265 return (-EINVAL);
1267 if(addr_len != sizeof(struct sockaddr_at))
1268 return (-EINVAL);
1270 if(addr->sat_family != AF_APPLETALK)
1271 return (-EAFNOSUPPORT);
1273 if(addr->sat_addr.s_net == htons(ATADDR_ANYNET))
1275 struct at_addr *ap=atalk_find_primary();
1277 if(ap == NULL)
1278 return (-EADDRNOTAVAIL);
1280 sk->protinfo.af_at.src_net = addr->sat_addr.s_net = ap->s_net;
1281 sk->protinfo.af_at.src_node = addr->sat_addr.s_node= ap->s_node;
1283 else
1285 if(atalk_find_interface(addr->sat_addr.s_net, addr->sat_addr.s_node) == NULL)
1286 return (-EADDRNOTAVAIL);
1288 sk->protinfo.af_at.src_net = addr->sat_addr.s_net;
1289 sk->protinfo.af_at.src_node = addr->sat_addr.s_node;
1292 if(addr->sat_port == ATADDR_ANYPORT)
1294 int n = atalk_pick_port(addr);
1295 if(n < 0)
1296 return (n);
1298 sk->protinfo.af_at.src_port = addr->sat_port = n;
1300 else
1301 sk->protinfo.af_at.src_port = addr->sat_port;
1303 if(atalk_find_socket(addr) != NULL)
1304 return (-EADDRINUSE);
1306 atalk_insert_socket(sk);
1307 sk->zapped = 0;
1309 return (0);
1313 * Set the address we talk to.
1315 static int atalk_connect(struct socket *sock, struct sockaddr *uaddr,
1316 int addr_len, int flags)
1318 struct sock *sk=sock->sk;
1319 struct sockaddr_at *addr;
1321 sk->state = TCP_CLOSE;
1322 sock->state = SS_UNCONNECTED;
1324 if(addr_len != sizeof(*addr))
1325 return (-EINVAL);
1326 addr = (struct sockaddr_at *)uaddr;
1328 if(addr->sat_family != AF_APPLETALK)
1329 return (-EAFNOSUPPORT);
1331 if(addr->sat_addr.s_node == ATADDR_BCAST && !sk->broadcast)
1333 #if 1
1334 printk(KERN_WARNING "%s is broken and did not set SO_BROADCAST. It will break when 2.2 is released.\n",
1335 current->comm);
1336 #else
1337 return (-EACCES);
1338 #endif
1341 if(sk->zapped)
1343 if(atalk_autobind(sk) < 0)
1344 return (-EBUSY);
1347 if(atrtr_get_dev(&addr->sat_addr) == NULL)
1348 return (-ENETUNREACH);
1350 sk->protinfo.af_at.dest_port = addr->sat_port;
1351 sk->protinfo.af_at.dest_net = addr->sat_addr.s_net;
1352 sk->protinfo.af_at.dest_node = addr->sat_addr.s_node;
1354 sock->state = SS_CONNECTED;
1355 sk->state = TCP_ESTABLISHED;
1357 return (0);
1361 * Not relevant
1363 static int atalk_accept(struct socket *sock, struct socket *newsock, int flags)
1365 if(newsock->sk)
1367 sk_free(newsock->sk);
1368 MOD_DEC_USE_COUNT;
1371 return (-EOPNOTSUPP);
1375 * Find the name of an AppleTalk socket. Just copy the right
1376 * fields into the sockaddr.
1378 static int atalk_getname(struct socket *sock, struct sockaddr *uaddr,
1379 int *uaddr_len, int peer)
1381 struct sockaddr_at sat;
1382 struct sock *sk;
1384 sk = sock->sk;
1385 if(sk->zapped)
1387 if(atalk_autobind(sk) < 0)
1388 return (-ENOBUFS);
1391 *uaddr_len = sizeof(struct sockaddr_at);
1393 if(peer)
1395 if(sk->state != TCP_ESTABLISHED)
1396 return (-ENOTCONN);
1397 sat.sat_addr.s_net = sk->protinfo.af_at.dest_net;
1398 sat.sat_addr.s_node = sk->protinfo.af_at.dest_node;
1399 sat.sat_port = sk->protinfo.af_at.dest_port;
1401 else
1403 sat.sat_addr.s_net = sk->protinfo.af_at.src_net;
1404 sat.sat_addr.s_node = sk->protinfo.af_at.src_node;
1405 sat.sat_port = sk->protinfo.af_at.src_port;
1408 sat.sat_family = AF_APPLETALK;
1409 memcpy(uaddr, &sat, sizeof(sat));
1411 return (0);
1415 * Receive a packet (in skb) from device dev. This has come from the SNAP
1416 * decoder, and on entry skb->h.raw is the DDP header, skb->len is the DDP
1417 * header, skb->len is the DDP length. The physical headers have been
1418 * extracted. PPP should probably pass frames marked as for this layer.
1419 * [ie ARPHRD_ETHERTALK]
1421 static int atalk_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
1423 struct sock *sock;
1424 struct ddpehdr *ddp=(void *)skb->h.raw;
1425 struct atalk_iface *atif;
1426 struct sockaddr_at tosat;
1427 int origlen;
1428 struct ddpebits ddphv;
1430 /* Size check */
1431 if(skb->len < sizeof(*ddp))
1433 kfree_skb(skb);
1434 return (0);
1438 * Fix up the length field [Ok this is horrible but otherwise
1439 * I end up with unions of bit fields and messy bit field order
1440 * compiler/endian dependencies..]
1442 * FIXME: This is a write to a shared object. Granted it
1443 * happens to be safe BUT.. (Its safe as user space will not
1444 * run until we put it back)
1447 *((__u16 *)&ddphv) = ntohs(*((__u16 *)ddp));
1450 * Trim buffer in case of stray trailing data
1453 origlen = skb->len;
1455 skb_trim(skb, min(skb->len, ddphv.deh_len));
1458 * Size check to see if ddp->deh_len was crap
1459 * (Otherwise we'll detonate most spectacularly
1460 * in the middle of recvmsg()).
1462 if(skb->len < sizeof(*ddp))
1464 kfree_skb(skb);
1465 return (0);
1469 * Any checksums. Note we don't do htons() on this == is assumed to be
1470 * valid for net byte orders all over the networking code...
1472 if(ddp->deh_sum && atalk_checksum(ddp, ddphv.deh_len) != ddp->deh_sum)
1474 /* Not a valid AppleTalk frame - dustbin time */
1475 kfree_skb(skb);
1476 return (0);
1479 if(call_in_firewall(PF_APPLETALK, skb->dev, ddp, NULL,&skb)!=FW_ACCEPT)
1481 kfree_skb(skb);
1482 return (0);
1485 /* Check the packet is aimed at us */
1487 if(ddp->deh_dnet == 0) /* Net 0 is 'this network' */
1488 atif = atalk_find_anynet(ddp->deh_dnode, dev);
1489 else
1490 atif = atalk_find_interface(ddp->deh_dnet, ddp->deh_dnode);
1493 * Not ours, so we route the packet via the correct AppleTalk interface.
1495 if(atif == NULL)
1497 struct atalk_route *rt;
1498 struct at_addr ta;
1501 * Don't route multicast, etc., packets, or packets
1502 * sent to "this network"
1504 if (skb->pkt_type != PACKET_HOST || ddp->deh_dnet == 0)
1507 * FIX ME:
1508 * Can it ever happen that a packet is from a PPP iface and needs to be broadcast onto the default network?
1510 if (dev->type == ARPHRD_PPP)
1511 printk(KERN_DEBUG "AppleTalk: didn't forward broadcast packet received from PPP iface\n");
1513 kfree_skb(skb);
1514 return (0);
1518 * Check firewall allows this routing
1520 if(call_fw_firewall(PF_APPLETALK, skb->dev, ddp, NULL, &skb) != FW_ACCEPT)
1522 kfree_skb(skb);
1523 return (0);
1526 ta.s_net = ddp->deh_dnet;
1527 ta.s_node = ddp->deh_dnode;
1529 /* Route the packet */
1530 rt = atrtr_find(&ta);
1531 if(rt == NULL || ddphv.deh_hops == DDP_MAXHOPS)
1533 kfree_skb(skb);
1534 return (0);
1536 ddphv.deh_hops++;
1539 * Route goes through another gateway, so
1540 * set the target to the gateway instead.
1542 if(rt->flags & RTF_GATEWAY)
1544 ta.s_net = rt->gateway.s_net;
1545 ta.s_node = rt->gateway.s_node;
1548 /* Fix up skb->len field */
1549 skb_trim(skb, min(origlen, rt->dev->hard_header_len +
1550 ddp_dl->header_length + ddphv.deh_len));
1552 /* Mend the byte order */
1553 *((__u16 *)ddp) = ntohs(*((__u16 *)&ddphv));
1556 * Send the buffer onwards
1558 * Now we must always be careful. If it's come from
1559 * LocalTalk to EtherTalk it might not fit
1561 * Order matters here: If a packet has to be copied
1562 * to make a new headroom (rare hopefully) then it
1563 * won't need unsharing.
1565 * Note. ddp-> becomes invalid at the realloc.
1567 if(skb_headroom(skb) < 22)
1568 /* 22 bytes - 12 ether, 2 len, 3 802.2 5 snap */
1569 skb = skb_realloc_headroom(skb, 32);
1570 else
1571 skb = skb_unshare(skb, GFP_ATOMIC);
1574 * If the buffer didn't vanish into the lack of
1575 * space bitbucket we can send it.
1577 if(skb)
1579 if(aarp_send_ddp(rt->dev, skb, &ta, NULL) == -1)
1580 kfree_skb(skb);
1583 return (0);
1586 #if defined(CONFIG_IPDDP) || defined(CONFIG_IPDDP_MODULE)
1588 * Check if IP-over-DDP
1590 if(skb->data[12] == 22)
1592 struct device *dev;
1594 /* This needs to be able to handle ipddp"N" devices */
1595 if((dev = dev_get("ipddp0")) == NULL)
1596 return (-ENODEV);
1598 skb->protocol = htons(ETH_P_IP);
1599 skb_pull(skb, 13);
1600 skb->dev = dev;
1601 skb->h.raw = skb->data;
1603 ((struct net_device_stats *)dev->priv)->rx_packets++;
1604 ((struct net_device_stats *)dev->priv)->rx_bytes += skb->len+13;
1605 netif_rx(skb); /* Send the SKB up to a higher place. */
1607 return (0);
1609 #endif
1612 * Which socket - atalk_search_socket() looks for a *full match*
1613 * of the <net,node,port> tuple.
1615 tosat.sat_addr.s_net = ddp->deh_dnet;
1616 tosat.sat_addr.s_node = ddp->deh_dnode;
1617 tosat.sat_port = ddp->deh_dport;
1619 sock = atalk_search_socket(&tosat, atif);
1621 if(sock == NULL) /* But not one of our sockets */
1623 kfree_skb(skb);
1624 return (0);
1628 * Queue packet (standard)
1631 skb->sk = sock;
1633 if(sock_queue_rcv_skb(sock, skb) < 0)
1634 kfree_skb(skb);
1636 return (0);
1640 * Receive a LocalTalk frame. We make some demands on the caller here.
1641 * Caller must provide enough headroom on the packet to pull the short
1642 * header and append a long one.
1644 static int ltalk_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
1646 struct ddpehdr *ddp;
1647 struct at_addr *ap;
1650 * Expand any short form frames.
1652 if(skb->mac.raw[2] == 1)
1655 * Find our address.
1658 ap = atalk_find_dev_addr(dev);
1659 if(ap == NULL || skb->len < sizeof(struct ddpshdr))
1661 kfree_skb(skb);
1662 return (0);
1666 * The push leaves us with a ddephdr not an shdr, and
1667 * handily the port bytes in the right place preset.
1670 skb_push(skb, sizeof(*ddp) - 4);
1671 ddp = (struct ddpehdr *)skb->data;
1674 * Now fill in the long header.
1678 * These two first. The mac overlays the new source/dest
1679 * network information so we MUST copy these before
1680 * we write the network numbers !
1683 ddp->deh_dnode = skb->mac.raw[0]; /* From physical header */
1684 ddp->deh_snode = skb->mac.raw[1]; /* From physical header */
1686 ddp->deh_dnet = ap->s_net; /* Network number */
1687 ddp->deh_snet = ap->s_net;
1688 ddp->deh_sum = 0; /* No checksum */
1690 * Not sure about this bit...
1692 ddp->deh_len = skb->len;
1693 ddp->deh_hops = DDP_MAXHOPS; /* Non routable, so force a drop
1694 if we slip up later */
1696 /* Mend the byte order */
1697 *((__u16 *)ddp) = htons(*((__u16 *)ddp));
1699 skb->h.raw = skb->data;
1701 return (atalk_rcv(skb, dev, pt));
1704 static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, int len,
1705 struct scm_cookie *scm)
1707 struct sock *sk=sock->sk;
1708 struct sockaddr_at *usat=(struct sockaddr_at *)msg->msg_name;
1709 struct sockaddr_at local_satalk, gsat;
1710 struct sk_buff *skb;
1711 struct device *dev;
1712 struct ddpehdr *ddp;
1713 int size;
1714 struct atalk_route *rt;
1715 int loopback=0;
1716 int err;
1717 int flags = msg->msg_flags;
1719 if(flags & ~MSG_DONTWAIT)
1720 return (-EINVAL);
1722 if(len > DDP_MAXSZ)
1723 return (-EMSGSIZE);
1725 if(usat)
1727 if(sk->zapped)
1729 if(atalk_autobind(sk) < 0)
1730 return (-EBUSY);
1733 if(msg->msg_namelen < sizeof(*usat))
1734 return (-EINVAL);
1735 if(usat->sat_family != AF_APPLETALK)
1736 return (-EINVAL);
1738 /* netatalk doesn't implement this check */
1739 if(usat->sat_addr.s_node == ATADDR_BCAST && !sk->broadcast)
1741 printk(KERN_INFO "SO_BROADCAST: Fix your netatalk as it will break before 2.2\n");
1742 #if 0
1743 return (-EPERM);
1744 #endif
1747 else
1749 if(sk->state != TCP_ESTABLISHED)
1750 return (-ENOTCONN);
1751 usat =& local_satalk;
1752 usat->sat_family = AF_APPLETALK;
1753 usat->sat_port = sk->protinfo.af_at.dest_port;
1754 usat->sat_addr.s_node = sk->protinfo.af_at.dest_node;
1755 usat->sat_addr.s_net = sk->protinfo.af_at.dest_net;
1758 /* Build a packet */
1760 SOCK_DEBUG(sk, "SK %p: Got address.\n",sk);
1762 /* For headers */
1763 size = sizeof(struct ddpehdr) + len + ddp_dl->header_length;
1765 if(usat->sat_addr.s_net != 0 || usat->sat_addr.s_node == ATADDR_ANYNODE)
1767 rt = atrtr_find(&usat->sat_addr);
1768 if(rt == NULL)
1769 return (-ENETUNREACH);
1770 dev = rt->dev;
1772 else
1774 struct at_addr at_hint;
1775 at_hint.s_node = 0;
1776 at_hint.s_net = sk->protinfo.af_at.src_net;
1777 rt = atrtr_find(&at_hint);
1778 if(rt == NULL)
1779 return (-ENETUNREACH);
1780 dev = rt->dev;
1783 SOCK_DEBUG(sk, "SK %p: Size needed %d, device %s\n", sk, size, dev->name);
1785 size += dev->hard_header_len;
1787 skb = sock_alloc_send_skb(sk, size, 0, flags&MSG_DONTWAIT, &err);
1788 if(skb == NULL)
1789 return (err);
1791 skb->sk = sk;
1792 skb_reserve(skb, ddp_dl->header_length);
1793 skb_reserve(skb, dev->hard_header_len);
1795 skb->dev = dev;
1797 SOCK_DEBUG(sk, "SK %p: Begin build.\n", sk);
1799 ddp = (struct ddpehdr *)skb_put(skb,sizeof(struct ddpehdr));
1800 ddp->deh_pad = 0;
1801 ddp->deh_hops = 0;
1802 ddp->deh_len = len + sizeof(*ddp);
1804 * Fix up the length field [Ok this is horrible but otherwise
1805 * I end up with unions of bit fields and messy bit field order
1806 * compiler/endian dependencies..
1808 *((__u16 *)ddp) = ntohs(*((__u16 *)ddp));
1810 ddp->deh_dnet = usat->sat_addr.s_net;
1811 ddp->deh_snet = sk->protinfo.af_at.src_net;
1812 ddp->deh_dnode = usat->sat_addr.s_node;
1813 ddp->deh_snode = sk->protinfo.af_at.src_node;
1814 ddp->deh_dport = usat->sat_port;
1815 ddp->deh_sport = sk->protinfo.af_at.src_port;
1817 SOCK_DEBUG(sk, "SK %p: Copy user data (%d bytes).\n", sk, len);
1819 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
1820 if(err)
1822 kfree_skb(skb);
1823 return (-EFAULT);
1826 if(sk->no_check == 1)
1827 ddp->deh_sum = 0;
1828 else
1829 ddp->deh_sum = atalk_checksum(ddp, len + sizeof(*ddp));
1831 if(call_out_firewall(PF_APPLETALK, skb->dev, ddp, NULL, &skb) != FW_ACCEPT)
1833 kfree_skb(skb);
1834 return (-EPERM);
1838 * Loopback broadcast packets to non gateway targets (ie routes
1839 * to group we are in)
1841 if(ddp->deh_dnode == ATADDR_BCAST)
1843 if((!(rt->flags&RTF_GATEWAY)) && (!(dev->flags&IFF_LOOPBACK)))
1845 struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL);
1846 if(skb2)
1848 loopback = 1;
1849 SOCK_DEBUG(sk, "SK %p: send out(copy).\n", sk);
1850 if(aarp_send_ddp(dev, skb2, &usat->sat_addr, NULL) == -1)
1851 kfree_skb(skb2);
1852 /* else queued/sent above in the aarp queue */
1857 if((dev->flags & IFF_LOOPBACK) || loopback)
1859 SOCK_DEBUG(sk, "SK %p: Loop back.\n", sk);
1860 /* loop back */
1861 skb_orphan(skb);
1862 ddp_dl->datalink_header(ddp_dl, skb, dev->dev_addr);
1863 skb->mac.raw = skb->data;
1864 skb->h.raw = skb->data + ddp_dl->header_length + dev->hard_header_len;
1865 skb_pull(skb,dev->hard_header_len);
1866 skb_pull(skb,ddp_dl->header_length);
1867 atalk_rcv(skb, dev, NULL);
1869 else
1871 SOCK_DEBUG(sk, "SK %p: send out.\n", sk);
1872 if (rt->flags & RTF_GATEWAY)
1874 gsat.sat_addr = rt->gateway;
1875 usat = &gsat;
1878 if(aarp_send_ddp(dev, skb, &usat->sat_addr, NULL) == -1)
1879 kfree_skb(skb);
1880 /* else queued/sent above in the aarp queue */
1882 SOCK_DEBUG(sk, "SK %p: Done write (%d).\n", sk, len);
1884 return (len);
1887 static int atalk_recvmsg(struct socket *sock, struct msghdr *msg, int size,
1888 int flags, struct scm_cookie *scm)
1890 struct sock *sk=sock->sk;
1891 struct sockaddr_at *sat=(struct sockaddr_at *)msg->msg_name;
1892 struct ddpehdr *ddp = NULL;
1893 struct ddpebits ddphv;
1894 int copied = 0;
1895 struct sk_buff *skb;
1896 int err = 0;
1899 skb = skb_recv_datagram(sk,flags&~MSG_DONTWAIT,flags&MSG_DONTWAIT,&err);
1900 if(skb == NULL)
1901 return (err);
1903 ddp = (struct ddpehdr *)(skb->h.raw);
1904 *((__u16 *)&ddphv) = ntohs(*((__u16 *)ddp));
1906 if(sk->type == SOCK_RAW)
1908 copied = ddphv.deh_len;
1909 if(copied > size)
1911 copied = size;
1912 msg->msg_flags |= MSG_TRUNC;
1915 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1917 else
1919 copied = ddphv.deh_len - sizeof(*ddp);
1920 if(copied > size)
1922 copied = size;
1923 msg->msg_flags |= MSG_TRUNC;
1925 err = skb_copy_datagram_iovec(skb,sizeof(*ddp),msg->msg_iov,copied);
1928 if(!err)
1930 if(sat)
1932 sat->sat_family = AF_APPLETALK;
1933 sat->sat_port = ddp->deh_sport;
1934 sat->sat_addr.s_node = ddp->deh_snode;
1935 sat->sat_addr.s_net = ddp->deh_snet;
1937 msg->msg_namelen = sizeof(*sat);
1940 skb_free_datagram(sk, skb); /* Free the datagram. */
1942 return (err ? err : (copied));
1945 static int atalk_shutdown(struct socket *sk,int how)
1947 return (-EOPNOTSUPP);
1951 * AppleTalk ioctl calls.
1953 static int atalk_ioctl(struct socket *sock,unsigned int cmd, unsigned long arg)
1955 long amount=0;
1956 struct sock *sk=sock->sk;
1958 switch(cmd)
1961 * Protocol layer
1963 case TIOCOUTQ:
1964 amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1965 if(amount < 0)
1966 amount = 0;
1967 break;
1969 case TIOCINQ:
1971 struct sk_buff *skb;
1972 /* These two are safe on a single CPU system as only user tasks fiddle here */
1973 if((skb = skb_peek(&sk->receive_queue)) != NULL)
1974 amount = skb->len-sizeof(struct ddpehdr);
1975 break;
1978 case SIOCGSTAMP:
1979 if(sk)
1981 if(sk->stamp.tv_sec == 0)
1982 return -ENOENT;
1983 return (copy_to_user((void *)arg,&sk->stamp,sizeof(struct timeval)) ? -EFAULT : 0);
1985 return (-EINVAL);
1988 * Routing
1990 case SIOCADDRT:
1991 case SIOCDELRT:
1992 if(!capable(CAP_NET_ADMIN))
1993 return -EPERM;
1994 return (atrtr_ioctl(cmd,(void *)arg));
1997 * Interface
1999 case SIOCGIFADDR:
2000 case SIOCSIFADDR:
2001 case SIOCGIFBRDADDR:
2002 case SIOCATALKDIFADDR:
2003 case SIOCDIFADDR:
2004 case SIOCSARP: /* proxy AARP */
2005 case SIOCDARP: /* proxy AARP */
2006 return (atif_ioctl(cmd,(void *)arg));
2009 * Physical layer ioctl calls
2011 case SIOCSIFLINK:
2012 case SIOCGIFHWADDR:
2013 case SIOCSIFHWADDR:
2014 case SIOCGIFFLAGS:
2015 case SIOCSIFFLAGS:
2016 case SIOCGIFMTU:
2017 case SIOCGIFCONF:
2018 case SIOCADDMULTI:
2019 case SIOCDELMULTI:
2020 case SIOCGIFCOUNT:
2021 case SIOCGIFINDEX:
2022 case SIOCGIFNAME:
2023 return ((dev_ioctl(cmd,(void *) arg)));
2025 case SIOCSIFMETRIC:
2026 case SIOCSIFBRDADDR:
2027 case SIOCGIFNETMASK:
2028 case SIOCSIFNETMASK:
2029 case SIOCGIFMEM:
2030 case SIOCSIFMEM:
2031 case SIOCGIFDSTADDR:
2032 case SIOCSIFDSTADDR:
2033 return (-EINVAL);
2035 default:
2036 return (-EINVAL);
2039 return (put_user(amount, (int *)arg));
2042 static struct net_proto_family atalk_family_ops=
2044 PF_APPLETALK,
2045 atalk_create
2048 static struct proto_ops atalk_dgram_ops=
2050 PF_APPLETALK,
2052 sock_no_dup,
2053 atalk_release,
2054 atalk_bind,
2055 atalk_connect,
2056 sock_no_socketpair,
2057 atalk_accept,
2058 atalk_getname,
2059 datagram_poll,
2060 atalk_ioctl,
2061 sock_no_listen,
2062 atalk_shutdown,
2063 sock_no_setsockopt,
2064 sock_no_getsockopt,
2065 sock_no_fcntl,
2066 atalk_sendmsg,
2067 atalk_recvmsg
2070 static struct notifier_block ddp_notifier=
2072 ddp_device_event,
2073 NULL,
2077 struct packet_type ltalk_packet_type=
2080 NULL,
2081 ltalk_rcv,
2082 NULL,
2083 NULL
2086 struct packet_type ppptalk_packet_type=
2089 NULL,
2090 atalk_rcv,
2091 NULL,
2092 NULL
2095 static char ddp_snap_id[] = {0x08, 0x00, 0x07, 0x80, 0x9B};
2098 * Export symbols for use by drivers when AppleTalk is a module.
2100 EXPORT_SYMBOL(aarp_send_ddp);
2101 EXPORT_SYMBOL(atrtr_get_dev);
2102 EXPORT_SYMBOL(atalk_find_dev_addr);
2104 #ifdef CONFIG_PROC_FS
2105 static struct proc_dir_entry proc_appletalk=
2107 PROC_NET_ATALK, 9, "appletalk",
2108 S_IFREG | S_IRUGO, 1, 0, 0,
2109 0, &proc_net_inode_operations,
2110 atalk_get_info
2113 static struct proc_dir_entry proc_atalk_route=
2115 PROC_NET_AT_ROUTE, 11,"atalk_route",
2116 S_IFREG | S_IRUGO, 1, 0, 0,
2117 0, &proc_net_inode_operations,
2118 atalk_rt_get_info
2121 static struct proc_dir_entry proc_atalk_iface=
2123 PROC_NET_ATIF, 11,"atalk_iface",
2124 S_IFREG | S_IRUGO, 1, 0, 0,
2125 0, &proc_net_inode_operations,
2126 atalk_if_get_info
2128 #endif /* CONFIG_PROC_FS */
2130 /* Called by proto.c on kernel start up */
2132 __initfunc(void atalk_proto_init(struct net_proto *pro))
2134 (void) sock_register(&atalk_family_ops);
2135 if((ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv)) == NULL)
2136 printk(KERN_CRIT "Unable to register DDP with SNAP.\n");
2138 ltalk_packet_type.type = htons(ETH_P_LOCALTALK);
2139 dev_add_pack(&ltalk_packet_type);
2141 ppptalk_packet_type.type = htons(ETH_P_PPPTALK);
2142 dev_add_pack(&ppptalk_packet_type);
2144 register_netdevice_notifier(&ddp_notifier);
2145 aarp_proto_init();
2147 #ifdef CONFIG_PROC_FS
2148 proc_net_register(&proc_appletalk);
2149 proc_net_register(&proc_atalk_route);
2150 proc_net_register(&proc_atalk_iface);
2152 aarp_register_proc_fs();
2153 #endif /* CONFIG_PROC_FS */
2155 #ifdef CONFIG_SYSCTL
2156 atalk_register_sysctl();
2157 #endif /* CONFIG_SYSCTL */
2159 printk(KERN_INFO "NET4: AppleTalk 0.18 for Linux NET4.0\n");
2162 #ifdef MODULE
2164 int init_module(void)
2166 atalk_proto_init(NULL);
2167 return (0);
2171 * Note on MOD_{INC,DEC}_USE_COUNT:
2173 * Use counts are incremented/decremented when
2174 * sockets are created/deleted.
2176 * AppleTalk interfaces are not incremented untill atalkd is run
2177 * and are only decremented when they are downed.
2179 * Ergo, before the AppleTalk module can be removed, all AppleTalk
2180 * sockets be closed from user space.
2183 void cleanup_module(void)
2185 #ifdef CONFIG_SYSCTL
2186 atalk_unregister_sysctl();
2187 #endif /* CONFIG_SYSCTL */
2189 #ifdef CONFIG_PROC_FS
2190 proc_net_unregister(PROC_NET_ATALK);
2191 proc_net_unregister(PROC_NET_AT_ROUTE);
2192 proc_net_unregister(PROC_NET_ATIF);
2194 aarp_unregister_proc_fs();
2195 #endif /* CONFIG_PROC_FS */
2197 aarp_cleanup_module(); /* General aarp clean-up. */
2199 unregister_netdevice_notifier(&ddp_notifier);
2200 dev_remove_pack(&ltalk_packet_type);
2201 dev_remove_pack(&ppptalk_packet_type);
2202 unregister_snap_client(ddp_snap_id);
2203 sock_unregister(PF_APPLETALK);
2205 return;
2208 #endif /* MODULE */
2209 #endif /* CONFIG_ATALK || CONFIG_ATALK_MODULE */