blob: 264c6b36931ce2e63e6a4d27e0c0e817e2e94e81 [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
218 sk->sk_state = IUCV_CLOSED;
219 sk->sk_state_change(sk);
220
Jennifer Hunteac37312007-02-08 13:51:54 -0800221 if (iucv->path) {
222 low_nmcpy(user_data, iucv->src_name);
223 high_nmcpy(user_data, iucv->dst_name);
224 ASCEBC(user_data, sizeof(user_data));
225 err = iucv_path_sever(iucv->path, user_data);
226 iucv_path_free(iucv->path);
227 iucv->path = NULL;
228 }
229
Jennifer Hunteac37312007-02-08 13:51:54 -0800230 sk->sk_err = ECONNRESET;
231 sk->sk_state_change(sk);
232
233 skb_queue_purge(&iucv->send_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700234 skb_queue_purge(&iucv->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800235
236 sock_set_flag(sk, SOCK_ZAPPED);
237 break;
238
239 default:
240 sock_set_flag(sk, SOCK_ZAPPED);
241 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700242 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800243
244 release_sock(sk);
245 iucv_sock_kill(sk);
246}
247
248static void iucv_sock_init(struct sock *sk, struct sock *parent)
249{
250 if (parent)
251 sk->sk_type = parent->sk_type;
252}
253
254static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
255{
256 struct sock *sk;
257
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700258 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -0800259 if (!sk)
260 return NULL;
261
262 sock_init_data(sock, sk);
263 INIT_LIST_HEAD(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700264 spin_lock_init(&iucv_sk(sk)->accept_q_lock);
Jennifer Hunteac37312007-02-08 13:51:54 -0800265 skb_queue_head_init(&iucv_sk(sk)->send_skb_q);
Ursula Braunf0703c82007-10-08 02:03:31 -0700266 INIT_LIST_HEAD(&iucv_sk(sk)->message_q.list);
267 spin_lock_init(&iucv_sk(sk)->message_q.lock);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700268 skb_queue_head_init(&iucv_sk(sk)->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800269 iucv_sk(sk)->send_tag = 0;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +0000270 iucv_sk(sk)->flags = 0;
Hendrik Brueckner09488e22009-04-21 23:26:27 +0000271 iucv_sk(sk)->msglimit = IUCV_QUEUELEN_DEFAULT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800272
273 sk->sk_destruct = iucv_sock_destruct;
274 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
275 sk->sk_allocation = GFP_DMA;
276
277 sock_reset_flag(sk, SOCK_ZAPPED);
278
279 sk->sk_protocol = proto;
280 sk->sk_state = IUCV_OPEN;
281
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800282 setup_timer(&sk->sk_timer, iucv_sock_timeout, (unsigned long)sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800283
284 iucv_sock_link(&iucv_sk_list, sk);
285 return sk;
286}
287
288/* Create an IUCV socket */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700289static int iucv_sock_create(struct net *net, struct socket *sock, int protocol)
Jennifer Hunteac37312007-02-08 13:51:54 -0800290{
291 struct sock *sk;
292
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000293 if (protocol && protocol != PF_IUCV)
294 return -EPROTONOSUPPORT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800295
296 sock->state = SS_UNCONNECTED;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000297
298 switch (sock->type) {
299 case SOCK_STREAM:
300 sock->ops = &iucv_sock_ops;
301 break;
302 case SOCK_SEQPACKET:
303 /* currently, proto ops can handle both sk types */
304 sock->ops = &iucv_sock_ops;
305 break;
306 default:
307 return -ESOCKTNOSUPPORT;
308 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800309
310 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
311 if (!sk)
312 return -ENOMEM;
313
314 iucv_sock_init(sk, NULL);
315
316 return 0;
317}
318
319void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
320{
321 write_lock_bh(&l->lock);
322 sk_add_node(sk, &l->head);
323 write_unlock_bh(&l->lock);
324}
325
326void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
327{
328 write_lock_bh(&l->lock);
329 sk_del_node_init(sk);
330 write_unlock_bh(&l->lock);
331}
332
333void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
334{
Ursula Braunfebca282007-07-14 19:04:25 -0700335 unsigned long flags;
336 struct iucv_sock *par = iucv_sk(parent);
337
Jennifer Hunteac37312007-02-08 13:51:54 -0800338 sock_hold(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700339 spin_lock_irqsave(&par->accept_q_lock, flags);
340 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
341 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800342 iucv_sk(sk)->parent = parent;
343 parent->sk_ack_backlog++;
344}
345
346void iucv_accept_unlink(struct sock *sk)
347{
Ursula Braunfebca282007-07-14 19:04:25 -0700348 unsigned long flags;
349 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
350
351 spin_lock_irqsave(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800352 list_del_init(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700353 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800354 iucv_sk(sk)->parent->sk_ack_backlog--;
355 iucv_sk(sk)->parent = NULL;
356 sock_put(sk);
357}
358
359struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
360{
361 struct iucv_sock *isk, *n;
362 struct sock *sk;
363
Heiko Carstensda99f052007-05-04 12:23:27 -0700364 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800365 sk = (struct sock *) isk;
366 lock_sock(sk);
367
368 if (sk->sk_state == IUCV_CLOSED) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800369 iucv_accept_unlink(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700370 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800371 continue;
372 }
373
374 if (sk->sk_state == IUCV_CONNECTED ||
375 sk->sk_state == IUCV_SEVERED ||
376 !newsock) {
377 iucv_accept_unlink(sk);
378 if (newsock)
379 sock_graft(sk, newsock);
380
381 if (sk->sk_state == IUCV_SEVERED)
382 sk->sk_state = IUCV_DISCONN;
383
384 release_sock(sk);
385 return sk;
386 }
387
388 release_sock(sk);
389 }
390 return NULL;
391}
392
393int iucv_sock_wait_state(struct sock *sk, int state, int state2,
394 unsigned long timeo)
395{
396 DECLARE_WAITQUEUE(wait, current);
397 int err = 0;
398
399 add_wait_queue(sk->sk_sleep, &wait);
400 while (sk->sk_state != state && sk->sk_state != state2) {
401 set_current_state(TASK_INTERRUPTIBLE);
402
403 if (!timeo) {
404 err = -EAGAIN;
405 break;
406 }
407
408 if (signal_pending(current)) {
409 err = sock_intr_errno(timeo);
410 break;
411 }
412
413 release_sock(sk);
414 timeo = schedule_timeout(timeo);
415 lock_sock(sk);
416
417 err = sock_error(sk);
418 if (err)
419 break;
420 }
421 set_current_state(TASK_RUNNING);
422 remove_wait_queue(sk->sk_sleep, &wait);
423 return err;
424}
425
426/* Bind an unbound socket */
427static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
428 int addr_len)
429{
430 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
431 struct sock *sk = sock->sk;
432 struct iucv_sock *iucv;
433 int err;
434
435 /* Verify the input sockaddr */
436 if (!addr || addr->sa_family != AF_IUCV)
437 return -EINVAL;
438
439 lock_sock(sk);
440 if (sk->sk_state != IUCV_OPEN) {
441 err = -EBADFD;
442 goto done;
443 }
444
445 write_lock_bh(&iucv_sk_list.lock);
446
447 iucv = iucv_sk(sk);
448 if (__iucv_get_sock_by_name(sa->siucv_name)) {
449 err = -EADDRINUSE;
450 goto done_unlock;
451 }
452 if (iucv->path) {
453 err = 0;
454 goto done_unlock;
455 }
456
457 /* Bind the socket */
458 memcpy(iucv->src_name, sa->siucv_name, 8);
459
460 /* Copy the user id */
461 memcpy(iucv->src_user_id, iucv_userid, 8);
462 sk->sk_state = IUCV_BOUND;
463 err = 0;
464
465done_unlock:
466 /* Release the socket list lock */
467 write_unlock_bh(&iucv_sk_list.lock);
468done:
469 release_sock(sk);
470 return err;
471}
472
473/* Automatically bind an unbound socket */
474static int iucv_sock_autobind(struct sock *sk)
475{
476 struct iucv_sock *iucv = iucv_sk(sk);
477 char query_buffer[80];
478 char name[12];
479 int err = 0;
480
481 /* Set the userid and name */
482 cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err);
483 if (unlikely(err))
484 return -EPROTO;
485
486 memcpy(iucv->src_user_id, query_buffer, 8);
487
488 write_lock_bh(&iucv_sk_list.lock);
489
490 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
491 while (__iucv_get_sock_by_name(name)) {
492 sprintf(name, "%08x",
493 atomic_inc_return(&iucv_sk_list.autobind_name));
494 }
495
496 write_unlock_bh(&iucv_sk_list.lock);
497
498 memcpy(&iucv->src_name, name, 8);
499
500 return err;
501}
502
503/* Connect an unconnected socket */
504static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
505 int alen, int flags)
506{
507 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
508 struct sock *sk = sock->sk;
509 struct iucv_sock *iucv;
510 unsigned char user_data[16];
511 int err;
512
513 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
514 return -EINVAL;
515
516 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
517 return -EBADFD;
518
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000519 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800520 return -EINVAL;
521
Jennifer Hunteac37312007-02-08 13:51:54 -0800522 if (sk->sk_state == IUCV_OPEN) {
523 err = iucv_sock_autobind(sk);
524 if (unlikely(err))
525 return err;
526 }
527
528 lock_sock(sk);
529
530 /* Set the destination information */
531 memcpy(iucv_sk(sk)->dst_user_id, sa->siucv_user_id, 8);
532 memcpy(iucv_sk(sk)->dst_name, sa->siucv_name, 8);
533
534 high_nmcpy(user_data, sa->siucv_name);
535 low_nmcpy(user_data, iucv_sk(sk)->src_name);
536 ASCEBC(user_data, sizeof(user_data));
537
538 iucv = iucv_sk(sk);
539 /* Create path. */
Hendrik Brueckner09488e22009-04-21 23:26:27 +0000540 iucv->path = iucv_path_alloc(iucv->msglimit,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000541 IUCV_IPRMDATA, GFP_KERNEL);
Ursula Braund4444722008-02-07 18:07:19 -0800542 if (!iucv->path) {
543 err = -ENOMEM;
544 goto done;
545 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800546 err = iucv_path_connect(iucv->path, &af_iucv_handler,
547 sa->siucv_user_id, NULL, user_data, sk);
548 if (err) {
549 iucv_path_free(iucv->path);
550 iucv->path = NULL;
Hendrik Brueckner55cdea92009-01-05 18:07:07 -0800551 switch (err) {
552 case 0x0b: /* Target communicator is not logged on */
553 err = -ENETUNREACH;
554 break;
555 case 0x0d: /* Max connections for this guest exceeded */
556 case 0x0e: /* Max connections for target guest exceeded */
557 err = -EAGAIN;
558 break;
559 case 0x0f: /* Missing IUCV authorization */
560 err = -EACCES;
561 break;
562 default:
563 err = -ECONNREFUSED;
564 break;
565 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800566 goto done;
567 }
568
569 if (sk->sk_state != IUCV_CONNECTED) {
570 err = iucv_sock_wait_state(sk, IUCV_CONNECTED, IUCV_DISCONN,
571 sock_sndtimeo(sk, flags & O_NONBLOCK));
572 }
573
574 if (sk->sk_state == IUCV_DISCONN) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000575 err = -ECONNREFUSED;
Jennifer Hunteac37312007-02-08 13:51:54 -0800576 }
Ursula Braun18becbc2009-01-05 18:07:46 -0800577
578 if (err) {
579 iucv_path_sever(iucv->path, NULL);
580 iucv_path_free(iucv->path);
581 iucv->path = NULL;
582 }
583
Jennifer Hunteac37312007-02-08 13:51:54 -0800584done:
585 release_sock(sk);
586 return err;
587}
588
589/* Move a socket into listening state. */
590static int iucv_sock_listen(struct socket *sock, int backlog)
591{
592 struct sock *sk = sock->sk;
593 int err;
594
595 lock_sock(sk);
596
597 err = -EINVAL;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000598 if (sk->sk_state != IUCV_BOUND)
599 goto done;
600
601 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800602 goto done;
603
604 sk->sk_max_ack_backlog = backlog;
605 sk->sk_ack_backlog = 0;
606 sk->sk_state = IUCV_LISTEN;
607 err = 0;
608
609done:
610 release_sock(sk);
611 return err;
612}
613
614/* Accept a pending connection */
615static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
616 int flags)
617{
618 DECLARE_WAITQUEUE(wait, current);
619 struct sock *sk = sock->sk, *nsk;
620 long timeo;
621 int err = 0;
622
Jennifer Hunt561e0362007-05-04 12:22:07 -0700623 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800624
625 if (sk->sk_state != IUCV_LISTEN) {
626 err = -EBADFD;
627 goto done;
628 }
629
630 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
631
632 /* Wait for an incoming connection */
633 add_wait_queue_exclusive(sk->sk_sleep, &wait);
Heiko Carstensda99f052007-05-04 12:23:27 -0700634 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800635 set_current_state(TASK_INTERRUPTIBLE);
636 if (!timeo) {
637 err = -EAGAIN;
638 break;
639 }
640
641 release_sock(sk);
642 timeo = schedule_timeout(timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700643 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800644
645 if (sk->sk_state != IUCV_LISTEN) {
646 err = -EBADFD;
647 break;
648 }
649
650 if (signal_pending(current)) {
651 err = sock_intr_errno(timeo);
652 break;
653 }
654 }
655
656 set_current_state(TASK_RUNNING);
657 remove_wait_queue(sk->sk_sleep, &wait);
658
659 if (err)
660 goto done;
661
662 newsock->state = SS_CONNECTED;
663
664done:
665 release_sock(sk);
666 return err;
667}
668
669static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
670 int *len, int peer)
671{
672 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
673 struct sock *sk = sock->sk;
674
675 addr->sa_family = AF_IUCV;
676 *len = sizeof(struct sockaddr_iucv);
677
678 if (peer) {
679 memcpy(siucv->siucv_user_id, iucv_sk(sk)->dst_user_id, 8);
680 memcpy(siucv->siucv_name, &iucv_sk(sk)->dst_name, 8);
681 } else {
682 memcpy(siucv->siucv_user_id, iucv_sk(sk)->src_user_id, 8);
683 memcpy(siucv->siucv_name, iucv_sk(sk)->src_name, 8);
684 }
685 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
686 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
687 memset(siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
688
689 return 0;
690}
691
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000692/**
693 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
694 * @path: IUCV path
695 * @msg: Pointer to a struct iucv_message
696 * @skb: The socket data to send, skb->len MUST BE <= 7
697 *
698 * Send the socket data in the parameter list in the iucv message
699 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
700 * list and the socket data len at index 7 (last byte).
701 * See also iucv_msg_length().
702 *
703 * Returns the error code from the iucv_message_send() call.
704 */
705static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
706 struct sk_buff *skb)
707{
708 u8 prmdata[8];
709
710 memcpy(prmdata, (void *) skb->data, skb->len);
711 prmdata[7] = 0xff - (u8) skb->len;
712 return iucv_message_send(path, msg, IUCV_IPRMDATA, 0,
713 (void *) prmdata, 8);
714}
715
Jennifer Hunteac37312007-02-08 13:51:54 -0800716static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
717 struct msghdr *msg, size_t len)
718{
719 struct sock *sk = sock->sk;
720 struct iucv_sock *iucv = iucv_sk(sk);
721 struct sk_buff *skb;
722 struct iucv_message txmsg;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000723 struct cmsghdr *cmsg;
724 int cmsg_done;
Ursula Braun8f7c5022008-12-25 13:39:47 +0100725 char user_id[9];
726 char appl_id[9];
Jennifer Hunteac37312007-02-08 13:51:54 -0800727 int err;
728
729 err = sock_error(sk);
730 if (err)
731 return err;
732
733 if (msg->msg_flags & MSG_OOB)
734 return -EOPNOTSUPP;
735
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000736 /* SOCK_SEQPACKET: we do not support segmented records */
737 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
738 return -EOPNOTSUPP;
739
Jennifer Hunteac37312007-02-08 13:51:54 -0800740 lock_sock(sk);
741
742 if (sk->sk_shutdown & SEND_SHUTDOWN) {
743 err = -EPIPE;
744 goto out;
745 }
746
Heiko Carstensda99f052007-05-04 12:23:27 -0700747 if (sk->sk_state == IUCV_CONNECTED) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000748 /* initialize defaults */
749 cmsg_done = 0; /* check for duplicate headers */
750 txmsg.class = 0;
751
752 /* iterate over control messages */
753 for (cmsg = CMSG_FIRSTHDR(msg); cmsg;
754 cmsg = CMSG_NXTHDR(msg, cmsg)) {
755
756 if (!CMSG_OK(msg, cmsg)) {
757 err = -EINVAL;
758 goto out;
759 }
760
761 if (cmsg->cmsg_level != SOL_IUCV)
762 continue;
763
764 if (cmsg->cmsg_type & cmsg_done) {
765 err = -EINVAL;
766 goto out;
767 }
768 cmsg_done |= cmsg->cmsg_type;
769
770 switch (cmsg->cmsg_type) {
771 case SCM_IUCV_TRGCLS:
772 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
773 err = -EINVAL;
774 goto out;
775 }
776
777 /* set iucv message target class */
778 memcpy(&txmsg.class,
779 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
780
781 break;
782
783 default:
784 err = -EINVAL;
785 goto out;
786 break;
787 }
788 }
789
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000790 /* allocate one skb for each iucv message:
791 * this is fine for SOCK_SEQPACKET (unless we want to support
792 * segmented records using the MSG_EOR flag), but
793 * for SOCK_STREAM we might want to improve it in future */
Heiko Carstensda99f052007-05-04 12:23:27 -0700794 if (!(skb = sock_alloc_send_skb(sk, len,
795 msg->msg_flags & MSG_DONTWAIT,
796 &err)))
Jennifer Hunt561e0362007-05-04 12:22:07 -0700797 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -0800798
Heiko Carstensda99f052007-05-04 12:23:27 -0700799 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800800 err = -EFAULT;
801 goto fail;
802 }
803
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000804 /* increment and save iucv message tag for msg_completion cbk */
Jennifer Hunteac37312007-02-08 13:51:54 -0800805 txmsg.tag = iucv->send_tag++;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000806 memcpy(CB_TAG(skb), &txmsg.tag, CB_TAG_LEN);
Jennifer Hunteac37312007-02-08 13:51:54 -0800807 skb_queue_tail(&iucv->send_skb_q, skb);
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000808
809 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags)
810 && skb->len <= 7) {
811 err = iucv_send_iprm(iucv->path, &txmsg, skb);
812
813 /* on success: there is no message_complete callback
814 * for an IPRMDATA msg; remove skb from send queue */
815 if (err == 0) {
816 skb_unlink(skb, &iucv->send_skb_q);
817 kfree_skb(skb);
818 }
819
820 /* this error should never happen since the
821 * IUCV_IPRMDATA path flag is set... sever path */
822 if (err == 0x15) {
823 iucv_path_sever(iucv->path, NULL);
824 skb_unlink(skb, &iucv->send_skb_q);
825 err = -EPIPE;
826 goto fail;
827 }
828 } else
829 err = iucv_message_send(iucv->path, &txmsg, 0, 0,
830 (void *) skb->data, skb->len);
Jennifer Hunteac37312007-02-08 13:51:54 -0800831 if (err) {
Ursula Braun8f7c5022008-12-25 13:39:47 +0100832 if (err == 3) {
833 user_id[8] = 0;
834 memcpy(user_id, iucv->dst_user_id, 8);
835 appl_id[8] = 0;
836 memcpy(appl_id, iucv->dst_name, 8);
837 pr_err("Application %s on z/VM guest %s"
838 " exceeds message limit\n",
839 user_id, appl_id);
840 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800841 skb_unlink(skb, &iucv->send_skb_q);
842 err = -EPIPE;
843 goto fail;
844 }
845
846 } else {
847 err = -ENOTCONN;
848 goto out;
849 }
850
851 release_sock(sk);
852 return len;
853
854fail:
855 kfree_skb(skb);
856out:
857 release_sock(sk);
858 return err;
859}
860
Ursula Braunf0703c82007-10-08 02:03:31 -0700861static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
862{
863 int dataleft, size, copied = 0;
864 struct sk_buff *nskb;
865
866 dataleft = len;
867 while (dataleft) {
868 if (dataleft >= sk->sk_rcvbuf / 4)
869 size = sk->sk_rcvbuf / 4;
870 else
871 size = dataleft;
872
873 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
874 if (!nskb)
875 return -ENOMEM;
876
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000877 /* copy target class to control buffer of new skb */
878 memcpy(CB_TRGCLS(nskb), CB_TRGCLS(skb), CB_TRGCLS_LEN);
879
880 /* copy data fragment */
Ursula Braunf0703c82007-10-08 02:03:31 -0700881 memcpy(nskb->data, skb->data + copied, size);
882 copied += size;
883 dataleft -= size;
884
885 skb_reset_transport_header(nskb);
886 skb_reset_network_header(nskb);
887 nskb->len = size;
888
889 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
890 }
891
892 return 0;
893}
894
895static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
896 struct iucv_path *path,
897 struct iucv_message *msg)
898{
899 int rc;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000900 unsigned int len;
Ursula Braunf0703c82007-10-08 02:03:31 -0700901
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000902 len = iucv_msg_length(msg);
903
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000904 /* store msg target class in the second 4 bytes of skb ctrl buffer */
905 /* Note: the first 4 bytes are reserved for msg tag */
906 memcpy(CB_TRGCLS(skb), &msg->class, CB_TRGCLS_LEN);
907
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000908 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
909 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
910 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
911 skb->data = NULL;
912 skb->len = 0;
913 }
Ursula Braunf0703c82007-10-08 02:03:31 -0700914 } else {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000915 rc = iucv_message_receive(path, msg, msg->flags & IUCV_IPRMDATA,
916 skb->data, len, NULL);
Ursula Braunf0703c82007-10-08 02:03:31 -0700917 if (rc) {
918 kfree_skb(skb);
919 return;
920 }
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000921 /* we need to fragment iucv messages for SOCK_STREAM only;
922 * for SOCK_SEQPACKET, it is only relevant if we support
923 * record segmentation using MSG_EOR (see also recvmsg()) */
924 if (sk->sk_type == SOCK_STREAM &&
925 skb->truesize >= sk->sk_rcvbuf / 4) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000926 rc = iucv_fragment_skb(sk, skb, len);
Ursula Braunf0703c82007-10-08 02:03:31 -0700927 kfree_skb(skb);
928 skb = NULL;
929 if (rc) {
930 iucv_path_sever(path, NULL);
931 return;
932 }
933 skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
934 } else {
935 skb_reset_transport_header(skb);
936 skb_reset_network_header(skb);
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000937 skb->len = len;
Ursula Braunf0703c82007-10-08 02:03:31 -0700938 }
939 }
940
941 if (sock_queue_rcv_skb(sk, skb))
942 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
943}
944
945static void iucv_process_message_q(struct sock *sk)
946{
947 struct iucv_sock *iucv = iucv_sk(sk);
948 struct sk_buff *skb;
949 struct sock_msg_q *p, *n;
950
951 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000952 skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -0700953 if (!skb)
954 break;
955 iucv_process_message(sk, skb, p->path, &p->msg);
956 list_del(&p->list);
957 kfree(p);
958 if (!skb_queue_empty(&iucv->backlog_skb_q))
959 break;
960 }
961}
962
Jennifer Hunteac37312007-02-08 13:51:54 -0800963static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
964 struct msghdr *msg, size_t len, int flags)
965{
966 int noblock = flags & MSG_DONTWAIT;
967 struct sock *sk = sock->sk;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700968 struct iucv_sock *iucv = iucv_sk(sk);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000969 unsigned int copied, rlen;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700970 struct sk_buff *skb, *rskb, *cskb;
Jennifer Hunteac37312007-02-08 13:51:54 -0800971 int err = 0;
972
Jennifer Hunt561e0362007-05-04 12:22:07 -0700973 if ((sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED) &&
Ursula Braunf0703c82007-10-08 02:03:31 -0700974 skb_queue_empty(&iucv->backlog_skb_q) &&
975 skb_queue_empty(&sk->sk_receive_queue) &&
976 list_empty(&iucv->message_q.list))
Jennifer Hunt561e0362007-05-04 12:22:07 -0700977 return 0;
978
Jennifer Hunteac37312007-02-08 13:51:54 -0800979 if (flags & (MSG_OOB))
980 return -EOPNOTSUPP;
981
Jennifer Hunteac37312007-02-08 13:51:54 -0800982 skb = skb_recv_datagram(sk, flags, noblock, &err);
983 if (!skb) {
984 if (sk->sk_shutdown & RCV_SHUTDOWN)
985 return 0;
986 return err;
987 }
988
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000989 rlen = skb->len; /* real length of skb */
990 copied = min_t(unsigned int, rlen, len);
Jennifer Hunteac37312007-02-08 13:51:54 -0800991
Jennifer Hunt561e0362007-05-04 12:22:07 -0700992 cskb = skb;
993 if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) {
Hendrik Brueckner802788b2009-04-21 23:26:26 +0000994 if (!(flags & MSG_PEEK))
995 skb_queue_head(&sk->sk_receive_queue, skb);
996 return -EFAULT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800997 }
998
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000999 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1000 if (sk->sk_type == SOCK_SEQPACKET) {
1001 if (copied < rlen)
1002 msg->msg_flags |= MSG_TRUNC;
1003 /* each iucv message contains a complete record */
1004 msg->msg_flags |= MSG_EOR;
1005 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001006
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001007 /* create control message to store iucv msg target class:
1008 * get the trgcls from the control buffer of the skb due to
1009 * fragmentation of original iucv message. */
1010 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1011 CB_TRGCLS_LEN, CB_TRGCLS(skb));
1012 if (err) {
1013 if (!(flags & MSG_PEEK))
1014 skb_queue_head(&sk->sk_receive_queue, skb);
1015 return err;
1016 }
1017
Jennifer Hunteac37312007-02-08 13:51:54 -08001018 /* Mark read part of skb as used */
1019 if (!(flags & MSG_PEEK)) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001020
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001021 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1022 if (sk->sk_type == SOCK_STREAM) {
1023 skb_pull(skb, copied);
1024 if (skb->len) {
1025 skb_queue_head(&sk->sk_receive_queue, skb);
1026 goto done;
1027 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001028 }
1029
1030 kfree_skb(skb);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001031
1032 /* Queue backlog skbs */
Ursula Braunf0703c82007-10-08 02:03:31 -07001033 rskb = skb_dequeue(&iucv->backlog_skb_q);
Heiko Carstensda99f052007-05-04 12:23:27 -07001034 while (rskb) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001035 if (sock_queue_rcv_skb(sk, rskb)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001036 skb_queue_head(&iucv->backlog_skb_q,
Jennifer Hunt561e0362007-05-04 12:22:07 -07001037 rskb);
1038 break;
1039 } else {
Ursula Braunf0703c82007-10-08 02:03:31 -07001040 rskb = skb_dequeue(&iucv->backlog_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001041 }
1042 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001043 if (skb_queue_empty(&iucv->backlog_skb_q)) {
1044 spin_lock_bh(&iucv->message_q.lock);
1045 if (!list_empty(&iucv->message_q.list))
1046 iucv_process_message_q(sk);
1047 spin_unlock_bh(&iucv->message_q.lock);
1048 }
1049
Jennifer Hunteac37312007-02-08 13:51:54 -08001050 } else
1051 skb_queue_head(&sk->sk_receive_queue, skb);
1052
1053done:
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001054 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1055 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1056 copied = rlen;
1057
1058 return copied;
Jennifer Hunteac37312007-02-08 13:51:54 -08001059}
1060
1061static inline unsigned int iucv_accept_poll(struct sock *parent)
1062{
1063 struct iucv_sock *isk, *n;
1064 struct sock *sk;
1065
Heiko Carstensda99f052007-05-04 12:23:27 -07001066 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001067 sk = (struct sock *) isk;
1068
1069 if (sk->sk_state == IUCV_CONNECTED)
1070 return POLLIN | POLLRDNORM;
1071 }
1072
1073 return 0;
1074}
1075
1076unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
1077 poll_table *wait)
1078{
1079 struct sock *sk = sock->sk;
1080 unsigned int mask = 0;
1081
1082 poll_wait(file, sk->sk_sleep, wait);
1083
1084 if (sk->sk_state == IUCV_LISTEN)
1085 return iucv_accept_poll(sk);
1086
1087 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1088 mask |= POLLERR;
1089
1090 if (sk->sk_shutdown & RCV_SHUTDOWN)
1091 mask |= POLLRDHUP;
1092
1093 if (sk->sk_shutdown == SHUTDOWN_MASK)
1094 mask |= POLLHUP;
1095
1096 if (!skb_queue_empty(&sk->sk_receive_queue) ||
Heiko Carstensda99f052007-05-04 12:23:27 -07001097 (sk->sk_shutdown & RCV_SHUTDOWN))
Jennifer Hunteac37312007-02-08 13:51:54 -08001098 mask |= POLLIN | POLLRDNORM;
1099
1100 if (sk->sk_state == IUCV_CLOSED)
1101 mask |= POLLHUP;
1102
Jennifer Hunt561e0362007-05-04 12:22:07 -07001103 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED)
1104 mask |= POLLIN;
1105
Jennifer Hunteac37312007-02-08 13:51:54 -08001106 if (sock_writeable(sk))
1107 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1108 else
1109 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1110
1111 return mask;
1112}
1113
1114static int iucv_sock_shutdown(struct socket *sock, int how)
1115{
1116 struct sock *sk = sock->sk;
1117 struct iucv_sock *iucv = iucv_sk(sk);
1118 struct iucv_message txmsg;
1119 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -08001120
1121 how++;
1122
1123 if ((how & ~SHUTDOWN_MASK) || !how)
1124 return -EINVAL;
1125
1126 lock_sock(sk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001127 switch (sk->sk_state) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001128 case IUCV_CLOSED:
1129 err = -ENOTCONN;
1130 goto fail;
1131
1132 default:
1133 sk->sk_shutdown |= how;
1134 break;
1135 }
1136
1137 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
1138 txmsg.class = 0;
1139 txmsg.tag = 0;
1140 err = iucv_message_send(iucv->path, &txmsg, IUCV_IPRMDATA, 0,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001141 (void *) iprm_shutdown, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -08001142 if (err) {
Heiko Carstensda99f052007-05-04 12:23:27 -07001143 switch (err) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001144 case 1:
1145 err = -ENOTCONN;
1146 break;
1147 case 2:
1148 err = -ECONNRESET;
1149 break;
1150 default:
1151 err = -ENOTCONN;
1152 break;
1153 }
1154 }
1155 }
1156
1157 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
1158 err = iucv_path_quiesce(iucv_sk(sk)->path, NULL);
1159 if (err)
1160 err = -ENOTCONN;
1161
1162 skb_queue_purge(&sk->sk_receive_queue);
1163 }
1164
1165 /* Wake up anyone sleeping in poll */
1166 sk->sk_state_change(sk);
1167
1168fail:
1169 release_sock(sk);
1170 return err;
1171}
1172
1173static int iucv_sock_release(struct socket *sock)
1174{
1175 struct sock *sk = sock->sk;
1176 int err = 0;
1177
1178 if (!sk)
1179 return 0;
1180
1181 iucv_sock_close(sk);
1182
1183 /* Unregister with IUCV base support */
1184 if (iucv_sk(sk)->path) {
1185 iucv_path_sever(iucv_sk(sk)->path, NULL);
1186 iucv_path_free(iucv_sk(sk)->path);
1187 iucv_sk(sk)->path = NULL;
1188 }
1189
Jennifer Hunteac37312007-02-08 13:51:54 -08001190 sock_orphan(sk);
1191 iucv_sock_kill(sk);
1192 return err;
1193}
1194
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001195/* getsockopt and setsockopt */
1196static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
1197 char __user *optval, int optlen)
1198{
1199 struct sock *sk = sock->sk;
1200 struct iucv_sock *iucv = iucv_sk(sk);
1201 int val;
1202 int rc;
1203
1204 if (level != SOL_IUCV)
1205 return -ENOPROTOOPT;
1206
1207 if (optlen < sizeof(int))
1208 return -EINVAL;
1209
1210 if (get_user(val, (int __user *) optval))
1211 return -EFAULT;
1212
1213 rc = 0;
1214
1215 lock_sock(sk);
1216 switch (optname) {
1217 case SO_IPRMDATA_MSG:
1218 if (val)
1219 iucv->flags |= IUCV_IPRMDATA;
1220 else
1221 iucv->flags &= ~IUCV_IPRMDATA;
1222 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001223 case SO_MSGLIMIT:
1224 switch (sk->sk_state) {
1225 case IUCV_OPEN:
1226 case IUCV_BOUND:
1227 if (val < 1 || val > (u16)(~0))
1228 rc = -EINVAL;
1229 else
1230 iucv->msglimit = val;
1231 break;
1232 default:
1233 rc = -EINVAL;
1234 break;
1235 }
1236 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001237 default:
1238 rc = -ENOPROTOOPT;
1239 break;
1240 }
1241 release_sock(sk);
1242
1243 return rc;
1244}
1245
1246static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1247 char __user *optval, int __user *optlen)
1248{
1249 struct sock *sk = sock->sk;
1250 struct iucv_sock *iucv = iucv_sk(sk);
1251 int val, len;
1252
1253 if (level != SOL_IUCV)
1254 return -ENOPROTOOPT;
1255
1256 if (get_user(len, optlen))
1257 return -EFAULT;
1258
1259 if (len < 0)
1260 return -EINVAL;
1261
1262 len = min_t(unsigned int, len, sizeof(int));
1263
1264 switch (optname) {
1265 case SO_IPRMDATA_MSG:
1266 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1267 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001268 case SO_MSGLIMIT:
1269 lock_sock(sk);
1270 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1271 : iucv->msglimit; /* default */
1272 release_sock(sk);
1273 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001274 default:
1275 return -ENOPROTOOPT;
1276 }
1277
1278 if (put_user(len, optlen))
1279 return -EFAULT;
1280 if (copy_to_user(optval, &val, len))
1281 return -EFAULT;
1282
1283 return 0;
1284}
1285
1286
Jennifer Hunteac37312007-02-08 13:51:54 -08001287/* Callback wrappers - called from iucv base support */
1288static int iucv_callback_connreq(struct iucv_path *path,
1289 u8 ipvmid[8], u8 ipuser[16])
1290{
1291 unsigned char user_data[16];
1292 unsigned char nuser_data[16];
1293 unsigned char src_name[8];
1294 struct hlist_node *node;
1295 struct sock *sk, *nsk;
1296 struct iucv_sock *iucv, *niucv;
1297 int err;
1298
1299 memcpy(src_name, ipuser, 8);
1300 EBCASC(src_name, 8);
1301 /* Find out if this path belongs to af_iucv. */
1302 read_lock(&iucv_sk_list.lock);
1303 iucv = NULL;
Ursula Braunfebca282007-07-14 19:04:25 -07001304 sk = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001305 sk_for_each(sk, node, &iucv_sk_list.head)
1306 if (sk->sk_state == IUCV_LISTEN &&
1307 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1308 /*
1309 * Found a listening socket with
1310 * src_name == ipuser[0-7].
1311 */
1312 iucv = iucv_sk(sk);
1313 break;
1314 }
1315 read_unlock(&iucv_sk_list.lock);
1316 if (!iucv)
1317 /* No socket found, not one of our paths. */
1318 return -EINVAL;
1319
1320 bh_lock_sock(sk);
1321
1322 /* Check if parent socket is listening */
1323 low_nmcpy(user_data, iucv->src_name);
1324 high_nmcpy(user_data, iucv->dst_name);
1325 ASCEBC(user_data, sizeof(user_data));
1326 if (sk->sk_state != IUCV_LISTEN) {
1327 err = iucv_path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001328 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001329 goto fail;
1330 }
1331
1332 /* Check for backlog size */
1333 if (sk_acceptq_is_full(sk)) {
1334 err = iucv_path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001335 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001336 goto fail;
1337 }
1338
1339 /* Create the new socket */
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001340 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
Heiko Carstensda99f052007-05-04 12:23:27 -07001341 if (!nsk) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001342 err = iucv_path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001343 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001344 goto fail;
1345 }
1346
1347 niucv = iucv_sk(nsk);
1348 iucv_sock_init(nsk, sk);
1349
1350 /* Set the new iucv_sock */
1351 memcpy(niucv->dst_name, ipuser + 8, 8);
1352 EBCASC(niucv->dst_name, 8);
1353 memcpy(niucv->dst_user_id, ipvmid, 8);
1354 memcpy(niucv->src_name, iucv->src_name, 8);
1355 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1356 niucv->path = path;
1357
1358 /* Call iucv_accept */
1359 high_nmcpy(nuser_data, ipuser + 8);
1360 memcpy(nuser_data + 8, niucv->src_name, 8);
1361 ASCEBC(nuser_data + 8, 8);
1362
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001363 /* set message limit for path based on msglimit of accepting socket */
1364 niucv->msglimit = iucv->msglimit;
1365 path->msglim = iucv->msglimit;
Jennifer Hunteac37312007-02-08 13:51:54 -08001366 err = iucv_path_accept(path, &af_iucv_handler, nuser_data, nsk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001367 if (err) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001368 err = iucv_path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001369 iucv_path_free(path);
1370 iucv_sock_kill(nsk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001371 goto fail;
1372 }
1373
1374 iucv_accept_enqueue(sk, nsk);
1375
1376 /* Wake up accept */
1377 nsk->sk_state = IUCV_CONNECTED;
1378 sk->sk_data_ready(sk, 1);
1379 err = 0;
1380fail:
1381 bh_unlock_sock(sk);
1382 return 0;
1383}
1384
1385static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1386{
1387 struct sock *sk = path->private;
1388
1389 sk->sk_state = IUCV_CONNECTED;
1390 sk->sk_state_change(sk);
1391}
1392
1393static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1394{
1395 struct sock *sk = path->private;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001396 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braunf0703c82007-10-08 02:03:31 -07001397 struct sk_buff *skb;
1398 struct sock_msg_q *save_msg;
1399 int len;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001400
Jennifer Hunteac37312007-02-08 13:51:54 -08001401 if (sk->sk_shutdown & RCV_SHUTDOWN)
1402 return;
1403
Ursula Braunf0703c82007-10-08 02:03:31 -07001404 if (!list_empty(&iucv->message_q.list) ||
1405 !skb_queue_empty(&iucv->backlog_skb_q))
1406 goto save_message;
1407
1408 len = atomic_read(&sk->sk_rmem_alloc);
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001409 len += iucv_msg_length(msg) + sizeof(struct sk_buff);
Ursula Braunf0703c82007-10-08 02:03:31 -07001410 if (len > sk->sk_rcvbuf)
1411 goto save_message;
1412
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001413 skb = alloc_skb(iucv_msg_length(msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -07001414 if (!skb)
1415 goto save_message;
Jennifer Hunteac37312007-02-08 13:51:54 -08001416
Ursula Braunf0703c82007-10-08 02:03:31 -07001417 spin_lock(&iucv->message_q.lock);
1418 iucv_process_message(sk, skb, path, msg);
1419 spin_unlock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001420
Ursula Braunf0703c82007-10-08 02:03:31 -07001421 return;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001422
Ursula Braunf0703c82007-10-08 02:03:31 -07001423save_message:
1424 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
Ursula Braund4444722008-02-07 18:07:19 -08001425 if (!save_msg)
1426 return;
Ursula Braunf0703c82007-10-08 02:03:31 -07001427 save_msg->path = path;
1428 save_msg->msg = *msg;
1429
1430 spin_lock(&iucv->message_q.lock);
1431 list_add_tail(&save_msg->list, &iucv->message_q.list);
1432 spin_unlock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001433}
1434
1435static void iucv_callback_txdone(struct iucv_path *path,
1436 struct iucv_message *msg)
1437{
1438 struct sock *sk = path->private;
Ursula Braunf2a77992008-02-07 18:07:44 -08001439 struct sk_buff *this = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001440 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1441 struct sk_buff *list_skb = list->next;
1442 unsigned long flags;
1443
Ursula Braunf2a77992008-02-07 18:07:44 -08001444 if (!skb_queue_empty(list)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001445 spin_lock_irqsave(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001446
Ursula Braunf2a77992008-02-07 18:07:44 -08001447 while (list_skb != (struct sk_buff *)list) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001448 if (!memcmp(&msg->tag, CB_TAG(list_skb), CB_TAG_LEN)) {
Ursula Braunf2a77992008-02-07 18:07:44 -08001449 this = list_skb;
1450 break;
1451 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001452 list_skb = list_skb->next;
Ursula Braunf2a77992008-02-07 18:07:44 -08001453 }
1454 if (this)
1455 __skb_unlink(this, list);
Jennifer Hunteac37312007-02-08 13:51:54 -08001456
Jennifer Hunt561e0362007-05-04 12:22:07 -07001457 spin_unlock_irqrestore(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001458
Wei Yongjun47a30b262009-02-25 00:41:03 +00001459 kfree_skb(this);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001460 }
Ursula Braunc2b4afd2008-07-14 09:59:29 +02001461 BUG_ON(!this);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001462
Heiko Carstensda99f052007-05-04 12:23:27 -07001463 if (sk->sk_state == IUCV_CLOSING) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001464 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1465 sk->sk_state = IUCV_CLOSED;
1466 sk->sk_state_change(sk);
1467 }
1468 }
1469
Jennifer Hunteac37312007-02-08 13:51:54 -08001470}
1471
1472static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1473{
1474 struct sock *sk = path->private;
1475
1476 if (!list_empty(&iucv_sk(sk)->accept_q))
1477 sk->sk_state = IUCV_SEVERED;
1478 else
1479 sk->sk_state = IUCV_DISCONN;
1480
1481 sk->sk_state_change(sk);
1482}
1483
Hendrik Brueckneraf88b522009-04-21 23:26:21 +00001484/* called if the other communication side shuts down its RECV direction;
1485 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1486 */
1487static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1488{
1489 struct sock *sk = path->private;
1490
1491 bh_lock_sock(sk);
1492 if (sk->sk_state != IUCV_CLOSED) {
1493 sk->sk_shutdown |= SEND_SHUTDOWN;
1494 sk->sk_state_change(sk);
1495 }
1496 bh_unlock_sock(sk);
1497}
1498
Jennifer Hunteac37312007-02-08 13:51:54 -08001499static struct proto_ops iucv_sock_ops = {
1500 .family = PF_IUCV,
1501 .owner = THIS_MODULE,
1502 .release = iucv_sock_release,
1503 .bind = iucv_sock_bind,
1504 .connect = iucv_sock_connect,
1505 .listen = iucv_sock_listen,
1506 .accept = iucv_sock_accept,
1507 .getname = iucv_sock_getname,
1508 .sendmsg = iucv_sock_sendmsg,
1509 .recvmsg = iucv_sock_recvmsg,
1510 .poll = iucv_sock_poll,
1511 .ioctl = sock_no_ioctl,
1512 .mmap = sock_no_mmap,
1513 .socketpair = sock_no_socketpair,
1514 .shutdown = iucv_sock_shutdown,
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001515 .setsockopt = iucv_sock_setsockopt,
1516 .getsockopt = iucv_sock_getsockopt,
Jennifer Hunteac37312007-02-08 13:51:54 -08001517};
1518
1519static struct net_proto_family iucv_sock_family_ops = {
1520 .family = AF_IUCV,
1521 .owner = THIS_MODULE,
1522 .create = iucv_sock_create,
1523};
1524
Heiko Carstensda99f052007-05-04 12:23:27 -07001525static int __init afiucv_init(void)
Jennifer Hunteac37312007-02-08 13:51:54 -08001526{
1527 int err;
1528
1529 if (!MACHINE_IS_VM) {
Ursula Braun8f7c5022008-12-25 13:39:47 +01001530 pr_err("The af_iucv module cannot be loaded"
1531 " without z/VM\n");
Jennifer Hunteac37312007-02-08 13:51:54 -08001532 err = -EPROTONOSUPPORT;
1533 goto out;
1534 }
1535 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
1536 if (unlikely(err)) {
Ursula Braunc2b4afd2008-07-14 09:59:29 +02001537 WARN_ON(err);
Jennifer Hunteac37312007-02-08 13:51:54 -08001538 err = -EPROTONOSUPPORT;
1539 goto out;
1540 }
1541
1542 err = iucv_register(&af_iucv_handler, 0);
1543 if (err)
1544 goto out;
1545 err = proto_register(&iucv_proto, 0);
1546 if (err)
1547 goto out_iucv;
1548 err = sock_register(&iucv_sock_family_ops);
1549 if (err)
1550 goto out_proto;
Jennifer Hunteac37312007-02-08 13:51:54 -08001551 return 0;
1552
1553out_proto:
1554 proto_unregister(&iucv_proto);
1555out_iucv:
1556 iucv_unregister(&af_iucv_handler, 0);
1557out:
1558 return err;
1559}
1560
1561static void __exit afiucv_exit(void)
1562{
1563 sock_unregister(PF_IUCV);
1564 proto_unregister(&iucv_proto);
1565 iucv_unregister(&af_iucv_handler, 0);
Jennifer Hunteac37312007-02-08 13:51:54 -08001566}
1567
1568module_init(afiucv_init);
1569module_exit(afiucv_exit);
1570
1571MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
1572MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
1573MODULE_VERSION(VERSION);
1574MODULE_LICENSE("GPL");
1575MODULE_ALIAS_NETPROTO(PF_IUCV);