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