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