af_decnet.c 53.6 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

/*
 * DECnet       An implementation of the DECnet protocol suite for the LINUX
 *              operating system.  DECnet is implemented using the  BSD Socket
 *              interface as the means of communication with the user level.
 *
 *              DECnet Socket Layer Interface
 *
 * Authors:     Eduardo Marcelo Serrat <emserrat@geocities.com>
 *              Patrick Caulfield <patrick@pandh.demon.co.uk>
 *
 * Changes:
 *        Steve Whitehouse: Copied from Eduardo Serrat and Patrick Caulfield's
 *                          version of the code. Original copyright preserved
 *                          below.
 *        Steve Whitehouse: Some bug fixes, cleaning up some code to make it
 *                          compatible with my routing layer.
 *        Steve Whitehouse: Merging changes from Eduardo Serrat and Patrick
 *                          Caulfield.
 *        Steve Whitehouse: Further bug fixes, checking module code still works
 *                          with new routing layer.
 *        Steve Whitehouse: Additional set/get_sockopt() calls.
 *        Steve Whitehouse: Fixed TIOCINQ ioctl to be same as Eduardo's new
 *                          code.
 *        Steve Whitehouse: recvmsg() changed to try and behave in a POSIX like
 *                          way. Didn't manage it entirely, but its better.
 *        Steve Whitehouse: ditto for sendmsg().
 *        Steve Whitehouse: A selection of bug fixes to various things.
 *        Steve Whitehouse: Added TIOCOUTQ ioctl.
 *        Steve Whitehouse: Fixes to username2sockaddr & sockaddr2username.
 *        Steve Whitehouse: Fixes to connect() error returns.
 *       Patrick Caulfield: Fixes to delayed acceptance logic.
 *         David S. Miller: New socket locking
 *        Steve Whitehouse: Socket list hashing/locking
 *         Arnaldo C. Melo: use capable, not suser
 *        Steve Whitehouse: Removed unused code. Fix to use sk->allocation
 *                          when required.
 *       Patrick Caulfield: /proc/net/decnet now has object name/number
 *        Steve Whitehouse: Fixed local port allocation, hashed sk list
 *          Matthew Wilcox: Fixes for dn_ioctl()
 *        Steve Whitehouse: New connect/accept logic to allow timeouts and
 *                          prepare for sendpage etc.
 */


/******************************************************************************
    (c) 1995-1998 E.M. Serrat		emserrat@geocities.com
48

Linus Torvalds's avatar
Linus Torvalds committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    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
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

HISTORY:

Version           Kernel     Date       Author/Comments
-------           ------     ----       ---------------
Version 0.0.1     2.0.30    01-dic-97	Eduardo Marcelo Serrat
					(emserrat@geocities.com)

66
					First Development of DECnet Socket La-
Linus Torvalds's avatar
Linus Torvalds committed
67 68 69 70 71 72 73 74 75 76 77
					yer for Linux. Only supports outgoing
					connections.

Version 0.0.2	  2.1.105   20-jun-98   Patrick J. Caulfield
					(patrick@pandh.demon.co.uk)

					Port to new kernel development version.

Version 0.0.3     2.1.106   25-jun-98   Eduardo Marcelo Serrat
					(emserrat@geocities.com)
					_
78 79 80
					Added support for incoming connections
					so we can start developing server apps
					on Linux.
Linus Torvalds's avatar
Linus Torvalds committed
81 82 83
					-
					Module Support
Version 0.0.4     2.1.109   21-jul-98   Eduardo Marcelo Serrat
84 85 86 87 88
				       (emserrat@geocities.com)
				       _
					Added support for X11R6.4. Now we can
					use DECnet transport for X on Linux!!!
				       -
Linus Torvalds's avatar
Linus Torvalds committed
89
Version 0.0.5    2.1.110   01-aug-98   Eduardo Marcelo Serrat
90 91 92 93 94
				       (emserrat@geocities.com)
				       Removed bugs on flow control
				       Removed bugs on incoming accessdata
				       order
				       -
Linus Torvalds's avatar
Linus Torvalds committed
95
Version 0.0.6    2.1.110   07-aug-98   Eduardo Marcelo Serrat
96
				       dn_recvmsg fixes
Linus Torvalds's avatar
Linus Torvalds committed
97

98 99
					Patrick J. Caulfield
				       dn_bind fixes
Linus Torvalds's avatar
Linus Torvalds committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
*******************************************************************************/

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/socket.h>
#include <linux/in.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/string.h>
#include <linux/sockios.h>
#include <linux/net.h>
#include <linux/netdevice.h>
#include <linux/inet.h>
#include <linux/route.h>
#include <linux/netfilter.h>
#include <linux/seq_file.h>
#include <net/sock.h>
120
#include <net/tcp_states.h>
Linus Torvalds's avatar
Linus Torvalds committed
121 122
#include <net/flow.h>
#include <asm/ioctls.h>
123
#include <linux/capability.h>
Linus Torvalds's avatar
Linus Torvalds committed
124 125 126 127 128 129
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/proc_fs.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/poll.h>
130
#include <linux/jiffies.h>
131
#include <net/net_namespace.h>
Linus Torvalds's avatar
Linus Torvalds committed
132 133
#include <net/neighbour.h>
#include <net/dst.h>
134
#include <net/fib_rules.h>
Linus Torvalds's avatar
Linus Torvalds committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
#include <net/dn.h>
#include <net/dn_nsp.h>
#include <net/dn_dev.h>
#include <net/dn_route.h>
#include <net/dn_fib.h>
#include <net/dn_neigh.h>

struct dn_sock {
	struct sock sk;
	struct dn_scp scp;
};

static void dn_keepalive(struct sock *sk);

#define DN_SK_HASH_SHIFT 8
#define DN_SK_HASH_SIZE (1 << DN_SK_HASH_SHIFT)
#define DN_SK_HASH_MASK (DN_SK_HASH_SIZE - 1)


154
static const struct proto_ops dn_proto_ops;
Linus Torvalds's avatar
Linus Torvalds committed
155 156 157
static DEFINE_RWLOCK(dn_hash_lock);
static struct hlist_head dn_sk_hash[DN_SK_HASH_SIZE];
static struct hlist_head dn_wild_sk;
Eric Dumazet's avatar
Eric Dumazet committed
158
static atomic_long_t decnet_memory_allocated;
Linus Torvalds's avatar
Linus Torvalds committed
159

160
static int __dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen, int flags);
Linus Torvalds's avatar
Linus Torvalds committed
161 162 163 164 165 166 167 168 169
static int __dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen, int flags);

static struct hlist_head *dn_find_list(struct sock *sk)
{
	struct dn_scp *scp = DN_SK(sk);

	if (scp->addr.sdn_flags & SDF_WILD)
		return hlist_empty(&dn_wild_sk) ? &dn_wild_sk : NULL;

170
	return &dn_sk_hash[le16_to_cpu(scp->addrloc) & DN_SK_HASH_MASK];
Linus Torvalds's avatar
Linus Torvalds committed
171 172
}

173
/*
Linus Torvalds's avatar
Linus Torvalds committed
174 175
 * Valid ports are those greater than zero and not already in use.
 */
176
static int check_port(__le16 port)
Linus Torvalds's avatar
Linus Torvalds committed
177 178 179 180 181 182
{
	struct sock *sk;

	if (port == 0)
		return -1;

183
	sk_for_each(sk, &dn_sk_hash[le16_to_cpu(port) & DN_SK_HASH_MASK]) {
Linus Torvalds's avatar
Linus Torvalds committed
184 185 186 187 188 189 190 191 192 193 194 195 196
		struct dn_scp *scp = DN_SK(sk);
		if (scp->addrloc == port)
			return -1;
	}
	return 0;
}

static unsigned short port_alloc(struct sock *sk)
{
	struct dn_scp *scp = DN_SK(sk);
static unsigned short port = 0x2000;
	unsigned short i_port = port;

197
	while(check_port(cpu_to_le16(++port)) != 0) {
Linus Torvalds's avatar
Linus Torvalds committed
198 199 200 201
		if (port == i_port)
			return 0;
	}

202
	scp->addrloc = cpu_to_le16(port);
Linus Torvalds's avatar
Linus Torvalds committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

	return 1;
}

/*
 * Since this is only ever called from user
 * level, we don't need a write_lock() version
 * of this.
 */
