blob: 1d8cb72da3a46b55bd9710df8170e950c6ff22f4 [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
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
53/* macros to set/get socket control buffer at correct offset */
54#define CB_TAG(skb) ((skb)->cb) /* iucv message tag */
55#define CB_TAG_LEN (sizeof(((struct iucv_message *) 0)->tag))
56#define CB_TRGCLS(skb) ((skb)->cb + CB_TAG_LEN) /* iucv msg target class */
57#define CB_TRGCLS_LEN (TRGCLS_SIZE)
58
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000059#define __iucv_sock_wait(sk, condition, timeo, ret) \
60do { \
61 DEFINE_WAIT(__wait); \
62 long __timeo = timeo; \
63 ret = 0; \
Eric Dumazetaa395142010-04-20 13:03:51 +000064 prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000065 while (!(condition)) { \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000066 if (!__timeo) { \
67 ret = -EAGAIN; \
68 break; \
69 } \
70 if (signal_pending(current)) { \
71 ret = sock_intr_errno(__timeo); \
72 break; \
73 } \
74 release_sock(sk); \
75 __timeo = schedule_timeout(__timeo); \
76 lock_sock(sk); \
77 ret = sock_error(sk); \
78 if (ret) \
79 break; \
80 } \
Eric Dumazetaa395142010-04-20 13:03:51 +000081 finish_wait(sk_sleep(sk), &__wait); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000082} while (0)
83
84#define iucv_sock_wait(sk, condition, timeo) \
85({ \
86 int __ret = 0; \
87 if (!(condition)) \
88 __iucv_sock_wait(sk, condition, timeo, __ret); \
89 __ret; \
90})
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000091
Heiko Carstens57f20442007-10-08 02:02:52 -070092static void iucv_sock_kill(struct sock *sk);
93static void iucv_sock_close(struct sock *sk);
94
Jennifer Hunteac37312007-02-08 13:51:54 -080095/* Call Back functions */
96static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
97static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
98static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
Heiko Carstensda99f052007-05-04 12:23:27 -070099static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8],
100 u8 ipuser[16]);
Jennifer Hunteac37312007-02-08 13:51:54 -0800101static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
Hendrik Brueckneraf88b522009-04-21 23:26:21 +0000102static void iucv_callback_shutdown(struct iucv_path *, u8 ipuser[16]);
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;
153 struct hlist_node *node;
154 int err = 0;
155
156#ifdef CONFIG_PM_DEBUG
157 printk(KERN_WARNING "afiucv_pm_freeze\n");
158#endif
159 read_lock(&iucv_sk_list.lock);
160 sk_for_each(sk, node, &iucv_sk_list.head) {
161 iucv = iucv_sk(sk);
162 skb_queue_purge(&iucv->send_skb_q);
163 skb_queue_purge(&iucv->backlog_skb_q);
164 switch (sk->sk_state) {
165 case IUCV_SEVERED:
166 case IUCV_DISCONN:
167 case IUCV_CLOSING:
168 case IUCV_CONNECTED:
169 if (iucv->path) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000170 err = pr_iucv->path_sever(iucv->path, NULL);
Ursula Braunc23cad92009-06-16 10:30:44 +0200171 iucv_path_free(iucv->path);
172 iucv->path = NULL;
173 }
174 break;
175 case IUCV_OPEN:
176 case IUCV_BOUND:
177 case IUCV_LISTEN:
178 case IUCV_CLOSED:
179 default:
180 break;
181 }
182 }
183 read_unlock(&iucv_sk_list.lock);
184 return err;
185}
186
187/**
188 * afiucv_pm_restore_thaw() - Thaw and restore PM callback
189 * @dev: AFIUCV dummy device
190 *
191 * socket clean up after freeze
192 */
193static int afiucv_pm_restore_thaw(struct device *dev)
194{
Ursula Braunc23cad92009-06-16 10:30:44 +0200195 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) {
Ursula Braunc23cad92009-06-16 10:30:44 +0200203 switch (sk->sk_state) {
204 case IUCV_CONNECTED:
205 sk->sk_err = EPIPE;
206 sk->sk_state = IUCV_DISCONN;
207 sk->sk_state_change(sk);
208 break;
209 case IUCV_DISCONN:
210 case IUCV_SEVERED:
211 case IUCV_CLOSING:
212 case IUCV_LISTEN:
213 case IUCV_BOUND:
214 case IUCV_OPEN:
215 default:
216 break;
217 }
218 }
219 read_unlock(&iucv_sk_list.lock);
220 return 0;
221}
222
Alexey Dobriyan47145212009-12-14 18:00:08 -0800223static const struct dev_pm_ops afiucv_pm_ops = {
Ursula Braunc23cad92009-06-16 10:30:44 +0200224 .prepare = afiucv_pm_prepare,
225 .complete = afiucv_pm_complete,
226 .freeze = afiucv_pm_freeze,
227 .thaw = afiucv_pm_restore_thaw,
228 .restore = afiucv_pm_restore_thaw,
229};
230
231static struct device_driver af_iucv_driver = {
232 .owner = THIS_MODULE,
233 .name = "afiucv",
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000234 .bus = NULL,
Ursula Braunc23cad92009-06-16 10:30:44 +0200235 .pm = &afiucv_pm_ops,
236};
237
238/* dummy device used as trigger for PM functions */
239static struct device *af_iucv_dev;
240
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000241/**
242 * iucv_msg_length() - Returns the length of an iucv message.
243 * @msg: Pointer to struct iucv_message, MUST NOT be NULL
244 *
245 * The function returns the length of the specified iucv message @msg of data
246 * stored in a buffer and of data stored in the parameter list (PRMDATA).
247 *
248 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
249 * data:
250 * PRMDATA[0..6] socket data (max 7 bytes);
251 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
252 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300253 * The socket data length is computed by subtracting the socket data length
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000254 * value from 0xFF.
255 * If the socket data len is greater 7, then PRMDATA can be used for special
256 * notifications (see iucv_sock_shutdown); and further,
257 * if the socket data len is > 7, the function returns 8.
258 *
259 * Use this function to allocate socket buffers to store iucv message data.
260 */
261static inline size_t iucv_msg_length(struct iucv_message *msg)
262{
263 size_t datalen;
264
265 if (msg->flags & IUCV_IPRMDATA) {
266 datalen = 0xff - msg->rmmsg[7];
267 return (datalen < 8) ? datalen : 8;
268 }
269 return msg->length;
270}
271
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000272/**
273 * iucv_sock_in_state() - check for specific states
274 * @sk: sock structure
275 * @state: first iucv sk state
276 * @state: second iucv sk state
277 *
278 * Returns true if the socket in either in the first or second state.
279 */
280static int iucv_sock_in_state(struct sock *sk, int state, int state2)
281{
282 return (sk->sk_state == state || sk->sk_state == state2);
283}
284
285/**
286 * iucv_below_msglim() - function to check if messages can be sent
287 * @sk: sock structure
288 *
289 * Returns true if the send queue length is lower than the message limit.
290 * Always returns true if the socket is not connected (no iucv path for
291 * checking the message limit).
292 */
293static inline int iucv_below_msglim(struct sock *sk)
294{
295 struct iucv_sock *iucv = iucv_sk(sk);
296
297 if (sk->sk_state != IUCV_CONNECTED)
298 return 1;
299 return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim);
300}
301
302/**
303 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
304 */
305static void iucv_sock_wake_msglim(struct sock *sk)
306{
Eric Dumazet43815482010-04-29 11:01:49 +0000307 struct socket_wq *wq;
308
309 rcu_read_lock();
310 wq = rcu_dereference(sk->sk_wq);
311 if (wq_has_sleeper(wq))
312 wake_up_interruptible_all(&wq->wait);
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000313 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Eric Dumazet43815482010-04-29 11:01:49 +0000314 rcu_read_unlock();
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000315}
316
Jennifer Hunteac37312007-02-08 13:51:54 -0800317/* Timers */
318static void iucv_sock_timeout(unsigned long arg)
319{
320 struct sock *sk = (struct sock *)arg;
321
322 bh_lock_sock(sk);
323 sk->sk_err = ETIMEDOUT;
324 sk->sk_state_change(sk);
325 bh_unlock_sock(sk);
326
327 iucv_sock_kill(sk);
328 sock_put(sk);
329}
330
331static void iucv_sock_clear_timer(struct sock *sk)
332{
333 sk_stop_timer(sk, &sk->sk_timer);
334}
335
Jennifer Hunteac37312007-02-08 13:51:54 -0800336static struct sock *__iucv_get_sock_by_name(char *nm)
337{
338 struct sock *sk;
339 struct hlist_node *node;
340
341 sk_for_each(sk, node, &iucv_sk_list.head)
342 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
343 return sk;
344
345 return NULL;
346}
347
348static void iucv_sock_destruct(struct sock *sk)
349{
350 skb_queue_purge(&sk->sk_receive_queue);
351 skb_queue_purge(&sk->sk_write_queue);
352}
353
354/* Cleanup Listen */
355static void iucv_sock_cleanup_listen(struct sock *parent)
356{
357 struct sock *sk;
358
359 /* Close non-accepted connections */
360 while ((sk = iucv_accept_dequeue(parent, NULL))) {
361 iucv_sock_close(sk);
362 iucv_sock_kill(sk);
363 }
364
365 parent->sk_state = IUCV_CLOSED;
Jennifer Hunteac37312007-02-08 13:51:54 -0800366}
367
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000368/* Kill socket (only if zapped and orphaned) */
Jennifer Hunteac37312007-02-08 13:51:54 -0800369static void iucv_sock_kill(struct sock *sk)
370{
371 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
372 return;
373
374 iucv_sock_unlink(&iucv_sk_list, sk);
375 sock_set_flag(sk, SOCK_DEAD);
376 sock_put(sk);
377}
378
379/* Close an IUCV socket */
380static void iucv_sock_close(struct sock *sk)
381{
382 unsigned char user_data[16];
383 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700384 unsigned long timeo;
Jennifer Hunteac37312007-02-08 13:51:54 -0800385
386 iucv_sock_clear_timer(sk);
387 lock_sock(sk);
388
Heiko Carstensda99f052007-05-04 12:23:27 -0700389 switch (sk->sk_state) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800390 case IUCV_LISTEN:
391 iucv_sock_cleanup_listen(sk);
392 break;
393
394 case IUCV_CONNECTED:
395 case IUCV_DISCONN:
Jennifer Hunt561e0362007-05-04 12:22:07 -0700396 sk->sk_state = IUCV_CLOSING;
397 sk->sk_state_change(sk);
398
Heiko Carstensda99f052007-05-04 12:23:27 -0700399 if (!skb_queue_empty(&iucv->send_skb_q)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -0700400 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
401 timeo = sk->sk_lingertime;
402 else
403 timeo = IUCV_DISCONN_TIMEOUT;
Ursula Braun9f6298a2011-05-12 18:45:08 +0000404 iucv_sock_wait(sk,
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000405 iucv_sock_in_state(sk, IUCV_CLOSED, 0),
406 timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700407 }
408
Ursula Braunbbe188c2009-04-21 06:04:20 +0000409 case IUCV_CLOSING: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700410 sk->sk_state = IUCV_CLOSED;
411 sk->sk_state_change(sk);
412
Jennifer Hunteac37312007-02-08 13:51:54 -0800413 if (iucv->path) {
414 low_nmcpy(user_data, iucv->src_name);
415 high_nmcpy(user_data, iucv->dst_name);
416 ASCEBC(user_data, sizeof(user_data));
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000417 pr_iucv->path_sever(iucv->path, user_data);
Jennifer Hunteac37312007-02-08 13:51:54 -0800418 iucv_path_free(iucv->path);
419 iucv->path = NULL;
420 }
421
Jennifer Hunteac37312007-02-08 13:51:54 -0800422 sk->sk_err = ECONNRESET;
423 sk->sk_state_change(sk);
424
425 skb_queue_purge(&iucv->send_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700426 skb_queue_purge(&iucv->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800427 break;
428
429 default:
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000430 /* nothing to do here */
Jennifer Hunteac37312007-02-08 13:51:54 -0800431 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700432 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800433
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000434 /* mark socket for deletion by iucv_sock_kill() */
435 sock_set_flag(sk, SOCK_ZAPPED);
436
Jennifer Hunteac37312007-02-08 13:51:54 -0800437 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800438}
439
440static void iucv_sock_init(struct sock *sk, struct sock *parent)
441{
442 if (parent)
443 sk->sk_type = parent->sk_type;
444}
445
446static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
447{
448 struct sock *sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000449 struct iucv_sock *iucv;
Jennifer Hunteac37312007-02-08 13:51:54 -0800450
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700451 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -0800452 if (!sk)
453 return NULL;
Ursula Braun493d3972011-08-08 01:33:52 +0000454 iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800455
456 sock_init_data(sock, sk);
Ursula Braun493d3972011-08-08 01:33:52 +0000457 INIT_LIST_HEAD(&iucv->accept_q);
458 spin_lock_init(&iucv->accept_q_lock);
459 skb_queue_head_init(&iucv->send_skb_q);
460 INIT_LIST_HEAD(&iucv->message_q.list);
461 spin_lock_init(&iucv->message_q.lock);
462 skb_queue_head_init(&iucv->backlog_skb_q);
463 iucv->send_tag = 0;
464 iucv->flags = 0;
465 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
466 iucv->path = NULL;
467 memset(&iucv->src_user_id , 0, 32);
Jennifer Hunteac37312007-02-08 13:51:54 -0800468
469 sk->sk_destruct = iucv_sock_destruct;
470 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
471 sk->sk_allocation = GFP_DMA;
472
473 sock_reset_flag(sk, SOCK_ZAPPED);
474
475 sk->sk_protocol = proto;
476 sk->sk_state = IUCV_OPEN;
477
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800478 setup_timer(&sk->sk_timer, iucv_sock_timeout, (unsigned long)sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800479
480 iucv_sock_link(&iucv_sk_list, sk);
481 return sk;
482}
483
484/* Create an IUCV socket */
Eric Paris3f378b62009-11-05 22:18:14 -0800485static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
486 int kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800487{
488 struct sock *sk;
489
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000490 if (protocol && protocol != PF_IUCV)
491 return -EPROTONOSUPPORT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800492
493 sock->state = SS_UNCONNECTED;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000494
495 switch (sock->type) {
496 case SOCK_STREAM:
497 sock->ops = &iucv_sock_ops;
498 break;
499 case SOCK_SEQPACKET:
500 /* currently, proto ops can handle both sk types */
501 sock->ops = &iucv_sock_ops;
502 break;
503 default:
504 return -ESOCKTNOSUPPORT;
505 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800506
507 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
508 if (!sk)
509 return -ENOMEM;
510
511 iucv_sock_init(sk, NULL);
512
513 return 0;
514}
515
516void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
517{
518 write_lock_bh(&l->lock);
519 sk_add_node(sk, &l->head);
520 write_unlock_bh(&l->lock);
521}
522
523void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
524{
525 write_lock_bh(&l->lock);
526 sk_del_node_init(sk);
527 write_unlock_bh(&l->lock);
528}
529
530void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
531{
Ursula Braunfebca282007-07-14 19:04:25 -0700532 unsigned long flags;
533 struct iucv_sock *par = iucv_sk(parent);
534
Jennifer Hunteac37312007-02-08 13:51:54 -0800535 sock_hold(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700536 spin_lock_irqsave(&par->accept_q_lock, flags);
537 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
538 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800539 iucv_sk(sk)->parent = parent;
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000540 sk_acceptq_added(parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800541}
542
543void iucv_accept_unlink(struct sock *sk)
544{
Ursula Braunfebca282007-07-14 19:04:25 -0700545 unsigned long flags;
546 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
547
548 spin_lock_irqsave(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800549 list_del_init(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700550 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000551 sk_acceptq_removed(iucv_sk(sk)->parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800552 iucv_sk(sk)->parent = NULL;
553 sock_put(sk);
554}
555
556struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
557{
558 struct iucv_sock *isk, *n;
559 struct sock *sk;
560
Heiko Carstensda99f052007-05-04 12:23:27 -0700561 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800562 sk = (struct sock *) isk;
563 lock_sock(sk);
564
565 if (sk->sk_state == IUCV_CLOSED) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800566 iucv_accept_unlink(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700567 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800568 continue;
569 }
570
571 if (sk->sk_state == IUCV_CONNECTED ||
572 sk->sk_state == IUCV_SEVERED ||
Hendrik Brueckner56a73de2009-09-16 04:37:26 +0000573 sk->sk_state == IUCV_DISCONN || /* due to PM restore */
Jennifer Hunteac37312007-02-08 13:51:54 -0800574 !newsock) {
575 iucv_accept_unlink(sk);
576 if (newsock)
577 sock_graft(sk, newsock);
578
579 if (sk->sk_state == IUCV_SEVERED)
580 sk->sk_state = IUCV_DISCONN;
581
582 release_sock(sk);
583 return sk;
584 }
585
586 release_sock(sk);
587 }
588 return NULL;
589}
590
Jennifer Hunteac37312007-02-08 13:51:54 -0800591/* Bind an unbound socket */
592static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
593 int addr_len)
594{
595 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
596 struct sock *sk = sock->sk;
597 struct iucv_sock *iucv;
598 int err;
599
600 /* Verify the input sockaddr */
601 if (!addr || addr->sa_family != AF_IUCV)
602 return -EINVAL;
603
604 lock_sock(sk);
605 if (sk->sk_state != IUCV_OPEN) {
606 err = -EBADFD;
607 goto done;
608 }
609
610 write_lock_bh(&iucv_sk_list.lock);
611
612 iucv = iucv_sk(sk);
613 if (__iucv_get_sock_by_name(sa->siucv_name)) {
614 err = -EADDRINUSE;
615 goto done_unlock;
616 }
617 if (iucv->path) {
618 err = 0;
619 goto done_unlock;
620 }
621
622 /* Bind the socket */
623 memcpy(iucv->src_name, sa->siucv_name, 8);
624
625 /* Copy the user id */
626 memcpy(iucv->src_user_id, iucv_userid, 8);
627 sk->sk_state = IUCV_BOUND;
628 err = 0;
629
630done_unlock:
631 /* Release the socket list lock */
632 write_unlock_bh(&iucv_sk_list.lock);
633done:
634 release_sock(sk);
635 return err;
636}
637
638/* Automatically bind an unbound socket */
639static int iucv_sock_autobind(struct sock *sk)
640{
641 struct iucv_sock *iucv = iucv_sk(sk);
642 char query_buffer[80];
643 char name[12];
644 int err = 0;
645
646 /* Set the userid and name */
647 cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err);
648 if (unlikely(err))
649 return -EPROTO;
650
651 memcpy(iucv->src_user_id, query_buffer, 8);
652
653 write_lock_bh(&iucv_sk_list.lock);
654
655 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
656 while (__iucv_get_sock_by_name(name)) {
657 sprintf(name, "%08x",
658 atomic_inc_return(&iucv_sk_list.autobind_name));
659 }
660
661 write_unlock_bh(&iucv_sk_list.lock);
662
663 memcpy(&iucv->src_name, name, 8);
664
665 return err;
666}
667
668/* Connect an unconnected socket */
669static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
670 int alen, int flags)
671{
672 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
673 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000674 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800675 unsigned char user_data[16];
676 int err;
677
678 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
679 return -EINVAL;
680
681 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
682 return -EBADFD;
683
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000684 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800685 return -EINVAL;
686
Jennifer Hunteac37312007-02-08 13:51:54 -0800687 if (sk->sk_state == IUCV_OPEN) {
688 err = iucv_sock_autobind(sk);
689 if (unlikely(err))
690 return err;
691 }
692
693 lock_sock(sk);
694
695 /* Set the destination information */
Ursula Braun493d3972011-08-08 01:33:52 +0000696 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
697 memcpy(iucv->dst_name, sa->siucv_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800698
699 high_nmcpy(user_data, sa->siucv_name);
Ursula Braun493d3972011-08-08 01:33:52 +0000700 low_nmcpy(user_data, iucv->src_name);
Jennifer Hunteac37312007-02-08 13:51:54 -0800701 ASCEBC(user_data, sizeof(user_data));
702
Jennifer Hunteac37312007-02-08 13:51:54 -0800703 /* Create path. */
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +0000704 iucv->path = iucv_path_alloc(iucv->msglimit,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000705 IUCV_IPRMDATA, GFP_KERNEL);
Ursula Braund4444722008-02-07 18:07:19 -0800706 if (!iucv->path) {
707 err = -ENOMEM;
708 goto done;
709 }
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000710 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
711 sa->siucv_user_id, NULL, user_data,
712 sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800713 if (err) {
714 iucv_path_free(iucv->path);
715 iucv->path = NULL;
Hendrik Brueckner55cdea92009-01-05 18:07:07 -0800716 switch (err) {
717 case 0x0b: /* Target communicator is not logged on */
718 err = -ENETUNREACH;
719 break;
720 case 0x0d: /* Max connections for this guest exceeded */
721 case 0x0e: /* Max connections for target guest exceeded */
722 err = -EAGAIN;
723 break;
724 case 0x0f: /* Missing IUCV authorization */
725 err = -EACCES;
726 break;
727 default:
728 err = -ECONNREFUSED;
729 break;
730 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800731 goto done;
732 }
733
734 if (sk->sk_state != IUCV_CONNECTED) {
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000735 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
736 IUCV_DISCONN),
737 sock_sndtimeo(sk, flags & O_NONBLOCK));
Jennifer Hunteac37312007-02-08 13:51:54 -0800738 }
739
740 if (sk->sk_state == IUCV_DISCONN) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000741 err = -ECONNREFUSED;
Jennifer Hunteac37312007-02-08 13:51:54 -0800742 }
Ursula Braun18becbc2009-01-05 18:07:46 -0800743
744 if (err) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000745 pr_iucv->path_sever(iucv->path, NULL);
Ursula Braun18becbc2009-01-05 18:07:46 -0800746 iucv_path_free(iucv->path);
747 iucv->path = NULL;
748 }
749
Jennifer Hunteac37312007-02-08 13:51:54 -0800750done:
751 release_sock(sk);
752 return err;
753}
754
755/* Move a socket into listening state. */
756static int iucv_sock_listen(struct socket *sock, int backlog)
757{
758 struct sock *sk = sock->sk;
759 int err;
760
761 lock_sock(sk);
762
763 err = -EINVAL;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000764 if (sk->sk_state != IUCV_BOUND)
765 goto done;
766
767 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800768 goto done;
769
770 sk->sk_max_ack_backlog = backlog;
771 sk->sk_ack_backlog = 0;
772 sk->sk_state = IUCV_LISTEN;
773 err = 0;
774
775done:
776 release_sock(sk);
777 return err;
778}
779
780/* Accept a pending connection */
781static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
782 int flags)
783{
784 DECLARE_WAITQUEUE(wait, current);
785 struct sock *sk = sock->sk, *nsk;
786 long timeo;
787 int err = 0;
788
Jennifer Hunt561e0362007-05-04 12:22:07 -0700789 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800790
791 if (sk->sk_state != IUCV_LISTEN) {
792 err = -EBADFD;
793 goto done;
794 }
795
796 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
797
798 /* Wait for an incoming connection */
Eric Dumazetaa395142010-04-20 13:03:51 +0000799 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Heiko Carstensda99f052007-05-04 12:23:27 -0700800 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800801 set_current_state(TASK_INTERRUPTIBLE);
802 if (!timeo) {
803 err = -EAGAIN;
804 break;
805 }
806
807 release_sock(sk);
808 timeo = schedule_timeout(timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700809 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800810
811 if (sk->sk_state != IUCV_LISTEN) {
812 err = -EBADFD;
813 break;
814 }
815
816 if (signal_pending(current)) {
817 err = sock_intr_errno(timeo);
818 break;
819 }
820 }
821
822 set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000823 remove_wait_queue(sk_sleep(sk), &wait);
Jennifer Hunteac37312007-02-08 13:51:54 -0800824
825 if (err)
826 goto done;
827
828 newsock->state = SS_CONNECTED;
829
830done:
831 release_sock(sk);
832 return err;
833}
834
835static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
836 int *len, int peer)
837{
838 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
839 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000840 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800841
842 addr->sa_family = AF_IUCV;
843 *len = sizeof(struct sockaddr_iucv);
844
845 if (peer) {
Ursula Braun493d3972011-08-08 01:33:52 +0000846 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
847 memcpy(siucv->siucv_name, iucv->dst_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800848 } else {
Ursula Braun493d3972011-08-08 01:33:52 +0000849 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
850 memcpy(siucv->siucv_name, iucv->src_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800851 }
852 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
853 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
Ursula Braun493d3972011-08-08 01:33:52 +0000854 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
Jennifer Hunteac37312007-02-08 13:51:54 -0800855
856 return 0;
857}
858
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000859/**
860 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
861 * @path: IUCV path
862 * @msg: Pointer to a struct iucv_message
863 * @skb: The socket data to send, skb->len MUST BE <= 7
864 *
865 * Send the socket data in the parameter list in the iucv message
866 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
867 * list and the socket data len at index 7 (last byte).
868 * See also iucv_msg_length().
869 *
870 * Returns the error code from the iucv_message_send() call.
871 */
872static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
873 struct sk_buff *skb)
874{
875 u8 prmdata[8];
876
877 memcpy(prmdata, (void *) skb->data, skb->len);
878 prmdata[7] = 0xff - (u8) skb->len;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000879 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000880 (void *) prmdata, 8);
881}
882
Jennifer Hunteac37312007-02-08 13:51:54 -0800883static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
884 struct msghdr *msg, size_t len)
885{
886 struct sock *sk = sock->sk;
887 struct iucv_sock *iucv = iucv_sk(sk);
888 struct sk_buff *skb;
889 struct iucv_message txmsg;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000890 struct cmsghdr *cmsg;
891 int cmsg_done;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000892 long timeo;
Ursula Braun8f7c5022008-12-25 13:39:47 +0100893 char user_id[9];
894 char appl_id[9];
Jennifer Hunteac37312007-02-08 13:51:54 -0800895 int err;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000896 int noblock = msg->msg_flags & MSG_DONTWAIT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800897
898 err = sock_error(sk);
899 if (err)
900 return err;
901
902 if (msg->msg_flags & MSG_OOB)
903 return -EOPNOTSUPP;
904
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000905 /* SOCK_SEQPACKET: we do not support segmented records */
906 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
907 return -EOPNOTSUPP;
908
Jennifer Hunteac37312007-02-08 13:51:54 -0800909 lock_sock(sk);
910
911 if (sk->sk_shutdown & SEND_SHUTDOWN) {
912 err = -EPIPE;
913 goto out;
914 }
915
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000916 /* Return if the socket is not in connected state */
917 if (sk->sk_state != IUCV_CONNECTED) {
918 err = -ENOTCONN;
919 goto out;
920 }
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000921
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000922 /* initialize defaults */
923 cmsg_done = 0; /* check for duplicate headers */
924 txmsg.class = 0;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000925
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000926 /* iterate over control messages */
927 for (cmsg = CMSG_FIRSTHDR(msg); cmsg;
928 cmsg = CMSG_NXTHDR(msg, cmsg)) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000929
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000930 if (!CMSG_OK(msg, cmsg)) {
931 err = -EINVAL;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700932 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -0800933 }
934
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000935 if (cmsg->cmsg_level != SOL_IUCV)
936 continue;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000937
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000938 if (cmsg->cmsg_type & cmsg_done) {
939 err = -EINVAL;
940 goto out;
941 }
942 cmsg_done |= cmsg->cmsg_type;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000943
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000944 switch (cmsg->cmsg_type) {
945 case SCM_IUCV_TRGCLS:
946 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
947 err = -EINVAL;
948 goto out;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000949 }
950
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000951 /* set iucv message target class */
952 memcpy(&txmsg.class,
953 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
954
955 break;
956
957 default:
958 err = -EINVAL;
959 goto out;
960 break;
961 }
962 }
963
964 /* allocate one skb for each iucv message:
965 * this is fine for SOCK_SEQPACKET (unless we want to support
966 * segmented records using the MSG_EOR flag), but
967 * for SOCK_STREAM we might want to improve it in future */
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000968 skb = sock_alloc_send_skb(sk, len, noblock, &err);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000969 if (!skb)
970 goto out;
971 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
972 err = -EFAULT;
973 goto fail;
974 }
975
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000976 /* wait if outstanding messages for iucv path has reached */
977 timeo = sock_sndtimeo(sk, noblock);
978 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
979 if (err)
980 goto fail;
981
982 /* return -ECONNRESET if the socket is no longer connected */
983 if (sk->sk_state != IUCV_CONNECTED) {
984 err = -ECONNRESET;
985 goto fail;
986 }
987
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +0000988 /* increment and save iucv message tag for msg_completion cbk */
989 txmsg.tag = iucv->send_tag++;
990 memcpy(CB_TAG(skb), &txmsg.tag, CB_TAG_LEN);
991 skb_queue_tail(&iucv->send_skb_q, skb);
992
993 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags)
994 && skb->len <= 7) {
995 err = iucv_send_iprm(iucv->path, &txmsg, skb);
996
997 /* on success: there is no message_complete callback
998 * for an IPRMDATA msg; remove skb from send queue */
999 if (err == 0) {
1000 skb_unlink(skb, &iucv->send_skb_q);
1001 kfree_skb(skb);
1002 }
1003
1004 /* this error should never happen since the
1005 * IUCV_IPRMDATA path flag is set... sever path */
1006 if (err == 0x15) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001007 pr_iucv->path_sever(iucv->path, NULL);
Jennifer Hunteac37312007-02-08 13:51:54 -08001008 skb_unlink(skb, &iucv->send_skb_q);
1009 err = -EPIPE;
1010 goto fail;
1011 }
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001012 } else
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001013 err = pr_iucv->message_send(iucv->path, &txmsg, 0, 0,
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001014 (void *) skb->data, skb->len);
1015 if (err) {
1016 if (err == 3) {
1017 user_id[8] = 0;
1018 memcpy(user_id, iucv->dst_user_id, 8);
1019 appl_id[8] = 0;
1020 memcpy(appl_id, iucv->dst_name, 8);
1021 pr_err("Application %s on z/VM guest %s"
1022 " exceeds message limit\n",
1023 appl_id, user_id);
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001024 err = -EAGAIN;
1025 } else
1026 err = -EPIPE;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001027 skb_unlink(skb, &iucv->send_skb_q);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001028 goto fail;
Jennifer Hunteac37312007-02-08 13:51:54 -08001029 }
1030
1031 release_sock(sk);
1032 return len;
1033
1034fail:
1035 kfree_skb(skb);
1036out:
1037 release_sock(sk);
1038 return err;
1039}
1040
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001041/* iucv_fragment_skb() - Fragment a single IUCV message into multiple skb's
1042 *
1043 * Locking: must be called with message_q.lock held
1044 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001045static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
1046{
1047 int dataleft, size, copied = 0;
1048 struct sk_buff *nskb;
1049
1050 dataleft = len;
1051 while (dataleft) {
1052 if (dataleft >= sk->sk_rcvbuf / 4)
1053 size = sk->sk_rcvbuf / 4;
1054 else
1055 size = dataleft;
1056
1057 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
1058 if (!nskb)
1059 return -ENOMEM;
1060
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001061 /* copy target class to control buffer of new skb */
1062 memcpy(CB_TRGCLS(nskb), CB_TRGCLS(skb), CB_TRGCLS_LEN);
1063
1064 /* copy data fragment */
Ursula Braunf0703c82007-10-08 02:03:31 -07001065 memcpy(nskb->data, skb->data + copied, size);
1066 copied += size;
1067 dataleft -= size;
1068
1069 skb_reset_transport_header(nskb);
1070 skb_reset_network_header(nskb);
1071 nskb->len = size;
1072
1073 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
1074 }
1075
1076 return 0;
1077}
1078
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001079/* iucv_process_message() - Receive a single outstanding IUCV message
1080 *
1081 * Locking: must be called with message_q.lock held
1082 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001083static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1084 struct iucv_path *path,
1085 struct iucv_message *msg)
1086{
1087 int rc;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001088 unsigned int len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001089
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001090 len = iucv_msg_length(msg);
1091
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001092 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1093 /* Note: the first 4 bytes are reserved for msg tag */
1094 memcpy(CB_TRGCLS(skb), &msg->class, CB_TRGCLS_LEN);
1095
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001096 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1097 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1098 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1099 skb->data = NULL;
1100 skb->len = 0;
1101 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001102 } else {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001103 rc = pr_iucv->message_receive(path, msg,
1104 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) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001119 pr_iucv->path_sever(path, NULL);
Ursula Braunf0703c82007-10-08 02:03:31 -07001120 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;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001336 err = pr_iucv->message_send(iucv->path, &txmsg, IUCV_IPRMDATA,
1337 0, (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) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001354 err = pr_iucv->path_quiesce(iucv->path, NULL);
Jennifer Hunteac37312007-02-08 13:51:54 -08001355 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) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001381 pr_iucv->path_sever(iucv_sk(sk)->path, NULL);
Jennifer Hunteac37312007-02-08 13:51:54 -08001382 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 Brueckner09488e2e2009-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 Brueckner09488e2e2009-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) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001523 err = pr_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)) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001530 err = pr_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) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001538 err = pr_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 Brueckner09488e2e2009-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;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001562 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001563 if (err) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001564 err = pr_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) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001598 pr_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)
Julia Lawalla56635a2010-05-26 05:56:48 +00001623 goto out_unlock;
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
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001727static int __init afiucv_iucv_init(void)
1728{
1729 int err;
1730
1731 err = pr_iucv->iucv_register(&af_iucv_handler, 0);
1732 if (err)
1733 goto out;
1734 /* establish dummy device */
1735 af_iucv_driver.bus = pr_iucv->bus;
1736 err = driver_register(&af_iucv_driver);
1737 if (err)
1738 goto out_iucv;
1739 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1740 if (!af_iucv_dev) {
1741 err = -ENOMEM;
1742 goto out_driver;
1743 }
1744 dev_set_name(af_iucv_dev, "af_iucv");
1745 af_iucv_dev->bus = pr_iucv->bus;
1746 af_iucv_dev->parent = pr_iucv->root;
1747 af_iucv_dev->release = (void (*)(struct device *))kfree;
1748 af_iucv_dev->driver = &af_iucv_driver;
1749 err = device_register(af_iucv_dev);
1750 if (err)
1751 goto out_driver;
1752 return 0;
1753
1754out_driver:
1755 driver_unregister(&af_iucv_driver);
1756out_iucv:
1757 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
1758out:
1759 return err;
1760}
1761
Heiko Carstensda99f052007-05-04 12:23:27 -07001762static int __init afiucv_init(void)
Jennifer Hunteac37312007-02-08 13:51:54 -08001763{
1764 int err;
1765
1766 if (!MACHINE_IS_VM) {
Ursula Braun8f7c5022008-12-25 13:39:47 +01001767 pr_err("The af_iucv module cannot be loaded"
1768 " without z/VM\n");
Jennifer Hunteac37312007-02-08 13:51:54 -08001769 err = -EPROTONOSUPPORT;
1770 goto out;
1771 }
1772 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
1773 if (unlikely(err)) {
Ursula Braunc2b4afd2008-07-14 09:59:29 +02001774 WARN_ON(err);
Jennifer Hunteac37312007-02-08 13:51:54 -08001775 err = -EPROTONOSUPPORT;
1776 goto out;
1777 }
1778
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001779 pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
1780 if (!pr_iucv) {
1781 printk(KERN_WARNING "iucv_if lookup failed\n");
1782 err = -EPROTONOSUPPORT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001783 goto out;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001784 }
1785
Jennifer Hunteac37312007-02-08 13:51:54 -08001786 err = proto_register(&iucv_proto, 0);
1787 if (err)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001788 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08001789 err = sock_register(&iucv_sock_family_ops);
1790 if (err)
1791 goto out_proto;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001792
1793 err = afiucv_iucv_init();
Ursula Braunc23cad92009-06-16 10:30:44 +02001794 if (err)
1795 goto out_sock;
Ursula Braunc23cad92009-06-16 10:30:44 +02001796
Jennifer Hunteac37312007-02-08 13:51:54 -08001797 return 0;
1798
Ursula Braunc23cad92009-06-16 10:30:44 +02001799out_sock:
1800 sock_unregister(PF_IUCV);
Jennifer Hunteac37312007-02-08 13:51:54 -08001801out_proto:
1802 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08001803out:
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001804 if (pr_iucv)
1805 symbol_put(iucv_if);
Jennifer Hunteac37312007-02-08 13:51:54 -08001806 return err;
1807}
1808
1809static void __exit afiucv_exit(void)
1810{
Ursula Braunc23cad92009-06-16 10:30:44 +02001811 device_unregister(af_iucv_dev);
1812 driver_unregister(&af_iucv_driver);
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001813 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
1814 symbol_put(iucv_if);
Jennifer Hunteac37312007-02-08 13:51:54 -08001815 sock_unregister(PF_IUCV);
1816 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08001817}
1818
1819module_init(afiucv_init);
1820module_exit(afiucv_exit);
1821
1822MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
1823MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
1824MODULE_VERSION(VERSION);
1825MODULE_LICENSE("GPL");
1826MODULE_ALIAS_NETPROTO(PF_IUCV);