update atp870u driver to 0.78 from D-Link source
[linux-2.4.git] / net / appletalk / ddp.c
1 /*
2  *      DDP:    An implementation of the AppleTalk DDP protocol for
3  *              Ethernet 'ELAP'.
4  *
5  *              Alan Cox  <Alan.Cox@linux.org>
6  *
7  *              With more than a little assistance from
8  *
9  *              Wesley Craig <netatalk@umich.edu>
10  *
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
33  *                                              procfs, moved probing to AARP
34  *                                              module.
35  *              Adrian Sun/ 
36  *              Michael Zuelsdorff      :       fix for net.0 packets. don't 
37  *                                              allow illegal ether/tokentalk
38  *                                              port assignment. we lose a 
39  *                                              valid localtalk port as a 
40  *                                              result.
41  *              Arnaldo C. de Melo      :       Cleanup, in preparation for
42  *                                              shared skb support 8)
43  *
44  *              This program is free software; you can redistribute it and/or
45  *              modify it under the terms of the GNU General Public License
46  *              as published by the Free Software Foundation; either version
47  *              2 of the License, or (at your option) any later version.
48  * 
49  */
50
51 #include <linux/config.h>
52 #if defined(CONFIG_ATALK) || defined(CONFIG_ATALK_MODULE)
53 #include <linux/module.h>
54 #include <asm/uaccess.h>
55 #include <asm/system.h>
56 #include <asm/bitops.h>
57 #include <linux/types.h>
58 #include <linux/kernel.h>
59 #include <linux/sched.h>
60 #include <linux/string.h>
61 #include <linux/mm.h>
62 #include <linux/socket.h>
63 #include <linux/sockios.h>
64 #include <linux/in.h>
65 #include <linux/errno.h>
66 #include <linux/interrupt.h>
67 #include <linux/if_ether.h>
68 #include <linux/notifier.h>
69 #include <linux/netdevice.h>
70 #include <linux/inetdevice.h>
71 #include <linux/route.h>
72 #include <linux/inet.h>
73 #include <linux/etherdevice.h>
74 #include <linux/if_arp.h>
75 #include <linux/skbuff.h>
76 #include <linux/spinlock.h>
77 #include <linux/termios.h>      /* For TIOCOUTQ/INQ */
78 #include <net/datalink.h>
79 #include <net/p8022.h>
80 #include <net/psnap.h>
81 #include <net/sock.h>
82 #include <linux/ip.h>
83 #include <net/route.h>
84 #include <linux/atalk.h>
85 #include <linux/proc_fs.h>
86 #include <linux/stat.h>
87 #include <linux/init.h>
88
89 #ifdef CONFIG_PROC_FS
90 extern void aarp_register_proc_fs(void);
91 extern void aarp_unregister_proc_fs(void);
92 #endif
93
94 extern void aarp_cleanup_module(void);
95
96 extern void aarp_probe_network(struct atalk_iface *atif);
97 extern int  aarp_proxy_probe_network(struct atalk_iface *atif,
98                                         struct at_addr *sa);
99 extern void aarp_proxy_remove(struct net_device *dev, struct at_addr *sa);
100
101 #undef APPLETALK_DEBUG
102 #ifdef APPLETALK_DEBUG
103 #define DPRINT(x)               print(x)
104 #else
105 #define DPRINT(x)
106 #endif /* APPLETALK_DEBUG */
107
108 #ifdef CONFIG_SYSCTL
109 extern void atalk_register_sysctl(void);
110 extern void atalk_unregister_sysctl(void);
111 #endif /* CONFIG_SYSCTL */
112
113 struct datalink_proto *ddp_dl, *aarp_dl;
114 static struct proto_ops atalk_dgram_ops;
115
116 /**************************************************************************\
117 *                                                                          *
118 * Handlers for the socket list.                                            *
119 *                                                                          *
120 \**************************************************************************/
121
122 static struct sock *atalk_sockets;
123 static spinlock_t atalk_sockets_lock = SPIN_LOCK_UNLOCKED;
124
125 extern inline void atalk_insert_socket(struct sock *sk)
126 {
127         spin_lock_bh(&atalk_sockets_lock);
128         sk->next = atalk_sockets;
129         if (sk->next)
130                 atalk_sockets->pprev = &sk->next;
131         atalk_sockets = sk;
132         sk->pprev = &atalk_sockets;
133         spin_unlock_bh(&atalk_sockets_lock);
134 }
135
136 extern inline void atalk_remove_socket(struct sock *sk)
137 {
138         spin_lock_bh(&atalk_sockets_lock);
139         if (sk->pprev) {
140                 if (sk->next)
141                         sk->next->pprev = sk->pprev;
142                 *sk->pprev = sk->next;
143                 sk->pprev = NULL;
144         }
145         spin_unlock_bh(&atalk_sockets_lock);
146 }
147
148 static struct sock *atalk_search_socket(struct sockaddr_at *to,
149                                         struct atalk_iface *atif)
150 {
151         struct sock *s;
152
153         spin_lock_bh(&atalk_sockets_lock);
154         for (s = atalk_sockets; s; s = s->next) {
155                 if (to->sat_port != s->protinfo.af_at.src_port)
156                         continue;
157
158                 if (to->sat_addr.s_net == ATADDR_ANYNET &&
159                     to->sat_addr.s_node == ATADDR_BCAST &&
160                     s->protinfo.af_at.src_net == atif->address.s_net)
161                         break;
162
163                 if (to->sat_addr.s_net == s->protinfo.af_at.src_net &&
164                     (to->sat_addr.s_node == s->protinfo.af_at.src_node ||
165                      to->sat_addr.s_node == ATADDR_BCAST ||
166                      to->sat_addr.s_node == ATADDR_ANYNODE))
167                         break;
168
169                 /* XXXX.0 -- we got a request for this router. make sure
170                  * that the node is appropriately set. */
171                 if (to->sat_addr.s_node == ATADDR_ANYNODE &&
172                     to->sat_addr.s_net != ATADDR_ANYNET &&
173                     atif->address.s_node == s->protinfo.af_at.src_node) {
174                         to->sat_addr.s_node = atif->address.s_node;
175                         break; 
176                 }
177         }
178         spin_unlock_bh(&atalk_sockets_lock);
179         return s;
180 }
181
182 /*
183  * Try to find a socket matching ADDR in the socket list,
184  * if found then return it.  If not, insert SK into the
185  * socket list.
186  *
187  * This entire operation must execute atomically.
188  */
189 static struct sock *atalk_find_or_insert_socket(struct sock *sk,
190                                                 struct sockaddr_at *sat)
191 {
192         struct sock *s;
193
194         spin_lock_bh(&atalk_sockets_lock);
195         for (s = atalk_sockets; s; s = s->next)
196                 if (s->protinfo.af_at.src_net == sat->sat_addr.s_net &&
197                     s->protinfo.af_at.src_node == sat->sat_addr.s_node &&
198                     s->protinfo.af_at.src_port == sat->sat_port)
199                         break;
200
201         if (!s) {
202                 /* Wheee, it's free, assign and insert. */
203                 sk->next = atalk_sockets;
204                 if (sk->next)
205                         atalk_sockets->pprev = &sk->next;
206                 atalk_sockets = sk;
207                 sk->pprev = &atalk_sockets;
208         }
209
210         spin_unlock_bh(&atalk_sockets_lock);
211         return s;
212 }
213
214 static void atalk_destroy_timer(unsigned long data)
215 {
216         struct sock *sk = (struct sock *) data;
217
218         if (!atomic_read(&sk->wmem_alloc) &&
219             !atomic_read(&sk->rmem_alloc) && sk->dead) {
220                 sock_put(sk);
221                 MOD_DEC_USE_COUNT;
222         } else {
223                 sk->timer.expires = jiffies + SOCK_DESTROY_TIME;
224                 add_timer(&sk->timer);
225         }
226 }
227
228 extern inline void atalk_destroy_socket(struct sock *sk)
229 {
230         atalk_remove_socket(sk);
231         skb_queue_purge(&sk->receive_queue);
232
233         if (!atomic_read(&sk->wmem_alloc) &&
234             !atomic_read(&sk->rmem_alloc) && sk->dead) {
235                 sock_put(sk);
236                 MOD_DEC_USE_COUNT;
237         } else {
238                 init_timer(&sk->timer);
239                 sk->timer.expires = jiffies + SOCK_DESTROY_TIME;
240                 sk->timer.function = atalk_destroy_timer;
241                 sk->timer.data = (unsigned long) sk;
242                 add_timer(&sk->timer);
243         }
244 }
245
246 /* Called from proc fs */
247 static int atalk_get_info(char *buffer, char **start, off_t offset, int length)
248 {
249         off_t pos = 0;
250         off_t begin = 0;
251         int len = sprintf(buffer, "Type local_addr  remote_addr tx_queue "
252                                   "rx_queue st uid\n");
253         struct sock *s;
254         /* Output the AppleTalk data for the /proc filesystem */
255
256         spin_lock_bh(&atalk_sockets_lock);
257         for (s = atalk_sockets; s; s = s->next) {
258                 len += sprintf(buffer + len,"%02X   ", s->type);
259                 len += sprintf(buffer + len,"%04X:%02X:%02X  ",
260                                ntohs(s->protinfo.af_at.src_net),
261                                s->protinfo.af_at.src_node,
262                                s->protinfo.af_at.src_port);
263                 len += sprintf(buffer + len,"%04X:%02X:%02X  ",
264                                ntohs(s->protinfo.af_at.dest_net),
265                                s->protinfo.af_at.dest_node,
266                                s->protinfo.af_at.dest_port);
267                 len += sprintf(buffer + len,"%08X:%08X ",
268                                atomic_read(&s->wmem_alloc),
269                                atomic_read(&s->rmem_alloc));
270                 len += sprintf(buffer + len,"%02X %d\n", s->state, 
271                                SOCK_INODE(s->socket)->i_uid);
272
273                 /* Are we still dumping unwanted data then discard the record */
274                 pos = begin + len;
275
276                 if (pos < offset) {
277                         len = 0;        /* Keep dumping into the buffer start */
278                         begin = pos;
279                 }
280                 if (pos > offset + length)      /* We have dumped enough */
281                         break;
282         }
283         spin_unlock_bh(&atalk_sockets_lock);
284
285         /* The data in question runs from begin to begin+len */
286         *start = buffer + offset - begin;       /* Start of wanted data */
287         len -= offset - begin;   /* Remove unwanted header data from length */
288         if (len > length)
289                 len = length;      /* Remove unwanted tail data from length */
290
291         return len;
292 }
293
294 /**************************************************************************\
295 *                                                                          *
296 * Routing tables for the AppleTalk socket layer.                           *
297 *                                                                          *
298 \**************************************************************************/
299
300 /* Anti-deadlock ordering is router_lock --> iface_lock -DaveM */
301 static struct atalk_route *atalk_router_list;
302 static rwlock_t atalk_router_lock = RW_LOCK_UNLOCKED;
303
304 static struct atalk_iface *atalk_iface_list;
305 static spinlock_t atalk_iface_lock = SPIN_LOCK_UNLOCKED;
306
307 /* For probing devices or in a routerless network */
308 static struct atalk_route atrtr_default;
309
310 /* AppleTalk interface control */
311 /*
312  * Drop a device. Doesn't drop any of its routes - that is the caller's
313  * problem. Called when we down the interface or delete the address.
314  */
315 static void atif_drop_device(struct net_device *dev)
316 {
317         struct atalk_iface **iface = &atalk_iface_list;
318         struct atalk_iface *tmp;
319
320         spin_lock_bh(&atalk_iface_lock);
321         while ((tmp = *iface) != NULL) {
322                 if (tmp->dev == dev) {
323                         *iface = tmp->next;
324                         kfree(tmp);
325                         dev->atalk_ptr = NULL;
326                         MOD_DEC_USE_COUNT;
327                 } else
328                         iface = &tmp->next;
329         }
330         spin_unlock_bh(&atalk_iface_lock);
331 }
332
333 static struct atalk_iface *atif_add_device(struct net_device *dev,
334                                            struct at_addr *sa)
335 {
336         struct atalk_iface *iface = kmalloc(sizeof(*iface), GFP_KERNEL);
337
338         if (!iface)
339                 return NULL;
340
341         iface->dev = dev;
342         dev->atalk_ptr = iface;
343         iface->address = *sa;
344         iface->status = 0;
345
346         spin_lock_bh(&atalk_iface_lock);
347         iface->next = atalk_iface_list;
348         atalk_iface_list = iface;
349         spin_unlock_bh(&atalk_iface_lock);
350
351         MOD_INC_USE_COUNT;
352         return iface;
353 }
354
355 /* Perform phase 2 AARP probing on our tentative address */
356 static int atif_probe_device(struct atalk_iface *atif)
357 {
358         int netrange = ntohs(atif->nets.nr_lastnet) -
359                         ntohs(atif->nets.nr_firstnet) + 1;
360         int probe_net = ntohs(atif->address.s_net);
361         int probe_node = atif->address.s_node;
362         int netct, nodect;
363
364         /* Offset the network we start probing with */
365         if (probe_net == ATADDR_ANYNET) {
366                 probe_net = ntohs(atif->nets.nr_firstnet);
367                 if (netrange)
368                         probe_net += jiffies % netrange;
369         }
370         if (probe_node == ATADDR_ANYNODE)
371                 probe_node = jiffies & 0xFF;
372
373         /* Scan the networks */
374         atif->status |= ATIF_PROBE;
375         for (netct = 0; netct <= netrange; netct++) {
376                 /* Sweep the available nodes from a given start */
377                 atif->address.s_net = htons(probe_net);
378                 for (nodect = 0; nodect < 256; nodect++) {
379                         atif->address.s_node = ((nodect+probe_node) & 0xFF);
380                         if (atif->address.s_node > 0 &&
381                             atif->address.s_node < 254) {
382                                 /* Probe a proposed address */
383                                 aarp_probe_network(atif);
384
385                                 if (!(atif->status & ATIF_PROBE_FAIL)) {
386                                         atif->status &= ~ATIF_PROBE;
387                                         return 0;
388                                 }
389                         }
390                         atif->status &= ~ATIF_PROBE_FAIL;
391                 }
392                 probe_net++;
393                 if (probe_net > ntohs(atif->nets.nr_lastnet))
394                         probe_net = ntohs(atif->nets.nr_firstnet);
395         }
396         atif->status &= ~ATIF_PROBE;
397
398         return -EADDRINUSE;     /* Network is full... */
399 }
400
401
402 /* Perform AARP probing for a proxy address */
403 static int atif_proxy_probe_device(struct atalk_iface *atif,
404                                    struct at_addr* proxy_addr)
405 {
406         int netrange = ntohs(atif->nets.nr_lastnet) -
407                         ntohs(atif->nets.nr_firstnet) + 1;
408         /* we probe the interface's network */
409         int probe_net = ntohs(atif->address.s_net);
410         int probe_node = ATADDR_ANYNODE;            /* we'll take anything */
411         int netct, nodect;
412
413         /* Offset the network we start probing with */
414         if (probe_net == ATADDR_ANYNET) {
415                 probe_net = ntohs(atif->nets.nr_firstnet);
416                 if (netrange)
417                         probe_net += jiffies % netrange;
418         }
419
420         if (probe_node == ATADDR_ANYNODE)
421                 probe_node = jiffies & 0xFF;
422                 
423         /* Scan the networks */
424         for (netct = 0; netct <= netrange; netct++) {
425                 /* Sweep the available nodes from a given start */
426                 proxy_addr->s_net = htons(probe_net);
427                 for (nodect = 0; nodect < 256; nodect++) {
428                         proxy_addr->s_node = ((nodect + probe_node) & 0xFF);
429                         if (proxy_addr->s_node > 0 &&
430                             proxy_addr->s_node < 254) {
431                                 /* Tell AARP to probe a proposed address */
432                                 int ret = aarp_proxy_probe_network(atif,
433                                                                     proxy_addr);
434
435                                 if (ret != -EADDRINUSE)
436                                         return ret;
437                         }
438                 }
439                 probe_net++;
440                 if (probe_net > ntohs(atif->nets.nr_lastnet))
441                         probe_net = ntohs(atif->nets.nr_firstnet);
442         }
443
444         return -EADDRINUSE;     /* Network is full... */
445 }
446
447
448 struct at_addr *atalk_find_dev_addr(struct net_device *dev)
449 {
450         struct atalk_iface *iface = dev->atalk_ptr;
451         return iface ? &iface->address : NULL;
452 }
453
454 static struct at_addr *atalk_find_primary(void)
455 {
456         struct atalk_iface *fiface = NULL;
457         struct at_addr *retval;
458         struct atalk_iface *iface;
459
460         /*
461          * Return a point-to-point interface only if
462          * there is no non-ptp interface available.
463          */
464         spin_lock_bh(&atalk_iface_lock);
465         for (iface = atalk_iface_list; iface; iface = iface->next) {
466                 if (!fiface && !(iface->dev->flags & IFF_LOOPBACK))
467                         fiface = iface;
468                 if (!(iface->dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
469                         retval = &iface->address;
470                         goto out;
471                 }
472         }
473
474         if (fiface)
475                 retval = &fiface->address;
476         else if (atalk_iface_list)
477                 retval = &atalk_iface_list->address;
478         else
479                 retval = NULL;
480 out:    spin_unlock_bh(&atalk_iface_lock);
481         return retval;
482 }
483
484 /*
485  * Find a match for 'any network' - ie any of our interfaces with that
486  * node number will do just nicely.
487  */
488 static struct atalk_iface *atalk_find_anynet(int node, struct net_device *dev)
489 {
490         struct atalk_iface *iface = dev->atalk_ptr;
491
492         if (!iface || iface->status & ATIF_PROBE)
493                 return NULL;
494
495         if (node == ATADDR_BCAST ||
496             iface->address.s_node == node ||
497             node == ATADDR_ANYNODE)
498                 return iface;
499
500         return NULL;
501 }
502
503 /* Find a match for a specific network:node pair */
504 static struct atalk_iface *atalk_find_interface(int net, int node)
505 {
506         struct atalk_iface *iface;
507
508         spin_lock_bh(&atalk_iface_lock);
509         for (iface = atalk_iface_list; iface; iface = iface->next) {
510                 if ((node == ATADDR_BCAST ||
511                      node == ATADDR_ANYNODE ||
512                      iface->address.s_node == node) &&
513                     iface->address.s_net == net &&
514                     !(iface->status & ATIF_PROBE))
515                         break;
516
517                 /* XXXX.0 -- net.0 returns the iface associated with net */
518                 if (node == ATADDR_ANYNODE && net != ATADDR_ANYNET &&
519                     ntohs(iface->nets.nr_firstnet) <= ntohs(net) &&
520                     ntohs(net) <= ntohs(iface->nets.nr_lastnet))
521                         break;
522         }
523         spin_unlock_bh(&atalk_iface_lock);
524         return iface;
525 }
526
527
528 /*
529  * Find a route for an AppleTalk packet. This ought to get cached in
530  * the socket (later on...). We know about host routes and the fact
531  * that a route must be direct to broadcast.
532  */
533 static struct atalk_route *atrtr_find(struct at_addr *target)
534 {
535         /*
536          * we must search through all routes unless we find a 
537          * host route, because some host routes might overlap
538          * network routes
539          */
540         struct atalk_route *net_route = NULL;
541         struct atalk_route *r;
542         
543         read_lock_bh(&atalk_router_lock);
544         for (r = atalk_router_list; r; r = r->next) {
545                 if (!(r->flags & RTF_UP))
546                         continue;
547
548                 if (r->target.s_net == target->s_net) {
549                         if (r->flags & RTF_HOST) {
550                                 /*
551                                  * if this host route is for the target,
552                                  * the we're done
553                                  */
554                                 if (r->target.s_node == target->s_node)
555                                         goto out;
556                         } else
557                                 /*
558                                  * this route will work if there isn't a
559                                  * direct host route, so cache it
560                                  */
561                                 net_route = r;
562                 }
563         }
564         
565         /* 
566          * if we found a network route but not a direct host
567          * route, then return it
568          */
569         if (net_route)
570                 r = net_route;
571         else if (atrtr_default.dev)
572                 r = &atrtr_default;
573         else /* No route can be found */
574                 r = NULL;
575 out:    read_unlock_bh(&atalk_router_lock);
576         return r;
577 }
578
579
580 /*
581  * Given an AppleTalk network, find the device to use. This can be
582  * a simple lookup.
583  */
584 struct net_device *atrtr_get_dev(struct at_addr *sa)
585 {
586         struct atalk_route *atr = atrtr_find(sa);
587         return atr ? atr->dev : NULL;
588 }
589
590 /* Set up a default router */
591 static void atrtr_set_default(struct net_device *dev)
592 {
593         atrtr_default.dev = dev;
594         atrtr_default.flags = RTF_UP;
595         atrtr_default.gateway.s_net = htons(0);
596         atrtr_default.gateway.s_node = 0;
597 }
598
599 /*
600  * Add a router. Basically make sure it looks valid and stuff the
601  * entry in the list. While it uses netranges we always set them to one
602  * entry to work like netatalk.
603  */
604 static int atrtr_create(struct rtentry *r, struct net_device *devhint)
605 {
606         struct sockaddr_at *ta = (struct sockaddr_at *)&r->rt_dst;
607         struct sockaddr_at *ga = (struct sockaddr_at *)&r->rt_gateway;
608         struct atalk_route *rt;
609         struct atalk_iface *iface, *riface;
610         int retval;
611
612         /*
613          * Fixme: Raise/Lower a routing change semaphore for these
614          * operations.
615          */
616
617         /* Validate the request */
618         if (ta->sat_family != AF_APPLETALK)
619                 return -EINVAL;
620
621         if (!devhint && ga->sat_family != AF_APPLETALK)
622                 return -EINVAL;
623
624         /* Now walk the routing table and make our decisions */
625         write_lock_bh(&atalk_router_lock);
626         for (rt = atalk_router_list; rt; rt = rt->next) {
627                 if (r->rt_flags != rt->flags)
628                         continue;
629
630                 if (ta->sat_addr.s_net == rt->target.s_net) {
631                         if (!(rt->flags & RTF_HOST))
632                                 break;
633                         if (ta->sat_addr.s_node == rt->target.s_node)
634                                 break;
635                 }
636         }
637
638         if (!devhint) {
639                 riface = NULL;
640
641                 spin_lock_bh(&atalk_iface_lock);
642                 for (iface = atalk_iface_list; iface; iface = iface->next) {
643                         if (!riface &&
644                             ntohs(ga->sat_addr.s_net) >=
645                                         ntohs(iface->nets.nr_firstnet) &&
646                             ntohs(ga->sat_addr.s_net) <=
647                                         ntohs(iface->nets.nr_lastnet))
648                                 riface = iface;
649
650                         if (ga->sat_addr.s_net == iface->address.s_net &&
651                             ga->sat_addr.s_node == iface->address.s_node)
652                                 riface = iface;
653                 }               
654                 spin_unlock_bh(&atalk_iface_lock);
655
656                 retval = -ENETUNREACH;
657                 if (!riface)
658                         goto out;
659
660                 devhint = riface->dev;
661         }
662
663         if (!rt) {
664                 rt = kmalloc(sizeof(struct atalk_route), GFP_ATOMIC);
665
666                 retval = -ENOBUFS;
667                 if (!rt)
668                         goto out;
669
670                 rt->next = atalk_router_list;
671                 atalk_router_list = rt;
672         }
673
674         /* Fill in the routing entry */
675         rt->target  = ta->sat_addr;
676         rt->dev     = devhint;
677         rt->flags   = r->rt_flags;
678         rt->gateway = ga->sat_addr;
679
680         retval = 0;
681 out:    write_unlock_bh(&atalk_router_lock);
682         return retval;
683 }
684
685 /* Delete a route. Find it and discard it */
686 static int atrtr_delete(struct at_addr * addr)
687 {
688         struct atalk_route **r = &atalk_router_list;
689         int retval = 0;
690         struct atalk_route *tmp;
691
692         write_lock_bh(&atalk_router_lock);
693         while ((tmp = *r) != NULL) {
694                 if (tmp->target.s_net == addr->s_net &&
695                     (!(tmp->flags&RTF_GATEWAY) ||
696                      tmp->target.s_node == addr->s_node)) {
697                         *r = tmp->next;
698                         kfree(tmp);
699                         goto out;
700                 }
701                 r = &tmp->next;
702         }
703         retval = -ENOENT;
704 out:    write_unlock_bh(&atalk_router_lock);
705         return retval;
706 }
707
708 /*
709  * Called when a device is downed. Just throw away any routes
710  * via it.
711  */
712 void atrtr_device_down(struct net_device *dev)
713 {
714         struct atalk_route **r = &atalk_router_list;
715         struct atalk_route *tmp;
716
717         write_lock_bh(&atalk_router_lock);
718         while ((tmp = *r) != NULL) {
719                 if (tmp->dev == dev) {
720                         *r = tmp->next;
721                         kfree(tmp);
722                 } else
723                         r = &tmp->next;
724         }
725         write_unlock_bh(&atalk_router_lock);
726
727         if (atrtr_default.dev == dev)
728                 atrtr_set_default(NULL);
729 }
730
731 /* Actually down the interface */
732 static inline void atalk_dev_down(struct net_device *dev)
733 {
734         atrtr_device_down(dev); /* Remove all routes for the device */
735         aarp_device_down(dev);  /* Remove AARP entries for the device */
736         atif_drop_device(dev);  /* Remove the device */
737 }
738
739 /*
740  * A device event has occurred. Watch for devices going down and
741  * delete our use of them (iface and route).
742  */
743 static int ddp_device_event(struct notifier_block *this, unsigned long event,
744                                 void *ptr)
745 {
746         if (event == NETDEV_DOWN)
747                 /* Discard any use of this */
748                 atalk_dev_down((struct net_device *) ptr);
749
750         return NOTIFY_DONE;
751 }
752
753 /* ioctl calls. Shouldn't even need touching */
754 /* Device configuration ioctl calls */
755 static int atif_ioctl(int cmd, void *arg)
756 {
757         static char aarp_mcast[6] = {0x09, 0x00, 0x00, 0xFF, 0xFF, 0xFF};
758         struct ifreq atreq;
759         struct netrange *nr;
760         struct sockaddr_at *sa;
761         struct net_device *dev;
762         struct atalk_iface *atif;
763         int ct;
764         int limit;
765         struct rtentry rtdef;
766         int add_route;
767
768         if (copy_from_user(&atreq, arg, sizeof(atreq)))
769                 return -EFAULT;
770
771         dev = __dev_get_by_name(atreq.ifr_name);
772         if (!dev)
773                 return -ENODEV;
774
775         sa = (struct sockaddr_at*) &atreq.ifr_addr;
776         atif = atalk_find_dev(dev);
777
778         switch (cmd) {
779                 case SIOCSIFADDR:
780                         if (!capable(CAP_NET_ADMIN))
781                                 return -EPERM;
782                         if (sa->sat_family != AF_APPLETALK)
783                                 return -EINVAL;
784                         if (dev->type != ARPHRD_ETHER &&
785                             dev->type != ARPHRD_LOOPBACK &&
786                             dev->type != ARPHRD_LOCALTLK &&
787                             dev->type != ARPHRD_PPP)
788                                 return -EPROTONOSUPPORT;
789
790                         nr = (struct netrange *) &sa->sat_zero[0];
791                         add_route = 1;
792
793                         /*
794                          * if this is a point-to-point iface, and we already
795                          * have an iface for this AppleTalk address, then we
796                          * should not add a route
797                          */
798                         if ((dev->flags & IFF_POINTOPOINT) &&
799                             atalk_find_interface(sa->sat_addr.s_net,
800                                                  sa->sat_addr.s_node)) {
801                                 printk(KERN_DEBUG "AppleTalk: point-to-point "
802                                                   "interface added with "
803                                                   "existing address\n");
804                                 add_route = 0;
805                         }
806                         
807                         /*
808                          * Phase 1 is fine on LocalTalk but we don't do
809                          * EtherTalk phase 1. Anyone wanting to add it go ahead.
810                          */
811                         if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
812                                 return -EPROTONOSUPPORT;
813                         if (sa->sat_addr.s_node == ATADDR_BCAST ||
814                             sa->sat_addr.s_node == 254)
815                                 return -EINVAL;
816                         if (atif) {
817                                 /* Already setting address */
818                                 if (atif->status & ATIF_PROBE)
819                                         return -EBUSY;
820
821                                 atif->address.s_net  = sa->sat_addr.s_net;
822                                 atif->address.s_node = sa->sat_addr.s_node;
823                                 atrtr_device_down(dev); /* Flush old routes */
824                         } else {
825                                 atif = atif_add_device(dev, &sa->sat_addr);
826                                 if (!atif)
827                                         return -ENOMEM;
828                         }
829                         atif->nets = *nr;
830
831                         /*
832                          * Check if the chosen address is used. If so we
833                          * error and atalkd will try another.
834                          */
835
836                         if (!(dev->flags & IFF_LOOPBACK) &&
837                             !(dev->flags & IFF_POINTOPOINT) &&
838                             atif_probe_device(atif) < 0) {
839                                 atif_drop_device(dev);
840                                 return -EADDRINUSE;
841                         }
842
843                         /* Hey it worked - add the direct routes */
844                         sa = (struct sockaddr_at *) &rtdef.rt_gateway;
845                         sa->sat_family = AF_APPLETALK;
846                         sa->sat_addr.s_net  = atif->address.s_net;
847                         sa->sat_addr.s_node = atif->address.s_node;
848                         sa = (struct sockaddr_at *) &rtdef.rt_dst;
849                         rtdef.rt_flags = RTF_UP;
850                         sa->sat_family = AF_APPLETALK;
851                         sa->sat_addr.s_node = ATADDR_ANYNODE;
852                         if (dev->flags & IFF_LOOPBACK ||
853                             dev->flags & IFF_POINTOPOINT)
854                                 rtdef.rt_flags |= RTF_HOST;
855
856                         /* Routerless initial state */
857                         if (nr->nr_firstnet == htons(0) &&
858                             nr->nr_lastnet == htons(0xFFFE)) {
859                                 sa->sat_addr.s_net = atif->address.s_net;
860                                 atrtr_create(&rtdef, dev);
861                                 atrtr_set_default(dev);
862                         } else {
863                                 limit = ntohs(nr->nr_lastnet);
864                                 if (limit - ntohs(nr->nr_firstnet) > 4096) {
865                                         printk(KERN_WARNING "Too many routes/"
866                                                             "iface.\n");
867                                         return -EINVAL;
868                                 }
869                                 if (add_route)
870                                         for (ct = ntohs(nr->nr_firstnet);
871                                              ct <= limit; ct++) {
872                                                 sa->sat_addr.s_net = htons(ct);
873                                                 atrtr_create(&rtdef, dev);
874                                         }
875                         }
876                         dev_mc_add(dev, aarp_mcast, 6, 1);
877                         return 0;
878
879                 case SIOCGIFADDR:
880                         if (!atif)
881                                 return -EADDRNOTAVAIL;
882
883                         sa->sat_family = AF_APPLETALK;
884                         sa->sat_addr = atif->address;
885                         break;
886
887                 case SIOCGIFBRDADDR:
888                         if (!atif)
889                                 return -EADDRNOTAVAIL;
890
891                         sa->sat_family = AF_APPLETALK;
892                         sa->sat_addr.s_net = atif->address.s_net;
893                         sa->sat_addr.s_node = ATADDR_BCAST;
894                         break;
895
896                 case SIOCATALKDIFADDR:
897                 case SIOCDIFADDR:
898                         if (!capable(CAP_NET_ADMIN))
899                                 return -EPERM;
900                         if (sa->sat_family != AF_APPLETALK)
901                                 return -EINVAL;
902                         atalk_dev_down(dev);
903                         break;                  
904
905                 case SIOCSARP:
906                         if (!capable(CAP_NET_ADMIN))
907                                 return -EPERM;
908                         if (sa->sat_family != AF_APPLETALK)
909                                 return -EINVAL;
910                         if (!atif)
911                                 return -EADDRNOTAVAIL;
912
913                         /*
914                          * for now, we only support proxy AARP on ELAP;
915                          * we should be able to do it for LocalTalk, too.
916                          */
917                         if (dev->type != ARPHRD_ETHER)
918                                 return -EPROTONOSUPPORT;
919
920                         /*
921                          * atif points to the current interface on this network;
922                          * we aren't concerned about its current status (at
923                          * least for now), but it has all the settings about
924                          * the network we're going to probe. Consequently, it
925                          * must exist.
926                          */
927                         if (!atif)
928                                 return -EADDRNOTAVAIL;
929
930                         nr = (struct netrange *) &(atif->nets);
931                         /*
932                          * Phase 1 is fine on Localtalk but we don't do
933                          * Ethertalk phase 1. Anyone wanting to add it go ahead.
934                          */
935                         if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
936                                 return -EPROTONOSUPPORT;
937
938                         if (sa->sat_addr.s_node == ATADDR_BCAST ||
939                             sa->sat_addr.s_node == 254)
940                                 return -EINVAL;
941
942                         /*
943                          * Check if the chosen address is used. If so we
944                          * error and ATCP will try another.
945                          */
946                         if (atif_proxy_probe_device(atif, &(sa->sat_addr)) < 0)
947                                 return -EADDRINUSE;
948                         
949                         /*
950                          * We now have an address on the local network, and
951                          * the AARP code will defend it for us until we take it
952                          * down. We don't set up any routes right now, because
953                          * ATCP will install them manually via SIOCADDRT.
954                          */
955                         break;
956
957                 case SIOCDARP:
958                         if (!capable(CAP_NET_ADMIN))
959                                 return -EPERM;
960                         if (sa->sat_family != AF_APPLETALK)
961                                 return -EINVAL;
962                         if (!atif)
963                                 return -EADDRNOTAVAIL;
964
965                         /* give to aarp module to remove proxy entry */
966                         aarp_proxy_remove(atif->dev, &(sa->sat_addr));
967                         return 0;
968         }
969
970         return copy_to_user(arg, &atreq, sizeof(atreq)) ? -EFAULT : 0;
971 }
972
973 /* Routing ioctl() calls */
974 static int atrtr_ioctl(unsigned int cmd, void *arg)
975 {
976         struct net_device *dev = NULL;
977         struct rtentry rt;
978
979         if (copy_from_user(&rt, arg, sizeof(rt)))
980                 return -EFAULT;
981
982         switch (cmd) {
983                 case SIOCDELRT:
984                         if (rt.rt_dst.sa_family != AF_APPLETALK)
985                                 return -EINVAL;
986                         return atrtr_delete(&((struct sockaddr_at *)
987                                                 &rt.rt_dst)->sat_addr);
988
989                 case SIOCADDRT:
990                         /* FIXME: the name of the device is still in user
991                          * space, isn't it? */
992                         if (rt.rt_dev) {
993                                 dev = __dev_get_by_name(rt.rt_dev);
994                                 if (!dev)
995                                         return -ENODEV;
996                         }                       
997                         return atrtr_create(&rt, dev);
998         }
999         return -EINVAL;
1000 }
1001
1002 /* Called from proc fs - just make it print the ifaces neatly */
1003 static int atalk_if_get_info(char *buffer, char **start, off_t offset,
1004                              int length)
1005 {
1006         off_t pos = 0;
1007         off_t begin = 0;
1008         struct atalk_iface *iface;
1009         int len = sprintf(buffer, "Interface      Address   "
1010                                   "Networks   Status\n");
1011
1012         spin_lock_bh(&atalk_iface_lock);
1013         for (iface = atalk_iface_list; iface; iface = iface->next) {
1014                 len += sprintf(buffer+len,"%-16s %04X:%02X  %04X-%04X  %d\n",
1015                                iface->dev->name, ntohs(iface->address.s_net),
1016                                iface->address.s_node,
1017                                ntohs(iface->nets.nr_firstnet),
1018                                ntohs(iface->nets.nr_lastnet), iface->status);
1019                 pos = begin + len;
1020                 if (pos < offset) {
1021                         len   = 0;
1022                         begin = pos;
1023                 }
1024                 if (pos > offset + length)
1025                         break;
1026         }
1027         spin_unlock_bh(&atalk_iface_lock);
1028
1029         *start = buffer + (offset - begin);
1030         len -= (offset - begin);
1031         if (len > length)
1032                 len = length;
1033         return len;
1034 }
1035
1036 /* Called from proc fs - just make it print the routes neatly */
1037 static int atalk_rt_get_info(char *buffer, char **start, off_t offset,
1038                              int length)
1039 {
1040         off_t pos = 0;
1041         off_t begin = 0;
1042         int len = sprintf(buffer, "Target        Router  Flags Dev\n");
1043         struct atalk_route *rt;
1044
1045         if (atrtr_default.dev) {
1046                 rt = &atrtr_default;
1047                 len += sprintf(buffer + len,"Default     %04X:%02X  %-4d  %s\n",
1048                                ntohs(rt->gateway.s_net), rt->gateway.s_node,
1049                                rt->flags, rt->dev->name);
1050         }
1051
1052         read_lock_bh(&atalk_router_lock);
1053         for (rt = atalk_router_list; rt; rt = rt->next) {
1054                 len += sprintf(buffer + len,
1055                                 "%04X:%02X     %04X:%02X  %-4d  %s\n",
1056                                ntohs(rt->target.s_net), rt->target.s_node,
1057                                ntohs(rt->gateway.s_net), rt->gateway.s_node,
1058                                rt->flags, rt->dev->name);
1059                 pos = begin + len;
1060                 if (pos < offset) {
1061                         len = 0;
1062                         begin = pos;
1063                 }
1064                 if (pos > offset + length)
1065                         break;
1066         }
1067         read_unlock_bh(&atalk_router_lock);
1068
1069         *start = buffer + (offset - begin);
1070         len -= (offset - begin);
1071         if (len > length)
1072                 len = length;
1073         return len;
1074 }
1075
1076 /**************************************************************************\
1077 *                                                                          *
1078 * Handling for system calls applied via the various interfaces to an       *
1079 * AppleTalk socket object.                                                 *
1080 *                                                                          *
1081 \**************************************************************************/
1082
1083 /*
1084  * Checksum: This is 'optional'. It's quite likely also a good
1085  * candidate for assembler hackery 8)
1086  */
1087 unsigned short atalk_checksum(struct ddpehdr *ddp, int len)
1088 {
1089         unsigned long sum = 0;  /* Assume unsigned long is >16 bits */
1090         unsigned char *data = (unsigned char *) ddp;
1091
1092         len  -= 4;              /* skip header 4 bytes */
1093         data += 4;
1094
1095         /* This ought to be unwrapped neatly. I'll trust gcc for now */
1096         while (len--) {
1097                 sum += *data;
1098                 sum <<= 1;
1099                 if (sum & 0x10000) {
1100                         sum++;
1101                         sum &= 0xFFFF;
1102                 }
1103                 data++;
1104         }
1105         /* Use 0xFFFF for 0. 0 itself means none */
1106         return sum ? htons((unsigned short) sum) : 0xFFFF;
1107 }
1108
1109 /*
1110  * Create a socket. Initialise the socket, blank the addresses
1111  * set the state.
1112  */
1113 static int atalk_create(struct socket *sock, int protocol)
1114 {
1115         struct sock *sk = sk_alloc(PF_APPLETALK, GFP_KERNEL, 1);
1116
1117         if (!sk)
1118                 return -ENOMEM;
1119
1120         switch (sock->type) {
1121                 /*
1122                  * We permit SOCK_DGRAM and RAW is an extension. It is
1123                  * trivial to do and gives you the full ELAP frame.
1124                  * Should be handy for CAP 8) 
1125                  */
1126                 case SOCK_RAW:
1127                 case SOCK_DGRAM:
1128                         sock->ops = &atalk_dgram_ops;
1129                         break;
1130                         
1131                 case SOCK_STREAM:
1132                         /*
1133                          * TODO: if you want to implement ADSP, here's the
1134                          * place to start
1135                          */
1136                         /*
1137                         sock->ops = &atalk_stream_ops;
1138                         break;
1139                         */
1140                 default:
1141                         sk_free(sk);
1142                         return -ESOCKTNOSUPPORT;
1143         }
1144
1145         MOD_INC_USE_COUNT;
1146         sock_init_data(sock, sk);
1147         sk->destruct = NULL;
1148         /* Checksums on by default */
1149         sk->zapped = 1;
1150         return 0;
1151 }
1152
1153 /* Free a socket. No work needed */
1154 static int atalk_release(struct socket *sock)
1155 {
1156         struct sock *sk = sock->sk;
1157
1158         if (!sk)
1159                 return 0;
1160
1161         if (!sk->dead)
1162                 sk->state_change(sk);
1163
1164         sk->dead = 1;
1165         sock->sk = NULL;
1166         atalk_destroy_socket(sk);
1167         return 0;
1168 }
1169
1170 /*
1171  * Pick a source port when one is not given. If we can
1172  * find a suitable free one, we insert the socket into
1173  * the tables using it.
1174  *
1175  * This whole operation must be atomic.
1176  */
1177 static int atalk_pick_and_bind_port(struct sock *sk, struct sockaddr_at *sat)
1178 {
1179         struct sock *s;
1180         int retval;
1181
1182         spin_lock_bh(&atalk_sockets_lock);
1183
1184         for (sat->sat_port = ATPORT_RESERVED;
1185              sat->sat_port < ATPORT_LAST;
1186              sat->sat_port++) {
1187                 for (s = atalk_sockets; s; s = s->next) {
1188                         if (s->protinfo.af_at.src_net == sat->sat_addr.s_net &&
1189                             s->protinfo.af_at.src_node ==
1190                                 sat->sat_addr.s_node &&
1191                             s->protinfo.af_at.src_port == sat->sat_port)
1192                                 goto try_next_port;
1193                 }
1194
1195                 /* Wheee, it's free, assign and insert. */
1196                 sk->next = atalk_sockets;
1197                 if (sk->next)
1198                         atalk_sockets->pprev = &sk->next;
1199                 atalk_sockets = sk;
1200                 sk->pprev = &atalk_sockets;
1201                 sk->protinfo.af_at.src_port = sat->sat_port;
1202                 retval = 0;
1203                 goto out;
1204
1205         try_next_port:
1206                 ;
1207         }
1208
1209         retval = -EBUSY;
1210 out:    spin_unlock_bh(&atalk_sockets_lock);
1211         return retval;
1212 }
1213
1214 static int atalk_autobind(struct sock *sk)
1215 {
1216         struct sockaddr_at sat;
1217         int n;
1218         struct at_addr *ap = atalk_find_primary();
1219
1220         if (!ap || ap->s_net == htons(ATADDR_ANYNET))
1221                 return -EADDRNOTAVAIL;
1222
1223         sk->protinfo.af_at.src_net  = sat.sat_addr.s_net  = ap->s_net;
1224         sk->protinfo.af_at.src_node = sat.sat_addr.s_node = ap->s_node;
1225
1226         n = atalk_pick_and_bind_port(sk, &sat);
1227         if (n < 0)
1228                 return n;
1229
1230         sk->zapped = 0;
1231         return 0;
1232 }
1233
1234 /* Set the address 'our end' of the connection */
1235 static int atalk_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1236 {
1237         struct sockaddr_at *addr = (struct sockaddr_at *)uaddr;
1238         struct sock *sk = sock->sk;
1239
1240         if (!sk->zapped || addr_len != sizeof(struct sockaddr_at))
1241                 return -EINVAL;
1242
1243         if (addr->sat_family != AF_APPLETALK)
1244                 return -EAFNOSUPPORT;
1245
1246         if (addr->sat_addr.s_net == htons(ATADDR_ANYNET)) {
1247                 struct at_addr *ap = atalk_find_primary();
1248
1249                 if (!ap)
1250                         return -EADDRNOTAVAIL;
1251
1252                 sk->protinfo.af_at.src_net  = addr->sat_addr.s_net = ap->s_net;
1253                 sk->protinfo.af_at.src_node = addr->sat_addr.s_node= ap->s_node;
1254         } else {
1255                 if (!atalk_find_interface(addr->sat_addr.s_net,
1256                                           addr->sat_addr.s_node))
1257                         return -EADDRNOTAVAIL;
1258
1259                 sk->protinfo.af_at.src_net  = addr->sat_addr.s_net;
1260                 sk->protinfo.af_at.src_node = addr->sat_addr.s_node;
1261         }
1262
1263         if (addr->sat_port == ATADDR_ANYPORT) {
1264                 int n = atalk_pick_and_bind_port(sk, addr);
1265
1266                 if (n < 0)
1267                         return n;
1268         } else {
1269                 sk->protinfo.af_at.src_port = addr->sat_port;
1270
1271                 if (atalk_find_or_insert_socket(sk, addr))
1272                         return -EADDRINUSE;
1273         }
1274
1275         sk->zapped = 0;
1276         return 0;
1277 }
1278
1279 /* Set the address we talk to */
1280 static int atalk_connect(struct socket *sock, struct sockaddr *uaddr,
1281                          int addr_len, int flags)
1282 {
1283         struct sock *sk = sock->sk;
1284         struct sockaddr_at *addr;
1285
1286         sk->state   = TCP_CLOSE;
1287         sock->state = SS_UNCONNECTED;
1288
1289         if (addr_len != sizeof(*addr))
1290                 return -EINVAL;
1291
1292         addr = (struct sockaddr_at *)uaddr;
1293
1294         if (addr->sat_family != AF_APPLETALK)
1295                 return -EAFNOSUPPORT;
1296
1297         if (addr->sat_addr.s_node == ATADDR_BCAST && !sk->broadcast) {
1298 #if 1   
1299                 printk(KERN_WARNING "%s is broken and did not set "
1300                                     "SO_BROADCAST. It will break when 2.2 is "
1301                                     "released.\n",
1302                         current->comm);
1303 #else
1304                 return -EACCES;
1305 #endif                  
1306         }
1307
1308         if (sk->zapped)
1309                 if (atalk_autobind(sk) < 0)
1310                         return -EBUSY;
1311
1312         if (!atrtr_get_dev(&addr->sat_addr))
1313                 return -ENETUNREACH;
1314
1315         sk->protinfo.af_at.dest_port = addr->sat_port;
1316         sk->protinfo.af_at.dest_net  = addr->sat_addr.s_net;
1317         sk->protinfo.af_at.dest_node = addr->sat_addr.s_node;
1318
1319         sock->state = SS_CONNECTED;
1320         sk->state   = TCP_ESTABLISHED;
1321         return 0;
1322 }
1323
1324
1325 /*
1326  * Find the name of an AppleTalk socket. Just copy the right
1327  * fields into the sockaddr.
1328  */
1329 static int atalk_getname(struct socket *sock, struct sockaddr *uaddr,
1330                          int *uaddr_len, int peer)
1331 {
1332         struct sockaddr_at sat;
1333         struct sock *sk = sock->sk;
1334
1335         if (sk->zapped)
1336                 if (atalk_autobind(sk) < 0)
1337                         return -ENOBUFS;
1338
1339         *uaddr_len = sizeof(struct sockaddr_at);
1340
1341         if (peer) {
1342                 if (sk->state != TCP_ESTABLISHED)
1343                         return -ENOTCONN;
1344
1345                 sat.sat_addr.s_net  = sk->protinfo.af_at.dest_net;
1346                 sat.sat_addr.s_node = sk->protinfo.af_at.dest_node;
1347                 sat.sat_port = sk->protinfo.af_at.dest_port;
1348         } else {
1349                 sat.sat_addr.s_net  = sk->protinfo.af_at.src_net;
1350                 sat.sat_addr.s_node = sk->protinfo.af_at.src_node;
1351                 sat.sat_port = sk->protinfo.af_at.src_port;
1352         }
1353
1354         sat.sat_family = AF_APPLETALK;
1355         memcpy(uaddr, &sat, sizeof(sat));
1356         return 0;
1357 }
1358
1359 /*
1360  * Receive a packet (in skb) from device dev. This has come from the SNAP
1361  * decoder, and on entry skb->h.raw is the DDP header, skb->len is the DDP
1362  * header, skb->len is the DDP length. The physical headers have been
1363  * extracted. PPP should probably pass frames marked as for this layer.
1364  * [ie ARPHRD_ETHERTALK]
1365  */
1366 static int atalk_rcv(struct sk_buff *skb, struct net_device *dev,
1367                         struct packet_type *pt)
1368 {
1369         struct ddpehdr *ddp = (void *) skb->h.raw;
1370         struct sock *sock;
1371         struct atalk_iface *atif;
1372         struct sockaddr_at tosat;
1373         int origlen;
1374         struct ddpebits ddphv;
1375
1376         /* Size check */
1377         if (skb->len < sizeof(*ddp))
1378                 goto freeit;
1379
1380         /*
1381          *      Fix up the length field [Ok this is horrible but otherwise
1382          *      I end up with unions of bit fields and messy bit field order
1383          *      compiler/endian dependencies..]
1384          *
1385          *      FIXME: This is a write to a shared object. Granted it
1386          *      happens to be safe BUT.. (Its safe as user space will not
1387          *      run until we put it back)
1388          */
1389         *((__u16 *)&ddphv) = ntohs(*((__u16 *)ddp));
1390
1391         /* Trim buffer in case of stray trailing data */
1392         origlen = skb->len;
1393         skb_trim(skb, min_t(unsigned int, skb->len, ddphv.deh_len));
1394
1395         /*
1396          * Size check to see if ddp->deh_len was crap
1397          * (Otherwise we'll detonate most spectacularly
1398          * in the middle of recvmsg()).
1399          */
1400         if (skb->len < sizeof(*ddp))
1401                 goto freeit;
1402
1403         /*
1404          * Any checksums. Note we don't do htons() on this == is assumed to be
1405          * valid for net byte orders all over the networking code...
1406          */
1407         if (ddp->deh_sum &&
1408             atalk_checksum(ddp, ddphv.deh_len) != ddp->deh_sum)
1409                 /* Not a valid AppleTalk frame - dustbin time */
1410                 goto freeit;
1411
1412         /* Check the packet is aimed at us */
1413         if (!ddp->deh_dnet)     /* Net 0 is 'this network' */
1414                 atif = atalk_find_anynet(ddp->deh_dnode, dev);
1415         else
1416                 atif = atalk_find_interface(ddp->deh_dnet, ddp->deh_dnode);
1417
1418         /* Not ours, so we route the packet via the correct AppleTalk iface */
1419         if (!atif) {
1420                 struct atalk_route *rt;
1421                 struct at_addr ta;
1422
1423                 /*
1424                  * Don't route multicast, etc., packets, or packets
1425                  * sent to "this network" 
1426                  */
1427                 if (skb->pkt_type != PACKET_HOST || !ddp->deh_dnet) {
1428                         /* FIXME:
1429                          * Can it ever happen that a packet is from a PPP
1430                          * iface and needs to be broadcast onto the default
1431                          * network? */
1432                         if (dev->type == ARPHRD_PPP)
1433                                 printk(KERN_DEBUG "AppleTalk: didn't forward "
1434                                                   "broadcast packet received "
1435                                                   "from PPP iface\n");
1436                         goto freeit;
1437                 }
1438
1439                 ta.s_net  = ddp->deh_dnet;
1440                 ta.s_node = ddp->deh_dnode;
1441
1442                 /* Route the packet */
1443                 rt = atrtr_find(&ta);
1444                 if (!rt || ddphv.deh_hops == DDP_MAXHOPS)
1445                         goto freeit;
1446                 ddphv.deh_hops++;
1447
1448                 /*
1449                  * Route goes through another gateway, so
1450                  * set the target to the gateway instead.
1451                  */
1452                 if (rt->flags & RTF_GATEWAY) {
1453                         ta.s_net  = rt->gateway.s_net;
1454                         ta.s_node = rt->gateway.s_node;
1455                 }
1456
1457                 /* Fix up skb->len field */
1458                 skb_trim(skb, min_t(unsigned int, origlen, rt->dev->hard_header_len +
1459                         ddp_dl->header_length + ddphv.deh_len));
1460
1461                 /* Mend the byte order */
1462                 *((__u16 *)ddp) = ntohs(*((__u16 *)&ddphv));
1463
1464                 /*
1465                  * Send the buffer onwards
1466                  *
1467                  * Now we must always be careful. If it's come from 
1468                  * LocalTalk to EtherTalk it might not fit
1469                  *
1470                  * Order matters here: If a packet has to be copied
1471                  * to make a new headroom (rare hopefully) then it
1472                  * won't need unsharing.
1473                  *
1474                  * Note. ddp-> becomes invalid at the realloc.
1475                  */
1476                 if (skb_headroom(skb) < 22) {
1477                         /* 22 bytes - 12 ether, 2 len, 3 802.2 5 snap */
1478                         struct sk_buff *nskb = skb_realloc_headroom(skb, 32);
1479                         kfree_skb(skb);
1480                         if (!nskb) 
1481                                 goto out;
1482                         skb = nskb;
1483                 } else
1484                         skb = skb_unshare(skb, GFP_ATOMIC);
1485                 
1486                 /*
1487                  * If the buffer didn't vanish into the lack of
1488                  * space bitbucket we can send it.
1489                  */
1490                 if (skb && aarp_send_ddp(rt->dev, skb, &ta, NULL) == -1)
1491                         goto freeit;
1492                 goto out;
1493         }
1494
1495 #if defined(CONFIG_IPDDP) || defined(CONFIG_IPDDP_MODULE)
1496         /* Check if IP-over-DDP */
1497         if (skb->data[12] == 22) {
1498                 struct net_device *dev = __dev_get_by_name("ipddp0");
1499                 struct net_device_stats *stats;
1500
1501                 /* This needs to be able to handle ipddp"N" devices */
1502                 if (!dev)
1503                         return -ENODEV;
1504
1505                 skb->protocol = htons(ETH_P_IP);
1506                 skb_pull(skb, 13);
1507                 skb->dev = dev;
1508                 skb->h.raw = skb->data;
1509
1510                 stats = dev->priv;
1511                 stats->rx_packets++;
1512                 stats->rx_bytes += skb->len + 13;
1513                 netif_rx(skb);  /* Send the SKB up to a higher place. */
1514                 goto out;
1515         }
1516 #endif
1517         /*
1518          * Which socket - atalk_search_socket() looks for a *full match*
1519          * of the <net,node,port> tuple.
1520          */
1521         tosat.sat_addr.s_net  = ddp->deh_dnet;
1522         tosat.sat_addr.s_node = ddp->deh_dnode;
1523         tosat.sat_port = ddp->deh_dport;
1524
1525         sock = atalk_search_socket(&tosat, atif);
1526         if (!sock) /* But not one of our sockets */
1527                 goto freeit;
1528
1529         /* Queue packet (standard) */
1530         skb->sk = sock;
1531
1532         if (sock_queue_rcv_skb(sock, skb) < 0)
1533                 goto freeit;
1534         goto out;
1535 freeit: kfree_skb(skb);
1536 out:    return 0;
1537 }
1538
1539 /*
1540  * Receive a LocalTalk frame. We make some demands on the caller here.
1541  * Caller must provide enough headroom on the packet to pull the short
1542  * header and append a long one.
1543  */
1544 static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev,
1545                         struct packet_type *pt)
1546 {
1547         struct ddpehdr *ddp;
1548         struct at_addr *ap;
1549
1550         /* Expand any short form frames */
1551         if (skb->mac.raw[2] == 1) {
1552                 /* Find our address */
1553
1554                 ap = atalk_find_dev_addr(dev);
1555                 if (!ap || skb->len < sizeof(struct ddpshdr)) {
1556                         kfree_skb(skb);
1557                         return 0;
1558                 }
1559
1560                 /*
1561                  * The push leaves us with a ddephdr not an shdr, and
1562                  * handily the port bytes in the right place preset.
1563                  */
1564
1565                 skb_push(skb, sizeof(*ddp) - 4);
1566                 ddp = (struct ddpehdr *)skb->data;
1567
1568                 /* Now fill in the long header */
1569
1570                 /*
1571                  * These two first. The mac overlays the new source/dest
1572                  * network information so we MUST copy these before
1573                  * we write the network numbers !
1574                  */
1575
1576                 ddp->deh_dnode = skb->mac.raw[0];     /* From physical header */
1577                 ddp->deh_snode = skb->mac.raw[1];     /* From physical header */
1578
1579                 ddp->deh_dnet  = ap->s_net;     /* Network number */
1580                 ddp->deh_snet  = ap->s_net;
1581                 ddp->deh_sum   = 0;             /* No checksum */
1582                 /*
1583                  * Not sure about this bit...
1584                  */
1585                 ddp->deh_len   = skb->len;
1586                 ddp->deh_hops  = DDP_MAXHOPS;   /* Non routable, so force a drop
1587                                                    if we slip up later */
1588                 /* Mend the byte order */
1589                 *((__u16 *)ddp) = htons(*((__u16 *)ddp));
1590         }
1591         skb->h.raw = skb->data;
1592
1593         return atalk_rcv(skb, dev, pt);
1594 }
1595
1596 static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, int len,
1597                                 struct scm_cookie *scm)
1598 {
1599         struct sock *sk = sock->sk;
1600         struct sockaddr_at *usat = (struct sockaddr_at *)msg->msg_name;
1601         int flags = msg->msg_flags;
1602         int loopback = 0;
1603         struct sockaddr_at local_satalk, gsat;
1604         struct sk_buff *skb;
1605         struct net_device *dev;
1606         struct ddpehdr *ddp;
1607         int size;
1608         struct atalk_route *rt;
1609         int err;
1610
1611         if (flags & ~MSG_DONTWAIT)
1612                 return -EINVAL;
1613
1614         if (len > DDP_MAXSZ)
1615                 return -EMSGSIZE;
1616
1617         if (usat) {
1618                 if (sk->zapped)
1619                         if (atalk_autobind(sk) < 0)
1620                                 return -EBUSY;
1621
1622                 if (msg->msg_namelen < sizeof(*usat) ||
1623                     usat->sat_family != AF_APPLETALK)
1624                         return -EINVAL;
1625
1626                 /* netatalk doesn't implement this check */
1627                 if (usat->sat_addr.s_node == ATADDR_BCAST && !sk->broadcast) {
1628                         printk(KERN_INFO "SO_BROADCAST: Fix your netatalk as "
1629                                          "it will break before 2.2\n");
1630 #if 0
1631                         return -EPERM;
1632 #endif
1633                 }
1634         } else {
1635                 if (sk->state != TCP_ESTABLISHED)
1636                         return -ENOTCONN;
1637                 usat = &local_satalk;
1638                 usat->sat_family = AF_APPLETALK;
1639                 usat->sat_port   = sk->protinfo.af_at.dest_port;
1640                 usat->sat_addr.s_node = sk->protinfo.af_at.dest_node;
1641                 usat->sat_addr.s_net  = sk->protinfo.af_at.dest_net;
1642         }
1643
1644         /* Build a packet */
1645         SOCK_DEBUG(sk, "SK %p: Got address.\n", sk);
1646
1647         /* For headers */
1648         size = sizeof(struct ddpehdr) + len + ddp_dl->header_length;
1649
1650         if (usat->sat_addr.s_net || usat->sat_addr.s_node == ATADDR_ANYNODE) {
1651                 rt = atrtr_find(&usat->sat_addr);
1652                 if (!rt)
1653                         return -ENETUNREACH;
1654
1655                 dev = rt->dev;
1656         } else {
1657                 struct at_addr at_hint;
1658
1659                 at_hint.s_node = 0;
1660                 at_hint.s_net  = sk->protinfo.af_at.src_net;
1661
1662                 rt = atrtr_find(&at_hint);
1663                 if (!rt)
1664                         return -ENETUNREACH;
1665
1666                 dev = rt->dev;
1667         }
1668
1669         SOCK_DEBUG(sk, "SK %p: Size needed %d, device %s\n",
1670                         sk, size, dev->name);
1671
1672         size += dev->hard_header_len;
1673         skb = sock_alloc_send_skb(sk, size, (flags & MSG_DONTWAIT), &err);
1674         if (!skb)
1675                 return err;
1676         
1677         skb->sk = sk;
1678         skb_reserve(skb, ddp_dl->header_length);
1679         skb_reserve(skb, dev->hard_header_len);
1680         skb->dev = dev;
1681
1682         SOCK_DEBUG(sk, "SK %p: Begin build.\n", sk);
1683
1684         ddp = (struct ddpehdr *)skb_put(skb, sizeof(struct ddpehdr));
1685         ddp->deh_pad  = 0;
1686         ddp->deh_hops = 0;
1687         ddp->deh_len  = len + sizeof(*ddp);
1688         /*
1689          * Fix up the length field [Ok this is horrible but otherwise
1690          * I end up with unions of bit fields and messy bit field order
1691          * compiler/endian dependencies..
1692          */
1693         *((__u16 *)ddp) = ntohs(*((__u16 *)ddp));
1694
1695         ddp->deh_dnet  = usat->sat_addr.s_net;
1696         ddp->deh_snet  = sk->protinfo.af_at.src_net;
1697         ddp->deh_dnode = usat->sat_addr.s_node;
1698         ddp->deh_snode = sk->protinfo.af_at.src_node;
1699         ddp->deh_dport = usat->sat_port;
1700         ddp->deh_sport = sk->protinfo.af_at.src_port;
1701
1702         SOCK_DEBUG(sk, "SK %p: Copy user data (%d bytes).\n", sk, len);
1703
1704         err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1705         if (err) {
1706                 kfree_skb(skb);
1707                 return -EFAULT;
1708         }
1709
1710         if (sk->no_check == 1)
1711                 ddp->deh_sum = 0;
1712         else
1713                 ddp->deh_sum = atalk_checksum(ddp, len + sizeof(*ddp));
1714
1715         /*
1716          * Loopback broadcast packets to non gateway targets (ie routes
1717          * to group we are in)
1718          */
1719         if (ddp->deh_dnode == ATADDR_BCAST &&
1720             !(rt->flags & RTF_GATEWAY) && !(dev->flags & IFF_LOOPBACK)) {
1721                 struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL);
1722
1723                 if (skb2) {
1724                         loopback = 1;
1725                         SOCK_DEBUG(sk, "SK %p: send out(copy).\n", sk);
1726                         if (aarp_send_ddp(dev, skb2,
1727                                           &usat->sat_addr, NULL) == -1)
1728                                 kfree_skb(skb2);
1729                                 /* else queued/sent above in the aarp queue */
1730                 }
1731         }
1732
1733         if (dev->flags & IFF_LOOPBACK || loopback) {
1734                 SOCK_DEBUG(sk, "SK %p: Loop back.\n", sk);
1735                 /* loop back */
1736                 skb_orphan(skb);
1737                 ddp_dl->datalink_header(ddp_dl, skb, dev->dev_addr);
1738                 skb->mac.raw = skb->data;
1739                 skb->h.raw   = skb->data + ddp_dl->header_length +
1740                                 dev->hard_header_len;
1741                 skb_pull(skb, dev->hard_header_len);
1742                 skb_pull(skb, ddp_dl->header_length);
1743                 atalk_rcv(skb, dev, NULL);
1744         } else {
1745                 SOCK_DEBUG(sk, "SK %p: send out.\n", sk);
1746                 if (rt->flags & RTF_GATEWAY) {
1747                     gsat.sat_addr = rt->gateway;
1748                     usat = &gsat;
1749                 }
1750
1751                 if (aarp_send_ddp(dev, skb, &usat->sat_addr, NULL) == -1)
1752                         kfree_skb(skb);
1753                 /* else queued/sent above in the aarp queue */
1754         }
1755         SOCK_DEBUG(sk, "SK %p: Done write (%d).\n", sk, len);
1756
1757         return len;
1758 }
1759
1760 static int atalk_recvmsg(struct socket *sock, struct msghdr *msg, int size,
1761                          int flags, struct scm_cookie *scm)
1762 {
1763         struct sock *sk = sock->sk;
1764         struct sockaddr_at *sat = (struct sockaddr_at *)msg->msg_name;
1765         struct ddpehdr *ddp = NULL;
1766         int copied = 0;
1767         int err = 0;
1768         struct ddpebits ddphv;
1769         struct sk_buff *skb;
1770
1771         skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1772                                 flags & MSG_DONTWAIT, &err);
1773         if (!skb)
1774                 return err;
1775
1776         ddp = (struct ddpehdr *)(skb->h.raw);
1777         *((__u16 *)&ddphv) = ntohs(*((__u16 *)ddp));
1778
1779         if (sk->type == SOCK_RAW) {
1780                 copied = ddphv.deh_len;
1781                 if (copied > size) {
1782                         copied = size;
1783                         msg->msg_flags |= MSG_TRUNC;
1784                 }
1785
1786                 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1787         } else {
1788                 copied = ddphv.deh_len - sizeof(*ddp);
1789                 if (copied > size) {
1790                         copied = size;
1791                         msg->msg_flags |= MSG_TRUNC;
1792                 }
1793                 err = skb_copy_datagram_iovec(skb, sizeof(*ddp),
1794                                                 msg->msg_iov, copied);
1795         }
1796
1797         if (!err) {
1798                 if (sat) {
1799                         sat->sat_family      = AF_APPLETALK;
1800                         sat->sat_port        = ddp->deh_sport;
1801                         sat->sat_addr.s_node = ddp->deh_snode;
1802                         sat->sat_addr.s_net  = ddp->deh_snet;
1803                 }
1804                 msg->msg_namelen = sizeof(*sat);
1805         }
1806
1807         skb_free_datagram(sk, skb);     /* Free the datagram. */
1808         return err ? err : copied;
1809 }
1810
1811
1812 /*
1813  * AppleTalk ioctl calls.
1814  */
1815 static int atalk_ioctl(struct socket *sock,unsigned int cmd, unsigned long arg)
1816 {
1817         long amount = 0;
1818         struct sock *sk = sock->sk;
1819
1820         switch (cmd) {
1821                 /* Protocol layer */
1822                 case TIOCOUTQ:
1823                         amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1824                         if (amount < 0)
1825                                 amount = 0;
1826                         break;
1827                 case TIOCINQ:
1828                 {
1829                         /* These two are safe on a single CPU system as only
1830                          * user tasks fiddle here */
1831                         struct sk_buff *skb = skb_peek(&sk->receive_queue);
1832
1833                         if (skb)
1834                                 amount = skb->len-sizeof(struct ddpehdr);
1835                         break;
1836                 }
1837                 case SIOCGSTAMP:
1838                         if (!sk)
1839                                 return -EINVAL;
1840                         if (!sk->stamp.tv_sec)
1841                                 return -ENOENT;
1842                         return copy_to_user((void *)arg, &sk->stamp,
1843                                         sizeof(struct timeval)) ? -EFAULT : 0;
1844                 /* Routing */
1845                 case SIOCADDRT:
1846                 case SIOCDELRT:
1847                         if (!capable(CAP_NET_ADMIN))
1848                                 return -EPERM;
1849                         return atrtr_ioctl(cmd, (void *)arg);
1850                 /* Interface */
1851                 case SIOCGIFADDR:
1852                 case SIOCSIFADDR:
1853                 case SIOCGIFBRDADDR:
1854                 case SIOCATALKDIFADDR:
1855                 case SIOCDIFADDR:
1856                 case SIOCSARP:  /* proxy AARP */
1857                 case SIOCDARP:  /* proxy AARP */
1858                 {
1859                         int ret;
1860
1861                         rtnl_lock();
1862                         ret = atif_ioctl(cmd, (void *)arg);
1863                         rtnl_unlock();
1864
1865                         return ret;
1866                 }
1867                 /* Physical layer ioctl calls */
1868                 case SIOCSIFLINK:
1869                 case SIOCGIFHWADDR:
1870                 case SIOCSIFHWADDR:
1871                 case SIOCGIFFLAGS:
1872                 case SIOCSIFFLAGS:
1873                 case SIOCGIFMTU:
1874                 case SIOCGIFCONF:
1875                 case SIOCADDMULTI:
1876                 case SIOCDELMULTI:
1877                 case SIOCGIFCOUNT:
1878                 case SIOCGIFINDEX:
1879                 case SIOCGIFNAME:
1880                         return dev_ioctl(cmd,(void *) arg);
1881                 case SIOCSIFMETRIC:
1882                 case SIOCSIFBRDADDR:
1883                 case SIOCGIFNETMASK:
1884                 case SIOCSIFNETMASK:
1885                 case SIOCGIFMEM:
1886                 case SIOCSIFMEM:
1887                 case SIOCGIFDSTADDR:
1888                 case SIOCSIFDSTADDR:
1889                         return -EINVAL;
1890                 default:
1891                         return -EINVAL;
1892         }
1893
1894         return put_user(amount, (int *)arg);
1895 }
1896
1897 static struct net_proto_family atalk_family_ops =
1898 {
1899         PF_APPLETALK,
1900         atalk_create
1901 };
1902
1903 static struct proto_ops SOCKOPS_WRAPPED(atalk_dgram_ops)=
1904 {
1905         family:         PF_APPLETALK,
1906
1907         release:        atalk_release,
1908         bind:           atalk_bind,
1909         connect:        atalk_connect,
1910         socketpair:     sock_no_socketpair,
1911         accept:         sock_no_accept,
1912         getname:        atalk_getname,
1913         poll:           datagram_poll,
1914         ioctl:          atalk_ioctl,
1915         listen:         sock_no_listen,
1916         shutdown:       sock_no_shutdown,
1917         setsockopt:     sock_no_setsockopt,
1918         getsockopt:     sock_no_getsockopt,
1919         sendmsg:        atalk_sendmsg,
1920         recvmsg:        atalk_recvmsg,
1921         mmap:           sock_no_mmap,
1922         sendpage:       sock_no_sendpage,
1923 };
1924
1925 #include <linux/smp_lock.h>
1926 SOCKOPS_WRAP(atalk_dgram, PF_APPLETALK);
1927
1928 static struct notifier_block ddp_notifier=
1929 {
1930         ddp_device_event,
1931         NULL,
1932         0
1933 };
1934
1935 struct packet_type ltalk_packet_type=
1936 {
1937         0,
1938         NULL,
1939         ltalk_rcv,
1940         NULL,
1941         NULL
1942 };
1943
1944 struct packet_type ppptalk_packet_type=
1945 {
1946         0,
1947         NULL,
1948         atalk_rcv,
1949         NULL,
1950         NULL
1951 };
1952
1953 static char ddp_snap_id[] = {0x08, 0x00, 0x07, 0x80, 0x9B};
1954
1955 /* Export symbols for use by drivers when AppleTalk is a module */
1956 EXPORT_SYMBOL(aarp_send_ddp);
1957 EXPORT_SYMBOL(atrtr_get_dev);
1958 EXPORT_SYMBOL(atalk_find_dev_addr);
1959
1960 /* Called by proto.c on kernel start up */
1961 static int __init atalk_init(void)
1962 {
1963         (void) sock_register(&atalk_family_ops);
1964         ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv);
1965         if (!ddp_dl)
1966                 printk(KERN_CRIT "Unable to register DDP with SNAP.\n");
1967
1968         ltalk_packet_type.type = htons(ETH_P_LOCALTALK);
1969         dev_add_pack(&ltalk_packet_type);
1970
1971         ppptalk_packet_type.type = htons(ETH_P_PPPTALK);
1972         dev_add_pack(&ppptalk_packet_type);
1973
1974         register_netdevice_notifier(&ddp_notifier);
1975         aarp_proto_init();
1976
1977         proc_net_create("appletalk", 0, atalk_get_info);
1978         proc_net_create("atalk_route", 0, atalk_rt_get_info);
1979         proc_net_create("atalk_iface", 0, atalk_if_get_info);
1980 #ifdef CONFIG_PROC_FS
1981         aarp_register_proc_fs();
1982 #endif /* CONFIG_PROC_FS */
1983 #ifdef CONFIG_SYSCTL
1984         atalk_register_sysctl();
1985 #endif /* CONFIG_SYSCTL */
1986         printk(KERN_INFO "NET4: AppleTalk 0.18a for Linux NET4.0\n");
1987         return 0;
1988 }
1989 module_init(atalk_init);
1990
1991 #ifdef MODULE
1992 /*
1993  * Note on MOD_{INC,DEC}_USE_COUNT:
1994  *
1995  * Use counts are incremented/decremented when
1996  * sockets are created/deleted.
1997  *
1998  * AppleTalk interfaces are not incremented until atalkd is run
1999  * and are only decremented when they are downed.
2000  *
2001  * Ergo, before the AppleTalk module can be removed, all AppleTalk
2002  * sockets be closed from user space.
2003  */
2004 static void __exit atalk_exit(void)
2005 {
2006 #ifdef CONFIG_SYSCTL
2007         atalk_unregister_sysctl();
2008 #endif /* CONFIG_SYSCTL */
2009         proc_net_remove("appletalk");
2010         proc_net_remove("atalk_route");
2011         proc_net_remove("atalk_iface");
2012 #ifdef CONFIG_PROC_FS
2013         aarp_unregister_proc_fs();
2014 #endif /* CONFIG_PROC_FS */
2015         aarp_cleanup_module();  /* General aarp clean-up. */
2016         unregister_netdevice_notifier(&ddp_notifier);
2017         dev_remove_pack(&ltalk_packet_type);
2018         dev_remove_pack(&ppptalk_packet_type);
2019         unregister_snap_client(ddp_snap_id);
2020         sock_unregister(PF_APPLETALK);
2021 }
2022 module_exit(atalk_exit);
2023 #endif  /* MODULE */
2024 #endif  /* CONFIG_ATALK || CONFIG_ATALK_MODULE */