static int dn_hash_sock(struct sock *sk)
{
	struct dn_scp *scp = DN_SK(sk);
	struct hlist_head *list;
	int rv = -EUSERS;

	BUG_ON(sk_hashed(sk));

	write_lock_bh(&dn_hash_lock);
221

Linus Torvalds's avatar
Linus Torvalds committed
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
	if (!scp->addrloc && !port_alloc(sk))
		goto out;

	rv = -EADDRINUSE;
	if ((list = dn_find_list(sk)) == NULL)
		goto out;

	sk_add_node(sk, list);
	rv = 0;
out:
	write_unlock_bh(&dn_hash_lock);
	return rv;
}

static void dn_unhash_sock(struct sock *sk)
{
	write_lock(&dn_hash_lock);
	sk_del_node_init(sk);
	write_unlock(&dn_hash_lock);
}

static void dn_unhash_sock_bh(struct sock *sk)
{
	write_lock_bh(&dn_hash_lock);
	sk_del_node_init(sk);
	write_unlock_bh(&dn_hash_lock);
}

static struct hlist_head *listen_hash(struct sockaddr_dn *addr)
{
	int i;
253
	unsigned int hash = addr->sdn_objnum;
Linus Torvalds's avatar
Linus Torvalds committed
254 255 256

	if (hash == 0) {
		hash = addr->sdn_objnamel;
257
		for(i = 0; i < le16_to_cpu(addr->sdn_objnamel); i++) {
Linus Torvalds's avatar
Linus Torvalds committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
			hash ^= addr->sdn_objname[i];
			hash ^= (hash << 3);
		}
	}

	return &dn_sk_hash[hash & DN_SK_HASH_MASK];
}

/*
 * Called to transform a socket from bound (i.e. with a local address)
 * into a listening socket (doesn't need a local port number) and rehashes
 * based upon the object name/number.
 */
static void dn_rehash_sock(struct sock *sk)
{
	struct hlist_head *list;
	struct dn_scp *scp = DN_SK(sk);

	if (scp->addr.sdn_flags & SDF_WILD)
		return;

	write_lock_bh(&dn_hash_lock);
	sk_del_node_init(sk);
	DN_SK(sk)->addrloc = 0;
	list = listen_hash(&DN_SK(sk)->addr);
	sk_add_node(sk, list);
	write_unlock_bh(&dn_hash_lock);
}

int dn_sockaddr2username(struct sockaddr_dn *sdn, unsigned char *buf, unsigned char type)
{
	int len = 2;

	*buf++ = type;

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	switch (type) {
	case 0:
		*buf++ = sdn->sdn_objnum;
		break;
	case 1:
		*buf++ = 0;
		*buf++ = le16_to_cpu(sdn->sdn_objnamel);
		memcpy(buf, sdn->sdn_objname, le16_to_cpu(sdn->sdn_objnamel));
		len = 3 + le16_to_cpu(sdn->sdn_objnamel);
		break;
	case 2:
		memset(buf, 0, 5);
		buf += 5;
		*buf++ = le16_to_cpu(sdn->sdn_objnamel);
		memcpy(buf, sdn->sdn_objname, le16_to_cpu(sdn->sdn_objnamel));
		len = 7 + le16_to_cpu(sdn->sdn_objnamel);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
	}

	return len;
}

/*
 * On reception of usernames, we handle types 1 and 0 for destination
 * addresses only. Types 2 and 4 are used for source addresses, but the
 * UIC, GIC are ignored and they are both treated the same way. Type 3
 * is never used as I've no idea what its purpose might be or what its
 * format is.
 */
int dn_username2sockaddr(unsigned char *data, int len, struct sockaddr_dn *sdn, unsigned char *fmt)
{
	unsigned char type;
	int size = len;
	int namel = 12;

	sdn->sdn_objnum = 0;
329
	sdn->sdn_objnamel = cpu_to_le16(0);
Linus Torvalds's avatar
Linus Torvalds committed
330 331 332 333 334 335 336 337 338
	memset(sdn->sdn_objname, 0, DN_MAXOBJL);

	if (len < 2)
		return -1;

	len -= 2;
	*fmt = *data++;
	type = *data++;

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	switch (*fmt) {
	case 0:
		sdn->sdn_objnum = type;
		return 2;
	case 1:
		namel = 16;
		break;
	case 2:
		len  -= 4;
		data += 4;
		break;
	case 4:
		len  -= 8;
		data += 8;
		break;
	default:
		return -1;
Linus Torvalds's avatar
Linus Torvalds committed
356 357 358 359 360 361 362
	}

	len -= 1;

	if (len < 0)
		return -1;

363 364
	sdn->sdn_objnamel = cpu_to_le16(*data++);
	len -= le16_to_cpu(sdn->sdn_objnamel);
Linus Torvalds's avatar
Linus Torvalds committed
365

366
	if ((len < 0) || (le16_to_cpu(sdn->sdn_objnamel) > namel))
Linus Torvalds's avatar
Linus Torvalds committed
367 368
		return -1;

369
	memcpy(sdn->sdn_objname, data, le16_to_cpu(sdn->sdn_objnamel));
Linus Torvalds's avatar
Linus Torvalds committed
370 371 372 373 374 375 376 377 378 379

	return size - len;
}

struct sock *dn_sklist_find_listener(struct sockaddr_dn *addr)
{
	struct hlist_head *list = listen_hash(addr);
	struct sock *sk;

	read_lock(&dn_hash_lock);
380
	sk_for_each(sk, list) {
Linus Torvalds's avatar
Linus Torvalds committed
381 382 383 384 385 386 387 388 389 390 391
		struct dn_scp *scp = DN_SK(sk);
		if (sk->sk_state != TCP_LISTEN)
			continue;
		if (scp->addr.sdn_objnum) {
			if (scp->addr.sdn_objnum != addr->sdn_objnum)
				continue;
		} else {
			if (addr->sdn_objnum)
				continue;
			if (scp->addr.sdn_objnamel != addr->sdn_objnamel)
				continue;
392
			if (memcmp(scp->addr.sdn_objname, addr->sdn_objname, le16_to_cpu(addr->sdn_objnamel)) != 0)
Linus Torvalds's avatar
Linus Torvalds committed
393 394 395 396 397 398 399 400 401
				continue;
		}
		sock_hold(sk);
		read_unlock(&dn_hash_lock);
		return sk;
	}

	sk = sk_head(&dn_wild_sk);
	if (sk) {
402
		if (sk->sk_state == TCP_LISTEN)
Linus Torvalds's avatar
Linus Torvalds committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
			sock_hold(sk);
		else
			sk = NULL;
	}

	read_unlock(&dn_hash_lock);
	return sk;
}

struct sock *dn_find_by_skb(struct sk_buff *skb)
{
	struct dn_skb_cb *cb = DN_SKB_CB(skb);
	struct sock *sk;
	struct dn_scp *scp;

	read_lock(&dn_hash_lock);
419
	sk_for_each(sk, &dn_sk_hash[le16_to_cpu(cb->dst_port) & DN_SK_HASH_MASK]) {
Linus Torvalds's avatar
Linus Torvalds committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
		scp = DN_SK(sk);
		if (cb->src != dn_saddr2dn(&scp->peer))
			continue;
		if (cb->dst_port != scp->addrloc)
			continue;
		if (scp->addrrem && (cb->src_port != scp->addrrem))
			continue;
		sock_hold(sk);
		goto found;
	}
	sk = NULL;
found:
	read_unlock(&dn_hash_lock);
	return sk;
}



static void dn_destruct(struct sock *sk)
{
	struct dn_scp *scp = DN_SK(sk);

	skb_queue_purge(&scp->data_xmit_queue);
	skb_queue_purge(&scp->other_xmit_queue);
	skb_queue_purge(&scp->other_receive_queue);

446
	dst_release(rcu_dereference_check(sk->sk_dst_cache, 1));
Linus Torvalds's avatar
Linus Torvalds committed
447 448
}

449 450
static int dn_memory_pressure;

