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