af_econet.c 25.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 *	An implementation of the Acorn Econet and AUN protocols.
 *	Philip Blundell <philb@gnu.org>
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 *
 */

#include <linux/module.h>

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/in.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/if_ether.h>
#include <linux/netdevice.h>
#include <linux/inetdevice.h>
#include <linux/route.h>
#include <linux/inet.h>
#include <linux/etherdevice.h>
#include <linux/if_arp.h>
#include <linux/wireless.h>
#include <linux/skbuff.h>
32
#include <linux/udp.h>
33
#include <linux/slab.h>
P
Phil Blundell 已提交
34
#include <linux/vmalloc.h>
L
Linus Torvalds 已提交
35 36 37 38 39 40 41 42 43 44
#include <net/sock.h>
#include <net/inet_common.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/if_ec.h>
#include <net/udp.h>
#include <net/ip.h>
#include <linux/spinlock.h>
#include <linux/rcupdate.h>
#include <linux/bitops.h>
45
#include <linux/mutex.h>
L
Linus Torvalds 已提交
46 47 48 49

#include <asm/uaccess.h>
#include <asm/system.h>

50
static const struct proto_ops econet_ops;
L
Linus Torvalds 已提交
51
static struct hlist_head econet_sklist;
E
Eric Dumazet 已提交
52
static DEFINE_SPINLOCK(econet_lock);
53
static DEFINE_MUTEX(econet_mutex);
L
Linus Torvalds 已提交
54 55 56 57 58 59 60 61

/* Since there are only 256 possible network numbers (or fewer, depends
   how you count) it makes sense to use a simple lookup table. */
static struct net_device *net2dev_map[256];

#define EC_PORT_IP	0xd2

#ifdef CONFIG_ECONET_AUNUDP
62
static DEFINE_SPINLOCK(aun_queue_lock);
L
Linus Torvalds 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
static struct socket *udpsock;
#define AUN_PORT	0x8000


struct aunhdr
{
	unsigned char code;		/* AUN magic protocol byte */
	unsigned char port;
	unsigned char cb;
	unsigned char pad;
	unsigned long handle;
};

static unsigned long aun_seq;

/* Queue of packets waiting to be transmitted. */
static struct sk_buff_head aun_queue;
static struct timer_list ab_cleanup_timer;

#endif		/* CONFIG_ECONET_AUNUDP */

/* Per-packet information */
struct ec_cb
{
	struct sockaddr_ec sec;
	unsigned long cookie;		/* Supplied by user. */
#ifdef CONFIG_ECONET_AUNUDP
	int done;
	unsigned long seq;		/* Sequencing */
	unsigned long timeout;		/* Timeout */
	unsigned long start;		/* jiffies */
#endif
#ifdef CONFIG_ECONET_NATIVE
	void (*sent)(struct sk_buff *, int result);
#endif
};

static void econet_remove_socket(struct hlist_head *list, struct sock *sk)
{
E
Eric Dumazet 已提交
102
	spin_lock_bh(&econet_lock);
L
Linus Torvalds 已提交
103
	sk_del_node_init(sk);
E
Eric Dumazet 已提交
104
	spin_unlock_bh(&econet_lock);
L
Linus Torvalds 已提交
105 106 107 108
}

static void econet_insert_socket(struct hlist_head *list, struct sock *sk)
{
E
Eric Dumazet 已提交
109
	spin_lock_bh(&econet_lock);
L
Linus Torvalds 已提交
110
	sk_add_node(sk, list);
E
Eric Dumazet 已提交
111
	spin_unlock_bh(&econet_lock);
L
Linus Torvalds 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
}

/*
 *	Pull a packet from our receive queue and hand it to the user.
 *	If necessary we block.
 */

static int econet_recvmsg(struct kiocb *iocb, struct socket *sock,
			  struct msghdr *msg, size_t len, int flags)
{
	struct sock *sk = sock->sk;
	struct sk_buff *skb;
	size_t copied;
	int err;

	msg->msg_namelen = sizeof(struct sockaddr_ec);

129 130
	mutex_lock(&econet_mutex);

L
Linus Torvalds 已提交
131 132 133 134 135 136 137 138 139 140 141 142
	/*
	 *	Call the generic datagram receiver. This handles all sorts
	 *	of horrible races and re-entrancy so we can forget about it
	 *	in the protocol layers.
	 *
	 *	Now it will return ENETDOWN, if device have just gone down,
	 *	but then it will block.
	 */

	skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err);

	/*
143
	 *	An error occurred so return it. Because skb_recv_datagram()
L
Linus Torvalds 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	 *	handles the blocking we don't see and worry about blocking
	 *	retries.
	 */

	if(skb==NULL)
		goto out;

	/*
	 *	You lose any data beyond the buffer you gave. If it worries a
	 *	user program they can ask the device for its MTU anyway.
	 */

	copied = skb->len;
	if (copied > len)
	{
		copied=len;
		msg->msg_flags|=MSG_TRUNC;
	}

	/* We can't use skb_copy_datagram here */
	err = memcpy_toiovec(msg->msg_iov, skb->data, copied);
	if (err)
		goto out_free;
167
	sk->sk_stamp = skb->tstamp;
