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