451
static void dn_enter_memory_pressure(struct sock *sk)
452 453 454 455 456 457
{
	if (!dn_memory_pressure) {
		dn_memory_pressure = 1;
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
458
static struct proto dn_proto = {
459 460 461 462 463 464 465 466 467 468
	.name			= "NSP",
	.owner			= THIS_MODULE,
	.enter_memory_pressure	= dn_enter_memory_pressure,
	.memory_pressure	= &dn_memory_pressure,
	.memory_allocated	= &decnet_memory_allocated,
	.sysctl_mem		= sysctl_decnet_mem,
	.sysctl_wmem		= sysctl_decnet_wmem,
	.sysctl_rmem		= sysctl_decnet_rmem,
	.max_header		= DN_MAX_NSP_DATA_HEADER + 64,
	.obj_size		= sizeof(struct dn_sock),
Linus Torvalds's avatar
Linus Torvalds committed
469 470
};

471
static struct sock *dn_alloc_sock(struct net *net, struct socket *sock, gfp_t gfp, int kern)
Linus Torvalds's avatar
Linus Torvalds committed
472 473
{
	struct dn_scp *scp;
474
	struct sock *sk = sk_alloc(net, PF_DECnet, gfp, &dn_proto, kern);
Linus Torvalds's avatar
Linus Torvalds committed
475 476 477 478 479 480 481 482 483 484

	if  (!sk)
		goto out;

	if (sock)
		sock->ops = &dn_proto_ops;
	sock_init_data(sock, sk);

	sk->sk_backlog_rcv = dn_nsp_backlog_rcv;
	sk->sk_destruct    = dn_destruct;
485
	sk->sk_no_check_tx = 1;
Linus Torvalds's avatar
Linus Torvalds committed
486 487 488
	sk->sk_family      = PF_DECnet;
	sk->sk_protocol    = 0;
	sk->sk_allocation  = gfp;
489 490
	sk->sk_sndbuf	   = sysctl_decnet_wmem[1];
	sk->sk_rcvbuf	   = sysctl_decnet_rmem[1];
Linus Torvalds's avatar
Linus Torvalds committed
491 492 493 494 495 496 497 498 499 500

	/* Initialization of DECnet Session Control Port		*/
	scp = DN_SK(sk);
	scp->state	= DN_O;		/* Open			*/
	scp->numdat	= 1;		/* Next data seg to tx	*/
	scp->numoth	= 1;		/* Next oth data to tx  */
	scp->ackxmt_dat = 0;		/* Last data seg ack'ed */
	scp->ackxmt_oth = 0;		/* Last oth data ack'ed */
	scp->ackrcv_dat = 0;		/* Highest data ack recv*/
	scp->ackrcv_oth = 0;		/* Last oth data ack rec*/
501
	scp->flowrem_sw = DN_SEND;
Linus Torvalds's avatar
Linus Torvalds committed
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
	scp->flowloc_sw = DN_SEND;
	scp->flowrem_dat = 0;
	scp->flowrem_oth = 1;
	scp->flowloc_dat = 0;
	scp->flowloc_oth = 1;
	scp->services_rem = 0;
	scp->services_loc = 1 | NSP_FC_NONE;
	scp->info_rem = 0;
	scp->info_loc = 0x03; /* NSP version 4.1 */
	scp->segsize_rem = 230 - DN_MAX_NSP_DATA_HEADER; /* Default: Updated by remote segsize */
	scp->nonagle = 0;
	scp->multi_ireq = 1;
	scp->accept_mode = ACC_IMMED;
	scp->addr.sdn_family    = AF_DECnet;
	scp->peer.sdn_family    = AF_DECnet;
	scp->accessdata.acc_accl = 5;
	memcpy(scp->accessdata.acc_acc, "LINUX", 5);

	scp->max_window   = NSP_MAX_WINDOW;
	scp->snd_window   = NSP_MIN_WINDOW;
	scp->nsp_srtt     = NSP_INITIAL_SRTT;
	scp->nsp_rttvar   = NSP_INITIAL_RTTVAR;
	scp->nsp_rxtshift = 0;

	skb_queue_head_init(&scp->data_xmit_queue);
	skb_queue_head_init(&scp->other_xmit_queue);
	skb_queue_head_init(&scp->other_receive_queue);

	scp->persist = 0;
	scp->persist_fxn = NULL;
	scp->keepalive = 10 * HZ;
	scp->keepalive_fxn = dn_keepalive;

	init_timer(&scp->delack_timer);
	scp->delack_pending = 0;
	scp->delack_fxn = dn_nsp_delayed_ack;

	dn_start_slow_timer(sk);
out:
	return sk;
}

/*
 * Keepalive timer.
 * FIXME: Should respond to SO_KEEPALIVE etc.
 */
static void dn_keepalive(struct sock *sk)
{
	struct dn_scp *scp = DN_SK(sk);

	/*
	 * By checking the other_data transmit queue is empty
	 * we are double checking that we are not sending too
	 * many of these keepalive frames.
	 */
557
	if (skb_queue_empty(&scp->other_xmit_queue))
Linus Torvalds's avatar
Linus Torvalds committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
		dn_nsp_send_link(sk, DN_NOCHANGE, 0);
}


/*
 * Timer for shutdown/destroyed sockets.
 * When socket is dead & no packets have been sent for a
 * certain amount of time, they are removed by this
 * routine. Also takes care of sending out DI & DC
 * frames at correct times.
 */
int dn_destroy_timer(struct sock *sk)
{
	struct dn_scp *scp = DN_SK(sk);

	scp->persist = dn_nsp_persist(sk);

575 576 577 578 579 580
	switch (scp->state) {
	case DN_DI:
		dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC);
		if (scp->nsp_rxtshift >= decnet_di_count)
			scp->state = DN_CN;
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
581

582 583 584 585 586
	case DN_DR:
		dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC);
		if (scp->nsp_rxtshift >= decnet_dr_count)
			scp->state = DN_DRC;
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
587

588 589 590 591 592 593 594
	case DN_DN:
		if (scp->nsp_rxtshift < decnet_dn_count) {
			/* printk(KERN_DEBUG "dn_destroy_timer: DN\n"); */
			dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC,
					 GFP_ATOMIC);
			return 0;
		}
Linus Torvalds's avatar
Linus Torvalds committed
595 596 597 598 599 600 601
	}

	scp->persist = (HZ * decnet_time_wait);

	if (sk->sk_socket)
		return 0;

602
	if (time_after_eq(jiffies, scp->stamp + HZ * decnet_time_wait)) {
Linus Torvalds's avatar
Linus Torvalds committed
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
		dn_unhash_sock(sk);
		sock_put(sk);
		return 1;
	}

	return 0;
}

static void dn_destroy_sock(struct sock *sk)
{
	struct dn_scp *scp = DN_SK(sk);

	scp->nsp_rxtshift = 0; /* reset back off */

	if (sk->sk_socket) {
		if (sk->sk_socket->state != SS_UNCONNECTED)
			sk->sk_socket->state = SS_DISCONNECTING;
	}

	sk->sk_state = TCP_CLOSE;

624 625 626 627 628 629 630 631 632 633 634 635 636 637
	switch (scp->state) {
	case DN_DN:
		dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC,
				 sk->sk_allocation);
		scp->persist_fxn = dn_destroy_timer;
		scp->persist = dn_nsp_persist(sk);
		break;
	case DN_CR:
		scp->state = DN_DR;
		goto disc_reject;
	case DN_RUN:
		scp->state = DN_DI;
	case DN_DI:
	case DN_DR:
Linus Torvalds's avatar
Linus Torvalds committed
638
disc_reject:
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
		dn_nsp_send_disc(sk, NSP_DISCINIT, 0, sk->sk_allocation);
	case DN_NC:
	case DN_NR:
	case DN_RJ:
	case DN_DIC:
	case DN_CN:
	case DN_DRC:
	case DN_CI:
	case DN_CD:
		scp->persist_fxn = dn_destroy_timer;
		scp->persist = dn_nsp_persist(sk);
		break;
	default:
		printk(KERN_DEBUG "DECnet: dn_destroy_sock passed socket in invalid state\n");
	case DN_O:
		dn_stop_slow_timer(sk);
Linus Torvalds's avatar
Linus Torvalds committed
655

656 657
		dn_unhash_sock_bh(sk);
		sock_put(sk);
Linus Torvalds's avatar
Linus Torvalds committed
658

659
		break;
Linus Torvalds's avatar
Linus Torvalds committed
660 661 662
	}
}