L
Linus Torvalds 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180

	if (msg->msg_name)
		memcpy(msg->msg_name, skb->cb, msg->msg_namelen);

	/*
	 *	Free or return the buffer as appropriate. Again this
	 *	hides all the races and re-entrancy issues from us.
	 */
	err = copied;

out_free:
	skb_free_datagram(sk, skb);
out:
181
	mutex_unlock(&econet_mutex);
L
Linus Torvalds 已提交
182 183 184 185 186 187 188 189 190 191
	return err;
}

/*
 *	Bind an Econet socket.
 */

static int econet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
{
	struct sockaddr_ec *sec = (struct sockaddr_ec *)uaddr;
192 193
	struct sock *sk;
	struct econet_sock *eo;
194

L
Linus Torvalds 已提交
195 196 197
	/*
	 *	Check legality
	 */
198

L
Linus Torvalds 已提交
199 200 201
	if (addr_len < sizeof(struct sockaddr_ec) ||
	    sec->sec_family != AF_ECONET)
		return -EINVAL;
202

203 204 205 206 207
	mutex_lock(&econet_mutex);

	sk = sock->sk;
	eo = ec_sk(sk);

L
Linus Torvalds 已提交
208 209 210 211 212
	eo->cb	    = sec->cb;
	eo->port    = sec->port;
	eo->station = sec->addr.station;
	eo->net	    = sec->addr.net;

213 214
	mutex_unlock(&econet_mutex);

L
Linus Torvalds 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	return 0;
}

#if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE)
/*
 *	Queue a transmit result for the user to be told about.
 */

static void tx_result(struct sock *sk, unsigned long cookie, int result)
{
	struct sk_buff *skb = alloc_skb(0, GFP_ATOMIC);
	struct ec_cb *eb;
	struct sockaddr_ec *sec;

	if (skb == NULL)
	{
		printk(KERN_DEBUG "ec: memory squeeze, transmit result dropped.\n");
		return;
	}

	eb = (struct ec_cb *)&skb->cb;
	sec = (struct sockaddr_ec *)&eb->sec;
	memset(sec, 0, sizeof(struct sockaddr_ec));
	sec->cookie = cookie;
	sec->type = ECTYPE_TRANSMIT_STATUS | result;
	sec->sec_family = AF_ECONET;

	if (sock_queue_rcv_skb(sk, skb) < 0)
		kfree_skb(skb);
}
#endif

#ifdef CONFIG_ECONET_NATIVE
/*
 *	Called by the Econet hardware driver when a packet transmit
 *	has completed.  Tell the user.
 */

static void ec_tx_done(struct sk_buff *skb, int result)
{
	struct ec_cb *eb = (struct ec_cb *)&skb->cb;
	tx_result(skb->sk, eb->cookie, result);
}
#endif

/*
 *	Send a packet.  We have to work out which device it's going out on
 *	and hence whether to use real Econet or the UDP emulation.
 */

static int econet_sendmsg(struct kiocb *iocb, struct socket *sock,
			  struct msghdr *msg, size_t len)
{
	struct sock *sk = sock->sk;
	struct sockaddr_ec *saddr=(struct sockaddr_ec *)msg->msg_name;
	struct net_device *dev;
	struct ec_addr addr;
	int err;
	unsigned char port, cb;
#if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE)
	struct sk_buff *skb;
	struct ec_cb *eb;
#endif
#ifdef CONFIG_ECONET_AUNUDP
	struct msghdr udpmsg;
P
Phil Blundell 已提交
280
	struct iovec iov[2];
L
Linus Torvalds 已提交
281 282 283 284
	struct aunhdr ah;
	struct sockaddr_in udpdest;
	__kernel_size_t size;
	mm_segment_t oldfs;
P
Phil Blundell 已提交
285
	char *userbuf;
L
Linus Torvalds 已提交
286
#endif
287

L
Linus Torvalds 已提交
288
	/*
289
	 *	Check the flags.
L
Linus Torvalds 已提交
290 291
	 */

292
	if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
L
Linus Torvalds 已提交
293 294 295
		return -EINVAL;

	/*
296
	 *	Get and verify the address.
L
Linus Torvalds 已提交
297
	 */
298

299 300
	mutex_lock(&econet_mutex);

301 302 303 304 305 306 307 308
        if (saddr == NULL || msg->msg_namelen < sizeof(struct sockaddr_ec)) {
                mutex_unlock(&econet_mutex);
                return -EINVAL;
        }
        addr.station = saddr->addr.station;
        addr.net = saddr->addr.net;
        port = saddr->port;
        cb = saddr->cb;
L
Linus Torvalds 已提交
309 310 311 312 313

	/* Look for a device with the right network number. */
	dev = net2dev_map[addr.net];

