xref: /linux/net/iucv/af_iucv.c (revision 151ebcf0797b1a3ba53c8843dc21748c80e098c7)
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 	if (iucv->path) {
339 		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 	skb = skb_recv_datagram(sk, flags, &err);
1241 	if (!skb) {
1242 		if (sk->sk_shutdown & RCV_SHUTDOWN)
1243 			return 0;
1244 		return err;
1245 	}
1246 
1247 	offset = IUCV_SKB_CB(skb)->offset;
1248 	rlen   = skb->len - offset;		/* real length of skb */
1249 	copied = min_t(unsigned int, rlen, len);
1250 	if (!rlen)
1251 		sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
1252 
1253 	cskb = skb;
1254 	if (skb_copy_datagram_msg(cskb, offset, msg, copied)) {
1255 		if (!(flags & MSG_PEEK))
1256 			skb_queue_head(&sk->sk_receive_queue, skb);
1257 		return -EFAULT;
1258 	}
1259 
1260 	/* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1261 	if (sk->sk_type == SOCK_SEQPACKET) {
1262 		if (copied < rlen)
1263 			msg->msg_flags |= MSG_TRUNC;
1264 		/* each iucv message contains a complete record */
1265 		msg->msg_flags |= MSG_EOR;
1266 	}
1267 
1268 	/* create control message to store iucv msg target class:
1269 	 * get the trgcls from the control buffer of the skb due to
1270 	 * fragmentation of original iucv message. */
1271 	err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1272 		       sizeof(IUCV_SKB_CB(skb)->class),
1273 		       (void *)&IUCV_SKB_CB(skb)->class);
1274 	if (err) {
1275 		if (!(flags & MSG_PEEK))
1276 			skb_queue_head(&sk->sk_receive_queue, skb);
1277 		return err;
1278 	}
1279 
1280 	/* Mark read part of skb as used */
1281 	if (!(flags & MSG_PEEK)) {
1282 
1283 		/* SOCK_STREAM: re-queue skb if it contains unreceived data */
1284 		if (sk->sk_type == SOCK_STREAM) {
1285 			if (copied < rlen) {
1286 				IUCV_SKB_CB(skb)->offset = offset + copied;
1287 				skb_queue_head(&sk->sk_receive_queue, skb);
1288 				goto done;
1289 			}
1290 		}
1291 
1292 		consume_skb(skb);
1293 		if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1294 			atomic_inc(&iucv->msg_recv);
1295 			if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
1296 				WARN_ON(1);
1297 				iucv_sock_close(sk);
1298 				return -EFAULT;
1299 			}
1300 		}
1301 
1302 		/* Queue backlog skbs */
1303 		spin_lock_bh(&iucv->message_q.lock);
1304 		rskb = skb_dequeue(&iucv->backlog_skb_q);
1305 		while (rskb) {
1306 			IUCV_SKB_CB(rskb)->offset = 0;
1307 			if (__sock_queue_rcv_skb(sk, rskb)) {
1308 				/* handle rcv queue full */
1309 				skb_queue_head(&iucv->backlog_skb_q,
1310 						rskb);
1311 				break;
1312 			}
1313 			rskb = skb_dequeue(&iucv->backlog_skb_q);
1314 		}
1315 		if (skb_queue_empty(&iucv->backlog_skb_q)) {
1316 			if (!list_empty(&iucv->message_q.list))
1317 				iucv_process_message_q(sk);
1318 			if (atomic_read(&iucv->msg_recv) >=
1319 							iucv->msglimit / 2) {
1320 				err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
1321 				if (err) {
1322 					sk->sk_state = IUCV_DISCONN;
1323 					sk->sk_state_change(sk);
1324 				}
1325 			}
1326 		}
1327 		spin_unlock_bh(&iucv->message_q.lock);
1328 	}
1329 
1330 done:
1331 	/* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1332 	if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1333 		copied = rlen;
1334 
1335 	return copied;
1336 }
1337 
1338 static inline __poll_t iucv_accept_poll(struct sock *parent)
1339 {
1340 	struct iucv_sock *isk, *n;
1341 	struct sock *sk;
1342 
1343 	list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
1344 		sk = (struct sock *) isk;
1345 
1346 		if (sk->sk_state == IUCV_CONNECTED)
1347 			return EPOLLIN | EPOLLRDNORM;
1348 	}
1349 
1350 	return 0;
1351 }
1352 
1353 static __poll_t iucv_sock_poll(struct file *file, struct socket *sock,
1354 			       poll_table *wait)
1355 {
1356 	struct sock *sk = sock->sk;
1357 	__poll_t mask = 0;
1358 
1359 	sock_poll_wait(file, sock, wait);
1360 
1361 	if (sk->sk_state == IUCV_LISTEN)
1362 		return iucv_accept_poll(sk);
1363 
1364 	if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1365 		mask |= EPOLLERR |
1366 			(sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
1367 
1368 	if (sk->sk_shutdown & RCV_SHUTDOWN)
1369 		mask |= EPOLLRDHUP;
1370 
1371 	if (sk->sk_shutdown == SHUTDOWN_MASK)
1372 		mask |= EPOLLHUP;
1373 
1374 	if (!skb_queue_empty(&sk->sk_receive_queue) ||
1375 	    (sk->sk_shutdown & RCV_SHUTDOWN))
1376 		mask |= EPOLLIN | EPOLLRDNORM;
1377 
1378 	if (sk->sk_state == IUCV_CLOSED)
1379 		mask |= EPOLLHUP;
1380 
1381 	if (sk->sk_state == IUCV_DISCONN)
1382 		mask |= EPOLLIN;
1383 
1384 	if (sock_writeable(sk) && iucv_below_msglim(sk))
1385 		mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
1386 	else
1387 		sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1388 
1389 	return mask;
1390 }
1391 
1392 static int iucv_sock_shutdown(struct socket *sock, int how)
1393 {
1394 	struct sock *sk = sock->sk;
1395 	struct iucv_sock *iucv = iucv_sk(sk);
1396 	struct iucv_message txmsg;
1397 	int err = 0;
1398 
1399 	how++;
1400 
1401 	if ((how & ~SHUTDOWN_MASK) || !how)
1402 		return -EINVAL;
1403 
1404 	lock_sock(sk);
1405 	switch (sk->sk_state) {
1406 	case IUCV_LISTEN:
1407 	case IUCV_DISCONN:
1408 	case IUCV_CLOSING:
1409 	case IUCV_CLOSED:
1410 		err = -ENOTCONN;
1411 		goto fail;
1412 	default:
1413 		break;
1414 	}
1415 
1416 	if ((how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) &&
1417 	    sk->sk_state == IUCV_CONNECTED) {
1418 		if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1419 			txmsg.class = 0;
1420 			txmsg.tag = 0;
1421 			err = pr_iucv->message_send(iucv->path, &txmsg,
1422 				IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8);
1423 			if (err) {
1424 				switch (err) {
1425 				case 1:
1426 					err = -ENOTCONN;
1427 					break;
1428 				case 2:
1429 					err = -ECONNRESET;
1430 					break;
1431 				default:
1432 					err = -ENOTCONN;
1433 					break;
1434 				}
1435 			}
1436 		} else
1437 			iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
1438 	}
1439 
1440 	sk->sk_shutdown |= how;
1441 	if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
1442 		if ((iucv->transport == AF_IUCV_TRANS_IUCV) &&
1443 		    iucv->path) {
1444 			err = pr_iucv->path_quiesce(iucv->path, NULL);
1445 			if (err)
1446 				err = -ENOTCONN;
1447 /*			skb_queue_purge(&sk->sk_receive_queue); */
1448 		}
1449 		skb_queue_purge(&sk->sk_receive_queue);
1450 	}
1451 
1452 	/* Wake up anyone sleeping in poll */
1453 	sk->sk_state_change(sk);
1454 
1455 fail:
1456 	release_sock(sk);
1457 	return err;
1458 }
1459 
1460 static int iucv_sock_release(struct socket *sock)
1461 {
1462 	struct sock *sk = sock->sk;
1463 	int err = 0;
1464 
1465 	if (!sk)
1466 		return 0;
1467 
1468 	iucv_sock_close(sk);
1469 
1470 	sock_orphan(sk);
1471 	iucv_sock_kill(sk);
1472 	return err;
1473 }
1474 
1475 /* getsockopt and setsockopt */
1476 static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
1477 				sockptr_t optval, unsigned int optlen)
1478 {
1479 	struct sock *sk = sock->sk;
1480 	struct iucv_sock *iucv = iucv_sk(sk);
1481 	int val;
1482 	int rc;
1483 
1484 	if (level != SOL_IUCV)
1485 		return -ENOPROTOOPT;
1486 
1487 	if (optlen < sizeof(int))
1488 		return -EINVAL;
1489 
1490 	if (copy_from_sockptr(&val, optval, sizeof(int)))
1491 		return -EFAULT;
1492 
1493 	rc = 0;
1494 
1495 	lock_sock(sk);
1496 	switch (optname) {
1497 	case SO_IPRMDATA_MSG:
1498 		if (val)
1499 			iucv->flags |= IUCV_IPRMDATA;
1500 		else
1501 			iucv->flags &= ~IUCV_IPRMDATA;
1502 		break;
1503 	case SO_MSGLIMIT:
1504 		switch (sk->sk_state) {
1505 		case IUCV_OPEN:
1506 		case IUCV_BOUND:
1507 			if (val < 1 || val > U16_MAX)
1508 				rc = -EINVAL;
1509 			else
1510 				iucv->msglimit = val;
1511 			break;
1512 		default:
1513 			rc = -EINVAL;
1514 			break;
1515 		}
1516 		break;
1517 	default:
1518 		rc = -ENOPROTOOPT;
1519 		break;
1520 	}
1521 	release_sock(sk);
1522 
1523 	return rc;
1524 }
1525 
1526 static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1527 				char __user *optval, int __user *optlen)
1528 {
1529 	struct sock *sk = sock->sk;
1530 	struct iucv_sock *iucv = iucv_sk(sk);
1531 	unsigned int val;
1532 	int len;
1533 
1534 	if (level != SOL_IUCV)
1535 		return -ENOPROTOOPT;
1536 
1537 	if (get_user(len, optlen))
1538 		return -EFAULT;
1539 
1540 	if (len < 0)
1541 		return -EINVAL;
1542 
1543 	len = min_t(unsigned int, len, sizeof(int));
1544 
1545 	switch (optname) {
1546 	case SO_IPRMDATA_MSG:
1547 		val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1548 		break;
1549 	case SO_MSGLIMIT:
1550 		lock_sock(sk);
1551 		val = (iucv->path != NULL) ? iucv->path->msglim	/* connected */
1552 					   : iucv->msglimit;	/* default */
1553 		release_sock(sk);
1554 		break;
1555 	case SO_MSGSIZE:
1556 		if (sk->sk_state == IUCV_OPEN)
1557 			return -EBADFD;
1558 		val = (iucv->hs_dev) ? iucv->hs_dev->mtu -
1559 				sizeof(struct af_iucv_trans_hdr) - ETH_HLEN :
1560 				0x7fffffff;
1561 		break;
1562 	default:
1563 		return -ENOPROTOOPT;
1564 	}
1565 
1566 	if (put_user(len, optlen))
1567 		return -EFAULT;
1568 	if (copy_to_user(optval, &val, len))
1569 		return -EFAULT;
1570 
1571 	return 0;
1572 }
1573 
1574 
1575 /* Callback wrappers - called from iucv base support */
1576 static int iucv_callback_connreq(struct iucv_path *path,
1577 				 u8 ipvmid[8], u8 ipuser[16])
1578 {
1579 	unsigned char user_data[16];
1580 	unsigned char nuser_data[16];
1581 	unsigned char src_name[8];
1582 	struct sock *sk, *nsk;
1583 	struct iucv_sock *iucv, *niucv;
1584 	int err;
1585 
1586 	memcpy(src_name, ipuser, 8);
1587 	EBCASC(src_name, 8);
1588 	/* Find out if this path belongs to af_iucv. */
1589 	read_lock(&iucv_sk_list.lock);
1590 	iucv = NULL;
1591 	sk = NULL;
1592 	sk_for_each(sk, &iucv_sk_list.head)
1593 		if (sk->sk_state == IUCV_LISTEN &&
1594 		    !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1595 			/*
1596 			 * Found a listening socket with
1597 			 * src_name == ipuser[0-7].
1598 			 */
1599 			iucv = iucv_sk(sk);
1600 			break;
1601 		}
1602 	read_unlock(&iucv_sk_list.lock);
1603 	if (!iucv)
1604 		/* No socket found, not one of our paths. */
1605 		return -EINVAL;
1606 
1607 	bh_lock_sock(sk);
1608 
1609 	/* Check if parent socket is listening */
1610 	low_nmcpy(user_data, iucv->src_name);
1611 	high_nmcpy(user_data, iucv->dst_name);
1612 	ASCEBC(user_data, sizeof(user_data));
1613 	if (sk->sk_state != IUCV_LISTEN) {
1614 		err = pr_iucv->path_sever(path, user_data);
1615 		iucv_path_free(path);
1616 		goto fail;
1617 	}
1618 
1619 	/* Check for backlog size */
1620 	if (sk_acceptq_is_full(sk)) {
1621 		err = pr_iucv->path_sever(path, user_data);
1622 		iucv_path_free(path);
1623 		goto fail;
1624 	}
1625 
1626 	/* Create the new socket */
1627 	nsk = iucv_sock_alloc(NULL, sk->sk_protocol, GFP_ATOMIC, 0);
1628 	if (!nsk) {
1629 		err = pr_iucv->path_sever(path, user_data);
1630 		iucv_path_free(path);
1631 		goto fail;
1632 	}
1633 
1634 	niucv = iucv_sk(nsk);
1635 	iucv_sock_init(nsk, sk);
1636 	niucv->transport = AF_IUCV_TRANS_IUCV;
1637 	nsk->sk_allocation |= GFP_DMA;
1638 
1639 	/* Set the new iucv_sock */
1640 	memcpy(niucv->dst_name, ipuser + 8, 8);
1641 	EBCASC(niucv->dst_name, 8);
1642 	memcpy(niucv->dst_user_id, ipvmid, 8);
1643 	memcpy(niucv->src_name, iucv->src_name, 8);
1644 	memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1645 	niucv->path = path;
1646 
1647 	/* Call iucv_accept */
1648 	high_nmcpy(nuser_data, ipuser + 8);
1649 	memcpy(nuser_data + 8, niucv->src_name, 8);
1650 	ASCEBC(nuser_data + 8, 8);
1651 
1652 	/* set message limit for path based on msglimit of accepting socket */
1653 	niucv->msglimit = iucv->msglimit;
1654 	path->msglim = iucv->msglimit;
1655 	err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
1656 	if (err) {
1657 		iucv_sever_path(nsk, 1);
1658 		iucv_sock_kill(nsk);
1659 		goto fail;
1660 	}
1661 
1662 	iucv_accept_enqueue(sk, nsk);
1663 
1664 	/* Wake up accept */
1665 	nsk->sk_state = IUCV_CONNECTED;
1666 	sk->sk_data_ready(sk);
1667 	err = 0;
1668 fail:
1669 	bh_unlock_sock(sk);
1670 	return 0;
1671 }
1672 
1673 static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1674 {
1675 	struct sock *sk = path->private;
1676 
1677 	sk->sk_state = IUCV_CONNECTED;
1678 	sk->sk_state_change(sk);
1679 }
1680 
1681 static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1682 {
1683 	struct sock *sk = path->private;
1684 	struct iucv_sock *iucv = iucv_sk(sk);
1685 	struct sk_buff *skb;
1686 	struct sock_msg_q *save_msg;
1687 	int len;
1688 
1689 	if (sk->sk_shutdown & RCV_SHUTDOWN) {
1690 		pr_iucv->message_reject(path, msg);
1691 		return;
1692 	}
1693 
1694 	spin_lock(&iucv->message_q.lock);
1695 
1696 	if (!list_empty(&iucv->message_q.list) ||
1697 	    !skb_queue_empty(&iucv->backlog_skb_q))
1698 		goto save_message;
1699 
1700 	len = atomic_read(&sk->sk_rmem_alloc);
1701 	len += SKB_TRUESIZE(iucv_msg_length(msg));
1702 	if (len > sk->sk_rcvbuf)
1703 		goto save_message;
1704 
1705 	skb = alloc_iucv_recv_skb(iucv_msg_length(msg));
1706 	if (!skb)
1707 		goto save_message;
1708 
1709 	iucv_process_message(sk, skb, path, msg);
1710 	goto out_unlock;
1711 
1712 save_message:
1713 	save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
1714 	if (!save_msg)
1715 		goto out_unlock;
1716 	save_msg->path = path;
1717 	save_msg->msg = *msg;
1718 
1719 	list_add_tail(&save_msg->list, &iucv->message_q.list);
1720 
1721 out_unlock:
1722 	spin_unlock(&iucv->message_q.lock);
1723 }
1724 
1725 static void iucv_callback_txdone(struct iucv_path *path,
1726 				 struct iucv_message *msg)
1727 {
1728 	struct sock *sk = path->private;
1729 	struct sk_buff *this = NULL;
1730 	struct sk_buff_head *list;
1731 	struct sk_buff *list_skb;
1732 	struct iucv_sock *iucv;
1733 	unsigned long flags;
1734 
1735 	iucv = iucv_sk(sk);
1736 	list = &iucv->send_skb_q;
1737 
1738 	bh_lock_sock(sk);
1739 
1740 	spin_lock_irqsave(&list->lock, flags);
1741 	skb_queue_walk(list, list_skb) {
1742 		if (msg->tag == IUCV_SKB_CB(list_skb)->tag) {
1743 			this = list_skb;
1744 			break;
1745 		}
1746 	}
1747 	if (this) {
1748 		atomic_dec(&iucv->skbs_in_xmit);
1749 		__skb_unlink(this, list);
1750 	}
1751 
1752 	spin_unlock_irqrestore(&list->lock, flags);
1753 
1754 	if (this) {
1755 		consume_skb(this);
1756 		/* wake up any process waiting for sending */
1757 		iucv_sock_wake_msglim(sk);
1758 	}
1759 
1760 	if (sk->sk_state == IUCV_CLOSING) {
1761 		if (atomic_read(&iucv->skbs_in_xmit) == 0) {
1762 			sk->sk_state = IUCV_CLOSED;
1763 			sk->sk_state_change(sk);
1764 		}
1765 	}
1766 	bh_unlock_sock(sk);
1767 
1768 }
1769 
1770 static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1771 {
1772 	struct sock *sk = path->private;
1773 
1774 	if (sk->sk_state == IUCV_CLOSED)
1775 		return;
1776 
1777 	bh_lock_sock(sk);
1778 	iucv_sever_path(sk, 1);
1779 	sk->sk_state = IUCV_DISCONN;
1780 
1781 	sk->sk_state_change(sk);
1782 	bh_unlock_sock(sk);
1783 }
1784 
1785 /* called if the other communication side shuts down its RECV direction;
1786  * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1787  */
1788 static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1789 {
1790 	struct sock *sk = path->private;
1791 
1792 	bh_lock_sock(sk);
1793 	if (sk->sk_state != IUCV_CLOSED) {
1794 		sk->sk_shutdown |= SEND_SHUTDOWN;
1795 		sk->sk_state_change(sk);
1796 	}
1797 	bh_unlock_sock(sk);
1798 }
1799 
1800 static struct iucv_handler af_iucv_handler = {
1801 	.path_pending		= iucv_callback_connreq,
1802 	.path_complete		= iucv_callback_connack,
1803 	.path_severed		= iucv_callback_connrej,
1804 	.message_pending	= iucv_callback_rx,
1805 	.message_complete	= iucv_callback_txdone,
1806 	.path_quiesced		= iucv_callback_shutdown,
1807 };
1808 
1809 /***************** HiperSockets transport callbacks ********************/
1810 static void afiucv_swap_src_dest(struct sk_buff *skb)
1811 {
1812 	struct af_iucv_trans_hdr *trans_hdr = iucv_trans_hdr(skb);
1813 	char tmpID[8];
1814 	char tmpName[8];
1815 
1816 	ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1817 	ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1818 	ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1819 	ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1820 	memcpy(tmpID, trans_hdr->srcUserID, 8);
1821 	memcpy(tmpName, trans_hdr->srcAppName, 8);
1822 	memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1823 	memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1824 	memcpy(trans_hdr->destUserID, tmpID, 8);
1825 	memcpy(trans_hdr->destAppName, tmpName, 8);
1826 	skb_push(skb, ETH_HLEN);
1827 	memset(skb->data, 0, ETH_HLEN);
1828 }
1829 
1830 /*
1831  * afiucv_hs_callback_syn - react on received SYN
1832  */
1833 static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1834 {
1835 	struct af_iucv_trans_hdr *trans_hdr = iucv_trans_hdr(skb);
1836 	struct sock *nsk;
1837 	struct iucv_sock *iucv, *niucv;
1838 	int err;
1839 
1840 	iucv = iucv_sk(sk);
1841 	if (!iucv) {
1842 		/* no sock - connection refused */
1843 		afiucv_swap_src_dest(skb);
1844 		trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1845 		err = dev_queue_xmit(skb);
1846 		goto out;
1847 	}
1848 
1849 	nsk = iucv_sock_alloc(NULL, sk->sk_protocol, GFP_ATOMIC, 0);
1850 	bh_lock_sock(sk);
1851 	if ((sk->sk_state != IUCV_LISTEN) ||
1852 	    sk_acceptq_is_full(sk) ||
1853 	    !nsk) {
1854 		/* error on server socket - connection refused */
1855 		afiucv_swap_src_dest(skb);
1856 		trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1857 		err = dev_queue_xmit(skb);
1858 		iucv_sock_kill(nsk);
1859 		bh_unlock_sock(sk);
1860 		goto out;
1861 	}
1862 
1863 	niucv = iucv_sk(nsk);
1864 	iucv_sock_init(nsk, sk);
1865 	niucv->transport = AF_IUCV_TRANS_HIPER;
1866 	niucv->msglimit = iucv->msglimit;
1867 	if (!trans_hdr->window)
1868 		niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
1869 	else
1870 		niucv->msglimit_peer = trans_hdr->window;
1871 	memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
1872 	memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
1873 	memcpy(niucv->src_name, iucv->src_name, 8);
1874 	memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1875 	nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
1876 	niucv->hs_dev = iucv->hs_dev;
1877 	dev_hold(niucv->hs_dev);
1878 	afiucv_swap_src_dest(skb);
1879 	trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
1880 	trans_hdr->window = niucv->msglimit;
1881 	/* if receiver acks the xmit connection is established */
1882 	err = dev_queue_xmit(skb);
1883 	if (!err) {
1884 		iucv_accept_enqueue(sk, nsk);
1885 		nsk->sk_state = IUCV_CONNECTED;
1886 		sk->sk_data_ready(sk);
1887 	} else
1888 		iucv_sock_kill(nsk);
1889 	bh_unlock_sock(sk);
1890 
1891 out:
1892 	return NET_RX_SUCCESS;
1893 }
1894 
1895 /*
1896  * afiucv_hs_callback_synack() - react on received SYN-ACK
1897  */
1898 static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
1899 {
1900 	struct iucv_sock *iucv = iucv_sk(sk);
1901 
1902 	if (!iucv || sk->sk_state != IUCV_BOUND) {
1903 		kfree_skb(skb);
1904 		return NET_RX_SUCCESS;
1905 	}
1906 
1907 	bh_lock_sock(sk);
1908 	iucv->msglimit_peer = iucv_trans_hdr(skb)->window;
1909 	sk->sk_state = IUCV_CONNECTED;
1910 	sk->sk_state_change(sk);
1911 	bh_unlock_sock(sk);
1912 	consume_skb(skb);
1913 	return NET_RX_SUCCESS;
1914 }
1915 
1916 /*
1917  * afiucv_hs_callback_synfin() - react on received SYN_FIN
1918  */
1919 static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
1920 {
1921 	struct iucv_sock *iucv = iucv_sk(sk);
1922 
1923 	if (!iucv || sk->sk_state != IUCV_BOUND) {
1924 		kfree_skb(skb);
1925 		return NET_RX_SUCCESS;
1926 	}
1927 
1928 	bh_lock_sock(sk);
1929 	sk->sk_state = IUCV_DISCONN;
1930 	sk->sk_state_change(sk);
1931 	bh_unlock_sock(sk);
1932 	consume_skb(skb);
1933 	return NET_RX_SUCCESS;
1934 }
1935 
1936 /*
1937  * afiucv_hs_callback_fin() - react on received FIN
1938  */
1939 static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
1940 {
1941 	struct iucv_sock *iucv = iucv_sk(sk);
1942 
1943 	/* other end of connection closed */
1944 	if (!iucv) {
1945 		kfree_skb(skb);
1946 		return NET_RX_SUCCESS;
1947 	}
1948 
1949 	bh_lock_sock(sk);
1950 	if (sk->sk_state == IUCV_CONNECTED) {
1951 		sk->sk_state = IUCV_DISCONN;
1952 		sk->sk_state_change(sk);
1953 	}
1954 	bh_unlock_sock(sk);
1955 	consume_skb(skb);
1956 	return NET_RX_SUCCESS;
1957 }
1958 
1959 /*
1960  * afiucv_hs_callback_win() - react on received WIN
1961  */
1962 static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
1963 {
1964 	struct iucv_sock *iucv = iucv_sk(sk);
1965 
1966 	if (!iucv)
1967 		return NET_RX_SUCCESS;
1968 
1969 	if (sk->sk_state != IUCV_CONNECTED)
1970 		return NET_RX_SUCCESS;
1971 
1972 	atomic_sub(iucv_trans_hdr(skb)->window, &iucv->msg_sent);
1973 	iucv_sock_wake_msglim(sk);
1974 	return NET_RX_SUCCESS;
1975 }
1976 
1977 /*
1978  * afiucv_hs_callback_rx() - react on received data
1979  */
1980 static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
1981 {
1982 	struct iucv_sock *iucv = iucv_sk(sk);
1983 
1984 	if (!iucv) {
1985 		kfree_skb(skb);
1986 		return NET_RX_SUCCESS;
1987 	}
1988 
1989 	if (sk->sk_state != IUCV_CONNECTED) {
1990 		kfree_skb(skb);
1991 		return NET_RX_SUCCESS;
1992 	}
1993 
1994 	if (sk->sk_shutdown & RCV_SHUTDOWN) {
1995 		kfree_skb(skb);
1996 		return NET_RX_SUCCESS;
1997 	}
1998 
1999 	/* write stuff from iucv_msg to skb cb */
2000 	skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2001 	skb_reset_transport_header(skb);
2002 	skb_reset_network_header(skb);
2003 	IUCV_SKB_CB(skb)->offset = 0;
2004 	if (sk_filter(sk, skb)) {
2005 		atomic_inc(&sk->sk_drops);	/* skb rejected by filter */
2006 		kfree_skb(skb);
2007 		return NET_RX_SUCCESS;
2008 	}
2009 
2010 	spin_lock(&iucv->message_q.lock);
2011 	if (skb_queue_empty(&iucv->backlog_skb_q)) {
2012 		if (__sock_queue_rcv_skb(sk, skb))
2013 			/* handle rcv queue full */
2014 			skb_queue_tail(&iucv->backlog_skb_q, skb);
2015 	} else
2016 		skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2017 	spin_unlock(&iucv->message_q.lock);
2018 	return NET_RX_SUCCESS;
2019 }
2020 
2021 /*
2022  * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2023  *                   transport
2024  *                   called from netif RX softirq
2025  */
2026 static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2027 	struct packet_type *pt, struct net_device *orig_dev)
2028 {
2029 	struct sock *sk;
2030 	struct iucv_sock *iucv;
2031 	struct af_iucv_trans_hdr *trans_hdr;
2032 	int err = NET_RX_SUCCESS;
2033 	char nullstring[8];
2034 
2035 	if (!pskb_may_pull(skb, sizeof(*trans_hdr))) {
2036 		kfree_skb(skb);
2037 		return NET_RX_SUCCESS;
2038 	}
2039 
2040 	trans_hdr = iucv_trans_hdr(skb);
2041 	EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2042 	EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2043 	EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2044 	EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2045 	memset(nullstring, 0, sizeof(nullstring));
2046 	iucv = NULL;
2047 	sk = NULL;
2048 	read_lock(&iucv_sk_list.lock);
2049 	sk_for_each(sk, &iucv_sk_list.head) {
2050 		if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2051 			if ((!memcmp(&iucv_sk(sk)->src_name,
2052 				     trans_hdr->destAppName, 8)) &&
2053 			    (!memcmp(&iucv_sk(sk)->src_user_id,
2054 				     trans_hdr->destUserID, 8)) &&
2055 			    (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2056 			    (!memcmp(&iucv_sk(sk)->dst_user_id,
2057 				     nullstring, 8))) {
2058 				iucv = iucv_sk(sk);
2059 				break;
2060 			}
2061 		} else {
2062 			if ((!memcmp(&iucv_sk(sk)->src_name,
2063 				     trans_hdr->destAppName, 8)) &&
2064 			    (!memcmp(&iucv_sk(sk)->src_user_id,
2065 				     trans_hdr->destUserID, 8)) &&
2066 			    (!memcmp(&iucv_sk(sk)->dst_name,
2067 				     trans_hdr->srcAppName, 8)) &&
2068 			    (!memcmp(&iucv_sk(sk)->dst_user_id,
2069 				     trans_hdr->srcUserID, 8))) {
2070 				iucv = iucv_sk(sk);
2071 				break;
2072 			}
2073 		}
2074 	}
2075 	read_unlock(&iucv_sk_list.lock);
2076 	if (!iucv)
2077 		sk = NULL;
2078 
2079 	/* no sock
2080 	how should we send with no sock
2081 	1) send without sock no send rc checking?
2082 	2) introduce default sock to handle this cases
2083 
2084 	 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2085 	 data -> send FIN
2086 	 SYN|ACK, SYN|FIN, FIN -> no action? */
2087 
2088 	switch (trans_hdr->flags) {
2089 	case AF_IUCV_FLAG_SYN:
2090 		/* connect request */
2091 		err = afiucv_hs_callback_syn(sk, skb);
2092 		break;
2093 	case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2094 		/* connect request confirmed */
2095 		err = afiucv_hs_callback_synack(sk, skb);
2096 		break;
2097 	case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2098 		/* connect request refused */
2099 		err = afiucv_hs_callback_synfin(sk, skb);
2100 		break;
2101 	case (AF_IUCV_FLAG_FIN):
2102 		/* close request */
2103 		err = afiucv_hs_callback_fin(sk, skb);
2104 		break;
2105 	case (AF_IUCV_FLAG_WIN):
2106 		err = afiucv_hs_callback_win(sk, skb);
2107 		if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
2108 			consume_skb(skb);
2109 			break;
2110 		}
2111 		fallthrough;	/* and receive non-zero length data */
2112 	case (AF_IUCV_FLAG_SHT):
2113 		/* shutdown request */
2114 		fallthrough;	/* and receive zero length data */
2115 	case 0:
2116 		/* plain data frame */
2117 		IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
2118 		err = afiucv_hs_callback_rx(sk, skb);
2119 		break;
2120 	default:
2121 		kfree_skb(skb);
2122 	}
2123 
2124 	return err;
2125 }
2126 
2127 /*
2128  * afiucv_hs_callback_txnotify() - handle send notifications from HiperSockets
2129  *                                 transport
2130  */
2131 static void afiucv_hs_callback_txnotify(struct sock *sk, enum iucv_tx_notify n)
2132 {
2133 	struct iucv_sock *iucv = iucv_sk(sk);
2134 
2135 	if (sock_flag(sk, SOCK_ZAPPED))
2136 		return;
2137 
2138 	switch (n) {
2139 	case TX_NOTIFY_OK:
2140 		atomic_dec(&iucv->skbs_in_xmit);
2141 		iucv_sock_wake_msglim(sk);
2142 		break;
2143 	case TX_NOTIFY_PENDING:
2144 		atomic_inc(&iucv->pendings);
2145 		break;
2146 	case TX_NOTIFY_DELAYED_OK:
2147 		atomic_dec(&iucv->skbs_in_xmit);
2148 		if (atomic_dec_return(&iucv->pendings) <= 0)
2149 			iucv_sock_wake_msglim(sk);
2150 		break;
2151 	default:
2152 		atomic_dec(&iucv->skbs_in_xmit);
2153 		if (sk->sk_state == IUCV_CONNECTED) {
2154 			sk->sk_state = IUCV_DISCONN;
2155 			sk->sk_state_change(sk);
2156 		}
2157 	}
2158 
2159 	if (sk->sk_state == IUCV_CLOSING) {
2160 		if (atomic_read(&iucv->skbs_in_xmit) == 0) {
2161 			sk->sk_state = IUCV_CLOSED;
2162 			sk->sk_state_change(sk);
2163 		}
2164 	}
2165 }
2166 
2167 /*
2168  * afiucv_netdev_event: handle netdev notifier chain events
2169  */
2170 static int afiucv_netdev_event(struct notifier_block *this,
2171 			       unsigned long event, void *ptr)
2172 {
2173 	struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2174 	struct sock *sk;
2175 	struct iucv_sock *iucv;
2176 
2177 	switch (event) {
2178 	case NETDEV_REBOOT:
2179 	case NETDEV_GOING_DOWN:
2180 		sk_for_each(sk, &iucv_sk_list.head) {
2181 			iucv = iucv_sk(sk);
2182 			if ((iucv->hs_dev == event_dev) &&
2183 			    (sk->sk_state == IUCV_CONNECTED)) {
2184 				if (event == NETDEV_GOING_DOWN)
2185 					iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
2186 				sk->sk_state = IUCV_DISCONN;
2187 				sk->sk_state_change(sk);
2188 			}
2189 		}
2190 		break;
2191 	case NETDEV_DOWN:
2192 	case NETDEV_UNREGISTER:
2193 	default:
2194 		break;
2195 	}
2196 	return NOTIFY_DONE;
2197 }
2198 
2199 static struct notifier_block afiucv_netdev_notifier = {
2200 	.notifier_call = afiucv_netdev_event,
2201 };
2202 
2203 static const struct proto_ops iucv_sock_ops = {
2204 	.family		= PF_IUCV,
2205 	.owner		= THIS_MODULE,
2206 	.release	= iucv_sock_release,
2207 	.bind		= iucv_sock_bind,
2208 	.connect	= iucv_sock_connect,
2209 	.listen		= iucv_sock_listen,
2210 	.accept		= iucv_sock_accept,
2211 	.getname	= iucv_sock_getname,
2212 	.sendmsg	= iucv_sock_sendmsg,
2213 	.recvmsg	= iucv_sock_recvmsg,
2214 	.poll		= iucv_sock_poll,
2215 	.ioctl		= sock_no_ioctl,
2216 	.mmap		= sock_no_mmap,
2217 	.socketpair	= sock_no_socketpair,
2218 	.shutdown	= iucv_sock_shutdown,
2219 	.setsockopt	= iucv_sock_setsockopt,
2220 	.getsockopt	= iucv_sock_getsockopt,
2221 };
2222 
2223 static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
2224 			    int kern)
2225 {
2226 	struct sock *sk;
2227 
2228 	if (protocol && protocol != PF_IUCV)
2229 		return -EPROTONOSUPPORT;
2230 
2231 	sock->state = SS_UNCONNECTED;
2232 
2233 	switch (sock->type) {
2234 	case SOCK_STREAM:
2235 	case SOCK_SEQPACKET:
2236 		/* currently, proto ops can handle both sk types */
2237 		sock->ops = &iucv_sock_ops;
2238 		break;
2239 	default:
2240 		return -ESOCKTNOSUPPORT;
2241 	}
2242 
2243 	sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL, kern);
2244 	if (!sk)
2245 		return -ENOMEM;
2246 
2247 	iucv_sock_init(sk, NULL);
2248 
2249 	return 0;
2250 }
2251 
2252 static const struct net_proto_family iucv_sock_family_ops = {
2253 	.family	= AF_IUCV,
2254 	.owner	= THIS_MODULE,
2255 	.create	= iucv_sock_create,
2256 };
2257 
2258 static struct packet_type iucv_packet_type = {
2259 	.type = cpu_to_be16(ETH_P_AF_IUCV),
2260 	.func = afiucv_hs_rcv,
2261 };
2262 
2263 static int __init afiucv_init(void)
2264 {
2265 	int err;
2266 
2267 	if (MACHINE_IS_VM && IS_ENABLED(CONFIG_IUCV)) {
2268 		cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2269 		if (unlikely(err)) {
2270 			WARN_ON(err);
2271 			err = -EPROTONOSUPPORT;
2272 			goto out;
2273 		}
2274 
2275 		pr_iucv = &iucv_if;
2276 	} else {
2277 		memset(&iucv_userid, 0, sizeof(iucv_userid));
2278 		pr_iucv = NULL;
2279 	}
2280 
2281 	err = proto_register(&iucv_proto, 0);
2282 	if (err)
2283 		goto out;
2284 	err = sock_register(&iucv_sock_family_ops);
2285 	if (err)
2286 		goto out_proto;
2287 
2288 	if (pr_iucv) {
2289 		err = pr_iucv->iucv_register(&af_iucv_handler, 0);
2290 		if (err)
2291 			goto out_sock;
2292 	}
2293 
2294 	err = register_netdevice_notifier(&afiucv_netdev_notifier);
2295 	if (err)
2296 		goto out_notifier;
2297 
2298 	dev_add_pack(&iucv_packet_type);
2299 	return 0;
2300 
2301 out_notifier:
2302 	if (pr_iucv)
2303 		pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2304 out_sock:
2305 	sock_unregister(PF_IUCV);
2306 out_proto:
2307 	proto_unregister(&iucv_proto);
2308 out:
2309 	return err;
2310 }
2311 
2312 static void __exit afiucv_exit(void)
2313 {
2314 	if (pr_iucv)
2315 		pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2316 
2317 	unregister_netdevice_notifier(&afiucv_netdev_notifier);
2318 	dev_remove_pack(&iucv_packet_type);
2319 	sock_unregister(PF_IUCV);
2320 	proto_unregister(&iucv_proto);
2321 }
2322 
2323 module_init(afiucv_init);
2324 module_exit(afiucv_exit);
2325 
2326 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2327 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2328 MODULE_VERSION(VERSION);
2329 MODULE_LICENSE("GPL");
2330 MODULE_ALIAS_NETPROTO(PF_IUCV);
2331