Import 2.3.13
[davej-history.git] / net / econet / econet.c
blobd790ae536bcc4acf95691a2b4033848c87f0f19a
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 <asm/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 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 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.
120 static struct ec_device *edev_get(struct device *dev)
122 struct ec_device *edev;
123 unsigned long flags;
124 spin_lock_irqsave(&edevlist_lock, flags);
125 edev = __edev_get(dev);
126 spin_unlock_irqrestore(&edevlist_lock, flags);
127 return edev;
131 * Pull a packet from our receive queue and hand it to the user.
132 * If necessary we block.
135 static int econet_recvmsg(struct socket *sock, struct msghdr *msg, int len,
136 int flags, struct scm_cookie *scm)
138 struct sock *sk = sock->sk;
139 struct sk_buff *skb;
140 int copied, err;
142 msg->msg_namelen = sizeof(struct sockaddr_ec);
145 * Call the generic datagram receiver. This handles all sorts
146 * of horrible races and re-entrancy so we can forget about it
147 * in the protocol layers.
149 * Now it will return ENETDOWN, if device have just gone down,
150 * but then it will block.
153 skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err);
156 * An error occurred so return it. Because skb_recv_datagram()
157 * handles the blocking we don't see and worry about blocking
158 * retries.
161 if(skb==NULL)
162 goto out;
165 * You lose any data beyond the buffer you gave. If it worries a
166 * user program they can ask the device for its MTU anyway.
169 copied = skb->len;
170 if (copied > len)
172 copied=len;
173 msg->msg_flags|=MSG_TRUNC;
176 /* We can't use skb_copy_datagram here */
177 err = memcpy_toiovec(msg->msg_iov, skb->data, copied);
178 if (err)
179 goto out_free;
180 sk->stamp=skb->stamp;
182 if (msg->msg_name)
183 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
186 * Free or return the buffer as appropriate. Again this
187 * hides all the races and re-entrancy issues from us.
189 err = copied;
191 out_free:
192 skb_free_datagram(sk, skb);
193 out:
194 return err;
198 * Bind an Econet socket.
201 static int econet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
203 struct sockaddr_ec *sec = (struct sockaddr_ec *)uaddr;
204 struct sock *sk=sock->sk;
207 * Check legality
210 if (addr_len < sizeof(struct sockaddr_ec))
211 return -EINVAL;
212 if (sec->sec_family != AF_ECONET)
213 return -EINVAL;
215 sk->protinfo.af_econet->cb = sec->cb;
216 sk->protinfo.af_econet->port = sec->port;
217 sk->protinfo.af_econet->station = sec->addr.station;
218 sk->protinfo.af_econet->net = sec->addr.net;
220 return 0;
224 * Queue a transmit result for the user to be told about.
227 static void tx_result(struct sock *sk, unsigned long cookie, int result)
229 struct sk_buff *skb = alloc_skb(0, GFP_ATOMIC);
230 struct ec_cb *eb;
231 struct sockaddr_ec *sec;
233 if (skb == NULL)
235 printk(KERN_DEBUG "ec: memory squeeze, transmit result dropped.\n");
236 return;
239 eb = (struct ec_cb *)&skb->cb;
240 sec = (struct sockaddr_ec *)&eb->sec;
241 memset(sec, 0, sizeof(struct sockaddr_ec));
242 sec->cookie = cookie;
243 sec->type = ECTYPE_TRANSMIT_STATUS | result;
244 sec->sec_family = AF_ECONET;
246 if (sock_queue_rcv_skb(sk, skb) < 0)
247 kfree_skb(skb);
250 #ifdef CONFIG_ECONET_NATIVE
252 * Called by the Econet hardware driver when a packet transmit
253 * has completed. Tell the user.
256 static void ec_tx_done(struct sk_buff *skb, int result)
258 struct ec_cb *eb = (struct ec_cb *)&skb->cb;
259 tx_result(skb->sk, eb->cookie, result);
261 #endif
264 * Send a packet. We have to work out which device it's going out on
265 * and hence whether to use real Econet or the UDP emulation.
268 static int econet_sendmsg(struct socket *sock, struct msghdr *msg, int len,
269 struct scm_cookie *scm)
271 struct sock *sk = sock->sk;
272 struct sockaddr_ec *saddr=(struct sockaddr_ec *)msg->msg_name;
273 struct device *dev;
274 struct ec_addr addr;
275 struct ec_device *edev;
276 int err;
277 unsigned char port, cb;
278 struct sk_buff *skb;
279 struct ec_cb *eb;
280 #ifdef CONFIG_ECONET_NATIVE
281 unsigned short proto = 0;
282 #endif
283 #ifdef CONFIG_ECONET_AUNUDP
284 struct msghdr udpmsg;
285 struct iovec iov[msg->msg_iovlen+1];
286 struct aunhdr ah;
287 struct sockaddr_in udpdest;
288 __kernel_size_t size;
289 int i;
290 mm_segment_t oldfs;
291 #endif
294 * Check the flags.
297 if (msg->msg_flags&~MSG_DONTWAIT)
298 return(-EINVAL);
301 * Get and verify the address.
304 if (saddr == NULL) {
305 addr.station = sk->protinfo.af_econet->station;
306 addr.net = sk->protinfo.af_econet->net;
307 port = sk->protinfo.af_econet->port;
308 cb = sk->protinfo.af_econet->cb;
309 } else {
310 if (msg->msg_namelen < sizeof(struct sockaddr_ec))
311 return -EINVAL;
312 addr.station = saddr->addr.station;
313 addr.net = saddr->addr.net;
314 port = saddr->port;
315 cb = saddr->cb;
318 /* Look for a device with the right network number. */
319 for (edev = edevlist; edev && (edev->net != addr.net);
320 edev = edev->next);
322 /* Bridge? What's that? */
323 if (edev == NULL)
324 return -ENETUNREACH;
326 dev = edev->dev;
328 if (dev->type == ARPHRD_ECONET)
330 /* Real hardware Econet. We're not worthy etc. */
331 #ifdef CONFIG_ECONET_NATIVE
332 dev_lock_list();
334 skb = sock_alloc_send_skb(sk, len+dev->hard_header_len+15, 0,
335 msg->msg_flags & MSG_DONTWAIT, &err);
336 if (skb==NULL)
337 goto out_unlock;
339 skb_reserve(skb, (dev->hard_header_len+15)&~15);
340 skb->nh.raw = skb->data;
342 eb = (struct ec_cb *)&skb->cb;
344 eb->cookie = saddr->cookie;
345 eb->sec = *saddr;
346 eb->sent = ec_tx_done;
348 if (dev->hard_header) {
349 int res;
350 err = -EINVAL;
351 res = dev->hard_header(skb, dev, ntohs(proto), &addr, NULL, len);
352 if (sock->type != SOCK_DGRAM) {
353 skb->tail = skb->data;
354 skb->len = 0;
355 } else if (res < 0)
356 goto out_free;
359 /* Copy the data. Returns -EFAULT on error */
360 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
361 skb->protocol = proto;
362 skb->dev = dev;
363 skb->priority = sk->priority;
364 if (err)
365 goto out_free;
367 err = -ENETDOWN;
368 if (!(dev->flags & IFF_UP))
369 goto out_free;
372 * Now send it
375 dev_unlock_list();
376 dev_queue_xmit(skb);
377 return(len);
379 out_free:
380 kfree_skb(skb);
381 out_unlock:
382 dev_unlock_list();
383 #else
384 err = -EPROTOTYPE;
385 #endif
386 return err;
389 #ifdef CONFIG_ECONET_AUNUDP
390 /* AUN virtual Econet. */
392 if (udpsock == NULL)
393 return -ENETDOWN; /* No socket - can't send */
395 /* Make up a UDP datagram and hand it off to some higher intellect. */
397 memset(&udpdest, 0, sizeof(udpdest));
398 udpdest.sin_family = AF_INET;
399 udpdest.sin_port = htons(AUN_PORT);
401 /* At the moment we use the stupid Acorn scheme of Econet address
402 y.x maps to IP a.b.c.x. This should be replaced with something
403 more flexible and more aware of subnet masks. */
405 struct in_device *idev = (struct in_device *)dev->ip_ptr;
406 unsigned long network = ntohl(idev->ifa_list->ifa_address) &
407 0xffffff00; /* !!! */
408 udpdest.sin_addr.s_addr = htonl(network | addr.station);
411 ah.port = port;
412 ah.cb = cb & 0x7f;
413 ah.code = 2; /* magic */
414 ah.pad = 0;
416 /* tack our header on the front of the iovec */
417 size = sizeof(struct aunhdr);
418 iov[0].iov_base = (void *)&ah;
419 iov[0].iov_len = size;
420 for (i = 0; i < msg->msg_iovlen; i++) {
421 void *base = msg->msg_iov[i].iov_base;
422 size_t len = msg->msg_iov[i].iov_len;
423 /* Check it now since we switch to KERNEL_DS later. */
424 if ((err = verify_area(VERIFY_READ, base, len)) < 0)
425 return err;
426 iov[i+1].iov_base = base;
427 iov[i+1].iov_len = len;
428 size += len;
431 /* Get a skbuff (no data, just holds our cb information) */
432 if ((skb = sock_alloc_send_skb(sk, 0, 0,
433 msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
434 return err;
436 eb = (struct ec_cb *)&skb->cb;
438 eb->cookie = saddr->cookie;
439 eb->timeout = (5*HZ);
440 eb->start = jiffies;
441 ah.handle = aun_seq;
442 eb->seq = (aun_seq++);
443 eb->sec = *saddr;
445 skb_queue_tail(&aun_queue, skb);
447 udpmsg.msg_name = (void *)&udpdest;
448 udpmsg.msg_namelen = sizeof(udpdest);
449 udpmsg.msg_iov = &iov[0];
450 udpmsg.msg_iovlen = msg->msg_iovlen + 1;
451 udpmsg.msg_control = NULL;
452 udpmsg.msg_controllen = 0;
453 udpmsg.msg_flags=0;
455 oldfs = get_fs(); set_fs(KERNEL_DS); /* More privs :-) */
456 err = sock_sendmsg(udpsock, &udpmsg, size);
457 set_fs(oldfs);
458 #else
459 err = -EPROTOTYPE;
460 #endif
461 return err;
465 * Look up the address of a socket.
468 static int econet_getname(struct socket *sock, struct sockaddr *uaddr,
469 int *uaddr_len, int peer)
471 struct sock *sk = sock->sk;
472 struct sockaddr_ec *sec = (struct sockaddr_ec *)uaddr;
474 if (peer)
475 return -EOPNOTSUPP;
477 sec->sec_family = AF_ECONET;
478 sec->port = sk->protinfo.af_econet->port;
479 sec->addr.station = sk->protinfo.af_econet->station;
480 sec->addr.net = sk->protinfo.af_econet->net;
482 *uaddr_len = sizeof(*sec);
483 return 0;
486 static void econet_destroy_timer(unsigned long data)
488 struct sock *sk=(struct sock *)data;
490 if (!atomic_read(&sk->wmem_alloc) && !atomic_read(&sk->rmem_alloc)) {
491 sk_free(sk);
492 MOD_DEC_USE_COUNT;
493 return;
496 sk->timer.expires=jiffies+10*HZ;
497 add_timer(&sk->timer);
498 printk(KERN_DEBUG "econet socket destroy delayed\n");
502 * Close an econet socket.
505 static int econet_release(struct socket *sock, struct socket *peersock)
507 struct sk_buff *skb;
508 struct sock *sk = sock->sk;
510 if (!sk)
511 return 0;
513 sklist_remove_socket(&econet_sklist, sk);
516 * Now the socket is dead. No more input will appear.
519 sk->state_change(sk); /* It is useless. Just for sanity. */
521 sock->sk = NULL;
522 sk->socket = NULL;
523 sk->dead = 1;
525 /* Purge queues */
527 while ((skb=skb_dequeue(&sk->receive_queue))!=NULL)
528 kfree_skb(skb);
530 if (atomic_read(&sk->rmem_alloc) || atomic_read(&sk->wmem_alloc)) {
531 sk->timer.data=(unsigned long)sk;
532 sk->timer.expires=jiffies+HZ;
533 sk->timer.function=econet_destroy_timer;
534 add_timer(&sk->timer);
535 return 0;
538 sk_free(sk);
539 MOD_DEC_USE_COUNT;
540 return 0;
544 * Create an Econet socket
547 static int econet_create(struct socket *sock, int protocol)
549 struct sock *sk;
550 int err;
552 /* Econet only provides datagram services. */
553 if (sock->type != SOCK_DGRAM)
554 return -ESOCKTNOSUPPORT;
556 sock->state = SS_UNCONNECTED;
557 MOD_INC_USE_COUNT;
559 err = -ENOBUFS;
560 sk = sk_alloc(PF_ECONET, GFP_KERNEL, 1);
561 if (sk == NULL)
562 goto out;
564 sk->reuse = 1;
565 sock->ops = &econet_ops;
566 sock_init_data(sock,sk);
568 sk->protinfo.af_econet = kmalloc(sizeof(struct econet_opt), GFP_KERNEL);
569 if (sk->protinfo.af_econet == NULL)
570 goto out_free;
571 memset(sk->protinfo.af_econet, 0, sizeof(struct econet_opt));
572 sk->zapped=0;
573 sk->family = PF_ECONET;
574 sk->num = protocol;
576 sklist_insert_socket(&econet_sklist, sk);
577 return(0);
579 out_free:
580 sk_free(sk);
581 out:
582 MOD_DEC_USE_COUNT;
583 return err;
587 * Handle Econet specific ioctls
590 static int ec_dev_ioctl(struct socket *sock, unsigned int cmd, void *arg)
592 struct ifreq ifr;
593 struct ec_device *edev;
594 struct device *dev;
595 unsigned long flags;
596 struct sockaddr_ec *sec;
599 * Fetch the caller's info block into kernel space
602 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
603 return -EFAULT;
605 if ((dev = dev_get(ifr.ifr_name)) == NULL)
606 return -ENODEV;
608 sec = (struct sockaddr_ec *)&ifr.ifr_addr;
610 switch (cmd)
612 case SIOCSIFADDR:
613 spin_lock_irqsave(&edevlist_lock, flags);
614 edev = __edev_get(dev);
615 if (edev == NULL)
617 /* Magic up a new one. */
618 edev = kmalloc(GFP_KERNEL, sizeof(struct ec_device));
619 if (edev == NULL) {
620 printk("af_ec: memory squeeze.\n");
621 spin_unlock_irqrestore(&edevlist_lock, flags);
622 return -ENOMEM;
624 memset(edev, 0, sizeof(struct ec_device));
625 edev->dev = dev;
626 edev->next = edevlist;
627 edevlist = edev;
629 edev->station = sec->addr.station;
630 edev->net = sec->addr.net;
631 spin_unlock_irqrestore(&edevlist_lock, flags);
632 return 0;
634 case SIOCGIFADDR:
635 spin_lock_irqsave(&edevlist_lock, flags);
636 edev = __edev_get(dev);
637 if (edev == NULL)
639 spin_unlock_irqrestore(&edevlist_lock, flags);
640 return -ENODEV;
642 memset(sec, 0, sizeof(struct sockaddr_ec));
643 sec->addr.station = edev->station;
644 sec->addr.net = edev->net;
645 sec->sec_family = AF_ECONET;
646 spin_unlock_irqrestore(&edevlist_lock, flags);
647 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
648 return -EFAULT;
649 return 0;
652 return -EINVAL;
656 * Handle generic ioctls
659 static int econet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
661 struct sock *sk = sock->sk;
662 int err;
663 int pid;
665 switch(cmd)
667 case FIOSETOWN:
668 case SIOCSPGRP:
669 err = get_user(pid, (int *) arg);
670 if (err)
671 return err;
672 if (current->pid != pid && current->pgrp != -pid && !suser())
673 return -EPERM;
674 sk->proc = pid;
675 return(0);
676 case FIOGETOWN:
677 case SIOCGPGRP:
678 return put_user(sk->proc, (int *)arg);
679 case SIOCGSTAMP:
680 if(sk->stamp.tv_sec==0)
681 return -ENOENT;
682 err = -EFAULT;
683 if (!copy_to_user((void *)arg, &sk->stamp, sizeof(struct timeval)))
684 err = 0;
685 return err;
686 case SIOCGIFFLAGS:
687 case SIOCSIFFLAGS:
688 case SIOCGIFCONF:
689 case SIOCGIFMETRIC:
690 case SIOCSIFMETRIC:
691 case SIOCGIFMEM:
692 case SIOCSIFMEM:
693 case SIOCGIFMTU:
694 case SIOCSIFMTU:
695 case SIOCSIFLINK:
696 case SIOCGIFHWADDR:
697 case SIOCSIFHWADDR:
698 case SIOCSIFMAP:
699 case SIOCGIFMAP:
700 case SIOCSIFSLAVE:
701 case SIOCGIFSLAVE:
702 case SIOCGIFINDEX:
703 case SIOCGIFNAME:
704 case SIOCGIFCOUNT:
705 case SIOCSIFHWBROADCAST:
706 return(dev_ioctl(cmd,(void *) arg));
708 case SIOCSIFADDR:
709 case SIOCGIFADDR:
710 return ec_dev_ioctl(sock, cmd, (void *)arg);
711 break;
713 default:
714 return(dev_ioctl(cmd,(void *) arg));
716 /*NOTREACHED*/
717 return 0;
720 static struct net_proto_family econet_family_ops = {
721 PF_ECONET,
722 econet_create
725 static struct proto_ops econet_ops = {
726 PF_ECONET,
728 sock_no_dup,
729 econet_release,
730 econet_bind,
731 sock_no_connect,
732 sock_no_socketpair,
733 sock_no_accept,
734 econet_getname,
735 datagram_poll,
736 econet_ioctl,
737 sock_no_listen,
738 sock_no_shutdown,
739 sock_no_setsockopt,
740 sock_no_getsockopt,
741 sock_no_fcntl,
742 econet_sendmsg,
743 econet_recvmsg
747 * Find the listening socket, if any, for the given data.
750 static struct sock *ec_listening_socket(unsigned char port, unsigned char
751 station, unsigned char net)
753 struct sock *sk = econet_sklist;
755 while (sk)
757 struct econet_opt *opt = sk->protinfo.af_econet;
758 if ((opt->port == port || opt->port == 0) &&
759 (opt->station == station || opt->station == 0) &&
760 (opt->net == net || opt->net == 0))
761 return sk;
763 sk = sk->next;
766 return NULL;
769 #ifdef CONFIG_ECONET_AUNUDP
772 * Send an AUN protocol response.
775 static void aun_send_response(__u32 addr, unsigned long seq, int code, int cb)
777 struct sockaddr_in sin;
778 struct iovec iov;
779 struct aunhdr ah;
780 struct msghdr udpmsg;
781 int err;
782 mm_segment_t oldfs;
784 memset(&sin, 0, sizeof(sin));
785 sin.sin_family = AF_INET;
786 sin.sin_port = htons(AUN_PORT);
787 sin.sin_addr.s_addr = addr;
789 ah.code = code;
790 ah.pad = 0;
791 ah.port = 0;
792 ah.cb = cb;
793 ah.handle = seq;
795 iov.iov_base = (void *)&ah;
796 iov.iov_len = sizeof(ah);
798 udpmsg.msg_name = (void *)&sin;
799 udpmsg.msg_namelen = sizeof(sin);
800 udpmsg.msg_iov = &iov;
801 udpmsg.msg_iovlen = 1;
802 udpmsg.msg_control = NULL;
803 udpmsg.msg_controllen = 0;
804 udpmsg.msg_flags=0;
806 oldfs = get_fs(); set_fs(KERNEL_DS);
807 err = sock_sendmsg(udpsock, &udpmsg, sizeof(ah));
808 set_fs(oldfs);
812 * Handle incoming AUN packets. Work out if anybody wants them,
813 * and send positive or negative acknowledgements as appropriate.
816 static void aun_incoming(struct sk_buff *skb, struct aunhdr *ah, size_t len)
818 struct ec_device *edev = edev_get(skb->dev);
819 struct iphdr *ip = skb->nh.iph;
820 unsigned char stn = ntohl(ip->saddr) & 0xff;
821 struct sock *sk;
822 struct sk_buff *newskb;
823 struct ec_cb *eb;
824 struct sockaddr_ec *sec;
826 if (edev == NULL)
827 return; /* Device not configured for AUN */
829 if ((sk = ec_listening_socket(ah->port, stn, edev->net)) == NULL)
830 goto bad; /* Nobody wants it */
832 newskb = alloc_skb((len - sizeof(struct aunhdr) + 15) & ~15,
833 GFP_ATOMIC);
834 if (newskb == NULL)
836 printk(KERN_DEBUG "AUN: memory squeeze, dropping packet.\n");
837 /* Send nack and hope sender tries again */
838 goto bad;
841 eb = (struct ec_cb *)&newskb->cb;
842 sec = (struct sockaddr_ec *)&eb->sec;
843 memset(sec, 0, sizeof(struct sockaddr_ec));
844 sec->sec_family = AF_ECONET;
845 sec->type = ECTYPE_PACKET_RECEIVED;
846 sec->port = ah->port;
847 sec->cb = ah->cb;
848 sec->addr.net = edev->net;
849 sec->addr.station = stn;
851 memcpy(skb_put(newskb, len - sizeof(struct aunhdr)), (void *)(ah+1),
852 len - sizeof(struct aunhdr));
854 if (sock_queue_rcv_skb(sk, newskb) < 0)
856 /* Socket is bankrupt. */
857 kfree_skb(newskb);
858 goto bad;
861 aun_send_response(ip->saddr, ah->handle, 3, 0);
862 return;
864 bad:
865 aun_send_response(ip->saddr, ah->handle, 4, 0);
869 * Handle incoming AUN transmit acknowledgements. If the sequence
870 * number matches something in our backlog then kill it and tell
871 * the user. If the remote took too long to reply then we may have
872 * dropped the packet already.
875 static void aun_tx_ack(unsigned long seq, int result)
877 struct sk_buff *skb;
878 unsigned long flags;
879 struct ec_cb *eb;
881 spin_lock_irqsave(&aun_queue_lock, flags);
882 skb = skb_peek(&aun_queue);
883 while (skb && skb != (struct sk_buff *)&aun_queue)
885 struct sk_buff *newskb = skb->next;
886 eb = (struct ec_cb *)&skb->cb;
887 if (eb->seq == seq)
888 goto foundit;
890 skb = newskb;
892 spin_unlock_irqrestore(&aun_queue_lock, flags);
893 printk(KERN_DEBUG "AUN: unknown sequence %ld\n", seq);
894 return;
896 foundit:
897 tx_result(skb->sk, eb->cookie, result);
898 skb_unlink(skb);
899 spin_unlock_irqrestore(&aun_queue_lock, flags);
903 * Deal with received AUN frames - sort out what type of thing it is
904 * and hand it to the right function.
907 static void aun_data_available(struct sock *sk, int slen)
909 int err;
910 struct sk_buff *skb;
911 unsigned char *data;
912 struct aunhdr *ah;
913 struct iphdr *ip;
914 size_t len;
916 while ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) {
917 if (err == -EAGAIN) {
918 printk(KERN_ERR "AUN: no data available?!");
919 return;
921 printk(KERN_DEBUG "AUN: recvfrom() error %d\n", -err);
924 data = skb->h.raw + sizeof(struct udphdr);
925 ah = (struct aunhdr *)data;
926 len = skb->len - sizeof(struct udphdr);
927 ip = skb->nh.iph;
929 switch (ah->code)
931 case 2:
932 aun_incoming(skb, ah, len);
933 break;
934 case 3:
935 aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_OK);
936 break;
937 case 4:
938 aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_NOT_LISTENING);
939 break;
940 #if 0
941 /* This isn't quite right yet. */
942 case 5:
943 aun_send_response(ip->saddr, ah->handle, 6, ah->cb);
944 break;
945 #endif
946 default:
947 printk(KERN_DEBUG "unknown AUN packet (type %d)\n", data[0]);
950 skb_free_datagram(sk, skb);
954 * Called by the timer to manage the AUN transmit queue. If a packet
955 * was sent to a dead or nonexistent host then we will never get an
956 * acknowledgement back. After a few seconds we need to spot this and
957 * drop the packet.
961 static void ab_cleanup(unsigned long h)
963 struct sk_buff *skb;
964 unsigned long flags;
966 spin_lock_irqsave(&aun_queue_lock, flags);
967 skb = skb_peek(&aun_queue);
968 while (skb && skb != (struct sk_buff *)&aun_queue)
970 struct sk_buff *newskb = skb->next;
971 struct ec_cb *eb = (struct ec_cb *)&skb->cb;
972 if ((jiffies - eb->start) > eb->timeout)
974 tx_result(skb->sk, eb->cookie,
975 ECTYPE_TRANSMIT_NOT_PRESENT);
976 skb_unlink(skb);
978 skb = newskb;
980 spin_unlock_irqrestore(&aun_queue_lock, flags);
982 mod_timer(&ab_cleanup_timer, jiffies + (HZ*2));
985 __initfunc(static int aun_udp_initialise(void))
987 int error;
988 struct sockaddr_in sin;
990 skb_queue_head_init(&aun_queue);
991 spin_lock_init(&aun_queue_lock);
992 init_timer(&ab_cleanup_timer);
993 ab_cleanup_timer.expires = jiffies + (HZ*2);
994 ab_cleanup_timer.function = ab_cleanup;
995 add_timer(&ab_cleanup_timer);
997 memset(&sin, 0, sizeof(sin));
998 sin.sin_port = htons(AUN_PORT);
1000 /* We can count ourselves lucky Acorn machines are too dim to
1001 speak IPv6. :-) */
1002 if ((error = sock_create(PF_INET, SOCK_DGRAM, 0, &udpsock)) < 0)
1004 printk("AUN: socket error %d\n", -error);
1005 return error;
1008 udpsock->sk->reuse = 1;
1009 udpsock->sk->allocation = GFP_ATOMIC; /* we're going to call it
1010 from interrupts */
1012 error = udpsock->ops->bind(udpsock, (struct sockaddr *)&sin,
1013 sizeof(sin));
1014 if (error < 0)
1016 printk("AUN: bind error %d\n", -error);
1017 goto release;
1020 udpsock->sk->data_ready = aun_data_available;
1022 return 0;
1024 release:
1025 sock_release(udpsock);
1026 udpsock = NULL;
1027 return error;
1029 #endif
1031 static int econet_notifier(struct notifier_block *this, unsigned long msg, void *data)
1033 struct device *dev = (struct device *)data;
1034 struct ec_device *edev;
1035 unsigned long flags;
1037 switch (msg) {
1038 case NETDEV_UNREGISTER:
1039 /* A device has gone down - kill any data we hold for it. */
1040 spin_lock_irqsave(&edevlist_lock, flags);
1041 for (edev = edevlist; edev; edev = edev->next)
1043 if (edev->dev == dev)
1045 if (edev->prev)
1046 edev->prev->next = edev->next;
1047 else
1048 edevlist = edev->next;
1049 if (edev->next)
1050 edev->next->prev = edev->prev;
1051 kfree(edev);
1052 break;
1055 spin_unlock_irqrestore(&edevlist_lock, flags);
1056 break;
1059 return NOTIFY_DONE;
1062 struct notifier_block econet_netdev_notifier={
1063 econet_notifier,
1064 NULL,
1068 #ifdef MODULE
1069 void cleanup_module(void)
1071 #ifdef CONFIG_ECONET_AUNUDP
1072 del_timer(&ab_cleanup_timer);
1073 if (udpsock)
1074 sock_release(udpsock);
1075 #endif
1076 unregister_netdevice_notifier(&econet_netdev_notifier);
1077 sock_unregister(econet_family_ops.family);
1078 return;
1081 int init_module(void)
1082 #else
1083 __initfunc(void econet_proto_init(struct net_proto *pro))
1084 #endif
1086 spin_lock_init(&edevlist_lock);
1087 spin_lock_init(&aun_queue_lock);
1088 /* Stop warnings from happening on UP systems. */
1089 (void)edevlist_lock;
1090 (void)aun_queue_lock;
1091 sock_register(&econet_family_ops);
1092 #ifdef CONFIG_ECONET_AUNUDP
1093 aun_udp_initialise();
1094 #endif
1095 register_netdevice_notifier(&econet_netdev_notifier);
1096 #ifdef MODULE
1097 return 0;
1098 #endif