	/* If not directly reachable, use some default */
314
	if (dev == NULL) {
L
Linus Torvalds 已提交
315 316
		dev = net2dev_map[0];
		/* No interfaces at all? */
317 318
		if (dev == NULL) {
			mutex_unlock(&econet_mutex);
L
Linus Torvalds 已提交
319
			return -ENETDOWN;
320
		}
L
Linus Torvalds 已提交
321 322
	}

323
	if (dev->type == ARPHRD_ECONET) {
L
Linus Torvalds 已提交
324 325 326
		/* Real hardware Econet.  We're not worthy etc. */
#ifdef CONFIG_ECONET_NATIVE
		unsigned short proto = 0;
327
		int res;
L
Linus Torvalds 已提交
328

P
Phil Blundell 已提交
329 330 331 332 333
		if (len + 15 > dev->mtu) {
			mutex_unlock(&econet_mutex);
			return -EMSGSIZE;
		}

L
Linus Torvalds 已提交
334
		dev_hold(dev);
335

336
		skb = sock_alloc_send_skb(sk, len+LL_ALLOCATED_SPACE(dev),
L
Linus Torvalds 已提交
337 338 339
					  msg->msg_flags & MSG_DONTWAIT, &err);
		if (skb==NULL)
			goto out_unlock;
340

L
Linus Torvalds 已提交
341
		skb_reserve(skb, LL_RESERVED_SPACE(dev));
342
		skb_reset_network_header(skb);
343

L
Linus Torvalds 已提交
344
		eb = (struct ec_cb *)&skb->cb;
345

L
Linus Torvalds 已提交
346 347 348 349
		eb->cookie = saddr->cookie;
		eb->sec = *saddr;
		eb->sent = ec_tx_done;

350 351 352 353 354
		err = -EINVAL;
		res = dev_hard_header(skb, dev, ntohs(proto), &addr, NULL, len);
		if (res < 0)
			goto out_free;
		if (res > 0) {
L
Linus Torvalds 已提交
355 356 357 358 359 360 361
			struct ec_framehdr *fh;
			/* Poke in our control byte and
			   port number.  Hack, hack.  */
			fh = (struct ec_framehdr *)(skb->data);
			fh->cb = cb;
			fh->port = port;
			if (sock->type != SOCK_DGRAM) {
362
				skb_reset_tail_pointer(skb);
L
Linus Torvalds 已提交
363
				skb->len = 0;
364
			}
L
Linus Torvalds 已提交
365
		}
366

L
Linus Torvalds 已提交
367 368 369 370 371 372 373
		/* Copy the data. Returns -EFAULT on error */
		err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
		skb->protocol = proto;
		skb->dev = dev;
		skb->priority = sk->sk_priority;
		if (err)
			goto out_free;
374

L
Linus Torvalds 已提交
375 376 377
		err = -ENETDOWN;
		if (!(dev->flags & IFF_UP))
			goto out_free;
378

L
Linus Torvalds 已提交
379 380 381
		/*
		 *	Now send it
		 */
382

L
Linus Torvalds 已提交
383 384
		dev_queue_xmit(skb);
		dev_put(dev);
385
		mutex_unlock(&econet_mutex);
E
Eric Dumazet 已提交
386
		return len;
L
Linus Torvalds 已提交
387 388 389 390 391 392 393 394 395

	out_free:
		kfree_skb(skb);
	out_unlock:
		if (dev)
			dev_put(dev);
#else
		err = -EPROTOTYPE;
#endif
396 397
		mutex_unlock(&econet_mutex);

L
Linus Torvalds 已提交
398 399 400 401 402 403
		return err;
	}

#ifdef CONFIG_ECONET_AUNUDP
	/* AUN virtual Econet. */

404 405
	if (udpsock == NULL) {
		mutex_unlock(&econet_mutex);
L
Linus Torvalds 已提交
406
		return -ENETDOWN;		/* No socket - can't send */
407
	}
408

P
Phil Blundell 已提交
409 410 411 412 413
	if (len > 32768) {
		err = -E2BIG;
		goto error;
	}

L
Linus Torvalds 已提交
414 415 416 417 418 419 420 421 422 423 424 425 426 427
	/* Make up a UDP datagram and hand it off to some higher intellect. */

	memset(&udpdest, 0, sizeof(udpdest));
	udpdest.sin_family = AF_INET;
	udpdest.sin_port = htons(AUN_PORT);

	/* At the moment we use the stupid Acorn scheme of Econet address
	   y.x maps to IP a.b.c.x.  This should be replaced with something
	   more flexible and more aware of subnet masks.  */
	{
		struct in_device *idev;
		unsigned long network = 0;

		rcu_read_lock();
428
		idev = __in_dev_get_rcu(dev);
L
Linus Torvalds 已提交
429 430
		if (idev) {
			if (idev->ifa_list)
431
				network = ntohl(idev->ifa_list->ifa_address) &
L
Linus Torvalds 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
					0xffffff00;		/* !!! */
		}
		rcu_read_unlock();
		udpdest.sin_addr.s_addr = htonl(network | addr.station);
	}

	ah.port = port;
	ah.cb = cb & 0x7f;
	ah.code = 2;		/* magic */
	ah.pad = 0;

	/* tack our header on the front of the iovec */
	size = sizeof(struct aunhdr);
	iov[0].iov_base = (void *)&ah;
	iov[0].iov_len = size;
P
Phil Blundell 已提交
447 448 449 450 451

