af_iucv.c 59.9 KB
Newer Older
1 2 3
/*
 *  IUCV protocol stack for Linux on zSeries
 *
4
 *  Copyright IBM Corp. 2006, 2009
5 6
 *
 *  Author(s):	Jennifer Hunt <jenhunt@us.ibm.com>
7 8 9
 *		Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
 *  PM functions:
 *		Ursula Braun <ursula.braun@de.ibm.com>
10 11
 */

12 13 14
#define KMSG_COMPONENT "af_iucv"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

15 16 17 18 19
#include <linux/module.h>
#include <linux/types.h>
#include <linux/list.h>
#include <linux/errno.h>
#include <linux/kernel.h>
20
#include <linux/sched/signal.h>
21 22 23 24
#include <linux/slab.h>
#include <linux/skbuff.h>
#include <linux/init.h>
#include <linux/poll.h>
25
#include <linux/security.h>
26 27 28 29 30 31 32
#include <net/sock.h>
#include <asm/ebcdic.h>
#include <asm/cpcmd.h>
#include <linux/kmod.h>

#include <net/iucv/af_iucv.h>

33
#define VERSION "1.2"
34 35 36

static char iucv_userid[80];

37
static const struct proto_ops iucv_sock_ops;
38 39 40 41 42 43 44

static struct proto iucv_proto = {
	.name		= "AF_IUCV",
	.owner		= THIS_MODULE,
	.obj_size	= sizeof(struct iucv_sock),
};

45 46
static struct iucv_interface *pr_iucv;

47 48 49 50
/* special AF_IUCV IPRM messages */
static const u8 iprm_shutdown[8] =
	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};

51 52
#define TRGCLS_SIZE	(sizeof(((struct iucv_message *)0)->class))

53 54 55 56 57
#define __iucv_sock_wait(sk, condition, timeo, ret)			\
do {									\
	DEFINE_WAIT(__wait);						\
	long __timeo = timeo;						\
	ret = 0;							\
E
Eric Dumazet 已提交
58
	prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE);	\
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
	while (!(condition)) {						\
		if (!__timeo) {						\
			ret = -EAGAIN;					\
			break;						\
		}							\
		if (signal_pending(current)) {				\
			ret = sock_intr_errno(__timeo);			\
			break;						\
		}							\
		release_sock(sk);					\
		__timeo = schedule_timeout(__timeo);			\
		lock_sock(sk);						\
		ret = sock_error(sk);					\
		if (ret)						\
			break;						\
	}								\
E
Eric Dumazet 已提交
75
	finish_wait(sk_sleep(sk), &__wait);				\
76 77 78 79 80 81 82 83 84
} while (0)

#define iucv_sock_wait(sk, condition, timeo)				\
({									\
	int __ret = 0;							\
	if (!(condition))						\
		__iucv_sock_wait(sk, condition, timeo, __ret);		\
	__ret;								\
})
85

86 87
static void iucv_sock_kill(struct sock *sk);
static void iucv_sock_close(struct sock *sk);
88
static void iucv_sever_path(struct sock *, int);
89

90 91 92 93 94 95
static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
	struct packet_type *pt, struct net_device *orig_dev);
static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
		   struct sk_buff *skb, u8 flags);
static void afiucv_hs_callback_txnotify(struct sk_buff *, enum iucv_tx_notify);

96 97 98
/* Call Back functions */
static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
99 100 101 102
static void iucv_callback_connack(struct iucv_path *, u8 *);
static int iucv_callback_connreq(struct iucv_path *, u8 *, u8 *);
static void iucv_callback_connrej(struct iucv_path *, u8 *);
static void iucv_callback_shutdown(struct iucv_path *, u8 *);
103 104

static struct iucv_sock_list iucv_sk_list = {
105
	.lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
106 107 108 109 110 111 112 113
	.autobind_name = ATOMIC_INIT(0)
};

static struct iucv_handler af_iucv_handler = {
	.path_pending	  = iucv_callback_connreq,
	.path_complete	  = iucv_callback_connack,
	.path_severed	  = iucv_callback_connrej,
	.message_pending  = iucv_callback_rx,
114 115
	.message_complete = iucv_callback_txdone,
	.path_quiesced	  = iucv_callback_shutdown,
116 117 118 119 120 121 122 123 124 125 126 127
};

static inline void high_nmcpy(unsigned char *dst, char *src)
{
       memcpy(dst, src, 8);
}

static inline void low_nmcpy(unsigned char *dst, char *src)
{
       memcpy(&dst[8], src, 8);
}

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
static int afiucv_pm_prepare(struct device *dev)
{
#ifdef CONFIG_PM_DEBUG
	printk(KERN_WARNING "afiucv_pm_prepare\n");
#endif
	return 0;
}

static void afiucv_pm_complete(struct device *dev)
{
#ifdef CONFIG_PM_DEBUG
	printk(KERN_WARNING "afiucv_pm_complete\n");
#endif
}

/**
 * afiucv_pm_freeze() - Freeze PM callback
 * @dev:	AFIUCV dummy device
 *
 * Sever all established IUCV communication pathes
 */
static int afiucv_pm_freeze(struct device *dev)
{
	struct iucv_sock *iucv;
	struct sock *sk;
	int err = 0;

#ifdef CONFIG_PM_DEBUG
	printk(KERN_WARNING "afiucv_pm_freeze\n");
#endif
	read_lock(&iucv_sk_list.lock);
159
	sk_for_each(sk, &iucv_sk_list.head) {
160 161 162 163 164
		iucv = iucv_sk(sk);
		switch (sk->sk_state) {
		case IUCV_DISCONN:
		case IUCV_CLOSING:
		case IUCV_CONNECTED:
165
			iucv_sever_path(sk, 0);
166 167 168 169 170 171 172 173
			break;
		case IUCV_OPEN:
		case IUCV_BOUND:
		case IUCV_LISTEN:
		case IUCV_CLOSED:
		default:
			break;
		}
174 175
		skb_queue_purge(&iucv->send_skb_q);
		skb_queue_purge(&iucv->backlog_skb_q);
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	}
	read_unlock(&iucv_sk_list.lock);
	return err;
}

/**
 * afiucv_pm_restore_thaw() - Thaw and restore PM callback
 * @dev:	AFIUCV dummy device
 *
 * socket clean up after freeze
 */
static int afiucv_pm_restore_thaw(struct device *dev)
{
	struct sock *sk;

#ifdef CONFIG_PM_DEBUG
	printk(KERN_WARNING "afiucv_pm_restore_thaw\n");
#endif
	read_lock(&iucv_sk_list.lock);
195
	sk_for_each(sk, &iucv_sk_list.head) {
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
		switch (sk->sk_state) {
		case IUCV_CONNECTED:
			sk->sk_err = EPIPE;
			sk->sk_state = IUCV_DISCONN;
			sk->sk_state_change(sk);
			break;
		case IUCV_DISCONN:
		case IUCV_CLOSING:
		case IUCV_LISTEN:
		case IUCV_BOUND:
		case IUCV_OPEN:
		default:
			break;
		}
	}
	read_unlock(&iucv_sk_list.lock);
	return 0;
}

215
static const struct dev_pm_ops afiucv_pm_ops = {
216 217 218 219 220 221 222 223 224 225
	.prepare = afiucv_pm_prepare,
	.complete = afiucv_pm_complete,
	.freeze = afiucv_pm_freeze,
	.thaw = afiucv_pm_restore_thaw,
	.restore = afiucv_pm_restore_thaw,
};

static struct device_driver af_iucv_driver = {
	.owner = THIS_MODULE,
	.name = "afiucv",
226
	.bus  = NULL,
227 228 229 230 231 232
	.pm   = &afiucv_pm_ops,
};

/* dummy device used as trigger for PM functions */
static struct device *af_iucv_dev;

233 234 235 236 237 238 239 240 241 242 243 244
/**
 * iucv_msg_length() - Returns the length of an iucv message.
 * @msg:	Pointer to struct iucv_message, MUST NOT be NULL
 *
 * The function returns the length of the specified iucv message @msg of data
 * stored in a buffer and of data stored in the parameter list (PRMDATA).
 *
 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
 * data:
 *	PRMDATA[0..6]	socket data (max 7 bytes);
 *	PRMDATA[7]	socket data length value (len is 0xff - PRMDATA[7])
 *
L
Lucas De Marchi 已提交
245
 * The socket data length is computed by subtracting the socket data length
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
 * value from 0xFF.
 * If the socket data len is greater 7, then PRMDATA can be used for special
 * notifications (see iucv_sock_shutdown); and further,
 * if the socket data len is > 7, the function returns 8.
 *
 * Use this function to allocate socket buffers to store iucv message data.
 */
static inline size_t iucv_msg_length(struct iucv_message *msg)
{
	size_t datalen;

	if (msg->flags & IUCV_IPRMDATA) {
		datalen = 0xff - msg->rmmsg[7];
		return (datalen < 8) ? datalen : 8;
	}
	return msg->length;
}

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
/**
 * iucv_sock_in_state() - check for specific states
 * @sk:		sock structure
 * @state:	first iucv sk state
 * @state:	second iucv sk state
 *
 * Returns true if the socket in either in the first or second state.
 */
static int iucv_sock_in_state(struct sock *sk, int state, int state2)
{
	return (sk->sk_state == state || sk->sk_state == state2);
}

/**
 * iucv_below_msglim() - function to check if messages can be sent
 * @sk:		sock structure
 *
 * Returns true if the send queue length is lower than the message limit.
 * Always returns true if the socket is not connected (no iucv path for
 * checking the message limit).
 */
static inline int iucv_below_msglim(struct sock *sk)
{
	struct iucv_sock *iucv = iucv_sk(sk);

	if (sk->sk_state != IUCV_CONNECTED)
		return 1;
291 292 293 294 295
	if (iucv->transport == AF_IUCV_TRANS_IUCV)
		return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim);
	else
		return ((atomic_read(&iucv->msg_sent) < iucv->msglimit_peer) &&
			(atomic_read(&iucv->pendings) <= 0));
296 297 298 299 300 301 302
}

/**
 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
 */