663
char *dn_addr2asc(__u16 addr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
664 665 666 667 668 669 670 671 672 673 674 675
{
	unsigned short node, area;

	node = addr & 0x03ff;
	area = addr >> 10;
	sprintf(buf, "%hd.%hd", area, node);

	return buf;
}



676 677
static int dn_create(struct net *net, struct socket *sock, int protocol,
		     int kern)
Linus Torvalds's avatar
Linus Torvalds committed
678 679 680
{
	struct sock *sk;

681 682 683
	if (protocol < 0 || protocol > SK_PROTOCOL_MAX)
		return -EINVAL;

684
	if (!net_eq(net, &init_net))
685 686
		return -EAFNOSUPPORT;

687 688 689 690 691 692 693 694 695
	switch (sock->type) {
	case SOCK_SEQPACKET:
		if (protocol != DNPROTO_NSP)
			return -EPROTONOSUPPORT;
		break;
	case SOCK_STREAM:
		break;
	default:
		return -ESOCKTNOSUPPORT;
Linus Torvalds's avatar
Linus Torvalds committed
696 697 698
	}


699
	if ((sk = dn_alloc_sock(net, sock, GFP_KERNEL, kern)) == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
		return -ENOBUFS;

	sk->sk_protocol = protocol;

	return 0;
}


static int
dn_release(struct socket *sock)
{
	struct sock *sk = sock->sk;

	if (sk) {
		sock_orphan(sk);
		sock_hold(sk);
		lock_sock(sk);
		dn_destroy_sock(sk);
		release_sock(sk);
		sock_put(sk);
	}

722
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
723 724 725 726 727 728 729
}

static int dn_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
{
	struct sock *sk = sock->sk;
	struct dn_scp *scp = DN_SK(sk);
	struct sockaddr_dn *saddr = (struct sockaddr_dn *)uaddr;
730
	struct net_device *dev, *ldev;
Linus Torvalds's avatar
Linus Torvalds committed
731 732 733 734 735 736 737 738
	int rv;

	if (addr_len != sizeof(struct sockaddr_dn))
		return -EINVAL;

	if (saddr->sdn_family != AF_DECnet)
		return -EINVAL;

739
	if (le16_to_cpu(saddr->sdn_nodeaddrl) && (le16_to_cpu(saddr->sdn_nodeaddrl) != 2))
Linus Torvalds's avatar
Linus Torvalds committed
740 741
		return -EINVAL;

742
	if (le16_to_cpu(saddr->sdn_objnamel) > DN_MAXOBJL)
Linus Torvalds's avatar
Linus Torvalds committed
743 744 745 746 747 748 749 750 751 752
		return -EINVAL;

	if (saddr->sdn_flags & ~SDF_WILD)
		return -EINVAL;

	if (!capable(CAP_NET_BIND_SERVICE) && (saddr->sdn_objnum ||
	    (saddr->sdn_flags & SDF_WILD)))
		return -EACCES;

	if (!(saddr->sdn_flags & SDF_WILD)) {
753
		if (le16_to_cpu(saddr->sdn_nodeaddrl)) {
754
			rcu_read_lock();
755
			ldev = NULL;
756
			for_each_netdev_rcu(&init_net, dev) {
Linus Torvalds's avatar
Linus Torvalds committed
757 758
				if (!dev->dn_ptr)
					continue;
759 760
				if (dn_dev_islocal(dev, dn_saddr2dn(saddr))) {
					ldev = dev;
Linus Torvalds's avatar
Linus Torvalds committed
761
					break;
762
				}
Linus Torvalds's avatar
Linus Torvalds committed
763
			}
764
			rcu_read_unlock();
765
			if (ldev == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
				return -EADDRNOTAVAIL;
		}
	}

	rv = -EINVAL;
	lock_sock(sk);
	if (sock_flag(sk, SOCK_ZAPPED)) {
		memcpy(&scp->addr, saddr, addr_len);
		sock_reset_flag(sk, SOCK_ZAPPED);

		rv = dn_hash_sock(sk);
		if (rv)
			sock_set_flag(sk, SOCK_ZAPPED);
	}
	release_sock(sk);

782
	return rv;
Linus Torvalds's avatar
Linus Torvalds committed
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
}


static int dn_auto_bind(struct socket *sock)
{
	struct sock *sk = sock->sk;
	struct dn_scp *scp = DN_SK(sk);
	int rv;

	sock_reset_flag(sk, SOCK_ZAPPED);

	scp->addr.sdn_flags  = 0;
	scp->addr.sdn_objnum = 0;

	/*
	 * This stuff is to keep compatibility with Eduardo's
	 * patch. I hope I can dispense with it shortly...
	 */
	if ((scp->accessdata.acc_accl != 0) &&
		(scp->accessdata.acc_accl <= 12)) {
803

804 805
		scp->addr.sdn_objnamel = cpu_to_le16(scp->accessdata.acc_accl);
		memcpy(scp->addr.sdn_objname, scp->accessdata.acc_acc, le16_to_cpu(scp->addr.sdn_objnamel));
Linus Torvalds's avatar
Linus Torvalds committed
806 807 808 809 810 811

		scp->accessdata.acc_accl = 0;
		memset(scp->accessdata.acc_acc, 0, 40);
	}
	/* End of compatibility stuff */

812
	scp->addr.sdn_add.a_len = cpu_to_le16(2);
813
	rv = dn_dev_bind_default((__le16 *)scp->addr.sdn_add.a_addr);
Linus Torvalds's avatar
Linus Torvalds committed
814 815 816 817 818 819 820 821 822
	if (rv == 0) {
		rv = dn_hash_sock(sk);
		if (rv)
			sock_set_flag(sk, SOCK_ZAPPED);
	}

	return rv;
}

823
static int dn_confirm_accept(struct sock *sk, long *timeo, gfp_t allocation)
Linus Torvalds's avatar
Linus Torvalds committed
824 825 826 827 828 829 830 831 832
{
	struct dn_scp *scp = DN_SK(sk);
	DEFINE_WAIT(wait);
	int err;

	if (scp->state != DN_CR)
		return -EINVAL;

	scp->state = DN_CC;
833
	scp->segsize_loc = dst_metric_advmss(__sk_dst_get(sk));
Linus Torvalds's avatar
Linus Torvalds committed
834 835
	dn_send_conn_conf(sk, allocation);

Eric Dumazet's avatar
Eric Dumazet committed
836
	prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds's avatar
Linus Torvalds committed
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
	for(;;) {
		release_sock(sk);
		if (scp->state == DN_CC)
			*timeo = schedule_timeout(*timeo);
		lock_sock(sk);
		err = 0;
		if (scp->state == DN_RUN)
			break;
		err = sock_error(sk);
		if (err)
			break;
		err = sock_intr_errno(*timeo);
		if (signal_pending(current))
			break;
		err = -EAGAIN;
		if (!*timeo)
			break;
Eric Dumazet's avatar
Eric Dumazet committed
854
		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds's avatar
Linus Torvalds committed
855
	}
Eric Dumazet's avatar
Eric Dumazet committed
856
	finish_wait(sk_sleep(sk), &wait);
Linus Torvalds's avatar
Linus Torvalds committed
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
	if (err == 0) {
		sk->sk_socket->state = SS_CONNECTED;
	} else if (scp->state != DN_CC) {
		sk->sk_socket->state = SS_UNCONNECTED;
	}
	return err;
}

static int dn_wait_run(struct sock *sk, long *timeo)
{
	struct dn_scp *scp = DN_SK(sk);
	DEFINE_WAIT(wait);
	int err = 0;

	if (scp->state == DN_RUN)
		goto out;

	if (!*timeo)
		return -EALREADY;

Eric Dumazet's avatar
Eric Dumazet committed
877
	prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds's avatar
Linus Torvalds committed
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
	for(;;) {
		release_sock(sk);
		if (scp->state == DN_CI || scp->state == DN_CC)
			*timeo = schedule_timeout(*timeo);
		lock_sock(sk);
		err = 0;
		if (scp->state == DN_RUN)
			break;
		err = sock_error(sk);
		if (err)
			break;
		err = sock_intr_errno(*timeo);
		if (signal_pending(current))
			break;
		err = -ETIMEDOUT;
		if (!*timeo)
			break;
Eric Dumazet's avatar
Eric Dumazet committed
895
		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds's avatar
Linus Torvalds committed
896
	}
Eric Dumazet's avatar
Eric Dumazet committed
897
	finish_wait(sk_sleep(sk), &wait);
Linus Torvalds's avatar
Linus Torvalds committed
898 899 900 901 902 903 904 905 906 907 908 909 910 911
out:
	if (err == 0) {
		sk->sk_socket->state = SS_CONNECTED;
	} else if (scp->state != DN_CI && scp->state != DN_CC) {
		sk->sk_socket->state = SS_UNCONNECTED;
	}
	return err;
}

static int __dn_connect(struct sock *sk, struct sockaddr_dn *addr, int addrlen, long *timeo, int flags)
{
	struct socket *sock = sk->sk_socket;
	struct dn_scp *scp = DN_SK(sk);
	int err = -EISCONN;
912
	struct flowidn fld;
913
	struct dst_entry *dst;
Linus Torvalds's avatar
Linus Torvalds committed
914 915 916 917 918 919 920 921 922 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 949 950 951

	if (sock->state == SS_CONNECTED)
		goto out;

	if (sock->state == SS_CONNECTING) {
		err = 0;
		if (scp->state == DN_RUN) {
			sock->state = SS_CONNECTED;
			goto out;
		}
		err = -ECONNREFUSED;
		if (scp->state != DN_CI && scp->state != DN_CC) {
			sock->state = SS_UNCONNECTED;
			goto out;
		}
		return dn_wait_run(sk, timeo);
	}

	err = -EINVAL;
	if (scp->state != DN_O)
		goto out;

	if (addr == NULL || addrlen != sizeof(struct sockaddr_dn))
		goto out;
	if (addr->sdn_family != AF_DECnet)
		goto out;
	if (addr->sdn_flags & SDF_WILD)
		goto out;

	if (sock_flag(sk, SOCK_ZAPPED)) {
		err = dn_auto_bind(sk->sk_socket);
		if (err)
			goto out;
	}

	memcpy(&scp->peer, addr, sizeof(struct sockaddr_dn));

	err = -EHOSTUNREACH;
952 953 954 955 956 957 958
	memset(&fld, 0, sizeof(fld));
	fld.flowidn_oif = sk->sk_bound_dev_if;
	fld.daddr = dn_saddr2dn(&scp->peer);
	fld.saddr = dn_saddr2dn(&scp->addr);
	dn_sk_ports_copy(&fld, scp);
	fld.flowidn_proto = DNPROTO_NSP;
	if (dn_route_output_sock(&sk->sk_dst_cache, &fld, sk, flags) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
959
		goto out;
960 961
	dst = __sk_dst_get(sk);
	sk->sk_route_caps = dst->dev->features;
Linus Torvalds's avatar
Linus Torvalds committed
962 963
	sock->state = SS_CONNECTING;
	scp->state = DN_CI;
964
	scp->segsize_loc = dst_metric_advmss(dst);
Linus Torvalds's avatar
Linus Torvalds committed
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992

	dn_nsp_send_conninit(sk, NSP_CI);
	err = -EINPROGRESS;
	if (*timeo) {
		err = dn_wait_run(sk, timeo);
	}
out:
	return err;
}

static int dn_connect(struct socket *sock, struct sockaddr *uaddr, int addrlen, int flags)
{
	struct sockaddr_dn *addr = (struct sockaddr_dn *)uaddr;
	struct sock *sk = sock->sk;
	int err;
	long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);

	lock_sock(sk);
	err = __dn_connect(sk, addr, addrlen, &timeo, 0);
	release_sock(sk);

	return err;
}

static inline int dn_check_state(struct sock *sk, struct sockaddr_dn *addr, int addrlen, long *timeo, int flags)
{
	struct dn_scp *scp = DN_SK(sk);

993 994 995 996 997 998 999 1000 1001 1002
	switch (scp->state) {
	case DN_RUN:
		return 0;
	case DN_CR:
		return dn_confirm_accept(sk, timeo, sk->sk_allocation);
	case DN_CI:
	case DN_CC:
		return dn_wait_run(sk, timeo);
	case DN_O:
		return __dn_connect(sk, addr, addrlen, timeo, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1003 1004 1005 1006 1007 1008 1009 1010
	}

	return -EINVAL;
}


static void dn_access_copy(struct sk_buff *skb, struct accessdata_dn *acc)
{
1011
	unsigned char *ptr = skb->data;
Linus Torvalds's avatar
Linus Torvalds committed
1012

1013 1014 1015
	acc->acc_userl = *ptr++;
	memcpy(&acc->acc_user, ptr, acc->acc_userl);
	ptr += acc->acc_userl;
Linus Torvalds's avatar
Linus Torvalds committed
1016

1017 1018 1019
	acc->acc_passl = *ptr++;
	memcpy(&acc->acc_pass, ptr, acc->acc_passl);
	ptr += acc->acc_passl;
Linus Torvalds's avatar
Linus Torvalds committed
1020

1021 1022
	acc->acc_accl = *ptr++;
	memcpy(&acc->acc_acc, ptr, acc->acc_accl);
Linus Torvalds's avatar
Linus Torvalds committed
1023

1024
	skb_pull(skb, acc->acc_accl + acc->acc_passl + acc->acc_userl + 3);
Linus Torvalds's avatar
Linus Torvalds committed
1025 1026 1027 1028 1029

}

static void dn_user_copy(struct sk_buff *skb, struct optdata_dn *opt)
{
1030 1031 1032 1033
	unsigned char *ptr = skb->data;
	u16 len = *ptr++; /* yes, it's 8bit on the wire */

	BUG_ON(len > 16); /* we've checked the contents earlier */
1034
	opt->opt_optl   = cpu_to_le16(len);
1035 1036 1037
	opt->opt_status = 0;
	memcpy(opt->opt_data, ptr, len);
	skb_pull(skb, len + 1);
Linus Torvalds's avatar
Linus Torvalds committed
1038 1039 1040 1041 1042 1043 1044 1045
}

static struct sk_buff *dn_wait_for_connect(struct sock *sk, long *timeo)
{
	DEFINE_WAIT(wait);
	struct sk_buff *skb = NULL;
	int err = 0;

Eric Dumazet's avatar
Eric Dumazet committed
1046
	prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds's avatar
Linus Torvalds committed
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
	for(;;) {
		release_sock(sk);
		skb = skb_dequeue(&sk->sk_receive_queue);
		if (skb == NULL) {
			*timeo = schedule_timeout(*timeo);
			skb = skb_dequeue(&sk->sk_receive_queue);
		}
		lock_sock(sk);
		if (skb != NULL)
			break;
		err = -EINVAL;
		if (sk->sk_state != TCP_LISTEN)
			break;
		err = sock_intr_errno(*timeo);
		if (signal_pending(current))
			break;
		err = -EAGAIN;
		if (!*timeo)
			break;
Eric Dumazet's avatar
Eric Dumazet committed
1066
		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds's avatar
Linus Torvalds committed
1067
	}
Eric Dumazet's avatar
Eric Dumazet committed
1068
	finish_wait(sk_sleep(sk), &wait);
Linus Torvalds's avatar
Linus Torvalds committed
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081

	return skb == NULL ? ERR_PTR(err) : skb;
}

static int dn_accept(struct socket *sock, struct socket *newsock, int flags)
{
	struct sock *sk = sock->sk, *newsk;
	struct sk_buff *skb = NULL;
	struct dn_skb_cb *cb;
	unsigned char menuver;
	int err = 0;
	unsigned char type;
	long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Eric Dumazet's avatar
Eric Dumazet committed
1082
	struct dst_entry *dst;
Linus Torvalds's avatar
Linus Torvalds committed
1083 1084 1085

	lock_sock(sk);

1086
	if (sk->sk_state != TCP_LISTEN || DN_SK(sk)->state != DN_O) {
Linus Torvalds's avatar
Linus Torvalds committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
		release_sock(sk);
		return -EINVAL;
	}

	skb = skb_dequeue(&sk->sk_receive_queue);
	if (skb == NULL) {
		skb = dn_wait_for_connect(sk, &timeo);
		if (IS_ERR(skb)) {
			release_sock(sk);
			return PTR_ERR(skb);
		}
	}

	cb = DN_SKB_CB(skb);
	sk->sk_ack_backlog--;
1102
	newsk = dn_alloc_sock(sock_net(sk), newsock, sk->sk_allocation, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1103 1104 1105 1106 1107 1108 1109
	if (newsk == NULL) {
		release_sock(sk);
		kfree_skb(skb);
		return -ENOBUFS;
	}
	release_sock(sk);

Eric Dumazet's avatar
Eric Dumazet committed
1110
	dst = skb_dst(skb);
1111
	sk_dst_set(newsk, dst);
Eric Dumazet's avatar
Eric Dumazet committed
1112
	skb_dst_set(skb, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
1113

1114
	DN_SK(newsk)->state        = DN_CR;
Linus Torvalds's avatar
Linus Torvalds committed
1115 1116 1117 1118 1119
	DN_SK(newsk)->addrrem      = cb->src_port;
	DN_SK(newsk)->services_rem = cb->services;
	DN_SK(newsk)->info_rem     = cb->info;
	DN_SK(newsk)->segsize_rem  = cb->segsize;
	DN_SK(newsk)->accept_mode  = DN_SK(sk)->accept_mode;
1120

Linus Torvalds's avatar
Linus Torvalds committed
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
	if (DN_SK(newsk)->segsize_rem < 230)
		DN_SK(newsk)->segsize_rem = 230;

	if ((DN_SK(newsk)->services_rem & NSP_FC_MASK) == NSP_FC_NONE)
		DN_SK(newsk)->max_window = decnet_no_fc_max_cwnd;

	newsk->sk_state  = TCP_LISTEN;
	memcpy(&(DN_SK(newsk)->addr), &(DN_SK(sk)->addr), sizeof(struct sockaddr_dn));

	/*
	 * If we are listening on a wild socket, we don't want
	 * the newly created socket on the wrong hash queue.
	 */
	DN_SK(newsk)->addr.sdn_flags &= ~SDF_WILD;

	skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->addr), &type));
	skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->peer), &type));
1138 1139
	*(__le16 *)(DN_SK(newsk)->peer.sdn_add.a_addr) = cb->src;
	*(__le16 *)(DN_SK(newsk)->addr.sdn_add.a_addr) = cb->dst;
Linus Torvalds's avatar
Linus Torvalds committed
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169

	menuver = *skb->data;
	skb_pull(skb, 1);

	if (menuver & DN_MENUVER_ACC)
		dn_access_copy(skb, &(DN_SK(newsk)->accessdata));

	if (menuver & DN_MENUVER_USR)
		dn_user_copy(skb, &(DN_SK(newsk)->conndata_in));

	if (menuver & DN_MENUVER_PRX)
		DN_SK(newsk)->peer.sdn_flags |= SDF_PROXY;

	if (menuver & DN_MENUVER_UIC)
		DN_SK(newsk)->peer.sdn_flags |= SDF_UICPROXY;

	kfree_skb(skb);

	memcpy(&(DN_SK(newsk)->conndata_out), &(DN_SK(sk)->conndata_out),
		sizeof(struct optdata_dn));
	memcpy(&(DN_SK(newsk)->discdata_out), &(DN_SK(sk)->discdata_out),
		sizeof(struct optdata_dn));

	lock_sock(newsk);
	err = dn_hash_sock(newsk);
	if (err == 0) {
		sock_reset_flag(newsk, SOCK_ZAPPED);
		dn_send_conn_ack(newsk);

		/*
1170 1171 1172
		 * Here we use sk->sk_allocation since although the conn conf is
		 * for the newsk, the context is the old socket.
		 */
Linus Torvalds's avatar
Linus Torvalds committed
1173 1174 1175 1176 1177
		if (DN_SK(newsk)->accept_mode == ACC_IMMED)
			err = dn_confirm_accept(newsk, &timeo,
						sk->sk_allocation);
	}
	release_sock(newsk);
1178
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
}