	userbuf = vmalloc(len);
	if (userbuf == NULL) {
		err = -ENOMEM;
		goto error;
L
Linus Torvalds 已提交
452 453
	}

P
Phil Blundell 已提交
454 455 456 457 458 459
	iov[1].iov_base = userbuf;
	iov[1].iov_len = len;
	err = memcpy_fromiovec(userbuf, msg->msg_iov, len);
	if (err)
		goto error_free_buf;

L
Linus Torvalds 已提交
460
	/* Get a skbuff (no data, just holds our cb information) */
461
	if ((skb = sock_alloc_send_skb(sk, 0,
462
				       msg->msg_flags & MSG_DONTWAIT,
P
Phil Blundell 已提交
463 464
				       &err)) == NULL)
		goto error_free_buf;
L
Linus Torvalds 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

	eb = (struct ec_cb *)&skb->cb;

	eb->cookie = saddr->cookie;
	eb->timeout = (5*HZ);
	eb->start = jiffies;
	ah.handle = aun_seq;
	eb->seq = (aun_seq++);
	eb->sec = *saddr;

	skb_queue_tail(&aun_queue, skb);

	udpmsg.msg_name = (void *)&udpdest;
	udpmsg.msg_namelen = sizeof(udpdest);
	udpmsg.msg_iov = &iov[0];
P
Phil Blundell 已提交
480
	udpmsg.msg_iovlen = 2;
L
Linus Torvalds 已提交
481 482 483 484 485 486 487
	udpmsg.msg_control = NULL;
	udpmsg.msg_controllen = 0;
	udpmsg.msg_flags=0;

	oldfs = get_fs(); set_fs(KERNEL_DS);	/* More privs :-) */
	err = sock_sendmsg(udpsock, &udpmsg, size);
	set_fs(oldfs);
P
Phil Blundell 已提交
488 489 490

error_free_buf:
	vfree(userbuf);
L
Linus Torvalds 已提交
491 492 493
#else
	err = -EPROTOTYPE;
#endif
P
Phil Blundell 已提交
494
	error:
495 496
	mutex_unlock(&econet_mutex);

L
Linus Torvalds 已提交
497 498 499 500 501 502 503 504 505 506
	return err;
}

/*
 *	Look up the address of a socket.
 */

static int econet_getname(struct socket *sock, struct sockaddr *uaddr,
			  int *uaddr_len, int peer)
{
507 508
	struct sock *sk;
	struct econet_sock *eo;
L
Linus Torvalds 已提交
509 510 511 512 513
	struct sockaddr_ec *sec = (struct sockaddr_ec *)uaddr;

	if (peer)
		return -EOPNOTSUPP;

E
Eric Dumazet 已提交
514
	memset(sec, 0, sizeof(*sec));
515 516 517 518 519
	mutex_lock(&econet_mutex);

	sk = sock->sk;
	eo = ec_sk(sk);

L
Linus Torvalds 已提交
520 521 522 523 524
	sec->sec_family	  = AF_ECONET;
	sec->port	  = eo->port;
	sec->addr.station = eo->station;
	sec->addr.net	  = eo->net;

525 526
	mutex_unlock(&econet_mutex);

L
Linus Torvalds 已提交
527 528 529 530 531 532 533 534
	*uaddr_len = sizeof(*sec);
	return 0;
}

static void econet_destroy_timer(unsigned long data)
{
	struct sock *sk=(struct sock *)data;

535
	if (!sk_has_allocations(sk)) {
L
Linus Torvalds 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
		sk_free(sk);
		return;
	}

	sk->sk_timer.expires = jiffies + 10 * HZ;
	add_timer(&sk->sk_timer);
	printk(KERN_DEBUG "econet socket destroy delayed\n");
}

/*
 *	Close an econet socket.
 */

static int econet_release(struct socket *sock)
{
551
	struct sock *sk;
L
Linus Torvalds 已提交
552

553 554 555
	mutex_lock(&econet_mutex);

	sk = sock->sk;
L
Linus Torvalds 已提交
556
	if (!sk)
557
		goto out_unlock;
L
Linus Torvalds 已提交
558 559 560 561 562 563 564 565 566

	econet_remove_socket(&econet_sklist, sk);

	/*
	 *	Now the socket is dead. No more input will appear.
	 */

	sk->sk_state_change(sk);	/* It is useless. Just for sanity. */

567
	sock_orphan(sk);
L
Linus Torvalds 已提交
568 569 570 571 572

	/* Purge queues */

	skb_queue_purge(&sk->sk_receive_queue);

573
	if (sk_has_allocations(sk)) {
L
Linus Torvalds 已提交
574 575 576 577
		sk->sk_timer.data     = (unsigned long)sk;
		sk->sk_timer.expires  = jiffies + HZ;
		sk->sk_timer.function = econet_destroy_timer;
		add_timer(&sk->sk_timer);
578 579

		goto out_unlock;
L
Linus Torvalds 已提交
580 581 582
	}

	sk_free(sk);
583 584 585

out_unlock:
	mutex_unlock(&econet_mutex);
L
Linus Torvalds 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598
	return 0;
}

static struct proto econet_proto = {
	.name	  = "ECONET",
	.owner	  = THIS_MODULE,
	.obj_size = sizeof(struct econet_sock),
};

/*
 *	Create an Econet socket
 */

599 600
static int econet_create(struct net *net, struct socket *sock, int protocol,
			 int kern)