static void iucv_sock_wake_msglim(struct sock *sk)
{
303 304 305 306
	struct socket_wq *wq;

	rcu_read_lock();
	wq = rcu_dereference(sk->sk_wq);
H
Herbert Xu 已提交
307
	if (skwq_has_sleeper(wq))
308
		wake_up_interruptible_all(&wq->wait);
309
	sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
310
	rcu_read_unlock();
311 312
}

313 314 315 316 317 318 319 320 321 322 323 324
/**
 * afiucv_hs_send() - send a message through HiperSockets transport
 */
static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
		   struct sk_buff *skb, u8 flags)
{
	struct iucv_sock *iucv = iucv_sk(sock);
	struct af_iucv_trans_hdr *phs_hdr;
	struct sk_buff *nskb;
	int err, confirm_recv = 0;

	memset(skb->head, 0, ETH_HLEN);
325
	phs_hdr = skb_push(skb, sizeof(struct af_iucv_trans_hdr));
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
	skb_reset_mac_header(skb);
	skb_reset_network_header(skb);
	skb_push(skb, ETH_HLEN);
	skb_reset_mac_header(skb);
	memset(phs_hdr, 0, sizeof(struct af_iucv_trans_hdr));

	phs_hdr->magic = ETH_P_AF_IUCV;
	phs_hdr->version = 1;
	phs_hdr->flags = flags;
	if (flags == AF_IUCV_FLAG_SYN)
		phs_hdr->window = iucv->msglimit;
	else if ((flags == AF_IUCV_FLAG_WIN) || !flags) {
		confirm_recv = atomic_read(&iucv->msg_recv);
		phs_hdr->window = confirm_recv;
		if (confirm_recv)
			phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
	}
	memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
	memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
	memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
	memcpy(phs_hdr->srcAppName, iucv->src_name, 8);
	ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID));
	ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName));
	ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID));
	ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName));
	if (imsg)
		memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message));

354
	skb->dev = iucv->hs_dev;
355 356
	if (!skb->dev)
		return -ENODEV;
357
	if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev))
358 359 360 361 362 363 364
		return -ENETDOWN;
	if (skb->len > skb->dev->mtu) {
		if (sock->sk_type == SOCK_SEQPACKET)
			return -EMSGSIZE;
		else
			skb_trim(skb, skb->dev->mtu);
	}
365
	skb->protocol = cpu_to_be16(ETH_P_AF_IUCV);
366 367 368 369 370
	nskb = skb_clone(skb, GFP_ATOMIC);
	if (!nskb)
		return -ENOMEM;
	skb_queue_tail(&iucv->send_skb_q, nskb);
	err = dev_queue_xmit(skb);
371
	if (net_xmit_eval(err)) {
372 373 374 375 376 377
		skb_unlink(nskb, &iucv->send_skb_q);
		kfree_skb(nskb);
	} else {
		atomic_sub(confirm_recv, &iucv->msg_recv);
		WARN_ON(atomic_read(&iucv->msg_recv) < 0);
	}
378
	return net_xmit_eval(err);
379 380
}

381 382 383 384
static struct sock *__iucv_get_sock_by_name(char *nm)
{
	struct sock *sk;

385
	sk_for_each(sk, &iucv_sk_list.head)
386 387 388 389 390 391 392 393 394
		if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
			return sk;

	return NULL;
}

static void iucv_sock_destruct(struct sock *sk)
{
	skb_queue_purge(&sk->sk_receive_queue);
395 396 397 398 399 400 401 402 403 404
	skb_queue_purge(&sk->sk_error_queue);

	sk_mem_reclaim(sk);

	if (!sock_flag(sk, SOCK_DEAD)) {
		pr_err("Attempt to release alive iucv socket %p\n", sk);
		return;
	}

	WARN_ON(atomic_read(&sk->sk_rmem_alloc));
405
	WARN_ON(refcount_read(&sk->sk_wmem_alloc));
406 407
	WARN_ON(sk->sk_wmem_queued);
	WARN_ON(sk->sk_forward_alloc);
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
}

/* Cleanup Listen */
static void iucv_sock_cleanup_listen(struct sock *parent)
{
	struct sock *sk;

	/* Close non-accepted connections */
	while ((sk = iucv_accept_dequeue(parent, NULL))) {
		iucv_sock_close(sk);
		iucv_sock_kill(sk);
	}

	parent->sk_state = IUCV_CLOSED;
}

424
/* Kill socket (only if zapped and orphaned) */
425 426 427 428 429 430 431 432 433 434
static void iucv_sock_kill(struct sock *sk)
{
	if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
		return;

	iucv_sock_unlink(&iucv_sk_list, sk);
	sock_set_flag(sk, SOCK_DEAD);
	sock_put(sk);
}

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
/* Terminate an IUCV path */
static void iucv_sever_path(struct sock *sk, int with_user_data)
{
	unsigned char user_data[16];
	struct iucv_sock *iucv = iucv_sk(sk);
	struct iucv_path *path = iucv->path;

	if (iucv->path) {
		iucv->path = NULL;
		if (with_user_data) {
			low_nmcpy(user_data, iucv->src_name);
			high_nmcpy(user_data, iucv->dst_name);
			ASCEBC(user_data, sizeof(user_data));
			pr_iucv->path_sever(path, user_data);
		} else
			pr_iucv->path_sever(path, NULL);
		iucv_path_free(path);
	}
}

455
/* Send controlling flags through an IUCV socket for HIPER transport */
U
Ursula Braun 已提交
456 457 458 459 460
static int iucv_send_ctrl(struct sock *sk, u8 flags)
{
	int err = 0;
	int blen;
	struct sk_buff *skb;
461
	u8 shutdown = 0;
U
Ursula Braun 已提交
462 463

	blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
464 465 466 467 468
	if (sk->sk_shutdown & SEND_SHUTDOWN) {
		/* controlling flags should be sent anyway */
		shutdown = sk->sk_shutdown;
		sk->sk_shutdown &= RCV_SHUTDOWN;
	}
U
Ursula Braun 已提交
469 470 471 472 473
	skb = sock_alloc_send_skb(sk, blen, 1, &err);
	if (skb) {
		skb_reserve(skb, blen);
		err = afiucv_hs_send(NULL, sk, skb, flags);
	}
474 475
	if (shutdown)
		sk->sk_shutdown = shutdown;
U
Ursula Braun 已提交
476 477 478
	return err;
}

479 480 481 482
/* Close an IUCV socket */
static void iucv_sock_close(struct sock *sk)
{
	struct iucv_sock *iucv = iucv_sk(sk);
483
	unsigned long timeo;
484
	int err = 0;
485 486 487

	lock_sock(sk);

488
	switch (sk->sk_state) {
489 490 491 492 493
	case IUCV_LISTEN:
		iucv_sock_cleanup_listen(sk);
		break;

	case IUCV_CONNECTED:
494
		if (iucv->transport == AF_IUCV_TRANS_HIPER) {
U
Ursula Braun 已提交
495
			err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
496 497 498
			sk->sk_state = IUCV_DISCONN;
			sk->sk_state_change(sk);
		}
499
	case IUCV_DISCONN:   /* fall through */
500 501 502
		sk->sk_state = IUCV_CLOSING;
		sk->sk_state_change(sk);

503
		if (!err && !skb_queue_empty(&iucv->send_skb_q)) {
504 505 506 507
			if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
				timeo = sk->sk_lingertime;
			else
				timeo = IUCV_DISCONN_TIMEOUT;
508
			iucv_sock_wait(sk,
509 510
					iucv_sock_in_state(sk, IUCV_CLOSED, 0),
					timeo);
511 512
		}

513
	case IUCV_CLOSING:   /* fall through */
514 515 516
		sk->sk_state = IUCV_CLOSED;
		sk->sk_state_change(sk);

517 518 519
		sk->sk_err = ECONNRESET;
		sk->sk_state_change(sk);

520
		skb_queue_purge(&iucv->send_skb_q);
521
		skb_queue_purge(&iucv->backlog_skb_q);
522

523 524
	default:   /* fall through */
		iucv_sever_path(sk, 1);
525
	}
526

527 528 529 530 531 532
	if (iucv->hs_dev) {
		dev_put(iucv->hs_dev);
		iucv->hs_dev = NULL;
		sk->sk_bound_dev_if = 0;
	}

533 534 535
	/* mark socket for deletion by iucv_sock_kill() */
	sock_set_flag(sk, SOCK_ZAPPED);

536 537 538 539 540
	release_sock(sk);
}

static void iucv_sock_init(struct sock *sk, struct sock *parent)
{
541
	if (parent) {
542
		sk->sk_type = parent->sk_type;
543 544
		security_sk_clone(parent, sk);
	}
545 546
}

547
static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio, int kern)
548 549
{
	struct sock *sk;
550
	struct iucv_sock *iucv;
551

552
	sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, kern);
553 554
	if (!sk)
		return NULL;
555
	iucv = iucv_sk(sk);
556 557

	sock_init_data(sock, sk);
558 559 560 561 562 563 564
	INIT_LIST_HEAD(&iucv->accept_q);
	spin_lock_init(&iucv->accept_q_lock);
	skb_queue_head_init(&iucv->send_skb_q);
	INIT_LIST_HEAD(&iucv->message_q.list);
	spin_lock_init(&iucv->message_q.lock);
	skb_queue_head_init(&iucv->backlog_skb_q);
	iucv->send_tag = 0;
565
	atomic_set(&iucv->pendings, 0);
566
	iucv->flags = 0;
567 568 569
	iucv->msglimit = 0;
	atomic_set(&iucv->msg_sent, 0);
	atomic_set(&iucv->msg_recv, 0);
570
	iucv->path = NULL;
571
	iucv->sk_txnotify = afiucv_hs_callback_txnotify;
572
	memset(&iucv->src_user_id , 0, 32);
573 574 575 576
	if (pr_iucv)
		iucv->transport = AF_IUCV_TRANS_IUCV;
	else
		iucv->transport = AF_IUCV_TRANS_HIPER;
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591

	sk->sk_destruct = iucv_sock_destruct;
	sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
	sk->sk_allocation = GFP_DMA;

	sock_reset_flag(sk, SOCK_ZAPPED);

	sk->sk_protocol = proto;
	sk->sk_state	= IUCV_OPEN;

	iucv_sock_link(&iucv_sk_list, sk);
	return sk;
}

/* Create an IUCV socket */
592 593
static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
			    int kern)
