blob: 026704a47296cbb4eb0eb91fc354cb2dcdfc1b3e [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
11#include <linux/module.h>
12#include <linux/types.h>
13#include <linux/list.h>
14#include <linux/errno.h>
15#include <linux/kernel.h>
16#include <linux/sched.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/init.h>
20#include <linux/poll.h>
21#include <net/sock.h>
22#include <asm/ebcdic.h>
23#include <asm/cpcmd.h>
24#include <linux/kmod.h>
25
26#include <net/iucv/iucv.h>
27#include <net/iucv/af_iucv.h>
28
29#define CONFIG_IUCV_SOCK_DEBUG 1
30
31#define IPRMDATA 0x80
32#define VERSION "1.0"
33
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
44/* Call Back functions */
45static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
46static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
47static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
48static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8], u8 ipuser[16]);
49static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
50
51static struct iucv_sock_list iucv_sk_list = {
52 .lock = RW_LOCK_UNLOCKED,
53 .autobind_name = ATOMIC_INIT(0)
54};
55
56static struct iucv_handler af_iucv_handler = {
57 .path_pending = iucv_callback_connreq,
58 .path_complete = iucv_callback_connack,
59 .path_severed = iucv_callback_connrej,
60 .message_pending = iucv_callback_rx,
61 .message_complete = iucv_callback_txdone
62};
63
64static inline void high_nmcpy(unsigned char *dst, char *src)
65{
66 memcpy(dst, src, 8);
67}
68
69static inline void low_nmcpy(unsigned char *dst, char *src)
70{
71 memcpy(&dst[8], src, 8);
72}
73
74/* Timers */
75static void iucv_sock_timeout(unsigned long arg)
76{
77 struct sock *sk = (struct sock *)arg;
78
79 bh_lock_sock(sk);
80 sk->sk_err = ETIMEDOUT;
81 sk->sk_state_change(sk);
82 bh_unlock_sock(sk);
83
84 iucv_sock_kill(sk);
85 sock_put(sk);
86}
87
88static void iucv_sock_clear_timer(struct sock *sk)
89{
90 sk_stop_timer(sk, &sk->sk_timer);
91}
92
93static void iucv_sock_init_timer(struct sock *sk)
94{
95 init_timer(&sk->sk_timer);
96 sk->sk_timer.function = iucv_sock_timeout;
97 sk->sk_timer.data = (unsigned long)sk;
98}
99
100static struct sock *__iucv_get_sock_by_name(char *nm)
101{
102 struct sock *sk;
103 struct hlist_node *node;
104
105 sk_for_each(sk, node, &iucv_sk_list.head)
106 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
107 return sk;
108
109 return NULL;
110}
111
112static void iucv_sock_destruct(struct sock *sk)
113{
114 skb_queue_purge(&sk->sk_receive_queue);
115 skb_queue_purge(&sk->sk_write_queue);
116}
117
118/* Cleanup Listen */
119static void iucv_sock_cleanup_listen(struct sock *parent)
120{
121 struct sock *sk;
122
123 /* Close non-accepted connections */
124 while ((sk = iucv_accept_dequeue(parent, NULL))) {
125 iucv_sock_close(sk);
126 iucv_sock_kill(sk);
127 }
128
129 parent->sk_state = IUCV_CLOSED;
130 sock_set_flag(parent, SOCK_ZAPPED);
131}
132
133/* Kill socket */
134static void iucv_sock_kill(struct sock *sk)
135{
136 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
137 return;
138
139 iucv_sock_unlink(&iucv_sk_list, sk);
140 sock_set_flag(sk, SOCK_DEAD);
141 sock_put(sk);
142}
143
144/* Close an IUCV socket */
145static void iucv_sock_close(struct sock *sk)
146{
147 unsigned char user_data[16];
148 struct iucv_sock *iucv = iucv_sk(sk);
149 int err;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700150 unsigned long timeo;
Jennifer Hunteac37312007-02-08 13:51:54 -0800151
152 iucv_sock_clear_timer(sk);
153 lock_sock(sk);
154
155 switch(sk->sk_state) {
156 case IUCV_LISTEN:
157 iucv_sock_cleanup_listen(sk);
158 break;
159
160 case IUCV_CONNECTED:
161 case IUCV_DISCONN:
162 err = 0;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700163
164 sk->sk_state = IUCV_CLOSING;
165 sk->sk_state_change(sk);
166
167 if(!skb_queue_empty(&iucv->send_skb_q)) {
168 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
169 timeo = sk->sk_lingertime;
170 else
171 timeo = IUCV_DISCONN_TIMEOUT;
172 err = iucv_sock_wait_state(sk, IUCV_CLOSED, 0, timeo);
173 }
174
175 sk->sk_state = IUCV_CLOSED;
176 sk->sk_state_change(sk);
177
Jennifer Hunteac37312007-02-08 13:51:54 -0800178 if (iucv->path) {
179 low_nmcpy(user_data, iucv->src_name);
180 high_nmcpy(user_data, iucv->dst_name);
181 ASCEBC(user_data, sizeof(user_data));
182 err = iucv_path_sever(iucv->path, user_data);
183 iucv_path_free(iucv->path);
184 iucv->path = NULL;
185 }
186
Jennifer Hunteac37312007-02-08 13:51:54 -0800187 sk->sk_err = ECONNRESET;
188 sk->sk_state_change(sk);
189
190 skb_queue_purge(&iucv->send_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700191 skb_queue_purge(&iucv->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800192
193 sock_set_flag(sk, SOCK_ZAPPED);
194 break;
195
196 default:
197 sock_set_flag(sk, SOCK_ZAPPED);
198 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700199 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800200
201 release_sock(sk);
202 iucv_sock_kill(sk);
203}
204
205static void iucv_sock_init(struct sock *sk, struct sock *parent)
206{
207 if (parent)
208 sk->sk_type = parent->sk_type;
209}
210
211static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
212{
213 struct sock *sk;
214
215 sk = sk_alloc(PF_IUCV, prio, &iucv_proto, 1);
216 if (!sk)
217 return NULL;
218
219 sock_init_data(sock, sk);
220 INIT_LIST_HEAD(&iucv_sk(sk)->accept_q);
221 skb_queue_head_init(&iucv_sk(sk)->send_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700222 skb_queue_head_init(&iucv_sk(sk)->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800223 iucv_sk(sk)->send_tag = 0;
224
225 sk->sk_destruct = iucv_sock_destruct;
226 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
227 sk->sk_allocation = GFP_DMA;
228
229 sock_reset_flag(sk, SOCK_ZAPPED);
230
231 sk->sk_protocol = proto;
232 sk->sk_state = IUCV_OPEN;
233
234 iucv_sock_init_timer(sk);
235
236 iucv_sock_link(&iucv_sk_list, sk);
237 return sk;
238}
239
240/* Create an IUCV socket */
241static int iucv_sock_create(struct socket *sock, int protocol)
242{
243 struct sock *sk;
244
245 if (sock->type != SOCK_STREAM)
246 return -ESOCKTNOSUPPORT;
247
248 sock->state = SS_UNCONNECTED;
249 sock->ops = &iucv_sock_ops;
250
251 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
252 if (!sk)
253 return -ENOMEM;
254
255 iucv_sock_init(sk, NULL);
256
257 return 0;
258}
259
260void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
261{
262 write_lock_bh(&l->lock);
263 sk_add_node(sk, &l->head);
264 write_unlock_bh(&l->lock);
265}
266
267void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
268{
269 write_lock_bh(&l->lock);
270 sk_del_node_init(sk);
271 write_unlock_bh(&l->lock);
272}
273
274void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
275{
276 sock_hold(sk);
277 list_add_tail(&iucv_sk(sk)->accept_q, &iucv_sk(parent)->accept_q);
278 iucv_sk(sk)->parent = parent;
279 parent->sk_ack_backlog++;
280}
281
282void iucv_accept_unlink(struct sock *sk)
283{
284 list_del_init(&iucv_sk(sk)->accept_q);
285 iucv_sk(sk)->parent->sk_ack_backlog--;
286 iucv_sk(sk)->parent = NULL;
287 sock_put(sk);
288}
289
290struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
291{
292 struct iucv_sock *isk, *n;
293 struct sock *sk;
294
295 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q){
296 sk = (struct sock *) isk;
297 lock_sock(sk);
298
299 if (sk->sk_state == IUCV_CLOSED) {
300 release_sock(sk);
301 iucv_accept_unlink(sk);
302 continue;
303 }
304
305 if (sk->sk_state == IUCV_CONNECTED ||
306 sk->sk_state == IUCV_SEVERED ||
307 !newsock) {
308 iucv_accept_unlink(sk);
309 if (newsock)
310 sock_graft(sk, newsock);
311
312 if (sk->sk_state == IUCV_SEVERED)
313 sk->sk_state = IUCV_DISCONN;
314
315 release_sock(sk);
316 return sk;
317 }
318
319 release_sock(sk);
320 }
321 return NULL;
322}
323
324int iucv_sock_wait_state(struct sock *sk, int state, int state2,
325 unsigned long timeo)
326{
327 DECLARE_WAITQUEUE(wait, current);
328 int err = 0;
329
330 add_wait_queue(sk->sk_sleep, &wait);
331 while (sk->sk_state != state && sk->sk_state != state2) {
332 set_current_state(TASK_INTERRUPTIBLE);
333
334 if (!timeo) {
335 err = -EAGAIN;
336 break;
337 }
338
339 if (signal_pending(current)) {
340 err = sock_intr_errno(timeo);
341 break;
342 }
343
344 release_sock(sk);
345 timeo = schedule_timeout(timeo);
346 lock_sock(sk);
347
348 err = sock_error(sk);
349 if (err)
350 break;
351 }
352 set_current_state(TASK_RUNNING);
353 remove_wait_queue(sk->sk_sleep, &wait);
354 return err;
355}
356
357/* Bind an unbound socket */
358static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
359 int addr_len)
360{
361 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
362 struct sock *sk = sock->sk;
363 struct iucv_sock *iucv;
364 int err;
365
366 /* Verify the input sockaddr */
367 if (!addr || addr->sa_family != AF_IUCV)
368 return -EINVAL;
369
370 lock_sock(sk);
371 if (sk->sk_state != IUCV_OPEN) {
372 err = -EBADFD;
373 goto done;
374 }
375
376 write_lock_bh(&iucv_sk_list.lock);
377
378 iucv = iucv_sk(sk);
379 if (__iucv_get_sock_by_name(sa->siucv_name)) {
380 err = -EADDRINUSE;
381 goto done_unlock;
382 }
383 if (iucv->path) {
384 err = 0;
385 goto done_unlock;
386 }
387
388 /* Bind the socket */
389 memcpy(iucv->src_name, sa->siucv_name, 8);
390
391 /* Copy the user id */
392 memcpy(iucv->src_user_id, iucv_userid, 8);
393 sk->sk_state = IUCV_BOUND;
394 err = 0;
395
396done_unlock:
397 /* Release the socket list lock */
398 write_unlock_bh(&iucv_sk_list.lock);
399done:
400 release_sock(sk);
401 return err;
402}
403
404/* Automatically bind an unbound socket */
405static int iucv_sock_autobind(struct sock *sk)
406{
407 struct iucv_sock *iucv = iucv_sk(sk);
408 char query_buffer[80];
409 char name[12];
410 int err = 0;
411
412 /* Set the userid and name */
413 cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err);
414 if (unlikely(err))
415 return -EPROTO;
416
417 memcpy(iucv->src_user_id, query_buffer, 8);
418
419 write_lock_bh(&iucv_sk_list.lock);
420
421 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
422 while (__iucv_get_sock_by_name(name)) {
423 sprintf(name, "%08x",
424 atomic_inc_return(&iucv_sk_list.autobind_name));
425 }
426
427 write_unlock_bh(&iucv_sk_list.lock);
428
429 memcpy(&iucv->src_name, name, 8);
430
431 return err;
432}
433
434/* Connect an unconnected socket */
435static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
436 int alen, int flags)
437{
438 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
439 struct sock *sk = sock->sk;
440 struct iucv_sock *iucv;
441 unsigned char user_data[16];
442 int err;
443
444 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
445 return -EINVAL;
446
447 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
448 return -EBADFD;
449
450 if (sk->sk_type != SOCK_STREAM)
451 return -EINVAL;
452
453 iucv = iucv_sk(sk);
454
455 if (sk->sk_state == IUCV_OPEN) {
456 err = iucv_sock_autobind(sk);
457 if (unlikely(err))
458 return err;
459 }
460
461 lock_sock(sk);
462
463 /* Set the destination information */
464 memcpy(iucv_sk(sk)->dst_user_id, sa->siucv_user_id, 8);
465 memcpy(iucv_sk(sk)->dst_name, sa->siucv_name, 8);
466
467 high_nmcpy(user_data, sa->siucv_name);
468 low_nmcpy(user_data, iucv_sk(sk)->src_name);
469 ASCEBC(user_data, sizeof(user_data));
470
471 iucv = iucv_sk(sk);
472 /* Create path. */
473 iucv->path = iucv_path_alloc(IUCV_QUEUELEN_DEFAULT,
474 IPRMDATA, GFP_KERNEL);
475 err = iucv_path_connect(iucv->path, &af_iucv_handler,
476 sa->siucv_user_id, NULL, user_data, sk);
477 if (err) {
478 iucv_path_free(iucv->path);
479 iucv->path = NULL;
480 err = -ECONNREFUSED;
481 goto done;
482 }
483
484 if (sk->sk_state != IUCV_CONNECTED) {
485 err = iucv_sock_wait_state(sk, IUCV_CONNECTED, IUCV_DISCONN,
486 sock_sndtimeo(sk, flags & O_NONBLOCK));
487 }
488
489 if (sk->sk_state == IUCV_DISCONN) {
490 release_sock(sk);
491 return -ECONNREFUSED;
492 }
493done:
494 release_sock(sk);
495 return err;
496}
497
498/* Move a socket into listening state. */
499static int iucv_sock_listen(struct socket *sock, int backlog)
500{
501 struct sock *sk = sock->sk;
502 int err;
503
504 lock_sock(sk);
505
506 err = -EINVAL;
507 if (sk->sk_state != IUCV_BOUND || sock->type != SOCK_STREAM)
508 goto done;
509
510 sk->sk_max_ack_backlog = backlog;
511 sk->sk_ack_backlog = 0;
512 sk->sk_state = IUCV_LISTEN;
513 err = 0;
514
515done:
516 release_sock(sk);
517 return err;
518}
519
520/* Accept a pending connection */
521static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
522 int flags)
523{
524 DECLARE_WAITQUEUE(wait, current);
525 struct sock *sk = sock->sk, *nsk;
526 long timeo;
527 int err = 0;
528
Jennifer Hunt561e0362007-05-04 12:22:07 -0700529 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800530
531 if (sk->sk_state != IUCV_LISTEN) {
532 err = -EBADFD;
533 goto done;
534 }
535
536 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
537
538 /* Wait for an incoming connection */
539 add_wait_queue_exclusive(sk->sk_sleep, &wait);
540 while (!(nsk = iucv_accept_dequeue(sk, newsock))){
541 set_current_state(TASK_INTERRUPTIBLE);
542 if (!timeo) {
543 err = -EAGAIN;
544 break;
545 }
546
547 release_sock(sk);
548 timeo = schedule_timeout(timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700549 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800550
551 if (sk->sk_state != IUCV_LISTEN) {
552 err = -EBADFD;
553 break;
554 }
555
556 if (signal_pending(current)) {
557 err = sock_intr_errno(timeo);
558 break;
559 }
560 }
561
562 set_current_state(TASK_RUNNING);
563 remove_wait_queue(sk->sk_sleep, &wait);
564
565 if (err)
566 goto done;
567
568 newsock->state = SS_CONNECTED;
569
570done:
571 release_sock(sk);
572 return err;
573}
574
575static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
576 int *len, int peer)
577{
578 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
579 struct sock *sk = sock->sk;
580
581 addr->sa_family = AF_IUCV;
582 *len = sizeof(struct sockaddr_iucv);
583
584 if (peer) {
585 memcpy(siucv->siucv_user_id, iucv_sk(sk)->dst_user_id, 8);
586 memcpy(siucv->siucv_name, &iucv_sk(sk)->dst_name, 8);
587 } else {
588 memcpy(siucv->siucv_user_id, iucv_sk(sk)->src_user_id, 8);
589 memcpy(siucv->siucv_name, iucv_sk(sk)->src_name, 8);
590 }
591 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
592 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
593 memset(siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
594
595 return 0;
596}
597
598static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
599 struct msghdr *msg, size_t len)
600{
601 struct sock *sk = sock->sk;
602 struct iucv_sock *iucv = iucv_sk(sk);
603 struct sk_buff *skb;
604 struct iucv_message txmsg;
605 int err;
606
607 err = sock_error(sk);
608 if (err)
609 return err;
610
611 if (msg->msg_flags & MSG_OOB)
612 return -EOPNOTSUPP;
613
614 lock_sock(sk);
615
616 if (sk->sk_shutdown & SEND_SHUTDOWN) {
617 err = -EPIPE;
618 goto out;
619 }
620
621 if (sk->sk_state == IUCV_CONNECTED){
622 if(!(skb = sock_alloc_send_skb(sk, len,
623 msg->msg_flags & MSG_DONTWAIT,
624 &err)))
Jennifer Hunt561e0362007-05-04 12:22:07 -0700625 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -0800626
627 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)){
628 err = -EFAULT;
629 goto fail;
630 }
631
632 txmsg.class = 0;
633 txmsg.tag = iucv->send_tag++;
634 memcpy(skb->cb, &txmsg.tag, 4);
635 skb_queue_tail(&iucv->send_skb_q, skb);
636 err = iucv_message_send(iucv->path, &txmsg, 0, 0,
637 (void *) skb->data, skb->len);
638 if (err) {
639 if (err == 3)
640 printk(KERN_ERR "AF_IUCV msg limit exceeded\n");
641 skb_unlink(skb, &iucv->send_skb_q);
642 err = -EPIPE;
643 goto fail;
644 }
645
646 } else {
647 err = -ENOTCONN;
648 goto out;
649 }
650
651 release_sock(sk);
652 return len;
653
654fail:
655 kfree_skb(skb);
656out:
657 release_sock(sk);
658 return err;
659}
660
661static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
662 struct msghdr *msg, size_t len, int flags)
663{
664 int noblock = flags & MSG_DONTWAIT;
665 struct sock *sk = sock->sk;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700666 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800667 int target, copied = 0;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700668 struct sk_buff *skb, *rskb, *cskb;
Jennifer Hunteac37312007-02-08 13:51:54 -0800669 int err = 0;
670
Jennifer Hunt561e0362007-05-04 12:22:07 -0700671 if ((sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED) &&
672 skb_queue_empty(&iucv->backlog_skb_q) &&
673 skb_queue_empty(&sk->sk_receive_queue))
674 return 0;
675
Jennifer Hunteac37312007-02-08 13:51:54 -0800676 if (flags & (MSG_OOB))
677 return -EOPNOTSUPP;
678
679 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
680
681 skb = skb_recv_datagram(sk, flags, noblock, &err);
682 if (!skb) {
683 if (sk->sk_shutdown & RCV_SHUTDOWN)
684 return 0;
685 return err;
686 }
687
688 copied = min_t(unsigned int, skb->len, len);
689
Jennifer Hunt561e0362007-05-04 12:22:07 -0700690 cskb = skb;
691 if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800692 skb_queue_head(&sk->sk_receive_queue, skb);
693 if (copied == 0)
694 return -EFAULT;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700695 goto done;
Jennifer Hunteac37312007-02-08 13:51:54 -0800696 }
697
698 len -= copied;
699
700 /* Mark read part of skb as used */
701 if (!(flags & MSG_PEEK)) {
702 skb_pull(skb, copied);
703
704 if (skb->len) {
705 skb_queue_head(&sk->sk_receive_queue, skb);
706 goto done;
707 }
708
709 kfree_skb(skb);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700710
711 /* Queue backlog skbs */
712 rskb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
713 while(rskb) {
714 if (sock_queue_rcv_skb(sk, rskb)) {
715 skb_queue_head(&iucv_sk(sk)->backlog_skb_q,
716 rskb);
717 break;
718 } else {
719 rskb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
720 }
721 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800722 } else
723 skb_queue_head(&sk->sk_receive_queue, skb);
724
725done:
726 return err ? : copied;
727}
728
729static inline unsigned int iucv_accept_poll(struct sock *parent)
730{
731 struct iucv_sock *isk, *n;
732 struct sock *sk;
733
734 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q){
735 sk = (struct sock *) isk;
736
737 if (sk->sk_state == IUCV_CONNECTED)
738 return POLLIN | POLLRDNORM;
739 }
740
741 return 0;
742}
743
744unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
745 poll_table *wait)
746{
747 struct sock *sk = sock->sk;
748 unsigned int mask = 0;
749
750 poll_wait(file, sk->sk_sleep, wait);
751
752 if (sk->sk_state == IUCV_LISTEN)
753 return iucv_accept_poll(sk);
754
755 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
756 mask |= POLLERR;
757
758 if (sk->sk_shutdown & RCV_SHUTDOWN)
759 mask |= POLLRDHUP;
760
761 if (sk->sk_shutdown == SHUTDOWN_MASK)
762 mask |= POLLHUP;
763
764 if (!skb_queue_empty(&sk->sk_receive_queue) ||
765 (sk->sk_shutdown & RCV_SHUTDOWN))
766 mask |= POLLIN | POLLRDNORM;
767
768 if (sk->sk_state == IUCV_CLOSED)
769 mask |= POLLHUP;
770
Jennifer Hunt561e0362007-05-04 12:22:07 -0700771 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED)
772 mask |= POLLIN;
773
Jennifer Hunteac37312007-02-08 13:51:54 -0800774 if (sock_writeable(sk))
775 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
776 else
777 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
778
779 return mask;
780}
781
782static int iucv_sock_shutdown(struct socket *sock, int how)
783{
784 struct sock *sk = sock->sk;
785 struct iucv_sock *iucv = iucv_sk(sk);
786 struct iucv_message txmsg;
787 int err = 0;
788 u8 prmmsg[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
789
790 how++;
791
792 if ((how & ~SHUTDOWN_MASK) || !how)
793 return -EINVAL;
794
795 lock_sock(sk);
796 switch(sk->sk_state) {
797 case IUCV_CLOSED:
798 err = -ENOTCONN;
799 goto fail;
800
801 default:
802 sk->sk_shutdown |= how;
803 break;
804 }
805
806 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
807 txmsg.class = 0;
808 txmsg.tag = 0;
809 err = iucv_message_send(iucv->path, &txmsg, IUCV_IPRMDATA, 0,
810 (void *) prmmsg, 8);
811 if (err) {
812 switch(err) {
813 case 1:
814 err = -ENOTCONN;
815 break;
816 case 2:
817 err = -ECONNRESET;
818 break;
819 default:
820 err = -ENOTCONN;
821 break;
822 }
823 }
824 }
825
826 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
827 err = iucv_path_quiesce(iucv_sk(sk)->path, NULL);
828 if (err)
829 err = -ENOTCONN;
830
831 skb_queue_purge(&sk->sk_receive_queue);
832 }
833
834 /* Wake up anyone sleeping in poll */
835 sk->sk_state_change(sk);
836
837fail:
838 release_sock(sk);
839 return err;
840}
841
842static int iucv_sock_release(struct socket *sock)
843{
844 struct sock *sk = sock->sk;
845 int err = 0;
846
847 if (!sk)
848 return 0;
849
850 iucv_sock_close(sk);
851
852 /* Unregister with IUCV base support */
853 if (iucv_sk(sk)->path) {
854 iucv_path_sever(iucv_sk(sk)->path, NULL);
855 iucv_path_free(iucv_sk(sk)->path);
856 iucv_sk(sk)->path = NULL;
857 }
858
Jennifer Hunteac37312007-02-08 13:51:54 -0800859 sock_orphan(sk);
860 iucv_sock_kill(sk);
861 return err;
862}
863
864/* Callback wrappers - called from iucv base support */
865static int iucv_callback_connreq(struct iucv_path *path,
866 u8 ipvmid[8], u8 ipuser[16])
867{
868 unsigned char user_data[16];
869 unsigned char nuser_data[16];
870 unsigned char src_name[8];
871 struct hlist_node *node;
872 struct sock *sk, *nsk;
873 struct iucv_sock *iucv, *niucv;
874 int err;
875
876 memcpy(src_name, ipuser, 8);
877 EBCASC(src_name, 8);
878 /* Find out if this path belongs to af_iucv. */
879 read_lock(&iucv_sk_list.lock);
880 iucv = NULL;
881 sk_for_each(sk, node, &iucv_sk_list.head)
882 if (sk->sk_state == IUCV_LISTEN &&
883 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
884 /*
885 * Found a listening socket with
886 * src_name == ipuser[0-7].
887 */
888 iucv = iucv_sk(sk);
889 break;
890 }
891 read_unlock(&iucv_sk_list.lock);
892 if (!iucv)
893 /* No socket found, not one of our paths. */
894 return -EINVAL;
895
896 bh_lock_sock(sk);
897
898 /* Check if parent socket is listening */
899 low_nmcpy(user_data, iucv->src_name);
900 high_nmcpy(user_data, iucv->dst_name);
901 ASCEBC(user_data, sizeof(user_data));
902 if (sk->sk_state != IUCV_LISTEN) {
903 err = iucv_path_sever(path, user_data);
904 goto fail;
905 }
906
907 /* Check for backlog size */
908 if (sk_acceptq_is_full(sk)) {
909 err = iucv_path_sever(path, user_data);
910 goto fail;
911 }
912
913 /* Create the new socket */
914 nsk = iucv_sock_alloc(NULL, SOCK_STREAM, GFP_ATOMIC);
915 if (!nsk){
916 err = iucv_path_sever(path, user_data);
917 goto fail;
918 }
919
920 niucv = iucv_sk(nsk);
921 iucv_sock_init(nsk, sk);
922
923 /* Set the new iucv_sock */
924 memcpy(niucv->dst_name, ipuser + 8, 8);
925 EBCASC(niucv->dst_name, 8);
926 memcpy(niucv->dst_user_id, ipvmid, 8);
927 memcpy(niucv->src_name, iucv->src_name, 8);
928 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
929 niucv->path = path;
930
931 /* Call iucv_accept */
932 high_nmcpy(nuser_data, ipuser + 8);
933 memcpy(nuser_data + 8, niucv->src_name, 8);
934 ASCEBC(nuser_data + 8, 8);
935
936 path->msglim = IUCV_QUEUELEN_DEFAULT;
937 err = iucv_path_accept(path, &af_iucv_handler, nuser_data, nsk);
938 if (err){
939 err = iucv_path_sever(path, user_data);
940 goto fail;
941 }
942
943 iucv_accept_enqueue(sk, nsk);
944
945 /* Wake up accept */
946 nsk->sk_state = IUCV_CONNECTED;
947 sk->sk_data_ready(sk, 1);
948 err = 0;
949fail:
950 bh_unlock_sock(sk);
951 return 0;
952}
953
954static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
955{
956 struct sock *sk = path->private;
957
958 sk->sk_state = IUCV_CONNECTED;
959 sk->sk_state_change(sk);
960}
961
Jennifer Hunt561e0362007-05-04 12:22:07 -0700962static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len,
963 struct sk_buff_head fragmented_skb_q)
964{
965 int dataleft, size, copied = 0;
966 struct sk_buff *nskb;
967
968 dataleft = len;
969 while(dataleft) {
970 if (dataleft >= sk->sk_rcvbuf / 4)
971 size = sk->sk_rcvbuf / 4;
972 else
973 size = dataleft;
974
975 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
976 if (!nskb)
977 return -ENOMEM;
978
979 memcpy(nskb->data, skb->data + copied, size);
980 copied += size;
981 dataleft -= size;
982
983 nskb->h.raw = nskb->data;
984 nskb->nh.raw = nskb->data;
985 nskb->len = size;
986
987 skb_queue_tail(fragmented_skb_q, nskb);
988 }
989
990 return 0;
991}
Jennifer Hunteac37312007-02-08 13:51:54 -0800992static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
993{
994 struct sock *sk = path->private;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700995 struct iucv_sock *iucv = iucv_sk(sk);
996 struct sk_buff *skb, *fskb;
997 struct sk_buff_head fragmented_skb_q;
Jennifer Hunteac37312007-02-08 13:51:54 -0800998 int rc;
999
Jennifer Hunt561e0362007-05-04 12:22:07 -07001000 skb_queue_head_init(&fragmented_skb_q);
1001
Jennifer Hunteac37312007-02-08 13:51:54 -08001002 if (sk->sk_shutdown & RCV_SHUTDOWN)
1003 return;
1004
1005 skb = alloc_skb(msg->length, GFP_ATOMIC | GFP_DMA);
1006 if (!skb) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001007 iucv_path_sever(path, NULL);
Jennifer Hunteac37312007-02-08 13:51:54 -08001008 return;
1009 }
1010
1011 if (msg->flags & IPRMDATA) {
1012 skb->data = NULL;
1013 skb->len = 0;
1014 } else {
1015 rc = iucv_message_receive(path, msg, 0, skb->data,
1016 msg->length, NULL);
1017 if (rc) {
1018 kfree_skb(skb);
1019 return;
1020 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001021 if (skb->truesize >= sk->sk_rcvbuf / 4) {
1022 rc = iucv_fragment_skb(sk, skb, msg->length,
1023 &fragmented_skb_q);
1024 kfree_skb(skb);
1025 skb = NULL;
1026 if (rc) {
1027 iucv_path_sever(path, NULL);
1028 return;
1029 }
1030 } else {
1031 skb_reset_transport_header(skb);
1032 skb_reset_network_header(skb);
1033 skb->len = msg->length;
1034 }
1035 }
1036 /* Queue the fragmented skb */
1037 fskb = skb_dequeue(&fragmented_skb_q);
1038 while(fskb) {
1039 if (!skb_queue_empty(&iucv->backlog_skb_q))
1040 skb_queue_tail(&iucv->backlog_skb_q, fskb);
1041 else if (sock_queue_rcv_skb(sk, fskb))
1042 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, fskb);
1043 fskb = skb_dequeue(&fragmented_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -08001044 }
1045
Jennifer Hunt561e0362007-05-04 12:22:07 -07001046 /* Queue the original skb if it exists (was not fragmented) */
1047 if (skb) {
1048 if (!skb_queue_empty(&iucv->backlog_skb_q))
1049 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
1050 else if (sock_queue_rcv_skb(sk, skb))
1051 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
1052 }
1053
Jennifer Hunteac37312007-02-08 13:51:54 -08001054}
1055
1056static void iucv_callback_txdone(struct iucv_path *path,
1057 struct iucv_message *msg)
1058{
1059 struct sock *sk = path->private;
1060 struct sk_buff *this;
1061 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1062 struct sk_buff *list_skb = list->next;
1063 unsigned long flags;
1064
Jennifer Hunt561e0362007-05-04 12:22:07 -07001065 if (list_skb) {
1066 spin_lock_irqsave(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001067
Jennifer Hunt561e0362007-05-04 12:22:07 -07001068 do {
1069 this = list_skb;
1070 list_skb = list_skb->next;
1071 } while (memcmp(&msg->tag, this->cb, 4) && list_skb);
Jennifer Hunteac37312007-02-08 13:51:54 -08001072
Jennifer Hunt561e0362007-05-04 12:22:07 -07001073 spin_unlock_irqrestore(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001074
Jennifer Hunt561e0362007-05-04 12:22:07 -07001075 skb_unlink(this, &iucv_sk(sk)->send_skb_q);
1076 kfree_skb(this);
1077 }
1078
1079 if (sk->sk_state == IUCV_CLOSING){
1080 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1081 sk->sk_state = IUCV_CLOSED;
1082 sk->sk_state_change(sk);
1083 }
1084 }
1085
Jennifer Hunteac37312007-02-08 13:51:54 -08001086}
1087
1088static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1089{
1090 struct sock *sk = path->private;
1091
1092 if (!list_empty(&iucv_sk(sk)->accept_q))
1093 sk->sk_state = IUCV_SEVERED;
1094 else
1095 sk->sk_state = IUCV_DISCONN;
1096
1097 sk->sk_state_change(sk);
1098}
1099
1100static struct proto_ops iucv_sock_ops = {
1101 .family = PF_IUCV,
1102 .owner = THIS_MODULE,
1103 .release = iucv_sock_release,
1104 .bind = iucv_sock_bind,
1105 .connect = iucv_sock_connect,
1106 .listen = iucv_sock_listen,
1107 .accept = iucv_sock_accept,
1108 .getname = iucv_sock_getname,
1109 .sendmsg = iucv_sock_sendmsg,
1110 .recvmsg = iucv_sock_recvmsg,
1111 .poll = iucv_sock_poll,
1112 .ioctl = sock_no_ioctl,
1113 .mmap = sock_no_mmap,
1114 .socketpair = sock_no_socketpair,
1115 .shutdown = iucv_sock_shutdown,
1116 .setsockopt = sock_no_setsockopt,
1117 .getsockopt = sock_no_getsockopt
1118};
1119
1120static struct net_proto_family iucv_sock_family_ops = {
1121 .family = AF_IUCV,
1122 .owner = THIS_MODULE,
1123 .create = iucv_sock_create,
1124};
1125
1126static int afiucv_init(void)
1127{
1128 int err;
1129
1130 if (!MACHINE_IS_VM) {
1131 printk(KERN_ERR "AF_IUCV connection needs VM as base\n");
1132 err = -EPROTONOSUPPORT;
1133 goto out;
1134 }
1135 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
1136 if (unlikely(err)) {
1137 printk(KERN_ERR "AF_IUCV needs the VM userid\n");
1138 err = -EPROTONOSUPPORT;
1139 goto out;
1140 }
1141
1142 err = iucv_register(&af_iucv_handler, 0);
1143 if (err)
1144 goto out;
1145 err = proto_register(&iucv_proto, 0);
1146 if (err)
1147 goto out_iucv;
1148 err = sock_register(&iucv_sock_family_ops);
1149 if (err)
1150 goto out_proto;
1151 printk(KERN_INFO "AF_IUCV lowlevel driver initialized\n");
1152 return 0;
1153
1154out_proto:
1155 proto_unregister(&iucv_proto);
1156out_iucv:
1157 iucv_unregister(&af_iucv_handler, 0);
1158out:
1159 return err;
1160}
1161
1162static void __exit afiucv_exit(void)
1163{
1164 sock_unregister(PF_IUCV);
1165 proto_unregister(&iucv_proto);
1166 iucv_unregister(&af_iucv_handler, 0);
1167
1168 printk(KERN_INFO "AF_IUCV lowlevel driver unloaded\n");
1169}
1170
1171module_init(afiucv_init);
1172module_exit(afiucv_exit);
1173
1174MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
1175MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
1176MODULE_VERSION(VERSION);
1177MODULE_LICENSE("GPL");
1178MODULE_ALIAS_NETPROTO(PF_IUCV);