static int dn_getname(struct socket *sock, struct sockaddr *uaddr,int *uaddr_len,int peer)
{
	struct sockaddr_dn *sa = (struct sockaddr_dn *)uaddr;
	struct sock *sk = sock->sk;
	struct dn_scp *scp = DN_SK(sk);

	*uaddr_len = sizeof(struct sockaddr_dn);

	lock_sock(sk);

	if (peer) {
1193 1194
		if ((sock->state != SS_CONNECTED &&
		     sock->state != SS_CONNECTING) &&
1195
		    scp->accept_mode == ACC_IMMED) {
1196
			release_sock(sk);
Linus Torvalds's avatar
Linus Torvalds committed
1197
			return -ENOTCONN;
1198
		}
Linus Torvalds's avatar
Linus Torvalds committed
1199 1200 1201 1202 1203 1204 1205 1206

		memcpy(sa, &scp->peer, sizeof(struct sockaddr_dn));
	} else {
		memcpy(sa, &scp->addr, sizeof(struct sockaddr_dn));
	}

	release_sock(sk);

1207
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1208 1209 1210 1211 1212 1213 1214 1215 1216
}


static unsigned int dn_poll(struct file *file, struct socket *sock, poll_table  *wait)
{
	struct sock *sk = sock->sk;
	struct dn_scp *scp = DN_SK(sk);
	int mask = datagram_poll(file, sock, wait);

1217
	if (!skb_queue_empty(&scp->other_receive_queue))
Linus Torvalds's avatar
Linus Torvalds committed
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
		mask |= POLLRDBAND;

	return mask;
}