594 595 596
{
	struct sock *sk;

597 598
	if (protocol && protocol != PF_IUCV)
		return -EPROTONOSUPPORT;
599 600

	sock->state = SS_UNCONNECTED;
601 602 603 604 605 606 607 608 609 610 611 612

	switch (sock->type) {
	case SOCK_STREAM:
		sock->ops = &iucv_sock_ops;
		break;
	case SOCK_SEQPACKET:
		/* currently, proto ops can handle both sk types */
		sock->ops = &iucv_sock_ops;
		break;
	default:
		return -ESOCKTNOSUPPORT;
	}
613

614
	sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL, kern);
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
	if (!sk)
		return -ENOMEM;

	iucv_sock_init(sk, NULL);

	return 0;
}

void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
{
	write_lock_bh(&l->lock);
	sk_add_node(sk, &l->head);
	write_unlock_bh(&l->lock);
}

void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
{
	write_lock_bh(&l->lock);
	sk_del_node_init(sk);
	write_unlock_bh(&l->lock);
}

void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
{
639 640 641
	unsigned long flags;
	struct iucv_sock *par = iucv_sk(parent);

642
	sock_hold(sk);
643 644 645
	spin_lock_irqsave(&par->accept_q_lock, flags);
	list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
	spin_unlock_irqrestore(&par->accept_q_lock, flags);
646
	iucv_sk(sk)->parent = parent;
647
	sk_acceptq_added(parent);
648 649 650 651
}

void iucv_accept_unlink(struct sock *sk)
{
652 653 654 655
	unsigned long flags;
	struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);

	spin_lock_irqsave(&par->accept_q_lock, flags);
656
	list_del_init(&iucv_sk(sk)->accept_q);
657
	spin_unlock_irqrestore(&par->accept_q_lock, flags);
658
	sk_acceptq_removed(iucv_sk(sk)->parent);
659 660 661 662 663 664 665 666 667
	iucv_sk(sk)->parent = NULL;
	sock_put(sk);
}

struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
{
	struct iucv_sock *isk, *n;
	struct sock *sk;

668
	list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
669 670 671 672 673
		sk = (struct sock *) isk;
		lock_sock(sk);

		if (sk->sk_state == IUCV_CLOSED) {
			iucv_accept_unlink(sk);
674
			release_sock(sk);
675 676 677 678
			continue;
		}

		if (sk->sk_state == IUCV_CONNECTED ||
679
		    sk->sk_state == IUCV_DISCONN ||
680 681 682 683 684 685 686 687 688 689 690 691 692 693
		    !newsock) {
			iucv_accept_unlink(sk);
			if (newsock)
				sock_graft(sk, newsock);

			release_sock(sk);
			return sk;
		}

		release_sock(sk);
	}
	return NULL;
}

694 695 696 697 698 699 700 701 702 703 704 705
static void __iucv_auto_name(struct iucv_sock *iucv)
{
	char name[12];

	sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
	while (__iucv_get_sock_by_name(name)) {
		sprintf(name, "%08x",
			atomic_inc_return(&iucv_sk_list.autobind_name));
	}
	memcpy(iucv->src_name, name, 8);
}

706 707 708 709 710 711 712
/* Bind an unbound socket */
static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
			  int addr_len)
{
	struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
	struct sock *sk = sock->sk;
	struct iucv_sock *iucv;
713 714 715
	int err = 0;
	struct net_device *dev;
	char uid[9];
716 717

	/* Verify the input sockaddr */
718 719
	if (addr_len < sizeof(struct sockaddr_iucv) ||
	    addr->sa_family != AF_IUCV)
720 721
		return -EINVAL;

722 723 724 725 726 727 728 729 730 731 732 733 734
	lock_sock(sk);
	if (sk->sk_state != IUCV_OPEN) {
		err = -EBADFD;
		goto done;
	}

	write_lock_bh(&iucv_sk_list.lock);

	iucv = iucv_sk(sk);
	if (__iucv_get_sock_by_name(sa->siucv_name)) {
		err = -EADDRINUSE;
		goto done_unlock;
	}
735
	if (iucv->path)
736 737 738
		goto done_unlock;

	/* Bind the socket */
739 740 741
	if (pr_iucv)
		if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
			goto vm_bind; /* VM IUCV transport */
742

743 744 745 746 747 748 749
	/* try hiper transport */
	memcpy(uid, sa->siucv_user_id, sizeof(uid));
	ASCEBC(uid, 8);
	rcu_read_lock();
	for_each_netdev_rcu(&init_net, dev) {
		if (!memcmp(dev->perm_addr, uid, 8)) {
			memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
750 751 752 753 754
			/* Check for unitialized siucv_name */
			if (strncmp(sa->siucv_name, "        ", 8) == 0)
				__iucv_auto_name(iucv);
			else
				memcpy(iucv->src_name, sa->siucv_name, 8);
755
			sk->sk_bound_dev_if = dev->ifindex;
756 757
			iucv->hs_dev = dev;
			dev_hold(dev);
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
			sk->sk_state = IUCV_BOUND;
			iucv->transport = AF_IUCV_TRANS_HIPER;
			if (!iucv->msglimit)
				iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
			rcu_read_unlock();
			goto done_unlock;
		}
	}
	rcu_read_unlock();
vm_bind:
	if (pr_iucv) {
		/* use local userid for backward compat */
		memcpy(iucv->src_name, sa->siucv_name, 8);
		memcpy(iucv->src_user_id, iucv_userid, 8);
		sk->sk_state = IUCV_BOUND;
		iucv->transport = AF_IUCV_TRANS_IUCV;
		if (!iucv->msglimit)
			iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
		goto done_unlock;
	}
	/* found no dev to bind */
	err = -ENODEV;
780 781 782 783 784 785 786 787 788 789 790 791 792 793
done_unlock:
	/* Release the socket list lock */
	write_unlock_bh(&iucv_sk_list.lock);
done:
	release_sock(sk);
	return err;
}

/* Automatically bind an unbound socket */
static int iucv_sock_autobind(struct sock *sk)
{
	struct iucv_sock *iucv = iucv_sk(sk);
	int err = 0;

794
	if (unlikely(!pr_iucv))
795 796
		return -EPROTO;

797
	memcpy(iucv->src_user_id, iucv_userid, 8);
798 799

	write_lock_bh(&iucv_sk_list.lock);
800
	__iucv_auto_name(iucv);
801 802
	write_unlock_bh(&iucv_sk_list.lock);

803 804 805
	if (!iucv->msglimit)
		iucv->msglimit = IUCV_QUEUELEN_DEFAULT;

806 807 808
	return err;
}

809
static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
810 811 812
{
	struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
	struct sock *sk = sock->sk;
813
	struct iucv_sock *iucv = iucv_sk(sk);
814 815 816 817
	unsigned char user_data[16];
	int err;

	high_nmcpy(user_data, sa->siucv_name);
818
	low_nmcpy(user_data, iucv->src_name);
819 820 821
	ASCEBC(user_data, sizeof(user_data));

	/* Create path. */
822
	iucv->path = iucv_path_alloc(iucv->msglimit,
823
				     IUCV_IPRMDATA, GFP_KERNEL);
824 825 826 827
	if (!iucv->path) {
		err = -ENOMEM;
		goto done;
	}
828 829 830
	err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
				    sa->siucv_user_id, NULL, user_data,
				    sk);
831 832 833
	if (err) {
		iucv_path_free(iucv->path);
		iucv->path = NULL;
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
		switch (err) {
		case 0x0b:	/* Target communicator is not logged on */
			err = -ENETUNREACH;
			break;
		case 0x0d:	/* Max connections for this guest exceeded */
		case 0x0e:	/* Max connections for target guest exceeded */
			err = -EAGAIN;
			break;
		case 0x0f:	/* Missing IUCV authorization */
			err = -EACCES;
			break;
		default:
			err = -ECONNREFUSED;
			break;
		}
849
	}
850 851 852
done:
	return err;
}
853

854 855 856 857 858 859 860 861 862
/* Connect an unconnected socket */
static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
			     int alen, int flags)
{
	struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
	struct sock *sk = sock->sk;
	struct iucv_sock *iucv = iucv_sk(sk);
	int err;

863
	if (alen < sizeof(struct sockaddr_iucv) || addr->sa_family != AF_IUCV)
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
		return -EINVAL;

	if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
		return -EBADFD;

	if (sk->sk_state == IUCV_OPEN &&
	    iucv->transport == AF_IUCV_TRANS_HIPER)
		return -EBADFD; /* explicit bind required */

	if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
		return -EINVAL;

	if (sk->sk_state == IUCV_OPEN) {
		err = iucv_sock_autobind(sk);
		if (unlikely(err))
			return err;
	}

	lock_sock(sk);

	/* Set the destination information */
	memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
	memcpy(iucv->dst_name, sa->siucv_name, 8);

	if (iucv->transport == AF_IUCV_TRANS_HIPER)
U
Ursula Braun 已提交
889
		err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
890 891 892 893 894 895
	else
		err = afiucv_path_connect(sock, addr);
	if (err)
		goto done;

	if (sk->sk_state != IUCV_CONNECTED)
896 897 898
		err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
							    IUCV_DISCONN),
				     sock_sndtimeo(sk, flags & O_NONBLOCK));
899

900
	if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
901
		err = -ECONNREFUSED;
902

903 904
	if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
		iucv_sever_path(sk, 0);
905

906 907 908 909 910 911 912 913 914 915 916 917 918 919
done:
	release_sock(sk);
	return err;
}

/* Move a socket into listening state. */
static int iucv_sock_listen(struct socket *sock, int backlog)
{
	struct sock *sk = sock->sk;
	int err;

	lock_sock(sk);

	err = -EINVAL;
920 921 922 923
	if (sk->sk_state != IUCV_BOUND)
		goto done;

	if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
924 925 926 927 928 929 930 931 932 933 934 935 936 937
		goto done;

	sk->sk_max_ack_backlog = backlog;
	sk->sk_ack_backlog = 0;
	sk->sk_state = IUCV_LISTEN;
	err = 0;

done:
	release_sock(sk);
	return err;
}

/* Accept a pending connection */
static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
938
			    int flags, bool kern)
