]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/econet/af_econet.c
[NET]: Fix ipx/econet/appletalk/irda ioctl crashes
[mv-sheeva.git] / net / econet / af_econet.c
1 /*
2  *      An implementation of the Acorn Econet and AUN protocols.
3  *      Philip Blundell <philb@gnu.org>
4  *
5  *      This program is free software; you can redistribute it and/or
6  *      modify it under the terms of the GNU General Public License
7  *      as published by the Free Software Foundation; either version
8  *      2 of the License, or (at your option) any later version.
9  *
10  */
11
12 #include <linux/config.h>
13 #include <linux/module.h>
14
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/string.h>
19 #include <linux/mm.h>
20 #include <linux/socket.h>
21 #include <linux/sockios.h>
22 #include <linux/in.h>
23 #include <linux/errno.h>
24 #include <linux/interrupt.h>
25 #include <linux/if_ether.h>
26 #include <linux/netdevice.h>
27 #include <linux/inetdevice.h>
28 #include <linux/route.h>
29 #include <linux/inet.h>
30 #include <linux/etherdevice.h>
31 #include <linux/if_arp.h>
32 #include <linux/wireless.h>
33 #include <linux/skbuff.h>
34 #include <linux/udp.h>
35 #include <net/sock.h>
36 #include <net/inet_common.h>
37 #include <linux/stat.h>
38 #include <linux/init.h>
39 #include <linux/if_ec.h>
40 #include <net/udp.h>
41 #include <net/ip.h>
42 #include <linux/spinlock.h>
43 #include <linux/rcupdate.h>
44 #include <linux/bitops.h>
45
46 #include <asm/uaccess.h>
47 #include <asm/system.h>
48
49 static const struct proto_ops econet_ops;
50 static struct hlist_head econet_sklist;
51 static DEFINE_RWLOCK(econet_lock);
52
53 /* Since there are only 256 possible network numbers (or fewer, depends
54    how you count) it makes sense to use a simple lookup table. */
55 static struct net_device *net2dev_map[256];
56
57 #define EC_PORT_IP      0xd2
58
59 #ifdef CONFIG_ECONET_AUNUDP
60 static DEFINE_SPINLOCK(aun_queue_lock);
61 static struct socket *udpsock;
62 #define AUN_PORT        0x8000
63
64
65 struct aunhdr
66 {
67         unsigned char code;             /* AUN magic protocol byte */
68         unsigned char port;
69         unsigned char cb;
70         unsigned char pad;
71         unsigned long handle;
72 };
73
74 static unsigned long aun_seq;
75
76 /* Queue of packets waiting to be transmitted. */
77 static struct sk_buff_head aun_queue;
78 static struct timer_list ab_cleanup_timer;
79
80 #endif          /* CONFIG_ECONET_AUNUDP */
81
82 /* Per-packet information */
83 struct ec_cb
84 {
85         struct sockaddr_ec sec;
86         unsigned long cookie;           /* Supplied by user. */
87 #ifdef CONFIG_ECONET_AUNUDP
88         int done;
89         unsigned long seq;              /* Sequencing */
90         unsigned long timeout;          /* Timeout */
91         unsigned long start;            /* jiffies */
92 #endif
93 #ifdef CONFIG_ECONET_NATIVE
94         void (*sent)(struct sk_buff *, int result);
95 #endif
96 };
97
98 static void econet_remove_socket(struct hlist_head *list, struct sock *sk)
99 {
100         write_lock_bh(&econet_lock);
101         sk_del_node_init(sk);
102         write_unlock_bh(&econet_lock);
103 }
104
105 static void econet_insert_socket(struct hlist_head *list, struct sock *sk)
106 {
107         write_lock_bh(&econet_lock);
108         sk_add_node(sk, list);
109         write_unlock_bh(&econet_lock);
110 }
111
112 /*
113  *      Pull a packet from our receive queue and hand it to the user.
114  *      If necessary we block.
115  */
116
117 static int econet_recvmsg(struct kiocb *iocb, struct socket *sock,
118                           struct msghdr *msg, size_t len, int flags)
119 {
120         struct sock *sk = sock->sk;
121         struct sk_buff *skb;
122         size_t copied;
123         int err;
124
125         msg->msg_namelen = sizeof(struct sockaddr_ec);
126
127         /*
128          *      Call the generic datagram receiver. This handles all sorts
129          *      of horrible races and re-entrancy so we can forget about it
130          *      in the protocol layers.
131          *
132          *      Now it will return ENETDOWN, if device have just gone down,
133          *      but then it will block.
134          */
135
136         skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err);
137
138         /*
139          *      An error occurred so return it. Because skb_recv_datagram() 
140          *      handles the blocking we don't see and worry about blocking
141          *      retries.
142          */
143
144         if(skb==NULL)
145                 goto out;
146
147         /*
148          *      You lose any data beyond the buffer you gave. If it worries a
149          *      user program they can ask the device for its MTU anyway.
150          */
151
152         copied = skb->len;
153         if (copied > len)
154         {
155                 copied=len;
156                 msg->msg_flags|=MSG_TRUNC;
157         }
158
159         /* We can't use skb_copy_datagram here */
160         err = memcpy_toiovec(msg->msg_iov, skb->data, copied);
161         if (err)
162                 goto out_free;
163         skb_get_timestamp(skb, &sk->sk_stamp);
164
165         if (msg->msg_name)
166                 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
167
168         /*
169          *      Free or return the buffer as appropriate. Again this
170          *      hides all the races and re-entrancy issues from us.
171          */
172         err = copied;
173
174 out_free:
175         skb_free_datagram(sk, skb);
176 out:
177         return err;
178 }
179
180 /*
181  *      Bind an Econet socket.
182  */
183
184 static int econet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
185 {
186         struct sockaddr_ec *sec = (struct sockaddr_ec *)uaddr;
187         struct sock *sk=sock->sk;
188         struct econet_sock *eo = ec_sk(sk);
189         
190         /*
191          *      Check legality
192          */
193          
194         if (addr_len < sizeof(struct sockaddr_ec) ||
195             sec->sec_family != AF_ECONET)
196                 return -EINVAL;
197         
198         eo->cb      = sec->cb;
199         eo->port    = sec->port;
200         eo->station = sec->addr.station;
201         eo->net     = sec->addr.net;
202
203         return 0;
204 }
205
206 #if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE)
207 /*
208  *      Queue a transmit result for the user to be told about.
209  */
210
211 static void tx_result(struct sock *sk, unsigned long cookie, int result)
212 {
213         struct sk_buff *skb = alloc_skb(0, GFP_ATOMIC);
214         struct ec_cb *eb;
215         struct sockaddr_ec *sec;
216
217         if (skb == NULL)
218         {
219                 printk(KERN_DEBUG "ec: memory squeeze, transmit result dropped.\n");
220                 return;
221         }
222
223         eb = (struct ec_cb *)&skb->cb;
224         sec = (struct sockaddr_ec *)&eb->sec;
225         memset(sec, 0, sizeof(struct sockaddr_ec));
226         sec->cookie = cookie;
227         sec->type = ECTYPE_TRANSMIT_STATUS | result;
228         sec->sec_family = AF_ECONET;
229
230         if (sock_queue_rcv_skb(sk, skb) < 0)
231                 kfree_skb(skb);
232 }
233 #endif
234
235 #ifdef CONFIG_ECONET_NATIVE
236 /*
237  *      Called by the Econet hardware driver when a packet transmit
238  *      has completed.  Tell the user.
239  */
240
241 static void ec_tx_done(struct sk_buff *skb, int result)
242 {
243         struct ec_cb *eb = (struct ec_cb *)&skb->cb;
244         tx_result(skb->sk, eb->cookie, result);
245 }
246 #endif
247
248 /*
249  *      Send a packet.  We have to work out which device it's going out on
250  *      and hence whether to use real Econet or the UDP emulation.
251  */
252
253 static int econet_sendmsg(struct kiocb *iocb, struct socket *sock,
254                           struct msghdr *msg, size_t len)
255 {
256         struct sock *sk = sock->sk;
257         struct sockaddr_ec *saddr=(struct sockaddr_ec *)msg->msg_name;
258         struct net_device *dev;
259         struct ec_addr addr;
260         int err;
261         unsigned char port, cb;
262 #if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE)
263         struct sk_buff *skb;
264         struct ec_cb *eb;
265 #endif
266 #ifdef CONFIG_ECONET_AUNUDP
267         struct msghdr udpmsg;
268         struct iovec iov[msg->msg_iovlen+1];
269         struct aunhdr ah;
270         struct sockaddr_in udpdest;
271         __kernel_size_t size;
272         int i;
273         mm_segment_t oldfs;
274 #endif
275                 
276         /*
277          *      Check the flags. 
278          */
279
280         if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT)) 
281                 return -EINVAL;
282
283         /*
284          *      Get and verify the address. 
285          */
286          
287         if (saddr == NULL) {
288                 struct econet_sock *eo = ec_sk(sk);
289
290                 addr.station = eo->station;
291                 addr.net     = eo->net;
292                 port         = eo->port;
293                 cb           = eo->cb;
294         } else {
295                 if (msg->msg_namelen < sizeof(struct sockaddr_ec)) 
296                         return -EINVAL;
297                 addr.station = saddr->addr.station;
298                 addr.net = saddr->addr.net;
299                 port = saddr->port;
300                 cb = saddr->cb;
301         }
302
303         /* Look for a device with the right network number. */
304         dev = net2dev_map[addr.net];
305
306         /* If not directly reachable, use some default */
307         if (dev == NULL)
308         {
309                 dev = net2dev_map[0];
310                 /* No interfaces at all? */
311                 if (dev == NULL)
312                         return -ENETDOWN;
313         }
314
315         if (len + 15 > dev->mtu)
316                 return -EMSGSIZE;
317
318         if (dev->type == ARPHRD_ECONET)
319         {
320                 /* Real hardware Econet.  We're not worthy etc. */
321 #ifdef CONFIG_ECONET_NATIVE
322                 unsigned short proto = 0;
323
324                 dev_hold(dev);
325                 
326                 skb = sock_alloc_send_skb(sk, len+LL_RESERVED_SPACE(dev), 
327                                           msg->msg_flags & MSG_DONTWAIT, &err);
328                 if (skb==NULL)
329                         goto out_unlock;
330                 
331                 skb_reserve(skb, LL_RESERVED_SPACE(dev));
332                 skb->nh.raw = skb->data;
333                 
334                 eb = (struct ec_cb *)&skb->cb;
335                 
336                 /* BUG: saddr may be NULL */
337                 eb->cookie = saddr->cookie;
338                 eb->sec = *saddr;
339                 eb->sent = ec_tx_done;
340
341                 if (dev->hard_header) {
342                         int res;
343                         struct ec_framehdr *fh;
344                         err = -EINVAL;
345                         res = dev->hard_header(skb, dev, ntohs(proto), 
346                                                &addr, NULL, len);
347                         /* Poke in our control byte and
348                            port number.  Hack, hack.  */
349                         fh = (struct ec_framehdr *)(skb->data);
350                         fh->cb = cb;
351                         fh->port = port;
352                         if (sock->type != SOCK_DGRAM) {
353                                 skb->tail = skb->data;
354                                 skb->len = 0;
355                         } else if (res < 0)
356                                 goto out_free;
357                 }
358                 
359                 /* Copy the data. Returns -EFAULT on error */
360                 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
361                 skb->protocol = proto;
362                 skb->dev = dev;
363                 skb->priority = sk->sk_priority;
364                 if (err)
365                         goto out_free;
366                 
367                 err = -ENETDOWN;
368                 if (!(dev->flags & IFF_UP))
369                         goto out_free;
370                 
371                 /*
372                  *      Now send it
373                  */
374                 
375                 dev_queue_xmit(skb);
376                 dev_put(dev);
377                 return(len);
378
379         out_free:
380                 kfree_skb(skb);
381         out_unlock:
382                 if (dev)
383                         dev_put(dev);
384 #else
385                 err = -EPROTOTYPE;
386 #endif
387                 return err;
388         }
389
390 #ifdef CONFIG_ECONET_AUNUDP
391         /* AUN virtual Econet. */
392
393         if (udpsock == NULL)
394                 return -ENETDOWN;               /* No socket - can't send */
395         
396         /* Make up a UDP datagram and hand it off to some higher intellect. */
397
398         memset(&udpdest, 0, sizeof(udpdest));
399         udpdest.sin_family = AF_INET;
400         udpdest.sin_port = htons(AUN_PORT);
401
402         /* At the moment we use the stupid Acorn scheme of Econet address
403            y.x maps to IP a.b.c.x.  This should be replaced with something
404            more flexible and more aware of subnet masks.  */
405         {
406                 struct in_device *idev;
407                 unsigned long network = 0;
408
409                 rcu_read_lock();
410                 idev = __in_dev_get_rcu(dev);
411                 if (idev) {
412                         if (idev->ifa_list)
413                                 network = ntohl(idev->ifa_list->ifa_address) & 
414                                         0xffffff00;             /* !!! */
415                 }
416                 rcu_read_unlock();
417                 udpdest.sin_addr.s_addr = htonl(network | addr.station);
418         }
419
420         ah.port = port;
421         ah.cb = cb & 0x7f;
422         ah.code = 2;            /* magic */
423         ah.pad = 0;
424
425         /* tack our header on the front of the iovec */
426         size = sizeof(struct aunhdr);
427         /*
428          * XXX: that is b0rken.  We can't mix userland and kernel pointers
429          * in iovec, since on a lot of platforms copy_from_user() will
430          * *not* work with the kernel and userland ones at the same time,
431          * regardless of what we do with set_fs().  And we are talking about
432          * econet-over-ethernet here, so "it's only ARM anyway" doesn't
433          * apply.  Any suggestions on fixing that code?         -- AV
434          */
435         iov[0].iov_base = (void *)&ah;
436         iov[0].iov_len = size;
437         for (i = 0; i < msg->msg_iovlen; i++) {
438                 void __user *base = msg->msg_iov[i].iov_base;
439                 size_t len = msg->msg_iov[i].iov_len;
440                 /* Check it now since we switch to KERNEL_DS later. */
441                 if (!access_ok(VERIFY_READ, base, len))
442                         return -EFAULT;
443                 iov[i+1].iov_base = base;
444                 iov[i+1].iov_len = len;
445                 size += len;
446         }
447
448         /* Get a skbuff (no data, just holds our cb information) */
449         if ((skb = sock_alloc_send_skb(sk, 0, 
450                              msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
451                 return err;
452
453         eb = (struct ec_cb *)&skb->cb;
454
455         eb->cookie = saddr->cookie;
456         eb->timeout = (5*HZ);
457         eb->start = jiffies;
458         ah.handle = aun_seq;
459         eb->seq = (aun_seq++);
460         eb->sec = *saddr;
461
462         skb_queue_tail(&aun_queue, skb);
463
464         udpmsg.msg_name = (void *)&udpdest;
465         udpmsg.msg_namelen = sizeof(udpdest);
466         udpmsg.msg_iov = &iov[0];
467         udpmsg.msg_iovlen = msg->msg_iovlen + 1;
468         udpmsg.msg_control = NULL;
469         udpmsg.msg_controllen = 0;
470         udpmsg.msg_flags=0;
471
472         oldfs = get_fs(); set_fs(KERNEL_DS);    /* More privs :-) */
473         err = sock_sendmsg(udpsock, &udpmsg, size);
474         set_fs(oldfs);
475 #else
476         err = -EPROTOTYPE;
477 #endif
478         return err;
479 }
480
481 /*
482  *      Look up the address of a socket.
483  */
484
485 static int econet_getname(struct socket *sock, struct sockaddr *uaddr,
486                           int *uaddr_len, int peer)
487 {
488         struct sock *sk = sock->sk;
489         struct econet_sock *eo = ec_sk(sk);
490         struct sockaddr_ec *sec = (struct sockaddr_ec *)uaddr;
491
492         if (peer)
493                 return -EOPNOTSUPP;
494
495         sec->sec_family   = AF_ECONET;
496         sec->port         = eo->port;
497         sec->addr.station = eo->station;
498         sec->addr.net     = eo->net;
499
500         *uaddr_len = sizeof(*sec);
501         return 0;
502 }
503
504 static void econet_destroy_timer(unsigned long data)
505 {
506         struct sock *sk=(struct sock *)data;
507
508         if (!atomic_read(&sk->sk_wmem_alloc) &&
509             !atomic_read(&sk->sk_rmem_alloc)) {
510                 sk_free(sk);
511                 return;
512         }
513
514         sk->sk_timer.expires = jiffies + 10 * HZ;
515         add_timer(&sk->sk_timer);
516         printk(KERN_DEBUG "econet socket destroy delayed\n");
517 }
518
519 /*
520  *      Close an econet socket.
521  */
522
523 static int econet_release(struct socket *sock)
524 {
525         struct sock *sk = sock->sk;
526
527         if (!sk)
528                 return 0;
529
530         econet_remove_socket(&econet_sklist, sk);
531
532         /*
533          *      Now the socket is dead. No more input will appear.
534          */
535
536         sk->sk_state_change(sk);        /* It is useless. Just for sanity. */
537
538         sock->sk = NULL;
539         sk->sk_socket = NULL;
540         sock_set_flag(sk, SOCK_DEAD);
541
542         /* Purge queues */
543
544         skb_queue_purge(&sk->sk_receive_queue);
545
546         if (atomic_read(&sk->sk_rmem_alloc) ||
547             atomic_read(&sk->sk_wmem_alloc)) {
548                 sk->sk_timer.data     = (unsigned long)sk;
549                 sk->sk_timer.expires  = jiffies + HZ;
550                 sk->sk_timer.function = econet_destroy_timer;
551                 add_timer(&sk->sk_timer);
552                 return 0;
553         }
554
555         sk_free(sk);
556         return 0;
557 }
558
559 static struct proto econet_proto = {
560         .name     = "ECONET",
561         .owner    = THIS_MODULE,
562         .obj_size = sizeof(struct econet_sock),
563 };
564
565 /*
566  *      Create an Econet socket
567  */
568
569 static int econet_create(struct socket *sock, int protocol)
570 {
571         struct sock *sk;
572         struct econet_sock *eo;
573         int err;
574
575         /* Econet only provides datagram services. */
576         if (sock->type != SOCK_DGRAM)
577                 return -ESOCKTNOSUPPORT;
578
579         sock->state = SS_UNCONNECTED;
580
581         err = -ENOBUFS;
582         sk = sk_alloc(PF_ECONET, GFP_KERNEL, &econet_proto, 1);
583         if (sk == NULL)
584                 goto out;
585
586         sk->sk_reuse = 1;
587         sock->ops = &econet_ops;
588         sock_init_data(sock, sk);
589
590         eo = ec_sk(sk);
591         sock_reset_flag(sk, SOCK_ZAPPED);
592         sk->sk_family = PF_ECONET;
593         eo->num = protocol;
594
595         econet_insert_socket(&econet_sklist, sk);
596         return(0);
597 out:
598         return err;
599 }
600
601 /*
602  *      Handle Econet specific ioctls
603  */
604
605 static int ec_dev_ioctl(struct socket *sock, unsigned int cmd, void __user *arg)
606 {
607         struct ifreq ifr;
608         struct ec_device *edev;
609         struct net_device *dev;
610         struct sockaddr_ec *sec;
611
612         /*
613          *      Fetch the caller's info block into kernel space
614          */
615
616         if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
617                 return -EFAULT;
618
619         if ((dev = dev_get_by_name(ifr.ifr_name)) == NULL) 
620                 return -ENODEV;
621
622         sec = (struct sockaddr_ec *)&ifr.ifr_addr;
623
624         switch (cmd)
625         {
626         case SIOCSIFADDR:
627                 edev = dev->ec_ptr;
628                 if (edev == NULL)
629                 {
630                         /* Magic up a new one. */
631                         edev = kmalloc(sizeof(struct ec_device), GFP_KERNEL);
632                         if (edev == NULL) {
633                                 printk("af_ec: memory squeeze.\n");
634                                 dev_put(dev);
635                                 return -ENOMEM;
636                         }
637                         memset(edev, 0, sizeof(struct ec_device));
638                         dev->ec_ptr = edev;
639                 }
640                 else
641                         net2dev_map[edev->net] = NULL;
642                 edev->station = sec->addr.station;
643                 edev->net = sec->addr.net;
644                 net2dev_map[sec->addr.net] = dev;
645                 if (!net2dev_map[0])
646                         net2dev_map[0] = dev;
647                 dev_put(dev);
648                 return 0;
649
650         case SIOCGIFADDR:
651                 edev = dev->ec_ptr;
652                 if (edev == NULL)
653                 {
654                         dev_put(dev);
655                         return -ENODEV;
656                 }
657                 memset(sec, 0, sizeof(struct sockaddr_ec));
658                 sec->addr.station = edev->station;
659                 sec->addr.net = edev->net;
660                 sec->sec_family = AF_ECONET;
661                 dev_put(dev);
662                 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
663                         return -EFAULT;
664                 return 0;
665         }
666
667         dev_put(dev);
668         return -EINVAL;
669 }
670
671 /*
672  *      Handle generic ioctls
673  */
674
675 static int econet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
676 {
677         struct sock *sk = sock->sk;
678         void __user *argp = (void __user *)arg;
679
680         switch(cmd) {
681                 case SIOCGSTAMP:
682                         return sock_get_timestamp(sk, argp);
683
684                 case SIOCSIFADDR:
685                 case SIOCGIFADDR:
686                         return ec_dev_ioctl(sock, cmd, argp);
687                         break;
688
689                 default:
690                         return -ENOIOCTLCMD;
691         }
692         /*NOTREACHED*/
693         return 0;
694 }
695
696 #ifdef CONFIG_COMPAT
697 static int econet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
698 {
699         /*
700          * All ioctls provided by econet are standard.  There is one gotcha, sockaddr_ec
701          * differs between 32bit and 64bit.  Fortunately nobody in kernel uses portion
702          * of sockaddr which differs between 32bit and 64bit, so we do not need special
703          * handling.
704          */
705         return -ENOIOCTLCMD;
706 }
707 #endif
708
709 static struct net_proto_family econet_family_ops = {
710         .family =       PF_ECONET,
711         .create =       econet_create,
712         .owner  =       THIS_MODULE,
713 };
714
715 static const struct proto_ops SOCKOPS_WRAPPED(econet_ops) = {
716         .family =       PF_ECONET,
717         .owner =        THIS_MODULE,
718         .release =      econet_release,
719         .bind =         econet_bind,
720         .connect =      sock_no_connect,
721         .socketpair =   sock_no_socketpair,
722         .accept =       sock_no_accept,
723         .getname =      econet_getname, 
724         .poll =         datagram_poll,
725         .ioctl =        econet_ioctl,
726 #ifdef CONFIG_COMPAT
727         .compat_ioctl = econet_compat_ioctl,
728 #endif
729         .listen =       sock_no_listen,
730         .shutdown =     sock_no_shutdown,
731         .setsockopt =   sock_no_setsockopt,
732         .getsockopt =   sock_no_getsockopt,
733         .sendmsg =      econet_sendmsg,
734         .recvmsg =      econet_recvmsg,
735         .mmap =         sock_no_mmap,
736         .sendpage =     sock_no_sendpage,
737 };
738
739 #include <linux/smp_lock.h>
740 SOCKOPS_WRAP(econet, PF_ECONET);
741
742 #if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE)
743 /*
744  *      Find the listening socket, if any, for the given data.
745  */
746
747 static struct sock *ec_listening_socket(unsigned char port, unsigned char
748                                  station, unsigned char net)
749 {
750         struct sock *sk;
751         struct hlist_node *node;
752
753         sk_for_each(sk, node, &econet_sklist) {
754                 struct econet_sock *opt = ec_sk(sk);
755                 if ((opt->port == port || opt->port == 0) && 
756                     (opt->station == station || opt->station == 0) &&
757                     (opt->net == net || opt->net == 0))
758                         goto found;
759         }
760         sk = NULL;
761 found:
762         return sk;
763 }
764
765 /*
766  *      Queue a received packet for a socket.
767  */
768
769 static int ec_queue_packet(struct sock *sk, struct sk_buff *skb,
770                            unsigned char stn, unsigned char net,
771                            unsigned char cb, unsigned char port)
772 {
773         struct ec_cb *eb = (struct ec_cb *)&skb->cb;
774         struct sockaddr_ec *sec = (struct sockaddr_ec *)&eb->sec;
775
776         memset(sec, 0, sizeof(struct sockaddr_ec));
777         sec->sec_family = AF_ECONET;
778         sec->type = ECTYPE_PACKET_RECEIVED;
779         sec->port = port;
780         sec->cb = cb;
781         sec->addr.net = net;
782         sec->addr.station = stn;
783
784         return sock_queue_rcv_skb(sk, skb);
785 }
786 #endif
787
788 #ifdef CONFIG_ECONET_AUNUDP
789 /*
790  *      Send an AUN protocol response. 
791  */
792
793 static void aun_send_response(__u32 addr, unsigned long seq, int code, int cb)
794 {
795         struct sockaddr_in sin = {
796                 .sin_family = AF_INET,
797                 .sin_port = htons(AUN_PORT),
798                 .sin_addr = {.s_addr = addr}
799         };
800         struct aunhdr ah = {.code = code, .cb = cb, .handle = seq};
801         struct kvec iov = {.iov_base = (void *)&ah, .iov_len = sizeof(ah)};
802         struct msghdr udpmsg;
803         
804         udpmsg.msg_name = (void *)&sin;
805         udpmsg.msg_namelen = sizeof(sin);
806         udpmsg.msg_control = NULL;
807         udpmsg.msg_controllen = 0;
808         udpmsg.msg_flags=0;
809
810         kernel_sendmsg(udpsock, &udpmsg, &iov, 1, sizeof(ah));
811 }
812
813
814 /*
815  *      Handle incoming AUN packets.  Work out if anybody wants them,
816  *      and send positive or negative acknowledgements as appropriate.
817  */
818
819 static void aun_incoming(struct sk_buff *skb, struct aunhdr *ah, size_t len)
820 {
821         struct iphdr *ip = skb->nh.iph;
822         unsigned char stn = ntohl(ip->saddr) & 0xff;
823         struct sock *sk;
824         struct sk_buff *newskb;
825         struct ec_device *edev = skb->dev->ec_ptr;
826
827         if (! edev)
828                 goto bad;
829
830         if ((sk = ec_listening_socket(ah->port, stn, edev->net)) == NULL)
831                 goto bad;               /* Nobody wants it */
832
833         newskb = alloc_skb((len - sizeof(struct aunhdr) + 15) & ~15, 
834                            GFP_ATOMIC);
835         if (newskb == NULL)
836         {
837                 printk(KERN_DEBUG "AUN: memory squeeze, dropping packet.\n");
838                 /* Send nack and hope sender tries again */
839                 goto bad;
840         }
841
842         memcpy(skb_put(newskb, len - sizeof(struct aunhdr)), (void *)(ah+1), 
843                len - sizeof(struct aunhdr));
844
845         if (ec_queue_packet(sk, newskb, stn, edev->net, ah->cb, ah->port))
846         {
847                 /* Socket is bankrupt. */
848                 kfree_skb(newskb);
849                 goto bad;
850         }
851
852         aun_send_response(ip->saddr, ah->handle, 3, 0);
853         return;
854
855 bad:
856         aun_send_response(ip->saddr, ah->handle, 4, 0);
857 }
858
859 /*
860  *      Handle incoming AUN transmit acknowledgements.  If the sequence
861  *      number matches something in our backlog then kill it and tell
862  *      the user.  If the remote took too long to reply then we may have
863  *      dropped the packet already.
864  */
865
866 static void aun_tx_ack(unsigned long seq, int result)
867 {
868         struct sk_buff *skb;
869         unsigned long flags;
870         struct ec_cb *eb;
871
872         spin_lock_irqsave(&aun_queue_lock, flags);
873         skb = skb_peek(&aun_queue);
874         while (skb && skb != (struct sk_buff *)&aun_queue)
875         {
876                 struct sk_buff *newskb = skb->next;
877                 eb = (struct ec_cb *)&skb->cb;
878                 if (eb->seq == seq)
879                         goto foundit;
880
881                 skb = newskb;
882         }
883         spin_unlock_irqrestore(&aun_queue_lock, flags);
884         printk(KERN_DEBUG "AUN: unknown sequence %ld\n", seq);
885         return;
886
887 foundit:
888         tx_result(skb->sk, eb->cookie, result);
889         skb_unlink(skb, &aun_queue);
890         spin_unlock_irqrestore(&aun_queue_lock, flags);
891         kfree_skb(skb);
892 }
893
894 /*
895  *      Deal with received AUN frames - sort out what type of thing it is
896  *      and hand it to the right function.
897  */
898
899 static void aun_data_available(struct sock *sk, int slen)
900 {
901         int err;
902         struct sk_buff *skb;
903         unsigned char *data;
904         struct aunhdr *ah;
905         struct iphdr *ip;
906         size_t len;
907
908         while ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) {
909                 if (err == -EAGAIN) {
910                         printk(KERN_ERR "AUN: no data available?!");
911                         return;
912                 }
913                 printk(KERN_DEBUG "AUN: recvfrom() error %d\n", -err);
914         }
915
916         data = skb->h.raw + sizeof(struct udphdr);
917         ah = (struct aunhdr *)data;
918         len = skb->len - sizeof(struct udphdr);
919         ip = skb->nh.iph;
920
921         switch (ah->code)
922         {
923         case 2:
924                 aun_incoming(skb, ah, len);
925                 break;
926         case 3:
927                 aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_OK);
928                 break;
929         case 4:
930                 aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_NOT_LISTENING);
931                 break;
932 #if 0
933                 /* This isn't quite right yet. */
934         case 5:
935                 aun_send_response(ip->saddr, ah->handle, 6, ah->cb);
936                 break;
937 #endif
938         default:
939                 printk(KERN_DEBUG "unknown AUN packet (type %d)\n", data[0]);
940         }
941
942         skb_free_datagram(sk, skb);
943 }
944
945 /*
946  *      Called by the timer to manage the AUN transmit queue.  If a packet
947  *      was sent to a dead or nonexistent host then we will never get an
948  *      acknowledgement back.  After a few seconds we need to spot this and
949  *      drop the packet.
950  */
951
952 static void ab_cleanup(unsigned long h)
953 {
954         struct sk_buff *skb;
955         unsigned long flags;
956
957         spin_lock_irqsave(&aun_queue_lock, flags);
958         skb = skb_peek(&aun_queue);
959         while (skb && skb != (struct sk_buff *)&aun_queue)
960         {
961                 struct sk_buff *newskb = skb->next;
962                 struct ec_cb *eb = (struct ec_cb *)&skb->cb;
963                 if ((jiffies - eb->start) > eb->timeout)
964                 {
965                         tx_result(skb->sk, eb->cookie, 
966                                   ECTYPE_TRANSMIT_NOT_PRESENT);
967                         skb_unlink(skb, &aun_queue);
968                         kfree_skb(skb);
969                 }
970                 skb = newskb;
971         }
972         spin_unlock_irqrestore(&aun_queue_lock, flags);
973
974         mod_timer(&ab_cleanup_timer, jiffies + (HZ*2));
975 }
976
977 static int __init aun_udp_initialise(void)
978 {
979         int error;
980         struct sockaddr_in sin;
981
982         skb_queue_head_init(&aun_queue);
983         spin_lock_init(&aun_queue_lock);
984         init_timer(&ab_cleanup_timer);
985         ab_cleanup_timer.expires = jiffies + (HZ*2);
986         ab_cleanup_timer.function = ab_cleanup;
987         add_timer(&ab_cleanup_timer);
988
989         memset(&sin, 0, sizeof(sin));
990         sin.sin_port = htons(AUN_PORT);
991
992         /* We can count ourselves lucky Acorn machines are too dim to
993            speak IPv6. :-) */
994         if ((error = sock_create_kern(PF_INET, SOCK_DGRAM, 0, &udpsock)) < 0)
995         {
996                 printk("AUN: socket error %d\n", -error);
997                 return error;
998         }
999         
1000         udpsock->sk->sk_reuse = 1;
1001         udpsock->sk->sk_allocation = GFP_ATOMIC; /* we're going to call it
1002                                                     from interrupts */
1003         
1004         error = udpsock->ops->bind(udpsock, (struct sockaddr *)&sin,
1005                                 sizeof(sin));
1006         if (error < 0)
1007         {
1008                 printk("AUN: bind error %d\n", -error);
1009                 goto release;
1010         }
1011
1012         udpsock->sk->sk_data_ready = aun_data_available;
1013
1014         return 0;
1015
1016 release:
1017         sock_release(udpsock);
1018         udpsock = NULL;
1019         return error;
1020 }
1021 #endif
1022
1023 #ifdef CONFIG_ECONET_NATIVE
1024
1025 /*
1026  *      Receive an Econet frame from a device.
1027  */
1028
1029 static int econet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
1030 {
1031         struct ec_framehdr *hdr;
1032         struct sock *sk;
1033         struct ec_device *edev = dev->ec_ptr;
1034
1035         if (skb->pkt_type == PACKET_OTHERHOST)
1036                 goto drop;
1037
1038         if (!edev)
1039                 goto drop;
1040
1041         if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
1042                 return NET_RX_DROP;
1043
1044         if (!pskb_may_pull(skb, sizeof(struct ec_framehdr)))
1045                 goto drop;
1046
1047         hdr = (struct ec_framehdr *) skb->data;
1048
1049         /* First check for encapsulated IP */
1050         if (hdr->port == EC_PORT_IP) {
1051                 skb->protocol = htons(ETH_P_IP);
1052                 skb_pull(skb, sizeof(struct ec_framehdr));
1053                 netif_rx(skb);
1054                 return 0;
1055         }
1056
1057         sk = ec_listening_socket(hdr->port, hdr->src_stn, hdr->src_net);
1058         if (!sk)
1059                 goto drop;
1060
1061         if (ec_queue_packet(sk, skb, edev->net, hdr->src_stn, hdr->cb,
1062                             hdr->port))
1063                 goto drop;
1064
1065         return 0;
1066
1067 drop:
1068         kfree_skb(skb);
1069         return NET_RX_DROP;
1070 }
1071
1072 static struct packet_type econet_packet_type = {
1073         .type =         __constant_htons(ETH_P_ECONET),
1074         .func =         econet_rcv,
1075 };
1076
1077 static void econet_hw_initialise(void)
1078 {
1079         dev_add_pack(&econet_packet_type);
1080 }
1081
1082 #endif
1083
1084 static int econet_notifier(struct notifier_block *this, unsigned long msg, void *data)
1085 {
1086         struct net_device *dev = (struct net_device *)data;
1087         struct ec_device *edev;
1088
1089         switch (msg) {
1090         case NETDEV_UNREGISTER:
1091                 /* A device has gone down - kill any data we hold for it. */
1092                 edev = dev->ec_ptr;
1093                 if (edev)
1094                 {
1095                         if (net2dev_map[0] == dev)
1096                                 net2dev_map[0] = NULL;
1097                         net2dev_map[edev->net] = NULL;
1098                         kfree(edev);
1099                         dev->ec_ptr = NULL;
1100                 }
1101                 break;
1102         }
1103
1104         return NOTIFY_DONE;
1105 }
1106
1107 static struct notifier_block econet_netdev_notifier = {
1108         .notifier_call =econet_notifier,
1109 };
1110
1111 static void __exit econet_proto_exit(void)
1112 {
1113 #ifdef CONFIG_ECONET_AUNUDP
1114         del_timer(&ab_cleanup_timer);
1115         if (udpsock)
1116                 sock_release(udpsock);
1117 #endif
1118         unregister_netdevice_notifier(&econet_netdev_notifier);
1119         sock_unregister(econet_family_ops.family);
1120         proto_unregister(&econet_proto);
1121 }
1122
1123 static int __init econet_proto_init(void)
1124 {
1125         int err = proto_register(&econet_proto, 0);
1126
1127         if (err != 0)
1128                 goto out;
1129         sock_register(&econet_family_ops);
1130 #ifdef CONFIG_ECONET_AUNUDP
1131         spin_lock_init(&aun_queue_lock);
1132         aun_udp_initialise();
1133 #endif
1134 #ifdef CONFIG_ECONET_NATIVE
1135         econet_hw_initialise();
1136 #endif
1137         register_netdevice_notifier(&econet_netdev_notifier);
1138 out:
1139         return err;
1140 }
1141
1142 module_init(econet_proto_init);
1143 module_exit(econet_proto_exit);
1144
1145 MODULE_LICENSE("GPL");
1146 MODULE_ALIAS_NETPROTO(PF_ECONET);