static int dn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
{
	struct sock *sk = sock->sk;
	struct dn_scp *scp = DN_SK(sk);
	int err = -EOPNOTSUPP;
	long amount = 0;
	struct sk_buff *skb;
	int val;

	switch(cmd)
	{
	case SIOCGIFADDR:
	case SIOCSIFADDR:
		return dn_dev_ioctl(cmd, (void __user *)arg);

	case SIOCATMARK:
		lock_sock(sk);
1240
		val = !skb_queue_empty(&scp->other_receive_queue);
Linus Torvalds's avatar
Linus Torvalds committed
1241 1242 1243 1244 1245 1246
		if (scp->state != DN_RUN)
			val = -ENOTCONN;
		release_sock(sk);
		return val;

	case TIOCOUTQ:
1247
		amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds's avatar
Linus Torvalds committed
1248 1249 1250 1251 1252 1253 1254
		if (amount < 0)
			amount = 0;
		err = put_user(amount, (int __user *)arg);
		break;

	case TIOCINQ:
		lock_sock(sk);
1255 1256
		skb = skb_peek(&scp->other_receive_queue);
		if (skb) {
Linus Torvalds's avatar
Linus Torvalds committed
1257 1258
			amount = skb->len;
		} else {
1259
			skb_queue_walk(&sk->sk_receive_queue, skb)
Linus Torvalds's avatar
Linus Torvalds committed
1260 1261 1262 1263 1264 1265 1266
				amount += skb->len;
		}
		release_sock(sk);
		err = put_user(amount, (int __user *)arg);
		break;

	default:
1267
		err = -ENOIOCTLCMD;
Linus Torvalds's avatar
Linus Torvalds committed
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
		break;
	}

	return err;
}

static int dn_listen(struct socket *sock, int backlog)
{
	struct sock *sk = sock->sk;
	int err = -EINVAL;

	lock_sock(sk);

	if (sock_flag(sk, SOCK_ZAPPED))
		goto out;

	if ((DN_SK(sk)->state != DN_O) || (sk->sk_state == TCP_LISTEN))
		goto out;

	sk->sk_max_ack_backlog = backlog;
	sk->sk_ack_backlog     = 0;
	sk->sk_state           = TCP_LISTEN;
	err                 = 0;
	dn_rehash_sock(sk);

out:
	release_sock(sk);

1296
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
}


static int dn_shutdown(struct socket *sock, int how)
{
	struct sock *sk = sock->sk;
	struct dn_scp *scp = DN_SK(sk);
	int err = -ENOTCONN;

	lock_sock(sk);

	if (sock->state == SS_UNCONNECTED)
		goto out;

	err = 0;
	if (sock->state == SS_DISCONNECTING)
		goto out;

	err = -EINVAL;
	if (scp->state == DN_O)
		goto out;

1319
	if (how != SHUT_RDWR)
Linus Torvalds's avatar
Linus Torvalds committed
1320 1321
		goto out;

1322
	sk->sk_shutdown = SHUTDOWN_MASK;
Linus Torvalds's avatar
Linus Torvalds committed
1323 1324 1325 1326 1327 1328 1329 1330 1331
	dn_destroy_sock(sk);
	err = 0;

out:
	release_sock(sk);

	return err;
}

