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