blob: 6ee9851ac7c6801f4918398ec14d44ccf2f84c1d [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Jennifer Hunteac37312007-02-08 13:51:54 -08002/*
Jennifer Hunteac37312007-02-08 13:51:54 -08003 * IUCV protocol stack for Linux on zSeries
4 *
Ursula Braunc23cad92009-06-16 10:30:44 +02005 * Copyright IBM Corp. 2006, 2009
Jennifer Hunteac37312007-02-08 13:51:54 -08006 *
7 * Author(s): Jennifer Hunt <jenhunt@us.ibm.com>
Ursula Braunc23cad92009-06-16 10:30:44 +02008 * Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
9 * PM functions:
10 * Ursula Braun <ursula.braun@de.ibm.com>
Jennifer Hunteac37312007-02-08 13:51:54 -080011 */
12
Ursula Braun8f7c5022008-12-25 13:39:47 +010013#define KMSG_COMPONENT "af_iucv"
14#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
Jennifer Hunteac37312007-02-08 13:51:54 -080016#include <linux/module.h>
Julian Wiedmann238965b2019-06-18 20:43:00 +020017#include <linux/netdevice.h>
Jennifer Hunteac37312007-02-08 13:51:54 -080018#include <linux/types.h>
Julian Wiedmann0d1c7662020-05-19 21:10:10 +020019#include <linux/limits.h>
Jennifer Hunteac37312007-02-08 13:51:54 -080020#include <linux/list.h>
21#include <linux/errno.h>
22#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010023#include <linux/sched/signal.h>
Jennifer Hunteac37312007-02-08 13:51:54 -080024#include <linux/slab.h>
25#include <linux/skbuff.h>
26#include <linux/init.h>
27#include <linux/poll.h>
Paul Moore02f06912016-06-07 10:30:53 -040028#include <linux/security.h>
Jennifer Hunteac37312007-02-08 13:51:54 -080029#include <net/sock.h>
30#include <asm/ebcdic.h>
31#include <asm/cpcmd.h>
32#include <linux/kmod.h>
33
Jennifer Hunteac37312007-02-08 13:51:54 -080034#include <net/iucv/af_iucv.h>
35
Ursula Braun3881ac42011-08-08 01:33:54 +000036#define VERSION "1.2"
Jennifer Hunteac37312007-02-08 13:51:54 -080037
38static char iucv_userid[80];
39
Jennifer Hunteac37312007-02-08 13:51:54 -080040static struct proto iucv_proto = {
41 .name = "AF_IUCV",
42 .owner = THIS_MODULE,
43 .obj_size = sizeof(struct iucv_sock),
44};
45
Frank Blaschka6fcd61f2011-08-08 01:33:51 +000046static struct iucv_interface *pr_iucv;
47
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +000048/* special AF_IUCV IPRM messages */
49static const u8 iprm_shutdown[8] =
50 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
51
Pankaj Bharadiyac5936422019-12-09 10:31:43 -080052#define TRGCLS_SIZE sizeof_field(struct iucv_message, class)
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000053
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000054#define __iucv_sock_wait(sk, condition, timeo, ret) \
55do { \
56 DEFINE_WAIT(__wait); \
57 long __timeo = timeo; \
58 ret = 0; \
Eric Dumazetaa395142010-04-20 13:03:51 +000059 prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000060 while (!(condition)) { \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000061 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 Dumazetaa395142010-04-20 13:03:51 +000076 finish_wait(sk_sleep(sk), &__wait); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000077} 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 Brueckner44b1e6b2009-04-21 23:26:24 +000086
Julian Wiedmanne9a36ca2020-05-19 21:10:12 +020087static struct sock *iucv_accept_dequeue(struct sock *parent,
88 struct socket *newsock);
Heiko Carstens57f20442007-10-08 02:02:52 -070089static void iucv_sock_kill(struct sock *sk);
90static void iucv_sock_close(struct sock *sk);
91
Ursula Braun3881ac42011-08-08 01:33:54 +000092static void afiucv_hs_callback_txnotify(struct sk_buff *, enum iucv_tx_notify);
93
Jennifer Hunteac37312007-02-08 13:51:54 -080094/* Call Back functions */
95static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
96static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
Ursula Braun91e60eb2015-09-18 16:06:52 +020097static void iucv_callback_connack(struct iucv_path *, u8 *);
98static int iucv_callback_connreq(struct iucv_path *, u8 *, u8 *);
99static void iucv_callback_connrej(struct iucv_path *, u8 *);
100static void iucv_callback_shutdown(struct iucv_path *, u8 *);
Jennifer Hunteac37312007-02-08 13:51:54 -0800101
102static struct iucv_sock_list iucv_sk_list = {
Robert P. J. Day3db8ce32008-04-10 02:11:24 -0700103 .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
Jennifer Hunteac37312007-02-08 13:51:54 -0800104 .autobind_name = ATOMIC_INIT(0)
105};
106
107static 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 Brueckneraf88b522009-04-21 23:26:21 +0000112 .message_complete = iucv_callback_txdone,
113 .path_quiesced = iucv_callback_shutdown,
Jennifer Hunteac37312007-02-08 13:51:54 -0800114};
115
116static inline void high_nmcpy(unsigned char *dst, char *src)
117{
118 memcpy(dst, src, 8);
119}
120
121static inline void low_nmcpy(unsigned char *dst, char *src)
122{
123 memcpy(&dst[8], src, 8);
124}
125
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000126/**
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 Marchi25985ed2011-03-30 22:57:33 -0300138 * The socket data length is computed by subtracting the socket data length
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000139 * 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 */
146static 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 Brueckner0ea920d2009-06-17 21:54:48 +0000157/**
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 */
165static 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 */
178static 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 Braun3881ac42011-08-08 01:33:54 +0000184 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 Brueckner0ea920d2009-06-17 21:54:48 +0000189}
190
191/**
192 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
193 */
194static void iucv_sock_wake_msglim(struct sock *sk)
195{
Eric Dumazet43815482010-04-29 11:01:49 +0000196 struct socket_wq *wq;
197
198 rcu_read_lock();
199 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +0800200 if (skwq_has_sleeper(wq))
Eric Dumazet43815482010-04-29 11:01:49 +0000201 wake_up_interruptible_all(&wq->wait);
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000202 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Eric Dumazet43815482010-04-29 11:01:49 +0000203 rcu_read_unlock();
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000204}
205
Ursula Braun3881ac42011-08-08 01:33:54 +0000206/**
207 * afiucv_hs_send() - send a message through HiperSockets transport
208 */
209static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
210 struct sk_buff *skb, u8 flags)
211{
Ursula Braun3881ac42011-08-08 01:33:54 +0000212 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 Wiedmanncd11d112018-09-26 18:29:06 +0200217 phs_hdr = skb_push(skb, sizeof(*phs_hdr));
218 memset(phs_hdr, 0, sizeof(*phs_hdr));
Ursula Braun3881ac42011-08-08 01:33:54 +0000219 skb_reset_network_header(skb);
Ursula Braun3881ac42011-08-08 01:33:54 +0000220
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 Braun800c5eb2012-02-08 00:19:47 +0000243 skb->dev = iucv->hs_dev;
Julian Wiedmannb2f54392018-09-05 16:55:11 +0200244 if (!skb->dev) {
245 err = -ENODEV;
246 goto err_free;
247 }
Julian Wiedmann238965b2019-06-18 20:43:00 +0200248
249 dev_hard_header(skb, skb->dev, ETH_P_AF_IUCV, NULL, NULL, skb->len);
250
Julian Wiedmannb2f54392018-09-05 16:55:11 +0200251 if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev)) {
252 err = -ENETDOWN;
253 goto err_free;
254 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000255 if (skb->len > skb->dev->mtu) {
Julian Wiedmannb2f54392018-09-05 16:55:11 +0200256 if (sock->sk_type == SOCK_SEQPACKET) {
257 err = -EMSGSIZE;
258 goto err_free;
259 }
260 skb_trim(skb, skb->dev->mtu);
Ursula Braun3881ac42011-08-08 01:33:54 +0000261 }
Hans Wippel2e56c262017-06-23 19:32:27 +0200262 skb->protocol = cpu_to_be16(ETH_P_AF_IUCV);
Julian Wiedmann238965b2019-06-18 20:43:00 +0200263
264 __skb_header_release(skb);
Ursula Braun3881ac42011-08-08 01:33:54 +0000265 nskb = skb_clone(skb, GFP_ATOMIC);
Julian Wiedmannb2f54392018-09-05 16:55:11 +0200266 if (!nskb) {
267 err = -ENOMEM;
268 goto err_free;
269 }
270
Ursula Braun3881ac42011-08-08 01:33:54 +0000271 skb_queue_tail(&iucv->send_skb_q, nskb);
272 err = dev_queue_xmit(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +0000273 if (net_xmit_eval(err)) {
Ursula Braun3881ac42011-08-08 01:33:54 +0000274 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 Braun800c5eb2012-02-08 00:19:47 +0000280 return net_xmit_eval(err);
Julian Wiedmannb2f54392018-09-05 16:55:11 +0200281
282err_free:
283 kfree_skb(skb);
284 return err;
Ursula Braun3881ac42011-08-08 01:33:54 +0000285}
286
Jennifer Hunteac37312007-02-08 13:51:54 -0800287static struct sock *__iucv_get_sock_by_name(char *nm)
288{
289 struct sock *sk;
Jennifer Hunteac37312007-02-08 13:51:54 -0800290
Sasha Levinb67bfe02013-02-27 17:06:00 -0800291 sk_for_each(sk, &iucv_sk_list.head)
Jennifer Hunteac37312007-02-08 13:51:54 -0800292 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
293 return sk;
294
295 return NULL;
296}
297
298static void iucv_sock_destruct(struct sock *sk)
299{
300 skb_queue_purge(&sk->sk_receive_queue);
Ursula Braun82492a32012-03-07 02:06:24 +0000301 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. Millerb2c9c5d2017-07-03 02:31:22 -0700311 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
Ursula Braun82492a32012-03-07 02:06:24 +0000312 WARN_ON(sk->sk_wmem_queued);
313 WARN_ON(sk->sk_forward_alloc);
Jennifer Hunteac37312007-02-08 13:51:54 -0800314}
315
316/* Cleanup Listen */
317static 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 Hunteac37312007-02-08 13:51:54 -0800328}
329
Julian Wiedmanne9a36ca2020-05-19 21:10:12 +0200330static 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
337static 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 Brueckner7514bab2009-09-16 04:37:27 +0000344/* Kill socket (only if zapped and orphaned) */
Jennifer Hunteac37312007-02-08 13:51:54 -0800345static 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 Braun7d316b92012-02-08 00:19:45 +0000355/* Terminate an IUCV path */
356static 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 Braun4e0ad322016-10-07 15:51:47 +0200375/* Send controlling flags through an IUCV socket for HIPER transport */
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000376static int iucv_send_ctrl(struct sock *sk, u8 flags)
377{
Julian Wiedmann238965b2019-06-18 20:43:00 +0200378 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000379 int err = 0;
380 int blen;
381 struct sk_buff *skb;
Ursula Braun4e0ad322016-10-07 15:51:47 +0200382 u8 shutdown = 0;
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000383
Julian Wiedmann238965b2019-06-18 20:43:00 +0200384 blen = sizeof(struct af_iucv_trans_hdr) +
385 LL_RESERVED_SPACE(iucv->hs_dev);
Ursula Braun4e0ad322016-10-07 15:51:47 +0200386 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 Braun9fbd87d2012-03-07 02:06:23 +0000391 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 Braun4e0ad322016-10-07 15:51:47 +0200396 if (shutdown)
397 sk->sk_shutdown = shutdown;
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000398 return err;
399}
400
Jennifer Hunteac37312007-02-08 13:51:54 -0800401/* Close an IUCV socket */
402static void iucv_sock_close(struct sock *sk)
403{
Jennifer Hunteac37312007-02-08 13:51:54 -0800404 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700405 unsigned long timeo;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000406 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -0800407
Jennifer Hunteac37312007-02-08 13:51:54 -0800408 lock_sock(sk);
409
Heiko Carstensda99f052007-05-04 12:23:27 -0700410 switch (sk->sk_state) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800411 case IUCV_LISTEN:
412 iucv_sock_cleanup_listen(sk);
413 break;
414
415 case IUCV_CONNECTED:
Ursula Braun3881ac42011-08-08 01:33:54 +0000416 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000417 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000418 sk->sk_state = IUCV_DISCONN;
419 sk->sk_state_change(sk);
420 }
Gustavo A. R. Silva05bba1e2019-07-29 09:59:47 -0500421 /* fall through */
422
423 case IUCV_DISCONN:
Jennifer Hunt561e0362007-05-04 12:22:07 -0700424 sk->sk_state = IUCV_CLOSING;
425 sk->sk_state_change(sk);
426
Ursula Braun800c5eb2012-02-08 00:19:47 +0000427 if (!err && !skb_queue_empty(&iucv->send_skb_q)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -0700428 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
429 timeo = sk->sk_lingertime;
430 else
431 timeo = IUCV_DISCONN_TIMEOUT;
Ursula Braun9f6298a2011-05-12 18:45:08 +0000432 iucv_sock_wait(sk,
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000433 iucv_sock_in_state(sk, IUCV_CLOSED, 0),
434 timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700435 }
Gustavo A. R. Silva05bba1e2019-07-29 09:59:47 -0500436 /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700437
Gustavo A. R. Silva05bba1e2019-07-29 09:59:47 -0500438 case IUCV_CLOSING:
Jennifer Hunt561e0362007-05-04 12:22:07 -0700439 sk->sk_state = IUCV_CLOSED;
440 sk->sk_state_change(sk);
441
Jennifer Hunteac37312007-02-08 13:51:54 -0800442 sk->sk_err = ECONNRESET;
443 sk->sk_state_change(sk);
444
Ursula Braun800c5eb2012-02-08 00:19:47 +0000445 skb_queue_purge(&iucv->send_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700446 skb_queue_purge(&iucv->backlog_skb_q);
Gustavo A. R. Silva05bba1e2019-07-29 09:59:47 -0500447 /* fall through */
Jennifer Hunteac37312007-02-08 13:51:54 -0800448
Gustavo A. R. Silva05bba1e2019-07-29 09:59:47 -0500449 default:
Ursula Braun7d316b92012-02-08 00:19:45 +0000450 iucv_sever_path(sk, 1);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700451 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800452
Ursula Braun800c5eb2012-02-08 00:19:47 +0000453 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 Brueckner7514bab2009-09-16 04:37:27 +0000459 /* mark socket for deletion by iucv_sock_kill() */
460 sock_set_flag(sk, SOCK_ZAPPED);
461
Jennifer Hunteac37312007-02-08 13:51:54 -0800462 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800463}
464
465static void iucv_sock_init(struct sock *sk, struct sock *parent)
466{
Paul Moore02f06912016-06-07 10:30:53 -0400467 if (parent) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800468 sk->sk_type = parent->sk_type;
Paul Moore02f06912016-06-07 10:30:53 -0400469 security_sk_clone(parent, sk);
470 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800471}
472
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500473static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio, int kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800474{
475 struct sock *sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000476 struct iucv_sock *iucv;
Jennifer Hunteac37312007-02-08 13:51:54 -0800477
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500478 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, kern);
Jennifer Hunteac37312007-02-08 13:51:54 -0800479 if (!sk)
480 return NULL;
Ursula Braun493d3972011-08-08 01:33:52 +0000481 iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800482
483 sock_init_data(sock, sk);
Ursula Braun493d3972011-08-08 01:33:52 +0000484 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 Braun3881ac42011-08-08 01:33:54 +0000491 atomic_set(&iucv->pendings, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000492 iucv->flags = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000493 iucv->msglimit = 0;
494 atomic_set(&iucv->msg_sent, 0);
495 atomic_set(&iucv->msg_recv, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000496 iucv->path = NULL;
Ursula Braun3881ac42011-08-08 01:33:54 +0000497 iucv->sk_txnotify = afiucv_hs_callback_txnotify;
Ursula Braun493d3972011-08-08 01:33:52 +0000498 memset(&iucv->src_user_id , 0, 32);
Ursula Braun3881ac42011-08-08 01:33:54 +0000499 if (pr_iucv)
500 iucv->transport = AF_IUCV_TRANS_IUCV;
501 else
502 iucv->transport = AF_IUCV_TRANS_HIPER;
Jennifer Hunteac37312007-02-08 13:51:54 -0800503
504 sk->sk_destruct = iucv_sock_destruct;
505 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800506
507 sock_reset_flag(sk, SOCK_ZAPPED);
508
509 sk->sk_protocol = proto;
510 sk->sk_state = IUCV_OPEN;
511
Jennifer Hunteac37312007-02-08 13:51:54 -0800512 iucv_sock_link(&iucv_sk_list, sk);
513 return sk;
514}
515
Julian Wiedmanne9a36ca2020-05-19 21:10:12 +0200516static void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
Jennifer Hunteac37312007-02-08 13:51:54 -0800517{
Ursula Braunfebca282007-07-14 19:04:25 -0700518 unsigned long flags;
519 struct iucv_sock *par = iucv_sk(parent);
520
Jennifer Hunteac37312007-02-08 13:51:54 -0800521 sock_hold(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700522 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 Hunteac37312007-02-08 13:51:54 -0800525 iucv_sk(sk)->parent = parent;
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000526 sk_acceptq_added(parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800527}
528
Julian Wiedmanne9a36ca2020-05-19 21:10:12 +0200529static void iucv_accept_unlink(struct sock *sk)
Jennifer Hunteac37312007-02-08 13:51:54 -0800530{
Ursula Braunfebca282007-07-14 19:04:25 -0700531 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 Hunteac37312007-02-08 13:51:54 -0800535 list_del_init(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700536 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000537 sk_acceptq_removed(iucv_sk(sk)->parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800538 iucv_sk(sk)->parent = NULL;
539 sock_put(sk);
540}
541
Julian Wiedmanne9a36ca2020-05-19 21:10:12 +0200542static struct sock *iucv_accept_dequeue(struct sock *parent,
543 struct socket *newsock)
Jennifer Hunteac37312007-02-08 13:51:54 -0800544{
545 struct iucv_sock *isk, *n;
546 struct sock *sk;
547
Heiko Carstensda99f052007-05-04 12:23:27 -0700548 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800549 sk = (struct sock *) isk;
550 lock_sock(sk);
551
552 if (sk->sk_state == IUCV_CLOSED) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800553 iucv_accept_unlink(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700554 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800555 continue;
556 }
557
558 if (sk->sk_state == IUCV_CONNECTED ||
Ursula Braunaac63992011-12-19 22:56:31 +0000559 sk->sk_state == IUCV_DISCONN ||
Jennifer Hunteac37312007-02-08 13:51:54 -0800560 !newsock) {
561 iucv_accept_unlink(sk);
562 if (newsock)
563 sock_graft(sk, newsock);
564
Jennifer Hunteac37312007-02-08 13:51:54 -0800565 release_sock(sk);
566 return sk;
567 }
568
569 release_sock(sk);
570 }
571 return NULL;
572}
573
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200574static 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 Hunteac37312007-02-08 13:51:54 -0800586/* Bind an unbound socket */
587static 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 Braun3881ac42011-08-08 01:33:54 +0000593 int err = 0;
594 struct net_device *dev;
595 char uid[9];
Jennifer Hunteac37312007-02-08 13:51:54 -0800596
597 /* Verify the input sockaddr */
Mateusz Jurczyke3c42b62017-06-23 19:32:28 +0200598 if (addr_len < sizeof(struct sockaddr_iucv) ||
599 addr->sa_family != AF_IUCV)
Ursula Braun52a82e22016-01-19 10:41:33 +0100600 return -EINVAL;
601
Jennifer Hunteac37312007-02-08 13:51:54 -0800602 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 Braun3881ac42011-08-08 01:33:54 +0000615 if (iucv->path)
Jennifer Hunteac37312007-02-08 13:51:54 -0800616 goto done_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -0800617
618 /* Bind the socket */
Ursula Braun3881ac42011-08-08 01:33:54 +0000619 if (pr_iucv)
620 if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
621 goto vm_bind; /* VM IUCV transport */
Jennifer Hunteac37312007-02-08 13:51:54 -0800622
Ursula Braun3881ac42011-08-08 01:33:54 +0000623 /* 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 Braun3881ac42011-08-08 01:33:54 +0000629 memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200630 /* 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 Braun816abba2011-12-19 22:56:29 +0000635 sk->sk_bound_dev_if = dev->ifindex;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000636 iucv->hs_dev = dev;
637 dev_hold(dev);
Ursula Braun3881ac42011-08-08 01:33:54 +0000638 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();
647vm_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 Wiedmannfdbf6322019-06-18 20:42:59 +0200654 sk->sk_allocation |= GFP_DMA;
Ursula Braun3881ac42011-08-08 01:33:54 +0000655 if (!iucv->msglimit)
656 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
657 goto done_unlock;
658 }
659 /* found no dev to bind */
660 err = -ENODEV;
Jennifer Hunteac37312007-02-08 13:51:54 -0800661done_unlock:
662 /* Release the socket list lock */
663 write_unlock_bh(&iucv_sk_list.lock);
664done:
665 release_sock(sk);
666 return err;
667}
668
669/* Automatically bind an unbound socket */
670static int iucv_sock_autobind(struct sock *sk)
671{
672 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800673 int err = 0;
674
Ursula Braunaac63992011-12-19 22:56:31 +0000675 if (unlikely(!pr_iucv))
Jennifer Hunteac37312007-02-08 13:51:54 -0800676 return -EPROTO;
677
Ursula Braunaac63992011-12-19 22:56:31 +0000678 memcpy(iucv->src_user_id, iucv_userid, 8);
Julian Wiedmannfdbf6322019-06-18 20:42:59 +0200679 iucv->transport = AF_IUCV_TRANS_IUCV;
680 sk->sk_allocation |= GFP_DMA;
Jennifer Hunteac37312007-02-08 13:51:54 -0800681
682 write_lock_bh(&iucv_sk_list.lock);
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200683 __iucv_auto_name(iucv);
Jennifer Hunteac37312007-02-08 13:51:54 -0800684 write_unlock_bh(&iucv_sk_list.lock);
685
Ursula Braun3881ac42011-08-08 01:33:54 +0000686 if (!iucv->msglimit)
687 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
688
Jennifer Hunteac37312007-02-08 13:51:54 -0800689 return err;
690}
691
Ursula Braun3881ac42011-08-08 01:33:54 +0000692static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
Jennifer Hunteac37312007-02-08 13:51:54 -0800693{
694 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
695 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000696 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800697 unsigned char user_data[16];
698 int err;
699
Jennifer Hunteac37312007-02-08 13:51:54 -0800700 high_nmcpy(user_data, sa->siucv_name);
Ursula Braun493d3972011-08-08 01:33:52 +0000701 low_nmcpy(user_data, iucv->src_name);
Jennifer Hunteac37312007-02-08 13:51:54 -0800702 ASCEBC(user_data, sizeof(user_data));
703
Jennifer Hunteac37312007-02-08 13:51:54 -0800704 /* Create path. */
Hendrik Brueckner09488e22009-04-21 23:26:27 +0000705 iucv->path = iucv_path_alloc(iucv->msglimit,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000706 IUCV_IPRMDATA, GFP_KERNEL);
Ursula Braund4444722008-02-07 18:07:19 -0800707 if (!iucv->path) {
708 err = -ENOMEM;
709 goto done;
710 }
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000711 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
712 sa->siucv_user_id, NULL, user_data,
713 sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800714 if (err) {
715 iucv_path_free(iucv->path);
716 iucv->path = NULL;
Hendrik Brueckner55cdea92009-01-05 18:07:07 -0800717 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 Braun3881ac42011-08-08 01:33:54 +0000732 }
733done:
734 return err;
735}
736
737/* Connect an unconnected socket */
738static 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 Jurczyke3c42b62017-06-23 19:32:28 +0200746 if (alen < sizeof(struct sockaddr_iucv) || addr->sa_family != AF_IUCV)
Ursula Braun3881ac42011-08-08 01:33:54 +0000747 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 Hunteac37312007-02-08 13:51:54 -0800763 }
764
Ursula Braun3881ac42011-08-08 01:33:54 +0000765 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 Braun9fbd87d2012-03-07 02:06:23 +0000772 err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000773 else
774 err = afiucv_path_connect(sock, addr);
775 if (err)
776 goto done;
777
778 if (sk->sk_state != IUCV_CONNECTED)
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000779 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
780 IUCV_DISCONN),
781 sock_sndtimeo(sk, flags & O_NONBLOCK));
Jennifer Hunteac37312007-02-08 13:51:54 -0800782
Ursula Braun3881ac42011-08-08 01:33:54 +0000783 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000784 err = -ECONNREFUSED;
Ursula Braun18becbc2009-01-05 18:07:46 -0800785
Ursula Braun7d316b92012-02-08 00:19:45 +0000786 if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
787 iucv_sever_path(sk, 0);
Ursula Braun18becbc2009-01-05 18:07:46 -0800788
Jennifer Hunteac37312007-02-08 13:51:54 -0800789done:
790 release_sock(sk);
791 return err;
792}
793
794/* Move a socket into listening state. */
795static 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 Brueckneraa8e71f2009-04-21 23:26:25 +0000803 if (sk->sk_state != IUCV_BOUND)
804 goto done;
805
806 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800807 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
814done:
815 release_sock(sk);
816 return err;
817}
818
819/* Accept a pending connection */
820static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
David Howellscdfbabf2017-03-09 08:09:05 +0000821 int flags, bool kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800822{
823 DECLARE_WAITQUEUE(wait, current);
824 struct sock *sk = sock->sk, *nsk;
825 long timeo;
826 int err = 0;
827
Jennifer Hunt561e0362007-05-04 12:22:07 -0700828 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800829
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 Dumazetaa395142010-04-20 13:03:51 +0000838 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Heiko Carstensda99f052007-05-04 12:23:27 -0700839 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800840 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 Hunt561e0362007-05-04 12:22:07 -0700848 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800849
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 Dumazetaa395142010-04-20 13:03:51 +0000862 remove_wait_queue(sk_sleep(sk), &wait);
Jennifer Hunteac37312007-02-08 13:51:54 -0800863
864 if (err)
865 goto done;
866
867 newsock->state = SS_CONNECTED;
868
869done:
870 release_sock(sk);
871 return err;
872}
873
874static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100875 int peer)
Jennifer Hunteac37312007-02-08 13:51:54 -0800876{
877 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
878 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000879 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800880
881 addr->sa_family = AF_IUCV;
Jennifer Hunteac37312007-02-08 13:51:54 -0800882
883 if (peer) {
Ursula Braun493d3972011-08-08 01:33:52 +0000884 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
885 memcpy(siucv->siucv_name, iucv->dst_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800886 } else {
Ursula Braun493d3972011-08-08 01:33:52 +0000887 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
888 memcpy(siucv->siucv_name, iucv->src_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800889 }
890 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
891 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
Ursula Braun493d3972011-08-08 01:33:52 +0000892 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
Jennifer Hunteac37312007-02-08 13:51:54 -0800893
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100894 return sizeof(struct sockaddr_iucv);
Jennifer Hunteac37312007-02-08 13:51:54 -0800895}
896
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000897/**
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 */
910static 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 Blaschka6fcd61f2011-08-08 01:33:51 +0000917 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000918 (void *) prmdata, 8);
919}
920
Ying Xue1b784142015-03-02 15:37:48 +0800921static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg,
922 size_t len)
Jennifer Hunteac37312007-02-08 13:51:54 -0800923{
924 struct sock *sk = sock->sk;
925 struct iucv_sock *iucv = iucv_sk(sk);
Julian Wiedmanndc5367b2017-01-10 17:10:34 +0100926 size_t headroom = 0;
927 size_t linear;
Jennifer Hunteac37312007-02-08 13:51:54 -0800928 struct sk_buff *skb;
Eugene Crosser0506eb02015-12-11 12:27:50 +0100929 struct iucv_message txmsg = {0};
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000930 struct cmsghdr *cmsg;
931 int cmsg_done;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000932 long timeo;
Ursula Braun8f7c5022008-12-25 13:39:47 +0100933 char user_id[9];
934 char appl_id[9];
Jennifer Hunteac37312007-02-08 13:51:54 -0800935 int err;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000936 int noblock = msg->msg_flags & MSG_DONTWAIT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800937
938 err = sock_error(sk);
939 if (err)
940 return err;
941
942 if (msg->msg_flags & MSG_OOB)
943 return -EOPNOTSUPP;
944
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000945 /* 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 Hunteac37312007-02-08 13:51:54 -0800949 lock_sock(sk);
950
951 if (sk->sk_shutdown & SEND_SHUTDOWN) {
952 err = -EPIPE;
953 goto out;
954 }
955
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000956 /* Return if the socket is not in connected state */
957 if (sk->sk_state != IUCV_CONNECTED) {
958 err = -ENOTCONN;
959 goto out;
960 }
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000961
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000962 /* initialize defaults */
963 cmsg_done = 0; /* check for duplicate headers */
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000964
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000965 /* iterate over control messages */
Gu Zhengf95b4142014-12-11 11:22:04 +0800966 for_each_cmsghdr(cmsg, msg) {
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000967 if (!CMSG_OK(msg, cmsg)) {
968 err = -EINVAL;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700969 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -0800970 }
971
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000972 if (cmsg->cmsg_level != SOL_IUCV)
973 continue;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000974
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000975 if (cmsg->cmsg_type & cmsg_done) {
976 err = -EINVAL;
977 goto out;
978 }
979 cmsg_done |= cmsg->cmsg_type;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000980
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000981 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 Bruecknerb8942e32009-04-21 23:26:23 +0000986 }
987
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000988 /* 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 Bruecknerbb664f42009-06-17 21:54:47 +0000997 }
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 Wiedmanndc5367b2017-01-10 17:10:34 +01001004 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
Julian Wiedmann238965b2019-06-18 20:43:00 +02001005 headroom = sizeof(struct af_iucv_trans_hdr) +
1006 LL_RESERVED_SPACE(iucv->hs_dev);
Eugene Crossere5374392016-06-13 18:46:14 +02001007 linear = len;
1008 } else {
Julian Wiedmanndc5367b2017-01-10 17:10:34 +01001009 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 Crossere5374392016-06-13 18:46:14 +02001019 }
1020 skb = sock_alloc_send_pskb(sk, headroom + linear, len - linear,
1021 noblock, &err, 0);
Eugene Crossered4ac422015-03-30 15:40:42 +02001022 if (!skb)
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001023 goto out;
Eugene Crossere5374392016-06-13 18:46:14 +02001024 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 Bruecknerbb664f42009-06-17 21:54:47 +00001031 goto fail;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001032
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001033 /* 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 Bruecknerbb664f42009-06-17 21:54:47 +00001045 /* increment and save iucv message tag for msg_completion cbk */
1046 txmsg.tag = iucv->send_tag++;
Ursula Braunf9c41a62013-04-07 22:19:26 +00001047 IUCV_SKB_CB(skb)->tag = txmsg.tag;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001048
Ursula Braun3881ac42011-08-08 01:33:54 +00001049 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 Wiedmannb2f54392018-09-05 16:55:11 +02001054 goto out;
Ursula Braun3881ac42011-08-08 01:33:54 +00001055 }
Eugene Crossere5374392016-06-13 18:46:14 +02001056 } else { /* Classic VM IUCV transport */
1057 skb_queue_tail(&iucv->send_skb_q, skb);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001058
Eugene Crossere5374392016-06-13 18:46:14 +02001059 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags) &&
1060 skb->len <= 7) {
1061 err = iucv_send_iprm(iucv->path, &txmsg, skb);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001062
Eugene Crossere5374392016-06-13 18:46:14 +02001063 /* 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 Bruecknerbb664f42009-06-17 21:54:47 +00001098 }
Eugene Crossere5374392016-06-13 18:46:14 +02001099 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 Hunteac37312007-02-08 13:51:54 -08001112 skb_unlink(skb, &iucv->send_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -08001113 goto fail;
1114 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001115 }
1116
1117 release_sock(sk);
1118 return len;
1119
1120fail:
1121 kfree_skb(skb);
1122out:
1123 release_sock(sk);
1124 return err;
1125}
1126
Eugene Crossera006353a92016-06-13 18:46:16 +02001127static 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 Bruecknerbf95d202009-09-16 04:37:28 +00001155/* iucv_process_message() - Receive a single outstanding IUCV message
1156 *
1157 * Locking: must be called with message_q.lock held
1158 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001159static 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 Bruecknerb8942e32009-04-21 23:26:23 +00001164 unsigned int len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001165
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001166 len = iucv_msg_length(msg);
1167
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001168 /* 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 Braunf9c41a62013-04-07 22:19:26 +00001170 IUCV_SKB_CB(skb)->class = msg->class;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001171
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001172 /* 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 Braunf0703c82007-10-08 02:03:31 -07001178 } else {
Eugene Crossera006353a92016-06-13 18:46:16 +02001179 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 Blaschka6fcd61f2011-08-08 01:33:51 +00001197 msg->flags & IUCV_IPRMDATA,
1198 skb->data, len, NULL);
Eugene Crossera006353a92016-06-13 18:46:16 +02001199 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001200 if (rc) {
1201 kfree_skb(skb);
1202 return;
1203 }
Eugene Crossera006353a92016-06-13 18:46:16 +02001204 WARN_ON_ONCE(skb->len != len);
Ursula Braunf0703c82007-10-08 02:03:31 -07001205 }
1206
Ursula Braunf9c41a62013-04-07 22:19:26 +00001207 IUCV_SKB_CB(skb)->offset = 0;
Ursula Braun8c68b1a2016-10-07 15:51:48 +02001208 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 Braunf0703c82007-10-08 02:03:31 -07001215}
1216
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001217/* iucv_process_message_q() - Process outstanding IUCV messages
1218 *
1219 * Locking: must be called with message_q.lock held
1220 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001221static 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 Crossera006353a92016-06-13 18:46:16 +02001228 skb = alloc_iucv_recv_skb(iucv_msg_length(&p->msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001229 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 Xue1b784142015-03-02 15:37:48 +08001239static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1240 size_t len, int flags)
Jennifer Hunteac37312007-02-08 13:51:54 -08001241{
1242 int noblock = flags & MSG_DONTWAIT;
1243 struct sock *sk = sock->sk;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001244 struct iucv_sock *iucv = iucv_sk(sk);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001245 unsigned int copied, rlen;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001246 struct sk_buff *skb, *rskb, *cskb;
Jennifer Hunteac37312007-02-08 13:51:54 -08001247 int err = 0;
Ursula Braunf9c41a62013-04-07 22:19:26 +00001248 u32 offset;
Jennifer Hunteac37312007-02-08 13:51:54 -08001249
Ursula Braunaac63992011-12-19 22:56:31 +00001250 if ((sk->sk_state == IUCV_DISCONN) &&
Ursula Braunf0703c82007-10-08 02:03:31 -07001251 skb_queue_empty(&iucv->backlog_skb_q) &&
1252 skb_queue_empty(&sk->sk_receive_queue) &&
1253 list_empty(&iucv->message_q.list))
Jennifer Hunt561e0362007-05-04 12:22:07 -07001254 return 0;
1255
Jennifer Hunteac37312007-02-08 13:51:54 -08001256 if (flags & (MSG_OOB))
1257 return -EOPNOTSUPP;
1258
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001259 /* receive/dequeue next skb:
1260 * the function understands MSG_PEEK and, thus, does not dequeue skb */
Jennifer Hunteac37312007-02-08 13:51:54 -08001261 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 Braunf9c41a62013-04-07 22:19:26 +00001268 offset = IUCV_SKB_CB(skb)->offset;
1269 rlen = skb->len - offset; /* real length of skb */
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001270 copied = min_t(unsigned int, rlen, len);
Ursula Braun82492a32012-03-07 02:06:24 +00001271 if (!rlen)
1272 sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001273
Jennifer Hunt561e0362007-05-04 12:22:07 -07001274 cskb = skb;
David S. Miller51f3d022014-11-05 16:46:40 -05001275 if (skb_copy_datagram_msg(cskb, offset, msg, copied)) {
Hendrik Brueckner802788b2009-04-21 23:26:26 +00001276 if (!(flags & MSG_PEEK))
1277 skb_queue_head(&sk->sk_receive_queue, skb);
1278 return -EFAULT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001279 }
1280
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001281 /* 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 Hunteac37312007-02-08 13:51:54 -08001288
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001289 /* 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 Braunf9c41a62013-04-07 22:19:26 +00001293 sizeof(IUCV_SKB_CB(skb)->class),
1294 (void *)&IUCV_SKB_CB(skb)->class);
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001295 if (err) {
1296 if (!(flags & MSG_PEEK))
1297 skb_queue_head(&sk->sk_receive_queue, skb);
1298 return err;
1299 }
1300
Jennifer Hunteac37312007-02-08 13:51:54 -08001301 /* Mark read part of skb as used */
1302 if (!(flags & MSG_PEEK)) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001303
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001304 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1305 if (sk->sk_type == SOCK_STREAM) {
Ursula Braunf9c41a62013-04-07 22:19:26 +00001306 if (copied < rlen) {
1307 IUCV_SKB_CB(skb)->offset = offset + copied;
Ursula Braun2f139a52014-03-19 07:57:59 +01001308 skb_queue_head(&sk->sk_receive_queue, skb);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001309 goto done;
1310 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001311 }
1312
1313 kfree_skb(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00001314 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 Hunt561e0362007-05-04 12:22:07 -07001322
1323 /* Queue backlog skbs */
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001324 spin_lock_bh(&iucv->message_q.lock);
Ursula Braunf0703c82007-10-08 02:03:31 -07001325 rskb = skb_dequeue(&iucv->backlog_skb_q);
Heiko Carstensda99f052007-05-04 12:23:27 -07001326 while (rskb) {
Ursula Braunf9c41a62013-04-07 22:19:26 +00001327 IUCV_SKB_CB(rskb)->offset = 0;
Ursula Braun8c68b1a2016-10-07 15:51:48 +02001328 if (__sock_queue_rcv_skb(sk, rskb)) {
1329 /* handle rcv queue full */
Ursula Braunf0703c82007-10-08 02:03:31 -07001330 skb_queue_head(&iucv->backlog_skb_q,
Jennifer Hunt561e0362007-05-04 12:22:07 -07001331 rskb);
1332 break;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001333 }
Ursula Braun8c68b1a2016-10-07 15:51:48 +02001334 rskb = skb_dequeue(&iucv->backlog_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001335 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001336 if (skb_queue_empty(&iucv->backlog_skb_q)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001337 if (!list_empty(&iucv->message_q.list))
1338 iucv_process_message_q(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001339 if (atomic_read(&iucv->msg_recv) >=
1340 iucv->msglimit / 2) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001341 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
Ursula Braun3881ac42011-08-08 01:33:54 +00001342 if (err) {
1343 sk->sk_state = IUCV_DISCONN;
1344 sk->sk_state_change(sk);
1345 }
1346 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001347 }
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001348 spin_unlock_bh(&iucv->message_q.lock);
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001349 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001350
1351done:
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001352 /* 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 Hunteac37312007-02-08 13:51:54 -08001357}
1358
Al Viroade994f2017-07-03 00:01:49 -04001359static inline __poll_t iucv_accept_poll(struct sock *parent)
Jennifer Hunteac37312007-02-08 13:51:54 -08001360{
1361 struct iucv_sock *isk, *n;
1362 struct sock *sk;
1363
Heiko Carstensda99f052007-05-04 12:23:27 -07001364 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001365 sk = (struct sock *) isk;
1366
1367 if (sk->sk_state == IUCV_CONNECTED)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001368 return EPOLLIN | EPOLLRDNORM;
Jennifer Hunteac37312007-02-08 13:51:54 -08001369 }
1370
1371 return 0;
1372}
1373
Julian Wiedmanne9a36ca2020-05-19 21:10:12 +02001374static __poll_t iucv_sock_poll(struct file *file, struct socket *sock,
1375 poll_table *wait)
Jennifer Hunteac37312007-02-08 13:51:54 -08001376{
1377 struct sock *sk = sock->sk;
Al Viroade994f2017-07-03 00:01:49 -04001378 __poll_t mask = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -08001379
Karsten Graul89ab0662018-10-23 13:40:39 +02001380 sock_poll_wait(file, sock, wait);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001381
Jennifer Hunteac37312007-02-08 13:51:54 -08001382 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 Torvaldsa9a08842018-02-11 14:34:03 -08001386 mask |= EPOLLERR |
1387 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
Jennifer Hunteac37312007-02-08 13:51:54 -08001388
1389 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001390 mask |= EPOLLRDHUP;
Jennifer Hunteac37312007-02-08 13:51:54 -08001391
1392 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001393 mask |= EPOLLHUP;
Jennifer Hunteac37312007-02-08 13:51:54 -08001394
1395 if (!skb_queue_empty(&sk->sk_receive_queue) ||
Heiko Carstensda99f052007-05-04 12:23:27 -07001396 (sk->sk_shutdown & RCV_SHUTDOWN))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001397 mask |= EPOLLIN | EPOLLRDNORM;
Jennifer Hunteac37312007-02-08 13:51:54 -08001398
1399 if (sk->sk_state == IUCV_CLOSED)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001400 mask |= EPOLLHUP;
Jennifer Hunteac37312007-02-08 13:51:54 -08001401
Ursula Braunaac63992011-12-19 22:56:31 +00001402 if (sk->sk_state == IUCV_DISCONN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001403 mask |= EPOLLIN;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001404
Ursula Braun7f1b0ea2012-02-08 00:19:46 +00001405 if (sock_writeable(sk) && iucv_below_msglim(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001406 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
Jennifer Hunteac37312007-02-08 13:51:54 -08001407 else
Eric Dumazet9cd3e072015-11-29 20:03:10 -08001408 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001409
1410 return mask;
1411}
1412
1413static 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 Hunteac37312007-02-08 13:51:54 -08001419
1420 how++;
1421
1422 if ((how & ~SHUTDOWN_MASK) || !how)
1423 return -EINVAL;
1424
1425 lock_sock(sk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001426 switch (sk->sk_state) {
Ursula Braun82492a32012-03-07 02:06:24 +00001427 case IUCV_LISTEN:
Hendrik Bruecknere14ad5f2009-04-21 06:04:23 +00001428 case IUCV_DISCONN:
1429 case IUCV_CLOSING:
Jennifer Hunteac37312007-02-08 13:51:54 -08001430 case IUCV_CLOSED:
1431 err = -ENOTCONN;
1432 goto fail;
Jennifer Hunteac37312007-02-08 13:51:54 -08001433 default:
Jennifer Hunteac37312007-02-08 13:51:54 -08001434 break;
1435 }
1436
1437 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun82492a32012-03-07 02:06:24 +00001438 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 Hunteac37312007-02-08 13:51:54 -08001455 }
Ursula Braun82492a32012-03-07 02:06:24 +00001456 } else
1457 iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
Jennifer Hunteac37312007-02-08 13:51:54 -08001458 }
1459
Ursula Braun82492a32012-03-07 02:06:24 +00001460 sk->sk_shutdown |= how;
Jennifer Hunteac37312007-02-08 13:51:54 -08001461 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun1042cab2014-07-21 12:54:44 +02001462 if ((iucv->transport == AF_IUCV_TRANS_IUCV) &&
1463 iucv->path) {
Ursula Braun82492a32012-03-07 02:06:24 +00001464 err = pr_iucv->path_quiesce(iucv->path, NULL);
1465 if (err)
1466 err = -ENOTCONN;
1467/* skb_queue_purge(&sk->sk_receive_queue); */
1468 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001469 skb_queue_purge(&sk->sk_receive_queue);
1470 }
1471
1472 /* Wake up anyone sleeping in poll */
1473 sk->sk_state_change(sk);
1474
1475fail:
1476 release_sock(sk);
1477 return err;
1478}
1479
1480static 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 Hunteac37312007-02-08 13:51:54 -08001490 sock_orphan(sk);
1491 iucv_sock_kill(sk);
1492 return err;
1493}
1494
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001495/* getsockopt and setsockopt */
1496static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001497 sockptr_t optval, unsigned int optlen)
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001498{
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 Hellwiga7b75c52020-07-23 08:09:07 +02001510 if (copy_from_sockptr(&val, optval, sizeof(int)))
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001511 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 Brueckner09488e22009-04-21 23:26:27 +00001523 case SO_MSGLIMIT:
1524 switch (sk->sk_state) {
1525 case IUCV_OPEN:
1526 case IUCV_BOUND:
Julian Wiedmann0d1c7662020-05-19 21:10:10 +02001527 if (val < 1 || val > U16_MAX)
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001528 rc = -EINVAL;
1529 else
1530 iucv->msglimit = val;
1531 break;
1532 default:
1533 rc = -EINVAL;
1534 break;
1535 }
1536 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001537 default:
1538 rc = -ENOPROTOOPT;
1539 break;
1540 }
1541 release_sock(sk);
1542
1543 return rc;
1544}
1545
1546static 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 Braun51363b82012-02-08 00:19:48 +00001551 unsigned int val;
1552 int len;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001553
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 Brueckner09488e22009-04-21 23:26:27 +00001569 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 Braun51363b82012-02-08 00:19:48 +00001575 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 Brueckner9d5c5d82009-04-21 23:26:22 +00001582 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 Hunteac37312007-02-08 13:51:54 -08001595/* Callback wrappers - called from iucv base support */
1596static 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 Hunteac37312007-02-08 13:51:54 -08001602 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 Braunfebca282007-07-14 19:04:25 -07001611 sk = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001612 sk_for_each(sk, &iucv_sk_list.head)
Jennifer Hunteac37312007-02-08 13:51:54 -08001613 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 Blaschka6fcd61f2011-08-08 01:33:51 +00001634 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001635 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001636 goto fail;
1637 }
1638
1639 /* Check for backlog size */
1640 if (sk_acceptq_is_full(sk)) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001641 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001642 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001643 goto fail;
1644 }
1645
1646 /* Create the new socket */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001647 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
Heiko Carstensda99f052007-05-04 12:23:27 -07001648 if (!nsk) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001649 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001650 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001651 goto fail;
1652 }
1653
1654 niucv = iucv_sk(nsk);
1655 iucv_sock_init(nsk, sk);
Julian Wiedmannfdbf6322019-06-18 20:42:59 +02001656 niucv->transport = AF_IUCV_TRANS_IUCV;
1657 nsk->sk_allocation |= GFP_DMA;
Jennifer Hunteac37312007-02-08 13:51:54 -08001658
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 Brueckner09488e22009-04-21 23:26:27 +00001672 /* set message limit for path based on msglimit of accepting socket */
1673 niucv->msglimit = iucv->msglimit;
1674 path->msglim = iucv->msglimit;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001675 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001676 if (err) {
Ursula Braun7d316b92012-02-08 00:19:45 +00001677 iucv_sever_path(nsk, 1);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001678 iucv_sock_kill(nsk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001679 goto fail;
1680 }
1681
1682 iucv_accept_enqueue(sk, nsk);
1683
1684 /* Wake up accept */
1685 nsk->sk_state = IUCV_CONNECTED;
David S. Miller676d2362014-04-11 16:15:36 -04001686 sk->sk_data_ready(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001687 err = 0;
1688fail:
1689 bh_unlock_sock(sk);
1690 return 0;
1691}
1692
1693static 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
1701static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1702{
1703 struct sock *sk = path->private;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001704 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braunf0703c82007-10-08 02:03:31 -07001705 struct sk_buff *skb;
1706 struct sock_msg_q *save_msg;
1707 int len;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001708
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001709 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001710 pr_iucv->message_reject(path, msg);
Jennifer Hunteac37312007-02-08 13:51:54 -08001711 return;
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001712 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001713
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001714 spin_lock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001715
Ursula Braunf0703c82007-10-08 02:03:31 -07001716 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 Dumazet87fb4b72011-10-13 07:28:54 +00001721 len += SKB_TRUESIZE(iucv_msg_length(msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001722 if (len > sk->sk_rcvbuf)
1723 goto save_message;
1724
Eugene Crossera006353a92016-06-13 18:46:16 +02001725 skb = alloc_iucv_recv_skb(iucv_msg_length(msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001726 if (!skb)
1727 goto save_message;
Jennifer Hunteac37312007-02-08 13:51:54 -08001728
Ursula Braunf0703c82007-10-08 02:03:31 -07001729 iucv_process_message(sk, skb, path, msg);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001730 goto out_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -08001731
Ursula Braunf0703c82007-10-08 02:03:31 -07001732save_message:
1733 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
Ursula Braund4444722008-02-07 18:07:19 -08001734 if (!save_msg)
Julia Lawalla56635a2010-05-26 05:56:48 +00001735 goto out_unlock;
Ursula Braunf0703c82007-10-08 02:03:31 -07001736 save_msg->path = path;
1737 save_msg->msg = *msg;
1738
Ursula Braunf0703c82007-10-08 02:03:31 -07001739 list_add_tail(&save_msg->list, &iucv->message_q.list);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001740
1741out_unlock:
Ursula Braunf0703c82007-10-08 02:03:31 -07001742 spin_unlock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001743}
1744
1745static void iucv_callback_txdone(struct iucv_path *path,
1746 struct iucv_message *msg)
1747{
1748 struct sock *sk = path->private;
Ursula Braunf2a77992008-02-07 18:07:44 -08001749 struct sk_buff *this = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001750 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
David S. Miller9e733172018-08-22 17:01:51 -07001751 struct sk_buff *list_skb;
Jennifer Hunteac37312007-02-08 13:51:54 -08001752 unsigned long flags;
1753
Ursula Braun7d316b92012-02-08 00:19:45 +00001754 bh_lock_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001755
David S. Miller9e733172018-08-22 17:01:51 -07001756 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 Braunf2a77992008-02-07 18:07:44 -08001761 }
David S. Miller9e733172018-08-22 17:01:51 -07001762 }
1763 if (this)
1764 __skb_unlink(this, list);
1765 spin_unlock_irqrestore(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001766
David S. Miller9e733172018-08-22 17:01:51 -07001767 if (this) {
1768 kfree_skb(this);
1769 /* wake up any process waiting for sending */
1770 iucv_sock_wake_msglim(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001771 }
1772
Heiko Carstensda99f052007-05-04 12:23:27 -07001773 if (sk->sk_state == IUCV_CLOSING) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001774 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 Braun7d316b92012-02-08 00:19:45 +00001779 bh_unlock_sock(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001780
Jennifer Hunteac37312007-02-08 13:51:54 -08001781}
1782
1783static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1784{
1785 struct sock *sk = path->private;
1786
Ursula Braun7d316b92012-02-08 00:19:45 +00001787 if (sk->sk_state == IUCV_CLOSED)
1788 return;
1789
1790 bh_lock_sock(sk);
1791 iucv_sever_path(sk, 1);
Ursula Braunaac63992011-12-19 22:56:31 +00001792 sk->sk_state = IUCV_DISCONN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001793
1794 sk->sk_state_change(sk);
Ursula Braun7d316b92012-02-08 00:19:45 +00001795 bh_unlock_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001796}
1797
Hendrik Brueckneraf88b522009-04-21 23:26:21 +00001798/* 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 */
1801static 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 Braun3881ac42011-08-08 01:33:54 +00001813/***************** HiperSockets transport callbacks ********************/
1814static void afiucv_swap_src_dest(struct sk_buff *skb)
1815{
Julian Wiedmanncd11d112018-09-26 18:29:06 +02001816 struct af_iucv_trans_hdr *trans_hdr = iucv_trans_hdr(skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00001817 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 **/
1837static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1838{
Julian Wiedmanncd11d112018-09-26 18:29:06 +02001839 struct af_iucv_trans_hdr *trans_hdr = iucv_trans_hdr(skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00001840 struct sock *nsk;
1841 struct iucv_sock *iucv, *niucv;
Ursula Braun3881ac42011-08-08 01:33:54 +00001842 int err;
1843
1844 iucv = iucv_sk(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001845 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. Biederman11aa9c22015-05-08 21:09:13 -05001853 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
Ursula Braun3881ac42011-08-08 01:33:54 +00001854 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 Braun3881ac42011-08-08 01:33:54 +00001859 afiucv_swap_src_dest(skb);
1860 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1861 err = dev_queue_xmit(skb);
Ursula Braun4d520f62014-05-28 10:22:29 +02001862 iucv_sock_kill(nsk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001863 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 Braun800c5eb2012-02-08 00:19:47 +00001880 niucv->hs_dev = iucv->hs_dev;
1881 dev_hold(niucv->hs_dev);
Ursula Braun3881ac42011-08-08 01:33:54 +00001882 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. Miller676d2362014-04-11 16:15:36 -04001890 sk->sk_data_ready(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001891 } else
1892 iucv_sock_kill(nsk);
1893 bh_unlock_sock(sk);
1894
1895out:
1896 return NET_RX_SUCCESS;
1897}
1898
1899/**
1900 * afiucv_hs_callback_synack() - react on received SYN-ACK
1901 **/
1902static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
1903{
1904 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001905
1906 if (!iucv)
1907 goto out;
1908 if (sk->sk_state != IUCV_BOUND)
1909 goto out;
1910 bh_lock_sock(sk);
Julian Wiedmanncd11d112018-09-26 18:29:06 +02001911 iucv->msglimit_peer = iucv_trans_hdr(skb)->window;
Ursula Braun3881ac42011-08-08 01:33:54 +00001912 sk->sk_state = IUCV_CONNECTED;
1913 sk->sk_state_change(sk);
1914 bh_unlock_sock(sk);
1915out:
1916 kfree_skb(skb);
1917 return NET_RX_SUCCESS;
1918}
1919
1920/**
1921 * afiucv_hs_callback_synfin() - react on received SYN_FIN
1922 **/
1923static 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);
1935out:
1936 kfree_skb(skb);
1937 return NET_RX_SUCCESS;
1938}
1939
1940/**
1941 * afiucv_hs_callback_fin() - react on received FIN
1942 **/
1943static 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 Braun800c5eb2012-02-08 00:19:47 +00001948 if (!iucv)
1949 goto out;
1950 bh_lock_sock(sk);
1951 if (sk->sk_state == IUCV_CONNECTED) {
Ursula Braunaac63992011-12-19 22:56:31 +00001952 sk->sk_state = IUCV_DISCONN;
Ursula Braun3881ac42011-08-08 01:33:54 +00001953 sk->sk_state_change(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001954 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00001955 bh_unlock_sock(sk);
1956out:
Ursula Braun3881ac42011-08-08 01:33:54 +00001957 kfree_skb(skb);
1958 return NET_RX_SUCCESS;
1959}
1960
1961/**
1962 * afiucv_hs_callback_win() - react on received WIN
1963 **/
1964static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
1965{
1966 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001967
1968 if (!iucv)
1969 return NET_RX_SUCCESS;
1970
1971 if (sk->sk_state != IUCV_CONNECTED)
1972 return NET_RX_SUCCESS;
1973
Julian Wiedmanncd11d112018-09-26 18:29:06 +02001974 atomic_sub(iucv_trans_hdr(skb)->window, &iucv->msg_sent);
Ursula Braun3881ac42011-08-08 01:33:54 +00001975 iucv_sock_wake_msglim(sk);
1976 return NET_RX_SUCCESS;
1977}
1978
1979/**
1980 * afiucv_hs_callback_rx() - react on received data
1981 **/
1982static 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 Braun82492a32012-03-07 02:06:24 +00001996 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1997 kfree_skb(skb);
1998 return NET_RX_SUCCESS;
1999 }
2000
Eugene Crosser979f66b2015-12-11 12:27:51 +01002001 /* write stuff from iucv_msg to skb cb */
Ursula Braun3881ac42011-08-08 01:33:54 +00002002 skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2003 skb_reset_transport_header(skb);
2004 skb_reset_network_header(skb);
Ursula Braunf9c41a62013-04-07 22:19:26 +00002005 IUCV_SKB_CB(skb)->offset = 0;
Ursula Braun8c68b1a2016-10-07 15:51:48 +02002006 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 Braun3881ac42011-08-08 01:33:54 +00002012 spin_lock(&iucv->message_q.lock);
2013 if (skb_queue_empty(&iucv->backlog_skb_q)) {
Ursula Braun8c68b1a2016-10-07 15:51:48 +02002014 if (__sock_queue_rcv_skb(sk, skb))
Ursula Braun3881ac42011-08-08 01:33:54 +00002015 /* handle rcv queue full */
2016 skb_queue_tail(&iucv->backlog_skb_q, skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002017 } 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 **/
2028static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2029 struct packet_type *pt, struct net_device *orig_dev)
2030{
Ursula Braun3881ac42011-08-08 01:33:54 +00002031 struct sock *sk;
2032 struct iucv_sock *iucv;
2033 struct af_iucv_trans_hdr *trans_hdr;
Julian Wiedmann22244092018-09-05 16:55:10 +02002034 int err = NET_RX_SUCCESS;
Ursula Braun3881ac42011-08-08 01:33:54 +00002035 char nullstring[8];
Ursula Braun3881ac42011-08-08 01:33:54 +00002036
Julian Wiedmanncd11d112018-09-26 18:29:06 +02002037 if (!pskb_may_pull(skb, sizeof(*trans_hdr))) {
2038 WARN_ONCE(1, "AF_IUCV failed to receive skb, len=%u", skb->len);
Eugene Crosser979f66b2015-12-11 12:27:51 +01002039 kfree_skb(skb);
2040 return NET_RX_SUCCESS;
2041 }
Julian Wiedmanncd11d112018-09-26 18:29:06 +02002042
2043 trans_hdr = iucv_trans_hdr(skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002044 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 Levinb67bfe02013-02-27 17:06:00 -08002052 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002053 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 Braun800c5eb2012-02-08 00:19:47 +00002110 if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
2111 kfree_skb(skb);
2112 break;
2113 }
Gustavo A. R. Silva05bba1e2019-07-29 09:59:47 -05002114 /* fall through - and receive non-zero length data */
Ursula Braun82492a32012-03-07 02:06:24 +00002115 case (AF_IUCV_FLAG_SHT):
2116 /* shutdown request */
Gustavo A. R. Silva05bba1e2019-07-29 09:59:47 -05002117 /* fall through - and receive zero length data */
Ursula Braun3881ac42011-08-08 01:33:54 +00002118 case 0:
2119 /* plain data frame */
Ursula Braunf9c41a62013-04-07 22:19:26 +00002120 IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
Ursula Braun3881ac42011-08-08 01:33:54 +00002121 err = afiucv_hs_callback_rx(sk, skb);
2122 break;
2123 default:
Julian Wiedmann22244092018-09-05 16:55:10 +02002124 kfree_skb(skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002125 }
2126
2127 return err;
2128}
2129
2130/**
2131 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
2132 * transport
2133 **/
2134static 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 Braun800c5eb2012-02-08 00:19:47 +00002142 struct sk_buff *nskb;
Ursula Braun3881ac42011-08-08 01:33:54 +00002143 unsigned long flags;
Ursula Braun3881ac42011-08-08 01:33:54 +00002144
Ursula Braun800c5eb2012-02-08 00:19:47 +00002145 read_lock_irqsave(&iucv_sk_list.lock, flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002146 sk_for_each(sk, &iucv_sk_list.head)
Ursula Braun3881ac42011-08-08 01:33:54 +00002147 if (sk == isk) {
2148 iucv = iucv_sk(sk);
2149 break;
2150 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002151 read_unlock_irqrestore(&iucv_sk_list.lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002152
Ursula Braun800c5eb2012-02-08 00:19:47 +00002153 if (!iucv || sock_flag(sk, SOCK_ZAPPED))
Ursula Braun3881ac42011-08-08 01:33:54 +00002154 return;
2155
Ursula Braun3881ac42011-08-08 01:33:54 +00002156 list = &iucv->send_skb_q;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002157 spin_lock_irqsave(&list->lock, flags);
David S. Miller9e733172018-08-22 17:01:51 -07002158 skb_queue_walk_safe(list, list_skb, nskb) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002159 if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002160 switch (n) {
2161 case TX_NOTIFY_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002162 __skb_unlink(list_skb, list);
2163 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002164 iucv_sock_wake_msglim(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002165 break;
2166 case TX_NOTIFY_PENDING:
2167 atomic_inc(&iucv->pendings);
2168 break;
2169 case TX_NOTIFY_DELAYED_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002170 __skb_unlink(list_skb, list);
Ursula Braun3881ac42011-08-08 01:33:54 +00002171 atomic_dec(&iucv->pendings);
2172 if (atomic_read(&iucv->pendings) <= 0)
2173 iucv_sock_wake_msglim(sk);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002174 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002175 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 Braun800c5eb2012-02-08 00:19:47 +00002181 __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 Braun3881ac42011-08-08 01:33:54 +00002187 break;
2188 }
2189 break;
2190 }
Ursula Braun3881ac42011-08-08 01:33:54 +00002191 }
2192 spin_unlock_irqrestore(&list->lock, flags);
2193
Ursula Braun42bd48e2011-12-19 22:56:28 +00002194 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 Braun3881ac42011-08-08 01:33:54 +00002201}
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002202
2203/*
2204 * afiucv_netdev_event: handle netdev notifier chain events
2205 */
2206static int afiucv_netdev_event(struct notifier_block *this,
2207 unsigned long event, void *ptr)
2208{
Jiri Pirko351638e2013-05-28 01:30:21 +00002209 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002210 struct sock *sk;
2211 struct iucv_sock *iucv;
2212
2213 switch (event) {
2214 case NETDEV_REBOOT:
2215 case NETDEV_GOING_DOWN:
Sasha Levinb67bfe02013-02-27 17:06:00 -08002216 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002217 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
2235static struct notifier_block afiucv_netdev_notifier = {
2236 .notifier_call = afiucv_netdev_event,
2237};
2238
Alexey Dobriyan5708e862009-09-14 12:23:23 +00002239static const struct proto_ops iucv_sock_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002240 .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 Torvaldsa11e1d42018-06-28 09:43:44 -07002250 .poll = iucv_sock_poll,
Jennifer Hunteac37312007-02-08 13:51:54 -08002251 .ioctl = sock_no_ioctl,
2252 .mmap = sock_no_mmap,
2253 .socketpair = sock_no_socketpair,
2254 .shutdown = iucv_sock_shutdown,
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00002255 .setsockopt = iucv_sock_setsockopt,
2256 .getsockopt = iucv_sock_getsockopt,
Jennifer Hunteac37312007-02-08 13:51:54 -08002257};
2258
Julian Wiedmanne9a36ca2020-05-19 21:10:12 +02002259static 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 Hemmingerec1b4cf2009-10-05 05:58:39 +00002288static const struct net_proto_family iucv_sock_family_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002289 .family = AF_IUCV,
2290 .owner = THIS_MODULE,
2291 .create = iucv_sock_create,
2292};
2293
Ursula Braun3881ac42011-08-08 01:33:54 +00002294static struct packet_type iucv_packet_type = {
2295 .type = cpu_to_be16(ETH_P_AF_IUCV),
2296 .func = afiucv_hs_rcv,
2297};
2298
2299static int afiucv_iucv_init(void)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002300{
Julian Wiedmann585bc222020-05-19 21:10:09 +02002301 return pr_iucv->iucv_register(&af_iucv_handler, 0);
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002302}
2303
Julian Wiedmann06996c12019-06-18 20:43:01 +02002304static void afiucv_iucv_exit(void)
2305{
Julian Wiedmann06996c12019-06-18 20:43:01 +02002306 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2307}
2308
Heiko Carstensda99f052007-05-04 12:23:27 -07002309static int __init afiucv_init(void)
Jennifer Hunteac37312007-02-08 13:51:54 -08002310{
2311 int err;
2312
Ursula Braun3881ac42011-08-08 01:33:54 +00002313 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 Hunteac37312007-02-08 13:51:54 -08002320
Ursula Braun3881ac42011-08-08 01:33:54 +00002321 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 Blaschka6fcd61f2011-08-08 01:33:51 +00002329 }
2330
Jennifer Hunteac37312007-02-08 13:51:54 -08002331 err = proto_register(&iucv_proto, 0);
2332 if (err)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002333 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08002334 err = sock_register(&iucv_sock_family_ops);
2335 if (err)
2336 goto out_proto;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002337
Ursula Braun3881ac42011-08-08 01:33:54 +00002338 if (pr_iucv) {
2339 err = afiucv_iucv_init();
2340 if (err)
2341 goto out_sock;
Julian Wiedmann06996c12019-06-18 20:43:01 +02002342 }
2343
2344 err = register_netdevice_notifier(&afiucv_netdev_notifier);
2345 if (err)
2346 goto out_notifier;
2347
Ursula Braun3881ac42011-08-08 01:33:54 +00002348 dev_add_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002349 return 0;
2350
Julian Wiedmann06996c12019-06-18 20:43:01 +02002351out_notifier:
2352 if (pr_iucv)
2353 afiucv_iucv_exit();
Ursula Braunc23cad92009-06-16 10:30:44 +02002354out_sock:
2355 sock_unregister(PF_IUCV);
Jennifer Hunteac37312007-02-08 13:51:54 -08002356out_proto:
2357 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002358out:
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002359 if (pr_iucv)
2360 symbol_put(iucv_if);
Jennifer Hunteac37312007-02-08 13:51:54 -08002361 return err;
2362}
2363
2364static void __exit afiucv_exit(void)
2365{
Ursula Braun3881ac42011-08-08 01:33:54 +00002366 if (pr_iucv) {
Julian Wiedmann06996c12019-06-18 20:43:01 +02002367 afiucv_iucv_exit();
Ursula Braun3881ac42011-08-08 01:33:54 +00002368 symbol_put(iucv_if);
Julian Wiedmann06996c12019-06-18 20:43:01 +02002369 }
2370
2371 unregister_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002372 dev_remove_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002373 sock_unregister(PF_IUCV);
2374 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002375}
2376
2377module_init(afiucv_init);
2378module_exit(afiucv_exit);
2379
2380MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2381MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2382MODULE_VERSION(VERSION);
2383MODULE_LICENSE("GPL");
2384MODULE_ALIAS_NETPROTO(PF_IUCV);