1332
static int dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds's avatar
Linus Torvalds committed
1333 1334 1335 1336 1337 1338 1339
{
	struct sock *sk = sock->sk;
	int err;

	lock_sock(sk);
	err = __dn_setsockopt(sock, level, optname, optval, optlen, 0);
	release_sock(sk);
1340 1341 1342 1343 1344 1345
#ifdef CONFIG_NETFILTER
	/* we need to exclude all possible ENOPROTOOPTs except default case */
	if (err == -ENOPROTOOPT && optname != DSO_LINKINFO &&
	    optname != DSO_STREAM && optname != DSO_SEQPACKET)
		err = nf_setsockopt(sk, PF_DECnet, optname, optval, optlen);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1346 1347 1348 1349

	return err;
}

1350
static int __dn_setsockopt(struct socket *sock, int level,int optname, char __user *optval, unsigned int optlen, int flags)
Linus Torvalds's avatar
Linus Torvalds committed
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
{
	struct	sock *sk = sock->sk;
	struct dn_scp *scp = DN_SK(sk);
	long timeo;
	union {
		struct optdata_dn opt;
		struct accessdata_dn acc;
		int mode;
		unsigned long win;
		int val;
		unsigned char services;
		unsigned char info;
	} u;
	int err;

	if (optlen && !optval)
		return -EINVAL;

	if (optlen > sizeof(u))
		return -EINVAL;

	if (copy_from_user(&u, optval, optlen))
		return -EFAULT;

1375 1376 1377 1378 1379 1380
	switch (optname) {
	case DSO_CONDATA:
		if (sock->state == SS_CONNECTED)
			return -EISCONN;
		if ((scp->state != DN_O) && (scp->state != DN_CR))
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1381

1382 1383
		if (optlen != sizeof(struct optdata_dn))
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1384

1385 1386
		if (le16_to_cpu(u.opt.opt_optl) > 16)
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1387

1388 1389
		memcpy(&scp->conndata_out, &u.opt, optlen);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1390

1391 1392 1393 1394
	case DSO_DISDATA:
		if (sock->state != SS_CONNECTED &&
		    scp->accept_mode == ACC_IMMED)
			return -ENOTCONN;
Linus Torvalds's avatar
Linus Torvalds committed
1395

1396 1397
		if (optlen != sizeof(struct optdata_dn))
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1398

1399 1400
		if (le16_to_cpu(u.opt.opt_optl) > 16)
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1401

1402 1403
		memcpy(&scp->discdata_out, &u.opt, optlen);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1404

1405 1406 1407 1408 1409
	case DSO_CONACCESS:
		if (sock->state == SS_CONNECTED)
			return -EISCONN;
		if (scp->state != DN_O)
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1410

1411 1412
		if (optlen != sizeof(struct accessdata_dn))
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1413

1414 1415 1416 1417
		if ((u.acc.acc_accl > DN_MAXACCL) ||
		    (u.acc.acc_passl > DN_MAXACCL) ||
		    (u.acc.acc_userl > DN_MAXACCL))
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1418

1419 1420
		memcpy(&scp->accessdata, &u.acc, optlen);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1421

1422 1423 1424 1425 1426
	case DSO_ACCEPTMODE:
		if (sock->state == SS_CONNECTED)
			return -EISCONN;
		if (scp->state != DN_O)
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1427

1428 1429
		if (optlen != sizeof(int))
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1430

1431 1432
		if ((u.mode != ACC_IMMED) && (u.mode != ACC_DEFER))
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1433

1434 1435
		scp->accept_mode = (unsigned char)u.mode;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1436

1437 1438 1439 1440 1441 1442
	case DSO_CONACCEPT:
		if (scp->state != DN_CR)
			return -EINVAL;
		timeo = sock_rcvtimeo(sk, 0);
		err = dn_confirm_accept(sk, &timeo, sk->sk_allocation);
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
1443

1444 1445 1446
	case DSO_CONREJECT:
		if (scp->state != DN_CR)
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1447

1448 1449 1450 1451
		scp->state = DN_DR;
		sk->sk_shutdown = SHUTDOWN_MASK;
		dn_nsp_send_disc(sk, 0x38, 0, sk->sk_allocation);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1452

1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
	case DSO_MAXWINDOW:
		if (optlen != sizeof(unsigned long))
			return -EINVAL;
		if (u.win > NSP_MAX_WINDOW)
			u.win = NSP_MAX_WINDOW;
		if (u.win == 0)
			return -EINVAL;
		scp->max_window = u.win;
		if (scp->snd_window > u.win)
			scp->snd_window = u.win;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1464

1465 1466 1467 1468 1469 1470 1471 1472
	case DSO_NODELAY:
		if (optlen != sizeof(int))
			return -EINVAL;
		if (scp->nonagle == 2)
			return -EINVAL;
		scp->nonagle = (u.val == 0) ? 0 : 1;
		/* if (scp->nonagle == 1) { Push pending frames } */
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1473

1474 1475 1476 1477 1478 1479 1480 1481
	case DSO_CORK:
		if (optlen != sizeof(int))
			return -EINVAL;
		if (scp->nonagle == 1)
			return -EINVAL;
		scp->nonagle = (u.val == 0) ? 0 : 2;
		/* if (scp->nonagle == 0) { Push pending frames } */
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1482

1483 1484 1485 1486 1487 1488 1489 1490 1491
	case DSO_SERVICES:
		if (optlen != sizeof(unsigned char))
			return -EINVAL;
		if ((u.services & ~NSP_FC_MASK) != 0x01)
			return -EINVAL;
		if ((u.services & NSP_FC_MASK) == NSP_FC_MASK)
			return -EINVAL;
		scp->services_loc = u.services;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1492

1493 1494 1495 1496 1497 1498 1499
	case DSO_INFO:
		if (optlen != sizeof(unsigned char))
			return -EINVAL;
		if (u.info & 0xfc)
			return -EINVAL;
		scp->info_loc = u.info;
		break;
1500 1501 1502 1503 1504 1505

	case DSO_LINKINFO:
	case DSO_STREAM:
	case DSO_SEQPACKET:
	default:
		return -ENOPROTOOPT;
Linus Torvalds's avatar
Linus Torvalds committed
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
	}

	return 0;
}

static int dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
{
	struct sock *sk = sock->sk;
	int err;

	lock_sock(sk);
	err = __dn_getsockopt(sock, level, optname, optval, optlen, 0);
	release_sock(sk);
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
#ifdef CONFIG_NETFILTER
	if (err == -ENOPROTOOPT && optname != DSO_STREAM &&
	    optname != DSO_SEQPACKET && optname != DSO_CONACCEPT &&
	    optname != DSO_CONREJECT) {
		int len;

		if (get_user(len, optlen))
			return -EFAULT;

		err = nf_getsockopt(sk, PF_DECnet, optname, optval, &len);
		if (err >= 0)
			err = put_user(len, optlen);
	}
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547

	return err;
}

