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