blob: e2f16a0173a93bc876e293a68878d83d78cda7ef [file] [log] [blame]
Jennifer Hunteac37312007-02-08 13:51:54 -08001/*
Jennifer Hunteac37312007-02-08 13:51:54 -08002 * IUCV protocol stack for Linux on zSeries
3 *
Ursula Braunc23cad92009-06-16 10:30:44 +02004 * Copyright IBM Corp. 2006, 2009
Jennifer Hunteac37312007-02-08 13:51:54 -08005 *
6 * Author(s): Jennifer Hunt <jenhunt@us.ibm.com>
Ursula Braunc23cad92009-06-16 10:30:44 +02007 * Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
8 * PM functions:
9 * Ursula Braun <ursula.braun@de.ibm.com>
Jennifer Hunteac37312007-02-08 13:51:54 -080010 */
11
Ursula Braun8f7c5022008-12-25 13:39:47 +010012#define KMSG_COMPONENT "af_iucv"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
Jennifer Hunteac37312007-02-08 13:51:54 -080015#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/list.h>
18#include <linux/errno.h>
19#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010020#include <linux/sched/signal.h>
Jennifer Hunteac37312007-02-08 13:51:54 -080021#include <linux/slab.h>
22#include <linux/skbuff.h>
23#include <linux/init.h>
24#include <linux/poll.h>
Paul Moore02f06912016-06-07 10:30:53 -040025#include <linux/security.h>
Jennifer Hunteac37312007-02-08 13:51:54 -080026#include <net/sock.h>
27#include <asm/ebcdic.h>
28#include <asm/cpcmd.h>
29#include <linux/kmod.h>
30
Jennifer Hunteac37312007-02-08 13:51:54 -080031#include <net/iucv/af_iucv.h>
32
Ursula Braun3881ac42011-08-08 01:33:54 +000033#define VERSION "1.2"
Jennifer Hunteac37312007-02-08 13:51:54 -080034
35static char iucv_userid[80];
36
Alexey Dobriyan5708e862009-09-14 12:23:23 +000037static const struct proto_ops iucv_sock_ops;
Jennifer Hunteac37312007-02-08 13:51:54 -080038
39static struct proto iucv_proto = {
40 .name = "AF_IUCV",
41 .owner = THIS_MODULE,
42 .obj_size = sizeof(struct iucv_sock),
43};
44
Frank Blaschka6fcd61f2011-08-08 01:33:51 +000045static struct iucv_interface *pr_iucv;
46
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +000047/* special AF_IUCV IPRM messages */
48static const u8 iprm_shutdown[8] =
49 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
50
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000051#define TRGCLS_SIZE (sizeof(((struct iucv_message *)0)->class))
52
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000053#define __iucv_sock_wait(sk, condition, timeo, ret) \
54do { \
55 DEFINE_WAIT(__wait); \
56 long __timeo = timeo; \
57 ret = 0; \
Eric Dumazetaa395142010-04-20 13:03:51 +000058 prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000059 while (!(condition)) { \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000060 if (!__timeo) { \
61 ret = -EAGAIN; \
62 break; \
63 } \
64 if (signal_pending(current)) { \
65 ret = sock_intr_errno(__timeo); \
66 break; \
67 } \
68 release_sock(sk); \
69 __timeo = schedule_timeout(__timeo); \
70 lock_sock(sk); \
71 ret = sock_error(sk); \
72 if (ret) \
73 break; \
74 } \
Eric Dumazetaa395142010-04-20 13:03:51 +000075 finish_wait(sk_sleep(sk), &__wait); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000076} while (0)
77
78#define iucv_sock_wait(sk, condition, timeo) \
79({ \
80 int __ret = 0; \
81 if (!(condition)) \
82 __iucv_sock_wait(sk, condition, timeo, __ret); \
83 __ret; \
84})
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000085
Heiko Carstens57f20442007-10-08 02:02:52 -070086static void iucv_sock_kill(struct sock *sk);
87static void iucv_sock_close(struct sock *sk);
Ursula Braun7d316b92012-02-08 00:19:45 +000088static void iucv_sever_path(struct sock *, int);
Heiko Carstens57f20442007-10-08 02:02:52 -070089
Ursula Braun3881ac42011-08-08 01:33:54 +000090static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
91 struct packet_type *pt, struct net_device *orig_dev);
92static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
93 struct sk_buff *skb, u8 flags);
94static void afiucv_hs_callback_txnotify(struct sk_buff *, enum iucv_tx_notify);
95
Jennifer Hunteac37312007-02-08 13:51:54 -080096/* Call Back functions */
97static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
98static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
Ursula Braun91e60eb2015-09-18 16:06:52 +020099static void iucv_callback_connack(struct iucv_path *, u8 *);
100static int iucv_callback_connreq(struct iucv_path *, u8 *, u8 *);
101static void iucv_callback_connrej(struct iucv_path *, u8 *);
102static void iucv_callback_shutdown(struct iucv_path *, u8 *);
Jennifer Hunteac37312007-02-08 13:51:54 -0800103
104static struct iucv_sock_list iucv_sk_list = {
Robert P. J. Day3db8ce32008-04-10 02:11:24 -0700105 .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
Jennifer Hunteac37312007-02-08 13:51:54 -0800106 .autobind_name = ATOMIC_INIT(0)
107};
108
109static struct iucv_handler af_iucv_handler = {
110 .path_pending = iucv_callback_connreq,
111 .path_complete = iucv_callback_connack,
112 .path_severed = iucv_callback_connrej,
113 .message_pending = iucv_callback_rx,
Hendrik Brueckneraf88b522009-04-21 23:26:21 +0000114 .message_complete = iucv_callback_txdone,
115 .path_quiesced = iucv_callback_shutdown,
Jennifer Hunteac37312007-02-08 13:51:54 -0800116};
117
118static inline void high_nmcpy(unsigned char *dst, char *src)
119{
120 memcpy(dst, src, 8);
121}
122
123static inline void low_nmcpy(unsigned char *dst, char *src)
124{
125 memcpy(&dst[8], src, 8);
126}
127
Ursula Braunc23cad92009-06-16 10:30:44 +0200128static int afiucv_pm_prepare(struct device *dev)
129{
130#ifdef CONFIG_PM_DEBUG
131 printk(KERN_WARNING "afiucv_pm_prepare\n");
132#endif
133 return 0;
134}
135
136static void afiucv_pm_complete(struct device *dev)
137{
138#ifdef CONFIG_PM_DEBUG
139 printk(KERN_WARNING "afiucv_pm_complete\n");
140#endif
Ursula Braunc23cad92009-06-16 10:30:44 +0200141}
142
143/**
144 * afiucv_pm_freeze() - Freeze PM callback
145 * @dev: AFIUCV dummy device
146 *
147 * Sever all established IUCV communication pathes
148 */
149static int afiucv_pm_freeze(struct device *dev)
150{
151 struct iucv_sock *iucv;
152 struct sock *sk;
Ursula Braunc23cad92009-06-16 10:30:44 +0200153
154#ifdef CONFIG_PM_DEBUG
155 printk(KERN_WARNING "afiucv_pm_freeze\n");
156#endif
157 read_lock(&iucv_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800158 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braunc23cad92009-06-16 10:30:44 +0200159 iucv = iucv_sk(sk);
Ursula Braunc23cad92009-06-16 10:30:44 +0200160 switch (sk->sk_state) {
Ursula Braunc23cad92009-06-16 10:30:44 +0200161 case IUCV_DISCONN:
162 case IUCV_CLOSING:
163 case IUCV_CONNECTED:
Ursula Braun7d316b92012-02-08 00:19:45 +0000164 iucv_sever_path(sk, 0);
Ursula Braunc23cad92009-06-16 10:30:44 +0200165 break;
166 case IUCV_OPEN:
167 case IUCV_BOUND:
168 case IUCV_LISTEN:
169 case IUCV_CLOSED:
170 default:
171 break;
172 }
Ursula Braun7d316b92012-02-08 00:19:45 +0000173 skb_queue_purge(&iucv->send_skb_q);
174 skb_queue_purge(&iucv->backlog_skb_q);
Ursula Braunc23cad92009-06-16 10:30:44 +0200175 }
176 read_unlock(&iucv_sk_list.lock);
zhong jiangfb3b4672018-08-07 19:20:08 +0800177 return 0;
Ursula Braunc23cad92009-06-16 10:30:44 +0200178}
179
180/**
181 * afiucv_pm_restore_thaw() - Thaw and restore PM callback
182 * @dev: AFIUCV dummy device
183 *
184 * socket clean up after freeze
185 */
186static int afiucv_pm_restore_thaw(struct device *dev)
187{
Ursula Braunc23cad92009-06-16 10:30:44 +0200188 struct sock *sk;
Ursula Braunc23cad92009-06-16 10:30:44 +0200189
190#ifdef CONFIG_PM_DEBUG
191 printk(KERN_WARNING "afiucv_pm_restore_thaw\n");
192#endif
193 read_lock(&iucv_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800194 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braunc23cad92009-06-16 10:30:44 +0200195 switch (sk->sk_state) {
196 case IUCV_CONNECTED:
197 sk->sk_err = EPIPE;
198 sk->sk_state = IUCV_DISCONN;
199 sk->sk_state_change(sk);
200 break;
201 case IUCV_DISCONN:
Ursula Braunc23cad92009-06-16 10:30:44 +0200202 case IUCV_CLOSING:
203 case IUCV_LISTEN:
204 case IUCV_BOUND:
205 case IUCV_OPEN:
206 default:
207 break;
208 }
209 }
210 read_unlock(&iucv_sk_list.lock);
211 return 0;
212}
213
Alexey Dobriyan47145212009-12-14 18:00:08 -0800214static const struct dev_pm_ops afiucv_pm_ops = {
Ursula Braunc23cad92009-06-16 10:30:44 +0200215 .prepare = afiucv_pm_prepare,
216 .complete = afiucv_pm_complete,
217 .freeze = afiucv_pm_freeze,
218 .thaw = afiucv_pm_restore_thaw,
219 .restore = afiucv_pm_restore_thaw,
220};
221
222static struct device_driver af_iucv_driver = {
223 .owner = THIS_MODULE,
224 .name = "afiucv",
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000225 .bus = NULL,
Ursula Braunc23cad92009-06-16 10:30:44 +0200226 .pm = &afiucv_pm_ops,
227};
228
229/* dummy device used as trigger for PM functions */
230static struct device *af_iucv_dev;
231
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000232/**
233 * iucv_msg_length() - Returns the length of an iucv message.
234 * @msg: Pointer to struct iucv_message, MUST NOT be NULL
235 *
236 * The function returns the length of the specified iucv message @msg of data
237 * stored in a buffer and of data stored in the parameter list (PRMDATA).
238 *
239 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
240 * data:
241 * PRMDATA[0..6] socket data (max 7 bytes);
242 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
243 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300244 * The socket data length is computed by subtracting the socket data length
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000245 * value from 0xFF.
246 * If the socket data len is greater 7, then PRMDATA can be used for special
247 * notifications (see iucv_sock_shutdown); and further,
248 * if the socket data len is > 7, the function returns 8.
249 *
250 * Use this function to allocate socket buffers to store iucv message data.
251 */
252static inline size_t iucv_msg_length(struct iucv_message *msg)
253{
254 size_t datalen;
255
256 if (msg->flags & IUCV_IPRMDATA) {
257 datalen = 0xff - msg->rmmsg[7];
258 return (datalen < 8) ? datalen : 8;
259 }
260 return msg->length;
261}
262
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000263/**
264 * iucv_sock_in_state() - check for specific states
265 * @sk: sock structure
266 * @state: first iucv sk state
267 * @state: second iucv sk state
268 *
269 * Returns true if the socket in either in the first or second state.
270 */
271static int iucv_sock_in_state(struct sock *sk, int state, int state2)
272{
273 return (sk->sk_state == state || sk->sk_state == state2);
274}
275
276/**
277 * iucv_below_msglim() - function to check if messages can be sent
278 * @sk: sock structure
279 *
280 * Returns true if the send queue length is lower than the message limit.
281 * Always returns true if the socket is not connected (no iucv path for
282 * checking the message limit).
283 */
284static inline int iucv_below_msglim(struct sock *sk)
285{
286 struct iucv_sock *iucv = iucv_sk(sk);
287
288 if (sk->sk_state != IUCV_CONNECTED)
289 return 1;
Ursula Braun3881ac42011-08-08 01:33:54 +0000290 if (iucv->transport == AF_IUCV_TRANS_IUCV)
291 return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim);
292 else
293 return ((atomic_read(&iucv->msg_sent) < iucv->msglimit_peer) &&
294 (atomic_read(&iucv->pendings) <= 0));
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000295}
296
297/**
298 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
299 */
300static void iucv_sock_wake_msglim(struct sock *sk)
301{
Eric Dumazet43815482010-04-29 11:01:49 +0000302 struct socket_wq *wq;
303
304 rcu_read_lock();
305 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +0800306 if (skwq_has_sleeper(wq))
Eric Dumazet43815482010-04-29 11:01:49 +0000307 wake_up_interruptible_all(&wq->wait);
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000308 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Eric Dumazet43815482010-04-29 11:01:49 +0000309 rcu_read_unlock();
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000310}
311
Ursula Braun3881ac42011-08-08 01:33:54 +0000312/**
313 * afiucv_hs_send() - send a message through HiperSockets transport
314 */
315static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
316 struct sk_buff *skb, u8 flags)
317{
Ursula Braun3881ac42011-08-08 01:33:54 +0000318 struct iucv_sock *iucv = iucv_sk(sock);
319 struct af_iucv_trans_hdr *phs_hdr;
320 struct sk_buff *nskb;
321 int err, confirm_recv = 0;
322
323 memset(skb->head, 0, ETH_HLEN);
Johannes Bergd58ff352017-06-16 14:29:23 +0200324 phs_hdr = skb_push(skb, sizeof(struct af_iucv_trans_hdr));
Ursula Braun3881ac42011-08-08 01:33:54 +0000325 skb_reset_mac_header(skb);
326 skb_reset_network_header(skb);
327 skb_push(skb, ETH_HLEN);
328 skb_reset_mac_header(skb);
329 memset(phs_hdr, 0, sizeof(struct af_iucv_trans_hdr));
330
331 phs_hdr->magic = ETH_P_AF_IUCV;
332 phs_hdr->version = 1;
333 phs_hdr->flags = flags;
334 if (flags == AF_IUCV_FLAG_SYN)
335 phs_hdr->window = iucv->msglimit;
336 else if ((flags == AF_IUCV_FLAG_WIN) || !flags) {
337 confirm_recv = atomic_read(&iucv->msg_recv);
338 phs_hdr->window = confirm_recv;
339 if (confirm_recv)
340 phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
341 }
342 memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
343 memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
344 memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
345 memcpy(phs_hdr->srcAppName, iucv->src_name, 8);
346 ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID));
347 ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName));
348 ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID));
349 ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName));
350 if (imsg)
351 memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message));
352
Ursula Braun800c5eb2012-02-08 00:19:47 +0000353 skb->dev = iucv->hs_dev;
Julian Wiedmannb2f54392018-09-05 16:55:11 +0200354 if (!skb->dev) {
355 err = -ENODEV;
356 goto err_free;
357 }
358 if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev)) {
359 err = -ENETDOWN;
360 goto err_free;
361 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000362 if (skb->len > skb->dev->mtu) {
Julian Wiedmannb2f54392018-09-05 16:55:11 +0200363 if (sock->sk_type == SOCK_SEQPACKET) {
364 err = -EMSGSIZE;
365 goto err_free;
366 }
367 skb_trim(skb, skb->dev->mtu);
Ursula Braun3881ac42011-08-08 01:33:54 +0000368 }
Hans Wippel2e56c262017-06-23 19:32:27 +0200369 skb->protocol = cpu_to_be16(ETH_P_AF_IUCV);
Ursula Braun3881ac42011-08-08 01:33:54 +0000370 nskb = skb_clone(skb, GFP_ATOMIC);
Julian Wiedmannb2f54392018-09-05 16:55:11 +0200371 if (!nskb) {
372 err = -ENOMEM;
373 goto err_free;
374 }
375
Ursula Braun3881ac42011-08-08 01:33:54 +0000376 skb_queue_tail(&iucv->send_skb_q, nskb);
377 err = dev_queue_xmit(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +0000378 if (net_xmit_eval(err)) {
Ursula Braun3881ac42011-08-08 01:33:54 +0000379 skb_unlink(nskb, &iucv->send_skb_q);
380 kfree_skb(nskb);
381 } else {
382 atomic_sub(confirm_recv, &iucv->msg_recv);
383 WARN_ON(atomic_read(&iucv->msg_recv) < 0);
384 }
Ursula Braun800c5eb2012-02-08 00:19:47 +0000385 return net_xmit_eval(err);
Julian Wiedmannb2f54392018-09-05 16:55:11 +0200386
387err_free:
388 kfree_skb(skb);
389 return err;
Ursula Braun3881ac42011-08-08 01:33:54 +0000390}
391
Jennifer Hunteac37312007-02-08 13:51:54 -0800392static struct sock *__iucv_get_sock_by_name(char *nm)
393{
394 struct sock *sk;
Jennifer Hunteac37312007-02-08 13:51:54 -0800395
Sasha Levinb67bfe02013-02-27 17:06:00 -0800396 sk_for_each(sk, &iucv_sk_list.head)
Jennifer Hunteac37312007-02-08 13:51:54 -0800397 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
398 return sk;
399
400 return NULL;
401}
402
403static void iucv_sock_destruct(struct sock *sk)
404{
405 skb_queue_purge(&sk->sk_receive_queue);
Ursula Braun82492a32012-03-07 02:06:24 +0000406 skb_queue_purge(&sk->sk_error_queue);
407
408 sk_mem_reclaim(sk);
409
410 if (!sock_flag(sk, SOCK_DEAD)) {
411 pr_err("Attempt to release alive iucv socket %p\n", sk);
412 return;
413 }
414
415 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
David S. Millerb2c9c5d2017-07-03 02:31:22 -0700416 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
Ursula Braun82492a32012-03-07 02:06:24 +0000417 WARN_ON(sk->sk_wmem_queued);
418 WARN_ON(sk->sk_forward_alloc);
Jennifer Hunteac37312007-02-08 13:51:54 -0800419}
420
421/* Cleanup Listen */
422static void iucv_sock_cleanup_listen(struct sock *parent)
423{
424 struct sock *sk;
425
426 /* Close non-accepted connections */
427 while ((sk = iucv_accept_dequeue(parent, NULL))) {
428 iucv_sock_close(sk);
429 iucv_sock_kill(sk);
430 }
431
432 parent->sk_state = IUCV_CLOSED;
Jennifer Hunteac37312007-02-08 13:51:54 -0800433}
434
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000435/* Kill socket (only if zapped and orphaned) */
Jennifer Hunteac37312007-02-08 13:51:54 -0800436static void iucv_sock_kill(struct sock *sk)
437{
438 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
439 return;
440
441 iucv_sock_unlink(&iucv_sk_list, sk);
442 sock_set_flag(sk, SOCK_DEAD);
443 sock_put(sk);
444}
445
Ursula Braun7d316b92012-02-08 00:19:45 +0000446/* Terminate an IUCV path */
447static void iucv_sever_path(struct sock *sk, int with_user_data)
448{
449 unsigned char user_data[16];
450 struct iucv_sock *iucv = iucv_sk(sk);
451 struct iucv_path *path = iucv->path;
452
453 if (iucv->path) {
454 iucv->path = NULL;
455 if (with_user_data) {
456 low_nmcpy(user_data, iucv->src_name);
457 high_nmcpy(user_data, iucv->dst_name);
458 ASCEBC(user_data, sizeof(user_data));
459 pr_iucv->path_sever(path, user_data);
460 } else
461 pr_iucv->path_sever(path, NULL);
462 iucv_path_free(path);
463 }
464}
465
Ursula Braun4e0ad322016-10-07 15:51:47 +0200466/* Send controlling flags through an IUCV socket for HIPER transport */
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000467static int iucv_send_ctrl(struct sock *sk, u8 flags)
468{
469 int err = 0;
470 int blen;
471 struct sk_buff *skb;
Ursula Braun4e0ad322016-10-07 15:51:47 +0200472 u8 shutdown = 0;
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000473
474 blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
Ursula Braun4e0ad322016-10-07 15:51:47 +0200475 if (sk->sk_shutdown & SEND_SHUTDOWN) {
476 /* controlling flags should be sent anyway */
477 shutdown = sk->sk_shutdown;
478 sk->sk_shutdown &= RCV_SHUTDOWN;
479 }
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000480 skb = sock_alloc_send_skb(sk, blen, 1, &err);
481 if (skb) {
482 skb_reserve(skb, blen);
483 err = afiucv_hs_send(NULL, sk, skb, flags);
484 }
Ursula Braun4e0ad322016-10-07 15:51:47 +0200485 if (shutdown)
486 sk->sk_shutdown = shutdown;
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000487 return err;
488}
489
Jennifer Hunteac37312007-02-08 13:51:54 -0800490/* Close an IUCV socket */
491static void iucv_sock_close(struct sock *sk)
492{
Jennifer Hunteac37312007-02-08 13:51:54 -0800493 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700494 unsigned long timeo;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000495 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -0800496
Jennifer Hunteac37312007-02-08 13:51:54 -0800497 lock_sock(sk);
498
Heiko Carstensda99f052007-05-04 12:23:27 -0700499 switch (sk->sk_state) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800500 case IUCV_LISTEN:
501 iucv_sock_cleanup_listen(sk);
502 break;
503
504 case IUCV_CONNECTED:
Ursula Braun3881ac42011-08-08 01:33:54 +0000505 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000506 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000507 sk->sk_state = IUCV_DISCONN;
508 sk->sk_state_change(sk);
509 }
Ursula Braun816abba2011-12-19 22:56:29 +0000510 case IUCV_DISCONN: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700511 sk->sk_state = IUCV_CLOSING;
512 sk->sk_state_change(sk);
513
Ursula Braun800c5eb2012-02-08 00:19:47 +0000514 if (!err && !skb_queue_empty(&iucv->send_skb_q)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -0700515 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
516 timeo = sk->sk_lingertime;
517 else
518 timeo = IUCV_DISCONN_TIMEOUT;
Ursula Braun9f6298a2011-05-12 18:45:08 +0000519 iucv_sock_wait(sk,
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000520 iucv_sock_in_state(sk, IUCV_CLOSED, 0),
521 timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700522 }
523
Ursula Braunbbe188c2009-04-21 06:04:20 +0000524 case IUCV_CLOSING: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700525 sk->sk_state = IUCV_CLOSED;
526 sk->sk_state_change(sk);
527
Jennifer Hunteac37312007-02-08 13:51:54 -0800528 sk->sk_err = ECONNRESET;
529 sk->sk_state_change(sk);
530
Ursula Braun800c5eb2012-02-08 00:19:47 +0000531 skb_queue_purge(&iucv->send_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700532 skb_queue_purge(&iucv->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800533
Ursula Braun7d316b92012-02-08 00:19:45 +0000534 default: /* fall through */
535 iucv_sever_path(sk, 1);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700536 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800537
Ursula Braun800c5eb2012-02-08 00:19:47 +0000538 if (iucv->hs_dev) {
539 dev_put(iucv->hs_dev);
540 iucv->hs_dev = NULL;
541 sk->sk_bound_dev_if = 0;
542 }
543
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000544 /* mark socket for deletion by iucv_sock_kill() */
545 sock_set_flag(sk, SOCK_ZAPPED);
546
Jennifer Hunteac37312007-02-08 13:51:54 -0800547 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800548}
549
550static void iucv_sock_init(struct sock *sk, struct sock *parent)
551{
Paul Moore02f06912016-06-07 10:30:53 -0400552 if (parent) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800553 sk->sk_type = parent->sk_type;
Paul Moore02f06912016-06-07 10:30:53 -0400554 security_sk_clone(parent, sk);
555 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800556}
557
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500558static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio, int kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800559{
560 struct sock *sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000561 struct iucv_sock *iucv;
Jennifer Hunteac37312007-02-08 13:51:54 -0800562
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500563 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, kern);
Jennifer Hunteac37312007-02-08 13:51:54 -0800564 if (!sk)
565 return NULL;
Ursula Braun493d3972011-08-08 01:33:52 +0000566 iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800567
568 sock_init_data(sock, sk);
Ursula Braun493d3972011-08-08 01:33:52 +0000569 INIT_LIST_HEAD(&iucv->accept_q);
570 spin_lock_init(&iucv->accept_q_lock);
571 skb_queue_head_init(&iucv->send_skb_q);
572 INIT_LIST_HEAD(&iucv->message_q.list);
573 spin_lock_init(&iucv->message_q.lock);
574 skb_queue_head_init(&iucv->backlog_skb_q);
575 iucv->send_tag = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000576 atomic_set(&iucv->pendings, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000577 iucv->flags = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000578 iucv->msglimit = 0;
579 atomic_set(&iucv->msg_sent, 0);
580 atomic_set(&iucv->msg_recv, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000581 iucv->path = NULL;
Ursula Braun3881ac42011-08-08 01:33:54 +0000582 iucv->sk_txnotify = afiucv_hs_callback_txnotify;
Ursula Braun493d3972011-08-08 01:33:52 +0000583 memset(&iucv->src_user_id , 0, 32);
Ursula Braun3881ac42011-08-08 01:33:54 +0000584 if (pr_iucv)
585 iucv->transport = AF_IUCV_TRANS_IUCV;
586 else
587 iucv->transport = AF_IUCV_TRANS_HIPER;
Jennifer Hunteac37312007-02-08 13:51:54 -0800588
589 sk->sk_destruct = iucv_sock_destruct;
590 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
591 sk->sk_allocation = GFP_DMA;
592
593 sock_reset_flag(sk, SOCK_ZAPPED);
594
595 sk->sk_protocol = proto;
596 sk->sk_state = IUCV_OPEN;
597
Jennifer Hunteac37312007-02-08 13:51:54 -0800598 iucv_sock_link(&iucv_sk_list, sk);
599 return sk;
600}
601
602/* Create an IUCV socket */
Eric Paris3f378b62009-11-05 22:18:14 -0800603static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
604 int kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800605{
606 struct sock *sk;
607
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000608 if (protocol && protocol != PF_IUCV)
609 return -EPROTONOSUPPORT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800610
611 sock->state = SS_UNCONNECTED;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000612
613 switch (sock->type) {
614 case SOCK_STREAM:
615 sock->ops = &iucv_sock_ops;
616 break;
617 case SOCK_SEQPACKET:
618 /* currently, proto ops can handle both sk types */
619 sock->ops = &iucv_sock_ops;
620 break;
621 default:
622 return -ESOCKTNOSUPPORT;
623 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800624
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500625 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL, kern);
Jennifer Hunteac37312007-02-08 13:51:54 -0800626 if (!sk)
627 return -ENOMEM;
628
629 iucv_sock_init(sk, NULL);
630
631 return 0;
632}
633
634void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
635{
636 write_lock_bh(&l->lock);
637 sk_add_node(sk, &l->head);
638 write_unlock_bh(&l->lock);
639}
640
641void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
642{
643 write_lock_bh(&l->lock);
644 sk_del_node_init(sk);
645 write_unlock_bh(&l->lock);
646}
647
648void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
649{
Ursula Braunfebca282007-07-14 19:04:25 -0700650 unsigned long flags;
651 struct iucv_sock *par = iucv_sk(parent);
652
Jennifer Hunteac37312007-02-08 13:51:54 -0800653 sock_hold(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700654 spin_lock_irqsave(&par->accept_q_lock, flags);
655 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
656 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800657 iucv_sk(sk)->parent = parent;
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000658 sk_acceptq_added(parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800659}
660
661void iucv_accept_unlink(struct sock *sk)
662{
Ursula Braunfebca282007-07-14 19:04:25 -0700663 unsigned long flags;
664 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
665
666 spin_lock_irqsave(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800667 list_del_init(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700668 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000669 sk_acceptq_removed(iucv_sk(sk)->parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800670 iucv_sk(sk)->parent = NULL;
671 sock_put(sk);
672}
673
674struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
675{
676 struct iucv_sock *isk, *n;
677 struct sock *sk;
678
Heiko Carstensda99f052007-05-04 12:23:27 -0700679 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800680 sk = (struct sock *) isk;
681 lock_sock(sk);
682
683 if (sk->sk_state == IUCV_CLOSED) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800684 iucv_accept_unlink(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700685 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800686 continue;
687 }
688
689 if (sk->sk_state == IUCV_CONNECTED ||
Ursula Braunaac63992011-12-19 22:56:31 +0000690 sk->sk_state == IUCV_DISCONN ||
Jennifer Hunteac37312007-02-08 13:51:54 -0800691 !newsock) {
692 iucv_accept_unlink(sk);
693 if (newsock)
694 sock_graft(sk, newsock);
695
Jennifer Hunteac37312007-02-08 13:51:54 -0800696 release_sock(sk);
697 return sk;
698 }
699
700 release_sock(sk);
701 }
702 return NULL;
703}
704
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200705static void __iucv_auto_name(struct iucv_sock *iucv)
706{
707 char name[12];
708
709 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
710 while (__iucv_get_sock_by_name(name)) {
711 sprintf(name, "%08x",
712 atomic_inc_return(&iucv_sk_list.autobind_name));
713 }
714 memcpy(iucv->src_name, name, 8);
715}
716
Jennifer Hunteac37312007-02-08 13:51:54 -0800717/* Bind an unbound socket */
718static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
719 int addr_len)
720{
721 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
722 struct sock *sk = sock->sk;
723 struct iucv_sock *iucv;
Ursula Braun3881ac42011-08-08 01:33:54 +0000724 int err = 0;
725 struct net_device *dev;
726 char uid[9];
Jennifer Hunteac37312007-02-08 13:51:54 -0800727
728 /* Verify the input sockaddr */
Mateusz Jurczyke3c42b62017-06-23 19:32:28 +0200729 if (addr_len < sizeof(struct sockaddr_iucv) ||
730 addr->sa_family != AF_IUCV)
Ursula Braun52a82e22016-01-19 10:41:33 +0100731 return -EINVAL;
732
Jennifer Hunteac37312007-02-08 13:51:54 -0800733 lock_sock(sk);
734 if (sk->sk_state != IUCV_OPEN) {
735 err = -EBADFD;
736 goto done;
737 }
738
739 write_lock_bh(&iucv_sk_list.lock);
740
741 iucv = iucv_sk(sk);
742 if (__iucv_get_sock_by_name(sa->siucv_name)) {
743 err = -EADDRINUSE;
744 goto done_unlock;
745 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000746 if (iucv->path)
Jennifer Hunteac37312007-02-08 13:51:54 -0800747 goto done_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -0800748
749 /* Bind the socket */
Ursula Braun3881ac42011-08-08 01:33:54 +0000750 if (pr_iucv)
751 if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
752 goto vm_bind; /* VM IUCV transport */
Jennifer Hunteac37312007-02-08 13:51:54 -0800753
Ursula Braun3881ac42011-08-08 01:33:54 +0000754 /* try hiper transport */
755 memcpy(uid, sa->siucv_user_id, sizeof(uid));
756 ASCEBC(uid, 8);
757 rcu_read_lock();
758 for_each_netdev_rcu(&init_net, dev) {
759 if (!memcmp(dev->perm_addr, uid, 8)) {
Ursula Braun3881ac42011-08-08 01:33:54 +0000760 memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200761 /* Check for unitialized siucv_name */
762 if (strncmp(sa->siucv_name, " ", 8) == 0)
763 __iucv_auto_name(iucv);
764 else
765 memcpy(iucv->src_name, sa->siucv_name, 8);
Ursula Braun816abba2011-12-19 22:56:29 +0000766 sk->sk_bound_dev_if = dev->ifindex;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000767 iucv->hs_dev = dev;
768 dev_hold(dev);
Ursula Braun3881ac42011-08-08 01:33:54 +0000769 sk->sk_state = IUCV_BOUND;
770 iucv->transport = AF_IUCV_TRANS_HIPER;
771 if (!iucv->msglimit)
772 iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
773 rcu_read_unlock();
774 goto done_unlock;
775 }
776 }
777 rcu_read_unlock();
778vm_bind:
779 if (pr_iucv) {
780 /* use local userid for backward compat */
781 memcpy(iucv->src_name, sa->siucv_name, 8);
782 memcpy(iucv->src_user_id, iucv_userid, 8);
783 sk->sk_state = IUCV_BOUND;
784 iucv->transport = AF_IUCV_TRANS_IUCV;
785 if (!iucv->msglimit)
786 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
787 goto done_unlock;
788 }
789 /* found no dev to bind */
790 err = -ENODEV;
Jennifer Hunteac37312007-02-08 13:51:54 -0800791done_unlock:
792 /* Release the socket list lock */
793 write_unlock_bh(&iucv_sk_list.lock);
794done:
795 release_sock(sk);
796 return err;
797}
798
799/* Automatically bind an unbound socket */
800static int iucv_sock_autobind(struct sock *sk)
801{
802 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800803 int err = 0;
804
Ursula Braunaac63992011-12-19 22:56:31 +0000805 if (unlikely(!pr_iucv))
Jennifer Hunteac37312007-02-08 13:51:54 -0800806 return -EPROTO;
807
Ursula Braunaac63992011-12-19 22:56:31 +0000808 memcpy(iucv->src_user_id, iucv_userid, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800809
810 write_lock_bh(&iucv_sk_list.lock);
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200811 __iucv_auto_name(iucv);
Jennifer Hunteac37312007-02-08 13:51:54 -0800812 write_unlock_bh(&iucv_sk_list.lock);
813
Ursula Braun3881ac42011-08-08 01:33:54 +0000814 if (!iucv->msglimit)
815 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
816
Jennifer Hunteac37312007-02-08 13:51:54 -0800817 return err;
818}
819
Ursula Braun3881ac42011-08-08 01:33:54 +0000820static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
Jennifer Hunteac37312007-02-08 13:51:54 -0800821{
822 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
823 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000824 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800825 unsigned char user_data[16];
826 int err;
827
Jennifer Hunteac37312007-02-08 13:51:54 -0800828 high_nmcpy(user_data, sa->siucv_name);
Ursula Braun493d3972011-08-08 01:33:52 +0000829 low_nmcpy(user_data, iucv->src_name);
Jennifer Hunteac37312007-02-08 13:51:54 -0800830 ASCEBC(user_data, sizeof(user_data));
831
Jennifer Hunteac37312007-02-08 13:51:54 -0800832 /* Create path. */
Hendrik Brueckner09488e22009-04-21 23:26:27 +0000833 iucv->path = iucv_path_alloc(iucv->msglimit,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000834 IUCV_IPRMDATA, GFP_KERNEL);
Ursula Braund4444722008-02-07 18:07:19 -0800835 if (!iucv->path) {
836 err = -ENOMEM;
837 goto done;
838 }
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000839 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
840 sa->siucv_user_id, NULL, user_data,
841 sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800842 if (err) {
843 iucv_path_free(iucv->path);
844 iucv->path = NULL;
Hendrik Brueckner55cdea92009-01-05 18:07:07 -0800845 switch (err) {
846 case 0x0b: /* Target communicator is not logged on */
847 err = -ENETUNREACH;
848 break;
849 case 0x0d: /* Max connections for this guest exceeded */
850 case 0x0e: /* Max connections for target guest exceeded */
851 err = -EAGAIN;
852 break;
853 case 0x0f: /* Missing IUCV authorization */
854 err = -EACCES;
855 break;
856 default:
857 err = -ECONNREFUSED;
858 break;
859 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000860 }
861done:
862 return err;
863}
864
865/* Connect an unconnected socket */
866static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
867 int alen, int flags)
868{
869 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
870 struct sock *sk = sock->sk;
871 struct iucv_sock *iucv = iucv_sk(sk);
872 int err;
873
Mateusz Jurczyke3c42b62017-06-23 19:32:28 +0200874 if (alen < sizeof(struct sockaddr_iucv) || addr->sa_family != AF_IUCV)
Ursula Braun3881ac42011-08-08 01:33:54 +0000875 return -EINVAL;
876
877 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
878 return -EBADFD;
879
880 if (sk->sk_state == IUCV_OPEN &&
881 iucv->transport == AF_IUCV_TRANS_HIPER)
882 return -EBADFD; /* explicit bind required */
883
884 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
885 return -EINVAL;
886
887 if (sk->sk_state == IUCV_OPEN) {
888 err = iucv_sock_autobind(sk);
889 if (unlikely(err))
890 return err;
Jennifer Hunteac37312007-02-08 13:51:54 -0800891 }
892
Ursula Braun3881ac42011-08-08 01:33:54 +0000893 lock_sock(sk);
894
895 /* Set the destination information */
896 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
897 memcpy(iucv->dst_name, sa->siucv_name, 8);
898
899 if (iucv->transport == AF_IUCV_TRANS_HIPER)
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000900 err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000901 else
902 err = afiucv_path_connect(sock, addr);
903 if (err)
904 goto done;
905
906 if (sk->sk_state != IUCV_CONNECTED)
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000907 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
908 IUCV_DISCONN),
909 sock_sndtimeo(sk, flags & O_NONBLOCK));
Jennifer Hunteac37312007-02-08 13:51:54 -0800910
Ursula Braun3881ac42011-08-08 01:33:54 +0000911 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000912 err = -ECONNREFUSED;
Ursula Braun18becbc2009-01-05 18:07:46 -0800913
Ursula Braun7d316b92012-02-08 00:19:45 +0000914 if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
915 iucv_sever_path(sk, 0);
Ursula Braun18becbc2009-01-05 18:07:46 -0800916
Jennifer Hunteac37312007-02-08 13:51:54 -0800917done:
918 release_sock(sk);
919 return err;
920}
921
922/* Move a socket into listening state. */
923static int iucv_sock_listen(struct socket *sock, int backlog)
924{
925 struct sock *sk = sock->sk;
926 int err;
927
928 lock_sock(sk);
929
930 err = -EINVAL;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000931 if (sk->sk_state != IUCV_BOUND)
932 goto done;
933
934 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800935 goto done;
936
937 sk->sk_max_ack_backlog = backlog;
938 sk->sk_ack_backlog = 0;
939 sk->sk_state = IUCV_LISTEN;
940 err = 0;
941
942done:
943 release_sock(sk);
944 return err;
945}
946
947/* Accept a pending connection */
948static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
David Howellscdfbabf2017-03-09 08:09:05 +0000949 int flags, bool kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800950{
951 DECLARE_WAITQUEUE(wait, current);
952 struct sock *sk = sock->sk, *nsk;
953 long timeo;
954 int err = 0;
955
Jennifer Hunt561e0362007-05-04 12:22:07 -0700956 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800957
958 if (sk->sk_state != IUCV_LISTEN) {
959 err = -EBADFD;
960 goto done;
961 }
962
963 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
964
965 /* Wait for an incoming connection */
Eric Dumazetaa395142010-04-20 13:03:51 +0000966 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Heiko Carstensda99f052007-05-04 12:23:27 -0700967 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800968 set_current_state(TASK_INTERRUPTIBLE);
969 if (!timeo) {
970 err = -EAGAIN;
971 break;
972 }
973
974 release_sock(sk);
975 timeo = schedule_timeout(timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700976 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800977
978 if (sk->sk_state != IUCV_LISTEN) {
979 err = -EBADFD;
980 break;
981 }
982
983 if (signal_pending(current)) {
984 err = sock_intr_errno(timeo);
985 break;
986 }
987 }
988
989 set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000990 remove_wait_queue(sk_sleep(sk), &wait);
Jennifer Hunteac37312007-02-08 13:51:54 -0800991
992 if (err)
993 goto done;
994
995 newsock->state = SS_CONNECTED;
996
997done:
998 release_sock(sk);
999 return err;
1000}
1001
1002static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001003 int peer)
Jennifer Hunteac37312007-02-08 13:51:54 -08001004{
1005 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
1006 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +00001007 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001008
1009 addr->sa_family = AF_IUCV;
Jennifer Hunteac37312007-02-08 13:51:54 -08001010
1011 if (peer) {
Ursula Braun493d3972011-08-08 01:33:52 +00001012 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
1013 memcpy(siucv->siucv_name, iucv->dst_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -08001014 } else {
Ursula Braun493d3972011-08-08 01:33:52 +00001015 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
1016 memcpy(siucv->siucv_name, iucv->src_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -08001017 }
1018 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
1019 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
Ursula Braun493d3972011-08-08 01:33:52 +00001020 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
Jennifer Hunteac37312007-02-08 13:51:54 -08001021
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001022 return sizeof(struct sockaddr_iucv);
Jennifer Hunteac37312007-02-08 13:51:54 -08001023}
1024
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001025/**
1026 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
1027 * @path: IUCV path
1028 * @msg: Pointer to a struct iucv_message
1029 * @skb: The socket data to send, skb->len MUST BE <= 7
1030 *
1031 * Send the socket data in the parameter list in the iucv message
1032 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
1033 * list and the socket data len at index 7 (last byte).
1034 * See also iucv_msg_length().
1035 *
1036 * Returns the error code from the iucv_message_send() call.
1037 */
1038static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
1039 struct sk_buff *skb)
1040{
1041 u8 prmdata[8];
1042
1043 memcpy(prmdata, (void *) skb->data, skb->len);
1044 prmdata[7] = 0xff - (u8) skb->len;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001045 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001046 (void *) prmdata, 8);
1047}
1048
Ying Xue1b784142015-03-02 15:37:48 +08001049static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg,
1050 size_t len)
Jennifer Hunteac37312007-02-08 13:51:54 -08001051{
1052 struct sock *sk = sock->sk;
1053 struct iucv_sock *iucv = iucv_sk(sk);
Julian Wiedmanndc5367b2017-01-10 17:10:34 +01001054 size_t headroom = 0;
1055 size_t linear;
Jennifer Hunteac37312007-02-08 13:51:54 -08001056 struct sk_buff *skb;
Eugene Crosser0506eb02015-12-11 12:27:50 +01001057 struct iucv_message txmsg = {0};
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001058 struct cmsghdr *cmsg;
1059 int cmsg_done;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001060 long timeo;
Ursula Braun8f7c5022008-12-25 13:39:47 +01001061 char user_id[9];
1062 char appl_id[9];
Jennifer Hunteac37312007-02-08 13:51:54 -08001063 int err;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001064 int noblock = msg->msg_flags & MSG_DONTWAIT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001065
1066 err = sock_error(sk);
1067 if (err)
1068 return err;
1069
1070 if (msg->msg_flags & MSG_OOB)
1071 return -EOPNOTSUPP;
1072
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001073 /* SOCK_SEQPACKET: we do not support segmented records */
1074 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
1075 return -EOPNOTSUPP;
1076
Jennifer Hunteac37312007-02-08 13:51:54 -08001077 lock_sock(sk);
1078
1079 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1080 err = -EPIPE;
1081 goto out;
1082 }
1083
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001084 /* Return if the socket is not in connected state */
1085 if (sk->sk_state != IUCV_CONNECTED) {
1086 err = -ENOTCONN;
1087 goto out;
1088 }
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001089
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001090 /* initialize defaults */
1091 cmsg_done = 0; /* check for duplicate headers */
1092 txmsg.class = 0;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001093
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001094 /* iterate over control messages */
Gu Zhengf95b4142014-12-11 11:22:04 +08001095 for_each_cmsghdr(cmsg, msg) {
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001096 if (!CMSG_OK(msg, cmsg)) {
1097 err = -EINVAL;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001098 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08001099 }
1100
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001101 if (cmsg->cmsg_level != SOL_IUCV)
1102 continue;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001103
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001104 if (cmsg->cmsg_type & cmsg_done) {
1105 err = -EINVAL;
1106 goto out;
1107 }
1108 cmsg_done |= cmsg->cmsg_type;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001109
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001110 switch (cmsg->cmsg_type) {
1111 case SCM_IUCV_TRGCLS:
1112 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
1113 err = -EINVAL;
1114 goto out;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001115 }
1116
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001117 /* set iucv message target class */
1118 memcpy(&txmsg.class,
1119 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
1120
1121 break;
1122
1123 default:
1124 err = -EINVAL;
1125 goto out;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001126 }
1127 }
1128
1129 /* allocate one skb for each iucv message:
1130 * this is fine for SOCK_SEQPACKET (unless we want to support
1131 * segmented records using the MSG_EOR flag), but
1132 * for SOCK_STREAM we might want to improve it in future */
Julian Wiedmanndc5367b2017-01-10 17:10:34 +01001133 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1134 headroom = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
Eugene Crossere5374392016-06-13 18:46:14 +02001135 linear = len;
1136 } else {
Julian Wiedmanndc5367b2017-01-10 17:10:34 +01001137 if (len < PAGE_SIZE) {
1138 linear = len;
1139 } else {
1140 /* In nonlinear "classic" iucv skb,
1141 * reserve space for iucv_array
1142 */
1143 headroom = sizeof(struct iucv_array) *
1144 (MAX_SKB_FRAGS + 1);
1145 linear = PAGE_SIZE - headroom;
1146 }
Eugene Crossere5374392016-06-13 18:46:14 +02001147 }
1148 skb = sock_alloc_send_pskb(sk, headroom + linear, len - linear,
1149 noblock, &err, 0);
Eugene Crossered4ac422015-03-30 15:40:42 +02001150 if (!skb)
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001151 goto out;
Eugene Crossere5374392016-06-13 18:46:14 +02001152 if (headroom)
1153 skb_reserve(skb, headroom);
1154 skb_put(skb, linear);
1155 skb->len = len;
1156 skb->data_len = len - linear;
1157 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
1158 if (err)
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001159 goto fail;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001160
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001161 /* wait if outstanding messages for iucv path has reached */
1162 timeo = sock_sndtimeo(sk, noblock);
1163 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
1164 if (err)
1165 goto fail;
1166
1167 /* return -ECONNRESET if the socket is no longer connected */
1168 if (sk->sk_state != IUCV_CONNECTED) {
1169 err = -ECONNRESET;
1170 goto fail;
1171 }
1172
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001173 /* increment and save iucv message tag for msg_completion cbk */
1174 txmsg.tag = iucv->send_tag++;
Ursula Braunf9c41a62013-04-07 22:19:26 +00001175 IUCV_SKB_CB(skb)->tag = txmsg.tag;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001176
Ursula Braun3881ac42011-08-08 01:33:54 +00001177 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1178 atomic_inc(&iucv->msg_sent);
1179 err = afiucv_hs_send(&txmsg, sk, skb, 0);
1180 if (err) {
1181 atomic_dec(&iucv->msg_sent);
Julian Wiedmannb2f54392018-09-05 16:55:11 +02001182 goto out;
Ursula Braun3881ac42011-08-08 01:33:54 +00001183 }
Eugene Crossere5374392016-06-13 18:46:14 +02001184 } else { /* Classic VM IUCV transport */
1185 skb_queue_tail(&iucv->send_skb_q, skb);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001186
Eugene Crossere5374392016-06-13 18:46:14 +02001187 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags) &&
1188 skb->len <= 7) {
1189 err = iucv_send_iprm(iucv->path, &txmsg, skb);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001190
Eugene Crossere5374392016-06-13 18:46:14 +02001191 /* on success: there is no message_complete callback */
1192 /* for an IPRMDATA msg; remove skb from send queue */
1193 if (err == 0) {
1194 skb_unlink(skb, &iucv->send_skb_q);
1195 kfree_skb(skb);
1196 }
1197
1198 /* this error should never happen since the */
1199 /* IUCV_IPRMDATA path flag is set... sever path */
1200 if (err == 0x15) {
1201 pr_iucv->path_sever(iucv->path, NULL);
1202 skb_unlink(skb, &iucv->send_skb_q);
1203 err = -EPIPE;
1204 goto fail;
1205 }
1206 } else if (skb_is_nonlinear(skb)) {
1207 struct iucv_array *iba = (struct iucv_array *)skb->head;
1208 int i;
1209
1210 /* skip iucv_array lying in the headroom */
1211 iba[0].address = (u32)(addr_t)skb->data;
1212 iba[0].length = (u32)skb_headlen(skb);
1213 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1214 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1215
1216 iba[i + 1].address =
1217 (u32)(addr_t)skb_frag_address(frag);
1218 iba[i + 1].length = (u32)skb_frag_size(frag);
1219 }
1220 err = pr_iucv->message_send(iucv->path, &txmsg,
1221 IUCV_IPBUFLST, 0,
1222 (void *)iba, skb->len);
1223 } else { /* non-IPRM Linear skb */
1224 err = pr_iucv->message_send(iucv->path, &txmsg,
1225 0, 0, (void *)skb->data, skb->len);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001226 }
Eugene Crossere5374392016-06-13 18:46:14 +02001227 if (err) {
1228 if (err == 3) {
1229 user_id[8] = 0;
1230 memcpy(user_id, iucv->dst_user_id, 8);
1231 appl_id[8] = 0;
1232 memcpy(appl_id, iucv->dst_name, 8);
1233 pr_err(
1234 "Application %s on z/VM guest %s exceeds message limit\n",
1235 appl_id, user_id);
1236 err = -EAGAIN;
1237 } else {
1238 err = -EPIPE;
1239 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001240 skb_unlink(skb, &iucv->send_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -08001241 goto fail;
1242 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001243 }
1244
1245 release_sock(sk);
1246 return len;
1247
1248fail:
1249 kfree_skb(skb);
1250out:
1251 release_sock(sk);
1252 return err;
1253}
1254
Eugene Crossera006353a92016-06-13 18:46:16 +02001255static struct sk_buff *alloc_iucv_recv_skb(unsigned long len)
1256{
1257 size_t headroom, linear;
1258 struct sk_buff *skb;
1259 int err;
1260
1261 if (len < PAGE_SIZE) {
1262 headroom = 0;
1263 linear = len;
1264 } else {
1265 headroom = sizeof(struct iucv_array) * (MAX_SKB_FRAGS + 1);
1266 linear = PAGE_SIZE - headroom;
1267 }
1268 skb = alloc_skb_with_frags(headroom + linear, len - linear,
1269 0, &err, GFP_ATOMIC | GFP_DMA);
1270 WARN_ONCE(!skb,
1271 "alloc of recv iucv skb len=%lu failed with errcode=%d\n",
1272 len, err);
1273 if (skb) {
1274 if (headroom)
1275 skb_reserve(skb, headroom);
1276 skb_put(skb, linear);
1277 skb->len = len;
1278 skb->data_len = len - linear;
1279 }
1280 return skb;
1281}
1282
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001283/* iucv_process_message() - Receive a single outstanding IUCV message
1284 *
1285 * Locking: must be called with message_q.lock held
1286 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001287static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1288 struct iucv_path *path,
1289 struct iucv_message *msg)
1290{
1291 int rc;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001292 unsigned int len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001293
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001294 len = iucv_msg_length(msg);
1295
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001296 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1297 /* Note: the first 4 bytes are reserved for msg tag */
Ursula Braunf9c41a62013-04-07 22:19:26 +00001298 IUCV_SKB_CB(skb)->class = msg->class;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001299
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001300 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1301 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1302 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1303 skb->data = NULL;
1304 skb->len = 0;
1305 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001306 } else {
Eugene Crossera006353a92016-06-13 18:46:16 +02001307 if (skb_is_nonlinear(skb)) {
1308 struct iucv_array *iba = (struct iucv_array *)skb->head;
1309 int i;
1310
1311 iba[0].address = (u32)(addr_t)skb->data;
1312 iba[0].length = (u32)skb_headlen(skb);
1313 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1314 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1315
1316 iba[i + 1].address =
1317 (u32)(addr_t)skb_frag_address(frag);
1318 iba[i + 1].length = (u32)skb_frag_size(frag);
1319 }
1320 rc = pr_iucv->message_receive(path, msg,
1321 IUCV_IPBUFLST,
1322 (void *)iba, len, NULL);
1323 } else {
1324 rc = pr_iucv->message_receive(path, msg,
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001325 msg->flags & IUCV_IPRMDATA,
1326 skb->data, len, NULL);
Eugene Crossera006353a92016-06-13 18:46:16 +02001327 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001328 if (rc) {
1329 kfree_skb(skb);
1330 return;
1331 }
Eugene Crossera006353a92016-06-13 18:46:16 +02001332 WARN_ON_ONCE(skb->len != len);
Ursula Braunf0703c82007-10-08 02:03:31 -07001333 }
1334
Ursula Braunf9c41a62013-04-07 22:19:26 +00001335 IUCV_SKB_CB(skb)->offset = 0;
Ursula Braun8c68b1a2016-10-07 15:51:48 +02001336 if (sk_filter(sk, skb)) {
1337 atomic_inc(&sk->sk_drops); /* skb rejected by filter */
1338 kfree_skb(skb);
1339 return;
1340 }
1341 if (__sock_queue_rcv_skb(sk, skb)) /* handle rcv queue full */
1342 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
Ursula Braunf0703c82007-10-08 02:03:31 -07001343}
1344
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001345/* iucv_process_message_q() - Process outstanding IUCV messages
1346 *
1347 * Locking: must be called with message_q.lock held
1348 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001349static void iucv_process_message_q(struct sock *sk)
1350{
1351 struct iucv_sock *iucv = iucv_sk(sk);
1352 struct sk_buff *skb;
1353 struct sock_msg_q *p, *n;
1354
1355 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
Eugene Crossera006353a92016-06-13 18:46:16 +02001356 skb = alloc_iucv_recv_skb(iucv_msg_length(&p->msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001357 if (!skb)
1358 break;
1359 iucv_process_message(sk, skb, p->path, &p->msg);
1360 list_del(&p->list);
1361 kfree(p);
1362 if (!skb_queue_empty(&iucv->backlog_skb_q))
1363 break;
1364 }
1365}
1366
Ying Xue1b784142015-03-02 15:37:48 +08001367static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1368 size_t len, int flags)
Jennifer Hunteac37312007-02-08 13:51:54 -08001369{
1370 int noblock = flags & MSG_DONTWAIT;
1371 struct sock *sk = sock->sk;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001372 struct iucv_sock *iucv = iucv_sk(sk);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001373 unsigned int copied, rlen;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001374 struct sk_buff *skb, *rskb, *cskb;
Jennifer Hunteac37312007-02-08 13:51:54 -08001375 int err = 0;
Ursula Braunf9c41a62013-04-07 22:19:26 +00001376 u32 offset;
Jennifer Hunteac37312007-02-08 13:51:54 -08001377
Ursula Braunaac63992011-12-19 22:56:31 +00001378 if ((sk->sk_state == IUCV_DISCONN) &&
Ursula Braunf0703c82007-10-08 02:03:31 -07001379 skb_queue_empty(&iucv->backlog_skb_q) &&
1380 skb_queue_empty(&sk->sk_receive_queue) &&
1381 list_empty(&iucv->message_q.list))
Jennifer Hunt561e0362007-05-04 12:22:07 -07001382 return 0;
1383
Jennifer Hunteac37312007-02-08 13:51:54 -08001384 if (flags & (MSG_OOB))
1385 return -EOPNOTSUPP;
1386
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001387 /* receive/dequeue next skb:
1388 * the function understands MSG_PEEK and, thus, does not dequeue skb */
Jennifer Hunteac37312007-02-08 13:51:54 -08001389 skb = skb_recv_datagram(sk, flags, noblock, &err);
1390 if (!skb) {
1391 if (sk->sk_shutdown & RCV_SHUTDOWN)
1392 return 0;
1393 return err;
1394 }
1395
Ursula Braunf9c41a62013-04-07 22:19:26 +00001396 offset = IUCV_SKB_CB(skb)->offset;
1397 rlen = skb->len - offset; /* real length of skb */
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001398 copied = min_t(unsigned int, rlen, len);
Ursula Braun82492a32012-03-07 02:06:24 +00001399 if (!rlen)
1400 sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001401
Jennifer Hunt561e0362007-05-04 12:22:07 -07001402 cskb = skb;
David S. Miller51f3d022014-11-05 16:46:40 -05001403 if (skb_copy_datagram_msg(cskb, offset, msg, copied)) {
Hendrik Brueckner802788b2009-04-21 23:26:26 +00001404 if (!(flags & MSG_PEEK))
1405 skb_queue_head(&sk->sk_receive_queue, skb);
1406 return -EFAULT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001407 }
1408
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001409 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1410 if (sk->sk_type == SOCK_SEQPACKET) {
1411 if (copied < rlen)
1412 msg->msg_flags |= MSG_TRUNC;
1413 /* each iucv message contains a complete record */
1414 msg->msg_flags |= MSG_EOR;
1415 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001416
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001417 /* create control message to store iucv msg target class:
1418 * get the trgcls from the control buffer of the skb due to
1419 * fragmentation of original iucv message. */
1420 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
Ursula Braunf9c41a62013-04-07 22:19:26 +00001421 sizeof(IUCV_SKB_CB(skb)->class),
1422 (void *)&IUCV_SKB_CB(skb)->class);
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001423 if (err) {
1424 if (!(flags & MSG_PEEK))
1425 skb_queue_head(&sk->sk_receive_queue, skb);
1426 return err;
1427 }
1428
Jennifer Hunteac37312007-02-08 13:51:54 -08001429 /* Mark read part of skb as used */
1430 if (!(flags & MSG_PEEK)) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001431
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001432 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1433 if (sk->sk_type == SOCK_STREAM) {
Ursula Braunf9c41a62013-04-07 22:19:26 +00001434 if (copied < rlen) {
1435 IUCV_SKB_CB(skb)->offset = offset + copied;
Ursula Braun2f139a52014-03-19 07:57:59 +01001436 skb_queue_head(&sk->sk_receive_queue, skb);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001437 goto done;
1438 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001439 }
1440
1441 kfree_skb(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00001442 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1443 atomic_inc(&iucv->msg_recv);
1444 if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
1445 WARN_ON(1);
1446 iucv_sock_close(sk);
1447 return -EFAULT;
1448 }
1449 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001450
1451 /* Queue backlog skbs */
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001452 spin_lock_bh(&iucv->message_q.lock);
Ursula Braunf0703c82007-10-08 02:03:31 -07001453 rskb = skb_dequeue(&iucv->backlog_skb_q);
Heiko Carstensda99f052007-05-04 12:23:27 -07001454 while (rskb) {
Ursula Braunf9c41a62013-04-07 22:19:26 +00001455 IUCV_SKB_CB(rskb)->offset = 0;
Ursula Braun8c68b1a2016-10-07 15:51:48 +02001456 if (__sock_queue_rcv_skb(sk, rskb)) {
1457 /* handle rcv queue full */
Ursula Braunf0703c82007-10-08 02:03:31 -07001458 skb_queue_head(&iucv->backlog_skb_q,
Jennifer Hunt561e0362007-05-04 12:22:07 -07001459 rskb);
1460 break;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001461 }
Ursula Braun8c68b1a2016-10-07 15:51:48 +02001462 rskb = skb_dequeue(&iucv->backlog_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001463 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001464 if (skb_queue_empty(&iucv->backlog_skb_q)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001465 if (!list_empty(&iucv->message_q.list))
1466 iucv_process_message_q(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001467 if (atomic_read(&iucv->msg_recv) >=
1468 iucv->msglimit / 2) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001469 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
Ursula Braun3881ac42011-08-08 01:33:54 +00001470 if (err) {
1471 sk->sk_state = IUCV_DISCONN;
1472 sk->sk_state_change(sk);
1473 }
1474 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001475 }
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001476 spin_unlock_bh(&iucv->message_q.lock);
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001477 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001478
1479done:
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001480 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1481 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1482 copied = rlen;
1483
1484 return copied;
Jennifer Hunteac37312007-02-08 13:51:54 -08001485}
1486
Al Viroade994f2017-07-03 00:01:49 -04001487static inline __poll_t iucv_accept_poll(struct sock *parent)
Jennifer Hunteac37312007-02-08 13:51:54 -08001488{
1489 struct iucv_sock *isk, *n;
1490 struct sock *sk;
1491
Heiko Carstensda99f052007-05-04 12:23:27 -07001492 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001493 sk = (struct sock *) isk;
1494
1495 if (sk->sk_state == IUCV_CONNECTED)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001496 return EPOLLIN | EPOLLRDNORM;
Jennifer Hunteac37312007-02-08 13:51:54 -08001497 }
1498
1499 return 0;
1500}
1501
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001502__poll_t iucv_sock_poll(struct file *file, struct socket *sock,
1503 poll_table *wait)
Jennifer Hunteac37312007-02-08 13:51:54 -08001504{
1505 struct sock *sk = sock->sk;
Al Viroade994f2017-07-03 00:01:49 -04001506 __poll_t mask = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -08001507
Christoph Hellwigdd979b42018-07-30 09:42:10 +02001508 sock_poll_wait(file, wait);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001509
Jennifer Hunteac37312007-02-08 13:51:54 -08001510 if (sk->sk_state == IUCV_LISTEN)
1511 return iucv_accept_poll(sk);
1512
1513 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001514 mask |= EPOLLERR |
1515 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
Jennifer Hunteac37312007-02-08 13:51:54 -08001516
1517 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001518 mask |= EPOLLRDHUP;
Jennifer Hunteac37312007-02-08 13:51:54 -08001519
1520 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001521 mask |= EPOLLHUP;
Jennifer Hunteac37312007-02-08 13:51:54 -08001522
1523 if (!skb_queue_empty(&sk->sk_receive_queue) ||
Heiko Carstensda99f052007-05-04 12:23:27 -07001524 (sk->sk_shutdown & RCV_SHUTDOWN))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001525 mask |= EPOLLIN | EPOLLRDNORM;
Jennifer Hunteac37312007-02-08 13:51:54 -08001526
1527 if (sk->sk_state == IUCV_CLOSED)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001528 mask |= EPOLLHUP;
Jennifer Hunteac37312007-02-08 13:51:54 -08001529
Ursula Braunaac63992011-12-19 22:56:31 +00001530 if (sk->sk_state == IUCV_DISCONN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001531 mask |= EPOLLIN;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001532
Ursula Braun7f1b0ea2012-02-08 00:19:46 +00001533 if (sock_writeable(sk) && iucv_below_msglim(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001534 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
Jennifer Hunteac37312007-02-08 13:51:54 -08001535 else
Eric Dumazet9cd3e072015-11-29 20:03:10 -08001536 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001537
1538 return mask;
1539}
1540
1541static int iucv_sock_shutdown(struct socket *sock, int how)
1542{
1543 struct sock *sk = sock->sk;
1544 struct iucv_sock *iucv = iucv_sk(sk);
1545 struct iucv_message txmsg;
1546 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -08001547
1548 how++;
1549
1550 if ((how & ~SHUTDOWN_MASK) || !how)
1551 return -EINVAL;
1552
1553 lock_sock(sk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001554 switch (sk->sk_state) {
Ursula Braun82492a32012-03-07 02:06:24 +00001555 case IUCV_LISTEN:
Hendrik Bruecknere14ad5f2009-04-21 06:04:23 +00001556 case IUCV_DISCONN:
1557 case IUCV_CLOSING:
Jennifer Hunteac37312007-02-08 13:51:54 -08001558 case IUCV_CLOSED:
1559 err = -ENOTCONN;
1560 goto fail;
Jennifer Hunteac37312007-02-08 13:51:54 -08001561 default:
Jennifer Hunteac37312007-02-08 13:51:54 -08001562 break;
1563 }
1564
1565 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun82492a32012-03-07 02:06:24 +00001566 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1567 txmsg.class = 0;
1568 txmsg.tag = 0;
1569 err = pr_iucv->message_send(iucv->path, &txmsg,
1570 IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8);
1571 if (err) {
1572 switch (err) {
1573 case 1:
1574 err = -ENOTCONN;
1575 break;
1576 case 2:
1577 err = -ECONNRESET;
1578 break;
1579 default:
1580 err = -ENOTCONN;
1581 break;
1582 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001583 }
Ursula Braun82492a32012-03-07 02:06:24 +00001584 } else
1585 iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
Jennifer Hunteac37312007-02-08 13:51:54 -08001586 }
1587
Ursula Braun82492a32012-03-07 02:06:24 +00001588 sk->sk_shutdown |= how;
Jennifer Hunteac37312007-02-08 13:51:54 -08001589 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun1042cab2014-07-21 12:54:44 +02001590 if ((iucv->transport == AF_IUCV_TRANS_IUCV) &&
1591 iucv->path) {
Ursula Braun82492a32012-03-07 02:06:24 +00001592 err = pr_iucv->path_quiesce(iucv->path, NULL);
1593 if (err)
1594 err = -ENOTCONN;
1595/* skb_queue_purge(&sk->sk_receive_queue); */
1596 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001597 skb_queue_purge(&sk->sk_receive_queue);
1598 }
1599
1600 /* Wake up anyone sleeping in poll */
1601 sk->sk_state_change(sk);
1602
1603fail:
1604 release_sock(sk);
1605 return err;
1606}
1607
1608static int iucv_sock_release(struct socket *sock)
1609{
1610 struct sock *sk = sock->sk;
1611 int err = 0;
1612
1613 if (!sk)
1614 return 0;
1615
1616 iucv_sock_close(sk);
1617
Jennifer Hunteac37312007-02-08 13:51:54 -08001618 sock_orphan(sk);
1619 iucv_sock_kill(sk);
1620 return err;
1621}
1622
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001623/* getsockopt and setsockopt */
1624static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001625 char __user *optval, unsigned int optlen)
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001626{
1627 struct sock *sk = sock->sk;
1628 struct iucv_sock *iucv = iucv_sk(sk);
1629 int val;
1630 int rc;
1631
1632 if (level != SOL_IUCV)
1633 return -ENOPROTOOPT;
1634
1635 if (optlen < sizeof(int))
1636 return -EINVAL;
1637
1638 if (get_user(val, (int __user *) optval))
1639 return -EFAULT;
1640
1641 rc = 0;
1642
1643 lock_sock(sk);
1644 switch (optname) {
1645 case SO_IPRMDATA_MSG:
1646 if (val)
1647 iucv->flags |= IUCV_IPRMDATA;
1648 else
1649 iucv->flags &= ~IUCV_IPRMDATA;
1650 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001651 case SO_MSGLIMIT:
1652 switch (sk->sk_state) {
1653 case IUCV_OPEN:
1654 case IUCV_BOUND:
1655 if (val < 1 || val > (u16)(~0))
1656 rc = -EINVAL;
1657 else
1658 iucv->msglimit = val;
1659 break;
1660 default:
1661 rc = -EINVAL;
1662 break;
1663 }
1664 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001665 default:
1666 rc = -ENOPROTOOPT;
1667 break;
1668 }
1669 release_sock(sk);
1670
1671 return rc;
1672}
1673
1674static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1675 char __user *optval, int __user *optlen)
1676{
1677 struct sock *sk = sock->sk;
1678 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braun51363b82012-02-08 00:19:48 +00001679 unsigned int val;
1680 int len;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001681
1682 if (level != SOL_IUCV)
1683 return -ENOPROTOOPT;
1684
1685 if (get_user(len, optlen))
1686 return -EFAULT;
1687
1688 if (len < 0)
1689 return -EINVAL;
1690
1691 len = min_t(unsigned int, len, sizeof(int));
1692
1693 switch (optname) {
1694 case SO_IPRMDATA_MSG:
1695 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1696 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001697 case SO_MSGLIMIT:
1698 lock_sock(sk);
1699 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1700 : iucv->msglimit; /* default */
1701 release_sock(sk);
1702 break;
Ursula Braun51363b82012-02-08 00:19:48 +00001703 case SO_MSGSIZE:
1704 if (sk->sk_state == IUCV_OPEN)
1705 return -EBADFD;
1706 val = (iucv->hs_dev) ? iucv->hs_dev->mtu -
1707 sizeof(struct af_iucv_trans_hdr) - ETH_HLEN :
1708 0x7fffffff;
1709 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001710 default:
1711 return -ENOPROTOOPT;
1712 }
1713
1714 if (put_user(len, optlen))
1715 return -EFAULT;
1716 if (copy_to_user(optval, &val, len))
1717 return -EFAULT;
1718
1719 return 0;
1720}
1721
1722
Jennifer Hunteac37312007-02-08 13:51:54 -08001723/* Callback wrappers - called from iucv base support */
1724static int iucv_callback_connreq(struct iucv_path *path,
1725 u8 ipvmid[8], u8 ipuser[16])
1726{
1727 unsigned char user_data[16];
1728 unsigned char nuser_data[16];
1729 unsigned char src_name[8];
Jennifer Hunteac37312007-02-08 13:51:54 -08001730 struct sock *sk, *nsk;
1731 struct iucv_sock *iucv, *niucv;
1732 int err;
1733
1734 memcpy(src_name, ipuser, 8);
1735 EBCASC(src_name, 8);
1736 /* Find out if this path belongs to af_iucv. */
1737 read_lock(&iucv_sk_list.lock);
1738 iucv = NULL;
Ursula Braunfebca282007-07-14 19:04:25 -07001739 sk = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001740 sk_for_each(sk, &iucv_sk_list.head)
Jennifer Hunteac37312007-02-08 13:51:54 -08001741 if (sk->sk_state == IUCV_LISTEN &&
1742 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1743 /*
1744 * Found a listening socket with
1745 * src_name == ipuser[0-7].
1746 */
1747 iucv = iucv_sk(sk);
1748 break;
1749 }
1750 read_unlock(&iucv_sk_list.lock);
1751 if (!iucv)
1752 /* No socket found, not one of our paths. */
1753 return -EINVAL;
1754
1755 bh_lock_sock(sk);
1756
1757 /* Check if parent socket is listening */
1758 low_nmcpy(user_data, iucv->src_name);
1759 high_nmcpy(user_data, iucv->dst_name);
1760 ASCEBC(user_data, sizeof(user_data));
1761 if (sk->sk_state != IUCV_LISTEN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001762 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001763 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001764 goto fail;
1765 }
1766
1767 /* Check for backlog size */
1768 if (sk_acceptq_is_full(sk)) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001769 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001770 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001771 goto fail;
1772 }
1773
1774 /* Create the new socket */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001775 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
Heiko Carstensda99f052007-05-04 12:23:27 -07001776 if (!nsk) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001777 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001778 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001779 goto fail;
1780 }
1781
1782 niucv = iucv_sk(nsk);
1783 iucv_sock_init(nsk, sk);
1784
1785 /* Set the new iucv_sock */
1786 memcpy(niucv->dst_name, ipuser + 8, 8);
1787 EBCASC(niucv->dst_name, 8);
1788 memcpy(niucv->dst_user_id, ipvmid, 8);
1789 memcpy(niucv->src_name, iucv->src_name, 8);
1790 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1791 niucv->path = path;
1792
1793 /* Call iucv_accept */
1794 high_nmcpy(nuser_data, ipuser + 8);
1795 memcpy(nuser_data + 8, niucv->src_name, 8);
1796 ASCEBC(nuser_data + 8, 8);
1797
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001798 /* set message limit for path based on msglimit of accepting socket */
1799 niucv->msglimit = iucv->msglimit;
1800 path->msglim = iucv->msglimit;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001801 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001802 if (err) {
Ursula Braun7d316b92012-02-08 00:19:45 +00001803 iucv_sever_path(nsk, 1);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001804 iucv_sock_kill(nsk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001805 goto fail;
1806 }
1807
1808 iucv_accept_enqueue(sk, nsk);
1809
1810 /* Wake up accept */
1811 nsk->sk_state = IUCV_CONNECTED;
David S. Miller676d2362014-04-11 16:15:36 -04001812 sk->sk_data_ready(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001813 err = 0;
1814fail:
1815 bh_unlock_sock(sk);
1816 return 0;
1817}
1818
1819static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1820{
1821 struct sock *sk = path->private;
1822
1823 sk->sk_state = IUCV_CONNECTED;
1824 sk->sk_state_change(sk);
1825}
1826
1827static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1828{
1829 struct sock *sk = path->private;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001830 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braunf0703c82007-10-08 02:03:31 -07001831 struct sk_buff *skb;
1832 struct sock_msg_q *save_msg;
1833 int len;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001834
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001835 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001836 pr_iucv->message_reject(path, msg);
Jennifer Hunteac37312007-02-08 13:51:54 -08001837 return;
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001838 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001839
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001840 spin_lock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001841
Ursula Braunf0703c82007-10-08 02:03:31 -07001842 if (!list_empty(&iucv->message_q.list) ||
1843 !skb_queue_empty(&iucv->backlog_skb_q))
1844 goto save_message;
1845
1846 len = atomic_read(&sk->sk_rmem_alloc);
Eric Dumazet87fb4b72011-10-13 07:28:54 +00001847 len += SKB_TRUESIZE(iucv_msg_length(msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001848 if (len > sk->sk_rcvbuf)
1849 goto save_message;
1850
Eugene Crossera006353a92016-06-13 18:46:16 +02001851 skb = alloc_iucv_recv_skb(iucv_msg_length(msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001852 if (!skb)
1853 goto save_message;
Jennifer Hunteac37312007-02-08 13:51:54 -08001854
Ursula Braunf0703c82007-10-08 02:03:31 -07001855 iucv_process_message(sk, skb, path, msg);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001856 goto out_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -08001857
Ursula Braunf0703c82007-10-08 02:03:31 -07001858save_message:
1859 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
Ursula Braund4444722008-02-07 18:07:19 -08001860 if (!save_msg)
Julia Lawalla56635a2010-05-26 05:56:48 +00001861 goto out_unlock;
Ursula Braunf0703c82007-10-08 02:03:31 -07001862 save_msg->path = path;
1863 save_msg->msg = *msg;
1864
Ursula Braunf0703c82007-10-08 02:03:31 -07001865 list_add_tail(&save_msg->list, &iucv->message_q.list);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001866
1867out_unlock:
Ursula Braunf0703c82007-10-08 02:03:31 -07001868 spin_unlock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001869}
1870
1871static void iucv_callback_txdone(struct iucv_path *path,
1872 struct iucv_message *msg)
1873{
1874 struct sock *sk = path->private;
Ursula Braunf2a77992008-02-07 18:07:44 -08001875 struct sk_buff *this = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001876 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1877 struct sk_buff *list_skb = list->next;
1878 unsigned long flags;
1879
Ursula Braun7d316b92012-02-08 00:19:45 +00001880 bh_lock_sock(sk);
Ursula Braunf2a77992008-02-07 18:07:44 -08001881 if (!skb_queue_empty(list)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001882 spin_lock_irqsave(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001883
Ursula Braunf2a77992008-02-07 18:07:44 -08001884 while (list_skb != (struct sk_buff *)list) {
Ursula Braunf5738e22014-05-13 14:38:02 +02001885 if (msg->tag == IUCV_SKB_CB(list_skb)->tag) {
Ursula Braunf2a77992008-02-07 18:07:44 -08001886 this = list_skb;
1887 break;
1888 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001889 list_skb = list_skb->next;
Ursula Braunf2a77992008-02-07 18:07:44 -08001890 }
1891 if (this)
1892 __skb_unlink(this, list);
Jennifer Hunteac37312007-02-08 13:51:54 -08001893
Jennifer Hunt561e0362007-05-04 12:22:07 -07001894 spin_unlock_irqrestore(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001895
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001896 if (this) {
1897 kfree_skb(this);
1898 /* wake up any process waiting for sending */
1899 iucv_sock_wake_msglim(sk);
1900 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001901 }
1902
Heiko Carstensda99f052007-05-04 12:23:27 -07001903 if (sk->sk_state == IUCV_CLOSING) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001904 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1905 sk->sk_state = IUCV_CLOSED;
1906 sk->sk_state_change(sk);
1907 }
1908 }
Ursula Braun7d316b92012-02-08 00:19:45 +00001909 bh_unlock_sock(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001910
Jennifer Hunteac37312007-02-08 13:51:54 -08001911}
1912
1913static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1914{
1915 struct sock *sk = path->private;
1916
Ursula Braun7d316b92012-02-08 00:19:45 +00001917 if (sk->sk_state == IUCV_CLOSED)
1918 return;
1919
1920 bh_lock_sock(sk);
1921 iucv_sever_path(sk, 1);
Ursula Braunaac63992011-12-19 22:56:31 +00001922 sk->sk_state = IUCV_DISCONN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001923
1924 sk->sk_state_change(sk);
Ursula Braun7d316b92012-02-08 00:19:45 +00001925 bh_unlock_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001926}
1927
Hendrik Brueckneraf88b522009-04-21 23:26:21 +00001928/* called if the other communication side shuts down its RECV direction;
1929 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1930 */
1931static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1932{
1933 struct sock *sk = path->private;
1934
1935 bh_lock_sock(sk);
1936 if (sk->sk_state != IUCV_CLOSED) {
1937 sk->sk_shutdown |= SEND_SHUTDOWN;
1938 sk->sk_state_change(sk);
1939 }
1940 bh_unlock_sock(sk);
1941}
1942
Ursula Braun3881ac42011-08-08 01:33:54 +00001943/***************** HiperSockets transport callbacks ********************/
1944static void afiucv_swap_src_dest(struct sk_buff *skb)
1945{
1946 struct af_iucv_trans_hdr *trans_hdr =
1947 (struct af_iucv_trans_hdr *)skb->data;
1948 char tmpID[8];
1949 char tmpName[8];
1950
1951 ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1952 ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1953 ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1954 ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1955 memcpy(tmpID, trans_hdr->srcUserID, 8);
1956 memcpy(tmpName, trans_hdr->srcAppName, 8);
1957 memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1958 memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1959 memcpy(trans_hdr->destUserID, tmpID, 8);
1960 memcpy(trans_hdr->destAppName, tmpName, 8);
1961 skb_push(skb, ETH_HLEN);
1962 memset(skb->data, 0, ETH_HLEN);
1963}
1964
1965/**
1966 * afiucv_hs_callback_syn - react on received SYN
1967 **/
1968static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1969{
1970 struct sock *nsk;
1971 struct iucv_sock *iucv, *niucv;
1972 struct af_iucv_trans_hdr *trans_hdr;
1973 int err;
1974
1975 iucv = iucv_sk(sk);
1976 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
1977 if (!iucv) {
1978 /* no sock - connection refused */
1979 afiucv_swap_src_dest(skb);
1980 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1981 err = dev_queue_xmit(skb);
1982 goto out;
1983 }
1984
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001985 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
Ursula Braun3881ac42011-08-08 01:33:54 +00001986 bh_lock_sock(sk);
1987 if ((sk->sk_state != IUCV_LISTEN) ||
1988 sk_acceptq_is_full(sk) ||
1989 !nsk) {
1990 /* error on server socket - connection refused */
Ursula Braun3881ac42011-08-08 01:33:54 +00001991 afiucv_swap_src_dest(skb);
1992 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1993 err = dev_queue_xmit(skb);
Ursula Braun4d520f62014-05-28 10:22:29 +02001994 iucv_sock_kill(nsk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001995 bh_unlock_sock(sk);
1996 goto out;
1997 }
1998
1999 niucv = iucv_sk(nsk);
2000 iucv_sock_init(nsk, sk);
2001 niucv->transport = AF_IUCV_TRANS_HIPER;
2002 niucv->msglimit = iucv->msglimit;
2003 if (!trans_hdr->window)
2004 niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
2005 else
2006 niucv->msglimit_peer = trans_hdr->window;
2007 memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
2008 memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
2009 memcpy(niucv->src_name, iucv->src_name, 8);
2010 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
2011 nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002012 niucv->hs_dev = iucv->hs_dev;
2013 dev_hold(niucv->hs_dev);
Ursula Braun3881ac42011-08-08 01:33:54 +00002014 afiucv_swap_src_dest(skb);
2015 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
2016 trans_hdr->window = niucv->msglimit;
2017 /* if receiver acks the xmit connection is established */
2018 err = dev_queue_xmit(skb);
2019 if (!err) {
2020 iucv_accept_enqueue(sk, nsk);
2021 nsk->sk_state = IUCV_CONNECTED;
David S. Miller676d2362014-04-11 16:15:36 -04002022 sk->sk_data_ready(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002023 } else
2024 iucv_sock_kill(nsk);
2025 bh_unlock_sock(sk);
2026
2027out:
2028 return NET_RX_SUCCESS;
2029}
2030
2031/**
2032 * afiucv_hs_callback_synack() - react on received SYN-ACK
2033 **/
2034static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
2035{
2036 struct iucv_sock *iucv = iucv_sk(sk);
2037 struct af_iucv_trans_hdr *trans_hdr =
2038 (struct af_iucv_trans_hdr *)skb->data;
2039
2040 if (!iucv)
2041 goto out;
2042 if (sk->sk_state != IUCV_BOUND)
2043 goto out;
2044 bh_lock_sock(sk);
2045 iucv->msglimit_peer = trans_hdr->window;
2046 sk->sk_state = IUCV_CONNECTED;
2047 sk->sk_state_change(sk);
2048 bh_unlock_sock(sk);
2049out:
2050 kfree_skb(skb);
2051 return NET_RX_SUCCESS;
2052}
2053
2054/**
2055 * afiucv_hs_callback_synfin() - react on received SYN_FIN
2056 **/
2057static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
2058{
2059 struct iucv_sock *iucv = iucv_sk(sk);
2060
2061 if (!iucv)
2062 goto out;
2063 if (sk->sk_state != IUCV_BOUND)
2064 goto out;
2065 bh_lock_sock(sk);
2066 sk->sk_state = IUCV_DISCONN;
2067 sk->sk_state_change(sk);
2068 bh_unlock_sock(sk);
2069out:
2070 kfree_skb(skb);
2071 return NET_RX_SUCCESS;
2072}
2073
2074/**
2075 * afiucv_hs_callback_fin() - react on received FIN
2076 **/
2077static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
2078{
2079 struct iucv_sock *iucv = iucv_sk(sk);
2080
2081 /* other end of connection closed */
Ursula Braun800c5eb2012-02-08 00:19:47 +00002082 if (!iucv)
2083 goto out;
2084 bh_lock_sock(sk);
2085 if (sk->sk_state == IUCV_CONNECTED) {
Ursula Braunaac63992011-12-19 22:56:31 +00002086 sk->sk_state = IUCV_DISCONN;
Ursula Braun3881ac42011-08-08 01:33:54 +00002087 sk->sk_state_change(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002088 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002089 bh_unlock_sock(sk);
2090out:
Ursula Braun3881ac42011-08-08 01:33:54 +00002091 kfree_skb(skb);
2092 return NET_RX_SUCCESS;
2093}
2094
2095/**
2096 * afiucv_hs_callback_win() - react on received WIN
2097 **/
2098static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
2099{
2100 struct iucv_sock *iucv = iucv_sk(sk);
2101 struct af_iucv_trans_hdr *trans_hdr =
2102 (struct af_iucv_trans_hdr *)skb->data;
2103
2104 if (!iucv)
2105 return NET_RX_SUCCESS;
2106
2107 if (sk->sk_state != IUCV_CONNECTED)
2108 return NET_RX_SUCCESS;
2109
2110 atomic_sub(trans_hdr->window, &iucv->msg_sent);
2111 iucv_sock_wake_msglim(sk);
2112 return NET_RX_SUCCESS;
2113}
2114
2115/**
2116 * afiucv_hs_callback_rx() - react on received data
2117 **/
2118static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
2119{
2120 struct iucv_sock *iucv = iucv_sk(sk);
2121
2122 if (!iucv) {
2123 kfree_skb(skb);
2124 return NET_RX_SUCCESS;
2125 }
2126
2127 if (sk->sk_state != IUCV_CONNECTED) {
2128 kfree_skb(skb);
2129 return NET_RX_SUCCESS;
2130 }
2131
Ursula Braun82492a32012-03-07 02:06:24 +00002132 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2133 kfree_skb(skb);
2134 return NET_RX_SUCCESS;
2135 }
2136
Eugene Crosser979f66b2015-12-11 12:27:51 +01002137 /* write stuff from iucv_msg to skb cb */
Ursula Braun3881ac42011-08-08 01:33:54 +00002138 skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2139 skb_reset_transport_header(skb);
2140 skb_reset_network_header(skb);
Ursula Braunf9c41a62013-04-07 22:19:26 +00002141 IUCV_SKB_CB(skb)->offset = 0;
Ursula Braun8c68b1a2016-10-07 15:51:48 +02002142 if (sk_filter(sk, skb)) {
2143 atomic_inc(&sk->sk_drops); /* skb rejected by filter */
2144 kfree_skb(skb);
2145 return NET_RX_SUCCESS;
2146 }
2147
Ursula Braun3881ac42011-08-08 01:33:54 +00002148 spin_lock(&iucv->message_q.lock);
2149 if (skb_queue_empty(&iucv->backlog_skb_q)) {
Ursula Braun8c68b1a2016-10-07 15:51:48 +02002150 if (__sock_queue_rcv_skb(sk, skb))
Ursula Braun3881ac42011-08-08 01:33:54 +00002151 /* handle rcv queue full */
2152 skb_queue_tail(&iucv->backlog_skb_q, skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002153 } else
2154 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2155 spin_unlock(&iucv->message_q.lock);
2156 return NET_RX_SUCCESS;
2157}
2158
2159/**
2160 * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2161 * transport
2162 * called from netif RX softirq
2163 **/
2164static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2165 struct packet_type *pt, struct net_device *orig_dev)
2166{
Ursula Braun3881ac42011-08-08 01:33:54 +00002167 struct sock *sk;
2168 struct iucv_sock *iucv;
2169 struct af_iucv_trans_hdr *trans_hdr;
Julian Wiedmann22244092018-09-05 16:55:10 +02002170 int err = NET_RX_SUCCESS;
Ursula Braun3881ac42011-08-08 01:33:54 +00002171 char nullstring[8];
Ursula Braun3881ac42011-08-08 01:33:54 +00002172
Eugene Crosser979f66b2015-12-11 12:27:51 +01002173 if (skb->len < (ETH_HLEN + sizeof(struct af_iucv_trans_hdr))) {
2174 WARN_ONCE(1, "AF_IUCV too short skb, len=%d, min=%d",
2175 (int)skb->len,
2176 (int)(ETH_HLEN + sizeof(struct af_iucv_trans_hdr)));
2177 kfree_skb(skb);
2178 return NET_RX_SUCCESS;
2179 }
2180 if (skb_headlen(skb) < (ETH_HLEN + sizeof(struct af_iucv_trans_hdr)))
2181 if (skb_linearize(skb)) {
2182 WARN_ONCE(1, "AF_IUCV skb_linearize failed, len=%d",
2183 (int)skb->len);
2184 kfree_skb(skb);
2185 return NET_RX_SUCCESS;
2186 }
Ursula Braun3881ac42011-08-08 01:33:54 +00002187 skb_pull(skb, ETH_HLEN);
2188 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
2189 EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2190 EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2191 EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2192 EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2193 memset(nullstring, 0, sizeof(nullstring));
2194 iucv = NULL;
2195 sk = NULL;
2196 read_lock(&iucv_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002197 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002198 if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2199 if ((!memcmp(&iucv_sk(sk)->src_name,
2200 trans_hdr->destAppName, 8)) &&
2201 (!memcmp(&iucv_sk(sk)->src_user_id,
2202 trans_hdr->destUserID, 8)) &&
2203 (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2204 (!memcmp(&iucv_sk(sk)->dst_user_id,
2205 nullstring, 8))) {
2206 iucv = iucv_sk(sk);
2207 break;
2208 }
2209 } else {
2210 if ((!memcmp(&iucv_sk(sk)->src_name,
2211 trans_hdr->destAppName, 8)) &&
2212 (!memcmp(&iucv_sk(sk)->src_user_id,
2213 trans_hdr->destUserID, 8)) &&
2214 (!memcmp(&iucv_sk(sk)->dst_name,
2215 trans_hdr->srcAppName, 8)) &&
2216 (!memcmp(&iucv_sk(sk)->dst_user_id,
2217 trans_hdr->srcUserID, 8))) {
2218 iucv = iucv_sk(sk);
2219 break;
2220 }
2221 }
2222 }
2223 read_unlock(&iucv_sk_list.lock);
2224 if (!iucv)
2225 sk = NULL;
2226
2227 /* no sock
2228 how should we send with no sock
2229 1) send without sock no send rc checking?
2230 2) introduce default sock to handle this cases
2231
2232 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2233 data -> send FIN
2234 SYN|ACK, SYN|FIN, FIN -> no action? */
2235
2236 switch (trans_hdr->flags) {
2237 case AF_IUCV_FLAG_SYN:
2238 /* connect request */
2239 err = afiucv_hs_callback_syn(sk, skb);
2240 break;
2241 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2242 /* connect request confirmed */
2243 err = afiucv_hs_callback_synack(sk, skb);
2244 break;
2245 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2246 /* connect request refused */
2247 err = afiucv_hs_callback_synfin(sk, skb);
2248 break;
2249 case (AF_IUCV_FLAG_FIN):
2250 /* close request */
2251 err = afiucv_hs_callback_fin(sk, skb);
2252 break;
2253 case (AF_IUCV_FLAG_WIN):
2254 err = afiucv_hs_callback_win(sk, skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002255 if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
2256 kfree_skb(skb);
2257 break;
2258 }
Ursula Braun82492a32012-03-07 02:06:24 +00002259 /* fall through and receive non-zero length data */
2260 case (AF_IUCV_FLAG_SHT):
2261 /* shutdown request */
2262 /* fall through and receive zero length data */
Ursula Braun3881ac42011-08-08 01:33:54 +00002263 case 0:
2264 /* plain data frame */
Ursula Braunf9c41a62013-04-07 22:19:26 +00002265 IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
Ursula Braun3881ac42011-08-08 01:33:54 +00002266 err = afiucv_hs_callback_rx(sk, skb);
2267 break;
2268 default:
Julian Wiedmann22244092018-09-05 16:55:10 +02002269 kfree_skb(skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002270 }
2271
2272 return err;
2273}
2274
2275/**
2276 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
2277 * transport
2278 **/
2279static void afiucv_hs_callback_txnotify(struct sk_buff *skb,
2280 enum iucv_tx_notify n)
2281{
2282 struct sock *isk = skb->sk;
2283 struct sock *sk = NULL;
2284 struct iucv_sock *iucv = NULL;
2285 struct sk_buff_head *list;
2286 struct sk_buff *list_skb;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002287 struct sk_buff *nskb;
Ursula Braun3881ac42011-08-08 01:33:54 +00002288 unsigned long flags;
Ursula Braun3881ac42011-08-08 01:33:54 +00002289
Ursula Braun800c5eb2012-02-08 00:19:47 +00002290 read_lock_irqsave(&iucv_sk_list.lock, flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002291 sk_for_each(sk, &iucv_sk_list.head)
Ursula Braun3881ac42011-08-08 01:33:54 +00002292 if (sk == isk) {
2293 iucv = iucv_sk(sk);
2294 break;
2295 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002296 read_unlock_irqrestore(&iucv_sk_list.lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002297
Ursula Braun800c5eb2012-02-08 00:19:47 +00002298 if (!iucv || sock_flag(sk, SOCK_ZAPPED))
Ursula Braun3881ac42011-08-08 01:33:54 +00002299 return;
2300
Ursula Braun3881ac42011-08-08 01:33:54 +00002301 list = &iucv->send_skb_q;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002302 spin_lock_irqsave(&list->lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002303 if (skb_queue_empty(list))
2304 goto out_unlock;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002305 list_skb = list->next;
2306 nskb = list_skb->next;
Ursula Braun3881ac42011-08-08 01:33:54 +00002307 while (list_skb != (struct sk_buff *)list) {
2308 if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002309 switch (n) {
2310 case TX_NOTIFY_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002311 __skb_unlink(list_skb, list);
2312 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002313 iucv_sock_wake_msglim(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002314 break;
2315 case TX_NOTIFY_PENDING:
2316 atomic_inc(&iucv->pendings);
2317 break;
2318 case TX_NOTIFY_DELAYED_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002319 __skb_unlink(list_skb, list);
Ursula Braun3881ac42011-08-08 01:33:54 +00002320 atomic_dec(&iucv->pendings);
2321 if (atomic_read(&iucv->pendings) <= 0)
2322 iucv_sock_wake_msglim(sk);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002323 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002324 break;
2325 case TX_NOTIFY_UNREACHABLE:
2326 case TX_NOTIFY_DELAYED_UNREACHABLE:
2327 case TX_NOTIFY_TPQFULL: /* not yet used */
2328 case TX_NOTIFY_GENERALERROR:
2329 case TX_NOTIFY_DELAYED_GENERALERROR:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002330 __skb_unlink(list_skb, list);
2331 kfree_skb(list_skb);
2332 if (sk->sk_state == IUCV_CONNECTED) {
2333 sk->sk_state = IUCV_DISCONN;
2334 sk->sk_state_change(sk);
2335 }
Ursula Braun3881ac42011-08-08 01:33:54 +00002336 break;
2337 }
2338 break;
2339 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002340 list_skb = nskb;
2341 nskb = nskb->next;
Ursula Braun3881ac42011-08-08 01:33:54 +00002342 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002343out_unlock:
Ursula Braun3881ac42011-08-08 01:33:54 +00002344 spin_unlock_irqrestore(&list->lock, flags);
2345
Ursula Braun42bd48e2011-12-19 22:56:28 +00002346 if (sk->sk_state == IUCV_CLOSING) {
2347 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
2348 sk->sk_state = IUCV_CLOSED;
2349 sk->sk_state_change(sk);
2350 }
2351 }
2352
Ursula Braun3881ac42011-08-08 01:33:54 +00002353}
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002354
2355/*
2356 * afiucv_netdev_event: handle netdev notifier chain events
2357 */
2358static int afiucv_netdev_event(struct notifier_block *this,
2359 unsigned long event, void *ptr)
2360{
Jiri Pirko351638e2013-05-28 01:30:21 +00002361 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002362 struct sock *sk;
2363 struct iucv_sock *iucv;
2364
2365 switch (event) {
2366 case NETDEV_REBOOT:
2367 case NETDEV_GOING_DOWN:
Sasha Levinb67bfe02013-02-27 17:06:00 -08002368 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002369 iucv = iucv_sk(sk);
2370 if ((iucv->hs_dev == event_dev) &&
2371 (sk->sk_state == IUCV_CONNECTED)) {
2372 if (event == NETDEV_GOING_DOWN)
2373 iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
2374 sk->sk_state = IUCV_DISCONN;
2375 sk->sk_state_change(sk);
2376 }
2377 }
2378 break;
2379 case NETDEV_DOWN:
2380 case NETDEV_UNREGISTER:
2381 default:
2382 break;
2383 }
2384 return NOTIFY_DONE;
2385}
2386
2387static struct notifier_block afiucv_netdev_notifier = {
2388 .notifier_call = afiucv_netdev_event,
2389};
2390
Alexey Dobriyan5708e862009-09-14 12:23:23 +00002391static const struct proto_ops iucv_sock_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002392 .family = PF_IUCV,
2393 .owner = THIS_MODULE,
2394 .release = iucv_sock_release,
2395 .bind = iucv_sock_bind,
2396 .connect = iucv_sock_connect,
2397 .listen = iucv_sock_listen,
2398 .accept = iucv_sock_accept,
2399 .getname = iucv_sock_getname,
2400 .sendmsg = iucv_sock_sendmsg,
2401 .recvmsg = iucv_sock_recvmsg,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002402 .poll = iucv_sock_poll,
Jennifer Hunteac37312007-02-08 13:51:54 -08002403 .ioctl = sock_no_ioctl,
2404 .mmap = sock_no_mmap,
2405 .socketpair = sock_no_socketpair,
2406 .shutdown = iucv_sock_shutdown,
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00002407 .setsockopt = iucv_sock_setsockopt,
2408 .getsockopt = iucv_sock_getsockopt,
Jennifer Hunteac37312007-02-08 13:51:54 -08002409};
2410
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002411static const struct net_proto_family iucv_sock_family_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002412 .family = AF_IUCV,
2413 .owner = THIS_MODULE,
2414 .create = iucv_sock_create,
2415};
2416
Ursula Braun3881ac42011-08-08 01:33:54 +00002417static struct packet_type iucv_packet_type = {
2418 .type = cpu_to_be16(ETH_P_AF_IUCV),
2419 .func = afiucv_hs_rcv,
2420};
2421
2422static int afiucv_iucv_init(void)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002423{
2424 int err;
2425
2426 err = pr_iucv->iucv_register(&af_iucv_handler, 0);
2427 if (err)
2428 goto out;
2429 /* establish dummy device */
2430 af_iucv_driver.bus = pr_iucv->bus;
2431 err = driver_register(&af_iucv_driver);
2432 if (err)
2433 goto out_iucv;
2434 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
2435 if (!af_iucv_dev) {
2436 err = -ENOMEM;
2437 goto out_driver;
2438 }
2439 dev_set_name(af_iucv_dev, "af_iucv");
2440 af_iucv_dev->bus = pr_iucv->bus;
2441 af_iucv_dev->parent = pr_iucv->root;
2442 af_iucv_dev->release = (void (*)(struct device *))kfree;
2443 af_iucv_dev->driver = &af_iucv_driver;
2444 err = device_register(af_iucv_dev);
2445 if (err)
Arvind Yadavfa6a91e2018-03-13 16:50:06 +01002446 goto out_iucv_dev;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002447 return 0;
2448
Arvind Yadavfa6a91e2018-03-13 16:50:06 +01002449out_iucv_dev:
2450 put_device(af_iucv_dev);
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002451out_driver:
2452 driver_unregister(&af_iucv_driver);
2453out_iucv:
2454 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2455out:
2456 return err;
2457}
2458
Heiko Carstensda99f052007-05-04 12:23:27 -07002459static int __init afiucv_init(void)
Jennifer Hunteac37312007-02-08 13:51:54 -08002460{
2461 int err;
2462
Ursula Braun3881ac42011-08-08 01:33:54 +00002463 if (MACHINE_IS_VM) {
2464 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2465 if (unlikely(err)) {
2466 WARN_ON(err);
2467 err = -EPROTONOSUPPORT;
2468 goto out;
2469 }
Jennifer Hunteac37312007-02-08 13:51:54 -08002470
Ursula Braun3881ac42011-08-08 01:33:54 +00002471 pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
2472 if (!pr_iucv) {
2473 printk(KERN_WARNING "iucv_if lookup failed\n");
2474 memset(&iucv_userid, 0, sizeof(iucv_userid));
2475 }
2476 } else {
2477 memset(&iucv_userid, 0, sizeof(iucv_userid));
2478 pr_iucv = NULL;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002479 }
2480
Jennifer Hunteac37312007-02-08 13:51:54 -08002481 err = proto_register(&iucv_proto, 0);
2482 if (err)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002483 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08002484 err = sock_register(&iucv_sock_family_ops);
2485 if (err)
2486 goto out_proto;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002487
Ursula Braun3881ac42011-08-08 01:33:54 +00002488 if (pr_iucv) {
2489 err = afiucv_iucv_init();
2490 if (err)
2491 goto out_sock;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002492 } else
2493 register_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002494 dev_add_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002495 return 0;
2496
Ursula Braunc23cad92009-06-16 10:30:44 +02002497out_sock:
2498 sock_unregister(PF_IUCV);
Jennifer Hunteac37312007-02-08 13:51:54 -08002499out_proto:
2500 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002501out:
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002502 if (pr_iucv)
2503 symbol_put(iucv_if);
Jennifer Hunteac37312007-02-08 13:51:54 -08002504 return err;
2505}
2506
2507static void __exit afiucv_exit(void)
2508{
Ursula Braun3881ac42011-08-08 01:33:54 +00002509 if (pr_iucv) {
2510 device_unregister(af_iucv_dev);
2511 driver_unregister(&af_iucv_driver);
2512 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2513 symbol_put(iucv_if);
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002514 } else
2515 unregister_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002516 dev_remove_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002517 sock_unregister(PF_IUCV);
2518 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002519}
2520
2521module_init(afiucv_init);
2522module_exit(afiucv_exit);
2523
2524MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2525MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2526MODULE_VERSION(VERSION);
2527MODULE_LICENSE("GPL");
2528MODULE_ALIAS_NETPROTO(PF_IUCV);