L
Linus Torvalds 已提交
601 602 603 604 605
{
	struct sock *sk;
	struct econet_sock *eo;
	int err;

O
Octavian Purdila 已提交
606
	if (!net_eq(net, &init_net))
607 608
		return -EAFNOSUPPORT;

L
Linus Torvalds 已提交
609 610 611 612 613 614 615
	/* Econet only provides datagram services. */
	if (sock->type != SOCK_DGRAM)
		return -ESOCKTNOSUPPORT;

	sock->state = SS_UNCONNECTED;

	err = -ENOBUFS;
616
	sk = sk_alloc(net, PF_ECONET, GFP_KERNEL, &econet_proto);
L
Linus Torvalds 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629
	if (sk == NULL)
		goto out;

	sk->sk_reuse = 1;
	sock->ops = &econet_ops;
	sock_init_data(sock, sk);

	eo = ec_sk(sk);
	sock_reset_flag(sk, SOCK_ZAPPED);
	sk->sk_family = PF_ECONET;
	eo->num = protocol;

	econet_insert_socket(&econet_sklist, sk);
E
Eric Dumazet 已提交
630
	return 0;
L
Linus Torvalds 已提交
631 632 633 634 635 636 637 638 639 640 641 642 643 644
out:
	return err;
}

/*
 *	Handle Econet specific ioctls
 */

static int ec_dev_ioctl(struct socket *sock, unsigned int cmd, void __user *arg)
{
	struct ifreq ifr;
	struct ec_device *edev;
	struct net_device *dev;
	struct sockaddr_ec *sec;
645
	int err;
L
Linus Torvalds 已提交
646 647 648 649 650 651 652 653

	/*
	 *	Fetch the caller's info block into kernel space
	 */

	if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
		return -EFAULT;

654
	if ((dev = dev_get_by_name(&init_net, ifr.ifr_name)) == NULL)
L
Linus Torvalds 已提交
655 656 657 658
		return -ENODEV;

	sec = (struct sockaddr_ec *)&ifr.ifr_addr;

659 660 661 662
	mutex_lock(&econet_mutex);

	err = 0;
	switch (cmd) {
L
Linus Torvalds 已提交
663
	case SIOCSIFADDR:
664 665 666 667
		if (!capable(CAP_NET_ADMIN)) {
			err = -EPERM;
			break;
		}
P
Phil Blundell 已提交
668

L
Linus Torvalds 已提交
669
		edev = dev->ec_ptr;
670
		if (edev == NULL) {
L
Linus Torvalds 已提交
671
			/* Magic up a new one. */
672
			edev = kzalloc(sizeof(struct ec_device), GFP_KERNEL);
L
Linus Torvalds 已提交
673
			if (edev == NULL) {
674 675
				err = -ENOMEM;
				break;
L
Linus Torvalds 已提交
676 677
			}
			dev->ec_ptr = edev;
678
		} else
L
Linus Torvalds 已提交
679 680 681 682 683 684
			net2dev_map[edev->net] = NULL;
		edev->station = sec->addr.station;
		edev->net = sec->addr.net;
		net2dev_map[sec->addr.net] = dev;
		if (!net2dev_map[0])
			net2dev_map[0] = dev;
685
		break;
L
Linus Torvalds 已提交
686 687 688

	case SIOCGIFADDR:
		edev = dev->ec_ptr;
689 690 691
		if (edev == NULL) {
			err = -ENODEV;
			break;
L
Linus Torvalds 已提交
692 693 694 695 696 697 698
		}
		memset(sec, 0, sizeof(struct sockaddr_ec));
		sec->addr.station = edev->station;
		sec->addr.net = edev->net;
		sec->sec_family = AF_ECONET;
		dev_put(dev);
		if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
699 700 701 702 703 704
			err = -EFAULT;
		break;

	default:
		err = -EINVAL;
		break;
L
Linus Torvalds 已提交
705 706
	}

707 708
	mutex_unlock(&econet_mutex);

L
Linus Torvalds 已提交
709
	dev_put(dev);
710 711

	return err;
L
Linus Torvalds 已提交
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
}

/*
 *	Handle generic ioctls
 */

static int econet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
{
	struct sock *sk = sock->sk;
	void __user *argp = (void __user *)arg;

	switch(cmd) {
		case SIOCGSTAMP:
			return sock_get_timestamp(sk, argp);

727 728 729
		case SIOCGSTAMPNS:
			return sock_get_timestampns(sk, argp);

L
Linus Torvalds 已提交
730 731 732 733 734 735
		case SIOCSIFADDR:
		case SIOCGIFADDR:
			return ec_dev_ioctl(sock, cmd, argp);
			break;

		default:
736
			return -ENOIOCTLCMD;
L
Linus Torvalds 已提交
737 738 739 740 741
	}
	/*NOTREACHED*/
	return 0;
}

742
static const struct net_proto_family econet_family_ops = {
L
Linus Torvalds 已提交
743 744 745 746 747
	.family =	PF_ECONET,
	.create =	econet_create,
	.owner	=	THIS_MODULE,
};

