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