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