Merge branches 'tracing/docs', 'tracing/function-graph-tracer' and 'linus' into traci...
[linux-2.6] / net / iucv / af_iucv.c
1 /*
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 #define KMSG_COMPONENT "af_iucv"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
14 #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
32 #define CONFIG_IUCV_SOCK_DEBUG 1
33
34 #define IPRMDATA 0x80
35 #define VERSION "1.0"
36
37 static char iucv_userid[80];
38
39 static struct proto_ops iucv_sock_ops;
40
41 static struct proto iucv_proto = {
42         .name           = "AF_IUCV",
43         .owner          = THIS_MODULE,
44         .obj_size       = sizeof(struct iucv_sock),
45 };
46
47 static void iucv_sock_kill(struct sock *sk);
48 static void iucv_sock_close(struct sock *sk);
49
50 /* Call Back functions */
51 static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
52 static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
53 static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
54 static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8],
55                                  u8 ipuser[16]);
56 static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
57
58 static struct iucv_sock_list iucv_sk_list = {
59         .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
60         .autobind_name = ATOMIC_INIT(0)
61 };
62
63 static struct iucv_handler af_iucv_handler = {
64         .path_pending     = iucv_callback_connreq,
65         .path_complete    = iucv_callback_connack,
66         .path_severed     = iucv_callback_connrej,
67         .message_pending  = iucv_callback_rx,
68         .message_complete = iucv_callback_txdone
69 };
70
71 static inline void high_nmcpy(unsigned char *dst, char *src)
72 {
73        memcpy(dst, src, 8);
74 }
75
76 static inline void low_nmcpy(unsigned char *dst, char *src)
77 {
78        memcpy(&dst[8], src, 8);
79 }
80
81 /* Timers */
82 static void iucv_sock_timeout(unsigned long arg)
83 {
84         struct sock *sk = (struct sock *)arg;
85
86         bh_lock_sock(sk);
87         sk->sk_err = ETIMEDOUT;
88         sk->sk_state_change(sk);
89         bh_unlock_sock(sk);
90
91         iucv_sock_kill(sk);
92         sock_put(sk);
93 }
94
95 static void iucv_sock_clear_timer(struct sock *sk)
96 {
97         sk_stop_timer(sk, &sk->sk_timer);
98 }
99
100 static 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
112 static 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 */
119 static 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 */
134 static 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 */
145 static 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;
150         unsigned long timeo;
151
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;
163
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
178                 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
187                 sk->sk_err = ECONNRESET;
188                 sk->sk_state_change(sk);
189
190                 skb_queue_purge(&iucv->send_skb_q);
191                 skb_queue_purge(&iucv->backlog_skb_q);
192
193                 sock_set_flag(sk, SOCK_ZAPPED);
194                 break;
195
196         default:
197                 sock_set_flag(sk, SOCK_ZAPPED);
198                 break;
199         }
200
201         release_sock(sk);
202         iucv_sock_kill(sk);
203 }
204
205 static void iucv_sock_init(struct sock *sk, struct sock *parent)
206 {
207         if (parent)
208                 sk->sk_type = parent->sk_type;
209 }
210
211 static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
212 {
213         struct sock *sk;
214
215         sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
216         if (!sk)
217                 return NULL;
218
219         sock_init_data(sock, sk);
220         INIT_LIST_HEAD(&iucv_sk(sk)->accept_q);
221         spin_lock_init(&iucv_sk(sk)->accept_q_lock);
222         skb_queue_head_init(&iucv_sk(sk)->send_skb_q);
223         INIT_LIST_HEAD(&iucv_sk(sk)->message_q.list);
224         spin_lock_init(&iucv_sk(sk)->message_q.lock);
225         skb_queue_head_init(&iucv_sk(sk)->backlog_skb_q);
226         iucv_sk(sk)->send_tag = 0;
227
228         sk->sk_destruct = iucv_sock_destruct;
229         sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
230         sk->sk_allocation = GFP_DMA;
231
232         sock_reset_flag(sk, SOCK_ZAPPED);
233
234         sk->sk_protocol = proto;
235         sk->sk_state    = IUCV_OPEN;
236
237         setup_timer(&sk->sk_timer, iucv_sock_timeout, (unsigned long)sk);
238
239         iucv_sock_link(&iucv_sk_list, sk);
240         return sk;
241 }
242
243 /* Create an IUCV socket */
244 static int iucv_sock_create(struct net *net, struct socket *sock, int protocol)
245 {
246         struct sock *sk;
247
248         if (sock->type != SOCK_STREAM)
249                 return -ESOCKTNOSUPPORT;
250
251         sock->state = SS_UNCONNECTED;
252         sock->ops = &iucv_sock_ops;
253
254         sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
255         if (!sk)
256                 return -ENOMEM;
257
258         iucv_sock_init(sk, NULL);
259
260         return 0;
261 }
262
263 void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
264 {
265         write_lock_bh(&l->lock);
266         sk_add_node(sk, &l->head);
267         write_unlock_bh(&l->lock);
268 }
269
270 void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
271 {
272         write_lock_bh(&l->lock);
273         sk_del_node_init(sk);
274         write_unlock_bh(&l->lock);
275 }
276
277 void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
278 {
279         unsigned long flags;
280         struct iucv_sock *par = iucv_sk(parent);
281
282         sock_hold(sk);
283         spin_lock_irqsave(&par->accept_q_lock, flags);
284         list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
285         spin_unlock_irqrestore(&par->accept_q_lock, flags);
286         iucv_sk(sk)->parent = parent;
287         parent->sk_ack_backlog++;
288 }
289
290 void iucv_accept_unlink(struct sock *sk)
291 {
292         unsigned long flags;
293         struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
294
295         spin_lock_irqsave(&par->accept_q_lock, flags);
296         list_del_init(&iucv_sk(sk)->accept_q);
297         spin_unlock_irqrestore(&par->accept_q_lock, flags);
298         iucv_sk(sk)->parent->sk_ack_backlog--;
299         iucv_sk(sk)->parent = NULL;
300         sock_put(sk);
301 }
302
303 struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
304 {
305         struct iucv_sock *isk, *n;
306         struct sock *sk;
307
308         list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
309                 sk = (struct sock *) isk;
310                 lock_sock(sk);
311
312                 if (sk->sk_state == IUCV_CLOSED) {
313                         iucv_accept_unlink(sk);
314                         release_sock(sk);
315                         continue;
316                 }
317
318                 if (sk->sk_state == IUCV_CONNECTED ||
319                     sk->sk_state == IUCV_SEVERED ||
320                     !newsock) {
321                         iucv_accept_unlink(sk);
322                         if (newsock)
323                                 sock_graft(sk, newsock);
324
325                         if (sk->sk_state == IUCV_SEVERED)
326                                 sk->sk_state = IUCV_DISCONN;
327
328                         release_sock(sk);
329                         return sk;
330                 }
331
332                 release_sock(sk);
333         }
334         return NULL;
335 }
336
337 int iucv_sock_wait_state(struct sock *sk, int state, int state2,
338                          unsigned long timeo)
339 {
340         DECLARE_WAITQUEUE(wait, current);
341         int err = 0;
342
343         add_wait_queue(sk->sk_sleep, &wait);
344         while (sk->sk_state != state && sk->sk_state != state2) {
345                 set_current_state(TASK_INTERRUPTIBLE);
346
347                 if (!timeo) {
348                         err = -EAGAIN;
349                         break;
350                 }
351
352                 if (signal_pending(current)) {
353                         err = sock_intr_errno(timeo);
354                         break;
355                 }
356
357                 release_sock(sk);
358                 timeo = schedule_timeout(timeo);
359                 lock_sock(sk);
360
361                 err = sock_error(sk);
362                 if (err)
363                         break;
364         }
365         set_current_state(TASK_RUNNING);
366         remove_wait_queue(sk->sk_sleep, &wait);
367         return err;
368 }
369
370 /* Bind an unbound socket */
371 static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
372                           int addr_len)
373 {
374         struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
375         struct sock *sk = sock->sk;
376         struct iucv_sock *iucv;
377         int err;
378
379         /* Verify the input sockaddr */
380         if (!addr || addr->sa_family != AF_IUCV)
381                 return -EINVAL;
382
383         lock_sock(sk);
384         if (sk->sk_state != IUCV_OPEN) {
385                 err = -EBADFD;
386                 goto done;
387         }
388
389         write_lock_bh(&iucv_sk_list.lock);
390
391         iucv = iucv_sk(sk);
392         if (__iucv_get_sock_by_name(sa->siucv_name)) {
393                 err = -EADDRINUSE;
394                 goto done_unlock;
395         }
396         if (iucv->path) {
397                 err = 0;
398                 goto done_unlock;
399         }
400
401         /* Bind the socket */
402         memcpy(iucv->src_name, sa->siucv_name, 8);
403
404         /* Copy the user id */
405         memcpy(iucv->src_user_id, iucv_userid, 8);
406         sk->sk_state = IUCV_BOUND;
407         err = 0;
408
409 done_unlock:
410         /* Release the socket list lock */
411         write_unlock_bh(&iucv_sk_list.lock);
412 done:
413         release_sock(sk);
414         return err;
415 }
416
417 /* Automatically bind an unbound socket */
418 static int iucv_sock_autobind(struct sock *sk)
419 {
420         struct iucv_sock *iucv = iucv_sk(sk);
421         char query_buffer[80];
422         char name[12];
423         int err = 0;
424
425         /* Set the userid and name */
426         cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err);
427         if (unlikely(err))
428                 return -EPROTO;
429
430         memcpy(iucv->src_user_id, query_buffer, 8);
431
432         write_lock_bh(&iucv_sk_list.lock);
433
434         sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
435         while (__iucv_get_sock_by_name(name)) {
436                 sprintf(name, "%08x",
437                         atomic_inc_return(&iucv_sk_list.autobind_name));
438         }
439
440         write_unlock_bh(&iucv_sk_list.lock);
441
442         memcpy(&iucv->src_name, name, 8);
443
444         return err;
445 }
446
447 /* Connect an unconnected socket */
448 static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
449                              int alen, int flags)
450 {
451         struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
452         struct sock *sk = sock->sk;
453         struct iucv_sock *iucv;
454         unsigned char user_data[16];
455         int err;
456
457         if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
458                 return -EINVAL;
459
460         if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
461                 return -EBADFD;
462
463         if (sk->sk_type != SOCK_STREAM)
464                 return -EINVAL;
465
466         iucv = iucv_sk(sk);
467
468         if (sk->sk_state == IUCV_OPEN) {
469                 err = iucv_sock_autobind(sk);
470                 if (unlikely(err))
471                         return err;
472         }
473
474         lock_sock(sk);
475
476         /* Set the destination information */
477         memcpy(iucv_sk(sk)->dst_user_id, sa->siucv_user_id, 8);
478         memcpy(iucv_sk(sk)->dst_name, sa->siucv_name, 8);
479
480         high_nmcpy(user_data, sa->siucv_name);
481         low_nmcpy(user_data, iucv_sk(sk)->src_name);
482         ASCEBC(user_data, sizeof(user_data));
483
484         iucv = iucv_sk(sk);
485         /* Create path. */
486         iucv->path = iucv_path_alloc(IUCV_QUEUELEN_DEFAULT,
487                                      IPRMDATA, GFP_KERNEL);
488         if (!iucv->path) {
489                 err = -ENOMEM;
490                 goto done;
491         }
492         err = iucv_path_connect(iucv->path, &af_iucv_handler,
493                                 sa->siucv_user_id, NULL, user_data, sk);
494         if (err) {
495                 iucv_path_free(iucv->path);
496                 iucv->path = NULL;
497                 err = -ECONNREFUSED;
498                 goto done;
499         }
500
501         if (sk->sk_state != IUCV_CONNECTED) {
502                 err = iucv_sock_wait_state(sk, IUCV_CONNECTED, IUCV_DISCONN,
503                                 sock_sndtimeo(sk, flags & O_NONBLOCK));
504         }
505
506         if (sk->sk_state == IUCV_DISCONN) {
507                 release_sock(sk);
508                 return -ECONNREFUSED;
509         }
510 done:
511         release_sock(sk);
512         return err;
513 }
514
515 /* Move a socket into listening state. */
516 static int iucv_sock_listen(struct socket *sock, int backlog)
517 {
518         struct sock *sk = sock->sk;
519         int err;
520
521         lock_sock(sk);
522
523         err = -EINVAL;
524         if (sk->sk_state != IUCV_BOUND || sock->type != SOCK_STREAM)
525                 goto done;
526
527         sk->sk_max_ack_backlog = backlog;
528         sk->sk_ack_backlog = 0;
529         sk->sk_state = IUCV_LISTEN;
530         err = 0;
531
532 done:
533         release_sock(sk);
534         return err;
535 }
536
537 /* Accept a pending connection */
538 static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
539                             int flags)
540 {
541         DECLARE_WAITQUEUE(wait, current);
542         struct sock *sk = sock->sk, *nsk;
543         long timeo;
544         int err = 0;
545
546         lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
547
548         if (sk->sk_state != IUCV_LISTEN) {
549                 err = -EBADFD;
550                 goto done;
551         }
552
553         timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
554
555         /* Wait for an incoming connection */
556         add_wait_queue_exclusive(sk->sk_sleep, &wait);
557         while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
558                 set_current_state(TASK_INTERRUPTIBLE);
559                 if (!timeo) {
560                         err = -EAGAIN;
561                         break;
562                 }
563
564                 release_sock(sk);
565                 timeo = schedule_timeout(timeo);
566                 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
567
568                 if (sk->sk_state != IUCV_LISTEN) {
569                         err = -EBADFD;
570                         break;
571                 }
572
573                 if (signal_pending(current)) {
574                         err = sock_intr_errno(timeo);
575                         break;
576                 }
577         }
578
579         set_current_state(TASK_RUNNING);
580         remove_wait_queue(sk->sk_sleep, &wait);
581
582         if (err)
583                 goto done;
584
585         newsock->state = SS_CONNECTED;
586
587 done:
588         release_sock(sk);
589         return err;
590 }
591
592 static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
593                              int *len, int peer)
594 {
595         struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
596         struct sock *sk = sock->sk;
597
598         addr->sa_family = AF_IUCV;
599         *len = sizeof(struct sockaddr_iucv);
600
601         if (peer) {
602                 memcpy(siucv->siucv_user_id, iucv_sk(sk)->dst_user_id, 8);
603                 memcpy(siucv->siucv_name, &iucv_sk(sk)->dst_name, 8);
604         } else {
605                 memcpy(siucv->siucv_user_id, iucv_sk(sk)->src_user_id, 8);
606                 memcpy(siucv->siucv_name, iucv_sk(sk)->src_name, 8);
607         }
608         memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
609         memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
610         memset(siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
611
612         return 0;
613 }
614
615 static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
616                              struct msghdr *msg, size_t len)
617 {
618         struct sock *sk = sock->sk;
619         struct iucv_sock *iucv = iucv_sk(sk);
620         struct sk_buff *skb;
621         struct iucv_message txmsg;
622         char user_id[9];
623         char appl_id[9];
624         int err;
625
626         err = sock_error(sk);
627         if (err)
628                 return err;
629
630         if (msg->msg_flags & MSG_OOB)
631                 return -EOPNOTSUPP;
632
633         lock_sock(sk);
634
635         if (sk->sk_shutdown & SEND_SHUTDOWN) {
636                 err = -EPIPE;
637                 goto out;
638         }
639
640         if (sk->sk_state == IUCV_CONNECTED) {
641                 if (!(skb = sock_alloc_send_skb(sk, len,
642                                                 msg->msg_flags & MSG_DONTWAIT,
643                                                 &err)))
644                         goto out;
645
646                 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
647                         err = -EFAULT;
648                         goto fail;
649                 }
650
651                 txmsg.class = 0;
652                 memcpy(&txmsg.class, skb->data, skb->len >= 4 ? 4 : skb->len);
653                 txmsg.tag = iucv->send_tag++;
654                 memcpy(skb->cb, &txmsg.tag, 4);
655                 skb_queue_tail(&iucv->send_skb_q, skb);
656                 err = iucv_message_send(iucv->path, &txmsg, 0, 0,
657                                         (void *) skb->data, skb->len);
658                 if (err) {
659                         if (err == 3) {
660                                 user_id[8] = 0;
661                                 memcpy(user_id, iucv->dst_user_id, 8);
662                                 appl_id[8] = 0;
663                                 memcpy(appl_id, iucv->dst_name, 8);
664                                 pr_err("Application %s on z/VM guest %s"
665                                        " exceeds message limit\n",
666                                        user_id, appl_id);
667                         }
668                         skb_unlink(skb, &iucv->send_skb_q);
669                         err = -EPIPE;
670                         goto fail;
671                 }
672
673         } else {
674                 err = -ENOTCONN;
675                 goto out;
676         }
677
678         release_sock(sk);
679         return len;
680
681 fail:
682         kfree_skb(skb);
683 out:
684         release_sock(sk);
685         return err;
686 }
687
688 static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
689 {
690         int dataleft, size, copied = 0;
691         struct sk_buff *nskb;
692
693         dataleft = len;
694         while (dataleft) {
695                 if (dataleft >= sk->sk_rcvbuf / 4)
696                         size = sk->sk_rcvbuf / 4;
697                 else
698                         size = dataleft;
699
700                 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
701                 if (!nskb)
702                         return -ENOMEM;
703
704                 memcpy(nskb->data, skb->data + copied, size);
705                 copied += size;
706                 dataleft -= size;
707
708                 skb_reset_transport_header(nskb);
709                 skb_reset_network_header(nskb);
710                 nskb->len = size;
711
712                 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
713         }
714
715         return 0;
716 }
717
718 static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
719                                  struct iucv_path *path,
720                                  struct iucv_message *msg)
721 {
722         int rc;
723
724         if (msg->flags & IPRMDATA) {
725                 skb->data = NULL;
726                 skb->len = 0;
727         } else {
728                 rc = iucv_message_receive(path, msg, 0, skb->data,
729                                           msg->length, NULL);
730                 if (rc) {
731                         kfree_skb(skb);
732                         return;
733                 }
734                 if (skb->truesize >= sk->sk_rcvbuf / 4) {
735                         rc = iucv_fragment_skb(sk, skb, msg->length);
736                         kfree_skb(skb);
737                         skb = NULL;
738                         if (rc) {
739                                 iucv_path_sever(path, NULL);
740                                 return;
741                         }
742                         skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
743                 } else {
744                         skb_reset_transport_header(skb);
745                         skb_reset_network_header(skb);
746                         skb->len = msg->length;
747                 }
748         }
749
750         if (sock_queue_rcv_skb(sk, skb))
751                 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
752 }
753
754 static void iucv_process_message_q(struct sock *sk)
755 {
756         struct iucv_sock *iucv = iucv_sk(sk);
757         struct sk_buff *skb;
758         struct sock_msg_q *p, *n;
759
760         list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
761                 skb = alloc_skb(p->msg.length, GFP_ATOMIC | GFP_DMA);
762                 if (!skb)
763                         break;
764                 iucv_process_message(sk, skb, p->path, &p->msg);
765                 list_del(&p->list);
766                 kfree(p);
767                 if (!skb_queue_empty(&iucv->backlog_skb_q))
768                         break;
769         }
770 }
771
772 static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
773                              struct msghdr *msg, size_t len, int flags)
774 {
775         int noblock = flags & MSG_DONTWAIT;
776         struct sock *sk = sock->sk;
777         struct iucv_sock *iucv = iucv_sk(sk);
778         int target, copied = 0;
779         struct sk_buff *skb, *rskb, *cskb;
780         int err = 0;
781
782         if ((sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED) &&
783             skb_queue_empty(&iucv->backlog_skb_q) &&
784             skb_queue_empty(&sk->sk_receive_queue) &&
785             list_empty(&iucv->message_q.list))
786                 return 0;
787
788         if (flags & (MSG_OOB))
789                 return -EOPNOTSUPP;
790
791         target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
792
793         skb = skb_recv_datagram(sk, flags, noblock, &err);
794         if (!skb) {
795                 if (sk->sk_shutdown & RCV_SHUTDOWN)
796                         return 0;
797                 return err;
798         }
799
800         copied = min_t(unsigned int, skb->len, len);
801
802         cskb = skb;
803         if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) {
804                 skb_queue_head(&sk->sk_receive_queue, skb);
805                 if (copied == 0)
806                         return -EFAULT;
807                 goto done;
808         }
809
810         len -= copied;
811
812         /* Mark read part of skb as used */
813         if (!(flags & MSG_PEEK)) {
814                 skb_pull(skb, copied);
815
816                 if (skb->len) {
817                         skb_queue_head(&sk->sk_receive_queue, skb);
818                         goto done;
819                 }
820
821                 kfree_skb(skb);
822
823                 /* Queue backlog skbs */
824                 rskb = skb_dequeue(&iucv->backlog_skb_q);
825                 while (rskb) {
826                         if (sock_queue_rcv_skb(sk, rskb)) {
827                                 skb_queue_head(&iucv->backlog_skb_q,
828                                                 rskb);
829                                 break;
830                         } else {
831                                 rskb = skb_dequeue(&iucv->backlog_skb_q);
832                         }
833                 }
834                 if (skb_queue_empty(&iucv->backlog_skb_q)) {
835                         spin_lock_bh(&iucv->message_q.lock);
836                         if (!list_empty(&iucv->message_q.list))
837                                 iucv_process_message_q(sk);
838                         spin_unlock_bh(&iucv->message_q.lock);
839                 }
840
841         } else
842                 skb_queue_head(&sk->sk_receive_queue, skb);
843
844 done:
845         return err ? : copied;
846 }
847
848 static inline unsigned int iucv_accept_poll(struct sock *parent)
849 {
850         struct iucv_sock *isk, *n;
851         struct sock *sk;
852
853         list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
854                 sk = (struct sock *) isk;
855
856                 if (sk->sk_state == IUCV_CONNECTED)
857                         return POLLIN | POLLRDNORM;
858         }
859
860         return 0;
861 }
862
863 unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
864                             poll_table *wait)
865 {
866         struct sock *sk = sock->sk;
867         unsigned int mask = 0;
868
869         poll_wait(file, sk->sk_sleep, wait);
870
871         if (sk->sk_state == IUCV_LISTEN)
872                 return iucv_accept_poll(sk);
873
874         if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
875                 mask |= POLLERR;
876
877         if (sk->sk_shutdown & RCV_SHUTDOWN)
878                 mask |= POLLRDHUP;
879
880         if (sk->sk_shutdown == SHUTDOWN_MASK)
881                 mask |= POLLHUP;
882
883         if (!skb_queue_empty(&sk->sk_receive_queue) ||
884             (sk->sk_shutdown & RCV_SHUTDOWN))
885                 mask |= POLLIN | POLLRDNORM;
886
887         if (sk->sk_state == IUCV_CLOSED)
888                 mask |= POLLHUP;
889
890         if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED)
891                 mask |= POLLIN;
892
893         if (sock_writeable(sk))
894                 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
895         else
896                 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
897
898         return mask;
899 }
900
901 static int iucv_sock_shutdown(struct socket *sock, int how)
902 {
903         struct sock *sk = sock->sk;
904         struct iucv_sock *iucv = iucv_sk(sk);
905         struct iucv_message txmsg;
906         int err = 0;
907         u8 prmmsg[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
908
909         how++;
910
911         if ((how & ~SHUTDOWN_MASK) || !how)
912                 return -EINVAL;
913
914         lock_sock(sk);
915         switch (sk->sk_state) {
916         case IUCV_CLOSED:
917                 err = -ENOTCONN;
918                 goto fail;
919
920         default:
921                 sk->sk_shutdown |= how;
922                 break;
923         }
924
925         if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
926                 txmsg.class = 0;
927                 txmsg.tag = 0;
928                 err = iucv_message_send(iucv->path, &txmsg, IUCV_IPRMDATA, 0,
929                                         (void *) prmmsg, 8);
930                 if (err) {
931                         switch (err) {
932                         case 1:
933                                 err = -ENOTCONN;
934                                 break;
935                         case 2:
936                                 err = -ECONNRESET;
937                                 break;
938                         default:
939                                 err = -ENOTCONN;
940                                 break;
941                         }
942                 }
943         }
944
945         if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
946                 err = iucv_path_quiesce(iucv_sk(sk)->path, NULL);
947                 if (err)
948                         err = -ENOTCONN;
949
950                 skb_queue_purge(&sk->sk_receive_queue);
951         }
952
953         /* Wake up anyone sleeping in poll */
954         sk->sk_state_change(sk);
955
956 fail:
957         release_sock(sk);
958         return err;
959 }
960
961 static int iucv_sock_release(struct socket *sock)
962 {
963         struct sock *sk = sock->sk;
964         int err = 0;
965
966         if (!sk)
967                 return 0;
968
969         iucv_sock_close(sk);
970
971         /* Unregister with IUCV base support */
972         if (iucv_sk(sk)->path) {
973                 iucv_path_sever(iucv_sk(sk)->path, NULL);
974                 iucv_path_free(iucv_sk(sk)->path);
975                 iucv_sk(sk)->path = NULL;
976         }
977
978         sock_orphan(sk);
979         iucv_sock_kill(sk);
980         return err;
981 }
982
983 /* Callback wrappers - called from iucv base support */
984 static int iucv_callback_connreq(struct iucv_path *path,
985                                  u8 ipvmid[8], u8 ipuser[16])
986 {
987         unsigned char user_data[16];
988         unsigned char nuser_data[16];
989         unsigned char src_name[8];
990         struct hlist_node *node;
991         struct sock *sk, *nsk;
992         struct iucv_sock *iucv, *niucv;
993         int err;
994
995         memcpy(src_name, ipuser, 8);
996         EBCASC(src_name, 8);
997         /* Find out if this path belongs to af_iucv. */
998         read_lock(&iucv_sk_list.lock);
999         iucv = NULL;
1000         sk = NULL;
1001         sk_for_each(sk, node, &iucv_sk_list.head)
1002                 if (sk->sk_state == IUCV_LISTEN &&
1003                     !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1004                         /*
1005                          * Found a listening socket with
1006                          * src_name == ipuser[0-7].
1007                          */
1008                         iucv = iucv_sk(sk);
1009                         break;
1010                 }
1011         read_unlock(&iucv_sk_list.lock);
1012         if (!iucv)
1013                 /* No socket found, not one of our paths. */
1014                 return -EINVAL;
1015
1016         bh_lock_sock(sk);
1017
1018         /* Check if parent socket is listening */
1019         low_nmcpy(user_data, iucv->src_name);
1020         high_nmcpy(user_data, iucv->dst_name);
1021         ASCEBC(user_data, sizeof(user_data));
1022         if (sk->sk_state != IUCV_LISTEN) {
1023                 err = iucv_path_sever(path, user_data);
1024                 goto fail;
1025         }
1026
1027         /* Check for backlog size */
1028         if (sk_acceptq_is_full(sk)) {
1029                 err = iucv_path_sever(path, user_data);
1030                 goto fail;
1031         }
1032
1033         /* Create the new socket */
1034         nsk = iucv_sock_alloc(NULL, SOCK_STREAM, GFP_ATOMIC);
1035         if (!nsk) {
1036                 err = iucv_path_sever(path, user_data);
1037                 goto fail;
1038         }
1039
1040         niucv = iucv_sk(nsk);
1041         iucv_sock_init(nsk, sk);
1042
1043         /* Set the new iucv_sock */
1044         memcpy(niucv->dst_name, ipuser + 8, 8);
1045         EBCASC(niucv->dst_name, 8);
1046         memcpy(niucv->dst_user_id, ipvmid, 8);
1047         memcpy(niucv->src_name, iucv->src_name, 8);
1048         memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1049         niucv->path = path;
1050
1051         /* Call iucv_accept */
1052         high_nmcpy(nuser_data, ipuser + 8);
1053         memcpy(nuser_data + 8, niucv->src_name, 8);
1054         ASCEBC(nuser_data + 8, 8);
1055
1056         path->msglim = IUCV_QUEUELEN_DEFAULT;
1057         err = iucv_path_accept(path, &af_iucv_handler, nuser_data, nsk);
1058         if (err) {
1059                 err = iucv_path_sever(path, user_data);
1060                 goto fail;
1061         }
1062
1063         iucv_accept_enqueue(sk, nsk);
1064
1065         /* Wake up accept */
1066         nsk->sk_state = IUCV_CONNECTED;
1067         sk->sk_data_ready(sk, 1);
1068         err = 0;
1069 fail:
1070         bh_unlock_sock(sk);
1071         return 0;
1072 }
1073
1074 static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1075 {
1076         struct sock *sk = path->private;
1077
1078         sk->sk_state = IUCV_CONNECTED;
1079         sk->sk_state_change(sk);
1080 }
1081
1082 static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1083 {
1084         struct sock *sk = path->private;
1085         struct iucv_sock *iucv = iucv_sk(sk);
1086         struct sk_buff *skb;
1087         struct sock_msg_q *save_msg;
1088         int len;
1089
1090         if (sk->sk_shutdown & RCV_SHUTDOWN)
1091                 return;
1092
1093         if (!list_empty(&iucv->message_q.list) ||
1094             !skb_queue_empty(&iucv->backlog_skb_q))
1095                 goto save_message;
1096
1097         len = atomic_read(&sk->sk_rmem_alloc);
1098         len += msg->length + sizeof(struct sk_buff);
1099         if (len > sk->sk_rcvbuf)
1100                 goto save_message;
1101
1102         skb = alloc_skb(msg->length, GFP_ATOMIC | GFP_DMA);
1103         if (!skb)
1104                 goto save_message;
1105
1106         spin_lock(&iucv->message_q.lock);
1107         iucv_process_message(sk, skb, path, msg);
1108         spin_unlock(&iucv->message_q.lock);
1109
1110         return;
1111
1112 save_message:
1113         save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
1114         if (!save_msg)
1115                 return;
1116         save_msg->path = path;
1117         save_msg->msg = *msg;
1118
1119         spin_lock(&iucv->message_q.lock);
1120         list_add_tail(&save_msg->list, &iucv->message_q.list);
1121         spin_unlock(&iucv->message_q.lock);
1122 }
1123
1124 static void iucv_callback_txdone(struct iucv_path *path,
1125                                  struct iucv_message *msg)
1126 {
1127         struct sock *sk = path->private;
1128         struct sk_buff *this = NULL;
1129         struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1130         struct sk_buff *list_skb = list->next;
1131         unsigned long flags;
1132
1133         if (!skb_queue_empty(list)) {
1134                 spin_lock_irqsave(&list->lock, flags);
1135
1136                 while (list_skb != (struct sk_buff *)list) {
1137                         if (!memcmp(&msg->tag, list_skb->cb, 4)) {
1138                                 this = list_skb;
1139                                 break;
1140                         }
1141                         list_skb = list_skb->next;
1142                 }
1143                 if (this)
1144                         __skb_unlink(this, list);
1145
1146                 spin_unlock_irqrestore(&list->lock, flags);
1147
1148                 if (this)
1149                         kfree_skb(this);
1150         }
1151         BUG_ON(!this);
1152
1153         if (sk->sk_state == IUCV_CLOSING) {
1154                 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1155                         sk->sk_state = IUCV_CLOSED;
1156                         sk->sk_state_change(sk);
1157                 }
1158         }
1159
1160 }
1161
1162 static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1163 {
1164         struct sock *sk = path->private;
1165
1166         if (!list_empty(&iucv_sk(sk)->accept_q))
1167                 sk->sk_state = IUCV_SEVERED;
1168         else
1169                 sk->sk_state = IUCV_DISCONN;
1170
1171         sk->sk_state_change(sk);
1172 }
1173
1174 static struct proto_ops iucv_sock_ops = {
1175         .family         = PF_IUCV,
1176         .owner          = THIS_MODULE,
1177         .release        = iucv_sock_release,
1178         .bind           = iucv_sock_bind,
1179         .connect        = iucv_sock_connect,
1180         .listen         = iucv_sock_listen,
1181         .accept         = iucv_sock_accept,
1182         .getname        = iucv_sock_getname,
1183         .sendmsg        = iucv_sock_sendmsg,
1184         .recvmsg        = iucv_sock_recvmsg,
1185         .poll           = iucv_sock_poll,
1186         .ioctl          = sock_no_ioctl,
1187         .mmap           = sock_no_mmap,
1188         .socketpair     = sock_no_socketpair,
1189         .shutdown       = iucv_sock_shutdown,
1190         .setsockopt     = sock_no_setsockopt,
1191         .getsockopt     = sock_no_getsockopt
1192 };
1193
1194 static struct net_proto_family iucv_sock_family_ops = {
1195         .family = AF_IUCV,
1196         .owner  = THIS_MODULE,
1197         .create = iucv_sock_create,
1198 };
1199
1200 static int __init afiucv_init(void)
1201 {
1202         int err;
1203
1204         if (!MACHINE_IS_VM) {
1205                 pr_err("The af_iucv module cannot be loaded"
1206                        " without z/VM\n");
1207                 err = -EPROTONOSUPPORT;
1208                 goto out;
1209         }
1210         cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
1211         if (unlikely(err)) {
1212                 WARN_ON(err);
1213                 err = -EPROTONOSUPPORT;
1214                 goto out;
1215         }
1216
1217         err = iucv_register(&af_iucv_handler, 0);
1218         if (err)
1219                 goto out;
1220         err = proto_register(&iucv_proto, 0);
1221         if (err)
1222                 goto out_iucv;
1223         err = sock_register(&iucv_sock_family_ops);
1224         if (err)
1225                 goto out_proto;
1226         return 0;
1227
1228 out_proto:
1229         proto_unregister(&iucv_proto);
1230 out_iucv:
1231         iucv_unregister(&af_iucv_handler, 0);
1232 out:
1233         return err;
1234 }
1235
1236 static void __exit afiucv_exit(void)
1237 {
1238         sock_unregister(PF_IUCV);
1239         proto_unregister(&iucv_proto);
1240         iucv_unregister(&af_iucv_handler, 0);
1241 }
1242
1243 module_init(afiucv_init);
1244 module_exit(afiucv_exit);
1245
1246 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
1247 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
1248 MODULE_VERSION(VERSION);
1249 MODULE_LICENSE("GPL");
1250 MODULE_ALIAS_NETPROTO(PF_IUCV);