939 940 941 942 943 944
{
	DECLARE_WAITQUEUE(wait, current);
	struct sock *sk = sock->sk, *nsk;
	long timeo;
	int err = 0;

945
	lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
946 947 948 949 950 951 952 953 954

	if (sk->sk_state != IUCV_LISTEN) {
		err = -EBADFD;
		goto done;
	}

	timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);

	/* Wait for an incoming connection */
E
Eric Dumazet 已提交
955
	add_wait_queue_exclusive(sk_sleep(sk), &wait);
956
	while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
957 958 959 960 961 962 963 964
		set_current_state(TASK_INTERRUPTIBLE);
		if (!timeo) {
			err = -EAGAIN;
			break;
		}

		release_sock(sk);
		timeo = schedule_timeout(timeo);
965
		lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
966 967 968 969 970 971 972 973 974 975 976 977 978

		if (sk->sk_state != IUCV_LISTEN) {
			err = -EBADFD;
			break;
		}

		if (signal_pending(current)) {
			err = sock_intr_errno(timeo);
			break;
		}
	}

	set_current_state(TASK_RUNNING);
E
Eric Dumazet 已提交
979
	remove_wait_queue(sk_sleep(sk), &wait);
980 981 982 983 984 985 986 987 988 989 990 991

	if (err)
		goto done;

	newsock->state = SS_CONNECTED;

done:
	release_sock(sk);
	return err;
}

static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
992
			     int peer)
993 994 995
{
	struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
	struct sock *sk = sock->sk;
996
	struct iucv_sock *iucv = iucv_sk(sk);
997 998 999 1000

	addr->sa_family = AF_IUCV;

	if (peer) {
1001 1002
		memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
		memcpy(siucv->siucv_name, iucv->dst_name, 8);
1003
	} else {
1004 1005
		memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
		memcpy(siucv->siucv_name, iucv->src_name, 8);
1006 1007 1008
	}
	memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
	memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
1009
	memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
1010

1011
	return sizeof(struct sockaddr_iucv);
1012 1013
}

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
/**
 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
 * @path:	IUCV path
 * @msg:	Pointer to a struct iucv_message
 * @skb:	The socket data to send, skb->len MUST BE <= 7
 *
 * Send the socket data in the parameter list in the iucv message
 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
 * list and the socket data len at index 7 (last byte).
 * See also iucv_msg_length().
 *
 * Returns the error code from the iucv_message_send() call.
 */
static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
			  struct sk_buff *skb)
{
	u8 prmdata[8];

	memcpy(prmdata, (void *) skb->data, skb->len);
	prmdata[7] = 0xff - (u8) skb->len;
1034
	return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
1035 1036 1037
				 (void *) prmdata, 8);
}

1038 1039
static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg,
			     size_t len)
1040 1041 1042
{
	struct sock *sk = sock->sk;
	struct iucv_sock *iucv = iucv_sk(sk);
1043 1044
	size_t headroom = 0;
	size_t linear;
1045
	struct sk_buff *skb;
1046
	struct iucv_message txmsg = {0};
1047 1048
	struct cmsghdr *cmsg;
	int cmsg_done;
1049
	long timeo;
1050 1051
	char user_id[9];
	char appl_id[9];
1052
	int err;
1053
	int noblock = msg->msg_flags & MSG_DONTWAIT;
1054 1055 1056 1057 1058 1059 1060 1061

	err = sock_error(sk);
	if (err)
		return err;

	if (msg->msg_flags & MSG_OOB)
		return -EOPNOTSUPP;

1062 1063 1064 1065
	/* SOCK_SEQPACKET: we do not support segmented records */
	if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
		return -EOPNOTSUPP;

1066 1067 1068 1069 1070 1071 1072
	lock_sock(sk);

	if (sk->sk_shutdown & SEND_SHUTDOWN) {
		err = -EPIPE;
		goto out;
	}

1073 1074 1075 1076 1077
	/* Return if the socket is not in connected state */
	if (sk->sk_state != IUCV_CONNECTED) {
		err = -ENOTCONN;
		goto out;
	}
1078

1079 1080 1081
	/* initialize defaults */
	cmsg_done   = 0;	/* check for duplicate headers */
	txmsg.class = 0;
1082

1083
	/* iterate over control messages */
1084
	for_each_cmsghdr(cmsg, msg) {
1085 1086 1087 1088
		if (!CMSG_OK(msg, cmsg)) {
			err = -EINVAL;
			goto out;
		}
1089

1090 1091
		if (cmsg->cmsg_level != SOL_IUCV)
			continue;
1092

1093 1094 1095 1096 1097
		if (cmsg->cmsg_type & cmsg_done) {
			err = -EINVAL;
			goto out;
		}
		cmsg_done |= cmsg->cmsg_type;
1098

1099 1100 1101
		switch (cmsg->cmsg_type) {
		case SCM_IUCV_TRGCLS:
			if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
1102 1103 1104 1105
				err = -EINVAL;
				goto out;
			}

1106 1107 1108
			/* set iucv message target class */
			memcpy(&txmsg.class,
				(void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
1109

1110
			break;
1111

1112 1113 1114
		default:
			err = -EINVAL;
			goto out;
1115
		}
1116
	}
1117

1118 1119 1120 1121
	/* allocate one skb for each iucv message:
	 * this is fine for SOCK_SEQPACKET (unless we want to support
	 * segmented records using the MSG_EOR flag), but
	 * for SOCK_STREAM we might want to improve it in future */
1122 1123
	if (iucv->transport == AF_IUCV_TRANS_HIPER) {
		headroom = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
1124 1125
		linear = len;
	} else {
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
		if (len < PAGE_SIZE) {
			linear = len;
		} else {
			/* In nonlinear "classic" iucv skb,
			 * reserve space for iucv_array
			 */
			headroom = sizeof(struct iucv_array) *
				   (MAX_SKB_FRAGS + 1);
			linear = PAGE_SIZE - headroom;
		}
1136 1137 1138
	}
	skb = sock_alloc_send_pskb(sk, headroom + linear, len - linear,
				   noblock, &err, 0);
1139
	if (!skb)
1140
		goto out;
1141 1142 1143 1144 1145 1146 1147
	if (headroom)
		skb_reserve(skb, headroom);
	skb_put(skb, linear);
	skb->len = len;
	skb->data_len = len - linear;
	err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
	if (err)
1148
		goto fail;
1149

1150 1151 1152 1153 1154
	/* wait if outstanding messages for iucv path has reached */
	timeo = sock_sndtimeo(sk, noblock);
	err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
	if (err)
		goto fail;
1155

1156 1157 1158 1159 1160
	/* return -ECONNRESET if the socket is no longer connected */
	if (sk->sk_state != IUCV_CONNECTED) {
		err = -ECONNRESET;
		goto fail;
	}
1161

1162 1163
	/* increment and save iucv message tag for msg_completion cbk */
	txmsg.tag = iucv->send_tag++;
1164
	IUCV_SKB_CB(skb)->tag = txmsg.tag;
1165

1166 1167 1168 1169 1170 1171 1172
	if (iucv->transport == AF_IUCV_TRANS_HIPER) {
		atomic_inc(&iucv->msg_sent);
		err = afiucv_hs_send(&txmsg, sk, skb, 0);
		if (err) {
			atomic_dec(&iucv->msg_sent);
			goto fail;
		}
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
	} else { /* Classic VM IUCV transport */
		skb_queue_tail(&iucv->send_skb_q, skb);

		if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags) &&
		    skb->len <= 7) {
			err = iucv_send_iprm(iucv->path, &txmsg, skb);

			/* on success: there is no message_complete callback */
			/* for an IPRMDATA msg; remove skb from send queue   */
			if (err == 0) {
				skb_unlink(skb, &iucv->send_skb_q);
				kfree_skb(skb);
			}
1186

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
			/* this error should never happen since the	*/
			/* IUCV_IPRMDATA path flag is set... sever path */
			if (err == 0x15) {
				pr_iucv->path_sever(iucv->path, NULL);
				skb_unlink(skb, &iucv->send_skb_q);
				err = -EPIPE;
				goto fail;
			}
		} else if (skb_is_nonlinear(skb)) {
			struct iucv_array *iba = (struct iucv_array *)skb->head;
			int i;

			/* skip iucv_array lying in the headroom */
			iba[0].address = (u32)(addr_t)skb->data;
			iba[0].length = (u32)skb_headlen(skb);
			for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
				skb_frag_t *frag = &skb_shinfo(skb)->frags[i];

				iba[i + 1].address =
					(u32)(addr_t)skb_frag_address(frag);
				iba[i + 1].length = (u32)skb_frag_size(frag);
			}
			err = pr_iucv->message_send(iucv->path, &txmsg,
						    IUCV_IPBUFLST, 0,
						    (void *)iba, skb->len);
		} else { /* non-IPRM Linear skb */
			err = pr_iucv->message_send(iucv->path, &txmsg,
					0, 0, (void *)skb->data, skb->len);
1215
		}
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
		if (err) {
			if (err == 3) {
				user_id[8] = 0;
				memcpy(user_id, iucv->dst_user_id, 8);
				appl_id[8] = 0;
				memcpy(appl_id, iucv->dst_name, 8);
				pr_err(
		"Application %s on z/VM guest %s exceeds message limit\n",
					appl_id, user_id);
				err = -EAGAIN;
			} else {
				err = -EPIPE;
			}
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
			skb_unlink(skb, &iucv->send_skb_q);
			goto fail;
		}
	}

	release_sock(sk);
	return len;

fail:
	kfree_skb(skb);
out:
	release_sock(sk);
	return err;
}

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
static struct sk_buff *alloc_iucv_recv_skb(unsigned long len)
{
	size_t headroom, linear;
	struct sk_buff *skb;
	int err;

	if (len < PAGE_SIZE) {
		headroom = 0;
		linear = len;
	} else {
		headroom = sizeof(struct iucv_array) * (MAX_SKB_FRAGS + 1);
		linear = PAGE_SIZE - headroom;
	}
	skb = alloc_skb_with_frags(headroom + linear, len - linear,
				   0, &err, GFP_ATOMIC | GFP_DMA);
	WARN_ONCE(!skb,
		  "alloc of recv iucv skb len=%lu failed with errcode=%d\n",
		  len, err);
	if (skb) {
		if (headroom)
			skb_reserve(skb, headroom);
		skb_put(skb, linear);
		skb->len = len;
		skb->data_len = len - linear;
	}
	return skb;
}