748
static const struct proto_ops econet_ops = {
L
Linus Torvalds 已提交
749 750 751 752 753 754 755
	.family =	PF_ECONET,
	.owner =	THIS_MODULE,
	.release =	econet_release,
	.bind =		econet_bind,
	.connect =	sock_no_connect,
	.socketpair =	sock_no_socketpair,
	.accept =	sock_no_accept,
756
	.getname =	econet_getname,
L
Linus Torvalds 已提交
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
	.poll =		datagram_poll,
	.ioctl =	econet_ioctl,
	.listen =	sock_no_listen,
	.shutdown =	sock_no_shutdown,
	.setsockopt =	sock_no_setsockopt,
	.getsockopt =	sock_no_getsockopt,
	.sendmsg =	econet_sendmsg,
	.recvmsg =	econet_recvmsg,
	.mmap =		sock_no_mmap,
	.sendpage =	sock_no_sendpage,
};

#if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE)
/*
 *	Find the listening socket, if any, for the given data.
 */

static struct sock *ec_listening_socket(unsigned char port, unsigned char
				 station, unsigned char net)
{
	struct sock *sk;
	struct hlist_node *node;

E
Eric Dumazet 已提交
780
	spin_lock(&econet_lock);
L
Linus Torvalds 已提交
781 782
	sk_for_each(sk, node, &econet_sklist) {
		struct econet_sock *opt = ec_sk(sk);
783
		if ((opt->port == port || opt->port == 0) &&
L
Linus Torvalds 已提交
784
		    (opt->station == station || opt->station == 0) &&
E
Eric Dumazet 已提交
785 786
		    (opt->net == net || opt->net == 0)) {
			sock_hold(sk);
L
Linus Torvalds 已提交
787
			goto found;
E
Eric Dumazet 已提交
788
		}
L
Linus Torvalds 已提交
789 790 791
	}
	sk = NULL;
found:
E
Eric Dumazet 已提交
792
	spin_unlock(&econet_lock);
L
Linus Torvalds 已提交
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
	return sk;
}

/*
 *	Queue a received packet for a socket.
 */

static int ec_queue_packet(struct sock *sk, struct sk_buff *skb,
			   unsigned char stn, unsigned char net,
			   unsigned char cb, unsigned char port)
{
	struct ec_cb *eb = (struct ec_cb *)&skb->cb;
	struct sockaddr_ec *sec = (struct sockaddr_ec *)&eb->sec;

	memset(sec, 0, sizeof(struct sockaddr_ec));
	sec->sec_family = AF_ECONET;
	sec->type = ECTYPE_PACKET_RECEIVED;
	sec->port = port;
	sec->cb = cb;
	sec->addr.net = net;
	sec->addr.station = stn;

	return sock_queue_rcv_skb(sk, skb);
}
#endif

#ifdef CONFIG_ECONET_AUNUDP
/*
821
 *	Send an AUN protocol response.
L
Linus Torvalds 已提交
822 823 824 825 826 827 828 829 830 831 832 833
 */

static void aun_send_response(__u32 addr, unsigned long seq, int code, int cb)
{
	struct sockaddr_in sin = {
		.sin_family = AF_INET,
		.sin_port = htons(AUN_PORT),
		.sin_addr = {.s_addr = addr}
	};
	struct aunhdr ah = {.code = code, .cb = cb, .handle = seq};
	struct kvec iov = {.iov_base = (void *)&ah, .iov_len = sizeof(ah)};
	struct msghdr udpmsg;
834

L
Linus Torvalds 已提交
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
	udpmsg.msg_name = (void *)&sin;
	udpmsg.msg_namelen = sizeof(sin);
	udpmsg.msg_control = NULL;
	udpmsg.msg_controllen = 0;
	udpmsg.msg_flags=0;

	kernel_sendmsg(udpsock, &udpmsg, &iov, 1, sizeof(ah));
}


/*
 *	Handle incoming AUN packets.  Work out if anybody wants them,
 *	and send positive or negative acknowledgements as appropriate.
 */

static void aun_incoming(struct sk_buff *skb, struct aunhdr *ah, size_t len)
{
852
	struct iphdr *ip = ip_hdr(skb);
L
Linus Torvalds 已提交
853
	unsigned char stn = ntohl(ip->saddr) & 0xff;
854 855
	struct dst_entry *dst = skb_dst(skb);
	struct ec_device *edev = NULL;
E
Eric Dumazet 已提交
856
	struct sock *sk = NULL;
L
Linus Torvalds 已提交
857
	struct sk_buff *newskb;
858 859 860

	if (dst)
		edev = dst->dev->ec_ptr;
L
Linus Torvalds 已提交
861 862 863 864 865 866 867

	if (! edev)
		goto bad;

	if ((sk = ec_listening_socket(ah->port, stn, edev->net)) == NULL)
		goto bad;		/* Nobody wants it */

868
	newskb = alloc_skb((len - sizeof(struct aunhdr) + 15) & ~15,
L
Linus Torvalds 已提交
869 870 871 872 873 874 875 876
			   GFP_ATOMIC);
	if (newskb == NULL)
	{
		printk(KERN_DEBUG "AUN: memory squeeze, dropping packet.\n");
		/* Send nack and hope sender tries again */
		goto bad;
	}

877
	memcpy(skb_put(newskb, len - sizeof(struct aunhdr)), (void *)(ah+1),
L
Linus Torvalds 已提交
878 879 880 881 882 883 884 885 886 887
	       len - sizeof(struct aunhdr));

	if (ec_queue_packet(sk, newskb, stn, edev->net, ah->cb, ah->port))
	{
		/* Socket is bankrupt. */
		kfree_skb(newskb);
		goto bad;
	}

	aun_send_response(ip->saddr, ah->handle, 3, 0);
E
Eric Dumazet 已提交
888
	sock_put(sk);
L
Linus Torvalds 已提交
889 890 891 892
	return;

bad:
	aun_send_response(ip->saddr, ah->handle, 4, 0);
E
Eric Dumazet 已提交
893 894
	if (sk)
		sock_put(sk);
L
Linus Torvalds 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
}

