xref: /openbmc/linux/net/iucv/af_iucv.c (revision 78289b4a)
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 sk_buff *, 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 (skb_queue_len(&iucv->send_skb_q) < 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 	struct sk_buff *nskb;
215 	int err, confirm_recv = 0;
216 
217 	phs_hdr = skb_push(skb, sizeof(*phs_hdr));
218 	memset(phs_hdr, 0, sizeof(*phs_hdr));
219 	skb_reset_network_header(skb);
220 
221 	phs_hdr->magic = ETH_P_AF_IUCV;
222 	phs_hdr->version = 1;
223 	phs_hdr->flags = flags;
224 	if (flags == AF_IUCV_FLAG_SYN)
225 		phs_hdr->window = iucv->msglimit;
226 	else if ((flags == AF_IUCV_FLAG_WIN) || !flags) {
227 		confirm_recv = atomic_read(&iucv->msg_recv);
228 		phs_hdr->window = confirm_recv;
229 		if (confirm_recv)
230 			phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
231 	}
232 	memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
233 	memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
234 	memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
235 	memcpy(phs_hdr->srcAppName, iucv->src_name, 8);
236 	ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID));
237 	ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName));
238 	ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID));
239 	ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName));
240 	if (imsg)
241 		memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message));
242 
243 	skb->dev = iucv->hs_dev;
244 	if (!skb->dev) {
245 		err = -ENODEV;
246 		goto err_free;
247 	}
248 
249 	dev_hard_header(skb, skb->dev, ETH_P_AF_IUCV, NULL, NULL, skb->len);
250 
251 	if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev)) {
252 		err = -ENETDOWN;
253 		goto err_free;
254 	}
255 	if (skb->len > skb->dev->mtu) {
256 		if (sock->sk_type == SOCK_SEQPACKET) {
257 			err = -EMSGSIZE;
258 			goto err_free;
259 		}
260 		skb_trim(skb, skb->dev->mtu);
261 	}
262 	skb->protocol = cpu_to_be16(ETH_P_AF_IUCV);
263 
264 	__skb_header_release(skb);
265 	nskb = skb_clone(skb, GFP_ATOMIC);
266 	if (!nskb) {
267 		err = -ENOMEM;
268 		goto err_free;
269 	}
270 
271 	skb_queue_tail(&iucv->send_skb_q, nskb);
272 	err = dev_queue_xmit(skb);
273 	if (net_xmit_eval(err)) {
274 		skb_unlink(nskb, &iucv->send_skb_q);
275 		kfree_skb(nskb);
276 	} else {
277 		atomic_sub(confirm_recv, &iucv->msg_recv);
278 		WARN_ON(atomic_read(&iucv->msg_recv) < 0);
279 	}
280 	return net_xmit_eval(err);
281 
282 err_free:
283 	kfree_skb(skb);
284 	return err;
285 }
286 
287 static struct sock *__iucv_get_sock_by_name(char *nm)
288 {
289 	struct sock *sk;
290 
291 	sk_for_each(sk, &iucv_sk_list.head)
292 		if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
293 			return sk;
294 
295 	return NULL;
296 }
297 
298 static void iucv_sock_destruct(struct sock *sk)
299 {
300 	skb_queue_purge(&sk->sk_receive_queue);
301 	skb_queue_purge(&sk->sk_error_queue);
302 
303 	sk_mem_reclaim(sk);
304 
305 	if (!sock_flag(sk, SOCK_DEAD)) {
306 		pr_err("Attempt to release alive iucv socket %p\n", sk);
307 		return;
308 	}
309 
310 	WARN_ON(atomic_read(&sk->sk_rmem_alloc));
311 	WARN_ON(refcount_read(&sk->sk_wmem_alloc));
312 	WARN_ON(sk->sk_wmem_queued);
313 	WARN_ON(sk->sk_forward_alloc);
314 }
315 
316 /* Cleanup Listen */
317 static void iucv_sock_cleanup_listen(struct sock *parent)
318 {
319 	struct sock *sk;
320 
321 	/* Close non-accepted connections */
322 	while ((sk = iucv_accept_dequeue(parent, NULL))) {
323 		iucv_sock_close(sk);
324 		iucv_sock_kill(sk);
325 	}
326 
327 	parent->sk_state = IUCV_CLOSED;
328 }
329 
330 static void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
331 {
332 	write_lock_bh(&l->lock);
333 	sk_add_node(sk, &l->head);
334 	write_unlock_bh(&l->lock);
335 }
336 
337 static void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
338 {
339 	write_lock_bh(&l->lock);
340 	sk_del_node_init(sk);
341 	write_unlock_bh(&l->lock);
342 }
343 
344 /* Kill socket (only if zapped and orphaned) */
345 static void iucv_sock_kill(struct sock *sk)
346 {
347 	if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
348 		return;
349 
350 	iucv_sock_unlink(&iucv_sk_list, sk);
351 	sock_set_flag(sk, SOCK_DEAD);
352 	sock_put(sk);
353 }
354 
355 /* Terminate an IUCV path */
356 static void iucv_sever_path(struct sock *sk, int with_user_data)
357 {
358 	unsigned char user_data[16];
359 	struct iucv_sock *iucv = iucv_sk(sk);
360 	struct iucv_path *path = iucv->path;
361 
362 	if (iucv->path) {
363 		iucv->path = NULL;
364 		if (with_user_data) {
365 			low_nmcpy(user_data, iucv->src_name);
366 			high_nmcpy(user_data, iucv->dst_name);
367 			ASCEBC(user_data, sizeof(user_data));
368 			pr_iucv->path_sever(path, user_data);
369 		} else
370 			pr_iucv->path_sever(path, NULL);
371 		iucv_path_free(path);
372 	}
373 }
374 
375 /* Send controlling flags through an IUCV socket for HIPER transport */
376 static int iucv_send_ctrl(struct sock *sk, u8 flags)
377 {
378 	struct iucv_sock *iucv = iucv_sk(sk);
379 	int err = 0;
380 	int blen;
381 	struct sk_buff *skb;
382 	u8 shutdown = 0;
383 
384 	blen = sizeof(struct af_iucv_trans_hdr) +
385 	       LL_RESERVED_SPACE(iucv->hs_dev);
386 	if (sk->sk_shutdown & SEND_SHUTDOWN) {
387 		/* controlling flags should be sent anyway */
388 		shutdown = sk->sk_shutdown;
389 		sk->sk_shutdown &= RCV_SHUTDOWN;
390 	}
391 	skb = sock_alloc_send_skb(sk, blen, 1, &err);
392 	if (skb) {
393 		skb_reserve(skb, blen);
394 		err = afiucv_hs_send(NULL, sk, skb, flags);
395 	}
396 	if (shutdown)
397 		sk->sk_shutdown = shutdown;
398 	return err;
399 }
400 
401 /* Close an IUCV socket */
402 static void iucv_sock_close(struct sock *sk)
403 {
404 	struct iucv_sock *iucv = iucv_sk(sk);
405 	unsigned long timeo;
406 	int err = 0;
407 
408 	lock_sock(sk);
409 
410 	switch (sk->sk_state) {
411 	case IUCV_LISTEN:
412 		iucv_sock_cleanup_listen(sk);
413 		break;
414 
415 	case IUCV_CONNECTED:
416 		if (iucv->transport == AF_IUCV_TRANS_HIPER) {
417 			err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
418 			sk->sk_state = IUCV_DISCONN;
419 			sk->sk_state_change(sk);
420 		}
421 		/* fall through */
422 
423 	case IUCV_DISCONN:
424 		sk->sk_state = IUCV_CLOSING;
425 		sk->sk_state_change(sk);
426 
427 		if (!err && !skb_queue_empty(&iucv->send_skb_q)) {
428 			if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
429 				timeo = sk->sk_lingertime;
430 			else
431 				timeo = IUCV_DISCONN_TIMEOUT;
432 			iucv_sock_wait(sk,
433 					iucv_sock_in_state(sk, IUCV_CLOSED, 0),
434 					timeo);
435 		}
436 		/* fall through */
437 
438 	case IUCV_CLOSING:
439 		sk->sk_state = IUCV_CLOSED;
440 		sk->sk_state_change(sk);
441 
442 		sk->sk_err = ECONNRESET;
443 		sk->sk_state_change(sk);
444 
445 		skb_queue_purge(&iucv->send_skb_q);
446 		skb_queue_purge(&iucv->backlog_skb_q);
447 		/* fall through */
448 
449 	default:
450 		iucv_sever_path(sk, 1);
451 	}
452 
453 	if (iucv->hs_dev) {
454 		dev_put(iucv->hs_dev);
455 		iucv->hs_dev = NULL;
456 		sk->sk_bound_dev_if = 0;
457 	}
458 
459 	/* mark socket for deletion by iucv_sock_kill() */
460 	sock_set_flag(sk, SOCK_ZAPPED);
461 
462 	release_sock(sk);
463 }
464 
465 static void iucv_sock_init(struct sock *sk, struct sock *parent)
466 {
467 	if (parent) {
468 		sk->sk_type = parent->sk_type;
469 		security_sk_clone(parent, sk);
470 	}
471 }
472 
473 static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio, int kern)
474 {
475 	struct sock *sk;
476 	struct iucv_sock *iucv;
477 
478 	sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, kern);
479 	if (!sk)
480 		return NULL;
481 	iucv = iucv_sk(sk);
482 
483 	sock_init_data(sock, sk);
484 	INIT_LIST_HEAD(&iucv->accept_q);
485 	spin_lock_init(&iucv->accept_q_lock);
486 	skb_queue_head_init(&iucv->send_skb_q);
487 	INIT_LIST_HEAD(&iucv->message_q.list);
488 	spin_lock_init(&iucv->message_q.lock);
489 	skb_queue_head_init(&iucv->backlog_skb_q);
490 	iucv->send_tag = 0;
491 	atomic_set(&iucv->pendings, 0);
492 	iucv->flags = 0;
493 	iucv->msglimit = 0;
494 	atomic_set(&iucv->msg_sent, 0);
495 	atomic_set(&iucv->msg_recv, 0);
496 	iucv->path = NULL;
497 	iucv->sk_txnotify = afiucv_hs_callback_txnotify;
498 	memset(&iucv->src_user_id , 0, 32);
499 	if (pr_iucv)
500 		iucv->transport = AF_IUCV_TRANS_IUCV;
501 	else
502 		iucv->transport = AF_IUCV_TRANS_HIPER;
503 
504 	sk->sk_destruct = iucv_sock_destruct;
505 	sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
506 
507 	sock_reset_flag(sk, SOCK_ZAPPED);
508 
509 	sk->sk_protocol = proto;
510 	sk->sk_state	= IUCV_OPEN;
511 
512 	iucv_sock_link(&iucv_sk_list, sk);
513 	return sk;
514 }
515 
516 static void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
517 {
518 	unsigned long flags;
519 	struct iucv_sock *par = iucv_sk(parent);
520 
521 	sock_hold(sk);
522 	spin_lock_irqsave(&par->accept_q_lock, flags);
523 	list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
524 	spin_unlock_irqrestore(&par->accept_q_lock, flags);
525 	iucv_sk(sk)->parent = parent;
526 	sk_acceptq_added(parent);
527 }
528 
529 static void iucv_accept_unlink(struct sock *sk)
530 {
531 	unsigned long flags;
532 	struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
533 
534 	spin_lock_irqsave(&par->accept_q_lock, flags);
535 	list_del_init(&iucv_sk(sk)->accept_q);
536 	spin_unlock_irqrestore(&par->accept_q_lock, flags);
537 	sk_acceptq_removed(iucv_sk(sk)->parent);
538 	iucv_sk(sk)->parent = NULL;
539 	sock_put(sk);
540 }
541 
542 static struct sock *iucv_accept_dequeue(struct sock *parent,
543 					struct socket *newsock)
544 {
545 	struct iucv_sock *isk, *n;
546 	struct sock *sk;
547 
548 	list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
549 		sk = (struct sock *) isk;
550 		lock_sock(sk);
551 
552 		if (sk->sk_state == IUCV_CLOSED) {
553 			iucv_accept_unlink(sk);
554 			release_sock(sk);
555 			continue;
556 		}
557 
558 		if (sk->sk_state == IUCV_CONNECTED ||
559 		    sk->sk_state == IUCV_DISCONN ||
560 		    !newsock) {
561 			iucv_accept_unlink(sk);
562 			if (newsock)
563 				sock_graft(sk, newsock);
564 
565 			release_sock(sk);
566 			return sk;
567 		}
568 
569 		release_sock(sk);
570 	}
571 	return NULL;
572 }
573 
574 static void __iucv_auto_name(struct iucv_sock *iucv)
575 {
576 	char name[12];
577 
578 	sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
579 	while (__iucv_get_sock_by_name(name)) {
580 		sprintf(name, "%08x",
581 			atomic_inc_return(&iucv_sk_list.autobind_name));
582 	}
583 	memcpy(iucv->src_name, name, 8);
584 }
585 
586 /* Bind an unbound socket */
587 static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
588 			  int addr_len)
589 {
590 	struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
591 	struct sock *sk = sock->sk;
592 	struct iucv_sock *iucv;
593 	int err = 0;
594 	struct net_device *dev;
595 	char uid[9];
596 
597 	/* Verify the input sockaddr */
598 	if (addr_len < sizeof(struct sockaddr_iucv) ||
599 	    addr->sa_family != AF_IUCV)
600 		return -EINVAL;
601 
602 	lock_sock(sk);
603 	if (sk->sk_state != IUCV_OPEN) {
604 		err = -EBADFD;
605 		goto done;
606 	}
607 
608 	write_lock_bh(&iucv_sk_list.lock);
609 
610 	iucv = iucv_sk(sk);
611 	if (__iucv_get_sock_by_name(sa->siucv_name)) {
612 		err = -EADDRINUSE;
613 		goto done_unlock;
614 	}
615 	if (iucv->path)
616 		goto done_unlock;
617 
618 	/* Bind the socket */
619 	if (pr_iucv)
620 		if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
621 			goto vm_bind; /* VM IUCV transport */
622 
623 	/* try hiper transport */
624 	memcpy(uid, sa->siucv_user_id, sizeof(uid));
625 	ASCEBC(uid, 8);
626 	rcu_read_lock();
627 	for_each_netdev_rcu(&init_net, dev) {
628 		if (!memcmp(dev->perm_addr, uid, 8)) {
629 			memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
630 			/* Check for unitialized siucv_name */
631 			if (strncmp(sa->siucv_name, "        ", 8) == 0)
632 				__iucv_auto_name(iucv);
633 			else
634 				memcpy(iucv->src_name, sa->siucv_name, 8);
635 			sk->sk_bound_dev_if = dev->ifindex;
636 			iucv->hs_dev = dev;
637 			dev_hold(dev);
638 			sk->sk_state = IUCV_BOUND;
639 			iucv->transport = AF_IUCV_TRANS_HIPER;
640 			if (!iucv->msglimit)
641 				iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
642 			rcu_read_unlock();
643 			goto done_unlock;
644 		}
645 	}
646 	rcu_read_unlock();
647 vm_bind:
648 	if (pr_iucv) {
649 		/* use local userid for backward compat */
650 		memcpy(iucv->src_name, sa->siucv_name, 8);
651 		memcpy(iucv->src_user_id, iucv_userid, 8);
652 		sk->sk_state = IUCV_BOUND;
653 		iucv->transport = AF_IUCV_TRANS_IUCV;
654 		sk->sk_allocation |= GFP_DMA;
655 		if (!iucv->msglimit)
656 			iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
657 		goto done_unlock;
658 	}
659 	/* found no dev to bind */
660 	err = -ENODEV;
661 done_unlock:
662 	/* Release the socket list lock */
663 	write_unlock_bh(&iucv_sk_list.lock);
664 done:
665 	release_sock(sk);
666 	return err;
667 }
668 
669 /* Automatically bind an unbound socket */
670 static int iucv_sock_autobind(struct sock *sk)
671 {
672 	struct iucv_sock *iucv = iucv_sk(sk);
673 	int err = 0;
674 
675 	if (unlikely(!pr_iucv))
676 		return -EPROTO;
677 
678 	memcpy(iucv->src_user_id, iucv_userid, 8);
679 	iucv->transport = AF_IUCV_TRANS_IUCV;
680 	sk->sk_allocation |= GFP_DMA;
681 
682 	write_lock_bh(&iucv_sk_list.lock);
683 	__iucv_auto_name(iucv);
684 	write_unlock_bh(&iucv_sk_list.lock);
685 
686 	if (!iucv->msglimit)
687 		iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
688 
689 	return err;
690 }
691 
692 static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
693 {
694 	struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
695 	struct sock *sk = sock->sk;
696 	struct iucv_sock *iucv = iucv_sk(sk);
697 	unsigned char user_data[16];
698 	int err;
699 
700 	high_nmcpy(user_data, sa->siucv_name);
701 	low_nmcpy(user_data, iucv->src_name);
702 	ASCEBC(user_data, sizeof(user_data));
703 
704 	/* Create path. */
705 	iucv->path = iucv_path_alloc(iucv->msglimit,
706 				     IUCV_IPRMDATA, GFP_KERNEL);
707 	if (!iucv->path) {
708 		err = -ENOMEM;
709 		goto done;
710 	}
711 	err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
712 				    sa->siucv_user_id, NULL, user_data,
713 				    sk);
714 	if (err) {
715 		iucv_path_free(iucv->path);
716 		iucv->path = NULL;
717 		switch (err) {
718 		case 0x0b:	/* Target communicator is not logged on */
719 			err = -ENETUNREACH;
720 			break;
721 		case 0x0d:	/* Max connections for this guest exceeded */
722 		case 0x0e:	/* Max connections for target guest exceeded */
723 			err = -EAGAIN;
724 			break;
725 		case 0x0f:	/* Missing IUCV authorization */
726 			err = -EACCES;
727 			break;
728 		default:
729 			err = -ECONNREFUSED;
730 			break;
731 		}
732 	}
733 done:
734 	return err;
735 }
736 
737 /* Connect an unconnected socket */
738 static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
739 			     int alen, int flags)
740 {
741 	struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
742 	struct sock *sk = sock->sk;
743 	struct iucv_sock *iucv = iucv_sk(sk);
744 	int err;
745 
746 	if (alen < sizeof(struct sockaddr_iucv) || addr->sa_family != AF_IUCV)
747 		return -EINVAL;
748 
749 	if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
750 		return -EBADFD;
751 
752 	if (sk->sk_state == IUCV_OPEN &&
753 	    iucv->transport == AF_IUCV_TRANS_HIPER)
754 		return -EBADFD; /* explicit bind required */
755 
756 	if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
757 		return -EINVAL;
758 
759 	if (sk->sk_state == IUCV_OPEN) {
760 		err = iucv_sock_autobind(sk);
761 		if (unlikely(err))
762 			return err;
763 	}
764 
765 	lock_sock(sk);
766 
767 	/* Set the destination information */
768 	memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
769 	memcpy(iucv->dst_name, sa->siucv_name, 8);
770 
771 	if (iucv->transport == AF_IUCV_TRANS_HIPER)
772 		err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
773 	else
774 		err = afiucv_path_connect(sock, addr);
775 	if (err)
776 		goto done;
777 
778 	if (sk->sk_state != IUCV_CONNECTED)
779 		err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
780 							    IUCV_DISCONN),
781 				     sock_sndtimeo(sk, flags & O_NONBLOCK));
782 
783 	if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
784 		err = -ECONNREFUSED;
785 
786 	if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
787 		iucv_sever_path(sk, 0);
788 
789 done:
790 	release_sock(sk);
791 	return err;
792 }
793 
794 /* Move a socket into listening state. */
795 static int iucv_sock_listen(struct socket *sock, int backlog)
796 {
797 	struct sock *sk = sock->sk;
798 	int err;
799 
800 	lock_sock(sk);
801 
802 	err = -EINVAL;
803 	if (sk->sk_state != IUCV_BOUND)
804 		goto done;
805 
806 	if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
807 		goto done;
808 
809 	sk->sk_max_ack_backlog = backlog;
810 	sk->sk_ack_backlog = 0;
811 	sk->sk_state = IUCV_LISTEN;
812 	err = 0;
813 
814 done:
815 	release_sock(sk);
816 	return err;
817 }
818 
819 /* Accept a pending connection */
820 static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
821 			    int flags, bool kern)
822 {
823 	DECLARE_WAITQUEUE(wait, current);
824 	struct sock *sk = sock->sk, *nsk;
825 	long timeo;
826 	int err = 0;
827 
828 	lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
829 
830 	if (sk->sk_state != IUCV_LISTEN) {
831 		err = -EBADFD;
832 		goto done;
833 	}
834 
835 	timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
836 
837 	/* Wait for an incoming connection */
838 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
839 	while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
840 		set_current_state(TASK_INTERRUPTIBLE);
841 		if (!timeo) {
842 			err = -EAGAIN;
843 			break;
844 		}
845 
846 		release_sock(sk);
847 		timeo = schedule_timeout(timeo);
848 		lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
849 
850 		if (sk->sk_state != IUCV_LISTEN) {
851 			err = -EBADFD;
852 			break;
853 		}
854 
855 		if (signal_pending(current)) {
856 			err = sock_intr_errno(timeo);
857 			break;
858 		}
859 	}
860 
861 	set_current_state(TASK_RUNNING);
862 	remove_wait_queue(sk_sleep(sk), &wait);
863 
864 	if (err)
865 		goto done;
866 
867 	newsock->state = SS_CONNECTED;
868 
869 done:
870 	release_sock(sk);
871 	return err;
872 }
873 
874 static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
875 			     int peer)
876 {
877 	struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
878 	struct sock *sk = sock->sk;
879 	struct iucv_sock *iucv = iucv_sk(sk);
880 
881 	addr->sa_family = AF_IUCV;
882 
883 	if (peer) {
884 		memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
885 		memcpy(siucv->siucv_name, iucv->dst_name, 8);
886 	} else {
887 		memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
888 		memcpy(siucv->siucv_name, iucv->src_name, 8);
889 	}
890 	memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
891 	memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
892 	memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
893 
894 	return sizeof(struct sockaddr_iucv);
895 }
896 
897 /**
898  * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
899  * @path:	IUCV path
900  * @msg:	Pointer to a struct iucv_message
901  * @skb:	The socket data to send, skb->len MUST BE <= 7
902  *
903  * Send the socket data in the parameter list in the iucv message
904  * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
905  * list and the socket data len at index 7 (last byte).
906  * See also iucv_msg_length().
907  *
908  * Returns the error code from the iucv_message_send() call.
909  */
910 static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
911 			  struct sk_buff *skb)
912 {
913 	u8 prmdata[8];
914 
915 	memcpy(prmdata, (void *) skb->data, skb->len);
916 	prmdata[7] = 0xff - (u8) skb->len;
917 	return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
918 				 (void *) prmdata, 8);
919 }
920 
921 static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg,
922 			     size_t len)
923 {
924 	struct sock *sk = sock->sk;
925 	struct iucv_sock *iucv = iucv_sk(sk);
926 	size_t headroom = 0;
927 	size_t linear;
928 	struct sk_buff *skb;
929 	struct iucv_message txmsg = {0};
930 	struct cmsghdr *cmsg;
931 	int cmsg_done;
932 	long timeo;
933 	char user_id[9];
934 	char appl_id[9];
935 	int err;
936 	int noblock = msg->msg_flags & MSG_DONTWAIT;
937 
938 	err = sock_error(sk);
939 	if (err)
940 		return err;
941 
942 	if (msg->msg_flags & MSG_OOB)
943 		return -EOPNOTSUPP;
944 
945 	/* SOCK_SEQPACKET: we do not support segmented records */
946 	if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
947 		return -EOPNOTSUPP;
948 
949 	lock_sock(sk);
950 
951 	if (sk->sk_shutdown & SEND_SHUTDOWN) {
952 		err = -EPIPE;
953 		goto out;
954 	}
955 
956 	/* Return if the socket is not in connected state */
957 	if (sk->sk_state != IUCV_CONNECTED) {
958 		err = -ENOTCONN;
959 		goto out;
960 	}
961 
962 	/* initialize defaults */
963 	cmsg_done   = 0;	/* check for duplicate headers */
964 
965 	/* iterate over control messages */
966 	for_each_cmsghdr(cmsg, msg) {
967 		if (!CMSG_OK(msg, cmsg)) {
968 			err = -EINVAL;
969 			goto out;
970 		}
971 
972 		if (cmsg->cmsg_level != SOL_IUCV)
973 			continue;
974 
975 		if (cmsg->cmsg_type & cmsg_done) {
976 			err = -EINVAL;
977 			goto out;
978 		}
979 		cmsg_done |= cmsg->cmsg_type;
980 
981 		switch (cmsg->cmsg_type) {
982 		case SCM_IUCV_TRGCLS:
983 			if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
984 				err = -EINVAL;
985 				goto out;
986 			}
987 
988 			/* set iucv message target class */
989 			memcpy(&txmsg.class,
990 				(void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
991 
992 			break;
993 
994 		default:
995 			err = -EINVAL;
996 			goto out;
997 		}
998 	}
999 
1000 	/* allocate one skb for each iucv message:
1001 	 * this is fine for SOCK_SEQPACKET (unless we want to support
1002 	 * segmented records using the MSG_EOR flag), but
1003 	 * for SOCK_STREAM we might want to improve it in future */
1004 	if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1005 		headroom = sizeof(struct af_iucv_trans_hdr) +
1006 			   LL_RESERVED_SPACE(iucv->hs_dev);
1007 		linear = len;
1008 	} else {
1009 		if (len < PAGE_SIZE) {
1010 			linear = len;
1011 		} else {
1012 			/* In nonlinear "classic" iucv skb,
1013 			 * reserve space for iucv_array
1014 			 */
1015 			headroom = sizeof(struct iucv_array) *
1016 				   (MAX_SKB_FRAGS + 1);
1017 			linear = PAGE_SIZE - headroom;
1018 		}
1019 	}
1020 	skb = sock_alloc_send_pskb(sk, headroom + linear, len - linear,
1021 				   noblock, &err, 0);
1022 	if (!skb)
1023 		goto out;
1024 	if (headroom)
1025 		skb_reserve(skb, headroom);
1026 	skb_put(skb, linear);
1027 	skb->len = len;
1028 	skb->data_len = len - linear;
1029 	err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
1030 	if (err)
1031 		goto fail;
1032 
1033 	/* wait if outstanding messages for iucv path has reached */
1034 	timeo = sock_sndtimeo(sk, noblock);
1035 	err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
1036 	if (err)
1037 		goto fail;
1038 
1039 	/* return -ECONNRESET if the socket is no longer connected */
1040 	if (sk->sk_state != IUCV_CONNECTED) {
1041 		err = -ECONNRESET;
1042 		goto fail;
1043 	}
1044 
1045 	/* increment and save iucv message tag for msg_completion cbk */
1046 	txmsg.tag = iucv->send_tag++;
1047 	IUCV_SKB_CB(skb)->tag = txmsg.tag;
1048 
1049 	if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1050 		atomic_inc(&iucv->msg_sent);
1051 		err = afiucv_hs_send(&txmsg, sk, skb, 0);
1052 		if (err) {
1053 			atomic_dec(&iucv->msg_sent);
1054 			goto out;
1055 		}
1056 	} else { /* Classic VM IUCV transport */
1057 		skb_queue_tail(&iucv->send_skb_q, skb);
1058 
1059 		if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags) &&
1060 		    skb->len <= 7) {
1061 			err = iucv_send_iprm(iucv->path, &txmsg, skb);
1062 
1063 			/* on success: there is no message_complete callback */
1064 			/* for an IPRMDATA msg; remove skb from send queue   */
1065 			if (err == 0) {
1066 				skb_unlink(skb, &iucv->send_skb_q);
1067 				kfree_skb(skb);
1068 			}
1069 
1070 			/* this error should never happen since the	*/
1071 			/* IUCV_IPRMDATA path flag is set... sever path */
1072 			if (err == 0x15) {
1073 				pr_iucv->path_sever(iucv->path, NULL);
1074 				skb_unlink(skb, &iucv->send_skb_q);
1075 				err = -EPIPE;
1076 				goto fail;
1077 			}
1078 		} else if (skb_is_nonlinear(skb)) {
1079 			struct iucv_array *iba = (struct iucv_array *)skb->head;
1080 			int i;
1081 
1082 			/* skip iucv_array lying in the headroom */
1083 			iba[0].address = (u32)(addr_t)skb->data;
1084 			iba[0].length = (u32)skb_headlen(skb);
1085 			for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1086 				skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1087 
1088 				iba[i + 1].address =
1089 					(u32)(addr_t)skb_frag_address(frag);
1090 				iba[i + 1].length = (u32)skb_frag_size(frag);
1091 			}
1092 			err = pr_iucv->message_send(iucv->path, &txmsg,
1093 						    IUCV_IPBUFLST, 0,
1094 						    (void *)iba, skb->len);
1095 		} else { /* non-IPRM Linear skb */
1096 			err = pr_iucv->message_send(iucv->path, &txmsg,
1097 					0, 0, (void *)skb->data, skb->len);
1098 		}
1099 		if (err) {
1100 			if (err == 3) {
1101 				user_id[8] = 0;
1102 				memcpy(user_id, iucv->dst_user_id, 8);
1103 				appl_id[8] = 0;
1104 				memcpy(appl_id, iucv->dst_name, 8);
1105 				pr_err(
1106 		"Application %s on z/VM guest %s exceeds message limit\n",
1107 					appl_id, user_id);
1108 				err = -EAGAIN;
1109 			} else {
1110 				err = -EPIPE;
1111 			}
1112 			skb_unlink(skb, &iucv->send_skb_q);
1113 			goto fail;
1114 		}
1115 	}
1116 
1117 	release_sock(sk);
1118 	return len;
1119 
1120 fail:
1121 	kfree_skb(skb);
1122 out:
1123 	release_sock(sk);
1124 	return err;
1125 }
1126 
1127 static struct sk_buff *alloc_iucv_recv_skb(unsigned long len)
1128 {
1129 	size_t headroom, linear;
1130 	struct sk_buff *skb;
1131 	int err;
1132 
1133 	if (len < PAGE_SIZE) {
1134 		headroom = 0;
1135 		linear = len;
1136 	} else {
1137 		headroom = sizeof(struct iucv_array) * (MAX_SKB_FRAGS + 1);
1138 		linear = PAGE_SIZE - headroom;
1139 	}
1140 	skb = alloc_skb_with_frags(headroom + linear, len - linear,
1141 				   0, &err, GFP_ATOMIC | GFP_DMA);
1142 	WARN_ONCE(!skb,
1143 		  "alloc of recv iucv skb len=%lu failed with errcode=%d\n",
1144 		  len, err);
1145 	if (skb) {
1146 		if (headroom)
1147 			skb_reserve(skb, headroom);
1148 		skb_put(skb, linear);
1149 		skb->len = len;
1150 		skb->data_len = len - linear;
1151 	}
1152 	return skb;
1153 }
1154 
1155 /* iucv_process_message() - Receive a single outstanding IUCV message
1156  *
1157  * Locking: must be called with message_q.lock held
1158  */
1159 static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1160 				 struct iucv_path *path,
1161 				 struct iucv_message *msg)
1162 {
1163 	int rc;
1164 	unsigned int len;
1165 
1166 	len = iucv_msg_length(msg);
1167 
1168 	/* store msg target class in the second 4 bytes of skb ctrl buffer */
1169 	/* Note: the first 4 bytes are reserved for msg tag */
1170 	IUCV_SKB_CB(skb)->class = msg->class;
1171 
1172 	/* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1173 	if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1174 		if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1175 			skb->data = NULL;
1176 			skb->len = 0;
1177 		}
1178 	} else {
1179 		if (skb_is_nonlinear(skb)) {
1180 			struct iucv_array *iba = (struct iucv_array *)skb->head;
1181 			int i;
1182 
1183 			iba[0].address = (u32)(addr_t)skb->data;
1184 			iba[0].length = (u32)skb_headlen(skb);
1185 			for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1186 				skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1187 
1188 				iba[i + 1].address =
1189 					(u32)(addr_t)skb_frag_address(frag);
1190 				iba[i + 1].length = (u32)skb_frag_size(frag);
1191 			}
1192 			rc = pr_iucv->message_receive(path, msg,
1193 					      IUCV_IPBUFLST,
1194 					      (void *)iba, len, NULL);
1195 		} else {
1196 			rc = pr_iucv->message_receive(path, msg,
1197 					      msg->flags & IUCV_IPRMDATA,
1198 					      skb->data, len, NULL);
1199 		}
1200 		if (rc) {
1201 			kfree_skb(skb);
1202 			return;
1203 		}
1204 		WARN_ON_ONCE(skb->len != len);
1205 	}
1206 
1207 	IUCV_SKB_CB(skb)->offset = 0;
1208 	if (sk_filter(sk, skb)) {
1209 		atomic_inc(&sk->sk_drops);	/* skb rejected by filter */
1210 		kfree_skb(skb);
1211 		return;
1212 	}
1213 	if (__sock_queue_rcv_skb(sk, skb))	/* handle rcv queue full */
1214 		skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
1215 }
1216 
1217 /* iucv_process_message_q() - Process outstanding IUCV messages
1218  *
1219  * Locking: must be called with message_q.lock held
1220  */
1221 static void iucv_process_message_q(struct sock *sk)
1222 {
1223 	struct iucv_sock *iucv = iucv_sk(sk);
1224 	struct sk_buff *skb;
1225 	struct sock_msg_q *p, *n;
1226 
1227 	list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
1228 		skb = alloc_iucv_recv_skb(iucv_msg_length(&p->msg));
1229 		if (!skb)
1230 			break;
1231 		iucv_process_message(sk, skb, p->path, &p->msg);
1232 		list_del(&p->list);
1233 		kfree(p);
1234 		if (!skb_queue_empty(&iucv->backlog_skb_q))
1235 			break;
1236 	}
1237 }
1238 
1239 static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1240 			     size_t len, int flags)
1241 {
1242 	int noblock = flags & MSG_DONTWAIT;
1243 	struct sock *sk = sock->sk;
1244 	struct iucv_sock *iucv = iucv_sk(sk);
1245 	unsigned int copied, rlen;
1246 	struct sk_buff *skb, *rskb, *cskb;
1247 	int err = 0;
1248 	u32 offset;
1249 
1250 	if ((sk->sk_state == IUCV_DISCONN) &&
1251 	    skb_queue_empty(&iucv->backlog_skb_q) &&
1252 	    skb_queue_empty(&sk->sk_receive_queue) &&
1253 	    list_empty(&iucv->message_q.list))
1254 		return 0;
1255 
1256 	if (flags & (MSG_OOB))
1257 		return -EOPNOTSUPP;
1258 
1259 	/* receive/dequeue next skb:
1260 	 * the function understands MSG_PEEK and, thus, does not dequeue skb */
1261 	skb = skb_recv_datagram(sk, flags, noblock, &err);
1262 	if (!skb) {
1263 		if (sk->sk_shutdown & RCV_SHUTDOWN)
1264 			return 0;
1265 		return err;
1266 	}
1267 
1268 	offset = IUCV_SKB_CB(skb)->offset;
1269 	rlen   = skb->len - offset;		/* real length of skb */
1270 	copied = min_t(unsigned int, rlen, len);
1271 	if (!rlen)
1272 		sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
1273 
1274 	cskb = skb;
1275 	if (skb_copy_datagram_msg(cskb, offset, msg, copied)) {
1276 		if (!(flags & MSG_PEEK))
1277 			skb_queue_head(&sk->sk_receive_queue, skb);
1278 		return -EFAULT;
1279 	}
1280 
1281 	/* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1282 	if (sk->sk_type == SOCK_SEQPACKET) {
1283 		if (copied < rlen)
1284 			msg->msg_flags |= MSG_TRUNC;
1285 		/* each iucv message contains a complete record */
1286 		msg->msg_flags |= MSG_EOR;
1287 	}
1288 
1289 	/* create control message to store iucv msg target class:
1290 	 * get the trgcls from the control buffer of the skb due to
1291 	 * fragmentation of original iucv message. */
1292 	err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1293 		       sizeof(IUCV_SKB_CB(skb)->class),
1294 		       (void *)&IUCV_SKB_CB(skb)->class);
1295 	if (err) {
1296 		if (!(flags & MSG_PEEK))
1297 			skb_queue_head(&sk->sk_receive_queue, skb);
1298 		return err;
1299 	}
1300 
1301 	/* Mark read part of skb as used */
1302 	if (!(flags & MSG_PEEK)) {
1303 
1304 		/* SOCK_STREAM: re-queue skb if it contains unreceived data */
1305 		if (sk->sk_type == SOCK_STREAM) {
1306 			if (copied < rlen) {
1307 				IUCV_SKB_CB(skb)->offset = offset + copied;
1308 				skb_queue_head(&sk->sk_receive_queue, skb);
1309 				goto done;
1310 			}
1311 		}
1312 
1313 		kfree_skb(skb);
1314 		if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1315 			atomic_inc(&iucv->msg_recv);
1316 			if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
1317 				WARN_ON(1);
1318 				iucv_sock_close(sk);
1319 				return -EFAULT;
1320 			}
1321 		}
1322 
1323 		/* Queue backlog skbs */
1324 		spin_lock_bh(&iucv->message_q.lock);
1325 		rskb = skb_dequeue(&iucv->backlog_skb_q);
1326 		while (rskb) {
1327 			IUCV_SKB_CB(rskb)->offset = 0;
1328 			if (__sock_queue_rcv_skb(sk, rskb)) {
1329 				/* handle rcv queue full */
1330 				skb_queue_head(&iucv->backlog_skb_q,
1331 						rskb);
1332 				break;
1333 			}
1334 			rskb = skb_dequeue(&iucv->backlog_skb_q);
1335 		}
1336 		if (skb_queue_empty(&iucv->backlog_skb_q)) {
1337 			if (!list_empty(&iucv->message_q.list))
1338 				iucv_process_message_q(sk);
1339 			if (atomic_read(&iucv->msg_recv) >=
1340 							iucv->msglimit / 2) {
1341 				err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
1342 				if (err) {
1343 					sk->sk_state = IUCV_DISCONN;
1344 					sk->sk_state_change(sk);
1345 				}
1346 			}
1347 		}
1348 		spin_unlock_bh(&iucv->message_q.lock);
1349 	}
1350 
1351 done:
1352 	/* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1353 	if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1354 		copied = rlen;
1355 
1356 	return copied;
1357 }
1358 
1359 static inline __poll_t iucv_accept_poll(struct sock *parent)
1360 {
1361 	struct iucv_sock *isk, *n;
1362 	struct sock *sk;
1363 
1364 	list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
1365 		sk = (struct sock *) isk;
1366 
1367 		if (sk->sk_state == IUCV_CONNECTED)
1368 			return EPOLLIN | EPOLLRDNORM;
1369 	}
1370 
1371 	return 0;
1372 }
1373 
1374 static __poll_t iucv_sock_poll(struct file *file, struct socket *sock,
1375 			       poll_table *wait)
1376 {
1377 	struct sock *sk = sock->sk;
1378 	__poll_t mask = 0;
1379 
1380 	sock_poll_wait(file, sock, wait);
1381 
1382 	if (sk->sk_state == IUCV_LISTEN)
1383 		return iucv_accept_poll(sk);
1384 
1385 	if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1386 		mask |= EPOLLERR |
1387 			(sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
1388 
1389 	if (sk->sk_shutdown & RCV_SHUTDOWN)
1390 		mask |= EPOLLRDHUP;
1391 
1392 	if (sk->sk_shutdown == SHUTDOWN_MASK)
1393 		mask |= EPOLLHUP;
1394 
1395 	if (!skb_queue_empty(&sk->sk_receive_queue) ||
1396 	    (sk->sk_shutdown & RCV_SHUTDOWN))
1397 		mask |= EPOLLIN | EPOLLRDNORM;
1398 
1399 	if (sk->sk_state == IUCV_CLOSED)
1400 		mask |= EPOLLHUP;
1401 
1402 	if (sk->sk_state == IUCV_DISCONN)
1403 		mask |= EPOLLIN;
1404 
1405 	if (sock_writeable(sk) && iucv_below_msglim(sk))
1406 		mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
1407 	else
1408 		sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1409 
1410 	return mask;
1411 }
1412 
1413 static int iucv_sock_shutdown(struct socket *sock, int how)
1414 {
1415 	struct sock *sk = sock->sk;
1416 	struct iucv_sock *iucv = iucv_sk(sk);
1417 	struct iucv_message txmsg;
1418 	int err = 0;
1419 
1420 	how++;
1421 
1422 	if ((how & ~SHUTDOWN_MASK) || !how)
1423 		return -EINVAL;
1424 
1425 	lock_sock(sk);
1426 	switch (sk->sk_state) {
1427 	case IUCV_LISTEN:
1428 	case IUCV_DISCONN:
1429 	case IUCV_CLOSING:
1430 	case IUCV_CLOSED:
1431 		err = -ENOTCONN;
1432 		goto fail;
1433 	default:
1434 		break;
1435 	}
1436 
1437 	if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
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 				char __user *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 (get_user(val, (int __user *) optval))
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_type, 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 = &iucv_sk(sk)->send_skb_q;
1751 	struct sk_buff *list_skb;
1752 	unsigned long flags;
1753 
1754 	bh_lock_sock(sk);
1755 
1756 	spin_lock_irqsave(&list->lock, flags);
1757 	skb_queue_walk(list, list_skb) {
1758 		if (msg->tag == IUCV_SKB_CB(list_skb)->tag) {
1759 			this = list_skb;
1760 			break;
1761 		}
1762 	}
1763 	if (this)
1764 		__skb_unlink(this, list);
1765 	spin_unlock_irqrestore(&list->lock, flags);
1766 
1767 	if (this) {
1768 		kfree_skb(this);
1769 		/* wake up any process waiting for sending */
1770 		iucv_sock_wake_msglim(sk);
1771 	}
1772 
1773 	if (sk->sk_state == IUCV_CLOSING) {
1774 		if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1775 			sk->sk_state = IUCV_CLOSED;
1776 			sk->sk_state_change(sk);
1777 		}
1778 	}
1779 	bh_unlock_sock(sk);
1780 
1781 }
1782 
1783 static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1784 {
1785 	struct sock *sk = path->private;
1786 
1787 	if (sk->sk_state == IUCV_CLOSED)
1788 		return;
1789 
1790 	bh_lock_sock(sk);
1791 	iucv_sever_path(sk, 1);
1792 	sk->sk_state = IUCV_DISCONN;
1793 
1794 	sk->sk_state_change(sk);
1795 	bh_unlock_sock(sk);
1796 }
1797 
1798 /* called if the other communication side shuts down its RECV direction;
1799  * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1800  */
1801 static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1802 {
1803 	struct sock *sk = path->private;
1804 
1805 	bh_lock_sock(sk);
1806 	if (sk->sk_state != IUCV_CLOSED) {
1807 		sk->sk_shutdown |= SEND_SHUTDOWN;
1808 		sk->sk_state_change(sk);
1809 	}
1810 	bh_unlock_sock(sk);
1811 }
1812 
1813 /***************** HiperSockets transport callbacks ********************/
1814 static void afiucv_swap_src_dest(struct sk_buff *skb)
1815 {
1816 	struct af_iucv_trans_hdr *trans_hdr = iucv_trans_hdr(skb);
1817 	char tmpID[8];
1818 	char tmpName[8];
1819 
1820 	ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1821 	ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1822 	ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1823 	ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1824 	memcpy(tmpID, trans_hdr->srcUserID, 8);
1825 	memcpy(tmpName, trans_hdr->srcAppName, 8);
1826 	memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1827 	memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1828 	memcpy(trans_hdr->destUserID, tmpID, 8);
1829 	memcpy(trans_hdr->destAppName, tmpName, 8);
1830 	skb_push(skb, ETH_HLEN);
1831 	memset(skb->data, 0, ETH_HLEN);
1832 }
1833 
1834 /**
1835  * afiucv_hs_callback_syn - react on received SYN
1836  **/
1837 static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1838 {
1839 	struct af_iucv_trans_hdr *trans_hdr = iucv_trans_hdr(skb);
1840 	struct sock *nsk;
1841 	struct iucv_sock *iucv, *niucv;
1842 	int err;
1843 
1844 	iucv = iucv_sk(sk);
1845 	if (!iucv) {
1846 		/* no sock - connection refused */
1847 		afiucv_swap_src_dest(skb);
1848 		trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1849 		err = dev_queue_xmit(skb);
1850 		goto out;
1851 	}
1852 
1853 	nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
1854 	bh_lock_sock(sk);
1855 	if ((sk->sk_state != IUCV_LISTEN) ||
1856 	    sk_acceptq_is_full(sk) ||
1857 	    !nsk) {
1858 		/* error on server socket - connection refused */
1859 		afiucv_swap_src_dest(skb);
1860 		trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1861 		err = dev_queue_xmit(skb);
1862 		iucv_sock_kill(nsk);
1863 		bh_unlock_sock(sk);
1864 		goto out;
1865 	}
1866 
1867 	niucv = iucv_sk(nsk);
1868 	iucv_sock_init(nsk, sk);
1869 	niucv->transport = AF_IUCV_TRANS_HIPER;
1870 	niucv->msglimit = iucv->msglimit;
1871 	if (!trans_hdr->window)
1872 		niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
1873 	else
1874 		niucv->msglimit_peer = trans_hdr->window;
1875 	memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
1876 	memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
1877 	memcpy(niucv->src_name, iucv->src_name, 8);
1878 	memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1879 	nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
1880 	niucv->hs_dev = iucv->hs_dev;
1881 	dev_hold(niucv->hs_dev);
1882 	afiucv_swap_src_dest(skb);
1883 	trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
1884 	trans_hdr->window = niucv->msglimit;
1885 	/* if receiver acks the xmit connection is established */
1886 	err = dev_queue_xmit(skb);
1887 	if (!err) {
1888 		iucv_accept_enqueue(sk, nsk);
1889 		nsk->sk_state = IUCV_CONNECTED;
1890 		sk->sk_data_ready(sk);
1891 	} else
1892 		iucv_sock_kill(nsk);
1893 	bh_unlock_sock(sk);
1894 
1895 out:
1896 	return NET_RX_SUCCESS;
1897 }
1898 
1899 /**
1900  * afiucv_hs_callback_synack() - react on received SYN-ACK
1901  **/
1902 static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
1903 {
1904 	struct iucv_sock *iucv = iucv_sk(sk);
1905 
1906 	if (!iucv)
1907 		goto out;
1908 	if (sk->sk_state != IUCV_BOUND)
1909 		goto out;
1910 	bh_lock_sock(sk);
1911 	iucv->msglimit_peer = iucv_trans_hdr(skb)->window;
1912 	sk->sk_state = IUCV_CONNECTED;
1913 	sk->sk_state_change(sk);
1914 	bh_unlock_sock(sk);
1915 out:
1916 	kfree_skb(skb);
1917 	return NET_RX_SUCCESS;
1918 }
1919 
1920 /**
1921  * afiucv_hs_callback_synfin() - react on received SYN_FIN
1922  **/
1923 static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
1924 {
1925 	struct iucv_sock *iucv = iucv_sk(sk);
1926 
1927 	if (!iucv)
1928 		goto out;
1929 	if (sk->sk_state != IUCV_BOUND)
1930 		goto out;
1931 	bh_lock_sock(sk);
1932 	sk->sk_state = IUCV_DISCONN;
1933 	sk->sk_state_change(sk);
1934 	bh_unlock_sock(sk);
1935 out:
1936 	kfree_skb(skb);
1937 	return NET_RX_SUCCESS;
1938 }
1939 
1940 /**
1941  * afiucv_hs_callback_fin() - react on received FIN
1942  **/
1943 static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
1944 {
1945 	struct iucv_sock *iucv = iucv_sk(sk);
1946 
1947 	/* other end of connection closed */
1948 	if (!iucv)
1949 		goto out;
1950 	bh_lock_sock(sk);
1951 	if (sk->sk_state == IUCV_CONNECTED) {
1952 		sk->sk_state = IUCV_DISCONN;
1953 		sk->sk_state_change(sk);
1954 	}
1955 	bh_unlock_sock(sk);
1956 out:
1957 	kfree_skb(skb);
1958 	return NET_RX_SUCCESS;
1959 }
1960 
1961 /**
1962  * afiucv_hs_callback_win() - react on received WIN
1963  **/
1964 static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
1965 {
1966 	struct iucv_sock *iucv = iucv_sk(sk);
1967 
1968 	if (!iucv)
1969 		return NET_RX_SUCCESS;
1970 
1971 	if (sk->sk_state != IUCV_CONNECTED)
1972 		return NET_RX_SUCCESS;
1973 
1974 	atomic_sub(iucv_trans_hdr(skb)->window, &iucv->msg_sent);
1975 	iucv_sock_wake_msglim(sk);
1976 	return NET_RX_SUCCESS;
1977 }
1978 
1979 /**
1980  * afiucv_hs_callback_rx() - react on received data
1981  **/
1982 static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
1983 {
1984 	struct iucv_sock *iucv = iucv_sk(sk);
1985 
1986 	if (!iucv) {
1987 		kfree_skb(skb);
1988 		return NET_RX_SUCCESS;
1989 	}
1990 
1991 	if (sk->sk_state != IUCV_CONNECTED) {
1992 		kfree_skb(skb);
1993 		return NET_RX_SUCCESS;
1994 	}
1995 
1996 	if (sk->sk_shutdown & RCV_SHUTDOWN) {
1997 		kfree_skb(skb);
1998 		return NET_RX_SUCCESS;
1999 	}
2000 
2001 	/* write stuff from iucv_msg to skb cb */
2002 	skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2003 	skb_reset_transport_header(skb);
2004 	skb_reset_network_header(skb);
2005 	IUCV_SKB_CB(skb)->offset = 0;
2006 	if (sk_filter(sk, skb)) {
2007 		atomic_inc(&sk->sk_drops);	/* skb rejected by filter */
2008 		kfree_skb(skb);
2009 		return NET_RX_SUCCESS;
2010 	}
2011 
2012 	spin_lock(&iucv->message_q.lock);
2013 	if (skb_queue_empty(&iucv->backlog_skb_q)) {
2014 		if (__sock_queue_rcv_skb(sk, skb))
2015 			/* handle rcv queue full */
2016 			skb_queue_tail(&iucv->backlog_skb_q, skb);
2017 	} else
2018 		skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2019 	spin_unlock(&iucv->message_q.lock);
2020 	return NET_RX_SUCCESS;
2021 }
2022 
2023 /**
2024  * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2025  *                   transport
2026  *                   called from netif RX softirq
2027  **/
2028 static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2029 	struct packet_type *pt, struct net_device *orig_dev)
2030 {
2031 	struct sock *sk;
2032 	struct iucv_sock *iucv;
2033 	struct af_iucv_trans_hdr *trans_hdr;
2034 	int err = NET_RX_SUCCESS;
2035 	char nullstring[8];
2036 
2037 	if (!pskb_may_pull(skb, sizeof(*trans_hdr))) {
2038 		WARN_ONCE(1, "AF_IUCV failed to receive skb, len=%u", skb->len);
2039 		kfree_skb(skb);
2040 		return NET_RX_SUCCESS;
2041 	}
2042 
2043 	trans_hdr = iucv_trans_hdr(skb);
2044 	EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2045 	EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2046 	EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2047 	EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2048 	memset(nullstring, 0, sizeof(nullstring));
2049 	iucv = NULL;
2050 	sk = NULL;
2051 	read_lock(&iucv_sk_list.lock);
2052 	sk_for_each(sk, &iucv_sk_list.head) {
2053 		if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2054 			if ((!memcmp(&iucv_sk(sk)->src_name,
2055 				     trans_hdr->destAppName, 8)) &&
2056 			    (!memcmp(&iucv_sk(sk)->src_user_id,
2057 				     trans_hdr->destUserID, 8)) &&
2058 			    (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2059 			    (!memcmp(&iucv_sk(sk)->dst_user_id,
2060 				     nullstring, 8))) {
2061 				iucv = iucv_sk(sk);
2062 				break;
2063 			}
2064 		} else {
2065 			if ((!memcmp(&iucv_sk(sk)->src_name,
2066 				     trans_hdr->destAppName, 8)) &&
2067 			    (!memcmp(&iucv_sk(sk)->src_user_id,
2068 				     trans_hdr->destUserID, 8)) &&
2069 			    (!memcmp(&iucv_sk(sk)->dst_name,
2070 				     trans_hdr->srcAppName, 8)) &&
2071 			    (!memcmp(&iucv_sk(sk)->dst_user_id,
2072 				     trans_hdr->srcUserID, 8))) {
2073 				iucv = iucv_sk(sk);
2074 				break;
2075 			}
2076 		}
2077 	}
2078 	read_unlock(&iucv_sk_list.lock);
2079 	if (!iucv)
2080 		sk = NULL;
2081 
2082 	/* no sock
2083 	how should we send with no sock
2084 	1) send without sock no send rc checking?
2085 	2) introduce default sock to handle this cases
2086 
2087 	 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2088 	 data -> send FIN
2089 	 SYN|ACK, SYN|FIN, FIN -> no action? */
2090 
2091 	switch (trans_hdr->flags) {
2092 	case AF_IUCV_FLAG_SYN:
2093 		/* connect request */
2094 		err = afiucv_hs_callback_syn(sk, skb);
2095 		break;
2096 	case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2097 		/* connect request confirmed */
2098 		err = afiucv_hs_callback_synack(sk, skb);
2099 		break;
2100 	case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2101 		/* connect request refused */
2102 		err = afiucv_hs_callback_synfin(sk, skb);
2103 		break;
2104 	case (AF_IUCV_FLAG_FIN):
2105 		/* close request */
2106 		err = afiucv_hs_callback_fin(sk, skb);
2107 		break;
2108 	case (AF_IUCV_FLAG_WIN):
2109 		err = afiucv_hs_callback_win(sk, skb);
2110 		if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
2111 			kfree_skb(skb);
2112 			break;
2113 		}
2114 		/* fall through - and receive non-zero length data */
2115 	case (AF_IUCV_FLAG_SHT):
2116 		/* shutdown request */
2117 		/* fall through - and receive zero length data */
2118 	case 0:
2119 		/* plain data frame */
2120 		IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
2121 		err = afiucv_hs_callback_rx(sk, skb);
2122 		break;
2123 	default:
2124 		kfree_skb(skb);
2125 	}
2126 
2127 	return err;
2128 }
2129 
2130 /**
2131  * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
2132  *                                 transport
2133  **/
2134 static void afiucv_hs_callback_txnotify(struct sk_buff *skb,
2135 					enum iucv_tx_notify n)
2136 {
2137 	struct sock *isk = skb->sk;
2138 	struct sock *sk = NULL;
2139 	struct iucv_sock *iucv = NULL;
2140 	struct sk_buff_head *list;
2141 	struct sk_buff *list_skb;
2142 	struct sk_buff *nskb;
2143 	unsigned long flags;
2144 
2145 	read_lock_irqsave(&iucv_sk_list.lock, flags);
2146 	sk_for_each(sk, &iucv_sk_list.head)
2147 		if (sk == isk) {
2148 			iucv = iucv_sk(sk);
2149 			break;
2150 		}
2151 	read_unlock_irqrestore(&iucv_sk_list.lock, flags);
2152 
2153 	if (!iucv || sock_flag(sk, SOCK_ZAPPED))
2154 		return;
2155 
2156 	list = &iucv->send_skb_q;
2157 	spin_lock_irqsave(&list->lock, flags);
2158 	skb_queue_walk_safe(list, list_skb, nskb) {
2159 		if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
2160 			switch (n) {
2161 			case TX_NOTIFY_OK:
2162 				__skb_unlink(list_skb, list);
2163 				kfree_skb(list_skb);
2164 				iucv_sock_wake_msglim(sk);
2165 				break;
2166 			case TX_NOTIFY_PENDING:
2167 				atomic_inc(&iucv->pendings);
2168 				break;
2169 			case TX_NOTIFY_DELAYED_OK:
2170 				__skb_unlink(list_skb, list);
2171 				atomic_dec(&iucv->pendings);
2172 				if (atomic_read(&iucv->pendings) <= 0)
2173 					iucv_sock_wake_msglim(sk);
2174 				kfree_skb(list_skb);
2175 				break;
2176 			case TX_NOTIFY_UNREACHABLE:
2177 			case TX_NOTIFY_DELAYED_UNREACHABLE:
2178 			case TX_NOTIFY_TPQFULL: /* not yet used */
2179 			case TX_NOTIFY_GENERALERROR:
2180 			case TX_NOTIFY_DELAYED_GENERALERROR:
2181 				__skb_unlink(list_skb, list);
2182 				kfree_skb(list_skb);
2183 				if (sk->sk_state == IUCV_CONNECTED) {
2184 					sk->sk_state = IUCV_DISCONN;
2185 					sk->sk_state_change(sk);
2186 				}
2187 				break;
2188 			}
2189 			break;
2190 		}
2191 	}
2192 	spin_unlock_irqrestore(&list->lock, flags);
2193 
2194 	if (sk->sk_state == IUCV_CLOSING) {
2195 		if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
2196 			sk->sk_state = IUCV_CLOSED;
2197 			sk->sk_state_change(sk);
2198 		}
2199 	}
2200 
2201 }
2202 
2203 /*
2204  * afiucv_netdev_event: handle netdev notifier chain events
2205  */
2206 static int afiucv_netdev_event(struct notifier_block *this,
2207 			       unsigned long event, void *ptr)
2208 {
2209 	struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2210 	struct sock *sk;
2211 	struct iucv_sock *iucv;
2212 
2213 	switch (event) {
2214 	case NETDEV_REBOOT:
2215 	case NETDEV_GOING_DOWN:
2216 		sk_for_each(sk, &iucv_sk_list.head) {
2217 			iucv = iucv_sk(sk);
2218 			if ((iucv->hs_dev == event_dev) &&
2219 			    (sk->sk_state == IUCV_CONNECTED)) {
2220 				if (event == NETDEV_GOING_DOWN)
2221 					iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
2222 				sk->sk_state = IUCV_DISCONN;
2223 				sk->sk_state_change(sk);
2224 			}
2225 		}
2226 		break;
2227 	case NETDEV_DOWN:
2228 	case NETDEV_UNREGISTER:
2229 	default:
2230 		break;
2231 	}
2232 	return NOTIFY_DONE;
2233 }
2234 
2235 static struct notifier_block afiucv_netdev_notifier = {
2236 	.notifier_call = afiucv_netdev_event,
2237 };
2238 
2239 static const struct proto_ops iucv_sock_ops = {
2240 	.family		= PF_IUCV,
2241 	.owner		= THIS_MODULE,
2242 	.release	= iucv_sock_release,
2243 	.bind		= iucv_sock_bind,
2244 	.connect	= iucv_sock_connect,
2245 	.listen		= iucv_sock_listen,
2246 	.accept		= iucv_sock_accept,
2247 	.getname	= iucv_sock_getname,
2248 	.sendmsg	= iucv_sock_sendmsg,
2249 	.recvmsg	= iucv_sock_recvmsg,
2250 	.poll		= iucv_sock_poll,
2251 	.ioctl		= sock_no_ioctl,
2252 	.mmap		= sock_no_mmap,
2253 	.socketpair	= sock_no_socketpair,
2254 	.shutdown	= iucv_sock_shutdown,
2255 	.setsockopt	= iucv_sock_setsockopt,
2256 	.getsockopt	= iucv_sock_getsockopt,
2257 };
2258 
2259 static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
2260 			    int kern)
2261 {
2262 	struct sock *sk;
2263 
2264 	if (protocol && protocol != PF_IUCV)
2265 		return -EPROTONOSUPPORT;
2266 
2267 	sock->state = SS_UNCONNECTED;
2268 
2269 	switch (sock->type) {
2270 	case SOCK_STREAM:
2271 	case SOCK_SEQPACKET:
2272 		/* currently, proto ops can handle both sk types */
2273 		sock->ops = &iucv_sock_ops;
2274 		break;
2275 	default:
2276 		return -ESOCKTNOSUPPORT;
2277 	}
2278 
2279 	sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL, kern);
2280 	if (!sk)
2281 		return -ENOMEM;
2282 
2283 	iucv_sock_init(sk, NULL);
2284 
2285 	return 0;
2286 }
2287 
2288 static const struct net_proto_family iucv_sock_family_ops = {
2289 	.family	= AF_IUCV,
2290 	.owner	= THIS_MODULE,
2291 	.create	= iucv_sock_create,
2292 };
2293 
2294 static struct packet_type iucv_packet_type = {
2295 	.type = cpu_to_be16(ETH_P_AF_IUCV),
2296 	.func = afiucv_hs_rcv,
2297 };
2298 
2299 static int afiucv_iucv_init(void)
2300 {
2301 	return pr_iucv->iucv_register(&af_iucv_handler, 0);
2302 }
2303 
2304 static void afiucv_iucv_exit(void)
2305 {
2306 	pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2307 }
2308 
2309 static int __init afiucv_init(void)
2310 {
2311 	int err;
2312 
2313 	if (MACHINE_IS_VM) {
2314 		cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2315 		if (unlikely(err)) {
2316 			WARN_ON(err);
2317 			err = -EPROTONOSUPPORT;
2318 			goto out;
2319 		}
2320 
2321 		pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
2322 		if (!pr_iucv) {
2323 			printk(KERN_WARNING "iucv_if lookup failed\n");
2324 			memset(&iucv_userid, 0, sizeof(iucv_userid));
2325 		}
2326 	} else {
2327 		memset(&iucv_userid, 0, sizeof(iucv_userid));
2328 		pr_iucv = NULL;
2329 	}
2330 
2331 	err = proto_register(&iucv_proto, 0);
2332 	if (err)
2333 		goto out;
2334 	err = sock_register(&iucv_sock_family_ops);
2335 	if (err)
2336 		goto out_proto;
2337 
2338 	if (pr_iucv) {
2339 		err = afiucv_iucv_init();
2340 		if (err)
2341 			goto out_sock;
2342 	}
2343 
2344 	err = register_netdevice_notifier(&afiucv_netdev_notifier);
2345 	if (err)
2346 		goto out_notifier;
2347 
2348 	dev_add_pack(&iucv_packet_type);
2349 	return 0;
2350 
2351 out_notifier:
2352 	if (pr_iucv)
2353 		afiucv_iucv_exit();
2354 out_sock:
2355 	sock_unregister(PF_IUCV);
2356 out_proto:
2357 	proto_unregister(&iucv_proto);
2358 out:
2359 	if (pr_iucv)
2360 		symbol_put(iucv_if);
2361 	return err;
2362 }
2363 
2364 static void __exit afiucv_exit(void)
2365 {
2366 	if (pr_iucv) {
2367 		afiucv_iucv_exit();
2368 		symbol_put(iucv_if);
2369 	}
2370 
2371 	unregister_netdevice_notifier(&afiucv_netdev_notifier);
2372 	dev_remove_pack(&iucv_packet_type);
2373 	sock_unregister(PF_IUCV);
2374 	proto_unregister(&iucv_proto);
2375 }
2376 
2377 module_init(afiucv_init);
2378 module_exit(afiucv_exit);
2379 
2380 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2381 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2382 MODULE_VERSION(VERSION);
2383 MODULE_LICENSE("GPL");
2384 MODULE_ALIAS_NETPROTO(PF_IUCV);
2385