1272 1273 1274 1275
/* iucv_process_message() - Receive a single outstanding IUCV message
 *
 * Locking: must be called with message_q.lock held
 */
1276 1277 1278 1279 1280
static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
				 struct iucv_path *path,
				 struct iucv_message *msg)
{
	int rc;
1281 1282 1283
	unsigned int len;

	len = iucv_msg_length(msg);
1284

1285 1286
	/* store msg target class in the second 4 bytes of skb ctrl buffer */
	/* Note: the first 4 bytes are reserved for msg tag */
1287
	IUCV_SKB_CB(skb)->class = msg->class;
1288

1289 1290 1291 1292 1293 1294
	/* check for special IPRM messages (e.g. iucv_sock_shutdown) */
	if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
		if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
			skb->data = NULL;
			skb->len = 0;
		}
1295
	} else {
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
		if (skb_is_nonlinear(skb)) {
			struct iucv_array *iba = (struct iucv_array *)skb->head;
			int i;

			iba[0].address = (u32)(addr_t)skb->data;
			iba[0].length = (u32)skb_headlen(skb);
			for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
				skb_frag_t *frag = &skb_shinfo(skb)->frags[i];

				iba[i + 1].address =
					(u32)(addr_t)skb_frag_address(frag);
				iba[i + 1].length = (u32)skb_frag_size(frag);
			}
			rc = pr_iucv->message_receive(path, msg,
					      IUCV_IPBUFLST,
					      (void *)iba, len, NULL);
		} else {
			rc = pr_iucv->message_receive(path, msg,
1314 1315
					      msg->flags & IUCV_IPRMDATA,
					      skb->data, len, NULL);
1316
		}
1317 1318 1319 1320
		if (rc) {
			kfree_skb(skb);
			return;
		}
1321
		WARN_ON_ONCE(skb->len != len);
1322 1323
	}

1324
	IUCV_SKB_CB(skb)->offset = 0;
1325 1326 1327 1328 1329 1330 1331
	if (sk_filter(sk, skb)) {
		atomic_inc(&sk->sk_drops);	/* skb rejected by filter */
		kfree_skb(skb);
		return;
	}
	if (__sock_queue_rcv_skb(sk, skb))	/* handle rcv queue full */
		skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
1332 1333
}

1334 1335 1336 1337
/* iucv_process_message_q() - Process outstanding IUCV messages
 *
 * Locking: must be called with message_q.lock held
 */
1338 1339 1340 1341 1342 1343 1344
static void iucv_process_message_q(struct sock *sk)
{
	struct iucv_sock *iucv = iucv_sk(sk);
	struct sk_buff *skb;
	struct sock_msg_q *p, *n;

	list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
1345
		skb = alloc_iucv_recv_skb(iucv_msg_length(&p->msg));
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
		if (!skb)
			break;
		iucv_process_message(sk, skb, p->path, &p->msg);
		list_del(&p->list);
		kfree(p);
		if (!skb_queue_empty(&iucv->backlog_skb_q))
			break;
	}
}

1356 1357
static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
			     size_t len, int flags)
1358 1359 1360
{
	int noblock = flags & MSG_DONTWAIT;
	struct sock *sk = sock->sk;
1361
	struct iucv_sock *iucv = iucv_sk(sk);
1362
	unsigned int copied, rlen;
U
Ursula Braun 已提交
1363
	struct sk_buff *skb, *rskb, *cskb;
1364
	int err = 0;
1365
	u32 offset;
1366

1367
	if ((sk->sk_state == IUCV_DISCONN) &&
1368 1369 1370
	    skb_queue_empty(&iucv->backlog_skb_q) &&
	    skb_queue_empty(&sk->sk_receive_queue) &&
	    list_empty(&iucv->message_q.list))
1371 1372
		return 0;

1373 1374 1375
	if (flags & (MSG_OOB))
		return -EOPNOTSUPP;

1376 1377
	/* receive/dequeue next skb:
	 * the function understands MSG_PEEK and, thus, does not dequeue skb */
1378 1379 1380 1381 1382 1383 1384
	skb = skb_recv_datagram(sk, flags, noblock, &err);
	if (!skb) {
		if (sk->sk_shutdown & RCV_SHUTDOWN)
			return 0;
		return err;
	}

1385 1386
	offset = IUCV_SKB_CB(skb)->offset;
	rlen   = skb->len - offset;		/* real length of skb */
1387
	copied = min_t(unsigned int, rlen, len);
1388 1389
	if (!rlen)
		sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
1390

1391
	cskb = skb;
1392
	if (skb_copy_datagram_msg(cskb, offset, msg, copied)) {
1393 1394 1395
		if (!(flags & MSG_PEEK))
			skb_queue_head(&sk->sk_receive_queue, skb);
		return -EFAULT;
1396 1397
	}

1398 1399 1400 1401 1402 1403 1404
	/* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
	if (sk->sk_type == SOCK_SEQPACKET) {
		if (copied < rlen)
			msg->msg_flags |= MSG_TRUNC;
		/* each iucv message contains a complete record */
		msg->msg_flags |= MSG_EOR;
	}
1405

1406 1407 1408 1409
	/* create control message to store iucv msg target class:
	 * get the trgcls from the control buffer of the skb due to
	 * fragmentation of original iucv message. */
	err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1410 1411
		       sizeof(IUCV_SKB_CB(skb)->class),
		       (void *)&IUCV_SKB_CB(skb)->class);
1412 1413 1414 1415 1416 1417
	if (err) {
		if (!(flags & MSG_PEEK))
			skb_queue_head(&sk->sk_receive_queue, skb);
		return err;
	}

1418 1419 1420
	/* Mark read part of skb as used */
	if (!(flags & MSG_PEEK)) {

1421 1422
		/* SOCK_STREAM: re-queue skb if it contains unreceived data */
		if (sk->sk_type == SOCK_STREAM) {
1423 1424
			if (copied < rlen) {
				IUCV_SKB_CB(skb)->offset = offset + copied;
1425
				skb_queue_head(&sk->sk_receive_queue, skb);
1426 1427
				goto done;
			}
1428 1429 1430
		}

		kfree_skb(skb);
1431 1432 1433 1434 1435 1436 1437 1438
		if (iucv->transport == AF_IUCV_TRANS_HIPER) {
			atomic_inc(&iucv->msg_recv);
			if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
				WARN_ON(1);
				iucv_sock_close(sk);
				return -EFAULT;
			}
		}
1439 1440

		/* Queue backlog skbs */
1441
		spin_lock_bh(&iucv->message_q.lock);
1442
		rskb = skb_dequeue(&iucv->backlog_skb_q);
1443
		while (rskb) {
1444
			IUCV_SKB_CB(rskb)->offset = 0;
1445 1446
			if (__sock_queue_rcv_skb(sk, rskb)) {
				/* handle rcv queue full */
1447
				skb_queue_head(&iucv->backlog_skb_q,
1448 1449 1450
						rskb);
				break;
			}
1451
			rskb = skb_dequeue(&iucv->backlog_skb_q);
1452
		}
1453 1454 1455
		if (skb_queue_empty(&iucv->backlog_skb_q)) {
			if (!list_empty(&iucv->message_q.list))
				iucv_process_message_q(sk);
1456 1457
			if (atomic_read(&iucv->msg_recv) >=
							iucv->msglimit / 2) {
U
Ursula Braun 已提交
1458
				err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
1459 1460 1461 1462 1463
				if (err) {
					sk->sk_state = IUCV_DISCONN;
					sk->sk_state_change(sk);
				}
			}
1464
		}
1465
		spin_unlock_bh(&iucv->message_q.lock);
1466
	}
1467 1468

done:
1469 1470 1471 1472 1473
	/* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
	if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
		copied = rlen;

	return copied;
1474 1475
}

A
Al Viro 已提交
1476
static inline __poll_t iucv_accept_poll(struct sock *parent)
1477 1478 1479 1480
{
	struct iucv_sock *isk, *n;
	struct sock *sk;

1481
	list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
1482 1483 1484
		sk = (struct sock *) isk;

		if (sk->sk_state == IUCV_CONNECTED)
1485
			return EPOLLIN | EPOLLRDNORM;
1486 1487 1488 1489 1490
	}

	return 0;
}

1491 1492
__poll_t iucv_sock_poll(struct file *file, struct socket *sock,
			    poll_table *wait)
1493 1494
{
	struct sock *sk = sock->sk;
A
Al Viro 已提交
1495
	__poll_t mask = 0;
1496

1497 1498
	sock_poll_wait(file, sk_sleep(sk), wait);

1499 1500 1501 1502
	if (sk->sk_state == IUCV_LISTEN)
		return iucv_accept_poll(sk);

	if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1503 1504
		mask |= EPOLLERR |
			(sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
1505 1506

	if (sk->sk_shutdown & RCV_SHUTDOWN)
1507
		mask |= EPOLLRDHUP;
1508 1509

	if (sk->sk_shutdown == SHUTDOWN_MASK)
1510
		mask |= EPOLLHUP;
1511 1512

	if (!skb_queue_empty(&sk->sk_receive_queue) ||
1513
	    (sk->sk_shutdown & RCV_SHUTDOWN))
1514
		mask |= EPOLLIN | EPOLLRDNORM;
1515 1516

	if (sk->sk_state == IUCV_CLOSED)
1517
		mask |= EPOLLHUP;
1518

1519
	if (sk->sk_state == IUCV_DISCONN)
1520
		mask |= EPOLLIN;
1521

1522
	if (sock_writeable(sk) && iucv_below_msglim(sk))
1523
		mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
1524
	else
1525
		sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542

	return mask;
}

static int iucv_sock_shutdown(struct socket *sock, int how)
{
	struct sock *sk = sock->sk;
	struct iucv_sock *iucv = iucv_sk(sk);
	struct iucv_message txmsg;
	int err = 0;

	how++;

	if ((how & ~SHUTDOWN_MASK) || !how)
		return -EINVAL;

	lock_sock(sk);
1543
	switch (sk->sk_state) {
1544
	case IUCV_LISTEN:
1545 1546
	case IUCV_DISCONN:
	case IUCV_CLOSING:
1547 1548 1549 1550 1551 1552 1553 1554
	case IUCV_CLOSED:
		err = -ENOTCONN;
		goto fail;
	default:
		break;
	}

	if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
		if (iucv->transport == AF_IUCV_TRANS_IUCV) {
			txmsg.class = 0;
			txmsg.tag = 0;
			err = pr_iucv->message_send(iucv->path, &txmsg,
				IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8);
			if (err) {
				switch (err) {
				case 1:
					err = -ENOTCONN;
					break;
				case 2:
					err = -ECONNRESET;
					break;
				default:
					err = -ENOTCONN;
					break;
				}
1572
			}
1573 1574
		} else
			iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
1575 1576
	}

1577
	sk->sk_shutdown |= how;
1578
	if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
1579 1580
		if ((iucv->transport == AF_IUCV_TRANS_IUCV) &&
		    iucv->path) {
1581 1582 1583 1584 1585
			err = pr_iucv->path_quiesce(iucv->path, NULL);
			if (err)
				err = -ENOTCONN;
/*			skb_queue_purge(&sk->sk_receive_queue); */
		}
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
		skb_queue_purge(&sk->sk_receive_queue);
	}

	/* Wake up anyone sleeping in poll */
	sk->sk_state_change(sk);