/*
 *	Handle incoming AUN transmit acknowledgements.  If the sequence
 *      number matches something in our backlog then kill it and tell
 *	the user.  If the remote took too long to reply then we may have
 *	dropped the packet already.
 */

static void aun_tx_ack(unsigned long seq, int result)
{
	struct sk_buff *skb;
	unsigned long flags;
	struct ec_cb *eb;

	spin_lock_irqsave(&aun_queue_lock, flags);
911
	skb_queue_walk(&aun_queue, skb) {
L
Linus Torvalds 已提交
912 913 914 915 916 917 918 919 920 921
		eb = (struct ec_cb *)&skb->cb;
		if (eb->seq == seq)
			goto foundit;
	}
	spin_unlock_irqrestore(&aun_queue_lock, flags);
	printk(KERN_DEBUG "AUN: unknown sequence %ld\n", seq);
	return;

foundit:
	tx_result(skb->sk, eb->cookie, result);
D
David S. Miller 已提交
922
	skb_unlink(skb, &aun_queue);
L
Linus Torvalds 已提交
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
	spin_unlock_irqrestore(&aun_queue_lock, flags);
	kfree_skb(skb);
}

/*
 *	Deal with received AUN frames - sort out what type of thing it is
 *	and hand it to the right function.
 */

static void aun_data_available(struct sock *sk, int slen)
{
	int err;
	struct sk_buff *skb;
	unsigned char *data;
	struct aunhdr *ah;
	struct iphdr *ip;
	size_t len;

	while ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) {
		if (err == -EAGAIN) {
			printk(KERN_ERR "AUN: no data available?!");
			return;
		}
		printk(KERN_DEBUG "AUN: recvfrom() error %d\n", -err);
	}

949
	data = skb_transport_header(skb) + sizeof(struct udphdr);
L
Linus Torvalds 已提交
950 951
	ah = (struct aunhdr *)data;
	len = skb->len - sizeof(struct udphdr);
952
	ip = ip_hdr(skb);
L
Linus Torvalds 已提交
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986

	switch (ah->code)
	{
	case 2:
		aun_incoming(skb, ah, len);
		break;
	case 3:
		aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_OK);
		break;
	case 4:
		aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_NOT_LISTENING);
		break;
#if 0
		/* This isn't quite right yet. */
	case 5:
		aun_send_response(ip->saddr, ah->handle, 6, ah->cb);
		break;
#endif
	default:
		printk(KERN_DEBUG "unknown AUN packet (type %d)\n", data[0]);
	}

	skb_free_datagram(sk, skb);
}

/*
 *	Called by the timer to manage the AUN transmit queue.  If a packet
 *	was sent to a dead or nonexistent host then we will never get an
 *	acknowledgement back.  After a few seconds we need to spot this and
 *	drop the packet.
 */

static void ab_cleanup(unsigned long h)
{
987
	struct sk_buff *skb, *n;
L
Linus Torvalds 已提交
988 989 990
	unsigned long flags;

	spin_lock_irqsave(&aun_queue_lock, flags);
991
	skb_queue_walk_safe(&aun_queue, skb, n) {
L
Linus Torvalds 已提交
992
		struct ec_cb *eb = (struct ec_cb *)&skb->cb;
993
		if ((jiffies - eb->start) > eb->timeout) {
994
			tx_result(skb->sk, eb->cookie,
L
Linus Torvalds 已提交
995
				  ECTYPE_TRANSMIT_NOT_PRESENT);
D
David S. Miller 已提交
996
			skb_unlink(skb, &aun_queue);
L
Linus Torvalds 已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
			kfree_skb(skb);
		}
	}
	spin_unlock_irqrestore(&aun_queue_lock, flags);

	mod_timer(&ab_cleanup_timer, jiffies + (HZ*2));
}

