blob: 8be324fe08b999d114300d7bf6353c3292e31eae [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>
20#include <linux/sched.h>
21#include <linux/slab.h>
22#include <linux/skbuff.h>
23#include <linux/init.h>
24#include <linux/poll.h>
25#include <net/sock.h>
26#include <asm/ebcdic.h>
27#include <asm/cpcmd.h>
28#include <linux/kmod.h>
29
30#include <net/iucv/iucv.h>
31#include <net/iucv/af_iucv.h>
32
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +000033#define VERSION "1.1"
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
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +000045/* special AF_IUCV IPRM messages */
46static const u8 iprm_shutdown[8] =
47 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
48
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000049#define TRGCLS_SIZE (sizeof(((struct iucv_message *)0)->class))
50
51/* macros to set/get socket control buffer at correct offset */
52#define CB_TAG(skb) ((skb)->cb) /* iucv message tag */
53#define CB_TAG_LEN (sizeof(((struct iucv_message *) 0)->tag))
54#define CB_TRGCLS(skb) ((skb)->cb + CB_TAG_LEN) /* iucv msg target class */
55#define CB_TRGCLS_LEN (TRGCLS_SIZE)
56
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000057#define __iucv_sock_wait(sk, condition, timeo, ret) \
58do { \
59 DEFINE_WAIT(__wait); \
60 long __timeo = timeo; \
61 ret = 0; \
Eric Dumazetaa395142010-04-20 13:03:51 +000062 prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000063 while (!(condition)) { \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000064 if (!__timeo) { \
65 ret = -EAGAIN; \
66 break; \
67 } \
68 if (signal_pending(current)) { \
69 ret = sock_intr_errno(__timeo); \
70 break; \
71 } \
72 release_sock(sk); \
73 __timeo = schedule_timeout(__timeo); \
74 lock_sock(sk); \
75 ret = sock_error(sk); \
76 if (ret) \
77 break; \
78 } \
Eric Dumazetaa395142010-04-20 13:03:51 +000079 finish_wait(sk_sleep(sk), &__wait); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000080} while (0)
81
82#define iucv_sock_wait(sk, condition, timeo) \
83({ \
84 int __ret = 0; \
85 if (!(condition)) \
86 __iucv_sock_wait(sk, condition, timeo, __ret); \
87 __ret; \
88})
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000089
Heiko Carstens57f20442007-10-08 02:02:52 -070090static void iucv_sock_kill(struct sock *sk);
91static void iucv_sock_close(struct sock *sk);
92
Jennifer Hunteac37312007-02-08 13:51:54 -080093/* Call Back functions */
94static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
95static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
96static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
Heiko Carstensda99f052007-05-04 12:23:27 -070097static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8],
98 u8 ipuser[16]);
Jennifer Hunteac37312007-02-08 13:51:54 -080099static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
Hendrik Brueckneraf88b522009-04-21 23:26:21 +0000100static void iucv_callback_shutdown(struct iucv_path *, u8 ipuser[16]);
Jennifer Hunteac37312007-02-08 13:51:54 -0800101
102static struct iucv_sock_list iucv_sk_list = {
Robert P. J. Day3db8ce32008-04-10 02:11:24 -0700103 .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
Jennifer Hunteac37312007-02-08 13:51:54 -0800104 .autobind_name = ATOMIC_INIT(0)
105};
106
107static struct iucv_handler af_iucv_handler = {
108 .path_pending = iucv_callback_connreq,
109 .path_complete = iucv_callback_connack,
110 .path_severed = iucv_callback_connrej,
111 .message_pending = iucv_callback_rx,
Hendrik Brueckneraf88b522009-04-21 23:26:21 +0000112 .message_complete = iucv_callback_txdone,
113 .path_quiesced = iucv_callback_shutdown,
Jennifer Hunteac37312007-02-08 13:51:54 -0800114};
115
116static inline void high_nmcpy(unsigned char *dst, char *src)
117{
118 memcpy(dst, src, 8);
119}
120
121static inline void low_nmcpy(unsigned char *dst, char *src)
122{
123 memcpy(&dst[8], src, 8);
124}
125
Ursula Braunc23cad92009-06-16 10:30:44 +0200126static int afiucv_pm_prepare(struct device *dev)
127{
128#ifdef CONFIG_PM_DEBUG
129 printk(KERN_WARNING "afiucv_pm_prepare\n");
130#endif
131 return 0;
132}
133
134static void afiucv_pm_complete(struct device *dev)
135{
136#ifdef CONFIG_PM_DEBUG
137 printk(KERN_WARNING "afiucv_pm_complete\n");
138#endif
139 return;
140}
141
142/**
143 * afiucv_pm_freeze() - Freeze PM callback
144 * @dev: AFIUCV dummy device
145 *
146 * Sever all established IUCV communication pathes
147 */
148static int afiucv_pm_freeze(struct device *dev)
149{
150 struct iucv_sock *iucv;
151 struct sock *sk;
152 struct hlist_node *node;
153 int err = 0;
154
155#ifdef CONFIG_PM_DEBUG
156 printk(KERN_WARNING "afiucv_pm_freeze\n");
157#endif
158 read_lock(&iucv_sk_list.lock);
159 sk_for_each(sk, node, &iucv_sk_list.head) {
160 iucv = iucv_sk(sk);
161 skb_queue_purge(&iucv->send_skb_q);
162 skb_queue_purge(&iucv->backlog_skb_q);
163 switch (sk->sk_state) {
164 case IUCV_SEVERED:
165 case IUCV_DISCONN:
166 case IUCV_CLOSING:
167 case IUCV_CONNECTED:
168 if (iucv->path) {
169 err = iucv_path_sever(iucv->path, NULL);
170 iucv_path_free(iucv->path);
171 iucv->path = NULL;
172 }
173 break;
174 case IUCV_OPEN:
175 case IUCV_BOUND:
176 case IUCV_LISTEN:
177 case IUCV_CLOSED:
178 default:
179 break;
180 }
181 }
182 read_unlock(&iucv_sk_list.lock);
183 return err;
184}
185
186/**
187 * afiucv_pm_restore_thaw() - Thaw and restore PM callback
188 * @dev: AFIUCV dummy device
189 *
190 * socket clean up after freeze
191 */
192static int afiucv_pm_restore_thaw(struct device *dev)
193{
194 struct iucv_sock *iucv;
195 struct sock *sk;
196 struct hlist_node *node;
197
198#ifdef CONFIG_PM_DEBUG
199 printk(KERN_WARNING "afiucv_pm_restore_thaw\n");
200#endif
201 read_lock(&iucv_sk_list.lock);
202 sk_for_each(sk, node, &iucv_sk_list.head) {
203 iucv = iucv_sk(sk);
204 switch (sk->sk_state) {
205 case IUCV_CONNECTED:
206 sk->sk_err = EPIPE;
207 sk->sk_state = IUCV_DISCONN;
208 sk->sk_state_change(sk);
209 break;
210 case IUCV_DISCONN:
211 case IUCV_SEVERED:
212 case IUCV_CLOSING:
213 case IUCV_LISTEN:
214 case IUCV_BOUND:
215 case IUCV_OPEN:
216 default:
217 break;
218 }
219 }
220 read_unlock(&iucv_sk_list.lock);
221 return 0;
222}
223
Alexey Dobriyan47145212009-12-14 18:00:08 -0800224static const struct dev_pm_ops afiucv_pm_ops = {
Ursula Braunc23cad92009-06-16 10:30:44 +0200225 .prepare = afiucv_pm_prepare,
226 .complete = afiucv_pm_complete,
227 .freeze = afiucv_pm_freeze,
228 .thaw = afiucv_pm_restore_thaw,
229 .restore = afiucv_pm_restore_thaw,
230};
231
232static struct device_driver af_iucv_driver = {
233 .owner = THIS_MODULE,
234 .name = "afiucv",
235 .bus = &iucv_bus,
236 .pm = &afiucv_pm_ops,
237};
238
239/* dummy device used as trigger for PM functions */
240static struct device *af_iucv_dev;
241
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000242/**
243 * iucv_msg_length() - Returns the length of an iucv message.
244 * @msg: Pointer to struct iucv_message, MUST NOT be NULL
245 *
246 * The function returns the length of the specified iucv message @msg of data
247 * stored in a buffer and of data stored in the parameter list (PRMDATA).
248 *
249 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
250 * data:
251 * PRMDATA[0..6] socket data (max 7 bytes);
252 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
253 *
254 * The socket data length is computed by substracting the socket data length
255 * value from 0xFF.
256 * If the socket data len is greater 7, then PRMDATA can be used for special
257 * notifications (see iucv_sock_shutdown); and further,
258 * if the socket data len is > 7, the function returns 8.
259 *
260 * Use this function to allocate socket buffers to store iucv message data.
261 */
262static inline size_t iucv_msg_length(struct iucv_message *msg)
263{
264 size_t datalen;
265
266 if (msg->flags & IUCV_IPRMDATA) {
267 datalen = 0xff - msg->rmmsg[7];
268 return (datalen < 8) ? datalen : 8;
269 }
270 return msg->length;
271}
272
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000273/**
274 * iucv_sock_in_state() - check for specific states
275 * @sk: sock structure
276 * @state: first iucv sk state
277 * @state: second iucv sk state
278 *
279 * Returns true if the socket in either in the first or second state.
280 */
281static int iucv_sock_in_state(struct sock *sk, int state, int state2)
282{
283 return (sk->sk_state == state || sk->sk_state == state2);
284}
285
286/**
287 * iucv_below_msglim() - function to check if messages can be sent
288 * @sk: sock structure
289 *
290 * Returns true if the send queue length is lower than the message limit.
291 * Always returns true if the socket is not connected (no iucv path for
292 * checking the message limit).
293 */
294static inline int iucv_below_msglim(struct sock *sk)
295{
296 struct iucv_sock *iucv = iucv_sk(sk);
297
298 if (sk->sk_state != IUCV_CONNECTED)
299 return 1;
300 return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim);
301}
302
303/**
304 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
305 */
306static void iucv_sock_wake_msglim(struct sock *sk)
307{
Eric Dumazet43815482010-04-29 11:01:49 +0000308 struct socket_wq *wq;
309
310 rcu_read_lock();
311 wq = rcu_dereference(sk->sk_wq);
312 if (wq_has_sleeper(wq))
313 wake_up_interruptible_all(&wq->wait);
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000314 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Eric Dumazet43815482010-04-29 11:01:49 +0000315 rcu_read_unlock();
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000316}
317
Jennifer Hunteac37312007-02-08 13:51:54 -0800318/* Timers */
319static void iucv_sock_timeout(unsigned long arg)
320{
321 struct sock *sk = (struct sock *)arg;
322
323 bh_lock_sock(sk);
324 sk->sk_err = ETIMEDOUT;
325 sk->sk_state_change(sk);
326 bh_unlock_sock(sk);
327
328 iucv_sock_kill(sk);
329 sock_put(sk);
330}
331
332static void iucv_sock_clear_timer(struct sock *sk)
333{
334 sk_stop_timer(sk, &sk->sk_timer);
335}
336
Jennifer Hunteac37312007-02-08 13:51:54 -0800337static struct sock *__iucv_get_sock_by_name(char *nm)
338{
339 struct sock *sk;
340 struct hlist_node *node;
341
342 sk_for_each(sk, node, &iucv_sk_list.head)
343 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
344 return sk;
345
346 return NULL;
347}
348
349static void iucv_sock_destruct(struct sock *sk)
350{
351 skb_queue_purge(&sk->sk_receive_queue);
352 skb_queue_purge(&sk->sk_write_queue);
353}
354
355/* Cleanup Listen */
356static void iucv_sock_cleanup_listen(struct sock *parent)
357{
358 struct sock *sk;
359
360 /* Close non-accepted connections */
361 while ((sk = iucv_accept_dequeue(parent, NULL))) {
362 iucv_sock_close(sk);
363 iucv_sock_kill(sk);
364 }
365
366 parent->sk_state = IUCV_CLOSED;
Jennifer Hunteac37312007-02-08 13:51:54 -0800367}
368
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000369/* Kill socket (only if zapped and orphaned) */
Jennifer Hunteac37312007-02-08 13:51:54 -0800370static void iucv_sock_kill(struct sock *sk)
371{
372 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
373 return;
374
375 iucv_sock_unlink(&iucv_sk_list, sk);
376 sock_set_flag(sk, SOCK_DEAD);
377 sock_put(sk);
378}
379
380/* Close an IUCV socket */
381static void iucv_sock_close(struct sock *sk)
382{
383 unsigned char user_data[16];
384 struct iucv_sock *iucv = iucv_sk(sk);
385 int err;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700386 unsigned long timeo;
Jennifer Hunteac37312007-02-08 13:51:54 -0800387
388 iucv_sock_clear_timer(sk);
389 lock_sock(sk);
390
Heiko Carstensda99f052007-05-04 12:23:27 -0700391 switch (sk->sk_state) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800392 case IUCV_LISTEN:
393 iucv_sock_cleanup_listen(sk);
394 break;
395
396 case IUCV_CONNECTED:
397 case IUCV_DISCONN:
398 err = 0;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700399
400 sk->sk_state = IUCV_CLOSING;
401 sk->sk_state_change(sk);
402
Heiko Carstensda99f052007-05-04 12:23:27 -0700403 if (!skb_queue_empty(&iucv->send_skb_q)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -0700404 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
405 timeo = sk->sk_lingertime;
406 else
407 timeo = IUCV_DISCONN_TIMEOUT;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000408 err = iucv_sock_wait(sk,
409 iucv_sock_in_state(sk, IUCV_CLOSED, 0),
410 timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700411 }
412
Ursula Braunbbe188c2009-04-21 06:04:20 +0000413 case IUCV_CLOSING: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700414 sk->sk_state = IUCV_CLOSED;
415 sk->sk_state_change(sk);
416
Jennifer Hunteac37312007-02-08 13:51:54 -0800417 if (iucv->path) {
418 low_nmcpy(user_data, iucv->src_name);
419 high_nmcpy(user_data, iucv->dst_name);
420 ASCEBC(user_data, sizeof(user_data));
421 err = iucv_path_sever(iucv->path, user_data);
422 iucv_path_free(iucv->path);
423 iucv->path = NULL;
424 }
425
Jennifer Hunteac37312007-02-08 13:51:54 -0800426 sk->sk_err = ECONNRESET;
427 sk->sk_state_change(sk);
428
429 skb_queue_purge(&iucv->send_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700430 skb_queue_purge(&iucv->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800431 break;
432
433 default:
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000434 /* nothing to do here */
Jennifer Hunteac37312007-02-08 13:51:54 -0800435 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700436 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800437
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000438 /* mark socket for deletion by iucv_sock_kill() */
439 sock_set_flag(sk, SOCK_ZAPPED);
440
Jennifer Hunteac37312007-02-08 13:51:54 -0800441 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800442}
443
444static void iucv_sock_init(struct sock *sk, struct sock *parent)
445{
446 if (parent)
447 sk->sk_type = parent->sk_type;
448}
449
450static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
451{
452 struct sock *sk;
453
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700454 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -0800455 if (!sk)
456 return NULL;
457
458 sock_init_data(sock, sk);
459 INIT_LIST_HEAD(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700460 spin_lock_init(&iucv_sk(sk)->accept_q_lock);
Jennifer Hunteac37312007-02-08 13:51:54 -0800461 skb_queue_head_init(&iucv_sk(sk)->send_skb_q);
Ursula Braunf0703c82007-10-08 02:03:31 -0700462 INIT_LIST_HEAD(&iucv_sk(sk)->message_q.list);
463 spin_lock_init(&iucv_sk(sk)->message_q.lock);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700464 skb_queue_head_init(&iucv_sk(sk)->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800465 iucv_sk(sk)->send_tag = 0;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +0000466 iucv_sk(sk)->flags = 0;
Hendrik Brueckner09488e22009-04-21 23:26:27 +0000467 iucv_sk(sk)->msglimit = IUCV_QUEUELEN_DEFAULT;
Ursula Braunbbe188c2009-04-21 06:04:20 +0000468 iucv_sk(sk)->path = NULL;
469 memset(&iucv_sk(sk)->src_user_id , 0, 32);
Jennifer Hunteac37312007-02-08 13:51:54 -0800470
471 sk->sk_destruct = iucv_sock_destruct;
472 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
473 sk->sk_allocation = GFP_DMA;
474
475 sock_reset_flag(sk, SOCK_ZAPPED);
476
477 sk->sk_protocol = proto;
478 sk->sk_state = IUCV_OPEN;
479
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800480 setup_timer(&sk->sk_timer, iucv_sock_timeout, (unsigned long)sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800481
482 iucv_sock_link(&iucv_sk_list, sk);
483 return sk;
484}
485
486/* Create an IUCV socket */
Eric Paris3f378b62009-11-05 22:18:14 -0800487static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
488 int kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800489{
490 struct sock *sk;
491
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000492 if (protocol && protocol != PF_IUCV)
493 return -EPROTONOSUPPORT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800494
495 sock->state = SS_UNCONNECTED;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000496
497 switch (sock->type) {
498 case SOCK_STREAM:
499 sock->ops = &iucv_sock_ops;
500 break;
501 case SOCK_SEQPACKET:
502 /* currently, proto ops can handle both sk types */
503 sock->ops = &iucv_sock_ops;
504 break;
505 default:
506 return -ESOCKTNOSUPPORT;
507 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800508
509 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
510 if (!sk)
511 return -ENOMEM;
512
513 iucv_sock_init(sk, NULL);
514
515 return 0;
516}
517
518void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
519{
520 write_lock_bh(&l->lock);
521 sk_add_node(sk, &l->head);
522 write_unlock_bh(&l->lock);
523}
524
525void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
526{
527 write_lock_bh(&l->lock);
528 sk_del_node_init(sk);
529 write_unlock_bh(&l->lock);
530}
531
532void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
533{
Ursula Braunfebca282007-07-14 19:04:25 -0700534 unsigned long flags;
535 struct iucv_sock *par = iucv_sk(parent);
536
Jennifer Hunteac37312007-02-08 13:51:54 -0800537 sock_hold(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700538 spin_lock_irqsave(&par->accept_q_lock, flags);
539 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
540 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800541 iucv_sk(sk)->parent = parent;
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000542 sk_acceptq_added(parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800543}
544
545void iucv_accept_unlink(struct sock *sk)
546{
Ursula Braunfebca282007-07-14 19:04:25 -0700547 unsigned long flags;
548 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
549
550 spin_lock_irqsave(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800551 list_del_init(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700552 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000553 sk_acceptq_removed(iucv_sk(sk)->parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800554 iucv_sk(sk)->parent = NULL;
555 sock_put(sk);
556}
557
558struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
559{
560 struct iucv_sock *isk, *n;
561 struct sock *sk;
562
Heiko Carstensda99f052007-05-04 12:23:27 -0700563 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800564 sk = (struct sock *) isk;
565 lock_sock(sk);
566
567 if (sk->sk_state == IUCV_CLOSED) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800568 iucv_accept_unlink(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700569 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800570 continue;
571 }
572
573 if (sk->sk_state == IUCV_CONNECTED ||
574 sk->sk_state == IUCV_SEVERED ||
Hendrik Brueckner56a73de2009-09-16 04:37:26 +0000575 sk->sk_state == IUCV_DISCONN || /* due to PM restore */
Jennifer Hunteac37312007-02-08 13:51:54 -0800576 !newsock) {
577 iucv_accept_unlink(sk);
578 if (newsock)
579 sock_graft(sk, newsock);
580
581 if (sk->sk_state == IUCV_SEVERED)
582 sk->sk_state = IUCV_DISCONN;
583
584 release_sock(sk);
585 return sk;
586 }
587
588 release_sock(sk);
589 }
590 return NULL;
591}
592
Jennifer Hunteac37312007-02-08 13:51:54 -0800593/* Bind an unbound socket */
594static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
595 int addr_len)
596{
597 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
598 struct sock *sk = sock->sk;
599 struct iucv_sock *iucv;
600 int err;
601
602 /* Verify the input sockaddr */
603 if (!addr || addr->sa_family != AF_IUCV)
604 return -EINVAL;
605
606 lock_sock(sk);
607 if (sk->sk_state != IUCV_OPEN) {
608 err = -EBADFD;
609 goto done;
610 }
611
612 write_lock_bh(&iucv_sk_list.lock);
613
614 iucv = iucv_sk(sk);
615 if (__iucv_get_sock_by_name(sa->siucv_name)) {
616 err = -EADDRINUSE;
617 goto done_unlock;
618 }
619 if (iucv->path) {
620 err = 0;
621 goto done_unlock;
622 }
623
624 /* Bind the socket */
625 memcpy(iucv->src_name, sa->siucv_name, 8);
626
627 /* Copy the user id */
628 memcpy(iucv->src_user_id, iucv_userid, 8);
629 sk->sk_state = IUCV_BOUND;
630 err = 0;
631
632done_unlock:
633 /* Release the socket list lock */
634 write_unlock_bh(&iucv_sk_list.lock);
635done:
636 release_sock(sk);
637 return err;
638}
639
640/* Automatically bind an unbound socket */
641static int iucv_sock_autobind(struct sock *sk)
642{
643 struct iucv_sock *iucv = iucv_sk(sk);
644 char query_buffer[80];
645 char name[12];
646 int err = 0;
647
648 /* Set the userid and name */
649 cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err);
650 if (unlikely(err))
651 return -EPROTO;
652
653 memcpy(iucv->src_user_id, query_buffer, 8);
654
655 write_lock_bh(&iucv_sk_list.lock);
656
657 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
658 while (__iucv_get_sock_by_name(name)) {
659 sprintf(name, "%08x",
660 atomic_inc_return(&iucv_sk_list.autobind_name));
661 }
662
663 write_unlock_bh(&iucv_sk_list.lock);
664
665 memcpy(&iucv->src_name, name, 8);
666
667 return err;
668}
669
670/* Connect an unconnected socket */
671static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
672 int alen, int flags)
673{
674 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
675 struct sock *sk = sock->sk;
676 struct iucv_sock *iucv;
677 unsigned char user_data[16];
678 int err;
679
680 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
681 return -EINVAL;
682
683 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
684 return -EBADFD;
685
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000686 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800687 return -EINVAL;
688
Jennifer Hunteac37312007-02-08 13:51:54 -0800689 if (sk->sk_state == IUCV_OPEN) {
690 err = iucv_sock_autobind(sk);
691 if (unlikely(err))
692 return err;
693 }
694
695 lock_sock(sk);
696
697 /* Set the destination information */
698 memcpy(iucv_sk(sk)->dst_user_id, sa->siucv_user_id, 8);
699 memcpy(iucv_sk(sk)->dst_name, sa->siucv_name, 8);
700
701 high_nmcpy(user_data, sa->siucv_name);
702 low_nmcpy(user_data, iucv_sk(sk)->src_name);
703 ASCEBC(user_data, sizeof(user_data));
704
705 iucv = iucv_sk(sk);
706 /* Create path. */
Hendrik Brueckner09488e22009-04-21 23:26:27 +0000707 iucv->path = iucv_path_alloc(iucv->msglimit,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000708 IUCV_IPRMDATA, GFP_KERNEL);
Ursula Braund4444722008-02-07 18:07:19 -0800709 if (!iucv->path) {
710 err = -ENOMEM;
711 goto done;
712 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800713 err = iucv_path_connect(iucv->path, &af_iucv_handler,
714 sa->siucv_user_id, NULL, user_data, sk);
715 if (err) {
716 iucv_path_free(iucv->path);
717 iucv->path = NULL;
Hendrik Brueckner55cdea92009-01-05 18:07:07 -0800718 switch (err) {
719 case 0x0b: /* Target communicator is not logged on */
720 err = -ENETUNREACH;
721 break;
722 case 0x0d: /* Max connections for this guest exceeded */
723 case 0x0e: /* Max connections for target guest exceeded */
724 err = -EAGAIN;
725 break;
726 case 0x0f: /* Missing IUCV authorization */
727 err = -EACCES;
728 break;
729 default:
730 err = -ECONNREFUSED;
731 break;
732 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800733 goto done;
734 }
735
736 if (sk->sk_state != IUCV_CONNECTED) {
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000737 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
738 IUCV_DISCONN),
739 sock_sndtimeo(sk, flags & O_NONBLOCK));
Jennifer Hunteac37312007-02-08 13:51:54 -0800740 }
741
742 if (sk->sk_state == IUCV_DISCONN) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000743 err = -ECONNREFUSED;
Jennifer Hunteac37312007-02-08 13:51:54 -0800744 }
Ursula Braun18becbc2009-01-05 18:07:46 -0800745
746 if (err) {
747 iucv_path_sever(iucv->path, NULL);
748 iucv_path_free(iucv->path);
749 iucv->path = NULL;
750 }
751
Jennifer Hunteac37312007-02-08 13:51:54 -0800752done:
753 release_sock(sk);
754 return err;
755}
756
757/* Move a socket into listening state. */
758static int iucv_sock_listen(struct socket *sock, int backlog)
759{
760 struct sock *sk = sock->sk;
761 int err;
762
763 lock_sock(sk);
764
765 err = -EINVAL;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000766 if (sk->sk_state != IUCV_BOUND)
767 goto done;
768
769 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800770 goto done;
771
772 sk->sk_max_ack_backlog = backlog;
773 sk->sk_ack_backlog = 0;
774 sk->sk_state = IUCV_LISTEN;
775 err = 0;
776
777done:
778 release_sock(sk);
779 return err;
780}
781
782/* Accept a pending connection */
783static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
784 int flags)
785{
786 DECLARE_WAITQUEUE(wait, current);
787 struct sock *sk = sock->sk, *nsk;
788 long timeo;
789 int err = 0;
790
Jennifer Hunt561e0362007-05-04 12:22:07 -0700791 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800792
793 if (sk->sk_state != IUCV_LISTEN) {
794 err = -EBADFD;
795 goto done;
796 }
797
798 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
799
800 /* Wait for an incoming connection */
Eric Dumazetaa395142010-04-20 13:03:51 +0000801 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Heiko Carstensda99f052007-05-04 12:23:27 -0700802 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800803 set_current_state(TASK_INTERRUPTIBLE);
804 if (!timeo) {
805 err = -EAGAIN;
806 break;
807 }
808
809 release_sock(sk);
810 timeo = schedule_timeout(timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700811 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800812
813 if (sk->sk_state != IUCV_LISTEN) {
814 err = -EBADFD;
815 break;
816 }
817
818 if (signal_pending(current)) {
819 err = sock_intr_errno(timeo);
820 break;
821 }
822 }
823
824 set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000825 remove_wait_queue(sk_sleep(sk), &wait);
Jennifer Hunteac37312007-02-08 13:51:54 -0800826
827 if (err)
828 goto done;
829
830 newsock->state = SS_CONNECTED;
831
832done:
833 release_sock(sk);
834 return err;
835}
836
837static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
838 int *len, int peer)
839{
840 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
841 struct sock *sk = sock->sk;
842
843 addr->sa_family = AF_IUCV;
844 *len = sizeof(struct sockaddr_iucv);
845
846 if (peer) {
847 memcpy(siucv->siucv_user_id, iucv_sk(sk)->dst_user_id, 8);
848 memcpy(siucv->siucv_name, &iucv_sk(sk)->dst_name, 8);
849 } else {
850 memcpy(siucv->siucv_user_id, iucv_sk(sk)->src_user_id, 8);
851 memcpy(siucv->siucv_name, iucv_sk(sk)->src_name, 8);
852 }
853 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
854 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
855 memset(siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
856
857 return 0;
858}
859
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000860/**
861 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
862 * @path: IUCV path
863 * @msg: Pointer to a struct iucv_message
864 * @skb: The socket data to send, skb->len MUST BE <= 7
865 *
866 * Send the socket data in the parameter list in the iucv message
867 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
868 * list and the socket data len at index 7 (last byte).
869 * See also iucv_msg_length().
870 *
871 * Returns the error code from the iucv_message_send() call.
872 */
873static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
874 struct sk_buff *skb)
875{
876 u8 prmdata[8];
877
878 memcpy(prmdata, (void *) skb->data, skb->len);
879 prmdata[7] = 0xff - (u8) skb->len;
880 return iucv_message_send(path, msg, IUCV_IPRMDATA, 0,
881 (void *) prmdata, 8);
882}
883
Jennifer Hunteac37312007-02-08 13:51:54 -0800884static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
885 struct msghdr *msg, size_t len)
886{
887 struct sock *sk = sock->sk;
888 struct iucv_sock *iucv = iucv_sk(sk);
889 struct sk_buff *skb;
890 struct iucv_message txmsg;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000891 struct cmsghdr *cmsg;
892 int cmsg_done;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000893 long timeo;
Ursula Braun8f7c5022008-12-25 13:39:47 +0100894 char user_id[9];
895 char appl_id[9];
Jennifer Hunteac37312007-02-08 13:51:54 -0800896 int err;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000897 int noblock = msg->msg_flags & MSG_DONTWAIT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800898
899 err = sock_error(sk);
900 if (err)
901 return err;
902
903 if (msg->msg_flags & MSG_OOB)
904 return -EOPNOTSUPP;
905
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000906 /* SOCK_SEQPACKET: we do not support segmented records */
907 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
908 return -EOPNOTSUPP;
909
Jennifer Hunteac37312007-02-08 13:51:54 -0800910 lock_sock(sk);
911
912 if (sk->sk_shutdown & SEND_SHUTDOWN) {
913 err = -EPIPE;
914 goto out;
915 }
916
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000917 /* Return if the socket is not in connected state */
918 if (sk->sk_state != IUCV_CONNECTED) {
919 err = -ENOTCONN;
920 goto out;
921 }
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000922
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000923 /* initialize defaults */
924 cmsg_done = 0; /* check for duplicate headers */
925 txmsg.class = 0;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000926
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000927 /* iterate over control messages */
928 for (cmsg = CMSG_FIRSTHDR(msg); cmsg;
929 cmsg = CMSG_NXTHDR(msg, cmsg)) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000930
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000931 if (!CMSG_OK(msg, cmsg)) {
932 err = -EINVAL;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700933 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -0800934 }
935
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000936 if (cmsg->cmsg_level != SOL_IUCV)
937 continue;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000938
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000939 if (cmsg->cmsg_type & cmsg_done) {
940 err = -EINVAL;
941 goto out;
942 }
943 cmsg_done |= cmsg->cmsg_type;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000944
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000945 switch (cmsg->cmsg_type) {
946 case SCM_IUCV_TRGCLS:
947 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
948 err = -EINVAL;
949 goto out;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000950 }
951
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000952 /* set iucv message target class */
953 memcpy(&txmsg.class,
954 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
955
956 break;
957
958 default:
959 err = -EINVAL;
960 goto out;
961 break;
962 }
963 }
964
965 /* allocate one skb for each iucv message:
966 * this is fine for SOCK_SEQPACKET (unless we want to support
967 * segmented records using the MSG_EOR flag), but
968 * for SOCK_STREAM we might want to improve it in future */
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000969 skb = sock_alloc_send_skb(sk, len, noblock, &err);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000970 if (!skb)
971 goto out;
972 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
973 err = -EFAULT;
974 goto fail;
975 }
976
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000977 /* wait if outstanding messages for iucv path has reached */
978 timeo = sock_sndtimeo(sk, noblock);
979 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
980 if (err)
981 goto fail;
982
983 /* return -ECONNRESET if the socket is no longer connected */
984 if (sk->sk_state != IUCV_CONNECTED) {
985 err = -ECONNRESET;
986 goto fail;
987 }
988
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000989 /* increment and save iucv message tag for msg_completion cbk */
990 txmsg.tag = iucv->send_tag++;
991 memcpy(CB_TAG(skb), &txmsg.tag, CB_TAG_LEN);
992 skb_queue_tail(&iucv->send_skb_q, skb);
993
994 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags)
995 && skb->len <= 7) {
996 err = iucv_send_iprm(iucv->path, &txmsg, skb);
997
998 /* on success: there is no message_complete callback
999 * for an IPRMDATA msg; remove skb from send queue */
1000 if (err == 0) {
1001 skb_unlink(skb, &iucv->send_skb_q);
1002 kfree_skb(skb);
1003 }
1004
1005 /* this error should never happen since the
1006 * IUCV_IPRMDATA path flag is set... sever path */
1007 if (err == 0x15) {
1008 iucv_path_sever(iucv->path, NULL);
Jennifer Hunteac37312007-02-08 13:51:54 -08001009 skb_unlink(skb, &iucv->send_skb_q);
1010 err = -EPIPE;
1011 goto fail;
1012 }
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001013 } else
1014 err = iucv_message_send(iucv->path, &txmsg, 0, 0,
1015 (void *) skb->data, skb->len);
1016 if (err) {
1017 if (err == 3) {
1018 user_id[8] = 0;
1019 memcpy(user_id, iucv->dst_user_id, 8);
1020 appl_id[8] = 0;
1021 memcpy(appl_id, iucv->dst_name, 8);
1022 pr_err("Application %s on z/VM guest %s"
1023 " exceeds message limit\n",
1024 appl_id, user_id);
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001025 err = -EAGAIN;
1026 } else
1027 err = -EPIPE;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001028 skb_unlink(skb, &iucv->send_skb_q);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001029 goto fail;
Jennifer Hunteac37312007-02-08 13:51:54 -08001030 }
1031
1032 release_sock(sk);
1033 return len;
1034
1035fail:
1036 kfree_skb(skb);
1037out:
1038 release_sock(sk);
1039 return err;
1040}
1041
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001042/* iucv_fragment_skb() - Fragment a single IUCV message into multiple skb's
1043 *
1044 * Locking: must be called with message_q.lock held
1045 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001046static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
1047{
1048 int dataleft, size, copied = 0;
1049 struct sk_buff *nskb;
1050
1051 dataleft = len;
1052 while (dataleft) {
1053 if (dataleft >= sk->sk_rcvbuf / 4)
1054 size = sk->sk_rcvbuf / 4;
1055 else
1056 size = dataleft;
1057
1058 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
1059 if (!nskb)
1060 return -ENOMEM;
1061
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001062 /* copy target class to control buffer of new skb */
1063 memcpy(CB_TRGCLS(nskb), CB_TRGCLS(skb), CB_TRGCLS_LEN);
1064
1065 /* copy data fragment */
Ursula Braunf0703c82007-10-08 02:03:31 -07001066 memcpy(nskb->data, skb->data + copied, size);
1067 copied += size;
1068 dataleft -= size;
1069
1070 skb_reset_transport_header(nskb);
1071 skb_reset_network_header(nskb);
1072 nskb->len = size;
1073
1074 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
1075 }
1076
1077 return 0;
1078}
1079
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001080/* iucv_process_message() - Receive a single outstanding IUCV message
1081 *
1082 * Locking: must be called with message_q.lock held
1083 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001084static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1085 struct iucv_path *path,
1086 struct iucv_message *msg)
1087{
1088 int rc;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001089 unsigned int len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001090
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001091 len = iucv_msg_length(msg);
1092
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001093 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1094 /* Note: the first 4 bytes are reserved for msg tag */
1095 memcpy(CB_TRGCLS(skb), &msg->class, CB_TRGCLS_LEN);
1096
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001097 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1098 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1099 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1100 skb->data = NULL;
1101 skb->len = 0;
1102 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001103 } else {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001104 rc = iucv_message_receive(path, msg, msg->flags & IUCV_IPRMDATA,
1105 skb->data, len, NULL);
Ursula Braunf0703c82007-10-08 02:03:31 -07001106 if (rc) {
1107 kfree_skb(skb);
1108 return;
1109 }
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001110 /* we need to fragment iucv messages for SOCK_STREAM only;
1111 * for SOCK_SEQPACKET, it is only relevant if we support
1112 * record segmentation using MSG_EOR (see also recvmsg()) */
1113 if (sk->sk_type == SOCK_STREAM &&
1114 skb->truesize >= sk->sk_rcvbuf / 4) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001115 rc = iucv_fragment_skb(sk, skb, len);
Ursula Braunf0703c82007-10-08 02:03:31 -07001116 kfree_skb(skb);
1117 skb = NULL;
1118 if (rc) {
1119 iucv_path_sever(path, NULL);
1120 return;
1121 }
1122 skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
1123 } else {
1124 skb_reset_transport_header(skb);
1125 skb_reset_network_header(skb);
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001126 skb->len = len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001127 }
1128 }
1129
1130 if (sock_queue_rcv_skb(sk, skb))
1131 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
1132}
1133
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001134/* iucv_process_message_q() - Process outstanding IUCV messages
1135 *
1136 * Locking: must be called with message_q.lock held
1137 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001138static void iucv_process_message_q(struct sock *sk)
1139{
1140 struct iucv_sock *iucv = iucv_sk(sk);
1141 struct sk_buff *skb;
1142 struct sock_msg_q *p, *n;
1143
1144 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001145 skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -07001146 if (!skb)
1147 break;
1148 iucv_process_message(sk, skb, p->path, &p->msg);
1149 list_del(&p->list);
1150 kfree(p);
1151 if (!skb_queue_empty(&iucv->backlog_skb_q))
1152 break;
1153 }
1154}
1155
Jennifer Hunteac37312007-02-08 13:51:54 -08001156static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
1157 struct msghdr *msg, size_t len, int flags)
1158{
1159 int noblock = flags & MSG_DONTWAIT;
1160 struct sock *sk = sock->sk;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001161 struct iucv_sock *iucv = iucv_sk(sk);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001162 unsigned int copied, rlen;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001163 struct sk_buff *skb, *rskb, *cskb;
Jennifer Hunteac37312007-02-08 13:51:54 -08001164 int err = 0;
1165
Jennifer Hunt561e0362007-05-04 12:22:07 -07001166 if ((sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED) &&
Ursula Braunf0703c82007-10-08 02:03:31 -07001167 skb_queue_empty(&iucv->backlog_skb_q) &&
1168 skb_queue_empty(&sk->sk_receive_queue) &&
1169 list_empty(&iucv->message_q.list))
Jennifer Hunt561e0362007-05-04 12:22:07 -07001170 return 0;
1171
Jennifer Hunteac37312007-02-08 13:51:54 -08001172 if (flags & (MSG_OOB))
1173 return -EOPNOTSUPP;
1174
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001175 /* receive/dequeue next skb:
1176 * the function understands MSG_PEEK and, thus, does not dequeue skb */
Jennifer Hunteac37312007-02-08 13:51:54 -08001177 skb = skb_recv_datagram(sk, flags, noblock, &err);
1178 if (!skb) {
1179 if (sk->sk_shutdown & RCV_SHUTDOWN)
1180 return 0;
1181 return err;
1182 }
1183
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001184 rlen = skb->len; /* real length of skb */
1185 copied = min_t(unsigned int, rlen, len);
Jennifer Hunteac37312007-02-08 13:51:54 -08001186
Jennifer Hunt561e0362007-05-04 12:22:07 -07001187 cskb = skb;
1188 if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) {
Hendrik Brueckner802788b2009-04-21 23:26:26 +00001189 if (!(flags & MSG_PEEK))
1190 skb_queue_head(&sk->sk_receive_queue, skb);
1191 return -EFAULT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001192 }
1193
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001194 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1195 if (sk->sk_type == SOCK_SEQPACKET) {
1196 if (copied < rlen)
1197 msg->msg_flags |= MSG_TRUNC;
1198 /* each iucv message contains a complete record */
1199 msg->msg_flags |= MSG_EOR;
1200 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001201
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001202 /* create control message to store iucv msg target class:
1203 * get the trgcls from the control buffer of the skb due to
1204 * fragmentation of original iucv message. */
1205 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1206 CB_TRGCLS_LEN, CB_TRGCLS(skb));
1207 if (err) {
1208 if (!(flags & MSG_PEEK))
1209 skb_queue_head(&sk->sk_receive_queue, skb);
1210 return err;
1211 }
1212
Jennifer Hunteac37312007-02-08 13:51:54 -08001213 /* Mark read part of skb as used */
1214 if (!(flags & MSG_PEEK)) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001215
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001216 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1217 if (sk->sk_type == SOCK_STREAM) {
1218 skb_pull(skb, copied);
1219 if (skb->len) {
1220 skb_queue_head(&sk->sk_receive_queue, skb);
1221 goto done;
1222 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001223 }
1224
1225 kfree_skb(skb);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001226
1227 /* Queue backlog skbs */
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001228 spin_lock_bh(&iucv->message_q.lock);
Ursula Braunf0703c82007-10-08 02:03:31 -07001229 rskb = skb_dequeue(&iucv->backlog_skb_q);
Heiko Carstensda99f052007-05-04 12:23:27 -07001230 while (rskb) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001231 if (sock_queue_rcv_skb(sk, rskb)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001232 skb_queue_head(&iucv->backlog_skb_q,
Jennifer Hunt561e0362007-05-04 12:22:07 -07001233 rskb);
1234 break;
1235 } else {
Ursula Braunf0703c82007-10-08 02:03:31 -07001236 rskb = skb_dequeue(&iucv->backlog_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001237 }
1238 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001239 if (skb_queue_empty(&iucv->backlog_skb_q)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001240 if (!list_empty(&iucv->message_q.list))
1241 iucv_process_message_q(sk);
Ursula Braunf0703c82007-10-08 02:03:31 -07001242 }
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001243 spin_unlock_bh(&iucv->message_q.lock);
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001244 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001245
1246done:
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001247 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1248 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1249 copied = rlen;
1250
1251 return copied;
Jennifer Hunteac37312007-02-08 13:51:54 -08001252}
1253
1254static inline unsigned int iucv_accept_poll(struct sock *parent)
1255{
1256 struct iucv_sock *isk, *n;
1257 struct sock *sk;
1258
Heiko Carstensda99f052007-05-04 12:23:27 -07001259 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001260 sk = (struct sock *) isk;
1261
1262 if (sk->sk_state == IUCV_CONNECTED)
1263 return POLLIN | POLLRDNORM;
1264 }
1265
1266 return 0;
1267}
1268
1269unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
1270 poll_table *wait)
1271{
1272 struct sock *sk = sock->sk;
1273 unsigned int mask = 0;
1274
Eric Dumazetaa395142010-04-20 13:03:51 +00001275 sock_poll_wait(file, sk_sleep(sk), wait);
Jennifer Hunteac37312007-02-08 13:51:54 -08001276
1277 if (sk->sk_state == IUCV_LISTEN)
1278 return iucv_accept_poll(sk);
1279
1280 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1281 mask |= POLLERR;
1282
1283 if (sk->sk_shutdown & RCV_SHUTDOWN)
1284 mask |= POLLRDHUP;
1285
1286 if (sk->sk_shutdown == SHUTDOWN_MASK)
1287 mask |= POLLHUP;
1288
1289 if (!skb_queue_empty(&sk->sk_receive_queue) ||
Heiko Carstensda99f052007-05-04 12:23:27 -07001290 (sk->sk_shutdown & RCV_SHUTDOWN))
Jennifer Hunteac37312007-02-08 13:51:54 -08001291 mask |= POLLIN | POLLRDNORM;
1292
1293 if (sk->sk_state == IUCV_CLOSED)
1294 mask |= POLLHUP;
1295
Jennifer Hunt561e0362007-05-04 12:22:07 -07001296 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED)
1297 mask |= POLLIN;
1298
Jennifer Hunteac37312007-02-08 13:51:54 -08001299 if (sock_writeable(sk))
1300 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1301 else
1302 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1303
1304 return mask;
1305}
1306
1307static int iucv_sock_shutdown(struct socket *sock, int how)
1308{
1309 struct sock *sk = sock->sk;
1310 struct iucv_sock *iucv = iucv_sk(sk);
1311 struct iucv_message txmsg;
1312 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -08001313
1314 how++;
1315
1316 if ((how & ~SHUTDOWN_MASK) || !how)
1317 return -EINVAL;
1318
1319 lock_sock(sk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001320 switch (sk->sk_state) {
Hendrik Bruecknere14ad5f2009-04-21 06:04:23 +00001321 case IUCV_DISCONN:
1322 case IUCV_CLOSING:
1323 case IUCV_SEVERED:
Jennifer Hunteac37312007-02-08 13:51:54 -08001324 case IUCV_CLOSED:
1325 err = -ENOTCONN;
1326 goto fail;
1327
1328 default:
1329 sk->sk_shutdown |= how;
1330 break;
1331 }
1332
1333 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
1334 txmsg.class = 0;
1335 txmsg.tag = 0;
1336 err = iucv_message_send(iucv->path, &txmsg, IUCV_IPRMDATA, 0,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001337 (void *) iprm_shutdown, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -08001338 if (err) {
Heiko Carstensda99f052007-05-04 12:23:27 -07001339 switch (err) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001340 case 1:
1341 err = -ENOTCONN;
1342 break;
1343 case 2:
1344 err = -ECONNRESET;
1345 break;
1346 default:
1347 err = -ENOTCONN;
1348 break;
1349 }
1350 }
1351 }
1352
1353 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
1354 err = iucv_path_quiesce(iucv_sk(sk)->path, NULL);
1355 if (err)
1356 err = -ENOTCONN;
1357
1358 skb_queue_purge(&sk->sk_receive_queue);
1359 }
1360
1361 /* Wake up anyone sleeping in poll */
1362 sk->sk_state_change(sk);
1363
1364fail:
1365 release_sock(sk);
1366 return err;
1367}
1368
1369static int iucv_sock_release(struct socket *sock)
1370{
1371 struct sock *sk = sock->sk;
1372 int err = 0;
1373
1374 if (!sk)
1375 return 0;
1376
1377 iucv_sock_close(sk);
1378
1379 /* Unregister with IUCV base support */
1380 if (iucv_sk(sk)->path) {
1381 iucv_path_sever(iucv_sk(sk)->path, NULL);
1382 iucv_path_free(iucv_sk(sk)->path);
1383 iucv_sk(sk)->path = NULL;
1384 }
1385
Jennifer Hunteac37312007-02-08 13:51:54 -08001386 sock_orphan(sk);
1387 iucv_sock_kill(sk);
1388 return err;
1389}
1390
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001391/* getsockopt and setsockopt */
1392static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001393 char __user *optval, unsigned int optlen)
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001394{
1395 struct sock *sk = sock->sk;
1396 struct iucv_sock *iucv = iucv_sk(sk);
1397 int val;
1398 int rc;
1399
1400 if (level != SOL_IUCV)
1401 return -ENOPROTOOPT;
1402
1403 if (optlen < sizeof(int))
1404 return -EINVAL;
1405
1406 if (get_user(val, (int __user *) optval))
1407 return -EFAULT;
1408
1409 rc = 0;
1410
1411 lock_sock(sk);
1412 switch (optname) {
1413 case SO_IPRMDATA_MSG:
1414 if (val)
1415 iucv->flags |= IUCV_IPRMDATA;
1416 else
1417 iucv->flags &= ~IUCV_IPRMDATA;
1418 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001419 case SO_MSGLIMIT:
1420 switch (sk->sk_state) {
1421 case IUCV_OPEN:
1422 case IUCV_BOUND:
1423 if (val < 1 || val > (u16)(~0))
1424 rc = -EINVAL;
1425 else
1426 iucv->msglimit = val;
1427 break;
1428 default:
1429 rc = -EINVAL;
1430 break;
1431 }
1432 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001433 default:
1434 rc = -ENOPROTOOPT;
1435 break;
1436 }
1437 release_sock(sk);
1438
1439 return rc;
1440}
1441
1442static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1443 char __user *optval, int __user *optlen)
1444{
1445 struct sock *sk = sock->sk;
1446 struct iucv_sock *iucv = iucv_sk(sk);
1447 int val, len;
1448
1449 if (level != SOL_IUCV)
1450 return -ENOPROTOOPT;
1451
1452 if (get_user(len, optlen))
1453 return -EFAULT;
1454
1455 if (len < 0)
1456 return -EINVAL;
1457
1458 len = min_t(unsigned int, len, sizeof(int));
1459
1460 switch (optname) {
1461 case SO_IPRMDATA_MSG:
1462 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1463 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001464 case SO_MSGLIMIT:
1465 lock_sock(sk);
1466 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1467 : iucv->msglimit; /* default */
1468 release_sock(sk);
1469 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001470 default:
1471 return -ENOPROTOOPT;
1472 }
1473
1474 if (put_user(len, optlen))
1475 return -EFAULT;
1476 if (copy_to_user(optval, &val, len))
1477 return -EFAULT;
1478
1479 return 0;
1480}
1481
1482
Jennifer Hunteac37312007-02-08 13:51:54 -08001483/* Callback wrappers - called from iucv base support */
1484static int iucv_callback_connreq(struct iucv_path *path,
1485 u8 ipvmid[8], u8 ipuser[16])
1486{
1487 unsigned char user_data[16];
1488 unsigned char nuser_data[16];
1489 unsigned char src_name[8];
1490 struct hlist_node *node;
1491 struct sock *sk, *nsk;
1492 struct iucv_sock *iucv, *niucv;
1493 int err;
1494
1495 memcpy(src_name, ipuser, 8);
1496 EBCASC(src_name, 8);
1497 /* Find out if this path belongs to af_iucv. */
1498 read_lock(&iucv_sk_list.lock);
1499 iucv = NULL;
Ursula Braunfebca282007-07-14 19:04:25 -07001500 sk = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001501 sk_for_each(sk, node, &iucv_sk_list.head)
1502 if (sk->sk_state == IUCV_LISTEN &&
1503 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1504 /*
1505 * Found a listening socket with
1506 * src_name == ipuser[0-7].
1507 */
1508 iucv = iucv_sk(sk);
1509 break;
1510 }
1511 read_unlock(&iucv_sk_list.lock);
1512 if (!iucv)
1513 /* No socket found, not one of our paths. */
1514 return -EINVAL;
1515
1516 bh_lock_sock(sk);
1517
1518 /* Check if parent socket is listening */
1519 low_nmcpy(user_data, iucv->src_name);
1520 high_nmcpy(user_data, iucv->dst_name);
1521 ASCEBC(user_data, sizeof(user_data));
1522 if (sk->sk_state != IUCV_LISTEN) {
1523 err = iucv_path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001524 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001525 goto fail;
1526 }
1527
1528 /* Check for backlog size */
1529 if (sk_acceptq_is_full(sk)) {
1530 err = iucv_path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001531 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001532 goto fail;
1533 }
1534
1535 /* Create the new socket */
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001536 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
Heiko Carstensda99f052007-05-04 12:23:27 -07001537 if (!nsk) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001538 err = iucv_path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001539 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001540 goto fail;
1541 }
1542
1543 niucv = iucv_sk(nsk);
1544 iucv_sock_init(nsk, sk);
1545
1546 /* Set the new iucv_sock */
1547 memcpy(niucv->dst_name, ipuser + 8, 8);
1548 EBCASC(niucv->dst_name, 8);
1549 memcpy(niucv->dst_user_id, ipvmid, 8);
1550 memcpy(niucv->src_name, iucv->src_name, 8);
1551 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1552 niucv->path = path;
1553
1554 /* Call iucv_accept */
1555 high_nmcpy(nuser_data, ipuser + 8);
1556 memcpy(nuser_data + 8, niucv->src_name, 8);
1557 ASCEBC(nuser_data + 8, 8);
1558
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001559 /* set message limit for path based on msglimit of accepting socket */
1560 niucv->msglimit = iucv->msglimit;
1561 path->msglim = iucv->msglimit;
Jennifer Hunteac37312007-02-08 13:51:54 -08001562 err = iucv_path_accept(path, &af_iucv_handler, nuser_data, nsk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001563 if (err) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001564 err = iucv_path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001565 iucv_path_free(path);
1566 iucv_sock_kill(nsk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001567 goto fail;
1568 }
1569
1570 iucv_accept_enqueue(sk, nsk);
1571
1572 /* Wake up accept */
1573 nsk->sk_state = IUCV_CONNECTED;
1574 sk->sk_data_ready(sk, 1);
1575 err = 0;
1576fail:
1577 bh_unlock_sock(sk);
1578 return 0;
1579}
1580
1581static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1582{
1583 struct sock *sk = path->private;
1584
1585 sk->sk_state = IUCV_CONNECTED;
1586 sk->sk_state_change(sk);
1587}
1588
1589static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1590{
1591 struct sock *sk = path->private;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001592 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braunf0703c82007-10-08 02:03:31 -07001593 struct sk_buff *skb;
1594 struct sock_msg_q *save_msg;
1595 int len;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001596
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001597 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1598 iucv_message_reject(path, msg);
Jennifer Hunteac37312007-02-08 13:51:54 -08001599 return;
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001600 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001601
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001602 spin_lock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001603
Ursula Braunf0703c82007-10-08 02:03:31 -07001604 if (!list_empty(&iucv->message_q.list) ||
1605 !skb_queue_empty(&iucv->backlog_skb_q))
1606 goto save_message;
1607
1608 len = atomic_read(&sk->sk_rmem_alloc);
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001609 len += iucv_msg_length(msg) + sizeof(struct sk_buff);
Ursula Braunf0703c82007-10-08 02:03:31 -07001610 if (len > sk->sk_rcvbuf)
1611 goto save_message;
1612
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001613 skb = alloc_skb(iucv_msg_length(msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -07001614 if (!skb)
1615 goto save_message;
Jennifer Hunteac37312007-02-08 13:51:54 -08001616
Ursula Braunf0703c82007-10-08 02:03:31 -07001617 iucv_process_message(sk, skb, path, msg);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001618 goto out_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -08001619
Ursula Braunf0703c82007-10-08 02:03:31 -07001620save_message:
1621 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
Ursula Braund4444722008-02-07 18:07:19 -08001622 if (!save_msg)
1623 return;
Ursula Braunf0703c82007-10-08 02:03:31 -07001624 save_msg->path = path;
1625 save_msg->msg = *msg;
1626
Ursula Braunf0703c82007-10-08 02:03:31 -07001627 list_add_tail(&save_msg->list, &iucv->message_q.list);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001628
1629out_unlock:
Ursula Braunf0703c82007-10-08 02:03:31 -07001630 spin_unlock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001631}
1632
1633static void iucv_callback_txdone(struct iucv_path *path,
1634 struct iucv_message *msg)
1635{
1636 struct sock *sk = path->private;
Ursula Braunf2a77992008-02-07 18:07:44 -08001637 struct sk_buff *this = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001638 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1639 struct sk_buff *list_skb = list->next;
1640 unsigned long flags;
1641
Ursula Braunf2a77992008-02-07 18:07:44 -08001642 if (!skb_queue_empty(list)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001643 spin_lock_irqsave(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001644
Ursula Braunf2a77992008-02-07 18:07:44 -08001645 while (list_skb != (struct sk_buff *)list) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001646 if (!memcmp(&msg->tag, CB_TAG(list_skb), CB_TAG_LEN)) {
Ursula Braunf2a77992008-02-07 18:07:44 -08001647 this = list_skb;
1648 break;
1649 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001650 list_skb = list_skb->next;
Ursula Braunf2a77992008-02-07 18:07:44 -08001651 }
1652 if (this)
1653 __skb_unlink(this, list);
Jennifer Hunteac37312007-02-08 13:51:54 -08001654
Jennifer Hunt561e0362007-05-04 12:22:07 -07001655 spin_unlock_irqrestore(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001656
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001657 if (this) {
1658 kfree_skb(this);
1659 /* wake up any process waiting for sending */
1660 iucv_sock_wake_msglim(sk);
1661 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001662 }
Ursula Braunc2b4afd2008-07-14 09:59:29 +02001663 BUG_ON(!this);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001664
Heiko Carstensda99f052007-05-04 12:23:27 -07001665 if (sk->sk_state == IUCV_CLOSING) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001666 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1667 sk->sk_state = IUCV_CLOSED;
1668 sk->sk_state_change(sk);
1669 }
1670 }
1671
Jennifer Hunteac37312007-02-08 13:51:54 -08001672}
1673
1674static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1675{
1676 struct sock *sk = path->private;
1677
1678 if (!list_empty(&iucv_sk(sk)->accept_q))
1679 sk->sk_state = IUCV_SEVERED;
1680 else
1681 sk->sk_state = IUCV_DISCONN;
1682
1683 sk->sk_state_change(sk);
1684}
1685
Hendrik Brueckneraf88b522009-04-21 23:26:21 +00001686/* called if the other communication side shuts down its RECV direction;
1687 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1688 */
1689static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1690{
1691 struct sock *sk = path->private;
1692
1693 bh_lock_sock(sk);
1694 if (sk->sk_state != IUCV_CLOSED) {
1695 sk->sk_shutdown |= SEND_SHUTDOWN;
1696 sk->sk_state_change(sk);
1697 }
1698 bh_unlock_sock(sk);
1699}
1700
Alexey Dobriyan5708e862009-09-14 12:23:23 +00001701static const struct proto_ops iucv_sock_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08001702 .family = PF_IUCV,
1703 .owner = THIS_MODULE,
1704 .release = iucv_sock_release,
1705 .bind = iucv_sock_bind,
1706 .connect = iucv_sock_connect,
1707 .listen = iucv_sock_listen,
1708 .accept = iucv_sock_accept,
1709 .getname = iucv_sock_getname,
1710 .sendmsg = iucv_sock_sendmsg,
1711 .recvmsg = iucv_sock_recvmsg,
1712 .poll = iucv_sock_poll,
1713 .ioctl = sock_no_ioctl,
1714 .mmap = sock_no_mmap,
1715 .socketpair = sock_no_socketpair,
1716 .shutdown = iucv_sock_shutdown,
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001717 .setsockopt = iucv_sock_setsockopt,
1718 .getsockopt = iucv_sock_getsockopt,
Jennifer Hunteac37312007-02-08 13:51:54 -08001719};
1720
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001721static const struct net_proto_family iucv_sock_family_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08001722 .family = AF_IUCV,
1723 .owner = THIS_MODULE,
1724 .create = iucv_sock_create,
1725};
1726
Heiko Carstensda99f052007-05-04 12:23:27 -07001727static int __init afiucv_init(void)
Jennifer Hunteac37312007-02-08 13:51:54 -08001728{
1729 int err;
1730
1731 if (!MACHINE_IS_VM) {
Ursula Braun8f7c5022008-12-25 13:39:47 +01001732 pr_err("The af_iucv module cannot be loaded"
1733 " without z/VM\n");
Jennifer Hunteac37312007-02-08 13:51:54 -08001734 err = -EPROTONOSUPPORT;
1735 goto out;
1736 }
1737 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
1738 if (unlikely(err)) {
Ursula Braunc2b4afd2008-07-14 09:59:29 +02001739 WARN_ON(err);
Jennifer Hunteac37312007-02-08 13:51:54 -08001740 err = -EPROTONOSUPPORT;
1741 goto out;
1742 }
1743
1744 err = iucv_register(&af_iucv_handler, 0);
1745 if (err)
1746 goto out;
1747 err = proto_register(&iucv_proto, 0);
1748 if (err)
1749 goto out_iucv;
1750 err = sock_register(&iucv_sock_family_ops);
1751 if (err)
1752 goto out_proto;
Ursula Braunc23cad92009-06-16 10:30:44 +02001753 /* establish dummy device */
1754 err = driver_register(&af_iucv_driver);
1755 if (err)
1756 goto out_sock;
1757 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1758 if (!af_iucv_dev) {
1759 err = -ENOMEM;
1760 goto out_driver;
1761 }
1762 dev_set_name(af_iucv_dev, "af_iucv");
1763 af_iucv_dev->bus = &iucv_bus;
1764 af_iucv_dev->parent = iucv_root;
1765 af_iucv_dev->release = (void (*)(struct device *))kfree;
1766 af_iucv_dev->driver = &af_iucv_driver;
1767 err = device_register(af_iucv_dev);
1768 if (err)
1769 goto out_driver;
1770
Jennifer Hunteac37312007-02-08 13:51:54 -08001771 return 0;
1772
Ursula Braunc23cad92009-06-16 10:30:44 +02001773out_driver:
1774 driver_unregister(&af_iucv_driver);
1775out_sock:
1776 sock_unregister(PF_IUCV);
Jennifer Hunteac37312007-02-08 13:51:54 -08001777out_proto:
1778 proto_unregister(&iucv_proto);
1779out_iucv:
1780 iucv_unregister(&af_iucv_handler, 0);
1781out:
1782 return err;
1783}
1784
1785static void __exit afiucv_exit(void)
1786{
Ursula Braunc23cad92009-06-16 10:30:44 +02001787 device_unregister(af_iucv_dev);
1788 driver_unregister(&af_iucv_driver);
Jennifer Hunteac37312007-02-08 13:51:54 -08001789 sock_unregister(PF_IUCV);
1790 proto_unregister(&iucv_proto);
1791 iucv_unregister(&af_iucv_handler, 0);
Jennifer Hunteac37312007-02-08 13:51:54 -08001792}
1793
1794module_init(afiucv_init);
1795module_exit(afiucv_exit);
1796
1797MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
1798MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
1799MODULE_VERSION(VERSION);
1800MODULE_LICENSE("GPL");
1801MODULE_ALIAS_NETPROTO(PF_IUCV);