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