static int __init aun_udp_initialise(void)
{
	int error;
	struct sockaddr_in sin;

	skb_queue_head_init(&aun_queue);
1011
	setup_timer(&ab_cleanup_timer, ab_cleanup, 0);
L
Linus Torvalds 已提交
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	ab_cleanup_timer.expires = jiffies + (HZ*2);
	add_timer(&ab_cleanup_timer);

	memset(&sin, 0, sizeof(sin));
	sin.sin_port = htons(AUN_PORT);

	/* We can count ourselves lucky Acorn machines are too dim to
	   speak IPv6. :-) */
	if ((error = sock_create_kern(PF_INET, SOCK_DGRAM, 0, &udpsock)) < 0)
	{
		printk("AUN: socket error %d\n", -error);
		return error;
	}
1025

L
Linus Torvalds 已提交
1026 1027 1028
	udpsock->sk->sk_reuse = 1;
	udpsock->sk->sk_allocation = GFP_ATOMIC; /* we're going to call it
						    from interrupts */
1029

L
Linus Torvalds 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	error = udpsock->ops->bind(udpsock, (struct sockaddr *)&sin,
				sizeof(sin));
	if (error < 0)
	{
		printk("AUN: bind error %d\n", -error);
		goto release;
	}

	udpsock->sk->sk_data_ready = aun_data_available;

	return 0;

release:
	sock_release(udpsock);
	udpsock = NULL;
	return error;
}
#endif

#ifdef CONFIG_ECONET_NATIVE

/*
 *	Receive an Econet frame from a device.
 */

D
David S. Miller 已提交
1055
static int econet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
L
Linus Torvalds 已提交
1056 1057
{
	struct ec_framehdr *hdr;
E
Eric Dumazet 已提交
1058
	struct sock *sk = NULL;
L
Linus Torvalds 已提交
1059 1060
	struct ec_device *edev = dev->ec_ptr;

1061
	if (!net_eq(dev_net(dev), &init_net))
1062 1063
		goto drop;

L
Linus Torvalds 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
	if (skb->pkt_type == PACKET_OTHERHOST)
		goto drop;

	if (!edev)
		goto drop;

	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
		return NET_RX_DROP;

	if (!pskb_may_pull(skb, sizeof(struct ec_framehdr)))
		goto drop;

	hdr = (struct ec_framehdr *) skb->data;

	/* First check for encapsulated IP */
	if (hdr->port == EC_PORT_IP) {
		skb->protocol = htons(ETH_P_IP);
		skb_pull(skb, sizeof(struct ec_framehdr));
		netif_rx(skb);
1083
		return NET_RX_SUCCESS;
L
Linus Torvalds 已提交
1084 1085 1086 1087 1088 1089 1090 1091 1092
	}

	sk = ec_listening_socket(hdr->port, hdr->src_stn, hdr->src_net);
	if (!sk)
		goto drop;

	if (ec_queue_packet(sk, skb, edev->net, hdr->src_stn, hdr->cb,
			    hdr->port))
		goto drop;
E
Eric Dumazet 已提交
1093
	sock_put(sk);
1094
	return NET_RX_SUCCESS;
L
Linus Torvalds 已提交
1095 1096

drop:
E
Eric Dumazet 已提交
1097 1098
	if (sk)
		sock_put(sk);
L
Linus Torvalds 已提交
1099 1100 1101 1102
	kfree_skb(skb);
	return NET_RX_DROP;
}

1103
static struct packet_type econet_packet_type __read_mostly = {
1104
	.type =		cpu_to_be16(ETH_P_ECONET),
L
Linus Torvalds 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
	.func =		econet_rcv,
};

static void econet_hw_initialise(void)
{
	dev_add_pack(&econet_packet_type);
}

#endif

static int econet_notifier(struct notifier_block *this, unsigned long msg, void *data)
{
	struct net_device *dev = (struct net_device *)data;
	struct ec_device *edev;

1120
	if (!net_eq(dev_net(dev), &init_net))
1121 1122
		return NOTIFY_DONE;

L
Linus Torvalds 已提交
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
	switch (msg) {
	case NETDEV_UNREGISTER:
		/* A device has gone down - kill any data we hold for it. */
		edev = dev->ec_ptr;
		if (edev)
		{
			if (net2dev_map[0] == dev)
				net2dev_map[0] = NULL;
			net2dev_map[edev->net] = NULL;
			kfree(edev);
			dev->ec_ptr = NULL;
		}
		break;
	}

	return NOTIFY_DONE;
}

static struct notifier_block econet_netdev_notifier = {
	.notifier_call =econet_notifier,
};

static void __exit econet_proto_exit(void)
{
#ifdef CONFIG_ECONET_AUNUDP
	del_timer(&ab_cleanup_timer);
	if (udpsock)
		sock_release(udpsock);
#endif
	unregister_netdevice_notifier(&econet_netdev_notifier);
1153 1154 1155
#ifdef CONFIG_ECONET_NATIVE
	dev_remove_pack(&econet_packet_type);
#endif
L
Linus Torvalds 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
	sock_unregister(econet_family_ops.family);
	proto_unregister(&econet_proto);
}

static int __init econet_proto_init(void)
{
	int err = proto_register(&econet_proto, 0);

	if (err != 0)
		goto out;
	sock_register(&econet_family_ops);
#ifdef CONFIG_ECONET_AUNUDP
	aun_udp_initialise();
#endif
#ifdef CONFIG_ECONET_NATIVE
	econet_hw_initialise();
#endif
	register_netdevice_notifier(&econet_netdev_notifier);
out:
	return err;
}

module_init(econet_proto_init);
module_exit(econet_proto_exit);

MODULE_LICENSE("GPL");
MODULE_ALIAS_NETPROTO(PF_ECONET);