fail:
	release_sock(sk);
	return err;
}

static int iucv_sock_release(struct socket *sock)
{
	struct sock *sk = sock->sk;
	int err = 0;

	if (!sk)
		return 0;

	iucv_sock_close(sk);

	sock_orphan(sk);
	iucv_sock_kill(sk);
	return err;
}

1612 1613
/* getsockopt and setsockopt */
static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
1614
				char __user *optval, unsigned int optlen)
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
{
	struct sock *sk = sock->sk;
	struct iucv_sock *iucv = iucv_sk(sk);
	int val;
	int rc;

	if (level != SOL_IUCV)
		return -ENOPROTOOPT;

	if (optlen < sizeof(int))
		return -EINVAL;

	if (get_user(val, (int __user *) optval))
		return -EFAULT;

	rc = 0;

	lock_sock(sk);
	switch (optname) {
	case SO_IPRMDATA_MSG:
		if (val)
			iucv->flags |= IUCV_IPRMDATA;
		else
			iucv->flags &= ~IUCV_IPRMDATA;
		break;
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
	case SO_MSGLIMIT:
		switch (sk->sk_state) {
		case IUCV_OPEN:
		case IUCV_BOUND:
			if (val < 1 || val > (u16)(~0))
				rc = -EINVAL;
			else
				iucv->msglimit = val;
			break;
		default:
			rc = -EINVAL;
			break;
		}
		break;
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
	default:
		rc = -ENOPROTOOPT;
		break;
	}
	release_sock(sk);

	return rc;
}

static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
				char __user *optval, int __user *optlen)
{
	struct sock *sk = sock->sk;
	struct iucv_sock *iucv = iucv_sk(sk);
1668 1669
	unsigned int val;
	int len;
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685

	if (level != SOL_IUCV)
		return -ENOPROTOOPT;

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

	if (len < 0)
		return -EINVAL;

	len = min_t(unsigned int, len, sizeof(int));

	switch (optname) {
	case SO_IPRMDATA_MSG:
		val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
		break;
1686 1687 1688 1689 1690 1691
	case SO_MSGLIMIT:
		lock_sock(sk);
		val = (iucv->path != NULL) ? iucv->path->msglim	/* connected */
					   : iucv->msglimit;	/* default */
		release_sock(sk);
		break;
1692 1693 1694 1695 1696 1697 1698
	case SO_MSGSIZE:
		if (sk->sk_state == IUCV_OPEN)
			return -EBADFD;
		val = (iucv->hs_dev) ? iucv->hs_dev->mtu -
				sizeof(struct af_iucv_trans_hdr) - ETH_HLEN :
				0x7fffffff;
		break;
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
	default:
		return -ENOPROTOOPT;
	}

	if (put_user(len, optlen))
		return -EFAULT;
	if (copy_to_user(optval, &val, len))
		return -EFAULT;

	return 0;
}


1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
/* Callback wrappers - called from iucv base support */
static int iucv_callback_connreq(struct iucv_path *path,
				 u8 ipvmid[8], u8 ipuser[16])
{
	unsigned char user_data[16];
	unsigned char nuser_data[16];
	unsigned char src_name[8];
	struct sock *sk, *nsk;
	struct iucv_sock *iucv, *niucv;
	int err;

	memcpy(src_name, ipuser, 8);
	EBCASC(src_name, 8);
	/* Find out if this path belongs to af_iucv. */
	read_lock(&iucv_sk_list.lock);
	iucv = NULL;
1728
	sk = NULL;
1729
	sk_for_each(sk, &iucv_sk_list.head)
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
		if (sk->sk_state == IUCV_LISTEN &&
		    !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
			/*
			 * Found a listening socket with
			 * src_name == ipuser[0-7].
			 */
			iucv = iucv_sk(sk);
			break;
		}
	read_unlock(&iucv_sk_list.lock);
	if (!iucv)
		/* No socket found, not one of our paths. */
		return -EINVAL;

	bh_lock_sock(sk);

	/* Check if parent socket is listening */
	low_nmcpy(user_data, iucv->src_name);
	high_nmcpy(user_data, iucv->dst_name);
	ASCEBC(user_data, sizeof(user_data));
	if (sk->sk_state != IUCV_LISTEN) {
1751
		err = pr_iucv->path_sever(path, user_data);
1752
		iucv_path_free(path);
1753 1754 1755 1756 1757
		goto fail;
	}

	/* Check for backlog size */
	if (sk_acceptq_is_full(sk)) {
1758
		err = pr_iucv->path_sever(path, user_data);
1759
		iucv_path_free(path);
1760 1761 1762 1763
		goto fail;
	}

	/* Create the new socket */
1764
	nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
1765
	if (!nsk) {
1766
		err = pr_iucv->path_sever(path, user_data);
1767
		iucv_path_free(path);
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
		goto fail;
	}

	niucv = iucv_sk(nsk);
	iucv_sock_init(nsk, sk);

	/* Set the new iucv_sock */
	memcpy(niucv->dst_name, ipuser + 8, 8);
	EBCASC(niucv->dst_name, 8);
	memcpy(niucv->dst_user_id, ipvmid, 8);
	memcpy(niucv->src_name, iucv->src_name, 8);
	memcpy(niucv->src_user_id, iucv->src_user_id, 8);
	niucv->path = path;

	/* Call iucv_accept */
	high_nmcpy(nuser_data, ipuser + 8);
	memcpy(nuser_data + 8, niucv->src_name, 8);
	ASCEBC(nuser_data + 8, 8);

1787 1788 1789
	/* set message limit for path based on msglimit of accepting socket */
	niucv->msglimit = iucv->msglimit;
	path->msglim = iucv->msglimit;
1790
	err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
1791
	if (err) {
1792
		iucv_sever_path(nsk, 1);
1793
		iucv_sock_kill(nsk);
1794 1795 1796 1797 1798 1799 1800
		goto fail;
	}

	iucv_accept_enqueue(sk, nsk);

	/* Wake up accept */
	nsk->sk_state = IUCV_CONNECTED;
1801
	sk->sk_data_ready(sk);
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
	err = 0;
fail:
	bh_unlock_sock(sk);
	return 0;
}

static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
{
	struct sock *sk = path->private;

	sk->sk_state = IUCV_CONNECTED;
	sk->sk_state_change(sk);
}

static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
{
	struct sock *sk = path->private;
1819
	struct iucv_sock *iucv = iucv_sk(sk);
1820 1821 1822
	struct sk_buff *skb;
	struct sock_msg_q *save_msg;
	int len;
1823

1824
	if (sk->sk_shutdown & RCV_SHUTDOWN) {
1825
		pr_iucv->message_reject(path, msg);
1826
		return;
1827
	}
1828

1829
	spin_lock(&iucv->message_q.lock);
1830

1831 1832 1833 1834 1835
	if (!list_empty(&iucv->message_q.list) ||
	    !skb_queue_empty(&iucv->backlog_skb_q))
		goto save_message;

	len = atomic_read(&sk->sk_rmem_alloc);
E
Eric Dumazet 已提交
1836
	len += SKB_TRUESIZE(iucv_msg_length(msg));
1837 1838 1839
	if (len > sk->sk_rcvbuf)
		goto save_message;

1840
	skb = alloc_iucv_recv_skb(iucv_msg_length(msg));
1841 1842
	if (!skb)
		goto save_message;
1843

1844
	iucv_process_message(sk, skb, path, msg);
1845
	goto out_unlock;
1846

1847 1848
save_message:
	save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
1849
	if (!save_msg)
J
Julia Lawall 已提交
1850
		goto out_unlock;
1851 1852
	save_msg->path = path;
	save_msg->msg = *msg;
1853

1854
	list_add_tail(&save_msg->list, &iucv->message_q.list);
1855 1856

out_unlock:
1857
	spin_unlock(&iucv->message_q.lock);
1858 1859 1860 1861 1862 1863
}

static void iucv_callback_txdone(struct iucv_path *path,
				 struct iucv_message *msg)
{
	struct sock *sk = path->private;
1864
	struct sk_buff *this = NULL;
1865 1866 1867 1868
	struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
	struct sk_buff *list_skb = list->next;
	unsigned long flags;

1869
	bh_lock_sock(sk);
1870
	if (!skb_queue_empty(list)) {
1871 1872
		spin_lock_irqsave(&list->lock, flags);

1873
		while (list_skb != (struct sk_buff *)list) {
1874
			if (msg->tag == IUCV_SKB_CB(list_skb)->tag) {
1875 1876 1877
				this = list_skb;
				break;
			}
1878
			list_skb = list_skb->next;
1879 1880 1881
		}
		if (this)
			__skb_unlink(this, list);
1882 1883

		spin_unlock_irqrestore(&list->lock, flags);
1884

1885 1886 1887 1888 1889
		if (this) {
			kfree_skb(this);
			/* wake up any process waiting for sending */
			iucv_sock_wake_msglim(sk);
		}
1890
	}
1891

1892
	if (sk->sk_state == IUCV_CLOSING) {
1893 1894 1895 1896 1897
		if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
			sk->sk_state = IUCV_CLOSED;
			sk->sk_state_change(sk);
		}
	}
1898
	bh_unlock_sock(sk);
