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