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