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