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