Import 2.3.9
[davej-history.git] / net / econet / econet.c
blob8930109b37a02300026362774aa8bccc6cda000f
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;
762 sk = sk->sklist_next;
765 return NULL;
768 #ifdef CONFIG_ECONET_AUNUDP
771 * Send an AUN protocol response.
774 static void aun_send_response(__u32 addr, unsigned long seq, int code, int cb)
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;
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;
788 ah.code = code;
789 ah.pad = 0;
790 ah.port = 0;
791 ah.cb = cb;
792 ah.handle = seq;
794 iov.iov_base = (void *)&ah;
795 iov.iov_len = sizeof(ah);
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;
805 oldfs = get_fs(); set_fs(KERNEL_DS);
806 err = sock_sendmsg(udpsock, &udpmsg, sizeof(ah));
807 set_fs(oldfs);
811 * Handle incoming AUN packets. Work out if anybody wants them,
812 * and send positive or negative acknowledgements as appropriate.
815 static void aun_incoming(struct sk_buff *skb, struct aunhdr *ah, size_t len)
817 struct ec_device *edev = edev_get(skb->dev);
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_cb *eb;
823 struct sockaddr_ec *sec;
825 if (edev == NULL)
826 return; /* Device not configured for AUN */
828 if ((sk = ec_listening_socket(ah->port, stn, edev->net)) == NULL)
829 goto bad; /* Nobody wants it */
831 newskb = alloc_skb((len - sizeof(struct aunhdr) + 15) & ~15,
832 GFP_ATOMIC);
833 if (newskb == NULL)
835 printk(KERN_DEBUG "AUN: memory squeeze, dropping packet.\n");
836 /* Send nack and hope sender tries again */
837 goto bad;
840 eb = (struct ec_cb *)&newskb->cb;
841 sec = (struct sockaddr_ec *)&eb->sec;
842 memset(sec, 0, sizeof(struct sockaddr_ec));
843 sec->sec_family = AF_ECONET;
844 sec->type = ECTYPE_PACKET_RECEIVED;
845 sec->port = ah->port;
846 sec->cb = ah->cb;
847 sec->addr.net = edev->net;
848 sec->addr.station = stn;
850 memcpy(skb_put(newskb, len - sizeof(struct aunhdr)), (void *)(ah+1),
851 len - sizeof(struct aunhdr));
853 if (sock_queue_rcv_skb(sk, newskb) < 0)
855 /* Socket is bankrupt. */
856 kfree_skb(newskb);
857 goto bad;
860 aun_send_response(ip->saddr, ah->handle, 3, 0);
861 return;
863 bad:
864 aun_send_response(ip->saddr, ah->handle, 4, 0);
868 * Handle incoming AUN transmit acknowledgements. If the sequence
869 * number matches something in our backlog then kill it and tell
870 * the user. If the remote took too long to reply then we may have
871 * dropped the packet already.
874 static void aun_tx_ack(unsigned long seq, int result)
876 struct sk_buff *skb;
877 unsigned long flags;
878 struct ec_cb *eb;
880 spin_lock_irqsave(&aun_queue_lock, flags);
881 skb = skb_peek(&aun_queue);
882 while (skb && skb != (struct sk_buff *)&aun_queue)
884 struct sk_buff *newskb = skb->next;
885 eb = (struct ec_cb *)&skb->cb;
886 if (eb->seq == seq)
887 goto foundit;
889 skb = newskb;
891 spin_unlock_irqrestore(&aun_queue_lock, flags);
892 printk(KERN_DEBUG "AUN: unknown sequence %ld\n", seq);
893 return;
895 foundit:
896 tx_result(skb->sk, eb->cookie, result);
897 skb_unlink(skb);
898 spin_unlock_irqrestore(&aun_queue_lock, flags);
902 * Deal with received AUN frames - sort out what type of thing it is
903 * and hand it to the right function.
906 static void aun_data_available(struct sock *sk, int slen)
908 int err;
909 struct sk_buff *skb;
910 unsigned char *data;
911 struct aunhdr *ah;
912 struct iphdr *ip;
913 size_t len;
915 while ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) {
916 if (err == -EAGAIN) {
917 printk(KERN_ERR "AUN: no data available?!");
918 return;
920 printk(KERN_DEBUG "AUN: recvfrom() error %d\n", -err);
923 data = skb->h.raw + sizeof(struct udphdr);
924 ah = (struct aunhdr *)data;
925 len = skb->len - sizeof(struct udphdr);
926 ip = skb->nh.iph;
928 switch (ah->code)
930 case 2:
931 aun_incoming(skb, ah, len);
932 break;
933 case 3:
934 aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_OK);
935 break;
936 case 4:
937 aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_NOT_LISTENING);
938 break;
939 #if 0
940 /* This isn't quite right yet. */
941 case 5:
942 aun_send_response(ip->saddr, ah->handle, 6, ah->cb);
943 break;
944 #endif
945 default:
946 printk(KERN_DEBUG "unknown AUN packet (type %d)\n", data[0]);
949 skb_free_datagram(sk, skb);
953 * Called by the timer to manage the AUN transmit queue. If a packet
954 * was sent to a dead or nonexistent host then we will never get an
955 * acknowledgement back. After a few seconds we need to spot this and
956 * drop the packet.
960 static void ab_cleanup(unsigned long h)
962 struct sk_buff *skb;
963 unsigned long flags;
965 spin_lock_irqsave(&aun_queue_lock, flags);
966 skb = skb_peek(&aun_queue);
967 while (skb && skb != (struct sk_buff *)&aun_queue)
969 struct sk_buff *newskb = skb->next;
970 struct ec_cb *eb = (struct ec_cb *)&skb->cb;
971 if ((jiffies - eb->start) > eb->timeout)
973 tx_result(skb->sk, eb->cookie,
974 ECTYPE_TRANSMIT_NOT_PRESENT);
975 skb_unlink(skb);
977 skb = newskb;
979 spin_unlock_irqrestore(&aun_queue_lock, flags);
981 mod_timer(&ab_cleanup_timer, jiffies + (HZ*2));
984 __initfunc(static int aun_udp_initialise(void))
986 int error;
987 struct sockaddr_in sin;
989 skb_queue_head_init(&aun_queue);
990 spin_lock_init(&aun_queue_lock);
991 init_timer(&ab_cleanup_timer);
992 ab_cleanup_timer.expires = jiffies + (HZ*2);
993 ab_cleanup_timer.function = ab_cleanup;
994 add_timer(&ab_cleanup_timer);
996 memset(&sin, 0, sizeof(sin));
997 sin.sin_port = htons(AUN_PORT);
999 /* We can count ourselves lucky Acorn machines are too dim to
1000 speak IPv6. :-) */
1001 if ((error = sock_create(PF_INET, SOCK_DGRAM, 0, &udpsock)) < 0)
1003 printk("AUN: socket error %d\n", -error);
1004 return error;
1007 udpsock->sk->reuse = 1;
1008 udpsock->sk->allocation = GFP_ATOMIC; /* we're going to call it
1009 from interrupts */
1011 error = udpsock->ops->bind(udpsock, (struct sockaddr *)&sin,
1012 sizeof(sin));
1013 if (error < 0)
1015 printk("AUN: bind error %d\n", -error);
1016 goto release;
1019 udpsock->sk->data_ready = aun_data_available;
1021 return 0;
1023 release:
1024 sock_release(udpsock);
1025 udpsock = NULL;
1026 return error;
1028 #endif
1030 static int econet_notifier(struct notifier_block *this, unsigned long msg, void *data)
1032 struct device *dev = (struct device *)data;
1033 struct ec_device *edev;
1034 unsigned long flags;
1036 switch (msg) {
1037 case NETDEV_UNREGISTER:
1038 /* A device has gone down - kill any data we hold for it. */
1039 spin_lock_irqsave(&edevlist_lock, flags);
1040 for (edev = edevlist; edev; edev = edev->next)
1042 if (edev->dev == dev)
1044 if (edev->prev)
1045 edev->prev->next = edev->next;
1046 else
1047 edevlist = edev->next;
1048 if (edev->next)
1049 edev->next->prev = edev->prev;
1050 kfree(edev);
1051 break;
1054 spin_unlock_irqrestore(&edevlist_lock, flags);
1055 break;
1058 return NOTIFY_DONE;
1061 struct notifier_block econet_netdev_notifier={
1062 econet_notifier,
1063 NULL,
1067 #ifdef MODULE
1068 void cleanup_module(void)
1070 #ifdef CONFIG_ECONET_AUNUDP
1071 del_timer(&ab_cleanup_timer);
1072 if (udpsock)
1073 sock_release(udpsock);
1074 #endif
1075 unregister_netdevice_notifier(&econet_netdev_notifier);
1076 sock_unregister(econet_family_ops.family);
1077 return;
1080 int init_module(void)
1081 #else
1082 __initfunc(void econet_proto_init(struct net_proto *pro))
1083 #endif
1085 spin_lock_init(&edevlist_lock);
1086 spin_lock_init(&aun_queue_lock);
1087 /* Stop warnings from happening on UP systems. */
1088 (void)edevlist_lock;
1089 (void)aun_queue_lock;
1090 sock_register(&econet_family_ops);
1091 #ifdef CONFIG_ECONET_AUNUDP
1092 aun_udp_initialise();
1093 #endif
1094 register_netdevice_notifier(&econet_netdev_notifier);
1095 #ifdef MODULE
1096 return 0;
1097 #endif