1899 1900 1901 1902 1903 1904 1905

}

static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
{
	struct sock *sk = path->private;

1906 1907 1908 1909 1910
	if (sk->sk_state == IUCV_CLOSED)
		return;

	bh_lock_sock(sk);
	iucv_sever_path(sk, 1);
1911
	sk->sk_state = IUCV_DISCONN;
1912 1913

	sk->sk_state_change(sk);
1914
	bh_unlock_sock(sk);
1915 1916
}

1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
/* called if the other communication side shuts down its RECV direction;
 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
 */
static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
{
	struct sock *sk = path->private;

	bh_lock_sock(sk);
	if (sk->sk_state != IUCV_CLOSED) {
		sk->sk_shutdown |= SEND_SHUTDOWN;
		sk->sk_state_change(sk);
	}
	bh_unlock_sock(sk);
}

1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
/***************** HiperSockets transport callbacks ********************/
static void afiucv_swap_src_dest(struct sk_buff *skb)
{
	struct af_iucv_trans_hdr *trans_hdr =
				(struct af_iucv_trans_hdr *)skb->data;
	char tmpID[8];
	char tmpName[8];

	ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
	ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
	ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
	ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
	memcpy(tmpID, trans_hdr->srcUserID, 8);
	memcpy(tmpName, trans_hdr->srcAppName, 8);
	memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
	memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
	memcpy(trans_hdr->destUserID, tmpID, 8);
	memcpy(trans_hdr->destAppName, tmpName, 8);
	skb_push(skb, ETH_HLEN);
	memset(skb->data, 0, ETH_HLEN);
}

/**
 * afiucv_hs_callback_syn - react on received SYN
 **/
static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
{
	struct sock *nsk;
	struct iucv_sock *iucv, *niucv;
	struct af_iucv_trans_hdr *trans_hdr;
	int err;

	iucv = iucv_sk(sk);
	trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
	if (!iucv) {
		/* no sock - connection refused */
		afiucv_swap_src_dest(skb);
		trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
		err = dev_queue_xmit(skb);
		goto out;
	}

1974
	nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
1975 1976 1977 1978 1979 1980 1981 1982
	bh_lock_sock(sk);
	if ((sk->sk_state != IUCV_LISTEN) ||
	    sk_acceptq_is_full(sk) ||
	    !nsk) {
		/* error on server socket - connection refused */
		afiucv_swap_src_dest(skb);
		trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
		err = dev_queue_xmit(skb);
1983
		iucv_sock_kill(nsk);
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
		bh_unlock_sock(sk);
		goto out;
	}

	niucv = iucv_sk(nsk);
	iucv_sock_init(nsk, sk);
	niucv->transport = AF_IUCV_TRANS_HIPER;
	niucv->msglimit = iucv->msglimit;
	if (!trans_hdr->window)
		niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
	else
		niucv->msglimit_peer = trans_hdr->window;
	memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
	memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
	memcpy(niucv->src_name, iucv->src_name, 8);
	memcpy(niucv->src_user_id, iucv->src_user_id, 8);
	nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
2001 2002
	niucv->hs_dev = iucv->hs_dev;
	dev_hold(niucv->hs_dev);
2003 2004 2005 2006 2007 2008 2009 2010
	afiucv_swap_src_dest(skb);
	trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
	trans_hdr->window = niucv->msglimit;
	/* if receiver acks the xmit connection is established */
	err = dev_queue_xmit(skb);
	if (!err) {
		iucv_accept_enqueue(sk, nsk);
		nsk->sk_state = IUCV_CONNECTED;
2011
		sk->sk_data_ready(sk);
2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
	} else
		iucv_sock_kill(nsk);
	bh_unlock_sock(sk);

out:
	return NET_RX_SUCCESS;
}

/**
 * afiucv_hs_callback_synack() - react on received SYN-ACK
 **/
static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
{
	struct iucv_sock *iucv = iucv_sk(sk);
	struct af_iucv_trans_hdr *trans_hdr =
					(struct af_iucv_trans_hdr *)skb->data;

	if (!iucv)
		goto out;
	if (sk->sk_state != IUCV_BOUND)
		goto out;
	bh_lock_sock(sk);
	iucv->msglimit_peer = trans_hdr->window;
	sk->sk_state = IUCV_CONNECTED;
	sk->sk_state_change(sk);
	bh_unlock_sock(sk);
out:
	kfree_skb(skb);
	return NET_RX_SUCCESS;
}

/**
 * afiucv_hs_callback_synfin() - react on received SYN_FIN
 **/
static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
{
	struct iucv_sock *iucv = iucv_sk(sk);

	if (!iucv)
		goto out;
	if (sk->sk_state != IUCV_BOUND)
		goto out;
	bh_lock_sock(sk);
	sk->sk_state = IUCV_DISCONN;
	sk->sk_state_change(sk);
	bh_unlock_sock(sk);
out:
	kfree_skb(skb);
	return NET_RX_SUCCESS;
}

/**
 * afiucv_hs_callback_fin() - react on received FIN
 **/
static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
{
	struct iucv_sock *iucv = iucv_sk(sk);

	/* other end of connection closed */
2071 2072 2073 2074
	if (!iucv)
		goto out;
	bh_lock_sock(sk);
	if (sk->sk_state == IUCV_CONNECTED) {
2075
		sk->sk_state = IUCV_DISCONN;
2076 2077
		sk->sk_state_change(sk);
	}
2078 2079
	bh_unlock_sock(sk);
out:
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
	kfree_skb(skb);
	return NET_RX_SUCCESS;
}

/**
 * afiucv_hs_callback_win() - react on received WIN
 **/
static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
{
	struct iucv_sock *iucv = iucv_sk(sk);
	struct af_iucv_trans_hdr *trans_hdr =
					(struct af_iucv_trans_hdr *)skb->data;

	if (!iucv)
		return NET_RX_SUCCESS;

	if (sk->sk_state != IUCV_CONNECTED)
		return NET_RX_SUCCESS;

	atomic_sub(trans_hdr->window, &iucv->msg_sent);
	iucv_sock_wake_msglim(sk);
	return NET_RX_SUCCESS;
}

/**
 * afiucv_hs_callback_rx() - react on received data
 **/
static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
{
	struct iucv_sock *iucv = iucv_sk(sk);

	if (!iucv) {
		kfree_skb(skb);
		return NET_RX_SUCCESS;
	}

	if (sk->sk_state != IUCV_CONNECTED) {
		kfree_skb(skb);
		return NET_RX_SUCCESS;
	}

2121 2122 2123 2124 2125
	if (sk->sk_shutdown & RCV_SHUTDOWN) {
		kfree_skb(skb);
		return NET_RX_SUCCESS;
	}

2126
	/* write stuff from iucv_msg to skb cb */
2127 2128 2129
	skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
	skb_reset_transport_header(skb);
	skb_reset_network_header(skb);
2130
	IUCV_SKB_CB(skb)->offset = 0;
2131 2132 2133 2134 2135 2136
	if (sk_filter(sk, skb)) {
		atomic_inc(&sk->sk_drops);	/* skb rejected by filter */
		kfree_skb(skb);
		return NET_RX_SUCCESS;
	}

2137 2138
	spin_lock(&iucv->message_q.lock);
	if (skb_queue_empty(&iucv->backlog_skb_q)) {
2139
		if (__sock_queue_rcv_skb(sk, skb))
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
			/* handle rcv queue full */
			skb_queue_tail(&iucv->backlog_skb_q, skb);
	} else
		skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
	spin_unlock(&iucv->message_q.lock);
	return NET_RX_SUCCESS;
}

/**
 * afiucv_hs_rcv() - base function for arriving data through HiperSockets
 *                   transport
 *                   called from netif RX softirq
 **/
static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
	struct packet_type *pt, struct net_device *orig_dev)
{
	struct sock *sk;
	struct iucv_sock *iucv;
	struct af_iucv_trans_hdr *trans_hdr;
	char nullstring[8];
	int err = 0;

2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
	if (skb->len < (ETH_HLEN + sizeof(struct af_iucv_trans_hdr))) {
		WARN_ONCE(1, "AF_IUCV too short skb, len=%d, min=%d",
			  (int)skb->len,
			  (int)(ETH_HLEN + sizeof(struct af_iucv_trans_hdr)));
		kfree_skb(skb);
		return NET_RX_SUCCESS;
	}
	if (skb_headlen(skb) < (ETH_HLEN + sizeof(struct af_iucv_trans_hdr)))
		if (skb_linearize(skb)) {
			WARN_ONCE(1, "AF_IUCV skb_linearize failed, len=%d",
				  (int)skb->len);
			kfree_skb(skb);
			return NET_RX_SUCCESS;
		}
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185
	skb_pull(skb, ETH_HLEN);
	trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
	EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
	EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
	EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
	EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
	memset(nullstring, 0, sizeof(nullstring));
	iucv = NULL;
	sk = NULL;
	read_lock(&iucv_sk_list.lock);
2186
	sk_for_each(sk, &iucv_sk_list.head) {
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243
		if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
			if ((!memcmp(&iucv_sk(sk)->src_name,
				     trans_hdr->destAppName, 8)) &&
			    (!memcmp(&iucv_sk(sk)->src_user_id,
				     trans_hdr->destUserID, 8)) &&
			    (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
			    (!memcmp(&iucv_sk(sk)->dst_user_id,
				     nullstring, 8))) {
				iucv = iucv_sk(sk);
				break;
			}
		} else {
			if ((!memcmp(&iucv_sk(sk)->src_name,
				     trans_hdr->destAppName, 8)) &&
			    (!memcmp(&iucv_sk(sk)->src_user_id,
				     trans_hdr->destUserID, 8)) &&
			    (!memcmp(&iucv_sk(sk)->dst_name,
				     trans_hdr->srcAppName, 8)) &&
			    (!memcmp(&iucv_sk(sk)->dst_user_id,
				     trans_hdr->srcUserID, 8))) {
				iucv = iucv_sk(sk);
				break;
			}
		}
	}
	read_unlock(&iucv_sk_list.lock);
	if (!iucv)
		sk = NULL;

	/* no sock
	how should we send with no sock
	1) send without sock no send rc checking?
	2) introduce default sock to handle this cases

	 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
	 data -> send FIN
	 SYN|ACK, SYN|FIN, FIN -> no action? */

