VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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                 .sin_family = AF_INET,
779                 .sin_port = htons(AUN_PORT),
780                 .sin_addr = {.s_addr = addr}
781         };
782         struct aunhdr ah = {.code = code, .cb = cb, .handle = seq};
783         struct kvec iov = {.iov_base = (void *)&ah, .iov_len = sizeof(ah)};
784         struct msghdr udpmsg;
785         
786         udpmsg.msg_name = (void *)&sin;
787         udpmsg.msg_namelen = sizeof(sin);
788         udpmsg.msg_control = NULL;
789         udpmsg.msg_controllen = 0;
790         udpmsg.msg_flags=0;
791
792         kernel_sendmsg(udpsock, &udpmsg, &iov, 1, sizeof(ah));
793 }
794
795
796 /*
797  *      Handle incoming AUN packets.  Work out if anybody wants them,
798  *      and send positive or negative acknowledgements as appropriate.
799  */
800
801 static void aun_incoming(struct sk_buff *skb, struct aunhdr *ah, size_t len)
802 {
803         struct iphdr *ip = skb->nh.iph;
804         unsigned char stn = ntohl(ip->saddr) & 0xff;
805         struct sock *sk;
806         struct sk_buff *newskb;
807         struct ec_device *edev = skb->dev->ec_ptr;
808
809         if (! edev)
810                 goto bad;
811
812         if ((sk = ec_listening_socket(ah->port, stn, edev->net)) == NULL)
813                 goto bad;               /* Nobody wants it */
814
815         newskb = alloc_skb((len - sizeof(struct aunhdr) + 15) & ~15, 
816                            GFP_ATOMIC);
817         if (newskb == NULL)
818         {
819                 printk(KERN_DEBUG "AUN: memory squeeze, dropping packet.\n");
820                 /* Send nack and hope sender tries again */
821                 goto bad;
822         }
823
824         memcpy(skb_put(newskb, len - sizeof(struct aunhdr)), (void *)(ah+1), 
825                len - sizeof(struct aunhdr));
826
827         if (ec_queue_packet(sk, newskb, stn, edev->net, ah->cb, ah->port))
828         {
829                 /* Socket is bankrupt. */
830                 kfree_skb(newskb);
831                 goto bad;
832         }
833
834         aun_send_response(ip->saddr, ah->handle, 3, 0);
835         return;
836
837 bad:
838         aun_send_response(ip->saddr, ah->handle, 4, 0);
839 }
840
841 /*
842  *      Handle incoming AUN transmit acknowledgements.  If the sequence
843  *      number matches something in our backlog then kill it and tell
844  *      the user.  If the remote took too long to reply then we may have
845  *      dropped the packet already.
846  */
847
848 static void aun_tx_ack(unsigned long seq, int result)
849 {
850         struct sk_buff *skb;
851         unsigned long flags;
852         struct ec_cb *eb;
853
854         spin_lock_irqsave(&aun_queue_lock, flags);
855         skb = skb_peek(&aun_queue);
856         while (skb && skb != (struct sk_buff *)&aun_queue)
857         {
858                 struct sk_buff *newskb = skb->next;
859                 eb = (struct ec_cb *)&skb->cb;
860                 if (eb->seq == seq)
861                         goto foundit;
862
863                 skb = newskb;
864         }
865         spin_unlock_irqrestore(&aun_queue_lock, flags);
866         printk(KERN_DEBUG "AUN: unknown sequence %ld\n", seq);
867         return;
868
869 foundit:
870         tx_result(skb->sk, eb->cookie, result);
871         skb_unlink(skb);
872         spin_unlock_irqrestore(&aun_queue_lock, flags);
873         kfree_skb(skb);
874 }
875
876 /*
877  *      Deal with received AUN frames - sort out what type of thing it is
878  *      and hand it to the right function.
879  */
880
881 static void aun_data_available(struct sock *sk, int slen)
882 {
883         int err;
884         struct sk_buff *skb;
885         unsigned char *data;
886         struct aunhdr *ah;
887         struct iphdr *ip;
888         size_t len;
889
890         while ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) {
891                 if (err == -EAGAIN) {
892                         printk(KERN_ERR "AUN: no data available?!");
893                         return;
894                 }
895                 printk(KERN_DEBUG "AUN: recvfrom() error %d\n", -err);
896         }
897
898         data = skb->h.raw + sizeof(struct udphdr);
899         ah = (struct aunhdr *)data;
900         len = skb->len - sizeof(struct udphdr);
901         ip = skb->nh.iph;
902
903         switch (ah->code)
904         {
905         case 2:
906                 aun_incoming(skb, ah, len);
907                 break;
908         case 3:
909                 aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_OK);
910                 break;
911         case 4:
912                 aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_NOT_LISTENING);
913                 break;
914 #if 0
915                 /* This isn't quite right yet. */
916         case 5:
917                 aun_send_response(ip->saddr, ah->handle, 6, ah->cb);
918                 break;
919 #endif
920         default:
921                 printk(KERN_DEBUG "unknown AUN packet (type %d)\n", data[0]);
922         }
923
924         skb_free_datagram(sk, skb);
925 }
926
927 /*
928  *      Called by the timer to manage the AUN transmit queue.  If a packet
929  *      was sent to a dead or nonexistent host then we will never get an
930  *      acknowledgement back.  After a few seconds we need to spot this and
931  *      drop the packet.
932  */
933
934 static void ab_cleanup(unsigned long h)
935 {
936         struct sk_buff *skb;
937         unsigned long flags;
938
939         spin_lock_irqsave(&aun_queue_lock, flags);
940         skb = skb_peek(&aun_queue);
941         while (skb && skb != (struct sk_buff *)&aun_queue)
942         {
943                 struct sk_buff *newskb = skb->next;
944                 struct ec_cb *eb = (struct ec_cb *)&skb->cb;
945                 if ((jiffies - eb->start) > eb->timeout)
946                 {
947                         tx_result(skb->sk, eb->cookie, 
948                                   ECTYPE_TRANSMIT_NOT_PRESENT);
949                         skb_unlink(skb);
950                         kfree_skb(skb);
951                 }
952                 skb = newskb;
953         }
954         spin_unlock_irqrestore(&aun_queue_lock, flags);
955
956         mod_timer(&ab_cleanup_timer, jiffies + (HZ*2));
957 }
958
959 static int __init aun_udp_initialise(void)
960 {
961         int error;
962         struct sockaddr_in sin;
963
964         skb_queue_head_init(&aun_queue);
965         spin_lock_init(&aun_queue_lock);
966         init_timer(&ab_cleanup_timer);
967         ab_cleanup_timer.expires = jiffies + (HZ*2);
968         ab_cleanup_timer.function = ab_cleanup;
969         add_timer(&ab_cleanup_timer);
970
971         memset(&sin, 0, sizeof(sin));
972         sin.sin_port = htons(AUN_PORT);
973
974         /* We can count ourselves lucky Acorn machines are too dim to
975            speak IPv6. :-) */
976         if ((error = sock_create_kern(PF_INET, SOCK_DGRAM, 0, &udpsock)) < 0)
977         {
978                 printk("AUN: socket error %d\n", -error);
979                 return error;
980         }
981         
982         udpsock->sk->sk_reuse = 1;
983         udpsock->sk->sk_allocation = GFP_ATOMIC; /* we're going to call it
984                                                     from interrupts */
985         
986         error = udpsock->ops->bind(udpsock, (struct sockaddr *)&sin,
987                                 sizeof(sin));
988         if (error < 0)
989         {
990                 printk("AUN: bind error %d\n", -error);
991                 goto release;
992         }
993
994         udpsock->sk->sk_data_ready = aun_data_available;
995
996         return 0;
997
998 release:
999         sock_release(udpsock);
1000         udpsock = NULL;
1001         return error;
1002 }
1003 #endif
1004
1005 #ifdef CONFIG_ECONET_NATIVE
1006
1007 /*
1008  *      Receive an Econet frame from a device.
1009  */
1010
1011 static int econet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
1012 {
1013         struct ec_framehdr *hdr;
1014         struct sock *sk;
1015         struct ec_device *edev = dev->ec_ptr;
1016
1017         if (skb->pkt_type == PACKET_OTHERHOST)
1018                 goto drop;
1019
1020         if (!edev)
1021                 goto drop;
1022
1023         if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
1024                 return NET_RX_DROP;
1025
1026         if (!pskb_may_pull(skb, sizeof(struct ec_framehdr)))
1027                 goto drop;
1028
1029         hdr = (struct ec_framehdr *) skb->data;
1030
1031         /* First check for encapsulated IP */
1032         if (hdr->port == EC_PORT_IP) {
1033                 skb->protocol = htons(ETH_P_IP);
1034                 skb_pull(skb, sizeof(struct ec_framehdr));
1035                 netif_rx(skb);
1036                 return 0;
1037         }
1038
1039         sk = ec_listening_socket(hdr->port, hdr->src_stn, hdr->src_net);
1040         if (!sk)
1041                 goto drop;
1042
1043         if (ec_queue_packet(sk, skb, edev->net, hdr->src_stn, hdr->cb,
1044                             hdr->port))
1045                 goto drop;
1046
1047         return 0;
1048
1049 drop:
1050         kfree_skb(skb);
1051         return NET_RX_DROP;
1052 }
1053
1054 static struct packet_type econet_packet_type = {
1055         .type =         __constant_htons(ETH_P_ECONET),
1056         .func =         econet_rcv,
1057 };
1058
1059 static void econet_hw_initialise(void)
1060 {
1061         dev_add_pack(&econet_packet_type);
1062 }
1063
1064 #endif
1065
1066 static int econet_notifier(struct notifier_block *this, unsigned long msg, void *data)
1067 {
1068         struct net_device *dev = (struct net_device *)data;
1069         struct ec_device *edev;
1070
1071         switch (msg) {
1072         case NETDEV_UNREGISTER:
1073                 /* A device has gone down - kill any data we hold for it. */
1074                 edev = dev->ec_ptr;
1075                 if (edev)
1076                 {
1077                         if (net2dev_map[0] == dev)
1078                                 net2dev_map[0] = NULL;
1079                         net2dev_map[edev->net] = NULL;
1080                         kfree(edev);
1081                         dev->ec_ptr = NULL;
1082                 }
1083                 break;
1084         }
1085
1086         return NOTIFY_DONE;
1087 }
1088
1089 static struct notifier_block econet_netdev_notifier = {
1090         .notifier_call =econet_notifier,
1091 };
1092
1093 static void __exit econet_proto_exit(void)
1094 {
1095 #ifdef CONFIG_ECONET_AUNUDP
1096         del_timer(&ab_cleanup_timer);
1097         if (udpsock)
1098                 sock_release(udpsock);
1099 #endif
1100         unregister_netdevice_notifier(&econet_netdev_notifier);
1101         sock_unregister(econet_family_ops.family);
1102 }
1103
1104 static int __init econet_proto_init(void)
1105 {
1106         sock_register(&econet_family_ops);
1107 #ifdef CONFIG_ECONET_AUNUDP
1108         spin_lock_init(&aun_queue_lock);
1109         aun_udp_initialise();
1110 #endif
1111 #ifdef CONFIG_ECONET_NATIVE
1112         econet_hw_initialise();
1113 #endif
1114         register_netdevice_notifier(&econet_netdev_notifier);
1115         return 0;
1116 }
1117
1118 module_init(econet_proto_init);
1119 module_exit(econet_proto_exit);
1120
1121 MODULE_LICENSE("GPL");
1122 MODULE_ALIAS_NETPROTO(PF_ECONET);