Import 2.3.18pre1
[davej-history.git] / net / econet / econet.c
blob6e7523eed6c390c2fb8351f2302534cbec0c98df
1 /*
2 * An implementation of the Acorn Econet and AUN protocols.
3 * Philip Blundell <philb@gnu.org>
5 * Fixes:
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
14 #include <linux/config.h>
15 #include <linux/module.h>
17 #include <asm/uaccess.h>
18 #include <asm/system.h>
19 #include <asm/bitops.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/string.h>
24 #include <linux/mm.h>
25 #include <linux/socket.h>
26 #include <linux/sockios.h>
27 #include <linux/in.h>
28 #include <linux/errno.h>
29 #include <linux/interrupt.h>
30 #include <linux/if_ether.h>
31 #include <linux/netdevice.h>
32 #include <linux/inetdevice.h>
33 #include <linux/route.h>
34 #include <linux/inet.h>
35 #include <linux/etherdevice.h>
36 #include <linux/if_arp.h>
37 #include <linux/wireless.h>
38 #include <linux/skbuff.h>
39 #include <net/sock.h>
40 #include <net/inet_common.h>
41 #include <linux/stat.h>
42 #include <linux/init.h>
43 #include <linux/if_ec.h>
44 #include <net/udp.h>
45 #include <net/ip.h>
46 #include <linux/spinlock.h>
48 static struct proto_ops econet_ops;
49 static struct sock *econet_sklist;
51 static spinlock_t aun_queue_lock;
53 #ifdef CONFIG_ECONET_AUNUDP
54 static struct socket *udpsock;
55 #define AUN_PORT 0x8000
57 struct aunhdr
59 unsigned char code; /* AUN magic protocol byte */
60 unsigned char port;
61 unsigned char cb;
62 unsigned char pad;
63 unsigned long handle;
66 static unsigned long aun_seq = 0;
68 /* Queue of packets waiting to be transmitted. */
69 static struct sk_buff_head aun_queue;
70 static struct timer_list ab_cleanup_timer;
72 #endif /* CONFIG_ECONET_AUNUDP */
74 /* Per-packet information */
75 struct ec_cb
77 struct sockaddr_ec sec;
78 unsigned long cookie; /* Supplied by user. */
79 #ifdef CONFIG_ECONET_AUNUDP
80 int done;
81 unsigned long seq; /* Sequencing */
82 unsigned long timeout; /* Timeout */
83 unsigned long start; /* jiffies */
84 #endif
85 #ifdef CONFIG_ECONET_NATIVE
86 void (*sent)(struct sk_buff *, int result);
87 #endif
90 struct ec_device
92 struct net_device *dev; /* Real device structure */
93 unsigned char station, net; /* Econet protocol address */
94 struct ec_device *prev, *next; /* Linked list */
97 static struct ec_device *edevlist = NULL;
99 static spinlock_t edevlist_lock;
102 * Faster version of edev_get - call with IRQs off
105 static __inline__ struct ec_device *__edev_get(struct net_device *dev)
107 struct ec_device *edev;
108 for (edev = edevlist; edev; edev = edev->next)
110 if (edev->dev == dev)
111 break;
113 return edev;
117 * Find an Econet device given its `dev' pointer. This is IRQ safe.
119 * Against what is it safe? --ANK
122 static struct ec_device *edev_get(struct net_device *dev)
124 struct ec_device *edev;
125 unsigned long flags;
126 spin_lock_irqsave(&edevlist_lock, flags);
127 edev = __edev_get(dev);
128 spin_unlock_irqrestore(&edevlist_lock, flags);
129 return edev;
133 * Pull a packet from our receive queue and hand it to the user.
134 * If necessary we block.
137 static int econet_recvmsg(struct socket *sock, struct msghdr *msg, int len,
138 int flags, struct scm_cookie *scm)
140 struct sock *sk = sock->sk;
141 struct sk_buff *skb;
142 int copied, err;
144 msg->msg_namelen = sizeof(struct sockaddr_ec);
147 * Call the generic datagram receiver. This handles all sorts
148 * of horrible races and re-entrancy so we can forget about it
149 * in the protocol layers.
151 * Now it will return ENETDOWN, if device have just gone down,
152 * but then it will block.
155 skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err);
158 * An error occurred so return it. Because skb_recv_datagram()
159 * handles the blocking we don't see and worry about blocking
160 * retries.
163 if(skb==NULL)
164 goto out;
167 * You lose any data beyond the buffer you gave. If it worries a
168 * user program they can ask the device for its MTU anyway.
171 copied = skb->len;
172 if (copied > len)
174 copied=len;
175 msg->msg_flags|=MSG_TRUNC;
178 /* We can't use skb_copy_datagram here */
179 err = memcpy_toiovec(msg->msg_iov, skb->data, copied);
180 if (err)
181 goto out_free;
182 sk->stamp=skb->stamp;
184 if (msg->msg_name)
185 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
188 * Free or return the buffer as appropriate. Again this
189 * hides all the races and re-entrancy issues from us.
191 err = copied;
193 out_free:
194 skb_free_datagram(sk, skb);
195 out:
196 return err;
200 * Bind an Econet socket.
203 static int econet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
205 struct sockaddr_ec *sec = (struct sockaddr_ec *)uaddr;
206 struct sock *sk=sock->sk;
209 * Check legality
212 if (addr_len < sizeof(struct sockaddr_ec))
213 return -EINVAL;
214 if (sec->sec_family != AF_ECONET)
215 return -EINVAL;
217 sk->protinfo.af_econet->cb = sec->cb;
218 sk->protinfo.af_econet->port = sec->port;
219 sk->protinfo.af_econet->station = sec->addr.station;
220 sk->protinfo.af_econet->net = sec->addr.net;
222 return 0;
226 * Queue a transmit result for the user to be told about.
229 static void tx_result(struct sock *sk, unsigned long cookie, int result)
231 struct sk_buff *skb = alloc_skb(0, GFP_ATOMIC);
232 struct ec_cb *eb;
233 struct sockaddr_ec *sec;
235 if (skb == NULL)
237 printk(KERN_DEBUG "ec: memory squeeze, transmit result dropped.\n");
238 return;
241 eb = (struct ec_cb *)&skb->cb;
242 sec = (struct sockaddr_ec *)&eb->sec;
243 memset(sec, 0, sizeof(struct sockaddr_ec));
244 sec->cookie = cookie;
245 sec->type = ECTYPE_TRANSMIT_STATUS | result;
246 sec->sec_family = AF_ECONET;
248 if (sock_queue_rcv_skb(sk, skb) < 0)
249 kfree_skb(skb);
252 #ifdef CONFIG_ECONET_NATIVE
254 * Called by the Econet hardware driver when a packet transmit
255 * has completed. Tell the user.
258 static void ec_tx_done(struct sk_buff *skb, int result)
260 struct ec_cb *eb = (struct ec_cb *)&skb->cb;
261 tx_result(skb->sk, eb->cookie, result);
263 #endif
266 * Send a packet. We have to work out which device it's going out on
267 * and hence whether to use real Econet or the UDP emulation.
270 static int econet_sendmsg(struct socket *sock, struct msghdr *msg, int len,
271 struct scm_cookie *scm)
273 struct sock *sk = sock->sk;
274 struct sockaddr_ec *saddr=(struct sockaddr_ec *)msg->msg_name;
275 struct net_device *dev;
276 struct ec_addr addr;
277 struct ec_device *edev;
278 int err;
279 unsigned char port, cb;
280 struct sk_buff *skb;
281 struct ec_cb *eb;
282 #ifdef CONFIG_ECONET_NATIVE
283 unsigned short proto = 0;
284 #endif
285 #ifdef CONFIG_ECONET_AUNUDP
286 struct msghdr udpmsg;
287 struct iovec iov[msg->msg_iovlen+1];
288 struct aunhdr ah;
289 struct sockaddr_in udpdest;
290 __kernel_size_t size;
291 int i;
292 mm_segment_t oldfs;
293 #endif
296 * Check the flags.
299 if (msg->msg_flags&~MSG_DONTWAIT)
300 return(-EINVAL);
303 * Get and verify the address.
306 if (saddr == NULL) {
307 addr.station = sk->protinfo.af_econet->station;
308 addr.net = sk->protinfo.af_econet->net;
309 port = sk->protinfo.af_econet->port;
310 cb = sk->protinfo.af_econet->cb;
311 } else {
312 if (msg->msg_namelen < sizeof(struct sockaddr_ec))
313 return -EINVAL;
314 addr.station = saddr->addr.station;
315 addr.net = saddr->addr.net;
316 port = saddr->port;
317 cb = saddr->cb;
320 /* Look for a device with the right network number. */
321 for (edev = edevlist; edev && (edev->net != addr.net);
322 edev = edev->next);
324 /* Bridge? What's that? */
325 if (edev == NULL)
326 return -ENETUNREACH;
328 dev = edev->dev;
330 if (dev->type == ARPHRD_ECONET)
332 /* Real hardware Econet. We're not worthy etc. */
333 #ifdef CONFIG_ECONET_NATIVE
334 atomic_inc(&dev->refcnt);
336 skb = sock_alloc_send_skb(sk, len+dev->hard_header_len+15, 0,
337 msg->msg_flags & MSG_DONTWAIT, &err);
338 if (skb==NULL)
339 goto out_unlock;
341 skb_reserve(skb, (dev->hard_header_len+15)&~15);
342 skb->nh.raw = skb->data;
344 eb = (struct ec_cb *)&skb->cb;
346 eb->cookie = saddr->cookie;
347 eb->sec = *saddr;
348 eb->sent = ec_tx_done;
350 if (dev->hard_header) {
351 int res;
352 err = -EINVAL;
353 res = dev->hard_header(skb, dev, ntohs(proto), &addr, NULL, len);
354 if (sock->type != SOCK_DGRAM) {
355 skb->tail = skb->data;
356 skb->len = 0;
357 } else if (res < 0)
358 goto out_free;
361 /* Copy the data. Returns -EFAULT on error */
362 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
363 skb->protocol = proto;
364 skb->dev = dev;
365 skb->priority = sk->priority;
366 if (err)
367 goto out_free;
369 err = -ENETDOWN;
370 if (!(dev->flags & IFF_UP))
371 goto out_free;
374 * Now send it
377 dev_queue_xmit(skb);
378 dev_put(dev);
379 return(len);
381 out_free:
382 kfree_skb(skb);
383 out_unlock:
384 if (dev)
385 dev_put(dev);
386 #else
387 err = -EPROTOTYPE;
388 #endif
389 return err;
392 #ifdef CONFIG_ECONET_AUNUDP
393 /* AUN virtual Econet. */
395 if (udpsock == NULL)
396 return -ENETDOWN; /* No socket - can't send */
398 /* Make up a UDP datagram and hand it off to some higher intellect. */
400 memset(&udpdest, 0, sizeof(udpdest));
401 udpdest.sin_family = AF_INET;
402 udpdest.sin_port = htons(AUN_PORT);
404 /* At the moment we use the stupid Acorn scheme of Econet address
405 y.x maps to IP a.b.c.x. This should be replaced with something
406 more flexible and more aware of subnet masks. */
408 struct in_device *idev = in_dev_get(dev);
409 unsigned long network = 0;
410 if (idev) {
411 read_lock(&idev->lock);
412 if (idev->ifa_list)
413 network = ntohl(idev->ifa_list->ifa_address) &
414 0xffffff00; /* !!! */
415 read_unlock(&idev->lock);
416 in_dev_put(idev);
418 udpdest.sin_addr.s_addr = htonl(network | addr.station);
421 ah.port = port;
422 ah.cb = cb & 0x7f;
423 ah.code = 2; /* magic */
424 ah.pad = 0;
426 /* tack our header on the front of the iovec */
427 size = sizeof(struct aunhdr);
428 iov[0].iov_base = (void *)&ah;
429 iov[0].iov_len = size;
430 for (i = 0; i < msg->msg_iovlen; i++) {
431 void *base = msg->msg_iov[i].iov_base;
432 size_t len = msg->msg_iov[i].iov_len;
433 /* Check it now since we switch to KERNEL_DS later. */
434 if ((err = verify_area(VERIFY_READ, base, len)) < 0)
435 return err;
436 iov[i+1].iov_base = base;
437 iov[i+1].iov_len = len;
438 size += len;
441 /* Get a skbuff (no data, just holds our cb information) */
442 if ((skb = sock_alloc_send_skb(sk, 0, 0,
443 msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
444 return err;
446 eb = (struct ec_cb *)&skb->cb;
448 eb->cookie = saddr->cookie;
449 eb->timeout = (5*HZ);
450 eb->start = jiffies;
451 ah.handle = aun_seq;
452 eb->seq = (aun_seq++);
453 eb->sec = *saddr;
455 skb_queue_tail(&aun_queue, skb);
457 udpmsg.msg_name = (void *)&udpdest;
458 udpmsg.msg_namelen = sizeof(udpdest);
459 udpmsg.msg_iov = &iov[0];
460 udpmsg.msg_iovlen = msg->msg_iovlen + 1;
461 udpmsg.msg_control = NULL;
462 udpmsg.msg_controllen = 0;
463 udpmsg.msg_flags=0;
465 oldfs = get_fs(); set_fs(KERNEL_DS); /* More privs :-) */
466 err = sock_sendmsg(udpsock, &udpmsg, size);
467 set_fs(oldfs);
468 #else
469 err = -EPROTOTYPE;
470 #endif
471 return err;
475 * Look up the address of a socket.
478 static int econet_getname(struct socket *sock, struct sockaddr *uaddr,
479 int *uaddr_len, int peer)
481 struct sock *sk = sock->sk;
482 struct sockaddr_ec *sec = (struct sockaddr_ec *)uaddr;
484 if (peer)
485 return -EOPNOTSUPP;
487 sec->sec_family = AF_ECONET;
488 sec->port = sk->protinfo.af_econet->port;
489 sec->addr.station = sk->protinfo.af_econet->station;
490 sec->addr.net = sk->protinfo.af_econet->net;
492 *uaddr_len = sizeof(*sec);
493 return 0;
496 static void econet_destroy_timer(unsigned long data)
498 struct sock *sk=(struct sock *)data;
500 if (!atomic_read(&sk->wmem_alloc) && !atomic_read(&sk->rmem_alloc)) {
501 sk_free(sk);
502 MOD_DEC_USE_COUNT;
503 return;
506 sk->timer.expires=jiffies+10*HZ;
507 add_timer(&sk->timer);
508 printk(KERN_DEBUG "econet socket destroy delayed\n");
512 * Close an econet socket.
515 static int econet_release(struct socket *sock)
517 struct sk_buff *skb;
518 struct sock *sk = sock->sk;
520 if (!sk)
521 return 0;
523 sklist_remove_socket(&econet_sklist, sk);
526 * Now the socket is dead. No more input will appear.
529 sk->state_change(sk); /* It is useless. Just for sanity. */
531 sock->sk = NULL;
532 sk->socket = NULL;
533 sk->dead = 1;
535 /* Purge queues */
537 while ((skb=skb_dequeue(&sk->receive_queue))!=NULL)
538 kfree_skb(skb);
540 if (atomic_read(&sk->rmem_alloc) || atomic_read(&sk->wmem_alloc)) {
541 sk->timer.data=(unsigned long)sk;
542 sk->timer.expires=jiffies+HZ;
543 sk->timer.function=econet_destroy_timer;
544 add_timer(&sk->timer);
545 return 0;
548 sk_free(sk);
549 MOD_DEC_USE_COUNT;
550 return 0;
554 * Create an Econet socket
557 static int econet_create(struct socket *sock, int protocol)
559 struct sock *sk;
560 int err;
562 /* Econet only provides datagram services. */
563 if (sock->type != SOCK_DGRAM)
564 return -ESOCKTNOSUPPORT;
566 sock->state = SS_UNCONNECTED;
567 MOD_INC_USE_COUNT;
569 err = -ENOBUFS;
570 sk = sk_alloc(PF_ECONET, GFP_KERNEL, 1);
571 if (sk == NULL)
572 goto out;
574 sk->reuse = 1;
575 sock->ops = &econet_ops;
576 sock_init_data(sock,sk);
578 sk->protinfo.af_econet = kmalloc(sizeof(struct econet_opt), GFP_KERNEL);
579 if (sk->protinfo.af_econet == NULL)
580 goto out_free;
581 memset(sk->protinfo.af_econet, 0, sizeof(struct econet_opt));
582 sk->zapped=0;
583 sk->family = PF_ECONET;
584 sk->num = protocol;
586 sklist_insert_socket(&econet_sklist, sk);
587 return(0);
589 out_free:
590 sk_free(sk);
591 out:
592 MOD_DEC_USE_COUNT;
593 return err;
597 * Handle Econet specific ioctls
600 static int ec_dev_ioctl(struct socket *sock, unsigned int cmd, void *arg)
602 struct ifreq ifr;
603 struct ec_device *edev;
604 struct net_device *dev;
605 unsigned long flags;
606 struct sockaddr_ec *sec;
609 * Fetch the caller's info block into kernel space
612 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
613 return -EFAULT;
615 if ((dev = dev_get_by_name(ifr.ifr_name)) == NULL)
616 return -ENODEV;
618 sec = (struct sockaddr_ec *)&ifr.ifr_addr;
620 switch (cmd)
622 case SIOCSIFADDR:
623 spin_lock_irqsave(&edevlist_lock, flags);
624 edev = __edev_get(dev);
625 if (edev == NULL)
627 /* Magic up a new one. */
628 printk("Get fascist grenade!!!\n");
629 *(int*)0 = 0;
630 /* Note to author: please, remove this spinlock.
631 You do not change edevlist from interrupts,
632 so that such aggressive protection is redundant.
634 BTW not all scans of edev_list are protected.
636 edev = kmalloc(GFP_KERNEL, sizeof(struct ec_device));
637 if (edev == NULL) {
638 printk("af_ec: memory squeeze.\n");
639 spin_unlock_irqrestore(&edevlist_lock, flags);
640 dev_put(dev);
641 return -ENOMEM;
643 memset(edev, 0, sizeof(struct ec_device));
644 edev->dev = dev;
645 edev->next = edevlist;
646 edevlist = edev;
648 edev->station = sec->addr.station;
649 edev->net = sec->addr.net;
650 spin_unlock_irqrestore(&edevlist_lock, flags);
651 dev_put(dev);
652 return 0;
654 case SIOCGIFADDR:
655 spin_lock_irqsave(&edevlist_lock, flags);
656 edev = __edev_get(dev);
657 if (edev == NULL)
659 spin_unlock_irqrestore(&edevlist_lock, flags);
660 dev_put(dev);
661 return -ENODEV;
663 memset(sec, 0, sizeof(struct sockaddr_ec));
664 sec->addr.station = edev->station;
665 sec->addr.net = edev->net;
666 sec->sec_family = AF_ECONET;
667 spin_unlock_irqrestore(&edevlist_lock, flags);
668 dev_put(dev);
669 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
670 return -EFAULT;
671 return 0;
674 dev_put(dev);
675 return -EINVAL;
679 * Handle generic ioctls
682 static int econet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
684 struct sock *sk = sock->sk;
685 int err;
686 int pid;
688 switch(cmd)
690 case FIOSETOWN:
691 case SIOCSPGRP:
692 err = get_user(pid, (int *) arg);
693 if (err)
694 return err;
695 if (current->pid != pid && current->pgrp != -pid && !suser())
696 return -EPERM;
697 sk->proc = pid;
698 return(0);
699 case FIOGETOWN:
700 case SIOCGPGRP:
701 return put_user(sk->proc, (int *)arg);
702 case SIOCGSTAMP:
703 if(sk->stamp.tv_sec==0)
704 return -ENOENT;
705 err = -EFAULT;
706 if (!copy_to_user((void *)arg, &sk->stamp, sizeof(struct timeval)))
707 err = 0;
708 return err;
709 case SIOCGIFFLAGS:
710 case SIOCSIFFLAGS:
711 case SIOCGIFCONF:
712 case SIOCGIFMETRIC:
713 case SIOCSIFMETRIC:
714 case SIOCGIFMEM:
715 case SIOCSIFMEM:
716 case SIOCGIFMTU:
717 case SIOCSIFMTU:
718 case SIOCSIFLINK:
719 case SIOCGIFHWADDR:
720 case SIOCSIFHWADDR:
721 case SIOCSIFMAP:
722 case SIOCGIFMAP:
723 case SIOCSIFSLAVE:
724 case SIOCGIFSLAVE:
725 case SIOCGIFINDEX:
726 case SIOCGIFNAME:
727 case SIOCGIFCOUNT:
728 case SIOCSIFHWBROADCAST:
729 return(dev_ioctl(cmd,(void *) arg));
731 case SIOCSIFADDR:
732 case SIOCGIFADDR:
733 return ec_dev_ioctl(sock, cmd, (void *)arg);
734 break;
736 default:
737 return(dev_ioctl(cmd,(void *) arg));
739 /*NOTREACHED*/
740 return 0;
743 static struct net_proto_family econet_family_ops = {
744 PF_ECONET,
745 econet_create
748 static struct proto_ops SOCKOPS_WRAPPED(econet_ops) = {
749 PF_ECONET,
751 econet_release,
752 econet_bind,
753 sock_no_connect,
754 sock_no_socketpair,
755 sock_no_accept,
756 econet_getname,
757 datagram_poll,
758 econet_ioctl,
759 sock_no_listen,
760 sock_no_shutdown,
761 sock_no_setsockopt,
762 sock_no_getsockopt,
763 sock_no_fcntl,
764 econet_sendmsg,
765 econet_recvmsg,
766 sock_no_mmap
769 #include <linux/smp_lock.h>
770 SOCKOPS_WRAP(econet, PF_ECONET);
773 * Find the listening socket, if any, for the given data.
776 static struct sock *ec_listening_socket(unsigned char port, unsigned char
777 station, unsigned char net)
779 struct sock *sk = econet_sklist;
781 while (sk)
783 struct econet_opt *opt = sk->protinfo.af_econet;
784 if ((opt->port == port || opt->port == 0) &&
785 (opt->station == station || opt->station == 0) &&
786 (opt->net == net || opt->net == 0))
787 return sk;
789 sk = sk->next;
792 return NULL;
795 #ifdef CONFIG_ECONET_AUNUDP
798 * Send an AUN protocol response.
801 static void aun_send_response(__u32 addr, unsigned long seq, int code, int cb)
803 struct sockaddr_in sin;
804 struct iovec iov;
805 struct aunhdr ah;
806 struct msghdr udpmsg;
807 int err;
808 mm_segment_t oldfs;
810 memset(&sin, 0, sizeof(sin));
811 sin.sin_family = AF_INET;
812 sin.sin_port = htons(AUN_PORT);
813 sin.sin_addr.s_addr = addr;
815 ah.code = code;
816 ah.pad = 0;
817 ah.port = 0;
818 ah.cb = cb;
819 ah.handle = seq;
821 iov.iov_base = (void *)&ah;
822 iov.iov_len = sizeof(ah);
824 udpmsg.msg_name = (void *)&sin;
825 udpmsg.msg_namelen = sizeof(sin);
826 udpmsg.msg_iov = &iov;
827 udpmsg.msg_iovlen = 1;
828 udpmsg.msg_control = NULL;
829 udpmsg.msg_controllen = 0;
830 udpmsg.msg_flags=0;
832 oldfs = get_fs(); set_fs(KERNEL_DS);
833 err = sock_sendmsg(udpsock, &udpmsg, sizeof(ah));
834 set_fs(oldfs);
838 * Handle incoming AUN packets. Work out if anybody wants them,
839 * and send positive or negative acknowledgements as appropriate.
842 static void aun_incoming(struct sk_buff *skb, struct aunhdr *ah, size_t len)
844 struct ec_device *edev = edev_get(skb->dev);
845 struct iphdr *ip = skb->nh.iph;
846 unsigned char stn = ntohl(ip->saddr) & 0xff;
847 struct sock *sk;
848 struct sk_buff *newskb;
849 struct ec_cb *eb;
850 struct sockaddr_ec *sec;
852 if (edev == NULL)
853 return; /* Device not configured for AUN */
855 if ((sk = ec_listening_socket(ah->port, stn, edev->net)) == NULL)
856 goto bad; /* Nobody wants it */
858 newskb = alloc_skb((len - sizeof(struct aunhdr) + 15) & ~15,
859 GFP_ATOMIC);
860 if (newskb == NULL)
862 printk(KERN_DEBUG "AUN: memory squeeze, dropping packet.\n");
863 /* Send nack and hope sender tries again */
864 goto bad;
867 eb = (struct ec_cb *)&newskb->cb;
868 sec = (struct sockaddr_ec *)&eb->sec;
869 memset(sec, 0, sizeof(struct sockaddr_ec));
870 sec->sec_family = AF_ECONET;
871 sec->type = ECTYPE_PACKET_RECEIVED;
872 sec->port = ah->port;
873 sec->cb = ah->cb;
874 sec->addr.net = edev->net;
875 sec->addr.station = stn;
877 memcpy(skb_put(newskb, len - sizeof(struct aunhdr)), (void *)(ah+1),
878 len - sizeof(struct aunhdr));
880 if (sock_queue_rcv_skb(sk, newskb) < 0)
882 /* Socket is bankrupt. */
883 kfree_skb(newskb);
884 goto bad;
887 aun_send_response(ip->saddr, ah->handle, 3, 0);
888 return;
890 bad:
891 aun_send_response(ip->saddr, ah->handle, 4, 0);
895 * Handle incoming AUN transmit acknowledgements. If the sequence
896 * number matches something in our backlog then kill it and tell
897 * the user. If the remote took too long to reply then we may have
898 * dropped the packet already.
901 static void aun_tx_ack(unsigned long seq, int result)
903 struct sk_buff *skb;
904 unsigned long flags;
905 struct ec_cb *eb;
907 spin_lock_irqsave(&aun_queue_lock, flags);
908 skb = skb_peek(&aun_queue);
909 while (skb && skb != (struct sk_buff *)&aun_queue)
911 struct sk_buff *newskb = skb->next;
912 eb = (struct ec_cb *)&skb->cb;
913 if (eb->seq == seq)
914 goto foundit;
916 skb = newskb;
918 spin_unlock_irqrestore(&aun_queue_lock, flags);
919 printk(KERN_DEBUG "AUN: unknown sequence %ld\n", seq);
920 return;
922 foundit:
923 tx_result(skb->sk, eb->cookie, result);
924 skb_unlink(skb);
925 spin_unlock_irqrestore(&aun_queue_lock, flags);
929 * Deal with received AUN frames - sort out what type of thing it is
930 * and hand it to the right function.
933 static void aun_data_available(struct sock *sk, int slen)
935 int err;
936 struct sk_buff *skb;
937 unsigned char *data;
938 struct aunhdr *ah;
939 struct iphdr *ip;
940 size_t len;
942 while ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) {
943 if (err == -EAGAIN) {
944 printk(KERN_ERR "AUN: no data available?!");
945 return;
947 printk(KERN_DEBUG "AUN: recvfrom() error %d\n", -err);
950 data = skb->h.raw + sizeof(struct udphdr);
951 ah = (struct aunhdr *)data;
952 len = skb->len - sizeof(struct udphdr);
953 ip = skb->nh.iph;
955 switch (ah->code)
957 case 2:
958 aun_incoming(skb, ah, len);
959 break;
960 case 3:
961 aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_OK);
962 break;
963 case 4:
964 aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_NOT_LISTENING);
965 break;
966 #if 0
967 /* This isn't quite right yet. */
968 case 5:
969 aun_send_response(ip->saddr, ah->handle, 6, ah->cb);
970 break;
971 #endif
972 default:
973 printk(KERN_DEBUG "unknown AUN packet (type %d)\n", data[0]);
976 skb_free_datagram(sk, skb);
980 * Called by the timer to manage the AUN transmit queue. If a packet
981 * was sent to a dead or nonexistent host then we will never get an
982 * acknowledgement back. After a few seconds we need to spot this and
983 * drop the packet.
987 static void ab_cleanup(unsigned long h)
989 struct sk_buff *skb;
990 unsigned long flags;
992 spin_lock_irqsave(&aun_queue_lock, flags);
993 skb = skb_peek(&aun_queue);
994 while (skb && skb != (struct sk_buff *)&aun_queue)
996 struct sk_buff *newskb = skb->next;
997 struct ec_cb *eb = (struct ec_cb *)&skb->cb;
998 if ((jiffies - eb->start) > eb->timeout)
1000 tx_result(skb->sk, eb->cookie,
1001 ECTYPE_TRANSMIT_NOT_PRESENT);
1002 skb_unlink(skb);
1004 skb = newskb;
1006 spin_unlock_irqrestore(&aun_queue_lock, flags);
1008 mod_timer(&ab_cleanup_timer, jiffies + (HZ*2));
1011 static int __init aun_udp_initialise(void)
1013 int error;
1014 struct sockaddr_in sin;
1016 skb_queue_head_init(&aun_queue);
1017 spin_lock_init(&aun_queue_lock);
1018 init_timer(&ab_cleanup_timer);
1019 ab_cleanup_timer.expires = jiffies + (HZ*2);
1020 ab_cleanup_timer.function = ab_cleanup;
1021 add_timer(&ab_cleanup_timer);
1023 memset(&sin, 0, sizeof(sin));
1024 sin.sin_port = htons(AUN_PORT);
1026 /* We can count ourselves lucky Acorn machines are too dim to
1027 speak IPv6. :-) */
1028 if ((error = sock_create(PF_INET, SOCK_DGRAM, 0, &udpsock)) < 0)
1030 printk("AUN: socket error %d\n", -error);
1031 return error;
1034 udpsock->sk->reuse = 1;
1035 udpsock->sk->allocation = GFP_ATOMIC; /* we're going to call it
1036 from interrupts */
1038 error = udpsock->ops->bind(udpsock, (struct sockaddr *)&sin,
1039 sizeof(sin));
1040 if (error < 0)
1042 printk("AUN: bind error %d\n", -error);
1043 goto release;
1046 udpsock->sk->data_ready = aun_data_available;
1048 return 0;
1050 release:
1051 sock_release(udpsock);
1052 udpsock = NULL;
1053 return error;
1055 #endif
1057 static int econet_notifier(struct notifier_block *this, unsigned long msg, void *data)
1059 struct net_device *dev = (struct net_device *)data;
1060 struct ec_device *edev;
1061 unsigned long flags;
1063 switch (msg) {
1064 case NETDEV_UNREGISTER:
1065 /* A device has gone down - kill any data we hold for it. */
1066 spin_lock_irqsave(&edevlist_lock, flags);
1067 for (edev = edevlist; edev; edev = edev->next)
1069 if (edev->dev == dev)
1071 if (edev->prev)
1072 edev->prev->next = edev->next;
1073 else
1074 edevlist = edev->next;
1075 if (edev->next)
1076 edev->next->prev = edev->prev;
1077 kfree(edev);
1078 break;
1081 spin_unlock_irqrestore(&edevlist_lock, flags);
1082 break;
1085 return NOTIFY_DONE;
1088 struct notifier_block econet_netdev_notifier={
1089 econet_notifier,
1090 NULL,
1094 #ifdef MODULE
1095 void cleanup_module(void)
1097 #ifdef CONFIG_ECONET_AUNUDP
1098 del_timer(&ab_cleanup_timer);
1099 if (udpsock)
1100 sock_release(udpsock);
1101 #endif
1102 unregister_netdevice_notifier(&econet_netdev_notifier);
1103 sock_unregister(econet_family_ops.family);
1104 return;
1107 int init_module(void)
1108 #else
1109 void __init econet_proto_init(struct net_proto *pro)
1110 #endif
1112 spin_lock_init(&edevlist_lock);
1113 spin_lock_init(&aun_queue_lock);
1114 /* Stop warnings from happening on UP systems. */
1115 (void)edevlist_lock;
1116 (void)aun_queue_lock;
1117 sock_register(&econet_family_ops);
1118 #ifdef CONFIG_ECONET_AUNUDP
1119 aun_udp_initialise();
1120 #endif
1121 register_netdevice_notifier(&econet_netdev_notifier);
1122 #ifdef MODULE
1123 return 0;
1124 #endif