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