	switch (trans_hdr->flags) {
	case AF_IUCV_FLAG_SYN:
		/* connect request */
		err = afiucv_hs_callback_syn(sk, skb);
		break;
	case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
		/* connect request confirmed */
		err = afiucv_hs_callback_synack(sk, skb);
		break;
	case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
		/* connect request refused */
		err = afiucv_hs_callback_synfin(sk, skb);
		break;
	case (AF_IUCV_FLAG_FIN):
		/* close request */
		err = afiucv_hs_callback_fin(sk, skb);
		break;
	case (AF_IUCV_FLAG_WIN):
		err = afiucv_hs_callback_win(sk, skb);
2244 2245 2246 2247
		if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
			kfree_skb(skb);
			break;
		}
2248 2249 2250 2251
		/* fall through and receive non-zero length data */
	case (AF_IUCV_FLAG_SHT):
		/* shutdown request */
		/* fall through and receive zero length data */
2252 2253
	case 0:
		/* plain data frame */
2254
		IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275
		err = afiucv_hs_callback_rx(sk, skb);
		break;
	default:
		;
	}

	return err;
}

/**
 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
 *                                 transport
 **/
static void afiucv_hs_callback_txnotify(struct sk_buff *skb,
					enum iucv_tx_notify n)
{
	struct sock *isk = skb->sk;
	struct sock *sk = NULL;
	struct iucv_sock *iucv = NULL;
	struct sk_buff_head *list;
	struct sk_buff *list_skb;
2276
	struct sk_buff *nskb;
2277 2278
	unsigned long flags;

2279
	read_lock_irqsave(&iucv_sk_list.lock, flags);
2280
	sk_for_each(sk, &iucv_sk_list.head)
2281 2282 2283 2284
		if (sk == isk) {
			iucv = iucv_sk(sk);
			break;
		}
2285
	read_unlock_irqrestore(&iucv_sk_list.lock, flags);
2286

2287
	if (!iucv || sock_flag(sk, SOCK_ZAPPED))
2288 2289 2290
		return;

	list = &iucv->send_skb_q;
2291
	spin_lock_irqsave(&list->lock, flags);
2292 2293
	if (skb_queue_empty(list))
		goto out_unlock;
2294 2295
	list_skb = list->next;
	nskb = list_skb->next;
2296 2297 2298 2299
	while (list_skb != (struct sk_buff *)list) {
		if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
			switch (n) {
			case TX_NOTIFY_OK:
2300 2301
				__skb_unlink(list_skb, list);
				kfree_skb(list_skb);
2302 2303 2304 2305 2306 2307
				iucv_sock_wake_msglim(sk);
				break;
			case TX_NOTIFY_PENDING:
				atomic_inc(&iucv->pendings);
				break;
			case TX_NOTIFY_DELAYED_OK:
2308
				__skb_unlink(list_skb, list);
2309 2310 2311
				atomic_dec(&iucv->pendings);
				if (atomic_read(&iucv->pendings) <= 0)
					iucv_sock_wake_msglim(sk);
2312
				kfree_skb(list_skb);
2313 2314 2315 2316 2317 2318
				break;
			case TX_NOTIFY_UNREACHABLE:
			case TX_NOTIFY_DELAYED_UNREACHABLE:
			case TX_NOTIFY_TPQFULL: /* not yet used */
			case TX_NOTIFY_GENERALERROR:
			case TX_NOTIFY_DELAYED_GENERALERROR:
2319 2320 2321 2322 2323 2324
				__skb_unlink(list_skb, list);
				kfree_skb(list_skb);
				if (sk->sk_state == IUCV_CONNECTED) {
					sk->sk_state = IUCV_DISCONN;
					sk->sk_state_change(sk);
				}
2325 2326 2327 2328
				break;
			}
			break;
		}
2329 2330
		list_skb = nskb;
		nskb = nskb->next;
2331
	}
2332
out_unlock:
2333 2334
	spin_unlock_irqrestore(&list->lock, flags);

2335 2336 2337 2338 2339 2340 2341
	if (sk->sk_state == IUCV_CLOSING) {
		if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
			sk->sk_state = IUCV_CLOSED;
			sk->sk_state_change(sk);
		}
	}

2342
}
U
Ursula Braun 已提交
2343 2344 2345 2346 2347 2348 2349

/*
 * afiucv_netdev_event: handle netdev notifier chain events
 */
static int afiucv_netdev_event(struct notifier_block *this,
			       unsigned long event, void *ptr)
{
2350
	struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
U
Ursula Braun 已提交
2351 2352 2353 2354 2355 2356
	struct sock *sk;
	struct iucv_sock *iucv;

	switch (event) {
	case NETDEV_REBOOT:
	case NETDEV_GOING_DOWN:
2357
		sk_for_each(sk, &iucv_sk_list.head) {
U
Ursula Braun 已提交
2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379
			iucv = iucv_sk(sk);
			if ((iucv->hs_dev == event_dev) &&
			    (sk->sk_state == IUCV_CONNECTED)) {
				if (event == NETDEV_GOING_DOWN)
					iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
				sk->sk_state = IUCV_DISCONN;
				sk->sk_state_change(sk);
			}
		}
		break;
	case NETDEV_DOWN:
	case NETDEV_UNREGISTER:
	default:
		break;
	}
	return NOTIFY_DONE;
}

static struct notifier_block afiucv_netdev_notifier = {
	.notifier_call = afiucv_netdev_event,
};

2380
static const struct proto_ops iucv_sock_ops = {
2381 2382 2383 2384 2385 2386 2387 2388 2389 2390
	.family		= PF_IUCV,
	.owner		= THIS_MODULE,
	.release	= iucv_sock_release,
	.bind		= iucv_sock_bind,
	.connect	= iucv_sock_connect,
	.listen		= iucv_sock_listen,
	.accept		= iucv_sock_accept,
	.getname	= iucv_sock_getname,
	.sendmsg	= iucv_sock_sendmsg,
	.recvmsg	= iucv_sock_recvmsg,
2391
	.poll		= iucv_sock_poll,
2392 2393 2394 2395
	.ioctl		= sock_no_ioctl,
	.mmap		= sock_no_mmap,
	.socketpair	= sock_no_socketpair,
	.shutdown	= iucv_sock_shutdown,
2396 2397
	.setsockopt	= iucv_sock_setsockopt,
	.getsockopt	= iucv_sock_getsockopt,
2398 2399
};

2400
static const struct net_proto_family iucv_sock_family_ops = {
2401 2402 2403 2404 2405
	.family	= AF_IUCV,
	.owner	= THIS_MODULE,
	.create	= iucv_sock_create,
};

2406 2407 2408 2409 2410 2411
static struct packet_type iucv_packet_type = {
	.type = cpu_to_be16(ETH_P_AF_IUCV),
	.func = afiucv_hs_rcv,
};

static int afiucv_iucv_init(void)
2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434
{
	int err;

	err = pr_iucv->iucv_register(&af_iucv_handler, 0);
	if (err)
		goto out;
	/* establish dummy device */
	af_iucv_driver.bus = pr_iucv->bus;
	err = driver_register(&af_iucv_driver);
	if (err)
		goto out_iucv;
	af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
	if (!af_iucv_dev) {
		err = -ENOMEM;
		goto out_driver;
	}
	dev_set_name(af_iucv_dev, "af_iucv");
	af_iucv_dev->bus = pr_iucv->bus;
	af_iucv_dev->parent = pr_iucv->root;
	af_iucv_dev->release = (void (*)(struct device *))kfree;
	af_iucv_dev->driver = &af_iucv_driver;
	err = device_register(af_iucv_dev);
	if (err)
2435
		goto out_iucv_dev;
2436 2437
	return 0;

2438 2439
out_iucv_dev:
	put_device(af_iucv_dev);
2440 2441 2442 2443 2444 2445 2446 2447
out_driver:
	driver_unregister(&af_iucv_driver);
out_iucv:
	pr_iucv->iucv_unregister(&af_iucv_handler, 0);
out:
	return err;
}

2448
static int __init afiucv_init(void)
2449 2450 2451
{
	int err;

2452 2453 2454 2455 2456 2457 2458
	if (MACHINE_IS_VM) {
		cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
		if (unlikely(err)) {
			WARN_ON(err);
			err = -EPROTONOSUPPORT;
			goto out;
		}
2459

2460 2461 2462 2463 2464 2465 2466 2467
		pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
		if (!pr_iucv) {
			printk(KERN_WARNING "iucv_if lookup failed\n");
			memset(&iucv_userid, 0, sizeof(iucv_userid));
		}
	} else {
		memset(&iucv_userid, 0, sizeof(iucv_userid));
		pr_iucv = NULL;
2468 2469
	}

2470 2471
	err = proto_register(&iucv_proto, 0);
	if (err)
2472
		goto out;
2473 2474 2475
	err = sock_register(&iucv_sock_family_ops);
	if (err)
		goto out_proto;
2476

2477 2478 2479 2480
	if (pr_iucv) {
		err = afiucv_iucv_init();
		if (err)
			goto out_sock;
U
Ursula Braun 已提交
2481 2482
	} else
		register_netdevice_notifier(&afiucv_netdev_notifier);
2483
	dev_add_pack(&iucv_packet_type);
2484 2485
	return 0;

2486 2487
out_sock:
	sock_unregister(PF_IUCV);
2488 2489 2490
out_proto:
	proto_unregister(&iucv_proto);
out:
2491 2492
	if (pr_iucv)
		symbol_put(iucv_if);
2493 2494 2495 2496 2497
	return err;
}

static void __exit afiucv_exit(void)
{
2498 2499 2500 2501 2502
	if (pr_iucv) {
		device_unregister(af_iucv_dev);
		driver_unregister(&af_iucv_driver);
		pr_iucv->iucv_unregister(&af_iucv_handler, 0);
		symbol_put(iucv_if);
U
Ursula Braun 已提交
2503 2504
	} else
		unregister_netdevice_notifier(&afiucv_netdev_notifier);
2505
	dev_remove_pack(&iucv_packet_type);
2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517
	sock_unregister(PF_IUCV);
	proto_unregister(&iucv_proto);
}

module_init(afiucv_init);
module_exit(afiucv_exit);

MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");
MODULE_ALIAS_NETPROTO(PF_IUCV);