1 // SPDX-License-Identifier: GPL-2.0-only
3 * IUCV protocol stack for Linux on zSeries
5 * Copyright IBM Corp. 2006, 2009
7 * Author(s): Jennifer Hunt <jenhunt@us.ibm.com>
8 * Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
10 * Ursula Braun <ursula.braun@de.ibm.com>
13 #define KMSG_COMPONENT "af_iucv"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16 #include <linux/filter.h>
17 #include <linux/module.h>
18 #include <linux/netdevice.h>
19 #include <linux/types.h>
20 #include <linux/limits.h>
21 #include <linux/list.h>
22 #include <linux/errno.h>
23 #include <linux/kernel.h>
24 #include <linux/sched/signal.h>
25 #include <linux/slab.h>
26 #include <linux/skbuff.h>
27 #include <linux/init.h>
28 #include <linux/poll.h>
29 #include <linux/security.h>
31 #include <asm/ebcdic.h>
32 #include <asm/cpcmd.h>
33 #include <linux/kmod.h>
35 #include <net/iucv/af_iucv.h>
39 static char iucv_userid[80];
41 static struct proto iucv_proto = {
44 .obj_size = sizeof(struct iucv_sock),
47 static struct iucv_interface *pr_iucv;
48 static struct iucv_handler af_iucv_handler;
50 /* special AF_IUCV IPRM messages */
51 static const u8 iprm_shutdown[8] =
52 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
54 #define TRGCLS_SIZE sizeof_field(struct iucv_message, class)
56 #define __iucv_sock_wait(sk, condition, timeo, ret) \
58 DEFINE_WAIT(__wait); \
59 long __timeo = timeo; \
61 prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
62 while (!(condition)) { \
67 if (signal_pending(current)) { \
68 ret = sock_intr_errno(__timeo); \
72 __timeo = schedule_timeout(__timeo); \
74 ret = sock_error(sk); \
78 finish_wait(sk_sleep(sk), &__wait); \
81 #define iucv_sock_wait(sk, condition, timeo) \
85 __iucv_sock_wait(sk, condition, timeo, __ret); \
89 static struct sock *iucv_accept_dequeue(struct sock *parent,
90 struct socket *newsock);
91 static void iucv_sock_kill(struct sock *sk);
92 static void iucv_sock_close(struct sock *sk);
94 static void afiucv_hs_callback_txnotify(struct sock *sk, enum iucv_tx_notify);
96 static struct iucv_sock_list iucv_sk_list = {
97 .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
98 .autobind_name = ATOMIC_INIT(0)
101 static inline void high_nmcpy(unsigned char *dst, char *src)
106 static inline void low_nmcpy(unsigned char *dst, char *src)
108 memcpy(&dst[8], src, 8);
112 * iucv_msg_length() - Returns the length of an iucv message.
113 * @msg: Pointer to struct iucv_message, MUST NOT be NULL
115 * The function returns the length of the specified iucv message @msg of data
116 * stored in a buffer and of data stored in the parameter list (PRMDATA).
118 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
120 * PRMDATA[0..6] socket data (max 7 bytes);
121 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
123 * The socket data length is computed by subtracting the socket data length
125 * If the socket data len is greater 7, then PRMDATA can be used for special
126 * notifications (see iucv_sock_shutdown); and further,
127 * if the socket data len is > 7, the function returns 8.
129 * Use this function to allocate socket buffers to store iucv message data.
131 static inline size_t iucv_msg_length(struct iucv_message *msg)
135 if (msg->flags & IUCV_IPRMDATA) {
136 datalen = 0xff - msg->rmmsg[7];
137 return (datalen < 8) ? datalen : 8;
143 * iucv_sock_in_state() - check for specific states
144 * @sk: sock structure
145 * @state: first iucv sk state
146 * @state2: second iucv sk state
148 * Returns true if the socket in either in the first or second state.
150 static int iucv_sock_in_state(struct sock *sk, int state, int state2)
152 return (sk->sk_state == state || sk->sk_state == state2);
156 * iucv_below_msglim() - function to check if messages can be sent
157 * @sk: sock structure
159 * Returns true if the send queue length is lower than the message limit.
160 * Always returns true if the socket is not connected (no iucv path for
161 * checking the message limit).
163 static inline int iucv_below_msglim(struct sock *sk)
165 struct iucv_sock *iucv = iucv_sk(sk);
167 if (sk->sk_state != IUCV_CONNECTED)
169 if (iucv->transport == AF_IUCV_TRANS_IUCV)
170 return (atomic_read(&iucv->skbs_in_xmit) < iucv->path->msglim);
172 return ((atomic_read(&iucv->msg_sent) < iucv->msglimit_peer) &&
173 (atomic_read(&iucv->pendings) <= 0));
177 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
179 static void iucv_sock_wake_msglim(struct sock *sk)
181 struct socket_wq *wq;
184 wq = rcu_dereference(sk->sk_wq);
185 if (skwq_has_sleeper(wq))
186 wake_up_interruptible_all(&wq->wait);
187 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
192 * afiucv_hs_send() - send a message through HiperSockets transport
194 static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
195 struct sk_buff *skb, u8 flags)
197 struct iucv_sock *iucv = iucv_sk(sock);
198 struct af_iucv_trans_hdr *phs_hdr;
199 int err, confirm_recv = 0;
201 phs_hdr = skb_push(skb, sizeof(*phs_hdr));
202 memset(phs_hdr, 0, sizeof(*phs_hdr));
203 skb_reset_network_header(skb);
205 phs_hdr->magic = ETH_P_AF_IUCV;
206 phs_hdr->version = 1;
207 phs_hdr->flags = flags;
208 if (flags == AF_IUCV_FLAG_SYN)
209 phs_hdr->window = iucv->msglimit;
210 else if ((flags == AF_IUCV_FLAG_WIN) || !flags) {
211 confirm_recv = atomic_read(&iucv->msg_recv);
212 phs_hdr->window = confirm_recv;
214 phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
216 memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
217 memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
218 memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
219 memcpy(phs_hdr->srcAppName, iucv->src_name, 8);
220 ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID));
221 ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName));
222 ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID));
223 ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName));
225 memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message));
227 skb->dev = iucv->hs_dev;
233 dev_hard_header(skb, skb->dev, ETH_P_AF_IUCV, NULL, NULL, skb->len);
235 if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev)) {
239 if (skb->len > skb->dev->mtu) {
240 if (sock->sk_type == SOCK_SEQPACKET) {
244 err = pskb_trim(skb, skb->dev->mtu);
248 skb->protocol = cpu_to_be16(ETH_P_AF_IUCV);
250 atomic_inc(&iucv->skbs_in_xmit);
251 err = dev_queue_xmit(skb);
252 if (net_xmit_eval(err)) {
253 atomic_dec(&iucv->skbs_in_xmit);
255 atomic_sub(confirm_recv, &iucv->msg_recv);
256 WARN_ON(atomic_read(&iucv->msg_recv) < 0);
258 return net_xmit_eval(err);
265 static struct sock *__iucv_get_sock_by_name(char *nm)
269 sk_for_each(sk, &iucv_sk_list.head)
270 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
276 static void iucv_sock_destruct(struct sock *sk)
278 skb_queue_purge(&sk->sk_receive_queue);
279 skb_queue_purge(&sk->sk_error_queue);
283 if (!sock_flag(sk, SOCK_DEAD)) {
284 pr_err("Attempt to release alive iucv socket %p\n", sk);
288 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
289 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
290 WARN_ON(sk->sk_wmem_queued);
291 WARN_ON(sk->sk_forward_alloc);
295 static void iucv_sock_cleanup_listen(struct sock *parent)
299 /* Close non-accepted connections */
300 while ((sk = iucv_accept_dequeue(parent, NULL))) {
305 parent->sk_state = IUCV_CLOSED;
308 static void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
310 write_lock_bh(&l->lock);
311 sk_add_node(sk, &l->head);
312 write_unlock_bh(&l->lock);
315 static void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
317 write_lock_bh(&l->lock);
318 sk_del_node_init(sk);
319 write_unlock_bh(&l->lock);
322 /* Kill socket (only if zapped and orphaned) */
323 static void iucv_sock_kill(struct sock *sk)
325 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
328 iucv_sock_unlink(&iucv_sk_list, sk);
329 sock_set_flag(sk, SOCK_DEAD);
333 /* Terminate an IUCV path */
334 static void iucv_sever_path(struct sock *sk, int with_user_data)
336 unsigned char user_data[16];
337 struct iucv_sock *iucv = iucv_sk(sk);
338 struct iucv_path *path = iucv->path;
342 if (with_user_data) {
343 low_nmcpy(user_data, iucv->src_name);
344 high_nmcpy(user_data, iucv->dst_name);
345 ASCEBC(user_data, sizeof(user_data));
346 pr_iucv->path_sever(path, user_data);
348 pr_iucv->path_sever(path, NULL);
349 iucv_path_free(path);
353 /* Send controlling flags through an IUCV socket for HIPER transport */
354 static int iucv_send_ctrl(struct sock *sk, u8 flags)
356 struct iucv_sock *iucv = iucv_sk(sk);
362 blen = sizeof(struct af_iucv_trans_hdr) +
363 LL_RESERVED_SPACE(iucv->hs_dev);
364 if (sk->sk_shutdown & SEND_SHUTDOWN) {
365 /* controlling flags should be sent anyway */
366 shutdown = sk->sk_shutdown;
367 sk->sk_shutdown &= RCV_SHUTDOWN;
369 skb = sock_alloc_send_skb(sk, blen, 1, &err);
371 skb_reserve(skb, blen);
372 err = afiucv_hs_send(NULL, sk, skb, flags);
375 sk->sk_shutdown = shutdown;
379 /* Close an IUCV socket */
380 static void iucv_sock_close(struct sock *sk)
382 struct iucv_sock *iucv = iucv_sk(sk);
388 switch (sk->sk_state) {
390 iucv_sock_cleanup_listen(sk);
394 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
395 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
396 sk->sk_state = IUCV_DISCONN;
397 sk->sk_state_change(sk);
402 sk->sk_state = IUCV_CLOSING;
403 sk->sk_state_change(sk);
405 if (!err && atomic_read(&iucv->skbs_in_xmit) > 0) {
406 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
407 timeo = sk->sk_lingertime;
409 timeo = IUCV_DISCONN_TIMEOUT;
411 iucv_sock_in_state(sk, IUCV_CLOSED, 0),
417 sk->sk_state = IUCV_CLOSED;
418 sk->sk_state_change(sk);
420 sk->sk_err = ECONNRESET;
421 sk->sk_state_change(sk);
423 skb_queue_purge(&iucv->send_skb_q);
424 skb_queue_purge(&iucv->backlog_skb_q);
428 iucv_sever_path(sk, 1);
432 dev_put(iucv->hs_dev);
434 sk->sk_bound_dev_if = 0;
437 /* mark socket for deletion by iucv_sock_kill() */
438 sock_set_flag(sk, SOCK_ZAPPED);
443 static void iucv_sock_init(struct sock *sk, struct sock *parent)
446 sk->sk_type = parent->sk_type;
447 security_sk_clone(parent, sk);
451 static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio, int kern)
454 struct iucv_sock *iucv;
456 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, kern);
461 sock_init_data(sock, sk);
462 INIT_LIST_HEAD(&iucv->accept_q);
463 spin_lock_init(&iucv->accept_q_lock);
464 skb_queue_head_init(&iucv->send_skb_q);
465 INIT_LIST_HEAD(&iucv->message_q.list);
466 spin_lock_init(&iucv->message_q.lock);
467 skb_queue_head_init(&iucv->backlog_skb_q);
469 atomic_set(&iucv->pendings, 0);
472 atomic_set(&iucv->skbs_in_xmit, 0);
473 atomic_set(&iucv->msg_sent, 0);
474 atomic_set(&iucv->msg_recv, 0);
476 iucv->sk_txnotify = afiucv_hs_callback_txnotify;
477 memset(&iucv->init, 0, sizeof(iucv->init));
479 iucv->transport = AF_IUCV_TRANS_IUCV;
481 iucv->transport = AF_IUCV_TRANS_HIPER;
483 sk->sk_destruct = iucv_sock_destruct;
484 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
486 sock_reset_flag(sk, SOCK_ZAPPED);
488 sk->sk_protocol = proto;
489 sk->sk_state = IUCV_OPEN;
491 iucv_sock_link(&iucv_sk_list, sk);
495 static void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
498 struct iucv_sock *par = iucv_sk(parent);
501 spin_lock_irqsave(&par->accept_q_lock, flags);
502 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
503 spin_unlock_irqrestore(&par->accept_q_lock, flags);
504 iucv_sk(sk)->parent = parent;
505 sk_acceptq_added(parent);
508 static void iucv_accept_unlink(struct sock *sk)
511 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
513 spin_lock_irqsave(&par->accept_q_lock, flags);
514 list_del_init(&iucv_sk(sk)->accept_q);
515 spin_unlock_irqrestore(&par->accept_q_lock, flags);
516 sk_acceptq_removed(iucv_sk(sk)->parent);
517 iucv_sk(sk)->parent = NULL;
521 static struct sock *iucv_accept_dequeue(struct sock *parent,
522 struct socket *newsock)
524 struct iucv_sock *isk, *n;
527 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
528 sk = (struct sock *) isk;
531 if (sk->sk_state == IUCV_CLOSED) {
532 iucv_accept_unlink(sk);
537 if (sk->sk_state == IUCV_CONNECTED ||
538 sk->sk_state == IUCV_DISCONN ||
540 iucv_accept_unlink(sk);
542 sock_graft(sk, newsock);
553 static void __iucv_auto_name(struct iucv_sock *iucv)
557 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
558 while (__iucv_get_sock_by_name(name)) {
559 sprintf(name, "%08x",
560 atomic_inc_return(&iucv_sk_list.autobind_name));
562 memcpy(iucv->src_name, name, 8);
565 /* Bind an unbound socket */
566 static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
569 DECLARE_SOCKADDR(struct sockaddr_iucv *, sa, addr);
570 char uid[sizeof(sa->siucv_user_id)];
571 struct sock *sk = sock->sk;
572 struct iucv_sock *iucv;
574 struct net_device *dev;
576 /* Verify the input sockaddr */
577 if (addr_len < sizeof(struct sockaddr_iucv) ||
578 addr->sa_family != AF_IUCV)
582 if (sk->sk_state != IUCV_OPEN) {
587 write_lock_bh(&iucv_sk_list.lock);
590 if (__iucv_get_sock_by_name(sa->siucv_name)) {
597 /* Bind the socket */
599 if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
600 goto vm_bind; /* VM IUCV transport */
602 /* try hiper transport */
603 memcpy(uid, sa->siucv_user_id, sizeof(uid));
606 for_each_netdev_rcu(&init_net, dev) {
607 if (!memcmp(dev->perm_addr, uid, 8)) {
608 memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
609 /* Check for uninitialized siucv_name */
610 if (strncmp(sa->siucv_name, " ", 8) == 0)
611 __iucv_auto_name(iucv);
613 memcpy(iucv->src_name, sa->siucv_name, 8);
614 sk->sk_bound_dev_if = dev->ifindex;
617 sk->sk_state = IUCV_BOUND;
618 iucv->transport = AF_IUCV_TRANS_HIPER;
620 iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
628 /* use local userid for backward compat */
629 memcpy(iucv->src_name, sa->siucv_name, 8);
630 memcpy(iucv->src_user_id, iucv_userid, 8);
631 sk->sk_state = IUCV_BOUND;
632 iucv->transport = AF_IUCV_TRANS_IUCV;
633 sk->sk_allocation |= GFP_DMA;
635 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
638 /* found no dev to bind */
641 /* Release the socket list lock */
642 write_unlock_bh(&iucv_sk_list.lock);
648 /* Automatically bind an unbound socket */
649 static int iucv_sock_autobind(struct sock *sk)
651 struct iucv_sock *iucv = iucv_sk(sk);
654 if (unlikely(!pr_iucv))
657 memcpy(iucv->src_user_id, iucv_userid, 8);
658 iucv->transport = AF_IUCV_TRANS_IUCV;
659 sk->sk_allocation |= GFP_DMA;
661 write_lock_bh(&iucv_sk_list.lock);
662 __iucv_auto_name(iucv);
663 write_unlock_bh(&iucv_sk_list.lock);
666 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
671 static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
673 DECLARE_SOCKADDR(struct sockaddr_iucv *, sa, addr);
674 struct sock *sk = sock->sk;
675 struct iucv_sock *iucv = iucv_sk(sk);
676 unsigned char user_data[16];
679 high_nmcpy(user_data, sa->siucv_name);
680 low_nmcpy(user_data, iucv->src_name);
681 ASCEBC(user_data, sizeof(user_data));
684 iucv->path = iucv_path_alloc(iucv->msglimit,
685 IUCV_IPRMDATA, GFP_KERNEL);
690 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
691 sa->siucv_user_id, NULL, user_data,
694 iucv_path_free(iucv->path);
697 case 0x0b: /* Target communicator is not logged on */
700 case 0x0d: /* Max connections for this guest exceeded */
701 case 0x0e: /* Max connections for target guest exceeded */
704 case 0x0f: /* Missing IUCV authorization */
716 /* Connect an unconnected socket */
717 static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
720 DECLARE_SOCKADDR(struct sockaddr_iucv *, sa, addr);
721 struct sock *sk = sock->sk;
722 struct iucv_sock *iucv = iucv_sk(sk);
725 if (alen < sizeof(struct sockaddr_iucv) || addr->sa_family != AF_IUCV)
728 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
731 if (sk->sk_state == IUCV_OPEN &&
732 iucv->transport == AF_IUCV_TRANS_HIPER)
733 return -EBADFD; /* explicit bind required */
735 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
738 if (sk->sk_state == IUCV_OPEN) {
739 err = iucv_sock_autobind(sk);
746 /* Set the destination information */
747 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
748 memcpy(iucv->dst_name, sa->siucv_name, 8);
750 if (iucv->transport == AF_IUCV_TRANS_HIPER)
751 err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
753 err = afiucv_path_connect(sock, addr);
757 if (sk->sk_state != IUCV_CONNECTED)
758 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
760 sock_sndtimeo(sk, flags & O_NONBLOCK));
762 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
765 if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
766 iucv_sever_path(sk, 0);
773 /* Move a socket into listening state. */
774 static int iucv_sock_listen(struct socket *sock, int backlog)
776 struct sock *sk = sock->sk;
782 if (sk->sk_state != IUCV_BOUND)
785 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
788 sk->sk_max_ack_backlog = backlog;
789 sk->sk_ack_backlog = 0;
790 sk->sk_state = IUCV_LISTEN;
798 /* Accept a pending connection */
799 static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
800 int flags, bool kern)
802 DECLARE_WAITQUEUE(wait, current);
803 struct sock *sk = sock->sk, *nsk;
807 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
809 if (sk->sk_state != IUCV_LISTEN) {
814 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
816 /* Wait for an incoming connection */
817 add_wait_queue_exclusive(sk_sleep(sk), &wait);
818 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
819 set_current_state(TASK_INTERRUPTIBLE);
826 timeo = schedule_timeout(timeo);
827 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
829 if (sk->sk_state != IUCV_LISTEN) {
834 if (signal_pending(current)) {
835 err = sock_intr_errno(timeo);
840 set_current_state(TASK_RUNNING);
841 remove_wait_queue(sk_sleep(sk), &wait);
846 newsock->state = SS_CONNECTED;
853 static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
856 DECLARE_SOCKADDR(struct sockaddr_iucv *, siucv, addr);
857 struct sock *sk = sock->sk;
858 struct iucv_sock *iucv = iucv_sk(sk);
860 addr->sa_family = AF_IUCV;
863 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
864 memcpy(siucv->siucv_name, iucv->dst_name, 8);
866 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
867 memcpy(siucv->siucv_name, iucv->src_name, 8);
869 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
870 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
871 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
873 return sizeof(struct sockaddr_iucv);
877 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
879 * @msg: Pointer to a struct iucv_message
880 * @skb: The socket data to send, skb->len MUST BE <= 7
882 * Send the socket data in the parameter list in the iucv message
883 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
884 * list and the socket data len at index 7 (last byte).
885 * See also iucv_msg_length().
887 * Returns the error code from the iucv_message_send() call.
889 static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
894 memcpy(prmdata, (void *) skb->data, skb->len);
895 prmdata[7] = 0xff - (u8) skb->len;
896 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
897 (void *) prmdata, 8);
900 static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg,
903 struct sock *sk = sock->sk;
904 struct iucv_sock *iucv = iucv_sk(sk);
908 struct iucv_message txmsg = {0};
909 struct cmsghdr *cmsg;
915 int noblock = msg->msg_flags & MSG_DONTWAIT;
917 err = sock_error(sk);
921 if (msg->msg_flags & MSG_OOB)
924 /* SOCK_SEQPACKET: we do not support segmented records */
925 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
930 if (sk->sk_shutdown & SEND_SHUTDOWN) {
935 /* Return if the socket is not in connected state */
936 if (sk->sk_state != IUCV_CONNECTED) {
941 /* initialize defaults */
942 cmsg_done = 0; /* check for duplicate headers */
944 /* iterate over control messages */
945 for_each_cmsghdr(cmsg, msg) {
946 if (!CMSG_OK(msg, cmsg)) {
951 if (cmsg->cmsg_level != SOL_IUCV)
954 if (cmsg->cmsg_type & cmsg_done) {
958 cmsg_done |= cmsg->cmsg_type;
960 switch (cmsg->cmsg_type) {
961 case SCM_IUCV_TRGCLS:
962 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
967 /* set iucv message target class */
969 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
979 /* allocate one skb for each iucv message:
980 * this is fine for SOCK_SEQPACKET (unless we want to support
981 * segmented records using the MSG_EOR flag), but
982 * for SOCK_STREAM we might want to improve it in future */
983 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
984 headroom = sizeof(struct af_iucv_trans_hdr) +
985 LL_RESERVED_SPACE(iucv->hs_dev);
986 linear = min(len, PAGE_SIZE - headroom);
988 if (len < PAGE_SIZE) {
991 /* In nonlinear "classic" iucv skb,
992 * reserve space for iucv_array
994 headroom = sizeof(struct iucv_array) *
996 linear = PAGE_SIZE - headroom;
999 skb = sock_alloc_send_pskb(sk, headroom + linear, len - linear,
1004 skb_reserve(skb, headroom);
1005 skb_put(skb, linear);
1007 skb->data_len = len - linear;
1008 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
1012 /* wait if outstanding messages for iucv path has reached */
1013 timeo = sock_sndtimeo(sk, noblock);
1014 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
1018 /* return -ECONNRESET if the socket is no longer connected */
1019 if (sk->sk_state != IUCV_CONNECTED) {
1024 /* increment and save iucv message tag for msg_completion cbk */
1025 txmsg.tag = iucv->send_tag++;
1026 IUCV_SKB_CB(skb)->tag = txmsg.tag;
1028 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1029 atomic_inc(&iucv->msg_sent);
1030 err = afiucv_hs_send(&txmsg, sk, skb, 0);
1032 atomic_dec(&iucv->msg_sent);
1035 } else { /* Classic VM IUCV transport */
1036 skb_queue_tail(&iucv->send_skb_q, skb);
1037 atomic_inc(&iucv->skbs_in_xmit);
1039 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags) &&
1041 err = iucv_send_iprm(iucv->path, &txmsg, skb);
1043 /* on success: there is no message_complete callback */
1044 /* for an IPRMDATA msg; remove skb from send queue */
1046 atomic_dec(&iucv->skbs_in_xmit);
1047 skb_unlink(skb, &iucv->send_skb_q);
1051 /* this error should never happen since the */
1052 /* IUCV_IPRMDATA path flag is set... sever path */
1054 pr_iucv->path_sever(iucv->path, NULL);
1055 atomic_dec(&iucv->skbs_in_xmit);
1056 skb_unlink(skb, &iucv->send_skb_q);
1060 } else if (skb_is_nonlinear(skb)) {
1061 struct iucv_array *iba = (struct iucv_array *)skb->head;
1064 /* skip iucv_array lying in the headroom */
1065 iba[0].address = (u32)(addr_t)skb->data;
1066 iba[0].length = (u32)skb_headlen(skb);
1067 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1068 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1070 iba[i + 1].address =
1071 (u32)(addr_t)skb_frag_address(frag);
1072 iba[i + 1].length = (u32)skb_frag_size(frag);
1074 err = pr_iucv->message_send(iucv->path, &txmsg,
1076 (void *)iba, skb->len);
1077 } else { /* non-IPRM Linear skb */
1078 err = pr_iucv->message_send(iucv->path, &txmsg,
1079 0, 0, (void *)skb->data, skb->len);
1084 memcpy(user_id, iucv->dst_user_id, 8);
1086 memcpy(appl_id, iucv->dst_name, 8);
1088 "Application %s on z/VM guest %s exceeds message limit\n",
1095 atomic_dec(&iucv->skbs_in_xmit);
1096 skb_unlink(skb, &iucv->send_skb_q);
1111 static struct sk_buff *alloc_iucv_recv_skb(unsigned long len)
1113 size_t headroom, linear;
1114 struct sk_buff *skb;
1117 if (len < PAGE_SIZE) {
1121 headroom = sizeof(struct iucv_array) * (MAX_SKB_FRAGS + 1);
1122 linear = PAGE_SIZE - headroom;
1124 skb = alloc_skb_with_frags(headroom + linear, len - linear,
1125 0, &err, GFP_ATOMIC | GFP_DMA);
1127 "alloc of recv iucv skb len=%lu failed with errcode=%d\n",
1131 skb_reserve(skb, headroom);
1132 skb_put(skb, linear);
1134 skb->data_len = len - linear;
1139 /* iucv_process_message() - Receive a single outstanding IUCV message
1141 * Locking: must be called with message_q.lock held
1143 static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1144 struct iucv_path *path,
1145 struct iucv_message *msg)
1150 len = iucv_msg_length(msg);
1152 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1153 /* Note: the first 4 bytes are reserved for msg tag */
1154 IUCV_SKB_CB(skb)->class = msg->class;
1156 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1157 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1158 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1163 if (skb_is_nonlinear(skb)) {
1164 struct iucv_array *iba = (struct iucv_array *)skb->head;
1167 iba[0].address = (u32)(addr_t)skb->data;
1168 iba[0].length = (u32)skb_headlen(skb);
1169 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1170 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1172 iba[i + 1].address =
1173 (u32)(addr_t)skb_frag_address(frag);
1174 iba[i + 1].length = (u32)skb_frag_size(frag);
1176 rc = pr_iucv->message_receive(path, msg,
1178 (void *)iba, len, NULL);
1180 rc = pr_iucv->message_receive(path, msg,
1181 msg->flags & IUCV_IPRMDATA,
1182 skb->data, len, NULL);
1188 WARN_ON_ONCE(skb->len != len);
1191 IUCV_SKB_CB(skb)->offset = 0;
1192 if (sk_filter(sk, skb)) {
1193 atomic_inc(&sk->sk_drops); /* skb rejected by filter */
1197 if (__sock_queue_rcv_skb(sk, skb)) /* handle rcv queue full */
1198 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
1201 /* iucv_process_message_q() - Process outstanding IUCV messages
1203 * Locking: must be called with message_q.lock held
1205 static void iucv_process_message_q(struct sock *sk)
1207 struct iucv_sock *iucv = iucv_sk(sk);
1208 struct sk_buff *skb;
1209 struct sock_msg_q *p, *n;
1211 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
1212 skb = alloc_iucv_recv_skb(iucv_msg_length(&p->msg));
1215 iucv_process_message(sk, skb, p->path, &p->msg);
1218 if (!skb_queue_empty(&iucv->backlog_skb_q))
1223 static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1224 size_t len, int flags)
1226 int noblock = flags & MSG_DONTWAIT;
1227 struct sock *sk = sock->sk;
1228 struct iucv_sock *iucv = iucv_sk(sk);
1229 unsigned int copied, rlen;
1230 struct sk_buff *skb, *rskb, *cskb;
1234 if ((sk->sk_state == IUCV_DISCONN) &&
1235 skb_queue_empty(&iucv->backlog_skb_q) &&
1236 skb_queue_empty(&sk->sk_receive_queue) &&
1237 list_empty(&iucv->message_q.list))
1240 if (flags & (MSG_OOB))
1243 /* receive/dequeue next skb:
1244 * the function understands MSG_PEEK and, thus, does not dequeue skb */
1245 skb = skb_recv_datagram(sk, flags, noblock, &err);
1247 if (sk->sk_shutdown & RCV_SHUTDOWN)
1252 offset = IUCV_SKB_CB(skb)->offset;
1253 rlen = skb->len - offset; /* real length of skb */
1254 copied = min_t(unsigned int, rlen, len);
1256 sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
1259 if (skb_copy_datagram_msg(cskb, offset, msg, copied)) {
1260 if (!(flags & MSG_PEEK))
1261 skb_queue_head(&sk->sk_receive_queue, skb);
1265 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1266 if (sk->sk_type == SOCK_SEQPACKET) {
1268 msg->msg_flags |= MSG_TRUNC;
1269 /* each iucv message contains a complete record */
1270 msg->msg_flags |= MSG_EOR;
1273 /* create control message to store iucv msg target class:
1274 * get the trgcls from the control buffer of the skb due to
1275 * fragmentation of original iucv message. */
1276 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1277 sizeof(IUCV_SKB_CB(skb)->class),
1278 (void *)&IUCV_SKB_CB(skb)->class);
1280 if (!(flags & MSG_PEEK))
1281 skb_queue_head(&sk->sk_receive_queue, skb);
1285 /* Mark read part of skb as used */
1286 if (!(flags & MSG_PEEK)) {
1288 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1289 if (sk->sk_type == SOCK_STREAM) {
1290 if (copied < rlen) {
1291 IUCV_SKB_CB(skb)->offset = offset + copied;
1292 skb_queue_head(&sk->sk_receive_queue, skb);
1298 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1299 atomic_inc(&iucv->msg_recv);
1300 if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
1302 iucv_sock_close(sk);
1307 /* Queue backlog skbs */
1308 spin_lock_bh(&iucv->message_q.lock);
1309 rskb = skb_dequeue(&iucv->backlog_skb_q);
1311 IUCV_SKB_CB(rskb)->offset = 0;
1312 if (__sock_queue_rcv_skb(sk, rskb)) {
1313 /* handle rcv queue full */
1314 skb_queue_head(&iucv->backlog_skb_q,
1318 rskb = skb_dequeue(&iucv->backlog_skb_q);
1320 if (skb_queue_empty(&iucv->backlog_skb_q)) {
1321 if (!list_empty(&iucv->message_q.list))
1322 iucv_process_message_q(sk);
1323 if (atomic_read(&iucv->msg_recv) >=
1324 iucv->msglimit / 2) {
1325 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
1327 sk->sk_state = IUCV_DISCONN;
1328 sk->sk_state_change(sk);
1332 spin_unlock_bh(&iucv->message_q.lock);
1336 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1337 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1343 static inline __poll_t iucv_accept_poll(struct sock *parent)
1345 struct iucv_sock *isk, *n;
1348 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
1349 sk = (struct sock *) isk;
1351 if (sk->sk_state == IUCV_CONNECTED)
1352 return EPOLLIN | EPOLLRDNORM;
1358 static __poll_t iucv_sock_poll(struct file *file, struct socket *sock,
1361 struct sock *sk = sock->sk;
1364 sock_poll_wait(file, sock, wait);
1366 if (sk->sk_state == IUCV_LISTEN)
1367 return iucv_accept_poll(sk);
1369 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1371 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
1373 if (sk->sk_shutdown & RCV_SHUTDOWN)
1376 if (sk->sk_shutdown == SHUTDOWN_MASK)
1379 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1380 (sk->sk_shutdown & RCV_SHUTDOWN))
1381 mask |= EPOLLIN | EPOLLRDNORM;
1383 if (sk->sk_state == IUCV_CLOSED)
1386 if (sk->sk_state == IUCV_DISCONN)
1389 if (sock_writeable(sk) && iucv_below_msglim(sk))
1390 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
1392 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1397 static int iucv_sock_shutdown(struct socket *sock, int how)
1399 struct sock *sk = sock->sk;
1400 struct iucv_sock *iucv = iucv_sk(sk);
1401 struct iucv_message txmsg;
1406 if ((how & ~SHUTDOWN_MASK) || !how)
1410 switch (sk->sk_state) {
1421 if ((how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) &&
1422 sk->sk_state == IUCV_CONNECTED) {
1423 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1426 err = pr_iucv->message_send(iucv->path, &txmsg,
1427 IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8);
1442 iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
1445 sk->sk_shutdown |= how;
1446 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
1447 if ((iucv->transport == AF_IUCV_TRANS_IUCV) &&
1449 err = pr_iucv->path_quiesce(iucv->path, NULL);
1452 /* skb_queue_purge(&sk->sk_receive_queue); */
1454 skb_queue_purge(&sk->sk_receive_queue);
1457 /* Wake up anyone sleeping in poll */
1458 sk->sk_state_change(sk);
1465 static int iucv_sock_release(struct socket *sock)
1467 struct sock *sk = sock->sk;
1473 iucv_sock_close(sk);
1480 /* getsockopt and setsockopt */
1481 static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
1482 sockptr_t optval, unsigned int optlen)
1484 struct sock *sk = sock->sk;
1485 struct iucv_sock *iucv = iucv_sk(sk);
1489 if (level != SOL_IUCV)
1490 return -ENOPROTOOPT;
1492 if (optlen < sizeof(int))
1495 if (copy_from_sockptr(&val, optval, sizeof(int)))
1502 case SO_IPRMDATA_MSG:
1504 iucv->flags |= IUCV_IPRMDATA;
1506 iucv->flags &= ~IUCV_IPRMDATA;
1509 switch (sk->sk_state) {
1512 if (val < 1 || val > U16_MAX)
1515 iucv->msglimit = val;
1531 static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1532 char __user *optval, int __user *optlen)
1534 struct sock *sk = sock->sk;
1535 struct iucv_sock *iucv = iucv_sk(sk);
1539 if (level != SOL_IUCV)
1540 return -ENOPROTOOPT;
1542 if (get_user(len, optlen))
1548 len = min_t(unsigned int, len, sizeof(int));
1551 case SO_IPRMDATA_MSG:
1552 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1556 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1557 : iucv->msglimit; /* default */
1561 if (sk->sk_state == IUCV_OPEN)
1563 val = (iucv->hs_dev) ? iucv->hs_dev->mtu -
1564 sizeof(struct af_iucv_trans_hdr) - ETH_HLEN :
1568 return -ENOPROTOOPT;
1571 if (put_user(len, optlen))
1573 if (copy_to_user(optval, &val, len))
1580 /* Callback wrappers - called from iucv base support */
1581 static int iucv_callback_connreq(struct iucv_path *path,
1582 u8 ipvmid[8], u8 ipuser[16])
1584 unsigned char user_data[16];
1585 unsigned char nuser_data[16];
1586 unsigned char src_name[8];
1587 struct sock *sk, *nsk;
1588 struct iucv_sock *iucv, *niucv;
1591 memcpy(src_name, ipuser, 8);
1592 EBCASC(src_name, 8);
1593 /* Find out if this path belongs to af_iucv. */
1594 read_lock(&iucv_sk_list.lock);
1597 sk_for_each(sk, &iucv_sk_list.head)
1598 if (sk->sk_state == IUCV_LISTEN &&
1599 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1601 * Found a listening socket with
1602 * src_name == ipuser[0-7].
1607 read_unlock(&iucv_sk_list.lock);
1609 /* No socket found, not one of our paths. */
1614 /* Check if parent socket is listening */
1615 low_nmcpy(user_data, iucv->src_name);
1616 high_nmcpy(user_data, iucv->dst_name);
1617 ASCEBC(user_data, sizeof(user_data));
1618 if (sk->sk_state != IUCV_LISTEN) {
1619 err = pr_iucv->path_sever(path, user_data);
1620 iucv_path_free(path);
1624 /* Check for backlog size */
1625 if (sk_acceptq_is_full(sk)) {
1626 err = pr_iucv->path_sever(path, user_data);
1627 iucv_path_free(path);
1631 /* Create the new socket */
1632 nsk = iucv_sock_alloc(NULL, sk->sk_protocol, GFP_ATOMIC, 0);
1634 err = pr_iucv->path_sever(path, user_data);
1635 iucv_path_free(path);
1639 niucv = iucv_sk(nsk);
1640 iucv_sock_init(nsk, sk);
1641 niucv->transport = AF_IUCV_TRANS_IUCV;
1642 nsk->sk_allocation |= GFP_DMA;
1644 /* Set the new iucv_sock */
1645 memcpy(niucv->dst_name, ipuser + 8, 8);
1646 EBCASC(niucv->dst_name, 8);
1647 memcpy(niucv->dst_user_id, ipvmid, 8);
1648 memcpy(niucv->src_name, iucv->src_name, 8);
1649 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1652 /* Call iucv_accept */
1653 high_nmcpy(nuser_data, ipuser + 8);
1654 memcpy(nuser_data + 8, niucv->src_name, 8);
1655 ASCEBC(nuser_data + 8, 8);
1657 /* set message limit for path based on msglimit of accepting socket */
1658 niucv->msglimit = iucv->msglimit;
1659 path->msglim = iucv->msglimit;
1660 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
1662 iucv_sever_path(nsk, 1);
1663 iucv_sock_kill(nsk);
1667 iucv_accept_enqueue(sk, nsk);
1669 /* Wake up accept */
1670 nsk->sk_state = IUCV_CONNECTED;
1671 sk->sk_data_ready(sk);
1678 static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1680 struct sock *sk = path->private;
1682 sk->sk_state = IUCV_CONNECTED;
1683 sk->sk_state_change(sk);
1686 static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1688 struct sock *sk = path->private;
1689 struct iucv_sock *iucv = iucv_sk(sk);
1690 struct sk_buff *skb;
1691 struct sock_msg_q *save_msg;
1694 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1695 pr_iucv->message_reject(path, msg);
1699 spin_lock(&iucv->message_q.lock);
1701 if (!list_empty(&iucv->message_q.list) ||
1702 !skb_queue_empty(&iucv->backlog_skb_q))
1705 len = atomic_read(&sk->sk_rmem_alloc);
1706 len += SKB_TRUESIZE(iucv_msg_length(msg));
1707 if (len > sk->sk_rcvbuf)
1710 skb = alloc_iucv_recv_skb(iucv_msg_length(msg));
1714 iucv_process_message(sk, skb, path, msg);
1718 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
1721 save_msg->path = path;
1722 save_msg->msg = *msg;
1724 list_add_tail(&save_msg->list, &iucv->message_q.list);
1727 spin_unlock(&iucv->message_q.lock);
1730 static void iucv_callback_txdone(struct iucv_path *path,
1731 struct iucv_message *msg)
1733 struct sock *sk = path->private;
1734 struct sk_buff *this = NULL;
1735 struct sk_buff_head *list;
1736 struct sk_buff *list_skb;
1737 struct iucv_sock *iucv;
1738 unsigned long flags;
1741 list = &iucv->send_skb_q;
1745 spin_lock_irqsave(&list->lock, flags);
1746 skb_queue_walk(list, list_skb) {
1747 if (msg->tag == IUCV_SKB_CB(list_skb)->tag) {
1753 atomic_dec(&iucv->skbs_in_xmit);
1754 __skb_unlink(this, list);
1757 spin_unlock_irqrestore(&list->lock, flags);
1761 /* wake up any process waiting for sending */
1762 iucv_sock_wake_msglim(sk);
1765 if (sk->sk_state == IUCV_CLOSING) {
1766 if (atomic_read(&iucv->skbs_in_xmit) == 0) {
1767 sk->sk_state = IUCV_CLOSED;
1768 sk->sk_state_change(sk);
1775 static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1777 struct sock *sk = path->private;
1779 if (sk->sk_state == IUCV_CLOSED)
1783 iucv_sever_path(sk, 1);
1784 sk->sk_state = IUCV_DISCONN;
1786 sk->sk_state_change(sk);
1790 /* called if the other communication side shuts down its RECV direction;
1791 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1793 static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1795 struct sock *sk = path->private;
1798 if (sk->sk_state != IUCV_CLOSED) {
1799 sk->sk_shutdown |= SEND_SHUTDOWN;
1800 sk->sk_state_change(sk);
1805 static struct iucv_handler af_iucv_handler = {
1806 .path_pending = iucv_callback_connreq,
1807 .path_complete = iucv_callback_connack,
1808 .path_severed = iucv_callback_connrej,
1809 .message_pending = iucv_callback_rx,
1810 .message_complete = iucv_callback_txdone,
1811 .path_quiesced = iucv_callback_shutdown,
1814 /***************** HiperSockets transport callbacks ********************/
1815 static void afiucv_swap_src_dest(struct sk_buff *skb)
1817 struct af_iucv_trans_hdr *trans_hdr = iucv_trans_hdr(skb);
1821 ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1822 ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1823 ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1824 ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1825 memcpy(tmpID, trans_hdr->srcUserID, 8);
1826 memcpy(tmpName, trans_hdr->srcAppName, 8);
1827 memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1828 memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1829 memcpy(trans_hdr->destUserID, tmpID, 8);
1830 memcpy(trans_hdr->destAppName, tmpName, 8);
1831 skb_push(skb, ETH_HLEN);
1832 memset(skb->data, 0, ETH_HLEN);
1836 * afiucv_hs_callback_syn - react on received SYN
1838 static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1840 struct af_iucv_trans_hdr *trans_hdr = iucv_trans_hdr(skb);
1842 struct iucv_sock *iucv, *niucv;
1847 /* no sock - connection refused */
1848 afiucv_swap_src_dest(skb);
1849 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1850 err = dev_queue_xmit(skb);
1854 nsk = iucv_sock_alloc(NULL, sk->sk_protocol, GFP_ATOMIC, 0);
1856 if ((sk->sk_state != IUCV_LISTEN) ||
1857 sk_acceptq_is_full(sk) ||
1859 /* error on server socket - connection refused */
1860 afiucv_swap_src_dest(skb);
1861 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1862 err = dev_queue_xmit(skb);
1863 iucv_sock_kill(nsk);
1868 niucv = iucv_sk(nsk);
1869 iucv_sock_init(nsk, sk);
1870 niucv->transport = AF_IUCV_TRANS_HIPER;
1871 niucv->msglimit = iucv->msglimit;
1872 if (!trans_hdr->window)
1873 niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
1875 niucv->msglimit_peer = trans_hdr->window;
1876 memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
1877 memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
1878 memcpy(niucv->src_name, iucv->src_name, 8);
1879 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1880 nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
1881 niucv->hs_dev = iucv->hs_dev;
1882 dev_hold(niucv->hs_dev);
1883 afiucv_swap_src_dest(skb);
1884 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
1885 trans_hdr->window = niucv->msglimit;
1886 /* if receiver acks the xmit connection is established */
1887 err = dev_queue_xmit(skb);
1889 iucv_accept_enqueue(sk, nsk);
1890 nsk->sk_state = IUCV_CONNECTED;
1891 sk->sk_data_ready(sk);
1893 iucv_sock_kill(nsk);
1897 return NET_RX_SUCCESS;
1901 * afiucv_hs_callback_synack() - react on received SYN-ACK
1903 static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
1905 struct iucv_sock *iucv = iucv_sk(sk);
1907 if (!iucv || sk->sk_state != IUCV_BOUND) {
1909 return NET_RX_SUCCESS;
1913 iucv->msglimit_peer = iucv_trans_hdr(skb)->window;
1914 sk->sk_state = IUCV_CONNECTED;
1915 sk->sk_state_change(sk);
1918 return NET_RX_SUCCESS;
1922 * afiucv_hs_callback_synfin() - react on received SYN_FIN
1924 static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
1926 struct iucv_sock *iucv = iucv_sk(sk);
1928 if (!iucv || sk->sk_state != IUCV_BOUND) {
1930 return NET_RX_SUCCESS;
1934 sk->sk_state = IUCV_DISCONN;
1935 sk->sk_state_change(sk);
1938 return NET_RX_SUCCESS;
1942 * afiucv_hs_callback_fin() - react on received FIN
1944 static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
1946 struct iucv_sock *iucv = iucv_sk(sk);
1948 /* other end of connection closed */
1951 return NET_RX_SUCCESS;
1955 if (sk->sk_state == IUCV_CONNECTED) {
1956 sk->sk_state = IUCV_DISCONN;
1957 sk->sk_state_change(sk);
1961 return NET_RX_SUCCESS;
1965 * afiucv_hs_callback_win() - react on received WIN
1967 static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
1969 struct iucv_sock *iucv = iucv_sk(sk);
1972 return NET_RX_SUCCESS;
1974 if (sk->sk_state != IUCV_CONNECTED)
1975 return NET_RX_SUCCESS;
1977 atomic_sub(iucv_trans_hdr(skb)->window, &iucv->msg_sent);
1978 iucv_sock_wake_msglim(sk);
1979 return NET_RX_SUCCESS;
1983 * afiucv_hs_callback_rx() - react on received data
1985 static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
1987 struct iucv_sock *iucv = iucv_sk(sk);
1991 return NET_RX_SUCCESS;
1994 if (sk->sk_state != IUCV_CONNECTED) {
1996 return NET_RX_SUCCESS;
1999 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2001 return NET_RX_SUCCESS;
2004 /* write stuff from iucv_msg to skb cb */
2005 skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2006 skb_reset_transport_header(skb);
2007 skb_reset_network_header(skb);
2008 IUCV_SKB_CB(skb)->offset = 0;
2009 if (sk_filter(sk, skb)) {
2010 atomic_inc(&sk->sk_drops); /* skb rejected by filter */
2012 return NET_RX_SUCCESS;
2015 spin_lock(&iucv->message_q.lock);
2016 if (skb_queue_empty(&iucv->backlog_skb_q)) {
2017 if (__sock_queue_rcv_skb(sk, skb))
2018 /* handle rcv queue full */
2019 skb_queue_tail(&iucv->backlog_skb_q, skb);
2021 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2022 spin_unlock(&iucv->message_q.lock);
2023 return NET_RX_SUCCESS;
2027 * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2029 * called from netif RX softirq
2031 static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2032 struct packet_type *pt, struct net_device *orig_dev)
2035 struct iucv_sock *iucv;
2036 struct af_iucv_trans_hdr *trans_hdr;
2037 int err = NET_RX_SUCCESS;
2040 if (!pskb_may_pull(skb, sizeof(*trans_hdr))) {
2042 return NET_RX_SUCCESS;
2045 trans_hdr = iucv_trans_hdr(skb);
2046 EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2047 EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2048 EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2049 EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2050 memset(nullstring, 0, sizeof(nullstring));
2053 read_lock(&iucv_sk_list.lock);
2054 sk_for_each(sk, &iucv_sk_list.head) {
2055 if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2056 if ((!memcmp(&iucv_sk(sk)->src_name,
2057 trans_hdr->destAppName, 8)) &&
2058 (!memcmp(&iucv_sk(sk)->src_user_id,
2059 trans_hdr->destUserID, 8)) &&
2060 (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2061 (!memcmp(&iucv_sk(sk)->dst_user_id,
2067 if ((!memcmp(&iucv_sk(sk)->src_name,
2068 trans_hdr->destAppName, 8)) &&
2069 (!memcmp(&iucv_sk(sk)->src_user_id,
2070 trans_hdr->destUserID, 8)) &&
2071 (!memcmp(&iucv_sk(sk)->dst_name,
2072 trans_hdr->srcAppName, 8)) &&
2073 (!memcmp(&iucv_sk(sk)->dst_user_id,
2074 trans_hdr->srcUserID, 8))) {
2080 read_unlock(&iucv_sk_list.lock);
2085 how should we send with no sock
2086 1) send without sock no send rc checking?
2087 2) introduce default sock to handle this cases
2089 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2091 SYN|ACK, SYN|FIN, FIN -> no action? */
2093 switch (trans_hdr->flags) {
2094 case AF_IUCV_FLAG_SYN:
2095 /* connect request */
2096 err = afiucv_hs_callback_syn(sk, skb);
2098 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2099 /* connect request confirmed */
2100 err = afiucv_hs_callback_synack(sk, skb);
2102 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2103 /* connect request refused */
2104 err = afiucv_hs_callback_synfin(sk, skb);
2106 case (AF_IUCV_FLAG_FIN):
2108 err = afiucv_hs_callback_fin(sk, skb);
2110 case (AF_IUCV_FLAG_WIN):
2111 err = afiucv_hs_callback_win(sk, skb);
2112 if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
2116 fallthrough; /* and receive non-zero length data */
2117 case (AF_IUCV_FLAG_SHT):
2118 /* shutdown request */
2119 fallthrough; /* and receive zero length data */
2121 /* plain data frame */
2122 IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
2123 err = afiucv_hs_callback_rx(sk, skb);
2133 * afiucv_hs_callback_txnotify() - handle send notifications from HiperSockets
2136 static void afiucv_hs_callback_txnotify(struct sock *sk, enum iucv_tx_notify n)
2138 struct iucv_sock *iucv = iucv_sk(sk);
2140 if (sock_flag(sk, SOCK_ZAPPED))
2145 atomic_dec(&iucv->skbs_in_xmit);
2146 iucv_sock_wake_msglim(sk);
2148 case TX_NOTIFY_PENDING:
2149 atomic_inc(&iucv->pendings);
2151 case TX_NOTIFY_DELAYED_OK:
2152 atomic_dec(&iucv->skbs_in_xmit);
2153 if (atomic_dec_return(&iucv->pendings) <= 0)
2154 iucv_sock_wake_msglim(sk);
2157 atomic_dec(&iucv->skbs_in_xmit);
2158 if (sk->sk_state == IUCV_CONNECTED) {
2159 sk->sk_state = IUCV_DISCONN;
2160 sk->sk_state_change(sk);
2164 if (sk->sk_state == IUCV_CLOSING) {
2165 if (atomic_read(&iucv->skbs_in_xmit) == 0) {
2166 sk->sk_state = IUCV_CLOSED;
2167 sk->sk_state_change(sk);
2173 * afiucv_netdev_event: handle netdev notifier chain events
2175 static int afiucv_netdev_event(struct notifier_block *this,
2176 unsigned long event, void *ptr)
2178 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2180 struct iucv_sock *iucv;
2184 case NETDEV_GOING_DOWN:
2185 sk_for_each(sk, &iucv_sk_list.head) {
2187 if ((iucv->hs_dev == event_dev) &&
2188 (sk->sk_state == IUCV_CONNECTED)) {
2189 if (event == NETDEV_GOING_DOWN)
2190 iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
2191 sk->sk_state = IUCV_DISCONN;
2192 sk->sk_state_change(sk);
2197 case NETDEV_UNREGISTER:
2204 static struct notifier_block afiucv_netdev_notifier = {
2205 .notifier_call = afiucv_netdev_event,
2208 static const struct proto_ops iucv_sock_ops = {
2210 .owner = THIS_MODULE,
2211 .release = iucv_sock_release,
2212 .bind = iucv_sock_bind,
2213 .connect = iucv_sock_connect,
2214 .listen = iucv_sock_listen,
2215 .accept = iucv_sock_accept,
2216 .getname = iucv_sock_getname,
2217 .sendmsg = iucv_sock_sendmsg,
2218 .recvmsg = iucv_sock_recvmsg,
2219 .poll = iucv_sock_poll,
2220 .ioctl = sock_no_ioctl,
2221 .mmap = sock_no_mmap,
2222 .socketpair = sock_no_socketpair,
2223 .shutdown = iucv_sock_shutdown,
2224 .setsockopt = iucv_sock_setsockopt,
2225 .getsockopt = iucv_sock_getsockopt,
2228 static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
2233 if (protocol && protocol != PF_IUCV)
2234 return -EPROTONOSUPPORT;
2236 sock->state = SS_UNCONNECTED;
2238 switch (sock->type) {
2240 case SOCK_SEQPACKET:
2241 /* currently, proto ops can handle both sk types */
2242 sock->ops = &iucv_sock_ops;
2245 return -ESOCKTNOSUPPORT;
2248 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL, kern);
2252 iucv_sock_init(sk, NULL);
2257 static const struct net_proto_family iucv_sock_family_ops = {
2259 .owner = THIS_MODULE,
2260 .create = iucv_sock_create,
2263 static struct packet_type iucv_packet_type = {
2264 .type = cpu_to_be16(ETH_P_AF_IUCV),
2265 .func = afiucv_hs_rcv,
2268 static int __init afiucv_init(void)
2272 if (MACHINE_IS_VM && IS_ENABLED(CONFIG_IUCV)) {
2273 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2274 if (unlikely(err)) {
2276 err = -EPROTONOSUPPORT;
2282 memset(&iucv_userid, 0, sizeof(iucv_userid));
2286 err = proto_register(&iucv_proto, 0);
2289 err = sock_register(&iucv_sock_family_ops);
2294 err = pr_iucv->iucv_register(&af_iucv_handler, 0);
2299 err = register_netdevice_notifier(&afiucv_netdev_notifier);
2303 dev_add_pack(&iucv_packet_type);
2308 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2310 sock_unregister(PF_IUCV);
2312 proto_unregister(&iucv_proto);
2317 static void __exit afiucv_exit(void)
2320 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2322 unregister_netdevice_notifier(&afiucv_netdev_notifier);
2323 dev_remove_pack(&iucv_packet_type);
2324 sock_unregister(PF_IUCV);
2325 proto_unregister(&iucv_proto);
2328 module_init(afiucv_init);
2329 module_exit(afiucv_exit);
2331 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2332 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2333 MODULE_VERSION(VERSION);
2334 MODULE_LICENSE("GPL");
2335 MODULE_ALIAS_NETPROTO(PF_IUCV);