static int __dn_getsockopt(struct socket *sock, int level,int optname, char __user *optval,int __user *optlen, int flags)
{
	struct	sock *sk = sock->sk;
	struct dn_scp *scp = DN_SK(sk);
	struct linkinfo_dn link;
	unsigned int r_len;
	void *r_data = NULL;
	unsigned int val;

	if(get_user(r_len , optlen))
		return -EFAULT;
1548

1549 1550 1551 1552 1553 1554
	switch (optname) {
	case DSO_CONDATA:
		if (r_len > sizeof(struct optdata_dn))
			r_len = sizeof(struct optdata_dn);
		r_data = &scp->conndata_in;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1555

1556 1557 1558 1559 1560
	case DSO_DISDATA:
		if (r_len > sizeof(struct optdata_dn))
			r_len = sizeof(struct optdata_dn);
		r_data = &scp->discdata_in;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1561

1562 1563 1564 1565 1566
	case DSO_CONACCESS:
		if (r_len > sizeof(struct accessdata_dn))
			r_len = sizeof(struct accessdata_dn);
		r_data = &scp->accessdata;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1567

1568 1569 1570 1571 1572
	case DSO_ACCEPTMODE:
		if (r_len > sizeof(unsigned char))
			r_len = sizeof(unsigned char);
		r_data = &scp->accept_mode;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1573

1574 1575 1576
	case DSO_LINKINFO:
		if (r_len > sizeof(struct linkinfo_dn))
			r_len = sizeof(struct linkinfo_dn);
1577

1578
		memset(&link, 0, sizeof(link));
Linus Torvalds's avatar
Linus Torvalds committed
1579

1580 1581 1582 1583 1584 1585 1586 1587 1588
		switch (sock->state) {
		case SS_CONNECTING:
			link.idn_linkstate = LL_CONNECTING;
			break;
		case SS_DISCONNECTING:
			link.idn_linkstate = LL_DISCONNECTING;
			break;
		case SS_CONNECTED:
			link.idn_linkstate = LL_RUNNING;
Linus Torvalds's avatar
Linus Torvalds committed
1589 1590
			break;
		default:
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
			link.idn_linkstate = LL_INACTIVE;
		}

		link.idn_segsize = scp->segsize_rem;
		r_data = &link;
		break;

	case DSO_MAXWINDOW:
		if (r_len > sizeof(unsigned long))
			r_len = sizeof(unsigned long);
		r_data = &scp->max_window;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1603

1604 1605 1606 1607 1608 1609
	case DSO_NODELAY:
		if (r_len > sizeof(int))
			r_len = sizeof(int);
		val = (scp->nonagle == 1);
		r_data = &val;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1610

1611 1612 1613 1614 1615 1616
	case DSO_CORK:
		if (r_len > sizeof(int))
			r_len = sizeof(int);
		val = (scp->nonagle == 2);
		r_data = &val;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1617

1618 1619 1620 1621 1622
	case DSO_SERVICES:
		if (r_len > sizeof(unsigned char))
			r_len = sizeof(unsigned char);
		r_data = &scp->services_rem;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1623

1624 1625 1626 1627 1628
	case DSO_INFO:
		if (r_len > sizeof(unsigned char))
			r_len = sizeof(unsigned char);
		r_data = &scp->info_rem;
		break;
1629 1630 1631 1632 1633 1634 1635

	case DSO_STREAM:
	case DSO_SEQPACKET:
	case DSO_CONACCEPT:
	case DSO_CONREJECT:
	default:
		return -ENOPROTOOPT;
Linus Torvalds's avatar
Linus Torvalds committed
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
	}

	if (r_data) {
		if (copy_to_user(optval, r_data, r_len))
			return -EFAULT;
		if (put_user(r_len, optlen))
			return -EFAULT;
	}

	return 0;
}


static int dn_data_ready(struct sock *sk, struct sk_buff_head *q, int flags, int target)
{
1651
	struct sk_buff *skb;
Linus Torvalds's avatar
Linus Torvalds committed
1652 1653 1654
	int len = 0;

	if (flags & MSG_OOB)
1655
		return !skb_queue_empty(q) ? 1 : 0;
Linus Torvalds's avatar
Linus Torvalds committed
1656

1657
	skb_queue_walk(q, skb) {
Linus Torvalds's avatar
Linus Torvalds committed
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
		struct dn_skb_cb *cb = DN_SKB_CB(skb);
		len += skb->len;

		if (cb->nsp_flags & 0x40) {
			/* SOCK_SEQPACKET reads to EOM */
			if (sk->sk_type == SOCK_SEQPACKET)
				return 1;
			/* so does SOCK_STREAM unless WAITALL is specified */
			if (!(flags & MSG_WAITALL))
				return 1;
		}

		/* minimum data length for read exceeded */
		if (len >= target)
			return 1;
	}

	return 0;
}


1679 1680
static int dn_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
		      int flags)
Linus Torvalds's avatar
Linus Torvalds committed
1681 1682 1683 1684 1685 1686 1687
{
	struct sock *sk = sock->sk;
	struct dn_scp *scp = DN_SK(sk);
	struct sk_buff_head *queue = &sk->sk_receive_queue;
	size_t target = size > 1 ? 1 : 0;
	size_t copied = 0;
	int rv = 0;
1688
	struct sk_buff *skb, *n;
Linus Torvalds's avatar
Linus Torvalds committed
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
	struct dn_skb_cb *cb = NULL;
	unsigned char eor = 0;
	long timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);

	lock_sock(sk);

	if (sock_flag(sk, SOCK_ZAPPED)) {
		rv = -EADDRNOTAVAIL;
		goto out;
	}

	if (sk->sk_shutdown & RCV_SHUTDOWN) {
Patrick Caulfield's avatar
Patrick Caulfield committed
1701
		rv = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1702 1703 1704
		goto out;
	}

Patrick Caulfield's avatar
Patrick Caulfield committed
1705 1706 1707 1708
	rv = dn_check_state(sk, NULL, 0, &timeo, flags);
	if (rv)
		goto out;

1709
	if (flags & ~(MSG_CMSG_COMPAT|MSG_PEEK|MSG_OOB|MSG_WAITALL|MSG_DONTWAIT|MSG_NOSIGNAL)) {
Linus Torvalds's avatar
Linus Torvalds committed
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
		rv = -EOPNOTSUPP;
		goto out;
	}

	if (flags & MSG_OOB)
		queue = &scp->other_receive_queue;

	if (flags & MSG_WAITALL)
		target = size;


	/*
	 * See if there is data ready to read, sleep if there isn't
	 */
	for(;;) {
1725 1726
		DEFINE_WAIT(wait);

Linus Torvalds's avatar
Linus Torvalds committed
1727 1728 1729
		if (sk->sk_err)
			goto out;

1730
		if (!skb_queue_empty(&scp->other_receive_queue)) {
Linus Torvalds's avatar
Linus Torvalds committed
1731 1732 1733 1734 1735 1736 1737 1738
			if (!(flags & MSG_OOB)) {
				msg->msg_flags |= MSG_OOB;
				if (!scp->other_report) {
					scp->other_report = 1;
					goto out;
				}
			}
		}
1739

Linus Torvalds's avatar
Linus Torvalds committed
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
		if (scp->state != DN_RUN)
			goto out;

		if (signal_pending(current)) {
			rv = sock_intr_errno(timeo);
			goto out;
		}

		if (dn_data_ready(sk, queue, flags, target))
			break;

		if (flags & MSG_DONTWAIT) {
			rv = -EWOULDBLOCK;
			goto out;
		}

Eric Dumazet's avatar
Eric Dumazet committed
1756
		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1757
		sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1758
		sk_wait_event(sk, &timeo, dn_data_ready(sk, queue, flags, target));
1759
		sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
Eric Dumazet's avatar
Eric Dumazet committed
1760
		finish_wait(sk_sleep(sk), &wait);
Linus Torvalds's avatar
Linus Torvalds committed
1761 1762
	}

1763
	skb_queue_walk_safe(queue, skb, n) {
Linus Torvalds's avatar
Linus Torvalds committed
1764 1765 1766 1767 1768 1769
		unsigned int chunk = skb->len;
		cb = DN_SKB_CB(skb);

		if ((chunk + copied) > size)
			chunk = size - copied;

Al Viro's avatar
Al Viro committed
1770
		if (memcpy_to_msg(msg, skb->data, chunk)) {
Linus Torvalds's avatar
Linus Torvalds committed
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
			rv = -EFAULT;
			break;
		}
		copied += chunk;

		if (!(flags & MSG_PEEK))
			skb_pull(skb, chunk);

		eor = cb->nsp_flags & 0x40;

		if (skb->len == 0) {
David S. Miller's avatar
David S. Miller committed
1782
			skb_unlink(skb, queue);
Linus Torvalds's avatar
Linus Torvalds committed
1783
			kfree_skb(skb);
1784
			/*
Linus Torvalds's avatar
Linus Torvalds committed
1785 1786 1787 1788 1789 1790 1791 1792 1793
			 * N.B. Don't refer to skb or cb after this point
			 * in loop.
			 */
			if ((scp->flowloc_sw == DN_DONTSEND) && !dn_congested(sk)) {
				scp->flowloc_sw = DN_SEND;
				dn_nsp_send_link(sk, DN_SEND, 0);
			}
		}

1794
		if (eor) {
Linus Torvalds's avatar
Linus Torvalds committed
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812