xref: /linux/net/iucv/af_iucv.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  IUCV protocol stack for Linux on zSeries
4  *
5  *  Copyright IBM Corp. 2006, 2009
6  *
7  *  Author(s):	Jennifer Hunt <jenhunt@us.ibm.com>
8  *		Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
9  *  PM functions:
10  *		Ursula Braun <ursula.braun@de.ibm.com>
11  */
12 
13 #define KMSG_COMPONENT "af_iucv"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15 
16 #include <linux/filter.h>
17 #include <linux/module.h>
18 #include <linux/netdevice.h>
19 #include <linux/types.h>
20 #include <linux/limits.h>
21 #include <linux/list.h>
22 #include <linux/errno.h>
23 #include <linux/kernel.h>
24 #include <linux/sched/signal.h>
25 #include <linux/slab.h>
26 #include <linux/skbuff.h>
27 #include <linux/init.h>
28 #include <linux/poll.h>
29 #include <linux/security.h>
30 #include <net/sock.h>
31 #include <asm/ebcdic.h>
32 #include <asm/cpcmd.h>
33 #include <linux/kmod.h>
34 
35 #include <net/iucv/af_iucv.h>
36 
37 #define VERSION "1.2"
38 
39 static char iucv_userid[80];
40 
41 static struct proto iucv_proto = {
42 	.name		= "AF_IUCV",
43 	.owner		= THIS_MODULE,
44 	.obj_size	= sizeof(struct iucv_sock),
45 };
46 
47 static struct iucv_interface *pr_iucv;
48 static struct iucv_handler af_iucv_handler;
49 
50 /* special AF_IUCV IPRM messages */
51 static const u8 iprm_shutdown[8] =
52 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
53 
54 #define TRGCLS_SIZE	sizeof_field(struct iucv_message, class)
55 
56 #define __iucv_sock_wait(sk, condition, timeo, ret)			\
57 do {									\
58 	DEFINE_WAIT(__wait);						\
59 	long __timeo = timeo;						\
60 	ret = 0;							\
61 	prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE);	\
62 	while (!(condition)) {						\
63 		if (!__timeo) {						\
64 			ret = -EAGAIN;					\
65 			break;						\
66 		}							\
67 		if (signal_pending(current)) {				\
68 			ret = sock_intr_errno(__timeo);			\
69 			break;						\
70 		}							\
71 		release_sock(sk);					\
72 		__timeo = schedule_timeout(__timeo);			\
73 		lock_sock(sk);						\
74 		ret = sock_error(sk);					\
75 		if (ret)						\
76 			break;						\
77 	}								\
78 	finish_wait(sk_sleep(sk), &__wait);				\
79 } while (0)
80 
81 #define iucv_sock_wait(sk, condition, timeo)				\
82 ({									\
83 	int __ret = 0;							\
84 	if (!(condition))						\
85 		__iucv_sock_wait(sk, condition, timeo, __ret);		\
86 	__ret;								\
87 })
88 
89 static struct sock *iucv_accept_dequeue(struct sock *parent,
90 					struct socket *newsock);
91 static void iucv_sock_kill(struct sock *sk);
92 static void iucv_sock_close(struct sock *sk);
93 
94 static void afiucv_hs_callback_txnotify(struct sock *sk, enum iucv_tx_notify);
95 
96 static struct iucv_sock_list iucv_sk_list = {
97 	.lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
98 	.autobind_name = ATOMIC_INIT(0)
99 };
100 
101 static inline void high_nmcpy(unsigned char *dst, char *src)
102 {
103        memcpy(dst, src, 8);
104 }
105 
106 static inline void low_nmcpy(unsigned char *dst, char *src)
107 {
108        memcpy(&dst[8], src, 8);
109 }
110 
111 /**
112  * iucv_msg_length() - Returns the length of an iucv message.
113  * @msg:	Pointer to struct iucv_message, MUST NOT be NULL
114  *
115  * The function returns the length of the specified iucv message @msg of data
116  * stored in a buffer and of data stored in the parameter list (PRMDATA).
117  *
118  * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
119  * data:
120  *	PRMDATA[0..6]	socket data (max 7 bytes);
121  *	PRMDATA[7]	socket data length value (len is 0xff - PRMDATA[7])
122  *
123  * The socket data length is computed by subtracting the socket data length
124  * value from 0xFF.
125  * If the socket data len is greater 7, then PRMDATA can be used for special
126  * notifications (see iucv_sock_shutdown); and further,
127  * if the socket data len is > 7, the function returns 8.
128  *
129  * Use this function to allocate socket buffers to store iucv message data.
130  */
131 static inline size_t iucv_msg_length(struct iucv_message *msg)
132 {
133 	size_t datalen;
134 
135 	if (msg->flags & IUCV_IPRMDATA) {
136 		datalen = 0xff - msg->rmmsg[7];
137 		return (datalen < 8) ? datalen : 8;
138 	}
139 	return msg->length;
140 }
141 
142 /**
143  * iucv_sock_in_state() - check for specific states
144  * @sk:		sock structure
145  * @state:	first iucv sk state
146  * @state2:	second iucv sk state
147  *
148  * Returns true if the socket in either in the first or second state.
149  */
150 static int iucv_sock_in_state(struct sock *sk, int state, int state2)
151 {
152 	return (sk->sk_state == state || sk->sk_state == state2);
153 }
154 
155 /**
156  * iucv_below_msglim() - function to check if messages can be sent
157  * @sk:		sock structure
158  *
159  * Returns true if the send queue length is lower than the message limit.
160  * Always returns true if the socket is not connected (no iucv path for
161  * checking the message limit).
162  */
163 static inline int iucv_below_msglim(struct sock *sk)
164 {
165 	struct iucv_sock *iucv = iucv_sk(sk);
166 
167 	if (sk->sk_state != IUCV_CONNECTED)
168 		return 1;
169 	if (iucv->transport == AF_IUCV_TRANS_IUCV)
170 		return (atomic_read(&iucv->skbs_in_xmit) < iucv->path->msglim);
171 	else
172 		return ((atomic_read(&iucv->msg_sent) < iucv->msglimit_peer) &&
173 			(atomic_read(&iucv->pendings) <= 0));
174 }
175 
176 /*
177  * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
178  */
179 static void iucv_sock_wake_msglim(struct sock *sk)
180 {
181 	struct socket_wq *wq;
182 
183 	rcu_read_lock();
184 	wq = rcu_dereference(sk->sk_wq);
185 	if (skwq_has_sleeper(wq))
186 		wake_up_interruptible_all(&wq->wait);
187 	sk_wake_async_rcu(sk, SOCK_WAKE_SPACE, POLL_OUT);
188 	rcu_read_unlock();
189 }
190 
191 /*
192  * afiucv_hs_send() - send a message through HiperSockets transport
193  */
194 static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
195 		   struct sk_buff *skb, u8 flags)
196 {
197 	struct iucv_sock *iucv = iucv_sk(sock);
198 	struct af_iucv_trans_hdr *phs_hdr;
199 	int err, confirm_recv = 0;
200 
201 	phs_hdr = skb_push(skb, sizeof(*phs_hdr));
202 	memset(phs_hdr, 0, sizeof(*phs_hdr));
203 	skb_reset_network_header(skb);
204 
205 	phs_hdr->magic = ETH_P_AF_IUCV;
206 	phs_hdr->version = 1;
207 	phs_hdr->flags = flags;
208 	if (flags == AF_IUCV_FLAG_SYN)
209 		phs_hdr->window = iucv->msglimit;
210 	else if ((flags == AF_IUCV_FLAG_WIN) || !flags) {
211 		confirm_recv = atomic_read(&iucv->msg_recv);
212 		phs_hdr->window = confirm_recv;
213 		if (confirm_recv)
214 			phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
215 	}
216 	memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
217 	memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
218 	memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
219 	memcpy(phs_hdr->srcAppName, iucv->src_name, 8);
220 	ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID));
221 	ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName));
222 	ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID));
223 	ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName));
224 	if (imsg)
225 		memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message));
226 
227 	skb->dev = iucv->hs_dev;
228 	if (!skb->dev) {
229 		err = -ENODEV;
230 		goto err_free;
231 	}
232 
233 	dev_hard_header(skb, skb->dev, ETH_P_AF_IUCV, NULL, NULL, skb->len);
234 
235 	if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev)) {
236 		err = -ENETDOWN;
237 		goto err_free;
238 	}
239 	if (skb->len > skb->dev->mtu) {
240 		if (sock->sk_type == SOCK_SEQPACKET) {
241 			err = -EMSGSIZE;
242 			goto err_free;
243 		}
244 		err = pskb_trim(skb, skb->dev->mtu);
245 		if (err)
246 			goto err_free;
247 	}
248 	skb->protocol = cpu_to_be16(ETH_P_AF_IUCV);
249 
250 	atomic_inc(&iucv->skbs_in_xmit);
251 	err = dev_queue_xmit(skb);
252 	if (net_xmit_eval(err)) {
253 		atomic_dec(&iucv->skbs_in_xmit);
254 	} else {
255 		atomic_sub(confirm_recv, &iucv->msg_recv);
256 		WARN_ON(atomic_read(&iucv->msg_recv) < 0);
257 	}
258 	return net_xmit_eval(err);
259 
260 err_free:
261 	kfree_skb(skb);
262 	return err;
263 }
264 
265 static struct sock *__iucv_get_sock_by_name(char *nm)
266 {
267 	struct sock *sk;
268 
269 	sk_for_each(sk, &iucv_sk_list.head)
270 		if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
271 			return sk;
272 
273 	return NULL;
274 }
275 
276 static void iucv_sock_destruct(struct sock *sk)
277 {
278 	skb_queue_purge(&sk->sk_receive_queue);
279 	skb_queue_purge(&sk->sk_error_queue);
280 
281 	if (!sock_flag(sk, SOCK_DEAD)) {
282 		pr_err("Attempt to release alive iucv socket %p\n", sk);
283 		return;
284 	}
285 
286 	WARN_ON(atomic_read(&sk->sk_rmem_alloc));
287 	WARN_ON(refcount_read(&sk->sk_wmem_alloc));
288 	WARN_ON(sk->sk_wmem_queued);
289 	WARN_ON(sk->sk_forward_alloc);
290 }
291 
292 /* Cleanup Listen */
293 static void iucv_sock_cleanup_listen(struct sock *parent)
294 {
295 	struct sock *sk;
296 
297 	/* Close non-accepted connections */
298 	while ((sk = iucv_accept_dequeue(parent, NULL))) {
299 		iucv_sock_close(sk);
300 		iucv_sock_kill(sk);
301 	}
302 
303 	parent->sk_state = IUCV_CLOSED;
304 }
305 
306 static void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
307 {
308 	write_lock_bh(&l->lock);
309 	sk_add_node(sk, &l->head);
310 	write_unlock_bh(&l->lock);
311 }
312 
313 static void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
314 {
315 	write_lock_bh(&l->lock);
316 	sk_del_node_init(sk);
317 	write_unlock_bh(&l->lock);
318 }
319 
320 /* Kill socket (only if zapped and orphaned) */
321 static void iucv_sock_kill(struct sock *sk)
322 {
323 	if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
324 		return;
325 
326 	iucv_sock_unlink(&iucv_sk_list, sk);
327 	sock_set_flag(sk, SOCK_DEAD);
328 	sock_put(sk);
329 }
330 
331 /* Terminate an IUCV path */
332 static void iucv_sever_path(struct sock *sk, int with_user_data)
333 {
334 	unsigned char user_data[16];
335 	struct iucv_sock *iucv = iucv_sk(sk);
336 	struct iucv_path *path = iucv->path;
337 
338 	/* Whoever resets the path pointer, must sever and free it. */
339 	if (xchg(&iucv->path, NULL)) {
340 		if (with_user_data) {
341 			low_nmcpy(user_data, iucv->src_name);
342 			high_nmcpy(user_data, iucv->dst_name);
343 			ASCEBC(user_data, sizeof(user_data));
344 			pr_iucv->path_sever(path, user_data);
345 		} else
346 			pr_iucv->path_sever(path, NULL);
347 		iucv_path_free(path);
348 	}
349 }
350 
351 /* Send controlling flags through an IUCV socket for HIPER transport */
352 static int iucv_send_ctrl(struct sock *sk, u8 flags)
353 {
354 	struct iucv_sock *iucv = iucv_sk(sk);
355 	int err = 0;
356 	int blen;
357 	struct sk_buff *skb;
358 	u8 shutdown = 0;
359 
360 	blen = sizeof(struct af_iucv_trans_hdr) +
361 	       LL_RESERVED_SPACE(iucv->hs_dev);
362 	if (sk->sk_shutdown & SEND_SHUTDOWN) {
363 		/* controlling flags should be sent anyway */
364 		shutdown = sk->sk_shutdown;
365 		sk->sk_shutdown &= RCV_SHUTDOWN;
366 	}
367 	skb = sock_alloc_send_skb(sk, blen, 1, &err);
368 	if (skb) {
369 		skb_reserve(skb, blen);
370 		err = afiucv_hs_send(NULL, sk, skb, flags);
371 	}
372 	if (shutdown)
373 		sk->sk_shutdown = shutdown;
374 	return err;
375 }
376 
377 /* Close an IUCV socket */
378 static void iucv_sock_close(struct sock *sk)
379 {
380 	struct iucv_sock *iucv = iucv_sk(sk);
381 	unsigned long timeo;
382 	int err = 0;
383 
384 	lock_sock(sk);
385 
386 	switch (sk->sk_state) {
387 	case IUCV_LISTEN:
388 		iucv_sock_cleanup_listen(sk);
389 		break;
390 
391 	case IUCV_CONNECTED:
392 		if (iucv->transport == AF_IUCV_TRANS_HIPER) {
393 			err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
394 			sk->sk_state = IUCV_DISCONN;
395 			sk->sk_state_change(sk);
396 		}
397 		fallthrough;
398 
399 	case IUCV_DISCONN:
400 		sk->sk_state = IUCV_CLOSING;
401 		sk->sk_state_change(sk);
402 
403 		if (!err && atomic_read(&iucv->skbs_in_xmit) > 0) {
404 			if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
405 				timeo = sk->sk_lingertime;
406 			else
407 				timeo = IUCV_DISCONN_TIMEOUT;
408 			iucv_sock_wait(sk,
409 					iucv_sock_in_state(sk, IUCV_CLOSED, 0),
410 					timeo);
411 		}
412 		fallthrough;
413 
414 	case IUCV_CLOSING:
415 		sk->sk_state = IUCV_CLOSED;
416 		sk->sk_state_change(sk);
417 
418 		sk->sk_err = ECONNRESET;
419 		sk->sk_state_change(sk);
420 
421 		skb_queue_purge(&iucv->send_skb_q);
422 		skb_queue_purge(&iucv->backlog_skb_q);
423 		fallthrough;
424 
425 	default:
426 		iucv_sever_path(sk, 1);
427 	}
428 
429 	if (iucv->hs_dev) {
430 		dev_put(iucv->hs_dev);
431 		iucv->hs_dev = NULL;
432 		sk->sk_bound_dev_if = 0;
433 	}
434 
435 	/* mark socket for deletion by iucv_sock_kill() */
436 	sock_set_flag(sk, SOCK_ZAPPED);
437 
438 	release_sock(sk);
439 }
440 
441 static void iucv_sock_init(struct sock *sk, struct sock *parent)
442 {
443 	if (parent) {
444 		sk->sk_type = parent->sk_type;
445 		security_sk_clone(parent, sk);
446 	}
447 }
448 
449 static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio, int kern)
450 {
451 	struct sock *sk;
452 	struct iucv_sock *iucv;
453 
454 	sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, kern);
455 	if (!sk)
456 		return NULL;
457 	iucv = iucv_sk(sk);
458 
459 	sock_init_data(sock, sk);
460 	INIT_LIST_HEAD(&iucv->accept_q);
461 	spin_lock_init(&iucv->accept_q_lock);
462 	skb_queue_head_init(&iucv->send_skb_q);
463 	INIT_LIST_HEAD(&iucv->message_q.list);
464 	spin_lock_init(&iucv->message_q.lock);
465 	skb_queue_head_init(&iucv->backlog_skb_q);
466 	iucv->send_tag = 0;
467 	atomic_set(&iucv->pendings, 0);
468 	iucv->flags = 0;
469 	iucv->msglimit = 0;
470 	atomic_set(&iucv->skbs_in_xmit, 0);
471 	atomic_set(&iucv->msg_sent, 0);
472 	atomic_set(&iucv->msg_recv, 0);
473 	iucv->path = NULL;
474 	iucv->sk_txnotify = afiucv_hs_callback_txnotify;
475 	memset(&iucv->init, 0, sizeof(iucv->init));
476 	if (pr_iucv)
477 		iucv->transport = AF_IUCV_TRANS_IUCV;
478 	else
479 		iucv->transport = AF_IUCV_TRANS_HIPER;
480 
481 	sk->sk_destruct = iucv_sock_destruct;
482 	sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
483 
484 	sock_reset_flag(sk, SOCK_ZAPPED);
485 
486 	sk->sk_protocol = proto;
487 	sk->sk_state	= IUCV_OPEN;
488 
489 	iucv_sock_link(&iucv_sk_list, sk);
490 	return sk;
491 }
492 
493 static void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
494 {
495 	unsigned long flags;
496 	struct iucv_sock *par = iucv_sk(parent);
497 
498 	sock_hold(sk);
499 	spin_lock_irqsave(&par->accept_q_lock, flags);
500 	list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
501 	spin_unlock_irqrestore(&par->accept_q_lock, flags);
502 	iucv_sk(sk)->parent = parent;
503 	sk_acceptq_added(parent);
504 }
505 
506 static void iucv_accept_unlink(struct sock *sk)
507 {
508 	unsigned long flags;
509 	struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
510 
511 	spin_lock_irqsave(&par->accept_q_lock, flags);
512 	list_del_init(&iucv_sk(sk)->accept_q);
513 	spin_unlock_irqrestore(&par->accept_q_lock, flags);
514 	sk_acceptq_removed(iucv_sk(sk)->parent);
515 	iucv_sk(sk)->parent = NULL;
516 	sock_put(sk);
517 }
518 
519 static struct sock *iucv_accept_dequeue(struct sock *parent,
520 					struct socket *newsock)
521 {
522 	struct iucv_sock *isk, *n;
523 	struct sock *sk;
524 
525 	list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
526 		sk = (struct sock *) isk;
527 		lock_sock(sk);
528 
529 		if (sk->sk_state == IUCV_CLOSED) {
530 			iucv_accept_unlink(sk);
531 			release_sock(sk);
532 			continue;
533 		}
534 
535 		if (sk->sk_state == IUCV_CONNECTED ||
536 		    sk->sk_state == IUCV_DISCONN ||
537 		    !newsock) {
538 			iucv_accept_unlink(sk);
539 			if (newsock)
540 				sock_graft(sk, newsock);
541 
542 			release_sock(sk);
543 			return sk;
544 		}
545 
546 		release_sock(sk);
547 	}
548 	return NULL;
549 }
550 
551 static void __iucv_auto_name(struct iucv_sock *iucv)
552 {
553 	char name[12];
554 
555 	sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
556 	while (__iucv_get_sock_by_name(name)) {
557 		sprintf(name, "%08x",
558 			atomic_inc_return(&iucv_sk_list.autobind_name));
559 	}
560 	memcpy(iucv->src_name, name, 8);
561 }
562 
563 /* Bind an unbound socket */
564 static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
565 			  int addr_len)
566 {
567 	DECLARE_SOCKADDR(struct sockaddr_iucv *, sa, addr);
568 	char uid[sizeof(sa->siucv_user_id)];
569 	struct sock *sk = sock->sk;
570 	struct iucv_sock *iucv;
571 	int err = 0;
572 	struct net_device *dev;
573 
574 	/* Verify the input sockaddr */
575 	if (addr_len < sizeof(struct sockaddr_iucv) ||
576 	    addr->sa_family != AF_IUCV)
577 		return -EINVAL;
578 
579 	lock_sock(sk);
580 	if (sk->sk_state != IUCV_OPEN) {
581 		err = -EBADFD;
582 		goto done;
583 	}
584 
585 	write_lock_bh(&iucv_sk_list.lock);
586 
587 	iucv = iucv_sk(sk);
588 	if (__iucv_get_sock_by_name(sa->siucv_name)) {
589 		err = -EADDRINUSE;
590 		goto done_unlock;
591 	}
592 	if (iucv->path)
593 		goto done_unlock;
594 
595 	/* Bind the socket */
596 	if (pr_iucv)
597 		if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
598 			goto vm_bind; /* VM IUCV transport */
599 
600 	/* try hiper transport */
601 	memcpy(uid, sa->siucv_user_id, sizeof(uid));
602 	ASCEBC(uid, 8);
603 	rcu_read_lock();
604 	for_each_netdev_rcu(&init_net, dev) {
605 		if (!memcmp(dev->perm_addr, uid, 8)) {
606 			memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
607 			/* Check for uninitialized siucv_name */
608 			if (strncmp(sa->siucv_name, "        ", 8) == 0)
609 				__iucv_auto_name(iucv);
610 			else
611 				memcpy(iucv->src_name, sa->siucv_name, 8);
612 			sk->sk_bound_dev_if = dev->ifindex;
613 			iucv->hs_dev = dev;
614 			dev_hold(dev);
615 			sk->sk_state = IUCV_BOUND;
616 			iucv->transport = AF_IUCV_TRANS_HIPER;
617 			if (!iucv->msglimit)
618 				iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
619 			rcu_read_unlock();
620 			goto done_unlock;
621 		}
622 	}
623 	rcu_read_unlock();
624 vm_bind:
625 	if (pr_iucv) {
626 		/* use local userid for backward compat */
627 		memcpy(iucv->src_name, sa->siucv_name, 8);
628 		memcpy(iucv->src_user_id, iucv_userid, 8);
629 		sk->sk_state = IUCV_BOUND;
630 		iucv->transport = AF_IUCV_TRANS_IUCV;
631 		sk->sk_allocation |= GFP_DMA;
632 		if (!iucv->msglimit)
633 			iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
634 		goto done_unlock;
635 	}
636 	/* found no dev to bind */
637 	err = -ENODEV;
638 done_unlock:
639 	/* Release the socket list lock */
640 	write_unlock_bh(&iucv_sk_list.lock);
641 done:
642 	release_sock(sk);
643 	return err;
644 }
645 
646 /* Automatically bind an unbound socket */
647 static int iucv_sock_autobind(struct sock *sk)
648 {
649 	struct iucv_sock *iucv = iucv_sk(sk);
650 	int err = 0;
651 
652 	if (unlikely(!pr_iucv))
653 		return -EPROTO;
654 
655 	memcpy(iucv->src_user_id, iucv_userid, 8);
656 	iucv->transport = AF_IUCV_TRANS_IUCV;
657 	sk->sk_allocation |= GFP_DMA;
658 
659 	write_lock_bh(&iucv_sk_list.lock);
660 	__iucv_auto_name(iucv);
661 	write_unlock_bh(&iucv_sk_list.lock);
662 
663 	if (!iucv->msglimit)
664 		iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
665 
666 	return err;
667 }
668 
669 static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
670 {
671 	DECLARE_SOCKADDR(struct sockaddr_iucv *, sa, addr);
672 	struct sock *sk = sock->sk;
673 	struct iucv_sock *iucv = iucv_sk(sk);
674 	unsigned char user_data[16];
675 	int err;
676 
677 	high_nmcpy(user_data, sa->siucv_name);
678 	low_nmcpy(user_data, iucv->src_name);
679 	ASCEBC(user_data, sizeof(user_data));
680 
681 	/* Create path. */
682 	iucv->path = iucv_path_alloc(iucv->msglimit,
683 				     IUCV_IPRMDATA, GFP_KERNEL);
684 	if (!iucv->path) {
685 		err = -ENOMEM;
686 		goto done;
687 	}
688 	err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
689 				    sa->siucv_user_id, NULL, user_data,
690 				    sk);
691 	if (err) {
692 		iucv_path_free(iucv->path);
693 		iucv->path = NULL;
694 		switch (err) {
695 		case 0x0b:	/* Target communicator is not logged on */
696 			err = -ENETUNREACH;
697 			break;
698 		case 0x0d:	/* Max connections for this guest exceeded */
699 		case 0x0e:	/* Max connections for target guest exceeded */
700 			err = -EAGAIN;
701 			break;
702 		case 0x0f:	/* Missing IUCV authorization */
703 			err = -EACCES;
704 			break;
705 		default:
706 			err = -ECONNREFUSED;
707 			break;
708 		}
709 	}
710 done:
711 	return err;
712 }
713 
714 /* Connect an unconnected socket */
715 static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
716 			     int alen, int flags)
717 {
718 	DECLARE_SOCKADDR(struct sockaddr_iucv *, sa, addr);
719 	struct sock *sk = sock->sk;
720 	struct iucv_sock *iucv = iucv_sk(sk);
721 	int err;
722 
723 	if (alen < sizeof(struct sockaddr_iucv) || addr->sa_family != AF_IUCV)
724 		return -EINVAL;
725 
726 	if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
727 		return -EBADFD;
728 
729 	if (sk->sk_state == IUCV_OPEN &&
730 	    iucv->transport == AF_IUCV_TRANS_HIPER)
731 		return -EBADFD; /* explicit bind required */
732 
733 	if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
734 		return -EINVAL;
735 
736 	if (sk->sk_state == IUCV_OPEN) {
737 		err = iucv_sock_autobind(sk);
738 		if (unlikely(err))
739 			return err;
740 	}
741 
742 	lock_sock(sk);
743 
744 	/* Set the destination information */
745 	memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
746 	memcpy(iucv->dst_name, sa->siucv_name, 8);
747 
748 	if (iucv->transport == AF_IUCV_TRANS_HIPER)
749 		err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
750 	else
751 		err = afiucv_path_connect(sock, addr);
752 	if (err)
753 		goto done;
754 
755 	if (sk->sk_state != IUCV_CONNECTED)
756 		err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
757 							    IUCV_DISCONN),
758 				     sock_sndtimeo(sk, flags & O_NONBLOCK));
759 
760 	if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
761 		err = -ECONNREFUSED;
762 
763 	if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
764 		iucv_sever_path(sk, 0);
765 
766 done:
767 	release_sock(sk);
768 	return err;
769 }
770 
771 /* Move a socket into listening state. */
772 static int iucv_sock_listen(struct socket *sock, int backlog)
773 {
774 	struct sock *sk = sock->sk;
775 	int err;
776 
777 	lock_sock(sk);
778 
779 	err = -EINVAL;
780 	if (sk->sk_state != IUCV_BOUND)
781 		goto done;
782 
783 	if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
784 		goto done;
785 
786 	sk->sk_max_ack_backlog = backlog;
787 	sk->sk_ack_backlog = 0;
788 	sk->sk_state = IUCV_LISTEN;
789 	err = 0;
790 
791 done:
792 	release_sock(sk);
793 	return err;
794 }
795 
796 /* Accept a pending connection */
797 static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
798 			    struct proto_accept_arg *arg)
799 {
800 	DECLARE_WAITQUEUE(wait, current);
801 	struct sock *sk = sock->sk, *nsk;
802 	long timeo;
803 	int err = 0;
804 
805 	lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
806 
807 	if (sk->sk_state != IUCV_LISTEN) {
808 		err = -EBADFD;
809 		goto done;
810 	}
811 
812 	timeo = sock_rcvtimeo(sk, arg->flags & O_NONBLOCK);
813 
814 	/* Wait for an incoming connection */
815 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
816 	while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
817 		set_current_state(TASK_INTERRUPTIBLE);
818 		if (!timeo) {
819 			err = -EAGAIN;
820 			break;
821 		}
822 
823 		release_sock(sk);
824 		timeo = schedule_timeout(timeo);
825 		lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
826 
827 		if (sk->sk_state != IUCV_LISTEN) {
828 			err = -EBADFD;
829 			break;
830 		}
831 
832 		if (signal_pending(current)) {
833 			err = sock_intr_errno(timeo);
834 			break;
835 		}
836 	}
837 
838 	set_current_state(TASK_RUNNING);
839 	remove_wait_queue(sk_sleep(sk), &wait);
840 
841 	if (err)
842 		goto done;
843 
844 	newsock->state = SS_CONNECTED;
845 
846 done:
847 	release_sock(sk);
848 	return err;
849 }
850 
851 static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
852 			     int peer)
853 {
854 	DECLARE_SOCKADDR(struct sockaddr_iucv *, siucv, addr);
855 	struct sock *sk = sock->sk;
856 	struct iucv_sock *iucv = iucv_sk(sk);
857 
858 	addr->sa_family = AF_IUCV;
859 
860 	if (peer) {
861 		memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
862 		memcpy(siucv->siucv_name, iucv->dst_name, 8);
863 	} else {
864 		memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
865 		memcpy(siucv->siucv_name, iucv->src_name, 8);
866 	}
867 	memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
868 	memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
869 	memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
870 
871 	return sizeof(struct sockaddr_iucv);
872 }
873 
874 /**
875  * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
876  * @path:	IUCV path
877  * @msg:	Pointer to a struct iucv_message
878  * @skb:	The socket data to send, skb->len MUST BE <= 7
879  *
880  * Send the socket data in the parameter list in the iucv message
881  * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
882  * list and the socket data len at index 7 (last byte).
883  * See also iucv_msg_length().
884  *
885  * Returns the error code from the iucv_message_send() call.
886  */
887 static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
888 			  struct sk_buff *skb)
889 {
890 	u8 prmdata[8];
891 
892 	memcpy(prmdata, (void *) skb->data, skb->len);
893 	prmdata[7] = 0xff - (u8) skb->len;
894 	return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
895 				 (void *) prmdata, 8);
896 }
897 
898 static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg,
899 			     size_t len)
900 {
901 	struct sock *sk = sock->sk;
902 	struct iucv_sock *iucv = iucv_sk(sk);
903 	size_t headroom = 0;
904 	size_t linear;
905 	struct sk_buff *skb;
906 	struct iucv_message txmsg = {0};
907 	struct cmsghdr *cmsg;
908 	int cmsg_done;
909 	long timeo;
910 	char user_id[9];
911 	char appl_id[9];
912 	int err;
913 	int noblock = msg->msg_flags & MSG_DONTWAIT;
914 
915 	err = sock_error(sk);
916 	if (err)
917 		return err;
918 
919 	if (msg->msg_flags & MSG_OOB)
920 		return -EOPNOTSUPP;
921 
922 	/* SOCK_SEQPACKET: we do not support segmented records */
923 	if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
924 		return -EOPNOTSUPP;
925 
926 	lock_sock(sk);
927 
928 	if (sk->sk_shutdown & SEND_SHUTDOWN) {
929 		err = -EPIPE;
930 		goto out;
931 	}
932 
933 	/* Return if the socket is not in connected state */
934 	if (sk->sk_state != IUCV_CONNECTED) {
935 		err = -ENOTCONN;
936 		goto out;
937 	}
938 
939 	/* initialize defaults */
940 	cmsg_done   = 0;	/* check for duplicate headers */
941 
942 	/* iterate over control messages */
943 	for_each_cmsghdr(cmsg, msg) {
944 		if (!CMSG_OK(msg, cmsg)) {
945 			err = -EINVAL;
946 			goto out;
947 		}
948 
949 		if (cmsg->cmsg_level != SOL_IUCV)
950 			continue;
951 
952 		if (cmsg->cmsg_type & cmsg_done) {
953 			err = -EINVAL;
954 			goto out;
955 		}
956 		cmsg_done |= cmsg->cmsg_type;
957 
958 		switch (cmsg->cmsg_type) {
959 		case SCM_IUCV_TRGCLS:
960 			if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
961 				err = -EINVAL;
962 				goto out;
963 			}
964 
965 			/* set iucv message target class */
966 			memcpy(&txmsg.class,
967 				(void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
968 
969 			break;
970 
971 		default:
972 			err = -EINVAL;
973 			goto out;
974 		}
975 	}
976 
977 	/* allocate one skb for each iucv message:
978 	 * this is fine for SOCK_SEQPACKET (unless we want to support
979 	 * segmented records using the MSG_EOR flag), but
980 	 * for SOCK_STREAM we might want to improve it in future */
981 	if (iucv->transport == AF_IUCV_TRANS_HIPER) {
982 		headroom = sizeof(struct af_iucv_trans_hdr) +
983 			   LL_RESERVED_SPACE(iucv->hs_dev);
984 		linear = min(len, PAGE_SIZE - headroom);
985 	} else {
986 		if (len < PAGE_SIZE) {
987 			linear = len;
988 		} else {
989 			/* In nonlinear "classic" iucv skb,
990 			 * reserve space for iucv_array
991 			 */
992 			headroom = sizeof(struct iucv_array) *
993 				   (MAX_SKB_FRAGS + 1);
994 			linear = PAGE_SIZE - headroom;
995 		}
996 	}
997 	skb = sock_alloc_send_pskb(sk, headroom + linear, len - linear,
998 				   noblock, &err, 0);
999 	if (!skb)
1000 		goto out;
1001 	if (headroom)
1002 		skb_reserve(skb, headroom);
1003 	skb_put(skb, linear);
1004 	skb->len = len;
1005 	skb->data_len = len - linear;
1006 	err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
1007 	if (err)
1008 		goto fail;
1009 
1010 	/* wait if outstanding messages for iucv path has reached */
1011 	timeo = sock_sndtimeo(sk, noblock);
1012 	err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
1013 	if (err)
1014 		goto fail;
1015 
1016 	/* return -ECONNRESET if the socket is no longer connected */
1017 	if (sk->sk_state != IUCV_CONNECTED) {
1018 		err = -ECONNRESET;
1019 		goto fail;
1020 	}
1021 
1022 	/* increment and save iucv message tag for msg_completion cbk */
1023 	txmsg.tag = iucv->send_tag++;
1024 	IUCV_SKB_CB(skb)->tag = txmsg.tag;
1025 
1026 	if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1027 		atomic_inc(&iucv->msg_sent);
1028 		err = afiucv_hs_send(&txmsg, sk, skb, 0);
1029 		if (err) {
1030 			atomic_dec(&iucv->msg_sent);
1031 			goto out;
1032 		}
1033 	} else { /* Classic VM IUCV transport */
1034 		skb_queue_tail(&iucv->send_skb_q, skb);
1035 		atomic_inc(&iucv->skbs_in_xmit);
1036 
1037 		if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags) &&
1038 		    skb->len <= 7) {
1039 			err = iucv_send_iprm(iucv->path, &txmsg, skb);
1040 
1041 			/* on success: there is no message_complete callback */
1042 			/* for an IPRMDATA msg; remove skb from send queue   */
1043 			if (err == 0) {
1044 				atomic_dec(&iucv->skbs_in_xmit);
1045 				skb_unlink(skb, &iucv->send_skb_q);
1046 				consume_skb(skb);
1047 			}
1048 
1049 			/* this error should never happen since the	*/
1050 			/* IUCV_IPRMDATA path flag is set... sever path */
1051 			if (err == 0x15) {
1052 				pr_iucv->path_sever(iucv->path, NULL);
1053 				atomic_dec(&iucv->skbs_in_xmit);
1054 				skb_unlink(skb, &iucv->send_skb_q);
1055 				err = -EPIPE;
1056 				goto fail;
1057 			}
1058 		} else if (skb_is_nonlinear(skb)) {
1059 			struct iucv_array *iba = (struct iucv_array *)skb->head;
1060 			int i;
1061 
1062 			/* skip iucv_array lying in the headroom */
1063 			iba[0].address = virt_to_dma32(skb->data);
1064 			iba[0].length = (u32)skb_headlen(skb);
1065 			for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1066 				skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1067 
1068 				iba[i + 1].address = virt_to_dma32(skb_frag_address(frag));
1069 				iba[i + 1].length = (u32)skb_frag_size(frag);
1070 			}
1071 			err = pr_iucv->message_send(iucv->path, &txmsg,
1072 						    IUCV_IPBUFLST, 0,
1073 						    (void *)iba, skb->len);
1074 		} else { /* non-IPRM Linear skb */
1075 			err = pr_iucv->message_send(iucv->path, &txmsg,
1076 					0, 0, (void *)skb->data, skb->len);
1077 		}
1078 		if (err) {
1079 			if (err == 3) {
1080 				user_id[8] = 0;
1081 				memcpy(user_id, iucv->dst_user_id, 8);
1082 				appl_id[8] = 0;
1083 				memcpy(appl_id, iucv->dst_name, 8);
1084 				pr_err(
1085 		"Application %s on z/VM guest %s exceeds message limit\n",
1086 					appl_id, user_id);
1087 				err = -EAGAIN;
1088 			} else {
1089 				err = -EPIPE;
1090 			}
1091 
1092 			atomic_dec(&iucv->skbs_in_xmit);
1093 			skb_unlink(skb, &iucv->send_skb_q);
1094 			goto fail;
1095 		}
1096 	}
1097 
1098 	release_sock(sk);
1099 	return len;
1100 
1101 fail:
1102 	kfree_skb(skb);
1103 out:
1104 	release_sock(sk);
1105 	return err;
1106 }
1107 
1108 static struct sk_buff *alloc_iucv_recv_skb(unsigned long len)
1109 {
1110 	size_t headroom, linear;
1111 	struct sk_buff *skb;
1112 	int err;
1113 
1114 	if (len < PAGE_SIZE) {
1115 		headroom = 0;
1116 		linear = len;
1117 	} else {
1118 		headroom = sizeof(struct iucv_array) * (MAX_SKB_FRAGS + 1);
1119 		linear = PAGE_SIZE - headroom;
1120 	}
1121 	skb = alloc_skb_with_frags(headroom + linear, len - linear,
1122 				   0, &err, GFP_ATOMIC | GFP_DMA);
1123 	WARN_ONCE(!skb,
1124 		  "alloc of recv iucv skb len=%lu failed with errcode=%d\n",
1125 		  len, err);
1126 	if (skb) {
1127 		if (headroom)
1128 			skb_reserve(skb, headroom);
1129 		skb_put(skb, linear);
1130 		skb->len = len;
1131 		skb->data_len = len - linear;
1132 	}
1133 	return skb;
1134 }
1135 
1136 /* iucv_process_message() - Receive a single outstanding IUCV message
1137  *
1138  * Locking: must be called with message_q.lock held
1139  */
1140 static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1141 				 struct iucv_path *path,
1142 				 struct iucv_message *msg)
1143 {
1144 	int rc;
1145 	unsigned int len;
1146 
1147 	len = iucv_msg_length(msg);
1148 
1149 	/* store msg target class in the second 4 bytes of skb ctrl buffer */
1150 	/* Note: the first 4 bytes are reserved for msg tag */
1151 	IUCV_SKB_CB(skb)->class = msg->class;
1152 
1153 	/* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1154 	if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1155 		if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1156 			skb->data = NULL;
1157 			skb->len = 0;
1158 		}
1159 	} else {
1160 		if (skb_is_nonlinear(skb)) {
1161 			struct iucv_array *iba = (struct iucv_array *)skb->head;
1162 			int i;
1163 
1164 			iba[0].address = virt_to_dma32(skb->data);
1165 			iba[0].length = (u32)skb_headlen(skb);
1166 			for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1167 				skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1168 
1169 				iba[i + 1].address = virt_to_dma32(skb_frag_address(frag));
1170 				iba[i + 1].length = (u32)skb_frag_size(frag);
1171 			}
1172 			rc = pr_iucv->message_receive(path, msg,
1173 					      IUCV_IPBUFLST,
1174 					      (void *)iba, len, NULL);
1175 		} else {
1176 			rc = pr_iucv->message_receive(path, msg,
1177 					      msg->flags & IUCV_IPRMDATA,
1178 					      skb->data, len, NULL);
1179 		}
1180 		if (rc) {
1181 			kfree_skb(skb);
1182 			return;
1183 		}
1184 		WARN_ON_ONCE(skb->len != len);
1185 	}
1186 
1187 	IUCV_SKB_CB(skb)->offset = 0;
1188 	if (sk_filter(sk, skb)) {
1189 		atomic_inc(&sk->sk_drops);	/* skb rejected by filter */
1190 		kfree_skb(skb);
1191 		return;
1192 	}
1193 	if (__sock_queue_rcv_skb(sk, skb))	/* handle rcv queue full */
1194 		skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
1195 }
1196 
1197 /* iucv_process_message_q() - Process outstanding IUCV messages
1198  *
1199  * Locking: must be called with message_q.lock held
1200  */
1201 static void iucv_process_message_q(struct sock *sk)
1202 {
1203 	struct iucv_sock *iucv = iucv_sk(sk);
1204 	struct sk_buff *skb;
1205 	struct sock_msg_q *p, *n;
1206 
1207 	list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
1208 		skb = alloc_iucv_recv_skb(iucv_msg_length(&p->msg));
1209 		if (!skb)
1210 			break;
1211 		iucv_process_message(sk, skb, p->path, &p->msg);
1212 		list_del(&p->list);
1213 		kfree(p);
1214 		if (!skb_queue_empty(&iucv->backlog_skb_q))
1215 			break;
1216 	}
1217 }
1218 
1219 static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1220 			     size_t len, int flags)
1221 {
1222 	struct sock *sk = sock->sk;
1223 	struct iucv_sock *iucv = iucv_sk(sk);
1224 	unsigned int copied, rlen;
1225 	struct sk_buff *skb, *rskb, *cskb;
1226 	int err = 0;
1227 	u32 offset;
1228 
1229 	if ((sk->sk_state == IUCV_DISCONN) &&
1230 	    skb_queue_empty(&iucv->backlog_skb_q) &&
1231 	    skb_queue_empty(&sk->sk_receive_queue) &&
1232 	    list_empty(&iucv->message_q.list))
1233 		return 0;
1234 
1235 	if (flags & (MSG_OOB))
1236 		return -EOPNOTSUPP;
1237 
1238 	/* receive/dequeue next skb:
1239 	 * the function understands MSG_PEEK and, thus, does not dequeue skb
1240 	 * only refcount is increased.
1241 	 */
1242 	skb = skb_recv_datagram(sk, flags, &err);
1243 	if (!skb) {
1244 		if (sk->sk_shutdown & RCV_SHUTDOWN)
1245 			return 0;
1246 		return err;
1247 	}
1248 
1249 	offset = IUCV_SKB_CB(skb)->offset;
1250 	rlen   = skb->len - offset;		/* real length of skb */
1251 	copied = min_t(unsigned int, rlen, len);
1252 	if (!rlen)
1253 		sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
1254 
1255 	cskb = skb;
1256 	if (skb_copy_datagram_msg(cskb, offset, msg, copied)) {
1257 		err = -EFAULT;
1258 		goto err_out;
1259 	}
1260 
1261 	/* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1262 	if (sk->sk_type == SOCK_SEQPACKET) {
1263 		if (copied < rlen)
1264 			msg->msg_flags |= MSG_TRUNC;
1265 		/* each iucv message contains a complete record */
1266 		msg->msg_flags |= MSG_EOR;
1267 	}
1268 
1269 	/* create control message to store iucv msg target class:
1270 	 * get the trgcls from the control buffer of the skb due to
1271 	 * fragmentation of original iucv message. */
1272 	err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1273 		       sizeof(IUCV_SKB_CB(skb)->class),
1274 		       (void *)&IUCV_SKB_CB(skb)->class);
1275 	if (err)
1276 		goto err_out;
1277 
1278 	/* Mark read part of skb as used */
1279 	if (!(flags & MSG_PEEK)) {
1280 
1281 		/* SOCK_STREAM: re-queue skb if it contains unreceived data */
1282 		if (sk->sk_type == SOCK_STREAM) {
1283 			if (copied < rlen) {
1284 				IUCV_SKB_CB(skb)->offset = offset + copied;
1285 				skb_queue_head(&sk->sk_receive_queue, skb);
1286 				goto done;
1287 			}
1288 		}
1289 
1290 		consume_skb(skb);
1291 		if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1292 			atomic_inc(&iucv->msg_recv);
1293 			if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
1294 				WARN_ON(1);
1295 				iucv_sock_close(sk);
1296 				return -EFAULT;
1297 			}
1298 		}
1299 
1300 		/* Queue backlog skbs */
1301 		spin_lock_bh(&iucv->message_q.lock);
1302 		rskb = skb_dequeue(&iucv->backlog_skb_q);
1303 		while (rskb) {
1304 			IUCV_SKB_CB(rskb)->offset = 0;
1305 			if (__sock_queue_rcv_skb(sk, rskb)) {
1306 				/* handle rcv queue full */
1307 				skb_queue_head(&iucv->backlog_skb_q,
1308 						rskb);
1309 				break;
1310 			}
1311 			rskb = skb_dequeue(&iucv->backlog_skb_q);
1312 		}
1313 		if (skb_queue_empty(&iucv->backlog_skb_q)) {
1314 			if (!list_empty(&iucv->message_q.list))
1315 				iucv_process_message_q(sk);
1316 			if (atomic_read(&iucv->msg_recv) >=
1317 							iucv->msglimit / 2) {
1318 				err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
1319 				if (err) {
1320 					sk->sk_state = IUCV_DISCONN;
1321 					sk->sk_state_change(sk);
1322 				}
1323 			}
1324 		}
1325 		spin_unlock_bh(&iucv->message_q.lock);
1326 	}
1327 
1328 done:
1329 	/* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1330 	if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1331 		copied = rlen;
1332 	if (flags & MSG_PEEK)
1333 		skb_unref(skb);
1334 
1335 	return copied;
1336 
1337 err_out:
1338 	if (!(flags & MSG_PEEK))
1339 		skb_queue_head(&sk->sk_receive_queue, skb);
1340 	else
1341 		skb_unref(skb);
1342 
1343 	return err;
1344 }
1345 
1346 static inline __poll_t iucv_accept_poll(struct sock *parent)
1347 {
1348 	struct iucv_sock *isk, *n;
1349 	struct sock *sk;
1350 
1351 	list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
1352 		sk = (struct sock *) isk;
1353 
1354 		if (sk->sk_state == IUCV_CONNECTED)
1355 			return EPOLLIN | EPOLLRDNORM;
1356 	}
1357 
1358 	return 0;
1359 }
1360 
1361 static __poll_t iucv_sock_poll(struct file *file, struct socket *sock,
1362 			       poll_table *wait)
1363 {
1364 	struct sock *sk = sock->sk;
1365 	__poll_t mask = 0;
1366 
1367 	sock_poll_wait(file, sock, wait);
1368 
1369 	if (sk->sk_state == IUCV_LISTEN)
1370 		return iucv_accept_poll(sk);
1371 
1372 	if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1373 		mask |= EPOLLERR |
1374 			(sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
1375 
1376 	if (sk->sk_shutdown & RCV_SHUTDOWN)
1377 		mask |= EPOLLRDHUP;
1378 
1379 	if (sk->sk_shutdown == SHUTDOWN_MASK)
1380 		mask |= EPOLLHUP;
1381 
1382 	if (!skb_queue_empty(&sk->sk_receive_queue) ||
1383 	    (sk->sk_shutdown & RCV_SHUTDOWN))
1384 		mask |= EPOLLIN | EPOLLRDNORM;
1385 
1386 	if (sk->sk_state == IUCV_CLOSED)
1387 		mask |= EPOLLHUP;
1388 
1389 	if (sk->sk_state == IUCV_DISCONN)
1390 		mask |= EPOLLIN;
1391 
1392 	if (sock_writeable(sk) && iucv_below_msglim(sk))
1393 		mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
1394 	else
1395 		sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1396 
1397 	return mask;
1398 }
1399 
1400 static int iucv_sock_shutdown(struct socket *sock, int how)
1401 {
1402 	struct sock *sk = sock->sk;
1403 	struct iucv_sock *iucv = iucv_sk(sk);
1404 	struct iucv_message txmsg;
1405 	int err = 0;
1406 
1407 	how++;
1408 
1409 	if ((how & ~SHUTDOWN_MASK) || !how)
1410 		return -EINVAL;
1411 
1412 	lock_sock(sk);
1413 	switch (sk->sk_state) {
1414 	case IUCV_LISTEN:
1415 	case IUCV_DISCONN:
1416 	case IUCV_CLOSING:
1417 	case IUCV_CLOSED:
1418 		err = -ENOTCONN;
1419 		goto fail;
1420 	default:
1421 		break;
1422 	}
1423 
1424 	if ((how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) &&
1425 	    sk->sk_state == IUCV_CONNECTED) {
1426 		if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1427 			txmsg.class = 0;
1428 			txmsg.tag = 0;
1429 			err = pr_iucv->message_send(iucv->path, &txmsg,
1430 				IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8);
1431 			if (err) {
1432 				switch (err) {
1433 				case 1:
1434 					err = -ENOTCONN;
1435 					break;
1436 				case 2:
1437 					err = -ECONNRESET;
1438 					break;
1439 				default:
1440 					err = -ENOTCONN;
1441 					break;
1442 				}
1443 			}
1444 		} else
1445 			iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
1446 	}
1447 
1448 	sk->sk_shutdown |= how;
1449 	if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
1450 		if ((iucv->transport == AF_IUCV_TRANS_IUCV) &&
1451 		    iucv->path) {
1452 			err = pr_iucv->path_quiesce(iucv->path, NULL);
1453 			if (err)
1454 				err = -ENOTCONN;
1455 /*			skb_queue_purge(&sk->sk_receive_queue); */
1456 		}
1457 		skb_queue_purge(&sk->sk_receive_queue);
1458 	}
1459 
1460 	/* Wake up anyone sleeping in poll */
1461 	sk->sk_state_change(sk);
1462 
1463 fail:
1464 	release_sock(sk);
1465 	return err;
1466 }
1467 
1468 static int iucv_sock_release(struct socket *sock)
1469 {
1470 	struct sock *sk = sock->sk;
1471 	int err = 0;
1472 
1473 	if (!sk)
1474 		return 0;
1475 
1476 	iucv_sock_close(sk);
1477 
1478 	sock_orphan(sk);
1479 	iucv_sock_kill(sk);
1480 	return err;
1481 }
1482 
1483 /* getsockopt and setsockopt */
1484 static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
1485 				sockptr_t optval, unsigned int optlen)
1486 {
1487 	struct sock *sk = sock->sk;
1488 	struct iucv_sock *iucv = iucv_sk(sk);
1489 	int val;
1490 	int rc;
1491 
1492 	if (level != SOL_IUCV)
1493 		return -ENOPROTOOPT;
1494 
1495 	if (optlen < sizeof(int))
1496 		return -EINVAL;
1497 
1498 	if (copy_from_sockptr(&val, optval, sizeof(int)))
1499 		return -EFAULT;
1500 
1501 	rc = 0;
1502 
1503 	lock_sock(sk);
1504 	switch (optname) {
1505 	case SO_IPRMDATA_MSG:
1506 		if (val)
1507 			iucv->flags |= IUCV_IPRMDATA;
1508 		else
1509 			iucv->flags &= ~IUCV_IPRMDATA;
1510 		break;
1511 	case SO_MSGLIMIT:
1512 		switch (sk->sk_state) {
1513 		case IUCV_OPEN:
1514 		case IUCV_BOUND:
1515 			if (val < 1 || val > U16_MAX)
1516 				rc = -EINVAL;
1517 			else
1518 				iucv->msglimit = val;
1519 			break;
1520 		default:
1521 			rc = -EINVAL;
1522 			break;
1523 		}
1524 		break;
1525 	default:
1526 		rc = -ENOPROTOOPT;
1527 		break;
1528 	}
1529 	release_sock(sk);
1530 
1531 	return rc;
1532 }
1533 
1534 static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1535 				char __user *optval, int __user *optlen)
1536 {
1537 	struct sock *sk = sock->sk;
1538 	struct iucv_sock *iucv = iucv_sk(sk);
1539 	unsigned int val;
1540 	int len;
1541 
1542 	if (level != SOL_IUCV)
1543 		return -ENOPROTOOPT;
1544 
1545 	if (get_user(len, optlen))
1546 		return -EFAULT;
1547 
1548 	if (len < 0)
1549 		return -EINVAL;
1550 
1551 	len = min_t(unsigned int, len, sizeof(int));
1552 
1553 	switch (optname) {
1554 	case SO_IPRMDATA_MSG:
1555 		val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1556 		break;
1557 	case SO_MSGLIMIT:
1558 		lock_sock(sk);
1559 		val = (iucv->path != NULL) ? iucv->path->msglim	/* connected */
1560 					   : iucv->msglimit;	/* default */
1561 		release_sock(sk);
1562 		break;
1563 	case SO_MSGSIZE:
1564 		if (sk->sk_state == IUCV_OPEN)
1565 			return -EBADFD;
1566 		val = (iucv->hs_dev) ? iucv->hs_dev->mtu -
1567 				sizeof(struct af_iucv_trans_hdr) - ETH_HLEN :
1568 				0x7fffffff;
1569 		break;
1570 	default:
1571 		return -ENOPROTOOPT;
1572 	}
1573 
1574 	if (put_user(len, optlen))
1575 		return -EFAULT;
1576 	if (copy_to_user(optval, &val, len))
1577 		return -EFAULT;
1578 
1579 	return 0;
1580 }
1581 
1582 
1583 /* Callback wrappers - called from iucv base support */
1584 static int iucv_callback_connreq(struct iucv_path *path,
1585 				 u8 ipvmid[8], u8 ipuser[16])
1586 {
1587 	unsigned char user_data[16];
1588 	unsigned char nuser_data[16];
1589 	unsigned char src_name[8];
1590 	struct sock *sk, *nsk;
1591 	struct iucv_sock *iucv, *niucv;
1592 	int err;
1593 
1594 	memcpy(src_name, ipuser, 8);
1595 	EBCASC(src_name, 8);
1596 	/* Find out if this path belongs to af_iucv. */
1597 	read_lock(&iucv_sk_list.lock);
1598 	iucv = NULL;
1599 	sk = NULL;
1600 	sk_for_each(sk, &iucv_sk_list.head)
1601 		if (sk->sk_state == IUCV_LISTEN &&
1602 		    !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1603 			/*
1604 			 * Found a listening socket with
1605 			 * src_name == ipuser[0-7].
1606 			 */
1607 			iucv = iucv_sk(sk);
1608 			break;
1609 		}
1610 	read_unlock(&iucv_sk_list.lock);
1611 	if (!iucv)
1612 		/* No socket found, not one of our paths. */
1613 		return -EINVAL;
1614 
1615 	bh_lock_sock(sk);
1616 
1617 	/* Check if parent socket is listening */
1618 	low_nmcpy(user_data, iucv->src_name);
1619 	high_nmcpy(user_data, iucv->dst_name);
1620 	ASCEBC(user_data, sizeof(user_data));
1621 	if (sk->sk_state != IUCV_LISTEN) {
1622 		err = pr_iucv->path_sever(path, user_data);
1623 		iucv_path_free(path);
1624 		goto fail;
1625 	}
1626 
1627 	/* Check for backlog size */
1628 	if (sk_acceptq_is_full(sk)) {
1629 		err = pr_iucv->path_sever(path, user_data);
1630 		iucv_path_free(path);
1631 		goto fail;
1632 	}
1633 
1634 	/* Create the new socket */
1635 	nsk = iucv_sock_alloc(NULL, sk->sk_protocol, GFP_ATOMIC, 0);
1636 	if (!nsk) {
1637 		err = pr_iucv->path_sever(path, user_data);
1638 		iucv_path_free(path);
1639 		goto fail;
1640 	}
1641 
1642 	niucv = iucv_sk(nsk);
1643 	iucv_sock_init(nsk, sk);
1644 	niucv->transport = AF_IUCV_TRANS_IUCV;
1645 	nsk->sk_allocation |= GFP_DMA;
1646 
1647 	/* Set the new iucv_sock */
1648 	memcpy(niucv->dst_name, ipuser + 8, 8);
1649 	EBCASC(niucv->dst_name, 8);
1650 	memcpy(niucv->dst_user_id, ipvmid, 8);
1651 	memcpy(niucv->src_name, iucv->src_name, 8);
1652 	memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1653 	niucv->path = path;
1654 
1655 	/* Call iucv_accept */
1656 	high_nmcpy(nuser_data, ipuser + 8);
1657 	memcpy(nuser_data + 8, niucv->src_name, 8);
1658 	ASCEBC(nuser_data + 8, 8);
1659 
1660 	/* set message limit for path based on msglimit of accepting socket */
1661 	niucv->msglimit = iucv->msglimit;
1662 	path->msglim = iucv->msglimit;
1663 	err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
1664 	if (err) {
1665 		iucv_sever_path(nsk, 1);
1666 		iucv_sock_kill(nsk);
1667 		goto fail;
1668 	}
1669 
1670 	iucv_accept_enqueue(sk, nsk);
1671 
1672 	/* Wake up accept */
1673 	nsk->sk_state = IUCV_CONNECTED;
1674 	sk->sk_data_ready(sk);
1675 	err = 0;
1676 fail:
1677 	bh_unlock_sock(sk);
1678 	return 0;
1679 }
1680 
1681 static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1682 {
1683 	struct sock *sk = path->private;
1684 
1685 	sk->sk_state = IUCV_CONNECTED;
1686 	sk->sk_state_change(sk);
1687 }
1688 
1689 static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1690 {
1691 	struct sock *sk = path->private;
1692 	struct iucv_sock *iucv = iucv_sk(sk);
1693 	struct sk_buff *skb;
1694 	struct sock_msg_q *save_msg;
1695 	int len;
1696 
1697 	if (sk->sk_shutdown & RCV_SHUTDOWN) {
1698 		pr_iucv->message_reject(path, msg);
1699 		return;
1700 	}
1701 
1702 	spin_lock(&iucv->message_q.lock);
1703 
1704 	if (!list_empty(&iucv->message_q.list) ||
1705 	    !skb_queue_empty(&iucv->backlog_skb_q))
1706 		goto save_message;
1707 
1708 	len = atomic_read(&sk->sk_rmem_alloc);
1709 	len += SKB_TRUESIZE(iucv_msg_length(msg));
1710 	if (len > sk->sk_rcvbuf)
1711 		goto save_message;
1712 
1713 	skb = alloc_iucv_recv_skb(iucv_msg_length(msg));
1714 	if (!skb)
1715 		goto save_message;
1716 
1717 	iucv_process_message(sk, skb, path, msg);
1718 	goto out_unlock;
1719 
1720 save_message:
1721 	save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
1722 	if (!save_msg)
1723 		goto out_unlock;
1724 	save_msg->path = path;
1725 	save_msg->msg = *msg;
1726 
1727 	list_add_tail(&save_msg->list, &iucv->message_q.list);
1728 
1729 out_unlock:
1730 	spin_unlock(&iucv->message_q.lock);
1731 }
1732 
1733 static void iucv_callback_txdone(struct iucv_path *path,
1734 				 struct iucv_message *msg)
1735 {
1736 	struct sock *sk = path->private;
1737 	struct sk_buff *this = NULL;
1738 	struct sk_buff_head *list;
1739 	struct sk_buff *list_skb;
1740 	struct iucv_sock *iucv;
1741 	unsigned long flags;
1742 
1743 	iucv = iucv_sk(sk);
1744 	list = &iucv->send_skb_q;
1745 
1746 	bh_lock_sock(sk);
1747 
1748 	spin_lock_irqsave(&list->lock, flags);
1749 	skb_queue_walk(list, list_skb) {
1750 		if (msg->tag == IUCV_SKB_CB(list_skb)->tag) {
1751 			this = list_skb;
1752 			break;
1753 		}
1754 	}
1755 	if (this) {
1756 		atomic_dec(&iucv->skbs_in_xmit);
1757 		__skb_unlink(this, list);
1758 	}
1759 
1760 	spin_unlock_irqrestore(&list->lock, flags);
1761 
1762 	if (this) {
1763 		consume_skb(this);
1764 		/* wake up any process waiting for sending */
1765 		iucv_sock_wake_msglim(sk);
1766 	}
1767 
1768 	if (sk->sk_state == IUCV_CLOSING) {
1769 		if (atomic_read(&iucv->skbs_in_xmit) == 0) {
1770 			sk->sk_state = IUCV_CLOSED;
1771 			sk->sk_state_change(sk);
1772 		}
1773 	}
1774 	bh_unlock_sock(sk);
1775 
1776 }
1777 
1778 static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1779 {
1780 	struct sock *sk = path->private;
1781 
1782 	if (sk->sk_state == IUCV_CLOSED)
1783 		return;
1784 
1785 	bh_lock_sock(sk);
1786 	iucv_sever_path(sk, 1);
1787 	sk->sk_state = IUCV_DISCONN;
1788 
1789 	sk->sk_state_change(sk);
1790 	bh_unlock_sock(sk);
1791 }
1792 
1793 /* called if the other communication side shuts down its RECV direction;
1794  * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1795  */
1796 static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1797 {
1798 	struct sock *sk = path->private;
1799 
1800 	bh_lock_sock(sk);
1801 	if (sk->sk_state != IUCV_CLOSED) {
1802 		sk->sk_shutdown |= SEND_SHUTDOWN;
1803 		sk->sk_state_change(sk);
1804 	}
1805 	bh_unlock_sock(sk);
1806 }
1807 
1808 static struct iucv_handler af_iucv_handler = {
1809 	.path_pending		= iucv_callback_connreq,
1810 	.path_complete		= iucv_callback_connack,
1811 	.path_severed		= iucv_callback_connrej,
1812 	.message_pending	= iucv_callback_rx,
1813 	.message_complete	= iucv_callback_txdone,
1814 	.path_quiesced		= iucv_callback_shutdown,
1815 };
1816 
1817 /***************** HiperSockets transport callbacks ********************/
1818 static void afiucv_swap_src_dest(struct sk_buff *skb)
1819 {
1820 	struct af_iucv_trans_hdr *trans_hdr = iucv_trans_hdr(skb);
1821 	char tmpID[8];
1822 	char tmpName[8];
1823 
1824 	ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1825 	ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1826 	ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1827 	ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1828 	memcpy(tmpID, trans_hdr->srcUserID, 8);
1829 	memcpy(tmpName, trans_hdr->srcAppName, 8);
1830 	memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1831 	memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1832 	memcpy(trans_hdr->destUserID, tmpID, 8);
1833 	memcpy(trans_hdr->destAppName, tmpName, 8);
1834 	skb_push(skb, ETH_HLEN);
1835 	memset(skb->data, 0, ETH_HLEN);
1836 }
1837 
1838 /*
1839  * afiucv_hs_callback_syn - react on received SYN
1840  */
1841 static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1842 {
1843 	struct af_iucv_trans_hdr *trans_hdr = iucv_trans_hdr(skb);
1844 	struct sock *nsk;
1845 	struct iucv_sock *iucv, *niucv;
1846 	int err;
1847 
1848 	iucv = iucv_sk(sk);
1849 	if (!iucv) {
1850 		/* no sock - connection refused */
1851 		afiucv_swap_src_dest(skb);
1852 		trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1853 		err = dev_queue_xmit(skb);
1854 		goto out;
1855 	}
1856 
1857 	nsk = iucv_sock_alloc(NULL, sk->sk_protocol, GFP_ATOMIC, 0);
1858 	bh_lock_sock(sk);
1859 	if ((sk->sk_state != IUCV_LISTEN) ||
1860 	    sk_acceptq_is_full(sk) ||
1861 	    !nsk) {
1862 		/* error on server socket - connection refused */
1863 		afiucv_swap_src_dest(skb);
1864 		trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1865 		err = dev_queue_xmit(skb);
1866 		iucv_sock_kill(nsk);
1867 		bh_unlock_sock(sk);
1868 		goto out;
1869 	}
1870 
1871 	niucv = iucv_sk(nsk);
1872 	iucv_sock_init(nsk, sk);
1873 	niucv->transport = AF_IUCV_TRANS_HIPER;
1874 	niucv->msglimit = iucv->msglimit;
1875 	if (!trans_hdr->window)
1876 		niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
1877 	else
1878 		niucv->msglimit_peer = trans_hdr->window;
1879 	memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
1880 	memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
1881 	memcpy(niucv->src_name, iucv->src_name, 8);
1882 	memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1883 	nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
1884 	niucv->hs_dev = iucv->hs_dev;
1885 	dev_hold(niucv->hs_dev);
1886 	afiucv_swap_src_dest(skb);
1887 	trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
1888 	trans_hdr->window = niucv->msglimit;
1889 	/* if receiver acks the xmit connection is established */
1890 	err = dev_queue_xmit(skb);
1891 	if (!err) {
1892 		iucv_accept_enqueue(sk, nsk);
1893 		nsk->sk_state = IUCV_CONNECTED;
1894 		sk->sk_data_ready(sk);
1895 	} else
1896 		iucv_sock_kill(nsk);
1897 	bh_unlock_sock(sk);
1898 
1899 out:
1900 	return NET_RX_SUCCESS;
1901 }
1902 
1903 /*
1904  * afiucv_hs_callback_synack() - react on received SYN-ACK
1905  */
1906 static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
1907 {
1908 	struct iucv_sock *iucv = iucv_sk(sk);
1909 
1910 	if (!iucv || sk->sk_state != IUCV_BOUND) {
1911 		kfree_skb(skb);
1912 		return NET_RX_SUCCESS;
1913 	}
1914 
1915 	bh_lock_sock(sk);
1916 	iucv->msglimit_peer = iucv_trans_hdr(skb)->window;
1917 	sk->sk_state = IUCV_CONNECTED;
1918 	sk->sk_state_change(sk);
1919 	bh_unlock_sock(sk);
1920 	consume_skb(skb);
1921 	return NET_RX_SUCCESS;
1922 }
1923 
1924 /*
1925  * afiucv_hs_callback_synfin() - react on received SYN_FIN
1926  */
1927 static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
1928 {
1929 	struct iucv_sock *iucv = iucv_sk(sk);
1930 
1931 	if (!iucv || sk->sk_state != IUCV_BOUND) {
1932 		kfree_skb(skb);
1933 		return NET_RX_SUCCESS;
1934 	}
1935 
1936 	bh_lock_sock(sk);
1937 	sk->sk_state = IUCV_DISCONN;
1938 	sk->sk_state_change(sk);
1939 	bh_unlock_sock(sk);
1940 	consume_skb(skb);
1941 	return NET_RX_SUCCESS;
1942 }
1943 
1944 /*
1945  * afiucv_hs_callback_fin() - react on received FIN
1946  */
1947 static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
1948 {
1949 	struct iucv_sock *iucv = iucv_sk(sk);
1950 
1951 	/* other end of connection closed */
1952 	if (!iucv) {
1953 		kfree_skb(skb);
1954 		return NET_RX_SUCCESS;
1955 	}
1956 
1957 	bh_lock_sock(sk);
1958 	if (sk->sk_state == IUCV_CONNECTED) {
1959 		sk->sk_state = IUCV_DISCONN;
1960 		sk->sk_state_change(sk);
1961 	}
1962 	bh_unlock_sock(sk);
1963 	consume_skb(skb);
1964 	return NET_RX_SUCCESS;
1965 }
1966 
1967 /*
1968  * afiucv_hs_callback_win() - react on received WIN
1969  */
1970 static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
1971 {
1972 	struct iucv_sock *iucv = iucv_sk(sk);
1973 
1974 	if (!iucv)
1975 		return NET_RX_SUCCESS;
1976 
1977 	if (sk->sk_state != IUCV_CONNECTED)
1978 		return NET_RX_SUCCESS;
1979 
1980 	atomic_sub(iucv_trans_hdr(skb)->window, &iucv->msg_sent);
1981 	iucv_sock_wake_msglim(sk);
1982 	return NET_RX_SUCCESS;
1983 }
1984 
1985 /*
1986  * afiucv_hs_callback_rx() - react on received data
1987  */
1988 static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
1989 {
1990 	struct iucv_sock *iucv = iucv_sk(sk);
1991 
1992 	if (!iucv) {
1993 		kfree_skb(skb);
1994 		return NET_RX_SUCCESS;
1995 	}
1996 
1997 	if (sk->sk_state != IUCV_CONNECTED) {
1998 		kfree_skb(skb);
1999 		return NET_RX_SUCCESS;
2000 	}
2001 
2002 	if (sk->sk_shutdown & RCV_SHUTDOWN) {
2003 		kfree_skb(skb);
2004 		return NET_RX_SUCCESS;
2005 	}
2006 
2007 	/* write stuff from iucv_msg to skb cb */
2008 	skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2009 	skb_reset_transport_header(skb);
2010 	skb_reset_network_header(skb);
2011 	IUCV_SKB_CB(skb)->offset = 0;
2012 	if (sk_filter(sk, skb)) {
2013 		atomic_inc(&sk->sk_drops);	/* skb rejected by filter */
2014 		kfree_skb(skb);
2015 		return NET_RX_SUCCESS;
2016 	}
2017 
2018 	spin_lock(&iucv->message_q.lock);
2019 	if (skb_queue_empty(&iucv->backlog_skb_q)) {
2020 		if (__sock_queue_rcv_skb(sk, skb))
2021 			/* handle rcv queue full */
2022 			skb_queue_tail(&iucv->backlog_skb_q, skb);
2023 	} else
2024 		skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2025 	spin_unlock(&iucv->message_q.lock);
2026 	return NET_RX_SUCCESS;
2027 }
2028 
2029 /*
2030  * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2031  *                   transport
2032  *                   called from netif RX softirq
2033  */
2034 static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2035 	struct packet_type *pt, struct net_device *orig_dev)
2036 {
2037 	struct sock *sk;
2038 	struct iucv_sock *iucv;
2039 	struct af_iucv_trans_hdr *trans_hdr;
2040 	int err = NET_RX_SUCCESS;
2041 	char nullstring[8];
2042 
2043 	if (!pskb_may_pull(skb, sizeof(*trans_hdr))) {
2044 		kfree_skb(skb);
2045 		return NET_RX_SUCCESS;
2046 	}
2047 
2048 	trans_hdr = iucv_trans_hdr(skb);
2049 	EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2050 	EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2051 	EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2052 	EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2053 	memset(nullstring, 0, sizeof(nullstring));
2054 	iucv = NULL;
2055 	sk = NULL;
2056 	read_lock(&iucv_sk_list.lock);
2057 	sk_for_each(sk, &iucv_sk_list.head) {
2058 		if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2059 			if ((!memcmp(&iucv_sk(sk)->src_name,
2060 				     trans_hdr->destAppName, 8)) &&
2061 			    (!memcmp(&iucv_sk(sk)->src_user_id,
2062 				     trans_hdr->destUserID, 8)) &&
2063 			    (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2064 			    (!memcmp(&iucv_sk(sk)->dst_user_id,
2065 				     nullstring, 8))) {
2066 				iucv = iucv_sk(sk);
2067 				break;
2068 			}
2069 		} else {
2070 			if ((!memcmp(&iucv_sk(sk)->src_name,
2071 				     trans_hdr->destAppName, 8)) &&
2072 			    (!memcmp(&iucv_sk(sk)->src_user_id,
2073 				     trans_hdr->destUserID, 8)) &&
2074 			    (!memcmp(&iucv_sk(sk)->dst_name,
2075 				     trans_hdr->srcAppName, 8)) &&
2076 			    (!memcmp(&iucv_sk(sk)->dst_user_id,
2077 				     trans_hdr->srcUserID, 8))) {
2078 				iucv = iucv_sk(sk);
2079 				break;
2080 			}
2081 		}
2082 	}
2083 	read_unlock(&iucv_sk_list.lock);
2084 	if (!iucv)
2085 		sk = NULL;
2086 
2087 	/* no sock
2088 	how should we send with no sock
2089 	1) send without sock no send rc checking?
2090 	2) introduce default sock to handle this cases
2091 
2092 	 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2093 	 data -> send FIN
2094 	 SYN|ACK, SYN|FIN, FIN -> no action? */
2095 
2096 	switch (trans_hdr->flags) {
2097 	case AF_IUCV_FLAG_SYN:
2098 		/* connect request */
2099 		err = afiucv_hs_callback_syn(sk, skb);
2100 		break;
2101 	case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2102 		/* connect request confirmed */
2103 		err = afiucv_hs_callback_synack(sk, skb);
2104 		break;
2105 	case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2106 		/* connect request refused */
2107 		err = afiucv_hs_callback_synfin(sk, skb);
2108 		break;
2109 	case (AF_IUCV_FLAG_FIN):
2110 		/* close request */
2111 		err = afiucv_hs_callback_fin(sk, skb);
2112 		break;
2113 	case (AF_IUCV_FLAG_WIN):
2114 		err = afiucv_hs_callback_win(sk, skb);
2115 		if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
2116 			consume_skb(skb);
2117 			break;
2118 		}
2119 		fallthrough;	/* and receive non-zero length data */
2120 	case (AF_IUCV_FLAG_SHT):
2121 		/* shutdown request */
2122 		fallthrough;	/* and receive zero length data */
2123 	case 0:
2124 		/* plain data frame */
2125 		IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
2126 		err = afiucv_hs_callback_rx(sk, skb);
2127 		break;
2128 	default:
2129 		kfree_skb(skb);
2130 	}
2131 
2132 	return err;
2133 }
2134 
2135 /*
2136  * afiucv_hs_callback_txnotify() - handle send notifications from HiperSockets
2137  *                                 transport
2138  */
2139 static void afiucv_hs_callback_txnotify(struct sock *sk, enum iucv_tx_notify n)
2140 {
2141 	struct iucv_sock *iucv = iucv_sk(sk);
2142 
2143 	if (sock_flag(sk, SOCK_ZAPPED))
2144 		return;
2145 
2146 	switch (n) {
2147 	case TX_NOTIFY_OK:
2148 		atomic_dec(&iucv->skbs_in_xmit);
2149 		iucv_sock_wake_msglim(sk);
2150 		break;
2151 	case TX_NOTIFY_PENDING:
2152 		atomic_inc(&iucv->pendings);
2153 		break;
2154 	case TX_NOTIFY_DELAYED_OK:
2155 		atomic_dec(&iucv->skbs_in_xmit);
2156 		if (atomic_dec_return(&iucv->pendings) <= 0)
2157 			iucv_sock_wake_msglim(sk);
2158 		break;
2159 	default:
2160 		atomic_dec(&iucv->skbs_in_xmit);
2161 		if (sk->sk_state == IUCV_CONNECTED) {
2162 			sk->sk_state = IUCV_DISCONN;
2163 			sk->sk_state_change(sk);
2164 		}
2165 	}
2166 
2167 	if (sk->sk_state == IUCV_CLOSING) {
2168 		if (atomic_read(&iucv->skbs_in_xmit) == 0) {
2169 			sk->sk_state = IUCV_CLOSED;
2170 			sk->sk_state_change(sk);
2171 		}
2172 	}
2173 }
2174 
2175 /*
2176  * afiucv_netdev_event: handle netdev notifier chain events
2177  */
2178 static int afiucv_netdev_event(struct notifier_block *this,
2179 			       unsigned long event, void *ptr)
2180 {
2181 	struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2182 	struct sock *sk;
2183 	struct iucv_sock *iucv;
2184 
2185 	switch (event) {
2186 	case NETDEV_REBOOT:
2187 	case NETDEV_GOING_DOWN:
2188 		sk_for_each(sk, &iucv_sk_list.head) {
2189 			iucv = iucv_sk(sk);
2190 			if ((iucv->hs_dev == event_dev) &&
2191 			    (sk->sk_state == IUCV_CONNECTED)) {
2192 				if (event == NETDEV_GOING_DOWN)
2193 					iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
2194 				sk->sk_state = IUCV_DISCONN;
2195 				sk->sk_state_change(sk);
2196 			}
2197 		}
2198 		break;
2199 	case NETDEV_DOWN:
2200 	case NETDEV_UNREGISTER:
2201 	default:
2202 		break;
2203 	}
2204 	return NOTIFY_DONE;
2205 }
2206 
2207 static struct notifier_block afiucv_netdev_notifier = {
2208 	.notifier_call = afiucv_netdev_event,
2209 };
2210 
2211 static const struct proto_ops iucv_sock_ops = {
2212 	.family		= PF_IUCV,
2213 	.owner		= THIS_MODULE,
2214 	.release	= iucv_sock_release,
2215 	.bind		= iucv_sock_bind,
2216 	.connect	= iucv_sock_connect,
2217 	.listen		= iucv_sock_listen,
2218 	.accept		= iucv_sock_accept,
2219 	.getname	= iucv_sock_getname,
2220 	.sendmsg	= iucv_sock_sendmsg,
2221 	.recvmsg	= iucv_sock_recvmsg,
2222 	.poll		= iucv_sock_poll,
2223 	.ioctl		= sock_no_ioctl,
2224 	.mmap		= sock_no_mmap,
2225 	.socketpair	= sock_no_socketpair,
2226 	.shutdown	= iucv_sock_shutdown,
2227 	.setsockopt	= iucv_sock_setsockopt,
2228 	.getsockopt	= iucv_sock_getsockopt,
2229 };
2230 
2231 static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
2232 			    int kern)
2233 {
2234 	struct sock *sk;
2235 
2236 	if (protocol && protocol != PF_IUCV)
2237 		return -EPROTONOSUPPORT;
2238 
2239 	sock->state = SS_UNCONNECTED;
2240 
2241 	switch (sock->type) {
2242 	case SOCK_STREAM:
2243 	case SOCK_SEQPACKET:
2244 		/* currently, proto ops can handle both sk types */
2245 		sock->ops = &iucv_sock_ops;
2246 		break;
2247 	default:
2248 		return -ESOCKTNOSUPPORT;
2249 	}
2250 
2251 	sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL, kern);
2252 	if (!sk)
2253 		return -ENOMEM;
2254 
2255 	iucv_sock_init(sk, NULL);
2256 
2257 	return 0;
2258 }
2259 
2260 static const struct net_proto_family iucv_sock_family_ops = {
2261 	.family	= AF_IUCV,
2262 	.owner	= THIS_MODULE,
2263 	.create	= iucv_sock_create,
2264 };
2265 
2266 static struct packet_type iucv_packet_type = {
2267 	.type = cpu_to_be16(ETH_P_AF_IUCV),
2268 	.func = afiucv_hs_rcv,
2269 };
2270 
2271 static int __init afiucv_init(void)
2272 {
2273 	int err;
2274 
2275 	if (MACHINE_IS_VM && IS_ENABLED(CONFIG_IUCV)) {
2276 		cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2277 		if (unlikely(err)) {
2278 			WARN_ON(err);
2279 			err = -EPROTONOSUPPORT;
2280 			goto out;
2281 		}
2282 
2283 		pr_iucv = &iucv_if;
2284 	} else {
2285 		memset(&iucv_userid, 0, sizeof(iucv_userid));
2286 		pr_iucv = NULL;
2287 	}
2288 
2289 	err = proto_register(&iucv_proto, 0);
2290 	if (err)
2291 		goto out;
2292 	err = sock_register(&iucv_sock_family_ops);
2293 	if (err)
2294 		goto out_proto;
2295 
2296 	if (pr_iucv) {
2297 		err = pr_iucv->iucv_register(&af_iucv_handler, 0);
2298 		if (err)
2299 			goto out_sock;
2300 	}
2301 
2302 	err = register_netdevice_notifier(&afiucv_netdev_notifier);
2303 	if (err)
2304 		goto out_notifier;
2305 
2306 	dev_add_pack(&iucv_packet_type);
2307 	return 0;
2308 
2309 out_notifier:
2310 	if (pr_iucv)
2311 		pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2312 out_sock:
2313 	sock_unregister(PF_IUCV);
2314 out_proto:
2315 	proto_unregister(&iucv_proto);
2316 out:
2317 	return err;
2318 }
2319 
2320 static void __exit afiucv_exit(void)
2321 {
2322 	if (pr_iucv)
2323 		pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2324 
2325 	unregister_netdevice_notifier(&afiucv_netdev_notifier);
2326 	dev_remove_pack(&iucv_packet_type);
2327 	sock_unregister(PF_IUCV);
2328 	proto_unregister(&iucv_proto);
2329 }
2330 
2331 module_init(afiucv_init);
2332 module_exit(afiucv_exit);
2333 
2334 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2335 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2336 MODULE_VERSION(VERSION);
2337 MODULE_LICENSE("GPL");
2338 MODULE_ALIAS_NETPROTO(PF_IUCV);
2339