blob: a9b3a6f9ea95f18f2ea8325a72741fb61e0c14cf [file] [log] [blame]
Jennifer Hunteac37312007-02-08 13:51:54 -08001/*
2 * linux/net/iucv/af_iucv.c
3 *
4 * IUCV protocol stack for Linux on zSeries
5 *
6 * Copyright 2006 IBM Corporation
7 *
8 * Author(s): Jennifer Hunt <jenhunt@us.ibm.com>
9 */
10
Ursula Braun8f7c5022008-12-25 13:39:47 +010011#define KMSG_COMPONENT "af_iucv"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Jennifer Hunteac37312007-02-08 13:51:54 -080014#include <linux/module.h>
15#include <linux/types.h>
16#include <linux/list.h>
17#include <linux/errno.h>
18#include <linux/kernel.h>
19#include <linux/sched.h>
20#include <linux/slab.h>
21#include <linux/skbuff.h>
22#include <linux/init.h>
23#include <linux/poll.h>
24#include <net/sock.h>
25#include <asm/ebcdic.h>
26#include <asm/cpcmd.h>
27#include <linux/kmod.h>
28
29#include <net/iucv/iucv.h>
30#include <net/iucv/af_iucv.h>
31
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +000032#define VERSION "1.1"
Jennifer Hunteac37312007-02-08 13:51:54 -080033
34static char iucv_userid[80];
35
36static struct proto_ops iucv_sock_ops;
37
38static struct proto iucv_proto = {
39 .name = "AF_IUCV",
40 .owner = THIS_MODULE,
41 .obj_size = sizeof(struct iucv_sock),
42};
43
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +000044/* special AF_IUCV IPRM messages */
45static const u8 iprm_shutdown[8] =
46 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
47
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000048#define TRGCLS_SIZE (sizeof(((struct iucv_message *)0)->class))
49
50/* macros to set/get socket control buffer at correct offset */
51#define CB_TAG(skb) ((skb)->cb) /* iucv message tag */
52#define CB_TAG_LEN (sizeof(((struct iucv_message *) 0)->tag))
53#define CB_TRGCLS(skb) ((skb)->cb + CB_TAG_LEN) /* iucv msg target class */
54#define CB_TRGCLS_LEN (TRGCLS_SIZE)
55
56
Heiko Carstens57f20442007-10-08 02:02:52 -070057static void iucv_sock_kill(struct sock *sk);
58static void iucv_sock_close(struct sock *sk);
59
Jennifer Hunteac37312007-02-08 13:51:54 -080060/* Call Back functions */
61static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
62static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
63static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
Heiko Carstensda99f052007-05-04 12:23:27 -070064static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8],
65 u8 ipuser[16]);
Jennifer Hunteac37312007-02-08 13:51:54 -080066static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
Hendrik Brueckneraf88b522009-04-21 23:26:21 +000067static void iucv_callback_shutdown(struct iucv_path *, u8 ipuser[16]);
Jennifer Hunteac37312007-02-08 13:51:54 -080068
69static struct iucv_sock_list iucv_sk_list = {
Robert P. J. Day3db8ce32008-04-10 02:11:24 -070070 .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
Jennifer Hunteac37312007-02-08 13:51:54 -080071 .autobind_name = ATOMIC_INIT(0)
72};
73
74static struct iucv_handler af_iucv_handler = {
75 .path_pending = iucv_callback_connreq,
76 .path_complete = iucv_callback_connack,
77 .path_severed = iucv_callback_connrej,
78 .message_pending = iucv_callback_rx,
Hendrik Brueckneraf88b522009-04-21 23:26:21 +000079 .message_complete = iucv_callback_txdone,
80 .path_quiesced = iucv_callback_shutdown,
Jennifer Hunteac37312007-02-08 13:51:54 -080081};
82
83static inline void high_nmcpy(unsigned char *dst, char *src)
84{
85 memcpy(dst, src, 8);
86}
87
88static inline void low_nmcpy(unsigned char *dst, char *src)
89{
90 memcpy(&dst[8], src, 8);
91}
92
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +000093/**
94 * iucv_msg_length() - Returns the length of an iucv message.
95 * @msg: Pointer to struct iucv_message, MUST NOT be NULL
96 *
97 * The function returns the length of the specified iucv message @msg of data
98 * stored in a buffer and of data stored in the parameter list (PRMDATA).
99 *
100 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
101 * data:
102 * PRMDATA[0..6] socket data (max 7 bytes);
103 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
104 *
105 * The socket data length is computed by substracting the socket data length
106 * value from 0xFF.
107 * If the socket data len is greater 7, then PRMDATA can be used for special
108 * notifications (see iucv_sock_shutdown); and further,
109 * if the socket data len is > 7, the function returns 8.
110 *
111 * Use this function to allocate socket buffers to store iucv message data.
112 */
113static inline size_t iucv_msg_length(struct iucv_message *msg)
114{
115 size_t datalen;
116
117 if (msg->flags & IUCV_IPRMDATA) {
118 datalen = 0xff - msg->rmmsg[7];
119 return (datalen < 8) ? datalen : 8;
120 }
121 return msg->length;
122}
123
Jennifer Hunteac37312007-02-08 13:51:54 -0800124/* Timers */
125static void iucv_sock_timeout(unsigned long arg)
126{
127 struct sock *sk = (struct sock *)arg;
128
129 bh_lock_sock(sk);
130 sk->sk_err = ETIMEDOUT;
131 sk->sk_state_change(sk);
132 bh_unlock_sock(sk);
133
134 iucv_sock_kill(sk);
135 sock_put(sk);
136}
137
138static void iucv_sock_clear_timer(struct sock *sk)
139{
140 sk_stop_timer(sk, &sk->sk_timer);
141}
142
Jennifer Hunteac37312007-02-08 13:51:54 -0800143static struct sock *__iucv_get_sock_by_name(char *nm)
144{
145 struct sock *sk;
146 struct hlist_node *node;
147
148 sk_for_each(sk, node, &iucv_sk_list.head)
149 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
150 return sk;
151
152 return NULL;
153}
154
155static void iucv_sock_destruct(struct sock *sk)
156{
157 skb_queue_purge(&sk->sk_receive_queue);
158 skb_queue_purge(&sk->sk_write_queue);
159}
160
161/* Cleanup Listen */
162static void iucv_sock_cleanup_listen(struct sock *parent)
163{
164 struct sock *sk;
165
166 /* Close non-accepted connections */
167 while ((sk = iucv_accept_dequeue(parent, NULL))) {
168 iucv_sock_close(sk);
169 iucv_sock_kill(sk);
170 }
171
172 parent->sk_state = IUCV_CLOSED;
173 sock_set_flag(parent, SOCK_ZAPPED);
174}
175
176/* Kill socket */
177static void iucv_sock_kill(struct sock *sk)
178{
179 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
180 return;
181
182 iucv_sock_unlink(&iucv_sk_list, sk);
183 sock_set_flag(sk, SOCK_DEAD);
184 sock_put(sk);
185}
186
187/* Close an IUCV socket */
188static void iucv_sock_close(struct sock *sk)
189{
190 unsigned char user_data[16];
191 struct iucv_sock *iucv = iucv_sk(sk);
192 int err;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700193 unsigned long timeo;
Jennifer Hunteac37312007-02-08 13:51:54 -0800194
195 iucv_sock_clear_timer(sk);
196 lock_sock(sk);
197
Heiko Carstensda99f052007-05-04 12:23:27 -0700198 switch (sk->sk_state) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800199 case IUCV_LISTEN:
200 iucv_sock_cleanup_listen(sk);
201 break;
202
203 case IUCV_CONNECTED:
204 case IUCV_DISCONN:
205 err = 0;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700206
207 sk->sk_state = IUCV_CLOSING;
208 sk->sk_state_change(sk);
209
Heiko Carstensda99f052007-05-04 12:23:27 -0700210 if (!skb_queue_empty(&iucv->send_skb_q)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -0700211 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
212 timeo = sk->sk_lingertime;
213 else
214 timeo = IUCV_DISCONN_TIMEOUT;
215 err = iucv_sock_wait_state(sk, IUCV_CLOSED, 0, timeo);
216 }
217
Ursula Braunbbe188c2009-04-21 06:04:20 +0000218 case IUCV_CLOSING: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700219 sk->sk_state = IUCV_CLOSED;
220 sk->sk_state_change(sk);
221
Jennifer Hunteac37312007-02-08 13:51:54 -0800222 if (iucv->path) {
223 low_nmcpy(user_data, iucv->src_name);
224 high_nmcpy(user_data, iucv->dst_name);
225 ASCEBC(user_data, sizeof(user_data));
226 err = iucv_path_sever(iucv->path, user_data);
227 iucv_path_free(iucv->path);
228 iucv->path = NULL;
229 }
230
Jennifer Hunteac37312007-02-08 13:51:54 -0800231 sk->sk_err = ECONNRESET;
232 sk->sk_state_change(sk);
233
234 skb_queue_purge(&iucv->send_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700235 skb_queue_purge(&iucv->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800236
237 sock_set_flag(sk, SOCK_ZAPPED);
238 break;
239
240 default:
241 sock_set_flag(sk, SOCK_ZAPPED);
242 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700243 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800244
245 release_sock(sk);
246 iucv_sock_kill(sk);
247}
248
249static void iucv_sock_init(struct sock *sk, struct sock *parent)
250{
251 if (parent)
252 sk->sk_type = parent->sk_type;
253}
254
255static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
256{
257 struct sock *sk;
258
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700259 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -0800260 if (!sk)
261 return NULL;
262
263 sock_init_data(sock, sk);
264 INIT_LIST_HEAD(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700265 spin_lock_init(&iucv_sk(sk)->accept_q_lock);
Jennifer Hunteac37312007-02-08 13:51:54 -0800266 skb_queue_head_init(&iucv_sk(sk)->send_skb_q);
Ursula Braunf0703c82007-10-08 02:03:31 -0700267 INIT_LIST_HEAD(&iucv_sk(sk)->message_q.list);
268 spin_lock_init(&iucv_sk(sk)->message_q.lock);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700269 skb_queue_head_init(&iucv_sk(sk)->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800270 iucv_sk(sk)->send_tag = 0;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +0000271 iucv_sk(sk)->flags = 0;
Hendrik Brueckner09488e22009-04-21 23:26:27 +0000272 iucv_sk(sk)->msglimit = IUCV_QUEUELEN_DEFAULT;
Ursula Braunbbe188c2009-04-21 06:04:20 +0000273 iucv_sk(sk)->path = NULL;
274 memset(&iucv_sk(sk)->src_user_id , 0, 32);
Jennifer Hunteac37312007-02-08 13:51:54 -0800275
276 sk->sk_destruct = iucv_sock_destruct;
277 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
278 sk->sk_allocation = GFP_DMA;
279
280 sock_reset_flag(sk, SOCK_ZAPPED);
281
282 sk->sk_protocol = proto;
283 sk->sk_state = IUCV_OPEN;
284
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800285 setup_timer(&sk->sk_timer, iucv_sock_timeout, (unsigned long)sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800286
287 iucv_sock_link(&iucv_sk_list, sk);
288 return sk;
289}
290
291/* Create an IUCV socket */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700292static int iucv_sock_create(struct net *net, struct socket *sock, int protocol)
Jennifer Hunteac37312007-02-08 13:51:54 -0800293{
294 struct sock *sk;
295
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000296 if (protocol && protocol != PF_IUCV)
297 return -EPROTONOSUPPORT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800298
299 sock->state = SS_UNCONNECTED;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000300
301 switch (sock->type) {
302 case SOCK_STREAM:
303 sock->ops = &iucv_sock_ops;
304 break;
305 case SOCK_SEQPACKET:
306 /* currently, proto ops can handle both sk types */
307 sock->ops = &iucv_sock_ops;
308 break;
309 default:
310 return -ESOCKTNOSUPPORT;
311 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800312
313 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
314 if (!sk)
315 return -ENOMEM;
316
317 iucv_sock_init(sk, NULL);
318
319 return 0;
320}
321
322void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
323{
324 write_lock_bh(&l->lock);
325 sk_add_node(sk, &l->head);
326 write_unlock_bh(&l->lock);
327}
328
329void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
330{
331 write_lock_bh(&l->lock);
332 sk_del_node_init(sk);
333 write_unlock_bh(&l->lock);
334}
335
336void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
337{
Ursula Braunfebca282007-07-14 19:04:25 -0700338 unsigned long flags;
339 struct iucv_sock *par = iucv_sk(parent);
340
Jennifer Hunteac37312007-02-08 13:51:54 -0800341 sock_hold(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700342 spin_lock_irqsave(&par->accept_q_lock, flags);
343 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
344 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800345 iucv_sk(sk)->parent = parent;
346 parent->sk_ack_backlog++;
347}
348
349void iucv_accept_unlink(struct sock *sk)
350{
Ursula Braunfebca282007-07-14 19:04:25 -0700351 unsigned long flags;
352 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
353
354 spin_lock_irqsave(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800355 list_del_init(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700356 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800357 iucv_sk(sk)->parent->sk_ack_backlog--;
358 iucv_sk(sk)->parent = NULL;
359 sock_put(sk);
360}
361
362struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
363{
364 struct iucv_sock *isk, *n;
365 struct sock *sk;
366
Heiko Carstensda99f052007-05-04 12:23:27 -0700367 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800368 sk = (struct sock *) isk;
369 lock_sock(sk);
370
371 if (sk->sk_state == IUCV_CLOSED) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800372 iucv_accept_unlink(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700373 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800374 continue;
375 }
376
377 if (sk->sk_state == IUCV_CONNECTED ||
378 sk->sk_state == IUCV_SEVERED ||
379 !newsock) {
380 iucv_accept_unlink(sk);
381 if (newsock)
382 sock_graft(sk, newsock);
383
384 if (sk->sk_state == IUCV_SEVERED)
385 sk->sk_state = IUCV_DISCONN;
386
387 release_sock(sk);
388 return sk;
389 }
390
391 release_sock(sk);
392 }
393 return NULL;
394}
395
396int iucv_sock_wait_state(struct sock *sk, int state, int state2,
397 unsigned long timeo)
398{
399 DECLARE_WAITQUEUE(wait, current);
400 int err = 0;
401
402 add_wait_queue(sk->sk_sleep, &wait);
403 while (sk->sk_state != state && sk->sk_state != state2) {
404 set_current_state(TASK_INTERRUPTIBLE);
405
406 if (!timeo) {
407 err = -EAGAIN;
408 break;
409 }
410
411 if (signal_pending(current)) {
412 err = sock_intr_errno(timeo);
413 break;
414 }
415
416 release_sock(sk);
417 timeo = schedule_timeout(timeo);
418 lock_sock(sk);
419
420 err = sock_error(sk);
421 if (err)
422 break;
423 }
424 set_current_state(TASK_RUNNING);
425 remove_wait_queue(sk->sk_sleep, &wait);
426 return err;
427}
428
429/* Bind an unbound socket */
430static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
431 int addr_len)
432{
433 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
434 struct sock *sk = sock->sk;
435 struct iucv_sock *iucv;
436 int err;
437
438 /* Verify the input sockaddr */
439 if (!addr || addr->sa_family != AF_IUCV)
440 return -EINVAL;
441
442 lock_sock(sk);
443 if (sk->sk_state != IUCV_OPEN) {
444 err = -EBADFD;
445 goto done;
446 }
447
448 write_lock_bh(&iucv_sk_list.lock);
449
450 iucv = iucv_sk(sk);
451 if (__iucv_get_sock_by_name(sa->siucv_name)) {
452 err = -EADDRINUSE;
453 goto done_unlock;
454 }
455 if (iucv->path) {
456 err = 0;
457 goto done_unlock;
458 }
459
460 /* Bind the socket */
461 memcpy(iucv->src_name, sa->siucv_name, 8);
462
463 /* Copy the user id */
464 memcpy(iucv->src_user_id, iucv_userid, 8);
465 sk->sk_state = IUCV_BOUND;
466 err = 0;
467
468done_unlock:
469 /* Release the socket list lock */
470 write_unlock_bh(&iucv_sk_list.lock);
471done:
472 release_sock(sk);
473 return err;
474}
475
476/* Automatically bind an unbound socket */
477static int iucv_sock_autobind(struct sock *sk)
478{
479 struct iucv_sock *iucv = iucv_sk(sk);
480 char query_buffer[80];
481 char name[12];
482 int err = 0;
483
484 /* Set the userid and name */
485 cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err);
486 if (unlikely(err))
487 return -EPROTO;
488
489 memcpy(iucv->src_user_id, query_buffer, 8);
490
491 write_lock_bh(&iucv_sk_list.lock);
492
493 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
494 while (__iucv_get_sock_by_name(name)) {
495 sprintf(name, "%08x",
496 atomic_inc_return(&iucv_sk_list.autobind_name));
497 }
498
499 write_unlock_bh(&iucv_sk_list.lock);
500
501 memcpy(&iucv->src_name, name, 8);
502
503 return err;
504}
505
506/* Connect an unconnected socket */
507static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
508 int alen, int flags)
509{
510 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
511 struct sock *sk = sock->sk;
512 struct iucv_sock *iucv;
513 unsigned char user_data[16];
514 int err;
515
516 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
517 return -EINVAL;
518
519 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
520 return -EBADFD;
521
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000522 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800523 return -EINVAL;
524
Jennifer Hunteac37312007-02-08 13:51:54 -0800525 if (sk->sk_state == IUCV_OPEN) {
526 err = iucv_sock_autobind(sk);
527 if (unlikely(err))
528 return err;
529 }
530
531 lock_sock(sk);
532
533 /* Set the destination information */
534 memcpy(iucv_sk(sk)->dst_user_id, sa->siucv_user_id, 8);
535 memcpy(iucv_sk(sk)->dst_name, sa->siucv_name, 8);
536
537 high_nmcpy(user_data, sa->siucv_name);
538 low_nmcpy(user_data, iucv_sk(sk)->src_name);
539 ASCEBC(user_data, sizeof(user_data));
540
541 iucv = iucv_sk(sk);
542 /* Create path. */
Hendrik Brueckner09488e22009-04-21 23:26:27 +0000543 iucv->path = iucv_path_alloc(iucv->msglimit,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000544 IUCV_IPRMDATA, GFP_KERNEL);
Ursula Braund4444722008-02-07 18:07:19 -0800545 if (!iucv->path) {
546 err = -ENOMEM;
547 goto done;
548 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800549 err = iucv_path_connect(iucv->path, &af_iucv_handler,
550 sa->siucv_user_id, NULL, user_data, sk);
551 if (err) {
552 iucv_path_free(iucv->path);
553 iucv->path = NULL;
Hendrik Brueckner55cdea92009-01-05 18:07:07 -0800554 switch (err) {
555 case 0x0b: /* Target communicator is not logged on */
556 err = -ENETUNREACH;
557 break;
558 case 0x0d: /* Max connections for this guest exceeded */
559 case 0x0e: /* Max connections for target guest exceeded */
560 err = -EAGAIN;
561 break;
562 case 0x0f: /* Missing IUCV authorization */
563 err = -EACCES;
564 break;
565 default:
566 err = -ECONNREFUSED;
567 break;
568 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800569 goto done;
570 }
571
572 if (sk->sk_state != IUCV_CONNECTED) {
573 err = iucv_sock_wait_state(sk, IUCV_CONNECTED, IUCV_DISCONN,
574 sock_sndtimeo(sk, flags & O_NONBLOCK));
575 }
576
577 if (sk->sk_state == IUCV_DISCONN) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000578 err = -ECONNREFUSED;
Jennifer Hunteac37312007-02-08 13:51:54 -0800579 }
Ursula Braun18becbc2009-01-05 18:07:46 -0800580
581 if (err) {
582 iucv_path_sever(iucv->path, NULL);
583 iucv_path_free(iucv->path);
584 iucv->path = NULL;
585 }
586
Jennifer Hunteac37312007-02-08 13:51:54 -0800587done:
588 release_sock(sk);
589 return err;
590}
591
592/* Move a socket into listening state. */
593static int iucv_sock_listen(struct socket *sock, int backlog)
594{
595 struct sock *sk = sock->sk;
596 int err;
597
598 lock_sock(sk);
599
600 err = -EINVAL;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000601 if (sk->sk_state != IUCV_BOUND)
602 goto done;
603
604 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800605 goto done;
606
607 sk->sk_max_ack_backlog = backlog;
608 sk->sk_ack_backlog = 0;
609 sk->sk_state = IUCV_LISTEN;
610 err = 0;
611
612done:
613 release_sock(sk);
614 return err;
615}
616
617/* Accept a pending connection */
618static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
619 int flags)
620{
621 DECLARE_WAITQUEUE(wait, current);
622 struct sock *sk = sock->sk, *nsk;
623 long timeo;
624 int err = 0;
625
Jennifer Hunt561e0362007-05-04 12:22:07 -0700626 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800627
628 if (sk->sk_state != IUCV_LISTEN) {
629 err = -EBADFD;
630 goto done;
631 }
632
633 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
634
635 /* Wait for an incoming connection */
636 add_wait_queue_exclusive(sk->sk_sleep, &wait);
Heiko Carstensda99f052007-05-04 12:23:27 -0700637 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800638 set_current_state(TASK_INTERRUPTIBLE);
639 if (!timeo) {
640 err = -EAGAIN;
641 break;
642 }
643
644 release_sock(sk);
645 timeo = schedule_timeout(timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700646 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800647
648 if (sk->sk_state != IUCV_LISTEN) {
649 err = -EBADFD;
650 break;
651 }
652
653 if (signal_pending(current)) {
654 err = sock_intr_errno(timeo);
655 break;
656 }
657 }
658
659 set_current_state(TASK_RUNNING);
660 remove_wait_queue(sk->sk_sleep, &wait);
661
662 if (err)
663 goto done;
664
665 newsock->state = SS_CONNECTED;
666
667done:
668 release_sock(sk);
669 return err;
670}
671
672static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
673 int *len, int peer)
674{
675 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
676 struct sock *sk = sock->sk;
677
678 addr->sa_family = AF_IUCV;
679 *len = sizeof(struct sockaddr_iucv);
680
681 if (peer) {
682 memcpy(siucv->siucv_user_id, iucv_sk(sk)->dst_user_id, 8);
683 memcpy(siucv->siucv_name, &iucv_sk(sk)->dst_name, 8);
684 } else {
685 memcpy(siucv->siucv_user_id, iucv_sk(sk)->src_user_id, 8);
686 memcpy(siucv->siucv_name, iucv_sk(sk)->src_name, 8);
687 }
688 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
689 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
690 memset(siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
691
692 return 0;
693}
694
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000695/**
696 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
697 * @path: IUCV path
698 * @msg: Pointer to a struct iucv_message
699 * @skb: The socket data to send, skb->len MUST BE <= 7
700 *
701 * Send the socket data in the parameter list in the iucv message
702 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
703 * list and the socket data len at index 7 (last byte).
704 * See also iucv_msg_length().
705 *
706 * Returns the error code from the iucv_message_send() call.
707 */
708static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
709 struct sk_buff *skb)
710{
711 u8 prmdata[8];
712
713 memcpy(prmdata, (void *) skb->data, skb->len);
714 prmdata[7] = 0xff - (u8) skb->len;
715 return iucv_message_send(path, msg, IUCV_IPRMDATA, 0,
716 (void *) prmdata, 8);
717}
718
Jennifer Hunteac37312007-02-08 13:51:54 -0800719static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
720 struct msghdr *msg, size_t len)
721{
722 struct sock *sk = sock->sk;
723 struct iucv_sock *iucv = iucv_sk(sk);
724 struct sk_buff *skb;
725 struct iucv_message txmsg;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000726 struct cmsghdr *cmsg;
727 int cmsg_done;
Ursula Braun8f7c5022008-12-25 13:39:47 +0100728 char user_id[9];
729 char appl_id[9];
Jennifer Hunteac37312007-02-08 13:51:54 -0800730 int err;
731
732 err = sock_error(sk);
733 if (err)
734 return err;
735
736 if (msg->msg_flags & MSG_OOB)
737 return -EOPNOTSUPP;
738
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000739 /* SOCK_SEQPACKET: we do not support segmented records */
740 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
741 return -EOPNOTSUPP;
742
Jennifer Hunteac37312007-02-08 13:51:54 -0800743 lock_sock(sk);
744
745 if (sk->sk_shutdown & SEND_SHUTDOWN) {
746 err = -EPIPE;
747 goto out;
748 }
749
Heiko Carstensda99f052007-05-04 12:23:27 -0700750 if (sk->sk_state == IUCV_CONNECTED) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000751 /* initialize defaults */
752 cmsg_done = 0; /* check for duplicate headers */
753 txmsg.class = 0;
754
755 /* iterate over control messages */
756 for (cmsg = CMSG_FIRSTHDR(msg); cmsg;
757 cmsg = CMSG_NXTHDR(msg, cmsg)) {
758
759 if (!CMSG_OK(msg, cmsg)) {
760 err = -EINVAL;
761 goto out;
762 }
763
764 if (cmsg->cmsg_level != SOL_IUCV)
765 continue;
766
767 if (cmsg->cmsg_type & cmsg_done) {
768 err = -EINVAL;
769 goto out;
770 }
771 cmsg_done |= cmsg->cmsg_type;
772
773 switch (cmsg->cmsg_type) {
774 case SCM_IUCV_TRGCLS:
775 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
776 err = -EINVAL;
777 goto out;
778 }
779
780 /* set iucv message target class */
781 memcpy(&txmsg.class,
782 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
783
784 break;
785
786 default:
787 err = -EINVAL;
788 goto out;
789 break;
790 }
791 }
792
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000793 /* allocate one skb for each iucv message:
794 * this is fine for SOCK_SEQPACKET (unless we want to support
795 * segmented records using the MSG_EOR flag), but
796 * for SOCK_STREAM we might want to improve it in future */
Heiko Carstensda99f052007-05-04 12:23:27 -0700797 if (!(skb = sock_alloc_send_skb(sk, len,
798 msg->msg_flags & MSG_DONTWAIT,
799 &err)))
Jennifer Hunt561e0362007-05-04 12:22:07 -0700800 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -0800801
Heiko Carstensda99f052007-05-04 12:23:27 -0700802 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800803 err = -EFAULT;
804 goto fail;
805 }
806
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000807 /* increment and save iucv message tag for msg_completion cbk */
Jennifer Hunteac37312007-02-08 13:51:54 -0800808 txmsg.tag = iucv->send_tag++;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000809 memcpy(CB_TAG(skb), &txmsg.tag, CB_TAG_LEN);
Jennifer Hunteac37312007-02-08 13:51:54 -0800810 skb_queue_tail(&iucv->send_skb_q, skb);
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000811
812 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags)
813 && skb->len <= 7) {
814 err = iucv_send_iprm(iucv->path, &txmsg, skb);
815
816 /* on success: there is no message_complete callback
817 * for an IPRMDATA msg; remove skb from send queue */
818 if (err == 0) {
819 skb_unlink(skb, &iucv->send_skb_q);
820 kfree_skb(skb);
821 }
822
823 /* this error should never happen since the
824 * IUCV_IPRMDATA path flag is set... sever path */
825 if (err == 0x15) {
826 iucv_path_sever(iucv->path, NULL);
827 skb_unlink(skb, &iucv->send_skb_q);
828 err = -EPIPE;
829 goto fail;
830 }
831 } else
832 err = iucv_message_send(iucv->path, &txmsg, 0, 0,
833 (void *) skb->data, skb->len);
Jennifer Hunteac37312007-02-08 13:51:54 -0800834 if (err) {
Ursula Braun8f7c5022008-12-25 13:39:47 +0100835 if (err == 3) {
836 user_id[8] = 0;
837 memcpy(user_id, iucv->dst_user_id, 8);
838 appl_id[8] = 0;
839 memcpy(appl_id, iucv->dst_name, 8);
840 pr_err("Application %s on z/VM guest %s"
841 " exceeds message limit\n",
842 user_id, appl_id);
843 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800844 skb_unlink(skb, &iucv->send_skb_q);
845 err = -EPIPE;
846 goto fail;
847 }
848
849 } else {
850 err = -ENOTCONN;
851 goto out;
852 }
853
854 release_sock(sk);
855 return len;
856
857fail:
858 kfree_skb(skb);
859out:
860 release_sock(sk);
861 return err;
862}
863
Ursula Braunf0703c82007-10-08 02:03:31 -0700864static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
865{
866 int dataleft, size, copied = 0;
867 struct sk_buff *nskb;
868
869 dataleft = len;
870 while (dataleft) {
871 if (dataleft >= sk->sk_rcvbuf / 4)
872 size = sk->sk_rcvbuf / 4;
873 else
874 size = dataleft;
875
876 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
877 if (!nskb)
878 return -ENOMEM;
879
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000880 /* copy target class to control buffer of new skb */
881 memcpy(CB_TRGCLS(nskb), CB_TRGCLS(skb), CB_TRGCLS_LEN);
882
883 /* copy data fragment */
Ursula Braunf0703c82007-10-08 02:03:31 -0700884 memcpy(nskb->data, skb->data + copied, size);
885 copied += size;
886 dataleft -= size;
887
888 skb_reset_transport_header(nskb);
889 skb_reset_network_header(nskb);
890 nskb->len = size;
891
892 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
893 }
894
895 return 0;
896}
897
898static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
899 struct iucv_path *path,
900 struct iucv_message *msg)
901{
902 int rc;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000903 unsigned int len;
Ursula Braunf0703c82007-10-08 02:03:31 -0700904
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000905 len = iucv_msg_length(msg);
906
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000907 /* store msg target class in the second 4 bytes of skb ctrl buffer */
908 /* Note: the first 4 bytes are reserved for msg tag */
909 memcpy(CB_TRGCLS(skb), &msg->class, CB_TRGCLS_LEN);
910
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000911 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
912 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
913 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
914 skb->data = NULL;
915 skb->len = 0;
916 }
Ursula Braunf0703c82007-10-08 02:03:31 -0700917 } else {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000918 rc = iucv_message_receive(path, msg, msg->flags & IUCV_IPRMDATA,
919 skb->data, len, NULL);
Ursula Braunf0703c82007-10-08 02:03:31 -0700920 if (rc) {
921 kfree_skb(skb);
922 return;
923 }
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000924 /* we need to fragment iucv messages for SOCK_STREAM only;
925 * for SOCK_SEQPACKET, it is only relevant if we support
926 * record segmentation using MSG_EOR (see also recvmsg()) */
927 if (sk->sk_type == SOCK_STREAM &&
928 skb->truesize >= sk->sk_rcvbuf / 4) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000929 rc = iucv_fragment_skb(sk, skb, len);
Ursula Braunf0703c82007-10-08 02:03:31 -0700930 kfree_skb(skb);
931 skb = NULL;
932 if (rc) {
933 iucv_path_sever(path, NULL);
934 return;
935 }
936 skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
937 } else {
938 skb_reset_transport_header(skb);
939 skb_reset_network_header(skb);
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000940 skb->len = len;
Ursula Braunf0703c82007-10-08 02:03:31 -0700941 }
942 }
943
944 if (sock_queue_rcv_skb(sk, skb))
945 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
946}
947
948static void iucv_process_message_q(struct sock *sk)
949{
950 struct iucv_sock *iucv = iucv_sk(sk);
951 struct sk_buff *skb;
952 struct sock_msg_q *p, *n;
953
954 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000955 skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -0700956 if (!skb)
957 break;
958 iucv_process_message(sk, skb, p->path, &p->msg);
959 list_del(&p->list);
960 kfree(p);
961 if (!skb_queue_empty(&iucv->backlog_skb_q))
962 break;
963 }
964}
965
Jennifer Hunteac37312007-02-08 13:51:54 -0800966static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
967 struct msghdr *msg, size_t len, int flags)
968{
969 int noblock = flags & MSG_DONTWAIT;
970 struct sock *sk = sock->sk;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700971 struct iucv_sock *iucv = iucv_sk(sk);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000972 unsigned int copied, rlen;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700973 struct sk_buff *skb, *rskb, *cskb;
Jennifer Hunteac37312007-02-08 13:51:54 -0800974 int err = 0;
975
Jennifer Hunt561e0362007-05-04 12:22:07 -0700976 if ((sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED) &&
Ursula Braunf0703c82007-10-08 02:03:31 -0700977 skb_queue_empty(&iucv->backlog_skb_q) &&
978 skb_queue_empty(&sk->sk_receive_queue) &&
979 list_empty(&iucv->message_q.list))
Jennifer Hunt561e0362007-05-04 12:22:07 -0700980 return 0;
981
Jennifer Hunteac37312007-02-08 13:51:54 -0800982 if (flags & (MSG_OOB))
983 return -EOPNOTSUPP;
984
Hendrik Brueckner60d37052009-04-21 06:04:21 +0000985 /* receive/dequeue next skb:
986 * the function understands MSG_PEEK and, thus, does not dequeue skb */
Jennifer Hunteac37312007-02-08 13:51:54 -0800987 skb = skb_recv_datagram(sk, flags, noblock, &err);
988 if (!skb) {
989 if (sk->sk_shutdown & RCV_SHUTDOWN)
990 return 0;
991 return err;
992 }
993
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000994 rlen = skb->len; /* real length of skb */
995 copied = min_t(unsigned int, rlen, len);
Jennifer Hunteac37312007-02-08 13:51:54 -0800996
Jennifer Hunt561e0362007-05-04 12:22:07 -0700997 cskb = skb;
998 if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) {
Hendrik Brueckner802788b2009-04-21 23:26:26 +0000999 if (!(flags & MSG_PEEK))
1000 skb_queue_head(&sk->sk_receive_queue, skb);
1001 return -EFAULT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001002 }
1003
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001004 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1005 if (sk->sk_type == SOCK_SEQPACKET) {
1006 if (copied < rlen)
1007 msg->msg_flags |= MSG_TRUNC;
1008 /* each iucv message contains a complete record */
1009 msg->msg_flags |= MSG_EOR;
1010 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001011
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001012 /* create control message to store iucv msg target class:
1013 * get the trgcls from the control buffer of the skb due to
1014 * fragmentation of original iucv message. */
1015 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1016 CB_TRGCLS_LEN, CB_TRGCLS(skb));
1017 if (err) {
1018 if (!(flags & MSG_PEEK))
1019 skb_queue_head(&sk->sk_receive_queue, skb);
1020 return err;
1021 }
1022
Jennifer Hunteac37312007-02-08 13:51:54 -08001023 /* Mark read part of skb as used */
1024 if (!(flags & MSG_PEEK)) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001025
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001026 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1027 if (sk->sk_type == SOCK_STREAM) {
1028 skb_pull(skb, copied);
1029 if (skb->len) {
1030 skb_queue_head(&sk->sk_receive_queue, skb);
1031 goto done;
1032 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001033 }
1034
1035 kfree_skb(skb);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001036
1037 /* Queue backlog skbs */
Ursula Braunf0703c82007-10-08 02:03:31 -07001038 rskb = skb_dequeue(&iucv->backlog_skb_q);
Heiko Carstensda99f052007-05-04 12:23:27 -07001039 while (rskb) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001040 if (sock_queue_rcv_skb(sk, rskb)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001041 skb_queue_head(&iucv->backlog_skb_q,
Jennifer Hunt561e0362007-05-04 12:22:07 -07001042 rskb);
1043 break;
1044 } else {
Ursula Braunf0703c82007-10-08 02:03:31 -07001045 rskb = skb_dequeue(&iucv->backlog_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001046 }
1047 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001048 if (skb_queue_empty(&iucv->backlog_skb_q)) {
1049 spin_lock_bh(&iucv->message_q.lock);
1050 if (!list_empty(&iucv->message_q.list))
1051 iucv_process_message_q(sk);
1052 spin_unlock_bh(&iucv->message_q.lock);
1053 }
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001054 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001055
1056done:
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001057 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1058 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1059 copied = rlen;
1060
1061 return copied;
Jennifer Hunteac37312007-02-08 13:51:54 -08001062}
1063
1064static inline unsigned int iucv_accept_poll(struct sock *parent)
1065{
1066 struct iucv_sock *isk, *n;
1067 struct sock *sk;
1068
Heiko Carstensda99f052007-05-04 12:23:27 -07001069 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001070 sk = (struct sock *) isk;
1071
1072 if (sk->sk_state == IUCV_CONNECTED)
1073 return POLLIN | POLLRDNORM;
1074 }
1075
1076 return 0;
1077}
1078
1079unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
1080 poll_table *wait)
1081{
1082 struct sock *sk = sock->sk;
1083 unsigned int mask = 0;
1084
1085 poll_wait(file, sk->sk_sleep, wait);
1086
1087 if (sk->sk_state == IUCV_LISTEN)
1088 return iucv_accept_poll(sk);
1089
1090 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1091 mask |= POLLERR;
1092
1093 if (sk->sk_shutdown & RCV_SHUTDOWN)
1094 mask |= POLLRDHUP;
1095
1096 if (sk->sk_shutdown == SHUTDOWN_MASK)
1097 mask |= POLLHUP;
1098
1099 if (!skb_queue_empty(&sk->sk_receive_queue) ||
Heiko Carstensda99f052007-05-04 12:23:27 -07001100 (sk->sk_shutdown & RCV_SHUTDOWN))
Jennifer Hunteac37312007-02-08 13:51:54 -08001101 mask |= POLLIN | POLLRDNORM;
1102
1103 if (sk->sk_state == IUCV_CLOSED)
1104 mask |= POLLHUP;
1105
Jennifer Hunt561e0362007-05-04 12:22:07 -07001106 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED)
1107 mask |= POLLIN;
1108
Jennifer Hunteac37312007-02-08 13:51:54 -08001109 if (sock_writeable(sk))
1110 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1111 else
1112 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1113
1114 return mask;
1115}
1116
1117static int iucv_sock_shutdown(struct socket *sock, int how)
1118{
1119 struct sock *sk = sock->sk;
1120 struct iucv_sock *iucv = iucv_sk(sk);
1121 struct iucv_message txmsg;
1122 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -08001123
1124 how++;
1125
1126 if ((how & ~SHUTDOWN_MASK) || !how)
1127 return -EINVAL;
1128
1129 lock_sock(sk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001130 switch (sk->sk_state) {
Hendrik Bruecknere14ad5f2009-04-21 06:04:23 +00001131 case IUCV_DISCONN:
1132 case IUCV_CLOSING:
1133 case IUCV_SEVERED:
Jennifer Hunteac37312007-02-08 13:51:54 -08001134 case IUCV_CLOSED:
1135 err = -ENOTCONN;
1136 goto fail;
1137
1138 default:
1139 sk->sk_shutdown |= how;
1140 break;
1141 }
1142
1143 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
1144 txmsg.class = 0;
1145 txmsg.tag = 0;
1146 err = iucv_message_send(iucv->path, &txmsg, IUCV_IPRMDATA, 0,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001147 (void *) iprm_shutdown, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -08001148 if (err) {
Heiko Carstensda99f052007-05-04 12:23:27 -07001149 switch (err) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001150 case 1:
1151 err = -ENOTCONN;
1152 break;
1153 case 2:
1154 err = -ECONNRESET;
1155 break;
1156 default:
1157 err = -ENOTCONN;
1158 break;
1159 }
1160 }
1161 }
1162
1163 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
1164 err = iucv_path_quiesce(iucv_sk(sk)->path, NULL);
1165 if (err)
1166 err = -ENOTCONN;
1167
1168 skb_queue_purge(&sk->sk_receive_queue);
1169 }
1170
1171 /* Wake up anyone sleeping in poll */
1172 sk->sk_state_change(sk);
1173
1174fail:
1175 release_sock(sk);
1176 return err;
1177}
1178
1179static int iucv_sock_release(struct socket *sock)
1180{
1181 struct sock *sk = sock->sk;
1182 int err = 0;
1183
1184 if (!sk)
1185 return 0;
1186
1187 iucv_sock_close(sk);
1188
1189 /* Unregister with IUCV base support */
1190 if (iucv_sk(sk)->path) {
1191 iucv_path_sever(iucv_sk(sk)->path, NULL);
1192 iucv_path_free(iucv_sk(sk)->path);
1193 iucv_sk(sk)->path = NULL;
1194 }
1195
Jennifer Hunteac37312007-02-08 13:51:54 -08001196 sock_orphan(sk);
1197 iucv_sock_kill(sk);
1198 return err;
1199}
1200
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001201/* getsockopt and setsockopt */
1202static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
1203 char __user *optval, int optlen)
1204{
1205 struct sock *sk = sock->sk;
1206 struct iucv_sock *iucv = iucv_sk(sk);
1207 int val;
1208 int rc;
1209
1210 if (level != SOL_IUCV)
1211 return -ENOPROTOOPT;
1212
1213 if (optlen < sizeof(int))
1214 return -EINVAL;
1215
1216 if (get_user(val, (int __user *) optval))
1217 return -EFAULT;
1218
1219 rc = 0;
1220
1221 lock_sock(sk);
1222 switch (optname) {
1223 case SO_IPRMDATA_MSG:
1224 if (val)
1225 iucv->flags |= IUCV_IPRMDATA;
1226 else
1227 iucv->flags &= ~IUCV_IPRMDATA;
1228 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001229 case SO_MSGLIMIT:
1230 switch (sk->sk_state) {
1231 case IUCV_OPEN:
1232 case IUCV_BOUND:
1233 if (val < 1 || val > (u16)(~0))
1234 rc = -EINVAL;
1235 else
1236 iucv->msglimit = val;
1237 break;
1238 default:
1239 rc = -EINVAL;
1240 break;
1241 }
1242 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001243 default:
1244 rc = -ENOPROTOOPT;
1245 break;
1246 }
1247 release_sock(sk);
1248
1249 return rc;
1250}
1251
1252static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1253 char __user *optval, int __user *optlen)
1254{
1255 struct sock *sk = sock->sk;
1256 struct iucv_sock *iucv = iucv_sk(sk);
1257 int val, len;
1258
1259 if (level != SOL_IUCV)
1260 return -ENOPROTOOPT;
1261
1262 if (get_user(len, optlen))
1263 return -EFAULT;
1264
1265 if (len < 0)
1266 return -EINVAL;
1267
1268 len = min_t(unsigned int, len, sizeof(int));
1269
1270 switch (optname) {
1271 case SO_IPRMDATA_MSG:
1272 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1273 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001274 case SO_MSGLIMIT:
1275 lock_sock(sk);
1276 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1277 : iucv->msglimit; /* default */
1278 release_sock(sk);
1279 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001280 default:
1281 return -ENOPROTOOPT;
1282 }
1283
1284 if (put_user(len, optlen))
1285 return -EFAULT;
1286 if (copy_to_user(optval, &val, len))
1287 return -EFAULT;
1288
1289 return 0;
1290}
1291
1292
Jennifer Hunteac37312007-02-08 13:51:54 -08001293/* Callback wrappers - called from iucv base support */
1294static int iucv_callback_connreq(struct iucv_path *path,
1295 u8 ipvmid[8], u8 ipuser[16])
1296{
1297 unsigned char user_data[16];
1298 unsigned char nuser_data[16];
1299 unsigned char src_name[8];
1300 struct hlist_node *node;
1301 struct sock *sk, *nsk;
1302 struct iucv_sock *iucv, *niucv;
1303 int err;
1304
1305 memcpy(src_name, ipuser, 8);
1306 EBCASC(src_name, 8);
1307 /* Find out if this path belongs to af_iucv. */
1308 read_lock(&iucv_sk_list.lock);
1309 iucv = NULL;
Ursula Braunfebca282007-07-14 19:04:25 -07001310 sk = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001311 sk_for_each(sk, node, &iucv_sk_list.head)
1312 if (sk->sk_state == IUCV_LISTEN &&
1313 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1314 /*
1315 * Found a listening socket with
1316 * src_name == ipuser[0-7].
1317 */
1318 iucv = iucv_sk(sk);
1319 break;
1320 }
1321 read_unlock(&iucv_sk_list.lock);
1322 if (!iucv)
1323 /* No socket found, not one of our paths. */
1324 return -EINVAL;
1325
1326 bh_lock_sock(sk);
1327
1328 /* Check if parent socket is listening */
1329 low_nmcpy(user_data, iucv->src_name);
1330 high_nmcpy(user_data, iucv->dst_name);
1331 ASCEBC(user_data, sizeof(user_data));
1332 if (sk->sk_state != IUCV_LISTEN) {
1333 err = iucv_path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001334 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001335 goto fail;
1336 }
1337
1338 /* Check for backlog size */
1339 if (sk_acceptq_is_full(sk)) {
1340 err = iucv_path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001341 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001342 goto fail;
1343 }
1344
1345 /* Create the new socket */
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001346 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
Heiko Carstensda99f052007-05-04 12:23:27 -07001347 if (!nsk) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001348 err = iucv_path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001349 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001350 goto fail;
1351 }
1352
1353 niucv = iucv_sk(nsk);
1354 iucv_sock_init(nsk, sk);
1355
1356 /* Set the new iucv_sock */
1357 memcpy(niucv->dst_name, ipuser + 8, 8);
1358 EBCASC(niucv->dst_name, 8);
1359 memcpy(niucv->dst_user_id, ipvmid, 8);
1360 memcpy(niucv->src_name, iucv->src_name, 8);
1361 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1362 niucv->path = path;
1363
1364 /* Call iucv_accept */
1365 high_nmcpy(nuser_data, ipuser + 8);
1366 memcpy(nuser_data + 8, niucv->src_name, 8);
1367 ASCEBC(nuser_data + 8, 8);
1368
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001369 /* set message limit for path based on msglimit of accepting socket */
1370 niucv->msglimit = iucv->msglimit;
1371 path->msglim = iucv->msglimit;
Jennifer Hunteac37312007-02-08 13:51:54 -08001372 err = iucv_path_accept(path, &af_iucv_handler, nuser_data, nsk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001373 if (err) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001374 err = iucv_path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001375 iucv_path_free(path);
1376 iucv_sock_kill(nsk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001377 goto fail;
1378 }
1379
1380 iucv_accept_enqueue(sk, nsk);
1381
1382 /* Wake up accept */
1383 nsk->sk_state = IUCV_CONNECTED;
1384 sk->sk_data_ready(sk, 1);
1385 err = 0;
1386fail:
1387 bh_unlock_sock(sk);
1388 return 0;
1389}
1390
1391static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1392{
1393 struct sock *sk = path->private;
1394
1395 sk->sk_state = IUCV_CONNECTED;
1396 sk->sk_state_change(sk);
1397}
1398
1399static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1400{
1401 struct sock *sk = path->private;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001402 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braunf0703c82007-10-08 02:03:31 -07001403 struct sk_buff *skb;
1404 struct sock_msg_q *save_msg;
1405 int len;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001406
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001407 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1408 iucv_message_reject(path, msg);
Jennifer Hunteac37312007-02-08 13:51:54 -08001409 return;
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001410 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001411
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001412 spin_lock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001413
Ursula Braunf0703c82007-10-08 02:03:31 -07001414 if (!list_empty(&iucv->message_q.list) ||
1415 !skb_queue_empty(&iucv->backlog_skb_q))
1416 goto save_message;
1417
1418 len = atomic_read(&sk->sk_rmem_alloc);
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001419 len += iucv_msg_length(msg) + sizeof(struct sk_buff);
Ursula Braunf0703c82007-10-08 02:03:31 -07001420 if (len > sk->sk_rcvbuf)
1421 goto save_message;
1422
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001423 skb = alloc_skb(iucv_msg_length(msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -07001424 if (!skb)
1425 goto save_message;
Jennifer Hunteac37312007-02-08 13:51:54 -08001426
Ursula Braunf0703c82007-10-08 02:03:31 -07001427 iucv_process_message(sk, skb, path, msg);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001428 goto out_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -08001429
Ursula Braunf0703c82007-10-08 02:03:31 -07001430save_message:
1431 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
Ursula Braund4444722008-02-07 18:07:19 -08001432 if (!save_msg)
1433 return;
Ursula Braunf0703c82007-10-08 02:03:31 -07001434 save_msg->path = path;
1435 save_msg->msg = *msg;
1436
Ursula Braunf0703c82007-10-08 02:03:31 -07001437 list_add_tail(&save_msg->list, &iucv->message_q.list);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001438
1439out_unlock:
Ursula Braunf0703c82007-10-08 02:03:31 -07001440 spin_unlock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001441}
1442
1443static void iucv_callback_txdone(struct iucv_path *path,
1444 struct iucv_message *msg)
1445{
1446 struct sock *sk = path->private;
Ursula Braunf2a77992008-02-07 18:07:44 -08001447 struct sk_buff *this = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001448 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1449 struct sk_buff *list_skb = list->next;
1450 unsigned long flags;
1451
Ursula Braunf2a77992008-02-07 18:07:44 -08001452 if (!skb_queue_empty(list)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001453 spin_lock_irqsave(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001454
Ursula Braunf2a77992008-02-07 18:07:44 -08001455 while (list_skb != (struct sk_buff *)list) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001456 if (!memcmp(&msg->tag, CB_TAG(list_skb), CB_TAG_LEN)) {
Ursula Braunf2a77992008-02-07 18:07:44 -08001457 this = list_skb;
1458 break;
1459 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001460 list_skb = list_skb->next;
Ursula Braunf2a77992008-02-07 18:07:44 -08001461 }
1462 if (this)
1463 __skb_unlink(this, list);
Jennifer Hunteac37312007-02-08 13:51:54 -08001464
Jennifer Hunt561e0362007-05-04 12:22:07 -07001465 spin_unlock_irqrestore(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001466
Wei Yongjun47a30b22009-02-25 00:41:03 +00001467 kfree_skb(this);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001468 }
Ursula Braunc2b4afd2008-07-14 09:59:29 +02001469 BUG_ON(!this);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001470
Heiko Carstensda99f052007-05-04 12:23:27 -07001471 if (sk->sk_state == IUCV_CLOSING) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001472 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1473 sk->sk_state = IUCV_CLOSED;
1474 sk->sk_state_change(sk);
1475 }
1476 }
1477
Jennifer Hunteac37312007-02-08 13:51:54 -08001478}
1479
1480static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1481{
1482 struct sock *sk = path->private;
1483
1484 if (!list_empty(&iucv_sk(sk)->accept_q))
1485 sk->sk_state = IUCV_SEVERED;
1486 else
1487 sk->sk_state = IUCV_DISCONN;
1488
1489 sk->sk_state_change(sk);
1490}
1491
Hendrik Brueckneraf88b522009-04-21 23:26:21 +00001492/* called if the other communication side shuts down its RECV direction;
1493 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1494 */
1495static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1496{
1497 struct sock *sk = path->private;
1498
1499 bh_lock_sock(sk);
1500 if (sk->sk_state != IUCV_CLOSED) {
1501 sk->sk_shutdown |= SEND_SHUTDOWN;
1502 sk->sk_state_change(sk);
1503 }
1504 bh_unlock_sock(sk);
1505}
1506
Jennifer Hunteac37312007-02-08 13:51:54 -08001507static struct proto_ops iucv_sock_ops = {
1508 .family = PF_IUCV,
1509 .owner = THIS_MODULE,
1510 .release = iucv_sock_release,
1511 .bind = iucv_sock_bind,
1512 .connect = iucv_sock_connect,
1513 .listen = iucv_sock_listen,
1514 .accept = iucv_sock_accept,
1515 .getname = iucv_sock_getname,
1516 .sendmsg = iucv_sock_sendmsg,
1517 .recvmsg = iucv_sock_recvmsg,
1518 .poll = iucv_sock_poll,
1519 .ioctl = sock_no_ioctl,
1520 .mmap = sock_no_mmap,
1521 .socketpair = sock_no_socketpair,
1522 .shutdown = iucv_sock_shutdown,
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001523 .setsockopt = iucv_sock_setsockopt,
1524 .getsockopt = iucv_sock_getsockopt,
Jennifer Hunteac37312007-02-08 13:51:54 -08001525};
1526
1527static struct net_proto_family iucv_sock_family_ops = {
1528 .family = AF_IUCV,
1529 .owner = THIS_MODULE,
1530 .create = iucv_sock_create,
1531};
1532
Heiko Carstensda99f052007-05-04 12:23:27 -07001533static int __init afiucv_init(void)
Jennifer Hunteac37312007-02-08 13:51:54 -08001534{
1535 int err;
1536
1537 if (!MACHINE_IS_VM) {
Ursula Braun8f7c5022008-12-25 13:39:47 +01001538 pr_err("The af_iucv module cannot be loaded"
1539 " without z/VM\n");
Jennifer Hunteac37312007-02-08 13:51:54 -08001540 err = -EPROTONOSUPPORT;
1541 goto out;
1542 }
1543 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
1544 if (unlikely(err)) {
Ursula Braunc2b4afd2008-07-14 09:59:29 +02001545 WARN_ON(err);
Jennifer Hunteac37312007-02-08 13:51:54 -08001546 err = -EPROTONOSUPPORT;
1547 goto out;
1548 }
1549
1550 err = iucv_register(&af_iucv_handler, 0);
1551 if (err)
1552 goto out;
1553 err = proto_register(&iucv_proto, 0);
1554 if (err)
1555 goto out_iucv;
1556 err = sock_register(&iucv_sock_family_ops);
1557 if (err)
1558 goto out_proto;
Jennifer Hunteac37312007-02-08 13:51:54 -08001559 return 0;
1560
1561out_proto:
1562 proto_unregister(&iucv_proto);
1563out_iucv:
1564 iucv_unregister(&af_iucv_handler, 0);
1565out:
1566 return err;
1567}
1568
1569static void __exit afiucv_exit(void)
1570{
1571 sock_unregister(PF_IUCV);
1572 proto_unregister(&iucv_proto);
1573 iucv_unregister(&af_iucv_handler, 0);
Jennifer Hunteac37312007-02-08 13:51:54 -08001574}
1575
1576module_init(afiucv_init);
1577module_exit(afiucv_exit);
1578
1579MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
1580MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
1581MODULE_VERSION(VERSION);
1582MODULE_LICENSE("GPL");
1583MODULE_ALIAS_NETPROTO(PF_IUCV);